3 #include <linux/filter.h>
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>
11 #include <linux/kernel.h>
12 #include <linux/tcp.h>
13 #include <linux/workqueue.h>
14 #include <linux/nospec.h>
15 #include <linux/cookie.h>
16 #include <linux/inet_diag.h>
17 #include <linux/sock_diag.h>
19 static const struct sock_diag_handler __rcu
*sock_diag_handlers
[AF_MAX
];
21 static const struct sock_diag_inet_compat __rcu
*inet_rcv_compat
;
23 static struct workqueue_struct
*broadcast_wq
;
25 DEFINE_COOKIE(sock_cookie
);
27 u64
__sock_gen_cookie(struct sock
*sk
)
29 u64 res
= atomic64_read(&sk
->sk_cookie
);
32 u64
new = gen_cookie_next(&sock_cookie
);
34 atomic64_cmpxchg(&sk
->sk_cookie
, res
, new);
36 /* Another thread might have changed sk_cookie before us. */
37 res
= atomic64_read(&sk
->sk_cookie
);
42 int sock_diag_check_cookie(struct sock
*sk
, const __u32
*cookie
)
46 if (cookie
[0] == INET_DIAG_NOCOOKIE
&& cookie
[1] == INET_DIAG_NOCOOKIE
)
49 res
= sock_gen_cookie(sk
);
50 if ((u32
)res
!= cookie
[0] || (u32
)(res
>> 32) != cookie
[1])
55 EXPORT_SYMBOL_GPL(sock_diag_check_cookie
);
57 void sock_diag_save_cookie(struct sock
*sk
, __u32
*cookie
)
59 u64 res
= sock_gen_cookie(sk
);
62 cookie
[1] = (u32
)(res
>> 32);
64 EXPORT_SYMBOL_GPL(sock_diag_save_cookie
);
66 int sock_diag_put_meminfo(struct sock
*sk
, struct sk_buff
*skb
, int attrtype
)
68 u32 mem
[SK_MEMINFO_VARS
];
70 sk_get_meminfo(sk
, mem
);
72 return nla_put(skb
, attrtype
, sizeof(mem
), &mem
);
74 EXPORT_SYMBOL_GPL(sock_diag_put_meminfo
);
76 int sock_diag_put_filterinfo(bool may_report_filterinfo
, struct sock
*sk
,
77 struct sk_buff
*skb
, int attrtype
)
79 struct sock_fprog_kern
*fprog
;
80 struct sk_filter
*filter
;
85 if (!may_report_filterinfo
) {
86 nla_reserve(skb
, attrtype
, 0);
91 filter
= rcu_dereference(sk
->sk_filter
);
95 fprog
= filter
->prog
->orig_prog
;
99 flen
= bpf_classic_proglen(fprog
);
101 attr
= nla_reserve(skb
, attrtype
, flen
);
107 memcpy(nla_data(attr
), fprog
->filter
, flen
);
112 EXPORT_SYMBOL(sock_diag_put_filterinfo
);
114 struct broadcast_sk
{
116 struct work_struct work
;
119 static size_t sock_diag_nlmsg_size(void)
121 return NLMSG_ALIGN(sizeof(struct inet_diag_msg
)
122 + nla_total_size(sizeof(u8
)) /* INET_DIAG_PROTOCOL */
123 + nla_total_size_64bit(sizeof(struct tcp_info
))); /* INET_DIAG_INFO */
126 static const struct sock_diag_handler
*sock_diag_lock_handler(int family
)
128 const struct sock_diag_handler
*handler
;
131 handler
= rcu_dereference(sock_diag_handlers
[family
]);
132 if (handler
&& !try_module_get(handler
->owner
))
139 static void sock_diag_unlock_handler(const struct sock_diag_handler
*handler
)
141 module_put(handler
->owner
);
144 static void sock_diag_broadcast_destroy_work(struct work_struct
*work
)
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
;
151 const enum sknetlink_groups group
= sock_diag_destroy_group(sk
);
154 WARN_ON(group
== SKNLGRP_NONE
);
156 skb
= nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL
);
160 hndl
= sock_diag_lock_handler(sk
->sk_family
);
163 err
= hndl
->get_info(skb
, sk
);
164 sock_diag_unlock_handler(hndl
);
167 nlmsg_multicast(sock_net(sk
)->diag_nlsk
, skb
, 0, group
,
176 void sock_diag_broadcast_destroy(struct sock
*sk
)
178 /* Note, this function is often called from an interrupt context. */
179 struct broadcast_sk
*bsk
=
180 kmalloc(sizeof(struct broadcast_sk
), GFP_ATOMIC
);
182 return sk_destruct(sk
);
184 INIT_WORK(&bsk
->work
, sock_diag_broadcast_destroy_work
);
185 queue_work(broadcast_wq
, &bsk
->work
);
188 void sock_diag_register_inet_compat(const struct sock_diag_inet_compat
*ptr
)
190 xchg(&inet_rcv_compat
, RCU_INITIALIZER(ptr
));
192 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat
);
194 void sock_diag_unregister_inet_compat(const struct sock_diag_inet_compat
*ptr
)
196 const struct sock_diag_inet_compat
*old
;
198 old
= unrcu_pointer(xchg(&inet_rcv_compat
, NULL
));
199 WARN_ON_ONCE(old
!= ptr
);
201 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat
);
203 int sock_diag_register(const struct sock_diag_handler
*hndl
)
205 int family
= hndl
->family
;
207 if (family
>= AF_MAX
)
210 return !cmpxchg((const struct sock_diag_handler
**)
211 &sock_diag_handlers
[family
],
212 NULL
, hndl
) ? 0 : -EBUSY
;
214 EXPORT_SYMBOL_GPL(sock_diag_register
);
216 void sock_diag_unregister(const struct sock_diag_handler
*hndl
)
218 int family
= hndl
->family
;
220 if (family
>= AF_MAX
)
223 xchg((const struct sock_diag_handler
**)&sock_diag_handlers
[family
],
226 EXPORT_SYMBOL_GPL(sock_diag_unregister
);
228 static int __sock_diag_cmd(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
231 struct sock_diag_req
*req
= nlmsg_data(nlh
);
232 const struct sock_diag_handler
*hndl
;
234 if (nlmsg_len(nlh
) < sizeof(*req
))
237 if (req
->sdiag_family
>= AF_MAX
)
239 req
->sdiag_family
= array_index_nospec(req
->sdiag_family
, AF_MAX
);
241 if (!rcu_access_pointer(sock_diag_handlers
[req
->sdiag_family
]))
242 sock_load_diag_module(req
->sdiag_family
, 0);
244 hndl
= sock_diag_lock_handler(req
->sdiag_family
);
248 if (nlh
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
)
249 err
= hndl
->dump(skb
, nlh
);
250 else if (nlh
->nlmsg_type
== SOCK_DESTROY
&& hndl
->destroy
)
251 err
= hndl
->destroy(skb
, nlh
);
254 sock_diag_unlock_handler(hndl
);
259 static int sock_diag_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
260 struct netlink_ext_ack
*extack
)
262 const struct sock_diag_inet_compat
*ptr
;
265 switch (nlh
->nlmsg_type
) {
266 case TCPDIAG_GETSOCK
:
267 case DCCPDIAG_GETSOCK
:
269 if (!rcu_access_pointer(inet_rcv_compat
))
270 sock_load_diag_module(AF_INET
, 0);
273 ptr
= rcu_dereference(inet_rcv_compat
);
274 if (ptr
&& !try_module_get(ptr
->owner
))
280 ret
= ptr
->fn(skb
, nlh
);
281 module_put(ptr
->owner
);
285 case SOCK_DIAG_BY_FAMILY
:
287 return __sock_diag_cmd(skb
, nlh
);
293 static void sock_diag_rcv(struct sk_buff
*skb
)
295 netlink_rcv_skb(skb
, &sock_diag_rcv_msg
);
298 static int sock_diag_bind(struct net
*net
, int group
)
301 case SKNLGRP_INET_TCP_DESTROY
:
302 case SKNLGRP_INET_UDP_DESTROY
:
303 if (!rcu_access_pointer(sock_diag_handlers
[AF_INET
]))
304 sock_load_diag_module(AF_INET
, 0);
306 case SKNLGRP_INET6_TCP_DESTROY
:
307 case SKNLGRP_INET6_UDP_DESTROY
:
308 if (!rcu_access_pointer(sock_diag_handlers
[AF_INET6
]))
309 sock_load_diag_module(AF_INET6
, 0);
315 int sock_diag_destroy(struct sock
*sk
, int err
)
317 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
320 if (!sk
->sk_prot
->diag_destroy
)
323 return sk
->sk_prot
->diag_destroy(sk
, err
);
325 EXPORT_SYMBOL_GPL(sock_diag_destroy
);
327 static int __net_init
diag_net_init(struct net
*net
)
329 struct netlink_kernel_cfg cfg
= {
330 .groups
= SKNLGRP_MAX
,
331 .input
= sock_diag_rcv
,
332 .bind
= sock_diag_bind
,
333 .flags
= NL_CFG_F_NONROOT_RECV
,
336 net
->diag_nlsk
= netlink_kernel_create(net
, NETLINK_SOCK_DIAG
, &cfg
);
337 return net
->diag_nlsk
== NULL
? -ENOMEM
: 0;
340 static void __net_exit
diag_net_exit(struct net
*net
)
342 netlink_kernel_release(net
->diag_nlsk
);
343 net
->diag_nlsk
= NULL
;
346 static struct pernet_operations diag_net_ops
= {
347 .init
= diag_net_init
,
348 .exit
= diag_net_exit
,
351 static int __init
sock_diag_init(void)
353 broadcast_wq
= alloc_workqueue("sock_diag_events", 0, 0);
354 BUG_ON(!broadcast_wq
);
355 return register_pernet_subsys(&diag_net_ops
);
357 device_initcall(sock_diag_init
);