Commit | Line | Data |
---|---|---|
604326b4 DB |
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/bpf.h> | |
8 | #include <linux/filter.h> | |
9 | #include <linux/scatterlist.h> | |
10 | #include <linux/skbuff.h> | |
11 | ||
12 | #include <net/sock.h> | |
13 | #include <net/tcp.h> | |
14 | #include <net/strparser.h> | |
15 | ||
16 | #define MAX_MSG_FRAGS MAX_SKB_FRAGS | |
031097d9 | 17 | #define NR_MSG_FRAG_IDS (MAX_MSG_FRAGS + 1) |
604326b4 DB |
18 | |
19 | enum __sk_action { | |
20 | __SK_DROP = 0, | |
21 | __SK_PASS, | |
22 | __SK_REDIRECT, | |
23 | __SK_NONE, | |
24 | }; | |
25 | ||
26 | struct sk_msg_sg { | |
27 | u32 start; | |
28 | u32 curr; | |
29 | u32 end; | |
30 | u32 size; | |
31 | u32 copybreak; | |
163ab96b | 32 | unsigned long copy; |
031097d9 JK |
33 | /* The extra two elements: |
34 | * 1) used for chaining the front and sections when the list becomes | |
35 | * partitioned (e.g. end < start). The crypto APIs require the | |
36 | * chaining; | |
37 | * 2) to chain tailer SG entries after the message. | |
d3b18ad3 | 38 | */ |
031097d9 | 39 | struct scatterlist data[MAX_MSG_FRAGS + 2]; |
604326b4 | 40 | }; |
031097d9 | 41 | static_assert(BITS_PER_LONG >= NR_MSG_FRAG_IDS); |
604326b4 | 42 | |
7a69c0f2 | 43 | /* UAPI in filter.c depends on struct sk_msg_sg being first element. */ |
604326b4 DB |
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; | |
ae8b8332 CW |
59 | struct bpf_prog *stream_parser; |
60 | struct bpf_prog *stream_verdict; | |
604326b4 DB |
61 | }; |
62 | ||
63 | enum sk_psock_state_bits { | |
64 | SK_PSOCK_TX_ENABLED, | |
65 | }; | |
66 | ||
67 | struct sk_psock_link { | |
68 | struct list_head list; | |
69 | struct bpf_map *map; | |
70 | void *link_raw; | |
71 | }; | |
72 | ||
604326b4 DB |
73 | struct sk_psock_work_state { |
74 | struct sk_buff *skb; | |
75 | u32 len; | |
76 | u32 off; | |
77 | }; | |
78 | ||
79 | struct sk_psock { | |
80 | struct sock *sk; | |
81 | struct sock *sk_redir; | |
82 | u32 apply_bytes; | |
83 | u32 cork_bytes; | |
84 | u32 eval; | |
85 | struct sk_msg *cork; | |
86 | struct sk_psock_progs progs; | |
5a685cd9 CW |
87 | #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) |
88 | struct strparser strp; | |
89 | #endif | |
604326b4 DB |
90 | struct sk_buff_head ingress_skb; |
91 | struct list_head ingress_msg; | |
b01fd6e8 | 92 | spinlock_t ingress_lock; |
604326b4 DB |
93 | unsigned long state; |
94 | struct list_head link; | |
95 | spinlock_t link_lock; | |
96 | refcount_t refcnt; | |
97 | void (*saved_unhash)(struct sock *sk); | |
98 | void (*saved_close)(struct sock *sk, long timeout); | |
99 | void (*saved_write_space)(struct sock *sk); | |
5a685cd9 | 100 | void (*saved_data_ready)(struct sock *sk); |
604326b4 | 101 | struct proto *sk_proto; |
799aa7f9 | 102 | struct mutex work_mutex; |
604326b4 DB |
103 | struct sk_psock_work_state work_state; |
104 | struct work_struct work; | |
105 | union { | |
106 | struct rcu_head rcu; | |
107 | struct work_struct gc; | |
108 | }; | |
109 | }; | |
110 | ||
111 | int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len, | |
112 | int elem_first_coalesce); | |
d829e9c4 DB |
113 | int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src, |
114 | u32 off, u32 len); | |
604326b4 DB |
115 | void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len); |
116 | int sk_msg_free(struct sock *sk, struct sk_msg *msg); | |
117 | int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg); | |
118 | void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes); | |
119 | void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg, | |
120 | u32 bytes); | |
121 | ||
122 | void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes); | |
d3b18ad3 | 123 | void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes); |
604326b4 DB |
124 | |
125 | int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from, | |
126 | struct sk_msg *msg, u32 bytes); | |
127 | int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from, | |
128 | struct sk_msg *msg, u32 bytes); | |
129 | ||
130 | static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes) | |
131 | { | |
132 | WARN_ON(i == msg->sg.end && bytes); | |
133 | } | |
134 | ||
135 | static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes) | |
136 | { | |
137 | if (psock->apply_bytes) { | |
138 | if (psock->apply_bytes < bytes) | |
139 | psock->apply_bytes = 0; | |
140 | else | |
141 | psock->apply_bytes -= bytes; | |
142 | } | |
143 | } | |
144 | ||
683916f6 JK |
145 | static inline u32 sk_msg_iter_dist(u32 start, u32 end) |
146 | { | |
031097d9 | 147 | return end >= start ? end - start : end + (NR_MSG_FRAG_IDS - start); |
683916f6 JK |
148 | } |
149 | ||
604326b4 DB |
150 | #define sk_msg_iter_var_prev(var) \ |
151 | do { \ | |
152 | if (var == 0) \ | |
031097d9 | 153 | var = NR_MSG_FRAG_IDS - 1; \ |
604326b4 DB |
154 | else \ |
155 | var--; \ | |
156 | } while (0) | |
157 | ||
158 | #define sk_msg_iter_var_next(var) \ | |
159 | do { \ | |
160 | var++; \ | |
031097d9 | 161 | if (var == NR_MSG_FRAG_IDS) \ |
604326b4 DB |
162 | var = 0; \ |
163 | } while (0) | |
164 | ||
165 | #define sk_msg_iter_prev(msg, which) \ | |
166 | sk_msg_iter_var_prev(msg->sg.which) | |
167 | ||
168 | #define sk_msg_iter_next(msg, which) \ | |
169 | sk_msg_iter_var_next(msg->sg.which) | |
170 | ||
171 | static inline void sk_msg_clear_meta(struct sk_msg *msg) | |
172 | { | |
173 | memset(&msg->sg, 0, offsetofend(struct sk_msg_sg, copy)); | |
174 | } | |
175 | ||
176 | static inline void sk_msg_init(struct sk_msg *msg) | |
177 | { | |
031097d9 | 178 | BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != NR_MSG_FRAG_IDS); |
604326b4 | 179 | memset(msg, 0, sizeof(*msg)); |
031097d9 | 180 | sg_init_marker(msg->sg.data, NR_MSG_FRAG_IDS); |
604326b4 DB |
181 | } |
182 | ||
183 | static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src, | |
184 | int which, u32 size) | |
185 | { | |
186 | dst->sg.data[which] = src->sg.data[which]; | |
187 | dst->sg.data[which].length = size; | |
3f4c3127 | 188 | dst->sg.size += size; |
81aabbb9 | 189 | src->sg.size -= size; |
604326b4 DB |
190 | src->sg.data[which].length -= size; |
191 | src->sg.data[which].offset += size; | |
192 | } | |
193 | ||
d3b18ad3 JF |
194 | static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src) |
195 | { | |
196 | memcpy(dst, src, sizeof(*src)); | |
197 | sk_msg_init(src); | |
198 | } | |
199 | ||
8734a162 JF |
200 | static inline bool sk_msg_full(const struct sk_msg *msg) |
201 | { | |
031097d9 | 202 | return sk_msg_iter_dist(msg->sg.start, msg->sg.end) == MAX_MSG_FRAGS; |
8734a162 JF |
203 | } |
204 | ||
604326b4 DB |
205 | static inline u32 sk_msg_elem_used(const struct sk_msg *msg) |
206 | { | |
683916f6 | 207 | return sk_msg_iter_dist(msg->sg.start, msg->sg.end); |
604326b4 DB |
208 | } |
209 | ||
604326b4 DB |
210 | static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which) |
211 | { | |
212 | return &msg->sg.data[which]; | |
213 | } | |
214 | ||
6fff607e JF |
215 | static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which) |
216 | { | |
217 | return msg->sg.data[which]; | |
218 | } | |
219 | ||
604326b4 DB |
220 | static inline struct page *sk_msg_page(struct sk_msg *msg, int which) |
221 | { | |
222 | return sg_page(sk_msg_elem(msg, which)); | |
223 | } | |
224 | ||
225 | static inline bool sk_msg_to_ingress(const struct sk_msg *msg) | |
226 | { | |
227 | return msg->flags & BPF_F_INGRESS; | |
228 | } | |
229 | ||
230 | static inline void sk_msg_compute_data_pointers(struct sk_msg *msg) | |
231 | { | |
232 | struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start); | |
233 | ||
163ab96b | 234 | if (test_bit(msg->sg.start, &msg->sg.copy)) { |
604326b4 DB |
235 | msg->data = NULL; |
236 | msg->data_end = NULL; | |
237 | } else { | |
238 | msg->data = sg_virt(sge); | |
239 | msg->data_end = msg->data + sge->length; | |
240 | } | |
241 | } | |
242 | ||
243 | static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page, | |
244 | u32 len, u32 offset) | |
245 | { | |
246 | struct scatterlist *sge; | |
247 | ||
248 | get_page(page); | |
249 | sge = sk_msg_elem(msg, msg->sg.end); | |
250 | sg_set_page(sge, page, len, offset); | |
251 | sg_unmark_end(sge); | |
252 | ||
163ab96b | 253 | __set_bit(msg->sg.end, &msg->sg.copy); |
604326b4 DB |
254 | msg->sg.size += len; |
255 | sk_msg_iter_next(msg, end); | |
256 | } | |
257 | ||
d3b18ad3 JF |
258 | static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state) |
259 | { | |
260 | do { | |
163ab96b JK |
261 | if (copy_state) |
262 | __set_bit(i, &msg->sg.copy); | |
263 | else | |
264 | __clear_bit(i, &msg->sg.copy); | |
d3b18ad3 JF |
265 | sk_msg_iter_var_next(i); |
266 | if (i == msg->sg.end) | |
267 | break; | |
268 | } while (1); | |
269 | } | |
270 | ||
271 | static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start) | |
272 | { | |
273 | sk_msg_sg_copy(msg, start, true); | |
274 | } | |
275 | ||
276 | static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start) | |
277 | { | |
278 | sk_msg_sg_copy(msg, start, false); | |
279 | } | |
280 | ||
604326b4 DB |
281 | static inline struct sk_psock *sk_psock(const struct sock *sk) |
282 | { | |
283 | return rcu_dereference_sk_user_data(sk); | |
284 | } | |
285 | ||
604326b4 DB |
286 | static inline void sk_psock_queue_msg(struct sk_psock *psock, |
287 | struct sk_msg *msg) | |
288 | { | |
b01fd6e8 | 289 | spin_lock_bh(&psock->ingress_lock); |
604326b4 | 290 | list_add_tail(&msg->list, &psock->ingress_msg); |
b01fd6e8 CW |
291 | spin_unlock_bh(&psock->ingress_lock); |
292 | } | |
293 | ||
294 | static inline struct sk_msg *sk_psock_dequeue_msg(struct sk_psock *psock) | |
295 | { | |
296 | struct sk_msg *msg; | |
297 | ||
298 | spin_lock_bh(&psock->ingress_lock); | |
299 | msg = list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list); | |
300 | if (msg) | |
301 | list_del(&msg->list); | |
302 | spin_unlock_bh(&psock->ingress_lock); | |
303 | return msg; | |
304 | } | |
305 | ||
306 | static inline struct sk_msg *sk_psock_peek_msg(struct sk_psock *psock) | |
307 | { | |
308 | struct sk_msg *msg; | |
309 | ||
310 | spin_lock_bh(&psock->ingress_lock); | |
311 | msg = list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list); | |
312 | spin_unlock_bh(&psock->ingress_lock); | |
313 | return msg; | |
314 | } | |
315 | ||
316 | static inline struct sk_msg *sk_psock_next_msg(struct sk_psock *psock, | |
317 | struct sk_msg *msg) | |
318 | { | |
319 | struct sk_msg *ret; | |
320 | ||
321 | spin_lock_bh(&psock->ingress_lock); | |
322 | if (list_is_last(&msg->list, &psock->ingress_msg)) | |
323 | ret = NULL; | |
324 | else | |
325 | ret = list_next_entry(msg, list); | |
326 | spin_unlock_bh(&psock->ingress_lock); | |
327 | return ret; | |
604326b4 DB |
328 | } |
329 | ||
d3b18ad3 JF |
330 | static inline bool sk_psock_queue_empty(const struct sk_psock *psock) |
331 | { | |
332 | return psock ? list_empty(&psock->ingress_msg) : true; | |
333 | } | |
334 | ||
b01fd6e8 CW |
335 | static inline void kfree_sk_msg(struct sk_msg *msg) |
336 | { | |
337 | if (msg->skb) | |
338 | consume_skb(msg->skb); | |
339 | kfree(msg); | |
340 | } | |
341 | ||
604326b4 DB |
342 | static inline void sk_psock_report_error(struct sk_psock *psock, int err) |
343 | { | |
344 | struct sock *sk = psock->sk; | |
345 | ||
346 | sk->sk_err = err; | |
347 | sk->sk_error_report(sk); | |
348 | } | |
349 | ||
350 | struct sk_psock *sk_psock_init(struct sock *sk, int node); | |
799aa7f9 | 351 | void sk_psock_stop(struct sk_psock *psock, bool wait); |
604326b4 | 352 | |
88759609 | 353 | #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) |
604326b4 DB |
354 | int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock); |
355 | void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock); | |
356 | void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock); | |
88759609 CW |
357 | #else |
358 | static inline int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock) | |
359 | { | |
360 | return -EOPNOTSUPP; | |
361 | } | |
362 | ||
363 | static inline void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock) | |
364 | { | |
365 | } | |
366 | ||
367 | static inline void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock) | |
368 | { | |
369 | } | |
370 | #endif | |
371 | ||
ef565928 JF |
372 | void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock); |
373 | void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock); | |
604326b4 DB |
374 | |
375 | int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock, | |
376 | struct sk_msg *msg); | |
377 | ||
378 | static inline struct sk_psock_link *sk_psock_init_link(void) | |
379 | { | |
380 | return kzalloc(sizeof(struct sk_psock_link), | |
381 | GFP_ATOMIC | __GFP_NOWARN); | |
382 | } | |
383 | ||
384 | static inline void sk_psock_free_link(struct sk_psock_link *link) | |
385 | { | |
386 | kfree(link); | |
387 | } | |
388 | ||
389 | struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock); | |
604326b4 | 390 | |
604326b4 DB |
391 | static inline void sk_psock_cork_free(struct sk_psock *psock) |
392 | { | |
393 | if (psock->cork) { | |
394 | sk_msg_free(psock->sk, psock->cork); | |
395 | kfree(psock->cork); | |
396 | psock->cork = NULL; | |
397 | } | |
398 | } | |
399 | ||
400 | static inline void sk_psock_update_proto(struct sock *sk, | |
401 | struct sk_psock *psock, | |
402 | struct proto *ops) | |
403 | { | |
b8e202d1 JS |
404 | /* Pairs with lockless read in sk_clone_lock() */ |
405 | WRITE_ONCE(sk->sk_prot, ops); | |
604326b4 DB |
406 | } |
407 | ||
408 | static inline void sk_psock_restore_proto(struct sock *sk, | |
409 | struct sk_psock *psock) | |
410 | { | |
4da6a196 | 411 | sk->sk_prot->unhash = psock->saved_unhash; |
7b70973d LB |
412 | if (inet_csk_has_ulp(sk)) { |
413 | tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); | |
414 | } else { | |
415 | sk->sk_write_space = psock->saved_write_space; | |
416 | /* Pairs with lockless read in sk_clone_lock() */ | |
417 | WRITE_ONCE(sk->sk_prot, psock->sk_proto); | |
418 | } | |
604326b4 DB |
419 | } |
420 | ||
421 | static inline void sk_psock_set_state(struct sk_psock *psock, | |
422 | enum sk_psock_state_bits bit) | |
423 | { | |
424 | set_bit(bit, &psock->state); | |
425 | } | |
426 | ||
427 | static inline void sk_psock_clear_state(struct sk_psock *psock, | |
428 | enum sk_psock_state_bits bit) | |
429 | { | |
430 | clear_bit(bit, &psock->state); | |
431 | } | |
432 | ||
433 | static inline bool sk_psock_test_state(const struct sk_psock *psock, | |
434 | enum sk_psock_state_bits bit) | |
435 | { | |
436 | return test_bit(bit, &psock->state); | |
437 | } | |
438 | ||
439 | static inline struct sk_psock *sk_psock_get(struct sock *sk) | |
440 | { | |
441 | struct sk_psock *psock; | |
442 | ||
443 | rcu_read_lock(); | |
444 | psock = sk_psock(sk); | |
445 | if (psock && !refcount_inc_not_zero(&psock->refcnt)) | |
446 | psock = NULL; | |
447 | rcu_read_unlock(); | |
448 | return psock; | |
449 | } | |
450 | ||
604326b4 DB |
451 | void sk_psock_drop(struct sock *sk, struct sk_psock *psock); |
452 | ||
453 | static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock) | |
454 | { | |
455 | if (refcount_dec_and_test(&psock->refcnt)) | |
456 | sk_psock_drop(sk, psock); | |
457 | } | |
458 | ||
552de910 JF |
459 | static inline void sk_psock_data_ready(struct sock *sk, struct sk_psock *psock) |
460 | { | |
5a685cd9 CW |
461 | if (psock->saved_data_ready) |
462 | psock->saved_data_ready(sk); | |
552de910 JF |
463 | else |
464 | sk->sk_data_ready(sk); | |
465 | } | |
466 | ||
604326b4 DB |
467 | static inline void psock_set_prog(struct bpf_prog **pprog, |
468 | struct bpf_prog *prog) | |
469 | { | |
470 | prog = xchg(pprog, prog); | |
471 | if (prog) | |
472 | bpf_prog_put(prog); | |
473 | } | |
474 | ||
bb0de313 LB |
475 | static inline int psock_replace_prog(struct bpf_prog **pprog, |
476 | struct bpf_prog *prog, | |
477 | struct bpf_prog *old) | |
478 | { | |
479 | if (cmpxchg(pprog, old, prog) != old) | |
480 | return -ENOENT; | |
481 | ||
482 | if (old) | |
483 | bpf_prog_put(old); | |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
604326b4 DB |
488 | static inline void psock_progs_drop(struct sk_psock_progs *progs) |
489 | { | |
490 | psock_set_prog(&progs->msg_parser, NULL); | |
ae8b8332 CW |
491 | psock_set_prog(&progs->stream_parser, NULL); |
492 | psock_set_prog(&progs->stream_verdict, NULL); | |
604326b4 DB |
493 | } |
494 | ||
e91de6af JF |
495 | int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb); |
496 | ||
497 | static inline bool sk_psock_strp_enabled(struct sk_psock *psock) | |
498 | { | |
499 | if (!psock) | |
500 | return false; | |
5a685cd9 | 501 | return !!psock->saved_data_ready; |
e91de6af | 502 | } |
e3526bb9 CW |
503 | |
504 | #if IS_ENABLED(CONFIG_NET_SOCK_MSG) | |
505 | ||
506 | /* We only have one bit so far. */ | |
507 | #define BPF_F_PTR_MASK ~(BPF_F_INGRESS) | |
508 | ||
509 | static inline bool skb_bpf_ingress(const struct sk_buff *skb) | |
510 | { | |
511 | unsigned long sk_redir = skb->_sk_redir; | |
512 | ||
513 | return sk_redir & BPF_F_INGRESS; | |
514 | } | |
515 | ||
516 | static inline void skb_bpf_set_ingress(struct sk_buff *skb) | |
517 | { | |
518 | skb->_sk_redir |= BPF_F_INGRESS; | |
519 | } | |
520 | ||
521 | static inline void skb_bpf_set_redir(struct sk_buff *skb, struct sock *sk_redir, | |
522 | bool ingress) | |
523 | { | |
524 | skb->_sk_redir = (unsigned long)sk_redir; | |
525 | if (ingress) | |
526 | skb->_sk_redir |= BPF_F_INGRESS; | |
527 | } | |
528 | ||
529 | static inline struct sock *skb_bpf_redirect_fetch(const struct sk_buff *skb) | |
530 | { | |
531 | unsigned long sk_redir = skb->_sk_redir; | |
532 | ||
533 | return (struct sock *)(sk_redir & BPF_F_PTR_MASK); | |
534 | } | |
535 | ||
536 | static inline void skb_bpf_redirect_clear(struct sk_buff *skb) | |
537 | { | |
538 | skb->_sk_redir = 0; | |
539 | } | |
540 | #endif /* CONFIG_NET_SOCK_MSG */ | |
604326b4 | 541 | #endif /* _LINUX_SKMSG_H */ |