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
35a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **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
51d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex);
52d523a328SHerbert Xu
inet_diag_lock_handler(int proto)53f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
54d523a328SHerbert Xu {
553f935c75SPaolo Abeni if (proto < 0 || proto >= IPPROTO_MAX) {
563f935c75SPaolo Abeni mutex_lock(&inet_diag_table_mutex);
573f935c75SPaolo Abeni return ERR_PTR(-ENOENT);
583f935c75SPaolo Abeni }
593f935c75SPaolo Abeni
609311cc66SEric Dumazet if (!READ_ONCE(inet_diag_table[proto]))
61bf2ae2e4SXin Long sock_load_diag_module(AF_INET, proto);
62d523a328SHerbert Xu
63d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex);
64f13c95f0SPavel Emelyanov if (!inet_diag_table[proto])
65d523a328SHerbert Xu return ERR_PTR(-ENOENT);
66d523a328SHerbert Xu
67f13c95f0SPavel Emelyanov return inet_diag_table[proto];
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 {
72d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex);
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
107b37e8840SIvan Delalande handler = inet_diag_table[req->sdiag_protocol];
108b37e8840SIvan Delalande if (handler && handler->idiag_get_aux_size)
109b37e8840SIvan Delalande aux = handler->idiag_get_aux_size(sk, net_admin);
110b37e8840SIvan Delalande
111c8e2c80dSEric Dumazet return nla_total_size(sizeof(struct tcp_info))
112c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_msg))
11383f73c5bSDmitry Yakunin + inet_diag_msg_attrs_size()
11483f73c5bSDmitry Yakunin + nla_total_size(sizeof(struct inet_diag_meminfo))
115c8e2c80dSEric Dumazet + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
116c8e2c80dSEric Dumazet + nla_total_size(TCP_CA_NAME_MAX)
117c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct tcpvegas_info))
118b37e8840SIvan Delalande + aux
119c8e2c80dSEric Dumazet + 64;
120c8e2c80dSEric Dumazet }
121c8e2c80dSEric 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)122cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
123cb2050a7SXin Long struct inet_diag_msg *r, int ext,
124d545cacaSLorenzo Colitti struct user_namespace *user_ns,
125d545cacaSLorenzo Colitti bool net_admin)
126a8c2190eSArnaldo Carvalho de Melo {
127a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk);
128c1077616SWei Wang struct inet_diag_sockopt inet_sockopt;
129a8c2190eSArnaldo Carvalho de Melo
130e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
131e4e541a8SPavel Emelyanov goto errout;
132e4e541a8SPavel Emelyanov
133717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
134717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family.
135717b6d83SMaciej Żenczykowski */
136717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1)))
1376e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
1386e277ed5SThomas Graf goto errout;
139717b6d83SMaciej Żenczykowski
140dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
141a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) {
14206236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1)))
143efe4208fSEric Dumazet if (nla_put_u8(skb, INET_DIAG_TCLASS,
144efe4208fSEric Dumazet inet6_sk(sk)->tclass) < 0)
1456e277ed5SThomas Graf goto errout;
14620462155SPhil Sutter
1478220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
1488220ea23SPhil Sutter nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
14920462155SPhil Sutter goto errout;
150a8c2190eSArnaldo Carvalho de Melo }
151a8c2190eSArnaldo Carvalho de Melo #endif
152a8c2190eSArnaldo Carvalho de Melo
1533c5b4d69SEric Dumazet if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, READ_ONCE(sk->sk_mark)))
154d545cacaSLorenzo Colitti goto errout;
155d545cacaSLorenzo Colitti
15683f73c5bSDmitry Yakunin if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
15783f73c5bSDmitry Yakunin ext & (1 << (INET_DIAG_TCLASS - 1))) {
15883f73c5bSDmitry Yakunin u32 classid = 0;
15983f73c5bSDmitry Yakunin
16083f73c5bSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
16183f73c5bSDmitry Yakunin classid = sock_cgroup_classid(&sk->sk_cgrp_data);
16283f73c5bSDmitry Yakunin #endif
16383f73c5bSDmitry Yakunin /* Fallback to socket priority if class id isn't set.
16483f73c5bSDmitry Yakunin * Classful qdiscs use it as direct reference to class.
16583f73c5bSDmitry Yakunin * For cgroup2 classid is always zero.
16683f73c5bSDmitry Yakunin */
16783f73c5bSDmitry Yakunin if (!classid)
16883f73c5bSDmitry Yakunin classid = sk->sk_priority;
16983f73c5bSDmitry Yakunin
17083f73c5bSDmitry Yakunin if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
17183f73c5bSDmitry Yakunin goto errout;
17283f73c5bSDmitry Yakunin }
17383f73c5bSDmitry Yakunin
1746e3a401fSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
1756e3a401fSDmitry Yakunin if (nla_put_u64_64bit(skb, INET_DIAG_CGROUP_ID,
1766e3a401fSDmitry Yakunin cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)),
1776e3a401fSDmitry Yakunin INET_DIAG_PAD))
1786e3a401fSDmitry Yakunin goto errout;
1796e3a401fSDmitry Yakunin #endif
1806e3a401fSDmitry Yakunin
181d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
1823c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk);
1833c4d05c8SPavel Emelyanov
184c1077616SWei Wang memset(&inet_sockopt, 0, sizeof(inet_sockopt));
1856b5f43eaSEric Dumazet inet_sockopt.recverr = inet_test_bit(RECVERR, sk);
186b1c0356aSEric Dumazet inet_sockopt.is_icsk = inet_test_bit(IS_ICSK, sk);
1873f7e7532SEric Dumazet inet_sockopt.freebind = inet_test_bit(FREEBIND, sk);
188cafbe182SEric Dumazet inet_sockopt.hdrincl = inet_test_bit(HDRINCL, sk);
189b09bde5cSEric Dumazet inet_sockopt.mc_loop = inet_test_bit(MC_LOOP, sk);
1904bd0623fSEric Dumazet inet_sockopt.transparent = inet_test_bit(TRANSPARENT, sk);
191307b4ac6SEric Dumazet inet_sockopt.mc_all = inet_test_bit(MC_ALL, sk);
192f04b8d34SEric Dumazet inet_sockopt.nodefrag = inet_test_bit(NODEFRAG, sk);
193ca571e2eSEric Dumazet inet_sockopt.bind_address_no_port = inet_test_bit(BIND_ADDRESS_NO_PORT, sk);
1948e8cfb11SEric Dumazet inet_sockopt.recverr_rfc4884 = inet_test_bit(RECVERR_RFC4884, sk);
19508e39c0dSEric Dumazet inet_sockopt.defer_connect = inet_test_bit(DEFER_CONNECT, sk);
196c1077616SWei Wang if (nla_put(skb, INET_DIAG_SOCKOPT, sizeof(inet_sockopt),
197c1077616SWei Wang &inet_sockopt))
198c1077616SWei Wang goto errout;
199c1077616SWei Wang
200cb2050a7SXin Long return 0;
201cb2050a7SXin Long errout:
202cb2050a7SXin Long return 1;
203cb2050a7SXin Long }
204cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
205cb2050a7SXin Long
inet_diag_parse_attrs(const struct nlmsghdr * nlh,int hdrlen,struct nlattr ** req_nlas)206d5e4d0a5SEric Dumazet static int inet_diag_parse_attrs(const struct nlmsghdr *nlh, int hdrlen,
2073f935c75SPaolo Abeni struct nlattr **req_nlas)
2083f935c75SPaolo Abeni {
2093f935c75SPaolo Abeni struct nlattr *nla;
2103f935c75SPaolo Abeni int remaining;
2113f935c75SPaolo Abeni
2123f935c75SPaolo Abeni nlmsg_for_each_attr(nla, nlh, hdrlen, remaining) {
2133f935c75SPaolo Abeni int type = nla_type(nla);
2143f935c75SPaolo Abeni
215d5e4d0a5SEric Dumazet if (type == INET_DIAG_REQ_PROTOCOL && nla_len(nla) != sizeof(u32))
216d5e4d0a5SEric Dumazet return -EINVAL;
217d5e4d0a5SEric Dumazet
2183f935c75SPaolo Abeni if (type < __INET_DIAG_REQ_MAX)
2193f935c75SPaolo Abeni req_nlas[type] = nla;
2203f935c75SPaolo Abeni }
221d5e4d0a5SEric Dumazet return 0;
2223f935c75SPaolo Abeni }
2233f935c75SPaolo Abeni
inet_diag_get_protocol(const struct inet_diag_req_v2 * req,const struct inet_diag_dump_data * data)2243f935c75SPaolo Abeni static int inet_diag_get_protocol(const struct inet_diag_req_v2 *req,
2253f935c75SPaolo Abeni const struct inet_diag_dump_data *data)
2263f935c75SPaolo Abeni {
2273f935c75SPaolo Abeni if (data->req_nlas[INET_DIAG_REQ_PROTOCOL])
2283f935c75SPaolo Abeni return nla_get_u32(data->req_nlas[INET_DIAG_REQ_PROTOCOL]);
2293f935c75SPaolo Abeni return req->sdiag_protocol;
2303f935c75SPaolo Abeni }
2313f935c75SPaolo Abeni
232085c20caSMartin KaFai Lau #define MAX_DUMP_ALLOC_SIZE (KMALLOC_MAX_SIZE - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
233085c20caSMartin 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)234cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
2355682d393SMartin KaFai Lau struct sk_buff *skb, struct netlink_callback *cb,
2365682d393SMartin KaFai Lau const struct inet_diag_req_v2 *req,
2375682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin)
238cb2050a7SXin Long {
239cb2050a7SXin Long const struct tcp_congestion_ops *ca_ops;
240cb2050a7SXin Long const struct inet_diag_handler *handler;
241085c20caSMartin KaFai Lau struct inet_diag_dump_data *cb_data;
242cb2050a7SXin Long int ext = req->idiag_ext;
243cb2050a7SXin Long struct inet_diag_msg *r;
244cb2050a7SXin Long struct nlmsghdr *nlh;
245cb2050a7SXin Long struct nlattr *attr;
246cb2050a7SXin Long void *info = NULL;
247cb2050a7SXin Long
248085c20caSMartin KaFai Lau cb_data = cb->data;
2493f935c75SPaolo Abeni handler = inet_diag_table[inet_diag_get_protocol(req, cb_data)];
250cb2050a7SXin Long BUG_ON(!handler);
251cb2050a7SXin Long
2525682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2535682d393SMartin KaFai Lau cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
254cb2050a7SXin Long if (!nlh)
255cb2050a7SXin Long return -EMSGSIZE;
256cb2050a7SXin Long
257cb2050a7SXin Long r = nlmsg_data(nlh);
258cb2050a7SXin Long BUG_ON(!sk_fullsock(sk));
259cb2050a7SXin Long
260cb2050a7SXin Long inet_diag_msg_common_fill(r, sk);
261cb2050a7SXin Long r->idiag_state = sk->sk_state;
262cb2050a7SXin Long r->idiag_timer = 0;
263cb2050a7SXin Long r->idiag_retrans = 0;
26471ddeac8SEric Dumazet r->idiag_expires = 0;
265cb2050a7SXin Long
2665682d393SMartin KaFai Lau if (inet_diag_msg_attrs_fill(sk, skb, r, ext,
2675682d393SMartin KaFai Lau sk_user_ns(NETLINK_CB(cb->skb).sk),
2685682d393SMartin KaFai Lau net_admin))
269cb2050a7SXin Long goto errout;
270cb2050a7SXin Long
2716e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
2726e277ed5SThomas Graf struct inet_diag_meminfo minfo = {
2736e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk),
274ab4e846aSEric Dumazet .idiag_wmem = READ_ONCE(sk->sk_wmem_queued),
275292e6077SPaolo Abeni .idiag_fmem = sk_forward_alloc_get(sk),
2766e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk),
2776e277ed5SThomas Graf };
2786e277ed5SThomas Graf
2796e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
2806e277ed5SThomas Graf goto errout;
2813c4d05c8SPavel Emelyanov }
2823c4d05c8SPavel Emelyanov
283c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
284c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
2856e277ed5SThomas Graf goto errout;
286c0636faaSPavel Emelyanov
287432490f9SCyrill Gorcunov /*
288432490f9SCyrill Gorcunov * RAW sockets might have user-defined protocols assigned,
289432490f9SCyrill Gorcunov * so report the one supplied on socket creation.
290432490f9SCyrill Gorcunov */
291432490f9SCyrill Gorcunov if (sk->sk_type == SOCK_RAW) {
292432490f9SCyrill Gorcunov if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
293432490f9SCyrill Gorcunov goto errout;
294432490f9SCyrill Gorcunov }
295432490f9SCyrill Gorcunov
296e31c5e0eSEric Dumazet if (!icsk) {
29762ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL);
2983c4d05c8SPavel Emelyanov goto out;
2993c4d05c8SPavel Emelyanov }
3003c4d05c8SPavel Emelyanov
3016ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
30257dde7f7SYuchung Cheng icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
3036ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
304a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1;
305a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits;
306b7de529cSXin Long r->idiag_expires =
3073828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
308a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
309a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4;
310a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out;
311b7de529cSXin Long r->idiag_expires =
3123828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
313a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) {
314a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2;
315a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out;
316b7de529cSXin Long r->idiag_expires =
3173828a93fSEric Dumazet jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies);
318a8c2190eSArnaldo Carvalho de Melo }
319a8c2190eSArnaldo Carvalho de Melo
3203fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
3216ed46d12SNicolas Dichtel attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
3226ed46d12SNicolas Dichtel handler->idiag_info_size,
3236ed46d12SNicolas Dichtel INET_DIAG_PAD);
3246e277ed5SThomas Graf if (!attr)
3256e277ed5SThomas Graf goto errout;
326a8c2190eSArnaldo Carvalho de Melo
3276e277ed5SThomas Graf info = nla_data(attr);
328a8c2190eSArnaldo Carvalho de Melo }
329a8c2190eSArnaldo Carvalho de Melo
330521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) {
331521f1cf1SEric Dumazet int err = 0;
332521f1cf1SEric Dumazet
333521f1cf1SEric Dumazet rcu_read_lock();
334521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops);
335521f1cf1SEric Dumazet if (ca_ops)
336521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
337521f1cf1SEric Dumazet rcu_read_unlock();
338521f1cf1SEric Dumazet if (err < 0)
3396e277ed5SThomas Graf goto errout;
340521f1cf1SEric Dumazet }
3416e277ed5SThomas Graf
342a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info);
343a8c2190eSArnaldo Carvalho de Melo
344b37e8840SIvan Delalande if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
345b37e8840SIvan Delalande if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
346b37e8840SIvan Delalande goto errout;
347b37e8840SIvan Delalande
348521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) {
34964f40ff5SEric Dumazet union tcp_cc_info info;
35064f40ff5SEric Dumazet size_t sz = 0;
35164f40ff5SEric Dumazet int attr;
352521f1cf1SEric Dumazet
353521f1cf1SEric Dumazet rcu_read_lock();
354521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops);
355521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info)
35664f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info);
357521f1cf1SEric Dumazet rcu_read_unlock();
35864f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0)
359521f1cf1SEric Dumazet goto errout;
360521f1cf1SEric Dumazet }
361a8c2190eSArnaldo Carvalho de Melo
362085c20caSMartin KaFai Lau /* Keep it at the end for potential retry with a larger skb,
363085c20caSMartin KaFai Lau * or else do best-effort fitting, which is only done for the
364085c20caSMartin KaFai Lau * first_nlmsg.
365085c20caSMartin KaFai Lau */
366085c20caSMartin KaFai Lau if (cb_data->bpf_stg_diag) {
367085c20caSMartin KaFai Lau bool first_nlmsg = ((unsigned char *)nlh == skb->data);
368085c20caSMartin KaFai Lau unsigned int prev_min_dump_alloc;
369085c20caSMartin KaFai Lau unsigned int total_nla_size = 0;
370085c20caSMartin KaFai Lau unsigned int msg_len;
371085c20caSMartin KaFai Lau int err;
372085c20caSMartin KaFai Lau
373085c20caSMartin KaFai Lau msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
374085c20caSMartin KaFai Lau err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb,
375085c20caSMartin KaFai Lau INET_DIAG_SK_BPF_STORAGES,
376085c20caSMartin KaFai Lau &total_nla_size);
377085c20caSMartin KaFai Lau
378085c20caSMartin KaFai Lau if (!err)
379085c20caSMartin KaFai Lau goto out;
380085c20caSMartin KaFai Lau
381085c20caSMartin KaFai Lau total_nla_size += msg_len;
382085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc;
383085c20caSMartin KaFai Lau if (total_nla_size > prev_min_dump_alloc)
384085c20caSMartin KaFai Lau cb->min_dump_alloc = min_t(u32, total_nla_size,
385085c20caSMartin KaFai Lau MAX_DUMP_ALLOC_SIZE);
386085c20caSMartin KaFai Lau
387085c20caSMartin KaFai Lau if (!first_nlmsg)
388085c20caSMartin KaFai Lau goto errout;
389085c20caSMartin KaFai Lau
390085c20caSMartin KaFai Lau if (cb->min_dump_alloc > prev_min_dump_alloc)
391085c20caSMartin KaFai Lau /* Retry with pskb_expand_head() with
392085c20caSMartin KaFai Lau * __GFP_DIRECT_RECLAIM
393085c20caSMartin KaFai Lau */
394085c20caSMartin KaFai Lau goto errout;
395085c20caSMartin KaFai Lau
396085c20caSMartin KaFai Lau WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc);
397085c20caSMartin KaFai Lau
398085c20caSMartin KaFai Lau /* Send what we have for this sk
399085c20caSMartin KaFai Lau * and move on to the next sk in the following
400085c20caSMartin KaFai Lau * dump()
401085c20caSMartin KaFai Lau */
402085c20caSMartin KaFai Lau }
403085c20caSMartin KaFai Lau
4043c4d05c8SPavel Emelyanov out:
405053c095aSJohannes Berg nlmsg_end(skb, nlh);
406053c095aSJohannes Berg return 0;
407a8c2190eSArnaldo Carvalho de Melo
4086e277ed5SThomas Graf errout:
4096e277ed5SThomas Graf nlmsg_cancel(skb, nlh);
41026932566SPatrick McHardy return -EMSGSIZE;
411a8c2190eSArnaldo Carvalho de Melo }
4123c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
4133c4d05c8SPavel Emelyanov
inet_twsk_diag_fill(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,u16 nlmsg_flags,bool net_admin)41433cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk,
415e31c5e0eSEric Dumazet struct sk_buff *skb,
4165682d393SMartin KaFai Lau struct netlink_callback *cb,
4171b3fc771SMaciej Żenczykowski u16 nlmsg_flags, bool net_admin)
418c7d58aabSArnaldo Carvalho de Melo {
41933cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk);
420c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r;
4216e277ed5SThomas Graf struct nlmsghdr *nlh;
422789f558cSEric Dumazet long tmo;
423c7d58aabSArnaldo Carvalho de Melo
4245682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4255682d393SMartin KaFai Lau cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type,
4265682d393SMartin KaFai Lau sizeof(*r), nlmsg_flags);
4276e277ed5SThomas Graf if (!nlh)
428d106352dSDavid S. Miller return -EMSGSIZE;
429d106352dSDavid S. Miller
430d106352dSDavid S. Miller r = nlmsg_data(nlh);
431c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT);
432c7d58aabSArnaldo Carvalho de Melo
433a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk);
434c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0;
435b1aac815SDaniel Borkmann
436c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate;
437c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3;
4383828a93fSEric Dumazet tmo = tw->tw_timer.expires - jiffies;
4393828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo);
440c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0;
441c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0;
442c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0;
443c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0;
4446e277ed5SThomas Graf
4451b3fc771SMaciej Żenczykowski if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
4461b3fc771SMaciej Żenczykowski tw->tw_mark)) {
4471b3fc771SMaciej Żenczykowski nlmsg_cancel(skb, nlh);
4481b3fc771SMaciej Żenczykowski return -EMSGSIZE;
4491b3fc771SMaciej Żenczykowski }
4501b3fc771SMaciej Żenczykowski
451053c095aSJohannes Berg nlmsg_end(skb, nlh);
452053c095aSJohannes Berg return 0;
453c7d58aabSArnaldo Carvalho de Melo }
454c7d58aabSArnaldo Carvalho de Melo
inet_req_diag_fill(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,u16 nlmsg_flags,bool net_admin)455a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
4565682d393SMartin KaFai Lau struct netlink_callback *cb,
4575682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin)
458a58917f5SEric Dumazet {
459d545cacaSLorenzo Colitti struct request_sock *reqsk = inet_reqsk(sk);
460a58917f5SEric Dumazet struct inet_diag_msg *r;
461a58917f5SEric Dumazet struct nlmsghdr *nlh;
462a58917f5SEric Dumazet long tmo;
463a58917f5SEric Dumazet
4645682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4655682d393SMartin KaFai Lau cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
466a58917f5SEric Dumazet if (!nlh)
467a58917f5SEric Dumazet return -EMSGSIZE;
468a58917f5SEric Dumazet
469a58917f5SEric Dumazet r = nlmsg_data(nlh);
470a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk);
471a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV;
472a58917f5SEric Dumazet r->idiag_timer = 1;
473d545cacaSLorenzo Colitti r->idiag_retrans = reqsk->num_retrans;
474a58917f5SEric Dumazet
475a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
476a58917f5SEric Dumazet offsetof(struct sock, sk_cookie));
477a58917f5SEric Dumazet
478fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
4793828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo);
480a58917f5SEric Dumazet r->idiag_rqueue = 0;
481a58917f5SEric Dumazet r->idiag_wqueue = 0;
482a58917f5SEric Dumazet r->idiag_uid = 0;
483a58917f5SEric Dumazet r->idiag_inode = 0;
484a58917f5SEric Dumazet
485d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
486e33de7c5SWang Hai inet_rsk(reqsk)->ir_mark)) {
487e33de7c5SWang Hai nlmsg_cancel(skb, nlh);
488d545cacaSLorenzo Colitti return -EMSGSIZE;
489e33de7c5SWang Hai }
490d545cacaSLorenzo Colitti
491a58917f5SEric Dumazet nlmsg_end(skb, nlh);
492a58917f5SEric Dumazet return 0;
493a58917f5SEric Dumazet }
494a58917f5SEric 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)495dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
4965682d393SMartin KaFai Lau struct netlink_callback *cb,
49734160ea3SEric Dumazet const struct inet_diag_req_v2 *r,
4985682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin)
499dff2c035SArnaldo Carvalho de Melo {
500dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT)
5011b3fc771SMaciej Żenczykowski return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
502a58917f5SEric Dumazet
503a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV)
5045682d393SMartin KaFai Lau return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
505efe4208fSEric Dumazet
5065682d393SMartin KaFai Lau return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags,
5075682d393SMartin KaFai Lau net_admin);
508dff2c035SArnaldo Carvalho de Melo }
509dff2c035SArnaldo Carvalho de Melo
inet_diag_find_one_icsk(struct net * net,struct inet_hashinfo * hashinfo,const struct inet_diag_req_v2 * req)510b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net,
511b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo,
51234160ea3SEric Dumazet const struct inet_diag_req_v2 *req)
513a8c2190eSArnaldo Carvalho de Melo {
514e31c5e0eSEric Dumazet struct sock *sk;
515a8c2190eSArnaldo Carvalho de Melo
5162d331915SEric Dumazet rcu_read_lock();
517e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET)
518a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
519a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0],
520a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if);
521dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
5227c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) {
5237c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
5247c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
525a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
5267c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3],
5277c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if);
5287c130672SEric Dumazet else
529a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0,
530a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst,
531a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport,
532a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src,
533a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport,
534a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if);
5357c130672SEric Dumazet }
536a8c2190eSArnaldo Carvalho de Melo #endif
5372d331915SEric Dumazet else {
5382d331915SEric Dumazet rcu_read_unlock();
539b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL);
5402d331915SEric Dumazet }
5412d331915SEric Dumazet rcu_read_unlock();
542e31c5e0eSEric Dumazet if (!sk)
543b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT);
544a8c2190eSArnaldo Carvalho de Melo
545b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
546b613f56eSLorenzo Colitti sock_gen_put(sk);
547b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT);
548b613f56eSLorenzo Colitti }
549b613f56eSLorenzo Colitti
550b613f56eSLorenzo Colitti return sk;
551b613f56eSLorenzo Colitti }
552b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
553b613f56eSLorenzo Colitti
inet_diag_dump_one_icsk(struct inet_hashinfo * hashinfo,struct netlink_callback * cb,const struct inet_diag_req_v2 * req)554b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
5555682d393SMartin KaFai Lau struct netlink_callback *cb,
556b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req)
557b613f56eSLorenzo Colitti {
5585682d393SMartin KaFai Lau struct sk_buff *in_skb = cb->skb;
559b37e8840SIvan Delalande bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
560b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk);
561b613f56eSLorenzo Colitti struct sk_buff *rep;
562b613f56eSLorenzo Colitti struct sock *sk;
563b613f56eSLorenzo Colitti int err;
564b613f56eSLorenzo Colitti
565b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req);
566b613f56eSLorenzo Colitti if (IS_ERR(sk))
567b613f56eSLorenzo Colitti return PTR_ERR(sk);
568a8c2190eSArnaldo Carvalho de Melo
569b37e8840SIvan Delalande rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
5706e277ed5SThomas Graf if (!rep) {
5716e277ed5SThomas Graf err = -ENOMEM;
572a8c2190eSArnaldo Carvalho de Melo goto out;
5736e277ed5SThomas Graf }
574a8c2190eSArnaldo Carvalho de Melo
5755682d393SMartin KaFai Lau err = sk_diag_fill(sk, rep, cb, req, 0, net_admin);
57626932566SPatrick McHardy if (err < 0) {
57726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
5786e277ed5SThomas Graf nlmsg_free(rep);
57926932566SPatrick McHardy goto out;
58026932566SPatrick McHardy }
58101757f53SYajun Deng err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
582a8c2190eSArnaldo Carvalho de Melo
583a8c2190eSArnaldo Carvalho de Melo out:
584c1d607ccSEric Dumazet if (sk)
585c1d607ccSEric Dumazet sock_gen_put(sk);
586c1d607ccSEric Dumazet
587476f7dbfSPavel Emelyanov return err;
588476f7dbfSPavel Emelyanov }
5891942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
590476f7dbfSPavel 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)5916eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
592476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh,
5933f935c75SPaolo Abeni int hdrlen,
59434160ea3SEric Dumazet const struct inet_diag_req_v2 *req)
595476f7dbfSPavel Emelyanov {
596476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler;
5973f935c75SPaolo Abeni struct inet_diag_dump_data dump_data;
5983f935c75SPaolo Abeni int err, protocol;
599476f7dbfSPavel Emelyanov
6003f935c75SPaolo Abeni memset(&dump_data, 0, sizeof(dump_data));
601d5e4d0a5SEric Dumazet err = inet_diag_parse_attrs(nlh, hdrlen, dump_data.req_nlas);
602d5e4d0a5SEric Dumazet if (err)
603d5e4d0a5SEric Dumazet return err;
604d5e4d0a5SEric Dumazet
6053f935c75SPaolo Abeni protocol = inet_diag_get_protocol(req, &dump_data);
6063f935c75SPaolo Abeni
6073f935c75SPaolo Abeni handler = inet_diag_lock_handler(protocol);
6085682d393SMartin KaFai Lau if (IS_ERR(handler)) {
609476f7dbfSPavel Emelyanov err = PTR_ERR(handler);
6105682d393SMartin KaFai Lau } else if (cmd == SOCK_DIAG_BY_FAMILY) {
6115682d393SMartin KaFai Lau struct netlink_callback cb = {
6125682d393SMartin KaFai Lau .nlh = nlh,
6135682d393SMartin KaFai Lau .skb = in_skb,
6143f935c75SPaolo Abeni .data = &dump_data,
6155682d393SMartin KaFai Lau };
6165682d393SMartin KaFai Lau err = handler->dump_one(&cb, req);
6175682d393SMartin KaFai Lau } else if (cmd == SOCK_DESTROY && handler->destroy) {
6186eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req);
6195682d393SMartin KaFai Lau } else {
6206eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP;
6215682d393SMartin KaFai Lau }
622d523a328SHerbert Xu inet_diag_unlock_handler(handler);
623476f7dbfSPavel Emelyanov
624a8c2190eSArnaldo Carvalho de Melo return err;
625a8c2190eSArnaldo Carvalho de Melo }
626a8c2190eSArnaldo Carvalho de Melo
bitstring_match(const __be32 * a1,const __be32 * a2,int bits)6279f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
628a8c2190eSArnaldo Carvalho de Melo {
629a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5;
630a8c2190eSArnaldo Carvalho de Melo
631a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f;
632a8c2190eSArnaldo Carvalho de Melo
633a8c2190eSArnaldo Carvalho de Melo if (words) {
634a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2))
635a8c2190eSArnaldo Carvalho de Melo return 0;
636a8c2190eSArnaldo Carvalho de Melo }
637a8c2190eSArnaldo Carvalho de Melo if (bits) {
6389f855299SAl Viro __be32 w1, w2;
6399f855299SAl Viro __be32 mask;
640a8c2190eSArnaldo Carvalho de Melo
641a8c2190eSArnaldo Carvalho de Melo w1 = a1[words];
642a8c2190eSArnaldo Carvalho de Melo w2 = a2[words];
643a8c2190eSArnaldo Carvalho de Melo
644a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits));
645a8c2190eSArnaldo Carvalho de Melo
646a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask)
647a8c2190eSArnaldo Carvalho de Melo return 0;
648a8c2190eSArnaldo Carvalho de Melo }
649a8c2190eSArnaldo Carvalho de Melo
650a8c2190eSArnaldo Carvalho de Melo return 1;
651a8c2190eSArnaldo Carvalho de Melo }
652a8c2190eSArnaldo Carvalho de Melo
inet_diag_bc_run(const struct nlattr * _bc,const struct inet_diag_entry * entry)65387c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc,
654a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry)
655a8c2190eSArnaldo Carvalho de Melo {
65687c22ea5SPavel Emelyanov const void *bc = nla_data(_bc);
65787c22ea5SPavel Emelyanov int len = nla_len(_bc);
65887c22ea5SPavel Emelyanov
659a8c2190eSArnaldo Carvalho de Melo while (len > 0) {
660a8c2190eSArnaldo Carvalho de Melo int yes = 1;
661a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc;
662a8c2190eSArnaldo Carvalho de Melo
663a8c2190eSArnaldo Carvalho de Melo switch (op->code) {
664a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP:
665a8c2190eSArnaldo Carvalho de Melo break;
666a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP:
667a8c2190eSArnaldo Carvalho de Melo yes = 0;
668a8c2190eSArnaldo Carvalho de Melo break;
669bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ:
670bbb6189dSKristian Evensen yes = entry->sport == op[1].no;
671bbb6189dSKristian Evensen break;
672a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE:
673a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no;
674a8c2190eSArnaldo Carvalho de Melo break;
675a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE:
676b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no;
677a8c2190eSArnaldo Carvalho de Melo break;
678bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ:
679bbb6189dSKristian Evensen yes = entry->dport == op[1].no;
680bbb6189dSKristian Evensen break;
681a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE:
682a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no;
683a8c2190eSArnaldo Carvalho de Melo break;
684a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE:
685a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no;
686a8c2190eSArnaldo Carvalho de Melo break;
687a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO:
688a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
689a8c2190eSArnaldo Carvalho de Melo break;
690a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND:
691a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: {
692e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond;
693e31c5e0eSEric Dumazet const __be32 *addr;
694a8c2190eSArnaldo Carvalho de Melo
695e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1);
696a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 &&
697a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ?
698a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) {
699a8c2190eSArnaldo Carvalho de Melo yes = 0;
700a8c2190eSArnaldo Carvalho de Melo break;
701a8c2190eSArnaldo Carvalho de Melo }
702a8c2190eSArnaldo Carvalho de Melo
703a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND)
704a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr;
705a8c2190eSArnaldo Carvalho de Melo else
706a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr;
707a8c2190eSArnaldo Carvalho de Melo
708f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC &&
709f67caec9SNeal Cardwell cond->family != entry->family) {
710a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 &&
711a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) {
712a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 &&
713a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) &&
714f67caec9SNeal Cardwell bitstring_match(addr + 3,
715f67caec9SNeal Cardwell cond->addr,
716a8c2190eSArnaldo Carvalho de Melo cond->prefix_len))
717a8c2190eSArnaldo Carvalho de Melo break;
718a8c2190eSArnaldo Carvalho de Melo }
719a8c2190eSArnaldo Carvalho de Melo yes = 0;
720a8c2190eSArnaldo Carvalho de Melo break;
721a8c2190eSArnaldo Carvalho de Melo }
722f67caec9SNeal Cardwell
723f67caec9SNeal Cardwell if (cond->prefix_len == 0)
724f67caec9SNeal Cardwell break;
725f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr,
726f67caec9SNeal Cardwell cond->prefix_len))
727f67caec9SNeal Cardwell break;
728f67caec9SNeal Cardwell yes = 0;
729f67caec9SNeal Cardwell break;
730f67caec9SNeal Cardwell }
731637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: {
732637c841dSDavid Ahern u32 ifindex;
733637c841dSDavid Ahern
734637c841dSDavid Ahern ifindex = *((const u32 *)(op + 1));
735637c841dSDavid Ahern if (ifindex != entry->ifindex)
736637c841dSDavid Ahern yes = 0;
737637c841dSDavid Ahern break;
738637c841dSDavid Ahern }
739a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: {
740a52e95abSLorenzo Colitti struct inet_diag_markcond *cond;
741a52e95abSLorenzo Colitti
742a52e95abSLorenzo Colitti cond = (struct inet_diag_markcond *)(op + 1);
743a52e95abSLorenzo Colitti if ((entry->mark & cond->mask) != cond->mark)
744a52e95abSLorenzo Colitti yes = 0;
745a52e95abSLorenzo Colitti break;
746a52e95abSLorenzo Colitti }
747b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
748b1f3e43dSDmitry Yakunin case INET_DIAG_BC_CGROUP_COND: {
749b1f3e43dSDmitry Yakunin u64 cgroup_id;
750b1f3e43dSDmitry Yakunin
751b1f3e43dSDmitry Yakunin cgroup_id = get_unaligned((const u64 *)(op + 1));
752b1f3e43dSDmitry Yakunin if (cgroup_id != entry->cgroup_id)
753b1f3e43dSDmitry Yakunin yes = 0;
754b1f3e43dSDmitry Yakunin break;
755b1f3e43dSDmitry Yakunin }
756b1f3e43dSDmitry Yakunin #endif
757a8c2190eSArnaldo Carvalho de Melo }
758a8c2190eSArnaldo Carvalho de Melo
759a8c2190eSArnaldo Carvalho de Melo if (yes) {
760a8c2190eSArnaldo Carvalho de Melo len -= op->yes;
761a8c2190eSArnaldo Carvalho de Melo bc += op->yes;
762a8c2190eSArnaldo Carvalho de Melo } else {
763a8c2190eSArnaldo Carvalho de Melo len -= op->no;
764a8c2190eSArnaldo Carvalho de Melo bc += op->no;
765a8c2190eSArnaldo Carvalho de Melo }
766a8c2190eSArnaldo Carvalho de Melo }
767a02cec21SEric Dumazet return len == 0;
768a8c2190eSArnaldo Carvalho de Melo }
769a8c2190eSArnaldo Carvalho de Melo
770a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
771a4458343SEric Dumazet */
entry_fill_addrs(struct inet_diag_entry * entry,const struct sock * sk)772a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry,
773a4458343SEric Dumazet const struct sock *sk)
774a4458343SEric Dumazet {
775a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
776a4458343SEric Dumazet if (sk->sk_family == AF_INET6) {
777a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
778a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32;
779a4458343SEric Dumazet } else
780a4458343SEric Dumazet #endif
781a4458343SEric Dumazet {
782a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr;
783a4458343SEric Dumazet entry->daddr = &sk->sk_daddr;
784a4458343SEric Dumazet }
785a4458343SEric Dumazet }
786a4458343SEric Dumazet
inet_diag_bc_sk(const struct nlattr * bc,struct sock * sk)7878d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
7888d07d151SPavel Emelyanov {
7898d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk);
790e31c5e0eSEric Dumazet struct inet_diag_entry entry;
7918d07d151SPavel Emelyanov
792e31c5e0eSEric Dumazet if (!bc)
7938d07d151SPavel Emelyanov return 1;
7948d07d151SPavel Emelyanov
7958d07d151SPavel Emelyanov entry.family = sk->sk_family;
796a4458343SEric Dumazet entry_fill_addrs(&entry, sk);
7978d07d151SPavel Emelyanov entry.sport = inet->inet_num;
7988d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport);
799637c841dSDavid Ahern entry.ifindex = sk->sk_bound_dev_if;
800a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
801a52e95abSLorenzo Colitti if (sk_fullsock(sk))
8023c5b4d69SEric Dumazet entry.mark = READ_ONCE(sk->sk_mark);
803a52e95abSLorenzo Colitti else if (sk->sk_state == TCP_NEW_SYN_RECV)
804a52e95abSLorenzo Colitti entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
8051b3fc771SMaciej Żenczykowski else if (sk->sk_state == TCP_TIME_WAIT)
8061b3fc771SMaciej Żenczykowski entry.mark = inet_twsk(sk)->tw_mark;
807a52e95abSLorenzo Colitti else
808a52e95abSLorenzo Colitti entry.mark = 0;
809b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
810ee1bd483SDmitry Yakunin entry.cgroup_id = sk_fullsock(sk) ?
811ee1bd483SDmitry Yakunin cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)) : 0;
812b1f3e43dSDmitry Yakunin #endif
8138d07d151SPavel Emelyanov
8148d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry);
8158d07d151SPavel Emelyanov }
8168d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
8178d07d151SPavel Emelyanov
valid_cc(const void * bc,int len,int cc)818a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc)
819a8c2190eSArnaldo Carvalho de Melo {
820a8c2190eSArnaldo Carvalho de Melo while (len >= 0) {
821a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc;
822a8c2190eSArnaldo Carvalho de Melo
823a8c2190eSArnaldo Carvalho de Melo if (cc > len)
824a8c2190eSArnaldo Carvalho de Melo return 0;
825a8c2190eSArnaldo Carvalho de Melo if (cc == len)
826a8c2190eSArnaldo Carvalho de Melo return 1;
827eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3)
828a8c2190eSArnaldo Carvalho de Melo return 0;
829a8c2190eSArnaldo Carvalho de Melo len -= op->yes;
830a8c2190eSArnaldo Carvalho de Melo bc += op->yes;
831a8c2190eSArnaldo Carvalho de Melo }
832a8c2190eSArnaldo Carvalho de Melo return 0;
833a8c2190eSArnaldo Carvalho de Melo }
834a8c2190eSArnaldo Carvalho de Melo
835637c841dSDavid Ahern /* data is u32 ifindex */
valid_devcond(const struct inet_diag_bc_op * op,int len,int * min_len)836637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
837637c841dSDavid Ahern int *min_len)
838637c841dSDavid Ahern {
839637c841dSDavid Ahern /* Check ifindex space. */
840637c841dSDavid Ahern *min_len += sizeof(u32);
841637c841dSDavid Ahern if (len < *min_len)
842637c841dSDavid Ahern return false;
843637c841dSDavid Ahern
844637c841dSDavid Ahern return true;
845637c841dSDavid Ahern }
846405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */
valid_hostcond(const struct inet_diag_bc_op * op,int len,int * min_len)847405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
848405c0059SNeal Cardwell int *min_len)
849405c0059SNeal Cardwell {
850405c0059SNeal Cardwell struct inet_diag_hostcond *cond;
851e31c5e0eSEric Dumazet int addr_len;
852405c0059SNeal Cardwell
853405c0059SNeal Cardwell /* Check hostcond space. */
854405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond);
855405c0059SNeal Cardwell if (len < *min_len)
856405c0059SNeal Cardwell return false;
857405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1);
858405c0059SNeal Cardwell
859405c0059SNeal Cardwell /* Check address family and address length. */
860405c0059SNeal Cardwell switch (cond->family) {
861405c0059SNeal Cardwell case AF_UNSPEC:
862405c0059SNeal Cardwell addr_len = 0;
863405c0059SNeal Cardwell break;
864405c0059SNeal Cardwell case AF_INET:
865405c0059SNeal Cardwell addr_len = sizeof(struct in_addr);
866405c0059SNeal Cardwell break;
867405c0059SNeal Cardwell case AF_INET6:
868405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr);
869405c0059SNeal Cardwell break;
870405c0059SNeal Cardwell default:
871405c0059SNeal Cardwell return false;
872405c0059SNeal Cardwell }
873405c0059SNeal Cardwell *min_len += addr_len;
874405c0059SNeal Cardwell if (len < *min_len)
875405c0059SNeal Cardwell return false;
876405c0059SNeal Cardwell
877405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */
878405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len)
879405c0059SNeal Cardwell return false;
880405c0059SNeal Cardwell
881405c0059SNeal Cardwell return true;
882405c0059SNeal Cardwell }
883405c0059SNeal Cardwell
8845e1f5420SNeal Cardwell /* Validate a port comparison operator. */
valid_port_comparison(const struct inet_diag_bc_op * op,int len,int * min_len)885e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op,
8865e1f5420SNeal Cardwell int len, int *min_len)
8875e1f5420SNeal Cardwell {
8885e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
8895e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op);
8905e1f5420SNeal Cardwell if (len < *min_len)
8915e1f5420SNeal Cardwell return false;
8925e1f5420SNeal Cardwell return true;
8935e1f5420SNeal Cardwell }
8945e1f5420SNeal Cardwell
valid_markcond(const struct inet_diag_bc_op * op,int len,int * min_len)895a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
896a52e95abSLorenzo Colitti int *min_len)
897a8c2190eSArnaldo Carvalho de Melo {
898a52e95abSLorenzo Colitti *min_len += sizeof(struct inet_diag_markcond);
899a52e95abSLorenzo Colitti return len >= *min_len;
900a52e95abSLorenzo Colitti }
901a52e95abSLorenzo Colitti
902b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
valid_cgroupcond(const struct inet_diag_bc_op * op,int len,int * min_len)903b1f3e43dSDmitry Yakunin static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len,
904b1f3e43dSDmitry Yakunin int *min_len)
905b1f3e43dSDmitry Yakunin {
906b1f3e43dSDmitry Yakunin *min_len += sizeof(u64);
907b1f3e43dSDmitry Yakunin return len >= *min_len;
908b1f3e43dSDmitry Yakunin }
909b1f3e43dSDmitry Yakunin #endif
910b1f3e43dSDmitry Yakunin
inet_diag_bc_audit(const struct nlattr * attr,const struct sk_buff * skb)911a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr,
912a52e95abSLorenzo Colitti const struct sk_buff *skb)
913a52e95abSLorenzo Colitti {
914a52e95abSLorenzo Colitti bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
915627cc4adSLorenzo Colitti const void *bytecode, *bc;
916627cc4adSLorenzo Colitti int bytecode_len, len;
917627cc4adSLorenzo Colitti
918627cc4adSLorenzo Colitti if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
919627cc4adSLorenzo Colitti return -EINVAL;
920627cc4adSLorenzo Colitti
921627cc4adSLorenzo Colitti bytecode = bc = nla_data(attr);
922627cc4adSLorenzo Colitti len = bytecode_len = nla_len(attr);
923a8c2190eSArnaldo Carvalho de Melo
924a8c2190eSArnaldo Carvalho de Melo while (len > 0) {
925405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op);
926e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc;
927a8c2190eSArnaldo Carvalho de Melo
928a8c2190eSArnaldo Carvalho de Melo switch (op->code) {
929a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND:
930a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND:
931405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len))
932405c0059SNeal Cardwell return -EINVAL;
9335e1f5420SNeal Cardwell break;
934637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND:
935637c841dSDavid Ahern if (!valid_devcond(bc, len, &min_len))
936637c841dSDavid Ahern return -EINVAL;
937637c841dSDavid Ahern break;
938bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ:
939a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE:
940a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE:
941bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ:
942a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE:
943a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE:
9445e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len))
945a8c2190eSArnaldo Carvalho de Melo return -EINVAL;
946a8c2190eSArnaldo Carvalho de Melo break;
947a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND:
948a52e95abSLorenzo Colitti if (!net_admin)
949a52e95abSLorenzo Colitti return -EPERM;
950a52e95abSLorenzo Colitti if (!valid_markcond(bc, len, &min_len))
951a52e95abSLorenzo Colitti return -EINVAL;
952a52e95abSLorenzo Colitti break;
953b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA
954b1f3e43dSDmitry Yakunin case INET_DIAG_BC_CGROUP_COND:
955b1f3e43dSDmitry Yakunin if (!valid_cgroupcond(bc, len, &min_len))
956b1f3e43dSDmitry Yakunin return -EINVAL;
957b1f3e43dSDmitry Yakunin break;
958b1f3e43dSDmitry Yakunin #endif
9595e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO:
9605e1f5420SNeal Cardwell case INET_DIAG_BC_JMP:
961a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP:
962a8c2190eSArnaldo Carvalho de Melo break;
963a8c2190eSArnaldo Carvalho de Melo default:
964a8c2190eSArnaldo Carvalho de Melo return -EINVAL;
965a8c2190eSArnaldo Carvalho de Melo }
9665e1f5420SNeal Cardwell
9675e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) {
9685e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3)
9695e1f5420SNeal Cardwell return -EINVAL;
9705e1f5420SNeal Cardwell if (op->no < len &&
9715e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no))
9725e1f5420SNeal Cardwell return -EINVAL;
9735e1f5420SNeal Cardwell }
9745e1f5420SNeal Cardwell
975405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
976eeb14972SEric Dumazet return -EINVAL;
977a8c2190eSArnaldo Carvalho de Melo bc += op->yes;
978a8c2190eSArnaldo Carvalho de Melo len -= op->yes;
979a8c2190eSArnaldo Carvalho de Melo }
980a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL;
981a8c2190eSArnaldo Carvalho de Melo }
982a8c2190eSArnaldo Carvalho de Melo
twsk_build_assert(void)98349612729SEric Dumazet static void twsk_build_assert(void)
98449612729SEric Dumazet {
98549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
98649612729SEric Dumazet offsetof(struct sock, sk_family));
98749612729SEric Dumazet
98849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
98949612729SEric Dumazet offsetof(struct inet_sock, inet_num));
99049612729SEric Dumazet
99149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
99249612729SEric Dumazet offsetof(struct inet_sock, inet_dport));
99349612729SEric Dumazet
99449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
99549612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr));
99649612729SEric Dumazet
99749612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
99849612729SEric Dumazet offsetof(struct inet_sock, inet_daddr));
99949612729SEric Dumazet
100049612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
100149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
100249612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr));
100349612729SEric Dumazet
100449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
100549612729SEric Dumazet offsetof(struct sock, sk_v6_daddr));
100649612729SEric Dumazet #endif
100749612729SEric Dumazet }
100849612729SEric Dumazet
inet_diag_dump_icsk(struct inet_hashinfo * hashinfo,struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r)10091942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
1010e31c5e0eSEric Dumazet struct netlink_callback *cb,
10110df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r)
1012a8c2190eSArnaldo Carvalho de Melo {
1013d545cacaSLorenzo Colitti bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
10140df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data;
101567db3e4bSEric Dumazet struct net *net = sock_net(skb->sk);
101667db3e4bSEric Dumazet u32 idiag_states = r->idiag_states;
101767db3e4bSEric Dumazet int i, num, s_i, s_num;
10180df6d328SMartin KaFai Lau struct nlattr *bc;
101967db3e4bSEric Dumazet struct sock *sk;
1020a8c2190eSArnaldo Carvalho de Melo
10210df6d328SMartin KaFai Lau bc = cb_data->inet_diag_nla_bc;
1022079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV)
1023079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV;
1024a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1];
1025a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2];
1026a8c2190eSArnaldo Carvalho de Melo
1027a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) {
10289652dc2eSEric Dumazet if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
1029a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht;
1030a8c2190eSArnaldo Carvalho de Melo
1031cae3873cSMartin KaFai Lau for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
10325caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb;
10338dbd76e7SEric Dumazet struct hlist_nulls_node *node;
1034a8c2190eSArnaldo Carvalho de Melo
1035a8c2190eSArnaldo Carvalho de Melo num = 0;
1036cae3873cSMartin KaFai Lau ilb = &hashinfo->lhash2[i];
1037cae3873cSMartin KaFai Lau
10389652dc2eSEric Dumazet spin_lock(&ilb->lock);
10398dbd76e7SEric Dumazet sk_nulls_for_each(sk, node, &ilb->nulls_head) {
1040a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk);
1041a8c2190eSArnaldo Carvalho de Melo
104251d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net))
104351d7cccfSAndrey Vagin continue;
104451d7cccfSAndrey Vagin
1045a8c2190eSArnaldo Carvalho de Melo if (num < s_num) {
1046a8c2190eSArnaldo Carvalho de Melo num++;
1047a8c2190eSArnaldo Carvalho de Melo continue;
1048a8c2190eSArnaldo Carvalho de Melo }
1049a8c2190eSArnaldo Carvalho de Melo
1050d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC &&
1051d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family)
1052d23deaa0SPavel Emelyanov goto next_listen;
1053d23deaa0SPavel Emelyanov
1054c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport &&
1055a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport)
1056a8c2190eSArnaldo Carvalho de Melo goto next_listen;
1057a8c2190eSArnaldo Carvalho de Melo
10585682d393SMartin KaFai Lau if (!inet_diag_bc_sk(bc, sk))
10595682d393SMartin KaFai Lau goto next_listen;
10605682d393SMartin KaFai Lau
10615682d393SMartin KaFai Lau if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
10625682d393SMartin KaFai Lau cb, r, NLM_F_MULTI,
10635682d393SMartin KaFai Lau net_admin) < 0) {
10649652dc2eSEric Dumazet spin_unlock(&ilb->lock);
1065a8c2190eSArnaldo Carvalho de Melo goto done;
1066a8c2190eSArnaldo Carvalho de Melo }
1067a8c2190eSArnaldo Carvalho de Melo
1068a8c2190eSArnaldo Carvalho de Melo next_listen:
1069a8c2190eSArnaldo Carvalho de Melo ++num;
1070a8c2190eSArnaldo Carvalho de Melo }
10719652dc2eSEric Dumazet spin_unlock(&ilb->lock);
1072a8c2190eSArnaldo Carvalho de Melo
1073a8c2190eSArnaldo Carvalho de Melo s_num = 0;
1074a8c2190eSArnaldo Carvalho de Melo }
1075a8c2190eSArnaldo Carvalho de Melo skip_listen_ht:
1076a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1;
1077a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0;
1078a8c2190eSArnaldo Carvalho de Melo }
1079a8c2190eSArnaldo Carvalho de Melo
1080079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN))
1081efb3cb42SPavel Emelyanov goto out;
1082a8c2190eSArnaldo Carvalho de Melo
108367db3e4bSEric Dumazet #define SKARR_SZ 16
1084f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) {
1085a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i];
10867e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
10873ab5aee7SEric Dumazet struct hlist_nulls_node *node;
108867db3e4bSEric Dumazet struct sock *sk_arr[SKARR_SZ];
108967db3e4bSEric Dumazet int num_arr[SKARR_SZ];
109067db3e4bSEric Dumazet int idx, accum, res;
10916be547a6SAndi Kleen
109205dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain))
10936be547a6SAndi Kleen continue;
10946be547a6SAndi Kleen
1095a8c2190eSArnaldo Carvalho de Melo if (i > s_i)
1096a8c2190eSArnaldo Carvalho de Melo s_num = 0;
1097a8c2190eSArnaldo Carvalho de Melo
109867db3e4bSEric Dumazet next_chunk:
109967db3e4bSEric Dumazet num = 0;
110067db3e4bSEric Dumazet accum = 0;
11017e3aab4aSDavid S. Miller spin_lock_bh(lock);
11023ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) {
110367db3e4bSEric Dumazet int state;
1104a8c2190eSArnaldo Carvalho de Melo
110551d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net))
110651d7cccfSAndrey Vagin continue;
1107a8c2190eSArnaldo Carvalho de Melo if (num < s_num)
1108a8c2190eSArnaldo Carvalho de Melo goto next_normal;
110970315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ?
111070315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state;
1111079096f1SEric Dumazet if (!(idiag_states & (1 << state)))
1112a8c2190eSArnaldo Carvalho de Melo goto next_normal;
1113d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC &&
1114d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family)
1115d23deaa0SPavel Emelyanov goto next_normal;
111605dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) &&
1117a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport)
1118a8c2190eSArnaldo Carvalho de Melo goto next_normal;
111905dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport &&
11204e852c02SArnaldo Carvalho de Melo r->id.idiag_dport)
1121a8c2190eSArnaldo Carvalho de Melo goto next_normal;
1122a58917f5SEric Dumazet twsk_build_assert();
1123a58917f5SEric Dumazet
1124a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk))
1125a58917f5SEric Dumazet goto next_normal;
1126a58917f5SEric Dumazet
1127f0c928d8SEric Dumazet if (!refcount_inc_not_zero(&sk->sk_refcnt))
1128f0c928d8SEric Dumazet goto next_normal;
1129f0c928d8SEric Dumazet
113067db3e4bSEric Dumazet num_arr[accum] = num;
113167db3e4bSEric Dumazet sk_arr[accum] = sk;
113267db3e4bSEric Dumazet if (++accum == SKARR_SZ)
113367db3e4bSEric Dumazet break;
113467db3e4bSEric Dumazet next_normal:
113567db3e4bSEric Dumazet ++num;
113667db3e4bSEric Dumazet }
113767db3e4bSEric Dumazet spin_unlock_bh(lock);
113867db3e4bSEric Dumazet res = 0;
113967db3e4bSEric Dumazet for (idx = 0; idx < accum; idx++) {
114067db3e4bSEric Dumazet if (res >= 0) {
11415682d393SMartin KaFai Lau res = sk_diag_fill(sk_arr[idx], skb, cb, r,
11425682d393SMartin KaFai Lau NLM_F_MULTI, net_admin);
114367db3e4bSEric Dumazet if (res < 0)
114467db3e4bSEric Dumazet num = num_arr[idx];
1145a8c2190eSArnaldo Carvalho de Melo }
114667db3e4bSEric Dumazet sock_gen_put(sk_arr[idx]);
1147a8c2190eSArnaldo Carvalho de Melo }
114867db3e4bSEric Dumazet if (res < 0)
114967db3e4bSEric Dumazet break;
1150acffb584SEric Dumazet cond_resched();
115167db3e4bSEric Dumazet if (accum == SKARR_SZ) {
115267db3e4bSEric Dumazet s_num = num + 1;
115367db3e4bSEric Dumazet goto next_chunk;
115467db3e4bSEric Dumazet }
1155a8c2190eSArnaldo Carvalho de Melo }
1156a8c2190eSArnaldo Carvalho de Melo
1157a8c2190eSArnaldo Carvalho de Melo done:
1158a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i;
1159a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num;
1160efb3cb42SPavel Emelyanov out:
1161efb3cb42SPavel Emelyanov ;
1162efb3cb42SPavel Emelyanov }
11631942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
1164efb3cb42SPavel Emelyanov
__inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r)1165efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
11660df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r)
1167efb3cb42SPavel Emelyanov {
11683f935c75SPaolo Abeni struct inet_diag_dump_data *cb_data = cb->data;
1169efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler;
1170085c20caSMartin KaFai Lau u32 prev_min_dump_alloc;
11713f935c75SPaolo Abeni int protocol, err = 0;
11723f935c75SPaolo Abeni
11733f935c75SPaolo Abeni protocol = inet_diag_get_protocol(r, cb_data);
1174efb3cb42SPavel Emelyanov
1175085c20caSMartin KaFai Lau again:
1176085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc;
11773f935c75SPaolo Abeni handler = inet_diag_lock_handler(protocol);
1178efb3cb42SPavel Emelyanov if (!IS_ERR(handler))
11790df6d328SMartin KaFai Lau handler->dump(skb, cb, r);
1180cacb6ba0SCyrill Gorcunov else
1181cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler);
1182d523a328SHerbert Xu inet_diag_unlock_handler(handler);
1183efb3cb42SPavel Emelyanov
1184085c20caSMartin KaFai Lau /* The skb is not large enough to fit one sk info and
1185085c20caSMartin KaFai Lau * inet_sk_diag_fill() has requested for a larger skb.
1186085c20caSMartin KaFai Lau */
1187085c20caSMartin KaFai Lau if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) {
1188085c20caSMartin KaFai Lau err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL);
1189085c20caSMartin KaFai Lau if (!err)
1190085c20caSMartin KaFai Lau goto again;
1191085c20caSMartin KaFai Lau }
1192085c20caSMartin KaFai Lau
1193cacb6ba0SCyrill Gorcunov return err ? : skb->len;
1194a8c2190eSArnaldo Carvalho de Melo }
1195a8c2190eSArnaldo Carvalho de Melo
inet_diag_dump(struct sk_buff * skb,struct netlink_callback * cb)119625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
119725c4cd2bSPavel Emelyanov {
11980df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh));
11990df6d328SMartin KaFai Lau }
120025c4cd2bSPavel Emelyanov
__inet_diag_dump_start(struct netlink_callback * cb,int hdrlen)12010df6d328SMartin KaFai Lau static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen)
12020df6d328SMartin KaFai Lau {
12030df6d328SMartin KaFai Lau const struct nlmsghdr *nlh = cb->nlh;
12040df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data;
12050df6d328SMartin KaFai Lau struct sk_buff *skb = cb->skb;
12060df6d328SMartin KaFai Lau struct nlattr *nla;
12073f935c75SPaolo Abeni int err;
120825c4cd2bSPavel Emelyanov
12090df6d328SMartin KaFai Lau cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL);
12100df6d328SMartin KaFai Lau if (!cb_data)
12110df6d328SMartin KaFai Lau return -ENOMEM;
12120df6d328SMartin KaFai Lau
1213d5e4d0a5SEric Dumazet err = inet_diag_parse_attrs(nlh, hdrlen, cb_data->req_nlas);
1214d5e4d0a5SEric Dumazet if (err) {
1215d5e4d0a5SEric Dumazet kfree(cb_data);
1216d5e4d0a5SEric Dumazet return err;
1217d5e4d0a5SEric Dumazet }
12180df6d328SMartin KaFai Lau nla = cb_data->inet_diag_nla_bc;
12190df6d328SMartin KaFai Lau if (nla) {
12200df6d328SMartin KaFai Lau err = inet_diag_bc_audit(nla, skb);
12210df6d328SMartin KaFai Lau if (err) {
12220df6d328SMartin KaFai Lau kfree(cb_data);
12230df6d328SMartin KaFai Lau return err;
12240df6d328SMartin KaFai Lau }
12250df6d328SMartin KaFai Lau }
12260df6d328SMartin KaFai Lau
1227085c20caSMartin KaFai Lau nla = cb_data->inet_diag_nla_bpf_stgs;
1228085c20caSMartin KaFai Lau if (nla) {
1229085c20caSMartin KaFai Lau struct bpf_sk_storage_diag *bpf_stg_diag;
1230085c20caSMartin KaFai Lau
1231085c20caSMartin KaFai Lau bpf_stg_diag = bpf_sk_storage_diag_alloc(nla);
1232085c20caSMartin KaFai Lau if (IS_ERR(bpf_stg_diag)) {
1233085c20caSMartin KaFai Lau kfree(cb_data);
1234085c20caSMartin KaFai Lau return PTR_ERR(bpf_stg_diag);
1235085c20caSMartin KaFai Lau }
1236085c20caSMartin KaFai Lau cb_data->bpf_stg_diag = bpf_stg_diag;
1237085c20caSMartin KaFai Lau }
1238085c20caSMartin KaFai Lau
12390df6d328SMartin KaFai Lau cb->data = cb_data;
12400df6d328SMartin KaFai Lau return 0;
12410df6d328SMartin KaFai Lau }
12420df6d328SMartin KaFai Lau
inet_diag_dump_start(struct netlink_callback * cb)12430df6d328SMartin KaFai Lau static int inet_diag_dump_start(struct netlink_callback *cb)
12440df6d328SMartin KaFai Lau {
12450df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2));
12460df6d328SMartin KaFai Lau }
12470df6d328SMartin KaFai Lau
inet_diag_dump_start_compat(struct netlink_callback * cb)12480df6d328SMartin KaFai Lau static int inet_diag_dump_start_compat(struct netlink_callback *cb)
12490df6d328SMartin KaFai Lau {
12500df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
12510df6d328SMartin KaFai Lau }
12520df6d328SMartin KaFai Lau
inet_diag_dump_done(struct netlink_callback * cb)12530df6d328SMartin KaFai Lau static int inet_diag_dump_done(struct netlink_callback *cb)
12540df6d328SMartin KaFai Lau {
1255085c20caSMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data;
1256085c20caSMartin KaFai Lau
1257085c20caSMartin KaFai Lau bpf_sk_storage_diag_free(cb_data->bpf_stg_diag);
12580df6d328SMartin KaFai Lau kfree(cb->data);
12590df6d328SMartin KaFai Lau
12600df6d328SMartin KaFai Lau return 0;
126125c4cd2bSPavel Emelyanov }
126225c4cd2bSPavel Emelyanov
inet_diag_type2proto(int type)1263e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type)
1264a029fe26SPavel Emelyanov {
1265a029fe26SPavel Emelyanov switch (type) {
1266a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK:
1267a029fe26SPavel Emelyanov return IPPROTO_TCP;
1268a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK:
1269a029fe26SPavel Emelyanov return IPPROTO_DCCP;
1270a029fe26SPavel Emelyanov default:
1271a029fe26SPavel Emelyanov return 0;
1272a029fe26SPavel Emelyanov }
1273a029fe26SPavel Emelyanov }
1274a029fe26SPavel Emelyanov
inet_diag_dump_compat(struct sk_buff * skb,struct netlink_callback * cb)1275e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb,
1276e31c5e0eSEric Dumazet struct netlink_callback *cb)
127725c4cd2bSPavel Emelyanov {
1278d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1279c8991362SPavel Emelyanov struct inet_diag_req_v2 req;
128025c4cd2bSPavel Emelyanov
1281d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */
128225c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
128325c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext;
128476965648SShigeru Yoshida req.pad = 0;
128525c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states;
128625c4cd2bSPavel Emelyanov req.id = rc->id;
128725c4cd2bSPavel Emelyanov
12880df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, &req);
128925c4cd2bSPavel Emelyanov }
129025c4cd2bSPavel Emelyanov
inet_diag_get_exact_compat(struct sk_buff * in_skb,const struct nlmsghdr * nlh)1291fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1292fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh)
1293fe50ce28SPavel Emelyanov {
1294d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh);
1295c8991362SPavel Emelyanov struct inet_diag_req_v2 req;
1296fe50ce28SPavel Emelyanov
1297fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family;
1298fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1299fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext;
130076965648SShigeru Yoshida req.pad = 0;
1301fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states;
1302fe50ce28SPavel Emelyanov req.id = rc->id;
1303fe50ce28SPavel Emelyanov
13043f935c75SPaolo Abeni return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh,
13053f935c75SPaolo Abeni sizeof(struct inet_diag_req), &req);
1306fe50ce28SPavel Emelyanov }
1307fe50ce28SPavel Emelyanov
inet_diag_rcv_msg_compat(struct sk_buff * skb,struct nlmsghdr * nlh)13088d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1309a8c2190eSArnaldo Carvalho de Melo {
13103b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req);
131151d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk);
1312a8c2190eSArnaldo Carvalho de Melo
1313ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1314ead592baSThomas Graf nlmsg_len(nlh) < hdrlen)
1315ead592baSThomas Graf return -EINVAL;
1316a8c2190eSArnaldo Carvalho de Melo
1317b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) {
131880d326faSPablo Neira Ayuso struct netlink_dump_control c = {
13190df6d328SMartin KaFai Lau .start = inet_diag_dump_start_compat,
13200df6d328SMartin KaFai Lau .done = inet_diag_dump_done,
132180d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat,
132280d326faSPablo Neira Ayuso };
132351d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
132480d326faSPablo Neira Ayuso }
1325ead592baSThomas Graf
1326fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh);
1327a8c2190eSArnaldo Carvalho de Melo }
1328a8c2190eSArnaldo Carvalho de Melo
inet_diag_handler_cmd(struct sk_buff * skb,struct nlmsghdr * h)13296eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1330d366477aSPavel Emelyanov {
1331c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2);
133251d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk);
1333d366477aSPavel Emelyanov
1334d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen)
1335d366477aSPavel Emelyanov return -EINVAL;
1336d366477aSPavel Emelyanov
13376eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
13386eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) {
133980d326faSPablo Neira Ayuso struct netlink_dump_control c = {
13400df6d328SMartin KaFai Lau .start = inet_diag_dump_start,
13410df6d328SMartin KaFai Lau .done = inet_diag_dump_done,
134280d326faSPablo Neira Ayuso .dump = inet_diag_dump,
134380d326faSPablo Neira Ayuso };
134451d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c);
134580d326faSPablo Neira Ayuso }
1346d366477aSPavel Emelyanov
13473f935c75SPaolo Abeni return inet_diag_cmd_exact(h->nlmsg_type, skb, h, hdrlen,
13483f935c75SPaolo Abeni nlmsg_data(h));
1349d366477aSPavel Emelyanov }
1350d366477aSPavel Emelyanov
135135ac838aSCraig Gallek static
inet_diag_handler_get_info(struct sk_buff * skb,struct sock * sk)135235ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
135335ac838aSCraig Gallek {
135435ac838aSCraig Gallek const struct inet_diag_handler *handler;
135535ac838aSCraig Gallek struct nlmsghdr *nlh;
135635ac838aSCraig Gallek struct nlattr *attr;
135735ac838aSCraig Gallek struct inet_diag_msg *r;
135835ac838aSCraig Gallek void *info = NULL;
135935ac838aSCraig Gallek int err = 0;
136035ac838aSCraig Gallek
136135ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
136235ac838aSCraig Gallek if (!nlh)
136335ac838aSCraig Gallek return -ENOMEM;
136435ac838aSCraig Gallek
136535ac838aSCraig Gallek r = nlmsg_data(nlh);
136635ac838aSCraig Gallek memset(r, 0, sizeof(*r));
136735ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk);
1368e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1369e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport;
137035ac838aSCraig Gallek r->idiag_state = sk->sk_state;
137135ac838aSCraig Gallek
137235ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
137335ac838aSCraig Gallek nlmsg_cancel(skb, nlh);
137435ac838aSCraig Gallek return err;
137535ac838aSCraig Gallek }
137635ac838aSCraig Gallek
137735ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol);
137835ac838aSCraig Gallek if (IS_ERR(handler)) {
137935ac838aSCraig Gallek inet_diag_unlock_handler(handler);
138035ac838aSCraig Gallek nlmsg_cancel(skb, nlh);
138135ac838aSCraig Gallek return PTR_ERR(handler);
138235ac838aSCraig Gallek }
138335ac838aSCraig Gallek
138435ac838aSCraig Gallek attr = handler->idiag_info_size
13856ed46d12SNicolas Dichtel ? nla_reserve_64bit(skb, INET_DIAG_INFO,
13866ed46d12SNicolas Dichtel handler->idiag_info_size,
13876ed46d12SNicolas Dichtel INET_DIAG_PAD)
138835ac838aSCraig Gallek : NULL;
138935ac838aSCraig Gallek if (attr)
139035ac838aSCraig Gallek info = nla_data(attr);
139135ac838aSCraig Gallek
139235ac838aSCraig Gallek handler->idiag_get_info(sk, r, info);
139335ac838aSCraig Gallek inet_diag_unlock_handler(handler);
139435ac838aSCraig Gallek
139535ac838aSCraig Gallek nlmsg_end(skb, nlh);
139635ac838aSCraig Gallek return 0;
139735ac838aSCraig Gallek }
139835ac838aSCraig Gallek
13998dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = {
140037103a9dSEric Dumazet .owner = THIS_MODULE,
1401d366477aSPavel Emelyanov .family = AF_INET,
14026eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd,
140335ac838aSCraig Gallek .get_info = inet_diag_handler_get_info,
14046eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd,
1405d366477aSPavel Emelyanov };
1406d366477aSPavel Emelyanov
14078dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = {
140837103a9dSEric Dumazet .owner = THIS_MODULE,
1409d366477aSPavel Emelyanov .family = AF_INET6,
14106eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd,
141135ac838aSCraig Gallek .get_info = inet_diag_handler_get_info,
14126eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd,
1413d366477aSPavel Emelyanov };
1414d366477aSPavel Emelyanov
inet_diag_register(const struct inet_diag_handler * h)1415a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h)
1416a8c2190eSArnaldo Carvalho de Melo {
1417a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type;
1418a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL;
1419a8c2190eSArnaldo Carvalho de Melo
1420f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX)
1421a8c2190eSArnaldo Carvalho de Melo goto out;
1422a8c2190eSArnaldo Carvalho de Melo
1423d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex);
1424a8c2190eSArnaldo Carvalho de Melo err = -EEXIST;
1425e31c5e0eSEric Dumazet if (!inet_diag_table[type]) {
14269311cc66SEric Dumazet WRITE_ONCE(inet_diag_table[type], h);
1427a8c2190eSArnaldo Carvalho de Melo err = 0;
1428a8c2190eSArnaldo Carvalho de Melo }
1429d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex);
1430a8c2190eSArnaldo Carvalho de Melo out:
1431a8c2190eSArnaldo Carvalho de Melo return err;
1432a8c2190eSArnaldo Carvalho de Melo }
1433a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register);
1434a8c2190eSArnaldo Carvalho de Melo
inet_diag_unregister(const struct inet_diag_handler * h)1435a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h)
1436a8c2190eSArnaldo Carvalho de Melo {
1437a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type;
1438a8c2190eSArnaldo Carvalho de Melo
1439f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX)
1440a8c2190eSArnaldo Carvalho de Melo return;
1441a8c2190eSArnaldo Carvalho de Melo
1442d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex);
14439311cc66SEric Dumazet WRITE_ONCE(inet_diag_table[type], NULL);
1444d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex);
1445a8c2190eSArnaldo Carvalho de Melo }
1446a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister);
1447a8c2190eSArnaldo Carvalho de Melo
1448*bba7266dSEric Dumazet static const struct sock_diag_inet_compat inet_diag_compat = {
1449*bba7266dSEric Dumazet .owner = THIS_MODULE,
1450*bba7266dSEric Dumazet .fn = inet_diag_rcv_msg_compat,
1451*bba7266dSEric Dumazet };
1452*bba7266dSEric Dumazet
inet_diag_init(void)1453a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void)
1454a8c2190eSArnaldo Carvalho de Melo {
1455f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX *
1456a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *));
1457a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM;
1458a8c2190eSArnaldo Carvalho de Melo
14590da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1460a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table)
1461a8c2190eSArnaldo Carvalho de Melo goto out;
1462a8c2190eSArnaldo Carvalho de Melo
1463d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler);
1464d366477aSPavel Emelyanov if (err)
1465d366477aSPavel Emelyanov goto out_free_nl;
1466d366477aSPavel Emelyanov
1467d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler);
1468d366477aSPavel Emelyanov if (err)
1469d366477aSPavel Emelyanov goto out_free_inet;
1470d366477aSPavel Emelyanov
1471*bba7266dSEric Dumazet sock_diag_register_inet_compat(&inet_diag_compat);
1472a8c2190eSArnaldo Carvalho de Melo out:
1473a8c2190eSArnaldo Carvalho de Melo return err;
1474d366477aSPavel Emelyanov
1475d366477aSPavel Emelyanov out_free_inet:
1476d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler);
1477d366477aSPavel Emelyanov out_free_nl:
1478a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table);
1479a8c2190eSArnaldo Carvalho de Melo goto out;
1480a8c2190eSArnaldo Carvalho de Melo }
1481a8c2190eSArnaldo Carvalho de Melo
inet_diag_exit(void)1482a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void)
1483a8c2190eSArnaldo Carvalho de Melo {
1484d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler);
1485d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler);
1486*bba7266dSEric Dumazet sock_diag_unregister_inet_compat(&inet_diag_compat);
1487a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table);
1488a8c2190eSArnaldo Carvalho de Melo }
1489a8c2190eSArnaldo Carvalho de Melo
1490a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init);
1491a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit);
1492a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
1493aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1494aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);
1495