blob: 8f1779ff7e30619825c43db9b06be07274bc2f8e [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* iptables module for using new netfilter netlink queue
2 *
3 * (C) 2005 by Harald Welte <laforge@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08006 * it under the terms of the GNU General Public License version 2 as
Harald Welte2e4e6a12006-01-12 13:30:04 -08007 * published by the Free Software Foundation.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08008 *
Harald Welte2e4e6a12006-01-12 13:30:04 -08009 */
10
11#include <linux/module.h>
12#include <linux/skbuff.h>
13
14#include <linux/netfilter.h>
15#include <linux/netfilter_arp.h>
16#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter/xt_NFQUEUE.h>
18
Eric Leblond97a2d412013-12-06 00:24:12 +010019#include <net/netfilter/nf_queue.h>
20
Harald Welte2e4e6a12006-01-12 13:30:04 -080021MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080022MODULE_DESCRIPTION("Xtables: packet forwarding to netlink");
Harald Welte2e4e6a12006-01-12 13:30:04 -080023MODULE_LICENSE("GPL");
24MODULE_ALIAS("ipt_NFQUEUE");
25MODULE_ALIAS("ip6t_NFQUEUE");
26MODULE_ALIAS("arpt_NFQUEUE");
27
Florian Westphal10662aa2009-06-05 13:24:24 +020028static u32 jhash_initval __read_mostly;
29
Harald Welte2e4e6a12006-01-12 13:30:04 -080030static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020031nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080032{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020033 const struct xt_NFQ_info *tinfo = par->targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080034
35 return NF_QUEUE_NR(tinfo->queuenum);
36}
37
Florian Westphal10662aa2009-06-05 13:24:24 +020038static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020039nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
Florian Westphal10662aa2009-06-05 13:24:24 +020040{
41 const struct xt_NFQ_info_v1 *info = par->targinfo;
42 u32 queue = info->queuenum;
43
Eric Leblond97a2d412013-12-06 00:24:12 +010044 if (info->queues_total > 1) {
45 queue = nfqueue_hash(skb, queue, info->queues_total,
46 par->family, jhash_initval);
47 }
Florian Westphal10662aa2009-06-05 13:24:24 +020048 return NF_QUEUE_NR(queue);
49}
Florian Westphal10662aa2009-06-05 13:24:24 +020050
Florian Westphal94b27cc2011-01-18 16:08:30 +010051static unsigned int
52nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
Florian Westphal10662aa2009-06-05 13:24:24 +020053{
Florian Westphal94b27cc2011-01-18 16:08:30 +010054 const struct xt_NFQ_info_v2 *info = par->targinfo;
55 unsigned int ret = nfqueue_tg_v1(skb, par);
56
57 if (info->bypass)
58 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
59 return ret;
60}
61
62static int nfqueue_tg_check(const struct xt_tgchk_param *par)
63{
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +000064 const struct xt_NFQ_info_v3 *info = par->targinfo;
Florian Westphal10662aa2009-06-05 13:24:24 +020065 u32 maxid;
66
Eric Leblond97a2d412013-12-06 00:24:12 +010067 init_hashrandom(&jhash_initval);
68
Florian Westphal10662aa2009-06-05 13:24:24 +020069 if (info->queues_total == 0) {
70 pr_err("NFQUEUE: number of total queues is 0\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010071 return -EINVAL;
Florian Westphal10662aa2009-06-05 13:24:24 +020072 }
73 maxid = info->queues_total - 1 + info->queuenum;
74 if (maxid > 0xffff) {
75 pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n",
76 info->queues_total, maxid);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010077 return -ERANGE;
Florian Westphal10662aa2009-06-05 13:24:24 +020078 }
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +000079 if (par->target->revision == 2 && info->flags > 1)
Florian Westphal94b27cc2011-01-18 16:08:30 +010080 return -EINVAL;
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +000081 if (par->target->revision == 3 && info->flags & ~NFQ_FLAG_MASK)
82 return -EINVAL;
83
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010084 return 0;
Florian Westphal10662aa2009-06-05 13:24:24 +020085}
86
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +000087static unsigned int
88nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
89{
90 const struct xt_NFQ_info_v3 *info = par->targinfo;
91 u32 queue = info->queuenum;
Holger Eitzenbergerd9547772013-10-28 14:42:33 +010092 int ret;
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +000093
94 if (info->queues_total > 1) {
95 if (info->flags & NFQ_FLAG_CPU_FANOUT) {
96 int cpu = smp_processor_id();
97
98 queue = info->queuenum + cpu % info->queues_total;
Eric Leblond97a2d412013-12-06 00:24:12 +010099 } else {
100 queue = nfqueue_hash(skb, queue, info->queues_total,
101 par->family, jhash_initval);
102 }
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +0000103 }
holger@eitzenberger.org5c334482013-03-23 10:04:04 +0000104
Holger Eitzenbergerd9547772013-10-28 14:42:33 +0100105 ret = NF_QUEUE_NR(queue);
106 if (info->flags & NFQ_FLAG_BYPASS)
107 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
108
109 return ret;
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +0000110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112static struct xt_target nfqueue_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700113 {
114 .name = "NFQUEUE",
Florian Westphal61f5abc2009-06-05 13:18:07 +0200115 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800116 .target = nfqueue_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700117 .targetsize = sizeof(struct xt_NFQ_info),
118 .me = THIS_MODULE,
119 },
Florian Westphal10662aa2009-06-05 13:24:24 +0200120 {
121 .name = "NFQUEUE",
122 .revision = 1,
Jan Engelhardtf76a47c2009-06-05 15:22:15 +0200123 .family = NFPROTO_UNSPEC,
Florian Westphal94b27cc2011-01-18 16:08:30 +0100124 .checkentry = nfqueue_tg_check,
Jan Engelhardtf76a47c2009-06-05 15:22:15 +0200125 .target = nfqueue_tg_v1,
Florian Westphal10662aa2009-06-05 13:24:24 +0200126 .targetsize = sizeof(struct xt_NFQ_info_v1),
127 .me = THIS_MODULE,
128 },
Florian Westphal94b27cc2011-01-18 16:08:30 +0100129 {
130 .name = "NFQUEUE",
131 .revision = 2,
132 .family = NFPROTO_UNSPEC,
133 .checkentry = nfqueue_tg_check,
134 .target = nfqueue_tg_v2,
135 .targetsize = sizeof(struct xt_NFQ_info_v2),
136 .me = THIS_MODULE,
137 },
holger@eitzenberger.org8746ddc2013-03-23 10:04:03 +0000138 {
139 .name = "NFQUEUE",
140 .revision = 3,
141 .family = NFPROTO_UNSPEC,
142 .checkentry = nfqueue_tg_check,
143 .target = nfqueue_tg_v3,
144 .targetsize = sizeof(struct xt_NFQ_info_v3),
145 .me = THIS_MODULE,
146 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147};
148
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800149static int __init nfqueue_tg_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800150{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800151 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152}
153
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800154static void __exit nfqueue_tg_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800155{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800156 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157}
158
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800159module_init(nfqueue_tg_init);
160module_exit(nfqueue_tg_exit);