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> 36a8c2190eSArnaldo Carvalho de Melo 37a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table; 38a8c2190eSArnaldo Carvalho de Melo 39a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry { 409f855299SAl Viro __be32 *saddr; 419f855299SAl Viro __be32 *daddr; 42a8c2190eSArnaldo Carvalho de Melo u16 sport; 43a8c2190eSArnaldo Carvalho de Melo u16 dport; 44a8c2190eSArnaldo Carvalho de Melo u16 family; 45a8c2190eSArnaldo Carvalho de Melo u16 userlocks; 46a8c2190eSArnaldo Carvalho de Melo }; 47a8c2190eSArnaldo Carvalho de Melo 487f1fb60cSPavel Emelyanov static struct sock *sdiagnl; 49a8c2190eSArnaldo Carvalho de Melo 50a8c2190eSArnaldo Carvalho de Melo #define INET_DIAG_PUT(skb, attrtype, attrlen) \ 51a8c2190eSArnaldo Carvalho de Melo RTA_DATA(__RTA_PUT(skb, attrtype, attrlen)) 52a8c2190eSArnaldo Carvalho de Melo 53d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 54d523a328SHerbert Xu 55d523a328SHerbert Xu static const struct inet_diag_handler *inet_diag_lock_handler(int type) 56d523a328SHerbert Xu { 57d523a328SHerbert Xu if (!inet_diag_table[type]) 58d523a328SHerbert Xu request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 597f1fb60cSPavel Emelyanov NETLINK_SOCK_DIAG, type); 60d523a328SHerbert Xu 61d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 62d523a328SHerbert Xu if (!inet_diag_table[type]) 63d523a328SHerbert Xu return ERR_PTR(-ENOENT); 64d523a328SHerbert Xu 65d523a328SHerbert Xu return inet_diag_table[type]; 66d523a328SHerbert Xu } 67d523a328SHerbert Xu 68d523a328SHerbert Xu static inline void inet_diag_unlock_handler( 69d523a328SHerbert Xu const struct inet_diag_handler *handler) 70d523a328SHerbert Xu { 71d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 72d523a328SHerbert Xu } 73d523a328SHerbert Xu 74dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_fill(struct sock *sk, 75dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 76a8c2190eSArnaldo Carvalho de Melo int ext, u32 pid, u32 seq, u16 nlmsg_flags, 77a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 78a8c2190eSArnaldo Carvalho de Melo { 79a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 80a8c2190eSArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 81a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 82a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 83a8c2190eSArnaldo Carvalho de Melo void *info = NULL; 84a8c2190eSArnaldo Carvalho de Melo struct inet_diag_meminfo *minfo = NULL; 8527a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 86a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 87a8c2190eSArnaldo Carvalho de Melo 88a8c2190eSArnaldo Carvalho de Melo handler = inet_diag_table[unlh->nlmsg_type]; 89a8c2190eSArnaldo Carvalho de Melo BUG_ON(handler == NULL); 90a8c2190eSArnaldo Carvalho de Melo 91a8c2190eSArnaldo Carvalho de Melo nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 92a8c2190eSArnaldo Carvalho de Melo nlh->nlmsg_flags = nlmsg_flags; 93a8c2190eSArnaldo Carvalho de Melo 94a8c2190eSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 95c7d58aabSArnaldo Carvalho de Melo BUG_ON(sk->sk_state == TCP_TIME_WAIT); 96c7d58aabSArnaldo Carvalho de Melo 97a8c2190eSArnaldo Carvalho de Melo if (ext & (1 << (INET_DIAG_MEMINFO - 1))) 98c7d58aabSArnaldo Carvalho de Melo minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo)); 99c7d58aabSArnaldo Carvalho de Melo 100a8c2190eSArnaldo Carvalho de Melo if (ext & (1 << (INET_DIAG_INFO - 1))) 101a8c2190eSArnaldo Carvalho de Melo info = INET_DIAG_PUT(skb, INET_DIAG_INFO, 102a8c2190eSArnaldo Carvalho de Melo handler->idiag_info_size); 103a8c2190eSArnaldo Carvalho de Melo 104a8c2190eSArnaldo Carvalho de Melo if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) { 105c7d58aabSArnaldo Carvalho de Melo const size_t len = strlen(icsk->icsk_ca_ops->name); 106c7d58aabSArnaldo Carvalho de Melo 107a8c2190eSArnaldo Carvalho de Melo strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1), 108a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->name); 109a8c2190eSArnaldo Carvalho de Melo } 110c7d58aabSArnaldo Carvalho de Melo 111a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 112a8c2190eSArnaldo Carvalho de Melo r->idiag_state = sk->sk_state; 113a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 114a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = 0; 115a8c2190eSArnaldo Carvalho de Melo 116a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 117a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)sk; 118a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); 119a8c2190eSArnaldo Carvalho de Melo 120c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 121c720c7e8SEric Dumazet r->id.idiag_dport = inet->inet_dport; 122c720c7e8SEric Dumazet r->id.idiag_src[0] = inet->inet_rcv_saddr; 123c720c7e8SEric Dumazet r->id.idiag_dst[0] = inet->inet_daddr; 124a8c2190eSArnaldo Carvalho de Melo 125717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 126717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 127717b6d83SMaciej Żenczykowski */ 128717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 129717b6d83SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos); 130717b6d83SMaciej Żenczykowski 131a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 132a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 133b71d1d42SEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 134a8c2190eSArnaldo Carvalho de Melo 1354e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 1364e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = np->daddr; 13706236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 13806236ac3SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); 139a8c2190eSArnaldo Carvalho de Melo } 140a8c2190eSArnaldo Carvalho de Melo #endif 141a8c2190eSArnaldo Carvalho de Melo 142172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 143a8c2190eSArnaldo Carvalho de Melo 144a8c2190eSArnaldo Carvalho de Melo if (icsk->icsk_pending == ICSK_TIME_RETRANS) { 145a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 146a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 147a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 148a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 149a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 150a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 151a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 152a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 153a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 154a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 155a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 156a8c2190eSArnaldo Carvalho de Melo } else { 157a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 158a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 159a8c2190eSArnaldo Carvalho de Melo } 160a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS 161a8c2190eSArnaldo Carvalho de Melo 162a8c2190eSArnaldo Carvalho de Melo r->idiag_uid = sock_i_uid(sk); 163a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = sock_i_ino(sk); 164a8c2190eSArnaldo Carvalho de Melo 165a8c2190eSArnaldo Carvalho de Melo if (minfo) { 16631e6d363SEric Dumazet minfo->idiag_rmem = sk_rmem_alloc_get(sk); 167a8c2190eSArnaldo Carvalho de Melo minfo->idiag_wmem = sk->sk_wmem_queued; 168a8c2190eSArnaldo Carvalho de Melo minfo->idiag_fmem = sk->sk_forward_alloc; 16931e6d363SEric Dumazet minfo->idiag_tmem = sk_wmem_alloc_get(sk); 170a8c2190eSArnaldo Carvalho de Melo } 171a8c2190eSArnaldo Carvalho de Melo 172a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 173a8c2190eSArnaldo Carvalho de Melo 174a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state < TCP_TIME_WAIT && 175a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 176a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->get_info(sk, ext, skb); 177a8c2190eSArnaldo Carvalho de Melo 17827a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 179a8c2190eSArnaldo Carvalho de Melo return skb->len; 180a8c2190eSArnaldo Carvalho de Melo 181a8c2190eSArnaldo Carvalho de Melo rtattr_failure: 182a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 183dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 18426932566SPatrick McHardy return -EMSGSIZE; 185a8c2190eSArnaldo Carvalho de Melo } 186a8c2190eSArnaldo Carvalho de Melo 187c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 188c7d58aabSArnaldo Carvalho de Melo struct sk_buff *skb, int ext, u32 pid, 189c7d58aabSArnaldo Carvalho de Melo u32 seq, u16 nlmsg_flags, 190c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 191c7d58aabSArnaldo Carvalho de Melo { 192c7d58aabSArnaldo Carvalho de Melo long tmo; 193c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 19427a884dcSArnaldo Carvalho de Melo const unsigned char *previous_tail = skb_tail_pointer(skb); 195c7d58aabSArnaldo Carvalho de Melo struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, 196c7d58aabSArnaldo Carvalho de Melo unlh->nlmsg_type, sizeof(*r)); 197c7d58aabSArnaldo Carvalho de Melo 198c7d58aabSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 199c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 200c7d58aabSArnaldo Carvalho de Melo 201c7d58aabSArnaldo Carvalho de Melo nlh->nlmsg_flags = nlmsg_flags; 202c7d58aabSArnaldo Carvalho de Melo 203c7d58aabSArnaldo Carvalho de Melo tmo = tw->tw_ttd - jiffies; 204c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 205c7d58aabSArnaldo Carvalho de Melo tmo = 0; 206c7d58aabSArnaldo Carvalho de Melo 207c7d58aabSArnaldo Carvalho de Melo r->idiag_family = tw->tw_family; 208c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 209c7d58aabSArnaldo Carvalho de Melo r->id.idiag_if = tw->tw_bound_dev_if; 210c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)tw; 211c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1); 212c7d58aabSArnaldo Carvalho de Melo r->id.idiag_sport = tw->tw_sport; 213c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dport = tw->tw_dport; 214c7d58aabSArnaldo Carvalho de Melo r->id.idiag_src[0] = tw->tw_rcv_saddr; 215c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dst[0] = tw->tw_daddr; 216c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 217c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 218172589ccSIlpo Järvinen r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 219c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 220c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 221c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 222c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 223c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 224c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 225c7d58aabSArnaldo Carvalho de Melo const struct inet6_timewait_sock *tw6 = 226c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 227c7d58aabSArnaldo Carvalho de Melo 2284e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr; 2294e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr; 230c7d58aabSArnaldo Carvalho de Melo } 231c7d58aabSArnaldo Carvalho de Melo #endif 23227a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; 233c7d58aabSArnaldo Carvalho de Melo return skb->len; 234c7d58aabSArnaldo Carvalho de Melo nlmsg_failure: 235dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, previous_tail); 23626932566SPatrick McHardy return -EMSGSIZE; 237c7d58aabSArnaldo Carvalho de Melo } 238c7d58aabSArnaldo Carvalho de Melo 239dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 240dff2c035SArnaldo Carvalho de Melo int ext, u32 pid, u32 seq, u16 nlmsg_flags, 241dff2c035SArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 242dff2c035SArnaldo Carvalho de Melo { 243dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 244dff2c035SArnaldo Carvalho de Melo return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 245dff2c035SArnaldo Carvalho de Melo skb, ext, pid, seq, nlmsg_flags, 246dff2c035SArnaldo Carvalho de Melo unlh); 247dff2c035SArnaldo Carvalho de Melo return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh); 248dff2c035SArnaldo Carvalho de Melo } 249dff2c035SArnaldo Carvalho de Melo 2504e852c02SArnaldo Carvalho de Melo static int inet_diag_get_exact(struct sk_buff *in_skb, 2514e852c02SArnaldo Carvalho de Melo const struct nlmsghdr *nlh) 252a8c2190eSArnaldo Carvalho de Melo { 253a8c2190eSArnaldo Carvalho de Melo int err; 254a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 255a8c2190eSArnaldo Carvalho de Melo struct inet_diag_req *req = NLMSG_DATA(nlh); 256a8c2190eSArnaldo Carvalho de Melo struct sk_buff *rep; 257a8c2190eSArnaldo Carvalho de Melo struct inet_hashinfo *hashinfo; 258a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 259a8c2190eSArnaldo Carvalho de Melo 260d523a328SHerbert Xu handler = inet_diag_lock_handler(nlh->nlmsg_type); 2618cf8e5a6SArnaldo Carvalho de Melo if (IS_ERR(handler)) { 2628cf8e5a6SArnaldo Carvalho de Melo err = PTR_ERR(handler); 2638cf8e5a6SArnaldo Carvalho de Melo goto unlock; 2648cf8e5a6SArnaldo Carvalho de Melo } 265d523a328SHerbert Xu 266a8c2190eSArnaldo Carvalho de Melo hashinfo = handler->idiag_hashinfo; 267d523a328SHerbert Xu err = -EINVAL; 268a8c2190eSArnaldo Carvalho de Melo 269a8c2190eSArnaldo Carvalho de Melo if (req->idiag_family == AF_INET) { 270c67499c0SPavel Emelyanov sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], 271a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 272a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 273a8c2190eSArnaldo Carvalho de Melo } 274a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 275a8c2190eSArnaldo Carvalho de Melo else if (req->idiag_family == AF_INET6) { 276d86e0dacSPavel Emelyanov sk = inet6_lookup(&init_net, hashinfo, 277a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 278a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 279a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 280a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 281a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 282a8c2190eSArnaldo Carvalho de Melo } 283a8c2190eSArnaldo Carvalho de Melo #endif 284a8c2190eSArnaldo Carvalho de Melo else { 285d523a328SHerbert Xu goto unlock; 286a8c2190eSArnaldo Carvalho de Melo } 287a8c2190eSArnaldo Carvalho de Melo 288d523a328SHerbert Xu err = -ENOENT; 289a8c2190eSArnaldo Carvalho de Melo if (sk == NULL) 290d523a328SHerbert Xu goto unlock; 291a8c2190eSArnaldo Carvalho de Melo 292a8c2190eSArnaldo Carvalho de Melo err = -ESTALE; 293a8c2190eSArnaldo Carvalho de Melo if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE || 294a8c2190eSArnaldo Carvalho de Melo req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) && 295a8c2190eSArnaldo Carvalho de Melo ((u32)(unsigned long)sk != req->id.idiag_cookie[0] || 296a8c2190eSArnaldo Carvalho de Melo (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1])) 297a8c2190eSArnaldo Carvalho de Melo goto out; 298a8c2190eSArnaldo Carvalho de Melo 299a8c2190eSArnaldo Carvalho de Melo err = -ENOMEM; 300a8c2190eSArnaldo Carvalho de Melo rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + 301a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_meminfo) + 302a8c2190eSArnaldo Carvalho de Melo handler->idiag_info_size + 64)), 303a8c2190eSArnaldo Carvalho de Melo GFP_KERNEL); 304a8c2190eSArnaldo Carvalho de Melo if (!rep) 305a8c2190eSArnaldo Carvalho de Melo goto out; 306a8c2190eSArnaldo Carvalho de Melo 30726932566SPatrick McHardy err = sk_diag_fill(sk, rep, req->idiag_ext, 308a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(in_skb).pid, 30926932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 31026932566SPatrick McHardy if (err < 0) { 31126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 31226932566SPatrick McHardy kfree_skb(rep); 31326932566SPatrick McHardy goto out; 31426932566SPatrick McHardy } 3157f1fb60cSPavel Emelyanov err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid, 316a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 317a8c2190eSArnaldo Carvalho de Melo if (err > 0) 318a8c2190eSArnaldo Carvalho de Melo err = 0; 319a8c2190eSArnaldo Carvalho de Melo 320a8c2190eSArnaldo Carvalho de Melo out: 321a8c2190eSArnaldo Carvalho de Melo if (sk) { 322a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 323a8c2190eSArnaldo Carvalho de Melo inet_twsk_put((struct inet_timewait_sock *)sk); 324a8c2190eSArnaldo Carvalho de Melo else 325a8c2190eSArnaldo Carvalho de Melo sock_put(sk); 326a8c2190eSArnaldo Carvalho de Melo } 327d523a328SHerbert Xu unlock: 328d523a328SHerbert Xu inet_diag_unlock_handler(handler); 329a8c2190eSArnaldo Carvalho de Melo return err; 330a8c2190eSArnaldo Carvalho de Melo } 331a8c2190eSArnaldo Carvalho de Melo 3329f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 333a8c2190eSArnaldo Carvalho de Melo { 334a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 335a8c2190eSArnaldo Carvalho de Melo 336a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 337a8c2190eSArnaldo Carvalho de Melo 338a8c2190eSArnaldo Carvalho de Melo if (words) { 339a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 340a8c2190eSArnaldo Carvalho de Melo return 0; 341a8c2190eSArnaldo Carvalho de Melo } 342a8c2190eSArnaldo Carvalho de Melo if (bits) { 3439f855299SAl Viro __be32 w1, w2; 3449f855299SAl Viro __be32 mask; 345a8c2190eSArnaldo Carvalho de Melo 346a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 347a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 348a8c2190eSArnaldo Carvalho de Melo 349a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 350a8c2190eSArnaldo Carvalho de Melo 351a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 352a8c2190eSArnaldo Carvalho de Melo return 0; 353a8c2190eSArnaldo Carvalho de Melo } 354a8c2190eSArnaldo Carvalho de Melo 355a8c2190eSArnaldo Carvalho de Melo return 1; 356a8c2190eSArnaldo Carvalho de Melo } 357a8c2190eSArnaldo Carvalho de Melo 358a8c2190eSArnaldo Carvalho de Melo 359a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_run(const void *bc, int len, 360a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 361a8c2190eSArnaldo Carvalho de Melo { 362a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 363a8c2190eSArnaldo Carvalho de Melo int yes = 1; 364a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 365a8c2190eSArnaldo Carvalho de Melo 366a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 367a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 368a8c2190eSArnaldo Carvalho de Melo break; 369a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 370a8c2190eSArnaldo Carvalho de Melo yes = 0; 371a8c2190eSArnaldo Carvalho de Melo break; 372a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 373a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 374a8c2190eSArnaldo Carvalho de Melo break; 375a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 376b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 377a8c2190eSArnaldo Carvalho de Melo break; 378a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 379a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 380a8c2190eSArnaldo Carvalho de Melo break; 381a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 382a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 383a8c2190eSArnaldo Carvalho de Melo break; 384a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 385a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 386a8c2190eSArnaldo Carvalho de Melo break; 387a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 388a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 389a8c2190eSArnaldo Carvalho de Melo struct inet_diag_hostcond *cond; 3909f855299SAl Viro __be32 *addr; 391a8c2190eSArnaldo Carvalho de Melo 392a8c2190eSArnaldo Carvalho de Melo cond = (struct inet_diag_hostcond *)(op + 1); 393a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 394a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 395a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 396a8c2190eSArnaldo Carvalho de Melo yes = 0; 397a8c2190eSArnaldo Carvalho de Melo break; 398a8c2190eSArnaldo Carvalho de Melo } 399a8c2190eSArnaldo Carvalho de Melo 400a8c2190eSArnaldo Carvalho de Melo if (cond->prefix_len == 0) 401a8c2190eSArnaldo Carvalho de Melo break; 402a8c2190eSArnaldo Carvalho de Melo 403a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 404a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 405a8c2190eSArnaldo Carvalho de Melo else 406a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 407a8c2190eSArnaldo Carvalho de Melo 4084e852c02SArnaldo Carvalho de Melo if (bitstring_match(addr, cond->addr, 4094e852c02SArnaldo Carvalho de Melo cond->prefix_len)) 410a8c2190eSArnaldo Carvalho de Melo break; 411a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 412a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 413a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 414a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 415a8c2190eSArnaldo Carvalho de Melo bitstring_match(addr + 3, cond->addr, 416a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 417a8c2190eSArnaldo Carvalho de Melo break; 418a8c2190eSArnaldo Carvalho de Melo } 419a8c2190eSArnaldo Carvalho de Melo yes = 0; 420a8c2190eSArnaldo Carvalho de Melo break; 421a8c2190eSArnaldo Carvalho de Melo } 422a8c2190eSArnaldo Carvalho de Melo } 423a8c2190eSArnaldo Carvalho de Melo 424a8c2190eSArnaldo Carvalho de Melo if (yes) { 425a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 426a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 427a8c2190eSArnaldo Carvalho de Melo } else { 428a8c2190eSArnaldo Carvalho de Melo len -= op->no; 429a8c2190eSArnaldo Carvalho de Melo bc += op->no; 430a8c2190eSArnaldo Carvalho de Melo } 431a8c2190eSArnaldo Carvalho de Melo } 432a02cec21SEric Dumazet return len == 0; 433a8c2190eSArnaldo Carvalho de Melo } 434a8c2190eSArnaldo Carvalho de Melo 435a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 436a8c2190eSArnaldo Carvalho de Melo { 437a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 438a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 439a8c2190eSArnaldo Carvalho de Melo 440a8c2190eSArnaldo Carvalho de Melo if (cc > len) 441a8c2190eSArnaldo Carvalho de Melo return 0; 442a8c2190eSArnaldo Carvalho de Melo if (cc == len) 443a8c2190eSArnaldo Carvalho de Melo return 1; 444eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 445a8c2190eSArnaldo Carvalho de Melo return 0; 446a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 447a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 448a8c2190eSArnaldo Carvalho de Melo } 449a8c2190eSArnaldo Carvalho de Melo return 0; 450a8c2190eSArnaldo Carvalho de Melo } 451a8c2190eSArnaldo Carvalho de Melo 452a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 453a8c2190eSArnaldo Carvalho de Melo { 454eeb14972SEric Dumazet const void *bc = bytecode; 455a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 456a8c2190eSArnaldo Carvalho de Melo 457a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 458eeb14972SEric Dumazet const struct inet_diag_bc_op *op = bc; 459a8c2190eSArnaldo Carvalho de Melo 460a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); 461a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 462a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 463a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 464a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 465a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 466a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 467a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 468a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 469a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 470eeb14972SEric Dumazet if (op->no < 4 || op->no > len + 4 || op->no & 3) 471a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 472a8c2190eSArnaldo Carvalho de Melo if (op->no < len && 473a8c2190eSArnaldo Carvalho de Melo !valid_cc(bytecode, bytecode_len, len - op->no)) 474a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 475a8c2190eSArnaldo Carvalho de Melo break; 476a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 477a8c2190eSArnaldo Carvalho de Melo break; 478a8c2190eSArnaldo Carvalho de Melo default: 479a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 480a8c2190eSArnaldo Carvalho de Melo } 481eeb14972SEric Dumazet if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) 482eeb14972SEric Dumazet return -EINVAL; 483a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 484a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 485a8c2190eSArnaldo Carvalho de Melo } 486a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 487a8c2190eSArnaldo Carvalho de Melo } 488a8c2190eSArnaldo Carvalho de Melo 489dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 490dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 491a8c2190eSArnaldo Carvalho de Melo struct netlink_callback *cb) 492a8c2190eSArnaldo Carvalho de Melo { 493a8c2190eSArnaldo Carvalho de Melo struct inet_diag_req *r = NLMSG_DATA(cb->nlh); 494a8c2190eSArnaldo Carvalho de Melo 49522e76c84SNelson Elhage if (nlmsg_attrlen(cb->nlh, sizeof(*r))) { 496a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry entry; 49722e76c84SNelson Elhage const struct nlattr *bc = nlmsg_find_attr(cb->nlh, 49822e76c84SNelson Elhage sizeof(*r), 49922e76c84SNelson Elhage INET_DIAG_REQ_BYTECODE); 500a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 501a8c2190eSArnaldo Carvalho de Melo 502a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 503a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 504a8c2190eSArnaldo Carvalho de Melo if (entry.family == AF_INET6) { 505a8c2190eSArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 506a8c2190eSArnaldo Carvalho de Melo 507a8c2190eSArnaldo Carvalho de Melo entry.saddr = np->rcv_saddr.s6_addr32; 508a8c2190eSArnaldo Carvalho de Melo entry.daddr = np->daddr.s6_addr32; 509a8c2190eSArnaldo Carvalho de Melo } else 510a8c2190eSArnaldo Carvalho de Melo #endif 511a8c2190eSArnaldo Carvalho de Melo { 512c720c7e8SEric Dumazet entry.saddr = &inet->inet_rcv_saddr; 513c720c7e8SEric Dumazet entry.daddr = &inet->inet_daddr; 514a8c2190eSArnaldo Carvalho de Melo } 515c720c7e8SEric Dumazet entry.sport = inet->inet_num; 516c720c7e8SEric Dumazet entry.dport = ntohs(inet->inet_dport); 517a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 518a8c2190eSArnaldo Carvalho de Melo 51922e76c84SNelson Elhage if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) 520a8c2190eSArnaldo Carvalho de Melo return 0; 521a8c2190eSArnaldo Carvalho de Melo } 522a8c2190eSArnaldo Carvalho de Melo 523dff2c035SArnaldo Carvalho de Melo return inet_csk_diag_fill(sk, skb, r->idiag_ext, 524dff2c035SArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 525a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 526a8c2190eSArnaldo Carvalho de Melo } 527a8c2190eSArnaldo Carvalho de Melo 528c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 529c7d58aabSArnaldo Carvalho de Melo struct sk_buff *skb, 530c7d58aabSArnaldo Carvalho de Melo struct netlink_callback *cb) 531c7d58aabSArnaldo Carvalho de Melo { 532c7d58aabSArnaldo Carvalho de Melo struct inet_diag_req *r = NLMSG_DATA(cb->nlh); 533c7d58aabSArnaldo Carvalho de Melo 53422e76c84SNelson Elhage if (nlmsg_attrlen(cb->nlh, sizeof(*r))) { 535c7d58aabSArnaldo Carvalho de Melo struct inet_diag_entry entry; 53622e76c84SNelson Elhage const struct nlattr *bc = nlmsg_find_attr(cb->nlh, 53722e76c84SNelson Elhage sizeof(*r), 53822e76c84SNelson Elhage INET_DIAG_REQ_BYTECODE); 539c7d58aabSArnaldo Carvalho de Melo 540c7d58aabSArnaldo Carvalho de Melo entry.family = tw->tw_family; 541c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 542c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 543c7d58aabSArnaldo Carvalho de Melo struct inet6_timewait_sock *tw6 = 544c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 545c7d58aabSArnaldo Carvalho de Melo entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32; 546c7d58aabSArnaldo Carvalho de Melo entry.daddr = tw6->tw_v6_daddr.s6_addr32; 547c7d58aabSArnaldo Carvalho de Melo } else 548c7d58aabSArnaldo Carvalho de Melo #endif 549c7d58aabSArnaldo Carvalho de Melo { 550c7d58aabSArnaldo Carvalho de Melo entry.saddr = &tw->tw_rcv_saddr; 551c7d58aabSArnaldo Carvalho de Melo entry.daddr = &tw->tw_daddr; 552c7d58aabSArnaldo Carvalho de Melo } 553c7d58aabSArnaldo Carvalho de Melo entry.sport = tw->tw_num; 554c7d58aabSArnaldo Carvalho de Melo entry.dport = ntohs(tw->tw_dport); 555c7d58aabSArnaldo Carvalho de Melo entry.userlocks = 0; 556c7d58aabSArnaldo Carvalho de Melo 55722e76c84SNelson Elhage if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) 558c7d58aabSArnaldo Carvalho de Melo return 0; 559c7d58aabSArnaldo Carvalho de Melo } 560c7d58aabSArnaldo Carvalho de Melo 561c7d58aabSArnaldo Carvalho de Melo return inet_twsk_diag_fill(tw, skb, r->idiag_ext, 562c7d58aabSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 563c7d58aabSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 564c7d58aabSArnaldo Carvalho de Melo } 565c7d58aabSArnaldo Carvalho de Melo 566a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, 5674e852c02SArnaldo Carvalho de Melo struct request_sock *req, u32 pid, u32 seq, 568a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 569a8c2190eSArnaldo Carvalho de Melo { 570a8c2190eSArnaldo Carvalho de Melo const struct inet_request_sock *ireq = inet_rsk(req); 571a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 57227a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 573a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 574a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 575a8c2190eSArnaldo Carvalho de Melo long tmo; 576a8c2190eSArnaldo Carvalho de Melo 577a8c2190eSArnaldo Carvalho de Melo nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 578a8c2190eSArnaldo Carvalho de Melo nlh->nlmsg_flags = NLM_F_MULTI; 579a8c2190eSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 580a8c2190eSArnaldo Carvalho de Melo 581a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 582a8c2190eSArnaldo Carvalho de Melo r->idiag_state = TCP_SYN_RECV; 583a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 584a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = req->retrans; 585a8c2190eSArnaldo Carvalho de Melo 586a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 587a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)req; 588a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); 589a8c2190eSArnaldo Carvalho de Melo 590a8c2190eSArnaldo Carvalho de Melo tmo = req->expires - jiffies; 591a8c2190eSArnaldo Carvalho de Melo if (tmo < 0) 592a8c2190eSArnaldo Carvalho de Melo tmo = 0; 593a8c2190eSArnaldo Carvalho de Melo 594c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 595a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport = ireq->rmt_port; 596a8c2190eSArnaldo Carvalho de Melo r->id.idiag_src[0] = ireq->loc_addr; 597a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dst[0] = ireq->rmt_addr; 598a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = jiffies_to_msecs(tmo); 599a8c2190eSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 600a8c2190eSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 601a8c2190eSArnaldo Carvalho de Melo r->idiag_uid = sock_i_uid(sk); 602a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = 0; 603a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 604a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 6054e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr; 6064e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr; 607a8c2190eSArnaldo Carvalho de Melo } 608a8c2190eSArnaldo Carvalho de Melo #endif 60927a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 610a8c2190eSArnaldo Carvalho de Melo 611a8c2190eSArnaldo Carvalho de Melo return skb->len; 612a8c2190eSArnaldo Carvalho de Melo 613a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 614dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 615a8c2190eSArnaldo Carvalho de Melo return -1; 616a8c2190eSArnaldo Carvalho de Melo } 617a8c2190eSArnaldo Carvalho de Melo 618a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 619a8c2190eSArnaldo Carvalho de Melo struct netlink_callback *cb) 620a8c2190eSArnaldo Carvalho de Melo { 621a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry entry; 622a8c2190eSArnaldo Carvalho de Melo struct inet_diag_req *r = NLMSG_DATA(cb->nlh); 623a8c2190eSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 624a8c2190eSArnaldo Carvalho de Melo struct listen_sock *lopt; 62522e76c84SNelson Elhage const struct nlattr *bc = NULL; 626a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 627a8c2190eSArnaldo Carvalho de Melo int j, s_j; 628a8c2190eSArnaldo Carvalho de Melo int reqnum, s_reqnum; 629a8c2190eSArnaldo Carvalho de Melo int err = 0; 630a8c2190eSArnaldo Carvalho de Melo 631a8c2190eSArnaldo Carvalho de Melo s_j = cb->args[3]; 632a8c2190eSArnaldo Carvalho de Melo s_reqnum = cb->args[4]; 633a8c2190eSArnaldo Carvalho de Melo 634a8c2190eSArnaldo Carvalho de Melo if (s_j > 0) 635a8c2190eSArnaldo Carvalho de Melo s_j--; 636a8c2190eSArnaldo Carvalho de Melo 637a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 638a8c2190eSArnaldo Carvalho de Melo 639a8c2190eSArnaldo Carvalho de Melo read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 640a8c2190eSArnaldo Carvalho de Melo 641a8c2190eSArnaldo Carvalho de Melo lopt = icsk->icsk_accept_queue.listen_opt; 642a8c2190eSArnaldo Carvalho de Melo if (!lopt || !lopt->qlen) 643a8c2190eSArnaldo Carvalho de Melo goto out; 644a8c2190eSArnaldo Carvalho de Melo 64522e76c84SNelson Elhage if (nlmsg_attrlen(cb->nlh, sizeof(*r))) { 64622e76c84SNelson Elhage bc = nlmsg_find_attr(cb->nlh, sizeof(*r), 64722e76c84SNelson Elhage INET_DIAG_REQ_BYTECODE); 648c720c7e8SEric Dumazet entry.sport = inet->inet_num; 649a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 650a8c2190eSArnaldo Carvalho de Melo } 651a8c2190eSArnaldo Carvalho de Melo 652a8c2190eSArnaldo Carvalho de Melo for (j = s_j; j < lopt->nr_table_entries; j++) { 653a8c2190eSArnaldo Carvalho de Melo struct request_sock *req, *head = lopt->syn_table[j]; 654a8c2190eSArnaldo Carvalho de Melo 655a8c2190eSArnaldo Carvalho de Melo reqnum = 0; 656a8c2190eSArnaldo Carvalho de Melo for (req = head; req; reqnum++, req = req->dl_next) { 657a8c2190eSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 658a8c2190eSArnaldo Carvalho de Melo 659a8c2190eSArnaldo Carvalho de Melo if (reqnum < s_reqnum) 660a8c2190eSArnaldo Carvalho de Melo continue; 661a8c2190eSArnaldo Carvalho de Melo if (r->id.idiag_dport != ireq->rmt_port && 662a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 663a8c2190eSArnaldo Carvalho de Melo continue; 664a8c2190eSArnaldo Carvalho de Melo 665a8c2190eSArnaldo Carvalho de Melo if (bc) { 666a8c2190eSArnaldo Carvalho de Melo entry.saddr = 667a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 668a8c2190eSArnaldo Carvalho de Melo (entry.family == AF_INET6) ? 669ca304b61SArnaldo Carvalho de Melo inet6_rsk(req)->loc_addr.s6_addr32 : 670a8c2190eSArnaldo Carvalho de Melo #endif 671a8c2190eSArnaldo Carvalho de Melo &ireq->loc_addr; 672a8c2190eSArnaldo Carvalho de Melo entry.daddr = 673a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 674a8c2190eSArnaldo Carvalho de Melo (entry.family == AF_INET6) ? 675ca304b61SArnaldo Carvalho de Melo inet6_rsk(req)->rmt_addr.s6_addr32 : 676a8c2190eSArnaldo Carvalho de Melo #endif 677a8c2190eSArnaldo Carvalho de Melo &ireq->rmt_addr; 678a8c2190eSArnaldo Carvalho de Melo entry.dport = ntohs(ireq->rmt_port); 679a8c2190eSArnaldo Carvalho de Melo 68022e76c84SNelson Elhage if (!inet_diag_bc_run(nla_data(bc), 68122e76c84SNelson Elhage nla_len(bc), &entry)) 682a8c2190eSArnaldo Carvalho de Melo continue; 683a8c2190eSArnaldo Carvalho de Melo } 684a8c2190eSArnaldo Carvalho de Melo 685a8c2190eSArnaldo Carvalho de Melo err = inet_diag_fill_req(skb, sk, req, 686a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 687a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, cb->nlh); 688a8c2190eSArnaldo Carvalho de Melo if (err < 0) { 689a8c2190eSArnaldo Carvalho de Melo cb->args[3] = j + 1; 690a8c2190eSArnaldo Carvalho de Melo cb->args[4] = reqnum; 691a8c2190eSArnaldo Carvalho de Melo goto out; 692a8c2190eSArnaldo Carvalho de Melo } 693a8c2190eSArnaldo Carvalho de Melo } 694a8c2190eSArnaldo Carvalho de Melo 695a8c2190eSArnaldo Carvalho de Melo s_reqnum = 0; 696a8c2190eSArnaldo Carvalho de Melo } 697a8c2190eSArnaldo Carvalho de Melo 698a8c2190eSArnaldo Carvalho de Melo out: 699a8c2190eSArnaldo Carvalho de Melo read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 700a8c2190eSArnaldo Carvalho de Melo 701a8c2190eSArnaldo Carvalho de Melo return err; 702a8c2190eSArnaldo Carvalho de Melo } 703a8c2190eSArnaldo Carvalho de Melo 704a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 705a8c2190eSArnaldo Carvalho de Melo { 706a8c2190eSArnaldo Carvalho de Melo int i, num; 707a8c2190eSArnaldo Carvalho de Melo int s_i, s_num; 708a8c2190eSArnaldo Carvalho de Melo struct inet_diag_req *r = NLMSG_DATA(cb->nlh); 709a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 710a8c2190eSArnaldo Carvalho de Melo struct inet_hashinfo *hashinfo; 711a8c2190eSArnaldo Carvalho de Melo 712d523a328SHerbert Xu handler = inet_diag_lock_handler(cb->nlh->nlmsg_type); 7138cf8e5a6SArnaldo Carvalho de Melo if (IS_ERR(handler)) 7148cf8e5a6SArnaldo Carvalho de Melo goto unlock; 715d523a328SHerbert Xu 716a8c2190eSArnaldo Carvalho de Melo hashinfo = handler->idiag_hashinfo; 717a8c2190eSArnaldo Carvalho de Melo 718a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 719a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 720a8c2190eSArnaldo Carvalho de Melo 721a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 722a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 723a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 724a8c2190eSArnaldo Carvalho de Melo 725a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 726a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 727c25eb3bfSEric Dumazet struct hlist_nulls_node *node; 7285caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 729a8c2190eSArnaldo Carvalho de Melo 730a8c2190eSArnaldo Carvalho de Melo num = 0; 7315caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 7325caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 733c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 734a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 735a8c2190eSArnaldo Carvalho de Melo 736a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 737a8c2190eSArnaldo Carvalho de Melo num++; 738a8c2190eSArnaldo Carvalho de Melo continue; 739a8c2190eSArnaldo Carvalho de Melo } 740a8c2190eSArnaldo Carvalho de Melo 741c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 742a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 743a8c2190eSArnaldo Carvalho de Melo goto next_listen; 744a8c2190eSArnaldo Carvalho de Melo 745a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_LISTEN) || 746a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport || 747a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 748a8c2190eSArnaldo Carvalho de Melo goto syn_recv; 749a8c2190eSArnaldo Carvalho de Melo 750dff2c035SArnaldo Carvalho de Melo if (inet_csk_diag_dump(sk, skb, cb) < 0) { 7515caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 752a8c2190eSArnaldo Carvalho de Melo goto done; 753a8c2190eSArnaldo Carvalho de Melo } 754a8c2190eSArnaldo Carvalho de Melo 755a8c2190eSArnaldo Carvalho de Melo syn_recv: 756a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_SYN_RECV)) 757a8c2190eSArnaldo Carvalho de Melo goto next_listen; 758a8c2190eSArnaldo Carvalho de Melo 759a8c2190eSArnaldo Carvalho de Melo if (inet_diag_dump_reqs(skb, sk, cb) < 0) { 7605caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 761a8c2190eSArnaldo Carvalho de Melo goto done; 762a8c2190eSArnaldo Carvalho de Melo } 763a8c2190eSArnaldo Carvalho de Melo 764a8c2190eSArnaldo Carvalho de Melo next_listen: 765a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 766a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 767a8c2190eSArnaldo Carvalho de Melo ++num; 768a8c2190eSArnaldo Carvalho de Melo } 7695caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 770a8c2190eSArnaldo Carvalho de Melo 771a8c2190eSArnaldo Carvalho de Melo s_num = 0; 772a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 773a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 774a8c2190eSArnaldo Carvalho de Melo } 775a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 776a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 777a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 778a8c2190eSArnaldo Carvalho de Melo } 779a8c2190eSArnaldo Carvalho de Melo 780a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 781d523a328SHerbert Xu goto unlock; 782a8c2190eSArnaldo Carvalho de Melo 783f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 784a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 7857e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 786a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 7873ab5aee7SEric Dumazet struct hlist_nulls_node *node; 788a8c2190eSArnaldo Carvalho de Melo 7896be547a6SAndi Kleen num = 0; 7906be547a6SAndi Kleen 7913ab5aee7SEric Dumazet if (hlist_nulls_empty(&head->chain) && 7923ab5aee7SEric Dumazet hlist_nulls_empty(&head->twchain)) 7936be547a6SAndi Kleen continue; 7946be547a6SAndi Kleen 795a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 796a8c2190eSArnaldo Carvalho de Melo s_num = 0; 797a8c2190eSArnaldo Carvalho de Melo 7987e3aab4aSDavid S. Miller spin_lock_bh(lock); 7993ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 800a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 801a8c2190eSArnaldo Carvalho de Melo 802a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 803a8c2190eSArnaldo Carvalho de Melo goto next_normal; 804a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (1 << sk->sk_state))) 805a8c2190eSArnaldo Carvalho de Melo goto next_normal; 806c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 807a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 808a8c2190eSArnaldo Carvalho de Melo goto next_normal; 809c720c7e8SEric Dumazet if (r->id.idiag_dport != inet->inet_dport && 8104e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 811a8c2190eSArnaldo Carvalho de Melo goto next_normal; 812dff2c035SArnaldo Carvalho de Melo if (inet_csk_diag_dump(sk, skb, cb) < 0) { 8137e3aab4aSDavid S. Miller spin_unlock_bh(lock); 814a8c2190eSArnaldo Carvalho de Melo goto done; 815a8c2190eSArnaldo Carvalho de Melo } 816a8c2190eSArnaldo Carvalho de Melo next_normal: 817a8c2190eSArnaldo Carvalho de Melo ++num; 818a8c2190eSArnaldo Carvalho de Melo } 819a8c2190eSArnaldo Carvalho de Melo 820a8c2190eSArnaldo Carvalho de Melo if (r->idiag_states & TCPF_TIME_WAIT) { 821c7d58aabSArnaldo Carvalho de Melo struct inet_timewait_sock *tw; 822c7d58aabSArnaldo Carvalho de Melo 823c7d58aabSArnaldo Carvalho de Melo inet_twsk_for_each(tw, node, 824dbca9b27SEric Dumazet &head->twchain) { 825a8c2190eSArnaldo Carvalho de Melo 826a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 827a8c2190eSArnaldo Carvalho de Melo goto next_dying; 8287dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_sport != tw->tw_sport && 829a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 830a8c2190eSArnaldo Carvalho de Melo goto next_dying; 8317dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_dport != tw->tw_dport && 832a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 833a8c2190eSArnaldo Carvalho de Melo goto next_dying; 834c7d58aabSArnaldo Carvalho de Melo if (inet_twsk_diag_dump(tw, skb, cb) < 0) { 8357e3aab4aSDavid S. Miller spin_unlock_bh(lock); 836a8c2190eSArnaldo Carvalho de Melo goto done; 837a8c2190eSArnaldo Carvalho de Melo } 838a8c2190eSArnaldo Carvalho de Melo next_dying: 839a8c2190eSArnaldo Carvalho de Melo ++num; 840a8c2190eSArnaldo Carvalho de Melo } 841a8c2190eSArnaldo Carvalho de Melo } 8427e3aab4aSDavid S. Miller spin_unlock_bh(lock); 843a8c2190eSArnaldo Carvalho de Melo } 844a8c2190eSArnaldo Carvalho de Melo 845a8c2190eSArnaldo Carvalho de Melo done: 846a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 847a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 848d523a328SHerbert Xu unlock: 849d523a328SHerbert Xu inet_diag_unlock_handler(handler); 850a8c2190eSArnaldo Carvalho de Melo return skb->len; 851a8c2190eSArnaldo Carvalho de Melo } 852a8c2190eSArnaldo Carvalho de Melo 853*8d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 854a8c2190eSArnaldo Carvalho de Melo { 855ead592baSThomas Graf int hdrlen = sizeof(struct inet_diag_req); 856a8c2190eSArnaldo Carvalho de Melo 857ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 858ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 859ead592baSThomas Graf return -EINVAL; 860a8c2190eSArnaldo Carvalho de Melo 861b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 862ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 863ead592baSThomas Graf struct nlattr *attr; 864ead592baSThomas Graf 865ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 866ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 867ead592baSThomas Graf if (attr == NULL || 868ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 869ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 870a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 871a8c2190eSArnaldo Carvalho de Melo } 872a8c2190eSArnaldo Carvalho de Melo 8737f1fb60cSPavel Emelyanov return netlink_dump_start(sdiagnl, skb, nlh, 874c7ac8679SGreg Rose inet_diag_dump, NULL, 0); 875a8c2190eSArnaldo Carvalho de Melo } 876ead592baSThomas Graf 877ead592baSThomas Graf return inet_diag_get_exact(skb, nlh); 878a8c2190eSArnaldo Carvalho de Melo } 879a8c2190eSArnaldo Carvalho de Melo 880*8d34172dSPavel Emelyanov static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 881*8d34172dSPavel Emelyanov { 882*8d34172dSPavel Emelyanov return -EOPNOTSUPP; 883*8d34172dSPavel Emelyanov } 884*8d34172dSPavel Emelyanov 8857f1fb60cSPavel Emelyanov static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 886a8c2190eSArnaldo Carvalho de Melo { 887*8d34172dSPavel Emelyanov switch (nlh->nlmsg_type) { 888*8d34172dSPavel Emelyanov case TCPDIAG_GETSOCK: 889*8d34172dSPavel Emelyanov case DCCPDIAG_GETSOCK: 890*8d34172dSPavel Emelyanov return inet_diag_rcv_msg_compat(skb, nlh); 891*8d34172dSPavel Emelyanov case SOCK_DIAG_BY_FAMILY: 892*8d34172dSPavel Emelyanov return __sock_diag_rcv_msg(skb, nlh); 893*8d34172dSPavel Emelyanov default: 894*8d34172dSPavel Emelyanov return -EINVAL; 895*8d34172dSPavel Emelyanov } 8967f1fb60cSPavel Emelyanov } 8977f1fb60cSPavel Emelyanov 8987f1fb60cSPavel Emelyanov static DEFINE_MUTEX(sock_diag_mutex); 8997f1fb60cSPavel Emelyanov 9007f1fb60cSPavel Emelyanov static void sock_diag_rcv(struct sk_buff *skb) 9017f1fb60cSPavel Emelyanov { 9027f1fb60cSPavel Emelyanov mutex_lock(&sock_diag_mutex); 9037f1fb60cSPavel Emelyanov netlink_rcv_skb(skb, &sock_diag_rcv_msg); 9047f1fb60cSPavel Emelyanov mutex_unlock(&sock_diag_mutex); 905a8c2190eSArnaldo Carvalho de Melo } 906a8c2190eSArnaldo Carvalho de Melo 907a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 908a8c2190eSArnaldo Carvalho de Melo { 909a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 910a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 911a8c2190eSArnaldo Carvalho de Melo 912a8c2190eSArnaldo Carvalho de Melo if (type >= INET_DIAG_GETSOCK_MAX) 913a8c2190eSArnaldo Carvalho de Melo goto out; 914a8c2190eSArnaldo Carvalho de Melo 915d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 916a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 917a8c2190eSArnaldo Carvalho de Melo if (inet_diag_table[type] == NULL) { 918a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 919a8c2190eSArnaldo Carvalho de Melo err = 0; 920a8c2190eSArnaldo Carvalho de Melo } 921d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 922a8c2190eSArnaldo Carvalho de Melo out: 923a8c2190eSArnaldo Carvalho de Melo return err; 924a8c2190eSArnaldo Carvalho de Melo } 925a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 926a8c2190eSArnaldo Carvalho de Melo 927a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 928a8c2190eSArnaldo Carvalho de Melo { 929a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 930a8c2190eSArnaldo Carvalho de Melo 931a8c2190eSArnaldo Carvalho de Melo if (type >= INET_DIAG_GETSOCK_MAX) 932a8c2190eSArnaldo Carvalho de Melo return; 933a8c2190eSArnaldo Carvalho de Melo 934d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 935a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 936d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 937a8c2190eSArnaldo Carvalho de Melo } 938a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 939a8c2190eSArnaldo Carvalho de Melo 940a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 941a8c2190eSArnaldo Carvalho de Melo { 942a8c2190eSArnaldo Carvalho de Melo const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX * 943a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 944a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 945a8c2190eSArnaldo Carvalho de Melo 9460da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 947a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 948a8c2190eSArnaldo Carvalho de Melo goto out; 949a8c2190eSArnaldo Carvalho de Melo 9507f1fb60cSPavel Emelyanov sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, 9517f1fb60cSPavel Emelyanov sock_diag_rcv, NULL, THIS_MODULE); 9527f1fb60cSPavel Emelyanov if (sdiagnl == NULL) 953a8c2190eSArnaldo Carvalho de Melo goto out_free_table; 95417b085eaSArnaldo Carvalho de Melo err = 0; 955a8c2190eSArnaldo Carvalho de Melo out: 956a8c2190eSArnaldo Carvalho de Melo return err; 957a8c2190eSArnaldo Carvalho de Melo out_free_table: 958a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 959a8c2190eSArnaldo Carvalho de Melo goto out; 960a8c2190eSArnaldo Carvalho de Melo } 961a8c2190eSArnaldo Carvalho de Melo 962a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 963a8c2190eSArnaldo Carvalho de Melo { 9647f1fb60cSPavel Emelyanov netlink_kernel_release(sdiagnl); 965a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 966a8c2190eSArnaldo Carvalho de Melo } 967a8c2190eSArnaldo Carvalho de Melo 968a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 969a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 970a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 9717f1fb60cSPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); 972