Merge branch 'for-5.4/apple' into for-linus
[linux-2.6-block.git] / net / netfilter / nf_flow_table_core.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
ac2a6666
PNA
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>
4f3780c0
FF
8#include <net/ip.h>
9#include <net/ip6_route.h>
c0ea1bcb 10#include <net/netfilter/nf_tables.h>
ac2a6666
PNA
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_tuple.h>
15
16struct flow_offload_entry {
17 struct flow_offload flow;
18 struct nf_conn *ct;
19 struct rcu_head rcu_head;
20};
21
84453a90
FF
22static DEFINE_MUTEX(flowtable_lock);
23static LIST_HEAD(flowtables);
24
047b300e
FF
25static void
26flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
27 struct nf_flow_route *route,
28 enum flow_offload_tuple_dir dir)
29{
30 struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
31 struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
10f4e765 32 struct dst_entry *other_dst = route->tuple[!dir].dst;
4f3780c0 33 struct dst_entry *dst = route->tuple[dir].dst;
047b300e
FF
34
35 ft->dir = dir;
36
37 switch (ctt->src.l3num) {
38 case NFPROTO_IPV4:
39 ft->src_v4 = ctt->src.u3.in;
40 ft->dst_v4 = ctt->dst.u3.in;
4f3780c0 41 ft->mtu = ip_dst_mtu_maybe_forward(dst, true);
047b300e
FF
42 break;
43 case NFPROTO_IPV6:
44 ft->src_v6 = ctt->src.u3.in6;
45 ft->dst_v6 = ctt->dst.u3.in6;
4f3780c0 46 ft->mtu = ip6_dst_mtu_forward(dst);
047b300e
FF
47 break;
48 }
49
50 ft->l3proto = ctt->src.l3num;
51 ft->l4proto = ctt->dst.protonum;
52 ft->src_port = ctt->src.u.tcp.port;
53 ft->dst_port = ctt->dst.u.tcp.port;
54
10f4e765 55 ft->iifidx = other_dst->dev->ifindex;
4f3780c0 56 ft->dst_cache = dst;
047b300e
FF
57}
58
ac2a6666
PNA
59struct flow_offload *
60flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
61{
62 struct flow_offload_entry *entry;
63 struct flow_offload *flow;
64
65 if (unlikely(nf_ct_is_dying(ct) ||
66 !atomic_inc_not_zero(&ct->ct_general.use)))
67 return NULL;
68
69 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
70 if (!entry)
71 goto err_ct_refcnt;
72
73 flow = &entry->flow;
74
75 if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst))
76 goto err_dst_cache_original;
77
78 if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst))
79 goto err_dst_cache_reply;
80
81 entry->ct = ct;
82
047b300e
FF
83 flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
84 flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
ac2a6666
PNA
85
86 if (ct->status & IPS_SRC_NAT)
87 flow->flags |= FLOW_OFFLOAD_SNAT;
df1e2025 88 if (ct->status & IPS_DST_NAT)
ac2a6666
PNA
89 flow->flags |= FLOW_OFFLOAD_DNAT;
90
91 return flow;
92
93err_dst_cache_reply:
94 dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
95err_dst_cache_original:
96 kfree(entry);
97err_ct_refcnt:
98 nf_ct_put(ct);
99
100 return NULL;
101}
102EXPORT_SYMBOL_GPL(flow_offload_alloc);
103
da5984e5
FF
104static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
105{
106 tcp->state = TCP_CONNTRACK_ESTABLISHED;
107 tcp->seen[0].td_maxwin = 0;
108 tcp->seen[1].td_maxwin = 0;
109}
110
e97d9404
FW
111#define NF_FLOWTABLE_TCP_PICKUP_TIMEOUT (120 * HZ)
112#define NF_FLOWTABLE_UDP_PICKUP_TIMEOUT (30 * HZ)
113
1e5b2471
PNA
114static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
115{
116 return (__s32)(timeout - (u32)jiffies);
117}
118
119static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
da5984e5
FF
120{
121 const struct nf_conntrack_l4proto *l4proto;
1e5b2471 122 int l4num = nf_ct_protonum(ct);
da5984e5 123 unsigned int timeout;
da5984e5 124
4a60dc74 125 l4proto = nf_ct_l4proto_find(l4num);
da5984e5
FF
126 if (!l4proto)
127 return;
128
da5984e5 129 if (l4num == IPPROTO_TCP)
e97d9404 130 timeout = NF_FLOWTABLE_TCP_PICKUP_TIMEOUT;
da5984e5 131 else if (l4num == IPPROTO_UDP)
e97d9404 132 timeout = NF_FLOWTABLE_UDP_PICKUP_TIMEOUT;
da5984e5
FF
133 else
134 return;
135
1e5b2471
PNA
136 if (nf_flow_timeout_delta(ct->timeout) > (__s32)timeout)
137 ct->timeout = nfct_time_stamp + timeout;
138}
139
140static void flow_offload_fixup_ct_state(struct nf_conn *ct)
141{
142 if (nf_ct_protonum(ct) == IPPROTO_TCP)
143 flow_offload_fixup_tcp(&ct->proto.tcp);
144}
145
146static void flow_offload_fixup_ct(struct nf_conn *ct)
147{
148 flow_offload_fixup_ct_state(ct);
149 flow_offload_fixup_ct_timeout(ct);
da5984e5
FF
150}
151
ac2a6666
PNA
152void flow_offload_free(struct flow_offload *flow)
153{
154 struct flow_offload_entry *e;
155
156 dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
157 dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
158 e = container_of(flow, struct flow_offload_entry, flow);
da5984e5
FF
159 if (flow->flags & FLOW_OFFLOAD_DYING)
160 nf_ct_delete(e->ct, 0, 0);
0ff90b6c
FF
161 nf_ct_put(e->ct);
162 kfree_rcu(e, rcu_head);
ac2a6666
PNA
163}
164EXPORT_SYMBOL_GPL(flow_offload_free);
165
a268de77
FF
166static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
167{
168 const struct flow_offload_tuple *tuple = data;
169
170 return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
171}
172
173static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
174{
175 const struct flow_offload_tuple_rhash *tuplehash = data;
176
177 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
178}
179
180static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
181 const void *ptr)
182{
183 const struct flow_offload_tuple *tuple = arg->key;
184 const struct flow_offload_tuple_rhash *x = ptr;
185
186 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
187 return 1;
188
189 return 0;
190}
191
192static const struct rhashtable_params nf_flow_offload_rhash_params = {
193 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
194 .hashfn = flow_offload_hash,
195 .obj_hashfn = flow_offload_hash_obj,
196 .obj_cmpfn = flow_offload_hash_cmp,
197 .automatic_shrinking = true,
198};
199
ac2a6666
PNA
200int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
201{
43c8f131
TY
202 int err;
203
204 err = rhashtable_insert_fast(&flow_table->rhashtable,
205 &flow->tuplehash[0].node,
206 nf_flow_offload_rhash_params);
207 if (err < 0)
208 return err;
209
210 err = rhashtable_insert_fast(&flow_table->rhashtable,
211 &flow->tuplehash[1].node,
212 nf_flow_offload_rhash_params);
213 if (err < 0) {
214 rhashtable_remove_fast(&flow_table->rhashtable,
215 &flow->tuplehash[0].node,
216 nf_flow_offload_rhash_params);
217 return err;
218 }
ac2a6666 219
43c8f131 220 flow->timeout = (u32)jiffies;
ac2a6666
PNA
221 return 0;
222}
223EXPORT_SYMBOL_GPL(flow_offload_add);
224
3e68db2f
PNA
225static inline bool nf_flow_has_expired(const struct flow_offload *flow)
226{
1e5b2471 227 return nf_flow_timeout_delta(flow->timeout) <= 0;
3e68db2f
PNA
228}
229
0ff90b6c
FF
230static void flow_offload_del(struct nf_flowtable *flow_table,
231 struct flow_offload *flow)
ac2a6666 232{
da5984e5
FF
233 struct flow_offload_entry *e;
234
ac2a6666
PNA
235 rhashtable_remove_fast(&flow_table->rhashtable,
236 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
a268de77 237 nf_flow_offload_rhash_params);
ac2a6666
PNA
238 rhashtable_remove_fast(&flow_table->rhashtable,
239 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
a268de77 240 nf_flow_offload_rhash_params);
ac2a6666 241
da5984e5
FF
242 e = container_of(flow, struct flow_offload_entry, flow);
243 clear_bit(IPS_OFFLOAD_BIT, &e->ct->status);
244
3e68db2f
PNA
245 if (nf_flow_has_expired(flow))
246 flow_offload_fixup_ct(e->ct);
1e5b2471
PNA
247 else if (flow->flags & FLOW_OFFLOAD_TEARDOWN)
248 flow_offload_fixup_ct_timeout(e->ct);
3e68db2f 249
0ff90b6c 250 flow_offload_free(flow);
ac2a6666 251}
ac2a6666 252
59c466dd
FF
253void flow_offload_teardown(struct flow_offload *flow)
254{
da5984e5
FF
255 struct flow_offload_entry *e;
256
59c466dd 257 flow->flags |= FLOW_OFFLOAD_TEARDOWN;
da5984e5
FF
258
259 e = container_of(flow, struct flow_offload_entry, flow);
1e5b2471 260 flow_offload_fixup_ct_state(e->ct);
59c466dd
FF
261}
262EXPORT_SYMBOL_GPL(flow_offload_teardown);
263
ac2a6666
PNA
264struct flow_offload_tuple_rhash *
265flow_offload_lookup(struct nf_flowtable *flow_table,
266 struct flow_offload_tuple *tuple)
267{
ba03137f
FF
268 struct flow_offload_tuple_rhash *tuplehash;
269 struct flow_offload *flow;
8cd2bc98 270 struct flow_offload_entry *e;
ba03137f
FF
271 int dir;
272
a2d88182
TY
273 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
274 nf_flow_offload_rhash_params);
ba03137f
FF
275 if (!tuplehash)
276 return NULL;
277
278 dir = tuplehash->tuple.dir;
279 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
280 if (flow->flags & (FLOW_OFFLOAD_DYING | FLOW_OFFLOAD_TEARDOWN))
281 return NULL;
282
8cd2bc98
TY
283 e = container_of(flow, struct flow_offload_entry, flow);
284 if (unlikely(nf_ct_is_dying(e->ct)))
285 return NULL;
286
ba03137f 287 return tuplehash;
ac2a6666
PNA
288}
289EXPORT_SYMBOL_GPL(flow_offload_lookup);
290
49de9c09
TY
291static int
292nf_flow_table_iterate(struct nf_flowtable *flow_table,
293 void (*iter)(struct flow_offload *flow, void *data),
294 void *data)
ac2a6666
PNA
295{
296 struct flow_offload_tuple_rhash *tuplehash;
297 struct rhashtable_iter hti;
298 struct flow_offload *flow;
0de22baa 299 int err = 0;
ac2a6666 300
0de22baa 301 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
ac2a6666
PNA
302 rhashtable_walk_start(&hti);
303
304 while ((tuplehash = rhashtable_walk_next(&hti))) {
305 if (IS_ERR(tuplehash)) {
0de22baa
TY
306 if (PTR_ERR(tuplehash) != -EAGAIN) {
307 err = PTR_ERR(tuplehash);
308 break;
309 }
ac2a6666
PNA
310 continue;
311 }
312 if (tuplehash->tuple.dir)
313 continue;
314
315 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
316
317 iter(flow, data);
318 }
ac2a6666
PNA
319 rhashtable_walk_stop(&hti);
320 rhashtable_walk_exit(&hti);
321
322 return err;
323}
ac2a6666 324
b9660987 325static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
ac2a6666 326{
b9660987 327 struct nf_flowtable *flow_table = data;
8cd2bc98 328 struct flow_offload_entry *e;
ac2a6666 329
8cd2bc98
TY
330 e = container_of(flow, struct flow_offload_entry, flow);
331 if (nf_flow_has_expired(flow) || nf_ct_is_dying(e->ct) ||
b9660987
TY
332 (flow->flags & (FLOW_OFFLOAD_DYING | FLOW_OFFLOAD_TEARDOWN)))
333 flow_offload_del(flow_table, flow);
b408c5b0
PNA
334}
335
a268de77 336static void nf_flow_offload_work_gc(struct work_struct *work)
b408c5b0
PNA
337{
338 struct nf_flowtable *flow_table;
339
340 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
b9660987 341 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
ac2a6666
PNA
342 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
343}
ac2a6666
PNA
344
345static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
346 __be16 port, __be16 new_port)
347{
348 struct tcphdr *tcph;
349
350 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
351 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
352 return -1;
353
354 tcph = (void *)(skb_network_header(skb) + thoff);
355 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, true);
356
357 return 0;
358}
359
360static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
361 __be16 port, __be16 new_port)
362{
363 struct udphdr *udph;
364
365 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
366 skb_try_make_writable(skb, thoff + sizeof(*udph)))
367 return -1;
368
369 udph = (void *)(skb_network_header(skb) + thoff);
370 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
371 inet_proto_csum_replace2(&udph->check, skb, port,
372 new_port, true);
373 if (!udph->check)
374 udph->check = CSUM_MANGLED_0;
375 }
376
377 return 0;
378}
379
380static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
381 u8 protocol, __be16 port, __be16 new_port)
382{
383 switch (protocol) {
384 case IPPROTO_TCP:
385 if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
386 return NF_DROP;
387 break;
388 case IPPROTO_UDP:
389 if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
390 return NF_DROP;
391 break;
392 }
393
394 return 0;
395}
396
397int nf_flow_snat_port(const struct flow_offload *flow,
398 struct sk_buff *skb, unsigned int thoff,
399 u8 protocol, enum flow_offload_tuple_dir dir)
400{
401 struct flow_ports *hdr;
402 __be16 port, new_port;
403
404 if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
405 skb_try_make_writable(skb, thoff + sizeof(*hdr)))
406 return -1;
407
408 hdr = (void *)(skb_network_header(skb) + thoff);
409
410 switch (dir) {
411 case FLOW_OFFLOAD_DIR_ORIGINAL:
412 port = hdr->source;
413 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
414 hdr->source = new_port;
415 break;
416 case FLOW_OFFLOAD_DIR_REPLY:
417 port = hdr->dest;
418 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
419 hdr->dest = new_port;
420 break;
421 default:
422 return -1;
423 }
424
425 return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
426}
427EXPORT_SYMBOL_GPL(nf_flow_snat_port);
428
429int nf_flow_dnat_port(const struct flow_offload *flow,
430 struct sk_buff *skb, unsigned int thoff,
431 u8 protocol, enum flow_offload_tuple_dir dir)
432{
433 struct flow_ports *hdr;
434 __be16 port, new_port;
435
436 if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
437 skb_try_make_writable(skb, thoff + sizeof(*hdr)))
438 return -1;
439
440 hdr = (void *)(skb_network_header(skb) + thoff);
441
442 switch (dir) {
443 case FLOW_OFFLOAD_DIR_ORIGINAL:
444 port = hdr->dest;
445 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
446 hdr->dest = new_port;
447 break;
448 case FLOW_OFFLOAD_DIR_REPLY:
449 port = hdr->source;
450 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
451 hdr->source = new_port;
452 break;
453 default:
454 return -1;
455 }
456
457 return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
458}
459EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
460
a268de77
FF
461int nf_flow_table_init(struct nf_flowtable *flowtable)
462{
463 int err;
464
465 INIT_DEFERRABLE_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
466
467 err = rhashtable_init(&flowtable->rhashtable,
468 &nf_flow_offload_rhash_params);
469 if (err < 0)
470 return err;
471
472 queue_delayed_work(system_power_efficient_wq,
473 &flowtable->gc_work, HZ);
474
84453a90
FF
475 mutex_lock(&flowtable_lock);
476 list_add(&flowtable->list, &flowtables);
477 mutex_unlock(&flowtable_lock);
478
a268de77
FF
479 return 0;
480}
481EXPORT_SYMBOL_GPL(nf_flow_table_init);
482
c0ea1bcb
PNA
483static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
484{
485 struct net_device *dev = data;
a3fb3698
TY
486 struct flow_offload_entry *e;
487
488 e = container_of(flow, struct flow_offload_entry, flow);
c0ea1bcb 489
59c466dd
FF
490 if (!dev) {
491 flow_offload_teardown(flow);
c0ea1bcb 492 return;
59c466dd 493 }
a3fb3698
TY
494 if (net_eq(nf_ct_net(e->ct), dev_net(dev)) &&
495 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
496 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
59c466dd 497 flow_offload_dead(flow);
c0ea1bcb
PNA
498}
499
500static void nf_flow_table_iterate_cleanup(struct nf_flowtable *flowtable,
84453a90 501 struct net_device *dev)
c0ea1bcb 502{
84453a90 503 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
c0ea1bcb
PNA
504 flush_delayed_work(&flowtable->gc_work);
505}
506
5f1be84a 507void nf_flow_table_cleanup(struct net_device *dev)
c0ea1bcb 508{
84453a90
FF
509 struct nf_flowtable *flowtable;
510
511 mutex_lock(&flowtable_lock);
512 list_for_each_entry(flowtable, &flowtables, list)
513 nf_flow_table_iterate_cleanup(flowtable, dev);
514 mutex_unlock(&flowtable_lock);
c0ea1bcb
PNA
515}
516EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
517
b408c5b0
PNA
518void nf_flow_table_free(struct nf_flowtable *flow_table)
519{
84453a90
FF
520 mutex_lock(&flowtable_lock);
521 list_del(&flow_table->list);
522 mutex_unlock(&flowtable_lock);
a268de77 523 cancel_delayed_work_sync(&flow_table->gc_work);
b408c5b0 524 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
b9660987 525 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
a268de77 526 rhashtable_destroy(&flow_table->rhashtable);
b408c5b0
PNA
527}
528EXPORT_SYMBOL_GPL(nf_flow_table_free);
529
ac2a6666
PNA
530MODULE_LICENSE("GPL");
531MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");