blob: 12dcd7007c3e4f6b8713752d7e2a49faeafb2ad5 [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
Florian Westphal10662aa2009-06-05 13:24:24 +020014#include <linux/ip.h>
15#include <linux/ipv6.h>
16#include <linux/jhash.h>
17
Harald Welte2e4e6a12006-01-12 13:30:04 -080018#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>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080024MODULE_DESCRIPTION("Xtables: packet forwarding to netlink");
Harald Welte2e4e6a12006-01-12 13:30:04 -080025MODULE_LICENSE("GPL");
26MODULE_ALIAS("ipt_NFQUEUE");
27MODULE_ALIAS("ip6t_NFQUEUE");
28MODULE_ALIAS("arpt_NFQUEUE");
29
Florian Westphal10662aa2009-06-05 13:24:24 +020030static u32 jhash_initval __read_mostly;
Jan Engelhardt5191d502010-01-04 16:27:25 +010031static bool rnd_inited __read_mostly;
Florian Westphal10662aa2009-06-05 13:24:24 +020032
Harald Welte2e4e6a12006-01-12 13:30:04 -080033static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020034nfqueue_tg(struct sk_buff *skb, const struct xt_target_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080035{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020036 const struct xt_NFQ_info *tinfo = par->targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080037
38 return NF_QUEUE_NR(tinfo->queuenum);
39}
40
Florian Westphal10662aa2009-06-05 13:24:24 +020041static u32 hash_v4(const struct sk_buff *skb)
42{
43 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020044 __be32 ipaddr;
Florian Westphal10662aa2009-06-05 13:24:24 +020045
46 /* packets in either direction go into same queue */
47 ipaddr = iph->saddr ^ iph->daddr;
48
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020049 return jhash_2words((__force u32)ipaddr, iph->protocol, jhash_initval);
Florian Westphal10662aa2009-06-05 13:24:24 +020050}
51
52static unsigned int
53nfqueue_tg4_v1(struct sk_buff *skb, const struct xt_target_param *par)
54{
55 const struct xt_NFQ_info_v1 *info = par->targinfo;
56 u32 queue = info->queuenum;
57
58 if (info->queues_total > 1)
59 queue = hash_v4(skb) % info->queues_total + queue;
60 return NF_QUEUE_NR(queue);
61}
62
63#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
64static u32 hash_v6(const struct sk_buff *skb)
65{
66 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020067 __be32 addr[4];
Florian Westphal10662aa2009-06-05 13:24:24 +020068
69 addr[0] = ip6h->saddr.s6_addr32[0] ^ ip6h->daddr.s6_addr32[0];
70 addr[1] = ip6h->saddr.s6_addr32[1] ^ ip6h->daddr.s6_addr32[1];
71 addr[2] = ip6h->saddr.s6_addr32[2] ^ ip6h->daddr.s6_addr32[2];
72 addr[3] = ip6h->saddr.s6_addr32[3] ^ ip6h->daddr.s6_addr32[3];
73
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020074 return jhash2((__force u32 *)addr, ARRAY_SIZE(addr), jhash_initval);
Florian Westphal10662aa2009-06-05 13:24:24 +020075}
76
77static unsigned int
78nfqueue_tg6_v1(struct sk_buff *skb, const struct xt_target_param *par)
79{
80 const struct xt_NFQ_info_v1 *info = par->targinfo;
81 u32 queue = info->queuenum;
82
83 if (info->queues_total > 1)
84 queue = hash_v6(skb) % info->queues_total + queue;
85 return NF_QUEUE_NR(queue);
86}
87#endif
88
89static bool nfqueue_tg_v1_check(const struct xt_tgchk_param *par)
90{
91 const struct xt_NFQ_info_v1 *info = par->targinfo;
92 u32 maxid;
93
Jan Engelhardt5191d502010-01-04 16:27:25 +010094 if (unlikely(!rnd_inited)) {
95 get_random_bytes(&jhash_initval, sizeof(jhash_initval));
96 rnd_inited = true;
97 }
Florian Westphal10662aa2009-06-05 13:24:24 +020098 if (info->queues_total == 0) {
99 pr_err("NFQUEUE: number of total queues is 0\n");
100 return false;
101 }
102 maxid = info->queues_total - 1 + info->queuenum;
103 if (maxid > 0xffff) {
104 pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n",
105 info->queues_total, maxid);
106 return false;
107 }
108 return true;
109}
110
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111static struct xt_target nfqueue_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700112 {
113 .name = "NFQUEUE",
Florian Westphal61f5abc2009-06-05 13:18:07 +0200114 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800115 .target = nfqueue_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700116 .targetsize = sizeof(struct xt_NFQ_info),
117 .me = THIS_MODULE,
118 },
Florian Westphal10662aa2009-06-05 13:24:24 +0200119 {
120 .name = "NFQUEUE",
121 .revision = 1,
122 .family = NFPROTO_IPV4,
123 .checkentry = nfqueue_tg_v1_check,
124 .target = nfqueue_tg4_v1,
125 .targetsize = sizeof(struct xt_NFQ_info_v1),
126 .me = THIS_MODULE,
127 },
128#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
129 {
130 .name = "NFQUEUE",
131 .revision = 1,
132 .family = NFPROTO_IPV6,
133 .checkentry = nfqueue_tg_v1_check,
134 .target = nfqueue_tg6_v1,
135 .targetsize = sizeof(struct xt_NFQ_info_v1),
136 .me = THIS_MODULE,
137 },
138#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800139};
140
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141static int __init nfqueue_tg_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800143 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144}
145
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800146static void __exit nfqueue_tg_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800148 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800149}
150
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800151module_init(nfqueue_tg_init);
152module_exit(nfqueue_tg_exit);