1 // 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 #include <linux/util_macros.h>
10
11 #include <net/inet_common.h>
12 #include <net/tls.h>
13
bpf_tcp_ingress(struct sock * sk,struct sk_psock * psock,struct sk_msg * msg,u32 apply_bytes,int flags)14 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
15 struct sk_msg *msg, u32 apply_bytes, int flags)
16 {
17 bool apply = apply_bytes;
18 struct scatterlist *sge;
19 u32 size, copied = 0;
20 struct sk_msg *tmp;
21 int i, ret = 0;
22
23 tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
24 if (unlikely(!tmp))
25 return -ENOMEM;
26
27 lock_sock(sk);
28 tmp->sg.start = msg->sg.start;
29 i = msg->sg.start;
30 do {
31 sge = sk_msg_elem(msg, i);
32 size = (apply && apply_bytes < sge->length) ?
33 apply_bytes : sge->length;
34 if (!sk_wmem_schedule(sk, size)) {
35 if (!copied)
36 ret = -ENOMEM;
37 break;
38 }
39
40 sk_mem_charge(sk, size);
41 sk_msg_xfer(tmp, msg, i, size);
42 copied += size;
43 if (sge->length)
44 get_page(sk_msg_page(tmp, i));
45 sk_msg_iter_var_next(i);
46 tmp->sg.end = i;
47 if (apply) {
48 apply_bytes -= size;
49 if (!apply_bytes) {
50 if (sge->length)
51 sk_msg_iter_var_prev(i);
52 break;
53 }
54 }
55 } while (i != msg->sg.end);
56
57 if (!ret) {
58 msg->sg.start = i;
59 sk_psock_queue_msg(psock, tmp);
60 sk_psock_data_ready(sk, psock);
61 } else {
62 sk_msg_free(sk, tmp);
63 kfree(tmp);
64 }
65
66 release_sock(sk);
67 return ret;
68 }
69
tcp_bpf_push(struct sock * sk,struct sk_msg * msg,u32 apply_bytes,int flags,bool uncharge)70 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
71 int flags, bool uncharge)
72 {
73 bool apply = apply_bytes;
74 struct scatterlist *sge;
75 struct page *page;
76 int size, ret = 0;
77 u32 off;
78
79 while (1) {
80 bool has_tx_ulp;
81
82 sge = sk_msg_elem(msg, msg->sg.start);
83 size = (apply && apply_bytes < sge->length) ?
84 apply_bytes : sge->length;
85 off = sge->offset;
86 page = sg_page(sge);
87
88 tcp_rate_check_app_limited(sk);
89 retry:
90 has_tx_ulp = tls_sw_has_ctx_tx(sk);
91 if (has_tx_ulp) {
92 flags |= MSG_SENDPAGE_NOPOLICY;
93 ret = kernel_sendpage_locked(sk,
94 page, off, size, flags);
95 } else {
96 ret = do_tcp_sendpages(sk, page, off, size, flags);
97 }
98
99 if (ret <= 0)
100 return ret;
101 if (apply)
102 apply_bytes -= ret;
103 msg->sg.size -= ret;
104 sge->offset += ret;
105 sge->length -= ret;
106 if (uncharge)
107 sk_mem_uncharge(sk, ret);
108 if (ret != size) {
109 size -= ret;
110 off += ret;
111 goto retry;
112 }
113 if (!sge->length) {
114 put_page(page);
115 sk_msg_iter_next(msg, start);
116 sg_init_table(sge, 1);
117 if (msg->sg.start == msg->sg.end)
118 break;
119 }
120 if (apply && !apply_bytes)
121 break;
122 }
123
124 return 0;
125 }
126
tcp_bpf_push_locked(struct sock * sk,struct sk_msg * msg,u32 apply_bytes,int flags,bool uncharge)127 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
128 u32 apply_bytes, int flags, bool uncharge)
129 {
130 int ret;
131
132 lock_sock(sk);
133 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
134 release_sock(sk);
135 return ret;
136 }
137
tcp_bpf_sendmsg_redir(struct sock * sk,bool ingress,struct sk_msg * msg,u32 bytes,int flags)138 int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
139 struct sk_msg *msg, u32 bytes, int flags)
140 {
141 struct sk_psock *psock = sk_psock_get(sk);
142 int ret;
143
144 if (unlikely(!psock))
145 return -EPIPE;
146
147 ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
148 tcp_bpf_push_locked(sk, msg, bytes, flags, false);
149 sk_psock_put(sk, psock);
150 return ret;
151 }
152 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
153
154 #ifdef CONFIG_BPF_SYSCALL
tcp_msg_wait_data(struct sock * sk,struct sk_psock * psock,long timeo)155 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
156 long timeo)
157 {
158 DEFINE_WAIT_FUNC(wait, woken_wake_function);
159 int ret = 0;
160
161 if (sk->sk_shutdown & RCV_SHUTDOWN)
162 return 1;
163
164 if (!timeo)
165 return ret;
166
167 add_wait_queue(sk_sleep(sk), &wait);
168 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
169 ret = sk_wait_event(sk, &timeo,
170 !list_empty(&psock->ingress_msg) ||
171 !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait);
172 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
173 remove_wait_queue(sk_sleep(sk), &wait);
174 return ret;
175 }
176
tcp_bpf_recvmsg_parser(struct sock * sk,struct msghdr * msg,size_t len,int nonblock,int flags,int * addr_len)177 static int tcp_bpf_recvmsg_parser(struct sock *sk,
178 struct msghdr *msg,
179 size_t len,
180 int nonblock,
181 int flags,
182 int *addr_len)
183 {
184 struct sk_psock *psock;
185 int copied;
186
187 if (unlikely(flags & MSG_ERRQUEUE))
188 return inet_recv_error(sk, msg, len, addr_len);
189
190 if (!len)
191 return 0;
192
193 psock = sk_psock_get(sk);
194 if (unlikely(!psock))
195 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
196
197 lock_sock(sk);
198 msg_bytes_ready:
199 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
200 if (!copied) {
201 long timeo;
202 int data;
203
204 if (sock_flag(sk, SOCK_DONE))
205 goto out;
206
207 if (sk->sk_err) {
208 copied = sock_error(sk);
209 goto out;
210 }
211
212 if (sk->sk_shutdown & RCV_SHUTDOWN)
213 goto out;
214
215 if (sk->sk_state == TCP_CLOSE) {
216 copied = -ENOTCONN;
217 goto out;
218 }
219
220 timeo = sock_rcvtimeo(sk, nonblock);
221 if (!timeo) {
222 copied = -EAGAIN;
223 goto out;
224 }
225
226 if (signal_pending(current)) {
227 copied = sock_intr_errno(timeo);
228 goto out;
229 }
230
231 data = tcp_msg_wait_data(sk, psock, timeo);
232 if (data && !sk_psock_queue_empty(psock))
233 goto msg_bytes_ready;
234 copied = -EAGAIN;
235 }
236 out:
237 release_sock(sk);
238 sk_psock_put(sk, psock);
239 return copied;
240 }
241
tcp_bpf_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int nonblock,int flags,int * addr_len)242 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
243 int nonblock, int flags, int *addr_len)
244 {
245 struct sk_psock *psock;
246 int copied, ret;
247
248 if (unlikely(flags & MSG_ERRQUEUE))
249 return inet_recv_error(sk, msg, len, addr_len);
250
251 if (!len)
252 return 0;
253
254 psock = sk_psock_get(sk);
255 if (unlikely(!psock))
256 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
257 if (!skb_queue_empty(&sk->sk_receive_queue) &&
258 sk_psock_queue_empty(psock)) {
259 sk_psock_put(sk, psock);
260 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
261 }
262 lock_sock(sk);
263 msg_bytes_ready:
264 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
265 if (!copied) {
266 long timeo;
267 int data;
268
269 timeo = sock_rcvtimeo(sk, nonblock);
270 data = tcp_msg_wait_data(sk, psock, timeo);
271 if (data) {
272 if (!sk_psock_queue_empty(psock))
273 goto msg_bytes_ready;
274 release_sock(sk);
275 sk_psock_put(sk, psock);
276 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
277 }
278 copied = -EAGAIN;
279 }
280 ret = copied;
281 release_sock(sk);
282 sk_psock_put(sk, psock);
283 return ret;
284 }
285
tcp_bpf_send_verdict(struct sock * sk,struct sk_psock * psock,struct sk_msg * msg,int * copied,int flags)286 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
287 struct sk_msg *msg, int *copied, int flags)
288 {
289 bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
290 struct sock *sk_redir;
291 u32 tosend, origsize, sent, delta = 0;
292 u32 eval;
293 int ret;
294
295 more_data:
296 if (psock->eval == __SK_NONE) {
297 /* Track delta in msg size to add/subtract it on SK_DROP from
298 * returned to user copied size. This ensures user doesn't
299 * get a positive return code with msg_cut_data and SK_DROP
300 * verdict.
301 */
302 delta = msg->sg.size;
303 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
304 delta -= msg->sg.size;
305 }
306
307 if (msg->cork_bytes &&
308 msg->cork_bytes > msg->sg.size && !enospc) {
309 psock->cork_bytes = msg->cork_bytes - msg->sg.size;
310 if (!psock->cork) {
311 psock->cork = kzalloc(sizeof(*psock->cork),
312 GFP_ATOMIC | __GFP_NOWARN);
313 if (!psock->cork)
314 return -ENOMEM;
315 }
316 memcpy(psock->cork, msg, sizeof(*msg));
317 return 0;
318 }
319
320 tosend = msg->sg.size;
321 if (psock->apply_bytes && psock->apply_bytes < tosend)
322 tosend = psock->apply_bytes;
323 eval = __SK_NONE;
324
325 switch (psock->eval) {
326 case __SK_PASS:
327 ret = tcp_bpf_push(sk, msg, tosend, flags, true);
328 if (unlikely(ret)) {
329 *copied -= sk_msg_free(sk, msg);
330 break;
331 }
332 sk_msg_apply_bytes(psock, tosend);
333 break;
334 case __SK_REDIRECT:
335 redir_ingress = psock->redir_ingress;
336 sk_redir = psock->sk_redir;
337 sk_msg_apply_bytes(psock, tosend);
338 if (!psock->apply_bytes) {
339 /* Clean up before releasing the sock lock. */
340 eval = psock->eval;
341 psock->eval = __SK_NONE;
342 psock->sk_redir = NULL;
343 }
344 if (psock->cork) {
345 cork = true;
346 psock->cork = NULL;
347 }
348 sk_msg_return(sk, msg, tosend);
349 release_sock(sk);
350
351 origsize = msg->sg.size;
352 ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
353 msg, tosend, flags);
354 sent = origsize - msg->sg.size;
355
356 if (eval == __SK_REDIRECT)
357 sock_put(sk_redir);
358
359 lock_sock(sk);
360 if (unlikely(ret < 0)) {
361 int free = sk_msg_free_nocharge(sk, msg);
362
363 if (!cork)
364 *copied -= free;
365 }
366 if (cork) {
367 sk_msg_free(sk, msg);
368 kfree(msg);
369 msg = NULL;
370 ret = 0;
371 }
372 break;
373 case __SK_DROP:
374 default:
375 sk_msg_free_partial(sk, msg, tosend);
376 sk_msg_apply_bytes(psock, tosend);
377 *copied -= (tosend + delta);
378 return -EACCES;
379 }
380
381 if (likely(!ret)) {
382 if (!psock->apply_bytes) {
383 psock->eval = __SK_NONE;
384 if (psock->sk_redir) {
385 sock_put(psock->sk_redir);
386 psock->sk_redir = NULL;
387 }
388 }
389 if (msg &&
390 msg->sg.data[msg->sg.start].page_link &&
391 msg->sg.data[msg->sg.start].length) {
392 if (eval == __SK_REDIRECT)
393 sk_mem_charge(sk, tosend - sent);
394 goto more_data;
395 }
396 }
397 return ret;
398 }
399
tcp_bpf_sendmsg(struct sock * sk,struct msghdr * msg,size_t size)400 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
401 {
402 struct sk_msg tmp, *msg_tx = NULL;
403 int copied = 0, err = 0;
404 struct sk_psock *psock;
405 long timeo;
406 int flags;
407
408 /* Don't let internal do_tcp_sendpages() flags through */
409 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
410 flags |= MSG_NO_SHARED_FRAGS;
411
412 psock = sk_psock_get(sk);
413 if (unlikely(!psock))
414 return tcp_sendmsg(sk, msg, size);
415
416 lock_sock(sk);
417 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
418 while (msg_data_left(msg)) {
419 bool enospc = false;
420 u32 copy, osize;
421
422 if (sk->sk_err) {
423 err = -sk->sk_err;
424 goto out_err;
425 }
426
427 copy = msg_data_left(msg);
428 if (!sk_stream_memory_free(sk))
429 goto wait_for_sndbuf;
430 if (psock->cork) {
431 msg_tx = psock->cork;
432 } else {
433 msg_tx = &tmp;
434 sk_msg_init(msg_tx);
435 }
436
437 osize = msg_tx->sg.size;
438 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
439 if (err) {
440 if (err != -ENOSPC)
441 goto wait_for_memory;
442 enospc = true;
443 copy = msg_tx->sg.size - osize;
444 }
445
446 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
447 copy);
448 if (err < 0) {
449 sk_msg_trim(sk, msg_tx, osize);
450 goto out_err;
451 }
452
453 copied += copy;
454 if (psock->cork_bytes) {
455 if (size > psock->cork_bytes)
456 psock->cork_bytes = 0;
457 else
458 psock->cork_bytes -= size;
459 if (psock->cork_bytes && !enospc)
460 goto out_err;
461 /* All cork bytes are accounted, rerun the prog. */
462 psock->eval = __SK_NONE;
463 psock->cork_bytes = 0;
464 }
465
466 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
467 if (unlikely(err < 0))
468 goto out_err;
469 continue;
470 wait_for_sndbuf:
471 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
472 wait_for_memory:
473 err = sk_stream_wait_memory(sk, &timeo);
474 if (err) {
475 if (msg_tx && msg_tx != psock->cork)
476 sk_msg_free(sk, msg_tx);
477 goto out_err;
478 }
479 }
480 out_err:
481 if (err < 0)
482 err = sk_stream_error(sk, msg->msg_flags, err);
483 release_sock(sk);
484 sk_psock_put(sk, psock);
485 return copied ? copied : err;
486 }
487
tcp_bpf_sendpage(struct sock * sk,struct page * page,int offset,size_t size,int flags)488 static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
489 size_t size, int flags)
490 {
491 struct sk_msg tmp, *msg = NULL;
492 int err = 0, copied = 0;
493 struct sk_psock *psock;
494 bool enospc = false;
495
496 psock = sk_psock_get(sk);
497 if (unlikely(!psock))
498 return tcp_sendpage(sk, page, offset, size, flags);
499
500 lock_sock(sk);
501 if (psock->cork) {
502 msg = psock->cork;
503 } else {
504 msg = &tmp;
505 sk_msg_init(msg);
506 }
507
508 /* Catch case where ring is full and sendpage is stalled. */
509 if (unlikely(sk_msg_full(msg)))
510 goto out_err;
511
512 sk_msg_page_add(msg, page, size, offset);
513 sk_mem_charge(sk, size);
514 copied = size;
515 if (sk_msg_full(msg))
516 enospc = true;
517 if (psock->cork_bytes) {
518 if (size > psock->cork_bytes)
519 psock->cork_bytes = 0;
520 else
521 psock->cork_bytes -= size;
522 if (psock->cork_bytes && !enospc)
523 goto out_err;
524 /* All cork bytes are accounted, rerun the prog. */
525 psock->eval = __SK_NONE;
526 psock->cork_bytes = 0;
527 }
528
529 err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
530 out_err:
531 release_sock(sk);
532 sk_psock_put(sk, psock);
533 return copied ? copied : err;
534 }
535
536 enum {
537 TCP_BPF_IPV4,
538 TCP_BPF_IPV6,
539 TCP_BPF_NUM_PROTS,
540 };
541
542 enum {
543 TCP_BPF_BASE,
544 TCP_BPF_TX,
545 TCP_BPF_RX,
546 TCP_BPF_TXRX,
547 TCP_BPF_NUM_CFGS,
548 };
549
550 static struct proto *tcpv6_prot_saved __read_mostly;
551 static DEFINE_SPINLOCK(tcpv6_prot_lock);
552 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
553
tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],struct proto * base)554 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
555 struct proto *base)
556 {
557 prot[TCP_BPF_BASE] = *base;
558 prot[TCP_BPF_BASE].destroy = sock_map_destroy;
559 prot[TCP_BPF_BASE].close = sock_map_close;
560 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
561 prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable;
562
563 prot[TCP_BPF_TX] = prot[TCP_BPF_BASE];
564 prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg;
565 prot[TCP_BPF_TX].sendpage = tcp_bpf_sendpage;
566
567 prot[TCP_BPF_RX] = prot[TCP_BPF_BASE];
568 prot[TCP_BPF_RX].recvmsg = tcp_bpf_recvmsg_parser;
569
570 prot[TCP_BPF_TXRX] = prot[TCP_BPF_TX];
571 prot[TCP_BPF_TXRX].recvmsg = tcp_bpf_recvmsg_parser;
572 }
573
tcp_bpf_check_v6_needs_rebuild(struct proto * ops)574 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
575 {
576 if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
577 spin_lock_bh(&tcpv6_prot_lock);
578 if (likely(ops != tcpv6_prot_saved)) {
579 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
580 smp_store_release(&tcpv6_prot_saved, ops);
581 }
582 spin_unlock_bh(&tcpv6_prot_lock);
583 }
584 }
585
tcp_bpf_v4_build_proto(void)586 static int __init tcp_bpf_v4_build_proto(void)
587 {
588 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
589 return 0;
590 }
591 late_initcall(tcp_bpf_v4_build_proto);
592
tcp_bpf_assert_proto_ops(struct proto * ops)593 static int tcp_bpf_assert_proto_ops(struct proto *ops)
594 {
595 /* In order to avoid retpoline, we make assumptions when we call
596 * into ops if e.g. a psock is not present. Make sure they are
597 * indeed valid assumptions.
598 */
599 return ops->recvmsg == tcp_recvmsg &&
600 ops->sendmsg == tcp_sendmsg &&
601 ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
602 }
603
tcp_bpf_update_proto(struct sock * sk,struct sk_psock * psock,bool restore)604 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
605 {
606 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
607 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
608
609 if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
610 config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
611 }
612
613 if (restore) {
614 if (inet_csk_has_ulp(sk)) {
615 /* TLS does not have an unhash proto in SW cases,
616 * but we need to ensure we stop using the sock_map
617 * unhash routine because the associated psock is being
618 * removed. So use the original unhash handler.
619 */
620 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
621 tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
622 } else {
623 sk->sk_write_space = psock->saved_write_space;
624 /* Pairs with lockless read in sk_clone_lock() */
625 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
626 }
627 return 0;
628 }
629
630 if (sk->sk_family == AF_INET6) {
631 if (tcp_bpf_assert_proto_ops(psock->sk_proto))
632 return -EINVAL;
633
634 tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
635 }
636
637 /* Pairs with lockless read in sk_clone_lock() */
638 WRITE_ONCE(sk->sk_prot, &tcp_bpf_prots[family][config]);
639 return 0;
640 }
641 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
642
643 /* If a child got cloned from a listening socket that had tcp_bpf
644 * protocol callbacks installed, we need to restore the callbacks to
645 * the default ones because the child does not inherit the psock state
646 * that tcp_bpf callbacks expect.
647 */
tcp_bpf_clone(const struct sock * sk,struct sock * newsk)648 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
649 {
650 struct proto *prot = newsk->sk_prot;
651
652 if (is_insidevar(prot, tcp_bpf_prots))
653 newsk->sk_prot = sk->sk_prot_creator;
654 }
655 #endif /* CONFIG_BPF_SYSCALL */
656