xref: /linux/net/ipv4/inet_diag.c (revision 64f40ff5bbdb1b679fb3c4dbc8230d6517d2b8dc)
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;
47a8c2190eSArnaldo Carvalho de Melo };
48a8c2190eSArnaldo Carvalho de Melo 
49d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
50d523a328SHerbert Xu 
51f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
52d523a328SHerbert Xu {
53f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
54aec8dc62SPavel Emelyanov 		request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
55aec8dc62SPavel Emelyanov 			       NETLINK_SOCK_DIAG, AF_INET, proto);
56d523a328SHerbert Xu 
57d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
58f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
59d523a328SHerbert Xu 		return ERR_PTR(-ENOENT);
60d523a328SHerbert Xu 
61f13c95f0SPavel Emelyanov 	return inet_diag_table[proto];
62d523a328SHerbert Xu }
63d523a328SHerbert Xu 
64e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
65d523a328SHerbert Xu {
66d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
67d523a328SHerbert Xu }
68d523a328SHerbert Xu 
69a4458343SEric Dumazet static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
70a4458343SEric Dumazet {
71a4458343SEric Dumazet 	r->idiag_family = sk->sk_family;
72a4458343SEric Dumazet 
73a4458343SEric Dumazet 	r->id.idiag_sport = htons(sk->sk_num);
74a4458343SEric Dumazet 	r->id.idiag_dport = sk->sk_dport;
75a4458343SEric Dumazet 	r->id.idiag_if = sk->sk_bound_dev_if;
76a4458343SEric Dumazet 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
77a4458343SEric Dumazet 
78a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
79a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
80a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
81a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
82a4458343SEric Dumazet 	} else
83a4458343SEric Dumazet #endif
84a4458343SEric Dumazet 	{
85a4458343SEric Dumazet 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
86a4458343SEric Dumazet 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
87a4458343SEric Dumazet 
88a4458343SEric Dumazet 	r->id.idiag_src[0] = sk->sk_rcv_saddr;
89a4458343SEric Dumazet 	r->id.idiag_dst[0] = sk->sk_daddr;
90a4458343SEric Dumazet 	}
91a4458343SEric Dumazet }
92a4458343SEric Dumazet 
93c8e2c80dSEric Dumazet static size_t inet_sk_attr_size(void)
94c8e2c80dSEric Dumazet {
95c8e2c80dSEric Dumazet 	return	  nla_total_size(sizeof(struct tcp_info))
96c8e2c80dSEric Dumazet 		+ nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97c8e2c80dSEric Dumazet 		+ nla_total_size(1) /* INET_DIAG_TOS */
98c8e2c80dSEric Dumazet 		+ nla_total_size(1) /* INET_DIAG_TCLASS */
99c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct inet_diag_meminfo))
100c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct inet_diag_msg))
101c8e2c80dSEric Dumazet 		+ nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
102c8e2c80dSEric Dumazet 		+ nla_total_size(TCP_CA_NAME_MAX)
103c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct tcpvegas_info))
104c8e2c80dSEric Dumazet 		+ 64;
105c8e2c80dSEric Dumazet }
106c8e2c80dSEric Dumazet 
1073c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
10834160ea3SEric Dumazet 		      struct sk_buff *skb, const struct inet_diag_req_v2 *req,
109d06ca956SEric W. Biederman 		      struct user_namespace *user_ns,
11015e47304SEric W. Biederman 		      u32 portid, u32 seq, u16 nlmsg_flags,
111a8c2190eSArnaldo Carvalho de Melo 		      const struct nlmsghdr *unlh)
112a8c2190eSArnaldo Carvalho de Melo {
113a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
114521f1cf1SEric Dumazet 	const struct tcp_congestion_ops *ca_ops;
115e31c5e0eSEric Dumazet 	const struct inet_diag_handler *handler;
116e31c5e0eSEric Dumazet 	int ext = req->idiag_ext;
117a8c2190eSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
118a8c2190eSArnaldo Carvalho de Melo 	struct nlmsghdr  *nlh;
1196e277ed5SThomas Graf 	struct nlattr *attr;
120a8c2190eSArnaldo Carvalho de Melo 	void *info = NULL;
121a8c2190eSArnaldo Carvalho de Melo 
122a029fe26SPavel Emelyanov 	handler = inet_diag_table[req->sdiag_protocol];
123e31c5e0eSEric Dumazet 	BUG_ON(!handler);
124a8c2190eSArnaldo Carvalho de Melo 
12515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
1266e277ed5SThomas Graf 			nlmsg_flags);
1276e277ed5SThomas Graf 	if (!nlh)
128d106352dSDavid S. Miller 		return -EMSGSIZE;
129a8c2190eSArnaldo Carvalho de Melo 
130d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
131a58917f5SEric Dumazet 	BUG_ON(!sk_fullsock(sk));
132c7d58aabSArnaldo Carvalho de Melo 
133a4458343SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
134a8c2190eSArnaldo Carvalho de Melo 	r->idiag_state = sk->sk_state;
135a8c2190eSArnaldo Carvalho de Melo 	r->idiag_timer = 0;
136a8c2190eSArnaldo Carvalho de Melo 	r->idiag_retrans = 0;
137a8c2190eSArnaldo Carvalho de Melo 
138e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
139e4e541a8SPavel Emelyanov 		goto errout;
140e4e541a8SPavel Emelyanov 
141717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
142717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
143717b6d83SMaciej Żenczykowski 	 */
144717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1456e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1466e277ed5SThomas Graf 			goto errout;
147717b6d83SMaciej Żenczykowski 
148dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
149a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
15006236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
151efe4208fSEric Dumazet 			if (nla_put_u8(skb, INET_DIAG_TCLASS,
152efe4208fSEric Dumazet 				       inet6_sk(sk)->tclass) < 0)
1536e277ed5SThomas Graf 				goto errout;
154a8c2190eSArnaldo Carvalho de Melo 	}
155a8c2190eSArnaldo Carvalho de Melo #endif
156a8c2190eSArnaldo Carvalho de Melo 
157d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1583c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1593c4d05c8SPavel Emelyanov 
1606e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
1616e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
1626e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
1636e277ed5SThomas Graf 			.idiag_wmem = sk->sk_wmem_queued,
1646e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
1656e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
1666e277ed5SThomas Graf 		};
1676e277ed5SThomas Graf 
1686e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
1696e277ed5SThomas Graf 			goto errout;
1703c4d05c8SPavel Emelyanov 	}
1713c4d05c8SPavel Emelyanov 
172c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
173c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
1746e277ed5SThomas Graf 			goto errout;
175c0636faaSPavel Emelyanov 
176e31c5e0eSEric Dumazet 	if (!icsk) {
17762ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
1783c4d05c8SPavel Emelyanov 		goto out;
1793c4d05c8SPavel Emelyanov 	}
1803c4d05c8SPavel Emelyanov 
181172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo)  DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
182a8c2190eSArnaldo Carvalho de Melo 
1836ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
1846ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
1856ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
186a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
187a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
188a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
189a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
190a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
191a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
192a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
193a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
194a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
195a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
196a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
197a8c2190eSArnaldo Carvalho de Melo 	} else {
198a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
199a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
200a8c2190eSArnaldo Carvalho de Melo 	}
201a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS
202a8c2190eSArnaldo Carvalho de Melo 
2036e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_INFO - 1))) {
2046e277ed5SThomas Graf 		attr = nla_reserve(skb, INET_DIAG_INFO,
2056e277ed5SThomas Graf 				   sizeof(struct tcp_info));
2066e277ed5SThomas Graf 		if (!attr)
2076e277ed5SThomas Graf 			goto errout;
208a8c2190eSArnaldo Carvalho de Melo 
2096e277ed5SThomas Graf 		info = nla_data(attr);
210a8c2190eSArnaldo Carvalho de Melo 	}
211a8c2190eSArnaldo Carvalho de Melo 
212521f1cf1SEric Dumazet 	if (ext & (1 << (INET_DIAG_CONG - 1))) {
213521f1cf1SEric Dumazet 		int err = 0;
214521f1cf1SEric Dumazet 
215521f1cf1SEric Dumazet 		rcu_read_lock();
216521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
217521f1cf1SEric Dumazet 		if (ca_ops)
218521f1cf1SEric Dumazet 			err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
219521f1cf1SEric Dumazet 		rcu_read_unlock();
220521f1cf1SEric Dumazet 		if (err < 0)
2216e277ed5SThomas Graf 			goto errout;
222521f1cf1SEric Dumazet 	}
2236e277ed5SThomas Graf 
224a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
225a8c2190eSArnaldo Carvalho de Melo 
226521f1cf1SEric Dumazet 	if (sk->sk_state < TCP_TIME_WAIT) {
227*64f40ff5SEric Dumazet 		union tcp_cc_info info;
228*64f40ff5SEric Dumazet 		size_t sz = 0;
229*64f40ff5SEric Dumazet 		int attr;
230521f1cf1SEric Dumazet 
231521f1cf1SEric Dumazet 		rcu_read_lock();
232521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
233521f1cf1SEric Dumazet 		if (ca_ops && ca_ops->get_info)
234*64f40ff5SEric Dumazet 			sz = ca_ops->get_info(sk, ext, &attr, &info);
235521f1cf1SEric Dumazet 		rcu_read_unlock();
236*64f40ff5SEric Dumazet 		if (sz && nla_put(skb, attr, sz, &info) < 0)
237521f1cf1SEric Dumazet 			goto errout;
238521f1cf1SEric Dumazet 	}
239a8c2190eSArnaldo Carvalho de Melo 
2403c4d05c8SPavel Emelyanov out:
241053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
242053c095aSJohannes Berg 	return 0;
243a8c2190eSArnaldo Carvalho de Melo 
2446e277ed5SThomas Graf errout:
2456e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
24626932566SPatrick McHardy 	return -EMSGSIZE;
247a8c2190eSArnaldo Carvalho de Melo }
2483c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
2493c4d05c8SPavel Emelyanov 
2503c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
251e31c5e0eSEric Dumazet 			      struct sk_buff *skb,
25234160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *req,
253d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
25415e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
2553c4d05c8SPavel Emelyanov 			      const struct nlmsghdr *unlh)
2563c4d05c8SPavel Emelyanov {
257e31c5e0eSEric Dumazet 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
258e31c5e0eSEric Dumazet 				 user_ns, portid, seq, nlmsg_flags, unlh);
2593c4d05c8SPavel Emelyanov }
260a8c2190eSArnaldo Carvalho de Melo 
26133cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk,
262e31c5e0eSEric Dumazet 			       struct sk_buff *skb,
26315e47304SEric W. Biederman 			       u32 portid, u32 seq, u16 nlmsg_flags,
264c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
265c7d58aabSArnaldo Carvalho de Melo {
26633cf7c90SEric Dumazet 	struct inet_timewait_sock *tw = inet_twsk(sk);
267c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
2686e277ed5SThomas Graf 	struct nlmsghdr *nlh;
269789f558cSEric Dumazet 	long tmo;
270c7d58aabSArnaldo Carvalho de Melo 
27115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
2726e277ed5SThomas Graf 			nlmsg_flags);
2736e277ed5SThomas Graf 	if (!nlh)
274d106352dSDavid S. Miller 		return -EMSGSIZE;
275d106352dSDavid S. Miller 
276d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
277c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
278c7d58aabSArnaldo Carvalho de Melo 
279789f558cSEric Dumazet 	tmo = tw->tw_timer.expires - jiffies;
280c7d58aabSArnaldo Carvalho de Melo 	if (tmo < 0)
281c7d58aabSArnaldo Carvalho de Melo 		tmo = 0;
282c7d58aabSArnaldo Carvalho de Melo 
283a4458343SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
284c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
285b1aac815SDaniel Borkmann 
286c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
287c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
28896f817feSEric Dumazet 	r->idiag_expires      = jiffies_to_msecs(tmo);
289c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
290c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
291c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
292c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
2936e277ed5SThomas Graf 
294053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
295053c095aSJohannes Berg 	return 0;
296c7d58aabSArnaldo Carvalho de Melo }
297c7d58aabSArnaldo Carvalho de Melo 
298a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
299a58917f5SEric Dumazet 			      u32 portid, u32 seq, u16 nlmsg_flags,
300a58917f5SEric Dumazet 			      const struct nlmsghdr *unlh)
301a58917f5SEric Dumazet {
302a58917f5SEric Dumazet 	struct inet_diag_msg *r;
303a58917f5SEric Dumazet 	struct nlmsghdr *nlh;
304a58917f5SEric Dumazet 	long tmo;
305a58917f5SEric Dumazet 
306a58917f5SEric Dumazet 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
307a58917f5SEric Dumazet 			nlmsg_flags);
308a58917f5SEric Dumazet 	if (!nlh)
309a58917f5SEric Dumazet 		return -EMSGSIZE;
310a58917f5SEric Dumazet 
311a58917f5SEric Dumazet 	r = nlmsg_data(nlh);
312a58917f5SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
313a58917f5SEric Dumazet 	r->idiag_state = TCP_SYN_RECV;
314a58917f5SEric Dumazet 	r->idiag_timer = 1;
315a58917f5SEric Dumazet 	r->idiag_retrans = inet_reqsk(sk)->num_retrans;
316a58917f5SEric Dumazet 
317a58917f5SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
318a58917f5SEric Dumazet 		     offsetof(struct sock, sk_cookie));
319a58917f5SEric Dumazet 
320fa76ce73SEric Dumazet 	tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
321a58917f5SEric Dumazet 	r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
322a58917f5SEric Dumazet 	r->idiag_rqueue	= 0;
323a58917f5SEric Dumazet 	r->idiag_wqueue	= 0;
324a58917f5SEric Dumazet 	r->idiag_uid	= 0;
325a58917f5SEric Dumazet 	r->idiag_inode	= 0;
326a58917f5SEric Dumazet 
327a58917f5SEric Dumazet 	nlmsg_end(skb, nlh);
328a58917f5SEric Dumazet 	return 0;
329a58917f5SEric Dumazet }
330a58917f5SEric Dumazet 
331dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
33234160ea3SEric Dumazet 			const struct inet_diag_req_v2 *r,
333d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
33415e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
335dff2c035SArnaldo Carvalho de Melo 			const struct nlmsghdr *unlh)
336dff2c035SArnaldo Carvalho de Melo {
337dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
338a58917f5SEric Dumazet 		return inet_twsk_diag_fill(sk, skb, portid, seq,
339a58917f5SEric Dumazet 					   nlmsg_flags, unlh);
340a58917f5SEric Dumazet 
341a58917f5SEric Dumazet 	if (sk->sk_state == TCP_NEW_SYN_RECV)
342a58917f5SEric Dumazet 		return inet_req_diag_fill(sk, skb, portid, seq,
343efe4208fSEric Dumazet 					  nlmsg_flags, unlh);
344efe4208fSEric Dumazet 
345efe4208fSEric Dumazet 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
346efe4208fSEric Dumazet 				  nlmsg_flags, unlh);
347dff2c035SArnaldo Carvalho de Melo }
348dff2c035SArnaldo Carvalho de Melo 
349e31c5e0eSEric Dumazet int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
350e31c5e0eSEric Dumazet 			    struct sk_buff *in_skb,
351e31c5e0eSEric Dumazet 			    const struct nlmsghdr *nlh,
35234160ea3SEric Dumazet 			    const struct inet_diag_req_v2 *req)
353a8c2190eSArnaldo Carvalho de Melo {
35451d7cccfSAndrey Vagin 	struct net *net = sock_net(in_skb->sk);
355e31c5e0eSEric Dumazet 	struct sk_buff *rep;
356e31c5e0eSEric Dumazet 	struct sock *sk;
357e31c5e0eSEric Dumazet 	int err;
358a8c2190eSArnaldo Carvalho de Melo 
359d523a328SHerbert Xu 	err = -EINVAL;
360e31c5e0eSEric Dumazet 	if (req->sdiag_family == AF_INET)
36151d7cccfSAndrey Vagin 		sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
362a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
363a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
364dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
365e31c5e0eSEric Dumazet 	else if (req->sdiag_family == AF_INET6)
36651d7cccfSAndrey Vagin 		sk = inet6_lookup(net, hashinfo,
367a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_dst,
368a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_dport,
369a8c2190eSArnaldo Carvalho de Melo 				  (struct in6_addr *)req->id.idiag_src,
370a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_sport,
371a8c2190eSArnaldo Carvalho de Melo 				  req->id.idiag_if);
372a8c2190eSArnaldo Carvalho de Melo #endif
373e31c5e0eSEric Dumazet 	else
374476f7dbfSPavel Emelyanov 		goto out_nosk;
375a8c2190eSArnaldo Carvalho de Melo 
376d523a328SHerbert Xu 	err = -ENOENT;
377e31c5e0eSEric Dumazet 	if (!sk)
378476f7dbfSPavel Emelyanov 		goto out_nosk;
379a8c2190eSArnaldo Carvalho de Melo 
380f65c1b53SPavel Emelyanov 	err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
381b005ab4eSPavel Emelyanov 	if (err)
382a8c2190eSArnaldo Carvalho de Melo 		goto out;
383a8c2190eSArnaldo Carvalho de Melo 
384c8e2c80dSEric Dumazet 	rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
3856e277ed5SThomas Graf 	if (!rep) {
3866e277ed5SThomas Graf 		err = -ENOMEM;
387a8c2190eSArnaldo Carvalho de Melo 		goto out;
3886e277ed5SThomas Graf 	}
389a8c2190eSArnaldo Carvalho de Melo 
390a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
391e32123e5SPatrick McHardy 			   sk_user_ns(NETLINK_CB(in_skb).sk),
39215e47304SEric W. Biederman 			   NETLINK_CB(in_skb).portid,
39326932566SPatrick McHardy 			   nlh->nlmsg_seq, 0, nlh);
39426932566SPatrick McHardy 	if (err < 0) {
39526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
3966e277ed5SThomas Graf 		nlmsg_free(rep);
39726932566SPatrick McHardy 		goto out;
39826932566SPatrick McHardy 	}
39915e47304SEric W. Biederman 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
400a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
401a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
402a8c2190eSArnaldo Carvalho de Melo 		err = 0;
403a8c2190eSArnaldo Carvalho de Melo 
404a8c2190eSArnaldo Carvalho de Melo out:
405c1d607ccSEric Dumazet 	if (sk)
406c1d607ccSEric Dumazet 		sock_gen_put(sk);
407c1d607ccSEric Dumazet 
408476f7dbfSPavel Emelyanov out_nosk:
409476f7dbfSPavel Emelyanov 	return err;
410476f7dbfSPavel Emelyanov }
4111942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
412476f7dbfSPavel Emelyanov 
413476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb,
414476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
41534160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *req)
416476f7dbfSPavel Emelyanov {
417476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
418476f7dbfSPavel Emelyanov 	int err;
419476f7dbfSPavel Emelyanov 
420476f7dbfSPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
421476f7dbfSPavel Emelyanov 	if (IS_ERR(handler))
422476f7dbfSPavel Emelyanov 		err = PTR_ERR(handler);
423476f7dbfSPavel Emelyanov 	else
4241942c518SPavel Emelyanov 		err = handler->dump_one(in_skb, nlh, req);
425d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
426476f7dbfSPavel Emelyanov 
427a8c2190eSArnaldo Carvalho de Melo 	return err;
428a8c2190eSArnaldo Carvalho de Melo }
429a8c2190eSArnaldo Carvalho de Melo 
4309f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
431a8c2190eSArnaldo Carvalho de Melo {
432a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
433a8c2190eSArnaldo Carvalho de Melo 
434a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
435a8c2190eSArnaldo Carvalho de Melo 
436a8c2190eSArnaldo Carvalho de Melo 	if (words) {
437a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
438a8c2190eSArnaldo Carvalho de Melo 			return 0;
439a8c2190eSArnaldo Carvalho de Melo 	}
440a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
4419f855299SAl Viro 		__be32 w1, w2;
4429f855299SAl Viro 		__be32 mask;
443a8c2190eSArnaldo Carvalho de Melo 
444a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
445a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
446a8c2190eSArnaldo Carvalho de Melo 
447a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
448a8c2190eSArnaldo Carvalho de Melo 
449a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
450a8c2190eSArnaldo Carvalho de Melo 			return 0;
451a8c2190eSArnaldo Carvalho de Melo 	}
452a8c2190eSArnaldo Carvalho de Melo 
453a8c2190eSArnaldo Carvalho de Melo 	return 1;
454a8c2190eSArnaldo Carvalho de Melo }
455a8c2190eSArnaldo Carvalho de Melo 
45687c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
457a8c2190eSArnaldo Carvalho de Melo 			    const struct inet_diag_entry *entry)
458a8c2190eSArnaldo Carvalho de Melo {
45987c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
46087c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
46187c22ea5SPavel Emelyanov 
462a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
463a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
464a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
465a8c2190eSArnaldo Carvalho de Melo 
466a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
467a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
468a8c2190eSArnaldo Carvalho de Melo 			break;
469a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
470a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
471a8c2190eSArnaldo Carvalho de Melo 			break;
472a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
473a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
474a8c2190eSArnaldo Carvalho de Melo 			break;
475a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
476b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
477a8c2190eSArnaldo Carvalho de Melo 			break;
478a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
479a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
480a8c2190eSArnaldo Carvalho de Melo 			break;
481a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
482a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
483a8c2190eSArnaldo Carvalho de Melo 			break;
484a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
485a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
486a8c2190eSArnaldo Carvalho de Melo 			break;
487a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
488a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
489e31c5e0eSEric Dumazet 			const struct inet_diag_hostcond *cond;
490e31c5e0eSEric Dumazet 			const __be32 *addr;
491a8c2190eSArnaldo Carvalho de Melo 
492e31c5e0eSEric Dumazet 			cond = (const struct inet_diag_hostcond *)(op + 1);
493a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
494a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
495a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
496a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
497a8c2190eSArnaldo Carvalho de Melo 				break;
498a8c2190eSArnaldo Carvalho de Melo 			}
499a8c2190eSArnaldo Carvalho de Melo 
500a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
501a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
502a8c2190eSArnaldo Carvalho de Melo 			else
503a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
504a8c2190eSArnaldo Carvalho de Melo 
505f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
506f67caec9SNeal Cardwell 			    cond->family != entry->family) {
507a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
508a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
509a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
510a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
511f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
512f67caec9SNeal Cardwell 							    cond->addr,
513a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
514a8c2190eSArnaldo Carvalho de Melo 						break;
515a8c2190eSArnaldo Carvalho de Melo 				}
516a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
517a8c2190eSArnaldo Carvalho de Melo 				break;
518a8c2190eSArnaldo Carvalho de Melo 			}
519f67caec9SNeal Cardwell 
520f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
521f67caec9SNeal Cardwell 				break;
522f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
523f67caec9SNeal Cardwell 					    cond->prefix_len))
524f67caec9SNeal Cardwell 				break;
525f67caec9SNeal Cardwell 			yes = 0;
526f67caec9SNeal Cardwell 			break;
527f67caec9SNeal Cardwell 		}
528a8c2190eSArnaldo Carvalho de Melo 		}
529a8c2190eSArnaldo Carvalho de Melo 
530a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
531a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
532a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
533a8c2190eSArnaldo Carvalho de Melo 		} else {
534a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
535a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
536a8c2190eSArnaldo Carvalho de Melo 		}
537a8c2190eSArnaldo Carvalho de Melo 	}
538a02cec21SEric Dumazet 	return len == 0;
539a8c2190eSArnaldo Carvalho de Melo }
540a8c2190eSArnaldo Carvalho de Melo 
541a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
542a4458343SEric Dumazet  */
543a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry,
544a4458343SEric Dumazet 			     const struct sock *sk)
545a4458343SEric Dumazet {
546a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
547a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
548a4458343SEric Dumazet 		entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
549a4458343SEric Dumazet 		entry->daddr = sk->sk_v6_daddr.s6_addr32;
550a4458343SEric Dumazet 	} else
551a4458343SEric Dumazet #endif
552a4458343SEric Dumazet 	{
553a4458343SEric Dumazet 		entry->saddr = &sk->sk_rcv_saddr;
554a4458343SEric Dumazet 		entry->daddr = &sk->sk_daddr;
555a4458343SEric Dumazet 	}
556a4458343SEric Dumazet }
557a4458343SEric Dumazet 
5588d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
5598d07d151SPavel Emelyanov {
5608d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
561e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
5628d07d151SPavel Emelyanov 
563e31c5e0eSEric Dumazet 	if (!bc)
5648d07d151SPavel Emelyanov 		return 1;
5658d07d151SPavel Emelyanov 
5668d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
567a4458343SEric Dumazet 	entry_fill_addrs(&entry, sk);
5688d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
5698d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
570a58917f5SEric Dumazet 	entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
5718d07d151SPavel Emelyanov 
5728d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
5738d07d151SPavel Emelyanov }
5748d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
5758d07d151SPavel Emelyanov 
576a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
577a8c2190eSArnaldo Carvalho de Melo {
578a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
579a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
580a8c2190eSArnaldo Carvalho de Melo 
581a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
582a8c2190eSArnaldo Carvalho de Melo 			return 0;
583a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
584a8c2190eSArnaldo Carvalho de Melo 			return 1;
585eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
586a8c2190eSArnaldo Carvalho de Melo 			return 0;
587a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
588a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
589a8c2190eSArnaldo Carvalho de Melo 	}
590a8c2190eSArnaldo Carvalho de Melo 	return 0;
591a8c2190eSArnaldo Carvalho de Melo }
592a8c2190eSArnaldo Carvalho de Melo 
593405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
594405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
595405c0059SNeal Cardwell 			   int *min_len)
596405c0059SNeal Cardwell {
597405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
598e31c5e0eSEric Dumazet 	int addr_len;
599405c0059SNeal Cardwell 
600405c0059SNeal Cardwell 	/* Check hostcond space. */
601405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
602405c0059SNeal Cardwell 	if (len < *min_len)
603405c0059SNeal Cardwell 		return false;
604405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
605405c0059SNeal Cardwell 
606405c0059SNeal Cardwell 	/* Check address family and address length. */
607405c0059SNeal Cardwell 	switch (cond->family) {
608405c0059SNeal Cardwell 	case AF_UNSPEC:
609405c0059SNeal Cardwell 		addr_len = 0;
610405c0059SNeal Cardwell 		break;
611405c0059SNeal Cardwell 	case AF_INET:
612405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
613405c0059SNeal Cardwell 		break;
614405c0059SNeal Cardwell 	case AF_INET6:
615405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
616405c0059SNeal Cardwell 		break;
617405c0059SNeal Cardwell 	default:
618405c0059SNeal Cardwell 		return false;
619405c0059SNeal Cardwell 	}
620405c0059SNeal Cardwell 	*min_len += addr_len;
621405c0059SNeal Cardwell 	if (len < *min_len)
622405c0059SNeal Cardwell 		return false;
623405c0059SNeal Cardwell 
624405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
625405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
626405c0059SNeal Cardwell 		return false;
627405c0059SNeal Cardwell 
628405c0059SNeal Cardwell 	return true;
629405c0059SNeal Cardwell }
630405c0059SNeal Cardwell 
6315e1f5420SNeal Cardwell /* Validate a port comparison operator. */
632e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op,
6335e1f5420SNeal Cardwell 				  int len, int *min_len)
6345e1f5420SNeal Cardwell {
6355e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
6365e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
6375e1f5420SNeal Cardwell 	if (len < *min_len)
6385e1f5420SNeal Cardwell 		return false;
6395e1f5420SNeal Cardwell 	return true;
6405e1f5420SNeal Cardwell }
6415e1f5420SNeal Cardwell 
642a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
643a8c2190eSArnaldo Carvalho de Melo {
644eeb14972SEric Dumazet 	const void *bc = bytecode;
645a8c2190eSArnaldo Carvalho de Melo 	int  len = bytecode_len;
646a8c2190eSArnaldo Carvalho de Melo 
647a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
648405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
649e31c5e0eSEric Dumazet 		const struct inet_diag_bc_op *op = bc;
650a8c2190eSArnaldo Carvalho de Melo 
651a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
652a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
653a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
654405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
655405c0059SNeal Cardwell 				return -EINVAL;
6565e1f5420SNeal Cardwell 			break;
657a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
658a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
659a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
660a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
6615e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
662a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
663a8c2190eSArnaldo Carvalho de Melo 			break;
6645e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
6655e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
666a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
667a8c2190eSArnaldo Carvalho de Melo 			break;
668a8c2190eSArnaldo Carvalho de Melo 		default:
669a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
670a8c2190eSArnaldo Carvalho de Melo 		}
6715e1f5420SNeal Cardwell 
6725e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
6735e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
6745e1f5420SNeal Cardwell 				return -EINVAL;
6755e1f5420SNeal Cardwell 			if (op->no < len &&
6765e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
6775e1f5420SNeal Cardwell 				return -EINVAL;
6785e1f5420SNeal Cardwell 		}
6795e1f5420SNeal Cardwell 
680405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
681eeb14972SEric Dumazet 			return -EINVAL;
682a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
683a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
684a8c2190eSArnaldo Carvalho de Melo 	}
685a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
686a8c2190eSArnaldo Carvalho de Melo }
687a8c2190eSArnaldo Carvalho de Melo 
688dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
689dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
69037f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
69134160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *r,
69237f352b5SPavel Emelyanov 			      const struct nlattr *bc)
693a8c2190eSArnaldo Carvalho de Melo {
6948d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
695a8c2190eSArnaldo Carvalho de Melo 		return 0;
696a8c2190eSArnaldo Carvalho de Melo 
697a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
698e32123e5SPatrick McHardy 				  sk_user_ns(NETLINK_CB(cb->skb).sk),
69915e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
700a8c2190eSArnaldo Carvalho de Melo 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
701a8c2190eSArnaldo Carvalho de Melo }
702a8c2190eSArnaldo Carvalho de Melo 
70349612729SEric Dumazet static void twsk_build_assert(void)
70449612729SEric Dumazet {
70549612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
70649612729SEric Dumazet 		     offsetof(struct sock, sk_family));
70749612729SEric Dumazet 
70849612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
70949612729SEric Dumazet 		     offsetof(struct inet_sock, inet_num));
71049612729SEric Dumazet 
71149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
71249612729SEric Dumazet 		     offsetof(struct inet_sock, inet_dport));
71349612729SEric Dumazet 
71449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
71549612729SEric Dumazet 		     offsetof(struct inet_sock, inet_rcv_saddr));
71649612729SEric Dumazet 
71749612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
71849612729SEric Dumazet 		     offsetof(struct inet_sock, inet_daddr));
71949612729SEric Dumazet 
72049612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
72149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
72249612729SEric Dumazet 		     offsetof(struct sock, sk_v6_rcv_saddr));
72349612729SEric Dumazet 
72449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
72549612729SEric Dumazet 		     offsetof(struct sock, sk_v6_daddr));
72649612729SEric Dumazet #endif
72749612729SEric Dumazet }
72849612729SEric Dumazet 
729a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
73037f352b5SPavel Emelyanov 			       struct netlink_callback *cb,
73134160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *r,
73237f352b5SPavel Emelyanov 			       const struct nlattr *bc)
733a8c2190eSArnaldo Carvalho de Melo {
734a8c2190eSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
735a8c2190eSArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
736e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
737e31c5e0eSEric Dumazet 	int j, s_j, reqnum, s_reqnum;
738e31c5e0eSEric Dumazet 	struct listen_sock *lopt;
739a8c2190eSArnaldo Carvalho de Melo 	int err = 0;
740a8c2190eSArnaldo Carvalho de Melo 
741a8c2190eSArnaldo Carvalho de Melo 	s_j = cb->args[3];
742a8c2190eSArnaldo Carvalho de Melo 	s_reqnum = cb->args[4];
743a8c2190eSArnaldo Carvalho de Melo 
744a8c2190eSArnaldo Carvalho de Melo 	if (s_j > 0)
745a8c2190eSArnaldo Carvalho de Melo 		s_j--;
746a8c2190eSArnaldo Carvalho de Melo 
747a8c2190eSArnaldo Carvalho de Melo 	entry.family = sk->sk_family;
748a8c2190eSArnaldo Carvalho de Melo 
749b2827053SEric Dumazet 	spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
750a8c2190eSArnaldo Carvalho de Melo 
751a8c2190eSArnaldo Carvalho de Melo 	lopt = icsk->icsk_accept_queue.listen_opt;
752fa76ce73SEric Dumazet 	if (!lopt || !listen_sock_qlen(lopt))
753a8c2190eSArnaldo Carvalho de Melo 		goto out;
754a8c2190eSArnaldo Carvalho de Melo 
755e31c5e0eSEric Dumazet 	if (bc) {
756c720c7e8SEric Dumazet 		entry.sport = inet->inet_num;
757a8c2190eSArnaldo Carvalho de Melo 		entry.userlocks = sk->sk_userlocks;
758a8c2190eSArnaldo Carvalho de Melo 	}
759a8c2190eSArnaldo Carvalho de Melo 
760a8c2190eSArnaldo Carvalho de Melo 	for (j = s_j; j < lopt->nr_table_entries; j++) {
761a8c2190eSArnaldo Carvalho de Melo 		struct request_sock *req, *head = lopt->syn_table[j];
762a8c2190eSArnaldo Carvalho de Melo 
763a8c2190eSArnaldo Carvalho de Melo 		reqnum = 0;
764a8c2190eSArnaldo Carvalho de Melo 		for (req = head; req; reqnum++, req = req->dl_next) {
765a8c2190eSArnaldo Carvalho de Melo 			struct inet_request_sock *ireq = inet_rsk(req);
766a8c2190eSArnaldo Carvalho de Melo 
767a8c2190eSArnaldo Carvalho de Melo 			if (reqnum < s_reqnum)
768a8c2190eSArnaldo Carvalho de Melo 				continue;
769634fb979SEric Dumazet 			if (r->id.idiag_dport != ireq->ir_rmt_port &&
770a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_dport)
771a8c2190eSArnaldo Carvalho de Melo 				continue;
772a8c2190eSArnaldo Carvalho de Melo 
773a8c2190eSArnaldo Carvalho de Melo 			if (bc) {
774a4458343SEric Dumazet 				/* Note: entry.sport and entry.userlocks are already set */
77508d2cc3bSEric Dumazet 				entry_fill_addrs(&entry, req_to_sk(req));
776634fb979SEric Dumazet 				entry.dport = ntohs(ireq->ir_rmt_port);
777a8c2190eSArnaldo Carvalho de Melo 
77887c22ea5SPavel Emelyanov 				if (!inet_diag_bc_run(bc, &entry))
779a8c2190eSArnaldo Carvalho de Melo 					continue;
780a8c2190eSArnaldo Carvalho de Melo 			}
781a8c2190eSArnaldo Carvalho de Melo 
78208d2cc3bSEric Dumazet 			err = inet_req_diag_fill(req_to_sk(req), skb,
78315e47304SEric W. Biederman 						 NETLINK_CB(cb->skb).portid,
784a58917f5SEric Dumazet 						 cb->nlh->nlmsg_seq,
785a58917f5SEric Dumazet 						 NLM_F_MULTI, cb->nlh);
786a8c2190eSArnaldo Carvalho de Melo 			if (err < 0) {
787a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = j + 1;
788a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = reqnum;
789a8c2190eSArnaldo Carvalho de Melo 				goto out;
790a8c2190eSArnaldo Carvalho de Melo 			}
791a8c2190eSArnaldo Carvalho de Melo 		}
792a8c2190eSArnaldo Carvalho de Melo 
793a8c2190eSArnaldo Carvalho de Melo 		s_reqnum = 0;
794a8c2190eSArnaldo Carvalho de Melo 	}
795a8c2190eSArnaldo Carvalho de Melo 
796a8c2190eSArnaldo Carvalho de Melo out:
797b2827053SEric Dumazet 	spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
798a8c2190eSArnaldo Carvalho de Melo 
799a8c2190eSArnaldo Carvalho de Melo 	return err;
800a8c2190eSArnaldo Carvalho de Melo }
801a8c2190eSArnaldo Carvalho de Melo 
8021942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
803e31c5e0eSEric Dumazet 			 struct netlink_callback *cb,
80434160ea3SEric Dumazet 			 const struct inet_diag_req_v2 *r, struct nlattr *bc)
805a8c2190eSArnaldo Carvalho de Melo {
80651d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
807e31c5e0eSEric Dumazet 	int i, num, s_i, s_num;
808a8c2190eSArnaldo Carvalho de Melo 
809a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
810a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
811a8c2190eSArnaldo Carvalho de Melo 
812a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
813a8c2190eSArnaldo Carvalho de Melo 		if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
814a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
815a8c2190eSArnaldo Carvalho de Melo 
816a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
8175caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
818e31c5e0eSEric Dumazet 			struct hlist_nulls_node *node;
819e31c5e0eSEric Dumazet 			struct sock *sk;
820a8c2190eSArnaldo Carvalho de Melo 
821a8c2190eSArnaldo Carvalho de Melo 			num = 0;
8225caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
8235caea4eaSEric Dumazet 			spin_lock_bh(&ilb->lock);
824c25eb3bfSEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->head) {
825a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
826a8c2190eSArnaldo Carvalho de Melo 
82751d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
82851d7cccfSAndrey Vagin 					continue;
82951d7cccfSAndrey Vagin 
830a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
831a8c2190eSArnaldo Carvalho de Melo 					num++;
832a8c2190eSArnaldo Carvalho de Melo 					continue;
833a8c2190eSArnaldo Carvalho de Melo 				}
834a8c2190eSArnaldo Carvalho de Melo 
835d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
836d23deaa0SPavel Emelyanov 				    sk->sk_family != r->sdiag_family)
837d23deaa0SPavel Emelyanov 					goto next_listen;
838d23deaa0SPavel Emelyanov 
839c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
840a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
841a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
842a8c2190eSArnaldo Carvalho de Melo 
843a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_LISTEN) ||
844a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_dport ||
845a8c2190eSArnaldo Carvalho de Melo 				    cb->args[3] > 0)
846a8c2190eSArnaldo Carvalho de Melo 					goto syn_recv;
847a8c2190eSArnaldo Carvalho de Melo 
84825c4cd2bSPavel Emelyanov 				if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
8495caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
850a8c2190eSArnaldo Carvalho de Melo 					goto done;
851a8c2190eSArnaldo Carvalho de Melo 				}
852a8c2190eSArnaldo Carvalho de Melo 
853a8c2190eSArnaldo Carvalho de Melo syn_recv:
854a8c2190eSArnaldo Carvalho de Melo 				if (!(r->idiag_states & TCPF_SYN_RECV))
855a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
856a8c2190eSArnaldo Carvalho de Melo 
85725c4cd2bSPavel Emelyanov 				if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
8585caea4eaSEric Dumazet 					spin_unlock_bh(&ilb->lock);
859a8c2190eSArnaldo Carvalho de Melo 					goto done;
860a8c2190eSArnaldo Carvalho de Melo 				}
861a8c2190eSArnaldo Carvalho de Melo 
862a8c2190eSArnaldo Carvalho de Melo next_listen:
863a8c2190eSArnaldo Carvalho de Melo 				cb->args[3] = 0;
864a8c2190eSArnaldo Carvalho de Melo 				cb->args[4] = 0;
865a8c2190eSArnaldo Carvalho de Melo 				++num;
866a8c2190eSArnaldo Carvalho de Melo 			}
8675caea4eaSEric Dumazet 			spin_unlock_bh(&ilb->lock);
868a8c2190eSArnaldo Carvalho de Melo 
869a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
870a8c2190eSArnaldo Carvalho de Melo 			cb->args[3] = 0;
871a8c2190eSArnaldo Carvalho de Melo 			cb->args[4] = 0;
872a8c2190eSArnaldo Carvalho de Melo 		}
873a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
874a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
875a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
876a8c2190eSArnaldo Carvalho de Melo 	}
877a8c2190eSArnaldo Carvalho de Melo 
878a8c2190eSArnaldo Carvalho de Melo 	if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
879efb3cb42SPavel Emelyanov 		goto out;
880a8c2190eSArnaldo Carvalho de Melo 
881f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
882a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
8837e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
8843ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
885e31c5e0eSEric Dumazet 		struct sock *sk;
886a8c2190eSArnaldo Carvalho de Melo 
8876be547a6SAndi Kleen 		num = 0;
8886be547a6SAndi Kleen 
88905dbc7b5SEric Dumazet 		if (hlist_nulls_empty(&head->chain))
8906be547a6SAndi Kleen 			continue;
8916be547a6SAndi Kleen 
892a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
893a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
894a8c2190eSArnaldo Carvalho de Melo 
8957e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
8963ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
897e31c5e0eSEric Dumazet 			int state, res;
898a8c2190eSArnaldo Carvalho de Melo 
89951d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
90051d7cccfSAndrey Vagin 				continue;
901a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
902a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
90370315d22SNeal Cardwell 			state = (sk->sk_state == TCP_TIME_WAIT) ?
90470315d22SNeal Cardwell 				inet_twsk(sk)->tw_substate : sk->sk_state;
90570315d22SNeal Cardwell 			if (!(r->idiag_states & (1 << state)))
906a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
907d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
908d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
909d23deaa0SPavel Emelyanov 				goto next_normal;
91005dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
911a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
912a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
91305dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
9144e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
915a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
916a58917f5SEric Dumazet 			twsk_build_assert();
917a58917f5SEric Dumazet 
918a58917f5SEric Dumazet 			if (!inet_diag_bc_sk(bc, sk))
919a58917f5SEric Dumazet 				goto next_normal;
920a58917f5SEric Dumazet 
921a58917f5SEric Dumazet 			res = sk_diag_fill(sk, skb, r,
922a58917f5SEric Dumazet 					   sk_user_ns(NETLINK_CB(cb->skb).sk),
923a58917f5SEric Dumazet 					   NETLINK_CB(cb->skb).portid,
924a58917f5SEric Dumazet 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
925a58917f5SEric Dumazet 					   cb->nlh);
92605dbc7b5SEric Dumazet 			if (res < 0) {
9277e3aab4aSDavid S. Miller 				spin_unlock_bh(lock);
928a8c2190eSArnaldo Carvalho de Melo 				goto done;
929a8c2190eSArnaldo Carvalho de Melo 			}
930a8c2190eSArnaldo Carvalho de Melo next_normal:
931a8c2190eSArnaldo Carvalho de Melo 			++num;
932a8c2190eSArnaldo Carvalho de Melo 		}
933a8c2190eSArnaldo Carvalho de Melo 
9347e3aab4aSDavid S. Miller 		spin_unlock_bh(lock);
935a8c2190eSArnaldo Carvalho de Melo 	}
936a8c2190eSArnaldo Carvalho de Melo 
937a8c2190eSArnaldo Carvalho de Melo done:
938a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
939a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
940efb3cb42SPavel Emelyanov out:
941efb3cb42SPavel Emelyanov 	;
942efb3cb42SPavel Emelyanov }
9431942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
944efb3cb42SPavel Emelyanov 
945efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
94634160ea3SEric Dumazet 			    const struct inet_diag_req_v2 *r,
947e31c5e0eSEric Dumazet 			    struct nlattr *bc)
948efb3cb42SPavel Emelyanov {
949efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
950cacb6ba0SCyrill Gorcunov 	int err = 0;
951efb3cb42SPavel Emelyanov 
952efb3cb42SPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
953efb3cb42SPavel Emelyanov 	if (!IS_ERR(handler))
9541942c518SPavel Emelyanov 		handler->dump(skb, cb, r, bc);
955cacb6ba0SCyrill Gorcunov 	else
956cacb6ba0SCyrill Gorcunov 		err = PTR_ERR(handler);
957d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
958efb3cb42SPavel Emelyanov 
959cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
960a8c2190eSArnaldo Carvalho de Melo }
961a8c2190eSArnaldo Carvalho de Melo 
96225c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
96325c4cd2bSPavel Emelyanov {
964c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
965e31c5e0eSEric Dumazet 	struct nlattr *bc = NULL;
96625c4cd2bSPavel Emelyanov 
96725c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
96825c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
96925c4cd2bSPavel Emelyanov 
970d106352dSDavid S. Miller 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
97125c4cd2bSPavel Emelyanov }
97225c4cd2bSPavel Emelyanov 
973e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type)
974a029fe26SPavel Emelyanov {
975a029fe26SPavel Emelyanov 	switch (type) {
976a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
977a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
978a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
979a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
980a029fe26SPavel Emelyanov 	default:
981a029fe26SPavel Emelyanov 		return 0;
982a029fe26SPavel Emelyanov 	}
983a029fe26SPavel Emelyanov }
984a029fe26SPavel Emelyanov 
985e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb,
986e31c5e0eSEric Dumazet 				 struct netlink_callback *cb)
98725c4cd2bSPavel Emelyanov {
988d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
989e31c5e0eSEric Dumazet 	int hdrlen = sizeof(struct inet_diag_req);
990c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
99125c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
99225c4cd2bSPavel Emelyanov 
993d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
99425c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
99525c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
99625c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
99725c4cd2bSPavel Emelyanov 	req.id = rc->id;
99825c4cd2bSPavel Emelyanov 
99925c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
100025c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
100125c4cd2bSPavel Emelyanov 
100225c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
100325c4cd2bSPavel Emelyanov }
100425c4cd2bSPavel Emelyanov 
1005fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1006fe50ce28SPavel Emelyanov 				      const struct nlmsghdr *nlh)
1007fe50ce28SPavel Emelyanov {
1008d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1009c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1010fe50ce28SPavel Emelyanov 
1011fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1012fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1013fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1014fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1015fe50ce28SPavel Emelyanov 	req.id = rc->id;
1016fe50ce28SPavel Emelyanov 
1017fe50ce28SPavel Emelyanov 	return inet_diag_get_exact(in_skb, nlh, &req);
1018fe50ce28SPavel Emelyanov }
1019fe50ce28SPavel Emelyanov 
10208d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1021a8c2190eSArnaldo Carvalho de Melo {
10223b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
102351d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1024a8c2190eSArnaldo Carvalho de Melo 
1025ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1026ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1027ead592baSThomas Graf 		return -EINVAL;
1028a8c2190eSArnaldo Carvalho de Melo 
1029b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1030ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1031ead592baSThomas Graf 			struct nlattr *attr;
1032ead592baSThomas Graf 
1033ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1034ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1035e31c5e0eSEric Dumazet 			if (!attr ||
1036ead592baSThomas Graf 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1037ead592baSThomas Graf 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1038a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
1039a8c2190eSArnaldo Carvalho de Melo 		}
104080d326faSPablo Neira Ayuso 		{
104180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
104280d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
104380d326faSPablo Neira Ayuso 			};
104451d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
104580d326faSPablo Neira Ayuso 		}
1046a8c2190eSArnaldo Carvalho de Melo 	}
1047ead592baSThomas Graf 
1048fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1049a8c2190eSArnaldo Carvalho de Melo }
1050a8c2190eSArnaldo Carvalho de Melo 
1051d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1052d366477aSPavel Emelyanov {
1053c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
105451d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1055d366477aSPavel Emelyanov 
1056d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1057d366477aSPavel Emelyanov 		return -EINVAL;
1058d366477aSPavel Emelyanov 
1059d366477aSPavel Emelyanov 	if (h->nlmsg_flags & NLM_F_DUMP) {
106025c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
106125c4cd2bSPavel Emelyanov 			struct nlattr *attr;
1062e31c5e0eSEric Dumazet 
106325c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
106425c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
1065e31c5e0eSEric Dumazet 			if (!attr ||
106625c4cd2bSPavel Emelyanov 			    nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
106725c4cd2bSPavel Emelyanov 			    inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
106825c4cd2bSPavel Emelyanov 				return -EINVAL;
106925c4cd2bSPavel Emelyanov 		}
107080d326faSPablo Neira Ayuso 		{
107180d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
107280d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
107380d326faSPablo Neira Ayuso 			};
107451d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
107580d326faSPablo Neira Ayuso 		}
1076d366477aSPavel Emelyanov 	}
1077d366477aSPavel Emelyanov 
1078d106352dSDavid S. Miller 	return inet_diag_get_exact(skb, h, nlmsg_data(h));
1079d366477aSPavel Emelyanov }
1080d366477aSPavel Emelyanov 
10818dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1082d366477aSPavel Emelyanov 	.family = AF_INET,
1083d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1084d366477aSPavel Emelyanov };
1085d366477aSPavel Emelyanov 
10868dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1087d366477aSPavel Emelyanov 	.family = AF_INET6,
1088d366477aSPavel Emelyanov 	.dump = inet_diag_handler_dump,
1089d366477aSPavel Emelyanov };
1090d366477aSPavel Emelyanov 
1091a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1092a8c2190eSArnaldo Carvalho de Melo {
1093a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1094a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1095a8c2190eSArnaldo Carvalho de Melo 
1096f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1097a8c2190eSArnaldo Carvalho de Melo 		goto out;
1098a8c2190eSArnaldo Carvalho de Melo 
1099d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1100a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1101e31c5e0eSEric Dumazet 	if (!inet_diag_table[type]) {
1102a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1103a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1104a8c2190eSArnaldo Carvalho de Melo 	}
1105d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1106a8c2190eSArnaldo Carvalho de Melo out:
1107a8c2190eSArnaldo Carvalho de Melo 	return err;
1108a8c2190eSArnaldo Carvalho de Melo }
1109a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1110a8c2190eSArnaldo Carvalho de Melo 
1111a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1112a8c2190eSArnaldo Carvalho de Melo {
1113a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1114a8c2190eSArnaldo Carvalho de Melo 
1115f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1116a8c2190eSArnaldo Carvalho de Melo 		return;
1117a8c2190eSArnaldo Carvalho de Melo 
1118d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1119a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1120d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1121a8c2190eSArnaldo Carvalho de Melo }
1122a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1123a8c2190eSArnaldo Carvalho de Melo 
1124a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1125a8c2190eSArnaldo Carvalho de Melo {
1126f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1127a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1128a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1129a8c2190eSArnaldo Carvalho de Melo 
11300da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1131a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1132a8c2190eSArnaldo Carvalho de Melo 		goto out;
1133a8c2190eSArnaldo Carvalho de Melo 
1134d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1135d366477aSPavel Emelyanov 	if (err)
1136d366477aSPavel Emelyanov 		goto out_free_nl;
1137d366477aSPavel Emelyanov 
1138d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1139d366477aSPavel Emelyanov 	if (err)
1140d366477aSPavel Emelyanov 		goto out_free_inet;
1141d366477aSPavel Emelyanov 
11428ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1143a8c2190eSArnaldo Carvalho de Melo out:
1144a8c2190eSArnaldo Carvalho de Melo 	return err;
1145d366477aSPavel Emelyanov 
1146d366477aSPavel Emelyanov out_free_inet:
1147d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1148d366477aSPavel Emelyanov out_free_nl:
1149a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1150a8c2190eSArnaldo Carvalho de Melo 	goto out;
1151a8c2190eSArnaldo Carvalho de Melo }
1152a8c2190eSArnaldo Carvalho de Melo 
1153a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1154a8c2190eSArnaldo Carvalho de Melo {
1155d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1156d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
11578ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1158a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1159a8c2190eSArnaldo Carvalho de Melo }
1160a8c2190eSArnaldo Carvalho de Melo 
1161a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1162a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1163a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1164aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1165aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1166