ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 rwlock_t queue_lock = RW_LOCK_UNLOCKED;
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         __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 (copy_range == 0 || copy_range > entry->skb->len)
209                         data_len = entry->skb->len;
210                 else
211                         data_len = copy_range;
212                 
213                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
214                 break;
215         
216         default:
217                 *errp = -EINVAL;
218                 read_unlock_bh(&queue_lock);
219                 return NULL;
220         }
221
222         read_unlock_bh(&queue_lock);
223
224         skb = alloc_skb(size, GFP_ATOMIC);
225         if (!skb)
226                 goto nlmsg_failure;
227                 
228         old_tail= skb->tail;
229         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
230         pmsg = NLMSG_DATA(nlh);
231         memset(pmsg, 0, sizeof(*pmsg));
232
233         pmsg->packet_id       = (unsigned long )entry;
234         pmsg->data_len        = data_len;
235         pmsg->timestamp_sec   = entry->skb->stamp.tv_sec;
236         pmsg->timestamp_usec  = entry->skb->stamp.tv_usec;
237         pmsg->mark            = entry->skb->nfmark;
238         pmsg->hook            = entry->info->hook;
239         pmsg->hw_protocol     = entry->skb->protocol;
240         
241         if (entry->info->indev)
242                 strcpy(pmsg->indev_name, entry->info->indev->name);
243         else
244                 pmsg->indev_name[0] = '\0';
245         
246         if (entry->info->outdev)
247                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
248         else
249                 pmsg->outdev_name[0] = '\0';
250         
251         if (entry->info->indev && entry->skb->dev) {
252                 pmsg->hw_type = entry->skb->dev->type;
253                 if (entry->skb->dev->hard_header_parse)
254                         pmsg->hw_addrlen =
255                                 entry->skb->dev->hard_header_parse(entry->skb,
256                                                                    pmsg->hw_addr);
257         }
258         
259         if (data_len)
260                 memcpy(pmsg->payload, entry->skb->data, data_len);
261                 
262         nlh->nlmsg_len = skb->tail - old_tail;
263         return skb;
264
265 nlmsg_failure:
266         if (skb)
267                 kfree_skb(skb);
268         *errp = -EINVAL;
269         printk(KERN_ERR "ip_queue: error creating packet message\n");
270         return NULL;
271 }
272
273 static int
274 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
275 {
276         int status = -EINVAL;
277         struct sk_buff *nskb;
278         struct ipq_queue_entry *entry;
279
280         if (copy_mode == IPQ_COPY_NONE)
281                 return -EAGAIN;
282
283         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
284         if (entry == NULL) {
285                 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
286                 return -ENOMEM;
287         }
288
289         entry->info = info;
290         entry->skb = skb;
291
292         if (entry->info->hook == NF_IP_LOCAL_OUT) {
293                 struct iphdr *iph = skb->nh.iph;
294
295                 entry->rt_info.tos = iph->tos;
296                 entry->rt_info.daddr = iph->daddr;
297                 entry->rt_info.saddr = iph->saddr;
298         }
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         /* 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                 goto err_out_unlock;
313         
314         status = __ipq_enqueue_entry(entry);
315         if (status < 0)
316                 goto err_out_unlock;
317
318         write_unlock_bh(&queue_lock);
319         return status;
320
321 err_out_free_nskb:
322         kfree_skb(nskb); 
323         
324 err_out_unlock:
325         write_unlock_bh(&queue_lock);
326
327 err_out_free:
328         kfree(entry);
329         return status;
330 }
331
332 static int
333 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
334 {
335         int diff;
336         struct iphdr *user_iph = (struct iphdr *)v->payload;
337
338         if (v->data_len < sizeof(*user_iph))
339                 return 0;
340         diff = v->data_len - e->skb->len;
341         if (diff < 0)
342                 skb_trim(e->skb, v->data_len);
343         else if (diff > 0) {
344                 if (v->data_len > 0xFFFF)
345                         return -EINVAL;
346                 if (diff > skb_tailroom(e->skb)) {
347                         struct sk_buff *newskb;
348                         
349                         newskb = skb_copy_expand(e->skb,
350                                                  skb_headroom(e->skb),
351                                                  diff,
352                                                  GFP_ATOMIC);
353                         if (newskb == NULL) {
354                                 printk(KERN_WARNING "ip_queue: OOM "
355                                       "in mangle, dropping packet\n");
356                                 return -ENOMEM;
357                         }
358                         if (e->skb->sk)
359                                 skb_set_owner_w(newskb, e->skb->sk);
360                         kfree_skb(e->skb);
361                         e->skb = newskb;
362                 }
363                 skb_put(e->skb, diff);
364         }
365         memcpy(e->skb->data, v->payload, v->data_len);
366         e->skb->nfcache |= NFC_ALTERED;
367
368         /*
369          * Extra routing may needed on local out, as the QUEUE target never
370          * returns control to the table.
371          */
372         if (e->info->hook == NF_IP_LOCAL_OUT) {
373                 struct iphdr *iph = e->skb->nh.iph;
374
375                 if (!(iph->tos == e->rt_info.tos
376                       && iph->daddr == e->rt_info.daddr
377                       && iph->saddr == e->rt_info.saddr))
378                         return ip_route_me_harder(&e->skb);
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_ipv4(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         }
518         else
519                 peer_pid = pid;
520                 
521         write_unlock_bh(&queue_lock);
522         
523         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
524                                   skblen - NLMSG_LENGTH(0));
525         if (status < 0)
526                 RCV_SKB_FAIL(status);
527                 
528         if (flags & NLM_F_ACK)
529                 netlink_ack(skb, nlh, 0);
530         return;
531 }
532
533 static void
534 ipq_rcv_sk(struct sock *sk, int len)
535 {
536         do {
537                 struct sk_buff *skb;
538
539                 if (down_trylock(&ipqnl_sem))
540                         return;
541                         
542                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
543                         ipq_rcv_skb(skb);
544                         kfree_skb(skb);
545                 }
546                 
547                 up(&ipqnl_sem);
548
549         } while (ipqnl && ipqnl->sk_receive_queue.qlen);
550 }
551
552 static int
553 ipq_rcv_dev_event(struct notifier_block *this,
554                   unsigned long event, void *ptr)
555 {
556         struct net_device *dev = ptr;
557
558         /* Drop any packets associated with the downed device */
559         if (event == NETDEV_DOWN)
560                 ipq_dev_drop(dev->ifindex);
561         return NOTIFY_DONE;
562 }
563
564 static struct notifier_block ipq_dev_notifier = {
565         .notifier_call  = ipq_rcv_dev_event,
566 };
567
568 static int
569 ipq_rcv_nl_event(struct notifier_block *this,
570                  unsigned long event, void *ptr)
571 {
572         struct netlink_notify *n = ptr;
573
574         if (event == NETLINK_URELEASE &&
575             n->protocol == NETLINK_FIREWALL && n->pid) {
576                 write_lock_bh(&queue_lock);
577                 if (n->pid == peer_pid)
578                         __ipq_reset();
579                 write_unlock_bh(&queue_lock);
580         }
581         return NOTIFY_DONE;
582 }
583
584 static struct notifier_block ipq_nl_notifier = {
585         .notifier_call  = ipq_rcv_nl_event,
586 };
587
588 static struct ctl_table_header *ipq_sysctl_header;
589
590 static ctl_table ipq_table[] = {
591         {
592                 .ctl_name       = NET_IPQ_QMAX,
593                 .procname       = NET_IPQ_QMAX_NAME,
594                 .data           = &queue_maxlen,
595                 .maxlen         = sizeof(queue_maxlen),
596                 .mode           = 0644,
597                 .proc_handler   = proc_dointvec
598         },
599         { .ctl_name = 0 }
600 };
601
602 static ctl_table ipq_dir_table[] = {
603         {
604                 .ctl_name       = NET_IPV4,
605                 .procname       = "ipv4",
606                 .mode           = 0555,
607                 .child          = ipq_table
608         },
609         { .ctl_name = 0 }
610 };
611
612 static ctl_table ipq_root_table[] = {
613         {
614                 .ctl_name       = CTL_NET,
615                 .procname       = "net",
616                 .mode           = 0555,
617                 .child          = ipq_dir_table
618         },
619         { .ctl_name = 0 }
620 };
621
622 static int
623 ipq_get_info(char *buffer, char **start, off_t offset, int length)
624 {
625         int len;
626
627         read_lock_bh(&queue_lock);
628         
629         len = sprintf(buffer,
630                       "Peer PID          : %d\n"
631                       "Copy mode         : %hu\n"
632                       "Copy range        : %u\n"
633                       "Queue length      : %u\n"
634                       "Queue max. length : %u\n",
635                       peer_pid,
636                       copy_mode,
637                       copy_range,
638                       queue_total,
639                       queue_maxlen);
640
641         read_unlock_bh(&queue_lock);
642         
643         *start = buffer + offset;
644         len -= offset;
645         if (len > length)
646                 len = length;
647         else if (len < 0)
648                 len = 0;
649         return len;
650 }
651
652 static int
653 init_or_cleanup(int init)
654 {
655         int status = -ENOMEM;
656         struct proc_dir_entry *proc;
657         
658         if (!init)
659                 goto cleanup;
660
661         netlink_register_notifier(&ipq_nl_notifier);
662         ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
663         if (ipqnl == NULL) {
664                 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
665                 goto cleanup_netlink_notifier;
666         }
667
668         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
669         if (proc)
670                 proc->owner = THIS_MODULE;
671         else {
672                 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
673                 goto cleanup_ipqnl;
674         }
675         
676         register_netdevice_notifier(&ipq_dev_notifier);
677         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
678         
679         status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
680         if (status < 0) {
681                 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
682                 goto cleanup_sysctl;
683         }
684         return status;
685
686 cleanup:
687         nf_unregister_queue_handler(PF_INET);
688         synchronize_net();
689         ipq_flush(NF_DROP);
690         
691 cleanup_sysctl:
692         unregister_sysctl_table(ipq_sysctl_header);
693         unregister_netdevice_notifier(&ipq_dev_notifier);
694         proc_net_remove(IPQ_PROC_FS_NAME);
695         
696 cleanup_ipqnl:
697         sock_release(ipqnl->sk_socket);
698         down(&ipqnl_sem);
699         up(&ipqnl_sem);
700         
701 cleanup_netlink_notifier:
702         netlink_unregister_notifier(&ipq_nl_notifier);
703         return status;
704 }
705
706 static int __init init(void)
707 {
708         
709         return init_or_cleanup(1);
710 }
711
712 static void __exit fini(void)
713 {
714         init_or_cleanup(0);
715 }
716
717 MODULE_DESCRIPTION("IPv4 packet queue handler");
718 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
719 MODULE_LICENSE("GPL");
720
721 module_init(init);
722 module_exit(fini);