xref: /openbmc/linux/net/ipv4/inet_diag.c (revision 33cf7c90fe2f97afb1cadaa0cfb782cb9d1b9ee2)
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 {
41e31c5e0eSEric Dumazet 	const __be32 *saddr;
42e31c5e0eSEric Dumazet 	const __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 
68e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
69d523a328SHerbert Xu {
70d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
71d523a328SHerbert Xu }
72d523a328SHerbert Xu 
733c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
7434160ea3SEric Dumazet 		      struct sk_buff *skb, const struct inet_diag_req_v2 *req,
75d06ca956SEric W. Biederman 		      struct user_namespace *user_ns,
7615e47304SEric W. Biederman 		      u32 portid, u32 seq, u16 nlmsg_flags,
77a8c2190eSArnaldo Carvalho de Melo 		      const struct nlmsghdr *unlh)
78a8c2190eSArnaldo Carvalho de Melo {
79a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
80e31c5e0eSEric Dumazet 	const struct inet_diag_handler *handler;
81e31c5e0eSEric Dumazet 	int ext = req->idiag_ext;
82a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
83a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr  *nlh;
846e277ed5SThomas Graf 	struct nlattr *attr;
85a8c2190eSArnaldo Carvalho de Melo 	void *info = NULL;
86a8c2190eSArnaldo Carvalho de Melo 
87a029fe26SPavel Emelyanov 	handler = inet_diag_table[req->sdiag_protocol];
88e31c5e0eSEric Dumazet 	BUG_ON(!handler);
89a8c2190eSArnaldo Carvalho de Melo 
9015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
916e277ed5SThomas Graf 			nlmsg_flags);
926e277ed5SThomas Graf 	if (!nlh)
93d106352dSDavid S. Miller 		return -EMSGSIZE;
94a8c2190eSArnaldo Carvalho de Melo 
95d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
96c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(sk->sk_state == TCP_TIME_WAIT);
97c7d58aabSArnaldo Carvalho de Melo 
98a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
99a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = sk->sk_state;
100a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 0;
101a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = 0;
102a8c2190eSArnaldo Carvalho de Melo 
103a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
104f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
105a8c2190eSArnaldo Carvalho de Melo 
106c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
107c720c7e8SEric Dumazet 	r->id.idiag_dport = inet->inet_dport;
108b1aac815SDaniel Borkmann 
109b1aac815SDaniel Borkmann 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
110b1aac815SDaniel Borkmann 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
111b1aac815SDaniel Borkmann 
112c720c7e8SEric Dumazet 	r->id.idiag_src[0] = inet->inet_rcv_saddr;
113c720c7e8SEric Dumazet 	r->id.idiag_dst[0] = inet->inet_daddr;
114a8c2190eSArnaldo Carvalho de Melo 
115e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
116e4e541a8SPavel Emelyanov 		goto errout;
117e4e541a8SPavel Emelyanov 
118717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
119717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
120717b6d83SMaciej Żenczykowski 	 */
121717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1226e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1236e277ed5SThomas Graf 			goto errout;
124717b6d83SMaciej Żenczykowski 
125dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
126a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
127efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
128efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
1296e277ed5SThomas Graf 
13006236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
131efe4208fSEric Dumazet 			if (nla_put_u8(skb, INET_DIAG_TCLASS,
132efe4208fSEric Dumazet 				       inet6_sk(sk)->tclass) < 0)
1336e277ed5SThomas Graf 				goto errout;
134a8c2190eSArnaldo Carvalho de Melo 	}
135a8c2190eSArnaldo Carvalho de Melo #endif
136a8c2190eSArnaldo Carvalho de Melo 
137d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1383c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1393c4d05c8SPavel Emelyanov 
1406e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
1416e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
1426e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
1436e277ed5SThomas Graf 			.idiag_wmem = sk->sk_wmem_queued,
1446e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
1456e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
1466e277ed5SThomas Graf 		};
1476e277ed5SThomas Graf 
1486e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
1496e277ed5SThomas Graf 			goto errout;
1503c4d05c8SPavel Emelyanov 	}
1513c4d05c8SPavel Emelyanov 
152c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
153c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
1546e277ed5SThomas Graf 			goto errout;
155c0636faaSPavel Emelyanov 
156e31c5e0eSEric Dumazet 	if (!icsk) {
15762ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
1583c4d05c8SPavel Emelyanov 		goto out;
1593c4d05c8SPavel Emelyanov 	}
1603c4d05c8SPavel Emelyanov 
161172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
162a8c2190eSArnaldo Carvalho de Melo 
1636ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
1646ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
1656ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
166a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
167a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
168a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
169a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
170a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
171a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
172a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
173a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
174a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
175a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
176a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
177a8c2190eSArnaldo Carvalho de Melo 	} else {
178a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
179a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
180a8c2190eSArnaldo Carvalho de Melo 	}
181a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
182a8c2190eSArnaldo Carvalho de Melo 
1836e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_INFO - 1))) {
1846e277ed5SThomas Graf 		attr = nla_reserve(skb, INET_DIAG_INFO,
1856e277ed5SThomas Graf 				   sizeof(struct tcp_info));
1866e277ed5SThomas Graf 		if (!attr)
1876e277ed5SThomas Graf 			goto errout;
188a8c2190eSArnaldo Carvalho de Melo 
1896e277ed5SThomas Graf 		info = nla_data(attr);
190a8c2190eSArnaldo Carvalho de Melo 	}
191a8c2190eSArnaldo Carvalho de Melo 
1926e277ed5SThomas Graf 	if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
1936e277ed5SThomas Graf 		if (nla_put_string(skb, INET_DIAG_CONG,
1946e277ed5SThomas Graf 				   icsk->icsk_ca_ops->name) < 0)
1956e277ed5SThomas Graf 			goto errout;
1966e277ed5SThomas Graf 
197a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
198a8c2190eSArnaldo Carvalho de Melo 
199a8c2190eSArnaldo Carvalho de Melo 	if (sk->sk_state < TCP_TIME_WAIT &&
200a8c2190eSArnaldo Carvalho de Melo 	    icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
201a8c2190eSArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->get_info(sk, ext, skb);
202a8c2190eSArnaldo Carvalho de Melo 
2033c4d05c8SPavel Emelyanov out:
204053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
205053c095aSJohannes Berg 	return 0;
206a8c2190eSArnaldo Carvalho de Melo 
2076e277ed5SThomas Graf errout:
2086e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
20926932566SPatrick McHardy 	return -EMSGSIZE;
210a8c2190eSArnaldo Carvalho de Melo }
2113c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
2123c4d05c8SPavel Emelyanov 
2133c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
214e31c5e0eSEric Dumazet 			      struct sk_buff *skb,
21534160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *req,
216d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
21715e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
2183c4d05c8SPavel Emelyanov 			      const struct nlmsghdr *unlh)
2193c4d05c8SPavel Emelyanov {
220e31c5e0eSEric Dumazet 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
221e31c5e0eSEric Dumazet 				 user_ns, portid, seq, nlmsg_flags, unlh);
2223c4d05c8SPavel Emelyanov }
223a8c2190eSArnaldo Carvalho de Melo 
224*33cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk,
225e31c5e0eSEric Dumazet 			       struct sk_buff *skb,
22634160ea3SEric Dumazet 			       const 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 {
230*33cf7c90SEric Dumazet 	struct inet_timewait_sock *tw = inet_twsk(sk);
231c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
2326e277ed5SThomas Graf 	struct nlmsghdr *nlh;
233e31c5e0eSEric Dumazet 	s32 tmo;
234c7d58aabSArnaldo Carvalho de Melo 
23515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
2366e277ed5SThomas Graf 			nlmsg_flags);
2376e277ed5SThomas Graf 	if (!nlh)
238d106352dSDavid S. Miller 		return -EMSGSIZE;
239d106352dSDavid S. Miller 
240d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
241c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
242c7d58aabSArnaldo Carvalho de Melo 
24396f817feSEric Dumazet 	tmo = tw->tw_ttd - inet_tw_time_stamp();
244c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
245c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
246c7d58aabSArnaldo Carvalho de Melo 
247c7d58aabSArnaldo Carvalho de Melo 	r->idiag_family	      = tw->tw_family;
248c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
249b1aac815SDaniel Borkmann 
250c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_if	      = tw->tw_bound_dev_if;
251*33cf7c90SEric Dumazet 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
252b1aac815SDaniel Borkmann 
253c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_sport     = tw->tw_sport;
254c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dport     = tw->tw_dport;
255b1aac815SDaniel Borkmann 
256b1aac815SDaniel Borkmann 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
257b1aac815SDaniel Borkmann 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
258b1aac815SDaniel Borkmann 
259c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_src[0]    = tw->tw_rcv_saddr;
260c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dst[0]    = tw->tw_daddr;
261b1aac815SDaniel Borkmann 
262c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
263c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
26496f817feSEric Dumazet 	r->idiag_expires      = jiffies_to_msecs(tmo);
265c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
266c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
267c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
268c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
269dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
270c7d58aabSArnaldo Carvalho de Melo 	if (tw->tw_family == AF_INET6) {
271efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr;
272efe4208fSEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr;
273c7d58aabSArnaldo Carvalho de Melo 	}
274c7d58aabSArnaldo Carvalho de Melo #endif
2756e277ed5SThomas Graf 
276053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
277053c095aSJohannes Berg 	return 0;
278c7d58aabSArnaldo Carvalho de Melo }
279c7d58aabSArnaldo Carvalho de Melo 
280dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
28134160ea3SEric Dumazet 			const struct inet_diag_req_v2 *r,
282d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
28315e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
284dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
285dff2c035SArnaldo Carvalho de Melo {
286dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
287*33cf7c90SEric Dumazet 		return inet_twsk_diag_fill(sk, skb, r, portid, seq,
288efe4208fSEric Dumazet 					   nlmsg_flags, unlh);
289efe4208fSEric Dumazet 
290efe4208fSEric Dumazet 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
291efe4208fSEric Dumazet 				  nlmsg_flags, unlh);
292dff2c035SArnaldo Carvalho de Melo }
293dff2c035SArnaldo Carvalho de Melo 
294e31c5e0eSEric Dumazet int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
295e31c5e0eSEric Dumazet 			    struct sk_buff *in_skb,
296e31c5e0eSEric Dumazet 			    const struct nlmsghdr *nlh,
29734160ea3SEric Dumazet 			    const struct inet_diag_req_v2 *req)
298a8c2190eSArnaldo Carvalho de Melo {
29951d7cccfSAndrey Vagin 	struct net *net = sock_net(in_skb->sk);
300e31c5e0eSEric Dumazet 	struct sk_buff *rep;
301e31c5e0eSEric Dumazet 	struct sock *sk;
302e31c5e0eSEric Dumazet 	int err;
303a8c2190eSArnaldo Carvalho de Melo 
304d523a328SHerbert Xu 	err = -EINVAL;
305e31c5e0eSEric Dumazet 	if (req->sdiag_family == AF_INET)
30651d7cccfSAndrey Vagin 		sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
307a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
308a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
309dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
310e31c5e0eSEric Dumazet 	else if (req->sdiag_family == AF_INET6)
31151d7cccfSAndrey Vagin 		sk = inet6_lookup(net, hashinfo,
312a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
313a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
314a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
315a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
316a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
317a8c2190eSArnaldo Carvalho de Melo #endif
318e31c5e0eSEric Dumazet 	else
319476f7dbfSPavel Emelyanov 		goto out_nosk;
320a8c2190eSArnaldo Carvalho de Melo 
321d523a328SHerbert Xu 	err = -ENOENT;
322e31c5e0eSEric Dumazet 	if (!sk)
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,
36234160ea3SEric Dumazet 			       const 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 
40387c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
404a8c2190eSArnaldo Carvalho de Melo 			    const struct inet_diag_entry *entry)
405a8c2190eSArnaldo Carvalho de Melo {
40687c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
40787c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
40887c22ea5SPavel Emelyanov 
409a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
410a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
411a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
412a8c2190eSArnaldo Carvalho de Melo 
413a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
414a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
415a8c2190eSArnaldo Carvalho de Melo 			break;
416a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
417a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
418a8c2190eSArnaldo Carvalho de Melo 			break;
419a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
420a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
421a8c2190eSArnaldo Carvalho de Melo 			break;
422a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
423b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
424a8c2190eSArnaldo Carvalho de Melo 			break;
425a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
426a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
427a8c2190eSArnaldo Carvalho de Melo 			break;
428a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
429a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
430a8c2190eSArnaldo Carvalho de Melo 			break;
431a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
432a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
433a8c2190eSArnaldo Carvalho de Melo 			break;
434a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
435a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
436e31c5e0eSEric Dumazet 			const struct inet_diag_hostcond *cond;
437e31c5e0eSEric Dumazet 			const __be32 *addr;
438a8c2190eSArnaldo Carvalho de Melo 
439e31c5e0eSEric Dumazet 			cond = (const struct inet_diag_hostcond *)(op + 1);
440a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
441a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
442a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
443a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
444a8c2190eSArnaldo Carvalho de Melo 				break;
445a8c2190eSArnaldo Carvalho de Melo 			}
446a8c2190eSArnaldo Carvalho de Melo 
447a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
448a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
449a8c2190eSArnaldo Carvalho de Melo 			else
450a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
451a8c2190eSArnaldo Carvalho de Melo 
452f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
453f67caec9SNeal Cardwell 			    cond->family != entry->family) {
454a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
455a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
456a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
457a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
458f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
459f67caec9SNeal Cardwell 							    cond->addr,
460a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
461a8c2190eSArnaldo Carvalho de Melo 						break;
462a8c2190eSArnaldo Carvalho de Melo 				}
463a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
464a8c2190eSArnaldo Carvalho de Melo 				break;
465a8c2190eSArnaldo Carvalho de Melo 			}
466f67caec9SNeal Cardwell 
467f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
468f67caec9SNeal Cardwell 				break;
469f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
470f67caec9SNeal Cardwell 					    cond->prefix_len))
471f67caec9SNeal Cardwell 				break;
472f67caec9SNeal Cardwell 			yes = 0;
473f67caec9SNeal Cardwell 			break;
474f67caec9SNeal Cardwell 		}
475a8c2190eSArnaldo Carvalho de Melo 		}
476a8c2190eSArnaldo Carvalho de Melo 
477a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
478a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
479a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
480a8c2190eSArnaldo Carvalho de Melo 		} else {
481a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
482a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
483a8c2190eSArnaldo Carvalho de Melo 		}
484a8c2190eSArnaldo Carvalho de Melo 	}
485a02cec21SEric Dumazet 	return len == 0;
486a8c2190eSArnaldo Carvalho de Melo }
487a8c2190eSArnaldo Carvalho de Melo 
4888d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
4898d07d151SPavel Emelyanov {
4908d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
491e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
4928d07d151SPavel Emelyanov 
493e31c5e0eSEric Dumazet 	if (!bc)
4948d07d151SPavel Emelyanov 		return 1;
4958d07d151SPavel Emelyanov 
4968d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
497dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4988d07d151SPavel Emelyanov 	if (entry.family == AF_INET6) {
499efe4208fSEric Dumazet 		entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
500efe4208fSEric Dumazet 		entry.daddr = sk->sk_v6_daddr.s6_addr32;
5018d07d151SPavel Emelyanov 	} else
5028d07d151SPavel Emelyanov #endif
5038d07d151SPavel Emelyanov 	{
5048d07d151SPavel Emelyanov 		entry.saddr = &inet->inet_rcv_saddr;
5058d07d151SPavel Emelyanov 		entry.daddr = &inet->inet_daddr;
5068d07d151SPavel Emelyanov 	}
5078d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
5088d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
50949612729SEric Dumazet 	entry.userlocks = (sk->sk_state != TCP_TIME_WAIT) ? sk->sk_userlocks : 0;
5108d07d151SPavel Emelyanov 
5118d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
5128d07d151SPavel Emelyanov }
5138d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
5148d07d151SPavel Emelyanov 
515a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
516a8c2190eSArnaldo Carvalho de Melo {
517a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
518a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
519a8c2190eSArnaldo Carvalho de Melo 
520a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
521a8c2190eSArnaldo Carvalho de Melo 			return 0;
522a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
523a8c2190eSArnaldo Carvalho de Melo 			return 1;
524eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
525a8c2190eSArnaldo Carvalho de Melo 			return 0;
526a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
527a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
528a8c2190eSArnaldo Carvalho de Melo 	}
529a8c2190eSArnaldo Carvalho de Melo 	return 0;
530a8c2190eSArnaldo Carvalho de Melo }
531a8c2190eSArnaldo Carvalho de Melo 
532405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
533405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
534405c0059SNeal Cardwell 			   int *min_len)
535405c0059SNeal Cardwell {
536405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
537e31c5e0eSEric Dumazet 	int addr_len;
538405c0059SNeal Cardwell 
539405c0059SNeal Cardwell 	/* Check hostcond space. */
540405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
541405c0059SNeal Cardwell 	if (len < *min_len)
542405c0059SNeal Cardwell 		return false;
543405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
544405c0059SNeal Cardwell 
545405c0059SNeal Cardwell 	/* Check address family and address length. */
546405c0059SNeal Cardwell 	switch (cond->family) {
547405c0059SNeal Cardwell 	case AF_UNSPEC:
548405c0059SNeal Cardwell 		addr_len = 0;
549405c0059SNeal Cardwell 		break;
550405c0059SNeal Cardwell 	case AF_INET:
551405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
552405c0059SNeal Cardwell 		break;
553405c0059SNeal Cardwell 	case AF_INET6:
554405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
555405c0059SNeal Cardwell 		break;
556405c0059SNeal Cardwell 	default:
557405c0059SNeal Cardwell 		return false;
558405c0059SNeal Cardwell 	}
559405c0059SNeal Cardwell 	*min_len += addr_len;
560405c0059SNeal Cardwell 	if (len < *min_len)
561405c0059SNeal Cardwell 		return false;
562405c0059SNeal Cardwell 
563405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
564405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
565405c0059SNeal Cardwell 		return false;
566405c0059SNeal Cardwell 
567405c0059SNeal Cardwell 	return true;
568405c0059SNeal Cardwell }
569405c0059SNeal Cardwell 
5705e1f5420SNeal Cardwell /* Validate a port comparison operator. */
571e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op,
5725e1f5420SNeal Cardwell 				  int len, int *min_len)
5735e1f5420SNeal Cardwell {
5745e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
5755e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
5765e1f5420SNeal Cardwell 	if (len < *min_len)
5775e1f5420SNeal Cardwell 		return false;
5785e1f5420SNeal Cardwell 	return true;
5795e1f5420SNeal Cardwell }
5805e1f5420SNeal Cardwell 
581a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
582a8c2190eSArnaldo Carvalho de Melo {
583eeb14972SEric Dumazet 	const void *bc = bytecode;
584a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
585a8c2190eSArnaldo Carvalho de Melo 
586a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
587405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
588e31c5e0eSEric Dumazet 		const struct inet_diag_bc_op *op = bc;
589a8c2190eSArnaldo Carvalho de Melo 
590a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
591a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
592a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
593405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
594405c0059SNeal Cardwell 				return -EINVAL;
5955e1f5420SNeal Cardwell 			break;
596a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
597a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
598a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
599a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
6005e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
601a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
602a8c2190eSArnaldo Carvalho de Melo 			break;
6035e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
6045e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
605a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
606a8c2190eSArnaldo Carvalho de Melo 			break;
607a8c2190eSArnaldo Carvalho de Melo 		default:
608a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
609a8c2190eSArnaldo Carvalho de Melo 		}
6105e1f5420SNeal Cardwell 
6115e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
6125e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
6135e1f5420SNeal Cardwell 				return -EINVAL;
6145e1f5420SNeal Cardwell 			if (op->no < len &&
6155e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
6165e1f5420SNeal Cardwell 				return -EINVAL;
6175e1f5420SNeal Cardwell 		}
6185e1f5420SNeal Cardwell 
619405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
620eeb14972SEric Dumazet 			return -EINVAL;
621a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
622a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
623a8c2190eSArnaldo Carvalho de Melo 	}
624a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
625a8c2190eSArnaldo Carvalho de Melo }
626a8c2190eSArnaldo Carvalho de Melo 
627dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
628dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
62937f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
63034160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *r,
63137f352b5SPavel Emelyanov 			      const struct nlattr *bc)
632a8c2190eSArnaldo Carvalho de Melo {
6338d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
634a8c2190eSArnaldo Carvalho de Melo 		return 0;
635a8c2190eSArnaldo Carvalho de Melo 
636a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
637e32123e5SPatrick McHardy 				  sk_user_ns(NETLINK_CB(cb->skb).sk),
63815e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
639a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
640a8c2190eSArnaldo Carvalho de Melo }
641a8c2190eSArnaldo Carvalho de Melo 
64249612729SEric Dumazet static void twsk_build_assert(void)
64349612729SEric Dumazet {
64449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
64549612729SEric Dumazet 		     offsetof(struct sock, sk_family));
64649612729SEric Dumazet 
64749612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
64849612729SEric Dumazet 		     offsetof(struct inet_sock, inet_num));
64949612729SEric Dumazet 
65049612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
65149612729SEric Dumazet 		     offsetof(struct inet_sock, inet_dport));
65249612729SEric Dumazet 
65349612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
65449612729SEric Dumazet 		     offsetof(struct inet_sock, inet_rcv_saddr));
65549612729SEric Dumazet 
65649612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
65749612729SEric Dumazet 		     offsetof(struct inet_sock, inet_daddr));
65849612729SEric Dumazet 
65949612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
66049612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
66149612729SEric Dumazet 		     offsetof(struct sock, sk_v6_rcv_saddr));
66249612729SEric Dumazet 
66349612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
66449612729SEric Dumazet 		     offsetof(struct sock, sk_v6_daddr));
66549612729SEric Dumazet #endif
66649612729SEric Dumazet }
66749612729SEric Dumazet 
66805dbc7b5SEric Dumazet static int inet_twsk_diag_dump(struct sock *sk,
669c7d58aabSArnaldo Carvalho de Melo 			       struct sk_buff *skb,
67037f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
67134160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *r,
67237f352b5SPavel Emelyanov 			       const struct nlattr *bc)
673c7d58aabSArnaldo Carvalho de Melo {
67449612729SEric Dumazet 	twsk_build_assert();
67505dbc7b5SEric Dumazet 
67649612729SEric Dumazet 	if (!inet_diag_bc_sk(bc, sk))
677c7d58aabSArnaldo Carvalho de Melo 		return 0;
678c7d58aabSArnaldo Carvalho de Melo 
679*33cf7c90SEric Dumazet 	return inet_twsk_diag_fill(sk, skb, r,
68015e47304SEric W. Biederman 				   NETLINK_CB(cb->skb).portid,
681c7d58aabSArnaldo Carvalho de Melo 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
682c7d58aabSArnaldo Carvalho de Melo }
683c7d58aabSArnaldo Carvalho de Melo 
6841c95df85SNeal Cardwell /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
6851c95df85SNeal Cardwell  * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
6861c95df85SNeal Cardwell  */
687e31c5e0eSEric Dumazet static void inet_diag_req_addrs(const struct sock *sk,
6881c95df85SNeal Cardwell 				const struct request_sock *req,
6891c95df85SNeal Cardwell 				struct inet_diag_entry *entry)
6901c95df85SNeal Cardwell {
691e31c5e0eSEric Dumazet 	const struct inet_request_sock *ireq = inet_rsk(req);
6921c95df85SNeal Cardwell 
6931c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
6941c95df85SNeal Cardwell 	if (sk->sk_family == AF_INET6) {
6951c95df85SNeal Cardwell 		if (req->rsk_ops->family == AF_INET6) {
696634fb979SEric Dumazet 			entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
697634fb979SEric Dumazet 			entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
6981c95df85SNeal Cardwell 		} else if (req->rsk_ops->family == AF_INET) {
699634fb979SEric Dumazet 			ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
7001c95df85SNeal Cardwell 					       &entry->saddr_storage);
701634fb979SEric Dumazet 			ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
7021c95df85SNeal Cardwell 					       &entry->daddr_storage);
7031c95df85SNeal Cardwell 			entry->saddr = entry->saddr_storage.s6_addr32;
7041c95df85SNeal Cardwell 			entry->daddr = entry->daddr_storage.s6_addr32;
7051c95df85SNeal Cardwell 		}
7061c95df85SNeal Cardwell 	} else
7071c95df85SNeal Cardwell #endif
7081c95df85SNeal Cardwell 	{
709634fb979SEric Dumazet 		entry->saddr = &ireq->ir_loc_addr;
710634fb979SEric Dumazet 		entry->daddr = &ireq->ir_rmt_addr;
7111c95df85SNeal Cardwell 	}
7121c95df85SNeal Cardwell }
7131c95df85SNeal Cardwell 
714a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
715d06ca956SEric W. Biederman 			      struct request_sock *req,
716d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
71715e47304SEric W. Biederman 			      u32 portid, u32 seq,
718a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
719a8c2190eSArnaldo Carvalho de Melo {
720a8c2190eSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
721a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
722a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
723a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh;
724a8c2190eSArnaldo Carvalho de Melo 	long tmo;
725a8c2190eSArnaldo Carvalho de Melo 
72615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
7276e277ed5SThomas Graf 			NLM_F_MULTI);
7286e277ed5SThomas Graf 	if (!nlh)
7296e277ed5SThomas Graf 		return -EMSGSIZE;
730a8c2190eSArnaldo Carvalho de Melo 
7316e277ed5SThomas Graf 	r = nlmsg_data(nlh);
732a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
733a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = TCP_SYN_RECV;
734a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 1;
735e6c022a4SEric Dumazet 	r->idiag_retrans = req->num_retrans;
736a8c2190eSArnaldo Carvalho de Melo 
737a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
738*33cf7c90SEric Dumazet 
739*33cf7c90SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
740*33cf7c90SEric Dumazet 		     offsetof(struct sock, sk_cookie));
741*33cf7c90SEric Dumazet 	sock_diag_save_cookie((struct sock *)ireq, 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;
764e31c5e0eSEric Dumazet 
7651c95df85SNeal Cardwell 		inet_diag_req_addrs(sk, req, &entry);
7661c95df85SNeal Cardwell 		memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
7671c95df85SNeal Cardwell 		memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
768a8c2190eSArnaldo Carvalho de Melo 	}
769a8c2190eSArnaldo Carvalho de Melo #endif
770a8c2190eSArnaldo Carvalho de Melo 
771053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
772053c095aSJohannes Berg 	return 0;
773a8c2190eSArnaldo Carvalho de Melo }
774a8c2190eSArnaldo Carvalho de Melo 
775a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
77637f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
77734160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *r,
77837f352b5SPavel Emelyanov 			       const struct nlattr *bc)
779a8c2190eSArnaldo Carvalho de Melo {
780a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
781a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
782e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
783e31c5e0eSEric Dumazet 	int j, s_j, reqnum, s_reqnum;
784e31c5e0eSEric Dumazet 	struct listen_sock *lopt;
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 
801e31c5e0eSEric Dumazet 	if (bc) {
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,
848e31c5e0eSEric Dumazet 			 struct netlink_callback *cb,
84934160ea3SEric Dumazet 			 const struct inet_diag_req_v2 *r, struct nlattr *bc)
850a8c2190eSArnaldo Carvalho de Melo {
85151d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
852e31c5e0eSEric Dumazet 	int i, num, s_i, s_num;
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++) {
8625caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
863e31c5e0eSEric Dumazet 			struct hlist_nulls_node *node;
864e31c5e0eSEric Dumazet 			struct sock *sk;
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);
9293ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
930e31c5e0eSEric Dumazet 		struct sock *sk;
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) {
942e31c5e0eSEric Dumazet 			int state, res;
943a8c2190eSArnaldo Carvalho de Melo 
94451d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
94551d7cccfSAndrey Vagin 				continue;
946a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
947a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
94870315d22SNeal Cardwell 			state = (sk->sk_state == TCP_TIME_WAIT) ?
94970315d22SNeal Cardwell 				inet_twsk(sk)->tw_substate : sk->sk_state;
95070315d22SNeal Cardwell 			if (!(r->idiag_states & (1 << state)))
951a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
952d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
953d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
954d23deaa0SPavel Emelyanov 				goto next_normal;
95505dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
956a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
957a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
95805dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
9594e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
960a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
96105dbc7b5SEric Dumazet 			if (sk->sk_state == TCP_TIME_WAIT)
96205dbc7b5SEric Dumazet 				res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
96305dbc7b5SEric Dumazet 			else
96405dbc7b5SEric Dumazet 				res = inet_csk_diag_dump(sk, skb, cb, r, bc);
96505dbc7b5SEric Dumazet 			if (res < 0) {
9667e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
967a8c2190eSArnaldo Carvalho de Melo 				goto done;
968a8c2190eSArnaldo Carvalho de Melo 			}
969a8c2190eSArnaldo Carvalho de Melo next_normal:
970a8c2190eSArnaldo Carvalho de Melo 			++num;
971a8c2190eSArnaldo Carvalho de Melo 		}
972a8c2190eSArnaldo Carvalho de Melo 
9737e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
974a8c2190eSArnaldo Carvalho de Melo 	}
975a8c2190eSArnaldo Carvalho de Melo 
976a8c2190eSArnaldo Carvalho de Melo done:
977a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
978a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
979efb3cb42SPavel Emelyanov out:
980efb3cb42SPavel Emelyanov 	;
981efb3cb42SPavel Emelyanov }
9821942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
983efb3cb42SPavel Emelyanov 
984efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
98534160ea3SEric Dumazet 			    const struct inet_diag_req_v2 *r,
986e31c5e0eSEric Dumazet 			    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 {
1003c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
1004e31c5e0eSEric Dumazet 	struct nlattr *bc = NULL;
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 
1012e31c5e0eSEric Dumazet static 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 
1024e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb,
1025e31c5e0eSEric Dumazet 				 struct netlink_callback *cb)
102625c4cd2bSPavel Emelyanov {
1027d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1028e31c5e0eSEric Dumazet 	int hdrlen = sizeof(struct inet_diag_req);
1029c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
103025c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
103125c4cd2bSPavel Emelyanov 
1032d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
103325c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
103425c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
103525c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
103625c4cd2bSPavel Emelyanov 	req.id = rc->id;
103725c4cd2bSPavel Emelyanov 
103825c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
103925c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
104025c4cd2bSPavel Emelyanov 
104125c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
104225c4cd2bSPavel Emelyanov }
104325c4cd2bSPavel Emelyanov 
1044fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1045fe50ce28SPavel Emelyanov 				      const struct nlmsghdr *nlh)
1046fe50ce28SPavel Emelyanov {
1047d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1048c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1049fe50ce28SPavel Emelyanov 
1050fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1051fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1052fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1053fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1054fe50ce28SPavel Emelyanov 	req.id = rc->id;
1055fe50ce28SPavel Emelyanov 
1056fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
1057fe50ce28SPavel Emelyanov }
1058fe50ce28SPavel Emelyanov 
10598d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1060a8c2190eSArnaldo Carvalho de Melo {
10613b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
106251d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1063a8c2190eSArnaldo Carvalho de Melo 
1064ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1065ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1066ead592baSThomas Graf 		return -EINVAL;
1067a8c2190eSArnaldo Carvalho de Melo 
1068b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1069ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1070ead592baSThomas Graf 			struct nlattr *attr;
1071ead592baSThomas Graf 
1072ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1073ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1074e31c5e0eSEric Dumazet 			if (!attr ||
1075ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1076ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1077a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
1078a8c2190eSArnaldo Carvalho de Melo 		}
107980d326faSPablo Neira Ayuso 		{
108080d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
108180d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
108280d326faSPablo Neira Ayuso 			};
108351d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
108480d326faSPablo Neira Ayuso 		}
1085a8c2190eSArnaldo Carvalho de Melo 	}
1086ead592baSThomas Graf 
1087fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1088a8c2190eSArnaldo Carvalho de Melo }
1089a8c2190eSArnaldo Carvalho de Melo 
1090d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1091d366477aSPavel Emelyanov {
1092c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
109351d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1094d366477aSPavel Emelyanov 
1095d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1096d366477aSPavel Emelyanov 		return -EINVAL;
1097d366477aSPavel Emelyanov 
1098d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
109925c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
110025c4cd2bSPavel Emelyanov 			struct nlattr *attr;
1101e31c5e0eSEric Dumazet 
110225c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
110325c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
1104e31c5e0eSEric Dumazet 			if (!attr ||
110525c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
110625c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
110725c4cd2bSPavel Emelyanov 				return -EINVAL;
110825c4cd2bSPavel Emelyanov 		}
110980d326faSPablo Neira Ayuso 		{
111080d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
111180d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
111280d326faSPablo Neira Ayuso 			};
111351d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
111480d326faSPablo Neira Ayuso 		}
1115d366477aSPavel Emelyanov 	}
1116d366477aSPavel Emelyanov 
1117d106352dSDavid S. Miller 	return inet_diag_get_exact(skb, h, nlmsg_data(h));
1118d366477aSPavel Emelyanov }
1119d366477aSPavel Emelyanov 
11208dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1121d366477aSPavel Emelyanov 	.family = AF_INET,
1122d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1123d366477aSPavel Emelyanov };
1124d366477aSPavel Emelyanov 
11258dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1126d366477aSPavel Emelyanov 	.family = AF_INET6,
1127d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1128d366477aSPavel Emelyanov };
1129d366477aSPavel Emelyanov 
1130a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1131a8c2190eSArnaldo Carvalho de Melo {
1132a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1133a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1134a8c2190eSArnaldo Carvalho de Melo 
1135f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1136a8c2190eSArnaldo Carvalho de Melo 		goto out;
1137a8c2190eSArnaldo Carvalho de Melo 
1138d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1139a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1140e31c5e0eSEric Dumazet 	if (!inet_diag_table[type]) {
1141a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1142a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1143a8c2190eSArnaldo Carvalho de Melo 	}
1144d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1145a8c2190eSArnaldo Carvalho de Melo out:
1146a8c2190eSArnaldo Carvalho de Melo 	return err;
1147a8c2190eSArnaldo Carvalho de Melo }
1148a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1149a8c2190eSArnaldo Carvalho de Melo 
1150a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1151a8c2190eSArnaldo Carvalho de Melo {
1152a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1153a8c2190eSArnaldo Carvalho de Melo 
1154f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1155a8c2190eSArnaldo Carvalho de Melo 		return;
1156a8c2190eSArnaldo Carvalho de Melo 
1157d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1158a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1159d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1160a8c2190eSArnaldo Carvalho de Melo }
1161a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1162a8c2190eSArnaldo Carvalho de Melo 
1163a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1164a8c2190eSArnaldo Carvalho de Melo {
1165f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1166a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1167a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1168a8c2190eSArnaldo Carvalho de Melo 
11690da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1170a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1171a8c2190eSArnaldo Carvalho de Melo 		goto out;
1172a8c2190eSArnaldo Carvalho de Melo 
1173d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1174d366477aSPavel Emelyanov 	if (err)
1175d366477aSPavel Emelyanov 		goto out_free_nl;
1176d366477aSPavel Emelyanov 
1177d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1178d366477aSPavel Emelyanov 	if (err)
1179d366477aSPavel Emelyanov 		goto out_free_inet;
1180d366477aSPavel Emelyanov 
11818ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1182a8c2190eSArnaldo Carvalho de Melo out:
1183a8c2190eSArnaldo Carvalho de Melo 	return err;
1184d366477aSPavel Emelyanov 
1185d366477aSPavel Emelyanov out_free_inet:
1186d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1187d366477aSPavel Emelyanov out_free_nl:
1188a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1189a8c2190eSArnaldo Carvalho de Melo 	goto out;
1190a8c2190eSArnaldo Carvalho de Melo }
1191a8c2190eSArnaldo Carvalho de Melo 
1192a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1193a8c2190eSArnaldo Carvalho de Melo {
1194d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1195d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
11968ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1197a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1198a8c2190eSArnaldo Carvalho de Melo }
1199a8c2190eSArnaldo Carvalho de Melo 
1200a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1201a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1202a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1203aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1204aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1205