]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/ip_queue.c
[NETFILTER]: nf_queue: move queueing related functions/struct to seperate header
[net-next-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 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/ip.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/security.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/route.h>
31 #include <net/netfilter/nf_queue.h>
32
33 #define IPQ_QMAX_DEFAULT 1024
34 #define IPQ_PROC_FS_NAME "ip_queue"
35 #define NET_IPQ_QMAX 2088
36 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
37
38 struct ipq_queue_entry {
39         struct list_head list;
40         struct nf_info *info;
41         struct sk_buff *skb;
42 };
43
44 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
45
46 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
47 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
48 static DEFINE_RWLOCK(queue_lock);
49 static int peer_pid __read_mostly;
50 static unsigned int copy_range __read_mostly;
51 static unsigned int queue_total;
52 static unsigned int queue_dropped = 0;
53 static unsigned int queue_user_dropped = 0;
54 static struct sock *ipqnl __read_mostly;
55 static LIST_HEAD(queue_list);
56 static DEFINE_MUTEX(ipqnl_mutex);
57
58 static void
59 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
60 {
61         /* TCP input path (and probably other bits) assume to be called
62          * from softirq context, not from syscall, like ipq_issue_verdict is
63          * called.  TCP input path deadlocks with locks taken from timer
64          * softirq, e.g.  We therefore emulate this by local_bh_disable() */
65
66         local_bh_disable();
67         nf_reinject(entry->skb, entry->info, verdict);
68         local_bh_enable();
69
70         kfree(entry);
71 }
72
73 static inline void
74 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
75 {
76        list_add(&entry->list, &queue_list);
77        queue_total++;
78 }
79
80 /*
81  * Find and return a queued entry matched by cmpfn, or return the last
82  * entry if cmpfn is NULL.
83  */
84 static inline struct ipq_queue_entry *
85 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
86 {
87         struct list_head *p;
88
89         list_for_each_prev(p, &queue_list) {
90                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
91
92                 if (!cmpfn || cmpfn(entry, data))
93                         return entry;
94         }
95         return NULL;
96 }
97
98 static inline void
99 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
100 {
101         list_del(&entry->list);
102         queue_total--;
103 }
104
105 static inline struct ipq_queue_entry *
106 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
107 {
108         struct ipq_queue_entry *entry;
109
110         entry = __ipq_find_entry(cmpfn, data);
111         if (entry == NULL)
112                 return NULL;
113
114         __ipq_dequeue_entry(entry);
115         return entry;
116 }
117
118
119 static inline void
120 __ipq_flush(int verdict)
121 {
122         struct ipq_queue_entry *entry;
123
124         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
125                 ipq_issue_verdict(entry, verdict);
126 }
127
128 static inline int
129 __ipq_set_mode(unsigned char mode, unsigned int range)
130 {
131         int status = 0;
132
133         switch(mode) {
134         case IPQ_COPY_NONE:
135         case IPQ_COPY_META:
136                 copy_mode = mode;
137                 copy_range = 0;
138                 break;
139
140         case IPQ_COPY_PACKET:
141                 copy_mode = mode;
142                 copy_range = range;
143                 if (copy_range > 0xFFFF)
144                         copy_range = 0xFFFF;
145                 break;
146
147         default:
148                 status = -EINVAL;
149
150         }
151         return status;
152 }
153
154 static inline void
155 __ipq_reset(void)
156 {
157         peer_pid = 0;
158         net_disable_timestamp();
159         __ipq_set_mode(IPQ_COPY_NONE, 0);
160         __ipq_flush(NF_DROP);
161 }
162
163 static struct ipq_queue_entry *
164 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
165 {
166         struct ipq_queue_entry *entry;
167
168         write_lock_bh(&queue_lock);
169         entry = __ipq_find_dequeue_entry(cmpfn, data);
170         write_unlock_bh(&queue_lock);
171         return entry;
172 }
173
174 static void
175 ipq_flush(int verdict)
176 {
177         write_lock_bh(&queue_lock);
178         __ipq_flush(verdict);
179         write_unlock_bh(&queue_lock);
180 }
181
182 static struct sk_buff *
183 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
184 {
185         sk_buff_data_t old_tail;
186         size_t size = 0;
187         size_t data_len = 0;
188         struct sk_buff *skb;
189         struct ipq_packet_msg *pmsg;
190         struct nlmsghdr *nlh;
191         struct timeval tv;
192
193         read_lock_bh(&queue_lock);
194
195         switch (copy_mode) {
196         case IPQ_COPY_META:
197         case IPQ_COPY_NONE:
198                 size = NLMSG_SPACE(sizeof(*pmsg));
199                 data_len = 0;
200                 break;
201
202         case IPQ_COPY_PACKET:
203                 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
204                      entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
205                     (*errp = skb_checksum_help(entry->skb))) {
206                         read_unlock_bh(&queue_lock);
207                         return NULL;
208                 }
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         tv = ktime_to_timeval(entry->skb->tstamp);
237         pmsg->timestamp_sec   = tv.tv_sec;
238         pmsg->timestamp_usec  = tv.tv_usec;
239         pmsg->mark            = entry->skb->mark;
240         pmsg->hook            = entry->info->hook;
241         pmsg->hw_protocol     = entry->skb->protocol;
242
243         if (entry->info->indev)
244                 strcpy(pmsg->indev_name, entry->info->indev->name);
245         else
246                 pmsg->indev_name[0] = '\0';
247
248         if (entry->info->outdev)
249                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
250         else
251                 pmsg->outdev_name[0] = '\0';
252
253         if (entry->info->indev && entry->skb->dev) {
254                 pmsg->hw_type = entry->skb->dev->type;
255                 pmsg->hw_addrlen = dev_parse_header(entry->skb,
256                                                     pmsg->hw_addr);
257         }
258
259         if (data_len)
260                 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
261                         BUG();
262
263         nlh->nlmsg_len = skb->tail - old_tail;
264         return skb;
265
266 nlmsg_failure:
267         if (skb)
268                 kfree_skb(skb);
269         *errp = -EINVAL;
270         printk(KERN_ERR "ip_queue: error creating packet message\n");
271         return NULL;
272 }
273
274 static int
275 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
276                    unsigned int queuenum)
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         nskb = ipq_build_packet_message(entry, &status);
295         if (nskb == NULL)
296                 goto err_out_free;
297
298         write_lock_bh(&queue_lock);
299
300         if (!peer_pid)
301                 goto err_out_free_nskb;
302
303         if (queue_total >= queue_maxlen) {
304                 queue_dropped++;
305                 status = -ENOSPC;
306                 if (net_ratelimit())
307                           printk (KERN_WARNING "ip_queue: full at %d entries, "
308                                   "dropping packets(s). Dropped: %d\n", queue_total,
309                                   queue_dropped);
310                 goto err_out_free_nskb;
311         }
312
313         /* netlink_unicast will either free the nskb or attach it to a socket */
314         status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
315         if (status < 0) {
316                 queue_user_dropped++;
317                 goto err_out_unlock;
318         }
319
320         __ipq_enqueue_entry(entry);
321
322         write_unlock_bh(&queue_lock);
323         return status;
324
325 err_out_free_nskb:
326         kfree_skb(nskb);
327
328 err_out_unlock:
329         write_unlock_bh(&queue_lock);
330
331 err_out_free:
332         kfree(entry);
333         return status;
334 }
335
336 static int
337 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
338 {
339         int diff;
340         int err;
341         struct iphdr *user_iph = (struct iphdr *)v->payload;
342
343         if (v->data_len < sizeof(*user_iph))
344                 return 0;
345         diff = v->data_len - e->skb->len;
346         if (diff < 0) {
347                 if (pskb_trim(e->skb, v->data_len))
348                         return -ENOMEM;
349         } else if (diff > 0) {
350                 if (v->data_len > 0xFFFF)
351                         return -EINVAL;
352                 if (diff > skb_tailroom(e->skb)) {
353                         err = pskb_expand_head(e->skb, 0,
354                                                diff - skb_tailroom(e->skb),
355                                                GFP_ATOMIC);
356                         if (err) {
357                                 printk(KERN_WARNING "ip_queue: error "
358                                       "in mangle, dropping packet: %d\n", -err);
359                                 return err;
360                         }
361                 }
362                 skb_put(e->skb, diff);
363         }
364         if (!skb_make_writable(e->skb, v->data_len))
365                 return -ENOMEM;
366         skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
367         e->skb->ip_summed = CHECKSUM_NONE;
368
369         return 0;
370 }
371
372 static inline int
373 id_cmp(struct ipq_queue_entry *e, unsigned long id)
374 {
375         return (id == (unsigned long )e);
376 }
377
378 static int
379 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
380 {
381         struct ipq_queue_entry *entry;
382
383         if (vmsg->value > NF_MAX_VERDICT)
384                 return -EINVAL;
385
386         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
387         if (entry == NULL)
388                 return -ENOENT;
389         else {
390                 int verdict = vmsg->value;
391
392                 if (vmsg->data_len && vmsg->data_len == len)
393                         if (ipq_mangle_ipv4(vmsg, entry) < 0)
394                                 verdict = NF_DROP;
395
396                 ipq_issue_verdict(entry, verdict);
397                 return 0;
398         }
399 }
400
401 static int
402 ipq_set_mode(unsigned char mode, unsigned int range)
403 {
404         int status;
405
406         write_lock_bh(&queue_lock);
407         status = __ipq_set_mode(mode, range);
408         write_unlock_bh(&queue_lock);
409         return status;
410 }
411
412 static int
413 ipq_receive_peer(struct ipq_peer_msg *pmsg,
414                  unsigned char type, unsigned int len)
415 {
416         int status = 0;
417
418         if (len < sizeof(*pmsg))
419                 return -EINVAL;
420
421         switch (type) {
422         case IPQM_MODE:
423                 status = ipq_set_mode(pmsg->msg.mode.value,
424                                       pmsg->msg.mode.range);
425                 break;
426
427         case IPQM_VERDICT:
428                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
429                         status = -EINVAL;
430                 else
431                         status = ipq_set_verdict(&pmsg->msg.verdict,
432                                                  len - sizeof(*pmsg));
433                         break;
434         default:
435                 status = -EINVAL;
436         }
437         return status;
438 }
439
440 static int
441 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
442 {
443         if (entry->info->indev)
444                 if (entry->info->indev->ifindex == ifindex)
445                         return 1;
446         if (entry->info->outdev)
447                 if (entry->info->outdev->ifindex == ifindex)
448                         return 1;
449 #ifdef CONFIG_BRIDGE_NETFILTER
450         if (entry->skb->nf_bridge) {
451                 if (entry->skb->nf_bridge->physindev &&
452                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
453                         return 1;
454                 if (entry->skb->nf_bridge->physoutdev &&
455                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
456                         return 1;
457         }
458 #endif
459         return 0;
460 }
461
462 static void
463 ipq_dev_drop(int ifindex)
464 {
465         struct ipq_queue_entry *entry;
466
467         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
468                 ipq_issue_verdict(entry, NF_DROP);
469 }
470
471 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
472
473 static inline void
474 __ipq_rcv_skb(struct sk_buff *skb)
475 {
476         int status, type, pid, flags, nlmsglen, skblen;
477         struct nlmsghdr *nlh;
478
479         skblen = skb->len;
480         if (skblen < sizeof(*nlh))
481                 return;
482
483         nlh = nlmsg_hdr(skb);
484         nlmsglen = nlh->nlmsg_len;
485         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
486                 return;
487
488         pid = nlh->nlmsg_pid;
489         flags = nlh->nlmsg_flags;
490
491         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
492                 RCV_SKB_FAIL(-EINVAL);
493
494         if (flags & MSG_TRUNC)
495                 RCV_SKB_FAIL(-ECOMM);
496
497         type = nlh->nlmsg_type;
498         if (type < NLMSG_NOOP || type >= IPQM_MAX)
499                 RCV_SKB_FAIL(-EINVAL);
500
501         if (type <= IPQM_BASE)
502                 return;
503
504         if (security_netlink_recv(skb, CAP_NET_ADMIN))
505                 RCV_SKB_FAIL(-EPERM);
506
507         write_lock_bh(&queue_lock);
508
509         if (peer_pid) {
510                 if (peer_pid != pid) {
511                         write_unlock_bh(&queue_lock);
512                         RCV_SKB_FAIL(-EBUSY);
513                 }
514         } else {
515                 net_enable_timestamp();
516                 peer_pid = pid;
517         }
518
519         write_unlock_bh(&queue_lock);
520
521         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
522                                   nlmsglen - NLMSG_LENGTH(0));
523         if (status < 0)
524                 RCV_SKB_FAIL(status);
525
526         if (flags & NLM_F_ACK)
527                 netlink_ack(skb, nlh, 0);
528         return;
529 }
530
531 static void
532 ipq_rcv_skb(struct sk_buff *skb)
533 {
534         mutex_lock(&ipqnl_mutex);
535         __ipq_rcv_skb(skb);
536         mutex_unlock(&ipqnl_mutex);
537 }
538
539 static int
540 ipq_rcv_dev_event(struct notifier_block *this,
541                   unsigned long event, void *ptr)
542 {
543         struct net_device *dev = ptr;
544
545         if (dev->nd_net != &init_net)
546                 return NOTIFY_DONE;
547
548         /* Drop any packets associated with the downed device */
549         if (event == NETDEV_DOWN)
550                 ipq_dev_drop(dev->ifindex);
551         return NOTIFY_DONE;
552 }
553
554 static struct notifier_block ipq_dev_notifier = {
555         .notifier_call  = ipq_rcv_dev_event,
556 };
557
558 static int
559 ipq_rcv_nl_event(struct notifier_block *this,
560                  unsigned long event, void *ptr)
561 {
562         struct netlink_notify *n = ptr;
563
564         if (event == NETLINK_URELEASE &&
565             n->protocol == NETLINK_FIREWALL && n->pid) {
566                 write_lock_bh(&queue_lock);
567                 if ((n->net == &init_net) && (n->pid == peer_pid))
568                         __ipq_reset();
569                 write_unlock_bh(&queue_lock);
570         }
571         return NOTIFY_DONE;
572 }
573
574 static struct notifier_block ipq_nl_notifier = {
575         .notifier_call  = ipq_rcv_nl_event,
576 };
577
578 static struct ctl_table_header *ipq_sysctl_header;
579
580 static ctl_table ipq_table[] = {
581         {
582                 .ctl_name       = NET_IPQ_QMAX,
583                 .procname       = NET_IPQ_QMAX_NAME,
584                 .data           = &queue_maxlen,
585                 .maxlen         = sizeof(queue_maxlen),
586                 .mode           = 0644,
587                 .proc_handler   = proc_dointvec
588         },
589         { .ctl_name = 0 }
590 };
591
592 static ctl_table ipq_dir_table[] = {
593         {
594                 .ctl_name       = NET_IPV4,
595                 .procname       = "ipv4",
596                 .mode           = 0555,
597                 .child          = ipq_table
598         },
599         { .ctl_name = 0 }
600 };
601
602 static ctl_table ipq_root_table[] = {
603         {
604                 .ctl_name       = CTL_NET,
605                 .procname       = "net",
606                 .mode           = 0555,
607                 .child          = ipq_dir_table
608         },
609         { .ctl_name = 0 }
610 };
611
612 static int ip_queue_show(struct seq_file *m, void *v)
613 {
614         read_lock_bh(&queue_lock);
615
616         seq_printf(m,
617                       "Peer PID          : %d\n"
618                       "Copy mode         : %hu\n"
619                       "Copy range        : %u\n"
620                       "Queue length      : %u\n"
621                       "Queue max. length : %u\n"
622                       "Queue dropped     : %u\n"
623                       "Netlink dropped   : %u\n",
624                       peer_pid,
625                       copy_mode,
626                       copy_range,
627                       queue_total,
628                       queue_maxlen,
629                       queue_dropped,
630                       queue_user_dropped);
631
632         read_unlock_bh(&queue_lock);
633         return 0;
634 }
635
636 static int ip_queue_open(struct inode *inode, struct file *file)
637 {
638         return single_open(file, ip_queue_show, NULL);
639 }
640
641 static const struct file_operations ip_queue_proc_fops = {
642         .open           = ip_queue_open,
643         .read           = seq_read,
644         .llseek         = seq_lseek,
645         .release        = single_release,
646         .owner          = THIS_MODULE,
647 };
648
649 static const struct nf_queue_handler nfqh = {
650         .name   = "ip_queue",
651         .outfn  = &ipq_enqueue_packet,
652 };
653
654 static int __init ip_queue_init(void)
655 {
656         int status = -ENOMEM;
657         struct proc_dir_entry *proc;
658
659         netlink_register_notifier(&ipq_nl_notifier);
660         ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
661                                       ipq_rcv_skb, NULL, THIS_MODULE);
662         if (ipqnl == NULL) {
663                 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
664                 goto cleanup_netlink_notifier;
665         }
666
667         proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
668         if (proc) {
669                 proc->owner = THIS_MODULE;
670                 proc->proc_fops = &ip_queue_proc_fops;
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);
678
679         status = nf_register_queue_handler(PF_INET, &nfqh);
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_sysctl:
687         unregister_sysctl_table(ipq_sysctl_header);
688         unregister_netdevice_notifier(&ipq_dev_notifier);
689         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
690 cleanup_ipqnl:
691         sock_release(ipqnl->sk_socket);
692         mutex_lock(&ipqnl_mutex);
693         mutex_unlock(&ipqnl_mutex);
694
695 cleanup_netlink_notifier:
696         netlink_unregister_notifier(&ipq_nl_notifier);
697         return status;
698 }
699
700 static void __exit ip_queue_fini(void)
701 {
702         nf_unregister_queue_handlers(&nfqh);
703         synchronize_net();
704         ipq_flush(NF_DROP);
705
706         unregister_sysctl_table(ipq_sysctl_header);
707         unregister_netdevice_notifier(&ipq_dev_notifier);
708         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
709
710         sock_release(ipqnl->sk_socket);
711         mutex_lock(&ipqnl_mutex);
712         mutex_unlock(&ipqnl_mutex);
713
714         netlink_unregister_notifier(&ipq_nl_notifier);
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(ip_queue_init);
722 module_exit(ip_queue_fini);