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