Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * tcp_diag.c Module for monitoring TCP sockets. |
| 3 | * |
| 4 | * Version: $Id: tcp_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $ |
| 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 | |
| 14 | #include <linux/config.h> |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <linux/inet.h> |
| 33 | #include <linux/stddef.h> |
| 34 | |
| 35 | #include <linux/tcp_diag.h> |
| 36 | |
| 37 | struct tcpdiag_entry |
| 38 | { |
| 39 | u32 *saddr; |
| 40 | u32 *daddr; |
| 41 | u16 sport; |
| 42 | u16 dport; |
| 43 | u16 family; |
| 44 | u16 userlocks; |
| 45 | }; |
| 46 | |
| 47 | static struct sock *tcpnl; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define TCPDIAG_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 | e41aac4 | 2005-08-11 14:37:16 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_IP_TCPDIAG_DCCP |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 53 | extern struct inet_hashinfo dccp_hashinfo; |
| 54 | #endif |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 57 | int ext, u32 pid, u32 seq, u16 nlmsg_flags, |
| 58 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 60 | const struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 61 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct tcpdiagmsg *r; |
| 63 | struct nlmsghdr *nlh; |
| 64 | struct tcp_info *info = NULL; |
| 65 | struct tcpdiag_meminfo *minfo = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | unsigned char *b = skb->tail; |
| 67 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 68 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | nlh->nlmsg_flags = nlmsg_flags; |
| 70 | r = NLMSG_DATA(nlh); |
| 71 | if (sk->sk_state != TCP_TIME_WAIT) { |
| 72 | if (ext & (1<<(TCPDIAG_MEMINFO-1))) |
| 73 | minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo)); |
| 74 | if (ext & (1<<(TCPDIAG_INFO-1))) |
| 75 | info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info)); |
| 76 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 77 | if ((ext & (1 << (TCPDIAG_CONG - 1))) && icsk->icsk_ca_ops) { |
| 78 | size_t len = strlen(icsk->icsk_ca_ops->name); |
Stephen Hemminger | 056ede6 | 2005-06-23 12:21:28 -0700 | [diff] [blame] | 79 | strcpy(TCPDIAG_PUT(skb, TCPDIAG_CONG, len+1), |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 80 | icsk->icsk_ca_ops->name); |
Stephen Hemminger | 056ede6 | 2005-06-23 12:21:28 -0700 | [diff] [blame] | 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | r->tcpdiag_family = sk->sk_family; |
| 84 | r->tcpdiag_state = sk->sk_state; |
| 85 | r->tcpdiag_timer = 0; |
| 86 | r->tcpdiag_retrans = 0; |
| 87 | |
| 88 | r->id.tcpdiag_if = sk->sk_bound_dev_if; |
| 89 | r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk; |
| 90 | r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); |
| 91 | |
| 92 | if (r->tcpdiag_state == TCP_TIME_WAIT) { |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 93 | const struct inet_timewait_sock *tw = inet_twsk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | long tmo = tw->tw_ttd - jiffies; |
| 95 | if (tmo < 0) |
| 96 | tmo = 0; |
| 97 | |
| 98 | r->id.tcpdiag_sport = tw->tw_sport; |
| 99 | r->id.tcpdiag_dport = tw->tw_dport; |
| 100 | r->id.tcpdiag_src[0] = tw->tw_rcv_saddr; |
| 101 | r->id.tcpdiag_dst[0] = tw->tw_daddr; |
| 102 | r->tcpdiag_state = tw->tw_substate; |
| 103 | r->tcpdiag_timer = 3; |
| 104 | r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ; |
| 105 | r->tcpdiag_rqueue = 0; |
| 106 | r->tcpdiag_wqueue = 0; |
| 107 | r->tcpdiag_uid = 0; |
| 108 | r->tcpdiag_inode = 0; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 109 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (r->tcpdiag_family == AF_INET6) { |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 111 | const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk); |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src, |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 114 | &tcp6tw->tw_v6_rcv_saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst, |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 116 | &tcp6tw->tw_v6_daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | nlh->nlmsg_len = skb->tail - b; |
| 120 | return skb->len; |
| 121 | } |
| 122 | |
| 123 | r->id.tcpdiag_sport = inet->sport; |
| 124 | r->id.tcpdiag_dport = inet->dport; |
| 125 | r->id.tcpdiag_src[0] = inet->rcv_saddr; |
| 126 | r->id.tcpdiag_dst[0] = inet->daddr; |
| 127 | |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 128 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (r->tcpdiag_family == AF_INET6) { |
| 130 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 131 | |
| 132 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src, |
| 133 | &np->rcv_saddr); |
| 134 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst, |
| 135 | &np->daddr); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ |
| 140 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 141 | if (icsk->icsk_pending == ICSK_TIME_RETRANS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | r->tcpdiag_timer = 1; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 143 | r->tcpdiag_retrans = icsk->icsk_retransmits; |
| 144 | r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
| 145 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | r->tcpdiag_timer = 4; |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 147 | r->tcpdiag_retrans = icsk->icsk_probes_out; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 148 | r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } else if (timer_pending(&sk->sk_timer)) { |
| 150 | r->tcpdiag_timer = 2; |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 151 | r->tcpdiag_retrans = icsk->icsk_probes_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); |
| 153 | } else { |
| 154 | r->tcpdiag_timer = 0; |
| 155 | r->tcpdiag_expires = 0; |
| 156 | } |
| 157 | #undef EXPIRES_IN_MS |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 158 | /* |
| 159 | * Ahem... for now we'll have some knowledge about TCP -acme |
| 160 | * But this is just one of two small exceptions, both in this |
| 161 | * function, so lets close our eyes for some 15 lines or so... 8) |
| 162 | * -acme |
| 163 | */ |
| 164 | if (sk->sk_protocol == IPPROTO_TCP) { |
| 165 | const struct tcp_sock *tp = tcp_sk(sk); |
| 166 | |
| 167 | r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq; |
| 168 | r->tcpdiag_wqueue = tp->write_seq - tp->snd_una; |
| 169 | } else |
| 170 | r->tcpdiag_rqueue = r->tcpdiag_wqueue = 0; |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | r->tcpdiag_uid = sock_i_uid(sk); |
| 173 | r->tcpdiag_inode = sock_i_ino(sk); |
| 174 | |
| 175 | if (minfo) { |
| 176 | minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc); |
| 177 | minfo->tcpdiag_wmem = sk->sk_wmem_queued; |
| 178 | minfo->tcpdiag_fmem = sk->sk_forward_alloc; |
| 179 | minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc); |
| 180 | } |
| 181 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 182 | /* Ahem... for now we'll have some knowledge about TCP -acme */ |
| 183 | if (info) { |
| 184 | if (sk->sk_protocol == IPPROTO_TCP) |
| 185 | tcp_get_info(sk, info); |
| 186 | else |
| 187 | memset(info, 0, sizeof(*info)); |
| 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 190 | if (sk->sk_state < TCP_TIME_WAIT && |
| 191 | icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) |
| 192 | icsk->icsk_ca_ops->get_info(sk, ext, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | nlh->nlmsg_len = skb->tail - b; |
| 195 | return skb->len; |
| 196 | |
Stephen Hemminger | 7c99c90 | 2005-06-23 12:20:36 -0700 | [diff] [blame] | 197 | rtattr_failure: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | nlmsg_failure: |
| 199 | skb_trim(skb, b - skb->data); |
| 200 | return -1; |
| 201 | } |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh) |
| 204 | { |
| 205 | int err; |
| 206 | struct sock *sk; |
| 207 | struct tcpdiagreq *req = NLMSG_DATA(nlh); |
| 208 | struct sk_buff *rep; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 209 | struct inet_hashinfo *hashinfo = &tcp_hashinfo; |
Arnaldo Carvalho de Melo | e41aac4 | 2005-08-11 14:37:16 -0700 | [diff] [blame] | 210 | #ifdef CONFIG_IP_TCPDIAG_DCCP |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 211 | if (nlh->nlmsg_type == DCCPDIAG_GETSOCK) |
| 212 | hashinfo = &dccp_hashinfo; |
| 213 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (req->tcpdiag_family == AF_INET) { |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 215 | sk = inet_lookup(hashinfo, req->id.tcpdiag_dst[0], |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 216 | req->id.tcpdiag_dport, req->id.tcpdiag_src[0], |
| 217 | req->id.tcpdiag_sport, req->id.tcpdiag_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 219 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | else if (req->tcpdiag_family == AF_INET6) { |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 221 | sk = inet6_lookup(hashinfo, |
| 222 | (struct in6_addr*)req->id.tcpdiag_dst, |
| 223 | req->id.tcpdiag_dport, |
| 224 | (struct in6_addr*)req->id.tcpdiag_src, |
| 225 | req->id.tcpdiag_sport, |
| 226 | req->id.tcpdiag_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | #endif |
| 229 | else { |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | if (sk == NULL) |
| 234 | return -ENOENT; |
| 235 | |
| 236 | err = -ESTALE; |
| 237 | if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE || |
| 238 | req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) && |
| 239 | ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] || |
| 240 | (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1])) |
| 241 | goto out; |
| 242 | |
| 243 | err = -ENOMEM; |
| 244 | rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+ |
| 245 | sizeof(struct tcpdiag_meminfo)+ |
| 246 | sizeof(struct tcp_info)+64), GFP_KERNEL); |
| 247 | if (!rep) |
| 248 | goto out; |
| 249 | |
| 250 | if (tcpdiag_fill(rep, sk, req->tcpdiag_ext, |
| 251 | NETLINK_CB(in_skb).pid, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 252 | nlh->nlmsg_seq, 0, nlh) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | BUG(); |
| 254 | |
| 255 | err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); |
| 256 | if (err > 0) |
| 257 | err = 0; |
| 258 | |
| 259 | out: |
| 260 | if (sk) { |
| 261 | if (sk->sk_state == TCP_TIME_WAIT) |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 262 | inet_twsk_put((struct inet_timewait_sock *)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | else |
| 264 | sock_put(sk); |
| 265 | } |
| 266 | return err; |
| 267 | } |
| 268 | |
| 269 | static int bitstring_match(const u32 *a1, const u32 *a2, int bits) |
| 270 | { |
| 271 | int words = bits >> 5; |
| 272 | |
| 273 | bits &= 0x1f; |
| 274 | |
| 275 | if (words) { |
| 276 | if (memcmp(a1, a2, words << 2)) |
| 277 | return 0; |
| 278 | } |
| 279 | if (bits) { |
| 280 | __u32 w1, w2; |
| 281 | __u32 mask; |
| 282 | |
| 283 | w1 = a1[words]; |
| 284 | w2 = a2[words]; |
| 285 | |
| 286 | mask = htonl((0xffffffff) << (32 - bits)); |
| 287 | |
| 288 | if ((w1 ^ w2) & mask) |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | static int tcpdiag_bc_run(const void *bc, int len, |
| 297 | const struct tcpdiag_entry *entry) |
| 298 | { |
| 299 | while (len > 0) { |
| 300 | int yes = 1; |
| 301 | const struct tcpdiag_bc_op *op = bc; |
| 302 | |
| 303 | switch (op->code) { |
| 304 | case TCPDIAG_BC_NOP: |
| 305 | break; |
| 306 | case TCPDIAG_BC_JMP: |
| 307 | yes = 0; |
| 308 | break; |
| 309 | case TCPDIAG_BC_S_GE: |
| 310 | yes = entry->sport >= op[1].no; |
| 311 | break; |
| 312 | case TCPDIAG_BC_S_LE: |
| 313 | yes = entry->dport <= op[1].no; |
| 314 | break; |
| 315 | case TCPDIAG_BC_D_GE: |
| 316 | yes = entry->dport >= op[1].no; |
| 317 | break; |
| 318 | case TCPDIAG_BC_D_LE: |
| 319 | yes = entry->dport <= op[1].no; |
| 320 | break; |
| 321 | case TCPDIAG_BC_AUTO: |
| 322 | yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); |
| 323 | break; |
| 324 | case TCPDIAG_BC_S_COND: |
| 325 | case TCPDIAG_BC_D_COND: |
| 326 | { |
| 327 | struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1); |
| 328 | u32 *addr; |
| 329 | |
| 330 | if (cond->port != -1 && |
| 331 | cond->port != (op->code == TCPDIAG_BC_S_COND ? |
| 332 | entry->sport : entry->dport)) { |
| 333 | yes = 0; |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | if (cond->prefix_len == 0) |
| 338 | break; |
| 339 | |
| 340 | if (op->code == TCPDIAG_BC_S_COND) |
| 341 | addr = entry->saddr; |
| 342 | else |
| 343 | addr = entry->daddr; |
| 344 | |
| 345 | if (bitstring_match(addr, cond->addr, cond->prefix_len)) |
| 346 | break; |
| 347 | if (entry->family == AF_INET6 && |
| 348 | cond->family == AF_INET) { |
| 349 | if (addr[0] == 0 && addr[1] == 0 && |
| 350 | addr[2] == htonl(0xffff) && |
| 351 | bitstring_match(addr+3, cond->addr, cond->prefix_len)) |
| 352 | break; |
| 353 | } |
| 354 | yes = 0; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (yes) { |
| 360 | len -= op->yes; |
| 361 | bc += op->yes; |
| 362 | } else { |
| 363 | len -= op->no; |
| 364 | bc += op->no; |
| 365 | } |
| 366 | } |
| 367 | return (len == 0); |
| 368 | } |
| 369 | |
| 370 | static int valid_cc(const void *bc, int len, int cc) |
| 371 | { |
| 372 | while (len >= 0) { |
| 373 | const struct tcpdiag_bc_op *op = bc; |
| 374 | |
| 375 | if (cc > len) |
| 376 | return 0; |
| 377 | if (cc == len) |
| 378 | return 1; |
| 379 | if (op->yes < 4) |
| 380 | return 0; |
| 381 | len -= op->yes; |
| 382 | bc += op->yes; |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len) |
| 388 | { |
| 389 | const unsigned char *bc = bytecode; |
| 390 | int len = bytecode_len; |
| 391 | |
| 392 | while (len > 0) { |
| 393 | struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc; |
| 394 | |
| 395 | //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); |
| 396 | switch (op->code) { |
| 397 | case TCPDIAG_BC_AUTO: |
| 398 | case TCPDIAG_BC_S_COND: |
| 399 | case TCPDIAG_BC_D_COND: |
| 400 | case TCPDIAG_BC_S_GE: |
| 401 | case TCPDIAG_BC_S_LE: |
| 402 | case TCPDIAG_BC_D_GE: |
| 403 | case TCPDIAG_BC_D_LE: |
| 404 | if (op->yes < 4 || op->yes > len+4) |
| 405 | return -EINVAL; |
| 406 | case TCPDIAG_BC_JMP: |
| 407 | if (op->no < 4 || op->no > len+4) |
| 408 | return -EINVAL; |
| 409 | if (op->no < len && |
| 410 | !valid_cc(bytecode, bytecode_len, len-op->no)) |
| 411 | return -EINVAL; |
| 412 | break; |
| 413 | case TCPDIAG_BC_NOP: |
| 414 | if (op->yes < 4 || op->yes > len+4) |
| 415 | return -EINVAL; |
| 416 | break; |
| 417 | default: |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | bc += op->yes; |
| 421 | len -= op->yes; |
| 422 | } |
| 423 | return len == 0 ? 0 : -EINVAL; |
| 424 | } |
| 425 | |
| 426 | static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk, |
| 427 | struct netlink_callback *cb) |
| 428 | { |
| 429 | struct tcpdiagreq *r = NLMSG_DATA(cb->nlh); |
| 430 | |
| 431 | if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) { |
| 432 | struct tcpdiag_entry entry; |
| 433 | struct rtattr *bc = (struct rtattr *)(r + 1); |
| 434 | struct inet_sock *inet = inet_sk(sk); |
| 435 | |
| 436 | entry.family = sk->sk_family; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 437 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (entry.family == AF_INET6) { |
| 439 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 440 | |
| 441 | entry.saddr = np->rcv_saddr.s6_addr32; |
| 442 | entry.daddr = np->daddr.s6_addr32; |
| 443 | } else |
| 444 | #endif |
| 445 | { |
| 446 | entry.saddr = &inet->rcv_saddr; |
| 447 | entry.daddr = &inet->daddr; |
| 448 | } |
| 449 | entry.sport = inet->num; |
| 450 | entry.dport = ntohs(inet->dport); |
| 451 | entry.userlocks = sk->sk_userlocks; |
| 452 | |
| 453 | if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry)) |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 458 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk, |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 462 | struct request_sock *req, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 463 | u32 pid, u32 seq, |
| 464 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 466 | const struct inet_request_sock *ireq = inet_rsk(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct inet_sock *inet = inet_sk(sk); |
| 468 | unsigned char *b = skb->tail; |
| 469 | struct tcpdiagmsg *r; |
| 470 | struct nlmsghdr *nlh; |
| 471 | long tmo; |
| 472 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 473 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | nlh->nlmsg_flags = NLM_F_MULTI; |
| 475 | r = NLMSG_DATA(nlh); |
| 476 | |
| 477 | r->tcpdiag_family = sk->sk_family; |
| 478 | r->tcpdiag_state = TCP_SYN_RECV; |
| 479 | r->tcpdiag_timer = 1; |
| 480 | r->tcpdiag_retrans = req->retrans; |
| 481 | |
| 482 | r->id.tcpdiag_if = sk->sk_bound_dev_if; |
| 483 | r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req; |
| 484 | r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); |
| 485 | |
| 486 | tmo = req->expires - jiffies; |
| 487 | if (tmo < 0) |
| 488 | tmo = 0; |
| 489 | |
| 490 | r->id.tcpdiag_sport = inet->sport; |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 491 | r->id.tcpdiag_dport = ireq->rmt_port; |
| 492 | r->id.tcpdiag_src[0] = ireq->loc_addr; |
| 493 | r->id.tcpdiag_dst[0] = ireq->rmt_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | r->tcpdiag_expires = jiffies_to_msecs(tmo), |
| 495 | r->tcpdiag_rqueue = 0; |
| 496 | r->tcpdiag_wqueue = 0; |
| 497 | r->tcpdiag_uid = sock_i_uid(sk); |
| 498 | r->tcpdiag_inode = 0; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 499 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (r->tcpdiag_family == AF_INET6) { |
| 501 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src, |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 502 | &tcp6_rsk(req)->loc_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst, |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 504 | &tcp6_rsk(req)->rmt_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | #endif |
| 507 | nlh->nlmsg_len = skb->tail - b; |
| 508 | |
| 509 | return skb->len; |
| 510 | |
| 511 | nlmsg_failure: |
| 512 | skb_trim(skb, b - skb->data); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk, |
| 517 | struct netlink_callback *cb) |
| 518 | { |
| 519 | struct tcpdiag_entry entry; |
| 520 | struct tcpdiagreq *r = NLMSG_DATA(cb->nlh); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 521 | struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 2ad69c5 | 2005-06-18 22:48:55 -0700 | [diff] [blame] | 522 | struct listen_sock *lopt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | struct rtattr *bc = NULL; |
| 524 | struct inet_sock *inet = inet_sk(sk); |
| 525 | int j, s_j; |
| 526 | int reqnum, s_reqnum; |
| 527 | int err = 0; |
| 528 | |
| 529 | s_j = cb->args[3]; |
| 530 | s_reqnum = cb->args[4]; |
| 531 | |
| 532 | if (s_j > 0) |
| 533 | s_j--; |
| 534 | |
| 535 | entry.family = sk->sk_family; |
| 536 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 537 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 539 | lopt = icsk->icsk_accept_queue.listen_opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (!lopt || !lopt->qlen) |
| 541 | goto out; |
| 542 | |
| 543 | if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) { |
| 544 | bc = (struct rtattr *)(r + 1); |
| 545 | entry.sport = inet->num; |
| 546 | entry.userlocks = sk->sk_userlocks; |
| 547 | } |
| 548 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 549 | for (j = s_j; j < lopt->nr_table_entries; j++) { |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 550 | struct request_sock *req, *head = lopt->syn_table[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | reqnum = 0; |
| 553 | for (req = head; req; reqnum++, req = req->dl_next) { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 554 | struct inet_request_sock *ireq = inet_rsk(req); |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (reqnum < s_reqnum) |
| 557 | continue; |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 558 | if (r->id.tcpdiag_dport != ireq->rmt_port && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | r->id.tcpdiag_dport) |
| 560 | continue; |
| 561 | |
| 562 | if (bc) { |
| 563 | entry.saddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 564 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 566 | tcp6_rsk(req)->loc_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 568 | &ireq->loc_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | entry.daddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame^] | 570 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 572 | tcp6_rsk(req)->rmt_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 574 | &ireq->rmt_addr; |
| 575 | entry.dport = ntohs(ireq->rmt_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | if (!tcpdiag_bc_run(RTA_DATA(bc), |
| 578 | RTA_PAYLOAD(bc), &entry)) |
| 579 | continue; |
| 580 | } |
| 581 | |
| 582 | err = tcpdiag_fill_req(skb, sk, req, |
| 583 | NETLINK_CB(cb->skb).pid, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 584 | cb->nlh->nlmsg_seq, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (err < 0) { |
| 586 | cb->args[3] = j + 1; |
| 587 | cb->args[4] = reqnum; |
| 588 | goto out; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | s_reqnum = 0; |
| 593 | } |
| 594 | |
| 595 | out: |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 596 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | return err; |
| 599 | } |
| 600 | |
| 601 | static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 602 | { |
| 603 | int i, num; |
| 604 | int s_i, s_num; |
| 605 | struct tcpdiagreq *r = NLMSG_DATA(cb->nlh); |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 606 | struct inet_hashinfo *hashinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | s_i = cb->args[1]; |
| 609 | s_num = num = cb->args[2]; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 610 | hashinfo = &tcp_hashinfo; |
Arnaldo Carvalho de Melo | e41aac4 | 2005-08-11 14:37:16 -0700 | [diff] [blame] | 611 | #ifdef CONFIG_IP_TCPDIAG_DCCP |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 612 | if (cb->nlh->nlmsg_type == DCCPDIAG_GETSOCK) |
| 613 | hashinfo = &dccp_hashinfo; |
| 614 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (cb->args[0] == 0) { |
| 616 | if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV))) |
| 617 | goto skip_listen_ht; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 618 | |
| 619 | inet_listen_lock(hashinfo); |
Arnaldo Carvalho de Melo | 0f7ff92 | 2005-08-09 19:59:44 -0700 | [diff] [blame] | 620 | for (i = s_i; i < INET_LHTABLE_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | struct sock *sk; |
| 622 | struct hlist_node *node; |
| 623 | |
| 624 | num = 0; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 625 | sk_for_each(sk, node, &hashinfo->listening_hash[i]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | struct inet_sock *inet = inet_sk(sk); |
| 627 | |
| 628 | if (num < s_num) { |
| 629 | num++; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (r->id.tcpdiag_sport != inet->sport && |
| 634 | r->id.tcpdiag_sport) |
| 635 | goto next_listen; |
| 636 | |
| 637 | if (!(r->tcpdiag_states&TCPF_LISTEN) || |
| 638 | r->id.tcpdiag_dport || |
| 639 | cb->args[3] > 0) |
| 640 | goto syn_recv; |
| 641 | |
| 642 | if (tcpdiag_dump_sock(skb, sk, cb) < 0) { |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 643 | inet_listen_unlock(hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | goto done; |
| 645 | } |
| 646 | |
| 647 | syn_recv: |
| 648 | if (!(r->tcpdiag_states&TCPF_SYN_RECV)) |
| 649 | goto next_listen; |
| 650 | |
| 651 | if (tcpdiag_dump_reqs(skb, sk, cb) < 0) { |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 652 | inet_listen_unlock(hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | goto done; |
| 654 | } |
| 655 | |
| 656 | next_listen: |
| 657 | cb->args[3] = 0; |
| 658 | cb->args[4] = 0; |
| 659 | ++num; |
| 660 | } |
| 661 | |
| 662 | s_num = 0; |
| 663 | cb->args[3] = 0; |
| 664 | cb->args[4] = 0; |
| 665 | } |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 666 | inet_listen_unlock(hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | skip_listen_ht: |
| 668 | cb->args[0] = 1; |
| 669 | s_i = num = s_num = 0; |
| 670 | } |
| 671 | |
| 672 | if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV))) |
| 673 | return skb->len; |
| 674 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 675 | for (i = s_i; i < hashinfo->ehash_size; i++) { |
| 676 | struct inet_ehash_bucket *head = &hashinfo->ehash[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | struct sock *sk; |
| 678 | struct hlist_node *node; |
| 679 | |
| 680 | if (i > s_i) |
| 681 | s_num = 0; |
| 682 | |
| 683 | read_lock_bh(&head->lock); |
| 684 | |
| 685 | num = 0; |
| 686 | sk_for_each(sk, node, &head->chain) { |
| 687 | struct inet_sock *inet = inet_sk(sk); |
| 688 | |
| 689 | if (num < s_num) |
| 690 | goto next_normal; |
| 691 | if (!(r->tcpdiag_states & (1 << sk->sk_state))) |
| 692 | goto next_normal; |
| 693 | if (r->id.tcpdiag_sport != inet->sport && |
| 694 | r->id.tcpdiag_sport) |
| 695 | goto next_normal; |
| 696 | if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport) |
| 697 | goto next_normal; |
| 698 | if (tcpdiag_dump_sock(skb, sk, cb) < 0) { |
| 699 | read_unlock_bh(&head->lock); |
| 700 | goto done; |
| 701 | } |
| 702 | next_normal: |
| 703 | ++num; |
| 704 | } |
| 705 | |
| 706 | if (r->tcpdiag_states&TCPF_TIME_WAIT) { |
| 707 | sk_for_each(sk, node, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 708 | &hashinfo->ehash[i + hashinfo->ehash_size].chain) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | struct inet_sock *inet = inet_sk(sk); |
| 710 | |
| 711 | if (num < s_num) |
| 712 | goto next_dying; |
| 713 | if (r->id.tcpdiag_sport != inet->sport && |
| 714 | r->id.tcpdiag_sport) |
| 715 | goto next_dying; |
| 716 | if (r->id.tcpdiag_dport != inet->dport && |
| 717 | r->id.tcpdiag_dport) |
| 718 | goto next_dying; |
| 719 | if (tcpdiag_dump_sock(skb, sk, cb) < 0) { |
| 720 | read_unlock_bh(&head->lock); |
| 721 | goto done; |
| 722 | } |
| 723 | next_dying: |
| 724 | ++num; |
| 725 | } |
| 726 | } |
| 727 | read_unlock_bh(&head->lock); |
| 728 | } |
| 729 | |
| 730 | done: |
| 731 | cb->args[1] = i; |
| 732 | cb->args[2] = num; |
| 733 | return skb->len; |
| 734 | } |
| 735 | |
| 736 | static int tcpdiag_dump_done(struct netlink_callback *cb) |
| 737 | { |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | static __inline__ int |
| 743 | tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 744 | { |
| 745 | if (!(nlh->nlmsg_flags&NLM_F_REQUEST)) |
| 746 | return 0; |
| 747 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 748 | if (nlh->nlmsg_type != TCPDIAG_GETSOCK |
Arnaldo Carvalho de Melo | e41aac4 | 2005-08-11 14:37:16 -0700 | [diff] [blame] | 749 | #ifdef CONFIG_IP_TCPDIAG_DCCP |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 750 | && nlh->nlmsg_type != DCCPDIAG_GETSOCK |
| 751 | #endif |
| 752 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | goto err_inval; |
| 754 | |
| 755 | if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len) |
| 756 | goto err_inval; |
| 757 | |
| 758 | if (nlh->nlmsg_flags&NLM_F_DUMP) { |
| 759 | if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) { |
| 760 | struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq)); |
| 761 | if (rta->rta_type != TCPDIAG_REQ_BYTECODE || |
| 762 | rta->rta_len < 8 || |
| 763 | rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq))) |
| 764 | goto err_inval; |
| 765 | if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta))) |
| 766 | goto err_inval; |
| 767 | } |
| 768 | return netlink_dump_start(tcpnl, skb, nlh, |
| 769 | tcpdiag_dump, |
| 770 | tcpdiag_dump_done); |
| 771 | } else { |
| 772 | return tcpdiag_get_exact(skb, nlh); |
| 773 | } |
| 774 | |
| 775 | err_inval: |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | static inline void tcpdiag_rcv_skb(struct sk_buff *skb) |
| 781 | { |
| 782 | int err; |
| 783 | struct nlmsghdr * nlh; |
| 784 | |
| 785 | if (skb->len >= NLMSG_SPACE(0)) { |
| 786 | nlh = (struct nlmsghdr *)skb->data; |
| 787 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
| 788 | return; |
| 789 | err = tcpdiag_rcv_msg(skb, nlh); |
| 790 | if (err || nlh->nlmsg_flags & NLM_F_ACK) |
| 791 | netlink_ack(skb, nlh, err); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static void tcpdiag_rcv(struct sock *sk, int len) |
| 796 | { |
| 797 | struct sk_buff *skb; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 798 | unsigned int qlen = skb_queue_len(&sk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 800 | while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | tcpdiag_rcv_skb(skb); |
| 802 | kfree_skb(skb); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static int __init tcpdiag_init(void) |
| 807 | { |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 808 | tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv, |
| 809 | THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (tcpnl == NULL) |
| 811 | return -ENOMEM; |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static void __exit tcpdiag_exit(void) |
| 816 | { |
| 817 | sock_release(tcpnl->sk_socket); |
| 818 | } |
| 819 | |
| 820 | module_init(tcpdiag_init); |
| 821 | module_exit(tcpdiag_exit); |
| 822 | MODULE_LICENSE("GPL"); |