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