netfilter: nfnetlink: add struct nfnl_info and pass it to callbacks
[linux-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>
40d102cd 14#include <net/netfilter/nf_conntrack_l4proto.h>
ac2a6666
PNA
15#include <net/netfilter/nf_conntrack_tuple.h>
16
84453a90
FF
17static DEFINE_MUTEX(flowtable_lock);
18static LIST_HEAD(flowtables);
19
047b300e 20static void
458a1828 21flow_offload_fill_dir(struct flow_offload *flow,
047b300e
FF
22 enum flow_offload_tuple_dir dir)
23{
24 struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
458a1828 25 struct nf_conntrack_tuple *ctt = &flow->ct->tuplehash[dir].tuple;
047b300e
FF
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;
047b300e
FF
44}
45
f1363e05 46struct flow_offload *flow_offload_alloc(struct nf_conn *ct)
ac2a6666 47{
ac2a6666
PNA
48 struct flow_offload *flow;
49
50 if (unlikely(nf_ct_is_dying(ct) ||
51 !atomic_inc_not_zero(&ct->ct_general.use)))
52 return NULL;
53
62248df8
PNA
54 flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
55 if (!flow)
ac2a6666
PNA
56 goto err_ct_refcnt;
57
b32d2f34 58 flow->ct = ct;
ac2a6666 59
458a1828 60 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
61 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_REPLY);
ac2a6666
PNA
62
63 if (ct->status & IPS_SRC_NAT)
355a8b13 64 __set_bit(NF_FLOW_SNAT, &flow->flags);
df1e2025 65 if (ct->status & IPS_DST_NAT)
355a8b13 66 __set_bit(NF_FLOW_DNAT, &flow->flags);
ac2a6666
PNA
67
68 return flow;
69
ac2a6666
PNA
70err_ct_refcnt:
71 nf_ct_put(ct);
72
73 return NULL;
74}
75EXPORT_SYMBOL_GPL(flow_offload_alloc);
76
8b9229d1
PNA
77static 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
f1363e05
PNA
89static 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;
f1363e05 94 struct dst_entry *dst = route->tuple[dir].dst;
4cd91f7c 95 int i, j = 0;
f1363e05 96
f1363e05
PNA
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_forward(dst);
103 break;
104 }
105
c63a7cc4 106 flow_tuple->iifidx = route->tuple[dir].in.ifindex;
4cd91f7c
PNA
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;
26267bf9
FF
110 if (route->tuple[dir].in.ingress_vlans & BIT(i))
111 flow_tuple->in_vlan_ingress |= BIT(j);
4cd91f7c
PNA
112 j++;
113 }
114 flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
7a27f6ab
PNA
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;
73f97025 123 flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
7a27f6ab
PNA
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;
8b9229d1 131 flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
7a27f6ab 132 break;
78ed0a9b
RD
133 default:
134 WARN_ON_ONCE(1);
135 break;
7a27f6ab 136 }
5139c0c0 137 flow_tuple->xmit_type = route->tuple[dir].xmit_type;
f1363e05
PNA
138
139 return 0;
140}
141
7a27f6ab
PNA
142static 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
f1363e05
PNA
150int 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
167err_route_reply:
7a27f6ab 168 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
f1363e05
PNA
169
170 return err;
171}
172EXPORT_SYMBOL_GPL(flow_offload_route_init);
173
da5984e5
FF
174static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
175{
176 tcp->state = TCP_CONNTRACK_ESTABLISHED;
177 tcp->seen[0].td_maxwin = 0;
178 tcp->seen[1].td_maxwin = 0;
179}
180
e97d9404
FW
181#define NF_FLOWTABLE_TCP_PICKUP_TIMEOUT (120 * HZ)
182#define NF_FLOWTABLE_UDP_PICKUP_TIMEOUT (30 * HZ)
183
1e5b2471 184static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
da5984e5
FF
185{
186 const struct nf_conntrack_l4proto *l4proto;
1e5b2471 187 int l4num = nf_ct_protonum(ct);
da5984e5 188 unsigned int timeout;
da5984e5 189
4a60dc74 190 l4proto = nf_ct_l4proto_find(l4num);
da5984e5
FF
191 if (!l4proto)
192 return;
193
da5984e5 194 if (l4num == IPPROTO_TCP)
e97d9404 195 timeout = NF_FLOWTABLE_TCP_PICKUP_TIMEOUT;
da5984e5 196 else if (l4num == IPPROTO_UDP)
e97d9404 197 timeout = NF_FLOWTABLE_UDP_PICKUP_TIMEOUT;
da5984e5
FF
198 else
199 return;
200
1e5b2471
PNA
201 if (nf_flow_timeout_delta(ct->timeout) > (__s32)timeout)
202 ct->timeout = nfct_time_stamp + timeout;
203}
204
205static void flow_offload_fixup_ct_state(struct nf_conn *ct)
206{
207 if (nf_ct_protonum(ct) == IPPROTO_TCP)
208 flow_offload_fixup_tcp(&ct->proto.tcp);
209}
210
211static void flow_offload_fixup_ct(struct nf_conn *ct)
212{
213 flow_offload_fixup_ct_state(ct);
214 flow_offload_fixup_ct_timeout(ct);
da5984e5
FF
215}
216
f1363e05 217static void flow_offload_route_release(struct flow_offload *flow)
ac2a6666 218{
7a27f6ab
PNA
219 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
220 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
f1363e05
PNA
221}
222
223void flow_offload_free(struct flow_offload *flow)
224{
225 switch (flow->type) {
226 case NF_FLOW_OFFLOAD_ROUTE:
227 flow_offload_route_release(flow);
228 break;
229 default:
230 break;
231 }
b32d2f34 232 nf_ct_put(flow->ct);
62248df8 233 kfree_rcu(flow, rcu_head);
ac2a6666
PNA
234}
235EXPORT_SYMBOL_GPL(flow_offload_free);
236
a268de77
FF
237static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
238{
239 const struct flow_offload_tuple *tuple = data;
240
dbc859d9 241 return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
a268de77
FF
242}
243
244static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
245{
246 const struct flow_offload_tuple_rhash *tuplehash = data;
247
dbc859d9 248 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
a268de77
FF
249}
250
251static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
252 const void *ptr)
253{
254 const struct flow_offload_tuple *tuple = arg->key;
255 const struct flow_offload_tuple_rhash *x = ptr;
256
dbc859d9 257 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
a268de77
FF
258 return 1;
259
260 return 0;
261}
262
263static const struct rhashtable_params nf_flow_offload_rhash_params = {
264 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
265 .hashfn = flow_offload_hash,
266 .obj_hashfn = flow_offload_hash_obj,
267 .obj_cmpfn = flow_offload_hash_cmp,
268 .automatic_shrinking = true,
269};
270
ac2a6666
PNA
271int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
272{
43c8f131
TY
273 int err;
274
fb46f1b7 275 flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
daf61b02 276
43c8f131
TY
277 err = rhashtable_insert_fast(&flow_table->rhashtable,
278 &flow->tuplehash[0].node,
279 nf_flow_offload_rhash_params);
280 if (err < 0)
281 return err;
282
283 err = rhashtable_insert_fast(&flow_table->rhashtable,
284 &flow->tuplehash[1].node,
285 nf_flow_offload_rhash_params);
286 if (err < 0) {
287 rhashtable_remove_fast(&flow_table->rhashtable,
288 &flow->tuplehash[0].node,
289 nf_flow_offload_rhash_params);
290 return err;
291 }
ac2a6666 292
4203b19c
RD
293 nf_ct_offload_timeout(flow->ct);
294
f698fe40
PNA
295 if (nf_flowtable_hw_offload(flow_table)) {
296 __set_bit(NF_FLOW_HW, &flow->flags);
c29f74e0 297 nf_flow_offload_add(flow_table, flow);
f698fe40 298 }
c29f74e0 299
ac2a6666
PNA
300 return 0;
301}
302EXPORT_SYMBOL_GPL(flow_offload_add);
303
8b3646d6
PB
304void flow_offload_refresh(struct nf_flowtable *flow_table,
305 struct flow_offload *flow)
306{
307 flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
308
309 if (likely(!nf_flowtable_hw_offload(flow_table) ||
310 !test_and_clear_bit(NF_FLOW_HW_REFRESH, &flow->flags)))
311 return;
312
313 nf_flow_offload_add(flow_table, flow);
314}
315EXPORT_SYMBOL_GPL(flow_offload_refresh);
316
3e68db2f
PNA
317static inline bool nf_flow_has_expired(const struct flow_offload *flow)
318{
1e5b2471 319 return nf_flow_timeout_delta(flow->timeout) <= 0;
3e68db2f
PNA
320}
321
0ff90b6c
FF
322static void flow_offload_del(struct nf_flowtable *flow_table,
323 struct flow_offload *flow)
ac2a6666 324{
ac2a6666
PNA
325 rhashtable_remove_fast(&flow_table->rhashtable,
326 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
a268de77 327 nf_flow_offload_rhash_params);
ac2a6666
PNA
328 rhashtable_remove_fast(&flow_table->rhashtable,
329 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
a268de77 330 nf_flow_offload_rhash_params);
ac2a6666 331
b32d2f34 332 clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
da5984e5 333
3e68db2f 334 if (nf_flow_has_expired(flow))
b32d2f34 335 flow_offload_fixup_ct(flow->ct);
9ed81c8e 336 else
b32d2f34 337 flow_offload_fixup_ct_timeout(flow->ct);
3e68db2f 338
0ff90b6c 339 flow_offload_free(flow);
ac2a6666 340}
ac2a6666 341
59c466dd
FF
342void flow_offload_teardown(struct flow_offload *flow)
343{
355a8b13 344 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
da5984e5 345
b32d2f34 346 flow_offload_fixup_ct_state(flow->ct);
59c466dd
FF
347}
348EXPORT_SYMBOL_GPL(flow_offload_teardown);
349
ac2a6666
PNA
350struct flow_offload_tuple_rhash *
351flow_offload_lookup(struct nf_flowtable *flow_table,
352 struct flow_offload_tuple *tuple)
353{
ba03137f
FF
354 struct flow_offload_tuple_rhash *tuplehash;
355 struct flow_offload *flow;
356 int dir;
357
a2d88182
TY
358 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
359 nf_flow_offload_rhash_params);
ba03137f
FF
360 if (!tuplehash)
361 return NULL;
362
363 dir = tuplehash->tuple.dir;
364 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
355a8b13 365 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
ba03137f
FF
366 return NULL;
367
b32d2f34 368 if (unlikely(nf_ct_is_dying(flow->ct)))
8cd2bc98
TY
369 return NULL;
370
ba03137f 371 return tuplehash;
ac2a6666
PNA
372}
373EXPORT_SYMBOL_GPL(flow_offload_lookup);
374
49de9c09
TY
375static int
376nf_flow_table_iterate(struct nf_flowtable *flow_table,
377 void (*iter)(struct flow_offload *flow, void *data),
378 void *data)
ac2a6666
PNA
379{
380 struct flow_offload_tuple_rhash *tuplehash;
381 struct rhashtable_iter hti;
382 struct flow_offload *flow;
0de22baa 383 int err = 0;
ac2a6666 384
0de22baa 385 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
ac2a6666
PNA
386 rhashtable_walk_start(&hti);
387
388 while ((tuplehash = rhashtable_walk_next(&hti))) {
389 if (IS_ERR(tuplehash)) {
0de22baa
TY
390 if (PTR_ERR(tuplehash) != -EAGAIN) {
391 err = PTR_ERR(tuplehash);
392 break;
393 }
ac2a6666
PNA
394 continue;
395 }
396 if (tuplehash->tuple.dir)
397 continue;
398
399 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
400
401 iter(flow, data);
402 }
ac2a6666
PNA
403 rhashtable_walk_stop(&hti);
404 rhashtable_walk_exit(&hti);
405
406 return err;
407}
ac2a6666 408
8b9229d1
PNA
409static bool flow_offload_stale_dst(struct flow_offload_tuple *tuple)
410{
411 struct dst_entry *dst;
412
413 if (tuple->xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
414 tuple->xmit_type == FLOW_OFFLOAD_XMIT_XFRM) {
415 dst = tuple->dst_cache;
416 if (!dst_check(dst, tuple->dst_cookie))
417 return true;
418 }
419
420 return false;
421}
422
423static bool nf_flow_has_stale_dst(struct flow_offload *flow)
424{
425 return flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
426 flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple);
427}
428
b9660987 429static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
ac2a6666 430{
b9660987 431 struct nf_flowtable *flow_table = data;
ac2a6666 432
8b9229d1
PNA
433 if (nf_flow_has_expired(flow) ||
434 nf_ct_is_dying(flow->ct) ||
435 nf_flow_has_stale_dst(flow))
9ed81c8e
PNA
436 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
437
438 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
355a8b13
PNA
439 if (test_bit(NF_FLOW_HW, &flow->flags)) {
440 if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
c29f74e0 441 nf_flow_offload_del(flow_table, flow);
355a8b13 442 else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
c29f74e0
PNA
443 flow_offload_del(flow_table, flow);
444 } else {
445 flow_offload_del(flow_table, flow);
446 }
355a8b13 447 } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
79b9b685 448 nf_flow_offload_stats(flow_table, flow);
c29f74e0 449 }
b408c5b0
PNA
450}
451
a268de77 452static void nf_flow_offload_work_gc(struct work_struct *work)
b408c5b0
PNA
453{
454 struct nf_flowtable *flow_table;
455
456 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
b9660987 457 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
ac2a6666
PNA
458 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
459}
ac2a6666 460
f4401262
PNA
461static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
462 __be16 port, __be16 new_port)
ac2a6666
PNA
463{
464 struct tcphdr *tcph;
465
ac2a6666 466 tcph = (void *)(skb_network_header(skb) + thoff);
8d6bca15 467 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
ac2a6666
PNA
468}
469
f4401262
PNA
470static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
471 __be16 port, __be16 new_port)
ac2a6666
PNA
472{
473 struct udphdr *udph;
474
ac2a6666
PNA
475 udph = (void *)(skb_network_header(skb) + thoff);
476 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
477 inet_proto_csum_replace2(&udph->check, skb, port,
8d6bca15 478 new_port, false);
ac2a6666
PNA
479 if (!udph->check)
480 udph->check = CSUM_MANGLED_0;
481 }
ac2a6666
PNA
482}
483
f4401262
PNA
484static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
485 u8 protocol, __be16 port, __be16 new_port)
ac2a6666
PNA
486{
487 switch (protocol) {
488 case IPPROTO_TCP:
f4401262 489 nf_flow_nat_port_tcp(skb, thoff, port, new_port);
ac2a6666
PNA
490 break;
491 case IPPROTO_UDP:
f4401262 492 nf_flow_nat_port_udp(skb, thoff, port, new_port);
ac2a6666
PNA
493 break;
494 }
ac2a6666
PNA
495}
496
f4401262
PNA
497void nf_flow_snat_port(const struct flow_offload *flow,
498 struct sk_buff *skb, unsigned int thoff,
499 u8 protocol, enum flow_offload_tuple_dir dir)
ac2a6666
PNA
500{
501 struct flow_ports *hdr;
502 __be16 port, new_port;
503
ac2a6666
PNA
504 hdr = (void *)(skb_network_header(skb) + thoff);
505
506 switch (dir) {
507 case FLOW_OFFLOAD_DIR_ORIGINAL:
508 port = hdr->source;
509 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
510 hdr->source = new_port;
511 break;
512 case FLOW_OFFLOAD_DIR_REPLY:
513 port = hdr->dest;
514 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
515 hdr->dest = new_port;
516 break;
ac2a6666
PNA
517 }
518
f4401262 519 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
ac2a6666
PNA
520}
521EXPORT_SYMBOL_GPL(nf_flow_snat_port);
522
f4401262
PNA
523void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
524 unsigned int thoff, u8 protocol,
525 enum flow_offload_tuple_dir dir)
ac2a6666
PNA
526{
527 struct flow_ports *hdr;
528 __be16 port, new_port;
529
ac2a6666
PNA
530 hdr = (void *)(skb_network_header(skb) + thoff);
531
532 switch (dir) {
533 case FLOW_OFFLOAD_DIR_ORIGINAL:
534 port = hdr->dest;
535 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
536 hdr->dest = new_port;
537 break;
538 case FLOW_OFFLOAD_DIR_REPLY:
539 port = hdr->source;
540 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
541 hdr->source = new_port;
542 break;
ac2a6666
PNA
543 }
544
f4401262 545 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
ac2a6666
PNA
546}
547EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
548
a268de77
FF
549int nf_flow_table_init(struct nf_flowtable *flowtable)
550{
551 int err;
552
740b486a 553 INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
c29f74e0 554 flow_block_init(&flowtable->flow_block);
422c032a 555 init_rwsem(&flowtable->flow_block_lock);
a268de77
FF
556
557 err = rhashtable_init(&flowtable->rhashtable,
558 &nf_flow_offload_rhash_params);
559 if (err < 0)
560 return err;
561
562 queue_delayed_work(system_power_efficient_wq,
563 &flowtable->gc_work, HZ);
564
84453a90
FF
565 mutex_lock(&flowtable_lock);
566 list_add(&flowtable->list, &flowtables);
567 mutex_unlock(&flowtable_lock);
568
a268de77
FF
569 return 0;
570}
571EXPORT_SYMBOL_GPL(nf_flow_table_init);
572
c0ea1bcb
PNA
573static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
574{
575 struct net_device *dev = data;
576
59c466dd
FF
577 if (!dev) {
578 flow_offload_teardown(flow);
c0ea1bcb 579 return;
59c466dd 580 }
b32d2f34
PNA
581
582 if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
a3fb3698
TY
583 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
584 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
445db8d0 585 flow_offload_teardown(flow);
c0ea1bcb
PNA
586}
587
a8284c68
PNA
588void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
589 struct net_device *dev)
c0ea1bcb 590{
84453a90 591 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
c0ea1bcb 592 flush_delayed_work(&flowtable->gc_work);
91bfaa15 593 nf_flow_table_offload_flush(flowtable);
c0ea1bcb
PNA
594}
595
5f1be84a 596void nf_flow_table_cleanup(struct net_device *dev)
c0ea1bcb 597{
84453a90
FF
598 struct nf_flowtable *flowtable;
599
600 mutex_lock(&flowtable_lock);
601 list_for_each_entry(flowtable, &flowtables, list)
a8284c68 602 nf_flow_table_gc_cleanup(flowtable, dev);
84453a90 603 mutex_unlock(&flowtable_lock);
c0ea1bcb
PNA
604}
605EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
606
b408c5b0
PNA
607void nf_flow_table_free(struct nf_flowtable *flow_table)
608{
84453a90
FF
609 mutex_lock(&flowtable_lock);
610 list_del(&flow_table->list);
611 mutex_unlock(&flowtable_lock);
978703f4 612
a268de77 613 cancel_delayed_work_sync(&flow_table->gc_work);
b408c5b0 614 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
b9660987 615 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
0f34f30a 616 nf_flow_table_offload_flush(flow_table);
c921ffe8
PB
617 if (nf_flowtable_hw_offload(flow_table))
618 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step,
619 flow_table);
a268de77 620 rhashtable_destroy(&flow_table->rhashtable);
b408c5b0
PNA
621}
622EXPORT_SYMBOL_GPL(nf_flow_table_free);
623
c29f74e0
PNA
624static int __init nf_flow_table_module_init(void)
625{
626 return nf_flow_table_offload_init();
627}
628
629static void __exit nf_flow_table_module_exit(void)
630{
631 nf_flow_table_offload_exit();
632}
633
634module_init(nf_flow_table_module_init);
635module_exit(nf_flow_table_module_exit);
636
ac2a6666
PNA
637MODULE_LICENSE("GPL");
638MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
4cacc395 639MODULE_DESCRIPTION("Netfilter flow table module");