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> 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 24 static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 25 int nonblock, int flags, int *addr_len) 26 { 27 struct sk_psock *psock; 28 int copied, ret; 29 30 if (unlikely(flags & MSG_ERRQUEUE)) 31 return inet_recv_error(sk, msg, len, addr_len); 32 33 psock = sk_psock_get(sk); 34 if (unlikely(!psock)) 35 return sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); 36 37 lock_sock(sk); 38 if (sk_psock_queue_empty(psock)) { 39 ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); 40 goto out; 41 } 42 43 msg_bytes_ready: 44 copied = sk_msg_recvmsg(sk, psock, msg, len, flags); 45 if (!copied) { 46 long timeo; 47 int data; 48 49 timeo = sock_rcvtimeo(sk, nonblock); 50 data = sk_msg_wait_data(sk, psock, timeo); 51 if (data) { 52 if (!sk_psock_queue_empty(psock)) 53 goto msg_bytes_ready; 54 ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); 55 goto out; 56 } 57 copied = -EAGAIN; 58 } 59 ret = copied; 60 out: 61 release_sock(sk); 62 sk_psock_put(sk, psock); 63 return ret; 64 } 65 66 enum { 67 UDP_BPF_IPV4, 68 UDP_BPF_IPV6, 69 UDP_BPF_NUM_PROTS, 70 }; 71 72 static DEFINE_SPINLOCK(udpv6_prot_lock); 73 static struct proto udp_bpf_prots[UDP_BPF_NUM_PROTS]; 74 75 static void udp_bpf_rebuild_protos(struct proto *prot, const struct proto *base) 76 { 77 *prot = *base; 78 prot->unhash = sock_map_unhash; 79 prot->close = sock_map_close; 80 prot->recvmsg = udp_bpf_recvmsg; 81 } 82 83 static void udp_bpf_check_v6_needs_rebuild(struct proto *ops) 84 { 85 if (unlikely(ops != smp_load_acquire(&udpv6_prot_saved))) { 86 spin_lock_bh(&udpv6_prot_lock); 87 if (likely(ops != udpv6_prot_saved)) { 88 udp_bpf_rebuild_protos(&udp_bpf_prots[UDP_BPF_IPV6], ops); 89 smp_store_release(&udpv6_prot_saved, ops); 90 } 91 spin_unlock_bh(&udpv6_prot_lock); 92 } 93 } 94 95 static int __init udp_bpf_v4_build_proto(void) 96 { 97 udp_bpf_rebuild_protos(&udp_bpf_prots[UDP_BPF_IPV4], &udp_prot); 98 return 0; 99 } 100 core_initcall(udp_bpf_v4_build_proto); 101 102 int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) 103 { 104 int family = sk->sk_family == AF_INET ? UDP_BPF_IPV4 : UDP_BPF_IPV6; 105 106 if (restore) { 107 sk->sk_write_space = psock->saved_write_space; 108 WRITE_ONCE(sk->sk_prot, psock->sk_proto); 109 return 0; 110 } 111 112 if (sk->sk_family == AF_INET6) 113 udp_bpf_check_v6_needs_rebuild(psock->sk_proto); 114 115 WRITE_ONCE(sk->sk_prot, &udp_bpf_prots[family]); 116 return 0; 117 } 118 EXPORT_SYMBOL_GPL(udp_bpf_update_proto); 119