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