1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
4 #include <linux/skmsg.h>
5 #include <linux/filter.h>
7 #include <linux/init.h>
8 #include <linux/wait.h>
9 #include <linux/util_macros.h>
11 #include <net/inet_common.h>
14 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
15 struct sk_msg *msg, u32 apply_bytes, int flags)
17 bool apply = apply_bytes;
18 struct scatterlist *sge;
23 tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
28 tmp->sg.start = msg->sg.start;
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)) {
40 sk_mem_charge(sk, size);
41 sk_msg_xfer(tmp, msg, i, size);
44 get_page(sk_msg_page(tmp, i));
45 sk_msg_iter_var_next(i);
51 sk_msg_iter_var_prev(i);
55 } while (i != msg->sg.end);
59 sk_psock_queue_msg(psock, tmp);
60 sk_psock_data_ready(sk, psock);
70 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
71 int flags, bool uncharge)
73 bool apply = apply_bytes;
74 struct scatterlist *sge;
82 sge = sk_msg_elem(msg, msg->sg.start);
83 size = (apply && apply_bytes < sge->length) ?
84 apply_bytes : sge->length;
88 tcp_rate_check_app_limited(sk);
90 has_tx_ulp = tls_sw_has_ctx_tx(sk);
92 flags |= MSG_SENDPAGE_NOPOLICY;
93 ret = kernel_sendpage_locked(sk,
94 page, off, size, flags);
96 ret = do_tcp_sendpages(sk, page, off, size, flags);
107 sk_mem_uncharge(sk, ret);
115 sk_msg_iter_next(msg, start);
116 sg_init_table(sge, 1);
117 if (msg->sg.start == msg->sg.end)
120 if (apply && !apply_bytes)
127 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
128 u32 apply_bytes, int flags, bool uncharge)
133 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
138 int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
139 struct sk_msg *msg, u32 bytes, int flags)
141 struct sk_psock *psock = sk_psock_get(sk);
144 if (unlikely(!psock))
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);
152 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
154 #ifdef CONFIG_BPF_SYSCALL
155 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
158 DEFINE_WAIT_FUNC(wait, woken_wake_function);
161 if (sk->sk_shutdown & RCV_SHUTDOWN)
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(&sk->sk_receive_queue), &wait);
172 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
173 remove_wait_queue(sk_sleep(sk), &wait);
177 static int tcp_bpf_recvmsg_parser(struct sock *sk,
183 struct sk_psock *psock;
186 if (unlikely(flags & MSG_ERRQUEUE))
187 return inet_recv_error(sk, msg, len, addr_len);
192 psock = sk_psock_get(sk);
193 if (unlikely(!psock))
194 return tcp_recvmsg(sk, msg, len, flags, addr_len);
198 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
203 if (sock_flag(sk, SOCK_DONE))
207 copied = sock_error(sk);
211 if (sk->sk_shutdown & RCV_SHUTDOWN)
214 if (sk->sk_state == TCP_CLOSE) {
219 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
225 if (signal_pending(current)) {
226 copied = sock_intr_errno(timeo);
230 data = tcp_msg_wait_data(sk, psock, timeo);
231 if (data && !sk_psock_queue_empty(psock))
232 goto msg_bytes_ready;
237 sk_psock_put(sk, psock);
241 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
242 int flags, int *addr_len)
244 struct sk_psock *psock;
247 if (unlikely(flags & MSG_ERRQUEUE))
248 return inet_recv_error(sk, msg, len, addr_len);
253 psock = sk_psock_get(sk);
254 if (unlikely(!psock))
255 return tcp_recvmsg(sk, msg, len, flags, addr_len);
256 if (!skb_queue_empty(&sk->sk_receive_queue) &&
257 sk_psock_queue_empty(psock)) {
258 sk_psock_put(sk, psock);
259 return tcp_recvmsg(sk, msg, len, flags, addr_len);
263 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
268 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
269 data = tcp_msg_wait_data(sk, psock, timeo);
271 if (!sk_psock_queue_empty(psock))
272 goto msg_bytes_ready;
274 sk_psock_put(sk, psock);
275 return tcp_recvmsg(sk, msg, len, flags, addr_len);
281 sk_psock_put(sk, psock);
285 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
286 struct sk_msg *msg, int *copied, int flags)
288 bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
289 struct sock *sk_redir;
290 u32 tosend, origsize, sent, delta = 0;
295 if (psock->eval == __SK_NONE) {
296 /* Track delta in msg size to add/subtract it on SK_DROP from
297 * returned to user copied size. This ensures user doesn't
298 * get a positive return code with msg_cut_data and SK_DROP
301 delta = msg->sg.size;
302 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
303 delta -= msg->sg.size;
306 if (msg->cork_bytes &&
307 msg->cork_bytes > msg->sg.size && !enospc) {
308 psock->cork_bytes = msg->cork_bytes - msg->sg.size;
310 psock->cork = kzalloc(sizeof(*psock->cork),
311 GFP_ATOMIC | __GFP_NOWARN);
315 memcpy(psock->cork, msg, sizeof(*msg));
319 tosend = msg->sg.size;
320 if (psock->apply_bytes && psock->apply_bytes < tosend)
321 tosend = psock->apply_bytes;
324 switch (psock->eval) {
326 ret = tcp_bpf_push(sk, msg, tosend, flags, true);
328 *copied -= sk_msg_free(sk, msg);
331 sk_msg_apply_bytes(psock, tosend);
334 redir_ingress = psock->redir_ingress;
335 sk_redir = psock->sk_redir;
336 sk_msg_apply_bytes(psock, tosend);
337 if (!psock->apply_bytes) {
338 /* Clean up before releasing the sock lock. */
340 psock->eval = __SK_NONE;
341 psock->sk_redir = NULL;
347 sk_msg_return(sk, msg, tosend);
350 origsize = msg->sg.size;
351 ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
353 sent = origsize - msg->sg.size;
355 if (eval == __SK_REDIRECT)
359 if (unlikely(ret < 0)) {
360 int free = sk_msg_free_nocharge(sk, msg);
366 sk_msg_free(sk, msg);
374 sk_msg_free_partial(sk, msg, tosend);
375 sk_msg_apply_bytes(psock, tosend);
376 *copied -= (tosend + delta);
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;
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);
399 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
401 struct sk_msg tmp, *msg_tx = NULL;
402 int copied = 0, err = 0;
403 struct sk_psock *psock;
407 /* Don't let internal do_tcp_sendpages() flags through */
408 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
409 flags |= MSG_NO_SHARED_FRAGS;
411 psock = sk_psock_get(sk);
412 if (unlikely(!psock))
413 return tcp_sendmsg(sk, msg, size);
416 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
417 while (msg_data_left(msg)) {
426 copy = msg_data_left(msg);
427 if (!sk_stream_memory_free(sk))
428 goto wait_for_sndbuf;
430 msg_tx = psock->cork;
436 osize = msg_tx->sg.size;
437 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
440 goto wait_for_memory;
442 copy = msg_tx->sg.size - osize;
445 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
448 sk_msg_trim(sk, msg_tx, osize);
453 if (psock->cork_bytes) {
454 if (size > psock->cork_bytes)
455 psock->cork_bytes = 0;
457 psock->cork_bytes -= size;
458 if (psock->cork_bytes && !enospc)
460 /* All cork bytes are accounted, rerun the prog. */
461 psock->eval = __SK_NONE;
462 psock->cork_bytes = 0;
465 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
466 if (unlikely(err < 0))
470 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
472 err = sk_stream_wait_memory(sk, &timeo);
474 if (msg_tx && msg_tx != psock->cork)
475 sk_msg_free(sk, msg_tx);
481 err = sk_stream_error(sk, msg->msg_flags, err);
483 sk_psock_put(sk, psock);
484 return copied ? copied : err;
487 static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
488 size_t size, int flags)
490 struct sk_msg tmp, *msg = NULL;
491 int err = 0, copied = 0;
492 struct sk_psock *psock;
495 psock = sk_psock_get(sk);
496 if (unlikely(!psock))
497 return tcp_sendpage(sk, page, offset, size, flags);
507 /* Catch case where ring is full and sendpage is stalled. */
508 if (unlikely(sk_msg_full(msg)))
511 sk_msg_page_add(msg, page, size, offset);
512 sk_mem_charge(sk, size);
514 if (sk_msg_full(msg))
516 if (psock->cork_bytes) {
517 if (size > psock->cork_bytes)
518 psock->cork_bytes = 0;
520 psock->cork_bytes -= size;
521 if (psock->cork_bytes && !enospc)
523 /* All cork bytes are accounted, rerun the prog. */
524 psock->eval = __SK_NONE;
525 psock->cork_bytes = 0;
528 err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
531 sk_psock_put(sk, psock);
532 return copied ? copied : err;
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];
553 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
556 prot[TCP_BPF_BASE] = *base;
557 prot[TCP_BPF_BASE].destroy = sock_map_destroy;
558 prot[TCP_BPF_BASE].close = sock_map_close;
559 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
560 prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable;
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;
566 prot[TCP_BPF_RX] = prot[TCP_BPF_BASE];
567 prot[TCP_BPF_RX].recvmsg = tcp_bpf_recvmsg_parser;
569 prot[TCP_BPF_TXRX] = prot[TCP_BPF_TX];
570 prot[TCP_BPF_TXRX].recvmsg = tcp_bpf_recvmsg_parser;
573 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
575 if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
576 spin_lock_bh(&tcpv6_prot_lock);
577 if (likely(ops != tcpv6_prot_saved)) {
578 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
579 smp_store_release(&tcpv6_prot_saved, ops);
581 spin_unlock_bh(&tcpv6_prot_lock);
585 static int __init tcp_bpf_v4_build_proto(void)
587 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
590 late_initcall(tcp_bpf_v4_build_proto);
592 static int tcp_bpf_assert_proto_ops(struct proto *ops)
594 /* In order to avoid retpoline, we make assumptions when we call
595 * into ops if e.g. a psock is not present. Make sure they are
596 * indeed valid assumptions.
598 return ops->recvmsg == tcp_recvmsg &&
599 ops->sendmsg == tcp_sendmsg &&
600 ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
603 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
605 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
606 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
608 if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
609 config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
613 if (inet_csk_has_ulp(sk)) {
614 /* TLS does not have an unhash proto in SW cases,
615 * but we need to ensure we stop using the sock_map
616 * unhash routine because the associated psock is being
617 * removed. So use the original unhash handler.
619 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
620 tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
622 sk->sk_write_space = psock->saved_write_space;
623 /* Pairs with lockless read in sk_clone_lock() */
624 sock_replace_proto(sk, psock->sk_proto);
629 if (sk->sk_family == AF_INET6) {
630 if (tcp_bpf_assert_proto_ops(psock->sk_proto))
633 tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
636 /* Pairs with lockless read in sk_clone_lock() */
637 sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
640 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
642 /* If a child got cloned from a listening socket that had tcp_bpf
643 * protocol callbacks installed, we need to restore the callbacks to
644 * the default ones because the child does not inherit the psock state
645 * that tcp_bpf callbacks expect.
647 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
649 struct proto *prot = newsk->sk_prot;
651 if (is_insidevar(prot, tcp_bpf_prots))
652 newsk->sk_prot = sk->sk_prot_creator;
654 #endif /* CONFIG_BPF_SYSCALL */