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