Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / net / core / sock_diag.c
CommitLineData
b6191aee
PG
1/* License: GPL */
2
b6459415 3#include <linux/filter.h>
8ef874bf
PE
4#include <linux/mutex.h>
5#include <linux/socket.h>
6#include <linux/skbuff.h>
7#include <net/netlink.h>
8#include <net/net_namespace.h>
9#include <linux/module.h>
5d2e5f27 10#include <net/sock.h>
eb4cb008
CG
11#include <linux/kernel.h>
12#include <linux/tcp.h>
13#include <linux/workqueue.h>
66b51b0a 14#include <linux/nospec.h>
92acdc58 15#include <linux/cookie.h>
8ef874bf
PE
16#include <linux/inet_diag.h>
17#include <linux/sock_diag.h>
18
1d55a697 19static const struct sock_diag_handler __rcu *sock_diag_handlers[AF_MAX];
86e8921d 20
b4cb4a13 21static const struct sock_diag_inet_compat __rcu *inet_rcv_compat;
86e8921d 22
eb4cb008 23static struct workqueue_struct *broadcast_wq;
8ef874bf 24
92acdc58
DB
25DEFINE_COOKIE(sock_cookie);
26
27u64 __sock_gen_cookie(struct sock *sk)
f65c1b53 28{
4ebf802c 29 u64 res = atomic64_read(&sk->sk_cookie);
33cf7c90 30
4ebf802c
ED
31 if (!res) {
32 u64 new = gen_cookie_next(&sock_cookie);
33
32634819
ED
34 atomic64_cmpxchg(&sk->sk_cookie, res, new);
35
36 /* Another thread might have changed sk_cookie before us. */
37 res = atomic64_read(&sk->sk_cookie);
33cf7c90 38 }
4ebf802c 39 return res;
33cf7c90
ED
40}
41
42int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
43{
44 u64 res;
45
46 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
f65c1b53 47 return 0;
33cf7c90
ED
48
49 res = sock_gen_cookie(sk);
50 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
51 return -ESTALE;
52
53 return 0;
f65c1b53
PE
54}
55EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
56
33cf7c90 57void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
f65c1b53 58{
33cf7c90
ED
59 u64 res = sock_gen_cookie(sk);
60
61 cookie[0] = (u32)res;
62 cookie[1] = (u32)(res >> 32);
f65c1b53
PE
63}
64EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
65
5d2e5f27
PE
66int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
67{
7b46866d 68 u32 mem[SK_MEMINFO_VARS];
5d2e5f27 69
a2d133b1 70 sk_get_meminfo(sk, mem);
5d2e5f27 71
7b46866d 72 return nla_put(skb, attrtype, sizeof(mem), &mem);
5d2e5f27
PE
73}
74EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
75
a53b72c8 76int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
e8d9612c
ND
77 struct sk_buff *skb, int attrtype)
78{
a3ea269b 79 struct sock_fprog_kern *fprog;
e8d9612c 80 struct sk_filter *filter;
a3ea269b
DB
81 struct nlattr *attr;
82 unsigned int flen;
e8d9612c
ND
83 int err = 0;
84
a53b72c8 85 if (!may_report_filterinfo) {
e8d9612c
ND
86 nla_reserve(skb, attrtype, 0);
87 return 0;
88 }
89
90 rcu_read_lock();
e8d9612c 91 filter = rcu_dereference(sk->sk_filter);
a3ea269b
DB
92 if (!filter)
93 goto out;
e8d9612c 94
7ae457c1 95 fprog = filter->prog->orig_prog;
b382c086
DB
96 if (!fprog)
97 goto out;
98
009937e7 99 flen = bpf_classic_proglen(fprog);
a3ea269b
DB
100
101 attr = nla_reserve(skb, attrtype, flen);
e8d9612c
ND
102 if (attr == NULL) {
103 err = -EMSGSIZE;
104 goto out;
105 }
106
a3ea269b 107 memcpy(nla_data(attr), fprog->filter, flen);
e8d9612c
ND
108out:
109 rcu_read_unlock();
110 return err;
111}
112EXPORT_SYMBOL(sock_diag_put_filterinfo);
113
eb4cb008
CG
114struct broadcast_sk {
115 struct sock *sk;
116 struct work_struct work;
117};
118
119static size_t sock_diag_nlmsg_size(void)
120{
121 return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
122 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
6ed46d12 123 + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
eb4cb008
CG
124}
125
1d55a697
ED
126static const struct sock_diag_handler *sock_diag_lock_handler(int family)
127{
128 const struct sock_diag_handler *handler;
129
130 rcu_read_lock();
131 handler = rcu_dereference(sock_diag_handlers[family]);
132 if (handler && !try_module_get(handler->owner))
133 handler = NULL;
134 rcu_read_unlock();
135
136 return handler;
137}
138
139static void sock_diag_unlock_handler(const struct sock_diag_handler *handler)
140{
141 module_put(handler->owner);
142}
143
eb4cb008
CG
144static void sock_diag_broadcast_destroy_work(struct work_struct *work)
145{
146 struct broadcast_sk *bsk =
147 container_of(work, struct broadcast_sk, work);
148 struct sock *sk = bsk->sk;
149 const struct sock_diag_handler *hndl;
150 struct sk_buff *skb;
151 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
152 int err = -1;
153
154 WARN_ON(group == SKNLGRP_NONE);
155
156 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
157 if (!skb)
158 goto out;
159
1d55a697
ED
160 hndl = sock_diag_lock_handler(sk->sk_family);
161 if (hndl) {
162 if (hndl->get_info)
163 err = hndl->get_info(skb, sk);
164 sock_diag_unlock_handler(hndl);
165 }
eb4cb008
CG
166 if (!err)
167 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
168 GFP_KERNEL);
169 else
170 kfree_skb(skb);
171out:
172 sk_destruct(sk);
173 kfree(bsk);
174}
175
176void sock_diag_broadcast_destroy(struct sock *sk)
177{
178 /* Note, this function is often called from an interrupt context. */
179 struct broadcast_sk *bsk =
180 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
181 if (!bsk)
182 return sk_destruct(sk);
183 bsk->sk = sk;
184 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
185 queue_work(broadcast_wq, &bsk->work);
186}
187
86e8921d 188void sock_diag_register_inet_compat(const struct sock_diag_inet_compat *ptr)
8ef874bf 189{
b4cb4a13 190 xchg(&inet_rcv_compat, RCU_INITIALIZER(ptr));
8ef874bf
PE
191}
192EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
193
86e8921d 194void sock_diag_unregister_inet_compat(const struct sock_diag_inet_compat *ptr)
8ef874bf 195{
86e8921d
ED
196 const struct sock_diag_inet_compat *old;
197
b4cb4a13 198 old = unrcu_pointer(xchg(&inet_rcv_compat, NULL));
86e8921d 199 WARN_ON_ONCE(old != ptr);
8ef874bf
PE
200}
201EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
202
8dcf01fc 203int sock_diag_register(const struct sock_diag_handler *hndl)
8ef874bf 204{
1d55a697 205 int family = hndl->family;
8ef874bf 206
1d55a697 207 if (family >= AF_MAX)
8ef874bf
PE
208 return -EINVAL;
209
1d55a697
ED
210 return !cmpxchg((const struct sock_diag_handler **)
211 &sock_diag_handlers[family],
212 NULL, hndl) ? 0 : -EBUSY;
8ef874bf
PE
213}
214EXPORT_SYMBOL_GPL(sock_diag_register);
215
1d55a697 216void sock_diag_unregister(const struct sock_diag_handler *hndl)
8ef874bf 217{
1d55a697 218 int family = hndl->family;
8ef874bf 219
6f8e4ad0 220 if (family >= AF_MAX)
8ef874bf
PE
221 return;
222
1d55a697
ED
223 xchg((const struct sock_diag_handler **)&sock_diag_handlers[family],
224 NULL);
8ef874bf
PE
225}
226EXPORT_SYMBOL_GPL(sock_diag_unregister);
227
64be0aed 228static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
8ef874bf
PE
229{
230 int err;
7b46866d 231 struct sock_diag_req *req = nlmsg_data(nlh);
8dcf01fc 232 const struct sock_diag_handler *hndl;
8ef874bf
PE
233
234 if (nlmsg_len(nlh) < sizeof(*req))
235 return -EINVAL;
236
6e601a53
MK
237 if (req->sdiag_family >= AF_MAX)
238 return -EINVAL;
66b51b0a 239 req->sdiag_family = array_index_nospec(req->sdiag_family, AF_MAX);
6e601a53 240
1d55a697 241 if (!rcu_access_pointer(sock_diag_handlers[req->sdiag_family]))
bf2ae2e4 242 sock_load_diag_module(req->sdiag_family, 0);
8e904550 243
1d55a697 244 hndl = sock_diag_lock_handler(req->sdiag_family);
8ef874bf 245 if (hndl == NULL)
1d55a697
ED
246 return -ENOENT;
247
248 if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
8ef874bf 249 err = hndl->dump(skb, nlh);
64be0aed
LC
250 else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
251 err = hndl->destroy(skb, nlh);
252 else
253 err = -EOPNOTSUPP;
1d55a697 254 sock_diag_unlock_handler(hndl);
8ef874bf
PE
255
256 return err;
257}
258
2d4bc933
JB
259static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
260 struct netlink_ext_ack *extack)
8ef874bf 261{
86e8921d 262 const struct sock_diag_inet_compat *ptr;
8ef874bf
PE
263 int ret;
264
265 switch (nlh->nlmsg_type) {
266 case TCPDIAG_GETSOCK:
267 case DCCPDIAG_GETSOCK:
86e8921d
ED
268
269 if (!rcu_access_pointer(inet_rcv_compat))
bf2ae2e4 270 sock_load_diag_module(AF_INET, 0);
8ef874bf 271
86e8921d
ED
272 rcu_read_lock();
273 ptr = rcu_dereference(inet_rcv_compat);
274 if (ptr && !try_module_get(ptr->owner))
275 ptr = NULL;
276 rcu_read_unlock();
277
278 ret = -EOPNOTSUPP;
279 if (ptr) {
280 ret = ptr->fn(skb, nlh);
281 module_put(ptr->owner);
282 }
8ef874bf
PE
283
284 return ret;
285 case SOCK_DIAG_BY_FAMILY:
64be0aed
LC
286 case SOCK_DESTROY:
287 return __sock_diag_cmd(skb, nlh);
8ef874bf
PE
288 default:
289 return -EINVAL;
290 }
291}
292
8ef874bf
PE
293static void sock_diag_rcv(struct sk_buff *skb)
294{
8ef874bf 295 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
8ef874bf
PE
296}
297
eb4cb008
CG
298static int sock_diag_bind(struct net *net, int group)
299{
300 switch (group) {
301 case SKNLGRP_INET_TCP_DESTROY:
302 case SKNLGRP_INET_UDP_DESTROY:
1d55a697 303 if (!rcu_access_pointer(sock_diag_handlers[AF_INET]))
bf2ae2e4 304 sock_load_diag_module(AF_INET, 0);
eb4cb008
CG
305 break;
306 case SKNLGRP_INET6_TCP_DESTROY:
307 case SKNLGRP_INET6_UDP_DESTROY:
1d55a697 308 if (!rcu_access_pointer(sock_diag_handlers[AF_INET6]))
bf2ae2e4 309 sock_load_diag_module(AF_INET6, 0);
eb4cb008
CG
310 break;
311 }
312 return 0;
313}
314
64be0aed
LC
315int sock_diag_destroy(struct sock *sk, int err)
316{
317 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
318 return -EPERM;
319
320 if (!sk->sk_prot->diag_destroy)
321 return -EOPNOTSUPP;
322
323 return sk->sk_prot->diag_destroy(sk, err);
324}
325EXPORT_SYMBOL_GPL(sock_diag_destroy);
326
51d7cccf 327static int __net_init diag_net_init(struct net *net)
8ef874bf 328{
a31f2d17 329 struct netlink_kernel_cfg cfg = {
eb4cb008 330 .groups = SKNLGRP_MAX,
a31f2d17 331 .input = sock_diag_rcv,
eb4cb008
CG
332 .bind = sock_diag_bind,
333 .flags = NL_CFG_F_NONROOT_RECV,
a31f2d17
PNA
334 };
335
9f00d977 336 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
51d7cccf
AV
337 return net->diag_nlsk == NULL ? -ENOMEM : 0;
338}
339
340static void __net_exit diag_net_exit(struct net *net)
341{
342 netlink_kernel_release(net->diag_nlsk);
343 net->diag_nlsk = NULL;
344}
345
346static struct pernet_operations diag_net_ops = {
347 .init = diag_net_init,
348 .exit = diag_net_exit,
349};
350
351static int __init sock_diag_init(void)
352{
eb4cb008
CG
353 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
354 BUG_ON(!broadcast_wq);
51d7cccf 355 return register_pernet_subsys(&diag_net_ops);
8ef874bf 356}
b6191aee 357device_initcall(sock_diag_init);