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
;
22 static atomic64_t cookie_gen
;
24 u64
sock_gen_cookie(struct sock
*sk
)
27 u64 res
= atomic64_read(&sk
->sk_cookie
);
31 res
= atomic64_inc_return(&cookie_gen
);
32 atomic64_cmpxchg(&sk
->sk_cookie
, 0, res
);
36 int sock_diag_check_cookie(struct sock
*sk
, const __u32
*cookie
)
40 if (cookie
[0] == INET_DIAG_NOCOOKIE
&& cookie
[1] == INET_DIAG_NOCOOKIE
)
43 res
= sock_gen_cookie(sk
);
44 if ((u32
)res
!= cookie
[0] || (u32
)(res
>> 32) != cookie
[1])
49 EXPORT_SYMBOL_GPL(sock_diag_check_cookie
);
51 void sock_diag_save_cookie(struct sock
*sk
, __u32
*cookie
)
53 u64 res
= sock_gen_cookie(sk
);
56 cookie
[1] = (u32
)(res
>> 32);
58 EXPORT_SYMBOL_GPL(sock_diag_save_cookie
);
60 int sock_diag_put_meminfo(struct sock
*sk
, struct sk_buff
*skb
, int attrtype
)
62 u32 mem
[SK_MEMINFO_VARS
];
64 sk_get_meminfo(sk
, mem
);
66 return nla_put(skb
, attrtype
, sizeof(mem
), &mem
);
68 EXPORT_SYMBOL_GPL(sock_diag_put_meminfo
);
70 int sock_diag_put_filterinfo(bool may_report_filterinfo
, struct sock
*sk
,
71 struct sk_buff
*skb
, int attrtype
)
73 struct sock_fprog_kern
*fprog
;
74 struct sk_filter
*filter
;
79 if (!may_report_filterinfo
) {
80 nla_reserve(skb
, attrtype
, 0);
85 filter
= rcu_dereference(sk
->sk_filter
);
89 fprog
= filter
->prog
->orig_prog
;
93 flen
= bpf_classic_proglen(fprog
);
95 attr
= nla_reserve(skb
, attrtype
, flen
);
101 memcpy(nla_data(attr
), fprog
->filter
, flen
);
106 EXPORT_SYMBOL(sock_diag_put_filterinfo
);
108 struct broadcast_sk
{
110 struct work_struct work
;
113 static size_t sock_diag_nlmsg_size(void)
115 return NLMSG_ALIGN(sizeof(struct inet_diag_msg
)
116 + nla_total_size(sizeof(u8
)) /* INET_DIAG_PROTOCOL */
117 + nla_total_size_64bit(sizeof(struct tcp_info
))); /* INET_DIAG_INFO */
120 static void sock_diag_broadcast_destroy_work(struct work_struct
*work
)
122 struct broadcast_sk
*bsk
=
123 container_of(work
, struct broadcast_sk
, work
);
124 struct sock
*sk
= bsk
->sk
;
125 const struct sock_diag_handler
*hndl
;
127 const enum sknetlink_groups group
= sock_diag_destroy_group(sk
);
130 WARN_ON(group
== SKNLGRP_NONE
);
132 skb
= nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL
);
136 mutex_lock(&sock_diag_table_mutex
);
137 hndl
= sock_diag_handlers
[sk
->sk_family
];
138 if (hndl
&& hndl
->get_info
)
139 err
= hndl
->get_info(skb
, sk
);
140 mutex_unlock(&sock_diag_table_mutex
);
143 nlmsg_multicast(sock_net(sk
)->diag_nlsk
, skb
, 0, group
,
152 void sock_diag_broadcast_destroy(struct sock
*sk
)
154 /* Note, this function is often called from an interrupt context. */
155 struct broadcast_sk
*bsk
=
156 kmalloc(sizeof(struct broadcast_sk
), GFP_ATOMIC
);
158 return sk_destruct(sk
);
160 INIT_WORK(&bsk
->work
, sock_diag_broadcast_destroy_work
);
161 queue_work(broadcast_wq
, &bsk
->work
);
164 void sock_diag_register_inet_compat(int (*fn
)(struct sk_buff
*skb
, struct nlmsghdr
*nlh
))
166 mutex_lock(&sock_diag_table_mutex
);
167 inet_rcv_compat
= fn
;
168 mutex_unlock(&sock_diag_table_mutex
);
170 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat
);
172 void sock_diag_unregister_inet_compat(int (*fn
)(struct sk_buff
*skb
, struct nlmsghdr
*nlh
))
174 mutex_lock(&sock_diag_table_mutex
);
175 inet_rcv_compat
= NULL
;
176 mutex_unlock(&sock_diag_table_mutex
);
178 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat
);
180 int sock_diag_register(const struct sock_diag_handler
*hndl
)
184 if (hndl
->family
>= AF_MAX
)
187 mutex_lock(&sock_diag_table_mutex
);
188 if (sock_diag_handlers
[hndl
->family
])
191 sock_diag_handlers
[hndl
->family
] = hndl
;
192 mutex_unlock(&sock_diag_table_mutex
);
196 EXPORT_SYMBOL_GPL(sock_diag_register
);
198 void sock_diag_unregister(const struct sock_diag_handler
*hnld
)
200 int family
= hnld
->family
;
202 if (family
>= AF_MAX
)
205 mutex_lock(&sock_diag_table_mutex
);
206 BUG_ON(sock_diag_handlers
[family
] != hnld
);
207 sock_diag_handlers
[family
] = NULL
;
208 mutex_unlock(&sock_diag_table_mutex
);
210 EXPORT_SYMBOL_GPL(sock_diag_unregister
);
212 static int __sock_diag_cmd(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
215 struct sock_diag_req
*req
= nlmsg_data(nlh
);
216 const struct sock_diag_handler
*hndl
;
218 if (nlmsg_len(nlh
) < sizeof(*req
))
221 if (req
->sdiag_family
>= AF_MAX
)
223 req
->sdiag_family
= array_index_nospec(req
->sdiag_family
, AF_MAX
);
225 if (sock_diag_handlers
[req
->sdiag_family
] == NULL
)
226 sock_load_diag_module(req
->sdiag_family
, 0);
228 mutex_lock(&sock_diag_table_mutex
);
229 hndl
= sock_diag_handlers
[req
->sdiag_family
];
232 else if (nlh
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
)
233 err
= hndl
->dump(skb
, nlh
);
234 else if (nlh
->nlmsg_type
== SOCK_DESTROY
&& hndl
->destroy
)
235 err
= hndl
->destroy(skb
, nlh
);
238 mutex_unlock(&sock_diag_table_mutex
);
243 static int sock_diag_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
244 struct netlink_ext_ack
*extack
)
248 switch (nlh
->nlmsg_type
) {
249 case TCPDIAG_GETSOCK
:
250 case DCCPDIAG_GETSOCK
:
251 if (inet_rcv_compat
== NULL
)
252 sock_load_diag_module(AF_INET
, 0);
254 mutex_lock(&sock_diag_table_mutex
);
255 if (inet_rcv_compat
!= NULL
)
256 ret
= inet_rcv_compat(skb
, nlh
);
259 mutex_unlock(&sock_diag_table_mutex
);
262 case SOCK_DIAG_BY_FAMILY
:
264 return __sock_diag_cmd(skb
, nlh
);
270 static DEFINE_MUTEX(sock_diag_mutex
);
272 static void sock_diag_rcv(struct sk_buff
*skb
)
274 mutex_lock(&sock_diag_mutex
);
275 netlink_rcv_skb(skb
, &sock_diag_rcv_msg
);
276 mutex_unlock(&sock_diag_mutex
);
279 static int sock_diag_bind(struct net
*net
, int group
)
282 case SKNLGRP_INET_TCP_DESTROY
:
283 case SKNLGRP_INET_UDP_DESTROY
:
284 if (!sock_diag_handlers
[AF_INET
])
285 sock_load_diag_module(AF_INET
, 0);
287 case SKNLGRP_INET6_TCP_DESTROY
:
288 case SKNLGRP_INET6_UDP_DESTROY
:
289 if (!sock_diag_handlers
[AF_INET6
])
290 sock_load_diag_module(AF_INET6
, 0);
296 int sock_diag_destroy(struct sock
*sk
, int err
)
298 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
301 if (!sk
->sk_prot
->diag_destroy
)
304 return sk
->sk_prot
->diag_destroy(sk
, err
);
306 EXPORT_SYMBOL_GPL(sock_diag_destroy
);
308 static int __net_init
diag_net_init(struct net
*net
)
310 struct netlink_kernel_cfg cfg
= {
311 .groups
= SKNLGRP_MAX
,
312 .input
= sock_diag_rcv
,
313 .bind
= sock_diag_bind
,
314 .flags
= NL_CFG_F_NONROOT_RECV
,
317 net
->diag_nlsk
= netlink_kernel_create(net
, NETLINK_SOCK_DIAG
, &cfg
);
318 return net
->diag_nlsk
== NULL
? -ENOMEM
: 0;
321 static void __net_exit
diag_net_exit(struct net
*net
)
323 netlink_kernel_release(net
->diag_nlsk
);
324 net
->diag_nlsk
= NULL
;
327 static struct pernet_operations diag_net_ops
= {
328 .init
= diag_net_init
,
329 .exit
= diag_net_exit
,
332 static int __init
sock_diag_init(void)
334 broadcast_wq
= alloc_workqueue("sock_diag_events", 0, 0);
335 BUG_ON(!broadcast_wq
);
336 return register_pernet_subsys(&diag_net_ops
);
338 device_initcall(sock_diag_init
);