xref: /openbmc/linux/net/ipv4/inet_diag.c (revision 496127290f298d839918a14728b653b43ef02708)
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;
109b1aac815SDaniel Borkmann 
110b1aac815SDaniel Borkmann 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
111b1aac815SDaniel Borkmann 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
112b1aac815SDaniel Borkmann 
113c720c7e8SEric Dumazet 	r->id.idiag_src[0] = inet->inet_rcv_saddr;
114c720c7e8SEric Dumazet 	r->id.idiag_dst[0] = inet->inet_daddr;
115a8c2190eSArnaldo Carvalho de Melo 
116e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
117e4e541a8SPavel Emelyanov 		goto errout;
118e4e541a8SPavel Emelyanov 
119717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
120717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
121717b6d83SMaciej Żenczykowski 	 */
122717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1236e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1246e277ed5SThomas Graf 			goto errout;
125717b6d83SMaciej Żenczykowski 
126dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
127a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
128a8c2190eSArnaldo Carvalho de Melo 
129efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
130efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
1316e277ed5SThomas Graf 
13206236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
133efe4208fSEric Dumazet 			if (nla_put_u8(skb, INET_DIAG_TCLASS,
134efe4208fSEric Dumazet 				       inet6_sk(sk)->tclass) < 0)
1356e277ed5SThomas Graf 				goto errout;
136a8c2190eSArnaldo Carvalho de Melo 	}
137a8c2190eSArnaldo Carvalho de Melo #endif
138a8c2190eSArnaldo Carvalho de Melo 
139d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1403c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1413c4d05c8SPavel Emelyanov 
1426e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
1436e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
1446e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
1456e277ed5SThomas Graf 			.idiag_wmem = sk->sk_wmem_queued,
1466e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
1476e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
1486e277ed5SThomas Graf 		};
1496e277ed5SThomas Graf 
1506e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
1516e277ed5SThomas Graf 			goto errout;
1523c4d05c8SPavel Emelyanov 	}
1533c4d05c8SPavel Emelyanov 
154c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
155c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
1566e277ed5SThomas Graf 			goto errout;
157c0636faaSPavel Emelyanov 
1583c4d05c8SPavel Emelyanov 	if (icsk == NULL) {
15962ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
1603c4d05c8SPavel Emelyanov 		goto out;
1613c4d05c8SPavel Emelyanov 	}
1623c4d05c8SPavel Emelyanov 
163172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
164a8c2190eSArnaldo Carvalho de Melo 
1656ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
1666ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
1676ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
168a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
169a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
170a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
171a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
172a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
173a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
174a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
175a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
176a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
177a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
178a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
179a8c2190eSArnaldo Carvalho de Melo 	} else {
180a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
181a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
182a8c2190eSArnaldo Carvalho de Melo 	}
183a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
184a8c2190eSArnaldo Carvalho de Melo 
1856e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_INFO - 1))) {
1866e277ed5SThomas Graf 		attr = nla_reserve(skb, INET_DIAG_INFO,
1876e277ed5SThomas Graf 				   sizeof(struct tcp_info));
1886e277ed5SThomas Graf 		if (!attr)
1896e277ed5SThomas Graf 			goto errout;
190a8c2190eSArnaldo Carvalho de Melo 
1916e277ed5SThomas Graf 		info = nla_data(attr);
192a8c2190eSArnaldo Carvalho de Melo 	}
193a8c2190eSArnaldo Carvalho de Melo 
1946e277ed5SThomas Graf 	if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
1956e277ed5SThomas Graf 		if (nla_put_string(skb, INET_DIAG_CONG,
1966e277ed5SThomas Graf 				   icsk->icsk_ca_ops->name) < 0)
1976e277ed5SThomas Graf 			goto errout;
1986e277ed5SThomas Graf 
199a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
200a8c2190eSArnaldo Carvalho de Melo 
201a8c2190eSArnaldo Carvalho de Melo 	if (sk->sk_state < TCP_TIME_WAIT &&
202a8c2190eSArnaldo Carvalho de Melo 	    icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
203a8c2190eSArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->get_info(sk, ext, skb);
204a8c2190eSArnaldo Carvalho de Melo 
2053c4d05c8SPavel Emelyanov out:
206053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
207053c095aSJohannes Berg 	return 0;
208a8c2190eSArnaldo Carvalho de Melo 
2096e277ed5SThomas Graf errout:
2106e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
21126932566SPatrick McHardy 	return -EMSGSIZE;
212a8c2190eSArnaldo Carvalho de Melo }
2133c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
2143c4d05c8SPavel Emelyanov 
2153c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
216c8991362SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req_v2 *req,
217d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
21815e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
2193c4d05c8SPavel Emelyanov 			      const struct nlmsghdr *unlh)
2203c4d05c8SPavel Emelyanov {
2213c4d05c8SPavel Emelyanov 	return inet_sk_diag_fill(sk, inet_csk(sk),
22215e47304SEric W. Biederman 			skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
2233c4d05c8SPavel Emelyanov }
224a8c2190eSArnaldo Carvalho de Melo 
225c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
226c8991362SPavel Emelyanov 			       struct sk_buff *skb, struct inet_diag_req_v2 *req,
22715e47304SEric W. Biederman 			       u32 portid, u32 seq, u16 nlmsg_flags,
228c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
229c7d58aabSArnaldo Carvalho de Melo {
23096f817feSEric Dumazet 	s32 tmo;
231c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
2326e277ed5SThomas Graf 	struct nlmsghdr *nlh;
233c7d58aabSArnaldo Carvalho de Melo 
23415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
2356e277ed5SThomas Graf 			nlmsg_flags);
2366e277ed5SThomas Graf 	if (!nlh)
237d106352dSDavid S. Miller 		return -EMSGSIZE;
238d106352dSDavid S. Miller 
239d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
240c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
241c7d58aabSArnaldo Carvalho de Melo 
24296f817feSEric Dumazet 	tmo = tw->tw_ttd - inet_tw_time_stamp();
243c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
244c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
245c7d58aabSArnaldo Carvalho de Melo 
246c7d58aabSArnaldo Carvalho de Melo 	r->idiag_family	      = tw->tw_family;
247c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
248b1aac815SDaniel Borkmann 
249c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_if	      = tw->tw_bound_dev_if;
250f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(tw, r->id.idiag_cookie);
251b1aac815SDaniel Borkmann 
252c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_sport     = tw->tw_sport;
253c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dport     = tw->tw_dport;
254b1aac815SDaniel Borkmann 
255b1aac815SDaniel Borkmann 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
256b1aac815SDaniel Borkmann 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
257b1aac815SDaniel Borkmann 
258c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_src[0]    = tw->tw_rcv_saddr;
259c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dst[0]    = tw->tw_daddr;
260b1aac815SDaniel Borkmann 
261c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
262c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
26396f817feSEric Dumazet 	r->idiag_expires      = jiffies_to_msecs(tmo);
264c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
265c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
266c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
267c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
268dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
269c7d58aabSArnaldo Carvalho de Melo 	if (tw->tw_family == AF_INET6) {
270efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr;
271efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr;
272c7d58aabSArnaldo Carvalho de Melo 	}
273c7d58aabSArnaldo Carvalho de Melo #endif
2746e277ed5SThomas Graf 
275053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
276053c095aSJohannes Berg 	return 0;
277c7d58aabSArnaldo Carvalho de Melo }
278c7d58aabSArnaldo Carvalho de Melo 
279dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
280d06ca956SEric W. Biederman 			struct inet_diag_req_v2 *r,
281d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
28215e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
283dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
284dff2c035SArnaldo Carvalho de Melo {
285dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
286efe4208fSEric Dumazet 		return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq,
287efe4208fSEric Dumazet 					   nlmsg_flags, unlh);
288efe4208fSEric Dumazet 
289efe4208fSEric Dumazet 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
290efe4208fSEric Dumazet 				  nlmsg_flags, unlh);
291dff2c035SArnaldo Carvalho de Melo }
292dff2c035SArnaldo Carvalho de Melo 
2931942c518SPavel Emelyanov int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
294c8991362SPavel Emelyanov 		const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
295a8c2190eSArnaldo Carvalho de Melo {
296a8c2190eSArnaldo Carvalho de Melo 	int err;
297a8c2190eSArnaldo Carvalho de Melo 	struct sock *sk;
298a8c2190eSArnaldo Carvalho de Melo 	struct sk_buff *rep;
29951d7cccfSAndrey Vagin 	struct net *net = sock_net(in_skb->sk);
300a8c2190eSArnaldo Carvalho de Melo 
301d523a328SHerbert Xu 	err = -EINVAL;
302fe50ce28SPavel Emelyanov 	if (req->sdiag_family == AF_INET) {
30351d7cccfSAndrey Vagin 		sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
304a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
305a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
306a8c2190eSArnaldo Carvalho de Melo 	}
307dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
308fe50ce28SPavel Emelyanov 	else if (req->sdiag_family == AF_INET6) {
30951d7cccfSAndrey Vagin 		sk = inet6_lookup(net, hashinfo,
310a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
311a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
312a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
313a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
314a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
315a8c2190eSArnaldo Carvalho de Melo 	}
316a8c2190eSArnaldo Carvalho de Melo #endif
317a8c2190eSArnaldo Carvalho de Melo 	else {
318476f7dbfSPavel Emelyanov 		goto out_nosk;
319a8c2190eSArnaldo Carvalho de Melo 	}
320a8c2190eSArnaldo Carvalho de Melo 
321d523a328SHerbert Xu 	err = -ENOENT;
322a8c2190eSArnaldo Carvalho de Melo 	if (sk == NULL)
323476f7dbfSPavel Emelyanov 		goto out_nosk;
324a8c2190eSArnaldo Carvalho de Melo 
325f65c1b53SPavel Emelyanov 	err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
326b005ab4eSPavel Emelyanov 	if (err)
327a8c2190eSArnaldo Carvalho de Melo 		goto out;
328a8c2190eSArnaldo Carvalho de Melo 
3296e277ed5SThomas Graf 	rep = nlmsg_new(sizeof(struct inet_diag_msg) +
330a8c2190eSArnaldo Carvalho de Melo 			sizeof(struct inet_diag_meminfo) +
3316e277ed5SThomas Graf 			sizeof(struct tcp_info) + 64, GFP_KERNEL);
3326e277ed5SThomas Graf 	if (!rep) {
3336e277ed5SThomas Graf 		err = -ENOMEM;
334a8c2190eSArnaldo Carvalho de Melo 		goto out;
3356e277ed5SThomas Graf 	}
336a8c2190eSArnaldo Carvalho de Melo 
337a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
338e32123e5SPatrick McHardy 			   sk_user_ns(NETLINK_CB(in_skb).sk),
33915e47304SEric W. Biederman 			   NETLINK_CB(in_skb).portid,
34026932566SPatrick McHardy 			   nlh->nlmsg_seq, 0, nlh);
34126932566SPatrick McHardy 	if (err < 0) {
34226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
3436e277ed5SThomas Graf 		nlmsg_free(rep);
34426932566SPatrick McHardy 		goto out;
34526932566SPatrick McHardy 	}
34615e47304SEric W. Biederman 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
347a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
348a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
349a8c2190eSArnaldo Carvalho de Melo 		err = 0;
350a8c2190eSArnaldo Carvalho de Melo 
351a8c2190eSArnaldo Carvalho de Melo out:
352c1d607ccSEric Dumazet 	if (sk)
353c1d607ccSEric Dumazet 		sock_gen_put(sk);
354c1d607ccSEric Dumazet 
355476f7dbfSPavel Emelyanov out_nosk:
356476f7dbfSPavel Emelyanov 	return err;
357476f7dbfSPavel Emelyanov }
3581942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
359476f7dbfSPavel Emelyanov 
360476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb,
361476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
362c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *req)
363476f7dbfSPavel Emelyanov {
364476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
365476f7dbfSPavel Emelyanov 	int err;
366476f7dbfSPavel Emelyanov 
367476f7dbfSPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
368476f7dbfSPavel Emelyanov 	if (IS_ERR(handler))
369476f7dbfSPavel Emelyanov 		err = PTR_ERR(handler);
370476f7dbfSPavel Emelyanov 	else
3711942c518SPavel Emelyanov 		err = handler->dump_one(in_skb, nlh, req);
372d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
373476f7dbfSPavel Emelyanov 
374a8c2190eSArnaldo Carvalho de Melo 	return err;
375a8c2190eSArnaldo Carvalho de Melo }
376a8c2190eSArnaldo Carvalho de Melo 
3779f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
378a8c2190eSArnaldo Carvalho de Melo {
379a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
380a8c2190eSArnaldo Carvalho de Melo 
381a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
382a8c2190eSArnaldo Carvalho de Melo 
383a8c2190eSArnaldo Carvalho de Melo 	if (words) {
384a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
385a8c2190eSArnaldo Carvalho de Melo 			return 0;
386a8c2190eSArnaldo Carvalho de Melo 	}
387a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
3889f855299SAl Viro 		__be32 w1, w2;
3899f855299SAl Viro 		__be32 mask;
390a8c2190eSArnaldo Carvalho de Melo 
391a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
392a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
393a8c2190eSArnaldo Carvalho de Melo 
394a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
395a8c2190eSArnaldo Carvalho de Melo 
396a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
397a8c2190eSArnaldo Carvalho de Melo 			return 0;
398a8c2190eSArnaldo Carvalho de Melo 	}
399a8c2190eSArnaldo Carvalho de Melo 
400a8c2190eSArnaldo Carvalho de Melo 	return 1;
401a8c2190eSArnaldo Carvalho de Melo }
402a8c2190eSArnaldo Carvalho de Melo 
403a8c2190eSArnaldo Carvalho de Melo 
40487c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
405a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_entry *entry)
406a8c2190eSArnaldo Carvalho de Melo {
40787c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
40887c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
40987c22ea5SPavel Emelyanov 
410a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
411a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
412a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
413a8c2190eSArnaldo Carvalho de Melo 
414a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
415a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
416a8c2190eSArnaldo Carvalho de Melo 			break;
417a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
418a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
419a8c2190eSArnaldo Carvalho de Melo 			break;
420a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
421a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
422a8c2190eSArnaldo Carvalho de Melo 			break;
423a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
424b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
425a8c2190eSArnaldo Carvalho de Melo 			break;
426a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
427a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
428a8c2190eSArnaldo Carvalho de Melo 			break;
429a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
430a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
431a8c2190eSArnaldo Carvalho de Melo 			break;
432a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
433a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
434a8c2190eSArnaldo Carvalho de Melo 			break;
435a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
436a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
437a8c2190eSArnaldo Carvalho de Melo 			struct inet_diag_hostcond *cond;
4389f855299SAl Viro 			__be32 *addr;
439a8c2190eSArnaldo Carvalho de Melo 
440a8c2190eSArnaldo Carvalho de Melo 			cond = (struct inet_diag_hostcond *)(op + 1);
441a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
442a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
443a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
444a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
445a8c2190eSArnaldo Carvalho de Melo 				break;
446a8c2190eSArnaldo Carvalho de Melo 			}
447a8c2190eSArnaldo Carvalho de Melo 
448a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
449a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
450a8c2190eSArnaldo Carvalho de Melo 			else
451a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
452a8c2190eSArnaldo Carvalho de Melo 
453f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
454f67caec9SNeal Cardwell 			    cond->family != entry->family) {
455a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
456a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
457a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
458a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
459f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
460f67caec9SNeal Cardwell 							    cond->addr,
461a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
462a8c2190eSArnaldo Carvalho de Melo 						break;
463a8c2190eSArnaldo Carvalho de Melo 				}
464a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
465a8c2190eSArnaldo Carvalho de Melo 				break;
466a8c2190eSArnaldo Carvalho de Melo 			}
467f67caec9SNeal Cardwell 
468f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
469f67caec9SNeal Cardwell 				break;
470f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
471f67caec9SNeal Cardwell 					    cond->prefix_len))
472f67caec9SNeal Cardwell 				break;
473f67caec9SNeal Cardwell 			yes = 0;
474f67caec9SNeal Cardwell 			break;
475f67caec9SNeal Cardwell 		}
476a8c2190eSArnaldo Carvalho de Melo 		}
477a8c2190eSArnaldo Carvalho de Melo 
478a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
479a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
480a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
481a8c2190eSArnaldo Carvalho de Melo 		} else {
482a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
483a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
484a8c2190eSArnaldo Carvalho de Melo 		}
485a8c2190eSArnaldo Carvalho de Melo 	}
486a02cec21SEric Dumazet 	return len == 0;
487a8c2190eSArnaldo Carvalho de Melo }
488a8c2190eSArnaldo Carvalho de Melo 
4898d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
4908d07d151SPavel Emelyanov {
4918d07d151SPavel Emelyanov 	struct inet_diag_entry entry;
4928d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
4938d07d151SPavel Emelyanov 
4948d07d151SPavel Emelyanov 	if (bc == NULL)
4958d07d151SPavel Emelyanov 		return 1;
4968d07d151SPavel Emelyanov 
4978d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
498dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4998d07d151SPavel Emelyanov 	if (entry.family == AF_INET6) {
5008d07d151SPavel Emelyanov 
501efe4208fSEric Dumazet 		entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
502efe4208fSEric Dumazet 		entry.daddr = sk->sk_v6_daddr.s6_addr32;
5038d07d151SPavel Emelyanov 	} else
5048d07d151SPavel Emelyanov #endif
5058d07d151SPavel Emelyanov 	{
5068d07d151SPavel Emelyanov 		entry.saddr = &inet->inet_rcv_saddr;
5078d07d151SPavel Emelyanov 		entry.daddr = &inet->inet_daddr;
5088d07d151SPavel Emelyanov 	}
5098d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
5108d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
511*49612729SEric Dumazet 	entry.userlocks = (sk->sk_state != TCP_TIME_WAIT) ? sk->sk_userlocks : 0;
5128d07d151SPavel Emelyanov 
5138d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
5148d07d151SPavel Emelyanov }
5158d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
5168d07d151SPavel Emelyanov 
517a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
518a8c2190eSArnaldo Carvalho de Melo {
519a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
520a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
521a8c2190eSArnaldo Carvalho de Melo 
522a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
523a8c2190eSArnaldo Carvalho de Melo 			return 0;
524a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
525a8c2190eSArnaldo Carvalho de Melo 			return 1;
526eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
527a8c2190eSArnaldo Carvalho de Melo 			return 0;
528a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
529a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
530a8c2190eSArnaldo Carvalho de Melo 	}
531a8c2190eSArnaldo Carvalho de Melo 	return 0;
532a8c2190eSArnaldo Carvalho de Melo }
533a8c2190eSArnaldo Carvalho de Melo 
534405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
535405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
536405c0059SNeal Cardwell 			   int *min_len)
537405c0059SNeal Cardwell {
538405c0059SNeal Cardwell 	int addr_len;
539405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
540405c0059SNeal Cardwell 
541405c0059SNeal Cardwell 	/* Check hostcond space. */
542405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
543405c0059SNeal Cardwell 	if (len < *min_len)
544405c0059SNeal Cardwell 		return false;
545405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
546405c0059SNeal Cardwell 
547405c0059SNeal Cardwell 	/* Check address family and address length. */
548405c0059SNeal Cardwell 	switch (cond->family) {
549405c0059SNeal Cardwell 	case AF_UNSPEC:
550405c0059SNeal Cardwell 		addr_len = 0;
551405c0059SNeal Cardwell 		break;
552405c0059SNeal Cardwell 	case AF_INET:
553405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
554405c0059SNeal Cardwell 		break;
555405c0059SNeal Cardwell 	case AF_INET6:
556405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
557405c0059SNeal Cardwell 		break;
558405c0059SNeal Cardwell 	default:
559405c0059SNeal Cardwell 		return false;
560405c0059SNeal Cardwell 	}
561405c0059SNeal Cardwell 	*min_len += addr_len;
562405c0059SNeal Cardwell 	if (len < *min_len)
563405c0059SNeal Cardwell 		return false;
564405c0059SNeal Cardwell 
565405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
566405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
567405c0059SNeal Cardwell 		return false;
568405c0059SNeal Cardwell 
569405c0059SNeal Cardwell 	return true;
570405c0059SNeal Cardwell }
571405c0059SNeal Cardwell 
5725e1f5420SNeal Cardwell /* Validate a port comparison operator. */
5735e1f5420SNeal Cardwell static inline bool valid_port_comparison(const struct inet_diag_bc_op *op,
5745e1f5420SNeal Cardwell 					 int len, int *min_len)
5755e1f5420SNeal Cardwell {
5765e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
5775e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
5785e1f5420SNeal Cardwell 	if (len < *min_len)
5795e1f5420SNeal Cardwell 		return false;
5805e1f5420SNeal Cardwell 	return true;
5815e1f5420SNeal Cardwell }
5825e1f5420SNeal Cardwell 
583a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
584a8c2190eSArnaldo Carvalho de Melo {
585eeb14972SEric Dumazet 	const void *bc = bytecode;
586a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
587a8c2190eSArnaldo Carvalho de Melo 
588a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
589eeb14972SEric Dumazet 		const struct inet_diag_bc_op *op = bc;
590405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
591a8c2190eSArnaldo Carvalho de Melo 
592a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
593a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
594a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
595a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
596405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
597405c0059SNeal Cardwell 				return -EINVAL;
5985e1f5420SNeal Cardwell 			break;
599a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
600a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
601a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
602a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
6035e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
604a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
605a8c2190eSArnaldo Carvalho de Melo 			break;
6065e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
6075e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
608a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
609a8c2190eSArnaldo Carvalho de Melo 			break;
610a8c2190eSArnaldo Carvalho de Melo 		default:
611a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
612a8c2190eSArnaldo Carvalho de Melo 		}
6135e1f5420SNeal Cardwell 
6145e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
6155e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
6165e1f5420SNeal Cardwell 				return -EINVAL;
6175e1f5420SNeal Cardwell 			if (op->no < len &&
6185e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
6195e1f5420SNeal Cardwell 				return -EINVAL;
6205e1f5420SNeal Cardwell 		}
6215e1f5420SNeal Cardwell 
622405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
623eeb14972SEric Dumazet 			return -EINVAL;
624a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
625a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
626a8c2190eSArnaldo Carvalho de Melo 	}
627a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
628a8c2190eSArnaldo Carvalho de Melo }
629a8c2190eSArnaldo Carvalho de Melo 
630dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
631dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
63237f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
633c8991362SPavel Emelyanov 			      struct inet_diag_req_v2 *r,
63437f352b5SPavel Emelyanov 			      const struct nlattr *bc)
635a8c2190eSArnaldo Carvalho de Melo {
6368d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
637a8c2190eSArnaldo Carvalho de Melo 		return 0;
638a8c2190eSArnaldo Carvalho de Melo 
639a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
640e32123e5SPatrick McHardy 				  sk_user_ns(NETLINK_CB(cb->skb).sk),
64115e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
642a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
643a8c2190eSArnaldo Carvalho de Melo }
644a8c2190eSArnaldo Carvalho de Melo 
645*49612729SEric Dumazet static void twsk_build_assert(void)
646*49612729SEric Dumazet {
647*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
648*49612729SEric Dumazet 		     offsetof(struct sock, sk_family));
649*49612729SEric Dumazet 
650*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
651*49612729SEric Dumazet 		     offsetof(struct inet_sock, inet_num));
652*49612729SEric Dumazet 
653*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
654*49612729SEric Dumazet 		     offsetof(struct inet_sock, inet_dport));
655*49612729SEric Dumazet 
656*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
657*49612729SEric Dumazet 		     offsetof(struct inet_sock, inet_rcv_saddr));
658*49612729SEric Dumazet 
659*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
660*49612729SEric Dumazet 		     offsetof(struct inet_sock, inet_daddr));
661*49612729SEric Dumazet 
662*49612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
663*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
664*49612729SEric Dumazet 		     offsetof(struct sock, sk_v6_rcv_saddr));
665*49612729SEric Dumazet 
666*49612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
667*49612729SEric Dumazet 		     offsetof(struct sock, sk_v6_daddr));
668*49612729SEric Dumazet #endif
669*49612729SEric Dumazet }
670*49612729SEric Dumazet 
67105dbc7b5SEric Dumazet static int inet_twsk_diag_dump(struct sock *sk,
672c7d58aabSArnaldo Carvalho de Melo 			       struct sk_buff *skb,
67337f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
674c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
67537f352b5SPavel Emelyanov 			       const struct nlattr *bc)
676c7d58aabSArnaldo Carvalho de Melo {
677*49612729SEric Dumazet 	twsk_build_assert();
67805dbc7b5SEric Dumazet 
679*49612729SEric Dumazet 	if (!inet_diag_bc_sk(bc, sk))
680c7d58aabSArnaldo Carvalho de Melo 		return 0;
681c7d58aabSArnaldo Carvalho de Melo 
682*49612729SEric Dumazet 	return inet_twsk_diag_fill(inet_twsk(sk), skb, r,
68315e47304SEric W. Biederman 				   NETLINK_CB(cb->skb).portid,
684c7d58aabSArnaldo Carvalho de Melo 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
685c7d58aabSArnaldo Carvalho de Melo }
686c7d58aabSArnaldo Carvalho de Melo 
6871c95df85SNeal Cardwell /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
6881c95df85SNeal Cardwell  * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
6891c95df85SNeal Cardwell  */
6901c95df85SNeal Cardwell static inline void inet_diag_req_addrs(const struct sock *sk,
6911c95df85SNeal Cardwell 				       const struct request_sock *req,
6921c95df85SNeal Cardwell 				       struct inet_diag_entry *entry)
6931c95df85SNeal Cardwell {
6941c95df85SNeal Cardwell 	struct inet_request_sock *ireq = inet_rsk(req);
6951c95df85SNeal Cardwell 
6961c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
6971c95df85SNeal Cardwell 	if (sk->sk_family == AF_INET6) {
6981c95df85SNeal Cardwell 		if (req->rsk_ops->family == AF_INET6) {
699634fb979SEric Dumazet 			entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
700634fb979SEric Dumazet 			entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
7011c95df85SNeal Cardwell 		} else if (req->rsk_ops->family == AF_INET) {
702634fb979SEric Dumazet 			ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
7031c95df85SNeal Cardwell 					       &entry->saddr_storage);
704634fb979SEric Dumazet 			ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
7051c95df85SNeal Cardwell 					       &entry->daddr_storage);
7061c95df85SNeal Cardwell 			entry->saddr = entry->saddr_storage.s6_addr32;
7071c95df85SNeal Cardwell 			entry->daddr = entry->daddr_storage.s6_addr32;
7081c95df85SNeal Cardwell 		}
7091c95df85SNeal Cardwell 	} else
7101c95df85SNeal Cardwell #endif
7111c95df85SNeal Cardwell 	{
712634fb979SEric Dumazet 		entry->saddr = &ireq->ir_loc_addr;
713634fb979SEric Dumazet 		entry->daddr = &ireq->ir_rmt_addr;
7141c95df85SNeal Cardwell 	}
7151c95df85SNeal Cardwell }
7161c95df85SNeal Cardwell 
717a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
718d06ca956SEric W. Biederman 			      struct request_sock *req,
719d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
72015e47304SEric W. Biederman 			      u32 portid, u32 seq,
721a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
722a8c2190eSArnaldo Carvalho de Melo {
723a8c2190eSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
724a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
725a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
726a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh;
727a8c2190eSArnaldo Carvalho de Melo 	long tmo;
728a8c2190eSArnaldo Carvalho de Melo 
72915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
7306e277ed5SThomas Graf 			NLM_F_MULTI);
7316e277ed5SThomas Graf 	if (!nlh)
7326e277ed5SThomas Graf 		return -EMSGSIZE;
733a8c2190eSArnaldo Carvalho de Melo 
7346e277ed5SThomas Graf 	r = nlmsg_data(nlh);
735a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
736a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = TCP_SYN_RECV;
737a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 1;
738e6c022a4SEric Dumazet 	r->idiag_retrans = req->num_retrans;
739a8c2190eSArnaldo Carvalho de Melo 
740a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
741f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(req, r->id.idiag_cookie);
742a8c2190eSArnaldo Carvalho de Melo 
743a8c2190eSArnaldo Carvalho de Melo 	tmo = req->expires - jiffies;
744a8c2190eSArnaldo Carvalho de Melo 	if (tmo < 0)
745a8c2190eSArnaldo Carvalho de Melo 		tmo = 0;
746a8c2190eSArnaldo Carvalho de Melo 
747c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
748634fb979SEric Dumazet 	r->id.idiag_dport = ireq->ir_rmt_port;
749b1aac815SDaniel Borkmann 
750b1aac815SDaniel Borkmann 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
751b1aac815SDaniel Borkmann 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
752b1aac815SDaniel Borkmann 
753634fb979SEric Dumazet 	r->id.idiag_src[0] = ireq->ir_loc_addr;
754634fb979SEric Dumazet 	r->id.idiag_dst[0] = ireq->ir_rmt_addr;
755b1aac815SDaniel Borkmann 
756a8c2190eSArnaldo Carvalho de Melo 	r->idiag_expires = jiffies_to_msecs(tmo);
757a8c2190eSArnaldo Carvalho de Melo 	r->idiag_rqueue = 0;
758a8c2190eSArnaldo Carvalho de Melo 	r->idiag_wqueue = 0;
759d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
760a8c2190eSArnaldo Carvalho de Melo 	r->idiag_inode = 0;
761dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
762a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
7631c95df85SNeal Cardwell 		struct inet_diag_entry entry;
7641c95df85SNeal Cardwell 		inet_diag_req_addrs(sk, req, &entry);
7651c95df85SNeal Cardwell 		memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
7661c95df85SNeal Cardwell 		memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
767a8c2190eSArnaldo Carvalho de Melo 	}
768a8c2190eSArnaldo Carvalho de Melo #endif
769a8c2190eSArnaldo Carvalho de Melo 
770053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
771053c095aSJohannes Berg 	return 0;
772a8c2190eSArnaldo Carvalho de Melo }
773a8c2190eSArnaldo Carvalho de Melo 
774a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
77537f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
776c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
77737f352b5SPavel Emelyanov 			       const struct nlattr *bc)
778a8c2190eSArnaldo Carvalho de Melo {
779a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_entry entry;
780a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
781a8c2190eSArnaldo Carvalho de Melo 	struct listen_sock *lopt;
782a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
783a8c2190eSArnaldo Carvalho de Melo 	int j, s_j;
784a8c2190eSArnaldo Carvalho de Melo 	int reqnum, s_reqnum;
785a8c2190eSArnaldo Carvalho de Melo 	int err = 0;
786a8c2190eSArnaldo Carvalho de Melo 
787a8c2190eSArnaldo Carvalho de Melo 	s_j = cb->args[3];
788a8c2190eSArnaldo Carvalho de Melo 	s_reqnum = cb->args[4];
789a8c2190eSArnaldo Carvalho de Melo 
790a8c2190eSArnaldo Carvalho de Melo 	if (s_j > 0)
791a8c2190eSArnaldo Carvalho de Melo 		s_j--;
792a8c2190eSArnaldo Carvalho de Melo 
793a8c2190eSArnaldo Carvalho de Melo 	entry.family = sk->sk_family;
794a8c2190eSArnaldo Carvalho de Melo 
795a8c2190eSArnaldo Carvalho de Melo 	read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
796a8c2190eSArnaldo Carvalho de Melo 
797a8c2190eSArnaldo Carvalho de Melo 	lopt = icsk->icsk_accept_queue.listen_opt;
798a8c2190eSArnaldo Carvalho de Melo 	if (!lopt || !lopt->qlen)
799a8c2190eSArnaldo Carvalho de Melo 		goto out;
800a8c2190eSArnaldo Carvalho de Melo 
80137f352b5SPavel Emelyanov 	if (bc != NULL) {
802c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
803a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
804a8c2190eSArnaldo Carvalho de Melo 	}
805a8c2190eSArnaldo Carvalho de Melo 
806a8c2190eSArnaldo Carvalho de Melo 	for (j = s_j; j < lopt->nr_table_entries; j++) {
807a8c2190eSArnaldo Carvalho de Melo 		struct request_sock *req, *head = lopt->syn_table[j];
808a8c2190eSArnaldo Carvalho de Melo 
809a8c2190eSArnaldo Carvalho de Melo 		reqnum = 0;
810a8c2190eSArnaldo Carvalho de Melo 		for (req = head; req; reqnum++, req = req->dl_next) {
811a8c2190eSArnaldo Carvalho de Melo 			struct inet_request_sock *ireq = inet_rsk(req);
812a8c2190eSArnaldo Carvalho de Melo 
813a8c2190eSArnaldo Carvalho de Melo 			if (reqnum < s_reqnum)
814a8c2190eSArnaldo Carvalho de Melo 				continue;
815634fb979SEric Dumazet 			if (r->id.idiag_dport != ireq->ir_rmt_port &&
816a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_dport)
817a8c2190eSArnaldo Carvalho de Melo 				continue;
818a8c2190eSArnaldo Carvalho de Melo 
819a8c2190eSArnaldo Carvalho de Melo 			if (bc) {
8201c95df85SNeal Cardwell 				inet_diag_req_addrs(sk, req, &entry);
821634fb979SEric Dumazet 				entry.dport = ntohs(ireq->ir_rmt_port);
822a8c2190eSArnaldo Carvalho de Melo 
82387c22ea5SPavel Emelyanov 				if (!inet_diag_bc_run(bc, &entry))
824a8c2190eSArnaldo Carvalho de Melo 					continue;
825a8c2190eSArnaldo Carvalho de Melo 			}
826a8c2190eSArnaldo Carvalho de Melo 
827a8c2190eSArnaldo Carvalho de Melo 			err = inet_diag_fill_req(skb, sk, req,
828e32123e5SPatrick McHardy 					       sk_user_ns(NETLINK_CB(cb->skb).sk),
82915e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
830a8c2190eSArnaldo Carvalho de Melo 					       cb->nlh->nlmsg_seq, cb->nlh);
831a8c2190eSArnaldo Carvalho de Melo 			if (err < 0) {
832a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = j + 1;
833a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = reqnum;
834a8c2190eSArnaldo Carvalho de Melo 				goto out;
835a8c2190eSArnaldo Carvalho de Melo 			}
836a8c2190eSArnaldo Carvalho de Melo 		}
837a8c2190eSArnaldo Carvalho de Melo 
838a8c2190eSArnaldo Carvalho de Melo 		s_reqnum = 0;
839a8c2190eSArnaldo Carvalho de Melo 	}
840a8c2190eSArnaldo Carvalho de Melo 
841a8c2190eSArnaldo Carvalho de Melo out:
842a8c2190eSArnaldo Carvalho de Melo 	read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
843a8c2190eSArnaldo Carvalho de Melo 
844a8c2190eSArnaldo Carvalho de Melo 	return err;
845a8c2190eSArnaldo Carvalho de Melo }
846a8c2190eSArnaldo Carvalho de Melo 
8471942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
848c8991362SPavel Emelyanov 		struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
849a8c2190eSArnaldo Carvalho de Melo {
850a8c2190eSArnaldo Carvalho de Melo 	int i, num;
851a8c2190eSArnaldo Carvalho de Melo 	int s_i, s_num;
85251d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
853a8c2190eSArnaldo Carvalho de Melo 
854a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
855a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
856a8c2190eSArnaldo Carvalho de Melo 
857a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
858a8c2190eSArnaldo Carvalho de Melo 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
859a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
860a8c2190eSArnaldo Carvalho de Melo 
861a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
862a8c2190eSArnaldo Carvalho de Melo 			struct sock *sk;
863c25eb3bfSEric Dumazet 			struct hlist_nulls_node *node;
8645caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
865a8c2190eSArnaldo Carvalho de Melo 
866a8c2190eSArnaldo Carvalho de Melo 			num = 0;
8675caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
8685caea4eaSEric Dumazet 			spin_lock_bh(&ilb->lock);
869c25eb3bfSEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->head) {
870a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
871a8c2190eSArnaldo Carvalho de Melo 
87251d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
87351d7cccfSAndrey Vagin 					continue;
87451d7cccfSAndrey Vagin 
875a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
876a8c2190eSArnaldo Carvalho de Melo 					num++;
877a8c2190eSArnaldo Carvalho de Melo 					continue;
878a8c2190eSArnaldo Carvalho de Melo 				}
879a8c2190eSArnaldo Carvalho de Melo 
880d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
881d23deaa0SPavel Emelyanov 						sk->sk_family != r->sdiag_family)
882d23deaa0SPavel Emelyanov 					goto next_listen;
883d23deaa0SPavel Emelyanov 
884c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
885a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
886a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
887a8c2190eSArnaldo Carvalho de Melo 
888a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_LISTEN) ||
889a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport ||
890a8c2190eSArnaldo Carvalho de Melo 				    cb->args[3] > 0)
891a8c2190eSArnaldo Carvalho de Melo 					goto syn_recv;
892a8c2190eSArnaldo Carvalho de Melo 
89325c4cd2bSPavel Emelyanov 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
8945caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
895a8c2190eSArnaldo Carvalho de Melo 					goto done;
896a8c2190eSArnaldo Carvalho de Melo 				}
897a8c2190eSArnaldo Carvalho de Melo 
898a8c2190eSArnaldo Carvalho de Melo syn_recv:
899a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_SYN_RECV))
900a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
901a8c2190eSArnaldo Carvalho de Melo 
90225c4cd2bSPavel Emelyanov 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
9035caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
904a8c2190eSArnaldo Carvalho de Melo 					goto done;
905a8c2190eSArnaldo Carvalho de Melo 				}
906a8c2190eSArnaldo Carvalho de Melo 
907a8c2190eSArnaldo Carvalho de Melo next_listen:
908a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = 0;
909a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = 0;
910a8c2190eSArnaldo Carvalho de Melo 				++num;
911a8c2190eSArnaldo Carvalho de Melo 			}
9125caea4eaSEric Dumazet 			spin_unlock_bh(&ilb->lock);
913a8c2190eSArnaldo Carvalho de Melo 
914a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
915a8c2190eSArnaldo Carvalho de Melo 			cb->args[3] = 0;
916a8c2190eSArnaldo Carvalho de Melo 			cb->args[4] = 0;
917a8c2190eSArnaldo Carvalho de Melo 		}
918a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
919a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
920a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
921a8c2190eSArnaldo Carvalho de Melo 	}
922a8c2190eSArnaldo Carvalho de Melo 
923a8c2190eSArnaldo Carvalho de Melo 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
924efb3cb42SPavel Emelyanov 		goto out;
925a8c2190eSArnaldo Carvalho de Melo 
926f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
927a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
9287e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
929a8c2190eSArnaldo Carvalho de Melo 		struct sock *sk;
9303ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
931a8c2190eSArnaldo Carvalho de Melo 
9326be547a6SAndi Kleen 		num = 0;
9336be547a6SAndi Kleen 
93405dbc7b5SEric Dumazet 		if (hlist_nulls_empty(&head->chain))
9356be547a6SAndi Kleen 			continue;
9366be547a6SAndi Kleen 
937a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
938a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
939a8c2190eSArnaldo Carvalho de Melo 
9407e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
9413ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
94205dbc7b5SEric Dumazet 			int res;
94370315d22SNeal Cardwell 			int state;
944a8c2190eSArnaldo Carvalho de Melo 
94551d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
94651d7cccfSAndrey Vagin 				continue;
947a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
948a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
94970315d22SNeal Cardwell 			state = (sk->sk_state == TCP_TIME_WAIT) ?
95070315d22SNeal Cardwell 				inet_twsk(sk)->tw_substate : sk->sk_state;
95170315d22SNeal Cardwell 			if (!(r->idiag_states & (1 << state)))
952a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
953d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
954d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
955d23deaa0SPavel Emelyanov 				goto next_normal;
95605dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
957a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
958a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
95905dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
9604e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
961a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
96205dbc7b5SEric Dumazet 			if (sk->sk_state == TCP_TIME_WAIT)
96305dbc7b5SEric Dumazet 				res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
96405dbc7b5SEric Dumazet 			else
96505dbc7b5SEric Dumazet 				res = inet_csk_diag_dump(sk, skb, cb, r, bc);
96605dbc7b5SEric Dumazet 			if (res < 0) {
9677e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
968a8c2190eSArnaldo Carvalho de Melo 				goto done;
969a8c2190eSArnaldo Carvalho de Melo 			}
970a8c2190eSArnaldo Carvalho de Melo next_normal:
971a8c2190eSArnaldo Carvalho de Melo 			++num;
972a8c2190eSArnaldo Carvalho de Melo 		}
973a8c2190eSArnaldo Carvalho de Melo 
9747e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
975a8c2190eSArnaldo Carvalho de Melo 	}
976a8c2190eSArnaldo Carvalho de Melo 
977a8c2190eSArnaldo Carvalho de Melo done:
978a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
979a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
980efb3cb42SPavel Emelyanov out:
981efb3cb42SPavel Emelyanov 	;
982efb3cb42SPavel Emelyanov }
9831942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
984efb3cb42SPavel Emelyanov 
985efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
986c8991362SPavel Emelyanov 		struct inet_diag_req_v2 *r, struct nlattr *bc)
987efb3cb42SPavel Emelyanov {
988efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
989cacb6ba0SCyrill Gorcunov 	int err = 0;
990efb3cb42SPavel Emelyanov 
991efb3cb42SPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
992efb3cb42SPavel Emelyanov 	if (!IS_ERR(handler))
9931942c518SPavel Emelyanov 		handler->dump(skb, cb, r, bc);
994cacb6ba0SCyrill Gorcunov 	else
995cacb6ba0SCyrill Gorcunov 		err = PTR_ERR(handler);
996d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
997efb3cb42SPavel Emelyanov 
998cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
999a8c2190eSArnaldo Carvalho de Melo }
1000a8c2190eSArnaldo Carvalho de Melo 
100125c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
100225c4cd2bSPavel Emelyanov {
100325c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
1004c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
100525c4cd2bSPavel Emelyanov 
100625c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
100725c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
100825c4cd2bSPavel Emelyanov 
1009d106352dSDavid S. Miller 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
101025c4cd2bSPavel Emelyanov }
101125c4cd2bSPavel Emelyanov 
1012a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type)
1013a029fe26SPavel Emelyanov {
1014a029fe26SPavel Emelyanov 	switch (type) {
1015a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
1016a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
1017a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
1018a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
1019a029fe26SPavel Emelyanov 	default:
1020a029fe26SPavel Emelyanov 		return 0;
1021a029fe26SPavel Emelyanov 	}
1022a029fe26SPavel Emelyanov }
1023a029fe26SPavel Emelyanov 
102425c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
102525c4cd2bSPavel Emelyanov {
1026d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1027c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
102825c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
10293b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
103025c4cd2bSPavel Emelyanov 
1031d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
103225c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
103325c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
103425c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
103525c4cd2bSPavel Emelyanov 	req.id = rc->id;
103625c4cd2bSPavel Emelyanov 
103725c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
103825c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
103925c4cd2bSPavel Emelyanov 
104025c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
104125c4cd2bSPavel Emelyanov }
104225c4cd2bSPavel Emelyanov 
1043fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1044fe50ce28SPavel Emelyanov 			       const struct nlmsghdr *nlh)
1045fe50ce28SPavel Emelyanov {
1046d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1047c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1048fe50ce28SPavel Emelyanov 
1049fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1050fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1051fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1052fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1053fe50ce28SPavel Emelyanov 	req.id = rc->id;
1054fe50ce28SPavel Emelyanov 
1055fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
1056fe50ce28SPavel Emelyanov }
1057fe50ce28SPavel Emelyanov 
10588d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1059a8c2190eSArnaldo Carvalho de Melo {
10603b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
106151d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1062a8c2190eSArnaldo Carvalho de Melo 
1063ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1064ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1065ead592baSThomas Graf 		return -EINVAL;
1066a8c2190eSArnaldo Carvalho de Melo 
1067b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1068ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1069ead592baSThomas Graf 			struct nlattr *attr;
1070ead592baSThomas Graf 
1071ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1072ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1073ead592baSThomas Graf 			if (attr == NULL ||
1074ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1075ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1076a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
1077a8c2190eSArnaldo Carvalho de Melo 		}
107880d326faSPablo Neira Ayuso 		{
107980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
108080d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
108180d326faSPablo Neira Ayuso 			};
108251d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
108380d326faSPablo Neira Ayuso 		}
1084a8c2190eSArnaldo Carvalho de Melo 	}
1085ead592baSThomas Graf 
1086fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1087a8c2190eSArnaldo Carvalho de Melo }
1088a8c2190eSArnaldo Carvalho de Melo 
1089d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1090d366477aSPavel Emelyanov {
1091c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
109251d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1093d366477aSPavel Emelyanov 
1094d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1095d366477aSPavel Emelyanov 		return -EINVAL;
1096d366477aSPavel Emelyanov 
1097d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
109825c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
109925c4cd2bSPavel Emelyanov 			struct nlattr *attr;
110025c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
110125c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
110225c4cd2bSPavel Emelyanov 			if (attr == NULL ||
110325c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
110425c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
110525c4cd2bSPavel Emelyanov 				return -EINVAL;
110625c4cd2bSPavel Emelyanov 		}
110780d326faSPablo Neira Ayuso 		{
110880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
110980d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
111080d326faSPablo Neira Ayuso 			};
111151d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
111280d326faSPablo Neira Ayuso 		}
1113d366477aSPavel Emelyanov 	}
1114d366477aSPavel Emelyanov 
1115d106352dSDavid S. Miller 	return inet_diag_get_exact(skb, h, nlmsg_data(h));
1116d366477aSPavel Emelyanov }
1117d366477aSPavel Emelyanov 
11188dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1119d366477aSPavel Emelyanov 	.family = AF_INET,
1120d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1121d366477aSPavel Emelyanov };
1122d366477aSPavel Emelyanov 
11238dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1124d366477aSPavel Emelyanov 	.family = AF_INET6,
1125d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1126d366477aSPavel Emelyanov };
1127d366477aSPavel Emelyanov 
1128a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1129a8c2190eSArnaldo Carvalho de Melo {
1130a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1131a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1132a8c2190eSArnaldo Carvalho de Melo 
1133f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1134a8c2190eSArnaldo Carvalho de Melo 		goto out;
1135a8c2190eSArnaldo Carvalho de Melo 
1136d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1137a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1138a8c2190eSArnaldo Carvalho de Melo 	if (inet_diag_table[type] == NULL) {
1139a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1140a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1141a8c2190eSArnaldo Carvalho de Melo 	}
1142d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1143a8c2190eSArnaldo Carvalho de Melo out:
1144a8c2190eSArnaldo Carvalho de Melo 	return err;
1145a8c2190eSArnaldo Carvalho de Melo }
1146a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1147a8c2190eSArnaldo Carvalho de Melo 
1148a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1149a8c2190eSArnaldo Carvalho de Melo {
1150a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1151a8c2190eSArnaldo Carvalho de Melo 
1152f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1153a8c2190eSArnaldo Carvalho de Melo 		return;
1154a8c2190eSArnaldo Carvalho de Melo 
1155d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1156a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1157d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1158a8c2190eSArnaldo Carvalho de Melo }
1159a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1160a8c2190eSArnaldo Carvalho de Melo 
1161a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1162a8c2190eSArnaldo Carvalho de Melo {
1163f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1164a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1165a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1166a8c2190eSArnaldo Carvalho de Melo 
11670da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1168a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1169a8c2190eSArnaldo Carvalho de Melo 		goto out;
1170a8c2190eSArnaldo Carvalho de Melo 
1171d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1172d366477aSPavel Emelyanov 	if (err)
1173d366477aSPavel Emelyanov 		goto out_free_nl;
1174d366477aSPavel Emelyanov 
1175d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1176d366477aSPavel Emelyanov 	if (err)
1177d366477aSPavel Emelyanov 		goto out_free_inet;
1178d366477aSPavel Emelyanov 
11798ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1180a8c2190eSArnaldo Carvalho de Melo out:
1181a8c2190eSArnaldo Carvalho de Melo 	return err;
1182d366477aSPavel Emelyanov 
1183d366477aSPavel Emelyanov out_free_inet:
1184d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1185d366477aSPavel Emelyanov out_free_nl:
1186a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1187a8c2190eSArnaldo Carvalho de Melo 	goto out;
1188a8c2190eSArnaldo Carvalho de Melo }
1189a8c2190eSArnaldo Carvalho de Melo 
1190a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1191a8c2190eSArnaldo Carvalho de Melo {
1192d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1193d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
11948ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1195a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1196a8c2190eSArnaldo Carvalho de Melo }
1197a8c2190eSArnaldo Carvalho de Melo 
1198a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1199a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1200a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1201aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1202aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1203