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 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 7331942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 734e31c5e0eSEric Dumazet struct netlink_callback *cb, 73534160ea3SEric Dumazet const struct inet_diag_req_v2 *r, struct nlattr *bc) 736a8c2190eSArnaldo Carvalho de Melo { 73751d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 738e31c5e0eSEric Dumazet int i, num, s_i, s_num; 739*079096f1SEric Dumazet u32 idiag_states = r->idiag_states; 740a8c2190eSArnaldo Carvalho de Melo 741*079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 742*079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 743a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 744a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 745a8c2190eSArnaldo Carvalho de Melo 746a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 747*079096f1SEric Dumazet if (!(idiag_states & TCPF_LISTEN)) 748a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 749a8c2190eSArnaldo Carvalho de Melo 750a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 7515caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 752e31c5e0eSEric Dumazet struct hlist_nulls_node *node; 753e31c5e0eSEric Dumazet struct sock *sk; 754a8c2190eSArnaldo Carvalho de Melo 755a8c2190eSArnaldo Carvalho de Melo num = 0; 7565caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 7575caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 758c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 759a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 760a8c2190eSArnaldo Carvalho de Melo 76151d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 76251d7cccfSAndrey Vagin continue; 76351d7cccfSAndrey Vagin 764a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 765a8c2190eSArnaldo Carvalho de Melo num++; 766a8c2190eSArnaldo Carvalho de Melo continue; 767a8c2190eSArnaldo Carvalho de Melo } 768a8c2190eSArnaldo Carvalho de Melo 769d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 770d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 771d23deaa0SPavel Emelyanov goto next_listen; 772d23deaa0SPavel Emelyanov 773c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 774a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 775a8c2190eSArnaldo Carvalho de Melo goto next_listen; 776a8c2190eSArnaldo Carvalho de Melo 777*079096f1SEric Dumazet if (r->id.idiag_dport || 778a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 779a8c2190eSArnaldo Carvalho de Melo goto next_listen; 780a8c2190eSArnaldo Carvalho de Melo 781*079096f1SEric Dumazet if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 7825caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 783a8c2190eSArnaldo Carvalho de Melo goto done; 784a8c2190eSArnaldo Carvalho de Melo } 785a8c2190eSArnaldo Carvalho de Melo 786a8c2190eSArnaldo Carvalho de Melo next_listen: 787a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 788a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 789a8c2190eSArnaldo Carvalho de Melo ++num; 790a8c2190eSArnaldo Carvalho de Melo } 7915caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 792a8c2190eSArnaldo Carvalho de Melo 793a8c2190eSArnaldo Carvalho de Melo s_num = 0; 794a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 795a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 796a8c2190eSArnaldo Carvalho de Melo } 797a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 798a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 799a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 800a8c2190eSArnaldo Carvalho de Melo } 801a8c2190eSArnaldo Carvalho de Melo 802*079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 803efb3cb42SPavel Emelyanov goto out; 804a8c2190eSArnaldo Carvalho de Melo 805f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 806a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 8077e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 8083ab5aee7SEric Dumazet struct hlist_nulls_node *node; 809e31c5e0eSEric Dumazet struct sock *sk; 810a8c2190eSArnaldo Carvalho de Melo 8116be547a6SAndi Kleen num = 0; 8126be547a6SAndi Kleen 81305dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 8146be547a6SAndi Kleen continue; 8156be547a6SAndi Kleen 816a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 817a8c2190eSArnaldo Carvalho de Melo s_num = 0; 818a8c2190eSArnaldo Carvalho de Melo 8197e3aab4aSDavid S. Miller spin_lock_bh(lock); 8203ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 821e31c5e0eSEric Dumazet int state, res; 822a8c2190eSArnaldo Carvalho de Melo 82351d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 82451d7cccfSAndrey Vagin continue; 825a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 826a8c2190eSArnaldo Carvalho de Melo goto next_normal; 82770315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 82870315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 829*079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 830a8c2190eSArnaldo Carvalho de Melo goto next_normal; 831d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 832d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 833d23deaa0SPavel Emelyanov goto next_normal; 83405dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 835a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 836a8c2190eSArnaldo Carvalho de Melo goto next_normal; 83705dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 8384e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 839a8c2190eSArnaldo Carvalho de Melo goto next_normal; 840a58917f5SEric Dumazet twsk_build_assert(); 841a58917f5SEric Dumazet 842a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 843a58917f5SEric Dumazet goto next_normal; 844a58917f5SEric Dumazet 845a58917f5SEric Dumazet res = sk_diag_fill(sk, skb, r, 846a58917f5SEric Dumazet sk_user_ns(NETLINK_CB(cb->skb).sk), 847a58917f5SEric Dumazet NETLINK_CB(cb->skb).portid, 848a58917f5SEric Dumazet cb->nlh->nlmsg_seq, NLM_F_MULTI, 849a58917f5SEric Dumazet cb->nlh); 85005dbc7b5SEric Dumazet if (res < 0) { 8517e3aab4aSDavid S. Miller spin_unlock_bh(lock); 852a8c2190eSArnaldo Carvalho de Melo goto done; 853a8c2190eSArnaldo Carvalho de Melo } 854a8c2190eSArnaldo Carvalho de Melo next_normal: 855a8c2190eSArnaldo Carvalho de Melo ++num; 856a8c2190eSArnaldo Carvalho de Melo } 857a8c2190eSArnaldo Carvalho de Melo 8587e3aab4aSDavid S. Miller spin_unlock_bh(lock); 859a8c2190eSArnaldo Carvalho de Melo } 860a8c2190eSArnaldo Carvalho de Melo 861a8c2190eSArnaldo Carvalho de Melo done: 862a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 863a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 864efb3cb42SPavel Emelyanov out: 865efb3cb42SPavel Emelyanov ; 866efb3cb42SPavel Emelyanov } 8671942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 868efb3cb42SPavel Emelyanov 869efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 87034160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 871e31c5e0eSEric Dumazet struct nlattr *bc) 872efb3cb42SPavel Emelyanov { 873efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 874cacb6ba0SCyrill Gorcunov int err = 0; 875efb3cb42SPavel Emelyanov 876efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 877efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 8781942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 879cacb6ba0SCyrill Gorcunov else 880cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 881d523a328SHerbert Xu inet_diag_unlock_handler(handler); 882efb3cb42SPavel Emelyanov 883cacb6ba0SCyrill Gorcunov return err ? : skb->len; 884a8c2190eSArnaldo Carvalho de Melo } 885a8c2190eSArnaldo Carvalho de Melo 88625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 88725c4cd2bSPavel Emelyanov { 888c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 889e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 89025c4cd2bSPavel Emelyanov 89125c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 89225c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 89325c4cd2bSPavel Emelyanov 894d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 89525c4cd2bSPavel Emelyanov } 89625c4cd2bSPavel Emelyanov 897e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 898a029fe26SPavel Emelyanov { 899a029fe26SPavel Emelyanov switch (type) { 900a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 901a029fe26SPavel Emelyanov return IPPROTO_TCP; 902a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 903a029fe26SPavel Emelyanov return IPPROTO_DCCP; 904a029fe26SPavel Emelyanov default: 905a029fe26SPavel Emelyanov return 0; 906a029fe26SPavel Emelyanov } 907a029fe26SPavel Emelyanov } 908a029fe26SPavel Emelyanov 909e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 910e31c5e0eSEric Dumazet struct netlink_callback *cb) 91125c4cd2bSPavel Emelyanov { 912d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 913e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 914c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 91525c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 91625c4cd2bSPavel Emelyanov 917d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 91825c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 91925c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 92025c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 92125c4cd2bSPavel Emelyanov req.id = rc->id; 92225c4cd2bSPavel Emelyanov 92325c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 92425c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 92525c4cd2bSPavel Emelyanov 92625c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 92725c4cd2bSPavel Emelyanov } 92825c4cd2bSPavel Emelyanov 929fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 930fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 931fe50ce28SPavel Emelyanov { 932d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 933c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 934fe50ce28SPavel Emelyanov 935fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 936fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 937fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 938fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 939fe50ce28SPavel Emelyanov req.id = rc->id; 940fe50ce28SPavel Emelyanov 941fe50ce28SPavel Emelyanov return inet_diag_get_exact(in_skb, nlh, &req); 942fe50ce28SPavel Emelyanov } 943fe50ce28SPavel Emelyanov 9448d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 945a8c2190eSArnaldo Carvalho de Melo { 9463b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 94751d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 948a8c2190eSArnaldo Carvalho de Melo 949ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 950ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 951ead592baSThomas Graf return -EINVAL; 952a8c2190eSArnaldo Carvalho de Melo 953b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 954ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 955ead592baSThomas Graf struct nlattr *attr; 956ead592baSThomas Graf 957ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 958ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 959e31c5e0eSEric Dumazet if (!attr || 960ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 961ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 962a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 963a8c2190eSArnaldo Carvalho de Melo } 96480d326faSPablo Neira Ayuso { 96580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 96680d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 96780d326faSPablo Neira Ayuso }; 96851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 96980d326faSPablo Neira Ayuso } 970a8c2190eSArnaldo Carvalho de Melo } 971ead592baSThomas Graf 972fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 973a8c2190eSArnaldo Carvalho de Melo } 974a8c2190eSArnaldo Carvalho de Melo 975d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 976d366477aSPavel Emelyanov { 977c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 97851d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 979d366477aSPavel Emelyanov 980d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 981d366477aSPavel Emelyanov return -EINVAL; 982d366477aSPavel Emelyanov 983d366477aSPavel Emelyanov if (h->nlmsg_flags & NLM_F_DUMP) { 98425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 98525c4cd2bSPavel Emelyanov struct nlattr *attr; 986e31c5e0eSEric Dumazet 98725c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 98825c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 989e31c5e0eSEric Dumazet if (!attr || 99025c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 99125c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 99225c4cd2bSPavel Emelyanov return -EINVAL; 99325c4cd2bSPavel Emelyanov } 99480d326faSPablo Neira Ayuso { 99580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 99680d326faSPablo Neira Ayuso .dump = inet_diag_dump, 99780d326faSPablo Neira Ayuso }; 99851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 99980d326faSPablo Neira Ayuso } 1000d366477aSPavel Emelyanov } 1001d366477aSPavel Emelyanov 1002d106352dSDavid S. Miller return inet_diag_get_exact(skb, h, nlmsg_data(h)); 1003d366477aSPavel Emelyanov } 1004d366477aSPavel Emelyanov 100535ac838aSCraig Gallek static 100635ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 100735ac838aSCraig Gallek { 100835ac838aSCraig Gallek const struct inet_diag_handler *handler; 100935ac838aSCraig Gallek struct nlmsghdr *nlh; 101035ac838aSCraig Gallek struct nlattr *attr; 101135ac838aSCraig Gallek struct inet_diag_msg *r; 101235ac838aSCraig Gallek void *info = NULL; 101335ac838aSCraig Gallek int err = 0; 101435ac838aSCraig Gallek 101535ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 101635ac838aSCraig Gallek if (!nlh) 101735ac838aSCraig Gallek return -ENOMEM; 101835ac838aSCraig Gallek 101935ac838aSCraig Gallek r = nlmsg_data(nlh); 102035ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 102135ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1022e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1023e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 102435ac838aSCraig Gallek r->idiag_state = sk->sk_state; 102535ac838aSCraig Gallek 102635ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 102735ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 102835ac838aSCraig Gallek return err; 102935ac838aSCraig Gallek } 103035ac838aSCraig Gallek 103135ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 103235ac838aSCraig Gallek if (IS_ERR(handler)) { 103335ac838aSCraig Gallek inet_diag_unlock_handler(handler); 103435ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 103535ac838aSCraig Gallek return PTR_ERR(handler); 103635ac838aSCraig Gallek } 103735ac838aSCraig Gallek 103835ac838aSCraig Gallek attr = handler->idiag_info_size 103935ac838aSCraig Gallek ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size) 104035ac838aSCraig Gallek : NULL; 104135ac838aSCraig Gallek if (attr) 104235ac838aSCraig Gallek info = nla_data(attr); 104335ac838aSCraig Gallek 104435ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 104535ac838aSCraig Gallek inet_diag_unlock_handler(handler); 104635ac838aSCraig Gallek 104735ac838aSCraig Gallek nlmsg_end(skb, nlh); 104835ac838aSCraig Gallek return 0; 104935ac838aSCraig Gallek } 105035ac838aSCraig Gallek 10518dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1052d366477aSPavel Emelyanov .family = AF_INET, 1053d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 105435ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 1055d366477aSPavel Emelyanov }; 1056d366477aSPavel Emelyanov 10578dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1058d366477aSPavel Emelyanov .family = AF_INET6, 1059d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 106035ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 1061d366477aSPavel Emelyanov }; 1062d366477aSPavel Emelyanov 1063a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1064a8c2190eSArnaldo Carvalho de Melo { 1065a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1066a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1067a8c2190eSArnaldo Carvalho de Melo 1068f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1069a8c2190eSArnaldo Carvalho de Melo goto out; 1070a8c2190eSArnaldo Carvalho de Melo 1071d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1072a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1073e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1074a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1075a8c2190eSArnaldo Carvalho de Melo err = 0; 1076a8c2190eSArnaldo Carvalho de Melo } 1077d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1078a8c2190eSArnaldo Carvalho de Melo out: 1079a8c2190eSArnaldo Carvalho de Melo return err; 1080a8c2190eSArnaldo Carvalho de Melo } 1081a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1082a8c2190eSArnaldo Carvalho de Melo 1083a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1084a8c2190eSArnaldo Carvalho de Melo { 1085a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1086a8c2190eSArnaldo Carvalho de Melo 1087f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1088a8c2190eSArnaldo Carvalho de Melo return; 1089a8c2190eSArnaldo Carvalho de Melo 1090d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1091a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1092d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1093a8c2190eSArnaldo Carvalho de Melo } 1094a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1095a8c2190eSArnaldo Carvalho de Melo 1096a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1097a8c2190eSArnaldo Carvalho de Melo { 1098f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1099a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1100a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1101a8c2190eSArnaldo Carvalho de Melo 11020da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1103a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1104a8c2190eSArnaldo Carvalho de Melo goto out; 1105a8c2190eSArnaldo Carvalho de Melo 1106d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1107d366477aSPavel Emelyanov if (err) 1108d366477aSPavel Emelyanov goto out_free_nl; 1109d366477aSPavel Emelyanov 1110d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1111d366477aSPavel Emelyanov if (err) 1112d366477aSPavel Emelyanov goto out_free_inet; 1113d366477aSPavel Emelyanov 11148ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1115a8c2190eSArnaldo Carvalho de Melo out: 1116a8c2190eSArnaldo Carvalho de Melo return err; 1117d366477aSPavel Emelyanov 1118d366477aSPavel Emelyanov out_free_inet: 1119d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1120d366477aSPavel Emelyanov out_free_nl: 1121a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1122a8c2190eSArnaldo Carvalho de Melo goto out; 1123a8c2190eSArnaldo Carvalho de Melo } 1124a8c2190eSArnaldo Carvalho de Melo 1125a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1126a8c2190eSArnaldo Carvalho de Melo { 1127d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1128d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 11298ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1130a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1131a8c2190eSArnaldo Carvalho de Melo } 1132a8c2190eSArnaldo Carvalho de Melo 1133a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1134a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1135a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1136aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1137aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1138