1 From: Pablo Neira Ayuso <pablo@netfilter.org>
2 Date: Sun, 7 Jan 2018 01:04:19 +0100
3 Subject: [PATCH] netfilter: flow table support for IPv6
5 This patch adds the IPv6 flow table type, that implements the datapath
6 flow table to forward IPv6 traffic.
8 This patch exports ip6_dst_mtu_forward() that is required to check for
9 mtu to pass up packets that need PMTUD handling to the classic
12 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
14 create mode 100644 net/ipv6/netfilter/nf_flow_table_ipv6.c
16 --- a/include/net/ipv6.h
17 +++ b/include/net/ipv6.h
18 @@ -913,6 +913,8 @@ static inline struct sk_buff *ip6_finish
22 +unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst);
24 int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
26 struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6,
27 --- a/net/ipv6/ip6_output.c
28 +++ b/net/ipv6/ip6_output.c
29 @@ -370,7 +370,7 @@ static inline int ip6_forward_finish(str
30 return dst_output(net, sk, skb);
33 -static unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
34 +unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
37 struct inet6_dev *idev;
38 @@ -390,6 +390,7 @@ static unsigned int ip6_dst_mtu_forward(
42 +EXPORT_SYMBOL_GPL(ip6_dst_mtu_forward);
44 static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
46 --- a/net/ipv6/netfilter/Kconfig
47 +++ b/net/ipv6/netfilter/Kconfig
48 @@ -71,6 +71,14 @@ config NFT_FIB_IPV6
49 endif # NF_TABLES_IPV6
52 +config NF_FLOW_TABLE_IPV6
53 + select NF_FLOW_TABLE
54 + tristate "Netfilter flow table IPv6 module"
56 + This option adds the flow table IPv6 support.
58 + To compile it as a module, choose M here.
61 tristate "Netfilter IPv6 packet duplication to alternate destination"
62 depends on !NF_CONNTRACK || NF_CONNTRACK
63 --- a/net/ipv6/netfilter/Makefile
64 +++ b/net/ipv6/netfilter/Makefile
65 @@ -45,6 +45,9 @@ obj-$(CONFIG_NFT_REDIR_IPV6) += nft_redi
66 obj-$(CONFIG_NFT_DUP_IPV6) += nft_dup_ipv6.o
67 obj-$(CONFIG_NFT_FIB_IPV6) += nft_fib_ipv6.o
70 +obj-$(CONFIG_NF_FLOW_TABLE_IPV6) += nf_flow_table_ipv6.o
73 obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
74 obj-$(CONFIG_IP6_NF_MATCH_EUI64) += ip6t_eui64.o
76 +++ b/net/ipv6/netfilter/nf_flow_table_ipv6.c
78 +#include <linux/kernel.h>
79 +#include <linux/init.h>
80 +#include <linux/module.h>
81 +#include <linux/netfilter.h>
82 +#include <linux/rhashtable.h>
83 +#include <linux/ipv6.h>
84 +#include <linux/netdevice.h>
85 +#include <linux/ipv6.h>
86 +#include <net/ipv6.h>
87 +#include <net/ip6_route.h>
88 +#include <net/neighbour.h>
89 +#include <net/netfilter/nf_flow_table.h>
90 +#include <net/netfilter/nf_tables.h>
91 +/* For layer 4 checksum field offset. */
92 +#include <linux/tcp.h>
93 +#include <linux/udp.h>
95 +static int nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
96 + struct in6_addr *addr,
97 + struct in6_addr *new_addr)
99 + struct tcphdr *tcph;
101 + if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
102 + skb_try_make_writable(skb, thoff + sizeof(*tcph)))
105 + tcph = (void *)(skb_network_header(skb) + thoff);
106 + inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
107 + new_addr->s6_addr32, true);
112 +static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
113 + struct in6_addr *addr,
114 + struct in6_addr *new_addr)
116 + struct udphdr *udph;
118 + if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
119 + skb_try_make_writable(skb, thoff + sizeof(*udph)))
122 + udph = (void *)(skb_network_header(skb) + thoff);
123 + if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
124 + inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
125 + new_addr->s6_addr32, true);
127 + udph->check = CSUM_MANGLED_0;
133 +static int nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
134 + unsigned int thoff, struct in6_addr *addr,
135 + struct in6_addr *new_addr)
137 + switch (ip6h->nexthdr) {
139 + if (nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr) < 0)
143 + if (nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr) < 0)
151 +static int nf_flow_snat_ipv6(const struct flow_offload *flow,
152 + struct sk_buff *skb, struct ipv6hdr *ip6h,
153 + unsigned int thoff,
154 + enum flow_offload_tuple_dir dir)
156 + struct in6_addr addr, new_addr;
159 + case FLOW_OFFLOAD_DIR_ORIGINAL:
160 + addr = ip6h->saddr;
161 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
162 + ip6h->saddr = new_addr;
164 + case FLOW_OFFLOAD_DIR_REPLY:
165 + addr = ip6h->daddr;
166 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
167 + ip6h->daddr = new_addr;
173 + return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
176 +static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
177 + struct sk_buff *skb, struct ipv6hdr *ip6h,
178 + unsigned int thoff,
179 + enum flow_offload_tuple_dir dir)
181 + struct in6_addr addr, new_addr;
184 + case FLOW_OFFLOAD_DIR_ORIGINAL:
185 + addr = ip6h->daddr;
186 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
187 + ip6h->daddr = new_addr;
189 + case FLOW_OFFLOAD_DIR_REPLY:
190 + addr = ip6h->saddr;
191 + new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
192 + ip6h->saddr = new_addr;
198 + return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
201 +static int nf_flow_nat_ipv6(const struct flow_offload *flow,
202 + struct sk_buff *skb,
203 + enum flow_offload_tuple_dir dir)
205 + struct ipv6hdr *ip6h = ipv6_hdr(skb);
206 + unsigned int thoff = sizeof(*ip6h);
208 + if (flow->flags & FLOW_OFFLOAD_SNAT &&
209 + (nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
210 + nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
212 + if (flow->flags & FLOW_OFFLOAD_DNAT &&
213 + (nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
214 + nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
220 +static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
221 + struct flow_offload_tuple *tuple)
223 + struct flow_ports *ports;
224 + struct ipv6hdr *ip6h;
225 + unsigned int thoff;
227 + if (!pskb_may_pull(skb, sizeof(*ip6h)))
230 + ip6h = ipv6_hdr(skb);
232 + if (ip6h->nexthdr != IPPROTO_TCP &&
233 + ip6h->nexthdr != IPPROTO_UDP)
236 + thoff = sizeof(*ip6h);
237 + if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
240 + ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
242 + tuple->src_v6 = ip6h->saddr;
243 + tuple->dst_v6 = ip6h->daddr;
244 + tuple->src_port = ports->source;
245 + tuple->dst_port = ports->dest;
246 + tuple->l3proto = AF_INET6;
247 + tuple->l4proto = ip6h->nexthdr;
248 + tuple->iifidx = dev->ifindex;
253 +/* Based on ip_exceeds_mtu(). */
254 +static bool __nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
256 + if (skb->len <= mtu)
259 + if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
265 +static bool nf_flow_exceeds_mtu(struct sk_buff *skb, const struct rt6_info *rt)
269 + mtu = ip6_dst_mtu_forward(&rt->dst);
270 + if (__nf_flow_exceeds_mtu(skb, mtu))
277 +nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
278 + const struct nf_hook_state *state)
280 + struct flow_offload_tuple_rhash *tuplehash;
281 + struct nf_flowtable *flow_table = priv;
282 + struct flow_offload_tuple tuple = {};
283 + enum flow_offload_tuple_dir dir;
284 + struct flow_offload *flow;
285 + struct net_device *outdev;
286 + struct in6_addr *nexthop;
287 + struct ipv6hdr *ip6h;
288 + struct rt6_info *rt;
290 + if (skb->protocol != htons(ETH_P_IPV6))
293 + if (nf_flow_tuple_ipv6(skb, state->in, &tuple) < 0)
296 + tuplehash = flow_offload_lookup(flow_table, &tuple);
297 + if (tuplehash == NULL)
300 + outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
304 + dir = tuplehash->tuple.dir;
305 + flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
307 + rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
308 + if (unlikely(nf_flow_exceeds_mtu(skb, rt)))
311 + if (skb_try_make_writable(skb, sizeof(*ip6h)))
314 + if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
315 + nf_flow_nat_ipv6(flow, skb, dir) < 0)
318 + flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
319 + ip6h = ipv6_hdr(skb);
323 + nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
324 + neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
329 +static struct nf_flowtable_type flowtable_ipv6 = {
330 + .family = NFPROTO_IPV6,
331 + .params = &nf_flow_offload_rhash_params,
332 + .gc = nf_flow_offload_work_gc,
333 + .hook = nf_flow_offload_ipv6_hook,
334 + .owner = THIS_MODULE,
337 +static int __init nf_flow_ipv6_module_init(void)
339 + nft_register_flowtable_type(&flowtable_ipv6);
344 +static void __exit nf_flow_ipv6_module_exit(void)
346 + nft_unregister_flowtable_type(&flowtable_ipv6);
349 +module_init(nf_flow_ipv6_module_init);
350 +module_exit(nf_flow_ipv6_module_exit);
352 +MODULE_LICENSE("GPL");
353 +MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
354 +MODULE_ALIAS_NF_FLOWTABLE(AF_INET6);