[NETFILTER]: Rename skb_ip_make_writable() to skb_make_writable()
[linux-2.6.git] / net / ipv4 / netfilter / ip_queue.c
blobae975ac59c6a4da507b4399d3b4c53c71d2c53e5
1 /*
2 * This is a module which is used for queueing IPv4 packets and
3 * communicating with userspace via netlink.
5 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
6 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
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.
12 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
13 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
14 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
15 * Zander).
16 * 2000-08-01: Added Nick Williams' MAC support.
17 * 2002-06-25: Code cleanup.
18 * 2005-01-10: Added /proc counter for dropped packets; fixed so
19 * packets aren't delivered to user space if they're going
20 * to be dropped.
21 * 2005-05-26: local_bh_{disable,enable} around nf_reinject (Harald Welte)
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
27 #include <linux/ip.h>
28 #include <linux/notifier.h>
29 #include <linux/netdevice.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4/ip_queue.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
33 #include <linux/netlink.h>
34 #include <linux/spinlock.h>
35 #include <linux/sysctl.h>
36 #include <linux/proc_fs.h>
37 #include <linux/security.h>
38 #include <net/sock.h>
39 #include <net/route.h>
41 #define IPQ_QMAX_DEFAULT 1024
42 #define IPQ_PROC_FS_NAME "ip_queue"
43 #define NET_IPQ_QMAX 2088
44 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
46 struct ipq_rt_info {
47 __u8 tos;
48 __u32 daddr;
49 __u32 saddr;
52 struct ipq_queue_entry {
53 struct list_head list;
54 struct nf_info *info;
55 struct sk_buff *skb;
56 struct ipq_rt_info rt_info;
59 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
61 static unsigned char copy_mode = IPQ_COPY_NONE;
62 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
63 static DEFINE_RWLOCK(queue_lock);
64 static int peer_pid;
65 static unsigned int copy_range;
66 static unsigned int queue_total;
67 static unsigned int queue_dropped = 0;
68 static unsigned int queue_user_dropped = 0;
69 static struct sock *ipqnl;
70 static LIST_HEAD(queue_list);
71 static DECLARE_MUTEX(ipqnl_sem);
73 static void
74 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
76 /* TCP input path (and probably other bits) assume to be called
77 * from softirq context, not from syscall, like ipq_issue_verdict is
78 * called. TCP input path deadlocks with locks taken from timer
79 * softirq, e.g. We therefore emulate this by local_bh_disable() */
81 local_bh_disable();
82 nf_reinject(entry->skb, entry->info, verdict);
83 local_bh_enable();
85 kfree(entry);
88 static inline void
89 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
91 list_add(&entry->list, &queue_list);
92 queue_total++;
96 * Find and return a queued entry matched by cmpfn, or return the last
97 * entry if cmpfn is NULL.
99 static inline struct ipq_queue_entry *
100 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
102 struct list_head *p;
104 list_for_each_prev(p, &queue_list) {
105 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
107 if (!cmpfn || cmpfn(entry, data))
108 return entry;
110 return NULL;
113 static inline void
114 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
116 list_del(&entry->list);
117 queue_total--;
120 static inline struct ipq_queue_entry *
121 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
123 struct ipq_queue_entry *entry;
125 entry = __ipq_find_entry(cmpfn, data);
126 if (entry == NULL)
127 return NULL;
129 __ipq_dequeue_entry(entry);
130 return entry;
134 static inline void
135 __ipq_flush(int verdict)
137 struct ipq_queue_entry *entry;
139 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
140 ipq_issue_verdict(entry, verdict);
143 static inline int
144 __ipq_set_mode(unsigned char mode, unsigned int range)
146 int status = 0;
148 switch(mode) {
149 case IPQ_COPY_NONE:
150 case IPQ_COPY_META:
151 copy_mode = mode;
152 copy_range = 0;
153 break;
155 case IPQ_COPY_PACKET:
156 copy_mode = mode;
157 copy_range = range;
158 if (copy_range > 0xFFFF)
159 copy_range = 0xFFFF;
160 break;
162 default:
163 status = -EINVAL;
166 return status;
169 static inline void
170 __ipq_reset(void)
172 peer_pid = 0;
173 net_disable_timestamp();
174 __ipq_set_mode(IPQ_COPY_NONE, 0);
175 __ipq_flush(NF_DROP);
178 static struct ipq_queue_entry *
179 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
181 struct ipq_queue_entry *entry;
183 write_lock_bh(&queue_lock);
184 entry = __ipq_find_dequeue_entry(cmpfn, data);
185 write_unlock_bh(&queue_lock);
186 return entry;
189 static void
190 ipq_flush(int verdict)
192 write_lock_bh(&queue_lock);
193 __ipq_flush(verdict);
194 write_unlock_bh(&queue_lock);
197 static struct sk_buff *
198 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
200 unsigned char *old_tail;
201 size_t size = 0;
202 size_t data_len = 0;
203 struct sk_buff *skb;
204 struct ipq_packet_msg *pmsg;
205 struct nlmsghdr *nlh;
207 read_lock_bh(&queue_lock);
209 switch (copy_mode) {
210 case IPQ_COPY_META:
211 case IPQ_COPY_NONE:
212 size = NLMSG_SPACE(sizeof(*pmsg));
213 data_len = 0;
214 break;
216 case IPQ_COPY_PACKET:
217 if (entry->skb->ip_summed == CHECKSUM_HW &&
218 (*errp = skb_checksum_help(entry->skb,
219 entry->info->outdev == NULL))) {
220 read_unlock_bh(&queue_lock);
221 return NULL;
223 if (copy_range == 0 || copy_range > entry->skb->len)
224 data_len = entry->skb->len;
225 else
226 data_len = copy_range;
228 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
229 break;
231 default:
232 *errp = -EINVAL;
233 read_unlock_bh(&queue_lock);
234 return NULL;
237 read_unlock_bh(&queue_lock);
239 skb = alloc_skb(size, GFP_ATOMIC);
240 if (!skb)
241 goto nlmsg_failure;
243 old_tail= skb->tail;
244 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
245 pmsg = NLMSG_DATA(nlh);
246 memset(pmsg, 0, sizeof(*pmsg));
248 pmsg->packet_id = (unsigned long )entry;
249 pmsg->data_len = data_len;
250 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
251 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
252 pmsg->mark = entry->skb->nfmark;
253 pmsg->hook = entry->info->hook;
254 pmsg->hw_protocol = entry->skb->protocol;
256 if (entry->info->indev)
257 strcpy(pmsg->indev_name, entry->info->indev->name);
258 else
259 pmsg->indev_name[0] = '\0';
261 if (entry->info->outdev)
262 strcpy(pmsg->outdev_name, entry->info->outdev->name);
263 else
264 pmsg->outdev_name[0] = '\0';
266 if (entry->info->indev && entry->skb->dev) {
267 pmsg->hw_type = entry->skb->dev->type;
268 if (entry->skb->dev->hard_header_parse)
269 pmsg->hw_addrlen =
270 entry->skb->dev->hard_header_parse(entry->skb,
271 pmsg->hw_addr);
274 if (data_len)
275 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
276 BUG();
278 nlh->nlmsg_len = skb->tail - old_tail;
279 return skb;
281 nlmsg_failure:
282 if (skb)
283 kfree_skb(skb);
284 *errp = -EINVAL;
285 printk(KERN_ERR "ip_queue: error creating packet message\n");
286 return NULL;
289 static int
290 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
292 int status = -EINVAL;
293 struct sk_buff *nskb;
294 struct ipq_queue_entry *entry;
296 if (copy_mode == IPQ_COPY_NONE)
297 return -EAGAIN;
299 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
300 if (entry == NULL) {
301 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
302 return -ENOMEM;
305 entry->info = info;
306 entry->skb = skb;
308 if (entry->info->hook == NF_IP_LOCAL_OUT) {
309 struct iphdr *iph = skb->nh.iph;
311 entry->rt_info.tos = iph->tos;
312 entry->rt_info.daddr = iph->daddr;
313 entry->rt_info.saddr = iph->saddr;
316 nskb = ipq_build_packet_message(entry, &status);
317 if (nskb == NULL)
318 goto err_out_free;
320 write_lock_bh(&queue_lock);
322 if (!peer_pid)
323 goto err_out_free_nskb;
325 if (queue_total >= queue_maxlen) {
326 queue_dropped++;
327 status = -ENOSPC;
328 if (net_ratelimit())
329 printk (KERN_WARNING "ip_queue: full at %d entries, "
330 "dropping packets(s). Dropped: %d\n", queue_total,
331 queue_dropped);
332 goto err_out_free_nskb;
335 /* netlink_unicast will either free the nskb or attach it to a socket */
336 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
337 if (status < 0) {
338 queue_user_dropped++;
339 goto err_out_unlock;
342 __ipq_enqueue_entry(entry);
344 write_unlock_bh(&queue_lock);
345 return status;
347 err_out_free_nskb:
348 kfree_skb(nskb);
350 err_out_unlock:
351 write_unlock_bh(&queue_lock);
353 err_out_free:
354 kfree(entry);
355 return status;
358 static int
359 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
361 int diff;
362 struct iphdr *user_iph = (struct iphdr *)v->payload;
364 if (v->data_len < sizeof(*user_iph))
365 return 0;
366 diff = v->data_len - e->skb->len;
367 if (diff < 0)
368 skb_trim(e->skb, v->data_len);
369 else if (diff > 0) {
370 if (v->data_len > 0xFFFF)
371 return -EINVAL;
372 if (diff > skb_tailroom(e->skb)) {
373 struct sk_buff *newskb;
375 newskb = skb_copy_expand(e->skb,
376 skb_headroom(e->skb),
377 diff,
378 GFP_ATOMIC);
379 if (newskb == NULL) {
380 printk(KERN_WARNING "ip_queue: OOM "
381 "in mangle, dropping packet\n");
382 return -ENOMEM;
384 if (e->skb->sk)
385 skb_set_owner_w(newskb, e->skb->sk);
386 kfree_skb(e->skb);
387 e->skb = newskb;
389 skb_put(e->skb, diff);
391 if (!skb_make_writable(&e->skb, v->data_len))
392 return -ENOMEM;
393 memcpy(e->skb->data, v->payload, v->data_len);
394 e->skb->ip_summed = CHECKSUM_NONE;
397 * Extra routing may needed on local out, as the QUEUE target never
398 * returns control to the table.
400 if (e->info->hook == NF_IP_LOCAL_OUT) {
401 struct iphdr *iph = e->skb->nh.iph;
403 if (!(iph->tos == e->rt_info.tos
404 && iph->daddr == e->rt_info.daddr
405 && iph->saddr == e->rt_info.saddr))
406 return ip_route_me_harder(&e->skb);
408 return 0;
411 static inline int
412 id_cmp(struct ipq_queue_entry *e, unsigned long id)
414 return (id == (unsigned long )e);
417 static int
418 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
420 struct ipq_queue_entry *entry;
422 if (vmsg->value > NF_MAX_VERDICT)
423 return -EINVAL;
425 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
426 if (entry == NULL)
427 return -ENOENT;
428 else {
429 int verdict = vmsg->value;
431 if (vmsg->data_len && vmsg->data_len == len)
432 if (ipq_mangle_ipv4(vmsg, entry) < 0)
433 verdict = NF_DROP;
435 ipq_issue_verdict(entry, verdict);
436 return 0;
440 static int
441 ipq_set_mode(unsigned char mode, unsigned int range)
443 int status;
445 write_lock_bh(&queue_lock);
446 status = __ipq_set_mode(mode, range);
447 write_unlock_bh(&queue_lock);
448 return status;
451 static int
452 ipq_receive_peer(struct ipq_peer_msg *pmsg,
453 unsigned char type, unsigned int len)
455 int status = 0;
457 if (len < sizeof(*pmsg))
458 return -EINVAL;
460 switch (type) {
461 case IPQM_MODE:
462 status = ipq_set_mode(pmsg->msg.mode.value,
463 pmsg->msg.mode.range);
464 break;
466 case IPQM_VERDICT:
467 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
468 status = -EINVAL;
469 else
470 status = ipq_set_verdict(&pmsg->msg.verdict,
471 len - sizeof(*pmsg));
472 break;
473 default:
474 status = -EINVAL;
476 return status;
479 static int
480 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
482 if (entry->info->indev)
483 if (entry->info->indev->ifindex == ifindex)
484 return 1;
486 if (entry->info->outdev)
487 if (entry->info->outdev->ifindex == ifindex)
488 return 1;
490 return 0;
493 static void
494 ipq_dev_drop(int ifindex)
496 struct ipq_queue_entry *entry;
498 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
499 ipq_issue_verdict(entry, NF_DROP);
502 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
504 static inline void
505 ipq_rcv_skb(struct sk_buff *skb)
507 int status, type, pid, flags, nlmsglen, skblen;
508 struct nlmsghdr *nlh;
510 skblen = skb->len;
511 if (skblen < sizeof(*nlh))
512 return;
514 nlh = (struct nlmsghdr *)skb->data;
515 nlmsglen = nlh->nlmsg_len;
516 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
517 return;
519 pid = nlh->nlmsg_pid;
520 flags = nlh->nlmsg_flags;
522 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
523 RCV_SKB_FAIL(-EINVAL);
525 if (flags & MSG_TRUNC)
526 RCV_SKB_FAIL(-ECOMM);
528 type = nlh->nlmsg_type;
529 if (type < NLMSG_NOOP || type >= IPQM_MAX)
530 RCV_SKB_FAIL(-EINVAL);
532 if (type <= IPQM_BASE)
533 return;
535 if (security_netlink_recv(skb))
536 RCV_SKB_FAIL(-EPERM);
538 write_lock_bh(&queue_lock);
540 if (peer_pid) {
541 if (peer_pid != pid) {
542 write_unlock_bh(&queue_lock);
543 RCV_SKB_FAIL(-EBUSY);
545 } else {
546 net_enable_timestamp();
547 peer_pid = pid;
550 write_unlock_bh(&queue_lock);
552 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
553 skblen - NLMSG_LENGTH(0));
554 if (status < 0)
555 RCV_SKB_FAIL(status);
557 if (flags & NLM_F_ACK)
558 netlink_ack(skb, nlh, 0);
559 return;
562 static void
563 ipq_rcv_sk(struct sock *sk, int len)
565 struct sk_buff *skb;
566 unsigned int qlen;
568 down(&ipqnl_sem);
570 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
571 skb = skb_dequeue(&sk->sk_receive_queue);
572 ipq_rcv_skb(skb);
573 kfree_skb(skb);
576 up(&ipqnl_sem);
579 static int
580 ipq_rcv_dev_event(struct notifier_block *this,
581 unsigned long event, void *ptr)
583 struct net_device *dev = ptr;
585 /* Drop any packets associated with the downed device */
586 if (event == NETDEV_DOWN)
587 ipq_dev_drop(dev->ifindex);
588 return NOTIFY_DONE;
591 static struct notifier_block ipq_dev_notifier = {
592 .notifier_call = ipq_rcv_dev_event,
595 static int
596 ipq_rcv_nl_event(struct notifier_block *this,
597 unsigned long event, void *ptr)
599 struct netlink_notify *n = ptr;
601 if (event == NETLINK_URELEASE &&
602 n->protocol == NETLINK_FIREWALL && n->pid) {
603 write_lock_bh(&queue_lock);
604 if (n->pid == peer_pid)
605 __ipq_reset();
606 write_unlock_bh(&queue_lock);
608 return NOTIFY_DONE;
611 static struct notifier_block ipq_nl_notifier = {
612 .notifier_call = ipq_rcv_nl_event,
615 static struct ctl_table_header *ipq_sysctl_header;
617 static ctl_table ipq_table[] = {
619 .ctl_name = NET_IPQ_QMAX,
620 .procname = NET_IPQ_QMAX_NAME,
621 .data = &queue_maxlen,
622 .maxlen = sizeof(queue_maxlen),
623 .mode = 0644,
624 .proc_handler = proc_dointvec
626 { .ctl_name = 0 }
629 static ctl_table ipq_dir_table[] = {
631 .ctl_name = NET_IPV4,
632 .procname = "ipv4",
633 .mode = 0555,
634 .child = ipq_table
636 { .ctl_name = 0 }
639 static ctl_table ipq_root_table[] = {
641 .ctl_name = CTL_NET,
642 .procname = "net",
643 .mode = 0555,
644 .child = ipq_dir_table
646 { .ctl_name = 0 }
649 #ifdef CONFIG_PROC_FS
650 static int
651 ipq_get_info(char *buffer, char **start, off_t offset, int length)
653 int len;
655 read_lock_bh(&queue_lock);
657 len = sprintf(buffer,
658 "Peer PID : %d\n"
659 "Copy mode : %hu\n"
660 "Copy range : %u\n"
661 "Queue length : %u\n"
662 "Queue max. length : %u\n"
663 "Queue dropped : %u\n"
664 "Netlink dropped : %u\n",
665 peer_pid,
666 copy_mode,
667 copy_range,
668 queue_total,
669 queue_maxlen,
670 queue_dropped,
671 queue_user_dropped);
673 read_unlock_bh(&queue_lock);
675 *start = buffer + offset;
676 len -= offset;
677 if (len > length)
678 len = length;
679 else if (len < 0)
680 len = 0;
681 return len;
683 #endif /* CONFIG_PROC_FS */
685 static int
686 init_or_cleanup(int init)
688 int status = -ENOMEM;
689 struct proc_dir_entry *proc;
691 if (!init)
692 goto cleanup;
694 netlink_register_notifier(&ipq_nl_notifier);
695 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
696 if (ipqnl == NULL) {
697 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
698 goto cleanup_netlink_notifier;
701 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
702 if (proc)
703 proc->owner = THIS_MODULE;
704 else {
705 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
706 goto cleanup_ipqnl;
709 register_netdevice_notifier(&ipq_dev_notifier);
710 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
712 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
713 if (status < 0) {
714 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
715 goto cleanup_sysctl;
717 return status;
719 cleanup:
720 nf_unregister_queue_handler(PF_INET);
721 synchronize_net();
722 ipq_flush(NF_DROP);
724 cleanup_sysctl:
725 unregister_sysctl_table(ipq_sysctl_header);
726 unregister_netdevice_notifier(&ipq_dev_notifier);
727 proc_net_remove(IPQ_PROC_FS_NAME);
729 cleanup_ipqnl:
730 sock_release(ipqnl->sk_socket);
731 down(&ipqnl_sem);
732 up(&ipqnl_sem);
734 cleanup_netlink_notifier:
735 netlink_unregister_notifier(&ipq_nl_notifier);
736 return status;
739 static int __init init(void)
742 return init_or_cleanup(1);
745 static void __exit fini(void)
747 init_or_cleanup(0);
750 MODULE_DESCRIPTION("IPv4 packet queue handler");
751 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
752 MODULE_LICENSE("GPL");
754 module_init(init);
755 module_exit(fini);