Merge tag 'batman-adv-for-davem' of git://git.open-mesh.org/linux-merge
[deliverable/linux.git] / net / ipv4 / inet_diag.c
CommitLineData
1da177e4 1/*
73c1f4a0 2 * inet_diag.c Module for monitoring INET transport protocols sockets.
1da177e4 3 *
1da177e4
LT
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
172589cc 12#include <linux/kernel.h>
1da177e4
LT
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
16#include <linux/random.h>
5a0e3ad6 17#include <linux/slab.h>
1da177e4
LT
18#include <linux/cache.h>
19#include <linux/init.h>
20#include <linux/time.h>
21
22#include <net/icmp.h>
23#include <net/tcp.h>
24#include <net/ipv6.h>
25#include <net/inet_common.h>
505cbfc5
ACM
26#include <net/inet_connection_sock.h>
27#include <net/inet_hashtables.h>
28#include <net/inet_timewait_sock.h>
29#include <net/inet6_hashtables.h>
dc5fc579 30#include <net/netlink.h>
1da177e4
LT
31
32#include <linux/inet.h>
33#include <linux/stddef.h>
34
a8c2190e 35#include <linux/inet_diag.h>
d366477a 36#include <linux/sock_diag.h>
1da177e4 37
4f5736c4
ACM
38static const struct inet_diag_handler **inet_diag_table;
39
73c1f4a0 40struct inet_diag_entry {
9f855299
AV
41 __be32 *saddr;
42 __be32 *daddr;
1da177e4
LT
43 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
1c95df85
NC
47#if IS_ENABLED(CONFIG_IPV6)
48 struct in6_addr saddr_storage; /* for IPv4-mapped-IPv6 addresses */
49 struct in6_addr daddr_storage; /* for IPv4-mapped-IPv6 addresses */
50#endif
1da177e4
LT
51};
52
d523a328
HX
53static DEFINE_MUTEX(inet_diag_table_mutex);
54
f13c95f0 55static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
d523a328 56{
f13c95f0 57 if (!inet_diag_table[proto])
aec8dc62
PE
58 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
59 NETLINK_SOCK_DIAG, AF_INET, proto);
d523a328
HX
60
61 mutex_lock(&inet_diag_table_mutex);
f13c95f0 62 if (!inet_diag_table[proto])
d523a328
HX
63 return ERR_PTR(-ENOENT);
64
f13c95f0 65 return inet_diag_table[proto];
d523a328
HX
66}
67
68static inline void inet_diag_unlock_handler(
69 const struct inet_diag_handler *handler)
70{
71 mutex_unlock(&inet_diag_table_mutex);
72}
73
3c4d05c8 74int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
c8991362 75 struct sk_buff *skb, struct inet_diag_req_v2 *req,
d06ca956 76 struct user_namespace *user_ns,
15e47304 77 u32 portid, u32 seq, u16 nlmsg_flags,
dff2c035 78 const struct nlmsghdr *unlh)
1da177e4 79{
463c84b9 80 const struct inet_sock *inet = inet_sk(sk);
73c1f4a0 81 struct inet_diag_msg *r;
1da177e4 82 struct nlmsghdr *nlh;
6e277ed5 83 struct nlattr *attr;
4f5736c4 84 void *info = NULL;
4f5736c4 85 const struct inet_diag_handler *handler;
a029fe26 86 int ext = req->idiag_ext;
4f5736c4 87
a029fe26 88 handler = inet_diag_table[req->sdiag_protocol];
4f5736c4 89 BUG_ON(handler == NULL);
1da177e4 90
15e47304 91 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
6e277ed5
TG
92 nlmsg_flags);
93 if (!nlh)
d106352d 94 return -EMSGSIZE;
4f5736c4 95
d106352d 96 r = nlmsg_data(nlh);
c7d58aab
ACM
97 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
98
73c1f4a0
ACM
99 r->idiag_family = sk->sk_family;
100 r->idiag_state = sk->sk_state;
101 r->idiag_timer = 0;
102 r->idiag_retrans = 0;
1da177e4 103
73c1f4a0 104 r->id.idiag_if = sk->sk_bound_dev_if;
f65c1b53 105 sock_diag_save_cookie(sk, r->id.idiag_cookie);
1da177e4 106
c720c7e8
ED
107 r->id.idiag_sport = inet->inet_sport;
108 r->id.idiag_dport = inet->inet_dport;
109 r->id.idiag_src[0] = inet->inet_rcv_saddr;
110 r->id.idiag_dst[0] = inet->inet_daddr;
1da177e4 111
e4e541a8
PE
112 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
113 goto errout;
114
717b6d83
115 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
116 * hence this needs to be included regardless of socket family.
117 */
118 if (ext & (1 << (INET_DIAG_TOS - 1)))
6e277ed5
TG
119 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
120 goto errout;
717b6d83 121
dfd56b8b 122#if IS_ENABLED(CONFIG_IPV6)
73c1f4a0 123 if (r->idiag_family == AF_INET6) {
1da177e4 124
efe4208f
ED
125 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
126 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
6e277ed5 127
06236ac3 128 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
efe4208f
ED
129 if (nla_put_u8(skb, INET_DIAG_TCLASS,
130 inet6_sk(sk)->tclass) < 0)
6e277ed5 131 goto errout;
1da177e4
LT
132 }
133#endif
134
d06ca956 135 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
3c4d05c8
PE
136 r->idiag_inode = sock_i_ino(sk);
137
6e277ed5
TG
138 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
139 struct inet_diag_meminfo minfo = {
140 .idiag_rmem = sk_rmem_alloc_get(sk),
141 .idiag_wmem = sk->sk_wmem_queued,
142 .idiag_fmem = sk->sk_forward_alloc,
143 .idiag_tmem = sk_wmem_alloc_get(sk),
144 };
145
146 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
147 goto errout;
3c4d05c8
PE
148 }
149
c0636faa
PE
150 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
151 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
6e277ed5 152 goto errout;
c0636faa 153
3c4d05c8 154 if (icsk == NULL) {
62ad6fcd 155 handler->idiag_get_info(sk, r, NULL);
3c4d05c8
PE
156 goto out;
157 }
158
172589cc 159#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
1da177e4 160
6ba8a3b1
ND
161 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
162 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
163 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
73c1f4a0
ACM
164 r->idiag_timer = 1;
165 r->idiag_retrans = icsk->icsk_retransmits;
166 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
463c84b9 167 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
73c1f4a0
ACM
168 r->idiag_timer = 4;
169 r->idiag_retrans = icsk->icsk_probes_out;
170 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
1da177e4 171 } else if (timer_pending(&sk->sk_timer)) {
73c1f4a0
ACM
172 r->idiag_timer = 2;
173 r->idiag_retrans = icsk->icsk_probes_out;
174 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
1da177e4 175 } else {
73c1f4a0
ACM
176 r->idiag_timer = 0;
177 r->idiag_expires = 0;
1da177e4
LT
178 }
179#undef EXPIRES_IN_MS
540722ff 180
6e277ed5
TG
181 if (ext & (1 << (INET_DIAG_INFO - 1))) {
182 attr = nla_reserve(skb, INET_DIAG_INFO,
183 sizeof(struct tcp_info));
184 if (!attr)
185 goto errout;
3c4d05c8 186
6e277ed5 187 info = nla_data(attr);
1da177e4
LT
188 }
189
6e277ed5
TG
190 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
191 if (nla_put_string(skb, INET_DIAG_CONG,
192 icsk->icsk_ca_ops->name) < 0)
193 goto errout;
194
4f5736c4 195 handler->idiag_get_info(sk, r, info);
1da177e4 196
6687e988
ACM
197 if (sk->sk_state < TCP_TIME_WAIT &&
198 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
199 icsk->icsk_ca_ops->get_info(sk, ext, skb);
1da177e4 200
3c4d05c8 201out:
6e277ed5 202 return nlmsg_end(skb, nlh);
1da177e4 203
6e277ed5
TG
204errout:
205 nlmsg_cancel(skb, nlh);
26932566 206 return -EMSGSIZE;
1da177e4 207}
3c4d05c8
PE
208EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
209
210static int inet_csk_diag_fill(struct sock *sk,
c8991362 211 struct sk_buff *skb, struct inet_diag_req_v2 *req,
d06ca956 212 struct user_namespace *user_ns,
15e47304 213 u32 portid, u32 seq, u16 nlmsg_flags,
3c4d05c8
PE
214 const struct nlmsghdr *unlh)
215{
216 return inet_sk_diag_fill(sk, inet_csk(sk),
15e47304 217 skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
3c4d05c8 218}
1da177e4 219
c7d58aab 220static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
c8991362 221 struct sk_buff *skb, struct inet_diag_req_v2 *req,
15e47304 222 u32 portid, u32 seq, u16 nlmsg_flags,
c7d58aab
ACM
223 const struct nlmsghdr *unlh)
224{
96f817fe 225 s32 tmo;
c7d58aab 226 struct inet_diag_msg *r;
6e277ed5 227 struct nlmsghdr *nlh;
d106352d 228
15e47304 229 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
6e277ed5
TG
230 nlmsg_flags);
231 if (!nlh)
d106352d 232 return -EMSGSIZE;
c7d58aab 233
d106352d 234 r = nlmsg_data(nlh);
c7d58aab
ACM
235 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
236
96f817fe 237 tmo = tw->tw_ttd - inet_tw_time_stamp();
c7d58aab
ACM
238 if (tmo < 0)
239 tmo = 0;
240
241 r->idiag_family = tw->tw_family;
c7d58aab
ACM
242 r->idiag_retrans = 0;
243 r->id.idiag_if = tw->tw_bound_dev_if;
f65c1b53 244 sock_diag_save_cookie(tw, r->id.idiag_cookie);
c7d58aab
ACM
245 r->id.idiag_sport = tw->tw_sport;
246 r->id.idiag_dport = tw->tw_dport;
247 r->id.idiag_src[0] = tw->tw_rcv_saddr;
248 r->id.idiag_dst[0] = tw->tw_daddr;
249 r->idiag_state = tw->tw_substate;
250 r->idiag_timer = 3;
96f817fe 251 r->idiag_expires = jiffies_to_msecs(tmo);
c7d58aab
ACM
252 r->idiag_rqueue = 0;
253 r->idiag_wqueue = 0;
254 r->idiag_uid = 0;
255 r->idiag_inode = 0;
dfd56b8b 256#if IS_ENABLED(CONFIG_IPV6)
c7d58aab 257 if (tw->tw_family == AF_INET6) {
efe4208f
ED
258 *(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr;
259 *(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr;
c7d58aab
ACM
260 }
261#endif
6e277ed5
TG
262
263 return nlmsg_end(skb, nlh);
c7d58aab
ACM
264}
265
dff2c035 266static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
d06ca956
EB
267 struct inet_diag_req_v2 *r,
268 struct user_namespace *user_ns,
15e47304 269 u32 portid, u32 seq, u16 nlmsg_flags,
dff2c035
ACM
270 const struct nlmsghdr *unlh)
271{
272 if (sk->sk_state == TCP_TIME_WAIT)
efe4208f
ED
273 return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq,
274 nlmsg_flags, unlh);
275
276 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
277 nlmsg_flags, unlh);
dff2c035
ACM
278}
279
1942c518 280int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
c8991362 281 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
1da177e4
LT
282{
283 int err;
284 struct sock *sk;
1da177e4 285 struct sk_buff *rep;
51d7cccf 286 struct net *net = sock_net(in_skb->sk);
d523a328 287
d523a328 288 err = -EINVAL;
fe50ce28 289 if (req->sdiag_family == AF_INET) {
51d7cccf 290 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
73c1f4a0
ACM
291 req->id.idiag_dport, req->id.idiag_src[0],
292 req->id.idiag_sport, req->id.idiag_if);
1da177e4 293 }
dfd56b8b 294#if IS_ENABLED(CONFIG_IPV6)
fe50ce28 295 else if (req->sdiag_family == AF_INET6) {
51d7cccf 296 sk = inet6_lookup(net, hashinfo,
73c1f4a0
ACM
297 (struct in6_addr *)req->id.idiag_dst,
298 req->id.idiag_dport,
299 (struct in6_addr *)req->id.idiag_src,
300 req->id.idiag_sport,
301 req->id.idiag_if);
1da177e4
LT
302 }
303#endif
304 else {
476f7dbf 305 goto out_nosk;
1da177e4
LT
306 }
307
d523a328 308 err = -ENOENT;
1da177e4 309 if (sk == NULL)
476f7dbf 310 goto out_nosk;
1da177e4 311
f65c1b53 312 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
b005ab4e 313 if (err)
1da177e4
LT
314 goto out;
315
6e277ed5
TG
316 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
317 sizeof(struct inet_diag_meminfo) +
318 sizeof(struct tcp_info) + 64, GFP_KERNEL);
319 if (!rep) {
320 err = -ENOMEM;
1da177e4 321 goto out;
6e277ed5 322 }
1da177e4 323
a029fe26 324 err = sk_diag_fill(sk, rep, req,
e32123e5 325 sk_user_ns(NETLINK_CB(in_skb).sk),
15e47304 326 NETLINK_CB(in_skb).portid,
26932566
PM
327 nlh->nlmsg_seq, 0, nlh);
328 if (err < 0) {
329 WARN_ON(err == -EMSGSIZE);
6e277ed5 330 nlmsg_free(rep);
26932566
PM
331 goto out;
332 }
15e47304 333 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
73c1f4a0 334 MSG_DONTWAIT);
1da177e4
LT
335 if (err > 0)
336 err = 0;
337
338out:
339 if (sk) {
340 if (sk->sk_state == TCP_TIME_WAIT)
8feaf0c0 341 inet_twsk_put((struct inet_timewait_sock *)sk);
1da177e4
LT
342 else
343 sock_put(sk);
344 }
476f7dbf
PE
345out_nosk:
346 return err;
347}
1942c518 348EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
476f7dbf
PE
349
350static int inet_diag_get_exact(struct sk_buff *in_skb,
351 const struct nlmsghdr *nlh,
c8991362 352 struct inet_diag_req_v2 *req)
476f7dbf
PE
353{
354 const struct inet_diag_handler *handler;
355 int err;
356
357 handler = inet_diag_lock_handler(req->sdiag_protocol);
358 if (IS_ERR(handler))
359 err = PTR_ERR(handler);
360 else
1942c518 361 err = handler->dump_one(in_skb, nlh, req);
d523a328 362 inet_diag_unlock_handler(handler);
476f7dbf 363
1da177e4
LT
364 return err;
365}
366
9f855299 367static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
1da177e4
LT
368{
369 int words = bits >> 5;
370
371 bits &= 0x1f;
372
373 if (words) {
374 if (memcmp(a1, a2, words << 2))
375 return 0;
376 }
377 if (bits) {
9f855299
AV
378 __be32 w1, w2;
379 __be32 mask;
1da177e4
LT
380
381 w1 = a1[words];
382 w2 = a2[words];
383
384 mask = htonl((0xffffffff) << (32 - bits));
385
386 if ((w1 ^ w2) & mask)
387 return 0;
388 }
389
390 return 1;
391}
392
393
87c22ea5
PE
394static int inet_diag_bc_run(const struct nlattr *_bc,
395 const struct inet_diag_entry *entry)
1da177e4 396{
87c22ea5
PE
397 const void *bc = nla_data(_bc);
398 int len = nla_len(_bc);
399
1da177e4
LT
400 while (len > 0) {
401 int yes = 1;
73c1f4a0 402 const struct inet_diag_bc_op *op = bc;
1da177e4
LT
403
404 switch (op->code) {
73c1f4a0 405 case INET_DIAG_BC_NOP:
1da177e4 406 break;
73c1f4a0 407 case INET_DIAG_BC_JMP:
1da177e4
LT
408 yes = 0;
409 break;
73c1f4a0 410 case INET_DIAG_BC_S_GE:
1da177e4
LT
411 yes = entry->sport >= op[1].no;
412 break;
73c1f4a0 413 case INET_DIAG_BC_S_LE:
b4ced2b7 414 yes = entry->sport <= op[1].no;
1da177e4 415 break;
73c1f4a0 416 case INET_DIAG_BC_D_GE:
1da177e4
LT
417 yes = entry->dport >= op[1].no;
418 break;
73c1f4a0 419 case INET_DIAG_BC_D_LE:
1da177e4
LT
420 yes = entry->dport <= op[1].no;
421 break;
73c1f4a0 422 case INET_DIAG_BC_AUTO:
1da177e4
LT
423 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
424 break;
73c1f4a0 425 case INET_DIAG_BC_S_COND:
a8c2190e
ACM
426 case INET_DIAG_BC_D_COND: {
427 struct inet_diag_hostcond *cond;
9f855299 428 __be32 *addr;
1da177e4 429
a8c2190e 430 cond = (struct inet_diag_hostcond *)(op + 1);
1da177e4 431 if (cond->port != -1 &&
73c1f4a0 432 cond->port != (op->code == INET_DIAG_BC_S_COND ?
1da177e4
LT
433 entry->sport : entry->dport)) {
434 yes = 0;
435 break;
436 }
4e852c02 437
73c1f4a0 438 if (op->code == INET_DIAG_BC_S_COND)
1da177e4
LT
439 addr = entry->saddr;
440 else
441 addr = entry->daddr;
442
f67caec9
NC
443 if (cond->family != AF_UNSPEC &&
444 cond->family != entry->family) {
445 if (entry->family == AF_INET6 &&
446 cond->family == AF_INET) {
447 if (addr[0] == 0 && addr[1] == 0 &&
448 addr[2] == htonl(0xffff) &&
449 bitstring_match(addr + 3,
450 cond->addr,
451 cond->prefix_len))
452 break;
453 }
454 yes = 0;
455 break;
456 }
457
458 if (cond->prefix_len == 0)
459 break;
4e852c02
ACM
460 if (bitstring_match(addr, cond->addr,
461 cond->prefix_len))
1da177e4 462 break;
1da177e4
LT
463 yes = 0;
464 break;
465 }
466 }
467
4e852c02 468 if (yes) {
1da177e4
LT
469 len -= op->yes;
470 bc += op->yes;
471 } else {
472 len -= op->no;
473 bc += op->no;
474 }
475 }
a02cec21 476 return len == 0;
1da177e4
LT
477}
478
8d07d151
PE
479int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
480{
481 struct inet_diag_entry entry;
482 struct inet_sock *inet = inet_sk(sk);
483
484 if (bc == NULL)
485 return 1;
486
487 entry.family = sk->sk_family;
dfd56b8b 488#if IS_ENABLED(CONFIG_IPV6)
8d07d151 489 if (entry.family == AF_INET6) {
8d07d151 490
efe4208f
ED
491 entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
492 entry.daddr = sk->sk_v6_daddr.s6_addr32;
8d07d151
PE
493 } else
494#endif
495 {
496 entry.saddr = &inet->inet_rcv_saddr;
497 entry.daddr = &inet->inet_daddr;
498 }
499 entry.sport = inet->inet_num;
500 entry.dport = ntohs(inet->inet_dport);
501 entry.userlocks = sk->sk_userlocks;
502
503 return inet_diag_bc_run(bc, &entry);
504}
505EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
506
1da177e4
LT
507static int valid_cc(const void *bc, int len, int cc)
508{
509 while (len >= 0) {
73c1f4a0 510 const struct inet_diag_bc_op *op = bc;
1da177e4
LT
511
512 if (cc > len)
513 return 0;
514 if (cc == len)
515 return 1;
eeb14972 516 if (op->yes < 4 || op->yes & 3)
1da177e4
LT
517 return 0;
518 len -= op->yes;
519 bc += op->yes;
520 }
521 return 0;
522}
523
405c0059
NC
524/* Validate an inet_diag_hostcond. */
525static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
526 int *min_len)
527{
528 int addr_len;
529 struct inet_diag_hostcond *cond;
530
531 /* Check hostcond space. */
532 *min_len += sizeof(struct inet_diag_hostcond);
533 if (len < *min_len)
534 return false;
535 cond = (struct inet_diag_hostcond *)(op + 1);
536
537 /* Check address family and address length. */
538 switch (cond->family) {
539 case AF_UNSPEC:
540 addr_len = 0;
541 break;
542 case AF_INET:
543 addr_len = sizeof(struct in_addr);
544 break;
545 case AF_INET6:
546 addr_len = sizeof(struct in6_addr);
547 break;
548 default:
549 return false;
550 }
551 *min_len += addr_len;
552 if (len < *min_len)
553 return false;
554
555 /* Check prefix length (in bits) vs address length (in bytes). */
556 if (cond->prefix_len > 8 * addr_len)
557 return false;
558
559 return true;
560}
561
5e1f5420
NC
562/* Validate a port comparison operator. */
563static inline bool valid_port_comparison(const struct inet_diag_bc_op *op,
564 int len, int *min_len)
565{
566 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
567 *min_len += sizeof(struct inet_diag_bc_op);
568 if (len < *min_len)
569 return false;
570 return true;
571}
572
73c1f4a0 573static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
1da177e4 574{
eeb14972 575 const void *bc = bytecode;
1da177e4
LT
576 int len = bytecode_len;
577
578 while (len > 0) {
eeb14972 579 const struct inet_diag_bc_op *op = bc;
405c0059 580 int min_len = sizeof(struct inet_diag_bc_op);
1da177e4
LT
581
582//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
583 switch (op->code) {
73c1f4a0
ACM
584 case INET_DIAG_BC_S_COND:
585 case INET_DIAG_BC_D_COND:
405c0059
NC
586 if (!valid_hostcond(bc, len, &min_len))
587 return -EINVAL;
5e1f5420 588 break;
73c1f4a0
ACM
589 case INET_DIAG_BC_S_GE:
590 case INET_DIAG_BC_S_LE:
591 case INET_DIAG_BC_D_GE:
592 case INET_DIAG_BC_D_LE:
5e1f5420 593 if (!valid_port_comparison(bc, len, &min_len))
1da177e4
LT
594 return -EINVAL;
595 break;
5e1f5420
NC
596 case INET_DIAG_BC_AUTO:
597 case INET_DIAG_BC_JMP:
73c1f4a0 598 case INET_DIAG_BC_NOP:
1da177e4
LT
599 break;
600 default:
601 return -EINVAL;
602 }
5e1f5420
NC
603
604 if (op->code != INET_DIAG_BC_NOP) {
605 if (op->no < min_len || op->no > len + 4 || op->no & 3)
606 return -EINVAL;
607 if (op->no < len &&
608 !valid_cc(bytecode, bytecode_len, len - op->no))
609 return -EINVAL;
610 }
611
405c0059 612 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
eeb14972 613 return -EINVAL;
4e852c02 614 bc += op->yes;
1da177e4
LT
615 len -= op->yes;
616 }
617 return len == 0 ? 0 : -EINVAL;
618}
619
dff2c035
ACM
620static int inet_csk_diag_dump(struct sock *sk,
621 struct sk_buff *skb,
37f352b5 622 struct netlink_callback *cb,
c8991362 623 struct inet_diag_req_v2 *r,
37f352b5 624 const struct nlattr *bc)
1da177e4 625{
8d07d151
PE
626 if (!inet_diag_bc_sk(bc, sk))
627 return 0;
1da177e4 628
a029fe26 629 return inet_csk_diag_fill(sk, skb, r,
e32123e5 630 sk_user_ns(NETLINK_CB(cb->skb).sk),
15e47304 631 NETLINK_CB(cb->skb).portid,
dff2c035 632 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
1da177e4
LT
633}
634
05dbc7b5 635static int inet_twsk_diag_dump(struct sock *sk,
c7d58aab 636 struct sk_buff *skb,
37f352b5 637 struct netlink_callback *cb,
c8991362 638 struct inet_diag_req_v2 *r,
37f352b5 639 const struct nlattr *bc)
c7d58aab 640{
05dbc7b5
ED
641 struct inet_timewait_sock *tw = inet_twsk(sk);
642
37f352b5 643 if (bc != NULL) {
c7d58aab 644 struct inet_diag_entry entry;
c7d58aab
ACM
645
646 entry.family = tw->tw_family;
dfd56b8b 647#if IS_ENABLED(CONFIG_IPV6)
c7d58aab 648 if (tw->tw_family == AF_INET6) {
efe4208f
ED
649 entry.saddr = tw->tw_v6_rcv_saddr.s6_addr32;
650 entry.daddr = tw->tw_v6_daddr.s6_addr32;
c7d58aab
ACM
651 } else
652#endif
653 {
654 entry.saddr = &tw->tw_rcv_saddr;
655 entry.daddr = &tw->tw_daddr;
656 }
657 entry.sport = tw->tw_num;
658 entry.dport = ntohs(tw->tw_dport);
e905a9ed 659 entry.userlocks = 0;
c7d58aab 660
87c22ea5 661 if (!inet_diag_bc_run(bc, &entry))
c7d58aab
ACM
662 return 0;
663 }
664
a029fe26 665 return inet_twsk_diag_fill(tw, skb, r,
15e47304 666 NETLINK_CB(cb->skb).portid,
c7d58aab
ACM
667 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
668}
669
1c95df85
NC
670/* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
671 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
672 */
673static inline void inet_diag_req_addrs(const struct sock *sk,
674 const struct request_sock *req,
675 struct inet_diag_entry *entry)
676{
677 struct inet_request_sock *ireq = inet_rsk(req);
678
679#if IS_ENABLED(CONFIG_IPV6)
680 if (sk->sk_family == AF_INET6) {
681 if (req->rsk_ops->family == AF_INET6) {
634fb979
ED
682 entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
683 entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
1c95df85 684 } else if (req->rsk_ops->family == AF_INET) {
634fb979 685 ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
1c95df85 686 &entry->saddr_storage);
634fb979 687 ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
1c95df85
NC
688 &entry->daddr_storage);
689 entry->saddr = entry->saddr_storage.s6_addr32;
690 entry->daddr = entry->daddr_storage.s6_addr32;
691 }
692 } else
693#endif
694 {
634fb979
ED
695 entry->saddr = &ireq->ir_loc_addr;
696 entry->daddr = &ireq->ir_rmt_addr;
1c95df85
NC
697 }
698}
699
73c1f4a0 700static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
d06ca956
EB
701 struct request_sock *req,
702 struct user_namespace *user_ns,
15e47304 703 u32 portid, u32 seq,
4e852c02 704 const struct nlmsghdr *unlh)
1da177e4 705{
2e6599cb 706 const struct inet_request_sock *ireq = inet_rsk(req);
1da177e4 707 struct inet_sock *inet = inet_sk(sk);
73c1f4a0 708 struct inet_diag_msg *r;
1da177e4
LT
709 struct nlmsghdr *nlh;
710 long tmo;
711
15e47304 712 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
6e277ed5
TG
713 NLM_F_MULTI);
714 if (!nlh)
715 return -EMSGSIZE;
1da177e4 716
6e277ed5 717 r = nlmsg_data(nlh);
73c1f4a0
ACM
718 r->idiag_family = sk->sk_family;
719 r->idiag_state = TCP_SYN_RECV;
720 r->idiag_timer = 1;
e6c022a4 721 r->idiag_retrans = req->num_retrans;
1da177e4 722
73c1f4a0 723 r->id.idiag_if = sk->sk_bound_dev_if;
f65c1b53 724 sock_diag_save_cookie(req, r->id.idiag_cookie);
1da177e4
LT
725
726 tmo = req->expires - jiffies;
727 if (tmo < 0)
728 tmo = 0;
729
c720c7e8 730 r->id.idiag_sport = inet->inet_sport;
634fb979
ED
731 r->id.idiag_dport = ireq->ir_rmt_port;
732 r->id.idiag_src[0] = ireq->ir_loc_addr;
733 r->id.idiag_dst[0] = ireq->ir_rmt_addr;
73c1f4a0
ACM
734 r->idiag_expires = jiffies_to_msecs(tmo);
735 r->idiag_rqueue = 0;
736 r->idiag_wqueue = 0;
d06ca956 737 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
73c1f4a0 738 r->idiag_inode = 0;
dfd56b8b 739#if IS_ENABLED(CONFIG_IPV6)
73c1f4a0 740 if (r->idiag_family == AF_INET6) {
1c95df85
NC
741 struct inet_diag_entry entry;
742 inet_diag_req_addrs(sk, req, &entry);
743 memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
744 memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
1da177e4
LT
745 }
746#endif
1da177e4 747
6e277ed5 748 return nlmsg_end(skb, nlh);
1da177e4
LT
749}
750
73c1f4a0 751static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
37f352b5 752 struct netlink_callback *cb,
c8991362 753 struct inet_diag_req_v2 *r,
37f352b5 754 const struct nlattr *bc)
1da177e4 755{
73c1f4a0 756 struct inet_diag_entry entry;
463c84b9 757 struct inet_connection_sock *icsk = inet_csk(sk);
2ad69c55 758 struct listen_sock *lopt;
1da177e4
LT
759 struct inet_sock *inet = inet_sk(sk);
760 int j, s_j;
761 int reqnum, s_reqnum;
762 int err = 0;
763
764 s_j = cb->args[3];
765 s_reqnum = cb->args[4];
766
767 if (s_j > 0)
768 s_j--;
769
770 entry.family = sk->sk_family;
771
463c84b9 772 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1da177e4 773
463c84b9 774 lopt = icsk->icsk_accept_queue.listen_opt;
1da177e4
LT
775 if (!lopt || !lopt->qlen)
776 goto out;
777
37f352b5 778 if (bc != NULL) {
c720c7e8 779 entry.sport = inet->inet_num;
1da177e4
LT
780 entry.userlocks = sk->sk_userlocks;
781 }
782
540722ff 783 for (j = s_j; j < lopt->nr_table_entries; j++) {
60236fdd 784 struct request_sock *req, *head = lopt->syn_table[j];
1da177e4
LT
785
786 reqnum = 0;
787 for (req = head; req; reqnum++, req = req->dl_next) {
2e6599cb
ACM
788 struct inet_request_sock *ireq = inet_rsk(req);
789
1da177e4
LT
790 if (reqnum < s_reqnum)
791 continue;
634fb979 792 if (r->id.idiag_dport != ireq->ir_rmt_port &&
73c1f4a0 793 r->id.idiag_dport)
1da177e4
LT
794 continue;
795
796 if (bc) {
1c95df85 797 inet_diag_req_addrs(sk, req, &entry);
634fb979 798 entry.dport = ntohs(ireq->ir_rmt_port);
1da177e4 799
87c22ea5 800 if (!inet_diag_bc_run(bc, &entry))
1da177e4
LT
801 continue;
802 }
803
73c1f4a0 804 err = inet_diag_fill_req(skb, sk, req,
e32123e5 805 sk_user_ns(NETLINK_CB(cb->skb).sk),
15e47304 806 NETLINK_CB(cb->skb).portid,
540722ff 807 cb->nlh->nlmsg_seq, cb->nlh);
1da177e4
LT
808 if (err < 0) {
809 cb->args[3] = j + 1;
810 cb->args[4] = reqnum;
811 goto out;
812 }
813 }
814
815 s_reqnum = 0;
816 }
817
818out:
463c84b9 819 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1da177e4
LT
820
821 return err;
822}
823
1942c518 824void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
c8991362 825 struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
1da177e4
LT
826{
827 int i, num;
828 int s_i, s_num;
51d7cccf 829 struct net *net = sock_net(skb->sk);
4e852c02 830
1da177e4
LT
831 s_i = cb->args[1];
832 s_num = num = cb->args[2];
4f5736c4 833
1da177e4 834 if (cb->args[0] == 0) {
73c1f4a0 835 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
1da177e4 836 goto skip_listen_ht;
540722ff 837
0f7ff927 838 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
1da177e4 839 struct sock *sk;
c25eb3bf 840 struct hlist_nulls_node *node;
5caea4ea 841 struct inet_listen_hashbucket *ilb;
1da177e4
LT
842
843 num = 0;
5caea4ea
ED
844 ilb = &hashinfo->listening_hash[i];
845 spin_lock_bh(&ilb->lock);
c25eb3bf 846 sk_nulls_for_each(sk, node, &ilb->head) {
1da177e4
LT
847 struct inet_sock *inet = inet_sk(sk);
848
51d7cccf
AV
849 if (!net_eq(sock_net(sk), net))
850 continue;
851
1da177e4
LT
852 if (num < s_num) {
853 num++;
854 continue;
855 }
856
d23deaa0
PE
857 if (r->sdiag_family != AF_UNSPEC &&
858 sk->sk_family != r->sdiag_family)
859 goto next_listen;
860
c720c7e8 861 if (r->id.idiag_sport != inet->inet_sport &&
73c1f4a0 862 r->id.idiag_sport)
1da177e4
LT
863 goto next_listen;
864
73c1f4a0
ACM
865 if (!(r->idiag_states & TCPF_LISTEN) ||
866 r->id.idiag_dport ||
1da177e4
LT
867 cb->args[3] > 0)
868 goto syn_recv;
869
25c4cd2b 870 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
5caea4ea 871 spin_unlock_bh(&ilb->lock);
1da177e4
LT
872 goto done;
873 }
874
875syn_recv:
73c1f4a0 876 if (!(r->idiag_states & TCPF_SYN_RECV))
1da177e4
LT
877 goto next_listen;
878
25c4cd2b 879 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
5caea4ea 880 spin_unlock_bh(&ilb->lock);
1da177e4
LT
881 goto done;
882 }
883
884next_listen:
885 cb->args[3] = 0;
886 cb->args[4] = 0;
887 ++num;
888 }
5caea4ea 889 spin_unlock_bh(&ilb->lock);
1da177e4
LT
890
891 s_num = 0;
892 cb->args[3] = 0;
893 cb->args[4] = 0;
894 }
1da177e4
LT
895skip_listen_ht:
896 cb->args[0] = 1;
897 s_i = num = s_num = 0;
898 }
899
73c1f4a0 900 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
efb3cb42 901 goto out;
1da177e4 902
f373b53b 903 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
540722ff 904 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
7e3aab4a 905 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
1da177e4 906 struct sock *sk;
3ab5aee7 907 struct hlist_nulls_node *node;
1da177e4 908
6be547a6
AK
909 num = 0;
910
05dbc7b5 911 if (hlist_nulls_empty(&head->chain))
6be547a6
AK
912 continue;
913
1da177e4
LT
914 if (i > s_i)
915 s_num = 0;
916
7e3aab4a 917 spin_lock_bh(lock);
3ab5aee7 918 sk_nulls_for_each(sk, node, &head->chain) {
05dbc7b5 919 int res;
1da177e4 920
51d7cccf
AV
921 if (!net_eq(sock_net(sk), net))
922 continue;
1da177e4
LT
923 if (num < s_num)
924 goto next_normal;
73c1f4a0 925 if (!(r->idiag_states & (1 << sk->sk_state)))
1da177e4 926 goto next_normal;
d23deaa0 927 if (r->sdiag_family != AF_UNSPEC &&
05dbc7b5 928 sk->sk_family != r->sdiag_family)
d23deaa0 929 goto next_normal;
05dbc7b5 930 if (r->id.idiag_sport != htons(sk->sk_num) &&
73c1f4a0 931 r->id.idiag_sport)
1da177e4 932 goto next_normal;
05dbc7b5 933 if (r->id.idiag_dport != sk->sk_dport &&
4e852c02 934 r->id.idiag_dport)
1da177e4 935 goto next_normal;
05dbc7b5
ED
936 if (sk->sk_state == TCP_TIME_WAIT)
937 res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
938 else
939 res = inet_csk_diag_dump(sk, skb, cb, r, bc);
940 if (res < 0) {
7e3aab4a 941 spin_unlock_bh(lock);
1da177e4
LT
942 goto done;
943 }
944next_normal:
945 ++num;
946 }
947
7e3aab4a 948 spin_unlock_bh(lock);
1da177e4
LT
949 }
950
951done:
952 cb->args[1] = i;
953 cb->args[2] = num;
efb3cb42
PE
954out:
955 ;
956}
1942c518 957EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
efb3cb42
PE
958
959static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
c8991362 960 struct inet_diag_req_v2 *r, struct nlattr *bc)
efb3cb42
PE
961{
962 const struct inet_diag_handler *handler;
cacb6ba0 963 int err = 0;
efb3cb42
PE
964
965 handler = inet_diag_lock_handler(r->sdiag_protocol);
966 if (!IS_ERR(handler))
1942c518 967 handler->dump(skb, cb, r, bc);
cacb6ba0
CG
968 else
969 err = PTR_ERR(handler);
d523a328 970 inet_diag_unlock_handler(handler);
efb3cb42 971
cacb6ba0 972 return err ? : skb->len;
1da177e4
LT
973}
974
25c4cd2b
PE
975static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
976{
977 struct nlattr *bc = NULL;
c8991362 978 int hdrlen = sizeof(struct inet_diag_req_v2);
25c4cd2b
PE
979
980 if (nlmsg_attrlen(cb->nlh, hdrlen))
981 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
982
d106352d 983 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
25c4cd2b
PE
984}
985
a029fe26
PE
986static inline int inet_diag_type2proto(int type)
987{
988 switch (type) {
989 case TCPDIAG_GETSOCK:
990 return IPPROTO_TCP;
991 case DCCPDIAG_GETSOCK:
992 return IPPROTO_DCCP;
993 default:
994 return 0;
995 }
996}
997
25c4cd2b
PE
998static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
999{
d106352d 1000 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
c8991362 1001 struct inet_diag_req_v2 req;
25c4cd2b 1002 struct nlattr *bc = NULL;
3b09c84c 1003 int hdrlen = sizeof(struct inet_diag_req);
25c4cd2b 1004
d23deaa0 1005 req.sdiag_family = AF_UNSPEC; /* compatibility */
25c4cd2b
PE
1006 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1007 req.idiag_ext = rc->idiag_ext;
1008 req.idiag_states = rc->idiag_states;
1009 req.id = rc->id;
1010
1011 if (nlmsg_attrlen(cb->nlh, hdrlen))
1012 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1013
1014 return __inet_diag_dump(skb, cb, &req, bc);
1015}
1016
fe50ce28
PE
1017static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1018 const struct nlmsghdr *nlh)
1019{
d106352d 1020 struct inet_diag_req *rc = nlmsg_data(nlh);
c8991362 1021 struct inet_diag_req_v2 req;
fe50ce28
PE
1022
1023 req.sdiag_family = rc->idiag_family;
1024 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1025 req.idiag_ext = rc->idiag_ext;
1026 req.idiag_states = rc->idiag_states;
1027 req.id = rc->id;
1028
1029 return inet_diag_get_exact(in_skb, nlh, &req);
1030}
1031
8d34172d 1032static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1da177e4 1033{
3b09c84c 1034 int hdrlen = sizeof(struct inet_diag_req);
51d7cccf 1035 struct net *net = sock_net(skb->sk);
1da177e4 1036
ead592ba
TG
1037 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1038 nlmsg_len(nlh) < hdrlen)
1039 return -EINVAL;
1da177e4 1040
b8f3ab42 1041 if (nlh->nlmsg_flags & NLM_F_DUMP) {
ead592ba
TG
1042 if (nlmsg_attrlen(nlh, hdrlen)) {
1043 struct nlattr *attr;
1da177e4 1044
ead592ba
TG
1045 attr = nlmsg_find_attr(nlh, hdrlen,
1046 INET_DIAG_REQ_BYTECODE);
1047 if (attr == NULL ||
1048 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1049 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1050 return -EINVAL;
1051 }
80d326fa
PNA
1052 {
1053 struct netlink_dump_control c = {
1054 .dump = inet_diag_dump_compat,
1055 };
51d7cccf 1056 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
80d326fa 1057 }
1da177e4 1058 }
ead592ba 1059
fe50ce28 1060 return inet_diag_get_exact_compat(skb, nlh);
1da177e4
LT
1061}
1062
d366477a
PE
1063static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1064{
c8991362 1065 int hdrlen = sizeof(struct inet_diag_req_v2);
51d7cccf 1066 struct net *net = sock_net(skb->sk);
d366477a
PE
1067
1068 if (nlmsg_len(h) < hdrlen)
1069 return -EINVAL;
1070
1071 if (h->nlmsg_flags & NLM_F_DUMP) {
25c4cd2b
PE
1072 if (nlmsg_attrlen(h, hdrlen)) {
1073 struct nlattr *attr;
1074 attr = nlmsg_find_attr(h, hdrlen,
1075 INET_DIAG_REQ_BYTECODE);
1076 if (attr == NULL ||
1077 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1078 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1079 return -EINVAL;
1080 }
80d326fa
PNA
1081 {
1082 struct netlink_dump_control c = {
1083 .dump = inet_diag_dump,
1084 };
51d7cccf 1085 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
80d326fa 1086 }
d366477a
PE
1087 }
1088
d106352d 1089 return inet_diag_get_exact(skb, h, nlmsg_data(h));
d366477a
PE
1090}
1091
8dcf01fc 1092static const struct sock_diag_handler inet_diag_handler = {
d366477a
PE
1093 .family = AF_INET,
1094 .dump = inet_diag_handler_dump,
1095};
1096
8dcf01fc 1097static const struct sock_diag_handler inet6_diag_handler = {
d366477a
PE
1098 .family = AF_INET6,
1099 .dump = inet_diag_handler_dump,
1100};
1101
4f5736c4
ACM
1102int inet_diag_register(const struct inet_diag_handler *h)
1103{
1104 const __u16 type = h->idiag_type;
1105 int err = -EINVAL;
1106
f13c95f0 1107 if (type >= IPPROTO_MAX)
4f5736c4
ACM
1108 goto out;
1109
d523a328 1110 mutex_lock(&inet_diag_table_mutex);
4f5736c4
ACM
1111 err = -EEXIST;
1112 if (inet_diag_table[type] == NULL) {
1113 inet_diag_table[type] = h;
1114 err = 0;
1115 }
d523a328 1116 mutex_unlock(&inet_diag_table_mutex);
4f5736c4
ACM
1117out:
1118 return err;
1119}
1120EXPORT_SYMBOL_GPL(inet_diag_register);
1121
1122void inet_diag_unregister(const struct inet_diag_handler *h)
1123{
1124 const __u16 type = h->idiag_type;
1125
f13c95f0 1126 if (type >= IPPROTO_MAX)
4f5736c4
ACM
1127 return;
1128
d523a328 1129 mutex_lock(&inet_diag_table_mutex);
4f5736c4 1130 inet_diag_table[type] = NULL;
d523a328 1131 mutex_unlock(&inet_diag_table_mutex);
4f5736c4
ACM
1132}
1133EXPORT_SYMBOL_GPL(inet_diag_unregister);
1134
73c1f4a0 1135static int __init inet_diag_init(void)
1da177e4 1136{
f13c95f0 1137 const int inet_diag_table_size = (IPPROTO_MAX *
4f5736c4
ACM
1138 sizeof(struct inet_diag_handler *));
1139 int err = -ENOMEM;
1140
0da974f4 1141 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
4f5736c4
ACM
1142 if (!inet_diag_table)
1143 goto out;
1144
d366477a
PE
1145 err = sock_diag_register(&inet_diag_handler);
1146 if (err)
1147 goto out_free_nl;
1148
1149 err = sock_diag_register(&inet6_diag_handler);
1150 if (err)
1151 goto out_free_inet;
1152
8ef874bf 1153 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
4f5736c4
ACM
1154out:
1155 return err;
d366477a
PE
1156
1157out_free_inet:
1158 sock_diag_unregister(&inet_diag_handler);
1159out_free_nl:
4f5736c4
ACM
1160 kfree(inet_diag_table);
1161 goto out;
1da177e4
LT
1162}
1163
73c1f4a0 1164static void __exit inet_diag_exit(void)
1da177e4 1165{
d366477a
PE
1166 sock_diag_unregister(&inet6_diag_handler);
1167 sock_diag_unregister(&inet_diag_handler);
8ef874bf 1168 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
4f5736c4 1169 kfree(inet_diag_table);
1da177e4
LT
1170}
1171
73c1f4a0
ACM
1172module_init(inet_diag_init);
1173module_exit(inet_diag_exit);
1da177e4 1174MODULE_LICENSE("GPL");
aec8dc62
PE
1175MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1176MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
This page took 0.832062 seconds and 5 git commands to generate.