xref: /openbmc/linux/net/ipv4/inet_diag.c (revision 83f73c5bb7b9a9135173f0ba2b1aa00c06664ff9)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a8c2190eSArnaldo Carvalho de Melo /*
3a8c2190eSArnaldo Carvalho de Melo  * inet_diag.c	Module for monitoring INET transport protocols sockets.
4a8c2190eSArnaldo Carvalho de Melo  *
5a8c2190eSArnaldo Carvalho de Melo  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6a8c2190eSArnaldo Carvalho de Melo  */
7a8c2190eSArnaldo Carvalho de Melo 
8172589ccSIlpo Järvinen #include <linux/kernel.h>
9a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h>
10a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h>
11a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h>
12a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h>
15a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h>
16a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h>
17a8c2190eSArnaldo Carvalho de Melo 
18a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h>
19a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h>
20a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h>
21a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h>
22a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
23a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
24a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
25a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h>
26dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
27a8c2190eSArnaldo Carvalho de Melo 
28a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h>
29a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h>
30a8c2190eSArnaldo Carvalho de Melo 
31a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h>
32d366477aSPavel Emelyanov #include <linux/sock_diag.h>
33a8c2190eSArnaldo Carvalho de Melo 
34a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table;
35a8c2190eSArnaldo Carvalho de Melo 
36a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry {
37e31c5e0eSEric Dumazet 	const __be32 *saddr;
38e31c5e0eSEric Dumazet 	const __be32 *daddr;
39a8c2190eSArnaldo Carvalho de Melo 	u16 sport;
40a8c2190eSArnaldo Carvalho de Melo 	u16 dport;
41a8c2190eSArnaldo Carvalho de Melo 	u16 family;
42a8c2190eSArnaldo Carvalho de Melo 	u16 userlocks;
43637c841dSDavid Ahern 	u32 ifindex;
44a52e95abSLorenzo Colitti 	u32 mark;
45a8c2190eSArnaldo Carvalho de Melo };
46a8c2190eSArnaldo Carvalho de Melo 
47d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
48d523a328SHerbert Xu 
49f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
50d523a328SHerbert Xu {
51f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
52bf2ae2e4SXin Long 		sock_load_diag_module(AF_INET, proto);
53d523a328SHerbert Xu 
54d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
55f13c95f0SPavel Emelyanov 	if (!inet_diag_table[proto])
56d523a328SHerbert Xu 		return ERR_PTR(-ENOENT);
57d523a328SHerbert Xu 
58f13c95f0SPavel Emelyanov 	return inet_diag_table[proto];
59d523a328SHerbert Xu }
60d523a328SHerbert Xu 
61e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
62d523a328SHerbert Xu {
63d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
64d523a328SHerbert Xu }
65d523a328SHerbert Xu 
66cb2050a7SXin Long void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
67a4458343SEric Dumazet {
68a4458343SEric Dumazet 	r->idiag_family = sk->sk_family;
69a4458343SEric Dumazet 
70a4458343SEric Dumazet 	r->id.idiag_sport = htons(sk->sk_num);
71a4458343SEric Dumazet 	r->id.idiag_dport = sk->sk_dport;
72a4458343SEric Dumazet 	r->id.idiag_if = sk->sk_bound_dev_if;
73a4458343SEric Dumazet 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
74a4458343SEric Dumazet 
75a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
76a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
77a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
78a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
79a4458343SEric Dumazet 	} else
80a4458343SEric Dumazet #endif
81a4458343SEric Dumazet 	{
82a4458343SEric Dumazet 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
83a4458343SEric Dumazet 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
84a4458343SEric Dumazet 
85a4458343SEric Dumazet 	r->id.idiag_src[0] = sk->sk_rcv_saddr;
86a4458343SEric Dumazet 	r->id.idiag_dst[0] = sk->sk_daddr;
87a4458343SEric Dumazet 	}
88a4458343SEric Dumazet }
89cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
90a4458343SEric Dumazet 
91b37e8840SIvan Delalande static size_t inet_sk_attr_size(struct sock *sk,
92b37e8840SIvan Delalande 				const struct inet_diag_req_v2 *req,
93b37e8840SIvan Delalande 				bool net_admin)
94c8e2c80dSEric Dumazet {
95b37e8840SIvan Delalande 	const struct inet_diag_handler *handler;
96b37e8840SIvan Delalande 	size_t aux = 0;
97b37e8840SIvan Delalande 
98b37e8840SIvan Delalande 	handler = inet_diag_table[req->sdiag_protocol];
99b37e8840SIvan Delalande 	if (handler && handler->idiag_get_aux_size)
100b37e8840SIvan Delalande 		aux = handler->idiag_get_aux_size(sk, net_admin);
101b37e8840SIvan Delalande 
102c8e2c80dSEric Dumazet 	return	  nla_total_size(sizeof(struct tcp_info))
103c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct inet_diag_msg))
104*83f73c5bSDmitry Yakunin 		+ inet_diag_msg_attrs_size()
105*83f73c5bSDmitry Yakunin 		+ nla_total_size(sizeof(struct inet_diag_meminfo))
106c8e2c80dSEric Dumazet 		+ nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
107c8e2c80dSEric Dumazet 		+ nla_total_size(TCP_CA_NAME_MAX)
108c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct tcpvegas_info))
109b37e8840SIvan Delalande 		+ aux
110c8e2c80dSEric Dumazet 		+ 64;
111c8e2c80dSEric Dumazet }
112c8e2c80dSEric Dumazet 
113cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
114cb2050a7SXin Long 			     struct inet_diag_msg *r, int ext,
115d545cacaSLorenzo Colitti 			     struct user_namespace *user_ns,
116d545cacaSLorenzo Colitti 			     bool net_admin)
117a8c2190eSArnaldo Carvalho de Melo {
118a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
119a8c2190eSArnaldo Carvalho de Melo 
120e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
121e4e541a8SPavel Emelyanov 		goto errout;
122e4e541a8SPavel Emelyanov 
123717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
124717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
125717b6d83SMaciej Żenczykowski 	 */
126717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
1276e277ed5SThomas Graf 		if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1286e277ed5SThomas Graf 			goto errout;
129717b6d83SMaciej Żenczykowski 
130dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
131a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
13206236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
133efe4208fSEric Dumazet 			if (nla_put_u8(skb, INET_DIAG_TCLASS,
134efe4208fSEric Dumazet 				       inet6_sk(sk)->tclass) < 0)
1356e277ed5SThomas Graf 				goto errout;
13620462155SPhil Sutter 
1378220ea23SPhil Sutter 		if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
1388220ea23SPhil Sutter 		    nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
13920462155SPhil Sutter 			goto errout;
140a8c2190eSArnaldo Carvalho de Melo 	}
141a8c2190eSArnaldo Carvalho de Melo #endif
142a8c2190eSArnaldo Carvalho de Melo 
143d545cacaSLorenzo Colitti 	if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
144d545cacaSLorenzo Colitti 		goto errout;
145d545cacaSLorenzo Colitti 
146*83f73c5bSDmitry Yakunin 	if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
147*83f73c5bSDmitry Yakunin 	    ext & (1 << (INET_DIAG_TCLASS - 1))) {
148*83f73c5bSDmitry Yakunin 		u32 classid = 0;
149*83f73c5bSDmitry Yakunin 
150*83f73c5bSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
151*83f73c5bSDmitry Yakunin 		classid = sock_cgroup_classid(&sk->sk_cgrp_data);
152*83f73c5bSDmitry Yakunin #endif
153*83f73c5bSDmitry Yakunin 		/* Fallback to socket priority if class id isn't set.
154*83f73c5bSDmitry Yakunin 		 * Classful qdiscs use it as direct reference to class.
155*83f73c5bSDmitry Yakunin 		 * For cgroup2 classid is always zero.
156*83f73c5bSDmitry Yakunin 		 */
157*83f73c5bSDmitry Yakunin 		if (!classid)
158*83f73c5bSDmitry Yakunin 			classid = sk->sk_priority;
159*83f73c5bSDmitry Yakunin 
160*83f73c5bSDmitry Yakunin 		if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
161*83f73c5bSDmitry Yakunin 			goto errout;
162*83f73c5bSDmitry Yakunin 	}
163*83f73c5bSDmitry Yakunin 
164d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1653c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1663c4d05c8SPavel Emelyanov 
167cb2050a7SXin Long 	return 0;
168cb2050a7SXin Long errout:
169cb2050a7SXin Long 	return 1;
170cb2050a7SXin Long }
171cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
172cb2050a7SXin Long 
173cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
174cb2050a7SXin Long 		      struct sk_buff *skb, const struct inet_diag_req_v2 *req,
175cb2050a7SXin Long 		      struct user_namespace *user_ns,
176cb2050a7SXin Long 		      u32 portid, u32 seq, u16 nlmsg_flags,
177d545cacaSLorenzo Colitti 		      const struct nlmsghdr *unlh,
178d545cacaSLorenzo Colitti 		      bool net_admin)
179cb2050a7SXin Long {
180cb2050a7SXin Long 	const struct tcp_congestion_ops *ca_ops;
181cb2050a7SXin Long 	const struct inet_diag_handler *handler;
182cb2050a7SXin Long 	int ext = req->idiag_ext;
183cb2050a7SXin Long 	struct inet_diag_msg *r;
184cb2050a7SXin Long 	struct nlmsghdr  *nlh;
185cb2050a7SXin Long 	struct nlattr *attr;
186cb2050a7SXin Long 	void *info = NULL;
187cb2050a7SXin Long 
188cb2050a7SXin Long 	handler = inet_diag_table[req->sdiag_protocol];
189cb2050a7SXin Long 	BUG_ON(!handler);
190cb2050a7SXin Long 
191cb2050a7SXin Long 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
192cb2050a7SXin Long 			nlmsg_flags);
193cb2050a7SXin Long 	if (!nlh)
194cb2050a7SXin Long 		return -EMSGSIZE;
195cb2050a7SXin Long 
196cb2050a7SXin Long 	r = nlmsg_data(nlh);
197cb2050a7SXin Long 	BUG_ON(!sk_fullsock(sk));
198cb2050a7SXin Long 
199cb2050a7SXin Long 	inet_diag_msg_common_fill(r, sk);
200cb2050a7SXin Long 	r->idiag_state = sk->sk_state;
201cb2050a7SXin Long 	r->idiag_timer = 0;
202cb2050a7SXin Long 	r->idiag_retrans = 0;
203cb2050a7SXin Long 
204d545cacaSLorenzo Colitti 	if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
205cb2050a7SXin Long 		goto errout;
206cb2050a7SXin Long 
2076e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
2086e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
2096e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
210ab4e846aSEric Dumazet 			.idiag_wmem = READ_ONCE(sk->sk_wmem_queued),
2116e277ed5SThomas Graf 			.idiag_fmem = sk->sk_forward_alloc,
2126e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
2136e277ed5SThomas Graf 		};
2146e277ed5SThomas Graf 
2156e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
2166e277ed5SThomas Graf 			goto errout;
2173c4d05c8SPavel Emelyanov 	}
2183c4d05c8SPavel Emelyanov 
219c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
220c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
2216e277ed5SThomas Graf 			goto errout;
222c0636faaSPavel Emelyanov 
223432490f9SCyrill Gorcunov 	/*
224432490f9SCyrill Gorcunov 	 * RAW sockets might have user-defined protocols assigned,
225432490f9SCyrill Gorcunov 	 * so report the one supplied on socket creation.
226432490f9SCyrill Gorcunov 	 */
227432490f9SCyrill Gorcunov 	if (sk->sk_type == SOCK_RAW) {
228432490f9SCyrill Gorcunov 		if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
229432490f9SCyrill Gorcunov 			goto errout;
230432490f9SCyrill Gorcunov 	}
231432490f9SCyrill Gorcunov 
232e31c5e0eSEric Dumazet 	if (!icsk) {
23362ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
2343c4d05c8SPavel Emelyanov 		goto out;
2353c4d05c8SPavel Emelyanov 	}
2363c4d05c8SPavel Emelyanov 
2376ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
23857dde7f7SYuchung Cheng 	    icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2396ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
240a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
241a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
242b7de529cSXin Long 		r->idiag_expires =
2433828a93fSEric Dumazet 			jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
244a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
245a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
246a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
247b7de529cSXin Long 		r->idiag_expires =
2483828a93fSEric Dumazet 			jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
249a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
250a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
251a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
252b7de529cSXin Long 		r->idiag_expires =
2533828a93fSEric Dumazet 			jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies);
254a8c2190eSArnaldo Carvalho de Melo 	} else {
255a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 0;
256a8c2190eSArnaldo Carvalho de Melo 		r->idiag_expires = 0;
257a8c2190eSArnaldo Carvalho de Melo 	}
258a8c2190eSArnaldo Carvalho de Melo 
2593fd22af8SCraig Gallek 	if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
2606ed46d12SNicolas Dichtel 		attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
2616ed46d12SNicolas Dichtel 					 handler->idiag_info_size,
2626ed46d12SNicolas Dichtel 					 INET_DIAG_PAD);
2636e277ed5SThomas Graf 		if (!attr)
2646e277ed5SThomas Graf 			goto errout;
265a8c2190eSArnaldo Carvalho de Melo 
2666e277ed5SThomas Graf 		info = nla_data(attr);
267a8c2190eSArnaldo Carvalho de Melo 	}
268a8c2190eSArnaldo Carvalho de Melo 
269521f1cf1SEric Dumazet 	if (ext & (1 << (INET_DIAG_CONG - 1))) {
270521f1cf1SEric Dumazet 		int err = 0;
271521f1cf1SEric Dumazet 
272521f1cf1SEric Dumazet 		rcu_read_lock();
273521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
274521f1cf1SEric Dumazet 		if (ca_ops)
275521f1cf1SEric Dumazet 			err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
276521f1cf1SEric Dumazet 		rcu_read_unlock();
277521f1cf1SEric Dumazet 		if (err < 0)
2786e277ed5SThomas Graf 			goto errout;
279521f1cf1SEric Dumazet 	}
2806e277ed5SThomas Graf 
281a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
282a8c2190eSArnaldo Carvalho de Melo 
283b37e8840SIvan Delalande 	if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
284b37e8840SIvan Delalande 		if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
285b37e8840SIvan Delalande 			goto errout;
286b37e8840SIvan Delalande 
287521f1cf1SEric Dumazet 	if (sk->sk_state < TCP_TIME_WAIT) {
28864f40ff5SEric Dumazet 		union tcp_cc_info info;
28964f40ff5SEric Dumazet 		size_t sz = 0;
29064f40ff5SEric Dumazet 		int attr;
291521f1cf1SEric Dumazet 
292521f1cf1SEric Dumazet 		rcu_read_lock();
293521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
294521f1cf1SEric Dumazet 		if (ca_ops && ca_ops->get_info)
29564f40ff5SEric Dumazet 			sz = ca_ops->get_info(sk, ext, &attr, &info);
296521f1cf1SEric Dumazet 		rcu_read_unlock();
29764f40ff5SEric Dumazet 		if (sz && nla_put(skb, attr, sz, &info) < 0)
298521f1cf1SEric Dumazet 			goto errout;
299521f1cf1SEric Dumazet 	}
300a8c2190eSArnaldo Carvalho de Melo 
3013c4d05c8SPavel Emelyanov out:
302053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
303053c095aSJohannes Berg 	return 0;
304a8c2190eSArnaldo Carvalho de Melo 
3056e277ed5SThomas Graf errout:
3066e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
30726932566SPatrick McHardy 	return -EMSGSIZE;
308a8c2190eSArnaldo Carvalho de Melo }
3093c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
3103c4d05c8SPavel Emelyanov 
3113c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk,
312e31c5e0eSEric Dumazet 			      struct sk_buff *skb,
31334160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *req,
314d06ca956SEric W. Biederman 			      struct user_namespace *user_ns,
31515e47304SEric W. Biederman 			      u32 portid, u32 seq, u16 nlmsg_flags,
316d545cacaSLorenzo Colitti 			      const struct nlmsghdr *unlh,
317d545cacaSLorenzo Colitti 			      bool net_admin)
3183c4d05c8SPavel Emelyanov {
319d545cacaSLorenzo Colitti 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
320d545cacaSLorenzo Colitti 				 portid, seq, nlmsg_flags, unlh, net_admin);
3213c4d05c8SPavel Emelyanov }
322a8c2190eSArnaldo Carvalho de Melo 
32333cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk,
324e31c5e0eSEric Dumazet 			       struct sk_buff *skb,
32515e47304SEric W. Biederman 			       u32 portid, u32 seq, u16 nlmsg_flags,
326c7d58aabSArnaldo Carvalho de Melo 			       const struct nlmsghdr *unlh)
327c7d58aabSArnaldo Carvalho de Melo {
32833cf7c90SEric Dumazet 	struct inet_timewait_sock *tw = inet_twsk(sk);
329c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
3306e277ed5SThomas Graf 	struct nlmsghdr *nlh;
331789f558cSEric Dumazet 	long tmo;
332c7d58aabSArnaldo Carvalho de Melo 
33315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
3346e277ed5SThomas Graf 			nlmsg_flags);
3356e277ed5SThomas Graf 	if (!nlh)
336d106352dSDavid S. Miller 		return -EMSGSIZE;
337d106352dSDavid S. Miller 
338d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
339c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
340c7d58aabSArnaldo Carvalho de Melo 
341a4458343SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
342c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
343b1aac815SDaniel Borkmann 
344c7d58aabSArnaldo Carvalho de Melo 	r->idiag_state	      = tw->tw_substate;
345c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
3463828a93fSEric Dumazet 	tmo = tw->tw_timer.expires - jiffies;
3473828a93fSEric Dumazet 	r->idiag_expires      = jiffies_delta_to_msecs(tmo);
348c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
349c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
350c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
351c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
3526e277ed5SThomas Graf 
353053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
354053c095aSJohannes Berg 	return 0;
355c7d58aabSArnaldo Carvalho de Melo }
356c7d58aabSArnaldo Carvalho de Melo 
357a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
358a58917f5SEric Dumazet 			      u32 portid, u32 seq, u16 nlmsg_flags,
359d545cacaSLorenzo Colitti 			      const struct nlmsghdr *unlh, bool net_admin)
360a58917f5SEric Dumazet {
361d545cacaSLorenzo Colitti 	struct request_sock *reqsk = inet_reqsk(sk);
362a58917f5SEric Dumazet 	struct inet_diag_msg *r;
363a58917f5SEric Dumazet 	struct nlmsghdr *nlh;
364a58917f5SEric Dumazet 	long tmo;
365a58917f5SEric Dumazet 
366a58917f5SEric Dumazet 	nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
367a58917f5SEric Dumazet 			nlmsg_flags);
368a58917f5SEric Dumazet 	if (!nlh)
369a58917f5SEric Dumazet 		return -EMSGSIZE;
370a58917f5SEric Dumazet 
371a58917f5SEric Dumazet 	r = nlmsg_data(nlh);
372a58917f5SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
373a58917f5SEric Dumazet 	r->idiag_state = TCP_SYN_RECV;
374a58917f5SEric Dumazet 	r->idiag_timer = 1;
375d545cacaSLorenzo Colitti 	r->idiag_retrans = reqsk->num_retrans;
376a58917f5SEric Dumazet 
377a58917f5SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
378a58917f5SEric Dumazet 		     offsetof(struct sock, sk_cookie));
379a58917f5SEric Dumazet 
380fa76ce73SEric Dumazet 	tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
3813828a93fSEric Dumazet 	r->idiag_expires = jiffies_delta_to_msecs(tmo);
382a58917f5SEric Dumazet 	r->idiag_rqueue	= 0;
383a58917f5SEric Dumazet 	r->idiag_wqueue	= 0;
384a58917f5SEric Dumazet 	r->idiag_uid	= 0;
385a58917f5SEric Dumazet 	r->idiag_inode	= 0;
386a58917f5SEric Dumazet 
387d545cacaSLorenzo Colitti 	if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
388d545cacaSLorenzo Colitti 				     inet_rsk(reqsk)->ir_mark))
389d545cacaSLorenzo Colitti 		return -EMSGSIZE;
390d545cacaSLorenzo Colitti 
391a58917f5SEric Dumazet 	nlmsg_end(skb, nlh);
392a58917f5SEric Dumazet 	return 0;
393a58917f5SEric Dumazet }
394a58917f5SEric Dumazet 
395dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
39634160ea3SEric Dumazet 			const struct inet_diag_req_v2 *r,
397d06ca956SEric W. Biederman 			struct user_namespace *user_ns,
39815e47304SEric W. Biederman 			u32 portid, u32 seq, u16 nlmsg_flags,
399d545cacaSLorenzo Colitti 			const struct nlmsghdr *unlh, bool net_admin)
400dff2c035SArnaldo Carvalho de Melo {
401dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
402a58917f5SEric Dumazet 		return inet_twsk_diag_fill(sk, skb, portid, seq,
403a58917f5SEric Dumazet 					   nlmsg_flags, unlh);
404a58917f5SEric Dumazet 
405a58917f5SEric Dumazet 	if (sk->sk_state == TCP_NEW_SYN_RECV)
406a58917f5SEric Dumazet 		return inet_req_diag_fill(sk, skb, portid, seq,
407d545cacaSLorenzo Colitti 					  nlmsg_flags, unlh, net_admin);
408efe4208fSEric Dumazet 
409efe4208fSEric Dumazet 	return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
410d545cacaSLorenzo Colitti 				  nlmsg_flags, unlh, net_admin);
411dff2c035SArnaldo Carvalho de Melo }
412dff2c035SArnaldo Carvalho de Melo 
413b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net,
414b613f56eSLorenzo Colitti 				     struct inet_hashinfo *hashinfo,
41534160ea3SEric Dumazet 				     const struct inet_diag_req_v2 *req)
416a8c2190eSArnaldo Carvalho de Melo {
417e31c5e0eSEric Dumazet 	struct sock *sk;
418a8c2190eSArnaldo Carvalho de Melo 
4192d331915SEric Dumazet 	rcu_read_lock();
420e31c5e0eSEric Dumazet 	if (req->sdiag_family == AF_INET)
421a583636aSCraig Gallek 		sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
422a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
423a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
424dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
4257c130672SEric Dumazet 	else if (req->sdiag_family == AF_INET6) {
4267c130672SEric Dumazet 		if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
4277c130672SEric Dumazet 		    ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
428a583636aSCraig Gallek 			sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
4297c130672SEric Dumazet 					 req->id.idiag_dport, req->id.idiag_src[3],
4307c130672SEric Dumazet 					 req->id.idiag_sport, req->id.idiag_if);
4317c130672SEric Dumazet 		else
432a583636aSCraig Gallek 			sk = inet6_lookup(net, hashinfo, NULL, 0,
433a8c2190eSArnaldo Carvalho de Melo 					  (struct in6_addr *)req->id.idiag_dst,
434a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_dport,
435a8c2190eSArnaldo Carvalho de Melo 					  (struct in6_addr *)req->id.idiag_src,
436a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_sport,
437a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_if);
4387c130672SEric Dumazet 	}
439a8c2190eSArnaldo Carvalho de Melo #endif
4402d331915SEric Dumazet 	else {
4412d331915SEric Dumazet 		rcu_read_unlock();
442b613f56eSLorenzo Colitti 		return ERR_PTR(-EINVAL);
4432d331915SEric Dumazet 	}
4442d331915SEric Dumazet 	rcu_read_unlock();
445e31c5e0eSEric Dumazet 	if (!sk)
446b613f56eSLorenzo Colitti 		return ERR_PTR(-ENOENT);
447a8c2190eSArnaldo Carvalho de Melo 
448b613f56eSLorenzo Colitti 	if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
449b613f56eSLorenzo Colitti 		sock_gen_put(sk);
450b613f56eSLorenzo Colitti 		return ERR_PTR(-ENOENT);
451b613f56eSLorenzo Colitti 	}
452b613f56eSLorenzo Colitti 
453b613f56eSLorenzo Colitti 	return sk;
454b613f56eSLorenzo Colitti }
455b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
456b613f56eSLorenzo Colitti 
457b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
458b613f56eSLorenzo Colitti 			    struct sk_buff *in_skb,
459b613f56eSLorenzo Colitti 			    const struct nlmsghdr *nlh,
460b613f56eSLorenzo Colitti 			    const struct inet_diag_req_v2 *req)
461b613f56eSLorenzo Colitti {
462b37e8840SIvan Delalande 	bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
463b613f56eSLorenzo Colitti 	struct net *net = sock_net(in_skb->sk);
464b613f56eSLorenzo Colitti 	struct sk_buff *rep;
465b613f56eSLorenzo Colitti 	struct sock *sk;
466b613f56eSLorenzo Colitti 	int err;
467b613f56eSLorenzo Colitti 
468b613f56eSLorenzo Colitti 	sk = inet_diag_find_one_icsk(net, hashinfo, req);
469b613f56eSLorenzo Colitti 	if (IS_ERR(sk))
470b613f56eSLorenzo Colitti 		return PTR_ERR(sk);
471a8c2190eSArnaldo Carvalho de Melo 
472b37e8840SIvan Delalande 	rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
4736e277ed5SThomas Graf 	if (!rep) {
4746e277ed5SThomas Graf 		err = -ENOMEM;
475a8c2190eSArnaldo Carvalho de Melo 		goto out;
4766e277ed5SThomas Graf 	}
477a8c2190eSArnaldo Carvalho de Melo 
478a029fe26SPavel Emelyanov 	err = sk_diag_fill(sk, rep, req,
479e32123e5SPatrick McHardy 			   sk_user_ns(NETLINK_CB(in_skb).sk),
48015e47304SEric W. Biederman 			   NETLINK_CB(in_skb).portid,
481b37e8840SIvan Delalande 			   nlh->nlmsg_seq, 0, nlh, net_admin);
48226932566SPatrick McHardy 	if (err < 0) {
48326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
4846e277ed5SThomas Graf 		nlmsg_free(rep);
48526932566SPatrick McHardy 		goto out;
48626932566SPatrick McHardy 	}
48715e47304SEric W. Biederman 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
488a8c2190eSArnaldo Carvalho de Melo 			      MSG_DONTWAIT);
489a8c2190eSArnaldo Carvalho de Melo 	if (err > 0)
490a8c2190eSArnaldo Carvalho de Melo 		err = 0;
491a8c2190eSArnaldo Carvalho de Melo 
492a8c2190eSArnaldo Carvalho de Melo out:
493c1d607ccSEric Dumazet 	if (sk)
494c1d607ccSEric Dumazet 		sock_gen_put(sk);
495c1d607ccSEric Dumazet 
496476f7dbfSPavel Emelyanov 	return err;
497476f7dbfSPavel Emelyanov }
4981942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
499476f7dbfSPavel Emelyanov 
5006eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
501476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
50234160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *req)
503476f7dbfSPavel Emelyanov {
504476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
505476f7dbfSPavel Emelyanov 	int err;
506476f7dbfSPavel Emelyanov 
507476f7dbfSPavel Emelyanov 	handler = inet_diag_lock_handler(req->sdiag_protocol);
508476f7dbfSPavel Emelyanov 	if (IS_ERR(handler))
509476f7dbfSPavel Emelyanov 		err = PTR_ERR(handler);
5106eb5d2e0SLorenzo Colitti 	else if (cmd == SOCK_DIAG_BY_FAMILY)
5111942c518SPavel Emelyanov 		err = handler->dump_one(in_skb, nlh, req);
5126eb5d2e0SLorenzo Colitti 	else if (cmd == SOCK_DESTROY && handler->destroy)
5136eb5d2e0SLorenzo Colitti 		err = handler->destroy(in_skb, req);
5146eb5d2e0SLorenzo Colitti 	else
5156eb5d2e0SLorenzo Colitti 		err = -EOPNOTSUPP;
516d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
517476f7dbfSPavel Emelyanov 
518a8c2190eSArnaldo Carvalho de Melo 	return err;
519a8c2190eSArnaldo Carvalho de Melo }
520a8c2190eSArnaldo Carvalho de Melo 
5219f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
522a8c2190eSArnaldo Carvalho de Melo {
523a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
524a8c2190eSArnaldo Carvalho de Melo 
525a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
526a8c2190eSArnaldo Carvalho de Melo 
527a8c2190eSArnaldo Carvalho de Melo 	if (words) {
528a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
529a8c2190eSArnaldo Carvalho de Melo 			return 0;
530a8c2190eSArnaldo Carvalho de Melo 	}
531a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
5329f855299SAl Viro 		__be32 w1, w2;
5339f855299SAl Viro 		__be32 mask;
534a8c2190eSArnaldo Carvalho de Melo 
535a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
536a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
537a8c2190eSArnaldo Carvalho de Melo 
538a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
539a8c2190eSArnaldo Carvalho de Melo 
540a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
541a8c2190eSArnaldo Carvalho de Melo 			return 0;
542a8c2190eSArnaldo Carvalho de Melo 	}
543a8c2190eSArnaldo Carvalho de Melo 
544a8c2190eSArnaldo Carvalho de Melo 	return 1;
545a8c2190eSArnaldo Carvalho de Melo }
546a8c2190eSArnaldo Carvalho de Melo 
54787c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
548a8c2190eSArnaldo Carvalho de Melo 			    const struct inet_diag_entry *entry)
549a8c2190eSArnaldo Carvalho de Melo {
55087c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
55187c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
55287c22ea5SPavel Emelyanov 
553a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
554a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
555a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
556a8c2190eSArnaldo Carvalho de Melo 
557a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
558a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
559a8c2190eSArnaldo Carvalho de Melo 			break;
560a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
561a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
562a8c2190eSArnaldo Carvalho de Melo 			break;
563bbb6189dSKristian Evensen 		case INET_DIAG_BC_S_EQ:
564bbb6189dSKristian Evensen 			yes = entry->sport == op[1].no;
565bbb6189dSKristian Evensen 			break;
566a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
567a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
568a8c2190eSArnaldo Carvalho de Melo 			break;
569a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
570b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
571a8c2190eSArnaldo Carvalho de Melo 			break;
572bbb6189dSKristian Evensen 		case INET_DIAG_BC_D_EQ:
573bbb6189dSKristian Evensen 			yes = entry->dport == op[1].no;
574bbb6189dSKristian Evensen 			break;
575a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
576a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
577a8c2190eSArnaldo Carvalho de Melo 			break;
578a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
579a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
580a8c2190eSArnaldo Carvalho de Melo 			break;
581a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
582a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
583a8c2190eSArnaldo Carvalho de Melo 			break;
584a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
585a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
586e31c5e0eSEric Dumazet 			const struct inet_diag_hostcond *cond;
587e31c5e0eSEric Dumazet 			const __be32 *addr;
588a8c2190eSArnaldo Carvalho de Melo 
589e31c5e0eSEric Dumazet 			cond = (const struct inet_diag_hostcond *)(op + 1);
590a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
591a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
592a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
593a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
594a8c2190eSArnaldo Carvalho de Melo 				break;
595a8c2190eSArnaldo Carvalho de Melo 			}
596a8c2190eSArnaldo Carvalho de Melo 
597a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
598a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
599a8c2190eSArnaldo Carvalho de Melo 			else
600a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
601a8c2190eSArnaldo Carvalho de Melo 
602f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
603f67caec9SNeal Cardwell 			    cond->family != entry->family) {
604a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
605a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
606a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
607a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
608f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
609f67caec9SNeal Cardwell 							    cond->addr,
610a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
611a8c2190eSArnaldo Carvalho de Melo 						break;
612a8c2190eSArnaldo Carvalho de Melo 				}
613a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
614a8c2190eSArnaldo Carvalho de Melo 				break;
615a8c2190eSArnaldo Carvalho de Melo 			}
616f67caec9SNeal Cardwell 
617f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
618f67caec9SNeal Cardwell 				break;
619f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
620f67caec9SNeal Cardwell 					    cond->prefix_len))
621f67caec9SNeal Cardwell 				break;
622f67caec9SNeal Cardwell 			yes = 0;
623f67caec9SNeal Cardwell 			break;
624f67caec9SNeal Cardwell 		}
625637c841dSDavid Ahern 		case INET_DIAG_BC_DEV_COND: {
626637c841dSDavid Ahern 			u32 ifindex;
627637c841dSDavid Ahern 
628637c841dSDavid Ahern 			ifindex = *((const u32 *)(op + 1));
629637c841dSDavid Ahern 			if (ifindex != entry->ifindex)
630637c841dSDavid Ahern 				yes = 0;
631637c841dSDavid Ahern 			break;
632637c841dSDavid Ahern 		}
633a52e95abSLorenzo Colitti 		case INET_DIAG_BC_MARK_COND: {
634a52e95abSLorenzo Colitti 			struct inet_diag_markcond *cond;
635a52e95abSLorenzo Colitti 
636a52e95abSLorenzo Colitti 			cond = (struct inet_diag_markcond *)(op + 1);
637a52e95abSLorenzo Colitti 			if ((entry->mark & cond->mask) != cond->mark)
638a52e95abSLorenzo Colitti 				yes = 0;
639a52e95abSLorenzo Colitti 			break;
640a52e95abSLorenzo Colitti 		}
641a8c2190eSArnaldo Carvalho de Melo 		}
642a8c2190eSArnaldo Carvalho de Melo 
643a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
644a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
645a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
646a8c2190eSArnaldo Carvalho de Melo 		} else {
647a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
648a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
649a8c2190eSArnaldo Carvalho de Melo 		}
650a8c2190eSArnaldo Carvalho de Melo 	}
651a02cec21SEric Dumazet 	return len == 0;
652a8c2190eSArnaldo Carvalho de Melo }
653a8c2190eSArnaldo Carvalho de Melo 
654a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
655a4458343SEric Dumazet  */
656a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry,
657a4458343SEric Dumazet 			     const struct sock *sk)
658a4458343SEric Dumazet {
659a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
660a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
661a4458343SEric Dumazet 		entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
662a4458343SEric Dumazet 		entry->daddr = sk->sk_v6_daddr.s6_addr32;
663a4458343SEric Dumazet 	} else
664a4458343SEric Dumazet #endif
665a4458343SEric Dumazet 	{
666a4458343SEric Dumazet 		entry->saddr = &sk->sk_rcv_saddr;
667a4458343SEric Dumazet 		entry->daddr = &sk->sk_daddr;
668a4458343SEric Dumazet 	}
669a4458343SEric Dumazet }
670a4458343SEric Dumazet 
6718d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
6728d07d151SPavel Emelyanov {
6738d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
674e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
6758d07d151SPavel Emelyanov 
676e31c5e0eSEric Dumazet 	if (!bc)
6778d07d151SPavel Emelyanov 		return 1;
6788d07d151SPavel Emelyanov 
6798d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
680a4458343SEric Dumazet 	entry_fill_addrs(&entry, sk);
6818d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
6828d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
683637c841dSDavid Ahern 	entry.ifindex = sk->sk_bound_dev_if;
684a58917f5SEric Dumazet 	entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
685a52e95abSLorenzo Colitti 	if (sk_fullsock(sk))
686a52e95abSLorenzo Colitti 		entry.mark = sk->sk_mark;
687a52e95abSLorenzo Colitti 	else if (sk->sk_state == TCP_NEW_SYN_RECV)
688a52e95abSLorenzo Colitti 		entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
689a52e95abSLorenzo Colitti 	else
690a52e95abSLorenzo Colitti 		entry.mark = 0;
6918d07d151SPavel Emelyanov 
6928d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
6938d07d151SPavel Emelyanov }
6948d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
6958d07d151SPavel Emelyanov 
696a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
697a8c2190eSArnaldo Carvalho de Melo {
698a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
699a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
700a8c2190eSArnaldo Carvalho de Melo 
701a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
702a8c2190eSArnaldo Carvalho de Melo 			return 0;
703a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
704a8c2190eSArnaldo Carvalho de Melo 			return 1;
705eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
706a8c2190eSArnaldo Carvalho de Melo 			return 0;
707a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
708a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
709a8c2190eSArnaldo Carvalho de Melo 	}
710a8c2190eSArnaldo Carvalho de Melo 	return 0;
711a8c2190eSArnaldo Carvalho de Melo }
712a8c2190eSArnaldo Carvalho de Melo 
713637c841dSDavid Ahern /* data is u32 ifindex */
714637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
715637c841dSDavid Ahern 			  int *min_len)
716637c841dSDavid Ahern {
717637c841dSDavid Ahern 	/* Check ifindex space. */
718637c841dSDavid Ahern 	*min_len += sizeof(u32);
719637c841dSDavid Ahern 	if (len < *min_len)
720637c841dSDavid Ahern 		return false;
721637c841dSDavid Ahern 
722637c841dSDavid Ahern 	return true;
723637c841dSDavid Ahern }
724405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
725405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
726405c0059SNeal Cardwell 			   int *min_len)
727405c0059SNeal Cardwell {
728405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
729e31c5e0eSEric Dumazet 	int addr_len;
730405c0059SNeal Cardwell 
731405c0059SNeal Cardwell 	/* Check hostcond space. */
732405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
733405c0059SNeal Cardwell 	if (len < *min_len)
734405c0059SNeal Cardwell 		return false;
735405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
736405c0059SNeal Cardwell 
737405c0059SNeal Cardwell 	/* Check address family and address length. */
738405c0059SNeal Cardwell 	switch (cond->family) {
739405c0059SNeal Cardwell 	case AF_UNSPEC:
740405c0059SNeal Cardwell 		addr_len = 0;
741405c0059SNeal Cardwell 		break;
742405c0059SNeal Cardwell 	case AF_INET:
743405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
744405c0059SNeal Cardwell 		break;
745405c0059SNeal Cardwell 	case AF_INET6:
746405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
747405c0059SNeal Cardwell 		break;
748405c0059SNeal Cardwell 	default:
749405c0059SNeal Cardwell 		return false;
750405c0059SNeal Cardwell 	}
751405c0059SNeal Cardwell 	*min_len += addr_len;
752405c0059SNeal Cardwell 	if (len < *min_len)
753405c0059SNeal Cardwell 		return false;
754405c0059SNeal Cardwell 
755405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
756405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
757405c0059SNeal Cardwell 		return false;
758405c0059SNeal Cardwell 
759405c0059SNeal Cardwell 	return true;
760405c0059SNeal Cardwell }
761405c0059SNeal Cardwell 
7625e1f5420SNeal Cardwell /* Validate a port comparison operator. */
763e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op,
7645e1f5420SNeal Cardwell 				  int len, int *min_len)
7655e1f5420SNeal Cardwell {
7665e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
7675e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
7685e1f5420SNeal Cardwell 	if (len < *min_len)
7695e1f5420SNeal Cardwell 		return false;
7705e1f5420SNeal Cardwell 	return true;
7715e1f5420SNeal Cardwell }
7725e1f5420SNeal Cardwell 
773a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
774a52e95abSLorenzo Colitti 			   int *min_len)
775a8c2190eSArnaldo Carvalho de Melo {
776a52e95abSLorenzo Colitti 	*min_len += sizeof(struct inet_diag_markcond);
777a52e95abSLorenzo Colitti 	return len >= *min_len;
778a52e95abSLorenzo Colitti }
779a52e95abSLorenzo Colitti 
780a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr,
781a52e95abSLorenzo Colitti 			      const struct sk_buff *skb)
782a52e95abSLorenzo Colitti {
783a52e95abSLorenzo Colitti 	bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
784627cc4adSLorenzo Colitti 	const void *bytecode, *bc;
785627cc4adSLorenzo Colitti 	int bytecode_len, len;
786627cc4adSLorenzo Colitti 
787627cc4adSLorenzo Colitti 	if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
788627cc4adSLorenzo Colitti 		return -EINVAL;
789627cc4adSLorenzo Colitti 
790627cc4adSLorenzo Colitti 	bytecode = bc = nla_data(attr);
791627cc4adSLorenzo Colitti 	len = bytecode_len = nla_len(attr);
792a8c2190eSArnaldo Carvalho de Melo 
793a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
794405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
795e31c5e0eSEric Dumazet 		const struct inet_diag_bc_op *op = bc;
796a8c2190eSArnaldo Carvalho de Melo 
797a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
798a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
799a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
800405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
801405c0059SNeal Cardwell 				return -EINVAL;
8025e1f5420SNeal Cardwell 			break;
803637c841dSDavid Ahern 		case INET_DIAG_BC_DEV_COND:
804637c841dSDavid Ahern 			if (!valid_devcond(bc, len, &min_len))
805637c841dSDavid Ahern 				return -EINVAL;
806637c841dSDavid Ahern 			break;
807bbb6189dSKristian Evensen 		case INET_DIAG_BC_S_EQ:
808a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
809a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
810bbb6189dSKristian Evensen 		case INET_DIAG_BC_D_EQ:
811a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
812a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
8135e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
814a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
815a8c2190eSArnaldo Carvalho de Melo 			break;
816a52e95abSLorenzo Colitti 		case INET_DIAG_BC_MARK_COND:
817a52e95abSLorenzo Colitti 			if (!net_admin)
818a52e95abSLorenzo Colitti 				return -EPERM;
819a52e95abSLorenzo Colitti 			if (!valid_markcond(bc, len, &min_len))
820a52e95abSLorenzo Colitti 				return -EINVAL;
821a52e95abSLorenzo Colitti 			break;
8225e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
8235e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
824a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
825a8c2190eSArnaldo Carvalho de Melo 			break;
826a8c2190eSArnaldo Carvalho de Melo 		default:
827a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
828a8c2190eSArnaldo Carvalho de Melo 		}
8295e1f5420SNeal Cardwell 
8305e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
8315e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
8325e1f5420SNeal Cardwell 				return -EINVAL;
8335e1f5420SNeal Cardwell 			if (op->no < len &&
8345e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
8355e1f5420SNeal Cardwell 				return -EINVAL;
8365e1f5420SNeal Cardwell 		}
8375e1f5420SNeal Cardwell 
838405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
839eeb14972SEric Dumazet 			return -EINVAL;
840a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
841a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
842a8c2190eSArnaldo Carvalho de Melo 	}
843a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
844a8c2190eSArnaldo Carvalho de Melo }
845a8c2190eSArnaldo Carvalho de Melo 
846dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk,
847dff2c035SArnaldo Carvalho de Melo 			      struct sk_buff *skb,
84837f352b5SPavel Emelyanov 			      struct netlink_callback *cb,
84934160ea3SEric Dumazet 			      const struct inet_diag_req_v2 *r,
850d545cacaSLorenzo Colitti 			      const struct nlattr *bc,
851d545cacaSLorenzo Colitti 			      bool net_admin)
852a8c2190eSArnaldo Carvalho de Melo {
8538d07d151SPavel Emelyanov 	if (!inet_diag_bc_sk(bc, sk))
854a8c2190eSArnaldo Carvalho de Melo 		return 0;
855a8c2190eSArnaldo Carvalho de Melo 
856a029fe26SPavel Emelyanov 	return inet_csk_diag_fill(sk, skb, r,
857e32123e5SPatrick McHardy 				  sk_user_ns(NETLINK_CB(cb->skb).sk),
85815e47304SEric W. Biederman 				  NETLINK_CB(cb->skb).portid,
859d545cacaSLorenzo Colitti 				  cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
860d545cacaSLorenzo Colitti 				  net_admin);
861a8c2190eSArnaldo Carvalho de Melo }
862a8c2190eSArnaldo Carvalho de Melo 
86349612729SEric Dumazet static void twsk_build_assert(void)
86449612729SEric Dumazet {
86549612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
86649612729SEric Dumazet 		     offsetof(struct sock, sk_family));
86749612729SEric Dumazet 
86849612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
86949612729SEric Dumazet 		     offsetof(struct inet_sock, inet_num));
87049612729SEric Dumazet 
87149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
87249612729SEric Dumazet 		     offsetof(struct inet_sock, inet_dport));
87349612729SEric Dumazet 
87449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
87549612729SEric Dumazet 		     offsetof(struct inet_sock, inet_rcv_saddr));
87649612729SEric Dumazet 
87749612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
87849612729SEric Dumazet 		     offsetof(struct inet_sock, inet_daddr));
87949612729SEric Dumazet 
88049612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
88149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
88249612729SEric Dumazet 		     offsetof(struct sock, sk_v6_rcv_saddr));
88349612729SEric Dumazet 
88449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
88549612729SEric Dumazet 		     offsetof(struct sock, sk_v6_daddr));
88649612729SEric Dumazet #endif
88749612729SEric Dumazet }
88849612729SEric Dumazet 
8891942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
890e31c5e0eSEric Dumazet 			 struct netlink_callback *cb,
89134160ea3SEric Dumazet 			 const struct inet_diag_req_v2 *r, struct nlattr *bc)
892a8c2190eSArnaldo Carvalho de Melo {
893d545cacaSLorenzo Colitti 	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
89467db3e4bSEric Dumazet 	struct net *net = sock_net(skb->sk);
89567db3e4bSEric Dumazet 	u32 idiag_states = r->idiag_states;
89667db3e4bSEric Dumazet 	int i, num, s_i, s_num;
89767db3e4bSEric Dumazet 	struct sock *sk;
898a8c2190eSArnaldo Carvalho de Melo 
899079096f1SEric Dumazet 	if (idiag_states & TCPF_SYN_RECV)
900079096f1SEric Dumazet 		idiag_states |= TCPF_NEW_SYN_RECV;
901a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
902a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
903a8c2190eSArnaldo Carvalho de Melo 
904a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
9059652dc2eSEric Dumazet 		if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
906a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
907a8c2190eSArnaldo Carvalho de Melo 
908a8c2190eSArnaldo Carvalho de Melo 		for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
9095caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
9108dbd76e7SEric Dumazet 			struct hlist_nulls_node *node;
911a8c2190eSArnaldo Carvalho de Melo 
912a8c2190eSArnaldo Carvalho de Melo 			num = 0;
9135caea4eaSEric Dumazet 			ilb = &hashinfo->listening_hash[i];
9149652dc2eSEric Dumazet 			spin_lock(&ilb->lock);
9158dbd76e7SEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->nulls_head) {
916a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
917a8c2190eSArnaldo Carvalho de Melo 
91851d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
91951d7cccfSAndrey Vagin 					continue;
92051d7cccfSAndrey Vagin 
921a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
922a8c2190eSArnaldo Carvalho de Melo 					num++;
923a8c2190eSArnaldo Carvalho de Melo 					continue;
924a8c2190eSArnaldo Carvalho de Melo 				}
925a8c2190eSArnaldo Carvalho de Melo 
926d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
927d23deaa0SPavel Emelyanov 				    sk->sk_family != r->sdiag_family)
928d23deaa0SPavel Emelyanov 					goto next_listen;
929d23deaa0SPavel Emelyanov 
930c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
931a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
932a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
933a8c2190eSArnaldo Carvalho de Melo 
934d545cacaSLorenzo Colitti 				if (inet_csk_diag_dump(sk, skb, cb, r,
935d545cacaSLorenzo Colitti 						       bc, net_admin) < 0) {
9369652dc2eSEric Dumazet 					spin_unlock(&ilb->lock);
937a8c2190eSArnaldo Carvalho de Melo 					goto done;
938a8c2190eSArnaldo Carvalho de Melo 				}
939a8c2190eSArnaldo Carvalho de Melo 
940a8c2190eSArnaldo Carvalho de Melo next_listen:
941a8c2190eSArnaldo Carvalho de Melo 				++num;
942a8c2190eSArnaldo Carvalho de Melo 			}
9439652dc2eSEric Dumazet 			spin_unlock(&ilb->lock);
944a8c2190eSArnaldo Carvalho de Melo 
945a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
946a8c2190eSArnaldo Carvalho de Melo 		}
947a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
948a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
949a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
950a8c2190eSArnaldo Carvalho de Melo 	}
951a8c2190eSArnaldo Carvalho de Melo 
952079096f1SEric Dumazet 	if (!(idiag_states & ~TCPF_LISTEN))
953efb3cb42SPavel Emelyanov 		goto out;
954a8c2190eSArnaldo Carvalho de Melo 
95567db3e4bSEric Dumazet #define SKARR_SZ 16
956f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
957a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
9587e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
9593ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
96067db3e4bSEric Dumazet 		struct sock *sk_arr[SKARR_SZ];
96167db3e4bSEric Dumazet 		int num_arr[SKARR_SZ];
96267db3e4bSEric Dumazet 		int idx, accum, res;
9636be547a6SAndi Kleen 
96405dbc7b5SEric Dumazet 		if (hlist_nulls_empty(&head->chain))
9656be547a6SAndi Kleen 			continue;
9666be547a6SAndi Kleen 
967a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
968a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
969a8c2190eSArnaldo Carvalho de Melo 
97067db3e4bSEric Dumazet next_chunk:
97167db3e4bSEric Dumazet 		num = 0;
97267db3e4bSEric Dumazet 		accum = 0;
9737e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
9743ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
97567db3e4bSEric Dumazet 			int state;
976a8c2190eSArnaldo Carvalho de Melo 
97751d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
97851d7cccfSAndrey Vagin 				continue;
979a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
980a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
98170315d22SNeal Cardwell 			state = (sk->sk_state == TCP_TIME_WAIT) ?
98270315d22SNeal Cardwell 				inet_twsk(sk)->tw_substate : sk->sk_state;
983079096f1SEric Dumazet 			if (!(idiag_states & (1 << state)))
984a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
985d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
986d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
987d23deaa0SPavel Emelyanov 				goto next_normal;
98805dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
989a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
990a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
99105dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
9924e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
993a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
994a58917f5SEric Dumazet 			twsk_build_assert();
995a58917f5SEric Dumazet 
996a58917f5SEric Dumazet 			if (!inet_diag_bc_sk(bc, sk))
997a58917f5SEric Dumazet 				goto next_normal;
998a58917f5SEric Dumazet 
999f0c928d8SEric Dumazet 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
1000f0c928d8SEric Dumazet 				goto next_normal;
1001f0c928d8SEric Dumazet 
100267db3e4bSEric Dumazet 			num_arr[accum] = num;
100367db3e4bSEric Dumazet 			sk_arr[accum] = sk;
100467db3e4bSEric Dumazet 			if (++accum == SKARR_SZ)
100567db3e4bSEric Dumazet 				break;
100667db3e4bSEric Dumazet next_normal:
100767db3e4bSEric Dumazet 			++num;
100867db3e4bSEric Dumazet 		}
100967db3e4bSEric Dumazet 		spin_unlock_bh(lock);
101067db3e4bSEric Dumazet 		res = 0;
101167db3e4bSEric Dumazet 		for (idx = 0; idx < accum; idx++) {
101267db3e4bSEric Dumazet 			if (res >= 0) {
101367db3e4bSEric Dumazet 				res = sk_diag_fill(sk_arr[idx], skb, r,
1014a58917f5SEric Dumazet 					   sk_user_ns(NETLINK_CB(cb->skb).sk),
1015a58917f5SEric Dumazet 					   NETLINK_CB(cb->skb).portid,
1016a58917f5SEric Dumazet 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
1017d545cacaSLorenzo Colitti 					   cb->nlh, net_admin);
101867db3e4bSEric Dumazet 				if (res < 0)
101967db3e4bSEric Dumazet 					num = num_arr[idx];
1020a8c2190eSArnaldo Carvalho de Melo 			}
102167db3e4bSEric Dumazet 			sock_gen_put(sk_arr[idx]);
1022a8c2190eSArnaldo Carvalho de Melo 		}
102367db3e4bSEric Dumazet 		if (res < 0)
102467db3e4bSEric Dumazet 			break;
1025acffb584SEric Dumazet 		cond_resched();
102667db3e4bSEric Dumazet 		if (accum == SKARR_SZ) {
102767db3e4bSEric Dumazet 			s_num = num + 1;
102867db3e4bSEric Dumazet 			goto next_chunk;
102967db3e4bSEric Dumazet 		}
1030a8c2190eSArnaldo Carvalho de Melo 	}
1031a8c2190eSArnaldo Carvalho de Melo 
1032a8c2190eSArnaldo Carvalho de Melo done:
1033a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
1034a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
1035efb3cb42SPavel Emelyanov out:
1036efb3cb42SPavel Emelyanov 	;
1037efb3cb42SPavel Emelyanov }
10381942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
1039efb3cb42SPavel Emelyanov 
1040efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
104134160ea3SEric Dumazet 			    const struct inet_diag_req_v2 *r,
1042e31c5e0eSEric Dumazet 			    struct nlattr *bc)
1043efb3cb42SPavel Emelyanov {
1044efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
1045cacb6ba0SCyrill Gorcunov 	int err = 0;
1046efb3cb42SPavel Emelyanov 
1047efb3cb42SPavel Emelyanov 	handler = inet_diag_lock_handler(r->sdiag_protocol);
1048efb3cb42SPavel Emelyanov 	if (!IS_ERR(handler))
10491942c518SPavel Emelyanov 		handler->dump(skb, cb, r, bc);
1050cacb6ba0SCyrill Gorcunov 	else
1051cacb6ba0SCyrill Gorcunov 		err = PTR_ERR(handler);
1052d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
1053efb3cb42SPavel Emelyanov 
1054cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
1055a8c2190eSArnaldo Carvalho de Melo }
1056a8c2190eSArnaldo Carvalho de Melo 
105725c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
105825c4cd2bSPavel Emelyanov {
1059c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
1060e31c5e0eSEric Dumazet 	struct nlattr *bc = NULL;
106125c4cd2bSPavel Emelyanov 
106225c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
106325c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
106425c4cd2bSPavel Emelyanov 
1065d106352dSDavid S. Miller 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
106625c4cd2bSPavel Emelyanov }
106725c4cd2bSPavel Emelyanov 
1068e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type)
1069a029fe26SPavel Emelyanov {
1070a029fe26SPavel Emelyanov 	switch (type) {
1071a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
1072a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
1073a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
1074a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
1075a029fe26SPavel Emelyanov 	default:
1076a029fe26SPavel Emelyanov 		return 0;
1077a029fe26SPavel Emelyanov 	}
1078a029fe26SPavel Emelyanov }
1079a029fe26SPavel Emelyanov 
1080e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb,
1081e31c5e0eSEric Dumazet 				 struct netlink_callback *cb)
108225c4cd2bSPavel Emelyanov {
1083d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1084e31c5e0eSEric Dumazet 	int hdrlen = sizeof(struct inet_diag_req);
1085c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
108625c4cd2bSPavel Emelyanov 	struct nlattr *bc = NULL;
108725c4cd2bSPavel Emelyanov 
1088d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
108925c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
109025c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
109125c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
109225c4cd2bSPavel Emelyanov 	req.id = rc->id;
109325c4cd2bSPavel Emelyanov 
109425c4cd2bSPavel Emelyanov 	if (nlmsg_attrlen(cb->nlh, hdrlen))
109525c4cd2bSPavel Emelyanov 		bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
109625c4cd2bSPavel Emelyanov 
109725c4cd2bSPavel Emelyanov 	return __inet_diag_dump(skb, cb, &req, bc);
109825c4cd2bSPavel Emelyanov }
109925c4cd2bSPavel Emelyanov 
1100fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1101fe50ce28SPavel Emelyanov 				      const struct nlmsghdr *nlh)
1102fe50ce28SPavel Emelyanov {
1103d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1104c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1105fe50ce28SPavel Emelyanov 
1106fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1107fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1108fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
1109fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1110fe50ce28SPavel Emelyanov 	req.id = rc->id;
1111fe50ce28SPavel Emelyanov 
11126eb5d2e0SLorenzo Colitti 	return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
1113fe50ce28SPavel Emelyanov }
1114fe50ce28SPavel Emelyanov 
11158d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1116a8c2190eSArnaldo Carvalho de Melo {
11173b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
111851d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1119a8c2190eSArnaldo Carvalho de Melo 
1120ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1121ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1122ead592baSThomas Graf 		return -EINVAL;
1123a8c2190eSArnaldo Carvalho de Melo 
1124b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
1125ead592baSThomas Graf 		if (nlmsg_attrlen(nlh, hdrlen)) {
1126ead592baSThomas Graf 			struct nlattr *attr;
1127627cc4adSLorenzo Colitti 			int err;
1128ead592baSThomas Graf 
1129ead592baSThomas Graf 			attr = nlmsg_find_attr(nlh, hdrlen,
1130ead592baSThomas Graf 					       INET_DIAG_REQ_BYTECODE);
1131a52e95abSLorenzo Colitti 			err = inet_diag_bc_audit(attr, skb);
1132627cc4adSLorenzo Colitti 			if (err)
1133627cc4adSLorenzo Colitti 				return err;
1134a8c2190eSArnaldo Carvalho de Melo 		}
113580d326faSPablo Neira Ayuso 		{
113680d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
113780d326faSPablo Neira Ayuso 				.dump = inet_diag_dump_compat,
113880d326faSPablo Neira Ayuso 			};
113951d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
114080d326faSPablo Neira Ayuso 		}
1141a8c2190eSArnaldo Carvalho de Melo 	}
1142ead592baSThomas Graf 
1143fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1144a8c2190eSArnaldo Carvalho de Melo }
1145a8c2190eSArnaldo Carvalho de Melo 
11466eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1147d366477aSPavel Emelyanov {
1148c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
114951d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1150d366477aSPavel Emelyanov 
1151d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1152d366477aSPavel Emelyanov 		return -EINVAL;
1153d366477aSPavel Emelyanov 
11546eb5d2e0SLorenzo Colitti 	if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
11556eb5d2e0SLorenzo Colitti 	    h->nlmsg_flags & NLM_F_DUMP) {
115625c4cd2bSPavel Emelyanov 		if (nlmsg_attrlen(h, hdrlen)) {
115725c4cd2bSPavel Emelyanov 			struct nlattr *attr;
1158627cc4adSLorenzo Colitti 			int err;
1159e31c5e0eSEric Dumazet 
116025c4cd2bSPavel Emelyanov 			attr = nlmsg_find_attr(h, hdrlen,
116125c4cd2bSPavel Emelyanov 					       INET_DIAG_REQ_BYTECODE);
1162a52e95abSLorenzo Colitti 			err = inet_diag_bc_audit(attr, skb);
1163627cc4adSLorenzo Colitti 			if (err)
1164627cc4adSLorenzo Colitti 				return err;
116525c4cd2bSPavel Emelyanov 		}
116680d326faSPablo Neira Ayuso 		{
116780d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
116880d326faSPablo Neira Ayuso 				.dump = inet_diag_dump,
116980d326faSPablo Neira Ayuso 			};
117051d7cccfSAndrey Vagin 			return netlink_dump_start(net->diag_nlsk, skb, h, &c);
117180d326faSPablo Neira Ayuso 		}
1172d366477aSPavel Emelyanov 	}
1173d366477aSPavel Emelyanov 
11746eb5d2e0SLorenzo Colitti 	return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
1175d366477aSPavel Emelyanov }
1176d366477aSPavel Emelyanov 
117735ac838aSCraig Gallek static
117835ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
117935ac838aSCraig Gallek {
118035ac838aSCraig Gallek 	const struct inet_diag_handler *handler;
118135ac838aSCraig Gallek 	struct nlmsghdr *nlh;
118235ac838aSCraig Gallek 	struct nlattr *attr;
118335ac838aSCraig Gallek 	struct inet_diag_msg *r;
118435ac838aSCraig Gallek 	void *info = NULL;
118535ac838aSCraig Gallek 	int err = 0;
118635ac838aSCraig Gallek 
118735ac838aSCraig Gallek 	nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
118835ac838aSCraig Gallek 	if (!nlh)
118935ac838aSCraig Gallek 		return -ENOMEM;
119035ac838aSCraig Gallek 
119135ac838aSCraig Gallek 	r = nlmsg_data(nlh);
119235ac838aSCraig Gallek 	memset(r, 0, sizeof(*r));
119335ac838aSCraig Gallek 	inet_diag_msg_common_fill(r, sk);
1194e0df02e0SCraig Gallek 	if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1195e0df02e0SCraig Gallek 		r->id.idiag_sport = inet_sk(sk)->inet_sport;
119635ac838aSCraig Gallek 	r->idiag_state = sk->sk_state;
119735ac838aSCraig Gallek 
119835ac838aSCraig Gallek 	if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
119935ac838aSCraig Gallek 		nlmsg_cancel(skb, nlh);
120035ac838aSCraig Gallek 		return err;
120135ac838aSCraig Gallek 	}
120235ac838aSCraig Gallek 
120335ac838aSCraig Gallek 	handler = inet_diag_lock_handler(sk->sk_protocol);
120435ac838aSCraig Gallek 	if (IS_ERR(handler)) {
120535ac838aSCraig Gallek 		inet_diag_unlock_handler(handler);
120635ac838aSCraig Gallek 		nlmsg_cancel(skb, nlh);
120735ac838aSCraig Gallek 		return PTR_ERR(handler);
120835ac838aSCraig Gallek 	}
120935ac838aSCraig Gallek 
121035ac838aSCraig Gallek 	attr = handler->idiag_info_size
12116ed46d12SNicolas Dichtel 		? nla_reserve_64bit(skb, INET_DIAG_INFO,
12126ed46d12SNicolas Dichtel 				    handler->idiag_info_size,
12136ed46d12SNicolas Dichtel 				    INET_DIAG_PAD)
121435ac838aSCraig Gallek 		: NULL;
121535ac838aSCraig Gallek 	if (attr)
121635ac838aSCraig Gallek 		info = nla_data(attr);
121735ac838aSCraig Gallek 
121835ac838aSCraig Gallek 	handler->idiag_get_info(sk, r, info);
121935ac838aSCraig Gallek 	inet_diag_unlock_handler(handler);
122035ac838aSCraig Gallek 
122135ac838aSCraig Gallek 	nlmsg_end(skb, nlh);
122235ac838aSCraig Gallek 	return 0;
122335ac838aSCraig Gallek }
122435ac838aSCraig Gallek 
12258dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1226d366477aSPavel Emelyanov 	.family = AF_INET,
12276eb5d2e0SLorenzo Colitti 	.dump = inet_diag_handler_cmd,
122835ac838aSCraig Gallek 	.get_info = inet_diag_handler_get_info,
12296eb5d2e0SLorenzo Colitti 	.destroy = inet_diag_handler_cmd,
1230d366477aSPavel Emelyanov };
1231d366477aSPavel Emelyanov 
12328dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1233d366477aSPavel Emelyanov 	.family = AF_INET6,
12346eb5d2e0SLorenzo Colitti 	.dump = inet_diag_handler_cmd,
123535ac838aSCraig Gallek 	.get_info = inet_diag_handler_get_info,
12366eb5d2e0SLorenzo Colitti 	.destroy = inet_diag_handler_cmd,
1237d366477aSPavel Emelyanov };
1238d366477aSPavel Emelyanov 
1239a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1240a8c2190eSArnaldo Carvalho de Melo {
1241a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1242a8c2190eSArnaldo Carvalho de Melo 	int err = -EINVAL;
1243a8c2190eSArnaldo Carvalho de Melo 
1244f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1245a8c2190eSArnaldo Carvalho de Melo 		goto out;
1246a8c2190eSArnaldo Carvalho de Melo 
1247d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1248a8c2190eSArnaldo Carvalho de Melo 	err = -EEXIST;
1249e31c5e0eSEric Dumazet 	if (!inet_diag_table[type]) {
1250a8c2190eSArnaldo Carvalho de Melo 		inet_diag_table[type] = h;
1251a8c2190eSArnaldo Carvalho de Melo 		err = 0;
1252a8c2190eSArnaldo Carvalho de Melo 	}
1253d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1254a8c2190eSArnaldo Carvalho de Melo out:
1255a8c2190eSArnaldo Carvalho de Melo 	return err;
1256a8c2190eSArnaldo Carvalho de Melo }
1257a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1258a8c2190eSArnaldo Carvalho de Melo 
1259a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1260a8c2190eSArnaldo Carvalho de Melo {
1261a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1262a8c2190eSArnaldo Carvalho de Melo 
1263f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1264a8c2190eSArnaldo Carvalho de Melo 		return;
1265a8c2190eSArnaldo Carvalho de Melo 
1266d523a328SHerbert Xu 	mutex_lock(&inet_diag_table_mutex);
1267a8c2190eSArnaldo Carvalho de Melo 	inet_diag_table[type] = NULL;
1268d523a328SHerbert Xu 	mutex_unlock(&inet_diag_table_mutex);
1269a8c2190eSArnaldo Carvalho de Melo }
1270a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1271a8c2190eSArnaldo Carvalho de Melo 
1272a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1273a8c2190eSArnaldo Carvalho de Melo {
1274f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1275a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1276a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1277a8c2190eSArnaldo Carvalho de Melo 
12780da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1279a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1280a8c2190eSArnaldo Carvalho de Melo 		goto out;
1281a8c2190eSArnaldo Carvalho de Melo 
1282d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1283d366477aSPavel Emelyanov 	if (err)
1284d366477aSPavel Emelyanov 		goto out_free_nl;
1285d366477aSPavel Emelyanov 
1286d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1287d366477aSPavel Emelyanov 	if (err)
1288d366477aSPavel Emelyanov 		goto out_free_inet;
1289d366477aSPavel Emelyanov 
12908ef874bfSPavel Emelyanov 	sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1291a8c2190eSArnaldo Carvalho de Melo out:
1292a8c2190eSArnaldo Carvalho de Melo 	return err;
1293d366477aSPavel Emelyanov 
1294d366477aSPavel Emelyanov out_free_inet:
1295d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1296d366477aSPavel Emelyanov out_free_nl:
1297a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1298a8c2190eSArnaldo Carvalho de Melo 	goto out;
1299a8c2190eSArnaldo Carvalho de Melo }
1300a8c2190eSArnaldo Carvalho de Melo 
1301a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1302a8c2190eSArnaldo Carvalho de Melo {
1303d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1304d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
13058ef874bfSPavel Emelyanov 	sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1306a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1307a8c2190eSArnaldo Carvalho de Melo }
1308a8c2190eSArnaldo Carvalho de Melo 
1309a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1310a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1311a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1312aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1313aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1314