blob: 817622f3dbb79e3f94ec5257be032ec5574185d6 [file] [log] [blame]
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001#include <linux/mutex.h>
2#include <linux/socket.h>
3#include <linux/skbuff.h>
4#include <net/netlink.h>
5#include <net/net_namespace.h>
6#include <linux/module.h>
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +00007#include <net/sock.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -04008#include <linux/kernel.h>
9#include <linux/tcp.h>
10#include <linux/workqueue.h>
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000011
12#include <linux/inet_diag.h>
13#include <linux/sock_diag.h>
14
Shan Wei8dcf01f2012-04-24 18:21:07 +000015static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000016static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
17static DEFINE_MUTEX(sock_diag_table_mutex);
Craig Gallekeb4cb002015-06-15 11:26:18 -040018static struct workqueue_struct *broadcast_wq;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000019
Eric Dumazet33cf7c92015-03-11 18:53:14 -070020static u64 sock_gen_cookie(struct sock *sk)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000021{
Eric Dumazet33cf7c92015-03-11 18:53:14 -070022 while (1) {
23 u64 res = atomic64_read(&sk->sk_cookie);
24
25 if (res)
26 return res;
27 res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
28 atomic64_cmpxchg(&sk->sk_cookie, 0, res);
29 }
30}
31
32int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
33{
34 u64 res;
35
36 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000037 return 0;
Eric Dumazet33cf7c92015-03-11 18:53:14 -070038
39 res = sock_gen_cookie(sk);
40 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
41 return -ESTALE;
42
43 return 0;
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000044}
45EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
46
Eric Dumazet33cf7c92015-03-11 18:53:14 -070047void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000048{
Eric Dumazet33cf7c92015-03-11 18:53:14 -070049 u64 res = sock_gen_cookie(sk);
50
51 cookie[0] = (u32)res;
52 cookie[1] = (u32)(res >> 32);
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000053}
54EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
55
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000056int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
57{
Thomas Graf7b468662012-06-26 23:36:11 +000058 u32 mem[SK_MEMINFO_VARS];
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000059
60 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
61 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
62 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
63 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
64 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
65 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
66 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
Eric Dumazetd594e982012-06-04 03:50:35 +000067 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000068
Thomas Graf7b468662012-06-26 23:36:11 +000069 return nla_put(skb, attrtype, sizeof(mem), &mem);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000070}
71EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
72
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070073int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000074 struct sk_buff *skb, int attrtype)
75{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010076 struct sock_fprog_kern *fprog;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000077 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010078 struct nlattr *attr;
79 unsigned int flen;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000080 int err = 0;
81
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070082 if (!may_report_filterinfo) {
Nicolas Dichtele8d96122013-04-25 06:53:54 +000083 nla_reserve(skb, attrtype, 0);
84 return 0;
85 }
86
87 rcu_read_lock();
Nicolas Dichtele8d96122013-04-25 06:53:54 +000088 filter = rcu_dereference(sk->sk_filter);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010089 if (!filter)
90 goto out;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000091
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070092 fprog = filter->prog->orig_prog;
Daniel Borkmannb382c082015-09-02 14:00:36 +020093 if (!fprog)
94 goto out;
95
Alexei Starovoitov009937e2014-07-30 20:34:13 -070096 flen = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010097
98 attr = nla_reserve(skb, attrtype, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +000099 if (attr == NULL) {
100 err = -EMSGSIZE;
101 goto out;
102 }
103
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100104 memcpy(nla_data(attr), fprog->filter, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +0000105out:
106 rcu_read_unlock();
107 return err;
108}
109EXPORT_SYMBOL(sock_diag_put_filterinfo);
110
Craig Gallekeb4cb002015-06-15 11:26:18 -0400111struct broadcast_sk {
112 struct sock *sk;
113 struct work_struct work;
114};
115
116static size_t sock_diag_nlmsg_size(void)
117{
118 return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
119 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
120 + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
121}
122
123static void sock_diag_broadcast_destroy_work(struct work_struct *work)
124{
125 struct broadcast_sk *bsk =
126 container_of(work, struct broadcast_sk, work);
127 struct sock *sk = bsk->sk;
128 const struct sock_diag_handler *hndl;
129 struct sk_buff *skb;
130 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
131 int err = -1;
132
133 WARN_ON(group == SKNLGRP_NONE);
134
135 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
136 if (!skb)
137 goto out;
138
139 mutex_lock(&sock_diag_table_mutex);
140 hndl = sock_diag_handlers[sk->sk_family];
141 if (hndl && hndl->get_info)
142 err = hndl->get_info(skb, sk);
143 mutex_unlock(&sock_diag_table_mutex);
144
145 if (!err)
146 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
147 GFP_KERNEL);
148 else
149 kfree_skb(skb);
150out:
151 sk_destruct(sk);
152 kfree(bsk);
153}
154
155void sock_diag_broadcast_destroy(struct sock *sk)
156{
157 /* Note, this function is often called from an interrupt context. */
158 struct broadcast_sk *bsk =
159 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
160 if (!bsk)
161 return sk_destruct(sk);
162 bsk->sk = sk;
163 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
164 queue_work(broadcast_wq, &bsk->work);
165}
166
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000167void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
168{
169 mutex_lock(&sock_diag_table_mutex);
170 inet_rcv_compat = fn;
171 mutex_unlock(&sock_diag_table_mutex);
172}
173EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
174
175void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
176{
177 mutex_lock(&sock_diag_table_mutex);
178 inet_rcv_compat = NULL;
179 mutex_unlock(&sock_diag_table_mutex);
180}
181EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
182
Shan Wei8dcf01f2012-04-24 18:21:07 +0000183int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000184{
185 int err = 0;
186
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000187 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000188 return -EINVAL;
189
190 mutex_lock(&sock_diag_table_mutex);
191 if (sock_diag_handlers[hndl->family])
192 err = -EBUSY;
193 else
194 sock_diag_handlers[hndl->family] = hndl;
195 mutex_unlock(&sock_diag_table_mutex);
196
197 return err;
198}
199EXPORT_SYMBOL_GPL(sock_diag_register);
200
Shan Wei8dcf01f2012-04-24 18:21:07 +0000201void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000202{
203 int family = hnld->family;
204
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000205 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000206 return;
207
208 mutex_lock(&sock_diag_table_mutex);
209 BUG_ON(sock_diag_handlers[family] != hnld);
210 sock_diag_handlers[family] = NULL;
211 mutex_unlock(&sock_diag_table_mutex);
212}
213EXPORT_SYMBOL_GPL(sock_diag_unregister);
214
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000215static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
216{
217 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000218 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000219 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000220
221 if (nlmsg_len(nlh) < sizeof(*req))
222 return -EINVAL;
223
Mathias Krause6e601a52013-02-23 01:13:47 +0000224 if (req->sdiag_family >= AF_MAX)
225 return -EINVAL;
226
Mathias Krause8e904552013-02-23 01:13:48 +0000227 if (sock_diag_handlers[req->sdiag_family] == NULL)
228 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
229 NETLINK_SOCK_DIAG, req->sdiag_family);
230
231 mutex_lock(&sock_diag_table_mutex);
232 hndl = sock_diag_handlers[req->sdiag_family];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000233 if (hndl == NULL)
234 err = -ENOENT;
235 else
236 err = hndl->dump(skb, nlh);
Mathias Krause8e904552013-02-23 01:13:48 +0000237 mutex_unlock(&sock_diag_table_mutex);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000238
239 return err;
240}
241
242static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
243{
244 int ret;
245
246 switch (nlh->nlmsg_type) {
247 case TCPDIAG_GETSOCK:
248 case DCCPDIAG_GETSOCK:
249 if (inet_rcv_compat == NULL)
250 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000251 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000252
253 mutex_lock(&sock_diag_table_mutex);
254 if (inet_rcv_compat != NULL)
255 ret = inet_rcv_compat(skb, nlh);
256 else
257 ret = -EOPNOTSUPP;
258 mutex_unlock(&sock_diag_table_mutex);
259
260 return ret;
261 case SOCK_DIAG_BY_FAMILY:
262 return __sock_diag_rcv_msg(skb, nlh);
263 default:
264 return -EINVAL;
265 }
266}
267
268static DEFINE_MUTEX(sock_diag_mutex);
269
270static void sock_diag_rcv(struct sk_buff *skb)
271{
272 mutex_lock(&sock_diag_mutex);
273 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
274 mutex_unlock(&sock_diag_mutex);
275}
276
Craig Gallekeb4cb002015-06-15 11:26:18 -0400277static int sock_diag_bind(struct net *net, int group)
278{
279 switch (group) {
280 case SKNLGRP_INET_TCP_DESTROY:
281 case SKNLGRP_INET_UDP_DESTROY:
282 if (!sock_diag_handlers[AF_INET])
283 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
284 NETLINK_SOCK_DIAG, AF_INET);
285 break;
286 case SKNLGRP_INET6_TCP_DESTROY:
287 case SKNLGRP_INET6_UDP_DESTROY:
288 if (!sock_diag_handlers[AF_INET6])
289 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
290 NETLINK_SOCK_DIAG, AF_INET);
291 break;
292 }
293 return 0;
294}
295
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000296static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000297{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000298 struct netlink_kernel_cfg cfg = {
Craig Gallekeb4cb002015-06-15 11:26:18 -0400299 .groups = SKNLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000300 .input = sock_diag_rcv,
Craig Gallekeb4cb002015-06-15 11:26:18 -0400301 .bind = sock_diag_bind,
302 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000303 };
304
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000305 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000306 return net->diag_nlsk == NULL ? -ENOMEM : 0;
307}
308
309static void __net_exit diag_net_exit(struct net *net)
310{
311 netlink_kernel_release(net->diag_nlsk);
312 net->diag_nlsk = NULL;
313}
314
315static struct pernet_operations diag_net_ops = {
316 .init = diag_net_init,
317 .exit = diag_net_exit,
318};
319
320static int __init sock_diag_init(void)
321{
Craig Gallekeb4cb002015-06-15 11:26:18 -0400322 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
323 BUG_ON(!broadcast_wq);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000324 return register_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000325}
326
327static void __exit sock_diag_exit(void)
328{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000329 unregister_pernet_subsys(&diag_net_ops);
Craig Gallekeb4cb002015-06-15 11:26:18 -0400330 destroy_workqueue(broadcast_wq);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000331}
332
333module_init(sock_diag_init);
334module_exit(sock_diag_exit);
335MODULE_LICENSE("GPL");
336MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);