netfilter: flowtable: refresh timeout after dst and writable checks
[linux-2.6-microblaze.git] / net / netfilter / nf_flow_table_ip.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/netfilter.h>
6 #include <linux/rhashtable.h>
7 #include <linux/ip.h>
8 #include <linux/ipv6.h>
9 #include <linux/netdevice.h>
10 #include <net/ip.h>
11 #include <net/ipv6.h>
12 #include <net/ip6_route.h>
13 #include <net/neighbour.h>
14 #include <net/netfilter/nf_flow_table.h>
15 #include <net/netfilter/nf_conntrack_acct.h>
16 /* For layer 4 checksum field offset. */
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19
20 static int nf_flow_state_check(struct flow_offload *flow, int proto,
21                                struct sk_buff *skb, unsigned int thoff)
22 {
23         struct tcphdr *tcph;
24
25         if (proto != IPPROTO_TCP)
26                 return 0;
27
28         tcph = (void *)(skb_network_header(skb) + thoff);
29         if (unlikely(tcph->fin || tcph->rst)) {
30                 flow_offload_teardown(flow);
31                 return -1;
32         }
33
34         return 0;
35 }
36
37 static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
38                                __be32 addr, __be32 new_addr)
39 {
40         struct tcphdr *tcph;
41
42         tcph = (void *)(skb_network_header(skb) + thoff);
43         inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
44 }
45
46 static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
47                                __be32 addr, __be32 new_addr)
48 {
49         struct udphdr *udph;
50
51         udph = (void *)(skb_network_header(skb) + thoff);
52         if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
53                 inet_proto_csum_replace4(&udph->check, skb, addr,
54                                          new_addr, true);
55                 if (!udph->check)
56                         udph->check = CSUM_MANGLED_0;
57         }
58 }
59
60 static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
61                                    unsigned int thoff, __be32 addr,
62                                    __be32 new_addr)
63 {
64         switch (iph->protocol) {
65         case IPPROTO_TCP:
66                 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
67                 break;
68         case IPPROTO_UDP:
69                 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
70                 break;
71         }
72 }
73
74 static void nf_flow_snat_ip(const struct flow_offload *flow,
75                             struct sk_buff *skb, struct iphdr *iph,
76                             unsigned int thoff, enum flow_offload_tuple_dir dir)
77 {
78         __be32 addr, new_addr;
79
80         switch (dir) {
81         case FLOW_OFFLOAD_DIR_ORIGINAL:
82                 addr = iph->saddr;
83                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
84                 iph->saddr = new_addr;
85                 break;
86         case FLOW_OFFLOAD_DIR_REPLY:
87                 addr = iph->daddr;
88                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
89                 iph->daddr = new_addr;
90                 break;
91         }
92         csum_replace4(&iph->check, addr, new_addr);
93
94         nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
95 }
96
97 static void nf_flow_dnat_ip(const struct flow_offload *flow,
98                             struct sk_buff *skb, struct iphdr *iph,
99                             unsigned int thoff, enum flow_offload_tuple_dir dir)
100 {
101         __be32 addr, new_addr;
102
103         switch (dir) {
104         case FLOW_OFFLOAD_DIR_ORIGINAL:
105                 addr = iph->daddr;
106                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
107                 iph->daddr = new_addr;
108                 break;
109         case FLOW_OFFLOAD_DIR_REPLY:
110                 addr = iph->saddr;
111                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
112                 iph->saddr = new_addr;
113                 break;
114         }
115         csum_replace4(&iph->check, addr, new_addr);
116
117         nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
118 }
119
120 static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
121                           unsigned int thoff, enum flow_offload_tuple_dir dir,
122                           struct iphdr *iph)
123 {
124         if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
125                 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
126                 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
127         }
128         if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
129                 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
130                 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
131         }
132 }
133
134 static bool ip_has_options(unsigned int thoff)
135 {
136         return thoff != sizeof(struct iphdr);
137 }
138
139 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
140                             struct flow_offload_tuple *tuple, u32 *hdrsize)
141 {
142         struct flow_ports *ports;
143         unsigned int thoff;
144         struct iphdr *iph;
145
146         if (!pskb_may_pull(skb, sizeof(*iph)))
147                 return -1;
148
149         iph = ip_hdr(skb);
150         thoff = iph->ihl * 4;
151
152         if (ip_is_fragment(iph) ||
153             unlikely(ip_has_options(thoff)))
154                 return -1;
155
156         switch (iph->protocol) {
157         case IPPROTO_TCP:
158                 *hdrsize = sizeof(struct tcphdr);
159                 break;
160         case IPPROTO_UDP:
161                 *hdrsize = sizeof(struct udphdr);
162                 break;
163         default:
164                 return -1;
165         }
166
167         if (iph->ttl <= 1)
168                 return -1;
169
170         thoff = iph->ihl * 4;
171         if (!pskb_may_pull(skb, thoff + *hdrsize))
172                 return -1;
173
174         iph = ip_hdr(skb);
175         ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
176
177         tuple->src_v4.s_addr    = iph->saddr;
178         tuple->dst_v4.s_addr    = iph->daddr;
179         tuple->src_port         = ports->source;
180         tuple->dst_port         = ports->dest;
181         tuple->l3proto          = AF_INET;
182         tuple->l4proto          = iph->protocol;
183         tuple->iifidx           = dev->ifindex;
184
185         return 0;
186 }
187
188 /* Based on ip_exceeds_mtu(). */
189 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
190 {
191         if (skb->len <= mtu)
192                 return false;
193
194         if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
195                 return false;
196
197         return true;
198 }
199
200 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
201                                       const struct nf_hook_state *state,
202                                       struct dst_entry *dst)
203 {
204         skb_orphan(skb);
205         skb_dst_set_noref(skb, dst);
206         dst_output(state->net, state->sk, skb);
207         return NF_STOLEN;
208 }
209
210 unsigned int
211 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
212                         const struct nf_hook_state *state)
213 {
214         struct flow_offload_tuple_rhash *tuplehash;
215         struct nf_flowtable *flow_table = priv;
216         struct flow_offload_tuple tuple = {};
217         enum flow_offload_tuple_dir dir;
218         struct flow_offload *flow;
219         struct net_device *outdev;
220         struct rtable *rt;
221         unsigned int thoff;
222         struct iphdr *iph;
223         __be32 nexthop;
224         u32 hdrsize;
225
226         if (skb->protocol != htons(ETH_P_IP))
227                 return NF_ACCEPT;
228
229         if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize) < 0)
230                 return NF_ACCEPT;
231
232         tuplehash = flow_offload_lookup(flow_table, &tuple);
233         if (tuplehash == NULL)
234                 return NF_ACCEPT;
235
236         dir = tuplehash->tuple.dir;
237         flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
238         rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
239         outdev = rt->dst.dev;
240
241         if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
242                 return NF_ACCEPT;
243
244         iph = ip_hdr(skb);
245         thoff = iph->ihl * 4;
246         if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
247                 return NF_ACCEPT;
248
249         if (!dst_check(&rt->dst, 0)) {
250                 flow_offload_teardown(flow);
251                 return NF_ACCEPT;
252         }
253
254         if (skb_try_make_writable(skb, thoff + hdrsize))
255                 return NF_DROP;
256
257         flow_offload_refresh(flow_table, flow);
258
259         iph = ip_hdr(skb);
260         nf_flow_nat_ip(flow, skb, thoff, dir, iph);
261
262         ip_decrease_ttl(iph);
263         skb->tstamp = 0;
264
265         if (flow_table->flags & NF_FLOWTABLE_COUNTER)
266                 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
267
268         if (unlikely(dst_xfrm(&rt->dst))) {
269                 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
270                 IPCB(skb)->iif = skb->dev->ifindex;
271                 IPCB(skb)->flags = IPSKB_FORWARDED;
272                 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
273         }
274
275         skb->dev = outdev;
276         nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
277         skb_dst_set_noref(skb, &rt->dst);
278         neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
279
280         return NF_STOLEN;
281 }
282 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
283
284 static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
285                                  struct in6_addr *addr,
286                                  struct in6_addr *new_addr,
287                                  struct ipv6hdr *ip6h)
288 {
289         struct tcphdr *tcph;
290
291         tcph = (void *)(skb_network_header(skb) + thoff);
292         inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
293                                   new_addr->s6_addr32, true);
294 }
295
296 static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
297                                  struct in6_addr *addr,
298                                  struct in6_addr *new_addr)
299 {
300         struct udphdr *udph;
301
302         udph = (void *)(skb_network_header(skb) + thoff);
303         if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
304                 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
305                                           new_addr->s6_addr32, true);
306                 if (!udph->check)
307                         udph->check = CSUM_MANGLED_0;
308         }
309 }
310
311 static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
312                                      unsigned int thoff, struct in6_addr *addr,
313                                      struct in6_addr *new_addr)
314 {
315         switch (ip6h->nexthdr) {
316         case IPPROTO_TCP:
317                 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
318                 break;
319         case IPPROTO_UDP:
320                 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
321                 break;
322         }
323 }
324
325 static void nf_flow_snat_ipv6(const struct flow_offload *flow,
326                               struct sk_buff *skb, struct ipv6hdr *ip6h,
327                               unsigned int thoff,
328                               enum flow_offload_tuple_dir dir)
329 {
330         struct in6_addr addr, new_addr;
331
332         switch (dir) {
333         case FLOW_OFFLOAD_DIR_ORIGINAL:
334                 addr = ip6h->saddr;
335                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
336                 ip6h->saddr = new_addr;
337                 break;
338         case FLOW_OFFLOAD_DIR_REPLY:
339                 addr = ip6h->daddr;
340                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
341                 ip6h->daddr = new_addr;
342                 break;
343         }
344
345         nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
346 }
347
348 static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
349                               struct sk_buff *skb, struct ipv6hdr *ip6h,
350                               unsigned int thoff,
351                               enum flow_offload_tuple_dir dir)
352 {
353         struct in6_addr addr, new_addr;
354
355         switch (dir) {
356         case FLOW_OFFLOAD_DIR_ORIGINAL:
357                 addr = ip6h->daddr;
358                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
359                 ip6h->daddr = new_addr;
360                 break;
361         case FLOW_OFFLOAD_DIR_REPLY:
362                 addr = ip6h->saddr;
363                 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
364                 ip6h->saddr = new_addr;
365                 break;
366         }
367
368         nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
369 }
370
371 static void nf_flow_nat_ipv6(const struct flow_offload *flow,
372                              struct sk_buff *skb,
373                              enum flow_offload_tuple_dir dir,
374                              struct ipv6hdr *ip6h)
375 {
376         unsigned int thoff = sizeof(*ip6h);
377
378         if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
379                 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
380                 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
381         }
382         if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
383                 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
384                 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
385         }
386 }
387
388 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
389                               struct flow_offload_tuple *tuple, u32 *hdrsize)
390 {
391         struct flow_ports *ports;
392         struct ipv6hdr *ip6h;
393         unsigned int thoff;
394
395         if (!pskb_may_pull(skb, sizeof(*ip6h)))
396                 return -1;
397
398         ip6h = ipv6_hdr(skb);
399
400         switch (ip6h->nexthdr) {
401         case IPPROTO_TCP:
402                 *hdrsize = sizeof(struct tcphdr);
403                 break;
404         case IPPROTO_UDP:
405                 *hdrsize = sizeof(struct udphdr);
406                 break;
407         default:
408                 return -1;
409         }
410
411         if (ip6h->hop_limit <= 1)
412                 return -1;
413
414         thoff = sizeof(*ip6h);
415         if (!pskb_may_pull(skb, thoff + *hdrsize))
416                 return -1;
417
418         ip6h = ipv6_hdr(skb);
419         ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
420
421         tuple->src_v6           = ip6h->saddr;
422         tuple->dst_v6           = ip6h->daddr;
423         tuple->src_port         = ports->source;
424         tuple->dst_port         = ports->dest;
425         tuple->l3proto          = AF_INET6;
426         tuple->l4proto          = ip6h->nexthdr;
427         tuple->iifidx           = dev->ifindex;
428
429         return 0;
430 }
431
432 unsigned int
433 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
434                           const struct nf_hook_state *state)
435 {
436         struct flow_offload_tuple_rhash *tuplehash;
437         struct nf_flowtable *flow_table = priv;
438         struct flow_offload_tuple tuple = {};
439         enum flow_offload_tuple_dir dir;
440         const struct in6_addr *nexthop;
441         struct flow_offload *flow;
442         struct net_device *outdev;
443         struct ipv6hdr *ip6h;
444         struct rt6_info *rt;
445         u32 hdrsize;
446
447         if (skb->protocol != htons(ETH_P_IPV6))
448                 return NF_ACCEPT;
449
450         if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize) < 0)
451                 return NF_ACCEPT;
452
453         tuplehash = flow_offload_lookup(flow_table, &tuple);
454         if (tuplehash == NULL)
455                 return NF_ACCEPT;
456
457         dir = tuplehash->tuple.dir;
458         flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
459         rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
460         outdev = rt->dst.dev;
461
462         if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
463                 return NF_ACCEPT;
464
465         if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
466                                 sizeof(*ip6h)))
467                 return NF_ACCEPT;
468
469         if (!dst_check(&rt->dst, 0)) {
470                 flow_offload_teardown(flow);
471                 return NF_ACCEPT;
472         }
473
474         if (skb_try_make_writable(skb, sizeof(*ip6h) + hdrsize))
475                 return NF_DROP;
476
477         flow_offload_refresh(flow_table, flow);
478
479         ip6h = ipv6_hdr(skb);
480         nf_flow_nat_ipv6(flow, skb, dir, ip6h);
481
482         ip6h->hop_limit--;
483         skb->tstamp = 0;
484
485         if (flow_table->flags & NF_FLOWTABLE_COUNTER)
486                 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
487
488         if (unlikely(dst_xfrm(&rt->dst))) {
489                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
490                 IP6CB(skb)->iif = skb->dev->ifindex;
491                 IP6CB(skb)->flags = IP6SKB_FORWARDED;
492                 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
493         }
494
495         skb->dev = outdev;
496         nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
497         skb_dst_set_noref(skb, &rt->dst);
498         neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
499
500         return NF_STOLEN;
501 }
502 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);