xref: /linux/net/sched/act_ct.c (revision dad19b59da050cb60d3f7023dac2a042a84bf0bd)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* -
3  * net/sched/act_ct.c  Connection Tracking action
4  *
5  * Authors:   Paul Blakey <paulb@mellanox.com>
6  *            Yossi Kuperman <yossiku@mellanox.com>
7  *            Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/pkt_cls.h>
16 #include <linux/if_tunnel.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/rhashtable.h>
20 #include <net/gre.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <net/pkt_cls.h>
24 #include <net/act_api.h>
25 #include <net/ip.h>
26 #include <net/ipv6_frag.h>
27 #include <uapi/linux/tc_act/tc_ct.h>
28 #include <net/tc_act/tc_ct.h>
29 #include <net/tc_wrapper.h>
30 
31 #include <net/netfilter/nf_flow_table.h>
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_acct.h>
37 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
38 #include <net/netfilter/nf_conntrack_act_ct.h>
39 #include <net/netfilter/nf_conntrack_seqadj.h>
40 #include <uapi/linux/netfilter/nf_nat.h>
41 
42 static struct workqueue_struct *act_ct_wq;
43 static struct rhashtable zones_ht;
44 static DEFINE_MUTEX(zones_mutex);
45 
46 struct zones_ht_key {
47 	struct net *net;
48 	u16 zone;
49 };
50 
51 struct tcf_ct_flow_table {
52 	struct rhash_head node; /* In zones tables */
53 
54 	struct rcu_work rwork;
55 	struct nf_flowtable nf_ft;
56 	refcount_t ref;
57 	struct zones_ht_key key;
58 
59 	bool dying;
60 };
61 
62 static const struct rhashtable_params zones_params = {
63 	.head_offset = offsetof(struct tcf_ct_flow_table, node),
64 	.key_offset = offsetof(struct tcf_ct_flow_table, key),
65 	.key_len = offsetofend(struct zones_ht_key, zone),
66 	.automatic_shrinking = true,
67 };
68 
69 static struct flow_action_entry *
70 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
71 {
72 	int i = flow_action->num_entries++;
73 
74 	return &flow_action->entries[i];
75 }
76 
77 static void tcf_ct_add_mangle_action(struct flow_action *action,
78 				     enum flow_action_mangle_base htype,
79 				     u32 offset,
80 				     u32 mask,
81 				     u32 val)
82 {
83 	struct flow_action_entry *entry;
84 
85 	entry = tcf_ct_flow_table_flow_action_get_next(action);
86 	entry->id = FLOW_ACTION_MANGLE;
87 	entry->mangle.htype = htype;
88 	entry->mangle.mask = ~mask;
89 	entry->mangle.offset = offset;
90 	entry->mangle.val = val;
91 }
92 
93 /* The following nat helper functions check if the inverted reverse tuple
94  * (target) is different then the current dir tuple - meaning nat for ports
95  * and/or ip is needed, and add the relevant mangle actions.
96  */
97 static void
98 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
99 				      struct nf_conntrack_tuple target,
100 				      struct flow_action *action)
101 {
102 	if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
103 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
104 					 offsetof(struct iphdr, saddr),
105 					 0xFFFFFFFF,
106 					 be32_to_cpu(target.src.u3.ip));
107 	if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
108 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
109 					 offsetof(struct iphdr, daddr),
110 					 0xFFFFFFFF,
111 					 be32_to_cpu(target.dst.u3.ip));
112 }
113 
114 static void
115 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
116 				   union nf_inet_addr *addr,
117 				   u32 offset)
118 {
119 	int i;
120 
121 	for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
122 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
123 					 i * sizeof(u32) + offset,
124 					 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
125 }
126 
127 static void
128 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
129 				      struct nf_conntrack_tuple target,
130 				      struct flow_action *action)
131 {
132 	if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
133 		tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
134 						   offsetof(struct ipv6hdr,
135 							    saddr));
136 	if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
137 		tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
138 						   offsetof(struct ipv6hdr,
139 							    daddr));
140 }
141 
142 static void
143 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
144 				     struct nf_conntrack_tuple target,
145 				     struct flow_action *action)
146 {
147 	__be16 target_src = target.src.u.tcp.port;
148 	__be16 target_dst = target.dst.u.tcp.port;
149 
150 	if (target_src != tuple->src.u.tcp.port)
151 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
152 					 offsetof(struct tcphdr, source),
153 					 0xFFFF, be16_to_cpu(target_src));
154 	if (target_dst != tuple->dst.u.tcp.port)
155 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
156 					 offsetof(struct tcphdr, dest),
157 					 0xFFFF, be16_to_cpu(target_dst));
158 }
159 
160 static void
161 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
162 				     struct nf_conntrack_tuple target,
163 				     struct flow_action *action)
164 {
165 	__be16 target_src = target.src.u.udp.port;
166 	__be16 target_dst = target.dst.u.udp.port;
167 
168 	if (target_src != tuple->src.u.udp.port)
169 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
170 					 offsetof(struct udphdr, source),
171 					 0xFFFF, be16_to_cpu(target_src));
172 	if (target_dst != tuple->dst.u.udp.port)
173 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
174 					 offsetof(struct udphdr, dest),
175 					 0xFFFF, be16_to_cpu(target_dst));
176 }
177 
178 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
179 					      enum ip_conntrack_dir dir,
180 					      enum ip_conntrack_info ctinfo,
181 					      struct flow_action *action)
182 {
183 	struct nf_conn_labels *ct_labels;
184 	struct flow_action_entry *entry;
185 	u32 *act_ct_labels;
186 
187 	entry = tcf_ct_flow_table_flow_action_get_next(action);
188 	entry->id = FLOW_ACTION_CT_METADATA;
189 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
190 	entry->ct_metadata.mark = READ_ONCE(ct->mark);
191 #endif
192 	/* aligns with the CT reference on the SKB nf_ct_set */
193 	entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
194 	entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL;
195 
196 	act_ct_labels = entry->ct_metadata.labels;
197 	ct_labels = nf_ct_labels_find(ct);
198 	if (ct_labels)
199 		memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
200 	else
201 		memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
202 }
203 
204 static int tcf_ct_flow_table_add_action_nat(struct net *net,
205 					    struct nf_conn *ct,
206 					    enum ip_conntrack_dir dir,
207 					    struct flow_action *action)
208 {
209 	const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
210 	struct nf_conntrack_tuple target;
211 
212 	if (!(ct->status & IPS_NAT_MASK))
213 		return 0;
214 
215 	nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
216 
217 	switch (tuple->src.l3num) {
218 	case NFPROTO_IPV4:
219 		tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
220 						      action);
221 		break;
222 	case NFPROTO_IPV6:
223 		tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
224 						      action);
225 		break;
226 	default:
227 		return -EOPNOTSUPP;
228 	}
229 
230 	switch (nf_ct_protonum(ct)) {
231 	case IPPROTO_TCP:
232 		tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
233 		break;
234 	case IPPROTO_UDP:
235 		tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
236 		break;
237 	default:
238 		return -EOPNOTSUPP;
239 	}
240 
241 	return 0;
242 }
243 
244 static int tcf_ct_flow_table_fill_actions(struct net *net,
245 					  struct flow_offload *flow,
246 					  enum flow_offload_tuple_dir tdir,
247 					  struct nf_flow_rule *flow_rule)
248 {
249 	struct flow_action *action = &flow_rule->rule->action;
250 	int num_entries = action->num_entries;
251 	struct nf_conn *ct = flow->ct;
252 	enum ip_conntrack_info ctinfo;
253 	enum ip_conntrack_dir dir;
254 	int i, err;
255 
256 	switch (tdir) {
257 	case FLOW_OFFLOAD_DIR_ORIGINAL:
258 		dir = IP_CT_DIR_ORIGINAL;
259 		ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ?
260 			IP_CT_ESTABLISHED : IP_CT_NEW;
261 		if (ctinfo == IP_CT_ESTABLISHED)
262 			set_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags);
263 		break;
264 	case FLOW_OFFLOAD_DIR_REPLY:
265 		dir = IP_CT_DIR_REPLY;
266 		ctinfo = IP_CT_ESTABLISHED_REPLY;
267 		break;
268 	default:
269 		return -EOPNOTSUPP;
270 	}
271 
272 	err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
273 	if (err)
274 		goto err_nat;
275 
276 	tcf_ct_flow_table_add_action_meta(ct, dir, ctinfo, action);
277 	return 0;
278 
279 err_nat:
280 	/* Clear filled actions */
281 	for (i = num_entries; i < action->num_entries; i++)
282 		memset(&action->entries[i], 0, sizeof(action->entries[i]));
283 	action->num_entries = num_entries;
284 
285 	return err;
286 }
287 
288 static bool tcf_ct_flow_is_outdated(const struct flow_offload *flow)
289 {
290 	return test_bit(IPS_SEEN_REPLY_BIT, &flow->ct->status) &&
291 	       test_bit(IPS_HW_OFFLOAD_BIT, &flow->ct->status) &&
292 	       !test_bit(NF_FLOW_HW_PENDING, &flow->flags) &&
293 	       !test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags);
294 }
295 
296 static void tcf_ct_flow_table_get_ref(struct tcf_ct_flow_table *ct_ft);
297 
298 static void tcf_ct_nf_get(struct nf_flowtable *ft)
299 {
300 	struct tcf_ct_flow_table *ct_ft =
301 		container_of(ft, struct tcf_ct_flow_table, nf_ft);
302 
303 	tcf_ct_flow_table_get_ref(ct_ft);
304 }
305 
306 static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft);
307 
308 static void tcf_ct_nf_put(struct nf_flowtable *ft)
309 {
310 	struct tcf_ct_flow_table *ct_ft =
311 		container_of(ft, struct tcf_ct_flow_table, nf_ft);
312 
313 	tcf_ct_flow_table_put(ct_ft);
314 }
315 
316 static struct nf_flowtable_type flowtable_ct = {
317 	.gc		= tcf_ct_flow_is_outdated,
318 	.action		= tcf_ct_flow_table_fill_actions,
319 	.get		= tcf_ct_nf_get,
320 	.put		= tcf_ct_nf_put,
321 	.owner		= THIS_MODULE,
322 };
323 
324 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params)
325 {
326 	struct zones_ht_key key = { .net = net, .zone = params->zone };
327 	struct tcf_ct_flow_table *ct_ft;
328 	int err = -ENOMEM;
329 
330 	mutex_lock(&zones_mutex);
331 	rcu_read_lock();
332 	ct_ft = rhashtable_lookup(&zones_ht, &key, zones_params);
333 	if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) {
334 		rcu_read_unlock();
335 		goto out_unlock;
336 	}
337 	rcu_read_unlock();
338 
339 	ct_ft = kzalloc_obj(*ct_ft);
340 	if (!ct_ft)
341 		goto err_alloc;
342 	refcount_set(&ct_ft->ref, 1);
343 
344 	ct_ft->key = key;
345 	err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
346 	if (err)
347 		goto err_insert;
348 
349 	ct_ft->nf_ft.type = &flowtable_ct;
350 	ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD |
351 			      NF_FLOWTABLE_COUNTER;
352 	err = nf_flow_table_init(&ct_ft->nf_ft);
353 	if (err)
354 		goto err_init;
355 	write_pnet(&ct_ft->nf_ft.net, net);
356 
357 	__module_get(THIS_MODULE);
358 out_unlock:
359 	params->ct_ft = ct_ft;
360 	params->nf_ft = &ct_ft->nf_ft;
361 	mutex_unlock(&zones_mutex);
362 
363 	return 0;
364 
365 err_init:
366 	rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
367 err_insert:
368 	kfree(ct_ft);
369 err_alloc:
370 	mutex_unlock(&zones_mutex);
371 	return err;
372 }
373 
374 static void tcf_ct_flow_table_get_ref(struct tcf_ct_flow_table *ct_ft)
375 {
376 	refcount_inc(&ct_ft->ref);
377 }
378 
379 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
380 {
381 	struct tcf_ct_flow_table *ct_ft;
382 	struct flow_block *block;
383 
384 	ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
385 			     rwork);
386 	nf_flow_table_free(&ct_ft->nf_ft);
387 
388 	block = &ct_ft->nf_ft.flow_block;
389 	down_write(&ct_ft->nf_ft.flow_block_lock);
390 	WARN_ON(!list_empty(&block->cb_list));
391 	up_write(&ct_ft->nf_ft.flow_block_lock);
392 	kfree(ct_ft);
393 
394 	module_put(THIS_MODULE);
395 }
396 
397 static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft)
398 {
399 	if (refcount_dec_and_test(&ct_ft->ref)) {
400 		rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
401 		INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
402 		queue_rcu_work(act_ct_wq, &ct_ft->rwork);
403 	}
404 }
405 
406 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,
407 				 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir)
408 {
409 	entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC;
410 	entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir];
411 }
412 
413 static void tcf_ct_flow_ct_ext_ifidx_update(struct flow_offload *entry)
414 {
415 	struct nf_conn_act_ct_ext *act_ct_ext;
416 
417 	act_ct_ext = nf_conn_act_ct_ext_find(entry->ct);
418 	if (act_ct_ext) {
419 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
420 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
421 	}
422 }
423 
424 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
425 				  struct nf_conn *ct,
426 				  bool tcp, bool bidirectional)
427 {
428 	struct nf_conn_act_ct_ext *act_ct_ext;
429 	struct flow_offload *entry;
430 	int err;
431 
432 	if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
433 		return;
434 
435 	/* NULL if ct is dying (raced flush) or the atomic alloc failed. */
436 	entry = flow_offload_alloc(ct);
437 	if (!entry)
438 		goto err_alloc;
439 
440 	if (tcp) {
441 		ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
442 		ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
443 	}
444 	if (bidirectional)
445 		__set_bit(NF_FLOW_HW_BIDIRECTIONAL, &entry->flags);
446 
447 	act_ct_ext = nf_conn_act_ct_ext_find(ct);
448 	if (act_ct_ext) {
449 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
450 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
451 	}
452 
453 	err = flow_offload_add(&ct_ft->nf_ft, entry);
454 	if (err)
455 		goto err_add;
456 
457 	return;
458 
459 err_add:
460 	flow_offload_free(entry);
461 err_alloc:
462 	clear_bit(IPS_OFFLOAD_BIT, &ct->status);
463 }
464 
465 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
466 					   struct nf_conn *ct,
467 					   enum ip_conntrack_info ctinfo)
468 {
469 	bool tcp = false, bidirectional = true;
470 
471 	switch (nf_ct_protonum(ct)) {
472 	case IPPROTO_TCP:
473 		if ((ctinfo != IP_CT_ESTABLISHED &&
474 		     ctinfo != IP_CT_ESTABLISHED_REPLY) ||
475 		    !test_bit(IPS_ASSURED_BIT, &ct->status) ||
476 		    ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
477 			return;
478 
479 		tcp = true;
480 		break;
481 	case IPPROTO_UDP:
482 		if (!nf_ct_is_confirmed(ct))
483 			return;
484 		if (!test_bit(IPS_ASSURED_BIT, &ct->status))
485 			bidirectional = false;
486 		break;
487 #ifdef CONFIG_NF_CT_PROTO_GRE
488 	case IPPROTO_GRE: {
489 		struct nf_conntrack_tuple *tuple;
490 
491 		if ((ctinfo != IP_CT_ESTABLISHED &&
492 		     ctinfo != IP_CT_ESTABLISHED_REPLY) ||
493 		    !test_bit(IPS_ASSURED_BIT, &ct->status) ||
494 		    ct->status & IPS_NAT_MASK)
495 			return;
496 
497 		tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
498 		/* No support for GRE v1 */
499 		if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
500 			return;
501 		break;
502 	}
503 #endif
504 	default:
505 		return;
506 	}
507 
508 	if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
509 	    ct->status & IPS_SEQ_ADJUST)
510 		return;
511 
512 	tcf_ct_flow_table_add(ct_ft, ct, tcp, bidirectional);
513 }
514 
515 static bool
516 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
517 				  struct flow_offload_tuple *tuple,
518 				  struct tcphdr **tcph)
519 {
520 	struct flow_ports *ports;
521 	unsigned int thoff;
522 	struct iphdr *iph;
523 	size_t hdrsize;
524 	u8 ipproto;
525 
526 	if (!pskb_network_may_pull(skb, sizeof(*iph)))
527 		return false;
528 
529 	iph = ip_hdr(skb);
530 	thoff = iph->ihl * 4;
531 
532 	if (ip_is_fragment(iph) ||
533 	    unlikely(thoff != sizeof(struct iphdr)))
534 		return false;
535 
536 	ipproto = iph->protocol;
537 	switch (ipproto) {
538 	case IPPROTO_TCP:
539 		hdrsize = sizeof(struct tcphdr);
540 		break;
541 	case IPPROTO_UDP:
542 		hdrsize = sizeof(*ports);
543 		break;
544 #ifdef CONFIG_NF_CT_PROTO_GRE
545 	case IPPROTO_GRE:
546 		hdrsize = sizeof(struct gre_base_hdr);
547 		break;
548 #endif
549 	default:
550 		return false;
551 	}
552 
553 	if (iph->ttl <= 1)
554 		return false;
555 
556 	if (!pskb_network_may_pull(skb, thoff + hdrsize))
557 		return false;
558 
559 	switch (ipproto) {
560 	case IPPROTO_TCP:
561 		*tcph = (void *)(skb_network_header(skb) + thoff);
562 		fallthrough;
563 	case IPPROTO_UDP:
564 		ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
565 		tuple->src_port = ports->source;
566 		tuple->dst_port = ports->dest;
567 		break;
568 	case IPPROTO_GRE: {
569 		struct gre_base_hdr *greh;
570 
571 		greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
572 		if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
573 			return false;
574 		break;
575 	}
576 	}
577 
578 	iph = ip_hdr(skb);
579 
580 	tuple->src_v4.s_addr = iph->saddr;
581 	tuple->dst_v4.s_addr = iph->daddr;
582 	tuple->l3proto = AF_INET;
583 	tuple->l4proto = ipproto;
584 
585 	return true;
586 }
587 
588 static bool
589 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
590 				  struct flow_offload_tuple *tuple,
591 				  struct tcphdr **tcph)
592 {
593 	struct flow_ports *ports;
594 	struct ipv6hdr *ip6h;
595 	unsigned int thoff;
596 	size_t hdrsize;
597 	u8 nexthdr;
598 
599 	if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
600 		return false;
601 
602 	ip6h = ipv6_hdr(skb);
603 	thoff = sizeof(*ip6h);
604 
605 	nexthdr = ip6h->nexthdr;
606 	switch (nexthdr) {
607 	case IPPROTO_TCP:
608 		hdrsize = sizeof(struct tcphdr);
609 		break;
610 	case IPPROTO_UDP:
611 		hdrsize = sizeof(*ports);
612 		break;
613 #ifdef CONFIG_NF_CT_PROTO_GRE
614 	case IPPROTO_GRE:
615 		hdrsize = sizeof(struct gre_base_hdr);
616 		break;
617 #endif
618 	default:
619 		return false;
620 	}
621 
622 	if (ip6h->hop_limit <= 1)
623 		return false;
624 
625 	if (!pskb_network_may_pull(skb, thoff + hdrsize))
626 		return false;
627 
628 	switch (nexthdr) {
629 	case IPPROTO_TCP:
630 		*tcph = (void *)(skb_network_header(skb) + thoff);
631 		fallthrough;
632 	case IPPROTO_UDP:
633 		ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
634 		tuple->src_port = ports->source;
635 		tuple->dst_port = ports->dest;
636 		break;
637 	case IPPROTO_GRE: {
638 		struct gre_base_hdr *greh;
639 
640 		greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
641 		if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
642 			return false;
643 		break;
644 	}
645 	}
646 
647 	ip6h = ipv6_hdr(skb);
648 
649 	tuple->src_v6 = ip6h->saddr;
650 	tuple->dst_v6 = ip6h->daddr;
651 	tuple->l3proto = AF_INET6;
652 	tuple->l4proto = nexthdr;
653 
654 	return true;
655 }
656 
657 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
658 				     struct sk_buff *skb,
659 				     u8 family)
660 {
661 	struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
662 	struct flow_offload_tuple_rhash *tuplehash;
663 	struct flow_offload_tuple tuple = {};
664 	enum ip_conntrack_info ctinfo;
665 	struct tcphdr *tcph = NULL;
666 	bool force_refresh = false;
667 	struct flow_offload *flow;
668 	struct nf_conn *ct;
669 	u8 dir;
670 
671 	switch (family) {
672 	case NFPROTO_IPV4:
673 		if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
674 			return false;
675 		break;
676 	case NFPROTO_IPV6:
677 		if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
678 			return false;
679 		break;
680 	default:
681 		return false;
682 	}
683 
684 	tuplehash = flow_offload_lookup(nf_ft, &tuple);
685 	if (!tuplehash)
686 		return false;
687 
688 	dir = tuplehash->tuple.dir;
689 	flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
690 	ct = flow->ct;
691 
692 	if (dir == FLOW_OFFLOAD_DIR_REPLY &&
693 	    !test_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags)) {
694 		/* Only offload reply direction after connection became
695 		 * assured.
696 		 */
697 		if (test_bit(IPS_ASSURED_BIT, &ct->status))
698 			set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags);
699 		else if (test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags))
700 			/* If flow_table flow has already been updated to the
701 			 * established state, then don't refresh.
702 			 */
703 			return false;
704 		force_refresh = true;
705 	}
706 
707 	if (tcph && (unlikely(tcph->fin || tcph->rst))) {
708 		flow_offload_teardown(flow);
709 		return false;
710 	}
711 
712 	if (dir == FLOW_OFFLOAD_DIR_ORIGINAL)
713 		ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ?
714 			IP_CT_ESTABLISHED : IP_CT_NEW;
715 	else
716 		ctinfo = IP_CT_ESTABLISHED_REPLY;
717 
718 	nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
719 	tcf_ct_flow_ct_ext_ifidx_update(flow);
720 	flow_offload_refresh(nf_ft, flow, force_refresh);
721 	if (!test_bit(IPS_ASSURED_BIT, &ct->status)) {
722 		/* Process this flow in SW to allow promoting to ASSURED */
723 		return false;
724 	}
725 
726 	nf_conntrack_get(&ct->ct_general);
727 	nf_ct_set(skb, ct, ctinfo);
728 	if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
729 		nf_ct_acct_update(ct, dir, skb->len);
730 
731 	return true;
732 }
733 
734 static int tcf_ct_flow_tables_init(void)
735 {
736 	return rhashtable_init(&zones_ht, &zones_params);
737 }
738 
739 static void tcf_ct_flow_tables_uninit(void)
740 {
741 	rhashtable_destroy(&zones_ht);
742 }
743 
744 static struct tc_action_ops act_ct_ops;
745 
746 struct tc_ct_action_net {
747 	struct tc_action_net tn; /* Must be first */
748 };
749 
750 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
751 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
752 				   struct tcf_ct_params *p)
753 {
754 	enum ip_conntrack_info ctinfo;
755 	struct nf_conn *ct;
756 
757 	ct = nf_ct_get(skb, &ctinfo);
758 	if (!ct)
759 		return false;
760 	if (!net_eq(net, read_pnet(&ct->ct_net)))
761 		goto drop_ct;
762 	if (nf_ct_zone(ct)->id != p->zone)
763 		goto drop_ct;
764 	if (p->helper) {
765 		struct nf_conn_help *help;
766 
767 		help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
768 		if (help && rcu_access_pointer(help->helper) != p->helper)
769 			goto drop_ct;
770 	}
771 
772 	/* Force conntrack entry direction. */
773 	if ((p->ct_action & TCA_CT_ACT_FORCE) &&
774 	    CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
775 		if (nf_ct_is_confirmed(ct))
776 			nf_ct_kill(ct);
777 
778 		goto drop_ct;
779 	}
780 
781 	return true;
782 
783 drop_ct:
784 	nf_reset_ct(skb);
785 	nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
786 
787 	return false;
788 }
789 
790 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
791 {
792 	u8 family = NFPROTO_UNSPEC;
793 
794 	switch (skb_protocol(skb, true)) {
795 	case htons(ETH_P_IP):
796 		family = NFPROTO_IPV4;
797 		break;
798 	case htons(ETH_P_IPV6):
799 		family = NFPROTO_IPV6;
800 		break;
801 	default:
802 		break;
803 	}
804 
805 	return family;
806 }
807 
808 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
809 {
810 	unsigned int len;
811 
812 	len =  skb_network_offset(skb) + sizeof(struct iphdr);
813 	if (unlikely(skb->len < len))
814 		return -EINVAL;
815 	if (unlikely(!pskb_may_pull(skb, len)))
816 		return -ENOMEM;
817 
818 	*frag = ip_is_fragment(ip_hdr(skb));
819 	return 0;
820 }
821 
822 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
823 {
824 	unsigned int flags = 0, len, payload_ofs = 0;
825 	unsigned short frag_off;
826 	int nexthdr;
827 
828 	len =  skb_network_offset(skb) + sizeof(struct ipv6hdr);
829 	if (unlikely(skb->len < len))
830 		return -EINVAL;
831 	if (unlikely(!pskb_may_pull(skb, len)))
832 		return -ENOMEM;
833 
834 	nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
835 	if (unlikely(nexthdr < 0))
836 		return -EPROTO;
837 
838 	*frag = flags & IP6_FH_F_FRAG;
839 	return 0;
840 }
841 
842 /* On error, tells the caller whether it still owns @skb and must free it
843  * itself.  @skb is ours only when the header checks below reject the packet
844  * before it is handed to the defragmentation engine; once nf_ct_handle_
845  * fragments() has been called the skb is either queued (-EINPROGRESS) or has
846  * already been freed by it.
847  */
848 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
849 				   u8 family, u16 zone, bool *defrag,
850 				   bool *skb_is_ours)
851 {
852 	enum ip_conntrack_info ctinfo;
853 	struct tc_skb_cb cb;
854 	struct nf_conn *ct;
855 	int err = 0;
856 	bool frag;
857 	u8 proto;
858 
859 	/* Previously seen (loopback)? Ignore. */
860 	ct = nf_ct_get(skb, &ctinfo);
861 	if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
862 		return 0;
863 
864 	if (family == NFPROTO_IPV4)
865 		err = tcf_ct_ipv4_is_fragment(skb, &frag);
866 	else
867 		err = tcf_ct_ipv6_is_fragment(skb, &frag);
868 	if (err) {
869 		*skb_is_ours = true;
870 		return err;
871 	}
872 	if (!frag)
873 		return 0;
874 
875 	cb = *tc_skb_cb(skb);
876 	err = nf_ct_handle_fragments(net, skb, zone, family, &proto, &cb.mru);
877 	if (err)
878 		return err;
879 
880 	*defrag = true;
881 	*tc_skb_cb(skb) = cb;
882 
883 	return 0;
884 }
885 
886 static void tcf_ct_params_free(struct tcf_ct_params *params)
887 {
888 	if (params->helper) {
889 #if IS_ENABLED(CONFIG_NF_NAT)
890 		if (params->ct_action & TCA_CT_ACT_NAT)
891 			nf_nat_helper_put(params->helper);
892 #endif
893 		nf_conntrack_helper_put(params->helper);
894 	}
895 	if (params->ct_ft)
896 		tcf_ct_flow_table_put(params->ct_ft);
897 	if (params->tmpl) {
898 		if (params->put_labels)
899 			nf_connlabels_put(nf_ct_net(params->tmpl));
900 
901 		nf_ct_put(params->tmpl);
902 	}
903 
904 	kfree(params);
905 }
906 
907 static void tcf_ct_params_free_rcu(struct rcu_head *head)
908 {
909 	struct tcf_ct_params *params;
910 
911 	params = container_of(head, struct tcf_ct_params, rcu);
912 	tcf_ct_params_free(params);
913 }
914 
915 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
916 {
917 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
918 	u32 new_mark;
919 
920 	if (!mask)
921 		return;
922 
923 	new_mark = mark | (READ_ONCE(ct->mark) & ~(mask));
924 	if (READ_ONCE(ct->mark) != new_mark) {
925 		WRITE_ONCE(ct->mark, new_mark);
926 		if (nf_ct_is_confirmed(ct))
927 			nf_conntrack_event_cache(IPCT_MARK, ct);
928 	}
929 #endif
930 }
931 
932 static void tcf_ct_act_set_labels(struct nf_conn *ct,
933 				  u32 *labels,
934 				  u32 *labels_m)
935 {
936 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
937 	size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
938 
939 	if (!memchr_inv(labels_m, 0, labels_sz))
940 		return;
941 
942 	nf_connlabels_replace(ct, labels, labels_m, 4);
943 #endif
944 }
945 
946 static int tcf_ct_act_nat(struct sk_buff *skb,
947 			  struct nf_conn *ct,
948 			  enum ip_conntrack_info ctinfo,
949 			  int ct_action,
950 			  struct nf_nat_range2 *range,
951 			  bool commit)
952 {
953 #if IS_ENABLED(CONFIG_NF_NAT)
954 	int err, action = 0;
955 
956 	if (!(ct_action & TCA_CT_ACT_NAT))
957 		return NF_ACCEPT;
958 	if (ct_action & TCA_CT_ACT_NAT_SRC)
959 		action |= BIT(NF_NAT_MANIP_SRC);
960 	if (ct_action & TCA_CT_ACT_NAT_DST)
961 		action |= BIT(NF_NAT_MANIP_DST);
962 
963 	err = nf_ct_nat(skb, ct, ctinfo, &action, range, commit);
964 	if (err != NF_ACCEPT)
965 		return err & NF_VERDICT_MASK;
966 
967 	if (action & BIT(NF_NAT_MANIP_SRC))
968 		qdisc_skb_cb(skb)->post_ct_snat = 1;
969 	if (action & BIT(NF_NAT_MANIP_DST))
970 		qdisc_skb_cb(skb)->post_ct_dnat = 1;
971 
972 	return err;
973 #else
974 	return NF_ACCEPT;
975 #endif
976 }
977 
978 TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
979 				 struct tcf_result *res)
980 {
981 	struct net *net = dev_net(skb->dev);
982 	bool cached, commit, clear, nat;
983 	enum ip_conntrack_info ctinfo;
984 	struct tcf_ct *c = to_ct(a);
985 	struct nf_conn *tmpl = NULL;
986 	struct nf_hook_state state;
987 	int nh_ofs, err, retval;
988 	struct tcf_ct_params *p;
989 	bool skb_is_ours = false;
990 	bool skip_add = false;
991 	bool defrag = false;
992 	struct nf_conn *ct;
993 	u8 family;
994 
995 	p = rcu_dereference_bh(c->params);
996 
997 	retval = p->action;
998 	commit = p->ct_action & TCA_CT_ACT_COMMIT;
999 	clear = p->ct_action & TCA_CT_ACT_CLEAR;
1000 	nat = p->ct_action & TCA_CT_ACT_NAT;
1001 	tmpl = p->tmpl;
1002 
1003 	tcf_lastuse_update(&c->tcf_tm);
1004 	tcf_action_update_bstats(&c->common, skb);
1005 
1006 	if (clear) {
1007 		qdisc_skb_cb(skb)->post_ct = false;
1008 		ct = nf_ct_get(skb, &ctinfo);
1009 		if (ct) {
1010 			nf_reset_ct(skb);
1011 			nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1012 		}
1013 
1014 		goto out_clear;
1015 	}
1016 
1017 	family = tcf_ct_skb_nf_family(skb);
1018 	if (family == NFPROTO_UNSPEC)
1019 		goto drop;
1020 
1021 	/* The conntrack module expects to be working at L3.
1022 	 * We also try to pull the IPv4/6 header to linear area
1023 	 */
1024 	nh_ofs = skb_network_offset(skb);
1025 	skb_pull_rcsum(skb, nh_ofs);
1026 	err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag,
1027 				      &skb_is_ours);
1028 	if (err) {
1029 		/* The skb is still ours only when the header checks rejected
1030 		 * it; returning TC_ACT_CONSUMED for such a packet would leak
1031 		 * it, since no caller frees an skb it was told it no longer
1032 		 * owns.
1033 		 */
1034 		if (skb_is_ours)
1035 			goto drop;
1036 		goto out_frag;
1037 	}
1038 
1039 	err = nf_ct_skb_network_trim(skb, family);
1040 	if (err)
1041 		goto drop;
1042 
1043 	/* If we are recirculating packets to match on ct fields and
1044 	 * committing with a separate ct action, then we don't need to
1045 	 * actually run the packet through conntrack twice unless it's for a
1046 	 * different zone.
1047 	 */
1048 	cached = tcf_ct_skb_nfct_cached(net, skb, p);
1049 
1050 	/* If the ct entry is not confirmed and shared with some other skb,
1051 	 * e.g., a cloned one, we can't just modify it with a commit or nat
1052 	 * as we must not modify the extension set.  Reset.
1053 	 */
1054 	if (cached && (commit || nat)) {
1055 		ct = nf_ct_get(skb, &ctinfo);
1056 		if (ct && !nf_ct_is_confirmed(ct) && nf_ct_shared(ct)) {
1057 			nf_reset_ct(skb);
1058 			cached = false;
1059 		}
1060 	}
1061 
1062 	if (!cached) {
1063 		if (tcf_ct_flow_table_lookup(p, skb, family)) {
1064 			skip_add = true;
1065 			goto do_nat;
1066 		}
1067 
1068 		/* Associate skb with specified zone. */
1069 		if (tmpl) {
1070 			nf_reset_ct(skb);
1071 			nf_conntrack_get(&tmpl->ct_general);
1072 			nf_ct_set(skb, tmpl, IP_CT_NEW);
1073 		}
1074 
1075 		state.hook = NF_INET_PRE_ROUTING;
1076 		state.net = net;
1077 		state.pf = family;
1078 		err = nf_conntrack_in(skb, &state);
1079 		if (err != NF_ACCEPT)
1080 			goto nf_error;
1081 	}
1082 
1083 do_nat:
1084 	ct = nf_ct_get(skb, &ctinfo);
1085 	if (!ct)
1086 		goto out_push;
1087 	nf_ct_deliver_cached_events(ct);
1088 	nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
1089 
1090 	err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1091 	if (err != NF_ACCEPT)
1092 		goto nf_error;
1093 
1094 	if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) {
1095 		err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
1096 		if (err)
1097 			goto drop;
1098 
1099 		if (nat && !nfct_seqadj(ct)) {
1100 			if (!nfct_seqadj_ext_add(ct))
1101 				goto drop;
1102 		}
1103 	}
1104 
1105 	if (commit) {
1106 		tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1107 		tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1108 
1109 		if (!nf_ct_is_confirmed(ct))
1110 			nf_conn_act_ct_ext_add(skb, ct, ctinfo);
1111 	}
1112 
1113 	/* Run helpers for the connection if nf_conntrack_in() was executed
1114 	 * or if we're about to commit.  This has to be done after all the
1115 	 * extensions are already added.
1116 	 */
1117 	if (nf_ct_is_confirmed(ct) ? (!cached && !skip_add) : commit) {
1118 		err = nf_ct_helper(skb, ct, ctinfo, family);
1119 		if (err != NF_ACCEPT)
1120 			goto nf_error;
1121 	}
1122 
1123 	if (commit) {
1124 		/* This will take care of sending queued events
1125 		 * even if the connection is already confirmed.
1126 		 */
1127 		err = nf_conntrack_confirm(skb);
1128 		if (err != NF_ACCEPT)
1129 			goto nf_error;
1130 
1131 		/* The ct may be dropped if a clash has been resolved,
1132 		 * so it's necessary to retrieve it from skb again to
1133 		 * prevent UAF.
1134 		 */
1135 		ct = nf_ct_get(skb, &ctinfo);
1136 		if (!ct)
1137 			skip_add = true;
1138 	}
1139 
1140 	if (!skip_add)
1141 		tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1142 
1143 out_push:
1144 	skb_push_rcsum(skb, nh_ofs);
1145 
1146 	qdisc_skb_cb(skb)->post_ct = true;
1147 	tc_skb_cb(skb)->zone = p->zone;
1148 out_clear:
1149 	if (defrag)
1150 		qdisc_skb_cb(skb)->pkt_len = skb->len;
1151 	return retval;
1152 
1153 out_frag:
1154 	if (err != -EINPROGRESS)
1155 		tcf_action_inc_drop_qstats(&c->common);
1156 	return TC_ACT_CONSUMED;
1157 
1158 drop:
1159 	tcf_action_inc_drop_qstats(&c->common);
1160 	return TC_ACT_SHOT;
1161 
1162 nf_error:
1163 	/* some verdicts store extra data in upper bits, such
1164 	 * as errno or queue number.
1165 	 */
1166 	switch (err & NF_VERDICT_MASK) {
1167 	case NF_DROP:
1168 		goto drop;
1169 	case NF_STOLEN:
1170 		tcf_action_inc_drop_qstats(&c->common);
1171 		return TC_ACT_CONSUMED;
1172 	default:
1173 		DEBUG_NET_WARN_ON_ONCE(1);
1174 		goto drop;
1175 	}
1176 }
1177 
1178 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1179 	[TCA_CT_ACTION] = { .type = NLA_U16 },
1180 	[TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1181 	[TCA_CT_ZONE] = { .type = NLA_U16 },
1182 	[TCA_CT_MARK] = { .type = NLA_U32 },
1183 	[TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1184 	[TCA_CT_LABELS] = { .type = NLA_BINARY,
1185 			    .len = 128 / BITS_PER_BYTE },
1186 	[TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1187 				 .len = 128 / BITS_PER_BYTE },
1188 	[TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1189 	[TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1190 	[TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1191 	[TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1192 	[TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1193 	[TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1194 	[TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN },
1195 	[TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 },
1196 	[TCA_CT_HELPER_PROTO] = { .type = NLA_U8 },
1197 };
1198 
1199 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1200 				  struct tc_ct *parm,
1201 				  struct nlattr **tb,
1202 				  struct netlink_ext_ack *extack)
1203 {
1204 	struct nf_nat_range2 *range;
1205 
1206 	if (!(p->ct_action & TCA_CT_ACT_NAT))
1207 		return 0;
1208 
1209 	if (!IS_ENABLED(CONFIG_NF_NAT)) {
1210 		NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1211 		return -EOPNOTSUPP;
1212 	}
1213 
1214 	if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1215 		return 0;
1216 
1217 	if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1218 	    (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1219 		NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1220 		return -EOPNOTSUPP;
1221 	}
1222 
1223 	range = &p->range;
1224 	if (tb[TCA_CT_NAT_IPV4_MIN]) {
1225 		struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1226 
1227 		p->ipv4_range = true;
1228 		range->flags |= NF_NAT_RANGE_MAP_IPS;
1229 		range->min_addr.ip =
1230 			nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1231 
1232 		range->max_addr.ip =
1233 			nla_get_in_addr_default(max_attr, range->min_addr.ip);
1234 	} else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1235 		struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1236 
1237 		p->ipv4_range = false;
1238 		range->flags |= NF_NAT_RANGE_MAP_IPS;
1239 		range->min_addr.in6 =
1240 			nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1241 
1242 		range->max_addr.in6 = max_attr ?
1243 				      nla_get_in6_addr(max_attr) :
1244 				      range->min_addr.in6;
1245 	}
1246 
1247 	if (tb[TCA_CT_NAT_PORT_MIN]) {
1248 		range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1249 		range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1250 
1251 		range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1252 				       nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1253 				       range->min_proto.all;
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static void tcf_ct_set_key_val(struct nlattr **tb,
1260 			       void *val, int val_type,
1261 			       void *mask, int mask_type,
1262 			       int len)
1263 {
1264 	if (!tb[val_type])
1265 		return;
1266 	nla_memcpy(val, tb[val_type], len);
1267 
1268 	if (!mask)
1269 		return;
1270 
1271 	if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1272 		memset(mask, 0xff, len);
1273 	else
1274 		nla_memcpy(mask, tb[mask_type], len);
1275 }
1276 
1277 static int tcf_ct_fill_params(struct net *net,
1278 			      struct tcf_ct_params *p,
1279 			      struct tc_ct *parm,
1280 			      struct nlattr **tb,
1281 			      struct netlink_ext_ack *extack)
1282 {
1283 	struct nf_conntrack_zone zone;
1284 	int err, family, proto, len;
1285 	bool put_labels = false;
1286 	struct nf_conn *tmpl;
1287 	char *name;
1288 
1289 	p->zone = NF_CT_DEFAULT_ZONE_ID;
1290 
1291 	tcf_ct_set_key_val(tb,
1292 			   &p->ct_action, TCA_CT_ACTION,
1293 			   NULL, TCA_CT_UNSPEC,
1294 			   sizeof(p->ct_action));
1295 
1296 	if (p->ct_action & TCA_CT_ACT_CLEAR)
1297 		return 0;
1298 
1299 	err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1300 	if (err)
1301 		return err;
1302 
1303 	if (tb[TCA_CT_MARK]) {
1304 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1305 			NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1306 			return -EOPNOTSUPP;
1307 		}
1308 		tcf_ct_set_key_val(tb,
1309 				   &p->mark, TCA_CT_MARK,
1310 				   &p->mark_mask, TCA_CT_MARK_MASK,
1311 				   sizeof(p->mark));
1312 	}
1313 
1314 	if (tb[TCA_CT_LABELS]) {
1315 		unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1316 
1317 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1318 			NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1319 			return -EOPNOTSUPP;
1320 		}
1321 
1322 		if (nf_connlabels_get(net, n_bits - 1)) {
1323 			NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1324 			return -EOPNOTSUPP;
1325 		} else {
1326 			put_labels = true;
1327 		}
1328 
1329 		tcf_ct_set_key_val(tb,
1330 				   p->labels, TCA_CT_LABELS,
1331 				   p->labels_mask, TCA_CT_LABELS_MASK,
1332 				   sizeof(p->labels));
1333 	}
1334 
1335 	if (tb[TCA_CT_ZONE]) {
1336 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1337 			NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1338 			err = -EOPNOTSUPP;
1339 			goto err;
1340 		}
1341 
1342 		tcf_ct_set_key_val(tb,
1343 				   &p->zone, TCA_CT_ZONE,
1344 				   NULL, TCA_CT_UNSPEC,
1345 				   sizeof(p->zone));
1346 	}
1347 
1348 	nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1349 	tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1350 	if (!tmpl) {
1351 		NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1352 		err = -ENOMEM;
1353 		goto err;
1354 	}
1355 	p->tmpl = tmpl;
1356 	if (tb[TCA_CT_HELPER_NAME]) {
1357 		name = nla_data(tb[TCA_CT_HELPER_NAME]);
1358 		len = nla_len(tb[TCA_CT_HELPER_NAME]);
1359 		if (len > 16 || name[len - 1] != '\0') {
1360 			NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name.");
1361 			err = -EINVAL;
1362 			goto err;
1363 		}
1364 		family = nla_get_u8_default(tb[TCA_CT_HELPER_FAMILY], AF_INET);
1365 		proto = nla_get_u8_default(tb[TCA_CT_HELPER_PROTO],
1366 					   IPPROTO_TCP);
1367 		err = nf_ct_add_helper(tmpl, name, family, proto,
1368 				       p->ct_action & TCA_CT_ACT_NAT, &p->helper);
1369 		if (err) {
1370 			NL_SET_ERR_MSG_MOD(extack, "Failed to add helper");
1371 			goto err;
1372 		}
1373 	}
1374 
1375 	p->put_labels = put_labels;
1376 
1377 	if (p->ct_action & TCA_CT_ACT_COMMIT)
1378 		__set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1379 	return 0;
1380 err:
1381 	if (put_labels)
1382 		nf_connlabels_put(net);
1383 
1384 	nf_ct_put(p->tmpl);
1385 	p->tmpl = NULL;
1386 	return err;
1387 }
1388 
1389 static int tcf_ct_init(struct net *net, struct nlattr *nla,
1390 		       struct nlattr *est, struct tc_action **a,
1391 		       struct tcf_proto *tp, u32 flags,
1392 		       struct netlink_ext_ack *extack)
1393 {
1394 	struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id);
1395 	bool bind = flags & TCA_ACT_FLAGS_BIND;
1396 	struct tcf_ct_params *params = NULL;
1397 	struct nlattr *tb[TCA_CT_MAX + 1];
1398 	struct tcf_chain *goto_ch = NULL;
1399 	struct tc_ct *parm;
1400 	struct tcf_ct *c;
1401 	int err, res = 0;
1402 	u32 index;
1403 
1404 	if (!nla) {
1405 		NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1406 		return -EINVAL;
1407 	}
1408 
1409 	if (bind && !(flags & TCA_ACT_FLAGS_AT_INGRESS_OR_CLSACT)) {
1410 		NL_SET_ERR_MSG_MOD(extack,
1411 				   "Attaching ct to a non ingress/clsact qdisc is unsupported");
1412 		return -EOPNOTSUPP;
1413 	}
1414 
1415 	err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1416 	if (err < 0)
1417 		return err;
1418 
1419 	if (!tb[TCA_CT_PARMS]) {
1420 		NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1421 		return -EINVAL;
1422 	}
1423 	parm = nla_data(tb[TCA_CT_PARMS]);
1424 	index = parm->index;
1425 	err = tcf_idr_check_alloc(tn, &index, a, bind);
1426 	if (err < 0)
1427 		return err;
1428 
1429 	if (!err) {
1430 		err = tcf_idr_create_from_flags(tn, index, est, a,
1431 						&act_ct_ops, bind, flags);
1432 		if (err) {
1433 			tcf_idr_cleanup(tn, index);
1434 			return err;
1435 		}
1436 		res = ACT_P_CREATED;
1437 	} else {
1438 		if (bind)
1439 			return ACT_P_BOUND;
1440 
1441 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
1442 			tcf_idr_release(*a, bind);
1443 			return -EEXIST;
1444 		}
1445 	}
1446 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1447 	if (err < 0)
1448 		goto cleanup;
1449 
1450 	c = to_ct(*a);
1451 
1452 	params = kzalloc_obj(*params);
1453 	if (unlikely(!params)) {
1454 		err = -ENOMEM;
1455 		goto cleanup;
1456 	}
1457 
1458 	err = tcf_ct_fill_params(net, params, parm, tb, extack);
1459 	if (err)
1460 		goto cleanup;
1461 
1462 	err = tcf_ct_flow_table_get(net, params);
1463 	if (err)
1464 		goto cleanup;
1465 
1466 	params->action = parm->action;
1467 	spin_lock_bh(&c->tcf_lock);
1468 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1469 	params = rcu_replace_pointer(c->params, params,
1470 				     lockdep_is_held(&c->tcf_lock));
1471 	spin_unlock_bh(&c->tcf_lock);
1472 
1473 	if (goto_ch)
1474 		tcf_chain_put_by_act(goto_ch);
1475 	if (params)
1476 		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
1477 
1478 	return res;
1479 
1480 cleanup:
1481 	if (goto_ch)
1482 		tcf_chain_put_by_act(goto_ch);
1483 	if (params)
1484 		tcf_ct_params_free(params);
1485 	tcf_idr_release(*a, bind);
1486 	return err;
1487 }
1488 
1489 static void tcf_ct_cleanup(struct tc_action *a)
1490 {
1491 	struct tcf_ct_params *params;
1492 	struct tcf_ct *c = to_ct(a);
1493 
1494 	params = rcu_dereference_protected(c->params, 1);
1495 	if (params)
1496 		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
1497 }
1498 
1499 static int tcf_ct_dump_key_val(struct sk_buff *skb,
1500 			       const void *val, int val_type,
1501 			       const void *mask, int mask_type,
1502 			       int len)
1503 {
1504 	int err;
1505 
1506 	if (mask && !memchr_inv(mask, 0, len))
1507 		return 0;
1508 
1509 	err = nla_put(skb, val_type, len, val);
1510 	if (err)
1511 		return err;
1512 
1513 	if (mask_type != TCA_CT_UNSPEC) {
1514 		err = nla_put(skb, mask_type, len, mask);
1515 		if (err)
1516 			return err;
1517 	}
1518 
1519 	return 0;
1520 }
1521 
1522 static int tcf_ct_dump_nat(struct sk_buff *skb, const struct tcf_ct_params *p)
1523 {
1524 	const struct nf_nat_range2 *range = &p->range;
1525 
1526 	if (!(p->ct_action & TCA_CT_ACT_NAT))
1527 		return 0;
1528 
1529 	if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1530 		return 0;
1531 
1532 	if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1533 		if (p->ipv4_range) {
1534 			if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1535 					    range->min_addr.ip))
1536 				return -1;
1537 			if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1538 					    range->max_addr.ip))
1539 				return -1;
1540 		} else {
1541 			if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1542 					     &range->min_addr.in6))
1543 				return -1;
1544 			if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1545 					     &range->max_addr.in6))
1546 				return -1;
1547 		}
1548 	}
1549 
1550 	if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1551 		if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1552 				 range->min_proto.all))
1553 			return -1;
1554 		if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1555 				 range->max_proto.all))
1556 			return -1;
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 static int tcf_ct_dump_helper(struct sk_buff *skb,
1563 			      const struct nf_conntrack_helper *helper)
1564 {
1565 	if (!helper)
1566 		return 0;
1567 
1568 	if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) ||
1569 	    nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->nfproto) ||
1570 	    nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->l4proto))
1571 		return -1;
1572 
1573 	return 0;
1574 }
1575 
1576 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1577 			      int bind, int ref)
1578 {
1579 	unsigned char *b = skb_tail_pointer(skb);
1580 	const struct tcf_ct *c = to_ct(a);
1581 	const struct tcf_ct_params *p;
1582 	struct tc_ct opt = {
1583 		.index   = c->tcf_index,
1584 		.refcnt  = refcount_read(&c->tcf_refcnt) - ref,
1585 		.bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1586 	};
1587 	struct tcf_t t;
1588 
1589 	rcu_read_lock();
1590 	p = rcu_dereference(c->params);
1591 	opt.action = p->action;
1592 
1593 	if (tcf_ct_dump_key_val(skb,
1594 				&p->ct_action, TCA_CT_ACTION,
1595 				NULL, TCA_CT_UNSPEC,
1596 				sizeof(p->ct_action)))
1597 		goto nla_put_failure;
1598 
1599 	if (p->ct_action & TCA_CT_ACT_CLEAR)
1600 		goto skip_dump;
1601 
1602 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1603 	    tcf_ct_dump_key_val(skb,
1604 				&p->mark, TCA_CT_MARK,
1605 				&p->mark_mask, TCA_CT_MARK_MASK,
1606 				sizeof(p->mark)))
1607 		goto nla_put_failure;
1608 
1609 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1610 	    tcf_ct_dump_key_val(skb,
1611 				p->labels, TCA_CT_LABELS,
1612 				p->labels_mask, TCA_CT_LABELS_MASK,
1613 				sizeof(p->labels)))
1614 		goto nla_put_failure;
1615 
1616 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1617 	    tcf_ct_dump_key_val(skb,
1618 				&p->zone, TCA_CT_ZONE,
1619 				NULL, TCA_CT_UNSPEC,
1620 				sizeof(p->zone)))
1621 		goto nla_put_failure;
1622 
1623 	if (tcf_ct_dump_nat(skb, p))
1624 		goto nla_put_failure;
1625 
1626 	if (tcf_ct_dump_helper(skb, p->helper))
1627 		goto nla_put_failure;
1628 
1629 skip_dump:
1630 	if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1631 		goto nla_put_failure;
1632 
1633 	tcf_tm_dump(&t, &c->tcf_tm);
1634 	if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1635 		goto nla_put_failure;
1636 	rcu_read_unlock();
1637 
1638 	return skb->len;
1639 nla_put_failure:
1640 	rcu_read_unlock();
1641 	nlmsg_trim(skb, b);
1642 	return -1;
1643 }
1644 
1645 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1646 			     u64 drops, u64 lastuse, bool hw)
1647 {
1648 	struct tcf_ct *c = to_ct(a);
1649 
1650 	tcf_action_update_stats(a, bytes, packets, drops, hw);
1651 	c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1652 }
1653 
1654 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data,
1655 				    u32 *index_inc, bool bind,
1656 				    struct netlink_ext_ack *extack)
1657 {
1658 	if (bind) {
1659 		struct flow_action_entry *entry = entry_data;
1660 
1661 		if (tcf_ct_helper(act))
1662 			return -EOPNOTSUPP;
1663 
1664 		entry->id = FLOW_ACTION_CT;
1665 		entry->ct.action = tcf_ct_action(act);
1666 		entry->ct.zone = tcf_ct_zone(act);
1667 		entry->ct.flow_table = tcf_ct_ft(act);
1668 		*index_inc = 1;
1669 	} else {
1670 		struct flow_offload_action *fl_action = entry_data;
1671 
1672 		fl_action->id = FLOW_ACTION_CT;
1673 	}
1674 
1675 	return 0;
1676 }
1677 
1678 static size_t tcf_ct_get_fill_size(const struct tc_action *act)
1679 {
1680 	const struct tcf_ct_params *p;
1681 	size_t size;
1682 
1683 	size = nla_total_size(sizeof(struct tc_ct)) /* TCA_CT_PARMS */
1684 		+ nla_total_size(sizeof(u16)); /* TCA_CT_ACTION */
1685 
1686 	rcu_read_lock();
1687 	p = rcu_dereference(to_ct(act)->params);
1688 
1689 	if (p->ct_action & TCA_CT_ACT_CLEAR)
1690 		goto out;
1691 
1692 	/* TCA_CT_MARK, TCA_CT_MARK_MASK */
1693 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK))
1694 		size += nla_total_size(sizeof(p->mark))
1695 			+ nla_total_size(sizeof(p->mark_mask));
1696 
1697 	/* TCA_CT_LABELS, TCA_CT_LABELS_MASK */
1698 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS))
1699 		size += nla_total_size(sizeof(p->labels))
1700 			+ nla_total_size(sizeof(p->labels_mask));
1701 
1702 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES))
1703 		size += nla_total_size(sizeof(p->zone)); /* TCA_CT_ZONE */
1704 
1705 	if (p->ct_action & TCA_CT_ACT_NAT)
1706 		/* TCA_CT_NAT_IPV6_{MIN,MAX}, the larger of the two address
1707 		 * variants, plus TCA_CT_NAT_PORT_{MIN,MAX}.
1708 		 */
1709 		size += 2 * nla_total_size(sizeof(struct in6_addr))
1710 			+ 2 * nla_total_size(sizeof(__be16));
1711 
1712 	/* TCA_CT_HELPER_{NAME,FAMILY,PROTO} */
1713 	if (p->helper)
1714 		size += nla_total_size(NF_CT_HELPER_NAME_LEN)
1715 			+ nla_total_size(sizeof(u8))
1716 			+ nla_total_size(sizeof(u8));
1717 out:
1718 	rcu_read_unlock();
1719 
1720 	return size;
1721 }
1722 
1723 static struct tc_action_ops act_ct_ops = {
1724 	.kind		=	"ct",
1725 	.id		=	TCA_ID_CT,
1726 	.owner		=	THIS_MODULE,
1727 	.act		=	tcf_ct_act,
1728 	.dump		=	tcf_ct_dump,
1729 	.init		=	tcf_ct_init,
1730 	.cleanup	=	tcf_ct_cleanup,
1731 	.stats_update	=	tcf_stats_update,
1732 	.get_fill_size	=	tcf_ct_get_fill_size,
1733 	.offload_act_setup =	tcf_ct_offload_act_setup,
1734 	.size		=	sizeof(struct tcf_ct),
1735 };
1736 MODULE_ALIAS_NET_ACT("ct");
1737 
1738 static __net_init int ct_init_net(struct net *net)
1739 {
1740 	struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1741 
1742 	return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1743 }
1744 
1745 static void __net_exit ct_exit_net(struct list_head *net_list)
1746 {
1747 	tc_action_net_exit(net_list, act_ct_ops.net_id);
1748 }
1749 
1750 static struct pernet_operations ct_net_ops = {
1751 	.init = ct_init_net,
1752 	.exit_batch = ct_exit_net,
1753 	.id   = &act_ct_ops.net_id,
1754 	.size = sizeof(struct tc_ct_action_net),
1755 };
1756 
1757 static int __init ct_init_module(void)
1758 {
1759 	int err;
1760 
1761 	act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1762 	if (!act_ct_wq)
1763 		return -ENOMEM;
1764 
1765 	err = tcf_ct_flow_tables_init();
1766 	if (err)
1767 		goto err_tbl_init;
1768 
1769 	err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1770 	if (err)
1771 		goto err_register;
1772 
1773 	static_branch_inc(&tcf_frag_xmit_count);
1774 
1775 	return 0;
1776 
1777 err_register:
1778 	tcf_ct_flow_tables_uninit();
1779 err_tbl_init:
1780 	destroy_workqueue(act_ct_wq);
1781 	return err;
1782 }
1783 
1784 static void __exit ct_cleanup_module(void)
1785 {
1786 	static_branch_dec(&tcf_frag_xmit_count);
1787 	tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1788 	tcf_ct_flow_tables_uninit();
1789 	destroy_workqueue(act_ct_wq);
1790 }
1791 
1792 module_init(ct_init_module);
1793 module_exit(ct_cleanup_module);
1794 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1795 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1796 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1797 MODULE_DESCRIPTION("Connection tracking action");
1798 MODULE_LICENSE("GPL v2");
1799