more changes on original files
[linux-2.4.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  * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
12  *             to adapt it to IPv6
13  *             HEAVILY based in ipqueue.c by James Morris. It's just
14  *             a little modified version of it, so he's nearly the
15  *             real coder of this.
16  *             Few changes needed, mainly the hard_routing code and
17  *             the netlink socket protocol (we're NETLINK_IP6_FW).
18  * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
19  */
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/init.h>
23 #include <linux/ipv6.h>
24 #include <linux/notifier.h>
25 #include <linux/netdevice.h>
26 #include <linux/netfilter.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/brlock.h>
30 #include <linux/sysctl.h>
31 #include <linux/proc_fs.h>
32 #include <net/sock.h>
33 #include <net/ipv6.h>
34 #include <net/ip6_route.h>
35 #include <linux/netfilter_ipv4/ip_queue.h>
36 #include <linux/netfilter_ipv4/ip_tables.h>
37 #include <linux/netfilter_ipv6/ip6_tables.h>
38
39 #define IPQ_QMAX_DEFAULT 1024
40 #define IPQ_PROC_FS_NAME "ip6_queue"
41 #define NET_IPQ_QMAX 2088
42 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
43
44 struct ipq_rt_info {
45         struct in6_addr daddr;
46         struct in6_addr saddr;
47 };
48
49 struct ipq_queue_entry {
50         struct list_head list;
51         struct nf_info *info;
52         struct sk_buff *skb;
53         struct ipq_rt_info rt_info;
54 };
55
56 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
57
58 static unsigned char copy_mode = IPQ_COPY_NONE;
59 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
60 static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
61 static int peer_pid;
62 static unsigned int copy_range;
63 static unsigned int queue_total;
64 static struct sock *ipqnl;
65 static LIST_HEAD(queue_list);
66 static DECLARE_MUTEX(ipqnl_sem);
67
68 static void
69 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
70 {
71         local_bh_disable();
72         nf_reinject(entry->skb, entry->info, verdict);
73         local_bh_enable();
74         kfree(entry);
75 }
76
77 static inline int
78 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
79 {
80        if (queue_total >= queue_maxlen) {
81                if (net_ratelimit()) 
82                        printk(KERN_WARNING "ip6_queue: full at %d entries, "
83                               "dropping packet(s).\n", queue_total);
84                return -ENOSPC;
85        }
86        list_add(&entry->list, &queue_list);
87        queue_total++;
88        return 0;
89 }
90
91 /*
92  * Find and return a queued entry matched by cmpfn, or return the last
93  * entry if cmpfn is NULL.
94  */
95 static inline struct ipq_queue_entry *
96 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
97 {
98         struct list_head *p;
99
100         list_for_each_prev(p, &queue_list) {
101                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
102                 
103                 if (!cmpfn || cmpfn(entry, data))
104                         return entry;
105         }
106         return NULL;
107 }
108
109 static inline void
110 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
111 {
112         list_del(&entry->list);
113         queue_total--;
114 }
115
116 static inline struct ipq_queue_entry *
117 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
118 {
119         struct ipq_queue_entry *entry;
120
121         entry = __ipq_find_entry(cmpfn, data);
122         if (entry == NULL)
123                 return NULL;
124
125         __ipq_dequeue_entry(entry);
126         return entry;
127 }
128
129
130 static inline void
131 __ipq_flush(int verdict)
132 {
133         struct ipq_queue_entry *entry;
134         
135         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
136                 ipq_issue_verdict(entry, verdict);
137 }
138
139 static inline int
140 __ipq_set_mode(unsigned char mode, unsigned int range)
141 {
142         int status = 0;
143         
144         switch(mode) {
145         case IPQ_COPY_NONE:
146         case IPQ_COPY_META:
147                 copy_mode = mode;
148                 copy_range = 0;
149                 break;
150                 
151         case IPQ_COPY_PACKET:
152                 copy_mode = mode;
153                 copy_range = range;
154                 if (copy_range > 0xFFFF)
155                         copy_range = 0xFFFF;
156                 break;
157                 
158         default:
159                 status = -EINVAL;
160
161         }
162         return status;
163 }
164
165 static inline void
166 __ipq_reset(void)
167 {
168         peer_pid = 0;
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                 memcpy(pmsg->payload, entry->skb->data, data_len);
265                 
266         nlh->nlmsg_len = skb->tail - old_tail;
267         return skb;
268
269 nlmsg_failure:
270         if (skb)
271                 kfree_skb(skb);
272         *errp = -EINVAL;
273         printk(KERN_ERR "ip6_queue: error creating packet message\n");
274         return NULL;
275 }
276
277 static int
278 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
279 {
280         int status = -EINVAL;
281         struct sk_buff *nskb;
282         struct ipq_queue_entry *entry;
283
284         if (copy_mode == IPQ_COPY_NONE)
285                 return -EAGAIN;
286
287         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
288         if (entry == NULL) {
289                 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
290                 return -ENOMEM;
291         }
292
293         entry->info = info;
294         entry->skb = skb;
295
296         if (entry->info->hook == NF_IP_LOCAL_OUT) {
297                 struct ipv6hdr *iph = skb->nh.ipv6h;
298
299                 entry->rt_info.daddr = iph->daddr;
300                 entry->rt_info.saddr = iph->saddr;
301         }
302
303         nskb = ipq_build_packet_message(entry, &status);
304         if (nskb == NULL)
305                 goto err_out_free;
306                 
307         write_lock_bh(&queue_lock);
308         
309         if (!peer_pid)
310                 goto err_out_free_nskb; 
311
312         /* netlink_unicast will either free the nskb or attach it to a socket */ 
313         status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
314         if (status < 0)
315                 goto err_out_unlock;
316         
317         status = __ipq_enqueue_entry(entry);
318         if (status < 0)
319                 goto err_out_unlock;
320
321         write_unlock_bh(&queue_lock);
322         return status;
323         
324 err_out_free_nskb:
325         kfree_skb(nskb); 
326         
327 err_out_unlock:
328         write_unlock_bh(&queue_lock);
329
330 err_out_free:
331         kfree(entry);
332         return status;
333 }
334
335 static int
336 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
337 {
338         int diff;
339         struct ipv6hdr *user_iph = (struct ipv6hdr *)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                 skb_trim(e->skb, v->data_len);
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 "ip6_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         memcpy(e->skb->data, v->payload, v->data_len);
369         e->skb->nfcache |= NFC_ALTERED;
370
371         /*
372          * Extra routing may needed on local out, as the QUEUE target never
373          * returns control to the table.
374          * Not a nice way to cmp, but works
375          */
376         if (e->info->hook == NF_IP_LOCAL_OUT) {
377                 struct ipv6hdr *iph = e->skb->nh.ipv6h;
378                 if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) ||
379                     ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr))
380                         return ip6_route_me_harder(e->skb);
381         }
382         return 0;
383 }
384
385 static inline int
386 id_cmp(struct ipq_queue_entry *e, unsigned long id)
387 {
388         return (id == (unsigned long )e);
389 }
390
391 static int
392 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
393 {
394         struct ipq_queue_entry *entry;
395
396         if (vmsg->value > NF_MAX_VERDICT)
397                 return -EINVAL;
398
399         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
400         if (entry == NULL)
401                 return -ENOENT;
402         else {
403                 int verdict = vmsg->value;
404                 
405                 if (vmsg->data_len && vmsg->data_len == len)
406                         if (ipq_mangle_ipv6(vmsg, entry) < 0)
407                                 verdict = NF_DROP;
408                 
409                 ipq_issue_verdict(entry, verdict);
410                 return 0;
411         }
412 }
413
414 static int
415 ipq_set_mode(unsigned char mode, unsigned int range)
416 {
417         int status;
418
419         write_lock_bh(&queue_lock);
420         status = __ipq_set_mode(mode, range);
421         write_unlock_bh(&queue_lock);
422         return status;
423 }
424
425 static int
426 ipq_receive_peer(struct ipq_peer_msg *pmsg,
427                  unsigned char type, unsigned int len)
428 {
429         int status = 0;
430
431         if (len < sizeof(*pmsg))
432                 return -EINVAL;
433
434         switch (type) {
435         case IPQM_MODE:
436                 status = ipq_set_mode(pmsg->msg.mode.value,
437                                       pmsg->msg.mode.range);
438                 break;
439                 
440         case IPQM_VERDICT:
441                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
442                         status = -EINVAL;
443                 else
444                         status = ipq_set_verdict(&pmsg->msg.verdict,
445                                                  len - sizeof(*pmsg));
446                         break;
447         default:
448                 status = -EINVAL;
449         }
450         return status;
451 }
452
453 static int
454 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
455 {
456         if (entry->info->indev)
457                 if (entry->info->indev->ifindex == ifindex)
458                         return 1;
459                         
460         if (entry->info->outdev)
461                 if (entry->info->outdev->ifindex == ifindex)
462                         return 1;
463
464         return 0;
465 }
466
467 static void
468 ipq_dev_drop(int ifindex)
469 {
470         struct ipq_queue_entry *entry;
471         
472         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
473                 ipq_issue_verdict(entry, NF_DROP);
474 }
475
476 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
477
478 static inline void
479 ipq_rcv_skb(struct sk_buff *skb)
480 {
481         int status, type, pid, flags, nlmsglen, skblen;
482         struct nlmsghdr *nlh;
483
484         skblen = skb->len;
485         if (skblen < sizeof(*nlh))
486                 return;
487
488         nlh = (struct nlmsghdr *)skb->data;
489         nlmsglen = nlh->nlmsg_len;
490         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
491                 return;
492
493         pid = nlh->nlmsg_pid;
494         flags = nlh->nlmsg_flags;
495         
496         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
497                 RCV_SKB_FAIL(-EINVAL);
498                 
499         if (flags & MSG_TRUNC)
500                 RCV_SKB_FAIL(-ECOMM);
501                 
502         type = nlh->nlmsg_type;
503         if (type < NLMSG_NOOP || type >= IPQM_MAX)
504                 RCV_SKB_FAIL(-EINVAL);
505                 
506         if (type <= IPQM_BASE)
507                 return;
508                 
509         if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
510                 RCV_SKB_FAIL(-EPERM);
511         
512         write_lock_bh(&queue_lock);
513         
514         if (peer_pid) {
515                 if (peer_pid != pid) {
516                         write_unlock_bh(&queue_lock);
517                         RCV_SKB_FAIL(-EBUSY);
518                 }
519         }
520         else
521                 peer_pid = pid;
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         do {
539                 struct sk_buff *skb;
540
541                 if (down_trylock(&ipqnl_sem))
542                         return;
543                         
544                 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
545                         ipq_rcv_skb(skb);
546                         kfree_skb(skb);
547                 }
548                 
549                 up(&ipqnl_sem);
550
551         } while (ipqnl && ipqnl->receive_queue.qlen);
552 }
553
554 static int
555 ipq_rcv_dev_event(struct notifier_block *this,
556                   unsigned long event, void *ptr)
557 {
558         struct net_device *dev = ptr;
559
560         /* Drop any packets associated with the downed device */
561         if (event == NETDEV_DOWN)
562                 ipq_dev_drop(dev->ifindex);
563         return NOTIFY_DONE;
564 }
565
566 static struct notifier_block ipq_dev_notifier = {
567         ipq_rcv_dev_event,
568         NULL,
569         0
570 };
571
572 static int
573 ipq_rcv_nl_event(struct notifier_block *this,
574                  unsigned long event, void *ptr)
575 {
576         struct netlink_notify *n = ptr;
577
578         if (event == NETLINK_URELEASE &&
579             n->protocol == NETLINK_IP6_FW && n->pid) {
580                 write_lock_bh(&queue_lock);
581                 if (n->pid == peer_pid)
582                         __ipq_reset();
583                 write_unlock_bh(&queue_lock);
584         }
585         return NOTIFY_DONE;
586 }
587
588 static struct notifier_block ipq_nl_notifier = {
589         ipq_rcv_nl_event,
590         NULL,
591         0
592 };
593
594 static struct ctl_table_header *ipq_sysctl_header;
595
596 static ctl_table ipq_table[] = {
597         { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &queue_maxlen,
598           sizeof(queue_maxlen), 0644,  NULL, proc_dointvec },
599         { 0 }
600 };
601
602 static ctl_table ipq_dir_table[] = {
603         {NET_IPV6, "ipv6", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0},
604         { 0 }
605 };
606
607 static ctl_table ipq_root_table[] = {
608         {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0},
609         { 0 }
610 };
611
612 static int
613 ipq_get_info(char *buffer, char **start, off_t offset, int length)
614 {
615         int len;
616
617         read_lock_bh(&queue_lock);
618         
619         len = sprintf(buffer,
620                       "Peer PID          : %d\n"
621                       "Copy mode         : %hu\n"
622                       "Copy range        : %u\n"
623                       "Queue length      : %u\n"
624                       "Queue max. length : %u\n",
625                       peer_pid,
626                       copy_mode,
627                       copy_range,
628                       queue_total,
629                       queue_maxlen);
630
631         read_unlock_bh(&queue_lock);
632         
633         *start = buffer + offset;
634         len -= offset;
635         if (len > length)
636                 len = length;
637         else if (len < 0)
638                 len = 0;
639         return len;
640 }
641
642 static int
643 init_or_cleanup(int init)
644 {
645         int status = -ENOMEM;
646         struct proc_dir_entry *proc;
647         
648         if (!init)
649                 goto cleanup;
650
651         netlink_register_notifier(&ipq_nl_notifier);
652         ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
653         if (ipqnl == NULL) {
654                 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
655                 goto cleanup_netlink_notifier;
656         }
657
658         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
659         if (proc)
660                 proc->owner = THIS_MODULE;
661         else {
662                 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
663                 goto cleanup_ipqnl;
664         }
665         
666         register_netdevice_notifier(&ipq_dev_notifier);
667         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
668         
669         status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
670         if (status < 0) {
671                 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
672                 goto cleanup_sysctl;
673         }
674         return status;
675
676 cleanup:
677         nf_unregister_queue_handler(PF_INET6);
678         br_write_lock_bh(BR_NETPROTO_LOCK);
679         br_write_unlock_bh(BR_NETPROTO_LOCK);
680         ipq_flush(NF_DROP);
681         
682 cleanup_sysctl:
683         unregister_sysctl_table(ipq_sysctl_header);
684         unregister_netdevice_notifier(&ipq_dev_notifier);
685         proc_net_remove(IPQ_PROC_FS_NAME);
686         
687 cleanup_ipqnl:
688         sock_release(ipqnl->socket);
689         down(&ipqnl_sem);
690         up(&ipqnl_sem);
691         
692 cleanup_netlink_notifier:
693         netlink_unregister_notifier(&ipq_nl_notifier);
694         return status;
695 }
696
697 static int __init init(void)
698 {
699         
700         return init_or_cleanup(1);
701 }
702
703 static void __exit fini(void)
704 {
705         init_or_cleanup(0);
706 }
707
708 MODULE_DESCRIPTION("IPv6 packet queue handler");
709 MODULE_LICENSE("GPL");
710
711 module_init(init);
712 module_exit(fini);