Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This is a module which is used for queueing IPv4 packets and | |
3 | * communicating with userspace via netlink. | |
4 | * | |
5 | * (C) 2000-2002 James Morris <jmorris@intercode.com.au> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * 2000-03-27: Simplified code (thanks to Andi Kleen for clues). | |
12 | * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report). | |
13 | * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian | |
14 | * Zander). | |
15 | * 2000-08-01: Added Nick Williams' MAC support. | |
16 | * 2002-06-25: Code cleanup. | |
17 | * 2005-01-10: Added /proc counter for dropped packets; fixed so | |
18 | * packets aren't delivered to user space if they're going | |
19 | * to be dropped. | |
20 | * | |
21 | */ | |
22 | #include <linux/module.h> | |
23 | #include <linux/skbuff.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/ip.h> | |
26 | #include <linux/notifier.h> | |
27 | #include <linux/netdevice.h> | |
28 | #include <linux/netfilter.h> | |
29 | #include <linux/netfilter_ipv4/ip_queue.h> | |
30 | #include <linux/netfilter_ipv4/ip_tables.h> | |
31 | #include <linux/netlink.h> | |
32 | #include <linux/spinlock.h> | |
33 | #include <linux/sysctl.h> | |
34 | #include <linux/proc_fs.h> | |
35 | #include <linux/security.h> | |
36 | #include <net/sock.h> | |
37 | #include <net/route.h> | |
38 | ||
39 | #define IPQ_QMAX_DEFAULT 1024 | |
40 | #define IPQ_PROC_FS_NAME "ip_queue" | |
41 | #define NET_IPQ_QMAX 2088 | |
42 | #define NET_IPQ_QMAX_NAME "ip_queue_maxlen" | |
43 | ||
44 | struct ipq_rt_info { | |
45 | __u8 tos; | |
46 | __u32 daddr; | |
47 | __u32 saddr; | |
48 | }; | |
49 | ||
50 | struct ipq_queue_entry { | |
51 | struct list_head list; | |
52 | struct nf_info *info; | |
53 | struct sk_buff *skb; | |
54 | struct ipq_rt_info rt_info; | |
55 | }; | |
56 | ||
57 | typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long); | |
58 | ||
59 | static unsigned char copy_mode = IPQ_COPY_NONE; | |
60 | static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT; | |
61 | static DEFINE_RWLOCK(queue_lock); | |
62 | static int peer_pid; | |
63 | static unsigned int copy_range; | |
64 | static unsigned int queue_total; | |
65 | static unsigned int queue_dropped = 0; | |
66 | static unsigned int queue_user_dropped = 0; | |
67 | static struct sock *ipqnl; | |
68 | static LIST_HEAD(queue_list); | |
69 | static DECLARE_MUTEX(ipqnl_sem); | |
70 | ||
71 | static void | |
72 | ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict) | |
73 | { | |
74 | nf_reinject(entry->skb, entry->info, verdict); | |
75 | kfree(entry); | |
76 | } | |
77 | ||
78 | static inline void | |
79 | __ipq_enqueue_entry(struct ipq_queue_entry *entry) | |
80 | { | |
81 | list_add(&entry->list, &queue_list); | |
82 | queue_total++; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Find and return a queued entry matched by cmpfn, or return the last | |
87 | * entry if cmpfn is NULL. | |
88 | */ | |
89 | static inline struct ipq_queue_entry * | |
90 | __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data) | |
91 | { | |
92 | struct list_head *p; | |
93 | ||
94 | list_for_each_prev(p, &queue_list) { | |
95 | struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p; | |
96 | ||
97 | if (!cmpfn || cmpfn(entry, data)) | |
98 | return entry; | |
99 | } | |
100 | return NULL; | |
101 | } | |
102 | ||
103 | static inline void | |
104 | __ipq_dequeue_entry(struct ipq_queue_entry *entry) | |
105 | { | |
106 | list_del(&entry->list); | |
107 | queue_total--; | |
108 | } | |
109 | ||
110 | static inline struct ipq_queue_entry * | |
111 | __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data) | |
112 | { | |
113 | struct ipq_queue_entry *entry; | |
114 | ||
115 | entry = __ipq_find_entry(cmpfn, data); | |
116 | if (entry == NULL) | |
117 | return NULL; | |
118 | ||
119 | __ipq_dequeue_entry(entry); | |
120 | return entry; | |
121 | } | |
122 | ||
123 | ||
124 | static inline void | |
125 | __ipq_flush(int verdict) | |
126 | { | |
127 | struct ipq_queue_entry *entry; | |
128 | ||
129 | while ((entry = __ipq_find_dequeue_entry(NULL, 0))) | |
130 | ipq_issue_verdict(entry, verdict); | |
131 | } | |
132 | ||
133 | static inline int | |
134 | __ipq_set_mode(unsigned char mode, unsigned int range) | |
135 | { | |
136 | int status = 0; | |
137 | ||
138 | switch(mode) { | |
139 | case IPQ_COPY_NONE: | |
140 | case IPQ_COPY_META: | |
141 | copy_mode = mode; | |
142 | copy_range = 0; | |
143 | break; | |
144 | ||
145 | case IPQ_COPY_PACKET: | |
146 | copy_mode = mode; | |
147 | copy_range = range; | |
148 | if (copy_range > 0xFFFF) | |
149 | copy_range = 0xFFFF; | |
150 | break; | |
151 | ||
152 | default: | |
153 | status = -EINVAL; | |
154 | ||
155 | } | |
156 | return status; | |
157 | } | |
158 | ||
159 | static inline void | |
160 | __ipq_reset(void) | |
161 | { | |
162 | peer_pid = 0; | |
163 | net_disable_timestamp(); | |
164 | __ipq_set_mode(IPQ_COPY_NONE, 0); | |
165 | __ipq_flush(NF_DROP); | |
166 | } | |
167 | ||
168 | static struct ipq_queue_entry * | |
169 | ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data) | |
170 | { | |
171 | struct ipq_queue_entry *entry; | |
172 | ||
173 | write_lock_bh(&queue_lock); | |
174 | entry = __ipq_find_dequeue_entry(cmpfn, data); | |
175 | write_unlock_bh(&queue_lock); | |
176 | return entry; | |
177 | } | |
178 | ||
179 | static void | |
180 | ipq_flush(int verdict) | |
181 | { | |
182 | write_lock_bh(&queue_lock); | |
183 | __ipq_flush(verdict); | |
184 | write_unlock_bh(&queue_lock); | |
185 | } | |
186 | ||
187 | static struct sk_buff * | |
188 | ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp) | |
189 | { | |
190 | unsigned char *old_tail; | |
191 | size_t size = 0; | |
192 | size_t data_len = 0; | |
193 | struct sk_buff *skb; | |
194 | struct ipq_packet_msg *pmsg; | |
195 | struct nlmsghdr *nlh; | |
196 | ||
197 | read_lock_bh(&queue_lock); | |
198 | ||
199 | switch (copy_mode) { | |
200 | case IPQ_COPY_META: | |
201 | case IPQ_COPY_NONE: | |
202 | size = NLMSG_SPACE(sizeof(*pmsg)); | |
203 | data_len = 0; | |
204 | break; | |
205 | ||
206 | case IPQ_COPY_PACKET: | |
207 | if (copy_range == 0 || copy_range > entry->skb->len) | |
208 | data_len = entry->skb->len; | |
209 | else | |
210 | data_len = copy_range; | |
211 | ||
212 | size = NLMSG_SPACE(sizeof(*pmsg) + data_len); | |
213 | break; | |
214 | ||
215 | default: | |
216 | *errp = -EINVAL; | |
217 | read_unlock_bh(&queue_lock); | |
218 | return NULL; | |
219 | } | |
220 | ||
221 | read_unlock_bh(&queue_lock); | |
222 | ||
223 | skb = alloc_skb(size, GFP_ATOMIC); | |
224 | if (!skb) | |
225 | goto nlmsg_failure; | |
226 | ||
227 | old_tail= skb->tail; | |
228 | nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh)); | |
229 | pmsg = NLMSG_DATA(nlh); | |
230 | memset(pmsg, 0, sizeof(*pmsg)); | |
231 | ||
232 | pmsg->packet_id = (unsigned long )entry; | |
233 | pmsg->data_len = data_len; | |
234 | pmsg->timestamp_sec = entry->skb->stamp.tv_sec; | |
235 | pmsg->timestamp_usec = entry->skb->stamp.tv_usec; | |
236 | pmsg->mark = entry->skb->nfmark; | |
237 | pmsg->hook = entry->info->hook; | |
238 | pmsg->hw_protocol = entry->skb->protocol; | |
239 | ||
240 | if (entry->info->indev) | |
241 | strcpy(pmsg->indev_name, entry->info->indev->name); | |
242 | else | |
243 | pmsg->indev_name[0] = '\0'; | |
244 | ||
245 | if (entry->info->outdev) | |
246 | strcpy(pmsg->outdev_name, entry->info->outdev->name); | |
247 | else | |
248 | pmsg->outdev_name[0] = '\0'; | |
249 | ||
250 | if (entry->info->indev && entry->skb->dev) { | |
251 | pmsg->hw_type = entry->skb->dev->type; | |
252 | if (entry->skb->dev->hard_header_parse) | |
253 | pmsg->hw_addrlen = | |
254 | entry->skb->dev->hard_header_parse(entry->skb, | |
255 | pmsg->hw_addr); | |
256 | } | |
257 | ||
258 | if (data_len) | |
259 | if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len)) | |
260 | BUG(); | |
261 | ||
262 | nlh->nlmsg_len = skb->tail - old_tail; | |
263 | return skb; | |
264 | ||
265 | nlmsg_failure: | |
266 | if (skb) | |
267 | kfree_skb(skb); | |
268 | *errp = -EINVAL; | |
269 | printk(KERN_ERR "ip_queue: error creating packet message\n"); | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | static int | |
274 | ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data) | |
275 | { | |
276 | int status = -EINVAL; | |
277 | struct sk_buff *nskb; | |
278 | struct ipq_queue_entry *entry; | |
279 | ||
280 | if (copy_mode == IPQ_COPY_NONE) | |
281 | return -EAGAIN; | |
282 | ||
283 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); | |
284 | if (entry == NULL) { | |
285 | printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n"); | |
286 | return -ENOMEM; | |
287 | } | |
288 | ||
289 | entry->info = info; | |
290 | entry->skb = skb; | |
291 | ||
292 | if (entry->info->hook == NF_IP_LOCAL_OUT) { | |
293 | struct iphdr *iph = skb->nh.iph; | |
294 | ||
295 | entry->rt_info.tos = iph->tos; | |
296 | entry->rt_info.daddr = iph->daddr; | |
297 | entry->rt_info.saddr = iph->saddr; | |
298 | } | |
299 | ||
300 | nskb = ipq_build_packet_message(entry, &status); | |
301 | if (nskb == NULL) | |
302 | goto err_out_free; | |
303 | ||
304 | write_lock_bh(&queue_lock); | |
305 | ||
306 | if (!peer_pid) | |
307 | goto err_out_free_nskb; | |
308 | ||
309 | if (queue_total >= queue_maxlen) { | |
310 | queue_dropped++; | |
311 | status = -ENOSPC; | |
312 | if (net_ratelimit()) | |
313 | printk (KERN_WARNING "ip_queue: full at %d entries, " | |
314 | "dropping packets(s). Dropped: %d\n", queue_total, | |
315 | queue_dropped); | |
316 | goto err_out_free_nskb; | |
317 | } | |
318 | ||
319 | /* netlink_unicast will either free the nskb or attach it to a socket */ | |
320 | status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT); | |
321 | if (status < 0) { | |
322 | queue_user_dropped++; | |
323 | goto err_out_unlock; | |
324 | } | |
325 | ||
326 | __ipq_enqueue_entry(entry); | |
327 | ||
328 | write_unlock_bh(&queue_lock); | |
329 | return status; | |
330 | ||
331 | err_out_free_nskb: | |
332 | kfree_skb(nskb); | |
333 | ||
334 | err_out_unlock: | |
335 | write_unlock_bh(&queue_lock); | |
336 | ||
337 | err_out_free: | |
338 | kfree(entry); | |
339 | return status; | |
340 | } | |
341 | ||
342 | static int | |
343 | ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e) | |
344 | { | |
345 | int diff; | |
346 | struct iphdr *user_iph = (struct iphdr *)v->payload; | |
347 | ||
348 | if (v->data_len < sizeof(*user_iph)) | |
349 | return 0; | |
350 | diff = v->data_len - e->skb->len; | |
351 | if (diff < 0) | |
352 | skb_trim(e->skb, v->data_len); | |
353 | else if (diff > 0) { | |
354 | if (v->data_len > 0xFFFF) | |
355 | return -EINVAL; | |
356 | if (diff > skb_tailroom(e->skb)) { | |
357 | struct sk_buff *newskb; | |
358 | ||
359 | newskb = skb_copy_expand(e->skb, | |
360 | skb_headroom(e->skb), | |
361 | diff, | |
362 | GFP_ATOMIC); | |
363 | if (newskb == NULL) { | |
364 | printk(KERN_WARNING "ip_queue: OOM " | |
365 | "in mangle, dropping packet\n"); | |
366 | return -ENOMEM; | |
367 | } | |
368 | if (e->skb->sk) | |
369 | skb_set_owner_w(newskb, e->skb->sk); | |
370 | kfree_skb(e->skb); | |
371 | e->skb = newskb; | |
372 | } | |
373 | skb_put(e->skb, diff); | |
374 | } | |
375 | if (!skb_ip_make_writable(&e->skb, v->data_len)) | |
376 | return -ENOMEM; | |
377 | memcpy(e->skb->data, v->payload, v->data_len); | |
378 | e->skb->nfcache |= NFC_ALTERED; | |
379 | ||
380 | /* | |
381 | * Extra routing may needed on local out, as the QUEUE target never | |
382 | * returns control to the table. | |
383 | */ | |
384 | if (e->info->hook == NF_IP_LOCAL_OUT) { | |
385 | struct iphdr *iph = e->skb->nh.iph; | |
386 | ||
387 | if (!(iph->tos == e->rt_info.tos | |
388 | && iph->daddr == e->rt_info.daddr | |
389 | && iph->saddr == e->rt_info.saddr)) | |
390 | return ip_route_me_harder(&e->skb); | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
395 | static inline int | |
396 | id_cmp(struct ipq_queue_entry *e, unsigned long id) | |
397 | { | |
398 | return (id == (unsigned long )e); | |
399 | } | |
400 | ||
401 | static int | |
402 | ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len) | |
403 | { | |
404 | struct ipq_queue_entry *entry; | |
405 | ||
406 | if (vmsg->value > NF_MAX_VERDICT) | |
407 | return -EINVAL; | |
408 | ||
409 | entry = ipq_find_dequeue_entry(id_cmp, vmsg->id); | |
410 | if (entry == NULL) | |
411 | return -ENOENT; | |
412 | else { | |
413 | int verdict = vmsg->value; | |
414 | ||
415 | if (vmsg->data_len && vmsg->data_len == len) | |
416 | if (ipq_mangle_ipv4(vmsg, entry) < 0) | |
417 | verdict = NF_DROP; | |
418 | ||
419 | ipq_issue_verdict(entry, verdict); | |
420 | return 0; | |
421 | } | |
422 | } | |
423 | ||
424 | static int | |
425 | ipq_set_mode(unsigned char mode, unsigned int range) | |
426 | { | |
427 | int status; | |
428 | ||
429 | write_lock_bh(&queue_lock); | |
430 | status = __ipq_set_mode(mode, range); | |
431 | write_unlock_bh(&queue_lock); | |
432 | return status; | |
433 | } | |
434 | ||
435 | static int | |
436 | ipq_receive_peer(struct ipq_peer_msg *pmsg, | |
437 | unsigned char type, unsigned int len) | |
438 | { | |
439 | int status = 0; | |
440 | ||
441 | if (len < sizeof(*pmsg)) | |
442 | return -EINVAL; | |
443 | ||
444 | switch (type) { | |
445 | case IPQM_MODE: | |
446 | status = ipq_set_mode(pmsg->msg.mode.value, | |
447 | pmsg->msg.mode.range); | |
448 | break; | |
449 | ||
450 | case IPQM_VERDICT: | |
451 | if (pmsg->msg.verdict.value > NF_MAX_VERDICT) | |
452 | status = -EINVAL; | |
453 | else | |
454 | status = ipq_set_verdict(&pmsg->msg.verdict, | |
455 | len - sizeof(*pmsg)); | |
456 | break; | |
457 | default: | |
458 | status = -EINVAL; | |
459 | } | |
460 | return status; | |
461 | } | |
462 | ||
463 | static int | |
464 | dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex) | |
465 | { | |
466 | if (entry->info->indev) | |
467 | if (entry->info->indev->ifindex == ifindex) | |
468 | return 1; | |
469 | ||
470 | if (entry->info->outdev) | |
471 | if (entry->info->outdev->ifindex == ifindex) | |
472 | return 1; | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | static void | |
478 | ipq_dev_drop(int ifindex) | |
479 | { | |
480 | struct ipq_queue_entry *entry; | |
481 | ||
482 | while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL) | |
483 | ipq_issue_verdict(entry, NF_DROP); | |
484 | } | |
485 | ||
486 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) | |
487 | ||
488 | static inline void | |
489 | ipq_rcv_skb(struct sk_buff *skb) | |
490 | { | |
491 | int status, type, pid, flags, nlmsglen, skblen; | |
492 | struct nlmsghdr *nlh; | |
493 | ||
494 | skblen = skb->len; | |
495 | if (skblen < sizeof(*nlh)) | |
496 | return; | |
497 | ||
498 | nlh = (struct nlmsghdr *)skb->data; | |
499 | nlmsglen = nlh->nlmsg_len; | |
500 | if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen) | |
501 | return; | |
502 | ||
503 | pid = nlh->nlmsg_pid; | |
504 | flags = nlh->nlmsg_flags; | |
505 | ||
506 | if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI) | |
507 | RCV_SKB_FAIL(-EINVAL); | |
508 | ||
509 | if (flags & MSG_TRUNC) | |
510 | RCV_SKB_FAIL(-ECOMM); | |
511 | ||
512 | type = nlh->nlmsg_type; | |
513 | if (type < NLMSG_NOOP || type >= IPQM_MAX) | |
514 | RCV_SKB_FAIL(-EINVAL); | |
515 | ||
516 | if (type <= IPQM_BASE) | |
517 | return; | |
518 | ||
519 | if (security_netlink_recv(skb)) | |
520 | RCV_SKB_FAIL(-EPERM); | |
521 | ||
522 | write_lock_bh(&queue_lock); | |
523 | ||
524 | if (peer_pid) { | |
525 | if (peer_pid != pid) { | |
526 | write_unlock_bh(&queue_lock); | |
527 | RCV_SKB_FAIL(-EBUSY); | |
528 | } | |
529 | } else { | |
530 | net_enable_timestamp(); | |
531 | peer_pid = pid; | |
532 | } | |
533 | ||
534 | write_unlock_bh(&queue_lock); | |
535 | ||
536 | status = ipq_receive_peer(NLMSG_DATA(nlh), type, | |
537 | skblen - NLMSG_LENGTH(0)); | |
538 | if (status < 0) | |
539 | RCV_SKB_FAIL(status); | |
540 | ||
541 | if (flags & NLM_F_ACK) | |
542 | netlink_ack(skb, nlh, 0); | |
543 | return; | |
544 | } | |
545 | ||
546 | static void | |
547 | ipq_rcv_sk(struct sock *sk, int len) | |
548 | { | |
2a0a6ebe HX |
549 | struct sk_buff *skb; |
550 | unsigned int qlen; | |
1da177e4 | 551 | |
2a0a6ebe | 552 | down(&ipqnl_sem); |
1da177e4 | 553 | |
2a0a6ebe HX |
554 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
555 | skb = skb_dequeue(&sk->sk_receive_queue); | |
556 | ipq_rcv_skb(skb); | |
557 | kfree_skb(skb); | |
558 | } | |
1da177e4 | 559 | |
2a0a6ebe | 560 | up(&ipqnl_sem); |
1da177e4 LT |
561 | } |
562 | ||
563 | static int | |
564 | ipq_rcv_dev_event(struct notifier_block *this, | |
565 | unsigned long event, void *ptr) | |
566 | { | |
567 | struct net_device *dev = ptr; | |
568 | ||
569 | /* Drop any packets associated with the downed device */ | |
570 | if (event == NETDEV_DOWN) | |
571 | ipq_dev_drop(dev->ifindex); | |
572 | return NOTIFY_DONE; | |
573 | } | |
574 | ||
575 | static struct notifier_block ipq_dev_notifier = { | |
576 | .notifier_call = ipq_rcv_dev_event, | |
577 | }; | |
578 | ||
579 | static int | |
580 | ipq_rcv_nl_event(struct notifier_block *this, | |
581 | unsigned long event, void *ptr) | |
582 | { | |
583 | struct netlink_notify *n = ptr; | |
584 | ||
585 | if (event == NETLINK_URELEASE && | |
586 | n->protocol == NETLINK_FIREWALL && n->pid) { | |
587 | write_lock_bh(&queue_lock); | |
588 | if (n->pid == peer_pid) | |
589 | __ipq_reset(); | |
590 | write_unlock_bh(&queue_lock); | |
591 | } | |
592 | return NOTIFY_DONE; | |
593 | } | |
594 | ||
595 | static struct notifier_block ipq_nl_notifier = { | |
596 | .notifier_call = ipq_rcv_nl_event, | |
597 | }; | |
598 | ||
599 | static struct ctl_table_header *ipq_sysctl_header; | |
600 | ||
601 | static ctl_table ipq_table[] = { | |
602 | { | |
603 | .ctl_name = NET_IPQ_QMAX, | |
604 | .procname = NET_IPQ_QMAX_NAME, | |
605 | .data = &queue_maxlen, | |
606 | .maxlen = sizeof(queue_maxlen), | |
607 | .mode = 0644, | |
608 | .proc_handler = proc_dointvec | |
609 | }, | |
610 | { .ctl_name = 0 } | |
611 | }; | |
612 | ||
613 | static ctl_table ipq_dir_table[] = { | |
614 | { | |
615 | .ctl_name = NET_IPV4, | |
616 | .procname = "ipv4", | |
617 | .mode = 0555, | |
618 | .child = ipq_table | |
619 | }, | |
620 | { .ctl_name = 0 } | |
621 | }; | |
622 | ||
623 | static ctl_table ipq_root_table[] = { | |
624 | { | |
625 | .ctl_name = CTL_NET, | |
626 | .procname = "net", | |
627 | .mode = 0555, | |
628 | .child = ipq_dir_table | |
629 | }, | |
630 | { .ctl_name = 0 } | |
631 | }; | |
632 | ||
633 | #ifdef CONFIG_PROC_FS | |
634 | static int | |
635 | ipq_get_info(char *buffer, char **start, off_t offset, int length) | |
636 | { | |
637 | int len; | |
638 | ||
639 | read_lock_bh(&queue_lock); | |
640 | ||
641 | len = sprintf(buffer, | |
642 | "Peer PID : %d\n" | |
643 | "Copy mode : %hu\n" | |
644 | "Copy range : %u\n" | |
645 | "Queue length : %u\n" | |
646 | "Queue max. length : %u\n" | |
647 | "Queue dropped : %u\n" | |
648 | "Netlink dropped : %u\n", | |
649 | peer_pid, | |
650 | copy_mode, | |
651 | copy_range, | |
652 | queue_total, | |
653 | queue_maxlen, | |
654 | queue_dropped, | |
655 | queue_user_dropped); | |
656 | ||
657 | read_unlock_bh(&queue_lock); | |
658 | ||
659 | *start = buffer + offset; | |
660 | len -= offset; | |
661 | if (len > length) | |
662 | len = length; | |
663 | else if (len < 0) | |
664 | len = 0; | |
665 | return len; | |
666 | } | |
667 | #endif /* CONFIG_PROC_FS */ | |
668 | ||
669 | static int | |
670 | init_or_cleanup(int init) | |
671 | { | |
672 | int status = -ENOMEM; | |
673 | struct proc_dir_entry *proc; | |
674 | ||
675 | if (!init) | |
676 | goto cleanup; | |
677 | ||
678 | netlink_register_notifier(&ipq_nl_notifier); | |
679 | ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk); | |
680 | if (ipqnl == NULL) { | |
681 | printk(KERN_ERR "ip_queue: failed to create netlink socket\n"); | |
682 | goto cleanup_netlink_notifier; | |
683 | } | |
684 | ||
685 | proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info); | |
686 | if (proc) | |
687 | proc->owner = THIS_MODULE; | |
688 | else { | |
689 | printk(KERN_ERR "ip_queue: failed to create proc entry\n"); | |
690 | goto cleanup_ipqnl; | |
691 | } | |
692 | ||
693 | register_netdevice_notifier(&ipq_dev_notifier); | |
694 | ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0); | |
695 | ||
696 | status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL); | |
697 | if (status < 0) { | |
698 | printk(KERN_ERR "ip_queue: failed to register queue handler\n"); | |
699 | goto cleanup_sysctl; | |
700 | } | |
701 | return status; | |
702 | ||
703 | cleanup: | |
704 | nf_unregister_queue_handler(PF_INET); | |
705 | synchronize_net(); | |
706 | ipq_flush(NF_DROP); | |
707 | ||
708 | cleanup_sysctl: | |
709 | unregister_sysctl_table(ipq_sysctl_header); | |
710 | unregister_netdevice_notifier(&ipq_dev_notifier); | |
711 | proc_net_remove(IPQ_PROC_FS_NAME); | |
712 | ||
713 | cleanup_ipqnl: | |
714 | sock_release(ipqnl->sk_socket); | |
715 | down(&ipqnl_sem); | |
716 | up(&ipqnl_sem); | |
717 | ||
718 | cleanup_netlink_notifier: | |
719 | netlink_unregister_notifier(&ipq_nl_notifier); | |
720 | return status; | |
721 | } | |
722 | ||
723 | static int __init init(void) | |
724 | { | |
725 | ||
726 | return init_or_cleanup(1); | |
727 | } | |
728 | ||
729 | static void __exit fini(void) | |
730 | { | |
731 | init_or_cleanup(0); | |
732 | } | |
733 | ||
734 | MODULE_DESCRIPTION("IPv4 packet queue handler"); | |
735 | MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); | |
736 | MODULE_LICENSE("GPL"); | |
737 | ||
738 | module_init(init); | |
739 | module_exit(fini); |