]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/ip_queue.c
[NET]: Wrap hard_header_parse
[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/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         struct iphdr *user_iph = (struct iphdr *)v->payload;
339
340         if (v->data_len < sizeof(*user_iph))
341                 return 0;
342         diff = v->data_len - e->skb->len;
343         if (diff < 0) {
344                 if (pskb_trim(e->skb, v->data_len))
345                         return -ENOMEM;
346         } else if (diff > 0) {
347                 if (v->data_len > 0xFFFF)
348                         return -EINVAL;
349                 if (diff > skb_tailroom(e->skb)) {
350                         struct sk_buff *newskb;
351
352                         newskb = skb_copy_expand(e->skb,
353                                                  skb_headroom(e->skb),
354                                                  diff,
355                                                  GFP_ATOMIC);
356                         if (newskb == NULL) {
357                                 printk(KERN_WARNING "ip_queue: OOM "
358                                       "in mangle, dropping packet\n");
359                                 return -ENOMEM;
360                         }
361                         if (e->skb->sk)
362                                 skb_set_owner_w(newskb, e->skb->sk);
363                         kfree_skb(e->skb);
364                         e->skb = newskb;
365                 }
366                 skb_put(e->skb, diff);
367         }
368         if (!skb_make_writable(&e->skb, v->data_len))
369                 return -ENOMEM;
370         skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
371         e->skb->ip_summed = CHECKSUM_NONE;
372
373         return 0;
374 }
375
376 static inline int
377 id_cmp(struct ipq_queue_entry *e, unsigned long id)
378 {
379         return (id == (unsigned long )e);
380 }
381
382 static int
383 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
384 {
385         struct ipq_queue_entry *entry;
386
387         if (vmsg->value > NF_MAX_VERDICT)
388                 return -EINVAL;
389
390         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
391         if (entry == NULL)
392                 return -ENOENT;
393         else {
394                 int verdict = vmsg->value;
395
396                 if (vmsg->data_len && vmsg->data_len == len)
397                         if (ipq_mangle_ipv4(vmsg, entry) < 0)
398                                 verdict = NF_DROP;
399
400                 ipq_issue_verdict(entry, verdict);
401                 return 0;
402         }
403 }
404
405 static int
406 ipq_set_mode(unsigned char mode, unsigned int range)
407 {
408         int status;
409
410         write_lock_bh(&queue_lock);
411         status = __ipq_set_mode(mode, range);
412         write_unlock_bh(&queue_lock);
413         return status;
414 }
415
416 static int
417 ipq_receive_peer(struct ipq_peer_msg *pmsg,
418                  unsigned char type, unsigned int len)
419 {
420         int status = 0;
421
422         if (len < sizeof(*pmsg))
423                 return -EINVAL;
424
425         switch (type) {
426         case IPQM_MODE:
427                 status = ipq_set_mode(pmsg->msg.mode.value,
428                                       pmsg->msg.mode.range);
429                 break;
430
431         case IPQM_VERDICT:
432                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
433                         status = -EINVAL;
434                 else
435                         status = ipq_set_verdict(&pmsg->msg.verdict,
436                                                  len - sizeof(*pmsg));
437                         break;
438         default:
439                 status = -EINVAL;
440         }
441         return status;
442 }
443
444 static int
445 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
446 {
447         if (entry->info->indev)
448                 if (entry->info->indev->ifindex == ifindex)
449                         return 1;
450         if (entry->info->outdev)
451                 if (entry->info->outdev->ifindex == ifindex)
452                         return 1;
453 #ifdef CONFIG_BRIDGE_NETFILTER
454         if (entry->skb->nf_bridge) {
455                 if (entry->skb->nf_bridge->physindev &&
456                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
457                         return 1;
458                 if (entry->skb->nf_bridge->physoutdev &&
459                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
460                         return 1;
461         }
462 #endif
463         return 0;
464 }
465
466 static void
467 ipq_dev_drop(int ifindex)
468 {
469         struct ipq_queue_entry *entry;
470
471         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
472                 ipq_issue_verdict(entry, NF_DROP);
473 }
474
475 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
476
477 static inline void
478 ipq_rcv_skb(struct sk_buff *skb)
479 {
480         int status, type, pid, flags, nlmsglen, skblen;
481         struct nlmsghdr *nlh;
482
483         skblen = skb->len;
484         if (skblen < sizeof(*nlh))
485                 return;
486
487         nlh = nlmsg_hdr(skb);
488         nlmsglen = nlh->nlmsg_len;
489         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
490                 return;
491
492         pid = nlh->nlmsg_pid;
493         flags = nlh->nlmsg_flags;
494
495         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
496                 RCV_SKB_FAIL(-EINVAL);
497
498         if (flags & MSG_TRUNC)
499                 RCV_SKB_FAIL(-ECOMM);
500
501         type = nlh->nlmsg_type;
502         if (type < NLMSG_NOOP || type >= IPQM_MAX)
503                 RCV_SKB_FAIL(-EINVAL);
504
505         if (type <= IPQM_BASE)
506                 return;
507
508         if (security_netlink_recv(skb, CAP_NET_ADMIN))
509                 RCV_SKB_FAIL(-EPERM);
510
511         write_lock_bh(&queue_lock);
512
513         if (peer_pid) {
514                 if (peer_pid != pid) {
515                         write_unlock_bh(&queue_lock);
516                         RCV_SKB_FAIL(-EBUSY);
517                 }
518         } else {
519                 net_enable_timestamp();
520                 peer_pid = pid;
521         }
522
523         write_unlock_bh(&queue_lock);
524
525         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
526                                   nlmsglen - NLMSG_LENGTH(0));
527         if (status < 0)
528                 RCV_SKB_FAIL(status);
529
530         if (flags & NLM_F_ACK)
531                 netlink_ack(skb, nlh, 0);
532         return;
533 }
534
535 static void
536 ipq_rcv_sk(struct sock *sk, int len)
537 {
538         struct sk_buff *skb;
539         unsigned int qlen;
540
541         mutex_lock(&ipqnl_mutex);
542
543         for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544                 skb = skb_dequeue(&sk->sk_receive_queue);
545                 ipq_rcv_skb(skb);
546                 kfree_skb(skb);
547         }
548
549         mutex_unlock(&ipqnl_mutex);
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         if (dev->nd_net != &init_net)
559                 return NOTIFY_DONE;
560
561         /* Drop any packets associated with the downed device */
562         if (event == NETDEV_DOWN)
563                 ipq_dev_drop(dev->ifindex);
564         return NOTIFY_DONE;
565 }
566
567 static struct notifier_block ipq_dev_notifier = {
568         .notifier_call  = ipq_rcv_dev_event,
569 };
570
571 static int
572 ipq_rcv_nl_event(struct notifier_block *this,
573                  unsigned long event, void *ptr)
574 {
575         struct netlink_notify *n = ptr;
576
577         if (event == NETLINK_URELEASE &&
578             n->protocol == NETLINK_FIREWALL && n->pid) {
579                 write_lock_bh(&queue_lock);
580                 if ((n->net == &init_net) && (n->pid == peer_pid))
581                         __ipq_reset();
582                 write_unlock_bh(&queue_lock);
583         }
584         return NOTIFY_DONE;
585 }
586
587 static struct notifier_block ipq_nl_notifier = {
588         .notifier_call  = ipq_rcv_nl_event,
589 };
590
591 static struct ctl_table_header *ipq_sysctl_header;
592
593 static ctl_table ipq_table[] = {
594         {
595                 .ctl_name       = NET_IPQ_QMAX,
596                 .procname       = NET_IPQ_QMAX_NAME,
597                 .data           = &queue_maxlen,
598                 .maxlen         = sizeof(queue_maxlen),
599                 .mode           = 0644,
600                 .proc_handler   = proc_dointvec
601         },
602         { .ctl_name = 0 }
603 };
604
605 static ctl_table ipq_dir_table[] = {
606         {
607                 .ctl_name       = NET_IPV4,
608                 .procname       = "ipv4",
609                 .mode           = 0555,
610                 .child          = ipq_table
611         },
612         { .ctl_name = 0 }
613 };
614
615 static ctl_table ipq_root_table[] = {
616         {
617                 .ctl_name       = CTL_NET,
618                 .procname       = "net",
619                 .mode           = 0555,
620                 .child          = ipq_dir_table
621         },
622         { .ctl_name = 0 }
623 };
624
625 #ifdef CONFIG_PROC_FS
626 static int
627 ipq_get_info(char *buffer, char **start, off_t offset, int length)
628 {
629         int len;
630
631         read_lock_bh(&queue_lock);
632
633         len = sprintf(buffer,
634                       "Peer PID          : %d\n"
635                       "Copy mode         : %hu\n"
636                       "Copy range        : %u\n"
637                       "Queue length      : %u\n"
638                       "Queue max. length : %u\n"
639                       "Queue dropped     : %u\n"
640                       "Netlink dropped   : %u\n",
641                       peer_pid,
642                       copy_mode,
643                       copy_range,
644                       queue_total,
645                       queue_maxlen,
646                       queue_dropped,
647                       queue_user_dropped);
648
649         read_unlock_bh(&queue_lock);
650
651         *start = buffer + offset;
652         len -= offset;
653         if (len > length)
654                 len = length;
655         else if (len < 0)
656                 len = 0;
657         return len;
658 }
659 #endif /* CONFIG_PROC_FS */
660
661 static struct nf_queue_handler nfqh = {
662         .name   = "ip_queue",
663         .outfn  = &ipq_enqueue_packet,
664 };
665
666 static int __init ip_queue_init(void)
667 {
668         int status = -ENOMEM;
669         struct proc_dir_entry *proc;
670
671         netlink_register_notifier(&ipq_nl_notifier);
672         ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
673                                       ipq_rcv_sk, NULL, THIS_MODULE);
674         if (ipqnl == NULL) {
675                 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
676                 goto cleanup_netlink_notifier;
677         }
678
679         proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
680         if (proc)
681                 proc->owner = THIS_MODULE;
682         else {
683                 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
684                 goto cleanup_ipqnl;
685         }
686
687         register_netdevice_notifier(&ipq_dev_notifier);
688         ipq_sysctl_header = register_sysctl_table(ipq_root_table);
689
690         status = nf_register_queue_handler(PF_INET, &nfqh);
691         if (status < 0) {
692                 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
693                 goto cleanup_sysctl;
694         }
695         return status;
696
697 cleanup_sysctl:
698         unregister_sysctl_table(ipq_sysctl_header);
699         unregister_netdevice_notifier(&ipq_dev_notifier);
700         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
701 cleanup_ipqnl:
702         sock_release(ipqnl->sk_socket);
703         mutex_lock(&ipqnl_mutex);
704         mutex_unlock(&ipqnl_mutex);
705
706 cleanup_netlink_notifier:
707         netlink_unregister_notifier(&ipq_nl_notifier);
708         return status;
709 }
710
711 static void __exit ip_queue_fini(void)
712 {
713         nf_unregister_queue_handlers(&nfqh);
714         synchronize_net();
715         ipq_flush(NF_DROP);
716
717         unregister_sysctl_table(ipq_sysctl_header);
718         unregister_netdevice_notifier(&ipq_dev_notifier);
719         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
720
721         sock_release(ipqnl->sk_socket);
722         mutex_lock(&ipqnl_mutex);
723         mutex_unlock(&ipqnl_mutex);
724
725         netlink_unregister_notifier(&ipq_nl_notifier);
726 }
727
728 MODULE_DESCRIPTION("IPv4 packet queue handler");
729 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
730 MODULE_LICENSE("GPL");
731
732 module_init(ip_queue_init);
733 module_exit(ip_queue_fini);