2 #include <linux/errno.h>
3 #include <linux/errqueue.h>
4 #include <linux/file.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/poll.h>
11 #include <linux/rculist.h>
12 #include <linux/skbuff.h>
13 #include <linux/socket.h>
14 #include <linux/uaccess.h>
15 #include <linux/workqueue.h>
16 #include <linux/syscalls.h>
18 #include <net/netns/generic.h>
21 #include <uapi/linux/kcm.h>
23 unsigned int kcm_net_id;
25 static struct kmem_cache *kcm_psockp __read_mostly;
26 static struct kmem_cache *kcm_muxp __read_mostly;
27 static struct workqueue_struct *kcm_wq;
29 static inline struct kcm_sock *kcm_sk(const struct sock *sk)
31 return (struct kcm_sock *)sk;
34 static inline struct kcm_tx_msg *kcm_tx_msg(struct sk_buff *skb)
36 return (struct kcm_tx_msg *)skb->cb;
39 static inline struct kcm_rx_msg *kcm_rx_msg(struct sk_buff *skb)
41 return (struct kcm_rx_msg *)((void *)skb->cb +
42 offsetof(struct qdisc_skb_cb, data));
45 static void report_csk_error(struct sock *csk, int err)
48 csk->sk_error_report(csk);
51 /* Callback lock held */
52 static void kcm_abort_rx_psock(struct kcm_psock *psock, int err,
55 struct sock *csk = psock->sk;
57 /* Unrecoverable error in receive */
59 del_timer(&psock->rx_msg_timer);
61 if (psock->rx_stopped)
64 psock->rx_stopped = 1;
65 KCM_STATS_INCR(psock->stats.rx_aborts);
67 /* Report an error on the lower socket */
68 report_csk_error(csk, err);
71 static void kcm_abort_tx_psock(struct kcm_psock *psock, int err,
74 struct sock *csk = psock->sk;
75 struct kcm_mux *mux = psock->mux;
77 /* Unrecoverable error in transmit */
79 spin_lock_bh(&mux->lock);
81 if (psock->tx_stopped) {
82 spin_unlock_bh(&mux->lock);
86 psock->tx_stopped = 1;
87 KCM_STATS_INCR(psock->stats.tx_aborts);
90 /* Take off psocks_avail list */
91 list_del(&psock->psock_avail_list);
92 } else if (wakeup_kcm) {
93 /* In this case psock is being aborted while outside of
94 * write_msgs and psock is reserved. Schedule tx_work
95 * to handle the failure there. Need to commit tx_stopped
96 * before queuing work.
100 queue_work(kcm_wq, &psock->tx_kcm->tx_work);
103 spin_unlock_bh(&mux->lock);
105 /* Report error on lower socket */
106 report_csk_error(csk, err);
109 /* RX mux lock held. */
110 static void kcm_update_rx_mux_stats(struct kcm_mux *mux,
111 struct kcm_psock *psock)
113 KCM_STATS_ADD(mux->stats.rx_bytes,
114 psock->stats.rx_bytes - psock->saved_rx_bytes);
115 mux->stats.rx_msgs +=
116 psock->stats.rx_msgs - psock->saved_rx_msgs;
117 psock->saved_rx_msgs = psock->stats.rx_msgs;
118 psock->saved_rx_bytes = psock->stats.rx_bytes;
121 static void kcm_update_tx_mux_stats(struct kcm_mux *mux,
122 struct kcm_psock *psock)
124 KCM_STATS_ADD(mux->stats.tx_bytes,
125 psock->stats.tx_bytes - psock->saved_tx_bytes);
126 mux->stats.tx_msgs +=
127 psock->stats.tx_msgs - psock->saved_tx_msgs;
128 psock->saved_tx_msgs = psock->stats.tx_msgs;
129 psock->saved_tx_bytes = psock->stats.tx_bytes;
132 static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
134 /* KCM is ready to receive messages on its queue-- either the KCM is new or
135 * has become unblocked after being blocked on full socket buffer. Queue any
136 * pending ready messages on a psock. RX mux lock held.
138 static void kcm_rcv_ready(struct kcm_sock *kcm)
140 struct kcm_mux *mux = kcm->mux;
141 struct kcm_psock *psock;
144 if (unlikely(kcm->rx_wait || kcm->rx_psock || kcm->rx_disabled))
147 while (unlikely((skb = __skb_dequeue(&mux->rx_hold_queue)))) {
148 if (kcm_queue_rcv_skb(&kcm->sk, skb)) {
149 /* Assuming buffer limit has been reached */
150 skb_queue_head(&mux->rx_hold_queue, skb);
151 WARN_ON(!sk_rmem_alloc_get(&kcm->sk));
156 while (!list_empty(&mux->psocks_ready)) {
157 psock = list_first_entry(&mux->psocks_ready, struct kcm_psock,
160 if (kcm_queue_rcv_skb(&kcm->sk, psock->ready_rx_msg)) {
161 /* Assuming buffer limit has been reached */
162 WARN_ON(!sk_rmem_alloc_get(&kcm->sk));
166 /* Consumed the ready message on the psock. Schedule rx_work to
169 list_del(&psock->psock_ready_list);
170 psock->ready_rx_msg = NULL;
172 /* Commit clearing of ready_rx_msg for queuing work */
175 queue_work(kcm_wq, &psock->rx_work);
178 /* Buffer limit is okay now, add to ready list */
179 list_add_tail(&kcm->wait_rx_list,
180 &kcm->mux->kcm_rx_waiters);
184 static void kcm_rfree(struct sk_buff *skb)
186 struct sock *sk = skb->sk;
187 struct kcm_sock *kcm = kcm_sk(sk);
188 struct kcm_mux *mux = kcm->mux;
189 unsigned int len = skb->truesize;
191 sk_mem_uncharge(sk, len);
192 atomic_sub(len, &sk->sk_rmem_alloc);
194 /* For reading rx_wait and rx_psock without holding lock */
195 smp_mb__after_atomic();
197 if (!kcm->rx_wait && !kcm->rx_psock &&
198 sk_rmem_alloc_get(sk) < sk->sk_rcvlowat) {
199 spin_lock_bh(&mux->rx_lock);
201 spin_unlock_bh(&mux->rx_lock);
205 static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
207 struct sk_buff_head *list = &sk->sk_receive_queue;
209 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
212 if (!sk_rmem_schedule(sk, skb, skb->truesize))
219 skb->destructor = kcm_rfree;
220 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
221 sk_mem_charge(sk, skb->truesize);
223 skb_queue_tail(list, skb);
225 if (!sock_flag(sk, SOCK_DEAD))
226 sk->sk_data_ready(sk);
231 /* Requeue received messages for a kcm socket to other kcm sockets. This is
232 * called with a kcm socket is receive disabled.
235 static void requeue_rx_msgs(struct kcm_mux *mux, struct sk_buff_head *head)
238 struct kcm_sock *kcm;
240 while ((skb = __skb_dequeue(head))) {
241 /* Reset destructor to avoid calling kcm_rcv_ready */
242 skb->destructor = sock_rfree;
245 if (list_empty(&mux->kcm_rx_waiters)) {
246 skb_queue_tail(&mux->rx_hold_queue, skb);
250 kcm = list_first_entry(&mux->kcm_rx_waiters,
251 struct kcm_sock, wait_rx_list);
253 if (kcm_queue_rcv_skb(&kcm->sk, skb)) {
254 /* Should mean socket buffer full */
255 list_del(&kcm->wait_rx_list);
256 kcm->rx_wait = false;
258 /* Commit rx_wait to read in kcm_free */
266 /* Lower sock lock held */
267 static struct kcm_sock *reserve_rx_kcm(struct kcm_psock *psock,
268 struct sk_buff *head)
270 struct kcm_mux *mux = psock->mux;
271 struct kcm_sock *kcm;
273 WARN_ON(psock->ready_rx_msg);
276 return psock->rx_kcm;
278 spin_lock_bh(&mux->rx_lock);
281 spin_unlock_bh(&mux->rx_lock);
282 return psock->rx_kcm;
285 kcm_update_rx_mux_stats(mux, psock);
287 if (list_empty(&mux->kcm_rx_waiters)) {
288 psock->ready_rx_msg = head;
289 list_add_tail(&psock->psock_ready_list,
291 spin_unlock_bh(&mux->rx_lock);
295 kcm = list_first_entry(&mux->kcm_rx_waiters,
296 struct kcm_sock, wait_rx_list);
297 list_del(&kcm->wait_rx_list);
298 kcm->rx_wait = false;
301 kcm->rx_psock = psock;
303 spin_unlock_bh(&mux->rx_lock);
308 static void kcm_done(struct kcm_sock *kcm);
310 static void kcm_done_work(struct work_struct *w)
312 kcm_done(container_of(w, struct kcm_sock, done_work));
315 /* Lower sock held */
316 static void unreserve_rx_kcm(struct kcm_psock *psock,
319 struct kcm_sock *kcm = psock->rx_kcm;
320 struct kcm_mux *mux = psock->mux;
325 spin_lock_bh(&mux->rx_lock);
327 psock->rx_kcm = NULL;
328 kcm->rx_psock = NULL;
330 /* Commit kcm->rx_psock before sk_rmem_alloc_get to sync with
335 if (unlikely(kcm->done)) {
336 spin_unlock_bh(&mux->rx_lock);
338 /* Need to run kcm_done in a task since we need to qcquire
339 * callback locks which may already be held here.
341 INIT_WORK(&kcm->done_work, kcm_done_work);
342 schedule_work(&kcm->done_work);
346 if (unlikely(kcm->rx_disabled)) {
347 requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue);
348 } else if (rcv_ready || unlikely(!sk_rmem_alloc_get(&kcm->sk))) {
349 /* Check for degenerative race with rx_wait that all
350 * data was dequeued (accounted for in kcm_rfree).
354 spin_unlock_bh(&mux->rx_lock);
357 static void kcm_start_rx_timer(struct kcm_psock *psock)
359 if (psock->sk->sk_rcvtimeo)
360 mod_timer(&psock->rx_msg_timer, psock->sk->sk_rcvtimeo);
363 /* Macro to invoke filter function. */
364 #define KCM_RUN_FILTER(prog, ctx) \
365 (*prog->bpf_func)(ctx, prog->insnsi)
367 /* Lower socket lock held */
368 static int kcm_tcp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
369 unsigned int orig_offset, size_t orig_len)
371 struct kcm_psock *psock = (struct kcm_psock *)desc->arg.data;
372 struct kcm_rx_msg *rxm;
373 struct kcm_sock *kcm;
374 struct sk_buff *head, *skb;
375 size_t eaten = 0, cand_len;
378 bool cloned_orig = false;
380 if (psock->ready_rx_msg)
383 head = psock->rx_skb_head;
385 /* Message already in progress */
387 rxm = kcm_rx_msg(head);
388 if (unlikely(rxm->early_eaten)) {
389 /* Already some number of bytes on the receive sock
390 * data saved in rx_skb_head, just indicate they
393 eaten = orig_len <= rxm->early_eaten ?
394 orig_len : rxm->early_eaten;
395 rxm->early_eaten -= eaten;
400 if (unlikely(orig_offset)) {
401 /* Getting data with a non-zero offset when a message is
402 * in progress is not expected. If it does happen, we
403 * need to clone and pull since we can't deal with
404 * offsets in the skbs for a message expect in the head.
406 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
408 KCM_STATS_INCR(psock->stats.rx_mem_fail);
409 desc->error = -ENOMEM;
412 if (!pskb_pull(orig_skb, orig_offset)) {
413 KCM_STATS_INCR(psock->stats.rx_mem_fail);
415 desc->error = -ENOMEM;
422 if (!psock->rx_skb_nextp) {
423 /* We are going to append to the frags_list of head.
424 * Need to unshare the frag_list.
426 err = skb_unclone(head, GFP_ATOMIC);
428 KCM_STATS_INCR(psock->stats.rx_mem_fail);
433 if (unlikely(skb_shinfo(head)->frag_list)) {
434 /* We can't append to an sk_buff that already
435 * has a frag_list. We create a new head, point
436 * the frag_list of that to the old head, and
437 * then are able to use the old head->next for
438 * appending to the message.
440 if (WARN_ON(head->next)) {
441 desc->error = -EINVAL;
445 skb = alloc_skb(0, GFP_ATOMIC);
447 KCM_STATS_INCR(psock->stats.rx_mem_fail);
448 desc->error = -ENOMEM;
451 skb->len = head->len;
452 skb->data_len = head->len;
453 skb->truesize = head->truesize;
454 *kcm_rx_msg(skb) = *kcm_rx_msg(head);
455 psock->rx_skb_nextp = &head->next;
456 skb_shinfo(skb)->frag_list = head;
457 psock->rx_skb_head = skb;
460 psock->rx_skb_nextp =
461 &skb_shinfo(head)->frag_list;
466 while (eaten < orig_len) {
467 /* Always clone since we will consume something */
468 skb = skb_clone(orig_skb, GFP_ATOMIC);
470 KCM_STATS_INCR(psock->stats.rx_mem_fail);
471 desc->error = -ENOMEM;
475 cand_len = orig_len - eaten;
477 head = psock->rx_skb_head;
480 psock->rx_skb_head = head;
481 /* Will set rx_skb_nextp on next packet if needed */
482 psock->rx_skb_nextp = NULL;
483 rxm = kcm_rx_msg(head);
484 memset(rxm, 0, sizeof(*rxm));
485 rxm->offset = orig_offset + eaten;
487 /* Unclone since we may be appending to an skb that we
488 * already share a frag_list with.
490 err = skb_unclone(skb, GFP_ATOMIC);
492 KCM_STATS_INCR(psock->stats.rx_mem_fail);
497 rxm = kcm_rx_msg(head);
498 *psock->rx_skb_nextp = skb;
499 psock->rx_skb_nextp = &skb->next;
500 head->data_len += skb->len;
501 head->len += skb->len;
502 head->truesize += skb->truesize;
505 if (!rxm->full_len) {
508 len = KCM_RUN_FILTER(psock->bpf_prog, head);
511 /* Need more header to determine length */
512 if (!rxm->accum_len) {
513 /* Start RX timer for new message */
514 kcm_start_rx_timer(psock);
516 rxm->accum_len += cand_len;
518 KCM_STATS_INCR(psock->stats.rx_need_more_hdr);
519 WARN_ON(eaten != orig_len);
521 } else if (len > psock->sk->sk_rcvbuf) {
522 /* Message length exceeds maximum allowed */
523 KCM_STATS_INCR(psock->stats.rx_msg_too_big);
524 desc->error = -EMSGSIZE;
525 psock->rx_skb_head = NULL;
526 kcm_abort_rx_psock(psock, EMSGSIZE, head);
528 } else if (len <= (ssize_t)head->len -
529 skb->len - rxm->offset) {
530 /* Length must be into new skb (and also
533 KCM_STATS_INCR(psock->stats.rx_bad_hdr_len);
534 desc->error = -EPROTO;
535 psock->rx_skb_head = NULL;
536 kcm_abort_rx_psock(psock, EPROTO, head);
543 extra = (ssize_t)(rxm->accum_len + cand_len) - rxm->full_len;
546 /* Message not complete yet. */
547 if (rxm->full_len - rxm->accum_len >
548 tcp_inq(psock->sk)) {
549 /* Don't have the whole messages in the socket
550 * buffer. Set psock->rx_need_bytes to wait for
551 * the rest of the message. Also, set "early
552 * eaten" since we've already buffered the skb
553 * but don't consume yet per tcp_read_sock.
556 if (!rxm->accum_len) {
557 /* Start RX timer for new message */
558 kcm_start_rx_timer(psock);
561 psock->rx_need_bytes = rxm->full_len -
563 rxm->accum_len += cand_len;
564 rxm->early_eaten = cand_len;
565 KCM_STATS_ADD(psock->stats.rx_bytes, cand_len);
566 desc->count = 0; /* Stop reading socket */
569 rxm->accum_len += cand_len;
571 WARN_ON(eaten != orig_len);
575 /* Positive extra indicates ore bytes than needed for the
579 WARN_ON(extra > cand_len);
581 eaten += (cand_len - extra);
583 /* Hurray, we have a new message! */
584 del_timer(&psock->rx_msg_timer);
585 psock->rx_skb_head = NULL;
586 KCM_STATS_INCR(psock->stats.rx_msgs);
589 kcm = reserve_rx_kcm(psock, head);
591 /* Unable to reserve a KCM, message is held in psock. */
595 if (kcm_queue_rcv_skb(&kcm->sk, head)) {
596 /* Should mean socket buffer full */
597 unreserve_rx_kcm(psock, false);
605 KCM_STATS_ADD(psock->stats.rx_bytes, eaten);
610 /* Called with lock held on lower socket */
611 static int psock_tcp_read_sock(struct kcm_psock *psock)
613 read_descriptor_t desc;
615 desc.arg.data = psock;
617 desc.count = 1; /* give more than one skb per call */
619 /* sk should be locked here, so okay to do tcp_read_sock */
620 tcp_read_sock(psock->sk, &desc, kcm_tcp_recv);
622 unreserve_rx_kcm(psock, true);
627 /* Lower sock lock held */
628 static void psock_tcp_data_ready(struct sock *sk)
630 struct kcm_psock *psock;
632 read_lock_bh(&sk->sk_callback_lock);
634 psock = (struct kcm_psock *)sk->sk_user_data;
635 if (unlikely(!psock || psock->rx_stopped))
638 if (psock->ready_rx_msg)
641 if (psock->rx_need_bytes) {
642 if (tcp_inq(sk) >= psock->rx_need_bytes)
643 psock->rx_need_bytes = 0;
648 if (psock_tcp_read_sock(psock) == -ENOMEM)
649 queue_delayed_work(kcm_wq, &psock->rx_delayed_work, 0);
652 read_unlock_bh(&sk->sk_callback_lock);
655 static void do_psock_rx_work(struct kcm_psock *psock)
657 read_descriptor_t rd_desc;
658 struct sock *csk = psock->sk;
660 /* We need the read lock to synchronize with psock_tcp_data_ready. We
661 * need the socket lock for calling tcp_read_sock.
664 read_lock_bh(&csk->sk_callback_lock);
666 if (unlikely(csk->sk_user_data != psock))
669 if (unlikely(psock->rx_stopped))
672 if (psock->ready_rx_msg)
675 rd_desc.arg.data = psock;
677 if (psock_tcp_read_sock(psock) == -ENOMEM)
678 queue_delayed_work(kcm_wq, &psock->rx_delayed_work, 0);
681 read_unlock_bh(&csk->sk_callback_lock);
685 static void psock_rx_work(struct work_struct *w)
687 do_psock_rx_work(container_of(w, struct kcm_psock, rx_work));
690 static void psock_rx_delayed_work(struct work_struct *w)
692 do_psock_rx_work(container_of(w, struct kcm_psock,
693 rx_delayed_work.work));
696 static void psock_tcp_state_change(struct sock *sk)
698 /* TCP only does a POLLIN for a half close. Do a POLLHUP here
699 * since application will normally not poll with POLLIN
700 * on the TCP sockets.
703 report_csk_error(sk, EPIPE);
706 static void psock_tcp_write_space(struct sock *sk)
708 struct kcm_psock *psock;
710 struct kcm_sock *kcm;
712 read_lock_bh(&sk->sk_callback_lock);
714 psock = (struct kcm_psock *)sk->sk_user_data;
715 if (unlikely(!psock))
720 spin_lock_bh(&mux->lock);
722 /* Check if the socket is reserved so someone is waiting for sending. */
725 queue_work(kcm_wq, &kcm->tx_work);
727 spin_unlock_bh(&mux->lock);
729 read_unlock_bh(&sk->sk_callback_lock);
732 static void unreserve_psock(struct kcm_sock *kcm);
734 /* kcm sock is locked. */
735 static struct kcm_psock *reserve_psock(struct kcm_sock *kcm)
737 struct kcm_mux *mux = kcm->mux;
738 struct kcm_psock *psock;
740 psock = kcm->tx_psock;
742 smp_rmb(); /* Must read tx_psock before tx_wait */
745 WARN_ON(kcm->tx_wait);
746 if (unlikely(psock->tx_stopped))
747 unreserve_psock(kcm);
749 return kcm->tx_psock;
752 spin_lock_bh(&mux->lock);
754 /* Check again under lock to see if psock was reserved for this
755 * psock via psock_unreserve.
757 psock = kcm->tx_psock;
758 if (unlikely(psock)) {
759 WARN_ON(kcm->tx_wait);
760 spin_unlock_bh(&mux->lock);
761 return kcm->tx_psock;
764 if (!list_empty(&mux->psocks_avail)) {
765 psock = list_first_entry(&mux->psocks_avail,
768 list_del(&psock->psock_avail_list);
770 list_del(&kcm->wait_psock_list);
771 kcm->tx_wait = false;
773 kcm->tx_psock = psock;
775 KCM_STATS_INCR(psock->stats.reserved);
776 } else if (!kcm->tx_wait) {
777 list_add_tail(&kcm->wait_psock_list,
778 &mux->kcm_tx_waiters);
782 spin_unlock_bh(&mux->lock);
788 static void psock_now_avail(struct kcm_psock *psock)
790 struct kcm_mux *mux = psock->mux;
791 struct kcm_sock *kcm;
793 if (list_empty(&mux->kcm_tx_waiters)) {
794 list_add_tail(&psock->psock_avail_list,
797 kcm = list_first_entry(&mux->kcm_tx_waiters,
800 list_del(&kcm->wait_psock_list);
801 kcm->tx_wait = false;
804 /* Commit before changing tx_psock since that is read in
805 * reserve_psock before queuing work.
809 kcm->tx_psock = psock;
810 KCM_STATS_INCR(psock->stats.reserved);
811 queue_work(kcm_wq, &kcm->tx_work);
815 /* kcm sock is locked. */
816 static void unreserve_psock(struct kcm_sock *kcm)
818 struct kcm_psock *psock;
819 struct kcm_mux *mux = kcm->mux;
821 spin_lock_bh(&mux->lock);
823 psock = kcm->tx_psock;
825 if (WARN_ON(!psock)) {
826 spin_unlock_bh(&mux->lock);
830 smp_rmb(); /* Read tx_psock before tx_wait */
832 kcm_update_tx_mux_stats(mux, psock);
834 WARN_ON(kcm->tx_wait);
836 kcm->tx_psock = NULL;
837 psock->tx_kcm = NULL;
838 KCM_STATS_INCR(psock->stats.unreserved);
840 if (unlikely(psock->tx_stopped)) {
843 list_del(&psock->psock_list);
846 fput(psock->sk->sk_socket->file);
847 kmem_cache_free(kcm_psockp, psock);
850 /* Don't put back on available list */
852 spin_unlock_bh(&mux->lock);
857 psock_now_avail(psock);
859 spin_unlock_bh(&mux->lock);
862 static void kcm_report_tx_retry(struct kcm_sock *kcm)
864 struct kcm_mux *mux = kcm->mux;
866 spin_lock_bh(&mux->lock);
867 KCM_STATS_INCR(mux->stats.tx_retries);
868 spin_unlock_bh(&mux->lock);
871 /* Write any messages ready on the kcm socket. Called with kcm sock lock
872 * held. Return bytes actually sent or error.
874 static int kcm_write_msgs(struct kcm_sock *kcm)
876 struct sock *sk = &kcm->sk;
877 struct kcm_psock *psock;
878 struct sk_buff *skb, *head;
879 struct kcm_tx_msg *txm;
880 unsigned short fragidx, frag_offset;
881 unsigned int sent, total_sent = 0;
884 kcm->tx_wait_more = false;
885 psock = kcm->tx_psock;
886 if (unlikely(psock && psock->tx_stopped)) {
887 /* A reserved psock was aborted asynchronously. Unreserve
888 * it and we'll retry the message.
890 unreserve_psock(kcm);
891 kcm_report_tx_retry(kcm);
892 if (skb_queue_empty(&sk->sk_write_queue))
895 kcm_tx_msg(skb_peek(&sk->sk_write_queue))->sent = 0;
897 } else if (skb_queue_empty(&sk->sk_write_queue)) {
901 head = skb_peek(&sk->sk_write_queue);
902 txm = kcm_tx_msg(head);
905 /* Send of first skbuff in queue already in progress */
906 if (WARN_ON(!psock)) {
911 frag_offset = txm->frag_offset;
912 fragidx = txm->fragidx;
919 psock = reserve_psock(kcm);
925 txm = kcm_tx_msg(head);
929 if (WARN_ON(!skb_shinfo(skb)->nr_frags)) {
934 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags;
940 frag = &skb_shinfo(skb)->frags[fragidx];
941 if (WARN_ON(!frag->size)) {
946 ret = kernel_sendpage(psock->sk->sk_socket,
948 frag->page_offset + frag_offset,
949 frag->size - frag_offset,
952 if (ret == -EAGAIN) {
953 /* Save state to try again when there's
954 * write space on the socket
957 txm->frag_offset = frag_offset;
958 txm->fragidx = fragidx;
965 /* Hard failure in sending message, abort this
966 * psock since it has lost framing
967 * synchonization and retry sending the
968 * message from the beginning.
970 kcm_abort_tx_psock(psock, ret ? -ret : EPIPE,
972 unreserve_psock(kcm);
975 kcm_report_tx_retry(kcm);
983 KCM_STATS_ADD(psock->stats.tx_bytes, ret);
984 if (frag_offset < frag->size) {
985 /* Not finished with this frag */
991 if (skb_has_frag_list(skb)) {
992 skb = skb_shinfo(skb)->frag_list;
995 } else if (skb->next) {
1000 /* Successfully sent the whole packet, account for it. */
1001 skb_dequeue(&sk->sk_write_queue);
1003 sk->sk_wmem_queued -= sent;
1005 KCM_STATS_INCR(psock->stats.tx_msgs);
1006 } while ((head = skb_peek(&sk->sk_write_queue)));
1009 /* Done with all queued messages. */
1010 WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
1011 unreserve_psock(kcm);
1014 /* Check if write space is available */
1015 sk->sk_write_space(sk);
1017 return total_sent ? : ret;
1020 static void kcm_tx_work(struct work_struct *w)
1022 struct kcm_sock *kcm = container_of(w, struct kcm_sock, tx_work);
1023 struct sock *sk = &kcm->sk;
1028 /* Primarily for SOCK_DGRAM sockets, also handle asynchronous tx
1031 err = kcm_write_msgs(kcm);
1033 /* Hard failure in write, report error on KCM socket */
1034 pr_warn("KCM: Hard failure on kcm_write_msgs %d\n", err);
1035 report_csk_error(&kcm->sk, -err);
1039 /* Primarily for SOCK_SEQPACKET sockets */
1040 if (likely(sk->sk_socket) &&
1041 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1042 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1043 sk->sk_write_space(sk);
1050 static void kcm_push(struct kcm_sock *kcm)
1052 if (kcm->tx_wait_more)
1053 kcm_write_msgs(kcm);
1056 static ssize_t kcm_sendpage(struct socket *sock, struct page *page,
1057 int offset, size_t size, int flags)
1060 struct sock *sk = sock->sk;
1061 struct kcm_sock *kcm = kcm_sk(sk);
1062 struct sk_buff *skb = NULL, *head = NULL;
1063 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1068 if (flags & MSG_SENDPAGE_NOTLAST)
1071 /* No MSG_EOR from splice, only look at MSG_MORE */
1072 eor = !(flags & MSG_MORE);
1076 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1083 /* Previously opened message */
1084 head = kcm->seq_skb;
1085 skb = kcm_tx_msg(head)->last_skb;
1086 i = skb_shinfo(skb)->nr_frags;
1088 if (skb_can_coalesce(skb, i, page, offset)) {
1089 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
1090 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1094 if (i >= MAX_SKB_FRAGS) {
1095 struct sk_buff *tskb;
1097 tskb = alloc_skb(0, sk->sk_allocation);
1100 err = sk_stream_wait_memory(sk, &timeo);
1106 skb_shinfo(head)->frag_list = tskb;
1111 skb->ip_summed = CHECKSUM_UNNECESSARY;
1115 /* Call the sk_stream functions to manage the sndbuf mem. */
1116 if (!sk_stream_memory_free(sk)) {
1118 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1119 err = sk_stream_wait_memory(sk, &timeo);
1124 head = alloc_skb(0, sk->sk_allocation);
1127 err = sk_stream_wait_memory(sk, &timeo);
1137 skb_fill_page_desc(skb, i, page, offset, size);
1138 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1142 skb->data_len += size;
1143 skb->truesize += size;
1144 sk->sk_wmem_queued += size;
1145 sk_mem_charge(sk, size);
1149 head->data_len += size;
1150 head->truesize += size;
1154 bool not_busy = skb_queue_empty(&sk->sk_write_queue);
1156 /* Message complete, queue it on send buffer */
1157 __skb_queue_tail(&sk->sk_write_queue, head);
1158 kcm->seq_skb = NULL;
1159 KCM_STATS_INCR(kcm->stats.tx_msgs);
1161 if (flags & MSG_BATCH) {
1162 kcm->tx_wait_more = true;
1163 } else if (kcm->tx_wait_more || not_busy) {
1164 err = kcm_write_msgs(kcm);
1166 /* We got a hard error in write_msgs but have
1167 * already queued this message. Report an error
1168 * in the socket, but don't affect return value
1171 pr_warn("KCM: Hard failure on kcm_write_msgs\n");
1172 report_csk_error(&kcm->sk, -err);
1176 /* Message not complete, save state */
1177 kcm->seq_skb = head;
1178 kcm_tx_msg(head)->last_skb = skb;
1181 KCM_STATS_ADD(kcm->stats.tx_bytes, size);
1189 err = sk_stream_error(sk, flags, err);
1191 /* make sure we wake any epoll edge trigger waiter */
1192 if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN))
1193 sk->sk_write_space(sk);
1199 static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1201 struct sock *sk = sock->sk;
1202 struct kcm_sock *kcm = kcm_sk(sk);
1203 struct sk_buff *skb = NULL, *head = NULL;
1204 size_t copy, copied = 0;
1205 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1206 int eor = (sock->type == SOCK_DGRAM) ?
1207 !(msg->msg_flags & MSG_MORE) : !!(msg->msg_flags & MSG_EOR);
1212 /* Per tcp_sendmsg this should be in poll */
1213 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1219 /* Previously opened message */
1220 head = kcm->seq_skb;
1221 skb = kcm_tx_msg(head)->last_skb;
1225 /* Call the sk_stream functions to manage the sndbuf mem. */
1226 if (!sk_stream_memory_free(sk)) {
1228 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1229 err = sk_stream_wait_memory(sk, &timeo);
1234 /* New message, alloc head skb */
1235 head = alloc_skb(0, sk->sk_allocation);
1238 err = sk_stream_wait_memory(sk, &timeo);
1242 head = alloc_skb(0, sk->sk_allocation);
1247 /* Set ip_summed to CHECKSUM_UNNECESSARY to avoid calling
1248 * csum_and_copy_from_iter from skb_do_copy_data_nocache.
1250 skb->ip_summed = CHECKSUM_UNNECESSARY;
1253 while (msg_data_left(msg)) {
1255 int i = skb_shinfo(skb)->nr_frags;
1256 struct page_frag *pfrag = sk_page_frag(sk);
1258 if (!sk_page_frag_refill(sk, pfrag))
1259 goto wait_for_memory;
1261 if (!skb_can_coalesce(skb, i, pfrag->page,
1263 if (i == MAX_SKB_FRAGS) {
1264 struct sk_buff *tskb;
1266 tskb = alloc_skb(0, sk->sk_allocation);
1268 goto wait_for_memory;
1271 skb_shinfo(head)->frag_list = tskb;
1276 skb->ip_summed = CHECKSUM_UNNECESSARY;
1282 copy = min_t(int, msg_data_left(msg),
1283 pfrag->size - pfrag->offset);
1285 if (!sk_wmem_schedule(sk, copy))
1286 goto wait_for_memory;
1288 err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb,
1295 /* Update the skb. */
1297 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1299 skb_fill_page_desc(skb, i, pfrag->page,
1300 pfrag->offset, copy);
1301 get_page(pfrag->page);
1304 pfrag->offset += copy;
1308 head->data_len += copy;
1315 err = sk_stream_wait_memory(sk, &timeo);
1321 bool not_busy = skb_queue_empty(&sk->sk_write_queue);
1323 /* Message complete, queue it on send buffer */
1324 __skb_queue_tail(&sk->sk_write_queue, head);
1325 kcm->seq_skb = NULL;
1326 KCM_STATS_INCR(kcm->stats.tx_msgs);
1328 if (msg->msg_flags & MSG_BATCH) {
1329 kcm->tx_wait_more = true;
1330 } else if (kcm->tx_wait_more || not_busy) {
1331 err = kcm_write_msgs(kcm);
1333 /* We got a hard error in write_msgs but have
1334 * already queued this message. Report an error
1335 * in the socket, but don't affect return value
1338 pr_warn("KCM: Hard failure on kcm_write_msgs\n");
1339 report_csk_error(&kcm->sk, -err);
1343 /* Message not complete, save state */
1345 kcm->seq_skb = head;
1346 kcm_tx_msg(head)->last_skb = skb;
1349 KCM_STATS_ADD(kcm->stats.tx_bytes, copied);
1357 if (copied && sock->type == SOCK_SEQPACKET) {
1358 /* Wrote some bytes before encountering an
1359 * error, return partial success.
1361 goto partial_message;
1364 if (head != kcm->seq_skb)
1367 err = sk_stream_error(sk, msg->msg_flags, err);
1369 /* make sure we wake any epoll edge trigger waiter */
1370 if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN))
1371 sk->sk_write_space(sk);
1377 static struct sk_buff *kcm_wait_data(struct sock *sk, int flags,
1378 long timeo, int *err)
1380 struct sk_buff *skb;
1382 while (!(skb = skb_peek(&sk->sk_receive_queue))) {
1384 *err = sock_error(sk);
1388 if (sock_flag(sk, SOCK_DONE))
1391 if ((flags & MSG_DONTWAIT) || !timeo) {
1396 sk_wait_data(sk, &timeo, NULL);
1398 /* Handle signals */
1399 if (signal_pending(current)) {
1400 *err = sock_intr_errno(timeo);
1408 static int kcm_recvmsg(struct socket *sock, struct msghdr *msg,
1409 size_t len, int flags)
1411 struct sock *sk = sock->sk;
1412 struct kcm_sock *kcm = kcm_sk(sk);
1415 struct kcm_rx_msg *rxm;
1417 struct sk_buff *skb;
1419 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1423 skb = kcm_wait_data(sk, flags, timeo, &err);
1427 /* Okay, have a message on the receive queue */
1429 rxm = kcm_rx_msg(skb);
1431 if (len > rxm->full_len)
1432 len = rxm->full_len;
1434 err = skb_copy_datagram_msg(skb, rxm->offset, msg, len);
1439 if (likely(!(flags & MSG_PEEK))) {
1440 KCM_STATS_ADD(kcm->stats.rx_bytes, copied);
1441 if (copied < rxm->full_len) {
1442 if (sock->type == SOCK_DGRAM) {
1443 /* Truncated message */
1444 msg->msg_flags |= MSG_TRUNC;
1447 rxm->offset += copied;
1448 rxm->full_len -= copied;
1451 /* Finished with message */
1452 msg->msg_flags |= MSG_EOR;
1453 KCM_STATS_INCR(kcm->stats.rx_msgs);
1454 skb_unlink(skb, &sk->sk_receive_queue);
1462 return copied ? : err;
1465 static ssize_t kcm_splice_read(struct socket *sock, loff_t *ppos,
1466 struct pipe_inode_info *pipe, size_t len,
1469 struct sock *sk = sock->sk;
1470 struct kcm_sock *kcm = kcm_sk(sk);
1472 struct kcm_rx_msg *rxm;
1475 struct sk_buff *skb;
1477 /* Only support splice for SOCKSEQPACKET */
1479 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1483 skb = kcm_wait_data(sk, flags, timeo, &err);
1487 /* Okay, have a message on the receive queue */
1489 rxm = kcm_rx_msg(skb);
1491 if (len > rxm->full_len)
1492 len = rxm->full_len;
1494 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, len, flags);
1500 KCM_STATS_ADD(kcm->stats.rx_bytes, copied);
1502 rxm->offset += copied;
1503 rxm->full_len -= copied;
1505 /* We have no way to return MSG_EOR. If all the bytes have been
1506 * read we still leave the message in the receive socket buffer.
1507 * A subsequent recvmsg needs to be done to return MSG_EOR and
1508 * finish reading the message.
1521 /* kcm sock lock held */
1522 static void kcm_recv_disable(struct kcm_sock *kcm)
1524 struct kcm_mux *mux = kcm->mux;
1526 if (kcm->rx_disabled)
1529 spin_lock_bh(&mux->rx_lock);
1531 kcm->rx_disabled = 1;
1533 /* If a psock is reserved we'll do cleanup in unreserve */
1534 if (!kcm->rx_psock) {
1536 list_del(&kcm->wait_rx_list);
1537 kcm->rx_wait = false;
1540 requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue);
1543 spin_unlock_bh(&mux->rx_lock);
1546 /* kcm sock lock held */
1547 static void kcm_recv_enable(struct kcm_sock *kcm)
1549 struct kcm_mux *mux = kcm->mux;
1551 if (!kcm->rx_disabled)
1554 spin_lock_bh(&mux->rx_lock);
1556 kcm->rx_disabled = 0;
1559 spin_unlock_bh(&mux->rx_lock);
1562 static int kcm_setsockopt(struct socket *sock, int level, int optname,
1563 char __user *optval, unsigned int optlen)
1565 struct kcm_sock *kcm = kcm_sk(sock->sk);
1569 if (level != SOL_KCM)
1570 return -ENOPROTOOPT;
1572 if (optlen < sizeof(int))
1575 if (get_user(val, (int __user *)optval))
1578 valbool = val ? 1 : 0;
1581 case KCM_RECV_DISABLE:
1582 lock_sock(&kcm->sk);
1584 kcm_recv_disable(kcm);
1586 kcm_recv_enable(kcm);
1587 release_sock(&kcm->sk);
1596 static int kcm_getsockopt(struct socket *sock, int level, int optname,
1597 char __user *optval, int __user *optlen)
1599 struct kcm_sock *kcm = kcm_sk(sock->sk);
1602 if (level != SOL_KCM)
1603 return -ENOPROTOOPT;
1605 if (get_user(len, optlen))
1608 len = min_t(unsigned int, len, sizeof(int));
1613 case KCM_RECV_DISABLE:
1614 val = kcm->rx_disabled;
1617 return -ENOPROTOOPT;
1620 if (put_user(len, optlen))
1622 if (copy_to_user(optval, &val, len))
1627 static void init_kcm_sock(struct kcm_sock *kcm, struct kcm_mux *mux)
1629 struct kcm_sock *tkcm;
1630 struct list_head *head;
1633 /* For SOCK_SEQPACKET sock type, datagram_poll checks the sk_state, so
1634 * we set sk_state, otherwise epoll_wait always returns right away with
1637 kcm->sk.sk_state = TCP_ESTABLISHED;
1639 /* Add to mux's kcm sockets list */
1641 spin_lock_bh(&mux->lock);
1643 head = &mux->kcm_socks;
1644 list_for_each_entry(tkcm, &mux->kcm_socks, kcm_sock_list) {
1645 if (tkcm->index != index)
1647 head = &tkcm->kcm_sock_list;
1651 list_add(&kcm->kcm_sock_list, head);
1654 mux->kcm_socks_cnt++;
1655 spin_unlock_bh(&mux->lock);
1657 INIT_WORK(&kcm->tx_work, kcm_tx_work);
1659 spin_lock_bh(&mux->rx_lock);
1661 spin_unlock_bh(&mux->rx_lock);
1664 static void kcm_rx_msg_timeout(unsigned long arg)
1666 struct kcm_psock *psock = (struct kcm_psock *)arg;
1668 /* Message assembly timed out */
1669 KCM_STATS_INCR(psock->stats.rx_msg_timeouts);
1670 kcm_abort_rx_psock(psock, ETIMEDOUT, NULL);
1673 static int kcm_attach(struct socket *sock, struct socket *csock,
1674 struct bpf_prog *prog)
1676 struct kcm_sock *kcm = kcm_sk(sock->sk);
1677 struct kcm_mux *mux = kcm->mux;
1679 struct kcm_psock *psock = NULL, *tpsock;
1680 struct list_head *head;
1683 if (csock->ops->family != PF_INET &&
1684 csock->ops->family != PF_INET6)
1691 /* Only support TCP for now */
1692 if (csk->sk_protocol != IPPROTO_TCP)
1695 psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL);
1701 psock->bpf_prog = prog;
1703 setup_timer(&psock->rx_msg_timer, kcm_rx_msg_timeout,
1704 (unsigned long)psock);
1706 INIT_WORK(&psock->rx_work, psock_rx_work);
1707 INIT_DELAYED_WORK(&psock->rx_delayed_work, psock_rx_delayed_work);
1711 write_lock_bh(&csk->sk_callback_lock);
1712 psock->save_data_ready = csk->sk_data_ready;
1713 psock->save_write_space = csk->sk_write_space;
1714 psock->save_state_change = csk->sk_state_change;
1715 csk->sk_user_data = psock;
1716 csk->sk_data_ready = psock_tcp_data_ready;
1717 csk->sk_write_space = psock_tcp_write_space;
1718 csk->sk_state_change = psock_tcp_state_change;
1719 write_unlock_bh(&csk->sk_callback_lock);
1721 /* Finished initialization, now add the psock to the MUX. */
1722 spin_lock_bh(&mux->lock);
1723 head = &mux->psocks;
1724 list_for_each_entry(tpsock, &mux->psocks, psock_list) {
1725 if (tpsock->index != index)
1727 head = &tpsock->psock_list;
1731 list_add(&psock->psock_list, head);
1732 psock->index = index;
1734 KCM_STATS_INCR(mux->stats.psock_attach);
1736 psock_now_avail(psock);
1737 spin_unlock_bh(&mux->lock);
1739 /* Schedule RX work in case there are already bytes queued */
1740 queue_work(kcm_wq, &psock->rx_work);
1745 static int kcm_attach_ioctl(struct socket *sock, struct kcm_attach *info)
1747 struct socket *csock;
1748 struct bpf_prog *prog;
1751 csock = sockfd_lookup(info->fd, &err);
1755 prog = bpf_prog_get_type(info->bpf_fd, BPF_PROG_TYPE_SOCKET_FILTER);
1757 err = PTR_ERR(prog);
1761 err = kcm_attach(sock, csock, prog);
1767 /* Keep reference on file also */
1775 static void kcm_unattach(struct kcm_psock *psock)
1777 struct sock *csk = psock->sk;
1778 struct kcm_mux *mux = psock->mux;
1780 /* Stop getting callbacks from TCP socket. After this there should
1781 * be no way to reserve a kcm for this psock.
1783 write_lock_bh(&csk->sk_callback_lock);
1784 csk->sk_user_data = NULL;
1785 csk->sk_data_ready = psock->save_data_ready;
1786 csk->sk_write_space = psock->save_write_space;
1787 csk->sk_state_change = psock->save_state_change;
1788 psock->rx_stopped = 1;
1790 if (WARN_ON(psock->rx_kcm)) {
1791 write_unlock_bh(&csk->sk_callback_lock);
1795 spin_lock_bh(&mux->rx_lock);
1797 /* Stop receiver activities. After this point psock should not be
1798 * able to get onto ready list either through callbacks or work.
1800 if (psock->ready_rx_msg) {
1801 list_del(&psock->psock_ready_list);
1802 kfree_skb(psock->ready_rx_msg);
1803 psock->ready_rx_msg = NULL;
1804 KCM_STATS_INCR(mux->stats.rx_ready_drops);
1807 spin_unlock_bh(&mux->rx_lock);
1809 write_unlock_bh(&csk->sk_callback_lock);
1811 del_timer_sync(&psock->rx_msg_timer);
1812 cancel_work_sync(&psock->rx_work);
1813 cancel_delayed_work_sync(&psock->rx_delayed_work);
1815 bpf_prog_put(psock->bpf_prog);
1817 kfree_skb(psock->rx_skb_head);
1818 psock->rx_skb_head = NULL;
1820 spin_lock_bh(&mux->lock);
1822 aggregate_psock_stats(&psock->stats, &mux->aggregate_psock_stats);
1824 KCM_STATS_INCR(mux->stats.psock_unattach);
1826 if (psock->tx_kcm) {
1827 /* psock was reserved. Just mark it finished and we will clean
1828 * up in the kcm paths, we need kcm lock which can not be
1831 KCM_STATS_INCR(mux->stats.psock_unattach_rsvd);
1832 spin_unlock_bh(&mux->lock);
1834 /* We are unattaching a socket that is reserved. Abort the
1835 * socket since we may be out of sync in sending on it. We need
1836 * to do this without the mux lock.
1838 kcm_abort_tx_psock(psock, EPIPE, false);
1840 spin_lock_bh(&mux->lock);
1841 if (!psock->tx_kcm) {
1842 /* psock now unreserved in window mux was unlocked */
1847 /* Commit done before queuing work to process it */
1850 /* Queue tx work to make sure psock->done is handled */
1851 queue_work(kcm_wq, &psock->tx_kcm->tx_work);
1852 spin_unlock_bh(&mux->lock);
1855 if (!psock->tx_stopped)
1856 list_del(&psock->psock_avail_list);
1857 list_del(&psock->psock_list);
1859 spin_unlock_bh(&mux->lock);
1862 fput(csk->sk_socket->file);
1863 kmem_cache_free(kcm_psockp, psock);
1867 static int kcm_unattach_ioctl(struct socket *sock, struct kcm_unattach *info)
1869 struct kcm_sock *kcm = kcm_sk(sock->sk);
1870 struct kcm_mux *mux = kcm->mux;
1871 struct kcm_psock *psock;
1872 struct socket *csock;
1876 csock = sockfd_lookup(info->fd, &err);
1888 spin_lock_bh(&mux->lock);
1890 list_for_each_entry(psock, &mux->psocks, psock_list) {
1891 if (psock->sk != csk)
1894 /* Found the matching psock */
1896 if (psock->unattaching || WARN_ON(psock->done)) {
1901 psock->unattaching = 1;
1903 spin_unlock_bh(&mux->lock);
1905 kcm_unattach(psock);
1911 spin_unlock_bh(&mux->lock);
1918 static struct proto kcm_proto = {
1920 .owner = THIS_MODULE,
1921 .obj_size = sizeof(struct kcm_sock),
1924 /* Clone a kcm socket. */
1925 static int kcm_clone(struct socket *osock, struct kcm_clone *info,
1926 struct socket **newsockp)
1928 struct socket *newsock;
1930 struct file *newfile;
1934 newsock = sock_alloc();
1938 newsock->type = osock->type;
1939 newsock->ops = osock->ops;
1941 __module_get(newsock->ops->owner);
1943 newfd = get_unused_fd_flags(0);
1944 if (unlikely(newfd < 0)) {
1949 newfile = sock_alloc_file(newsock, 0, osock->sk->sk_prot_creator->name);
1950 if (unlikely(IS_ERR(newfile))) {
1951 err = PTR_ERR(newfile);
1952 goto out_sock_alloc_fail;
1955 newsk = sk_alloc(sock_net(osock->sk), PF_KCM, GFP_KERNEL,
1959 goto out_sk_alloc_fail;
1962 sock_init_data(newsock, newsk);
1963 init_kcm_sock(kcm_sk(newsk), kcm_sk(osock->sk)->mux);
1965 fd_install(newfd, newfile);
1966 *newsockp = newsock;
1973 out_sock_alloc_fail:
1974 put_unused_fd(newfd);
1976 sock_release(newsock);
1981 static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1986 case SIOCKCMATTACH: {
1987 struct kcm_attach info;
1989 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
1992 err = kcm_attach_ioctl(sock, &info);
1996 case SIOCKCMUNATTACH: {
1997 struct kcm_unattach info;
1999 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
2002 err = kcm_unattach_ioctl(sock, &info);
2006 case SIOCKCMCLONE: {
2007 struct kcm_clone info;
2008 struct socket *newsock = NULL;
2010 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
2013 err = kcm_clone(sock, &info, &newsock);
2016 if (copy_to_user((void __user *)arg, &info,
2033 static void free_mux(struct rcu_head *rcu)
2035 struct kcm_mux *mux = container_of(rcu,
2036 struct kcm_mux, rcu);
2038 kmem_cache_free(kcm_muxp, mux);
2041 static void release_mux(struct kcm_mux *mux)
2043 struct kcm_net *knet = mux->knet;
2044 struct kcm_psock *psock, *tmp_psock;
2046 /* Release psocks */
2047 list_for_each_entry_safe(psock, tmp_psock,
2048 &mux->psocks, psock_list) {
2049 if (!WARN_ON(psock->unattaching))
2050 kcm_unattach(psock);
2053 if (WARN_ON(mux->psocks_cnt))
2056 __skb_queue_purge(&mux->rx_hold_queue);
2058 mutex_lock(&knet->mutex);
2059 aggregate_mux_stats(&mux->stats, &knet->aggregate_mux_stats);
2060 aggregate_psock_stats(&mux->aggregate_psock_stats,
2061 &knet->aggregate_psock_stats);
2062 list_del_rcu(&mux->kcm_mux_list);
2064 mutex_unlock(&knet->mutex);
2066 call_rcu(&mux->rcu, free_mux);
2069 static void kcm_done(struct kcm_sock *kcm)
2071 struct kcm_mux *mux = kcm->mux;
2072 struct sock *sk = &kcm->sk;
2075 spin_lock_bh(&mux->rx_lock);
2076 if (kcm->rx_psock) {
2077 /* Cleanup in unreserve_rx_kcm */
2079 kcm->rx_disabled = 1;
2081 spin_unlock_bh(&mux->rx_lock);
2086 list_del(&kcm->wait_rx_list);
2087 kcm->rx_wait = false;
2089 /* Move any pending receive messages to other kcm sockets */
2090 requeue_rx_msgs(mux, &sk->sk_receive_queue);
2092 spin_unlock_bh(&mux->rx_lock);
2094 if (WARN_ON(sk_rmem_alloc_get(sk)))
2097 /* Detach from MUX */
2098 spin_lock_bh(&mux->lock);
2100 list_del(&kcm->kcm_sock_list);
2101 mux->kcm_socks_cnt--;
2102 socks_cnt = mux->kcm_socks_cnt;
2104 spin_unlock_bh(&mux->lock);
2107 /* We are done with the mux now. */
2111 WARN_ON(kcm->rx_wait);
2116 /* Called by kcm_release to close a KCM socket.
2117 * If this is the last KCM socket on the MUX, destroy the MUX.
2119 static int kcm_release(struct socket *sock)
2121 struct sock *sk = sock->sk;
2122 struct kcm_sock *kcm;
2123 struct kcm_mux *mux;
2124 struct kcm_psock *psock;
2133 kfree_skb(kcm->seq_skb);
2136 /* Purge queue under lock to avoid race condition with tx_work trying
2137 * to act when queue is nonempty. If tx_work runs after this point
2138 * it will just return.
2140 __skb_queue_purge(&sk->sk_write_queue);
2143 spin_lock_bh(&mux->lock);
2145 /* Take of tx_wait list, after this point there should be no way
2146 * that a psock will be assigned to this kcm.
2148 list_del(&kcm->wait_psock_list);
2149 kcm->tx_wait = false;
2151 spin_unlock_bh(&mux->lock);
2153 /* Cancel work. After this point there should be no outside references
2154 * to the kcm socket.
2156 cancel_work_sync(&kcm->tx_work);
2159 psock = kcm->tx_psock;
2161 /* A psock was reserved, so we need to kill it since it
2162 * may already have some bytes queued from a message. We
2163 * need to do this after removing kcm from tx_wait list.
2165 kcm_abort_tx_psock(psock, EPIPE, false);
2166 unreserve_psock(kcm);
2170 WARN_ON(kcm->tx_wait);
2171 WARN_ON(kcm->tx_psock);
2180 static const struct proto_ops kcm_dgram_ops = {
2182 .owner = THIS_MODULE,
2183 .release = kcm_release,
2184 .bind = sock_no_bind,
2185 .connect = sock_no_connect,
2186 .socketpair = sock_no_socketpair,
2187 .accept = sock_no_accept,
2188 .getname = sock_no_getname,
2189 .poll = datagram_poll,
2191 .listen = sock_no_listen,
2192 .shutdown = sock_no_shutdown,
2193 .setsockopt = kcm_setsockopt,
2194 .getsockopt = kcm_getsockopt,
2195 .sendmsg = kcm_sendmsg,
2196 .recvmsg = kcm_recvmsg,
2197 .mmap = sock_no_mmap,
2198 .sendpage = kcm_sendpage,
2201 static const struct proto_ops kcm_seqpacket_ops = {
2203 .owner = THIS_MODULE,
2204 .release = kcm_release,
2205 .bind = sock_no_bind,
2206 .connect = sock_no_connect,
2207 .socketpair = sock_no_socketpair,
2208 .accept = sock_no_accept,
2209 .getname = sock_no_getname,
2210 .poll = datagram_poll,
2212 .listen = sock_no_listen,
2213 .shutdown = sock_no_shutdown,
2214 .setsockopt = kcm_setsockopt,
2215 .getsockopt = kcm_getsockopt,
2216 .sendmsg = kcm_sendmsg,
2217 .recvmsg = kcm_recvmsg,
2218 .mmap = sock_no_mmap,
2219 .sendpage = kcm_sendpage,
2220 .splice_read = kcm_splice_read,
2223 /* Create proto operation for kcm sockets */
2224 static int kcm_create(struct net *net, struct socket *sock,
2225 int protocol, int kern)
2227 struct kcm_net *knet = net_generic(net, kcm_net_id);
2229 struct kcm_mux *mux;
2231 switch (sock->type) {
2233 sock->ops = &kcm_dgram_ops;
2235 case SOCK_SEQPACKET:
2236 sock->ops = &kcm_seqpacket_ops;
2239 return -ESOCKTNOSUPPORT;
2242 if (protocol != KCMPROTO_CONNECTED)
2243 return -EPROTONOSUPPORT;
2245 sk = sk_alloc(net, PF_KCM, GFP_KERNEL, &kcm_proto, kern);
2249 /* Allocate a kcm mux, shared between KCM sockets */
2250 mux = kmem_cache_zalloc(kcm_muxp, GFP_KERNEL);
2256 spin_lock_init(&mux->lock);
2257 spin_lock_init(&mux->rx_lock);
2258 INIT_LIST_HEAD(&mux->kcm_socks);
2259 INIT_LIST_HEAD(&mux->kcm_rx_waiters);
2260 INIT_LIST_HEAD(&mux->kcm_tx_waiters);
2262 INIT_LIST_HEAD(&mux->psocks);
2263 INIT_LIST_HEAD(&mux->psocks_ready);
2264 INIT_LIST_HEAD(&mux->psocks_avail);
2268 /* Add new MUX to list */
2269 mutex_lock(&knet->mutex);
2270 list_add_rcu(&mux->kcm_mux_list, &knet->mux_list);
2272 mutex_unlock(&knet->mutex);
2274 skb_queue_head_init(&mux->rx_hold_queue);
2276 /* Init KCM socket */
2277 sock_init_data(sock, sk);
2278 init_kcm_sock(kcm_sk(sk), mux);
2283 static struct net_proto_family kcm_family_ops = {
2285 .create = kcm_create,
2286 .owner = THIS_MODULE,
2289 static __net_init int kcm_init_net(struct net *net)
2291 struct kcm_net *knet = net_generic(net, kcm_net_id);
2293 INIT_LIST_HEAD_RCU(&knet->mux_list);
2294 mutex_init(&knet->mutex);
2299 static __net_exit void kcm_exit_net(struct net *net)
2301 struct kcm_net *knet = net_generic(net, kcm_net_id);
2303 /* All KCM sockets should be closed at this point, which should mean
2304 * that all multiplexors and psocks have been destroyed.
2306 WARN_ON(!list_empty(&knet->mux_list));
2309 static struct pernet_operations kcm_net_ops = {
2310 .init = kcm_init_net,
2311 .exit = kcm_exit_net,
2313 .size = sizeof(struct kcm_net),
2316 static int __init kcm_init(void)
2320 kcm_muxp = kmem_cache_create("kcm_mux_cache",
2321 sizeof(struct kcm_mux), 0,
2322 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2326 kcm_psockp = kmem_cache_create("kcm_psock_cache",
2327 sizeof(struct kcm_psock), 0,
2328 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2332 kcm_wq = create_singlethread_workqueue("kkcmd");
2336 err = proto_register(&kcm_proto, 1);
2340 err = sock_register(&kcm_family_ops);
2342 goto sock_register_fail;
2344 err = register_pernet_device(&kcm_net_ops);
2348 err = kcm_proc_init();
2350 goto proc_init_fail;
2355 unregister_pernet_device(&kcm_net_ops);
2358 sock_unregister(PF_KCM);
2361 proto_unregister(&kcm_proto);
2364 kmem_cache_destroy(kcm_muxp);
2365 kmem_cache_destroy(kcm_psockp);
2368 destroy_workqueue(kcm_wq);
2373 static void __exit kcm_exit(void)
2376 unregister_pernet_device(&kcm_net_ops);
2377 sock_unregister(PF_KCM);
2378 proto_unregister(&kcm_proto);
2379 destroy_workqueue(kcm_wq);
2381 kmem_cache_destroy(kcm_muxp);
2382 kmem_cache_destroy(kcm_psockp);
2385 module_init(kcm_init);
2386 module_exit(kcm_exit);
2388 MODULE_LICENSE("GPL");
2389 MODULE_ALIAS_NETPROTO(PF_KCM);