1 /* iptables module for using new netfilter netlink queue
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_arp.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter/xt_NFQUEUE.h>
21 #include <net/netfilter/nf_queue.h>
24 MODULE_DESCRIPTION("Xtables: packet forwarding to netlink");
25 MODULE_LICENSE("GPL");
26 MODULE_ALIAS("ipt_NFQUEUE");
27 MODULE_ALIAS("ip6t_NFQUEUE");
28 MODULE_ALIAS("arpt_NFQUEUE");
30 static u32 jhash_initval __read_mostly;
33 nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
35 const struct xt_NFQ_info *tinfo = par->targinfo;
37 return NF_QUEUE_NR(tinfo->queuenum);
41 nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
43 const struct xt_NFQ_info_v1 *info = par->targinfo;
44 u32 queue = info->queuenum;
46 if (info->queues_total > 1) {
47 queue = nfqueue_hash(skb, queue, info->queues_total,
48 xt_family(par), jhash_initval);
50 return NF_QUEUE_NR(queue);
54 nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
56 const struct xt_NFQ_info_v2 *info = par->targinfo;
57 unsigned int ret = nfqueue_tg_v1(skb, par);
60 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
64 static int nfqueue_tg_check(const struct xt_tgchk_param *par)
66 const struct xt_NFQ_info_v3 *info = par->targinfo;
69 init_hashrandom(&jhash_initval);
71 if (info->queues_total == 0) {
72 pr_info_ratelimited("number of total queues is 0\n");
75 maxid = info->queues_total - 1 + info->queuenum;
77 pr_info_ratelimited("number of queues (%u) out of range (got %u)\n",
78 info->queues_total, maxid);
81 if (par->target->revision == 2 && info->flags > 1)
83 if (par->target->revision == 3 && info->flags & ~NFQ_FLAG_MASK)
90 nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
92 const struct xt_NFQ_info_v3 *info = par->targinfo;
93 u32 queue = info->queuenum;
96 if (info->queues_total > 1) {
97 if (info->flags & NFQ_FLAG_CPU_FANOUT) {
98 int cpu = smp_processor_id();
100 queue = info->queuenum + cpu % info->queues_total;
102 queue = nfqueue_hash(skb, queue, info->queues_total,
103 xt_family(par), jhash_initval);
107 ret = NF_QUEUE_NR(queue);
108 if (info->flags & NFQ_FLAG_BYPASS)
109 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
114 static struct xt_target nfqueue_tg_reg[] __read_mostly = {
117 .family = NFPROTO_UNSPEC,
118 .target = nfqueue_tg,
119 .targetsize = sizeof(struct xt_NFQ_info),
125 .family = NFPROTO_UNSPEC,
126 .checkentry = nfqueue_tg_check,
127 .target = nfqueue_tg_v1,
128 .targetsize = sizeof(struct xt_NFQ_info_v1),
134 .family = NFPROTO_UNSPEC,
135 .checkentry = nfqueue_tg_check,
136 .target = nfqueue_tg_v2,
137 .targetsize = sizeof(struct xt_NFQ_info_v2),
143 .family = NFPROTO_UNSPEC,
144 .checkentry = nfqueue_tg_check,
145 .target = nfqueue_tg_v3,
146 .targetsize = sizeof(struct xt_NFQ_info_v3),
151 static int __init nfqueue_tg_init(void)
153 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
156 static void __exit nfqueue_tg_exit(void)
158 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
161 module_init(nfqueue_tg_init);
162 module_exit(nfqueue_tg_exit);