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