Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 6 | * it under the terms of the GNU General Public License version 2 as |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 8 | * |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 14 | #include <linux/ip.h> |
| 15 | #include <linux/ipv6.h> |
| 16 | #include <linux/jhash.h> |
| 17 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 24 | MODULE_DESCRIPTION("Xtables: packet forwarding to netlink"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | MODULE_LICENSE("GPL"); |
| 26 | MODULE_ALIAS("ipt_NFQUEUE"); |
| 27 | MODULE_ALIAS("ip6t_NFQUEUE"); |
| 28 | MODULE_ALIAS("arpt_NFQUEUE"); |
| 29 | |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 30 | static u32 jhash_initval __read_mostly; |
Jan Engelhardt | 5191d50 | 2010-01-04 16:27:25 +0100 | [diff] [blame] | 31 | static bool rnd_inited __read_mostly; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 32 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 33 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 34 | nfqueue_tg(struct sk_buff *skb, const struct xt_action_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 35 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 36 | const struct xt_NFQ_info *tinfo = par->targinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | |
| 38 | return NF_QUEUE_NR(tinfo->queuenum); |
| 39 | } |
| 40 | |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 41 | static u32 hash_v4(const struct sk_buff *skb) |
| 42 | { |
| 43 | const struct iphdr *iph = ip_hdr(skb); |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 44 | |
| 45 | /* packets in either direction go into same queue */ |
Patrick McHardy | fe31d1a | 2012-08-19 10:16:09 +0000 | [diff] [blame] | 46 | if ((__force u32)iph->saddr < (__force u32)iph->daddr) |
Florian Westphal | 1da6dd0 | 2012-06-04 02:53:54 +0000 | [diff] [blame] | 47 | return jhash_3words((__force u32)iph->saddr, |
| 48 | (__force u32)iph->daddr, iph->protocol, jhash_initval); |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 49 | |
Florian Westphal | 1da6dd0 | 2012-06-04 02:53:54 +0000 | [diff] [blame] | 50 | return jhash_3words((__force u32)iph->daddr, |
| 51 | (__force u32)iph->saddr, iph->protocol, jhash_initval); |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 54 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 55 | static u32 hash_v6(const struct sk_buff *skb) |
| 56 | { |
| 57 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
Florian Westphal | 1da6dd0 | 2012-06-04 02:53:54 +0000 | [diff] [blame] | 58 | u32 a, b, c; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 59 | |
Patrick McHardy | fe31d1a | 2012-08-19 10:16:09 +0000 | [diff] [blame] | 60 | if ((__force u32)ip6h->saddr.s6_addr32[3] < |
| 61 | (__force u32)ip6h->daddr.s6_addr32[3]) { |
Florian Westphal | 1da6dd0 | 2012-06-04 02:53:54 +0000 | [diff] [blame] | 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]; |
| 67 | } |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 68 | |
Patrick McHardy | fe31d1a | 2012-08-19 10:16:09 +0000 | [diff] [blame] | 69 | if ((__force u32)ip6h->saddr.s6_addr32[1] < |
| 70 | (__force u32)ip6h->daddr.s6_addr32[1]) |
Florian Westphal | 1da6dd0 | 2012-06-04 02:53:54 +0000 | [diff] [blame] | 71 | c = (__force u32) ip6h->saddr.s6_addr32[1]; |
| 72 | else |
| 73 | c = (__force u32) ip6h->daddr.s6_addr32[1]; |
| 74 | |
| 75 | return jhash_3words(a, b, c, jhash_initval); |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 76 | } |
Jan Engelhardt | f76a47c | 2009-06-05 15:22:15 +0200 | [diff] [blame] | 77 | #endif |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 78 | |
| 79 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 80 | nfqueue_tg_v1(struct sk_buff *skb, const struct xt_action_param *par) |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 81 | { |
| 82 | const struct xt_NFQ_info_v1 *info = par->targinfo; |
| 83 | u32 queue = info->queuenum; |
| 84 | |
Jan Engelhardt | f76a47c | 2009-06-05 15:22:15 +0200 | [diff] [blame] | 85 | if (info->queues_total > 1) { |
Jan Engelhardt | 0d34545 | 2010-03-19 18:47:51 +0100 | [diff] [blame] | 86 | if (par->family == NFPROTO_IPV4) |
Changli Gao | ca36181 | 2010-11-12 17:34:17 +0100 | [diff] [blame] | 87 | queue = (((u64) hash_v4(skb) * info->queues_total) >> |
| 88 | 32) + queue; |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 89 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Jan Engelhardt | 0d34545 | 2010-03-19 18:47:51 +0100 | [diff] [blame] | 90 | else if (par->family == NFPROTO_IPV6) |
Changli Gao | ca36181 | 2010-11-12 17:34:17 +0100 | [diff] [blame] | 91 | queue = (((u64) hash_v6(skb) * info->queues_total) >> |
| 92 | 32) + queue; |
Jan Engelhardt | f76a47c | 2009-06-05 15:22:15 +0200 | [diff] [blame] | 93 | #endif |
| 94 | } |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 95 | return NF_QUEUE_NR(queue); |
| 96 | } |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 97 | |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 98 | static unsigned int |
| 99 | nfqueue_tg_v2(struct sk_buff *skb, const struct xt_action_param *par) |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 100 | { |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 101 | const struct xt_NFQ_info_v2 *info = par->targinfo; |
| 102 | unsigned int ret = nfqueue_tg_v1(skb, par); |
| 103 | |
| 104 | if (info->bypass) |
| 105 | ret |= NF_VERDICT_FLAG_QUEUE_BYPASS; |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static int nfqueue_tg_check(const struct xt_tgchk_param *par) |
| 110 | { |
| 111 | const struct xt_NFQ_info_v2 *info = par->targinfo; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 112 | u32 maxid; |
| 113 | |
Jan Engelhardt | 5191d50 | 2010-01-04 16:27:25 +0100 | [diff] [blame] | 114 | if (unlikely(!rnd_inited)) { |
| 115 | get_random_bytes(&jhash_initval, sizeof(jhash_initval)); |
| 116 | rnd_inited = true; |
| 117 | } |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 118 | if (info->queues_total == 0) { |
| 119 | pr_err("NFQUEUE: number of total queues is 0\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 120 | return -EINVAL; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 121 | } |
| 122 | maxid = info->queues_total - 1 + info->queuenum; |
| 123 | if (maxid > 0xffff) { |
| 124 | pr_err("NFQUEUE: number of queues (%u) out of range (got %u)\n", |
| 125 | info->queues_total, maxid); |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 126 | return -ERANGE; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 127 | } |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 128 | if (par->target->revision == 2 && info->bypass > 1) |
| 129 | return -EINVAL; |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 130 | return 0; |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 133 | static struct xt_target nfqueue_tg_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 134 | { |
| 135 | .name = "NFQUEUE", |
Florian Westphal | 61f5abc | 2009-06-05 13:18:07 +0200 | [diff] [blame] | 136 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 137 | .target = nfqueue_tg, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 138 | .targetsize = sizeof(struct xt_NFQ_info), |
| 139 | .me = THIS_MODULE, |
| 140 | }, |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 141 | { |
| 142 | .name = "NFQUEUE", |
| 143 | .revision = 1, |
Jan Engelhardt | f76a47c | 2009-06-05 15:22:15 +0200 | [diff] [blame] | 144 | .family = NFPROTO_UNSPEC, |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 145 | .checkentry = nfqueue_tg_check, |
Jan Engelhardt | f76a47c | 2009-06-05 15:22:15 +0200 | [diff] [blame] | 146 | .target = nfqueue_tg_v1, |
Florian Westphal | 10662aa | 2009-06-05 13:24:24 +0200 | [diff] [blame] | 147 | .targetsize = sizeof(struct xt_NFQ_info_v1), |
| 148 | .me = THIS_MODULE, |
| 149 | }, |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 150 | { |
| 151 | .name = "NFQUEUE", |
| 152 | .revision = 2, |
| 153 | .family = NFPROTO_UNSPEC, |
| 154 | .checkentry = nfqueue_tg_check, |
| 155 | .target = nfqueue_tg_v2, |
| 156 | .targetsize = sizeof(struct xt_NFQ_info_v2), |
| 157 | .me = THIS_MODULE, |
| 158 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 161 | static int __init nfqueue_tg_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 162 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 163 | return xt_register_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 166 | static void __exit nfqueue_tg_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 167 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 168 | xt_unregister_targets(nfqueue_tg_reg, ARRAY_SIZE(nfqueue_tg_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 171 | module_init(nfqueue_tg_init); |
| 172 | module_exit(nfqueue_tg_exit); |