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