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