xref: /openbmc/linux/net/ipv4/inet_diag.c (revision f67caec9068cee426ec23cf9005a1dee2ecad187)
1a8c2190eSArnaldo Carvalho de Melo /*
2a8c2190eSArnaldo Carvalho de Melo  * inet_diag.c	Module for monitoring INET transport protocols sockets.
3a8c2190eSArnaldo Carvalho de Melo  *
4a8c2190eSArnaldo Carvalho de Melo  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5a8c2190eSArnaldo Carvalho de Melo  *
6a8c2190eSArnaldo Carvalho de Melo  *	This program is free software; you can redistribute it and/or
7a8c2190eSArnaldo Carvalho de Melo  *      modify it under the terms of the GNU General Public License
8a8c2190eSArnaldo Carvalho de Melo  *      as published by the Free Software Foundation; either version
9a8c2190eSArnaldo Carvalho de Melo  *      2 of the License, or (at your option) any later version.
10a8c2190eSArnaldo Carvalho de Melo  */
11a8c2190eSArnaldo Carvalho de Melo 
12172589ccSIlpo Järvinen #include <linux/kernel.h>
13a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h>
14a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h>
15a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h>
16a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h>
19a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h>
20a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h>
21a8c2190eSArnaldo Carvalho de Melo 
22a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h>
23a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h>
24a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h>
25a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h>
26a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
27a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
28a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
29a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h>
30dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
31a8c2190eSArnaldo Carvalho de Melo 
32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h>
33a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h>
34a8c2190eSArnaldo Carvalho de Melo 
35a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h>
36d366477aSPavel Emelyanov #include <linux/sock_diag.h>
37a8c2190eSArnaldo Carvalho de Melo 
38a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table;
39a8c2190eSArnaldo Carvalho de Melo 
40a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry {
419f855299SAl Viro 	__be32 *saddr;
429f855299SAl Viro 	__be32 *daddr;
43a8c2190eSArnaldo Carvalho de Melo 	u16 sport;
44a8c2190eSArnaldo Carvalho de Melo 	u16 dport;
45a8c2190eSArnaldo Carvalho de Melo 	u16 family;
46a8c2190eSArnaldo Carvalho de Melo 	u16 userlocks;
471c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
481c95df85SNeal Cardwell 	struct in6_addr saddr_storage;	/* for IPv4-mapped-IPv6 addresses */
491c95df85SNeal Cardwell 	struct in6_addr daddr_storage;	/* for IPv4-mapped-IPv6 addresses */
501c95df85SNeal Cardwell #endif
51a8c2190eSArnaldo Carvalho de Melo };
52a8c2190eSArnaldo Carvalho de Melo 
53d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
54d523a328SHerbert Xu 
55f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
56d523a328SHerbert Xu {
57f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
58aec8dc62SPavel Emelyanov 		request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
59aec8dc62SPavel Emelyanov 			       NETLINK_SOCK_DIAG, AF_INET, proto);
60d523a328SHerbert Xu 
61d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
62f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
63d523a328SHerbert Xu 		return ERR_PTR(-ENOENT);
64d523a328SHerbert Xu 
65f13c95f0SPavel Emelyanov 	return inet_diag_table[proto];
66d523a328SHerbert Xu }
67d523a328SHerbert Xu 
68d523a328SHerbert Xu static inline void inet_diag_unlock_handler(
69d523a328SHerbert Xu 	const struct inet_diag_handler *handler)
70d523a328SHerbert Xu {
71d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
72d523a328SHerbert Xu }
73d523a328SHerbert Xu 
743c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
75c8991362SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req_v2 *req,
76d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
7715e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
78a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
79a8c2190eSArnaldo Carvalho de Melo {
80a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
81a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
82a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr  *nlh;
836e277ed5SThomas Graf 	struct nlattr *attr;
84a8c2190eSArnaldo Carvalho de Melo 	void *info = NULL;
85a8c2190eSArnaldo Carvalho de Melo 	const struct inet_diag_handler *handler;
86a029fe26SPavel Emelyanov 	int ext = req->idiag_ext;
87a8c2190eSArnaldo Carvalho de Melo 
88a029fe26SPavel Emelyanov 	handler = inet_diag_table[req->sdiag_protocol];
89a8c2190eSArnaldo Carvalho de Melo 	BUG_ON(handler == NULL);
90a8c2190eSArnaldo Carvalho de Melo 
9115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
926e277ed5SThomas Graf 			nlmsg_flags);
936e277ed5SThomas Graf 	if (!nlh)
94d106352dSDavid S. Miller 		return -EMSGSIZE;
95a8c2190eSArnaldo Carvalho de Melo 
96d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
97c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(sk->sk_state == TCP_TIME_WAIT);
98c7d58aabSArnaldo Carvalho de Melo 
99a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
100a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = sk->sk_state;
101a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 0;
102a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = 0;
103a8c2190eSArnaldo Carvalho de Melo 
104a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
105f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
106a8c2190eSArnaldo Carvalho de Melo 
107c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
108c720c7e8SEric Dumazet 	r->id.idiag_dport = inet->inet_dport;
109c720c7e8SEric Dumazet 	r->id.idiag_src[0] = inet->inet_rcv_saddr;
110c720c7e8SEric Dumazet 	r->id.idiag_dst[0] = inet->inet_daddr;
111a8c2190eSArnaldo Carvalho de Melo 
112717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
113717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
114717b6d83SMaciej Żenczykowski 	 */
115717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1166e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1176e277ed5SThomas Graf 			goto errout;
118717b6d83SMaciej Żenczykowski 
119dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
120a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
121b71d1d42SEric Dumazet 		const struct ipv6_pinfo *np = inet6_sk(sk);
122a8c2190eSArnaldo Carvalho de Melo 
1234e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
1244e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = np->daddr;
1256e277ed5SThomas Graf 
12606236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
1276e277ed5SThomas Graf 			if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0)
1286e277ed5SThomas Graf 				goto errout;
129a8c2190eSArnaldo Carvalho de Melo 	}
130a8c2190eSArnaldo Carvalho de Melo #endif
131a8c2190eSArnaldo Carvalho de Melo 
132d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1333c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1343c4d05c8SPavel Emelyanov 
1356e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
1366e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
1376e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
1386e277ed5SThomas Graf 			.idiag_wmem = sk->sk_wmem_queued,
1396e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
1406e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
1416e277ed5SThomas Graf 		};
1426e277ed5SThomas Graf 
1436e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
1446e277ed5SThomas Graf 			goto errout;
1453c4d05c8SPavel Emelyanov 	}
1463c4d05c8SPavel Emelyanov 
147c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
148c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
1496e277ed5SThomas Graf 			goto errout;
150c0636faaSPavel Emelyanov 
1513c4d05c8SPavel Emelyanov 	if (icsk == NULL) {
15262ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
1533c4d05c8SPavel Emelyanov 		goto out;
1543c4d05c8SPavel Emelyanov 	}
1553c4d05c8SPavel Emelyanov 
156172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
157a8c2190eSArnaldo Carvalho de Melo 
158a8c2190eSArnaldo Carvalho de Melo 	if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
159a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
160a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
161a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
162a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
163a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
164a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
165a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
166a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
167a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
168a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
169a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
170a8c2190eSArnaldo Carvalho de Melo 	} else {
171a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
172a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
173a8c2190eSArnaldo Carvalho de Melo 	}
174a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
175a8c2190eSArnaldo Carvalho de Melo 
1766e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_INFO - 1))) {
1776e277ed5SThomas Graf 		attr = nla_reserve(skb, INET_DIAG_INFO,
1786e277ed5SThomas Graf 				   sizeof(struct tcp_info));
1796e277ed5SThomas Graf 		if (!attr)
1806e277ed5SThomas Graf 			goto errout;
181a8c2190eSArnaldo Carvalho de Melo 
1826e277ed5SThomas Graf 		info = nla_data(attr);
183a8c2190eSArnaldo Carvalho de Melo 	}
184a8c2190eSArnaldo Carvalho de Melo 
1856e277ed5SThomas Graf 	if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
1866e277ed5SThomas Graf 		if (nla_put_string(skb, INET_DIAG_CONG,
1876e277ed5SThomas Graf 				   icsk->icsk_ca_ops->name) < 0)
1886e277ed5SThomas Graf 			goto errout;
1896e277ed5SThomas Graf 
190a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
191a8c2190eSArnaldo Carvalho de Melo 
192a8c2190eSArnaldo Carvalho de Melo 	if (sk->sk_state < TCP_TIME_WAIT &&
193a8c2190eSArnaldo Carvalho de Melo 	    icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
194a8c2190eSArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->get_info(sk, ext, skb);
195a8c2190eSArnaldo Carvalho de Melo 
1963c4d05c8SPavel Emelyanov out:
1976e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
198a8c2190eSArnaldo Carvalho de Melo 
1996e277ed5SThomas Graf errout:
2006e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
20126932566SPatrick McHardy 	return -EMSGSIZE;
202a8c2190eSArnaldo Carvalho de Melo }
2033c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
2043c4d05c8SPavel Emelyanov 
2053c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
206c8991362SPavel Emelyanov 			      struct sk_buff *skb, struct inet_diag_req_v2 *req,
207d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
20815e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
2093c4d05c8SPavel Emelyanov 			      const struct nlmsghdr *unlh)
2103c4d05c8SPavel Emelyanov {
2113c4d05c8SPavel Emelyanov 	return inet_sk_diag_fill(sk, inet_csk(sk),
21215e47304SEric W. Biederman 			skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
2133c4d05c8SPavel Emelyanov }
214a8c2190eSArnaldo Carvalho de Melo 
215c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
216c8991362SPavel Emelyanov 			       struct sk_buff *skb, struct inet_diag_req_v2 *req,
21715e47304SEric W. Biederman 			       u32 portid, u32 seq, u16 nlmsg_flags,
218c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
219c7d58aabSArnaldo Carvalho de Melo {
220c7d58aabSArnaldo Carvalho de Melo 	long tmo;
221c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
2226e277ed5SThomas Graf 	struct nlmsghdr *nlh;
223c7d58aabSArnaldo Carvalho de Melo 
22415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
2256e277ed5SThomas Graf 			nlmsg_flags);
2266e277ed5SThomas Graf 	if (!nlh)
227d106352dSDavid S. Miller 		return -EMSGSIZE;
228d106352dSDavid S. Miller 
229d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
230c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
231c7d58aabSArnaldo Carvalho de Melo 
232c7d58aabSArnaldo Carvalho de Melo 	tmo = tw->tw_ttd - jiffies;
233c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
234c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
235c7d58aabSArnaldo Carvalho de Melo 
236c7d58aabSArnaldo Carvalho de Melo 	r->idiag_family	      = tw->tw_family;
237c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
238c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_if	      = tw->tw_bound_dev_if;
239f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(tw, r->id.idiag_cookie);
240c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_sport     = tw->tw_sport;
241c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dport     = tw->tw_dport;
242c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_src[0]    = tw->tw_rcv_saddr;
243c7d58aabSArnaldo Carvalho de Melo 	r->id.idiag_dst[0]    = tw->tw_daddr;
244c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
245c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
246172589ccSIlpo Järvinen 	r->idiag_expires      = DIV_ROUND_UP(tmo * 1000, HZ);
247c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
248c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
249c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
250c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
251dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
252c7d58aabSArnaldo Carvalho de Melo 	if (tw->tw_family == AF_INET6) {
253c7d58aabSArnaldo Carvalho de Melo 		const struct inet6_timewait_sock *tw6 =
254c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
255c7d58aabSArnaldo Carvalho de Melo 
2564e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
2574e3fd7a0SAlexey Dobriyan 		*(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
258c7d58aabSArnaldo Carvalho de Melo 	}
259c7d58aabSArnaldo Carvalho de Melo #endif
2606e277ed5SThomas Graf 
2616e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
262c7d58aabSArnaldo Carvalho de Melo }
263c7d58aabSArnaldo Carvalho de Melo 
264dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
265d06ca956SEric W. Biederman 			struct inet_diag_req_v2 *r,
266d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
26715e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
268dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
269dff2c035SArnaldo Carvalho de Melo {
270dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
271dff2c035SArnaldo Carvalho de Melo 		return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
27215e47304SEric W. Biederman 					   skb, r, portid, seq, nlmsg_flags,
273dff2c035SArnaldo Carvalho de Melo 					   unlh);
27415e47304SEric W. Biederman 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh);
275dff2c035SArnaldo Carvalho de Melo }
276dff2c035SArnaldo Carvalho de Melo 
2771942c518SPavel Emelyanov int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
278c8991362SPavel Emelyanov 		const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
279a8c2190eSArnaldo Carvalho de Melo {
280a8c2190eSArnaldo Carvalho de Melo 	int err;
281a8c2190eSArnaldo Carvalho de Melo 	struct sock *sk;
282a8c2190eSArnaldo Carvalho de Melo 	struct sk_buff *rep;
28351d7cccfSAndrey Vagin 	struct net *net = sock_net(in_skb->sk);
284a8c2190eSArnaldo Carvalho de Melo 
285d523a328SHerbert Xu 	err = -EINVAL;
286fe50ce28SPavel Emelyanov 	if (req->sdiag_family == AF_INET) {
28751d7cccfSAndrey Vagin 		sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
288a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
289a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
290a8c2190eSArnaldo Carvalho de Melo 	}
291dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
292fe50ce28SPavel Emelyanov 	else if (req->sdiag_family == AF_INET6) {
29351d7cccfSAndrey Vagin 		sk = inet6_lookup(net, hashinfo,
294a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
295a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
296a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
297a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
298a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
299a8c2190eSArnaldo Carvalho de Melo 	}
300a8c2190eSArnaldo Carvalho de Melo #endif
301a8c2190eSArnaldo Carvalho de Melo 	else {
302476f7dbfSPavel Emelyanov 		goto out_nosk;
303a8c2190eSArnaldo Carvalho de Melo 	}
304a8c2190eSArnaldo Carvalho de Melo 
305d523a328SHerbert Xu 	err = -ENOENT;
306a8c2190eSArnaldo Carvalho de Melo 	if (sk == NULL)
307476f7dbfSPavel Emelyanov 		goto out_nosk;
308a8c2190eSArnaldo Carvalho de Melo 
309f65c1b53SPavel Emelyanov 	err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
310b005ab4eSPavel Emelyanov 	if (err)
311a8c2190eSArnaldo Carvalho de Melo 		goto out;
312a8c2190eSArnaldo Carvalho de Melo 
3136e277ed5SThomas Graf 	rep = nlmsg_new(sizeof(struct inet_diag_msg) +
314a8c2190eSArnaldo Carvalho de Melo 			sizeof(struct inet_diag_meminfo) +
3156e277ed5SThomas Graf 			sizeof(struct tcp_info) + 64, GFP_KERNEL);
3166e277ed5SThomas Graf 	if (!rep) {
3176e277ed5SThomas Graf 		err = -ENOMEM;
318a8c2190eSArnaldo Carvalho de Melo 		goto out;
3196e277ed5SThomas Graf 	}
320a8c2190eSArnaldo Carvalho de Melo 
321a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
322d06ca956SEric W. Biederman 			   sk_user_ns(NETLINK_CB(in_skb).ssk),
32315e47304SEric W. Biederman 			   NETLINK_CB(in_skb).portid,
32426932566SPatrick McHardy 			   nlh->nlmsg_seq, 0, nlh);
32526932566SPatrick McHardy 	if (err < 0) {
32626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
3276e277ed5SThomas Graf 		nlmsg_free(rep);
32826932566SPatrick McHardy 		goto out;
32926932566SPatrick McHardy 	}
33015e47304SEric W. Biederman 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
331a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
332a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
333a8c2190eSArnaldo Carvalho de Melo 		err = 0;
334a8c2190eSArnaldo Carvalho de Melo 
335a8c2190eSArnaldo Carvalho de Melo out:
336a8c2190eSArnaldo Carvalho de Melo 	if (sk) {
337a8c2190eSArnaldo Carvalho de Melo 		if (sk->sk_state == TCP_TIME_WAIT)
338a8c2190eSArnaldo Carvalho de Melo 			inet_twsk_put((struct inet_timewait_sock *)sk);
339a8c2190eSArnaldo Carvalho de Melo 		else
340a8c2190eSArnaldo Carvalho de Melo 			sock_put(sk);
341a8c2190eSArnaldo Carvalho de Melo 	}
342476f7dbfSPavel Emelyanov out_nosk:
343476f7dbfSPavel Emelyanov 	return err;
344476f7dbfSPavel Emelyanov }
3451942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
346476f7dbfSPavel Emelyanov 
347476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb,
348476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
349c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *req)
350476f7dbfSPavel Emelyanov {
351476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
352476f7dbfSPavel Emelyanov 	int err;
353476f7dbfSPavel Emelyanov 
354476f7dbfSPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
355476f7dbfSPavel Emelyanov 	if (IS_ERR(handler))
356476f7dbfSPavel Emelyanov 		err = PTR_ERR(handler);
357476f7dbfSPavel Emelyanov 	else
3581942c518SPavel Emelyanov 		err = handler->dump_one(in_skb, nlh, req);
359d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
360476f7dbfSPavel Emelyanov 
361a8c2190eSArnaldo Carvalho de Melo 	return err;
362a8c2190eSArnaldo Carvalho de Melo }
363a8c2190eSArnaldo Carvalho de Melo 
3649f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
365a8c2190eSArnaldo Carvalho de Melo {
366a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
367a8c2190eSArnaldo Carvalho de Melo 
368a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
369a8c2190eSArnaldo Carvalho de Melo 
370a8c2190eSArnaldo Carvalho de Melo 	if (words) {
371a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
372a8c2190eSArnaldo Carvalho de Melo 			return 0;
373a8c2190eSArnaldo Carvalho de Melo 	}
374a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
3759f855299SAl Viro 		__be32 w1, w2;
3769f855299SAl Viro 		__be32 mask;
377a8c2190eSArnaldo Carvalho de Melo 
378a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
379a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
380a8c2190eSArnaldo Carvalho de Melo 
381a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
382a8c2190eSArnaldo Carvalho de Melo 
383a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
384a8c2190eSArnaldo Carvalho de Melo 			return 0;
385a8c2190eSArnaldo Carvalho de Melo 	}
386a8c2190eSArnaldo Carvalho de Melo 
387a8c2190eSArnaldo Carvalho de Melo 	return 1;
388a8c2190eSArnaldo Carvalho de Melo }
389a8c2190eSArnaldo Carvalho de Melo 
390a8c2190eSArnaldo Carvalho de Melo 
39187c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
392a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_entry *entry)
393a8c2190eSArnaldo Carvalho de Melo {
39487c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
39587c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
39687c22ea5SPavel Emelyanov 
397a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
398a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
399a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
400a8c2190eSArnaldo Carvalho de Melo 
401a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
402a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
403a8c2190eSArnaldo Carvalho de Melo 			break;
404a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
405a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
406a8c2190eSArnaldo Carvalho de Melo 			break;
407a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
408a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
409a8c2190eSArnaldo Carvalho de Melo 			break;
410a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
411b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
412a8c2190eSArnaldo Carvalho de Melo 			break;
413a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
414a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
415a8c2190eSArnaldo Carvalho de Melo 			break;
416a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
417a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
418a8c2190eSArnaldo Carvalho de Melo 			break;
419a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
420a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
421a8c2190eSArnaldo Carvalho de Melo 			break;
422a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
423a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
424a8c2190eSArnaldo Carvalho de Melo 			struct inet_diag_hostcond *cond;
4259f855299SAl Viro 			__be32 *addr;
426a8c2190eSArnaldo Carvalho de Melo 
427a8c2190eSArnaldo Carvalho de Melo 			cond = (struct inet_diag_hostcond *)(op + 1);
428a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
429a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
430a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
431a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
432a8c2190eSArnaldo Carvalho de Melo 				break;
433a8c2190eSArnaldo Carvalho de Melo 			}
434a8c2190eSArnaldo Carvalho de Melo 
435a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
436a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
437a8c2190eSArnaldo Carvalho de Melo 			else
438a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
439a8c2190eSArnaldo Carvalho de Melo 
440*f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
441*f67caec9SNeal Cardwell 			    cond->family != entry->family) {
442a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
443a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
444a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
445a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
446*f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
447*f67caec9SNeal Cardwell 							    cond->addr,
448a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
449a8c2190eSArnaldo Carvalho de Melo 						break;
450a8c2190eSArnaldo Carvalho de Melo 				}
451a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
452a8c2190eSArnaldo Carvalho de Melo 				break;
453a8c2190eSArnaldo Carvalho de Melo 			}
454*f67caec9SNeal Cardwell 
455*f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
456*f67caec9SNeal Cardwell 				break;
457*f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
458*f67caec9SNeal Cardwell 					    cond->prefix_len))
459*f67caec9SNeal Cardwell 				break;
460*f67caec9SNeal Cardwell 			yes = 0;
461*f67caec9SNeal Cardwell 			break;
462*f67caec9SNeal Cardwell 		}
463a8c2190eSArnaldo Carvalho de Melo 		}
464a8c2190eSArnaldo Carvalho de Melo 
465a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
466a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
467a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
468a8c2190eSArnaldo Carvalho de Melo 		} else {
469a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
470a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
471a8c2190eSArnaldo Carvalho de Melo 		}
472a8c2190eSArnaldo Carvalho de Melo 	}
473a02cec21SEric Dumazet 	return len == 0;
474a8c2190eSArnaldo Carvalho de Melo }
475a8c2190eSArnaldo Carvalho de Melo 
4768d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
4778d07d151SPavel Emelyanov {
4788d07d151SPavel Emelyanov 	struct inet_diag_entry entry;
4798d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
4808d07d151SPavel Emelyanov 
4818d07d151SPavel Emelyanov 	if (bc == NULL)
4828d07d151SPavel Emelyanov 		return 1;
4838d07d151SPavel Emelyanov 
4848d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
485dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4868d07d151SPavel Emelyanov 	if (entry.family == AF_INET6) {
4878d07d151SPavel Emelyanov 		struct ipv6_pinfo *np = inet6_sk(sk);
4888d07d151SPavel Emelyanov 
4898d07d151SPavel Emelyanov 		entry.saddr = np->rcv_saddr.s6_addr32;
4908d07d151SPavel Emelyanov 		entry.daddr = np->daddr.s6_addr32;
4918d07d151SPavel Emelyanov 	} else
4928d07d151SPavel Emelyanov #endif
4938d07d151SPavel Emelyanov 	{
4948d07d151SPavel Emelyanov 		entry.saddr = &inet->inet_rcv_saddr;
4958d07d151SPavel Emelyanov 		entry.daddr = &inet->inet_daddr;
4968d07d151SPavel Emelyanov 	}
4978d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
4988d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
4998d07d151SPavel Emelyanov 	entry.userlocks = sk->sk_userlocks;
5008d07d151SPavel Emelyanov 
5018d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
5028d07d151SPavel Emelyanov }
5038d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
5048d07d151SPavel Emelyanov 
505a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
506a8c2190eSArnaldo Carvalho de Melo {
507a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
508a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
509a8c2190eSArnaldo Carvalho de Melo 
510a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
511a8c2190eSArnaldo Carvalho de Melo 			return 0;
512a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
513a8c2190eSArnaldo Carvalho de Melo 			return 1;
514eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
515a8c2190eSArnaldo Carvalho de Melo 			return 0;
516a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
517a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
518a8c2190eSArnaldo Carvalho de Melo 	}
519a8c2190eSArnaldo Carvalho de Melo 	return 0;
520a8c2190eSArnaldo Carvalho de Melo }
521a8c2190eSArnaldo Carvalho de Melo 
522405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
523405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
524405c0059SNeal Cardwell 			   int *min_len)
525405c0059SNeal Cardwell {
526405c0059SNeal Cardwell 	int addr_len;
527405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
528405c0059SNeal Cardwell 
529405c0059SNeal Cardwell 	/* Check hostcond space. */
530405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
531405c0059SNeal Cardwell 	if (len < *min_len)
532405c0059SNeal Cardwell 		return false;
533405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
534405c0059SNeal Cardwell 
535405c0059SNeal Cardwell 	/* Check address family and address length. */
536405c0059SNeal Cardwell 	switch (cond->family) {
537405c0059SNeal Cardwell 	case AF_UNSPEC:
538405c0059SNeal Cardwell 		addr_len = 0;
539405c0059SNeal Cardwell 		break;
540405c0059SNeal Cardwell 	case AF_INET:
541405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
542405c0059SNeal Cardwell 		break;
543405c0059SNeal Cardwell 	case AF_INET6:
544405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
545405c0059SNeal Cardwell 		break;
546405c0059SNeal Cardwell 	default:
547405c0059SNeal Cardwell 		return false;
548405c0059SNeal Cardwell 	}
549405c0059SNeal Cardwell 	*min_len += addr_len;
550405c0059SNeal Cardwell 	if (len < *min_len)
551405c0059SNeal Cardwell 		return false;
552405c0059SNeal Cardwell 
553405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
554405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
555405c0059SNeal Cardwell 		return false;
556405c0059SNeal Cardwell 
557405c0059SNeal Cardwell 	return true;
558405c0059SNeal Cardwell }
559405c0059SNeal Cardwell 
560a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
561a8c2190eSArnaldo Carvalho de Melo {
562eeb14972SEric Dumazet 	const void *bc = bytecode;
563a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
564a8c2190eSArnaldo Carvalho de Melo 
565a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
566eeb14972SEric Dumazet 		const struct inet_diag_bc_op *op = bc;
567405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
568a8c2190eSArnaldo Carvalho de Melo 
569a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
570a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
571a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
572a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
573405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
574405c0059SNeal Cardwell 				return -EINVAL;
575405c0059SNeal Cardwell 			/* fall through */
576405c0059SNeal Cardwell 		case INET_DIAG_BC_AUTO:
577a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
578a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
579a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
580a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
581a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
582405c0059SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
583a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
584a8c2190eSArnaldo Carvalho de Melo 			if (op->no < len &&
585a8c2190eSArnaldo Carvalho de Melo 			    !valid_cc(bytecode, bytecode_len, len - op->no))
586a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
587a8c2190eSArnaldo Carvalho de Melo 			break;
588a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
589a8c2190eSArnaldo Carvalho de Melo 			break;
590a8c2190eSArnaldo Carvalho de Melo 		default:
591a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
592a8c2190eSArnaldo Carvalho de Melo 		}
593405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
594eeb14972SEric Dumazet 			return -EINVAL;
595a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
596a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
597a8c2190eSArnaldo Carvalho de Melo 	}
598a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
599a8c2190eSArnaldo Carvalho de Melo }
600a8c2190eSArnaldo Carvalho de Melo 
601dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
602dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
60337f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
604c8991362SPavel Emelyanov 			      struct inet_diag_req_v2 *r,
60537f352b5SPavel Emelyanov 			      const struct nlattr *bc)
606a8c2190eSArnaldo Carvalho de Melo {
6078d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
608a8c2190eSArnaldo Carvalho de Melo 		return 0;
609a8c2190eSArnaldo Carvalho de Melo 
610a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
611d06ca956SEric W. Biederman 				  sk_user_ns(NETLINK_CB(cb->skb).ssk),
61215e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
613a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
614a8c2190eSArnaldo Carvalho de Melo }
615a8c2190eSArnaldo Carvalho de Melo 
616c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
617c7d58aabSArnaldo Carvalho de Melo 			       struct sk_buff *skb,
61837f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
619c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
62037f352b5SPavel Emelyanov 			       const struct nlattr *bc)
621c7d58aabSArnaldo Carvalho de Melo {
62237f352b5SPavel Emelyanov 	if (bc != NULL) {
623c7d58aabSArnaldo Carvalho de Melo 		struct inet_diag_entry entry;
624c7d58aabSArnaldo Carvalho de Melo 
625c7d58aabSArnaldo Carvalho de Melo 		entry.family = tw->tw_family;
626dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
627c7d58aabSArnaldo Carvalho de Melo 		if (tw->tw_family == AF_INET6) {
628c7d58aabSArnaldo Carvalho de Melo 			struct inet6_timewait_sock *tw6 =
629c7d58aabSArnaldo Carvalho de Melo 						inet6_twsk((struct sock *)tw);
630c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
631c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = tw6->tw_v6_daddr.s6_addr32;
632c7d58aabSArnaldo Carvalho de Melo 		} else
633c7d58aabSArnaldo Carvalho de Melo #endif
634c7d58aabSArnaldo Carvalho de Melo 		{
635c7d58aabSArnaldo Carvalho de Melo 			entry.saddr = &tw->tw_rcv_saddr;
636c7d58aabSArnaldo Carvalho de Melo 			entry.daddr = &tw->tw_daddr;
637c7d58aabSArnaldo Carvalho de Melo 		}
638c7d58aabSArnaldo Carvalho de Melo 		entry.sport = tw->tw_num;
639c7d58aabSArnaldo Carvalho de Melo 		entry.dport = ntohs(tw->tw_dport);
640c7d58aabSArnaldo Carvalho de Melo 		entry.userlocks = 0;
641c7d58aabSArnaldo Carvalho de Melo 
64287c22ea5SPavel Emelyanov 		if (!inet_diag_bc_run(bc, &entry))
643c7d58aabSArnaldo Carvalho de Melo 			return 0;
644c7d58aabSArnaldo Carvalho de Melo 	}
645c7d58aabSArnaldo Carvalho de Melo 
646a029fe26SPavel Emelyanov 	return inet_twsk_diag_fill(tw, skb, r,
64715e47304SEric W. Biederman 				   NETLINK_CB(cb->skb).portid,
648c7d58aabSArnaldo Carvalho de Melo 				   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
649c7d58aabSArnaldo Carvalho de Melo }
650c7d58aabSArnaldo Carvalho de Melo 
6511c95df85SNeal Cardwell /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
6521c95df85SNeal Cardwell  * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
6531c95df85SNeal Cardwell  */
6541c95df85SNeal Cardwell static inline void inet_diag_req_addrs(const struct sock *sk,
6551c95df85SNeal Cardwell 				       const struct request_sock *req,
6561c95df85SNeal Cardwell 				       struct inet_diag_entry *entry)
6571c95df85SNeal Cardwell {
6581c95df85SNeal Cardwell 	struct inet_request_sock *ireq = inet_rsk(req);
6591c95df85SNeal Cardwell 
6601c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6)
6611c95df85SNeal Cardwell 	if (sk->sk_family == AF_INET6) {
6621c95df85SNeal Cardwell 		if (req->rsk_ops->family == AF_INET6) {
6631c95df85SNeal Cardwell 			entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32;
6641c95df85SNeal Cardwell 			entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32;
6651c95df85SNeal Cardwell 		} else if (req->rsk_ops->family == AF_INET) {
6661c95df85SNeal Cardwell 			ipv6_addr_set_v4mapped(ireq->loc_addr,
6671c95df85SNeal Cardwell 					       &entry->saddr_storage);
6681c95df85SNeal Cardwell 			ipv6_addr_set_v4mapped(ireq->rmt_addr,
6691c95df85SNeal Cardwell 					       &entry->daddr_storage);
6701c95df85SNeal Cardwell 			entry->saddr = entry->saddr_storage.s6_addr32;
6711c95df85SNeal Cardwell 			entry->daddr = entry->daddr_storage.s6_addr32;
6721c95df85SNeal Cardwell 		}
6731c95df85SNeal Cardwell 	} else
6741c95df85SNeal Cardwell #endif
6751c95df85SNeal Cardwell 	{
6761c95df85SNeal Cardwell 		entry->saddr = &ireq->loc_addr;
6771c95df85SNeal Cardwell 		entry->daddr = &ireq->rmt_addr;
6781c95df85SNeal Cardwell 	}
6791c95df85SNeal Cardwell }
6801c95df85SNeal Cardwell 
681a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
682d06ca956SEric W. Biederman 			      struct request_sock *req,
683d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
68415e47304SEric W. Biederman 			      u32 portid, u32 seq,
685a8c2190eSArnaldo Carvalho de Melo 			      const struct nlmsghdr *unlh)
686a8c2190eSArnaldo Carvalho de Melo {
687a8c2190eSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
688a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
689a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
690a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr *nlh;
691a8c2190eSArnaldo Carvalho de Melo 	long tmo;
692a8c2190eSArnaldo Carvalho de Melo 
69315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
6946e277ed5SThomas Graf 			NLM_F_MULTI);
6956e277ed5SThomas Graf 	if (!nlh)
6966e277ed5SThomas Graf 		return -EMSGSIZE;
697a8c2190eSArnaldo Carvalho de Melo 
6986e277ed5SThomas Graf 	r = nlmsg_data(nlh);
699a8c2190eSArnaldo Carvalho de Melo 	r->idiag_family = sk->sk_family;
700a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = TCP_SYN_RECV;
701a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 1;
702a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = req->retrans;
703a8c2190eSArnaldo Carvalho de Melo 
704a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_if = sk->sk_bound_dev_if;
705f65c1b53SPavel Emelyanov 	sock_diag_save_cookie(req, r->id.idiag_cookie);
706a8c2190eSArnaldo Carvalho de Melo 
707a8c2190eSArnaldo Carvalho de Melo 	tmo = req->expires - jiffies;
708a8c2190eSArnaldo Carvalho de Melo 	if (tmo < 0)
709a8c2190eSArnaldo Carvalho de Melo 		tmo = 0;
710a8c2190eSArnaldo Carvalho de Melo 
711c720c7e8SEric Dumazet 	r->id.idiag_sport = inet->inet_sport;
712a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dport = ireq->rmt_port;
713a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_src[0] = ireq->loc_addr;
714a8c2190eSArnaldo Carvalho de Melo 	r->id.idiag_dst[0] = ireq->rmt_addr;
715a8c2190eSArnaldo Carvalho de Melo 	r->idiag_expires = jiffies_to_msecs(tmo);
716a8c2190eSArnaldo Carvalho de Melo 	r->idiag_rqueue = 0;
717a8c2190eSArnaldo Carvalho de Melo 	r->idiag_wqueue = 0;
718d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
719a8c2190eSArnaldo Carvalho de Melo 	r->idiag_inode = 0;
720dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
721a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
7221c95df85SNeal Cardwell 		struct inet_diag_entry entry;
7231c95df85SNeal Cardwell 		inet_diag_req_addrs(sk, req, &entry);
7241c95df85SNeal Cardwell 		memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
7251c95df85SNeal Cardwell 		memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
726a8c2190eSArnaldo Carvalho de Melo 	}
727a8c2190eSArnaldo Carvalho de Melo #endif
728a8c2190eSArnaldo Carvalho de Melo 
7296e277ed5SThomas Graf 	return nlmsg_end(skb, nlh);
730a8c2190eSArnaldo Carvalho de Melo }
731a8c2190eSArnaldo Carvalho de Melo 
732a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
73337f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
734c8991362SPavel Emelyanov 			       struct inet_diag_req_v2 *r,
73537f352b5SPavel Emelyanov 			       const struct nlattr *bc)
736a8c2190eSArnaldo Carvalho de Melo {
737a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_entry entry;
738a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
739a8c2190eSArnaldo Carvalho de Melo 	struct listen_sock *lopt;
740a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
741a8c2190eSArnaldo Carvalho de Melo 	int j, s_j;
742a8c2190eSArnaldo Carvalho de Melo 	int reqnum, s_reqnum;
743a8c2190eSArnaldo Carvalho de Melo 	int err = 0;
744a8c2190eSArnaldo Carvalho de Melo 
745a8c2190eSArnaldo Carvalho de Melo 	s_j = cb->args[3];
746a8c2190eSArnaldo Carvalho de Melo 	s_reqnum = cb->args[4];
747a8c2190eSArnaldo Carvalho de Melo 
748a8c2190eSArnaldo Carvalho de Melo 	if (s_j > 0)
749a8c2190eSArnaldo Carvalho de Melo 		s_j--;
750a8c2190eSArnaldo Carvalho de Melo 
751a8c2190eSArnaldo Carvalho de Melo 	entry.family = sk->sk_family;
752a8c2190eSArnaldo Carvalho de Melo 
753a8c2190eSArnaldo Carvalho de Melo 	read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
754a8c2190eSArnaldo Carvalho de Melo 
755a8c2190eSArnaldo Carvalho de Melo 	lopt = icsk->icsk_accept_queue.listen_opt;
756a8c2190eSArnaldo Carvalho de Melo 	if (!lopt || !lopt->qlen)
757a8c2190eSArnaldo Carvalho de Melo 		goto out;
758a8c2190eSArnaldo Carvalho de Melo 
75937f352b5SPavel Emelyanov 	if (bc != NULL) {
760c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
761a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
762a8c2190eSArnaldo Carvalho de Melo 	}
763a8c2190eSArnaldo Carvalho de Melo 
764a8c2190eSArnaldo Carvalho de Melo 	for (j = s_j; j < lopt->nr_table_entries; j++) {
765a8c2190eSArnaldo Carvalho de Melo 		struct request_sock *req, *head = lopt->syn_table[j];
766a8c2190eSArnaldo Carvalho de Melo 
767a8c2190eSArnaldo Carvalho de Melo 		reqnum = 0;
768a8c2190eSArnaldo Carvalho de Melo 		for (req = head; req; reqnum++, req = req->dl_next) {
769a8c2190eSArnaldo Carvalho de Melo 			struct inet_request_sock *ireq = inet_rsk(req);
770a8c2190eSArnaldo Carvalho de Melo 
771a8c2190eSArnaldo Carvalho de Melo 			if (reqnum < s_reqnum)
772a8c2190eSArnaldo Carvalho de Melo 				continue;
773a8c2190eSArnaldo Carvalho de Melo 			if (r->id.idiag_dport != ireq->rmt_port &&
774a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_dport)
775a8c2190eSArnaldo Carvalho de Melo 				continue;
776a8c2190eSArnaldo Carvalho de Melo 
777a8c2190eSArnaldo Carvalho de Melo 			if (bc) {
7781c95df85SNeal Cardwell 				inet_diag_req_addrs(sk, req, &entry);
779a8c2190eSArnaldo Carvalho de Melo 				entry.dport = ntohs(ireq->rmt_port);
780a8c2190eSArnaldo Carvalho de Melo 
78187c22ea5SPavel Emelyanov 				if (!inet_diag_bc_run(bc, &entry))
782a8c2190eSArnaldo Carvalho de Melo 					continue;
783a8c2190eSArnaldo Carvalho de Melo 			}
784a8c2190eSArnaldo Carvalho de Melo 
785a8c2190eSArnaldo Carvalho de Melo 			err = inet_diag_fill_req(skb, sk, req,
786d06ca956SEric W. Biederman 					       sk_user_ns(NETLINK_CB(cb->skb).ssk),
78715e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
788a8c2190eSArnaldo Carvalho de Melo 					       cb->nlh->nlmsg_seq, cb->nlh);
789a8c2190eSArnaldo Carvalho de Melo 			if (err < 0) {
790a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = j + 1;
791a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = reqnum;
792a8c2190eSArnaldo Carvalho de Melo 				goto out;
793a8c2190eSArnaldo Carvalho de Melo 			}
794a8c2190eSArnaldo Carvalho de Melo 		}
795a8c2190eSArnaldo Carvalho de Melo 
796a8c2190eSArnaldo Carvalho de Melo 		s_reqnum = 0;
797a8c2190eSArnaldo Carvalho de Melo 	}
798a8c2190eSArnaldo Carvalho de Melo 
799a8c2190eSArnaldo Carvalho de Melo out:
800a8c2190eSArnaldo Carvalho de Melo 	read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
801a8c2190eSArnaldo Carvalho de Melo 
802a8c2190eSArnaldo Carvalho de Melo 	return err;
803a8c2190eSArnaldo Carvalho de Melo }
804a8c2190eSArnaldo Carvalho de Melo 
8051942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
806c8991362SPavel Emelyanov 		struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
807a8c2190eSArnaldo Carvalho de Melo {
808a8c2190eSArnaldo Carvalho de Melo 	int i, num;
809a8c2190eSArnaldo Carvalho de Melo 	int s_i, s_num;
81051d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
811a8c2190eSArnaldo Carvalho de Melo 
812a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
813a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
814a8c2190eSArnaldo Carvalho de Melo 
815a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
816a8c2190eSArnaldo Carvalho de Melo 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
817a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
818a8c2190eSArnaldo Carvalho de Melo 
819a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
820a8c2190eSArnaldo Carvalho de Melo 			struct sock *sk;
821c25eb3bfSEric Dumazet 			struct hlist_nulls_node *node;
8225caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
823a8c2190eSArnaldo Carvalho de Melo 
824a8c2190eSArnaldo Carvalho de Melo 			num = 0;
8255caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
8265caea4eaSEric Dumazet 			spin_lock_bh(&ilb->lock);
827c25eb3bfSEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->head) {
828a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
829a8c2190eSArnaldo Carvalho de Melo 
83051d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
83151d7cccfSAndrey Vagin 					continue;
83251d7cccfSAndrey Vagin 
833a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
834a8c2190eSArnaldo Carvalho de Melo 					num++;
835a8c2190eSArnaldo Carvalho de Melo 					continue;
836a8c2190eSArnaldo Carvalho de Melo 				}
837a8c2190eSArnaldo Carvalho de Melo 
838d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
839d23deaa0SPavel Emelyanov 						sk->sk_family != r->sdiag_family)
840d23deaa0SPavel Emelyanov 					goto next_listen;
841d23deaa0SPavel Emelyanov 
842c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
843a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
844a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
845a8c2190eSArnaldo Carvalho de Melo 
846a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_LISTEN) ||
847a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport ||
848a8c2190eSArnaldo Carvalho de Melo 				    cb->args[3] > 0)
849a8c2190eSArnaldo Carvalho de Melo 					goto syn_recv;
850a8c2190eSArnaldo Carvalho de Melo 
85125c4cd2bSPavel Emelyanov 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
8525caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
853a8c2190eSArnaldo Carvalho de Melo 					goto done;
854a8c2190eSArnaldo Carvalho de Melo 				}
855a8c2190eSArnaldo Carvalho de Melo 
856a8c2190eSArnaldo Carvalho de Melo syn_recv:
857a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_SYN_RECV))
858a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
859a8c2190eSArnaldo Carvalho de Melo 
86025c4cd2bSPavel Emelyanov 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
8615caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
862a8c2190eSArnaldo Carvalho de Melo 					goto done;
863a8c2190eSArnaldo Carvalho de Melo 				}
864a8c2190eSArnaldo Carvalho de Melo 
865a8c2190eSArnaldo Carvalho de Melo next_listen:
866a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = 0;
867a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = 0;
868a8c2190eSArnaldo Carvalho de Melo 				++num;
869a8c2190eSArnaldo Carvalho de Melo 			}
8705caea4eaSEric Dumazet 			spin_unlock_bh(&ilb->lock);
871a8c2190eSArnaldo Carvalho de Melo 
872a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
873a8c2190eSArnaldo Carvalho de Melo 			cb->args[3] = 0;
874a8c2190eSArnaldo Carvalho de Melo 			cb->args[4] = 0;
875a8c2190eSArnaldo Carvalho de Melo 		}
876a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
877a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
878a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
879a8c2190eSArnaldo Carvalho de Melo 	}
880a8c2190eSArnaldo Carvalho de Melo 
881a8c2190eSArnaldo Carvalho de Melo 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
882efb3cb42SPavel Emelyanov 		goto out;
883a8c2190eSArnaldo Carvalho de Melo 
884f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
885a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
8867e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
887a8c2190eSArnaldo Carvalho de Melo 		struct sock *sk;
8883ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
889a8c2190eSArnaldo Carvalho de Melo 
8906be547a6SAndi Kleen 		num = 0;
8916be547a6SAndi Kleen 
8923ab5aee7SEric Dumazet 		if (hlist_nulls_empty(&head->chain) &&
8933ab5aee7SEric Dumazet 			hlist_nulls_empty(&head->twchain))
8946be547a6SAndi Kleen 			continue;
8956be547a6SAndi Kleen 
896a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
897a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
898a8c2190eSArnaldo Carvalho de Melo 
8997e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
9003ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
901a8c2190eSArnaldo Carvalho de Melo 			struct inet_sock *inet = inet_sk(sk);
902a8c2190eSArnaldo Carvalho de Melo 
90351d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
90451d7cccfSAndrey Vagin 				continue;
905a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
906a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
907a8c2190eSArnaldo Carvalho de Melo 			if (!(r->idiag_states & (1 << sk->sk_state)))
908a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
909d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
910d23deaa0SPavel Emelyanov 					sk->sk_family != r->sdiag_family)
911d23deaa0SPavel Emelyanov 				goto next_normal;
912c720c7e8SEric Dumazet 			if (r->id.idiag_sport != inet->inet_sport &&
913a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
914a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
915c720c7e8SEric Dumazet 			if (r->id.idiag_dport != inet->inet_dport &&
9164e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
917a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
91825c4cd2bSPavel Emelyanov 			if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
9197e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
920a8c2190eSArnaldo Carvalho de Melo 				goto done;
921a8c2190eSArnaldo Carvalho de Melo 			}
922a8c2190eSArnaldo Carvalho de Melo next_normal:
923a8c2190eSArnaldo Carvalho de Melo 			++num;
924a8c2190eSArnaldo Carvalho de Melo 		}
925a8c2190eSArnaldo Carvalho de Melo 
926a8c2190eSArnaldo Carvalho de Melo 		if (r->idiag_states & TCPF_TIME_WAIT) {
927c7d58aabSArnaldo Carvalho de Melo 			struct inet_timewait_sock *tw;
928c7d58aabSArnaldo Carvalho de Melo 
929c7d58aabSArnaldo Carvalho de Melo 			inet_twsk_for_each(tw, node,
930dbca9b27SEric Dumazet 				    &head->twchain) {
93151d7cccfSAndrey Vagin 				if (!net_eq(twsk_net(tw), net))
93251d7cccfSAndrey Vagin 					continue;
933a8c2190eSArnaldo Carvalho de Melo 
934a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num)
935a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
936d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
937d23deaa0SPavel Emelyanov 						tw->tw_family != r->sdiag_family)
938d23deaa0SPavel Emelyanov 					goto next_dying;
9397dbf0755SArnaldo Carvalho de Melo 				if (r->id.idiag_sport != tw->tw_sport &&
940a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
941a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
9427dbf0755SArnaldo Carvalho de Melo 				if (r->id.idiag_dport != tw->tw_dport &&
943a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport)
944a8c2190eSArnaldo Carvalho de Melo 					goto next_dying;
94525c4cd2bSPavel Emelyanov 				if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
9467e3aab4aSDavid S. Miller 					spin_unlock_bh(lock);
947a8c2190eSArnaldo Carvalho de Melo 					goto done;
948a8c2190eSArnaldo Carvalho de Melo 				}
949a8c2190eSArnaldo Carvalho de Melo next_dying:
950a8c2190eSArnaldo Carvalho de Melo 				++num;
951a8c2190eSArnaldo Carvalho de Melo 			}
952a8c2190eSArnaldo Carvalho de Melo 		}
9537e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
954a8c2190eSArnaldo Carvalho de Melo 	}
955a8c2190eSArnaldo Carvalho de Melo 
956a8c2190eSArnaldo Carvalho de Melo done:
957a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
958a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
959efb3cb42SPavel Emelyanov out:
960efb3cb42SPavel Emelyanov 	;
961efb3cb42SPavel Emelyanov }
9621942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
963efb3cb42SPavel Emelyanov 
964efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
965c8991362SPavel Emelyanov 		struct inet_diag_req_v2 *r, struct nlattr *bc)
966efb3cb42SPavel Emelyanov {
967efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
968cacb6ba0SCyrill Gorcunov 	int err = 0;
969efb3cb42SPavel Emelyanov 
970efb3cb42SPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
971efb3cb42SPavel Emelyanov 	if (!IS_ERR(handler))
9721942c518SPavel Emelyanov 		handler->dump(skb, cb, r, bc);
973cacb6ba0SCyrill Gorcunov 	else
974cacb6ba0SCyrill Gorcunov 		err = PTR_ERR(handler);
975d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
976efb3cb42SPavel Emelyanov 
977cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
978a8c2190eSArnaldo Carvalho de Melo }
979a8c2190eSArnaldo Carvalho de Melo 
98025c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
98125c4cd2bSPavel Emelyanov {
98225c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
983c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
98425c4cd2bSPavel Emelyanov 
98525c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
98625c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
98725c4cd2bSPavel Emelyanov 
988d106352dSDavid S. Miller 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
98925c4cd2bSPavel Emelyanov }
99025c4cd2bSPavel Emelyanov 
991a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type)
992a029fe26SPavel Emelyanov {
993a029fe26SPavel Emelyanov 	switch (type) {
994a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
995a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
996a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
997a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
998a029fe26SPavel Emelyanov 	default:
999a029fe26SPavel Emelyanov 		return 0;
1000a029fe26SPavel Emelyanov 	}
1001a029fe26SPavel Emelyanov }
1002a029fe26SPavel Emelyanov 
100325c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
100425c4cd2bSPavel Emelyanov {
1005d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1006c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
100725c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
10083b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
100925c4cd2bSPavel Emelyanov 
1010d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
101125c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
101225c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
101325c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
101425c4cd2bSPavel Emelyanov 	req.id = rc->id;
101525c4cd2bSPavel Emelyanov 
101625c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
101725c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
101825c4cd2bSPavel Emelyanov 
101925c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
102025c4cd2bSPavel Emelyanov }
102125c4cd2bSPavel Emelyanov 
1022fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1023fe50ce28SPavel Emelyanov 			       const struct nlmsghdr *nlh)
1024fe50ce28SPavel Emelyanov {
1025d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1026c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1027fe50ce28SPavel Emelyanov 
1028fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1029fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1030fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1031fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1032fe50ce28SPavel Emelyanov 	req.id = rc->id;
1033fe50ce28SPavel Emelyanov 
1034fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
1035fe50ce28SPavel Emelyanov }
1036fe50ce28SPavel Emelyanov 
10378d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1038a8c2190eSArnaldo Carvalho de Melo {
10393b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
104051d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1041a8c2190eSArnaldo Carvalho de Melo 
1042ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1043ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1044ead592baSThomas Graf 		return -EINVAL;
1045a8c2190eSArnaldo Carvalho de Melo 
1046b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1047ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1048ead592baSThomas Graf 			struct nlattr *attr;
1049ead592baSThomas Graf 
1050ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1051ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1052ead592baSThomas Graf 			if (attr == NULL ||
1053ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1054ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1055a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
1056a8c2190eSArnaldo Carvalho de Melo 		}
105780d326faSPablo Neira Ayuso 		{
105880d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
105980d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
106080d326faSPablo Neira Ayuso 			};
106151d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
106280d326faSPablo Neira Ayuso 		}
1063a8c2190eSArnaldo Carvalho de Melo 	}
1064ead592baSThomas Graf 
1065fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1066a8c2190eSArnaldo Carvalho de Melo }
1067a8c2190eSArnaldo Carvalho de Melo 
1068d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1069d366477aSPavel Emelyanov {
1070c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
107151d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1072d366477aSPavel Emelyanov 
1073d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1074d366477aSPavel Emelyanov 		return -EINVAL;
1075d366477aSPavel Emelyanov 
1076d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
107725c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
107825c4cd2bSPavel Emelyanov 			struct nlattr *attr;
107925c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
108025c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
108125c4cd2bSPavel Emelyanov 			if (attr == NULL ||
108225c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
108325c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
108425c4cd2bSPavel Emelyanov 				return -EINVAL;
108525c4cd2bSPavel Emelyanov 		}
108680d326faSPablo Neira Ayuso 		{
108780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
108880d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
108980d326faSPablo Neira Ayuso 			};
109051d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
109180d326faSPablo Neira Ayuso 		}
1092d366477aSPavel Emelyanov 	}
1093d366477aSPavel Emelyanov 
1094d106352dSDavid S. Miller 	return inet_diag_get_exact(skb, h, nlmsg_data(h));
1095d366477aSPavel Emelyanov }
1096d366477aSPavel Emelyanov 
10978dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1098d366477aSPavel Emelyanov 	.family = AF_INET,
1099d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1100d366477aSPavel Emelyanov };
1101d366477aSPavel Emelyanov 
11028dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1103d366477aSPavel Emelyanov 	.family = AF_INET6,
1104d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1105d366477aSPavel Emelyanov };
1106d366477aSPavel Emelyanov 
1107a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1108a8c2190eSArnaldo Carvalho de Melo {
1109a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1110a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1111a8c2190eSArnaldo Carvalho de Melo 
1112f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1113a8c2190eSArnaldo Carvalho de Melo 		goto out;
1114a8c2190eSArnaldo Carvalho de Melo 
1115d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1116a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1117a8c2190eSArnaldo Carvalho de Melo 	if (inet_diag_table[type] == NULL) {
1118a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1119a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1120a8c2190eSArnaldo Carvalho de Melo 	}
1121d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1122a8c2190eSArnaldo Carvalho de Melo out:
1123a8c2190eSArnaldo Carvalho de Melo 	return err;
1124a8c2190eSArnaldo Carvalho de Melo }
1125a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1126a8c2190eSArnaldo Carvalho de Melo 
1127a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1128a8c2190eSArnaldo Carvalho de Melo {
1129a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1130a8c2190eSArnaldo Carvalho de Melo 
1131f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1132a8c2190eSArnaldo Carvalho de Melo 		return;
1133a8c2190eSArnaldo Carvalho de Melo 
1134d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1135a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1136d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1137a8c2190eSArnaldo Carvalho de Melo }
1138a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1139a8c2190eSArnaldo Carvalho de Melo 
1140a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1141a8c2190eSArnaldo Carvalho de Melo {
1142f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1143a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1144a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1145a8c2190eSArnaldo Carvalho de Melo 
11460da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1147a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1148a8c2190eSArnaldo Carvalho de Melo 		goto out;
1149a8c2190eSArnaldo Carvalho de Melo 
1150d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1151d366477aSPavel Emelyanov 	if (err)
1152d366477aSPavel Emelyanov 		goto out_free_nl;
1153d366477aSPavel Emelyanov 
1154d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1155d366477aSPavel Emelyanov 	if (err)
1156d366477aSPavel Emelyanov 		goto out_free_inet;
1157d366477aSPavel Emelyanov 
11588ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1159a8c2190eSArnaldo Carvalho de Melo out:
1160a8c2190eSArnaldo Carvalho de Melo 	return err;
1161d366477aSPavel Emelyanov 
1162d366477aSPavel Emelyanov out_free_inet:
1163d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1164d366477aSPavel Emelyanov out_free_nl:
1165a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1166a8c2190eSArnaldo Carvalho de Melo 	goto out;
1167a8c2190eSArnaldo Carvalho de Melo }
1168a8c2190eSArnaldo Carvalho de Melo 
1169a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1170a8c2190eSArnaldo Carvalho de Melo {
1171d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1172d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
11738ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1174a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1175a8c2190eSArnaldo Carvalho de Melo }
1176a8c2190eSArnaldo Carvalho de Melo 
1177a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1178a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1179a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1180aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1181aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1182