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 155*8220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 156*8220ea23SPhil 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 353e31c5e0eSEric Dumazet int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 354e31c5e0eSEric Dumazet struct sk_buff *in_skb, 355e31c5e0eSEric Dumazet const struct nlmsghdr *nlh, 35634160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 357a8c2190eSArnaldo Carvalho de Melo { 35851d7cccfSAndrey Vagin struct net *net = sock_net(in_skb->sk); 359e31c5e0eSEric Dumazet struct sk_buff *rep; 360e31c5e0eSEric Dumazet struct sock *sk; 361e31c5e0eSEric Dumazet int err; 362a8c2190eSArnaldo Carvalho de Melo 363d523a328SHerbert Xu err = -EINVAL; 364e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 36551d7cccfSAndrey Vagin sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0], 366a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 367a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 368dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 369e31c5e0eSEric Dumazet else if (req->sdiag_family == AF_INET6) 37051d7cccfSAndrey Vagin sk = inet6_lookup(net, hashinfo, 371a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 372a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 373a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 374a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 375a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 376a8c2190eSArnaldo Carvalho de Melo #endif 377e31c5e0eSEric Dumazet else 378476f7dbfSPavel Emelyanov goto out_nosk; 379a8c2190eSArnaldo Carvalho de Melo 380d523a328SHerbert Xu err = -ENOENT; 381e31c5e0eSEric Dumazet if (!sk) 382476f7dbfSPavel Emelyanov goto out_nosk; 383a8c2190eSArnaldo Carvalho de Melo 384f65c1b53SPavel Emelyanov err = sock_diag_check_cookie(sk, req->id.idiag_cookie); 385b005ab4eSPavel Emelyanov if (err) 386a8c2190eSArnaldo Carvalho de Melo goto out; 387a8c2190eSArnaldo Carvalho de Melo 388c8e2c80dSEric Dumazet rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL); 3896e277ed5SThomas Graf if (!rep) { 3906e277ed5SThomas Graf err = -ENOMEM; 391a8c2190eSArnaldo Carvalho de Melo goto out; 3926e277ed5SThomas Graf } 393a8c2190eSArnaldo Carvalho de Melo 394a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 395e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(in_skb).sk), 39615e47304SEric W. Biederman NETLINK_CB(in_skb).portid, 39726932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 39826932566SPatrick McHardy if (err < 0) { 39926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 4006e277ed5SThomas Graf nlmsg_free(rep); 40126932566SPatrick McHardy goto out; 40226932566SPatrick McHardy } 40315e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 404a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 405a8c2190eSArnaldo Carvalho de Melo if (err > 0) 406a8c2190eSArnaldo Carvalho de Melo err = 0; 407a8c2190eSArnaldo Carvalho de Melo 408a8c2190eSArnaldo Carvalho de Melo out: 409c1d607ccSEric Dumazet if (sk) 410c1d607ccSEric Dumazet sock_gen_put(sk); 411c1d607ccSEric Dumazet 412476f7dbfSPavel Emelyanov out_nosk: 413476f7dbfSPavel Emelyanov return err; 414476f7dbfSPavel Emelyanov } 4151942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 416476f7dbfSPavel Emelyanov 417476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb, 418476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 41934160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 420476f7dbfSPavel Emelyanov { 421476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 422476f7dbfSPavel Emelyanov int err; 423476f7dbfSPavel Emelyanov 424476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 425476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 426476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 427476f7dbfSPavel Emelyanov else 4281942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 429d523a328SHerbert Xu inet_diag_unlock_handler(handler); 430476f7dbfSPavel Emelyanov 431a8c2190eSArnaldo Carvalho de Melo return err; 432a8c2190eSArnaldo Carvalho de Melo } 433a8c2190eSArnaldo Carvalho de Melo 4349f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 435a8c2190eSArnaldo Carvalho de Melo { 436a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 437a8c2190eSArnaldo Carvalho de Melo 438a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 439a8c2190eSArnaldo Carvalho de Melo 440a8c2190eSArnaldo Carvalho de Melo if (words) { 441a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 442a8c2190eSArnaldo Carvalho de Melo return 0; 443a8c2190eSArnaldo Carvalho de Melo } 444a8c2190eSArnaldo Carvalho de Melo if (bits) { 4459f855299SAl Viro __be32 w1, w2; 4469f855299SAl Viro __be32 mask; 447a8c2190eSArnaldo Carvalho de Melo 448a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 449a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 450a8c2190eSArnaldo Carvalho de Melo 451a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 452a8c2190eSArnaldo Carvalho de Melo 453a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 454a8c2190eSArnaldo Carvalho de Melo return 0; 455a8c2190eSArnaldo Carvalho de Melo } 456a8c2190eSArnaldo Carvalho de Melo 457a8c2190eSArnaldo Carvalho de Melo return 1; 458a8c2190eSArnaldo Carvalho de Melo } 459a8c2190eSArnaldo Carvalho de Melo 46087c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 461a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 462a8c2190eSArnaldo Carvalho de Melo { 46387c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 46487c22ea5SPavel Emelyanov int len = nla_len(_bc); 46587c22ea5SPavel Emelyanov 466a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 467a8c2190eSArnaldo Carvalho de Melo int yes = 1; 468a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 469a8c2190eSArnaldo Carvalho de Melo 470a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 471a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 472a8c2190eSArnaldo Carvalho de Melo break; 473a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 474a8c2190eSArnaldo Carvalho de Melo yes = 0; 475a8c2190eSArnaldo Carvalho de Melo break; 476a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 477a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 478a8c2190eSArnaldo Carvalho de Melo break; 479a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 480b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 481a8c2190eSArnaldo Carvalho de Melo break; 482a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 483a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 484a8c2190eSArnaldo Carvalho de Melo break; 485a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 486a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 487a8c2190eSArnaldo Carvalho de Melo break; 488a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 489a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 490a8c2190eSArnaldo Carvalho de Melo break; 491a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 492a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 493e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 494e31c5e0eSEric Dumazet const __be32 *addr; 495a8c2190eSArnaldo Carvalho de Melo 496e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 497a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 498a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 499a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 500a8c2190eSArnaldo Carvalho de Melo yes = 0; 501a8c2190eSArnaldo Carvalho de Melo break; 502a8c2190eSArnaldo Carvalho de Melo } 503a8c2190eSArnaldo Carvalho de Melo 504a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 505a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 506a8c2190eSArnaldo Carvalho de Melo else 507a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 508a8c2190eSArnaldo Carvalho de Melo 509f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 510f67caec9SNeal Cardwell cond->family != entry->family) { 511a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 512a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 513a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 514a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 515f67caec9SNeal Cardwell bitstring_match(addr + 3, 516f67caec9SNeal Cardwell cond->addr, 517a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 518a8c2190eSArnaldo Carvalho de Melo break; 519a8c2190eSArnaldo Carvalho de Melo } 520a8c2190eSArnaldo Carvalho de Melo yes = 0; 521a8c2190eSArnaldo Carvalho de Melo break; 522a8c2190eSArnaldo Carvalho de Melo } 523f67caec9SNeal Cardwell 524f67caec9SNeal Cardwell if (cond->prefix_len == 0) 525f67caec9SNeal Cardwell break; 526f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 527f67caec9SNeal Cardwell cond->prefix_len)) 528f67caec9SNeal Cardwell break; 529f67caec9SNeal Cardwell yes = 0; 530f67caec9SNeal Cardwell break; 531f67caec9SNeal Cardwell } 532a8c2190eSArnaldo Carvalho de Melo } 533a8c2190eSArnaldo Carvalho de Melo 534a8c2190eSArnaldo Carvalho de Melo if (yes) { 535a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 536a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 537a8c2190eSArnaldo Carvalho de Melo } else { 538a8c2190eSArnaldo Carvalho de Melo len -= op->no; 539a8c2190eSArnaldo Carvalho de Melo bc += op->no; 540a8c2190eSArnaldo Carvalho de Melo } 541a8c2190eSArnaldo Carvalho de Melo } 542a02cec21SEric Dumazet return len == 0; 543a8c2190eSArnaldo Carvalho de Melo } 544a8c2190eSArnaldo Carvalho de Melo 545a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 546a4458343SEric Dumazet */ 547a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 548a4458343SEric Dumazet const struct sock *sk) 549a4458343SEric Dumazet { 550a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 551a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 552a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 553a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 554a4458343SEric Dumazet } else 555a4458343SEric Dumazet #endif 556a4458343SEric Dumazet { 557a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 558a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 559a4458343SEric Dumazet } 560a4458343SEric Dumazet } 561a4458343SEric Dumazet 5628d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 5638d07d151SPavel Emelyanov { 5648d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 565e31c5e0eSEric Dumazet struct inet_diag_entry entry; 5668d07d151SPavel Emelyanov 567e31c5e0eSEric Dumazet if (!bc) 5688d07d151SPavel Emelyanov return 1; 5698d07d151SPavel Emelyanov 5708d07d151SPavel Emelyanov entry.family = sk->sk_family; 571a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 5728d07d151SPavel Emelyanov entry.sport = inet->inet_num; 5738d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 574a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 5758d07d151SPavel Emelyanov 5768d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 5778d07d151SPavel Emelyanov } 5788d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 5798d07d151SPavel Emelyanov 580a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 581a8c2190eSArnaldo Carvalho de Melo { 582a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 583a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 584a8c2190eSArnaldo Carvalho de Melo 585a8c2190eSArnaldo Carvalho de Melo if (cc > len) 586a8c2190eSArnaldo Carvalho de Melo return 0; 587a8c2190eSArnaldo Carvalho de Melo if (cc == len) 588a8c2190eSArnaldo Carvalho de Melo return 1; 589eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 590a8c2190eSArnaldo Carvalho de Melo return 0; 591a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 592a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 593a8c2190eSArnaldo Carvalho de Melo } 594a8c2190eSArnaldo Carvalho de Melo return 0; 595a8c2190eSArnaldo Carvalho de Melo } 596a8c2190eSArnaldo Carvalho de Melo 597405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 598405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 599405c0059SNeal Cardwell int *min_len) 600405c0059SNeal Cardwell { 601405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 602e31c5e0eSEric Dumazet int addr_len; 603405c0059SNeal Cardwell 604405c0059SNeal Cardwell /* Check hostcond space. */ 605405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 606405c0059SNeal Cardwell if (len < *min_len) 607405c0059SNeal Cardwell return false; 608405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 609405c0059SNeal Cardwell 610405c0059SNeal Cardwell /* Check address family and address length. */ 611405c0059SNeal Cardwell switch (cond->family) { 612405c0059SNeal Cardwell case AF_UNSPEC: 613405c0059SNeal Cardwell addr_len = 0; 614405c0059SNeal Cardwell break; 615405c0059SNeal Cardwell case AF_INET: 616405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 617405c0059SNeal Cardwell break; 618405c0059SNeal Cardwell case AF_INET6: 619405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 620405c0059SNeal Cardwell break; 621405c0059SNeal Cardwell default: 622405c0059SNeal Cardwell return false; 623405c0059SNeal Cardwell } 624405c0059SNeal Cardwell *min_len += addr_len; 625405c0059SNeal Cardwell if (len < *min_len) 626405c0059SNeal Cardwell return false; 627405c0059SNeal Cardwell 628405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 629405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 630405c0059SNeal Cardwell return false; 631405c0059SNeal Cardwell 632405c0059SNeal Cardwell return true; 633405c0059SNeal Cardwell } 634405c0059SNeal Cardwell 6355e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 636e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 6375e1f5420SNeal Cardwell int len, int *min_len) 6385e1f5420SNeal Cardwell { 6395e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 6405e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 6415e1f5420SNeal Cardwell if (len < *min_len) 6425e1f5420SNeal Cardwell return false; 6435e1f5420SNeal Cardwell return true; 6445e1f5420SNeal Cardwell } 6455e1f5420SNeal Cardwell 646a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 647a8c2190eSArnaldo Carvalho de Melo { 648eeb14972SEric Dumazet const void *bc = bytecode; 649a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 650a8c2190eSArnaldo Carvalho de Melo 651a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 652405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 653e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 654a8c2190eSArnaldo Carvalho de Melo 655a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 656a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 657a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 658405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 659405c0059SNeal Cardwell return -EINVAL; 6605e1f5420SNeal Cardwell break; 661a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 662a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 663a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 664a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 6655e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 666a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 667a8c2190eSArnaldo Carvalho de Melo break; 6685e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 6695e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 670a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 671a8c2190eSArnaldo Carvalho de Melo break; 672a8c2190eSArnaldo Carvalho de Melo default: 673a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 674a8c2190eSArnaldo Carvalho de Melo } 6755e1f5420SNeal Cardwell 6765e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 6775e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 6785e1f5420SNeal Cardwell return -EINVAL; 6795e1f5420SNeal Cardwell if (op->no < len && 6805e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 6815e1f5420SNeal Cardwell return -EINVAL; 6825e1f5420SNeal Cardwell } 6835e1f5420SNeal Cardwell 684405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 685eeb14972SEric Dumazet return -EINVAL; 686a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 687a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 688a8c2190eSArnaldo Carvalho de Melo } 689a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 690a8c2190eSArnaldo Carvalho de Melo } 691a8c2190eSArnaldo Carvalho de Melo 692dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 693dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 69437f352b5SPavel Emelyanov struct netlink_callback *cb, 69534160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 69637f352b5SPavel Emelyanov const struct nlattr *bc) 697a8c2190eSArnaldo Carvalho de Melo { 6988d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 699a8c2190eSArnaldo Carvalho de Melo return 0; 700a8c2190eSArnaldo Carvalho de Melo 701a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 702e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 70315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 704a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 705a8c2190eSArnaldo Carvalho de Melo } 706a8c2190eSArnaldo Carvalho de Melo 70749612729SEric Dumazet static void twsk_build_assert(void) 70849612729SEric Dumazet { 70949612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 71049612729SEric Dumazet offsetof(struct sock, sk_family)); 71149612729SEric Dumazet 71249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 71349612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 71449612729SEric Dumazet 71549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 71649612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 71749612729SEric Dumazet 71849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 71949612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 72049612729SEric Dumazet 72149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 72249612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 72349612729SEric Dumazet 72449612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 72549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 72649612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 72749612729SEric Dumazet 72849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 72949612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 73049612729SEric Dumazet #endif 73149612729SEric Dumazet } 73249612729SEric Dumazet 733a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 73437f352b5SPavel Emelyanov struct netlink_callback *cb, 73534160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 73637f352b5SPavel Emelyanov const struct nlattr *bc) 737a8c2190eSArnaldo Carvalho de Melo { 738a8c2190eSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 739a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 740e31c5e0eSEric Dumazet struct inet_diag_entry entry; 741e31c5e0eSEric Dumazet int j, s_j, reqnum, s_reqnum; 742e31c5e0eSEric Dumazet struct listen_sock *lopt; 743a8c2190eSArnaldo Carvalho de Melo int err = 0; 744a8c2190eSArnaldo Carvalho de Melo 745a8c2190eSArnaldo Carvalho de Melo s_j = cb->args[3]; 746a8c2190eSArnaldo Carvalho de Melo s_reqnum = cb->args[4]; 747a8c2190eSArnaldo Carvalho de Melo 748a8c2190eSArnaldo Carvalho de Melo if (s_j > 0) 749a8c2190eSArnaldo Carvalho de Melo s_j--; 750a8c2190eSArnaldo Carvalho de Melo 751a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 752a8c2190eSArnaldo Carvalho de Melo 7533b188443SHiroaki SHIMODA spin_lock(&icsk->icsk_accept_queue.syn_wait_lock); 754a8c2190eSArnaldo Carvalho de Melo 755a8c2190eSArnaldo Carvalho de Melo lopt = icsk->icsk_accept_queue.listen_opt; 756fa76ce73SEric Dumazet if (!lopt || !listen_sock_qlen(lopt)) 757a8c2190eSArnaldo Carvalho de Melo goto out; 758a8c2190eSArnaldo Carvalho de Melo 759e31c5e0eSEric Dumazet if (bc) { 760c720c7e8SEric Dumazet entry.sport = inet->inet_num; 761a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 762a8c2190eSArnaldo Carvalho de Melo } 763a8c2190eSArnaldo Carvalho de Melo 764a8c2190eSArnaldo Carvalho de Melo for (j = s_j; j < lopt->nr_table_entries; j++) { 765a8c2190eSArnaldo Carvalho de Melo struct request_sock *req, *head = lopt->syn_table[j]; 766a8c2190eSArnaldo Carvalho de Melo 767a8c2190eSArnaldo Carvalho de Melo reqnum = 0; 768a8c2190eSArnaldo Carvalho de Melo for (req = head; req; reqnum++, req = req->dl_next) { 769a8c2190eSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 770a8c2190eSArnaldo Carvalho de Melo 771a8c2190eSArnaldo Carvalho de Melo if (reqnum < s_reqnum) 772a8c2190eSArnaldo Carvalho de Melo continue; 773634fb979SEric Dumazet if (r->id.idiag_dport != ireq->ir_rmt_port && 774a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 775a8c2190eSArnaldo Carvalho de Melo continue; 776a8c2190eSArnaldo Carvalho de Melo 777a8c2190eSArnaldo Carvalho de Melo if (bc) { 778a4458343SEric Dumazet /* Note: entry.sport and entry.userlocks are already set */ 77908d2cc3bSEric Dumazet entry_fill_addrs(&entry, req_to_sk(req)); 780634fb979SEric Dumazet entry.dport = ntohs(ireq->ir_rmt_port); 781a8c2190eSArnaldo Carvalho de Melo 78287c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 783a8c2190eSArnaldo Carvalho de Melo continue; 784a8c2190eSArnaldo Carvalho de Melo } 785a8c2190eSArnaldo Carvalho de Melo 78608d2cc3bSEric Dumazet err = inet_req_diag_fill(req_to_sk(req), skb, 78715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 788a58917f5SEric Dumazet cb->nlh->nlmsg_seq, 789a58917f5SEric Dumazet NLM_F_MULTI, cb->nlh); 790a8c2190eSArnaldo Carvalho de Melo if (err < 0) { 791a8c2190eSArnaldo Carvalho de Melo cb->args[3] = j + 1; 792a8c2190eSArnaldo Carvalho de Melo cb->args[4] = reqnum; 793a8c2190eSArnaldo Carvalho de Melo goto out; 794a8c2190eSArnaldo Carvalho de Melo } 795a8c2190eSArnaldo Carvalho de Melo } 796a8c2190eSArnaldo Carvalho de Melo 797a8c2190eSArnaldo Carvalho de Melo s_reqnum = 0; 798a8c2190eSArnaldo Carvalho de Melo } 799a8c2190eSArnaldo Carvalho de Melo 800a8c2190eSArnaldo Carvalho de Melo out: 8013b188443SHiroaki SHIMODA spin_unlock(&icsk->icsk_accept_queue.syn_wait_lock); 802a8c2190eSArnaldo Carvalho de Melo 803a8c2190eSArnaldo Carvalho de Melo return err; 804a8c2190eSArnaldo Carvalho de Melo } 805a8c2190eSArnaldo Carvalho de Melo 8061942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 807e31c5e0eSEric Dumazet struct netlink_callback *cb, 80834160ea3SEric Dumazet const struct inet_diag_req_v2 *r, struct nlattr *bc) 809a8c2190eSArnaldo Carvalho de Melo { 81051d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 811e31c5e0eSEric Dumazet int i, num, s_i, s_num; 812a8c2190eSArnaldo Carvalho de Melo 813a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 814a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 815a8c2190eSArnaldo Carvalho de Melo 816a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 817a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 818a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 819a8c2190eSArnaldo Carvalho de Melo 820a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 8215caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 822e31c5e0eSEric Dumazet struct hlist_nulls_node *node; 823e31c5e0eSEric Dumazet struct sock *sk; 824a8c2190eSArnaldo Carvalho de Melo 825a8c2190eSArnaldo Carvalho de Melo num = 0; 8265caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 8275caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 828c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 829a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 830a8c2190eSArnaldo Carvalho de Melo 83151d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 83251d7cccfSAndrey Vagin continue; 83351d7cccfSAndrey Vagin 834a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 835a8c2190eSArnaldo Carvalho de Melo num++; 836a8c2190eSArnaldo Carvalho de Melo continue; 837a8c2190eSArnaldo Carvalho de Melo } 838a8c2190eSArnaldo Carvalho de Melo 839d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 840d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 841d23deaa0SPavel Emelyanov goto next_listen; 842d23deaa0SPavel Emelyanov 843c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 844a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 845a8c2190eSArnaldo Carvalho de Melo goto next_listen; 846a8c2190eSArnaldo Carvalho de Melo 847a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_LISTEN) || 848a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport || 849a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 850a8c2190eSArnaldo Carvalho de Melo goto syn_recv; 851a8c2190eSArnaldo Carvalho de Melo 85225c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 8535caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 854a8c2190eSArnaldo Carvalho de Melo goto done; 855a8c2190eSArnaldo Carvalho de Melo } 856a8c2190eSArnaldo Carvalho de Melo 857a8c2190eSArnaldo Carvalho de Melo syn_recv: 858a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_SYN_RECV)) 859a8c2190eSArnaldo Carvalho de Melo goto next_listen; 860a8c2190eSArnaldo Carvalho de Melo 86125c4cd2bSPavel Emelyanov if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { 8625caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 863a8c2190eSArnaldo Carvalho de Melo goto done; 864a8c2190eSArnaldo Carvalho de Melo } 865a8c2190eSArnaldo Carvalho de Melo 866a8c2190eSArnaldo Carvalho de Melo next_listen: 867a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 868a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 869a8c2190eSArnaldo Carvalho de Melo ++num; 870a8c2190eSArnaldo Carvalho de Melo } 8715caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 872a8c2190eSArnaldo Carvalho de Melo 873a8c2190eSArnaldo Carvalho de Melo s_num = 0; 874a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 875a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 876a8c2190eSArnaldo Carvalho de Melo } 877a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 878a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 879a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 880a8c2190eSArnaldo Carvalho de Melo } 881a8c2190eSArnaldo Carvalho de Melo 882a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 883efb3cb42SPavel Emelyanov goto out; 884a8c2190eSArnaldo Carvalho de Melo 885f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 886a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 8877e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 8883ab5aee7SEric Dumazet struct hlist_nulls_node *node; 889e31c5e0eSEric Dumazet struct sock *sk; 890a8c2190eSArnaldo Carvalho de Melo 8916be547a6SAndi Kleen num = 0; 8926be547a6SAndi Kleen 89305dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 8946be547a6SAndi Kleen continue; 8956be547a6SAndi Kleen 896a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 897a8c2190eSArnaldo Carvalho de Melo s_num = 0; 898a8c2190eSArnaldo Carvalho de Melo 8997e3aab4aSDavid S. Miller spin_lock_bh(lock); 9003ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 901e31c5e0eSEric Dumazet int state, res; 902a8c2190eSArnaldo Carvalho de Melo 90351d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 90451d7cccfSAndrey Vagin continue; 905a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 906a8c2190eSArnaldo Carvalho de Melo goto next_normal; 90770315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 90870315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 90970315d22SNeal Cardwell if (!(r->idiag_states & (1 << state))) 910a8c2190eSArnaldo Carvalho de Melo goto next_normal; 911d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 912d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 913d23deaa0SPavel Emelyanov goto next_normal; 91405dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 915a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 916a8c2190eSArnaldo Carvalho de Melo goto next_normal; 91705dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 9184e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 919a8c2190eSArnaldo Carvalho de Melo goto next_normal; 920a58917f5SEric Dumazet twsk_build_assert(); 921a58917f5SEric Dumazet 922a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 923a58917f5SEric Dumazet goto next_normal; 924a58917f5SEric Dumazet 925a58917f5SEric Dumazet res = sk_diag_fill(sk, skb, r, 926a58917f5SEric Dumazet sk_user_ns(NETLINK_CB(cb->skb).sk), 927a58917f5SEric Dumazet NETLINK_CB(cb->skb).portid, 928a58917f5SEric Dumazet cb->nlh->nlmsg_seq, NLM_F_MULTI, 929a58917f5SEric Dumazet cb->nlh); 93005dbc7b5SEric Dumazet if (res < 0) { 9317e3aab4aSDavid S. Miller spin_unlock_bh(lock); 932a8c2190eSArnaldo Carvalho de Melo goto done; 933a8c2190eSArnaldo Carvalho de Melo } 934a8c2190eSArnaldo Carvalho de Melo next_normal: 935a8c2190eSArnaldo Carvalho de Melo ++num; 936a8c2190eSArnaldo Carvalho de Melo } 937a8c2190eSArnaldo Carvalho de Melo 9387e3aab4aSDavid S. Miller spin_unlock_bh(lock); 939a8c2190eSArnaldo Carvalho de Melo } 940a8c2190eSArnaldo Carvalho de Melo 941a8c2190eSArnaldo Carvalho de Melo done: 942a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 943a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 944efb3cb42SPavel Emelyanov out: 945efb3cb42SPavel Emelyanov ; 946efb3cb42SPavel Emelyanov } 9471942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 948efb3cb42SPavel Emelyanov 949efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 95034160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 951e31c5e0eSEric Dumazet struct nlattr *bc) 952efb3cb42SPavel Emelyanov { 953efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 954cacb6ba0SCyrill Gorcunov int err = 0; 955efb3cb42SPavel Emelyanov 956efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 957efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 9581942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 959cacb6ba0SCyrill Gorcunov else 960cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 961d523a328SHerbert Xu inet_diag_unlock_handler(handler); 962efb3cb42SPavel Emelyanov 963cacb6ba0SCyrill Gorcunov return err ? : skb->len; 964a8c2190eSArnaldo Carvalho de Melo } 965a8c2190eSArnaldo Carvalho de Melo 96625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 96725c4cd2bSPavel Emelyanov { 968c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 969e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 97025c4cd2bSPavel Emelyanov 97125c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 97225c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 97325c4cd2bSPavel Emelyanov 974d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 97525c4cd2bSPavel Emelyanov } 97625c4cd2bSPavel Emelyanov 977e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 978a029fe26SPavel Emelyanov { 979a029fe26SPavel Emelyanov switch (type) { 980a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 981a029fe26SPavel Emelyanov return IPPROTO_TCP; 982a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 983a029fe26SPavel Emelyanov return IPPROTO_DCCP; 984a029fe26SPavel Emelyanov default: 985a029fe26SPavel Emelyanov return 0; 986a029fe26SPavel Emelyanov } 987a029fe26SPavel Emelyanov } 988a029fe26SPavel Emelyanov 989e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 990e31c5e0eSEric Dumazet struct netlink_callback *cb) 99125c4cd2bSPavel Emelyanov { 992d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 993e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 994c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 99525c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 99625c4cd2bSPavel Emelyanov 997d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 99825c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 99925c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 100025c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 100125c4cd2bSPavel Emelyanov req.id = rc->id; 100225c4cd2bSPavel Emelyanov 100325c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 100425c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 100525c4cd2bSPavel Emelyanov 100625c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 100725c4cd2bSPavel Emelyanov } 100825c4cd2bSPavel Emelyanov 1009fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1010fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1011fe50ce28SPavel Emelyanov { 1012d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1013c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1014fe50ce28SPavel Emelyanov 1015fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1016fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1017fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1018fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1019fe50ce28SPavel Emelyanov req.id = rc->id; 1020fe50ce28SPavel Emelyanov 1021fe50ce28SPavel Emelyanov return inet_diag_get_exact(in_skb, nlh, &req); 1022fe50ce28SPavel Emelyanov } 1023fe50ce28SPavel Emelyanov 10248d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1025a8c2190eSArnaldo Carvalho de Melo { 10263b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 102751d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1028a8c2190eSArnaldo Carvalho de Melo 1029ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1030ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1031ead592baSThomas Graf return -EINVAL; 1032a8c2190eSArnaldo Carvalho de Melo 1033b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 1034ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 1035ead592baSThomas Graf struct nlattr *attr; 1036ead592baSThomas Graf 1037ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 1038ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 1039e31c5e0eSEric Dumazet if (!attr || 1040ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 1041ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 1042a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 1043a8c2190eSArnaldo Carvalho de Melo } 104480d326faSPablo Neira Ayuso { 104580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 104680d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 104780d326faSPablo Neira Ayuso }; 104851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 104980d326faSPablo Neira Ayuso } 1050a8c2190eSArnaldo Carvalho de Melo } 1051ead592baSThomas Graf 1052fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1053a8c2190eSArnaldo Carvalho de Melo } 1054a8c2190eSArnaldo Carvalho de Melo 1055d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 1056d366477aSPavel Emelyanov { 1057c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 105851d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1059d366477aSPavel Emelyanov 1060d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1061d366477aSPavel Emelyanov return -EINVAL; 1062d366477aSPavel Emelyanov 1063d366477aSPavel Emelyanov if (h->nlmsg_flags & NLM_F_DUMP) { 106425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 106525c4cd2bSPavel Emelyanov struct nlattr *attr; 1066e31c5e0eSEric Dumazet 106725c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 106825c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 1069e31c5e0eSEric Dumazet if (!attr || 107025c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 107125c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 107225c4cd2bSPavel Emelyanov return -EINVAL; 107325c4cd2bSPavel Emelyanov } 107480d326faSPablo Neira Ayuso { 107580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 107680d326faSPablo Neira Ayuso .dump = inet_diag_dump, 107780d326faSPablo Neira Ayuso }; 107851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 107980d326faSPablo Neira Ayuso } 1080d366477aSPavel Emelyanov } 1081d366477aSPavel Emelyanov 1082d106352dSDavid S. Miller return inet_diag_get_exact(skb, h, nlmsg_data(h)); 1083d366477aSPavel Emelyanov } 1084d366477aSPavel Emelyanov 108535ac838aSCraig Gallek static 108635ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 108735ac838aSCraig Gallek { 108835ac838aSCraig Gallek const struct inet_diag_handler *handler; 108935ac838aSCraig Gallek struct nlmsghdr *nlh; 109035ac838aSCraig Gallek struct nlattr *attr; 109135ac838aSCraig Gallek struct inet_diag_msg *r; 109235ac838aSCraig Gallek void *info = NULL; 109335ac838aSCraig Gallek int err = 0; 109435ac838aSCraig Gallek 109535ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 109635ac838aSCraig Gallek if (!nlh) 109735ac838aSCraig Gallek return -ENOMEM; 109835ac838aSCraig Gallek 109935ac838aSCraig Gallek r = nlmsg_data(nlh); 110035ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 110135ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1102e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1103e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 110435ac838aSCraig Gallek r->idiag_state = sk->sk_state; 110535ac838aSCraig Gallek 110635ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 110735ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 110835ac838aSCraig Gallek return err; 110935ac838aSCraig Gallek } 111035ac838aSCraig Gallek 111135ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 111235ac838aSCraig Gallek if (IS_ERR(handler)) { 111335ac838aSCraig Gallek inet_diag_unlock_handler(handler); 111435ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 111535ac838aSCraig Gallek return PTR_ERR(handler); 111635ac838aSCraig Gallek } 111735ac838aSCraig Gallek 111835ac838aSCraig Gallek attr = handler->idiag_info_size 111935ac838aSCraig Gallek ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size) 112035ac838aSCraig Gallek : NULL; 112135ac838aSCraig Gallek if (attr) 112235ac838aSCraig Gallek info = nla_data(attr); 112335ac838aSCraig Gallek 112435ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 112535ac838aSCraig Gallek inet_diag_unlock_handler(handler); 112635ac838aSCraig Gallek 112735ac838aSCraig Gallek nlmsg_end(skb, nlh); 112835ac838aSCraig Gallek return 0; 112935ac838aSCraig Gallek } 113035ac838aSCraig Gallek 11318dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1132d366477aSPavel Emelyanov .family = AF_INET, 1133d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 113435ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 1135d366477aSPavel Emelyanov }; 1136d366477aSPavel Emelyanov 11378dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1138d366477aSPavel Emelyanov .family = AF_INET6, 1139d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 114035ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 1141d366477aSPavel Emelyanov }; 1142d366477aSPavel Emelyanov 1143a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1144a8c2190eSArnaldo Carvalho de Melo { 1145a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1146a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1147a8c2190eSArnaldo Carvalho de Melo 1148f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1149a8c2190eSArnaldo Carvalho de Melo goto out; 1150a8c2190eSArnaldo Carvalho de Melo 1151d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1152a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1153e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1154a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1155a8c2190eSArnaldo Carvalho de Melo err = 0; 1156a8c2190eSArnaldo Carvalho de Melo } 1157d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1158a8c2190eSArnaldo Carvalho de Melo out: 1159a8c2190eSArnaldo Carvalho de Melo return err; 1160a8c2190eSArnaldo Carvalho de Melo } 1161a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1162a8c2190eSArnaldo Carvalho de Melo 1163a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1164a8c2190eSArnaldo Carvalho de Melo { 1165a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1166a8c2190eSArnaldo Carvalho de Melo 1167f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1168a8c2190eSArnaldo Carvalho de Melo return; 1169a8c2190eSArnaldo Carvalho de Melo 1170d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1171a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1172d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1173a8c2190eSArnaldo Carvalho de Melo } 1174a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1175a8c2190eSArnaldo Carvalho de Melo 1176a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1177a8c2190eSArnaldo Carvalho de Melo { 1178f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1179a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1180a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1181a8c2190eSArnaldo Carvalho de Melo 11820da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1183a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1184a8c2190eSArnaldo Carvalho de Melo goto out; 1185a8c2190eSArnaldo Carvalho de Melo 1186d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1187d366477aSPavel Emelyanov if (err) 1188d366477aSPavel Emelyanov goto out_free_nl; 1189d366477aSPavel Emelyanov 1190d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1191d366477aSPavel Emelyanov if (err) 1192d366477aSPavel Emelyanov goto out_free_inet; 1193d366477aSPavel Emelyanov 11948ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1195a8c2190eSArnaldo Carvalho de Melo out: 1196a8c2190eSArnaldo Carvalho de Melo return err; 1197d366477aSPavel Emelyanov 1198d366477aSPavel Emelyanov out_free_inet: 1199d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1200d366477aSPavel Emelyanov out_free_nl: 1201a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1202a8c2190eSArnaldo Carvalho de Melo goto out; 1203a8c2190eSArnaldo Carvalho de Melo } 1204a8c2190eSArnaldo Carvalho de Melo 1205a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1206a8c2190eSArnaldo Carvalho de Melo { 1207d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1208d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 12098ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1210a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1211a8c2190eSArnaldo Carvalho de Melo } 1212a8c2190eSArnaldo Carvalho de Melo 1213a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1214a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1215a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1216aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1217aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1218