staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / net / kcm / kcmsock.c
blob5dbc0c48f8cb61c819fc1ced07890bef1016962e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel Connection Multiplexor
5 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
6 */
8 #include <linux/bpf.h>
9 #include <linux/errno.h>
10 #include <linux/errqueue.h>
11 #include <linux/file.h>
12 #include <linux/in.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/netdevice.h>
17 #include <linux/poll.h>
18 #include <linux/rculist.h>
19 #include <linux/skbuff.h>
20 #include <linux/socket.h>
21 #include <linux/uaccess.h>
22 #include <linux/workqueue.h>
23 #include <linux/syscalls.h>
24 #include <linux/sched/signal.h>
26 #include <net/kcm.h>
27 #include <net/netns/generic.h>
28 #include <net/sock.h>
29 #include <uapi/linux/kcm.h>
31 unsigned int kcm_net_id;
33 static struct kmem_cache *kcm_psockp __read_mostly;
34 static struct kmem_cache *kcm_muxp __read_mostly;
35 static struct workqueue_struct *kcm_wq;
37 static inline struct kcm_sock *kcm_sk(const struct sock *sk)
39 return (struct kcm_sock *)sk;
42 static inline struct kcm_tx_msg *kcm_tx_msg(struct sk_buff *skb)
44 return (struct kcm_tx_msg *)skb->cb;
47 static void report_csk_error(struct sock *csk, int err)
49 csk->sk_err = EPIPE;
50 csk->sk_error_report(csk);
53 static void kcm_abort_tx_psock(struct kcm_psock *psock, int err,
54 bool wakeup_kcm)
56 struct sock *csk = psock->sk;
57 struct kcm_mux *mux = psock->mux;
59 /* Unrecoverable error in transmit */
61 spin_lock_bh(&mux->lock);
63 if (psock->tx_stopped) {
64 spin_unlock_bh(&mux->lock);
65 return;
68 psock->tx_stopped = 1;
69 KCM_STATS_INCR(psock->stats.tx_aborts);
71 if (!psock->tx_kcm) {
72 /* Take off psocks_avail list */
73 list_del(&psock->psock_avail_list);
74 } else if (wakeup_kcm) {
75 /* In this case psock is being aborted while outside of
76 * write_msgs and psock is reserved. Schedule tx_work
77 * to handle the failure there. Need to commit tx_stopped
78 * before queuing work.
80 smp_mb();
82 queue_work(kcm_wq, &psock->tx_kcm->tx_work);
85 spin_unlock_bh(&mux->lock);
87 /* Report error on lower socket */
88 report_csk_error(csk, err);
91 /* RX mux lock held. */
92 static void kcm_update_rx_mux_stats(struct kcm_mux *mux,
93 struct kcm_psock *psock)
95 STRP_STATS_ADD(mux->stats.rx_bytes,
96 psock->strp.stats.bytes -
97 psock->saved_rx_bytes);
98 mux->stats.rx_msgs +=
99 psock->strp.stats.msgs - psock->saved_rx_msgs;
100 psock->saved_rx_msgs = psock->strp.stats.msgs;
101 psock->saved_rx_bytes = psock->strp.stats.bytes;
104 static void kcm_update_tx_mux_stats(struct kcm_mux *mux,
105 struct kcm_psock *psock)
107 KCM_STATS_ADD(mux->stats.tx_bytes,
108 psock->stats.tx_bytes - psock->saved_tx_bytes);
109 mux->stats.tx_msgs +=
110 psock->stats.tx_msgs - psock->saved_tx_msgs;
111 psock->saved_tx_msgs = psock->stats.tx_msgs;
112 psock->saved_tx_bytes = psock->stats.tx_bytes;
115 static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
117 /* KCM is ready to receive messages on its queue-- either the KCM is new or
118 * has become unblocked after being blocked on full socket buffer. Queue any
119 * pending ready messages on a psock. RX mux lock held.
121 static void kcm_rcv_ready(struct kcm_sock *kcm)
123 struct kcm_mux *mux = kcm->mux;
124 struct kcm_psock *psock;
125 struct sk_buff *skb;
127 if (unlikely(kcm->rx_wait || kcm->rx_psock || kcm->rx_disabled))
128 return;
130 while (unlikely((skb = __skb_dequeue(&mux->rx_hold_queue)))) {
131 if (kcm_queue_rcv_skb(&kcm->sk, skb)) {
132 /* Assuming buffer limit has been reached */
133 skb_queue_head(&mux->rx_hold_queue, skb);
134 WARN_ON(!sk_rmem_alloc_get(&kcm->sk));
135 return;
139 while (!list_empty(&mux->psocks_ready)) {
140 psock = list_first_entry(&mux->psocks_ready, struct kcm_psock,
141 psock_ready_list);
143 if (kcm_queue_rcv_skb(&kcm->sk, psock->ready_rx_msg)) {
144 /* Assuming buffer limit has been reached */
145 WARN_ON(!sk_rmem_alloc_get(&kcm->sk));
146 return;
149 /* Consumed the ready message on the psock. Schedule rx_work to
150 * get more messages.
152 list_del(&psock->psock_ready_list);
153 psock->ready_rx_msg = NULL;
154 /* Commit clearing of ready_rx_msg for queuing work */
155 smp_mb();
157 strp_unpause(&psock->strp);
158 strp_check_rcv(&psock->strp);
161 /* Buffer limit is okay now, add to ready list */
162 list_add_tail(&kcm->wait_rx_list,
163 &kcm->mux->kcm_rx_waiters);
164 kcm->rx_wait = true;
167 static void kcm_rfree(struct sk_buff *skb)
169 struct sock *sk = skb->sk;
170 struct kcm_sock *kcm = kcm_sk(sk);
171 struct kcm_mux *mux = kcm->mux;
172 unsigned int len = skb->truesize;
174 sk_mem_uncharge(sk, len);
175 atomic_sub(len, &sk->sk_rmem_alloc);
177 /* For reading rx_wait and rx_psock without holding lock */
178 smp_mb__after_atomic();
180 if (!kcm->rx_wait && !kcm->rx_psock &&
181 sk_rmem_alloc_get(sk) < sk->sk_rcvlowat) {
182 spin_lock_bh(&mux->rx_lock);
183 kcm_rcv_ready(kcm);
184 spin_unlock_bh(&mux->rx_lock);
188 static int kcm_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
190 struct sk_buff_head *list = &sk->sk_receive_queue;
192 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
193 return -ENOMEM;
195 if (!sk_rmem_schedule(sk, skb, skb->truesize))
196 return -ENOBUFS;
198 skb->dev = NULL;
200 skb_orphan(skb);
201 skb->sk = sk;
202 skb->destructor = kcm_rfree;
203 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
204 sk_mem_charge(sk, skb->truesize);
206 skb_queue_tail(list, skb);
208 if (!sock_flag(sk, SOCK_DEAD))
209 sk->sk_data_ready(sk);
211 return 0;
214 /* Requeue received messages for a kcm socket to other kcm sockets. This is
215 * called with a kcm socket is receive disabled.
216 * RX mux lock held.
218 static void requeue_rx_msgs(struct kcm_mux *mux, struct sk_buff_head *head)
220 struct sk_buff *skb;
221 struct kcm_sock *kcm;
223 while ((skb = __skb_dequeue(head))) {
224 /* Reset destructor to avoid calling kcm_rcv_ready */
225 skb->destructor = sock_rfree;
226 skb_orphan(skb);
227 try_again:
228 if (list_empty(&mux->kcm_rx_waiters)) {
229 skb_queue_tail(&mux->rx_hold_queue, skb);
230 continue;
233 kcm = list_first_entry(&mux->kcm_rx_waiters,
234 struct kcm_sock, wait_rx_list);
236 if (kcm_queue_rcv_skb(&kcm->sk, skb)) {
237 /* Should mean socket buffer full */
238 list_del(&kcm->wait_rx_list);
239 kcm->rx_wait = false;
241 /* Commit rx_wait to read in kcm_free */
242 smp_wmb();
244 goto try_again;
249 /* Lower sock lock held */
250 static struct kcm_sock *reserve_rx_kcm(struct kcm_psock *psock,
251 struct sk_buff *head)
253 struct kcm_mux *mux = psock->mux;
254 struct kcm_sock *kcm;
256 WARN_ON(psock->ready_rx_msg);
258 if (psock->rx_kcm)
259 return psock->rx_kcm;
261 spin_lock_bh(&mux->rx_lock);
263 if (psock->rx_kcm) {
264 spin_unlock_bh(&mux->rx_lock);
265 return psock->rx_kcm;
268 kcm_update_rx_mux_stats(mux, psock);
270 if (list_empty(&mux->kcm_rx_waiters)) {
271 psock->ready_rx_msg = head;
272 strp_pause(&psock->strp);
273 list_add_tail(&psock->psock_ready_list,
274 &mux->psocks_ready);
275 spin_unlock_bh(&mux->rx_lock);
276 return NULL;
279 kcm = list_first_entry(&mux->kcm_rx_waiters,
280 struct kcm_sock, wait_rx_list);
281 list_del(&kcm->wait_rx_list);
282 kcm->rx_wait = false;
284 psock->rx_kcm = kcm;
285 kcm->rx_psock = psock;
287 spin_unlock_bh(&mux->rx_lock);
289 return kcm;
292 static void kcm_done(struct kcm_sock *kcm);
294 static void kcm_done_work(struct work_struct *w)
296 kcm_done(container_of(w, struct kcm_sock, done_work));
299 /* Lower sock held */
300 static void unreserve_rx_kcm(struct kcm_psock *psock,
301 bool rcv_ready)
303 struct kcm_sock *kcm = psock->rx_kcm;
304 struct kcm_mux *mux = psock->mux;
306 if (!kcm)
307 return;
309 spin_lock_bh(&mux->rx_lock);
311 psock->rx_kcm = NULL;
312 kcm->rx_psock = NULL;
314 /* Commit kcm->rx_psock before sk_rmem_alloc_get to sync with
315 * kcm_rfree
317 smp_mb();
319 if (unlikely(kcm->done)) {
320 spin_unlock_bh(&mux->rx_lock);
322 /* Need to run kcm_done in a task since we need to qcquire
323 * callback locks which may already be held here.
325 INIT_WORK(&kcm->done_work, kcm_done_work);
326 schedule_work(&kcm->done_work);
327 return;
330 if (unlikely(kcm->rx_disabled)) {
331 requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue);
332 } else if (rcv_ready || unlikely(!sk_rmem_alloc_get(&kcm->sk))) {
333 /* Check for degenerative race with rx_wait that all
334 * data was dequeued (accounted for in kcm_rfree).
336 kcm_rcv_ready(kcm);
338 spin_unlock_bh(&mux->rx_lock);
341 /* Lower sock lock held */
342 static void psock_data_ready(struct sock *sk)
344 struct kcm_psock *psock;
346 read_lock_bh(&sk->sk_callback_lock);
348 psock = (struct kcm_psock *)sk->sk_user_data;
349 if (likely(psock))
350 strp_data_ready(&psock->strp);
352 read_unlock_bh(&sk->sk_callback_lock);
355 /* Called with lower sock held */
356 static void kcm_rcv_strparser(struct strparser *strp, struct sk_buff *skb)
358 struct kcm_psock *psock = container_of(strp, struct kcm_psock, strp);
359 struct kcm_sock *kcm;
361 try_queue:
362 kcm = reserve_rx_kcm(psock, skb);
363 if (!kcm) {
364 /* Unable to reserve a KCM, message is held in psock and strp
365 * is paused.
367 return;
370 if (kcm_queue_rcv_skb(&kcm->sk, skb)) {
371 /* Should mean socket buffer full */
372 unreserve_rx_kcm(psock, false);
373 goto try_queue;
377 static int kcm_parse_func_strparser(struct strparser *strp, struct sk_buff *skb)
379 struct kcm_psock *psock = container_of(strp, struct kcm_psock, strp);
380 struct bpf_prog *prog = psock->bpf_prog;
382 return (*prog->bpf_func)(skb, prog->insnsi);
385 static int kcm_read_sock_done(struct strparser *strp, int err)
387 struct kcm_psock *psock = container_of(strp, struct kcm_psock, strp);
389 unreserve_rx_kcm(psock, true);
391 return err;
394 static void psock_state_change(struct sock *sk)
396 /* TCP only does a EPOLLIN for a half close. Do a EPOLLHUP here
397 * since application will normally not poll with EPOLLIN
398 * on the TCP sockets.
401 report_csk_error(sk, EPIPE);
404 static void psock_write_space(struct sock *sk)
406 struct kcm_psock *psock;
407 struct kcm_mux *mux;
408 struct kcm_sock *kcm;
410 read_lock_bh(&sk->sk_callback_lock);
412 psock = (struct kcm_psock *)sk->sk_user_data;
413 if (unlikely(!psock))
414 goto out;
415 mux = psock->mux;
417 spin_lock_bh(&mux->lock);
419 /* Check if the socket is reserved so someone is waiting for sending. */
420 kcm = psock->tx_kcm;
421 if (kcm && !unlikely(kcm->tx_stopped))
422 queue_work(kcm_wq, &kcm->tx_work);
424 spin_unlock_bh(&mux->lock);
425 out:
426 read_unlock_bh(&sk->sk_callback_lock);
429 static void unreserve_psock(struct kcm_sock *kcm);
431 /* kcm sock is locked. */
432 static struct kcm_psock *reserve_psock(struct kcm_sock *kcm)
434 struct kcm_mux *mux = kcm->mux;
435 struct kcm_psock *psock;
437 psock = kcm->tx_psock;
439 smp_rmb(); /* Must read tx_psock before tx_wait */
441 if (psock) {
442 WARN_ON(kcm->tx_wait);
443 if (unlikely(psock->tx_stopped))
444 unreserve_psock(kcm);
445 else
446 return kcm->tx_psock;
449 spin_lock_bh(&mux->lock);
451 /* Check again under lock to see if psock was reserved for this
452 * psock via psock_unreserve.
454 psock = kcm->tx_psock;
455 if (unlikely(psock)) {
456 WARN_ON(kcm->tx_wait);
457 spin_unlock_bh(&mux->lock);
458 return kcm->tx_psock;
461 if (!list_empty(&mux->psocks_avail)) {
462 psock = list_first_entry(&mux->psocks_avail,
463 struct kcm_psock,
464 psock_avail_list);
465 list_del(&psock->psock_avail_list);
466 if (kcm->tx_wait) {
467 list_del(&kcm->wait_psock_list);
468 kcm->tx_wait = false;
470 kcm->tx_psock = psock;
471 psock->tx_kcm = kcm;
472 KCM_STATS_INCR(psock->stats.reserved);
473 } else if (!kcm->tx_wait) {
474 list_add_tail(&kcm->wait_psock_list,
475 &mux->kcm_tx_waiters);
476 kcm->tx_wait = true;
479 spin_unlock_bh(&mux->lock);
481 return psock;
484 /* mux lock held */
485 static void psock_now_avail(struct kcm_psock *psock)
487 struct kcm_mux *mux = psock->mux;
488 struct kcm_sock *kcm;
490 if (list_empty(&mux->kcm_tx_waiters)) {
491 list_add_tail(&psock->psock_avail_list,
492 &mux->psocks_avail);
493 } else {
494 kcm = list_first_entry(&mux->kcm_tx_waiters,
495 struct kcm_sock,
496 wait_psock_list);
497 list_del(&kcm->wait_psock_list);
498 kcm->tx_wait = false;
499 psock->tx_kcm = kcm;
501 /* Commit before changing tx_psock since that is read in
502 * reserve_psock before queuing work.
504 smp_mb();
506 kcm->tx_psock = psock;
507 KCM_STATS_INCR(psock->stats.reserved);
508 queue_work(kcm_wq, &kcm->tx_work);
512 /* kcm sock is locked. */
513 static void unreserve_psock(struct kcm_sock *kcm)
515 struct kcm_psock *psock;
516 struct kcm_mux *mux = kcm->mux;
518 spin_lock_bh(&mux->lock);
520 psock = kcm->tx_psock;
522 if (WARN_ON(!psock)) {
523 spin_unlock_bh(&mux->lock);
524 return;
527 smp_rmb(); /* Read tx_psock before tx_wait */
529 kcm_update_tx_mux_stats(mux, psock);
531 WARN_ON(kcm->tx_wait);
533 kcm->tx_psock = NULL;
534 psock->tx_kcm = NULL;
535 KCM_STATS_INCR(psock->stats.unreserved);
537 if (unlikely(psock->tx_stopped)) {
538 if (psock->done) {
539 /* Deferred free */
540 list_del(&psock->psock_list);
541 mux->psocks_cnt--;
542 sock_put(psock->sk);
543 fput(psock->sk->sk_socket->file);
544 kmem_cache_free(kcm_psockp, psock);
547 /* Don't put back on available list */
549 spin_unlock_bh(&mux->lock);
551 return;
554 psock_now_avail(psock);
556 spin_unlock_bh(&mux->lock);
559 static void kcm_report_tx_retry(struct kcm_sock *kcm)
561 struct kcm_mux *mux = kcm->mux;
563 spin_lock_bh(&mux->lock);
564 KCM_STATS_INCR(mux->stats.tx_retries);
565 spin_unlock_bh(&mux->lock);
568 /* Write any messages ready on the kcm socket. Called with kcm sock lock
569 * held. Return bytes actually sent or error.
571 static int kcm_write_msgs(struct kcm_sock *kcm)
573 struct sock *sk = &kcm->sk;
574 struct kcm_psock *psock;
575 struct sk_buff *skb, *head;
576 struct kcm_tx_msg *txm;
577 unsigned short fragidx, frag_offset;
578 unsigned int sent, total_sent = 0;
579 int ret = 0;
581 kcm->tx_wait_more = false;
582 psock = kcm->tx_psock;
583 if (unlikely(psock && psock->tx_stopped)) {
584 /* A reserved psock was aborted asynchronously. Unreserve
585 * it and we'll retry the message.
587 unreserve_psock(kcm);
588 kcm_report_tx_retry(kcm);
589 if (skb_queue_empty(&sk->sk_write_queue))
590 return 0;
592 kcm_tx_msg(skb_peek(&sk->sk_write_queue))->sent = 0;
594 } else if (skb_queue_empty(&sk->sk_write_queue)) {
595 return 0;
598 head = skb_peek(&sk->sk_write_queue);
599 txm = kcm_tx_msg(head);
601 if (txm->sent) {
602 /* Send of first skbuff in queue already in progress */
603 if (WARN_ON(!psock)) {
604 ret = -EINVAL;
605 goto out;
607 sent = txm->sent;
608 frag_offset = txm->frag_offset;
609 fragidx = txm->fragidx;
610 skb = txm->frag_skb;
612 goto do_frag;
615 try_again:
616 psock = reserve_psock(kcm);
617 if (!psock)
618 goto out;
620 do {
621 skb = head;
622 txm = kcm_tx_msg(head);
623 sent = 0;
625 do_frag_list:
626 if (WARN_ON(!skb_shinfo(skb)->nr_frags)) {
627 ret = -EINVAL;
628 goto out;
631 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags;
632 fragidx++) {
633 skb_frag_t *frag;
635 frag_offset = 0;
636 do_frag:
637 frag = &skb_shinfo(skb)->frags[fragidx];
638 if (WARN_ON(!frag->size)) {
639 ret = -EINVAL;
640 goto out;
643 ret = kernel_sendpage(psock->sk->sk_socket,
644 frag->page.p,
645 frag->page_offset + frag_offset,
646 frag->size - frag_offset,
647 MSG_DONTWAIT);
648 if (ret <= 0) {
649 if (ret == -EAGAIN) {
650 /* Save state to try again when there's
651 * write space on the socket
653 txm->sent = sent;
654 txm->frag_offset = frag_offset;
655 txm->fragidx = fragidx;
656 txm->frag_skb = skb;
658 ret = 0;
659 goto out;
662 /* Hard failure in sending message, abort this
663 * psock since it has lost framing
664 * synchonization and retry sending the
665 * message from the beginning.
667 kcm_abort_tx_psock(psock, ret ? -ret : EPIPE,
668 true);
669 unreserve_psock(kcm);
671 txm->sent = 0;
672 kcm_report_tx_retry(kcm);
673 ret = 0;
675 goto try_again;
678 sent += ret;
679 frag_offset += ret;
680 KCM_STATS_ADD(psock->stats.tx_bytes, ret);
681 if (frag_offset < frag->size) {
682 /* Not finished with this frag */
683 goto do_frag;
687 if (skb == head) {
688 if (skb_has_frag_list(skb)) {
689 skb = skb_shinfo(skb)->frag_list;
690 goto do_frag_list;
692 } else if (skb->next) {
693 skb = skb->next;
694 goto do_frag_list;
697 /* Successfully sent the whole packet, account for it. */
698 skb_dequeue(&sk->sk_write_queue);
699 kfree_skb(head);
700 sk->sk_wmem_queued -= sent;
701 total_sent += sent;
702 KCM_STATS_INCR(psock->stats.tx_msgs);
703 } while ((head = skb_peek(&sk->sk_write_queue)));
704 out:
705 if (!head) {
706 /* Done with all queued messages. */
707 WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
708 unreserve_psock(kcm);
711 /* Check if write space is available */
712 sk->sk_write_space(sk);
714 return total_sent ? : ret;
717 static void kcm_tx_work(struct work_struct *w)
719 struct kcm_sock *kcm = container_of(w, struct kcm_sock, tx_work);
720 struct sock *sk = &kcm->sk;
721 int err;
723 lock_sock(sk);
725 /* Primarily for SOCK_DGRAM sockets, also handle asynchronous tx
726 * aborts
728 err = kcm_write_msgs(kcm);
729 if (err < 0) {
730 /* Hard failure in write, report error on KCM socket */
731 pr_warn("KCM: Hard failure on kcm_write_msgs %d\n", err);
732 report_csk_error(&kcm->sk, -err);
733 goto out;
736 /* Primarily for SOCK_SEQPACKET sockets */
737 if (likely(sk->sk_socket) &&
738 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
739 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
740 sk->sk_write_space(sk);
743 out:
744 release_sock(sk);
747 static void kcm_push(struct kcm_sock *kcm)
749 if (kcm->tx_wait_more)
750 kcm_write_msgs(kcm);
753 static ssize_t kcm_sendpage(struct socket *sock, struct page *page,
754 int offset, size_t size, int flags)
757 struct sock *sk = sock->sk;
758 struct kcm_sock *kcm = kcm_sk(sk);
759 struct sk_buff *skb = NULL, *head = NULL;
760 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
761 bool eor;
762 int err = 0;
763 int i;
765 if (flags & MSG_SENDPAGE_NOTLAST)
766 flags |= MSG_MORE;
768 /* No MSG_EOR from splice, only look at MSG_MORE */
769 eor = !(flags & MSG_MORE);
771 lock_sock(sk);
773 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
775 err = -EPIPE;
776 if (sk->sk_err)
777 goto out_error;
779 if (kcm->seq_skb) {
780 /* Previously opened message */
781 head = kcm->seq_skb;
782 skb = kcm_tx_msg(head)->last_skb;
783 i = skb_shinfo(skb)->nr_frags;
785 if (skb_can_coalesce(skb, i, page, offset)) {
786 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
787 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
788 goto coalesced;
791 if (i >= MAX_SKB_FRAGS) {
792 struct sk_buff *tskb;
794 tskb = alloc_skb(0, sk->sk_allocation);
795 while (!tskb) {
796 kcm_push(kcm);
797 err = sk_stream_wait_memory(sk, &timeo);
798 if (err)
799 goto out_error;
802 if (head == skb)
803 skb_shinfo(head)->frag_list = tskb;
804 else
805 skb->next = tskb;
807 skb = tskb;
808 skb->ip_summed = CHECKSUM_UNNECESSARY;
809 i = 0;
811 } else {
812 /* Call the sk_stream functions to manage the sndbuf mem. */
813 if (!sk_stream_memory_free(sk)) {
814 kcm_push(kcm);
815 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
816 err = sk_stream_wait_memory(sk, &timeo);
817 if (err)
818 goto out_error;
821 head = alloc_skb(0, sk->sk_allocation);
822 while (!head) {
823 kcm_push(kcm);
824 err = sk_stream_wait_memory(sk, &timeo);
825 if (err)
826 goto out_error;
829 skb = head;
830 i = 0;
833 get_page(page);
834 skb_fill_page_desc(skb, i, page, offset, size);
835 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
837 coalesced:
838 skb->len += size;
839 skb->data_len += size;
840 skb->truesize += size;
841 sk->sk_wmem_queued += size;
842 sk_mem_charge(sk, size);
844 if (head != skb) {
845 head->len += size;
846 head->data_len += size;
847 head->truesize += size;
850 if (eor) {
851 bool not_busy = skb_queue_empty(&sk->sk_write_queue);
853 /* Message complete, queue it on send buffer */
854 __skb_queue_tail(&sk->sk_write_queue, head);
855 kcm->seq_skb = NULL;
856 KCM_STATS_INCR(kcm->stats.tx_msgs);
858 if (flags & MSG_BATCH) {
859 kcm->tx_wait_more = true;
860 } else if (kcm->tx_wait_more || not_busy) {
861 err = kcm_write_msgs(kcm);
862 if (err < 0) {
863 /* We got a hard error in write_msgs but have
864 * already queued this message. Report an error
865 * in the socket, but don't affect return value
866 * from sendmsg
868 pr_warn("KCM: Hard failure on kcm_write_msgs\n");
869 report_csk_error(&kcm->sk, -err);
872 } else {
873 /* Message not complete, save state */
874 kcm->seq_skb = head;
875 kcm_tx_msg(head)->last_skb = skb;
878 KCM_STATS_ADD(kcm->stats.tx_bytes, size);
880 release_sock(sk);
881 return size;
883 out_error:
884 kcm_push(kcm);
886 err = sk_stream_error(sk, flags, err);
888 /* make sure we wake any epoll edge trigger waiter */
889 if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN))
890 sk->sk_write_space(sk);
892 release_sock(sk);
893 return err;
896 static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
898 struct sock *sk = sock->sk;
899 struct kcm_sock *kcm = kcm_sk(sk);
900 struct sk_buff *skb = NULL, *head = NULL;
901 size_t copy, copied = 0;
902 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
903 int eor = (sock->type == SOCK_DGRAM) ?
904 !(msg->msg_flags & MSG_MORE) : !!(msg->msg_flags & MSG_EOR);
905 int err = -EPIPE;
907 lock_sock(sk);
909 /* Per tcp_sendmsg this should be in poll */
910 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
912 if (sk->sk_err)
913 goto out_error;
915 if (kcm->seq_skb) {
916 /* Previously opened message */
917 head = kcm->seq_skb;
918 skb = kcm_tx_msg(head)->last_skb;
919 goto start;
922 /* Call the sk_stream functions to manage the sndbuf mem. */
923 if (!sk_stream_memory_free(sk)) {
924 kcm_push(kcm);
925 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
926 err = sk_stream_wait_memory(sk, &timeo);
927 if (err)
928 goto out_error;
931 if (msg_data_left(msg)) {
932 /* New message, alloc head skb */
933 head = alloc_skb(0, sk->sk_allocation);
934 while (!head) {
935 kcm_push(kcm);
936 err = sk_stream_wait_memory(sk, &timeo);
937 if (err)
938 goto out_error;
940 head = alloc_skb(0, sk->sk_allocation);
943 skb = head;
945 /* Set ip_summed to CHECKSUM_UNNECESSARY to avoid calling
946 * csum_and_copy_from_iter from skb_do_copy_data_nocache.
948 skb->ip_summed = CHECKSUM_UNNECESSARY;
951 start:
952 while (msg_data_left(msg)) {
953 bool merge = true;
954 int i = skb_shinfo(skb)->nr_frags;
955 struct page_frag *pfrag = sk_page_frag(sk);
957 if (!sk_page_frag_refill(sk, pfrag))
958 goto wait_for_memory;
960 if (!skb_can_coalesce(skb, i, pfrag->page,
961 pfrag->offset)) {
962 if (i == MAX_SKB_FRAGS) {
963 struct sk_buff *tskb;
965 tskb = alloc_skb(0, sk->sk_allocation);
966 if (!tskb)
967 goto wait_for_memory;
969 if (head == skb)
970 skb_shinfo(head)->frag_list = tskb;
971 else
972 skb->next = tskb;
974 skb = tskb;
975 skb->ip_summed = CHECKSUM_UNNECESSARY;
976 continue;
978 merge = false;
981 copy = min_t(int, msg_data_left(msg),
982 pfrag->size - pfrag->offset);
984 if (!sk_wmem_schedule(sk, copy))
985 goto wait_for_memory;
987 err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb,
988 pfrag->page,
989 pfrag->offset,
990 copy);
991 if (err)
992 goto out_error;
994 /* Update the skb. */
995 if (merge) {
996 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
997 } else {
998 skb_fill_page_desc(skb, i, pfrag->page,
999 pfrag->offset, copy);
1000 get_page(pfrag->page);
1003 pfrag->offset += copy;
1004 copied += copy;
1005 if (head != skb) {
1006 head->len += copy;
1007 head->data_len += copy;
1010 continue;
1012 wait_for_memory:
1013 kcm_push(kcm);
1014 err = sk_stream_wait_memory(sk, &timeo);
1015 if (err)
1016 goto out_error;
1019 if (eor) {
1020 bool not_busy = skb_queue_empty(&sk->sk_write_queue);
1022 if (head) {
1023 /* Message complete, queue it on send buffer */
1024 __skb_queue_tail(&sk->sk_write_queue, head);
1025 kcm->seq_skb = NULL;
1026 KCM_STATS_INCR(kcm->stats.tx_msgs);
1029 if (msg->msg_flags & MSG_BATCH) {
1030 kcm->tx_wait_more = true;
1031 } else if (kcm->tx_wait_more || not_busy) {
1032 err = kcm_write_msgs(kcm);
1033 if (err < 0) {
1034 /* We got a hard error in write_msgs but have
1035 * already queued this message. Report an error
1036 * in the socket, but don't affect return value
1037 * from sendmsg
1039 pr_warn("KCM: Hard failure on kcm_write_msgs\n");
1040 report_csk_error(&kcm->sk, -err);
1043 } else {
1044 /* Message not complete, save state */
1045 partial_message:
1046 if (head) {
1047 kcm->seq_skb = head;
1048 kcm_tx_msg(head)->last_skb = skb;
1052 KCM_STATS_ADD(kcm->stats.tx_bytes, copied);
1054 release_sock(sk);
1055 return copied;
1057 out_error:
1058 kcm_push(kcm);
1060 if (copied && sock->type == SOCK_SEQPACKET) {
1061 /* Wrote some bytes before encountering an
1062 * error, return partial success.
1064 goto partial_message;
1067 if (head != kcm->seq_skb)
1068 kfree_skb(head);
1070 err = sk_stream_error(sk, msg->msg_flags, err);
1072 /* make sure we wake any epoll edge trigger waiter */
1073 if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN))
1074 sk->sk_write_space(sk);
1076 release_sock(sk);
1077 return err;
1080 static struct sk_buff *kcm_wait_data(struct sock *sk, int flags,
1081 long timeo, int *err)
1083 struct sk_buff *skb;
1085 while (!(skb = skb_peek(&sk->sk_receive_queue))) {
1086 if (sk->sk_err) {
1087 *err = sock_error(sk);
1088 return NULL;
1091 if (sock_flag(sk, SOCK_DONE))
1092 return NULL;
1094 if ((flags & MSG_DONTWAIT) || !timeo) {
1095 *err = -EAGAIN;
1096 return NULL;
1099 sk_wait_data(sk, &timeo, NULL);
1101 /* Handle signals */
1102 if (signal_pending(current)) {
1103 *err = sock_intr_errno(timeo);
1104 return NULL;
1108 return skb;
1111 static int kcm_recvmsg(struct socket *sock, struct msghdr *msg,
1112 size_t len, int flags)
1114 struct sock *sk = sock->sk;
1115 struct kcm_sock *kcm = kcm_sk(sk);
1116 int err = 0;
1117 long timeo;
1118 struct strp_msg *stm;
1119 int copied = 0;
1120 struct sk_buff *skb;
1122 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1124 lock_sock(sk);
1126 skb = kcm_wait_data(sk, flags, timeo, &err);
1127 if (!skb)
1128 goto out;
1130 /* Okay, have a message on the receive queue */
1132 stm = strp_msg(skb);
1134 if (len > stm->full_len)
1135 len = stm->full_len;
1137 err = skb_copy_datagram_msg(skb, stm->offset, msg, len);
1138 if (err < 0)
1139 goto out;
1141 copied = len;
1142 if (likely(!(flags & MSG_PEEK))) {
1143 KCM_STATS_ADD(kcm->stats.rx_bytes, copied);
1144 if (copied < stm->full_len) {
1145 if (sock->type == SOCK_DGRAM) {
1146 /* Truncated message */
1147 msg->msg_flags |= MSG_TRUNC;
1148 goto msg_finished;
1150 stm->offset += copied;
1151 stm->full_len -= copied;
1152 } else {
1153 msg_finished:
1154 /* Finished with message */
1155 msg->msg_flags |= MSG_EOR;
1156 KCM_STATS_INCR(kcm->stats.rx_msgs);
1157 skb_unlink(skb, &sk->sk_receive_queue);
1158 kfree_skb(skb);
1162 out:
1163 release_sock(sk);
1165 return copied ? : err;
1168 static ssize_t kcm_splice_read(struct socket *sock, loff_t *ppos,
1169 struct pipe_inode_info *pipe, size_t len,
1170 unsigned int flags)
1172 struct sock *sk = sock->sk;
1173 struct kcm_sock *kcm = kcm_sk(sk);
1174 long timeo;
1175 struct strp_msg *stm;
1176 int err = 0;
1177 ssize_t copied;
1178 struct sk_buff *skb;
1180 /* Only support splice for SOCKSEQPACKET */
1182 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1184 lock_sock(sk);
1186 skb = kcm_wait_data(sk, flags, timeo, &err);
1187 if (!skb)
1188 goto err_out;
1190 /* Okay, have a message on the receive queue */
1192 stm = strp_msg(skb);
1194 if (len > stm->full_len)
1195 len = stm->full_len;
1197 copied = skb_splice_bits(skb, sk, stm->offset, pipe, len, flags);
1198 if (copied < 0) {
1199 err = copied;
1200 goto err_out;
1203 KCM_STATS_ADD(kcm->stats.rx_bytes, copied);
1205 stm->offset += copied;
1206 stm->full_len -= copied;
1208 /* We have no way to return MSG_EOR. If all the bytes have been
1209 * read we still leave the message in the receive socket buffer.
1210 * A subsequent recvmsg needs to be done to return MSG_EOR and
1211 * finish reading the message.
1214 release_sock(sk);
1216 return copied;
1218 err_out:
1219 release_sock(sk);
1221 return err;
1224 /* kcm sock lock held */
1225 static void kcm_recv_disable(struct kcm_sock *kcm)
1227 struct kcm_mux *mux = kcm->mux;
1229 if (kcm->rx_disabled)
1230 return;
1232 spin_lock_bh(&mux->rx_lock);
1234 kcm->rx_disabled = 1;
1236 /* If a psock is reserved we'll do cleanup in unreserve */
1237 if (!kcm->rx_psock) {
1238 if (kcm->rx_wait) {
1239 list_del(&kcm->wait_rx_list);
1240 kcm->rx_wait = false;
1243 requeue_rx_msgs(mux, &kcm->sk.sk_receive_queue);
1246 spin_unlock_bh(&mux->rx_lock);
1249 /* kcm sock lock held */
1250 static void kcm_recv_enable(struct kcm_sock *kcm)
1252 struct kcm_mux *mux = kcm->mux;
1254 if (!kcm->rx_disabled)
1255 return;
1257 spin_lock_bh(&mux->rx_lock);
1259 kcm->rx_disabled = 0;
1260 kcm_rcv_ready(kcm);
1262 spin_unlock_bh(&mux->rx_lock);
1265 static int kcm_setsockopt(struct socket *sock, int level, int optname,
1266 char __user *optval, unsigned int optlen)
1268 struct kcm_sock *kcm = kcm_sk(sock->sk);
1269 int val, valbool;
1270 int err = 0;
1272 if (level != SOL_KCM)
1273 return -ENOPROTOOPT;
1275 if (optlen < sizeof(int))
1276 return -EINVAL;
1278 if (get_user(val, (int __user *)optval))
1279 return -EINVAL;
1281 valbool = val ? 1 : 0;
1283 switch (optname) {
1284 case KCM_RECV_DISABLE:
1285 lock_sock(&kcm->sk);
1286 if (valbool)
1287 kcm_recv_disable(kcm);
1288 else
1289 kcm_recv_enable(kcm);
1290 release_sock(&kcm->sk);
1291 break;
1292 default:
1293 err = -ENOPROTOOPT;
1296 return err;
1299 static int kcm_getsockopt(struct socket *sock, int level, int optname,
1300 char __user *optval, int __user *optlen)
1302 struct kcm_sock *kcm = kcm_sk(sock->sk);
1303 int val, len;
1305 if (level != SOL_KCM)
1306 return -ENOPROTOOPT;
1308 if (get_user(len, optlen))
1309 return -EFAULT;
1311 len = min_t(unsigned int, len, sizeof(int));
1312 if (len < 0)
1313 return -EINVAL;
1315 switch (optname) {
1316 case KCM_RECV_DISABLE:
1317 val = kcm->rx_disabled;
1318 break;
1319 default:
1320 return -ENOPROTOOPT;
1323 if (put_user(len, optlen))
1324 return -EFAULT;
1325 if (copy_to_user(optval, &val, len))
1326 return -EFAULT;
1327 return 0;
1330 static void init_kcm_sock(struct kcm_sock *kcm, struct kcm_mux *mux)
1332 struct kcm_sock *tkcm;
1333 struct list_head *head;
1334 int index = 0;
1336 /* For SOCK_SEQPACKET sock type, datagram_poll checks the sk_state, so
1337 * we set sk_state, otherwise epoll_wait always returns right away with
1338 * EPOLLHUP
1340 kcm->sk.sk_state = TCP_ESTABLISHED;
1342 /* Add to mux's kcm sockets list */
1343 kcm->mux = mux;
1344 spin_lock_bh(&mux->lock);
1346 head = &mux->kcm_socks;
1347 list_for_each_entry(tkcm, &mux->kcm_socks, kcm_sock_list) {
1348 if (tkcm->index != index)
1349 break;
1350 head = &tkcm->kcm_sock_list;
1351 index++;
1354 list_add(&kcm->kcm_sock_list, head);
1355 kcm->index = index;
1357 mux->kcm_socks_cnt++;
1358 spin_unlock_bh(&mux->lock);
1360 INIT_WORK(&kcm->tx_work, kcm_tx_work);
1362 spin_lock_bh(&mux->rx_lock);
1363 kcm_rcv_ready(kcm);
1364 spin_unlock_bh(&mux->rx_lock);
1367 static int kcm_attach(struct socket *sock, struct socket *csock,
1368 struct bpf_prog *prog)
1370 struct kcm_sock *kcm = kcm_sk(sock->sk);
1371 struct kcm_mux *mux = kcm->mux;
1372 struct sock *csk;
1373 struct kcm_psock *psock = NULL, *tpsock;
1374 struct list_head *head;
1375 int index = 0;
1376 static const struct strp_callbacks cb = {
1377 .rcv_msg = kcm_rcv_strparser,
1378 .parse_msg = kcm_parse_func_strparser,
1379 .read_sock_done = kcm_read_sock_done,
1381 int err = 0;
1383 csk = csock->sk;
1384 if (!csk)
1385 return -EINVAL;
1387 lock_sock(csk);
1389 /* Only allow TCP sockets to be attached for now */
1390 if ((csk->sk_family != AF_INET && csk->sk_family != AF_INET6) ||
1391 csk->sk_protocol != IPPROTO_TCP) {
1392 err = -EOPNOTSUPP;
1393 goto out;
1396 /* Don't allow listeners or closed sockets */
1397 if (csk->sk_state == TCP_LISTEN || csk->sk_state == TCP_CLOSE) {
1398 err = -EOPNOTSUPP;
1399 goto out;
1402 psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL);
1403 if (!psock) {
1404 err = -ENOMEM;
1405 goto out;
1408 psock->mux = mux;
1409 psock->sk = csk;
1410 psock->bpf_prog = prog;
1412 err = strp_init(&psock->strp, csk, &cb);
1413 if (err) {
1414 kmem_cache_free(kcm_psockp, psock);
1415 goto out;
1418 write_lock_bh(&csk->sk_callback_lock);
1420 /* Check if sk_user_data is aready by KCM or someone else.
1421 * Must be done under lock to prevent race conditions.
1423 if (csk->sk_user_data) {
1424 write_unlock_bh(&csk->sk_callback_lock);
1425 strp_stop(&psock->strp);
1426 strp_done(&psock->strp);
1427 kmem_cache_free(kcm_psockp, psock);
1428 err = -EALREADY;
1429 goto out;
1432 psock->save_data_ready = csk->sk_data_ready;
1433 psock->save_write_space = csk->sk_write_space;
1434 psock->save_state_change = csk->sk_state_change;
1435 csk->sk_user_data = psock;
1436 csk->sk_data_ready = psock_data_ready;
1437 csk->sk_write_space = psock_write_space;
1438 csk->sk_state_change = psock_state_change;
1440 write_unlock_bh(&csk->sk_callback_lock);
1442 sock_hold(csk);
1444 /* Finished initialization, now add the psock to the MUX. */
1445 spin_lock_bh(&mux->lock);
1446 head = &mux->psocks;
1447 list_for_each_entry(tpsock, &mux->psocks, psock_list) {
1448 if (tpsock->index != index)
1449 break;
1450 head = &tpsock->psock_list;
1451 index++;
1454 list_add(&psock->psock_list, head);
1455 psock->index = index;
1457 KCM_STATS_INCR(mux->stats.psock_attach);
1458 mux->psocks_cnt++;
1459 psock_now_avail(psock);
1460 spin_unlock_bh(&mux->lock);
1462 /* Schedule RX work in case there are already bytes queued */
1463 strp_check_rcv(&psock->strp);
1465 out:
1466 release_sock(csk);
1468 return err;
1471 static int kcm_attach_ioctl(struct socket *sock, struct kcm_attach *info)
1473 struct socket *csock;
1474 struct bpf_prog *prog;
1475 int err;
1477 csock = sockfd_lookup(info->fd, &err);
1478 if (!csock)
1479 return -ENOENT;
1481 prog = bpf_prog_get_type(info->bpf_fd, BPF_PROG_TYPE_SOCKET_FILTER);
1482 if (IS_ERR(prog)) {
1483 err = PTR_ERR(prog);
1484 goto out;
1487 err = kcm_attach(sock, csock, prog);
1488 if (err) {
1489 bpf_prog_put(prog);
1490 goto out;
1493 /* Keep reference on file also */
1495 return 0;
1496 out:
1497 fput(csock->file);
1498 return err;
1501 static void kcm_unattach(struct kcm_psock *psock)
1503 struct sock *csk = psock->sk;
1504 struct kcm_mux *mux = psock->mux;
1506 lock_sock(csk);
1508 /* Stop getting callbacks from TCP socket. After this there should
1509 * be no way to reserve a kcm for this psock.
1511 write_lock_bh(&csk->sk_callback_lock);
1512 csk->sk_user_data = NULL;
1513 csk->sk_data_ready = psock->save_data_ready;
1514 csk->sk_write_space = psock->save_write_space;
1515 csk->sk_state_change = psock->save_state_change;
1516 strp_stop(&psock->strp);
1518 if (WARN_ON(psock->rx_kcm)) {
1519 write_unlock_bh(&csk->sk_callback_lock);
1520 release_sock(csk);
1521 return;
1524 spin_lock_bh(&mux->rx_lock);
1526 /* Stop receiver activities. After this point psock should not be
1527 * able to get onto ready list either through callbacks or work.
1529 if (psock->ready_rx_msg) {
1530 list_del(&psock->psock_ready_list);
1531 kfree_skb(psock->ready_rx_msg);
1532 psock->ready_rx_msg = NULL;
1533 KCM_STATS_INCR(mux->stats.rx_ready_drops);
1536 spin_unlock_bh(&mux->rx_lock);
1538 write_unlock_bh(&csk->sk_callback_lock);
1540 /* Call strp_done without sock lock */
1541 release_sock(csk);
1542 strp_done(&psock->strp);
1543 lock_sock(csk);
1545 bpf_prog_put(psock->bpf_prog);
1547 spin_lock_bh(&mux->lock);
1549 aggregate_psock_stats(&psock->stats, &mux->aggregate_psock_stats);
1550 save_strp_stats(&psock->strp, &mux->aggregate_strp_stats);
1552 KCM_STATS_INCR(mux->stats.psock_unattach);
1554 if (psock->tx_kcm) {
1555 /* psock was reserved. Just mark it finished and we will clean
1556 * up in the kcm paths, we need kcm lock which can not be
1557 * acquired here.
1559 KCM_STATS_INCR(mux->stats.psock_unattach_rsvd);
1560 spin_unlock_bh(&mux->lock);
1562 /* We are unattaching a socket that is reserved. Abort the
1563 * socket since we may be out of sync in sending on it. We need
1564 * to do this without the mux lock.
1566 kcm_abort_tx_psock(psock, EPIPE, false);
1568 spin_lock_bh(&mux->lock);
1569 if (!psock->tx_kcm) {
1570 /* psock now unreserved in window mux was unlocked */
1571 goto no_reserved;
1573 psock->done = 1;
1575 /* Commit done before queuing work to process it */
1576 smp_mb();
1578 /* Queue tx work to make sure psock->done is handled */
1579 queue_work(kcm_wq, &psock->tx_kcm->tx_work);
1580 spin_unlock_bh(&mux->lock);
1581 } else {
1582 no_reserved:
1583 if (!psock->tx_stopped)
1584 list_del(&psock->psock_avail_list);
1585 list_del(&psock->psock_list);
1586 mux->psocks_cnt--;
1587 spin_unlock_bh(&mux->lock);
1589 sock_put(csk);
1590 fput(csk->sk_socket->file);
1591 kmem_cache_free(kcm_psockp, psock);
1594 release_sock(csk);
1597 static int kcm_unattach_ioctl(struct socket *sock, struct kcm_unattach *info)
1599 struct kcm_sock *kcm = kcm_sk(sock->sk);
1600 struct kcm_mux *mux = kcm->mux;
1601 struct kcm_psock *psock;
1602 struct socket *csock;
1603 struct sock *csk;
1604 int err;
1606 csock = sockfd_lookup(info->fd, &err);
1607 if (!csock)
1608 return -ENOENT;
1610 csk = csock->sk;
1611 if (!csk) {
1612 err = -EINVAL;
1613 goto out;
1616 err = -ENOENT;
1618 spin_lock_bh(&mux->lock);
1620 list_for_each_entry(psock, &mux->psocks, psock_list) {
1621 if (psock->sk != csk)
1622 continue;
1624 /* Found the matching psock */
1626 if (psock->unattaching || WARN_ON(psock->done)) {
1627 err = -EALREADY;
1628 break;
1631 psock->unattaching = 1;
1633 spin_unlock_bh(&mux->lock);
1635 /* Lower socket lock should already be held */
1636 kcm_unattach(psock);
1638 err = 0;
1639 goto out;
1642 spin_unlock_bh(&mux->lock);
1644 out:
1645 fput(csock->file);
1646 return err;
1649 static struct proto kcm_proto = {
1650 .name = "KCM",
1651 .owner = THIS_MODULE,
1652 .obj_size = sizeof(struct kcm_sock),
1655 /* Clone a kcm socket. */
1656 static struct file *kcm_clone(struct socket *osock)
1658 struct socket *newsock;
1659 struct sock *newsk;
1661 newsock = sock_alloc();
1662 if (!newsock)
1663 return ERR_PTR(-ENFILE);
1665 newsock->type = osock->type;
1666 newsock->ops = osock->ops;
1668 __module_get(newsock->ops->owner);
1670 newsk = sk_alloc(sock_net(osock->sk), PF_KCM, GFP_KERNEL,
1671 &kcm_proto, false);
1672 if (!newsk) {
1673 sock_release(newsock);
1674 return ERR_PTR(-ENOMEM);
1676 sock_init_data(newsock, newsk);
1677 init_kcm_sock(kcm_sk(newsk), kcm_sk(osock->sk)->mux);
1679 return sock_alloc_file(newsock, 0, osock->sk->sk_prot_creator->name);
1682 static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1684 int err;
1686 switch (cmd) {
1687 case SIOCKCMATTACH: {
1688 struct kcm_attach info;
1690 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
1691 return -EFAULT;
1693 err = kcm_attach_ioctl(sock, &info);
1695 break;
1697 case SIOCKCMUNATTACH: {
1698 struct kcm_unattach info;
1700 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
1701 return -EFAULT;
1703 err = kcm_unattach_ioctl(sock, &info);
1705 break;
1707 case SIOCKCMCLONE: {
1708 struct kcm_clone info;
1709 struct file *file;
1711 info.fd = get_unused_fd_flags(0);
1712 if (unlikely(info.fd < 0))
1713 return info.fd;
1715 file = kcm_clone(sock);
1716 if (IS_ERR(file)) {
1717 put_unused_fd(info.fd);
1718 return PTR_ERR(file);
1720 if (copy_to_user((void __user *)arg, &info,
1721 sizeof(info))) {
1722 put_unused_fd(info.fd);
1723 fput(file);
1724 return -EFAULT;
1726 fd_install(info.fd, file);
1727 err = 0;
1728 break;
1730 default:
1731 err = -ENOIOCTLCMD;
1732 break;
1735 return err;
1738 static void free_mux(struct rcu_head *rcu)
1740 struct kcm_mux *mux = container_of(rcu,
1741 struct kcm_mux, rcu);
1743 kmem_cache_free(kcm_muxp, mux);
1746 static void release_mux(struct kcm_mux *mux)
1748 struct kcm_net *knet = mux->knet;
1749 struct kcm_psock *psock, *tmp_psock;
1751 /* Release psocks */
1752 list_for_each_entry_safe(psock, tmp_psock,
1753 &mux->psocks, psock_list) {
1754 if (!WARN_ON(psock->unattaching))
1755 kcm_unattach(psock);
1758 if (WARN_ON(mux->psocks_cnt))
1759 return;
1761 __skb_queue_purge(&mux->rx_hold_queue);
1763 mutex_lock(&knet->mutex);
1764 aggregate_mux_stats(&mux->stats, &knet->aggregate_mux_stats);
1765 aggregate_psock_stats(&mux->aggregate_psock_stats,
1766 &knet->aggregate_psock_stats);
1767 aggregate_strp_stats(&mux->aggregate_strp_stats,
1768 &knet->aggregate_strp_stats);
1769 list_del_rcu(&mux->kcm_mux_list);
1770 knet->count--;
1771 mutex_unlock(&knet->mutex);
1773 call_rcu(&mux->rcu, free_mux);
1776 static void kcm_done(struct kcm_sock *kcm)
1778 struct kcm_mux *mux = kcm->mux;
1779 struct sock *sk = &kcm->sk;
1780 int socks_cnt;
1782 spin_lock_bh(&mux->rx_lock);
1783 if (kcm->rx_psock) {
1784 /* Cleanup in unreserve_rx_kcm */
1785 WARN_ON(kcm->done);
1786 kcm->rx_disabled = 1;
1787 kcm->done = 1;
1788 spin_unlock_bh(&mux->rx_lock);
1789 return;
1792 if (kcm->rx_wait) {
1793 list_del(&kcm->wait_rx_list);
1794 kcm->rx_wait = false;
1796 /* Move any pending receive messages to other kcm sockets */
1797 requeue_rx_msgs(mux, &sk->sk_receive_queue);
1799 spin_unlock_bh(&mux->rx_lock);
1801 if (WARN_ON(sk_rmem_alloc_get(sk)))
1802 return;
1804 /* Detach from MUX */
1805 spin_lock_bh(&mux->lock);
1807 list_del(&kcm->kcm_sock_list);
1808 mux->kcm_socks_cnt--;
1809 socks_cnt = mux->kcm_socks_cnt;
1811 spin_unlock_bh(&mux->lock);
1813 if (!socks_cnt) {
1814 /* We are done with the mux now. */
1815 release_mux(mux);
1818 WARN_ON(kcm->rx_wait);
1820 sock_put(&kcm->sk);
1823 /* Called by kcm_release to close a KCM socket.
1824 * If this is the last KCM socket on the MUX, destroy the MUX.
1826 static int kcm_release(struct socket *sock)
1828 struct sock *sk = sock->sk;
1829 struct kcm_sock *kcm;
1830 struct kcm_mux *mux;
1831 struct kcm_psock *psock;
1833 if (!sk)
1834 return 0;
1836 kcm = kcm_sk(sk);
1837 mux = kcm->mux;
1839 sock_orphan(sk);
1840 kfree_skb(kcm->seq_skb);
1842 lock_sock(sk);
1843 /* Purge queue under lock to avoid race condition with tx_work trying
1844 * to act when queue is nonempty. If tx_work runs after this point
1845 * it will just return.
1847 __skb_queue_purge(&sk->sk_write_queue);
1849 /* Set tx_stopped. This is checked when psock is bound to a kcm and we
1850 * get a writespace callback. This prevents further work being queued
1851 * from the callback (unbinding the psock occurs after canceling work.
1853 kcm->tx_stopped = 1;
1855 release_sock(sk);
1857 spin_lock_bh(&mux->lock);
1858 if (kcm->tx_wait) {
1859 /* Take of tx_wait list, after this point there should be no way
1860 * that a psock will be assigned to this kcm.
1862 list_del(&kcm->wait_psock_list);
1863 kcm->tx_wait = false;
1865 spin_unlock_bh(&mux->lock);
1867 /* Cancel work. After this point there should be no outside references
1868 * to the kcm socket.
1870 cancel_work_sync(&kcm->tx_work);
1872 lock_sock(sk);
1873 psock = kcm->tx_psock;
1874 if (psock) {
1875 /* A psock was reserved, so we need to kill it since it
1876 * may already have some bytes queued from a message. We
1877 * need to do this after removing kcm from tx_wait list.
1879 kcm_abort_tx_psock(psock, EPIPE, false);
1880 unreserve_psock(kcm);
1882 release_sock(sk);
1884 WARN_ON(kcm->tx_wait);
1885 WARN_ON(kcm->tx_psock);
1887 sock->sk = NULL;
1889 kcm_done(kcm);
1891 return 0;
1894 static const struct proto_ops kcm_dgram_ops = {
1895 .family = PF_KCM,
1896 .owner = THIS_MODULE,
1897 .release = kcm_release,
1898 .bind = sock_no_bind,
1899 .connect = sock_no_connect,
1900 .socketpair = sock_no_socketpair,
1901 .accept = sock_no_accept,
1902 .getname = sock_no_getname,
1903 .poll = datagram_poll,
1904 .ioctl = kcm_ioctl,
1905 .listen = sock_no_listen,
1906 .shutdown = sock_no_shutdown,
1907 .setsockopt = kcm_setsockopt,
1908 .getsockopt = kcm_getsockopt,
1909 .sendmsg = kcm_sendmsg,
1910 .recvmsg = kcm_recvmsg,
1911 .mmap = sock_no_mmap,
1912 .sendpage = kcm_sendpage,
1915 static const struct proto_ops kcm_seqpacket_ops = {
1916 .family = PF_KCM,
1917 .owner = THIS_MODULE,
1918 .release = kcm_release,
1919 .bind = sock_no_bind,
1920 .connect = sock_no_connect,
1921 .socketpair = sock_no_socketpair,
1922 .accept = sock_no_accept,
1923 .getname = sock_no_getname,
1924 .poll = datagram_poll,
1925 .ioctl = kcm_ioctl,
1926 .listen = sock_no_listen,
1927 .shutdown = sock_no_shutdown,
1928 .setsockopt = kcm_setsockopt,
1929 .getsockopt = kcm_getsockopt,
1930 .sendmsg = kcm_sendmsg,
1931 .recvmsg = kcm_recvmsg,
1932 .mmap = sock_no_mmap,
1933 .sendpage = kcm_sendpage,
1934 .splice_read = kcm_splice_read,
1937 /* Create proto operation for kcm sockets */
1938 static int kcm_create(struct net *net, struct socket *sock,
1939 int protocol, int kern)
1941 struct kcm_net *knet = net_generic(net, kcm_net_id);
1942 struct sock *sk;
1943 struct kcm_mux *mux;
1945 switch (sock->type) {
1946 case SOCK_DGRAM:
1947 sock->ops = &kcm_dgram_ops;
1948 break;
1949 case SOCK_SEQPACKET:
1950 sock->ops = &kcm_seqpacket_ops;
1951 break;
1952 default:
1953 return -ESOCKTNOSUPPORT;
1956 if (protocol != KCMPROTO_CONNECTED)
1957 return -EPROTONOSUPPORT;
1959 sk = sk_alloc(net, PF_KCM, GFP_KERNEL, &kcm_proto, kern);
1960 if (!sk)
1961 return -ENOMEM;
1963 /* Allocate a kcm mux, shared between KCM sockets */
1964 mux = kmem_cache_zalloc(kcm_muxp, GFP_KERNEL);
1965 if (!mux) {
1966 sk_free(sk);
1967 return -ENOMEM;
1970 spin_lock_init(&mux->lock);
1971 spin_lock_init(&mux->rx_lock);
1972 INIT_LIST_HEAD(&mux->kcm_socks);
1973 INIT_LIST_HEAD(&mux->kcm_rx_waiters);
1974 INIT_LIST_HEAD(&mux->kcm_tx_waiters);
1976 INIT_LIST_HEAD(&mux->psocks);
1977 INIT_LIST_HEAD(&mux->psocks_ready);
1978 INIT_LIST_HEAD(&mux->psocks_avail);
1980 mux->knet = knet;
1982 /* Add new MUX to list */
1983 mutex_lock(&knet->mutex);
1984 list_add_rcu(&mux->kcm_mux_list, &knet->mux_list);
1985 knet->count++;
1986 mutex_unlock(&knet->mutex);
1988 skb_queue_head_init(&mux->rx_hold_queue);
1990 /* Init KCM socket */
1991 sock_init_data(sock, sk);
1992 init_kcm_sock(kcm_sk(sk), mux);
1994 return 0;
1997 static const struct net_proto_family kcm_family_ops = {
1998 .family = PF_KCM,
1999 .create = kcm_create,
2000 .owner = THIS_MODULE,
2003 static __net_init int kcm_init_net(struct net *net)
2005 struct kcm_net *knet = net_generic(net, kcm_net_id);
2007 INIT_LIST_HEAD_RCU(&knet->mux_list);
2008 mutex_init(&knet->mutex);
2010 return 0;
2013 static __net_exit void kcm_exit_net(struct net *net)
2015 struct kcm_net *knet = net_generic(net, kcm_net_id);
2017 /* All KCM sockets should be closed at this point, which should mean
2018 * that all multiplexors and psocks have been destroyed.
2020 WARN_ON(!list_empty(&knet->mux_list));
2023 static struct pernet_operations kcm_net_ops = {
2024 .init = kcm_init_net,
2025 .exit = kcm_exit_net,
2026 .id = &kcm_net_id,
2027 .size = sizeof(struct kcm_net),
2030 static int __init kcm_init(void)
2032 int err = -ENOMEM;
2034 kcm_muxp = kmem_cache_create("kcm_mux_cache",
2035 sizeof(struct kcm_mux), 0,
2036 SLAB_HWCACHE_ALIGN, NULL);
2037 if (!kcm_muxp)
2038 goto fail;
2040 kcm_psockp = kmem_cache_create("kcm_psock_cache",
2041 sizeof(struct kcm_psock), 0,
2042 SLAB_HWCACHE_ALIGN, NULL);
2043 if (!kcm_psockp)
2044 goto fail;
2046 kcm_wq = create_singlethread_workqueue("kkcmd");
2047 if (!kcm_wq)
2048 goto fail;
2050 err = proto_register(&kcm_proto, 1);
2051 if (err)
2052 goto fail;
2054 err = register_pernet_device(&kcm_net_ops);
2055 if (err)
2056 goto net_ops_fail;
2058 err = sock_register(&kcm_family_ops);
2059 if (err)
2060 goto sock_register_fail;
2062 err = kcm_proc_init();
2063 if (err)
2064 goto proc_init_fail;
2066 return 0;
2068 proc_init_fail:
2069 sock_unregister(PF_KCM);
2071 sock_register_fail:
2072 unregister_pernet_device(&kcm_net_ops);
2074 net_ops_fail:
2075 proto_unregister(&kcm_proto);
2077 fail:
2078 kmem_cache_destroy(kcm_muxp);
2079 kmem_cache_destroy(kcm_psockp);
2081 if (kcm_wq)
2082 destroy_workqueue(kcm_wq);
2084 return err;
2087 static void __exit kcm_exit(void)
2089 kcm_proc_exit();
2090 sock_unregister(PF_KCM);
2091 unregister_pernet_device(&kcm_net_ops);
2092 proto_unregister(&kcm_proto);
2093 destroy_workqueue(kcm_wq);
2095 kmem_cache_destroy(kcm_muxp);
2096 kmem_cache_destroy(kcm_psockp);
2099 module_init(kcm_init);
2100 module_exit(kcm_exit);
2102 MODULE_LICENSE("GPL");
2103 MODULE_ALIAS_NETPROTO(PF_KCM);