Merge branch 'x86-smap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.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
11#include <linux/module.h>
12#include <linux/skbuff.h>
13
10662aa3
FW
14#include <linux/ip.h>
15#include <linux/ipv6.h>
16#include <linux/jhash.h>
17
2e4e6a17
HW
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>
22
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 30static u32 jhash_initval __read_mostly;
5191d501 31static bool rnd_inited __read_mostly;
10662aa3 32
2e4e6a17 33static unsigned int
4b560b44 34nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par)
2e4e6a17 35{
7eb35586 36 const struct xt_NFQ_info *tinfo = par->targinfo;
2e4e6a17
HW
37
38 return NF_QUEUE_NR(tinfo->queuenum);
39}
40
10662aa3
FW
41static u32 hash_v4(const struct sk_buff *skb)
42{
43 const struct iphdr *iph = ip_hdr(skb);
10662aa3
FW
44
45 /* packets in either direction go into same queue */
1da6dd07
FW
46 if (iph->saddr < iph->daddr)
47 return jhash_3words((__force u32)iph->saddr,
48 (__force u32)iph->daddr, iph->protocol, jhash_initval);
10662aa3 49
1da6dd07
FW
50 return jhash_3words((__force u32)iph->daddr,
51 (__force u32)iph->saddr, iph->protocol, jhash_initval);
10662aa3
FW
52}
53
c0cd1156 54#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
10662aa3
FW
55static u32 hash_v6(const struct sk_buff *skb)
56{
57 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1da6dd07 58 u32 a, b, c;
10662aa3 59
1da6dd07
FW
60 if (ip6h->saddr.s6_addr32[3] < ip6h->daddr.s6_addr32[3]) {
61 a = (__force u32) ip6h->saddr.s6_addr32[3];
62 b = (__force u32) ip6h->daddr.s6_addr32[3];
63 } else {
64 b = (__force u32) ip6h->saddr.s6_addr32[3];
65 a = (__force u32) ip6h->daddr.s6_addr32[3];
66 }
10662aa3 67
1da6dd07
FW
68 if (ip6h->saddr.s6_addr32[1] < ip6h->daddr.s6_addr32[1])
69 c = (__force u32) ip6h->saddr.s6_addr32[1];
70 else
71 c = (__force u32) ip6h->daddr.s6_addr32[1];
72
73 return jhash_3words(a, b, c, jhash_initval);
10662aa3 74}
f76a47c8 75#endif
10662aa3
FW
76
77static unsigned int
4b560b44 78nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
10662aa3
FW
79{
80 const struct xt_NFQ_info_v1 *info = par->targinfo;
81 u32 queue = info->queuenum;
82
f76a47c8 83 if (info->queues_total > 1) {
0d345455 84 if (par->family == NFPROTO_IPV4)
ca361810
CG
85 queue = (((u64) hash_v4(skb) * info->queues_total) >>
86 32) + queue;
c0cd1156 87#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
0d345455 88 else if (par->family == NFPROTO_IPV6)
ca361810
CG
89 queue = (((u64) hash_v6(skb) * info->queues_total) >>
90 32) + queue;
f76a47c8
JE
91#endif
92 }
10662aa3
FW
93 return NF_QUEUE_NR(queue);
94}
10662aa3 95
94b27cc3
FW
96static unsigned int
97nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
10662aa3 98{
94b27cc3
FW
99 const struct xt_NFQ_info_v2 *info = par->targinfo;
100 unsigned int ret = nfqueue_tg_v1(skb, par);
101
102 if (info->bypass)
103 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
104 return ret;
105}
106
107static int nfqueue_tg_check(const struct xt_tgchk_param *par)
108{
109 const struct xt_NFQ_info_v2 *info = par->targinfo;
10662aa3
FW
110 u32 maxid;
111
5191d501
JE
112 if (unlikely(!rnd_inited)) {
113 get_random_bytes(&jhash_initval, sizeof(jhash_initval));
114 rnd_inited = true;
115 }
10662aa3
FW
116 if (info->queues_total == 0) {
117 pr_err("NFQUEUE: number of total queues is 0\n");
d6b00a53 118 return -EINVAL;
10662aa3
FW
119 }
120 maxid = info->queues_total - 1 + info->queuenum;
121 if (maxid > 0xffff) {
122 pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n",
123 info->queues_total, maxid);
4a5a5c73 124 return -ERANGE;
10662aa3 125 }
94b27cc3
FW
126 if (par->target->revision == 2 && info->bypass > 1)
127 return -EINVAL;
d6b00a53 128 return 0;
10662aa3
FW
129}
130
d3c5ee6d 131static struct xt_target nfqueue_tg_reg[] __read_mostly = {
4470bbc7
PM
132 {
133 .name = "NFQUEUE",
61f5abca 134 .family = NFPROTO_UNSPEC,
d3c5ee6d 135 .target = nfqueue_tg,
4470bbc7
PM
136 .targetsize = sizeof(struct xt_NFQ_info),
137 .me = THIS_MODULE,
138 },
10662aa3
FW
139 {
140 .name = "NFQUEUE",
141 .revision = 1,
f76a47c8 142 .family = NFPROTO_UNSPEC,
94b27cc3 143 .checkentry = nfqueue_tg_check,
f76a47c8 144 .target = nfqueue_tg_v1,
10662aa3
FW
145 .targetsize = sizeof(struct xt_NFQ_info_v1),
146 .me = THIS_MODULE,
147 },
94b27cc3
FW
148 {
149 .name = "NFQUEUE",
150 .revision = 2,
151 .family = NFPROTO_UNSPEC,
152 .checkentry = nfqueue_tg_check,
153 .target = nfqueue_tg_v2,
154 .targetsize = sizeof(struct xt_NFQ_info_v2),
155 .me = THIS_MODULE,
156 },
2e4e6a17
HW
157};
158
d3c5ee6d 159static int __init nfqueue_tg_init(void)
2e4e6a17 160{
d3c5ee6d 161 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
2e4e6a17
HW
162}
163
d3c5ee6d 164static void __exit nfqueue_tg_exit(void)
2e4e6a17 165{
d3c5ee6d 166 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
2e4e6a17
HW
167}
168
d3c5ee6d
JE
169module_init(nfqueue_tg_init);
170module_exit(nfqueue_tg_exit);