3 #include <linux/mutex.h>
4 #include <linux/socket.h>
5 #include <linux/skbuff.h>
6 #include <net/netlink.h>
7 #include <net/net_namespace.h>
8 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/tcp.h>
12 #include <linux/workqueue.h>
13 #include <linux/nospec.h>
15 #include <linux/inet_diag.h>
16 #include <linux/sock_diag.h>
18 static const struct sock_diag_handler
*sock_diag_handlers
[AF_MAX
];
19 static int (*inet_rcv_compat
)(struct sk_buff
*skb
, struct nlmsghdr
*nlh
);
20 static DEFINE_MUTEX(sock_diag_table_mutex
);
21 static struct workqueue_struct
*broadcast_wq
;
23 u64
sock_gen_cookie(struct sock
*sk
)
26 u64 res
= atomic64_read(&sk
->sk_cookie
);
30 res
= atomic64_inc_return(&sock_net(sk
)->cookie_gen
);
31 atomic64_cmpxchg(&sk
->sk_cookie
, 0, res
);
35 int sock_diag_check_cookie(struct sock
*sk
, const __u32
*cookie
)
39 if (cookie
[0] == INET_DIAG_NOCOOKIE
&& cookie
[1] == INET_DIAG_NOCOOKIE
)
42 res
= sock_gen_cookie(sk
);
43 if ((u32
)res
!= cookie
[0] || (u32
)(res
>> 32) != cookie
[1])
48 EXPORT_SYMBOL_GPL(sock_diag_check_cookie
);
50 void sock_diag_save_cookie(struct sock
*sk
, __u32
*cookie
)
52 u64 res
= sock_gen_cookie(sk
);
55 cookie
[1] = (u32
)(res
>> 32);
57 EXPORT_SYMBOL_GPL(sock_diag_save_cookie
);
59 int sock_diag_put_meminfo(struct sock
*sk
, struct sk_buff
*skb
, int attrtype
)
61 u32 mem
[SK_MEMINFO_VARS
];
63 sk_get_meminfo(sk
, mem
);
65 return nla_put(skb
, attrtype
, sizeof(mem
), &mem
);
67 EXPORT_SYMBOL_GPL(sock_diag_put_meminfo
);
69 int sock_diag_put_filterinfo(bool may_report_filterinfo
, struct sock
*sk
,
70 struct sk_buff
*skb
, int attrtype
)
72 struct sock_fprog_kern
*fprog
;
73 struct sk_filter
*filter
;
78 if (!may_report_filterinfo
) {
79 nla_reserve(skb
, attrtype
, 0);
84 filter
= rcu_dereference(sk
->sk_filter
);
88 fprog
= filter
->prog
->orig_prog
;
92 flen
= bpf_classic_proglen(fprog
);
94 attr
= nla_reserve(skb
, attrtype
, flen
);
100 memcpy(nla_data(attr
), fprog
->filter
, flen
);
105 EXPORT_SYMBOL(sock_diag_put_filterinfo
);
107 struct broadcast_sk
{
109 struct work_struct work
;
112 static size_t sock_diag_nlmsg_size(void)
114 return NLMSG_ALIGN(sizeof(struct inet_diag_msg
)
115 + nla_total_size(sizeof(u8
)) /* INET_DIAG_PROTOCOL */
116 + nla_total_size_64bit(sizeof(struct tcp_info
))); /* INET_DIAG_INFO */
119 static void sock_diag_broadcast_destroy_work(struct work_struct
*work
)
121 struct broadcast_sk
*bsk
=
122 container_of(work
, struct broadcast_sk
, work
);
123 struct sock
*sk
= bsk
->sk
;
124 const struct sock_diag_handler
*hndl
;
126 const enum sknetlink_groups group
= sock_diag_destroy_group(sk
);
129 WARN_ON(group
== SKNLGRP_NONE
);
131 skb
= nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL
);
135 mutex_lock(&sock_diag_table_mutex
);
136 hndl
= sock_diag_handlers
[sk
->sk_family
];
137 if (hndl
&& hndl
->get_info
)
138 err
= hndl
->get_info(skb
, sk
);
139 mutex_unlock(&sock_diag_table_mutex
);
142 nlmsg_multicast(sock_net(sk
)->diag_nlsk
, skb
, 0, group
,
151 void sock_diag_broadcast_destroy(struct sock
*sk
)
153 /* Note, this function is often called from an interrupt context. */
154 struct broadcast_sk
*bsk
=
155 kmalloc(sizeof(struct broadcast_sk
), GFP_ATOMIC
);
157 return sk_destruct(sk
);
159 INIT_WORK(&bsk
->work
, sock_diag_broadcast_destroy_work
);
160 queue_work(broadcast_wq
, &bsk
->work
);
163 void sock_diag_register_inet_compat(int (*fn
)(struct sk_buff
*skb
, struct nlmsghdr
*nlh
))
165 mutex_lock(&sock_diag_table_mutex
);
166 inet_rcv_compat
= fn
;
167 mutex_unlock(&sock_diag_table_mutex
);
169 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat
);
171 void sock_diag_unregister_inet_compat(int (*fn
)(struct sk_buff
*skb
, struct nlmsghdr
*nlh
))
173 mutex_lock(&sock_diag_table_mutex
);
174 inet_rcv_compat
= NULL
;
175 mutex_unlock(&sock_diag_table_mutex
);
177 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat
);
179 int sock_diag_register(const struct sock_diag_handler
*hndl
)
183 if (hndl
->family
>= AF_MAX
)
186 mutex_lock(&sock_diag_table_mutex
);
187 if (sock_diag_handlers
[hndl
->family
])
190 sock_diag_handlers
[hndl
->family
] = hndl
;
191 mutex_unlock(&sock_diag_table_mutex
);
195 EXPORT_SYMBOL_GPL(sock_diag_register
);
197 void sock_diag_unregister(const struct sock_diag_handler
*hnld
)
199 int family
= hnld
->family
;
201 if (family
>= AF_MAX
)
204 mutex_lock(&sock_diag_table_mutex
);
205 BUG_ON(sock_diag_handlers
[family
] != hnld
);
206 sock_diag_handlers
[family
] = NULL
;
207 mutex_unlock(&sock_diag_table_mutex
);
209 EXPORT_SYMBOL_GPL(sock_diag_unregister
);
211 static int __sock_diag_cmd(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
214 struct sock_diag_req
*req
= nlmsg_data(nlh
);
215 const struct sock_diag_handler
*hndl
;
217 if (nlmsg_len(nlh
) < sizeof(*req
))
220 if (req
->sdiag_family
>= AF_MAX
)
222 req
->sdiag_family
= array_index_nospec(req
->sdiag_family
, AF_MAX
);
224 if (sock_diag_handlers
[req
->sdiag_family
] == NULL
)
225 sock_load_diag_module(req
->sdiag_family
, 0);
227 mutex_lock(&sock_diag_table_mutex
);
228 hndl
= sock_diag_handlers
[req
->sdiag_family
];
231 else if (nlh
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
)
232 err
= hndl
->dump(skb
, nlh
);
233 else if (nlh
->nlmsg_type
== SOCK_DESTROY
&& hndl
->destroy
)
234 err
= hndl
->destroy(skb
, nlh
);
237 mutex_unlock(&sock_diag_table_mutex
);
242 static int sock_diag_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
243 struct netlink_ext_ack
*extack
)
247 switch (nlh
->nlmsg_type
) {
248 case TCPDIAG_GETSOCK
:
249 case DCCPDIAG_GETSOCK
:
250 if (inet_rcv_compat
== NULL
)
251 sock_load_diag_module(AF_INET
, 0);
253 mutex_lock(&sock_diag_table_mutex
);
254 if (inet_rcv_compat
!= NULL
)
255 ret
= inet_rcv_compat(skb
, nlh
);
258 mutex_unlock(&sock_diag_table_mutex
);
261 case SOCK_DIAG_BY_FAMILY
:
263 return __sock_diag_cmd(skb
, nlh
);
269 static DEFINE_MUTEX(sock_diag_mutex
);
271 static void sock_diag_rcv(struct sk_buff
*skb
)
273 mutex_lock(&sock_diag_mutex
);
274 netlink_rcv_skb(skb
, &sock_diag_rcv_msg
);
275 mutex_unlock(&sock_diag_mutex
);
278 static int sock_diag_bind(struct net
*net
, int group
)
281 case SKNLGRP_INET_TCP_DESTROY
:
282 case SKNLGRP_INET_UDP_DESTROY
:
283 if (!sock_diag_handlers
[AF_INET
])
284 sock_load_diag_module(AF_INET
, 0);
286 case SKNLGRP_INET6_TCP_DESTROY
:
287 case SKNLGRP_INET6_UDP_DESTROY
:
288 if (!sock_diag_handlers
[AF_INET6
])
289 sock_load_diag_module(AF_INET6
, 0);
295 int sock_diag_destroy(struct sock
*sk
, int err
)
297 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
300 if (!sk
->sk_prot
->diag_destroy
)
303 return sk
->sk_prot
->diag_destroy(sk
, err
);
305 EXPORT_SYMBOL_GPL(sock_diag_destroy
);
307 static int __net_init
diag_net_init(struct net
*net
)
309 struct netlink_kernel_cfg cfg
= {
310 .groups
= SKNLGRP_MAX
,
311 .input
= sock_diag_rcv
,
312 .bind
= sock_diag_bind
,
313 .flags
= NL_CFG_F_NONROOT_RECV
,
316 net
->diag_nlsk
= netlink_kernel_create(net
, NETLINK_SOCK_DIAG
, &cfg
);
317 return net
->diag_nlsk
== NULL
? -ENOMEM
: 0;
320 static void __net_exit
diag_net_exit(struct net
*net
)
322 netlink_kernel_release(net
->diag_nlsk
);
323 net
->diag_nlsk
= NULL
;
326 static struct pernet_operations diag_net_ops
= {
327 .init
= diag_net_init
,
328 .exit
= diag_net_exit
,
331 static int __init
sock_diag_init(void)
333 broadcast_wq
= alloc_workqueue("sock_diag_events", 0, 0);
334 BUG_ON(!broadcast_wq
);
335 return register_pernet_subsys(&diag_net_ops
);
337 device_initcall(sock_diag_init
);