xref: /openbmc/linux/net/sched/act_ct.c (revision a21b06e7319129994f339ed47f512bbe57b77f5b)
1b57dc7c1SPaul Blakey // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2b57dc7c1SPaul Blakey /* -
3b57dc7c1SPaul Blakey  * net/sched/act_ct.c  Connection Tracking action
4b57dc7c1SPaul Blakey  *
5b57dc7c1SPaul Blakey  * Authors:   Paul Blakey <paulb@mellanox.com>
6b57dc7c1SPaul Blakey  *            Yossi Kuperman <yossiku@mellanox.com>
7b57dc7c1SPaul Blakey  *            Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
8b57dc7c1SPaul Blakey  */
9b57dc7c1SPaul Blakey 
10b57dc7c1SPaul Blakey #include <linux/module.h>
11b57dc7c1SPaul Blakey #include <linux/init.h>
12b57dc7c1SPaul Blakey #include <linux/kernel.h>
13b57dc7c1SPaul Blakey #include <linux/skbuff.h>
14b57dc7c1SPaul Blakey #include <linux/rtnetlink.h>
15b57dc7c1SPaul Blakey #include <linux/pkt_cls.h>
16b57dc7c1SPaul Blakey #include <linux/ip.h>
17b57dc7c1SPaul Blakey #include <linux/ipv6.h>
18c34b961aSPaul Blakey #include <linux/rhashtable.h>
19b57dc7c1SPaul Blakey #include <net/netlink.h>
20b57dc7c1SPaul Blakey #include <net/pkt_sched.h>
21b57dc7c1SPaul Blakey #include <net/pkt_cls.h>
22b57dc7c1SPaul Blakey #include <net/act_api.h>
23b57dc7c1SPaul Blakey #include <net/ip.h>
24b57dc7c1SPaul Blakey #include <net/ipv6_frag.h>
25b57dc7c1SPaul Blakey #include <uapi/linux/tc_act/tc_ct.h>
26b57dc7c1SPaul Blakey #include <net/tc_act/tc_ct.h>
27b57dc7c1SPaul Blakey 
28c34b961aSPaul Blakey #include <net/netfilter/nf_flow_table.h>
29b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack.h>
30b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_core.h>
31b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_zones.h>
32b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_helper.h>
33beb97d3aSwenxu #include <net/netfilter/nf_conntrack_acct.h>
34b57dc7c1SPaul Blakey #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
359795ded7SPaul Blakey #include <net/netfilter/nf_conntrack_act_ct.h>
36*a21b06e7SXin Long #include <net/netfilter/nf_conntrack_seqadj.h>
3740d102cdSJeremy Sowden #include <uapi/linux/netfilter/nf_nat.h>
38b57dc7c1SPaul Blakey 
39c34b961aSPaul Blakey static struct workqueue_struct *act_ct_wq;
40c34b961aSPaul Blakey static struct rhashtable zones_ht;
41138470a9SEric Dumazet static DEFINE_MUTEX(zones_mutex);
42c34b961aSPaul Blakey 
43c34b961aSPaul Blakey struct tcf_ct_flow_table {
44c34b961aSPaul Blakey 	struct rhash_head node; /* In zones tables */
45c34b961aSPaul Blakey 
46c34b961aSPaul Blakey 	struct rcu_work rwork;
47c34b961aSPaul Blakey 	struct nf_flowtable nf_ft;
48138470a9SEric Dumazet 	refcount_t ref;
49c34b961aSPaul Blakey 	u16 zone;
50c34b961aSPaul Blakey 
51c34b961aSPaul Blakey 	bool dying;
52c34b961aSPaul Blakey };
53c34b961aSPaul Blakey 
54c34b961aSPaul Blakey static const struct rhashtable_params zones_params = {
55c34b961aSPaul Blakey 	.head_offset = offsetof(struct tcf_ct_flow_table, node),
56c34b961aSPaul Blakey 	.key_offset = offsetof(struct tcf_ct_flow_table, zone),
57c34b961aSPaul Blakey 	.key_len = sizeof_field(struct tcf_ct_flow_table, zone),
58c34b961aSPaul Blakey 	.automatic_shrinking = true,
59c34b961aSPaul Blakey };
60c34b961aSPaul Blakey 
619c26ba9bSPaul Blakey static struct flow_action_entry *
629c26ba9bSPaul Blakey tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
639c26ba9bSPaul Blakey {
649c26ba9bSPaul Blakey 	int i = flow_action->num_entries++;
659c26ba9bSPaul Blakey 
669c26ba9bSPaul Blakey 	return &flow_action->entries[i];
679c26ba9bSPaul Blakey }
689c26ba9bSPaul Blakey 
699c26ba9bSPaul Blakey static void tcf_ct_add_mangle_action(struct flow_action *action,
709c26ba9bSPaul Blakey 				     enum flow_action_mangle_base htype,
719c26ba9bSPaul Blakey 				     u32 offset,
729c26ba9bSPaul Blakey 				     u32 mask,
739c26ba9bSPaul Blakey 				     u32 val)
749c26ba9bSPaul Blakey {
759c26ba9bSPaul Blakey 	struct flow_action_entry *entry;
769c26ba9bSPaul Blakey 
779c26ba9bSPaul Blakey 	entry = tcf_ct_flow_table_flow_action_get_next(action);
789c26ba9bSPaul Blakey 	entry->id = FLOW_ACTION_MANGLE;
799c26ba9bSPaul Blakey 	entry->mangle.htype = htype;
809c26ba9bSPaul Blakey 	entry->mangle.mask = ~mask;
819c26ba9bSPaul Blakey 	entry->mangle.offset = offset;
829c26ba9bSPaul Blakey 	entry->mangle.val = val;
839c26ba9bSPaul Blakey }
849c26ba9bSPaul Blakey 
859c26ba9bSPaul Blakey /* The following nat helper functions check if the inverted reverse tuple
869c26ba9bSPaul Blakey  * (target) is different then the current dir tuple - meaning nat for ports
879c26ba9bSPaul Blakey  * and/or ip is needed, and add the relevant mangle actions.
889c26ba9bSPaul Blakey  */
899c26ba9bSPaul Blakey static void
909c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
919c26ba9bSPaul Blakey 				      struct nf_conntrack_tuple target,
929c26ba9bSPaul Blakey 				      struct flow_action *action)
939c26ba9bSPaul Blakey {
949c26ba9bSPaul Blakey 	if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
959c26ba9bSPaul Blakey 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
969c26ba9bSPaul Blakey 					 offsetof(struct iphdr, saddr),
979c26ba9bSPaul Blakey 					 0xFFFFFFFF,
989c26ba9bSPaul Blakey 					 be32_to_cpu(target.src.u3.ip));
999c26ba9bSPaul Blakey 	if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
1009c26ba9bSPaul Blakey 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
1019c26ba9bSPaul Blakey 					 offsetof(struct iphdr, daddr),
1029c26ba9bSPaul Blakey 					 0xFFFFFFFF,
1039c26ba9bSPaul Blakey 					 be32_to_cpu(target.dst.u3.ip));
1049c26ba9bSPaul Blakey }
1059c26ba9bSPaul Blakey 
1069c26ba9bSPaul Blakey static void
1079c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
1089c26ba9bSPaul Blakey 				   union nf_inet_addr *addr,
1099c26ba9bSPaul Blakey 				   u32 offset)
1109c26ba9bSPaul Blakey {
1119c26ba9bSPaul Blakey 	int i;
1129c26ba9bSPaul Blakey 
1139c26ba9bSPaul Blakey 	for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
1149c26ba9bSPaul Blakey 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
1159c26ba9bSPaul Blakey 					 i * sizeof(u32) + offset,
1169c26ba9bSPaul Blakey 					 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
1179c26ba9bSPaul Blakey }
1189c26ba9bSPaul Blakey 
1199c26ba9bSPaul Blakey static void
1209c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
1219c26ba9bSPaul Blakey 				      struct nf_conntrack_tuple target,
1229c26ba9bSPaul Blakey 				      struct flow_action *action)
1239c26ba9bSPaul Blakey {
1249c26ba9bSPaul Blakey 	if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
1259c26ba9bSPaul Blakey 		tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
1269c26ba9bSPaul Blakey 						   offsetof(struct ipv6hdr,
1279c26ba9bSPaul Blakey 							    saddr));
1289c26ba9bSPaul Blakey 	if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
1299c26ba9bSPaul Blakey 		tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
1309c26ba9bSPaul Blakey 						   offsetof(struct ipv6hdr,
1319c26ba9bSPaul Blakey 							    daddr));
1329c26ba9bSPaul Blakey }
1339c26ba9bSPaul Blakey 
1349c26ba9bSPaul Blakey static void
1359c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
1369c26ba9bSPaul Blakey 				     struct nf_conntrack_tuple target,
1379c26ba9bSPaul Blakey 				     struct flow_action *action)
1389c26ba9bSPaul Blakey {
1399c26ba9bSPaul Blakey 	__be16 target_src = target.src.u.tcp.port;
1409c26ba9bSPaul Blakey 	__be16 target_dst = target.dst.u.tcp.port;
1419c26ba9bSPaul Blakey 
1429c26ba9bSPaul Blakey 	if (target_src != tuple->src.u.tcp.port)
1439c26ba9bSPaul Blakey 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
1449c26ba9bSPaul Blakey 					 offsetof(struct tcphdr, source),
1459c26ba9bSPaul Blakey 					 0xFFFF, be16_to_cpu(target_src));
1469c26ba9bSPaul Blakey 	if (target_dst != tuple->dst.u.tcp.port)
1479c26ba9bSPaul Blakey 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
1489c26ba9bSPaul Blakey 					 offsetof(struct tcphdr, dest),
1499c26ba9bSPaul Blakey 					 0xFFFF, be16_to_cpu(target_dst));
1509c26ba9bSPaul Blakey }
1519c26ba9bSPaul Blakey 
1529c26ba9bSPaul Blakey static void
1539c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
1549c26ba9bSPaul Blakey 				     struct nf_conntrack_tuple target,
1559c26ba9bSPaul Blakey 				     struct flow_action *action)
1569c26ba9bSPaul Blakey {
1579c26ba9bSPaul Blakey 	__be16 target_src = target.src.u.udp.port;
1589c26ba9bSPaul Blakey 	__be16 target_dst = target.dst.u.udp.port;
1599c26ba9bSPaul Blakey 
1609c26ba9bSPaul Blakey 	if (target_src != tuple->src.u.udp.port)
16147b5d2a1SRoi Dayan 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
1629c26ba9bSPaul Blakey 					 offsetof(struct udphdr, source),
1639c26ba9bSPaul Blakey 					 0xFFFF, be16_to_cpu(target_src));
1649c26ba9bSPaul Blakey 	if (target_dst != tuple->dst.u.udp.port)
16547b5d2a1SRoi Dayan 		tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
1669c26ba9bSPaul Blakey 					 offsetof(struct udphdr, dest),
1679c26ba9bSPaul Blakey 					 0xFFFF, be16_to_cpu(target_dst));
1689c26ba9bSPaul Blakey }
1699c26ba9bSPaul Blakey 
1709c26ba9bSPaul Blakey static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
1719c26ba9bSPaul Blakey 					      enum ip_conntrack_dir dir,
1729c26ba9bSPaul Blakey 					      struct flow_action *action)
1739c26ba9bSPaul Blakey {
1749c26ba9bSPaul Blakey 	struct nf_conn_labels *ct_labels;
1759c26ba9bSPaul Blakey 	struct flow_action_entry *entry;
17630b0cf90SPaul Blakey 	enum ip_conntrack_info ctinfo;
1779c26ba9bSPaul Blakey 	u32 *act_ct_labels;
1789c26ba9bSPaul Blakey 
1799c26ba9bSPaul Blakey 	entry = tcf_ct_flow_table_flow_action_get_next(action);
1809c26ba9bSPaul Blakey 	entry->id = FLOW_ACTION_CT_METADATA;
1819c26ba9bSPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
1829c26ba9bSPaul Blakey 	entry->ct_metadata.mark = ct->mark;
1839c26ba9bSPaul Blakey #endif
18430b0cf90SPaul Blakey 	ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
18530b0cf90SPaul Blakey 					     IP_CT_ESTABLISHED_REPLY;
18630b0cf90SPaul Blakey 	/* aligns with the CT reference on the SKB nf_ct_set */
18730b0cf90SPaul Blakey 	entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
188941eff5aSPaul Blakey 	entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL;
1899c26ba9bSPaul Blakey 
1909c26ba9bSPaul Blakey 	act_ct_labels = entry->ct_metadata.labels;
1919c26ba9bSPaul Blakey 	ct_labels = nf_ct_labels_find(ct);
1929c26ba9bSPaul Blakey 	if (ct_labels)
1939c26ba9bSPaul Blakey 		memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
1949c26ba9bSPaul Blakey 	else
1959c26ba9bSPaul Blakey 		memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
1969c26ba9bSPaul Blakey }
1979c26ba9bSPaul Blakey 
1989c26ba9bSPaul Blakey static int tcf_ct_flow_table_add_action_nat(struct net *net,
1999c26ba9bSPaul Blakey 					    struct nf_conn *ct,
2009c26ba9bSPaul Blakey 					    enum ip_conntrack_dir dir,
2019c26ba9bSPaul Blakey 					    struct flow_action *action)
2029c26ba9bSPaul Blakey {
2039c26ba9bSPaul Blakey 	const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
2049c26ba9bSPaul Blakey 	struct nf_conntrack_tuple target;
2059c26ba9bSPaul Blakey 
20605aa69e5Swenxu 	if (!(ct->status & IPS_NAT_MASK))
20705aa69e5Swenxu 		return 0;
20805aa69e5Swenxu 
2099c26ba9bSPaul Blakey 	nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
2109c26ba9bSPaul Blakey 
2119c26ba9bSPaul Blakey 	switch (tuple->src.l3num) {
2129c26ba9bSPaul Blakey 	case NFPROTO_IPV4:
2139c26ba9bSPaul Blakey 		tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
2149c26ba9bSPaul Blakey 						      action);
2159c26ba9bSPaul Blakey 		break;
2169c26ba9bSPaul Blakey 	case NFPROTO_IPV6:
2179c26ba9bSPaul Blakey 		tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
2189c26ba9bSPaul Blakey 						      action);
2199c26ba9bSPaul Blakey 		break;
2209c26ba9bSPaul Blakey 	default:
2219c26ba9bSPaul Blakey 		return -EOPNOTSUPP;
2229c26ba9bSPaul Blakey 	}
2239c26ba9bSPaul Blakey 
2249c26ba9bSPaul Blakey 	switch (nf_ct_protonum(ct)) {
2259c26ba9bSPaul Blakey 	case IPPROTO_TCP:
2269c26ba9bSPaul Blakey 		tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
2279c26ba9bSPaul Blakey 		break;
2289c26ba9bSPaul Blakey 	case IPPROTO_UDP:
2299c26ba9bSPaul Blakey 		tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
2309c26ba9bSPaul Blakey 		break;
2319c26ba9bSPaul Blakey 	default:
2329c26ba9bSPaul Blakey 		return -EOPNOTSUPP;
2339c26ba9bSPaul Blakey 	}
2349c26ba9bSPaul Blakey 
2359c26ba9bSPaul Blakey 	return 0;
2369c26ba9bSPaul Blakey }
2379c26ba9bSPaul Blakey 
2389c26ba9bSPaul Blakey static int tcf_ct_flow_table_fill_actions(struct net *net,
2399c26ba9bSPaul Blakey 					  const struct flow_offload *flow,
2409c26ba9bSPaul Blakey 					  enum flow_offload_tuple_dir tdir,
2419c26ba9bSPaul Blakey 					  struct nf_flow_rule *flow_rule)
2429c26ba9bSPaul Blakey {
2439c26ba9bSPaul Blakey 	struct flow_action *action = &flow_rule->rule->action;
2449c26ba9bSPaul Blakey 	int num_entries = action->num_entries;
2459c26ba9bSPaul Blakey 	struct nf_conn *ct = flow->ct;
2469c26ba9bSPaul Blakey 	enum ip_conntrack_dir dir;
2479c26ba9bSPaul Blakey 	int i, err;
2489c26ba9bSPaul Blakey 
2499c26ba9bSPaul Blakey 	switch (tdir) {
2509c26ba9bSPaul Blakey 	case FLOW_OFFLOAD_DIR_ORIGINAL:
2519c26ba9bSPaul Blakey 		dir = IP_CT_DIR_ORIGINAL;
2529c26ba9bSPaul Blakey 		break;
2539c26ba9bSPaul Blakey 	case FLOW_OFFLOAD_DIR_REPLY:
2549c26ba9bSPaul Blakey 		dir = IP_CT_DIR_REPLY;
2559c26ba9bSPaul Blakey 		break;
2569c26ba9bSPaul Blakey 	default:
2579c26ba9bSPaul Blakey 		return -EOPNOTSUPP;
2589c26ba9bSPaul Blakey 	}
2599c26ba9bSPaul Blakey 
2609c26ba9bSPaul Blakey 	err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
2619c26ba9bSPaul Blakey 	if (err)
2629c26ba9bSPaul Blakey 		goto err_nat;
2639c26ba9bSPaul Blakey 
2649c26ba9bSPaul Blakey 	tcf_ct_flow_table_add_action_meta(ct, dir, action);
2659c26ba9bSPaul Blakey 	return 0;
2669c26ba9bSPaul Blakey 
2679c26ba9bSPaul Blakey err_nat:
2689c26ba9bSPaul Blakey 	/* Clear filled actions */
2699c26ba9bSPaul Blakey 	for (i = num_entries; i < action->num_entries; i++)
2709c26ba9bSPaul Blakey 		memset(&action->entries[i], 0, sizeof(action->entries[i]));
2719c26ba9bSPaul Blakey 	action->num_entries = num_entries;
2729c26ba9bSPaul Blakey 
2739c26ba9bSPaul Blakey 	return err;
2749c26ba9bSPaul Blakey }
2759c26ba9bSPaul Blakey 
276c34b961aSPaul Blakey static struct nf_flowtable_type flowtable_ct = {
2779c26ba9bSPaul Blakey 	.action		= tcf_ct_flow_table_fill_actions,
278c34b961aSPaul Blakey 	.owner		= THIS_MODULE,
279c34b961aSPaul Blakey };
280c34b961aSPaul Blakey 
281fc54d906SVlad Buslov static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params)
282c34b961aSPaul Blakey {
283c34b961aSPaul Blakey 	struct tcf_ct_flow_table *ct_ft;
284c34b961aSPaul Blakey 	int err = -ENOMEM;
285c34b961aSPaul Blakey 
286138470a9SEric Dumazet 	mutex_lock(&zones_mutex);
287c34b961aSPaul Blakey 	ct_ft = rhashtable_lookup_fast(&zones_ht, &params->zone, zones_params);
288138470a9SEric Dumazet 	if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
289138470a9SEric Dumazet 		goto out_unlock;
290c34b961aSPaul Blakey 
291138470a9SEric Dumazet 	ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
292c34b961aSPaul Blakey 	if (!ct_ft)
293c34b961aSPaul Blakey 		goto err_alloc;
294138470a9SEric Dumazet 	refcount_set(&ct_ft->ref, 1);
295c34b961aSPaul Blakey 
296c34b961aSPaul Blakey 	ct_ft->zone = params->zone;
297c34b961aSPaul Blakey 	err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
298c34b961aSPaul Blakey 	if (err)
299c34b961aSPaul Blakey 		goto err_insert;
300c34b961aSPaul Blakey 
301c34b961aSPaul Blakey 	ct_ft->nf_ft.type = &flowtable_ct;
3023567e233SMarcelo Ricardo Leitner 	ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD |
3033567e233SMarcelo Ricardo Leitner 			      NF_FLOWTABLE_COUNTER;
304c34b961aSPaul Blakey 	err = nf_flow_table_init(&ct_ft->nf_ft);
305c34b961aSPaul Blakey 	if (err)
306c34b961aSPaul Blakey 		goto err_init;
307fc54d906SVlad Buslov 	write_pnet(&ct_ft->nf_ft.net, net);
308c34b961aSPaul Blakey 
309c34b961aSPaul Blakey 	__module_get(THIS_MODULE);
310138470a9SEric Dumazet out_unlock:
311c34b961aSPaul Blakey 	params->ct_ft = ct_ft;
312edd5861eSPaul Blakey 	params->nf_ft = &ct_ft->nf_ft;
313138470a9SEric Dumazet 	mutex_unlock(&zones_mutex);
314c34b961aSPaul Blakey 
315c34b961aSPaul Blakey 	return 0;
316c34b961aSPaul Blakey 
317c34b961aSPaul Blakey err_init:
318c34b961aSPaul Blakey 	rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
319c34b961aSPaul Blakey err_insert:
320c34b961aSPaul Blakey 	kfree(ct_ft);
321c34b961aSPaul Blakey err_alloc:
322138470a9SEric Dumazet 	mutex_unlock(&zones_mutex);
323c34b961aSPaul Blakey 	return err;
324c34b961aSPaul Blakey }
325c34b961aSPaul Blakey 
326c34b961aSPaul Blakey static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
327c34b961aSPaul Blakey {
32877ac5e40SLouis Peens 	struct flow_block_cb *block_cb, *tmp_cb;
329c34b961aSPaul Blakey 	struct tcf_ct_flow_table *ct_ft;
33077ac5e40SLouis Peens 	struct flow_block *block;
331c34b961aSPaul Blakey 
332c34b961aSPaul Blakey 	ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
333c34b961aSPaul Blakey 			     rwork);
334c34b961aSPaul Blakey 	nf_flow_table_free(&ct_ft->nf_ft);
33577ac5e40SLouis Peens 
33677ac5e40SLouis Peens 	/* Remove any remaining callbacks before cleanup */
33777ac5e40SLouis Peens 	block = &ct_ft->nf_ft.flow_block;
33877ac5e40SLouis Peens 	down_write(&ct_ft->nf_ft.flow_block_lock);
33977ac5e40SLouis Peens 	list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
34077ac5e40SLouis Peens 		list_del(&block_cb->list);
34177ac5e40SLouis Peens 		flow_block_cb_free(block_cb);
34277ac5e40SLouis Peens 	}
34377ac5e40SLouis Peens 	up_write(&ct_ft->nf_ft.flow_block_lock);
344c34b961aSPaul Blakey 	kfree(ct_ft);
345c34b961aSPaul Blakey 
346c34b961aSPaul Blakey 	module_put(THIS_MODULE);
347c34b961aSPaul Blakey }
348c34b961aSPaul Blakey 
34919138941SXin Long static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft)
350c34b961aSPaul Blakey {
35119138941SXin Long 	if (refcount_dec_and_test(&ct_ft->ref)) {
352c34b961aSPaul Blakey 		rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
353c34b961aSPaul Blakey 		INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
354c34b961aSPaul Blakey 		queue_rcu_work(act_ct_wq, &ct_ft->rwork);
355c34b961aSPaul Blakey 	}
356c34b961aSPaul Blakey }
357c34b961aSPaul Blakey 
358db6140e5SPaul Blakey static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,
359db6140e5SPaul Blakey 				 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir)
360db6140e5SPaul Blakey {
361db6140e5SPaul Blakey 	entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC;
362db6140e5SPaul Blakey 	entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir];
363db6140e5SPaul Blakey }
364db6140e5SPaul Blakey 
36564ff70b8SPaul Blakey static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
36664ff70b8SPaul Blakey 				  struct nf_conn *ct,
36764ff70b8SPaul Blakey 				  bool tcp)
36864ff70b8SPaul Blakey {
3699795ded7SPaul Blakey 	struct nf_conn_act_ct_ext *act_ct_ext;
37064ff70b8SPaul Blakey 	struct flow_offload *entry;
37164ff70b8SPaul Blakey 	int err;
37264ff70b8SPaul Blakey 
37364ff70b8SPaul Blakey 	if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
37464ff70b8SPaul Blakey 		return;
37564ff70b8SPaul Blakey 
37664ff70b8SPaul Blakey 	entry = flow_offload_alloc(ct);
37764ff70b8SPaul Blakey 	if (!entry) {
37864ff70b8SPaul Blakey 		WARN_ON_ONCE(1);
37964ff70b8SPaul Blakey 		goto err_alloc;
38064ff70b8SPaul Blakey 	}
38164ff70b8SPaul Blakey 
38264ff70b8SPaul Blakey 	if (tcp) {
38364ff70b8SPaul Blakey 		ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
38464ff70b8SPaul Blakey 		ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
38564ff70b8SPaul Blakey 	}
38664ff70b8SPaul Blakey 
3879795ded7SPaul Blakey 	act_ct_ext = nf_conn_act_ct_ext_find(ct);
3889795ded7SPaul Blakey 	if (act_ct_ext) {
389db6140e5SPaul Blakey 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
390db6140e5SPaul Blakey 		tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
3919795ded7SPaul Blakey 	}
3929795ded7SPaul Blakey 
39364ff70b8SPaul Blakey 	err = flow_offload_add(&ct_ft->nf_ft, entry);
39464ff70b8SPaul Blakey 	if (err)
39564ff70b8SPaul Blakey 		goto err_add;
39664ff70b8SPaul Blakey 
39764ff70b8SPaul Blakey 	return;
39864ff70b8SPaul Blakey 
39964ff70b8SPaul Blakey err_add:
40064ff70b8SPaul Blakey 	flow_offload_free(entry);
40164ff70b8SPaul Blakey err_alloc:
40264ff70b8SPaul Blakey 	clear_bit(IPS_OFFLOAD_BIT, &ct->status);
40364ff70b8SPaul Blakey }
40464ff70b8SPaul Blakey 
40564ff70b8SPaul Blakey static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
40664ff70b8SPaul Blakey 					   struct nf_conn *ct,
40764ff70b8SPaul Blakey 					   enum ip_conntrack_info ctinfo)
40864ff70b8SPaul Blakey {
40964ff70b8SPaul Blakey 	bool tcp = false;
41064ff70b8SPaul Blakey 
41143332cf9SChris Mi 	if ((ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) ||
41243332cf9SChris Mi 	    !test_bit(IPS_ASSURED_BIT, &ct->status))
41364ff70b8SPaul Blakey 		return;
41464ff70b8SPaul Blakey 
41564ff70b8SPaul Blakey 	switch (nf_ct_protonum(ct)) {
41664ff70b8SPaul Blakey 	case IPPROTO_TCP:
41764ff70b8SPaul Blakey 		tcp = true;
41864ff70b8SPaul Blakey 		if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
41964ff70b8SPaul Blakey 			return;
42064ff70b8SPaul Blakey 		break;
42164ff70b8SPaul Blakey 	case IPPROTO_UDP:
42264ff70b8SPaul Blakey 		break;
423fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE
424fcb6aa86SToshiaki Makita 	case IPPROTO_GRE: {
425fcb6aa86SToshiaki Makita 		struct nf_conntrack_tuple *tuple;
426fcb6aa86SToshiaki Makita 
427fcb6aa86SToshiaki Makita 		if (ct->status & IPS_NAT_MASK)
428fcb6aa86SToshiaki Makita 			return;
429fcb6aa86SToshiaki Makita 		tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
430fcb6aa86SToshiaki Makita 		/* No support for GRE v1 */
431fcb6aa86SToshiaki Makita 		if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
432fcb6aa86SToshiaki Makita 			return;
433fcb6aa86SToshiaki Makita 		break;
434fcb6aa86SToshiaki Makita 	}
435fcb6aa86SToshiaki Makita #endif
43664ff70b8SPaul Blakey 	default:
43764ff70b8SPaul Blakey 		return;
43864ff70b8SPaul Blakey 	}
43964ff70b8SPaul Blakey 
44064ff70b8SPaul Blakey 	if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
44164ff70b8SPaul Blakey 	    ct->status & IPS_SEQ_ADJUST)
44264ff70b8SPaul Blakey 		return;
44364ff70b8SPaul Blakey 
44464ff70b8SPaul Blakey 	tcf_ct_flow_table_add(ct_ft, ct, tcp);
44564ff70b8SPaul Blakey }
44664ff70b8SPaul Blakey 
44746475bb2SPaul Blakey static bool
44846475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
44907ac9d16SPaul Blakey 				  struct flow_offload_tuple *tuple,
45007ac9d16SPaul Blakey 				  struct tcphdr **tcph)
45146475bb2SPaul Blakey {
45246475bb2SPaul Blakey 	struct flow_ports *ports;
45346475bb2SPaul Blakey 	unsigned int thoff;
45446475bb2SPaul Blakey 	struct iphdr *iph;
455fcb6aa86SToshiaki Makita 	size_t hdrsize;
456fcb6aa86SToshiaki Makita 	u8 ipproto;
45746475bb2SPaul Blakey 
4584cc5fdecSPaul Blakey 	if (!pskb_network_may_pull(skb, sizeof(*iph)))
45946475bb2SPaul Blakey 		return false;
46046475bb2SPaul Blakey 
46146475bb2SPaul Blakey 	iph = ip_hdr(skb);
46246475bb2SPaul Blakey 	thoff = iph->ihl * 4;
46346475bb2SPaul Blakey 
46446475bb2SPaul Blakey 	if (ip_is_fragment(iph) ||
46546475bb2SPaul Blakey 	    unlikely(thoff != sizeof(struct iphdr)))
46646475bb2SPaul Blakey 		return false;
46746475bb2SPaul Blakey 
468fcb6aa86SToshiaki Makita 	ipproto = iph->protocol;
469fcb6aa86SToshiaki Makita 	switch (ipproto) {
470fcb6aa86SToshiaki Makita 	case IPPROTO_TCP:
471fcb6aa86SToshiaki Makita 		hdrsize = sizeof(struct tcphdr);
472fcb6aa86SToshiaki Makita 		break;
473fcb6aa86SToshiaki Makita 	case IPPROTO_UDP:
474fcb6aa86SToshiaki Makita 		hdrsize = sizeof(*ports);
475fcb6aa86SToshiaki Makita 		break;
476fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE
477fcb6aa86SToshiaki Makita 	case IPPROTO_GRE:
478fcb6aa86SToshiaki Makita 		hdrsize = sizeof(struct gre_base_hdr);
479fcb6aa86SToshiaki Makita 		break;
480fcb6aa86SToshiaki Makita #endif
481fcb6aa86SToshiaki Makita 	default:
48246475bb2SPaul Blakey 		return false;
483fcb6aa86SToshiaki Makita 	}
48446475bb2SPaul Blakey 
48546475bb2SPaul Blakey 	if (iph->ttl <= 1)
48646475bb2SPaul Blakey 		return false;
48746475bb2SPaul Blakey 
488fcb6aa86SToshiaki Makita 	if (!pskb_network_may_pull(skb, thoff + hdrsize))
48946475bb2SPaul Blakey 		return false;
49046475bb2SPaul Blakey 
491fcb6aa86SToshiaki Makita 	switch (ipproto) {
492fcb6aa86SToshiaki Makita 	case IPPROTO_TCP:
49307ac9d16SPaul Blakey 		*tcph = (void *)(skb_network_header(skb) + thoff);
494fcb6aa86SToshiaki Makita 		fallthrough;
495fcb6aa86SToshiaki Makita 	case IPPROTO_UDP:
49607ac9d16SPaul Blakey 		ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
49746475bb2SPaul Blakey 		tuple->src_port = ports->source;
49846475bb2SPaul Blakey 		tuple->dst_port = ports->dest;
499fcb6aa86SToshiaki Makita 		break;
500fcb6aa86SToshiaki Makita 	case IPPROTO_GRE: {
501fcb6aa86SToshiaki Makita 		struct gre_base_hdr *greh;
502fcb6aa86SToshiaki Makita 
503fcb6aa86SToshiaki Makita 		greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
504fcb6aa86SToshiaki Makita 		if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
505fcb6aa86SToshiaki Makita 			return false;
506fcb6aa86SToshiaki Makita 		break;
507fcb6aa86SToshiaki Makita 	}
508fcb6aa86SToshiaki Makita 	}
509fcb6aa86SToshiaki Makita 
510fcb6aa86SToshiaki Makita 	iph = ip_hdr(skb);
511fcb6aa86SToshiaki Makita 
512fcb6aa86SToshiaki Makita 	tuple->src_v4.s_addr = iph->saddr;
513fcb6aa86SToshiaki Makita 	tuple->dst_v4.s_addr = iph->daddr;
51446475bb2SPaul Blakey 	tuple->l3proto = AF_INET;
515fcb6aa86SToshiaki Makita 	tuple->l4proto = ipproto;
51646475bb2SPaul Blakey 
51746475bb2SPaul Blakey 	return true;
51846475bb2SPaul Blakey }
51946475bb2SPaul Blakey 
52046475bb2SPaul Blakey static bool
52146475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
52207ac9d16SPaul Blakey 				  struct flow_offload_tuple *tuple,
52307ac9d16SPaul Blakey 				  struct tcphdr **tcph)
52446475bb2SPaul Blakey {
52546475bb2SPaul Blakey 	struct flow_ports *ports;
52646475bb2SPaul Blakey 	struct ipv6hdr *ip6h;
52746475bb2SPaul Blakey 	unsigned int thoff;
528fcb6aa86SToshiaki Makita 	size_t hdrsize;
529fcb6aa86SToshiaki Makita 	u8 nexthdr;
53046475bb2SPaul Blakey 
5314cc5fdecSPaul Blakey 	if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
53246475bb2SPaul Blakey 		return false;
53346475bb2SPaul Blakey 
53446475bb2SPaul Blakey 	ip6h = ipv6_hdr(skb);
535fcb6aa86SToshiaki Makita 	thoff = sizeof(*ip6h);
53646475bb2SPaul Blakey 
537fcb6aa86SToshiaki Makita 	nexthdr = ip6h->nexthdr;
538fcb6aa86SToshiaki Makita 	switch (nexthdr) {
539fcb6aa86SToshiaki Makita 	case IPPROTO_TCP:
540fcb6aa86SToshiaki Makita 		hdrsize = sizeof(struct tcphdr);
541fcb6aa86SToshiaki Makita 		break;
542fcb6aa86SToshiaki Makita 	case IPPROTO_UDP:
543fcb6aa86SToshiaki Makita 		hdrsize = sizeof(*ports);
544fcb6aa86SToshiaki Makita 		break;
545fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE
546fcb6aa86SToshiaki Makita 	case IPPROTO_GRE:
547fcb6aa86SToshiaki Makita 		hdrsize = sizeof(struct gre_base_hdr);
548fcb6aa86SToshiaki Makita 		break;
549fcb6aa86SToshiaki Makita #endif
550fcb6aa86SToshiaki Makita 	default:
55186360030SDan Carpenter 		return false;
552fcb6aa86SToshiaki Makita 	}
55346475bb2SPaul Blakey 
55446475bb2SPaul Blakey 	if (ip6h->hop_limit <= 1)
55546475bb2SPaul Blakey 		return false;
55646475bb2SPaul Blakey 
557fcb6aa86SToshiaki Makita 	if (!pskb_network_may_pull(skb, thoff + hdrsize))
55846475bb2SPaul Blakey 		return false;
55946475bb2SPaul Blakey 
560fcb6aa86SToshiaki Makita 	switch (nexthdr) {
561fcb6aa86SToshiaki Makita 	case IPPROTO_TCP:
56207ac9d16SPaul Blakey 		*tcph = (void *)(skb_network_header(skb) + thoff);
563fcb6aa86SToshiaki Makita 		fallthrough;
564fcb6aa86SToshiaki Makita 	case IPPROTO_UDP:
56507ac9d16SPaul Blakey 		ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
56646475bb2SPaul Blakey 		tuple->src_port = ports->source;
56746475bb2SPaul Blakey 		tuple->dst_port = ports->dest;
568fcb6aa86SToshiaki Makita 		break;
569fcb6aa86SToshiaki Makita 	case IPPROTO_GRE: {
570fcb6aa86SToshiaki Makita 		struct gre_base_hdr *greh;
571fcb6aa86SToshiaki Makita 
572fcb6aa86SToshiaki Makita 		greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
573fcb6aa86SToshiaki Makita 		if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
574fcb6aa86SToshiaki Makita 			return false;
575fcb6aa86SToshiaki Makita 		break;
576fcb6aa86SToshiaki Makita 	}
577fcb6aa86SToshiaki Makita 	}
578fcb6aa86SToshiaki Makita 
579fcb6aa86SToshiaki Makita 	ip6h = ipv6_hdr(skb);
580fcb6aa86SToshiaki Makita 
581fcb6aa86SToshiaki Makita 	tuple->src_v6 = ip6h->saddr;
582fcb6aa86SToshiaki Makita 	tuple->dst_v6 = ip6h->daddr;
58346475bb2SPaul Blakey 	tuple->l3proto = AF_INET6;
584fcb6aa86SToshiaki Makita 	tuple->l4proto = nexthdr;
58546475bb2SPaul Blakey 
58646475bb2SPaul Blakey 	return true;
58746475bb2SPaul Blakey }
58846475bb2SPaul Blakey 
58946475bb2SPaul Blakey static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
59046475bb2SPaul Blakey 				     struct sk_buff *skb,
59146475bb2SPaul Blakey 				     u8 family)
59246475bb2SPaul Blakey {
59346475bb2SPaul Blakey 	struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
59446475bb2SPaul Blakey 	struct flow_offload_tuple_rhash *tuplehash;
59546475bb2SPaul Blakey 	struct flow_offload_tuple tuple = {};
59646475bb2SPaul Blakey 	enum ip_conntrack_info ctinfo;
59707ac9d16SPaul Blakey 	struct tcphdr *tcph = NULL;
59846475bb2SPaul Blakey 	struct flow_offload *flow;
59946475bb2SPaul Blakey 	struct nf_conn *ct;
60046475bb2SPaul Blakey 	u8 dir;
60146475bb2SPaul Blakey 
60246475bb2SPaul Blakey 	switch (family) {
60346475bb2SPaul Blakey 	case NFPROTO_IPV4:
60407ac9d16SPaul Blakey 		if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
60546475bb2SPaul Blakey 			return false;
60646475bb2SPaul Blakey 		break;
60746475bb2SPaul Blakey 	case NFPROTO_IPV6:
60807ac9d16SPaul Blakey 		if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
60946475bb2SPaul Blakey 			return false;
61046475bb2SPaul Blakey 		break;
61146475bb2SPaul Blakey 	default:
61246475bb2SPaul Blakey 		return false;
61346475bb2SPaul Blakey 	}
61446475bb2SPaul Blakey 
61546475bb2SPaul Blakey 	tuplehash = flow_offload_lookup(nf_ft, &tuple);
61646475bb2SPaul Blakey 	if (!tuplehash)
61746475bb2SPaul Blakey 		return false;
61846475bb2SPaul Blakey 
61946475bb2SPaul Blakey 	dir = tuplehash->tuple.dir;
62046475bb2SPaul Blakey 	flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
62146475bb2SPaul Blakey 	ct = flow->ct;
62246475bb2SPaul Blakey 
62307ac9d16SPaul Blakey 	if (tcph && (unlikely(tcph->fin || tcph->rst))) {
62407ac9d16SPaul Blakey 		flow_offload_teardown(flow);
62507ac9d16SPaul Blakey 		return false;
62607ac9d16SPaul Blakey 	}
62707ac9d16SPaul Blakey 
62846475bb2SPaul Blakey 	ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
62946475bb2SPaul Blakey 						    IP_CT_ESTABLISHED_REPLY;
63046475bb2SPaul Blakey 
6318b3646d6SPaul Blakey 	flow_offload_refresh(nf_ft, flow);
63246475bb2SPaul Blakey 	nf_conntrack_get(&ct->ct_general);
63346475bb2SPaul Blakey 	nf_ct_set(skb, ct, ctinfo);
6343567e233SMarcelo Ricardo Leitner 	if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
635beb97d3aSwenxu 		nf_ct_acct_update(ct, dir, skb->len);
63646475bb2SPaul Blakey 
63746475bb2SPaul Blakey 	return true;
63846475bb2SPaul Blakey }
63946475bb2SPaul Blakey 
640c34b961aSPaul Blakey static int tcf_ct_flow_tables_init(void)
641c34b961aSPaul Blakey {
642c34b961aSPaul Blakey 	return rhashtable_init(&zones_ht, &zones_params);
643c34b961aSPaul Blakey }
644c34b961aSPaul Blakey 
645c34b961aSPaul Blakey static void tcf_ct_flow_tables_uninit(void)
646c34b961aSPaul Blakey {
647c34b961aSPaul Blakey 	rhashtable_destroy(&zones_ht);
648c34b961aSPaul Blakey }
649c34b961aSPaul Blakey 
650b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops;
651b57dc7c1SPaul Blakey 
652b57dc7c1SPaul Blakey struct tc_ct_action_net {
653b57dc7c1SPaul Blakey 	struct tc_action_net tn; /* Must be first */
654b57dc7c1SPaul Blakey 	bool labels;
655b57dc7c1SPaul Blakey };
656b57dc7c1SPaul Blakey 
657b57dc7c1SPaul Blakey /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
658b57dc7c1SPaul Blakey static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
659*a21b06e7SXin Long 				   struct tcf_ct_params *p)
660b57dc7c1SPaul Blakey {
661b57dc7c1SPaul Blakey 	enum ip_conntrack_info ctinfo;
662b57dc7c1SPaul Blakey 	struct nf_conn *ct;
663b57dc7c1SPaul Blakey 
664b57dc7c1SPaul Blakey 	ct = nf_ct_get(skb, &ctinfo);
665b57dc7c1SPaul Blakey 	if (!ct)
666b57dc7c1SPaul Blakey 		return false;
667b57dc7c1SPaul Blakey 	if (!net_eq(net, read_pnet(&ct->ct_net)))
668bcb74e13SMarcelo Ricardo Leitner 		goto drop_ct;
669*a21b06e7SXin Long 	if (nf_ct_zone(ct)->id != p->zone)
670bcb74e13SMarcelo Ricardo Leitner 		goto drop_ct;
671*a21b06e7SXin Long 	if (p->helper) {
672*a21b06e7SXin Long 		struct nf_conn_help *help;
673*a21b06e7SXin Long 
674*a21b06e7SXin Long 		help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
675*a21b06e7SXin Long 		if (help && rcu_access_pointer(help->helper) != p->helper)
676*a21b06e7SXin Long 			goto drop_ct;
677*a21b06e7SXin Long 	}
678b57dc7c1SPaul Blakey 
679b57dc7c1SPaul Blakey 	/* Force conntrack entry direction. */
680*a21b06e7SXin Long 	if ((p->ct_action & TCA_CT_ACT_FORCE) &&
681*a21b06e7SXin Long 	    CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
682b57dc7c1SPaul Blakey 		if (nf_ct_is_confirmed(ct))
683b57dc7c1SPaul Blakey 			nf_ct_kill(ct);
684b57dc7c1SPaul Blakey 
685bcb74e13SMarcelo Ricardo Leitner 		goto drop_ct;
686bcb74e13SMarcelo Ricardo Leitner 	}
687bcb74e13SMarcelo Ricardo Leitner 
688bcb74e13SMarcelo Ricardo Leitner 	return true;
689bcb74e13SMarcelo Ricardo Leitner 
690bcb74e13SMarcelo Ricardo Leitner drop_ct:
691408bdcfcSFlorian Westphal 	nf_ct_put(ct);
692b57dc7c1SPaul Blakey 	nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
693b57dc7c1SPaul Blakey 
694b57dc7c1SPaul Blakey 	return false;
695b57dc7c1SPaul Blakey }
696b57dc7c1SPaul Blakey 
697b57dc7c1SPaul Blakey /* Trim the skb to the length specified by the IP/IPv6 header,
698b57dc7c1SPaul Blakey  * removing any trailing lower-layer padding. This prepares the skb
699b57dc7c1SPaul Blakey  * for higher-layer processing that assumes skb->len excludes padding
700b57dc7c1SPaul Blakey  * (such as nf_ip_checksum). The caller needs to pull the skb to the
701b57dc7c1SPaul Blakey  * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
702b57dc7c1SPaul Blakey  */
703b57dc7c1SPaul Blakey static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
704b57dc7c1SPaul Blakey {
705b57dc7c1SPaul Blakey 	unsigned int len;
706b57dc7c1SPaul Blakey 
707b57dc7c1SPaul Blakey 	switch (family) {
708b57dc7c1SPaul Blakey 	case NFPROTO_IPV4:
709b57dc7c1SPaul Blakey 		len = ntohs(ip_hdr(skb)->tot_len);
710b57dc7c1SPaul Blakey 		break;
711b57dc7c1SPaul Blakey 	case NFPROTO_IPV6:
712b57dc7c1SPaul Blakey 		len = sizeof(struct ipv6hdr)
713b57dc7c1SPaul Blakey 			+ ntohs(ipv6_hdr(skb)->payload_len);
714b57dc7c1SPaul Blakey 		break;
715b57dc7c1SPaul Blakey 	default:
716b57dc7c1SPaul Blakey 		len = skb->len;
717b57dc7c1SPaul Blakey 	}
718b57dc7c1SPaul Blakey 
7192a566f01SJinpeng Cui 	return pskb_trim_rcsum(skb, len);
720b57dc7c1SPaul Blakey }
721b57dc7c1SPaul Blakey 
722b57dc7c1SPaul Blakey static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
723b57dc7c1SPaul Blakey {
724b57dc7c1SPaul Blakey 	u8 family = NFPROTO_UNSPEC;
725b57dc7c1SPaul Blakey 
726d7bf2ebeSToke Høiland-Jørgensen 	switch (skb_protocol(skb, true)) {
727b57dc7c1SPaul Blakey 	case htons(ETH_P_IP):
728b57dc7c1SPaul Blakey 		family = NFPROTO_IPV4;
729b57dc7c1SPaul Blakey 		break;
730b57dc7c1SPaul Blakey 	case htons(ETH_P_IPV6):
731b57dc7c1SPaul Blakey 		family = NFPROTO_IPV6;
732b57dc7c1SPaul Blakey 		break;
733b57dc7c1SPaul Blakey 	default:
734b57dc7c1SPaul Blakey 		break;
735b57dc7c1SPaul Blakey 	}
736b57dc7c1SPaul Blakey 
737b57dc7c1SPaul Blakey 	return family;
738b57dc7c1SPaul Blakey }
739b57dc7c1SPaul Blakey 
740b57dc7c1SPaul Blakey static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
741b57dc7c1SPaul Blakey {
742b57dc7c1SPaul Blakey 	unsigned int len;
743b57dc7c1SPaul Blakey 
744b57dc7c1SPaul Blakey 	len =  skb_network_offset(skb) + sizeof(struct iphdr);
745b57dc7c1SPaul Blakey 	if (unlikely(skb->len < len))
746b57dc7c1SPaul Blakey 		return -EINVAL;
747b57dc7c1SPaul Blakey 	if (unlikely(!pskb_may_pull(skb, len)))
748b57dc7c1SPaul Blakey 		return -ENOMEM;
749b57dc7c1SPaul Blakey 
750b57dc7c1SPaul Blakey 	*frag = ip_is_fragment(ip_hdr(skb));
751b57dc7c1SPaul Blakey 	return 0;
752b57dc7c1SPaul Blakey }
753b57dc7c1SPaul Blakey 
754b57dc7c1SPaul Blakey static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
755b57dc7c1SPaul Blakey {
756b57dc7c1SPaul Blakey 	unsigned int flags = 0, len, payload_ofs = 0;
757b57dc7c1SPaul Blakey 	unsigned short frag_off;
758b57dc7c1SPaul Blakey 	int nexthdr;
759b57dc7c1SPaul Blakey 
760b57dc7c1SPaul Blakey 	len =  skb_network_offset(skb) + sizeof(struct ipv6hdr);
761b57dc7c1SPaul Blakey 	if (unlikely(skb->len < len))
762b57dc7c1SPaul Blakey 		return -EINVAL;
763b57dc7c1SPaul Blakey 	if (unlikely(!pskb_may_pull(skb, len)))
764b57dc7c1SPaul Blakey 		return -ENOMEM;
765b57dc7c1SPaul Blakey 
766b57dc7c1SPaul Blakey 	nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
767b57dc7c1SPaul Blakey 	if (unlikely(nexthdr < 0))
768b57dc7c1SPaul Blakey 		return -EPROTO;
769b57dc7c1SPaul Blakey 
770b57dc7c1SPaul Blakey 	*frag = flags & IP6_FH_F_FRAG;
771b57dc7c1SPaul Blakey 	return 0;
772b57dc7c1SPaul Blakey }
773b57dc7c1SPaul Blakey 
774b57dc7c1SPaul Blakey static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
775ae372cb1Swenxu 				   u8 family, u16 zone, bool *defrag)
776b57dc7c1SPaul Blakey {
777b57dc7c1SPaul Blakey 	enum ip_conntrack_info ctinfo;
778b57dc7c1SPaul Blakey 	struct nf_conn *ct;
779b57dc7c1SPaul Blakey 	int err = 0;
780b57dc7c1SPaul Blakey 	bool frag;
781ec624fe7SPaul Blakey 	u16 mru;
782b57dc7c1SPaul Blakey 
783b57dc7c1SPaul Blakey 	/* Previously seen (loopback)? Ignore. */
784b57dc7c1SPaul Blakey 	ct = nf_ct_get(skb, &ctinfo);
785b57dc7c1SPaul Blakey 	if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
786b57dc7c1SPaul Blakey 		return 0;
787b57dc7c1SPaul Blakey 
788b57dc7c1SPaul Blakey 	if (family == NFPROTO_IPV4)
789b57dc7c1SPaul Blakey 		err = tcf_ct_ipv4_is_fragment(skb, &frag);
790b57dc7c1SPaul Blakey 	else
791b57dc7c1SPaul Blakey 		err = tcf_ct_ipv6_is_fragment(skb, &frag);
792b57dc7c1SPaul Blakey 	if (err || !frag)
793b57dc7c1SPaul Blakey 		return err;
794b57dc7c1SPaul Blakey 
795b57dc7c1SPaul Blakey 	skb_get(skb);
796ec624fe7SPaul Blakey 	mru = tc_skb_cb(skb)->mru;
797b57dc7c1SPaul Blakey 
798b57dc7c1SPaul Blakey 	if (family == NFPROTO_IPV4) {
799b57dc7c1SPaul Blakey 		enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
800b57dc7c1SPaul Blakey 
801b57dc7c1SPaul Blakey 		memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
802b57dc7c1SPaul Blakey 		local_bh_disable();
803b57dc7c1SPaul Blakey 		err = ip_defrag(net, skb, user);
804b57dc7c1SPaul Blakey 		local_bh_enable();
805b57dc7c1SPaul Blakey 		if (err && err != -EINPROGRESS)
806eda814b9SAlaa Hleihel 			return err;
807ae372cb1Swenxu 
808038ebb1aSwenxu 		if (!err) {
809ae372cb1Swenxu 			*defrag = true;
810ec624fe7SPaul Blakey 			mru = IPCB(skb)->frag_max_size;
811038ebb1aSwenxu 		}
812b57dc7c1SPaul Blakey 	} else { /* NFPROTO_IPV6 */
813b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
814b57dc7c1SPaul Blakey 		enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
815b57dc7c1SPaul Blakey 
816b57dc7c1SPaul Blakey 		memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
817b57dc7c1SPaul Blakey 		err = nf_ct_frag6_gather(net, skb, user);
818b57dc7c1SPaul Blakey 		if (err && err != -EINPROGRESS)
819b57dc7c1SPaul Blakey 			goto out_free;
820ae372cb1Swenxu 
821038ebb1aSwenxu 		if (!err) {
822ae372cb1Swenxu 			*defrag = true;
823ec624fe7SPaul Blakey 			mru = IP6CB(skb)->frag_max_size;
824038ebb1aSwenxu 		}
825b57dc7c1SPaul Blakey #else
826b57dc7c1SPaul Blakey 		err = -EOPNOTSUPP;
827b57dc7c1SPaul Blakey 		goto out_free;
828b57dc7c1SPaul Blakey #endif
829b57dc7c1SPaul Blakey 	}
830b57dc7c1SPaul Blakey 
831f77bd544SDavide Caratti 	if (err != -EINPROGRESS)
832ec624fe7SPaul Blakey 		tc_skb_cb(skb)->mru = mru;
833b57dc7c1SPaul Blakey 	skb_clear_hash(skb);
834b57dc7c1SPaul Blakey 	skb->ignore_df = 1;
835b57dc7c1SPaul Blakey 	return err;
836b57dc7c1SPaul Blakey 
837b57dc7c1SPaul Blakey out_free:
838b57dc7c1SPaul Blakey 	kfree_skb(skb);
839b57dc7c1SPaul Blakey 	return err;
840b57dc7c1SPaul Blakey }
841b57dc7c1SPaul Blakey 
84219138941SXin Long static void tcf_ct_params_free(struct tcf_ct_params *params)
843b57dc7c1SPaul Blakey {
844*a21b06e7SXin Long 	if (params->helper) {
845*a21b06e7SXin Long #if IS_ENABLED(CONFIG_NF_NAT)
846*a21b06e7SXin Long 		if (params->ct_action & TCA_CT_ACT_NAT)
847*a21b06e7SXin Long 			nf_nat_helper_put(params->helper);
848*a21b06e7SXin Long #endif
849*a21b06e7SXin Long 		nf_conntrack_helper_put(params->helper);
850*a21b06e7SXin Long 	}
85119138941SXin Long 	if (params->ct_ft)
85219138941SXin Long 		tcf_ct_flow_table_put(params->ct_ft);
853b57dc7c1SPaul Blakey 	if (params->tmpl)
854408bdcfcSFlorian Westphal 		nf_ct_put(params->tmpl);
855b57dc7c1SPaul Blakey 	kfree(params);
856b57dc7c1SPaul Blakey }
857b57dc7c1SPaul Blakey 
85819138941SXin Long static void tcf_ct_params_free_rcu(struct rcu_head *head)
85919138941SXin Long {
86019138941SXin Long 	struct tcf_ct_params *params;
86119138941SXin Long 
86219138941SXin Long 	params = container_of(head, struct tcf_ct_params, rcu);
86319138941SXin Long 	tcf_ct_params_free(params);
86419138941SXin Long }
86519138941SXin Long 
866b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT)
867b57dc7c1SPaul Blakey /* Modelled after nf_nat_ipv[46]_fn().
868b57dc7c1SPaul Blakey  * range is only used for new, uninitialized NAT state.
869b57dc7c1SPaul Blakey  * Returns either NF_ACCEPT or NF_DROP.
870b57dc7c1SPaul Blakey  */
871b57dc7c1SPaul Blakey static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
872b57dc7c1SPaul Blakey 			  enum ip_conntrack_info ctinfo,
873b57dc7c1SPaul Blakey 			  const struct nf_nat_range2 *range,
874b57dc7c1SPaul Blakey 			  enum nf_nat_manip_type maniptype)
875b57dc7c1SPaul Blakey {
876d7bf2ebeSToke Høiland-Jørgensen 	__be16 proto = skb_protocol(skb, true);
877b57dc7c1SPaul Blakey 	int hooknum, err = NF_ACCEPT;
878b57dc7c1SPaul Blakey 
879b57dc7c1SPaul Blakey 	/* See HOOK2MANIP(). */
880b57dc7c1SPaul Blakey 	if (maniptype == NF_NAT_MANIP_SRC)
881b57dc7c1SPaul Blakey 		hooknum = NF_INET_LOCAL_IN; /* Source NAT */
882b57dc7c1SPaul Blakey 	else
883b57dc7c1SPaul Blakey 		hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
884b57dc7c1SPaul Blakey 
885b57dc7c1SPaul Blakey 	switch (ctinfo) {
886b57dc7c1SPaul Blakey 	case IP_CT_RELATED:
887b57dc7c1SPaul Blakey 	case IP_CT_RELATED_REPLY:
888d7bf2ebeSToke Høiland-Jørgensen 		if (proto == htons(ETH_P_IP) &&
889b57dc7c1SPaul Blakey 		    ip_hdr(skb)->protocol == IPPROTO_ICMP) {
890b57dc7c1SPaul Blakey 			if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
891b57dc7c1SPaul Blakey 							   hooknum))
892b57dc7c1SPaul Blakey 				err = NF_DROP;
893b57dc7c1SPaul Blakey 			goto out;
894d7bf2ebeSToke Høiland-Jørgensen 		} else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
895b57dc7c1SPaul Blakey 			__be16 frag_off;
896b57dc7c1SPaul Blakey 			u8 nexthdr = ipv6_hdr(skb)->nexthdr;
897b57dc7c1SPaul Blakey 			int hdrlen = ipv6_skip_exthdr(skb,
898b57dc7c1SPaul Blakey 						      sizeof(struct ipv6hdr),
899b57dc7c1SPaul Blakey 						      &nexthdr, &frag_off);
900b57dc7c1SPaul Blakey 
901b57dc7c1SPaul Blakey 			if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
902b57dc7c1SPaul Blakey 				if (!nf_nat_icmpv6_reply_translation(skb, ct,
903b57dc7c1SPaul Blakey 								     ctinfo,
904b57dc7c1SPaul Blakey 								     hooknum,
905b57dc7c1SPaul Blakey 								     hdrlen))
906b57dc7c1SPaul Blakey 					err = NF_DROP;
907b57dc7c1SPaul Blakey 				goto out;
908b57dc7c1SPaul Blakey 			}
909b57dc7c1SPaul Blakey 		}
910b57dc7c1SPaul Blakey 		/* Non-ICMP, fall thru to initialize if needed. */
911964201deSGustavo A. R. Silva 		fallthrough;
912b57dc7c1SPaul Blakey 	case IP_CT_NEW:
913b57dc7c1SPaul Blakey 		/* Seen it before?  This can happen for loopback, retrans,
914b57dc7c1SPaul Blakey 		 * or local packets.
915b57dc7c1SPaul Blakey 		 */
916b57dc7c1SPaul Blakey 		if (!nf_nat_initialized(ct, maniptype)) {
917b57dc7c1SPaul Blakey 			/* Initialize according to the NAT action. */
918b57dc7c1SPaul Blakey 			err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
919b57dc7c1SPaul Blakey 				/* Action is set up to establish a new
920b57dc7c1SPaul Blakey 				 * mapping.
921b57dc7c1SPaul Blakey 				 */
922b57dc7c1SPaul Blakey 				? nf_nat_setup_info(ct, range, maniptype)
923b57dc7c1SPaul Blakey 				: nf_nat_alloc_null_binding(ct, hooknum);
924b57dc7c1SPaul Blakey 			if (err != NF_ACCEPT)
925b57dc7c1SPaul Blakey 				goto out;
926b57dc7c1SPaul Blakey 		}
927b57dc7c1SPaul Blakey 		break;
928b57dc7c1SPaul Blakey 
929b57dc7c1SPaul Blakey 	case IP_CT_ESTABLISHED:
930b57dc7c1SPaul Blakey 	case IP_CT_ESTABLISHED_REPLY:
931b57dc7c1SPaul Blakey 		break;
932b57dc7c1SPaul Blakey 
933b57dc7c1SPaul Blakey 	default:
934b57dc7c1SPaul Blakey 		err = NF_DROP;
935b57dc7c1SPaul Blakey 		goto out;
936b57dc7c1SPaul Blakey 	}
937b57dc7c1SPaul Blakey 
938b57dc7c1SPaul Blakey 	err = nf_nat_packet(ct, ctinfo, hooknum, skb);
9396f022c2dSPaul Blakey 	if (err == NF_ACCEPT) {
9406f022c2dSPaul Blakey 		if (maniptype == NF_NAT_MANIP_SRC)
9416f022c2dSPaul Blakey 			tc_skb_cb(skb)->post_ct_snat = 1;
9426f022c2dSPaul Blakey 		if (maniptype == NF_NAT_MANIP_DST)
9436f022c2dSPaul Blakey 			tc_skb_cb(skb)->post_ct_dnat = 1;
9446f022c2dSPaul Blakey 	}
945b57dc7c1SPaul Blakey out:
946b57dc7c1SPaul Blakey 	return err;
947b57dc7c1SPaul Blakey }
948b57dc7c1SPaul Blakey #endif /* CONFIG_NF_NAT */
949b57dc7c1SPaul Blakey 
950b57dc7c1SPaul Blakey static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
951b57dc7c1SPaul Blakey {
952b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
953b57dc7c1SPaul Blakey 	u32 new_mark;
954b57dc7c1SPaul Blakey 
955b57dc7c1SPaul Blakey 	if (!mask)
956b57dc7c1SPaul Blakey 		return;
957b57dc7c1SPaul Blakey 
958b57dc7c1SPaul Blakey 	new_mark = mark | (ct->mark & ~(mask));
959b57dc7c1SPaul Blakey 	if (ct->mark != new_mark) {
960b57dc7c1SPaul Blakey 		ct->mark = new_mark;
961b57dc7c1SPaul Blakey 		if (nf_ct_is_confirmed(ct))
962b57dc7c1SPaul Blakey 			nf_conntrack_event_cache(IPCT_MARK, ct);
963b57dc7c1SPaul Blakey 	}
964b57dc7c1SPaul Blakey #endif
965b57dc7c1SPaul Blakey }
966b57dc7c1SPaul Blakey 
967b57dc7c1SPaul Blakey static void tcf_ct_act_set_labels(struct nf_conn *ct,
968b57dc7c1SPaul Blakey 				  u32 *labels,
969b57dc7c1SPaul Blakey 				  u32 *labels_m)
970b57dc7c1SPaul Blakey {
971b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
972c593642cSPankaj Bharadiya 	size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
973b57dc7c1SPaul Blakey 
974b57dc7c1SPaul Blakey 	if (!memchr_inv(labels_m, 0, labels_sz))
975b57dc7c1SPaul Blakey 		return;
976b57dc7c1SPaul Blakey 
977b57dc7c1SPaul Blakey 	nf_connlabels_replace(ct, labels, labels_m, 4);
978b57dc7c1SPaul Blakey #endif
979b57dc7c1SPaul Blakey }
980b57dc7c1SPaul Blakey 
981b57dc7c1SPaul Blakey static int tcf_ct_act_nat(struct sk_buff *skb,
982b57dc7c1SPaul Blakey 			  struct nf_conn *ct,
983b57dc7c1SPaul Blakey 			  enum ip_conntrack_info ctinfo,
984b57dc7c1SPaul Blakey 			  int ct_action,
985b57dc7c1SPaul Blakey 			  struct nf_nat_range2 *range,
986b57dc7c1SPaul Blakey 			  bool commit)
987b57dc7c1SPaul Blakey {
988b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT)
98995219afbSAaron Conole 	int err;
990b57dc7c1SPaul Blakey 	enum nf_nat_manip_type maniptype;
991b57dc7c1SPaul Blakey 
992b57dc7c1SPaul Blakey 	if (!(ct_action & TCA_CT_ACT_NAT))
993b57dc7c1SPaul Blakey 		return NF_ACCEPT;
994b57dc7c1SPaul Blakey 
995b57dc7c1SPaul Blakey 	/* Add NAT extension if not confirmed yet. */
996b57dc7c1SPaul Blakey 	if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
997b57dc7c1SPaul Blakey 		return NF_DROP;   /* Can't NAT. */
998b57dc7c1SPaul Blakey 
999b57dc7c1SPaul Blakey 	if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
1000b57dc7c1SPaul Blakey 	    (ctinfo != IP_CT_RELATED || commit)) {
1001b57dc7c1SPaul Blakey 		/* NAT an established or related connection like before. */
1002b57dc7c1SPaul Blakey 		if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
1003b57dc7c1SPaul Blakey 			/* This is the REPLY direction for a connection
1004b57dc7c1SPaul Blakey 			 * for which NAT was applied in the forward
1005b57dc7c1SPaul Blakey 			 * direction.  Do the reverse NAT.
1006b57dc7c1SPaul Blakey 			 */
1007b57dc7c1SPaul Blakey 			maniptype = ct->status & IPS_SRC_NAT
1008b57dc7c1SPaul Blakey 				? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
1009b57dc7c1SPaul Blakey 		else
1010b57dc7c1SPaul Blakey 			maniptype = ct->status & IPS_SRC_NAT
1011b57dc7c1SPaul Blakey 				? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
1012b57dc7c1SPaul Blakey 	} else if (ct_action & TCA_CT_ACT_NAT_SRC) {
1013b57dc7c1SPaul Blakey 		maniptype = NF_NAT_MANIP_SRC;
1014b57dc7c1SPaul Blakey 	} else if (ct_action & TCA_CT_ACT_NAT_DST) {
1015b57dc7c1SPaul Blakey 		maniptype = NF_NAT_MANIP_DST;
1016b57dc7c1SPaul Blakey 	} else {
1017b57dc7c1SPaul Blakey 		return NF_ACCEPT;
1018b57dc7c1SPaul Blakey 	}
1019b57dc7c1SPaul Blakey 
102095219afbSAaron Conole 	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
102113c62f53SMarcelo Ricardo Leitner 	if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
102213c62f53SMarcelo Ricardo Leitner 		if (ct->status & IPS_SRC_NAT) {
102395219afbSAaron Conole 			if (maniptype == NF_NAT_MANIP_SRC)
102495219afbSAaron Conole 				maniptype = NF_NAT_MANIP_DST;
102595219afbSAaron Conole 			else
102695219afbSAaron Conole 				maniptype = NF_NAT_MANIP_SRC;
102795219afbSAaron Conole 
102813c62f53SMarcelo Ricardo Leitner 			err = ct_nat_execute(skb, ct, ctinfo, range,
102913c62f53SMarcelo Ricardo Leitner 					     maniptype);
103013c62f53SMarcelo Ricardo Leitner 		} else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
103113c62f53SMarcelo Ricardo Leitner 			err = ct_nat_execute(skb, ct, ctinfo, NULL,
103213c62f53SMarcelo Ricardo Leitner 					     NF_NAT_MANIP_SRC);
103313c62f53SMarcelo Ricardo Leitner 		}
103495219afbSAaron Conole 	}
103595219afbSAaron Conole 	return err;
1036b57dc7c1SPaul Blakey #else
1037b57dc7c1SPaul Blakey 	return NF_ACCEPT;
1038b57dc7c1SPaul Blakey #endif
1039b57dc7c1SPaul Blakey }
1040b57dc7c1SPaul Blakey 
1041b57dc7c1SPaul Blakey static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
1042b57dc7c1SPaul Blakey 		      struct tcf_result *res)
1043b57dc7c1SPaul Blakey {
1044b57dc7c1SPaul Blakey 	struct net *net = dev_net(skb->dev);
1045b57dc7c1SPaul Blakey 	enum ip_conntrack_info ctinfo;
1046b57dc7c1SPaul Blakey 	struct tcf_ct *c = to_ct(a);
1047b57dc7c1SPaul Blakey 	struct nf_conn *tmpl = NULL;
1048b57dc7c1SPaul Blakey 	struct nf_hook_state state;
1049*a21b06e7SXin Long 	bool cached, commit, clear;
1050b57dc7c1SPaul Blakey 	int nh_ofs, err, retval;
1051b57dc7c1SPaul Blakey 	struct tcf_ct_params *p;
1052*a21b06e7SXin Long 	bool add_helper = false;
105346475bb2SPaul Blakey 	bool skip_add = false;
1054ae372cb1Swenxu 	bool defrag = false;
1055b57dc7c1SPaul Blakey 	struct nf_conn *ct;
1056b57dc7c1SPaul Blakey 	u8 family;
1057b57dc7c1SPaul Blakey 
1058b57dc7c1SPaul Blakey 	p = rcu_dereference_bh(c->params);
1059b57dc7c1SPaul Blakey 
1060b57dc7c1SPaul Blakey 	retval = READ_ONCE(c->tcf_action);
1061b57dc7c1SPaul Blakey 	commit = p->ct_action & TCA_CT_ACT_COMMIT;
1062b57dc7c1SPaul Blakey 	clear = p->ct_action & TCA_CT_ACT_CLEAR;
1063b57dc7c1SPaul Blakey 	tmpl = p->tmpl;
1064b57dc7c1SPaul Blakey 
10658367b3abSwenxu 	tcf_lastuse_update(&c->tcf_tm);
10662dc4e9e8SPaul Blakey 	tcf_action_update_bstats(&c->common, skb);
10678367b3abSwenxu 
1068b57dc7c1SPaul Blakey 	if (clear) {
1069ec624fe7SPaul Blakey 		tc_skb_cb(skb)->post_ct = false;
1070b57dc7c1SPaul Blakey 		ct = nf_ct_get(skb, &ctinfo);
1071b57dc7c1SPaul Blakey 		if (ct) {
1072408bdcfcSFlorian Westphal 			nf_ct_put(ct);
1073b57dc7c1SPaul Blakey 			nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1074b57dc7c1SPaul Blakey 		}
1075b57dc7c1SPaul Blakey 
10768ca1b090SMarcelo Ricardo Leitner 		goto out_clear;
1077b57dc7c1SPaul Blakey 	}
1078b57dc7c1SPaul Blakey 
1079b57dc7c1SPaul Blakey 	family = tcf_ct_skb_nf_family(skb);
1080b57dc7c1SPaul Blakey 	if (family == NFPROTO_UNSPEC)
1081b57dc7c1SPaul Blakey 		goto drop;
1082b57dc7c1SPaul Blakey 
1083b57dc7c1SPaul Blakey 	/* The conntrack module expects to be working at L3.
1084b57dc7c1SPaul Blakey 	 * We also try to pull the IPv4/6 header to linear area
1085b57dc7c1SPaul Blakey 	 */
1086b57dc7c1SPaul Blakey 	nh_ofs = skb_network_offset(skb);
1087b57dc7c1SPaul Blakey 	skb_pull_rcsum(skb, nh_ofs);
1088ae372cb1Swenxu 	err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
1089b57dc7c1SPaul Blakey 	if (err == -EINPROGRESS) {
1090b57dc7c1SPaul Blakey 		retval = TC_ACT_STOLEN;
1091f77bd544SDavide Caratti 		goto out_clear;
1092b57dc7c1SPaul Blakey 	}
1093b57dc7c1SPaul Blakey 	if (err)
1094b57dc7c1SPaul Blakey 		goto drop;
1095b57dc7c1SPaul Blakey 
1096b57dc7c1SPaul Blakey 	err = tcf_ct_skb_network_trim(skb, family);
1097b57dc7c1SPaul Blakey 	if (err)
1098b57dc7c1SPaul Blakey 		goto drop;
1099b57dc7c1SPaul Blakey 
1100b57dc7c1SPaul Blakey 	/* If we are recirculating packets to match on ct fields and
1101b57dc7c1SPaul Blakey 	 * committing with a separate ct action, then we don't need to
1102b57dc7c1SPaul Blakey 	 * actually run the packet through conntrack twice unless it's for a
1103b57dc7c1SPaul Blakey 	 * different zone.
1104b57dc7c1SPaul Blakey 	 */
1105*a21b06e7SXin Long 	cached = tcf_ct_skb_nfct_cached(net, skb, p);
1106b57dc7c1SPaul Blakey 	if (!cached) {
11070cc254e5SPaul Blakey 		if (tcf_ct_flow_table_lookup(p, skb, family)) {
110846475bb2SPaul Blakey 			skip_add = true;
110946475bb2SPaul Blakey 			goto do_nat;
111046475bb2SPaul Blakey 		}
111146475bb2SPaul Blakey 
1112b57dc7c1SPaul Blakey 		/* Associate skb with specified zone. */
1113b57dc7c1SPaul Blakey 		if (tmpl) {
1114b57dc7c1SPaul Blakey 			nf_conntrack_put(skb_nfct(skb));
1115b57dc7c1SPaul Blakey 			nf_conntrack_get(&tmpl->ct_general);
1116b57dc7c1SPaul Blakey 			nf_ct_set(skb, tmpl, IP_CT_NEW);
1117b57dc7c1SPaul Blakey 		}
1118b57dc7c1SPaul Blakey 
1119b57dc7c1SPaul Blakey 		state.hook = NF_INET_PRE_ROUTING;
1120b57dc7c1SPaul Blakey 		state.net = net;
1121b57dc7c1SPaul Blakey 		state.pf = family;
1122b57dc7c1SPaul Blakey 		err = nf_conntrack_in(skb, &state);
1123b57dc7c1SPaul Blakey 		if (err != NF_ACCEPT)
1124b57dc7c1SPaul Blakey 			goto out_push;
1125b57dc7c1SPaul Blakey 	}
1126b57dc7c1SPaul Blakey 
112746475bb2SPaul Blakey do_nat:
1128b57dc7c1SPaul Blakey 	ct = nf_ct_get(skb, &ctinfo);
1129b57dc7c1SPaul Blakey 	if (!ct)
1130b57dc7c1SPaul Blakey 		goto out_push;
1131b57dc7c1SPaul Blakey 	nf_ct_deliver_cached_events(ct);
11329795ded7SPaul Blakey 	nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
1133b57dc7c1SPaul Blakey 
1134b57dc7c1SPaul Blakey 	err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1135b57dc7c1SPaul Blakey 	if (err != NF_ACCEPT)
1136b57dc7c1SPaul Blakey 		goto drop;
1137b57dc7c1SPaul Blakey 
1138*a21b06e7SXin Long 	if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) {
1139*a21b06e7SXin Long 		err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
1140*a21b06e7SXin Long 		if (err)
1141*a21b06e7SXin Long 			goto drop;
1142*a21b06e7SXin Long 		add_helper = true;
1143*a21b06e7SXin Long 		if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
1144*a21b06e7SXin Long 			if (!nfct_seqadj_ext_add(ct))
1145*a21b06e7SXin Long 				goto drop;
1146*a21b06e7SXin Long 		}
1147*a21b06e7SXin Long 	}
1148*a21b06e7SXin Long 
1149*a21b06e7SXin Long 	if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
1150*a21b06e7SXin Long 		if (nf_ct_helper(skb, ct, ctinfo, family) != NF_ACCEPT)
1151*a21b06e7SXin Long 			goto drop;
1152*a21b06e7SXin Long 	}
1153*a21b06e7SXin Long 
1154b57dc7c1SPaul Blakey 	if (commit) {
1155b57dc7c1SPaul Blakey 		tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1156b57dc7c1SPaul Blakey 		tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1157b57dc7c1SPaul Blakey 
11589795ded7SPaul Blakey 		if (!nf_ct_is_confirmed(ct))
11599795ded7SPaul Blakey 			nf_conn_act_ct_ext_add(ct);
11609795ded7SPaul Blakey 
1161b57dc7c1SPaul Blakey 		/* This will take care of sending queued events
1162b57dc7c1SPaul Blakey 		 * even if the connection is already confirmed.
1163b57dc7c1SPaul Blakey 		 */
11648955b90cSwenxu 		if (nf_conntrack_confirm(skb) != NF_ACCEPT)
11658955b90cSwenxu 			goto drop;
116646475bb2SPaul Blakey 	}
116764ff70b8SPaul Blakey 
11680cc254e5SPaul Blakey 	if (!skip_add)
11690cc254e5SPaul Blakey 		tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
11700cc254e5SPaul Blakey 
1171b57dc7c1SPaul Blakey out_push:
1172b57dc7c1SPaul Blakey 	skb_push_rcsum(skb, nh_ofs);
1173b57dc7c1SPaul Blakey 
1174ec624fe7SPaul Blakey 	tc_skb_cb(skb)->post_ct = true;
117538495958SPaul Blakey 	tc_skb_cb(skb)->zone = p->zone;
11768ca1b090SMarcelo Ricardo Leitner out_clear:
1177ae372cb1Swenxu 	if (defrag)
1178ae372cb1Swenxu 		qdisc_skb_cb(skb)->pkt_len = skb->len;
1179b57dc7c1SPaul Blakey 	return retval;
1180b57dc7c1SPaul Blakey 
1181b57dc7c1SPaul Blakey drop:
118226b537a8SVlad Buslov 	tcf_action_inc_drop_qstats(&c->common);
1183b57dc7c1SPaul Blakey 	return TC_ACT_SHOT;
1184b57dc7c1SPaul Blakey }
1185b57dc7c1SPaul Blakey 
1186b57dc7c1SPaul Blakey static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1187b57dc7c1SPaul Blakey 	[TCA_CT_ACTION] = { .type = NLA_U16 },
11888140860cSJohannes Berg 	[TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1189b57dc7c1SPaul Blakey 	[TCA_CT_ZONE] = { .type = NLA_U16 },
1190b57dc7c1SPaul Blakey 	[TCA_CT_MARK] = { .type = NLA_U32 },
1191b57dc7c1SPaul Blakey 	[TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1192b57dc7c1SPaul Blakey 	[TCA_CT_LABELS] = { .type = NLA_BINARY,
1193b57dc7c1SPaul Blakey 			    .len = 128 / BITS_PER_BYTE },
1194b57dc7c1SPaul Blakey 	[TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1195b57dc7c1SPaul Blakey 				 .len = 128 / BITS_PER_BYTE },
1196b57dc7c1SPaul Blakey 	[TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1197b57dc7c1SPaul Blakey 	[TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
11988140860cSJohannes Berg 	[TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
11998140860cSJohannes Berg 	[TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1200b57dc7c1SPaul Blakey 	[TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1201b57dc7c1SPaul Blakey 	[TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1202*a21b06e7SXin Long 	[TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN },
1203*a21b06e7SXin Long 	[TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 },
1204*a21b06e7SXin Long 	[TCA_CT_HELPER_PROTO] = { .type = NLA_U8 },
1205b57dc7c1SPaul Blakey };
1206b57dc7c1SPaul Blakey 
1207b57dc7c1SPaul Blakey static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1208b57dc7c1SPaul Blakey 				  struct tc_ct *parm,
1209b57dc7c1SPaul Blakey 				  struct nlattr **tb,
1210b57dc7c1SPaul Blakey 				  struct netlink_ext_ack *extack)
1211b57dc7c1SPaul Blakey {
1212b57dc7c1SPaul Blakey 	struct nf_nat_range2 *range;
1213b57dc7c1SPaul Blakey 
1214b57dc7c1SPaul Blakey 	if (!(p->ct_action & TCA_CT_ACT_NAT))
1215b57dc7c1SPaul Blakey 		return 0;
1216b57dc7c1SPaul Blakey 
1217b57dc7c1SPaul Blakey 	if (!IS_ENABLED(CONFIG_NF_NAT)) {
1218b57dc7c1SPaul Blakey 		NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1219b57dc7c1SPaul Blakey 		return -EOPNOTSUPP;
1220b57dc7c1SPaul Blakey 	}
1221b57dc7c1SPaul Blakey 
1222b57dc7c1SPaul Blakey 	if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1223b57dc7c1SPaul Blakey 		return 0;
1224b57dc7c1SPaul Blakey 
1225b57dc7c1SPaul Blakey 	if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1226b57dc7c1SPaul Blakey 	    (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1227b57dc7c1SPaul Blakey 		NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1228b57dc7c1SPaul Blakey 		return -EOPNOTSUPP;
1229b57dc7c1SPaul Blakey 	}
1230b57dc7c1SPaul Blakey 
1231b57dc7c1SPaul Blakey 	range = &p->range;
1232b57dc7c1SPaul Blakey 	if (tb[TCA_CT_NAT_IPV4_MIN]) {
1233b57dc7c1SPaul Blakey 		struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1234b57dc7c1SPaul Blakey 
1235b57dc7c1SPaul Blakey 		p->ipv4_range = true;
1236b57dc7c1SPaul Blakey 		range->flags |= NF_NAT_RANGE_MAP_IPS;
1237b57dc7c1SPaul Blakey 		range->min_addr.ip =
1238b57dc7c1SPaul Blakey 			nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1239b57dc7c1SPaul Blakey 
1240b57dc7c1SPaul Blakey 		range->max_addr.ip = max_attr ?
1241b57dc7c1SPaul Blakey 				     nla_get_in_addr(max_attr) :
1242b57dc7c1SPaul Blakey 				     range->min_addr.ip;
1243b57dc7c1SPaul Blakey 	} else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1244b57dc7c1SPaul Blakey 		struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1245b57dc7c1SPaul Blakey 
1246b57dc7c1SPaul Blakey 		p->ipv4_range = false;
1247b57dc7c1SPaul Blakey 		range->flags |= NF_NAT_RANGE_MAP_IPS;
1248b57dc7c1SPaul Blakey 		range->min_addr.in6 =
1249b57dc7c1SPaul Blakey 			nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1250b57dc7c1SPaul Blakey 
1251b57dc7c1SPaul Blakey 		range->max_addr.in6 = max_attr ?
1252b57dc7c1SPaul Blakey 				      nla_get_in6_addr(max_attr) :
1253b57dc7c1SPaul Blakey 				      range->min_addr.in6;
1254b57dc7c1SPaul Blakey 	}
1255b57dc7c1SPaul Blakey 
1256b57dc7c1SPaul Blakey 	if (tb[TCA_CT_NAT_PORT_MIN]) {
1257b57dc7c1SPaul Blakey 		range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1258b57dc7c1SPaul Blakey 		range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1259b57dc7c1SPaul Blakey 
1260b57dc7c1SPaul Blakey 		range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1261b57dc7c1SPaul Blakey 				       nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1262b57dc7c1SPaul Blakey 				       range->min_proto.all;
1263b57dc7c1SPaul Blakey 	}
1264b57dc7c1SPaul Blakey 
1265b57dc7c1SPaul Blakey 	return 0;
1266b57dc7c1SPaul Blakey }
1267b57dc7c1SPaul Blakey 
1268b57dc7c1SPaul Blakey static void tcf_ct_set_key_val(struct nlattr **tb,
1269b57dc7c1SPaul Blakey 			       void *val, int val_type,
1270b57dc7c1SPaul Blakey 			       void *mask, int mask_type,
1271b57dc7c1SPaul Blakey 			       int len)
1272b57dc7c1SPaul Blakey {
1273b57dc7c1SPaul Blakey 	if (!tb[val_type])
1274b57dc7c1SPaul Blakey 		return;
1275b57dc7c1SPaul Blakey 	nla_memcpy(val, tb[val_type], len);
1276b57dc7c1SPaul Blakey 
1277b57dc7c1SPaul Blakey 	if (!mask)
1278b57dc7c1SPaul Blakey 		return;
1279b57dc7c1SPaul Blakey 
1280b57dc7c1SPaul Blakey 	if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1281b57dc7c1SPaul Blakey 		memset(mask, 0xff, len);
1282b57dc7c1SPaul Blakey 	else
1283b57dc7c1SPaul Blakey 		nla_memcpy(mask, tb[mask_type], len);
1284b57dc7c1SPaul Blakey }
1285b57dc7c1SPaul Blakey 
1286b57dc7c1SPaul Blakey static int tcf_ct_fill_params(struct net *net,
1287b57dc7c1SPaul Blakey 			      struct tcf_ct_params *p,
1288b57dc7c1SPaul Blakey 			      struct tc_ct *parm,
1289b57dc7c1SPaul Blakey 			      struct nlattr **tb,
1290b57dc7c1SPaul Blakey 			      struct netlink_ext_ack *extack)
1291b57dc7c1SPaul Blakey {
1292acd0a7abSZhengchao Shao 	struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1293b57dc7c1SPaul Blakey 	struct nf_conntrack_zone zone;
1294*a21b06e7SXin Long 	int err, family, proto, len;
1295b57dc7c1SPaul Blakey 	struct nf_conn *tmpl;
1296*a21b06e7SXin Long 	char *name;
1297b57dc7c1SPaul Blakey 
1298b57dc7c1SPaul Blakey 	p->zone = NF_CT_DEFAULT_ZONE_ID;
1299b57dc7c1SPaul Blakey 
1300b57dc7c1SPaul Blakey 	tcf_ct_set_key_val(tb,
1301b57dc7c1SPaul Blakey 			   &p->ct_action, TCA_CT_ACTION,
1302b57dc7c1SPaul Blakey 			   NULL, TCA_CT_UNSPEC,
1303b57dc7c1SPaul Blakey 			   sizeof(p->ct_action));
1304b57dc7c1SPaul Blakey 
1305b57dc7c1SPaul Blakey 	if (p->ct_action & TCA_CT_ACT_CLEAR)
1306b57dc7c1SPaul Blakey 		return 0;
1307b57dc7c1SPaul Blakey 
1308b57dc7c1SPaul Blakey 	err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1309b57dc7c1SPaul Blakey 	if (err)
1310b57dc7c1SPaul Blakey 		return err;
1311b57dc7c1SPaul Blakey 
1312b57dc7c1SPaul Blakey 	if (tb[TCA_CT_MARK]) {
1313b57dc7c1SPaul Blakey 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1314b57dc7c1SPaul Blakey 			NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1315b57dc7c1SPaul Blakey 			return -EOPNOTSUPP;
1316b57dc7c1SPaul Blakey 		}
1317b57dc7c1SPaul Blakey 		tcf_ct_set_key_val(tb,
1318b57dc7c1SPaul Blakey 				   &p->mark, TCA_CT_MARK,
1319b57dc7c1SPaul Blakey 				   &p->mark_mask, TCA_CT_MARK_MASK,
1320b57dc7c1SPaul Blakey 				   sizeof(p->mark));
1321b57dc7c1SPaul Blakey 	}
1322b57dc7c1SPaul Blakey 
1323b57dc7c1SPaul Blakey 	if (tb[TCA_CT_LABELS]) {
1324b57dc7c1SPaul Blakey 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1325b57dc7c1SPaul Blakey 			NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1326b57dc7c1SPaul Blakey 			return -EOPNOTSUPP;
1327b57dc7c1SPaul Blakey 		}
1328b57dc7c1SPaul Blakey 
1329b57dc7c1SPaul Blakey 		if (!tn->labels) {
1330b57dc7c1SPaul Blakey 			NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1331b57dc7c1SPaul Blakey 			return -EOPNOTSUPP;
1332b57dc7c1SPaul Blakey 		}
1333b57dc7c1SPaul Blakey 		tcf_ct_set_key_val(tb,
1334b57dc7c1SPaul Blakey 				   p->labels, TCA_CT_LABELS,
1335b57dc7c1SPaul Blakey 				   p->labels_mask, TCA_CT_LABELS_MASK,
1336b57dc7c1SPaul Blakey 				   sizeof(p->labels));
1337b57dc7c1SPaul Blakey 	}
1338b57dc7c1SPaul Blakey 
1339b57dc7c1SPaul Blakey 	if (tb[TCA_CT_ZONE]) {
1340b57dc7c1SPaul Blakey 		if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1341b57dc7c1SPaul Blakey 			NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1342b57dc7c1SPaul Blakey 			return -EOPNOTSUPP;
1343b57dc7c1SPaul Blakey 		}
1344b57dc7c1SPaul Blakey 
1345b57dc7c1SPaul Blakey 		tcf_ct_set_key_val(tb,
1346b57dc7c1SPaul Blakey 				   &p->zone, TCA_CT_ZONE,
1347b57dc7c1SPaul Blakey 				   NULL, TCA_CT_UNSPEC,
1348b57dc7c1SPaul Blakey 				   sizeof(p->zone));
1349b57dc7c1SPaul Blakey 	}
1350b57dc7c1SPaul Blakey 
1351b57dc7c1SPaul Blakey 	nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1352b57dc7c1SPaul Blakey 	tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1353b57dc7c1SPaul Blakey 	if (!tmpl) {
1354b57dc7c1SPaul Blakey 		NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1355b57dc7c1SPaul Blakey 		return -ENOMEM;
1356b57dc7c1SPaul Blakey 	}
1357b57dc7c1SPaul Blakey 	p->tmpl = tmpl;
1358*a21b06e7SXin Long 	if (tb[TCA_CT_HELPER_NAME]) {
1359*a21b06e7SXin Long 		name = nla_data(tb[TCA_CT_HELPER_NAME]);
1360*a21b06e7SXin Long 		len = nla_len(tb[TCA_CT_HELPER_NAME]);
1361*a21b06e7SXin Long 		if (len > 16 || name[len - 1] != '\0') {
1362*a21b06e7SXin Long 			NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name.");
1363*a21b06e7SXin Long 			err = -EINVAL;
1364*a21b06e7SXin Long 			goto err;
1365*a21b06e7SXin Long 		}
1366*a21b06e7SXin Long 		family = tb[TCA_CT_HELPER_FAMILY] ? nla_get_u8(tb[TCA_CT_HELPER_FAMILY]) : AF_INET;
1367*a21b06e7SXin Long 		proto = tb[TCA_CT_HELPER_PROTO] ? nla_get_u8(tb[TCA_CT_HELPER_PROTO]) : IPPROTO_TCP;
1368*a21b06e7SXin Long 		err = nf_ct_add_helper(tmpl, name, family, proto,
1369*a21b06e7SXin Long 				       p->ct_action & TCA_CT_ACT_NAT, &p->helper);
1370*a21b06e7SXin Long 		if (err) {
1371*a21b06e7SXin Long 			NL_SET_ERR_MSG_MOD(extack, "Failed to add helper");
1372*a21b06e7SXin Long 			goto err;
1373*a21b06e7SXin Long 		}
1374*a21b06e7SXin Long 	}
1375b57dc7c1SPaul Blakey 
1376*a21b06e7SXin Long 	__set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1377b57dc7c1SPaul Blakey 	return 0;
1378*a21b06e7SXin Long err:
1379*a21b06e7SXin Long 	nf_ct_put(p->tmpl);
1380*a21b06e7SXin Long 	p->tmpl = NULL;
1381*a21b06e7SXin Long 	return err;
1382b57dc7c1SPaul Blakey }
1383b57dc7c1SPaul Blakey 
1384b57dc7c1SPaul Blakey static int tcf_ct_init(struct net *net, struct nlattr *nla,
1385b57dc7c1SPaul Blakey 		       struct nlattr *est, struct tc_action **a,
1386abbb0d33SVlad Buslov 		       struct tcf_proto *tp, u32 flags,
1387b57dc7c1SPaul Blakey 		       struct netlink_ext_ack *extack)
1388b57dc7c1SPaul Blakey {
1389acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id);
1390695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
1391b57dc7c1SPaul Blakey 	struct tcf_ct_params *params = NULL;
1392b57dc7c1SPaul Blakey 	struct nlattr *tb[TCA_CT_MAX + 1];
1393b57dc7c1SPaul Blakey 	struct tcf_chain *goto_ch = NULL;
1394b57dc7c1SPaul Blakey 	struct tc_ct *parm;
1395b57dc7c1SPaul Blakey 	struct tcf_ct *c;
1396b57dc7c1SPaul Blakey 	int err, res = 0;
13977be8ef2cSDmytro Linkin 	u32 index;
1398b57dc7c1SPaul Blakey 
1399b57dc7c1SPaul Blakey 	if (!nla) {
1400b57dc7c1SPaul Blakey 		NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1401b57dc7c1SPaul Blakey 		return -EINVAL;
1402b57dc7c1SPaul Blakey 	}
1403b57dc7c1SPaul Blakey 
1404b57dc7c1SPaul Blakey 	err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1405b57dc7c1SPaul Blakey 	if (err < 0)
1406b57dc7c1SPaul Blakey 		return err;
1407b57dc7c1SPaul Blakey 
1408b57dc7c1SPaul Blakey 	if (!tb[TCA_CT_PARMS]) {
1409b57dc7c1SPaul Blakey 		NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1410b57dc7c1SPaul Blakey 		return -EINVAL;
1411b57dc7c1SPaul Blakey 	}
1412b57dc7c1SPaul Blakey 	parm = nla_data(tb[TCA_CT_PARMS]);
14137be8ef2cSDmytro Linkin 	index = parm->index;
14147be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
1415b57dc7c1SPaul Blakey 	if (err < 0)
1416b57dc7c1SPaul Blakey 		return err;
1417b57dc7c1SPaul Blakey 
1418b57dc7c1SPaul Blakey 	if (!err) {
1419e3822678SVlad Buslov 		err = tcf_idr_create_from_flags(tn, index, est, a,
1420e3822678SVlad Buslov 						&act_ct_ops, bind, flags);
1421b57dc7c1SPaul Blakey 		if (err) {
14227be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
1423b57dc7c1SPaul Blakey 			return err;
1424b57dc7c1SPaul Blakey 		}
1425b57dc7c1SPaul Blakey 		res = ACT_P_CREATED;
1426b57dc7c1SPaul Blakey 	} else {
1427b57dc7c1SPaul Blakey 		if (bind)
1428b57dc7c1SPaul Blakey 			return 0;
1429b57dc7c1SPaul Blakey 
1430695176bfSCong Wang 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
1431b57dc7c1SPaul Blakey 			tcf_idr_release(*a, bind);
1432b57dc7c1SPaul Blakey 			return -EEXIST;
1433b57dc7c1SPaul Blakey 		}
1434b57dc7c1SPaul Blakey 	}
1435b57dc7c1SPaul Blakey 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1436b57dc7c1SPaul Blakey 	if (err < 0)
1437b57dc7c1SPaul Blakey 		goto cleanup;
1438b57dc7c1SPaul Blakey 
1439b57dc7c1SPaul Blakey 	c = to_ct(*a);
1440b57dc7c1SPaul Blakey 
1441b57dc7c1SPaul Blakey 	params = kzalloc(sizeof(*params), GFP_KERNEL);
1442b57dc7c1SPaul Blakey 	if (unlikely(!params)) {
1443b57dc7c1SPaul Blakey 		err = -ENOMEM;
1444b57dc7c1SPaul Blakey 		goto cleanup;
1445b57dc7c1SPaul Blakey 	}
1446b57dc7c1SPaul Blakey 
1447b57dc7c1SPaul Blakey 	err = tcf_ct_fill_params(net, params, parm, tb, extack);
1448b57dc7c1SPaul Blakey 	if (err)
1449b57dc7c1SPaul Blakey 		goto cleanup;
1450b57dc7c1SPaul Blakey 
1451fc54d906SVlad Buslov 	err = tcf_ct_flow_table_get(net, params);
1452c34b961aSPaul Blakey 	if (err)
145319138941SXin Long 		goto cleanup;
1454c34b961aSPaul Blakey 
1455b57dc7c1SPaul Blakey 	spin_lock_bh(&c->tcf_lock);
1456b57dc7c1SPaul Blakey 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1457445d3749SPaul E. McKenney 	params = rcu_replace_pointer(c->params, params,
1458445d3749SPaul E. McKenney 				     lockdep_is_held(&c->tcf_lock));
1459b57dc7c1SPaul Blakey 	spin_unlock_bh(&c->tcf_lock);
1460b57dc7c1SPaul Blakey 
1461b57dc7c1SPaul Blakey 	if (goto_ch)
1462b57dc7c1SPaul Blakey 		tcf_chain_put_by_act(goto_ch);
1463b57dc7c1SPaul Blakey 	if (params)
146419138941SXin Long 		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
1465b57dc7c1SPaul Blakey 
1466b57dc7c1SPaul Blakey 	return res;
1467b57dc7c1SPaul Blakey 
1468b57dc7c1SPaul Blakey cleanup:
1469b57dc7c1SPaul Blakey 	if (goto_ch)
1470b57dc7c1SPaul Blakey 		tcf_chain_put_by_act(goto_ch);
147119138941SXin Long 	if (params)
147219138941SXin Long 		tcf_ct_params_free(params);
1473b57dc7c1SPaul Blakey 	tcf_idr_release(*a, bind);
1474b57dc7c1SPaul Blakey 	return err;
1475b57dc7c1SPaul Blakey }
1476b57dc7c1SPaul Blakey 
1477b57dc7c1SPaul Blakey static void tcf_ct_cleanup(struct tc_action *a)
1478b57dc7c1SPaul Blakey {
1479b57dc7c1SPaul Blakey 	struct tcf_ct_params *params;
1480b57dc7c1SPaul Blakey 	struct tcf_ct *c = to_ct(a);
1481b57dc7c1SPaul Blakey 
1482b57dc7c1SPaul Blakey 	params = rcu_dereference_protected(c->params, 1);
1483b57dc7c1SPaul Blakey 	if (params)
148419138941SXin Long 		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
1485b57dc7c1SPaul Blakey }
1486b57dc7c1SPaul Blakey 
1487b57dc7c1SPaul Blakey static int tcf_ct_dump_key_val(struct sk_buff *skb,
1488b57dc7c1SPaul Blakey 			       void *val, int val_type,
1489b57dc7c1SPaul Blakey 			       void *mask, int mask_type,
1490b57dc7c1SPaul Blakey 			       int len)
1491b57dc7c1SPaul Blakey {
1492b57dc7c1SPaul Blakey 	int err;
1493b57dc7c1SPaul Blakey 
1494b57dc7c1SPaul Blakey 	if (mask && !memchr_inv(mask, 0, len))
1495b57dc7c1SPaul Blakey 		return 0;
1496b57dc7c1SPaul Blakey 
1497b57dc7c1SPaul Blakey 	err = nla_put(skb, val_type, len, val);
1498b57dc7c1SPaul Blakey 	if (err)
1499b57dc7c1SPaul Blakey 		return err;
1500b57dc7c1SPaul Blakey 
1501b57dc7c1SPaul Blakey 	if (mask_type != TCA_CT_UNSPEC) {
1502b57dc7c1SPaul Blakey 		err = nla_put(skb, mask_type, len, mask);
1503b57dc7c1SPaul Blakey 		if (err)
1504b57dc7c1SPaul Blakey 			return err;
1505b57dc7c1SPaul Blakey 	}
1506b57dc7c1SPaul Blakey 
1507b57dc7c1SPaul Blakey 	return 0;
1508b57dc7c1SPaul Blakey }
1509b57dc7c1SPaul Blakey 
1510b57dc7c1SPaul Blakey static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1511b57dc7c1SPaul Blakey {
1512b57dc7c1SPaul Blakey 	struct nf_nat_range2 *range = &p->range;
1513b57dc7c1SPaul Blakey 
1514b57dc7c1SPaul Blakey 	if (!(p->ct_action & TCA_CT_ACT_NAT))
1515b57dc7c1SPaul Blakey 		return 0;
1516b57dc7c1SPaul Blakey 
1517b57dc7c1SPaul Blakey 	if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1518b57dc7c1SPaul Blakey 		return 0;
1519b57dc7c1SPaul Blakey 
1520b57dc7c1SPaul Blakey 	if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1521b57dc7c1SPaul Blakey 		if (p->ipv4_range) {
1522b57dc7c1SPaul Blakey 			if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1523b57dc7c1SPaul Blakey 					    range->min_addr.ip))
1524b57dc7c1SPaul Blakey 				return -1;
1525b57dc7c1SPaul Blakey 			if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1526b57dc7c1SPaul Blakey 					    range->max_addr.ip))
1527b57dc7c1SPaul Blakey 				return -1;
1528b57dc7c1SPaul Blakey 		} else {
1529b57dc7c1SPaul Blakey 			if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1530b57dc7c1SPaul Blakey 					     &range->min_addr.in6))
1531b57dc7c1SPaul Blakey 				return -1;
1532b57dc7c1SPaul Blakey 			if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1533b57dc7c1SPaul Blakey 					     &range->max_addr.in6))
1534b57dc7c1SPaul Blakey 				return -1;
1535b57dc7c1SPaul Blakey 		}
1536b57dc7c1SPaul Blakey 	}
1537b57dc7c1SPaul Blakey 
1538b57dc7c1SPaul Blakey 	if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1539b57dc7c1SPaul Blakey 		if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1540b57dc7c1SPaul Blakey 				 range->min_proto.all))
1541b57dc7c1SPaul Blakey 			return -1;
1542b57dc7c1SPaul Blakey 		if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1543b57dc7c1SPaul Blakey 				 range->max_proto.all))
1544b57dc7c1SPaul Blakey 			return -1;
1545b57dc7c1SPaul Blakey 	}
1546b57dc7c1SPaul Blakey 
1547b57dc7c1SPaul Blakey 	return 0;
1548b57dc7c1SPaul Blakey }
1549b57dc7c1SPaul Blakey 
1550*a21b06e7SXin Long static int tcf_ct_dump_helper(struct sk_buff *skb, struct nf_conntrack_helper *helper)
1551*a21b06e7SXin Long {
1552*a21b06e7SXin Long 	if (!helper)
1553*a21b06e7SXin Long 		return 0;
1554*a21b06e7SXin Long 
1555*a21b06e7SXin Long 	if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) ||
1556*a21b06e7SXin Long 	    nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->tuple.src.l3num) ||
1557*a21b06e7SXin Long 	    nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->tuple.dst.protonum))
1558*a21b06e7SXin Long 		return -1;
1559*a21b06e7SXin Long 
1560*a21b06e7SXin Long 	return 0;
1561*a21b06e7SXin Long }
1562*a21b06e7SXin Long 
1563b57dc7c1SPaul Blakey static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1564b57dc7c1SPaul Blakey 			      int bind, int ref)
1565b57dc7c1SPaul Blakey {
1566b57dc7c1SPaul Blakey 	unsigned char *b = skb_tail_pointer(skb);
1567b57dc7c1SPaul Blakey 	struct tcf_ct *c = to_ct(a);
1568b57dc7c1SPaul Blakey 	struct tcf_ct_params *p;
1569b57dc7c1SPaul Blakey 
1570b57dc7c1SPaul Blakey 	struct tc_ct opt = {
1571b57dc7c1SPaul Blakey 		.index   = c->tcf_index,
1572b57dc7c1SPaul Blakey 		.refcnt  = refcount_read(&c->tcf_refcnt) - ref,
1573b57dc7c1SPaul Blakey 		.bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1574b57dc7c1SPaul Blakey 	};
1575b57dc7c1SPaul Blakey 	struct tcf_t t;
1576b57dc7c1SPaul Blakey 
1577b57dc7c1SPaul Blakey 	spin_lock_bh(&c->tcf_lock);
1578b57dc7c1SPaul Blakey 	p = rcu_dereference_protected(c->params,
1579b57dc7c1SPaul Blakey 				      lockdep_is_held(&c->tcf_lock));
1580b57dc7c1SPaul Blakey 	opt.action = c->tcf_action;
1581b57dc7c1SPaul Blakey 
1582b57dc7c1SPaul Blakey 	if (tcf_ct_dump_key_val(skb,
1583b57dc7c1SPaul Blakey 				&p->ct_action, TCA_CT_ACTION,
1584b57dc7c1SPaul Blakey 				NULL, TCA_CT_UNSPEC,
1585b57dc7c1SPaul Blakey 				sizeof(p->ct_action)))
1586b57dc7c1SPaul Blakey 		goto nla_put_failure;
1587b57dc7c1SPaul Blakey 
1588b57dc7c1SPaul Blakey 	if (p->ct_action & TCA_CT_ACT_CLEAR)
1589b57dc7c1SPaul Blakey 		goto skip_dump;
1590b57dc7c1SPaul Blakey 
1591b57dc7c1SPaul Blakey 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1592b57dc7c1SPaul Blakey 	    tcf_ct_dump_key_val(skb,
1593b57dc7c1SPaul Blakey 				&p->mark, TCA_CT_MARK,
1594b57dc7c1SPaul Blakey 				&p->mark_mask, TCA_CT_MARK_MASK,
1595b57dc7c1SPaul Blakey 				sizeof(p->mark)))
1596b57dc7c1SPaul Blakey 		goto nla_put_failure;
1597b57dc7c1SPaul Blakey 
1598b57dc7c1SPaul Blakey 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1599b57dc7c1SPaul Blakey 	    tcf_ct_dump_key_val(skb,
1600b57dc7c1SPaul Blakey 				p->labels, TCA_CT_LABELS,
1601b57dc7c1SPaul Blakey 				p->labels_mask, TCA_CT_LABELS_MASK,
1602b57dc7c1SPaul Blakey 				sizeof(p->labels)))
1603b57dc7c1SPaul Blakey 		goto nla_put_failure;
1604b57dc7c1SPaul Blakey 
1605b57dc7c1SPaul Blakey 	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1606b57dc7c1SPaul Blakey 	    tcf_ct_dump_key_val(skb,
1607b57dc7c1SPaul Blakey 				&p->zone, TCA_CT_ZONE,
1608b57dc7c1SPaul Blakey 				NULL, TCA_CT_UNSPEC,
1609b57dc7c1SPaul Blakey 				sizeof(p->zone)))
1610b57dc7c1SPaul Blakey 		goto nla_put_failure;
1611b57dc7c1SPaul Blakey 
1612b57dc7c1SPaul Blakey 	if (tcf_ct_dump_nat(skb, p))
1613b57dc7c1SPaul Blakey 		goto nla_put_failure;
1614b57dc7c1SPaul Blakey 
1615*a21b06e7SXin Long 	if (tcf_ct_dump_helper(skb, p->helper))
1616*a21b06e7SXin Long 		goto nla_put_failure;
1617*a21b06e7SXin Long 
1618b57dc7c1SPaul Blakey skip_dump:
1619b57dc7c1SPaul Blakey 	if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1620b57dc7c1SPaul Blakey 		goto nla_put_failure;
1621b57dc7c1SPaul Blakey 
1622b57dc7c1SPaul Blakey 	tcf_tm_dump(&t, &c->tcf_tm);
1623b57dc7c1SPaul Blakey 	if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1624b57dc7c1SPaul Blakey 		goto nla_put_failure;
1625b57dc7c1SPaul Blakey 	spin_unlock_bh(&c->tcf_lock);
1626b57dc7c1SPaul Blakey 
1627b57dc7c1SPaul Blakey 	return skb->len;
1628b57dc7c1SPaul Blakey nla_put_failure:
1629b57dc7c1SPaul Blakey 	spin_unlock_bh(&c->tcf_lock);
1630b57dc7c1SPaul Blakey 	nlmsg_trim(skb, b);
1631b57dc7c1SPaul Blakey 	return -1;
1632b57dc7c1SPaul Blakey }
1633b57dc7c1SPaul Blakey 
16344b61d3e8SPo Liu static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
16354b61d3e8SPo Liu 			     u64 drops, u64 lastuse, bool hw)
1636b57dc7c1SPaul Blakey {
1637b57dc7c1SPaul Blakey 	struct tcf_ct *c = to_ct(a);
1638b57dc7c1SPaul Blakey 
16394b61d3e8SPo Liu 	tcf_action_update_stats(a, bytes, packets, drops, hw);
1640b57dc7c1SPaul Blakey 	c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1641b57dc7c1SPaul Blakey }
1642b57dc7c1SPaul Blakey 
1643c54e1d92SBaowen Zheng static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data,
1644c2ccf84eSIdo Schimmel 				    u32 *index_inc, bool bind,
1645c2ccf84eSIdo Schimmel 				    struct netlink_ext_ack *extack)
1646c54e1d92SBaowen Zheng {
1647c54e1d92SBaowen Zheng 	if (bind) {
1648c54e1d92SBaowen Zheng 		struct flow_action_entry *entry = entry_data;
1649c54e1d92SBaowen Zheng 
1650c54e1d92SBaowen Zheng 		entry->id = FLOW_ACTION_CT;
1651c54e1d92SBaowen Zheng 		entry->ct.action = tcf_ct_action(act);
1652c54e1d92SBaowen Zheng 		entry->ct.zone = tcf_ct_zone(act);
1653c54e1d92SBaowen Zheng 		entry->ct.flow_table = tcf_ct_ft(act);
1654c54e1d92SBaowen Zheng 		*index_inc = 1;
1655c54e1d92SBaowen Zheng 	} else {
16568cbfe939SBaowen Zheng 		struct flow_offload_action *fl_action = entry_data;
16578cbfe939SBaowen Zheng 
16588cbfe939SBaowen Zheng 		fl_action->id = FLOW_ACTION_CT;
1659c54e1d92SBaowen Zheng 	}
1660c54e1d92SBaowen Zheng 
1661c54e1d92SBaowen Zheng 	return 0;
1662c54e1d92SBaowen Zheng }
1663c54e1d92SBaowen Zheng 
1664b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops = {
1665b57dc7c1SPaul Blakey 	.kind		=	"ct",
1666b57dc7c1SPaul Blakey 	.id		=	TCA_ID_CT,
1667b57dc7c1SPaul Blakey 	.owner		=	THIS_MODULE,
1668b57dc7c1SPaul Blakey 	.act		=	tcf_ct_act,
1669b57dc7c1SPaul Blakey 	.dump		=	tcf_ct_dump,
1670b57dc7c1SPaul Blakey 	.init		=	tcf_ct_init,
1671b57dc7c1SPaul Blakey 	.cleanup	=	tcf_ct_cleanup,
1672b57dc7c1SPaul Blakey 	.stats_update	=	tcf_stats_update,
1673c54e1d92SBaowen Zheng 	.offload_act_setup =	tcf_ct_offload_act_setup,
1674b57dc7c1SPaul Blakey 	.size		=	sizeof(struct tcf_ct),
1675b57dc7c1SPaul Blakey };
1676b57dc7c1SPaul Blakey 
1677b57dc7c1SPaul Blakey static __net_init int ct_init_net(struct net *net)
1678b57dc7c1SPaul Blakey {
1679c593642cSPankaj Bharadiya 	unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1680acd0a7abSZhengchao Shao 	struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1681b57dc7c1SPaul Blakey 
1682b57dc7c1SPaul Blakey 	if (nf_connlabels_get(net, n_bits - 1)) {
1683b57dc7c1SPaul Blakey 		tn->labels = false;
1684b57dc7c1SPaul Blakey 		pr_err("act_ct: Failed to set connlabels length");
1685b57dc7c1SPaul Blakey 	} else {
1686b57dc7c1SPaul Blakey 		tn->labels = true;
1687b57dc7c1SPaul Blakey 	}
1688b57dc7c1SPaul Blakey 
1689981471bdSCong Wang 	return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1690b57dc7c1SPaul Blakey }
1691b57dc7c1SPaul Blakey 
1692b57dc7c1SPaul Blakey static void __net_exit ct_exit_net(struct list_head *net_list)
1693b57dc7c1SPaul Blakey {
1694b57dc7c1SPaul Blakey 	struct net *net;
1695b57dc7c1SPaul Blakey 
1696b57dc7c1SPaul Blakey 	rtnl_lock();
1697b57dc7c1SPaul Blakey 	list_for_each_entry(net, net_list, exit_list) {
1698acd0a7abSZhengchao Shao 		struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id);
1699b57dc7c1SPaul Blakey 
1700b57dc7c1SPaul Blakey 		if (tn->labels)
1701b57dc7c1SPaul Blakey 			nf_connlabels_put(net);
1702b57dc7c1SPaul Blakey 	}
1703b57dc7c1SPaul Blakey 	rtnl_unlock();
1704b57dc7c1SPaul Blakey 
1705acd0a7abSZhengchao Shao 	tc_action_net_exit(net_list, act_ct_ops.net_id);
1706b57dc7c1SPaul Blakey }
1707b57dc7c1SPaul Blakey 
1708b57dc7c1SPaul Blakey static struct pernet_operations ct_net_ops = {
1709b57dc7c1SPaul Blakey 	.init = ct_init_net,
1710b57dc7c1SPaul Blakey 	.exit_batch = ct_exit_net,
1711acd0a7abSZhengchao Shao 	.id   = &act_ct_ops.net_id,
1712b57dc7c1SPaul Blakey 	.size = sizeof(struct tc_ct_action_net),
1713b57dc7c1SPaul Blakey };
1714b57dc7c1SPaul Blakey 
1715b57dc7c1SPaul Blakey static int __init ct_init_module(void)
1716b57dc7c1SPaul Blakey {
1717c34b961aSPaul Blakey 	int err;
1718c34b961aSPaul Blakey 
1719c34b961aSPaul Blakey 	act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1720c34b961aSPaul Blakey 	if (!act_ct_wq)
1721c34b961aSPaul Blakey 		return -ENOMEM;
1722c34b961aSPaul Blakey 
1723c34b961aSPaul Blakey 	err = tcf_ct_flow_tables_init();
1724c34b961aSPaul Blakey 	if (err)
1725c34b961aSPaul Blakey 		goto err_tbl_init;
1726c34b961aSPaul Blakey 
1727c34b961aSPaul Blakey 	err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1728c34b961aSPaul Blakey 	if (err)
1729c34b961aSPaul Blakey 		goto err_register;
1730c34b961aSPaul Blakey 
1731c129412fSwenxu 	static_branch_inc(&tcf_frag_xmit_count);
1732c129412fSwenxu 
1733c34b961aSPaul Blakey 	return 0;
1734c34b961aSPaul Blakey 
1735c34b961aSPaul Blakey err_register:
1736c34b961aSPaul Blakey 	tcf_ct_flow_tables_uninit();
17378c5c51f5Sliujian err_tbl_init:
17388c5c51f5Sliujian 	destroy_workqueue(act_ct_wq);
1739c34b961aSPaul Blakey 	return err;
1740b57dc7c1SPaul Blakey }
1741b57dc7c1SPaul Blakey 
1742b57dc7c1SPaul Blakey static void __exit ct_cleanup_module(void)
1743b57dc7c1SPaul Blakey {
1744c129412fSwenxu 	static_branch_dec(&tcf_frag_xmit_count);
1745b57dc7c1SPaul Blakey 	tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1746c34b961aSPaul Blakey 	tcf_ct_flow_tables_uninit();
1747c34b961aSPaul Blakey 	destroy_workqueue(act_ct_wq);
1748b57dc7c1SPaul Blakey }
1749b57dc7c1SPaul Blakey 
1750b57dc7c1SPaul Blakey module_init(ct_init_module);
1751b57dc7c1SPaul Blakey module_exit(ct_cleanup_module);
1752b57dc7c1SPaul Blakey MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1753b57dc7c1SPaul Blakey MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1754b57dc7c1SPaul Blakey MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1755b57dc7c1SPaul Blakey MODULE_DESCRIPTION("Connection tracking action");
1756b57dc7c1SPaul Blakey MODULE_LICENSE("GPL v2");
1757