aio: checking for NULL instead of IS_ERR
[linux-2.6/btrfs-unstable.git] / net / netfilter / xt_NFQUEUE.c
blobed00fef58996a8d702b6020720daf679caa1af15
1 /* iptables module for using new netfilter netlink queue
3 * (C) 2005 by Harald Welte <laforge@netfilter.org>
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.
9 */
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <linux/ipv6.h>
16 #include <linux/jhash.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_arp.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter/xt_NFQUEUE.h>
23 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
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;
31 static bool rnd_inited __read_mostly;
33 static unsigned int
34 nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
36 const struct xt_NFQ_info *tinfo = par->targinfo;
38 return NF_QUEUE_NR(tinfo->queuenum);
41 static u32 hash_v4(const struct sk_buff *skb)
43 const struct iphdr *iph = ip_hdr(skb);
45 /* packets in either direction go into same queue */
46 if ((__force u32)iph->saddr < (__force u32)iph->daddr)
47 return jhash_3words((__force u32)iph->saddr,
48 (__force u32)iph->daddr, iph->protocol, jhash_initval);
50 return jhash_3words((__force u32)iph->daddr,
51 (__force u32)iph->saddr, iph->protocol, jhash_initval);
54 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
55 static u32 hash_v6(const struct sk_buff *skb)
57 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
58 u32 a, b, c;
60 if ((__force u32)ip6h->saddr.s6_addr32[3] <
61 (__force u32)ip6h->daddr.s6_addr32[3]) {
62 a = (__force u32) ip6h->saddr.s6_addr32[3];
63 b = (__force u32) ip6h->daddr.s6_addr32[3];
64 } else {
65 b = (__force u32) ip6h->saddr.s6_addr32[3];
66 a = (__force u32) ip6h->daddr.s6_addr32[3];
69 if ((__force u32)ip6h->saddr.s6_addr32[1] <
70 (__force u32)ip6h->daddr.s6_addr32[1])
71 c = (__force u32) ip6h->saddr.s6_addr32[1];
72 else
73 c = (__force u32) ip6h->daddr.s6_addr32[1];
75 return jhash_3words(a, b, c, jhash_initval);
77 #endif
79 static u32
80 nfqueue_hash(const struct sk_buff *skb, const struct xt_action_param *par)
82 const struct xt_NFQ_info_v1 *info = par->targinfo;
83 u32 queue = info->queuenum;
85 if (par->family == NFPROTO_IPV4)
86 queue += ((u64) hash_v4(skb) * info->queues_total) >> 32;
87 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
88 else if (par->family == NFPROTO_IPV6)
89 queue += ((u64) hash_v6(skb) * info->queues_total) >> 32;
90 #endif
92 return queue;
95 static unsigned int
96 nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
98 const struct xt_NFQ_info_v1 *info = par->targinfo;
99 u32 queue = info->queuenum;
101 if (info->queues_total > 1)
102 queue = nfqueue_hash(skb, par);
104 return NF_QUEUE_NR(queue);
107 static unsigned int
108 nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
110 const struct xt_NFQ_info_v2 *info = par->targinfo;
111 unsigned int ret = nfqueue_tg_v1(skb, par);
113 if (info->bypass)
114 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
115 return ret;
118 static int nfqueue_tg_check(const struct xt_tgchk_param *par)
120 const struct xt_NFQ_info_v3 *info = par->targinfo;
121 u32 maxid;
123 if (unlikely(!rnd_inited)) {
124 get_random_bytes(&jhash_initval, sizeof(jhash_initval));
125 rnd_inited = true;
127 if (info->queues_total == 0) {
128 pr_err("NFQUEUE: number of total queues is 0\n");
129 return -EINVAL;
131 maxid = info->queues_total - 1 + info->queuenum;
132 if (maxid > 0xffff) {
133 pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n",
134 info->queues_total, maxid);
135 return -ERANGE;
137 if (par->target->revision == 2 && info->flags > 1)
138 return -EINVAL;
139 if (par->target->revision == 3 && info->flags & ~NFQ_FLAG_MASK)
140 return -EINVAL;
142 return 0;
145 static unsigned int
146 nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
148 const struct xt_NFQ_info_v3 *info = par->targinfo;
149 u32 queue = info->queuenum;
150 int ret;
152 if (info->queues_total > 1) {
153 if (info->flags & NFQ_FLAG_CPU_FANOUT) {
154 int cpu = smp_processor_id();
156 queue = info->queuenum + cpu % info->queues_total;
157 } else
158 queue = nfqueue_hash(skb, par);
161 ret = NF_QUEUE_NR(queue);
162 if (info->flags & NFQ_FLAG_BYPASS)
163 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
165 return ret;
168 static struct xt_target nfqueue_tg_reg[] __read_mostly = {
170 .name = "NFQUEUE",
171 .family = NFPROTO_UNSPEC,
172 .target = nfqueue_tg,
173 .targetsize = sizeof(struct xt_NFQ_info),
174 .me = THIS_MODULE,
177 .name = "NFQUEUE",
178 .revision = 1,
179 .family = NFPROTO_UNSPEC,
180 .checkentry = nfqueue_tg_check,
181 .target = nfqueue_tg_v1,
182 .targetsize = sizeof(struct xt_NFQ_info_v1),
183 .me = THIS_MODULE,
186 .name = "NFQUEUE",
187 .revision = 2,
188 .family = NFPROTO_UNSPEC,
189 .checkentry = nfqueue_tg_check,
190 .target = nfqueue_tg_v2,
191 .targetsize = sizeof(struct xt_NFQ_info_v2),
192 .me = THIS_MODULE,
195 .name = "NFQUEUE",
196 .revision = 3,
197 .family = NFPROTO_UNSPEC,
198 .checkentry = nfqueue_tg_check,
199 .target = nfqueue_tg_v3,
200 .targetsize = sizeof(struct xt_NFQ_info_v3),
201 .me = THIS_MODULE,
205 static int __init nfqueue_tg_init(void)
207 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
210 static void __exit nfqueue_tg_exit(void)
212 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
215 module_init(nfqueue_tg_init);
216 module_exit(nfqueue_tg_exit);