blob: 2b915aafda428e5d3ee0fe5b61babadec888f57d [file] [log] [blame]
Daniel Borkmann604326b2018-10-13 02:45:58 +02001// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3
4#include <linux/skmsg.h>
5#include <linux/filter.h>
6#include <linux/bpf.h>
7#include <linux/init.h>
8#include <linux/wait.h>
9
10#include <net/inet_common.h>
John Fastabend0608c692018-12-20 11:35:35 -080011#include <net/tls.h>
Daniel Borkmann604326b2018-10-13 02:45:58 +020012
Daniel Borkmann604326b2018-10-13 02:45:58 +020013int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
John Fastabend02c558b2018-10-16 11:08:04 -070014 struct msghdr *msg, int len, int flags)
Daniel Borkmann604326b2018-10-13 02:45:58 +020015{
16 struct iov_iter *iter = &msg->msg_iter;
John Fastabend02c558b2018-10-16 11:08:04 -070017 int peek = flags & MSG_PEEK;
Daniel Borkmann604326b2018-10-13 02:45:58 +020018 int i, ret, copied = 0;
John Fastabend02c558b2018-10-16 11:08:04 -070019 struct sk_msg *msg_rx;
20
21 msg_rx = list_first_entry_or_null(&psock->ingress_msg,
22 struct sk_msg, list);
Daniel Borkmann604326b2018-10-13 02:45:58 +020023
24 while (copied != len) {
25 struct scatterlist *sge;
Daniel Borkmann604326b2018-10-13 02:45:58 +020026
Daniel Borkmann604326b2018-10-13 02:45:58 +020027 if (unlikely(!msg_rx))
28 break;
29
30 i = msg_rx->sg.start;
31 do {
32 struct page *page;
33 int copy;
34
35 sge = sk_msg_elem(msg_rx, i);
36 copy = sge->length;
37 page = sg_page(sge);
38 if (copied + copy > len)
39 copy = len - copied;
40 ret = copy_page_to_iter(page, sge->offset, copy, iter);
41 if (ret != copy) {
42 msg_rx->sg.start = i;
43 return -EFAULT;
44 }
45
46 copied += copy;
John Fastabend02c558b2018-10-16 11:08:04 -070047 if (likely(!peek)) {
48 sge->offset += copy;
49 sge->length -= copy;
50 sk_mem_uncharge(sk, copy);
51 msg_rx->sg.size -= copy;
52
53 if (!sge->length) {
54 sk_msg_iter_var_next(i);
55 if (!msg_rx->skb)
56 put_page(page);
57 }
58 } else {
59 sk_msg_iter_var_next(i);
Daniel Borkmann604326b2018-10-13 02:45:58 +020060 }
61
62 if (copied == len)
63 break;
64 } while (i != msg_rx->sg.end);
65
John Fastabend02c558b2018-10-16 11:08:04 -070066 if (unlikely(peek)) {
dihu487082f2020-06-05 16:46:25 +080067 if (msg_rx == list_last_entry(&psock->ingress_msg,
68 struct sk_msg, list))
69 break;
John Fastabend02c558b2018-10-16 11:08:04 -070070 msg_rx = list_next_entry(msg_rx, list);
71 continue;
72 }
73
Daniel Borkmann604326b2018-10-13 02:45:58 +020074 msg_rx->sg.start = i;
75 if (!sge->length && msg_rx->sg.start == msg_rx->sg.end) {
76 list_del(&msg_rx->list);
77 if (msg_rx->skb)
78 consume_skb(msg_rx->skb);
79 kfree(msg_rx);
80 }
John Fastabend02c558b2018-10-16 11:08:04 -070081 msg_rx = list_first_entry_or_null(&psock->ingress_msg,
82 struct sk_msg, list);
Daniel Borkmann604326b2018-10-13 02:45:58 +020083 }
84
85 return copied;
86}
87EXPORT_SYMBOL_GPL(__tcp_bpf_recvmsg);
88
Daniel Borkmann604326b2018-10-13 02:45:58 +020089static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
90 struct sk_msg *msg, u32 apply_bytes, int flags)
91{
92 bool apply = apply_bytes;
93 struct scatterlist *sge;
94 u32 size, copied = 0;
95 struct sk_msg *tmp;
96 int i, ret = 0;
97
98 tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
99 if (unlikely(!tmp))
100 return -ENOMEM;
101
102 lock_sock(sk);
103 tmp->sg.start = msg->sg.start;
104 i = msg->sg.start;
105 do {
106 sge = sk_msg_elem(msg, i);
107 size = (apply && apply_bytes < sge->length) ?
108 apply_bytes : sge->length;
109 if (!sk_wmem_schedule(sk, size)) {
110 if (!copied)
111 ret = -ENOMEM;
112 break;
113 }
114
115 sk_mem_charge(sk, size);
116 sk_msg_xfer(tmp, msg, i, size);
117 copied += size;
118 if (sge->length)
119 get_page(sk_msg_page(tmp, i));
120 sk_msg_iter_var_next(i);
121 tmp->sg.end = i;
122 if (apply) {
123 apply_bytes -= size;
124 if (!apply_bytes)
125 break;
126 }
127 } while (i != msg->sg.end);
128
129 if (!ret) {
130 msg->sg.start = i;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200131 sk_psock_queue_msg(psock, tmp);
John Fastabend552de9102018-12-20 11:35:33 -0800132 sk_psock_data_ready(sk, psock);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200133 } else {
134 sk_msg_free(sk, tmp);
135 kfree(tmp);
136 }
137
138 release_sock(sk);
139 return ret;
140}
141
142static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
143 int flags, bool uncharge)
144{
145 bool apply = apply_bytes;
146 struct scatterlist *sge;
147 struct page *page;
148 int size, ret = 0;
149 u32 off;
150
151 while (1) {
John Fastabend0608c692018-12-20 11:35:35 -0800152 bool has_tx_ulp;
153
Daniel Borkmann604326b2018-10-13 02:45:58 +0200154 sge = sk_msg_elem(msg, msg->sg.start);
155 size = (apply && apply_bytes < sge->length) ?
156 apply_bytes : sge->length;
157 off = sge->offset;
158 page = sg_page(sge);
159
160 tcp_rate_check_app_limited(sk);
161retry:
John Fastabend0608c692018-12-20 11:35:35 -0800162 has_tx_ulp = tls_sw_has_ctx_tx(sk);
163 if (has_tx_ulp) {
164 flags |= MSG_SENDPAGE_NOPOLICY;
165 ret = kernel_sendpage_locked(sk,
166 page, off, size, flags);
167 } else {
168 ret = do_tcp_sendpages(sk, page, off, size, flags);
169 }
170
Daniel Borkmann604326b2018-10-13 02:45:58 +0200171 if (ret <= 0)
172 return ret;
173 if (apply)
174 apply_bytes -= ret;
175 msg->sg.size -= ret;
176 sge->offset += ret;
177 sge->length -= ret;
178 if (uncharge)
179 sk_mem_uncharge(sk, ret);
180 if (ret != size) {
181 size -= ret;
182 off += ret;
183 goto retry;
184 }
185 if (!sge->length) {
186 put_page(page);
187 sk_msg_iter_next(msg, start);
188 sg_init_table(sge, 1);
189 if (msg->sg.start == msg->sg.end)
190 break;
191 }
192 if (apply && !apply_bytes)
193 break;
194 }
195
196 return 0;
197}
198
199static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
200 u32 apply_bytes, int flags, bool uncharge)
201{
202 int ret;
203
204 lock_sock(sk);
205 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
206 release_sock(sk);
207 return ret;
208}
209
210int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg,
211 u32 bytes, int flags)
212{
213 bool ingress = sk_msg_to_ingress(msg);
214 struct sk_psock *psock = sk_psock_get(sk);
215 int ret;
216
217 if (unlikely(!psock)) {
218 sk_msg_free(sk, msg);
219 return 0;
220 }
221 ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
222 tcp_bpf_push_locked(sk, msg, bytes, flags, false);
223 sk_psock_put(sk, psock);
224 return ret;
225}
226EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
227
YueHaibinga2652792020-03-20 10:34:25 +0800228#ifdef CONFIG_BPF_STREAM_PARSER
229static bool tcp_bpf_stream_read(const struct sock *sk)
230{
231 struct sk_psock *psock;
232 bool empty = true;
233
234 rcu_read_lock();
235 psock = sk_psock(sk);
236 if (likely(psock))
237 empty = list_empty(&psock->ingress_msg);
238 rcu_read_unlock();
239 return !empty;
240}
241
YueHaibingc0fd3362020-03-20 10:34:26 +0800242static int tcp_bpf_wait_data(struct sock *sk, struct sk_psock *psock,
243 int flags, long timeo, int *err)
244{
245 DEFINE_WAIT_FUNC(wait, woken_wake_function);
246 int ret = 0;
247
248 if (!timeo)
249 return ret;
250
251 add_wait_queue(sk_sleep(sk), &wait);
252 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
253 ret = sk_wait_event(sk, &timeo,
254 !list_empty(&psock->ingress_msg) ||
255 !skb_queue_empty(&sk->sk_receive_queue), &wait);
256 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
257 remove_wait_queue(sk_sleep(sk), &wait);
258 return ret;
259}
260
261static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
262 int nonblock, int flags, int *addr_len)
263{
264 struct sk_psock *psock;
265 int copied, ret;
266
Xiyu Yang18f02ad2020-04-26 11:35:15 +0800267 if (unlikely(flags & MSG_ERRQUEUE))
268 return inet_recv_error(sk, msg, len, addr_len);
269
YueHaibingc0fd3362020-03-20 10:34:26 +0800270 psock = sk_psock_get(sk);
271 if (unlikely(!psock))
272 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
YueHaibingc0fd3362020-03-20 10:34:26 +0800273 if (!skb_queue_empty(&sk->sk_receive_queue) &&
Xiyu Yang18f02ad2020-04-26 11:35:15 +0800274 sk_psock_queue_empty(psock)) {
275 sk_psock_put(sk, psock);
YueHaibingc0fd3362020-03-20 10:34:26 +0800276 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
Xiyu Yang18f02ad2020-04-26 11:35:15 +0800277 }
YueHaibingc0fd3362020-03-20 10:34:26 +0800278 lock_sock(sk);
279msg_bytes_ready:
280 copied = __tcp_bpf_recvmsg(sk, psock, msg, len, flags);
281 if (!copied) {
282 int data, err = 0;
283 long timeo;
284
285 timeo = sock_rcvtimeo(sk, nonblock);
286 data = tcp_bpf_wait_data(sk, psock, flags, timeo, &err);
287 if (data) {
288 if (!sk_psock_queue_empty(psock))
289 goto msg_bytes_ready;
290 release_sock(sk);
291 sk_psock_put(sk, psock);
292 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
293 }
294 if (err) {
295 ret = err;
296 goto out;
297 }
298 copied = -EAGAIN;
299 }
300 ret = copied;
301out:
302 release_sock(sk);
303 sk_psock_put(sk, psock);
304 return ret;
305}
306
Daniel Borkmann604326b2018-10-13 02:45:58 +0200307static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
308 struct sk_msg *msg, int *copied, int flags)
309{
Jakub Kicinski031097d2019-11-27 12:16:41 -0800310 bool cork = false, enospc = sk_msg_full(msg);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200311 struct sock *sk_redir;
John Fastabend7246d8e2018-11-26 14:16:17 -0800312 u32 tosend, delta = 0;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200313 int ret;
314
315more_data:
John Fastabend7246d8e2018-11-26 14:16:17 -0800316 if (psock->eval == __SK_NONE) {
317 /* Track delta in msg size to add/subtract it on SK_DROP from
318 * returned to user copied size. This ensures user doesn't
319 * get a positive return code with msg_cut_data and SK_DROP
320 * verdict.
321 */
322 delta = msg->sg.size;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200323 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
John Fastabend7361d442020-01-11 06:12:06 +0000324 delta -= msg->sg.size;
John Fastabend7246d8e2018-11-26 14:16:17 -0800325 }
Daniel Borkmann604326b2018-10-13 02:45:58 +0200326
327 if (msg->cork_bytes &&
328 msg->cork_bytes > msg->sg.size && !enospc) {
329 psock->cork_bytes = msg->cork_bytes - msg->sg.size;
330 if (!psock->cork) {
331 psock->cork = kzalloc(sizeof(*psock->cork),
332 GFP_ATOMIC | __GFP_NOWARN);
333 if (!psock->cork)
334 return -ENOMEM;
335 }
336 memcpy(psock->cork, msg, sizeof(*msg));
337 return 0;
338 }
339
340 tosend = msg->sg.size;
341 if (psock->apply_bytes && psock->apply_bytes < tosend)
342 tosend = psock->apply_bytes;
343
344 switch (psock->eval) {
345 case __SK_PASS:
346 ret = tcp_bpf_push(sk, msg, tosend, flags, true);
347 if (unlikely(ret)) {
348 *copied -= sk_msg_free(sk, msg);
349 break;
350 }
351 sk_msg_apply_bytes(psock, tosend);
352 break;
353 case __SK_REDIRECT:
354 sk_redir = psock->sk_redir;
355 sk_msg_apply_bytes(psock, tosend);
356 if (psock->cork) {
357 cork = true;
358 psock->cork = NULL;
359 }
360 sk_msg_return(sk, msg, tosend);
361 release_sock(sk);
362 ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
363 lock_sock(sk);
364 if (unlikely(ret < 0)) {
365 int free = sk_msg_free_nocharge(sk, msg);
366
367 if (!cork)
368 *copied -= free;
369 }
370 if (cork) {
371 sk_msg_free(sk, msg);
372 kfree(msg);
373 msg = NULL;
374 ret = 0;
375 }
376 break;
377 case __SK_DROP:
378 default:
379 sk_msg_free_partial(sk, msg, tosend);
380 sk_msg_apply_bytes(psock, tosend);
John Fastabend7246d8e2018-11-26 14:16:17 -0800381 *copied -= (tosend + delta);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200382 return -EACCES;
383 }
384
385 if (likely(!ret)) {
386 if (!psock->apply_bytes) {
387 psock->eval = __SK_NONE;
388 if (psock->sk_redir) {
389 sock_put(psock->sk_redir);
390 psock->sk_redir = NULL;
391 }
392 }
393 if (msg &&
394 msg->sg.data[msg->sg.start].page_link &&
395 msg->sg.data[msg->sg.start].length)
396 goto more_data;
397 }
398 return ret;
399}
400
401static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
402{
403 struct sk_msg tmp, *msg_tx = NULL;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200404 int copied = 0, err = 0;
405 struct sk_psock *psock;
406 long timeo;
Jakub Kicinski41477662019-08-07 17:03:59 -0700407 int flags;
408
409 /* Don't let internal do_tcp_sendpages() flags through */
410 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
411 flags |= MSG_NO_SHARED_FRAGS;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200412
413 psock = sk_psock_get(sk);
414 if (unlikely(!psock))
415 return tcp_sendmsg(sk, msg, size);
416
417 lock_sock(sk);
418 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
419 while (msg_data_left(msg)) {
420 bool enospc = false;
421 u32 copy, osize;
422
423 if (sk->sk_err) {
424 err = -sk->sk_err;
425 goto out_err;
426 }
427
428 copy = msg_data_left(msg);
429 if (!sk_stream_memory_free(sk))
430 goto wait_for_sndbuf;
431 if (psock->cork) {
432 msg_tx = psock->cork;
433 } else {
434 msg_tx = &tmp;
435 sk_msg_init(msg_tx);
436 }
437
438 osize = msg_tx->sg.size;
439 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
440 if (err) {
441 if (err != -ENOSPC)
442 goto wait_for_memory;
443 enospc = true;
444 copy = msg_tx->sg.size - osize;
445 }
446
447 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
448 copy);
449 if (err < 0) {
450 sk_msg_trim(sk, msg_tx, osize);
451 goto out_err;
452 }
453
454 copied += copy;
455 if (psock->cork_bytes) {
456 if (size > psock->cork_bytes)
457 psock->cork_bytes = 0;
458 else
459 psock->cork_bytes -= size;
460 if (psock->cork_bytes && !enospc)
461 goto out_err;
462 /* All cork bytes are accounted, rerun the prog. */
463 psock->eval = __SK_NONE;
464 psock->cork_bytes = 0;
465 }
466
467 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
468 if (unlikely(err < 0))
469 goto out_err;
470 continue;
471wait_for_sndbuf:
472 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
473wait_for_memory:
474 err = sk_stream_wait_memory(sk, &timeo);
475 if (err) {
476 if (msg_tx && msg_tx != psock->cork)
477 sk_msg_free(sk, msg_tx);
478 goto out_err;
479 }
480 }
481out_err:
482 if (err < 0)
483 err = sk_stream_error(sk, msg->msg_flags, err);
484 release_sock(sk);
485 sk_psock_put(sk, psock);
486 return copied ? copied : err;
487}
488
489static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
490 size_t size, int flags)
491{
492 struct sk_msg tmp, *msg = NULL;
493 int err = 0, copied = 0;
494 struct sk_psock *psock;
495 bool enospc = false;
496
497 psock = sk_psock_get(sk);
498 if (unlikely(!psock))
499 return tcp_sendpage(sk, page, offset, size, flags);
500
501 lock_sock(sk);
502 if (psock->cork) {
503 msg = psock->cork;
504 } else {
505 msg = &tmp;
506 sk_msg_init(msg);
507 }
508
509 /* Catch case where ring is full and sendpage is stalled. */
510 if (unlikely(sk_msg_full(msg)))
511 goto out_err;
512
513 sk_msg_page_add(msg, page, size, offset);
514 sk_mem_charge(sk, size);
515 copied = size;
516 if (sk_msg_full(msg))
517 enospc = true;
518 if (psock->cork_bytes) {
519 if (size > psock->cork_bytes)
520 psock->cork_bytes = 0;
521 else
522 psock->cork_bytes -= size;
523 if (psock->cork_bytes && !enospc)
524 goto out_err;
525 /* All cork bytes are accounted, rerun the prog. */
526 psock->eval = __SK_NONE;
527 psock->cork_bytes = 0;
528 }
529
530 err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
531out_err:
532 release_sock(sk);
533 sk_psock_put(sk, psock);
534 return copied ? copied : err;
535}
536
Daniel Borkmann604326b2018-10-13 02:45:58 +0200537enum {
538 TCP_BPF_IPV4,
539 TCP_BPF_IPV6,
540 TCP_BPF_NUM_PROTS,
541};
542
543enum {
544 TCP_BPF_BASE,
545 TCP_BPF_TX,
546 TCP_BPF_NUM_CFGS,
547};
548
549static struct proto *tcpv6_prot_saved __read_mostly;
550static DEFINE_SPINLOCK(tcpv6_prot_lock);
551static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
552
553static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
554 struct proto *base)
555{
556 prot[TCP_BPF_BASE] = *base;
Lorenz Bauerf7476322020-03-09 11:12:36 +0000557 prot[TCP_BPF_BASE].unhash = sock_map_unhash;
558 prot[TCP_BPF_BASE].close = sock_map_close;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200559 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
560 prot[TCP_BPF_BASE].stream_memory_read = tcp_bpf_stream_read;
561
562 prot[TCP_BPF_TX] = prot[TCP_BPF_BASE];
563 prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg;
564 prot[TCP_BPF_TX].sendpage = tcp_bpf_sendpage;
565}
566
567static void tcp_bpf_check_v6_needs_rebuild(struct sock *sk, struct proto *ops)
568{
569 if (sk->sk_family == AF_INET6 &&
570 unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
571 spin_lock_bh(&tcpv6_prot_lock);
572 if (likely(ops != tcpv6_prot_saved)) {
573 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
574 smp_store_release(&tcpv6_prot_saved, ops);
575 }
576 spin_unlock_bh(&tcpv6_prot_lock);
577 }
578}
579
580static int __init tcp_bpf_v4_build_proto(void)
581{
582 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
583 return 0;
584}
585core_initcall(tcp_bpf_v4_build_proto);
586
Daniel Borkmann604326b2018-10-13 02:45:58 +0200587static int tcp_bpf_assert_proto_ops(struct proto *ops)
588{
589 /* In order to avoid retpoline, we make assumptions when we call
590 * into ops if e.g. a psock is not present. Make sure they are
591 * indeed valid assumptions.
592 */
593 return ops->recvmsg == tcp_recvmsg &&
594 ops->sendmsg == tcp_sendmsg &&
595 ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
596}
597
Lorenz Bauerf7476322020-03-09 11:12:36 +0000598struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock)
Daniel Borkmann604326b2018-10-13 02:45:58 +0200599{
Lorenz Bauerd19da362020-03-09 11:12:34 +0000600 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
601 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200602
Lorenz Bauerd19da362020-03-09 11:12:34 +0000603 if (!psock->sk_proto) {
604 struct proto *ops = READ_ONCE(sk->sk_prot);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200605
Lorenz Bauerd19da362020-03-09 11:12:34 +0000606 if (tcp_bpf_assert_proto_ops(ops))
607 return ERR_PTR(-EINVAL);
608
609 tcp_bpf_check_v6_needs_rebuild(sk, ops);
610 }
611
612 return &tcp_bpf_prots[family][config];
Daniel Borkmann604326b2018-10-13 02:45:58 +0200613}
614
Jakub Sitnickie8025152020-02-18 17:10:15 +0000615/* If a child got cloned from a listening socket that had tcp_bpf
616 * protocol callbacks installed, we need to restore the callbacks to
617 * the default ones because the child does not inherit the psock state
618 * that tcp_bpf callbacks expect.
619 */
620void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
621{
622 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
623 struct proto *prot = newsk->sk_prot;
624
625 if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
626 newsk->sk_prot = sk->sk_prot_creator;
627}
Lorenz Bauerf7476322020-03-09 11:12:36 +0000628#endif /* CONFIG_BPF_STREAM_PARSER */