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 { 41*e31c5e0eSEric Dumazet const __be32 *saddr; 42*e31c5e0eSEric 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; 471c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6) 481c95df85SNeal Cardwell struct in6_addr saddr_storage; /* for IPv4-mapped-IPv6 addresses */ 491c95df85SNeal Cardwell struct in6_addr daddr_storage; /* for IPv4-mapped-IPv6 addresses */ 501c95df85SNeal Cardwell #endif 51a8c2190eSArnaldo Carvalho de Melo }; 52a8c2190eSArnaldo Carvalho de Melo 53d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 54d523a328SHerbert Xu 55f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 56d523a328SHerbert Xu { 57f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 58aec8dc62SPavel Emelyanov request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK, 59aec8dc62SPavel Emelyanov NETLINK_SOCK_DIAG, AF_INET, proto); 60d523a328SHerbert Xu 61d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 62f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 63d523a328SHerbert Xu return ERR_PTR(-ENOENT); 64d523a328SHerbert Xu 65f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 66d523a328SHerbert Xu } 67d523a328SHerbert Xu 68*e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 69d523a328SHerbert Xu { 70d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 71d523a328SHerbert Xu } 72d523a328SHerbert Xu 733c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 74c8991362SPavel Emelyanov struct sk_buff *skb, struct inet_diag_req_v2 *req, 75d06ca956SEric W. Biederman struct user_namespace *user_ns, 7615e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 77a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 78a8c2190eSArnaldo Carvalho de Melo { 79a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 80*e31c5e0eSEric Dumazet const struct inet_diag_handler *handler; 81*e31c5e0eSEric Dumazet int ext = req->idiag_ext; 82a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 83a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 846e277ed5SThomas Graf struct nlattr *attr; 85a8c2190eSArnaldo Carvalho de Melo void *info = NULL; 86a8c2190eSArnaldo Carvalho de Melo 87a029fe26SPavel Emelyanov handler = inet_diag_table[req->sdiag_protocol]; 88*e31c5e0eSEric Dumazet BUG_ON(!handler); 89a8c2190eSArnaldo Carvalho de Melo 9015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 916e277ed5SThomas Graf nlmsg_flags); 926e277ed5SThomas Graf if (!nlh) 93d106352dSDavid S. Miller return -EMSGSIZE; 94a8c2190eSArnaldo Carvalho de Melo 95d106352dSDavid S. Miller r = nlmsg_data(nlh); 96c7d58aabSArnaldo Carvalho de Melo BUG_ON(sk->sk_state == TCP_TIME_WAIT); 97c7d58aabSArnaldo Carvalho de Melo 98a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 99a8c2190eSArnaldo Carvalho de Melo r->idiag_state = sk->sk_state; 100a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 101a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = 0; 102a8c2190eSArnaldo Carvalho de Melo 103a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 104f65c1b53SPavel Emelyanov sock_diag_save_cookie(sk, r->id.idiag_cookie); 105a8c2190eSArnaldo Carvalho de Melo 106c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 107c720c7e8SEric Dumazet r->id.idiag_dport = inet->inet_dport; 108b1aac815SDaniel Borkmann 109b1aac815SDaniel Borkmann memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 110b1aac815SDaniel Borkmann memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 111b1aac815SDaniel Borkmann 112c720c7e8SEric Dumazet r->id.idiag_src[0] = inet->inet_rcv_saddr; 113c720c7e8SEric Dumazet r->id.idiag_dst[0] = inet->inet_daddr; 114a8c2190eSArnaldo Carvalho de Melo 115e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 116e4e541a8SPavel Emelyanov goto errout; 117e4e541a8SPavel Emelyanov 118717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 119717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 120717b6d83SMaciej Żenczykowski */ 121717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 1226e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 1236e277ed5SThomas Graf goto errout; 124717b6d83SMaciej Żenczykowski 125dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 126a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 127efe4208fSEric Dumazet *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 128efe4208fSEric Dumazet *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 1296e277ed5SThomas Graf 13006236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 131efe4208fSEric Dumazet if (nla_put_u8(skb, INET_DIAG_TCLASS, 132efe4208fSEric Dumazet inet6_sk(sk)->tclass) < 0) 1336e277ed5SThomas Graf goto errout; 134a8c2190eSArnaldo Carvalho de Melo } 135a8c2190eSArnaldo Carvalho de Melo #endif 136a8c2190eSArnaldo Carvalho de Melo 137d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 1383c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1393c4d05c8SPavel Emelyanov 1406e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 1416e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 1426e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 1436e277ed5SThomas Graf .idiag_wmem = sk->sk_wmem_queued, 1446e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 1456e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 1466e277ed5SThomas Graf }; 1476e277ed5SThomas Graf 1486e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 1496e277ed5SThomas Graf goto errout; 1503c4d05c8SPavel Emelyanov } 1513c4d05c8SPavel Emelyanov 152c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 153c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 1546e277ed5SThomas Graf goto errout; 155c0636faaSPavel Emelyanov 156*e31c5e0eSEric Dumazet if (!icsk) { 15762ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 1583c4d05c8SPavel Emelyanov goto out; 1593c4d05c8SPavel Emelyanov } 1603c4d05c8SPavel Emelyanov 161172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 162a8c2190eSArnaldo Carvalho de Melo 1636ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 1646ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || 1656ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 166a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 167a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 168a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 169a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 170a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 171a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 172a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 173a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 174a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 175a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 176a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 177a8c2190eSArnaldo Carvalho de Melo } else { 178a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 179a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 180a8c2190eSArnaldo Carvalho de Melo } 181a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS 182a8c2190eSArnaldo Carvalho de Melo 1836e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_INFO - 1))) { 1846e277ed5SThomas Graf attr = nla_reserve(skb, INET_DIAG_INFO, 1856e277ed5SThomas Graf sizeof(struct tcp_info)); 1866e277ed5SThomas Graf if (!attr) 1876e277ed5SThomas Graf goto errout; 188a8c2190eSArnaldo Carvalho de Melo 1896e277ed5SThomas Graf info = nla_data(attr); 190a8c2190eSArnaldo Carvalho de Melo } 191a8c2190eSArnaldo Carvalho de Melo 1926e277ed5SThomas Graf if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) 1936e277ed5SThomas Graf if (nla_put_string(skb, INET_DIAG_CONG, 1946e277ed5SThomas Graf icsk->icsk_ca_ops->name) < 0) 1956e277ed5SThomas Graf goto errout; 1966e277ed5SThomas Graf 197a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 198a8c2190eSArnaldo Carvalho de Melo 199a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state < TCP_TIME_WAIT && 200a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 201a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->get_info(sk, ext, skb); 202a8c2190eSArnaldo Carvalho de Melo 2033c4d05c8SPavel Emelyanov out: 204053c095aSJohannes Berg nlmsg_end(skb, nlh); 205053c095aSJohannes Berg return 0; 206a8c2190eSArnaldo Carvalho de Melo 2076e277ed5SThomas Graf errout: 2086e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 20926932566SPatrick McHardy return -EMSGSIZE; 210a8c2190eSArnaldo Carvalho de Melo } 2113c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 2123c4d05c8SPavel Emelyanov 2133c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk, 214*e31c5e0eSEric Dumazet struct sk_buff *skb, 215*e31c5e0eSEric Dumazet struct inet_diag_req_v2 *req, 216d06ca956SEric W. Biederman struct user_namespace *user_ns, 21715e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 2183c4d05c8SPavel Emelyanov const struct nlmsghdr *unlh) 2193c4d05c8SPavel Emelyanov { 220*e31c5e0eSEric Dumazet return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, 221*e31c5e0eSEric Dumazet user_ns, portid, seq, nlmsg_flags, unlh); 2223c4d05c8SPavel Emelyanov } 223a8c2190eSArnaldo Carvalho de Melo 224c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 225*e31c5e0eSEric Dumazet struct sk_buff *skb, 226*e31c5e0eSEric Dumazet struct inet_diag_req_v2 *req, 22715e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 228c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 229c7d58aabSArnaldo Carvalho de Melo { 230c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 2316e277ed5SThomas Graf struct nlmsghdr *nlh; 232*e31c5e0eSEric Dumazet s32 tmo; 233c7d58aabSArnaldo Carvalho de Melo 23415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 2356e277ed5SThomas Graf nlmsg_flags); 2366e277ed5SThomas Graf if (!nlh) 237d106352dSDavid S. Miller return -EMSGSIZE; 238d106352dSDavid S. Miller 239d106352dSDavid S. Miller r = nlmsg_data(nlh); 240c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 241c7d58aabSArnaldo Carvalho de Melo 24296f817feSEric Dumazet tmo = tw->tw_ttd - inet_tw_time_stamp(); 243c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 244c7d58aabSArnaldo Carvalho de Melo tmo = 0; 245c7d58aabSArnaldo Carvalho de Melo 246c7d58aabSArnaldo Carvalho de Melo r->idiag_family = tw->tw_family; 247c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 248b1aac815SDaniel Borkmann 249c7d58aabSArnaldo Carvalho de Melo r->id.idiag_if = tw->tw_bound_dev_if; 250f65c1b53SPavel Emelyanov sock_diag_save_cookie(tw, r->id.idiag_cookie); 251b1aac815SDaniel Borkmann 252c7d58aabSArnaldo Carvalho de Melo r->id.idiag_sport = tw->tw_sport; 253c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dport = tw->tw_dport; 254b1aac815SDaniel Borkmann 255b1aac815SDaniel Borkmann memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 256b1aac815SDaniel Borkmann memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 257b1aac815SDaniel Borkmann 258c7d58aabSArnaldo Carvalho de Melo r->id.idiag_src[0] = tw->tw_rcv_saddr; 259c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dst[0] = tw->tw_daddr; 260b1aac815SDaniel Borkmann 261c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 262c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 26396f817feSEric Dumazet r->idiag_expires = jiffies_to_msecs(tmo); 264c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 265c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 266c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 267c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 268dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 269c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 270efe4208fSEric Dumazet *(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr; 271efe4208fSEric Dumazet *(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr; 272c7d58aabSArnaldo Carvalho de Melo } 273c7d58aabSArnaldo Carvalho de Melo #endif 2746e277ed5SThomas Graf 275053c095aSJohannes Berg nlmsg_end(skb, nlh); 276053c095aSJohannes Berg return 0; 277c7d58aabSArnaldo Carvalho de Melo } 278c7d58aabSArnaldo Carvalho de Melo 279dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 280d06ca956SEric W. Biederman struct inet_diag_req_v2 *r, 281d06ca956SEric W. Biederman struct user_namespace *user_ns, 28215e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 283dff2c035SArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 284dff2c035SArnaldo Carvalho de Melo { 285dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 286efe4208fSEric Dumazet return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq, 287efe4208fSEric Dumazet nlmsg_flags, unlh); 288efe4208fSEric Dumazet 289efe4208fSEric Dumazet return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, 290efe4208fSEric Dumazet nlmsg_flags, unlh); 291dff2c035SArnaldo Carvalho de Melo } 292dff2c035SArnaldo Carvalho de Melo 293*e31c5e0eSEric Dumazet int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 294*e31c5e0eSEric Dumazet struct sk_buff *in_skb, 295*e31c5e0eSEric Dumazet const struct nlmsghdr *nlh, 296*e31c5e0eSEric Dumazet struct inet_diag_req_v2 *req) 297a8c2190eSArnaldo Carvalho de Melo { 29851d7cccfSAndrey Vagin struct net *net = sock_net(in_skb->sk); 299*e31c5e0eSEric Dumazet struct sk_buff *rep; 300*e31c5e0eSEric Dumazet struct sock *sk; 301*e31c5e0eSEric Dumazet int err; 302a8c2190eSArnaldo Carvalho de Melo 303d523a328SHerbert Xu err = -EINVAL; 304*e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 30551d7cccfSAndrey Vagin sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0], 306a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 307a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 308dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 309*e31c5e0eSEric Dumazet else if (req->sdiag_family == AF_INET6) 31051d7cccfSAndrey Vagin sk = inet6_lookup(net, hashinfo, 311a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 312a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 313a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 314a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 315a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 316a8c2190eSArnaldo Carvalho de Melo #endif 317*e31c5e0eSEric Dumazet else 318476f7dbfSPavel Emelyanov goto out_nosk; 319a8c2190eSArnaldo Carvalho de Melo 320d523a328SHerbert Xu err = -ENOENT; 321*e31c5e0eSEric Dumazet if (!sk) 322476f7dbfSPavel Emelyanov goto out_nosk; 323a8c2190eSArnaldo Carvalho de Melo 324f65c1b53SPavel Emelyanov err = sock_diag_check_cookie(sk, req->id.idiag_cookie); 325b005ab4eSPavel Emelyanov if (err) 326a8c2190eSArnaldo Carvalho de Melo goto out; 327a8c2190eSArnaldo Carvalho de Melo 3286e277ed5SThomas Graf rep = nlmsg_new(sizeof(struct inet_diag_msg) + 329a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_meminfo) + 3306e277ed5SThomas Graf sizeof(struct tcp_info) + 64, GFP_KERNEL); 3316e277ed5SThomas Graf if (!rep) { 3326e277ed5SThomas Graf err = -ENOMEM; 333a8c2190eSArnaldo Carvalho de Melo goto out; 3346e277ed5SThomas Graf } 335a8c2190eSArnaldo Carvalho de Melo 336a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 337e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(in_skb).sk), 33815e47304SEric W. Biederman NETLINK_CB(in_skb).portid, 33926932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 34026932566SPatrick McHardy if (err < 0) { 34126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 3426e277ed5SThomas Graf nlmsg_free(rep); 34326932566SPatrick McHardy goto out; 34426932566SPatrick McHardy } 34515e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 346a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 347a8c2190eSArnaldo Carvalho de Melo if (err > 0) 348a8c2190eSArnaldo Carvalho de Melo err = 0; 349a8c2190eSArnaldo Carvalho de Melo 350a8c2190eSArnaldo Carvalho de Melo out: 351c1d607ccSEric Dumazet if (sk) 352c1d607ccSEric Dumazet sock_gen_put(sk); 353c1d607ccSEric Dumazet 354476f7dbfSPavel Emelyanov out_nosk: 355476f7dbfSPavel Emelyanov return err; 356476f7dbfSPavel Emelyanov } 3571942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 358476f7dbfSPavel Emelyanov 359476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb, 360476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 361c8991362SPavel Emelyanov struct inet_diag_req_v2 *req) 362476f7dbfSPavel Emelyanov { 363476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 364476f7dbfSPavel Emelyanov int err; 365476f7dbfSPavel Emelyanov 366476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 367476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 368476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 369476f7dbfSPavel Emelyanov else 3701942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 371d523a328SHerbert Xu inet_diag_unlock_handler(handler); 372476f7dbfSPavel Emelyanov 373a8c2190eSArnaldo Carvalho de Melo return err; 374a8c2190eSArnaldo Carvalho de Melo } 375a8c2190eSArnaldo Carvalho de Melo 3769f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 377a8c2190eSArnaldo Carvalho de Melo { 378a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 379a8c2190eSArnaldo Carvalho de Melo 380a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 381a8c2190eSArnaldo Carvalho de Melo 382a8c2190eSArnaldo Carvalho de Melo if (words) { 383a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 384a8c2190eSArnaldo Carvalho de Melo return 0; 385a8c2190eSArnaldo Carvalho de Melo } 386a8c2190eSArnaldo Carvalho de Melo if (bits) { 3879f855299SAl Viro __be32 w1, w2; 3889f855299SAl Viro __be32 mask; 389a8c2190eSArnaldo Carvalho de Melo 390a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 391a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 392a8c2190eSArnaldo Carvalho de Melo 393a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 394a8c2190eSArnaldo Carvalho de Melo 395a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 396a8c2190eSArnaldo Carvalho de Melo return 0; 397a8c2190eSArnaldo Carvalho de Melo } 398a8c2190eSArnaldo Carvalho de Melo 399a8c2190eSArnaldo Carvalho de Melo return 1; 400a8c2190eSArnaldo Carvalho de Melo } 401a8c2190eSArnaldo Carvalho de Melo 40287c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 403a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 404a8c2190eSArnaldo Carvalho de Melo { 40587c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 40687c22ea5SPavel Emelyanov int len = nla_len(_bc); 40787c22ea5SPavel Emelyanov 408a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 409a8c2190eSArnaldo Carvalho de Melo int yes = 1; 410a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 411a8c2190eSArnaldo Carvalho de Melo 412a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 413a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 414a8c2190eSArnaldo Carvalho de Melo break; 415a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 416a8c2190eSArnaldo Carvalho de Melo yes = 0; 417a8c2190eSArnaldo Carvalho de Melo break; 418a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 419a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 420a8c2190eSArnaldo Carvalho de Melo break; 421a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 422b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 423a8c2190eSArnaldo Carvalho de Melo break; 424a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 425a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 426a8c2190eSArnaldo Carvalho de Melo break; 427a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 428a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 429a8c2190eSArnaldo Carvalho de Melo break; 430a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 431a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 432a8c2190eSArnaldo Carvalho de Melo break; 433a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 434a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 435*e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 436*e31c5e0eSEric Dumazet const __be32 *addr; 437a8c2190eSArnaldo Carvalho de Melo 438*e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 439a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 440a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 441a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 442a8c2190eSArnaldo Carvalho de Melo yes = 0; 443a8c2190eSArnaldo Carvalho de Melo break; 444a8c2190eSArnaldo Carvalho de Melo } 445a8c2190eSArnaldo Carvalho de Melo 446a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 447a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 448a8c2190eSArnaldo Carvalho de Melo else 449a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 450a8c2190eSArnaldo Carvalho de Melo 451f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 452f67caec9SNeal Cardwell cond->family != entry->family) { 453a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 454a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 455a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 456a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 457f67caec9SNeal Cardwell bitstring_match(addr + 3, 458f67caec9SNeal Cardwell cond->addr, 459a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 460a8c2190eSArnaldo Carvalho de Melo break; 461a8c2190eSArnaldo Carvalho de Melo } 462a8c2190eSArnaldo Carvalho de Melo yes = 0; 463a8c2190eSArnaldo Carvalho de Melo break; 464a8c2190eSArnaldo Carvalho de Melo } 465f67caec9SNeal Cardwell 466f67caec9SNeal Cardwell if (cond->prefix_len == 0) 467f67caec9SNeal Cardwell break; 468f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 469f67caec9SNeal Cardwell cond->prefix_len)) 470f67caec9SNeal Cardwell break; 471f67caec9SNeal Cardwell yes = 0; 472f67caec9SNeal Cardwell break; 473f67caec9SNeal Cardwell } 474a8c2190eSArnaldo Carvalho de Melo } 475a8c2190eSArnaldo Carvalho de Melo 476a8c2190eSArnaldo Carvalho de Melo if (yes) { 477a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 478a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 479a8c2190eSArnaldo Carvalho de Melo } else { 480a8c2190eSArnaldo Carvalho de Melo len -= op->no; 481a8c2190eSArnaldo Carvalho de Melo bc += op->no; 482a8c2190eSArnaldo Carvalho de Melo } 483a8c2190eSArnaldo Carvalho de Melo } 484a02cec21SEric Dumazet return len == 0; 485a8c2190eSArnaldo Carvalho de Melo } 486a8c2190eSArnaldo Carvalho de Melo 4878d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 4888d07d151SPavel Emelyanov { 4898d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 490*e31c5e0eSEric Dumazet struct inet_diag_entry entry; 4918d07d151SPavel Emelyanov 492*e31c5e0eSEric Dumazet if (!bc) 4938d07d151SPavel Emelyanov return 1; 4948d07d151SPavel Emelyanov 4958d07d151SPavel Emelyanov entry.family = sk->sk_family; 496dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4978d07d151SPavel Emelyanov if (entry.family == AF_INET6) { 498efe4208fSEric Dumazet entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32; 499efe4208fSEric Dumazet entry.daddr = sk->sk_v6_daddr.s6_addr32; 5008d07d151SPavel Emelyanov } else 5018d07d151SPavel Emelyanov #endif 5028d07d151SPavel Emelyanov { 5038d07d151SPavel Emelyanov entry.saddr = &inet->inet_rcv_saddr; 5048d07d151SPavel Emelyanov entry.daddr = &inet->inet_daddr; 5058d07d151SPavel Emelyanov } 5068d07d151SPavel Emelyanov entry.sport = inet->inet_num; 5078d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 50849612729SEric Dumazet entry.userlocks = (sk->sk_state != TCP_TIME_WAIT) ? sk->sk_userlocks : 0; 5098d07d151SPavel Emelyanov 5108d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 5118d07d151SPavel Emelyanov } 5128d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 5138d07d151SPavel Emelyanov 514a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 515a8c2190eSArnaldo Carvalho de Melo { 516a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 517a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 518a8c2190eSArnaldo Carvalho de Melo 519a8c2190eSArnaldo Carvalho de Melo if (cc > len) 520a8c2190eSArnaldo Carvalho de Melo return 0; 521a8c2190eSArnaldo Carvalho de Melo if (cc == len) 522a8c2190eSArnaldo Carvalho de Melo return 1; 523eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 524a8c2190eSArnaldo Carvalho de Melo return 0; 525a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 526a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 527a8c2190eSArnaldo Carvalho de Melo } 528a8c2190eSArnaldo Carvalho de Melo return 0; 529a8c2190eSArnaldo Carvalho de Melo } 530a8c2190eSArnaldo Carvalho de Melo 531405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 532405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 533405c0059SNeal Cardwell int *min_len) 534405c0059SNeal Cardwell { 535405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 536*e31c5e0eSEric Dumazet int addr_len; 537405c0059SNeal Cardwell 538405c0059SNeal Cardwell /* Check hostcond space. */ 539405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 540405c0059SNeal Cardwell if (len < *min_len) 541405c0059SNeal Cardwell return false; 542405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 543405c0059SNeal Cardwell 544405c0059SNeal Cardwell /* Check address family and address length. */ 545405c0059SNeal Cardwell switch (cond->family) { 546405c0059SNeal Cardwell case AF_UNSPEC: 547405c0059SNeal Cardwell addr_len = 0; 548405c0059SNeal Cardwell break; 549405c0059SNeal Cardwell case AF_INET: 550405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 551405c0059SNeal Cardwell break; 552405c0059SNeal Cardwell case AF_INET6: 553405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 554405c0059SNeal Cardwell break; 555405c0059SNeal Cardwell default: 556405c0059SNeal Cardwell return false; 557405c0059SNeal Cardwell } 558405c0059SNeal Cardwell *min_len += addr_len; 559405c0059SNeal Cardwell if (len < *min_len) 560405c0059SNeal Cardwell return false; 561405c0059SNeal Cardwell 562405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 563405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 564405c0059SNeal Cardwell return false; 565405c0059SNeal Cardwell 566405c0059SNeal Cardwell return true; 567405c0059SNeal Cardwell } 568405c0059SNeal Cardwell 5695e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 570*e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 5715e1f5420SNeal Cardwell int len, int *min_len) 5725e1f5420SNeal Cardwell { 5735e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 5745e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 5755e1f5420SNeal Cardwell if (len < *min_len) 5765e1f5420SNeal Cardwell return false; 5775e1f5420SNeal Cardwell return true; 5785e1f5420SNeal Cardwell } 5795e1f5420SNeal Cardwell 580a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 581a8c2190eSArnaldo Carvalho de Melo { 582eeb14972SEric Dumazet const void *bc = bytecode; 583a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 584a8c2190eSArnaldo Carvalho de Melo 585a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 586405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 587*e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 588a8c2190eSArnaldo Carvalho de Melo 589a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 590a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 591a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 592405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 593405c0059SNeal Cardwell return -EINVAL; 5945e1f5420SNeal Cardwell break; 595a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 596a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 597a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 598a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 5995e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 600a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 601a8c2190eSArnaldo Carvalho de Melo break; 6025e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 6035e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 604a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 605a8c2190eSArnaldo Carvalho de Melo break; 606a8c2190eSArnaldo Carvalho de Melo default: 607a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 608a8c2190eSArnaldo Carvalho de Melo } 6095e1f5420SNeal Cardwell 6105e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 6115e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 6125e1f5420SNeal Cardwell return -EINVAL; 6135e1f5420SNeal Cardwell if (op->no < len && 6145e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 6155e1f5420SNeal Cardwell return -EINVAL; 6165e1f5420SNeal Cardwell } 6175e1f5420SNeal Cardwell 618405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 619eeb14972SEric Dumazet return -EINVAL; 620a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 621a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 622a8c2190eSArnaldo Carvalho de Melo } 623a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 624a8c2190eSArnaldo Carvalho de Melo } 625a8c2190eSArnaldo Carvalho de Melo 626dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 627dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 62837f352b5SPavel Emelyanov struct netlink_callback *cb, 629c8991362SPavel Emelyanov struct inet_diag_req_v2 *r, 63037f352b5SPavel Emelyanov const struct nlattr *bc) 631a8c2190eSArnaldo Carvalho de Melo { 6328d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 633a8c2190eSArnaldo Carvalho de Melo return 0; 634a8c2190eSArnaldo Carvalho de Melo 635a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 636e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 63715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 638a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 639a8c2190eSArnaldo Carvalho de Melo } 640a8c2190eSArnaldo Carvalho de Melo 64149612729SEric Dumazet static void twsk_build_assert(void) 64249612729SEric Dumazet { 64349612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 64449612729SEric Dumazet offsetof(struct sock, sk_family)); 64549612729SEric Dumazet 64649612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 64749612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 64849612729SEric Dumazet 64949612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 65049612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 65149612729SEric Dumazet 65249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 65349612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 65449612729SEric Dumazet 65549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 65649612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 65749612729SEric Dumazet 65849612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 65949612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 66049612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 66149612729SEric Dumazet 66249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 66349612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 66449612729SEric Dumazet #endif 66549612729SEric Dumazet } 66649612729SEric Dumazet 66705dbc7b5SEric Dumazet static int inet_twsk_diag_dump(struct sock *sk, 668c7d58aabSArnaldo Carvalho de Melo struct sk_buff *skb, 66937f352b5SPavel Emelyanov struct netlink_callback *cb, 670c8991362SPavel Emelyanov struct inet_diag_req_v2 *r, 67137f352b5SPavel Emelyanov const struct nlattr *bc) 672c7d58aabSArnaldo Carvalho de Melo { 67349612729SEric Dumazet twsk_build_assert(); 67405dbc7b5SEric Dumazet 67549612729SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 676c7d58aabSArnaldo Carvalho de Melo return 0; 677c7d58aabSArnaldo Carvalho de Melo 67849612729SEric Dumazet return inet_twsk_diag_fill(inet_twsk(sk), skb, r, 67915e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 680c7d58aabSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 681c7d58aabSArnaldo Carvalho de Melo } 682c7d58aabSArnaldo Carvalho de Melo 6831c95df85SNeal Cardwell /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses 6841c95df85SNeal Cardwell * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6. 6851c95df85SNeal Cardwell */ 686*e31c5e0eSEric Dumazet static void inet_diag_req_addrs(const struct sock *sk, 6871c95df85SNeal Cardwell const struct request_sock *req, 6881c95df85SNeal Cardwell struct inet_diag_entry *entry) 6891c95df85SNeal Cardwell { 690*e31c5e0eSEric Dumazet const struct inet_request_sock *ireq = inet_rsk(req); 6911c95df85SNeal Cardwell 6921c95df85SNeal Cardwell #if IS_ENABLED(CONFIG_IPV6) 6931c95df85SNeal Cardwell if (sk->sk_family == AF_INET6) { 6941c95df85SNeal Cardwell if (req->rsk_ops->family == AF_INET6) { 695634fb979SEric Dumazet entry->saddr = ireq->ir_v6_loc_addr.s6_addr32; 696634fb979SEric Dumazet entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32; 6971c95df85SNeal Cardwell } else if (req->rsk_ops->family == AF_INET) { 698634fb979SEric Dumazet ipv6_addr_set_v4mapped(ireq->ir_loc_addr, 6991c95df85SNeal Cardwell &entry->saddr_storage); 700634fb979SEric Dumazet ipv6_addr_set_v4mapped(ireq->ir_rmt_addr, 7011c95df85SNeal Cardwell &entry->daddr_storage); 7021c95df85SNeal Cardwell entry->saddr = entry->saddr_storage.s6_addr32; 7031c95df85SNeal Cardwell entry->daddr = entry->daddr_storage.s6_addr32; 7041c95df85SNeal Cardwell } 7051c95df85SNeal Cardwell } else 7061c95df85SNeal Cardwell #endif 7071c95df85SNeal Cardwell { 708634fb979SEric Dumazet entry->saddr = &ireq->ir_loc_addr; 709634fb979SEric Dumazet entry->daddr = &ireq->ir_rmt_addr; 7101c95df85SNeal Cardwell } 7111c95df85SNeal Cardwell } 7121c95df85SNeal Cardwell 713a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, 714d06ca956SEric W. Biederman struct request_sock *req, 715d06ca956SEric W. Biederman struct user_namespace *user_ns, 71615e47304SEric W. Biederman u32 portid, u32 seq, 717a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 718a8c2190eSArnaldo Carvalho de Melo { 719a8c2190eSArnaldo Carvalho de Melo const struct inet_request_sock *ireq = inet_rsk(req); 720a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 721a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 722a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 723a8c2190eSArnaldo Carvalho de Melo long tmo; 724a8c2190eSArnaldo Carvalho de Melo 72515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 7266e277ed5SThomas Graf NLM_F_MULTI); 7276e277ed5SThomas Graf if (!nlh) 7286e277ed5SThomas Graf return -EMSGSIZE; 729a8c2190eSArnaldo Carvalho de Melo 7306e277ed5SThomas Graf r = nlmsg_data(nlh); 731a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 732a8c2190eSArnaldo Carvalho de Melo r->idiag_state = TCP_SYN_RECV; 733a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 734e6c022a4SEric Dumazet r->idiag_retrans = req->num_retrans; 735a8c2190eSArnaldo Carvalho de Melo 736a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 737f65c1b53SPavel Emelyanov sock_diag_save_cookie(req, r->id.idiag_cookie); 738a8c2190eSArnaldo Carvalho de Melo 739a8c2190eSArnaldo Carvalho de Melo tmo = req->expires - jiffies; 740a8c2190eSArnaldo Carvalho de Melo if (tmo < 0) 741a8c2190eSArnaldo Carvalho de Melo tmo = 0; 742a8c2190eSArnaldo Carvalho de Melo 743c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 744634fb979SEric Dumazet r->id.idiag_dport = ireq->ir_rmt_port; 745b1aac815SDaniel Borkmann 746b1aac815SDaniel Borkmann memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 747b1aac815SDaniel Borkmann memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 748b1aac815SDaniel Borkmann 749634fb979SEric Dumazet r->id.idiag_src[0] = ireq->ir_loc_addr; 750634fb979SEric Dumazet r->id.idiag_dst[0] = ireq->ir_rmt_addr; 751b1aac815SDaniel Borkmann 752a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = jiffies_to_msecs(tmo); 753a8c2190eSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 754a8c2190eSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 755d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 756a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = 0; 757dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 758a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 7591c95df85SNeal Cardwell struct inet_diag_entry entry; 760*e31c5e0eSEric Dumazet 7611c95df85SNeal Cardwell inet_diag_req_addrs(sk, req, &entry); 7621c95df85SNeal Cardwell memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr)); 7631c95df85SNeal Cardwell memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr)); 764a8c2190eSArnaldo Carvalho de Melo } 765a8c2190eSArnaldo Carvalho de Melo #endif 766a8c2190eSArnaldo Carvalho de Melo 767053c095aSJohannes Berg nlmsg_end(skb, nlh); 768053c095aSJohannes Berg return 0; 769a8c2190eSArnaldo Carvalho de Melo } 770a8c2190eSArnaldo Carvalho de Melo 771a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 77237f352b5SPavel Emelyanov struct netlink_callback *cb, 773c8991362SPavel Emelyanov struct inet_diag_req_v2 *r, 77437f352b5SPavel Emelyanov const struct nlattr *bc) 775a8c2190eSArnaldo Carvalho de Melo { 776a8c2190eSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 777a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 778*e31c5e0eSEric Dumazet struct inet_diag_entry entry; 779*e31c5e0eSEric Dumazet int j, s_j, reqnum, s_reqnum; 780*e31c5e0eSEric Dumazet struct listen_sock *lopt; 781a8c2190eSArnaldo Carvalho de Melo int err = 0; 782a8c2190eSArnaldo Carvalho de Melo 783a8c2190eSArnaldo Carvalho de Melo s_j = cb->args[3]; 784a8c2190eSArnaldo Carvalho de Melo s_reqnum = cb->args[4]; 785a8c2190eSArnaldo Carvalho de Melo 786a8c2190eSArnaldo Carvalho de Melo if (s_j > 0) 787a8c2190eSArnaldo Carvalho de Melo s_j--; 788a8c2190eSArnaldo Carvalho de Melo 789a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 790a8c2190eSArnaldo Carvalho de Melo 791a8c2190eSArnaldo Carvalho de Melo read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 792a8c2190eSArnaldo Carvalho de Melo 793a8c2190eSArnaldo Carvalho de Melo lopt = icsk->icsk_accept_queue.listen_opt; 794a8c2190eSArnaldo Carvalho de Melo if (!lopt || !lopt->qlen) 795a8c2190eSArnaldo Carvalho de Melo goto out; 796a8c2190eSArnaldo Carvalho de Melo 797*e31c5e0eSEric Dumazet if (bc) { 798c720c7e8SEric Dumazet entry.sport = inet->inet_num; 799a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 800a8c2190eSArnaldo Carvalho de Melo } 801a8c2190eSArnaldo Carvalho de Melo 802a8c2190eSArnaldo Carvalho de Melo for (j = s_j; j < lopt->nr_table_entries; j++) { 803a8c2190eSArnaldo Carvalho de Melo struct request_sock *req, *head = lopt->syn_table[j]; 804a8c2190eSArnaldo Carvalho de Melo 805a8c2190eSArnaldo Carvalho de Melo reqnum = 0; 806a8c2190eSArnaldo Carvalho de Melo for (req = head; req; reqnum++, req = req->dl_next) { 807a8c2190eSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 808a8c2190eSArnaldo Carvalho de Melo 809a8c2190eSArnaldo Carvalho de Melo if (reqnum < s_reqnum) 810a8c2190eSArnaldo Carvalho de Melo continue; 811634fb979SEric Dumazet if (r->id.idiag_dport != ireq->ir_rmt_port && 812a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 813a8c2190eSArnaldo Carvalho de Melo continue; 814a8c2190eSArnaldo Carvalho de Melo 815a8c2190eSArnaldo Carvalho de Melo if (bc) { 8161c95df85SNeal Cardwell inet_diag_req_addrs(sk, req, &entry); 817634fb979SEric Dumazet entry.dport = ntohs(ireq->ir_rmt_port); 818a8c2190eSArnaldo Carvalho de Melo 81987c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 820a8c2190eSArnaldo Carvalho de Melo continue; 821a8c2190eSArnaldo Carvalho de Melo } 822a8c2190eSArnaldo Carvalho de Melo 823a8c2190eSArnaldo Carvalho de Melo err = inet_diag_fill_req(skb, sk, req, 824e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 82515e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 826a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, cb->nlh); 827a8c2190eSArnaldo Carvalho de Melo if (err < 0) { 828a8c2190eSArnaldo Carvalho de Melo cb->args[3] = j + 1; 829a8c2190eSArnaldo Carvalho de Melo cb->args[4] = reqnum; 830a8c2190eSArnaldo Carvalho de Melo goto out; 831a8c2190eSArnaldo Carvalho de Melo } 832a8c2190eSArnaldo Carvalho de Melo } 833a8c2190eSArnaldo Carvalho de Melo 834a8c2190eSArnaldo Carvalho de Melo s_reqnum = 0; 835a8c2190eSArnaldo Carvalho de Melo } 836a8c2190eSArnaldo Carvalho de Melo 837a8c2190eSArnaldo Carvalho de Melo out: 838a8c2190eSArnaldo Carvalho de Melo read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 839a8c2190eSArnaldo Carvalho de Melo 840a8c2190eSArnaldo Carvalho de Melo return err; 841a8c2190eSArnaldo Carvalho de Melo } 842a8c2190eSArnaldo Carvalho de Melo 8431942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 844*e31c5e0eSEric Dumazet struct netlink_callback *cb, 845*e31c5e0eSEric Dumazet struct inet_diag_req_v2 *r, struct nlattr *bc) 846a8c2190eSArnaldo Carvalho de Melo { 84751d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 848*e31c5e0eSEric Dumazet int i, num, s_i, s_num; 849a8c2190eSArnaldo Carvalho de Melo 850a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 851a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 852a8c2190eSArnaldo Carvalho de Melo 853a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 854a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 855a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 856a8c2190eSArnaldo Carvalho de Melo 857a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 8585caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 859*e31c5e0eSEric Dumazet struct hlist_nulls_node *node; 860*e31c5e0eSEric Dumazet struct sock *sk; 861a8c2190eSArnaldo Carvalho de Melo 862a8c2190eSArnaldo Carvalho de Melo num = 0; 8635caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 8645caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 865c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 866a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 867a8c2190eSArnaldo Carvalho de Melo 86851d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 86951d7cccfSAndrey Vagin continue; 87051d7cccfSAndrey Vagin 871a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 872a8c2190eSArnaldo Carvalho de Melo num++; 873a8c2190eSArnaldo Carvalho de Melo continue; 874a8c2190eSArnaldo Carvalho de Melo } 875a8c2190eSArnaldo Carvalho de Melo 876d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 877d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 878d23deaa0SPavel Emelyanov goto next_listen; 879d23deaa0SPavel Emelyanov 880c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 881a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 882a8c2190eSArnaldo Carvalho de Melo goto next_listen; 883a8c2190eSArnaldo Carvalho de Melo 884a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_LISTEN) || 885a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport || 886a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 887a8c2190eSArnaldo Carvalho de Melo goto syn_recv; 888a8c2190eSArnaldo Carvalho de Melo 88925c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 8905caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 891a8c2190eSArnaldo Carvalho de Melo goto done; 892a8c2190eSArnaldo Carvalho de Melo } 893a8c2190eSArnaldo Carvalho de Melo 894a8c2190eSArnaldo Carvalho de Melo syn_recv: 895a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_SYN_RECV)) 896a8c2190eSArnaldo Carvalho de Melo goto next_listen; 897a8c2190eSArnaldo Carvalho de Melo 89825c4cd2bSPavel Emelyanov if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { 8995caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 900a8c2190eSArnaldo Carvalho de Melo goto done; 901a8c2190eSArnaldo Carvalho de Melo } 902a8c2190eSArnaldo Carvalho de Melo 903a8c2190eSArnaldo Carvalho de Melo next_listen: 904a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 905a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 906a8c2190eSArnaldo Carvalho de Melo ++num; 907a8c2190eSArnaldo Carvalho de Melo } 9085caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 909a8c2190eSArnaldo Carvalho de Melo 910a8c2190eSArnaldo Carvalho de Melo s_num = 0; 911a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 912a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 913a8c2190eSArnaldo Carvalho de Melo } 914a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 915a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 916a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 917a8c2190eSArnaldo Carvalho de Melo } 918a8c2190eSArnaldo Carvalho de Melo 919a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 920efb3cb42SPavel Emelyanov goto out; 921a8c2190eSArnaldo Carvalho de Melo 922f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 923a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 9247e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 9253ab5aee7SEric Dumazet struct hlist_nulls_node *node; 926*e31c5e0eSEric Dumazet struct sock *sk; 927a8c2190eSArnaldo Carvalho de Melo 9286be547a6SAndi Kleen num = 0; 9296be547a6SAndi Kleen 93005dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 9316be547a6SAndi Kleen continue; 9326be547a6SAndi Kleen 933a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 934a8c2190eSArnaldo Carvalho de Melo s_num = 0; 935a8c2190eSArnaldo Carvalho de Melo 9367e3aab4aSDavid S. Miller spin_lock_bh(lock); 9373ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 938*e31c5e0eSEric Dumazet int state, res; 939a8c2190eSArnaldo Carvalho de Melo 94051d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 94151d7cccfSAndrey Vagin continue; 942a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 943a8c2190eSArnaldo Carvalho de Melo goto next_normal; 94470315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 94570315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 94670315d22SNeal Cardwell if (!(r->idiag_states & (1 << state))) 947a8c2190eSArnaldo Carvalho de Melo goto next_normal; 948d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 949d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 950d23deaa0SPavel Emelyanov goto next_normal; 95105dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 952a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 953a8c2190eSArnaldo Carvalho de Melo goto next_normal; 95405dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 9554e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 956a8c2190eSArnaldo Carvalho de Melo goto next_normal; 95705dbc7b5SEric Dumazet if (sk->sk_state == TCP_TIME_WAIT) 95805dbc7b5SEric Dumazet res = inet_twsk_diag_dump(sk, skb, cb, r, bc); 95905dbc7b5SEric Dumazet else 96005dbc7b5SEric Dumazet res = inet_csk_diag_dump(sk, skb, cb, r, bc); 96105dbc7b5SEric Dumazet if (res < 0) { 9627e3aab4aSDavid S. Miller spin_unlock_bh(lock); 963a8c2190eSArnaldo Carvalho de Melo goto done; 964a8c2190eSArnaldo Carvalho de Melo } 965a8c2190eSArnaldo Carvalho de Melo next_normal: 966a8c2190eSArnaldo Carvalho de Melo ++num; 967a8c2190eSArnaldo Carvalho de Melo } 968a8c2190eSArnaldo Carvalho de Melo 9697e3aab4aSDavid S. Miller spin_unlock_bh(lock); 970a8c2190eSArnaldo Carvalho de Melo } 971a8c2190eSArnaldo Carvalho de Melo 972a8c2190eSArnaldo Carvalho de Melo done: 973a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 974a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 975efb3cb42SPavel Emelyanov out: 976efb3cb42SPavel Emelyanov ; 977efb3cb42SPavel Emelyanov } 9781942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 979efb3cb42SPavel Emelyanov 980efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 981*e31c5e0eSEric Dumazet struct inet_diag_req_v2 *r, 982*e31c5e0eSEric Dumazet struct nlattr *bc) 983efb3cb42SPavel Emelyanov { 984efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 985cacb6ba0SCyrill Gorcunov int err = 0; 986efb3cb42SPavel Emelyanov 987efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 988efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 9891942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 990cacb6ba0SCyrill Gorcunov else 991cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 992d523a328SHerbert Xu inet_diag_unlock_handler(handler); 993efb3cb42SPavel Emelyanov 994cacb6ba0SCyrill Gorcunov return err ? : skb->len; 995a8c2190eSArnaldo Carvalho de Melo } 996a8c2190eSArnaldo Carvalho de Melo 99725c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 99825c4cd2bSPavel Emelyanov { 999c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 1000*e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 100125c4cd2bSPavel Emelyanov 100225c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 100325c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 100425c4cd2bSPavel Emelyanov 1005d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 100625c4cd2bSPavel Emelyanov } 100725c4cd2bSPavel Emelyanov 1008*e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 1009a029fe26SPavel Emelyanov { 1010a029fe26SPavel Emelyanov switch (type) { 1011a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 1012a029fe26SPavel Emelyanov return IPPROTO_TCP; 1013a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 1014a029fe26SPavel Emelyanov return IPPROTO_DCCP; 1015a029fe26SPavel Emelyanov default: 1016a029fe26SPavel Emelyanov return 0; 1017a029fe26SPavel Emelyanov } 1018a029fe26SPavel Emelyanov } 1019a029fe26SPavel Emelyanov 1020*e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 1021*e31c5e0eSEric Dumazet struct netlink_callback *cb) 102225c4cd2bSPavel Emelyanov { 1023d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1024*e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 1025c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 102625c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 102725c4cd2bSPavel Emelyanov 1028d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 102925c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 103025c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 103125c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 103225c4cd2bSPavel Emelyanov req.id = rc->id; 103325c4cd2bSPavel Emelyanov 103425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 103525c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 103625c4cd2bSPavel Emelyanov 103725c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 103825c4cd2bSPavel Emelyanov } 103925c4cd2bSPavel Emelyanov 1040fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1041fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1042fe50ce28SPavel Emelyanov { 1043d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1044c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1045fe50ce28SPavel Emelyanov 1046fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1047fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1048fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1049fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1050fe50ce28SPavel Emelyanov req.id = rc->id; 1051fe50ce28SPavel Emelyanov 1052fe50ce28SPavel Emelyanov return inet_diag_get_exact(in_skb, nlh, &req); 1053fe50ce28SPavel Emelyanov } 1054fe50ce28SPavel Emelyanov 10558d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1056a8c2190eSArnaldo Carvalho de Melo { 10573b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 105851d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1059a8c2190eSArnaldo Carvalho de Melo 1060ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1061ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1062ead592baSThomas Graf return -EINVAL; 1063a8c2190eSArnaldo Carvalho de Melo 1064b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 1065ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 1066ead592baSThomas Graf struct nlattr *attr; 1067ead592baSThomas Graf 1068ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 1069ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 1070*e31c5e0eSEric Dumazet if (!attr || 1071ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 1072ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 1073a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 1074a8c2190eSArnaldo Carvalho de Melo } 107580d326faSPablo Neira Ayuso { 107680d326faSPablo Neira Ayuso struct netlink_dump_control c = { 107780d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 107880d326faSPablo Neira Ayuso }; 107951d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 108080d326faSPablo Neira Ayuso } 1081a8c2190eSArnaldo Carvalho de Melo } 1082ead592baSThomas Graf 1083fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1084a8c2190eSArnaldo Carvalho de Melo } 1085a8c2190eSArnaldo Carvalho de Melo 1086d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 1087d366477aSPavel Emelyanov { 1088c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 108951d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1090d366477aSPavel Emelyanov 1091d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1092d366477aSPavel Emelyanov return -EINVAL; 1093d366477aSPavel Emelyanov 1094d366477aSPavel Emelyanov if (h->nlmsg_flags & NLM_F_DUMP) { 109525c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 109625c4cd2bSPavel Emelyanov struct nlattr *attr; 1097*e31c5e0eSEric Dumazet 109825c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 109925c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 1100*e31c5e0eSEric Dumazet if (!attr || 110125c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 110225c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 110325c4cd2bSPavel Emelyanov return -EINVAL; 110425c4cd2bSPavel Emelyanov } 110580d326faSPablo Neira Ayuso { 110680d326faSPablo Neira Ayuso struct netlink_dump_control c = { 110780d326faSPablo Neira Ayuso .dump = inet_diag_dump, 110880d326faSPablo Neira Ayuso }; 110951d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 111080d326faSPablo Neira Ayuso } 1111d366477aSPavel Emelyanov } 1112d366477aSPavel Emelyanov 1113d106352dSDavid S. Miller return inet_diag_get_exact(skb, h, nlmsg_data(h)); 1114d366477aSPavel Emelyanov } 1115d366477aSPavel Emelyanov 11168dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1117d366477aSPavel Emelyanov .family = AF_INET, 1118d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 1119d366477aSPavel Emelyanov }; 1120d366477aSPavel Emelyanov 11218dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1122d366477aSPavel Emelyanov .family = AF_INET6, 1123d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 1124d366477aSPavel Emelyanov }; 1125d366477aSPavel Emelyanov 1126a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1127a8c2190eSArnaldo Carvalho de Melo { 1128a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1129a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1130a8c2190eSArnaldo Carvalho de Melo 1131f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1132a8c2190eSArnaldo Carvalho de Melo goto out; 1133a8c2190eSArnaldo Carvalho de Melo 1134d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1135a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1136*e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1137a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1138a8c2190eSArnaldo Carvalho de Melo err = 0; 1139a8c2190eSArnaldo Carvalho de Melo } 1140d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1141a8c2190eSArnaldo Carvalho de Melo out: 1142a8c2190eSArnaldo Carvalho de Melo return err; 1143a8c2190eSArnaldo Carvalho de Melo } 1144a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1145a8c2190eSArnaldo Carvalho de Melo 1146a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1147a8c2190eSArnaldo Carvalho de Melo { 1148a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1149a8c2190eSArnaldo Carvalho de Melo 1150f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1151a8c2190eSArnaldo Carvalho de Melo return; 1152a8c2190eSArnaldo Carvalho de Melo 1153d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1154a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1155d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1156a8c2190eSArnaldo Carvalho de Melo } 1157a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1158a8c2190eSArnaldo Carvalho de Melo 1159a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1160a8c2190eSArnaldo Carvalho de Melo { 1161f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1162a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1163a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1164a8c2190eSArnaldo Carvalho de Melo 11650da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1166a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1167a8c2190eSArnaldo Carvalho de Melo goto out; 1168a8c2190eSArnaldo Carvalho de Melo 1169d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1170d366477aSPavel Emelyanov if (err) 1171d366477aSPavel Emelyanov goto out_free_nl; 1172d366477aSPavel Emelyanov 1173d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1174d366477aSPavel Emelyanov if (err) 1175d366477aSPavel Emelyanov goto out_free_inet; 1176d366477aSPavel Emelyanov 11778ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1178a8c2190eSArnaldo Carvalho de Melo out: 1179a8c2190eSArnaldo Carvalho de Melo return err; 1180d366477aSPavel Emelyanov 1181d366477aSPavel Emelyanov out_free_inet: 1182d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1183d366477aSPavel Emelyanov out_free_nl: 1184a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1185a8c2190eSArnaldo Carvalho de Melo goto out; 1186a8c2190eSArnaldo Carvalho de Melo } 1187a8c2190eSArnaldo Carvalho de Melo 1188a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1189a8c2190eSArnaldo Carvalho de Melo { 1190d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1191d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 11928ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1193a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1194a8c2190eSArnaldo Carvalho de Melo } 1195a8c2190eSArnaldo Carvalho de Melo 1196a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1197a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1198a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1199aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1200aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1201