Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 2 | * inet_diag.c Module for monitoring INET transport protocols sockets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/fcntl.h> |
| 16 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/cache.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/time.h> |
| 21 | |
| 22 | #include <net/icmp.h> |
| 23 | #include <net/tcp.h> |
| 24 | #include <net/ipv6.h> |
| 25 | #include <net/inet_common.h> |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 26 | #include <net/inet_connection_sock.h> |
| 27 | #include <net/inet_hashtables.h> |
| 28 | #include <net/inet_timewait_sock.h> |
| 29 | #include <net/inet6_hashtables.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 30 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <linux/inet.h> |
| 33 | #include <linux/stddef.h> |
| 34 | |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 35 | #include <linux/inet_diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 37 | static const struct inet_diag_handler **inet_diag_table; |
| 38 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 39 | struct inet_diag_entry { |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 40 | __be32 *saddr; |
| 41 | __be32 *daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | u16 sport; |
| 43 | u16 dport; |
| 44 | u16 family; |
| 45 | u16 userlocks; |
| 46 | }; |
| 47 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 48 | static struct sock *sdiagnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 50 | #define INET_DIAG_PUT(skb, attrtype, attrlen) \ |
Stephen Hemminger | 7c99c90 | 2005-06-23 12:20:36 -0700 | [diff] [blame] | 51 | RTA_DATA(__RTA_PUT(skb, attrtype, attrlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 53 | static inline int inet_diag_type2proto(int type) |
| 54 | { |
| 55 | switch (type) { |
| 56 | case TCPDIAG_GETSOCK: |
| 57 | return IPPROTO_TCP; |
| 58 | case DCCPDIAG_GETSOCK: |
| 59 | return IPPROTO_DCCP; |
| 60 | default: |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 65 | static DEFINE_MUTEX(inet_diag_table_mutex); |
| 66 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 67 | static const struct inet_diag_handler *inet_diag_lock_handler(int proto) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 68 | { |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 69 | if (!inet_diag_table[proto]) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 70 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 71 | NETLINK_SOCK_DIAG, proto); |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 72 | |
| 73 | mutex_lock(&inet_diag_table_mutex); |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 74 | if (!inet_diag_table[proto]) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 75 | return ERR_PTR(-ENOENT); |
| 76 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 77 | return inet_diag_table[proto]; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline void inet_diag_unlock_handler( |
| 81 | const struct inet_diag_handler *handler) |
| 82 | { |
| 83 | mutex_unlock(&inet_diag_table_mutex); |
| 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 86 | static int inet_csk_diag_fill(struct sock *sk, |
| 87 | struct sk_buff *skb, |
| 88 | int ext, u32 pid, u32 seq, u16 nlmsg_flags, |
| 89 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 91 | const struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 92 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 93 | struct inet_diag_msg *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 95 | void *info = NULL; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 96 | struct inet_diag_meminfo *minfo = NULL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 97 | unsigned char *b = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 98 | const struct inet_diag_handler *handler; |
| 99 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 100 | handler = inet_diag_table[inet_diag_type2proto(unlh->nlmsg_type)]; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 101 | BUG_ON(handler == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 103 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | nlh->nlmsg_flags = nlmsg_flags; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | r = NLMSG_DATA(nlh); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 107 | BUG_ON(sk->sk_state == TCP_TIME_WAIT); |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 108 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 109 | if (ext & (1 << (INET_DIAG_MEMINFO - 1))) |
| 110 | minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo)); |
| 111 | |
| 112 | if (ext & (1 << (INET_DIAG_INFO - 1))) |
| 113 | info = INET_DIAG_PUT(skb, INET_DIAG_INFO, |
| 114 | handler->idiag_info_size); |
| 115 | |
| 116 | if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) { |
| 117 | const size_t len = strlen(icsk->icsk_ca_ops->name); |
| 118 | |
| 119 | strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1), |
| 120 | icsk->icsk_ca_ops->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 122 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 123 | r->idiag_family = sk->sk_family; |
| 124 | r->idiag_state = sk->sk_state; |
| 125 | r->idiag_timer = 0; |
| 126 | r->idiag_retrans = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 128 | r->id.idiag_if = sk->sk_bound_dev_if; |
| 129 | r->id.idiag_cookie[0] = (u32)(unsigned long)sk; |
| 130 | r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 132 | r->id.idiag_sport = inet->inet_sport; |
| 133 | r->id.idiag_dport = inet->inet_dport; |
| 134 | r->id.idiag_src[0] = inet->inet_rcv_saddr; |
| 135 | r->id.idiag_dst[0] = inet->inet_daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Maciej Żenczykowski | 717b6d8 | 2011-11-22 16:03:10 -0500 | [diff] [blame] | 137 | /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, |
| 138 | * hence this needs to be included regardless of socket family. |
| 139 | */ |
| 140 | if (ext & (1 << (INET_DIAG_TOS - 1))) |
| 141 | RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos); |
| 142 | |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 143 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 144 | if (r->idiag_family == AF_INET6) { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 145 | const struct ipv6_pinfo *np = inet6_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 147 | *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; |
| 148 | *(struct in6_addr *)r->id.idiag_dst = np->daddr; |
Maciej Żenczykowski | 06236ac | 2011-11-07 14:23:11 +0000 | [diff] [blame] | 149 | if (ext & (1 << (INET_DIAG_TCLASS - 1))) |
| 150 | RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | #endif |
| 153 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 154 | #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 156 | if (icsk->icsk_pending == ICSK_TIME_RETRANS) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 157 | r->idiag_timer = 1; |
| 158 | r->idiag_retrans = icsk->icsk_retransmits; |
| 159 | r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 160 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 161 | r->idiag_timer = 4; |
| 162 | r->idiag_retrans = icsk->icsk_probes_out; |
| 163 | r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } else if (timer_pending(&sk->sk_timer)) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 165 | r->idiag_timer = 2; |
| 166 | r->idiag_retrans = icsk->icsk_probes_out; |
| 167 | r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } else { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 169 | r->idiag_timer = 0; |
| 170 | r->idiag_expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | #undef EXPIRES_IN_MS |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 173 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 174 | r->idiag_uid = sock_i_uid(sk); |
| 175 | r->idiag_inode = sock_i_ino(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | if (minfo) { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 178 | minfo->idiag_rmem = sk_rmem_alloc_get(sk); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 179 | minfo->idiag_wmem = sk->sk_wmem_queued; |
| 180 | minfo->idiag_fmem = sk->sk_forward_alloc; |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 181 | minfo->idiag_tmem = sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 184 | handler->idiag_get_info(sk, r, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 186 | if (sk->sk_state < TCP_TIME_WAIT && |
| 187 | icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) |
| 188 | icsk->icsk_ca_ops->get_info(sk, ext, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 190 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return skb->len; |
| 192 | |
Stephen Hemminger | 7c99c90 | 2005-06-23 12:20:36 -0700 | [diff] [blame] | 193 | rtattr_failure: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 195 | nlmsg_trim(skb, b); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 196 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 199 | static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, |
| 200 | struct sk_buff *skb, int ext, u32 pid, |
| 201 | u32 seq, u16 nlmsg_flags, |
| 202 | const struct nlmsghdr *unlh) |
| 203 | { |
| 204 | long tmo; |
| 205 | struct inet_diag_msg *r; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 206 | const unsigned char *previous_tail = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 207 | struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, |
| 208 | unlh->nlmsg_type, sizeof(*r)); |
| 209 | |
| 210 | r = NLMSG_DATA(nlh); |
| 211 | BUG_ON(tw->tw_state != TCP_TIME_WAIT); |
| 212 | |
| 213 | nlh->nlmsg_flags = nlmsg_flags; |
| 214 | |
| 215 | tmo = tw->tw_ttd - jiffies; |
| 216 | if (tmo < 0) |
| 217 | tmo = 0; |
| 218 | |
| 219 | r->idiag_family = tw->tw_family; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 220 | r->idiag_retrans = 0; |
| 221 | r->id.idiag_if = tw->tw_bound_dev_if; |
| 222 | r->id.idiag_cookie[0] = (u32)(unsigned long)tw; |
| 223 | r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1); |
| 224 | r->id.idiag_sport = tw->tw_sport; |
| 225 | r->id.idiag_dport = tw->tw_dport; |
| 226 | r->id.idiag_src[0] = tw->tw_rcv_saddr; |
| 227 | r->id.idiag_dst[0] = tw->tw_daddr; |
| 228 | r->idiag_state = tw->tw_substate; |
| 229 | r->idiag_timer = 3; |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 230 | r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 231 | r->idiag_rqueue = 0; |
| 232 | r->idiag_wqueue = 0; |
| 233 | r->idiag_uid = 0; |
| 234 | r->idiag_inode = 0; |
| 235 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 236 | if (tw->tw_family == AF_INET6) { |
| 237 | const struct inet6_timewait_sock *tw6 = |
| 238 | inet6_twsk((struct sock *)tw); |
| 239 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 240 | *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr; |
| 241 | *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 242 | } |
| 243 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 244 | nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 245 | return skb->len; |
| 246 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 247 | nlmsg_trim(skb, previous_tail); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 248 | return -EMSGSIZE; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 251 | static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, |
| 252 | int ext, u32 pid, u32 seq, u16 nlmsg_flags, |
| 253 | const struct nlmsghdr *unlh) |
| 254 | { |
| 255 | if (sk->sk_state == TCP_TIME_WAIT) |
| 256 | return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, |
| 257 | skb, ext, pid, seq, nlmsg_flags, |
| 258 | unlh); |
| 259 | return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh); |
| 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 262 | static int inet_diag_get_exact(struct sk_buff *in_skb, |
| 263 | const struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | int err; |
| 266 | struct sock *sk; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 267 | struct inet_diag_req *req = NLMSG_DATA(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | struct sk_buff *rep; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 269 | struct inet_hashinfo *hashinfo; |
| 270 | const struct inet_diag_handler *handler; |
| 271 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 272 | handler = inet_diag_lock_handler(inet_diag_type2proto(nlh->nlmsg_type)); |
Arnaldo Carvalho de Melo | 8cf8e5a | 2008-01-28 20:52:12 -0800 | [diff] [blame] | 273 | if (IS_ERR(handler)) { |
| 274 | err = PTR_ERR(handler); |
| 275 | goto unlock; |
| 276 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 277 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 278 | hashinfo = handler->idiag_hashinfo; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 279 | err = -EINVAL; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 280 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 281 | if (req->idiag_family == AF_INET) { |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 282 | sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 283 | req->id.idiag_dport, req->id.idiag_src[0], |
| 284 | req->id.idiag_sport, req->id.idiag_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 286 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 287 | else if (req->idiag_family == AF_INET6) { |
Pavel Emelyanov | d86e0da | 2008-01-31 05:07:21 -0800 | [diff] [blame] | 288 | sk = inet6_lookup(&init_net, hashinfo, |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 289 | (struct in6_addr *)req->id.idiag_dst, |
| 290 | req->id.idiag_dport, |
| 291 | (struct in6_addr *)req->id.idiag_src, |
| 292 | req->id.idiag_sport, |
| 293 | req->id.idiag_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | #endif |
| 296 | else { |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 297 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 300 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (sk == NULL) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 302 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | err = -ESTALE; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 305 | if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE || |
| 306 | req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) && |
| 307 | ((u32)(unsigned long)sk != req->id.idiag_cookie[0] || |
| 308 | (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | goto out; |
| 310 | |
| 311 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 312 | rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + |
| 313 | sizeof(struct inet_diag_meminfo) + |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 314 | handler->idiag_info_size + 64)), |
| 315 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (!rep) |
| 317 | goto out; |
| 318 | |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 319 | err = sk_diag_fill(sk, rep, req->idiag_ext, |
| 320 | NETLINK_CB(in_skb).pid, |
| 321 | nlh->nlmsg_seq, 0, nlh); |
| 322 | if (err < 0) { |
| 323 | WARN_ON(err == -EMSGSIZE); |
| 324 | kfree_skb(rep); |
| 325 | goto out; |
| 326 | } |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 327 | err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid, |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 328 | MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (err > 0) |
| 330 | err = 0; |
| 331 | |
| 332 | out: |
| 333 | if (sk) { |
| 334 | if (sk->sk_state == TCP_TIME_WAIT) |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 335 | inet_twsk_put((struct inet_timewait_sock *)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | else |
| 337 | sock_put(sk); |
| 338 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 339 | unlock: |
| 340 | inet_diag_unlock_handler(handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return err; |
| 342 | } |
| 343 | |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 344 | static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | int words = bits >> 5; |
| 347 | |
| 348 | bits &= 0x1f; |
| 349 | |
| 350 | if (words) { |
| 351 | if (memcmp(a1, a2, words << 2)) |
| 352 | return 0; |
| 353 | } |
| 354 | if (bits) { |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 355 | __be32 w1, w2; |
| 356 | __be32 mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | w1 = a1[words]; |
| 359 | w2 = a2[words]; |
| 360 | |
| 361 | mask = htonl((0xffffffff) << (32 - bits)); |
| 362 | |
| 363 | if ((w1 ^ w2) & mask) |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | return 1; |
| 368 | } |
| 369 | |
| 370 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 371 | static int inet_diag_bc_run(const void *bc, int len, |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 372 | const struct inet_diag_entry *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | while (len > 0) { |
| 375 | int yes = 1; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 376 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | switch (op->code) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 379 | case INET_DIAG_BC_NOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 381 | case INET_DIAG_BC_JMP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | yes = 0; |
| 383 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 384 | case INET_DIAG_BC_S_GE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | yes = entry->sport >= op[1].no; |
| 386 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 387 | case INET_DIAG_BC_S_LE: |
Roel Kluin | b4ced2b | 2010-01-19 14:12:20 -0800 | [diff] [blame] | 388 | yes = entry->sport <= op[1].no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 390 | case INET_DIAG_BC_D_GE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | yes = entry->dport >= op[1].no; |
| 392 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 393 | case INET_DIAG_BC_D_LE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | yes = entry->dport <= op[1].no; |
| 395 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 396 | case INET_DIAG_BC_AUTO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); |
| 398 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 399 | case INET_DIAG_BC_S_COND: |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 400 | case INET_DIAG_BC_D_COND: { |
| 401 | struct inet_diag_hostcond *cond; |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 402 | __be32 *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 404 | cond = (struct inet_diag_hostcond *)(op + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (cond->port != -1 && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 406 | cond->port != (op->code == INET_DIAG_BC_S_COND ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | entry->sport : entry->dport)) { |
| 408 | yes = 0; |
| 409 | break; |
| 410 | } |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (cond->prefix_len == 0) |
| 413 | break; |
| 414 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 415 | if (op->code == INET_DIAG_BC_S_COND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | addr = entry->saddr; |
| 417 | else |
| 418 | addr = entry->daddr; |
| 419 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 420 | if (bitstring_match(addr, cond->addr, |
| 421 | cond->prefix_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | break; |
| 423 | if (entry->family == AF_INET6 && |
| 424 | cond->family == AF_INET) { |
| 425 | if (addr[0] == 0 && addr[1] == 0 && |
| 426 | addr[2] == htonl(0xffff) && |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 427 | bitstring_match(addr + 3, cond->addr, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 428 | cond->prefix_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | yes = 0; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 436 | if (yes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | len -= op->yes; |
| 438 | bc += op->yes; |
| 439 | } else { |
| 440 | len -= op->no; |
| 441 | bc += op->no; |
| 442 | } |
| 443 | } |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 444 | return len == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static int valid_cc(const void *bc, int len, int cc) |
| 448 | { |
| 449 | while (len >= 0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 450 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | if (cc > len) |
| 453 | return 0; |
| 454 | if (cc == len) |
| 455 | return 1; |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 456 | if (op->yes < 4 || op->yes & 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return 0; |
| 458 | len -= op->yes; |
| 459 | bc += op->yes; |
| 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 464 | static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 466 | const void *bc = bytecode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | int len = bytecode_len; |
| 468 | |
| 469 | while (len > 0) { |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 470 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
| 472 | //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); |
| 473 | switch (op->code) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 474 | case INET_DIAG_BC_AUTO: |
| 475 | case INET_DIAG_BC_S_COND: |
| 476 | case INET_DIAG_BC_D_COND: |
| 477 | case INET_DIAG_BC_S_GE: |
| 478 | case INET_DIAG_BC_S_LE: |
| 479 | case INET_DIAG_BC_D_GE: |
| 480 | case INET_DIAG_BC_D_LE: |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 481 | case INET_DIAG_BC_JMP: |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 482 | if (op->no < 4 || op->no > len + 4 || op->no & 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return -EINVAL; |
| 484 | if (op->no < len && |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 485 | !valid_cc(bytecode, bytecode_len, len - op->no)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return -EINVAL; |
| 487 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 488 | case INET_DIAG_BC_NOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | break; |
| 490 | default: |
| 491 | return -EINVAL; |
| 492 | } |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 493 | if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) |
| 494 | return -EINVAL; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 495 | bc += op->yes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | len -= op->yes; |
| 497 | } |
| 498 | return len == 0 ? 0 : -EINVAL; |
| 499 | } |
| 500 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 501 | static int inet_csk_diag_dump(struct sock *sk, |
| 502 | struct sk_buff *skb, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 503 | struct netlink_callback *cb, |
| 504 | const struct nlattr *bc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 506 | struct inet_diag_req *r = NLMSG_DATA(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 508 | if (bc != NULL) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 509 | struct inet_diag_entry entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | struct inet_sock *inet = inet_sk(sk); |
| 511 | |
| 512 | entry.family = sk->sk_family; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 513 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | if (entry.family == AF_INET6) { |
| 515 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 516 | |
| 517 | entry.saddr = np->rcv_saddr.s6_addr32; |
| 518 | entry.daddr = np->daddr.s6_addr32; |
| 519 | } else |
| 520 | #endif |
| 521 | { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 522 | entry.saddr = &inet->inet_rcv_saddr; |
| 523 | entry.daddr = &inet->inet_daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 525 | entry.sport = inet->inet_num; |
| 526 | entry.dport = ntohs(inet->inet_dport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | entry.userlocks = sk->sk_userlocks; |
| 528 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 529 | if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 533 | return inet_csk_diag_fill(sk, skb, r->idiag_ext, |
| 534 | NETLINK_CB(cb->skb).pid, |
| 535 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 538 | static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, |
| 539 | struct sk_buff *skb, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 540 | struct netlink_callback *cb, |
| 541 | const struct nlattr *bc) |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 542 | { |
| 543 | struct inet_diag_req *r = NLMSG_DATA(cb->nlh); |
| 544 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 545 | if (bc != NULL) { |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 546 | struct inet_diag_entry entry; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 547 | |
| 548 | entry.family = tw->tw_family; |
| 549 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 550 | if (tw->tw_family == AF_INET6) { |
| 551 | struct inet6_timewait_sock *tw6 = |
| 552 | inet6_twsk((struct sock *)tw); |
| 553 | entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32; |
| 554 | entry.daddr = tw6->tw_v6_daddr.s6_addr32; |
| 555 | } else |
| 556 | #endif |
| 557 | { |
| 558 | entry.saddr = &tw->tw_rcv_saddr; |
| 559 | entry.daddr = &tw->tw_daddr; |
| 560 | } |
| 561 | entry.sport = tw->tw_num; |
| 562 | entry.dport = ntohs(tw->tw_dport); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 563 | entry.userlocks = 0; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 564 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 565 | if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | return inet_twsk_diag_fill(tw, skb, r->idiag_ext, |
| 570 | NETLINK_CB(cb->skb).pid, |
| 571 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); |
| 572 | } |
| 573 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 574 | static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 575 | struct request_sock *req, u32 pid, u32 seq, |
| 576 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 578 | const struct inet_request_sock *ireq = inet_rsk(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 580 | unsigned char *b = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 581 | struct inet_diag_msg *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | struct nlmsghdr *nlh; |
| 583 | long tmo; |
| 584 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 585 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | nlh->nlmsg_flags = NLM_F_MULTI; |
| 587 | r = NLMSG_DATA(nlh); |
| 588 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 589 | r->idiag_family = sk->sk_family; |
| 590 | r->idiag_state = TCP_SYN_RECV; |
| 591 | r->idiag_timer = 1; |
| 592 | r->idiag_retrans = req->retrans; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 594 | r->id.idiag_if = sk->sk_bound_dev_if; |
| 595 | r->id.idiag_cookie[0] = (u32)(unsigned long)req; |
| 596 | r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | tmo = req->expires - jiffies; |
| 599 | if (tmo < 0) |
| 600 | tmo = 0; |
| 601 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 602 | r->id.idiag_sport = inet->inet_sport; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 603 | r->id.idiag_dport = ireq->rmt_port; |
| 604 | r->id.idiag_src[0] = ireq->loc_addr; |
| 605 | r->id.idiag_dst[0] = ireq->rmt_addr; |
| 606 | r->idiag_expires = jiffies_to_msecs(tmo); |
| 607 | r->idiag_rqueue = 0; |
| 608 | r->idiag_wqueue = 0; |
| 609 | r->idiag_uid = sock_i_uid(sk); |
| 610 | r->idiag_inode = 0; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 611 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 612 | if (r->idiag_family == AF_INET6) { |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 613 | *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr; |
| 614 | *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 617 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | return skb->len; |
| 620 | |
| 621 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 622 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return -1; |
| 624 | } |
| 625 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 626 | static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 627 | struct netlink_callback *cb, |
| 628 | const struct nlattr *bc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 630 | struct inet_diag_entry entry; |
| 631 | struct inet_diag_req *r = NLMSG_DATA(cb->nlh); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 632 | struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 2ad69c5 | 2005-06-18 22:48:55 -0700 | [diff] [blame] | 633 | struct listen_sock *lopt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | struct inet_sock *inet = inet_sk(sk); |
| 635 | int j, s_j; |
| 636 | int reqnum, s_reqnum; |
| 637 | int err = 0; |
| 638 | |
| 639 | s_j = cb->args[3]; |
| 640 | s_reqnum = cb->args[4]; |
| 641 | |
| 642 | if (s_j > 0) |
| 643 | s_j--; |
| 644 | |
| 645 | entry.family = sk->sk_family; |
| 646 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 647 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 649 | lopt = icsk->icsk_accept_queue.listen_opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (!lopt || !lopt->qlen) |
| 651 | goto out; |
| 652 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 653 | if (bc != NULL) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 654 | entry.sport = inet->inet_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | entry.userlocks = sk->sk_userlocks; |
| 656 | } |
| 657 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 658 | for (j = s_j; j < lopt->nr_table_entries; j++) { |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 659 | struct request_sock *req, *head = lopt->syn_table[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | reqnum = 0; |
| 662 | for (req = head; req; reqnum++, req = req->dl_next) { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 663 | struct inet_request_sock *ireq = inet_rsk(req); |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | if (reqnum < s_reqnum) |
| 666 | continue; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 667 | if (r->id.idiag_dport != ireq->rmt_port && |
| 668 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | continue; |
| 670 | |
| 671 | if (bc) { |
| 672 | entry.saddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 673 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | ca304b6 | 2005-12-13 23:15:40 -0800 | [diff] [blame] | 675 | inet6_rsk(req)->loc_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 677 | &ireq->loc_addr; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 678 | entry.daddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 679 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | ca304b6 | 2005-12-13 23:15:40 -0800 | [diff] [blame] | 681 | inet6_rsk(req)->rmt_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 683 | &ireq->rmt_addr; |
| 684 | entry.dport = ntohs(ireq->rmt_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 686 | if (!inet_diag_bc_run(nla_data(bc), |
| 687 | nla_len(bc), &entry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | continue; |
| 689 | } |
| 690 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 691 | err = inet_diag_fill_req(skb, sk, req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | NETLINK_CB(cb->skb).pid, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 693 | cb->nlh->nlmsg_seq, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (err < 0) { |
| 695 | cb->args[3] = j + 1; |
| 696 | cb->args[4] = reqnum; |
| 697 | goto out; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | s_reqnum = 0; |
| 702 | } |
| 703 | |
| 704 | out: |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 705 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | return err; |
| 708 | } |
| 709 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 710 | static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | int i, num; |
| 713 | int s_i, s_num; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 714 | struct inet_diag_req *r = NLMSG_DATA(cb->nlh); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 715 | const struct inet_diag_handler *handler; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 716 | struct inet_hashinfo *hashinfo; |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 717 | const struct nlattr *bc = NULL; |
| 718 | |
| 719 | if (nlmsg_attrlen(cb->nlh, sizeof(struct inet_diag_req))) |
| 720 | bc = nlmsg_find_attr(cb->nlh, sizeof(*r), INET_DIAG_REQ_BYTECODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 722 | handler = inet_diag_lock_handler(inet_diag_type2proto(cb->nlh->nlmsg_type)); |
Arnaldo Carvalho de Melo | 8cf8e5a | 2008-01-28 20:52:12 -0800 | [diff] [blame] | 723 | if (IS_ERR(handler)) |
| 724 | goto unlock; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 725 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 726 | hashinfo = handler->idiag_hashinfo; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | s_i = cb->args[1]; |
| 729 | s_num = num = cb->args[2]; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (cb->args[0] == 0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 732 | if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | goto skip_listen_ht; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 734 | |
Arnaldo Carvalho de Melo | 0f7ff92 | 2005-08-09 19:59:44 -0700 | [diff] [blame] | 735 | for (i = s_i; i < INET_LHTABLE_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | struct sock *sk; |
Eric Dumazet | c25eb3b | 2008-11-23 17:22:55 -0800 | [diff] [blame] | 737 | struct hlist_nulls_node *node; |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 738 | struct inet_listen_hashbucket *ilb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | num = 0; |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 741 | ilb = &hashinfo->listening_hash[i]; |
| 742 | spin_lock_bh(&ilb->lock); |
Eric Dumazet | c25eb3b | 2008-11-23 17:22:55 -0800 | [diff] [blame] | 743 | sk_nulls_for_each(sk, node, &ilb->head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | struct inet_sock *inet = inet_sk(sk); |
| 745 | |
| 746 | if (num < s_num) { |
| 747 | num++; |
| 748 | continue; |
| 749 | } |
| 750 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 751 | if (r->id.idiag_sport != inet->inet_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 752 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | goto next_listen; |
| 754 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 755 | if (!(r->idiag_states & TCPF_LISTEN) || |
| 756 | r->id.idiag_dport || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | cb->args[3] > 0) |
| 758 | goto syn_recv; |
| 759 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 760 | if (inet_csk_diag_dump(sk, skb, cb, bc) < 0) { |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 761 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | goto done; |
| 763 | } |
| 764 | |
| 765 | syn_recv: |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 766 | if (!(r->idiag_states & TCPF_SYN_RECV)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | goto next_listen; |
| 768 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 769 | if (inet_diag_dump_reqs(skb, sk, cb, bc) < 0) { |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 770 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | goto done; |
| 772 | } |
| 773 | |
| 774 | next_listen: |
| 775 | cb->args[3] = 0; |
| 776 | cb->args[4] = 0; |
| 777 | ++num; |
| 778 | } |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 779 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | s_num = 0; |
| 782 | cb->args[3] = 0; |
| 783 | cb->args[4] = 0; |
| 784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | skip_listen_ht: |
| 786 | cb->args[0] = 1; |
| 787 | s_i = num = s_num = 0; |
| 788 | } |
| 789 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 790 | if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 791 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Eric Dumazet | f373b53 | 2009-10-09 00:16:19 +0000 | [diff] [blame] | 793 | for (i = s_i; i <= hashinfo->ehash_mask; i++) { |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 794 | struct inet_ehash_bucket *head = &hashinfo->ehash[i]; |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 795 | spinlock_t *lock = inet_ehash_lockp(hashinfo, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | struct sock *sk; |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 797 | struct hlist_nulls_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Andi Kleen | 6be547a | 2008-08-28 01:09:54 -0700 | [diff] [blame] | 799 | num = 0; |
| 800 | |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 801 | if (hlist_nulls_empty(&head->chain) && |
| 802 | hlist_nulls_empty(&head->twchain)) |
Andi Kleen | 6be547a | 2008-08-28 01:09:54 -0700 | [diff] [blame] | 803 | continue; |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | if (i > s_i) |
| 806 | s_num = 0; |
| 807 | |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 808 | spin_lock_bh(lock); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 809 | sk_nulls_for_each(sk, node, &head->chain) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | struct inet_sock *inet = inet_sk(sk); |
| 811 | |
| 812 | if (num < s_num) |
| 813 | goto next_normal; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 814 | if (!(r->idiag_states & (1 << sk->sk_state))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | goto next_normal; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 816 | if (r->id.idiag_sport != inet->inet_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 817 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | goto next_normal; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 819 | if (r->id.idiag_dport != inet->inet_dport && |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 820 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | goto next_normal; |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 822 | if (inet_csk_diag_dump(sk, skb, cb, bc) < 0) { |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 823 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | goto done; |
| 825 | } |
| 826 | next_normal: |
| 827 | ++num; |
| 828 | } |
| 829 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 830 | if (r->idiag_states & TCPF_TIME_WAIT) { |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 831 | struct inet_timewait_sock *tw; |
| 832 | |
| 833 | inet_twsk_for_each(tw, node, |
Eric Dumazet | dbca9b275 | 2007-02-08 14:16:46 -0800 | [diff] [blame] | 834 | &head->twchain) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | if (num < s_num) |
| 837 | goto next_dying; |
Arnaldo Carvalho de Melo | 7dbf075 | 2006-01-09 14:56:03 -0800 | [diff] [blame] | 838 | if (r->id.idiag_sport != tw->tw_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 839 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | goto next_dying; |
Arnaldo Carvalho de Melo | 7dbf075 | 2006-01-09 14:56:03 -0800 | [diff] [blame] | 841 | if (r->id.idiag_dport != tw->tw_dport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 842 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | goto next_dying; |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 844 | if (inet_twsk_diag_dump(tw, skb, cb, bc) < 0) { |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 845 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | goto done; |
| 847 | } |
| 848 | next_dying: |
| 849 | ++num; |
| 850 | } |
| 851 | } |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 852 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | done: |
| 856 | cb->args[1] = i; |
| 857 | cb->args[2] = num; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 858 | unlock: |
| 859 | inet_diag_unlock_handler(handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | return skb->len; |
| 861 | } |
| 862 | |
Pavel Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 863 | static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 865 | int hdrlen = sizeof(struct inet_diag_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 867 | if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || |
| 868 | nlmsg_len(nlh) < hdrlen) |
| 869 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 871 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 872 | if (nlmsg_attrlen(nlh, hdrlen)) { |
| 873 | struct nlattr *attr; |
| 874 | |
| 875 | attr = nlmsg_find_attr(nlh, hdrlen, |
| 876 | INET_DIAG_REQ_BYTECODE); |
| 877 | if (attr == NULL || |
| 878 | nla_len(attr) < sizeof(struct inet_diag_bc_op) || |
| 879 | inet_diag_bc_audit(nla_data(attr), nla_len(attr))) |
| 880 | return -EINVAL; |
| 881 | } |
| 882 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 883 | return netlink_dump_start(sdiagnl, skb, nlh, |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 884 | inet_diag_dump, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 886 | |
| 887 | return inet_diag_get_exact(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Pavel Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 890 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 891 | { |
| 892 | return -EOPNOTSUPP; |
| 893 | } |
| 894 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 895 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | { |
Pavel Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 897 | switch (nlh->nlmsg_type) { |
| 898 | case TCPDIAG_GETSOCK: |
| 899 | case DCCPDIAG_GETSOCK: |
| 900 | return inet_diag_rcv_msg_compat(skb, nlh); |
| 901 | case SOCK_DIAG_BY_FAMILY: |
| 902 | return __sock_diag_rcv_msg(skb, nlh); |
| 903 | default: |
| 904 | return -EINVAL; |
| 905 | } |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static DEFINE_MUTEX(sock_diag_mutex); |
| 909 | |
| 910 | static void sock_diag_rcv(struct sk_buff *skb) |
| 911 | { |
| 912 | mutex_lock(&sock_diag_mutex); |
| 913 | netlink_rcv_skb(skb, &sock_diag_rcv_msg); |
| 914 | mutex_unlock(&sock_diag_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 917 | int inet_diag_register(const struct inet_diag_handler *h) |
| 918 | { |
| 919 | const __u16 type = h->idiag_type; |
| 920 | int err = -EINVAL; |
| 921 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 922 | if (type >= IPPROTO_MAX) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 923 | goto out; |
| 924 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 925 | mutex_lock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 926 | err = -EEXIST; |
| 927 | if (inet_diag_table[type] == NULL) { |
| 928 | inet_diag_table[type] = h; |
| 929 | err = 0; |
| 930 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 931 | mutex_unlock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 932 | out: |
| 933 | return err; |
| 934 | } |
| 935 | EXPORT_SYMBOL_GPL(inet_diag_register); |
| 936 | |
| 937 | void inet_diag_unregister(const struct inet_diag_handler *h) |
| 938 | { |
| 939 | const __u16 type = h->idiag_type; |
| 940 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 941 | if (type >= IPPROTO_MAX) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 942 | return; |
| 943 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 944 | mutex_lock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 945 | inet_diag_table[type] = NULL; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 946 | mutex_unlock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 947 | } |
| 948 | EXPORT_SYMBOL_GPL(inet_diag_unregister); |
| 949 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 950 | static int __init inet_diag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame^] | 952 | const int inet_diag_table_size = (IPPROTO_MAX * |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 953 | sizeof(struct inet_diag_handler *)); |
| 954 | int err = -ENOMEM; |
| 955 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 956 | inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 957 | if (!inet_diag_table) |
| 958 | goto out; |
| 959 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 960 | sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, |
| 961 | sock_diag_rcv, NULL, THIS_MODULE); |
| 962 | if (sdiagnl == NULL) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 963 | goto out_free_table; |
Arnaldo Carvalho de Melo | 17b085e | 2005-08-12 12:59:17 -0300 | [diff] [blame] | 964 | err = 0; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 965 | out: |
| 966 | return err; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 967 | out_free_table: |
| 968 | kfree(inet_diag_table); |
| 969 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 972 | static void __exit inet_diag_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 974 | netlink_kernel_release(sdiagnl); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 975 | kfree(inet_diag_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 978 | module_init(inet_diag_init); |
| 979 | module_exit(inet_diag_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | MODULE_LICENSE("GPL"); |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 981 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |