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 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 60f13c95f0SPavel Emelyanov if (!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 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 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 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 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 153d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 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)); 185c1077616SWei Wang inet_sockopt.recverr = inet->recverr; 186c1077616SWei Wang inet_sockopt.is_icsk = inet->is_icsk; 187c1077616SWei Wang inet_sockopt.freebind = inet->freebind; 188c1077616SWei Wang inet_sockopt.hdrincl = inet->hdrincl; 189c1077616SWei Wang inet_sockopt.mc_loop = inet->mc_loop; 190c1077616SWei Wang inet_sockopt.transparent = inet->transparent; 191c1077616SWei Wang inet_sockopt.mc_all = inet->mc_all; 192c1077616SWei Wang inet_sockopt.nodefrag = inet->nodefrag; 193c1077616SWei Wang inet_sockopt.bind_address_no_port = inet->bind_address_no_port; 194c1077616SWei Wang inet_sockopt.recverr_rfc4884 = inet->recverr_rfc4884; 195c1077616SWei Wang inet_sockopt.defer_connect = inet->defer_connect; 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 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 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 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; 264cb2050a7SXin Long 2655682d393SMartin KaFai Lau if (inet_diag_msg_attrs_fill(sk, skb, r, ext, 2665682d393SMartin KaFai Lau sk_user_ns(NETLINK_CB(cb->skb).sk), 2675682d393SMartin KaFai Lau net_admin)) 268cb2050a7SXin Long goto errout; 269cb2050a7SXin Long 2706e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 2716e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 2726e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 273ab4e846aSEric Dumazet .idiag_wmem = READ_ONCE(sk->sk_wmem_queued), 2746e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 2756e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 2766e277ed5SThomas Graf }; 2776e277ed5SThomas Graf 2786e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 2796e277ed5SThomas Graf goto errout; 2803c4d05c8SPavel Emelyanov } 2813c4d05c8SPavel Emelyanov 282c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 283c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 2846e277ed5SThomas Graf goto errout; 285c0636faaSPavel Emelyanov 286432490f9SCyrill Gorcunov /* 287432490f9SCyrill Gorcunov * RAW sockets might have user-defined protocols assigned, 288432490f9SCyrill Gorcunov * so report the one supplied on socket creation. 289432490f9SCyrill Gorcunov */ 290432490f9SCyrill Gorcunov if (sk->sk_type == SOCK_RAW) { 291432490f9SCyrill Gorcunov if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) 292432490f9SCyrill Gorcunov goto errout; 293432490f9SCyrill Gorcunov } 294432490f9SCyrill Gorcunov 295e31c5e0eSEric Dumazet if (!icsk) { 29662ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 2973c4d05c8SPavel Emelyanov goto out; 2983c4d05c8SPavel Emelyanov } 2993c4d05c8SPavel Emelyanov 3006ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 30157dde7f7SYuchung Cheng icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 3026ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 303a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 304a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 305b7de529cSXin Long r->idiag_expires = 3063828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 307a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 308a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 309a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 310b7de529cSXin Long r->idiag_expires = 3113828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 312a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 313a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 314a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 315b7de529cSXin Long r->idiag_expires = 3163828a93fSEric Dumazet jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies); 317a8c2190eSArnaldo Carvalho de Melo } else { 318a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 319a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 320a8c2190eSArnaldo Carvalho de Melo } 321a8c2190eSArnaldo Carvalho de Melo 3223fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 3236ed46d12SNicolas Dichtel attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 3246ed46d12SNicolas Dichtel handler->idiag_info_size, 3256ed46d12SNicolas Dichtel INET_DIAG_PAD); 3266e277ed5SThomas Graf if (!attr) 3276e277ed5SThomas Graf goto errout; 328a8c2190eSArnaldo Carvalho de Melo 3296e277ed5SThomas Graf info = nla_data(attr); 330a8c2190eSArnaldo Carvalho de Melo } 331a8c2190eSArnaldo Carvalho de Melo 332521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) { 333521f1cf1SEric Dumazet int err = 0; 334521f1cf1SEric Dumazet 335521f1cf1SEric Dumazet rcu_read_lock(); 336521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 337521f1cf1SEric Dumazet if (ca_ops) 338521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 339521f1cf1SEric Dumazet rcu_read_unlock(); 340521f1cf1SEric Dumazet if (err < 0) 3416e277ed5SThomas Graf goto errout; 342521f1cf1SEric Dumazet } 3436e277ed5SThomas Graf 344a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 345a8c2190eSArnaldo Carvalho de Melo 346b37e8840SIvan Delalande if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux) 347b37e8840SIvan Delalande if (handler->idiag_get_aux(sk, net_admin, skb) < 0) 348b37e8840SIvan Delalande goto errout; 349b37e8840SIvan Delalande 350521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) { 35164f40ff5SEric Dumazet union tcp_cc_info info; 35264f40ff5SEric Dumazet size_t sz = 0; 35364f40ff5SEric Dumazet int attr; 354521f1cf1SEric Dumazet 355521f1cf1SEric Dumazet rcu_read_lock(); 356521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 357521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info) 35864f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info); 359521f1cf1SEric Dumazet rcu_read_unlock(); 36064f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0) 361521f1cf1SEric Dumazet goto errout; 362521f1cf1SEric Dumazet } 363a8c2190eSArnaldo Carvalho de Melo 364085c20caSMartin KaFai Lau /* Keep it at the end for potential retry with a larger skb, 365085c20caSMartin KaFai Lau * or else do best-effort fitting, which is only done for the 366085c20caSMartin KaFai Lau * first_nlmsg. 367085c20caSMartin KaFai Lau */ 368085c20caSMartin KaFai Lau if (cb_data->bpf_stg_diag) { 369085c20caSMartin KaFai Lau bool first_nlmsg = ((unsigned char *)nlh == skb->data); 370085c20caSMartin KaFai Lau unsigned int prev_min_dump_alloc; 371085c20caSMartin KaFai Lau unsigned int total_nla_size = 0; 372085c20caSMartin KaFai Lau unsigned int msg_len; 373085c20caSMartin KaFai Lau int err; 374085c20caSMartin KaFai Lau 375085c20caSMartin KaFai Lau msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 376085c20caSMartin KaFai Lau err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb, 377085c20caSMartin KaFai Lau INET_DIAG_SK_BPF_STORAGES, 378085c20caSMartin KaFai Lau &total_nla_size); 379085c20caSMartin KaFai Lau 380085c20caSMartin KaFai Lau if (!err) 381085c20caSMartin KaFai Lau goto out; 382085c20caSMartin KaFai Lau 383085c20caSMartin KaFai Lau total_nla_size += msg_len; 384085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc; 385085c20caSMartin KaFai Lau if (total_nla_size > prev_min_dump_alloc) 386085c20caSMartin KaFai Lau cb->min_dump_alloc = min_t(u32, total_nla_size, 387085c20caSMartin KaFai Lau MAX_DUMP_ALLOC_SIZE); 388085c20caSMartin KaFai Lau 389085c20caSMartin KaFai Lau if (!first_nlmsg) 390085c20caSMartin KaFai Lau goto errout; 391085c20caSMartin KaFai Lau 392085c20caSMartin KaFai Lau if (cb->min_dump_alloc > prev_min_dump_alloc) 393085c20caSMartin KaFai Lau /* Retry with pskb_expand_head() with 394085c20caSMartin KaFai Lau * __GFP_DIRECT_RECLAIM 395085c20caSMartin KaFai Lau */ 396085c20caSMartin KaFai Lau goto errout; 397085c20caSMartin KaFai Lau 398085c20caSMartin KaFai Lau WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc); 399085c20caSMartin KaFai Lau 400085c20caSMartin KaFai Lau /* Send what we have for this sk 401085c20caSMartin KaFai Lau * and move on to the next sk in the following 402085c20caSMartin KaFai Lau * dump() 403085c20caSMartin KaFai Lau */ 404085c20caSMartin KaFai Lau } 405085c20caSMartin KaFai Lau 4063c4d05c8SPavel Emelyanov out: 407053c095aSJohannes Berg nlmsg_end(skb, nlh); 408053c095aSJohannes Berg return 0; 409a8c2190eSArnaldo Carvalho de Melo 4106e277ed5SThomas Graf errout: 4116e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 41226932566SPatrick McHardy return -EMSGSIZE; 413a8c2190eSArnaldo Carvalho de Melo } 4143c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 4153c4d05c8SPavel Emelyanov 41633cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk, 417e31c5e0eSEric Dumazet struct sk_buff *skb, 4185682d393SMartin KaFai Lau struct netlink_callback *cb, 4195682d393SMartin KaFai Lau u16 nlmsg_flags) 420c7d58aabSArnaldo Carvalho de Melo { 42133cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk); 422c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 4236e277ed5SThomas Graf struct nlmsghdr *nlh; 424789f558cSEric Dumazet long tmo; 425c7d58aabSArnaldo Carvalho de Melo 4265682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 4275682d393SMartin KaFai Lau cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, 4285682d393SMartin KaFai Lau sizeof(*r), nlmsg_flags); 4296e277ed5SThomas Graf if (!nlh) 430d106352dSDavid S. Miller return -EMSGSIZE; 431d106352dSDavid S. Miller 432d106352dSDavid S. Miller r = nlmsg_data(nlh); 433c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 434c7d58aabSArnaldo Carvalho de Melo 435a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 436c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 437b1aac815SDaniel Borkmann 438c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 439c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 4403828a93fSEric Dumazet tmo = tw->tw_timer.expires - jiffies; 4413828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo); 442c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 443c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 444c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 445c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 4466e277ed5SThomas Graf 447053c095aSJohannes Berg nlmsg_end(skb, nlh); 448053c095aSJohannes Berg return 0; 449c7d58aabSArnaldo Carvalho de Melo } 450c7d58aabSArnaldo Carvalho de Melo 451a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 4525682d393SMartin KaFai Lau struct netlink_callback *cb, 4535682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin) 454a58917f5SEric Dumazet { 455d545cacaSLorenzo Colitti struct request_sock *reqsk = inet_reqsk(sk); 456a58917f5SEric Dumazet struct inet_diag_msg *r; 457a58917f5SEric Dumazet struct nlmsghdr *nlh; 458a58917f5SEric Dumazet long tmo; 459a58917f5SEric Dumazet 4605682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 4615682d393SMartin KaFai Lau cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags); 462a58917f5SEric Dumazet if (!nlh) 463a58917f5SEric Dumazet return -EMSGSIZE; 464a58917f5SEric Dumazet 465a58917f5SEric Dumazet r = nlmsg_data(nlh); 466a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk); 467a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV; 468a58917f5SEric Dumazet r->idiag_timer = 1; 469d545cacaSLorenzo Colitti r->idiag_retrans = reqsk->num_retrans; 470a58917f5SEric Dumazet 471a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 472a58917f5SEric Dumazet offsetof(struct sock, sk_cookie)); 473a58917f5SEric Dumazet 474fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 4753828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo); 476a58917f5SEric Dumazet r->idiag_rqueue = 0; 477a58917f5SEric Dumazet r->idiag_wqueue = 0; 478a58917f5SEric Dumazet r->idiag_uid = 0; 479a58917f5SEric Dumazet r->idiag_inode = 0; 480a58917f5SEric Dumazet 481d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 482*e33de7c5SWang Hai inet_rsk(reqsk)->ir_mark)) { 483*e33de7c5SWang Hai nlmsg_cancel(skb, nlh); 484d545cacaSLorenzo Colitti return -EMSGSIZE; 485*e33de7c5SWang Hai } 486d545cacaSLorenzo Colitti 487a58917f5SEric Dumazet nlmsg_end(skb, nlh); 488a58917f5SEric Dumazet return 0; 489a58917f5SEric Dumazet } 490a58917f5SEric Dumazet 491dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 4925682d393SMartin KaFai Lau struct netlink_callback *cb, 49334160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 4945682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin) 495dff2c035SArnaldo Carvalho de Melo { 496dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 4975682d393SMartin KaFai Lau return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags); 498a58917f5SEric Dumazet 499a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) 5005682d393SMartin KaFai Lau return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin); 501efe4208fSEric Dumazet 5025682d393SMartin KaFai Lau return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags, 5035682d393SMartin KaFai Lau net_admin); 504dff2c035SArnaldo Carvalho de Melo } 505dff2c035SArnaldo Carvalho de Melo 506b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net, 507b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo, 50834160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 509a8c2190eSArnaldo Carvalho de Melo { 510e31c5e0eSEric Dumazet struct sock *sk; 511a8c2190eSArnaldo Carvalho de Melo 5122d331915SEric Dumazet rcu_read_lock(); 513e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 514a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 515a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 516a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 517dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5187c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) { 5197c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 5207c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 521a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 5227c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3], 5237c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if); 5247c130672SEric Dumazet else 525a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0, 526a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 527a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 528a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 529a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 530a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 5317c130672SEric Dumazet } 532a8c2190eSArnaldo Carvalho de Melo #endif 5332d331915SEric Dumazet else { 5342d331915SEric Dumazet rcu_read_unlock(); 535b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL); 5362d331915SEric Dumazet } 5372d331915SEric Dumazet rcu_read_unlock(); 538e31c5e0eSEric Dumazet if (!sk) 539b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 540a8c2190eSArnaldo Carvalho de Melo 541b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 542b613f56eSLorenzo Colitti sock_gen_put(sk); 543b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 544b613f56eSLorenzo Colitti } 545b613f56eSLorenzo Colitti 546b613f56eSLorenzo Colitti return sk; 547b613f56eSLorenzo Colitti } 548b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 549b613f56eSLorenzo Colitti 550b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 5515682d393SMartin KaFai Lau struct netlink_callback *cb, 552b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req) 553b613f56eSLorenzo Colitti { 5545682d393SMartin KaFai Lau struct sk_buff *in_skb = cb->skb; 555b37e8840SIvan Delalande bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN); 556b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk); 557b613f56eSLorenzo Colitti struct sk_buff *rep; 558b613f56eSLorenzo Colitti struct sock *sk; 559b613f56eSLorenzo Colitti int err; 560b613f56eSLorenzo Colitti 561b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req); 562b613f56eSLorenzo Colitti if (IS_ERR(sk)) 563b613f56eSLorenzo Colitti return PTR_ERR(sk); 564a8c2190eSArnaldo Carvalho de Melo 565b37e8840SIvan Delalande rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL); 5666e277ed5SThomas Graf if (!rep) { 5676e277ed5SThomas Graf err = -ENOMEM; 568a8c2190eSArnaldo Carvalho de Melo goto out; 5696e277ed5SThomas Graf } 570a8c2190eSArnaldo Carvalho de Melo 5715682d393SMartin KaFai Lau err = sk_diag_fill(sk, rep, cb, req, 0, net_admin); 57226932566SPatrick McHardy if (err < 0) { 57326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 5746e277ed5SThomas Graf nlmsg_free(rep); 57526932566SPatrick McHardy goto out; 57626932566SPatrick McHardy } 57715e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 578a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 579a8c2190eSArnaldo Carvalho de Melo if (err > 0) 580a8c2190eSArnaldo Carvalho de Melo err = 0; 581a8c2190eSArnaldo Carvalho de Melo 582a8c2190eSArnaldo Carvalho de Melo out: 583c1d607ccSEric Dumazet if (sk) 584c1d607ccSEric Dumazet sock_gen_put(sk); 585c1d607ccSEric Dumazet 586476f7dbfSPavel Emelyanov return err; 587476f7dbfSPavel Emelyanov } 5881942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 589476f7dbfSPavel Emelyanov 5906eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 591476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 5923f935c75SPaolo Abeni int hdrlen, 59334160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 594476f7dbfSPavel Emelyanov { 595476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 5963f935c75SPaolo Abeni struct inet_diag_dump_data dump_data; 5973f935c75SPaolo Abeni int err, protocol; 598476f7dbfSPavel Emelyanov 5993f935c75SPaolo Abeni memset(&dump_data, 0, sizeof(dump_data)); 600d5e4d0a5SEric Dumazet err = inet_diag_parse_attrs(nlh, hdrlen, dump_data.req_nlas); 601d5e4d0a5SEric Dumazet if (err) 602d5e4d0a5SEric Dumazet return err; 603d5e4d0a5SEric Dumazet 6043f935c75SPaolo Abeni protocol = inet_diag_get_protocol(req, &dump_data); 6053f935c75SPaolo Abeni 6063f935c75SPaolo Abeni handler = inet_diag_lock_handler(protocol); 6075682d393SMartin KaFai Lau if (IS_ERR(handler)) { 608476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 6095682d393SMartin KaFai Lau } else if (cmd == SOCK_DIAG_BY_FAMILY) { 6105682d393SMartin KaFai Lau struct netlink_callback cb = { 6115682d393SMartin KaFai Lau .nlh = nlh, 6125682d393SMartin KaFai Lau .skb = in_skb, 6133f935c75SPaolo Abeni .data = &dump_data, 6145682d393SMartin KaFai Lau }; 6155682d393SMartin KaFai Lau err = handler->dump_one(&cb, req); 6165682d393SMartin KaFai Lau } else if (cmd == SOCK_DESTROY && handler->destroy) { 6176eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req); 6185682d393SMartin KaFai Lau } else { 6196eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP; 6205682d393SMartin KaFai Lau } 621d523a328SHerbert Xu inet_diag_unlock_handler(handler); 622476f7dbfSPavel Emelyanov 623a8c2190eSArnaldo Carvalho de Melo return err; 624a8c2190eSArnaldo Carvalho de Melo } 625a8c2190eSArnaldo Carvalho de Melo 6269f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 627a8c2190eSArnaldo Carvalho de Melo { 628a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 629a8c2190eSArnaldo Carvalho de Melo 630a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 631a8c2190eSArnaldo Carvalho de Melo 632a8c2190eSArnaldo Carvalho de Melo if (words) { 633a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 634a8c2190eSArnaldo Carvalho de Melo return 0; 635a8c2190eSArnaldo Carvalho de Melo } 636a8c2190eSArnaldo Carvalho de Melo if (bits) { 6379f855299SAl Viro __be32 w1, w2; 6389f855299SAl Viro __be32 mask; 639a8c2190eSArnaldo Carvalho de Melo 640a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 641a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 642a8c2190eSArnaldo Carvalho de Melo 643a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 644a8c2190eSArnaldo Carvalho de Melo 645a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 646a8c2190eSArnaldo Carvalho de Melo return 0; 647a8c2190eSArnaldo Carvalho de Melo } 648a8c2190eSArnaldo Carvalho de Melo 649a8c2190eSArnaldo Carvalho de Melo return 1; 650a8c2190eSArnaldo Carvalho de Melo } 651a8c2190eSArnaldo Carvalho de Melo 65287c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 653a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 654a8c2190eSArnaldo Carvalho de Melo { 65587c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 65687c22ea5SPavel Emelyanov int len = nla_len(_bc); 65787c22ea5SPavel Emelyanov 658a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 659a8c2190eSArnaldo Carvalho de Melo int yes = 1; 660a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 661a8c2190eSArnaldo Carvalho de Melo 662a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 663a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 664a8c2190eSArnaldo Carvalho de Melo break; 665a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 666a8c2190eSArnaldo Carvalho de Melo yes = 0; 667a8c2190eSArnaldo Carvalho de Melo break; 668bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 669bbb6189dSKristian Evensen yes = entry->sport == op[1].no; 670bbb6189dSKristian Evensen break; 671a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 672a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 673a8c2190eSArnaldo Carvalho de Melo break; 674a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 675b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 676a8c2190eSArnaldo Carvalho de Melo break; 677bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 678bbb6189dSKristian Evensen yes = entry->dport == op[1].no; 679bbb6189dSKristian Evensen break; 680a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 681a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 682a8c2190eSArnaldo Carvalho de Melo break; 683a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 684a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 685a8c2190eSArnaldo Carvalho de Melo break; 686a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 687a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 688a8c2190eSArnaldo Carvalho de Melo break; 689a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 690a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 691e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 692e31c5e0eSEric Dumazet const __be32 *addr; 693a8c2190eSArnaldo Carvalho de Melo 694e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 695a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 696a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 697a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 698a8c2190eSArnaldo Carvalho de Melo yes = 0; 699a8c2190eSArnaldo Carvalho de Melo break; 700a8c2190eSArnaldo Carvalho de Melo } 701a8c2190eSArnaldo Carvalho de Melo 702a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 703a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 704a8c2190eSArnaldo Carvalho de Melo else 705a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 706a8c2190eSArnaldo Carvalho de Melo 707f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 708f67caec9SNeal Cardwell cond->family != entry->family) { 709a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 710a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 711a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 712a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 713f67caec9SNeal Cardwell bitstring_match(addr + 3, 714f67caec9SNeal Cardwell cond->addr, 715a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 716a8c2190eSArnaldo Carvalho de Melo break; 717a8c2190eSArnaldo Carvalho de Melo } 718a8c2190eSArnaldo Carvalho de Melo yes = 0; 719a8c2190eSArnaldo Carvalho de Melo break; 720a8c2190eSArnaldo Carvalho de Melo } 721f67caec9SNeal Cardwell 722f67caec9SNeal Cardwell if (cond->prefix_len == 0) 723f67caec9SNeal Cardwell break; 724f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 725f67caec9SNeal Cardwell cond->prefix_len)) 726f67caec9SNeal Cardwell break; 727f67caec9SNeal Cardwell yes = 0; 728f67caec9SNeal Cardwell break; 729f67caec9SNeal Cardwell } 730637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: { 731637c841dSDavid Ahern u32 ifindex; 732637c841dSDavid Ahern 733637c841dSDavid Ahern ifindex = *((const u32 *)(op + 1)); 734637c841dSDavid Ahern if (ifindex != entry->ifindex) 735637c841dSDavid Ahern yes = 0; 736637c841dSDavid Ahern break; 737637c841dSDavid Ahern } 738a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: { 739a52e95abSLorenzo Colitti struct inet_diag_markcond *cond; 740a52e95abSLorenzo Colitti 741a52e95abSLorenzo Colitti cond = (struct inet_diag_markcond *)(op + 1); 742a52e95abSLorenzo Colitti if ((entry->mark & cond->mask) != cond->mark) 743a52e95abSLorenzo Colitti yes = 0; 744a52e95abSLorenzo Colitti break; 745a52e95abSLorenzo Colitti } 746b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA 747b1f3e43dSDmitry Yakunin case INET_DIAG_BC_CGROUP_COND: { 748b1f3e43dSDmitry Yakunin u64 cgroup_id; 749b1f3e43dSDmitry Yakunin 750b1f3e43dSDmitry Yakunin cgroup_id = get_unaligned((const u64 *)(op + 1)); 751b1f3e43dSDmitry Yakunin if (cgroup_id != entry->cgroup_id) 752b1f3e43dSDmitry Yakunin yes = 0; 753b1f3e43dSDmitry Yakunin break; 754b1f3e43dSDmitry Yakunin } 755b1f3e43dSDmitry Yakunin #endif 756a8c2190eSArnaldo Carvalho de Melo } 757a8c2190eSArnaldo Carvalho de Melo 758a8c2190eSArnaldo Carvalho de Melo if (yes) { 759a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 760a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 761a8c2190eSArnaldo Carvalho de Melo } else { 762a8c2190eSArnaldo Carvalho de Melo len -= op->no; 763a8c2190eSArnaldo Carvalho de Melo bc += op->no; 764a8c2190eSArnaldo Carvalho de Melo } 765a8c2190eSArnaldo Carvalho de Melo } 766a02cec21SEric Dumazet return len == 0; 767a8c2190eSArnaldo Carvalho de Melo } 768a8c2190eSArnaldo Carvalho de Melo 769a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 770a4458343SEric Dumazet */ 771a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 772a4458343SEric Dumazet const struct sock *sk) 773a4458343SEric Dumazet { 774a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 775a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 776a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 777a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 778a4458343SEric Dumazet } else 779a4458343SEric Dumazet #endif 780a4458343SEric Dumazet { 781a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 782a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 783a4458343SEric Dumazet } 784a4458343SEric Dumazet } 785a4458343SEric Dumazet 7868d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 7878d07d151SPavel Emelyanov { 7888d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 789e31c5e0eSEric Dumazet struct inet_diag_entry entry; 7908d07d151SPavel Emelyanov 791e31c5e0eSEric Dumazet if (!bc) 7928d07d151SPavel Emelyanov return 1; 7938d07d151SPavel Emelyanov 7948d07d151SPavel Emelyanov entry.family = sk->sk_family; 795a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 7968d07d151SPavel Emelyanov entry.sport = inet->inet_num; 7978d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 798637c841dSDavid Ahern entry.ifindex = sk->sk_bound_dev_if; 799a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 800a52e95abSLorenzo Colitti if (sk_fullsock(sk)) 801a52e95abSLorenzo Colitti entry.mark = sk->sk_mark; 802a52e95abSLorenzo Colitti else if (sk->sk_state == TCP_NEW_SYN_RECV) 803a52e95abSLorenzo Colitti entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; 804a52e95abSLorenzo Colitti else 805a52e95abSLorenzo Colitti entry.mark = 0; 806b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA 807ee1bd483SDmitry Yakunin entry.cgroup_id = sk_fullsock(sk) ? 808ee1bd483SDmitry Yakunin cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)) : 0; 809b1f3e43dSDmitry Yakunin #endif 8108d07d151SPavel Emelyanov 8118d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 8128d07d151SPavel Emelyanov } 8138d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 8148d07d151SPavel Emelyanov 815a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 816a8c2190eSArnaldo Carvalho de Melo { 817a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 818a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 819a8c2190eSArnaldo Carvalho de Melo 820a8c2190eSArnaldo Carvalho de Melo if (cc > len) 821a8c2190eSArnaldo Carvalho de Melo return 0; 822a8c2190eSArnaldo Carvalho de Melo if (cc == len) 823a8c2190eSArnaldo Carvalho de Melo return 1; 824eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 825a8c2190eSArnaldo Carvalho de Melo return 0; 826a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 827a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 828a8c2190eSArnaldo Carvalho de Melo } 829a8c2190eSArnaldo Carvalho de Melo return 0; 830a8c2190eSArnaldo Carvalho de Melo } 831a8c2190eSArnaldo Carvalho de Melo 832637c841dSDavid Ahern /* data is u32 ifindex */ 833637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len, 834637c841dSDavid Ahern int *min_len) 835637c841dSDavid Ahern { 836637c841dSDavid Ahern /* Check ifindex space. */ 837637c841dSDavid Ahern *min_len += sizeof(u32); 838637c841dSDavid Ahern if (len < *min_len) 839637c841dSDavid Ahern return false; 840637c841dSDavid Ahern 841637c841dSDavid Ahern return true; 842637c841dSDavid Ahern } 843405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 844405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 845405c0059SNeal Cardwell int *min_len) 846405c0059SNeal Cardwell { 847405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 848e31c5e0eSEric Dumazet int addr_len; 849405c0059SNeal Cardwell 850405c0059SNeal Cardwell /* Check hostcond space. */ 851405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 852405c0059SNeal Cardwell if (len < *min_len) 853405c0059SNeal Cardwell return false; 854405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 855405c0059SNeal Cardwell 856405c0059SNeal Cardwell /* Check address family and address length. */ 857405c0059SNeal Cardwell switch (cond->family) { 858405c0059SNeal Cardwell case AF_UNSPEC: 859405c0059SNeal Cardwell addr_len = 0; 860405c0059SNeal Cardwell break; 861405c0059SNeal Cardwell case AF_INET: 862405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 863405c0059SNeal Cardwell break; 864405c0059SNeal Cardwell case AF_INET6: 865405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 866405c0059SNeal Cardwell break; 867405c0059SNeal Cardwell default: 868405c0059SNeal Cardwell return false; 869405c0059SNeal Cardwell } 870405c0059SNeal Cardwell *min_len += addr_len; 871405c0059SNeal Cardwell if (len < *min_len) 872405c0059SNeal Cardwell return false; 873405c0059SNeal Cardwell 874405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 875405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 876405c0059SNeal Cardwell return false; 877405c0059SNeal Cardwell 878405c0059SNeal Cardwell return true; 879405c0059SNeal Cardwell } 880405c0059SNeal Cardwell 8815e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 882e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 8835e1f5420SNeal Cardwell int len, int *min_len) 8845e1f5420SNeal Cardwell { 8855e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 8865e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 8875e1f5420SNeal Cardwell if (len < *min_len) 8885e1f5420SNeal Cardwell return false; 8895e1f5420SNeal Cardwell return true; 8905e1f5420SNeal Cardwell } 8915e1f5420SNeal Cardwell 892a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len, 893a52e95abSLorenzo Colitti int *min_len) 894a8c2190eSArnaldo Carvalho de Melo { 895a52e95abSLorenzo Colitti *min_len += sizeof(struct inet_diag_markcond); 896a52e95abSLorenzo Colitti return len >= *min_len; 897a52e95abSLorenzo Colitti } 898a52e95abSLorenzo Colitti 899b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA 900b1f3e43dSDmitry Yakunin static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len, 901b1f3e43dSDmitry Yakunin int *min_len) 902b1f3e43dSDmitry Yakunin { 903b1f3e43dSDmitry Yakunin *min_len += sizeof(u64); 904b1f3e43dSDmitry Yakunin return len >= *min_len; 905b1f3e43dSDmitry Yakunin } 906b1f3e43dSDmitry Yakunin #endif 907b1f3e43dSDmitry Yakunin 908a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr, 909a52e95abSLorenzo Colitti const struct sk_buff *skb) 910a52e95abSLorenzo Colitti { 911a52e95abSLorenzo Colitti bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); 912627cc4adSLorenzo Colitti const void *bytecode, *bc; 913627cc4adSLorenzo Colitti int bytecode_len, len; 914627cc4adSLorenzo Colitti 915627cc4adSLorenzo Colitti if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) 916627cc4adSLorenzo Colitti return -EINVAL; 917627cc4adSLorenzo Colitti 918627cc4adSLorenzo Colitti bytecode = bc = nla_data(attr); 919627cc4adSLorenzo Colitti len = bytecode_len = nla_len(attr); 920a8c2190eSArnaldo Carvalho de Melo 921a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 922405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 923e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 924a8c2190eSArnaldo Carvalho de Melo 925a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 926a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 927a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 928405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 929405c0059SNeal Cardwell return -EINVAL; 9305e1f5420SNeal Cardwell break; 931637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: 932637c841dSDavid Ahern if (!valid_devcond(bc, len, &min_len)) 933637c841dSDavid Ahern return -EINVAL; 934637c841dSDavid Ahern break; 935bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 936a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 937a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 938bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 939a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 940a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 9415e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 942a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 943a8c2190eSArnaldo Carvalho de Melo break; 944a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: 945a52e95abSLorenzo Colitti if (!net_admin) 946a52e95abSLorenzo Colitti return -EPERM; 947a52e95abSLorenzo Colitti if (!valid_markcond(bc, len, &min_len)) 948a52e95abSLorenzo Colitti return -EINVAL; 949a52e95abSLorenzo Colitti break; 950b1f3e43dSDmitry Yakunin #ifdef CONFIG_SOCK_CGROUP_DATA 951b1f3e43dSDmitry Yakunin case INET_DIAG_BC_CGROUP_COND: 952b1f3e43dSDmitry Yakunin if (!valid_cgroupcond(bc, len, &min_len)) 953b1f3e43dSDmitry Yakunin return -EINVAL; 954b1f3e43dSDmitry Yakunin break; 955b1f3e43dSDmitry Yakunin #endif 9565e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 9575e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 958a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 959a8c2190eSArnaldo Carvalho de Melo break; 960a8c2190eSArnaldo Carvalho de Melo default: 961a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 962a8c2190eSArnaldo Carvalho de Melo } 9635e1f5420SNeal Cardwell 9645e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 9655e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 9665e1f5420SNeal Cardwell return -EINVAL; 9675e1f5420SNeal Cardwell if (op->no < len && 9685e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 9695e1f5420SNeal Cardwell return -EINVAL; 9705e1f5420SNeal Cardwell } 9715e1f5420SNeal Cardwell 972405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 973eeb14972SEric Dumazet return -EINVAL; 974a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 975a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 976a8c2190eSArnaldo Carvalho de Melo } 977a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 978a8c2190eSArnaldo Carvalho de Melo } 979a8c2190eSArnaldo Carvalho de Melo 98049612729SEric Dumazet static void twsk_build_assert(void) 98149612729SEric Dumazet { 98249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 98349612729SEric Dumazet offsetof(struct sock, sk_family)); 98449612729SEric Dumazet 98549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 98649612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 98749612729SEric Dumazet 98849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 98949612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 99049612729SEric Dumazet 99149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 99249612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 99349612729SEric Dumazet 99449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 99549612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 99649612729SEric Dumazet 99749612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 99849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 99949612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 100049612729SEric Dumazet 100149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 100249612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 100349612729SEric Dumazet #endif 100449612729SEric Dumazet } 100549612729SEric Dumazet 10061942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 1007e31c5e0eSEric Dumazet struct netlink_callback *cb, 10080df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r) 1009a8c2190eSArnaldo Carvalho de Melo { 1010d545cacaSLorenzo Colitti bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); 10110df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data; 101267db3e4bSEric Dumazet struct net *net = sock_net(skb->sk); 101367db3e4bSEric Dumazet u32 idiag_states = r->idiag_states; 101467db3e4bSEric Dumazet int i, num, s_i, s_num; 10150df6d328SMartin KaFai Lau struct nlattr *bc; 101667db3e4bSEric Dumazet struct sock *sk; 1017a8c2190eSArnaldo Carvalho de Melo 10180df6d328SMartin KaFai Lau bc = cb_data->inet_diag_nla_bc; 1019079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 1020079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 1021a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 1022a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 1023a8c2190eSArnaldo Carvalho de Melo 1024a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 10259652dc2eSEric Dumazet if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) 1026a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 1027a8c2190eSArnaldo Carvalho de Melo 1028a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 10295caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 10308dbd76e7SEric Dumazet struct hlist_nulls_node *node; 1031a8c2190eSArnaldo Carvalho de Melo 1032a8c2190eSArnaldo Carvalho de Melo num = 0; 10335caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 10349652dc2eSEric Dumazet spin_lock(&ilb->lock); 10358dbd76e7SEric Dumazet sk_nulls_for_each(sk, node, &ilb->nulls_head) { 1036a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 1037a8c2190eSArnaldo Carvalho de Melo 103851d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 103951d7cccfSAndrey Vagin continue; 104051d7cccfSAndrey Vagin 1041a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 1042a8c2190eSArnaldo Carvalho de Melo num++; 1043a8c2190eSArnaldo Carvalho de Melo continue; 1044a8c2190eSArnaldo Carvalho de Melo } 1045a8c2190eSArnaldo Carvalho de Melo 1046d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 1047d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 1048d23deaa0SPavel Emelyanov goto next_listen; 1049d23deaa0SPavel Emelyanov 1050c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 1051a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 1052a8c2190eSArnaldo Carvalho de Melo goto next_listen; 1053a8c2190eSArnaldo Carvalho de Melo 10545682d393SMartin KaFai Lau if (!inet_diag_bc_sk(bc, sk)) 10555682d393SMartin KaFai Lau goto next_listen; 10565682d393SMartin KaFai Lau 10575682d393SMartin KaFai Lau if (inet_sk_diag_fill(sk, inet_csk(sk), skb, 10585682d393SMartin KaFai Lau cb, r, NLM_F_MULTI, 10595682d393SMartin KaFai Lau net_admin) < 0) { 10609652dc2eSEric Dumazet spin_unlock(&ilb->lock); 1061a8c2190eSArnaldo Carvalho de Melo goto done; 1062a8c2190eSArnaldo Carvalho de Melo } 1063a8c2190eSArnaldo Carvalho de Melo 1064a8c2190eSArnaldo Carvalho de Melo next_listen: 1065a8c2190eSArnaldo Carvalho de Melo ++num; 1066a8c2190eSArnaldo Carvalho de Melo } 10679652dc2eSEric Dumazet spin_unlock(&ilb->lock); 1068a8c2190eSArnaldo Carvalho de Melo 1069a8c2190eSArnaldo Carvalho de Melo s_num = 0; 1070a8c2190eSArnaldo Carvalho de Melo } 1071a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 1072a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 1073a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 1074a8c2190eSArnaldo Carvalho de Melo } 1075a8c2190eSArnaldo Carvalho de Melo 1076079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 1077efb3cb42SPavel Emelyanov goto out; 1078a8c2190eSArnaldo Carvalho de Melo 107967db3e4bSEric Dumazet #define SKARR_SZ 16 1080f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 1081a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 10827e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 10833ab5aee7SEric Dumazet struct hlist_nulls_node *node; 108467db3e4bSEric Dumazet struct sock *sk_arr[SKARR_SZ]; 108567db3e4bSEric Dumazet int num_arr[SKARR_SZ]; 108667db3e4bSEric Dumazet int idx, accum, res; 10876be547a6SAndi Kleen 108805dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 10896be547a6SAndi Kleen continue; 10906be547a6SAndi Kleen 1091a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 1092a8c2190eSArnaldo Carvalho de Melo s_num = 0; 1093a8c2190eSArnaldo Carvalho de Melo 109467db3e4bSEric Dumazet next_chunk: 109567db3e4bSEric Dumazet num = 0; 109667db3e4bSEric Dumazet accum = 0; 10977e3aab4aSDavid S. Miller spin_lock_bh(lock); 10983ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 109967db3e4bSEric Dumazet int state; 1100a8c2190eSArnaldo Carvalho de Melo 110151d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 110251d7cccfSAndrey Vagin continue; 1103a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 1104a8c2190eSArnaldo Carvalho de Melo goto next_normal; 110570315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 110670315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 1107079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 1108a8c2190eSArnaldo Carvalho de Melo goto next_normal; 1109d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 1110d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 1111d23deaa0SPavel Emelyanov goto next_normal; 111205dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 1113a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 1114a8c2190eSArnaldo Carvalho de Melo goto next_normal; 111505dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 11164e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 1117a8c2190eSArnaldo Carvalho de Melo goto next_normal; 1118a58917f5SEric Dumazet twsk_build_assert(); 1119a58917f5SEric Dumazet 1120a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 1121a58917f5SEric Dumazet goto next_normal; 1122a58917f5SEric Dumazet 1123f0c928d8SEric Dumazet if (!refcount_inc_not_zero(&sk->sk_refcnt)) 1124f0c928d8SEric Dumazet goto next_normal; 1125f0c928d8SEric Dumazet 112667db3e4bSEric Dumazet num_arr[accum] = num; 112767db3e4bSEric Dumazet sk_arr[accum] = sk; 112867db3e4bSEric Dumazet if (++accum == SKARR_SZ) 112967db3e4bSEric Dumazet break; 113067db3e4bSEric Dumazet next_normal: 113167db3e4bSEric Dumazet ++num; 113267db3e4bSEric Dumazet } 113367db3e4bSEric Dumazet spin_unlock_bh(lock); 113467db3e4bSEric Dumazet res = 0; 113567db3e4bSEric Dumazet for (idx = 0; idx < accum; idx++) { 113667db3e4bSEric Dumazet if (res >= 0) { 11375682d393SMartin KaFai Lau res = sk_diag_fill(sk_arr[idx], skb, cb, r, 11385682d393SMartin KaFai Lau NLM_F_MULTI, net_admin); 113967db3e4bSEric Dumazet if (res < 0) 114067db3e4bSEric Dumazet num = num_arr[idx]; 1141a8c2190eSArnaldo Carvalho de Melo } 114267db3e4bSEric Dumazet sock_gen_put(sk_arr[idx]); 1143a8c2190eSArnaldo Carvalho de Melo } 114467db3e4bSEric Dumazet if (res < 0) 114567db3e4bSEric Dumazet break; 1146acffb584SEric Dumazet cond_resched(); 114767db3e4bSEric Dumazet if (accum == SKARR_SZ) { 114867db3e4bSEric Dumazet s_num = num + 1; 114967db3e4bSEric Dumazet goto next_chunk; 115067db3e4bSEric Dumazet } 1151a8c2190eSArnaldo Carvalho de Melo } 1152a8c2190eSArnaldo Carvalho de Melo 1153a8c2190eSArnaldo Carvalho de Melo done: 1154a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 1155a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 1156efb3cb42SPavel Emelyanov out: 1157efb3cb42SPavel Emelyanov ; 1158efb3cb42SPavel Emelyanov } 11591942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 1160efb3cb42SPavel Emelyanov 1161efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 11620df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r) 1163efb3cb42SPavel Emelyanov { 11643f935c75SPaolo Abeni struct inet_diag_dump_data *cb_data = cb->data; 1165efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 1166085c20caSMartin KaFai Lau u32 prev_min_dump_alloc; 11673f935c75SPaolo Abeni int protocol, err = 0; 11683f935c75SPaolo Abeni 11693f935c75SPaolo Abeni protocol = inet_diag_get_protocol(r, cb_data); 1170efb3cb42SPavel Emelyanov 1171085c20caSMartin KaFai Lau again: 1172085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc; 11733f935c75SPaolo Abeni handler = inet_diag_lock_handler(protocol); 1174efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 11750df6d328SMartin KaFai Lau handler->dump(skb, cb, r); 1176cacb6ba0SCyrill Gorcunov else 1177cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 1178d523a328SHerbert Xu inet_diag_unlock_handler(handler); 1179efb3cb42SPavel Emelyanov 1180085c20caSMartin KaFai Lau /* The skb is not large enough to fit one sk info and 1181085c20caSMartin KaFai Lau * inet_sk_diag_fill() has requested for a larger skb. 1182085c20caSMartin KaFai Lau */ 1183085c20caSMartin KaFai Lau if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) { 1184085c20caSMartin KaFai Lau err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL); 1185085c20caSMartin KaFai Lau if (!err) 1186085c20caSMartin KaFai Lau goto again; 1187085c20caSMartin KaFai Lau } 1188085c20caSMartin KaFai Lau 1189cacb6ba0SCyrill Gorcunov return err ? : skb->len; 1190a8c2190eSArnaldo Carvalho de Melo } 1191a8c2190eSArnaldo Carvalho de Melo 119225c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 119325c4cd2bSPavel Emelyanov { 11940df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh)); 11950df6d328SMartin KaFai Lau } 119625c4cd2bSPavel Emelyanov 11970df6d328SMartin KaFai Lau static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen) 11980df6d328SMartin KaFai Lau { 11990df6d328SMartin KaFai Lau const struct nlmsghdr *nlh = cb->nlh; 12000df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data; 12010df6d328SMartin KaFai Lau struct sk_buff *skb = cb->skb; 12020df6d328SMartin KaFai Lau struct nlattr *nla; 12033f935c75SPaolo Abeni int err; 120425c4cd2bSPavel Emelyanov 12050df6d328SMartin KaFai Lau cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL); 12060df6d328SMartin KaFai Lau if (!cb_data) 12070df6d328SMartin KaFai Lau return -ENOMEM; 12080df6d328SMartin KaFai Lau 1209d5e4d0a5SEric Dumazet err = inet_diag_parse_attrs(nlh, hdrlen, cb_data->req_nlas); 1210d5e4d0a5SEric Dumazet if (err) { 1211d5e4d0a5SEric Dumazet kfree(cb_data); 1212d5e4d0a5SEric Dumazet return err; 1213d5e4d0a5SEric Dumazet } 12140df6d328SMartin KaFai Lau nla = cb_data->inet_diag_nla_bc; 12150df6d328SMartin KaFai Lau if (nla) { 12160df6d328SMartin KaFai Lau err = inet_diag_bc_audit(nla, skb); 12170df6d328SMartin KaFai Lau if (err) { 12180df6d328SMartin KaFai Lau kfree(cb_data); 12190df6d328SMartin KaFai Lau return err; 12200df6d328SMartin KaFai Lau } 12210df6d328SMartin KaFai Lau } 12220df6d328SMartin KaFai Lau 1223085c20caSMartin KaFai Lau nla = cb_data->inet_diag_nla_bpf_stgs; 1224085c20caSMartin KaFai Lau if (nla) { 1225085c20caSMartin KaFai Lau struct bpf_sk_storage_diag *bpf_stg_diag; 1226085c20caSMartin KaFai Lau 1227085c20caSMartin KaFai Lau bpf_stg_diag = bpf_sk_storage_diag_alloc(nla); 1228085c20caSMartin KaFai Lau if (IS_ERR(bpf_stg_diag)) { 1229085c20caSMartin KaFai Lau kfree(cb_data); 1230085c20caSMartin KaFai Lau return PTR_ERR(bpf_stg_diag); 1231085c20caSMartin KaFai Lau } 1232085c20caSMartin KaFai Lau cb_data->bpf_stg_diag = bpf_stg_diag; 1233085c20caSMartin KaFai Lau } 1234085c20caSMartin KaFai Lau 12350df6d328SMartin KaFai Lau cb->data = cb_data; 12360df6d328SMartin KaFai Lau return 0; 12370df6d328SMartin KaFai Lau } 12380df6d328SMartin KaFai Lau 12390df6d328SMartin KaFai Lau static int inet_diag_dump_start(struct netlink_callback *cb) 12400df6d328SMartin KaFai Lau { 12410df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2)); 12420df6d328SMartin KaFai Lau } 12430df6d328SMartin KaFai Lau 12440df6d328SMartin KaFai Lau static int inet_diag_dump_start_compat(struct netlink_callback *cb) 12450df6d328SMartin KaFai Lau { 12460df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req)); 12470df6d328SMartin KaFai Lau } 12480df6d328SMartin KaFai Lau 12490df6d328SMartin KaFai Lau static int inet_diag_dump_done(struct netlink_callback *cb) 12500df6d328SMartin KaFai Lau { 1251085c20caSMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data; 1252085c20caSMartin KaFai Lau 1253085c20caSMartin KaFai Lau bpf_sk_storage_diag_free(cb_data->bpf_stg_diag); 12540df6d328SMartin KaFai Lau kfree(cb->data); 12550df6d328SMartin KaFai Lau 12560df6d328SMartin KaFai Lau return 0; 125725c4cd2bSPavel Emelyanov } 125825c4cd2bSPavel Emelyanov 1259e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 1260a029fe26SPavel Emelyanov { 1261a029fe26SPavel Emelyanov switch (type) { 1262a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 1263a029fe26SPavel Emelyanov return IPPROTO_TCP; 1264a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 1265a029fe26SPavel Emelyanov return IPPROTO_DCCP; 1266a029fe26SPavel Emelyanov default: 1267a029fe26SPavel Emelyanov return 0; 1268a029fe26SPavel Emelyanov } 1269a029fe26SPavel Emelyanov } 1270a029fe26SPavel Emelyanov 1271e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 1272e31c5e0eSEric Dumazet struct netlink_callback *cb) 127325c4cd2bSPavel Emelyanov { 1274d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1275c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 127625c4cd2bSPavel Emelyanov 1277d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 127825c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 127925c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 128025c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 128125c4cd2bSPavel Emelyanov req.id = rc->id; 128225c4cd2bSPavel Emelyanov 12830df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, &req); 128425c4cd2bSPavel Emelyanov } 128525c4cd2bSPavel Emelyanov 1286fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1287fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1288fe50ce28SPavel Emelyanov { 1289d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1290c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1291fe50ce28SPavel Emelyanov 1292fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1293fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1294fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1295fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1296fe50ce28SPavel Emelyanov req.id = rc->id; 1297fe50ce28SPavel Emelyanov 12983f935c75SPaolo Abeni return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, 12993f935c75SPaolo Abeni sizeof(struct inet_diag_req), &req); 1300fe50ce28SPavel Emelyanov } 1301fe50ce28SPavel Emelyanov 13028d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1303a8c2190eSArnaldo Carvalho de Melo { 13043b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 130551d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1306a8c2190eSArnaldo Carvalho de Melo 1307ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1308ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1309ead592baSThomas Graf return -EINVAL; 1310a8c2190eSArnaldo Carvalho de Melo 1311b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 131280d326faSPablo Neira Ayuso struct netlink_dump_control c = { 13130df6d328SMartin KaFai Lau .start = inet_diag_dump_start_compat, 13140df6d328SMartin KaFai Lau .done = inet_diag_dump_done, 131580d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 131680d326faSPablo Neira Ayuso }; 131751d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 131880d326faSPablo Neira Ayuso } 1319ead592baSThomas Graf 1320fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1321a8c2190eSArnaldo Carvalho de Melo } 1322a8c2190eSArnaldo Carvalho de Melo 13236eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 1324d366477aSPavel Emelyanov { 1325c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 132651d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1327d366477aSPavel Emelyanov 1328d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1329d366477aSPavel Emelyanov return -EINVAL; 1330d366477aSPavel Emelyanov 13316eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 13326eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) { 133380d326faSPablo Neira Ayuso struct netlink_dump_control c = { 13340df6d328SMartin KaFai Lau .start = inet_diag_dump_start, 13350df6d328SMartin KaFai Lau .done = inet_diag_dump_done, 133680d326faSPablo Neira Ayuso .dump = inet_diag_dump, 133780d326faSPablo Neira Ayuso }; 133851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 133980d326faSPablo Neira Ayuso } 1340d366477aSPavel Emelyanov 13413f935c75SPaolo Abeni return inet_diag_cmd_exact(h->nlmsg_type, skb, h, hdrlen, 13423f935c75SPaolo Abeni nlmsg_data(h)); 1343d366477aSPavel Emelyanov } 1344d366477aSPavel Emelyanov 134535ac838aSCraig Gallek static 134635ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 134735ac838aSCraig Gallek { 134835ac838aSCraig Gallek const struct inet_diag_handler *handler; 134935ac838aSCraig Gallek struct nlmsghdr *nlh; 135035ac838aSCraig Gallek struct nlattr *attr; 135135ac838aSCraig Gallek struct inet_diag_msg *r; 135235ac838aSCraig Gallek void *info = NULL; 135335ac838aSCraig Gallek int err = 0; 135435ac838aSCraig Gallek 135535ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 135635ac838aSCraig Gallek if (!nlh) 135735ac838aSCraig Gallek return -ENOMEM; 135835ac838aSCraig Gallek 135935ac838aSCraig Gallek r = nlmsg_data(nlh); 136035ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 136135ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1362e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1363e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 136435ac838aSCraig Gallek r->idiag_state = sk->sk_state; 136535ac838aSCraig Gallek 136635ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 136735ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 136835ac838aSCraig Gallek return err; 136935ac838aSCraig Gallek } 137035ac838aSCraig Gallek 137135ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 137235ac838aSCraig Gallek if (IS_ERR(handler)) { 137335ac838aSCraig Gallek inet_diag_unlock_handler(handler); 137435ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 137535ac838aSCraig Gallek return PTR_ERR(handler); 137635ac838aSCraig Gallek } 137735ac838aSCraig Gallek 137835ac838aSCraig Gallek attr = handler->idiag_info_size 13796ed46d12SNicolas Dichtel ? nla_reserve_64bit(skb, INET_DIAG_INFO, 13806ed46d12SNicolas Dichtel handler->idiag_info_size, 13816ed46d12SNicolas Dichtel INET_DIAG_PAD) 138235ac838aSCraig Gallek : NULL; 138335ac838aSCraig Gallek if (attr) 138435ac838aSCraig Gallek info = nla_data(attr); 138535ac838aSCraig Gallek 138635ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 138735ac838aSCraig Gallek inet_diag_unlock_handler(handler); 138835ac838aSCraig Gallek 138935ac838aSCraig Gallek nlmsg_end(skb, nlh); 139035ac838aSCraig Gallek return 0; 139135ac838aSCraig Gallek } 139235ac838aSCraig Gallek 13938dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1394d366477aSPavel Emelyanov .family = AF_INET, 13956eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 139635ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 13976eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1398d366477aSPavel Emelyanov }; 1399d366477aSPavel Emelyanov 14008dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1401d366477aSPavel Emelyanov .family = AF_INET6, 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 1407a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1408a8c2190eSArnaldo Carvalho de Melo { 1409a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1410a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1411a8c2190eSArnaldo Carvalho de Melo 1412f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1413a8c2190eSArnaldo Carvalho de Melo goto out; 1414a8c2190eSArnaldo Carvalho de Melo 1415d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1416a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1417e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1418a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1419a8c2190eSArnaldo Carvalho de Melo err = 0; 1420a8c2190eSArnaldo Carvalho de Melo } 1421d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1422a8c2190eSArnaldo Carvalho de Melo out: 1423a8c2190eSArnaldo Carvalho de Melo return err; 1424a8c2190eSArnaldo Carvalho de Melo } 1425a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1426a8c2190eSArnaldo Carvalho de Melo 1427a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1428a8c2190eSArnaldo Carvalho de Melo { 1429a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1430a8c2190eSArnaldo Carvalho de Melo 1431f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1432a8c2190eSArnaldo Carvalho de Melo return; 1433a8c2190eSArnaldo Carvalho de Melo 1434d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1435a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1436d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1437a8c2190eSArnaldo Carvalho de Melo } 1438a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1439a8c2190eSArnaldo Carvalho de Melo 1440a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1441a8c2190eSArnaldo Carvalho de Melo { 1442f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1443a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1444a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1445a8c2190eSArnaldo Carvalho de Melo 14460da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1447a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1448a8c2190eSArnaldo Carvalho de Melo goto out; 1449a8c2190eSArnaldo Carvalho de Melo 1450d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1451d366477aSPavel Emelyanov if (err) 1452d366477aSPavel Emelyanov goto out_free_nl; 1453d366477aSPavel Emelyanov 1454d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1455d366477aSPavel Emelyanov if (err) 1456d366477aSPavel Emelyanov goto out_free_inet; 1457d366477aSPavel Emelyanov 14588ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1459a8c2190eSArnaldo Carvalho de Melo out: 1460a8c2190eSArnaldo Carvalho de Melo return err; 1461d366477aSPavel Emelyanov 1462d366477aSPavel Emelyanov out_free_inet: 1463d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1464d366477aSPavel Emelyanov out_free_nl: 1465a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1466a8c2190eSArnaldo Carvalho de Melo goto out; 1467a8c2190eSArnaldo Carvalho de Melo } 1468a8c2190eSArnaldo Carvalho de Melo 1469a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1470a8c2190eSArnaldo Carvalho de Melo { 1471d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1472d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 14738ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1474a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1475a8c2190eSArnaldo Carvalho de Melo } 1476a8c2190eSArnaldo Carvalho de Melo 1477a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1478a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1479a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1480aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1481aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1482