[IRDA] sem2mutex: drivers/net/irda
[linux-2.6.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  * 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.
26  */
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>
44
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"
49
50 struct ipq_queue_entry {
51         struct list_head list;
52         struct nf_info *info;
53         struct sk_buff *skb;
54 };
55
56 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
57
58 static unsigned char copy_mode = IPQ_COPY_NONE;
59 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
60 static DEFINE_RWLOCK(queue_lock);
61 static int peer_pid;
62 static unsigned int copy_range;
63 static unsigned int queue_total;
64 static unsigned int queue_dropped = 0;
65 static unsigned int queue_user_dropped = 0;
66 static struct sock *ipqnl;
67 static LIST_HEAD(queue_list);
68 static DECLARE_MUTEX(ipqnl_sem);
69
70 static void
71 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
72 {
73         local_bh_disable();
74         nf_reinject(entry->skb, entry->info, verdict);
75         local_bh_enable();
76         kfree(entry);
77 }
78
79 static inline void
80 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
81 {
82        list_add(&entry->list, &queue_list);
83        queue_total++;
84 }
85
86 /*
87  * Find and return a queued entry matched by cmpfn, or return the last
88  * entry if cmpfn is NULL.
89  */
90 static inline struct ipq_queue_entry *
91 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
92 {
93         struct list_head *p;
94
95         list_for_each_prev(p, &queue_list) {
96                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
97                 
98                 if (!cmpfn || cmpfn(entry, data))
99                         return entry;
100         }
101         return NULL;
102 }
103
104 static inline void
105 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
106 {
107         list_del(&entry->list);
108         queue_total--;
109 }
110
111 static inline struct ipq_queue_entry *
112 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
113 {
114         struct ipq_queue_entry *entry;
115
116         entry = __ipq_find_entry(cmpfn, data);
117         if (entry == NULL)
118                 return NULL;
119
120         __ipq_dequeue_entry(entry);
121         return entry;
122 }
123
124
125 static inline void
126 __ipq_flush(int verdict)
127 {
128         struct ipq_queue_entry *entry;
129         
130         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
131                 ipq_issue_verdict(entry, verdict);
132 }
133
134 static inline int
135 __ipq_set_mode(unsigned char mode, unsigned int range)
136 {
137         int status = 0;
138         
139         switch(mode) {
140         case IPQ_COPY_NONE:
141         case IPQ_COPY_META:
142                 copy_mode = mode;
143                 copy_range = 0;
144                 break;
145                 
146         case IPQ_COPY_PACKET:
147                 copy_mode = mode;
148                 copy_range = range;
149                 if (copy_range > 0xFFFF)
150                         copy_range = 0xFFFF;
151                 break;
152                 
153         default:
154                 status = -EINVAL;
155
156         }
157         return status;
158 }
159
160 static inline void
161 __ipq_reset(void)
162 {
163         peer_pid = 0;
164         net_disable_timestamp();
165         __ipq_set_mode(IPQ_COPY_NONE, 0);
166         __ipq_flush(NF_DROP);
167 }
168
169 static struct ipq_queue_entry *
170 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
171 {
172         struct ipq_queue_entry *entry;
173         
174         write_lock_bh(&queue_lock);
175         entry = __ipq_find_dequeue_entry(cmpfn, data);
176         write_unlock_bh(&queue_lock);
177         return entry;
178 }
179
180 static void
181 ipq_flush(int verdict)
182 {
183         write_lock_bh(&queue_lock);
184         __ipq_flush(verdict);
185         write_unlock_bh(&queue_lock);
186 }
187
188 static struct sk_buff *
189 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
190 {
191         unsigned char *old_tail;
192         size_t size = 0;
193         size_t data_len = 0;
194         struct sk_buff *skb;
195         struct ipq_packet_msg *pmsg;
196         struct nlmsghdr *nlh;
197
198         read_lock_bh(&queue_lock);
199         
200         switch (copy_mode) {
201         case IPQ_COPY_META:
202         case IPQ_COPY_NONE:
203                 size = NLMSG_SPACE(sizeof(*pmsg));
204                 data_len = 0;
205                 break;
206         
207         case IPQ_COPY_PACKET:
208                 if (entry->skb->ip_summed == CHECKSUM_HW &&
209                     (*errp = skb_checksum_help(entry->skb,
210                                                entry->info->outdev == NULL))) {
211                         read_unlock_bh(&queue_lock);
212                         return NULL;
213                 }
214                 if (copy_range == 0 || copy_range > entry->skb->len)
215                         data_len = entry->skb->len;
216                 else
217                         data_len = copy_range;
218                 
219                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
220                 break;
221         
222         default:
223                 *errp = -EINVAL;
224                 read_unlock_bh(&queue_lock);
225                 return NULL;
226         }
227
228         read_unlock_bh(&queue_lock);
229
230         skb = alloc_skb(size, GFP_ATOMIC);
231         if (!skb)
232                 goto nlmsg_failure;
233                 
234         old_tail= skb->tail;
235         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
236         pmsg = NLMSG_DATA(nlh);
237         memset(pmsg, 0, sizeof(*pmsg));
238
239         pmsg->packet_id       = (unsigned long )entry;
240         pmsg->data_len        = data_len;
241         pmsg->timestamp_sec   = entry->skb->tstamp.off_sec;
242         pmsg->timestamp_usec  = entry->skb->tstamp.off_usec;
243         pmsg->mark            = entry->skb->nfmark;
244         pmsg->hook            = entry->info->hook;
245         pmsg->hw_protocol     = entry->skb->protocol;
246         
247         if (entry->info->indev)
248                 strcpy(pmsg->indev_name, entry->info->indev->name);
249         else
250                 pmsg->indev_name[0] = '\0';
251         
252         if (entry->info->outdev)
253                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
254         else
255                 pmsg->outdev_name[0] = '\0';
256         
257         if (entry->info->indev && entry->skb->dev) {
258                 pmsg->hw_type = entry->skb->dev->type;
259                 if (entry->skb->dev->hard_header_parse)
260                         pmsg->hw_addrlen =
261                                 entry->skb->dev->hard_header_parse(entry->skb,
262                                                                    pmsg->hw_addr);
263         }
264         
265         if (data_len)
266                 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
267                         BUG();
268                 
269         nlh->nlmsg_len = skb->tail - old_tail;
270         return skb;
271
272 nlmsg_failure:
273         if (skb)
274                 kfree_skb(skb);
275         *errp = -EINVAL;
276         printk(KERN_ERR "ip6_queue: error creating packet message\n");
277         return NULL;
278 }
279
280 static int
281 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, 
282                    unsigned int queuenum, void *data)
283 {
284         int status = -EINVAL;
285         struct sk_buff *nskb;
286         struct ipq_queue_entry *entry;
287
288         if (copy_mode == IPQ_COPY_NONE)
289                 return -EAGAIN;
290
291         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
292         if (entry == NULL) {
293                 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
294                 return -ENOMEM;
295         }
296
297         entry->info = info;
298         entry->skb = skb;
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 "ip6_queue: fill at %d entries, "
314                                 "dropping packet(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_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
344 {
345         int diff;
346         struct ipv6hdr *user_iph = (struct ipv6hdr *)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 "ip6_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_make_writable(&e->skb, v->data_len))
376                 return -ENOMEM;
377         memcpy(e->skb->data, v->payload, v->data_len);
378         e->skb->ip_summed = CHECKSUM_NONE;
379
380         return 0;
381 }
382
383 static inline int
384 id_cmp(struct ipq_queue_entry *e, unsigned long id)
385 {
386         return (id == (unsigned long )e);
387 }
388
389 static int
390 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
391 {
392         struct ipq_queue_entry *entry;
393
394         if (vmsg->value > NF_MAX_VERDICT)
395                 return -EINVAL;
396
397         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
398         if (entry == NULL)
399                 return -ENOENT;
400         else {
401                 int verdict = vmsg->value;
402                 
403                 if (vmsg->data_len && vmsg->data_len == len)
404                         if (ipq_mangle_ipv6(vmsg, entry) < 0)
405                                 verdict = NF_DROP;
406                 
407                 ipq_issue_verdict(entry, verdict);
408                 return 0;
409         }
410 }
411
412 static int
413 ipq_set_mode(unsigned char mode, unsigned int range)
414 {
415         int status;
416
417         write_lock_bh(&queue_lock);
418         status = __ipq_set_mode(mode, range);
419         write_unlock_bh(&queue_lock);
420         return status;
421 }
422
423 static int
424 ipq_receive_peer(struct ipq_peer_msg *pmsg,
425                  unsigned char type, unsigned int len)
426 {
427         int status = 0;
428
429         if (len < sizeof(*pmsg))
430                 return -EINVAL;
431
432         switch (type) {
433         case IPQM_MODE:
434                 status = ipq_set_mode(pmsg->msg.mode.value,
435                                       pmsg->msg.mode.range);
436                 break;
437                 
438         case IPQM_VERDICT:
439                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
440                         status = -EINVAL;
441                 else
442                         status = ipq_set_verdict(&pmsg->msg.verdict,
443                                                  len - sizeof(*pmsg));
444                         break;
445         default:
446                 status = -EINVAL;
447         }
448         return status;
449 }
450
451 static int
452 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
453 {
454         if (entry->info->indev)
455                 if (entry->info->indev->ifindex == ifindex)
456                         return 1;
457                         
458         if (entry->info->outdev)
459                 if (entry->info->outdev->ifindex == ifindex)
460                         return 1;
461
462         return 0;
463 }
464
465 static void
466 ipq_dev_drop(int ifindex)
467 {
468         struct ipq_queue_entry *entry;
469         
470         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
471                 ipq_issue_verdict(entry, NF_DROP);
472 }
473
474 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
475
476 static inline void
477 ipq_rcv_skb(struct sk_buff *skb)
478 {
479         int status, type, pid, flags, nlmsglen, skblen;
480         struct nlmsghdr *nlh;
481
482         skblen = skb->len;
483         if (skblen < sizeof(*nlh))
484                 return;
485
486         nlh = (struct nlmsghdr *)skb->data;
487         nlmsglen = nlh->nlmsg_len;
488         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
489                 return;
490
491         pid = nlh->nlmsg_pid;
492         flags = nlh->nlmsg_flags;
493         
494         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
495                 RCV_SKB_FAIL(-EINVAL);
496                 
497         if (flags & MSG_TRUNC)
498                 RCV_SKB_FAIL(-ECOMM);
499                 
500         type = nlh->nlmsg_type;
501         if (type < NLMSG_NOOP || type >= IPQM_MAX)
502                 RCV_SKB_FAIL(-EINVAL);
503                 
504         if (type <= IPQM_BASE)
505                 return;
506         
507         if (security_netlink_recv(skb))
508                 RCV_SKB_FAIL(-EPERM);   
509
510         write_lock_bh(&queue_lock);
511         
512         if (peer_pid) {
513                 if (peer_pid != pid) {
514                         write_unlock_bh(&queue_lock);
515                         RCV_SKB_FAIL(-EBUSY);
516                 }
517         } else {
518                 net_enable_timestamp();
519                 peer_pid = pid;
520         }
521                 
522         write_unlock_bh(&queue_lock);
523         
524         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
525                                   nlmsglen - NLMSG_LENGTH(0));
526         if (status < 0)
527                 RCV_SKB_FAIL(status);
528                 
529         if (flags & NLM_F_ACK)
530                 netlink_ack(skb, nlh, 0);
531         return;
532 }
533
534 static void
535 ipq_rcv_sk(struct sock *sk, int len)
536 {
537         struct sk_buff *skb;
538         unsigned int qlen;
539
540         down(&ipqnl_sem);
541                         
542         for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
543                 skb = skb_dequeue(&sk->sk_receive_queue);
544                 ipq_rcv_skb(skb);
545                 kfree_skb(skb);
546         }
547                 
548         up(&ipqnl_sem);
549 }
550
551 static int
552 ipq_rcv_dev_event(struct notifier_block *this,
553                   unsigned long event, void *ptr)
554 {
555         struct net_device *dev = ptr;
556
557         /* Drop any packets associated with the downed device */
558         if (event == NETDEV_DOWN)
559                 ipq_dev_drop(dev->ifindex);
560         return NOTIFY_DONE;
561 }
562
563 static struct notifier_block ipq_dev_notifier = {
564         .notifier_call  = ipq_rcv_dev_event,
565 };
566
567 static int
568 ipq_rcv_nl_event(struct notifier_block *this,
569                  unsigned long event, void *ptr)
570 {
571         struct netlink_notify *n = ptr;
572
573         if (event == NETLINK_URELEASE &&
574             n->protocol == NETLINK_IP6_FW && n->pid) {
575                 write_lock_bh(&queue_lock);
576                 if (n->pid == peer_pid)
577                         __ipq_reset();
578                 write_unlock_bh(&queue_lock);
579         }
580         return NOTIFY_DONE;
581 }
582
583 static struct notifier_block ipq_nl_notifier = {
584         .notifier_call  = ipq_rcv_nl_event,
585 };
586
587 static struct ctl_table_header *ipq_sysctl_header;
588
589 static ctl_table ipq_table[] = {
590         {
591                 .ctl_name       = NET_IPQ_QMAX,
592                 .procname       = NET_IPQ_QMAX_NAME,
593                 .data           = &queue_maxlen,
594                 .maxlen         = sizeof(queue_maxlen),
595                 .mode           = 0644,
596                 .proc_handler   = proc_dointvec
597         },
598         { .ctl_name = 0 }
599 };
600
601 static ctl_table ipq_dir_table[] = {
602         {
603                 .ctl_name       = NET_IPV6,
604                 .procname       = "ipv6",
605                 .mode           = 0555,
606                 .child          = ipq_table
607         },
608         { .ctl_name = 0 }
609 };
610
611 static ctl_table ipq_root_table[] = {
612         {
613                 .ctl_name       = CTL_NET,
614                 .procname       = "net",
615                 .mode           = 0555,
616                 .child          = ipq_dir_table
617         },
618         { .ctl_name = 0 }
619 };
620
621 static int
622 ipq_get_info(char *buffer, char **start, off_t offset, int length)
623 {
624         int len;
625
626         read_lock_bh(&queue_lock);
627         
628         len = sprintf(buffer,
629                       "Peer PID          : %d\n"
630                       "Copy mode         : %hu\n"
631                       "Copy range        : %u\n"
632                       "Queue length      : %u\n"
633                       "Queue max. length : %u\n"
634                       "Queue dropped     : %u\n"
635                       "Netfilter dropped : %u\n",
636                       peer_pid,
637                       copy_mode,
638                       copy_range,
639                       queue_total,
640                       queue_maxlen,
641                       queue_dropped,
642                       queue_user_dropped);
643
644         read_unlock_bh(&queue_lock);
645         
646         *start = buffer + offset;
647         len -= offset;
648         if (len > length)
649                 len = length;
650         else if (len < 0)
651                 len = 0;
652         return len;
653 }
654
655 static struct nf_queue_handler nfqh = {
656         .name   = "ip6_queue",
657         .outfn  = &ipq_enqueue_packet,
658 };
659
660 static int
661 init_or_cleanup(int init)
662 {
663         int status = -ENOMEM;
664         struct proc_dir_entry *proc;
665         
666         if (!init)
667                 goto cleanup;
668
669         netlink_register_notifier(&ipq_nl_notifier);
670         ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk,
671                                       THIS_MODULE);
672         if (ipqnl == NULL) {
673                 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
674                 goto cleanup_netlink_notifier;
675         }
676
677         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
678         if (proc)
679                 proc->owner = THIS_MODULE;
680         else {
681                 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
682                 goto cleanup_ipqnl;
683         }
684         
685         register_netdevice_notifier(&ipq_dev_notifier);
686         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
687         
688         status = nf_register_queue_handler(PF_INET6, &nfqh);
689         if (status < 0) {
690                 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
691                 goto cleanup_sysctl;
692         }
693         return status;
694
695 cleanup:
696         nf_unregister_queue_handlers(&nfqh);
697         synchronize_net();
698         ipq_flush(NF_DROP);
699         
700 cleanup_sysctl:
701         unregister_sysctl_table(ipq_sysctl_header);
702         unregister_netdevice_notifier(&ipq_dev_notifier);
703         proc_net_remove(IPQ_PROC_FS_NAME);
704         
705 cleanup_ipqnl:
706         sock_release(ipqnl->sk_socket);
707         down(&ipqnl_sem);
708         up(&ipqnl_sem);
709         
710 cleanup_netlink_notifier:
711         netlink_unregister_notifier(&ipq_nl_notifier);
712         return status;
713 }
714
715 static int __init init(void)
716 {
717         
718         return init_or_cleanup(1);
719 }
720
721 static void __exit fini(void)
722 {
723         init_or_cleanup(0);
724 }
725
726 MODULE_DESCRIPTION("IPv6 packet queue handler");
727 MODULE_LICENSE("GPL");
728
729 module_init(init);
730 module_exit(fini);