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>
10 #include <net/inet_common.h>
13 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
14 struct sk_msg *msg, u32 apply_bytes, int flags)
16 bool apply = apply_bytes;
17 struct scatterlist *sge;
22 tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
27 tmp->sg.start = msg->sg.start;
30 sge = sk_msg_elem(msg, i);
31 size = (apply && apply_bytes < sge->length) ?
32 apply_bytes : sge->length;
33 if (!sk_wmem_schedule(sk, size)) {
39 sk_mem_charge(sk, size);
40 sk_msg_xfer(tmp, msg, i, size);
43 get_page(sk_msg_page(tmp, i));
44 sk_msg_iter_var_next(i);
51 } while (i != msg->sg.end);
55 sk_psock_queue_msg(psock, tmp);
56 sk_psock_data_ready(sk, psock);
66 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
67 int flags, bool uncharge)
69 bool apply = apply_bytes;
70 struct scatterlist *sge;
78 sge = sk_msg_elem(msg, msg->sg.start);
79 size = (apply && apply_bytes < sge->length) ?
80 apply_bytes : sge->length;
84 tcp_rate_check_app_limited(sk);
86 has_tx_ulp = tls_sw_has_ctx_tx(sk);
88 flags |= MSG_SENDPAGE_NOPOLICY;
89 ret = kernel_sendpage_locked(sk,
90 page, off, size, flags);
92 ret = do_tcp_sendpages(sk, page, off, size, flags);
103 sk_mem_uncharge(sk, ret);
111 sk_msg_iter_next(msg, start);
112 sg_init_table(sge, 1);
113 if (msg->sg.start == msg->sg.end)
116 if (apply && !apply_bytes)
123 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
124 u32 apply_bytes, int flags, bool uncharge)
129 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
134 int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg,
135 u32 bytes, int flags)
137 bool ingress = sk_msg_to_ingress(msg);
138 struct sk_psock *psock = sk_psock_get(sk);
141 if (unlikely(!psock)) {
142 sk_msg_free(sk, msg);
145 ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
146 tcp_bpf_push_locked(sk, msg, bytes, flags, false);
147 sk_psock_put(sk, psock);
150 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
152 #ifdef CONFIG_BPF_SYSCALL
153 static bool tcp_bpf_stream_read(const struct sock *sk)
155 struct sk_psock *psock;
159 psock = sk_psock(sk);
161 empty = list_empty(&psock->ingress_msg);
166 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
169 DEFINE_WAIT_FUNC(wait, woken_wake_function);
172 if (sk->sk_shutdown & RCV_SHUTDOWN)
178 add_wait_queue(sk_sleep(sk), &wait);
179 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
180 ret = sk_wait_event(sk, &timeo,
181 !list_empty(&psock->ingress_msg) ||
182 !skb_queue_empty(&sk->sk_receive_queue), &wait);
183 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
184 remove_wait_queue(sk_sleep(sk), &wait);
188 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
189 int nonblock, int flags, int *addr_len)
191 struct sk_psock *psock;
194 if (unlikely(flags & MSG_ERRQUEUE))
195 return inet_recv_error(sk, msg, len, addr_len);
197 psock = sk_psock_get(sk);
198 if (unlikely(!psock))
199 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
200 if (!skb_queue_empty(&sk->sk_receive_queue) &&
201 sk_psock_queue_empty(psock)) {
202 sk_psock_put(sk, psock);
203 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
207 copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
212 timeo = sock_rcvtimeo(sk, nonblock);
213 data = tcp_msg_wait_data(sk, psock, timeo);
215 if (!sk_psock_queue_empty(psock))
216 goto msg_bytes_ready;
218 sk_psock_put(sk, psock);
219 return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
225 sk_psock_put(sk, psock);
229 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
230 struct sk_msg *msg, int *copied, int flags)
232 bool cork = false, enospc = sk_msg_full(msg);
233 struct sock *sk_redir;
234 u32 tosend, delta = 0;
238 if (psock->eval == __SK_NONE) {
239 /* Track delta in msg size to add/subtract it on SK_DROP from
240 * returned to user copied size. This ensures user doesn't
241 * get a positive return code with msg_cut_data and SK_DROP
244 delta = msg->sg.size;
245 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
246 delta -= msg->sg.size;
249 if (msg->cork_bytes &&
250 msg->cork_bytes > msg->sg.size && !enospc) {
251 psock->cork_bytes = msg->cork_bytes - msg->sg.size;
253 psock->cork = kzalloc(sizeof(*psock->cork),
254 GFP_ATOMIC | __GFP_NOWARN);
258 memcpy(psock->cork, msg, sizeof(*msg));
262 tosend = msg->sg.size;
263 if (psock->apply_bytes && psock->apply_bytes < tosend)
264 tosend = psock->apply_bytes;
266 switch (psock->eval) {
268 ret = tcp_bpf_push(sk, msg, tosend, flags, true);
270 *copied -= sk_msg_free(sk, msg);
273 sk_msg_apply_bytes(psock, tosend);
276 sk_redir = psock->sk_redir;
277 sk_msg_apply_bytes(psock, tosend);
282 sk_msg_return(sk, msg, tosend);
284 ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
286 if (unlikely(ret < 0)) {
287 int free = sk_msg_free_nocharge(sk, msg);
293 sk_msg_free(sk, msg);
301 sk_msg_free_partial(sk, msg, tosend);
302 sk_msg_apply_bytes(psock, tosend);
303 *copied -= (tosend + delta);
308 if (!psock->apply_bytes) {
309 psock->eval = __SK_NONE;
310 if (psock->sk_redir) {
311 sock_put(psock->sk_redir);
312 psock->sk_redir = NULL;
316 msg->sg.data[msg->sg.start].page_link &&
317 msg->sg.data[msg->sg.start].length)
323 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
325 struct sk_msg tmp, *msg_tx = NULL;
326 int copied = 0, err = 0;
327 struct sk_psock *psock;
331 /* Don't let internal do_tcp_sendpages() flags through */
332 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
333 flags |= MSG_NO_SHARED_FRAGS;
335 psock = sk_psock_get(sk);
336 if (unlikely(!psock))
337 return tcp_sendmsg(sk, msg, size);
340 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
341 while (msg_data_left(msg)) {
350 copy = msg_data_left(msg);
351 if (!sk_stream_memory_free(sk))
352 goto wait_for_sndbuf;
354 msg_tx = psock->cork;
360 osize = msg_tx->sg.size;
361 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
364 goto wait_for_memory;
366 copy = msg_tx->sg.size - osize;
369 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
372 sk_msg_trim(sk, msg_tx, osize);
377 if (psock->cork_bytes) {
378 if (size > psock->cork_bytes)
379 psock->cork_bytes = 0;
381 psock->cork_bytes -= size;
382 if (psock->cork_bytes && !enospc)
384 /* All cork bytes are accounted, rerun the prog. */
385 psock->eval = __SK_NONE;
386 psock->cork_bytes = 0;
389 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
390 if (unlikely(err < 0))
394 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
396 err = sk_stream_wait_memory(sk, &timeo);
398 if (msg_tx && msg_tx != psock->cork)
399 sk_msg_free(sk, msg_tx);
405 err = sk_stream_error(sk, msg->msg_flags, err);
407 sk_psock_put(sk, psock);
408 return copied ? copied : err;
411 static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
412 size_t size, int flags)
414 struct sk_msg tmp, *msg = NULL;
415 int err = 0, copied = 0;
416 struct sk_psock *psock;
419 psock = sk_psock_get(sk);
420 if (unlikely(!psock))
421 return tcp_sendpage(sk, page, offset, size, flags);
431 /* Catch case where ring is full and sendpage is stalled. */
432 if (unlikely(sk_msg_full(msg)))
435 sk_msg_page_add(msg, page, size, offset);
436 sk_mem_charge(sk, size);
438 if (sk_msg_full(msg))
440 if (psock->cork_bytes) {
441 if (size > psock->cork_bytes)
442 psock->cork_bytes = 0;
444 psock->cork_bytes -= size;
445 if (psock->cork_bytes && !enospc)
447 /* All cork bytes are accounted, rerun the prog. */
448 psock->eval = __SK_NONE;
449 psock->cork_bytes = 0;
452 err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
455 sk_psock_put(sk, psock);
456 return copied ? copied : err;
471 static struct proto *tcpv6_prot_saved __read_mostly;
472 static DEFINE_SPINLOCK(tcpv6_prot_lock);
473 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
475 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
478 prot[TCP_BPF_BASE] = *base;
479 prot[TCP_BPF_BASE].unhash = sock_map_unhash;
480 prot[TCP_BPF_BASE].close = sock_map_close;
481 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
482 prot[TCP_BPF_BASE].stream_memory_read = tcp_bpf_stream_read;
484 prot[TCP_BPF_TX] = prot[TCP_BPF_BASE];
485 prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg;
486 prot[TCP_BPF_TX].sendpage = tcp_bpf_sendpage;
489 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
491 if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
492 spin_lock_bh(&tcpv6_prot_lock);
493 if (likely(ops != tcpv6_prot_saved)) {
494 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
495 smp_store_release(&tcpv6_prot_saved, ops);
497 spin_unlock_bh(&tcpv6_prot_lock);
501 static int __init tcp_bpf_v4_build_proto(void)
503 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
506 late_initcall(tcp_bpf_v4_build_proto);
508 static int tcp_bpf_assert_proto_ops(struct proto *ops)
510 /* In order to avoid retpoline, we make assumptions when we call
511 * into ops if e.g. a psock is not present. Make sure they are
512 * indeed valid assumptions.
514 return ops->recvmsg == tcp_recvmsg &&
515 ops->sendmsg == tcp_sendmsg &&
516 ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
519 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
521 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
522 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE;
525 if (inet_csk_has_ulp(sk)) {
526 /* TLS does not have an unhash proto in SW cases,
527 * but we need to ensure we stop using the sock_map
528 * unhash routine because the associated psock is being
529 * removed. So use the original unhash handler.
531 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
532 tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
534 sk->sk_write_space = psock->saved_write_space;
535 /* Pairs with lockless read in sk_clone_lock() */
536 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
541 if (inet_csk_has_ulp(sk))
544 if (sk->sk_family == AF_INET6) {
545 if (tcp_bpf_assert_proto_ops(psock->sk_proto))
548 tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
551 /* Pairs with lockless read in sk_clone_lock() */
552 WRITE_ONCE(sk->sk_prot, &tcp_bpf_prots[family][config]);
555 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
557 /* If a child got cloned from a listening socket that had tcp_bpf
558 * protocol callbacks installed, we need to restore the callbacks to
559 * the default ones because the child does not inherit the psock state
560 * that tcp_bpf callbacks expect.
562 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
564 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
565 struct proto *prot = newsk->sk_prot;
567 if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
568 newsk->sk_prot = sk->sk_prot_creator;
570 #endif /* CONFIG_BPF_SYSCALL */