Merge remote-tracking branch 'asoc/topic/pcm5102a' into asoc-next
[linux-2.6-block.git] / net / netfilter / xt_NFQUEUE.c
CommitLineData
2e4e6a17
HW
1/* 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
601e68e1 6 * it under the terms of the GNU General Public License version 2 as
2e4e6a17 7 * published by the Free Software Foundation.
601e68e1 8 *
2e4e6a17
HW
9 */
10
e016c5e4
FW
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
2e4e6a17
HW
13#include <linux/module.h>
14#include <linux/skbuff.h>
15
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>
20
97a2d41c
EL
21#include <net/netfilter/nf_queue.h>
22
2e4e6a17 23MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
2ae15b64 24MODULE_DESCRIPTION("Xtables: packet forwarding to netlink");
2e4e6a17
HW
25MODULE_LICENSE("GPL");
26MODULE_ALIAS("ipt_NFQUEUE");
27MODULE_ALIAS("ip6t_NFQUEUE");
28MODULE_ALIAS("arpt_NFQUEUE");
29
10662aa3
FW
30static u32 jhash_initval __read_mostly;
31
2e4e6a17 32static unsigned int
4b560b44 33nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
2e4e6a17 34{
7eb35586 35 const struct xt_NFQ_info *tinfo = par->targinfo;
2e4e6a17
HW
36
37 return NF_QUEUE_NR(tinfo->queuenum);
38}
39
5c33448c 40static unsigned int
41nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
42{
43 const struct xt_NFQ_info_v1 *info = par->targinfo;
44 u32 queue = info->queuenum;
45
97a2d41c
EL
46 if (info->queues_total > 1) {
47 queue = nfqueue_hash(skb, queue, info->queues_total,
613dbd95 48 xt_family(par), jhash_initval);
97a2d41c 49 }
10662aa3
FW
50 return NF_QUEUE_NR(queue);
51}
10662aa3 52
94b27cc3
FW
53static unsigned int
54nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
10662aa3 55{
94b27cc3
FW
56 const struct xt_NFQ_info_v2 *info = par->targinfo;
57 unsigned int ret = nfqueue_tg_v1(skb, par);
58
59 if (info->bypass)
60 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
61 return ret;
62}
63
64static int nfqueue_tg_check(const struct xt_tgchk_param *par)
65{
8746ddcf 66 const struct xt_NFQ_info_v3 *info = par->targinfo;
10662aa3
FW
67 u32 maxid;
68
97a2d41c
EL
69 init_hashrandom(&jhash_initval);
70
10662aa3 71 if (info->queues_total == 0) {
e016c5e4 72 pr_info_ratelimited("number of total queues is 0\n");
d6b00a53 73 return -EINVAL;
10662aa3
FW
74 }
75 maxid = info->queues_total - 1 + info->queuenum;
76 if (maxid > 0xffff) {
e016c5e4
FW
77 pr_info_ratelimited("number of queues (%u) out of range (got %u)\n",
78 info->queues_total, maxid);
4a5a5c73 79 return -ERANGE;
10662aa3 80 }
8746ddcf 81 if (par->target->revision == 2 && info->flags > 1)
82 return -EINVAL;
83 if (par->target->revision == 3 && info->flags & ~NFQ_FLAG_MASK)
94b27cc3 84 return -EINVAL;
8746ddcf 85
d6b00a53 86 return 0;
10662aa3
FW
87}
88
8746ddcf 89static unsigned int
90nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
91{
92 const struct xt_NFQ_info_v3 *info = par->targinfo;
93 u32 queue = info->queuenum;
d9547773 94 int ret;
8746ddcf 95
96 if (info->queues_total > 1) {
97 if (info->flags & NFQ_FLAG_CPU_FANOUT) {
98 int cpu = smp_processor_id();
99
100 queue = info->queuenum + cpu % info->queues_total;
97a2d41c
EL
101 } else {
102 queue = nfqueue_hash(skb, queue, info->queues_total,
613dbd95 103 xt_family(par), jhash_initval);
97a2d41c 104 }
8746ddcf 105 }
5c33448c 106
d9547773
HE
107 ret = NF_QUEUE_NR(queue);
108 if (info->flags & NFQ_FLAG_BYPASS)
109 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
110
111 return ret;
8746ddcf 112}
113
d3c5ee6d 114static struct xt_target nfqueue_tg_reg[] __read_mostly = {
4470bbc7
PM
115 {
116 .name = "NFQUEUE",
61f5abca 117 .family = NFPROTO_UNSPEC,
d3c5ee6d 118 .target = nfqueue_tg,
4470bbc7
PM
119 .targetsize = sizeof(struct xt_NFQ_info),
120 .me = THIS_MODULE,
121 },
10662aa3
FW
122 {
123 .name = "NFQUEUE",
124 .revision = 1,
f76a47c8 125 .family = NFPROTO_UNSPEC,
94b27cc3 126 .checkentry = nfqueue_tg_check,
f76a47c8 127 .target = nfqueue_tg_v1,
10662aa3
FW
128 .targetsize = sizeof(struct xt_NFQ_info_v1),
129 .me = THIS_MODULE,
130 },
94b27cc3
FW
131 {
132 .name = "NFQUEUE",
133 .revision = 2,
134 .family = NFPROTO_UNSPEC,
135 .checkentry = nfqueue_tg_check,
136 .target = nfqueue_tg_v2,
137 .targetsize = sizeof(struct xt_NFQ_info_v2),
138 .me = THIS_MODULE,
139 },
8746ddcf 140 {
141 .name = "NFQUEUE",
142 .revision = 3,
143 .family = NFPROTO_UNSPEC,
144 .checkentry = nfqueue_tg_check,
145 .target = nfqueue_tg_v3,
146 .targetsize = sizeof(struct xt_NFQ_info_v3),
147 .me = THIS_MODULE,
148 },
2e4e6a17
HW
149};
150
d3c5ee6d 151static int __init nfqueue_tg_init(void)
2e4e6a17 152{
d3c5ee6d 153 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
2e4e6a17
HW
154}
155
d3c5ee6d 156static void __exit nfqueue_tg_exit(void)
2e4e6a17 157{
d3c5ee6d 158 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
2e4e6a17
HW
159}
160
d3c5ee6d
JE
161module_init(nfqueue_tg_init);
162module_exit(nfqueue_tg_exit);