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]) 57*aec8dc62SPavel Emelyanov request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK, 58*aec8dc62SPavel Emelyanov NETLINK_SOCK_DIAG, AF_INET, 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 733c4d05c8SPavel Emelyanov int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 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 struct inet_diag_msg *r; 80a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 81a8c2190eSArnaldo Carvalho de Melo void *info = NULL; 82a8c2190eSArnaldo Carvalho de Melo struct inet_diag_meminfo *minfo = NULL; 8327a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 84a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_handler *handler; 85a029fe26SPavel Emelyanov int ext = req->idiag_ext; 86a8c2190eSArnaldo Carvalho de Melo 87a029fe26SPavel Emelyanov handler = inet_diag_table[req->sdiag_protocol]; 88a8c2190eSArnaldo Carvalho de Melo BUG_ON(handler == NULL); 89a8c2190eSArnaldo Carvalho de Melo 90a8c2190eSArnaldo Carvalho de Melo nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 91a8c2190eSArnaldo Carvalho de Melo nlh->nlmsg_flags = nlmsg_flags; 92a8c2190eSArnaldo Carvalho de Melo 93a8c2190eSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 94c7d58aabSArnaldo Carvalho de Melo BUG_ON(sk->sk_state == TCP_TIME_WAIT); 95c7d58aabSArnaldo Carvalho de Melo 96a8c2190eSArnaldo Carvalho de Melo if (ext & (1 << (INET_DIAG_MEMINFO - 1))) 97c7d58aabSArnaldo Carvalho de Melo minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo)); 98c7d58aabSArnaldo Carvalho de Melo 99a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 100a8c2190eSArnaldo Carvalho de Melo r->idiag_state = sk->sk_state; 101a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 102a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = 0; 103a8c2190eSArnaldo Carvalho de Melo 104a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 105a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)sk; 106a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); 107a8c2190eSArnaldo Carvalho de Melo 108c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 109c720c7e8SEric Dumazet r->id.idiag_dport = inet->inet_dport; 110c720c7e8SEric Dumazet r->id.idiag_src[0] = inet->inet_rcv_saddr; 111c720c7e8SEric Dumazet r->id.idiag_dst[0] = inet->inet_daddr; 112a8c2190eSArnaldo Carvalho de Melo 113717b6d83SMaciej Żenczykowski /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 114717b6d83SMaciej Żenczykowski * hence this needs to be included regardless of socket family. 115717b6d83SMaciej Żenczykowski */ 116717b6d83SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TOS - 1))) 117717b6d83SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos); 118717b6d83SMaciej Żenczykowski 119dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 120a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 121b71d1d42SEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 122a8c2190eSArnaldo Carvalho de Melo 1234e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 1244e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = np->daddr; 12506236ac3SMaciej Żenczykowski if (ext & (1 << (INET_DIAG_TCLASS - 1))) 12606236ac3SMaciej Żenczykowski RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); 127a8c2190eSArnaldo Carvalho de Melo } 128a8c2190eSArnaldo Carvalho de Melo #endif 129a8c2190eSArnaldo Carvalho de Melo 1303c4d05c8SPavel Emelyanov r->idiag_uid = sock_i_uid(sk); 1313c4d05c8SPavel Emelyanov r->idiag_inode = sock_i_ino(sk); 1323c4d05c8SPavel Emelyanov 1333c4d05c8SPavel Emelyanov if (minfo) { 1343c4d05c8SPavel Emelyanov minfo->idiag_rmem = sk_rmem_alloc_get(sk); 1353c4d05c8SPavel Emelyanov minfo->idiag_wmem = sk->sk_wmem_queued; 1363c4d05c8SPavel Emelyanov minfo->idiag_fmem = sk->sk_forward_alloc; 1373c4d05c8SPavel Emelyanov minfo->idiag_tmem = sk_wmem_alloc_get(sk); 1383c4d05c8SPavel Emelyanov } 1393c4d05c8SPavel Emelyanov 1403c4d05c8SPavel Emelyanov if (icsk == NULL) { 1413c4d05c8SPavel Emelyanov r->idiag_rqueue = r->idiag_wqueue = 0; 1423c4d05c8SPavel Emelyanov goto out; 1433c4d05c8SPavel Emelyanov } 1443c4d05c8SPavel Emelyanov 145172589ccSIlpo Järvinen #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 146a8c2190eSArnaldo Carvalho de Melo 147a8c2190eSArnaldo Carvalho de Melo if (icsk->icsk_pending == ICSK_TIME_RETRANS) { 148a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 149a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_retransmits; 150a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 151a8c2190eSArnaldo Carvalho de Melo } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 152a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 4; 153a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 154a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 155a8c2190eSArnaldo Carvalho de Melo } else if (timer_pending(&sk->sk_timer)) { 156a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 2; 157a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = icsk->icsk_probes_out; 158a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 159a8c2190eSArnaldo Carvalho de Melo } else { 160a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 0; 161a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = 0; 162a8c2190eSArnaldo Carvalho de Melo } 163a8c2190eSArnaldo Carvalho de Melo #undef EXPIRES_IN_MS 164a8c2190eSArnaldo Carvalho de Melo 1653c4d05c8SPavel Emelyanov if (ext & (1 << (INET_DIAG_INFO - 1))) 1663c4d05c8SPavel Emelyanov info = INET_DIAG_PUT(skb, INET_DIAG_INFO, sizeof(struct tcp_info)); 167a8c2190eSArnaldo Carvalho de Melo 1683c4d05c8SPavel Emelyanov if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) { 1693c4d05c8SPavel Emelyanov const size_t len = strlen(icsk->icsk_ca_ops->name); 1703c4d05c8SPavel Emelyanov 1713c4d05c8SPavel Emelyanov strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1), 1723c4d05c8SPavel Emelyanov icsk->icsk_ca_ops->name); 173a8c2190eSArnaldo Carvalho de Melo } 174a8c2190eSArnaldo Carvalho de Melo 175a8c2190eSArnaldo Carvalho de Melo handler->idiag_get_info(sk, r, info); 176a8c2190eSArnaldo Carvalho de Melo 177a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state < TCP_TIME_WAIT && 178a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 179a8c2190eSArnaldo Carvalho de Melo icsk->icsk_ca_ops->get_info(sk, ext, skb); 180a8c2190eSArnaldo Carvalho de Melo 1813c4d05c8SPavel Emelyanov out: 18227a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 183a8c2190eSArnaldo Carvalho de Melo return skb->len; 184a8c2190eSArnaldo Carvalho de Melo 185a8c2190eSArnaldo Carvalho de Melo rtattr_failure: 186a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 187dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 18826932566SPatrick McHardy return -EMSGSIZE; 189a8c2190eSArnaldo Carvalho de Melo } 1903c4d05c8SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 1913c4d05c8SPavel Emelyanov 1923c4d05c8SPavel Emelyanov static int inet_csk_diag_fill(struct sock *sk, 1933c4d05c8SPavel Emelyanov struct sk_buff *skb, struct inet_diag_req *req, 1943c4d05c8SPavel Emelyanov u32 pid, u32 seq, u16 nlmsg_flags, 1953c4d05c8SPavel Emelyanov const struct nlmsghdr *unlh) 1963c4d05c8SPavel Emelyanov { 1973c4d05c8SPavel Emelyanov return inet_sk_diag_fill(sk, inet_csk(sk), 1983c4d05c8SPavel Emelyanov skb, req, pid, seq, nlmsg_flags, unlh); 1993c4d05c8SPavel Emelyanov } 200a8c2190eSArnaldo Carvalho de Melo 201c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 202a029fe26SPavel Emelyanov struct sk_buff *skb, struct inet_diag_req *req, 203a029fe26SPavel Emelyanov u32 pid, u32 seq, u16 nlmsg_flags, 204c7d58aabSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 205c7d58aabSArnaldo Carvalho de Melo { 206c7d58aabSArnaldo Carvalho de Melo long tmo; 207c7d58aabSArnaldo Carvalho de Melo struct inet_diag_msg *r; 20827a884dcSArnaldo Carvalho de Melo const unsigned char *previous_tail = skb_tail_pointer(skb); 209c7d58aabSArnaldo Carvalho de Melo struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, 210c7d58aabSArnaldo Carvalho de Melo unlh->nlmsg_type, sizeof(*r)); 211c7d58aabSArnaldo Carvalho de Melo 212c7d58aabSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 213c7d58aabSArnaldo Carvalho de Melo BUG_ON(tw->tw_state != TCP_TIME_WAIT); 214c7d58aabSArnaldo Carvalho de Melo 215c7d58aabSArnaldo Carvalho de Melo nlh->nlmsg_flags = nlmsg_flags; 216c7d58aabSArnaldo Carvalho de Melo 217c7d58aabSArnaldo Carvalho de Melo tmo = tw->tw_ttd - jiffies; 218c7d58aabSArnaldo Carvalho de Melo if (tmo < 0) 219c7d58aabSArnaldo Carvalho de Melo tmo = 0; 220c7d58aabSArnaldo Carvalho de Melo 221c7d58aabSArnaldo Carvalho de Melo r->idiag_family = tw->tw_family; 222c7d58aabSArnaldo Carvalho de Melo r->idiag_retrans = 0; 223c7d58aabSArnaldo Carvalho de Melo r->id.idiag_if = tw->tw_bound_dev_if; 224c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)tw; 225c7d58aabSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1); 226c7d58aabSArnaldo Carvalho de Melo r->id.idiag_sport = tw->tw_sport; 227c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dport = tw->tw_dport; 228c7d58aabSArnaldo Carvalho de Melo r->id.idiag_src[0] = tw->tw_rcv_saddr; 229c7d58aabSArnaldo Carvalho de Melo r->id.idiag_dst[0] = tw->tw_daddr; 230c7d58aabSArnaldo Carvalho de Melo r->idiag_state = tw->tw_substate; 231c7d58aabSArnaldo Carvalho de Melo r->idiag_timer = 3; 232172589ccSIlpo Järvinen r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 233c7d58aabSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 234c7d58aabSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 235c7d58aabSArnaldo Carvalho de Melo r->idiag_uid = 0; 236c7d58aabSArnaldo Carvalho de Melo r->idiag_inode = 0; 237dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 238c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 239c7d58aabSArnaldo Carvalho de Melo const struct inet6_timewait_sock *tw6 = 240c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 241c7d58aabSArnaldo Carvalho de Melo 2424e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr; 2434e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr; 244c7d58aabSArnaldo Carvalho de Melo } 245c7d58aabSArnaldo Carvalho de Melo #endif 24627a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; 247c7d58aabSArnaldo Carvalho de Melo return skb->len; 248c7d58aabSArnaldo Carvalho de Melo nlmsg_failure: 249dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, previous_tail); 25026932566SPatrick McHardy return -EMSGSIZE; 251c7d58aabSArnaldo Carvalho de Melo } 252c7d58aabSArnaldo Carvalho de Melo 253dff2c035SArnaldo Carvalho de Melo static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 254a029fe26SPavel Emelyanov struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags, 255dff2c035SArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 256dff2c035SArnaldo Carvalho de Melo { 257dff2c035SArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 258dff2c035SArnaldo Carvalho de Melo return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 259a029fe26SPavel Emelyanov skb, r, pid, seq, nlmsg_flags, 260dff2c035SArnaldo Carvalho de Melo unlh); 261a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh); 262dff2c035SArnaldo Carvalho de Melo } 263dff2c035SArnaldo Carvalho de Melo 264b005ab4eSPavel Emelyanov int inet_diag_check_cookie(struct sock *sk, struct inet_diag_req *req) 265b005ab4eSPavel Emelyanov { 266b005ab4eSPavel Emelyanov if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE || 267b005ab4eSPavel Emelyanov req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) && 268b005ab4eSPavel Emelyanov ((u32)(unsigned long)sk != req->id.idiag_cookie[0] || 269b005ab4eSPavel Emelyanov (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1])) 270b005ab4eSPavel Emelyanov return -ESTALE; 271b005ab4eSPavel Emelyanov else 272b005ab4eSPavel Emelyanov return 0; 273b005ab4eSPavel Emelyanov } 274b005ab4eSPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_check_cookie); 275b005ab4eSPavel Emelyanov 2761942c518SPavel Emelyanov int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, 277476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, struct inet_diag_req *req) 278a8c2190eSArnaldo Carvalho de Melo { 279a8c2190eSArnaldo Carvalho de Melo int err; 280a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 281a8c2190eSArnaldo Carvalho de Melo struct sk_buff *rep; 282a8c2190eSArnaldo Carvalho de Melo 283d523a328SHerbert Xu err = -EINVAL; 284fe50ce28SPavel Emelyanov if (req->sdiag_family == AF_INET) { 285c67499c0SPavel Emelyanov sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], 286a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, req->id.idiag_src[0], 287a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, req->id.idiag_if); 288a8c2190eSArnaldo Carvalho de Melo } 289dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 290fe50ce28SPavel Emelyanov else if (req->sdiag_family == AF_INET6) { 291d86e0dacSPavel Emelyanov sk = inet6_lookup(&init_net, hashinfo, 292a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_dst, 293a8c2190eSArnaldo Carvalho de Melo req->id.idiag_dport, 294a8c2190eSArnaldo Carvalho de Melo (struct in6_addr *)req->id.idiag_src, 295a8c2190eSArnaldo Carvalho de Melo req->id.idiag_sport, 296a8c2190eSArnaldo Carvalho de Melo req->id.idiag_if); 297a8c2190eSArnaldo Carvalho de Melo } 298a8c2190eSArnaldo Carvalho de Melo #endif 299a8c2190eSArnaldo Carvalho de Melo else { 300476f7dbfSPavel Emelyanov goto out_nosk; 301a8c2190eSArnaldo Carvalho de Melo } 302a8c2190eSArnaldo Carvalho de Melo 303d523a328SHerbert Xu err = -ENOENT; 304a8c2190eSArnaldo Carvalho de Melo if (sk == NULL) 305476f7dbfSPavel Emelyanov goto out_nosk; 306a8c2190eSArnaldo Carvalho de Melo 307b005ab4eSPavel Emelyanov err = inet_diag_check_cookie(sk, req); 308b005ab4eSPavel Emelyanov if (err) 309a8c2190eSArnaldo Carvalho de Melo goto out; 310a8c2190eSArnaldo Carvalho de Melo 311a8c2190eSArnaldo Carvalho de Melo err = -ENOMEM; 312a8c2190eSArnaldo Carvalho de Melo rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + 313a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_meminfo) + 3147b35eaddSPavel Emelyanov sizeof(struct tcp_info) + 64)), 315a8c2190eSArnaldo Carvalho de Melo GFP_KERNEL); 316a8c2190eSArnaldo Carvalho de Melo if (!rep) 317a8c2190eSArnaldo Carvalho de Melo goto out; 318a8c2190eSArnaldo Carvalho de Melo 319a029fe26SPavel Emelyanov err = sk_diag_fill(sk, rep, req, 320a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(in_skb).pid, 32126932566SPatrick McHardy nlh->nlmsg_seq, 0, nlh); 32226932566SPatrick McHardy if (err < 0) { 32326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 32426932566SPatrick McHardy kfree_skb(rep); 32526932566SPatrick McHardy goto out; 32626932566SPatrick McHardy } 3278ef874bfSPavel Emelyanov err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid, 328a8c2190eSArnaldo Carvalho de Melo MSG_DONTWAIT); 329a8c2190eSArnaldo Carvalho de Melo if (err > 0) 330a8c2190eSArnaldo Carvalho de Melo err = 0; 331a8c2190eSArnaldo Carvalho de Melo 332a8c2190eSArnaldo Carvalho de Melo out: 333a8c2190eSArnaldo Carvalho de Melo if (sk) { 334a8c2190eSArnaldo Carvalho de Melo if (sk->sk_state == TCP_TIME_WAIT) 335a8c2190eSArnaldo Carvalho de Melo inet_twsk_put((struct inet_timewait_sock *)sk); 336a8c2190eSArnaldo Carvalho de Melo else 337a8c2190eSArnaldo Carvalho de Melo sock_put(sk); 338a8c2190eSArnaldo Carvalho de Melo } 339476f7dbfSPavel Emelyanov out_nosk: 340476f7dbfSPavel Emelyanov return err; 341476f7dbfSPavel Emelyanov } 3421942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 343476f7dbfSPavel Emelyanov 344476f7dbfSPavel Emelyanov static int inet_diag_get_exact(struct sk_buff *in_skb, 345476f7dbfSPavel Emelyanov const struct nlmsghdr *nlh, 346476f7dbfSPavel Emelyanov struct inet_diag_req *req) 347476f7dbfSPavel Emelyanov { 348476f7dbfSPavel Emelyanov const struct inet_diag_handler *handler; 349476f7dbfSPavel Emelyanov int err; 350476f7dbfSPavel Emelyanov 351476f7dbfSPavel Emelyanov handler = inet_diag_lock_handler(req->sdiag_protocol); 352476f7dbfSPavel Emelyanov if (IS_ERR(handler)) 353476f7dbfSPavel Emelyanov err = PTR_ERR(handler); 354476f7dbfSPavel Emelyanov else 3551942c518SPavel Emelyanov err = handler->dump_one(in_skb, nlh, req); 356d523a328SHerbert Xu inet_diag_unlock_handler(handler); 357476f7dbfSPavel Emelyanov 358a8c2190eSArnaldo Carvalho de Melo return err; 359a8c2190eSArnaldo Carvalho de Melo } 360a8c2190eSArnaldo Carvalho de Melo 3619f855299SAl Viro static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 362a8c2190eSArnaldo Carvalho de Melo { 363a8c2190eSArnaldo Carvalho de Melo int words = bits >> 5; 364a8c2190eSArnaldo Carvalho de Melo 365a8c2190eSArnaldo Carvalho de Melo bits &= 0x1f; 366a8c2190eSArnaldo Carvalho de Melo 367a8c2190eSArnaldo Carvalho de Melo if (words) { 368a8c2190eSArnaldo Carvalho de Melo if (memcmp(a1, a2, words << 2)) 369a8c2190eSArnaldo Carvalho de Melo return 0; 370a8c2190eSArnaldo Carvalho de Melo } 371a8c2190eSArnaldo Carvalho de Melo if (bits) { 3729f855299SAl Viro __be32 w1, w2; 3739f855299SAl Viro __be32 mask; 374a8c2190eSArnaldo Carvalho de Melo 375a8c2190eSArnaldo Carvalho de Melo w1 = a1[words]; 376a8c2190eSArnaldo Carvalho de Melo w2 = a2[words]; 377a8c2190eSArnaldo Carvalho de Melo 378a8c2190eSArnaldo Carvalho de Melo mask = htonl((0xffffffff) << (32 - bits)); 379a8c2190eSArnaldo Carvalho de Melo 380a8c2190eSArnaldo Carvalho de Melo if ((w1 ^ w2) & mask) 381a8c2190eSArnaldo Carvalho de Melo return 0; 382a8c2190eSArnaldo Carvalho de Melo } 383a8c2190eSArnaldo Carvalho de Melo 384a8c2190eSArnaldo Carvalho de Melo return 1; 385a8c2190eSArnaldo Carvalho de Melo } 386a8c2190eSArnaldo Carvalho de Melo 387a8c2190eSArnaldo Carvalho de Melo 38887c22ea5SPavel Emelyanov static int inet_diag_bc_run(const struct nlattr *_bc, 389a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_entry *entry) 390a8c2190eSArnaldo Carvalho de Melo { 39187c22ea5SPavel Emelyanov const void *bc = nla_data(_bc); 39287c22ea5SPavel Emelyanov int len = nla_len(_bc); 39387c22ea5SPavel Emelyanov 394a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 395a8c2190eSArnaldo Carvalho de Melo int yes = 1; 396a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 397a8c2190eSArnaldo Carvalho de Melo 398a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 399a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 400a8c2190eSArnaldo Carvalho de Melo break; 401a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 402a8c2190eSArnaldo Carvalho de Melo yes = 0; 403a8c2190eSArnaldo Carvalho de Melo break; 404a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 405a8c2190eSArnaldo Carvalho de Melo yes = entry->sport >= op[1].no; 406a8c2190eSArnaldo Carvalho de Melo break; 407a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 408b4ced2b7SRoel Kluin yes = entry->sport <= op[1].no; 409a8c2190eSArnaldo Carvalho de Melo break; 410a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 411a8c2190eSArnaldo Carvalho de Melo yes = entry->dport >= op[1].no; 412a8c2190eSArnaldo Carvalho de Melo break; 413a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 414a8c2190eSArnaldo Carvalho de Melo yes = entry->dport <= op[1].no; 415a8c2190eSArnaldo Carvalho de Melo break; 416a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 417a8c2190eSArnaldo Carvalho de Melo yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 418a8c2190eSArnaldo Carvalho de Melo break; 419a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 420a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: { 421a8c2190eSArnaldo Carvalho de Melo struct inet_diag_hostcond *cond; 4229f855299SAl Viro __be32 *addr; 423a8c2190eSArnaldo Carvalho de Melo 424a8c2190eSArnaldo Carvalho de Melo cond = (struct inet_diag_hostcond *)(op + 1); 425a8c2190eSArnaldo Carvalho de Melo if (cond->port != -1 && 426a8c2190eSArnaldo Carvalho de Melo cond->port != (op->code == INET_DIAG_BC_S_COND ? 427a8c2190eSArnaldo Carvalho de Melo entry->sport : entry->dport)) { 428a8c2190eSArnaldo Carvalho de Melo yes = 0; 429a8c2190eSArnaldo Carvalho de Melo break; 430a8c2190eSArnaldo Carvalho de Melo } 431a8c2190eSArnaldo Carvalho de Melo 432a8c2190eSArnaldo Carvalho de Melo if (cond->prefix_len == 0) 433a8c2190eSArnaldo Carvalho de Melo break; 434a8c2190eSArnaldo Carvalho de Melo 435a8c2190eSArnaldo Carvalho de Melo if (op->code == INET_DIAG_BC_S_COND) 436a8c2190eSArnaldo Carvalho de Melo addr = entry->saddr; 437a8c2190eSArnaldo Carvalho de Melo else 438a8c2190eSArnaldo Carvalho de Melo addr = entry->daddr; 439a8c2190eSArnaldo Carvalho de Melo 4404e852c02SArnaldo Carvalho de Melo if (bitstring_match(addr, cond->addr, 4414e852c02SArnaldo Carvalho de Melo cond->prefix_len)) 442a8c2190eSArnaldo Carvalho de Melo break; 443a8c2190eSArnaldo Carvalho de Melo if (entry->family == AF_INET6 && 444a8c2190eSArnaldo Carvalho de Melo cond->family == AF_INET) { 445a8c2190eSArnaldo Carvalho de Melo if (addr[0] == 0 && addr[1] == 0 && 446a8c2190eSArnaldo Carvalho de Melo addr[2] == htonl(0xffff) && 447a8c2190eSArnaldo Carvalho de Melo bitstring_match(addr + 3, cond->addr, 448a8c2190eSArnaldo Carvalho de Melo cond->prefix_len)) 449a8c2190eSArnaldo Carvalho de Melo break; 450a8c2190eSArnaldo Carvalho de Melo } 451a8c2190eSArnaldo Carvalho de Melo yes = 0; 452a8c2190eSArnaldo Carvalho de Melo break; 453a8c2190eSArnaldo Carvalho de Melo } 454a8c2190eSArnaldo Carvalho de Melo } 455a8c2190eSArnaldo Carvalho de Melo 456a8c2190eSArnaldo Carvalho de Melo if (yes) { 457a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 458a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 459a8c2190eSArnaldo Carvalho de Melo } else { 460a8c2190eSArnaldo Carvalho de Melo len -= op->no; 461a8c2190eSArnaldo Carvalho de Melo bc += op->no; 462a8c2190eSArnaldo Carvalho de Melo } 463a8c2190eSArnaldo Carvalho de Melo } 464a02cec21SEric Dumazet return len == 0; 465a8c2190eSArnaldo Carvalho de Melo } 466a8c2190eSArnaldo Carvalho de Melo 4678d07d151SPavel Emelyanov int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 4688d07d151SPavel Emelyanov { 4698d07d151SPavel Emelyanov struct inet_diag_entry entry; 4708d07d151SPavel Emelyanov struct inet_sock *inet = inet_sk(sk); 4718d07d151SPavel Emelyanov 4728d07d151SPavel Emelyanov if (bc == NULL) 4738d07d151SPavel Emelyanov return 1; 4748d07d151SPavel Emelyanov 4758d07d151SPavel Emelyanov entry.family = sk->sk_family; 476dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4778d07d151SPavel Emelyanov if (entry.family == AF_INET6) { 4788d07d151SPavel Emelyanov struct ipv6_pinfo *np = inet6_sk(sk); 4798d07d151SPavel Emelyanov 4808d07d151SPavel Emelyanov entry.saddr = np->rcv_saddr.s6_addr32; 4818d07d151SPavel Emelyanov entry.daddr = np->daddr.s6_addr32; 4828d07d151SPavel Emelyanov } else 4838d07d151SPavel Emelyanov #endif 4848d07d151SPavel Emelyanov { 4858d07d151SPavel Emelyanov entry.saddr = &inet->inet_rcv_saddr; 4868d07d151SPavel Emelyanov entry.daddr = &inet->inet_daddr; 4878d07d151SPavel Emelyanov } 4888d07d151SPavel Emelyanov entry.sport = inet->inet_num; 4898d07d151SPavel Emelyanov entry.dport = ntohs(inet->inet_dport); 4908d07d151SPavel Emelyanov entry.userlocks = sk->sk_userlocks; 4918d07d151SPavel Emelyanov 4928d07d151SPavel Emelyanov return inet_diag_bc_run(bc, &entry); 4938d07d151SPavel Emelyanov } 4948d07d151SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 4958d07d151SPavel Emelyanov 496a8c2190eSArnaldo Carvalho de Melo static int valid_cc(const void *bc, int len, int cc) 497a8c2190eSArnaldo Carvalho de Melo { 498a8c2190eSArnaldo Carvalho de Melo while (len >= 0) { 499a8c2190eSArnaldo Carvalho de Melo const struct inet_diag_bc_op *op = bc; 500a8c2190eSArnaldo Carvalho de Melo 501a8c2190eSArnaldo Carvalho de Melo if (cc > len) 502a8c2190eSArnaldo Carvalho de Melo return 0; 503a8c2190eSArnaldo Carvalho de Melo if (cc == len) 504a8c2190eSArnaldo Carvalho de Melo return 1; 505eeb14972SEric Dumazet if (op->yes < 4 || op->yes & 3) 506a8c2190eSArnaldo Carvalho de Melo return 0; 507a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 508a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 509a8c2190eSArnaldo Carvalho de Melo } 510a8c2190eSArnaldo Carvalho de Melo return 0; 511a8c2190eSArnaldo Carvalho de Melo } 512a8c2190eSArnaldo Carvalho de Melo 513a8c2190eSArnaldo Carvalho de Melo static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 514a8c2190eSArnaldo Carvalho de Melo { 515eeb14972SEric Dumazet const void *bc = bytecode; 516a8c2190eSArnaldo Carvalho de Melo int len = bytecode_len; 517a8c2190eSArnaldo Carvalho de Melo 518a8c2190eSArnaldo Carvalho de Melo while (len > 0) { 519eeb14972SEric Dumazet const struct inet_diag_bc_op *op = bc; 520a8c2190eSArnaldo Carvalho de Melo 521a8c2190eSArnaldo Carvalho de Melo //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); 522a8c2190eSArnaldo Carvalho de Melo switch (op->code) { 523a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_AUTO: 524a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_COND: 525a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_COND: 526a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_GE: 527a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_S_LE: 528a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_GE: 529a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_D_LE: 530a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_JMP: 531eeb14972SEric Dumazet if (op->no < 4 || op->no > len + 4 || op->no & 3) 532a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 533a8c2190eSArnaldo Carvalho de Melo if (op->no < len && 534a8c2190eSArnaldo Carvalho de Melo !valid_cc(bytecode, bytecode_len, len - op->no)) 535a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 536a8c2190eSArnaldo Carvalho de Melo break; 537a8c2190eSArnaldo Carvalho de Melo case INET_DIAG_BC_NOP: 538a8c2190eSArnaldo Carvalho de Melo break; 539a8c2190eSArnaldo Carvalho de Melo default: 540a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 541a8c2190eSArnaldo Carvalho de Melo } 542eeb14972SEric Dumazet if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) 543eeb14972SEric Dumazet return -EINVAL; 544a8c2190eSArnaldo Carvalho de Melo bc += op->yes; 545a8c2190eSArnaldo Carvalho de Melo len -= op->yes; 546a8c2190eSArnaldo Carvalho de Melo } 547a8c2190eSArnaldo Carvalho de Melo return len == 0 ? 0 : -EINVAL; 548a8c2190eSArnaldo Carvalho de Melo } 549a8c2190eSArnaldo Carvalho de Melo 550dff2c035SArnaldo Carvalho de Melo static int inet_csk_diag_dump(struct sock *sk, 551dff2c035SArnaldo Carvalho de Melo struct sk_buff *skb, 55237f352b5SPavel Emelyanov struct netlink_callback *cb, 55325c4cd2bSPavel Emelyanov struct inet_diag_req *r, 55437f352b5SPavel Emelyanov const struct nlattr *bc) 555a8c2190eSArnaldo Carvalho de Melo { 5568d07d151SPavel Emelyanov if (!inet_diag_bc_sk(bc, sk)) 557a8c2190eSArnaldo Carvalho de Melo return 0; 558a8c2190eSArnaldo Carvalho de Melo 559a029fe26SPavel Emelyanov return inet_csk_diag_fill(sk, skb, r, 560dff2c035SArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 561a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 562a8c2190eSArnaldo Carvalho de Melo } 563a8c2190eSArnaldo Carvalho de Melo 564c7d58aabSArnaldo Carvalho de Melo static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 565c7d58aabSArnaldo Carvalho de Melo struct sk_buff *skb, 56637f352b5SPavel Emelyanov struct netlink_callback *cb, 56725c4cd2bSPavel Emelyanov struct inet_diag_req *r, 56837f352b5SPavel Emelyanov const struct nlattr *bc) 569c7d58aabSArnaldo Carvalho de Melo { 57037f352b5SPavel Emelyanov if (bc != NULL) { 571c7d58aabSArnaldo Carvalho de Melo struct inet_diag_entry entry; 572c7d58aabSArnaldo Carvalho de Melo 573c7d58aabSArnaldo Carvalho de Melo entry.family = tw->tw_family; 574dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 575c7d58aabSArnaldo Carvalho de Melo if (tw->tw_family == AF_INET6) { 576c7d58aabSArnaldo Carvalho de Melo struct inet6_timewait_sock *tw6 = 577c7d58aabSArnaldo Carvalho de Melo inet6_twsk((struct sock *)tw); 578c7d58aabSArnaldo Carvalho de Melo entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32; 579c7d58aabSArnaldo Carvalho de Melo entry.daddr = tw6->tw_v6_daddr.s6_addr32; 580c7d58aabSArnaldo Carvalho de Melo } else 581c7d58aabSArnaldo Carvalho de Melo #endif 582c7d58aabSArnaldo Carvalho de Melo { 583c7d58aabSArnaldo Carvalho de Melo entry.saddr = &tw->tw_rcv_saddr; 584c7d58aabSArnaldo Carvalho de Melo entry.daddr = &tw->tw_daddr; 585c7d58aabSArnaldo Carvalho de Melo } 586c7d58aabSArnaldo Carvalho de Melo entry.sport = tw->tw_num; 587c7d58aabSArnaldo Carvalho de Melo entry.dport = ntohs(tw->tw_dport); 588c7d58aabSArnaldo Carvalho de Melo entry.userlocks = 0; 589c7d58aabSArnaldo Carvalho de Melo 59087c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 591c7d58aabSArnaldo Carvalho de Melo return 0; 592c7d58aabSArnaldo Carvalho de Melo } 593c7d58aabSArnaldo Carvalho de Melo 594a029fe26SPavel Emelyanov return inet_twsk_diag_fill(tw, skb, r, 595c7d58aabSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 596c7d58aabSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 597c7d58aabSArnaldo Carvalho de Melo } 598c7d58aabSArnaldo Carvalho de Melo 599a8c2190eSArnaldo Carvalho de Melo static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, 6004e852c02SArnaldo Carvalho de Melo struct request_sock *req, u32 pid, u32 seq, 601a8c2190eSArnaldo Carvalho de Melo const struct nlmsghdr *unlh) 602a8c2190eSArnaldo Carvalho de Melo { 603a8c2190eSArnaldo Carvalho de Melo const struct inet_request_sock *ireq = inet_rsk(req); 604a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 60527a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 606a8c2190eSArnaldo Carvalho de Melo struct inet_diag_msg *r; 607a8c2190eSArnaldo Carvalho de Melo struct nlmsghdr *nlh; 608a8c2190eSArnaldo Carvalho de Melo long tmo; 609a8c2190eSArnaldo Carvalho de Melo 610a8c2190eSArnaldo Carvalho de Melo nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 611a8c2190eSArnaldo Carvalho de Melo nlh->nlmsg_flags = NLM_F_MULTI; 612a8c2190eSArnaldo Carvalho de Melo r = NLMSG_DATA(nlh); 613a8c2190eSArnaldo Carvalho de Melo 614a8c2190eSArnaldo Carvalho de Melo r->idiag_family = sk->sk_family; 615a8c2190eSArnaldo Carvalho de Melo r->idiag_state = TCP_SYN_RECV; 616a8c2190eSArnaldo Carvalho de Melo r->idiag_timer = 1; 617a8c2190eSArnaldo Carvalho de Melo r->idiag_retrans = req->retrans; 618a8c2190eSArnaldo Carvalho de Melo 619a8c2190eSArnaldo Carvalho de Melo r->id.idiag_if = sk->sk_bound_dev_if; 620a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[0] = (u32)(unsigned long)req; 621a8c2190eSArnaldo Carvalho de Melo r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); 622a8c2190eSArnaldo Carvalho de Melo 623a8c2190eSArnaldo Carvalho de Melo tmo = req->expires - jiffies; 624a8c2190eSArnaldo Carvalho de Melo if (tmo < 0) 625a8c2190eSArnaldo Carvalho de Melo tmo = 0; 626a8c2190eSArnaldo Carvalho de Melo 627c720c7e8SEric Dumazet r->id.idiag_sport = inet->inet_sport; 628a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport = ireq->rmt_port; 629a8c2190eSArnaldo Carvalho de Melo r->id.idiag_src[0] = ireq->loc_addr; 630a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dst[0] = ireq->rmt_addr; 631a8c2190eSArnaldo Carvalho de Melo r->idiag_expires = jiffies_to_msecs(tmo); 632a8c2190eSArnaldo Carvalho de Melo r->idiag_rqueue = 0; 633a8c2190eSArnaldo Carvalho de Melo r->idiag_wqueue = 0; 634a8c2190eSArnaldo Carvalho de Melo r->idiag_uid = sock_i_uid(sk); 635a8c2190eSArnaldo Carvalho de Melo r->idiag_inode = 0; 636dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 637a8c2190eSArnaldo Carvalho de Melo if (r->idiag_family == AF_INET6) { 6384e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr; 6394e3fd7a0SAlexey Dobriyan *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr; 640a8c2190eSArnaldo Carvalho de Melo } 641a8c2190eSArnaldo Carvalho de Melo #endif 64227a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - b; 643a8c2190eSArnaldo Carvalho de Melo 644a8c2190eSArnaldo Carvalho de Melo return skb->len; 645a8c2190eSArnaldo Carvalho de Melo 646a8c2190eSArnaldo Carvalho de Melo nlmsg_failure: 647dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 648a8c2190eSArnaldo Carvalho de Melo return -1; 649a8c2190eSArnaldo Carvalho de Melo } 650a8c2190eSArnaldo Carvalho de Melo 651a8c2190eSArnaldo Carvalho de Melo static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 65237f352b5SPavel Emelyanov struct netlink_callback *cb, 65325c4cd2bSPavel Emelyanov struct inet_diag_req *r, 65437f352b5SPavel Emelyanov const struct nlattr *bc) 655a8c2190eSArnaldo Carvalho de Melo { 656a8c2190eSArnaldo Carvalho de Melo struct inet_diag_entry entry; 657a8c2190eSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 658a8c2190eSArnaldo Carvalho de Melo struct listen_sock *lopt; 659a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 660a8c2190eSArnaldo Carvalho de Melo int j, s_j; 661a8c2190eSArnaldo Carvalho de Melo int reqnum, s_reqnum; 662a8c2190eSArnaldo Carvalho de Melo int err = 0; 663a8c2190eSArnaldo Carvalho de Melo 664a8c2190eSArnaldo Carvalho de Melo s_j = cb->args[3]; 665a8c2190eSArnaldo Carvalho de Melo s_reqnum = cb->args[4]; 666a8c2190eSArnaldo Carvalho de Melo 667a8c2190eSArnaldo Carvalho de Melo if (s_j > 0) 668a8c2190eSArnaldo Carvalho de Melo s_j--; 669a8c2190eSArnaldo Carvalho de Melo 670a8c2190eSArnaldo Carvalho de Melo entry.family = sk->sk_family; 671a8c2190eSArnaldo Carvalho de Melo 672a8c2190eSArnaldo Carvalho de Melo read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 673a8c2190eSArnaldo Carvalho de Melo 674a8c2190eSArnaldo Carvalho de Melo lopt = icsk->icsk_accept_queue.listen_opt; 675a8c2190eSArnaldo Carvalho de Melo if (!lopt || !lopt->qlen) 676a8c2190eSArnaldo Carvalho de Melo goto out; 677a8c2190eSArnaldo Carvalho de Melo 67837f352b5SPavel Emelyanov if (bc != NULL) { 679c720c7e8SEric Dumazet entry.sport = inet->inet_num; 680a8c2190eSArnaldo Carvalho de Melo entry.userlocks = sk->sk_userlocks; 681a8c2190eSArnaldo Carvalho de Melo } 682a8c2190eSArnaldo Carvalho de Melo 683a8c2190eSArnaldo Carvalho de Melo for (j = s_j; j < lopt->nr_table_entries; j++) { 684a8c2190eSArnaldo Carvalho de Melo struct request_sock *req, *head = lopt->syn_table[j]; 685a8c2190eSArnaldo Carvalho de Melo 686a8c2190eSArnaldo Carvalho de Melo reqnum = 0; 687a8c2190eSArnaldo Carvalho de Melo for (req = head; req; reqnum++, req = req->dl_next) { 688a8c2190eSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 689a8c2190eSArnaldo Carvalho de Melo 690a8c2190eSArnaldo Carvalho de Melo if (reqnum < s_reqnum) 691a8c2190eSArnaldo Carvalho de Melo continue; 692a8c2190eSArnaldo Carvalho de Melo if (r->id.idiag_dport != ireq->rmt_port && 693a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 694a8c2190eSArnaldo Carvalho de Melo continue; 695a8c2190eSArnaldo Carvalho de Melo 696a8c2190eSArnaldo Carvalho de Melo if (bc) { 697a8c2190eSArnaldo Carvalho de Melo entry.saddr = 698dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 699a8c2190eSArnaldo Carvalho de Melo (entry.family == AF_INET6) ? 700ca304b61SArnaldo Carvalho de Melo inet6_rsk(req)->loc_addr.s6_addr32 : 701a8c2190eSArnaldo Carvalho de Melo #endif 702a8c2190eSArnaldo Carvalho de Melo &ireq->loc_addr; 703a8c2190eSArnaldo Carvalho de Melo entry.daddr = 704dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 705a8c2190eSArnaldo Carvalho de Melo (entry.family == AF_INET6) ? 706ca304b61SArnaldo Carvalho de Melo inet6_rsk(req)->rmt_addr.s6_addr32 : 707a8c2190eSArnaldo Carvalho de Melo #endif 708a8c2190eSArnaldo Carvalho de Melo &ireq->rmt_addr; 709a8c2190eSArnaldo Carvalho de Melo entry.dport = ntohs(ireq->rmt_port); 710a8c2190eSArnaldo Carvalho de Melo 71187c22ea5SPavel Emelyanov if (!inet_diag_bc_run(bc, &entry)) 712a8c2190eSArnaldo Carvalho de Melo continue; 713a8c2190eSArnaldo Carvalho de Melo } 714a8c2190eSArnaldo Carvalho de Melo 715a8c2190eSArnaldo Carvalho de Melo err = inet_diag_fill_req(skb, sk, req, 716a8c2190eSArnaldo Carvalho de Melo NETLINK_CB(cb->skb).pid, 717a8c2190eSArnaldo Carvalho de Melo cb->nlh->nlmsg_seq, cb->nlh); 718a8c2190eSArnaldo Carvalho de Melo if (err < 0) { 719a8c2190eSArnaldo Carvalho de Melo cb->args[3] = j + 1; 720a8c2190eSArnaldo Carvalho de Melo cb->args[4] = reqnum; 721a8c2190eSArnaldo Carvalho de Melo goto out; 722a8c2190eSArnaldo Carvalho de Melo } 723a8c2190eSArnaldo Carvalho de Melo } 724a8c2190eSArnaldo Carvalho de Melo 725a8c2190eSArnaldo Carvalho de Melo s_reqnum = 0; 726a8c2190eSArnaldo Carvalho de Melo } 727a8c2190eSArnaldo Carvalho de Melo 728a8c2190eSArnaldo Carvalho de Melo out: 729a8c2190eSArnaldo Carvalho de Melo read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 730a8c2190eSArnaldo Carvalho de Melo 731a8c2190eSArnaldo Carvalho de Melo return err; 732a8c2190eSArnaldo Carvalho de Melo } 733a8c2190eSArnaldo Carvalho de Melo 7341942c518SPavel Emelyanov void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 735efb3cb42SPavel Emelyanov struct netlink_callback *cb, struct inet_diag_req *r, struct nlattr *bc) 736a8c2190eSArnaldo Carvalho de Melo { 737a8c2190eSArnaldo Carvalho de Melo int i, num; 738a8c2190eSArnaldo Carvalho de Melo int s_i, s_num; 739a8c2190eSArnaldo Carvalho de Melo 740a8c2190eSArnaldo Carvalho de Melo s_i = cb->args[1]; 741a8c2190eSArnaldo Carvalho de Melo s_num = num = cb->args[2]; 742a8c2190eSArnaldo Carvalho de Melo 743a8c2190eSArnaldo Carvalho de Melo if (cb->args[0] == 0) { 744a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 745a8c2190eSArnaldo Carvalho de Melo goto skip_listen_ht; 746a8c2190eSArnaldo Carvalho de Melo 747a8c2190eSArnaldo Carvalho de Melo for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 748a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 749c25eb3bfSEric Dumazet struct hlist_nulls_node *node; 7505caea4eaSEric Dumazet struct inet_listen_hashbucket *ilb; 751a8c2190eSArnaldo Carvalho de Melo 752a8c2190eSArnaldo Carvalho de Melo num = 0; 7535caea4eaSEric Dumazet ilb = &hashinfo->listening_hash[i]; 7545caea4eaSEric Dumazet spin_lock_bh(&ilb->lock); 755c25eb3bfSEric Dumazet sk_nulls_for_each(sk, node, &ilb->head) { 756a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 757a8c2190eSArnaldo Carvalho de Melo 758a8c2190eSArnaldo Carvalho de Melo if (num < s_num) { 759a8c2190eSArnaldo Carvalho de Melo num++; 760a8c2190eSArnaldo Carvalho de Melo continue; 761a8c2190eSArnaldo Carvalho de Melo } 762a8c2190eSArnaldo Carvalho de Melo 763d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 764d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 765d23deaa0SPavel Emelyanov goto next_listen; 766d23deaa0SPavel Emelyanov 767c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 768a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 769a8c2190eSArnaldo Carvalho de Melo goto next_listen; 770a8c2190eSArnaldo Carvalho de Melo 771a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_LISTEN) || 772a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport || 773a8c2190eSArnaldo Carvalho de Melo cb->args[3] > 0) 774a8c2190eSArnaldo Carvalho de Melo goto syn_recv; 775a8c2190eSArnaldo Carvalho de Melo 77625c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 7775caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 778a8c2190eSArnaldo Carvalho de Melo goto done; 779a8c2190eSArnaldo Carvalho de Melo } 780a8c2190eSArnaldo Carvalho de Melo 781a8c2190eSArnaldo Carvalho de Melo syn_recv: 782a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & TCPF_SYN_RECV)) 783a8c2190eSArnaldo Carvalho de Melo goto next_listen; 784a8c2190eSArnaldo Carvalho de Melo 78525c4cd2bSPavel Emelyanov if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { 7865caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 787a8c2190eSArnaldo Carvalho de Melo goto done; 788a8c2190eSArnaldo Carvalho de Melo } 789a8c2190eSArnaldo Carvalho de Melo 790a8c2190eSArnaldo Carvalho de Melo next_listen: 791a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 792a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 793a8c2190eSArnaldo Carvalho de Melo ++num; 794a8c2190eSArnaldo Carvalho de Melo } 7955caea4eaSEric Dumazet spin_unlock_bh(&ilb->lock); 796a8c2190eSArnaldo Carvalho de Melo 797a8c2190eSArnaldo Carvalho de Melo s_num = 0; 798a8c2190eSArnaldo Carvalho de Melo cb->args[3] = 0; 799a8c2190eSArnaldo Carvalho de Melo cb->args[4] = 0; 800a8c2190eSArnaldo Carvalho de Melo } 801a8c2190eSArnaldo Carvalho de Melo skip_listen_ht: 802a8c2190eSArnaldo Carvalho de Melo cb->args[0] = 1; 803a8c2190eSArnaldo Carvalho de Melo s_i = num = s_num = 0; 804a8c2190eSArnaldo Carvalho de Melo } 805a8c2190eSArnaldo Carvalho de Melo 806a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 807efb3cb42SPavel Emelyanov goto out; 808a8c2190eSArnaldo Carvalho de Melo 809f373b53bSEric Dumazet for (i = s_i; i <= hashinfo->ehash_mask; i++) { 810a8c2190eSArnaldo Carvalho de Melo struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 8117e3aab4aSDavid S. Miller spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 812a8c2190eSArnaldo Carvalho de Melo struct sock *sk; 8133ab5aee7SEric Dumazet struct hlist_nulls_node *node; 814a8c2190eSArnaldo Carvalho de Melo 8156be547a6SAndi Kleen num = 0; 8166be547a6SAndi Kleen 8173ab5aee7SEric Dumazet if (hlist_nulls_empty(&head->chain) && 8183ab5aee7SEric Dumazet hlist_nulls_empty(&head->twchain)) 8196be547a6SAndi Kleen continue; 8206be547a6SAndi Kleen 821a8c2190eSArnaldo Carvalho de Melo if (i > s_i) 822a8c2190eSArnaldo Carvalho de Melo s_num = 0; 823a8c2190eSArnaldo Carvalho de Melo 8247e3aab4aSDavid S. Miller spin_lock_bh(lock); 8253ab5aee7SEric Dumazet sk_nulls_for_each(sk, node, &head->chain) { 826a8c2190eSArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 827a8c2190eSArnaldo Carvalho de Melo 828a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 829a8c2190eSArnaldo Carvalho de Melo goto next_normal; 830a8c2190eSArnaldo Carvalho de Melo if (!(r->idiag_states & (1 << sk->sk_state))) 831a8c2190eSArnaldo Carvalho de Melo goto next_normal; 832d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 833d23deaa0SPavel Emelyanov sk->sk_family != r->sdiag_family) 834d23deaa0SPavel Emelyanov goto next_normal; 835c720c7e8SEric Dumazet if (r->id.idiag_sport != inet->inet_sport && 836a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 837a8c2190eSArnaldo Carvalho de Melo goto next_normal; 838c720c7e8SEric Dumazet if (r->id.idiag_dport != inet->inet_dport && 8394e852c02SArnaldo Carvalho de Melo r->id.idiag_dport) 840a8c2190eSArnaldo Carvalho de Melo goto next_normal; 84125c4cd2bSPavel Emelyanov if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 8427e3aab4aSDavid S. Miller spin_unlock_bh(lock); 843a8c2190eSArnaldo Carvalho de Melo goto done; 844a8c2190eSArnaldo Carvalho de Melo } 845a8c2190eSArnaldo Carvalho de Melo next_normal: 846a8c2190eSArnaldo Carvalho de Melo ++num; 847a8c2190eSArnaldo Carvalho de Melo } 848a8c2190eSArnaldo Carvalho de Melo 849a8c2190eSArnaldo Carvalho de Melo if (r->idiag_states & TCPF_TIME_WAIT) { 850c7d58aabSArnaldo Carvalho de Melo struct inet_timewait_sock *tw; 851c7d58aabSArnaldo Carvalho de Melo 852c7d58aabSArnaldo Carvalho de Melo inet_twsk_for_each(tw, node, 853dbca9b27SEric Dumazet &head->twchain) { 854a8c2190eSArnaldo Carvalho de Melo 855a8c2190eSArnaldo Carvalho de Melo if (num < s_num) 856a8c2190eSArnaldo Carvalho de Melo goto next_dying; 857d23deaa0SPavel Emelyanov if (r->sdiag_family != AF_UNSPEC && 858d23deaa0SPavel Emelyanov tw->tw_family != r->sdiag_family) 859d23deaa0SPavel Emelyanov goto next_dying; 8607dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_sport != tw->tw_sport && 861a8c2190eSArnaldo Carvalho de Melo r->id.idiag_sport) 862a8c2190eSArnaldo Carvalho de Melo goto next_dying; 8637dbf0755SArnaldo Carvalho de Melo if (r->id.idiag_dport != tw->tw_dport && 864a8c2190eSArnaldo Carvalho de Melo r->id.idiag_dport) 865a8c2190eSArnaldo Carvalho de Melo goto next_dying; 86625c4cd2bSPavel Emelyanov if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) { 8677e3aab4aSDavid S. Miller spin_unlock_bh(lock); 868a8c2190eSArnaldo Carvalho de Melo goto done; 869a8c2190eSArnaldo Carvalho de Melo } 870a8c2190eSArnaldo Carvalho de Melo next_dying: 871a8c2190eSArnaldo Carvalho de Melo ++num; 872a8c2190eSArnaldo Carvalho de Melo } 873a8c2190eSArnaldo Carvalho de Melo } 8747e3aab4aSDavid S. Miller spin_unlock_bh(lock); 875a8c2190eSArnaldo Carvalho de Melo } 876a8c2190eSArnaldo Carvalho de Melo 877a8c2190eSArnaldo Carvalho de Melo done: 878a8c2190eSArnaldo Carvalho de Melo cb->args[1] = i; 879a8c2190eSArnaldo Carvalho de Melo cb->args[2] = num; 880efb3cb42SPavel Emelyanov out: 881efb3cb42SPavel Emelyanov ; 882efb3cb42SPavel Emelyanov } 8831942c518SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 884efb3cb42SPavel Emelyanov 885efb3cb42SPavel Emelyanov static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 886efb3cb42SPavel Emelyanov struct inet_diag_req *r, struct nlattr *bc) 887efb3cb42SPavel Emelyanov { 888efb3cb42SPavel Emelyanov const struct inet_diag_handler *handler; 889efb3cb42SPavel Emelyanov 890efb3cb42SPavel Emelyanov handler = inet_diag_lock_handler(r->sdiag_protocol); 891efb3cb42SPavel Emelyanov if (!IS_ERR(handler)) 8921942c518SPavel Emelyanov handler->dump(skb, cb, r, bc); 893d523a328SHerbert Xu inet_diag_unlock_handler(handler); 894efb3cb42SPavel Emelyanov 895a8c2190eSArnaldo Carvalho de Melo return skb->len; 896a8c2190eSArnaldo Carvalho de Melo } 897a8c2190eSArnaldo Carvalho de Melo 89825c4cd2bSPavel Emelyanov static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 89925c4cd2bSPavel Emelyanov { 90025c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 90125c4cd2bSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 90225c4cd2bSPavel Emelyanov 90325c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 90425c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 90525c4cd2bSPavel Emelyanov 90625c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc); 90725c4cd2bSPavel Emelyanov } 90825c4cd2bSPavel Emelyanov 909a029fe26SPavel Emelyanov static inline int inet_diag_type2proto(int type) 910a029fe26SPavel Emelyanov { 911a029fe26SPavel Emelyanov switch (type) { 912a029fe26SPavel Emelyanov case TCPDIAG_GETSOCK: 913a029fe26SPavel Emelyanov return IPPROTO_TCP; 914a029fe26SPavel Emelyanov case DCCPDIAG_GETSOCK: 915a029fe26SPavel Emelyanov return IPPROTO_DCCP; 916a029fe26SPavel Emelyanov default: 917a029fe26SPavel Emelyanov return 0; 918a029fe26SPavel Emelyanov } 919a029fe26SPavel Emelyanov } 920a029fe26SPavel Emelyanov 92125c4cd2bSPavel Emelyanov static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb) 92225c4cd2bSPavel Emelyanov { 92325c4cd2bSPavel Emelyanov struct inet_diag_req_compat *rc = NLMSG_DATA(cb->nlh); 92425c4cd2bSPavel Emelyanov struct inet_diag_req req; 92525c4cd2bSPavel Emelyanov struct nlattr *bc = NULL; 92625c4cd2bSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_compat); 92725c4cd2bSPavel Emelyanov 928d23deaa0SPavel Emelyanov req.sdiag_family = AF_UNSPEC; /* compatibility */ 92925c4cd2bSPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 93025c4cd2bSPavel Emelyanov req.idiag_ext = rc->idiag_ext; 93125c4cd2bSPavel Emelyanov req.idiag_states = rc->idiag_states; 93225c4cd2bSPavel Emelyanov req.id = rc->id; 93325c4cd2bSPavel Emelyanov 93425c4cd2bSPavel Emelyanov if (nlmsg_attrlen(cb->nlh, hdrlen)) 93525c4cd2bSPavel Emelyanov bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 93625c4cd2bSPavel Emelyanov 93725c4cd2bSPavel Emelyanov return __inet_diag_dump(skb, cb, &req, bc); 93825c4cd2bSPavel Emelyanov } 93925c4cd2bSPavel Emelyanov 940fe50ce28SPavel Emelyanov static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 941fe50ce28SPavel Emelyanov const struct nlmsghdr *nlh) 942fe50ce28SPavel Emelyanov { 943fe50ce28SPavel Emelyanov struct inet_diag_req_compat *rc = NLMSG_DATA(nlh); 944fe50ce28SPavel Emelyanov struct inet_diag_req req; 945fe50ce28SPavel Emelyanov 946fe50ce28SPavel Emelyanov req.sdiag_family = rc->idiag_family; 947fe50ce28SPavel Emelyanov req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 948fe50ce28SPavel Emelyanov req.idiag_ext = rc->idiag_ext; 949fe50ce28SPavel Emelyanov req.idiag_states = rc->idiag_states; 950fe50ce28SPavel Emelyanov req.id = rc->id; 951fe50ce28SPavel Emelyanov 952fe50ce28SPavel Emelyanov return inet_diag_get_exact(in_skb, nlh, &req); 953fe50ce28SPavel Emelyanov } 954fe50ce28SPavel Emelyanov 9558d34172dSPavel Emelyanov static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 956a8c2190eSArnaldo Carvalho de Melo { 957126fdc32SPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req_compat); 958a8c2190eSArnaldo Carvalho de Melo 959ead592baSThomas Graf if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 960ead592baSThomas Graf nlmsg_len(nlh) < hdrlen) 961ead592baSThomas Graf return -EINVAL; 962a8c2190eSArnaldo Carvalho de Melo 963b8f3ab42SDavid S. Miller if (nlh->nlmsg_flags & NLM_F_DUMP) { 964ead592baSThomas Graf if (nlmsg_attrlen(nlh, hdrlen)) { 965ead592baSThomas Graf struct nlattr *attr; 966ead592baSThomas Graf 967ead592baSThomas Graf attr = nlmsg_find_attr(nlh, hdrlen, 968ead592baSThomas Graf INET_DIAG_REQ_BYTECODE); 969ead592baSThomas Graf if (attr == NULL || 970ead592baSThomas Graf nla_len(attr) < sizeof(struct inet_diag_bc_op) || 971ead592baSThomas Graf inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 972a8c2190eSArnaldo Carvalho de Melo return -EINVAL; 973a8c2190eSArnaldo Carvalho de Melo } 974a8c2190eSArnaldo Carvalho de Melo 9758ef874bfSPavel Emelyanov return netlink_dump_start(sock_diag_nlsk, skb, nlh, 97625c4cd2bSPavel Emelyanov inet_diag_dump_compat, NULL, 0); 977a8c2190eSArnaldo Carvalho de Melo } 978ead592baSThomas Graf 979fe50ce28SPavel Emelyanov return inet_diag_get_exact_compat(skb, nlh); 980a8c2190eSArnaldo Carvalho de Melo } 981a8c2190eSArnaldo Carvalho de Melo 982d366477aSPavel Emelyanov static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 983d366477aSPavel Emelyanov { 984d366477aSPavel Emelyanov int hdrlen = sizeof(struct inet_diag_req); 985d366477aSPavel Emelyanov 986d366477aSPavel Emelyanov if (nlmsg_len(h) < hdrlen) 987d366477aSPavel Emelyanov return -EINVAL; 988d366477aSPavel Emelyanov 989d366477aSPavel Emelyanov if (h->nlmsg_flags & NLM_F_DUMP) { 99025c4cd2bSPavel Emelyanov if (nlmsg_attrlen(h, hdrlen)) { 99125c4cd2bSPavel Emelyanov struct nlattr *attr; 99225c4cd2bSPavel Emelyanov attr = nlmsg_find_attr(h, hdrlen, 99325c4cd2bSPavel Emelyanov INET_DIAG_REQ_BYTECODE); 99425c4cd2bSPavel Emelyanov if (attr == NULL || 99525c4cd2bSPavel Emelyanov nla_len(attr) < sizeof(struct inet_diag_bc_op) || 99625c4cd2bSPavel Emelyanov inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 99725c4cd2bSPavel Emelyanov return -EINVAL; 99825c4cd2bSPavel Emelyanov } 99925c4cd2bSPavel Emelyanov 10008ef874bfSPavel Emelyanov return netlink_dump_start(sock_diag_nlsk, skb, h, 100125c4cd2bSPavel Emelyanov inet_diag_dump, NULL, 0); 1002d366477aSPavel Emelyanov } 1003d366477aSPavel Emelyanov 1004fe50ce28SPavel Emelyanov return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h)); 1005d366477aSPavel Emelyanov } 1006d366477aSPavel Emelyanov 1007d366477aSPavel Emelyanov static struct sock_diag_handler inet_diag_handler = { 1008d366477aSPavel Emelyanov .family = AF_INET, 1009d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 1010d366477aSPavel Emelyanov }; 1011d366477aSPavel Emelyanov 1012d366477aSPavel Emelyanov static struct sock_diag_handler inet6_diag_handler = { 1013d366477aSPavel Emelyanov .family = AF_INET6, 1014d366477aSPavel Emelyanov .dump = inet_diag_handler_dump, 1015d366477aSPavel Emelyanov }; 1016d366477aSPavel Emelyanov 1017a8c2190eSArnaldo Carvalho de Melo int inet_diag_register(const struct inet_diag_handler *h) 1018a8c2190eSArnaldo Carvalho de Melo { 1019a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1020a8c2190eSArnaldo Carvalho de Melo int err = -EINVAL; 1021a8c2190eSArnaldo Carvalho de Melo 1022f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1023a8c2190eSArnaldo Carvalho de Melo goto out; 1024a8c2190eSArnaldo Carvalho de Melo 1025d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1026a8c2190eSArnaldo Carvalho de Melo err = -EEXIST; 1027a8c2190eSArnaldo Carvalho de Melo if (inet_diag_table[type] == NULL) { 1028a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = h; 1029a8c2190eSArnaldo Carvalho de Melo err = 0; 1030a8c2190eSArnaldo Carvalho de Melo } 1031d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1032a8c2190eSArnaldo Carvalho de Melo out: 1033a8c2190eSArnaldo Carvalho de Melo return err; 1034a8c2190eSArnaldo Carvalho de Melo } 1035a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_register); 1036a8c2190eSArnaldo Carvalho de Melo 1037a8c2190eSArnaldo Carvalho de Melo void inet_diag_unregister(const struct inet_diag_handler *h) 1038a8c2190eSArnaldo Carvalho de Melo { 1039a8c2190eSArnaldo Carvalho de Melo const __u16 type = h->idiag_type; 1040a8c2190eSArnaldo Carvalho de Melo 1041f13c95f0SPavel Emelyanov if (type >= IPPROTO_MAX) 1042a8c2190eSArnaldo Carvalho de Melo return; 1043a8c2190eSArnaldo Carvalho de Melo 1044d523a328SHerbert Xu mutex_lock(&inet_diag_table_mutex); 1045a8c2190eSArnaldo Carvalho de Melo inet_diag_table[type] = NULL; 1046d523a328SHerbert Xu mutex_unlock(&inet_diag_table_mutex); 1047a8c2190eSArnaldo Carvalho de Melo } 1048a8c2190eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_diag_unregister); 1049a8c2190eSArnaldo Carvalho de Melo 1050a8c2190eSArnaldo Carvalho de Melo static int __init inet_diag_init(void) 1051a8c2190eSArnaldo Carvalho de Melo { 1052f13c95f0SPavel Emelyanov const int inet_diag_table_size = (IPPROTO_MAX * 1053a8c2190eSArnaldo Carvalho de Melo sizeof(struct inet_diag_handler *)); 1054a8c2190eSArnaldo Carvalho de Melo int err = -ENOMEM; 1055a8c2190eSArnaldo Carvalho de Melo 10560da974f4SPanagiotis Issaris inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1057a8c2190eSArnaldo Carvalho de Melo if (!inet_diag_table) 1058a8c2190eSArnaldo Carvalho de Melo goto out; 1059a8c2190eSArnaldo Carvalho de Melo 1060d366477aSPavel Emelyanov err = sock_diag_register(&inet_diag_handler); 1061d366477aSPavel Emelyanov if (err) 1062d366477aSPavel Emelyanov goto out_free_nl; 1063d366477aSPavel Emelyanov 1064d366477aSPavel Emelyanov err = sock_diag_register(&inet6_diag_handler); 1065d366477aSPavel Emelyanov if (err) 1066d366477aSPavel Emelyanov goto out_free_inet; 1067d366477aSPavel Emelyanov 10688ef874bfSPavel Emelyanov sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1069a8c2190eSArnaldo Carvalho de Melo out: 1070a8c2190eSArnaldo Carvalho de Melo return err; 1071d366477aSPavel Emelyanov 1072d366477aSPavel Emelyanov out_free_inet: 1073d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 1074d366477aSPavel Emelyanov out_free_nl: 1075a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1076a8c2190eSArnaldo Carvalho de Melo goto out; 1077a8c2190eSArnaldo Carvalho de Melo } 1078a8c2190eSArnaldo Carvalho de Melo 1079a8c2190eSArnaldo Carvalho de Melo static void __exit inet_diag_exit(void) 1080a8c2190eSArnaldo Carvalho de Melo { 1081d366477aSPavel Emelyanov sock_diag_unregister(&inet6_diag_handler); 1082d366477aSPavel Emelyanov sock_diag_unregister(&inet_diag_handler); 10838ef874bfSPavel Emelyanov sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1084a8c2190eSArnaldo Carvalho de Melo kfree(inet_diag_table); 1085a8c2190eSArnaldo Carvalho de Melo } 1086a8c2190eSArnaldo Carvalho de Melo 1087a8c2190eSArnaldo Carvalho de Melo module_init(inet_diag_init); 1088a8c2190eSArnaldo Carvalho de Melo module_exit(inet_diag_exit); 1089a8c2190eSArnaldo Carvalho de Melo MODULE_LICENSE("GPL"); 1090*aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1091*aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1092