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