xref: /openbmc/linux/net/ipv4/inet_diag.c (revision 7b35eadd7eee2e0b42421ce3efbc30f1c3c745e5)
1a8c2190eSArnaldo Carvalho de Melo /*
2a8c2190eSArnaldo Carvalho de Melo  * inet_diag.c	Module for monitoring INET transport protocols sockets.
3a8c2190eSArnaldo Carvalho de Melo  *
4a8c2190eSArnaldo Carvalho de Melo  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5a8c2190eSArnaldo Carvalho de Melo  *
6a8c2190eSArnaldo Carvalho de Melo  *	This program is free software; you can redistribute it and/or
7a8c2190eSArnaldo Carvalho de Melo  *      modify it under the terms of the GNU General Public License
8a8c2190eSArnaldo Carvalho de Melo  *      as published by the Free Software Foundation; either version
9a8c2190eSArnaldo Carvalho de Melo  *      2 of the License, or (at your option) any later version.
10a8c2190eSArnaldo Carvalho de Melo  */
11a8c2190eSArnaldo Carvalho de Melo 
12172589ccSIlpo Järvinen #include <linux/kernel.h>
13a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h>
14a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h>
15a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h>
16a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h>
19a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h>
20a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h>
21a8c2190eSArnaldo Carvalho de Melo 
22a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h>
23a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h>
24a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h>
25a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h>
26a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
27a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
28a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
29a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h>
30dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
31a8c2190eSArnaldo Carvalho de Melo 
32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h>
33a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h>
34a8c2190eSArnaldo Carvalho de Melo 
35a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h>
36d366477aSPavel Emelyanov #include <linux/sock_diag.h>
37a8c2190eSArnaldo Carvalho de Melo 
38a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table;
39a8c2190eSArnaldo Carvalho de Melo 
40a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry {
419f855299SAl Viro 	__be32 *saddr;
429f855299SAl Viro 	__be32 *daddr;
43a8c2190eSArnaldo Carvalho de Melo 	u16 sport;
44a8c2190eSArnaldo Carvalho de Melo 	u16 dport;
45a8c2190eSArnaldo Carvalho de Melo 	u16 family;
46a8c2190eSArnaldo Carvalho de Melo 	u16 userlocks;
47a8c2190eSArnaldo Carvalho de Melo };
48a8c2190eSArnaldo Carvalho de Melo 
49a8c2190eSArnaldo Carvalho de Melo #define INET_DIAG_PUT(skb, attrtype, attrlen) \
50a8c2190eSArnaldo Carvalho de Melo 	RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
51a8c2190eSArnaldo Carvalho de Melo 
52d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
53d523a328SHerbert Xu 
54f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
55d523a328SHerbert Xu {
56f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
57d523a328SHerbert Xu 		request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
58f13c95f0SPavel Emelyanov 			       NETLINK_SOCK_DIAG, proto);
59d523a328SHerbert Xu 
60d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
61f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
62d523a328SHerbert Xu 		return ERR_PTR(-ENOENT);
63d523a328SHerbert Xu 
64f13c95f0SPavel Emelyanov 	return inet_diag_table[proto];
65d523a328SHerbert Xu }
66d523a328SHerbert Xu 
67d523a328SHerbert Xu static inline void inet_diag_unlock_handler(
68d523a328SHerbert Xu 	const struct inet_diag_handler *handler)
69d523a328SHerbert Xu {
70d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
71d523a328SHerbert Xu }
72d523a328SHerbert Xu 
73dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_fill(struct sock *sk,
74a029fe26SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req *req,
75a029fe26SPavel Emelyanov 			      u32 pid, u32 seq, u16 nlmsg_flags,
76a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
77a8c2190eSArnaldo Carvalho de Melo {
78a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
79a8c2190eSArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
80a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
81a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr  *nlh;
82a8c2190eSArnaldo Carvalho de Melo 	void *info = NULL;
83a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_meminfo  *minfo = NULL;
8427a884dcSArnaldo Carvalho de Melo 	unsigned char	 *b = skb_tail_pointer(skb);
85a8c2190eSArnaldo Carvalho de Melo 	const struct inet_diag_handler *handler;
86a029fe26SPavel Emelyanov 	int ext = req->idiag_ext;
87a8c2190eSArnaldo Carvalho de Melo 
88a029fe26SPavel Emelyanov 	handler = inet_diag_table[req->sdiag_protocol];
89a8c2190eSArnaldo Carvalho de Melo 	BUG_ON(handler == NULL);
90a8c2190eSArnaldo Carvalho de Melo 
91a8c2190eSArnaldo Carvalho de Melo 	nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
92a8c2190eSArnaldo Carvalho de Melo 	nlh->nlmsg_flags = nlmsg_flags;
93a8c2190eSArnaldo Carvalho de Melo 
94a8c2190eSArnaldo Carvalho de Melo 	r = NLMSG_DATA(nlh);
95c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(sk->sk_state == TCP_TIME_WAIT);
96c7d58aabSArnaldo Carvalho de Melo 
97a8c2190eSArnaldo Carvalho de Melo 	if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
98c7d58aabSArnaldo Carvalho de Melo 		minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
99c7d58aabSArnaldo Carvalho de Melo 
100a8c2190eSArnaldo Carvalho de Melo 	if (ext & (1 << (INET_DIAG_INFO - 1)))
101*7b35eaddSPavel Emelyanov 		info = INET_DIAG_PUT(skb, INET_DIAG_INFO, sizeof(struct tcp_info));
102a8c2190eSArnaldo Carvalho de Melo 
103a8c2190eSArnaldo Carvalho de Melo 	if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
104c7d58aabSArnaldo Carvalho de Melo 		const size_t len = strlen(icsk->icsk_ca_ops->name);
105c7d58aabSArnaldo Carvalho de Melo 
106a8c2190eSArnaldo Carvalho de Melo 		strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
107a8c2190eSArnaldo Carvalho de Melo 		       icsk->icsk_ca_ops->name);
108a8c2190eSArnaldo Carvalho de Melo 	}
109c7d58aabSArnaldo Carvalho de Melo 
110a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
111a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = sk->sk_state;
112a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 0;
113a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = 0;
114a8c2190eSArnaldo Carvalho de Melo 
115a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
116a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
117a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
118a8c2190eSArnaldo Carvalho de Melo 
119c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
120c720c7e8SEric Dumazet 	r->id.idiag_dport = inet->inet_dport;
121c720c7e8SEric Dumazet 	r->id.idiag_src[0] = inet->inet_rcv_saddr;
122c720c7e8SEric Dumazet 	r->id.idiag_dst[0] = inet->inet_daddr;
123a8c2190eSArnaldo Carvalho de Melo 
124717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
125717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
126717b6d83SMaciej Żenczykowski 	 */
127717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
128717b6d83SMaciej Żenczykowski 		RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
129717b6d83SMaciej Żenczykowski 
130a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
131a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
132b71d1d42SEric Dumazet 		const struct ipv6_pinfo *np = inet6_sk(sk);
133a8c2190eSArnaldo Carvalho de Melo 
1344e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
1354e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = np->daddr;
13606236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
13706236ac3SMaciej Żenczykowski 			RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass);
138a8c2190eSArnaldo Carvalho de Melo 	}
139a8c2190eSArnaldo Carvalho de Melo #endif
140a8c2190eSArnaldo Carvalho de Melo 
141172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
142a8c2190eSArnaldo Carvalho de Melo 
143a8c2190eSArnaldo Carvalho de Melo 	if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
144a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
145a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
146a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
147a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
148a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
149a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
150a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
151a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
152a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
153a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
154a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
155a8c2190eSArnaldo Carvalho de Melo 	} else {
156a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
157a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
158a8c2190eSArnaldo Carvalho de Melo 	}
159a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
160a8c2190eSArnaldo Carvalho de Melo 
161a8c2190eSArnaldo Carvalho de Melo 	r->idiag_uid = sock_i_uid(sk);
162a8c2190eSArnaldo Carvalho de Melo 	r->idiag_inode = sock_i_ino(sk);
163a8c2190eSArnaldo Carvalho de Melo 
164a8c2190eSArnaldo Carvalho de Melo 	if (minfo) {
16531e6d363SEric Dumazet 		minfo->idiag_rmem = sk_rmem_alloc_get(sk);
166a8c2190eSArnaldo Carvalho de Melo 		minfo->idiag_wmem = sk->sk_wmem_queued;
167a8c2190eSArnaldo Carvalho de Melo 		minfo->idiag_fmem = sk->sk_forward_alloc;
16831e6d363SEric Dumazet 		minfo->idiag_tmem = sk_wmem_alloc_get(sk);
169a8c2190eSArnaldo Carvalho de Melo 	}
170a8c2190eSArnaldo Carvalho de Melo 
171a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
172a8c2190eSArnaldo Carvalho de Melo 
173a8c2190eSArnaldo Carvalho de Melo 	if (sk->sk_state < TCP_TIME_WAIT &&
174a8c2190eSArnaldo Carvalho de Melo 	    icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
175a8c2190eSArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->get_info(sk, ext, skb);
176a8c2190eSArnaldo Carvalho de Melo 
17727a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
178a8c2190eSArnaldo Carvalho de Melo 	return skb->len;
179a8c2190eSArnaldo Carvalho de Melo 
180a8c2190eSArnaldo Carvalho de Melo rtattr_failure:
181a8c2190eSArnaldo Carvalho de Melo nlmsg_failure:
182dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
18326932566SPatrick McHardy 	return -EMSGSIZE;
184a8c2190eSArnaldo Carvalho de Melo }
185a8c2190eSArnaldo Carvalho de Melo 
186c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
187a029fe26SPavel Emelyanov 			       struct sk_buff *skb, struct inet_diag_req *req,
188a029fe26SPavel Emelyanov 			       u32 pid, u32 seq, u16 nlmsg_flags,
189c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
190c7d58aabSArnaldo Carvalho de Melo {
191c7d58aabSArnaldo Carvalho de Melo 	long tmo;
192c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
19327a884dcSArnaldo Carvalho de Melo 	const unsigned char *previous_tail = skb_tail_pointer(skb);
194c7d58aabSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
195c7d58aabSArnaldo Carvalho de Melo 					 unlh->nlmsg_type, sizeof(*r));
196c7d58aabSArnaldo Carvalho de Melo 
197c7d58aabSArnaldo Carvalho de Melo 	r = NLMSG_DATA(nlh);
198c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
199c7d58aabSArnaldo Carvalho de Melo 
200c7d58aabSArnaldo Carvalho de Melo 	nlh->nlmsg_flags = nlmsg_flags;
201c7d58aabSArnaldo Carvalho de Melo 
202c7d58aabSArnaldo Carvalho de Melo 	tmo = tw->tw_ttd - jiffies;
203c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
204c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
205c7d58aabSArnaldo Carvalho de Melo 
206c7d58aabSArnaldo Carvalho de Melo 	r->idiag_family	      = tw->tw_family;
207c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
208c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_if	      = tw->tw_bound_dev_if;
209c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
210c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
211c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_sport     = tw->tw_sport;
212c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dport     = tw->tw_dport;
213c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_src[0]    = tw->tw_rcv_saddr;
214c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dst[0]    = tw->tw_daddr;
215c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
216c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
217172589ccSIlpo Järvinen 	r->idiag_expires      = DIV_ROUND_UP(tmo * 1000, HZ);
218c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
219c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
220c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
221c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
222c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
223c7d58aabSArnaldo Carvalho de Melo 	if (tw->tw_family == AF_INET6) {
224c7d58aabSArnaldo Carvalho de Melo 		const struct inet6_timewait_sock *tw6 =
225c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
226c7d58aabSArnaldo Carvalho de Melo 
2274e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
2284e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
229c7d58aabSArnaldo Carvalho de Melo 	}
230c7d58aabSArnaldo Carvalho de Melo #endif
23127a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
232c7d58aabSArnaldo Carvalho de Melo 	return skb->len;
233c7d58aabSArnaldo Carvalho de Melo nlmsg_failure:
234dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, previous_tail);
23526932566SPatrick McHardy 	return -EMSGSIZE;
236c7d58aabSArnaldo Carvalho de Melo }
237c7d58aabSArnaldo Carvalho de Melo 
238dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
239a029fe26SPavel Emelyanov 			struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags,
240dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
241dff2c035SArnaldo Carvalho de Melo {
242dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
243dff2c035SArnaldo Carvalho de Melo 		return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
244a029fe26SPavel Emelyanov 					   skb, r, pid, seq, nlmsg_flags,
245dff2c035SArnaldo Carvalho de Melo 					   unlh);
246a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh);
247dff2c035SArnaldo Carvalho de Melo }
248dff2c035SArnaldo Carvalho de Melo 
2494e852c02SArnaldo Carvalho de Melo static int inet_diag_get_exact(struct sk_buff *in_skb,
250fe50ce28SPavel Emelyanov 			       const struct nlmsghdr *nlh,
251fe50ce28SPavel Emelyanov 			       struct inet_diag_req *req)
252a8c2190eSArnaldo Carvalho de Melo {
253a8c2190eSArnaldo Carvalho de Melo 	int err;
254a8c2190eSArnaldo Carvalho de Melo 	struct sock *sk;
255a8c2190eSArnaldo Carvalho de Melo 	struct sk_buff *rep;
256a8c2190eSArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo;
257a8c2190eSArnaldo Carvalho de Melo 	const struct inet_diag_handler *handler;
258a8c2190eSArnaldo Carvalho de Melo 
259fe50ce28SPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
2608cf8e5a6SArnaldo Carvalho de Melo 	if (IS_ERR(handler)) {
2618cf8e5a6SArnaldo Carvalho de Melo 		err = PTR_ERR(handler);
2628cf8e5a6SArnaldo Carvalho de Melo 		goto unlock;
2638cf8e5a6SArnaldo Carvalho de Melo 	}
264d523a328SHerbert Xu 
265a8c2190eSArnaldo Carvalho de Melo 	hashinfo = handler->idiag_hashinfo;
266d523a328SHerbert Xu 	err = -EINVAL;
267a8c2190eSArnaldo Carvalho de Melo 
268fe50ce28SPavel Emelyanov 	if (req->sdiag_family == AF_INET) {
269c67499c0SPavel Emelyanov 		sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
270a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
271a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
272a8c2190eSArnaldo Carvalho de Melo 	}
273a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
274fe50ce28SPavel Emelyanov 	else if (req->sdiag_family == AF_INET6) {
275d86e0dacSPavel Emelyanov 		sk = inet6_lookup(&init_net, hashinfo,
276a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
277a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
278a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
279a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
280a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
281a8c2190eSArnaldo Carvalho de Melo 	}
282a8c2190eSArnaldo Carvalho de Melo #endif
283a8c2190eSArnaldo Carvalho de Melo 	else {
284d523a328SHerbert Xu 		goto unlock;
285a8c2190eSArnaldo Carvalho de Melo 	}
286a8c2190eSArnaldo Carvalho de Melo 
287d523a328SHerbert Xu 	err = -ENOENT;
288a8c2190eSArnaldo Carvalho de Melo 	if (sk == NULL)
289d523a328SHerbert Xu 		goto unlock;
290a8c2190eSArnaldo Carvalho de Melo 
291a8c2190eSArnaldo Carvalho de Melo 	err = -ESTALE;
292a8c2190eSArnaldo Carvalho de Melo 	if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
293a8c2190eSArnaldo Carvalho de Melo 	     req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
294a8c2190eSArnaldo Carvalho de Melo 	    ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
295a8c2190eSArnaldo Carvalho de Melo 	     (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
296a8c2190eSArnaldo Carvalho de Melo 		goto out;
297a8c2190eSArnaldo Carvalho de Melo 
298a8c2190eSArnaldo Carvalho de Melo 	err = -ENOMEM;
299a8c2190eSArnaldo Carvalho de Melo 	rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
300a8c2190eSArnaldo Carvalho de Melo 				     sizeof(struct inet_diag_meminfo) +
301*7b35eaddSPavel Emelyanov 				     sizeof(struct tcp_info) + 64)),
302a8c2190eSArnaldo Carvalho de Melo 			GFP_KERNEL);
303a8c2190eSArnaldo Carvalho de Melo 	if (!rep)
304a8c2190eSArnaldo Carvalho de Melo 		goto out;
305a8c2190eSArnaldo Carvalho de Melo 
306a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
307a8c2190eSArnaldo Carvalho de Melo 			   NETLINK_CB(in_skb).pid,
30826932566SPatrick McHardy 			   nlh->nlmsg_seq, 0, nlh);
30926932566SPatrick McHardy 	if (err < 0) {
31026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
31126932566SPatrick McHardy 		kfree_skb(rep);
31226932566SPatrick McHardy 		goto out;
31326932566SPatrick McHardy 	}
3148ef874bfSPavel Emelyanov 	err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
315a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
316a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
317a8c2190eSArnaldo Carvalho de Melo 		err = 0;
318a8c2190eSArnaldo Carvalho de Melo 
319a8c2190eSArnaldo Carvalho de Melo out:
320a8c2190eSArnaldo Carvalho de Melo 	if (sk) {
321a8c2190eSArnaldo Carvalho de Melo 		if (sk->sk_state == TCP_TIME_WAIT)
322a8c2190eSArnaldo Carvalho de Melo 			inet_twsk_put((struct inet_timewait_sock *)sk);
323a8c2190eSArnaldo Carvalho de Melo 		else
324a8c2190eSArnaldo Carvalho de Melo 			sock_put(sk);
325a8c2190eSArnaldo Carvalho de Melo 	}
326d523a328SHerbert Xu unlock:
327d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
328a8c2190eSArnaldo Carvalho de Melo 	return err;
329a8c2190eSArnaldo Carvalho de Melo }
330a8c2190eSArnaldo Carvalho de Melo 
3319f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
332a8c2190eSArnaldo Carvalho de Melo {
333a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
334a8c2190eSArnaldo Carvalho de Melo 
335a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
336a8c2190eSArnaldo Carvalho de Melo 
337a8c2190eSArnaldo Carvalho de Melo 	if (words) {
338a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
339a8c2190eSArnaldo Carvalho de Melo 			return 0;
340a8c2190eSArnaldo Carvalho de Melo 	}
341a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
3429f855299SAl Viro 		__be32 w1, w2;
3439f855299SAl Viro 		__be32 mask;
344a8c2190eSArnaldo Carvalho de Melo 
345a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
346a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
347a8c2190eSArnaldo Carvalho de Melo 
348a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
349a8c2190eSArnaldo Carvalho de Melo 
350a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
351a8c2190eSArnaldo Carvalho de Melo 			return 0;
352a8c2190eSArnaldo Carvalho de Melo 	}
353a8c2190eSArnaldo Carvalho de Melo 
354a8c2190eSArnaldo Carvalho de Melo 	return 1;
355a8c2190eSArnaldo Carvalho de Melo }
356a8c2190eSArnaldo Carvalho de Melo 
357a8c2190eSArnaldo Carvalho de Melo 
358a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_run(const void *bc, int len,
359a8c2190eSArnaldo Carvalho de Melo 			    const struct inet_diag_entry *entry)
360a8c2190eSArnaldo Carvalho de Melo {
361a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
362a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
363a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
364a8c2190eSArnaldo Carvalho de Melo 
365a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
366a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
367a8c2190eSArnaldo Carvalho de Melo 			break;
368a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
369a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
370a8c2190eSArnaldo Carvalho de Melo 			break;
371a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
372a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
373a8c2190eSArnaldo Carvalho de Melo 			break;
374a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
375b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
376a8c2190eSArnaldo Carvalho de Melo 			break;
377a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
378a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
379a8c2190eSArnaldo Carvalho de Melo 			break;
380a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
381a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
382a8c2190eSArnaldo Carvalho de Melo 			break;
383a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
384a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
385a8c2190eSArnaldo Carvalho de Melo 			break;
386a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
387a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
388a8c2190eSArnaldo Carvalho de Melo 			struct inet_diag_hostcond *cond;
3899f855299SAl Viro 			__be32 *addr;
390a8c2190eSArnaldo Carvalho de Melo 
391a8c2190eSArnaldo Carvalho de Melo 			cond = (struct inet_diag_hostcond *)(op + 1);
392a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
393a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
394a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
395a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
396a8c2190eSArnaldo Carvalho de Melo 				break;
397a8c2190eSArnaldo Carvalho de Melo 			}
398a8c2190eSArnaldo Carvalho de Melo 
399a8c2190eSArnaldo Carvalho de Melo 			if (cond->prefix_len == 0)
400a8c2190eSArnaldo Carvalho de Melo 				break;
401a8c2190eSArnaldo Carvalho de Melo 
402a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
403a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
404a8c2190eSArnaldo Carvalho de Melo 			else
405a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
406a8c2190eSArnaldo Carvalho de Melo 
4074e852c02SArnaldo Carvalho de Melo 			if (bitstring_match(addr, cond->addr,
4084e852c02SArnaldo Carvalho de Melo 					    cond->prefix_len))
409a8c2190eSArnaldo Carvalho de Melo 				break;
410a8c2190eSArnaldo Carvalho de Melo 			if (entry->family == AF_INET6 &&
411a8c2190eSArnaldo Carvalho de Melo 			    cond->family == AF_INET) {
412a8c2190eSArnaldo Carvalho de Melo 				if (addr[0] == 0 && addr[1] == 0 &&
413a8c2190eSArnaldo Carvalho de Melo 				    addr[2] == htonl(0xffff) &&
414a8c2190eSArnaldo Carvalho de Melo 				    bitstring_match(addr + 3, cond->addr,
415a8c2190eSArnaldo Carvalho de Melo 						    cond->prefix_len))
416a8c2190eSArnaldo Carvalho de Melo 					break;
417a8c2190eSArnaldo Carvalho de Melo 			}
418a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
419a8c2190eSArnaldo Carvalho de Melo 			break;
420a8c2190eSArnaldo Carvalho de Melo 		}
421a8c2190eSArnaldo Carvalho de Melo 		}
422a8c2190eSArnaldo Carvalho de Melo 
423a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
424a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
425a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
426a8c2190eSArnaldo Carvalho de Melo 		} else {
427a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
428a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
429a8c2190eSArnaldo Carvalho de Melo 		}
430a8c2190eSArnaldo Carvalho de Melo 	}
431a02cec21SEric Dumazet 	return len == 0;
432a8c2190eSArnaldo Carvalho de Melo }
433a8c2190eSArnaldo Carvalho de Melo 
434a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
435a8c2190eSArnaldo Carvalho de Melo {
436a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
437a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
438a8c2190eSArnaldo Carvalho de Melo 
439a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
440a8c2190eSArnaldo Carvalho de Melo 			return 0;
441a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
442a8c2190eSArnaldo Carvalho de Melo 			return 1;
443eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
444a8c2190eSArnaldo Carvalho de Melo 			return 0;
445a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
446a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
447a8c2190eSArnaldo Carvalho de Melo 	}
448a8c2190eSArnaldo Carvalho de Melo 	return 0;
449a8c2190eSArnaldo Carvalho de Melo }
450a8c2190eSArnaldo Carvalho de Melo 
451a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
452a8c2190eSArnaldo Carvalho de Melo {
453eeb14972SEric Dumazet 	const void *bc = bytecode;
454a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
455a8c2190eSArnaldo Carvalho de Melo 
456a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
457eeb14972SEric Dumazet 		const struct inet_diag_bc_op *op = bc;
458a8c2190eSArnaldo Carvalho de Melo 
459a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
460a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
461a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
462a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
463a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
464a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
465a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
466a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
467a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
468a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
469eeb14972SEric Dumazet 			if (op->no < 4 || op->no > len + 4 || op->no & 3)
470a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
471a8c2190eSArnaldo Carvalho de Melo 			if (op->no < len &&
472a8c2190eSArnaldo Carvalho de Melo 			    !valid_cc(bytecode, bytecode_len, len - op->no))
473a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
474a8c2190eSArnaldo Carvalho de Melo 			break;
475a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
476a8c2190eSArnaldo Carvalho de Melo 			break;
477a8c2190eSArnaldo Carvalho de Melo 		default:
478a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
479a8c2190eSArnaldo Carvalho de Melo 		}
480eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
481eeb14972SEric Dumazet 			return -EINVAL;
482a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
483a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
484a8c2190eSArnaldo Carvalho de Melo 	}
485a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
486a8c2190eSArnaldo Carvalho de Melo }
487a8c2190eSArnaldo Carvalho de Melo 
488dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
489dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
49037f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
49125c4cd2bSPavel Emelyanov 			      struct inet_diag_req *r,
49237f352b5SPavel Emelyanov 			      const struct nlattr *bc)
493a8c2190eSArnaldo Carvalho de Melo {
49437f352b5SPavel Emelyanov 	if (bc != NULL) {
495a8c2190eSArnaldo Carvalho de Melo 		struct inet_diag_entry entry;
496a8c2190eSArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
497a8c2190eSArnaldo Carvalho de Melo 
498a8c2190eSArnaldo Carvalho de Melo 		entry.family = sk->sk_family;
499a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
500a8c2190eSArnaldo Carvalho de Melo 		if (entry.family == AF_INET6) {
501a8c2190eSArnaldo Carvalho de Melo 			struct ipv6_pinfo *np = inet6_sk(sk);
502a8c2190eSArnaldo Carvalho de Melo 
503a8c2190eSArnaldo Carvalho de Melo 			entry.saddr = np->rcv_saddr.s6_addr32;
504a8c2190eSArnaldo Carvalho de Melo 			entry.daddr = np->daddr.s6_addr32;
505a8c2190eSArnaldo Carvalho de Melo 		} else
506a8c2190eSArnaldo Carvalho de Melo #endif
507a8c2190eSArnaldo Carvalho de Melo 		{
508c720c7e8SEric Dumazet 			entry.saddr = &inet->inet_rcv_saddr;
509c720c7e8SEric Dumazet 			entry.daddr = &inet->inet_daddr;
510a8c2190eSArnaldo Carvalho de Melo 		}
511c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
512c720c7e8SEric Dumazet 		entry.dport = ntohs(inet->inet_dport);
513a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
514a8c2190eSArnaldo Carvalho de Melo 
51522e76c84SNelson Elhage 		if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
516a8c2190eSArnaldo Carvalho de Melo 			return 0;
517a8c2190eSArnaldo Carvalho de Melo 	}
518a8c2190eSArnaldo Carvalho de Melo 
519a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
520dff2c035SArnaldo Carvalho de Melo 				  NETLINK_CB(cb->skb).pid,
521a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
522a8c2190eSArnaldo Carvalho de Melo }
523a8c2190eSArnaldo Carvalho de Melo 
524c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
525c7d58aabSArnaldo Carvalho de Melo 			       struct sk_buff *skb,
52637f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
52725c4cd2bSPavel Emelyanov 			       struct inet_diag_req *r,
52837f352b5SPavel Emelyanov 			       const struct nlattr *bc)
529c7d58aabSArnaldo Carvalho de Melo {
53037f352b5SPavel Emelyanov 	if (bc != NULL) {
531c7d58aabSArnaldo Carvalho de Melo 		struct inet_diag_entry entry;
532c7d58aabSArnaldo Carvalho de Melo 
533c7d58aabSArnaldo Carvalho de Melo 		entry.family = tw->tw_family;
534c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
535c7d58aabSArnaldo Carvalho de Melo 		if (tw->tw_family == AF_INET6) {
536c7d58aabSArnaldo Carvalho de Melo 			struct inet6_timewait_sock *tw6 =
537c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
538c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
539c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = tw6->tw_v6_daddr.s6_addr32;
540c7d58aabSArnaldo Carvalho de Melo 		} else
541c7d58aabSArnaldo Carvalho de Melo #endif
542c7d58aabSArnaldo Carvalho de Melo 		{
543c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = &tw->tw_rcv_saddr;
544c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = &tw->tw_daddr;
545c7d58aabSArnaldo Carvalho de Melo 		}
546c7d58aabSArnaldo Carvalho de Melo 		entry.sport = tw->tw_num;
547c7d58aabSArnaldo Carvalho de Melo 		entry.dport = ntohs(tw->tw_dport);
548c7d58aabSArnaldo Carvalho de Melo 		entry.userlocks = 0;
549c7d58aabSArnaldo Carvalho de Melo 
55022e76c84SNelson Elhage 		if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
551c7d58aabSArnaldo Carvalho de Melo 			return 0;
552c7d58aabSArnaldo Carvalho de Melo 	}
553c7d58aabSArnaldo Carvalho de Melo 
554a029fe26SPavel Emelyanov 	return inet_twsk_diag_fill(tw, skb, r,
555c7d58aabSArnaldo Carvalho de Melo 				   NETLINK_CB(cb->skb).pid,
556c7d58aabSArnaldo Carvalho de Melo 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
557c7d58aabSArnaldo Carvalho de Melo }
558c7d58aabSArnaldo Carvalho de Melo 
559a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
5604e852c02SArnaldo Carvalho de Melo 			      struct request_sock *req, u32 pid, u32 seq,
561a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
562a8c2190eSArnaldo Carvalho de Melo {
563a8c2190eSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
564a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
56527a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
566a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
567a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh;
568a8c2190eSArnaldo Carvalho de Melo 	long tmo;
569a8c2190eSArnaldo Carvalho de Melo 
570a8c2190eSArnaldo Carvalho de Melo 	nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
571a8c2190eSArnaldo Carvalho de Melo 	nlh->nlmsg_flags = NLM_F_MULTI;
572a8c2190eSArnaldo Carvalho de Melo 	r = NLMSG_DATA(nlh);
573a8c2190eSArnaldo Carvalho de Melo 
574a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
575a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = TCP_SYN_RECV;
576a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 1;
577a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = req->retrans;
578a8c2190eSArnaldo Carvalho de Melo 
579a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
580a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_cookie[0] = (u32)(unsigned long)req;
581a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
582a8c2190eSArnaldo Carvalho de Melo 
583a8c2190eSArnaldo Carvalho de Melo 	tmo = req->expires - jiffies;
584a8c2190eSArnaldo Carvalho de Melo 	if (tmo < 0)
585a8c2190eSArnaldo Carvalho de Melo 		tmo = 0;
586a8c2190eSArnaldo Carvalho de Melo 
587c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
588a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dport = ireq->rmt_port;
589a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_src[0] = ireq->loc_addr;
590a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dst[0] = ireq->rmt_addr;
591a8c2190eSArnaldo Carvalho de Melo 	r->idiag_expires = jiffies_to_msecs(tmo);
592a8c2190eSArnaldo Carvalho de Melo 	r->idiag_rqueue = 0;
593a8c2190eSArnaldo Carvalho de Melo 	r->idiag_wqueue = 0;
594a8c2190eSArnaldo Carvalho de Melo 	r->idiag_uid = sock_i_uid(sk);
595a8c2190eSArnaldo Carvalho de Melo 	r->idiag_inode = 0;
596a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
597a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
5984e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
5994e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
600a8c2190eSArnaldo Carvalho de Melo 	}
601a8c2190eSArnaldo Carvalho de Melo #endif
60227a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
603a8c2190eSArnaldo Carvalho de Melo 
604a8c2190eSArnaldo Carvalho de Melo 	return skb->len;
605a8c2190eSArnaldo Carvalho de Melo 
606a8c2190eSArnaldo Carvalho de Melo nlmsg_failure:
607dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
608a8c2190eSArnaldo Carvalho de Melo 	return -1;
609a8c2190eSArnaldo Carvalho de Melo }
610a8c2190eSArnaldo Carvalho de Melo 
611a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
61237f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
61325c4cd2bSPavel Emelyanov 			       struct inet_diag_req *r,
61437f352b5SPavel Emelyanov 			       const struct nlattr *bc)
615a8c2190eSArnaldo Carvalho de Melo {
616a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_entry entry;
617a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
618a8c2190eSArnaldo Carvalho de Melo 	struct listen_sock *lopt;
619a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
620a8c2190eSArnaldo Carvalho de Melo 	int j, s_j;
621a8c2190eSArnaldo Carvalho de Melo 	int reqnum, s_reqnum;
622a8c2190eSArnaldo Carvalho de Melo 	int err = 0;
623a8c2190eSArnaldo Carvalho de Melo 
624a8c2190eSArnaldo Carvalho de Melo 	s_j = cb->args[3];
625a8c2190eSArnaldo Carvalho de Melo 	s_reqnum = cb->args[4];
626a8c2190eSArnaldo Carvalho de Melo 
627a8c2190eSArnaldo Carvalho de Melo 	if (s_j > 0)
628a8c2190eSArnaldo Carvalho de Melo 		s_j--;
629a8c2190eSArnaldo Carvalho de Melo 
630a8c2190eSArnaldo Carvalho de Melo 	entry.family = sk->sk_family;
631a8c2190eSArnaldo Carvalho de Melo 
632a8c2190eSArnaldo Carvalho de Melo 	read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
633a8c2190eSArnaldo Carvalho de Melo 
634a8c2190eSArnaldo Carvalho de Melo 	lopt = icsk->icsk_accept_queue.listen_opt;
635a8c2190eSArnaldo Carvalho de Melo 	if (!lopt || !lopt->qlen)
636a8c2190eSArnaldo Carvalho de Melo 		goto out;
637a8c2190eSArnaldo Carvalho de Melo 
63837f352b5SPavel Emelyanov 	if (bc != NULL) {
639c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
640a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
641a8c2190eSArnaldo Carvalho de Melo 	}
642a8c2190eSArnaldo Carvalho de Melo 
643a8c2190eSArnaldo Carvalho de Melo 	for (j = s_j; j < lopt->nr_table_entries; j++) {
644a8c2190eSArnaldo Carvalho de Melo 		struct request_sock *req, *head = lopt->syn_table[j];
645a8c2190eSArnaldo Carvalho de Melo 
646a8c2190eSArnaldo Carvalho de Melo 		reqnum = 0;
647a8c2190eSArnaldo Carvalho de Melo 		for (req = head; req; reqnum++, req = req->dl_next) {
648a8c2190eSArnaldo Carvalho de Melo 			struct inet_request_sock *ireq = inet_rsk(req);
649a8c2190eSArnaldo Carvalho de Melo 
650a8c2190eSArnaldo Carvalho de Melo 			if (reqnum < s_reqnum)
651a8c2190eSArnaldo Carvalho de Melo 				continue;
652a8c2190eSArnaldo Carvalho de Melo 			if (r->id.idiag_dport != ireq->rmt_port &&
653a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_dport)
654a8c2190eSArnaldo Carvalho de Melo 				continue;
655a8c2190eSArnaldo Carvalho de Melo 
656a8c2190eSArnaldo Carvalho de Melo 			if (bc) {
657a8c2190eSArnaldo Carvalho de Melo 				entry.saddr =
658a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
659a8c2190eSArnaldo Carvalho de Melo 					(entry.family == AF_INET6) ?
660ca304b61SArnaldo Carvalho de Melo 					inet6_rsk(req)->loc_addr.s6_addr32 :
661a8c2190eSArnaldo Carvalho de Melo #endif
662a8c2190eSArnaldo Carvalho de Melo 					&ireq->loc_addr;
663a8c2190eSArnaldo Carvalho de Melo 				entry.daddr =
664a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
665a8c2190eSArnaldo Carvalho de Melo 					(entry.family == AF_INET6) ?
666ca304b61SArnaldo Carvalho de Melo 					inet6_rsk(req)->rmt_addr.s6_addr32 :
667a8c2190eSArnaldo Carvalho de Melo #endif
668a8c2190eSArnaldo Carvalho de Melo 					&ireq->rmt_addr;
669a8c2190eSArnaldo Carvalho de Melo 				entry.dport = ntohs(ireq->rmt_port);
670a8c2190eSArnaldo Carvalho de Melo 
67122e76c84SNelson Elhage 				if (!inet_diag_bc_run(nla_data(bc),
67222e76c84SNelson Elhage 						      nla_len(bc), &entry))
673a8c2190eSArnaldo Carvalho de Melo 					continue;
674a8c2190eSArnaldo Carvalho de Melo 			}
675a8c2190eSArnaldo Carvalho de Melo 
676a8c2190eSArnaldo Carvalho de Melo 			err = inet_diag_fill_req(skb, sk, req,
677a8c2190eSArnaldo Carvalho de Melo 					       NETLINK_CB(cb->skb).pid,
678a8c2190eSArnaldo Carvalho de Melo 					       cb->nlh->nlmsg_seq, cb->nlh);
679a8c2190eSArnaldo Carvalho de Melo 			if (err < 0) {
680a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = j + 1;
681a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = reqnum;
682a8c2190eSArnaldo Carvalho de Melo 				goto out;
683a8c2190eSArnaldo Carvalho de Melo 			}
684a8c2190eSArnaldo Carvalho de Melo 		}
685a8c2190eSArnaldo Carvalho de Melo 
686a8c2190eSArnaldo Carvalho de Melo 		s_reqnum = 0;
687a8c2190eSArnaldo Carvalho de Melo 	}
688a8c2190eSArnaldo Carvalho de Melo 
689a8c2190eSArnaldo Carvalho de Melo out:
690a8c2190eSArnaldo Carvalho de Melo 	read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
691a8c2190eSArnaldo Carvalho de Melo 
692a8c2190eSArnaldo Carvalho de Melo 	return err;
693a8c2190eSArnaldo Carvalho de Melo }
694a8c2190eSArnaldo Carvalho de Melo 
69525c4cd2bSPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
69625c4cd2bSPavel Emelyanov 		struct inet_diag_req *r, struct nlattr *bc)
697a8c2190eSArnaldo Carvalho de Melo {
698a8c2190eSArnaldo Carvalho de Melo 	int i, num;
699a8c2190eSArnaldo Carvalho de Melo 	int s_i, s_num;
700a8c2190eSArnaldo Carvalho de Melo 	const struct inet_diag_handler *handler;
701a8c2190eSArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo;
70237f352b5SPavel Emelyanov 
70325c4cd2bSPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
7048cf8e5a6SArnaldo Carvalho de Melo 	if (IS_ERR(handler))
7058cf8e5a6SArnaldo Carvalho de Melo 		goto unlock;
706d523a328SHerbert Xu 
707a8c2190eSArnaldo Carvalho de Melo 	hashinfo = handler->idiag_hashinfo;
708a8c2190eSArnaldo Carvalho de Melo 
709a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
710a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
711a8c2190eSArnaldo Carvalho de Melo 
712a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
713a8c2190eSArnaldo Carvalho de Melo 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
714a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
715a8c2190eSArnaldo Carvalho de Melo 
716a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
717a8c2190eSArnaldo Carvalho de Melo 			struct sock *sk;
718c25eb3bfSEric Dumazet 			struct hlist_nulls_node *node;
7195caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
720a8c2190eSArnaldo Carvalho de Melo 
721a8c2190eSArnaldo Carvalho de Melo 			num = 0;
7225caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
7235caea4eaSEric Dumazet 			spin_lock_bh(&ilb->lock);
724c25eb3bfSEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->head) {
725a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
726a8c2190eSArnaldo Carvalho de Melo 
727a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
728a8c2190eSArnaldo Carvalho de Melo 					num++;
729a8c2190eSArnaldo Carvalho de Melo 					continue;
730a8c2190eSArnaldo Carvalho de Melo 				}
731a8c2190eSArnaldo Carvalho de Melo 
732d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
733d23deaa0SPavel Emelyanov 						sk->sk_family != r->sdiag_family)
734d23deaa0SPavel Emelyanov 					goto next_listen;
735d23deaa0SPavel Emelyanov 
736c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
737a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
738a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
739a8c2190eSArnaldo Carvalho de Melo 
740a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_LISTEN) ||
741a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport ||
742a8c2190eSArnaldo Carvalho de Melo 				    cb->args[3] > 0)
743a8c2190eSArnaldo Carvalho de Melo 					goto syn_recv;
744a8c2190eSArnaldo Carvalho de Melo 
74525c4cd2bSPavel Emelyanov 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
7465caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
747a8c2190eSArnaldo Carvalho de Melo 					goto done;
748a8c2190eSArnaldo Carvalho de Melo 				}
749a8c2190eSArnaldo Carvalho de Melo 
750a8c2190eSArnaldo Carvalho de Melo syn_recv:
751a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_SYN_RECV))
752a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
753a8c2190eSArnaldo Carvalho de Melo 
75425c4cd2bSPavel Emelyanov 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
7555caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
756a8c2190eSArnaldo Carvalho de Melo 					goto done;
757a8c2190eSArnaldo Carvalho de Melo 				}
758a8c2190eSArnaldo Carvalho de Melo 
759a8c2190eSArnaldo Carvalho de Melo next_listen:
760a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = 0;
761a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = 0;
762a8c2190eSArnaldo Carvalho de Melo 				++num;
763a8c2190eSArnaldo Carvalho de Melo 			}
7645caea4eaSEric Dumazet 			spin_unlock_bh(&ilb->lock);
765a8c2190eSArnaldo Carvalho de Melo 
766a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
767a8c2190eSArnaldo Carvalho de Melo 			cb->args[3] = 0;
768a8c2190eSArnaldo Carvalho de Melo 			cb->args[4] = 0;
769a8c2190eSArnaldo Carvalho de Melo 		}
770a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
771a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
772a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
773a8c2190eSArnaldo Carvalho de Melo 	}
774a8c2190eSArnaldo Carvalho de Melo 
775a8c2190eSArnaldo Carvalho de Melo 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
776d523a328SHerbert Xu 		goto unlock;
777a8c2190eSArnaldo Carvalho de Melo 
778f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
779a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
7807e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
781a8c2190eSArnaldo Carvalho de Melo 		struct sock *sk;
7823ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
783a8c2190eSArnaldo Carvalho de Melo 
7846be547a6SAndi Kleen 		num = 0;
7856be547a6SAndi Kleen 
7863ab5aee7SEric Dumazet 		if (hlist_nulls_empty(&head->chain) &&
7873ab5aee7SEric Dumazet 			hlist_nulls_empty(&head->twchain))
7886be547a6SAndi Kleen 			continue;
7896be547a6SAndi Kleen 
790a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
791a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
792a8c2190eSArnaldo Carvalho de Melo 
7937e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
7943ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
795a8c2190eSArnaldo Carvalho de Melo 			struct inet_sock *inet = inet_sk(sk);
796a8c2190eSArnaldo Carvalho de Melo 
797a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
798a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
799a8c2190eSArnaldo Carvalho de Melo 			if (!(r->idiag_states & (1 << sk->sk_state)))
800a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
801d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
802d23deaa0SPavel Emelyanov 					sk->sk_family != r->sdiag_family)
803d23deaa0SPavel Emelyanov 				goto next_normal;
804c720c7e8SEric Dumazet 			if (r->id.idiag_sport != inet->inet_sport &&
805a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
806a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
807c720c7e8SEric Dumazet 			if (r->id.idiag_dport != inet->inet_dport &&
8084e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
809a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
81025c4cd2bSPavel Emelyanov 			if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
8117e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
812a8c2190eSArnaldo Carvalho de Melo 				goto done;
813a8c2190eSArnaldo Carvalho de Melo 			}
814a8c2190eSArnaldo Carvalho de Melo next_normal:
815a8c2190eSArnaldo Carvalho de Melo 			++num;
816a8c2190eSArnaldo Carvalho de Melo 		}
817a8c2190eSArnaldo Carvalho de Melo 
818a8c2190eSArnaldo Carvalho de Melo 		if (r->idiag_states & TCPF_TIME_WAIT) {
819c7d58aabSArnaldo Carvalho de Melo 			struct inet_timewait_sock *tw;
820c7d58aabSArnaldo Carvalho de Melo 
821c7d58aabSArnaldo Carvalho de Melo 			inet_twsk_for_each(tw, node,
822dbca9b27SEric Dumazet 				    &head->twchain) {
823a8c2190eSArnaldo Carvalho de Melo 
824a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num)
825a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
826d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
827d23deaa0SPavel Emelyanov 						tw->tw_family != r->sdiag_family)
828d23deaa0SPavel Emelyanov 					goto next_dying;
8297dbf0755SArnaldo Carvalho de Melo 				if (r->id.idiag_sport != tw->tw_sport &&
830a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
831a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
8327dbf0755SArnaldo Carvalho de Melo 				if (r->id.idiag_dport != tw->tw_dport &&
833a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport)
834a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
83525c4cd2bSPavel Emelyanov 				if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
8367e3aab4aSDavid S. Miller 					spin_unlock_bh(lock);
837a8c2190eSArnaldo Carvalho de Melo 					goto done;
838a8c2190eSArnaldo Carvalho de Melo 				}
839a8c2190eSArnaldo Carvalho de Melo next_dying:
840a8c2190eSArnaldo Carvalho de Melo 				++num;
841a8c2190eSArnaldo Carvalho de Melo 			}
842a8c2190eSArnaldo Carvalho de Melo 		}
8437e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
844a8c2190eSArnaldo Carvalho de Melo 	}
845a8c2190eSArnaldo Carvalho de Melo 
846a8c2190eSArnaldo Carvalho de Melo done:
847a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
848a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
849d523a328SHerbert Xu unlock:
850d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
851a8c2190eSArnaldo Carvalho de Melo 	return skb->len;
852a8c2190eSArnaldo Carvalho de Melo }
853a8c2190eSArnaldo Carvalho de Melo 
85425c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
85525c4cd2bSPavel Emelyanov {
85625c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
85725c4cd2bSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
85825c4cd2bSPavel Emelyanov 
85925c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
86025c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
86125c4cd2bSPavel Emelyanov 
86225c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc);
86325c4cd2bSPavel Emelyanov }
86425c4cd2bSPavel Emelyanov 
865a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type)
866a029fe26SPavel Emelyanov {
867a029fe26SPavel Emelyanov 	switch (type) {
868a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
869a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
870a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
871a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
872a029fe26SPavel Emelyanov 	default:
873a029fe26SPavel Emelyanov 		return 0;
874a029fe26SPavel Emelyanov 	}
875a029fe26SPavel Emelyanov }
876a029fe26SPavel Emelyanov 
87725c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
87825c4cd2bSPavel Emelyanov {
87925c4cd2bSPavel Emelyanov 	struct inet_diag_req_compat *rc = NLMSG_DATA(cb->nlh);
88025c4cd2bSPavel Emelyanov 	struct inet_diag_req req;
88125c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
88225c4cd2bSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_compat);
88325c4cd2bSPavel Emelyanov 
884d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
88525c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
88625c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
88725c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
88825c4cd2bSPavel Emelyanov 	req.id = rc->id;
88925c4cd2bSPavel Emelyanov 
89025c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
89125c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
89225c4cd2bSPavel Emelyanov 
89325c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
89425c4cd2bSPavel Emelyanov }
89525c4cd2bSPavel Emelyanov 
896fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
897fe50ce28SPavel Emelyanov 			       const struct nlmsghdr *nlh)
898fe50ce28SPavel Emelyanov {
899fe50ce28SPavel Emelyanov 	struct inet_diag_req_compat *rc = NLMSG_DATA(nlh);
900fe50ce28SPavel Emelyanov 	struct inet_diag_req req;
901fe50ce28SPavel Emelyanov 
902fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
903fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
904fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
905fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
906fe50ce28SPavel Emelyanov 	req.id = rc->id;
907fe50ce28SPavel Emelyanov 
908fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
909fe50ce28SPavel Emelyanov }
910fe50ce28SPavel Emelyanov 
9118d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
912a8c2190eSArnaldo Carvalho de Melo {
913126fdc32SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_compat);
914a8c2190eSArnaldo Carvalho de Melo 
915ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
916ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
917ead592baSThomas Graf 		return -EINVAL;
918a8c2190eSArnaldo Carvalho de Melo 
919b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
920ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
921ead592baSThomas Graf 			struct nlattr *attr;
922ead592baSThomas Graf 
923ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
924ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
925ead592baSThomas Graf 			if (attr == NULL ||
926ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
927ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
928a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
929a8c2190eSArnaldo Carvalho de Melo 		}
930a8c2190eSArnaldo Carvalho de Melo 
9318ef874bfSPavel Emelyanov 		return netlink_dump_start(sock_diag_nlsk, skb, nlh,
93225c4cd2bSPavel Emelyanov 					  inet_diag_dump_compat, NULL, 0);
933a8c2190eSArnaldo Carvalho de Melo 	}
934ead592baSThomas Graf 
935fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
936a8c2190eSArnaldo Carvalho de Melo }
937a8c2190eSArnaldo Carvalho de Melo 
938d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
939d366477aSPavel Emelyanov {
940d366477aSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
941d366477aSPavel Emelyanov 
942d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
943d366477aSPavel Emelyanov 		return -EINVAL;
944d366477aSPavel Emelyanov 
945d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
94625c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
94725c4cd2bSPavel Emelyanov 			struct nlattr *attr;
94825c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
94925c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
95025c4cd2bSPavel Emelyanov 			if (attr == NULL ||
95125c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
95225c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
95325c4cd2bSPavel Emelyanov 				return -EINVAL;
95425c4cd2bSPavel Emelyanov 		}
95525c4cd2bSPavel Emelyanov 
9568ef874bfSPavel Emelyanov 		return netlink_dump_start(sock_diag_nlsk, skb, h,
95725c4cd2bSPavel Emelyanov 					  inet_diag_dump, NULL, 0);
958d366477aSPavel Emelyanov 	}
959d366477aSPavel Emelyanov 
960fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h));
961d366477aSPavel Emelyanov }
962d366477aSPavel Emelyanov 
963d366477aSPavel Emelyanov static struct sock_diag_handler inet_diag_handler = {
964d366477aSPavel Emelyanov 	.family = AF_INET,
965d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
966d366477aSPavel Emelyanov };
967d366477aSPavel Emelyanov 
968d366477aSPavel Emelyanov static struct sock_diag_handler inet6_diag_handler = {
969d366477aSPavel Emelyanov 	.family = AF_INET6,
970d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
971d366477aSPavel Emelyanov };
972d366477aSPavel Emelyanov 
973a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
974a8c2190eSArnaldo Carvalho de Melo {
975a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
976a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
977a8c2190eSArnaldo Carvalho de Melo 
978f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
979a8c2190eSArnaldo Carvalho de Melo 		goto out;
980a8c2190eSArnaldo Carvalho de Melo 
981d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
982a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
983a8c2190eSArnaldo Carvalho de Melo 	if (inet_diag_table[type] == NULL) {
984a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
985a8c2190eSArnaldo Carvalho de Melo 		err = 0;
986a8c2190eSArnaldo Carvalho de Melo 	}
987d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
988a8c2190eSArnaldo Carvalho de Melo out:
989a8c2190eSArnaldo Carvalho de Melo 	return err;
990a8c2190eSArnaldo Carvalho de Melo }
991a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
992a8c2190eSArnaldo Carvalho de Melo 
993a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
994a8c2190eSArnaldo Carvalho de Melo {
995a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
996a8c2190eSArnaldo Carvalho de Melo 
997f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
998a8c2190eSArnaldo Carvalho de Melo 		return;
999a8c2190eSArnaldo Carvalho de Melo 
1000d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1001a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1002d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1003a8c2190eSArnaldo Carvalho de Melo }
1004a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1005a8c2190eSArnaldo Carvalho de Melo 
1006a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1007a8c2190eSArnaldo Carvalho de Melo {
1008f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1009a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1010a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1011a8c2190eSArnaldo Carvalho de Melo 
10120da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1013a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1014a8c2190eSArnaldo Carvalho de Melo 		goto out;
1015a8c2190eSArnaldo Carvalho de Melo 
1016d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1017d366477aSPavel Emelyanov 	if (err)
1018d366477aSPavel Emelyanov 		goto out_free_nl;
1019d366477aSPavel Emelyanov 
1020d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1021d366477aSPavel Emelyanov 	if (err)
1022d366477aSPavel Emelyanov 		goto out_free_inet;
1023d366477aSPavel Emelyanov 
10248ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1025a8c2190eSArnaldo Carvalho de Melo out:
1026a8c2190eSArnaldo Carvalho de Melo 	return err;
1027d366477aSPavel Emelyanov 
1028d366477aSPavel Emelyanov out_free_inet:
1029d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1030d366477aSPavel Emelyanov out_free_nl:
1031a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1032a8c2190eSArnaldo Carvalho de Melo 	goto out;
1033a8c2190eSArnaldo Carvalho de Melo }
1034a8c2190eSArnaldo Carvalho de Melo 
1035a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1036a8c2190eSArnaldo Carvalho de Melo {
1037d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1038d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
10398ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1040a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1041a8c2190eSArnaldo Carvalho de Melo }
1042a8c2190eSArnaldo Carvalho de Melo 
1043a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1044a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1045a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
10468ef874bfSPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 0);
1047