xref: /linux/net/ipv4/inet_diag.c (revision 9410645520e9b820069761f3450ef6661418e279)
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>
26085c20caSMartin KaFai Lau #include <net/bpf_sk_storage.h>
27dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
28a8c2190eSArnaldo Carvalho de Melo 
29a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h>
30a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h>
31a8c2190eSArnaldo Carvalho de Melo 
32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h>
33d366477aSPavel Emelyanov #include <linux/sock_diag.h>
34a8c2190eSArnaldo Carvalho de Melo 
35223f5519SEric Dumazet static const struct inet_diag_handler __rcu **inet_diag_table;
36a8c2190eSArnaldo Carvalho de Melo 
37a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry {
38e31c5e0eSEric Dumazet 	const __be32 *saddr;
39e31c5e0eSEric Dumazet 	const __be32 *daddr;
40a8c2190eSArnaldo Carvalho de Melo 	u16 sport;
41a8c2190eSArnaldo Carvalho de Melo 	u16 dport;
42a8c2190eSArnaldo Carvalho de Melo 	u16 family;
43a8c2190eSArnaldo Carvalho de Melo 	u16 userlocks;
44637c841dSDavid Ahern 	u32 ifindex;
45a52e95abSLorenzo Colitti 	u32 mark;
46b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
47b1f3e43dSDmitry Yakunin 	u64 cgroup_id;
48b1f3e43dSDmitry Yakunin #endif
49a8c2190eSArnaldo Carvalho de Melo };
50a8c2190eSArnaldo Carvalho de Melo 
inet_diag_lock_handler(int proto)51f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
52d523a328SHerbert Xu {
53223f5519SEric Dumazet 	const struct inet_diag_handler *handler;
54223f5519SEric Dumazet 
55223f5519SEric Dumazet 	if (proto < 0 || proto >= IPPROTO_MAX)
56223f5519SEric Dumazet 		return NULL;
573f935c75SPaolo Abeni 
58e50e10aeSEric Dumazet 	if (!READ_ONCE(inet_diag_table[proto]))
59bf2ae2e4SXin Long 		sock_load_diag_module(AF_INET, proto);
60d523a328SHerbert Xu 
61223f5519SEric Dumazet 	rcu_read_lock();
62223f5519SEric Dumazet 	handler = rcu_dereference(inet_diag_table[proto]);
63223f5519SEric Dumazet 	if (handler && !try_module_get(handler->owner))
64223f5519SEric Dumazet 		handler = NULL;
65223f5519SEric Dumazet 	rcu_read_unlock();
66d523a328SHerbert Xu 
67223f5519SEric Dumazet 	return handler;
68d523a328SHerbert Xu }
69d523a328SHerbert Xu 
inet_diag_unlock_handler(const struct inet_diag_handler * handler)70e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
71d523a328SHerbert Xu {
72223f5519SEric Dumazet 	module_put(handler->owner);
73d523a328SHerbert Xu }
74d523a328SHerbert Xu 
inet_diag_msg_common_fill(struct inet_diag_msg * r,struct sock * sk)75cb2050a7SXin Long void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
76a4458343SEric Dumazet {
77a4458343SEric Dumazet 	r->idiag_family = sk->sk_family;
78a4458343SEric Dumazet 
79a4458343SEric Dumazet 	r->id.idiag_sport = htons(sk->sk_num);
80a4458343SEric Dumazet 	r->id.idiag_dport = sk->sk_dport;
81a4458343SEric Dumazet 	r->id.idiag_if = sk->sk_bound_dev_if;
82a4458343SEric Dumazet 	sock_diag_save_cookie(sk, r->id.idiag_cookie);
83a4458343SEric Dumazet 
84a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
85a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
86a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
87a4458343SEric Dumazet 		*(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
88a4458343SEric Dumazet 	} else
89a4458343SEric Dumazet #endif
90a4458343SEric Dumazet 	{
91a4458343SEric Dumazet 	memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
92a4458343SEric Dumazet 	memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
93a4458343SEric Dumazet 
94a4458343SEric Dumazet 	r->id.idiag_src[0] = sk->sk_rcv_saddr;
95a4458343SEric Dumazet 	r->id.idiag_dst[0] = sk->sk_daddr;
96a4458343SEric Dumazet 	}
97a4458343SEric Dumazet }
98cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
99a4458343SEric Dumazet 
inet_sk_attr_size(struct sock * sk,const struct inet_diag_req_v2 * req,bool net_admin)100b37e8840SIvan Delalande static size_t inet_sk_attr_size(struct sock *sk,
101b37e8840SIvan Delalande 				const struct inet_diag_req_v2 *req,
102b37e8840SIvan Delalande 				bool net_admin)
103c8e2c80dSEric Dumazet {
104b37e8840SIvan Delalande 	const struct inet_diag_handler *handler;
105b37e8840SIvan Delalande 	size_t aux = 0;
106b37e8840SIvan Delalande 
107223f5519SEric Dumazet 	rcu_read_lock();
108223f5519SEric Dumazet 	handler = rcu_dereference(inet_diag_table[req->sdiag_protocol]);
109223f5519SEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(!handler);
110b37e8840SIvan Delalande 	if (handler && handler->idiag_get_aux_size)
111b37e8840SIvan Delalande 		aux = handler->idiag_get_aux_size(sk, net_admin);
112223f5519SEric Dumazet 	rcu_read_unlock();
113b37e8840SIvan Delalande 
114c8e2c80dSEric Dumazet 	return	  nla_total_size(sizeof(struct tcp_info))
115c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct inet_diag_msg))
11683f73c5bSDmitry Yakunin 		+ inet_diag_msg_attrs_size()
11783f73c5bSDmitry Yakunin 		+ nla_total_size(sizeof(struct inet_diag_meminfo))
118c8e2c80dSEric Dumazet 		+ nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
119c8e2c80dSEric Dumazet 		+ nla_total_size(TCP_CA_NAME_MAX)
120c8e2c80dSEric Dumazet 		+ nla_total_size(sizeof(struct tcpvegas_info))
121b37e8840SIvan Delalande 		+ aux
122c8e2c80dSEric Dumazet 		+ 64;
123c8e2c80dSEric Dumazet }
124c8e2c80dSEric Dumazet 
inet_diag_msg_attrs_fill(struct sock * sk,struct sk_buff * skb,struct inet_diag_msg * r,int ext,struct user_namespace * user_ns,bool net_admin)125cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
126cb2050a7SXin Long 			     struct inet_diag_msg *r, int ext,
127d545cacaSLorenzo Colitti 			     struct user_namespace *user_ns,
128d545cacaSLorenzo Colitti 			     bool net_admin)
129a8c2190eSArnaldo Carvalho de Melo {
130a8c2190eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
131c1077616SWei Wang 	struct inet_diag_sockopt inet_sockopt;
132a8c2190eSArnaldo Carvalho de Melo 
133e4e541a8SPavel Emelyanov 	if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
134e4e541a8SPavel Emelyanov 		goto errout;
135e4e541a8SPavel Emelyanov 
136717b6d83SMaciej Żenczykowski 	/* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
137717b6d83SMaciej Żenczykowski 	 * hence this needs to be included regardless of socket family.
138717b6d83SMaciej Żenczykowski 	 */
139717b6d83SMaciej Żenczykowski 	if (ext & (1 << (INET_DIAG_TOS - 1)))
140e08d0b3dSEric Dumazet 		if (nla_put_u8(skb, INET_DIAG_TOS, READ_ONCE(inet->tos)) < 0)
1416e277ed5SThomas Graf 			goto errout;
142717b6d83SMaciej Żenczykowski 
143dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
144a8c2190eSArnaldo Carvalho de Melo 	if (r->idiag_family == AF_INET6) {
14506236ac3SMaciej Żenczykowski 		if (ext & (1 << (INET_DIAG_TCLASS - 1)))
146efe4208fSEric Dumazet 			if (nla_put_u8(skb, INET_DIAG_TCLASS,
147efe4208fSEric Dumazet 				       inet6_sk(sk)->tclass) < 0)
1486e277ed5SThomas Graf 				goto errout;
14920462155SPhil Sutter 
1508220ea23SPhil Sutter 		if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
1518220ea23SPhil Sutter 		    nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
15220462155SPhil Sutter 			goto errout;
153a8c2190eSArnaldo Carvalho de Melo 	}
154a8c2190eSArnaldo Carvalho de Melo #endif
155a8c2190eSArnaldo Carvalho de Melo 
1563c5b4d69SEric Dumazet 	if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, READ_ONCE(sk->sk_mark)))
157d545cacaSLorenzo Colitti 		goto errout;
158d545cacaSLorenzo Colitti 
15983f73c5bSDmitry Yakunin 	if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
16083f73c5bSDmitry Yakunin 	    ext & (1 << (INET_DIAG_TCLASS - 1))) {
16183f73c5bSDmitry Yakunin 		u32 classid = 0;
16283f73c5bSDmitry Yakunin 
16383f73c5bSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
16483f73c5bSDmitry Yakunin 		classid = sock_cgroup_classid(&sk->sk_cgrp_data);
16583f73c5bSDmitry Yakunin #endif
16683f73c5bSDmitry Yakunin 		/* Fallback to socket priority if class id isn't set.
16783f73c5bSDmitry Yakunin 		 * Classful qdiscs use it as direct reference to class.
16883f73c5bSDmitry Yakunin 		 * For cgroup2 classid is always zero.
16983f73c5bSDmitry Yakunin 		 */
17083f73c5bSDmitry Yakunin 		if (!classid)
17110bbf165SEric Dumazet 			classid = READ_ONCE(sk->sk_priority);
17283f73c5bSDmitry Yakunin 
17383f73c5bSDmitry Yakunin 		if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
17483f73c5bSDmitry Yakunin 			goto errout;
17583f73c5bSDmitry Yakunin 	}
17683f73c5bSDmitry Yakunin 
1776e3a401fSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
1786e3a401fSDmitry Yakunin 	if (nla_put_u64_64bit(skb, INET_DIAG_CGROUP_ID,
1796e3a401fSDmitry Yakunin 			      cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)),
1806e3a401fSDmitry Yakunin 			      INET_DIAG_PAD))
1816e3a401fSDmitry Yakunin 		goto errout;
1826e3a401fSDmitry Yakunin #endif
1836e3a401fSDmitry Yakunin 
184d06ca956SEric W. Biederman 	r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1853c4d05c8SPavel Emelyanov 	r->idiag_inode = sock_i_ino(sk);
1863c4d05c8SPavel Emelyanov 
187c1077616SWei Wang 	memset(&inet_sockopt, 0, sizeof(inet_sockopt));
1886b5f43eaSEric Dumazet 	inet_sockopt.recverr	= inet_test_bit(RECVERR, sk);
189b1c0356aSEric Dumazet 	inet_sockopt.is_icsk	= inet_test_bit(IS_ICSK, sk);
1903f7e7532SEric Dumazet 	inet_sockopt.freebind	= inet_test_bit(FREEBIND, sk);
191cafbe182SEric Dumazet 	inet_sockopt.hdrincl	= inet_test_bit(HDRINCL, sk);
192b09bde5cSEric Dumazet 	inet_sockopt.mc_loop	= inet_test_bit(MC_LOOP, sk);
1934bd0623fSEric Dumazet 	inet_sockopt.transparent = inet_test_bit(TRANSPARENT, sk);
194307b4ac6SEric Dumazet 	inet_sockopt.mc_all	= inet_test_bit(MC_ALL, sk);
195f04b8d34SEric Dumazet 	inet_sockopt.nodefrag	= inet_test_bit(NODEFRAG, sk);
196ca571e2eSEric Dumazet 	inet_sockopt.bind_address_no_port = inet_test_bit(BIND_ADDRESS_NO_PORT, sk);
1978e8cfb11SEric Dumazet 	inet_sockopt.recverr_rfc4884 = inet_test_bit(RECVERR_RFC4884, sk);
19808e39c0dSEric Dumazet 	inet_sockopt.defer_connect = inet_test_bit(DEFER_CONNECT, sk);
199c1077616SWei Wang 	if (nla_put(skb, INET_DIAG_SOCKOPT, sizeof(inet_sockopt),
200c1077616SWei Wang 		    &inet_sockopt))
201c1077616SWei Wang 		goto errout;
202c1077616SWei Wang 
203cb2050a7SXin Long 	return 0;
204cb2050a7SXin Long errout:
205cb2050a7SXin Long 	return 1;
206cb2050a7SXin Long }
207cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
208cb2050a7SXin Long 
inet_diag_parse_attrs(const struct nlmsghdr * nlh,int hdrlen,struct nlattr ** req_nlas)209d5e4d0a5SEric Dumazet static int inet_diag_parse_attrs(const struct nlmsghdr *nlh, int hdrlen,
2103f935c75SPaolo Abeni 				 struct nlattr **req_nlas)
2113f935c75SPaolo Abeni {
2123f935c75SPaolo Abeni 	struct nlattr *nla;
2133f935c75SPaolo Abeni 	int remaining;
2143f935c75SPaolo Abeni 
2153f935c75SPaolo Abeni 	nlmsg_for_each_attr(nla, nlh, hdrlen, remaining) {
2163f935c75SPaolo Abeni 		int type = nla_type(nla);
2173f935c75SPaolo Abeni 
218d5e4d0a5SEric Dumazet 		if (type == INET_DIAG_REQ_PROTOCOL && nla_len(nla) != sizeof(u32))
219d5e4d0a5SEric Dumazet 			return -EINVAL;
220d5e4d0a5SEric Dumazet 
2213f935c75SPaolo Abeni 		if (type < __INET_DIAG_REQ_MAX)
2223f935c75SPaolo Abeni 			req_nlas[type] = nla;
2233f935c75SPaolo Abeni 	}
224d5e4d0a5SEric Dumazet 	return 0;
2253f935c75SPaolo Abeni }
2263f935c75SPaolo Abeni 
inet_diag_get_protocol(const struct inet_diag_req_v2 * req,const struct inet_diag_dump_data * data)2273f935c75SPaolo Abeni static int inet_diag_get_protocol(const struct inet_diag_req_v2 *req,
2283f935c75SPaolo Abeni 				  const struct inet_diag_dump_data *data)
2293f935c75SPaolo Abeni {
2303f935c75SPaolo Abeni 	if (data->req_nlas[INET_DIAG_REQ_PROTOCOL])
2313f935c75SPaolo Abeni 		return nla_get_u32(data->req_nlas[INET_DIAG_REQ_PROTOCOL]);
2323f935c75SPaolo Abeni 	return req->sdiag_protocol;
2333f935c75SPaolo Abeni }
2343f935c75SPaolo Abeni 
235085c20caSMartin KaFai Lau #define MAX_DUMP_ALLOC_SIZE (KMALLOC_MAX_SIZE - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
236085c20caSMartin KaFai Lau 
inet_sk_diag_fill(struct sock * sk,struct inet_connection_sock * icsk,struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * req,u16 nlmsg_flags,bool net_admin)237cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
2385682d393SMartin KaFai Lau 		      struct sk_buff *skb, struct netlink_callback *cb,
2395682d393SMartin KaFai Lau 		      const struct inet_diag_req_v2 *req,
2405682d393SMartin KaFai Lau 		      u16 nlmsg_flags, bool net_admin)
241cb2050a7SXin Long {
242cb2050a7SXin Long 	const struct tcp_congestion_ops *ca_ops;
243cb2050a7SXin Long 	const struct inet_diag_handler *handler;
244085c20caSMartin KaFai Lau 	struct inet_diag_dump_data *cb_data;
245cb2050a7SXin Long 	int ext = req->idiag_ext;
246cb2050a7SXin Long 	struct inet_diag_msg *r;
247cb2050a7SXin Long 	struct nlmsghdr  *nlh;
248cb2050a7SXin Long 	struct nlattr *attr;
249cb2050a7SXin Long 	void *info = NULL;
250223f5519SEric Dumazet 	int protocol;
251cb2050a7SXin Long 
252085c20caSMartin KaFai Lau 	cb_data = cb->data;
253223f5519SEric Dumazet 	protocol = inet_diag_get_protocol(req, cb_data);
254223f5519SEric Dumazet 
255223f5519SEric Dumazet 	/* inet_diag_lock_handler() made sure inet_diag_table[] is stable. */
256223f5519SEric Dumazet 	handler = rcu_dereference_protected(inet_diag_table[protocol], 1);
257223f5519SEric Dumazet 	DEBUG_NET_WARN_ON_ONCE(!handler);
258223f5519SEric Dumazet 	if (!handler)
259223f5519SEric Dumazet 		return -ENXIO;
260cb2050a7SXin Long 
2615682d393SMartin KaFai Lau 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2625682d393SMartin KaFai Lau 			cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
263cb2050a7SXin Long 	if (!nlh)
264cb2050a7SXin Long 		return -EMSGSIZE;
265cb2050a7SXin Long 
266cb2050a7SXin Long 	r = nlmsg_data(nlh);
267cb2050a7SXin Long 	BUG_ON(!sk_fullsock(sk));
268cb2050a7SXin Long 
269cb2050a7SXin Long 	inet_diag_msg_common_fill(r, sk);
270cb2050a7SXin Long 	r->idiag_state = sk->sk_state;
271cb2050a7SXin Long 	r->idiag_timer = 0;
272cb2050a7SXin Long 	r->idiag_retrans = 0;
27371ddeac8SEric Dumazet 	r->idiag_expires = 0;
274cb2050a7SXin Long 
2755682d393SMartin KaFai Lau 	if (inet_diag_msg_attrs_fill(sk, skb, r, ext,
2765682d393SMartin KaFai Lau 				     sk_user_ns(NETLINK_CB(cb->skb).sk),
2775682d393SMartin KaFai Lau 				     net_admin))
278cb2050a7SXin Long 		goto errout;
279cb2050a7SXin Long 
2806e277ed5SThomas Graf 	if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
2816e277ed5SThomas Graf 		struct inet_diag_meminfo minfo = {
2826e277ed5SThomas Graf 			.idiag_rmem = sk_rmem_alloc_get(sk),
283ab4e846aSEric Dumazet 			.idiag_wmem = READ_ONCE(sk->sk_wmem_queued),
284292e6077SPaolo Abeni 			.idiag_fmem = sk_forward_alloc_get(sk),
2856e277ed5SThomas Graf 			.idiag_tmem = sk_wmem_alloc_get(sk),
2866e277ed5SThomas Graf 		};
2876e277ed5SThomas Graf 
2886e277ed5SThomas Graf 		if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
2896e277ed5SThomas Graf 			goto errout;
2903c4d05c8SPavel Emelyanov 	}
2913c4d05c8SPavel Emelyanov 
292c0636faaSPavel Emelyanov 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
293c0636faaSPavel Emelyanov 		if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
2946e277ed5SThomas Graf 			goto errout;
295c0636faaSPavel Emelyanov 
296432490f9SCyrill Gorcunov 	/*
297432490f9SCyrill Gorcunov 	 * RAW sockets might have user-defined protocols assigned,
298432490f9SCyrill Gorcunov 	 * so report the one supplied on socket creation.
299432490f9SCyrill Gorcunov 	 */
300432490f9SCyrill Gorcunov 	if (sk->sk_type == SOCK_RAW) {
301432490f9SCyrill Gorcunov 		if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
302432490f9SCyrill Gorcunov 			goto errout;
303432490f9SCyrill Gorcunov 	}
304432490f9SCyrill Gorcunov 
305e31c5e0eSEric Dumazet 	if (!icsk) {
30662ad6fcdSShan Wei 		handler->idiag_get_info(sk, r, NULL);
3073c4d05c8SPavel Emelyanov 		goto out;
3083c4d05c8SPavel Emelyanov 	}
3093c4d05c8SPavel Emelyanov 
3106ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
31157dde7f7SYuchung Cheng 	    icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
3126ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
313a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 1;
314a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_retransmits;
315b7de529cSXin Long 		r->idiag_expires =
3163828a93fSEric Dumazet 			jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
317a8c2190eSArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
318a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 4;
319a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
320b7de529cSXin Long 		r->idiag_expires =
3213828a93fSEric Dumazet 			jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
322a8c2190eSArnaldo Carvalho de Melo 	} else if (timer_pending(&sk->sk_timer)) {
323a8c2190eSArnaldo Carvalho de Melo 		r->idiag_timer = 2;
324a8c2190eSArnaldo Carvalho de Melo 		r->idiag_retrans = icsk->icsk_probes_out;
325b7de529cSXin Long 		r->idiag_expires =
3263828a93fSEric Dumazet 			jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies);
327a8c2190eSArnaldo Carvalho de Melo 	}
328a8c2190eSArnaldo Carvalho de Melo 
3293fd22af8SCraig Gallek 	if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
3306ed46d12SNicolas Dichtel 		attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
3316ed46d12SNicolas Dichtel 					 handler->idiag_info_size,
3326ed46d12SNicolas Dichtel 					 INET_DIAG_PAD);
3336e277ed5SThomas Graf 		if (!attr)
3346e277ed5SThomas Graf 			goto errout;
335a8c2190eSArnaldo Carvalho de Melo 
3366e277ed5SThomas Graf 		info = nla_data(attr);
337a8c2190eSArnaldo Carvalho de Melo 	}
338a8c2190eSArnaldo Carvalho de Melo 
339521f1cf1SEric Dumazet 	if (ext & (1 << (INET_DIAG_CONG - 1))) {
340521f1cf1SEric Dumazet 		int err = 0;
341521f1cf1SEric Dumazet 
342521f1cf1SEric Dumazet 		rcu_read_lock();
343521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
344521f1cf1SEric Dumazet 		if (ca_ops)
345521f1cf1SEric Dumazet 			err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
346521f1cf1SEric Dumazet 		rcu_read_unlock();
347521f1cf1SEric Dumazet 		if (err < 0)
3486e277ed5SThomas Graf 			goto errout;
349521f1cf1SEric Dumazet 	}
3506e277ed5SThomas Graf 
351a8c2190eSArnaldo Carvalho de Melo 	handler->idiag_get_info(sk, r, info);
352a8c2190eSArnaldo Carvalho de Melo 
353b37e8840SIvan Delalande 	if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
354b37e8840SIvan Delalande 		if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
355b37e8840SIvan Delalande 			goto errout;
356b37e8840SIvan Delalande 
357521f1cf1SEric Dumazet 	if (sk->sk_state < TCP_TIME_WAIT) {
35864f40ff5SEric Dumazet 		union tcp_cc_info info;
35964f40ff5SEric Dumazet 		size_t sz = 0;
36064f40ff5SEric Dumazet 		int attr;
361521f1cf1SEric Dumazet 
362521f1cf1SEric Dumazet 		rcu_read_lock();
363521f1cf1SEric Dumazet 		ca_ops = READ_ONCE(icsk->icsk_ca_ops);
364521f1cf1SEric Dumazet 		if (ca_ops && ca_ops->get_info)
36564f40ff5SEric Dumazet 			sz = ca_ops->get_info(sk, ext, &attr, &info);
366521f1cf1SEric Dumazet 		rcu_read_unlock();
36764f40ff5SEric Dumazet 		if (sz && nla_put(skb, attr, sz, &info) < 0)
368521f1cf1SEric Dumazet 			goto errout;
369521f1cf1SEric Dumazet 	}
370a8c2190eSArnaldo Carvalho de Melo 
371085c20caSMartin KaFai Lau 	/* Keep it at the end for potential retry with a larger skb,
372085c20caSMartin KaFai Lau 	 * or else do best-effort fitting, which is only done for the
373085c20caSMartin KaFai Lau 	 * first_nlmsg.
374085c20caSMartin KaFai Lau 	 */
375085c20caSMartin KaFai Lau 	if (cb_data->bpf_stg_diag) {
376085c20caSMartin KaFai Lau 		bool first_nlmsg = ((unsigned char *)nlh == skb->data);
377085c20caSMartin KaFai Lau 		unsigned int prev_min_dump_alloc;
378085c20caSMartin KaFai Lau 		unsigned int total_nla_size = 0;
379085c20caSMartin KaFai Lau 		unsigned int msg_len;
380085c20caSMartin KaFai Lau 		int err;
381085c20caSMartin KaFai Lau 
382085c20caSMartin KaFai Lau 		msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
383085c20caSMartin KaFai Lau 		err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb,
384085c20caSMartin KaFai Lau 					      INET_DIAG_SK_BPF_STORAGES,
385085c20caSMartin KaFai Lau 					      &total_nla_size);
386085c20caSMartin KaFai Lau 
387085c20caSMartin KaFai Lau 		if (!err)
388085c20caSMartin KaFai Lau 			goto out;
389085c20caSMartin KaFai Lau 
390085c20caSMartin KaFai Lau 		total_nla_size += msg_len;
391085c20caSMartin KaFai Lau 		prev_min_dump_alloc = cb->min_dump_alloc;
392085c20caSMartin KaFai Lau 		if (total_nla_size > prev_min_dump_alloc)
393085c20caSMartin KaFai Lau 			cb->min_dump_alloc = min_t(u32, total_nla_size,
394085c20caSMartin KaFai Lau 						   MAX_DUMP_ALLOC_SIZE);
395085c20caSMartin KaFai Lau 
396085c20caSMartin KaFai Lau 		if (!first_nlmsg)
397085c20caSMartin KaFai Lau 			goto errout;
398085c20caSMartin KaFai Lau 
399085c20caSMartin KaFai Lau 		if (cb->min_dump_alloc > prev_min_dump_alloc)
400085c20caSMartin KaFai Lau 			/* Retry with pskb_expand_head() with
401085c20caSMartin KaFai Lau 			 * __GFP_DIRECT_RECLAIM
402085c20caSMartin KaFai Lau 			 */
403085c20caSMartin KaFai Lau 			goto errout;
404085c20caSMartin KaFai Lau 
405085c20caSMartin KaFai Lau 		WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc);
406085c20caSMartin KaFai Lau 
407085c20caSMartin KaFai Lau 		/* Send what we have for this sk
408085c20caSMartin KaFai Lau 		 * and move on to the next sk in the following
409085c20caSMartin KaFai Lau 		 * dump()
410085c20caSMartin KaFai Lau 		 */
411085c20caSMartin KaFai Lau 	}
412085c20caSMartin KaFai Lau 
4133c4d05c8SPavel Emelyanov out:
414053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
415053c095aSJohannes Berg 	return 0;
416a8c2190eSArnaldo Carvalho de Melo 
4176e277ed5SThomas Graf errout:
4186e277ed5SThomas Graf 	nlmsg_cancel(skb, nlh);
41926932566SPatrick McHardy 	return -EMSGSIZE;
420a8c2190eSArnaldo Carvalho de Melo }
4213c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
4223c4d05c8SPavel Emelyanov 
inet_twsk_diag_fill(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,u16 nlmsg_flags,bool net_admin)42333cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk,
424e31c5e0eSEric Dumazet 			       struct sk_buff *skb,
4255682d393SMartin KaFai Lau 			       struct netlink_callback *cb,
4261b3fc771SMaciej Żenczykowski 			       u16 nlmsg_flags, bool net_admin)
427c7d58aabSArnaldo Carvalho de Melo {
42833cf7c90SEric Dumazet 	struct inet_timewait_sock *tw = inet_twsk(sk);
429c7d58aabSArnaldo Carvalho de Melo 	struct inet_diag_msg *r;
4306e277ed5SThomas Graf 	struct nlmsghdr *nlh;
431789f558cSEric Dumazet 	long tmo;
432c7d58aabSArnaldo Carvalho de Melo 
4335682d393SMartin KaFai Lau 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4345682d393SMartin KaFai Lau 			cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type,
4355682d393SMartin KaFai Lau 			sizeof(*r), nlmsg_flags);
4366e277ed5SThomas Graf 	if (!nlh)
437d106352dSDavid S. Miller 		return -EMSGSIZE;
438d106352dSDavid S. Miller 
439d106352dSDavid S. Miller 	r = nlmsg_data(nlh);
440c7d58aabSArnaldo Carvalho de Melo 	BUG_ON(tw->tw_state != TCP_TIME_WAIT);
441c7d58aabSArnaldo Carvalho de Melo 
442a4458343SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
443c7d58aabSArnaldo Carvalho de Melo 	r->idiag_retrans      = 0;
444b1aac815SDaniel Borkmann 
445*3e5cbbb1SEric Dumazet 	r->idiag_state	      = READ_ONCE(tw->tw_substate);
446c7d58aabSArnaldo Carvalho de Melo 	r->idiag_timer	      = 3;
4473828a93fSEric Dumazet 	tmo = tw->tw_timer.expires - jiffies;
4483828a93fSEric Dumazet 	r->idiag_expires      = jiffies_delta_to_msecs(tmo);
449c7d58aabSArnaldo Carvalho de Melo 	r->idiag_rqueue	      = 0;
450c7d58aabSArnaldo Carvalho de Melo 	r->idiag_wqueue	      = 0;
451c7d58aabSArnaldo Carvalho de Melo 	r->idiag_uid	      = 0;
452c7d58aabSArnaldo Carvalho de Melo 	r->idiag_inode	      = 0;
4536e277ed5SThomas Graf 
4541b3fc771SMaciej Żenczykowski 	if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
4551b3fc771SMaciej Żenczykowski 				     tw->tw_mark)) {
4561b3fc771SMaciej Żenczykowski 		nlmsg_cancel(skb, nlh);
4571b3fc771SMaciej Żenczykowski 		return -EMSGSIZE;
4581b3fc771SMaciej Żenczykowski 	}
4591b3fc771SMaciej Żenczykowski 
460053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
461053c095aSJohannes Berg 	return 0;
462c7d58aabSArnaldo Carvalho de Melo }
463c7d58aabSArnaldo Carvalho de Melo 
inet_req_diag_fill(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,u16 nlmsg_flags,bool net_admin)464a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
4655682d393SMartin KaFai Lau 			      struct netlink_callback *cb,
4665682d393SMartin KaFai Lau 			      u16 nlmsg_flags, bool net_admin)
467a58917f5SEric Dumazet {
468d545cacaSLorenzo Colitti 	struct request_sock *reqsk = inet_reqsk(sk);
469a58917f5SEric Dumazet 	struct inet_diag_msg *r;
470a58917f5SEric Dumazet 	struct nlmsghdr *nlh;
471a58917f5SEric Dumazet 	long tmo;
472a58917f5SEric Dumazet 
4735682d393SMartin KaFai Lau 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4745682d393SMartin KaFai Lau 			cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
475a58917f5SEric Dumazet 	if (!nlh)
476a58917f5SEric Dumazet 		return -EMSGSIZE;
477a58917f5SEric Dumazet 
478a58917f5SEric Dumazet 	r = nlmsg_data(nlh);
479a58917f5SEric Dumazet 	inet_diag_msg_common_fill(r, sk);
480a58917f5SEric Dumazet 	r->idiag_state = TCP_SYN_RECV;
481a58917f5SEric Dumazet 	r->idiag_timer = 1;
482d545cacaSLorenzo Colitti 	r->idiag_retrans = reqsk->num_retrans;
483a58917f5SEric Dumazet 
484a58917f5SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
485a58917f5SEric Dumazet 		     offsetof(struct sock, sk_cookie));
486a58917f5SEric Dumazet 
487fa76ce73SEric Dumazet 	tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
4883828a93fSEric Dumazet 	r->idiag_expires = jiffies_delta_to_msecs(tmo);
489a58917f5SEric Dumazet 	r->idiag_rqueue	= 0;
490a58917f5SEric Dumazet 	r->idiag_wqueue	= 0;
491a58917f5SEric Dumazet 	r->idiag_uid	= 0;
492a58917f5SEric Dumazet 	r->idiag_inode	= 0;
493a58917f5SEric Dumazet 
494d545cacaSLorenzo Colitti 	if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
495e33de7c5SWang Hai 				     inet_rsk(reqsk)->ir_mark)) {
496e33de7c5SWang Hai 		nlmsg_cancel(skb, nlh);
497d545cacaSLorenzo Colitti 		return -EMSGSIZE;
498e33de7c5SWang Hai 	}
499d545cacaSLorenzo Colitti 
500a58917f5SEric Dumazet 	nlmsg_end(skb, nlh);
501a58917f5SEric Dumazet 	return 0;
502a58917f5SEric Dumazet }
503a58917f5SEric Dumazet 
sk_diag_fill(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r,u16 nlmsg_flags,bool net_admin)504dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
5055682d393SMartin KaFai Lau 			struct netlink_callback *cb,
50634160ea3SEric Dumazet 			const struct inet_diag_req_v2 *r,
5075682d393SMartin KaFai Lau 			u16 nlmsg_flags, bool net_admin)
508dff2c035SArnaldo Carvalho de Melo {
509dff2c035SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_TIME_WAIT)
5101b3fc771SMaciej Żenczykowski 		return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
511a58917f5SEric Dumazet 
512a58917f5SEric Dumazet 	if (sk->sk_state == TCP_NEW_SYN_RECV)
5135682d393SMartin KaFai Lau 		return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
514efe4208fSEric Dumazet 
5155682d393SMartin KaFai Lau 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags,
5165682d393SMartin KaFai Lau 				 net_admin);
517dff2c035SArnaldo Carvalho de Melo }
518dff2c035SArnaldo Carvalho de Melo 
inet_diag_find_one_icsk(struct net * net,struct inet_hashinfo * hashinfo,const struct inet_diag_req_v2 * req)519b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net,
520b613f56eSLorenzo Colitti 				     struct inet_hashinfo *hashinfo,
52134160ea3SEric Dumazet 				     const struct inet_diag_req_v2 *req)
522a8c2190eSArnaldo Carvalho de Melo {
523e31c5e0eSEric Dumazet 	struct sock *sk;
524a8c2190eSArnaldo Carvalho de Melo 
5252d331915SEric Dumazet 	rcu_read_lock();
526e31c5e0eSEric Dumazet 	if (req->sdiag_family == AF_INET)
527a583636aSCraig Gallek 		sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
528a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_dport, req->id.idiag_src[0],
529a8c2190eSArnaldo Carvalho de Melo 				 req->id.idiag_sport, req->id.idiag_if);
530dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
5317c130672SEric Dumazet 	else if (req->sdiag_family == AF_INET6) {
5327c130672SEric Dumazet 		if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
5337c130672SEric Dumazet 		    ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
534a583636aSCraig Gallek 			sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
5357c130672SEric Dumazet 					 req->id.idiag_dport, req->id.idiag_src[3],
5367c130672SEric Dumazet 					 req->id.idiag_sport, req->id.idiag_if);
5377c130672SEric Dumazet 		else
538a583636aSCraig Gallek 			sk = inet6_lookup(net, hashinfo, NULL, 0,
539a8c2190eSArnaldo Carvalho de Melo 					  (struct in6_addr *)req->id.idiag_dst,
540a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_dport,
541a8c2190eSArnaldo Carvalho de Melo 					  (struct in6_addr *)req->id.idiag_src,
542a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_sport,
543a8c2190eSArnaldo Carvalho de Melo 					  req->id.idiag_if);
5447c130672SEric Dumazet 	}
545a8c2190eSArnaldo Carvalho de Melo #endif
5462d331915SEric Dumazet 	else {
5472d331915SEric Dumazet 		rcu_read_unlock();
548b613f56eSLorenzo Colitti 		return ERR_PTR(-EINVAL);
5492d331915SEric Dumazet 	}
5502d331915SEric Dumazet 	rcu_read_unlock();
551e31c5e0eSEric Dumazet 	if (!sk)
552b613f56eSLorenzo Colitti 		return ERR_PTR(-ENOENT);
553a8c2190eSArnaldo Carvalho de Melo 
554b613f56eSLorenzo Colitti 	if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
555b613f56eSLorenzo Colitti 		sock_gen_put(sk);
556b613f56eSLorenzo Colitti 		return ERR_PTR(-ENOENT);
557b613f56eSLorenzo Colitti 	}
558b613f56eSLorenzo Colitti 
559b613f56eSLorenzo Colitti 	return sk;
560b613f56eSLorenzo Colitti }
561b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
562b613f56eSLorenzo Colitti 
inet_diag_dump_one_icsk(struct inet_hashinfo * hashinfo,struct netlink_callback * cb,const struct inet_diag_req_v2 * req)563b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
5645682d393SMartin KaFai Lau 			    struct netlink_callback *cb,
565b613f56eSLorenzo Colitti 			    const struct inet_diag_req_v2 *req)
566b613f56eSLorenzo Colitti {
5675682d393SMartin KaFai Lau 	struct sk_buff *in_skb = cb->skb;
568b37e8840SIvan Delalande 	bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
569b613f56eSLorenzo Colitti 	struct net *net = sock_net(in_skb->sk);
570b613f56eSLorenzo Colitti 	struct sk_buff *rep;
571b613f56eSLorenzo Colitti 	struct sock *sk;
572b613f56eSLorenzo Colitti 	int err;
573b613f56eSLorenzo Colitti 
574b613f56eSLorenzo Colitti 	sk = inet_diag_find_one_icsk(net, hashinfo, req);
575b613f56eSLorenzo Colitti 	if (IS_ERR(sk))
576b613f56eSLorenzo Colitti 		return PTR_ERR(sk);
577a8c2190eSArnaldo Carvalho de Melo 
578b37e8840SIvan Delalande 	rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
5796e277ed5SThomas Graf 	if (!rep) {
5806e277ed5SThomas Graf 		err = -ENOMEM;
581a8c2190eSArnaldo Carvalho de Melo 		goto out;
5826e277ed5SThomas Graf 	}
583a8c2190eSArnaldo Carvalho de Melo 
5845682d393SMartin KaFai Lau 	err = sk_diag_fill(sk, rep, cb, req, 0, net_admin);
58526932566SPatrick McHardy 	if (err < 0) {
58626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
5876e277ed5SThomas Graf 		nlmsg_free(rep);
58826932566SPatrick McHardy 		goto out;
58926932566SPatrick McHardy 	}
59001757f53SYajun Deng 	err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
591a8c2190eSArnaldo Carvalho de Melo 
592a8c2190eSArnaldo Carvalho de Melo out:
593c1d607ccSEric Dumazet 	if (sk)
594c1d607ccSEric Dumazet 		sock_gen_put(sk);
595c1d607ccSEric Dumazet 
596476f7dbfSPavel Emelyanov 	return err;
597476f7dbfSPavel Emelyanov }
5981942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
599476f7dbfSPavel Emelyanov 
inet_diag_cmd_exact(int cmd,struct sk_buff * in_skb,const struct nlmsghdr * nlh,int hdrlen,const struct inet_diag_req_v2 * req)6006eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
601476f7dbfSPavel Emelyanov 			       const struct nlmsghdr *nlh,
6023f935c75SPaolo Abeni 			       int hdrlen,
60334160ea3SEric Dumazet 			       const struct inet_diag_req_v2 *req)
604476f7dbfSPavel Emelyanov {
605476f7dbfSPavel Emelyanov 	const struct inet_diag_handler *handler;
6063f935c75SPaolo Abeni 	struct inet_diag_dump_data dump_data;
6073f935c75SPaolo Abeni 	int err, protocol;
608476f7dbfSPavel Emelyanov 
6093f935c75SPaolo Abeni 	memset(&dump_data, 0, sizeof(dump_data));
610d5e4d0a5SEric Dumazet 	err = inet_diag_parse_attrs(nlh, hdrlen, dump_data.req_nlas);
611d5e4d0a5SEric Dumazet 	if (err)
612d5e4d0a5SEric Dumazet 		return err;
613d5e4d0a5SEric Dumazet 
6143f935c75SPaolo Abeni 	protocol = inet_diag_get_protocol(req, &dump_data);
6153f935c75SPaolo Abeni 
6163f935c75SPaolo Abeni 	handler = inet_diag_lock_handler(protocol);
617223f5519SEric Dumazet 	if (!handler)
618223f5519SEric Dumazet 		return -ENOENT;
619223f5519SEric Dumazet 
620223f5519SEric Dumazet 	if (cmd == SOCK_DIAG_BY_FAMILY) {
6215682d393SMartin KaFai Lau 		struct netlink_callback cb = {
6225682d393SMartin KaFai Lau 			.nlh = nlh,
6235682d393SMartin KaFai Lau 			.skb = in_skb,
6243f935c75SPaolo Abeni 			.data = &dump_data,
6255682d393SMartin KaFai Lau 		};
6265682d393SMartin KaFai Lau 		err = handler->dump_one(&cb, req);
6275682d393SMartin KaFai Lau 	} else if (cmd == SOCK_DESTROY && handler->destroy) {
6286eb5d2e0SLorenzo Colitti 		err = handler->destroy(in_skb, req);
6295682d393SMartin KaFai Lau 	} else {
6306eb5d2e0SLorenzo Colitti 		err = -EOPNOTSUPP;
6315682d393SMartin KaFai Lau 	}
632d523a328SHerbert Xu 	inet_diag_unlock_handler(handler);
633476f7dbfSPavel Emelyanov 
634a8c2190eSArnaldo Carvalho de Melo 	return err;
635a8c2190eSArnaldo Carvalho de Melo }
636a8c2190eSArnaldo Carvalho de Melo 
bitstring_match(const __be32 * a1,const __be32 * a2,int bits)6379f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
638a8c2190eSArnaldo Carvalho de Melo {
639a8c2190eSArnaldo Carvalho de Melo 	int words = bits >> 5;
640a8c2190eSArnaldo Carvalho de Melo 
641a8c2190eSArnaldo Carvalho de Melo 	bits &= 0x1f;
642a8c2190eSArnaldo Carvalho de Melo 
643a8c2190eSArnaldo Carvalho de Melo 	if (words) {
644a8c2190eSArnaldo Carvalho de Melo 		if (memcmp(a1, a2, words << 2))
645a8c2190eSArnaldo Carvalho de Melo 			return 0;
646a8c2190eSArnaldo Carvalho de Melo 	}
647a8c2190eSArnaldo Carvalho de Melo 	if (bits) {
6489f855299SAl Viro 		__be32 w1, w2;
6499f855299SAl Viro 		__be32 mask;
650a8c2190eSArnaldo Carvalho de Melo 
651a8c2190eSArnaldo Carvalho de Melo 		w1 = a1[words];
652a8c2190eSArnaldo Carvalho de Melo 		w2 = a2[words];
653a8c2190eSArnaldo Carvalho de Melo 
654a8c2190eSArnaldo Carvalho de Melo 		mask = htonl((0xffffffff) << (32 - bits));
655a8c2190eSArnaldo Carvalho de Melo 
656a8c2190eSArnaldo Carvalho de Melo 		if ((w1 ^ w2) & mask)
657a8c2190eSArnaldo Carvalho de Melo 			return 0;
658a8c2190eSArnaldo Carvalho de Melo 	}
659a8c2190eSArnaldo Carvalho de Melo 
660a8c2190eSArnaldo Carvalho de Melo 	return 1;
661a8c2190eSArnaldo Carvalho de Melo }
662a8c2190eSArnaldo Carvalho de Melo 
inet_diag_bc_run(const struct nlattr * _bc,const struct inet_diag_entry * entry)66387c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
664a8c2190eSArnaldo Carvalho de Melo 			    const struct inet_diag_entry *entry)
665a8c2190eSArnaldo Carvalho de Melo {
66687c22ea5SPavel Emelyanov 	const void *bc = nla_data(_bc);
66787c22ea5SPavel Emelyanov 	int len = nla_len(_bc);
66887c22ea5SPavel Emelyanov 
669a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
670a8c2190eSArnaldo Carvalho de Melo 		int yes = 1;
671a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
672a8c2190eSArnaldo Carvalho de Melo 
673a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
674a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
675a8c2190eSArnaldo Carvalho de Melo 			break;
676a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_JMP:
677a8c2190eSArnaldo Carvalho de Melo 			yes = 0;
678a8c2190eSArnaldo Carvalho de Melo 			break;
679bbb6189dSKristian Evensen 		case INET_DIAG_BC_S_EQ:
680bbb6189dSKristian Evensen 			yes = entry->sport == op[1].no;
681bbb6189dSKristian Evensen 			break;
682a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
683a8c2190eSArnaldo Carvalho de Melo 			yes = entry->sport >= op[1].no;
684a8c2190eSArnaldo Carvalho de Melo 			break;
685a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
686b4ced2b7SRoel Kluin 			yes = entry->sport <= op[1].no;
687a8c2190eSArnaldo Carvalho de Melo 			break;
688bbb6189dSKristian Evensen 		case INET_DIAG_BC_D_EQ:
689bbb6189dSKristian Evensen 			yes = entry->dport == op[1].no;
690bbb6189dSKristian Evensen 			break;
691a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
692a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport >= op[1].no;
693a8c2190eSArnaldo Carvalho de Melo 			break;
694a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
695a8c2190eSArnaldo Carvalho de Melo 			yes = entry->dport <= op[1].no;
696a8c2190eSArnaldo Carvalho de Melo 			break;
697a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_AUTO:
698a8c2190eSArnaldo Carvalho de Melo 			yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
699a8c2190eSArnaldo Carvalho de Melo 			break;
700a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
701a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND: {
702e31c5e0eSEric Dumazet 			const struct inet_diag_hostcond *cond;
703e31c5e0eSEric Dumazet 			const __be32 *addr;
704a8c2190eSArnaldo Carvalho de Melo 
705e31c5e0eSEric Dumazet 			cond = (const struct inet_diag_hostcond *)(op + 1);
706a8c2190eSArnaldo Carvalho de Melo 			if (cond->port != -1 &&
707a8c2190eSArnaldo Carvalho de Melo 			    cond->port != (op->code == INET_DIAG_BC_S_COND ?
708a8c2190eSArnaldo Carvalho de Melo 					     entry->sport : entry->dport)) {
709a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
710a8c2190eSArnaldo Carvalho de Melo 				break;
711a8c2190eSArnaldo Carvalho de Melo 			}
712a8c2190eSArnaldo Carvalho de Melo 
713a8c2190eSArnaldo Carvalho de Melo 			if (op->code == INET_DIAG_BC_S_COND)
714a8c2190eSArnaldo Carvalho de Melo 				addr = entry->saddr;
715a8c2190eSArnaldo Carvalho de Melo 			else
716a8c2190eSArnaldo Carvalho de Melo 				addr = entry->daddr;
717a8c2190eSArnaldo Carvalho de Melo 
718f67caec9SNeal Cardwell 			if (cond->family != AF_UNSPEC &&
719f67caec9SNeal Cardwell 			    cond->family != entry->family) {
720a8c2190eSArnaldo Carvalho de Melo 				if (entry->family == AF_INET6 &&
721a8c2190eSArnaldo Carvalho de Melo 				    cond->family == AF_INET) {
722a8c2190eSArnaldo Carvalho de Melo 					if (addr[0] == 0 && addr[1] == 0 &&
723a8c2190eSArnaldo Carvalho de Melo 					    addr[2] == htonl(0xffff) &&
724f67caec9SNeal Cardwell 					    bitstring_match(addr + 3,
725f67caec9SNeal Cardwell 							    cond->addr,
726a8c2190eSArnaldo Carvalho de Melo 							    cond->prefix_len))
727a8c2190eSArnaldo Carvalho de Melo 						break;
728a8c2190eSArnaldo Carvalho de Melo 				}
729a8c2190eSArnaldo Carvalho de Melo 				yes = 0;
730a8c2190eSArnaldo Carvalho de Melo 				break;
731a8c2190eSArnaldo Carvalho de Melo 			}
732f67caec9SNeal Cardwell 
733f67caec9SNeal Cardwell 			if (cond->prefix_len == 0)
734f67caec9SNeal Cardwell 				break;
735f67caec9SNeal Cardwell 			if (bitstring_match(addr, cond->addr,
736f67caec9SNeal Cardwell 					    cond->prefix_len))
737f67caec9SNeal Cardwell 				break;
738f67caec9SNeal Cardwell 			yes = 0;
739f67caec9SNeal Cardwell 			break;
740f67caec9SNeal Cardwell 		}
741637c841dSDavid Ahern 		case INET_DIAG_BC_DEV_COND: {
742637c841dSDavid Ahern 			u32 ifindex;
743637c841dSDavid Ahern 
744637c841dSDavid Ahern 			ifindex = *((const u32 *)(op + 1));
745637c841dSDavid Ahern 			if (ifindex != entry->ifindex)
746637c841dSDavid Ahern 				yes = 0;
747637c841dSDavid Ahern 			break;
748637c841dSDavid Ahern 		}
749a52e95abSLorenzo Colitti 		case INET_DIAG_BC_MARK_COND: {
750a52e95abSLorenzo Colitti 			struct inet_diag_markcond *cond;
751a52e95abSLorenzo Colitti 
752a52e95abSLorenzo Colitti 			cond = (struct inet_diag_markcond *)(op + 1);
753a52e95abSLorenzo Colitti 			if ((entry->mark & cond->mask) != cond->mark)
754a52e95abSLorenzo Colitti 				yes = 0;
755a52e95abSLorenzo Colitti 			break;
756a52e95abSLorenzo Colitti 		}
757b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
758b1f3e43dSDmitry Yakunin 		case INET_DIAG_BC_CGROUP_COND: {
759b1f3e43dSDmitry Yakunin 			u64 cgroup_id;
760b1f3e43dSDmitry Yakunin 
761b1f3e43dSDmitry Yakunin 			cgroup_id = get_unaligned((const u64 *)(op + 1));
762b1f3e43dSDmitry Yakunin 			if (cgroup_id != entry->cgroup_id)
763b1f3e43dSDmitry Yakunin 				yes = 0;
764b1f3e43dSDmitry Yakunin 			break;
765b1f3e43dSDmitry Yakunin 		}
766b1f3e43dSDmitry Yakunin #endif
767a8c2190eSArnaldo Carvalho de Melo 		}
768a8c2190eSArnaldo Carvalho de Melo 
769a8c2190eSArnaldo Carvalho de Melo 		if (yes) {
770a8c2190eSArnaldo Carvalho de Melo 			len -= op->yes;
771a8c2190eSArnaldo Carvalho de Melo 			bc += op->yes;
772a8c2190eSArnaldo Carvalho de Melo 		} else {
773a8c2190eSArnaldo Carvalho de Melo 			len -= op->no;
774a8c2190eSArnaldo Carvalho de Melo 			bc += op->no;
775a8c2190eSArnaldo Carvalho de Melo 		}
776a8c2190eSArnaldo Carvalho de Melo 	}
777a02cec21SEric Dumazet 	return len == 0;
778a8c2190eSArnaldo Carvalho de Melo }
779a8c2190eSArnaldo Carvalho de Melo 
780a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
781a4458343SEric Dumazet  */
entry_fill_addrs(struct inet_diag_entry * entry,const struct sock * sk)782a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry,
783a4458343SEric Dumazet 			     const struct sock *sk)
784a4458343SEric Dumazet {
785a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
786a4458343SEric Dumazet 	if (sk->sk_family == AF_INET6) {
787a4458343SEric Dumazet 		entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
788a4458343SEric Dumazet 		entry->daddr = sk->sk_v6_daddr.s6_addr32;
789a4458343SEric Dumazet 	} else
790a4458343SEric Dumazet #endif
791a4458343SEric Dumazet 	{
792a4458343SEric Dumazet 		entry->saddr = &sk->sk_rcv_saddr;
793a4458343SEric Dumazet 		entry->daddr = &sk->sk_daddr;
794a4458343SEric Dumazet 	}
795a4458343SEric Dumazet }
796a4458343SEric Dumazet 
inet_diag_bc_sk(const struct nlattr * bc,struct sock * sk)7978d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
7988d07d151SPavel Emelyanov {
7998d07d151SPavel Emelyanov 	struct inet_sock *inet = inet_sk(sk);
800e31c5e0eSEric Dumazet 	struct inet_diag_entry entry;
8018d07d151SPavel Emelyanov 
802e31c5e0eSEric Dumazet 	if (!bc)
8038d07d151SPavel Emelyanov 		return 1;
8048d07d151SPavel Emelyanov 
8058d07d151SPavel Emelyanov 	entry.family = sk->sk_family;
806a4458343SEric Dumazet 	entry_fill_addrs(&entry, sk);
8078d07d151SPavel Emelyanov 	entry.sport = inet->inet_num;
8088d07d151SPavel Emelyanov 	entry.dport = ntohs(inet->inet_dport);
809637c841dSDavid Ahern 	entry.ifindex = sk->sk_bound_dev_if;
810a58917f5SEric Dumazet 	entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
811a52e95abSLorenzo Colitti 	if (sk_fullsock(sk))
8123c5b4d69SEric Dumazet 		entry.mark = READ_ONCE(sk->sk_mark);
813a52e95abSLorenzo Colitti 	else if (sk->sk_state == TCP_NEW_SYN_RECV)
814a52e95abSLorenzo Colitti 		entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
8151b3fc771SMaciej Żenczykowski 	else if (sk->sk_state == TCP_TIME_WAIT)
8161b3fc771SMaciej Żenczykowski 		entry.mark = inet_twsk(sk)->tw_mark;
817a52e95abSLorenzo Colitti 	else
818a52e95abSLorenzo Colitti 		entry.mark = 0;
819b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
820ee1bd483SDmitry Yakunin 	entry.cgroup_id = sk_fullsock(sk) ?
821ee1bd483SDmitry Yakunin 		cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)) : 0;
822b1f3e43dSDmitry Yakunin #endif
8238d07d151SPavel Emelyanov 
8248d07d151SPavel Emelyanov 	return inet_diag_bc_run(bc, &entry);
8258d07d151SPavel Emelyanov }
8268d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
8278d07d151SPavel Emelyanov 
valid_cc(const void * bc,int len,int cc)828a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
829a8c2190eSArnaldo Carvalho de Melo {
830a8c2190eSArnaldo Carvalho de Melo 	while (len >= 0) {
831a8c2190eSArnaldo Carvalho de Melo 		const struct inet_diag_bc_op *op = bc;
832a8c2190eSArnaldo Carvalho de Melo 
833a8c2190eSArnaldo Carvalho de Melo 		if (cc > len)
834a8c2190eSArnaldo Carvalho de Melo 			return 0;
835a8c2190eSArnaldo Carvalho de Melo 		if (cc == len)
836a8c2190eSArnaldo Carvalho de Melo 			return 1;
837eeb14972SEric Dumazet 		if (op->yes < 4 || op->yes & 3)
838a8c2190eSArnaldo Carvalho de Melo 			return 0;
839a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
840a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
841a8c2190eSArnaldo Carvalho de Melo 	}
842a8c2190eSArnaldo Carvalho de Melo 	return 0;
843a8c2190eSArnaldo Carvalho de Melo }
844a8c2190eSArnaldo Carvalho de Melo 
845637c841dSDavid Ahern /* data is u32 ifindex */
valid_devcond(const struct inet_diag_bc_op * op,int len,int * min_len)846637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
847637c841dSDavid Ahern 			  int *min_len)
848637c841dSDavid Ahern {
849637c841dSDavid Ahern 	/* Check ifindex space. */
850637c841dSDavid Ahern 	*min_len += sizeof(u32);
851637c841dSDavid Ahern 	if (len < *min_len)
852637c841dSDavid Ahern 		return false;
853637c841dSDavid Ahern 
854637c841dSDavid Ahern 	return true;
855637c841dSDavid Ahern }
856405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
valid_hostcond(const struct inet_diag_bc_op * op,int len,int * min_len)857405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
858405c0059SNeal Cardwell 			   int *min_len)
859405c0059SNeal Cardwell {
860405c0059SNeal Cardwell 	struct inet_diag_hostcond *cond;
861e31c5e0eSEric Dumazet 	int addr_len;
862405c0059SNeal Cardwell 
863405c0059SNeal Cardwell 	/* Check hostcond space. */
864405c0059SNeal Cardwell 	*min_len += sizeof(struct inet_diag_hostcond);
865405c0059SNeal Cardwell 	if (len < *min_len)
866405c0059SNeal Cardwell 		return false;
867405c0059SNeal Cardwell 	cond = (struct inet_diag_hostcond *)(op + 1);
868405c0059SNeal Cardwell 
869405c0059SNeal Cardwell 	/* Check address family and address length. */
870405c0059SNeal Cardwell 	switch (cond->family) {
871405c0059SNeal Cardwell 	case AF_UNSPEC:
872405c0059SNeal Cardwell 		addr_len = 0;
873405c0059SNeal Cardwell 		break;
874405c0059SNeal Cardwell 	case AF_INET:
875405c0059SNeal Cardwell 		addr_len = sizeof(struct in_addr);
876405c0059SNeal Cardwell 		break;
877405c0059SNeal Cardwell 	case AF_INET6:
878405c0059SNeal Cardwell 		addr_len = sizeof(struct in6_addr);
879405c0059SNeal Cardwell 		break;
880405c0059SNeal Cardwell 	default:
881405c0059SNeal Cardwell 		return false;
882405c0059SNeal Cardwell 	}
883405c0059SNeal Cardwell 	*min_len += addr_len;
884405c0059SNeal Cardwell 	if (len < *min_len)
885405c0059SNeal Cardwell 		return false;
886405c0059SNeal Cardwell 
887405c0059SNeal Cardwell 	/* Check prefix length (in bits) vs address length (in bytes). */
888405c0059SNeal Cardwell 	if (cond->prefix_len > 8 * addr_len)
889405c0059SNeal Cardwell 		return false;
890405c0059SNeal Cardwell 
891405c0059SNeal Cardwell 	return true;
892405c0059SNeal Cardwell }
893405c0059SNeal Cardwell 
8945e1f5420SNeal Cardwell /* Validate a port comparison operator. */
valid_port_comparison(const struct inet_diag_bc_op * op,int len,int * min_len)895e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op,
8965e1f5420SNeal Cardwell 				  int len, int *min_len)
8975e1f5420SNeal Cardwell {
8985e1f5420SNeal Cardwell 	/* Port comparisons put the port in a follow-on inet_diag_bc_op. */
8995e1f5420SNeal Cardwell 	*min_len += sizeof(struct inet_diag_bc_op);
9005e1f5420SNeal Cardwell 	if (len < *min_len)
9015e1f5420SNeal Cardwell 		return false;
9025e1f5420SNeal Cardwell 	return true;
9035e1f5420SNeal Cardwell }
9045e1f5420SNeal Cardwell 
valid_markcond(const struct inet_diag_bc_op * op,int len,int * min_len)905a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
906a52e95abSLorenzo Colitti 			   int *min_len)
907a8c2190eSArnaldo Carvalho de Melo {
908a52e95abSLorenzo Colitti 	*min_len += sizeof(struct inet_diag_markcond);
909a52e95abSLorenzo Colitti 	return len >= *min_len;
910a52e95abSLorenzo Colitti }
911a52e95abSLorenzo Colitti 
912b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
valid_cgroupcond(const struct inet_diag_bc_op * op,int len,int * min_len)913b1f3e43dSDmitry Yakunin static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len,
914b1f3e43dSDmitry Yakunin 			     int *min_len)
915b1f3e43dSDmitry Yakunin {
916b1f3e43dSDmitry Yakunin 	*min_len += sizeof(u64);
917b1f3e43dSDmitry Yakunin 	return len >= *min_len;
918b1f3e43dSDmitry Yakunin }
919b1f3e43dSDmitry Yakunin #endif
920b1f3e43dSDmitry Yakunin 
inet_diag_bc_audit(const struct nlattr * attr,const struct sk_buff * skb)921a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr,
922a52e95abSLorenzo Colitti 			      const struct sk_buff *skb)
923a52e95abSLorenzo Colitti {
924a52e95abSLorenzo Colitti 	bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
925627cc4adSLorenzo Colitti 	const void *bytecode, *bc;
926627cc4adSLorenzo Colitti 	int bytecode_len, len;
927627cc4adSLorenzo Colitti 
928627cc4adSLorenzo Colitti 	if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
929627cc4adSLorenzo Colitti 		return -EINVAL;
930627cc4adSLorenzo Colitti 
931627cc4adSLorenzo Colitti 	bytecode = bc = nla_data(attr);
932627cc4adSLorenzo Colitti 	len = bytecode_len = nla_len(attr);
933a8c2190eSArnaldo Carvalho de Melo 
934a8c2190eSArnaldo Carvalho de Melo 	while (len > 0) {
935405c0059SNeal Cardwell 		int min_len = sizeof(struct inet_diag_bc_op);
936e31c5e0eSEric Dumazet 		const struct inet_diag_bc_op *op = bc;
937a8c2190eSArnaldo Carvalho de Melo 
938a8c2190eSArnaldo Carvalho de Melo 		switch (op->code) {
939a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_COND:
940a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_COND:
941405c0059SNeal Cardwell 			if (!valid_hostcond(bc, len, &min_len))
942405c0059SNeal Cardwell 				return -EINVAL;
9435e1f5420SNeal Cardwell 			break;
944637c841dSDavid Ahern 		case INET_DIAG_BC_DEV_COND:
945637c841dSDavid Ahern 			if (!valid_devcond(bc, len, &min_len))
946637c841dSDavid Ahern 				return -EINVAL;
947637c841dSDavid Ahern 			break;
948bbb6189dSKristian Evensen 		case INET_DIAG_BC_S_EQ:
949a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_GE:
950a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_S_LE:
951bbb6189dSKristian Evensen 		case INET_DIAG_BC_D_EQ:
952a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_GE:
953a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_D_LE:
9545e1f5420SNeal Cardwell 			if (!valid_port_comparison(bc, len, &min_len))
955a8c2190eSArnaldo Carvalho de Melo 				return -EINVAL;
956a8c2190eSArnaldo Carvalho de Melo 			break;
957a52e95abSLorenzo Colitti 		case INET_DIAG_BC_MARK_COND:
958a52e95abSLorenzo Colitti 			if (!net_admin)
959a52e95abSLorenzo Colitti 				return -EPERM;
960a52e95abSLorenzo Colitti 			if (!valid_markcond(bc, len, &min_len))
961a52e95abSLorenzo Colitti 				return -EINVAL;
962a52e95abSLorenzo Colitti 			break;
963b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
964b1f3e43dSDmitry Yakunin 		case INET_DIAG_BC_CGROUP_COND:
965b1f3e43dSDmitry Yakunin 			if (!valid_cgroupcond(bc, len, &min_len))
966b1f3e43dSDmitry Yakunin 				return -EINVAL;
967b1f3e43dSDmitry Yakunin 			break;
968b1f3e43dSDmitry Yakunin #endif
9695e1f5420SNeal Cardwell 		case INET_DIAG_BC_AUTO:
9705e1f5420SNeal Cardwell 		case INET_DIAG_BC_JMP:
971a8c2190eSArnaldo Carvalho de Melo 		case INET_DIAG_BC_NOP:
972a8c2190eSArnaldo Carvalho de Melo 			break;
973a8c2190eSArnaldo Carvalho de Melo 		default:
974a8c2190eSArnaldo Carvalho de Melo 			return -EINVAL;
975a8c2190eSArnaldo Carvalho de Melo 		}
9765e1f5420SNeal Cardwell 
9775e1f5420SNeal Cardwell 		if (op->code != INET_DIAG_BC_NOP) {
9785e1f5420SNeal Cardwell 			if (op->no < min_len || op->no > len + 4 || op->no & 3)
9795e1f5420SNeal Cardwell 				return -EINVAL;
9805e1f5420SNeal Cardwell 			if (op->no < len &&
9815e1f5420SNeal Cardwell 			    !valid_cc(bytecode, bytecode_len, len - op->no))
9825e1f5420SNeal Cardwell 				return -EINVAL;
9835e1f5420SNeal Cardwell 		}
9845e1f5420SNeal Cardwell 
985405c0059SNeal Cardwell 		if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
986eeb14972SEric Dumazet 			return -EINVAL;
987a8c2190eSArnaldo Carvalho de Melo 		bc  += op->yes;
988a8c2190eSArnaldo Carvalho de Melo 		len -= op->yes;
989a8c2190eSArnaldo Carvalho de Melo 	}
990a8c2190eSArnaldo Carvalho de Melo 	return len == 0 ? 0 : -EINVAL;
991a8c2190eSArnaldo Carvalho de Melo }
992a8c2190eSArnaldo Carvalho de Melo 
twsk_build_assert(void)99349612729SEric Dumazet static void twsk_build_assert(void)
99449612729SEric Dumazet {
99549612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
99649612729SEric Dumazet 		     offsetof(struct sock, sk_family));
99749612729SEric Dumazet 
99849612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
99949612729SEric Dumazet 		     offsetof(struct inet_sock, inet_num));
100049612729SEric Dumazet 
100149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
100249612729SEric Dumazet 		     offsetof(struct inet_sock, inet_dport));
100349612729SEric Dumazet 
100449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
100549612729SEric Dumazet 		     offsetof(struct inet_sock, inet_rcv_saddr));
100649612729SEric Dumazet 
100749612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
100849612729SEric Dumazet 		     offsetof(struct inet_sock, inet_daddr));
100949612729SEric Dumazet 
101049612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
101149612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
101249612729SEric Dumazet 		     offsetof(struct sock, sk_v6_rcv_saddr));
101349612729SEric Dumazet 
101449612729SEric Dumazet 	BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
101549612729SEric Dumazet 		     offsetof(struct sock, sk_v6_daddr));
101649612729SEric Dumazet #endif
101749612729SEric Dumazet }
101849612729SEric Dumazet 
inet_diag_dump_icsk(struct inet_hashinfo * hashinfo,struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r)10191942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
1020e31c5e0eSEric Dumazet 			 struct netlink_callback *cb,
10210df6d328SMartin KaFai Lau 			 const struct inet_diag_req_v2 *r)
1022a8c2190eSArnaldo Carvalho de Melo {
1023d545cacaSLorenzo Colitti 	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
10240df6d328SMartin KaFai Lau 	struct inet_diag_dump_data *cb_data = cb->data;
102567db3e4bSEric Dumazet 	struct net *net = sock_net(skb->sk);
102667db3e4bSEric Dumazet 	u32 idiag_states = r->idiag_states;
102767db3e4bSEric Dumazet 	int i, num, s_i, s_num;
10280df6d328SMartin KaFai Lau 	struct nlattr *bc;
102967db3e4bSEric Dumazet 	struct sock *sk;
1030a8c2190eSArnaldo Carvalho de Melo 
10310df6d328SMartin KaFai Lau 	bc = cb_data->inet_diag_nla_bc;
1032079096f1SEric Dumazet 	if (idiag_states & TCPF_SYN_RECV)
1033079096f1SEric Dumazet 		idiag_states |= TCPF_NEW_SYN_RECV;
1034a8c2190eSArnaldo Carvalho de Melo 	s_i = cb->args[1];
1035a8c2190eSArnaldo Carvalho de Melo 	s_num = num = cb->args[2];
1036a8c2190eSArnaldo Carvalho de Melo 
1037a8c2190eSArnaldo Carvalho de Melo 	if (cb->args[0] == 0) {
10389652dc2eSEric Dumazet 		if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
1039a8c2190eSArnaldo Carvalho de Melo 			goto skip_listen_ht;
1040a8c2190eSArnaldo Carvalho de Melo 
1041cae3873cSMartin KaFai Lau 		for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
10425caea4eaSEric Dumazet 			struct inet_listen_hashbucket *ilb;
10438dbd76e7SEric Dumazet 			struct hlist_nulls_node *node;
1044a8c2190eSArnaldo Carvalho de Melo 
1045a8c2190eSArnaldo Carvalho de Melo 			num = 0;
1046cae3873cSMartin KaFai Lau 			ilb = &hashinfo->lhash2[i];
1047cae3873cSMartin KaFai Lau 
1048622a08e8SEric Dumazet 			if (hlist_nulls_empty(&ilb->nulls_head)) {
1049622a08e8SEric Dumazet 				s_num = 0;
1050622a08e8SEric Dumazet 				continue;
1051622a08e8SEric Dumazet 			}
10529652dc2eSEric Dumazet 			spin_lock(&ilb->lock);
10538dbd76e7SEric Dumazet 			sk_nulls_for_each(sk, node, &ilb->nulls_head) {
1054a8c2190eSArnaldo Carvalho de Melo 				struct inet_sock *inet = inet_sk(sk);
1055a8c2190eSArnaldo Carvalho de Melo 
105651d7cccfSAndrey Vagin 				if (!net_eq(sock_net(sk), net))
105751d7cccfSAndrey Vagin 					continue;
105851d7cccfSAndrey Vagin 
1059a8c2190eSArnaldo Carvalho de Melo 				if (num < s_num) {
1060a8c2190eSArnaldo Carvalho de Melo 					num++;
1061a8c2190eSArnaldo Carvalho de Melo 					continue;
1062a8c2190eSArnaldo Carvalho de Melo 				}
1063a8c2190eSArnaldo Carvalho de Melo 
1064d23deaa0SPavel Emelyanov 				if (r->sdiag_family != AF_UNSPEC &&
1065d23deaa0SPavel Emelyanov 				    sk->sk_family != r->sdiag_family)
1066d23deaa0SPavel Emelyanov 					goto next_listen;
1067d23deaa0SPavel Emelyanov 
1068c720c7e8SEric Dumazet 				if (r->id.idiag_sport != inet->inet_sport &&
1069a8c2190eSArnaldo Carvalho de Melo 				    r->id.idiag_sport)
1070a8c2190eSArnaldo Carvalho de Melo 					goto next_listen;
1071a8c2190eSArnaldo Carvalho de Melo 
10725682d393SMartin KaFai Lau 				if (!inet_diag_bc_sk(bc, sk))
10735682d393SMartin KaFai Lau 					goto next_listen;
10745682d393SMartin KaFai Lau 
10755682d393SMartin KaFai Lau 				if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
10765682d393SMartin KaFai Lau 						      cb, r, NLM_F_MULTI,
10775682d393SMartin KaFai Lau 						      net_admin) < 0) {
10789652dc2eSEric Dumazet 					spin_unlock(&ilb->lock);
1079a8c2190eSArnaldo Carvalho de Melo 					goto done;
1080a8c2190eSArnaldo Carvalho de Melo 				}
1081a8c2190eSArnaldo Carvalho de Melo 
1082a8c2190eSArnaldo Carvalho de Melo next_listen:
1083a8c2190eSArnaldo Carvalho de Melo 				++num;
1084a8c2190eSArnaldo Carvalho de Melo 			}
10859652dc2eSEric Dumazet 			spin_unlock(&ilb->lock);
1086a8c2190eSArnaldo Carvalho de Melo 
1087a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
1088a8c2190eSArnaldo Carvalho de Melo 		}
1089a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
1090a8c2190eSArnaldo Carvalho de Melo 		cb->args[0] = 1;
1091a8c2190eSArnaldo Carvalho de Melo 		s_i = num = s_num = 0;
1092a8c2190eSArnaldo Carvalho de Melo 	}
1093a8c2190eSArnaldo Carvalho de Melo 
109491051f00SGuillaume Nault /* Process a maximum of SKARR_SZ sockets at a time when walking hash buckets
109591051f00SGuillaume Nault  * with bh disabled.
109691051f00SGuillaume Nault  */
109791051f00SGuillaume Nault #define SKARR_SZ 16
109891051f00SGuillaume Nault 
109991051f00SGuillaume Nault 	/* Dump bound but inactive (not listening, connecting, etc.) sockets */
110091051f00SGuillaume Nault 	if (cb->args[0] == 1) {
110191051f00SGuillaume Nault 		if (!(idiag_states & TCPF_BOUND_INACTIVE))
110291051f00SGuillaume Nault 			goto skip_bind_ht;
110391051f00SGuillaume Nault 
110491051f00SGuillaume Nault 		for (i = s_i; i < hashinfo->bhash_size; i++) {
110591051f00SGuillaume Nault 			struct inet_bind_hashbucket *ibb;
110691051f00SGuillaume Nault 			struct inet_bind2_bucket *tb2;
110791051f00SGuillaume Nault 			struct sock *sk_arr[SKARR_SZ];
110891051f00SGuillaume Nault 			int num_arr[SKARR_SZ];
110991051f00SGuillaume Nault 			int idx, accum, res;
111091051f00SGuillaume Nault 
111191051f00SGuillaume Nault resume_bind_walk:
111291051f00SGuillaume Nault 			num = 0;
111391051f00SGuillaume Nault 			accum = 0;
111491051f00SGuillaume Nault 			ibb = &hashinfo->bhash2[i];
111591051f00SGuillaume Nault 
1116622a08e8SEric Dumazet 			if (hlist_empty(&ibb->chain)) {
1117622a08e8SEric Dumazet 				s_num = 0;
1118622a08e8SEric Dumazet 				continue;
1119622a08e8SEric Dumazet 			}
112091051f00SGuillaume Nault 			spin_lock_bh(&ibb->lock);
112191051f00SGuillaume Nault 			inet_bind_bucket_for_each(tb2, &ibb->chain) {
112291051f00SGuillaume Nault 				if (!net_eq(ib2_net(tb2), net))
112391051f00SGuillaume Nault 					continue;
112491051f00SGuillaume Nault 
1125770041d3SKuniyuki Iwashima 				sk_for_each_bound(sk, &tb2->owners) {
112691051f00SGuillaume Nault 					struct inet_sock *inet = inet_sk(sk);
112791051f00SGuillaume Nault 
112891051f00SGuillaume Nault 					if (num < s_num)
112991051f00SGuillaume Nault 						goto next_bind;
113091051f00SGuillaume Nault 
113191051f00SGuillaume Nault 					if (sk->sk_state != TCP_CLOSE ||
113291051f00SGuillaume Nault 					    !inet->inet_num)
113391051f00SGuillaume Nault 						goto next_bind;
113491051f00SGuillaume Nault 
113591051f00SGuillaume Nault 					if (r->sdiag_family != AF_UNSPEC &&
113691051f00SGuillaume Nault 					    r->sdiag_family != sk->sk_family)
113791051f00SGuillaume Nault 						goto next_bind;
113891051f00SGuillaume Nault 
113991051f00SGuillaume Nault 					if (!inet_diag_bc_sk(bc, sk))
114091051f00SGuillaume Nault 						goto next_bind;
114191051f00SGuillaume Nault 
114291051f00SGuillaume Nault 					sock_hold(sk);
114391051f00SGuillaume Nault 					num_arr[accum] = num;
114491051f00SGuillaume Nault 					sk_arr[accum] = sk;
114591051f00SGuillaume Nault 					if (++accum == SKARR_SZ)
114691051f00SGuillaume Nault 						goto pause_bind_walk;
114791051f00SGuillaume Nault next_bind:
114891051f00SGuillaume Nault 					num++;
114991051f00SGuillaume Nault 				}
115091051f00SGuillaume Nault 			}
115191051f00SGuillaume Nault pause_bind_walk:
115291051f00SGuillaume Nault 			spin_unlock_bh(&ibb->lock);
115391051f00SGuillaume Nault 
115491051f00SGuillaume Nault 			res = 0;
115591051f00SGuillaume Nault 			for (idx = 0; idx < accum; idx++) {
115691051f00SGuillaume Nault 				if (res >= 0) {
115791051f00SGuillaume Nault 					res = inet_sk_diag_fill(sk_arr[idx],
115891051f00SGuillaume Nault 								NULL, skb, cb,
115991051f00SGuillaume Nault 								r, NLM_F_MULTI,
116091051f00SGuillaume Nault 								net_admin);
116191051f00SGuillaume Nault 					if (res < 0)
116291051f00SGuillaume Nault 						num = num_arr[idx];
116391051f00SGuillaume Nault 				}
116491051f00SGuillaume Nault 				sock_put(sk_arr[idx]);
116591051f00SGuillaume Nault 			}
116691051f00SGuillaume Nault 			if (res < 0)
116791051f00SGuillaume Nault 				goto done;
116891051f00SGuillaume Nault 
116991051f00SGuillaume Nault 			cond_resched();
117091051f00SGuillaume Nault 
117191051f00SGuillaume Nault 			if (accum == SKARR_SZ) {
117291051f00SGuillaume Nault 				s_num = num + 1;
117391051f00SGuillaume Nault 				goto resume_bind_walk;
117491051f00SGuillaume Nault 			}
117591051f00SGuillaume Nault 
117691051f00SGuillaume Nault 			s_num = 0;
117791051f00SGuillaume Nault 		}
117891051f00SGuillaume Nault skip_bind_ht:
117991051f00SGuillaume Nault 		cb->args[0] = 2;
118091051f00SGuillaume Nault 		s_i = num = s_num = 0;
118191051f00SGuillaume Nault 	}
118291051f00SGuillaume Nault 
1183079096f1SEric Dumazet 	if (!(idiag_states & ~TCPF_LISTEN))
1184efb3cb42SPavel Emelyanov 		goto out;
1185a8c2190eSArnaldo Carvalho de Melo 
1186f373b53bSEric Dumazet 	for (i = s_i; i <= hashinfo->ehash_mask; i++) {
1187a8c2190eSArnaldo Carvalho de Melo 		struct inet_ehash_bucket *head = &hashinfo->ehash[i];
11887e3aab4aSDavid S. Miller 		spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
11893ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
119067db3e4bSEric Dumazet 		struct sock *sk_arr[SKARR_SZ];
119167db3e4bSEric Dumazet 		int num_arr[SKARR_SZ];
119267db3e4bSEric Dumazet 		int idx, accum, res;
11936be547a6SAndi Kleen 
119405dbc7b5SEric Dumazet 		if (hlist_nulls_empty(&head->chain))
11956be547a6SAndi Kleen 			continue;
11966be547a6SAndi Kleen 
1197a8c2190eSArnaldo Carvalho de Melo 		if (i > s_i)
1198a8c2190eSArnaldo Carvalho de Melo 			s_num = 0;
1199a8c2190eSArnaldo Carvalho de Melo 
120067db3e4bSEric Dumazet next_chunk:
120167db3e4bSEric Dumazet 		num = 0;
120267db3e4bSEric Dumazet 		accum = 0;
12037e3aab4aSDavid S. Miller 		spin_lock_bh(lock);
12043ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &head->chain) {
120567db3e4bSEric Dumazet 			int state;
1206a8c2190eSArnaldo Carvalho de Melo 
120751d7cccfSAndrey Vagin 			if (!net_eq(sock_net(sk), net))
120851d7cccfSAndrey Vagin 				continue;
1209a8c2190eSArnaldo Carvalho de Melo 			if (num < s_num)
1210a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
121170315d22SNeal Cardwell 			state = (sk->sk_state == TCP_TIME_WAIT) ?
1212*3e5cbbb1SEric Dumazet 				READ_ONCE(inet_twsk(sk)->tw_substate) : sk->sk_state;
1213079096f1SEric Dumazet 			if (!(idiag_states & (1 << state)))
1214a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
1215d23deaa0SPavel Emelyanov 			if (r->sdiag_family != AF_UNSPEC &&
1216d23deaa0SPavel Emelyanov 			    sk->sk_family != r->sdiag_family)
1217d23deaa0SPavel Emelyanov 				goto next_normal;
121805dbc7b5SEric Dumazet 			if (r->id.idiag_sport != htons(sk->sk_num) &&
1219a8c2190eSArnaldo Carvalho de Melo 			    r->id.idiag_sport)
1220a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
122105dbc7b5SEric Dumazet 			if (r->id.idiag_dport != sk->sk_dport &&
12224e852c02SArnaldo Carvalho de Melo 			    r->id.idiag_dport)
1223a8c2190eSArnaldo Carvalho de Melo 				goto next_normal;
1224a58917f5SEric Dumazet 			twsk_build_assert();
1225a58917f5SEric Dumazet 
1226a58917f5SEric Dumazet 			if (!inet_diag_bc_sk(bc, sk))
1227a58917f5SEric Dumazet 				goto next_normal;
1228a58917f5SEric Dumazet 
1229f0c928d8SEric Dumazet 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
1230f0c928d8SEric Dumazet 				goto next_normal;
1231f0c928d8SEric Dumazet 
123267db3e4bSEric Dumazet 			num_arr[accum] = num;
123367db3e4bSEric Dumazet 			sk_arr[accum] = sk;
123467db3e4bSEric Dumazet 			if (++accum == SKARR_SZ)
123567db3e4bSEric Dumazet 				break;
123667db3e4bSEric Dumazet next_normal:
123767db3e4bSEric Dumazet 			++num;
123867db3e4bSEric Dumazet 		}
123967db3e4bSEric Dumazet 		spin_unlock_bh(lock);
124067db3e4bSEric Dumazet 		res = 0;
124167db3e4bSEric Dumazet 		for (idx = 0; idx < accum; idx++) {
124267db3e4bSEric Dumazet 			if (res >= 0) {
12435682d393SMartin KaFai Lau 				res = sk_diag_fill(sk_arr[idx], skb, cb, r,
12445682d393SMartin KaFai Lau 						   NLM_F_MULTI, net_admin);
124567db3e4bSEric Dumazet 				if (res < 0)
124667db3e4bSEric Dumazet 					num = num_arr[idx];
1247a8c2190eSArnaldo Carvalho de Melo 			}
124867db3e4bSEric Dumazet 			sock_gen_put(sk_arr[idx]);
1249a8c2190eSArnaldo Carvalho de Melo 		}
125067db3e4bSEric Dumazet 		if (res < 0)
125167db3e4bSEric Dumazet 			break;
1252acffb584SEric Dumazet 		cond_resched();
125367db3e4bSEric Dumazet 		if (accum == SKARR_SZ) {
125467db3e4bSEric Dumazet 			s_num = num + 1;
125567db3e4bSEric Dumazet 			goto next_chunk;
125667db3e4bSEric Dumazet 		}
1257a8c2190eSArnaldo Carvalho de Melo 	}
1258a8c2190eSArnaldo Carvalho de Melo 
1259a8c2190eSArnaldo Carvalho de Melo done:
1260a8c2190eSArnaldo Carvalho de Melo 	cb->args[1] = i;
1261a8c2190eSArnaldo Carvalho de Melo 	cb->args[2] = num;
1262efb3cb42SPavel Emelyanov out:
1263efb3cb42SPavel Emelyanov 	;
1264efb3cb42SPavel Emelyanov }
12651942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
1266efb3cb42SPavel Emelyanov 
__inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r)1267efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
12680df6d328SMartin KaFai Lau 			    const struct inet_diag_req_v2 *r)
1269efb3cb42SPavel Emelyanov {
12703f935c75SPaolo Abeni 	struct inet_diag_dump_data *cb_data = cb->data;
1271efb3cb42SPavel Emelyanov 	const struct inet_diag_handler *handler;
1272085c20caSMartin KaFai Lau 	u32 prev_min_dump_alloc;
12733f935c75SPaolo Abeni 	int protocol, err = 0;
12743f935c75SPaolo Abeni 
12753f935c75SPaolo Abeni 	protocol = inet_diag_get_protocol(r, cb_data);
1276efb3cb42SPavel Emelyanov 
1277085c20caSMartin KaFai Lau again:
1278085c20caSMartin KaFai Lau 	prev_min_dump_alloc = cb->min_dump_alloc;
12793f935c75SPaolo Abeni 	handler = inet_diag_lock_handler(protocol);
1280223f5519SEric Dumazet 	if (handler) {
12810df6d328SMartin KaFai Lau 		handler->dump(skb, cb, r);
1282d523a328SHerbert Xu 		inet_diag_unlock_handler(handler);
1283223f5519SEric Dumazet 	} else {
1284223f5519SEric Dumazet 		err = -ENOENT;
1285223f5519SEric Dumazet 	}
1286085c20caSMartin KaFai Lau 	/* The skb is not large enough to fit one sk info and
1287085c20caSMartin KaFai Lau 	 * inet_sk_diag_fill() has requested for a larger skb.
1288085c20caSMartin KaFai Lau 	 */
1289085c20caSMartin KaFai Lau 	if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) {
1290085c20caSMartin KaFai Lau 		err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL);
1291085c20caSMartin KaFai Lau 		if (!err)
1292085c20caSMartin KaFai Lau 			goto again;
1293085c20caSMartin KaFai Lau 	}
1294085c20caSMartin KaFai Lau 
1295cacb6ba0SCyrill Gorcunov 	return err ? : skb->len;
1296a8c2190eSArnaldo Carvalho de Melo }
1297a8c2190eSArnaldo Carvalho de Melo 
inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb)129825c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
129925c4cd2bSPavel Emelyanov {
13000df6d328SMartin KaFai Lau 	return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh));
13010df6d328SMartin KaFai Lau }
130225c4cd2bSPavel Emelyanov 
__inet_diag_dump_start(struct netlink_callback * cb,int hdrlen)13030df6d328SMartin KaFai Lau static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen)
13040df6d328SMartin KaFai Lau {
13050df6d328SMartin KaFai Lau 	const struct nlmsghdr *nlh = cb->nlh;
13060df6d328SMartin KaFai Lau 	struct inet_diag_dump_data *cb_data;
13070df6d328SMartin KaFai Lau 	struct sk_buff *skb = cb->skb;
13080df6d328SMartin KaFai Lau 	struct nlattr *nla;
13093f935c75SPaolo Abeni 	int err;
131025c4cd2bSPavel Emelyanov 
13110df6d328SMartin KaFai Lau 	cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL);
13120df6d328SMartin KaFai Lau 	if (!cb_data)
13130df6d328SMartin KaFai Lau 		return -ENOMEM;
13140df6d328SMartin KaFai Lau 
1315d5e4d0a5SEric Dumazet 	err = inet_diag_parse_attrs(nlh, hdrlen, cb_data->req_nlas);
1316d5e4d0a5SEric Dumazet 	if (err) {
1317d5e4d0a5SEric Dumazet 		kfree(cb_data);
1318d5e4d0a5SEric Dumazet 		return err;
1319d5e4d0a5SEric Dumazet 	}
13200df6d328SMartin KaFai Lau 	nla = cb_data->inet_diag_nla_bc;
13210df6d328SMartin KaFai Lau 	if (nla) {
13220df6d328SMartin KaFai Lau 		err = inet_diag_bc_audit(nla, skb);
13230df6d328SMartin KaFai Lau 		if (err) {
13240df6d328SMartin KaFai Lau 			kfree(cb_data);
13250df6d328SMartin KaFai Lau 			return err;
13260df6d328SMartin KaFai Lau 		}
13270df6d328SMartin KaFai Lau 	}
13280df6d328SMartin KaFai Lau 
1329085c20caSMartin KaFai Lau 	nla = cb_data->inet_diag_nla_bpf_stgs;
1330085c20caSMartin KaFai Lau 	if (nla) {
1331085c20caSMartin KaFai Lau 		struct bpf_sk_storage_diag *bpf_stg_diag;
1332085c20caSMartin KaFai Lau 
1333085c20caSMartin KaFai Lau 		bpf_stg_diag = bpf_sk_storage_diag_alloc(nla);
1334085c20caSMartin KaFai Lau 		if (IS_ERR(bpf_stg_diag)) {
1335085c20caSMartin KaFai Lau 			kfree(cb_data);
1336085c20caSMartin KaFai Lau 			return PTR_ERR(bpf_stg_diag);
1337085c20caSMartin KaFai Lau 		}
1338085c20caSMartin KaFai Lau 		cb_data->bpf_stg_diag = bpf_stg_diag;
1339085c20caSMartin KaFai Lau 	}
1340085c20caSMartin KaFai Lau 
13410df6d328SMartin KaFai Lau 	cb->data = cb_data;
13420df6d328SMartin KaFai Lau 	return 0;
13430df6d328SMartin KaFai Lau }
13440df6d328SMartin KaFai Lau 
inet_diag_dump_start(struct netlink_callback * cb)13450df6d328SMartin KaFai Lau static int inet_diag_dump_start(struct netlink_callback *cb)
13460df6d328SMartin KaFai Lau {
13470df6d328SMartin KaFai Lau 	return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2));
13480df6d328SMartin KaFai Lau }
13490df6d328SMartin KaFai Lau 
inet_diag_dump_start_compat(struct netlink_callback * cb)13500df6d328SMartin KaFai Lau static int inet_diag_dump_start_compat(struct netlink_callback *cb)
13510df6d328SMartin KaFai Lau {
13520df6d328SMartin KaFai Lau 	return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
13530df6d328SMartin KaFai Lau }
13540df6d328SMartin KaFai Lau 
inet_diag_dump_done(struct netlink_callback * cb)13550df6d328SMartin KaFai Lau static int inet_diag_dump_done(struct netlink_callback *cb)
13560df6d328SMartin KaFai Lau {
1357085c20caSMartin KaFai Lau 	struct inet_diag_dump_data *cb_data = cb->data;
1358085c20caSMartin KaFai Lau 
1359085c20caSMartin KaFai Lau 	bpf_sk_storage_diag_free(cb_data->bpf_stg_diag);
13600df6d328SMartin KaFai Lau 	kfree(cb->data);
13610df6d328SMartin KaFai Lau 
13620df6d328SMartin KaFai Lau 	return 0;
136325c4cd2bSPavel Emelyanov }
136425c4cd2bSPavel Emelyanov 
inet_diag_type2proto(int type)1365e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type)
1366a029fe26SPavel Emelyanov {
1367a029fe26SPavel Emelyanov 	switch (type) {
1368a029fe26SPavel Emelyanov 	case TCPDIAG_GETSOCK:
1369a029fe26SPavel Emelyanov 		return IPPROTO_TCP;
1370a029fe26SPavel Emelyanov 	case DCCPDIAG_GETSOCK:
1371a029fe26SPavel Emelyanov 		return IPPROTO_DCCP;
1372a029fe26SPavel Emelyanov 	default:
1373a029fe26SPavel Emelyanov 		return 0;
1374a029fe26SPavel Emelyanov 	}
1375a029fe26SPavel Emelyanov }
1376a029fe26SPavel Emelyanov 
inet_diag_dump_compat(struct sk_buff * skb,struct netlink_callback * cb)1377e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb,
1378e31c5e0eSEric Dumazet 				 struct netlink_callback *cb)
137925c4cd2bSPavel Emelyanov {
1380d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1381c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
138225c4cd2bSPavel Emelyanov 
1383d23deaa0SPavel Emelyanov 	req.sdiag_family = AF_UNSPEC; /* compatibility */
138425c4cd2bSPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
138525c4cd2bSPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
138661cf1c73SShigeru Yoshida 	req.pad = 0;
138725c4cd2bSPavel Emelyanov 	req.idiag_states = rc->idiag_states;
138825c4cd2bSPavel Emelyanov 	req.id = rc->id;
138925c4cd2bSPavel Emelyanov 
13900df6d328SMartin KaFai Lau 	return __inet_diag_dump(skb, cb, &req);
139125c4cd2bSPavel Emelyanov }
139225c4cd2bSPavel Emelyanov 
inet_diag_get_exact_compat(struct sk_buff * in_skb,const struct nlmsghdr * nlh)1393fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1394fe50ce28SPavel Emelyanov 				      const struct nlmsghdr *nlh)
1395fe50ce28SPavel Emelyanov {
1396d106352dSDavid S. Miller 	struct inet_diag_req *rc = nlmsg_data(nlh);
1397c8991362SPavel Emelyanov 	struct inet_diag_req_v2 req;
1398fe50ce28SPavel Emelyanov 
1399fe50ce28SPavel Emelyanov 	req.sdiag_family = rc->idiag_family;
1400fe50ce28SPavel Emelyanov 	req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1401fe50ce28SPavel Emelyanov 	req.idiag_ext = rc->idiag_ext;
140261cf1c73SShigeru Yoshida 	req.pad = 0;
1403fe50ce28SPavel Emelyanov 	req.idiag_states = rc->idiag_states;
1404fe50ce28SPavel Emelyanov 	req.id = rc->id;
1405fe50ce28SPavel Emelyanov 
14063f935c75SPaolo Abeni 	return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh,
14073f935c75SPaolo Abeni 				   sizeof(struct inet_diag_req), &req);
1408fe50ce28SPavel Emelyanov }
1409fe50ce28SPavel Emelyanov 
inet_diag_rcv_msg_compat(struct sk_buff * skb,struct nlmsghdr * nlh)14108d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1411a8c2190eSArnaldo Carvalho de Melo {
14123b09c84cSPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req);
141351d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1414a8c2190eSArnaldo Carvalho de Melo 
1415ead592baSThomas Graf 	if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1416ead592baSThomas Graf 	    nlmsg_len(nlh) < hdrlen)
1417ead592baSThomas Graf 		return -EINVAL;
1418a8c2190eSArnaldo Carvalho de Melo 
1419b8f3ab42SDavid S. Miller 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
142080d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
14210df6d328SMartin KaFai Lau 			.start = inet_diag_dump_start_compat,
14220df6d328SMartin KaFai Lau 			.done = inet_diag_dump_done,
142380d326faSPablo Neira Ayuso 			.dump = inet_diag_dump_compat,
142480d326faSPablo Neira Ayuso 		};
142551d7cccfSAndrey Vagin 		return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
142680d326faSPablo Neira Ayuso 	}
1427ead592baSThomas Graf 
1428fe50ce28SPavel Emelyanov 	return inet_diag_get_exact_compat(skb, nlh);
1429a8c2190eSArnaldo Carvalho de Melo }
1430a8c2190eSArnaldo Carvalho de Melo 
inet_diag_handler_cmd(struct sk_buff * skb,struct nlmsghdr * h)14316eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1432d366477aSPavel Emelyanov {
1433c8991362SPavel Emelyanov 	int hdrlen = sizeof(struct inet_diag_req_v2);
143451d7cccfSAndrey Vagin 	struct net *net = sock_net(skb->sk);
1435d366477aSPavel Emelyanov 
1436d366477aSPavel Emelyanov 	if (nlmsg_len(h) < hdrlen)
1437d366477aSPavel Emelyanov 		return -EINVAL;
1438d366477aSPavel Emelyanov 
14396eb5d2e0SLorenzo Colitti 	if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
14406eb5d2e0SLorenzo Colitti 	    h->nlmsg_flags & NLM_F_DUMP) {
144180d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
14420df6d328SMartin KaFai Lau 			.start = inet_diag_dump_start,
14430df6d328SMartin KaFai Lau 			.done = inet_diag_dump_done,
144480d326faSPablo Neira Ayuso 			.dump = inet_diag_dump,
144580d326faSPablo Neira Ayuso 		};
144651d7cccfSAndrey Vagin 		return netlink_dump_start(net->diag_nlsk, skb, h, &c);
144780d326faSPablo Neira Ayuso 	}
1448d366477aSPavel Emelyanov 
14493f935c75SPaolo Abeni 	return inet_diag_cmd_exact(h->nlmsg_type, skb, h, hdrlen,
14503f935c75SPaolo Abeni 				   nlmsg_data(h));
1451d366477aSPavel Emelyanov }
1452d366477aSPavel Emelyanov 
145335ac838aSCraig Gallek static
inet_diag_handler_get_info(struct sk_buff * skb,struct sock * sk)145435ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
145535ac838aSCraig Gallek {
145635ac838aSCraig Gallek 	const struct inet_diag_handler *handler;
145735ac838aSCraig Gallek 	struct nlmsghdr *nlh;
145835ac838aSCraig Gallek 	struct nlattr *attr;
145935ac838aSCraig Gallek 	struct inet_diag_msg *r;
146035ac838aSCraig Gallek 	void *info = NULL;
146135ac838aSCraig Gallek 	int err = 0;
146235ac838aSCraig Gallek 
146335ac838aSCraig Gallek 	nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
146435ac838aSCraig Gallek 	if (!nlh)
146535ac838aSCraig Gallek 		return -ENOMEM;
146635ac838aSCraig Gallek 
146735ac838aSCraig Gallek 	r = nlmsg_data(nlh);
146835ac838aSCraig Gallek 	memset(r, 0, sizeof(*r));
146935ac838aSCraig Gallek 	inet_diag_msg_common_fill(r, sk);
1470e0df02e0SCraig Gallek 	if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1471e0df02e0SCraig Gallek 		r->id.idiag_sport = inet_sk(sk)->inet_sport;
147235ac838aSCraig Gallek 	r->idiag_state = sk->sk_state;
147335ac838aSCraig Gallek 
147435ac838aSCraig Gallek 	if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
147535ac838aSCraig Gallek 		nlmsg_cancel(skb, nlh);
147635ac838aSCraig Gallek 		return err;
147735ac838aSCraig Gallek 	}
147835ac838aSCraig Gallek 
147935ac838aSCraig Gallek 	handler = inet_diag_lock_handler(sk->sk_protocol);
1480223f5519SEric Dumazet 	if (!handler) {
148135ac838aSCraig Gallek 		nlmsg_cancel(skb, nlh);
1482223f5519SEric Dumazet 		return -ENOENT;
148335ac838aSCraig Gallek 	}
148435ac838aSCraig Gallek 
148535ac838aSCraig Gallek 	attr = handler->idiag_info_size
14866ed46d12SNicolas Dichtel 		? nla_reserve_64bit(skb, INET_DIAG_INFO,
14876ed46d12SNicolas Dichtel 				    handler->idiag_info_size,
14886ed46d12SNicolas Dichtel 				    INET_DIAG_PAD)
148935ac838aSCraig Gallek 		: NULL;
149035ac838aSCraig Gallek 	if (attr)
149135ac838aSCraig Gallek 		info = nla_data(attr);
149235ac838aSCraig Gallek 
149335ac838aSCraig Gallek 	handler->idiag_get_info(sk, r, info);
149435ac838aSCraig Gallek 	inet_diag_unlock_handler(handler);
149535ac838aSCraig Gallek 
149635ac838aSCraig Gallek 	nlmsg_end(skb, nlh);
149735ac838aSCraig Gallek 	return 0;
149835ac838aSCraig Gallek }
149935ac838aSCraig Gallek 
15008dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
1501114b4bb1SEric Dumazet 	.owner = THIS_MODULE,
1502d366477aSPavel Emelyanov 	.family = AF_INET,
15036eb5d2e0SLorenzo Colitti 	.dump = inet_diag_handler_cmd,
150435ac838aSCraig Gallek 	.get_info = inet_diag_handler_get_info,
15056eb5d2e0SLorenzo Colitti 	.destroy = inet_diag_handler_cmd,
1506d366477aSPavel Emelyanov };
1507d366477aSPavel Emelyanov 
15088dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
1509114b4bb1SEric Dumazet 	.owner = THIS_MODULE,
1510d366477aSPavel Emelyanov 	.family = AF_INET6,
15116eb5d2e0SLorenzo Colitti 	.dump = inet_diag_handler_cmd,
151235ac838aSCraig Gallek 	.get_info = inet_diag_handler_get_info,
15136eb5d2e0SLorenzo Colitti 	.destroy = inet_diag_handler_cmd,
1514d366477aSPavel Emelyanov };
1515d366477aSPavel Emelyanov 
inet_diag_register(const struct inet_diag_handler * h)1516a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1517a8c2190eSArnaldo Carvalho de Melo {
1518a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1519a8c2190eSArnaldo Carvalho de Melo 
1520f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1521223f5519SEric Dumazet 		return -EINVAL;
1522a8c2190eSArnaldo Carvalho de Melo 
1523223f5519SEric Dumazet 	return !cmpxchg((const struct inet_diag_handler **)&inet_diag_table[type],
1524223f5519SEric Dumazet 			NULL, h) ? 0 : -EEXIST;
1525a8c2190eSArnaldo Carvalho de Melo }
1526a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1527a8c2190eSArnaldo Carvalho de Melo 
inet_diag_unregister(const struct inet_diag_handler * h)1528a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1529a8c2190eSArnaldo Carvalho de Melo {
1530a8c2190eSArnaldo Carvalho de Melo 	const __u16 type = h->idiag_type;
1531a8c2190eSArnaldo Carvalho de Melo 
1532f13c95f0SPavel Emelyanov 	if (type >= IPPROTO_MAX)
1533a8c2190eSArnaldo Carvalho de Melo 		return;
1534a8c2190eSArnaldo Carvalho de Melo 
1535223f5519SEric Dumazet 	xchg((const struct inet_diag_handler **)&inet_diag_table[type],
1536223f5519SEric Dumazet 	     NULL);
1537a8c2190eSArnaldo Carvalho de Melo }
1538a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1539a8c2190eSArnaldo Carvalho de Melo 
154086e8921dSEric Dumazet static const struct sock_diag_inet_compat inet_diag_compat = {
154186e8921dSEric Dumazet 	.owner	= THIS_MODULE,
154286e8921dSEric Dumazet 	.fn	= inet_diag_rcv_msg_compat,
154386e8921dSEric Dumazet };
154486e8921dSEric Dumazet 
inet_diag_init(void)1545a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1546a8c2190eSArnaldo Carvalho de Melo {
1547f13c95f0SPavel Emelyanov 	const int inet_diag_table_size = (IPPROTO_MAX *
1548a8c2190eSArnaldo Carvalho de Melo 					  sizeof(struct inet_diag_handler *));
1549a8c2190eSArnaldo Carvalho de Melo 	int err = -ENOMEM;
1550a8c2190eSArnaldo Carvalho de Melo 
15510da974f4SPanagiotis Issaris 	inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1552a8c2190eSArnaldo Carvalho de Melo 	if (!inet_diag_table)
1553a8c2190eSArnaldo Carvalho de Melo 		goto out;
1554a8c2190eSArnaldo Carvalho de Melo 
1555d366477aSPavel Emelyanov 	err = sock_diag_register(&inet_diag_handler);
1556d366477aSPavel Emelyanov 	if (err)
1557d366477aSPavel Emelyanov 		goto out_free_nl;
1558d366477aSPavel Emelyanov 
1559d366477aSPavel Emelyanov 	err = sock_diag_register(&inet6_diag_handler);
1560d366477aSPavel Emelyanov 	if (err)
1561d366477aSPavel Emelyanov 		goto out_free_inet;
1562d366477aSPavel Emelyanov 
156386e8921dSEric Dumazet 	sock_diag_register_inet_compat(&inet_diag_compat);
1564a8c2190eSArnaldo Carvalho de Melo out:
1565a8c2190eSArnaldo Carvalho de Melo 	return err;
1566d366477aSPavel Emelyanov 
1567d366477aSPavel Emelyanov out_free_inet:
1568d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
1569d366477aSPavel Emelyanov out_free_nl:
1570a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1571a8c2190eSArnaldo Carvalho de Melo 	goto out;
1572a8c2190eSArnaldo Carvalho de Melo }
1573a8c2190eSArnaldo Carvalho de Melo 
inet_diag_exit(void)1574a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1575a8c2190eSArnaldo Carvalho de Melo {
1576d366477aSPavel Emelyanov 	sock_diag_unregister(&inet6_diag_handler);
1577d366477aSPavel Emelyanov 	sock_diag_unregister(&inet_diag_handler);
157886e8921dSEric Dumazet 	sock_diag_unregister_inet_compat(&inet_diag_compat);
1579a8c2190eSArnaldo Carvalho de Melo 	kfree(inet_diag_table);
1580a8c2190eSArnaldo Carvalho de Melo }
1581a8c2190eSArnaldo Carvalho de Melo 
1582a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1583a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1584a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1585938dbeadSJakub Kicinski MODULE_DESCRIPTION("INET/INET6: socket monitoring via SOCK_DIAG");
1586aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1587aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1588