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