1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/netfilter.h>
5 #include <linux/rhashtable.h>
7 #include <linux/netdevice.h>
9 #include <net/neighbour.h>
10 #include <net/netfilter/nf_flow_table.h>
11 #include <net/netfilter/nf_tables.h>
12 /* For layer 4 checksum field offset. */
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
16 static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
17 __be32 addr, __be32 new_addr)
21 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
22 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
25 tcph = (void *)(skb_network_header(skb) + thoff);
26 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
31 static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
32 __be32 addr, __be32 new_addr)
36 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
37 skb_try_make_writable(skb, thoff + sizeof(*udph)))
40 udph = (void *)(skb_network_header(skb) + thoff);
41 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
42 inet_proto_csum_replace4(&udph->check, skb, addr,
45 udph->check = CSUM_MANGLED_0;
51 static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
52 unsigned int thoff, __be32 addr,
55 switch (iph->protocol) {
57 if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
61 if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
69 static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
70 struct iphdr *iph, unsigned int thoff,
71 enum flow_offload_tuple_dir dir)
73 __be32 addr, new_addr;
76 case FLOW_OFFLOAD_DIR_ORIGINAL:
78 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
79 iph->saddr = new_addr;
81 case FLOW_OFFLOAD_DIR_REPLY:
83 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
84 iph->daddr = new_addr;
89 csum_replace4(&iph->check, addr, new_addr);
91 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
94 static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
95 struct iphdr *iph, unsigned int thoff,
96 enum flow_offload_tuple_dir dir)
98 __be32 addr, new_addr;
101 case FLOW_OFFLOAD_DIR_ORIGINAL:
103 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
104 iph->daddr = new_addr;
106 case FLOW_OFFLOAD_DIR_REPLY:
108 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
109 iph->saddr = new_addr;
115 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
118 static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
119 enum flow_offload_tuple_dir dir)
121 struct iphdr *iph = ip_hdr(skb);
122 unsigned int thoff = iph->ihl * 4;
124 if (flow->flags & FLOW_OFFLOAD_SNAT &&
125 (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
126 nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
128 if (flow->flags & FLOW_OFFLOAD_DNAT &&
129 (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
130 nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
136 static bool ip_has_options(unsigned int thoff)
138 return thoff != sizeof(struct iphdr);
141 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
142 struct flow_offload_tuple *tuple)
144 struct flow_ports *ports;
148 if (!pskb_may_pull(skb, sizeof(*iph)))
152 thoff = iph->ihl * 4;
154 if (ip_is_fragment(iph) ||
155 unlikely(ip_has_options(thoff)))
158 if (iph->protocol != IPPROTO_TCP &&
159 iph->protocol != IPPROTO_UDP)
162 thoff = iph->ihl * 4;
163 if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
166 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
168 tuple->src_v4.s_addr = iph->saddr;
169 tuple->dst_v4.s_addr = iph->daddr;
170 tuple->src_port = ports->source;
171 tuple->dst_port = ports->dest;
172 tuple->l3proto = AF_INET;
173 tuple->l4proto = iph->protocol;
174 tuple->iifidx = dev->ifindex;
179 /* Based on ip_exceeds_mtu(). */
180 static bool __nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
185 if ((ip_hdr(skb)->frag_off & htons(IP_DF)) == 0)
188 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
194 static bool nf_flow_exceeds_mtu(struct sk_buff *skb, const struct rtable *rt)
198 mtu = ip_dst_mtu_maybe_forward(&rt->dst, true);
199 if (__nf_flow_exceeds_mtu(skb, mtu))
206 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
207 const struct nf_hook_state *state)
209 struct flow_offload_tuple_rhash *tuplehash;
210 struct nf_flowtable *flow_table = priv;
211 struct flow_offload_tuple tuple = {};
212 enum flow_offload_tuple_dir dir;
213 struct flow_offload *flow;
214 struct net_device *outdev;
215 const struct rtable *rt;
219 if (skb->protocol != htons(ETH_P_IP))
222 if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
225 tuplehash = flow_offload_lookup(flow_table, &tuple);
226 if (tuplehash == NULL)
229 outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
233 dir = tuplehash->tuple.dir;
234 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
236 rt = (const struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
237 if (unlikely(nf_flow_exceeds_mtu(skb, rt)))
240 if (skb_try_make_writable(skb, sizeof(*iph)))
243 if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
244 nf_flow_nat_ip(flow, skb, dir) < 0)
247 flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
249 ip_decrease_ttl(iph);
252 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
253 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
257 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
259 static struct nf_flowtable_type flowtable_ipv4 = {
260 .family = NFPROTO_IPV4,
261 .params = &nf_flow_offload_rhash_params,
262 .gc = nf_flow_offload_work_gc,
263 .free = nf_flow_table_free,
264 .hook = nf_flow_offload_ip_hook,
265 .owner = THIS_MODULE,
268 static int __init nf_flow_ipv4_module_init(void)
270 nft_register_flowtable_type(&flowtable_ipv4);
275 static void __exit nf_flow_ipv4_module_exit(void)
277 nft_unregister_flowtable_type(&flowtable_ipv4);
280 module_init(nf_flow_ipv4_module_init);
281 module_exit(nf_flow_ipv4_module_exit);
283 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
285 MODULE_ALIAS_NF_FLOWTABLE(AF_INET);