[NETFILTER]: Fix HW checksum handling in ip_queue/ip6_queue
[linux-2.6/suspend2-2.6.18.git] / net / ipv6 / netfilter / ip6_queue.c
bloba16df5b27c84e8ab93dc5dc42be41c1c5662e591
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 * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
16 * to adapt it to IPv6
17 * HEAVILY based in ipqueue.c by James Morris. It's just
18 * a little modified version of it, so he's nearly the
19 * real coder of this.
20 * Few changes needed, mainly the hard_routing code and
21 * the netlink socket protocol (we're NETLINK_IP6_FW).
22 * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
23 * 2005-02-04: Added /proc counter for dropped packets; fixed so
24 * packets aren't delivered to user space if they're going
25 * to be dropped.
27 #include <linux/module.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.h>
30 #include <linux/ipv6.h>
31 #include <linux/notifier.h>
32 #include <linux/netdevice.h>
33 #include <linux/netfilter.h>
34 #include <linux/netlink.h>
35 #include <linux/spinlock.h>
36 #include <linux/sysctl.h>
37 #include <linux/proc_fs.h>
38 #include <net/sock.h>
39 #include <net/ipv6.h>
40 #include <net/ip6_route.h>
41 #include <linux/netfilter_ipv4/ip_queue.h>
42 #include <linux/netfilter_ipv4/ip_tables.h>
43 #include <linux/netfilter_ipv6/ip6_tables.h>
45 #define IPQ_QMAX_DEFAULT 1024
46 #define IPQ_PROC_FS_NAME "ip6_queue"
47 #define NET_IPQ_QMAX 2088
48 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
50 struct ipq_rt_info {
51 struct in6_addr daddr;
52 struct in6_addr saddr;
55 struct ipq_queue_entry {
56 struct list_head list;
57 struct nf_info *info;
58 struct sk_buff *skb;
59 struct ipq_rt_info rt_info;
62 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
64 static unsigned char copy_mode = IPQ_COPY_NONE;
65 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
66 static DEFINE_RWLOCK(queue_lock);
67 static int peer_pid;
68 static unsigned int copy_range;
69 static unsigned int queue_total;
70 static unsigned int queue_dropped = 0;
71 static unsigned int queue_user_dropped = 0;
72 static struct sock *ipqnl;
73 static LIST_HEAD(queue_list);
74 static DECLARE_MUTEX(ipqnl_sem);
76 static void
77 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
79 local_bh_disable();
80 nf_reinject(entry->skb, entry->info, verdict);
81 local_bh_enable();
82 kfree(entry);
85 static inline void
86 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
88 list_add(&entry->list, &queue_list);
89 queue_total++;
93 * Find and return a queued entry matched by cmpfn, or return the last
94 * entry if cmpfn is NULL.
96 static inline struct ipq_queue_entry *
97 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
99 struct list_head *p;
101 list_for_each_prev(p, &queue_list) {
102 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
104 if (!cmpfn || cmpfn(entry, data))
105 return entry;
107 return NULL;
110 static inline void
111 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
113 list_del(&entry->list);
114 queue_total--;
117 static inline struct ipq_queue_entry *
118 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
120 struct ipq_queue_entry *entry;
122 entry = __ipq_find_entry(cmpfn, data);
123 if (entry == NULL)
124 return NULL;
126 __ipq_dequeue_entry(entry);
127 return entry;
131 static inline void
132 __ipq_flush(int verdict)
134 struct ipq_queue_entry *entry;
136 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
137 ipq_issue_verdict(entry, verdict);
140 static inline int
141 __ipq_set_mode(unsigned char mode, unsigned int range)
143 int status = 0;
145 switch(mode) {
146 case IPQ_COPY_NONE:
147 case IPQ_COPY_META:
148 copy_mode = mode;
149 copy_range = 0;
150 break;
152 case IPQ_COPY_PACKET:
153 copy_mode = mode;
154 copy_range = range;
155 if (copy_range > 0xFFFF)
156 copy_range = 0xFFFF;
157 break;
159 default:
160 status = -EINVAL;
163 return status;
166 static inline void
167 __ipq_reset(void)
169 peer_pid = 0;
170 net_disable_timestamp();
171 __ipq_set_mode(IPQ_COPY_NONE, 0);
172 __ipq_flush(NF_DROP);
175 static struct ipq_queue_entry *
176 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
178 struct ipq_queue_entry *entry;
180 write_lock_bh(&queue_lock);
181 entry = __ipq_find_dequeue_entry(cmpfn, data);
182 write_unlock_bh(&queue_lock);
183 return entry;
186 static void
187 ipq_flush(int verdict)
189 write_lock_bh(&queue_lock);
190 __ipq_flush(verdict);
191 write_unlock_bh(&queue_lock);
194 static struct sk_buff *
195 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
197 unsigned char *old_tail;
198 size_t size = 0;
199 size_t data_len = 0;
200 struct sk_buff *skb;
201 struct ipq_packet_msg *pmsg;
202 struct nlmsghdr *nlh;
204 read_lock_bh(&queue_lock);
206 switch (copy_mode) {
207 case IPQ_COPY_META:
208 case IPQ_COPY_NONE:
209 size = NLMSG_SPACE(sizeof(*pmsg));
210 data_len = 0;
211 break;
213 case IPQ_COPY_PACKET:
214 if (entry->skb->ip_summed == CHECKSUM_HW &&
215 (*errp = skb_checksum_help(entry->skb,
216 entry->info->outdev == NULL))) {
217 read_unlock_bh(&queue_lock);
218 return NULL;
220 if (copy_range == 0 || copy_range > entry->skb->len)
221 data_len = entry->skb->len;
222 else
223 data_len = copy_range;
225 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
226 break;
228 default:
229 *errp = -EINVAL;
230 read_unlock_bh(&queue_lock);
231 return NULL;
234 read_unlock_bh(&queue_lock);
236 skb = alloc_skb(size, GFP_ATOMIC);
237 if (!skb)
238 goto nlmsg_failure;
240 old_tail= skb->tail;
241 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
242 pmsg = NLMSG_DATA(nlh);
243 memset(pmsg, 0, sizeof(*pmsg));
245 pmsg->packet_id = (unsigned long )entry;
246 pmsg->data_len = data_len;
247 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
248 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
249 pmsg->mark = entry->skb->nfmark;
250 pmsg->hook = entry->info->hook;
251 pmsg->hw_protocol = entry->skb->protocol;
253 if (entry->info->indev)
254 strcpy(pmsg->indev_name, entry->info->indev->name);
255 else
256 pmsg->indev_name[0] = '\0';
258 if (entry->info->outdev)
259 strcpy(pmsg->outdev_name, entry->info->outdev->name);
260 else
261 pmsg->outdev_name[0] = '\0';
263 if (entry->info->indev && entry->skb->dev) {
264 pmsg->hw_type = entry->skb->dev->type;
265 if (entry->skb->dev->hard_header_parse)
266 pmsg->hw_addrlen =
267 entry->skb->dev->hard_header_parse(entry->skb,
268 pmsg->hw_addr);
271 if (data_len)
272 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
273 BUG();
275 nlh->nlmsg_len = skb->tail - old_tail;
276 return skb;
278 nlmsg_failure:
279 if (skb)
280 kfree_skb(skb);
281 *errp = -EINVAL;
282 printk(KERN_ERR "ip6_queue: error creating packet message\n");
283 return NULL;
286 static int
287 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
289 int status = -EINVAL;
290 struct sk_buff *nskb;
291 struct ipq_queue_entry *entry;
293 if (copy_mode == IPQ_COPY_NONE)
294 return -EAGAIN;
296 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
297 if (entry == NULL) {
298 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
299 return -ENOMEM;
302 entry->info = info;
303 entry->skb = skb;
305 if (entry->info->hook == NF_IP_LOCAL_OUT) {
306 struct ipv6hdr *iph = skb->nh.ipv6h;
308 entry->rt_info.daddr = iph->daddr;
309 entry->rt_info.saddr = iph->saddr;
312 nskb = ipq_build_packet_message(entry, &status);
313 if (nskb == NULL)
314 goto err_out_free;
316 write_lock_bh(&queue_lock);
318 if (!peer_pid)
319 goto err_out_free_nskb;
321 if (queue_total >= queue_maxlen) {
322 queue_dropped++;
323 status = -ENOSPC;
324 if (net_ratelimit())
325 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
326 "dropping packet(s). Dropped: %d\n", queue_total,
327 queue_dropped);
328 goto err_out_free_nskb;
331 /* netlink_unicast will either free the nskb or attach it to a socket */
332 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
333 if (status < 0) {
334 queue_user_dropped++;
335 goto err_out_unlock;
338 __ipq_enqueue_entry(entry);
340 write_unlock_bh(&queue_lock);
341 return status;
343 err_out_free_nskb:
344 kfree_skb(nskb);
346 err_out_unlock:
347 write_unlock_bh(&queue_lock);
349 err_out_free:
350 kfree(entry);
351 return status;
354 static int
355 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
357 int diff;
358 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
360 if (v->data_len < sizeof(*user_iph))
361 return 0;
362 diff = v->data_len - e->skb->len;
363 if (diff < 0)
364 skb_trim(e->skb, v->data_len);
365 else if (diff > 0) {
366 if (v->data_len > 0xFFFF)
367 return -EINVAL;
368 if (diff > skb_tailroom(e->skb)) {
369 struct sk_buff *newskb;
371 newskb = skb_copy_expand(e->skb,
372 skb_headroom(e->skb),
373 diff,
374 GFP_ATOMIC);
375 if (newskb == NULL) {
376 printk(KERN_WARNING "ip6_queue: OOM "
377 "in mangle, dropping packet\n");
378 return -ENOMEM;
380 if (e->skb->sk)
381 skb_set_owner_w(newskb, e->skb->sk);
382 kfree_skb(e->skb);
383 e->skb = newskb;
385 skb_put(e->skb, diff);
387 if (!skb_ip_make_writable(&e->skb, v->data_len))
388 return -ENOMEM;
389 memcpy(e->skb->data, v->payload, v->data_len);
390 e->skb->ip_summed = CHECKSUM_NONE;
391 e->skb->nfcache |= NFC_ALTERED;
394 * Extra routing may needed on local out, as the QUEUE target never
395 * returns control to the table.
396 * Not a nice way to cmp, but works
398 if (e->info->hook == NF_IP_LOCAL_OUT) {
399 struct ipv6hdr *iph = e->skb->nh.ipv6h;
400 if (!ipv6_addr_equal(&iph->daddr, &e->rt_info.daddr) ||
401 !ipv6_addr_equal(&iph->saddr, &e->rt_info.saddr))
402 return ip6_route_me_harder(e->skb);
404 return 0;
407 static inline int
408 id_cmp(struct ipq_queue_entry *e, unsigned long id)
410 return (id == (unsigned long )e);
413 static int
414 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
416 struct ipq_queue_entry *entry;
418 if (vmsg->value > NF_MAX_VERDICT)
419 return -EINVAL;
421 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
422 if (entry == NULL)
423 return -ENOENT;
424 else {
425 int verdict = vmsg->value;
427 if (vmsg->data_len && vmsg->data_len == len)
428 if (ipq_mangle_ipv6(vmsg, entry) < 0)
429 verdict = NF_DROP;
431 ipq_issue_verdict(entry, verdict);
432 return 0;
436 static int
437 ipq_set_mode(unsigned char mode, unsigned int range)
439 int status;
441 write_lock_bh(&queue_lock);
442 status = __ipq_set_mode(mode, range);
443 write_unlock_bh(&queue_lock);
444 return status;
447 static int
448 ipq_receive_peer(struct ipq_peer_msg *pmsg,
449 unsigned char type, unsigned int len)
451 int status = 0;
453 if (len < sizeof(*pmsg))
454 return -EINVAL;
456 switch (type) {
457 case IPQM_MODE:
458 status = ipq_set_mode(pmsg->msg.mode.value,
459 pmsg->msg.mode.range);
460 break;
462 case IPQM_VERDICT:
463 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
464 status = -EINVAL;
465 else
466 status = ipq_set_verdict(&pmsg->msg.verdict,
467 len - sizeof(*pmsg));
468 break;
469 default:
470 status = -EINVAL;
472 return status;
475 static int
476 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
478 if (entry->info->indev)
479 if (entry->info->indev->ifindex == ifindex)
480 return 1;
482 if (entry->info->outdev)
483 if (entry->info->outdev->ifindex == ifindex)
484 return 1;
486 return 0;
489 static void
490 ipq_dev_drop(int ifindex)
492 struct ipq_queue_entry *entry;
494 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
495 ipq_issue_verdict(entry, NF_DROP);
498 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
500 static inline void
501 ipq_rcv_skb(struct sk_buff *skb)
503 int status, type, pid, flags, nlmsglen, skblen;
504 struct nlmsghdr *nlh;
506 skblen = skb->len;
507 if (skblen < sizeof(*nlh))
508 return;
510 nlh = (struct nlmsghdr *)skb->data;
511 nlmsglen = nlh->nlmsg_len;
512 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
513 return;
515 pid = nlh->nlmsg_pid;
516 flags = nlh->nlmsg_flags;
518 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
519 RCV_SKB_FAIL(-EINVAL);
521 if (flags & MSG_TRUNC)
522 RCV_SKB_FAIL(-ECOMM);
524 type = nlh->nlmsg_type;
525 if (type < NLMSG_NOOP || type >= IPQM_MAX)
526 RCV_SKB_FAIL(-EINVAL);
528 if (type <= IPQM_BASE)
529 return;
531 if (security_netlink_recv(skb))
532 RCV_SKB_FAIL(-EPERM);
534 write_lock_bh(&queue_lock);
536 if (peer_pid) {
537 if (peer_pid != pid) {
538 write_unlock_bh(&queue_lock);
539 RCV_SKB_FAIL(-EBUSY);
541 } else {
542 net_enable_timestamp();
543 peer_pid = pid;
546 write_unlock_bh(&queue_lock);
548 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
549 skblen - NLMSG_LENGTH(0));
550 if (status < 0)
551 RCV_SKB_FAIL(status);
553 if (flags & NLM_F_ACK)
554 netlink_ack(skb, nlh, 0);
555 return;
558 static void
559 ipq_rcv_sk(struct sock *sk, int len)
561 struct sk_buff *skb;
562 unsigned int qlen;
564 down(&ipqnl_sem);
566 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
567 skb = skb_dequeue(&sk->sk_receive_queue);
568 ipq_rcv_skb(skb);
569 kfree_skb(skb);
572 up(&ipqnl_sem);
575 static int
576 ipq_rcv_dev_event(struct notifier_block *this,
577 unsigned long event, void *ptr)
579 struct net_device *dev = ptr;
581 /* Drop any packets associated with the downed device */
582 if (event == NETDEV_DOWN)
583 ipq_dev_drop(dev->ifindex);
584 return NOTIFY_DONE;
587 static struct notifier_block ipq_dev_notifier = {
588 .notifier_call = ipq_rcv_dev_event,
591 static int
592 ipq_rcv_nl_event(struct notifier_block *this,
593 unsigned long event, void *ptr)
595 struct netlink_notify *n = ptr;
597 if (event == NETLINK_URELEASE &&
598 n->protocol == NETLINK_IP6_FW && n->pid) {
599 write_lock_bh(&queue_lock);
600 if (n->pid == peer_pid)
601 __ipq_reset();
602 write_unlock_bh(&queue_lock);
604 return NOTIFY_DONE;
607 static struct notifier_block ipq_nl_notifier = {
608 .notifier_call = ipq_rcv_nl_event,
611 static struct ctl_table_header *ipq_sysctl_header;
613 static ctl_table ipq_table[] = {
615 .ctl_name = NET_IPQ_QMAX,
616 .procname = NET_IPQ_QMAX_NAME,
617 .data = &queue_maxlen,
618 .maxlen = sizeof(queue_maxlen),
619 .mode = 0644,
620 .proc_handler = proc_dointvec
622 { .ctl_name = 0 }
625 static ctl_table ipq_dir_table[] = {
627 .ctl_name = NET_IPV6,
628 .procname = "ipv6",
629 .mode = 0555,
630 .child = ipq_table
632 { .ctl_name = 0 }
635 static ctl_table ipq_root_table[] = {
637 .ctl_name = CTL_NET,
638 .procname = "net",
639 .mode = 0555,
640 .child = ipq_dir_table
642 { .ctl_name = 0 }
645 static int
646 ipq_get_info(char *buffer, char **start, off_t offset, int length)
648 int len;
650 read_lock_bh(&queue_lock);
652 len = sprintf(buffer,
653 "Peer PID : %d\n"
654 "Copy mode : %hu\n"
655 "Copy range : %u\n"
656 "Queue length : %u\n"
657 "Queue max. length : %u\n"
658 "Queue dropped : %u\n"
659 "Netfilter dropped : %u\n",
660 peer_pid,
661 copy_mode,
662 copy_range,
663 queue_total,
664 queue_maxlen,
665 queue_dropped,
666 queue_user_dropped);
668 read_unlock_bh(&queue_lock);
670 *start = buffer + offset;
671 len -= offset;
672 if (len > length)
673 len = length;
674 else if (len < 0)
675 len = 0;
676 return len;
679 static int
680 init_or_cleanup(int init)
682 int status = -ENOMEM;
683 struct proc_dir_entry *proc;
685 if (!init)
686 goto cleanup;
688 netlink_register_notifier(&ipq_nl_notifier);
689 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
690 if (ipqnl == NULL) {
691 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
692 goto cleanup_netlink_notifier;
695 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
696 if (proc)
697 proc->owner = THIS_MODULE;
698 else {
699 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
700 goto cleanup_ipqnl;
703 register_netdevice_notifier(&ipq_dev_notifier);
704 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
706 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
707 if (status < 0) {
708 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
709 goto cleanup_sysctl;
711 return status;
713 cleanup:
714 nf_unregister_queue_handler(PF_INET6);
715 synchronize_net();
716 ipq_flush(NF_DROP);
718 cleanup_sysctl:
719 unregister_sysctl_table(ipq_sysctl_header);
720 unregister_netdevice_notifier(&ipq_dev_notifier);
721 proc_net_remove(IPQ_PROC_FS_NAME);
723 cleanup_ipqnl:
724 sock_release(ipqnl->sk_socket);
725 down(&ipqnl_sem);
726 up(&ipqnl_sem);
728 cleanup_netlink_notifier:
729 netlink_unregister_notifier(&ipq_nl_notifier);
730 return status;
733 static int __init init(void)
736 return init_or_cleanup(1);
739 static void __exit fini(void)
741 init_or_cleanup(0);
744 MODULE_DESCRIPTION("IPv6 packet queue handler");
745 MODULE_LICENSE("GPL");
747 module_init(init);
748 module_exit(fini);