[NETFILTER]: nf_queue: move list_head/skb/id to struct nf_info
[linux-2.6/mini2440.git] / net / ipv4 / netfilter / ip_queue.c
blobf1affd2344a9358b09b6e5c10a6dac15aff39d6d
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 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/ip.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/security.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/route.h>
31 #include <net/netfilter/nf_queue.h>
33 #define IPQ_QMAX_DEFAULT 1024
34 #define IPQ_PROC_FS_NAME "ip_queue"
35 #define NET_IPQ_QMAX 2088
36 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
38 typedef int (*ipq_cmpfn)(struct nf_queue_entry *, unsigned long);
40 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
41 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
42 static DEFINE_RWLOCK(queue_lock);
43 static int peer_pid __read_mostly;
44 static unsigned int copy_range __read_mostly;
45 static unsigned int queue_total;
46 static unsigned int queue_dropped = 0;
47 static unsigned int queue_user_dropped = 0;
48 static struct sock *ipqnl __read_mostly;
49 static LIST_HEAD(queue_list);
50 static DEFINE_MUTEX(ipqnl_mutex);
52 static void
53 ipq_issue_verdict(struct nf_queue_entry *entry, int verdict)
55 /* TCP input path (and probably other bits) assume to be called
56 * from softirq context, not from syscall, like ipq_issue_verdict is
57 * called. TCP input path deadlocks with locks taken from timer
58 * softirq, e.g. We therefore emulate this by local_bh_disable() */
60 local_bh_disable();
61 nf_reinject(entry, verdict);
62 local_bh_enable();
65 static inline void
66 __ipq_enqueue_entry(struct nf_queue_entry *entry)
68 list_add_tail(&entry->list, &queue_list);
69 queue_total++;
72 static inline int
73 __ipq_set_mode(unsigned char mode, unsigned int range)
75 int status = 0;
77 switch(mode) {
78 case IPQ_COPY_NONE:
79 case IPQ_COPY_META:
80 copy_mode = mode;
81 copy_range = 0;
82 break;
84 case IPQ_COPY_PACKET:
85 copy_mode = mode;
86 copy_range = range;
87 if (copy_range > 0xFFFF)
88 copy_range = 0xFFFF;
89 break;
91 default:
92 status = -EINVAL;
95 return status;
98 static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data);
100 static inline void
101 __ipq_reset(void)
103 peer_pid = 0;
104 net_disable_timestamp();
105 __ipq_set_mode(IPQ_COPY_NONE, 0);
106 __ipq_flush(NULL, 0);
109 static struct nf_queue_entry *
110 ipq_find_dequeue_entry(unsigned long id)
112 struct nf_queue_entry *entry = NULL, *i;
114 write_lock_bh(&queue_lock);
116 list_for_each_entry(i, &queue_list, list) {
117 if ((unsigned long)i == id) {
118 entry = i;
119 break;
123 if (entry) {
124 list_del(&entry->list);
125 queue_total--;
128 write_unlock_bh(&queue_lock);
129 return entry;
132 static void
133 __ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
135 struct nf_queue_entry *entry, *next;
137 list_for_each_entry_safe(entry, next, &queue_list, list) {
138 if (!cmpfn || cmpfn(entry, data)) {
139 list_del(&entry->list);
140 queue_total--;
141 ipq_issue_verdict(entry, NF_DROP);
146 static void
147 ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
149 write_lock_bh(&queue_lock);
150 __ipq_flush(cmpfn, data);
151 write_unlock_bh(&queue_lock);
154 static struct sk_buff *
155 ipq_build_packet_message(struct nf_queue_entry *entry, int *errp)
157 sk_buff_data_t old_tail;
158 size_t size = 0;
159 size_t data_len = 0;
160 struct sk_buff *skb;
161 struct ipq_packet_msg *pmsg;
162 struct nlmsghdr *nlh;
163 struct timeval tv;
165 read_lock_bh(&queue_lock);
167 switch (copy_mode) {
168 case IPQ_COPY_META:
169 case IPQ_COPY_NONE:
170 size = NLMSG_SPACE(sizeof(*pmsg));
171 data_len = 0;
172 break;
174 case IPQ_COPY_PACKET:
175 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
176 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
177 (*errp = skb_checksum_help(entry->skb))) {
178 read_unlock_bh(&queue_lock);
179 return NULL;
181 if (copy_range == 0 || copy_range > entry->skb->len)
182 data_len = entry->skb->len;
183 else
184 data_len = copy_range;
186 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
187 break;
189 default:
190 *errp = -EINVAL;
191 read_unlock_bh(&queue_lock);
192 return NULL;
195 read_unlock_bh(&queue_lock);
197 skb = alloc_skb(size, GFP_ATOMIC);
198 if (!skb)
199 goto nlmsg_failure;
201 old_tail = skb->tail;
202 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
203 pmsg = NLMSG_DATA(nlh);
204 memset(pmsg, 0, sizeof(*pmsg));
206 pmsg->packet_id = (unsigned long )entry;
207 pmsg->data_len = data_len;
208 tv = ktime_to_timeval(entry->skb->tstamp);
209 pmsg->timestamp_sec = tv.tv_sec;
210 pmsg->timestamp_usec = tv.tv_usec;
211 pmsg->mark = entry->skb->mark;
212 pmsg->hook = entry->hook;
213 pmsg->hw_protocol = entry->skb->protocol;
215 if (entry->indev)
216 strcpy(pmsg->indev_name, entry->indev->name);
217 else
218 pmsg->indev_name[0] = '\0';
220 if (entry->outdev)
221 strcpy(pmsg->outdev_name, entry->outdev->name);
222 else
223 pmsg->outdev_name[0] = '\0';
225 if (entry->indev && entry->skb->dev) {
226 pmsg->hw_type = entry->skb->dev->type;
227 pmsg->hw_addrlen = dev_parse_header(entry->skb,
228 pmsg->hw_addr);
231 if (data_len)
232 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
233 BUG();
235 nlh->nlmsg_len = skb->tail - old_tail;
236 return skb;
238 nlmsg_failure:
239 if (skb)
240 kfree_skb(skb);
241 *errp = -EINVAL;
242 printk(KERN_ERR "ip_queue: error creating packet message\n");
243 return NULL;
246 static int
247 ipq_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
249 int status = -EINVAL;
250 struct sk_buff *nskb;
252 if (copy_mode == IPQ_COPY_NONE)
253 return -EAGAIN;
255 nskb = ipq_build_packet_message(entry, &status);
256 if (nskb == NULL)
257 return status;
259 write_lock_bh(&queue_lock);
261 if (!peer_pid)
262 goto err_out_free_nskb;
264 if (queue_total >= queue_maxlen) {
265 queue_dropped++;
266 status = -ENOSPC;
267 if (net_ratelimit())
268 printk (KERN_WARNING "ip_queue: full at %d entries, "
269 "dropping packets(s). Dropped: %d\n", queue_total,
270 queue_dropped);
271 goto err_out_free_nskb;
274 /* netlink_unicast will either free the nskb or attach it to a socket */
275 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
276 if (status < 0) {
277 queue_user_dropped++;
278 goto err_out_unlock;
281 __ipq_enqueue_entry(entry);
283 write_unlock_bh(&queue_lock);
284 return status;
286 err_out_free_nskb:
287 kfree_skb(nskb);
289 err_out_unlock:
290 write_unlock_bh(&queue_lock);
291 return status;
294 static int
295 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct nf_queue_entry *e)
297 int diff;
298 int err;
299 struct iphdr *user_iph = (struct iphdr *)v->payload;
301 if (v->data_len < sizeof(*user_iph))
302 return 0;
303 diff = v->data_len - e->skb->len;
304 if (diff < 0) {
305 if (pskb_trim(e->skb, v->data_len))
306 return -ENOMEM;
307 } else if (diff > 0) {
308 if (v->data_len > 0xFFFF)
309 return -EINVAL;
310 if (diff > skb_tailroom(e->skb)) {
311 err = pskb_expand_head(e->skb, 0,
312 diff - skb_tailroom(e->skb),
313 GFP_ATOMIC);
314 if (err) {
315 printk(KERN_WARNING "ip_queue: error "
316 "in mangle, dropping packet: %d\n", -err);
317 return err;
320 skb_put(e->skb, diff);
322 if (!skb_make_writable(e->skb, v->data_len))
323 return -ENOMEM;
324 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
325 e->skb->ip_summed = CHECKSUM_NONE;
327 return 0;
330 static int
331 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
333 struct nf_queue_entry *entry;
335 if (vmsg->value > NF_MAX_VERDICT)
336 return -EINVAL;
338 entry = ipq_find_dequeue_entry(vmsg->id);
339 if (entry == NULL)
340 return -ENOENT;
341 else {
342 int verdict = vmsg->value;
344 if (vmsg->data_len && vmsg->data_len == len)
345 if (ipq_mangle_ipv4(vmsg, entry) < 0)
346 verdict = NF_DROP;
348 ipq_issue_verdict(entry, verdict);
349 return 0;
353 static int
354 ipq_set_mode(unsigned char mode, unsigned int range)
356 int status;
358 write_lock_bh(&queue_lock);
359 status = __ipq_set_mode(mode, range);
360 write_unlock_bh(&queue_lock);
361 return status;
364 static int
365 ipq_receive_peer(struct ipq_peer_msg *pmsg,
366 unsigned char type, unsigned int len)
368 int status = 0;
370 if (len < sizeof(*pmsg))
371 return -EINVAL;
373 switch (type) {
374 case IPQM_MODE:
375 status = ipq_set_mode(pmsg->msg.mode.value,
376 pmsg->msg.mode.range);
377 break;
379 case IPQM_VERDICT:
380 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
381 status = -EINVAL;
382 else
383 status = ipq_set_verdict(&pmsg->msg.verdict,
384 len - sizeof(*pmsg));
385 break;
386 default:
387 status = -EINVAL;
389 return status;
392 static int
393 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
395 if (entry->indev)
396 if (entry->indev->ifindex == ifindex)
397 return 1;
398 if (entry->outdev)
399 if (entry->outdev->ifindex == ifindex)
400 return 1;
401 #ifdef CONFIG_BRIDGE_NETFILTER
402 if (entry->skb->nf_bridge) {
403 if (entry->skb->nf_bridge->physindev &&
404 entry->skb->nf_bridge->physindev->ifindex == ifindex)
405 return 1;
406 if (entry->skb->nf_bridge->physoutdev &&
407 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
408 return 1;
410 #endif
411 return 0;
414 static void
415 ipq_dev_drop(int ifindex)
417 ipq_flush(dev_cmp, ifindex);
420 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
422 static inline void
423 __ipq_rcv_skb(struct sk_buff *skb)
425 int status, type, pid, flags, nlmsglen, skblen;
426 struct nlmsghdr *nlh;
428 skblen = skb->len;
429 if (skblen < sizeof(*nlh))
430 return;
432 nlh = nlmsg_hdr(skb);
433 nlmsglen = nlh->nlmsg_len;
434 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
435 return;
437 pid = nlh->nlmsg_pid;
438 flags = nlh->nlmsg_flags;
440 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
441 RCV_SKB_FAIL(-EINVAL);
443 if (flags & MSG_TRUNC)
444 RCV_SKB_FAIL(-ECOMM);
446 type = nlh->nlmsg_type;
447 if (type < NLMSG_NOOP || type >= IPQM_MAX)
448 RCV_SKB_FAIL(-EINVAL);
450 if (type <= IPQM_BASE)
451 return;
453 if (security_netlink_recv(skb, CAP_NET_ADMIN))
454 RCV_SKB_FAIL(-EPERM);
456 write_lock_bh(&queue_lock);
458 if (peer_pid) {
459 if (peer_pid != pid) {
460 write_unlock_bh(&queue_lock);
461 RCV_SKB_FAIL(-EBUSY);
463 } else {
464 net_enable_timestamp();
465 peer_pid = pid;
468 write_unlock_bh(&queue_lock);
470 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
471 nlmsglen - NLMSG_LENGTH(0));
472 if (status < 0)
473 RCV_SKB_FAIL(status);
475 if (flags & NLM_F_ACK)
476 netlink_ack(skb, nlh, 0);
477 return;
480 static void
481 ipq_rcv_skb(struct sk_buff *skb)
483 mutex_lock(&ipqnl_mutex);
484 __ipq_rcv_skb(skb);
485 mutex_unlock(&ipqnl_mutex);
488 static int
489 ipq_rcv_dev_event(struct notifier_block *this,
490 unsigned long event, void *ptr)
492 struct net_device *dev = ptr;
494 if (dev->nd_net != &init_net)
495 return NOTIFY_DONE;
497 /* Drop any packets associated with the downed device */
498 if (event == NETDEV_DOWN)
499 ipq_dev_drop(dev->ifindex);
500 return NOTIFY_DONE;
503 static struct notifier_block ipq_dev_notifier = {
504 .notifier_call = ipq_rcv_dev_event,
507 static int
508 ipq_rcv_nl_event(struct notifier_block *this,
509 unsigned long event, void *ptr)
511 struct netlink_notify *n = ptr;
513 if (event == NETLINK_URELEASE &&
514 n->protocol == NETLINK_FIREWALL && n->pid) {
515 write_lock_bh(&queue_lock);
516 if ((n->net == &init_net) && (n->pid == peer_pid))
517 __ipq_reset();
518 write_unlock_bh(&queue_lock);
520 return NOTIFY_DONE;
523 static struct notifier_block ipq_nl_notifier = {
524 .notifier_call = ipq_rcv_nl_event,
527 static struct ctl_table_header *ipq_sysctl_header;
529 static ctl_table ipq_table[] = {
531 .ctl_name = NET_IPQ_QMAX,
532 .procname = NET_IPQ_QMAX_NAME,
533 .data = &queue_maxlen,
534 .maxlen = sizeof(queue_maxlen),
535 .mode = 0644,
536 .proc_handler = proc_dointvec
538 { .ctl_name = 0 }
541 static ctl_table ipq_dir_table[] = {
543 .ctl_name = NET_IPV4,
544 .procname = "ipv4",
545 .mode = 0555,
546 .child = ipq_table
548 { .ctl_name = 0 }
551 static ctl_table ipq_root_table[] = {
553 .ctl_name = CTL_NET,
554 .procname = "net",
555 .mode = 0555,
556 .child = ipq_dir_table
558 { .ctl_name = 0 }
561 static int ip_queue_show(struct seq_file *m, void *v)
563 read_lock_bh(&queue_lock);
565 seq_printf(m,
566 "Peer PID : %d\n"
567 "Copy mode : %hu\n"
568 "Copy range : %u\n"
569 "Queue length : %u\n"
570 "Queue max. length : %u\n"
571 "Queue dropped : %u\n"
572 "Netlink dropped : %u\n",
573 peer_pid,
574 copy_mode,
575 copy_range,
576 queue_total,
577 queue_maxlen,
578 queue_dropped,
579 queue_user_dropped);
581 read_unlock_bh(&queue_lock);
582 return 0;
585 static int ip_queue_open(struct inode *inode, struct file *file)
587 return single_open(file, ip_queue_show, NULL);
590 static const struct file_operations ip_queue_proc_fops = {
591 .open = ip_queue_open,
592 .read = seq_read,
593 .llseek = seq_lseek,
594 .release = single_release,
595 .owner = THIS_MODULE,
598 static const struct nf_queue_handler nfqh = {
599 .name = "ip_queue",
600 .outfn = &ipq_enqueue_packet,
603 static int __init ip_queue_init(void)
605 int status = -ENOMEM;
606 struct proc_dir_entry *proc;
608 netlink_register_notifier(&ipq_nl_notifier);
609 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
610 ipq_rcv_skb, NULL, THIS_MODULE);
611 if (ipqnl == NULL) {
612 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
613 goto cleanup_netlink_notifier;
616 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
617 if (proc) {
618 proc->owner = THIS_MODULE;
619 proc->proc_fops = &ip_queue_proc_fops;
620 } else {
621 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
622 goto cleanup_ipqnl;
625 register_netdevice_notifier(&ipq_dev_notifier);
626 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
628 status = nf_register_queue_handler(PF_INET, &nfqh);
629 if (status < 0) {
630 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
631 goto cleanup_sysctl;
633 return status;
635 cleanup_sysctl:
636 unregister_sysctl_table(ipq_sysctl_header);
637 unregister_netdevice_notifier(&ipq_dev_notifier);
638 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
639 cleanup_ipqnl:
640 sock_release(ipqnl->sk_socket);
641 mutex_lock(&ipqnl_mutex);
642 mutex_unlock(&ipqnl_mutex);
644 cleanup_netlink_notifier:
645 netlink_unregister_notifier(&ipq_nl_notifier);
646 return status;
649 static void __exit ip_queue_fini(void)
651 nf_unregister_queue_handlers(&nfqh);
652 synchronize_net();
653 ipq_flush(NULL, 0);
655 unregister_sysctl_table(ipq_sysctl_header);
656 unregister_netdevice_notifier(&ipq_dev_notifier);
657 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
659 sock_release(ipqnl->sk_socket);
660 mutex_lock(&ipqnl_mutex);
661 mutex_unlock(&ipqnl_mutex);
663 netlink_unregister_notifier(&ipq_nl_notifier);
666 MODULE_DESCRIPTION("IPv4 packet queue handler");
667 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
668 MODULE_LICENSE("GPL");
670 module_init(ip_queue_init);
671 module_exit(ip_queue_fini);