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; 47637c841dSDavid Ahern u32 ifindex; 48a52e95abSLorenzo Colitti u32 mark; 49a8c2190eSArnaldo Carvalho de Melo }; 50a8c2190eSArnaldo Carvalho de Melo 51d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 52d523a328SHerbert Xu 53f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 54d523a328SHerbert Xu { 55f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 56*bf2ae2e4SXin Long sock_load_diag_module(AF_INET, proto); 57d523a328SHerbert Xu 58d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 59f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 60d523a328SHerbert Xu return ERR_PTR(-ENOENT); 61d523a328SHerbert Xu 62f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 63d523a328SHerbert Xu } 64d523a328SHerbert Xu 65e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 66d523a328SHerbert Xu { 67d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 68d523a328SHerbert Xu } 69d523a328SHerbert Xu 70cb2050a7SXin Long void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) 71a4458343SEric Dumazet { 72a4458343SEric Dumazet r->idiag_family = sk->sk_family; 73a4458343SEric Dumazet 74a4458343SEric Dumazet r->id.idiag_sport = htons(sk->sk_num); 75a4458343SEric Dumazet r->id.idiag_dport = sk->sk_dport; 76a4458343SEric Dumazet r->id.idiag_if = sk->sk_bound_dev_if; 77a4458343SEric Dumazet sock_diag_save_cookie(sk, r->id.idiag_cookie); 78a4458343SEric Dumazet 79a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 80a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 81a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 82a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 83a4458343SEric Dumazet } else 84a4458343SEric Dumazet #endif 85a4458343SEric Dumazet { 86a4458343SEric Dumazet memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 87a4458343SEric Dumazet memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 88a4458343SEric Dumazet 89a4458343SEric Dumazet r->id.idiag_src[0] = sk->sk_rcv_saddr; 90a4458343SEric Dumazet r->id.idiag_dst[0] = sk->sk_daddr; 91a4458343SEric Dumazet } 92a4458343SEric Dumazet } 93cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill); 94a4458343SEric Dumazet 95b37e8840SIvan Delalande static size_t inet_sk_attr_size(struct sock *sk, 96b37e8840SIvan Delalande const struct inet_diag_req_v2 *req, 97b37e8840SIvan Delalande bool net_admin) 98c8e2c80dSEric Dumazet { 99b37e8840SIvan Delalande const struct inet_diag_handler *handler; 100b37e8840SIvan Delalande size_t aux = 0; 101b37e8840SIvan Delalande 102b37e8840SIvan Delalande handler = inet_diag_table[req->sdiag_protocol]; 103b37e8840SIvan Delalande if (handler && handler->idiag_get_aux_size) 104b37e8840SIvan Delalande aux = handler->idiag_get_aux_size(sk, net_admin); 105b37e8840SIvan Delalande 106c8e2c80dSEric Dumazet return nla_total_size(sizeof(struct tcp_info)) 107c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_SHUTDOWN */ 108c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TOS */ 109c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TCLASS */ 110d545cacaSLorenzo Colitti + nla_total_size(4) /* INET_DIAG_MARK */ 111c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_meminfo)) 112c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_msg)) 113c8e2c80dSEric Dumazet + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) 114c8e2c80dSEric Dumazet + nla_total_size(TCP_CA_NAME_MAX) 115c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct tcpvegas_info)) 116b37e8840SIvan Delalande + aux 117c8e2c80dSEric Dumazet + 64; 118c8e2c80dSEric Dumazet } 119c8e2c80dSEric Dumazet 120cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, 121cb2050a7SXin Long struct inet_diag_msg *r, int ext, 122d545cacaSLorenzo Colitti struct user_namespace *user_ns, 123d545cacaSLorenzo Colitti bool net_admin) 124a8c2190eSArnaldo Carvalho de Melo { 125a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 126a8c2190eSArnaldo Carvalho de Melo 127e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 128e4e541a8SPavel Emelyanov goto errout; 129e4e541a8SPavel Emelyanov 130717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 131717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 132717b6d83SMaciej Żenczykowski */ 133717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 1346e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 1356e277ed5SThomas Graf goto errout; 136717b6d83SMaciej Żenczykowski 137dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 138a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 13906236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 140efe4208fSEric Dumazet if (nla_put_u8(skb, INET_DIAG_TCLASS, 141efe4208fSEric Dumazet inet6_sk(sk)->tclass) < 0) 1426e277ed5SThomas Graf goto errout; 14320462155SPhil Sutter 1448220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 1458220ea23SPhil Sutter nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) 14620462155SPhil Sutter goto errout; 147a8c2190eSArnaldo Carvalho de Melo } 148a8c2190eSArnaldo Carvalho de Melo #endif 149a8c2190eSArnaldo Carvalho de Melo 150d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark)) 151d545cacaSLorenzo Colitti goto errout; 152d545cacaSLorenzo Colitti 153d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 1543c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1553c4d05c8SPavel Emelyanov 156cb2050a7SXin Long return 0; 157cb2050a7SXin Long errout: 158cb2050a7SXin Long return 1; 159cb2050a7SXin Long } 160cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill); 161cb2050a7SXin Long 162cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 163cb2050a7SXin Long struct sk_buff *skb, const struct inet_diag_req_v2 *req, 164cb2050a7SXin Long struct user_namespace *user_ns, 165cb2050a7SXin Long u32 portid, u32 seq, u16 nlmsg_flags, 166d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, 167d545cacaSLorenzo Colitti bool net_admin) 168cb2050a7SXin Long { 169cb2050a7SXin Long const struct tcp_congestion_ops *ca_ops; 170cb2050a7SXin Long const struct inet_diag_handler *handler; 171cb2050a7SXin Long int ext = req->idiag_ext; 172cb2050a7SXin Long struct inet_diag_msg *r; 173cb2050a7SXin Long struct nlmsghdr *nlh; 174cb2050a7SXin Long struct nlattr *attr; 175cb2050a7SXin Long void *info = NULL; 176cb2050a7SXin Long 177cb2050a7SXin Long handler = inet_diag_table[req->sdiag_protocol]; 178cb2050a7SXin Long BUG_ON(!handler); 179cb2050a7SXin Long 180cb2050a7SXin Long nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 181cb2050a7SXin Long nlmsg_flags); 182cb2050a7SXin Long if (!nlh) 183cb2050a7SXin Long return -EMSGSIZE; 184cb2050a7SXin Long 185cb2050a7SXin Long r = nlmsg_data(nlh); 186cb2050a7SXin Long BUG_ON(!sk_fullsock(sk)); 187cb2050a7SXin Long 188cb2050a7SXin Long inet_diag_msg_common_fill(r, sk); 189cb2050a7SXin Long r->idiag_state = sk->sk_state; 190cb2050a7SXin Long r->idiag_timer = 0; 191cb2050a7SXin Long r->idiag_retrans = 0; 192cb2050a7SXin Long 193d545cacaSLorenzo Colitti if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin)) 194cb2050a7SXin Long goto errout; 195cb2050a7SXin Long 1966e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 1976e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 1986e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 1996e277ed5SThomas Graf .idiag_wmem = sk->sk_wmem_queued, 2006e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 2016e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 2026e277ed5SThomas Graf }; 2036e277ed5SThomas Graf 2046e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 2056e277ed5SThomas Graf goto errout; 2063c4d05c8SPavel Emelyanov } 2073c4d05c8SPavel Emelyanov 208c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 209c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 2106e277ed5SThomas Graf goto errout; 211c0636faaSPavel Emelyanov 212432490f9SCyrill Gorcunov /* 213432490f9SCyrill Gorcunov * RAW sockets might have user-defined protocols assigned, 214432490f9SCyrill Gorcunov * so report the one supplied on socket creation. 215432490f9SCyrill Gorcunov */ 216432490f9SCyrill Gorcunov if (sk->sk_type == SOCK_RAW) { 217432490f9SCyrill Gorcunov if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) 218432490f9SCyrill Gorcunov goto errout; 219432490f9SCyrill Gorcunov } 220432490f9SCyrill Gorcunov 221e31c5e0eSEric Dumazet if (!icsk) { 22262ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 2233c4d05c8SPavel Emelyanov goto out; 2243c4d05c8SPavel Emelyanov } 2253c4d05c8SPavel Emelyanov 2266ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 22757dde7f7SYuchung Cheng icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 2286ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 229a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 230a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 231b7de529cSXin Long r->idiag_expires = 232b7de529cSXin Long jiffies_to_msecs(icsk->icsk_timeout - jiffies); 233a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 234a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 235a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 236b7de529cSXin Long r->idiag_expires = 237b7de529cSXin Long jiffies_to_msecs(icsk->icsk_timeout - jiffies); 238a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 239a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 240a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 241b7de529cSXin Long r->idiag_expires = 242b7de529cSXin Long jiffies_to_msecs(sk->sk_timer.expires - jiffies); 243a8c2190eSArnaldo Carvalho de Melo } else { 244a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 245a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 246a8c2190eSArnaldo Carvalho de Melo } 247a8c2190eSArnaldo Carvalho de Melo 2483fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 2496ed46d12SNicolas Dichtel attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 2506ed46d12SNicolas Dichtel handler->idiag_info_size, 2516ed46d12SNicolas Dichtel INET_DIAG_PAD); 2526e277ed5SThomas Graf if (!attr) 2536e277ed5SThomas Graf goto errout; 254a8c2190eSArnaldo Carvalho de Melo 2556e277ed5SThomas Graf info = nla_data(attr); 256a8c2190eSArnaldo Carvalho de Melo } 257a8c2190eSArnaldo Carvalho de Melo 258521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) { 259521f1cf1SEric Dumazet int err = 0; 260521f1cf1SEric Dumazet 261521f1cf1SEric Dumazet rcu_read_lock(); 262521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 263521f1cf1SEric Dumazet if (ca_ops) 264521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 265521f1cf1SEric Dumazet rcu_read_unlock(); 266521f1cf1SEric Dumazet if (err < 0) 2676e277ed5SThomas Graf goto errout; 268521f1cf1SEric Dumazet } 2696e277ed5SThomas Graf 270a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 271a8c2190eSArnaldo Carvalho de Melo 272b37e8840SIvan Delalande if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux) 273b37e8840SIvan Delalande if (handler->idiag_get_aux(sk, net_admin, skb) < 0) 274b37e8840SIvan Delalande goto errout; 275b37e8840SIvan Delalande 276521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) { 27764f40ff5SEric Dumazet union tcp_cc_info info; 27864f40ff5SEric Dumazet size_t sz = 0; 27964f40ff5SEric Dumazet int attr; 280521f1cf1SEric Dumazet 281521f1cf1SEric Dumazet rcu_read_lock(); 282521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 283521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info) 28464f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info); 285521f1cf1SEric Dumazet rcu_read_unlock(); 28664f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0) 287521f1cf1SEric Dumazet goto errout; 288521f1cf1SEric Dumazet } 289a8c2190eSArnaldo Carvalho de Melo 2900888e372SLevin, Alexander (Sasha Levin) if (ext & (1 << (INET_DIAG_CLASS_ID - 1))) { 2910888e372SLevin, Alexander (Sasha Levin) u32 classid = 0; 2920888e372SLevin, Alexander (Sasha Levin) 2930888e372SLevin, Alexander (Sasha Levin) #ifdef CONFIG_SOCK_CGROUP_DATA 2940888e372SLevin, Alexander (Sasha Levin) classid = sock_cgroup_classid(&sk->sk_cgrp_data); 2950888e372SLevin, Alexander (Sasha Levin) #endif 2960888e372SLevin, Alexander (Sasha Levin) 2970888e372SLevin, Alexander (Sasha Levin) if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid)) 2980888e372SLevin, Alexander (Sasha Levin) goto errout; 2990888e372SLevin, Alexander (Sasha Levin) } 3000888e372SLevin, Alexander (Sasha Levin) 3013c4d05c8SPavel Emelyanov out: 302053c095aSJohannes Berg nlmsg_end(skb, nlh); 303053c095aSJohannes Berg return 0; 304a8c2190eSArnaldo Carvalho de Melo 3056e277ed5SThomas Graf errout: 3066e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 30726932566SPatrick McHardy return -EMSGSIZE; 308a8c2190eSArnaldo Carvalho de Melo } 3093c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 3103c4d05c8SPavel Emelyanov 3113c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk, 312e31c5e0eSEric Dumazet struct sk_buff *skb, 31334160ea3SEric Dumazet const struct inet_diag_req_v2 *req, 314d06ca956SEric W. Biederman struct user_namespace *user_ns, 31515e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 316d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, 317d545cacaSLorenzo Colitti bool net_admin) 3183c4d05c8SPavel Emelyanov { 319d545cacaSLorenzo Colitti return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns, 320d545cacaSLorenzo Colitti portid, seq, nlmsg_flags, unlh, net_admin); 3213c4d05c8SPavel Emelyanov } 322a8c2190eSArnaldo Carvalho de Melo 32333cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk, 324e31c5e0eSEric Dumazet struct sk_buff *skb, 32515e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 326c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 327c7d58aabSArnaldo Carvalho de Melo { 32833cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk); 329c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 3306e277ed5SThomas Graf struct nlmsghdr *nlh; 331789f558cSEric Dumazet long tmo; 332c7d58aabSArnaldo Carvalho de Melo 33315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 3346e277ed5SThomas Graf nlmsg_flags); 3356e277ed5SThomas Graf if (!nlh) 336d106352dSDavid S. Miller return -EMSGSIZE; 337d106352dSDavid S. Miller 338d106352dSDavid S. Miller r = nlmsg_data(nlh); 339c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 340c7d58aabSArnaldo Carvalho de Melo 341789f558cSEric Dumazet tmo = tw->tw_timer.expires - jiffies; 342c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 343c7d58aabSArnaldo Carvalho de Melo tmo = 0; 344c7d58aabSArnaldo Carvalho de Melo 345a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 346c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 347b1aac815SDaniel Borkmann 348c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 349c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 35096f817feSEric Dumazet r->idiag_expires = jiffies_to_msecs(tmo); 351c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 352c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 353c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 354c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 3556e277ed5SThomas Graf 356053c095aSJohannes Berg nlmsg_end(skb, nlh); 357053c095aSJohannes Berg return 0; 358c7d58aabSArnaldo Carvalho de Melo } 359c7d58aabSArnaldo Carvalho de Melo 360a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 361a58917f5SEric Dumazet u32 portid, u32 seq, u16 nlmsg_flags, 362d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, bool net_admin) 363a58917f5SEric Dumazet { 364d545cacaSLorenzo Colitti struct request_sock *reqsk = inet_reqsk(sk); 365a58917f5SEric Dumazet struct inet_diag_msg *r; 366a58917f5SEric Dumazet struct nlmsghdr *nlh; 367a58917f5SEric Dumazet long tmo; 368a58917f5SEric Dumazet 369a58917f5SEric Dumazet nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 370a58917f5SEric Dumazet nlmsg_flags); 371a58917f5SEric Dumazet if (!nlh) 372a58917f5SEric Dumazet return -EMSGSIZE; 373a58917f5SEric Dumazet 374a58917f5SEric Dumazet r = nlmsg_data(nlh); 375a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk); 376a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV; 377a58917f5SEric Dumazet r->idiag_timer = 1; 378d545cacaSLorenzo Colitti r->idiag_retrans = reqsk->num_retrans; 379a58917f5SEric Dumazet 380a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 381a58917f5SEric Dumazet offsetof(struct sock, sk_cookie)); 382a58917f5SEric Dumazet 383fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 384a58917f5SEric Dumazet r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0; 385a58917f5SEric Dumazet r->idiag_rqueue = 0; 386a58917f5SEric Dumazet r->idiag_wqueue = 0; 387a58917f5SEric Dumazet r->idiag_uid = 0; 388a58917f5SEric Dumazet r->idiag_inode = 0; 389a58917f5SEric Dumazet 390d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 391d545cacaSLorenzo Colitti inet_rsk(reqsk)->ir_mark)) 392d545cacaSLorenzo Colitti return -EMSGSIZE; 393d545cacaSLorenzo Colitti 394a58917f5SEric Dumazet nlmsg_end(skb, nlh); 395a58917f5SEric Dumazet return 0; 396a58917f5SEric Dumazet } 397a58917f5SEric Dumazet 398dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 39934160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 400d06ca956SEric W. Biederman struct user_namespace *user_ns, 40115e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 402d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, bool net_admin) 403dff2c035SArnaldo Carvalho de Melo { 404dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 405a58917f5SEric Dumazet return inet_twsk_diag_fill(sk, skb, portid, seq, 406a58917f5SEric Dumazet nlmsg_flags, unlh); 407a58917f5SEric Dumazet 408a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) 409a58917f5SEric Dumazet return inet_req_diag_fill(sk, skb, portid, seq, 410d545cacaSLorenzo Colitti nlmsg_flags, unlh, net_admin); 411efe4208fSEric Dumazet 412efe4208fSEric Dumazet return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, 413d545cacaSLorenzo Colitti nlmsg_flags, unlh, net_admin); 414dff2c035SArnaldo Carvalho de Melo } 415dff2c035SArnaldo Carvalho de Melo 416b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net, 417b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo, 41834160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 419a8c2190eSArnaldo Carvalho de Melo { 420e31c5e0eSEric Dumazet struct sock *sk; 421a8c2190eSArnaldo Carvalho de Melo 4222d331915SEric Dumazet rcu_read_lock(); 423e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 424a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 425a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 426a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 427dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4287c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) { 4297c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 4307c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 431a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 4327c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3], 4337c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if); 4347c130672SEric Dumazet else 435a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0, 436a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 437a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 438a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 439a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 440a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 4417c130672SEric Dumazet } 442a8c2190eSArnaldo Carvalho de Melo #endif 4432d331915SEric Dumazet else { 4442d331915SEric Dumazet rcu_read_unlock(); 445b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL); 4462d331915SEric Dumazet } 4472d331915SEric Dumazet rcu_read_unlock(); 448e31c5e0eSEric Dumazet if (!sk) 449b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 450a8c2190eSArnaldo Carvalho de Melo 451b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 452b613f56eSLorenzo Colitti sock_gen_put(sk); 453b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 454b613f56eSLorenzo Colitti } 455b613f56eSLorenzo Colitti 456b613f56eSLorenzo Colitti return sk; 457b613f56eSLorenzo Colitti } 458b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 459b613f56eSLorenzo Colitti 460b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 461b613f56eSLorenzo Colitti struct sk_buff *in_skb, 462b613f56eSLorenzo Colitti const struct nlmsghdr *nlh, 463b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req) 464b613f56eSLorenzo Colitti { 465b37e8840SIvan Delalande bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN); 466b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk); 467b613f56eSLorenzo Colitti struct sk_buff *rep; 468b613f56eSLorenzo Colitti struct sock *sk; 469b613f56eSLorenzo Colitti int err; 470b613f56eSLorenzo Colitti 471b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req); 472b613f56eSLorenzo Colitti if (IS_ERR(sk)) 473b613f56eSLorenzo Colitti return PTR_ERR(sk); 474a8c2190eSArnaldo Carvalho de Melo 475b37e8840SIvan Delalande rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL); 4766e277ed5SThomas Graf if (!rep) { 4776e277ed5SThomas Graf err = -ENOMEM; 478a8c2190eSArnaldo Carvalho de Melo goto out; 4796e277ed5SThomas Graf } 480a8c2190eSArnaldo Carvalho de Melo 481a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 482e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(in_skb).sk), 48315e47304SEric W. Biederman NETLINK_CB(in_skb).portid, 484b37e8840SIvan Delalande nlh->nlmsg_seq, 0, nlh, net_admin); 48526932566SPatrick McHardy if (err < 0) { 48626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 4876e277ed5SThomas Graf nlmsg_free(rep); 48826932566SPatrick McHardy goto out; 48926932566SPatrick McHardy } 49015e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 491a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 492a8c2190eSArnaldo Carvalho de Melo if (err > 0) 493a8c2190eSArnaldo Carvalho de Melo err = 0; 494a8c2190eSArnaldo Carvalho de Melo 495a8c2190eSArnaldo Carvalho de Melo out: 496c1d607ccSEric Dumazet if (sk) 497c1d607ccSEric Dumazet sock_gen_put(sk); 498c1d607ccSEric Dumazet 499476f7dbfSPavel Emelyanov return err; 500476f7dbfSPavel Emelyanov } 5011942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 502476f7dbfSPavel Emelyanov 5036eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 504476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 50534160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 506476f7dbfSPavel Emelyanov { 507476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 508476f7dbfSPavel Emelyanov int err; 509476f7dbfSPavel Emelyanov 510476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 511476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 512476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 5136eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DIAG_BY_FAMILY) 5141942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 5156eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DESTROY && handler->destroy) 5166eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req); 5176eb5d2e0SLorenzo Colitti else 5186eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP; 519d523a328SHerbert Xu inet_diag_unlock_handler(handler); 520476f7dbfSPavel Emelyanov 521a8c2190eSArnaldo Carvalho de Melo return err; 522a8c2190eSArnaldo Carvalho de Melo } 523a8c2190eSArnaldo Carvalho de Melo 5249f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 525a8c2190eSArnaldo Carvalho de Melo { 526a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 527a8c2190eSArnaldo Carvalho de Melo 528a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 529a8c2190eSArnaldo Carvalho de Melo 530a8c2190eSArnaldo Carvalho de Melo if (words) { 531a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 532a8c2190eSArnaldo Carvalho de Melo return 0; 533a8c2190eSArnaldo Carvalho de Melo } 534a8c2190eSArnaldo Carvalho de Melo if (bits) { 5359f855299SAl Viro __be32 w1, w2; 5369f855299SAl Viro __be32 mask; 537a8c2190eSArnaldo Carvalho de Melo 538a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 539a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 540a8c2190eSArnaldo Carvalho de Melo 541a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 542a8c2190eSArnaldo Carvalho de Melo 543a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 544a8c2190eSArnaldo Carvalho de Melo return 0; 545a8c2190eSArnaldo Carvalho de Melo } 546a8c2190eSArnaldo Carvalho de Melo 547a8c2190eSArnaldo Carvalho de Melo return 1; 548a8c2190eSArnaldo Carvalho de Melo } 549a8c2190eSArnaldo Carvalho de Melo 55087c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 551a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 552a8c2190eSArnaldo Carvalho de Melo { 55387c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 55487c22ea5SPavel Emelyanov int len = nla_len(_bc); 55587c22ea5SPavel Emelyanov 556a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 557a8c2190eSArnaldo Carvalho de Melo int yes = 1; 558a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 559a8c2190eSArnaldo Carvalho de Melo 560a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 561a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 562a8c2190eSArnaldo Carvalho de Melo break; 563a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 564a8c2190eSArnaldo Carvalho de Melo yes = 0; 565a8c2190eSArnaldo Carvalho de Melo break; 566bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 567bbb6189dSKristian Evensen yes = entry->sport == op[1].no; 568bbb6189dSKristian Evensen break; 569a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 570a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 571a8c2190eSArnaldo Carvalho de Melo break; 572a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 573b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 574a8c2190eSArnaldo Carvalho de Melo break; 575bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 576bbb6189dSKristian Evensen yes = entry->dport == op[1].no; 577bbb6189dSKristian Evensen break; 578a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 579a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 580a8c2190eSArnaldo Carvalho de Melo break; 581a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 582a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 583a8c2190eSArnaldo Carvalho de Melo break; 584a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 585a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 586a8c2190eSArnaldo Carvalho de Melo break; 587a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 588a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 589e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 590e31c5e0eSEric Dumazet const __be32 *addr; 591a8c2190eSArnaldo Carvalho de Melo 592e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 593a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 594a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 595a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 596a8c2190eSArnaldo Carvalho de Melo yes = 0; 597a8c2190eSArnaldo Carvalho de Melo break; 598a8c2190eSArnaldo Carvalho de Melo } 599a8c2190eSArnaldo Carvalho de Melo 600a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 601a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 602a8c2190eSArnaldo Carvalho de Melo else 603a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 604a8c2190eSArnaldo Carvalho de Melo 605f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 606f67caec9SNeal Cardwell cond->family != entry->family) { 607a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 608a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 609a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 610a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 611f67caec9SNeal Cardwell bitstring_match(addr + 3, 612f67caec9SNeal Cardwell cond->addr, 613a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 614a8c2190eSArnaldo Carvalho de Melo break; 615a8c2190eSArnaldo Carvalho de Melo } 616a8c2190eSArnaldo Carvalho de Melo yes = 0; 617a8c2190eSArnaldo Carvalho de Melo break; 618a8c2190eSArnaldo Carvalho de Melo } 619f67caec9SNeal Cardwell 620f67caec9SNeal Cardwell if (cond->prefix_len == 0) 621f67caec9SNeal Cardwell break; 622f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 623f67caec9SNeal Cardwell cond->prefix_len)) 624f67caec9SNeal Cardwell break; 625f67caec9SNeal Cardwell yes = 0; 626f67caec9SNeal Cardwell break; 627f67caec9SNeal Cardwell } 628637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: { 629637c841dSDavid Ahern u32 ifindex; 630637c841dSDavid Ahern 631637c841dSDavid Ahern ifindex = *((const u32 *)(op + 1)); 632637c841dSDavid Ahern if (ifindex != entry->ifindex) 633637c841dSDavid Ahern yes = 0; 634637c841dSDavid Ahern break; 635637c841dSDavid Ahern } 636a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: { 637a52e95abSLorenzo Colitti struct inet_diag_markcond *cond; 638a52e95abSLorenzo Colitti 639a52e95abSLorenzo Colitti cond = (struct inet_diag_markcond *)(op + 1); 640a52e95abSLorenzo Colitti if ((entry->mark & cond->mask) != cond->mark) 641a52e95abSLorenzo Colitti yes = 0; 642a52e95abSLorenzo Colitti break; 643a52e95abSLorenzo Colitti } 644a8c2190eSArnaldo Carvalho de Melo } 645a8c2190eSArnaldo Carvalho de Melo 646a8c2190eSArnaldo Carvalho de Melo if (yes) { 647a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 648a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 649a8c2190eSArnaldo Carvalho de Melo } else { 650a8c2190eSArnaldo Carvalho de Melo len -= op->no; 651a8c2190eSArnaldo Carvalho de Melo bc += op->no; 652a8c2190eSArnaldo Carvalho de Melo } 653a8c2190eSArnaldo Carvalho de Melo } 654a02cec21SEric Dumazet return len == 0; 655a8c2190eSArnaldo Carvalho de Melo } 656a8c2190eSArnaldo Carvalho de Melo 657a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 658a4458343SEric Dumazet */ 659a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 660a4458343SEric Dumazet const struct sock *sk) 661a4458343SEric Dumazet { 662a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 663a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 664a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 665a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 666a4458343SEric Dumazet } else 667a4458343SEric Dumazet #endif 668a4458343SEric Dumazet { 669a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 670a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 671a4458343SEric Dumazet } 672a4458343SEric Dumazet } 673a4458343SEric Dumazet 6748d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 6758d07d151SPavel Emelyanov { 6768d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 677e31c5e0eSEric Dumazet struct inet_diag_entry entry; 6788d07d151SPavel Emelyanov 679e31c5e0eSEric Dumazet if (!bc) 6808d07d151SPavel Emelyanov return 1; 6818d07d151SPavel Emelyanov 6828d07d151SPavel Emelyanov entry.family = sk->sk_family; 683a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 6848d07d151SPavel Emelyanov entry.sport = inet->inet_num; 6858d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 686637c841dSDavid Ahern entry.ifindex = sk->sk_bound_dev_if; 687a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 688a52e95abSLorenzo Colitti if (sk_fullsock(sk)) 689a52e95abSLorenzo Colitti entry.mark = sk->sk_mark; 690a52e95abSLorenzo Colitti else if (sk->sk_state == TCP_NEW_SYN_RECV) 691a52e95abSLorenzo Colitti entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; 692a52e95abSLorenzo Colitti else 693a52e95abSLorenzo Colitti entry.mark = 0; 6948d07d151SPavel Emelyanov 6958d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 6968d07d151SPavel Emelyanov } 6978d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 6988d07d151SPavel Emelyanov 699a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 700a8c2190eSArnaldo Carvalho de Melo { 701a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 702a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 703a8c2190eSArnaldo Carvalho de Melo 704a8c2190eSArnaldo Carvalho de Melo if (cc > len) 705a8c2190eSArnaldo Carvalho de Melo return 0; 706a8c2190eSArnaldo Carvalho de Melo if (cc == len) 707a8c2190eSArnaldo Carvalho de Melo return 1; 708eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 709a8c2190eSArnaldo Carvalho de Melo return 0; 710a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 711a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 712a8c2190eSArnaldo Carvalho de Melo } 713a8c2190eSArnaldo Carvalho de Melo return 0; 714a8c2190eSArnaldo Carvalho de Melo } 715a8c2190eSArnaldo Carvalho de Melo 716637c841dSDavid Ahern /* data is u32 ifindex */ 717637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len, 718637c841dSDavid Ahern int *min_len) 719637c841dSDavid Ahern { 720637c841dSDavid Ahern /* Check ifindex space. */ 721637c841dSDavid Ahern *min_len += sizeof(u32); 722637c841dSDavid Ahern if (len < *min_len) 723637c841dSDavid Ahern return false; 724637c841dSDavid Ahern 725637c841dSDavid Ahern return true; 726637c841dSDavid Ahern } 727405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 728405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 729405c0059SNeal Cardwell int *min_len) 730405c0059SNeal Cardwell { 731405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 732e31c5e0eSEric Dumazet int addr_len; 733405c0059SNeal Cardwell 734405c0059SNeal Cardwell /* Check hostcond space. */ 735405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 736405c0059SNeal Cardwell if (len < *min_len) 737405c0059SNeal Cardwell return false; 738405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 739405c0059SNeal Cardwell 740405c0059SNeal Cardwell /* Check address family and address length. */ 741405c0059SNeal Cardwell switch (cond->family) { 742405c0059SNeal Cardwell case AF_UNSPEC: 743405c0059SNeal Cardwell addr_len = 0; 744405c0059SNeal Cardwell break; 745405c0059SNeal Cardwell case AF_INET: 746405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 747405c0059SNeal Cardwell break; 748405c0059SNeal Cardwell case AF_INET6: 749405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 750405c0059SNeal Cardwell break; 751405c0059SNeal Cardwell default: 752405c0059SNeal Cardwell return false; 753405c0059SNeal Cardwell } 754405c0059SNeal Cardwell *min_len += addr_len; 755405c0059SNeal Cardwell if (len < *min_len) 756405c0059SNeal Cardwell return false; 757405c0059SNeal Cardwell 758405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 759405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 760405c0059SNeal Cardwell return false; 761405c0059SNeal Cardwell 762405c0059SNeal Cardwell return true; 763405c0059SNeal Cardwell } 764405c0059SNeal Cardwell 7655e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 766e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 7675e1f5420SNeal Cardwell int len, int *min_len) 7685e1f5420SNeal Cardwell { 7695e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 7705e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 7715e1f5420SNeal Cardwell if (len < *min_len) 7725e1f5420SNeal Cardwell return false; 7735e1f5420SNeal Cardwell return true; 7745e1f5420SNeal Cardwell } 7755e1f5420SNeal Cardwell 776a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len, 777a52e95abSLorenzo Colitti int *min_len) 778a8c2190eSArnaldo Carvalho de Melo { 779a52e95abSLorenzo Colitti *min_len += sizeof(struct inet_diag_markcond); 780a52e95abSLorenzo Colitti return len >= *min_len; 781a52e95abSLorenzo Colitti } 782a52e95abSLorenzo Colitti 783a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr, 784a52e95abSLorenzo Colitti const struct sk_buff *skb) 785a52e95abSLorenzo Colitti { 786a52e95abSLorenzo Colitti bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); 787627cc4adSLorenzo Colitti const void *bytecode, *bc; 788627cc4adSLorenzo Colitti int bytecode_len, len; 789627cc4adSLorenzo Colitti 790627cc4adSLorenzo Colitti if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) 791627cc4adSLorenzo Colitti return -EINVAL; 792627cc4adSLorenzo Colitti 793627cc4adSLorenzo Colitti bytecode = bc = nla_data(attr); 794627cc4adSLorenzo Colitti len = bytecode_len = nla_len(attr); 795a8c2190eSArnaldo Carvalho de Melo 796a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 797405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 798e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 799a8c2190eSArnaldo Carvalho de Melo 800a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 801a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 802a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 803405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 804405c0059SNeal Cardwell return -EINVAL; 8055e1f5420SNeal Cardwell break; 806637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: 807637c841dSDavid Ahern if (!valid_devcond(bc, len, &min_len)) 808637c841dSDavid Ahern return -EINVAL; 809637c841dSDavid Ahern break; 810bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 811a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 812a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 813bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 814a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 815a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 8165e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 817a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 818a8c2190eSArnaldo Carvalho de Melo break; 819a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: 820a52e95abSLorenzo Colitti if (!net_admin) 821a52e95abSLorenzo Colitti return -EPERM; 822a52e95abSLorenzo Colitti if (!valid_markcond(bc, len, &min_len)) 823a52e95abSLorenzo Colitti return -EINVAL; 824a52e95abSLorenzo Colitti break; 8255e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 8265e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 827a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 828a8c2190eSArnaldo Carvalho de Melo break; 829a8c2190eSArnaldo Carvalho de Melo default: 830a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 831a8c2190eSArnaldo Carvalho de Melo } 8325e1f5420SNeal Cardwell 8335e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 8345e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 8355e1f5420SNeal Cardwell return -EINVAL; 8365e1f5420SNeal Cardwell if (op->no < len && 8375e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 8385e1f5420SNeal Cardwell return -EINVAL; 8395e1f5420SNeal Cardwell } 8405e1f5420SNeal Cardwell 841405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 842eeb14972SEric Dumazet return -EINVAL; 843a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 844a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 845a8c2190eSArnaldo Carvalho de Melo } 846a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 847a8c2190eSArnaldo Carvalho de Melo } 848a8c2190eSArnaldo Carvalho de Melo 849dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 850dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 85137f352b5SPavel Emelyanov struct netlink_callback *cb, 85234160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 853d545cacaSLorenzo Colitti const struct nlattr *bc, 854d545cacaSLorenzo Colitti bool net_admin) 855a8c2190eSArnaldo Carvalho de Melo { 8568d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 857a8c2190eSArnaldo Carvalho de Melo return 0; 858a8c2190eSArnaldo Carvalho de Melo 859a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 860e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 86115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 862d545cacaSLorenzo Colitti cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, 863d545cacaSLorenzo Colitti net_admin); 864a8c2190eSArnaldo Carvalho de Melo } 865a8c2190eSArnaldo Carvalho de Melo 86649612729SEric Dumazet static void twsk_build_assert(void) 86749612729SEric Dumazet { 86849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 86949612729SEric Dumazet offsetof(struct sock, sk_family)); 87049612729SEric Dumazet 87149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 87249612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 87349612729SEric Dumazet 87449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 87549612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 87649612729SEric Dumazet 87749612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 87849612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 87949612729SEric Dumazet 88049612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 88149612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 88249612729SEric Dumazet 88349612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 88449612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 88549612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 88649612729SEric Dumazet 88749612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 88849612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 88949612729SEric Dumazet #endif 89049612729SEric Dumazet } 89149612729SEric Dumazet 8921942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 893e31c5e0eSEric Dumazet struct netlink_callback *cb, 89434160ea3SEric Dumazet const struct inet_diag_req_v2 *r, struct nlattr *bc) 895a8c2190eSArnaldo Carvalho de Melo { 896d545cacaSLorenzo Colitti bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); 89767db3e4bSEric Dumazet struct net *net = sock_net(skb->sk); 89867db3e4bSEric Dumazet u32 idiag_states = r->idiag_states; 89967db3e4bSEric Dumazet int i, num, s_i, s_num; 90067db3e4bSEric Dumazet struct sock *sk; 901a8c2190eSArnaldo Carvalho de Melo 902079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 903079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 904a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 905a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 906a8c2190eSArnaldo Carvalho de Melo 907a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 9089652dc2eSEric Dumazet if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) 909a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 910a8c2190eSArnaldo Carvalho de Melo 911a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 9125caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 913a8c2190eSArnaldo Carvalho de Melo 914a8c2190eSArnaldo Carvalho de Melo num = 0; 9155caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 9169652dc2eSEric Dumazet spin_lock(&ilb->lock); 9173b24d854SEric Dumazet sk_for_each(sk, &ilb->head) { 918a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 919a8c2190eSArnaldo Carvalho de Melo 92051d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 92151d7cccfSAndrey Vagin continue; 92251d7cccfSAndrey Vagin 923a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 924a8c2190eSArnaldo Carvalho de Melo num++; 925a8c2190eSArnaldo Carvalho de Melo continue; 926a8c2190eSArnaldo Carvalho de Melo } 927a8c2190eSArnaldo Carvalho de Melo 928d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 929d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 930d23deaa0SPavel Emelyanov goto next_listen; 931d23deaa0SPavel Emelyanov 932c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 933a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 934a8c2190eSArnaldo Carvalho de Melo goto next_listen; 935a8c2190eSArnaldo Carvalho de Melo 936d545cacaSLorenzo Colitti if (inet_csk_diag_dump(sk, skb, cb, r, 937d545cacaSLorenzo Colitti bc, net_admin) < 0) { 9389652dc2eSEric Dumazet spin_unlock(&ilb->lock); 939a8c2190eSArnaldo Carvalho de Melo goto done; 940a8c2190eSArnaldo Carvalho de Melo } 941a8c2190eSArnaldo Carvalho de Melo 942a8c2190eSArnaldo Carvalho de Melo next_listen: 943a8c2190eSArnaldo Carvalho de Melo ++num; 944a8c2190eSArnaldo Carvalho de Melo } 9459652dc2eSEric Dumazet spin_unlock(&ilb->lock); 946a8c2190eSArnaldo Carvalho de Melo 947a8c2190eSArnaldo Carvalho de Melo s_num = 0; 948a8c2190eSArnaldo Carvalho de Melo } 949a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 950a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 951a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 952a8c2190eSArnaldo Carvalho de Melo } 953a8c2190eSArnaldo Carvalho de Melo 954079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 955efb3cb42SPavel Emelyanov goto out; 956a8c2190eSArnaldo Carvalho de Melo 95767db3e4bSEric Dumazet #define SKARR_SZ 16 958f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 959a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 9607e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 9613ab5aee7SEric Dumazet struct hlist_nulls_node *node; 96267db3e4bSEric Dumazet struct sock *sk_arr[SKARR_SZ]; 96367db3e4bSEric Dumazet int num_arr[SKARR_SZ]; 96467db3e4bSEric Dumazet int idx, accum, res; 9656be547a6SAndi Kleen 96605dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 9676be547a6SAndi Kleen continue; 9686be547a6SAndi Kleen 969a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 970a8c2190eSArnaldo Carvalho de Melo s_num = 0; 971a8c2190eSArnaldo Carvalho de Melo 97267db3e4bSEric Dumazet next_chunk: 97367db3e4bSEric Dumazet num = 0; 97467db3e4bSEric Dumazet accum = 0; 9757e3aab4aSDavid S. Miller spin_lock_bh(lock); 9763ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 97767db3e4bSEric Dumazet int state; 978a8c2190eSArnaldo Carvalho de Melo 97951d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 98051d7cccfSAndrey Vagin continue; 981a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 982a8c2190eSArnaldo Carvalho de Melo goto next_normal; 98370315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 98470315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 985079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 986a8c2190eSArnaldo Carvalho de Melo goto next_normal; 987d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 988d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 989d23deaa0SPavel Emelyanov goto next_normal; 99005dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 991a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 992a8c2190eSArnaldo Carvalho de Melo goto next_normal; 99305dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 9944e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 995a8c2190eSArnaldo Carvalho de Melo goto next_normal; 996a58917f5SEric Dumazet twsk_build_assert(); 997a58917f5SEric Dumazet 998a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 999a58917f5SEric Dumazet goto next_normal; 1000a58917f5SEric Dumazet 100167db3e4bSEric Dumazet sock_hold(sk); 100267db3e4bSEric Dumazet num_arr[accum] = num; 100367db3e4bSEric Dumazet sk_arr[accum] = sk; 100467db3e4bSEric Dumazet if (++accum == SKARR_SZ) 100567db3e4bSEric Dumazet break; 100667db3e4bSEric Dumazet next_normal: 100767db3e4bSEric Dumazet ++num; 100867db3e4bSEric Dumazet } 100967db3e4bSEric Dumazet spin_unlock_bh(lock); 101067db3e4bSEric Dumazet res = 0; 101167db3e4bSEric Dumazet for (idx = 0; idx < accum; idx++) { 101267db3e4bSEric Dumazet if (res >= 0) { 101367db3e4bSEric Dumazet res = sk_diag_fill(sk_arr[idx], skb, r, 1014a58917f5SEric Dumazet sk_user_ns(NETLINK_CB(cb->skb).sk), 1015a58917f5SEric Dumazet NETLINK_CB(cb->skb).portid, 1016a58917f5SEric Dumazet cb->nlh->nlmsg_seq, NLM_F_MULTI, 1017d545cacaSLorenzo Colitti cb->nlh, net_admin); 101867db3e4bSEric Dumazet if (res < 0) 101967db3e4bSEric Dumazet num = num_arr[idx]; 1020a8c2190eSArnaldo Carvalho de Melo } 102167db3e4bSEric Dumazet sock_gen_put(sk_arr[idx]); 1022a8c2190eSArnaldo Carvalho de Melo } 102367db3e4bSEric Dumazet if (res < 0) 102467db3e4bSEric Dumazet break; 1025acffb584SEric Dumazet cond_resched(); 102667db3e4bSEric Dumazet if (accum == SKARR_SZ) { 102767db3e4bSEric Dumazet s_num = num + 1; 102867db3e4bSEric Dumazet goto next_chunk; 102967db3e4bSEric Dumazet } 1030a8c2190eSArnaldo Carvalho de Melo } 1031a8c2190eSArnaldo Carvalho de Melo 1032a8c2190eSArnaldo Carvalho de Melo done: 1033a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 1034a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 1035efb3cb42SPavel Emelyanov out: 1036efb3cb42SPavel Emelyanov ; 1037efb3cb42SPavel Emelyanov } 10381942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 1039efb3cb42SPavel Emelyanov 1040efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 104134160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 1042e31c5e0eSEric Dumazet struct nlattr *bc) 1043efb3cb42SPavel Emelyanov { 1044efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 1045cacb6ba0SCyrill Gorcunov int err = 0; 1046efb3cb42SPavel Emelyanov 1047efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 1048efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 10491942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 1050cacb6ba0SCyrill Gorcunov else 1051cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 1052d523a328SHerbert Xu inet_diag_unlock_handler(handler); 1053efb3cb42SPavel Emelyanov 1054cacb6ba0SCyrill Gorcunov return err ? : skb->len; 1055a8c2190eSArnaldo Carvalho de Melo } 1056a8c2190eSArnaldo Carvalho de Melo 105725c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 105825c4cd2bSPavel Emelyanov { 1059c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 1060e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 106125c4cd2bSPavel Emelyanov 106225c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 106325c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 106425c4cd2bSPavel Emelyanov 1065d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 106625c4cd2bSPavel Emelyanov } 106725c4cd2bSPavel Emelyanov 1068e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 1069a029fe26SPavel Emelyanov { 1070a029fe26SPavel Emelyanov switch (type) { 1071a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 1072a029fe26SPavel Emelyanov return IPPROTO_TCP; 1073a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 1074a029fe26SPavel Emelyanov return IPPROTO_DCCP; 1075a029fe26SPavel Emelyanov default: 1076a029fe26SPavel Emelyanov return 0; 1077a029fe26SPavel Emelyanov } 1078a029fe26SPavel Emelyanov } 1079a029fe26SPavel Emelyanov 1080e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 1081e31c5e0eSEric Dumazet struct netlink_callback *cb) 108225c4cd2bSPavel Emelyanov { 1083d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1084e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 1085c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 108625c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 108725c4cd2bSPavel Emelyanov 1088d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 108925c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 109025c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 109125c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 109225c4cd2bSPavel Emelyanov req.id = rc->id; 109325c4cd2bSPavel Emelyanov 109425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 109525c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 109625c4cd2bSPavel Emelyanov 109725c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 109825c4cd2bSPavel Emelyanov } 109925c4cd2bSPavel Emelyanov 1100fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1101fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1102fe50ce28SPavel Emelyanov { 1103d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1104c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1105fe50ce28SPavel Emelyanov 1106fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1107fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1108fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1109fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1110fe50ce28SPavel Emelyanov req.id = rc->id; 1111fe50ce28SPavel Emelyanov 11126eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req); 1113fe50ce28SPavel Emelyanov } 1114fe50ce28SPavel Emelyanov 11158d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1116a8c2190eSArnaldo Carvalho de Melo { 11173b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 111851d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1119a8c2190eSArnaldo Carvalho de Melo 1120ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1121ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1122ead592baSThomas Graf return -EINVAL; 1123a8c2190eSArnaldo Carvalho de Melo 1124b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 1125ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 1126ead592baSThomas Graf struct nlattr *attr; 1127627cc4adSLorenzo Colitti int err; 1128ead592baSThomas Graf 1129ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 1130ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 1131a52e95abSLorenzo Colitti err = inet_diag_bc_audit(attr, skb); 1132627cc4adSLorenzo Colitti if (err) 1133627cc4adSLorenzo Colitti return err; 1134a8c2190eSArnaldo Carvalho de Melo } 113580d326faSPablo Neira Ayuso { 113680d326faSPablo Neira Ayuso struct netlink_dump_control c = { 113780d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 113880d326faSPablo Neira Ayuso }; 113951d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 114080d326faSPablo Neira Ayuso } 1141a8c2190eSArnaldo Carvalho de Melo } 1142ead592baSThomas Graf 1143fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1144a8c2190eSArnaldo Carvalho de Melo } 1145a8c2190eSArnaldo Carvalho de Melo 11466eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 1147d366477aSPavel Emelyanov { 1148c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 114951d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1150d366477aSPavel Emelyanov 1151d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1152d366477aSPavel Emelyanov return -EINVAL; 1153d366477aSPavel Emelyanov 11546eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 11556eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) { 115625c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 115725c4cd2bSPavel Emelyanov struct nlattr *attr; 1158627cc4adSLorenzo Colitti int err; 1159e31c5e0eSEric Dumazet 116025c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 116125c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 1162a52e95abSLorenzo Colitti err = inet_diag_bc_audit(attr, skb); 1163627cc4adSLorenzo Colitti if (err) 1164627cc4adSLorenzo Colitti return err; 116525c4cd2bSPavel Emelyanov } 116680d326faSPablo Neira Ayuso { 116780d326faSPablo Neira Ayuso struct netlink_dump_control c = { 116880d326faSPablo Neira Ayuso .dump = inet_diag_dump, 116980d326faSPablo Neira Ayuso }; 117051d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 117180d326faSPablo Neira Ayuso } 1172d366477aSPavel Emelyanov } 1173d366477aSPavel Emelyanov 11746eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); 1175d366477aSPavel Emelyanov } 1176d366477aSPavel Emelyanov 117735ac838aSCraig Gallek static 117835ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 117935ac838aSCraig Gallek { 118035ac838aSCraig Gallek const struct inet_diag_handler *handler; 118135ac838aSCraig Gallek struct nlmsghdr *nlh; 118235ac838aSCraig Gallek struct nlattr *attr; 118335ac838aSCraig Gallek struct inet_diag_msg *r; 118435ac838aSCraig Gallek void *info = NULL; 118535ac838aSCraig Gallek int err = 0; 118635ac838aSCraig Gallek 118735ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 118835ac838aSCraig Gallek if (!nlh) 118935ac838aSCraig Gallek return -ENOMEM; 119035ac838aSCraig Gallek 119135ac838aSCraig Gallek r = nlmsg_data(nlh); 119235ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 119335ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1194e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1195e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 119635ac838aSCraig Gallek r->idiag_state = sk->sk_state; 119735ac838aSCraig Gallek 119835ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 119935ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 120035ac838aSCraig Gallek return err; 120135ac838aSCraig Gallek } 120235ac838aSCraig Gallek 120335ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 120435ac838aSCraig Gallek if (IS_ERR(handler)) { 120535ac838aSCraig Gallek inet_diag_unlock_handler(handler); 120635ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 120735ac838aSCraig Gallek return PTR_ERR(handler); 120835ac838aSCraig Gallek } 120935ac838aSCraig Gallek 121035ac838aSCraig Gallek attr = handler->idiag_info_size 12116ed46d12SNicolas Dichtel ? nla_reserve_64bit(skb, INET_DIAG_INFO, 12126ed46d12SNicolas Dichtel handler->idiag_info_size, 12136ed46d12SNicolas Dichtel INET_DIAG_PAD) 121435ac838aSCraig Gallek : NULL; 121535ac838aSCraig Gallek if (attr) 121635ac838aSCraig Gallek info = nla_data(attr); 121735ac838aSCraig Gallek 121835ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 121935ac838aSCraig Gallek inet_diag_unlock_handler(handler); 122035ac838aSCraig Gallek 122135ac838aSCraig Gallek nlmsg_end(skb, nlh); 122235ac838aSCraig Gallek return 0; 122335ac838aSCraig Gallek } 122435ac838aSCraig Gallek 12258dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1226d366477aSPavel Emelyanov .family = AF_INET, 12276eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 122835ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 12296eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1230d366477aSPavel Emelyanov }; 1231d366477aSPavel Emelyanov 12328dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1233d366477aSPavel Emelyanov .family = AF_INET6, 12346eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 123535ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 12366eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1237d366477aSPavel Emelyanov }; 1238d366477aSPavel Emelyanov 1239a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1240a8c2190eSArnaldo Carvalho de Melo { 1241a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1242a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1243a8c2190eSArnaldo Carvalho de Melo 1244f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1245a8c2190eSArnaldo Carvalho de Melo goto out; 1246a8c2190eSArnaldo Carvalho de Melo 1247d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1248a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1249e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1250a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1251a8c2190eSArnaldo Carvalho de Melo err = 0; 1252a8c2190eSArnaldo Carvalho de Melo } 1253d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1254a8c2190eSArnaldo Carvalho de Melo out: 1255a8c2190eSArnaldo Carvalho de Melo return err; 1256a8c2190eSArnaldo Carvalho de Melo } 1257a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1258a8c2190eSArnaldo Carvalho de Melo 1259a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1260a8c2190eSArnaldo Carvalho de Melo { 1261a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1262a8c2190eSArnaldo Carvalho de Melo 1263f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1264a8c2190eSArnaldo Carvalho de Melo return; 1265a8c2190eSArnaldo Carvalho de Melo 1266d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1267a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1268d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1269a8c2190eSArnaldo Carvalho de Melo } 1270a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1271a8c2190eSArnaldo Carvalho de Melo 1272a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1273a8c2190eSArnaldo Carvalho de Melo { 1274f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1275a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1276a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1277a8c2190eSArnaldo Carvalho de Melo 12780da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1279a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1280a8c2190eSArnaldo Carvalho de Melo goto out; 1281a8c2190eSArnaldo Carvalho de Melo 1282d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1283d366477aSPavel Emelyanov if (err) 1284d366477aSPavel Emelyanov goto out_free_nl; 1285d366477aSPavel Emelyanov 1286d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1287d366477aSPavel Emelyanov if (err) 1288d366477aSPavel Emelyanov goto out_free_inet; 1289d366477aSPavel Emelyanov 12908ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1291a8c2190eSArnaldo Carvalho de Melo out: 1292a8c2190eSArnaldo Carvalho de Melo return err; 1293d366477aSPavel Emelyanov 1294d366477aSPavel Emelyanov out_free_inet: 1295d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1296d366477aSPavel Emelyanov out_free_nl: 1297a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1298a8c2190eSArnaldo Carvalho de Melo goto out; 1299a8c2190eSArnaldo Carvalho de Melo } 1300a8c2190eSArnaldo Carvalho de Melo 1301a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1302a8c2190eSArnaldo Carvalho de Melo { 1303d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1304d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 13058ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1306a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1307a8c2190eSArnaldo Carvalho de Melo } 1308a8c2190eSArnaldo Carvalho de Melo 1309a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1310a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1311a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1312aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1313aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1314