1a8c2190eSArnaldo Carvalho de Melo /* 2a8c2190eSArnaldo Carvalho de Melo * inet_diag.c Module for monitoring INET transport protocols sockets. 3a8c2190eSArnaldo Carvalho de Melo * 4a8c2190eSArnaldo Carvalho de Melo * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 5a8c2190eSArnaldo Carvalho de Melo * 6a8c2190eSArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 7a8c2190eSArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 8a8c2190eSArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 9a8c2190eSArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 10a8c2190eSArnaldo Carvalho de Melo */ 11a8c2190eSArnaldo Carvalho de Melo 12172589ccSIlpo Järvinen #include <linux/kernel.h> 13a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h> 14a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h> 15a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h> 16a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h> 19a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h> 20a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h> 21a8c2190eSArnaldo Carvalho de Melo 22a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h> 23a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h> 24a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h> 25a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h> 26a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 27a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 28a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h> 29a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h> 30dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 31a8c2190eSArnaldo Carvalho de Melo 32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h> 33a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h> 34a8c2190eSArnaldo Carvalho de Melo 35a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h> 36d366477aSPavel Emelyanov #include <linux/sock_diag.h> 37a8c2190eSArnaldo Carvalho de Melo 38a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table; 39a8c2190eSArnaldo Carvalho de Melo 40a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry { 41e31c5e0eSEric Dumazet const __be32 *saddr; 42e31c5e0eSEric Dumazet const __be32 *daddr; 43a8c2190eSArnaldo Carvalho de Melo u16 sport; 44a8c2190eSArnaldo Carvalho de Melo u16 dport; 45a8c2190eSArnaldo Carvalho de Melo u16 family; 46a8c2190eSArnaldo Carvalho de Melo u16 userlocks; 47a8c2190eSArnaldo Carvalho de Melo }; 48a8c2190eSArnaldo Carvalho de Melo 49d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 50d523a328SHerbert Xu 51f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 52d523a328SHerbert Xu { 53f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 54aec8dc62SPavel Emelyanov request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK, 55aec8dc62SPavel Emelyanov NETLINK_SOCK_DIAG, AF_INET, proto); 56d523a328SHerbert Xu 57d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 58f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 59d523a328SHerbert Xu return ERR_PTR(-ENOENT); 60d523a328SHerbert Xu 61f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 62d523a328SHerbert Xu } 63d523a328SHerbert Xu 64e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 65d523a328SHerbert Xu { 66d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 67d523a328SHerbert Xu } 68d523a328SHerbert Xu 69a4458343SEric Dumazet static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) 70a4458343SEric Dumazet { 71a4458343SEric Dumazet r->idiag_family = sk->sk_family; 72a4458343SEric Dumazet 73a4458343SEric Dumazet r->id.idiag_sport = htons(sk->sk_num); 74a4458343SEric Dumazet r->id.idiag_dport = sk->sk_dport; 75a4458343SEric Dumazet r->id.idiag_if = sk->sk_bound_dev_if; 76a4458343SEric Dumazet sock_diag_save_cookie(sk, r->id.idiag_cookie); 77a4458343SEric Dumazet 78a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 79a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 80a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 81a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 82a4458343SEric Dumazet } else 83a4458343SEric Dumazet #endif 84a4458343SEric Dumazet { 85a4458343SEric Dumazet memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 86a4458343SEric Dumazet memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 87a4458343SEric Dumazet 88a4458343SEric Dumazet r->id.idiag_src[0] = sk->sk_rcv_saddr; 89a4458343SEric Dumazet r->id.idiag_dst[0] = sk->sk_daddr; 90a4458343SEric Dumazet } 91a4458343SEric Dumazet } 92a4458343SEric Dumazet 93c8e2c80dSEric Dumazet static size_t inet_sk_attr_size(void) 94c8e2c80dSEric Dumazet { 95c8e2c80dSEric Dumazet return nla_total_size(sizeof(struct tcp_info)) 96c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_SHUTDOWN */ 97c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TOS */ 98c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TCLASS */ 99c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_meminfo)) 100c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_msg)) 101c8e2c80dSEric Dumazet + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) 102c8e2c80dSEric Dumazet + nla_total_size(TCP_CA_NAME_MAX) 103c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct tcpvegas_info)) 104c8e2c80dSEric Dumazet + 64; 105c8e2c80dSEric Dumazet } 106c8e2c80dSEric Dumazet 1073c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 10834160ea3SEric Dumazet struct sk_buff *skb, const struct inet_diag_req_v2 *req, 109d06ca956SEric W. Biederman struct user_namespace *user_ns, 11015e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 111a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 112a8c2190eSArnaldo Carvalho de Melo { 113a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 114521f1cf1SEric Dumazet const struct tcp_congestion_ops *ca_ops; 115e31c5e0eSEric Dumazet const struct inet_diag_handler *handler; 116e31c5e0eSEric Dumazet int ext = req->idiag_ext; 117a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 118a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 1196e277ed5SThomas Graf struct nlattr *attr; 120a8c2190eSArnaldo Carvalho de Melo void *info = NULL; 121a8c2190eSArnaldo Carvalho de Melo 122a029fe26SPavel Emelyanov handler = inet_diag_table[req->sdiag_protocol]; 123e31c5e0eSEric Dumazet BUG_ON(!handler); 124a8c2190eSArnaldo Carvalho de Melo 12515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 1266e277ed5SThomas Graf nlmsg_flags); 1276e277ed5SThomas Graf if (!nlh) 128d106352dSDavid S. Miller return -EMSGSIZE; 129a8c2190eSArnaldo Carvalho de Melo 130d106352dSDavid S. Miller r = nlmsg_data(nlh); 131a58917f5SEric Dumazet BUG_ON(!sk_fullsock(sk)); 132c7d58aabSArnaldo Carvalho de Melo 133a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 134a8c2190eSArnaldo Carvalho de Melo r->idiag_state = sk->sk_state; 135a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 136a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = 0; 137a8c2190eSArnaldo Carvalho de Melo 138e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 139e4e541a8SPavel Emelyanov goto errout; 140e4e541a8SPavel Emelyanov 141717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 142717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 143717b6d83SMaciej Żenczykowski */ 144717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 1456e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 1466e277ed5SThomas Graf goto errout; 147717b6d83SMaciej Żenczykowski 148dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 149a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 15006236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 151efe4208fSEric Dumazet if (nla_put_u8(skb, INET_DIAG_TCLASS, 152efe4208fSEric Dumazet inet6_sk(sk)->tclass) < 0) 1536e277ed5SThomas Graf goto errout; 15420462155SPhil Sutter 1558220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 1568220ea23SPhil Sutter nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) 15720462155SPhil Sutter goto errout; 158a8c2190eSArnaldo Carvalho de Melo } 159a8c2190eSArnaldo Carvalho de Melo #endif 160a8c2190eSArnaldo Carvalho de Melo 161d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 1623c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1633c4d05c8SPavel Emelyanov 1646e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 1656e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 1666e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 1676e277ed5SThomas Graf .idiag_wmem = sk->sk_wmem_queued, 1686e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 1696e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 1706e277ed5SThomas Graf }; 1716e277ed5SThomas Graf 1726e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 1736e277ed5SThomas Graf goto errout; 1743c4d05c8SPavel Emelyanov } 1753c4d05c8SPavel Emelyanov 176c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 177c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 1786e277ed5SThomas Graf goto errout; 179c0636faaSPavel Emelyanov 180e31c5e0eSEric Dumazet if (!icsk) { 18162ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 1823c4d05c8SPavel Emelyanov goto out; 1833c4d05c8SPavel Emelyanov } 1843c4d05c8SPavel Emelyanov 185172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 186a8c2190eSArnaldo Carvalho de Melo 1876ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 1886ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || 1896ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 190a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 191a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 192a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 193a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 194a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 195a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 196a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 197a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 198a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 199a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 200a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 201a8c2190eSArnaldo Carvalho de Melo } else { 202a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 203a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 204a8c2190eSArnaldo Carvalho de Melo } 205a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS 206a8c2190eSArnaldo Carvalho de Melo 2073fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 2086e277ed5SThomas Graf attr = nla_reserve(skb, INET_DIAG_INFO, 2093fd22af8SCraig Gallek handler->idiag_info_size); 2106e277ed5SThomas Graf if (!attr) 2116e277ed5SThomas Graf goto errout; 212a8c2190eSArnaldo Carvalho de Melo 2136e277ed5SThomas Graf info = nla_data(attr); 214a8c2190eSArnaldo Carvalho de Melo } 215a8c2190eSArnaldo Carvalho de Melo 216521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) { 217521f1cf1SEric Dumazet int err = 0; 218521f1cf1SEric Dumazet 219521f1cf1SEric Dumazet rcu_read_lock(); 220521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 221521f1cf1SEric Dumazet if (ca_ops) 222521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 223521f1cf1SEric Dumazet rcu_read_unlock(); 224521f1cf1SEric Dumazet if (err < 0) 2256e277ed5SThomas Graf goto errout; 226521f1cf1SEric Dumazet } 2276e277ed5SThomas Graf 228a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 229a8c2190eSArnaldo Carvalho de Melo 230521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) { 23164f40ff5SEric Dumazet union tcp_cc_info info; 23264f40ff5SEric Dumazet size_t sz = 0; 23364f40ff5SEric Dumazet int attr; 234521f1cf1SEric Dumazet 235521f1cf1SEric Dumazet rcu_read_lock(); 236521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 237521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info) 23864f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info); 239521f1cf1SEric Dumazet rcu_read_unlock(); 24064f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0) 241521f1cf1SEric Dumazet goto errout; 242521f1cf1SEric Dumazet } 243a8c2190eSArnaldo Carvalho de Melo 2443c4d05c8SPavel Emelyanov out: 245053c095aSJohannes Berg nlmsg_end(skb, nlh); 246053c095aSJohannes Berg return 0; 247a8c2190eSArnaldo Carvalho de Melo 2486e277ed5SThomas Graf errout: 2496e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 25026932566SPatrick McHardy return -EMSGSIZE; 251a8c2190eSArnaldo Carvalho de Melo } 2523c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 2533c4d05c8SPavel Emelyanov 2543c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk, 255e31c5e0eSEric Dumazet struct sk_buff *skb, 25634160ea3SEric Dumazet const struct inet_diag_req_v2 *req, 257d06ca956SEric W. Biederman struct user_namespace *user_ns, 25815e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 2593c4d05c8SPavel Emelyanov const struct nlmsghdr *unlh) 2603c4d05c8SPavel Emelyanov { 261e31c5e0eSEric Dumazet return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, 262e31c5e0eSEric Dumazet user_ns, portid, seq, nlmsg_flags, unlh); 2633c4d05c8SPavel Emelyanov } 264a8c2190eSArnaldo Carvalho de Melo 26533cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk, 266e31c5e0eSEric Dumazet struct sk_buff *skb, 26715e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 268c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 269c7d58aabSArnaldo Carvalho de Melo { 27033cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk); 271c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 2726e277ed5SThomas Graf struct nlmsghdr *nlh; 273789f558cSEric Dumazet long tmo; 274c7d58aabSArnaldo Carvalho de Melo 27515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 2766e277ed5SThomas Graf nlmsg_flags); 2776e277ed5SThomas Graf if (!nlh) 278d106352dSDavid S. Miller return -EMSGSIZE; 279d106352dSDavid S. Miller 280d106352dSDavid S. Miller r = nlmsg_data(nlh); 281c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 282c7d58aabSArnaldo Carvalho de Melo 283789f558cSEric Dumazet tmo = tw->tw_timer.expires - jiffies; 284c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 285c7d58aabSArnaldo Carvalho de Melo tmo = 0; 286c7d58aabSArnaldo Carvalho de Melo 287a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 288c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 289b1aac815SDaniel Borkmann 290c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 291c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 29296f817feSEric Dumazet r->idiag_expires = jiffies_to_msecs(tmo); 293c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 294c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 295c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 296c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 2976e277ed5SThomas Graf 298053c095aSJohannes Berg nlmsg_end(skb, nlh); 299053c095aSJohannes Berg return 0; 300c7d58aabSArnaldo Carvalho de Melo } 301c7d58aabSArnaldo Carvalho de Melo 302a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 303a58917f5SEric Dumazet u32 portid, u32 seq, u16 nlmsg_flags, 304a58917f5SEric Dumazet const struct nlmsghdr *unlh) 305a58917f5SEric Dumazet { 306a58917f5SEric Dumazet struct inet_diag_msg *r; 307a58917f5SEric Dumazet struct nlmsghdr *nlh; 308a58917f5SEric Dumazet long tmo; 309a58917f5SEric Dumazet 310a58917f5SEric Dumazet nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 311a58917f5SEric Dumazet nlmsg_flags); 312a58917f5SEric Dumazet if (!nlh) 313a58917f5SEric Dumazet return -EMSGSIZE; 314a58917f5SEric Dumazet 315a58917f5SEric Dumazet r = nlmsg_data(nlh); 316a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk); 317a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV; 318a58917f5SEric Dumazet r->idiag_timer = 1; 319a58917f5SEric Dumazet r->idiag_retrans = inet_reqsk(sk)->num_retrans; 320a58917f5SEric Dumazet 321a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 322a58917f5SEric Dumazet offsetof(struct sock, sk_cookie)); 323a58917f5SEric Dumazet 324fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 325a58917f5SEric Dumazet r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0; 326a58917f5SEric Dumazet r->idiag_rqueue = 0; 327a58917f5SEric Dumazet r->idiag_wqueue = 0; 328a58917f5SEric Dumazet r->idiag_uid = 0; 329a58917f5SEric Dumazet r->idiag_inode = 0; 330a58917f5SEric Dumazet 331a58917f5SEric Dumazet nlmsg_end(skb, nlh); 332a58917f5SEric Dumazet return 0; 333a58917f5SEric Dumazet } 334a58917f5SEric Dumazet 335dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 33634160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 337d06ca956SEric W. Biederman struct user_namespace *user_ns, 33815e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 339dff2c035SArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 340dff2c035SArnaldo Carvalho de Melo { 341dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 342a58917f5SEric Dumazet return inet_twsk_diag_fill(sk, skb, portid, seq, 343a58917f5SEric Dumazet nlmsg_flags, unlh); 344a58917f5SEric Dumazet 345a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) 346a58917f5SEric Dumazet return inet_req_diag_fill(sk, skb, portid, seq, 347efe4208fSEric Dumazet nlmsg_flags, unlh); 348efe4208fSEric Dumazet 349efe4208fSEric Dumazet return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, 350efe4208fSEric Dumazet nlmsg_flags, unlh); 351dff2c035SArnaldo Carvalho de Melo } 352dff2c035SArnaldo Carvalho de Melo 353b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net, 354b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo, 35534160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 356a8c2190eSArnaldo Carvalho de Melo { 357e31c5e0eSEric Dumazet struct sock *sk; 358a8c2190eSArnaldo Carvalho de Melo 359e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 360*a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 361a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 362a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 363dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3647c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) { 3657c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 3667c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 367*a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 3687c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3], 3697c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if); 3707c130672SEric Dumazet else 371*a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0, 372a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 373a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 374a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 375a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 376a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 3777c130672SEric Dumazet } 378a8c2190eSArnaldo Carvalho de Melo #endif 379e31c5e0eSEric Dumazet else 380b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL); 381a8c2190eSArnaldo Carvalho de Melo 382e31c5e0eSEric Dumazet if (!sk) 383b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 384a8c2190eSArnaldo Carvalho de Melo 385b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 386b613f56eSLorenzo Colitti sock_gen_put(sk); 387b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 388b613f56eSLorenzo Colitti } 389b613f56eSLorenzo Colitti 390b613f56eSLorenzo Colitti return sk; 391b613f56eSLorenzo Colitti } 392b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 393b613f56eSLorenzo Colitti 394b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 395b613f56eSLorenzo Colitti struct sk_buff *in_skb, 396b613f56eSLorenzo Colitti const struct nlmsghdr *nlh, 397b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req) 398b613f56eSLorenzo Colitti { 399b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk); 400b613f56eSLorenzo Colitti struct sk_buff *rep; 401b613f56eSLorenzo Colitti struct sock *sk; 402b613f56eSLorenzo Colitti int err; 403b613f56eSLorenzo Colitti 404b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req); 405b613f56eSLorenzo Colitti if (IS_ERR(sk)) 406b613f56eSLorenzo Colitti return PTR_ERR(sk); 407a8c2190eSArnaldo Carvalho de Melo 408c8e2c80dSEric Dumazet rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL); 4096e277ed5SThomas Graf if (!rep) { 4106e277ed5SThomas Graf err = -ENOMEM; 411a8c2190eSArnaldo Carvalho de Melo goto out; 4126e277ed5SThomas Graf } 413a8c2190eSArnaldo Carvalho de Melo 414a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 415e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(in_skb).sk), 41615e47304SEric W. Biederman NETLINK_CB(in_skb).portid, 41726932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 41826932566SPatrick McHardy if (err < 0) { 41926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 4206e277ed5SThomas Graf nlmsg_free(rep); 42126932566SPatrick McHardy goto out; 42226932566SPatrick McHardy } 42315e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 424a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 425a8c2190eSArnaldo Carvalho de Melo if (err > 0) 426a8c2190eSArnaldo Carvalho de Melo err = 0; 427a8c2190eSArnaldo Carvalho de Melo 428a8c2190eSArnaldo Carvalho de Melo out: 429c1d607ccSEric Dumazet if (sk) 430c1d607ccSEric Dumazet sock_gen_put(sk); 431c1d607ccSEric Dumazet 432476f7dbfSPavel Emelyanov return err; 433476f7dbfSPavel Emelyanov } 4341942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 435476f7dbfSPavel Emelyanov 4366eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 437476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 43834160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 439476f7dbfSPavel Emelyanov { 440476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 441476f7dbfSPavel Emelyanov int err; 442476f7dbfSPavel Emelyanov 443476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 444476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 445476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 4466eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DIAG_BY_FAMILY) 4471942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 4486eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DESTROY && handler->destroy) 4496eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req); 4506eb5d2e0SLorenzo Colitti else 4516eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP; 452d523a328SHerbert Xu inet_diag_unlock_handler(handler); 453476f7dbfSPavel Emelyanov 454a8c2190eSArnaldo Carvalho de Melo return err; 455a8c2190eSArnaldo Carvalho de Melo } 456a8c2190eSArnaldo Carvalho de Melo 4579f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 458a8c2190eSArnaldo Carvalho de Melo { 459a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 460a8c2190eSArnaldo Carvalho de Melo 461a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 462a8c2190eSArnaldo Carvalho de Melo 463a8c2190eSArnaldo Carvalho de Melo if (words) { 464a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 465a8c2190eSArnaldo Carvalho de Melo return 0; 466a8c2190eSArnaldo Carvalho de Melo } 467a8c2190eSArnaldo Carvalho de Melo if (bits) { 4689f855299SAl Viro __be32 w1, w2; 4699f855299SAl Viro __be32 mask; 470a8c2190eSArnaldo Carvalho de Melo 471a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 472a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 473a8c2190eSArnaldo Carvalho de Melo 474a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 475a8c2190eSArnaldo Carvalho de Melo 476a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 477a8c2190eSArnaldo Carvalho de Melo return 0; 478a8c2190eSArnaldo Carvalho de Melo } 479a8c2190eSArnaldo Carvalho de Melo 480a8c2190eSArnaldo Carvalho de Melo return 1; 481a8c2190eSArnaldo Carvalho de Melo } 482a8c2190eSArnaldo Carvalho de Melo 48387c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 484a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 485a8c2190eSArnaldo Carvalho de Melo { 48687c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 48787c22ea5SPavel Emelyanov int len = nla_len(_bc); 48887c22ea5SPavel Emelyanov 489a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 490a8c2190eSArnaldo Carvalho de Melo int yes = 1; 491a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 492a8c2190eSArnaldo Carvalho de Melo 493a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 494a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 495a8c2190eSArnaldo Carvalho de Melo break; 496a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 497a8c2190eSArnaldo Carvalho de Melo yes = 0; 498a8c2190eSArnaldo Carvalho de Melo break; 499a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 500a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 501a8c2190eSArnaldo Carvalho de Melo break; 502a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 503b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 504a8c2190eSArnaldo Carvalho de Melo break; 505a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 506a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 507a8c2190eSArnaldo Carvalho de Melo break; 508a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 509a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 510a8c2190eSArnaldo Carvalho de Melo break; 511a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 512a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 513a8c2190eSArnaldo Carvalho de Melo break; 514a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 515a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 516e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 517e31c5e0eSEric Dumazet const __be32 *addr; 518a8c2190eSArnaldo Carvalho de Melo 519e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 520a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 521a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 522a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 523a8c2190eSArnaldo Carvalho de Melo yes = 0; 524a8c2190eSArnaldo Carvalho de Melo break; 525a8c2190eSArnaldo Carvalho de Melo } 526a8c2190eSArnaldo Carvalho de Melo 527a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 528a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 529a8c2190eSArnaldo Carvalho de Melo else 530a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 531a8c2190eSArnaldo Carvalho de Melo 532f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 533f67caec9SNeal Cardwell cond->family != entry->family) { 534a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 535a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 536a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 537a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 538f67caec9SNeal Cardwell bitstring_match(addr + 3, 539f67caec9SNeal Cardwell cond->addr, 540a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 541a8c2190eSArnaldo Carvalho de Melo break; 542a8c2190eSArnaldo Carvalho de Melo } 543a8c2190eSArnaldo Carvalho de Melo yes = 0; 544a8c2190eSArnaldo Carvalho de Melo break; 545a8c2190eSArnaldo Carvalho de Melo } 546f67caec9SNeal Cardwell 547f67caec9SNeal Cardwell if (cond->prefix_len == 0) 548f67caec9SNeal Cardwell break; 549f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 550f67caec9SNeal Cardwell cond->prefix_len)) 551f67caec9SNeal Cardwell break; 552f67caec9SNeal Cardwell yes = 0; 553f67caec9SNeal Cardwell break; 554f67caec9SNeal Cardwell } 555a8c2190eSArnaldo Carvalho de Melo } 556a8c2190eSArnaldo Carvalho de Melo 557a8c2190eSArnaldo Carvalho de Melo if (yes) { 558a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 559a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 560a8c2190eSArnaldo Carvalho de Melo } else { 561a8c2190eSArnaldo Carvalho de Melo len -= op->no; 562a8c2190eSArnaldo Carvalho de Melo bc += op->no; 563a8c2190eSArnaldo Carvalho de Melo } 564a8c2190eSArnaldo Carvalho de Melo } 565a02cec21SEric Dumazet return len == 0; 566a8c2190eSArnaldo Carvalho de Melo } 567a8c2190eSArnaldo Carvalho de Melo 568a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 569a4458343SEric Dumazet */ 570a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 571a4458343SEric Dumazet const struct sock *sk) 572a4458343SEric Dumazet { 573a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 574a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 575a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 576a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 577a4458343SEric Dumazet } else 578a4458343SEric Dumazet #endif 579a4458343SEric Dumazet { 580a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 581a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 582a4458343SEric Dumazet } 583a4458343SEric Dumazet } 584a4458343SEric Dumazet 5858d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 5868d07d151SPavel Emelyanov { 5878d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 588e31c5e0eSEric Dumazet struct inet_diag_entry entry; 5898d07d151SPavel Emelyanov 590e31c5e0eSEric Dumazet if (!bc) 5918d07d151SPavel Emelyanov return 1; 5928d07d151SPavel Emelyanov 5938d07d151SPavel Emelyanov entry.family = sk->sk_family; 594a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 5958d07d151SPavel Emelyanov entry.sport = inet->inet_num; 5968d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 597a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 5988d07d151SPavel Emelyanov 5998d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 6008d07d151SPavel Emelyanov } 6018d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 6028d07d151SPavel Emelyanov 603a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 604a8c2190eSArnaldo Carvalho de Melo { 605a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 606a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 607a8c2190eSArnaldo Carvalho de Melo 608a8c2190eSArnaldo Carvalho de Melo if (cc > len) 609a8c2190eSArnaldo Carvalho de Melo return 0; 610a8c2190eSArnaldo Carvalho de Melo if (cc == len) 611a8c2190eSArnaldo Carvalho de Melo return 1; 612eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 613a8c2190eSArnaldo Carvalho de Melo return 0; 614a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 615a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 616a8c2190eSArnaldo Carvalho de Melo } 617a8c2190eSArnaldo Carvalho de Melo return 0; 618a8c2190eSArnaldo Carvalho de Melo } 619a8c2190eSArnaldo Carvalho de Melo 620405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 621405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 622405c0059SNeal Cardwell int *min_len) 623405c0059SNeal Cardwell { 624405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 625e31c5e0eSEric Dumazet int addr_len; 626405c0059SNeal Cardwell 627405c0059SNeal Cardwell /* Check hostcond space. */ 628405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 629405c0059SNeal Cardwell if (len < *min_len) 630405c0059SNeal Cardwell return false; 631405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 632405c0059SNeal Cardwell 633405c0059SNeal Cardwell /* Check address family and address length. */ 634405c0059SNeal Cardwell switch (cond->family) { 635405c0059SNeal Cardwell case AF_UNSPEC: 636405c0059SNeal Cardwell addr_len = 0; 637405c0059SNeal Cardwell break; 638405c0059SNeal Cardwell case AF_INET: 639405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 640405c0059SNeal Cardwell break; 641405c0059SNeal Cardwell case AF_INET6: 642405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 643405c0059SNeal Cardwell break; 644405c0059SNeal Cardwell default: 645405c0059SNeal Cardwell return false; 646405c0059SNeal Cardwell } 647405c0059SNeal Cardwell *min_len += addr_len; 648405c0059SNeal Cardwell if (len < *min_len) 649405c0059SNeal Cardwell return false; 650405c0059SNeal Cardwell 651405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 652405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 653405c0059SNeal Cardwell return false; 654405c0059SNeal Cardwell 655405c0059SNeal Cardwell return true; 656405c0059SNeal Cardwell } 657405c0059SNeal Cardwell 6585e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 659e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 6605e1f5420SNeal Cardwell int len, int *min_len) 6615e1f5420SNeal Cardwell { 6625e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 6635e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 6645e1f5420SNeal Cardwell if (len < *min_len) 6655e1f5420SNeal Cardwell return false; 6665e1f5420SNeal Cardwell return true; 6675e1f5420SNeal Cardwell } 6685e1f5420SNeal Cardwell 669a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 670a8c2190eSArnaldo Carvalho de Melo { 671eeb14972SEric Dumazet const void *bc = bytecode; 672a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 673a8c2190eSArnaldo Carvalho de Melo 674a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 675405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 676e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 677a8c2190eSArnaldo Carvalho de Melo 678a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 679a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 680a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 681405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 682405c0059SNeal Cardwell return -EINVAL; 6835e1f5420SNeal Cardwell break; 684a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 685a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 686a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 687a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 6885e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 689a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 690a8c2190eSArnaldo Carvalho de Melo break; 6915e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 6925e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 693a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 694a8c2190eSArnaldo Carvalho de Melo break; 695a8c2190eSArnaldo Carvalho de Melo default: 696a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 697a8c2190eSArnaldo Carvalho de Melo } 6985e1f5420SNeal Cardwell 6995e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 7005e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 7015e1f5420SNeal Cardwell return -EINVAL; 7025e1f5420SNeal Cardwell if (op->no < len && 7035e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 7045e1f5420SNeal Cardwell return -EINVAL; 7055e1f5420SNeal Cardwell } 7065e1f5420SNeal Cardwell 707405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 708eeb14972SEric Dumazet return -EINVAL; 709a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 710a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 711a8c2190eSArnaldo Carvalho de Melo } 712a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 713a8c2190eSArnaldo Carvalho de Melo } 714a8c2190eSArnaldo Carvalho de Melo 715dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 716dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 71737f352b5SPavel Emelyanov struct netlink_callback *cb, 71834160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 71937f352b5SPavel Emelyanov const struct nlattr *bc) 720a8c2190eSArnaldo Carvalho de Melo { 7218d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 722a8c2190eSArnaldo Carvalho de Melo return 0; 723a8c2190eSArnaldo Carvalho de Melo 724a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 725e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 72615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 727a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 728a8c2190eSArnaldo Carvalho de Melo } 729a8c2190eSArnaldo Carvalho de Melo 73049612729SEric Dumazet static void twsk_build_assert(void) 73149612729SEric Dumazet { 73249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 73349612729SEric Dumazet offsetof(struct sock, sk_family)); 73449612729SEric Dumazet 73549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 73649612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 73749612729SEric Dumazet 73849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 73949612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 74049612729SEric Dumazet 74149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 74249612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 74349612729SEric Dumazet 74449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 74549612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 74649612729SEric Dumazet 74749612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 74849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 74949612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 75049612729SEric Dumazet 75149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 75249612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 75349612729SEric Dumazet #endif 75449612729SEric Dumazet } 75549612729SEric Dumazet 7561942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 757e31c5e0eSEric Dumazet struct netlink_callback *cb, 75834160ea3SEric Dumazet const struct inet_diag_req_v2 *r, struct nlattr *bc) 759a8c2190eSArnaldo Carvalho de Melo { 76051d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 761e31c5e0eSEric Dumazet int i, num, s_i, s_num; 762079096f1SEric Dumazet u32 idiag_states = r->idiag_states; 763a8c2190eSArnaldo Carvalho de Melo 764079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 765079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 766a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 767a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 768a8c2190eSArnaldo Carvalho de Melo 769a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 770079096f1SEric Dumazet if (!(idiag_states & TCPF_LISTEN)) 771a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 772a8c2190eSArnaldo Carvalho de Melo 773a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 7745caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 775e31c5e0eSEric Dumazet struct hlist_nulls_node *node; 776e31c5e0eSEric Dumazet struct sock *sk; 777a8c2190eSArnaldo Carvalho de Melo 778a8c2190eSArnaldo Carvalho de Melo num = 0; 7795caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 7805caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 781c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 782a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 783a8c2190eSArnaldo Carvalho de Melo 78451d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 78551d7cccfSAndrey Vagin continue; 78651d7cccfSAndrey Vagin 787a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 788a8c2190eSArnaldo Carvalho de Melo num++; 789a8c2190eSArnaldo Carvalho de Melo continue; 790a8c2190eSArnaldo Carvalho de Melo } 791a8c2190eSArnaldo Carvalho de Melo 792d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 793d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 794d23deaa0SPavel Emelyanov goto next_listen; 795d23deaa0SPavel Emelyanov 796c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 797a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 798a8c2190eSArnaldo Carvalho de Melo goto next_listen; 799a8c2190eSArnaldo Carvalho de Melo 800079096f1SEric Dumazet if (r->id.idiag_dport || 801a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 802a8c2190eSArnaldo Carvalho de Melo goto next_listen; 803a8c2190eSArnaldo Carvalho de Melo 804079096f1SEric Dumazet if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 8055caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 806a8c2190eSArnaldo Carvalho de Melo goto done; 807a8c2190eSArnaldo Carvalho de Melo } 808a8c2190eSArnaldo Carvalho de Melo 809a8c2190eSArnaldo Carvalho de Melo next_listen: 810a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 811a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 812a8c2190eSArnaldo Carvalho de Melo ++num; 813a8c2190eSArnaldo Carvalho de Melo } 8145caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 815a8c2190eSArnaldo Carvalho de Melo 816a8c2190eSArnaldo Carvalho de Melo s_num = 0; 817a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 818a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 819a8c2190eSArnaldo Carvalho de Melo } 820a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 821a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 822a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 823a8c2190eSArnaldo Carvalho de Melo } 824a8c2190eSArnaldo Carvalho de Melo 825079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 826efb3cb42SPavel Emelyanov goto out; 827a8c2190eSArnaldo Carvalho de Melo 828f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 829a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 8307e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 8313ab5aee7SEric Dumazet struct hlist_nulls_node *node; 832e31c5e0eSEric Dumazet struct sock *sk; 833a8c2190eSArnaldo Carvalho de Melo 8346be547a6SAndi Kleen num = 0; 8356be547a6SAndi Kleen 83605dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 8376be547a6SAndi Kleen continue; 8386be547a6SAndi Kleen 839a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 840a8c2190eSArnaldo Carvalho de Melo s_num = 0; 841a8c2190eSArnaldo Carvalho de Melo 8427e3aab4aSDavid S. Miller spin_lock_bh(lock); 8433ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 844e31c5e0eSEric Dumazet int state, res; 845a8c2190eSArnaldo Carvalho de Melo 84651d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 84751d7cccfSAndrey Vagin continue; 848a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 849a8c2190eSArnaldo Carvalho de Melo goto next_normal; 85070315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 85170315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 852079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 853a8c2190eSArnaldo Carvalho de Melo goto next_normal; 854d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 855d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 856d23deaa0SPavel Emelyanov goto next_normal; 85705dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 858a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 859a8c2190eSArnaldo Carvalho de Melo goto next_normal; 86005dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 8614e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 862a8c2190eSArnaldo Carvalho de Melo goto next_normal; 863a58917f5SEric Dumazet twsk_build_assert(); 864a58917f5SEric Dumazet 865a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 866a58917f5SEric Dumazet goto next_normal; 867a58917f5SEric Dumazet 868a58917f5SEric Dumazet res = sk_diag_fill(sk, skb, r, 869a58917f5SEric Dumazet sk_user_ns(NETLINK_CB(cb->skb).sk), 870a58917f5SEric Dumazet NETLINK_CB(cb->skb).portid, 871a58917f5SEric Dumazet cb->nlh->nlmsg_seq, NLM_F_MULTI, 872a58917f5SEric Dumazet cb->nlh); 87305dbc7b5SEric Dumazet if (res < 0) { 8747e3aab4aSDavid S. Miller spin_unlock_bh(lock); 875a8c2190eSArnaldo Carvalho de Melo goto done; 876a8c2190eSArnaldo Carvalho de Melo } 877a8c2190eSArnaldo Carvalho de Melo next_normal: 878a8c2190eSArnaldo Carvalho de Melo ++num; 879a8c2190eSArnaldo Carvalho de Melo } 880a8c2190eSArnaldo Carvalho de Melo 8817e3aab4aSDavid S. Miller spin_unlock_bh(lock); 882a8c2190eSArnaldo Carvalho de Melo } 883a8c2190eSArnaldo Carvalho de Melo 884a8c2190eSArnaldo Carvalho de Melo done: 885a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 886a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 887efb3cb42SPavel Emelyanov out: 888efb3cb42SPavel Emelyanov ; 889efb3cb42SPavel Emelyanov } 8901942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 891efb3cb42SPavel Emelyanov 892efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 89334160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 894e31c5e0eSEric Dumazet struct nlattr *bc) 895efb3cb42SPavel Emelyanov { 896efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 897cacb6ba0SCyrill Gorcunov int err = 0; 898efb3cb42SPavel Emelyanov 899efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 900efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 9011942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 902cacb6ba0SCyrill Gorcunov else 903cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 904d523a328SHerbert Xu inet_diag_unlock_handler(handler); 905efb3cb42SPavel Emelyanov 906cacb6ba0SCyrill Gorcunov return err ? : skb->len; 907a8c2190eSArnaldo Carvalho de Melo } 908a8c2190eSArnaldo Carvalho de Melo 90925c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 91025c4cd2bSPavel Emelyanov { 911c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 912e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 91325c4cd2bSPavel Emelyanov 91425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 91525c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 91625c4cd2bSPavel Emelyanov 917d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 91825c4cd2bSPavel Emelyanov } 91925c4cd2bSPavel Emelyanov 920e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 921a029fe26SPavel Emelyanov { 922a029fe26SPavel Emelyanov switch (type) { 923a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 924a029fe26SPavel Emelyanov return IPPROTO_TCP; 925a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 926a029fe26SPavel Emelyanov return IPPROTO_DCCP; 927a029fe26SPavel Emelyanov default: 928a029fe26SPavel Emelyanov return 0; 929a029fe26SPavel Emelyanov } 930a029fe26SPavel Emelyanov } 931a029fe26SPavel Emelyanov 932e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 933e31c5e0eSEric Dumazet struct netlink_callback *cb) 93425c4cd2bSPavel Emelyanov { 935d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 936e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 937c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 93825c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 93925c4cd2bSPavel Emelyanov 940d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 94125c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 94225c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 94325c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 94425c4cd2bSPavel Emelyanov req.id = rc->id; 94525c4cd2bSPavel Emelyanov 94625c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 94725c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 94825c4cd2bSPavel Emelyanov 94925c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 95025c4cd2bSPavel Emelyanov } 95125c4cd2bSPavel Emelyanov 952fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 953fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 954fe50ce28SPavel Emelyanov { 955d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 956c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 957fe50ce28SPavel Emelyanov 958fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 959fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 960fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 961fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 962fe50ce28SPavel Emelyanov req.id = rc->id; 963fe50ce28SPavel Emelyanov 9646eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req); 965fe50ce28SPavel Emelyanov } 966fe50ce28SPavel Emelyanov 9678d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 968a8c2190eSArnaldo Carvalho de Melo { 9693b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 97051d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 971a8c2190eSArnaldo Carvalho de Melo 972ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 973ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 974ead592baSThomas Graf return -EINVAL; 975a8c2190eSArnaldo Carvalho de Melo 976b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 977ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 978ead592baSThomas Graf struct nlattr *attr; 979ead592baSThomas Graf 980ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 981ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 982e31c5e0eSEric Dumazet if (!attr || 983ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 984ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 985a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 986a8c2190eSArnaldo Carvalho de Melo } 98780d326faSPablo Neira Ayuso { 98880d326faSPablo Neira Ayuso struct netlink_dump_control c = { 98980d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 99080d326faSPablo Neira Ayuso }; 99151d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 99280d326faSPablo Neira Ayuso } 993a8c2190eSArnaldo Carvalho de Melo } 994ead592baSThomas Graf 995fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 996a8c2190eSArnaldo Carvalho de Melo } 997a8c2190eSArnaldo Carvalho de Melo 9986eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 999d366477aSPavel Emelyanov { 1000c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 100151d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1002d366477aSPavel Emelyanov 1003d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1004d366477aSPavel Emelyanov return -EINVAL; 1005d366477aSPavel Emelyanov 10066eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 10076eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) { 100825c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 100925c4cd2bSPavel Emelyanov struct nlattr *attr; 1010e31c5e0eSEric Dumazet 101125c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 101225c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 1013e31c5e0eSEric Dumazet if (!attr || 101425c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 101525c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 101625c4cd2bSPavel Emelyanov return -EINVAL; 101725c4cd2bSPavel Emelyanov } 101880d326faSPablo Neira Ayuso { 101980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 102080d326faSPablo Neira Ayuso .dump = inet_diag_dump, 102180d326faSPablo Neira Ayuso }; 102251d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 102380d326faSPablo Neira Ayuso } 1024d366477aSPavel Emelyanov } 1025d366477aSPavel Emelyanov 10266eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); 1027d366477aSPavel Emelyanov } 1028d366477aSPavel Emelyanov 102935ac838aSCraig Gallek static 103035ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 103135ac838aSCraig Gallek { 103235ac838aSCraig Gallek const struct inet_diag_handler *handler; 103335ac838aSCraig Gallek struct nlmsghdr *nlh; 103435ac838aSCraig Gallek struct nlattr *attr; 103535ac838aSCraig Gallek struct inet_diag_msg *r; 103635ac838aSCraig Gallek void *info = NULL; 103735ac838aSCraig Gallek int err = 0; 103835ac838aSCraig Gallek 103935ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 104035ac838aSCraig Gallek if (!nlh) 104135ac838aSCraig Gallek return -ENOMEM; 104235ac838aSCraig Gallek 104335ac838aSCraig Gallek r = nlmsg_data(nlh); 104435ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 104535ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1046e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1047e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 104835ac838aSCraig Gallek r->idiag_state = sk->sk_state; 104935ac838aSCraig Gallek 105035ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 105135ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 105235ac838aSCraig Gallek return err; 105335ac838aSCraig Gallek } 105435ac838aSCraig Gallek 105535ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 105635ac838aSCraig Gallek if (IS_ERR(handler)) { 105735ac838aSCraig Gallek inet_diag_unlock_handler(handler); 105835ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 105935ac838aSCraig Gallek return PTR_ERR(handler); 106035ac838aSCraig Gallek } 106135ac838aSCraig Gallek 106235ac838aSCraig Gallek attr = handler->idiag_info_size 106335ac838aSCraig Gallek ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size) 106435ac838aSCraig Gallek : NULL; 106535ac838aSCraig Gallek if (attr) 106635ac838aSCraig Gallek info = nla_data(attr); 106735ac838aSCraig Gallek 106835ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 106935ac838aSCraig Gallek inet_diag_unlock_handler(handler); 107035ac838aSCraig Gallek 107135ac838aSCraig Gallek nlmsg_end(skb, nlh); 107235ac838aSCraig Gallek return 0; 107335ac838aSCraig Gallek } 107435ac838aSCraig Gallek 10758dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1076d366477aSPavel Emelyanov .family = AF_INET, 10776eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 107835ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 10796eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1080d366477aSPavel Emelyanov }; 1081d366477aSPavel Emelyanov 10828dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1083d366477aSPavel Emelyanov .family = AF_INET6, 10846eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 108535ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 10866eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1087d366477aSPavel Emelyanov }; 1088d366477aSPavel Emelyanov 1089a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1090a8c2190eSArnaldo Carvalho de Melo { 1091a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1092a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1093a8c2190eSArnaldo Carvalho de Melo 1094f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1095a8c2190eSArnaldo Carvalho de Melo goto out; 1096a8c2190eSArnaldo Carvalho de Melo 1097d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1098a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1099e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1100a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1101a8c2190eSArnaldo Carvalho de Melo err = 0; 1102a8c2190eSArnaldo Carvalho de Melo } 1103d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1104a8c2190eSArnaldo Carvalho de Melo out: 1105a8c2190eSArnaldo Carvalho de Melo return err; 1106a8c2190eSArnaldo Carvalho de Melo } 1107a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1108a8c2190eSArnaldo Carvalho de Melo 1109a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1110a8c2190eSArnaldo Carvalho de Melo { 1111a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1112a8c2190eSArnaldo Carvalho de Melo 1113f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1114a8c2190eSArnaldo Carvalho de Melo return; 1115a8c2190eSArnaldo Carvalho de Melo 1116d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1117a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1118d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1119a8c2190eSArnaldo Carvalho de Melo } 1120a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1121a8c2190eSArnaldo Carvalho de Melo 1122a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1123a8c2190eSArnaldo Carvalho de Melo { 1124f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1125a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1126a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1127a8c2190eSArnaldo Carvalho de Melo 11280da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1129a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1130a8c2190eSArnaldo Carvalho de Melo goto out; 1131a8c2190eSArnaldo Carvalho de Melo 1132d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1133d366477aSPavel Emelyanov if (err) 1134d366477aSPavel Emelyanov goto out_free_nl; 1135d366477aSPavel Emelyanov 1136d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1137d366477aSPavel Emelyanov if (err) 1138d366477aSPavel Emelyanov goto out_free_inet; 1139d366477aSPavel Emelyanov 11408ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1141a8c2190eSArnaldo Carvalho de Melo out: 1142a8c2190eSArnaldo Carvalho de Melo return err; 1143d366477aSPavel Emelyanov 1144d366477aSPavel Emelyanov out_free_inet: 1145d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1146d366477aSPavel Emelyanov out_free_nl: 1147a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1148a8c2190eSArnaldo Carvalho de Melo goto out; 1149a8c2190eSArnaldo Carvalho de Melo } 1150a8c2190eSArnaldo Carvalho de Melo 1151a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1152a8c2190eSArnaldo Carvalho de Melo { 1153d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1154d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 11558ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1156a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1157a8c2190eSArnaldo Carvalho de Melo } 1158a8c2190eSArnaldo Carvalho de Melo 1159a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1160a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1161a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1162aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1163aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1164