blob: 217f4e3b82f6edca841b66089ab0772bd8d391e7 [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
Chenbo Feng91b82702017-03-22 17:27:34 -070022u64 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
Josh Hunta2d133b2017-03-20 15:22:03 -040062 sk_get_meminfo(sk, mem);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000063
Thomas Graf7b468662012-06-26 23:36:11 +000064 return nla_put(skb, attrtype, sizeof(mem), &mem);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000065}
66EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
67
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070068int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000069 struct sk_buff *skb, int attrtype)
70{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010071 struct sock_fprog_kern *fprog;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000072 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010073 struct nlattr *attr;
74 unsigned int flen;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000075 int err = 0;
76
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070077 if (!may_report_filterinfo) {
Nicolas Dichtele8d96122013-04-25 06:53:54 +000078 nla_reserve(skb, attrtype, 0);
79 return 0;
80 }
81
82 rcu_read_lock();
Nicolas Dichtele8d96122013-04-25 06:53:54 +000083 filter = rcu_dereference(sk->sk_filter);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010084 if (!filter)
85 goto out;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000086
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070087 fprog = filter->prog->orig_prog;
Daniel Borkmannb382c082015-09-02 14:00:36 +020088 if (!fprog)
89 goto out;
90
Alexei Starovoitov009937e2014-07-30 20:34:13 -070091 flen = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010092
93 attr = nla_reserve(skb, attrtype, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +000094 if (attr == NULL) {
95 err = -EMSGSIZE;
96 goto out;
97 }
98
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010099 memcpy(nla_data(attr), fprog->filter, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +0000100out:
101 rcu_read_unlock();
102 return err;
103}
104EXPORT_SYMBOL(sock_diag_put_filterinfo);
105
Craig Gallekeb4cb002015-06-15 11:26:18 -0400106struct broadcast_sk {
107 struct sock *sk;
108 struct work_struct work;
109};
110
111static size_t sock_diag_nlmsg_size(void)
112{
113 return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
114 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200115 + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
Craig Gallekeb4cb002015-06-15 11:26:18 -0400116}
117
118static void sock_diag_broadcast_destroy_work(struct work_struct *work)
119{
120 struct broadcast_sk *bsk =
121 container_of(work, struct broadcast_sk, work);
122 struct sock *sk = bsk->sk;
123 const struct sock_diag_handler *hndl;
124 struct sk_buff *skb;
125 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
126 int err = -1;
127
128 WARN_ON(group == SKNLGRP_NONE);
129
130 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
131 if (!skb)
132 goto out;
133
134 mutex_lock(&sock_diag_table_mutex);
135 hndl = sock_diag_handlers[sk->sk_family];
136 if (hndl && hndl->get_info)
137 err = hndl->get_info(skb, sk);
138 mutex_unlock(&sock_diag_table_mutex);
139
140 if (!err)
141 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
142 GFP_KERNEL);
143 else
144 kfree_skb(skb);
145out:
146 sk_destruct(sk);
147 kfree(bsk);
148}
149
150void sock_diag_broadcast_destroy(struct sock *sk)
151{
152 /* Note, this function is often called from an interrupt context. */
153 struct broadcast_sk *bsk =
154 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
155 if (!bsk)
156 return sk_destruct(sk);
157 bsk->sk = sk;
158 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
159 queue_work(broadcast_wq, &bsk->work);
160}
161
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000162void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
163{
164 mutex_lock(&sock_diag_table_mutex);
165 inet_rcv_compat = fn;
166 mutex_unlock(&sock_diag_table_mutex);
167}
168EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
169
170void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
171{
172 mutex_lock(&sock_diag_table_mutex);
173 inet_rcv_compat = NULL;
174 mutex_unlock(&sock_diag_table_mutex);
175}
176EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
177
Shan Wei8dcf01f2012-04-24 18:21:07 +0000178int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000179{
180 int err = 0;
181
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000182 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000183 return -EINVAL;
184
185 mutex_lock(&sock_diag_table_mutex);
186 if (sock_diag_handlers[hndl->family])
187 err = -EBUSY;
188 else
189 sock_diag_handlers[hndl->family] = hndl;
190 mutex_unlock(&sock_diag_table_mutex);
191
192 return err;
193}
194EXPORT_SYMBOL_GPL(sock_diag_register);
195
Shan Wei8dcf01f2012-04-24 18:21:07 +0000196void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000197{
198 int family = hnld->family;
199
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000200 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000201 return;
202
203 mutex_lock(&sock_diag_table_mutex);
204 BUG_ON(sock_diag_handlers[family] != hnld);
205 sock_diag_handlers[family] = NULL;
206 mutex_unlock(&sock_diag_table_mutex);
207}
208EXPORT_SYMBOL_GPL(sock_diag_unregister);
209
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900210static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000211{
212 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000213 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000214 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000215
216 if (nlmsg_len(nlh) < sizeof(*req))
217 return -EINVAL;
218
Mathias Krause6e601a52013-02-23 01:13:47 +0000219 if (req->sdiag_family >= AF_MAX)
220 return -EINVAL;
221
Mathias Krause8e904552013-02-23 01:13:48 +0000222 if (sock_diag_handlers[req->sdiag_family] == NULL)
223 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
224 NETLINK_SOCK_DIAG, req->sdiag_family);
225
226 mutex_lock(&sock_diag_table_mutex);
227 hndl = sock_diag_handlers[req->sdiag_family];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000228 if (hndl == NULL)
229 err = -ENOENT;
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900230 else if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000231 err = hndl->dump(skb, nlh);
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900232 else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
233 err = hndl->destroy(skb, nlh);
234 else
235 err = -EOPNOTSUPP;
Mathias Krause8e904552013-02-23 01:13:48 +0000236 mutex_unlock(&sock_diag_table_mutex);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000237
238 return err;
239}
240
Johannes Berg2d4bc932017-04-12 14:34:04 +0200241static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
242 struct netlink_ext_ack *extack)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000243{
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:
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900262 case SOCK_DESTROY:
263 return __sock_diag_cmd(skb, nlh);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000264 default:
265 return -EINVAL;
266 }
267}
268
269static DEFINE_MUTEX(sock_diag_mutex);
270
271static void sock_diag_rcv(struct sk_buff *skb)
272{
273 mutex_lock(&sock_diag_mutex);
274 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
275 mutex_unlock(&sock_diag_mutex);
276}
277
Craig Gallekeb4cb002015-06-15 11:26:18 -0400278static int sock_diag_bind(struct net *net, int group)
279{
280 switch (group) {
281 case SKNLGRP_INET_TCP_DESTROY:
282 case SKNLGRP_INET_UDP_DESTROY:
283 if (!sock_diag_handlers[AF_INET])
284 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
285 NETLINK_SOCK_DIAG, AF_INET);
286 break;
287 case SKNLGRP_INET6_TCP_DESTROY:
288 case SKNLGRP_INET6_UDP_DESTROY:
289 if (!sock_diag_handlers[AF_INET6])
290 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
291 NETLINK_SOCK_DIAG, AF_INET);
292 break;
293 }
294 return 0;
295}
296
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +0900297int sock_diag_destroy(struct sock *sk, int err)
298{
299 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
300 return -EPERM;
301
302 if (!sk->sk_prot->diag_destroy)
303 return -EOPNOTSUPP;
304
305 return sk->sk_prot->diag_destroy(sk, err);
306}
307EXPORT_SYMBOL_GPL(sock_diag_destroy);
308
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000309static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000310{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000311 struct netlink_kernel_cfg cfg = {
Craig Gallekeb4cb002015-06-15 11:26:18 -0400312 .groups = SKNLGRP_MAX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000313 .input = sock_diag_rcv,
Craig Gallekeb4cb002015-06-15 11:26:18 -0400314 .bind = sock_diag_bind,
315 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000316 };
317
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000318 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000319 return net->diag_nlsk == NULL ? -ENOMEM : 0;
320}
321
322static void __net_exit diag_net_exit(struct net *net)
323{
324 netlink_kernel_release(net->diag_nlsk);
325 net->diag_nlsk = NULL;
326}
327
328static struct pernet_operations diag_net_ops = {
329 .init = diag_net_init,
330 .exit = diag_net_exit,
331};
332
333static int __init sock_diag_init(void)
334{
Craig Gallekeb4cb002015-06-15 11:26:18 -0400335 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
336 BUG_ON(!broadcast_wq);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000337 return register_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000338}
Paul Gortmakerb6191ae2015-10-07 17:27:43 -0400339device_initcall(sock_diag_init);