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