Revert "brcmfmac: move configuration of probe request IEs"
[platform/kernel/linux-rpi.git] / net / netfilter / nf_flow_table_core.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/netdevice.h>
8 #include <net/ip.h>
9 #include <net/ip6_route.h>
10 #include <net/netfilter/nf_tables.h>
11 #include <net/netfilter/nf_flow_table.h>
12 #include <net/netfilter/nf_conntrack.h>
13 #include <net/netfilter/nf_conntrack_core.h>
14 #include <net/netfilter/nf_conntrack_l4proto.h>
15 #include <net/netfilter/nf_conntrack_tuple.h>
16
17 static DEFINE_MUTEX(flowtable_lock);
18 static LIST_HEAD(flowtables);
19
20 static void
21 flow_offload_fill_dir(struct flow_offload *flow,
22                       enum flow_offload_tuple_dir dir)
23 {
24         struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
25         struct nf_conntrack_tuple *ctt = &flow->ct->tuplehash[dir].tuple;
26
27         ft->dir = dir;
28
29         switch (ctt->src.l3num) {
30         case NFPROTO_IPV4:
31                 ft->src_v4 = ctt->src.u3.in;
32                 ft->dst_v4 = ctt->dst.u3.in;
33                 break;
34         case NFPROTO_IPV6:
35                 ft->src_v6 = ctt->src.u3.in6;
36                 ft->dst_v6 = ctt->dst.u3.in6;
37                 break;
38         }
39
40         ft->l3proto = ctt->src.l3num;
41         ft->l4proto = ctt->dst.protonum;
42         ft->src_port = ctt->src.u.tcp.port;
43         ft->dst_port = ctt->dst.u.tcp.port;
44 }
45
46 struct flow_offload *flow_offload_alloc(struct nf_conn *ct)
47 {
48         struct flow_offload *flow;
49
50         if (unlikely(nf_ct_is_dying(ct) ||
51             !refcount_inc_not_zero(&ct->ct_general.use)))
52                 return NULL;
53
54         flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
55         if (!flow)
56                 goto err_ct_refcnt;
57
58         flow->ct = ct;
59
60         flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
61         flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_REPLY);
62
63         if (ct->status & IPS_SRC_NAT)
64                 __set_bit(NF_FLOW_SNAT, &flow->flags);
65         if (ct->status & IPS_DST_NAT)
66                 __set_bit(NF_FLOW_DNAT, &flow->flags);
67
68         return flow;
69
70 err_ct_refcnt:
71         nf_ct_put(ct);
72
73         return NULL;
74 }
75 EXPORT_SYMBOL_GPL(flow_offload_alloc);
76
77 static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
78 {
79         const struct rt6_info *rt;
80
81         if (flow_tuple->l3proto == NFPROTO_IPV6) {
82                 rt = (const struct rt6_info *)flow_tuple->dst_cache;
83                 return rt6_get_cookie(rt);
84         }
85
86         return 0;
87 }
88
89 static int flow_offload_fill_route(struct flow_offload *flow,
90                                    const struct nf_flow_route *route,
91                                    enum flow_offload_tuple_dir dir)
92 {
93         struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
94         struct dst_entry *dst = route->tuple[dir].dst;
95         int i, j = 0;
96
97         switch (flow_tuple->l3proto) {
98         case NFPROTO_IPV4:
99                 flow_tuple->mtu = ip_dst_mtu_maybe_forward(dst, true);
100                 break;
101         case NFPROTO_IPV6:
102                 flow_tuple->mtu = ip6_dst_mtu_maybe_forward(dst, true);
103                 break;
104         }
105
106         flow_tuple->iifidx = route->tuple[dir].in.ifindex;
107         for (i = route->tuple[dir].in.num_encaps - 1; i >= 0; i--) {
108                 flow_tuple->encap[j].id = route->tuple[dir].in.encap[i].id;
109                 flow_tuple->encap[j].proto = route->tuple[dir].in.encap[i].proto;
110                 if (route->tuple[dir].in.ingress_vlans & BIT(i))
111                         flow_tuple->in_vlan_ingress |= BIT(j);
112                 j++;
113         }
114         flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
115
116         switch (route->tuple[dir].xmit_type) {
117         case FLOW_OFFLOAD_XMIT_DIRECT:
118                 memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,
119                        ETH_ALEN);
120                 memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
121                        ETH_ALEN);
122                 flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
123                 flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
124                 break;
125         case FLOW_OFFLOAD_XMIT_XFRM:
126         case FLOW_OFFLOAD_XMIT_NEIGH:
127                 if (!dst_hold_safe(route->tuple[dir].dst))
128                         return -1;
129
130                 flow_tuple->dst_cache = dst;
131                 flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
132                 break;
133         default:
134                 WARN_ON_ONCE(1);
135                 break;
136         }
137         flow_tuple->xmit_type = route->tuple[dir].xmit_type;
138
139         return 0;
140 }
141
142 static void nft_flow_dst_release(struct flow_offload *flow,
143                                  enum flow_offload_tuple_dir dir)
144 {
145         if (flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
146             flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)
147                 dst_release(flow->tuplehash[dir].tuple.dst_cache);
148 }
149
150 int flow_offload_route_init(struct flow_offload *flow,
151                             const struct nf_flow_route *route)
152 {
153         int err;
154
155         err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
156         if (err < 0)
157                 return err;
158
159         err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
160         if (err < 0)
161                 goto err_route_reply;
162
163         flow->type = NF_FLOW_OFFLOAD_ROUTE;
164
165         return 0;
166
167 err_route_reply:
168         nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
169
170         return err;
171 }
172 EXPORT_SYMBOL_GPL(flow_offload_route_init);
173
174 static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
175 {
176         tcp->seen[0].td_maxwin = 0;
177         tcp->seen[1].td_maxwin = 0;
178 }
179
180 static void flow_offload_fixup_ct(struct nf_conn *ct)
181 {
182         struct net *net = nf_ct_net(ct);
183         int l4num = nf_ct_protonum(ct);
184         s32 timeout;
185
186         if (l4num == IPPROTO_TCP) {
187                 struct nf_tcp_net *tn = nf_tcp_pernet(net);
188
189                 flow_offload_fixup_tcp(&ct->proto.tcp);
190
191                 timeout = tn->timeouts[ct->proto.tcp.state];
192                 timeout -= tn->offload_timeout;
193         } else if (l4num == IPPROTO_UDP) {
194                 struct nf_udp_net *tn = nf_udp_pernet(net);
195
196                 timeout = tn->timeouts[UDP_CT_REPLIED];
197                 timeout -= tn->offload_timeout;
198         } else {
199                 return;
200         }
201
202         if (timeout < 0)
203                 timeout = 0;
204
205         if (nf_flow_timeout_delta(READ_ONCE(ct->timeout)) > (__s32)timeout)
206                 WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);
207 }
208
209 static void flow_offload_route_release(struct flow_offload *flow)
210 {
211         nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
212         nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
213 }
214
215 void flow_offload_free(struct flow_offload *flow)
216 {
217         switch (flow->type) {
218         case NF_FLOW_OFFLOAD_ROUTE:
219                 flow_offload_route_release(flow);
220                 break;
221         default:
222                 break;
223         }
224         nf_ct_put(flow->ct);
225         kfree_rcu(flow, rcu_head);
226 }
227 EXPORT_SYMBOL_GPL(flow_offload_free);
228
229 static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
230 {
231         const struct flow_offload_tuple *tuple = data;
232
233         return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
234 }
235
236 static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
237 {
238         const struct flow_offload_tuple_rhash *tuplehash = data;
239
240         return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
241 }
242
243 static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
244                                         const void *ptr)
245 {
246         const struct flow_offload_tuple *tuple = arg->key;
247         const struct flow_offload_tuple_rhash *x = ptr;
248
249         if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
250                 return 1;
251
252         return 0;
253 }
254
255 static const struct rhashtable_params nf_flow_offload_rhash_params = {
256         .head_offset            = offsetof(struct flow_offload_tuple_rhash, node),
257         .hashfn                 = flow_offload_hash,
258         .obj_hashfn             = flow_offload_hash_obj,
259         .obj_cmpfn              = flow_offload_hash_cmp,
260         .automatic_shrinking    = true,
261 };
262
263 unsigned long flow_offload_get_timeout(struct flow_offload *flow)
264 {
265         unsigned long timeout = NF_FLOW_TIMEOUT;
266         struct net *net = nf_ct_net(flow->ct);
267         int l4num = nf_ct_protonum(flow->ct);
268
269         if (l4num == IPPROTO_TCP) {
270                 struct nf_tcp_net *tn = nf_tcp_pernet(net);
271
272                 timeout = tn->offload_timeout;
273         } else if (l4num == IPPROTO_UDP) {
274                 struct nf_udp_net *tn = nf_udp_pernet(net);
275
276                 timeout = tn->offload_timeout;
277         }
278
279         return timeout;
280 }
281
282 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
283 {
284         int err;
285
286         flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
287
288         err = rhashtable_insert_fast(&flow_table->rhashtable,
289                                      &flow->tuplehash[0].node,
290                                      nf_flow_offload_rhash_params);
291         if (err < 0)
292                 return err;
293
294         err = rhashtable_insert_fast(&flow_table->rhashtable,
295                                      &flow->tuplehash[1].node,
296                                      nf_flow_offload_rhash_params);
297         if (err < 0) {
298                 rhashtable_remove_fast(&flow_table->rhashtable,
299                                        &flow->tuplehash[0].node,
300                                        nf_flow_offload_rhash_params);
301                 return err;
302         }
303
304         nf_ct_offload_timeout(flow->ct);
305
306         if (nf_flowtable_hw_offload(flow_table)) {
307                 __set_bit(NF_FLOW_HW, &flow->flags);
308                 nf_flow_offload_add(flow_table, flow);
309         }
310
311         return 0;
312 }
313 EXPORT_SYMBOL_GPL(flow_offload_add);
314
315 void flow_offload_refresh(struct nf_flowtable *flow_table,
316                           struct flow_offload *flow)
317 {
318         u32 timeout;
319
320         timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
321         if (timeout - READ_ONCE(flow->timeout) > HZ)
322                 WRITE_ONCE(flow->timeout, timeout);
323         else
324                 return;
325
326         if (likely(!nf_flowtable_hw_offload(flow_table)))
327                 return;
328
329         nf_flow_offload_add(flow_table, flow);
330 }
331 EXPORT_SYMBOL_GPL(flow_offload_refresh);
332
333 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
334 {
335         return nf_flow_timeout_delta(flow->timeout) <= 0;
336 }
337
338 static void flow_offload_del(struct nf_flowtable *flow_table,
339                              struct flow_offload *flow)
340 {
341         rhashtable_remove_fast(&flow_table->rhashtable,
342                                &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
343                                nf_flow_offload_rhash_params);
344         rhashtable_remove_fast(&flow_table->rhashtable,
345                                &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
346                                nf_flow_offload_rhash_params);
347         flow_offload_free(flow);
348 }
349
350 void flow_offload_teardown(struct flow_offload *flow)
351 {
352         clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
353         set_bit(NF_FLOW_TEARDOWN, &flow->flags);
354         flow_offload_fixup_ct(flow->ct);
355 }
356 EXPORT_SYMBOL_GPL(flow_offload_teardown);
357
358 struct flow_offload_tuple_rhash *
359 flow_offload_lookup(struct nf_flowtable *flow_table,
360                     struct flow_offload_tuple *tuple)
361 {
362         struct flow_offload_tuple_rhash *tuplehash;
363         struct flow_offload *flow;
364         int dir;
365
366         tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
367                                       nf_flow_offload_rhash_params);
368         if (!tuplehash)
369                 return NULL;
370
371         dir = tuplehash->tuple.dir;
372         flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
373         if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
374                 return NULL;
375
376         if (unlikely(nf_ct_is_dying(flow->ct)))
377                 return NULL;
378
379         return tuplehash;
380 }
381 EXPORT_SYMBOL_GPL(flow_offload_lookup);
382
383 static int
384 nf_flow_table_iterate(struct nf_flowtable *flow_table,
385                       void (*iter)(struct nf_flowtable *flowtable,
386                                    struct flow_offload *flow, void *data),
387                       void *data)
388 {
389         struct flow_offload_tuple_rhash *tuplehash;
390         struct rhashtable_iter hti;
391         struct flow_offload *flow;
392         int err = 0;
393
394         rhashtable_walk_enter(&flow_table->rhashtable, &hti);
395         rhashtable_walk_start(&hti);
396
397         while ((tuplehash = rhashtable_walk_next(&hti))) {
398                 if (IS_ERR(tuplehash)) {
399                         if (PTR_ERR(tuplehash) != -EAGAIN) {
400                                 err = PTR_ERR(tuplehash);
401                                 break;
402                         }
403                         continue;
404                 }
405                 if (tuplehash->tuple.dir)
406                         continue;
407
408                 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
409
410                 iter(flow_table, flow, data);
411         }
412         rhashtable_walk_stop(&hti);
413         rhashtable_walk_exit(&hti);
414
415         return err;
416 }
417
418 static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,
419                                     struct flow_offload *flow, void *data)
420 {
421         if (nf_flow_has_expired(flow) ||
422             nf_ct_is_dying(flow->ct))
423                 flow_offload_teardown(flow);
424
425         if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
426                 if (test_bit(NF_FLOW_HW, &flow->flags)) {
427                         if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
428                                 nf_flow_offload_del(flow_table, flow);
429                         else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
430                                 flow_offload_del(flow_table, flow);
431                 } else {
432                         flow_offload_del(flow_table, flow);
433                 }
434         } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
435                 nf_flow_offload_stats(flow_table, flow);
436         }
437 }
438
439 void nf_flow_table_gc_run(struct nf_flowtable *flow_table)
440 {
441         nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, NULL);
442 }
443
444 static void nf_flow_offload_work_gc(struct work_struct *work)
445 {
446         struct nf_flowtable *flow_table;
447
448         flow_table = container_of(work, struct nf_flowtable, gc_work.work);
449         nf_flow_table_gc_run(flow_table);
450         queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
451 }
452
453 static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
454                                  __be16 port, __be16 new_port)
455 {
456         struct tcphdr *tcph;
457
458         tcph = (void *)(skb_network_header(skb) + thoff);
459         inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
460 }
461
462 static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
463                                  __be16 port, __be16 new_port)
464 {
465         struct udphdr *udph;
466
467         udph = (void *)(skb_network_header(skb) + thoff);
468         if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
469                 inet_proto_csum_replace2(&udph->check, skb, port,
470                                          new_port, false);
471                 if (!udph->check)
472                         udph->check = CSUM_MANGLED_0;
473         }
474 }
475
476 static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
477                              u8 protocol, __be16 port, __be16 new_port)
478 {
479         switch (protocol) {
480         case IPPROTO_TCP:
481                 nf_flow_nat_port_tcp(skb, thoff, port, new_port);
482                 break;
483         case IPPROTO_UDP:
484                 nf_flow_nat_port_udp(skb, thoff, port, new_port);
485                 break;
486         }
487 }
488
489 void nf_flow_snat_port(const struct flow_offload *flow,
490                        struct sk_buff *skb, unsigned int thoff,
491                        u8 protocol, enum flow_offload_tuple_dir dir)
492 {
493         struct flow_ports *hdr;
494         __be16 port, new_port;
495
496         hdr = (void *)(skb_network_header(skb) + thoff);
497
498         switch (dir) {
499         case FLOW_OFFLOAD_DIR_ORIGINAL:
500                 port = hdr->source;
501                 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
502                 hdr->source = new_port;
503                 break;
504         case FLOW_OFFLOAD_DIR_REPLY:
505                 port = hdr->dest;
506                 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
507                 hdr->dest = new_port;
508                 break;
509         }
510
511         nf_flow_nat_port(skb, thoff, protocol, port, new_port);
512 }
513 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
514
515 void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
516                        unsigned int thoff, u8 protocol,
517                        enum flow_offload_tuple_dir dir)
518 {
519         struct flow_ports *hdr;
520         __be16 port, new_port;
521
522         hdr = (void *)(skb_network_header(skb) + thoff);
523
524         switch (dir) {
525         case FLOW_OFFLOAD_DIR_ORIGINAL:
526                 port = hdr->dest;
527                 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
528                 hdr->dest = new_port;
529                 break;
530         case FLOW_OFFLOAD_DIR_REPLY:
531                 port = hdr->source;
532                 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
533                 hdr->source = new_port;
534                 break;
535         }
536
537         nf_flow_nat_port(skb, thoff, protocol, port, new_port);
538 }
539 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
540
541 int nf_flow_table_init(struct nf_flowtable *flowtable)
542 {
543         int err;
544
545         INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
546         flow_block_init(&flowtable->flow_block);
547         init_rwsem(&flowtable->flow_block_lock);
548
549         err = rhashtable_init(&flowtable->rhashtable,
550                               &nf_flow_offload_rhash_params);
551         if (err < 0)
552                 return err;
553
554         queue_delayed_work(system_power_efficient_wq,
555                            &flowtable->gc_work, HZ);
556
557         mutex_lock(&flowtable_lock);
558         list_add(&flowtable->list, &flowtables);
559         mutex_unlock(&flowtable_lock);
560
561         return 0;
562 }
563 EXPORT_SYMBOL_GPL(nf_flow_table_init);
564
565 static void nf_flow_table_do_cleanup(struct nf_flowtable *flow_table,
566                                      struct flow_offload *flow, void *data)
567 {
568         struct net_device *dev = data;
569
570         if (!dev) {
571                 flow_offload_teardown(flow);
572                 return;
573         }
574
575         if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
576             (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
577              flow->tuplehash[1].tuple.iifidx == dev->ifindex))
578                 flow_offload_teardown(flow);
579 }
580
581 void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
582                               struct net_device *dev)
583 {
584         nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
585         flush_delayed_work(&flowtable->gc_work);
586         nf_flow_table_offload_flush(flowtable);
587 }
588
589 void nf_flow_table_cleanup(struct net_device *dev)
590 {
591         struct nf_flowtable *flowtable;
592
593         mutex_lock(&flowtable_lock);
594         list_for_each_entry(flowtable, &flowtables, list)
595                 nf_flow_table_gc_cleanup(flowtable, dev);
596         mutex_unlock(&flowtable_lock);
597 }
598 EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
599
600 void nf_flow_table_free(struct nf_flowtable *flow_table)
601 {
602         mutex_lock(&flowtable_lock);
603         list_del(&flow_table->list);
604         mutex_unlock(&flowtable_lock);
605
606         cancel_delayed_work_sync(&flow_table->gc_work);
607         nf_flow_table_offload_flush(flow_table);
608         /* ... no more pending work after this stage ... */
609         nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
610         nf_flow_table_gc_run(flow_table);
611         nf_flow_table_offload_flush_cleanup(flow_table);
612         rhashtable_destroy(&flow_table->rhashtable);
613 }
614 EXPORT_SYMBOL_GPL(nf_flow_table_free);
615
616 static int __init nf_flow_table_module_init(void)
617 {
618         return nf_flow_table_offload_init();
619 }
620
621 static void __exit nf_flow_table_module_exit(void)
622 {
623         nf_flow_table_offload_exit();
624 }
625
626 module_init(nf_flow_table_module_init);
627 module_exit(nf_flow_table_module_exit);
628
629 MODULE_LICENSE("GPL");
630 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
631 MODULE_DESCRIPTION("Netfilter flow table module");