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