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