blob: b13b71cb9ced421205dc1da4255978d7566788e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Melo505cbfc2005-08-12 09:19:38 -030027#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 Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/inet.h>
33#include <linux/stddef.h>
34
35#include <linux/tcp_diag.h>
36
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030037static const struct inet_diag_handler **inet_diag_table;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct tcpdiag_entry
40{
41 u32 *saddr;
42 u32 *daddr;
43 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
47};
48
49static struct sock *tcpnl;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define TCPDIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070052 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Arnaldo Carvalho de Meloe41aac42005-08-11 14:37:16 -070054#ifdef CONFIG_IP_TCPDIAG_DCCP
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030055extern struct inet_hashinfo dccp_hashinfo;
56#endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030059 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
60 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070062 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070063 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct tcpdiagmsg *r;
65 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030066 void *info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct tcpdiag_meminfo *minfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned char *b = skb->tail;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030069 const struct inet_diag_handler *handler;
70
71 handler = inet_diag_table[unlh->nlmsg_type];
72 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030074 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 r = NLMSG_DATA(nlh);
78 if (sk->sk_state != TCP_TIME_WAIT) {
79 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
80 minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
81 if (ext & (1<<(TCPDIAG_INFO-1)))
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030082 info = TCPDIAG_PUT(skb, TCPDIAG_INFO,
83 handler->idiag_info_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030085 if ((ext & (1 << (TCPDIAG_CONG - 1))) && icsk->icsk_ca_ops) {
86 size_t len = strlen(icsk->icsk_ca_ops->name);
Stephen Hemminger056ede62005-06-23 12:21:28 -070087 strcpy(TCPDIAG_PUT(skb, TCPDIAG_CONG, len+1),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030088 icsk->icsk_ca_ops->name);
Stephen Hemminger056ede62005-06-23 12:21:28 -070089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 r->tcpdiag_family = sk->sk_family;
92 r->tcpdiag_state = sk->sk_state;
93 r->tcpdiag_timer = 0;
94 r->tcpdiag_retrans = 0;
95
96 r->id.tcpdiag_if = sk->sk_bound_dev_if;
97 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
98 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
99
100 if (r->tcpdiag_state == TCP_TIME_WAIT) {
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700101 const struct inet_timewait_sock *tw = inet_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 long tmo = tw->tw_ttd - jiffies;
103 if (tmo < 0)
104 tmo = 0;
105
106 r->id.tcpdiag_sport = tw->tw_sport;
107 r->id.tcpdiag_dport = tw->tw_dport;
108 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
109 r->id.tcpdiag_dst[0] = tw->tw_daddr;
110 r->tcpdiag_state = tw->tw_substate;
111 r->tcpdiag_timer = 3;
112 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
113 r->tcpdiag_rqueue = 0;
114 r->tcpdiag_wqueue = 0;
115 r->tcpdiag_uid = 0;
116 r->tcpdiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300117#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (r->tcpdiag_family == AF_INET6) {
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700119 const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700122 &tcp6tw->tw_v6_rcv_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700124 &tcp6tw->tw_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126#endif
127 nlh->nlmsg_len = skb->tail - b;
128 return skb->len;
129 }
130
131 r->id.tcpdiag_sport = inet->sport;
132 r->id.tcpdiag_dport = inet->dport;
133 r->id.tcpdiag_src[0] = inet->rcv_saddr;
134 r->id.tcpdiag_dst[0] = inet->daddr;
135
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300136#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (r->tcpdiag_family == AF_INET6) {
138 struct ipv6_pinfo *np = inet6_sk(sk);
139
140 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
141 &np->rcv_saddr);
142 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
143 &np->daddr);
144 }
145#endif
146
147#define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
148
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700149 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 r->tcpdiag_timer = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700151 r->tcpdiag_retrans = icsk->icsk_retransmits;
152 r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
153 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 r->tcpdiag_timer = 4;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300155 r->tcpdiag_retrans = icsk->icsk_probes_out;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700156 r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } else if (timer_pending(&sk->sk_timer)) {
158 r->tcpdiag_timer = 2;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300159 r->tcpdiag_retrans = icsk->icsk_probes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
161 } else {
162 r->tcpdiag_timer = 0;
163 r->tcpdiag_expires = 0;
164 }
165#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 r->tcpdiag_uid = sock_i_uid(sk);
168 r->tcpdiag_inode = sock_i_ino(sk);
169
170 if (minfo) {
171 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
172 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
173 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
174 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
175 }
176
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300177 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300179 if (sk->sk_state < TCP_TIME_WAIT &&
180 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
181 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 nlh->nlmsg_len = skb->tail - b;
184 return skb->len;
185
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700186rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187nlmsg_failure:
188 skb_trim(skb, b - skb->data);
189 return -1;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
193{
194 int err;
195 struct sock *sk;
196 struct tcpdiagreq *req = NLMSG_DATA(nlh);
197 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300198 struct inet_hashinfo *hashinfo;
199 const struct inet_diag_handler *handler;
200
201 handler = inet_diag_table[nlh->nlmsg_type];
202 BUG_ON(handler == NULL);
203 hashinfo = handler->idiag_hashinfo;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (req->tcpdiag_family == AF_INET) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300206 sk = inet_lookup(hashinfo, req->id.tcpdiag_dst[0],
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700207 req->id.tcpdiag_dport, req->id.tcpdiag_src[0],
208 req->id.tcpdiag_sport, req->id.tcpdiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300210#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 else if (req->tcpdiag_family == AF_INET6) {
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300212 sk = inet6_lookup(hashinfo,
213 (struct in6_addr*)req->id.tcpdiag_dst,
214 req->id.tcpdiag_dport,
215 (struct in6_addr*)req->id.tcpdiag_src,
216 req->id.tcpdiag_sport,
217 req->id.tcpdiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219#endif
220 else {
221 return -EINVAL;
222 }
223
224 if (sk == NULL)
225 return -ENOENT;
226
227 err = -ESTALE;
228 if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
229 req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
230 ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
231 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
232 goto out;
233
234 err = -ENOMEM;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300235 rep = alloc_skb(NLMSG_SPACE((sizeof(struct tcpdiagmsg) +
236 sizeof(struct tcpdiag_meminfo) +
237 handler->idiag_info_size + 64)),
238 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!rep)
240 goto out;
241
242 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
243 NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300244 nlh->nlmsg_seq, 0, nlh) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 BUG();
246
247 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
248 if (err > 0)
249 err = 0;
250
251out:
252 if (sk) {
253 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700254 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 else
256 sock_put(sk);
257 }
258 return err;
259}
260
261static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
262{
263 int words = bits >> 5;
264
265 bits &= 0x1f;
266
267 if (words) {
268 if (memcmp(a1, a2, words << 2))
269 return 0;
270 }
271 if (bits) {
272 __u32 w1, w2;
273 __u32 mask;
274
275 w1 = a1[words];
276 w2 = a2[words];
277
278 mask = htonl((0xffffffff) << (32 - bits));
279
280 if ((w1 ^ w2) & mask)
281 return 0;
282 }
283
284 return 1;
285}
286
287
288static int tcpdiag_bc_run(const void *bc, int len,
289 const struct tcpdiag_entry *entry)
290{
291 while (len > 0) {
292 int yes = 1;
293 const struct tcpdiag_bc_op *op = bc;
294
295 switch (op->code) {
296 case TCPDIAG_BC_NOP:
297 break;
298 case TCPDIAG_BC_JMP:
299 yes = 0;
300 break;
301 case TCPDIAG_BC_S_GE:
302 yes = entry->sport >= op[1].no;
303 break;
304 case TCPDIAG_BC_S_LE:
305 yes = entry->dport <= op[1].no;
306 break;
307 case TCPDIAG_BC_D_GE:
308 yes = entry->dport >= op[1].no;
309 break;
310 case TCPDIAG_BC_D_LE:
311 yes = entry->dport <= op[1].no;
312 break;
313 case TCPDIAG_BC_AUTO:
314 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
315 break;
316 case TCPDIAG_BC_S_COND:
317 case TCPDIAG_BC_D_COND:
318 {
319 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
320 u32 *addr;
321
322 if (cond->port != -1 &&
323 cond->port != (op->code == TCPDIAG_BC_S_COND ?
324 entry->sport : entry->dport)) {
325 yes = 0;
326 break;
327 }
328
329 if (cond->prefix_len == 0)
330 break;
331
332 if (op->code == TCPDIAG_BC_S_COND)
333 addr = entry->saddr;
334 else
335 addr = entry->daddr;
336
337 if (bitstring_match(addr, cond->addr, cond->prefix_len))
338 break;
339 if (entry->family == AF_INET6 &&
340 cond->family == AF_INET) {
341 if (addr[0] == 0 && addr[1] == 0 &&
342 addr[2] == htonl(0xffff) &&
343 bitstring_match(addr+3, cond->addr, cond->prefix_len))
344 break;
345 }
346 yes = 0;
347 break;
348 }
349 }
350
351 if (yes) {
352 len -= op->yes;
353 bc += op->yes;
354 } else {
355 len -= op->no;
356 bc += op->no;
357 }
358 }
359 return (len == 0);
360}
361
362static int valid_cc(const void *bc, int len, int cc)
363{
364 while (len >= 0) {
365 const struct tcpdiag_bc_op *op = bc;
366
367 if (cc > len)
368 return 0;
369 if (cc == len)
370 return 1;
371 if (op->yes < 4)
372 return 0;
373 len -= op->yes;
374 bc += op->yes;
375 }
376 return 0;
377}
378
379static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
380{
381 const unsigned char *bc = bytecode;
382 int len = bytecode_len;
383
384 while (len > 0) {
385 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
386
387//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
388 switch (op->code) {
389 case TCPDIAG_BC_AUTO:
390 case TCPDIAG_BC_S_COND:
391 case TCPDIAG_BC_D_COND:
392 case TCPDIAG_BC_S_GE:
393 case TCPDIAG_BC_S_LE:
394 case TCPDIAG_BC_D_GE:
395 case TCPDIAG_BC_D_LE:
396 if (op->yes < 4 || op->yes > len+4)
397 return -EINVAL;
398 case TCPDIAG_BC_JMP:
399 if (op->no < 4 || op->no > len+4)
400 return -EINVAL;
401 if (op->no < len &&
402 !valid_cc(bytecode, bytecode_len, len-op->no))
403 return -EINVAL;
404 break;
405 case TCPDIAG_BC_NOP:
406 if (op->yes < 4 || op->yes > len+4)
407 return -EINVAL;
408 break;
409 default:
410 return -EINVAL;
411 }
412 bc += op->yes;
413 len -= op->yes;
414 }
415 return len == 0 ? 0 : -EINVAL;
416}
417
418static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
419 struct netlink_callback *cb)
420{
421 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
422
423 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
424 struct tcpdiag_entry entry;
425 struct rtattr *bc = (struct rtattr *)(r + 1);
426 struct inet_sock *inet = inet_sk(sk);
427
428 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300429#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (entry.family == AF_INET6) {
431 struct ipv6_pinfo *np = inet6_sk(sk);
432
433 entry.saddr = np->rcv_saddr.s6_addr32;
434 entry.daddr = np->daddr.s6_addr32;
435 } else
436#endif
437 {
438 entry.saddr = &inet->rcv_saddr;
439 entry.daddr = &inet->daddr;
440 }
441 entry.sport = inet->num;
442 entry.dport = ntohs(inet->dport);
443 entry.userlocks = sk->sk_userlocks;
444
445 if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
446 return 0;
447 }
448
449 return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300450 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700454 struct request_sock *req,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300455 u32 pid, u32 seq,
456 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700458 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct inet_sock *inet = inet_sk(sk);
460 unsigned char *b = skb->tail;
461 struct tcpdiagmsg *r;
462 struct nlmsghdr *nlh;
463 long tmo;
464
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300465 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 nlh->nlmsg_flags = NLM_F_MULTI;
467 r = NLMSG_DATA(nlh);
468
469 r->tcpdiag_family = sk->sk_family;
470 r->tcpdiag_state = TCP_SYN_RECV;
471 r->tcpdiag_timer = 1;
472 r->tcpdiag_retrans = req->retrans;
473
474 r->id.tcpdiag_if = sk->sk_bound_dev_if;
475 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
476 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
477
478 tmo = req->expires - jiffies;
479 if (tmo < 0)
480 tmo = 0;
481
482 r->id.tcpdiag_sport = inet->sport;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700483 r->id.tcpdiag_dport = ireq->rmt_port;
484 r->id.tcpdiag_src[0] = ireq->loc_addr;
485 r->id.tcpdiag_dst[0] = ireq->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 r->tcpdiag_expires = jiffies_to_msecs(tmo),
487 r->tcpdiag_rqueue = 0;
488 r->tcpdiag_wqueue = 0;
489 r->tcpdiag_uid = sock_i_uid(sk);
490 r->tcpdiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300491#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (r->tcpdiag_family == AF_INET6) {
493 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700494 &tcp6_rsk(req)->loc_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700496 &tcp6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498#endif
499 nlh->nlmsg_len = skb->tail - b;
500
501 return skb->len;
502
503nlmsg_failure:
504 skb_trim(skb, b - skb->data);
505 return -1;
506}
507
508static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
509 struct netlink_callback *cb)
510{
511 struct tcpdiag_entry entry;
512 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700513 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700514 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct rtattr *bc = NULL;
516 struct inet_sock *inet = inet_sk(sk);
517 int j, s_j;
518 int reqnum, s_reqnum;
519 int err = 0;
520
521 s_j = cb->args[3];
522 s_reqnum = cb->args[4];
523
524 if (s_j > 0)
525 s_j--;
526
527 entry.family = sk->sk_family;
528
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700529 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700531 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!lopt || !lopt->qlen)
533 goto out;
534
535 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
536 bc = (struct rtattr *)(r + 1);
537 entry.sport = inet->num;
538 entry.userlocks = sk->sk_userlocks;
539 }
540
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300541 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700542 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 reqnum = 0;
545 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700546 struct inet_request_sock *ireq = inet_rsk(req);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (reqnum < s_reqnum)
549 continue;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700550 if (r->id.tcpdiag_dport != ireq->rmt_port &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 r->id.tcpdiag_dport)
552 continue;
553
554 if (bc) {
555 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300556#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700558 tcp6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700560 &ireq->loc_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300562#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700564 tcp6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700566 &ireq->rmt_addr;
567 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (!tcpdiag_bc_run(RTA_DATA(bc),
570 RTA_PAYLOAD(bc), &entry))
571 continue;
572 }
573
574 err = tcpdiag_fill_req(skb, sk, req,
575 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300576 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (err < 0) {
578 cb->args[3] = j + 1;
579 cb->args[4] = reqnum;
580 goto out;
581 }
582 }
583
584 s_reqnum = 0;
585 }
586
587out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700588 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 return err;
591}
592
593static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
594{
595 int i, num;
596 int s_i, s_num;
597 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300598 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300599 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300601 handler = inet_diag_table[cb->nlh->nlmsg_type];
602 BUG_ON(handler == NULL);
603 hashinfo = handler->idiag_hashinfo;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 s_i = cb->args[1];
606 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (cb->args[0] == 0) {
609 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
610 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300611
612 inet_listen_lock(hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700613 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct sock *sk;
615 struct hlist_node *node;
616
617 num = 0;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300618 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct inet_sock *inet = inet_sk(sk);
620
621 if (num < s_num) {
622 num++;
623 continue;
624 }
625
626 if (r->id.tcpdiag_sport != inet->sport &&
627 r->id.tcpdiag_sport)
628 goto next_listen;
629
630 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
631 r->id.tcpdiag_dport ||
632 cb->args[3] > 0)
633 goto syn_recv;
634
635 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300636 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto done;
638 }
639
640syn_recv:
641 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
642 goto next_listen;
643
644 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300645 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto done;
647 }
648
649next_listen:
650 cb->args[3] = 0;
651 cb->args[4] = 0;
652 ++num;
653 }
654
655 s_num = 0;
656 cb->args[3] = 0;
657 cb->args[4] = 0;
658 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300659 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660skip_listen_ht:
661 cb->args[0] = 1;
662 s_i = num = s_num = 0;
663 }
664
665 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
666 return skb->len;
667
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300668 for (i = s_i; i < hashinfo->ehash_size; i++) {
669 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct sock *sk;
671 struct hlist_node *node;
672
673 if (i > s_i)
674 s_num = 0;
675
676 read_lock_bh(&head->lock);
677
678 num = 0;
679 sk_for_each(sk, node, &head->chain) {
680 struct inet_sock *inet = inet_sk(sk);
681
682 if (num < s_num)
683 goto next_normal;
684 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
685 goto next_normal;
686 if (r->id.tcpdiag_sport != inet->sport &&
687 r->id.tcpdiag_sport)
688 goto next_normal;
689 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
690 goto next_normal;
691 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
692 read_unlock_bh(&head->lock);
693 goto done;
694 }
695next_normal:
696 ++num;
697 }
698
699 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
700 sk_for_each(sk, node,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300701 &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct inet_sock *inet = inet_sk(sk);
703
704 if (num < s_num)
705 goto next_dying;
706 if (r->id.tcpdiag_sport != inet->sport &&
707 r->id.tcpdiag_sport)
708 goto next_dying;
709 if (r->id.tcpdiag_dport != inet->dport &&
710 r->id.tcpdiag_dport)
711 goto next_dying;
712 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
713 read_unlock_bh(&head->lock);
714 goto done;
715 }
716next_dying:
717 ++num;
718 }
719 }
720 read_unlock_bh(&head->lock);
721 }
722
723done:
724 cb->args[1] = i;
725 cb->args[2] = num;
726 return skb->len;
727}
728
729static int tcpdiag_dump_done(struct netlink_callback *cb)
730{
731 return 0;
732}
733
734
735static __inline__ int
736tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
737{
738 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
739 return 0;
740
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300741 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto err_inval;
743
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300744 if (inet_diag_table[nlh->nlmsg_type] == NULL)
745 return -ENOENT;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
748 goto err_inval;
749
750 if (nlh->nlmsg_flags&NLM_F_DUMP) {
751 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
752 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
753 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
754 rta->rta_len < 8 ||
755 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
756 goto err_inval;
757 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
758 goto err_inval;
759 }
760 return netlink_dump_start(tcpnl, skb, nlh,
761 tcpdiag_dump,
762 tcpdiag_dump_done);
763 } else {
764 return tcpdiag_get_exact(skb, nlh);
765 }
766
767err_inval:
768 return -EINVAL;
769}
770
771
772static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
773{
774 int err;
775 struct nlmsghdr * nlh;
776
777 if (skb->len >= NLMSG_SPACE(0)) {
778 nlh = (struct nlmsghdr *)skb->data;
779 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
780 return;
781 err = tcpdiag_rcv_msg(skb, nlh);
782 if (err || nlh->nlmsg_flags & NLM_F_ACK)
783 netlink_ack(skb, nlh, err);
784 }
785}
786
787static void tcpdiag_rcv(struct sock *sk, int len)
788{
789 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700790 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700792 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 tcpdiag_rcv_skb(skb);
794 kfree_skb(skb);
795 }
796}
797
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300798static void tcp_diag_get_info(struct sock *sk, struct tcpdiagmsg *r,
799 void *_info)
800{
801 const struct tcp_sock *tp = tcp_sk(sk);
802 struct tcp_info *info = _info;
803
804 r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
805 r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
806 if (info != NULL)
807 tcp_get_info(sk, info);
808}
809
810static struct inet_diag_handler tcp_diag_handler = {
811 .idiag_hashinfo = &tcp_hashinfo,
812 .idiag_get_info = tcp_diag_get_info,
813 .idiag_type = TCPDIAG_GETSOCK,
814 .idiag_info_size = sizeof(struct tcp_info),
815};
816
817static DEFINE_SPINLOCK(inet_diag_register_lock);
818
819int inet_diag_register(const struct inet_diag_handler *h)
820{
821 const __u16 type = h->idiag_type;
822 int err = -EINVAL;
823
824 if (type >= INET_DIAG_GETSOCK_MAX)
825 goto out;
826
827 spin_lock(&inet_diag_register_lock);
828 err = -EEXIST;
829 if (inet_diag_table[type] == NULL) {
830 inet_diag_table[type] = h;
831 err = 0;
832 }
833 spin_unlock(&inet_diag_register_lock);
834out:
835 return err;
836}
837EXPORT_SYMBOL_GPL(inet_diag_register);
838
839void inet_diag_unregister(const struct inet_diag_handler *h)
840{
841 const __u16 type = h->idiag_type;
842
843 if (type >= INET_DIAG_GETSOCK_MAX)
844 return;
845
846 spin_lock(&inet_diag_register_lock);
847 inet_diag_table[type] = NULL;
848 spin_unlock(&inet_diag_register_lock);
849
850 synchronize_rcu();
851}
852EXPORT_SYMBOL_GPL(inet_diag_unregister);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static int __init tcpdiag_init(void)
855{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300856 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
857 sizeof(struct inet_diag_handler *));
858 int err = -ENOMEM;
859
860 inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
861 if (!inet_diag_table)
862 goto out;
863
864 memset(inet_diag_table, 0, inet_diag_table_size);
865
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700866 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv,
867 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (tcpnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300869 goto out_free_table;
870
871 err = inet_diag_register(&tcp_diag_handler);
872 if (err)
873 goto out_sock_release;
874out:
875 return err;
876out_sock_release:
877 sock_release(tcpnl->sk_socket);
878out_free_table:
879 kfree(inet_diag_table);
880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void __exit tcpdiag_exit(void)
884{
885 sock_release(tcpnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300886 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889module_init(tcpdiag_init);
890module_exit(tcpdiag_exit);
891MODULE_LICENSE("GPL");