xref: /openbmc/linux/net/ipv4/inet_diag.c (revision 87c22ea5)
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 static int inet_diag_get_exact(struct sk_buff *in_skb,
250 			       const struct nlmsghdr *nlh,
251 			       struct inet_diag_req *req)
252 {
253 	int err;
254 	struct sock *sk;
255 	struct sk_buff *rep;
256 	struct inet_hashinfo *hashinfo;
257 	const struct inet_diag_handler *handler;
258 
259 	handler = inet_diag_lock_handler(req->sdiag_protocol);
260 	if (IS_ERR(handler)) {
261 		err = PTR_ERR(handler);
262 		goto unlock;
263 	}
264 
265 	hashinfo = handler->idiag_hashinfo;
266 	err = -EINVAL;
267 
268 	if (req->sdiag_family == AF_INET) {
269 		sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
270 				 req->id.idiag_dport, req->id.idiag_src[0],
271 				 req->id.idiag_sport, req->id.idiag_if);
272 	}
273 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
274 	else if (req->sdiag_family == AF_INET6) {
275 		sk = inet6_lookup(&init_net, hashinfo,
276 				  (struct in6_addr *)req->id.idiag_dst,
277 				  req->id.idiag_dport,
278 				  (struct in6_addr *)req->id.idiag_src,
279 				  req->id.idiag_sport,
280 				  req->id.idiag_if);
281 	}
282 #endif
283 	else {
284 		goto unlock;
285 	}
286 
287 	err = -ENOENT;
288 	if (sk == NULL)
289 		goto unlock;
290 
291 	err = -ESTALE;
292 	if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
293 	     req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
294 	    ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
295 	     (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
296 		goto out;
297 
298 	err = -ENOMEM;
299 	rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
300 				     sizeof(struct inet_diag_meminfo) +
301 				     sizeof(struct tcp_info) + 64)),
302 			GFP_KERNEL);
303 	if (!rep)
304 		goto out;
305 
306 	err = sk_diag_fill(sk, rep, req,
307 			   NETLINK_CB(in_skb).pid,
308 			   nlh->nlmsg_seq, 0, nlh);
309 	if (err < 0) {
310 		WARN_ON(err == -EMSGSIZE);
311 		kfree_skb(rep);
312 		goto out;
313 	}
314 	err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
315 			      MSG_DONTWAIT);
316 	if (err > 0)
317 		err = 0;
318 
319 out:
320 	if (sk) {
321 		if (sk->sk_state == TCP_TIME_WAIT)
322 			inet_twsk_put((struct inet_timewait_sock *)sk);
323 		else
324 			sock_put(sk);
325 	}
326 unlock:
327 	inet_diag_unlock_handler(handler);
328 	return err;
329 }
330 
331 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
332 {
333 	int words = bits >> 5;
334 
335 	bits &= 0x1f;
336 
337 	if (words) {
338 		if (memcmp(a1, a2, words << 2))
339 			return 0;
340 	}
341 	if (bits) {
342 		__be32 w1, w2;
343 		__be32 mask;
344 
345 		w1 = a1[words];
346 		w2 = a2[words];
347 
348 		mask = htonl((0xffffffff) << (32 - bits));
349 
350 		if ((w1 ^ w2) & mask)
351 			return 0;
352 	}
353 
354 	return 1;
355 }
356 
357 
358 static int inet_diag_bc_run(const struct nlattr *_bc,
359 		const struct inet_diag_entry *entry)
360 {
361 	const void *bc = nla_data(_bc);
362 	int len = nla_len(_bc);
363 
364 	while (len > 0) {
365 		int yes = 1;
366 		const struct inet_diag_bc_op *op = bc;
367 
368 		switch (op->code) {
369 		case INET_DIAG_BC_NOP:
370 			break;
371 		case INET_DIAG_BC_JMP:
372 			yes = 0;
373 			break;
374 		case INET_DIAG_BC_S_GE:
375 			yes = entry->sport >= op[1].no;
376 			break;
377 		case INET_DIAG_BC_S_LE:
378 			yes = entry->sport <= op[1].no;
379 			break;
380 		case INET_DIAG_BC_D_GE:
381 			yes = entry->dport >= op[1].no;
382 			break;
383 		case INET_DIAG_BC_D_LE:
384 			yes = entry->dport <= op[1].no;
385 			break;
386 		case INET_DIAG_BC_AUTO:
387 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
388 			break;
389 		case INET_DIAG_BC_S_COND:
390 		case INET_DIAG_BC_D_COND: {
391 			struct inet_diag_hostcond *cond;
392 			__be32 *addr;
393 
394 			cond = (struct inet_diag_hostcond *)(op + 1);
395 			if (cond->port != -1 &&
396 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
397 					     entry->sport : entry->dport)) {
398 				yes = 0;
399 				break;
400 			}
401 
402 			if (cond->prefix_len == 0)
403 				break;
404 
405 			if (op->code == INET_DIAG_BC_S_COND)
406 				addr = entry->saddr;
407 			else
408 				addr = entry->daddr;
409 
410 			if (bitstring_match(addr, cond->addr,
411 					    cond->prefix_len))
412 				break;
413 			if (entry->family == AF_INET6 &&
414 			    cond->family == AF_INET) {
415 				if (addr[0] == 0 && addr[1] == 0 &&
416 				    addr[2] == htonl(0xffff) &&
417 				    bitstring_match(addr + 3, cond->addr,
418 						    cond->prefix_len))
419 					break;
420 			}
421 			yes = 0;
422 			break;
423 		}
424 		}
425 
426 		if (yes) {
427 			len -= op->yes;
428 			bc += op->yes;
429 		} else {
430 			len -= op->no;
431 			bc += op->no;
432 		}
433 	}
434 	return len == 0;
435 }
436 
437 static int valid_cc(const void *bc, int len, int cc)
438 {
439 	while (len >= 0) {
440 		const struct inet_diag_bc_op *op = bc;
441 
442 		if (cc > len)
443 			return 0;
444 		if (cc == len)
445 			return 1;
446 		if (op->yes < 4 || op->yes & 3)
447 			return 0;
448 		len -= op->yes;
449 		bc  += op->yes;
450 	}
451 	return 0;
452 }
453 
454 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
455 {
456 	const void *bc = bytecode;
457 	int  len = bytecode_len;
458 
459 	while (len > 0) {
460 		const struct inet_diag_bc_op *op = bc;
461 
462 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
463 		switch (op->code) {
464 		case INET_DIAG_BC_AUTO:
465 		case INET_DIAG_BC_S_COND:
466 		case INET_DIAG_BC_D_COND:
467 		case INET_DIAG_BC_S_GE:
468 		case INET_DIAG_BC_S_LE:
469 		case INET_DIAG_BC_D_GE:
470 		case INET_DIAG_BC_D_LE:
471 		case INET_DIAG_BC_JMP:
472 			if (op->no < 4 || op->no > len + 4 || op->no & 3)
473 				return -EINVAL;
474 			if (op->no < len &&
475 			    !valid_cc(bytecode, bytecode_len, len - op->no))
476 				return -EINVAL;
477 			break;
478 		case INET_DIAG_BC_NOP:
479 			break;
480 		default:
481 			return -EINVAL;
482 		}
483 		if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
484 			return -EINVAL;
485 		bc  += op->yes;
486 		len -= op->yes;
487 	}
488 	return len == 0 ? 0 : -EINVAL;
489 }
490 
491 static int inet_csk_diag_dump(struct sock *sk,
492 			      struct sk_buff *skb,
493 			      struct netlink_callback *cb,
494 			      struct inet_diag_req *r,
495 			      const struct nlattr *bc)
496 {
497 	if (bc != NULL) {
498 		struct inet_diag_entry entry;
499 		struct inet_sock *inet = inet_sk(sk);
500 
501 		entry.family = sk->sk_family;
502 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
503 		if (entry.family == AF_INET6) {
504 			struct ipv6_pinfo *np = inet6_sk(sk);
505 
506 			entry.saddr = np->rcv_saddr.s6_addr32;
507 			entry.daddr = np->daddr.s6_addr32;
508 		} else
509 #endif
510 		{
511 			entry.saddr = &inet->inet_rcv_saddr;
512 			entry.daddr = &inet->inet_daddr;
513 		}
514 		entry.sport = inet->inet_num;
515 		entry.dport = ntohs(inet->inet_dport);
516 		entry.userlocks = sk->sk_userlocks;
517 
518 		if (!inet_diag_bc_run(bc, &entry))
519 			return 0;
520 	}
521 
522 	return inet_csk_diag_fill(sk, skb, r,
523 				  NETLINK_CB(cb->skb).pid,
524 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
525 }
526 
527 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
528 			       struct sk_buff *skb,
529 			       struct netlink_callback *cb,
530 			       struct inet_diag_req *r,
531 			       const struct nlattr *bc)
532 {
533 	if (bc != NULL) {
534 		struct inet_diag_entry entry;
535 
536 		entry.family = tw->tw_family;
537 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
538 		if (tw->tw_family == AF_INET6) {
539 			struct inet6_timewait_sock *tw6 =
540 						inet6_twsk((struct sock *)tw);
541 			entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
542 			entry.daddr = tw6->tw_v6_daddr.s6_addr32;
543 		} else
544 #endif
545 		{
546 			entry.saddr = &tw->tw_rcv_saddr;
547 			entry.daddr = &tw->tw_daddr;
548 		}
549 		entry.sport = tw->tw_num;
550 		entry.dport = ntohs(tw->tw_dport);
551 		entry.userlocks = 0;
552 
553 		if (!inet_diag_bc_run(bc, &entry))
554 			return 0;
555 	}
556 
557 	return inet_twsk_diag_fill(tw, skb, r,
558 				   NETLINK_CB(cb->skb).pid,
559 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
560 }
561 
562 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
563 			      struct request_sock *req, u32 pid, u32 seq,
564 			      const struct nlmsghdr *unlh)
565 {
566 	const struct inet_request_sock *ireq = inet_rsk(req);
567 	struct inet_sock *inet = inet_sk(sk);
568 	unsigned char *b = skb_tail_pointer(skb);
569 	struct inet_diag_msg *r;
570 	struct nlmsghdr *nlh;
571 	long tmo;
572 
573 	nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
574 	nlh->nlmsg_flags = NLM_F_MULTI;
575 	r = NLMSG_DATA(nlh);
576 
577 	r->idiag_family = sk->sk_family;
578 	r->idiag_state = TCP_SYN_RECV;
579 	r->idiag_timer = 1;
580 	r->idiag_retrans = req->retrans;
581 
582 	r->id.idiag_if = sk->sk_bound_dev_if;
583 	r->id.idiag_cookie[0] = (u32)(unsigned long)req;
584 	r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
585 
586 	tmo = req->expires - jiffies;
587 	if (tmo < 0)
588 		tmo = 0;
589 
590 	r->id.idiag_sport = inet->inet_sport;
591 	r->id.idiag_dport = ireq->rmt_port;
592 	r->id.idiag_src[0] = ireq->loc_addr;
593 	r->id.idiag_dst[0] = ireq->rmt_addr;
594 	r->idiag_expires = jiffies_to_msecs(tmo);
595 	r->idiag_rqueue = 0;
596 	r->idiag_wqueue = 0;
597 	r->idiag_uid = sock_i_uid(sk);
598 	r->idiag_inode = 0;
599 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
600 	if (r->idiag_family == AF_INET6) {
601 		*(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
602 		*(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
603 	}
604 #endif
605 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
606 
607 	return skb->len;
608 
609 nlmsg_failure:
610 	nlmsg_trim(skb, b);
611 	return -1;
612 }
613 
614 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
615 			       struct netlink_callback *cb,
616 			       struct inet_diag_req *r,
617 			       const struct nlattr *bc)
618 {
619 	struct inet_diag_entry entry;
620 	struct inet_connection_sock *icsk = inet_csk(sk);
621 	struct listen_sock *lopt;
622 	struct inet_sock *inet = inet_sk(sk);
623 	int j, s_j;
624 	int reqnum, s_reqnum;
625 	int err = 0;
626 
627 	s_j = cb->args[3];
628 	s_reqnum = cb->args[4];
629 
630 	if (s_j > 0)
631 		s_j--;
632 
633 	entry.family = sk->sk_family;
634 
635 	read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
636 
637 	lopt = icsk->icsk_accept_queue.listen_opt;
638 	if (!lopt || !lopt->qlen)
639 		goto out;
640 
641 	if (bc != NULL) {
642 		entry.sport = inet->inet_num;
643 		entry.userlocks = sk->sk_userlocks;
644 	}
645 
646 	for (j = s_j; j < lopt->nr_table_entries; j++) {
647 		struct request_sock *req, *head = lopt->syn_table[j];
648 
649 		reqnum = 0;
650 		for (req = head; req; reqnum++, req = req->dl_next) {
651 			struct inet_request_sock *ireq = inet_rsk(req);
652 
653 			if (reqnum < s_reqnum)
654 				continue;
655 			if (r->id.idiag_dport != ireq->rmt_port &&
656 			    r->id.idiag_dport)
657 				continue;
658 
659 			if (bc) {
660 				entry.saddr =
661 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
662 					(entry.family == AF_INET6) ?
663 					inet6_rsk(req)->loc_addr.s6_addr32 :
664 #endif
665 					&ireq->loc_addr;
666 				entry.daddr =
667 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
668 					(entry.family == AF_INET6) ?
669 					inet6_rsk(req)->rmt_addr.s6_addr32 :
670 #endif
671 					&ireq->rmt_addr;
672 				entry.dport = ntohs(ireq->rmt_port);
673 
674 				if (!inet_diag_bc_run(bc, &entry))
675 					continue;
676 			}
677 
678 			err = inet_diag_fill_req(skb, sk, req,
679 					       NETLINK_CB(cb->skb).pid,
680 					       cb->nlh->nlmsg_seq, cb->nlh);
681 			if (err < 0) {
682 				cb->args[3] = j + 1;
683 				cb->args[4] = reqnum;
684 				goto out;
685 			}
686 		}
687 
688 		s_reqnum = 0;
689 	}
690 
691 out:
692 	read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
693 
694 	return err;
695 }
696 
697 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
698 		struct inet_diag_req *r, struct nlattr *bc)
699 {
700 	int i, num;
701 	int s_i, s_num;
702 	const struct inet_diag_handler *handler;
703 	struct inet_hashinfo *hashinfo;
704 
705 	handler = inet_diag_lock_handler(r->sdiag_protocol);
706 	if (IS_ERR(handler))
707 		goto unlock;
708 
709 	hashinfo = handler->idiag_hashinfo;
710 
711 	s_i = cb->args[1];
712 	s_num = num = cb->args[2];
713 
714 	if (cb->args[0] == 0) {
715 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
716 			goto skip_listen_ht;
717 
718 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
719 			struct sock *sk;
720 			struct hlist_nulls_node *node;
721 			struct inet_listen_hashbucket *ilb;
722 
723 			num = 0;
724 			ilb = &hashinfo->listening_hash[i];
725 			spin_lock_bh(&ilb->lock);
726 			sk_nulls_for_each(sk, node, &ilb->head) {
727 				struct inet_sock *inet = inet_sk(sk);
728 
729 				if (num < s_num) {
730 					num++;
731 					continue;
732 				}
733 
734 				if (r->sdiag_family != AF_UNSPEC &&
735 						sk->sk_family != r->sdiag_family)
736 					goto next_listen;
737 
738 				if (r->id.idiag_sport != inet->inet_sport &&
739 				    r->id.idiag_sport)
740 					goto next_listen;
741 
742 				if (!(r->idiag_states & TCPF_LISTEN) ||
743 				    r->id.idiag_dport ||
744 				    cb->args[3] > 0)
745 					goto syn_recv;
746 
747 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
748 					spin_unlock_bh(&ilb->lock);
749 					goto done;
750 				}
751 
752 syn_recv:
753 				if (!(r->idiag_states & TCPF_SYN_RECV))
754 					goto next_listen;
755 
756 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
757 					spin_unlock_bh(&ilb->lock);
758 					goto done;
759 				}
760 
761 next_listen:
762 				cb->args[3] = 0;
763 				cb->args[4] = 0;
764 				++num;
765 			}
766 			spin_unlock_bh(&ilb->lock);
767 
768 			s_num = 0;
769 			cb->args[3] = 0;
770 			cb->args[4] = 0;
771 		}
772 skip_listen_ht:
773 		cb->args[0] = 1;
774 		s_i = num = s_num = 0;
775 	}
776 
777 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
778 		goto unlock;
779 
780 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
781 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
782 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
783 		struct sock *sk;
784 		struct hlist_nulls_node *node;
785 
786 		num = 0;
787 
788 		if (hlist_nulls_empty(&head->chain) &&
789 			hlist_nulls_empty(&head->twchain))
790 			continue;
791 
792 		if (i > s_i)
793 			s_num = 0;
794 
795 		spin_lock_bh(lock);
796 		sk_nulls_for_each(sk, node, &head->chain) {
797 			struct inet_sock *inet = inet_sk(sk);
798 
799 			if (num < s_num)
800 				goto next_normal;
801 			if (!(r->idiag_states & (1 << sk->sk_state)))
802 				goto next_normal;
803 			if (r->sdiag_family != AF_UNSPEC &&
804 					sk->sk_family != r->sdiag_family)
805 				goto next_normal;
806 			if (r->id.idiag_sport != inet->inet_sport &&
807 			    r->id.idiag_sport)
808 				goto next_normal;
809 			if (r->id.idiag_dport != inet->inet_dport &&
810 			    r->id.idiag_dport)
811 				goto next_normal;
812 			if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
813 				spin_unlock_bh(lock);
814 				goto done;
815 			}
816 next_normal:
817 			++num;
818 		}
819 
820 		if (r->idiag_states & TCPF_TIME_WAIT) {
821 			struct inet_timewait_sock *tw;
822 
823 			inet_twsk_for_each(tw, node,
824 				    &head->twchain) {
825 
826 				if (num < s_num)
827 					goto next_dying;
828 				if (r->sdiag_family != AF_UNSPEC &&
829 						tw->tw_family != r->sdiag_family)
830 					goto next_dying;
831 				if (r->id.idiag_sport != tw->tw_sport &&
832 				    r->id.idiag_sport)
833 					goto next_dying;
834 				if (r->id.idiag_dport != tw->tw_dport &&
835 				    r->id.idiag_dport)
836 					goto next_dying;
837 				if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
838 					spin_unlock_bh(lock);
839 					goto done;
840 				}
841 next_dying:
842 				++num;
843 			}
844 		}
845 		spin_unlock_bh(lock);
846 	}
847 
848 done:
849 	cb->args[1] = i;
850 	cb->args[2] = num;
851 unlock:
852 	inet_diag_unlock_handler(handler);
853 	return skb->len;
854 }
855 
856 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
857 {
858 	struct nlattr *bc = NULL;
859 	int hdrlen = sizeof(struct inet_diag_req);
860 
861 	if (nlmsg_attrlen(cb->nlh, hdrlen))
862 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
863 
864 	return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc);
865 }
866 
867 static inline int inet_diag_type2proto(int type)
868 {
869 	switch (type) {
870 	case TCPDIAG_GETSOCK:
871 		return IPPROTO_TCP;
872 	case DCCPDIAG_GETSOCK:
873 		return IPPROTO_DCCP;
874 	default:
875 		return 0;
876 	}
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(sock_diag_nlsk, 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(sock_diag_nlsk, 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 int inet_diag_register(const struct inet_diag_handler *h)
976 {
977 	const __u16 type = h->idiag_type;
978 	int err = -EINVAL;
979 
980 	if (type >= IPPROTO_MAX)
981 		goto out;
982 
983 	mutex_lock(&inet_diag_table_mutex);
984 	err = -EEXIST;
985 	if (inet_diag_table[type] == NULL) {
986 		inet_diag_table[type] = h;
987 		err = 0;
988 	}
989 	mutex_unlock(&inet_diag_table_mutex);
990 out:
991 	return err;
992 }
993 EXPORT_SYMBOL_GPL(inet_diag_register);
994 
995 void inet_diag_unregister(const struct inet_diag_handler *h)
996 {
997 	const __u16 type = h->idiag_type;
998 
999 	if (type >= IPPROTO_MAX)
1000 		return;
1001 
1002 	mutex_lock(&inet_diag_table_mutex);
1003 	inet_diag_table[type] = NULL;
1004 	mutex_unlock(&inet_diag_table_mutex);
1005 }
1006 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1007 
1008 static int __init inet_diag_init(void)
1009 {
1010 	const int inet_diag_table_size = (IPPROTO_MAX *
1011 					  sizeof(struct inet_diag_handler *));
1012 	int err = -ENOMEM;
1013 
1014 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1015 	if (!inet_diag_table)
1016 		goto out;
1017 
1018 	err = sock_diag_register(&inet_diag_handler);
1019 	if (err)
1020 		goto out_free_nl;
1021 
1022 	err = sock_diag_register(&inet6_diag_handler);
1023 	if (err)
1024 		goto out_free_inet;
1025 
1026 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1027 out:
1028 	return err;
1029 
1030 out_free_inet:
1031 	sock_diag_unregister(&inet_diag_handler);
1032 out_free_nl:
1033 	kfree(inet_diag_table);
1034 	goto out;
1035 }
1036 
1037 static void __exit inet_diag_exit(void)
1038 {
1039 	sock_diag_unregister(&inet6_diag_handler);
1040 	sock_diag_unregister(&inet_diag_handler);
1041 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1042 	kfree(inet_diag_table);
1043 }
1044 
1045 module_init(inet_diag_init);
1046 module_exit(inet_diag_exit);
1047 MODULE_LICENSE("GPL");
1048 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 0);
1049