x86/mm/pae: Make pmd_t similar to pte_t
[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>
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;
4e8d9584
TM
42
43 switch (ctt->dst.protonum) {
44 case IPPROTO_TCP:
45 case IPPROTO_UDP:
46 ft->src_port = ctt->src.u.tcp.port;
47 ft->dst_port = ctt->dst.u.tcp.port;
48 break;
49 }
047b300e
FF
50}
51
f1363e05 52struct flow_offload *flow_offload_alloc(struct nf_conn *ct)
ac2a6666 53{
ac2a6666
PNA
54 struct flow_offload *flow;
55
f02e7dc4 56 if (unlikely(nf_ct_is_dying(ct)))
ac2a6666
PNA
57 return NULL;
58
62248df8
PNA
59 flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
60 if (!flow)
f02e7dc4 61 return NULL;
ac2a6666 62
f02e7dc4 63 refcount_inc(&ct->ct_general.use);
b32d2f34 64 flow->ct = ct;
ac2a6666 65
458a1828 66 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
67 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_REPLY);
ac2a6666
PNA
68
69 if (ct->status & IPS_SRC_NAT)
355a8b13 70 __set_bit(NF_FLOW_SNAT, &flow->flags);
df1e2025 71 if (ct->status & IPS_DST_NAT)
355a8b13 72 __set_bit(NF_FLOW_DNAT, &flow->flags);
ac2a6666
PNA
73
74 return flow;
ac2a6666
PNA
75}
76EXPORT_SYMBOL_GPL(flow_offload_alloc);
77
8b9229d1
PNA
78static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
79{
80 const struct rt6_info *rt;
81
82 if (flow_tuple->l3proto == NFPROTO_IPV6) {
83 rt = (const struct rt6_info *)flow_tuple->dst_cache;
84 return rt6_get_cookie(rt);
85 }
86
87 return 0;
88}
89
f1363e05
PNA
90static int flow_offload_fill_route(struct flow_offload *flow,
91 const struct nf_flow_route *route,
92 enum flow_offload_tuple_dir dir)
93{
94 struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
f1363e05 95 struct dst_entry *dst = route->tuple[dir].dst;
4cd91f7c 96 int i, j = 0;
f1363e05 97
f1363e05
PNA
98 switch (flow_tuple->l3proto) {
99 case NFPROTO_IPV4:
100 flow_tuple->mtu = ip_dst_mtu_maybe_forward(dst, true);
101 break;
102 case NFPROTO_IPV6:
427faee1 103 flow_tuple->mtu = ip6_dst_mtu_maybe_forward(dst, true);
f1363e05
PNA
104 break;
105 }
106
c63a7cc4 107 flow_tuple->iifidx = route->tuple[dir].in.ifindex;
4cd91f7c
PNA
108 for (i = route->tuple[dir].in.num_encaps - 1; i >= 0; i--) {
109 flow_tuple->encap[j].id = route->tuple[dir].in.encap[i].id;
110 flow_tuple->encap[j].proto = route->tuple[dir].in.encap[i].proto;
26267bf9
FF
111 if (route->tuple[dir].in.ingress_vlans & BIT(i))
112 flow_tuple->in_vlan_ingress |= BIT(j);
4cd91f7c
PNA
113 j++;
114 }
115 flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
7a27f6ab
PNA
116
117 switch (route->tuple[dir].xmit_type) {
118 case FLOW_OFFLOAD_XMIT_DIRECT:
119 memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,
120 ETH_ALEN);
121 memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
122 ETH_ALEN);
123 flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
73f97025 124 flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
7a27f6ab
PNA
125 break;
126 case FLOW_OFFLOAD_XMIT_XFRM:
127 case FLOW_OFFLOAD_XMIT_NEIGH:
128 if (!dst_hold_safe(route->tuple[dir].dst))
129 return -1;
130
131 flow_tuple->dst_cache = dst;
8b9229d1 132 flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
7a27f6ab 133 break;
78ed0a9b
RD
134 default:
135 WARN_ON_ONCE(1);
136 break;
7a27f6ab 137 }
5139c0c0 138 flow_tuple->xmit_type = route->tuple[dir].xmit_type;
f1363e05
PNA
139
140 return 0;
141}
142
7a27f6ab
PNA
143static void nft_flow_dst_release(struct flow_offload *flow,
144 enum flow_offload_tuple_dir dir)
145{
146 if (flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
147 flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)
148 dst_release(flow->tuplehash[dir].tuple.dst_cache);
149}
150
f1363e05
PNA
151int flow_offload_route_init(struct flow_offload *flow,
152 const struct nf_flow_route *route)
153{
154 int err;
155
156 err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
157 if (err < 0)
158 return err;
159
160 err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
161 if (err < 0)
162 goto err_route_reply;
163
164 flow->type = NF_FLOW_OFFLOAD_ROUTE;
165
166 return 0;
167
168err_route_reply:
7a27f6ab 169 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
f1363e05
PNA
170
171 return err;
172}
173EXPORT_SYMBOL_GPL(flow_offload_route_init);
174
da5984e5
FF
175static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
176{
da5984e5
FF
177 tcp->seen[0].td_maxwin = 0;
178 tcp->seen[1].td_maxwin = 0;
179}
180
e5eaac2b 181static void flow_offload_fixup_ct(struct nf_conn *ct)
da5984e5 182{
1d91d2e1 183 struct net *net = nf_ct_net(ct);
1e5b2471 184 int l4num = nf_ct_protonum(ct);
4592ee7f 185 s32 timeout;
da5984e5 186
1d91d2e1
OS
187 if (l4num == IPPROTO_TCP) {
188 struct nf_tcp_net *tn = nf_tcp_pernet(net);
189
e5eaac2b
PNA
190 flow_offload_fixup_tcp(&ct->proto.tcp);
191
192 timeout = tn->timeouts[ct->proto.tcp.state];
4592ee7f 193 timeout -= tn->offload_timeout;
1d91d2e1
OS
194 } else if (l4num == IPPROTO_UDP) {
195 struct nf_udp_net *tn = nf_udp_pernet(net);
196
4592ee7f
FW
197 timeout = tn->timeouts[UDP_CT_REPLIED];
198 timeout -= tn->offload_timeout;
1d91d2e1 199 } else {
da5984e5 200 return;
1d91d2e1 201 }
da5984e5 202
4592ee7f
FW
203 if (timeout < 0)
204 timeout = 0;
205
802a7dc5
ED
206 if (nf_flow_timeout_delta(READ_ONCE(ct->timeout)) > (__s32)timeout)
207 WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);
1e5b2471
PNA
208}
209
f1363e05 210static void flow_offload_route_release(struct flow_offload *flow)
ac2a6666 211{
7a27f6ab
PNA
212 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
213 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
f1363e05
PNA
214}
215
216void flow_offload_free(struct flow_offload *flow)
217{
218 switch (flow->type) {
219 case NF_FLOW_OFFLOAD_ROUTE:
220 flow_offload_route_release(flow);
221 break;
222 default:
223 break;
224 }
b32d2f34 225 nf_ct_put(flow->ct);
62248df8 226 kfree_rcu(flow, rcu_head);
ac2a6666
PNA
227}
228EXPORT_SYMBOL_GPL(flow_offload_free);
229
a268de77
FF
230static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
231{
232 const struct flow_offload_tuple *tuple = data;
233
dbc859d9 234 return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
a268de77
FF
235}
236
237static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
238{
239 const struct flow_offload_tuple_rhash *tuplehash = data;
240
dbc859d9 241 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
a268de77
FF
242}
243
244static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
245 const void *ptr)
246{
247 const struct flow_offload_tuple *tuple = arg->key;
248 const struct flow_offload_tuple_rhash *x = ptr;
249
dbc859d9 250 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
a268de77
FF
251 return 1;
252
253 return 0;
254}
255
256static const struct rhashtable_params nf_flow_offload_rhash_params = {
257 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
258 .hashfn = flow_offload_hash,
259 .obj_hashfn = flow_offload_hash_obj,
260 .obj_cmpfn = flow_offload_hash_cmp,
261 .automatic_shrinking = true,
262};
263
1d91d2e1
OS
264unsigned long flow_offload_get_timeout(struct flow_offload *flow)
265{
1d91d2e1
OS
266 unsigned long timeout = NF_FLOW_TIMEOUT;
267 struct net *net = nf_ct_net(flow->ct);
268 int l4num = nf_ct_protonum(flow->ct);
269
1d91d2e1
OS
270 if (l4num == IPPROTO_TCP) {
271 struct nf_tcp_net *tn = nf_tcp_pernet(net);
272
273 timeout = tn->offload_timeout;
274 } else if (l4num == IPPROTO_UDP) {
275 struct nf_udp_net *tn = nf_udp_pernet(net);
276
277 timeout = tn->offload_timeout;
278 }
279
280 return timeout;
281}
282
ac2a6666
PNA
283int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
284{
43c8f131
TY
285 int err;
286
1d91d2e1 287 flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
daf61b02 288
43c8f131
TY
289 err = rhashtable_insert_fast(&flow_table->rhashtable,
290 &flow->tuplehash[0].node,
291 nf_flow_offload_rhash_params);
292 if (err < 0)
293 return err;
294
295 err = rhashtable_insert_fast(&flow_table->rhashtable,
296 &flow->tuplehash[1].node,
297 nf_flow_offload_rhash_params);
298 if (err < 0) {
299 rhashtable_remove_fast(&flow_table->rhashtable,
300 &flow->tuplehash[0].node,
301 nf_flow_offload_rhash_params);
302 return err;
303 }
ac2a6666 304
4203b19c
RD
305 nf_ct_offload_timeout(flow->ct);
306
f698fe40
PNA
307 if (nf_flowtable_hw_offload(flow_table)) {
308 __set_bit(NF_FLOW_HW, &flow->flags);
c29f74e0 309 nf_flow_offload_add(flow_table, flow);
f698fe40 310 }
c29f74e0 311
ac2a6666
PNA
312 return 0;
313}
314EXPORT_SYMBOL_GPL(flow_offload_add);
315
8b3646d6
PB
316void flow_offload_refresh(struct nf_flowtable *flow_table,
317 struct flow_offload *flow)
318{
32c3973d
PNA
319 u32 timeout;
320
321 timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
396ef641 322 if (timeout - READ_ONCE(flow->timeout) > HZ)
32c3973d 323 WRITE_ONCE(flow->timeout, timeout);
396ef641
FF
324 else
325 return;
8b3646d6 326
c07531c0 327 if (likely(!nf_flowtable_hw_offload(flow_table)))
8b3646d6
PB
328 return;
329
330 nf_flow_offload_add(flow_table, flow);
331}
332EXPORT_SYMBOL_GPL(flow_offload_refresh);
333
3e68db2f
PNA
334static inline bool nf_flow_has_expired(const struct flow_offload *flow)
335{
1e5b2471 336 return nf_flow_timeout_delta(flow->timeout) <= 0;
3e68db2f
PNA
337}
338
0ff90b6c
FF
339static void flow_offload_del(struct nf_flowtable *flow_table,
340 struct flow_offload *flow)
ac2a6666 341{
ac2a6666
PNA
342 rhashtable_remove_fast(&flow_table->rhashtable,
343 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
a268de77 344 nf_flow_offload_rhash_params);
ac2a6666
PNA
345 rhashtable_remove_fast(&flow_table->rhashtable,
346 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
a268de77 347 nf_flow_offload_rhash_params);
0ff90b6c 348 flow_offload_free(flow);
ac2a6666 349}
ac2a6666 350
59c466dd
FF
351void flow_offload_teardown(struct flow_offload *flow)
352{
e5eaac2b 353 clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
355a8b13 354 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
e5eaac2b 355 flow_offload_fixup_ct(flow->ct);
59c466dd
FF
356}
357EXPORT_SYMBOL_GPL(flow_offload_teardown);
358
ac2a6666
PNA
359struct flow_offload_tuple_rhash *
360flow_offload_lookup(struct nf_flowtable *flow_table,
361 struct flow_offload_tuple *tuple)
362{
ba03137f
FF
363 struct flow_offload_tuple_rhash *tuplehash;
364 struct flow_offload *flow;
365 int dir;
366
a2d88182
TY
367 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
368 nf_flow_offload_rhash_params);
ba03137f
FF
369 if (!tuplehash)
370 return NULL;
371
372 dir = tuplehash->tuple.dir;
373 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
355a8b13 374 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
ba03137f
FF
375 return NULL;
376
b32d2f34 377 if (unlikely(nf_ct_is_dying(flow->ct)))
8cd2bc98
TY
378 return NULL;
379
ba03137f 380 return tuplehash;
ac2a6666
PNA
381}
382EXPORT_SYMBOL_GPL(flow_offload_lookup);
383
49de9c09
TY
384static int
385nf_flow_table_iterate(struct nf_flowtable *flow_table,
217cff36
PNA
386 void (*iter)(struct nf_flowtable *flowtable,
387 struct flow_offload *flow, void *data),
49de9c09 388 void *data)
ac2a6666
PNA
389{
390 struct flow_offload_tuple_rhash *tuplehash;
391 struct rhashtable_iter hti;
392 struct flow_offload *flow;
0de22baa 393 int err = 0;
ac2a6666 394
0de22baa 395 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
ac2a6666
PNA
396 rhashtable_walk_start(&hti);
397
398 while ((tuplehash = rhashtable_walk_next(&hti))) {
399 if (IS_ERR(tuplehash)) {
0de22baa
TY
400 if (PTR_ERR(tuplehash) != -EAGAIN) {
401 err = PTR_ERR(tuplehash);
402 break;
403 }
ac2a6666
PNA
404 continue;
405 }
406 if (tuplehash->tuple.dir)
407 continue;
408
409 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
410
217cff36 411 iter(flow_table, flow, data);
ac2a6666 412 }
ac2a6666
PNA
413 rhashtable_walk_stop(&hti);
414 rhashtable_walk_exit(&hti);
415
416 return err;
417}
ac2a6666 418
217cff36
PNA
419static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,
420 struct flow_offload *flow, void *data)
ac2a6666 421{
8b9229d1 422 if (nf_flow_has_expired(flow) ||
2738d9d9 423 nf_ct_is_dying(flow->ct))
e5eaac2b 424 flow_offload_teardown(flow);
9ed81c8e
PNA
425
426 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
355a8b13
PNA
427 if (test_bit(NF_FLOW_HW, &flow->flags)) {
428 if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
c29f74e0 429 nf_flow_offload_del(flow_table, flow);
355a8b13 430 else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
c29f74e0
PNA
431 flow_offload_del(flow_table, flow);
432 } else {
433 flow_offload_del(flow_table, flow);
434 }
355a8b13 435 } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
79b9b685 436 nf_flow_offload_stats(flow_table, flow);
c29f74e0 437 }
b408c5b0
PNA
438}
439
759eebbc
PNA
440void nf_flow_table_gc_run(struct nf_flowtable *flow_table)
441{
442 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, NULL);
443}
444
a268de77 445static void nf_flow_offload_work_gc(struct work_struct *work)
b408c5b0
PNA
446{
447 struct nf_flowtable *flow_table;
448
449 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
759eebbc 450 nf_flow_table_gc_run(flow_table);
ac2a6666
PNA
451 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
452}
ac2a6666 453
f4401262
PNA
454static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
455 __be16 port, __be16 new_port)
ac2a6666
PNA
456{
457 struct tcphdr *tcph;
458
ac2a6666 459 tcph = (void *)(skb_network_header(skb) + thoff);
8d6bca15 460 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
ac2a6666
PNA
461}
462
f4401262
PNA
463static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
464 __be16 port, __be16 new_port)
ac2a6666
PNA
465{
466 struct udphdr *udph;
467
ac2a6666
PNA
468 udph = (void *)(skb_network_header(skb) + thoff);
469 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
470 inet_proto_csum_replace2(&udph->check, skb, port,
8d6bca15 471 new_port, false);
ac2a6666
PNA
472 if (!udph->check)
473 udph->check = CSUM_MANGLED_0;
474 }
ac2a6666
PNA
475}
476
f4401262
PNA
477static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
478 u8 protocol, __be16 port, __be16 new_port)
ac2a6666
PNA
479{
480 switch (protocol) {
481 case IPPROTO_TCP:
f4401262 482 nf_flow_nat_port_tcp(skb, thoff, port, new_port);
ac2a6666
PNA
483 break;
484 case IPPROTO_UDP:
f4401262 485 nf_flow_nat_port_udp(skb, thoff, port, new_port);
ac2a6666
PNA
486 break;
487 }
ac2a6666
PNA
488}
489
f4401262
PNA
490void nf_flow_snat_port(const struct flow_offload *flow,
491 struct sk_buff *skb, unsigned int thoff,
492 u8 protocol, enum flow_offload_tuple_dir dir)
ac2a6666
PNA
493{
494 struct flow_ports *hdr;
495 __be16 port, new_port;
496
ac2a6666
PNA
497 hdr = (void *)(skb_network_header(skb) + thoff);
498
499 switch (dir) {
500 case FLOW_OFFLOAD_DIR_ORIGINAL:
501 port = hdr->source;
502 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
503 hdr->source = new_port;
504 break;
505 case FLOW_OFFLOAD_DIR_REPLY:
506 port = hdr->dest;
507 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
508 hdr->dest = new_port;
509 break;
ac2a6666
PNA
510 }
511
f4401262 512 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
ac2a6666
PNA
513}
514EXPORT_SYMBOL_GPL(nf_flow_snat_port);
515
f4401262
PNA
516void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
517 unsigned int thoff, u8 protocol,
518 enum flow_offload_tuple_dir dir)
ac2a6666
PNA
519{
520 struct flow_ports *hdr;
521 __be16 port, new_port;
522
ac2a6666
PNA
523 hdr = (void *)(skb_network_header(skb) + thoff);
524
525 switch (dir) {
526 case FLOW_OFFLOAD_DIR_ORIGINAL:
527 port = hdr->dest;
528 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
529 hdr->dest = new_port;
530 break;
531 case FLOW_OFFLOAD_DIR_REPLY:
532 port = hdr->source;
533 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
534 hdr->source = new_port;
535 break;
ac2a6666
PNA
536 }
537
f4401262 538 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
ac2a6666
PNA
539}
540EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
541
a268de77
FF
542int nf_flow_table_init(struct nf_flowtable *flowtable)
543{
544 int err;
545
740b486a 546 INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
c29f74e0 547 flow_block_init(&flowtable->flow_block);
422c032a 548 init_rwsem(&flowtable->flow_block_lock);
a268de77
FF
549
550 err = rhashtable_init(&flowtable->rhashtable,
551 &nf_flow_offload_rhash_params);
552 if (err < 0)
553 return err;
554
555 queue_delayed_work(system_power_efficient_wq,
556 &flowtable->gc_work, HZ);
557
84453a90
FF
558 mutex_lock(&flowtable_lock);
559 list_add(&flowtable->list, &flowtables);
560 mutex_unlock(&flowtable_lock);
561
a268de77
FF
562 return 0;
563}
564EXPORT_SYMBOL_GPL(nf_flow_table_init);
565
217cff36
PNA
566static void nf_flow_table_do_cleanup(struct nf_flowtable *flow_table,
567 struct flow_offload *flow, void *data)
c0ea1bcb
PNA
568{
569 struct net_device *dev = data;
570
59c466dd
FF
571 if (!dev) {
572 flow_offload_teardown(flow);
c0ea1bcb 573 return;
59c466dd 574 }
b32d2f34
PNA
575
576 if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
a3fb3698
TY
577 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
578 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
445db8d0 579 flow_offload_teardown(flow);
c0ea1bcb
PNA
580}
581
a8284c68
PNA
582void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
583 struct net_device *dev)
c0ea1bcb 584{
84453a90 585 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
c0ea1bcb 586 flush_delayed_work(&flowtable->gc_work);
91bfaa15 587 nf_flow_table_offload_flush(flowtable);
c0ea1bcb
PNA
588}
589
5f1be84a 590void nf_flow_table_cleanup(struct net_device *dev)
c0ea1bcb 591{
84453a90
FF
592 struct nf_flowtable *flowtable;
593
594 mutex_lock(&flowtable_lock);
595 list_for_each_entry(flowtable, &flowtables, list)
a8284c68 596 nf_flow_table_gc_cleanup(flowtable, dev);
84453a90 597 mutex_unlock(&flowtable_lock);
c0ea1bcb
PNA
598}
599EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
600
b408c5b0
PNA
601void nf_flow_table_free(struct nf_flowtable *flow_table)
602{
84453a90
FF
603 mutex_lock(&flowtable_lock);
604 list_del(&flow_table->list);
605 mutex_unlock(&flowtable_lock);
978703f4 606
a268de77 607 cancel_delayed_work_sync(&flow_table->gc_work);
9afb4b27
PNA
608 nf_flow_table_offload_flush(flow_table);
609 /* ... no more pending work after this stage ... */
b408c5b0 610 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
759eebbc 611 nf_flow_table_gc_run(flow_table);
9afb4b27 612 nf_flow_table_offload_flush_cleanup(flow_table);
a268de77 613 rhashtable_destroy(&flow_table->rhashtable);
b408c5b0
PNA
614}
615EXPORT_SYMBOL_GPL(nf_flow_table_free);
616
b0381776
VB
617static int nf_flow_table_init_net(struct net *net)
618{
619 net->ft.stat = alloc_percpu(struct nf_flow_table_stat);
620 return net->ft.stat ? 0 : -ENOMEM;
621}
622
623static void nf_flow_table_fini_net(struct net *net)
624{
625 free_percpu(net->ft.stat);
626}
627
628static int nf_flow_table_pernet_init(struct net *net)
629{
630 int ret;
631
632 ret = nf_flow_table_init_net(net);
633 if (ret < 0)
634 return ret;
635
636 ret = nf_flow_table_init_proc(net);
637 if (ret < 0)
638 goto out_proc;
639
640 return 0;
641
642out_proc:
643 nf_flow_table_fini_net(net);
644 return ret;
645}
646
647static void nf_flow_table_pernet_exit(struct list_head *net_exit_list)
648{
649 struct net *net;
650
651 list_for_each_entry(net, net_exit_list, exit_list) {
652 nf_flow_table_fini_proc(net);
653 nf_flow_table_fini_net(net);
654 }
655}
656
657static struct pernet_operations nf_flow_table_net_ops = {
658 .init = nf_flow_table_pernet_init,
659 .exit_batch = nf_flow_table_pernet_exit,
660};
661
c29f74e0
PNA
662static int __init nf_flow_table_module_init(void)
663{
b0381776
VB
664 int ret;
665
666 ret = register_pernet_subsys(&nf_flow_table_net_ops);
667 if (ret < 0)
668 return ret;
669
670 ret = nf_flow_table_offload_init();
671 if (ret)
672 goto out_offload;
673
674 return 0;
675
676out_offload:
677 unregister_pernet_subsys(&nf_flow_table_net_ops);
678 return ret;
c29f74e0
PNA
679}
680
681static void __exit nf_flow_table_module_exit(void)
682{
683 nf_flow_table_offload_exit();
b0381776 684 unregister_pernet_subsys(&nf_flow_table_net_ops);
c29f74e0
PNA
685}
686
687module_init(nf_flow_table_module_init);
688module_exit(nf_flow_table_module_exit);
689
ac2a6666
PNA
690MODULE_LICENSE("GPL");
691MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
4cacc395 692MODULE_DESCRIPTION("Netfilter flow table module");