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