12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2a8c2190eSArnaldo Carvalho de Melo /* 3a8c2190eSArnaldo Carvalho de Melo * inet_diag.c Module for monitoring INET transport protocols sockets. 4a8c2190eSArnaldo Carvalho de Melo * 5a8c2190eSArnaldo Carvalho de Melo * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 6a8c2190eSArnaldo Carvalho de Melo */ 7a8c2190eSArnaldo Carvalho de Melo 8172589ccSIlpo Järvinen #include <linux/kernel.h> 9a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h> 10a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h> 11a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h> 12a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 14a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h> 15a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h> 16a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h> 17a8c2190eSArnaldo Carvalho de Melo 18a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h> 19a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h> 20a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h> 21a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h> 22a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 23a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 24a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h> 25a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h> 26*085c20caSMartin KaFai Lau #include <net/bpf_sk_storage.h> 27dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 28a8c2190eSArnaldo Carvalho de Melo 29a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h> 30a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h> 31a8c2190eSArnaldo Carvalho de Melo 32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h> 33d366477aSPavel Emelyanov #include <linux/sock_diag.h> 34a8c2190eSArnaldo Carvalho de Melo 35a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table; 36a8c2190eSArnaldo Carvalho de Melo 37a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry { 38e31c5e0eSEric Dumazet const __be32 *saddr; 39e31c5e0eSEric Dumazet const __be32 *daddr; 40a8c2190eSArnaldo Carvalho de Melo u16 sport; 41a8c2190eSArnaldo Carvalho de Melo u16 dport; 42a8c2190eSArnaldo Carvalho de Melo u16 family; 43a8c2190eSArnaldo Carvalho de Melo u16 userlocks; 44637c841dSDavid Ahern u32 ifindex; 45a52e95abSLorenzo Colitti u32 mark; 46a8c2190eSArnaldo Carvalho de Melo }; 47a8c2190eSArnaldo Carvalho de Melo 48d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 49d523a328SHerbert Xu 50f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 51d523a328SHerbert Xu { 52f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 53bf2ae2e4SXin Long sock_load_diag_module(AF_INET, proto); 54d523a328SHerbert Xu 55d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 56f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 57d523a328SHerbert Xu return ERR_PTR(-ENOENT); 58d523a328SHerbert Xu 59f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 60d523a328SHerbert Xu } 61d523a328SHerbert Xu 62e31c5e0eSEric Dumazet static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 63d523a328SHerbert Xu { 64d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 65d523a328SHerbert Xu } 66d523a328SHerbert Xu 67cb2050a7SXin Long void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) 68a4458343SEric Dumazet { 69a4458343SEric Dumazet r->idiag_family = sk->sk_family; 70a4458343SEric Dumazet 71a4458343SEric Dumazet r->id.idiag_sport = htons(sk->sk_num); 72a4458343SEric Dumazet r->id.idiag_dport = sk->sk_dport; 73a4458343SEric Dumazet r->id.idiag_if = sk->sk_bound_dev_if; 74a4458343SEric Dumazet sock_diag_save_cookie(sk, r->id.idiag_cookie); 75a4458343SEric Dumazet 76a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 77a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 78a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 79a4458343SEric Dumazet *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 80a4458343SEric Dumazet } else 81a4458343SEric Dumazet #endif 82a4458343SEric Dumazet { 83a4458343SEric Dumazet memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 84a4458343SEric Dumazet memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 85a4458343SEric Dumazet 86a4458343SEric Dumazet r->id.idiag_src[0] = sk->sk_rcv_saddr; 87a4458343SEric Dumazet r->id.idiag_dst[0] = sk->sk_daddr; 88a4458343SEric Dumazet } 89a4458343SEric Dumazet } 90cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill); 91a4458343SEric Dumazet 92b37e8840SIvan Delalande static size_t inet_sk_attr_size(struct sock *sk, 93b37e8840SIvan Delalande const struct inet_diag_req_v2 *req, 94b37e8840SIvan Delalande bool net_admin) 95c8e2c80dSEric Dumazet { 96b37e8840SIvan Delalande const struct inet_diag_handler *handler; 97b37e8840SIvan Delalande size_t aux = 0; 98b37e8840SIvan Delalande 99b37e8840SIvan Delalande handler = inet_diag_table[req->sdiag_protocol]; 100b37e8840SIvan Delalande if (handler && handler->idiag_get_aux_size) 101b37e8840SIvan Delalande aux = handler->idiag_get_aux_size(sk, net_admin); 102b37e8840SIvan Delalande 103c8e2c80dSEric Dumazet return nla_total_size(sizeof(struct tcp_info)) 104c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_SHUTDOWN */ 105c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TOS */ 106c8e2c80dSEric Dumazet + nla_total_size(1) /* INET_DIAG_TCLASS */ 107d545cacaSLorenzo Colitti + nla_total_size(4) /* INET_DIAG_MARK */ 1081ec17dbdSKonstantin Khlebnikov + nla_total_size(4) /* INET_DIAG_CLASS_ID */ 109c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_meminfo)) 110c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct inet_diag_msg)) 111c8e2c80dSEric Dumazet + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) 112c8e2c80dSEric Dumazet + nla_total_size(TCP_CA_NAME_MAX) 113c8e2c80dSEric Dumazet + nla_total_size(sizeof(struct tcpvegas_info)) 114b37e8840SIvan Delalande + aux 115c8e2c80dSEric Dumazet + 64; 116c8e2c80dSEric Dumazet } 117c8e2c80dSEric Dumazet 118cb2050a7SXin Long int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, 119cb2050a7SXin Long struct inet_diag_msg *r, int ext, 120d545cacaSLorenzo Colitti struct user_namespace *user_ns, 121d545cacaSLorenzo Colitti bool net_admin) 122a8c2190eSArnaldo Carvalho de Melo { 123a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 124a8c2190eSArnaldo Carvalho de Melo 125e4e541a8SPavel Emelyanov if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 126e4e541a8SPavel Emelyanov goto errout; 127e4e541a8SPavel Emelyanov 128717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 129717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 130717b6d83SMaciej Żenczykowski */ 131717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 1326e277ed5SThomas Graf if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 1336e277ed5SThomas Graf goto errout; 134717b6d83SMaciej Żenczykowski 135dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 136a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 13706236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 138efe4208fSEric Dumazet if (nla_put_u8(skb, INET_DIAG_TCLASS, 139efe4208fSEric Dumazet inet6_sk(sk)->tclass) < 0) 1406e277ed5SThomas Graf goto errout; 14120462155SPhil Sutter 1428220ea23SPhil Sutter if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 1438220ea23SPhil Sutter nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) 14420462155SPhil Sutter goto errout; 145a8c2190eSArnaldo Carvalho de Melo } 146a8c2190eSArnaldo Carvalho de Melo #endif 147a8c2190eSArnaldo Carvalho de Melo 148d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark)) 149d545cacaSLorenzo Colitti goto errout; 150d545cacaSLorenzo Colitti 151d06ca956SEric W. Biederman r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 1523c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1533c4d05c8SPavel Emelyanov 154cb2050a7SXin Long return 0; 155cb2050a7SXin Long errout: 156cb2050a7SXin Long return 1; 157cb2050a7SXin Long } 158cb2050a7SXin Long EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill); 159cb2050a7SXin Long 160*085c20caSMartin KaFai Lau #define MAX_DUMP_ALLOC_SIZE (KMALLOC_MAX_SIZE - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 161*085c20caSMartin KaFai Lau 162cb2050a7SXin Long int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 1635682d393SMartin KaFai Lau struct sk_buff *skb, struct netlink_callback *cb, 1645682d393SMartin KaFai Lau const struct inet_diag_req_v2 *req, 1655682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin) 166cb2050a7SXin Long { 167cb2050a7SXin Long const struct tcp_congestion_ops *ca_ops; 168cb2050a7SXin Long const struct inet_diag_handler *handler; 169*085c20caSMartin KaFai Lau struct inet_diag_dump_data *cb_data; 170cb2050a7SXin Long int ext = req->idiag_ext; 171cb2050a7SXin Long struct inet_diag_msg *r; 172cb2050a7SXin Long struct nlmsghdr *nlh; 173cb2050a7SXin Long struct nlattr *attr; 174cb2050a7SXin Long void *info = NULL; 175cb2050a7SXin Long 176*085c20caSMartin KaFai Lau cb_data = cb->data; 177cb2050a7SXin Long handler = inet_diag_table[req->sdiag_protocol]; 178cb2050a7SXin Long BUG_ON(!handler); 179cb2050a7SXin Long 1805682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1815682d393SMartin KaFai Lau cb->nlh->nlmsg_type, sizeof(*r), 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 1935682d393SMartin KaFai Lau if (inet_diag_msg_attrs_fill(sk, skb, r, ext, 1945682d393SMartin KaFai Lau sk_user_ns(NETLINK_CB(cb->skb).sk), 1955682d393SMartin KaFai Lau net_admin)) 196cb2050a7SXin Long goto errout; 197cb2050a7SXin Long 1986e277ed5SThomas Graf if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 1996e277ed5SThomas Graf struct inet_diag_meminfo minfo = { 2006e277ed5SThomas Graf .idiag_rmem = sk_rmem_alloc_get(sk), 201ab4e846aSEric Dumazet .idiag_wmem = READ_ONCE(sk->sk_wmem_queued), 2026e277ed5SThomas Graf .idiag_fmem = sk->sk_forward_alloc, 2036e277ed5SThomas Graf .idiag_tmem = sk_wmem_alloc_get(sk), 2046e277ed5SThomas Graf }; 2056e277ed5SThomas Graf 2066e277ed5SThomas Graf if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 2076e277ed5SThomas Graf goto errout; 2083c4d05c8SPavel Emelyanov } 2093c4d05c8SPavel Emelyanov 210c0636faaSPavel Emelyanov if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 211c0636faaSPavel Emelyanov if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 2126e277ed5SThomas Graf goto errout; 213c0636faaSPavel Emelyanov 214432490f9SCyrill Gorcunov /* 215432490f9SCyrill Gorcunov * RAW sockets might have user-defined protocols assigned, 216432490f9SCyrill Gorcunov * so report the one supplied on socket creation. 217432490f9SCyrill Gorcunov */ 218432490f9SCyrill Gorcunov if (sk->sk_type == SOCK_RAW) { 219432490f9SCyrill Gorcunov if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) 220432490f9SCyrill Gorcunov goto errout; 221432490f9SCyrill Gorcunov } 222432490f9SCyrill Gorcunov 223e31c5e0eSEric Dumazet if (!icsk) { 22462ad6fcdSShan Wei handler->idiag_get_info(sk, r, NULL); 2253c4d05c8SPavel Emelyanov goto out; 2263c4d05c8SPavel Emelyanov } 2273c4d05c8SPavel Emelyanov 2286ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS || 22957dde7f7SYuchung Cheng icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 2306ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 231a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 232a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 233b7de529cSXin Long r->idiag_expires = 2343828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 235a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 236a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 237a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 238b7de529cSXin Long r->idiag_expires = 2393828a93fSEric Dumazet jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 240a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 241a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 242a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 243b7de529cSXin Long r->idiag_expires = 2443828a93fSEric Dumazet jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies); 245a8c2190eSArnaldo Carvalho de Melo } else { 246a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 247a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 248a8c2190eSArnaldo Carvalho de Melo } 249a8c2190eSArnaldo Carvalho de Melo 2503fd22af8SCraig Gallek if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 2516ed46d12SNicolas Dichtel attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 2526ed46d12SNicolas Dichtel handler->idiag_info_size, 2536ed46d12SNicolas Dichtel INET_DIAG_PAD); 2546e277ed5SThomas Graf if (!attr) 2556e277ed5SThomas Graf goto errout; 256a8c2190eSArnaldo Carvalho de Melo 2576e277ed5SThomas Graf info = nla_data(attr); 258a8c2190eSArnaldo Carvalho de Melo } 259a8c2190eSArnaldo Carvalho de Melo 260521f1cf1SEric Dumazet if (ext & (1 << (INET_DIAG_CONG - 1))) { 261521f1cf1SEric Dumazet int err = 0; 262521f1cf1SEric Dumazet 263521f1cf1SEric Dumazet rcu_read_lock(); 264521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 265521f1cf1SEric Dumazet if (ca_ops) 266521f1cf1SEric Dumazet err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 267521f1cf1SEric Dumazet rcu_read_unlock(); 268521f1cf1SEric Dumazet if (err < 0) 2696e277ed5SThomas Graf goto errout; 270521f1cf1SEric Dumazet } 2716e277ed5SThomas Graf 272a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 273a8c2190eSArnaldo Carvalho de Melo 274b37e8840SIvan Delalande if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux) 275b37e8840SIvan Delalande if (handler->idiag_get_aux(sk, net_admin, skb) < 0) 276b37e8840SIvan Delalande goto errout; 277b37e8840SIvan Delalande 278521f1cf1SEric Dumazet if (sk->sk_state < TCP_TIME_WAIT) { 27964f40ff5SEric Dumazet union tcp_cc_info info; 28064f40ff5SEric Dumazet size_t sz = 0; 28164f40ff5SEric Dumazet int attr; 282521f1cf1SEric Dumazet 283521f1cf1SEric Dumazet rcu_read_lock(); 284521f1cf1SEric Dumazet ca_ops = READ_ONCE(icsk->icsk_ca_ops); 285521f1cf1SEric Dumazet if (ca_ops && ca_ops->get_info) 28664f40ff5SEric Dumazet sz = ca_ops->get_info(sk, ext, &attr, &info); 287521f1cf1SEric Dumazet rcu_read_unlock(); 28864f40ff5SEric Dumazet if (sz && nla_put(skb, attr, sz, &info) < 0) 289521f1cf1SEric Dumazet goto errout; 290521f1cf1SEric Dumazet } 291a8c2190eSArnaldo Carvalho de Melo 2921ec17dbdSKonstantin Khlebnikov if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) || 2931ec17dbdSKonstantin Khlebnikov ext & (1 << (INET_DIAG_TCLASS - 1))) { 2940888e372SLevin, Alexander (Sasha Levin) u32 classid = 0; 2950888e372SLevin, Alexander (Sasha Levin) 2960888e372SLevin, Alexander (Sasha Levin) #ifdef CONFIG_SOCK_CGROUP_DATA 2970888e372SLevin, Alexander (Sasha Levin) classid = sock_cgroup_classid(&sk->sk_cgrp_data); 2980888e372SLevin, Alexander (Sasha Levin) #endif 2991ec17dbdSKonstantin Khlebnikov /* Fallback to socket priority if class id isn't set. 3001ec17dbdSKonstantin Khlebnikov * Classful qdiscs use it as direct reference to class. 3011ec17dbdSKonstantin Khlebnikov * For cgroup2 classid is always zero. 3021ec17dbdSKonstantin Khlebnikov */ 3031ec17dbdSKonstantin Khlebnikov if (!classid) 3041ec17dbdSKonstantin Khlebnikov classid = sk->sk_priority; 3050888e372SLevin, Alexander (Sasha Levin) 3060888e372SLevin, Alexander (Sasha Levin) if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid)) 3070888e372SLevin, Alexander (Sasha Levin) goto errout; 3080888e372SLevin, Alexander (Sasha Levin) } 3090888e372SLevin, Alexander (Sasha Levin) 310*085c20caSMartin KaFai Lau /* Keep it at the end for potential retry with a larger skb, 311*085c20caSMartin KaFai Lau * or else do best-effort fitting, which is only done for the 312*085c20caSMartin KaFai Lau * first_nlmsg. 313*085c20caSMartin KaFai Lau */ 314*085c20caSMartin KaFai Lau if (cb_data->bpf_stg_diag) { 315*085c20caSMartin KaFai Lau bool first_nlmsg = ((unsigned char *)nlh == skb->data); 316*085c20caSMartin KaFai Lau unsigned int prev_min_dump_alloc; 317*085c20caSMartin KaFai Lau unsigned int total_nla_size = 0; 318*085c20caSMartin KaFai Lau unsigned int msg_len; 319*085c20caSMartin KaFai Lau int err; 320*085c20caSMartin KaFai Lau 321*085c20caSMartin KaFai Lau msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 322*085c20caSMartin KaFai Lau err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb, 323*085c20caSMartin KaFai Lau INET_DIAG_SK_BPF_STORAGES, 324*085c20caSMartin KaFai Lau &total_nla_size); 325*085c20caSMartin KaFai Lau 326*085c20caSMartin KaFai Lau if (!err) 327*085c20caSMartin KaFai Lau goto out; 328*085c20caSMartin KaFai Lau 329*085c20caSMartin KaFai Lau total_nla_size += msg_len; 330*085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc; 331*085c20caSMartin KaFai Lau if (total_nla_size > prev_min_dump_alloc) 332*085c20caSMartin KaFai Lau cb->min_dump_alloc = min_t(u32, total_nla_size, 333*085c20caSMartin KaFai Lau MAX_DUMP_ALLOC_SIZE); 334*085c20caSMartin KaFai Lau 335*085c20caSMartin KaFai Lau if (!first_nlmsg) 336*085c20caSMartin KaFai Lau goto errout; 337*085c20caSMartin KaFai Lau 338*085c20caSMartin KaFai Lau if (cb->min_dump_alloc > prev_min_dump_alloc) 339*085c20caSMartin KaFai Lau /* Retry with pskb_expand_head() with 340*085c20caSMartin KaFai Lau * __GFP_DIRECT_RECLAIM 341*085c20caSMartin KaFai Lau */ 342*085c20caSMartin KaFai Lau goto errout; 343*085c20caSMartin KaFai Lau 344*085c20caSMartin KaFai Lau WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc); 345*085c20caSMartin KaFai Lau 346*085c20caSMartin KaFai Lau /* Send what we have for this sk 347*085c20caSMartin KaFai Lau * and move on to the next sk in the following 348*085c20caSMartin KaFai Lau * dump() 349*085c20caSMartin KaFai Lau */ 350*085c20caSMartin KaFai Lau } 351*085c20caSMartin KaFai Lau 3523c4d05c8SPavel Emelyanov out: 353053c095aSJohannes Berg nlmsg_end(skb, nlh); 354053c095aSJohannes Berg return 0; 355a8c2190eSArnaldo Carvalho de Melo 3566e277ed5SThomas Graf errout: 3576e277ed5SThomas Graf nlmsg_cancel(skb, nlh); 35826932566SPatrick McHardy return -EMSGSIZE; 359a8c2190eSArnaldo Carvalho de Melo } 3603c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 3613c4d05c8SPavel Emelyanov 36233cf7c90SEric Dumazet static int inet_twsk_diag_fill(struct sock *sk, 363e31c5e0eSEric Dumazet struct sk_buff *skb, 3645682d393SMartin KaFai Lau struct netlink_callback *cb, 3655682d393SMartin KaFai Lau u16 nlmsg_flags) 366c7d58aabSArnaldo Carvalho de Melo { 36733cf7c90SEric Dumazet struct inet_timewait_sock *tw = inet_twsk(sk); 368c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 3696e277ed5SThomas Graf struct nlmsghdr *nlh; 370789f558cSEric Dumazet long tmo; 371c7d58aabSArnaldo Carvalho de Melo 3725682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 3735682d393SMartin KaFai Lau cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, 3745682d393SMartin KaFai Lau sizeof(*r), nlmsg_flags); 3756e277ed5SThomas Graf if (!nlh) 376d106352dSDavid S. Miller return -EMSGSIZE; 377d106352dSDavid S. Miller 378d106352dSDavid S. Miller r = nlmsg_data(nlh); 379c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 380c7d58aabSArnaldo Carvalho de Melo 381a4458343SEric Dumazet inet_diag_msg_common_fill(r, sk); 382c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 383b1aac815SDaniel Borkmann 384c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 385c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 3863828a93fSEric Dumazet tmo = tw->tw_timer.expires - jiffies; 3873828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo); 388c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 389c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 390c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 391c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 3926e277ed5SThomas Graf 393053c095aSJohannes Berg nlmsg_end(skb, nlh); 394053c095aSJohannes Berg return 0; 395c7d58aabSArnaldo Carvalho de Melo } 396c7d58aabSArnaldo Carvalho de Melo 397a58917f5SEric Dumazet static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 3985682d393SMartin KaFai Lau struct netlink_callback *cb, 3995682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin) 400a58917f5SEric Dumazet { 401d545cacaSLorenzo Colitti struct request_sock *reqsk = inet_reqsk(sk); 402a58917f5SEric Dumazet struct inet_diag_msg *r; 403a58917f5SEric Dumazet struct nlmsghdr *nlh; 404a58917f5SEric Dumazet long tmo; 405a58917f5SEric Dumazet 4065682d393SMartin KaFai Lau nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 4075682d393SMartin KaFai Lau cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags); 408a58917f5SEric Dumazet if (!nlh) 409a58917f5SEric Dumazet return -EMSGSIZE; 410a58917f5SEric Dumazet 411a58917f5SEric Dumazet r = nlmsg_data(nlh); 412a58917f5SEric Dumazet inet_diag_msg_common_fill(r, sk); 413a58917f5SEric Dumazet r->idiag_state = TCP_SYN_RECV; 414a58917f5SEric Dumazet r->idiag_timer = 1; 415d545cacaSLorenzo Colitti r->idiag_retrans = reqsk->num_retrans; 416a58917f5SEric Dumazet 417a58917f5SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 418a58917f5SEric Dumazet offsetof(struct sock, sk_cookie)); 419a58917f5SEric Dumazet 420fa76ce73SEric Dumazet tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 4213828a93fSEric Dumazet r->idiag_expires = jiffies_delta_to_msecs(tmo); 422a58917f5SEric Dumazet r->idiag_rqueue = 0; 423a58917f5SEric Dumazet r->idiag_wqueue = 0; 424a58917f5SEric Dumazet r->idiag_uid = 0; 425a58917f5SEric Dumazet r->idiag_inode = 0; 426a58917f5SEric Dumazet 427d545cacaSLorenzo Colitti if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 428d545cacaSLorenzo Colitti inet_rsk(reqsk)->ir_mark)) 429d545cacaSLorenzo Colitti return -EMSGSIZE; 430d545cacaSLorenzo Colitti 431a58917f5SEric Dumazet nlmsg_end(skb, nlh); 432a58917f5SEric Dumazet return 0; 433a58917f5SEric Dumazet } 434a58917f5SEric Dumazet 435dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 4365682d393SMartin KaFai Lau struct netlink_callback *cb, 43734160ea3SEric Dumazet const struct inet_diag_req_v2 *r, 4385682d393SMartin KaFai Lau u16 nlmsg_flags, bool net_admin) 439dff2c035SArnaldo Carvalho de Melo { 440dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 4415682d393SMartin KaFai Lau return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags); 442a58917f5SEric Dumazet 443a58917f5SEric Dumazet if (sk->sk_state == TCP_NEW_SYN_RECV) 4445682d393SMartin KaFai Lau return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin); 445efe4208fSEric Dumazet 4465682d393SMartin KaFai Lau return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags, 4475682d393SMartin KaFai Lau net_admin); 448dff2c035SArnaldo Carvalho de Melo } 449dff2c035SArnaldo Carvalho de Melo 450b613f56eSLorenzo Colitti struct sock *inet_diag_find_one_icsk(struct net *net, 451b613f56eSLorenzo Colitti struct inet_hashinfo *hashinfo, 45234160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 453a8c2190eSArnaldo Carvalho de Melo { 454e31c5e0eSEric Dumazet struct sock *sk; 455a8c2190eSArnaldo Carvalho de Melo 4562d331915SEric Dumazet rcu_read_lock(); 457e31c5e0eSEric Dumazet if (req->sdiag_family == AF_INET) 458a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 459a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 460a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 461dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4627c130672SEric Dumazet else if (req->sdiag_family == AF_INET6) { 4637c130672SEric Dumazet if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 4647c130672SEric Dumazet ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 465a583636aSCraig Gallek sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 4667c130672SEric Dumazet req->id.idiag_dport, req->id.idiag_src[3], 4677c130672SEric Dumazet req->id.idiag_sport, req->id.idiag_if); 4687c130672SEric Dumazet else 469a583636aSCraig Gallek sk = inet6_lookup(net, hashinfo, NULL, 0, 470a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 471a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 472a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 473a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 474a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 4757c130672SEric Dumazet } 476a8c2190eSArnaldo Carvalho de Melo #endif 4772d331915SEric Dumazet else { 4782d331915SEric Dumazet rcu_read_unlock(); 479b613f56eSLorenzo Colitti return ERR_PTR(-EINVAL); 4802d331915SEric Dumazet } 4812d331915SEric Dumazet rcu_read_unlock(); 482e31c5e0eSEric Dumazet if (!sk) 483b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 484a8c2190eSArnaldo Carvalho de Melo 485b613f56eSLorenzo Colitti if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 486b613f56eSLorenzo Colitti sock_gen_put(sk); 487b613f56eSLorenzo Colitti return ERR_PTR(-ENOENT); 488b613f56eSLorenzo Colitti } 489b613f56eSLorenzo Colitti 490b613f56eSLorenzo Colitti return sk; 491b613f56eSLorenzo Colitti } 492b613f56eSLorenzo Colitti EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 493b613f56eSLorenzo Colitti 494b613f56eSLorenzo Colitti int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 4955682d393SMartin KaFai Lau struct netlink_callback *cb, 496b613f56eSLorenzo Colitti const struct inet_diag_req_v2 *req) 497b613f56eSLorenzo Colitti { 4985682d393SMartin KaFai Lau struct sk_buff *in_skb = cb->skb; 499b37e8840SIvan Delalande bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN); 500b613f56eSLorenzo Colitti struct net *net = sock_net(in_skb->sk); 501b613f56eSLorenzo Colitti struct sk_buff *rep; 502b613f56eSLorenzo Colitti struct sock *sk; 503b613f56eSLorenzo Colitti int err; 504b613f56eSLorenzo Colitti 505b613f56eSLorenzo Colitti sk = inet_diag_find_one_icsk(net, hashinfo, req); 506b613f56eSLorenzo Colitti if (IS_ERR(sk)) 507b613f56eSLorenzo Colitti return PTR_ERR(sk); 508a8c2190eSArnaldo Carvalho de Melo 509b37e8840SIvan Delalande rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL); 5106e277ed5SThomas Graf if (!rep) { 5116e277ed5SThomas Graf err = -ENOMEM; 512a8c2190eSArnaldo Carvalho de Melo goto out; 5136e277ed5SThomas Graf } 514a8c2190eSArnaldo Carvalho de Melo 5155682d393SMartin KaFai Lau err = sk_diag_fill(sk, rep, cb, req, 0, net_admin); 51626932566SPatrick McHardy if (err < 0) { 51726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 5186e277ed5SThomas Graf nlmsg_free(rep); 51926932566SPatrick McHardy goto out; 52026932566SPatrick McHardy } 52115e47304SEric W. Biederman err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 522a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 523a8c2190eSArnaldo Carvalho de Melo if (err > 0) 524a8c2190eSArnaldo Carvalho de Melo err = 0; 525a8c2190eSArnaldo Carvalho de Melo 526a8c2190eSArnaldo Carvalho de Melo out: 527c1d607ccSEric Dumazet if (sk) 528c1d607ccSEric Dumazet sock_gen_put(sk); 529c1d607ccSEric Dumazet 530476f7dbfSPavel Emelyanov return err; 531476f7dbfSPavel Emelyanov } 5321942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 533476f7dbfSPavel Emelyanov 5346eb5d2e0SLorenzo Colitti static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 535476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 53634160ea3SEric Dumazet const struct inet_diag_req_v2 *req) 537476f7dbfSPavel Emelyanov { 538476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 539476f7dbfSPavel Emelyanov int err; 540476f7dbfSPavel Emelyanov 541476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 5425682d393SMartin KaFai Lau if (IS_ERR(handler)) { 543476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 5445682d393SMartin KaFai Lau } else if (cmd == SOCK_DIAG_BY_FAMILY) { 5450df6d328SMartin KaFai Lau struct inet_diag_dump_data empty_dump_data = {}; 5465682d393SMartin KaFai Lau struct netlink_callback cb = { 5475682d393SMartin KaFai Lau .nlh = nlh, 5485682d393SMartin KaFai Lau .skb = in_skb, 5490df6d328SMartin KaFai Lau .data = &empty_dump_data, 5505682d393SMartin KaFai Lau }; 5515682d393SMartin KaFai Lau err = handler->dump_one(&cb, req); 5525682d393SMartin KaFai Lau } else if (cmd == SOCK_DESTROY && handler->destroy) { 5536eb5d2e0SLorenzo Colitti err = handler->destroy(in_skb, req); 5545682d393SMartin KaFai Lau } else { 5556eb5d2e0SLorenzo Colitti err = -EOPNOTSUPP; 5565682d393SMartin KaFai Lau } 557d523a328SHerbert Xu inet_diag_unlock_handler(handler); 558476f7dbfSPavel Emelyanov 559a8c2190eSArnaldo Carvalho de Melo return err; 560a8c2190eSArnaldo Carvalho de Melo } 561a8c2190eSArnaldo Carvalho de Melo 5629f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 563a8c2190eSArnaldo Carvalho de Melo { 564a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 565a8c2190eSArnaldo Carvalho de Melo 566a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 567a8c2190eSArnaldo Carvalho de Melo 568a8c2190eSArnaldo Carvalho de Melo if (words) { 569a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 570a8c2190eSArnaldo Carvalho de Melo return 0; 571a8c2190eSArnaldo Carvalho de Melo } 572a8c2190eSArnaldo Carvalho de Melo if (bits) { 5739f855299SAl Viro __be32 w1, w2; 5749f855299SAl Viro __be32 mask; 575a8c2190eSArnaldo Carvalho de Melo 576a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 577a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 578a8c2190eSArnaldo Carvalho de Melo 579a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 580a8c2190eSArnaldo Carvalho de Melo 581a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 582a8c2190eSArnaldo Carvalho de Melo return 0; 583a8c2190eSArnaldo Carvalho de Melo } 584a8c2190eSArnaldo Carvalho de Melo 585a8c2190eSArnaldo Carvalho de Melo return 1; 586a8c2190eSArnaldo Carvalho de Melo } 587a8c2190eSArnaldo Carvalho de Melo 58887c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 589a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 590a8c2190eSArnaldo Carvalho de Melo { 59187c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 59287c22ea5SPavel Emelyanov int len = nla_len(_bc); 59387c22ea5SPavel Emelyanov 594a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 595a8c2190eSArnaldo Carvalho de Melo int yes = 1; 596a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 597a8c2190eSArnaldo Carvalho de Melo 598a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 599a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 600a8c2190eSArnaldo Carvalho de Melo break; 601a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 602a8c2190eSArnaldo Carvalho de Melo yes = 0; 603a8c2190eSArnaldo Carvalho de Melo break; 604bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 605bbb6189dSKristian Evensen yes = entry->sport == op[1].no; 606bbb6189dSKristian Evensen break; 607a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 608a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 609a8c2190eSArnaldo Carvalho de Melo break; 610a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 611b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 612a8c2190eSArnaldo Carvalho de Melo break; 613bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 614bbb6189dSKristian Evensen yes = entry->dport == op[1].no; 615bbb6189dSKristian Evensen break; 616a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 617a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 618a8c2190eSArnaldo Carvalho de Melo break; 619a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 620a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 621a8c2190eSArnaldo Carvalho de Melo break; 622a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 623a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 624a8c2190eSArnaldo Carvalho de Melo break; 625a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 626a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 627e31c5e0eSEric Dumazet const struct inet_diag_hostcond *cond; 628e31c5e0eSEric Dumazet const __be32 *addr; 629a8c2190eSArnaldo Carvalho de Melo 630e31c5e0eSEric Dumazet cond = (const struct inet_diag_hostcond *)(op + 1); 631a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 632a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 633a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 634a8c2190eSArnaldo Carvalho de Melo yes = 0; 635a8c2190eSArnaldo Carvalho de Melo break; 636a8c2190eSArnaldo Carvalho de Melo } 637a8c2190eSArnaldo Carvalho de Melo 638a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 639a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 640a8c2190eSArnaldo Carvalho de Melo else 641a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 642a8c2190eSArnaldo Carvalho de Melo 643f67caec9SNeal Cardwell if (cond->family != AF_UNSPEC && 644f67caec9SNeal Cardwell cond->family != entry->family) { 645a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 646a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 647a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 648a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 649f67caec9SNeal Cardwell bitstring_match(addr + 3, 650f67caec9SNeal Cardwell cond->addr, 651a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 652a8c2190eSArnaldo Carvalho de Melo break; 653a8c2190eSArnaldo Carvalho de Melo } 654a8c2190eSArnaldo Carvalho de Melo yes = 0; 655a8c2190eSArnaldo Carvalho de Melo break; 656a8c2190eSArnaldo Carvalho de Melo } 657f67caec9SNeal Cardwell 658f67caec9SNeal Cardwell if (cond->prefix_len == 0) 659f67caec9SNeal Cardwell break; 660f67caec9SNeal Cardwell if (bitstring_match(addr, cond->addr, 661f67caec9SNeal Cardwell cond->prefix_len)) 662f67caec9SNeal Cardwell break; 663f67caec9SNeal Cardwell yes = 0; 664f67caec9SNeal Cardwell break; 665f67caec9SNeal Cardwell } 666637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: { 667637c841dSDavid Ahern u32 ifindex; 668637c841dSDavid Ahern 669637c841dSDavid Ahern ifindex = *((const u32 *)(op + 1)); 670637c841dSDavid Ahern if (ifindex != entry->ifindex) 671637c841dSDavid Ahern yes = 0; 672637c841dSDavid Ahern break; 673637c841dSDavid Ahern } 674a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: { 675a52e95abSLorenzo Colitti struct inet_diag_markcond *cond; 676a52e95abSLorenzo Colitti 677a52e95abSLorenzo Colitti cond = (struct inet_diag_markcond *)(op + 1); 678a52e95abSLorenzo Colitti if ((entry->mark & cond->mask) != cond->mark) 679a52e95abSLorenzo Colitti yes = 0; 680a52e95abSLorenzo Colitti break; 681a52e95abSLorenzo Colitti } 682a8c2190eSArnaldo Carvalho de Melo } 683a8c2190eSArnaldo Carvalho de Melo 684a8c2190eSArnaldo Carvalho de Melo if (yes) { 685a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 686a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 687a8c2190eSArnaldo Carvalho de Melo } else { 688a8c2190eSArnaldo Carvalho de Melo len -= op->no; 689a8c2190eSArnaldo Carvalho de Melo bc += op->no; 690a8c2190eSArnaldo Carvalho de Melo } 691a8c2190eSArnaldo Carvalho de Melo } 692a02cec21SEric Dumazet return len == 0; 693a8c2190eSArnaldo Carvalho de Melo } 694a8c2190eSArnaldo Carvalho de Melo 695a4458343SEric Dumazet /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 696a4458343SEric Dumazet */ 697a4458343SEric Dumazet static void entry_fill_addrs(struct inet_diag_entry *entry, 698a4458343SEric Dumazet const struct sock *sk) 699a4458343SEric Dumazet { 700a4458343SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 701a4458343SEric Dumazet if (sk->sk_family == AF_INET6) { 702a4458343SEric Dumazet entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 703a4458343SEric Dumazet entry->daddr = sk->sk_v6_daddr.s6_addr32; 704a4458343SEric Dumazet } else 705a4458343SEric Dumazet #endif 706a4458343SEric Dumazet { 707a4458343SEric Dumazet entry->saddr = &sk->sk_rcv_saddr; 708a4458343SEric Dumazet entry->daddr = &sk->sk_daddr; 709a4458343SEric Dumazet } 710a4458343SEric Dumazet } 711a4458343SEric Dumazet 7128d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 7138d07d151SPavel Emelyanov { 7148d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 715e31c5e0eSEric Dumazet struct inet_diag_entry entry; 7168d07d151SPavel Emelyanov 717e31c5e0eSEric Dumazet if (!bc) 7188d07d151SPavel Emelyanov return 1; 7198d07d151SPavel Emelyanov 7208d07d151SPavel Emelyanov entry.family = sk->sk_family; 721a4458343SEric Dumazet entry_fill_addrs(&entry, sk); 7228d07d151SPavel Emelyanov entry.sport = inet->inet_num; 7238d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 724637c841dSDavid Ahern entry.ifindex = sk->sk_bound_dev_if; 725a58917f5SEric Dumazet entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 726a52e95abSLorenzo Colitti if (sk_fullsock(sk)) 727a52e95abSLorenzo Colitti entry.mark = sk->sk_mark; 728a52e95abSLorenzo Colitti else if (sk->sk_state == TCP_NEW_SYN_RECV) 729a52e95abSLorenzo Colitti entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; 730a52e95abSLorenzo Colitti else 731a52e95abSLorenzo Colitti entry.mark = 0; 7328d07d151SPavel Emelyanov 7338d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 7348d07d151SPavel Emelyanov } 7358d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 7368d07d151SPavel Emelyanov 737a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 738a8c2190eSArnaldo Carvalho de Melo { 739a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 740a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 741a8c2190eSArnaldo Carvalho de Melo 742a8c2190eSArnaldo Carvalho de Melo if (cc > len) 743a8c2190eSArnaldo Carvalho de Melo return 0; 744a8c2190eSArnaldo Carvalho de Melo if (cc == len) 745a8c2190eSArnaldo Carvalho de Melo return 1; 746eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 747a8c2190eSArnaldo Carvalho de Melo return 0; 748a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 749a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 750a8c2190eSArnaldo Carvalho de Melo } 751a8c2190eSArnaldo Carvalho de Melo return 0; 752a8c2190eSArnaldo Carvalho de Melo } 753a8c2190eSArnaldo Carvalho de Melo 754637c841dSDavid Ahern /* data is u32 ifindex */ 755637c841dSDavid Ahern static bool valid_devcond(const struct inet_diag_bc_op *op, int len, 756637c841dSDavid Ahern int *min_len) 757637c841dSDavid Ahern { 758637c841dSDavid Ahern /* Check ifindex space. */ 759637c841dSDavid Ahern *min_len += sizeof(u32); 760637c841dSDavid Ahern if (len < *min_len) 761637c841dSDavid Ahern return false; 762637c841dSDavid Ahern 763637c841dSDavid Ahern return true; 764637c841dSDavid Ahern } 765405c0059SNeal Cardwell /* Validate an inet_diag_hostcond. */ 766405c0059SNeal Cardwell static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 767405c0059SNeal Cardwell int *min_len) 768405c0059SNeal Cardwell { 769405c0059SNeal Cardwell struct inet_diag_hostcond *cond; 770e31c5e0eSEric Dumazet int addr_len; 771405c0059SNeal Cardwell 772405c0059SNeal Cardwell /* Check hostcond space. */ 773405c0059SNeal Cardwell *min_len += sizeof(struct inet_diag_hostcond); 774405c0059SNeal Cardwell if (len < *min_len) 775405c0059SNeal Cardwell return false; 776405c0059SNeal Cardwell cond = (struct inet_diag_hostcond *)(op + 1); 777405c0059SNeal Cardwell 778405c0059SNeal Cardwell /* Check address family and address length. */ 779405c0059SNeal Cardwell switch (cond->family) { 780405c0059SNeal Cardwell case AF_UNSPEC: 781405c0059SNeal Cardwell addr_len = 0; 782405c0059SNeal Cardwell break; 783405c0059SNeal Cardwell case AF_INET: 784405c0059SNeal Cardwell addr_len = sizeof(struct in_addr); 785405c0059SNeal Cardwell break; 786405c0059SNeal Cardwell case AF_INET6: 787405c0059SNeal Cardwell addr_len = sizeof(struct in6_addr); 788405c0059SNeal Cardwell break; 789405c0059SNeal Cardwell default: 790405c0059SNeal Cardwell return false; 791405c0059SNeal Cardwell } 792405c0059SNeal Cardwell *min_len += addr_len; 793405c0059SNeal Cardwell if (len < *min_len) 794405c0059SNeal Cardwell return false; 795405c0059SNeal Cardwell 796405c0059SNeal Cardwell /* Check prefix length (in bits) vs address length (in bytes). */ 797405c0059SNeal Cardwell if (cond->prefix_len > 8 * addr_len) 798405c0059SNeal Cardwell return false; 799405c0059SNeal Cardwell 800405c0059SNeal Cardwell return true; 801405c0059SNeal Cardwell } 802405c0059SNeal Cardwell 8035e1f5420SNeal Cardwell /* Validate a port comparison operator. */ 804e31c5e0eSEric Dumazet static bool valid_port_comparison(const struct inet_diag_bc_op *op, 8055e1f5420SNeal Cardwell int len, int *min_len) 8065e1f5420SNeal Cardwell { 8075e1f5420SNeal Cardwell /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 8085e1f5420SNeal Cardwell *min_len += sizeof(struct inet_diag_bc_op); 8095e1f5420SNeal Cardwell if (len < *min_len) 8105e1f5420SNeal Cardwell return false; 8115e1f5420SNeal Cardwell return true; 8125e1f5420SNeal Cardwell } 8135e1f5420SNeal Cardwell 814a52e95abSLorenzo Colitti static bool valid_markcond(const struct inet_diag_bc_op *op, int len, 815a52e95abSLorenzo Colitti int *min_len) 816a8c2190eSArnaldo Carvalho de Melo { 817a52e95abSLorenzo Colitti *min_len += sizeof(struct inet_diag_markcond); 818a52e95abSLorenzo Colitti return len >= *min_len; 819a52e95abSLorenzo Colitti } 820a52e95abSLorenzo Colitti 821a52e95abSLorenzo Colitti static int inet_diag_bc_audit(const struct nlattr *attr, 822a52e95abSLorenzo Colitti const struct sk_buff *skb) 823a52e95abSLorenzo Colitti { 824a52e95abSLorenzo Colitti bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); 825627cc4adSLorenzo Colitti const void *bytecode, *bc; 826627cc4adSLorenzo Colitti int bytecode_len, len; 827627cc4adSLorenzo Colitti 828627cc4adSLorenzo Colitti if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) 829627cc4adSLorenzo Colitti return -EINVAL; 830627cc4adSLorenzo Colitti 831627cc4adSLorenzo Colitti bytecode = bc = nla_data(attr); 832627cc4adSLorenzo Colitti len = bytecode_len = nla_len(attr); 833a8c2190eSArnaldo Carvalho de Melo 834a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 835405c0059SNeal Cardwell int min_len = sizeof(struct inet_diag_bc_op); 836e31c5e0eSEric Dumazet const struct inet_diag_bc_op *op = bc; 837a8c2190eSArnaldo Carvalho de Melo 838a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 839a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 840a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 841405c0059SNeal Cardwell if (!valid_hostcond(bc, len, &min_len)) 842405c0059SNeal Cardwell return -EINVAL; 8435e1f5420SNeal Cardwell break; 844637c841dSDavid Ahern case INET_DIAG_BC_DEV_COND: 845637c841dSDavid Ahern if (!valid_devcond(bc, len, &min_len)) 846637c841dSDavid Ahern return -EINVAL; 847637c841dSDavid Ahern break; 848bbb6189dSKristian Evensen case INET_DIAG_BC_S_EQ: 849a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 850a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 851bbb6189dSKristian Evensen case INET_DIAG_BC_D_EQ: 852a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 853a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 8545e1f5420SNeal Cardwell if (!valid_port_comparison(bc, len, &min_len)) 855a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 856a8c2190eSArnaldo Carvalho de Melo break; 857a52e95abSLorenzo Colitti case INET_DIAG_BC_MARK_COND: 858a52e95abSLorenzo Colitti if (!net_admin) 859a52e95abSLorenzo Colitti return -EPERM; 860a52e95abSLorenzo Colitti if (!valid_markcond(bc, len, &min_len)) 861a52e95abSLorenzo Colitti return -EINVAL; 862a52e95abSLorenzo Colitti break; 8635e1f5420SNeal Cardwell case INET_DIAG_BC_AUTO: 8645e1f5420SNeal Cardwell case INET_DIAG_BC_JMP: 865a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 866a8c2190eSArnaldo Carvalho de Melo break; 867a8c2190eSArnaldo Carvalho de Melo default: 868a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 869a8c2190eSArnaldo Carvalho de Melo } 8705e1f5420SNeal Cardwell 8715e1f5420SNeal Cardwell if (op->code != INET_DIAG_BC_NOP) { 8725e1f5420SNeal Cardwell if (op->no < min_len || op->no > len + 4 || op->no & 3) 8735e1f5420SNeal Cardwell return -EINVAL; 8745e1f5420SNeal Cardwell if (op->no < len && 8755e1f5420SNeal Cardwell !valid_cc(bytecode, bytecode_len, len - op->no)) 8765e1f5420SNeal Cardwell return -EINVAL; 8775e1f5420SNeal Cardwell } 8785e1f5420SNeal Cardwell 879405c0059SNeal Cardwell if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 880eeb14972SEric Dumazet return -EINVAL; 881a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 882a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 883a8c2190eSArnaldo Carvalho de Melo } 884a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 885a8c2190eSArnaldo Carvalho de Melo } 886a8c2190eSArnaldo Carvalho de Melo 88749612729SEric Dumazet static void twsk_build_assert(void) 88849612729SEric Dumazet { 88949612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 89049612729SEric Dumazet offsetof(struct sock, sk_family)); 89149612729SEric Dumazet 89249612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 89349612729SEric Dumazet offsetof(struct inet_sock, inet_num)); 89449612729SEric Dumazet 89549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 89649612729SEric Dumazet offsetof(struct inet_sock, inet_dport)); 89749612729SEric Dumazet 89849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 89949612729SEric Dumazet offsetof(struct inet_sock, inet_rcv_saddr)); 90049612729SEric Dumazet 90149612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 90249612729SEric Dumazet offsetof(struct inet_sock, inet_daddr)); 90349612729SEric Dumazet 90449612729SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 90549612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 90649612729SEric Dumazet offsetof(struct sock, sk_v6_rcv_saddr)); 90749612729SEric Dumazet 90849612729SEric Dumazet BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 90949612729SEric Dumazet offsetof(struct sock, sk_v6_daddr)); 91049612729SEric Dumazet #endif 91149612729SEric Dumazet } 91249612729SEric Dumazet 9131942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 914e31c5e0eSEric Dumazet struct netlink_callback *cb, 9150df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r) 916a8c2190eSArnaldo Carvalho de Melo { 917d545cacaSLorenzo Colitti bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); 9180df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data; 91967db3e4bSEric Dumazet struct net *net = sock_net(skb->sk); 92067db3e4bSEric Dumazet u32 idiag_states = r->idiag_states; 92167db3e4bSEric Dumazet int i, num, s_i, s_num; 9220df6d328SMartin KaFai Lau struct nlattr *bc; 92367db3e4bSEric Dumazet struct sock *sk; 924a8c2190eSArnaldo Carvalho de Melo 9250df6d328SMartin KaFai Lau bc = cb_data->inet_diag_nla_bc; 926079096f1SEric Dumazet if (idiag_states & TCPF_SYN_RECV) 927079096f1SEric Dumazet idiag_states |= TCPF_NEW_SYN_RECV; 928a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 929a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 930a8c2190eSArnaldo Carvalho de Melo 931a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 9329652dc2eSEric Dumazet if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) 933a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 934a8c2190eSArnaldo Carvalho de Melo 935a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 9365caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 9378dbd76e7SEric Dumazet struct hlist_nulls_node *node; 938a8c2190eSArnaldo Carvalho de Melo 939a8c2190eSArnaldo Carvalho de Melo num = 0; 9405caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 9419652dc2eSEric Dumazet spin_lock(&ilb->lock); 9428dbd76e7SEric Dumazet sk_nulls_for_each(sk, node, &ilb->nulls_head) { 943a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 944a8c2190eSArnaldo Carvalho de Melo 94551d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 94651d7cccfSAndrey Vagin continue; 94751d7cccfSAndrey Vagin 948a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 949a8c2190eSArnaldo Carvalho de Melo num++; 950a8c2190eSArnaldo Carvalho de Melo continue; 951a8c2190eSArnaldo Carvalho de Melo } 952a8c2190eSArnaldo Carvalho de Melo 953d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 954d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 955d23deaa0SPavel Emelyanov goto next_listen; 956d23deaa0SPavel Emelyanov 957c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 958a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 959a8c2190eSArnaldo Carvalho de Melo goto next_listen; 960a8c2190eSArnaldo Carvalho de Melo 9615682d393SMartin KaFai Lau if (!inet_diag_bc_sk(bc, sk)) 9625682d393SMartin KaFai Lau goto next_listen; 9635682d393SMartin KaFai Lau 9645682d393SMartin KaFai Lau if (inet_sk_diag_fill(sk, inet_csk(sk), skb, 9655682d393SMartin KaFai Lau cb, r, NLM_F_MULTI, 9665682d393SMartin KaFai Lau net_admin) < 0) { 9679652dc2eSEric Dumazet spin_unlock(&ilb->lock); 968a8c2190eSArnaldo Carvalho de Melo goto done; 969a8c2190eSArnaldo Carvalho de Melo } 970a8c2190eSArnaldo Carvalho de Melo 971a8c2190eSArnaldo Carvalho de Melo next_listen: 972a8c2190eSArnaldo Carvalho de Melo ++num; 973a8c2190eSArnaldo Carvalho de Melo } 9749652dc2eSEric Dumazet spin_unlock(&ilb->lock); 975a8c2190eSArnaldo Carvalho de Melo 976a8c2190eSArnaldo Carvalho de Melo s_num = 0; 977a8c2190eSArnaldo Carvalho de Melo } 978a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 979a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 980a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 981a8c2190eSArnaldo Carvalho de Melo } 982a8c2190eSArnaldo Carvalho de Melo 983079096f1SEric Dumazet if (!(idiag_states & ~TCPF_LISTEN)) 984efb3cb42SPavel Emelyanov goto out; 985a8c2190eSArnaldo Carvalho de Melo 98667db3e4bSEric Dumazet #define SKARR_SZ 16 987f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 988a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 9897e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 9903ab5aee7SEric Dumazet struct hlist_nulls_node *node; 99167db3e4bSEric Dumazet struct sock *sk_arr[SKARR_SZ]; 99267db3e4bSEric Dumazet int num_arr[SKARR_SZ]; 99367db3e4bSEric Dumazet int idx, accum, res; 9946be547a6SAndi Kleen 99505dbc7b5SEric Dumazet if (hlist_nulls_empty(&head->chain)) 9966be547a6SAndi Kleen continue; 9976be547a6SAndi Kleen 998a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 999a8c2190eSArnaldo Carvalho de Melo s_num = 0; 1000a8c2190eSArnaldo Carvalho de Melo 100167db3e4bSEric Dumazet next_chunk: 100267db3e4bSEric Dumazet num = 0; 100367db3e4bSEric Dumazet accum = 0; 10047e3aab4aSDavid S. Miller spin_lock_bh(lock); 10053ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 100667db3e4bSEric Dumazet int state; 1007a8c2190eSArnaldo Carvalho de Melo 100851d7cccfSAndrey Vagin if (!net_eq(sock_net(sk), net)) 100951d7cccfSAndrey Vagin continue; 1010a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 1011a8c2190eSArnaldo Carvalho de Melo goto next_normal; 101270315d22SNeal Cardwell state = (sk->sk_state == TCP_TIME_WAIT) ? 101370315d22SNeal Cardwell inet_twsk(sk)->tw_substate : sk->sk_state; 1014079096f1SEric Dumazet if (!(idiag_states & (1 << state))) 1015a8c2190eSArnaldo Carvalho de Melo goto next_normal; 1016d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 1017d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 1018d23deaa0SPavel Emelyanov goto next_normal; 101905dbc7b5SEric Dumazet if (r->id.idiag_sport != htons(sk->sk_num) && 1020a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 1021a8c2190eSArnaldo Carvalho de Melo goto next_normal; 102205dbc7b5SEric Dumazet if (r->id.idiag_dport != sk->sk_dport && 10234e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 1024a8c2190eSArnaldo Carvalho de Melo goto next_normal; 1025a58917f5SEric Dumazet twsk_build_assert(); 1026a58917f5SEric Dumazet 1027a58917f5SEric Dumazet if (!inet_diag_bc_sk(bc, sk)) 1028a58917f5SEric Dumazet goto next_normal; 1029a58917f5SEric Dumazet 1030f0c928d8SEric Dumazet if (!refcount_inc_not_zero(&sk->sk_refcnt)) 1031f0c928d8SEric Dumazet goto next_normal; 1032f0c928d8SEric Dumazet 103367db3e4bSEric Dumazet num_arr[accum] = num; 103467db3e4bSEric Dumazet sk_arr[accum] = sk; 103567db3e4bSEric Dumazet if (++accum == SKARR_SZ) 103667db3e4bSEric Dumazet break; 103767db3e4bSEric Dumazet next_normal: 103867db3e4bSEric Dumazet ++num; 103967db3e4bSEric Dumazet } 104067db3e4bSEric Dumazet spin_unlock_bh(lock); 104167db3e4bSEric Dumazet res = 0; 104267db3e4bSEric Dumazet for (idx = 0; idx < accum; idx++) { 104367db3e4bSEric Dumazet if (res >= 0) { 10445682d393SMartin KaFai Lau res = sk_diag_fill(sk_arr[idx], skb, cb, r, 10455682d393SMartin KaFai Lau NLM_F_MULTI, net_admin); 104667db3e4bSEric Dumazet if (res < 0) 104767db3e4bSEric Dumazet num = num_arr[idx]; 1048a8c2190eSArnaldo Carvalho de Melo } 104967db3e4bSEric Dumazet sock_gen_put(sk_arr[idx]); 1050a8c2190eSArnaldo Carvalho de Melo } 105167db3e4bSEric Dumazet if (res < 0) 105267db3e4bSEric Dumazet break; 1053acffb584SEric Dumazet cond_resched(); 105467db3e4bSEric Dumazet if (accum == SKARR_SZ) { 105567db3e4bSEric Dumazet s_num = num + 1; 105667db3e4bSEric Dumazet goto next_chunk; 105767db3e4bSEric Dumazet } 1058a8c2190eSArnaldo Carvalho de Melo } 1059a8c2190eSArnaldo Carvalho de Melo 1060a8c2190eSArnaldo Carvalho de Melo done: 1061a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 1062a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 1063efb3cb42SPavel Emelyanov out: 1064efb3cb42SPavel Emelyanov ; 1065efb3cb42SPavel Emelyanov } 10661942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 1067efb3cb42SPavel Emelyanov 1068efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 10690df6d328SMartin KaFai Lau const struct inet_diag_req_v2 *r) 1070efb3cb42SPavel Emelyanov { 1071efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 1072*085c20caSMartin KaFai Lau u32 prev_min_dump_alloc; 1073cacb6ba0SCyrill Gorcunov int err = 0; 1074efb3cb42SPavel Emelyanov 1075*085c20caSMartin KaFai Lau again: 1076*085c20caSMartin KaFai Lau prev_min_dump_alloc = cb->min_dump_alloc; 1077efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 1078efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 10790df6d328SMartin KaFai Lau handler->dump(skb, cb, r); 1080cacb6ba0SCyrill Gorcunov else 1081cacb6ba0SCyrill Gorcunov err = PTR_ERR(handler); 1082d523a328SHerbert Xu inet_diag_unlock_handler(handler); 1083efb3cb42SPavel Emelyanov 1084*085c20caSMartin KaFai Lau /* The skb is not large enough to fit one sk info and 1085*085c20caSMartin KaFai Lau * inet_sk_diag_fill() has requested for a larger skb. 1086*085c20caSMartin KaFai Lau */ 1087*085c20caSMartin KaFai Lau if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) { 1088*085c20caSMartin KaFai Lau err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL); 1089*085c20caSMartin KaFai Lau if (!err) 1090*085c20caSMartin KaFai Lau goto again; 1091*085c20caSMartin KaFai Lau } 1092*085c20caSMartin KaFai Lau 1093cacb6ba0SCyrill Gorcunov return err ? : skb->len; 1094a8c2190eSArnaldo Carvalho de Melo } 1095a8c2190eSArnaldo Carvalho de Melo 109625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 109725c4cd2bSPavel Emelyanov { 10980df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh)); 10990df6d328SMartin KaFai Lau } 110025c4cd2bSPavel Emelyanov 11010df6d328SMartin KaFai Lau static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen) 11020df6d328SMartin KaFai Lau { 11030df6d328SMartin KaFai Lau const struct nlmsghdr *nlh = cb->nlh; 11040df6d328SMartin KaFai Lau struct inet_diag_dump_data *cb_data; 11050df6d328SMartin KaFai Lau struct sk_buff *skb = cb->skb; 11060df6d328SMartin KaFai Lau struct nlattr *nla; 11070df6d328SMartin KaFai Lau int rem, err; 110825c4cd2bSPavel Emelyanov 11090df6d328SMartin KaFai Lau cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL); 11100df6d328SMartin KaFai Lau if (!cb_data) 11110df6d328SMartin KaFai Lau return -ENOMEM; 11120df6d328SMartin KaFai Lau 11130df6d328SMartin KaFai Lau nla_for_each_attr(nla, nlmsg_attrdata(nlh, hdrlen), 11140df6d328SMartin KaFai Lau nlmsg_attrlen(nlh, hdrlen), rem) { 11150df6d328SMartin KaFai Lau int type = nla_type(nla); 11160df6d328SMartin KaFai Lau 11170df6d328SMartin KaFai Lau if (type < __INET_DIAG_REQ_MAX) 11180df6d328SMartin KaFai Lau cb_data->req_nlas[type] = nla; 11190df6d328SMartin KaFai Lau } 11200df6d328SMartin KaFai Lau 11210df6d328SMartin KaFai Lau nla = cb_data->inet_diag_nla_bc; 11220df6d328SMartin KaFai Lau if (nla) { 11230df6d328SMartin KaFai Lau err = inet_diag_bc_audit(nla, skb); 11240df6d328SMartin KaFai Lau if (err) { 11250df6d328SMartin KaFai Lau kfree(cb_data); 11260df6d328SMartin KaFai Lau return err; 11270df6d328SMartin KaFai Lau } 11280df6d328SMartin KaFai Lau } 11290df6d328SMartin KaFai Lau 1130*085c20caSMartin KaFai Lau nla = cb_data->inet_diag_nla_bpf_stgs; 1131*085c20caSMartin KaFai Lau if (nla) { 1132*085c20caSMartin KaFai Lau struct bpf_sk_storage_diag *bpf_stg_diag; 1133*085c20caSMartin KaFai Lau 1134*085c20caSMartin KaFai Lau bpf_stg_diag = bpf_sk_storage_diag_alloc(nla); 1135*085c20caSMartin KaFai Lau if (IS_ERR(bpf_stg_diag)) { 1136*085c20caSMartin KaFai Lau kfree(cb_data); 1137*085c20caSMartin KaFai Lau return PTR_ERR(bpf_stg_diag); 1138*085c20caSMartin KaFai Lau } 1139*085c20caSMartin KaFai Lau cb_data->bpf_stg_diag = bpf_stg_diag; 1140*085c20caSMartin KaFai Lau } 1141*085c20caSMartin KaFai Lau 11420df6d328SMartin KaFai Lau cb->data = cb_data; 11430df6d328SMartin KaFai Lau return 0; 11440df6d328SMartin KaFai Lau } 11450df6d328SMartin KaFai Lau 11460df6d328SMartin KaFai Lau static int inet_diag_dump_start(struct netlink_callback *cb) 11470df6d328SMartin KaFai Lau { 11480df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2)); 11490df6d328SMartin KaFai Lau } 11500df6d328SMartin KaFai Lau 11510df6d328SMartin KaFai Lau static int inet_diag_dump_start_compat(struct netlink_callback *cb) 11520df6d328SMartin KaFai Lau { 11530df6d328SMartin KaFai Lau return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req)); 11540df6d328SMartin KaFai Lau } 11550df6d328SMartin KaFai Lau 11560df6d328SMartin KaFai Lau static int inet_diag_dump_done(struct netlink_callback *cb) 11570df6d328SMartin KaFai Lau { 1158*085c20caSMartin KaFai Lau struct inet_diag_dump_data *cb_data = cb->data; 1159*085c20caSMartin KaFai Lau 1160*085c20caSMartin KaFai Lau bpf_sk_storage_diag_free(cb_data->bpf_stg_diag); 11610df6d328SMartin KaFai Lau kfree(cb->data); 11620df6d328SMartin KaFai Lau 11630df6d328SMartin KaFai Lau return 0; 116425c4cd2bSPavel Emelyanov } 116525c4cd2bSPavel Emelyanov 1166e31c5e0eSEric Dumazet static int inet_diag_type2proto(int type) 1167a029fe26SPavel Emelyanov { 1168a029fe26SPavel Emelyanov switch (type) { 1169a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 1170a029fe26SPavel Emelyanov return IPPROTO_TCP; 1171a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 1172a029fe26SPavel Emelyanov return IPPROTO_DCCP; 1173a029fe26SPavel Emelyanov default: 1174a029fe26SPavel Emelyanov return 0; 1175a029fe26SPavel Emelyanov } 1176a029fe26SPavel Emelyanov } 1177a029fe26SPavel Emelyanov 1178e31c5e0eSEric Dumazet static int inet_diag_dump_compat(struct sk_buff *skb, 1179e31c5e0eSEric Dumazet struct netlink_callback *cb) 118025c4cd2bSPavel Emelyanov { 1181d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1182c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 118325c4cd2bSPavel Emelyanov 1184d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 118525c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 118625c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 118725c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 118825c4cd2bSPavel Emelyanov req.id = rc->id; 118925c4cd2bSPavel Emelyanov 11900df6d328SMartin KaFai Lau return __inet_diag_dump(skb, cb, &req); 119125c4cd2bSPavel Emelyanov } 119225c4cd2bSPavel Emelyanov 1193fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1194fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 1195fe50ce28SPavel Emelyanov { 1196d106352dSDavid S. Miller struct inet_diag_req *rc = nlmsg_data(nlh); 1197c8991362SPavel Emelyanov struct inet_diag_req_v2 req; 1198fe50ce28SPavel Emelyanov 1199fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 1200fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1201fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 1202fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 1203fe50ce28SPavel Emelyanov req.id = rc->id; 1204fe50ce28SPavel Emelyanov 12056eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req); 1206fe50ce28SPavel Emelyanov } 1207fe50ce28SPavel Emelyanov 12088d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1209a8c2190eSArnaldo Carvalho de Melo { 12103b09c84cSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 121151d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1212a8c2190eSArnaldo Carvalho de Melo 1213ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1214ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 1215ead592baSThomas Graf return -EINVAL; 1216a8c2190eSArnaldo Carvalho de Melo 1217b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 121880d326faSPablo Neira Ayuso struct netlink_dump_control c = { 12190df6d328SMartin KaFai Lau .start = inet_diag_dump_start_compat, 12200df6d328SMartin KaFai Lau .done = inet_diag_dump_done, 122180d326faSPablo Neira Ayuso .dump = inet_diag_dump_compat, 122280d326faSPablo Neira Ayuso }; 122351d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 122480d326faSPablo Neira Ayuso } 1225ead592baSThomas Graf 1226fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 1227a8c2190eSArnaldo Carvalho de Melo } 1228a8c2190eSArnaldo Carvalho de Melo 12296eb5d2e0SLorenzo Colitti static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 1230d366477aSPavel Emelyanov { 1231c8991362SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_v2); 123251d7cccfSAndrey Vagin struct net *net = sock_net(skb->sk); 1233d366477aSPavel Emelyanov 1234d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 1235d366477aSPavel Emelyanov return -EINVAL; 1236d366477aSPavel Emelyanov 12376eb5d2e0SLorenzo Colitti if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 12386eb5d2e0SLorenzo Colitti h->nlmsg_flags & NLM_F_DUMP) { 123980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 12400df6d328SMartin KaFai Lau .start = inet_diag_dump_start, 12410df6d328SMartin KaFai Lau .done = inet_diag_dump_done, 124280d326faSPablo Neira Ayuso .dump = inet_diag_dump, 124380d326faSPablo Neira Ayuso }; 124451d7cccfSAndrey Vagin return netlink_dump_start(net->diag_nlsk, skb, h, &c); 124580d326faSPablo Neira Ayuso } 1246d366477aSPavel Emelyanov 12476eb5d2e0SLorenzo Colitti return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); 1248d366477aSPavel Emelyanov } 1249d366477aSPavel Emelyanov 125035ac838aSCraig Gallek static 125135ac838aSCraig Gallek int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 125235ac838aSCraig Gallek { 125335ac838aSCraig Gallek const struct inet_diag_handler *handler; 125435ac838aSCraig Gallek struct nlmsghdr *nlh; 125535ac838aSCraig Gallek struct nlattr *attr; 125635ac838aSCraig Gallek struct inet_diag_msg *r; 125735ac838aSCraig Gallek void *info = NULL; 125835ac838aSCraig Gallek int err = 0; 125935ac838aSCraig Gallek 126035ac838aSCraig Gallek nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 126135ac838aSCraig Gallek if (!nlh) 126235ac838aSCraig Gallek return -ENOMEM; 126335ac838aSCraig Gallek 126435ac838aSCraig Gallek r = nlmsg_data(nlh); 126535ac838aSCraig Gallek memset(r, 0, sizeof(*r)); 126635ac838aSCraig Gallek inet_diag_msg_common_fill(r, sk); 1267e0df02e0SCraig Gallek if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1268e0df02e0SCraig Gallek r->id.idiag_sport = inet_sk(sk)->inet_sport; 126935ac838aSCraig Gallek r->idiag_state = sk->sk_state; 127035ac838aSCraig Gallek 127135ac838aSCraig Gallek if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 127235ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 127335ac838aSCraig Gallek return err; 127435ac838aSCraig Gallek } 127535ac838aSCraig Gallek 127635ac838aSCraig Gallek handler = inet_diag_lock_handler(sk->sk_protocol); 127735ac838aSCraig Gallek if (IS_ERR(handler)) { 127835ac838aSCraig Gallek inet_diag_unlock_handler(handler); 127935ac838aSCraig Gallek nlmsg_cancel(skb, nlh); 128035ac838aSCraig Gallek return PTR_ERR(handler); 128135ac838aSCraig Gallek } 128235ac838aSCraig Gallek 128335ac838aSCraig Gallek attr = handler->idiag_info_size 12846ed46d12SNicolas Dichtel ? nla_reserve_64bit(skb, INET_DIAG_INFO, 12856ed46d12SNicolas Dichtel handler->idiag_info_size, 12866ed46d12SNicolas Dichtel INET_DIAG_PAD) 128735ac838aSCraig Gallek : NULL; 128835ac838aSCraig Gallek if (attr) 128935ac838aSCraig Gallek info = nla_data(attr); 129035ac838aSCraig Gallek 129135ac838aSCraig Gallek handler->idiag_get_info(sk, r, info); 129235ac838aSCraig Gallek inet_diag_unlock_handler(handler); 129335ac838aSCraig Gallek 129435ac838aSCraig Gallek nlmsg_end(skb, nlh); 129535ac838aSCraig Gallek return 0; 129635ac838aSCraig Gallek } 129735ac838aSCraig Gallek 12988dcf01fcSShan Wei static const struct sock_diag_handler inet_diag_handler = { 1299d366477aSPavel Emelyanov .family = AF_INET, 13006eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 130135ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 13026eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1303d366477aSPavel Emelyanov }; 1304d366477aSPavel Emelyanov 13058dcf01fcSShan Wei static const struct sock_diag_handler inet6_diag_handler = { 1306d366477aSPavel Emelyanov .family = AF_INET6, 13076eb5d2e0SLorenzo Colitti .dump = inet_diag_handler_cmd, 130835ac838aSCraig Gallek .get_info = inet_diag_handler_get_info, 13096eb5d2e0SLorenzo Colitti .destroy = inet_diag_handler_cmd, 1310d366477aSPavel Emelyanov }; 1311d366477aSPavel Emelyanov 1312a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1313a8c2190eSArnaldo Carvalho de Melo { 1314a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1315a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1316a8c2190eSArnaldo Carvalho de Melo 1317f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1318a8c2190eSArnaldo Carvalho de Melo goto out; 1319a8c2190eSArnaldo Carvalho de Melo 1320d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1321a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1322e31c5e0eSEric Dumazet if (!inet_diag_table[type]) { 1323a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1324a8c2190eSArnaldo Carvalho de Melo err = 0; 1325a8c2190eSArnaldo Carvalho de Melo } 1326d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1327a8c2190eSArnaldo Carvalho de Melo out: 1328a8c2190eSArnaldo Carvalho de Melo return err; 1329a8c2190eSArnaldo Carvalho de Melo } 1330a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1331a8c2190eSArnaldo Carvalho de Melo 1332a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1333a8c2190eSArnaldo Carvalho de Melo { 1334a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1335a8c2190eSArnaldo Carvalho de Melo 1336f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1337a8c2190eSArnaldo Carvalho de Melo return; 1338a8c2190eSArnaldo Carvalho de Melo 1339d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1340a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1341d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1342a8c2190eSArnaldo Carvalho de Melo } 1343a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1344a8c2190eSArnaldo Carvalho de Melo 1345a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1346a8c2190eSArnaldo Carvalho de Melo { 1347f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1348a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1349a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1350a8c2190eSArnaldo Carvalho de Melo 13510da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1352a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1353a8c2190eSArnaldo Carvalho de Melo goto out; 1354a8c2190eSArnaldo Carvalho de Melo 1355d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1356d366477aSPavel Emelyanov if (err) 1357d366477aSPavel Emelyanov goto out_free_nl; 1358d366477aSPavel Emelyanov 1359d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1360d366477aSPavel Emelyanov if (err) 1361d366477aSPavel Emelyanov goto out_free_inet; 1362d366477aSPavel Emelyanov 13638ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1364a8c2190eSArnaldo Carvalho de Melo out: 1365a8c2190eSArnaldo Carvalho de Melo return err; 1366d366477aSPavel Emelyanov 1367d366477aSPavel Emelyanov out_free_inet: 1368d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1369d366477aSPavel Emelyanov out_free_nl: 1370a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1371a8c2190eSArnaldo Carvalho de Melo goto out; 1372a8c2190eSArnaldo Carvalho de Melo } 1373a8c2190eSArnaldo Carvalho de Melo 1374a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1375a8c2190eSArnaldo Carvalho de Melo { 1376d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1377d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 13788ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1379a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1380a8c2190eSArnaldo Carvalho de Melo } 1381a8c2190eSArnaldo Carvalho de Melo 1382a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1383a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1384a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1385aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1386aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1387