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> 3540d102cdSJeremy Sowden #include <uapi/linux/netfilter/nf_nat.h> 36b57dc7c1SPaul Blakey 37c34b961aSPaul Blakey static struct workqueue_struct *act_ct_wq; 38c34b961aSPaul Blakey static struct rhashtable zones_ht; 39138470a9SEric Dumazet static DEFINE_MUTEX(zones_mutex); 40c34b961aSPaul Blakey 41c34b961aSPaul Blakey struct tcf_ct_flow_table { 42c34b961aSPaul Blakey struct rhash_head node; /* In zones tables */ 43c34b961aSPaul Blakey 44c34b961aSPaul Blakey struct rcu_work rwork; 45c34b961aSPaul Blakey struct nf_flowtable nf_ft; 46138470a9SEric Dumazet refcount_t ref; 47c34b961aSPaul Blakey u16 zone; 48c34b961aSPaul Blakey 49c34b961aSPaul Blakey bool dying; 50c34b961aSPaul Blakey }; 51c34b961aSPaul Blakey 52c34b961aSPaul Blakey static const struct rhashtable_params zones_params = { 53c34b961aSPaul Blakey .head_offset = offsetof(struct tcf_ct_flow_table, node), 54c34b961aSPaul Blakey .key_offset = offsetof(struct tcf_ct_flow_table, zone), 55c34b961aSPaul Blakey .key_len = sizeof_field(struct tcf_ct_flow_table, zone), 56c34b961aSPaul Blakey .automatic_shrinking = true, 57c34b961aSPaul Blakey }; 58c34b961aSPaul Blakey 599c26ba9bSPaul Blakey static struct flow_action_entry * 609c26ba9bSPaul Blakey tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 619c26ba9bSPaul Blakey { 629c26ba9bSPaul Blakey int i = flow_action->num_entries++; 639c26ba9bSPaul Blakey 649c26ba9bSPaul Blakey return &flow_action->entries[i]; 659c26ba9bSPaul Blakey } 669c26ba9bSPaul Blakey 679c26ba9bSPaul Blakey static void tcf_ct_add_mangle_action(struct flow_action *action, 689c26ba9bSPaul Blakey enum flow_action_mangle_base htype, 699c26ba9bSPaul Blakey u32 offset, 709c26ba9bSPaul Blakey u32 mask, 719c26ba9bSPaul Blakey u32 val) 729c26ba9bSPaul Blakey { 739c26ba9bSPaul Blakey struct flow_action_entry *entry; 749c26ba9bSPaul Blakey 759c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 769c26ba9bSPaul Blakey entry->id = FLOW_ACTION_MANGLE; 779c26ba9bSPaul Blakey entry->mangle.htype = htype; 789c26ba9bSPaul Blakey entry->mangle.mask = ~mask; 799c26ba9bSPaul Blakey entry->mangle.offset = offset; 809c26ba9bSPaul Blakey entry->mangle.val = val; 819c26ba9bSPaul Blakey } 829c26ba9bSPaul Blakey 839c26ba9bSPaul Blakey /* The following nat helper functions check if the inverted reverse tuple 849c26ba9bSPaul Blakey * (target) is different then the current dir tuple - meaning nat for ports 859c26ba9bSPaul Blakey * and/or ip is needed, and add the relevant mangle actions. 869c26ba9bSPaul Blakey */ 879c26ba9bSPaul Blakey static void 889c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 899c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 909c26ba9bSPaul Blakey struct flow_action *action) 919c26ba9bSPaul Blakey { 929c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 939c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 949c26ba9bSPaul Blakey offsetof(struct iphdr, saddr), 959c26ba9bSPaul Blakey 0xFFFFFFFF, 969c26ba9bSPaul Blakey be32_to_cpu(target.src.u3.ip)); 979c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 989c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 999c26ba9bSPaul Blakey offsetof(struct iphdr, daddr), 1009c26ba9bSPaul Blakey 0xFFFFFFFF, 1019c26ba9bSPaul Blakey be32_to_cpu(target.dst.u3.ip)); 1029c26ba9bSPaul Blakey } 1039c26ba9bSPaul Blakey 1049c26ba9bSPaul Blakey static void 1059c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 1069c26ba9bSPaul Blakey union nf_inet_addr *addr, 1079c26ba9bSPaul Blakey u32 offset) 1089c26ba9bSPaul Blakey { 1099c26ba9bSPaul Blakey int i; 1109c26ba9bSPaul Blakey 1119c26ba9bSPaul Blakey for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 1129c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 1139c26ba9bSPaul Blakey i * sizeof(u32) + offset, 1149c26ba9bSPaul Blakey 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 1159c26ba9bSPaul Blakey } 1169c26ba9bSPaul Blakey 1179c26ba9bSPaul Blakey static void 1189c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 1199c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1209c26ba9bSPaul Blakey struct flow_action *action) 1219c26ba9bSPaul Blakey { 1229c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 1239c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 1249c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 1259c26ba9bSPaul Blakey saddr)); 1269c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 1279c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 1289c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 1299c26ba9bSPaul Blakey daddr)); 1309c26ba9bSPaul Blakey } 1319c26ba9bSPaul Blakey 1329c26ba9bSPaul Blakey static void 1339c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 1349c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1359c26ba9bSPaul Blakey struct flow_action *action) 1369c26ba9bSPaul Blakey { 1379c26ba9bSPaul Blakey __be16 target_src = target.src.u.tcp.port; 1389c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.tcp.port; 1399c26ba9bSPaul Blakey 1409c26ba9bSPaul Blakey if (target_src != tuple->src.u.tcp.port) 1419c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 1429c26ba9bSPaul Blakey offsetof(struct tcphdr, source), 1439c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 1449c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.tcp.port) 1459c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 1469c26ba9bSPaul Blakey offsetof(struct tcphdr, dest), 1479c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 1489c26ba9bSPaul Blakey } 1499c26ba9bSPaul Blakey 1509c26ba9bSPaul Blakey static void 1519c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 1529c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1539c26ba9bSPaul Blakey struct flow_action *action) 1549c26ba9bSPaul Blakey { 1559c26ba9bSPaul Blakey __be16 target_src = target.src.u.udp.port; 1569c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.udp.port; 1579c26ba9bSPaul Blakey 1589c26ba9bSPaul Blakey if (target_src != tuple->src.u.udp.port) 15947b5d2a1SRoi Dayan tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 1609c26ba9bSPaul Blakey offsetof(struct udphdr, source), 1619c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 1629c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.udp.port) 16347b5d2a1SRoi Dayan tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 1649c26ba9bSPaul Blakey offsetof(struct udphdr, dest), 1659c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 1669c26ba9bSPaul Blakey } 1679c26ba9bSPaul Blakey 1689c26ba9bSPaul Blakey static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 1699c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 1709c26ba9bSPaul Blakey struct flow_action *action) 1719c26ba9bSPaul Blakey { 1729c26ba9bSPaul Blakey struct nf_conn_labels *ct_labels; 1739c26ba9bSPaul Blakey struct flow_action_entry *entry; 17430b0cf90SPaul Blakey enum ip_conntrack_info ctinfo; 1759c26ba9bSPaul Blakey u32 *act_ct_labels; 1769c26ba9bSPaul Blakey 1779c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 1789c26ba9bSPaul Blakey entry->id = FLOW_ACTION_CT_METADATA; 1799c26ba9bSPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 1809c26ba9bSPaul Blakey entry->ct_metadata.mark = ct->mark; 1819c26ba9bSPaul Blakey #endif 18230b0cf90SPaul Blakey ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 18330b0cf90SPaul Blakey IP_CT_ESTABLISHED_REPLY; 18430b0cf90SPaul Blakey /* aligns with the CT reference on the SKB nf_ct_set */ 18530b0cf90SPaul Blakey entry->ct_metadata.cookie = (unsigned long)ct | ctinfo; 186941eff5aSPaul Blakey entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL; 1879c26ba9bSPaul Blakey 1889c26ba9bSPaul Blakey act_ct_labels = entry->ct_metadata.labels; 1899c26ba9bSPaul Blakey ct_labels = nf_ct_labels_find(ct); 1909c26ba9bSPaul Blakey if (ct_labels) 1919c26ba9bSPaul Blakey memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 1929c26ba9bSPaul Blakey else 1939c26ba9bSPaul Blakey memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 1949c26ba9bSPaul Blakey } 1959c26ba9bSPaul Blakey 1969c26ba9bSPaul Blakey static int tcf_ct_flow_table_add_action_nat(struct net *net, 1979c26ba9bSPaul Blakey struct nf_conn *ct, 1989c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 1999c26ba9bSPaul Blakey struct flow_action *action) 2009c26ba9bSPaul Blakey { 2019c26ba9bSPaul Blakey const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 2029c26ba9bSPaul Blakey struct nf_conntrack_tuple target; 2039c26ba9bSPaul Blakey 20405aa69e5Swenxu if (!(ct->status & IPS_NAT_MASK)) 20505aa69e5Swenxu return 0; 20605aa69e5Swenxu 2079c26ba9bSPaul Blakey nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 2089c26ba9bSPaul Blakey 2099c26ba9bSPaul Blakey switch (tuple->src.l3num) { 2109c26ba9bSPaul Blakey case NFPROTO_IPV4: 2119c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 2129c26ba9bSPaul Blakey action); 2139c26ba9bSPaul Blakey break; 2149c26ba9bSPaul Blakey case NFPROTO_IPV6: 2159c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 2169c26ba9bSPaul Blakey action); 2179c26ba9bSPaul Blakey break; 2189c26ba9bSPaul Blakey default: 2199c26ba9bSPaul Blakey return -EOPNOTSUPP; 2209c26ba9bSPaul Blakey } 2219c26ba9bSPaul Blakey 2229c26ba9bSPaul Blakey switch (nf_ct_protonum(ct)) { 2239c26ba9bSPaul Blakey case IPPROTO_TCP: 2249c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 2259c26ba9bSPaul Blakey break; 2269c26ba9bSPaul Blakey case IPPROTO_UDP: 2279c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 2289c26ba9bSPaul Blakey break; 2299c26ba9bSPaul Blakey default: 2309c26ba9bSPaul Blakey return -EOPNOTSUPP; 2319c26ba9bSPaul Blakey } 2329c26ba9bSPaul Blakey 2339c26ba9bSPaul Blakey return 0; 2349c26ba9bSPaul Blakey } 2359c26ba9bSPaul Blakey 2369c26ba9bSPaul Blakey static int tcf_ct_flow_table_fill_actions(struct net *net, 2379c26ba9bSPaul Blakey const struct flow_offload *flow, 2389c26ba9bSPaul Blakey enum flow_offload_tuple_dir tdir, 2399c26ba9bSPaul Blakey struct nf_flow_rule *flow_rule) 2409c26ba9bSPaul Blakey { 2419c26ba9bSPaul Blakey struct flow_action *action = &flow_rule->rule->action; 2429c26ba9bSPaul Blakey int num_entries = action->num_entries; 2439c26ba9bSPaul Blakey struct nf_conn *ct = flow->ct; 2449c26ba9bSPaul Blakey enum ip_conntrack_dir dir; 2459c26ba9bSPaul Blakey int i, err; 2469c26ba9bSPaul Blakey 2479c26ba9bSPaul Blakey switch (tdir) { 2489c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_ORIGINAL: 2499c26ba9bSPaul Blakey dir = IP_CT_DIR_ORIGINAL; 2509c26ba9bSPaul Blakey break; 2519c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_REPLY: 2529c26ba9bSPaul Blakey dir = IP_CT_DIR_REPLY; 2539c26ba9bSPaul Blakey break; 2549c26ba9bSPaul Blakey default: 2559c26ba9bSPaul Blakey return -EOPNOTSUPP; 2569c26ba9bSPaul Blakey } 2579c26ba9bSPaul Blakey 2589c26ba9bSPaul Blakey err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 2599c26ba9bSPaul Blakey if (err) 2609c26ba9bSPaul Blakey goto err_nat; 2619c26ba9bSPaul Blakey 2629c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_meta(ct, dir, action); 2639c26ba9bSPaul Blakey return 0; 2649c26ba9bSPaul Blakey 2659c26ba9bSPaul Blakey err_nat: 2669c26ba9bSPaul Blakey /* Clear filled actions */ 2679c26ba9bSPaul Blakey for (i = num_entries; i < action->num_entries; i++) 2689c26ba9bSPaul Blakey memset(&action->entries[i], 0, sizeof(action->entries[i])); 2699c26ba9bSPaul Blakey action->num_entries = num_entries; 2709c26ba9bSPaul Blakey 2719c26ba9bSPaul Blakey return err; 2729c26ba9bSPaul Blakey } 2739c26ba9bSPaul Blakey 274c34b961aSPaul Blakey static struct nf_flowtable_type flowtable_ct = { 2759c26ba9bSPaul Blakey .action = tcf_ct_flow_table_fill_actions, 276c34b961aSPaul Blakey .owner = THIS_MODULE, 277c34b961aSPaul Blakey }; 278c34b961aSPaul Blakey 279c34b961aSPaul Blakey static int tcf_ct_flow_table_get(struct tcf_ct_params *params) 280c34b961aSPaul Blakey { 281c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 282c34b961aSPaul Blakey int err = -ENOMEM; 283c34b961aSPaul Blakey 284138470a9SEric Dumazet mutex_lock(&zones_mutex); 285c34b961aSPaul Blakey ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 286138470a9SEric Dumazet if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 287138470a9SEric Dumazet goto out_unlock; 288c34b961aSPaul Blakey 289138470a9SEric Dumazet ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 290c34b961aSPaul Blakey if (!ct_ft) 291c34b961aSPaul Blakey goto err_alloc; 292138470a9SEric Dumazet refcount_set(&ct_ft->ref, 1); 293c34b961aSPaul Blakey 294c34b961aSPaul Blakey ct_ft->zone = params->zone; 295c34b961aSPaul Blakey err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 296c34b961aSPaul Blakey if (err) 297c34b961aSPaul Blakey goto err_insert; 298c34b961aSPaul Blakey 299c34b961aSPaul Blakey ct_ft->nf_ft.type = &flowtable_ct; 3003567e233SMarcelo Ricardo Leitner ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 3013567e233SMarcelo Ricardo Leitner NF_FLOWTABLE_COUNTER; 302c34b961aSPaul Blakey err = nf_flow_table_init(&ct_ft->nf_ft); 303c34b961aSPaul Blakey if (err) 304c34b961aSPaul Blakey goto err_init; 305c34b961aSPaul Blakey 306c34b961aSPaul Blakey __module_get(THIS_MODULE); 307138470a9SEric Dumazet out_unlock: 308c34b961aSPaul Blakey params->ct_ft = ct_ft; 309edd5861eSPaul Blakey params->nf_ft = &ct_ft->nf_ft; 310138470a9SEric Dumazet mutex_unlock(&zones_mutex); 311c34b961aSPaul Blakey 312c34b961aSPaul Blakey return 0; 313c34b961aSPaul Blakey 314c34b961aSPaul Blakey err_init: 315c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 316c34b961aSPaul Blakey err_insert: 317c34b961aSPaul Blakey kfree(ct_ft); 318c34b961aSPaul Blakey err_alloc: 319138470a9SEric Dumazet mutex_unlock(&zones_mutex); 320c34b961aSPaul Blakey return err; 321c34b961aSPaul Blakey } 322c34b961aSPaul Blakey 323c34b961aSPaul Blakey static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 324c34b961aSPaul Blakey { 325c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 326c34b961aSPaul Blakey 327c34b961aSPaul Blakey ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 328c34b961aSPaul Blakey rwork); 329c34b961aSPaul Blakey nf_flow_table_free(&ct_ft->nf_ft); 330c34b961aSPaul Blakey kfree(ct_ft); 331c34b961aSPaul Blakey 332c34b961aSPaul Blakey module_put(THIS_MODULE); 333c34b961aSPaul Blakey } 334c34b961aSPaul Blakey 335c34b961aSPaul Blakey static void tcf_ct_flow_table_put(struct tcf_ct_params *params) 336c34b961aSPaul Blakey { 337c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft = params->ct_ft; 338c34b961aSPaul Blakey 339138470a9SEric Dumazet if (refcount_dec_and_test(¶ms->ct_ft->ref)) { 340c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 341c34b961aSPaul Blakey INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 342c34b961aSPaul Blakey queue_rcu_work(act_ct_wq, &ct_ft->rwork); 343c34b961aSPaul Blakey } 344c34b961aSPaul Blakey } 345c34b961aSPaul Blakey 34664ff70b8SPaul Blakey static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 34764ff70b8SPaul Blakey struct nf_conn *ct, 34864ff70b8SPaul Blakey bool tcp) 34964ff70b8SPaul Blakey { 35064ff70b8SPaul Blakey struct flow_offload *entry; 35164ff70b8SPaul Blakey int err; 35264ff70b8SPaul Blakey 35364ff70b8SPaul Blakey if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 35464ff70b8SPaul Blakey return; 35564ff70b8SPaul Blakey 35664ff70b8SPaul Blakey entry = flow_offload_alloc(ct); 35764ff70b8SPaul Blakey if (!entry) { 35864ff70b8SPaul Blakey WARN_ON_ONCE(1); 35964ff70b8SPaul Blakey goto err_alloc; 36064ff70b8SPaul Blakey } 36164ff70b8SPaul Blakey 36264ff70b8SPaul Blakey if (tcp) { 36364ff70b8SPaul Blakey ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 36464ff70b8SPaul Blakey ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 36564ff70b8SPaul Blakey } 36664ff70b8SPaul Blakey 36764ff70b8SPaul Blakey err = flow_offload_add(&ct_ft->nf_ft, entry); 36864ff70b8SPaul Blakey if (err) 36964ff70b8SPaul Blakey goto err_add; 37064ff70b8SPaul Blakey 37164ff70b8SPaul Blakey return; 37264ff70b8SPaul Blakey 37364ff70b8SPaul Blakey err_add: 37464ff70b8SPaul Blakey flow_offload_free(entry); 37564ff70b8SPaul Blakey err_alloc: 37664ff70b8SPaul Blakey clear_bit(IPS_OFFLOAD_BIT, &ct->status); 37764ff70b8SPaul Blakey } 37864ff70b8SPaul Blakey 37964ff70b8SPaul Blakey static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 38064ff70b8SPaul Blakey struct nf_conn *ct, 38164ff70b8SPaul Blakey enum ip_conntrack_info ctinfo) 38264ff70b8SPaul Blakey { 38364ff70b8SPaul Blakey bool tcp = false; 38464ff70b8SPaul Blakey 38564ff70b8SPaul Blakey if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) 38664ff70b8SPaul Blakey return; 38764ff70b8SPaul Blakey 38864ff70b8SPaul Blakey switch (nf_ct_protonum(ct)) { 38964ff70b8SPaul Blakey case IPPROTO_TCP: 39064ff70b8SPaul Blakey tcp = true; 39164ff70b8SPaul Blakey if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 39264ff70b8SPaul Blakey return; 39364ff70b8SPaul Blakey break; 39464ff70b8SPaul Blakey case IPPROTO_UDP: 39564ff70b8SPaul Blakey break; 39664ff70b8SPaul Blakey default: 39764ff70b8SPaul Blakey return; 39864ff70b8SPaul Blakey } 39964ff70b8SPaul Blakey 40064ff70b8SPaul Blakey if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 40164ff70b8SPaul Blakey ct->status & IPS_SEQ_ADJUST) 40264ff70b8SPaul Blakey return; 40364ff70b8SPaul Blakey 40464ff70b8SPaul Blakey tcf_ct_flow_table_add(ct_ft, ct, tcp); 40564ff70b8SPaul Blakey } 40664ff70b8SPaul Blakey 40746475bb2SPaul Blakey static bool 40846475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 40907ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 41007ac9d16SPaul Blakey struct tcphdr **tcph) 41146475bb2SPaul Blakey { 41246475bb2SPaul Blakey struct flow_ports *ports; 41346475bb2SPaul Blakey unsigned int thoff; 41446475bb2SPaul Blakey struct iphdr *iph; 41546475bb2SPaul Blakey 4164cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*iph))) 41746475bb2SPaul Blakey return false; 41846475bb2SPaul Blakey 41946475bb2SPaul Blakey iph = ip_hdr(skb); 42046475bb2SPaul Blakey thoff = iph->ihl * 4; 42146475bb2SPaul Blakey 42246475bb2SPaul Blakey if (ip_is_fragment(iph) || 42346475bb2SPaul Blakey unlikely(thoff != sizeof(struct iphdr))) 42446475bb2SPaul Blakey return false; 42546475bb2SPaul Blakey 42646475bb2SPaul Blakey if (iph->protocol != IPPROTO_TCP && 42746475bb2SPaul Blakey iph->protocol != IPPROTO_UDP) 42846475bb2SPaul Blakey return false; 42946475bb2SPaul Blakey 43046475bb2SPaul Blakey if (iph->ttl <= 1) 43146475bb2SPaul Blakey return false; 43246475bb2SPaul Blakey 4334cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ? 43407ac9d16SPaul Blakey thoff + sizeof(struct tcphdr) : 43507ac9d16SPaul Blakey thoff + sizeof(*ports))) 43646475bb2SPaul Blakey return false; 43746475bb2SPaul Blakey 43807ac9d16SPaul Blakey iph = ip_hdr(skb); 43907ac9d16SPaul Blakey if (iph->protocol == IPPROTO_TCP) 44007ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 44146475bb2SPaul Blakey 44207ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 44346475bb2SPaul Blakey tuple->src_v4.s_addr = iph->saddr; 44446475bb2SPaul Blakey tuple->dst_v4.s_addr = iph->daddr; 44546475bb2SPaul Blakey tuple->src_port = ports->source; 44646475bb2SPaul Blakey tuple->dst_port = ports->dest; 44746475bb2SPaul Blakey tuple->l3proto = AF_INET; 44846475bb2SPaul Blakey tuple->l4proto = iph->protocol; 44946475bb2SPaul Blakey 45046475bb2SPaul Blakey return true; 45146475bb2SPaul Blakey } 45246475bb2SPaul Blakey 45346475bb2SPaul Blakey static bool 45446475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 45507ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 45607ac9d16SPaul Blakey struct tcphdr **tcph) 45746475bb2SPaul Blakey { 45846475bb2SPaul Blakey struct flow_ports *ports; 45946475bb2SPaul Blakey struct ipv6hdr *ip6h; 46046475bb2SPaul Blakey unsigned int thoff; 46146475bb2SPaul Blakey 4624cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 46346475bb2SPaul Blakey return false; 46446475bb2SPaul Blakey 46546475bb2SPaul Blakey ip6h = ipv6_hdr(skb); 46646475bb2SPaul Blakey 46746475bb2SPaul Blakey if (ip6h->nexthdr != IPPROTO_TCP && 46846475bb2SPaul Blakey ip6h->nexthdr != IPPROTO_UDP) 46946475bb2SPaul Blakey return false; 47046475bb2SPaul Blakey 47146475bb2SPaul Blakey if (ip6h->hop_limit <= 1) 47246475bb2SPaul Blakey return false; 47346475bb2SPaul Blakey 47446475bb2SPaul Blakey thoff = sizeof(*ip6h); 4754cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ? 47607ac9d16SPaul Blakey thoff + sizeof(struct tcphdr) : 47707ac9d16SPaul Blakey thoff + sizeof(*ports))) 47846475bb2SPaul Blakey return false; 47946475bb2SPaul Blakey 48007ac9d16SPaul Blakey ip6h = ipv6_hdr(skb); 48107ac9d16SPaul Blakey if (ip6h->nexthdr == IPPROTO_TCP) 48207ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 48346475bb2SPaul Blakey 48407ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 48546475bb2SPaul Blakey tuple->src_v6 = ip6h->saddr; 48646475bb2SPaul Blakey tuple->dst_v6 = ip6h->daddr; 48746475bb2SPaul Blakey tuple->src_port = ports->source; 48846475bb2SPaul Blakey tuple->dst_port = ports->dest; 48946475bb2SPaul Blakey tuple->l3proto = AF_INET6; 49046475bb2SPaul Blakey tuple->l4proto = ip6h->nexthdr; 49146475bb2SPaul Blakey 49246475bb2SPaul Blakey return true; 49346475bb2SPaul Blakey } 49446475bb2SPaul Blakey 49546475bb2SPaul Blakey static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 49646475bb2SPaul Blakey struct sk_buff *skb, 49746475bb2SPaul Blakey u8 family) 49846475bb2SPaul Blakey { 49946475bb2SPaul Blakey struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 50046475bb2SPaul Blakey struct flow_offload_tuple_rhash *tuplehash; 50146475bb2SPaul Blakey struct flow_offload_tuple tuple = {}; 50246475bb2SPaul Blakey enum ip_conntrack_info ctinfo; 50307ac9d16SPaul Blakey struct tcphdr *tcph = NULL; 50446475bb2SPaul Blakey struct flow_offload *flow; 50546475bb2SPaul Blakey struct nf_conn *ct; 50646475bb2SPaul Blakey u8 dir; 50746475bb2SPaul Blakey 50846475bb2SPaul Blakey /* Previously seen or loopback */ 50946475bb2SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 51046475bb2SPaul Blakey if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 51146475bb2SPaul Blakey return false; 51246475bb2SPaul Blakey 51346475bb2SPaul Blakey switch (family) { 51446475bb2SPaul Blakey case NFPROTO_IPV4: 51507ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 51646475bb2SPaul Blakey return false; 51746475bb2SPaul Blakey break; 51846475bb2SPaul Blakey case NFPROTO_IPV6: 51907ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 52046475bb2SPaul Blakey return false; 52146475bb2SPaul Blakey break; 52246475bb2SPaul Blakey default: 52346475bb2SPaul Blakey return false; 52446475bb2SPaul Blakey } 52546475bb2SPaul Blakey 52646475bb2SPaul Blakey tuplehash = flow_offload_lookup(nf_ft, &tuple); 52746475bb2SPaul Blakey if (!tuplehash) 52846475bb2SPaul Blakey return false; 52946475bb2SPaul Blakey 53046475bb2SPaul Blakey dir = tuplehash->tuple.dir; 53146475bb2SPaul Blakey flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 53246475bb2SPaul Blakey ct = flow->ct; 53346475bb2SPaul Blakey 53407ac9d16SPaul Blakey if (tcph && (unlikely(tcph->fin || tcph->rst))) { 53507ac9d16SPaul Blakey flow_offload_teardown(flow); 53607ac9d16SPaul Blakey return false; 53707ac9d16SPaul Blakey } 53807ac9d16SPaul Blakey 53946475bb2SPaul Blakey ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 54046475bb2SPaul Blakey IP_CT_ESTABLISHED_REPLY; 54146475bb2SPaul Blakey 5428b3646d6SPaul Blakey flow_offload_refresh(nf_ft, flow); 54346475bb2SPaul Blakey nf_conntrack_get(&ct->ct_general); 54446475bb2SPaul Blakey nf_ct_set(skb, ct, ctinfo); 5453567e233SMarcelo Ricardo Leitner if (nf_ft->flags & NF_FLOWTABLE_COUNTER) 546beb97d3aSwenxu nf_ct_acct_update(ct, dir, skb->len); 54746475bb2SPaul Blakey 54846475bb2SPaul Blakey return true; 54946475bb2SPaul Blakey } 55046475bb2SPaul Blakey 551c34b961aSPaul Blakey static int tcf_ct_flow_tables_init(void) 552c34b961aSPaul Blakey { 553c34b961aSPaul Blakey return rhashtable_init(&zones_ht, &zones_params); 554c34b961aSPaul Blakey } 555c34b961aSPaul Blakey 556c34b961aSPaul Blakey static void tcf_ct_flow_tables_uninit(void) 557c34b961aSPaul Blakey { 558c34b961aSPaul Blakey rhashtable_destroy(&zones_ht); 559c34b961aSPaul Blakey } 560c34b961aSPaul Blakey 561b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops; 562b57dc7c1SPaul Blakey static unsigned int ct_net_id; 563b57dc7c1SPaul Blakey 564b57dc7c1SPaul Blakey struct tc_ct_action_net { 565b57dc7c1SPaul Blakey struct tc_action_net tn; /* Must be first */ 566b57dc7c1SPaul Blakey bool labels; 567b57dc7c1SPaul Blakey }; 568b57dc7c1SPaul Blakey 569b57dc7c1SPaul Blakey /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 570b57dc7c1SPaul Blakey static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 571b57dc7c1SPaul Blakey u16 zone_id, bool force) 572b57dc7c1SPaul Blakey { 573b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 574b57dc7c1SPaul Blakey struct nf_conn *ct; 575b57dc7c1SPaul Blakey 576b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 577b57dc7c1SPaul Blakey if (!ct) 578b57dc7c1SPaul Blakey return false; 579b57dc7c1SPaul Blakey if (!net_eq(net, read_pnet(&ct->ct_net))) 580b57dc7c1SPaul Blakey return false; 581b57dc7c1SPaul Blakey if (nf_ct_zone(ct)->id != zone_id) 582b57dc7c1SPaul Blakey return false; 583b57dc7c1SPaul Blakey 584b57dc7c1SPaul Blakey /* Force conntrack entry direction. */ 585b57dc7c1SPaul Blakey if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 586b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 587b57dc7c1SPaul Blakey nf_ct_kill(ct); 588b57dc7c1SPaul Blakey 589b57dc7c1SPaul Blakey nf_conntrack_put(&ct->ct_general); 590b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 591b57dc7c1SPaul Blakey 592b57dc7c1SPaul Blakey return false; 593b57dc7c1SPaul Blakey } 594b57dc7c1SPaul Blakey 595b57dc7c1SPaul Blakey return true; 596b57dc7c1SPaul Blakey } 597b57dc7c1SPaul Blakey 598b57dc7c1SPaul Blakey /* Trim the skb to the length specified by the IP/IPv6 header, 599b57dc7c1SPaul Blakey * removing any trailing lower-layer padding. This prepares the skb 600b57dc7c1SPaul Blakey * for higher-layer processing that assumes skb->len excludes padding 601b57dc7c1SPaul Blakey * (such as nf_ip_checksum). The caller needs to pull the skb to the 602b57dc7c1SPaul Blakey * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 603b57dc7c1SPaul Blakey */ 604b57dc7c1SPaul Blakey static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family) 605b57dc7c1SPaul Blakey { 606b57dc7c1SPaul Blakey unsigned int len; 607b57dc7c1SPaul Blakey int err; 608b57dc7c1SPaul Blakey 609b57dc7c1SPaul Blakey switch (family) { 610b57dc7c1SPaul Blakey case NFPROTO_IPV4: 611b57dc7c1SPaul Blakey len = ntohs(ip_hdr(skb)->tot_len); 612b57dc7c1SPaul Blakey break; 613b57dc7c1SPaul Blakey case NFPROTO_IPV6: 614b57dc7c1SPaul Blakey len = sizeof(struct ipv6hdr) 615b57dc7c1SPaul Blakey + ntohs(ipv6_hdr(skb)->payload_len); 616b57dc7c1SPaul Blakey break; 617b57dc7c1SPaul Blakey default: 618b57dc7c1SPaul Blakey len = skb->len; 619b57dc7c1SPaul Blakey } 620b57dc7c1SPaul Blakey 621b57dc7c1SPaul Blakey err = pskb_trim_rcsum(skb, len); 622b57dc7c1SPaul Blakey 623b57dc7c1SPaul Blakey return err; 624b57dc7c1SPaul Blakey } 625b57dc7c1SPaul Blakey 626b57dc7c1SPaul Blakey static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 627b57dc7c1SPaul Blakey { 628b57dc7c1SPaul Blakey u8 family = NFPROTO_UNSPEC; 629b57dc7c1SPaul Blakey 630d7bf2ebeSToke Høiland-Jørgensen switch (skb_protocol(skb, true)) { 631b57dc7c1SPaul Blakey case htons(ETH_P_IP): 632b57dc7c1SPaul Blakey family = NFPROTO_IPV4; 633b57dc7c1SPaul Blakey break; 634b57dc7c1SPaul Blakey case htons(ETH_P_IPV6): 635b57dc7c1SPaul Blakey family = NFPROTO_IPV6; 636b57dc7c1SPaul Blakey break; 637b57dc7c1SPaul Blakey default: 638b57dc7c1SPaul Blakey break; 639b57dc7c1SPaul Blakey } 640b57dc7c1SPaul Blakey 641b57dc7c1SPaul Blakey return family; 642b57dc7c1SPaul Blakey } 643b57dc7c1SPaul Blakey 644b57dc7c1SPaul Blakey static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 645b57dc7c1SPaul Blakey { 646b57dc7c1SPaul Blakey unsigned int len; 647b57dc7c1SPaul Blakey 648b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct iphdr); 649b57dc7c1SPaul Blakey if (unlikely(skb->len < len)) 650b57dc7c1SPaul Blakey return -EINVAL; 651b57dc7c1SPaul Blakey if (unlikely(!pskb_may_pull(skb, len))) 652b57dc7c1SPaul Blakey return -ENOMEM; 653b57dc7c1SPaul Blakey 654b57dc7c1SPaul Blakey *frag = ip_is_fragment(ip_hdr(skb)); 655b57dc7c1SPaul Blakey return 0; 656b57dc7c1SPaul Blakey } 657b57dc7c1SPaul Blakey 658b57dc7c1SPaul Blakey static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 659b57dc7c1SPaul Blakey { 660b57dc7c1SPaul Blakey unsigned int flags = 0, len, payload_ofs = 0; 661b57dc7c1SPaul Blakey unsigned short frag_off; 662b57dc7c1SPaul Blakey int nexthdr; 663b57dc7c1SPaul Blakey 664b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 665b57dc7c1SPaul Blakey if (unlikely(skb->len < len)) 666b57dc7c1SPaul Blakey return -EINVAL; 667b57dc7c1SPaul Blakey if (unlikely(!pskb_may_pull(skb, len))) 668b57dc7c1SPaul Blakey return -ENOMEM; 669b57dc7c1SPaul Blakey 670b57dc7c1SPaul Blakey nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 671b57dc7c1SPaul Blakey if (unlikely(nexthdr < 0)) 672b57dc7c1SPaul Blakey return -EPROTO; 673b57dc7c1SPaul Blakey 674b57dc7c1SPaul Blakey *frag = flags & IP6_FH_F_FRAG; 675b57dc7c1SPaul Blakey return 0; 676b57dc7c1SPaul Blakey } 677b57dc7c1SPaul Blakey 678b57dc7c1SPaul Blakey static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 679ae372cb1Swenxu u8 family, u16 zone, bool *defrag) 680b57dc7c1SPaul Blakey { 681b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 682ae372cb1Swenxu struct qdisc_skb_cb cb; 683b57dc7c1SPaul Blakey struct nf_conn *ct; 684b57dc7c1SPaul Blakey int err = 0; 685b57dc7c1SPaul Blakey bool frag; 686b57dc7c1SPaul Blakey 687b57dc7c1SPaul Blakey /* Previously seen (loopback)? Ignore. */ 688b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 689b57dc7c1SPaul Blakey if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 690b57dc7c1SPaul Blakey return 0; 691b57dc7c1SPaul Blakey 692b57dc7c1SPaul Blakey if (family == NFPROTO_IPV4) 693b57dc7c1SPaul Blakey err = tcf_ct_ipv4_is_fragment(skb, &frag); 694b57dc7c1SPaul Blakey else 695b57dc7c1SPaul Blakey err = tcf_ct_ipv6_is_fragment(skb, &frag); 696b57dc7c1SPaul Blakey if (err || !frag) 697b57dc7c1SPaul Blakey return err; 698b57dc7c1SPaul Blakey 699b57dc7c1SPaul Blakey skb_get(skb); 700ae372cb1Swenxu cb = *qdisc_skb_cb(skb); 701b57dc7c1SPaul Blakey 702b57dc7c1SPaul Blakey if (family == NFPROTO_IPV4) { 703b57dc7c1SPaul Blakey enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 704b57dc7c1SPaul Blakey 705b57dc7c1SPaul Blakey memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 706b57dc7c1SPaul Blakey local_bh_disable(); 707b57dc7c1SPaul Blakey err = ip_defrag(net, skb, user); 708b57dc7c1SPaul Blakey local_bh_enable(); 709b57dc7c1SPaul Blakey if (err && err != -EINPROGRESS) 710eda814b9SAlaa Hleihel return err; 711ae372cb1Swenxu 712038ebb1aSwenxu if (!err) { 713ae372cb1Swenxu *defrag = true; 714038ebb1aSwenxu cb.mru = IPCB(skb)->frag_max_size; 715038ebb1aSwenxu } 716b57dc7c1SPaul Blakey } else { /* NFPROTO_IPV6 */ 717b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 718b57dc7c1SPaul Blakey enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 719b57dc7c1SPaul Blakey 720b57dc7c1SPaul Blakey memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 721b57dc7c1SPaul Blakey err = nf_ct_frag6_gather(net, skb, user); 722b57dc7c1SPaul Blakey if (err && err != -EINPROGRESS) 723b57dc7c1SPaul Blakey goto out_free; 724ae372cb1Swenxu 725038ebb1aSwenxu if (!err) { 726ae372cb1Swenxu *defrag = true; 727038ebb1aSwenxu cb.mru = IP6CB(skb)->frag_max_size; 728038ebb1aSwenxu } 729b57dc7c1SPaul Blakey #else 730b57dc7c1SPaul Blakey err = -EOPNOTSUPP; 731b57dc7c1SPaul Blakey goto out_free; 732b57dc7c1SPaul Blakey #endif 733b57dc7c1SPaul Blakey } 734b57dc7c1SPaul Blakey 735ae372cb1Swenxu *qdisc_skb_cb(skb) = cb; 736b57dc7c1SPaul Blakey skb_clear_hash(skb); 737b57dc7c1SPaul Blakey skb->ignore_df = 1; 738b57dc7c1SPaul Blakey return err; 739b57dc7c1SPaul Blakey 740b57dc7c1SPaul Blakey out_free: 741b57dc7c1SPaul Blakey kfree_skb(skb); 742b57dc7c1SPaul Blakey return err; 743b57dc7c1SPaul Blakey } 744b57dc7c1SPaul Blakey 745b57dc7c1SPaul Blakey static void tcf_ct_params_free(struct rcu_head *head) 746b57dc7c1SPaul Blakey { 747b57dc7c1SPaul Blakey struct tcf_ct_params *params = container_of(head, 748b57dc7c1SPaul Blakey struct tcf_ct_params, rcu); 749b57dc7c1SPaul Blakey 750c34b961aSPaul Blakey tcf_ct_flow_table_put(params); 751c34b961aSPaul Blakey 752b57dc7c1SPaul Blakey if (params->tmpl) 753b57dc7c1SPaul Blakey nf_conntrack_put(¶ms->tmpl->ct_general); 754b57dc7c1SPaul Blakey kfree(params); 755b57dc7c1SPaul Blakey } 756b57dc7c1SPaul Blakey 757b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT) 758b57dc7c1SPaul Blakey /* Modelled after nf_nat_ipv[46]_fn(). 759b57dc7c1SPaul Blakey * range is only used for new, uninitialized NAT state. 760b57dc7c1SPaul Blakey * Returns either NF_ACCEPT or NF_DROP. 761b57dc7c1SPaul Blakey */ 762b57dc7c1SPaul Blakey static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 763b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo, 764b57dc7c1SPaul Blakey const struct nf_nat_range2 *range, 765b57dc7c1SPaul Blakey enum nf_nat_manip_type maniptype) 766b57dc7c1SPaul Blakey { 767d7bf2ebeSToke Høiland-Jørgensen __be16 proto = skb_protocol(skb, true); 768b57dc7c1SPaul Blakey int hooknum, err = NF_ACCEPT; 769b57dc7c1SPaul Blakey 770b57dc7c1SPaul Blakey /* See HOOK2MANIP(). */ 771b57dc7c1SPaul Blakey if (maniptype == NF_NAT_MANIP_SRC) 772b57dc7c1SPaul Blakey hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 773b57dc7c1SPaul Blakey else 774b57dc7c1SPaul Blakey hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 775b57dc7c1SPaul Blakey 776b57dc7c1SPaul Blakey switch (ctinfo) { 777b57dc7c1SPaul Blakey case IP_CT_RELATED: 778b57dc7c1SPaul Blakey case IP_CT_RELATED_REPLY: 779d7bf2ebeSToke Høiland-Jørgensen if (proto == htons(ETH_P_IP) && 780b57dc7c1SPaul Blakey ip_hdr(skb)->protocol == IPPROTO_ICMP) { 781b57dc7c1SPaul Blakey if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 782b57dc7c1SPaul Blakey hooknum)) 783b57dc7c1SPaul Blakey err = NF_DROP; 784b57dc7c1SPaul Blakey goto out; 785d7bf2ebeSToke Høiland-Jørgensen } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) { 786b57dc7c1SPaul Blakey __be16 frag_off; 787b57dc7c1SPaul Blakey u8 nexthdr = ipv6_hdr(skb)->nexthdr; 788b57dc7c1SPaul Blakey int hdrlen = ipv6_skip_exthdr(skb, 789b57dc7c1SPaul Blakey sizeof(struct ipv6hdr), 790b57dc7c1SPaul Blakey &nexthdr, &frag_off); 791b57dc7c1SPaul Blakey 792b57dc7c1SPaul Blakey if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 793b57dc7c1SPaul Blakey if (!nf_nat_icmpv6_reply_translation(skb, ct, 794b57dc7c1SPaul Blakey ctinfo, 795b57dc7c1SPaul Blakey hooknum, 796b57dc7c1SPaul Blakey hdrlen)) 797b57dc7c1SPaul Blakey err = NF_DROP; 798b57dc7c1SPaul Blakey goto out; 799b57dc7c1SPaul Blakey } 800b57dc7c1SPaul Blakey } 801b57dc7c1SPaul Blakey /* Non-ICMP, fall thru to initialize if needed. */ 802964201deSGustavo A. R. Silva fallthrough; 803b57dc7c1SPaul Blakey case IP_CT_NEW: 804b57dc7c1SPaul Blakey /* Seen it before? This can happen for loopback, retrans, 805b57dc7c1SPaul Blakey * or local packets. 806b57dc7c1SPaul Blakey */ 807b57dc7c1SPaul Blakey if (!nf_nat_initialized(ct, maniptype)) { 808b57dc7c1SPaul Blakey /* Initialize according to the NAT action. */ 809b57dc7c1SPaul Blakey err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 810b57dc7c1SPaul Blakey /* Action is set up to establish a new 811b57dc7c1SPaul Blakey * mapping. 812b57dc7c1SPaul Blakey */ 813b57dc7c1SPaul Blakey ? nf_nat_setup_info(ct, range, maniptype) 814b57dc7c1SPaul Blakey : nf_nat_alloc_null_binding(ct, hooknum); 815b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 816b57dc7c1SPaul Blakey goto out; 817b57dc7c1SPaul Blakey } 818b57dc7c1SPaul Blakey break; 819b57dc7c1SPaul Blakey 820b57dc7c1SPaul Blakey case IP_CT_ESTABLISHED: 821b57dc7c1SPaul Blakey case IP_CT_ESTABLISHED_REPLY: 822b57dc7c1SPaul Blakey break; 823b57dc7c1SPaul Blakey 824b57dc7c1SPaul Blakey default: 825b57dc7c1SPaul Blakey err = NF_DROP; 826b57dc7c1SPaul Blakey goto out; 827b57dc7c1SPaul Blakey } 828b57dc7c1SPaul Blakey 829b57dc7c1SPaul Blakey err = nf_nat_packet(ct, ctinfo, hooknum, skb); 830b57dc7c1SPaul Blakey out: 831b57dc7c1SPaul Blakey return err; 832b57dc7c1SPaul Blakey } 833b57dc7c1SPaul Blakey #endif /* CONFIG_NF_NAT */ 834b57dc7c1SPaul Blakey 835b57dc7c1SPaul Blakey static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 836b57dc7c1SPaul Blakey { 837b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 838b57dc7c1SPaul Blakey u32 new_mark; 839b57dc7c1SPaul Blakey 840b57dc7c1SPaul Blakey if (!mask) 841b57dc7c1SPaul Blakey return; 842b57dc7c1SPaul Blakey 843b57dc7c1SPaul Blakey new_mark = mark | (ct->mark & ~(mask)); 844b57dc7c1SPaul Blakey if (ct->mark != new_mark) { 845b57dc7c1SPaul Blakey ct->mark = new_mark; 846b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 847b57dc7c1SPaul Blakey nf_conntrack_event_cache(IPCT_MARK, ct); 848b57dc7c1SPaul Blakey } 849b57dc7c1SPaul Blakey #endif 850b57dc7c1SPaul Blakey } 851b57dc7c1SPaul Blakey 852b57dc7c1SPaul Blakey static void tcf_ct_act_set_labels(struct nf_conn *ct, 853b57dc7c1SPaul Blakey u32 *labels, 854b57dc7c1SPaul Blakey u32 *labels_m) 855b57dc7c1SPaul Blakey { 856b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 857c593642cSPankaj Bharadiya size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 858b57dc7c1SPaul Blakey 859b57dc7c1SPaul Blakey if (!memchr_inv(labels_m, 0, labels_sz)) 860b57dc7c1SPaul Blakey return; 861b57dc7c1SPaul Blakey 862b57dc7c1SPaul Blakey nf_connlabels_replace(ct, labels, labels_m, 4); 863b57dc7c1SPaul Blakey #endif 864b57dc7c1SPaul Blakey } 865b57dc7c1SPaul Blakey 866b57dc7c1SPaul Blakey static int tcf_ct_act_nat(struct sk_buff *skb, 867b57dc7c1SPaul Blakey struct nf_conn *ct, 868b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo, 869b57dc7c1SPaul Blakey int ct_action, 870b57dc7c1SPaul Blakey struct nf_nat_range2 *range, 871b57dc7c1SPaul Blakey bool commit) 872b57dc7c1SPaul Blakey { 873b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT) 87495219afbSAaron Conole int err; 875b57dc7c1SPaul Blakey enum nf_nat_manip_type maniptype; 876b57dc7c1SPaul Blakey 877b57dc7c1SPaul Blakey if (!(ct_action & TCA_CT_ACT_NAT)) 878b57dc7c1SPaul Blakey return NF_ACCEPT; 879b57dc7c1SPaul Blakey 880b57dc7c1SPaul Blakey /* Add NAT extension if not confirmed yet. */ 881b57dc7c1SPaul Blakey if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 882b57dc7c1SPaul Blakey return NF_DROP; /* Can't NAT. */ 883b57dc7c1SPaul Blakey 884b57dc7c1SPaul Blakey if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) && 885b57dc7c1SPaul Blakey (ctinfo != IP_CT_RELATED || commit)) { 886b57dc7c1SPaul Blakey /* NAT an established or related connection like before. */ 887b57dc7c1SPaul Blakey if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 888b57dc7c1SPaul Blakey /* This is the REPLY direction for a connection 889b57dc7c1SPaul Blakey * for which NAT was applied in the forward 890b57dc7c1SPaul Blakey * direction. Do the reverse NAT. 891b57dc7c1SPaul Blakey */ 892b57dc7c1SPaul Blakey maniptype = ct->status & IPS_SRC_NAT 893b57dc7c1SPaul Blakey ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 894b57dc7c1SPaul Blakey else 895b57dc7c1SPaul Blakey maniptype = ct->status & IPS_SRC_NAT 896b57dc7c1SPaul Blakey ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 897b57dc7c1SPaul Blakey } else if (ct_action & TCA_CT_ACT_NAT_SRC) { 898b57dc7c1SPaul Blakey maniptype = NF_NAT_MANIP_SRC; 899b57dc7c1SPaul Blakey } else if (ct_action & TCA_CT_ACT_NAT_DST) { 900b57dc7c1SPaul Blakey maniptype = NF_NAT_MANIP_DST; 901b57dc7c1SPaul Blakey } else { 902b57dc7c1SPaul Blakey return NF_ACCEPT; 903b57dc7c1SPaul Blakey } 904b57dc7c1SPaul Blakey 90595219afbSAaron Conole err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 90695219afbSAaron Conole if (err == NF_ACCEPT && 90795219afbSAaron Conole ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) { 90895219afbSAaron Conole if (maniptype == NF_NAT_MANIP_SRC) 90995219afbSAaron Conole maniptype = NF_NAT_MANIP_DST; 91095219afbSAaron Conole else 91195219afbSAaron Conole maniptype = NF_NAT_MANIP_SRC; 91295219afbSAaron Conole 91395219afbSAaron Conole err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 91495219afbSAaron Conole } 91595219afbSAaron Conole return err; 916b57dc7c1SPaul Blakey #else 917b57dc7c1SPaul Blakey return NF_ACCEPT; 918b57dc7c1SPaul Blakey #endif 919b57dc7c1SPaul Blakey } 920b57dc7c1SPaul Blakey 921b57dc7c1SPaul Blakey static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 922b57dc7c1SPaul Blakey struct tcf_result *res) 923b57dc7c1SPaul Blakey { 924b57dc7c1SPaul Blakey struct net *net = dev_net(skb->dev); 925b57dc7c1SPaul Blakey bool cached, commit, clear, force; 926b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 927b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 928b57dc7c1SPaul Blakey struct nf_conn *tmpl = NULL; 929b57dc7c1SPaul Blakey struct nf_hook_state state; 930b57dc7c1SPaul Blakey int nh_ofs, err, retval; 931b57dc7c1SPaul Blakey struct tcf_ct_params *p; 93246475bb2SPaul Blakey bool skip_add = false; 933ae372cb1Swenxu bool defrag = false; 934b57dc7c1SPaul Blakey struct nf_conn *ct; 935b57dc7c1SPaul Blakey u8 family; 936b57dc7c1SPaul Blakey 937b57dc7c1SPaul Blakey p = rcu_dereference_bh(c->params); 938b57dc7c1SPaul Blakey 939b57dc7c1SPaul Blakey retval = READ_ONCE(c->tcf_action); 940b57dc7c1SPaul Blakey commit = p->ct_action & TCA_CT_ACT_COMMIT; 941b57dc7c1SPaul Blakey clear = p->ct_action & TCA_CT_ACT_CLEAR; 942b57dc7c1SPaul Blakey force = p->ct_action & TCA_CT_ACT_FORCE; 943b57dc7c1SPaul Blakey tmpl = p->tmpl; 944b57dc7c1SPaul Blakey 9458367b3abSwenxu tcf_lastuse_update(&c->tcf_tm); 9468367b3abSwenxu 947b57dc7c1SPaul Blakey if (clear) { 948*8ca1b090SMarcelo Ricardo Leitner qdisc_skb_cb(skb)->post_ct = false; 949b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 950b57dc7c1SPaul Blakey if (ct) { 951b57dc7c1SPaul Blakey nf_conntrack_put(&ct->ct_general); 952b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 953b57dc7c1SPaul Blakey } 954b57dc7c1SPaul Blakey 955*8ca1b090SMarcelo Ricardo Leitner goto out_clear; 956b57dc7c1SPaul Blakey } 957b57dc7c1SPaul Blakey 958b57dc7c1SPaul Blakey family = tcf_ct_skb_nf_family(skb); 959b57dc7c1SPaul Blakey if (family == NFPROTO_UNSPEC) 960b57dc7c1SPaul Blakey goto drop; 961b57dc7c1SPaul Blakey 962b57dc7c1SPaul Blakey /* The conntrack module expects to be working at L3. 963b57dc7c1SPaul Blakey * We also try to pull the IPv4/6 header to linear area 964b57dc7c1SPaul Blakey */ 965b57dc7c1SPaul Blakey nh_ofs = skb_network_offset(skb); 966b57dc7c1SPaul Blakey skb_pull_rcsum(skb, nh_ofs); 967ae372cb1Swenxu err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag); 968b57dc7c1SPaul Blakey if (err == -EINPROGRESS) { 969b57dc7c1SPaul Blakey retval = TC_ACT_STOLEN; 970b57dc7c1SPaul Blakey goto out; 971b57dc7c1SPaul Blakey } 972b57dc7c1SPaul Blakey if (err) 973b57dc7c1SPaul Blakey goto drop; 974b57dc7c1SPaul Blakey 975b57dc7c1SPaul Blakey err = tcf_ct_skb_network_trim(skb, family); 976b57dc7c1SPaul Blakey if (err) 977b57dc7c1SPaul Blakey goto drop; 978b57dc7c1SPaul Blakey 979b57dc7c1SPaul Blakey /* If we are recirculating packets to match on ct fields and 980b57dc7c1SPaul Blakey * committing with a separate ct action, then we don't need to 981b57dc7c1SPaul Blakey * actually run the packet through conntrack twice unless it's for a 982b57dc7c1SPaul Blakey * different zone. 983b57dc7c1SPaul Blakey */ 984b57dc7c1SPaul Blakey cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force); 985b57dc7c1SPaul Blakey if (!cached) { 98646475bb2SPaul Blakey if (!commit && tcf_ct_flow_table_lookup(p, skb, family)) { 98746475bb2SPaul Blakey skip_add = true; 98846475bb2SPaul Blakey goto do_nat; 98946475bb2SPaul Blakey } 99046475bb2SPaul Blakey 991b57dc7c1SPaul Blakey /* Associate skb with specified zone. */ 992b57dc7c1SPaul Blakey if (tmpl) { 993b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 994b57dc7c1SPaul Blakey if (skb_nfct(skb)) 995b57dc7c1SPaul Blakey nf_conntrack_put(skb_nfct(skb)); 996b57dc7c1SPaul Blakey nf_conntrack_get(&tmpl->ct_general); 997b57dc7c1SPaul Blakey nf_ct_set(skb, tmpl, IP_CT_NEW); 998b57dc7c1SPaul Blakey } 999b57dc7c1SPaul Blakey 1000b57dc7c1SPaul Blakey state.hook = NF_INET_PRE_ROUTING; 1001b57dc7c1SPaul Blakey state.net = net; 1002b57dc7c1SPaul Blakey state.pf = family; 1003b57dc7c1SPaul Blakey err = nf_conntrack_in(skb, &state); 1004b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 1005b57dc7c1SPaul Blakey goto out_push; 1006b57dc7c1SPaul Blakey } 1007b57dc7c1SPaul Blakey 100846475bb2SPaul Blakey do_nat: 1009b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 1010b57dc7c1SPaul Blakey if (!ct) 1011b57dc7c1SPaul Blakey goto out_push; 1012b57dc7c1SPaul Blakey nf_ct_deliver_cached_events(ct); 1013b57dc7c1SPaul Blakey 1014b57dc7c1SPaul Blakey err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 1015b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 1016b57dc7c1SPaul Blakey goto drop; 1017b57dc7c1SPaul Blakey 1018b57dc7c1SPaul Blakey if (commit) { 1019b57dc7c1SPaul Blakey tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 1020b57dc7c1SPaul Blakey tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 1021b57dc7c1SPaul Blakey 1022b57dc7c1SPaul Blakey /* This will take care of sending queued events 1023b57dc7c1SPaul Blakey * even if the connection is already confirmed. 1024b57dc7c1SPaul Blakey */ 1025b57dc7c1SPaul Blakey nf_conntrack_confirm(skb); 102646475bb2SPaul Blakey } else if (!skip_add) { 102764ff70b8SPaul Blakey tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 102846475bb2SPaul Blakey } 102964ff70b8SPaul Blakey 1030b57dc7c1SPaul Blakey out_push: 1031b57dc7c1SPaul Blakey skb_push_rcsum(skb, nh_ofs); 1032b57dc7c1SPaul Blakey 1033b57dc7c1SPaul Blakey out: 10347baf2429Swenxu qdisc_skb_cb(skb)->post_ct = true; 1035*8ca1b090SMarcelo Ricardo Leitner out_clear: 1036*8ca1b090SMarcelo Ricardo Leitner tcf_action_update_bstats(&c->common, skb); 1037ae372cb1Swenxu if (defrag) 1038ae372cb1Swenxu qdisc_skb_cb(skb)->pkt_len = skb->len; 1039b57dc7c1SPaul Blakey return retval; 1040b57dc7c1SPaul Blakey 1041b57dc7c1SPaul Blakey drop: 104226b537a8SVlad Buslov tcf_action_inc_drop_qstats(&c->common); 1043b57dc7c1SPaul Blakey return TC_ACT_SHOT; 1044b57dc7c1SPaul Blakey } 1045b57dc7c1SPaul Blakey 1046b57dc7c1SPaul Blakey static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1047b57dc7c1SPaul Blakey [TCA_CT_ACTION] = { .type = NLA_U16 }, 10488140860cSJohannes Berg [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)), 1049b57dc7c1SPaul Blakey [TCA_CT_ZONE] = { .type = NLA_U16 }, 1050b57dc7c1SPaul Blakey [TCA_CT_MARK] = { .type = NLA_U32 }, 1051b57dc7c1SPaul Blakey [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1052b57dc7c1SPaul Blakey [TCA_CT_LABELS] = { .type = NLA_BINARY, 1053b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1054b57dc7c1SPaul Blakey [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1055b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1056b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1057b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 10588140860cSJohannes Berg [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 10598140860cSJohannes Berg [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1060b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1061b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1062b57dc7c1SPaul Blakey }; 1063b57dc7c1SPaul Blakey 1064b57dc7c1SPaul Blakey static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1065b57dc7c1SPaul Blakey struct tc_ct *parm, 1066b57dc7c1SPaul Blakey struct nlattr **tb, 1067b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1068b57dc7c1SPaul Blakey { 1069b57dc7c1SPaul Blakey struct nf_nat_range2 *range; 1070b57dc7c1SPaul Blakey 1071b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1072b57dc7c1SPaul Blakey return 0; 1073b57dc7c1SPaul Blakey 1074b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_NAT)) { 1075b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1076b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1077b57dc7c1SPaul Blakey } 1078b57dc7c1SPaul Blakey 1079b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1080b57dc7c1SPaul Blakey return 0; 1081b57dc7c1SPaul Blakey 1082b57dc7c1SPaul Blakey if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1083b57dc7c1SPaul Blakey (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1084b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1085b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1086b57dc7c1SPaul Blakey } 1087b57dc7c1SPaul Blakey 1088b57dc7c1SPaul Blakey range = &p->range; 1089b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_IPV4_MIN]) { 1090b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1091b57dc7c1SPaul Blakey 1092b57dc7c1SPaul Blakey p->ipv4_range = true; 1093b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1094b57dc7c1SPaul Blakey range->min_addr.ip = 1095b57dc7c1SPaul Blakey nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1096b57dc7c1SPaul Blakey 1097b57dc7c1SPaul Blakey range->max_addr.ip = max_attr ? 1098b57dc7c1SPaul Blakey nla_get_in_addr(max_attr) : 1099b57dc7c1SPaul Blakey range->min_addr.ip; 1100b57dc7c1SPaul Blakey } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1101b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1102b57dc7c1SPaul Blakey 1103b57dc7c1SPaul Blakey p->ipv4_range = false; 1104b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1105b57dc7c1SPaul Blakey range->min_addr.in6 = 1106b57dc7c1SPaul Blakey nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1107b57dc7c1SPaul Blakey 1108b57dc7c1SPaul Blakey range->max_addr.in6 = max_attr ? 1109b57dc7c1SPaul Blakey nla_get_in6_addr(max_attr) : 1110b57dc7c1SPaul Blakey range->min_addr.in6; 1111b57dc7c1SPaul Blakey } 1112b57dc7c1SPaul Blakey 1113b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_PORT_MIN]) { 1114b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1115b57dc7c1SPaul Blakey range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1116b57dc7c1SPaul Blakey 1117b57dc7c1SPaul Blakey range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1118b57dc7c1SPaul Blakey nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1119b57dc7c1SPaul Blakey range->min_proto.all; 1120b57dc7c1SPaul Blakey } 1121b57dc7c1SPaul Blakey 1122b57dc7c1SPaul Blakey return 0; 1123b57dc7c1SPaul Blakey } 1124b57dc7c1SPaul Blakey 1125b57dc7c1SPaul Blakey static void tcf_ct_set_key_val(struct nlattr **tb, 1126b57dc7c1SPaul Blakey void *val, int val_type, 1127b57dc7c1SPaul Blakey void *mask, int mask_type, 1128b57dc7c1SPaul Blakey int len) 1129b57dc7c1SPaul Blakey { 1130b57dc7c1SPaul Blakey if (!tb[val_type]) 1131b57dc7c1SPaul Blakey return; 1132b57dc7c1SPaul Blakey nla_memcpy(val, tb[val_type], len); 1133b57dc7c1SPaul Blakey 1134b57dc7c1SPaul Blakey if (!mask) 1135b57dc7c1SPaul Blakey return; 1136b57dc7c1SPaul Blakey 1137b57dc7c1SPaul Blakey if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1138b57dc7c1SPaul Blakey memset(mask, 0xff, len); 1139b57dc7c1SPaul Blakey else 1140b57dc7c1SPaul Blakey nla_memcpy(mask, tb[mask_type], len); 1141b57dc7c1SPaul Blakey } 1142b57dc7c1SPaul Blakey 1143b57dc7c1SPaul Blakey static int tcf_ct_fill_params(struct net *net, 1144b57dc7c1SPaul Blakey struct tcf_ct_params *p, 1145b57dc7c1SPaul Blakey struct tc_ct *parm, 1146b57dc7c1SPaul Blakey struct nlattr **tb, 1147b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1148b57dc7c1SPaul Blakey { 1149b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1150b57dc7c1SPaul Blakey struct nf_conntrack_zone zone; 1151b57dc7c1SPaul Blakey struct nf_conn *tmpl; 1152b57dc7c1SPaul Blakey int err; 1153b57dc7c1SPaul Blakey 1154b57dc7c1SPaul Blakey p->zone = NF_CT_DEFAULT_ZONE_ID; 1155b57dc7c1SPaul Blakey 1156b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1157b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1158b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1159b57dc7c1SPaul Blakey sizeof(p->ct_action)); 1160b57dc7c1SPaul Blakey 1161b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1162b57dc7c1SPaul Blakey return 0; 1163b57dc7c1SPaul Blakey 1164b57dc7c1SPaul Blakey err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1165b57dc7c1SPaul Blakey if (err) 1166b57dc7c1SPaul Blakey return err; 1167b57dc7c1SPaul Blakey 1168b57dc7c1SPaul Blakey if (tb[TCA_CT_MARK]) { 1169b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1170b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1171b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1172b57dc7c1SPaul Blakey } 1173b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1174b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1175b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1176b57dc7c1SPaul Blakey sizeof(p->mark)); 1177b57dc7c1SPaul Blakey } 1178b57dc7c1SPaul Blakey 1179b57dc7c1SPaul Blakey if (tb[TCA_CT_LABELS]) { 1180b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1181b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1182b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1183b57dc7c1SPaul Blakey } 1184b57dc7c1SPaul Blakey 1185b57dc7c1SPaul Blakey if (!tn->labels) { 1186b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1187b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1188b57dc7c1SPaul Blakey } 1189b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1190b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1191b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1192b57dc7c1SPaul Blakey sizeof(p->labels)); 1193b57dc7c1SPaul Blakey } 1194b57dc7c1SPaul Blakey 1195b57dc7c1SPaul Blakey if (tb[TCA_CT_ZONE]) { 1196b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1197b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1198b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1199b57dc7c1SPaul Blakey } 1200b57dc7c1SPaul Blakey 1201b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1202b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1203b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1204b57dc7c1SPaul Blakey sizeof(p->zone)); 1205b57dc7c1SPaul Blakey } 1206b57dc7c1SPaul Blakey 1207b57dc7c1SPaul Blakey if (p->zone == NF_CT_DEFAULT_ZONE_ID) 1208b57dc7c1SPaul Blakey return 0; 1209b57dc7c1SPaul Blakey 1210b57dc7c1SPaul Blakey nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1211b57dc7c1SPaul Blakey tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1212b57dc7c1SPaul Blakey if (!tmpl) { 1213b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1214b57dc7c1SPaul Blakey return -ENOMEM; 1215b57dc7c1SPaul Blakey } 1216b57dc7c1SPaul Blakey __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1217b57dc7c1SPaul Blakey nf_conntrack_get(&tmpl->ct_general); 1218b57dc7c1SPaul Blakey p->tmpl = tmpl; 1219b57dc7c1SPaul Blakey 1220b57dc7c1SPaul Blakey return 0; 1221b57dc7c1SPaul Blakey } 1222b57dc7c1SPaul Blakey 1223b57dc7c1SPaul Blakey static int tcf_ct_init(struct net *net, struct nlattr *nla, 1224b57dc7c1SPaul Blakey struct nlattr *est, struct tc_action **a, 1225b57dc7c1SPaul Blakey int replace, int bind, bool rtnl_held, 1226abbb0d33SVlad Buslov struct tcf_proto *tp, u32 flags, 1227b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1228b57dc7c1SPaul Blakey { 1229b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1230b57dc7c1SPaul Blakey struct tcf_ct_params *params = NULL; 1231b57dc7c1SPaul Blakey struct nlattr *tb[TCA_CT_MAX + 1]; 1232b57dc7c1SPaul Blakey struct tcf_chain *goto_ch = NULL; 1233b57dc7c1SPaul Blakey struct tc_ct *parm; 1234b57dc7c1SPaul Blakey struct tcf_ct *c; 1235b57dc7c1SPaul Blakey int err, res = 0; 12367be8ef2cSDmytro Linkin u32 index; 1237b57dc7c1SPaul Blakey 1238b57dc7c1SPaul Blakey if (!nla) { 1239b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1240b57dc7c1SPaul Blakey return -EINVAL; 1241b57dc7c1SPaul Blakey } 1242b57dc7c1SPaul Blakey 1243b57dc7c1SPaul Blakey err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1244b57dc7c1SPaul Blakey if (err < 0) 1245b57dc7c1SPaul Blakey return err; 1246b57dc7c1SPaul Blakey 1247b57dc7c1SPaul Blakey if (!tb[TCA_CT_PARMS]) { 1248b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1249b57dc7c1SPaul Blakey return -EINVAL; 1250b57dc7c1SPaul Blakey } 1251b57dc7c1SPaul Blakey parm = nla_data(tb[TCA_CT_PARMS]); 12527be8ef2cSDmytro Linkin index = parm->index; 12537be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind); 1254b57dc7c1SPaul Blakey if (err < 0) 1255b57dc7c1SPaul Blakey return err; 1256b57dc7c1SPaul Blakey 1257b57dc7c1SPaul Blakey if (!err) { 1258e3822678SVlad Buslov err = tcf_idr_create_from_flags(tn, index, est, a, 1259e3822678SVlad Buslov &act_ct_ops, bind, flags); 1260b57dc7c1SPaul Blakey if (err) { 12617be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 1262b57dc7c1SPaul Blakey return err; 1263b57dc7c1SPaul Blakey } 1264b57dc7c1SPaul Blakey res = ACT_P_CREATED; 1265b57dc7c1SPaul Blakey } else { 1266b57dc7c1SPaul Blakey if (bind) 1267b57dc7c1SPaul Blakey return 0; 1268b57dc7c1SPaul Blakey 1269b57dc7c1SPaul Blakey if (!replace) { 1270b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1271b57dc7c1SPaul Blakey return -EEXIST; 1272b57dc7c1SPaul Blakey } 1273b57dc7c1SPaul Blakey } 1274b57dc7c1SPaul Blakey err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1275b57dc7c1SPaul Blakey if (err < 0) 1276b57dc7c1SPaul Blakey goto cleanup; 1277b57dc7c1SPaul Blakey 1278b57dc7c1SPaul Blakey c = to_ct(*a); 1279b57dc7c1SPaul Blakey 1280b57dc7c1SPaul Blakey params = kzalloc(sizeof(*params), GFP_KERNEL); 1281b57dc7c1SPaul Blakey if (unlikely(!params)) { 1282b57dc7c1SPaul Blakey err = -ENOMEM; 1283b57dc7c1SPaul Blakey goto cleanup; 1284b57dc7c1SPaul Blakey } 1285b57dc7c1SPaul Blakey 1286b57dc7c1SPaul Blakey err = tcf_ct_fill_params(net, params, parm, tb, extack); 1287b57dc7c1SPaul Blakey if (err) 1288b57dc7c1SPaul Blakey goto cleanup; 1289b57dc7c1SPaul Blakey 1290c34b961aSPaul Blakey err = tcf_ct_flow_table_get(params); 1291c34b961aSPaul Blakey if (err) 1292c34b961aSPaul Blakey goto cleanup; 1293c34b961aSPaul Blakey 1294b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1295b57dc7c1SPaul Blakey goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1296445d3749SPaul E. McKenney params = rcu_replace_pointer(c->params, params, 1297445d3749SPaul E. McKenney lockdep_is_held(&c->tcf_lock)); 1298b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1299b57dc7c1SPaul Blakey 1300b57dc7c1SPaul Blakey if (goto_ch) 1301b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 1302b57dc7c1SPaul Blakey if (params) 1303dd2af104SPaul Blakey call_rcu(¶ms->rcu, tcf_ct_params_free); 1304b57dc7c1SPaul Blakey 1305b57dc7c1SPaul Blakey return res; 1306b57dc7c1SPaul Blakey 1307b57dc7c1SPaul Blakey cleanup: 1308b57dc7c1SPaul Blakey if (goto_ch) 1309b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 1310b57dc7c1SPaul Blakey kfree(params); 1311b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1312b57dc7c1SPaul Blakey return err; 1313b57dc7c1SPaul Blakey } 1314b57dc7c1SPaul Blakey 1315b57dc7c1SPaul Blakey static void tcf_ct_cleanup(struct tc_action *a) 1316b57dc7c1SPaul Blakey { 1317b57dc7c1SPaul Blakey struct tcf_ct_params *params; 1318b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1319b57dc7c1SPaul Blakey 1320b57dc7c1SPaul Blakey params = rcu_dereference_protected(c->params, 1); 1321b57dc7c1SPaul Blakey if (params) 1322b57dc7c1SPaul Blakey call_rcu(¶ms->rcu, tcf_ct_params_free); 1323b57dc7c1SPaul Blakey } 1324b57dc7c1SPaul Blakey 1325b57dc7c1SPaul Blakey static int tcf_ct_dump_key_val(struct sk_buff *skb, 1326b57dc7c1SPaul Blakey void *val, int val_type, 1327b57dc7c1SPaul Blakey void *mask, int mask_type, 1328b57dc7c1SPaul Blakey int len) 1329b57dc7c1SPaul Blakey { 1330b57dc7c1SPaul Blakey int err; 1331b57dc7c1SPaul Blakey 1332b57dc7c1SPaul Blakey if (mask && !memchr_inv(mask, 0, len)) 1333b57dc7c1SPaul Blakey return 0; 1334b57dc7c1SPaul Blakey 1335b57dc7c1SPaul Blakey err = nla_put(skb, val_type, len, val); 1336b57dc7c1SPaul Blakey if (err) 1337b57dc7c1SPaul Blakey return err; 1338b57dc7c1SPaul Blakey 1339b57dc7c1SPaul Blakey if (mask_type != TCA_CT_UNSPEC) { 1340b57dc7c1SPaul Blakey err = nla_put(skb, mask_type, len, mask); 1341b57dc7c1SPaul Blakey if (err) 1342b57dc7c1SPaul Blakey return err; 1343b57dc7c1SPaul Blakey } 1344b57dc7c1SPaul Blakey 1345b57dc7c1SPaul Blakey return 0; 1346b57dc7c1SPaul Blakey } 1347b57dc7c1SPaul Blakey 1348b57dc7c1SPaul Blakey static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1349b57dc7c1SPaul Blakey { 1350b57dc7c1SPaul Blakey struct nf_nat_range2 *range = &p->range; 1351b57dc7c1SPaul Blakey 1352b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1353b57dc7c1SPaul Blakey return 0; 1354b57dc7c1SPaul Blakey 1355b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1356b57dc7c1SPaul Blakey return 0; 1357b57dc7c1SPaul Blakey 1358b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1359b57dc7c1SPaul Blakey if (p->ipv4_range) { 1360b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1361b57dc7c1SPaul Blakey range->min_addr.ip)) 1362b57dc7c1SPaul Blakey return -1; 1363b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1364b57dc7c1SPaul Blakey range->max_addr.ip)) 1365b57dc7c1SPaul Blakey return -1; 1366b57dc7c1SPaul Blakey } else { 1367b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1368b57dc7c1SPaul Blakey &range->min_addr.in6)) 1369b57dc7c1SPaul Blakey return -1; 1370b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1371b57dc7c1SPaul Blakey &range->max_addr.in6)) 1372b57dc7c1SPaul Blakey return -1; 1373b57dc7c1SPaul Blakey } 1374b57dc7c1SPaul Blakey } 1375b57dc7c1SPaul Blakey 1376b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1377b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1378b57dc7c1SPaul Blakey range->min_proto.all)) 1379b57dc7c1SPaul Blakey return -1; 1380b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1381b57dc7c1SPaul Blakey range->max_proto.all)) 1382b57dc7c1SPaul Blakey return -1; 1383b57dc7c1SPaul Blakey } 1384b57dc7c1SPaul Blakey 1385b57dc7c1SPaul Blakey return 0; 1386b57dc7c1SPaul Blakey } 1387b57dc7c1SPaul Blakey 1388b57dc7c1SPaul Blakey static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1389b57dc7c1SPaul Blakey int bind, int ref) 1390b57dc7c1SPaul Blakey { 1391b57dc7c1SPaul Blakey unsigned char *b = skb_tail_pointer(skb); 1392b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1393b57dc7c1SPaul Blakey struct tcf_ct_params *p; 1394b57dc7c1SPaul Blakey 1395b57dc7c1SPaul Blakey struct tc_ct opt = { 1396b57dc7c1SPaul Blakey .index = c->tcf_index, 1397b57dc7c1SPaul Blakey .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1398b57dc7c1SPaul Blakey .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1399b57dc7c1SPaul Blakey }; 1400b57dc7c1SPaul Blakey struct tcf_t t; 1401b57dc7c1SPaul Blakey 1402b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1403b57dc7c1SPaul Blakey p = rcu_dereference_protected(c->params, 1404b57dc7c1SPaul Blakey lockdep_is_held(&c->tcf_lock)); 1405b57dc7c1SPaul Blakey opt.action = c->tcf_action; 1406b57dc7c1SPaul Blakey 1407b57dc7c1SPaul Blakey if (tcf_ct_dump_key_val(skb, 1408b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1409b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1410b57dc7c1SPaul Blakey sizeof(p->ct_action))) 1411b57dc7c1SPaul Blakey goto nla_put_failure; 1412b57dc7c1SPaul Blakey 1413b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1414b57dc7c1SPaul Blakey goto skip_dump; 1415b57dc7c1SPaul Blakey 1416b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1417b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1418b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1419b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1420b57dc7c1SPaul Blakey sizeof(p->mark))) 1421b57dc7c1SPaul Blakey goto nla_put_failure; 1422b57dc7c1SPaul Blakey 1423b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1424b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1425b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1426b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1427b57dc7c1SPaul Blakey sizeof(p->labels))) 1428b57dc7c1SPaul Blakey goto nla_put_failure; 1429b57dc7c1SPaul Blakey 1430b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1431b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1432b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1433b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1434b57dc7c1SPaul Blakey sizeof(p->zone))) 1435b57dc7c1SPaul Blakey goto nla_put_failure; 1436b57dc7c1SPaul Blakey 1437b57dc7c1SPaul Blakey if (tcf_ct_dump_nat(skb, p)) 1438b57dc7c1SPaul Blakey goto nla_put_failure; 1439b57dc7c1SPaul Blakey 1440b57dc7c1SPaul Blakey skip_dump: 1441b57dc7c1SPaul Blakey if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1442b57dc7c1SPaul Blakey goto nla_put_failure; 1443b57dc7c1SPaul Blakey 1444b57dc7c1SPaul Blakey tcf_tm_dump(&t, &c->tcf_tm); 1445b57dc7c1SPaul Blakey if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1446b57dc7c1SPaul Blakey goto nla_put_failure; 1447b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1448b57dc7c1SPaul Blakey 1449b57dc7c1SPaul Blakey return skb->len; 1450b57dc7c1SPaul Blakey nla_put_failure: 1451b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1452b57dc7c1SPaul Blakey nlmsg_trim(skb, b); 1453b57dc7c1SPaul Blakey return -1; 1454b57dc7c1SPaul Blakey } 1455b57dc7c1SPaul Blakey 1456b57dc7c1SPaul Blakey static int tcf_ct_walker(struct net *net, struct sk_buff *skb, 1457b57dc7c1SPaul Blakey struct netlink_callback *cb, int type, 1458b57dc7c1SPaul Blakey const struct tc_action_ops *ops, 1459b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1460b57dc7c1SPaul Blakey { 1461b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1462b57dc7c1SPaul Blakey 1463b57dc7c1SPaul Blakey return tcf_generic_walker(tn, skb, cb, type, ops, extack); 1464b57dc7c1SPaul Blakey } 1465b57dc7c1SPaul Blakey 1466b57dc7c1SPaul Blakey static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index) 1467b57dc7c1SPaul Blakey { 1468b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1469b57dc7c1SPaul Blakey 1470b57dc7c1SPaul Blakey return tcf_idr_search(tn, a, index); 1471b57dc7c1SPaul Blakey } 1472b57dc7c1SPaul Blakey 14734b61d3e8SPo Liu static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, 14744b61d3e8SPo Liu u64 drops, u64 lastuse, bool hw) 1475b57dc7c1SPaul Blakey { 1476b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1477b57dc7c1SPaul Blakey 14784b61d3e8SPo Liu tcf_action_update_stats(a, bytes, packets, drops, hw); 1479b57dc7c1SPaul Blakey c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1480b57dc7c1SPaul Blakey } 1481b57dc7c1SPaul Blakey 1482b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops = { 1483b57dc7c1SPaul Blakey .kind = "ct", 1484b57dc7c1SPaul Blakey .id = TCA_ID_CT, 1485b57dc7c1SPaul Blakey .owner = THIS_MODULE, 1486b57dc7c1SPaul Blakey .act = tcf_ct_act, 1487b57dc7c1SPaul Blakey .dump = tcf_ct_dump, 1488b57dc7c1SPaul Blakey .init = tcf_ct_init, 1489b57dc7c1SPaul Blakey .cleanup = tcf_ct_cleanup, 1490b57dc7c1SPaul Blakey .walk = tcf_ct_walker, 1491b57dc7c1SPaul Blakey .lookup = tcf_ct_search, 1492b57dc7c1SPaul Blakey .stats_update = tcf_stats_update, 1493b57dc7c1SPaul Blakey .size = sizeof(struct tcf_ct), 1494b57dc7c1SPaul Blakey }; 1495b57dc7c1SPaul Blakey 1496b57dc7c1SPaul Blakey static __net_init int ct_init_net(struct net *net) 1497b57dc7c1SPaul Blakey { 1498c593642cSPankaj Bharadiya unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1499b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1500b57dc7c1SPaul Blakey 1501b57dc7c1SPaul Blakey if (nf_connlabels_get(net, n_bits - 1)) { 1502b57dc7c1SPaul Blakey tn->labels = false; 1503b57dc7c1SPaul Blakey pr_err("act_ct: Failed to set connlabels length"); 1504b57dc7c1SPaul Blakey } else { 1505b57dc7c1SPaul Blakey tn->labels = true; 1506b57dc7c1SPaul Blakey } 1507b57dc7c1SPaul Blakey 1508981471bdSCong Wang return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1509b57dc7c1SPaul Blakey } 1510b57dc7c1SPaul Blakey 1511b57dc7c1SPaul Blakey static void __net_exit ct_exit_net(struct list_head *net_list) 1512b57dc7c1SPaul Blakey { 1513b57dc7c1SPaul Blakey struct net *net; 1514b57dc7c1SPaul Blakey 1515b57dc7c1SPaul Blakey rtnl_lock(); 1516b57dc7c1SPaul Blakey list_for_each_entry(net, net_list, exit_list) { 1517b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1518b57dc7c1SPaul Blakey 1519b57dc7c1SPaul Blakey if (tn->labels) 1520b57dc7c1SPaul Blakey nf_connlabels_put(net); 1521b57dc7c1SPaul Blakey } 1522b57dc7c1SPaul Blakey rtnl_unlock(); 1523b57dc7c1SPaul Blakey 1524b57dc7c1SPaul Blakey tc_action_net_exit(net_list, ct_net_id); 1525b57dc7c1SPaul Blakey } 1526b57dc7c1SPaul Blakey 1527b57dc7c1SPaul Blakey static struct pernet_operations ct_net_ops = { 1528b57dc7c1SPaul Blakey .init = ct_init_net, 1529b57dc7c1SPaul Blakey .exit_batch = ct_exit_net, 1530b57dc7c1SPaul Blakey .id = &ct_net_id, 1531b57dc7c1SPaul Blakey .size = sizeof(struct tc_ct_action_net), 1532b57dc7c1SPaul Blakey }; 1533b57dc7c1SPaul Blakey 1534b57dc7c1SPaul Blakey static int __init ct_init_module(void) 1535b57dc7c1SPaul Blakey { 1536c34b961aSPaul Blakey int err; 1537c34b961aSPaul Blakey 1538c34b961aSPaul Blakey act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1539c34b961aSPaul Blakey if (!act_ct_wq) 1540c34b961aSPaul Blakey return -ENOMEM; 1541c34b961aSPaul Blakey 1542c34b961aSPaul Blakey err = tcf_ct_flow_tables_init(); 1543c34b961aSPaul Blakey if (err) 1544c34b961aSPaul Blakey goto err_tbl_init; 1545c34b961aSPaul Blakey 1546c34b961aSPaul Blakey err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1547c34b961aSPaul Blakey if (err) 1548c34b961aSPaul Blakey goto err_register; 1549c34b961aSPaul Blakey 1550c129412fSwenxu static_branch_inc(&tcf_frag_xmit_count); 1551c129412fSwenxu 1552c34b961aSPaul Blakey return 0; 1553c34b961aSPaul Blakey 1554c34b961aSPaul Blakey err_register: 1555c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 15568c5c51f5Sliujian err_tbl_init: 15578c5c51f5Sliujian destroy_workqueue(act_ct_wq); 1558c34b961aSPaul Blakey return err; 1559b57dc7c1SPaul Blakey } 1560b57dc7c1SPaul Blakey 1561b57dc7c1SPaul Blakey static void __exit ct_cleanup_module(void) 1562b57dc7c1SPaul Blakey { 1563c129412fSwenxu static_branch_dec(&tcf_frag_xmit_count); 1564b57dc7c1SPaul Blakey tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1565c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 1566c34b961aSPaul Blakey destroy_workqueue(act_ct_wq); 1567b57dc7c1SPaul Blakey } 1568b57dc7c1SPaul Blakey 1569b57dc7c1SPaul Blakey module_init(ct_init_module); 1570b57dc7c1SPaul Blakey module_exit(ct_cleanup_module); 1571b57dc7c1SPaul Blakey MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1572b57dc7c1SPaul Blakey MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1573b57dc7c1SPaul Blakey MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1574b57dc7c1SPaul Blakey MODULE_DESCRIPTION("Connection tracking action"); 1575b57dc7c1SPaul Blakey MODULE_LICENSE("GPL v2"); 1576