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