2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
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.
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv6.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/nfnetlink_queue.h>
27 #include <linux/list.h>
30 #include <asm/atomic.h>
32 #define NFQNL_QMAX_DEFAULT 1024
35 #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
36 __FILE__, __LINE__, __FUNCTION__, \
39 #define QDEBUG(x, ...)
42 struct nfqnl_queue_entry {
43 struct list_head list;
49 struct nfqnl_instance {
50 struct hlist_node hlist; /* global list of queues */
53 unsigned int queue_maxlen;
54 unsigned int copy_range;
55 unsigned int queue_total;
56 unsigned int queue_dropped;
57 unsigned int queue_user_dropped;
59 atomic_t id_sequence; /* 'sequence' of pkt ids */
61 u_int16_t queue_num; /* number of this queue */
66 struct list_head queue_list; /* packets in queue */
69 typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
71 static DEFINE_RWLOCK(instances_lock);
73 u_int64_t htonll(u_int64_t in)
78 for (i = 0; i < sizeof(u_int64_t); i++)
79 ((u_int8_t *)&out)[sizeof(u_int64_t)-1] = ((u_int8_t *)&in)[i];
84 #define INSTANCE_BUCKETS 16
85 static struct hlist_head instance_table[INSTANCE_BUCKETS];
87 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
89 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
92 static struct nfqnl_instance *
93 __instance_lookup(u_int16_t queue_num)
95 struct hlist_head *head;
96 struct hlist_node *pos;
97 struct nfqnl_instance *inst;
99 head = &instance_table[instance_hashfn(queue_num)];
100 hlist_for_each_entry(inst, pos, head, hlist) {
101 if (inst->queue_num == queue_num)
107 static struct nfqnl_instance *
108 instance_lookup(u_int16_t queue_num)
110 struct nfqnl_instance *inst;
112 read_lock_bh(&instances_lock);
113 inst = __instance_lookup(queue_num);
114 read_unlock_bh(&instances_lock);
119 static struct nfqnl_instance *
120 instance_create(u_int16_t queue_num, int pid)
122 struct nfqnl_instance *inst;
124 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
126 write_lock_bh(&instances_lock);
127 if (__instance_lookup(queue_num)) {
129 QDEBUG("aborting, instance already exists\n");
133 inst = kmalloc(sizeof(*inst), GFP_ATOMIC);
137 memset(inst, 0, sizeof(*inst));
138 inst->queue_num = queue_num;
139 inst->peer_pid = pid;
140 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
141 inst->copy_range = 0xfffff;
142 inst->copy_mode = NFQNL_COPY_NONE;
143 atomic_set(&inst->id_sequence, 0);
144 inst->lock = SPIN_LOCK_UNLOCKED;
145 INIT_LIST_HEAD(&inst->queue_list);
147 if (!try_module_get(THIS_MODULE))
150 hlist_add_head(&inst->hlist,
151 &instance_table[instance_hashfn(queue_num)]);
153 write_unlock_bh(&instances_lock);
155 QDEBUG("successfully created new instance\n");
162 write_unlock_bh(&instances_lock);
166 static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
169 _instance_destroy2(struct nfqnl_instance *inst, int lock)
171 /* first pull it out of the global list */
173 write_lock_bh(&instances_lock);
175 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
176 inst, inst->queue_num);
177 hlist_del(&inst->hlist);
180 write_unlock_bh(&instances_lock);
182 /* then flush all pending skbs from the queue */
183 nfqnl_flush(inst, NF_DROP);
185 /* and finally free the data structure */
188 module_put(THIS_MODULE);
192 __instance_destroy(struct nfqnl_instance *inst)
194 _instance_destroy2(inst, 0);
198 instance_destroy(struct nfqnl_instance *inst)
200 _instance_destroy2(inst, 1);
206 issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
208 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
210 /* TCP input path (and probably other bits) assume to be called
211 * from softirq context, not from syscall, like issue_verdict is
212 * called. TCP input path deadlocks with locks taken from timer
213 * softirq, e.g. We therefore emulate this by local_bh_disable() */
216 nf_reinject(entry->skb, entry->info, verdict);
223 __enqueue_entry(struct nfqnl_instance *queue,
224 struct nfqnl_queue_entry *entry)
226 list_add(&entry->list, &queue->queue_list);
227 queue->queue_total++;
231 * Find and return a queued entry matched by cmpfn, or return the last
232 * entry if cmpfn is NULL.
234 static inline struct nfqnl_queue_entry *
235 __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
240 list_for_each_prev(p, &queue->queue_list) {
241 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
243 if (!cmpfn || cmpfn(entry, data))
250 __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
252 list_del(&entry->list);
256 static inline struct nfqnl_queue_entry *
257 __find_dequeue_entry(struct nfqnl_instance *queue,
258 nfqnl_cmpfn cmpfn, unsigned long data)
260 struct nfqnl_queue_entry *entry;
262 entry = __find_entry(queue, cmpfn, data);
266 __dequeue_entry(queue, entry);
272 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
274 struct nfqnl_queue_entry *entry;
276 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
277 issue_verdict(entry, verdict);
281 __nfqnl_set_mode(struct nfqnl_instance *queue,
282 unsigned char mode, unsigned int range)
287 case NFQNL_COPY_NONE:
288 case NFQNL_COPY_META:
289 queue->copy_mode = mode;
290 queue->copy_range = 0;
293 case NFQNL_COPY_PACKET:
294 queue->copy_mode = mode;
295 /* we're using struct nfattr which has 16bit nfa_len */
297 queue->copy_range = 0xffff;
299 queue->copy_range = range;
309 static struct nfqnl_queue_entry *
310 find_dequeue_entry(struct nfqnl_instance *queue,
311 nfqnl_cmpfn cmpfn, unsigned long data)
313 struct nfqnl_queue_entry *entry;
315 spin_lock_bh(&queue->lock);
316 entry = __find_dequeue_entry(queue, cmpfn, data);
317 spin_unlock_bh(&queue->lock);
323 nfqnl_flush(struct nfqnl_instance *queue, int verdict)
325 spin_lock_bh(&queue->lock);
326 __nfqnl_flush(queue, verdict);
327 spin_unlock_bh(&queue->lock);
330 static struct sk_buff *
331 nfqnl_build_packet_message(struct nfqnl_instance *queue,
332 struct nfqnl_queue_entry *entry, int *errp)
334 unsigned char *old_tail;
338 struct nfqnl_msg_packet_hdr pmsg;
339 struct nlmsghdr *nlh;
340 struct nfgenmsg *nfmsg;
341 unsigned int tmp_uint;
345 /* all macros expand to constant values at compile time */
346 size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
347 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
348 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
349 + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */
350 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
351 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
353 spin_lock_bh(&queue->lock);
355 switch (queue->copy_mode) {
356 case NFQNL_COPY_META:
357 case NFQNL_COPY_NONE:
361 case NFQNL_COPY_PACKET:
362 if (queue->copy_range == 0
363 || queue->copy_range > entry->skb->len)
364 data_len = entry->skb->len;
366 data_len = queue->copy_range;
368 size += NLMSG_SPACE(data_len);
373 spin_unlock_bh(&queue->lock);
377 spin_unlock_bh(&queue->lock);
379 skb = alloc_skb(size, GFP_ATOMIC);
384 nlh = NLMSG_PUT(skb, 0, 0,
385 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
386 sizeof(struct nfgenmsg));
387 nfmsg = NLMSG_DATA(nlh);
388 nfmsg->nfgen_family = entry->info->pf;
389 nfmsg->version = NFNETLINK_V0;
390 nfmsg->res_id = htons(queue->queue_num);
392 pmsg.packet_id = htonl(entry->id);
393 pmsg.hw_protocol = htons(entry->skb->protocol);
394 pmsg.hook = entry->info->hook;
396 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
398 if (entry->info->indev) {
399 tmp_uint = htonl(entry->info->indev->ifindex);
400 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
403 if (entry->info->outdev) {
404 tmp_uint = htonl(entry->info->outdev->ifindex);
405 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
408 if (entry->skb->nfmark) {
409 tmp_uint = htonl(entry->skb->nfmark);
410 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
413 if (entry->info->indev && entry->skb->dev
414 && entry->skb->dev->hard_header_parse) {
415 struct nfqnl_msg_packet_hw phw;
418 entry->skb->dev->hard_header_parse(entry->skb,
420 phw.hw_addrlen = htons(phw.hw_addrlen);
421 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
424 if (entry->skb->stamp.tv_sec) {
425 struct nfqnl_msg_packet_timestamp ts;
427 ts.sec = htonll(entry->skb->stamp.tv_sec);
428 ts.usec = htonll(entry->skb->stamp.tv_usec);
430 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
435 int size = NFA_LENGTH(data_len);
437 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
438 printk(KERN_WARNING "nf_queue: no tailroom!\n");
442 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
443 nfa->nfa_type = NFQA_PAYLOAD;
446 if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len))
450 nlh->nlmsg_len = skb->tail - old_tail;
459 printk(KERN_ERR "nf_queue: error creating packet message\n");
464 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
465 unsigned int queuenum, void *data)
467 int status = -EINVAL;
468 struct sk_buff *nskb;
469 struct nfqnl_instance *queue;
470 struct nfqnl_queue_entry *entry;
474 queue = instance_lookup(queuenum);
476 QDEBUG("no queue instance matching\n");
480 if (queue->copy_mode == NFQNL_COPY_NONE) {
481 QDEBUG("mode COPY_NONE, aborting\n");
485 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
489 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
495 entry->id = atomic_inc_return(&queue->id_sequence);
497 nskb = nfqnl_build_packet_message(queue, entry, &status);
501 spin_lock_bh(&queue->lock);
503 if (!queue->peer_pid)
504 goto err_out_free_nskb;
506 if (queue->queue_total >= queue->queue_maxlen) {
507 queue->queue_dropped++;
510 printk(KERN_WARNING "ip_queue: full at %d entries, "
511 "dropping packets(s). Dropped: %d\n",
512 queue->queue_total, queue->queue_dropped);
513 goto err_out_free_nskb;
516 /* nfnetlink_unicast will either free the nskb or add it to a socket */
517 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
519 queue->queue_user_dropped++;
523 __enqueue_entry(queue, entry);
525 spin_unlock_bh(&queue->lock);
532 spin_unlock_bh(&queue->lock);
540 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
544 diff = data_len - e->skb->len;
546 skb_trim(e->skb, data_len);
548 if (data_len > 0xFFFF)
550 if (diff > skb_tailroom(e->skb)) {
551 struct sk_buff *newskb;
553 newskb = skb_copy_expand(e->skb,
554 skb_headroom(e->skb),
557 if (newskb == NULL) {
558 printk(KERN_WARNING "ip_queue: OOM "
559 "in mangle, dropping packet\n");
563 skb_set_owner_w(newskb, e->skb->sk);
567 skb_put(e->skb, diff);
569 if (!skb_make_writable(&e->skb, data_len))
571 memcpy(e->skb->data, data, data_len);
577 id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
579 return (id == e->id);
583 nfqnl_set_mode(struct nfqnl_instance *queue,
584 unsigned char mode, unsigned int range)
588 spin_lock_bh(&queue->lock);
589 status = __nfqnl_set_mode(queue, mode, range);
590 spin_unlock_bh(&queue->lock);
596 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
598 if (entry->info->indev)
599 if (entry->info->indev->ifindex == ifindex)
602 if (entry->info->outdev)
603 if (entry->info->outdev->ifindex == ifindex)
609 /* drop all packets with either indev or outdev == ifindex from all queue
612 nfqnl_dev_drop(int ifindex)
616 QDEBUG("entering for ifindex %u\n", ifindex);
618 /* this only looks like we have to hold the readlock for a way too long
619 * time, issue_verdict(), nf_reinject(), ... - but we always only
620 * issue NF_DROP, which is processed directly in nf_reinject() */
621 read_lock_bh(&instances_lock);
623 for (i = 0; i < INSTANCE_BUCKETS; i++) {
624 struct hlist_node *tmp;
625 struct nfqnl_instance *inst;
626 struct hlist_head *head = &instance_table[i];
628 hlist_for_each_entry(inst, tmp, head, hlist) {
629 struct nfqnl_queue_entry *entry;
630 while ((entry = find_dequeue_entry(inst, dev_cmp,
632 issue_verdict(entry, NF_DROP);
636 read_unlock_bh(&instances_lock);
639 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
642 nfqnl_rcv_dev_event(struct notifier_block *this,
643 unsigned long event, void *ptr)
645 struct net_device *dev = ptr;
647 /* Drop any packets associated with the downed device */
648 if (event == NETDEV_DOWN)
649 nfqnl_dev_drop(dev->ifindex);
653 static struct notifier_block nfqnl_dev_notifier = {
654 .notifier_call = nfqnl_rcv_dev_event,
658 nfqnl_rcv_nl_event(struct notifier_block *this,
659 unsigned long event, void *ptr)
661 struct netlink_notify *n = ptr;
663 if (event == NETLINK_URELEASE &&
664 n->protocol == NETLINK_NETFILTER && n->pid) {
667 /* destroy all instances for this pid */
668 write_lock_bh(&instances_lock);
669 for (i = 0; i < INSTANCE_BUCKETS; i++) {
670 struct hlist_node *tmp, *t2;
671 struct nfqnl_instance *inst;
672 struct hlist_head *head = &instance_table[i];
674 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
675 if (n->pid == inst->peer_pid)
676 __instance_destroy(inst);
679 write_unlock_bh(&instances_lock);
684 static struct notifier_block nfqnl_rtnl_notifier = {
685 .notifier_call = nfqnl_rcv_nl_event,
689 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
690 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
692 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
693 u_int16_t queue_num = ntohs(nfmsg->res_id);
695 struct nfqnl_msg_verdict_hdr *vhdr;
696 struct nfqnl_instance *queue;
697 unsigned int verdict;
698 struct nfqnl_queue_entry *entry;
700 queue = instance_lookup(queue_num);
704 if (queue->peer_pid != NETLINK_CB(skb).pid)
707 if (!nfqa[NFQA_VERDICT_HDR-1])
710 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
711 verdict = ntohl(vhdr->verdict);
713 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT)
716 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
720 if (nfqa[NFQA_PAYLOAD-1]) {
721 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
722 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
726 if (nfqa[NFQA_MARK-1])
727 skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
729 issue_verdict(entry, verdict);
734 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
735 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
741 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
742 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
744 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
745 u_int16_t queue_num = ntohs(nfmsg->res_id);
746 struct nfqnl_instance *queue;
748 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
750 queue = instance_lookup(queue_num);
751 if (nfqa[NFQA_CFG_CMD-1]) {
752 struct nfqnl_msg_config_cmd *cmd;
753 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
754 QDEBUG("found CFG_CMD\n");
756 switch (cmd->command) {
757 case NFQNL_CFG_CMD_BIND:
761 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
765 case NFQNL_CFG_CMD_UNBIND:
769 if (queue->peer_pid != NETLINK_CB(skb).pid)
772 instance_destroy(queue);
774 case NFQNL_CFG_CMD_PF_BIND:
775 QDEBUG("registering queue handler for pf=%u\n",
777 return nf_register_queue_handler(ntohs(cmd->pf),
778 nfqnl_enqueue_packet,
782 case NFQNL_CFG_CMD_PF_UNBIND:
783 QDEBUG("unregistering queue handler for pf=%u\n",
785 /* This is a bug and a feature. We can unregister
786 * other handlers(!) */
787 return nf_unregister_queue_handler(ntohs(cmd->pf));
794 QDEBUG("no config command, and no instance ENOENT\n");
798 if (queue->peer_pid != NETLINK_CB(skb).pid) {
799 QDEBUG("no config command, and wrong pid\n");
804 if (nfqa[NFQA_CFG_PARAMS-1]) {
805 struct nfqnl_msg_config_params *params;
806 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
808 nfqnl_set_mode(queue, params->copy_mode,
809 ntohl(params->copy_range));
815 static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
816 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
817 .cap_required = CAP_NET_ADMIN },
818 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
819 .cap_required = CAP_NET_ADMIN },
820 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
821 .cap_required = CAP_NET_ADMIN },
824 static struct nfnetlink_subsystem nfqnl_subsys = {
826 .subsys_id = NFNL_SUBSYS_QUEUE,
827 .cb_count = NFQNL_MSG_MAX,
828 .attr_count = NFQA_MAX,
833 init_or_cleanup(int init)
835 int status = -ENOMEM;
840 netlink_register_notifier(&nfqnl_rtnl_notifier);
841 status = nfnetlink_subsys_register(&nfqnl_subsys);
843 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
844 goto cleanup_netlink_notifier;
847 register_netdevice_notifier(&nfqnl_dev_notifier);
851 nf_unregister_queue_handlers(nfqnl_enqueue_packet);
852 unregister_netdevice_notifier(&nfqnl_dev_notifier);
853 nfnetlink_subsys_unregister(&nfqnl_subsys);
855 cleanup_netlink_notifier:
856 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
860 static int __init init(void)
863 return init_or_cleanup(1);
866 static void __exit fini(void)
871 MODULE_DESCRIPTION("netfilter packet queue handler");
872 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
873 MODULE_LICENSE("GPL");
874 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);