xref: /linux/net/ipv4/inet_diag.c (revision 05dbc7b59481ca891bbcfe6799a562d48159fbf7)
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;
471c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
481c95df85SNeal Cardwell 	struct in6_addr saddr_storage;	/* for IPv4-mapped-IPv6 addresses */
491c95df85SNeal Cardwell 	struct in6_addr daddr_storage;	/* for IPv4-mapped-IPv6 addresses */
501c95df85SNeal Cardwell #endif
51a8c2190eSArnaldo Carvalho de Melo };
52a8c2190eSArnaldo Carvalho de Melo 
53d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
54d523a328SHerbert Xu 
55f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
56d523a328SHerbert Xu {
57f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
58aec8dc62SPavel Emelyanov 		request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
59aec8dc62SPavel Emelyanov 			       NETLINK_SOCK_DIAG, AF_INET, proto);
60d523a328SHerbert Xu 
61d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
62f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
63d523a328SHerbert Xu 		return ERR_PTR(-ENOENT);
64d523a328SHerbert Xu 
65f13c95f0SPavel Emelyanov 	return inet_diag_table[proto];
66d523a328SHerbert Xu }
67d523a328SHerbert Xu 
68d523a328SHerbert Xu static inline void inet_diag_unlock_handler(
69d523a328SHerbert Xu 	const struct inet_diag_handler *handler)
70d523a328SHerbert Xu {
71d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
72d523a328SHerbert Xu }
73d523a328SHerbert Xu 
743c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
75c8991362SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req_v2 *req,
76d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
7715e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
78a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
79a8c2190eSArnaldo Carvalho de Melo {
80a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
81a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
82a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr  *nlh;
836e277ed5SThomas Graf 	struct nlattr *attr;
84a8c2190eSArnaldo Carvalho de Melo 	void *info = NULL;
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 
9115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
926e277ed5SThomas Graf 			nlmsg_flags);
936e277ed5SThomas Graf 	if (!nlh)
94d106352dSDavid S. Miller 		return -EMSGSIZE;
95a8c2190eSArnaldo Carvalho de Melo 
96d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
97c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(sk->sk_state == TCP_TIME_WAIT);
98c7d58aabSArnaldo Carvalho de Melo 
99a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
100a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = sk->sk_state;
101a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 0;
102a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = 0;
103a8c2190eSArnaldo Carvalho de Melo 
104a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
105f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
106a8c2190eSArnaldo Carvalho de Melo 
107c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
108c720c7e8SEric Dumazet 	r->id.idiag_dport = inet->inet_dport;
109c720c7e8SEric Dumazet 	r->id.idiag_src[0] = inet->inet_rcv_saddr;
110c720c7e8SEric Dumazet 	r->id.idiag_dst[0] = inet->inet_daddr;
111a8c2190eSArnaldo Carvalho de Melo 
112e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
113e4e541a8SPavel Emelyanov 		goto errout;
114e4e541a8SPavel Emelyanov 
115717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
116717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
117717b6d83SMaciej Żenczykowski 	 */
118717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1196e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1206e277ed5SThomas Graf 			goto errout;
121717b6d83SMaciej Żenczykowski 
122dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
123a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
124b71d1d42SEric Dumazet 		const struct ipv6_pinfo *np = inet6_sk(sk);
125a8c2190eSArnaldo Carvalho de Melo 
1264e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
1274e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = np->daddr;
1286e277ed5SThomas Graf 
12906236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
1306e277ed5SThomas Graf 			if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0)
1316e277ed5SThomas Graf 				goto errout;
132a8c2190eSArnaldo Carvalho de Melo 	}
133a8c2190eSArnaldo Carvalho de Melo #endif
134a8c2190eSArnaldo Carvalho de Melo 
135d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1363c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1373c4d05c8SPavel Emelyanov 
1386e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
1396e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
1406e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
1416e277ed5SThomas Graf 			.idiag_wmem = sk->sk_wmem_queued,
1426e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
1436e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
1446e277ed5SThomas Graf 		};
1456e277ed5SThomas Graf 
1466e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
1476e277ed5SThomas Graf 			goto errout;
1483c4d05c8SPavel Emelyanov 	}
1493c4d05c8SPavel Emelyanov 
150c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
151c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
1526e277ed5SThomas Graf 			goto errout;
153c0636faaSPavel Emelyanov 
1543c4d05c8SPavel Emelyanov 	if (icsk == NULL) {
15562ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
1563c4d05c8SPavel Emelyanov 		goto out;
1573c4d05c8SPavel Emelyanov 	}
1583c4d05c8SPavel Emelyanov 
159172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
160a8c2190eSArnaldo Carvalho de Melo 
1616ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
1626ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
1636ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
164a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
165a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
166a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
167a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
168a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
169a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
170a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
171a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
172a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
173a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
174a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
175a8c2190eSArnaldo Carvalho de Melo 	} else {
176a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
177a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
178a8c2190eSArnaldo Carvalho de Melo 	}
179a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
180a8c2190eSArnaldo Carvalho de Melo 
1816e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_INFO - 1))) {
1826e277ed5SThomas Graf 		attr = nla_reserve(skb, INET_DIAG_INFO,
1836e277ed5SThomas Graf 				   sizeof(struct tcp_info));
1846e277ed5SThomas Graf 		if (!attr)
1856e277ed5SThomas Graf 			goto errout;
186a8c2190eSArnaldo Carvalho de Melo 
1876e277ed5SThomas Graf 		info = nla_data(attr);
188a8c2190eSArnaldo Carvalho de Melo 	}
189a8c2190eSArnaldo Carvalho de Melo 
1906e277ed5SThomas Graf 	if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
1916e277ed5SThomas Graf 		if (nla_put_string(skb, INET_DIAG_CONG,
1926e277ed5SThomas Graf 				   icsk->icsk_ca_ops->name) < 0)
1936e277ed5SThomas Graf 			goto errout;
1946e277ed5SThomas Graf 
195a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
196a8c2190eSArnaldo Carvalho de Melo 
197a8c2190eSArnaldo Carvalho de Melo 	if (sk->sk_state < TCP_TIME_WAIT &&
198a8c2190eSArnaldo Carvalho de Melo 	    icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
199a8c2190eSArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->get_info(sk, ext, skb);
200a8c2190eSArnaldo Carvalho de Melo 
2013c4d05c8SPavel Emelyanov out:
2026e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
203a8c2190eSArnaldo Carvalho de Melo 
2046e277ed5SThomas Graf errout:
2056e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
20626932566SPatrick McHardy 	return -EMSGSIZE;
207a8c2190eSArnaldo Carvalho de Melo }
2083c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
2093c4d05c8SPavel Emelyanov 
2103c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
211c8991362SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req_v2 *req,
212d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
21315e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
2143c4d05c8SPavel Emelyanov 			      const struct nlmsghdr *unlh)
2153c4d05c8SPavel Emelyanov {
2163c4d05c8SPavel Emelyanov 	return inet_sk_diag_fill(sk, inet_csk(sk),
21715e47304SEric W. Biederman 			skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
2183c4d05c8SPavel Emelyanov }
219a8c2190eSArnaldo Carvalho de Melo 
220c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
221c8991362SPavel Emelyanov 			       struct sk_buff *skb, struct inet_diag_req_v2 *req,
22215e47304SEric W. Biederman 			       u32 portid, u32 seq, u16 nlmsg_flags,
223c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
224c7d58aabSArnaldo Carvalho de Melo {
22596f817feSEric Dumazet 	s32 tmo;
226c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
2276e277ed5SThomas Graf 	struct nlmsghdr *nlh;
228c7d58aabSArnaldo Carvalho de Melo 
22915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
2306e277ed5SThomas Graf 			nlmsg_flags);
2316e277ed5SThomas Graf 	if (!nlh)
232d106352dSDavid S. Miller 		return -EMSGSIZE;
233d106352dSDavid S. Miller 
234d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
235c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
236c7d58aabSArnaldo Carvalho de Melo 
23796f817feSEric Dumazet 	tmo = tw->tw_ttd - inet_tw_time_stamp();
238c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
239c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
240c7d58aabSArnaldo Carvalho de Melo 
241c7d58aabSArnaldo Carvalho de Melo 	r->idiag_family	      = tw->tw_family;
242c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
243c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_if	      = tw->tw_bound_dev_if;
244f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(tw, r->id.idiag_cookie);
245c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_sport     = tw->tw_sport;
246c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dport     = tw->tw_dport;
247c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_src[0]    = tw->tw_rcv_saddr;
248c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dst[0]    = tw->tw_daddr;
249c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
250c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
25196f817feSEric Dumazet 	r->idiag_expires      = jiffies_to_msecs(tmo);
252c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
253c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
254c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
255c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
256dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
257c7d58aabSArnaldo Carvalho de Melo 	if (tw->tw_family == AF_INET6) {
258c7d58aabSArnaldo Carvalho de Melo 		const struct inet6_timewait_sock *tw6 =
259c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
260c7d58aabSArnaldo Carvalho de Melo 
2614e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
2624e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
263c7d58aabSArnaldo Carvalho de Melo 	}
264c7d58aabSArnaldo Carvalho de Melo #endif
2656e277ed5SThomas Graf 
2666e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
267c7d58aabSArnaldo Carvalho de Melo }
268c7d58aabSArnaldo Carvalho de Melo 
269dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
270d06ca956SEric W. Biederman 			struct inet_diag_req_v2 *r,
271d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
27215e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
273dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
274dff2c035SArnaldo Carvalho de Melo {
275dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
276dff2c035SArnaldo Carvalho de Melo 		return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
27715e47304SEric W. Biederman 					   skb, r, portid, seq, nlmsg_flags,
278dff2c035SArnaldo Carvalho de Melo 					   unlh);
27915e47304SEric W. Biederman 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh);
280dff2c035SArnaldo Carvalho de Melo }
281dff2c035SArnaldo Carvalho de Melo 
2821942c518SPavel Emelyanov int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
283c8991362SPavel Emelyanov 		const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
284a8c2190eSArnaldo Carvalho de Melo {
285a8c2190eSArnaldo Carvalho de Melo 	int err;
286a8c2190eSArnaldo Carvalho de Melo 	struct sock *sk;
287a8c2190eSArnaldo Carvalho de Melo 	struct sk_buff *rep;
28851d7cccfSAndrey Vagin 	struct net *net = sock_net(in_skb->sk);
289a8c2190eSArnaldo Carvalho de Melo 
290d523a328SHerbert Xu 	err = -EINVAL;
291fe50ce28SPavel Emelyanov 	if (req->sdiag_family == AF_INET) {
29251d7cccfSAndrey Vagin 		sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
293a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
294a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
295a8c2190eSArnaldo Carvalho de Melo 	}
296dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
297fe50ce28SPavel Emelyanov 	else if (req->sdiag_family == AF_INET6) {
29851d7cccfSAndrey Vagin 		sk = inet6_lookup(net, hashinfo,
299a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
300a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
301a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
302a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
303a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
304a8c2190eSArnaldo Carvalho de Melo 	}
305a8c2190eSArnaldo Carvalho de Melo #endif
306a8c2190eSArnaldo Carvalho de Melo 	else {
307476f7dbfSPavel Emelyanov 		goto out_nosk;
308a8c2190eSArnaldo Carvalho de Melo 	}
309a8c2190eSArnaldo Carvalho de Melo 
310d523a328SHerbert Xu 	err = -ENOENT;
311a8c2190eSArnaldo Carvalho de Melo 	if (sk == NULL)
312476f7dbfSPavel Emelyanov 		goto out_nosk;
313a8c2190eSArnaldo Carvalho de Melo 
314f65c1b53SPavel Emelyanov 	err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
315b005ab4eSPavel Emelyanov 	if (err)
316a8c2190eSArnaldo Carvalho de Melo 		goto out;
317a8c2190eSArnaldo Carvalho de Melo 
3186e277ed5SThomas Graf 	rep = nlmsg_new(sizeof(struct inet_diag_msg) +
319a8c2190eSArnaldo Carvalho de Melo 			sizeof(struct inet_diag_meminfo) +
3206e277ed5SThomas Graf 			sizeof(struct tcp_info) + 64, GFP_KERNEL);
3216e277ed5SThomas Graf 	if (!rep) {
3226e277ed5SThomas Graf 		err = -ENOMEM;
323a8c2190eSArnaldo Carvalho de Melo 		goto out;
3246e277ed5SThomas Graf 	}
325a8c2190eSArnaldo Carvalho de Melo 
326a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
327e32123e5SPatrick McHardy 			   sk_user_ns(NETLINK_CB(in_skb).sk),
32815e47304SEric W. Biederman 			   NETLINK_CB(in_skb).portid,
32926932566SPatrick McHardy 			   nlh->nlmsg_seq, 0, nlh);
33026932566SPatrick McHardy 	if (err < 0) {
33126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
3326e277ed5SThomas Graf 		nlmsg_free(rep);
33326932566SPatrick McHardy 		goto out;
33426932566SPatrick McHardy 	}
33515e47304SEric W. Biederman 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
336a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
337a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
338a8c2190eSArnaldo Carvalho de Melo 		err = 0;
339a8c2190eSArnaldo Carvalho de Melo 
340a8c2190eSArnaldo Carvalho de Melo out:
341a8c2190eSArnaldo Carvalho de Melo 	if (sk) {
342a8c2190eSArnaldo Carvalho de Melo 		if (sk->sk_state == TCP_TIME_WAIT)
343a8c2190eSArnaldo Carvalho de Melo 			inet_twsk_put((struct inet_timewait_sock *)sk);
344a8c2190eSArnaldo Carvalho de Melo 		else
345a8c2190eSArnaldo Carvalho de Melo 			sock_put(sk);
346a8c2190eSArnaldo Carvalho de Melo 	}
347476f7dbfSPavel Emelyanov out_nosk:
348476f7dbfSPavel Emelyanov 	return err;
349476f7dbfSPavel Emelyanov }
3501942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
351476f7dbfSPavel Emelyanov 
352476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb,
353476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
354c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *req)
355476f7dbfSPavel Emelyanov {
356476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
357476f7dbfSPavel Emelyanov 	int err;
358476f7dbfSPavel Emelyanov 
359476f7dbfSPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
360476f7dbfSPavel Emelyanov 	if (IS_ERR(handler))
361476f7dbfSPavel Emelyanov 		err = PTR_ERR(handler);
362476f7dbfSPavel Emelyanov 	else
3631942c518SPavel Emelyanov 		err = handler->dump_one(in_skb, nlh, req);
364d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
365476f7dbfSPavel Emelyanov 
366a8c2190eSArnaldo Carvalho de Melo 	return err;
367a8c2190eSArnaldo Carvalho de Melo }
368a8c2190eSArnaldo Carvalho de Melo 
3699f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
370a8c2190eSArnaldo Carvalho de Melo {
371a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
372a8c2190eSArnaldo Carvalho de Melo 
373a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
374a8c2190eSArnaldo Carvalho de Melo 
375a8c2190eSArnaldo Carvalho de Melo 	if (words) {
376a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
377a8c2190eSArnaldo Carvalho de Melo 			return 0;
378a8c2190eSArnaldo Carvalho de Melo 	}
379a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
3809f855299SAl Viro 		__be32 w1, w2;
3819f855299SAl Viro 		__be32 mask;
382a8c2190eSArnaldo Carvalho de Melo 
383a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
384a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
385a8c2190eSArnaldo Carvalho de Melo 
386a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
387a8c2190eSArnaldo Carvalho de Melo 
388a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
389a8c2190eSArnaldo Carvalho de Melo 			return 0;
390a8c2190eSArnaldo Carvalho de Melo 	}
391a8c2190eSArnaldo Carvalho de Melo 
392a8c2190eSArnaldo Carvalho de Melo 	return 1;
393a8c2190eSArnaldo Carvalho de Melo }
394a8c2190eSArnaldo Carvalho de Melo 
395a8c2190eSArnaldo Carvalho de Melo 
39687c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
397a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_entry *entry)
398a8c2190eSArnaldo Carvalho de Melo {
39987c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
40087c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
40187c22ea5SPavel Emelyanov 
402a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
403a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
404a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
405a8c2190eSArnaldo Carvalho de Melo 
406a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
407a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
408a8c2190eSArnaldo Carvalho de Melo 			break;
409a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
410a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
411a8c2190eSArnaldo Carvalho de Melo 			break;
412a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
413a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
414a8c2190eSArnaldo Carvalho de Melo 			break;
415a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
416b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
417a8c2190eSArnaldo Carvalho de Melo 			break;
418a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
419a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
420a8c2190eSArnaldo Carvalho de Melo 			break;
421a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
422a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
423a8c2190eSArnaldo Carvalho de Melo 			break;
424a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
425a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
426a8c2190eSArnaldo Carvalho de Melo 			break;
427a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
428a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
429a8c2190eSArnaldo Carvalho de Melo 			struct inet_diag_hostcond *cond;
4309f855299SAl Viro 			__be32 *addr;
431a8c2190eSArnaldo Carvalho de Melo 
432a8c2190eSArnaldo Carvalho de Melo 			cond = (struct inet_diag_hostcond *)(op + 1);
433a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
434a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
435a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
436a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
437a8c2190eSArnaldo Carvalho de Melo 				break;
438a8c2190eSArnaldo Carvalho de Melo 			}
439a8c2190eSArnaldo Carvalho de Melo 
440a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
441a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
442a8c2190eSArnaldo Carvalho de Melo 			else
443a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
444a8c2190eSArnaldo Carvalho de Melo 
445f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
446f67caec9SNeal Cardwell 			    cond->family != entry->family) {
447a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
448a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
449a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
450a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
451f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
452f67caec9SNeal Cardwell 							    cond->addr,
453a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
454a8c2190eSArnaldo Carvalho de Melo 						break;
455a8c2190eSArnaldo Carvalho de Melo 				}
456a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
457a8c2190eSArnaldo Carvalho de Melo 				break;
458a8c2190eSArnaldo Carvalho de Melo 			}
459f67caec9SNeal Cardwell 
460f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
461f67caec9SNeal Cardwell 				break;
462f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
463f67caec9SNeal Cardwell 					    cond->prefix_len))
464f67caec9SNeal Cardwell 				break;
465f67caec9SNeal Cardwell 			yes = 0;
466f67caec9SNeal Cardwell 			break;
467f67caec9SNeal Cardwell 		}
468a8c2190eSArnaldo Carvalho de Melo 		}
469a8c2190eSArnaldo Carvalho de Melo 
470a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
471a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
472a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
473a8c2190eSArnaldo Carvalho de Melo 		} else {
474a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
475a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
476a8c2190eSArnaldo Carvalho de Melo 		}
477a8c2190eSArnaldo Carvalho de Melo 	}
478a02cec21SEric Dumazet 	return len == 0;
479a8c2190eSArnaldo Carvalho de Melo }
480a8c2190eSArnaldo Carvalho de Melo 
4818d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
4828d07d151SPavel Emelyanov {
4838d07d151SPavel Emelyanov 	struct inet_diag_entry entry;
4848d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
4858d07d151SPavel Emelyanov 
4868d07d151SPavel Emelyanov 	if (bc == NULL)
4878d07d151SPavel Emelyanov 		return 1;
4888d07d151SPavel Emelyanov 
4898d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
490dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4918d07d151SPavel Emelyanov 	if (entry.family == AF_INET6) {
4928d07d151SPavel Emelyanov 		struct ipv6_pinfo *np = inet6_sk(sk);
4938d07d151SPavel Emelyanov 
4948d07d151SPavel Emelyanov 		entry.saddr = np->rcv_saddr.s6_addr32;
4958d07d151SPavel Emelyanov 		entry.daddr = np->daddr.s6_addr32;
4968d07d151SPavel Emelyanov 	} else
4978d07d151SPavel Emelyanov #endif
4988d07d151SPavel Emelyanov 	{
4998d07d151SPavel Emelyanov 		entry.saddr = &inet->inet_rcv_saddr;
5008d07d151SPavel Emelyanov 		entry.daddr = &inet->inet_daddr;
5018d07d151SPavel Emelyanov 	}
5028d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
5038d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
5048d07d151SPavel Emelyanov 	entry.userlocks = sk->sk_userlocks;
5058d07d151SPavel Emelyanov 
5068d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
5078d07d151SPavel Emelyanov }
5088d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
5098d07d151SPavel Emelyanov 
510a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
511a8c2190eSArnaldo Carvalho de Melo {
512a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
513a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
514a8c2190eSArnaldo Carvalho de Melo 
515a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
516a8c2190eSArnaldo Carvalho de Melo 			return 0;
517a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
518a8c2190eSArnaldo Carvalho de Melo 			return 1;
519eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
520a8c2190eSArnaldo Carvalho de Melo 			return 0;
521a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
522a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
523a8c2190eSArnaldo Carvalho de Melo 	}
524a8c2190eSArnaldo Carvalho de Melo 	return 0;
525a8c2190eSArnaldo Carvalho de Melo }
526a8c2190eSArnaldo Carvalho de Melo 
527405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
528405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
529405c0059SNeal Cardwell 			   int *min_len)
530405c0059SNeal Cardwell {
531405c0059SNeal Cardwell 	int addr_len;
532405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
533405c0059SNeal Cardwell 
534405c0059SNeal Cardwell 	/* Check hostcond space. */
535405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
536405c0059SNeal Cardwell 	if (len < *min_len)
537405c0059SNeal Cardwell 		return false;
538405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
539405c0059SNeal Cardwell 
540405c0059SNeal Cardwell 	/* Check address family and address length. */
541405c0059SNeal Cardwell 	switch (cond->family) {
542405c0059SNeal Cardwell 	case AF_UNSPEC:
543405c0059SNeal Cardwell 		addr_len = 0;
544405c0059SNeal Cardwell 		break;
545405c0059SNeal Cardwell 	case AF_INET:
546405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
547405c0059SNeal Cardwell 		break;
548405c0059SNeal Cardwell 	case AF_INET6:
549405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
550405c0059SNeal Cardwell 		break;
551405c0059SNeal Cardwell 	default:
552405c0059SNeal Cardwell 		return false;
553405c0059SNeal Cardwell 	}
554405c0059SNeal Cardwell 	*min_len += addr_len;
555405c0059SNeal Cardwell 	if (len < *min_len)
556405c0059SNeal Cardwell 		return false;
557405c0059SNeal Cardwell 
558405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
559405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
560405c0059SNeal Cardwell 		return false;
561405c0059SNeal Cardwell 
562405c0059SNeal Cardwell 	return true;
563405c0059SNeal Cardwell }
564405c0059SNeal Cardwell 
5655e1f5420SNeal Cardwell /* Validate a port comparison operator. */
5665e1f5420SNeal Cardwell static inline bool valid_port_comparison(const struct inet_diag_bc_op *op,
5675e1f5420SNeal Cardwell 					 int len, int *min_len)
5685e1f5420SNeal Cardwell {
5695e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
5705e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
5715e1f5420SNeal Cardwell 	if (len < *min_len)
5725e1f5420SNeal Cardwell 		return false;
5735e1f5420SNeal Cardwell 	return true;
5745e1f5420SNeal Cardwell }
5755e1f5420SNeal Cardwell 
576a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
577a8c2190eSArnaldo Carvalho de Melo {
578eeb14972SEric Dumazet 	const void *bc = bytecode;
579a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
580a8c2190eSArnaldo Carvalho de Melo 
581a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
582eeb14972SEric Dumazet 		const struct inet_diag_bc_op *op = bc;
583405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
584a8c2190eSArnaldo Carvalho de Melo 
585a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
586a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
587a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
588a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
589405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
590405c0059SNeal Cardwell 				return -EINVAL;
5915e1f5420SNeal Cardwell 			break;
592a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
593a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
594a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
595a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
5965e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
597a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
598a8c2190eSArnaldo Carvalho de Melo 			break;
5995e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
6005e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
601a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
602a8c2190eSArnaldo Carvalho de Melo 			break;
603a8c2190eSArnaldo Carvalho de Melo 		default:
604a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
605a8c2190eSArnaldo Carvalho de Melo 		}
6065e1f5420SNeal Cardwell 
6075e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
6085e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
6095e1f5420SNeal Cardwell 				return -EINVAL;
6105e1f5420SNeal Cardwell 			if (op->no < len &&
6115e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
6125e1f5420SNeal Cardwell 				return -EINVAL;
6135e1f5420SNeal Cardwell 		}
6145e1f5420SNeal Cardwell 
615405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
616eeb14972SEric Dumazet 			return -EINVAL;
617a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
618a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
619a8c2190eSArnaldo Carvalho de Melo 	}
620a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
621a8c2190eSArnaldo Carvalho de Melo }
622a8c2190eSArnaldo Carvalho de Melo 
623dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
624dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
62537f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
626c8991362SPavel Emelyanov 			      struct inet_diag_req_v2 *r,
62737f352b5SPavel Emelyanov 			      const struct nlattr *bc)
628a8c2190eSArnaldo Carvalho de Melo {
6298d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
630a8c2190eSArnaldo Carvalho de Melo 		return 0;
631a8c2190eSArnaldo Carvalho de Melo 
632a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
633e32123e5SPatrick McHardy 				  sk_user_ns(NETLINK_CB(cb->skb).sk),
63415e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
635a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
636a8c2190eSArnaldo Carvalho de Melo }
637a8c2190eSArnaldo Carvalho de Melo 
638*05dbc7b5SEric Dumazet static int inet_twsk_diag_dump(struct sock *sk,
639c7d58aabSArnaldo Carvalho de Melo 			       struct sk_buff *skb,
64037f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
641c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
64237f352b5SPavel Emelyanov 			       const struct nlattr *bc)
643c7d58aabSArnaldo Carvalho de Melo {
644*05dbc7b5SEric Dumazet 	struct inet_timewait_sock *tw = inet_twsk(sk);
645*05dbc7b5SEric Dumazet 
64637f352b5SPavel Emelyanov 	if (bc != NULL) {
647c7d58aabSArnaldo Carvalho de Melo 		struct inet_diag_entry entry;
648c7d58aabSArnaldo Carvalho de Melo 
649c7d58aabSArnaldo Carvalho de Melo 		entry.family = tw->tw_family;
650dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
651c7d58aabSArnaldo Carvalho de Melo 		if (tw->tw_family == AF_INET6) {
652c7d58aabSArnaldo Carvalho de Melo 			struct inet6_timewait_sock *tw6 =
653c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
654c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
655c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = tw6->tw_v6_daddr.s6_addr32;
656c7d58aabSArnaldo Carvalho de Melo 		} else
657c7d58aabSArnaldo Carvalho de Melo #endif
658c7d58aabSArnaldo Carvalho de Melo 		{
659c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = &tw->tw_rcv_saddr;
660c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = &tw->tw_daddr;
661c7d58aabSArnaldo Carvalho de Melo 		}
662c7d58aabSArnaldo Carvalho de Melo 		entry.sport = tw->tw_num;
663c7d58aabSArnaldo Carvalho de Melo 		entry.dport = ntohs(tw->tw_dport);
664c7d58aabSArnaldo Carvalho de Melo 		entry.userlocks = 0;
665c7d58aabSArnaldo Carvalho de Melo 
66687c22ea5SPavel Emelyanov 		if (!inet_diag_bc_run(bc, &entry))
667c7d58aabSArnaldo Carvalho de Melo 			return 0;
668c7d58aabSArnaldo Carvalho de Melo 	}
669c7d58aabSArnaldo Carvalho de Melo 
670a029fe26SPavel Emelyanov 	return inet_twsk_diag_fill(tw, skb, r,
67115e47304SEric W. Biederman 				   NETLINK_CB(cb->skb).portid,
672c7d58aabSArnaldo Carvalho de Melo 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
673c7d58aabSArnaldo Carvalho de Melo }
674c7d58aabSArnaldo Carvalho de Melo 
6751c95df85SNeal Cardwell /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
6761c95df85SNeal Cardwell  * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
6771c95df85SNeal Cardwell  */
6781c95df85SNeal Cardwell static inline void inet_diag_req_addrs(const struct sock *sk,
6791c95df85SNeal Cardwell 				       const struct request_sock *req,
6801c95df85SNeal Cardwell 				       struct inet_diag_entry *entry)
6811c95df85SNeal Cardwell {
6821c95df85SNeal Cardwell 	struct inet_request_sock *ireq = inet_rsk(req);
6831c95df85SNeal Cardwell 
6841c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
6851c95df85SNeal Cardwell 	if (sk->sk_family == AF_INET6) {
6861c95df85SNeal Cardwell 		if (req->rsk_ops->family == AF_INET6) {
6871c95df85SNeal Cardwell 			entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32;
6881c95df85SNeal Cardwell 			entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32;
6891c95df85SNeal Cardwell 		} else if (req->rsk_ops->family == AF_INET) {
6901c95df85SNeal Cardwell 			ipv6_addr_set_v4mapped(ireq->loc_addr,
6911c95df85SNeal Cardwell 					       &entry->saddr_storage);
6921c95df85SNeal Cardwell 			ipv6_addr_set_v4mapped(ireq->rmt_addr,
6931c95df85SNeal Cardwell 					       &entry->daddr_storage);
6941c95df85SNeal Cardwell 			entry->saddr = entry->saddr_storage.s6_addr32;
6951c95df85SNeal Cardwell 			entry->daddr = entry->daddr_storage.s6_addr32;
6961c95df85SNeal Cardwell 		}
6971c95df85SNeal Cardwell 	} else
6981c95df85SNeal Cardwell #endif
6991c95df85SNeal Cardwell 	{
7001c95df85SNeal Cardwell 		entry->saddr = &ireq->loc_addr;
7011c95df85SNeal Cardwell 		entry->daddr = &ireq->rmt_addr;
7021c95df85SNeal Cardwell 	}
7031c95df85SNeal Cardwell }
7041c95df85SNeal Cardwell 
705a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
706d06ca956SEric W. Biederman 			      struct request_sock *req,
707d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
70815e47304SEric W. Biederman 			      u32 portid, u32 seq,
709a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
710a8c2190eSArnaldo Carvalho de Melo {
711a8c2190eSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
712a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
713a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
714a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh;
715a8c2190eSArnaldo Carvalho de Melo 	long tmo;
716a8c2190eSArnaldo Carvalho de Melo 
71715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
7186e277ed5SThomas Graf 			NLM_F_MULTI);
7196e277ed5SThomas Graf 	if (!nlh)
7206e277ed5SThomas Graf 		return -EMSGSIZE;
721a8c2190eSArnaldo Carvalho de Melo 
7226e277ed5SThomas Graf 	r = nlmsg_data(nlh);
723a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
724a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = TCP_SYN_RECV;
725a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 1;
726e6c022a4SEric Dumazet 	r->idiag_retrans = req->num_retrans;
727a8c2190eSArnaldo Carvalho de Melo 
728a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
729f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(req, r->id.idiag_cookie);
730a8c2190eSArnaldo Carvalho de Melo 
731a8c2190eSArnaldo Carvalho de Melo 	tmo = req->expires - jiffies;
732a8c2190eSArnaldo Carvalho de Melo 	if (tmo < 0)
733a8c2190eSArnaldo Carvalho de Melo 		tmo = 0;
734a8c2190eSArnaldo Carvalho de Melo 
735c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
736a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dport = ireq->rmt_port;
737a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_src[0] = ireq->loc_addr;
738a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dst[0] = ireq->rmt_addr;
739a8c2190eSArnaldo Carvalho de Melo 	r->idiag_expires = jiffies_to_msecs(tmo);
740a8c2190eSArnaldo Carvalho de Melo 	r->idiag_rqueue = 0;
741a8c2190eSArnaldo Carvalho de Melo 	r->idiag_wqueue = 0;
742d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
743a8c2190eSArnaldo Carvalho de Melo 	r->idiag_inode = 0;
744dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
745a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
7461c95df85SNeal Cardwell 		struct inet_diag_entry entry;
7471c95df85SNeal Cardwell 		inet_diag_req_addrs(sk, req, &entry);
7481c95df85SNeal Cardwell 		memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
7491c95df85SNeal Cardwell 		memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
750a8c2190eSArnaldo Carvalho de Melo 	}
751a8c2190eSArnaldo Carvalho de Melo #endif
752a8c2190eSArnaldo Carvalho de Melo 
7536e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
754a8c2190eSArnaldo Carvalho de Melo }
755a8c2190eSArnaldo Carvalho de Melo 
756a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
75737f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
758c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
75937f352b5SPavel Emelyanov 			       const struct nlattr *bc)
760a8c2190eSArnaldo Carvalho de Melo {
761a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_entry entry;
762a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
763a8c2190eSArnaldo Carvalho de Melo 	struct listen_sock *lopt;
764a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
765a8c2190eSArnaldo Carvalho de Melo 	int j, s_j;
766a8c2190eSArnaldo Carvalho de Melo 	int reqnum, s_reqnum;
767a8c2190eSArnaldo Carvalho de Melo 	int err = 0;
768a8c2190eSArnaldo Carvalho de Melo 
769a8c2190eSArnaldo Carvalho de Melo 	s_j = cb->args[3];
770a8c2190eSArnaldo Carvalho de Melo 	s_reqnum = cb->args[4];
771a8c2190eSArnaldo Carvalho de Melo 
772a8c2190eSArnaldo Carvalho de Melo 	if (s_j > 0)
773a8c2190eSArnaldo Carvalho de Melo 		s_j--;
774a8c2190eSArnaldo Carvalho de Melo 
775a8c2190eSArnaldo Carvalho de Melo 	entry.family = sk->sk_family;
776a8c2190eSArnaldo Carvalho de Melo 
777a8c2190eSArnaldo Carvalho de Melo 	read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
778a8c2190eSArnaldo Carvalho de Melo 
779a8c2190eSArnaldo Carvalho de Melo 	lopt = icsk->icsk_accept_queue.listen_opt;
780a8c2190eSArnaldo Carvalho de Melo 	if (!lopt || !lopt->qlen)
781a8c2190eSArnaldo Carvalho de Melo 		goto out;
782a8c2190eSArnaldo Carvalho de Melo 
78337f352b5SPavel Emelyanov 	if (bc != NULL) {
784c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
785a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
786a8c2190eSArnaldo Carvalho de Melo 	}
787a8c2190eSArnaldo Carvalho de Melo 
788a8c2190eSArnaldo Carvalho de Melo 	for (j = s_j; j < lopt->nr_table_entries; j++) {
789a8c2190eSArnaldo Carvalho de Melo 		struct request_sock *req, *head = lopt->syn_table[j];
790a8c2190eSArnaldo Carvalho de Melo 
791a8c2190eSArnaldo Carvalho de Melo 		reqnum = 0;
792a8c2190eSArnaldo Carvalho de Melo 		for (req = head; req; reqnum++, req = req->dl_next) {
793a8c2190eSArnaldo Carvalho de Melo 			struct inet_request_sock *ireq = inet_rsk(req);
794a8c2190eSArnaldo Carvalho de Melo 
795a8c2190eSArnaldo Carvalho de Melo 			if (reqnum < s_reqnum)
796a8c2190eSArnaldo Carvalho de Melo 				continue;
797a8c2190eSArnaldo Carvalho de Melo 			if (r->id.idiag_dport != ireq->rmt_port &&
798a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_dport)
799a8c2190eSArnaldo Carvalho de Melo 				continue;
800a8c2190eSArnaldo Carvalho de Melo 
801a8c2190eSArnaldo Carvalho de Melo 			if (bc) {
8021c95df85SNeal Cardwell 				inet_diag_req_addrs(sk, req, &entry);
803a8c2190eSArnaldo Carvalho de Melo 				entry.dport = ntohs(ireq->rmt_port);
804a8c2190eSArnaldo Carvalho de Melo 
80587c22ea5SPavel Emelyanov 				if (!inet_diag_bc_run(bc, &entry))
806a8c2190eSArnaldo Carvalho de Melo 					continue;
807a8c2190eSArnaldo Carvalho de Melo 			}
808a8c2190eSArnaldo Carvalho de Melo 
809a8c2190eSArnaldo Carvalho de Melo 			err = inet_diag_fill_req(skb, sk, req,
810e32123e5SPatrick McHardy 					       sk_user_ns(NETLINK_CB(cb->skb).sk),
81115e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
812a8c2190eSArnaldo Carvalho de Melo 					       cb->nlh->nlmsg_seq, cb->nlh);
813a8c2190eSArnaldo Carvalho de Melo 			if (err < 0) {
814a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = j + 1;
815a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = reqnum;
816a8c2190eSArnaldo Carvalho de Melo 				goto out;
817a8c2190eSArnaldo Carvalho de Melo 			}
818a8c2190eSArnaldo Carvalho de Melo 		}
819a8c2190eSArnaldo Carvalho de Melo 
820a8c2190eSArnaldo Carvalho de Melo 		s_reqnum = 0;
821a8c2190eSArnaldo Carvalho de Melo 	}
822a8c2190eSArnaldo Carvalho de Melo 
823a8c2190eSArnaldo Carvalho de Melo out:
824a8c2190eSArnaldo Carvalho de Melo 	read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
825a8c2190eSArnaldo Carvalho de Melo 
826a8c2190eSArnaldo Carvalho de Melo 	return err;
827a8c2190eSArnaldo Carvalho de Melo }
828a8c2190eSArnaldo Carvalho de Melo 
8291942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
830c8991362SPavel Emelyanov 		struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
831a8c2190eSArnaldo Carvalho de Melo {
832a8c2190eSArnaldo Carvalho de Melo 	int i, num;
833a8c2190eSArnaldo Carvalho de Melo 	int s_i, s_num;
83451d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
835a8c2190eSArnaldo Carvalho de Melo 
836a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
837a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
838a8c2190eSArnaldo Carvalho de Melo 
839a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
840a8c2190eSArnaldo Carvalho de Melo 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
841a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
842a8c2190eSArnaldo Carvalho de Melo 
843a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
844a8c2190eSArnaldo Carvalho de Melo 			struct sock *sk;
845c25eb3bfSEric Dumazet 			struct hlist_nulls_node *node;
8465caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
847a8c2190eSArnaldo Carvalho de Melo 
848a8c2190eSArnaldo Carvalho de Melo 			num = 0;
8495caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
8505caea4eaSEric Dumazet 			spin_lock_bh(&ilb->lock);
851c25eb3bfSEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->head) {
852a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
853a8c2190eSArnaldo Carvalho de Melo 
85451d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
85551d7cccfSAndrey Vagin 					continue;
85651d7cccfSAndrey Vagin 
857a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
858a8c2190eSArnaldo Carvalho de Melo 					num++;
859a8c2190eSArnaldo Carvalho de Melo 					continue;
860a8c2190eSArnaldo Carvalho de Melo 				}
861a8c2190eSArnaldo Carvalho de Melo 
862d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
863d23deaa0SPavel Emelyanov 						sk->sk_family != r->sdiag_family)
864d23deaa0SPavel Emelyanov 					goto next_listen;
865d23deaa0SPavel Emelyanov 
866c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
867a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
868a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
869a8c2190eSArnaldo Carvalho de Melo 
870a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_LISTEN) ||
871a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport ||
872a8c2190eSArnaldo Carvalho de Melo 				    cb->args[3] > 0)
873a8c2190eSArnaldo Carvalho de Melo 					goto syn_recv;
874a8c2190eSArnaldo Carvalho de Melo 
87525c4cd2bSPavel Emelyanov 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
8765caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
877a8c2190eSArnaldo Carvalho de Melo 					goto done;
878a8c2190eSArnaldo Carvalho de Melo 				}
879a8c2190eSArnaldo Carvalho de Melo 
880a8c2190eSArnaldo Carvalho de Melo syn_recv:
881a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_SYN_RECV))
882a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
883a8c2190eSArnaldo Carvalho de Melo 
88425c4cd2bSPavel Emelyanov 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
8855caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
886a8c2190eSArnaldo Carvalho de Melo 					goto done;
887a8c2190eSArnaldo Carvalho de Melo 				}
888a8c2190eSArnaldo Carvalho de Melo 
889a8c2190eSArnaldo Carvalho de Melo next_listen:
890a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = 0;
891a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = 0;
892a8c2190eSArnaldo Carvalho de Melo 				++num;
893a8c2190eSArnaldo Carvalho de Melo 			}
8945caea4eaSEric Dumazet 			spin_unlock_bh(&ilb->lock);
895a8c2190eSArnaldo Carvalho de Melo 
896a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
897a8c2190eSArnaldo Carvalho de Melo 			cb->args[3] = 0;
898a8c2190eSArnaldo Carvalho de Melo 			cb->args[4] = 0;
899a8c2190eSArnaldo Carvalho de Melo 		}
900a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
901a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
902a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
903a8c2190eSArnaldo Carvalho de Melo 	}
904a8c2190eSArnaldo Carvalho de Melo 
905a8c2190eSArnaldo Carvalho de Melo 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
906efb3cb42SPavel Emelyanov 		goto out;
907a8c2190eSArnaldo Carvalho de Melo 
908f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
909a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
9107e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
911a8c2190eSArnaldo Carvalho de Melo 		struct sock *sk;
9123ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
913a8c2190eSArnaldo Carvalho de Melo 
9146be547a6SAndi Kleen 		num = 0;
9156be547a6SAndi Kleen 
916*05dbc7b5SEric Dumazet 		if (hlist_nulls_empty(&head->chain))
9176be547a6SAndi Kleen 			continue;
9186be547a6SAndi Kleen 
919a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
920a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
921a8c2190eSArnaldo Carvalho de Melo 
9227e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
9233ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
924*05dbc7b5SEric Dumazet 			int res;
925a8c2190eSArnaldo Carvalho de Melo 
92651d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
92751d7cccfSAndrey Vagin 				continue;
928a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
929a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
930a8c2190eSArnaldo Carvalho de Melo 			if (!(r->idiag_states & (1 << sk->sk_state)))
931a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
932d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
933d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
934d23deaa0SPavel Emelyanov 				goto next_normal;
935*05dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
936a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
937a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
938*05dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
9394e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
940a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
941*05dbc7b5SEric Dumazet 			if (sk->sk_state == TCP_TIME_WAIT)
942*05dbc7b5SEric Dumazet 				res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
943*05dbc7b5SEric Dumazet 			else
944*05dbc7b5SEric Dumazet 				res = inet_csk_diag_dump(sk, skb, cb, r, bc);
945*05dbc7b5SEric Dumazet 			if (res < 0) {
9467e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
947a8c2190eSArnaldo Carvalho de Melo 				goto done;
948a8c2190eSArnaldo Carvalho de Melo 			}
949a8c2190eSArnaldo Carvalho de Melo next_normal:
950a8c2190eSArnaldo Carvalho de Melo 			++num;
951a8c2190eSArnaldo Carvalho de Melo 		}
952a8c2190eSArnaldo Carvalho de Melo 
9537e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
954a8c2190eSArnaldo Carvalho de Melo 	}
955a8c2190eSArnaldo Carvalho de Melo 
956a8c2190eSArnaldo Carvalho de Melo done:
957a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
958a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
959efb3cb42SPavel Emelyanov out:
960efb3cb42SPavel Emelyanov 	;
961efb3cb42SPavel Emelyanov }
9621942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
963efb3cb42SPavel Emelyanov 
964efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
965c8991362SPavel Emelyanov 		struct inet_diag_req_v2 *r, struct nlattr *bc)
966efb3cb42SPavel Emelyanov {
967efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
968cacb6ba0SCyrill Gorcunov 	int err = 0;
969efb3cb42SPavel Emelyanov 
970efb3cb42SPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
971efb3cb42SPavel Emelyanov 	if (!IS_ERR(handler))
9721942c518SPavel Emelyanov 		handler->dump(skb, cb, r, bc);
973cacb6ba0SCyrill Gorcunov 	else
974cacb6ba0SCyrill Gorcunov 		err = PTR_ERR(handler);
975d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
976efb3cb42SPavel Emelyanov 
977cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
978a8c2190eSArnaldo Carvalho de Melo }
979a8c2190eSArnaldo Carvalho de Melo 
98025c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
98125c4cd2bSPavel Emelyanov {
98225c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
983c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
98425c4cd2bSPavel Emelyanov 
98525c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
98625c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
98725c4cd2bSPavel Emelyanov 
988d106352dSDavid S. Miller 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
98925c4cd2bSPavel Emelyanov }
99025c4cd2bSPavel Emelyanov 
991a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type)
992a029fe26SPavel Emelyanov {
993a029fe26SPavel Emelyanov 	switch (type) {
994a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
995a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
996a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
997a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
998a029fe26SPavel Emelyanov 	default:
999a029fe26SPavel Emelyanov 		return 0;
1000a029fe26SPavel Emelyanov 	}
1001a029fe26SPavel Emelyanov }
1002a029fe26SPavel Emelyanov 
100325c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
100425c4cd2bSPavel Emelyanov {
1005d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1006c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
100725c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
10083b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
100925c4cd2bSPavel Emelyanov 
1010d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
101125c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
101225c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
101325c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
101425c4cd2bSPavel Emelyanov 	req.id = rc->id;
101525c4cd2bSPavel Emelyanov 
101625c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
101725c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
101825c4cd2bSPavel Emelyanov 
101925c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
102025c4cd2bSPavel Emelyanov }
102125c4cd2bSPavel Emelyanov 
1022fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1023fe50ce28SPavel Emelyanov 			       const struct nlmsghdr *nlh)
1024fe50ce28SPavel Emelyanov {
1025d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1026c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1027fe50ce28SPavel Emelyanov 
1028fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1029fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1030fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1031fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1032fe50ce28SPavel Emelyanov 	req.id = rc->id;
1033fe50ce28SPavel Emelyanov 
1034fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
1035fe50ce28SPavel Emelyanov }
1036fe50ce28SPavel Emelyanov 
10378d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1038a8c2190eSArnaldo Carvalho de Melo {
10393b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
104051d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1041a8c2190eSArnaldo Carvalho de Melo 
1042ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1043ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1044ead592baSThomas Graf 		return -EINVAL;
1045a8c2190eSArnaldo Carvalho de Melo 
1046b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1047ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1048ead592baSThomas Graf 			struct nlattr *attr;
1049ead592baSThomas Graf 
1050ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1051ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1052ead592baSThomas Graf 			if (attr == NULL ||
1053ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1054ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1055a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
1056a8c2190eSArnaldo Carvalho de Melo 		}
105780d326faSPablo Neira Ayuso 		{
105880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
105980d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
106080d326faSPablo Neira Ayuso 			};
106151d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
106280d326faSPablo Neira Ayuso 		}
1063a8c2190eSArnaldo Carvalho de Melo 	}
1064ead592baSThomas Graf 
1065fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1066a8c2190eSArnaldo Carvalho de Melo }
1067a8c2190eSArnaldo Carvalho de Melo 
1068d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1069d366477aSPavel Emelyanov {
1070c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
107151d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1072d366477aSPavel Emelyanov 
1073d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1074d366477aSPavel Emelyanov 		return -EINVAL;
1075d366477aSPavel Emelyanov 
1076d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
107725c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
107825c4cd2bSPavel Emelyanov 			struct nlattr *attr;
107925c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
108025c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
108125c4cd2bSPavel Emelyanov 			if (attr == NULL ||
108225c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
108325c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
108425c4cd2bSPavel Emelyanov 				return -EINVAL;
108525c4cd2bSPavel Emelyanov 		}
108680d326faSPablo Neira Ayuso 		{
108780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
108880d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
108980d326faSPablo Neira Ayuso 			};
109051d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
109180d326faSPablo Neira Ayuso 		}
1092d366477aSPavel Emelyanov 	}
1093d366477aSPavel Emelyanov 
1094d106352dSDavid S. Miller 	return inet_diag_get_exact(skb, h, nlmsg_data(h));
1095d366477aSPavel Emelyanov }
1096d366477aSPavel Emelyanov 
10978dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1098d366477aSPavel Emelyanov 	.family = AF_INET,
1099d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1100d366477aSPavel Emelyanov };
1101d366477aSPavel Emelyanov 
11028dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1103d366477aSPavel Emelyanov 	.family = AF_INET6,
1104d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1105d366477aSPavel Emelyanov };
1106d366477aSPavel Emelyanov 
1107a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1108a8c2190eSArnaldo Carvalho de Melo {
1109a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1110a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1111a8c2190eSArnaldo Carvalho de Melo 
1112f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1113a8c2190eSArnaldo Carvalho de Melo 		goto out;
1114a8c2190eSArnaldo Carvalho de Melo 
1115d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1116a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1117a8c2190eSArnaldo Carvalho de Melo 	if (inet_diag_table[type] == NULL) {
1118a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1119a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1120a8c2190eSArnaldo Carvalho de Melo 	}
1121d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1122a8c2190eSArnaldo Carvalho de Melo out:
1123a8c2190eSArnaldo Carvalho de Melo 	return err;
1124a8c2190eSArnaldo Carvalho de Melo }
1125a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1126a8c2190eSArnaldo Carvalho de Melo 
1127a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1128a8c2190eSArnaldo Carvalho de Melo {
1129a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1130a8c2190eSArnaldo Carvalho de Melo 
1131f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1132a8c2190eSArnaldo Carvalho de Melo 		return;
1133a8c2190eSArnaldo Carvalho de Melo 
1134d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1135a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1136d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1137a8c2190eSArnaldo Carvalho de Melo }
1138a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1139a8c2190eSArnaldo Carvalho de Melo 
1140a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1141a8c2190eSArnaldo Carvalho de Melo {
1142f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1143a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1144a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1145a8c2190eSArnaldo Carvalho de Melo 
11460da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1147a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1148a8c2190eSArnaldo Carvalho de Melo 		goto out;
1149a8c2190eSArnaldo Carvalho de Melo 
1150d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1151d366477aSPavel Emelyanov 	if (err)
1152d366477aSPavel Emelyanov 		goto out_free_nl;
1153d366477aSPavel Emelyanov 
1154d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1155d366477aSPavel Emelyanov 	if (err)
1156d366477aSPavel Emelyanov 		goto out_free_inet;
1157d366477aSPavel Emelyanov 
11588ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1159a8c2190eSArnaldo Carvalho de Melo out:
1160a8c2190eSArnaldo Carvalho de Melo 	return err;
1161d366477aSPavel Emelyanov 
1162d366477aSPavel Emelyanov out_free_inet:
1163d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1164d366477aSPavel Emelyanov out_free_nl:
1165a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1166a8c2190eSArnaldo Carvalho de Melo 	goto out;
1167a8c2190eSArnaldo Carvalho de Melo }
1168a8c2190eSArnaldo Carvalho de Melo 
1169a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1170a8c2190eSArnaldo Carvalho de Melo {
1171d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1172d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
11738ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1174a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1175a8c2190eSArnaldo Carvalho de Melo }
1176a8c2190eSArnaldo Carvalho de Melo 
1177a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1178a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1179a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1180aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1181aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1182