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]) 56aec8dc62SPavel Emelyanov request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK, 57aec8dc62SPavel Emelyanov NETLINK_SOCK_DIAG, AF_INET, proto); 58d523a328SHerbert Xu 59d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 60f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 61d523a328SHerbert Xu return ERR_PTR(-ENOENT); 62d523a328SHerbert Xu 63f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 64d523a328SHerbert Xu } 65d523a328SHerbert Xu 66e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 67d523a328SHerbert Xu { 68d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 69d523a328SHerbert Xu } 70d523a328SHerbert Xu 71cb2050a7SXin Long void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) 72a4458343SEric Dumazet { 73a4458343SEric Dumazet r->idiag_family = sk->sk_family; 74a4458343SEric Dumazet 75a4458343SEric Dumazet r->id.idiag_sport = htons(sk->sk_num); 76a4458343SEric Dumazet r->id.idiag_dport = sk->sk_dport; 77a4458343SEric Dumazet r->id.idiag_if = sk->sk_bound_dev_if; 78a4458343SEric Dumazet sock_diag_save_cookie(sk, r->id.idiag_cookie); 79a4458343SEric Dumazet 80a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 81a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 82a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 83a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 84a4458343SEric Dumazet } else 85a4458343SEric Dumazet #endif 86a4458343SEric Dumazet { 87a4458343SEric Dumazet memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 88a4458343SEric Dumazet memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 89a4458343SEric Dumazet 90a4458343SEric Dumazet r->id.idiag_src[0] = sk->sk_rcv_saddr; 91a4458343SEric Dumazet r->id.idiag_dst[0] = sk->sk_daddr; 92a4458343SEric Dumazet } 93a4458343SEric Dumazet } 94cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill); 95a4458343SEric Dumazet 96c8e2c80dSEric Dumazet static size_t inet_sk_attr_size(void) 97c8e2c80dSEric Dumazet { 98c8e2c80dSEric Dumazet return nla_total_size(sizeof(struct tcp_info)) 99c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_SHUTDOWN */ 100c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TOS */ 101c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TCLASS */ 102d545cacaSLorenzo Colitti + nla_total_size(4) /* INET_DIAG_MARK */ 103c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_meminfo)) 104c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_msg)) 105c8e2c80dSEric Dumazet + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) 106c8e2c80dSEric Dumazet + nla_total_size(TCP_CA_NAME_MAX) 107c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct tcpvegas_info)) 108c8e2c80dSEric Dumazet + 64; 109c8e2c80dSEric Dumazet } 110c8e2c80dSEric Dumazet 111cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, 112cb2050a7SXin Long struct inet_diag_msg *r, int ext, 113d545cacaSLorenzo Colitti struct user_namespace *user_ns, 114d545cacaSLorenzo Colitti bool net_admin) 115a8c2190eSArnaldo Carvalho de Melo { 116a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 117a8c2190eSArnaldo Carvalho de Melo 118e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 119e4e541a8SPavel Emelyanov goto errout; 120e4e541a8SPavel Emelyanov 121717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 122717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 123717b6d83SMaciej Żenczykowski */ 124717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 1256e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 1266e277ed5SThomas Graf goto errout; 127717b6d83SMaciej Żenczykowski 128dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 129a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 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; 13420462155SPhil Sutter 1358220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 1368220ea23SPhil Sutter nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) 13720462155SPhil Sutter goto errout; 138a8c2190eSArnaldo Carvalho de Melo } 139a8c2190eSArnaldo Carvalho de Melo #endif 140a8c2190eSArnaldo Carvalho de Melo 141d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark)) 142d545cacaSLorenzo Colitti goto errout; 143d545cacaSLorenzo Colitti 144d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 1453c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1463c4d05c8SPavel Emelyanov 147cb2050a7SXin Long return 0; 148cb2050a7SXin Long errout: 149cb2050a7SXin Long return 1; 150cb2050a7SXin Long } 151cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill); 152cb2050a7SXin Long 153cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 154cb2050a7SXin Long struct sk_buff *skb, const struct inet_diag_req_v2 *req, 155cb2050a7SXin Long struct user_namespace *user_ns, 156cb2050a7SXin Long u32 portid, u32 seq, u16 nlmsg_flags, 157d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, 158d545cacaSLorenzo Colitti bool net_admin) 159cb2050a7SXin Long { 160cb2050a7SXin Long const struct tcp_congestion_ops *ca_ops; 161cb2050a7SXin Long const struct inet_diag_handler *handler; 162cb2050a7SXin Long int ext = req->idiag_ext; 163cb2050a7SXin Long struct inet_diag_msg *r; 164cb2050a7SXin Long struct nlmsghdr *nlh; 165cb2050a7SXin Long struct nlattr *attr; 166cb2050a7SXin Long void *info = NULL; 167cb2050a7SXin Long 168cb2050a7SXin Long handler = inet_diag_table[req->sdiag_protocol]; 169cb2050a7SXin Long BUG_ON(!handler); 170cb2050a7SXin Long 171cb2050a7SXin Long nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 172cb2050a7SXin Long nlmsg_flags); 173cb2050a7SXin Long if (!nlh) 174cb2050a7SXin Long return -EMSGSIZE; 175cb2050a7SXin Long 176cb2050a7SXin Long r = nlmsg_data(nlh); 177cb2050a7SXin Long BUG_ON(!sk_fullsock(sk)); 178cb2050a7SXin Long 179cb2050a7SXin Long inet_diag_msg_common_fill(r, sk); 180cb2050a7SXin Long r->idiag_state = sk->sk_state; 181cb2050a7SXin Long r->idiag_timer = 0; 182cb2050a7SXin Long r->idiag_retrans = 0; 183cb2050a7SXin Long 184d545cacaSLorenzo Colitti if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin)) 185cb2050a7SXin Long goto errout; 186cb2050a7SXin Long 1876e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 1886e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 1896e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 1906e277ed5SThomas Graf .idiag_wmem = sk->sk_wmem_queued, 1916e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 1926e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 1936e277ed5SThomas Graf }; 1946e277ed5SThomas Graf 1956e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 1966e277ed5SThomas Graf goto errout; 1973c4d05c8SPavel Emelyanov } 1983c4d05c8SPavel Emelyanov 199c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 200c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 2016e277ed5SThomas Graf goto errout; 202c0636faaSPavel Emelyanov 203432490f9SCyrill Gorcunov /* 204432490f9SCyrill Gorcunov * RAW sockets might have user-defined protocols assigned, 205432490f9SCyrill Gorcunov * so report the one supplied on socket creation. 206432490f9SCyrill Gorcunov */ 207432490f9SCyrill Gorcunov if (sk->sk_type == SOCK_RAW) { 208432490f9SCyrill Gorcunov if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) 209432490f9SCyrill Gorcunov goto errout; 210432490f9SCyrill Gorcunov } 211432490f9SCyrill Gorcunov 212e31c5e0eSEric Dumazet if (!icsk) { 21362ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 2143c4d05c8SPavel Emelyanov goto out; 2153c4d05c8SPavel Emelyanov } 2163c4d05c8SPavel Emelyanov 2176ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 2186ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || 219*57dde7f7SYuchung Cheng icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 2206ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 221a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 222a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 223b7de529cSXin Long r->idiag_expires = 224b7de529cSXin Long jiffies_to_msecs(icsk->icsk_timeout - jiffies); 225a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 226a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 227a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 228b7de529cSXin Long r->idiag_expires = 229b7de529cSXin Long jiffies_to_msecs(icsk->icsk_timeout - jiffies); 230a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 231a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 232a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 233b7de529cSXin Long r->idiag_expires = 234b7de529cSXin Long jiffies_to_msecs(sk->sk_timer.expires - jiffies); 235a8c2190eSArnaldo Carvalho de Melo } else { 236a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 237a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 238a8c2190eSArnaldo Carvalho de Melo } 239a8c2190eSArnaldo Carvalho de Melo 2403fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 2416ed46d12SNicolas Dichtel attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 2426ed46d12SNicolas Dichtel handler->idiag_info_size, 2436ed46d12SNicolas Dichtel INET_DIAG_PAD); 2446e277ed5SThomas Graf if (!attr) 2456e277ed5SThomas Graf goto errout; 246a8c2190eSArnaldo Carvalho de Melo 2476e277ed5SThomas Graf info = nla_data(attr); 248a8c2190eSArnaldo Carvalho de Melo } 249a8c2190eSArnaldo Carvalho de Melo 250521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) { 251521f1cf1SEric Dumazet int err = 0; 252521f1cf1SEric Dumazet 253521f1cf1SEric Dumazet rcu_read_lock(); 254521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 255521f1cf1SEric Dumazet if (ca_ops) 256521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 257521f1cf1SEric Dumazet rcu_read_unlock(); 258521f1cf1SEric Dumazet if (err < 0) 2596e277ed5SThomas Graf goto errout; 260521f1cf1SEric Dumazet } 2616e277ed5SThomas Graf 262a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 263a8c2190eSArnaldo Carvalho de Melo 264521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) { 26564f40ff5SEric Dumazet union tcp_cc_info info; 26664f40ff5SEric Dumazet size_t sz = 0; 26764f40ff5SEric Dumazet int attr; 268521f1cf1SEric Dumazet 269521f1cf1SEric Dumazet rcu_read_lock(); 270521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 271521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info) 27264f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info); 273521f1cf1SEric Dumazet rcu_read_unlock(); 27464f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0) 275521f1cf1SEric Dumazet goto errout; 276521f1cf1SEric Dumazet } 277a8c2190eSArnaldo Carvalho de Melo 2783c4d05c8SPavel Emelyanov out: 279053c095aSJohannes Berg nlmsg_end(skb, nlh); 280053c095aSJohannes Berg return 0; 281a8c2190eSArnaldo Carvalho de Melo 2826e277ed5SThomas Graf errout: 2836e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 28426932566SPatrick McHardy return -EMSGSIZE; 285a8c2190eSArnaldo Carvalho de Melo } 2863c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 2873c4d05c8SPavel Emelyanov 2883c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk, 289e31c5e0eSEric Dumazet struct sk_buff *skb, 29034160ea3SEric Dumazet const struct inet_diag_req_v2 *req, 291d06ca956SEric W. Biederman struct user_namespace *user_ns, 29215e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 293d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, 294d545cacaSLorenzo Colitti bool net_admin) 2953c4d05c8SPavel Emelyanov { 296d545cacaSLorenzo Colitti return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns, 297d545cacaSLorenzo Colitti portid, seq, nlmsg_flags, unlh, net_admin); 2983c4d05c8SPavel Emelyanov } 299a8c2190eSArnaldo Carvalho de Melo 30033cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk, 301e31c5e0eSEric Dumazet struct sk_buff *skb, 30215e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 303c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 304c7d58aabSArnaldo Carvalho de Melo { 30533cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk); 306c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 3076e277ed5SThomas Graf struct nlmsghdr *nlh; 308789f558cSEric Dumazet long tmo; 309c7d58aabSArnaldo Carvalho de Melo 31015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 3116e277ed5SThomas Graf nlmsg_flags); 3126e277ed5SThomas Graf if (!nlh) 313d106352dSDavid S. Miller return -EMSGSIZE; 314d106352dSDavid S. Miller 315d106352dSDavid S. Miller r = nlmsg_data(nlh); 316c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 317c7d58aabSArnaldo Carvalho de Melo 318789f558cSEric Dumazet tmo = tw->tw_timer.expires - jiffies; 319c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 320c7d58aabSArnaldo Carvalho de Melo tmo = 0; 321c7d58aabSArnaldo Carvalho de Melo 322a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 323c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 324b1aac815SDaniel Borkmann 325c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 326c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 32796f817feSEric Dumazet r->idiag_expires = jiffies_to_msecs(tmo); 328c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 329c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 330c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 331c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 3326e277ed5SThomas Graf 333053c095aSJohannes Berg nlmsg_end(skb, nlh); 334053c095aSJohannes Berg return 0; 335c7d58aabSArnaldo Carvalho de Melo } 336c7d58aabSArnaldo Carvalho de Melo 337a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 338a58917f5SEric Dumazet u32 portid, u32 seq, u16 nlmsg_flags, 339d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, bool net_admin) 340a58917f5SEric Dumazet { 341d545cacaSLorenzo Colitti struct request_sock *reqsk = inet_reqsk(sk); 342a58917f5SEric Dumazet struct inet_diag_msg *r; 343a58917f5SEric Dumazet struct nlmsghdr *nlh; 344a58917f5SEric Dumazet long tmo; 345a58917f5SEric Dumazet 346a58917f5SEric Dumazet nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 347a58917f5SEric Dumazet nlmsg_flags); 348a58917f5SEric Dumazet if (!nlh) 349a58917f5SEric Dumazet return -EMSGSIZE; 350a58917f5SEric Dumazet 351a58917f5SEric Dumazet r = nlmsg_data(nlh); 352a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk); 353a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV; 354a58917f5SEric Dumazet r->idiag_timer = 1; 355d545cacaSLorenzo Colitti r->idiag_retrans = reqsk->num_retrans; 356a58917f5SEric Dumazet 357a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 358a58917f5SEric Dumazet offsetof(struct sock, sk_cookie)); 359a58917f5SEric Dumazet 360fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 361a58917f5SEric Dumazet r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0; 362a58917f5SEric Dumazet r->idiag_rqueue = 0; 363a58917f5SEric Dumazet r->idiag_wqueue = 0; 364a58917f5SEric Dumazet r->idiag_uid = 0; 365a58917f5SEric Dumazet r->idiag_inode = 0; 366a58917f5SEric Dumazet 367d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 368d545cacaSLorenzo Colitti inet_rsk(reqsk)->ir_mark)) 369d545cacaSLorenzo Colitti return -EMSGSIZE; 370d545cacaSLorenzo Colitti 371a58917f5SEric Dumazet nlmsg_end(skb, nlh); 372a58917f5SEric Dumazet return 0; 373a58917f5SEric Dumazet } 374a58917f5SEric Dumazet 375dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 37634160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 377d06ca956SEric W. Biederman struct user_namespace *user_ns, 37815e47304SEric W. Biederman u32 portid, u32 seq, u16 nlmsg_flags, 379d545cacaSLorenzo Colitti const struct nlmsghdr *unlh, bool net_admin) 380dff2c035SArnaldo Carvalho de Melo { 381dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 382a58917f5SEric Dumazet return inet_twsk_diag_fill(sk, skb, portid, seq, 383a58917f5SEric Dumazet nlmsg_flags, unlh); 384a58917f5SEric Dumazet 385a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) 386a58917f5SEric Dumazet return inet_req_diag_fill(sk, skb, portid, seq, 387d545cacaSLorenzo Colitti nlmsg_flags, unlh, net_admin); 388efe4208fSEric Dumazet 389efe4208fSEric Dumazet return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, 390d545cacaSLorenzo Colitti nlmsg_flags, unlh, net_admin); 391dff2c035SArnaldo Carvalho de Melo } 392dff2c035SArnaldo Carvalho de Melo 393b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net, 394b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo, 39534160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 396a8c2190eSArnaldo Carvalho de Melo { 397e31c5e0eSEric Dumazet struct sock *sk; 398a8c2190eSArnaldo Carvalho de Melo 3992d331915SEric Dumazet rcu_read_lock(); 400e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 401a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 402a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 403a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 404dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4057c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) { 4067c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 4077c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 408a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 4097c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3], 4107c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if); 4117c130672SEric Dumazet else 412a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0, 413a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 414a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 415a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 416a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 417a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 4187c130672SEric Dumazet } 419a8c2190eSArnaldo Carvalho de Melo #endif 4202d331915SEric Dumazet else { 4212d331915SEric Dumazet rcu_read_unlock(); 422b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL); 4232d331915SEric Dumazet } 4242d331915SEric Dumazet rcu_read_unlock(); 425e31c5e0eSEric Dumazet if (!sk) 426b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 427a8c2190eSArnaldo Carvalho de Melo 428b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 429b613f56eSLorenzo Colitti sock_gen_put(sk); 430b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 431b613f56eSLorenzo Colitti } 432b613f56eSLorenzo Colitti 433b613f56eSLorenzo Colitti return sk; 434b613f56eSLorenzo Colitti } 435b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 436b613f56eSLorenzo Colitti 437b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 438b613f56eSLorenzo Colitti struct sk_buff *in_skb, 439b613f56eSLorenzo Colitti const struct nlmsghdr *nlh, 440b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req) 441b613f56eSLorenzo Colitti { 442b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk); 443b613f56eSLorenzo Colitti struct sk_buff *rep; 444b613f56eSLorenzo Colitti struct sock *sk; 445b613f56eSLorenzo Colitti int err; 446b613f56eSLorenzo Colitti 447b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req); 448b613f56eSLorenzo Colitti if (IS_ERR(sk)) 449b613f56eSLorenzo Colitti return PTR_ERR(sk); 450a8c2190eSArnaldo Carvalho de Melo 451c8e2c80dSEric Dumazet rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL); 4526e277ed5SThomas Graf if (!rep) { 4536e277ed5SThomas Graf err = -ENOMEM; 454a8c2190eSArnaldo Carvalho de Melo goto out; 4556e277ed5SThomas Graf } 456a8c2190eSArnaldo Carvalho de Melo 457a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 458e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(in_skb).sk), 45915e47304SEric W. Biederman NETLINK_CB(in_skb).portid, 460d545cacaSLorenzo Colitti nlh->nlmsg_seq, 0, nlh, 461d545cacaSLorenzo Colitti netlink_net_capable(in_skb, CAP_NET_ADMIN)); 46226932566SPatrick McHardy if (err < 0) { 46326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 4646e277ed5SThomas Graf nlmsg_free(rep); 46526932566SPatrick McHardy goto out; 46626932566SPatrick McHardy } 46715e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 468a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 469a8c2190eSArnaldo Carvalho de Melo if (err > 0) 470a8c2190eSArnaldo Carvalho de Melo err = 0; 471a8c2190eSArnaldo Carvalho de Melo 472a8c2190eSArnaldo Carvalho de Melo out: 473c1d607ccSEric Dumazet if (sk) 474c1d607ccSEric Dumazet sock_gen_put(sk); 475c1d607ccSEric Dumazet 476476f7dbfSPavel Emelyanov return err; 477476f7dbfSPavel Emelyanov } 4781942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 479476f7dbfSPavel Emelyanov 4806eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 481476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 48234160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 483476f7dbfSPavel Emelyanov { 484476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 485476f7dbfSPavel Emelyanov int err; 486476f7dbfSPavel Emelyanov 487476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 488476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 489476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 4906eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DIAG_BY_FAMILY) 4911942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 4926eb5d2e0SLorenzo Colitti else if (cmd == SOCK_DESTROY && handler->destroy) 4936eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req); 4946eb5d2e0SLorenzo Colitti else 4956eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP; 496d523a328SHerbert Xu inet_diag_unlock_handler(handler); 497476f7dbfSPavel Emelyanov 498a8c2190eSArnaldo Carvalho de Melo return err; 499a8c2190eSArnaldo Carvalho de Melo } 500a8c2190eSArnaldo Carvalho de Melo 5019f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 502a8c2190eSArnaldo Carvalho de Melo { 503a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 504a8c2190eSArnaldo Carvalho de Melo 505a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 506a8c2190eSArnaldo Carvalho de Melo 507a8c2190eSArnaldo Carvalho de Melo if (words) { 508a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 509a8c2190eSArnaldo Carvalho de Melo return 0; 510a8c2190eSArnaldo Carvalho de Melo } 511a8c2190eSArnaldo Carvalho de Melo if (bits) { 5129f855299SAl Viro __be32 w1, w2; 5139f855299SAl Viro __be32 mask; 514a8c2190eSArnaldo Carvalho de Melo 515a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 516a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 517a8c2190eSArnaldo Carvalho de Melo 518a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 519a8c2190eSArnaldo Carvalho de Melo 520a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 521a8c2190eSArnaldo Carvalho de Melo return 0; 522a8c2190eSArnaldo Carvalho de Melo } 523a8c2190eSArnaldo Carvalho de Melo 524a8c2190eSArnaldo Carvalho de Melo return 1; 525a8c2190eSArnaldo Carvalho de Melo } 526a8c2190eSArnaldo Carvalho de Melo 52787c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 528a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 529a8c2190eSArnaldo Carvalho de Melo { 53087c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 53187c22ea5SPavel Emelyanov int len = nla_len(_bc); 53287c22ea5SPavel Emelyanov 533a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 534a8c2190eSArnaldo Carvalho de Melo int yes = 1; 535a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 536a8c2190eSArnaldo Carvalho de Melo 537a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 538a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 539a8c2190eSArnaldo Carvalho de Melo break; 540a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 541a8c2190eSArnaldo Carvalho de Melo yes = 0; 542a8c2190eSArnaldo Carvalho de Melo break; 543a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 544a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 545a8c2190eSArnaldo Carvalho de Melo break; 546a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 547b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 548a8c2190eSArnaldo Carvalho de Melo break; 549a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 550a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 551a8c2190eSArnaldo Carvalho de Melo break; 552a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 553a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 554a8c2190eSArnaldo Carvalho de Melo break; 555a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 556a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 557a8c2190eSArnaldo Carvalho de Melo break; 558a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 559a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 560e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 561e31c5e0eSEric Dumazet const __be32 *addr; 562a8c2190eSArnaldo Carvalho de Melo 563e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 564a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 565a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 566a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 567a8c2190eSArnaldo Carvalho de Melo yes = 0; 568a8c2190eSArnaldo Carvalho de Melo break; 569a8c2190eSArnaldo Carvalho de Melo } 570a8c2190eSArnaldo Carvalho de Melo 571a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 572a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 573a8c2190eSArnaldo Carvalho de Melo else 574a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 575a8c2190eSArnaldo Carvalho de Melo 576f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 577f67caec9SNeal Cardwell cond->family != entry->family) { 578a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 579a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 580a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 581a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 582f67caec9SNeal Cardwell bitstring_match(addr + 3, 583f67caec9SNeal Cardwell cond->addr, 584a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 585a8c2190eSArnaldo Carvalho de Melo break; 586a8c2190eSArnaldo Carvalho de Melo } 587a8c2190eSArnaldo Carvalho de Melo yes = 0; 588a8c2190eSArnaldo Carvalho de Melo break; 589a8c2190eSArnaldo Carvalho de Melo } 590f67caec9SNeal Cardwell 591f67caec9SNeal Cardwell if (cond->prefix_len == 0) 592f67caec9SNeal Cardwell break; 593f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 594f67caec9SNeal Cardwell cond->prefix_len)) 595f67caec9SNeal Cardwell break; 596f67caec9SNeal Cardwell yes = 0; 597f67caec9SNeal Cardwell break; 598f67caec9SNeal Cardwell } 599637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: { 600637c841dSDavid Ahern u32 ifindex; 601637c841dSDavid Ahern 602637c841dSDavid Ahern ifindex = *((const u32 *)(op + 1)); 603637c841dSDavid Ahern if (ifindex != entry->ifindex) 604637c841dSDavid Ahern yes = 0; 605637c841dSDavid Ahern break; 606637c841dSDavid Ahern } 607a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: { 608a52e95abSLorenzo Colitti struct inet_diag_markcond *cond; 609a52e95abSLorenzo Colitti 610a52e95abSLorenzo Colitti cond = (struct inet_diag_markcond *)(op + 1); 611a52e95abSLorenzo Colitti if ((entry->mark & cond->mask) != cond->mark) 612a52e95abSLorenzo Colitti yes = 0; 613a52e95abSLorenzo Colitti break; 614a52e95abSLorenzo Colitti } 615a8c2190eSArnaldo Carvalho de Melo } 616a8c2190eSArnaldo Carvalho de Melo 617a8c2190eSArnaldo Carvalho de Melo if (yes) { 618a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 619a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 620a8c2190eSArnaldo Carvalho de Melo } else { 621a8c2190eSArnaldo Carvalho de Melo len -= op->no; 622a8c2190eSArnaldo Carvalho de Melo bc += op->no; 623a8c2190eSArnaldo Carvalho de Melo } 624a8c2190eSArnaldo Carvalho de Melo } 625a02cec21SEric Dumazet return len == 0; 626a8c2190eSArnaldo Carvalho de Melo } 627a8c2190eSArnaldo Carvalho de Melo 628a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 629a4458343SEric Dumazet */ 630a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 631a4458343SEric Dumazet const struct sock *sk) 632a4458343SEric Dumazet { 633a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 634a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 635a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 636a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 637a4458343SEric Dumazet } else 638a4458343SEric Dumazet #endif 639a4458343SEric Dumazet { 640a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 641a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 642a4458343SEric Dumazet } 643a4458343SEric Dumazet } 644a4458343SEric Dumazet 6458d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 6468d07d151SPavel Emelyanov { 6478d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 648e31c5e0eSEric Dumazet struct inet_diag_entry entry; 6498d07d151SPavel Emelyanov 650e31c5e0eSEric Dumazet if (!bc) 6518d07d151SPavel Emelyanov return 1; 6528d07d151SPavel Emelyanov 6538d07d151SPavel Emelyanov entry.family = sk->sk_family; 654a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 6558d07d151SPavel Emelyanov entry.sport = inet->inet_num; 6568d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 657637c841dSDavid Ahern entry.ifindex = sk->sk_bound_dev_if; 658a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 659a52e95abSLorenzo Colitti if (sk_fullsock(sk)) 660a52e95abSLorenzo Colitti entry.mark = sk->sk_mark; 661a52e95abSLorenzo Colitti else if (sk->sk_state == TCP_NEW_SYN_RECV) 662a52e95abSLorenzo Colitti entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; 663a52e95abSLorenzo Colitti else 664a52e95abSLorenzo Colitti entry.mark = 0; 6658d07d151SPavel Emelyanov 6668d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 6678d07d151SPavel Emelyanov } 6688d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 6698d07d151SPavel Emelyanov 670a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 671a8c2190eSArnaldo Carvalho de Melo { 672a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 673a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 674a8c2190eSArnaldo Carvalho de Melo 675a8c2190eSArnaldo Carvalho de Melo if (cc > len) 676a8c2190eSArnaldo Carvalho de Melo return 0; 677a8c2190eSArnaldo Carvalho de Melo if (cc == len) 678a8c2190eSArnaldo Carvalho de Melo return 1; 679eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 680a8c2190eSArnaldo Carvalho de Melo return 0; 681a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 682a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 683a8c2190eSArnaldo Carvalho de Melo } 684a8c2190eSArnaldo Carvalho de Melo return 0; 685a8c2190eSArnaldo Carvalho de Melo } 686a8c2190eSArnaldo Carvalho de Melo 687637c841dSDavid Ahern /* data is u32 ifindex */ 688637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len, 689637c841dSDavid Ahern int *min_len) 690637c841dSDavid Ahern { 691637c841dSDavid Ahern /* Check ifindex space. */ 692637c841dSDavid Ahern *min_len += sizeof(u32); 693637c841dSDavid Ahern if (len < *min_len) 694637c841dSDavid Ahern return false; 695637c841dSDavid Ahern 696637c841dSDavid Ahern return true; 697637c841dSDavid Ahern } 698405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 699405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 700405c0059SNeal Cardwell int *min_len) 701405c0059SNeal Cardwell { 702405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 703e31c5e0eSEric Dumazet int addr_len; 704405c0059SNeal Cardwell 705405c0059SNeal Cardwell /* Check hostcond space. */ 706405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 707405c0059SNeal Cardwell if (len < *min_len) 708405c0059SNeal Cardwell return false; 709405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 710405c0059SNeal Cardwell 711405c0059SNeal Cardwell /* Check address family and address length. */ 712405c0059SNeal Cardwell switch (cond->family) { 713405c0059SNeal Cardwell case AF_UNSPEC: 714405c0059SNeal Cardwell addr_len = 0; 715405c0059SNeal Cardwell break; 716405c0059SNeal Cardwell case AF_INET: 717405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 718405c0059SNeal Cardwell break; 719405c0059SNeal Cardwell case AF_INET6: 720405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 721405c0059SNeal Cardwell break; 722405c0059SNeal Cardwell default: 723405c0059SNeal Cardwell return false; 724405c0059SNeal Cardwell } 725405c0059SNeal Cardwell *min_len += addr_len; 726405c0059SNeal Cardwell if (len < *min_len) 727405c0059SNeal Cardwell return false; 728405c0059SNeal Cardwell 729405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 730405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 731405c0059SNeal Cardwell return false; 732405c0059SNeal Cardwell 733405c0059SNeal Cardwell return true; 734405c0059SNeal Cardwell } 735405c0059SNeal Cardwell 7365e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 737e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 7385e1f5420SNeal Cardwell int len, int *min_len) 7395e1f5420SNeal Cardwell { 7405e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 7415e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 7425e1f5420SNeal Cardwell if (len < *min_len) 7435e1f5420SNeal Cardwell return false; 7445e1f5420SNeal Cardwell return true; 7455e1f5420SNeal Cardwell } 7465e1f5420SNeal Cardwell 747a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len, 748a52e95abSLorenzo Colitti int *min_len) 749a8c2190eSArnaldo Carvalho de Melo { 750a52e95abSLorenzo Colitti *min_len += sizeof(struct inet_diag_markcond); 751a52e95abSLorenzo Colitti return len >= *min_len; 752a52e95abSLorenzo Colitti } 753a52e95abSLorenzo Colitti 754a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr, 755a52e95abSLorenzo Colitti const struct sk_buff *skb) 756a52e95abSLorenzo Colitti { 757a52e95abSLorenzo Colitti bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); 758627cc4adSLorenzo Colitti const void *bytecode, *bc; 759627cc4adSLorenzo Colitti int bytecode_len, len; 760627cc4adSLorenzo Colitti 761627cc4adSLorenzo Colitti if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) 762627cc4adSLorenzo Colitti return -EINVAL; 763627cc4adSLorenzo Colitti 764627cc4adSLorenzo Colitti bytecode = bc = nla_data(attr); 765627cc4adSLorenzo Colitti len = bytecode_len = nla_len(attr); 766a8c2190eSArnaldo Carvalho de Melo 767a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 768405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 769e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 770a8c2190eSArnaldo Carvalho de Melo 771a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 772a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 773a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 774405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 775405c0059SNeal Cardwell return -EINVAL; 7765e1f5420SNeal Cardwell break; 777637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: 778637c841dSDavid Ahern if (!valid_devcond(bc, len, &min_len)) 779637c841dSDavid Ahern return -EINVAL; 780637c841dSDavid Ahern break; 781a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 782a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 783a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 784a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 7855e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 786a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 787a8c2190eSArnaldo Carvalho de Melo break; 788a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: 789a52e95abSLorenzo Colitti if (!net_admin) 790a52e95abSLorenzo Colitti return -EPERM; 791a52e95abSLorenzo Colitti if (!valid_markcond(bc, len, &min_len)) 792a52e95abSLorenzo Colitti return -EINVAL; 793a52e95abSLorenzo Colitti break; 7945e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 7955e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 796a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 797a8c2190eSArnaldo Carvalho de Melo break; 798a8c2190eSArnaldo Carvalho de Melo default: 799a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 800a8c2190eSArnaldo Carvalho de Melo } 8015e1f5420SNeal Cardwell 8025e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 8035e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 8045e1f5420SNeal Cardwell return -EINVAL; 8055e1f5420SNeal Cardwell if (op->no < len && 8065e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 8075e1f5420SNeal Cardwell return -EINVAL; 8085e1f5420SNeal Cardwell } 8095e1f5420SNeal Cardwell 810405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 811eeb14972SEric Dumazet return -EINVAL; 812a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 813a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 814a8c2190eSArnaldo Carvalho de Melo } 815a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 816a8c2190eSArnaldo Carvalho de Melo } 817a8c2190eSArnaldo Carvalho de Melo 818dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 819dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 82037f352b5SPavel Emelyanov struct netlink_callback *cb, 82134160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 822d545cacaSLorenzo Colitti const struct nlattr *bc, 823d545cacaSLorenzo Colitti bool net_admin) 824a8c2190eSArnaldo Carvalho de Melo { 8258d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 826a8c2190eSArnaldo Carvalho de Melo return 0; 827a8c2190eSArnaldo Carvalho de Melo 828a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 829e32123e5SPatrick McHardy sk_user_ns(NETLINK_CB(cb->skb).sk), 83015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 831d545cacaSLorenzo Colitti cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, 832d545cacaSLorenzo Colitti net_admin); 833a8c2190eSArnaldo Carvalho de Melo } 834a8c2190eSArnaldo Carvalho de Melo 83549612729SEric Dumazet static void twsk_build_assert(void) 83649612729SEric Dumazet { 83749612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 83849612729SEric Dumazet offsetof(struct sock, sk_family)); 83949612729SEric Dumazet 84049612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 84149612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 84249612729SEric Dumazet 84349612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 84449612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 84549612729SEric Dumazet 84649612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 84749612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 84849612729SEric Dumazet 84949612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 85049612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 85149612729SEric Dumazet 85249612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 85349612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 85449612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 85549612729SEric Dumazet 85649612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 85749612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 85849612729SEric Dumazet #endif 85949612729SEric Dumazet } 86049612729SEric Dumazet 8611942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 862e31c5e0eSEric Dumazet struct netlink_callback *cb, 86334160ea3SEric Dumazet const struct inet_diag_req_v2 *r, struct nlattr *bc) 864a8c2190eSArnaldo Carvalho de Melo { 865d545cacaSLorenzo Colitti bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); 86667db3e4bSEric Dumazet struct net *net = sock_net(skb->sk); 86767db3e4bSEric Dumazet u32 idiag_states = r->idiag_states; 86867db3e4bSEric Dumazet int i, num, s_i, s_num; 86967db3e4bSEric Dumazet struct sock *sk; 870a8c2190eSArnaldo Carvalho de Melo 871079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 872079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 873a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 874a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 875a8c2190eSArnaldo Carvalho de Melo 876a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 8779652dc2eSEric Dumazet if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) 878a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 879a8c2190eSArnaldo Carvalho de Melo 880a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 8815caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 882a8c2190eSArnaldo Carvalho de Melo 883a8c2190eSArnaldo Carvalho de Melo num = 0; 8845caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 8859652dc2eSEric Dumazet spin_lock(&ilb->lock); 8863b24d854SEric Dumazet sk_for_each(sk, &ilb->head) { 887a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 888a8c2190eSArnaldo Carvalho de Melo 88951d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 89051d7cccfSAndrey Vagin continue; 89151d7cccfSAndrey Vagin 892a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 893a8c2190eSArnaldo Carvalho de Melo num++; 894a8c2190eSArnaldo Carvalho de Melo continue; 895a8c2190eSArnaldo Carvalho de Melo } 896a8c2190eSArnaldo Carvalho de Melo 897d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 898d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 899d23deaa0SPavel Emelyanov goto next_listen; 900d23deaa0SPavel Emelyanov 901c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 902a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 903a8c2190eSArnaldo Carvalho de Melo goto next_listen; 904a8c2190eSArnaldo Carvalho de Melo 905d545cacaSLorenzo Colitti if (inet_csk_diag_dump(sk, skb, cb, r, 906d545cacaSLorenzo Colitti bc, net_admin) < 0) { 9079652dc2eSEric Dumazet spin_unlock(&ilb->lock); 908a8c2190eSArnaldo Carvalho de Melo goto done; 909a8c2190eSArnaldo Carvalho de Melo } 910a8c2190eSArnaldo Carvalho de Melo 911a8c2190eSArnaldo Carvalho de Melo next_listen: 912a8c2190eSArnaldo Carvalho de Melo ++num; 913a8c2190eSArnaldo Carvalho de Melo } 9149652dc2eSEric Dumazet spin_unlock(&ilb->lock); 915a8c2190eSArnaldo Carvalho de Melo 916a8c2190eSArnaldo Carvalho de Melo s_num = 0; 917a8c2190eSArnaldo Carvalho de Melo } 918a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 919a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 920a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 921a8c2190eSArnaldo Carvalho de Melo } 922a8c2190eSArnaldo Carvalho de Melo 923079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 924efb3cb42SPavel Emelyanov goto out; 925a8c2190eSArnaldo Carvalho de Melo 92667db3e4bSEric Dumazet #define SKARR_SZ 16 927f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 928a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 9297e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 9303ab5aee7SEric Dumazet struct hlist_nulls_node *node; 93167db3e4bSEric Dumazet struct sock *sk_arr[SKARR_SZ]; 93267db3e4bSEric Dumazet int num_arr[SKARR_SZ]; 93367db3e4bSEric Dumazet int idx, accum, res; 9346be547a6SAndi Kleen 93505dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 9366be547a6SAndi Kleen continue; 9376be547a6SAndi Kleen 938a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 939a8c2190eSArnaldo Carvalho de Melo s_num = 0; 940a8c2190eSArnaldo Carvalho de Melo 94167db3e4bSEric Dumazet next_chunk: 94267db3e4bSEric Dumazet num = 0; 94367db3e4bSEric Dumazet accum = 0; 9447e3aab4aSDavid S. Miller spin_lock_bh(lock); 9453ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 94667db3e4bSEric Dumazet int state; 947a8c2190eSArnaldo Carvalho de Melo 94851d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 94951d7cccfSAndrey Vagin continue; 950a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 951a8c2190eSArnaldo Carvalho de Melo goto next_normal; 95270315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 95370315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 954079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 955a8c2190eSArnaldo Carvalho de Melo goto next_normal; 956d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 957d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 958d23deaa0SPavel Emelyanov goto next_normal; 95905dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 960a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 961a8c2190eSArnaldo Carvalho de Melo goto next_normal; 96205dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 9634e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 964a8c2190eSArnaldo Carvalho de Melo goto next_normal; 965a58917f5SEric Dumazet twsk_build_assert(); 966a58917f5SEric Dumazet 967a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 968a58917f5SEric Dumazet goto next_normal; 969a58917f5SEric Dumazet 97067db3e4bSEric Dumazet sock_hold(sk); 97167db3e4bSEric Dumazet num_arr[accum] = num; 97267db3e4bSEric Dumazet sk_arr[accum] = sk; 97367db3e4bSEric Dumazet if (++accum == SKARR_SZ) 97467db3e4bSEric Dumazet break; 97567db3e4bSEric Dumazet next_normal: 97667db3e4bSEric Dumazet ++num; 97767db3e4bSEric Dumazet } 97867db3e4bSEric Dumazet spin_unlock_bh(lock); 97967db3e4bSEric Dumazet res = 0; 98067db3e4bSEric Dumazet for (idx = 0; idx < accum; idx++) { 98167db3e4bSEric Dumazet if (res >= 0) { 98267db3e4bSEric Dumazet res = sk_diag_fill(sk_arr[idx], skb, r, 983a58917f5SEric Dumazet sk_user_ns(NETLINK_CB(cb->skb).sk), 984a58917f5SEric Dumazet NETLINK_CB(cb->skb).portid, 985a58917f5SEric Dumazet cb->nlh->nlmsg_seq, NLM_F_MULTI, 986d545cacaSLorenzo Colitti cb->nlh, net_admin); 98767db3e4bSEric Dumazet if (res < 0) 98867db3e4bSEric Dumazet num = num_arr[idx]; 989a8c2190eSArnaldo Carvalho de Melo } 99067db3e4bSEric Dumazet sock_gen_put(sk_arr[idx]); 991a8c2190eSArnaldo Carvalho de Melo } 99267db3e4bSEric Dumazet if (res < 0) 99367db3e4bSEric Dumazet break; 994acffb584SEric Dumazet cond_resched(); 99567db3e4bSEric Dumazet if (accum == SKARR_SZ) { 99667db3e4bSEric Dumazet s_num = num + 1; 99767db3e4bSEric Dumazet goto next_chunk; 99867db3e4bSEric Dumazet } 999a8c2190eSArnaldo Carvalho de Melo } 1000a8c2190eSArnaldo Carvalho de Melo 1001a8c2190eSArnaldo Carvalho de Melo done: 1002a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 1003a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 1004efb3cb42SPavel Emelyanov out: 1005efb3cb42SPavel Emelyanov ; 1006efb3cb42SPavel Emelyanov } 10071942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 1008efb3cb42SPavel Emelyanov 1009efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 101034160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 1011e31c5e0eSEric Dumazet struct nlattr *bc) 1012efb3cb42SPavel Emelyanov { 1013efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 1014cacb6ba0SCyrill Gorcunov int err = 0; 1015efb3cb42SPavel Emelyanov 1016efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 1017efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 10181942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 1019cacb6ba0SCyrill Gorcunov else 1020cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 1021d523a328SHerbert Xu inet_diag_unlock_handler(handler); 1022efb3cb42SPavel Emelyanov 1023cacb6ba0SCyrill Gorcunov return err ? : skb->len; 1024a8c2190eSArnaldo Carvalho de Melo } 1025a8c2190eSArnaldo Carvalho de Melo 102625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 102725c4cd2bSPavel Emelyanov { 1028c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 1029e31c5e0eSEric Dumazet struct nlattr *bc = NULL; 103025c4cd2bSPavel Emelyanov 103125c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 103225c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 103325c4cd2bSPavel Emelyanov 1034d106352dSDavid S. Miller return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 103525c4cd2bSPavel Emelyanov } 103625c4cd2bSPavel Emelyanov 1037e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 1038a029fe26SPavel Emelyanov { 1039a029fe26SPavel Emelyanov switch (type) { 1040a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 1041a029fe26SPavel Emelyanov return IPPROTO_TCP; 1042a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 1043a029fe26SPavel Emelyanov return IPPROTO_DCCP; 1044a029fe26SPavel Emelyanov default: 1045a029fe26SPavel Emelyanov return 0; 1046a029fe26SPavel Emelyanov } 1047a029fe26SPavel Emelyanov } 1048a029fe26SPavel Emelyanov 1049e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 1050e31c5e0eSEric Dumazet struct netlink_callback *cb) 105125c4cd2bSPavel Emelyanov { 1052d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1053e31c5e0eSEric Dumazet int hdrlen = sizeof(struct inet_diag_req); 1054c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 105525c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 105625c4cd2bSPavel Emelyanov 1057d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 105825c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 105925c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 106025c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 106125c4cd2bSPavel Emelyanov req.id = rc->id; 106225c4cd2bSPavel Emelyanov 106325c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 106425c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 106525c4cd2bSPavel Emelyanov 106625c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 106725c4cd2bSPavel Emelyanov } 106825c4cd2bSPavel Emelyanov 1069fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1070fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1071fe50ce28SPavel Emelyanov { 1072d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1073c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1074fe50ce28SPavel Emelyanov 1075fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1076fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1077fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1078fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1079fe50ce28SPavel Emelyanov req.id = rc->id; 1080fe50ce28SPavel Emelyanov 10816eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req); 1082fe50ce28SPavel Emelyanov } 1083fe50ce28SPavel Emelyanov 10848d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1085a8c2190eSArnaldo Carvalho de Melo { 10863b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 108751d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1088a8c2190eSArnaldo Carvalho de Melo 1089ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1090ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1091ead592baSThomas Graf return -EINVAL; 1092a8c2190eSArnaldo Carvalho de Melo 1093b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 1094ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 1095ead592baSThomas Graf struct nlattr *attr; 1096627cc4adSLorenzo Colitti int err; 1097ead592baSThomas Graf 1098ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 1099ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 1100a52e95abSLorenzo Colitti err = inet_diag_bc_audit(attr, skb); 1101627cc4adSLorenzo Colitti if (err) 1102627cc4adSLorenzo Colitti return err; 1103a8c2190eSArnaldo Carvalho de Melo } 110480d326faSPablo Neira Ayuso { 110580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 110680d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 110780d326faSPablo Neira Ayuso }; 110851d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 110980d326faSPablo Neira Ayuso } 1110a8c2190eSArnaldo Carvalho de Melo } 1111ead592baSThomas Graf 1112fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1113a8c2190eSArnaldo Carvalho de Melo } 1114a8c2190eSArnaldo Carvalho de Melo 11156eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 1116d366477aSPavel Emelyanov { 1117c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 111851d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1119d366477aSPavel Emelyanov 1120d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1121d366477aSPavel Emelyanov return -EINVAL; 1122d366477aSPavel Emelyanov 11236eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 11246eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) { 112525c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 112625c4cd2bSPavel Emelyanov struct nlattr *attr; 1127627cc4adSLorenzo Colitti int err; 1128e31c5e0eSEric Dumazet 112925c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 113025c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 1131a52e95abSLorenzo Colitti err = inet_diag_bc_audit(attr, skb); 1132627cc4adSLorenzo Colitti if (err) 1133627cc4adSLorenzo Colitti return err; 113425c4cd2bSPavel Emelyanov } 113580d326faSPablo Neira Ayuso { 113680d326faSPablo Neira Ayuso struct netlink_dump_control c = { 113780d326faSPablo Neira Ayuso .dump = inet_diag_dump, 113880d326faSPablo Neira Ayuso }; 113951d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 114080d326faSPablo Neira Ayuso } 1141d366477aSPavel Emelyanov } 1142d366477aSPavel Emelyanov 11436eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); 1144d366477aSPavel Emelyanov } 1145d366477aSPavel Emelyanov 114635ac838aSCraig Gallek static 114735ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 114835ac838aSCraig Gallek { 114935ac838aSCraig Gallek const struct inet_diag_handler *handler; 115035ac838aSCraig Gallek struct nlmsghdr *nlh; 115135ac838aSCraig Gallek struct nlattr *attr; 115235ac838aSCraig Gallek struct inet_diag_msg *r; 115335ac838aSCraig Gallek void *info = NULL; 115435ac838aSCraig Gallek int err = 0; 115535ac838aSCraig Gallek 115635ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 115735ac838aSCraig Gallek if (!nlh) 115835ac838aSCraig Gallek return -ENOMEM; 115935ac838aSCraig Gallek 116035ac838aSCraig Gallek r = nlmsg_data(nlh); 116135ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 116235ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1163e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1164e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 116535ac838aSCraig Gallek r->idiag_state = sk->sk_state; 116635ac838aSCraig Gallek 116735ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 116835ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 116935ac838aSCraig Gallek return err; 117035ac838aSCraig Gallek } 117135ac838aSCraig Gallek 117235ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 117335ac838aSCraig Gallek if (IS_ERR(handler)) { 117435ac838aSCraig Gallek inet_diag_unlock_handler(handler); 117535ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 117635ac838aSCraig Gallek return PTR_ERR(handler); 117735ac838aSCraig Gallek } 117835ac838aSCraig Gallek 117935ac838aSCraig Gallek attr = handler->idiag_info_size 11806ed46d12SNicolas Dichtel ? nla_reserve_64bit(skb, INET_DIAG_INFO, 11816ed46d12SNicolas Dichtel handler->idiag_info_size, 11826ed46d12SNicolas Dichtel INET_DIAG_PAD) 118335ac838aSCraig Gallek : NULL; 118435ac838aSCraig Gallek if (attr) 118535ac838aSCraig Gallek info = nla_data(attr); 118635ac838aSCraig Gallek 118735ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 118835ac838aSCraig Gallek inet_diag_unlock_handler(handler); 118935ac838aSCraig Gallek 119035ac838aSCraig Gallek nlmsg_end(skb, nlh); 119135ac838aSCraig Gallek return 0; 119235ac838aSCraig Gallek } 119335ac838aSCraig Gallek 11948dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1195d366477aSPavel Emelyanov .family = AF_INET, 11966eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 119735ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 11986eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1199d366477aSPavel Emelyanov }; 1200d366477aSPavel Emelyanov 12018dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1202d366477aSPavel Emelyanov .family = AF_INET6, 12036eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 120435ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 12056eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1206d366477aSPavel Emelyanov }; 1207d366477aSPavel Emelyanov 1208a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1209a8c2190eSArnaldo Carvalho de Melo { 1210a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1211a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1212a8c2190eSArnaldo Carvalho de Melo 1213f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1214a8c2190eSArnaldo Carvalho de Melo goto out; 1215a8c2190eSArnaldo Carvalho de Melo 1216d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1217a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1218e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1219a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1220a8c2190eSArnaldo Carvalho de Melo err = 0; 1221a8c2190eSArnaldo Carvalho de Melo } 1222d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1223a8c2190eSArnaldo Carvalho de Melo out: 1224a8c2190eSArnaldo Carvalho de Melo return err; 1225a8c2190eSArnaldo Carvalho de Melo } 1226a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1227a8c2190eSArnaldo Carvalho de Melo 1228a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1229a8c2190eSArnaldo Carvalho de Melo { 1230a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1231a8c2190eSArnaldo Carvalho de Melo 1232f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1233a8c2190eSArnaldo Carvalho de Melo return; 1234a8c2190eSArnaldo Carvalho de Melo 1235d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1236a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1237d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1238a8c2190eSArnaldo Carvalho de Melo } 1239a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1240a8c2190eSArnaldo Carvalho de Melo 1241a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1242a8c2190eSArnaldo Carvalho de Melo { 1243f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1244a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1245a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1246a8c2190eSArnaldo Carvalho de Melo 12470da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1248a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1249a8c2190eSArnaldo Carvalho de Melo goto out; 1250a8c2190eSArnaldo Carvalho de Melo 1251d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1252d366477aSPavel Emelyanov if (err) 1253d366477aSPavel Emelyanov goto out_free_nl; 1254d366477aSPavel Emelyanov 1255d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1256d366477aSPavel Emelyanov if (err) 1257d366477aSPavel Emelyanov goto out_free_inet; 1258d366477aSPavel Emelyanov 12598ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1260a8c2190eSArnaldo Carvalho de Melo out: 1261a8c2190eSArnaldo Carvalho de Melo return err; 1262d366477aSPavel Emelyanov 1263d366477aSPavel Emelyanov out_free_inet: 1264d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1265d366477aSPavel Emelyanov out_free_nl: 1266a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1267a8c2190eSArnaldo Carvalho de Melo goto out; 1268a8c2190eSArnaldo Carvalho de Melo } 1269a8c2190eSArnaldo Carvalho de Melo 1270a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1271a8c2190eSArnaldo Carvalho de Melo { 1272d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1273d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 12748ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1275a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1276a8c2190eSArnaldo Carvalho de Melo } 1277a8c2190eSArnaldo Carvalho de Melo 1278a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1279a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1280a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1281aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1282aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1283