V4L/DVB (7013): bw-qcam: add module parameter 'force_init' to skip polite auto-detect...
[firewire-audio.git] / net / ipv6 / netfilter / ip6_queue.c
blobe273605eef855469794849f9cdfd800d659e87e7
1 /*
2 * This is a module which is used for queueing IPv6 packets and
3 * communicating with userspace via netlink.
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
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.
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 <linux/netfilter_ipv4/ip_queue.h>
33 #include <linux/netfilter_ipv4/ip_tables.h>
34 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #define IPQ_QMAX_DEFAULT 1024
37 #define IPQ_PROC_FS_NAME "ip6_queue"
38 #define NET_IPQ_QMAX 2088
39 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
41 struct ipq_queue_entry {
42 struct list_head list;
43 struct nf_info *info;
44 struct sk_buff *skb;
47 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
49 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
50 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
51 static DEFINE_RWLOCK(queue_lock);
52 static int peer_pid __read_mostly;
53 static unsigned int copy_range __read_mostly;
54 static unsigned int queue_total;
55 static unsigned int queue_dropped = 0;
56 static unsigned int queue_user_dropped = 0;
57 static struct sock *ipqnl __read_mostly;
58 static LIST_HEAD(queue_list);
59 static DEFINE_MUTEX(ipqnl_mutex);
61 static void
62 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
64 local_bh_disable();
65 nf_reinject(entry->skb, entry->info, verdict);
66 local_bh_enable();
67 kfree(entry);
70 static inline void
71 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
73 list_add(&entry->list, &queue_list);
74 queue_total++;
78 * Find and return a queued entry matched by cmpfn, or return the last
79 * entry if cmpfn is NULL.
81 static inline struct ipq_queue_entry *
82 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
84 struct list_head *p;
86 list_for_each_prev(p, &queue_list) {
87 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
89 if (!cmpfn || cmpfn(entry, data))
90 return entry;
92 return NULL;
95 static inline void
96 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
98 list_del(&entry->list);
99 queue_total--;
102 static inline struct ipq_queue_entry *
103 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
105 struct ipq_queue_entry *entry;
107 entry = __ipq_find_entry(cmpfn, data);
108 if (entry == NULL)
109 return NULL;
111 __ipq_dequeue_entry(entry);
112 return entry;
116 static inline void
117 __ipq_flush(int verdict)
119 struct ipq_queue_entry *entry;
121 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
122 ipq_issue_verdict(entry, verdict);
125 static inline int
126 __ipq_set_mode(unsigned char mode, unsigned int range)
128 int status = 0;
130 switch(mode) {
131 case IPQ_COPY_NONE:
132 case IPQ_COPY_META:
133 copy_mode = mode;
134 copy_range = 0;
135 break;
137 case IPQ_COPY_PACKET:
138 copy_mode = mode;
139 copy_range = range;
140 if (copy_range > 0xFFFF)
141 copy_range = 0xFFFF;
142 break;
144 default:
145 status = -EINVAL;
148 return status;
151 static inline void
152 __ipq_reset(void)
154 peer_pid = 0;
155 net_disable_timestamp();
156 __ipq_set_mode(IPQ_COPY_NONE, 0);
157 __ipq_flush(NF_DROP);
160 static struct ipq_queue_entry *
161 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
163 struct ipq_queue_entry *entry;
165 write_lock_bh(&queue_lock);
166 entry = __ipq_find_dequeue_entry(cmpfn, data);
167 write_unlock_bh(&queue_lock);
168 return entry;
171 static void
172 ipq_flush(int verdict)
174 write_lock_bh(&queue_lock);
175 __ipq_flush(verdict);
176 write_unlock_bh(&queue_lock);
179 static struct sk_buff *
180 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
182 sk_buff_data_t old_tail;
183 size_t size = 0;
184 size_t data_len = 0;
185 struct sk_buff *skb;
186 struct ipq_packet_msg *pmsg;
187 struct nlmsghdr *nlh;
188 struct timeval tv;
190 read_lock_bh(&queue_lock);
192 switch (copy_mode) {
193 case IPQ_COPY_META:
194 case IPQ_COPY_NONE:
195 size = NLMSG_SPACE(sizeof(*pmsg));
196 data_len = 0;
197 break;
199 case IPQ_COPY_PACKET:
200 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
201 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
202 (*errp = skb_checksum_help(entry->skb))) {
203 read_unlock_bh(&queue_lock);
204 return NULL;
206 if (copy_range == 0 || copy_range > entry->skb->len)
207 data_len = entry->skb->len;
208 else
209 data_len = copy_range;
211 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
212 break;
214 default:
215 *errp = -EINVAL;
216 read_unlock_bh(&queue_lock);
217 return NULL;
220 read_unlock_bh(&queue_lock);
222 skb = alloc_skb(size, GFP_ATOMIC);
223 if (!skb)
224 goto nlmsg_failure;
226 old_tail = skb->tail;
227 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
228 pmsg = NLMSG_DATA(nlh);
229 memset(pmsg, 0, sizeof(*pmsg));
231 pmsg->packet_id = (unsigned long )entry;
232 pmsg->data_len = data_len;
233 tv = ktime_to_timeval(entry->skb->tstamp);
234 pmsg->timestamp_sec = tv.tv_sec;
235 pmsg->timestamp_usec = tv.tv_usec;
236 pmsg->mark = entry->skb->mark;
237 pmsg->hook = entry->info->hook;
238 pmsg->hw_protocol = entry->skb->protocol;
240 if (entry->info->indev)
241 strcpy(pmsg->indev_name, entry->info->indev->name);
242 else
243 pmsg->indev_name[0] = '\0';
245 if (entry->info->outdev)
246 strcpy(pmsg->outdev_name, entry->info->outdev->name);
247 else
248 pmsg->outdev_name[0] = '\0';
250 if (entry->info->indev && entry->skb->dev) {
251 pmsg->hw_type = entry->skb->dev->type;
252 pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
255 if (data_len)
256 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
257 BUG();
259 nlh->nlmsg_len = skb->tail - old_tail;
260 return skb;
262 nlmsg_failure:
263 if (skb)
264 kfree_skb(skb);
265 *errp = -EINVAL;
266 printk(KERN_ERR "ip6_queue: error creating packet message\n");
267 return NULL;
270 static int
271 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
272 unsigned int queuenum, void *data)
274 int status = -EINVAL;
275 struct sk_buff *nskb;
276 struct ipq_queue_entry *entry;
278 if (copy_mode == IPQ_COPY_NONE)
279 return -EAGAIN;
281 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
282 if (entry == NULL) {
283 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
284 return -ENOMEM;
287 entry->info = info;
288 entry->skb = skb;
290 nskb = ipq_build_packet_message(entry, &status);
291 if (nskb == NULL)
292 goto err_out_free;
294 write_lock_bh(&queue_lock);
296 if (!peer_pid)
297 goto err_out_free_nskb;
299 if (queue_total >= queue_maxlen) {
300 queue_dropped++;
301 status = -ENOSPC;
302 if (net_ratelimit())
303 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
304 "dropping packet(s). Dropped: %d\n", queue_total,
305 queue_dropped);
306 goto err_out_free_nskb;
309 /* netlink_unicast will either free the nskb or attach it to a socket */
310 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
311 if (status < 0) {
312 queue_user_dropped++;
313 goto err_out_unlock;
316 __ipq_enqueue_entry(entry);
318 write_unlock_bh(&queue_lock);
319 return status;
321 err_out_free_nskb:
322 kfree_skb(nskb);
324 err_out_unlock:
325 write_unlock_bh(&queue_lock);
327 err_out_free:
328 kfree(entry);
329 return status;
332 static int
333 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
335 int diff;
336 int err;
337 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
339 if (v->data_len < sizeof(*user_iph))
340 return 0;
341 diff = v->data_len - e->skb->len;
342 if (diff < 0) {
343 if (pskb_trim(e->skb, v->data_len))
344 return -ENOMEM;
345 } else if (diff > 0) {
346 if (v->data_len > 0xFFFF)
347 return -EINVAL;
348 if (diff > skb_tailroom(e->skb)) {
349 err = pskb_expand_head(e->skb, 0,
350 diff - skb_tailroom(e->skb),
351 GFP_ATOMIC);
352 if (err) {
353 printk(KERN_WARNING "ip6_queue: OOM "
354 "in mangle, dropping packet\n");
355 return err;
358 skb_put(e->skb, diff);
360 if (!skb_make_writable(e->skb, v->data_len))
361 return -ENOMEM;
362 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
363 e->skb->ip_summed = CHECKSUM_NONE;
365 return 0;
368 static inline int
369 id_cmp(struct ipq_queue_entry *e, unsigned long id)
371 return (id == (unsigned long )e);
374 static int
375 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
377 struct ipq_queue_entry *entry;
379 if (vmsg->value > NF_MAX_VERDICT)
380 return -EINVAL;
382 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
383 if (entry == NULL)
384 return -ENOENT;
385 else {
386 int verdict = vmsg->value;
388 if (vmsg->data_len && vmsg->data_len == len)
389 if (ipq_mangle_ipv6(vmsg, entry) < 0)
390 verdict = NF_DROP;
392 ipq_issue_verdict(entry, verdict);
393 return 0;
397 static int
398 ipq_set_mode(unsigned char mode, unsigned int range)
400 int status;
402 write_lock_bh(&queue_lock);
403 status = __ipq_set_mode(mode, range);
404 write_unlock_bh(&queue_lock);
405 return status;
408 static int
409 ipq_receive_peer(struct ipq_peer_msg *pmsg,
410 unsigned char type, unsigned int len)
412 int status = 0;
414 if (len < sizeof(*pmsg))
415 return -EINVAL;
417 switch (type) {
418 case IPQM_MODE:
419 status = ipq_set_mode(pmsg->msg.mode.value,
420 pmsg->msg.mode.range);
421 break;
423 case IPQM_VERDICT:
424 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
425 status = -EINVAL;
426 else
427 status = ipq_set_verdict(&pmsg->msg.verdict,
428 len - sizeof(*pmsg));
429 break;
430 default:
431 status = -EINVAL;
433 return status;
436 static int
437 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
439 if (entry->info->indev)
440 if (entry->info->indev->ifindex == ifindex)
441 return 1;
443 if (entry->info->outdev)
444 if (entry->info->outdev->ifindex == ifindex)
445 return 1;
447 return 0;
450 static void
451 ipq_dev_drop(int ifindex)
453 struct ipq_queue_entry *entry;
455 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
456 ipq_issue_verdict(entry, NF_DROP);
459 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
461 static inline void
462 __ipq_rcv_skb(struct sk_buff *skb)
464 int status, type, pid, flags, nlmsglen, skblen;
465 struct nlmsghdr *nlh;
467 skblen = skb->len;
468 if (skblen < sizeof(*nlh))
469 return;
471 nlh = nlmsg_hdr(skb);
472 nlmsglen = nlh->nlmsg_len;
473 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
474 return;
476 pid = nlh->nlmsg_pid;
477 flags = nlh->nlmsg_flags;
479 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
480 RCV_SKB_FAIL(-EINVAL);
482 if (flags & MSG_TRUNC)
483 RCV_SKB_FAIL(-ECOMM);
485 type = nlh->nlmsg_type;
486 if (type < NLMSG_NOOP || type >= IPQM_MAX)
487 RCV_SKB_FAIL(-EINVAL);
489 if (type <= IPQM_BASE)
490 return;
492 if (security_netlink_recv(skb, CAP_NET_ADMIN))
493 RCV_SKB_FAIL(-EPERM);
495 write_lock_bh(&queue_lock);
497 if (peer_pid) {
498 if (peer_pid != pid) {
499 write_unlock_bh(&queue_lock);
500 RCV_SKB_FAIL(-EBUSY);
502 } else {
503 net_enable_timestamp();
504 peer_pid = pid;
507 write_unlock_bh(&queue_lock);
509 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
510 nlmsglen - NLMSG_LENGTH(0));
511 if (status < 0)
512 RCV_SKB_FAIL(status);
514 if (flags & NLM_F_ACK)
515 netlink_ack(skb, nlh, 0);
516 return;
519 static void
520 ipq_rcv_skb(struct sk_buff *skb)
522 mutex_lock(&ipqnl_mutex);
523 __ipq_rcv_skb(skb);
524 mutex_unlock(&ipqnl_mutex);
527 static int
528 ipq_rcv_dev_event(struct notifier_block *this,
529 unsigned long event, void *ptr)
531 struct net_device *dev = ptr;
533 if (dev->nd_net != &init_net)
534 return NOTIFY_DONE;
536 /* Drop any packets associated with the downed device */
537 if (event == NETDEV_DOWN)
538 ipq_dev_drop(dev->ifindex);
539 return NOTIFY_DONE;
542 static struct notifier_block ipq_dev_notifier = {
543 .notifier_call = ipq_rcv_dev_event,
546 static int
547 ipq_rcv_nl_event(struct notifier_block *this,
548 unsigned long event, void *ptr)
550 struct netlink_notify *n = ptr;
552 if (event == NETLINK_URELEASE &&
553 n->protocol == NETLINK_IP6_FW && n->pid) {
554 write_lock_bh(&queue_lock);
555 if ((n->net == &init_net) && (n->pid == peer_pid))
556 __ipq_reset();
557 write_unlock_bh(&queue_lock);
559 return NOTIFY_DONE;
562 static struct notifier_block ipq_nl_notifier = {
563 .notifier_call = ipq_rcv_nl_event,
566 static struct ctl_table_header *ipq_sysctl_header;
568 static ctl_table ipq_table[] = {
570 .ctl_name = NET_IPQ_QMAX,
571 .procname = NET_IPQ_QMAX_NAME,
572 .data = &queue_maxlen,
573 .maxlen = sizeof(queue_maxlen),
574 .mode = 0644,
575 .proc_handler = proc_dointvec
577 { .ctl_name = 0 }
580 static ctl_table ipq_dir_table[] = {
582 .ctl_name = NET_IPV6,
583 .procname = "ipv6",
584 .mode = 0555,
585 .child = ipq_table
587 { .ctl_name = 0 }
590 static ctl_table ipq_root_table[] = {
592 .ctl_name = CTL_NET,
593 .procname = "net",
594 .mode = 0555,
595 .child = ipq_dir_table
597 { .ctl_name = 0 }
600 static int ip6_queue_show(struct seq_file *m, void *v)
602 read_lock_bh(&queue_lock);
604 seq_printf(m,
605 "Peer PID : %d\n"
606 "Copy mode : %hu\n"
607 "Copy range : %u\n"
608 "Queue length : %u\n"
609 "Queue max. length : %u\n"
610 "Queue dropped : %u\n"
611 "Netfilter dropped : %u\n",
612 peer_pid,
613 copy_mode,
614 copy_range,
615 queue_total,
616 queue_maxlen,
617 queue_dropped,
618 queue_user_dropped);
620 read_unlock_bh(&queue_lock);
621 return 0;
624 static int ip6_queue_open(struct inode *inode, struct file *file)
626 return single_open(file, ip6_queue_show, NULL);
629 static const struct file_operations ip6_queue_proc_fops = {
630 .open = ip6_queue_open,
631 .read = seq_read,
632 .llseek = seq_lseek,
633 .release = single_release,
634 .owner = THIS_MODULE,
637 static struct nf_queue_handler nfqh = {
638 .name = "ip6_queue",
639 .outfn = &ipq_enqueue_packet,
642 static int __init ip6_queue_init(void)
644 int status = -ENOMEM;
645 struct proc_dir_entry *proc;
647 netlink_register_notifier(&ipq_nl_notifier);
648 ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
649 ipq_rcv_skb, NULL, THIS_MODULE);
650 if (ipqnl == NULL) {
651 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
652 goto cleanup_netlink_notifier;
655 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
656 if (proc) {
657 proc->owner = THIS_MODULE;
658 proc->proc_fops = &ip6_queue_proc_fops;
659 } else {
660 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
661 goto cleanup_ipqnl;
664 register_netdevice_notifier(&ipq_dev_notifier);
665 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
667 status = nf_register_queue_handler(PF_INET6, &nfqh);
668 if (status < 0) {
669 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
670 goto cleanup_sysctl;
672 return status;
674 cleanup_sysctl:
675 unregister_sysctl_table(ipq_sysctl_header);
676 unregister_netdevice_notifier(&ipq_dev_notifier);
677 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
679 cleanup_ipqnl:
680 sock_release(ipqnl->sk_socket);
681 mutex_lock(&ipqnl_mutex);
682 mutex_unlock(&ipqnl_mutex);
684 cleanup_netlink_notifier:
685 netlink_unregister_notifier(&ipq_nl_notifier);
686 return status;
689 static void __exit ip6_queue_fini(void)
691 nf_unregister_queue_handlers(&nfqh);
692 synchronize_net();
693 ipq_flush(NF_DROP);
695 unregister_sysctl_table(ipq_sysctl_header);
696 unregister_netdevice_notifier(&ipq_dev_notifier);
697 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
699 sock_release(ipqnl->sk_socket);
700 mutex_lock(&ipqnl_mutex);
701 mutex_unlock(&ipqnl_mutex);
703 netlink_unregister_notifier(&ipq_nl_notifier);
706 MODULE_DESCRIPTION("IPv6 packet queue handler");
707 MODULE_LICENSE("GPL");
709 module_init(ip6_queue_init);
710 module_exit(ip6_queue_fini);