[NETFILTER]: nf_queue: move queueing related functions/struct to seperate header
[deliverable/linux.git] / net / ipv6 / netfilter / ip6_queue.c
1 /*
2 * This is a module which is used for queueing IPv6 packets and
3 * communicating with userspace via netlink.
4 *
5 * (C) 2001 Fernando Anton, this code is GPL.
6 * IPv64 Project - Work based in IPv64 draft by Arturo Azcorra.
7 * Universidad Carlos III de Madrid - Leganes (Madrid) - Spain
8 * Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain
9 * email: fanton@it.uc3m.es
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/init.h>
18 #include <linux/ipv6.h>
19 #include <linux/notifier.h>
20 #include <linux/netdevice.h>
21 #include <linux/netfilter.h>
22 #include <linux/netlink.h>
23 #include <linux/spinlock.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/ipv6.h>
31 #include <net/ip6_route.h>
32 #include <net/netfilter/nf_queue.h>
33 #include <linux/netfilter_ipv4/ip_queue.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
36
37 #define IPQ_QMAX_DEFAULT 1024
38 #define IPQ_PROC_FS_NAME "ip6_queue"
39 #define NET_IPQ_QMAX 2088
40 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
41
42 struct ipq_queue_entry {
43 struct list_head list;
44 struct nf_info *info;
45 struct sk_buff *skb;
46 };
47
48 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
49
50 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
51 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
52 static DEFINE_RWLOCK(queue_lock);
53 static int peer_pid __read_mostly;
54 static unsigned int copy_range __read_mostly;
55 static unsigned int queue_total;
56 static unsigned int queue_dropped = 0;
57 static unsigned int queue_user_dropped = 0;
58 static struct sock *ipqnl __read_mostly;
59 static LIST_HEAD(queue_list);
60 static DEFINE_MUTEX(ipqnl_mutex);
61
62 static void
63 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
64 {
65 local_bh_disable();
66 nf_reinject(entry->skb, entry->info, verdict);
67 local_bh_enable();
68 kfree(entry);
69 }
70
71 static inline void
72 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
73 {
74 list_add(&entry->list, &queue_list);
75 queue_total++;
76 }
77
78 /*
79 * Find and return a queued entry matched by cmpfn, or return the last
80 * entry if cmpfn is NULL.
81 */
82 static inline struct ipq_queue_entry *
83 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
84 {
85 struct list_head *p;
86
87 list_for_each_prev(p, &queue_list) {
88 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
89
90 if (!cmpfn || cmpfn(entry, data))
91 return entry;
92 }
93 return NULL;
94 }
95
96 static inline void
97 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
98 {
99 list_del(&entry->list);
100 queue_total--;
101 }
102
103 static inline struct ipq_queue_entry *
104 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
105 {
106 struct ipq_queue_entry *entry;
107
108 entry = __ipq_find_entry(cmpfn, data);
109 if (entry == NULL)
110 return NULL;
111
112 __ipq_dequeue_entry(entry);
113 return entry;
114 }
115
116
117 static inline void
118 __ipq_flush(int verdict)
119 {
120 struct ipq_queue_entry *entry;
121
122 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
123 ipq_issue_verdict(entry, verdict);
124 }
125
126 static inline int
127 __ipq_set_mode(unsigned char mode, unsigned int range)
128 {
129 int status = 0;
130
131 switch(mode) {
132 case IPQ_COPY_NONE:
133 case IPQ_COPY_META:
134 copy_mode = mode;
135 copy_range = 0;
136 break;
137
138 case IPQ_COPY_PACKET:
139 copy_mode = mode;
140 copy_range = range;
141 if (copy_range > 0xFFFF)
142 copy_range = 0xFFFF;
143 break;
144
145 default:
146 status = -EINVAL;
147
148 }
149 return status;
150 }
151
152 static inline void
153 __ipq_reset(void)
154 {
155 peer_pid = 0;
156 net_disable_timestamp();
157 __ipq_set_mode(IPQ_COPY_NONE, 0);
158 __ipq_flush(NF_DROP);
159 }
160
161 static struct ipq_queue_entry *
162 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
163 {
164 struct ipq_queue_entry *entry;
165
166 write_lock_bh(&queue_lock);
167 entry = __ipq_find_dequeue_entry(cmpfn, data);
168 write_unlock_bh(&queue_lock);
169 return entry;
170 }
171
172 static void
173 ipq_flush(int verdict)
174 {
175 write_lock_bh(&queue_lock);
176 __ipq_flush(verdict);
177 write_unlock_bh(&queue_lock);
178 }
179
180 static struct sk_buff *
181 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
182 {
183 sk_buff_data_t old_tail;
184 size_t size = 0;
185 size_t data_len = 0;
186 struct sk_buff *skb;
187 struct ipq_packet_msg *pmsg;
188 struct nlmsghdr *nlh;
189 struct timeval tv;
190
191 read_lock_bh(&queue_lock);
192
193 switch (copy_mode) {
194 case IPQ_COPY_META:
195 case IPQ_COPY_NONE:
196 size = NLMSG_SPACE(sizeof(*pmsg));
197 data_len = 0;
198 break;
199
200 case IPQ_COPY_PACKET:
201 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
202 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
203 (*errp = skb_checksum_help(entry->skb))) {
204 read_unlock_bh(&queue_lock);
205 return NULL;
206 }
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 tv = ktime_to_timeval(entry->skb->tstamp);
235 pmsg->timestamp_sec = tv.tv_sec;
236 pmsg->timestamp_usec = tv.tv_usec;
237 pmsg->mark = entry->skb->mark;
238 pmsg->hook = entry->info->hook;
239 pmsg->hw_protocol = entry->skb->protocol;
240
241 if (entry->info->indev)
242 strcpy(pmsg->indev_name, entry->info->indev->name);
243 else
244 pmsg->indev_name[0] = '\0';
245
246 if (entry->info->outdev)
247 strcpy(pmsg->outdev_name, entry->info->outdev->name);
248 else
249 pmsg->outdev_name[0] = '\0';
250
251 if (entry->info->indev && entry->skb->dev) {
252 pmsg->hw_type = entry->skb->dev->type;
253 pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
254 }
255
256 if (data_len)
257 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
258 BUG();
259
260 nlh->nlmsg_len = skb->tail - old_tail;
261 return skb;
262
263 nlmsg_failure:
264 if (skb)
265 kfree_skb(skb);
266 *errp = -EINVAL;
267 printk(KERN_ERR "ip6_queue: error creating packet message\n");
268 return NULL;
269 }
270
271 static int
272 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
273 unsigned int queuenum)
274 {
275 int status = -EINVAL;
276 struct sk_buff *nskb;
277 struct ipq_queue_entry *entry;
278
279 if (copy_mode == IPQ_COPY_NONE)
280 return -EAGAIN;
281
282 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
283 if (entry == NULL) {
284 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
285 return -ENOMEM;
286 }
287
288 entry->info = info;
289 entry->skb = skb;
290
291 nskb = ipq_build_packet_message(entry, &status);
292 if (nskb == NULL)
293 goto err_out_free;
294
295 write_lock_bh(&queue_lock);
296
297 if (!peer_pid)
298 goto err_out_free_nskb;
299
300 if (queue_total >= queue_maxlen) {
301 queue_dropped++;
302 status = -ENOSPC;
303 if (net_ratelimit())
304 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
305 "dropping packet(s). Dropped: %d\n", queue_total,
306 queue_dropped);
307 goto err_out_free_nskb;
308 }
309
310 /* netlink_unicast will either free the nskb or attach it to a socket */
311 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
312 if (status < 0) {
313 queue_user_dropped++;
314 goto err_out_unlock;
315 }
316
317 __ipq_enqueue_entry(entry);
318
319 write_unlock_bh(&queue_lock);
320 return status;
321
322 err_out_free_nskb:
323 kfree_skb(nskb);
324
325 err_out_unlock:
326 write_unlock_bh(&queue_lock);
327
328 err_out_free:
329 kfree(entry);
330 return status;
331 }
332
333 static int
334 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
335 {
336 int diff;
337 int err;
338 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
339
340 if (v->data_len < sizeof(*user_iph))
341 return 0;
342 diff = v->data_len - e->skb->len;
343 if (diff < 0) {
344 if (pskb_trim(e->skb, v->data_len))
345 return -ENOMEM;
346 } else if (diff > 0) {
347 if (v->data_len > 0xFFFF)
348 return -EINVAL;
349 if (diff > skb_tailroom(e->skb)) {
350 err = pskb_expand_head(e->skb, 0,
351 diff - skb_tailroom(e->skb),
352 GFP_ATOMIC);
353 if (err) {
354 printk(KERN_WARNING "ip6_queue: OOM "
355 "in mangle, dropping packet\n");
356 return err;
357 }
358 }
359 skb_put(e->skb, diff);
360 }
361 if (!skb_make_writable(e->skb, v->data_len))
362 return -ENOMEM;
363 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
364 e->skb->ip_summed = CHECKSUM_NONE;
365
366 return 0;
367 }
368
369 static inline int
370 id_cmp(struct ipq_queue_entry *e, unsigned long id)
371 {
372 return (id == (unsigned long )e);
373 }
374
375 static int
376 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
377 {
378 struct ipq_queue_entry *entry;
379
380 if (vmsg->value > NF_MAX_VERDICT)
381 return -EINVAL;
382
383 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
384 if (entry == NULL)
385 return -ENOENT;
386 else {
387 int verdict = vmsg->value;
388
389 if (vmsg->data_len && vmsg->data_len == len)
390 if (ipq_mangle_ipv6(vmsg, entry) < 0)
391 verdict = NF_DROP;
392
393 ipq_issue_verdict(entry, verdict);
394 return 0;
395 }
396 }
397
398 static int
399 ipq_set_mode(unsigned char mode, unsigned int range)
400 {
401 int status;
402
403 write_lock_bh(&queue_lock);
404 status = __ipq_set_mode(mode, range);
405 write_unlock_bh(&queue_lock);
406 return status;
407 }
408
409 static int
410 ipq_receive_peer(struct ipq_peer_msg *pmsg,
411 unsigned char type, unsigned int len)
412 {
413 int status = 0;
414
415 if (len < sizeof(*pmsg))
416 return -EINVAL;
417
418 switch (type) {
419 case IPQM_MODE:
420 status = ipq_set_mode(pmsg->msg.mode.value,
421 pmsg->msg.mode.range);
422 break;
423
424 case IPQM_VERDICT:
425 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
426 status = -EINVAL;
427 else
428 status = ipq_set_verdict(&pmsg->msg.verdict,
429 len - sizeof(*pmsg));
430 break;
431 default:
432 status = -EINVAL;
433 }
434 return status;
435 }
436
437 static int
438 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
439 {
440 if (entry->info->indev)
441 if (entry->info->indev->ifindex == ifindex)
442 return 1;
443
444 if (entry->info->outdev)
445 if (entry->info->outdev->ifindex == ifindex)
446 return 1;
447
448 return 0;
449 }
450
451 static void
452 ipq_dev_drop(int ifindex)
453 {
454 struct ipq_queue_entry *entry;
455
456 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
457 ipq_issue_verdict(entry, NF_DROP);
458 }
459
460 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
461
462 static inline void
463 __ipq_rcv_skb(struct sk_buff *skb)
464 {
465 int status, type, pid, flags, nlmsglen, skblen;
466 struct nlmsghdr *nlh;
467
468 skblen = skb->len;
469 if (skblen < sizeof(*nlh))
470 return;
471
472 nlh = nlmsg_hdr(skb);
473 nlmsglen = nlh->nlmsg_len;
474 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
475 return;
476
477 pid = nlh->nlmsg_pid;
478 flags = nlh->nlmsg_flags;
479
480 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
481 RCV_SKB_FAIL(-EINVAL);
482
483 if (flags & MSG_TRUNC)
484 RCV_SKB_FAIL(-ECOMM);
485
486 type = nlh->nlmsg_type;
487 if (type < NLMSG_NOOP || type >= IPQM_MAX)
488 RCV_SKB_FAIL(-EINVAL);
489
490 if (type <= IPQM_BASE)
491 return;
492
493 if (security_netlink_recv(skb, CAP_NET_ADMIN))
494 RCV_SKB_FAIL(-EPERM);
495
496 write_lock_bh(&queue_lock);
497
498 if (peer_pid) {
499 if (peer_pid != pid) {
500 write_unlock_bh(&queue_lock);
501 RCV_SKB_FAIL(-EBUSY);
502 }
503 } else {
504 net_enable_timestamp();
505 peer_pid = pid;
506 }
507
508 write_unlock_bh(&queue_lock);
509
510 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
511 nlmsglen - NLMSG_LENGTH(0));
512 if (status < 0)
513 RCV_SKB_FAIL(status);
514
515 if (flags & NLM_F_ACK)
516 netlink_ack(skb, nlh, 0);
517 return;
518 }
519
520 static void
521 ipq_rcv_skb(struct sk_buff *skb)
522 {
523 mutex_lock(&ipqnl_mutex);
524 __ipq_rcv_skb(skb);
525 mutex_unlock(&ipqnl_mutex);
526 }
527
528 static int
529 ipq_rcv_dev_event(struct notifier_block *this,
530 unsigned long event, void *ptr)
531 {
532 struct net_device *dev = ptr;
533
534 if (dev->nd_net != &init_net)
535 return NOTIFY_DONE;
536
537 /* Drop any packets associated with the downed device */
538 if (event == NETDEV_DOWN)
539 ipq_dev_drop(dev->ifindex);
540 return NOTIFY_DONE;
541 }
542
543 static struct notifier_block ipq_dev_notifier = {
544 .notifier_call = ipq_rcv_dev_event,
545 };
546
547 static int
548 ipq_rcv_nl_event(struct notifier_block *this,
549 unsigned long event, void *ptr)
550 {
551 struct netlink_notify *n = ptr;
552
553 if (event == NETLINK_URELEASE &&
554 n->protocol == NETLINK_IP6_FW && n->pid) {
555 write_lock_bh(&queue_lock);
556 if ((n->net == &init_net) && (n->pid == peer_pid))
557 __ipq_reset();
558 write_unlock_bh(&queue_lock);
559 }
560 return NOTIFY_DONE;
561 }
562
563 static struct notifier_block ipq_nl_notifier = {
564 .notifier_call = ipq_rcv_nl_event,
565 };
566
567 static struct ctl_table_header *ipq_sysctl_header;
568
569 static ctl_table ipq_table[] = {
570 {
571 .ctl_name = NET_IPQ_QMAX,
572 .procname = NET_IPQ_QMAX_NAME,
573 .data = &queue_maxlen,
574 .maxlen = sizeof(queue_maxlen),
575 .mode = 0644,
576 .proc_handler = proc_dointvec
577 },
578 { .ctl_name = 0 }
579 };
580
581 static ctl_table ipq_dir_table[] = {
582 {
583 .ctl_name = NET_IPV6,
584 .procname = "ipv6",
585 .mode = 0555,
586 .child = ipq_table
587 },
588 { .ctl_name = 0 }
589 };
590
591 static ctl_table ipq_root_table[] = {
592 {
593 .ctl_name = CTL_NET,
594 .procname = "net",
595 .mode = 0555,
596 .child = ipq_dir_table
597 },
598 { .ctl_name = 0 }
599 };
600
601 static int ip6_queue_show(struct seq_file *m, void *v)
602 {
603 read_lock_bh(&queue_lock);
604
605 seq_printf(m,
606 "Peer PID : %d\n"
607 "Copy mode : %hu\n"
608 "Copy range : %u\n"
609 "Queue length : %u\n"
610 "Queue max. length : %u\n"
611 "Queue dropped : %u\n"
612 "Netfilter dropped : %u\n",
613 peer_pid,
614 copy_mode,
615 copy_range,
616 queue_total,
617 queue_maxlen,
618 queue_dropped,
619 queue_user_dropped);
620
621 read_unlock_bh(&queue_lock);
622 return 0;
623 }
624
625 static int ip6_queue_open(struct inode *inode, struct file *file)
626 {
627 return single_open(file, ip6_queue_show, NULL);
628 }
629
630 static const struct file_operations ip6_queue_proc_fops = {
631 .open = ip6_queue_open,
632 .read = seq_read,
633 .llseek = seq_lseek,
634 .release = single_release,
635 .owner = THIS_MODULE,
636 };
637
638 static const struct nf_queue_handler nfqh = {
639 .name = "ip6_queue",
640 .outfn = &ipq_enqueue_packet,
641 };
642
643 static int __init ip6_queue_init(void)
644 {
645 int status = -ENOMEM;
646 struct proc_dir_entry *proc;
647
648 netlink_register_notifier(&ipq_nl_notifier);
649 ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
650 ipq_rcv_skb, NULL, THIS_MODULE);
651 if (ipqnl == NULL) {
652 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
653 goto cleanup_netlink_notifier;
654 }
655
656 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
657 if (proc) {
658 proc->owner = THIS_MODULE;
659 proc->proc_fops = &ip6_queue_proc_fops;
660 } else {
661 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
662 goto cleanup_ipqnl;
663 }
664
665 register_netdevice_notifier(&ipq_dev_notifier);
666 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
667
668 status = nf_register_queue_handler(PF_INET6, &nfqh);
669 if (status < 0) {
670 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
671 goto cleanup_sysctl;
672 }
673 return status;
674
675 cleanup_sysctl:
676 unregister_sysctl_table(ipq_sysctl_header);
677 unregister_netdevice_notifier(&ipq_dev_notifier);
678 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
679
680 cleanup_ipqnl:
681 sock_release(ipqnl->sk_socket);
682 mutex_lock(&ipqnl_mutex);
683 mutex_unlock(&ipqnl_mutex);
684
685 cleanup_netlink_notifier:
686 netlink_unregister_notifier(&ipq_nl_notifier);
687 return status;
688 }
689
690 static void __exit ip6_queue_fini(void)
691 {
692 nf_unregister_queue_handlers(&nfqh);
693 synchronize_net();
694 ipq_flush(NF_DROP);
695
696 unregister_sysctl_table(ipq_sysctl_header);
697 unregister_netdevice_notifier(&ipq_dev_notifier);
698 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
699
700 sock_release(ipqnl->sk_socket);
701 mutex_lock(&ipqnl_mutex);
702 mutex_unlock(&ipqnl_mutex);
703
704 netlink_unregister_notifier(&ipq_nl_notifier);
705 }
706
707 MODULE_DESCRIPTION("IPv6 packet queue handler");
708 MODULE_LICENSE("GPL");
709
710 module_init(ip6_queue_init);
711 module_exit(ip6_queue_fini);
This page took 0.048941 seconds and 6 git commands to generate.