1ab7ac4ebSTom Herbert #include <linux/bpf.h> 2ab7ac4ebSTom Herbert #include <linux/errno.h> 3ab7ac4ebSTom Herbert #include <linux/errqueue.h> 4ab7ac4ebSTom Herbert #include <linux/file.h> 5ab7ac4ebSTom Herbert #include <linux/in.h> 6ab7ac4ebSTom Herbert #include <linux/kernel.h> 7ab7ac4ebSTom Herbert #include <linux/module.h> 8ab7ac4ebSTom Herbert #include <linux/net.h> 9ab7ac4ebSTom Herbert #include <linux/netdevice.h> 10ab7ac4ebSTom Herbert #include <linux/poll.h> 11ab7ac4ebSTom Herbert #include <linux/rculist.h> 12ab7ac4ebSTom Herbert #include <linux/skbuff.h> 13ab7ac4ebSTom Herbert #include <linux/socket.h> 14ab7ac4ebSTom Herbert #include <linux/uaccess.h> 15ab7ac4ebSTom Herbert #include <linux/workqueue.h> 16ab7ac4ebSTom Herbert #include <net/kcm.h> 17ab7ac4ebSTom Herbert #include <net/netns/generic.h> 18ab7ac4ebSTom Herbert #include <net/sock.h> 19ab7ac4ebSTom Herbert #include <net/tcp.h> 20ab7ac4ebSTom Herbert #include <uapi/linux/kcm.h> 21ab7ac4ebSTom Herbert 22ab7ac4ebSTom Herbert unsigned int kcm_net_id; 23ab7ac4ebSTom Herbert 24ab7ac4ebSTom Herbert static struct kmem_cache *kcm_psockp __read_mostly; 25ab7ac4ebSTom Herbert static struct kmem_cache *kcm_muxp __read_mostly; 26ab7ac4ebSTom Herbert static struct workqueue_struct *kcm_wq; 27ab7ac4ebSTom Herbert 28ab7ac4ebSTom Herbert static inline struct kcm_sock *kcm_sk(const struct sock *sk) 29ab7ac4ebSTom Herbert { 30ab7ac4ebSTom Herbert return (struct kcm_sock *)sk; 31ab7ac4ebSTom Herbert } 32ab7ac4ebSTom Herbert 33ab7ac4ebSTom Herbert static inline struct kcm_tx_msg *kcm_tx_msg(struct sk_buff *skb) 34ab7ac4ebSTom Herbert { 35ab7ac4ebSTom Herbert return (struct kcm_tx_msg *)skb->cb; 36ab7ac4ebSTom Herbert } 37ab7ac4ebSTom Herbert 38ab7ac4ebSTom Herbert static inline struct kcm_rx_msg *kcm_rx_msg(struct sk_buff *skb) 39ab7ac4ebSTom Herbert { 40ab7ac4ebSTom Herbert return (struct kcm_rx_msg *)((void *)skb->cb + 41ab7ac4ebSTom Herbert offsetof(struct qdisc_skb_cb, data)); 42ab7ac4ebSTom Herbert } 43ab7ac4ebSTom Herbert 44ab7ac4ebSTom Herbert static void report_csk_error(struct sock *csk, int err) 45ab7ac4ebSTom Herbert { 46ab7ac4ebSTom Herbert csk->sk_err = EPIPE; 47ab7ac4ebSTom Herbert csk->sk_error_report(csk); 48ab7ac4ebSTom Herbert } 49ab7ac4ebSTom Herbert 50ab7ac4ebSTom Herbert /* Callback lock held */ 51ab7ac4ebSTom Herbert static void kcm_abort_rx_psock(struct kcm_psock *psock, int err, 52ab7ac4ebSTom Herbert struct sk_buff *skb) 53ab7ac4ebSTom Herbert { 54ab7ac4ebSTom Herbert struct sock *csk = psock->sk; 55ab7ac4ebSTom Herbert 56ab7ac4ebSTom Herbert /* Unrecoverable error in receive */ 57ab7ac4ebSTom Herbert 58ab7ac4ebSTom Herbert if (psock->rx_stopped) 59ab7ac4ebSTom Herbert return; 60ab7ac4ebSTom Herbert 61ab7ac4ebSTom Herbert psock->rx_stopped = 1; 62cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_aborts); 63ab7ac4ebSTom Herbert 64ab7ac4ebSTom Herbert /* Report an error on the lower socket */ 65ab7ac4ebSTom Herbert report_csk_error(csk, err); 66ab7ac4ebSTom Herbert } 67ab7ac4ebSTom Herbert 68ab7ac4ebSTom Herbert static void kcm_abort_tx_psock(struct kcm_psock *psock, int err, 69ab7ac4ebSTom Herbert bool wakeup_kcm) 70ab7ac4ebSTom Herbert { 71ab7ac4ebSTom Herbert struct sock *csk = psock->sk; 72ab7ac4ebSTom Herbert struct kcm_mux *mux = psock->mux; 73ab7ac4ebSTom Herbert 74ab7ac4ebSTom Herbert /* Unrecoverable error in transmit */ 75ab7ac4ebSTom Herbert 76ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 77ab7ac4ebSTom Herbert 78ab7ac4ebSTom Herbert if (psock->tx_stopped) { 79ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 80ab7ac4ebSTom Herbert return; 81ab7ac4ebSTom Herbert } 82ab7ac4ebSTom Herbert 83ab7ac4ebSTom Herbert psock->tx_stopped = 1; 84cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.tx_aborts); 85ab7ac4ebSTom Herbert 86ab7ac4ebSTom Herbert if (!psock->tx_kcm) { 87ab7ac4ebSTom Herbert /* Take off psocks_avail list */ 88ab7ac4ebSTom Herbert list_del(&psock->psock_avail_list); 89ab7ac4ebSTom Herbert } else if (wakeup_kcm) { 90ab7ac4ebSTom Herbert /* In this case psock is being aborted while outside of 91ab7ac4ebSTom Herbert * write_msgs and psock is reserved. Schedule tx_work 92ab7ac4ebSTom Herbert * to handle the failure there. Need to commit tx_stopped 93ab7ac4ebSTom Herbert * before queuing work. 94ab7ac4ebSTom Herbert */ 95ab7ac4ebSTom Herbert smp_mb(); 96ab7ac4ebSTom Herbert 97ab7ac4ebSTom Herbert queue_work(kcm_wq, &psock->tx_kcm->tx_work); 98ab7ac4ebSTom Herbert } 99ab7ac4ebSTom Herbert 100ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 101ab7ac4ebSTom Herbert 102ab7ac4ebSTom Herbert /* Report error on lower socket */ 103ab7ac4ebSTom Herbert report_csk_error(csk, err); 104ab7ac4ebSTom Herbert } 105ab7ac4ebSTom Herbert 106cd6e111bSTom Herbert /* RX mux lock held. */ 107cd6e111bSTom Herbert static void kcm_update_rx_mux_stats(struct kcm_mux *mux, 108cd6e111bSTom Herbert struct kcm_psock *psock) 109cd6e111bSTom Herbert { 110cd6e111bSTom Herbert KCM_STATS_ADD(mux->stats.rx_bytes, 111cd6e111bSTom Herbert psock->stats.rx_bytes - psock->saved_rx_bytes); 112cd6e111bSTom Herbert mux->stats.rx_msgs += 113cd6e111bSTom Herbert psock->stats.rx_msgs - psock->saved_rx_msgs; 114cd6e111bSTom Herbert psock->saved_rx_msgs = psock->stats.rx_msgs; 115cd6e111bSTom Herbert psock->saved_rx_bytes = psock->stats.rx_bytes; 116cd6e111bSTom Herbert } 117cd6e111bSTom Herbert 118cd6e111bSTom Herbert static void kcm_update_tx_mux_stats(struct kcm_mux *mux, 119cd6e111bSTom Herbert struct kcm_psock *psock) 120cd6e111bSTom Herbert { 121cd6e111bSTom Herbert KCM_STATS_ADD(mux->stats.tx_bytes, 122cd6e111bSTom Herbert psock->stats.tx_bytes - psock->saved_tx_bytes); 123cd6e111bSTom Herbert mux->stats.tx_msgs += 124cd6e111bSTom Herbert psock->stats.tx_msgs - psock->saved_tx_msgs; 125cd6e111bSTom Herbert psock->saved_tx_msgs = psock->stats.tx_msgs; 126cd6e111bSTom Herbert psock->saved_tx_bytes = psock->stats.tx_bytes; 127cd6e111bSTom Herbert } 128cd6e111bSTom Herbert 129ab7ac4ebSTom Herbert static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 130ab7ac4ebSTom Herbert 131ab7ac4ebSTom Herbert /* KCM is ready to receive messages on its queue-- either the KCM is new or 132ab7ac4ebSTom Herbert * has become unblocked after being blocked on full socket buffer. Queue any 133ab7ac4ebSTom Herbert * pending ready messages on a psock. RX mux lock held. 134ab7ac4ebSTom Herbert */ 135ab7ac4ebSTom Herbert static void kcm_rcv_ready(struct kcm_sock *kcm) 136ab7ac4ebSTom Herbert { 137ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 138ab7ac4ebSTom Herbert struct kcm_psock *psock; 139ab7ac4ebSTom Herbert struct sk_buff *skb; 140ab7ac4ebSTom Herbert 141ab7ac4ebSTom Herbert if (unlikely(kcm->rx_wait || kcm->rx_psock || kcm->rx_disabled)) 142ab7ac4ebSTom Herbert return; 143ab7ac4ebSTom Herbert 144ab7ac4ebSTom Herbert while (unlikely((skb = __skb_dequeue(&mux->rx_hold_queue)))) { 145ab7ac4ebSTom Herbert if (kcm_queue_rcv_skb(&kcm->sk, skb)) { 146ab7ac4ebSTom Herbert /* Assuming buffer limit has been reached */ 147ab7ac4ebSTom Herbert skb_queue_head(&mux->rx_hold_queue, skb); 148ab7ac4ebSTom Herbert WARN_ON(!sk_rmem_alloc_get(&kcm->sk)); 149ab7ac4ebSTom Herbert return; 150ab7ac4ebSTom Herbert } 151ab7ac4ebSTom Herbert } 152ab7ac4ebSTom Herbert 153ab7ac4ebSTom Herbert while (!list_empty(&mux->psocks_ready)) { 154ab7ac4ebSTom Herbert psock = list_first_entry(&mux->psocks_ready, struct kcm_psock, 155ab7ac4ebSTom Herbert psock_ready_list); 156ab7ac4ebSTom Herbert 157ab7ac4ebSTom Herbert if (kcm_queue_rcv_skb(&kcm->sk, psock->ready_rx_msg)) { 158ab7ac4ebSTom Herbert /* Assuming buffer limit has been reached */ 159ab7ac4ebSTom Herbert WARN_ON(!sk_rmem_alloc_get(&kcm->sk)); 160ab7ac4ebSTom Herbert return; 161ab7ac4ebSTom Herbert } 162ab7ac4ebSTom Herbert 163ab7ac4ebSTom Herbert /* Consumed the ready message on the psock. Schedule rx_work to 164ab7ac4ebSTom Herbert * get more messages. 165ab7ac4ebSTom Herbert */ 166ab7ac4ebSTom Herbert list_del(&psock->psock_ready_list); 167ab7ac4ebSTom Herbert psock->ready_rx_msg = NULL; 168ab7ac4ebSTom Herbert 169ab7ac4ebSTom Herbert /* Commit clearing of ready_rx_msg for queuing work */ 170ab7ac4ebSTom Herbert smp_mb(); 171ab7ac4ebSTom Herbert 172ab7ac4ebSTom Herbert queue_work(kcm_wq, &psock->rx_work); 173ab7ac4ebSTom Herbert } 174ab7ac4ebSTom Herbert 175ab7ac4ebSTom Herbert /* Buffer limit is okay now, add to ready list */ 176ab7ac4ebSTom Herbert list_add_tail(&kcm->wait_rx_list, 177ab7ac4ebSTom Herbert &kcm->mux->kcm_rx_waiters); 178ab7ac4ebSTom Herbert kcm->rx_wait = true; 179ab7ac4ebSTom Herbert } 180ab7ac4ebSTom Herbert 181ab7ac4ebSTom Herbert static void kcm_rfree(struct sk_buff *skb) 182ab7ac4ebSTom Herbert { 183ab7ac4ebSTom Herbert struct sock *sk = skb->sk; 184ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sk); 185ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 186ab7ac4ebSTom Herbert unsigned int len = skb->truesize; 187ab7ac4ebSTom Herbert 188ab7ac4ebSTom Herbert sk_mem_uncharge(sk, len); 189ab7ac4ebSTom Herbert atomic_sub(len, &sk->sk_rmem_alloc); 190ab7ac4ebSTom Herbert 191ab7ac4ebSTom Herbert /* For reading rx_wait and rx_psock without holding lock */ 192ab7ac4ebSTom Herbert smp_mb__after_atomic(); 193ab7ac4ebSTom Herbert 194ab7ac4ebSTom Herbert if (!kcm->rx_wait && !kcm->rx_psock && 195ab7ac4ebSTom Herbert sk_rmem_alloc_get(sk) < sk->sk_rcvlowat) { 196ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 197ab7ac4ebSTom Herbert kcm_rcv_ready(kcm); 198ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 199ab7ac4ebSTom Herbert } 200ab7ac4ebSTom Herbert } 201ab7ac4ebSTom Herbert 202ab7ac4ebSTom Herbert static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 203ab7ac4ebSTom Herbert { 204ab7ac4ebSTom Herbert struct sk_buff_head *list = &sk->sk_receive_queue; 205ab7ac4ebSTom Herbert 206ab7ac4ebSTom Herbert if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) 207ab7ac4ebSTom Herbert return -ENOMEM; 208ab7ac4ebSTom Herbert 209ab7ac4ebSTom Herbert if (!sk_rmem_schedule(sk, skb, skb->truesize)) 210ab7ac4ebSTom Herbert return -ENOBUFS; 211ab7ac4ebSTom Herbert 212ab7ac4ebSTom Herbert skb->dev = NULL; 213ab7ac4ebSTom Herbert 214ab7ac4ebSTom Herbert skb_orphan(skb); 215ab7ac4ebSTom Herbert skb->sk = sk; 216ab7ac4ebSTom Herbert skb->destructor = kcm_rfree; 217ab7ac4ebSTom Herbert atomic_add(skb->truesize, &sk->sk_rmem_alloc); 218ab7ac4ebSTom Herbert sk_mem_charge(sk, skb->truesize); 219ab7ac4ebSTom Herbert 220ab7ac4ebSTom Herbert skb_queue_tail(list, skb); 221ab7ac4ebSTom Herbert 222ab7ac4ebSTom Herbert if (!sock_flag(sk, SOCK_DEAD)) 223ab7ac4ebSTom Herbert sk->sk_data_ready(sk); 224ab7ac4ebSTom Herbert 225ab7ac4ebSTom Herbert return 0; 226ab7ac4ebSTom Herbert } 227ab7ac4ebSTom Herbert 228ab7ac4ebSTom Herbert /* Requeue received messages for a kcm socket to other kcm sockets. This is 229ab7ac4ebSTom Herbert * called with a kcm socket is receive disabled. 230ab7ac4ebSTom Herbert * RX mux lock held. 231ab7ac4ebSTom Herbert */ 232ab7ac4ebSTom Herbert static void requeue_rx_msgs(struct kcm_mux *mux, struct sk_buff_head *head) 233ab7ac4ebSTom Herbert { 234ab7ac4ebSTom Herbert struct sk_buff *skb; 235ab7ac4ebSTom Herbert struct kcm_sock *kcm; 236ab7ac4ebSTom Herbert 237ab7ac4ebSTom Herbert while ((skb = __skb_dequeue(head))) { 238ab7ac4ebSTom Herbert /* Reset destructor to avoid calling kcm_rcv_ready */ 239ab7ac4ebSTom Herbert skb->destructor = sock_rfree; 240ab7ac4ebSTom Herbert skb_orphan(skb); 241ab7ac4ebSTom Herbert try_again: 242ab7ac4ebSTom Herbert if (list_empty(&mux->kcm_rx_waiters)) { 243ab7ac4ebSTom Herbert skb_queue_tail(&mux->rx_hold_queue, skb); 244ab7ac4ebSTom Herbert continue; 245ab7ac4ebSTom Herbert } 246ab7ac4ebSTom Herbert 247ab7ac4ebSTom Herbert kcm = list_first_entry(&mux->kcm_rx_waiters, 248ab7ac4ebSTom Herbert struct kcm_sock, wait_rx_list); 249ab7ac4ebSTom Herbert 250ab7ac4ebSTom Herbert if (kcm_queue_rcv_skb(&kcm->sk, skb)) { 251ab7ac4ebSTom Herbert /* Should mean socket buffer full */ 252ab7ac4ebSTom Herbert list_del(&kcm->wait_rx_list); 253ab7ac4ebSTom Herbert kcm->rx_wait = false; 254ab7ac4ebSTom Herbert 255ab7ac4ebSTom Herbert /* Commit rx_wait to read in kcm_free */ 256ab7ac4ebSTom Herbert smp_wmb(); 257ab7ac4ebSTom Herbert 258ab7ac4ebSTom Herbert goto try_again; 259ab7ac4ebSTom Herbert } 260ab7ac4ebSTom Herbert } 261ab7ac4ebSTom Herbert } 262ab7ac4ebSTom Herbert 263ab7ac4ebSTom Herbert /* Lower sock lock held */ 264ab7ac4ebSTom Herbert static struct kcm_sock *reserve_rx_kcm(struct kcm_psock *psock, 265ab7ac4ebSTom Herbert struct sk_buff *head) 266ab7ac4ebSTom Herbert { 267ab7ac4ebSTom Herbert struct kcm_mux *mux = psock->mux; 268ab7ac4ebSTom Herbert struct kcm_sock *kcm; 269ab7ac4ebSTom Herbert 270ab7ac4ebSTom Herbert WARN_ON(psock->ready_rx_msg); 271ab7ac4ebSTom Herbert 272ab7ac4ebSTom Herbert if (psock->rx_kcm) 273ab7ac4ebSTom Herbert return psock->rx_kcm; 274ab7ac4ebSTom Herbert 275ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 276ab7ac4ebSTom Herbert 277ab7ac4ebSTom Herbert if (psock->rx_kcm) { 278ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 279ab7ac4ebSTom Herbert return psock->rx_kcm; 280ab7ac4ebSTom Herbert } 281ab7ac4ebSTom Herbert 282cd6e111bSTom Herbert kcm_update_rx_mux_stats(mux, psock); 283cd6e111bSTom Herbert 284ab7ac4ebSTom Herbert if (list_empty(&mux->kcm_rx_waiters)) { 285ab7ac4ebSTom Herbert psock->ready_rx_msg = head; 286ab7ac4ebSTom Herbert list_add_tail(&psock->psock_ready_list, 287ab7ac4ebSTom Herbert &mux->psocks_ready); 288ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 289ab7ac4ebSTom Herbert return NULL; 290ab7ac4ebSTom Herbert } 291ab7ac4ebSTom Herbert 292ab7ac4ebSTom Herbert kcm = list_first_entry(&mux->kcm_rx_waiters, 293ab7ac4ebSTom Herbert struct kcm_sock, wait_rx_list); 294ab7ac4ebSTom Herbert list_del(&kcm->wait_rx_list); 295ab7ac4ebSTom Herbert kcm->rx_wait = false; 296ab7ac4ebSTom Herbert 297ab7ac4ebSTom Herbert psock->rx_kcm = kcm; 298ab7ac4ebSTom Herbert kcm->rx_psock = psock; 299ab7ac4ebSTom Herbert 300ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 301ab7ac4ebSTom Herbert 302ab7ac4ebSTom Herbert return kcm; 303ab7ac4ebSTom Herbert } 304ab7ac4ebSTom Herbert 305ab7ac4ebSTom Herbert static void kcm_done(struct kcm_sock *kcm); 306ab7ac4ebSTom Herbert 307ab7ac4ebSTom Herbert static void kcm_done_work(struct work_struct *w) 308ab7ac4ebSTom Herbert { 309ab7ac4ebSTom Herbert kcm_done(container_of(w, struct kcm_sock, done_work)); 310ab7ac4ebSTom Herbert } 311ab7ac4ebSTom Herbert 312ab7ac4ebSTom Herbert /* Lower sock held */ 313ab7ac4ebSTom Herbert static void unreserve_rx_kcm(struct kcm_psock *psock, 314ab7ac4ebSTom Herbert bool rcv_ready) 315ab7ac4ebSTom Herbert { 316ab7ac4ebSTom Herbert struct kcm_sock *kcm = psock->rx_kcm; 317ab7ac4ebSTom Herbert struct kcm_mux *mux = psock->mux; 318ab7ac4ebSTom Herbert 319ab7ac4ebSTom Herbert if (!kcm) 320ab7ac4ebSTom Herbert return; 321ab7ac4ebSTom Herbert 322ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 323ab7ac4ebSTom Herbert 324ab7ac4ebSTom Herbert psock->rx_kcm = NULL; 325ab7ac4ebSTom Herbert kcm->rx_psock = NULL; 326ab7ac4ebSTom Herbert 327ab7ac4ebSTom Herbert /* Commit kcm->rx_psock before sk_rmem_alloc_get to sync with 328ab7ac4ebSTom Herbert * kcm_rfree 329ab7ac4ebSTom Herbert */ 330ab7ac4ebSTom Herbert smp_mb(); 331ab7ac4ebSTom Herbert 332ab7ac4ebSTom Herbert if (unlikely(kcm->done)) { 333ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 334ab7ac4ebSTom Herbert 335ab7ac4ebSTom Herbert /* Need to run kcm_done in a task since we need to qcquire 336ab7ac4ebSTom Herbert * callback locks which may already be held here. 337ab7ac4ebSTom Herbert */ 338ab7ac4ebSTom Herbert INIT_WORK(&kcm->done_work, kcm_done_work); 339ab7ac4ebSTom Herbert schedule_work(&kcm->done_work); 340ab7ac4ebSTom Herbert return; 341ab7ac4ebSTom Herbert } 342ab7ac4ebSTom Herbert 343ab7ac4ebSTom Herbert if (unlikely(kcm->rx_disabled)) { 344ab7ac4ebSTom Herbert requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue); 345ab7ac4ebSTom Herbert } else if (rcv_ready || unlikely(!sk_rmem_alloc_get(&kcm->sk))) { 346ab7ac4ebSTom Herbert /* Check for degenerative race with rx_wait that all 347ab7ac4ebSTom Herbert * data was dequeued (accounted for in kcm_rfree). 348ab7ac4ebSTom Herbert */ 349ab7ac4ebSTom Herbert kcm_rcv_ready(kcm); 350ab7ac4ebSTom Herbert } 351ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 352ab7ac4ebSTom Herbert } 353ab7ac4ebSTom Herbert 354ab7ac4ebSTom Herbert /* Macro to invoke filter function. */ 355ab7ac4ebSTom Herbert #define KCM_RUN_FILTER(prog, ctx) \ 356ab7ac4ebSTom Herbert (*prog->bpf_func)(ctx, prog->insnsi) 357ab7ac4ebSTom Herbert 358ab7ac4ebSTom Herbert /* Lower socket lock held */ 359ab7ac4ebSTom Herbert static int kcm_tcp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb, 360ab7ac4ebSTom Herbert unsigned int orig_offset, size_t orig_len) 361ab7ac4ebSTom Herbert { 362ab7ac4ebSTom Herbert struct kcm_psock *psock = (struct kcm_psock *)desc->arg.data; 363ab7ac4ebSTom Herbert struct kcm_rx_msg *rxm; 364ab7ac4ebSTom Herbert struct kcm_sock *kcm; 365ab7ac4ebSTom Herbert struct sk_buff *head, *skb; 366ab7ac4ebSTom Herbert size_t eaten = 0, cand_len; 367ab7ac4ebSTom Herbert ssize_t extra; 368ab7ac4ebSTom Herbert int err; 369ab7ac4ebSTom Herbert bool cloned_orig = false; 370ab7ac4ebSTom Herbert 371ab7ac4ebSTom Herbert if (psock->ready_rx_msg) 372ab7ac4ebSTom Herbert return 0; 373ab7ac4ebSTom Herbert 374ab7ac4ebSTom Herbert head = psock->rx_skb_head; 375ab7ac4ebSTom Herbert if (head) { 376ab7ac4ebSTom Herbert /* Message already in progress */ 377ab7ac4ebSTom Herbert 378ab7ac4ebSTom Herbert if (unlikely(orig_offset)) { 379ab7ac4ebSTom Herbert /* Getting data with a non-zero offset when a message is 380ab7ac4ebSTom Herbert * in progress is not expected. If it does happen, we 381ab7ac4ebSTom Herbert * need to clone and pull since we can't deal with 382ab7ac4ebSTom Herbert * offsets in the skbs for a message expect in the head. 383ab7ac4ebSTom Herbert */ 384ab7ac4ebSTom Herbert orig_skb = skb_clone(orig_skb, GFP_ATOMIC); 385ab7ac4ebSTom Herbert if (!orig_skb) { 386cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 387ab7ac4ebSTom Herbert desc->error = -ENOMEM; 388ab7ac4ebSTom Herbert return 0; 389ab7ac4ebSTom Herbert } 390ab7ac4ebSTom Herbert if (!pskb_pull(orig_skb, orig_offset)) { 391cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 392ab7ac4ebSTom Herbert kfree_skb(orig_skb); 393ab7ac4ebSTom Herbert desc->error = -ENOMEM; 394ab7ac4ebSTom Herbert return 0; 395ab7ac4ebSTom Herbert } 396ab7ac4ebSTom Herbert cloned_orig = true; 397ab7ac4ebSTom Herbert orig_offset = 0; 398ab7ac4ebSTom Herbert } 399ab7ac4ebSTom Herbert 400ab7ac4ebSTom Herbert if (!psock->rx_skb_nextp) { 401ab7ac4ebSTom Herbert /* We are going to append to the frags_list of head. 402ab7ac4ebSTom Herbert * Need to unshare the frag_list. 403ab7ac4ebSTom Herbert */ 404ab7ac4ebSTom Herbert err = skb_unclone(head, GFP_ATOMIC); 405ab7ac4ebSTom Herbert if (err) { 406cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 407ab7ac4ebSTom Herbert desc->error = err; 408ab7ac4ebSTom Herbert return 0; 409ab7ac4ebSTom Herbert } 410ab7ac4ebSTom Herbert 411ab7ac4ebSTom Herbert if (unlikely(skb_shinfo(head)->frag_list)) { 412ab7ac4ebSTom Herbert /* We can't append to an sk_buff that already 413ab7ac4ebSTom Herbert * has a frag_list. We create a new head, point 414ab7ac4ebSTom Herbert * the frag_list of that to the old head, and 415ab7ac4ebSTom Herbert * then are able to use the old head->next for 416ab7ac4ebSTom Herbert * appending to the message. 417ab7ac4ebSTom Herbert */ 418ab7ac4ebSTom Herbert if (WARN_ON(head->next)) { 419ab7ac4ebSTom Herbert desc->error = -EINVAL; 420ab7ac4ebSTom Herbert return 0; 421ab7ac4ebSTom Herbert } 422ab7ac4ebSTom Herbert 423ab7ac4ebSTom Herbert skb = alloc_skb(0, GFP_ATOMIC); 424ab7ac4ebSTom Herbert if (!skb) { 425cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 426ab7ac4ebSTom Herbert desc->error = -ENOMEM; 427ab7ac4ebSTom Herbert return 0; 428ab7ac4ebSTom Herbert } 429ab7ac4ebSTom Herbert skb->len = head->len; 430ab7ac4ebSTom Herbert skb->data_len = head->len; 431ab7ac4ebSTom Herbert skb->truesize = head->truesize; 432ab7ac4ebSTom Herbert *kcm_rx_msg(skb) = *kcm_rx_msg(head); 433ab7ac4ebSTom Herbert psock->rx_skb_nextp = &head->next; 434ab7ac4ebSTom Herbert skb_shinfo(skb)->frag_list = head; 435ab7ac4ebSTom Herbert psock->rx_skb_head = skb; 436ab7ac4ebSTom Herbert head = skb; 437ab7ac4ebSTom Herbert } else { 438ab7ac4ebSTom Herbert psock->rx_skb_nextp = 439ab7ac4ebSTom Herbert &skb_shinfo(head)->frag_list; 440ab7ac4ebSTom Herbert } 441ab7ac4ebSTom Herbert } 442ab7ac4ebSTom Herbert } 443ab7ac4ebSTom Herbert 444ab7ac4ebSTom Herbert while (eaten < orig_len) { 445ab7ac4ebSTom Herbert /* Always clone since we will consume something */ 446ab7ac4ebSTom Herbert skb = skb_clone(orig_skb, GFP_ATOMIC); 447ab7ac4ebSTom Herbert if (!skb) { 448cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 449ab7ac4ebSTom Herbert desc->error = -ENOMEM; 450ab7ac4ebSTom Herbert break; 451ab7ac4ebSTom Herbert } 452ab7ac4ebSTom Herbert 453ab7ac4ebSTom Herbert cand_len = orig_len - eaten; 454ab7ac4ebSTom Herbert 455ab7ac4ebSTom Herbert head = psock->rx_skb_head; 456ab7ac4ebSTom Herbert if (!head) { 457ab7ac4ebSTom Herbert head = skb; 458ab7ac4ebSTom Herbert psock->rx_skb_head = head; 459ab7ac4ebSTom Herbert /* Will set rx_skb_nextp on next packet if needed */ 460ab7ac4ebSTom Herbert psock->rx_skb_nextp = NULL; 461ab7ac4ebSTom Herbert rxm = kcm_rx_msg(head); 462ab7ac4ebSTom Herbert memset(rxm, 0, sizeof(*rxm)); 463ab7ac4ebSTom Herbert rxm->offset = orig_offset + eaten; 464ab7ac4ebSTom Herbert } else { 465ab7ac4ebSTom Herbert /* Unclone since we may be appending to an skb that we 466ab7ac4ebSTom Herbert * already share a frag_list with. 467ab7ac4ebSTom Herbert */ 468ab7ac4ebSTom Herbert err = skb_unclone(skb, GFP_ATOMIC); 469ab7ac4ebSTom Herbert if (err) { 470cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_mem_fail); 471ab7ac4ebSTom Herbert desc->error = err; 472ab7ac4ebSTom Herbert break; 473ab7ac4ebSTom Herbert } 474ab7ac4ebSTom Herbert 475ab7ac4ebSTom Herbert rxm = kcm_rx_msg(head); 476ab7ac4ebSTom Herbert *psock->rx_skb_nextp = skb; 477ab7ac4ebSTom Herbert psock->rx_skb_nextp = &skb->next; 478ab7ac4ebSTom Herbert head->data_len += skb->len; 479ab7ac4ebSTom Herbert head->len += skb->len; 480ab7ac4ebSTom Herbert head->truesize += skb->truesize; 481ab7ac4ebSTom Herbert } 482ab7ac4ebSTom Herbert 483ab7ac4ebSTom Herbert if (!rxm->full_len) { 484ab7ac4ebSTom Herbert ssize_t len; 485ab7ac4ebSTom Herbert 486ab7ac4ebSTom Herbert len = KCM_RUN_FILTER(psock->bpf_prog, head); 487ab7ac4ebSTom Herbert 488ab7ac4ebSTom Herbert if (!len) { 489ab7ac4ebSTom Herbert /* Need more header to determine length */ 490ab7ac4ebSTom Herbert rxm->accum_len += cand_len; 491ab7ac4ebSTom Herbert eaten += cand_len; 492cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_need_more_hdr); 493ab7ac4ebSTom Herbert WARN_ON(eaten != orig_len); 494ab7ac4ebSTom Herbert break; 495ab7ac4ebSTom Herbert } else if (len <= (ssize_t)head->len - 496ab7ac4ebSTom Herbert skb->len - rxm->offset) { 497ab7ac4ebSTom Herbert /* Length must be into new skb (and also 498ab7ac4ebSTom Herbert * greater than zero) 499ab7ac4ebSTom Herbert */ 500cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_bad_hdr_len); 501ab7ac4ebSTom Herbert desc->error = -EPROTO; 502ab7ac4ebSTom Herbert psock->rx_skb_head = NULL; 503ab7ac4ebSTom Herbert kcm_abort_rx_psock(psock, EPROTO, head); 504ab7ac4ebSTom Herbert break; 505ab7ac4ebSTom Herbert } 506ab7ac4ebSTom Herbert 507ab7ac4ebSTom Herbert rxm->full_len = len; 508ab7ac4ebSTom Herbert } 509ab7ac4ebSTom Herbert 510ab7ac4ebSTom Herbert extra = (ssize_t)(rxm->accum_len + cand_len) - rxm->full_len; 511ab7ac4ebSTom Herbert 512ab7ac4ebSTom Herbert if (extra < 0) { 513ab7ac4ebSTom Herbert /* Message not complete yet. */ 514ab7ac4ebSTom Herbert rxm->accum_len += cand_len; 515ab7ac4ebSTom Herbert eaten += cand_len; 516ab7ac4ebSTom Herbert WARN_ON(eaten != orig_len); 517ab7ac4ebSTom Herbert break; 518ab7ac4ebSTom Herbert } 519ab7ac4ebSTom Herbert 520ab7ac4ebSTom Herbert /* Positive extra indicates ore bytes than needed for the 521ab7ac4ebSTom Herbert * message 522ab7ac4ebSTom Herbert */ 523ab7ac4ebSTom Herbert 524ab7ac4ebSTom Herbert WARN_ON(extra > cand_len); 525ab7ac4ebSTom Herbert 526ab7ac4ebSTom Herbert eaten += (cand_len - extra); 527ab7ac4ebSTom Herbert 528ab7ac4ebSTom Herbert /* Hurray, we have a new message! */ 529ab7ac4ebSTom Herbert psock->rx_skb_head = NULL; 530cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.rx_msgs); 531ab7ac4ebSTom Herbert 532ab7ac4ebSTom Herbert try_queue: 533ab7ac4ebSTom Herbert kcm = reserve_rx_kcm(psock, head); 534ab7ac4ebSTom Herbert if (!kcm) { 535ab7ac4ebSTom Herbert /* Unable to reserve a KCM, message is held in psock. */ 536ab7ac4ebSTom Herbert break; 537ab7ac4ebSTom Herbert } 538ab7ac4ebSTom Herbert 539ab7ac4ebSTom Herbert if (kcm_queue_rcv_skb(&kcm->sk, head)) { 540ab7ac4ebSTom Herbert /* Should mean socket buffer full */ 541ab7ac4ebSTom Herbert unreserve_rx_kcm(psock, false); 542ab7ac4ebSTom Herbert goto try_queue; 543ab7ac4ebSTom Herbert } 544ab7ac4ebSTom Herbert } 545ab7ac4ebSTom Herbert 546ab7ac4ebSTom Herbert if (cloned_orig) 547ab7ac4ebSTom Herbert kfree_skb(orig_skb); 548ab7ac4ebSTom Herbert 549cd6e111bSTom Herbert KCM_STATS_ADD(psock->stats.rx_bytes, eaten); 550cd6e111bSTom Herbert 551ab7ac4ebSTom Herbert return eaten; 552ab7ac4ebSTom Herbert } 553ab7ac4ebSTom Herbert 554ab7ac4ebSTom Herbert /* Called with lock held on lower socket */ 555ab7ac4ebSTom Herbert static int psock_tcp_read_sock(struct kcm_psock *psock) 556ab7ac4ebSTom Herbert { 557ab7ac4ebSTom Herbert read_descriptor_t desc; 558ab7ac4ebSTom Herbert 559ab7ac4ebSTom Herbert desc.arg.data = psock; 560ab7ac4ebSTom Herbert desc.error = 0; 561ab7ac4ebSTom Herbert desc.count = 1; /* give more than one skb per call */ 562ab7ac4ebSTom Herbert 563ab7ac4ebSTom Herbert /* sk should be locked here, so okay to do tcp_read_sock */ 564ab7ac4ebSTom Herbert tcp_read_sock(psock->sk, &desc, kcm_tcp_recv); 565ab7ac4ebSTom Herbert 566ab7ac4ebSTom Herbert unreserve_rx_kcm(psock, true); 567ab7ac4ebSTom Herbert 568ab7ac4ebSTom Herbert return desc.error; 569ab7ac4ebSTom Herbert } 570ab7ac4ebSTom Herbert 571ab7ac4ebSTom Herbert /* Lower sock lock held */ 572ab7ac4ebSTom Herbert static void psock_tcp_data_ready(struct sock *sk) 573ab7ac4ebSTom Herbert { 574ab7ac4ebSTom Herbert struct kcm_psock *psock; 575ab7ac4ebSTom Herbert 576ab7ac4ebSTom Herbert read_lock_bh(&sk->sk_callback_lock); 577ab7ac4ebSTom Herbert 578ab7ac4ebSTom Herbert psock = (struct kcm_psock *)sk->sk_user_data; 579ab7ac4ebSTom Herbert if (unlikely(!psock || psock->rx_stopped)) 580ab7ac4ebSTom Herbert goto out; 581ab7ac4ebSTom Herbert 582ab7ac4ebSTom Herbert if (psock->ready_rx_msg) 583ab7ac4ebSTom Herbert goto out; 584ab7ac4ebSTom Herbert 585ab7ac4ebSTom Herbert if (psock_tcp_read_sock(psock) == -ENOMEM) 586ab7ac4ebSTom Herbert queue_delayed_work(kcm_wq, &psock->rx_delayed_work, 0); 587ab7ac4ebSTom Herbert 588ab7ac4ebSTom Herbert out: 589ab7ac4ebSTom Herbert read_unlock_bh(&sk->sk_callback_lock); 590ab7ac4ebSTom Herbert } 591ab7ac4ebSTom Herbert 592ab7ac4ebSTom Herbert static void do_psock_rx_work(struct kcm_psock *psock) 593ab7ac4ebSTom Herbert { 594ab7ac4ebSTom Herbert read_descriptor_t rd_desc; 595ab7ac4ebSTom Herbert struct sock *csk = psock->sk; 596ab7ac4ebSTom Herbert 597ab7ac4ebSTom Herbert /* We need the read lock to synchronize with psock_tcp_data_ready. We 598ab7ac4ebSTom Herbert * need the socket lock for calling tcp_read_sock. 599ab7ac4ebSTom Herbert */ 600ab7ac4ebSTom Herbert lock_sock(csk); 601ab7ac4ebSTom Herbert read_lock_bh(&csk->sk_callback_lock); 602ab7ac4ebSTom Herbert 603ab7ac4ebSTom Herbert if (unlikely(csk->sk_user_data != psock)) 604ab7ac4ebSTom Herbert goto out; 605ab7ac4ebSTom Herbert 606ab7ac4ebSTom Herbert if (unlikely(psock->rx_stopped)) 607ab7ac4ebSTom Herbert goto out; 608ab7ac4ebSTom Herbert 609ab7ac4ebSTom Herbert if (psock->ready_rx_msg) 610ab7ac4ebSTom Herbert goto out; 611ab7ac4ebSTom Herbert 612ab7ac4ebSTom Herbert rd_desc.arg.data = psock; 613ab7ac4ebSTom Herbert 614ab7ac4ebSTom Herbert if (psock_tcp_read_sock(psock) == -ENOMEM) 615ab7ac4ebSTom Herbert queue_delayed_work(kcm_wq, &psock->rx_delayed_work, 0); 616ab7ac4ebSTom Herbert 617ab7ac4ebSTom Herbert out: 618ab7ac4ebSTom Herbert read_unlock_bh(&csk->sk_callback_lock); 619ab7ac4ebSTom Herbert release_sock(csk); 620ab7ac4ebSTom Herbert } 621ab7ac4ebSTom Herbert 622ab7ac4ebSTom Herbert static void psock_rx_work(struct work_struct *w) 623ab7ac4ebSTom Herbert { 624ab7ac4ebSTom Herbert do_psock_rx_work(container_of(w, struct kcm_psock, rx_work)); 625ab7ac4ebSTom Herbert } 626ab7ac4ebSTom Herbert 627ab7ac4ebSTom Herbert static void psock_rx_delayed_work(struct work_struct *w) 628ab7ac4ebSTom Herbert { 629ab7ac4ebSTom Herbert do_psock_rx_work(container_of(w, struct kcm_psock, 630ab7ac4ebSTom Herbert rx_delayed_work.work)); 631ab7ac4ebSTom Herbert } 632ab7ac4ebSTom Herbert 633ab7ac4ebSTom Herbert static void psock_tcp_state_change(struct sock *sk) 634ab7ac4ebSTom Herbert { 635ab7ac4ebSTom Herbert /* TCP only does a POLLIN for a half close. Do a POLLHUP here 636ab7ac4ebSTom Herbert * since application will normally not poll with POLLIN 637ab7ac4ebSTom Herbert * on the TCP sockets. 638ab7ac4ebSTom Herbert */ 639ab7ac4ebSTom Herbert 640ab7ac4ebSTom Herbert report_csk_error(sk, EPIPE); 641ab7ac4ebSTom Herbert } 642ab7ac4ebSTom Herbert 643ab7ac4ebSTom Herbert static void psock_tcp_write_space(struct sock *sk) 644ab7ac4ebSTom Herbert { 645ab7ac4ebSTom Herbert struct kcm_psock *psock; 646ab7ac4ebSTom Herbert struct kcm_mux *mux; 647ab7ac4ebSTom Herbert struct kcm_sock *kcm; 648ab7ac4ebSTom Herbert 649ab7ac4ebSTom Herbert read_lock_bh(&sk->sk_callback_lock); 650ab7ac4ebSTom Herbert 651ab7ac4ebSTom Herbert psock = (struct kcm_psock *)sk->sk_user_data; 652ab7ac4ebSTom Herbert if (unlikely(!psock)) 653ab7ac4ebSTom Herbert goto out; 654ab7ac4ebSTom Herbert 655ab7ac4ebSTom Herbert mux = psock->mux; 656ab7ac4ebSTom Herbert 657ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 658ab7ac4ebSTom Herbert 659ab7ac4ebSTom Herbert /* Check if the socket is reserved so someone is waiting for sending. */ 660ab7ac4ebSTom Herbert kcm = psock->tx_kcm; 661ab7ac4ebSTom Herbert if (kcm) 662ab7ac4ebSTom Herbert queue_work(kcm_wq, &kcm->tx_work); 663ab7ac4ebSTom Herbert 664ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 665ab7ac4ebSTom Herbert out: 666ab7ac4ebSTom Herbert read_unlock_bh(&sk->sk_callback_lock); 667ab7ac4ebSTom Herbert } 668ab7ac4ebSTom Herbert 669ab7ac4ebSTom Herbert static void unreserve_psock(struct kcm_sock *kcm); 670ab7ac4ebSTom Herbert 671ab7ac4ebSTom Herbert /* kcm sock is locked. */ 672ab7ac4ebSTom Herbert static struct kcm_psock *reserve_psock(struct kcm_sock *kcm) 673ab7ac4ebSTom Herbert { 674ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 675ab7ac4ebSTom Herbert struct kcm_psock *psock; 676ab7ac4ebSTom Herbert 677ab7ac4ebSTom Herbert psock = kcm->tx_psock; 678ab7ac4ebSTom Herbert 679ab7ac4ebSTom Herbert smp_rmb(); /* Must read tx_psock before tx_wait */ 680ab7ac4ebSTom Herbert 681ab7ac4ebSTom Herbert if (psock) { 682ab7ac4ebSTom Herbert WARN_ON(kcm->tx_wait); 683ab7ac4ebSTom Herbert if (unlikely(psock->tx_stopped)) 684ab7ac4ebSTom Herbert unreserve_psock(kcm); 685ab7ac4ebSTom Herbert else 686ab7ac4ebSTom Herbert return kcm->tx_psock; 687ab7ac4ebSTom Herbert } 688ab7ac4ebSTom Herbert 689ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 690ab7ac4ebSTom Herbert 691ab7ac4ebSTom Herbert /* Check again under lock to see if psock was reserved for this 692ab7ac4ebSTom Herbert * psock via psock_unreserve. 693ab7ac4ebSTom Herbert */ 694ab7ac4ebSTom Herbert psock = kcm->tx_psock; 695ab7ac4ebSTom Herbert if (unlikely(psock)) { 696ab7ac4ebSTom Herbert WARN_ON(kcm->tx_wait); 697ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 698ab7ac4ebSTom Herbert return kcm->tx_psock; 699ab7ac4ebSTom Herbert } 700ab7ac4ebSTom Herbert 701ab7ac4ebSTom Herbert if (!list_empty(&mux->psocks_avail)) { 702ab7ac4ebSTom Herbert psock = list_first_entry(&mux->psocks_avail, 703ab7ac4ebSTom Herbert struct kcm_psock, 704ab7ac4ebSTom Herbert psock_avail_list); 705ab7ac4ebSTom Herbert list_del(&psock->psock_avail_list); 706ab7ac4ebSTom Herbert if (kcm->tx_wait) { 707ab7ac4ebSTom Herbert list_del(&kcm->wait_psock_list); 708ab7ac4ebSTom Herbert kcm->tx_wait = false; 709ab7ac4ebSTom Herbert } 710ab7ac4ebSTom Herbert kcm->tx_psock = psock; 711ab7ac4ebSTom Herbert psock->tx_kcm = kcm; 712cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.reserved); 713ab7ac4ebSTom Herbert } else if (!kcm->tx_wait) { 714ab7ac4ebSTom Herbert list_add_tail(&kcm->wait_psock_list, 715ab7ac4ebSTom Herbert &mux->kcm_tx_waiters); 716ab7ac4ebSTom Herbert kcm->tx_wait = true; 717ab7ac4ebSTom Herbert } 718ab7ac4ebSTom Herbert 719ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 720ab7ac4ebSTom Herbert 721ab7ac4ebSTom Herbert return psock; 722ab7ac4ebSTom Herbert } 723ab7ac4ebSTom Herbert 724ab7ac4ebSTom Herbert /* mux lock held */ 725ab7ac4ebSTom Herbert static void psock_now_avail(struct kcm_psock *psock) 726ab7ac4ebSTom Herbert { 727ab7ac4ebSTom Herbert struct kcm_mux *mux = psock->mux; 728ab7ac4ebSTom Herbert struct kcm_sock *kcm; 729ab7ac4ebSTom Herbert 730ab7ac4ebSTom Herbert if (list_empty(&mux->kcm_tx_waiters)) { 731ab7ac4ebSTom Herbert list_add_tail(&psock->psock_avail_list, 732ab7ac4ebSTom Herbert &mux->psocks_avail); 733ab7ac4ebSTom Herbert } else { 734ab7ac4ebSTom Herbert kcm = list_first_entry(&mux->kcm_tx_waiters, 735ab7ac4ebSTom Herbert struct kcm_sock, 736ab7ac4ebSTom Herbert wait_psock_list); 737ab7ac4ebSTom Herbert list_del(&kcm->wait_psock_list); 738ab7ac4ebSTom Herbert kcm->tx_wait = false; 739ab7ac4ebSTom Herbert psock->tx_kcm = kcm; 740ab7ac4ebSTom Herbert 741ab7ac4ebSTom Herbert /* Commit before changing tx_psock since that is read in 742ab7ac4ebSTom Herbert * reserve_psock before queuing work. 743ab7ac4ebSTom Herbert */ 744ab7ac4ebSTom Herbert smp_mb(); 745ab7ac4ebSTom Herbert 746ab7ac4ebSTom Herbert kcm->tx_psock = psock; 747cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.reserved); 748ab7ac4ebSTom Herbert queue_work(kcm_wq, &kcm->tx_work); 749ab7ac4ebSTom Herbert } 750ab7ac4ebSTom Herbert } 751ab7ac4ebSTom Herbert 752ab7ac4ebSTom Herbert /* kcm sock is locked. */ 753ab7ac4ebSTom Herbert static void unreserve_psock(struct kcm_sock *kcm) 754ab7ac4ebSTom Herbert { 755ab7ac4ebSTom Herbert struct kcm_psock *psock; 756ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 757ab7ac4ebSTom Herbert 758ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 759ab7ac4ebSTom Herbert 760ab7ac4ebSTom Herbert psock = kcm->tx_psock; 761ab7ac4ebSTom Herbert 762ab7ac4ebSTom Herbert if (WARN_ON(!psock)) { 763ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 764ab7ac4ebSTom Herbert return; 765ab7ac4ebSTom Herbert } 766ab7ac4ebSTom Herbert 767ab7ac4ebSTom Herbert smp_rmb(); /* Read tx_psock before tx_wait */ 768ab7ac4ebSTom Herbert 769cd6e111bSTom Herbert kcm_update_tx_mux_stats(mux, psock); 770cd6e111bSTom Herbert 771ab7ac4ebSTom Herbert WARN_ON(kcm->tx_wait); 772ab7ac4ebSTom Herbert 773ab7ac4ebSTom Herbert kcm->tx_psock = NULL; 774ab7ac4ebSTom Herbert psock->tx_kcm = NULL; 775cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.unreserved); 776ab7ac4ebSTom Herbert 777ab7ac4ebSTom Herbert if (unlikely(psock->tx_stopped)) { 778ab7ac4ebSTom Herbert if (psock->done) { 779ab7ac4ebSTom Herbert /* Deferred free */ 780ab7ac4ebSTom Herbert list_del(&psock->psock_list); 781ab7ac4ebSTom Herbert mux->psocks_cnt--; 782ab7ac4ebSTom Herbert sock_put(psock->sk); 783ab7ac4ebSTom Herbert fput(psock->sk->sk_socket->file); 784ab7ac4ebSTom Herbert kmem_cache_free(kcm_psockp, psock); 785ab7ac4ebSTom Herbert } 786ab7ac4ebSTom Herbert 787ab7ac4ebSTom Herbert /* Don't put back on available list */ 788ab7ac4ebSTom Herbert 789ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 790ab7ac4ebSTom Herbert 791ab7ac4ebSTom Herbert return; 792ab7ac4ebSTom Herbert } 793ab7ac4ebSTom Herbert 794ab7ac4ebSTom Herbert psock_now_avail(psock); 795ab7ac4ebSTom Herbert 796ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 797ab7ac4ebSTom Herbert } 798ab7ac4ebSTom Herbert 799cd6e111bSTom Herbert static void kcm_report_tx_retry(struct kcm_sock *kcm) 800cd6e111bSTom Herbert { 801cd6e111bSTom Herbert struct kcm_mux *mux = kcm->mux; 802cd6e111bSTom Herbert 803cd6e111bSTom Herbert spin_lock_bh(&mux->lock); 804cd6e111bSTom Herbert KCM_STATS_INCR(mux->stats.tx_retries); 805cd6e111bSTom Herbert spin_unlock_bh(&mux->lock); 806cd6e111bSTom Herbert } 807cd6e111bSTom Herbert 808ab7ac4ebSTom Herbert /* Write any messages ready on the kcm socket. Called with kcm sock lock 809ab7ac4ebSTom Herbert * held. Return bytes actually sent or error. 810ab7ac4ebSTom Herbert */ 811ab7ac4ebSTom Herbert static int kcm_write_msgs(struct kcm_sock *kcm) 812ab7ac4ebSTom Herbert { 813ab7ac4ebSTom Herbert struct sock *sk = &kcm->sk; 814ab7ac4ebSTom Herbert struct kcm_psock *psock; 815ab7ac4ebSTom Herbert struct sk_buff *skb, *head; 816ab7ac4ebSTom Herbert struct kcm_tx_msg *txm; 817ab7ac4ebSTom Herbert unsigned short fragidx, frag_offset; 818ab7ac4ebSTom Herbert unsigned int sent, total_sent = 0; 819ab7ac4ebSTom Herbert int ret = 0; 820ab7ac4ebSTom Herbert 821ab7ac4ebSTom Herbert kcm->tx_wait_more = false; 822ab7ac4ebSTom Herbert psock = kcm->tx_psock; 823ab7ac4ebSTom Herbert if (unlikely(psock && psock->tx_stopped)) { 824ab7ac4ebSTom Herbert /* A reserved psock was aborted asynchronously. Unreserve 825ab7ac4ebSTom Herbert * it and we'll retry the message. 826ab7ac4ebSTom Herbert */ 827ab7ac4ebSTom Herbert unreserve_psock(kcm); 828cd6e111bSTom Herbert kcm_report_tx_retry(kcm); 829ab7ac4ebSTom Herbert if (skb_queue_empty(&sk->sk_write_queue)) 830ab7ac4ebSTom Herbert return 0; 831ab7ac4ebSTom Herbert 832ab7ac4ebSTom Herbert kcm_tx_msg(skb_peek(&sk->sk_write_queue))->sent = 0; 833ab7ac4ebSTom Herbert 834ab7ac4ebSTom Herbert } else if (skb_queue_empty(&sk->sk_write_queue)) { 835ab7ac4ebSTom Herbert return 0; 836ab7ac4ebSTom Herbert } 837ab7ac4ebSTom Herbert 838ab7ac4ebSTom Herbert head = skb_peek(&sk->sk_write_queue); 839ab7ac4ebSTom Herbert txm = kcm_tx_msg(head); 840ab7ac4ebSTom Herbert 841ab7ac4ebSTom Herbert if (txm->sent) { 842ab7ac4ebSTom Herbert /* Send of first skbuff in queue already in progress */ 843ab7ac4ebSTom Herbert if (WARN_ON(!psock)) { 844ab7ac4ebSTom Herbert ret = -EINVAL; 845ab7ac4ebSTom Herbert goto out; 846ab7ac4ebSTom Herbert } 847ab7ac4ebSTom Herbert sent = txm->sent; 848ab7ac4ebSTom Herbert frag_offset = txm->frag_offset; 849ab7ac4ebSTom Herbert fragidx = txm->fragidx; 850ab7ac4ebSTom Herbert skb = txm->frag_skb; 851ab7ac4ebSTom Herbert 852ab7ac4ebSTom Herbert goto do_frag; 853ab7ac4ebSTom Herbert } 854ab7ac4ebSTom Herbert 855ab7ac4ebSTom Herbert try_again: 856ab7ac4ebSTom Herbert psock = reserve_psock(kcm); 857ab7ac4ebSTom Herbert if (!psock) 858ab7ac4ebSTom Herbert goto out; 859ab7ac4ebSTom Herbert 860ab7ac4ebSTom Herbert do { 861ab7ac4ebSTom Herbert skb = head; 862ab7ac4ebSTom Herbert txm = kcm_tx_msg(head); 863ab7ac4ebSTom Herbert sent = 0; 864ab7ac4ebSTom Herbert 865ab7ac4ebSTom Herbert do_frag_list: 866ab7ac4ebSTom Herbert if (WARN_ON(!skb_shinfo(skb)->nr_frags)) { 867ab7ac4ebSTom Herbert ret = -EINVAL; 868ab7ac4ebSTom Herbert goto out; 869ab7ac4ebSTom Herbert } 870ab7ac4ebSTom Herbert 871ab7ac4ebSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; 872ab7ac4ebSTom Herbert fragidx++) { 873ab7ac4ebSTom Herbert skb_frag_t *frag; 874ab7ac4ebSTom Herbert 875ab7ac4ebSTom Herbert frag_offset = 0; 876ab7ac4ebSTom Herbert do_frag: 877ab7ac4ebSTom Herbert frag = &skb_shinfo(skb)->frags[fragidx]; 878ab7ac4ebSTom Herbert if (WARN_ON(!frag->size)) { 879ab7ac4ebSTom Herbert ret = -EINVAL; 880ab7ac4ebSTom Herbert goto out; 881ab7ac4ebSTom Herbert } 882ab7ac4ebSTom Herbert 883ab7ac4ebSTom Herbert ret = kernel_sendpage(psock->sk->sk_socket, 884ab7ac4ebSTom Herbert frag->page.p, 885ab7ac4ebSTom Herbert frag->page_offset + frag_offset, 886ab7ac4ebSTom Herbert frag->size - frag_offset, 887ab7ac4ebSTom Herbert MSG_DONTWAIT); 888ab7ac4ebSTom Herbert if (ret <= 0) { 889ab7ac4ebSTom Herbert if (ret == -EAGAIN) { 890ab7ac4ebSTom Herbert /* Save state to try again when there's 891ab7ac4ebSTom Herbert * write space on the socket 892ab7ac4ebSTom Herbert */ 893ab7ac4ebSTom Herbert txm->sent = sent; 894ab7ac4ebSTom Herbert txm->frag_offset = frag_offset; 895ab7ac4ebSTom Herbert txm->fragidx = fragidx; 896ab7ac4ebSTom Herbert txm->frag_skb = skb; 897ab7ac4ebSTom Herbert 898ab7ac4ebSTom Herbert ret = 0; 899ab7ac4ebSTom Herbert goto out; 900ab7ac4ebSTom Herbert } 901ab7ac4ebSTom Herbert 902ab7ac4ebSTom Herbert /* Hard failure in sending message, abort this 903ab7ac4ebSTom Herbert * psock since it has lost framing 904ab7ac4ebSTom Herbert * synchonization and retry sending the 905ab7ac4ebSTom Herbert * message from the beginning. 906ab7ac4ebSTom Herbert */ 907ab7ac4ebSTom Herbert kcm_abort_tx_psock(psock, ret ? -ret : EPIPE, 908ab7ac4ebSTom Herbert true); 909ab7ac4ebSTom Herbert unreserve_psock(kcm); 910ab7ac4ebSTom Herbert 911ab7ac4ebSTom Herbert txm->sent = 0; 912cd6e111bSTom Herbert kcm_report_tx_retry(kcm); 913ab7ac4ebSTom Herbert ret = 0; 914ab7ac4ebSTom Herbert 915ab7ac4ebSTom Herbert goto try_again; 916ab7ac4ebSTom Herbert } 917ab7ac4ebSTom Herbert 918ab7ac4ebSTom Herbert sent += ret; 919ab7ac4ebSTom Herbert frag_offset += ret; 920cd6e111bSTom Herbert KCM_STATS_ADD(psock->stats.tx_bytes, ret); 921ab7ac4ebSTom Herbert if (frag_offset < frag->size) { 922ab7ac4ebSTom Herbert /* Not finished with this frag */ 923ab7ac4ebSTom Herbert goto do_frag; 924ab7ac4ebSTom Herbert } 925ab7ac4ebSTom Herbert } 926ab7ac4ebSTom Herbert 927ab7ac4ebSTom Herbert if (skb == head) { 928ab7ac4ebSTom Herbert if (skb_has_frag_list(skb)) { 929ab7ac4ebSTom Herbert skb = skb_shinfo(skb)->frag_list; 930ab7ac4ebSTom Herbert goto do_frag_list; 931ab7ac4ebSTom Herbert } 932ab7ac4ebSTom Herbert } else if (skb->next) { 933ab7ac4ebSTom Herbert skb = skb->next; 934ab7ac4ebSTom Herbert goto do_frag_list; 935ab7ac4ebSTom Herbert } 936ab7ac4ebSTom Herbert 937ab7ac4ebSTom Herbert /* Successfully sent the whole packet, account for it. */ 938ab7ac4ebSTom Herbert skb_dequeue(&sk->sk_write_queue); 939ab7ac4ebSTom Herbert kfree_skb(head); 940ab7ac4ebSTom Herbert sk->sk_wmem_queued -= sent; 941ab7ac4ebSTom Herbert total_sent += sent; 942cd6e111bSTom Herbert KCM_STATS_INCR(psock->stats.tx_msgs); 943ab7ac4ebSTom Herbert } while ((head = skb_peek(&sk->sk_write_queue))); 944ab7ac4ebSTom Herbert out: 945ab7ac4ebSTom Herbert if (!head) { 946ab7ac4ebSTom Herbert /* Done with all queued messages. */ 947ab7ac4ebSTom Herbert WARN_ON(!skb_queue_empty(&sk->sk_write_queue)); 948ab7ac4ebSTom Herbert unreserve_psock(kcm); 949ab7ac4ebSTom Herbert } 950ab7ac4ebSTom Herbert 951ab7ac4ebSTom Herbert /* Check if write space is available */ 952ab7ac4ebSTom Herbert sk->sk_write_space(sk); 953ab7ac4ebSTom Herbert 954ab7ac4ebSTom Herbert return total_sent ? : ret; 955ab7ac4ebSTom Herbert } 956ab7ac4ebSTom Herbert 957ab7ac4ebSTom Herbert static void kcm_tx_work(struct work_struct *w) 958ab7ac4ebSTom Herbert { 959ab7ac4ebSTom Herbert struct kcm_sock *kcm = container_of(w, struct kcm_sock, tx_work); 960ab7ac4ebSTom Herbert struct sock *sk = &kcm->sk; 961ab7ac4ebSTom Herbert int err; 962ab7ac4ebSTom Herbert 963ab7ac4ebSTom Herbert lock_sock(sk); 964ab7ac4ebSTom Herbert 965ab7ac4ebSTom Herbert /* Primarily for SOCK_DGRAM sockets, also handle asynchronous tx 966ab7ac4ebSTom Herbert * aborts 967ab7ac4ebSTom Herbert */ 968ab7ac4ebSTom Herbert err = kcm_write_msgs(kcm); 969ab7ac4ebSTom Herbert if (err < 0) { 970ab7ac4ebSTom Herbert /* Hard failure in write, report error on KCM socket */ 971ab7ac4ebSTom Herbert pr_warn("KCM: Hard failure on kcm_write_msgs %d\n", err); 972ab7ac4ebSTom Herbert report_csk_error(&kcm->sk, -err); 973ab7ac4ebSTom Herbert goto out; 974ab7ac4ebSTom Herbert } 975ab7ac4ebSTom Herbert 976ab7ac4ebSTom Herbert /* Primarily for SOCK_SEQPACKET sockets */ 977ab7ac4ebSTom Herbert if (likely(sk->sk_socket) && 978ab7ac4ebSTom Herbert test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 979ab7ac4ebSTom Herbert clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 980ab7ac4ebSTom Herbert sk->sk_write_space(sk); 981ab7ac4ebSTom Herbert } 982ab7ac4ebSTom Herbert 983ab7ac4ebSTom Herbert out: 984ab7ac4ebSTom Herbert release_sock(sk); 985ab7ac4ebSTom Herbert } 986ab7ac4ebSTom Herbert 987ab7ac4ebSTom Herbert static void kcm_push(struct kcm_sock *kcm) 988ab7ac4ebSTom Herbert { 989ab7ac4ebSTom Herbert if (kcm->tx_wait_more) 990ab7ac4ebSTom Herbert kcm_write_msgs(kcm); 991ab7ac4ebSTom Herbert } 992ab7ac4ebSTom Herbert 993*f29698fcSTom Herbert static ssize_t kcm_sendpage(struct socket *sock, struct page *page, 994*f29698fcSTom Herbert int offset, size_t size, int flags) 995*f29698fcSTom Herbert 996*f29698fcSTom Herbert { 997*f29698fcSTom Herbert struct sock *sk = sock->sk; 998*f29698fcSTom Herbert struct kcm_sock *kcm = kcm_sk(sk); 999*f29698fcSTom Herbert struct sk_buff *skb = NULL, *head = NULL; 1000*f29698fcSTom Herbert long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 1001*f29698fcSTom Herbert bool eor; 1002*f29698fcSTom Herbert int err = 0; 1003*f29698fcSTom Herbert int i; 1004*f29698fcSTom Herbert 1005*f29698fcSTom Herbert if (flags & MSG_SENDPAGE_NOTLAST) 1006*f29698fcSTom Herbert flags |= MSG_MORE; 1007*f29698fcSTom Herbert 1008*f29698fcSTom Herbert /* No MSG_EOR from splice, only look at MSG_MORE */ 1009*f29698fcSTom Herbert eor = !(flags & MSG_MORE); 1010*f29698fcSTom Herbert 1011*f29698fcSTom Herbert lock_sock(sk); 1012*f29698fcSTom Herbert 1013*f29698fcSTom Herbert sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1014*f29698fcSTom Herbert 1015*f29698fcSTom Herbert err = -EPIPE; 1016*f29698fcSTom Herbert if (sk->sk_err) 1017*f29698fcSTom Herbert goto out_error; 1018*f29698fcSTom Herbert 1019*f29698fcSTom Herbert if (kcm->seq_skb) { 1020*f29698fcSTom Herbert /* Previously opened message */ 1021*f29698fcSTom Herbert head = kcm->seq_skb; 1022*f29698fcSTom Herbert skb = kcm_tx_msg(head)->last_skb; 1023*f29698fcSTom Herbert i = skb_shinfo(skb)->nr_frags; 1024*f29698fcSTom Herbert 1025*f29698fcSTom Herbert if (skb_can_coalesce(skb, i, page, offset)) { 1026*f29698fcSTom Herbert skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 1027*f29698fcSTom Herbert skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1028*f29698fcSTom Herbert goto coalesced; 1029*f29698fcSTom Herbert } 1030*f29698fcSTom Herbert 1031*f29698fcSTom Herbert if (i >= MAX_SKB_FRAGS) { 1032*f29698fcSTom Herbert struct sk_buff *tskb; 1033*f29698fcSTom Herbert 1034*f29698fcSTom Herbert tskb = alloc_skb(0, sk->sk_allocation); 1035*f29698fcSTom Herbert while (!tskb) { 1036*f29698fcSTom Herbert kcm_push(kcm); 1037*f29698fcSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1038*f29698fcSTom Herbert if (err) 1039*f29698fcSTom Herbert goto out_error; 1040*f29698fcSTom Herbert } 1041*f29698fcSTom Herbert 1042*f29698fcSTom Herbert if (head == skb) 1043*f29698fcSTom Herbert skb_shinfo(head)->frag_list = tskb; 1044*f29698fcSTom Herbert else 1045*f29698fcSTom Herbert skb->next = tskb; 1046*f29698fcSTom Herbert 1047*f29698fcSTom Herbert skb = tskb; 1048*f29698fcSTom Herbert skb->ip_summed = CHECKSUM_UNNECESSARY; 1049*f29698fcSTom Herbert i = 0; 1050*f29698fcSTom Herbert } 1051*f29698fcSTom Herbert } else { 1052*f29698fcSTom Herbert /* Call the sk_stream functions to manage the sndbuf mem. */ 1053*f29698fcSTom Herbert if (!sk_stream_memory_free(sk)) { 1054*f29698fcSTom Herbert kcm_push(kcm); 1055*f29698fcSTom Herbert set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1056*f29698fcSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1057*f29698fcSTom Herbert if (err) 1058*f29698fcSTom Herbert goto out_error; 1059*f29698fcSTom Herbert } 1060*f29698fcSTom Herbert 1061*f29698fcSTom Herbert head = alloc_skb(0, sk->sk_allocation); 1062*f29698fcSTom Herbert while (!head) { 1063*f29698fcSTom Herbert kcm_push(kcm); 1064*f29698fcSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1065*f29698fcSTom Herbert if (err) 1066*f29698fcSTom Herbert goto out_error; 1067*f29698fcSTom Herbert } 1068*f29698fcSTom Herbert 1069*f29698fcSTom Herbert skb = head; 1070*f29698fcSTom Herbert i = 0; 1071*f29698fcSTom Herbert } 1072*f29698fcSTom Herbert 1073*f29698fcSTom Herbert get_page(page); 1074*f29698fcSTom Herbert skb_fill_page_desc(skb, i, page, offset, size); 1075*f29698fcSTom Herbert skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; 1076*f29698fcSTom Herbert 1077*f29698fcSTom Herbert coalesced: 1078*f29698fcSTom Herbert skb->len += size; 1079*f29698fcSTom Herbert skb->data_len += size; 1080*f29698fcSTom Herbert skb->truesize += size; 1081*f29698fcSTom Herbert sk->sk_wmem_queued += size; 1082*f29698fcSTom Herbert sk_mem_charge(sk, size); 1083*f29698fcSTom Herbert 1084*f29698fcSTom Herbert if (head != skb) { 1085*f29698fcSTom Herbert head->len += size; 1086*f29698fcSTom Herbert head->data_len += size; 1087*f29698fcSTom Herbert head->truesize += size; 1088*f29698fcSTom Herbert } 1089*f29698fcSTom Herbert 1090*f29698fcSTom Herbert if (eor) { 1091*f29698fcSTom Herbert bool not_busy = skb_queue_empty(&sk->sk_write_queue); 1092*f29698fcSTom Herbert 1093*f29698fcSTom Herbert /* Message complete, queue it on send buffer */ 1094*f29698fcSTom Herbert __skb_queue_tail(&sk->sk_write_queue, head); 1095*f29698fcSTom Herbert kcm->seq_skb = NULL; 1096*f29698fcSTom Herbert KCM_STATS_INCR(kcm->stats.tx_msgs); 1097*f29698fcSTom Herbert 1098*f29698fcSTom Herbert if (flags & MSG_BATCH) { 1099*f29698fcSTom Herbert kcm->tx_wait_more = true; 1100*f29698fcSTom Herbert } else if (kcm->tx_wait_more || not_busy) { 1101*f29698fcSTom Herbert err = kcm_write_msgs(kcm); 1102*f29698fcSTom Herbert if (err < 0) { 1103*f29698fcSTom Herbert /* We got a hard error in write_msgs but have 1104*f29698fcSTom Herbert * already queued this message. Report an error 1105*f29698fcSTom Herbert * in the socket, but don't affect return value 1106*f29698fcSTom Herbert * from sendmsg 1107*f29698fcSTom Herbert */ 1108*f29698fcSTom Herbert pr_warn("KCM: Hard failure on kcm_write_msgs\n"); 1109*f29698fcSTom Herbert report_csk_error(&kcm->sk, -err); 1110*f29698fcSTom Herbert } 1111*f29698fcSTom Herbert } 1112*f29698fcSTom Herbert } else { 1113*f29698fcSTom Herbert /* Message not complete, save state */ 1114*f29698fcSTom Herbert kcm->seq_skb = head; 1115*f29698fcSTom Herbert kcm_tx_msg(head)->last_skb = skb; 1116*f29698fcSTom Herbert } 1117*f29698fcSTom Herbert 1118*f29698fcSTom Herbert KCM_STATS_ADD(kcm->stats.tx_bytes, size); 1119*f29698fcSTom Herbert 1120*f29698fcSTom Herbert release_sock(sk); 1121*f29698fcSTom Herbert return size; 1122*f29698fcSTom Herbert 1123*f29698fcSTom Herbert out_error: 1124*f29698fcSTom Herbert kcm_push(kcm); 1125*f29698fcSTom Herbert 1126*f29698fcSTom Herbert err = sk_stream_error(sk, flags, err); 1127*f29698fcSTom Herbert 1128*f29698fcSTom Herbert /* make sure we wake any epoll edge trigger waiter */ 1129*f29698fcSTom Herbert if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN)) 1130*f29698fcSTom Herbert sk->sk_write_space(sk); 1131*f29698fcSTom Herbert 1132*f29698fcSTom Herbert release_sock(sk); 1133*f29698fcSTom Herbert return err; 1134*f29698fcSTom Herbert } 1135*f29698fcSTom Herbert 1136ab7ac4ebSTom Herbert static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 1137ab7ac4ebSTom Herbert { 1138ab7ac4ebSTom Herbert struct sock *sk = sock->sk; 1139ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sk); 1140ab7ac4ebSTom Herbert struct sk_buff *skb = NULL, *head = NULL; 1141ab7ac4ebSTom Herbert size_t copy, copied = 0; 1142ab7ac4ebSTom Herbert long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1143ab7ac4ebSTom Herbert int eor = (sock->type == SOCK_DGRAM) ? 1144ab7ac4ebSTom Herbert !(msg->msg_flags & MSG_MORE) : !!(msg->msg_flags & MSG_EOR); 1145ab7ac4ebSTom Herbert int err = -EPIPE; 1146ab7ac4ebSTom Herbert 1147ab7ac4ebSTom Herbert lock_sock(sk); 1148ab7ac4ebSTom Herbert 1149ab7ac4ebSTom Herbert /* Per tcp_sendmsg this should be in poll */ 1150ab7ac4ebSTom Herbert sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1151ab7ac4ebSTom Herbert 1152ab7ac4ebSTom Herbert if (sk->sk_err) 1153ab7ac4ebSTom Herbert goto out_error; 1154ab7ac4ebSTom Herbert 1155ab7ac4ebSTom Herbert if (kcm->seq_skb) { 1156ab7ac4ebSTom Herbert /* Previously opened message */ 1157ab7ac4ebSTom Herbert head = kcm->seq_skb; 1158ab7ac4ebSTom Herbert skb = kcm_tx_msg(head)->last_skb; 1159ab7ac4ebSTom Herbert goto start; 1160ab7ac4ebSTom Herbert } 1161ab7ac4ebSTom Herbert 1162ab7ac4ebSTom Herbert /* Call the sk_stream functions to manage the sndbuf mem. */ 1163ab7ac4ebSTom Herbert if (!sk_stream_memory_free(sk)) { 1164ab7ac4ebSTom Herbert kcm_push(kcm); 1165ab7ac4ebSTom Herbert set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1166ab7ac4ebSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1167ab7ac4ebSTom Herbert if (err) 1168ab7ac4ebSTom Herbert goto out_error; 1169ab7ac4ebSTom Herbert } 1170ab7ac4ebSTom Herbert 1171ab7ac4ebSTom Herbert /* New message, alloc head skb */ 1172ab7ac4ebSTom Herbert head = alloc_skb(0, sk->sk_allocation); 1173ab7ac4ebSTom Herbert while (!head) { 1174ab7ac4ebSTom Herbert kcm_push(kcm); 1175ab7ac4ebSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1176ab7ac4ebSTom Herbert if (err) 1177ab7ac4ebSTom Herbert goto out_error; 1178ab7ac4ebSTom Herbert 1179ab7ac4ebSTom Herbert head = alloc_skb(0, sk->sk_allocation); 1180ab7ac4ebSTom Herbert } 1181ab7ac4ebSTom Herbert 1182ab7ac4ebSTom Herbert skb = head; 1183ab7ac4ebSTom Herbert 1184ab7ac4ebSTom Herbert /* Set ip_summed to CHECKSUM_UNNECESSARY to avoid calling 1185ab7ac4ebSTom Herbert * csum_and_copy_from_iter from skb_do_copy_data_nocache. 1186ab7ac4ebSTom Herbert */ 1187ab7ac4ebSTom Herbert skb->ip_summed = CHECKSUM_UNNECESSARY; 1188ab7ac4ebSTom Herbert 1189ab7ac4ebSTom Herbert start: 1190ab7ac4ebSTom Herbert while (msg_data_left(msg)) { 1191ab7ac4ebSTom Herbert bool merge = true; 1192ab7ac4ebSTom Herbert int i = skb_shinfo(skb)->nr_frags; 1193ab7ac4ebSTom Herbert struct page_frag *pfrag = sk_page_frag(sk); 1194ab7ac4ebSTom Herbert 1195ab7ac4ebSTom Herbert if (!sk_page_frag_refill(sk, pfrag)) 1196ab7ac4ebSTom Herbert goto wait_for_memory; 1197ab7ac4ebSTom Herbert 1198ab7ac4ebSTom Herbert if (!skb_can_coalesce(skb, i, pfrag->page, 1199ab7ac4ebSTom Herbert pfrag->offset)) { 1200ab7ac4ebSTom Herbert if (i == MAX_SKB_FRAGS) { 1201ab7ac4ebSTom Herbert struct sk_buff *tskb; 1202ab7ac4ebSTom Herbert 1203ab7ac4ebSTom Herbert tskb = alloc_skb(0, sk->sk_allocation); 1204ab7ac4ebSTom Herbert if (!tskb) 1205ab7ac4ebSTom Herbert goto wait_for_memory; 1206ab7ac4ebSTom Herbert 1207ab7ac4ebSTom Herbert if (head == skb) 1208ab7ac4ebSTom Herbert skb_shinfo(head)->frag_list = tskb; 1209ab7ac4ebSTom Herbert else 1210ab7ac4ebSTom Herbert skb->next = tskb; 1211ab7ac4ebSTom Herbert 1212ab7ac4ebSTom Herbert skb = tskb; 1213ab7ac4ebSTom Herbert skb->ip_summed = CHECKSUM_UNNECESSARY; 1214ab7ac4ebSTom Herbert continue; 1215ab7ac4ebSTom Herbert } 1216ab7ac4ebSTom Herbert merge = false; 1217ab7ac4ebSTom Herbert } 1218ab7ac4ebSTom Herbert 1219ab7ac4ebSTom Herbert copy = min_t(int, msg_data_left(msg), 1220ab7ac4ebSTom Herbert pfrag->size - pfrag->offset); 1221ab7ac4ebSTom Herbert 1222ab7ac4ebSTom Herbert if (!sk_wmem_schedule(sk, copy)) 1223ab7ac4ebSTom Herbert goto wait_for_memory; 1224ab7ac4ebSTom Herbert 1225ab7ac4ebSTom Herbert err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, 1226ab7ac4ebSTom Herbert pfrag->page, 1227ab7ac4ebSTom Herbert pfrag->offset, 1228ab7ac4ebSTom Herbert copy); 1229ab7ac4ebSTom Herbert if (err) 1230ab7ac4ebSTom Herbert goto out_error; 1231ab7ac4ebSTom Herbert 1232ab7ac4ebSTom Herbert /* Update the skb. */ 1233ab7ac4ebSTom Herbert if (merge) { 1234ab7ac4ebSTom Herbert skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 1235ab7ac4ebSTom Herbert } else { 1236ab7ac4ebSTom Herbert skb_fill_page_desc(skb, i, pfrag->page, 1237ab7ac4ebSTom Herbert pfrag->offset, copy); 1238ab7ac4ebSTom Herbert get_page(pfrag->page); 1239ab7ac4ebSTom Herbert } 1240ab7ac4ebSTom Herbert 1241ab7ac4ebSTom Herbert pfrag->offset += copy; 1242ab7ac4ebSTom Herbert copied += copy; 1243ab7ac4ebSTom Herbert if (head != skb) { 1244ab7ac4ebSTom Herbert head->len += copy; 1245ab7ac4ebSTom Herbert head->data_len += copy; 1246ab7ac4ebSTom Herbert } 1247ab7ac4ebSTom Herbert 1248ab7ac4ebSTom Herbert continue; 1249ab7ac4ebSTom Herbert 1250ab7ac4ebSTom Herbert wait_for_memory: 1251ab7ac4ebSTom Herbert kcm_push(kcm); 1252ab7ac4ebSTom Herbert err = sk_stream_wait_memory(sk, &timeo); 1253ab7ac4ebSTom Herbert if (err) 1254ab7ac4ebSTom Herbert goto out_error; 1255ab7ac4ebSTom Herbert } 1256ab7ac4ebSTom Herbert 1257ab7ac4ebSTom Herbert if (eor) { 1258ab7ac4ebSTom Herbert bool not_busy = skb_queue_empty(&sk->sk_write_queue); 1259ab7ac4ebSTom Herbert 1260ab7ac4ebSTom Herbert /* Message complete, queue it on send buffer */ 1261ab7ac4ebSTom Herbert __skb_queue_tail(&sk->sk_write_queue, head); 1262ab7ac4ebSTom Herbert kcm->seq_skb = NULL; 1263cd6e111bSTom Herbert KCM_STATS_INCR(kcm->stats.tx_msgs); 1264ab7ac4ebSTom Herbert 1265ab7ac4ebSTom Herbert if (msg->msg_flags & MSG_BATCH) { 1266ab7ac4ebSTom Herbert kcm->tx_wait_more = true; 1267ab7ac4ebSTom Herbert } else if (kcm->tx_wait_more || not_busy) { 1268ab7ac4ebSTom Herbert err = kcm_write_msgs(kcm); 1269ab7ac4ebSTom Herbert if (err < 0) { 1270ab7ac4ebSTom Herbert /* We got a hard error in write_msgs but have 1271ab7ac4ebSTom Herbert * already queued this message. Report an error 1272ab7ac4ebSTom Herbert * in the socket, but don't affect return value 1273ab7ac4ebSTom Herbert * from sendmsg 1274ab7ac4ebSTom Herbert */ 1275ab7ac4ebSTom Herbert pr_warn("KCM: Hard failure on kcm_write_msgs\n"); 1276ab7ac4ebSTom Herbert report_csk_error(&kcm->sk, -err); 1277ab7ac4ebSTom Herbert } 1278ab7ac4ebSTom Herbert } 1279ab7ac4ebSTom Herbert } else { 1280ab7ac4ebSTom Herbert /* Message not complete, save state */ 1281ab7ac4ebSTom Herbert partial_message: 1282ab7ac4ebSTom Herbert kcm->seq_skb = head; 1283ab7ac4ebSTom Herbert kcm_tx_msg(head)->last_skb = skb; 1284ab7ac4ebSTom Herbert } 1285ab7ac4ebSTom Herbert 1286cd6e111bSTom Herbert KCM_STATS_ADD(kcm->stats.tx_bytes, copied); 1287cd6e111bSTom Herbert 1288ab7ac4ebSTom Herbert release_sock(sk); 1289ab7ac4ebSTom Herbert return copied; 1290ab7ac4ebSTom Herbert 1291ab7ac4ebSTom Herbert out_error: 1292ab7ac4ebSTom Herbert kcm_push(kcm); 1293ab7ac4ebSTom Herbert 1294ab7ac4ebSTom Herbert if (copied && sock->type == SOCK_SEQPACKET) { 1295ab7ac4ebSTom Herbert /* Wrote some bytes before encountering an 1296ab7ac4ebSTom Herbert * error, return partial success. 1297ab7ac4ebSTom Herbert */ 1298ab7ac4ebSTom Herbert goto partial_message; 1299ab7ac4ebSTom Herbert } 1300ab7ac4ebSTom Herbert 1301ab7ac4ebSTom Herbert if (head != kcm->seq_skb) 1302ab7ac4ebSTom Herbert kfree_skb(head); 1303ab7ac4ebSTom Herbert 1304ab7ac4ebSTom Herbert err = sk_stream_error(sk, msg->msg_flags, err); 1305ab7ac4ebSTom Herbert 1306ab7ac4ebSTom Herbert /* make sure we wake any epoll edge trigger waiter */ 1307ab7ac4ebSTom Herbert if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN)) 1308ab7ac4ebSTom Herbert sk->sk_write_space(sk); 1309ab7ac4ebSTom Herbert 1310ab7ac4ebSTom Herbert release_sock(sk); 1311ab7ac4ebSTom Herbert return err; 1312ab7ac4ebSTom Herbert } 1313ab7ac4ebSTom Herbert 1314ab7ac4ebSTom Herbert static struct sk_buff *kcm_wait_data(struct sock *sk, int flags, 1315ab7ac4ebSTom Herbert long timeo, int *err) 1316ab7ac4ebSTom Herbert { 1317ab7ac4ebSTom Herbert struct sk_buff *skb; 1318ab7ac4ebSTom Herbert 1319ab7ac4ebSTom Herbert while (!(skb = skb_peek(&sk->sk_receive_queue))) { 1320ab7ac4ebSTom Herbert if (sk->sk_err) { 1321ab7ac4ebSTom Herbert *err = sock_error(sk); 1322ab7ac4ebSTom Herbert return NULL; 1323ab7ac4ebSTom Herbert } 1324ab7ac4ebSTom Herbert 1325ab7ac4ebSTom Herbert if (sock_flag(sk, SOCK_DONE)) 1326ab7ac4ebSTom Herbert return NULL; 1327ab7ac4ebSTom Herbert 1328ab7ac4ebSTom Herbert if ((flags & MSG_DONTWAIT) || !timeo) { 1329ab7ac4ebSTom Herbert *err = -EAGAIN; 1330ab7ac4ebSTom Herbert return NULL; 1331ab7ac4ebSTom Herbert } 1332ab7ac4ebSTom Herbert 1333ab7ac4ebSTom Herbert sk_wait_data(sk, &timeo, NULL); 1334ab7ac4ebSTom Herbert 1335ab7ac4ebSTom Herbert /* Handle signals */ 1336ab7ac4ebSTom Herbert if (signal_pending(current)) { 1337ab7ac4ebSTom Herbert *err = sock_intr_errno(timeo); 1338ab7ac4ebSTom Herbert return NULL; 1339ab7ac4ebSTom Herbert } 1340ab7ac4ebSTom Herbert } 1341ab7ac4ebSTom Herbert 1342ab7ac4ebSTom Herbert return skb; 1343ab7ac4ebSTom Herbert } 1344ab7ac4ebSTom Herbert 1345ab7ac4ebSTom Herbert static int kcm_recvmsg(struct socket *sock, struct msghdr *msg, 1346ab7ac4ebSTom Herbert size_t len, int flags) 1347ab7ac4ebSTom Herbert { 1348ab7ac4ebSTom Herbert struct sock *sk = sock->sk; 1349cd6e111bSTom Herbert struct kcm_sock *kcm = kcm_sk(sk); 1350ab7ac4ebSTom Herbert int err = 0; 1351ab7ac4ebSTom Herbert long timeo; 1352ab7ac4ebSTom Herbert struct kcm_rx_msg *rxm; 1353ab7ac4ebSTom Herbert int copied = 0; 1354ab7ac4ebSTom Herbert struct sk_buff *skb; 1355ab7ac4ebSTom Herbert 1356ab7ac4ebSTom Herbert timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1357ab7ac4ebSTom Herbert 1358ab7ac4ebSTom Herbert lock_sock(sk); 1359ab7ac4ebSTom Herbert 1360ab7ac4ebSTom Herbert skb = kcm_wait_data(sk, flags, timeo, &err); 1361ab7ac4ebSTom Herbert if (!skb) 1362ab7ac4ebSTom Herbert goto out; 1363ab7ac4ebSTom Herbert 1364ab7ac4ebSTom Herbert /* Okay, have a message on the receive queue */ 1365ab7ac4ebSTom Herbert 1366ab7ac4ebSTom Herbert rxm = kcm_rx_msg(skb); 1367ab7ac4ebSTom Herbert 1368ab7ac4ebSTom Herbert if (len > rxm->full_len) 1369ab7ac4ebSTom Herbert len = rxm->full_len; 1370ab7ac4ebSTom Herbert 1371ab7ac4ebSTom Herbert err = skb_copy_datagram_msg(skb, rxm->offset, msg, len); 1372ab7ac4ebSTom Herbert if (err < 0) 1373ab7ac4ebSTom Herbert goto out; 1374ab7ac4ebSTom Herbert 1375ab7ac4ebSTom Herbert copied = len; 1376ab7ac4ebSTom Herbert if (likely(!(flags & MSG_PEEK))) { 1377cd6e111bSTom Herbert KCM_STATS_ADD(kcm->stats.rx_bytes, copied); 1378ab7ac4ebSTom Herbert if (copied < rxm->full_len) { 1379ab7ac4ebSTom Herbert if (sock->type == SOCK_DGRAM) { 1380ab7ac4ebSTom Herbert /* Truncated message */ 1381ab7ac4ebSTom Herbert msg->msg_flags |= MSG_TRUNC; 1382ab7ac4ebSTom Herbert goto msg_finished; 1383ab7ac4ebSTom Herbert } 1384ab7ac4ebSTom Herbert rxm->offset += copied; 1385ab7ac4ebSTom Herbert rxm->full_len -= copied; 1386ab7ac4ebSTom Herbert } else { 1387ab7ac4ebSTom Herbert msg_finished: 1388ab7ac4ebSTom Herbert /* Finished with message */ 1389ab7ac4ebSTom Herbert msg->msg_flags |= MSG_EOR; 1390cd6e111bSTom Herbert KCM_STATS_INCR(kcm->stats.rx_msgs); 1391ab7ac4ebSTom Herbert skb_unlink(skb, &sk->sk_receive_queue); 1392ab7ac4ebSTom Herbert kfree_skb(skb); 1393ab7ac4ebSTom Herbert } 1394ab7ac4ebSTom Herbert } 1395ab7ac4ebSTom Herbert 1396ab7ac4ebSTom Herbert out: 1397ab7ac4ebSTom Herbert release_sock(sk); 1398ab7ac4ebSTom Herbert 1399ab7ac4ebSTom Herbert return copied ? : err; 1400ab7ac4ebSTom Herbert } 1401ab7ac4ebSTom Herbert 140291687355STom Herbert static ssize_t kcm_sock_splice(struct sock *sk, 140391687355STom Herbert struct pipe_inode_info *pipe, 140491687355STom Herbert struct splice_pipe_desc *spd) 140591687355STom Herbert { 140691687355STom Herbert int ret; 140791687355STom Herbert 140891687355STom Herbert release_sock(sk); 140991687355STom Herbert ret = splice_to_pipe(pipe, spd); 141091687355STom Herbert lock_sock(sk); 141191687355STom Herbert 141291687355STom Herbert return ret; 141391687355STom Herbert } 141491687355STom Herbert 141591687355STom Herbert static ssize_t kcm_splice_read(struct socket *sock, loff_t *ppos, 141691687355STom Herbert struct pipe_inode_info *pipe, size_t len, 141791687355STom Herbert unsigned int flags) 141891687355STom Herbert { 141991687355STom Herbert struct sock *sk = sock->sk; 142091687355STom Herbert struct kcm_sock *kcm = kcm_sk(sk); 142191687355STom Herbert long timeo; 142291687355STom Herbert struct kcm_rx_msg *rxm; 142391687355STom Herbert int err = 0; 142491687355STom Herbert size_t copied; 142591687355STom Herbert struct sk_buff *skb; 142691687355STom Herbert 142791687355STom Herbert /* Only support splice for SOCKSEQPACKET */ 142891687355STom Herbert 142991687355STom Herbert timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 143091687355STom Herbert 143191687355STom Herbert lock_sock(sk); 143291687355STom Herbert 143391687355STom Herbert skb = kcm_wait_data(sk, flags, timeo, &err); 143491687355STom Herbert if (!skb) 143591687355STom Herbert goto err_out; 143691687355STom Herbert 143791687355STom Herbert /* Okay, have a message on the receive queue */ 143891687355STom Herbert 143991687355STom Herbert rxm = kcm_rx_msg(skb); 144091687355STom Herbert 144191687355STom Herbert if (len > rxm->full_len) 144291687355STom Herbert len = rxm->full_len; 144391687355STom Herbert 144491687355STom Herbert copied = skb_splice_bits(skb, sk, rxm->offset, pipe, len, flags, 144591687355STom Herbert kcm_sock_splice); 144691687355STom Herbert if (copied < 0) { 144791687355STom Herbert err = copied; 144891687355STom Herbert goto err_out; 144991687355STom Herbert } 145091687355STom Herbert 145191687355STom Herbert KCM_STATS_ADD(kcm->stats.rx_bytes, copied); 145291687355STom Herbert 145391687355STom Herbert rxm->offset += copied; 145491687355STom Herbert rxm->full_len -= copied; 145591687355STom Herbert 145691687355STom Herbert /* We have no way to return MSG_EOR. If all the bytes have been 145791687355STom Herbert * read we still leave the message in the receive socket buffer. 145891687355STom Herbert * A subsequent recvmsg needs to be done to return MSG_EOR and 145991687355STom Herbert * finish reading the message. 146091687355STom Herbert */ 146191687355STom Herbert 146291687355STom Herbert release_sock(sk); 146391687355STom Herbert 146491687355STom Herbert return copied; 146591687355STom Herbert 146691687355STom Herbert err_out: 146791687355STom Herbert release_sock(sk); 146891687355STom Herbert 146991687355STom Herbert return err; 147091687355STom Herbert } 147191687355STom Herbert 1472ab7ac4ebSTom Herbert /* kcm sock lock held */ 1473ab7ac4ebSTom Herbert static void kcm_recv_disable(struct kcm_sock *kcm) 1474ab7ac4ebSTom Herbert { 1475ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 1476ab7ac4ebSTom Herbert 1477ab7ac4ebSTom Herbert if (kcm->rx_disabled) 1478ab7ac4ebSTom Herbert return; 1479ab7ac4ebSTom Herbert 1480ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 1481ab7ac4ebSTom Herbert 1482ab7ac4ebSTom Herbert kcm->rx_disabled = 1; 1483ab7ac4ebSTom Herbert 1484ab7ac4ebSTom Herbert /* If a psock is reserved we'll do cleanup in unreserve */ 1485ab7ac4ebSTom Herbert if (!kcm->rx_psock) { 1486ab7ac4ebSTom Herbert if (kcm->rx_wait) { 1487ab7ac4ebSTom Herbert list_del(&kcm->wait_rx_list); 1488ab7ac4ebSTom Herbert kcm->rx_wait = false; 1489ab7ac4ebSTom Herbert } 1490ab7ac4ebSTom Herbert 1491ab7ac4ebSTom Herbert requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue); 1492ab7ac4ebSTom Herbert } 1493ab7ac4ebSTom Herbert 1494ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 1495ab7ac4ebSTom Herbert } 1496ab7ac4ebSTom Herbert 1497ab7ac4ebSTom Herbert /* kcm sock lock held */ 1498ab7ac4ebSTom Herbert static void kcm_recv_enable(struct kcm_sock *kcm) 1499ab7ac4ebSTom Herbert { 1500ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 1501ab7ac4ebSTom Herbert 1502ab7ac4ebSTom Herbert if (!kcm->rx_disabled) 1503ab7ac4ebSTom Herbert return; 1504ab7ac4ebSTom Herbert 1505ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 1506ab7ac4ebSTom Herbert 1507ab7ac4ebSTom Herbert kcm->rx_disabled = 0; 1508ab7ac4ebSTom Herbert kcm_rcv_ready(kcm); 1509ab7ac4ebSTom Herbert 1510ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 1511ab7ac4ebSTom Herbert } 1512ab7ac4ebSTom Herbert 1513ab7ac4ebSTom Herbert static int kcm_setsockopt(struct socket *sock, int level, int optname, 1514ab7ac4ebSTom Herbert char __user *optval, unsigned int optlen) 1515ab7ac4ebSTom Herbert { 1516ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sock->sk); 1517ab7ac4ebSTom Herbert int val, valbool; 1518ab7ac4ebSTom Herbert int err = 0; 1519ab7ac4ebSTom Herbert 1520ab7ac4ebSTom Herbert if (level != SOL_KCM) 1521ab7ac4ebSTom Herbert return -ENOPROTOOPT; 1522ab7ac4ebSTom Herbert 1523ab7ac4ebSTom Herbert if (optlen < sizeof(int)) 1524ab7ac4ebSTom Herbert return -EINVAL; 1525ab7ac4ebSTom Herbert 1526ab7ac4ebSTom Herbert if (get_user(val, (int __user *)optval)) 1527ab7ac4ebSTom Herbert return -EINVAL; 1528ab7ac4ebSTom Herbert 1529ab7ac4ebSTom Herbert valbool = val ? 1 : 0; 1530ab7ac4ebSTom Herbert 1531ab7ac4ebSTom Herbert switch (optname) { 1532ab7ac4ebSTom Herbert case KCM_RECV_DISABLE: 1533ab7ac4ebSTom Herbert lock_sock(&kcm->sk); 1534ab7ac4ebSTom Herbert if (valbool) 1535ab7ac4ebSTom Herbert kcm_recv_disable(kcm); 1536ab7ac4ebSTom Herbert else 1537ab7ac4ebSTom Herbert kcm_recv_enable(kcm); 1538ab7ac4ebSTom Herbert release_sock(&kcm->sk); 1539ab7ac4ebSTom Herbert break; 1540ab7ac4ebSTom Herbert default: 1541ab7ac4ebSTom Herbert err = -ENOPROTOOPT; 1542ab7ac4ebSTom Herbert } 1543ab7ac4ebSTom Herbert 1544ab7ac4ebSTom Herbert return err; 1545ab7ac4ebSTom Herbert } 1546ab7ac4ebSTom Herbert 1547ab7ac4ebSTom Herbert static int kcm_getsockopt(struct socket *sock, int level, int optname, 1548ab7ac4ebSTom Herbert char __user *optval, int __user *optlen) 1549ab7ac4ebSTom Herbert { 1550ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sock->sk); 1551ab7ac4ebSTom Herbert int val, len; 1552ab7ac4ebSTom Herbert 1553ab7ac4ebSTom Herbert if (level != SOL_KCM) 1554ab7ac4ebSTom Herbert return -ENOPROTOOPT; 1555ab7ac4ebSTom Herbert 1556ab7ac4ebSTom Herbert if (get_user(len, optlen)) 1557ab7ac4ebSTom Herbert return -EFAULT; 1558ab7ac4ebSTom Herbert 1559ab7ac4ebSTom Herbert len = min_t(unsigned int, len, sizeof(int)); 1560ab7ac4ebSTom Herbert if (len < 0) 1561ab7ac4ebSTom Herbert return -EINVAL; 1562ab7ac4ebSTom Herbert 1563ab7ac4ebSTom Herbert switch (optname) { 1564ab7ac4ebSTom Herbert case KCM_RECV_DISABLE: 1565ab7ac4ebSTom Herbert val = kcm->rx_disabled; 1566ab7ac4ebSTom Herbert break; 1567ab7ac4ebSTom Herbert default: 1568ab7ac4ebSTom Herbert return -ENOPROTOOPT; 1569ab7ac4ebSTom Herbert } 1570ab7ac4ebSTom Herbert 1571ab7ac4ebSTom Herbert if (put_user(len, optlen)) 1572ab7ac4ebSTom Herbert return -EFAULT; 1573ab7ac4ebSTom Herbert if (copy_to_user(optval, &val, len)) 1574ab7ac4ebSTom Herbert return -EFAULT; 1575ab7ac4ebSTom Herbert return 0; 1576ab7ac4ebSTom Herbert } 1577ab7ac4ebSTom Herbert 1578ab7ac4ebSTom Herbert static void init_kcm_sock(struct kcm_sock *kcm, struct kcm_mux *mux) 1579ab7ac4ebSTom Herbert { 1580ab7ac4ebSTom Herbert struct kcm_sock *tkcm; 1581ab7ac4ebSTom Herbert struct list_head *head; 1582ab7ac4ebSTom Herbert int index = 0; 1583ab7ac4ebSTom Herbert 1584ab7ac4ebSTom Herbert /* For SOCK_SEQPACKET sock type, datagram_poll checks the sk_state, so 1585ab7ac4ebSTom Herbert * we set sk_state, otherwise epoll_wait always returns right away with 1586ab7ac4ebSTom Herbert * POLLHUP 1587ab7ac4ebSTom Herbert */ 1588ab7ac4ebSTom Herbert kcm->sk.sk_state = TCP_ESTABLISHED; 1589ab7ac4ebSTom Herbert 1590ab7ac4ebSTom Herbert /* Add to mux's kcm sockets list */ 1591ab7ac4ebSTom Herbert kcm->mux = mux; 1592ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 1593ab7ac4ebSTom Herbert 1594ab7ac4ebSTom Herbert head = &mux->kcm_socks; 1595ab7ac4ebSTom Herbert list_for_each_entry(tkcm, &mux->kcm_socks, kcm_sock_list) { 1596ab7ac4ebSTom Herbert if (tkcm->index != index) 1597ab7ac4ebSTom Herbert break; 1598ab7ac4ebSTom Herbert head = &tkcm->kcm_sock_list; 1599ab7ac4ebSTom Herbert index++; 1600ab7ac4ebSTom Herbert } 1601ab7ac4ebSTom Herbert 1602ab7ac4ebSTom Herbert list_add(&kcm->kcm_sock_list, head); 1603ab7ac4ebSTom Herbert kcm->index = index; 1604ab7ac4ebSTom Herbert 1605ab7ac4ebSTom Herbert mux->kcm_socks_cnt++; 1606ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1607ab7ac4ebSTom Herbert 1608ab7ac4ebSTom Herbert INIT_WORK(&kcm->tx_work, kcm_tx_work); 1609ab7ac4ebSTom Herbert 1610ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 1611ab7ac4ebSTom Herbert kcm_rcv_ready(kcm); 1612ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 1613ab7ac4ebSTom Herbert } 1614ab7ac4ebSTom Herbert 1615ab7ac4ebSTom Herbert static int kcm_attach(struct socket *sock, struct socket *csock, 1616ab7ac4ebSTom Herbert struct bpf_prog *prog) 1617ab7ac4ebSTom Herbert { 1618ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sock->sk); 1619ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 1620ab7ac4ebSTom Herbert struct sock *csk; 1621ab7ac4ebSTom Herbert struct kcm_psock *psock = NULL, *tpsock; 1622ab7ac4ebSTom Herbert struct list_head *head; 1623ab7ac4ebSTom Herbert int index = 0; 1624ab7ac4ebSTom Herbert 1625ab7ac4ebSTom Herbert if (csock->ops->family != PF_INET && 1626ab7ac4ebSTom Herbert csock->ops->family != PF_INET6) 1627ab7ac4ebSTom Herbert return -EINVAL; 1628ab7ac4ebSTom Herbert 1629ab7ac4ebSTom Herbert csk = csock->sk; 1630ab7ac4ebSTom Herbert if (!csk) 1631ab7ac4ebSTom Herbert return -EINVAL; 1632ab7ac4ebSTom Herbert 1633ab7ac4ebSTom Herbert /* Only support TCP for now */ 1634ab7ac4ebSTom Herbert if (csk->sk_protocol != IPPROTO_TCP) 1635ab7ac4ebSTom Herbert return -EINVAL; 1636ab7ac4ebSTom Herbert 1637ab7ac4ebSTom Herbert psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL); 1638ab7ac4ebSTom Herbert if (!psock) 1639ab7ac4ebSTom Herbert return -ENOMEM; 1640ab7ac4ebSTom Herbert 1641ab7ac4ebSTom Herbert psock->mux = mux; 1642ab7ac4ebSTom Herbert psock->sk = csk; 1643ab7ac4ebSTom Herbert psock->bpf_prog = prog; 1644ab7ac4ebSTom Herbert INIT_WORK(&psock->rx_work, psock_rx_work); 1645ab7ac4ebSTom Herbert INIT_DELAYED_WORK(&psock->rx_delayed_work, psock_rx_delayed_work); 1646ab7ac4ebSTom Herbert 1647ab7ac4ebSTom Herbert sock_hold(csk); 1648ab7ac4ebSTom Herbert 1649ab7ac4ebSTom Herbert write_lock_bh(&csk->sk_callback_lock); 1650ab7ac4ebSTom Herbert psock->save_data_ready = csk->sk_data_ready; 1651ab7ac4ebSTom Herbert psock->save_write_space = csk->sk_write_space; 1652ab7ac4ebSTom Herbert psock->save_state_change = csk->sk_state_change; 1653ab7ac4ebSTom Herbert csk->sk_user_data = psock; 1654ab7ac4ebSTom Herbert csk->sk_data_ready = psock_tcp_data_ready; 1655ab7ac4ebSTom Herbert csk->sk_write_space = psock_tcp_write_space; 1656ab7ac4ebSTom Herbert csk->sk_state_change = psock_tcp_state_change; 1657ab7ac4ebSTom Herbert write_unlock_bh(&csk->sk_callback_lock); 1658ab7ac4ebSTom Herbert 1659ab7ac4ebSTom Herbert /* Finished initialization, now add the psock to the MUX. */ 1660ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 1661ab7ac4ebSTom Herbert head = &mux->psocks; 1662ab7ac4ebSTom Herbert list_for_each_entry(tpsock, &mux->psocks, psock_list) { 1663ab7ac4ebSTom Herbert if (tpsock->index != index) 1664ab7ac4ebSTom Herbert break; 1665ab7ac4ebSTom Herbert head = &tpsock->psock_list; 1666ab7ac4ebSTom Herbert index++; 1667ab7ac4ebSTom Herbert } 1668ab7ac4ebSTom Herbert 1669ab7ac4ebSTom Herbert list_add(&psock->psock_list, head); 1670ab7ac4ebSTom Herbert psock->index = index; 1671ab7ac4ebSTom Herbert 1672cd6e111bSTom Herbert KCM_STATS_INCR(mux->stats.psock_attach); 1673ab7ac4ebSTom Herbert mux->psocks_cnt++; 1674ab7ac4ebSTom Herbert psock_now_avail(psock); 1675ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1676ab7ac4ebSTom Herbert 1677ab7ac4ebSTom Herbert /* Schedule RX work in case there are already bytes queued */ 1678ab7ac4ebSTom Herbert queue_work(kcm_wq, &psock->rx_work); 1679ab7ac4ebSTom Herbert 1680ab7ac4ebSTom Herbert return 0; 1681ab7ac4ebSTom Herbert } 1682ab7ac4ebSTom Herbert 1683ab7ac4ebSTom Herbert static int kcm_attach_ioctl(struct socket *sock, struct kcm_attach *info) 1684ab7ac4ebSTom Herbert { 1685ab7ac4ebSTom Herbert struct socket *csock; 1686ab7ac4ebSTom Herbert struct bpf_prog *prog; 1687ab7ac4ebSTom Herbert int err; 1688ab7ac4ebSTom Herbert 1689ab7ac4ebSTom Herbert csock = sockfd_lookup(info->fd, &err); 1690ab7ac4ebSTom Herbert if (!csock) 1691ab7ac4ebSTom Herbert return -ENOENT; 1692ab7ac4ebSTom Herbert 1693ab7ac4ebSTom Herbert prog = bpf_prog_get(info->bpf_fd); 1694ab7ac4ebSTom Herbert if (IS_ERR(prog)) { 1695ab7ac4ebSTom Herbert err = PTR_ERR(prog); 1696ab7ac4ebSTom Herbert goto out; 1697ab7ac4ebSTom Herbert } 1698ab7ac4ebSTom Herbert 1699ab7ac4ebSTom Herbert if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) { 1700ab7ac4ebSTom Herbert bpf_prog_put(prog); 1701ab7ac4ebSTom Herbert err = -EINVAL; 1702ab7ac4ebSTom Herbert goto out; 1703ab7ac4ebSTom Herbert } 1704ab7ac4ebSTom Herbert 1705ab7ac4ebSTom Herbert err = kcm_attach(sock, csock, prog); 1706ab7ac4ebSTom Herbert if (err) { 1707ab7ac4ebSTom Herbert bpf_prog_put(prog); 1708ab7ac4ebSTom Herbert goto out; 1709ab7ac4ebSTom Herbert } 1710ab7ac4ebSTom Herbert 1711ab7ac4ebSTom Herbert /* Keep reference on file also */ 1712ab7ac4ebSTom Herbert 1713ab7ac4ebSTom Herbert return 0; 1714ab7ac4ebSTom Herbert out: 1715ab7ac4ebSTom Herbert fput(csock->file); 1716ab7ac4ebSTom Herbert return err; 1717ab7ac4ebSTom Herbert } 1718ab7ac4ebSTom Herbert 1719ab7ac4ebSTom Herbert static void kcm_unattach(struct kcm_psock *psock) 1720ab7ac4ebSTom Herbert { 1721ab7ac4ebSTom Herbert struct sock *csk = psock->sk; 1722ab7ac4ebSTom Herbert struct kcm_mux *mux = psock->mux; 1723ab7ac4ebSTom Herbert 1724ab7ac4ebSTom Herbert /* Stop getting callbacks from TCP socket. After this there should 1725ab7ac4ebSTom Herbert * be no way to reserve a kcm for this psock. 1726ab7ac4ebSTom Herbert */ 1727ab7ac4ebSTom Herbert write_lock_bh(&csk->sk_callback_lock); 1728ab7ac4ebSTom Herbert csk->sk_user_data = NULL; 1729ab7ac4ebSTom Herbert csk->sk_data_ready = psock->save_data_ready; 1730ab7ac4ebSTom Herbert csk->sk_write_space = psock->save_write_space; 1731ab7ac4ebSTom Herbert csk->sk_state_change = psock->save_state_change; 1732ab7ac4ebSTom Herbert psock->rx_stopped = 1; 1733ab7ac4ebSTom Herbert 1734ab7ac4ebSTom Herbert if (WARN_ON(psock->rx_kcm)) { 1735ab7ac4ebSTom Herbert write_unlock_bh(&csk->sk_callback_lock); 1736ab7ac4ebSTom Herbert return; 1737ab7ac4ebSTom Herbert } 1738ab7ac4ebSTom Herbert 1739ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 1740ab7ac4ebSTom Herbert 1741ab7ac4ebSTom Herbert /* Stop receiver activities. After this point psock should not be 1742ab7ac4ebSTom Herbert * able to get onto ready list either through callbacks or work. 1743ab7ac4ebSTom Herbert */ 1744ab7ac4ebSTom Herbert if (psock->ready_rx_msg) { 1745ab7ac4ebSTom Herbert list_del(&psock->psock_ready_list); 1746ab7ac4ebSTom Herbert kfree_skb(psock->ready_rx_msg); 1747ab7ac4ebSTom Herbert psock->ready_rx_msg = NULL; 1748cd6e111bSTom Herbert KCM_STATS_INCR(mux->stats.rx_ready_drops); 1749ab7ac4ebSTom Herbert } 1750ab7ac4ebSTom Herbert 1751ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 1752ab7ac4ebSTom Herbert 1753ab7ac4ebSTom Herbert write_unlock_bh(&csk->sk_callback_lock); 1754ab7ac4ebSTom Herbert 1755ab7ac4ebSTom Herbert cancel_work_sync(&psock->rx_work); 1756ab7ac4ebSTom Herbert cancel_delayed_work_sync(&psock->rx_delayed_work); 1757ab7ac4ebSTom Herbert 1758ab7ac4ebSTom Herbert bpf_prog_put(psock->bpf_prog); 1759ab7ac4ebSTom Herbert 1760ab7ac4ebSTom Herbert kfree_skb(psock->rx_skb_head); 1761ab7ac4ebSTom Herbert psock->rx_skb_head = NULL; 1762ab7ac4ebSTom Herbert 1763ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 1764ab7ac4ebSTom Herbert 1765cd6e111bSTom Herbert aggregate_psock_stats(&psock->stats, &mux->aggregate_psock_stats); 1766cd6e111bSTom Herbert 1767cd6e111bSTom Herbert KCM_STATS_INCR(mux->stats.psock_unattach); 1768cd6e111bSTom Herbert 1769ab7ac4ebSTom Herbert if (psock->tx_kcm) { 1770ab7ac4ebSTom Herbert /* psock was reserved. Just mark it finished and we will clean 1771ab7ac4ebSTom Herbert * up in the kcm paths, we need kcm lock which can not be 1772ab7ac4ebSTom Herbert * acquired here. 1773ab7ac4ebSTom Herbert */ 1774cd6e111bSTom Herbert KCM_STATS_INCR(mux->stats.psock_unattach_rsvd); 1775ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1776ab7ac4ebSTom Herbert 1777ab7ac4ebSTom Herbert /* We are unattaching a socket that is reserved. Abort the 1778ab7ac4ebSTom Herbert * socket since we may be out of sync in sending on it. We need 1779ab7ac4ebSTom Herbert * to do this without the mux lock. 1780ab7ac4ebSTom Herbert */ 1781ab7ac4ebSTom Herbert kcm_abort_tx_psock(psock, EPIPE, false); 1782ab7ac4ebSTom Herbert 1783ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 1784ab7ac4ebSTom Herbert if (!psock->tx_kcm) { 1785ab7ac4ebSTom Herbert /* psock now unreserved in window mux was unlocked */ 1786ab7ac4ebSTom Herbert goto no_reserved; 1787ab7ac4ebSTom Herbert } 1788ab7ac4ebSTom Herbert psock->done = 1; 1789ab7ac4ebSTom Herbert 1790ab7ac4ebSTom Herbert /* Commit done before queuing work to process it */ 1791ab7ac4ebSTom Herbert smp_mb(); 1792ab7ac4ebSTom Herbert 1793ab7ac4ebSTom Herbert /* Queue tx work to make sure psock->done is handled */ 1794ab7ac4ebSTom Herbert queue_work(kcm_wq, &psock->tx_kcm->tx_work); 1795ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1796ab7ac4ebSTom Herbert } else { 1797ab7ac4ebSTom Herbert no_reserved: 1798ab7ac4ebSTom Herbert if (!psock->tx_stopped) 1799ab7ac4ebSTom Herbert list_del(&psock->psock_avail_list); 1800ab7ac4ebSTom Herbert list_del(&psock->psock_list); 1801ab7ac4ebSTom Herbert mux->psocks_cnt--; 1802ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1803ab7ac4ebSTom Herbert 1804ab7ac4ebSTom Herbert sock_put(csk); 1805ab7ac4ebSTom Herbert fput(csk->sk_socket->file); 1806ab7ac4ebSTom Herbert kmem_cache_free(kcm_psockp, psock); 1807ab7ac4ebSTom Herbert } 1808ab7ac4ebSTom Herbert } 1809ab7ac4ebSTom Herbert 1810ab7ac4ebSTom Herbert static int kcm_unattach_ioctl(struct socket *sock, struct kcm_unattach *info) 1811ab7ac4ebSTom Herbert { 1812ab7ac4ebSTom Herbert struct kcm_sock *kcm = kcm_sk(sock->sk); 1813ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 1814ab7ac4ebSTom Herbert struct kcm_psock *psock; 1815ab7ac4ebSTom Herbert struct socket *csock; 1816ab7ac4ebSTom Herbert struct sock *csk; 1817ab7ac4ebSTom Herbert int err; 1818ab7ac4ebSTom Herbert 1819ab7ac4ebSTom Herbert csock = sockfd_lookup(info->fd, &err); 1820ab7ac4ebSTom Herbert if (!csock) 1821ab7ac4ebSTom Herbert return -ENOENT; 1822ab7ac4ebSTom Herbert 1823ab7ac4ebSTom Herbert csk = csock->sk; 1824ab7ac4ebSTom Herbert if (!csk) { 1825ab7ac4ebSTom Herbert err = -EINVAL; 1826ab7ac4ebSTom Herbert goto out; 1827ab7ac4ebSTom Herbert } 1828ab7ac4ebSTom Herbert 1829ab7ac4ebSTom Herbert err = -ENOENT; 1830ab7ac4ebSTom Herbert 1831ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 1832ab7ac4ebSTom Herbert 1833ab7ac4ebSTom Herbert list_for_each_entry(psock, &mux->psocks, psock_list) { 1834ab7ac4ebSTom Herbert if (psock->sk != csk) 1835ab7ac4ebSTom Herbert continue; 1836ab7ac4ebSTom Herbert 1837ab7ac4ebSTom Herbert /* Found the matching psock */ 1838ab7ac4ebSTom Herbert 1839ab7ac4ebSTom Herbert if (psock->unattaching || WARN_ON(psock->done)) { 1840ab7ac4ebSTom Herbert err = -EALREADY; 1841ab7ac4ebSTom Herbert break; 1842ab7ac4ebSTom Herbert } 1843ab7ac4ebSTom Herbert 1844ab7ac4ebSTom Herbert psock->unattaching = 1; 1845ab7ac4ebSTom Herbert 1846ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1847ab7ac4ebSTom Herbert 1848ab7ac4ebSTom Herbert kcm_unattach(psock); 1849ab7ac4ebSTom Herbert 1850ab7ac4ebSTom Herbert err = 0; 1851ab7ac4ebSTom Herbert goto out; 1852ab7ac4ebSTom Herbert } 1853ab7ac4ebSTom Herbert 1854ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 1855ab7ac4ebSTom Herbert 1856ab7ac4ebSTom Herbert out: 1857ab7ac4ebSTom Herbert fput(csock->file); 1858ab7ac4ebSTom Herbert return err; 1859ab7ac4ebSTom Herbert } 1860ab7ac4ebSTom Herbert 1861ab7ac4ebSTom Herbert static struct proto kcm_proto = { 1862ab7ac4ebSTom Herbert .name = "KCM", 1863ab7ac4ebSTom Herbert .owner = THIS_MODULE, 1864ab7ac4ebSTom Herbert .obj_size = sizeof(struct kcm_sock), 1865ab7ac4ebSTom Herbert }; 1866ab7ac4ebSTom Herbert 1867ab7ac4ebSTom Herbert /* Clone a kcm socket. */ 1868ab7ac4ebSTom Herbert static int kcm_clone(struct socket *osock, struct kcm_clone *info, 1869ab7ac4ebSTom Herbert struct socket **newsockp) 1870ab7ac4ebSTom Herbert { 1871ab7ac4ebSTom Herbert struct socket *newsock; 1872ab7ac4ebSTom Herbert struct sock *newsk; 1873ab7ac4ebSTom Herbert struct file *newfile; 1874ab7ac4ebSTom Herbert int err, newfd; 1875ab7ac4ebSTom Herbert 1876ab7ac4ebSTom Herbert err = -ENFILE; 1877ab7ac4ebSTom Herbert newsock = sock_alloc(); 1878ab7ac4ebSTom Herbert if (!newsock) 1879ab7ac4ebSTom Herbert goto out; 1880ab7ac4ebSTom Herbert 1881ab7ac4ebSTom Herbert newsock->type = osock->type; 1882ab7ac4ebSTom Herbert newsock->ops = osock->ops; 1883ab7ac4ebSTom Herbert 1884ab7ac4ebSTom Herbert __module_get(newsock->ops->owner); 1885ab7ac4ebSTom Herbert 1886ab7ac4ebSTom Herbert newfd = get_unused_fd_flags(0); 1887ab7ac4ebSTom Herbert if (unlikely(newfd < 0)) { 1888ab7ac4ebSTom Herbert err = newfd; 1889ab7ac4ebSTom Herbert goto out_fd_fail; 1890ab7ac4ebSTom Herbert } 1891ab7ac4ebSTom Herbert 1892ab7ac4ebSTom Herbert newfile = sock_alloc_file(newsock, 0, osock->sk->sk_prot_creator->name); 1893ab7ac4ebSTom Herbert if (unlikely(IS_ERR(newfile))) { 1894ab7ac4ebSTom Herbert err = PTR_ERR(newfile); 1895ab7ac4ebSTom Herbert goto out_sock_alloc_fail; 1896ab7ac4ebSTom Herbert } 1897ab7ac4ebSTom Herbert 1898ab7ac4ebSTom Herbert newsk = sk_alloc(sock_net(osock->sk), PF_KCM, GFP_KERNEL, 1899ab7ac4ebSTom Herbert &kcm_proto, true); 1900ab7ac4ebSTom Herbert if (!newsk) { 1901ab7ac4ebSTom Herbert err = -ENOMEM; 1902ab7ac4ebSTom Herbert goto out_sk_alloc_fail; 1903ab7ac4ebSTom Herbert } 1904ab7ac4ebSTom Herbert 1905ab7ac4ebSTom Herbert sock_init_data(newsock, newsk); 1906ab7ac4ebSTom Herbert init_kcm_sock(kcm_sk(newsk), kcm_sk(osock->sk)->mux); 1907ab7ac4ebSTom Herbert 1908ab7ac4ebSTom Herbert fd_install(newfd, newfile); 1909ab7ac4ebSTom Herbert *newsockp = newsock; 1910ab7ac4ebSTom Herbert info->fd = newfd; 1911ab7ac4ebSTom Herbert 1912ab7ac4ebSTom Herbert return 0; 1913ab7ac4ebSTom Herbert 1914ab7ac4ebSTom Herbert out_sk_alloc_fail: 1915ab7ac4ebSTom Herbert fput(newfile); 1916ab7ac4ebSTom Herbert out_sock_alloc_fail: 1917ab7ac4ebSTom Herbert put_unused_fd(newfd); 1918ab7ac4ebSTom Herbert out_fd_fail: 1919ab7ac4ebSTom Herbert sock_release(newsock); 1920ab7ac4ebSTom Herbert out: 1921ab7ac4ebSTom Herbert return err; 1922ab7ac4ebSTom Herbert } 1923ab7ac4ebSTom Herbert 1924ab7ac4ebSTom Herbert static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 1925ab7ac4ebSTom Herbert { 1926ab7ac4ebSTom Herbert int err; 1927ab7ac4ebSTom Herbert 1928ab7ac4ebSTom Herbert switch (cmd) { 1929ab7ac4ebSTom Herbert case SIOCKCMATTACH: { 1930ab7ac4ebSTom Herbert struct kcm_attach info; 1931ab7ac4ebSTom Herbert 1932ab7ac4ebSTom Herbert if (copy_from_user(&info, (void __user *)arg, sizeof(info))) 1933ab7ac4ebSTom Herbert err = -EFAULT; 1934ab7ac4ebSTom Herbert 1935ab7ac4ebSTom Herbert err = kcm_attach_ioctl(sock, &info); 1936ab7ac4ebSTom Herbert 1937ab7ac4ebSTom Herbert break; 1938ab7ac4ebSTom Herbert } 1939ab7ac4ebSTom Herbert case SIOCKCMUNATTACH: { 1940ab7ac4ebSTom Herbert struct kcm_unattach info; 1941ab7ac4ebSTom Herbert 1942ab7ac4ebSTom Herbert if (copy_from_user(&info, (void __user *)arg, sizeof(info))) 1943ab7ac4ebSTom Herbert err = -EFAULT; 1944ab7ac4ebSTom Herbert 1945ab7ac4ebSTom Herbert err = kcm_unattach_ioctl(sock, &info); 1946ab7ac4ebSTom Herbert 1947ab7ac4ebSTom Herbert break; 1948ab7ac4ebSTom Herbert } 1949ab7ac4ebSTom Herbert case SIOCKCMCLONE: { 1950ab7ac4ebSTom Herbert struct kcm_clone info; 1951ab7ac4ebSTom Herbert struct socket *newsock = NULL; 1952ab7ac4ebSTom Herbert 1953ab7ac4ebSTom Herbert if (copy_from_user(&info, (void __user *)arg, sizeof(info))) 1954ab7ac4ebSTom Herbert err = -EFAULT; 1955ab7ac4ebSTom Herbert 1956ab7ac4ebSTom Herbert err = kcm_clone(sock, &info, &newsock); 1957ab7ac4ebSTom Herbert 1958ab7ac4ebSTom Herbert if (!err) { 1959ab7ac4ebSTom Herbert if (copy_to_user((void __user *)arg, &info, 1960ab7ac4ebSTom Herbert sizeof(info))) { 1961ab7ac4ebSTom Herbert err = -EFAULT; 1962ab7ac4ebSTom Herbert sock_release(newsock); 1963ab7ac4ebSTom Herbert } 1964ab7ac4ebSTom Herbert } 1965ab7ac4ebSTom Herbert 1966ab7ac4ebSTom Herbert break; 1967ab7ac4ebSTom Herbert } 1968ab7ac4ebSTom Herbert default: 1969ab7ac4ebSTom Herbert err = -ENOIOCTLCMD; 1970ab7ac4ebSTom Herbert break; 1971ab7ac4ebSTom Herbert } 1972ab7ac4ebSTom Herbert 1973ab7ac4ebSTom Herbert return err; 1974ab7ac4ebSTom Herbert } 1975ab7ac4ebSTom Herbert 1976ab7ac4ebSTom Herbert static void free_mux(struct rcu_head *rcu) 1977ab7ac4ebSTom Herbert { 1978ab7ac4ebSTom Herbert struct kcm_mux *mux = container_of(rcu, 1979ab7ac4ebSTom Herbert struct kcm_mux, rcu); 1980ab7ac4ebSTom Herbert 1981ab7ac4ebSTom Herbert kmem_cache_free(kcm_muxp, mux); 1982ab7ac4ebSTom Herbert } 1983ab7ac4ebSTom Herbert 1984ab7ac4ebSTom Herbert static void release_mux(struct kcm_mux *mux) 1985ab7ac4ebSTom Herbert { 1986ab7ac4ebSTom Herbert struct kcm_net *knet = mux->knet; 1987ab7ac4ebSTom Herbert struct kcm_psock *psock, *tmp_psock; 1988ab7ac4ebSTom Herbert 1989ab7ac4ebSTom Herbert /* Release psocks */ 1990ab7ac4ebSTom Herbert list_for_each_entry_safe(psock, tmp_psock, 1991ab7ac4ebSTom Herbert &mux->psocks, psock_list) { 1992ab7ac4ebSTom Herbert if (!WARN_ON(psock->unattaching)) 1993ab7ac4ebSTom Herbert kcm_unattach(psock); 1994ab7ac4ebSTom Herbert } 1995ab7ac4ebSTom Herbert 1996ab7ac4ebSTom Herbert if (WARN_ON(mux->psocks_cnt)) 1997ab7ac4ebSTom Herbert return; 1998ab7ac4ebSTom Herbert 1999ab7ac4ebSTom Herbert __skb_queue_purge(&mux->rx_hold_queue); 2000ab7ac4ebSTom Herbert 2001ab7ac4ebSTom Herbert mutex_lock(&knet->mutex); 2002cd6e111bSTom Herbert aggregate_mux_stats(&mux->stats, &knet->aggregate_mux_stats); 2003cd6e111bSTom Herbert aggregate_psock_stats(&mux->aggregate_psock_stats, 2004cd6e111bSTom Herbert &knet->aggregate_psock_stats); 2005ab7ac4ebSTom Herbert list_del_rcu(&mux->kcm_mux_list); 2006ab7ac4ebSTom Herbert knet->count--; 2007ab7ac4ebSTom Herbert mutex_unlock(&knet->mutex); 2008ab7ac4ebSTom Herbert 2009ab7ac4ebSTom Herbert call_rcu(&mux->rcu, free_mux); 2010ab7ac4ebSTom Herbert } 2011ab7ac4ebSTom Herbert 2012ab7ac4ebSTom Herbert static void kcm_done(struct kcm_sock *kcm) 2013ab7ac4ebSTom Herbert { 2014ab7ac4ebSTom Herbert struct kcm_mux *mux = kcm->mux; 2015ab7ac4ebSTom Herbert struct sock *sk = &kcm->sk; 2016ab7ac4ebSTom Herbert int socks_cnt; 2017ab7ac4ebSTom Herbert 2018ab7ac4ebSTom Herbert spin_lock_bh(&mux->rx_lock); 2019ab7ac4ebSTom Herbert if (kcm->rx_psock) { 2020ab7ac4ebSTom Herbert /* Cleanup in unreserve_rx_kcm */ 2021ab7ac4ebSTom Herbert WARN_ON(kcm->done); 2022ab7ac4ebSTom Herbert kcm->rx_disabled = 1; 2023ab7ac4ebSTom Herbert kcm->done = 1; 2024ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 2025ab7ac4ebSTom Herbert return; 2026ab7ac4ebSTom Herbert } 2027ab7ac4ebSTom Herbert 2028ab7ac4ebSTom Herbert if (kcm->rx_wait) { 2029ab7ac4ebSTom Herbert list_del(&kcm->wait_rx_list); 2030ab7ac4ebSTom Herbert kcm->rx_wait = false; 2031ab7ac4ebSTom Herbert } 2032ab7ac4ebSTom Herbert /* Move any pending receive messages to other kcm sockets */ 2033ab7ac4ebSTom Herbert requeue_rx_msgs(mux, &sk->sk_receive_queue); 2034ab7ac4ebSTom Herbert 2035ab7ac4ebSTom Herbert spin_unlock_bh(&mux->rx_lock); 2036ab7ac4ebSTom Herbert 2037ab7ac4ebSTom Herbert if (WARN_ON(sk_rmem_alloc_get(sk))) 2038ab7ac4ebSTom Herbert return; 2039ab7ac4ebSTom Herbert 2040ab7ac4ebSTom Herbert /* Detach from MUX */ 2041ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 2042ab7ac4ebSTom Herbert 2043ab7ac4ebSTom Herbert list_del(&kcm->kcm_sock_list); 2044ab7ac4ebSTom Herbert mux->kcm_socks_cnt--; 2045ab7ac4ebSTom Herbert socks_cnt = mux->kcm_socks_cnt; 2046ab7ac4ebSTom Herbert 2047ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 2048ab7ac4ebSTom Herbert 2049ab7ac4ebSTom Herbert if (!socks_cnt) { 2050ab7ac4ebSTom Herbert /* We are done with the mux now. */ 2051ab7ac4ebSTom Herbert release_mux(mux); 2052ab7ac4ebSTom Herbert } 2053ab7ac4ebSTom Herbert 2054ab7ac4ebSTom Herbert WARN_ON(kcm->rx_wait); 2055ab7ac4ebSTom Herbert 2056ab7ac4ebSTom Herbert sock_put(&kcm->sk); 2057ab7ac4ebSTom Herbert } 2058ab7ac4ebSTom Herbert 2059ab7ac4ebSTom Herbert /* Called by kcm_release to close a KCM socket. 2060ab7ac4ebSTom Herbert * If this is the last KCM socket on the MUX, destroy the MUX. 2061ab7ac4ebSTom Herbert */ 2062ab7ac4ebSTom Herbert static int kcm_release(struct socket *sock) 2063ab7ac4ebSTom Herbert { 2064ab7ac4ebSTom Herbert struct sock *sk = sock->sk; 2065ab7ac4ebSTom Herbert struct kcm_sock *kcm; 2066ab7ac4ebSTom Herbert struct kcm_mux *mux; 2067ab7ac4ebSTom Herbert struct kcm_psock *psock; 2068ab7ac4ebSTom Herbert 2069ab7ac4ebSTom Herbert if (!sk) 2070ab7ac4ebSTom Herbert return 0; 2071ab7ac4ebSTom Herbert 2072ab7ac4ebSTom Herbert kcm = kcm_sk(sk); 2073ab7ac4ebSTom Herbert mux = kcm->mux; 2074ab7ac4ebSTom Herbert 2075ab7ac4ebSTom Herbert sock_orphan(sk); 2076ab7ac4ebSTom Herbert kfree_skb(kcm->seq_skb); 2077ab7ac4ebSTom Herbert 2078ab7ac4ebSTom Herbert lock_sock(sk); 2079ab7ac4ebSTom Herbert /* Purge queue under lock to avoid race condition with tx_work trying 2080ab7ac4ebSTom Herbert * to act when queue is nonempty. If tx_work runs after this point 2081ab7ac4ebSTom Herbert * it will just return. 2082ab7ac4ebSTom Herbert */ 2083ab7ac4ebSTom Herbert __skb_queue_purge(&sk->sk_write_queue); 2084ab7ac4ebSTom Herbert release_sock(sk); 2085ab7ac4ebSTom Herbert 2086ab7ac4ebSTom Herbert spin_lock_bh(&mux->lock); 2087ab7ac4ebSTom Herbert if (kcm->tx_wait) { 2088ab7ac4ebSTom Herbert /* Take of tx_wait list, after this point there should be no way 2089ab7ac4ebSTom Herbert * that a psock will be assigned to this kcm. 2090ab7ac4ebSTom Herbert */ 2091ab7ac4ebSTom Herbert list_del(&kcm->wait_psock_list); 2092ab7ac4ebSTom Herbert kcm->tx_wait = false; 2093ab7ac4ebSTom Herbert } 2094ab7ac4ebSTom Herbert spin_unlock_bh(&mux->lock); 2095ab7ac4ebSTom Herbert 2096ab7ac4ebSTom Herbert /* Cancel work. After this point there should be no outside references 2097ab7ac4ebSTom Herbert * to the kcm socket. 2098ab7ac4ebSTom Herbert */ 2099ab7ac4ebSTom Herbert cancel_work_sync(&kcm->tx_work); 2100ab7ac4ebSTom Herbert 2101ab7ac4ebSTom Herbert lock_sock(sk); 2102ab7ac4ebSTom Herbert psock = kcm->tx_psock; 2103ab7ac4ebSTom Herbert if (psock) { 2104ab7ac4ebSTom Herbert /* A psock was reserved, so we need to kill it since it 2105ab7ac4ebSTom Herbert * may already have some bytes queued from a message. We 2106ab7ac4ebSTom Herbert * need to do this after removing kcm from tx_wait list. 2107ab7ac4ebSTom Herbert */ 2108ab7ac4ebSTom Herbert kcm_abort_tx_psock(psock, EPIPE, false); 2109ab7ac4ebSTom Herbert unreserve_psock(kcm); 2110ab7ac4ebSTom Herbert } 2111ab7ac4ebSTom Herbert release_sock(sk); 2112ab7ac4ebSTom Herbert 2113ab7ac4ebSTom Herbert WARN_ON(kcm->tx_wait); 2114ab7ac4ebSTom Herbert WARN_ON(kcm->tx_psock); 2115ab7ac4ebSTom Herbert 2116ab7ac4ebSTom Herbert sock->sk = NULL; 2117ab7ac4ebSTom Herbert 2118ab7ac4ebSTom Herbert kcm_done(kcm); 2119ab7ac4ebSTom Herbert 2120ab7ac4ebSTom Herbert return 0; 2121ab7ac4ebSTom Herbert } 2122ab7ac4ebSTom Herbert 212391687355STom Herbert static const struct proto_ops kcm_dgram_ops = { 2124ab7ac4ebSTom Herbert .family = PF_KCM, 2125ab7ac4ebSTom Herbert .owner = THIS_MODULE, 2126ab7ac4ebSTom Herbert .release = kcm_release, 2127ab7ac4ebSTom Herbert .bind = sock_no_bind, 2128ab7ac4ebSTom Herbert .connect = sock_no_connect, 2129ab7ac4ebSTom Herbert .socketpair = sock_no_socketpair, 2130ab7ac4ebSTom Herbert .accept = sock_no_accept, 2131ab7ac4ebSTom Herbert .getname = sock_no_getname, 2132ab7ac4ebSTom Herbert .poll = datagram_poll, 2133ab7ac4ebSTom Herbert .ioctl = kcm_ioctl, 2134ab7ac4ebSTom Herbert .listen = sock_no_listen, 2135ab7ac4ebSTom Herbert .shutdown = sock_no_shutdown, 2136ab7ac4ebSTom Herbert .setsockopt = kcm_setsockopt, 2137ab7ac4ebSTom Herbert .getsockopt = kcm_getsockopt, 2138ab7ac4ebSTom Herbert .sendmsg = kcm_sendmsg, 2139ab7ac4ebSTom Herbert .recvmsg = kcm_recvmsg, 2140ab7ac4ebSTom Herbert .mmap = sock_no_mmap, 2141*f29698fcSTom Herbert .sendpage = kcm_sendpage, 2142ab7ac4ebSTom Herbert }; 2143ab7ac4ebSTom Herbert 214491687355STom Herbert static const struct proto_ops kcm_seqpacket_ops = { 214591687355STom Herbert .family = PF_KCM, 214691687355STom Herbert .owner = THIS_MODULE, 214791687355STom Herbert .release = kcm_release, 214891687355STom Herbert .bind = sock_no_bind, 214991687355STom Herbert .connect = sock_no_connect, 215091687355STom Herbert .socketpair = sock_no_socketpair, 215191687355STom Herbert .accept = sock_no_accept, 215291687355STom Herbert .getname = sock_no_getname, 215391687355STom Herbert .poll = datagram_poll, 215491687355STom Herbert .ioctl = kcm_ioctl, 215591687355STom Herbert .listen = sock_no_listen, 215691687355STom Herbert .shutdown = sock_no_shutdown, 215791687355STom Herbert .setsockopt = kcm_setsockopt, 215891687355STom Herbert .getsockopt = kcm_getsockopt, 215991687355STom Herbert .sendmsg = kcm_sendmsg, 216091687355STom Herbert .recvmsg = kcm_recvmsg, 216191687355STom Herbert .mmap = sock_no_mmap, 2162*f29698fcSTom Herbert .sendpage = kcm_sendpage, 216391687355STom Herbert .splice_read = kcm_splice_read, 216491687355STom Herbert }; 216591687355STom Herbert 2166ab7ac4ebSTom Herbert /* Create proto operation for kcm sockets */ 2167ab7ac4ebSTom Herbert static int kcm_create(struct net *net, struct socket *sock, 2168ab7ac4ebSTom Herbert int protocol, int kern) 2169ab7ac4ebSTom Herbert { 2170ab7ac4ebSTom Herbert struct kcm_net *knet = net_generic(net, kcm_net_id); 2171ab7ac4ebSTom Herbert struct sock *sk; 2172ab7ac4ebSTom Herbert struct kcm_mux *mux; 2173ab7ac4ebSTom Herbert 2174ab7ac4ebSTom Herbert switch (sock->type) { 2175ab7ac4ebSTom Herbert case SOCK_DGRAM: 217691687355STom Herbert sock->ops = &kcm_dgram_ops; 217791687355STom Herbert break; 2178ab7ac4ebSTom Herbert case SOCK_SEQPACKET: 217991687355STom Herbert sock->ops = &kcm_seqpacket_ops; 2180ab7ac4ebSTom Herbert break; 2181ab7ac4ebSTom Herbert default: 2182ab7ac4ebSTom Herbert return -ESOCKTNOSUPPORT; 2183ab7ac4ebSTom Herbert } 2184ab7ac4ebSTom Herbert 2185ab7ac4ebSTom Herbert if (protocol != KCMPROTO_CONNECTED) 2186ab7ac4ebSTom Herbert return -EPROTONOSUPPORT; 2187ab7ac4ebSTom Herbert 2188ab7ac4ebSTom Herbert sk = sk_alloc(net, PF_KCM, GFP_KERNEL, &kcm_proto, kern); 2189ab7ac4ebSTom Herbert if (!sk) 2190ab7ac4ebSTom Herbert return -ENOMEM; 2191ab7ac4ebSTom Herbert 2192ab7ac4ebSTom Herbert /* Allocate a kcm mux, shared between KCM sockets */ 2193ab7ac4ebSTom Herbert mux = kmem_cache_zalloc(kcm_muxp, GFP_KERNEL); 2194ab7ac4ebSTom Herbert if (!mux) { 2195ab7ac4ebSTom Herbert sk_free(sk); 2196ab7ac4ebSTom Herbert return -ENOMEM; 2197ab7ac4ebSTom Herbert } 2198ab7ac4ebSTom Herbert 2199ab7ac4ebSTom Herbert spin_lock_init(&mux->lock); 2200ab7ac4ebSTom Herbert spin_lock_init(&mux->rx_lock); 2201ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->kcm_socks); 2202ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->kcm_rx_waiters); 2203ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->kcm_tx_waiters); 2204ab7ac4ebSTom Herbert 2205ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->psocks); 2206ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->psocks_ready); 2207ab7ac4ebSTom Herbert INIT_LIST_HEAD(&mux->psocks_avail); 2208ab7ac4ebSTom Herbert 2209ab7ac4ebSTom Herbert mux->knet = knet; 2210ab7ac4ebSTom Herbert 2211ab7ac4ebSTom Herbert /* Add new MUX to list */ 2212ab7ac4ebSTom Herbert mutex_lock(&knet->mutex); 2213ab7ac4ebSTom Herbert list_add_rcu(&mux->kcm_mux_list, &knet->mux_list); 2214ab7ac4ebSTom Herbert knet->count++; 2215ab7ac4ebSTom Herbert mutex_unlock(&knet->mutex); 2216ab7ac4ebSTom Herbert 2217ab7ac4ebSTom Herbert skb_queue_head_init(&mux->rx_hold_queue); 2218ab7ac4ebSTom Herbert 2219ab7ac4ebSTom Herbert /* Init KCM socket */ 2220ab7ac4ebSTom Herbert sock_init_data(sock, sk); 2221ab7ac4ebSTom Herbert init_kcm_sock(kcm_sk(sk), mux); 2222ab7ac4ebSTom Herbert 2223ab7ac4ebSTom Herbert return 0; 2224ab7ac4ebSTom Herbert } 2225ab7ac4ebSTom Herbert 2226ab7ac4ebSTom Herbert static struct net_proto_family kcm_family_ops = { 2227ab7ac4ebSTom Herbert .family = PF_KCM, 2228ab7ac4ebSTom Herbert .create = kcm_create, 2229ab7ac4ebSTom Herbert .owner = THIS_MODULE, 2230ab7ac4ebSTom Herbert }; 2231ab7ac4ebSTom Herbert 2232ab7ac4ebSTom Herbert static __net_init int kcm_init_net(struct net *net) 2233ab7ac4ebSTom Herbert { 2234ab7ac4ebSTom Herbert struct kcm_net *knet = net_generic(net, kcm_net_id); 2235ab7ac4ebSTom Herbert 2236ab7ac4ebSTom Herbert INIT_LIST_HEAD_RCU(&knet->mux_list); 2237ab7ac4ebSTom Herbert mutex_init(&knet->mutex); 2238ab7ac4ebSTom Herbert 2239ab7ac4ebSTom Herbert return 0; 2240ab7ac4ebSTom Herbert } 2241ab7ac4ebSTom Herbert 2242ab7ac4ebSTom Herbert static __net_exit void kcm_exit_net(struct net *net) 2243ab7ac4ebSTom Herbert { 2244ab7ac4ebSTom Herbert struct kcm_net *knet = net_generic(net, kcm_net_id); 2245ab7ac4ebSTom Herbert 2246ab7ac4ebSTom Herbert /* All KCM sockets should be closed at this point, which should mean 2247ab7ac4ebSTom Herbert * that all multiplexors and psocks have been destroyed. 2248ab7ac4ebSTom Herbert */ 2249ab7ac4ebSTom Herbert WARN_ON(!list_empty(&knet->mux_list)); 2250ab7ac4ebSTom Herbert } 2251ab7ac4ebSTom Herbert 2252ab7ac4ebSTom Herbert static struct pernet_operations kcm_net_ops = { 2253ab7ac4ebSTom Herbert .init = kcm_init_net, 2254ab7ac4ebSTom Herbert .exit = kcm_exit_net, 2255ab7ac4ebSTom Herbert .id = &kcm_net_id, 2256ab7ac4ebSTom Herbert .size = sizeof(struct kcm_net), 2257ab7ac4ebSTom Herbert }; 2258ab7ac4ebSTom Herbert 2259ab7ac4ebSTom Herbert static int __init kcm_init(void) 2260ab7ac4ebSTom Herbert { 2261ab7ac4ebSTom Herbert int err = -ENOMEM; 2262ab7ac4ebSTom Herbert 2263ab7ac4ebSTom Herbert kcm_muxp = kmem_cache_create("kcm_mux_cache", 2264ab7ac4ebSTom Herbert sizeof(struct kcm_mux), 0, 2265ab7ac4ebSTom Herbert SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 2266ab7ac4ebSTom Herbert if (!kcm_muxp) 2267ab7ac4ebSTom Herbert goto fail; 2268ab7ac4ebSTom Herbert 2269ab7ac4ebSTom Herbert kcm_psockp = kmem_cache_create("kcm_psock_cache", 2270ab7ac4ebSTom Herbert sizeof(struct kcm_psock), 0, 2271ab7ac4ebSTom Herbert SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 2272ab7ac4ebSTom Herbert if (!kcm_psockp) 2273ab7ac4ebSTom Herbert goto fail; 2274ab7ac4ebSTom Herbert 2275ab7ac4ebSTom Herbert kcm_wq = create_singlethread_workqueue("kkcmd"); 2276ab7ac4ebSTom Herbert if (!kcm_wq) 2277ab7ac4ebSTom Herbert goto fail; 2278ab7ac4ebSTom Herbert 2279ab7ac4ebSTom Herbert err = proto_register(&kcm_proto, 1); 2280ab7ac4ebSTom Herbert if (err) 2281ab7ac4ebSTom Herbert goto fail; 2282ab7ac4ebSTom Herbert 2283ab7ac4ebSTom Herbert err = sock_register(&kcm_family_ops); 2284ab7ac4ebSTom Herbert if (err) 2285ab7ac4ebSTom Herbert goto sock_register_fail; 2286ab7ac4ebSTom Herbert 2287ab7ac4ebSTom Herbert err = register_pernet_device(&kcm_net_ops); 2288ab7ac4ebSTom Herbert if (err) 2289ab7ac4ebSTom Herbert goto net_ops_fail; 2290ab7ac4ebSTom Herbert 2291cd6e111bSTom Herbert err = kcm_proc_init(); 2292cd6e111bSTom Herbert if (err) 2293cd6e111bSTom Herbert goto proc_init_fail; 2294cd6e111bSTom Herbert 2295ab7ac4ebSTom Herbert return 0; 2296ab7ac4ebSTom Herbert 2297cd6e111bSTom Herbert proc_init_fail: 2298cd6e111bSTom Herbert unregister_pernet_device(&kcm_net_ops); 2299cd6e111bSTom Herbert 2300ab7ac4ebSTom Herbert net_ops_fail: 2301ab7ac4ebSTom Herbert sock_unregister(PF_KCM); 2302ab7ac4ebSTom Herbert 2303ab7ac4ebSTom Herbert sock_register_fail: 2304ab7ac4ebSTom Herbert proto_unregister(&kcm_proto); 2305ab7ac4ebSTom Herbert 2306ab7ac4ebSTom Herbert fail: 2307ab7ac4ebSTom Herbert kmem_cache_destroy(kcm_muxp); 2308ab7ac4ebSTom Herbert kmem_cache_destroy(kcm_psockp); 2309ab7ac4ebSTom Herbert 2310ab7ac4ebSTom Herbert if (kcm_wq) 2311ab7ac4ebSTom Herbert destroy_workqueue(kcm_wq); 2312ab7ac4ebSTom Herbert 2313ab7ac4ebSTom Herbert return err; 2314ab7ac4ebSTom Herbert } 2315ab7ac4ebSTom Herbert 2316ab7ac4ebSTom Herbert static void __exit kcm_exit(void) 2317ab7ac4ebSTom Herbert { 2318cd6e111bSTom Herbert kcm_proc_exit(); 2319ab7ac4ebSTom Herbert unregister_pernet_device(&kcm_net_ops); 2320ab7ac4ebSTom Herbert sock_unregister(PF_KCM); 2321ab7ac4ebSTom Herbert proto_unregister(&kcm_proto); 2322ab7ac4ebSTom Herbert destroy_workqueue(kcm_wq); 2323ab7ac4ebSTom Herbert 2324ab7ac4ebSTom Herbert kmem_cache_destroy(kcm_muxp); 2325ab7ac4ebSTom Herbert kmem_cache_destroy(kcm_psockp); 2326ab7ac4ebSTom Herbert } 2327ab7ac4ebSTom Herbert 2328ab7ac4ebSTom Herbert module_init(kcm_init); 2329ab7ac4ebSTom Herbert module_exit(kcm_exit); 2330ab7ac4ebSTom Herbert 2331ab7ac4ebSTom Herbert MODULE_LICENSE("GPL"); 2332ab7ac4ebSTom Herbert MODULE_ALIAS_NETPROTO(PF_KCM); 2333ab7ac4ebSTom Herbert 2334