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