GNU Linux-libre 5.15.137-gnu
[releases.git] / net / unix / unix_bpf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Cong Wang <cong.wang@bytedance.com> */
3
4 #include <linux/skmsg.h>
5 #include <linux/bpf.h>
6 #include <net/sock.h>
7 #include <net/af_unix.h>
8
9 #define unix_sk_has_data(__sk, __psock)                                 \
10                 ({      !skb_queue_empty(&__sk->sk_receive_queue) ||    \
11                         !skb_queue_empty(&__psock->ingress_skb) ||      \
12                         !list_empty(&__psock->ingress_msg);             \
13                 })
14
15 static int unix_msg_wait_data(struct sock *sk, struct sk_psock *psock,
16                               long timeo)
17 {
18         DEFINE_WAIT_FUNC(wait, woken_wake_function);
19         struct unix_sock *u = unix_sk(sk);
20         int ret = 0;
21
22         if (sk->sk_shutdown & RCV_SHUTDOWN)
23                 return 1;
24
25         if (!timeo)
26                 return ret;
27
28         add_wait_queue(sk_sleep(sk), &wait);
29         sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
30         if (!unix_sk_has_data(sk, psock)) {
31                 mutex_unlock(&u->iolock);
32                 wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
33                 mutex_lock(&u->iolock);
34                 ret = unix_sk_has_data(sk, psock);
35         }
36         sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
37         remove_wait_queue(sk_sleep(sk), &wait);
38         return ret;
39 }
40
41 static int __unix_recvmsg(struct sock *sk, struct msghdr *msg,
42                           size_t len, int flags)
43 {
44         if (sk->sk_type == SOCK_DGRAM)
45                 return __unix_dgram_recvmsg(sk, msg, len, flags);
46         else
47                 return __unix_stream_recvmsg(sk, msg, len, flags);
48 }
49
50 static int unix_bpf_recvmsg(struct sock *sk, struct msghdr *msg,
51                             size_t len, int nonblock, int flags,
52                             int *addr_len)
53 {
54         struct unix_sock *u = unix_sk(sk);
55         struct sk_psock *psock;
56         int copied;
57
58         if (!len)
59                 return 0;
60
61         psock = sk_psock_get(sk);
62         if (unlikely(!psock))
63                 return __unix_recvmsg(sk, msg, len, flags);
64
65         mutex_lock(&u->iolock);
66         if (!skb_queue_empty(&sk->sk_receive_queue) &&
67             sk_psock_queue_empty(psock)) {
68                 mutex_unlock(&u->iolock);
69                 sk_psock_put(sk, psock);
70                 return __unix_recvmsg(sk, msg, len, flags);
71         }
72
73 msg_bytes_ready:
74         copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
75         if (!copied) {
76                 long timeo;
77                 int data;
78
79                 timeo = sock_rcvtimeo(sk, nonblock);
80                 data = unix_msg_wait_data(sk, psock, timeo);
81                 if (data) {
82                         if (!sk_psock_queue_empty(psock))
83                                 goto msg_bytes_ready;
84                         mutex_unlock(&u->iolock);
85                         sk_psock_put(sk, psock);
86                         return __unix_recvmsg(sk, msg, len, flags);
87                 }
88                 copied = -EAGAIN;
89         }
90         mutex_unlock(&u->iolock);
91         sk_psock_put(sk, psock);
92         return copied;
93 }
94
95 static struct proto *unix_dgram_prot_saved __read_mostly;
96 static DEFINE_SPINLOCK(unix_dgram_prot_lock);
97 static struct proto unix_dgram_bpf_prot;
98
99 static struct proto *unix_stream_prot_saved __read_mostly;
100 static DEFINE_SPINLOCK(unix_stream_prot_lock);
101 static struct proto unix_stream_bpf_prot;
102
103 static void unix_dgram_bpf_rebuild_protos(struct proto *prot, const struct proto *base)
104 {
105         *prot        = *base;
106         prot->close  = sock_map_close;
107         prot->recvmsg = unix_bpf_recvmsg;
108         prot->sock_is_readable = sk_msg_is_readable;
109 }
110
111 static void unix_stream_bpf_rebuild_protos(struct proto *prot,
112                                            const struct proto *base)
113 {
114         *prot        = *base;
115         prot->close  = sock_map_close;
116         prot->recvmsg = unix_bpf_recvmsg;
117         prot->sock_is_readable = sk_msg_is_readable;
118         prot->unhash  = sock_map_unhash;
119 }
120
121 static void unix_dgram_bpf_check_needs_rebuild(struct proto *ops)
122 {
123         if (unlikely(ops != smp_load_acquire(&unix_dgram_prot_saved))) {
124                 spin_lock_bh(&unix_dgram_prot_lock);
125                 if (likely(ops != unix_dgram_prot_saved)) {
126                         unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, ops);
127                         smp_store_release(&unix_dgram_prot_saved, ops);
128                 }
129                 spin_unlock_bh(&unix_dgram_prot_lock);
130         }
131 }
132
133 static void unix_stream_bpf_check_needs_rebuild(struct proto *ops)
134 {
135         if (unlikely(ops != smp_load_acquire(&unix_stream_prot_saved))) {
136                 spin_lock_bh(&unix_stream_prot_lock);
137                 if (likely(ops != unix_stream_prot_saved)) {
138                         unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, ops);
139                         smp_store_release(&unix_stream_prot_saved, ops);
140                 }
141                 spin_unlock_bh(&unix_stream_prot_lock);
142         }
143 }
144
145 int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
146 {
147         if (sk->sk_type != SOCK_DGRAM)
148                 return -EOPNOTSUPP;
149
150         if (restore) {
151                 sk->sk_write_space = psock->saved_write_space;
152                 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
153                 return 0;
154         }
155
156         unix_dgram_bpf_check_needs_rebuild(psock->sk_proto);
157         WRITE_ONCE(sk->sk_prot, &unix_dgram_bpf_prot);
158         return 0;
159 }
160
161 int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
162 {
163         if (restore) {
164                 sk->sk_write_space = psock->saved_write_space;
165                 WRITE_ONCE(sk->sk_prot, psock->sk_proto);
166                 return 0;
167         }
168
169         unix_stream_bpf_check_needs_rebuild(psock->sk_proto);
170         WRITE_ONCE(sk->sk_prot, &unix_stream_bpf_prot);
171         return 0;
172 }
173
174 void __init unix_bpf_build_proto(void)
175 {
176         unix_dgram_bpf_rebuild_protos(&unix_dgram_bpf_prot, &unix_dgram_proto);
177         unix_stream_bpf_rebuild_protos(&unix_stream_bpf_prot, &unix_stream_proto);
178
179 }