1 /*
2 * inet_diag.c Module for monitoring INET transport protocols sockets.
3 *
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
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
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>
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>
30 #include <net/netlink.h>
31
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
34
35 #include <linux/inet_diag.h>
36 #include <linux/sock_diag.h>
37
38 static const struct inet_diag_handler **inet_diag_table;
39
40 struct inet_diag_entry {
41 __be32 *saddr;
42 __be32 *daddr;
43 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
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
51 };
52
53 #define INET_DIAG_PUT(skb, attrtype, attrlen) \
54 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
55
56 static DEFINE_MUTEX(inet_diag_table_mutex);
57
inet_diag_lock_handler(int proto)58 static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
59 {
60 if (!inet_diag_table[proto])
61 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
62 NETLINK_SOCK_DIAG, AF_INET, proto);
63
64 mutex_lock(&inet_diag_table_mutex);
65 if (!inet_diag_table[proto])
66 return ERR_PTR(-ENOENT);
67
68 return inet_diag_table[proto];
69 }
70
inet_diag_unlock_handler(const struct inet_diag_handler * handler)71 static inline void inet_diag_unlock_handler(
72 const struct inet_diag_handler *handler)
73 {
74 mutex_unlock(&inet_diag_table_mutex);
75 }
76
inet_sk_diag_fill(struct sock * sk,struct inet_connection_sock * icsk,struct sk_buff * skb,struct inet_diag_req_v2 * req,u32 pid,u32 seq,u16 nlmsg_flags,const struct nlmsghdr * unlh)77 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
78 struct sk_buff *skb, struct inet_diag_req_v2 *req,
79 u32 pid, u32 seq, u16 nlmsg_flags,
80 const struct nlmsghdr *unlh)
81 {
82 const struct inet_sock *inet = inet_sk(sk);
83 struct inet_diag_msg *r;
84 struct nlmsghdr *nlh;
85 void *info = NULL;
86 struct inet_diag_meminfo *minfo = NULL;
87 unsigned char *b = skb_tail_pointer(skb);
88 const struct inet_diag_handler *handler;
89 int ext = req->idiag_ext;
90
91 handler = inet_diag_table[req->sdiag_protocol];
92 BUG_ON(handler == NULL);
93
94 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
95 nlh->nlmsg_flags = nlmsg_flags;
96
97 r = NLMSG_DATA(nlh);
98 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
99
100 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
101 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
102
103 r->idiag_family = sk->sk_family;
104 r->idiag_state = sk->sk_state;
105 r->idiag_timer = 0;
106 r->idiag_retrans = 0;
107
108 r->id.idiag_if = sk->sk_bound_dev_if;
109 sock_diag_save_cookie(sk, r->id.idiag_cookie);
110
111 r->id.idiag_sport = inet->inet_sport;
112 r->id.idiag_dport = inet->inet_dport;
113
114 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
115 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
116
117 r->id.idiag_src[0] = inet->inet_rcv_saddr;
118 r->id.idiag_dst[0] = inet->inet_daddr;
119
120 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
121 * hence this needs to be included regardless of socket family.
122 */
123 if (ext & (1 << (INET_DIAG_TOS - 1)))
124 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
125
126 #if IS_ENABLED(CONFIG_IPV6)
127 if (r->idiag_family == AF_INET6) {
128 const struct ipv6_pinfo *np = inet6_sk(sk);
129
130 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
131 *(struct in6_addr *)r->id.idiag_dst = np->daddr;
132 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
133 RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass);
134 }
135 #endif
136
137 r->idiag_uid = sock_i_uid(sk);
138 r->idiag_inode = sock_i_ino(sk);
139
140 if (minfo) {
141 minfo->idiag_rmem = sk_rmem_alloc_get(sk);
142 minfo->idiag_wmem = sk->sk_wmem_queued;
143 minfo->idiag_fmem = sk->sk_forward_alloc;
144 minfo->idiag_tmem = sk_wmem_alloc_get(sk);
145 }
146
147 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
148 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
149 goto rtattr_failure;
150
151 if (icsk == NULL) {
152 handler->idiag_get_info(sk, r, NULL);
153 goto out;
154 }
155
156 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
157
158 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
159 r->idiag_timer = 1;
160 r->idiag_retrans = icsk->icsk_retransmits;
161 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
162 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
163 r->idiag_timer = 4;
164 r->idiag_retrans = icsk->icsk_probes_out;
165 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
166 } else if (timer_pending(&sk->sk_timer)) {
167 r->idiag_timer = 2;
168 r->idiag_retrans = icsk->icsk_probes_out;
169 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
170 } else {
171 r->idiag_timer = 0;
172 r->idiag_expires = 0;
173 }
174 #undef EXPIRES_IN_MS
175
176 if (ext & (1 << (INET_DIAG_INFO - 1)))
177 info = INET_DIAG_PUT(skb, INET_DIAG_INFO, sizeof(struct tcp_info));
178
179 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
180 const size_t len = strlen(icsk->icsk_ca_ops->name);
181
182 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
183 icsk->icsk_ca_ops->name);
184 }
185
186 handler->idiag_get_info(sk, r, info);
187
188 if (sk->sk_state < TCP_TIME_WAIT &&
189 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
190 icsk->icsk_ca_ops->get_info(sk, ext, skb);
191
192 out:
193 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
194 return skb->len;
195
196 rtattr_failure:
197 nlmsg_failure:
198 nlmsg_trim(skb, b);
199 return -EMSGSIZE;
200 }
201 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
202
inet_csk_diag_fill(struct sock * sk,struct sk_buff * skb,struct inet_diag_req_v2 * req,u32 pid,u32 seq,u16 nlmsg_flags,const struct nlmsghdr * unlh)203 static int inet_csk_diag_fill(struct sock *sk,
204 struct sk_buff *skb, struct inet_diag_req_v2 *req,
205 u32 pid, u32 seq, u16 nlmsg_flags,
206 const struct nlmsghdr *unlh)
207 {
208 return inet_sk_diag_fill(sk, inet_csk(sk),
209 skb, req, pid, seq, nlmsg_flags, unlh);
210 }
211
inet_twsk_diag_fill(struct inet_timewait_sock * tw,struct sk_buff * skb,struct inet_diag_req_v2 * req,u32 pid,u32 seq,u16 nlmsg_flags,const struct nlmsghdr * unlh)212 static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
213 struct sk_buff *skb, struct inet_diag_req_v2 *req,
214 u32 pid, u32 seq, u16 nlmsg_flags,
215 const struct nlmsghdr *unlh)
216 {
217 long tmo;
218 struct inet_diag_msg *r;
219 const unsigned char *previous_tail = skb_tail_pointer(skb);
220 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
221 unlh->nlmsg_type, sizeof(*r));
222
223 r = NLMSG_DATA(nlh);
224 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
225
226 nlh->nlmsg_flags = nlmsg_flags;
227
228 tmo = tw->tw_ttd - jiffies;
229 if (tmo < 0)
230 tmo = 0;
231
232 r->idiag_family = tw->tw_family;
233 r->idiag_retrans = 0;
234
235 r->id.idiag_if = tw->tw_bound_dev_if;
236 sock_diag_save_cookie(tw, r->id.idiag_cookie);
237
238 r->id.idiag_sport = tw->tw_sport;
239 r->id.idiag_dport = tw->tw_dport;
240
241 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
242 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
243
244 r->id.idiag_src[0] = tw->tw_rcv_saddr;
245 r->id.idiag_dst[0] = tw->tw_daddr;
246
247 r->idiag_state = tw->tw_substate;
248 r->idiag_timer = 3;
249 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
250 r->idiag_rqueue = 0;
251 r->idiag_wqueue = 0;
252 r->idiag_uid = 0;
253 r->idiag_inode = 0;
254 #if IS_ENABLED(CONFIG_IPV6)
255 if (tw->tw_family == AF_INET6) {
256 const struct inet6_timewait_sock *tw6 =
257 inet6_twsk((struct sock *)tw);
258
259 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
260 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
261 }
262 #endif
263 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
264 return skb->len;
265 nlmsg_failure:
266 nlmsg_trim(skb, previous_tail);
267 return -EMSGSIZE;
268 }
269
sk_diag_fill(struct sock * sk,struct sk_buff * skb,struct inet_diag_req_v2 * r,u32 pid,u32 seq,u16 nlmsg_flags,const struct nlmsghdr * unlh)270 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
271 struct inet_diag_req_v2 *r, u32 pid, u32 seq, u16 nlmsg_flags,
272 const struct nlmsghdr *unlh)
273 {
274 if (sk->sk_state == TCP_TIME_WAIT)
275 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
276 skb, r, pid, seq, nlmsg_flags,
277 unlh);
278 return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh);
279 }
280
inet_diag_dump_one_icsk(struct inet_hashinfo * hashinfo,struct sk_buff * in_skb,const struct nlmsghdr * nlh,struct inet_diag_req_v2 * req)281 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
282 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
283 {
284 int err;
285 struct sock *sk;
286 struct sk_buff *rep;
287
288 err = -EINVAL;
289 if (req->sdiag_family == AF_INET) {
290 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
291 req->id.idiag_dport, req->id.idiag_src[0],
292 req->id.idiag_sport, req->id.idiag_if);
293 }
294 #if IS_ENABLED(CONFIG_IPV6)
295 else if (req->sdiag_family == AF_INET6) {
296 sk = inet6_lookup(&init_net, hashinfo,
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);
302 }
303 #endif
304 else {
305 goto out_nosk;
306 }
307
308 err = -ENOENT;
309 if (sk == NULL)
310 goto out_nosk;
311
312 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
313 if (err)
314 goto out;
315
316 err = -ENOMEM;
317 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
318 sizeof(struct inet_diag_meminfo) +
319 sizeof(struct tcp_info) + 64)),
320 GFP_KERNEL);
321 if (!rep)
322 goto out;
323
324 err = sk_diag_fill(sk, rep, req,
325 NETLINK_CB(in_skb).pid,
326 nlh->nlmsg_seq, 0, nlh);
327 if (err < 0) {
328 WARN_ON(err == -EMSGSIZE);
329 kfree_skb(rep);
330 goto out;
331 }
332 err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
333 MSG_DONTWAIT);
334 if (err > 0)
335 err = 0;
336
337 out:
338 if (sk) {
339 if (sk->sk_state == TCP_TIME_WAIT)
340 inet_twsk_put((struct inet_timewait_sock *)sk);
341 else
342 sock_put(sk);
343 }
344 out_nosk:
345 return err;
346 }
347 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
348
inet_diag_get_exact(struct sk_buff * in_skb,const struct nlmsghdr * nlh,struct inet_diag_req_v2 * req)349 static int inet_diag_get_exact(struct sk_buff *in_skb,
350 const struct nlmsghdr *nlh,
351 struct inet_diag_req_v2 *req)
352 {
353 const struct inet_diag_handler *handler;
354 int err;
355
356 handler = inet_diag_lock_handler(req->sdiag_protocol);
357 if (IS_ERR(handler))
358 err = PTR_ERR(handler);
359 else
360 err = handler->dump_one(in_skb, nlh, req);
361 inet_diag_unlock_handler(handler);
362
363 return err;
364 }
365
bitstring_match(const __be32 * a1,const __be32 * a2,int bits)366 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
367 {
368 int words = bits >> 5;
369
370 bits &= 0x1f;
371
372 if (words) {
373 if (memcmp(a1, a2, words << 2))
374 return 0;
375 }
376 if (bits) {
377 __be32 w1, w2;
378 __be32 mask;
379
380 w1 = a1[words];
381 w2 = a2[words];
382
383 mask = htonl((0xffffffff) << (32 - bits));
384
385 if ((w1 ^ w2) & mask)
386 return 0;
387 }
388
389 return 1;
390 }
391
392
inet_diag_bc_run(const struct nlattr * _bc,const struct inet_diag_entry * entry)393 static int inet_diag_bc_run(const struct nlattr *_bc,
394 const struct inet_diag_entry *entry)
395 {
396 const void *bc = nla_data(_bc);
397 int len = nla_len(_bc);
398
399 while (len > 0) {
400 int yes = 1;
401 const struct inet_diag_bc_op *op = bc;
402
403 switch (op->code) {
404 case INET_DIAG_BC_NOP:
405 break;
406 case INET_DIAG_BC_JMP:
407 yes = 0;
408 break;
409 case INET_DIAG_BC_S_GE:
410 yes = entry->sport >= op[1].no;
411 break;
412 case INET_DIAG_BC_S_LE:
413 yes = entry->sport <= op[1].no;
414 break;
415 case INET_DIAG_BC_D_GE:
416 yes = entry->dport >= op[1].no;
417 break;
418 case INET_DIAG_BC_D_LE:
419 yes = entry->dport <= op[1].no;
420 break;
421 case INET_DIAG_BC_AUTO:
422 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
423 break;
424 case INET_DIAG_BC_S_COND:
425 case INET_DIAG_BC_D_COND: {
426 struct inet_diag_hostcond *cond;
427 __be32 *addr;
428
429 cond = (struct inet_diag_hostcond *)(op + 1);
430 if (cond->port != -1 &&
431 cond->port != (op->code == INET_DIAG_BC_S_COND ?
432 entry->sport : entry->dport)) {
433 yes = 0;
434 break;
435 }
436
437 if (op->code == INET_DIAG_BC_S_COND)
438 addr = entry->saddr;
439 else
440 addr = entry->daddr;
441
442 if (cond->family != AF_UNSPEC &&
443 cond->family != entry->family) {
444 if (entry->family == AF_INET6 &&
445 cond->family == AF_INET) {
446 if (addr[0] == 0 && addr[1] == 0 &&
447 addr[2] == htonl(0xffff) &&
448 bitstring_match(addr + 3,
449 cond->addr,
450 cond->prefix_len))
451 break;
452 }
453 yes = 0;
454 break;
455 }
456
457 if (cond->prefix_len == 0)
458 break;
459 if (bitstring_match(addr, cond->addr,
460 cond->prefix_len))
461 break;
462 yes = 0;
463 break;
464 }
465 }
466
467 if (yes) {
468 len -= op->yes;
469 bc += op->yes;
470 } else {
471 len -= op->no;
472 bc += op->no;
473 }
474 }
475 return len == 0;
476 }
477
inet_diag_bc_sk(const struct nlattr * bc,struct sock * sk)478 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
479 {
480 struct inet_diag_entry entry;
481 struct inet_sock *inet = inet_sk(sk);
482
483 if (bc == NULL)
484 return 1;
485
486 entry.family = sk->sk_family;
487 #if IS_ENABLED(CONFIG_IPV6)
488 if (entry.family == AF_INET6) {
489 struct ipv6_pinfo *np = inet6_sk(sk);
490
491 entry.saddr = np->rcv_saddr.s6_addr32;
492 entry.daddr = np->daddr.s6_addr32;
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 }
505 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
506
valid_cc(const void * bc,int len,int cc)507 static int valid_cc(const void *bc, int len, int cc)
508 {
509 while (len >= 0) {
510 const struct inet_diag_bc_op *op = bc;
511
512 if (cc > len)
513 return 0;
514 if (cc == len)
515 return 1;
516 if (op->yes < 4 || op->yes & 3)
517 return 0;
518 len -= op->yes;
519 bc += op->yes;
520 }
521 return 0;
522 }
523
524 /* Validate an inet_diag_hostcond. */
valid_hostcond(const struct inet_diag_bc_op * op,int len,int * min_len)525 static 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
562 /* Validate a port comparison operator. */
valid_port_comparison(const struct inet_diag_bc_op * op,int len,int * min_len)563 static 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
inet_diag_bc_audit(const void * bytecode,int bytecode_len)573 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
574 {
575 const void *bc = bytecode;
576 int len = bytecode_len;
577
578 while (len > 0) {
579 const struct inet_diag_bc_op *op = bc;
580 int min_len = sizeof(struct inet_diag_bc_op);
581
582 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
583 switch (op->code) {
584 case INET_DIAG_BC_S_COND:
585 case INET_DIAG_BC_D_COND:
586 if (!valid_hostcond(bc, len, &min_len))
587 return -EINVAL;
588 break;
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:
593 if (!valid_port_comparison(bc, len, &min_len))
594 return -EINVAL;
595 break;
596 case INET_DIAG_BC_AUTO:
597 case INET_DIAG_BC_JMP:
598 case INET_DIAG_BC_NOP:
599 break;
600 default:
601 return -EINVAL;
602 }
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
612 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
613 return -EINVAL;
614 bc += op->yes;
615 len -= op->yes;
616 }
617 return len == 0 ? 0 : -EINVAL;
618 }
619
inet_csk_diag_dump(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,const struct nlattr * bc)620 static int inet_csk_diag_dump(struct sock *sk,
621 struct sk_buff *skb,
622 struct netlink_callback *cb,
623 struct inet_diag_req_v2 *r,
624 const struct nlattr *bc)
625 {
626 if (!inet_diag_bc_sk(bc, sk))
627 return 0;
628
629 return inet_csk_diag_fill(sk, skb, r,
630 NETLINK_CB(cb->skb).pid,
631 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
632 }
633
inet_twsk_diag_dump(struct inet_timewait_sock * tw,struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,const struct nlattr * bc)634 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
635 struct sk_buff *skb,
636 struct netlink_callback *cb,
637 struct inet_diag_req_v2 *r,
638 const struct nlattr *bc)
639 {
640 if (bc != NULL) {
641 struct inet_diag_entry entry;
642
643 entry.family = tw->tw_family;
644 #if IS_ENABLED(CONFIG_IPV6)
645 if (tw->tw_family == AF_INET6) {
646 struct inet6_timewait_sock *tw6 =
647 inet6_twsk((struct sock *)tw);
648 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
649 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
650 } else
651 #endif
652 {
653 entry.saddr = &tw->tw_rcv_saddr;
654 entry.daddr = &tw->tw_daddr;
655 }
656 entry.sport = tw->tw_num;
657 entry.dport = ntohs(tw->tw_dport);
658 entry.userlocks = 0;
659
660 if (!inet_diag_bc_run(bc, &entry))
661 return 0;
662 }
663
664 return inet_twsk_diag_fill(tw, skb, r,
665 NETLINK_CB(cb->skb).pid,
666 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
667 }
668
669 /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
670 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
671 */
inet_diag_req_addrs(const struct sock * sk,const struct request_sock * req,struct inet_diag_entry * entry)672 static inline void inet_diag_req_addrs(const struct sock *sk,
673 const struct request_sock *req,
674 struct inet_diag_entry *entry)
675 {
676 struct inet_request_sock *ireq = inet_rsk(req);
677
678 #if IS_ENABLED(CONFIG_IPV6)
679 if (sk->sk_family == AF_INET6) {
680 if (req->rsk_ops->family == AF_INET6) {
681 entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32;
682 entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32;
683 } else if (req->rsk_ops->family == AF_INET) {
684 ipv6_addr_set_v4mapped(ireq->loc_addr,
685 &entry->saddr_storage);
686 ipv6_addr_set_v4mapped(ireq->rmt_addr,
687 &entry->daddr_storage);
688 entry->saddr = entry->saddr_storage.s6_addr32;
689 entry->daddr = entry->daddr_storage.s6_addr32;
690 }
691 } else
692 #endif
693 {
694 entry->saddr = &ireq->loc_addr;
695 entry->daddr = &ireq->rmt_addr;
696 }
697 }
698
inet_diag_fill_req(struct sk_buff * skb,struct sock * sk,struct request_sock * req,u32 pid,u32 seq,const struct nlmsghdr * unlh)699 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
700 struct request_sock *req, u32 pid, u32 seq,
701 const struct nlmsghdr *unlh)
702 {
703 const struct inet_request_sock *ireq = inet_rsk(req);
704 struct inet_sock *inet = inet_sk(sk);
705 unsigned char *b = skb_tail_pointer(skb);
706 struct inet_diag_msg *r;
707 struct nlmsghdr *nlh;
708 long tmo;
709
710 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
711 nlh->nlmsg_flags = NLM_F_MULTI;
712 r = NLMSG_DATA(nlh);
713
714 r->idiag_family = sk->sk_family;
715 r->idiag_state = TCP_SYN_RECV;
716 r->idiag_timer = 1;
717 r->idiag_retrans = req->retrans;
718
719 r->id.idiag_if = sk->sk_bound_dev_if;
720 sock_diag_save_cookie(req, r->id.idiag_cookie);
721
722 tmo = req->expires - jiffies;
723 if (tmo < 0)
724 tmo = 0;
725
726 r->id.idiag_sport = inet->inet_sport;
727 r->id.idiag_dport = ireq->rmt_port;
728
729 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
730 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
731
732 r->id.idiag_src[0] = ireq->loc_addr;
733 r->id.idiag_dst[0] = ireq->rmt_addr;
734
735 r->idiag_expires = jiffies_to_msecs(tmo);
736 r->idiag_rqueue = 0;
737 r->idiag_wqueue = 0;
738 r->idiag_uid = sock_i_uid(sk);
739 r->idiag_inode = 0;
740 #if IS_ENABLED(CONFIG_IPV6)
741 if (r->idiag_family == AF_INET6) {
742 struct inet_diag_entry entry;
743 inet_diag_req_addrs(sk, req, &entry);
744 memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
745 memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
746 }
747 #endif
748 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
749
750 return skb->len;
751
752 nlmsg_failure:
753 nlmsg_trim(skb, b);
754 return -1;
755 }
756
inet_diag_dump_reqs(struct sk_buff * skb,struct sock * sk,struct netlink_callback * cb,struct inet_diag_req_v2 * r,const struct nlattr * bc)757 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
758 struct netlink_callback *cb,
759 struct inet_diag_req_v2 *r,
760 const struct nlattr *bc)
761 {
762 struct inet_diag_entry entry;
763 struct inet_connection_sock *icsk = inet_csk(sk);
764 struct listen_sock *lopt;
765 struct inet_sock *inet = inet_sk(sk);
766 int j, s_j;
767 int reqnum, s_reqnum;
768 int err = 0;
769
770 s_j = cb->args[3];
771 s_reqnum = cb->args[4];
772
773 if (s_j > 0)
774 s_j--;
775
776 entry.family = sk->sk_family;
777
778 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
779
780 lopt = icsk->icsk_accept_queue.listen_opt;
781 if (!lopt || !lopt->qlen)
782 goto out;
783
784 if (bc != NULL) {
785 entry.sport = inet->inet_num;
786 entry.userlocks = sk->sk_userlocks;
787 }
788
789 for (j = s_j; j < lopt->nr_table_entries; j++) {
790 struct request_sock *req, *head = lopt->syn_table[j];
791
792 reqnum = 0;
793 for (req = head; req; reqnum++, req = req->dl_next) {
794 struct inet_request_sock *ireq = inet_rsk(req);
795
796 if (reqnum < s_reqnum)
797 continue;
798 if (r->id.idiag_dport != ireq->rmt_port &&
799 r->id.idiag_dport)
800 continue;
801
802 if (bc) {
803 inet_diag_req_addrs(sk, req, &entry);
804 entry.dport = ntohs(ireq->rmt_port);
805
806 if (!inet_diag_bc_run(bc, &entry))
807 continue;
808 }
809
810 err = inet_diag_fill_req(skb, sk, req,
811 NETLINK_CB(cb->skb).pid,
812 cb->nlh->nlmsg_seq, cb->nlh);
813 if (err < 0) {
814 cb->args[3] = j + 1;
815 cb->args[4] = reqnum;
816 goto out;
817 }
818 }
819
820 s_reqnum = 0;
821 }
822
823 out:
824 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
825
826 return err;
827 }
828
inet_diag_dump_icsk(struct inet_hashinfo * hashinfo,struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,struct nlattr * bc)829 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
830 struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
831 {
832 int i, num;
833 int s_i, s_num;
834
835 s_i = cb->args[1];
836 s_num = num = cb->args[2];
837
838 if (cb->args[0] == 0) {
839 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
840 goto skip_listen_ht;
841
842 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
843 struct sock *sk;
844 struct hlist_nulls_node *node;
845 struct inet_listen_hashbucket *ilb;
846
847 num = 0;
848 ilb = &hashinfo->listening_hash[i];
849 spin_lock_bh(&ilb->lock);
850 sk_nulls_for_each(sk, node, &ilb->head) {
851 struct inet_sock *inet = inet_sk(sk);
852
853 if (num < s_num) {
854 num++;
855 continue;
856 }
857
858 if (r->sdiag_family != AF_UNSPEC &&
859 sk->sk_family != r->sdiag_family)
860 goto next_listen;
861
862 if (r->id.idiag_sport != inet->inet_sport &&
863 r->id.idiag_sport)
864 goto next_listen;
865
866 if (!(r->idiag_states & TCPF_LISTEN) ||
867 r->id.idiag_dport ||
868 cb->args[3] > 0)
869 goto syn_recv;
870
871 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
872 spin_unlock_bh(&ilb->lock);
873 goto done;
874 }
875
876 syn_recv:
877 if (!(r->idiag_states & TCPF_SYN_RECV))
878 goto next_listen;
879
880 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
881 spin_unlock_bh(&ilb->lock);
882 goto done;
883 }
884
885 next_listen:
886 cb->args[3] = 0;
887 cb->args[4] = 0;
888 ++num;
889 }
890 spin_unlock_bh(&ilb->lock);
891
892 s_num = 0;
893 cb->args[3] = 0;
894 cb->args[4] = 0;
895 }
896 skip_listen_ht:
897 cb->args[0] = 1;
898 s_i = num = s_num = 0;
899 }
900
901 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
902 goto out;
903
904 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
905 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
906 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
907 struct sock *sk;
908 struct hlist_nulls_node *node;
909
910 num = 0;
911
912 if (hlist_nulls_empty(&head->chain) &&
913 hlist_nulls_empty(&head->twchain))
914 continue;
915
916 if (i > s_i)
917 s_num = 0;
918
919 spin_lock_bh(lock);
920 sk_nulls_for_each(sk, node, &head->chain) {
921 struct inet_sock *inet = inet_sk(sk);
922
923 if (num < s_num)
924 goto next_normal;
925 if (!(r->idiag_states & (1 << sk->sk_state)))
926 goto next_normal;
927 if (r->sdiag_family != AF_UNSPEC &&
928 sk->sk_family != r->sdiag_family)
929 goto next_normal;
930 if (r->id.idiag_sport != inet->inet_sport &&
931 r->id.idiag_sport)
932 goto next_normal;
933 if (r->id.idiag_dport != inet->inet_dport &&
934 r->id.idiag_dport)
935 goto next_normal;
936 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
937 spin_unlock_bh(lock);
938 goto done;
939 }
940 next_normal:
941 ++num;
942 }
943
944 if (r->idiag_states & (TCPF_TIME_WAIT | TCPF_FIN_WAIT2)) {
945 struct inet_timewait_sock *tw;
946
947 inet_twsk_for_each(tw, node,
948 &head->twchain) {
949
950 if (num < s_num)
951 goto next_dying;
952 if (!(r->idiag_states & (1 << tw->tw_substate)))
953 goto next_dying;
954 if (r->sdiag_family != AF_UNSPEC &&
955 tw->tw_family != r->sdiag_family)
956 goto next_dying;
957 if (r->id.idiag_sport != tw->tw_sport &&
958 r->id.idiag_sport)
959 goto next_dying;
960 if (r->id.idiag_dport != tw->tw_dport &&
961 r->id.idiag_dport)
962 goto next_dying;
963 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
964 spin_unlock_bh(lock);
965 goto done;
966 }
967 next_dying:
968 ++num;
969 }
970 }
971 spin_unlock_bh(lock);
972 }
973
974 done:
975 cb->args[1] = i;
976 cb->args[2] = num;
977 out:
978 ;
979 }
980 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
981
__inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,struct nlattr * bc)982 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
983 struct inet_diag_req_v2 *r, struct nlattr *bc)
984 {
985 const struct inet_diag_handler *handler;
986 int err = 0;
987
988 handler = inet_diag_lock_handler(r->sdiag_protocol);
989 if (!IS_ERR(handler))
990 handler->dump(skb, cb, r, bc);
991 else
992 err = PTR_ERR(handler);
993 inet_diag_unlock_handler(handler);
994
995 return err ? : skb->len;
996 }
997
inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb)998 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
999 {
1000 struct nlattr *bc = NULL;
1001 int hdrlen = sizeof(struct inet_diag_req_v2);
1002
1003 if (nlmsg_attrlen(cb->nlh, hdrlen))
1004 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1005
1006 return __inet_diag_dump(skb, cb, (struct inet_diag_req_v2 *)NLMSG_DATA(cb->nlh), bc);
1007 }
1008
inet_diag_type2proto(int type)1009 static inline int inet_diag_type2proto(int type)
1010 {
1011 switch (type) {
1012 case TCPDIAG_GETSOCK:
1013 return IPPROTO_TCP;
1014 case DCCPDIAG_GETSOCK:
1015 return IPPROTO_DCCP;
1016 default:
1017 return 0;
1018 }
1019 }
1020
inet_diag_dump_compat(struct sk_buff * skb,struct netlink_callback * cb)1021 static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
1022 {
1023 struct inet_diag_req *rc = NLMSG_DATA(cb->nlh);
1024 struct inet_diag_req_v2 req;
1025 struct nlattr *bc = NULL;
1026 int hdrlen = sizeof(struct inet_diag_req);
1027
1028 req.sdiag_family = AF_UNSPEC; /* compatibility */
1029 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1030 req.idiag_ext = rc->idiag_ext;
1031 req.idiag_states = rc->idiag_states;
1032 req.id = rc->id;
1033
1034 if (nlmsg_attrlen(cb->nlh, hdrlen))
1035 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1036
1037 return __inet_diag_dump(skb, cb, &req, bc);
1038 }
1039
inet_diag_get_exact_compat(struct sk_buff * in_skb,const struct nlmsghdr * nlh)1040 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1041 const struct nlmsghdr *nlh)
1042 {
1043 struct inet_diag_req *rc = NLMSG_DATA(nlh);
1044 struct inet_diag_req_v2 req;
1045
1046 req.sdiag_family = rc->idiag_family;
1047 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1048 req.idiag_ext = rc->idiag_ext;
1049 req.idiag_states = rc->idiag_states;
1050 req.id = rc->id;
1051
1052 return inet_diag_get_exact(in_skb, nlh, &req);
1053 }
1054
inet_diag_rcv_msg_compat(struct sk_buff * skb,struct nlmsghdr * nlh)1055 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1056 {
1057 int hdrlen = sizeof(struct inet_diag_req);
1058
1059 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1060 nlmsg_len(nlh) < hdrlen)
1061 return -EINVAL;
1062
1063 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1064 if (nlmsg_attrlen(nlh, hdrlen)) {
1065 struct nlattr *attr;
1066
1067 attr = nlmsg_find_attr(nlh, hdrlen,
1068 INET_DIAG_REQ_BYTECODE);
1069 if (attr == NULL ||
1070 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1071 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1072 return -EINVAL;
1073 }
1074 {
1075 struct netlink_dump_control c = {
1076 .dump = inet_diag_dump_compat,
1077 };
1078 return netlink_dump_start(sock_diag_nlsk, skb, nlh, &c);
1079 }
1080 }
1081
1082 return inet_diag_get_exact_compat(skb, nlh);
1083 }
1084
inet_diag_handler_dump(struct sk_buff * skb,struct nlmsghdr * h)1085 static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1086 {
1087 int hdrlen = sizeof(struct inet_diag_req_v2);
1088
1089 if (nlmsg_len(h) < hdrlen)
1090 return -EINVAL;
1091
1092 if (h->nlmsg_flags & NLM_F_DUMP) {
1093 if (nlmsg_attrlen(h, hdrlen)) {
1094 struct nlattr *attr;
1095 attr = nlmsg_find_attr(h, hdrlen,
1096 INET_DIAG_REQ_BYTECODE);
1097 if (attr == NULL ||
1098 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1099 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1100 return -EINVAL;
1101 }
1102 {
1103 struct netlink_dump_control c = {
1104 .dump = inet_diag_dump,
1105 };
1106 return netlink_dump_start(sock_diag_nlsk, skb, h, &c);
1107 }
1108 }
1109
1110 return inet_diag_get_exact(skb, h, (struct inet_diag_req_v2 *)NLMSG_DATA(h));
1111 }
1112
1113 static struct sock_diag_handler inet_diag_handler = {
1114 .family = AF_INET,
1115 .dump = inet_diag_handler_dump,
1116 };
1117
1118 static struct sock_diag_handler inet6_diag_handler = {
1119 .family = AF_INET6,
1120 .dump = inet_diag_handler_dump,
1121 };
1122
inet_diag_register(const struct inet_diag_handler * h)1123 int inet_diag_register(const struct inet_diag_handler *h)
1124 {
1125 const __u16 type = h->idiag_type;
1126 int err = -EINVAL;
1127
1128 if (type >= IPPROTO_MAX)
1129 goto out;
1130
1131 mutex_lock(&inet_diag_table_mutex);
1132 err = -EEXIST;
1133 if (inet_diag_table[type] == NULL) {
1134 inet_diag_table[type] = h;
1135 err = 0;
1136 }
1137 mutex_unlock(&inet_diag_table_mutex);
1138 out:
1139 return err;
1140 }
1141 EXPORT_SYMBOL_GPL(inet_diag_register);
1142
inet_diag_unregister(const struct inet_diag_handler * h)1143 void inet_diag_unregister(const struct inet_diag_handler *h)
1144 {
1145 const __u16 type = h->idiag_type;
1146
1147 if (type >= IPPROTO_MAX)
1148 return;
1149
1150 mutex_lock(&inet_diag_table_mutex);
1151 inet_diag_table[type] = NULL;
1152 mutex_unlock(&inet_diag_table_mutex);
1153 }
1154 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1155
inet_diag_init(void)1156 static int __init inet_diag_init(void)
1157 {
1158 const int inet_diag_table_size = (IPPROTO_MAX *
1159 sizeof(struct inet_diag_handler *));
1160 int err = -ENOMEM;
1161
1162 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1163 if (!inet_diag_table)
1164 goto out;
1165
1166 err = sock_diag_register(&inet_diag_handler);
1167 if (err)
1168 goto out_free_nl;
1169
1170 err = sock_diag_register(&inet6_diag_handler);
1171 if (err)
1172 goto out_free_inet;
1173
1174 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1175 out:
1176 return err;
1177
1178 out_free_inet:
1179 sock_diag_unregister(&inet_diag_handler);
1180 out_free_nl:
1181 kfree(inet_diag_table);
1182 goto out;
1183 }
1184
inet_diag_exit(void)1185 static void __exit inet_diag_exit(void)
1186 {
1187 sock_diag_unregister(&inet6_diag_handler);
1188 sock_diag_unregister(&inet_diag_handler);
1189 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1190 kfree(inet_diag_table);
1191 }
1192
1193 module_init(inet_diag_init);
1194 module_exit(inet_diag_exit);
1195 MODULE_LICENSE("GPL");
1196 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1197 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1198