]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * tcp_diag.c Module for monitoring TCP sockets. | |
3 | * | |
4 | * Version: $ | |
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 | ||
22 | #include <net/icmp.h> | |
23 | #include <net/tcp.h> | |
24 | #include <net/ipv6.h> | |
25 | #include <net/inet_common.h> | |
26 | ||
27 | #include <linux/inet.h> | |
28 | #include <linux/stddef.h> | |
29 | ||
30 | #include "tcp_diag.h" | |
31 | ||
32 | static struct sock *tcpnl; | |
33 | ||
34 | ||
35 | #define TCPDIAG_PUT(skb, attrtype, attrlen) \ | |
36 | ({ int rtalen = RTA_LENGTH(attrlen); \ | |
37 | struct rtattr *rta; \ | |
38 | if (skb_tailroom(skb) < RTA_ALIGN(rtalen)) goto nlmsg_failure; \ | |
39 | rta = (void*)__skb_put(skb, RTA_ALIGN(rtalen)); \ | |
40 | rta->rta_type = attrtype; \ | |
41 | rta->rta_len = rtalen; \ | |
42 | RTA_DATA(rta); }) | |
43 | ||
44 | static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk, | |
45 | int ext, u32 pid, u32 seq) | |
46 | { | |
47 | struct tcp_opt *tp = &sk->tp_pinfo.af_tcp; | |
48 | struct tcpdiagmsg *r; | |
49 | struct nlmsghdr *nlh; | |
50 | struct tcp_info *info = NULL; | |
51 | struct tcpdiag_meminfo *minfo = NULL; | |
52 | unsigned char *b = skb->tail; | |
53 | ||
54 | nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r)); | |
55 | r = NLMSG_DATA(nlh); | |
56 | if (sk->state != TCP_TIME_WAIT) { | |
57 | if (ext & (1<<(TCPDIAG_MEMINFO-1))) | |
58 | minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo)); | |
59 | if (ext & (1<<(TCPDIAG_INFO-1))) | |
60 | info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info)); | |
61 | } | |
62 | r->tcpdiag_family = sk->family; | |
63 | r->tcpdiag_state = sk->state; | |
64 | r->tcpdiag_timer = 0; | |
65 | r->tcpdiag_retrans = 0; | |
66 | ||
67 | r->id.tcpdiag_sport = sk->sport; | |
68 | r->id.tcpdiag_dport = sk->dport; | |
69 | r->id.tcpdiag_src[0] = sk->rcv_saddr; | |
70 | r->id.tcpdiag_dst[0] = sk->daddr; | |
71 | r->id.tcpdiag_if = sk->bound_dev_if; | |
72 | *((struct sock **)&r->id.tcpdiag_cookie) = sk; | |
73 | ||
74 | if (r->tcpdiag_state == TCP_TIME_WAIT) { | |
75 | struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk; | |
76 | long tmo = tw->ttd - jiffies; | |
77 | if (tmo < 0) | |
78 | tmo = 0; | |
79 | ||
80 | r->tcpdiag_state = tw->substate; | |
81 | r->tcpdiag_timer = 3; | |
82 | r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ; | |
83 | r->tcpdiag_rqueue = 0; | |
84 | r->tcpdiag_wqueue = 0; | |
85 | r->tcpdiag_uid = 0; | |
86 | r->tcpdiag_inode = 0; | |
87 | #ifdef CONFIG_IPV6 | |
88 | if (r->tcpdiag_family == AF_INET6) { | |
89 | memcpy(r->id.tcpdiag_src, &tw->v6_rcv_saddr, 16); | |
90 | memcpy(r->id.tcpdiag_dst, &tw->v6_daddr, 16); | |
91 | } | |
92 | #endif | |
93 | nlh->nlmsg_len = skb->tail - b; | |
94 | return skb->len; | |
95 | } | |
96 | ||
97 | #ifdef CONFIG_IPV6 | |
98 | if (r->tcpdiag_family == AF_INET6) { | |
99 | memcpy(r->id.tcpdiag_src, &sk->net_pinfo.af_inet6.rcv_saddr, 16); | |
100 | memcpy(r->id.tcpdiag_dst, &sk->net_pinfo.af_inet6.daddr, 16); | |
101 | } | |
102 | #endif | |
103 | ||
104 | #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ | |
105 | ||
106 | if (tp->pending == TCP_TIME_RETRANS) { | |
107 | r->tcpdiag_timer = 1; | |
108 | r->tcpdiag_retrans = tp->retransmits; | |
109 | r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout); | |
110 | } else if (tp->pending == TCP_TIME_PROBE0) { | |
111 | r->tcpdiag_timer = 4; | |
112 | r->tcpdiag_retrans = tp->probes_out; | |
113 | r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout); | |
114 | } else if (timer_pending(&sk->timer)) { | |
115 | r->tcpdiag_timer = 2; | |
116 | r->tcpdiag_retrans = tp->probes_out; | |
117 | r->tcpdiag_expires = EXPIRES_IN_MS(sk->timer.expires); | |
118 | } else { | |
119 | r->tcpdiag_timer = 0; | |
120 | r->tcpdiag_expires = 0; | |
121 | } | |
122 | #undef EXPIRES_IN_MS | |
123 | ||
124 | r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq; | |
125 | r->tcpdiag_wqueue = tp->write_seq - tp->snd_una; | |
126 | r->tcpdiag_uid = sock_i_uid(sk); | |
127 | r->tcpdiag_inode = sock_i_ino(sk); | |
128 | ||
129 | if (minfo) { | |
130 | minfo->tcpdiag_rmem = atomic_read(&sk->rmem_alloc); | |
131 | minfo->tcpdiag_wmem = sk->wmem_queued; | |
132 | minfo->tcpdiag_fmem = sk->forward_alloc; | |
133 | minfo->tcpdiag_tmem = atomic_read(&sk->wmem_alloc); | |
134 | } | |
135 | ||
136 | if (info) { | |
137 | u32 now = tcp_time_stamp; | |
138 | ||
139 | info->tcpi_state = sk->state; | |
140 | info->tcpi_ca_state = tp->ca_state; | |
141 | info->tcpi_retransmits = tp->retransmits; | |
142 | info->tcpi_probes = tp->probes_out; | |
143 | info->tcpi_backoff = tp->backoff; | |
144 | info->tcpi_options = 0; | |
145 | if (tp->tstamp_ok) | |
146 | info->tcpi_options |= TCPI_OPT_TIMESTAMPS; | |
147 | if (tp->sack_ok) | |
148 | info->tcpi_options |= TCPI_OPT_SACK; | |
149 | if (tp->wscale_ok) { | |
150 | info->tcpi_options |= TCPI_OPT_WSCALE; | |
151 | info->tcpi_snd_wscale = tp->snd_wscale; | |
152 | info->tcpi_rcv_wscale = tp->rcv_wscale; | |
153 | } else { | |
154 | info->tcpi_snd_wscale = 0; | |
155 | info->tcpi_rcv_wscale = 0; | |
156 | } | |
157 | #ifdef CONFIG_INET_ECN | |
158 | if (tp->ecn_flags&TCP_ECN_OK) | |
159 | info->tcpi_options |= TCPI_OPT_ECN; | |
160 | #endif | |
161 | ||
162 | info->tcpi_rto = (1000000*tp->rto)/HZ; | |
163 | info->tcpi_ato = (1000000*tp->ack.ato)/HZ; | |
164 | info->tcpi_snd_mss = tp->mss_cache; | |
165 | info->tcpi_rcv_mss = tp->ack.rcv_mss; | |
166 | ||
167 | info->tcpi_unacked = tp->packets_out; | |
168 | info->tcpi_sacked = tp->sacked_out; | |
169 | info->tcpi_lost = tp->lost_out; | |
170 | info->tcpi_retrans = tp->retrans_out; | |
171 | info->tcpi_fackets = tp->fackets_out; | |
172 | ||
173 | info->tcpi_last_data_sent = ((now - tp->lsndtime)*1000)/HZ; | |
174 | info->tcpi_last_ack_sent = 0; | |
175 | info->tcpi_last_data_recv = ((now - tp->ack.lrcvtime)*1000)/HZ; | |
176 | info->tcpi_last_ack_recv = ((now - tp->rcv_tstamp)*1000)/HZ; | |
177 | ||
178 | info->tcpi_pmtu = tp->pmtu_cookie; | |
179 | info->tcpi_rcv_ssthresh = tp->rcv_ssthresh; | |
180 | info->tcpi_rtt = ((1000000*tp->srtt)/HZ)>>3; | |
181 | info->tcpi_rttvar = ((1000000*tp->mdev)/HZ)>>2; | |
182 | info->tcpi_snd_ssthresh = tp->snd_ssthresh; | |
183 | info->tcpi_snd_cwnd = tp->snd_cwnd; | |
184 | info->tcpi_advmss = tp->advmss; | |
185 | info->tcpi_reordering = tp->reordering; | |
186 | } | |
187 | ||
188 | nlh->nlmsg_len = skb->tail - b; | |
189 | return skb->len; | |
190 | ||
191 | nlmsg_failure: | |
192 | skb_trim(skb, b - skb->data); | |
193 | return -1; | |
194 | } | |
195 | ||
196 | extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif); | |
197 | #ifdef CONFIG_IPV6 | |
198 | extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport, | |
199 | struct in6_addr *daddr, u16 dport, | |
200 | int dif); | |
201 | #endif | |
202 | ||
203 | static int tcpdiag_get_exact(struct sk_buff *in_skb, struct nlmsghdr *nlh) | |
204 | { | |
205 | int err; | |
206 | struct sock *sk; | |
207 | struct tcpdiagreq *req = NLMSG_DATA(nlh); | |
208 | struct sk_buff *rep; | |
209 | ||
210 | if (req->tcpdiag_family == AF_INET) { | |
211 | sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport, | |
212 | req->id.tcpdiag_src[0], req->id.tcpdiag_sport, | |
213 | req->id.tcpdiag_if); | |
214 | } | |
215 | #ifdef CONFIG_IPV6 | |
216 | else if (req->tcpdiag_family == AF_INET6) { | |
217 | sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport, | |
218 | (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport, | |
219 | req->id.tcpdiag_if); | |
220 | } | |
221 | #endif | |
222 | else { | |
223 | return -EINVAL; | |
224 | } | |
225 | ||
226 | if (sk == NULL) | |
227 | return -ENOENT; | |
228 | ||
229 | err = -ESTALE; | |
230 | if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE || | |
231 | req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) && | |
232 | sk != *((struct sock **)&req->id.tcpdiag_cookie[0])) | |
233 | goto out; | |
234 | ||
235 | err = -ENOMEM; | |
236 | rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+ | |
237 | sizeof(struct tcpdiag_meminfo)+ | |
238 | sizeof(struct tcp_info)+64), GFP_KERNEL); | |
239 | if (!rep) | |
240 | goto out; | |
241 | ||
242 | if (tcpdiag_fill(rep, sk, req->tcpdiag_ext, | |
243 | NETLINK_CB(in_skb).pid, | |
244 | nlh->nlmsg_seq) <= 0) | |
245 | BUG(); | |
246 | ||
247 | err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); | |
248 | if (err > 0) | |
249 | err = 0; | |
250 | ||
251 | out: | |
252 | if (sk) { | |
253 | if (sk->state == TCP_TIME_WAIT) | |
254 | tcp_tw_put((struct tcp_tw_bucket*)sk); | |
255 | else | |
256 | sock_put(sk); | |
257 | } | |
258 | return err; | |
259 | } | |
260 | ||
261 | int bitstring_match(u32 *a1, 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 | ||
288 | int tcpdiag_bc_run(char *bc, int len, struct sock *sk) | |
289 | { | |
290 | while (len > 0) { | |
291 | int yes = 1; | |
292 | struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc; | |
293 | ||
294 | switch (op->code) { | |
295 | case TCPDIAG_BC_NOP: | |
296 | break; | |
297 | case TCPDIAG_BC_JMP: | |
298 | yes = 0; | |
299 | break; | |
300 | case TCPDIAG_BC_S_GE: | |
301 | yes = (sk->num >= op[1].no); | |
302 | break; | |
303 | case TCPDIAG_BC_S_LE: | |
304 | yes = (sk->num <= op[1].no); | |
305 | break; | |
306 | case TCPDIAG_BC_D_GE: | |
307 | yes = (ntohs(sk->dport) >= op[1].no); | |
308 | break; | |
309 | case TCPDIAG_BC_D_LE: | |
310 | yes = (ntohs(sk->dport) <= op[1].no); | |
311 | break; | |
312 | case TCPDIAG_BC_AUTO: | |
313 | yes = !(sk->userlocks&SOCK_BINDPORT_LOCK); | |
314 | break; | |
315 | case TCPDIAG_BC_S_COND: | |
316 | case TCPDIAG_BC_D_COND: | |
317 | { | |
318 | struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1); | |
319 | u32 *addr; | |
320 | ||
321 | if (cond->port != -1 && | |
322 | cond->port != (op->code == TCPDIAG_BC_S_COND ? sk->num : ntohs(sk->dport))) { | |
323 | yes = 0; | |
324 | break; | |
325 | } | |
326 | ||
327 | if (cond->prefix_len == 0) | |
328 | break; | |
329 | ||
330 | if (sk->family == AF_INET6) { | |
331 | if (op->code == TCPDIAG_BC_S_COND) | |
332 | addr = (u32*)&sk->net_pinfo.af_inet6.rcv_saddr; | |
333 | else | |
334 | addr = (u32*)&sk->net_pinfo.af_inet6.daddr; | |
335 | } else { | |
336 | if (op->code == TCPDIAG_BC_S_COND) | |
337 | addr = &sk->rcv_saddr; | |
338 | else | |
339 | addr = &sk->daddr; | |
340 | } | |
341 | ||
342 | if (bitstring_match(addr, cond->addr, cond->prefix_len)) | |
343 | break; | |
344 | if (sk->family == AF_INET6 && cond->family == AF_INET) { | |
345 | if (addr[0] == 0 && addr[1] == 0 && | |
346 | addr[2] == __constant_htonl(0xffff) && | |
347 | bitstring_match(addr+3, cond->addr, cond->prefix_len)) | |
348 | break; | |
349 | } | |
350 | yes = 0; | |
351 | break; | |
352 | } | |
353 | } | |
354 | ||
355 | if (yes) { | |
356 | len -= op->yes; | |
357 | bc += op->yes; | |
358 | } else { | |
359 | len -= op->no; | |
360 | bc += op->no; | |
361 | } | |
362 | } | |
363 | return (len == 0); | |
364 | } | |
365 | ||
366 | int valid_cc(char *bc, int len, int cc) | |
367 | { | |
368 | while (len >= 0) { | |
369 | struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc; | |
370 | ||
371 | if (cc > len) | |
372 | return 0; | |
373 | if (cc == len) | |
374 | return 1; | |
375 | if (op->yes < 4) | |
376 | return 0; | |
377 | len -= op->yes; | |
378 | bc += op->yes; | |
379 | } | |
380 | return 0; | |
381 | } | |
382 | ||
383 | int tcpdiag_bc_audit(char *bytecode, int bytecode_len) | |
384 | { | |
385 | char *bc = bytecode; | |
386 | int len = bytecode_len; | |
387 | ||
388 | while (len > 0) { | |
389 | struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc; | |
390 | ||
391 | //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); | |
392 | switch (op->code) { | |
393 | case TCPDIAG_BC_AUTO: | |
394 | case TCPDIAG_BC_S_COND: | |
395 | case TCPDIAG_BC_D_COND: | |
396 | case TCPDIAG_BC_S_GE: | |
397 | case TCPDIAG_BC_S_LE: | |
398 | case TCPDIAG_BC_D_GE: | |
399 | case TCPDIAG_BC_D_LE: | |
400 | if (op->yes < 4 || op->yes > len+4) | |
401 | return -EINVAL; | |
402 | case TCPDIAG_BC_JMP: | |
403 | if (op->no < 4 || op->no > len+4) | |
404 | return -EINVAL; | |
405 | if (op->no < len && | |
406 | !valid_cc(bytecode, bytecode_len, len-op->no)) | |
407 | return -EINVAL; | |
408 | break; | |
409 | case TCPDIAG_BC_NOP: | |
410 | if (op->yes < 4 || op->yes > len+4) | |
411 | return -EINVAL; | |
412 | break; | |
413 | default: | |
414 | return -EINVAL; | |
415 | } | |
416 | bc += op->yes; | |
417 | len -= op->yes; | |
418 | } | |
419 | return len == 0 ? 0 : -EINVAL; | |
420 | } | |
421 | ||
422 | ||
423 | int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
424 | { | |
425 | int i, num; | |
426 | int s_i, s_num; | |
427 | struct tcpdiagreq *r = NLMSG_DATA(cb->nlh); | |
428 | struct rtattr *bc = NULL; | |
429 | ||
430 | if (cb->nlh->nlmsg_len > 4+NLMSG_SPACE(sizeof(struct tcpdiagreq))) | |
431 | bc = (struct rtattr*)(r+1); | |
432 | ||
433 | s_i = cb->args[1]; | |
434 | s_num = num = cb->args[2]; | |
435 | ||
436 | if (cb->args[0] == 0) { | |
437 | if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV))) | |
438 | goto skip_listen_ht; | |
439 | tcp_listen_lock(); | |
440 | for (i = s_i; i < TCP_LHTABLE_SIZE; i++) { | |
441 | struct sock *sk = tcp_listening_hash[i]; | |
442 | ||
443 | if (i > s_i) | |
444 | s_num = 0; | |
445 | ||
446 | for (sk = tcp_listening_hash[i], num = 0; | |
447 | sk != NULL; | |
448 | sk = sk->next, num++) { | |
449 | if (num < s_num) | |
450 | continue; | |
451 | if (!(r->tcpdiag_states&TCPF_LISTEN) || | |
452 | r->id.tcpdiag_dport) | |
453 | continue; | |
454 | if (r->id.tcpdiag_sport != sk->sport && r->id.tcpdiag_sport) | |
455 | continue; | |
456 | if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk)) | |
457 | continue; | |
458 | if (tcpdiag_fill(skb, sk, r->tcpdiag_ext, | |
459 | NETLINK_CB(cb->skb).pid, | |
460 | cb->nlh->nlmsg_seq) <= 0) { | |
461 | tcp_listen_unlock(); | |
462 | goto done; | |
463 | } | |
464 | } | |
465 | } | |
466 | tcp_listen_unlock(); | |
467 | skip_listen_ht: | |
468 | cb->args[0] = 1; | |
469 | s_i = num = s_num = 0; | |
470 | } | |
471 | ||
472 | if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV))) | |
473 | return skb->len; | |
474 | ||
475 | for (i = s_i; i < tcp_ehash_size; i++) { | |
476 | struct tcp_ehash_bucket *head = &tcp_ehash[i]; | |
477 | struct sock *sk; | |
478 | ||
479 | if (i > s_i) | |
480 | s_num = 0; | |
481 | ||
482 | read_lock_bh(&head->lock); | |
483 | ||
484 | for (sk = head->chain, num = 0; | |
485 | sk != NULL; | |
486 | sk = sk->next, num++) { | |
487 | if (num < s_num) | |
488 | continue; | |
489 | if (!(r->tcpdiag_states&(1<<sk->state))) | |
490 | continue; | |
491 | if (r->id.tcpdiag_sport != sk->sport && r->id.tcpdiag_sport) | |
492 | continue; | |
493 | if (r->id.tcpdiag_dport != sk->dport && r->id.tcpdiag_dport) | |
494 | continue; | |
495 | if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk)) | |
496 | continue; | |
497 | if (tcpdiag_fill(skb, sk, r->tcpdiag_ext, | |
498 | NETLINK_CB(cb->skb).pid, | |
499 | cb->nlh->nlmsg_seq) <= 0) { | |
500 | read_unlock_bh(&head->lock); | |
501 | goto done; | |
502 | } | |
503 | } | |
504 | ||
505 | if (r->tcpdiag_states&TCPF_TIME_WAIT) { | |
506 | for (sk = tcp_ehash[i+tcp_ehash_size].chain; | |
507 | sk != NULL; | |
508 | sk = sk->next, num++) { | |
509 | if (num < s_num) | |
510 | continue; | |
511 | if (!(r->tcpdiag_states&(1<<sk->zapped))) | |
512 | continue; | |
513 | if (r->id.tcpdiag_sport != sk->sport && r->id.tcpdiag_sport) | |
514 | continue; | |
515 | if (r->id.tcpdiag_dport != sk->dport && r->id.tcpdiag_dport) | |
516 | continue; | |
517 | if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk)) | |
518 | continue; | |
519 | if (tcpdiag_fill(skb, sk, r->tcpdiag_ext, | |
520 | NETLINK_CB(cb->skb).pid, | |
521 | cb->nlh->nlmsg_seq) <= 0) { | |
522 | read_unlock_bh(&head->lock); | |
523 | goto done; | |
524 | } | |
525 | } | |
526 | } | |
527 | read_unlock_bh(&head->lock); | |
528 | } | |
529 | ||
530 | done: | |
531 | cb->args[1] = i; | |
532 | cb->args[2] = num; | |
533 | return skb->len; | |
534 | } | |
535 | ||
536 | static int tcpdiag_dump_done(struct netlink_callback *cb) | |
537 | { | |
538 | return 0; | |
539 | } | |
540 | ||
541 | ||
542 | static __inline__ int | |
543 | tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
544 | { | |
545 | if (!(nlh->nlmsg_flags&NLM_F_REQUEST)) | |
546 | return 0; | |
547 | ||
548 | if (nlh->nlmsg_type != TCPDIAG_GETSOCK) | |
549 | goto err_inval; | |
550 | ||
551 | if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len) | |
552 | goto err_inval; | |
553 | ||
554 | if (nlh->nlmsg_flags&NLM_F_DUMP) { | |
555 | if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) { | |
556 | struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq)); | |
557 | if (rta->rta_type != TCPDIAG_REQ_BYTECODE || | |
558 | rta->rta_len < 8 || | |
559 | rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq))) | |
560 | goto err_inval; | |
561 | if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta))) | |
562 | goto err_inval; | |
563 | } | |
564 | return netlink_dump_start(tcpnl, skb, nlh, | |
565 | tcpdiag_dump, | |
566 | tcpdiag_dump_done); | |
567 | } else { | |
568 | return tcpdiag_get_exact(skb, nlh); | |
569 | } | |
570 | ||
571 | err_inval: | |
572 | return -EINVAL; | |
573 | } | |
574 | ||
575 | ||
576 | extern __inline__ void tcpdiag_rcv_skb(struct sk_buff *skb) | |
577 | { | |
578 | int err; | |
579 | struct nlmsghdr * nlh; | |
580 | ||
581 | if (skb->len >= NLMSG_SPACE(0)) { | |
582 | nlh = (struct nlmsghdr *)skb->data; | |
583 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
584 | return; | |
585 | err = tcpdiag_rcv_msg(skb, nlh); | |
586 | if (err) | |
587 | netlink_ack(skb, nlh, err); | |
588 | } | |
589 | } | |
590 | ||
591 | static void tcpdiag_rcv(struct sock *sk, int len) | |
592 | { | |
593 | struct sk_buff *skb; | |
594 | ||
595 | while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) { | |
596 | tcpdiag_rcv_skb(skb); | |
597 | kfree_skb(skb); | |
598 | } | |
599 | } | |
600 | ||
601 | static int __init tcpdiag_init(void) | |
602 | { | |
603 | tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv); | |
604 | if (tcpnl == NULL) | |
605 | return -EBUSY; | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static void __exit tcpdiag_exit(void) | |
610 | { | |
611 | printk(KERN_INFO "Caution: unloading tcp_diag is not very well supported. Nothing to worry, but yet.\n"); | |
612 | if (tcpnl) | |
613 | sock_release(tcpnl->socket); | |
614 | } | |
615 | ||
616 | module_init(tcpdiag_init); | |
617 | module_exit(tcpdiag_exit); | |
618 | ||
619 | /* | |
620 | * Local variables: | |
621 | * compile-command: "gcc -DMOPS -DMODULE -D__KERNEL__ -I../include -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -c tcp_diag.c" | |
622 | * End: | |
623 | */ |