]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
2ae15b64 | 24 | MODULE_DESCRIPTION("Xtables: packet forwarding to netlink"); |
2e4e6a17 HW |
25 | MODULE_LICENSE("GPL"); |
26 | MODULE_ALIAS("ipt_NFQUEUE"); | |
27 | MODULE_ALIAS("ip6t_NFQUEUE"); | |
28 | MODULE_ALIAS("arpt_NFQUEUE"); | |
29 | ||
10662aa3 | 30 | static u32 jhash_initval __read_mostly; |
5191d501 | 31 | static bool rnd_inited __read_mostly; |
10662aa3 | 32 | |
2e4e6a17 | 33 | static unsigned int |
4b560b44 | 34 | nfqueue_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 |
41 | static u32 hash_v4(const struct sk_buff *skb) |
42 | { | |
43 | const struct iphdr *iph = ip_hdr(skb); | |
f9ffc312 | 44 | __be32 ipaddr; |
10662aa3 FW |
45 | |
46 | /* packets in either direction go into same queue */ | |
47 | ipaddr = iph->saddr ^ iph->daddr; | |
48 | ||
f9ffc312 | 49 | return jhash_2words((__force u32)ipaddr, iph->protocol, jhash_initval); |
10662aa3 FW |
50 | } |
51 | ||
10662aa3 FW |
52 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
53 | static u32 hash_v6(const struct sk_buff *skb) | |
54 | { | |
55 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); | |
f9ffc312 | 56 | __be32 addr[4]; |
10662aa3 FW |
57 | |
58 | addr[0] = ip6h->saddr.s6_addr32[0] ^ ip6h->daddr.s6_addr32[0]; | |
59 | addr[1] = ip6h->saddr.s6_addr32[1] ^ ip6h->daddr.s6_addr32[1]; | |
60 | addr[2] = ip6h->saddr.s6_addr32[2] ^ ip6h->daddr.s6_addr32[2]; | |
61 | addr[3] = ip6h->saddr.s6_addr32[3] ^ ip6h->daddr.s6_addr32[3]; | |
62 | ||
f9ffc312 | 63 | return jhash2((__force u32 *)addr, ARRAY_SIZE(addr), jhash_initval); |
10662aa3 | 64 | } |
f76a47c8 | 65 | #endif |
10662aa3 FW |
66 | |
67 | static unsigned int | |
4b560b44 | 68 | nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par) |
10662aa3 FW |
69 | { |
70 | const struct xt_NFQ_info_v1 *info = par->targinfo; | |
71 | u32 queue = info->queuenum; | |
72 | ||
f76a47c8 | 73 | if (info->queues_total > 1) { |
0d345455 | 74 | if (par->family == NFPROTO_IPV4) |
f76a47c8 JE |
75 | queue = hash_v4(skb) % info->queues_total + queue; |
76 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) | |
0d345455 | 77 | else if (par->family == NFPROTO_IPV6) |
f76a47c8 JE |
78 | queue = hash_v6(skb) % info->queues_total + queue; |
79 | #endif | |
80 | } | |
10662aa3 FW |
81 | return NF_QUEUE_NR(queue); |
82 | } | |
10662aa3 | 83 | |
135367b8 | 84 | static int nfqueue_tg_v1_check(const struct xt_tgchk_param *par) |
10662aa3 FW |
85 | { |
86 | const struct xt_NFQ_info_v1 *info = par->targinfo; | |
87 | u32 maxid; | |
88 | ||
5191d501 JE |
89 | if (unlikely(!rnd_inited)) { |
90 | get_random_bytes(&jhash_initval, sizeof(jhash_initval)); | |
91 | rnd_inited = true; | |
92 | } | |
10662aa3 FW |
93 | if (info->queues_total == 0) { |
94 | pr_err("NFQUEUE: number of total queues is 0\n"); | |
d6b00a53 | 95 | return -EINVAL; |
10662aa3 FW |
96 | } |
97 | maxid = info->queues_total - 1 + info->queuenum; | |
98 | if (maxid > 0xffff) { | |
99 | pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n", | |
100 | info->queues_total, maxid); | |
4a5a5c73 | 101 | return -ERANGE; |
10662aa3 | 102 | } |
d6b00a53 | 103 | return 0; |
10662aa3 FW |
104 | } |
105 | ||
d3c5ee6d | 106 | static struct xt_target nfqueue_tg_reg[] __read_mostly = { |
4470bbc7 PM |
107 | { |
108 | .name = "NFQUEUE", | |
61f5abca | 109 | .family = NFPROTO_UNSPEC, |
d3c5ee6d | 110 | .target = nfqueue_tg, |
4470bbc7 PM |
111 | .targetsize = sizeof(struct xt_NFQ_info), |
112 | .me = THIS_MODULE, | |
113 | }, | |
10662aa3 FW |
114 | { |
115 | .name = "NFQUEUE", | |
116 | .revision = 1, | |
f76a47c8 | 117 | .family = NFPROTO_UNSPEC, |
10662aa3 | 118 | .checkentry = nfqueue_tg_v1_check, |
f76a47c8 | 119 | .target = nfqueue_tg_v1, |
10662aa3 FW |
120 | .targetsize = sizeof(struct xt_NFQ_info_v1), |
121 | .me = THIS_MODULE, | |
122 | }, | |
2e4e6a17 HW |
123 | }; |
124 | ||
d3c5ee6d | 125 | static int __init nfqueue_tg_init(void) |
2e4e6a17 | 126 | { |
d3c5ee6d | 127 | return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg)); |
2e4e6a17 HW |
128 | } |
129 | ||
d3c5ee6d | 130 | static void __exit nfqueue_tg_exit(void) |
2e4e6a17 | 131 | { |
d3c5ee6d | 132 | xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg)); |
2e4e6a17 HW |
133 | } |
134 | ||
d3c5ee6d JE |
135 | module_init(nfqueue_tg_init); |
136 | module_exit(nfqueue_tg_exit); |