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