xref: /linux/include/linux/skmsg.h (revision 12c45473dfe7b5438d253e67bc7bdf2bb98e3108)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3 
4 #ifndef _LINUX_SKMSG_H
5 #define _LINUX_SKMSG_H
6 
7 #include <linux/bitops.h>
8 #include <linux/bpf.h>
9 #include <linux/filter.h>
10 #include <linux/scatterlist.h>
11 #include <linux/skbuff.h>
12 
13 #include <net/sock.h>
14 #include <net/tcp.h>
15 #include <net/strparser.h>
16 
17 #define MAX_MSG_FRAGS			MAX_SKB_FRAGS
18 #define NR_MSG_FRAG_IDS			(MAX_MSG_FRAGS + 1)
19 
20 enum __sk_action {
21 	__SK_DROP = 0,
22 	__SK_PASS,
23 	__SK_REDIRECT,
24 	__SK_NONE,
25 };
26 
27 struct sk_msg_sg {
28 	u32				start;
29 	u32				curr;
30 	u32				end;
31 	u32				size;
32 	u32				copybreak;
33 	DECLARE_BITMAP(copy, MAX_MSG_FRAGS + 2);
34 	/* The extra two elements:
35 	 * 1) used for chaining the front and sections when the list becomes
36 	 *    partitioned (e.g. end < start). The crypto APIs require the
37 	 *    chaining;
38 	 * 2) to chain tailer SG entries after the message.
39 	 */
40 	struct scatterlist		data[MAX_MSG_FRAGS + 2];
41 };
42 
43 /* UAPI in filter.c depends on struct sk_msg_sg being first element. */
44 struct sk_msg {
45 	struct sk_msg_sg		sg;
46 	void				*data;
47 	void				*data_end;
48 	u32				apply_bytes;
49 	u32				cork_bytes;
50 	u32				flags;
51 	struct sk_buff			*skb;
52 	struct sock			*sk_redir;
53 	struct sock			*sk;
54 	struct list_head		list;
55 };
56 
57 struct sk_psock_progs {
58 	struct bpf_prog			*msg_parser;
59 	struct bpf_prog			*stream_parser;
60 	struct bpf_prog			*stream_verdict;
61 	struct bpf_prog			*skb_verdict;
62 	struct bpf_link			*msg_parser_link;
63 	struct bpf_link			*stream_parser_link;
64 	struct bpf_link			*stream_verdict_link;
65 	struct bpf_link			*skb_verdict_link;
66 };
67 
68 enum sk_psock_state_bits {
69 	SK_PSOCK_TX_ENABLED,
70 	SK_PSOCK_RX_STRP_ENABLED,
71 };
72 
73 struct sk_psock_link {
74 	struct list_head		list;
75 	struct bpf_map			*map;
76 	void				*link_raw;
77 };
78 
79 struct sk_psock_work_state {
80 	u32				len;
81 	u32				off;
82 };
83 
84 struct sk_psock {
85 	struct sock			*sk;
86 	struct sock			*sk_redir;
87 	u32				apply_bytes;
88 	u32				cork_bytes;
89 	u32				eval;
90 	bool				redir_ingress; /* undefined if sk_redir is null */
91 	struct sk_msg			*cork;
92 	struct sk_psock_progs		progs;
93 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
94 	struct strparser		strp;
95 	u32				copied_seq;
96 	u32				ingress_bytes;
97 #endif
98 	struct sk_buff_head		ingress_skb;
99 	struct list_head		ingress_msg;
100 	spinlock_t			ingress_lock;
101 	/** @msg_tot_len: Total bytes queued in ingress_msg list. */
102 	u32				msg_tot_len;
103 	unsigned long			state;
104 	struct list_head		link;
105 	spinlock_t			link_lock;
106 	refcount_t			refcnt;
107 	void (*saved_unhash)(struct sock *sk);
108 	void (*saved_destroy)(struct sock *sk);
109 	void (*saved_close)(struct sock *sk, long timeout);
110 	void (*saved_write_space)(struct sock *sk);
111 	void (*saved_data_ready)(struct sock *sk);
112 	/* psock_update_sk_prot may be called with restore=false many times
113 	 * so the handler must be safe for this case. It will be called
114 	 * exactly once with restore=true when the psock is being destroyed
115 	 * and psock refcnt is zero, but before an RCU grace period.
116 	 */
117 	int  (*psock_update_sk_prot)(struct sock *sk, struct sk_psock *psock,
118 				     bool restore);
119 	struct proto			*sk_proto;
120 	struct mutex			work_mutex;
121 	struct sk_psock_work_state	work_state;
122 	struct delayed_work		work;
123 	struct sock			*sk_pair;
124 	struct rcu_work			rwork;
125 };
126 
127 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
128 		 int elem_first_coalesce);
129 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
130 		 u32 off, u32 len);
131 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
132 int sk_msg_free(struct sock *sk, struct sk_msg *msg);
133 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
134 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
135 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
136 				  u32 bytes);
137 
138 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
139 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
140 
141 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
142 			      struct sk_msg *msg, u32 bytes);
143 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
144 			     struct sk_msg *msg, u32 bytes);
145 int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
146 		   int len, int flags);
147 int __sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
148 		     int len, int flags, int *copied_from_self);
149 bool sk_msg_is_readable(struct sock *sk);
150 
151 static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
152 {
153 	WARN_ON(i == msg->sg.end && bytes);
154 }
155 
156 static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
157 {
158 	if (psock->apply_bytes) {
159 		if (psock->apply_bytes < bytes)
160 			psock->apply_bytes = 0;
161 		else
162 			psock->apply_bytes -= bytes;
163 	}
164 }
165 
166 static inline u32 sk_msg_iter_dist(u32 start, u32 end)
167 {
168 	return end >= start ? end - start : end + (NR_MSG_FRAG_IDS - start);
169 }
170 
171 #define sk_msg_iter_var_prev(var)			\
172 	do {						\
173 		if (var == 0)				\
174 			var = NR_MSG_FRAG_IDS - 1;	\
175 		else					\
176 			var--;				\
177 	} while (0)
178 
179 #define sk_msg_iter_var_next(var)			\
180 	do {						\
181 		var++;					\
182 		if (var == NR_MSG_FRAG_IDS)		\
183 			var = 0;			\
184 	} while (0)
185 
186 #define sk_msg_iter_prev(msg, which)			\
187 	sk_msg_iter_var_prev(msg->sg.which)
188 
189 #define sk_msg_iter_next(msg, which)			\
190 	sk_msg_iter_var_next(msg->sg.which)
191 
192 static inline void sk_msg_init(struct sk_msg *msg)
193 {
194 	BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != NR_MSG_FRAG_IDS);
195 	memset(msg, 0, sizeof(*msg));
196 	sg_init_marker(msg->sg.data, NR_MSG_FRAG_IDS);
197 }
198 
199 /*
200  * sk_msg_memcopy_from_iter() writes to byte sg.copybreak of element sg.curr,
201  * counted from the offset of that element. Point both past the last byte of
202  * the message, after something other than a copy has changed its elements.
203  */
204 static inline void sk_msg_reset_curr(struct sk_msg *msg)
205 {
206 	if (!msg->sg.size) {
207 		msg->sg.curr = msg->sg.start;
208 		msg->sg.copybreak = 0;
209 	} else {
210 		u32 i = msg->sg.end;
211 
212 		sk_msg_iter_var_prev(i);
213 		msg->sg.curr = i;
214 		msg->sg.copybreak = msg->sg.data[i].length;
215 	}
216 }
217 
218 static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
219 			       int which, u32 size)
220 {
221 	dst->sg.data[which] = src->sg.data[which];
222 	__assign_bit(which, dst->sg.copy, test_bit(which, src->sg.copy));
223 	dst->sg.data[which].length  = size;
224 	dst->sg.size		   += size;
225 	src->sg.size		   -= size;
226 	src->sg.data[which].length -= size;
227 	src->sg.data[which].offset += size;
228 	if (!src->sg.data[which].length)
229 		__clear_bit(which, src->sg.copy);
230 }
231 
232 static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
233 {
234 	memcpy(dst, src, sizeof(*src));
235 	sk_msg_init(src);
236 }
237 
238 static inline bool sk_msg_full(const struct sk_msg *msg)
239 {
240 	return sk_msg_iter_dist(msg->sg.start, msg->sg.end) == MAX_MSG_FRAGS;
241 }
242 
243 static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
244 {
245 	return sk_msg_iter_dist(msg->sg.start, msg->sg.end);
246 }
247 
248 static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
249 {
250 	return &msg->sg.data[which];
251 }
252 
253 static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which)
254 {
255 	return msg->sg.data[which];
256 }
257 
258 static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
259 {
260 	return sg_page(sk_msg_elem(msg, which));
261 }
262 
263 static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
264 {
265 	return msg->flags & BPF_F_INGRESS;
266 }
267 
268 static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
269 {
270 	struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
271 
272 	if (test_bit(msg->sg.start, msg->sg.copy)) {
273 		msg->data = NULL;
274 		msg->data_end = NULL;
275 	} else {
276 		msg->data = sg_virt(sge);
277 		msg->data_end = msg->data + sge->length;
278 	}
279 }
280 
281 static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
282 				   u32 len, u32 offset)
283 {
284 	struct scatterlist *sge;
285 
286 	get_page(page);
287 	sge = sk_msg_elem(msg, msg->sg.end);
288 	sg_set_page(sge, page, len, offset);
289 	sg_unmark_end(sge);
290 
291 	__set_bit(msg->sg.end, msg->sg.copy);
292 	msg->sg.size += len;
293 	sk_msg_iter_next(msg, end);
294 }
295 
296 static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
297 {
298 	do {
299 		__assign_bit(i, msg->sg.copy, copy_state);
300 		sk_msg_iter_var_next(i);
301 		if (i == msg->sg.end)
302 			break;
303 	} while (1);
304 }
305 
306 static inline void sk_msg_sg_copy_assign(struct sk_msg *dst, u32 dst_i,
307 					 const struct sk_msg *src, u32 src_i)
308 {
309 	__assign_bit(dst_i, dst->sg.copy, test_bit(src_i, src->sg.copy));
310 }
311 
312 static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
313 {
314 	sk_msg_sg_copy(msg, start, true);
315 }
316 
317 static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
318 {
319 	sk_msg_sg_copy(msg, start, false);
320 }
321 
322 static inline struct sk_psock *sk_psock(const struct sock *sk)
323 {
324 	return __rcu_dereference_sk_user_data_with_flags(sk,
325 							 SK_USER_DATA_PSOCK);
326 }
327 
328 static inline void sk_psock_set_state(struct sk_psock *psock,
329 				      enum sk_psock_state_bits bit)
330 {
331 	set_bit(bit, &psock->state);
332 }
333 
334 static inline void sk_psock_clear_state(struct sk_psock *psock,
335 					enum sk_psock_state_bits bit)
336 {
337 	clear_bit(bit, &psock->state);
338 }
339 
340 static inline bool sk_psock_test_state(const struct sk_psock *psock,
341 				       enum sk_psock_state_bits bit)
342 {
343 	return test_bit(bit, &psock->state);
344 }
345 
346 static inline void sock_drop(struct sock *sk, struct sk_buff *skb)
347 {
348 	sk_drops_skbadd(sk, skb);
349 	kfree_skb(skb);
350 }
351 
352 static inline u32 sk_psock_get_msg_len_nolock(struct sk_psock *psock)
353 {
354 	/* Used by ioctl to read msg_tot_len only; lock-free for performance */
355 	return READ_ONCE(psock->msg_tot_len);
356 }
357 
358 static inline void sk_psock_msg_len_add_locked(struct sk_psock *psock, int diff)
359 {
360 	/* Use WRITE_ONCE to ensure correct read in sk_psock_get_msg_len_nolock().
361 	 * ingress_lock should be held to prevent concurrent updates to msg_tot_len
362 	 */
363 	WRITE_ONCE(psock->msg_tot_len, psock->msg_tot_len + diff);
364 }
365 
366 static inline void sk_psock_msg_len_add(struct sk_psock *psock, int diff)
367 {
368 	spin_lock_bh(&psock->ingress_lock);
369 	sk_psock_msg_len_add_locked(psock, diff);
370 	spin_unlock_bh(&psock->ingress_lock);
371 }
372 
373 static inline bool sk_psock_queue_msg(struct sk_psock *psock,
374 				      struct sk_msg *msg)
375 {
376 	bool ret;
377 
378 	spin_lock_bh(&psock->ingress_lock);
379 	if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
380 		list_add_tail(&msg->list, &psock->ingress_msg);
381 		sk_psock_msg_len_add_locked(psock, msg->sg.size);
382 		ret = true;
383 	} else {
384 		sk_msg_free(psock->sk, msg);
385 		kfree(msg);
386 		ret = false;
387 	}
388 	spin_unlock_bh(&psock->ingress_lock);
389 	return ret;
390 }
391 
392 static inline struct sk_msg *sk_psock_dequeue_msg(struct sk_psock *psock)
393 {
394 	struct sk_msg *msg;
395 
396 	spin_lock_bh(&psock->ingress_lock);
397 	msg = list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list);
398 	if (msg) {
399 		list_del(&msg->list);
400 		sk_psock_msg_len_add_locked(psock, -msg->sg.size);
401 	}
402 	spin_unlock_bh(&psock->ingress_lock);
403 	return msg;
404 }
405 
406 static inline struct sk_msg *sk_psock_peek_msg_locked(struct sk_psock *psock)
407 {
408 	return list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list);
409 }
410 
411 static inline struct sk_msg *sk_psock_peek_msg(struct sk_psock *psock)
412 {
413 	struct sk_msg *msg;
414 
415 	spin_lock_bh(&psock->ingress_lock);
416 	msg = sk_psock_peek_msg_locked(psock);
417 	spin_unlock_bh(&psock->ingress_lock);
418 	return msg;
419 }
420 
421 static inline struct sk_msg *sk_psock_next_msg(struct sk_psock *psock,
422 					       struct sk_msg *msg)
423 {
424 	struct sk_msg *ret;
425 
426 	spin_lock_bh(&psock->ingress_lock);
427 	if (list_is_last(&msg->list, &psock->ingress_msg))
428 		ret = NULL;
429 	else
430 		ret = list_next_entry(msg, list);
431 	spin_unlock_bh(&psock->ingress_lock);
432 	return ret;
433 }
434 
435 static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
436 {
437 	return psock ? list_empty(&psock->ingress_msg) : true;
438 }
439 
440 static inline void kfree_sk_msg(struct sk_msg *msg)
441 {
442 	if (msg->skb)
443 		consume_skb(msg->skb);
444 	kfree(msg);
445 }
446 
447 static inline void sk_psock_report_error(struct sk_psock *psock, int err)
448 {
449 	struct sock *sk = psock->sk;
450 
451 	sk->sk_err = err;
452 	sk_error_report(sk);
453 }
454 
455 struct sk_psock *sk_psock_init(struct sock *sk, int node);
456 void sk_psock_stop(struct sk_psock *psock);
457 
458 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
459 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
460 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
461 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
462 #else
463 static inline int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
464 {
465 	return -EOPNOTSUPP;
466 }
467 
468 static inline void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
469 {
470 }
471 
472 static inline void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
473 {
474 }
475 #endif
476 
477 void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock);
478 void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock);
479 
480 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
481 			 struct sk_msg *msg);
482 
483 /*
484  * This specialized allocator has to be a macro for its allocations to be
485  * accounted separately (to have a separate alloc_tag). The typecast is
486  * intentional to enforce typesafety.
487  */
488 #define sk_psock_init_link()	\
489 		kzalloc_obj(struct sk_psock_link, GFP_ATOMIC | __GFP_NOWARN)
490 
491 static inline void sk_psock_free_link(struct sk_psock_link *link)
492 {
493 	kfree(link);
494 }
495 
496 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
497 
498 static inline void sk_psock_cork_free(struct sk_psock *psock)
499 {
500 	if (psock->cork) {
501 		sk_msg_free(psock->sk, psock->cork);
502 		kfree(psock->cork);
503 		psock->cork = NULL;
504 	}
505 }
506 
507 static inline void sk_psock_restore_proto(struct sock *sk,
508 					  struct sk_psock *psock)
509 {
510 	if (psock->psock_update_sk_prot)
511 		psock->psock_update_sk_prot(sk, psock, true);
512 }
513 
514 static inline struct sk_psock *sk_psock_get(struct sock *sk)
515 {
516 	struct sk_psock *psock;
517 
518 	rcu_read_lock();
519 	psock = sk_psock(sk);
520 	if (psock && !refcount_inc_not_zero(&psock->refcnt))
521 		psock = NULL;
522 	rcu_read_unlock();
523 	return psock;
524 }
525 
526 void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
527 
528 static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
529 {
530 	if (refcount_dec_and_test(&psock->refcnt))
531 		sk_psock_drop(sk, psock);
532 }
533 
534 static inline void sk_psock_data_ready(struct sock *sk, struct sk_psock *psock)
535 {
536 	read_lock_bh(&sk->sk_callback_lock);
537 	if (psock->saved_data_ready)
538 		psock->saved_data_ready(sk);
539 	else
540 		sk->sk_data_ready(sk);
541 	read_unlock_bh(&sk->sk_callback_lock);
542 }
543 
544 static inline void psock_set_prog(struct bpf_prog **pprog,
545 				  struct bpf_prog *prog)
546 {
547 	prog = xchg(pprog, prog);
548 	if (prog)
549 		bpf_prog_put(prog);
550 }
551 
552 static inline int psock_replace_prog(struct bpf_prog **pprog,
553 				     struct bpf_prog *prog,
554 				     struct bpf_prog *old)
555 {
556 	if (cmpxchg(pprog, old, prog) != old)
557 		return -ENOENT;
558 
559 	if (old)
560 		bpf_prog_put(old);
561 
562 	return 0;
563 }
564 
565 static inline void psock_progs_drop(struct sk_psock_progs *progs)
566 {
567 	psock_set_prog(&progs->msg_parser, NULL);
568 	psock_set_prog(&progs->stream_parser, NULL);
569 	psock_set_prog(&progs->stream_verdict, NULL);
570 	psock_set_prog(&progs->skb_verdict, NULL);
571 }
572 
573 /* for udp only, sk is not locked */
574 static inline ssize_t sk_msg_first_len(struct sock *sk)
575 {
576 	struct sk_psock *psock;
577 	struct sk_msg *msg;
578 	ssize_t inq = 0;
579 
580 	psock = sk_psock_get(sk);
581 	if (likely(psock)) {
582 		spin_lock_bh(&psock->ingress_lock);
583 		msg = sk_psock_peek_msg_locked(psock);
584 		if (msg)
585 			inq = msg->sg.size;
586 		spin_unlock_bh(&psock->ingress_lock);
587 		sk_psock_put(sk, psock);
588 	}
589 	return inq;
590 }
591 
592 #if IS_ENABLED(CONFIG_NET_SOCK_MSG)
593 
594 #define BPF_F_STRPARSER	(1UL << 1)
595 
596 /* We only have two bits so far. */
597 #define BPF_F_PTR_MASK ~(BPF_F_INGRESS | BPF_F_STRPARSER)
598 
599 static inline bool skb_bpf_strparser(const struct sk_buff *skb)
600 {
601 	unsigned long sk_redir = skb->_sk_redir;
602 
603 	return sk_redir & BPF_F_STRPARSER;
604 }
605 
606 static inline void skb_bpf_set_strparser(struct sk_buff *skb)
607 {
608 	skb->_sk_redir |= BPF_F_STRPARSER;
609 }
610 
611 static inline bool skb_bpf_ingress(const struct sk_buff *skb)
612 {
613 	unsigned long sk_redir = skb->_sk_redir;
614 
615 	return sk_redir & BPF_F_INGRESS;
616 }
617 
618 static inline void skb_bpf_set_ingress(struct sk_buff *skb)
619 {
620 	skb->_sk_redir |= BPF_F_INGRESS;
621 }
622 
623 static inline void skb_bpf_set_redir(struct sk_buff *skb, struct sock *sk_redir,
624 				     bool ingress)
625 {
626 	skb->_sk_redir = (unsigned long)sk_redir;
627 	if (ingress)
628 		skb->_sk_redir |= BPF_F_INGRESS;
629 }
630 
631 static inline struct sock *skb_bpf_redirect_fetch(const struct sk_buff *skb)
632 {
633 	unsigned long sk_redir = skb->_sk_redir;
634 
635 	return (struct sock *)(sk_redir & BPF_F_PTR_MASK);
636 }
637 
638 static inline void skb_bpf_redirect_clear(struct sk_buff *skb)
639 {
640 	skb->_sk_redir = 0;
641 }
642 #endif /* CONFIG_NET_SOCK_MSG */
643 #endif /* _LINUX_SKMSG_H */
644