blob: 7babe7d687169d6231fa17149bfd10cb52ee7ebe [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 Engelhardt4b560b42009-07-05 19:43:26 +020034nfqueue_tg(struct sk_buff *skb, const struct xt_action_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);
Florian Westphal10662aa2009-06-05 13:24:24 +020044
45 /* packets in either direction go into same queue */
Florian Westphal1da6dd02012-06-04 02:53:54 +000046 if (iph->saddr < iph->daddr)
47 return jhash_3words((__force u32)iph->saddr,
48 (__force u32)iph->daddr, iph->protocol, jhash_initval);
Florian Westphal10662aa2009-06-05 13:24:24 +020049
Florian Westphal1da6dd02012-06-04 02:53:54 +000050 return jhash_3words((__force u32)iph->daddr,
51 (__force u32)iph->saddr, iph->protocol, jhash_initval);
Florian Westphal10662aa2009-06-05 13:24:24 +020052}
53
Igor Maravićc0cd1152011-12-12 02:58:24 +000054#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Florian Westphal10662aa2009-06-05 13:24:24 +020055static u32 hash_v6(const struct sk_buff *skb)
56{
57 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Florian Westphal1da6dd02012-06-04 02:53:54 +000058 u32 a, b, c;
Florian Westphal10662aa2009-06-05 13:24:24 +020059
Florian Westphal1da6dd02012-06-04 02:53:54 +000060 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 }
Florian Westphal10662aa2009-06-05 13:24:24 +020067
Florian Westphal1da6dd02012-06-04 02:53:54 +000068 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);
Florian Westphal10662aa2009-06-05 13:24:24 +020074}
Jan Engelhardtf76a47c2009-06-05 15:22:15 +020075#endif
Florian Westphal10662aa2009-06-05 13:24:24 +020076
77static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020078nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
Florian Westphal10662aa2009-06-05 13:24:24 +020079{
80 const struct xt_NFQ_info_v1 *info = par->targinfo;
81 u32 queue = info->queuenum;
82
Jan Engelhardtf76a47c2009-06-05 15:22:15 +020083 if (info->queues_total > 1) {
Jan Engelhardt0d345452010-03-19 18:47:51 +010084 if (par->family == NFPROTO_IPV4)
Changli Gaoca361812010-11-12 17:34:17 +010085 queue = (((u64) hash_v4(skb) * info->queues_total) >>
86 32) + queue;
Igor Maravićc0cd1152011-12-12 02:58:24 +000087#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
Jan Engelhardt0d345452010-03-19 18:47:51 +010088 else if (par->family == NFPROTO_IPV6)
Changli Gaoca361812010-11-12 17:34:17 +010089 queue = (((u64) hash_v6(skb) * info->queues_total) >>
90 32) + queue;
Jan Engelhardtf76a47c2009-06-05 15:22:15 +020091#endif
92 }
Florian Westphal10662aa2009-06-05 13:24:24 +020093 return NF_QUEUE_NR(queue);
94}
Florian Westphal10662aa2009-06-05 13:24:24 +020095
Florian Westphal94b27cc2011-01-18 16:08:30 +010096static unsigned int
97nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
Florian Westphal10662aa2009-06-05 13:24:24 +020098{
Florian Westphal94b27cc2011-01-18 16:08:30 +010099 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;
Florian Westphal10662aa2009-06-05 13:24:24 +0200110 u32 maxid;
111
Jan Engelhardt5191d502010-01-04 16:27:25 +0100112 if (unlikely(!rnd_inited)) {
113 get_random_bytes(&jhash_initval, sizeof(jhash_initval));
114 rnd_inited = true;
115 }
Florian Westphal10662aa2009-06-05 13:24:24 +0200116 if (info->queues_total == 0) {
117 pr_err("NFQUEUE: number of total queues is 0\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100118 return -EINVAL;
Florian Westphal10662aa2009-06-05 13:24:24 +0200119 }
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);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100124 return -ERANGE;
Florian Westphal10662aa2009-06-05 13:24:24 +0200125 }
Florian Westphal94b27cc2011-01-18 16:08:30 +0100126 if (par->target->revision == 2 && info->bypass > 1)
127 return -EINVAL;
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100128 return 0;
Florian Westphal10662aa2009-06-05 13:24:24 +0200129}
130
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800131static struct xt_target nfqueue_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700132 {
133 .name = "NFQUEUE",
Florian Westphal61f5abc2009-06-05 13:18:07 +0200134 .family = NFPROTO_UNSPEC,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800135 .target = nfqueue_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700136 .targetsize = sizeof(struct xt_NFQ_info),
137 .me = THIS_MODULE,
138 },
Florian Westphal10662aa2009-06-05 13:24:24 +0200139 {
140 .name = "NFQUEUE",
141 .revision = 1,
Jan Engelhardtf76a47c2009-06-05 15:22:15 +0200142 .family = NFPROTO_UNSPEC,
Florian Westphal94b27cc2011-01-18 16:08:30 +0100143 .checkentry = nfqueue_tg_check,
Jan Engelhardtf76a47c2009-06-05 15:22:15 +0200144 .target = nfqueue_tg_v1,
Florian Westphal10662aa2009-06-05 13:24:24 +0200145 .targetsize = sizeof(struct xt_NFQ_info_v1),
146 .me = THIS_MODULE,
147 },
Florian Westphal94b27cc2011-01-18 16:08:30 +0100148 {
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 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157};
158
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800159static int __init nfqueue_tg_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800160{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800161 return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800162}
163
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800164static void __exit nfqueue_tg_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800165{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800166 xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800167}
168
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800169module_init(nfqueue_tg_init);
170module_exit(nfqueue_tg_exit);