blob: 6b10573cc9faa790fe261b452b85f3b774c3ec21 [file] [log] [blame]
Paul Gortmakerb6191ae2015-10-07 17:27:43 -04001/* License: GPL */
2
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00003#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>
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +00009#include <net/sock.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -040010#include <linux/kernel.h>
11#include <linux/tcp.h>
12#include <linux/workqueue.h>
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000013
14#include <linux/inet_diag.h>
15#include <linux/sock_diag.h>
16
Shan Wei8dcf01f2012-04-24 18:21:07 +000017static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000018static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
19static DEFINE_MUTEX(sock_diag_table_mutex);
Craig Gallekeb4cb002015-06-15 11:26:18 -040020static struct workqueue_struct *broadcast_wq;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000021
Eric Dumazet33cf7c92015-03-11 18:53:14 -070022static u64 sock_gen_cookie(struct sock *sk)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000023{
Eric Dumazet33cf7c92015-03-11 18:53:14 -070024 while (1) {
25 u64 res = atomic64_read(&sk->sk_cookie);
26
27 if (res)
28 return res;
29 res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
30 atomic64_cmpxchg(&sk->sk_cookie, 0, res);
31 }
32}
33
34int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
35{
36 u64 res;
37
38 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000039 return 0;
Eric Dumazet33cf7c92015-03-11 18:53:14 -070040
41 res = sock_gen_cookie(sk);
42 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
43 return -ESTALE;
44
45 return 0;
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000046}
47EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
48
Eric Dumazet33cf7c92015-03-11 18:53:14 -070049void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000050{
Eric Dumazet33cf7c92015-03-11 18:53:14 -070051 u64 res = sock_gen_cookie(sk);
52
53 cookie[0] = (u32)res;
54 cookie[1] = (u32)(res >> 32);
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000055}
56EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
57
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000058int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
59{
Thomas Graf7b468662012-06-26 23:36:11 +000060 u32 mem[SK_MEMINFO_VARS];
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000061
62 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
63 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
64 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
65 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
66 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
67 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
68 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
Eric Dumazetd594e982012-06-04 03:50:35 +000069 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
Eric Dumazet15239302016-04-01 08:52:18 -070070 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000071
Thomas Graf7b468662012-06-26 23:36:11 +000072 return nla_put(skb, attrtype, sizeof(mem), &mem);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000073}
74EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
75
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070076int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000077 struct sk_buff *skb, int attrtype)
78{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010079 struct sock_fprog_kern *fprog;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000080 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010081 struct nlattr *attr;
82 unsigned int flen;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000083 int err = 0;
84
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070085 if (!may_report_filterinfo) {
Nicolas Dichtele8d96122013-04-25 06:53:54 +000086 nla_reserve(skb, attrtype, 0);
87 return 0;
88 }
89
90 rcu_read_lock();
Nicolas Dichtele8d96122013-04-25 06:53:54 +000091 filter = rcu_dereference(sk->sk_filter);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010092 if (!filter)
93 goto out;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000094
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070095 fprog = filter->prog->orig_prog;
Daniel Borkmannb382c082015-09-02 14:00:36 +020096 if (!fprog)
97 goto out;
98
Alexei Starovoitov009937e2014-07-30 20:34:13 -070099 flen = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100100
101 attr = nla_reserve(skb, attrtype, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +0000102 if (attr == NULL) {
103 err = -EMSGSIZE;
104 goto out;
105 }
106
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100107 memcpy(nla_data(attr), fprog->filter, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +0000108out:
109 rcu_read_unlock();
110 return err;
111}
112EXPORT_SYMBOL(sock_diag_put_filterinfo);
113
Craig Gallekeb4cb002015-06-15 11:26:18 -0400114struct 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 */
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200123 + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
Craig Gallekeb4cb002015-06-15 11:26:18 -0400124}
125
126static void sock_diag_broadcast_destroy_work(struct work_struct *work)
127{
128 struct broadcast_sk *bsk =
129 container_of(work, struct broadcast_sk, work);
130 struct sock *sk = bsk->sk;
131 const struct sock_diag_handler *hndl;
132 struct sk_buff *skb;
133 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
134 int err = -1;
135
136 WARN_ON(group == SKNLGRP_NONE);
137
138 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
139 if (!skb)
140 goto out;
141
142 mutex_lock(&sock_diag_table_mutex);
143 hndl = sock_diag_handlers[sk->sk_family];
144 if (hndl && hndl->get_info)
145 err = hndl->get_info(skb, sk);
146 mutex_unlock(&sock_diag_table_mutex);
147
148 if (!err)
149 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
150 GFP_KERNEL);
151 else
152 kfree_skb(skb);
153out:
154 sk_destruct(sk);
155 kfree(bsk);
156}
157
158void sock_diag_broadcast_destroy(struct sock *sk)
159{
160 /* Note, this function is often called from an interrupt context. */
161 struct broadcast_sk *bsk =
162 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
163 if (!bsk)
164 return sk_destruct(sk);
165 bsk->sk = sk;
166 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
167 queue_work(broadcast_wq, &bsk->work);
168}
169
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000170void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
171{
172 mutex_lock(&sock_diag_table_mutex);
173 inet_rcv_compat = fn;
174 mutex_unlock(&sock_diag_table_mutex);
175}
176EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
177
178void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
179{
180 mutex_lock(&sock_diag_table_mutex);
181 inet_rcv_compat = NULL;
182 mutex_unlock(&sock_diag_table_mutex);
183}
184EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
185
Shan Wei8dcf01f2012-04-24 18:21:07 +0000186int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000187{
188 int err = 0;
189
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000190 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000191 return -EINVAL;
192
193 mutex_lock(&sock_diag_table_mutex);
194 if (sock_diag_handlers[hndl->family])
195 err = -EBUSY;
196 else
197 sock_diag_handlers[hndl->family] = hndl;
198 mutex_unlock(&sock_diag_table_mutex);
199
200 return err;
201}
202EXPORT_SYMBOL_GPL(sock_diag_register);
203
Shan Wei8dcf01f2012-04-24 18:21:07 +0000204void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000205{
206 int family = hnld->family;
207
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000208 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000209 return;
210
211 mutex_lock(&sock_diag_table_mutex);
212 BUG_ON(sock_diag_handlers[family] != hnld);
213 sock_diag_handlers[family] = NULL;
214 mutex_unlock(&sock_diag_table_mutex);
215}
216EXPORT_SYMBOL_GPL(sock_diag_unregister);
217
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900218static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000219{
220 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000221 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000222 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000223
224 if (nlmsg_len(nlh) < sizeof(*req))
225 return -EINVAL;
226
Mathias Krause6e601a52013-02-23 01:13:47 +0000227 if (req->sdiag_family >= AF_MAX)
228 return -EINVAL;
229
Mathias Krause8e904552013-02-23 01:13:48 +0000230 if (sock_diag_handlers[req->sdiag_family] == NULL)
231 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
232 NETLINK_SOCK_DIAG, req->sdiag_family);
233
234 mutex_lock(&sock_diag_table_mutex);
235 hndl = sock_diag_handlers[req->sdiag_family];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000236 if (hndl == NULL)
237 err = -ENOENT;
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900238 else if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000239 err = hndl->dump(skb, nlh);
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900240 else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
241 err = hndl->destroy(skb, nlh);
242 else
243 err = -EOPNOTSUPP;
Mathias Krause8e904552013-02-23 01:13:48 +0000244 mutex_unlock(&sock_diag_table_mutex);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000245
246 return err;
247}
248
249static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
250{
251 int ret;
252
253 switch (nlh->nlmsg_type) {
254 case TCPDIAG_GETSOCK:
255 case DCCPDIAG_GETSOCK:
256 if (inet_rcv_compat == NULL)
257 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000258 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000259
260 mutex_lock(&sock_diag_table_mutex);
261 if (inet_rcv_compat != NULL)
262 ret = inet_rcv_compat(skb, nlh);
263 else
264 ret = -EOPNOTSUPP;
265 mutex_unlock(&sock_diag_table_mutex);
266
267 return ret;
268 case SOCK_DIAG_BY_FAMILY:
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900269 case SOCK_DESTROY:
270 return __sock_diag_cmd(skb, nlh);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000271 default:
272 return -EINVAL;
273 }
274}
275
276static DEFINE_MUTEX(sock_diag_mutex);
277
278static void sock_diag_rcv(struct sk_buff *skb)
279{
280 mutex_lock(&sock_diag_mutex);
281 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
282 mutex_unlock(&sock_diag_mutex);
283}
284
Craig Gallekeb4cb002015-06-15 11:26:18 -0400285static int sock_diag_bind(struct net *net, int group)
286{
287 switch (group) {
288 case SKNLGRP_INET_TCP_DESTROY:
289 case SKNLGRP_INET_UDP_DESTROY:
290 if (!sock_diag_handlers[AF_INET])
291 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
292 NETLINK_SOCK_DIAG, AF_INET);
293 break;
294 case SKNLGRP_INET6_TCP_DESTROY:
295 case SKNLGRP_INET6_UDP_DESTROY:
296 if (!sock_diag_handlers[AF_INET6])
297 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
298 NETLINK_SOCK_DIAG, AF_INET);
299 break;
300 }
301 return 0;
302}
303
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900304int sock_diag_destroy(struct sock *sk, int err)
305{
306 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
307 return -EPERM;
308
309 if (!sk->sk_prot->diag_destroy)
310 return -EOPNOTSUPP;
311
312 return sk->sk_prot->diag_destroy(sk, err);
313}
314EXPORT_SYMBOL_GPL(sock_diag_destroy);
315
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000316static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000317{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000318 struct netlink_kernel_cfg cfg = {
Craig Gallekeb4cb002015-06-15 11:26:18 -0400319 .groups = SKNLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000320 .input = sock_diag_rcv,
Craig Gallekeb4cb002015-06-15 11:26:18 -0400321 .bind = sock_diag_bind,
322 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000323 };
324
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000325 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000326 return net->diag_nlsk == NULL ? -ENOMEM : 0;
327}
328
329static void __net_exit diag_net_exit(struct net *net)
330{
331 netlink_kernel_release(net->diag_nlsk);
332 net->diag_nlsk = NULL;
333}
334
335static struct pernet_operations diag_net_ops = {
336 .init = diag_net_init,
337 .exit = diag_net_exit,
338};
339
340static int __init sock_diag_init(void)
341{
Craig Gallekeb4cb002015-06-15 11:26:18 -0400342 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
343 BUG_ON(!broadcast_wq);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000344 return register_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000345}
Paul Gortmakerb6191ae2015-10-07 17:27:43 -0400346device_initcall(sock_diag_init);