1a8c2190eSArnaldo Carvalho de Melo /* 2a8c2190eSArnaldo Carvalho de Melo * inet_diag.c Module for monitoring INET transport protocols sockets. 3a8c2190eSArnaldo Carvalho de Melo * 4a8c2190eSArnaldo Carvalho de Melo * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 5a8c2190eSArnaldo Carvalho de Melo * 6a8c2190eSArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 7a8c2190eSArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 8a8c2190eSArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 9a8c2190eSArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 10a8c2190eSArnaldo Carvalho de Melo */ 11a8c2190eSArnaldo Carvalho de Melo 12172589ccSIlpo Järvinen #include <linux/kernel.h> 13a8c2190eSArnaldo Carvalho de Melo #include <linux/module.h> 14a8c2190eSArnaldo Carvalho de Melo #include <linux/types.h> 15a8c2190eSArnaldo Carvalho de Melo #include <linux/fcntl.h> 16a8c2190eSArnaldo Carvalho de Melo #include <linux/random.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18a8c2190eSArnaldo Carvalho de Melo #include <linux/cache.h> 19a8c2190eSArnaldo Carvalho de Melo #include <linux/init.h> 20a8c2190eSArnaldo Carvalho de Melo #include <linux/time.h> 21a8c2190eSArnaldo Carvalho de Melo 22a8c2190eSArnaldo Carvalho de Melo #include <net/icmp.h> 23a8c2190eSArnaldo Carvalho de Melo #include <net/tcp.h> 24a8c2190eSArnaldo Carvalho de Melo #include <net/ipv6.h> 25a8c2190eSArnaldo Carvalho de Melo #include <net/inet_common.h> 26a8c2190eSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 27a8c2190eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 28a8c2190eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h> 29a8c2190eSArnaldo Carvalho de Melo #include <net/inet6_hashtables.h> 30dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 31a8c2190eSArnaldo Carvalho de Melo 32a8c2190eSArnaldo Carvalho de Melo #include <linux/inet.h> 33a8c2190eSArnaldo Carvalho de Melo #include <linux/stddef.h> 34a8c2190eSArnaldo Carvalho de Melo 35a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h> 36d366477aSPavel Emelyanov #include <linux/sock_diag.h> 37a8c2190eSArnaldo Carvalho de Melo 38a8c2190eSArnaldo Carvalho de Melo static const struct inet_diag_handler **inet_diag_table; 39a8c2190eSArnaldo Carvalho de Melo 40a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry { 419f855299SAl Viro __be32 *saddr; 429f855299SAl Viro __be32 *daddr; 43a8c2190eSArnaldo Carvalho de Melo u16 sport; 44a8c2190eSArnaldo Carvalho de Melo u16 dport; 45a8c2190eSArnaldo Carvalho de Melo u16 family; 46a8c2190eSArnaldo Carvalho de Melo u16 userlocks; 47a8c2190eSArnaldo Carvalho de Melo }; 48a8c2190eSArnaldo Carvalho de Melo 49a8c2190eSArnaldo Carvalho de Melo #define INET_DIAG_PUT(skb, attrtype, attrlen) \ 50a8c2190eSArnaldo Carvalho de Melo RTA_DATA(__RTA_PUT(skb, attrtype, attrlen)) 51a8c2190eSArnaldo Carvalho de Melo 52d523a328SHerbert Xu static DEFINE_MUTEX(inet_diag_table_mutex); 53d523a328SHerbert Xu 54f13c95f0SPavel Emelyanov static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 55d523a328SHerbert Xu { 56f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 57d523a328SHerbert Xu request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 58f13c95f0SPavel Emelyanov NETLINK_SOCK_DIAG, proto); 59d523a328SHerbert Xu 60d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 61f13c95f0SPavel Emelyanov if (!inet_diag_table[proto]) 62d523a328SHerbert Xu return ERR_PTR(-ENOENT); 63d523a328SHerbert Xu 64f13c95f0SPavel Emelyanov return inet_diag_table[proto]; 65d523a328SHerbert Xu } 66d523a328SHerbert Xu 67d523a328SHerbert Xu static inline void inet_diag_unlock_handler( 68d523a328SHerbert Xu const struct inet_diag_handler *handler) 69d523a328SHerbert Xu { 70d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 71d523a328SHerbert Xu } 72d523a328SHerbert Xu 73dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_fill(struct sock *sk, 74a029fe26SPavel Emelyanov struct sk_buff *skb, struct inet_diag_req *req, 75a029fe26SPavel Emelyanov u32 pid, u32 seq, u16 nlmsg_flags, 76a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 77a8c2190eSArnaldo Carvalho de Melo { 78a8c2190eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 79a8c2190eSArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 80a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 81a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 82a8c2190eSArnaldo Carvalho de Melo void *info = NULL; 83a8c2190eSArnaldo Carvalho de Melo struct inet_diag_meminfo *minfo = NULL; 8427a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 85a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 86a029fe26SPavel Emelyanov int ext = req->idiag_ext; 87a8c2190eSArnaldo Carvalho de Melo 88a029fe26SPavel Emelyanov handler = inet_diag_table[req->sdiag_protocol]; 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))) 1017b35eaddSPavel Emelyanov info = INET_DIAG_PUT(skb, INET_DIAG_INFO, sizeof(struct tcp_info)); 102a8c2190eSArnaldo Carvalho de Melo 103a8c2190eSArnaldo Carvalho de Melo if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) { 104c7d58aabSArnaldo Carvalho de Melo const size_t len = strlen(icsk->icsk_ca_ops->name); 105c7d58aabSArnaldo Carvalho de Melo 106a8c2190eSArnaldo Carvalho de Melo strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1), 107a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->name); 108a8c2190eSArnaldo Carvalho de Melo } 109c7d58aabSArnaldo Carvalho de Melo 110a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 111a8c2190eSArnaldo Carvalho de Melo r->idiag_state = sk->sk_state; 112a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 113a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = 0; 114a8c2190eSArnaldo Carvalho de Melo 115a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 116a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)sk; 117a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); 118a8c2190eSArnaldo Carvalho de Melo 119c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 120c720c7e8SEric Dumazet r->id.idiag_dport = inet->inet_dport; 121c720c7e8SEric Dumazet r->id.idiag_src[0] = inet->inet_rcv_saddr; 122c720c7e8SEric Dumazet r->id.idiag_dst[0] = inet->inet_daddr; 123a8c2190eSArnaldo Carvalho de Melo 124717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 125717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 126717b6d83SMaciej Żenczykowski */ 127717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 128717b6d83SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos); 129717b6d83SMaciej Żenczykowski 130a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 131a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 132b71d1d42SEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 133a8c2190eSArnaldo Carvalho de Melo 1344e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 1354e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = np->daddr; 13606236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 13706236ac3SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); 138a8c2190eSArnaldo Carvalho de Melo } 139a8c2190eSArnaldo Carvalho de Melo #endif 140a8c2190eSArnaldo Carvalho de Melo 141172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 142a8c2190eSArnaldo Carvalho de Melo 143a8c2190eSArnaldo Carvalho de Melo if (icsk->icsk_pending == ICSK_TIME_RETRANS) { 144a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 145a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 146a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 147a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 148a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 149a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 150a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 151a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 152a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 153a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 154a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 155a8c2190eSArnaldo Carvalho de Melo } else { 156a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 157a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 158a8c2190eSArnaldo Carvalho de Melo } 159a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS 160a8c2190eSArnaldo Carvalho de Melo 161a8c2190eSArnaldo Carvalho de Melo r->idiag_uid = sock_i_uid(sk); 162a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = sock_i_ino(sk); 163a8c2190eSArnaldo Carvalho de Melo 164a8c2190eSArnaldo Carvalho de Melo if (minfo) { 16531e6d363SEric Dumazet minfo->idiag_rmem = sk_rmem_alloc_get(sk); 166a8c2190eSArnaldo Carvalho de Melo minfo->idiag_wmem = sk->sk_wmem_queued; 167a8c2190eSArnaldo Carvalho de Melo minfo->idiag_fmem = sk->sk_forward_alloc; 16831e6d363SEric Dumazet minfo->idiag_tmem = sk_wmem_alloc_get(sk); 169a8c2190eSArnaldo Carvalho de Melo } 170a8c2190eSArnaldo Carvalho de Melo 171a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 172a8c2190eSArnaldo Carvalho de Melo 173a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state < TCP_TIME_WAIT && 174a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 175a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->get_info(sk, ext, skb); 176a8c2190eSArnaldo Carvalho de Melo 17727a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 178a8c2190eSArnaldo Carvalho de Melo return skb->len; 179a8c2190eSArnaldo Carvalho de Melo 180a8c2190eSArnaldo Carvalho de Melo rtattr_failure: 181a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 182dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 18326932566SPatrick McHardy return -EMSGSIZE; 184a8c2190eSArnaldo Carvalho de Melo } 185a8c2190eSArnaldo Carvalho de Melo 186c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 187a029fe26SPavel Emelyanov struct sk_buff *skb, struct inet_diag_req *req, 188a029fe26SPavel Emelyanov u32 pid, u32 seq, u16 nlmsg_flags, 189c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 190c7d58aabSArnaldo Carvalho de Melo { 191c7d58aabSArnaldo Carvalho de Melo long tmo; 192c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 19327a884dcSArnaldo Carvalho de Melo const unsigned char *previous_tail = skb_tail_pointer(skb); 194c7d58aabSArnaldo Carvalho de Melo struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, 195c7d58aabSArnaldo Carvalho de Melo unlh->nlmsg_type, sizeof(*r)); 196c7d58aabSArnaldo Carvalho de Melo 197c7d58aabSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 198c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 199c7d58aabSArnaldo Carvalho de Melo 200c7d58aabSArnaldo Carvalho de Melo nlh->nlmsg_flags = nlmsg_flags; 201c7d58aabSArnaldo Carvalho de Melo 202c7d58aabSArnaldo Carvalho de Melo tmo = tw->tw_ttd - jiffies; 203c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 204c7d58aabSArnaldo Carvalho de Melo tmo = 0; 205c7d58aabSArnaldo Carvalho de Melo 206c7d58aabSArnaldo Carvalho de Melo r->idiag_family = tw->tw_family; 207c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 208c7d58aabSArnaldo Carvalho de Melo r->id.idiag_if = tw->tw_bound_dev_if; 209c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)tw; 210c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1); 211c7d58aabSArnaldo Carvalho de Melo r->id.idiag_sport = tw->tw_sport; 212c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dport = tw->tw_dport; 213c7d58aabSArnaldo Carvalho de Melo r->id.idiag_src[0] = tw->tw_rcv_saddr; 214c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dst[0] = tw->tw_daddr; 215c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 216c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 217172589ccSIlpo Järvinen r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 218c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 219c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 220c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 221c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 222c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 223c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 224c7d58aabSArnaldo Carvalho de Melo const struct inet6_timewait_sock *tw6 = 225c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 226c7d58aabSArnaldo Carvalho de Melo 2274e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr; 2284e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr; 229c7d58aabSArnaldo Carvalho de Melo } 230c7d58aabSArnaldo Carvalho de Melo #endif 23127a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; 232c7d58aabSArnaldo Carvalho de Melo return skb->len; 233c7d58aabSArnaldo Carvalho de Melo nlmsg_failure: 234dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, previous_tail); 23526932566SPatrick McHardy return -EMSGSIZE; 236c7d58aabSArnaldo Carvalho de Melo } 237c7d58aabSArnaldo Carvalho de Melo 238dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 239a029fe26SPavel Emelyanov struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags, 240dff2c035SArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 241dff2c035SArnaldo Carvalho de Melo { 242dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 243dff2c035SArnaldo Carvalho de Melo return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 244a029fe26SPavel Emelyanov skb, r, pid, seq, nlmsg_flags, 245dff2c035SArnaldo Carvalho de Melo unlh); 246a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh); 247dff2c035SArnaldo Carvalho de Melo } 248dff2c035SArnaldo Carvalho de Melo 2494e852c02SArnaldo Carvalho de Melo static int inet_diag_get_exact(struct sk_buff *in_skb, 250fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh, 251fe50ce28SPavel Emelyanov struct inet_diag_req *req) 252a8c2190eSArnaldo Carvalho de Melo { 253a8c2190eSArnaldo Carvalho de Melo int err; 254a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 255a8c2190eSArnaldo Carvalho de Melo struct sk_buff *rep; 256a8c2190eSArnaldo Carvalho de Melo struct inet_hashinfo *hashinfo; 257a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 258a8c2190eSArnaldo Carvalho de Melo 259fe50ce28SPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 2608cf8e5a6SArnaldo Carvalho de Melo if (IS_ERR(handler)) { 2618cf8e5a6SArnaldo Carvalho de Melo err = PTR_ERR(handler); 2628cf8e5a6SArnaldo Carvalho de Melo goto unlock; 2638cf8e5a6SArnaldo Carvalho de Melo } 264d523a328SHerbert Xu 265a8c2190eSArnaldo Carvalho de Melo hashinfo = handler->idiag_hashinfo; 266d523a328SHerbert Xu err = -EINVAL; 267a8c2190eSArnaldo Carvalho de Melo 268fe50ce28SPavel Emelyanov if (req->sdiag_family == AF_INET) { 269c67499c0SPavel Emelyanov sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], 270a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 271a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 272a8c2190eSArnaldo Carvalho de Melo } 273a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 274fe50ce28SPavel Emelyanov else if (req->sdiag_family == AF_INET6) { 275d86e0dacSPavel Emelyanov sk = inet6_lookup(&init_net, hashinfo, 276a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 277a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 278a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 279a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 280a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 281a8c2190eSArnaldo Carvalho de Melo } 282a8c2190eSArnaldo Carvalho de Melo #endif 283a8c2190eSArnaldo Carvalho de Melo else { 284d523a328SHerbert Xu goto unlock; 285a8c2190eSArnaldo Carvalho de Melo } 286a8c2190eSArnaldo Carvalho de Melo 287d523a328SHerbert Xu err = -ENOENT; 288a8c2190eSArnaldo Carvalho de Melo if (sk == NULL) 289d523a328SHerbert Xu goto unlock; 290a8c2190eSArnaldo Carvalho de Melo 291a8c2190eSArnaldo Carvalho de Melo err = -ESTALE; 292a8c2190eSArnaldo Carvalho de Melo if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE || 293a8c2190eSArnaldo Carvalho de Melo req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) && 294a8c2190eSArnaldo Carvalho de Melo ((u32)(unsigned long)sk != req->id.idiag_cookie[0] || 295a8c2190eSArnaldo Carvalho de Melo (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1])) 296a8c2190eSArnaldo Carvalho de Melo goto out; 297a8c2190eSArnaldo Carvalho de Melo 298a8c2190eSArnaldo Carvalho de Melo err = -ENOMEM; 299a8c2190eSArnaldo Carvalho de Melo rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + 300a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_meminfo) + 3017b35eaddSPavel Emelyanov sizeof(struct tcp_info) + 64)), 302a8c2190eSArnaldo Carvalho de Melo GFP_KERNEL); 303a8c2190eSArnaldo Carvalho de Melo if (!rep) 304a8c2190eSArnaldo Carvalho de Melo goto out; 305a8c2190eSArnaldo Carvalho de Melo 306a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 307a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(in_skb).pid, 30826932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 30926932566SPatrick McHardy if (err < 0) { 31026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 31126932566SPatrick McHardy kfree_skb(rep); 31226932566SPatrick McHardy goto out; 31326932566SPatrick McHardy } 3148ef874bfSPavel Emelyanov err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid, 315a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 316a8c2190eSArnaldo Carvalho de Melo if (err > 0) 317a8c2190eSArnaldo Carvalho de Melo err = 0; 318a8c2190eSArnaldo Carvalho de Melo 319a8c2190eSArnaldo Carvalho de Melo out: 320a8c2190eSArnaldo Carvalho de Melo if (sk) { 321a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 322a8c2190eSArnaldo Carvalho de Melo inet_twsk_put((struct inet_timewait_sock *)sk); 323a8c2190eSArnaldo Carvalho de Melo else 324a8c2190eSArnaldo Carvalho de Melo sock_put(sk); 325a8c2190eSArnaldo Carvalho de Melo } 326d523a328SHerbert Xu unlock: 327d523a328SHerbert Xu inet_diag_unlock_handler(handler); 328a8c2190eSArnaldo Carvalho de Melo return err; 329a8c2190eSArnaldo Carvalho de Melo } 330a8c2190eSArnaldo Carvalho de Melo 3319f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 332a8c2190eSArnaldo Carvalho de Melo { 333a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 334a8c2190eSArnaldo Carvalho de Melo 335a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 336a8c2190eSArnaldo Carvalho de Melo 337a8c2190eSArnaldo Carvalho de Melo if (words) { 338a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 339a8c2190eSArnaldo Carvalho de Melo return 0; 340a8c2190eSArnaldo Carvalho de Melo } 341a8c2190eSArnaldo Carvalho de Melo if (bits) { 3429f855299SAl Viro __be32 w1, w2; 3439f855299SAl Viro __be32 mask; 344a8c2190eSArnaldo Carvalho de Melo 345a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 346a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 347a8c2190eSArnaldo Carvalho de Melo 348a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 349a8c2190eSArnaldo Carvalho de Melo 350a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 351a8c2190eSArnaldo Carvalho de Melo return 0; 352a8c2190eSArnaldo Carvalho de Melo } 353a8c2190eSArnaldo Carvalho de Melo 354a8c2190eSArnaldo Carvalho de Melo return 1; 355a8c2190eSArnaldo Carvalho de Melo } 356a8c2190eSArnaldo Carvalho de Melo 357a8c2190eSArnaldo Carvalho de Melo 358*87c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 359a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 360a8c2190eSArnaldo Carvalho de Melo { 361*87c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 362*87c22ea5SPavel Emelyanov int len = nla_len(_bc); 363*87c22ea5SPavel Emelyanov 364a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 365a8c2190eSArnaldo Carvalho de Melo int yes = 1; 366a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 367a8c2190eSArnaldo Carvalho de Melo 368a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 369a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 370a8c2190eSArnaldo Carvalho de Melo break; 371a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 372a8c2190eSArnaldo Carvalho de Melo yes = 0; 373a8c2190eSArnaldo Carvalho de Melo break; 374a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 375a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 376a8c2190eSArnaldo Carvalho de Melo break; 377a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 378b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 379a8c2190eSArnaldo Carvalho de Melo break; 380a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 381a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 382a8c2190eSArnaldo Carvalho de Melo break; 383a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 384a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 385a8c2190eSArnaldo Carvalho de Melo break; 386a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 387a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 388a8c2190eSArnaldo Carvalho de Melo break; 389a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 390a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 391a8c2190eSArnaldo Carvalho de Melo struct inet_diag_hostcond *cond; 3929f855299SAl Viro __be32 *addr; 393a8c2190eSArnaldo Carvalho de Melo 394a8c2190eSArnaldo Carvalho de Melo cond = (struct inet_diag_hostcond *)(op + 1); 395a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 396a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 397a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 398a8c2190eSArnaldo Carvalho de Melo yes = 0; 399a8c2190eSArnaldo Carvalho de Melo break; 400a8c2190eSArnaldo Carvalho de Melo } 401a8c2190eSArnaldo Carvalho de Melo 402a8c2190eSArnaldo Carvalho de Melo if (cond->prefix_len == 0) 403a8c2190eSArnaldo Carvalho de Melo break; 404a8c2190eSArnaldo Carvalho de Melo 405a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 406a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 407a8c2190eSArnaldo Carvalho de Melo else 408a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 409a8c2190eSArnaldo Carvalho de Melo 4104e852c02SArnaldo Carvalho de Melo if (bitstring_match(addr, cond->addr, 4114e852c02SArnaldo Carvalho de Melo cond->prefix_len)) 412a8c2190eSArnaldo Carvalho de Melo break; 413a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 414a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 415a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 416a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 417a8c2190eSArnaldo Carvalho de Melo bitstring_match(addr + 3, cond->addr, 418a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 419a8c2190eSArnaldo Carvalho de Melo break; 420a8c2190eSArnaldo Carvalho de Melo } 421a8c2190eSArnaldo Carvalho de Melo yes = 0; 422a8c2190eSArnaldo Carvalho de Melo break; 423a8c2190eSArnaldo Carvalho de Melo } 424a8c2190eSArnaldo Carvalho de Melo } 425a8c2190eSArnaldo Carvalho de Melo 426a8c2190eSArnaldo Carvalho de Melo if (yes) { 427a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 428a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 429a8c2190eSArnaldo Carvalho de Melo } else { 430a8c2190eSArnaldo Carvalho de Melo len -= op->no; 431a8c2190eSArnaldo Carvalho de Melo bc += op->no; 432a8c2190eSArnaldo Carvalho de Melo } 433a8c2190eSArnaldo Carvalho de Melo } 434a02cec21SEric Dumazet return len == 0; 435a8c2190eSArnaldo Carvalho de Melo } 436a8c2190eSArnaldo Carvalho de Melo 437a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 438a8c2190eSArnaldo Carvalho de Melo { 439a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 440a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 441a8c2190eSArnaldo Carvalho de Melo 442a8c2190eSArnaldo Carvalho de Melo if (cc > len) 443a8c2190eSArnaldo Carvalho de Melo return 0; 444a8c2190eSArnaldo Carvalho de Melo if (cc == len) 445a8c2190eSArnaldo Carvalho de Melo return 1; 446eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 447a8c2190eSArnaldo Carvalho de Melo return 0; 448a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 449a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 450a8c2190eSArnaldo Carvalho de Melo } 451a8c2190eSArnaldo Carvalho de Melo return 0; 452a8c2190eSArnaldo Carvalho de Melo } 453a8c2190eSArnaldo Carvalho de Melo 454a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 455a8c2190eSArnaldo Carvalho de Melo { 456eeb14972SEric Dumazet const void *bc = bytecode; 457a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 458a8c2190eSArnaldo Carvalho de Melo 459a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 460eeb14972SEric Dumazet const struct inet_diag_bc_op *op = bc; 461a8c2190eSArnaldo Carvalho de Melo 462a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); 463a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 464a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 465a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 466a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 467a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 468a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 469a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 470a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 471a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 472eeb14972SEric Dumazet if (op->no < 4 || op->no > len + 4 || op->no & 3) 473a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 474a8c2190eSArnaldo Carvalho de Melo if (op->no < len && 475a8c2190eSArnaldo Carvalho de Melo !valid_cc(bytecode, bytecode_len, len - op->no)) 476a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 477a8c2190eSArnaldo Carvalho de Melo break; 478a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 479a8c2190eSArnaldo Carvalho de Melo break; 480a8c2190eSArnaldo Carvalho de Melo default: 481a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 482a8c2190eSArnaldo Carvalho de Melo } 483eeb14972SEric Dumazet if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) 484eeb14972SEric Dumazet return -EINVAL; 485a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 486a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 487a8c2190eSArnaldo Carvalho de Melo } 488a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 489a8c2190eSArnaldo Carvalho de Melo } 490a8c2190eSArnaldo Carvalho de Melo 491dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 492dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 49337f352b5SPavel Emelyanov struct netlink_callback *cb, 49425c4cd2bSPavel Emelyanov struct inet_diag_req *r, 49537f352b5SPavel Emelyanov const struct nlattr *bc) 496a8c2190eSArnaldo Carvalho de Melo { 49737f352b5SPavel Emelyanov if (bc != NULL) { 498a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry entry; 499a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 500a8c2190eSArnaldo Carvalho de Melo 501a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 502a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 503a8c2190eSArnaldo Carvalho de Melo if (entry.family == AF_INET6) { 504a8c2190eSArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 505a8c2190eSArnaldo Carvalho de Melo 506a8c2190eSArnaldo Carvalho de Melo entry.saddr = np->rcv_saddr.s6_addr32; 507a8c2190eSArnaldo Carvalho de Melo entry.daddr = np->daddr.s6_addr32; 508a8c2190eSArnaldo Carvalho de Melo } else 509a8c2190eSArnaldo Carvalho de Melo #endif 510a8c2190eSArnaldo Carvalho de Melo { 511c720c7e8SEric Dumazet entry.saddr = &inet->inet_rcv_saddr; 512c720c7e8SEric Dumazet entry.daddr = &inet->inet_daddr; 513a8c2190eSArnaldo Carvalho de Melo } 514c720c7e8SEric Dumazet entry.sport = inet->inet_num; 515c720c7e8SEric Dumazet entry.dport = ntohs(inet->inet_dport); 516a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 517a8c2190eSArnaldo Carvalho de Melo 518*87c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 519a8c2190eSArnaldo Carvalho de Melo return 0; 520a8c2190eSArnaldo Carvalho de Melo } 521a8c2190eSArnaldo Carvalho de Melo 522a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 523dff2c035SArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 524a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 525a8c2190eSArnaldo Carvalho de Melo } 526a8c2190eSArnaldo Carvalho de Melo 527c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 528c7d58aabSArnaldo Carvalho de Melo struct sk_buff *skb, 52937f352b5SPavel Emelyanov struct netlink_callback *cb, 53025c4cd2bSPavel Emelyanov struct inet_diag_req *r, 53137f352b5SPavel Emelyanov const struct nlattr *bc) 532c7d58aabSArnaldo Carvalho de Melo { 53337f352b5SPavel Emelyanov if (bc != NULL) { 534c7d58aabSArnaldo Carvalho de Melo struct inet_diag_entry entry; 535c7d58aabSArnaldo Carvalho de Melo 536c7d58aabSArnaldo Carvalho de Melo entry.family = tw->tw_family; 537c7d58aabSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 538c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 539c7d58aabSArnaldo Carvalho de Melo struct inet6_timewait_sock *tw6 = 540c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 541c7d58aabSArnaldo Carvalho de Melo entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32; 542c7d58aabSArnaldo Carvalho de Melo entry.daddr = tw6->tw_v6_daddr.s6_addr32; 543c7d58aabSArnaldo Carvalho de Melo } else 544c7d58aabSArnaldo Carvalho de Melo #endif 545c7d58aabSArnaldo Carvalho de Melo { 546c7d58aabSArnaldo Carvalho de Melo entry.saddr = &tw->tw_rcv_saddr; 547c7d58aabSArnaldo Carvalho de Melo entry.daddr = &tw->tw_daddr; 548c7d58aabSArnaldo Carvalho de Melo } 549c7d58aabSArnaldo Carvalho de Melo entry.sport = tw->tw_num; 550c7d58aabSArnaldo Carvalho de Melo entry.dport = ntohs(tw->tw_dport); 551c7d58aabSArnaldo Carvalho de Melo entry.userlocks = 0; 552c7d58aabSArnaldo Carvalho de Melo 553*87c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 554c7d58aabSArnaldo Carvalho de Melo return 0; 555c7d58aabSArnaldo Carvalho de Melo } 556c7d58aabSArnaldo Carvalho de Melo 557a029fe26SPavel Emelyanov return inet_twsk_diag_fill(tw, skb, r, 558c7d58aabSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 559c7d58aabSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 560c7d58aabSArnaldo Carvalho de Melo } 561c7d58aabSArnaldo Carvalho de Melo 562a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, 5634e852c02SArnaldo Carvalho de Melo struct request_sock *req, u32 pid, u32 seq, 564a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 565a8c2190eSArnaldo Carvalho de Melo { 566a8c2190eSArnaldo Carvalho de Melo const struct inet_request_sock *ireq = inet_rsk(req); 567a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 56827a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 569a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 570a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 571a8c2190eSArnaldo Carvalho de Melo long tmo; 572a8c2190eSArnaldo Carvalho de Melo 573a8c2190eSArnaldo Carvalho de Melo nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 574a8c2190eSArnaldo Carvalho de Melo nlh->nlmsg_flags = NLM_F_MULTI; 575a8c2190eSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 576a8c2190eSArnaldo Carvalho de Melo 577a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 578a8c2190eSArnaldo Carvalho de Melo r->idiag_state = TCP_SYN_RECV; 579a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 580a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = req->retrans; 581a8c2190eSArnaldo Carvalho de Melo 582a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 583a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)req; 584a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); 585a8c2190eSArnaldo Carvalho de Melo 586a8c2190eSArnaldo Carvalho de Melo tmo = req->expires - jiffies; 587a8c2190eSArnaldo Carvalho de Melo if (tmo < 0) 588a8c2190eSArnaldo Carvalho de Melo tmo = 0; 589a8c2190eSArnaldo Carvalho de Melo 590c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 591a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport = ireq->rmt_port; 592a8c2190eSArnaldo Carvalho de Melo r->id.idiag_src[0] = ireq->loc_addr; 593a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dst[0] = ireq->rmt_addr; 594a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = jiffies_to_msecs(tmo); 595a8c2190eSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 596a8c2190eSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 597a8c2190eSArnaldo Carvalho de Melo r->idiag_uid = sock_i_uid(sk); 598a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = 0; 599a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 600a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 6014e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr; 6024e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr; 603a8c2190eSArnaldo Carvalho de Melo } 604a8c2190eSArnaldo Carvalho de Melo #endif 60527a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 606a8c2190eSArnaldo Carvalho de Melo 607a8c2190eSArnaldo Carvalho de Melo return skb->len; 608a8c2190eSArnaldo Carvalho de Melo 609a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 610dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 611a8c2190eSArnaldo Carvalho de Melo return -1; 612a8c2190eSArnaldo Carvalho de Melo } 613a8c2190eSArnaldo Carvalho de Melo 614a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 61537f352b5SPavel Emelyanov struct netlink_callback *cb, 61625c4cd2bSPavel Emelyanov struct inet_diag_req *r, 61737f352b5SPavel Emelyanov const struct nlattr *bc) 618a8c2190eSArnaldo Carvalho de Melo { 619a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry entry; 620a8c2190eSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 621a8c2190eSArnaldo Carvalho de Melo struct listen_sock *lopt; 622a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 623a8c2190eSArnaldo Carvalho de Melo int j, s_j; 624a8c2190eSArnaldo Carvalho de Melo int reqnum, s_reqnum; 625a8c2190eSArnaldo Carvalho de Melo int err = 0; 626a8c2190eSArnaldo Carvalho de Melo 627a8c2190eSArnaldo Carvalho de Melo s_j = cb->args[3]; 628a8c2190eSArnaldo Carvalho de Melo s_reqnum = cb->args[4]; 629a8c2190eSArnaldo Carvalho de Melo 630a8c2190eSArnaldo Carvalho de Melo if (s_j > 0) 631a8c2190eSArnaldo Carvalho de Melo s_j--; 632a8c2190eSArnaldo Carvalho de Melo 633a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 634a8c2190eSArnaldo Carvalho de Melo 635a8c2190eSArnaldo Carvalho de Melo read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 636a8c2190eSArnaldo Carvalho de Melo 637a8c2190eSArnaldo Carvalho de Melo lopt = icsk->icsk_accept_queue.listen_opt; 638a8c2190eSArnaldo Carvalho de Melo if (!lopt || !lopt->qlen) 639a8c2190eSArnaldo Carvalho de Melo goto out; 640a8c2190eSArnaldo Carvalho de Melo 64137f352b5SPavel Emelyanov if (bc != NULL) { 642c720c7e8SEric Dumazet entry.sport = inet->inet_num; 643a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 644a8c2190eSArnaldo Carvalho de Melo } 645a8c2190eSArnaldo Carvalho de Melo 646a8c2190eSArnaldo Carvalho de Melo for (j = s_j; j < lopt->nr_table_entries; j++) { 647a8c2190eSArnaldo Carvalho de Melo struct request_sock *req, *head = lopt->syn_table[j]; 648a8c2190eSArnaldo Carvalho de Melo 649a8c2190eSArnaldo Carvalho de Melo reqnum = 0; 650a8c2190eSArnaldo Carvalho de Melo for (req = head; req; reqnum++, req = req->dl_next) { 651a8c2190eSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 652a8c2190eSArnaldo Carvalho de Melo 653a8c2190eSArnaldo Carvalho de Melo if (reqnum < s_reqnum) 654a8c2190eSArnaldo Carvalho de Melo continue; 655a8c2190eSArnaldo Carvalho de Melo if (r->id.idiag_dport != ireq->rmt_port && 656a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 657a8c2190eSArnaldo Carvalho de Melo continue; 658a8c2190eSArnaldo Carvalho de Melo 659a8c2190eSArnaldo Carvalho de Melo if (bc) { 660a8c2190eSArnaldo Carvalho de Melo entry.saddr = 661a8c2190eSArnaldo Carvalho de Melo #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 662a8c2190eSArnaldo Carvalho de Melo (entry.family == AF_INET6) ? 663ca304b61SArnaldo Carvalho de Melo inet6_rsk(req)->loc_addr.s6_addr32 : 664a8c2190eSArnaldo Carvalho de Melo #endif 665a8c2190eSArnaldo Carvalho de Melo &ireq->loc_addr; 666a8c2190eSArnaldo Carvalho de Melo entry.daddr = 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)->rmt_addr.s6_addr32 : 670a8c2190eSArnaldo Carvalho de Melo #endif 671a8c2190eSArnaldo Carvalho de Melo &ireq->rmt_addr; 672a8c2190eSArnaldo Carvalho de Melo entry.dport = ntohs(ireq->rmt_port); 673a8c2190eSArnaldo Carvalho de Melo 674*87c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 675a8c2190eSArnaldo Carvalho de Melo continue; 676a8c2190eSArnaldo Carvalho de Melo } 677a8c2190eSArnaldo Carvalho de Melo 678a8c2190eSArnaldo Carvalho de Melo err = inet_diag_fill_req(skb, sk, req, 679a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 680a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, cb->nlh); 681a8c2190eSArnaldo Carvalho de Melo if (err < 0) { 682a8c2190eSArnaldo Carvalho de Melo cb->args[3] = j + 1; 683a8c2190eSArnaldo Carvalho de Melo cb->args[4] = reqnum; 684a8c2190eSArnaldo Carvalho de Melo goto out; 685a8c2190eSArnaldo Carvalho de Melo } 686a8c2190eSArnaldo Carvalho de Melo } 687a8c2190eSArnaldo Carvalho de Melo 688a8c2190eSArnaldo Carvalho de Melo s_reqnum = 0; 689a8c2190eSArnaldo Carvalho de Melo } 690a8c2190eSArnaldo Carvalho de Melo 691a8c2190eSArnaldo Carvalho de Melo out: 692a8c2190eSArnaldo Carvalho de Melo read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 693a8c2190eSArnaldo Carvalho de Melo 694a8c2190eSArnaldo Carvalho de Melo return err; 695a8c2190eSArnaldo Carvalho de Melo } 696a8c2190eSArnaldo Carvalho de Melo 69725c4cd2bSPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 69825c4cd2bSPavel Emelyanov struct inet_diag_req *r, struct nlattr *bc) 699a8c2190eSArnaldo Carvalho de Melo { 700a8c2190eSArnaldo Carvalho de Melo int i, num; 701a8c2190eSArnaldo Carvalho de Melo int s_i, s_num; 702a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 703a8c2190eSArnaldo Carvalho de Melo struct inet_hashinfo *hashinfo; 70437f352b5SPavel Emelyanov 70525c4cd2bSPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 7068cf8e5a6SArnaldo Carvalho de Melo if (IS_ERR(handler)) 7078cf8e5a6SArnaldo Carvalho de Melo goto unlock; 708d523a328SHerbert Xu 709a8c2190eSArnaldo Carvalho de Melo hashinfo = handler->idiag_hashinfo; 710a8c2190eSArnaldo Carvalho de Melo 711a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 712a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 713a8c2190eSArnaldo Carvalho de Melo 714a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 715a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 716a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 717a8c2190eSArnaldo Carvalho de Melo 718a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 719a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 720c25eb3bfSEric Dumazet struct hlist_nulls_node *node; 7215caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 722a8c2190eSArnaldo Carvalho de Melo 723a8c2190eSArnaldo Carvalho de Melo num = 0; 7245caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 7255caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 726c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 727a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 728a8c2190eSArnaldo Carvalho de Melo 729a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 730a8c2190eSArnaldo Carvalho de Melo num++; 731a8c2190eSArnaldo Carvalho de Melo continue; 732a8c2190eSArnaldo Carvalho de Melo } 733a8c2190eSArnaldo Carvalho de Melo 734d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 735d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 736d23deaa0SPavel Emelyanov goto next_listen; 737d23deaa0SPavel Emelyanov 738c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 739a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 740a8c2190eSArnaldo Carvalho de Melo goto next_listen; 741a8c2190eSArnaldo Carvalho de Melo 742a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_LISTEN) || 743a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport || 744a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 745a8c2190eSArnaldo Carvalho de Melo goto syn_recv; 746a8c2190eSArnaldo Carvalho de Melo 74725c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 7485caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 749a8c2190eSArnaldo Carvalho de Melo goto done; 750a8c2190eSArnaldo Carvalho de Melo } 751a8c2190eSArnaldo Carvalho de Melo 752a8c2190eSArnaldo Carvalho de Melo syn_recv: 753a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_SYN_RECV)) 754a8c2190eSArnaldo Carvalho de Melo goto next_listen; 755a8c2190eSArnaldo Carvalho de Melo 75625c4cd2bSPavel Emelyanov if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { 7575caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 758a8c2190eSArnaldo Carvalho de Melo goto done; 759a8c2190eSArnaldo Carvalho de Melo } 760a8c2190eSArnaldo Carvalho de Melo 761a8c2190eSArnaldo Carvalho de Melo next_listen: 762a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 763a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 764a8c2190eSArnaldo Carvalho de Melo ++num; 765a8c2190eSArnaldo Carvalho de Melo } 7665caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 767a8c2190eSArnaldo Carvalho de Melo 768a8c2190eSArnaldo Carvalho de Melo s_num = 0; 769a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 770a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 771a8c2190eSArnaldo Carvalho de Melo } 772a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 773a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 774a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 775a8c2190eSArnaldo Carvalho de Melo } 776a8c2190eSArnaldo Carvalho de Melo 777a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 778d523a328SHerbert Xu goto unlock; 779a8c2190eSArnaldo Carvalho de Melo 780f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 781a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 7827e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 783a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 7843ab5aee7SEric Dumazet struct hlist_nulls_node *node; 785a8c2190eSArnaldo Carvalho de Melo 7866be547a6SAndi Kleen num = 0; 7876be547a6SAndi Kleen 7883ab5aee7SEric Dumazet if (hlist_nulls_empty(&head->chain) && 7893ab5aee7SEric Dumazet hlist_nulls_empty(&head->twchain)) 7906be547a6SAndi Kleen continue; 7916be547a6SAndi Kleen 792a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 793a8c2190eSArnaldo Carvalho de Melo s_num = 0; 794a8c2190eSArnaldo Carvalho de Melo 7957e3aab4aSDavid S. Miller spin_lock_bh(lock); 7963ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 797a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 798a8c2190eSArnaldo Carvalho de Melo 799a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 800a8c2190eSArnaldo Carvalho de Melo goto next_normal; 801a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (1 << sk->sk_state))) 802a8c2190eSArnaldo Carvalho de Melo goto next_normal; 803d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 804d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 805d23deaa0SPavel Emelyanov 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; 81225c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 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; 828d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 829d23deaa0SPavel Emelyanov tw->tw_family != r->sdiag_family) 830d23deaa0SPavel Emelyanov goto next_dying; 8317dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_sport != tw->tw_sport && 832a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 833a8c2190eSArnaldo Carvalho de Melo goto next_dying; 8347dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_dport != tw->tw_dport && 835a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 836a8c2190eSArnaldo Carvalho de Melo goto next_dying; 83725c4cd2bSPavel Emelyanov if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) { 8387e3aab4aSDavid S. Miller spin_unlock_bh(lock); 839a8c2190eSArnaldo Carvalho de Melo goto done; 840a8c2190eSArnaldo Carvalho de Melo } 841a8c2190eSArnaldo Carvalho de Melo next_dying: 842a8c2190eSArnaldo Carvalho de Melo ++num; 843a8c2190eSArnaldo Carvalho de Melo } 844a8c2190eSArnaldo Carvalho de Melo } 8457e3aab4aSDavid S. Miller spin_unlock_bh(lock); 846a8c2190eSArnaldo Carvalho de Melo } 847a8c2190eSArnaldo Carvalho de Melo 848a8c2190eSArnaldo Carvalho de Melo done: 849a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 850a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 851d523a328SHerbert Xu unlock: 852d523a328SHerbert Xu inet_diag_unlock_handler(handler); 853a8c2190eSArnaldo Carvalho de Melo return skb->len; 854a8c2190eSArnaldo Carvalho de Melo } 855a8c2190eSArnaldo Carvalho de Melo 85625c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 85725c4cd2bSPavel Emelyanov { 85825c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 85925c4cd2bSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 86025c4cd2bSPavel Emelyanov 86125c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 86225c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 86325c4cd2bSPavel Emelyanov 86425c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc); 86525c4cd2bSPavel Emelyanov } 86625c4cd2bSPavel Emelyanov 867a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type) 868a029fe26SPavel Emelyanov { 869a029fe26SPavel Emelyanov switch (type) { 870a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 871a029fe26SPavel Emelyanov return IPPROTO_TCP; 872a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 873a029fe26SPavel Emelyanov return IPPROTO_DCCP; 874a029fe26SPavel Emelyanov default: 875a029fe26SPavel Emelyanov return 0; 876a029fe26SPavel Emelyanov } 877a029fe26SPavel Emelyanov } 878a029fe26SPavel Emelyanov 87925c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb) 88025c4cd2bSPavel Emelyanov { 88125c4cd2bSPavel Emelyanov struct inet_diag_req_compat *rc = NLMSG_DATA(cb->nlh); 88225c4cd2bSPavel Emelyanov struct inet_diag_req req; 88325c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 88425c4cd2bSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_compat); 88525c4cd2bSPavel Emelyanov 886d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 88725c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 88825c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 88925c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 89025c4cd2bSPavel Emelyanov req.id = rc->id; 89125c4cd2bSPavel Emelyanov 89225c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 89325c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 89425c4cd2bSPavel Emelyanov 89525c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 89625c4cd2bSPavel Emelyanov } 89725c4cd2bSPavel Emelyanov 898fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 899fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 900fe50ce28SPavel Emelyanov { 901fe50ce28SPavel Emelyanov struct inet_diag_req_compat *rc = NLMSG_DATA(nlh); 902fe50ce28SPavel Emelyanov struct inet_diag_req req; 903fe50ce28SPavel Emelyanov 904fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 905fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 906fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 907fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 908fe50ce28SPavel Emelyanov req.id = rc->id; 909fe50ce28SPavel Emelyanov 910fe50ce28SPavel Emelyanov return inet_diag_get_exact(in_skb, nlh, &req); 911fe50ce28SPavel Emelyanov } 912fe50ce28SPavel Emelyanov 9138d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 914a8c2190eSArnaldo Carvalho de Melo { 915126fdc32SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_compat); 916a8c2190eSArnaldo Carvalho de Melo 917ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 918ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 919ead592baSThomas Graf return -EINVAL; 920a8c2190eSArnaldo Carvalho de Melo 921b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 922ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 923ead592baSThomas Graf struct nlattr *attr; 924ead592baSThomas Graf 925ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 926ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 927ead592baSThomas Graf if (attr == NULL || 928ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 929ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 930a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 931a8c2190eSArnaldo Carvalho de Melo } 932a8c2190eSArnaldo Carvalho de Melo 9338ef874bfSPavel Emelyanov return netlink_dump_start(sock_diag_nlsk, skb, nlh, 93425c4cd2bSPavel Emelyanov inet_diag_dump_compat, NULL, 0); 935a8c2190eSArnaldo Carvalho de Melo } 936ead592baSThomas Graf 937fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 938a8c2190eSArnaldo Carvalho de Melo } 939a8c2190eSArnaldo Carvalho de Melo 940d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 941d366477aSPavel Emelyanov { 942d366477aSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 943d366477aSPavel Emelyanov 944d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 945d366477aSPavel Emelyanov return -EINVAL; 946d366477aSPavel Emelyanov 947d366477aSPavel Emelyanov if (h->nlmsg_flags & NLM_F_DUMP) { 94825c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 94925c4cd2bSPavel Emelyanov struct nlattr *attr; 95025c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 95125c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 95225c4cd2bSPavel Emelyanov if (attr == NULL || 95325c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 95425c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 95525c4cd2bSPavel Emelyanov return -EINVAL; 95625c4cd2bSPavel Emelyanov } 95725c4cd2bSPavel Emelyanov 9588ef874bfSPavel Emelyanov return netlink_dump_start(sock_diag_nlsk, skb, h, 95925c4cd2bSPavel Emelyanov inet_diag_dump, NULL, 0); 960d366477aSPavel Emelyanov } 961d366477aSPavel Emelyanov 962fe50ce28SPavel Emelyanov return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h)); 963d366477aSPavel Emelyanov } 964d366477aSPavel Emelyanov 965d366477aSPavel Emelyanov static struct sock_diag_handler inet_diag_handler = { 966d366477aSPavel Emelyanov .family = AF_INET, 967d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 968d366477aSPavel Emelyanov }; 969d366477aSPavel Emelyanov 970d366477aSPavel Emelyanov static struct sock_diag_handler inet6_diag_handler = { 971d366477aSPavel Emelyanov .family = AF_INET6, 972d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 973d366477aSPavel Emelyanov }; 974d366477aSPavel Emelyanov 975a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 976a8c2190eSArnaldo Carvalho de Melo { 977a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 978a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 979a8c2190eSArnaldo Carvalho de Melo 980f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 981a8c2190eSArnaldo Carvalho de Melo goto out; 982a8c2190eSArnaldo Carvalho de Melo 983d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 984a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 985a8c2190eSArnaldo Carvalho de Melo if (inet_diag_table[type] == NULL) { 986a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 987a8c2190eSArnaldo Carvalho de Melo err = 0; 988a8c2190eSArnaldo Carvalho de Melo } 989d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 990a8c2190eSArnaldo Carvalho de Melo out: 991a8c2190eSArnaldo Carvalho de Melo return err; 992a8c2190eSArnaldo Carvalho de Melo } 993a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 994a8c2190eSArnaldo Carvalho de Melo 995a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 996a8c2190eSArnaldo Carvalho de Melo { 997a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 998a8c2190eSArnaldo Carvalho de Melo 999f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1000a8c2190eSArnaldo Carvalho de Melo return; 1001a8c2190eSArnaldo Carvalho de Melo 1002d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1003a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1004d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1005a8c2190eSArnaldo Carvalho de Melo } 1006a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1007a8c2190eSArnaldo Carvalho de Melo 1008a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1009a8c2190eSArnaldo Carvalho de Melo { 1010f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1011a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1012a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1013a8c2190eSArnaldo Carvalho de Melo 10140da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1015a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1016a8c2190eSArnaldo Carvalho de Melo goto out; 1017a8c2190eSArnaldo Carvalho de Melo 1018d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1019d366477aSPavel Emelyanov if (err) 1020d366477aSPavel Emelyanov goto out_free_nl; 1021d366477aSPavel Emelyanov 1022d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1023d366477aSPavel Emelyanov if (err) 1024d366477aSPavel Emelyanov goto out_free_inet; 1025d366477aSPavel Emelyanov 10268ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1027a8c2190eSArnaldo Carvalho de Melo out: 1028a8c2190eSArnaldo Carvalho de Melo return err; 1029d366477aSPavel Emelyanov 1030d366477aSPavel Emelyanov out_free_inet: 1031d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1032d366477aSPavel Emelyanov out_free_nl: 1033a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1034a8c2190eSArnaldo Carvalho de Melo goto out; 1035a8c2190eSArnaldo Carvalho de Melo } 1036a8c2190eSArnaldo Carvalho de Melo 1037a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1038a8c2190eSArnaldo Carvalho de Melo { 1039d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1040d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 10418ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1042a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1043a8c2190eSArnaldo Carvalho de Melo } 1044a8c2190eSArnaldo Carvalho de Melo 1045a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1046a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1047a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 10488ef874bfSPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 0); 1049