Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2020 Cloudflare Ltd https://cloudflare.com */ |
| 3 | |
| 4 | #include <linux/skmsg.h> |
| 5 | #include <net/sock.h> |
| 6 | #include <net/udp.h> |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 7 | #include <net/inet_common.h> |
| 8 | |
| 9 | #include "udp_impl.h" |
| 10 | |
| 11 | static struct proto *udpv6_prot_saved __read_mostly; |
| 12 | |
| 13 | static int sk_udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 14 | int noblock, int flags, int *addr_len) |
| 15 | { |
| 16 | #if IS_ENABLED(CONFIG_IPV6) |
| 17 | if (sk->sk_family == AF_INET6) |
| 18 | return udpv6_prot_saved->recvmsg(sk, msg, len, noblock, flags, |
| 19 | addr_len); |
| 20 | #endif |
| 21 | return udp_prot.recvmsg(sk, msg, len, noblock, flags, addr_len); |
| 22 | } |
| 23 | |
Cong Wang | 9f2470f | 2021-06-14 19:13:35 -0700 | [diff] [blame] | 24 | static bool udp_sk_has_data(struct sock *sk) |
| 25 | { |
| 26 | return !skb_queue_empty(&udp_sk(sk)->reader_queue) || |
| 27 | !skb_queue_empty(&sk->sk_receive_queue); |
| 28 | } |
| 29 | |
| 30 | static bool psock_has_data(struct sk_psock *psock) |
| 31 | { |
| 32 | return !skb_queue_empty(&psock->ingress_skb) || |
| 33 | !sk_psock_queue_empty(psock); |
| 34 | } |
| 35 | |
| 36 | #define udp_msg_has_data(__sk, __psock) \ |
| 37 | ({ udp_sk_has_data(__sk) || psock_has_data(__psock); }) |
| 38 | |
Jakub Kicinski | b6df007 | 2021-06-29 15:45:27 -0700 | [diff] [blame] | 39 | static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock, |
| 40 | long timeo) |
Cong Wang | 9f2470f | 2021-06-14 19:13:35 -0700 | [diff] [blame] | 41 | { |
| 42 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 43 | int ret = 0; |
| 44 | |
| 45 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 46 | return 1; |
| 47 | |
| 48 | if (!timeo) |
| 49 | return ret; |
| 50 | |
| 51 | add_wait_queue(sk_sleep(sk), &wait); |
| 52 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 53 | ret = udp_msg_has_data(sk, psock); |
| 54 | if (!ret) { |
| 55 | wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); |
| 56 | ret = udp_msg_has_data(sk, psock); |
| 57 | } |
| 58 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 59 | remove_wait_queue(sk_sleep(sk), &wait); |
| 60 | return ret; |
| 61 | } |
| 62 | |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 63 | static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 64 | int nonblock, int flags, int *addr_len) |
| 65 | { |
| 66 | struct sk_psock *psock; |
| 67 | int copied, ret; |
| 68 | |
| 69 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 70 | return inet_recv_error(sk, msg, len, addr_len); |
| 71 | |
| 72 | psock = sk_psock_get(sk); |
| 73 | if (unlikely(!psock)) |
| 74 | return sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
| 75 | |
Cong Wang | 9f2470f | 2021-06-14 19:13:35 -0700 | [diff] [blame] | 76 | if (!psock_has_data(psock)) { |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 77 | ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
| 78 | goto out; |
| 79 | } |
| 80 | |
| 81 | msg_bytes_ready: |
| 82 | copied = sk_msg_recvmsg(sk, psock, msg, len, flags); |
| 83 | if (!copied) { |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 84 | long timeo; |
Cong Wang | c49661a | 2021-05-16 19:23:48 -0700 | [diff] [blame] | 85 | int data; |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 86 | |
| 87 | timeo = sock_rcvtimeo(sk, nonblock); |
Jakub Kicinski | b6df007 | 2021-06-29 15:45:27 -0700 | [diff] [blame] | 88 | data = udp_msg_wait_data(sk, psock, timeo); |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 89 | if (data) { |
Cong Wang | 9f2470f | 2021-06-14 19:13:35 -0700 | [diff] [blame] | 90 | if (psock_has_data(psock)) |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 91 | goto msg_bytes_ready; |
| 92 | ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); |
| 93 | goto out; |
| 94 | } |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 95 | copied = -EAGAIN; |
| 96 | } |
| 97 | ret = copied; |
| 98 | out: |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 99 | sk_psock_put(sk, psock); |
| 100 | return ret; |
| 101 | } |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 102 | |
| 103 | enum { |
| 104 | UDP_BPF_IPV4, |
| 105 | UDP_BPF_IPV6, |
| 106 | UDP_BPF_NUM_PROTS, |
| 107 | }; |
| 108 | |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 109 | static DEFINE_SPINLOCK(udpv6_prot_lock); |
| 110 | static struct proto udp_bpf_prots[UDP_BPF_NUM_PROTS]; |
| 111 | |
| 112 | static void udp_bpf_rebuild_protos(struct proto *prot, const struct proto *base) |
| 113 | { |
| 114 | *prot = *base; |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 115 | prot->close = sock_map_close; |
Cong Wang | 1f5be6b | 2021-03-30 19:32:34 -0700 | [diff] [blame] | 116 | prot->recvmsg = udp_bpf_recvmsg; |
Cong Wang | af49338 | 2021-10-08 13:33:05 -0700 | [diff] [blame] | 117 | prot->sock_is_readable = sk_msg_is_readable; |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 120 | static void udp_bpf_check_v6_needs_rebuild(struct proto *ops) |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 121 | { |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 122 | if (unlikely(ops != smp_load_acquire(&udpv6_prot_saved))) { |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 123 | spin_lock_bh(&udpv6_prot_lock); |
| 124 | if (likely(ops != udpv6_prot_saved)) { |
| 125 | udp_bpf_rebuild_protos(&udp_bpf_prots[UDP_BPF_IPV6], ops); |
| 126 | smp_store_release(&udpv6_prot_saved, ops); |
| 127 | } |
| 128 | spin_unlock_bh(&udpv6_prot_lock); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static int __init udp_bpf_v4_build_proto(void) |
| 133 | { |
| 134 | udp_bpf_rebuild_protos(&udp_bpf_prots[UDP_BPF_IPV4], &udp_prot); |
| 135 | return 0; |
| 136 | } |
Jakub Sitnicki | 54ea2f4 | 2021-07-14 17:47:50 +0200 | [diff] [blame] | 137 | late_initcall(udp_bpf_v4_build_proto); |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 138 | |
Cong Wang | 51e0158 | 2021-04-06 20:21:11 -0700 | [diff] [blame] | 139 | int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 140 | { |
| 141 | int family = sk->sk_family == AF_INET ? UDP_BPF_IPV4 : UDP_BPF_IPV6; |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 142 | |
| 143 | if (restore) { |
| 144 | sk->sk_write_space = psock->saved_write_space; |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 145 | WRITE_ONCE(sk->sk_prot, psock->sk_proto); |
| 146 | return 0; |
| 147 | } |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 148 | |
Lorenz Bauer | 7b219da | 2020-08-21 11:29:43 +0100 | [diff] [blame] | 149 | if (sk->sk_family == AF_INET6) |
| 150 | udp_bpf_check_v6_needs_rebuild(psock->sk_proto); |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 151 | |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 152 | WRITE_ONCE(sk->sk_prot, &udp_bpf_prots[family]); |
| 153 | return 0; |
Lorenz Bauer | edc6741c | 2020-03-09 11:12:38 +0000 | [diff] [blame] | 154 | } |
Cong Wang | 8a59f9d | 2021-03-30 19:32:31 -0700 | [diff] [blame] | 155 | EXPORT_SYMBOL_GPL(udp_bpf_update_proto); |