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