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