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> 27871cf386SPedro Tammela #include <net/tc_wrapper.h> 28b57dc7c1SPaul Blakey 29c34b961aSPaul Blakey #include <net/netfilter/nf_flow_table.h> 30b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack.h> 31b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_core.h> 32b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_zones.h> 33b57dc7c1SPaul Blakey #include <net/netfilter/nf_conntrack_helper.h> 34beb97d3aSwenxu #include <net/netfilter/nf_conntrack_acct.h> 35b57dc7c1SPaul Blakey #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 369795ded7SPaul Blakey #include <net/netfilter/nf_conntrack_act_ct.h> 37a21b06e7SXin Long #include <net/netfilter/nf_conntrack_seqadj.h> 3840d102cdSJeremy Sowden #include <uapi/linux/netfilter/nf_nat.h> 39b57dc7c1SPaul Blakey 40c34b961aSPaul Blakey static struct workqueue_struct *act_ct_wq; 41c34b961aSPaul Blakey static struct rhashtable zones_ht; 42138470a9SEric Dumazet static DEFINE_MUTEX(zones_mutex); 43c34b961aSPaul Blakey 449126fd82SXin Long struct zones_ht_key { 459126fd82SXin Long struct net *net; 469126fd82SXin Long u16 zone; 47*d7cc186dSEric Dumazet /* Note : pad[] must be the last field. */ 48*d7cc186dSEric Dumazet u8 pad[]; 499126fd82SXin Long }; 509126fd82SXin Long 51c34b961aSPaul Blakey struct tcf_ct_flow_table { 52c34b961aSPaul Blakey struct rhash_head node; /* In zones tables */ 53c34b961aSPaul Blakey 54c34b961aSPaul Blakey struct rcu_work rwork; 55c34b961aSPaul Blakey struct nf_flowtable nf_ft; 56138470a9SEric Dumazet refcount_t ref; 579126fd82SXin Long struct zones_ht_key key; 58c34b961aSPaul Blakey 59c34b961aSPaul Blakey bool dying; 60c34b961aSPaul Blakey }; 61c34b961aSPaul Blakey 62c34b961aSPaul Blakey static const struct rhashtable_params zones_params = { 63c34b961aSPaul Blakey .head_offset = offsetof(struct tcf_ct_flow_table, node), 649126fd82SXin Long .key_offset = offsetof(struct tcf_ct_flow_table, key), 65*d7cc186dSEric Dumazet .key_len = offsetof(struct zones_ht_key, pad), 66c34b961aSPaul Blakey .automatic_shrinking = true, 67c34b961aSPaul Blakey }; 68c34b961aSPaul Blakey 699c26ba9bSPaul Blakey static struct flow_action_entry * 709c26ba9bSPaul Blakey tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 719c26ba9bSPaul Blakey { 729c26ba9bSPaul Blakey int i = flow_action->num_entries++; 739c26ba9bSPaul Blakey 749c26ba9bSPaul Blakey return &flow_action->entries[i]; 759c26ba9bSPaul Blakey } 769c26ba9bSPaul Blakey 779c26ba9bSPaul Blakey static void tcf_ct_add_mangle_action(struct flow_action *action, 789c26ba9bSPaul Blakey enum flow_action_mangle_base htype, 799c26ba9bSPaul Blakey u32 offset, 809c26ba9bSPaul Blakey u32 mask, 819c26ba9bSPaul Blakey u32 val) 829c26ba9bSPaul Blakey { 839c26ba9bSPaul Blakey struct flow_action_entry *entry; 849c26ba9bSPaul Blakey 859c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 869c26ba9bSPaul Blakey entry->id = FLOW_ACTION_MANGLE; 879c26ba9bSPaul Blakey entry->mangle.htype = htype; 889c26ba9bSPaul Blakey entry->mangle.mask = ~mask; 899c26ba9bSPaul Blakey entry->mangle.offset = offset; 909c26ba9bSPaul Blakey entry->mangle.val = val; 919c26ba9bSPaul Blakey } 929c26ba9bSPaul Blakey 939c26ba9bSPaul Blakey /* The following nat helper functions check if the inverted reverse tuple 949c26ba9bSPaul Blakey * (target) is different then the current dir tuple - meaning nat for ports 959c26ba9bSPaul Blakey * and/or ip is needed, and add the relevant mangle actions. 969c26ba9bSPaul Blakey */ 979c26ba9bSPaul Blakey static void 989c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 999c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1009c26ba9bSPaul Blakey struct flow_action *action) 1019c26ba9bSPaul Blakey { 1029c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 1039c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 1049c26ba9bSPaul Blakey offsetof(struct iphdr, saddr), 1059c26ba9bSPaul Blakey 0xFFFFFFFF, 1069c26ba9bSPaul Blakey be32_to_cpu(target.src.u3.ip)); 1079c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 1089c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 1099c26ba9bSPaul Blakey offsetof(struct iphdr, daddr), 1109c26ba9bSPaul Blakey 0xFFFFFFFF, 1119c26ba9bSPaul Blakey be32_to_cpu(target.dst.u3.ip)); 1129c26ba9bSPaul Blakey } 1139c26ba9bSPaul Blakey 1149c26ba9bSPaul Blakey static void 1159c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 1169c26ba9bSPaul Blakey union nf_inet_addr *addr, 1179c26ba9bSPaul Blakey u32 offset) 1189c26ba9bSPaul Blakey { 1199c26ba9bSPaul Blakey int i; 1209c26ba9bSPaul Blakey 1219c26ba9bSPaul Blakey for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 1229c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 1239c26ba9bSPaul Blakey i * sizeof(u32) + offset, 1249c26ba9bSPaul Blakey 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 1259c26ba9bSPaul Blakey } 1269c26ba9bSPaul Blakey 1279c26ba9bSPaul Blakey static void 1289c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 1299c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1309c26ba9bSPaul Blakey struct flow_action *action) 1319c26ba9bSPaul Blakey { 1329c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 1339c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 1349c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 1359c26ba9bSPaul Blakey saddr)); 1369c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 1379c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 1389c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 1399c26ba9bSPaul Blakey daddr)); 1409c26ba9bSPaul Blakey } 1419c26ba9bSPaul Blakey 1429c26ba9bSPaul Blakey static void 1439c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 1449c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1459c26ba9bSPaul Blakey struct flow_action *action) 1469c26ba9bSPaul Blakey { 1479c26ba9bSPaul Blakey __be16 target_src = target.src.u.tcp.port; 1489c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.tcp.port; 1499c26ba9bSPaul Blakey 1509c26ba9bSPaul Blakey if (target_src != tuple->src.u.tcp.port) 1519c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 1529c26ba9bSPaul Blakey offsetof(struct tcphdr, source), 1539c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 1549c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.tcp.port) 1559c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 1569c26ba9bSPaul Blakey offsetof(struct tcphdr, dest), 1579c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 1589c26ba9bSPaul Blakey } 1599c26ba9bSPaul Blakey 1609c26ba9bSPaul Blakey static void 1619c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 1629c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 1639c26ba9bSPaul Blakey struct flow_action *action) 1649c26ba9bSPaul Blakey { 1659c26ba9bSPaul Blakey __be16 target_src = target.src.u.udp.port; 1669c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.udp.port; 1679c26ba9bSPaul Blakey 1689c26ba9bSPaul Blakey if (target_src != tuple->src.u.udp.port) 16947b5d2a1SRoi Dayan tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 1709c26ba9bSPaul Blakey offsetof(struct udphdr, source), 1719c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 1729c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.udp.port) 17347b5d2a1SRoi Dayan tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 1749c26ba9bSPaul Blakey offsetof(struct udphdr, dest), 1759c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 1769c26ba9bSPaul Blakey } 1779c26ba9bSPaul Blakey 1789c26ba9bSPaul Blakey static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 1799c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 1801a441a9bSVlad Buslov enum ip_conntrack_info ctinfo, 1819c26ba9bSPaul Blakey struct flow_action *action) 1829c26ba9bSPaul Blakey { 1839c26ba9bSPaul Blakey struct nf_conn_labels *ct_labels; 1849c26ba9bSPaul Blakey struct flow_action_entry *entry; 1859c26ba9bSPaul Blakey u32 *act_ct_labels; 1869c26ba9bSPaul Blakey 1879c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 1889c26ba9bSPaul Blakey entry->id = FLOW_ACTION_CT_METADATA; 1899c26ba9bSPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 19052d1aa8bSDaniel Xu entry->ct_metadata.mark = READ_ONCE(ct->mark); 1919c26ba9bSPaul Blakey #endif 19230b0cf90SPaul Blakey /* aligns with the CT reference on the SKB nf_ct_set */ 19330b0cf90SPaul Blakey entry->ct_metadata.cookie = (unsigned long)ct | ctinfo; 194941eff5aSPaul Blakey entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL; 1959c26ba9bSPaul Blakey 1969c26ba9bSPaul Blakey act_ct_labels = entry->ct_metadata.labels; 1979c26ba9bSPaul Blakey ct_labels = nf_ct_labels_find(ct); 1989c26ba9bSPaul Blakey if (ct_labels) 1999c26ba9bSPaul Blakey memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 2009c26ba9bSPaul Blakey else 2019c26ba9bSPaul Blakey memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 2029c26ba9bSPaul Blakey } 2039c26ba9bSPaul Blakey 2049c26ba9bSPaul Blakey static int tcf_ct_flow_table_add_action_nat(struct net *net, 2059c26ba9bSPaul Blakey struct nf_conn *ct, 2069c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 2079c26ba9bSPaul Blakey struct flow_action *action) 2089c26ba9bSPaul Blakey { 2099c26ba9bSPaul Blakey const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 2109c26ba9bSPaul Blakey struct nf_conntrack_tuple target; 2119c26ba9bSPaul Blakey 21205aa69e5Swenxu if (!(ct->status & IPS_NAT_MASK)) 21305aa69e5Swenxu return 0; 21405aa69e5Swenxu 2159c26ba9bSPaul Blakey nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 2169c26ba9bSPaul Blakey 2179c26ba9bSPaul Blakey switch (tuple->src.l3num) { 2189c26ba9bSPaul Blakey case NFPROTO_IPV4: 2199c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 2209c26ba9bSPaul Blakey action); 2219c26ba9bSPaul Blakey break; 2229c26ba9bSPaul Blakey case NFPROTO_IPV6: 2239c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 2249c26ba9bSPaul Blakey action); 2259c26ba9bSPaul Blakey break; 2269c26ba9bSPaul Blakey default: 2279c26ba9bSPaul Blakey return -EOPNOTSUPP; 2289c26ba9bSPaul Blakey } 2299c26ba9bSPaul Blakey 2309c26ba9bSPaul Blakey switch (nf_ct_protonum(ct)) { 2319c26ba9bSPaul Blakey case IPPROTO_TCP: 2329c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 2339c26ba9bSPaul Blakey break; 2349c26ba9bSPaul Blakey case IPPROTO_UDP: 2359c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 2369c26ba9bSPaul Blakey break; 2379c26ba9bSPaul Blakey default: 2389c26ba9bSPaul Blakey return -EOPNOTSUPP; 2399c26ba9bSPaul Blakey } 2409c26ba9bSPaul Blakey 2419c26ba9bSPaul Blakey return 0; 2429c26ba9bSPaul Blakey } 2439c26ba9bSPaul Blakey 2449c26ba9bSPaul Blakey static int tcf_ct_flow_table_fill_actions(struct net *net, 2451a441a9bSVlad Buslov struct flow_offload *flow, 2469c26ba9bSPaul Blakey enum flow_offload_tuple_dir tdir, 2479c26ba9bSPaul Blakey struct nf_flow_rule *flow_rule) 2489c26ba9bSPaul Blakey { 2499c26ba9bSPaul Blakey struct flow_action *action = &flow_rule->rule->action; 2509c26ba9bSPaul Blakey int num_entries = action->num_entries; 2519c26ba9bSPaul Blakey struct nf_conn *ct = flow->ct; 2521a441a9bSVlad Buslov enum ip_conntrack_info ctinfo; 2539c26ba9bSPaul Blakey enum ip_conntrack_dir dir; 2549c26ba9bSPaul Blakey int i, err; 2559c26ba9bSPaul Blakey 2569c26ba9bSPaul Blakey switch (tdir) { 2579c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_ORIGINAL: 2589c26ba9bSPaul Blakey dir = IP_CT_DIR_ORIGINAL; 259d5774cb6SVlad Buslov ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ? 260d5774cb6SVlad Buslov IP_CT_ESTABLISHED : IP_CT_NEW; 261d5774cb6SVlad Buslov if (ctinfo == IP_CT_ESTABLISHED) 2621a441a9bSVlad Buslov set_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags); 2639c26ba9bSPaul Blakey break; 2649c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_REPLY: 2659c26ba9bSPaul Blakey dir = IP_CT_DIR_REPLY; 2661a441a9bSVlad Buslov ctinfo = IP_CT_ESTABLISHED_REPLY; 2679c26ba9bSPaul Blakey break; 2689c26ba9bSPaul Blakey default: 2699c26ba9bSPaul Blakey return -EOPNOTSUPP; 2709c26ba9bSPaul Blakey } 2719c26ba9bSPaul Blakey 2729c26ba9bSPaul Blakey err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 2739c26ba9bSPaul Blakey if (err) 2749c26ba9bSPaul Blakey goto err_nat; 2759c26ba9bSPaul Blakey 2761a441a9bSVlad Buslov tcf_ct_flow_table_add_action_meta(ct, dir, ctinfo, action); 2779c26ba9bSPaul Blakey return 0; 2789c26ba9bSPaul Blakey 2799c26ba9bSPaul Blakey err_nat: 2809c26ba9bSPaul Blakey /* Clear filled actions */ 2819c26ba9bSPaul Blakey for (i = num_entries; i < action->num_entries; i++) 2829c26ba9bSPaul Blakey memset(&action->entries[i], 0, sizeof(action->entries[i])); 2839c26ba9bSPaul Blakey action->num_entries = num_entries; 2849c26ba9bSPaul Blakey 2859c26ba9bSPaul Blakey return err; 2869c26ba9bSPaul Blakey } 2879c26ba9bSPaul Blakey 288735795f6SPablo Neira Ayuso static bool tcf_ct_flow_is_outdated(const struct flow_offload *flow) 289735795f6SPablo Neira Ayuso { 290735795f6SPablo Neira Ayuso return test_bit(IPS_SEEN_REPLY_BIT, &flow->ct->status) && 291a63b6622SVlad Buslov test_bit(IPS_HW_OFFLOAD_BIT, &flow->ct->status) && 292a63b6622SVlad Buslov !test_bit(NF_FLOW_HW_PENDING, &flow->flags) && 293735795f6SPablo Neira Ayuso !test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags); 294735795f6SPablo Neira Ayuso } 295735795f6SPablo Neira Ayuso 29615f300edSVlad Buslov static void tcf_ct_flow_table_get_ref(struct tcf_ct_flow_table *ct_ft); 29715f300edSVlad Buslov 29815f300edSVlad Buslov static void tcf_ct_nf_get(struct nf_flowtable *ft) 29915f300edSVlad Buslov { 30015f300edSVlad Buslov struct tcf_ct_flow_table *ct_ft = 30115f300edSVlad Buslov container_of(ft, struct tcf_ct_flow_table, nf_ft); 30215f300edSVlad Buslov 30315f300edSVlad Buslov tcf_ct_flow_table_get_ref(ct_ft); 30415f300edSVlad Buslov } 30515f300edSVlad Buslov 30615f300edSVlad Buslov static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft); 30715f300edSVlad Buslov 30815f300edSVlad Buslov static void tcf_ct_nf_put(struct nf_flowtable *ft) 30915f300edSVlad Buslov { 31015f300edSVlad Buslov struct tcf_ct_flow_table *ct_ft = 31115f300edSVlad Buslov container_of(ft, struct tcf_ct_flow_table, nf_ft); 31215f300edSVlad Buslov 31315f300edSVlad Buslov tcf_ct_flow_table_put(ct_ft); 31415f300edSVlad Buslov } 31515f300edSVlad Buslov 316c34b961aSPaul Blakey static struct nf_flowtable_type flowtable_ct = { 317735795f6SPablo Neira Ayuso .gc = tcf_ct_flow_is_outdated, 3189c26ba9bSPaul Blakey .action = tcf_ct_flow_table_fill_actions, 31915f300edSVlad Buslov .get = tcf_ct_nf_get, 32015f300edSVlad Buslov .put = tcf_ct_nf_put, 321c34b961aSPaul Blakey .owner = THIS_MODULE, 322c34b961aSPaul Blakey }; 323c34b961aSPaul Blakey 324fc54d906SVlad Buslov static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params) 325c34b961aSPaul Blakey { 3269126fd82SXin Long struct zones_ht_key key = { .net = net, .zone = params->zone }; 327c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 328c34b961aSPaul Blakey int err = -ENOMEM; 329c34b961aSPaul Blakey 330138470a9SEric Dumazet mutex_lock(&zones_mutex); 3319126fd82SXin Long ct_ft = rhashtable_lookup_fast(&zones_ht, &key, zones_params); 332138470a9SEric Dumazet if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 333138470a9SEric Dumazet goto out_unlock; 334c34b961aSPaul Blakey 335138470a9SEric Dumazet ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 336c34b961aSPaul Blakey if (!ct_ft) 337c34b961aSPaul Blakey goto err_alloc; 338138470a9SEric Dumazet refcount_set(&ct_ft->ref, 1); 339c34b961aSPaul Blakey 3409126fd82SXin Long ct_ft->key = key; 341c34b961aSPaul Blakey err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 342c34b961aSPaul Blakey if (err) 343c34b961aSPaul Blakey goto err_insert; 344c34b961aSPaul Blakey 345c34b961aSPaul Blakey ct_ft->nf_ft.type = &flowtable_ct; 3463567e233SMarcelo Ricardo Leitner ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 3473567e233SMarcelo Ricardo Leitner NF_FLOWTABLE_COUNTER; 348c34b961aSPaul Blakey err = nf_flow_table_init(&ct_ft->nf_ft); 349c34b961aSPaul Blakey if (err) 350c34b961aSPaul Blakey goto err_init; 351fc54d906SVlad Buslov write_pnet(&ct_ft->nf_ft.net, net); 352c34b961aSPaul Blakey 353c34b961aSPaul Blakey __module_get(THIS_MODULE); 354138470a9SEric Dumazet out_unlock: 355c34b961aSPaul Blakey params->ct_ft = ct_ft; 356edd5861eSPaul Blakey params->nf_ft = &ct_ft->nf_ft; 357138470a9SEric Dumazet mutex_unlock(&zones_mutex); 358c34b961aSPaul Blakey 359c34b961aSPaul Blakey return 0; 360c34b961aSPaul Blakey 361c34b961aSPaul Blakey err_init: 362c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 363c34b961aSPaul Blakey err_insert: 364c34b961aSPaul Blakey kfree(ct_ft); 365c34b961aSPaul Blakey err_alloc: 366138470a9SEric Dumazet mutex_unlock(&zones_mutex); 367c34b961aSPaul Blakey return err; 368c34b961aSPaul Blakey } 369c34b961aSPaul Blakey 37015f300edSVlad Buslov static void tcf_ct_flow_table_get_ref(struct tcf_ct_flow_table *ct_ft) 37115f300edSVlad Buslov { 37215f300edSVlad Buslov refcount_inc(&ct_ft->ref); 37315f300edSVlad Buslov } 37415f300edSVlad Buslov 375c34b961aSPaul Blakey static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 376c34b961aSPaul Blakey { 377c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 37877ac5e40SLouis Peens struct flow_block *block; 379c34b961aSPaul Blakey 380c34b961aSPaul Blakey ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 381c34b961aSPaul Blakey rwork); 382c34b961aSPaul Blakey nf_flow_table_free(&ct_ft->nf_ft); 38377ac5e40SLouis Peens 38477ac5e40SLouis Peens block = &ct_ft->nf_ft.flow_block; 38577ac5e40SLouis Peens down_write(&ct_ft->nf_ft.flow_block_lock); 38615f300edSVlad Buslov WARN_ON(!list_empty(&block->cb_list)); 38777ac5e40SLouis Peens up_write(&ct_ft->nf_ft.flow_block_lock); 388c34b961aSPaul Blakey kfree(ct_ft); 389c34b961aSPaul Blakey 390c34b961aSPaul Blakey module_put(THIS_MODULE); 391c34b961aSPaul Blakey } 392c34b961aSPaul Blakey 39319138941SXin Long static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft) 394c34b961aSPaul Blakey { 39519138941SXin Long if (refcount_dec_and_test(&ct_ft->ref)) { 396c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 397c34b961aSPaul Blakey INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 398c34b961aSPaul Blakey queue_rcu_work(act_ct_wq, &ct_ft->rwork); 399c34b961aSPaul Blakey } 400c34b961aSPaul Blakey } 401c34b961aSPaul Blakey 402db6140e5SPaul Blakey static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry, 403db6140e5SPaul Blakey struct nf_conn_act_ct_ext *act_ct_ext, u8 dir) 404db6140e5SPaul Blakey { 405db6140e5SPaul Blakey entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC; 406db6140e5SPaul Blakey entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir]; 407db6140e5SPaul Blakey } 408db6140e5SPaul Blakey 409d5a116dbSVlad Buslov static void tcf_ct_flow_ct_ext_ifidx_update(struct flow_offload *entry) 410d5a116dbSVlad Buslov { 411d5a116dbSVlad Buslov struct nf_conn_act_ct_ext *act_ct_ext; 412d5a116dbSVlad Buslov 413d5a116dbSVlad Buslov act_ct_ext = nf_conn_act_ct_ext_find(entry->ct); 414d5a116dbSVlad Buslov if (act_ct_ext) { 415d5a116dbSVlad Buslov tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 416d5a116dbSVlad Buslov tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 417d5a116dbSVlad Buslov } 418d5a116dbSVlad Buslov } 419d5a116dbSVlad Buslov 42064ff70b8SPaul Blakey static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 42164ff70b8SPaul Blakey struct nf_conn *ct, 4226a9bad00SVlad Buslov bool tcp, bool bidirectional) 42364ff70b8SPaul Blakey { 4249795ded7SPaul Blakey struct nf_conn_act_ct_ext *act_ct_ext; 42564ff70b8SPaul Blakey struct flow_offload *entry; 42664ff70b8SPaul Blakey int err; 42764ff70b8SPaul Blakey 42864ff70b8SPaul Blakey if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 42964ff70b8SPaul Blakey return; 43064ff70b8SPaul Blakey 43164ff70b8SPaul Blakey entry = flow_offload_alloc(ct); 43264ff70b8SPaul Blakey if (!entry) { 43364ff70b8SPaul Blakey WARN_ON_ONCE(1); 43464ff70b8SPaul Blakey goto err_alloc; 43564ff70b8SPaul Blakey } 43664ff70b8SPaul Blakey 43764ff70b8SPaul Blakey if (tcp) { 43864ff70b8SPaul Blakey ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 43964ff70b8SPaul Blakey ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 44064ff70b8SPaul Blakey } 4416a9bad00SVlad Buslov if (bidirectional) 4426a9bad00SVlad Buslov __set_bit(NF_FLOW_HW_BIDIRECTIONAL, &entry->flags); 44364ff70b8SPaul Blakey 4449795ded7SPaul Blakey act_ct_ext = nf_conn_act_ct_ext_find(ct); 4459795ded7SPaul Blakey if (act_ct_ext) { 446db6140e5SPaul Blakey tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 447db6140e5SPaul Blakey tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 4489795ded7SPaul Blakey } 4499795ded7SPaul Blakey 45064ff70b8SPaul Blakey err = flow_offload_add(&ct_ft->nf_ft, entry); 45164ff70b8SPaul Blakey if (err) 45264ff70b8SPaul Blakey goto err_add; 45364ff70b8SPaul Blakey 45464ff70b8SPaul Blakey return; 45564ff70b8SPaul Blakey 45664ff70b8SPaul Blakey err_add: 45764ff70b8SPaul Blakey flow_offload_free(entry); 45864ff70b8SPaul Blakey err_alloc: 45964ff70b8SPaul Blakey clear_bit(IPS_OFFLOAD_BIT, &ct->status); 46064ff70b8SPaul Blakey } 46164ff70b8SPaul Blakey 46264ff70b8SPaul Blakey static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 46364ff70b8SPaul Blakey struct nf_conn *ct, 46464ff70b8SPaul Blakey enum ip_conntrack_info ctinfo) 46564ff70b8SPaul Blakey { 4666a9bad00SVlad Buslov bool tcp = false, bidirectional = true; 46764ff70b8SPaul Blakey 46864ff70b8SPaul Blakey switch (nf_ct_protonum(ct)) { 46964ff70b8SPaul Blakey case IPPROTO_TCP: 4706a9bad00SVlad Buslov if ((ctinfo != IP_CT_ESTABLISHED && 4716a9bad00SVlad Buslov ctinfo != IP_CT_ESTABLISHED_REPLY) || 4726a9bad00SVlad Buslov !test_bit(IPS_ASSURED_BIT, &ct->status) || 4736a9bad00SVlad Buslov ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 47464ff70b8SPaul Blakey return; 4756a9bad00SVlad Buslov 4766a9bad00SVlad Buslov tcp = true; 47764ff70b8SPaul Blakey break; 47864ff70b8SPaul Blakey case IPPROTO_UDP: 4796a9bad00SVlad Buslov if (!nf_ct_is_confirmed(ct)) 4806a9bad00SVlad Buslov return; 4816a9bad00SVlad Buslov if (!test_bit(IPS_ASSURED_BIT, &ct->status)) 4826a9bad00SVlad Buslov bidirectional = false; 48364ff70b8SPaul Blakey break; 484fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE 485fcb6aa86SToshiaki Makita case IPPROTO_GRE: { 486fcb6aa86SToshiaki Makita struct nf_conntrack_tuple *tuple; 487fcb6aa86SToshiaki Makita 4886a9bad00SVlad Buslov if ((ctinfo != IP_CT_ESTABLISHED && 4896a9bad00SVlad Buslov ctinfo != IP_CT_ESTABLISHED_REPLY) || 4906a9bad00SVlad Buslov !test_bit(IPS_ASSURED_BIT, &ct->status) || 4916a9bad00SVlad Buslov ct->status & IPS_NAT_MASK) 492fcb6aa86SToshiaki Makita return; 4936a9bad00SVlad Buslov 494fcb6aa86SToshiaki Makita tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; 495fcb6aa86SToshiaki Makita /* No support for GRE v1 */ 496fcb6aa86SToshiaki Makita if (tuple->src.u.gre.key || tuple->dst.u.gre.key) 497fcb6aa86SToshiaki Makita return; 498fcb6aa86SToshiaki Makita break; 499fcb6aa86SToshiaki Makita } 500fcb6aa86SToshiaki Makita #endif 50164ff70b8SPaul Blakey default: 50264ff70b8SPaul Blakey return; 50364ff70b8SPaul Blakey } 50464ff70b8SPaul Blakey 50564ff70b8SPaul Blakey if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 50664ff70b8SPaul Blakey ct->status & IPS_SEQ_ADJUST) 50764ff70b8SPaul Blakey return; 50864ff70b8SPaul Blakey 5096a9bad00SVlad Buslov tcf_ct_flow_table_add(ct_ft, ct, tcp, bidirectional); 51064ff70b8SPaul Blakey } 51164ff70b8SPaul Blakey 51246475bb2SPaul Blakey static bool 51346475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 51407ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 51507ac9d16SPaul Blakey struct tcphdr **tcph) 51646475bb2SPaul Blakey { 51746475bb2SPaul Blakey struct flow_ports *ports; 51846475bb2SPaul Blakey unsigned int thoff; 51946475bb2SPaul Blakey struct iphdr *iph; 520fcb6aa86SToshiaki Makita size_t hdrsize; 521fcb6aa86SToshiaki Makita u8 ipproto; 52246475bb2SPaul Blakey 5234cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*iph))) 52446475bb2SPaul Blakey return false; 52546475bb2SPaul Blakey 52646475bb2SPaul Blakey iph = ip_hdr(skb); 52746475bb2SPaul Blakey thoff = iph->ihl * 4; 52846475bb2SPaul Blakey 52946475bb2SPaul Blakey if (ip_is_fragment(iph) || 53046475bb2SPaul Blakey unlikely(thoff != sizeof(struct iphdr))) 53146475bb2SPaul Blakey return false; 53246475bb2SPaul Blakey 533fcb6aa86SToshiaki Makita ipproto = iph->protocol; 534fcb6aa86SToshiaki Makita switch (ipproto) { 535fcb6aa86SToshiaki Makita case IPPROTO_TCP: 536fcb6aa86SToshiaki Makita hdrsize = sizeof(struct tcphdr); 537fcb6aa86SToshiaki Makita break; 538fcb6aa86SToshiaki Makita case IPPROTO_UDP: 539fcb6aa86SToshiaki Makita hdrsize = sizeof(*ports); 540fcb6aa86SToshiaki Makita break; 541fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE 542fcb6aa86SToshiaki Makita case IPPROTO_GRE: 543fcb6aa86SToshiaki Makita hdrsize = sizeof(struct gre_base_hdr); 544fcb6aa86SToshiaki Makita break; 545fcb6aa86SToshiaki Makita #endif 546fcb6aa86SToshiaki Makita default: 54746475bb2SPaul Blakey return false; 548fcb6aa86SToshiaki Makita } 54946475bb2SPaul Blakey 55046475bb2SPaul Blakey if (iph->ttl <= 1) 55146475bb2SPaul Blakey return false; 55246475bb2SPaul Blakey 553fcb6aa86SToshiaki Makita if (!pskb_network_may_pull(skb, thoff + hdrsize)) 55446475bb2SPaul Blakey return false; 55546475bb2SPaul Blakey 556fcb6aa86SToshiaki Makita switch (ipproto) { 557fcb6aa86SToshiaki Makita case IPPROTO_TCP: 55807ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 559fcb6aa86SToshiaki Makita fallthrough; 560fcb6aa86SToshiaki Makita case IPPROTO_UDP: 56107ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 56246475bb2SPaul Blakey tuple->src_port = ports->source; 56346475bb2SPaul Blakey tuple->dst_port = ports->dest; 564fcb6aa86SToshiaki Makita break; 565fcb6aa86SToshiaki Makita case IPPROTO_GRE: { 566fcb6aa86SToshiaki Makita struct gre_base_hdr *greh; 567fcb6aa86SToshiaki Makita 568fcb6aa86SToshiaki Makita greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 569fcb6aa86SToshiaki Makita if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 570fcb6aa86SToshiaki Makita return false; 571fcb6aa86SToshiaki Makita break; 572fcb6aa86SToshiaki Makita } 573fcb6aa86SToshiaki Makita } 574fcb6aa86SToshiaki Makita 575fcb6aa86SToshiaki Makita iph = ip_hdr(skb); 576fcb6aa86SToshiaki Makita 577fcb6aa86SToshiaki Makita tuple->src_v4.s_addr = iph->saddr; 578fcb6aa86SToshiaki Makita tuple->dst_v4.s_addr = iph->daddr; 57946475bb2SPaul Blakey tuple->l3proto = AF_INET; 580fcb6aa86SToshiaki Makita tuple->l4proto = ipproto; 58146475bb2SPaul Blakey 58246475bb2SPaul Blakey return true; 58346475bb2SPaul Blakey } 58446475bb2SPaul Blakey 58546475bb2SPaul Blakey static bool 58646475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 58707ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 58807ac9d16SPaul Blakey struct tcphdr **tcph) 58946475bb2SPaul Blakey { 59046475bb2SPaul Blakey struct flow_ports *ports; 59146475bb2SPaul Blakey struct ipv6hdr *ip6h; 59246475bb2SPaul Blakey unsigned int thoff; 593fcb6aa86SToshiaki Makita size_t hdrsize; 594fcb6aa86SToshiaki Makita u8 nexthdr; 59546475bb2SPaul Blakey 5964cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 59746475bb2SPaul Blakey return false; 59846475bb2SPaul Blakey 59946475bb2SPaul Blakey ip6h = ipv6_hdr(skb); 600fcb6aa86SToshiaki Makita thoff = sizeof(*ip6h); 60146475bb2SPaul Blakey 602fcb6aa86SToshiaki Makita nexthdr = ip6h->nexthdr; 603fcb6aa86SToshiaki Makita switch (nexthdr) { 604fcb6aa86SToshiaki Makita case IPPROTO_TCP: 605fcb6aa86SToshiaki Makita hdrsize = sizeof(struct tcphdr); 606fcb6aa86SToshiaki Makita break; 607fcb6aa86SToshiaki Makita case IPPROTO_UDP: 608fcb6aa86SToshiaki Makita hdrsize = sizeof(*ports); 609fcb6aa86SToshiaki Makita break; 610fcb6aa86SToshiaki Makita #ifdef CONFIG_NF_CT_PROTO_GRE 611fcb6aa86SToshiaki Makita case IPPROTO_GRE: 612fcb6aa86SToshiaki Makita hdrsize = sizeof(struct gre_base_hdr); 613fcb6aa86SToshiaki Makita break; 614fcb6aa86SToshiaki Makita #endif 615fcb6aa86SToshiaki Makita default: 61686360030SDan Carpenter return false; 617fcb6aa86SToshiaki Makita } 61846475bb2SPaul Blakey 61946475bb2SPaul Blakey if (ip6h->hop_limit <= 1) 62046475bb2SPaul Blakey return false; 62146475bb2SPaul Blakey 622fcb6aa86SToshiaki Makita if (!pskb_network_may_pull(skb, thoff + hdrsize)) 62346475bb2SPaul Blakey return false; 62446475bb2SPaul Blakey 625fcb6aa86SToshiaki Makita switch (nexthdr) { 626fcb6aa86SToshiaki Makita case IPPROTO_TCP: 62707ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 628fcb6aa86SToshiaki Makita fallthrough; 629fcb6aa86SToshiaki Makita case IPPROTO_UDP: 63007ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 63146475bb2SPaul Blakey tuple->src_port = ports->source; 63246475bb2SPaul Blakey tuple->dst_port = ports->dest; 633fcb6aa86SToshiaki Makita break; 634fcb6aa86SToshiaki Makita case IPPROTO_GRE: { 635fcb6aa86SToshiaki Makita struct gre_base_hdr *greh; 636fcb6aa86SToshiaki Makita 637fcb6aa86SToshiaki Makita greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 638fcb6aa86SToshiaki Makita if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 639fcb6aa86SToshiaki Makita return false; 640fcb6aa86SToshiaki Makita break; 641fcb6aa86SToshiaki Makita } 642fcb6aa86SToshiaki Makita } 643fcb6aa86SToshiaki Makita 644fcb6aa86SToshiaki Makita ip6h = ipv6_hdr(skb); 645fcb6aa86SToshiaki Makita 646fcb6aa86SToshiaki Makita tuple->src_v6 = ip6h->saddr; 647fcb6aa86SToshiaki Makita tuple->dst_v6 = ip6h->daddr; 64846475bb2SPaul Blakey tuple->l3proto = AF_INET6; 649fcb6aa86SToshiaki Makita tuple->l4proto = nexthdr; 65046475bb2SPaul Blakey 65146475bb2SPaul Blakey return true; 65246475bb2SPaul Blakey } 65346475bb2SPaul Blakey 65446475bb2SPaul Blakey static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 65546475bb2SPaul Blakey struct sk_buff *skb, 65646475bb2SPaul Blakey u8 family) 65746475bb2SPaul Blakey { 65846475bb2SPaul Blakey struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 65946475bb2SPaul Blakey struct flow_offload_tuple_rhash *tuplehash; 66046475bb2SPaul Blakey struct flow_offload_tuple tuple = {}; 66146475bb2SPaul Blakey enum ip_conntrack_info ctinfo; 66207ac9d16SPaul Blakey struct tcphdr *tcph = NULL; 66341f2c7c3SPaul Blakey bool force_refresh = false; 66446475bb2SPaul Blakey struct flow_offload *flow; 66546475bb2SPaul Blakey struct nf_conn *ct; 66646475bb2SPaul Blakey u8 dir; 66746475bb2SPaul Blakey 66846475bb2SPaul Blakey switch (family) { 66946475bb2SPaul Blakey case NFPROTO_IPV4: 67007ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 67146475bb2SPaul Blakey return false; 67246475bb2SPaul Blakey break; 67346475bb2SPaul Blakey case NFPROTO_IPV6: 67407ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 67546475bb2SPaul Blakey return false; 67646475bb2SPaul Blakey break; 67746475bb2SPaul Blakey default: 67846475bb2SPaul Blakey return false; 67946475bb2SPaul Blakey } 68046475bb2SPaul Blakey 68146475bb2SPaul Blakey tuplehash = flow_offload_lookup(nf_ft, &tuple); 68246475bb2SPaul Blakey if (!tuplehash) 68346475bb2SPaul Blakey return false; 68446475bb2SPaul Blakey 68546475bb2SPaul Blakey dir = tuplehash->tuple.dir; 68646475bb2SPaul Blakey flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 68746475bb2SPaul Blakey ct = flow->ct; 68846475bb2SPaul Blakey 6896a9bad00SVlad Buslov if (dir == FLOW_OFFLOAD_DIR_REPLY && 6906a9bad00SVlad Buslov !test_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags)) { 6916a9bad00SVlad Buslov /* Only offload reply direction after connection became 6926a9bad00SVlad Buslov * assured. 6936a9bad00SVlad Buslov */ 6946a9bad00SVlad Buslov if (test_bit(IPS_ASSURED_BIT, &ct->status)) 6956a9bad00SVlad Buslov set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags); 6966a9bad00SVlad Buslov else if (test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags)) 6976a9bad00SVlad Buslov /* If flow_table flow has already been updated to the 6986a9bad00SVlad Buslov * established state, then don't refresh. 6996a9bad00SVlad Buslov */ 7006a9bad00SVlad Buslov return false; 70141f2c7c3SPaul Blakey force_refresh = true; 7026a9bad00SVlad Buslov } 7036a9bad00SVlad Buslov 70407ac9d16SPaul Blakey if (tcph && (unlikely(tcph->fin || tcph->rst))) { 70507ac9d16SPaul Blakey flow_offload_teardown(flow); 70607ac9d16SPaul Blakey return false; 70707ac9d16SPaul Blakey } 70807ac9d16SPaul Blakey 7096a9bad00SVlad Buslov if (dir == FLOW_OFFLOAD_DIR_ORIGINAL) 7106a9bad00SVlad Buslov ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ? 7116a9bad00SVlad Buslov IP_CT_ESTABLISHED : IP_CT_NEW; 7126a9bad00SVlad Buslov else 7136a9bad00SVlad Buslov ctinfo = IP_CT_ESTABLISHED_REPLY; 71446475bb2SPaul Blakey 715d5a116dbSVlad Buslov nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 716d5a116dbSVlad Buslov tcf_ct_flow_ct_ext_ifidx_update(flow); 71741f2c7c3SPaul Blakey flow_offload_refresh(nf_ft, flow, force_refresh); 71841f2c7c3SPaul Blakey if (!test_bit(IPS_ASSURED_BIT, &ct->status)) { 71941f2c7c3SPaul Blakey /* Process this flow in SW to allow promoting to ASSURED */ 72041f2c7c3SPaul Blakey return false; 72141f2c7c3SPaul Blakey } 72241f2c7c3SPaul Blakey 72346475bb2SPaul Blakey nf_conntrack_get(&ct->ct_general); 72446475bb2SPaul Blakey nf_ct_set(skb, ct, ctinfo); 7253567e233SMarcelo Ricardo Leitner if (nf_ft->flags & NF_FLOWTABLE_COUNTER) 726beb97d3aSwenxu nf_ct_acct_update(ct, dir, skb->len); 72746475bb2SPaul Blakey 72846475bb2SPaul Blakey return true; 72946475bb2SPaul Blakey } 73046475bb2SPaul Blakey 731c34b961aSPaul Blakey static int tcf_ct_flow_tables_init(void) 732c34b961aSPaul Blakey { 733c34b961aSPaul Blakey return rhashtable_init(&zones_ht, &zones_params); 734c34b961aSPaul Blakey } 735c34b961aSPaul Blakey 736c34b961aSPaul Blakey static void tcf_ct_flow_tables_uninit(void) 737c34b961aSPaul Blakey { 738c34b961aSPaul Blakey rhashtable_destroy(&zones_ht); 739c34b961aSPaul Blakey } 740c34b961aSPaul Blakey 741b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops; 742b57dc7c1SPaul Blakey 743b57dc7c1SPaul Blakey struct tc_ct_action_net { 744b57dc7c1SPaul Blakey struct tc_action_net tn; /* Must be first */ 745b57dc7c1SPaul Blakey bool labels; 746b57dc7c1SPaul Blakey }; 747b57dc7c1SPaul Blakey 748b57dc7c1SPaul Blakey /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 749b57dc7c1SPaul Blakey static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 750a21b06e7SXin Long struct tcf_ct_params *p) 751b57dc7c1SPaul Blakey { 752b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 753b57dc7c1SPaul Blakey struct nf_conn *ct; 754b57dc7c1SPaul Blakey 755b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 756b57dc7c1SPaul Blakey if (!ct) 757b57dc7c1SPaul Blakey return false; 758b57dc7c1SPaul Blakey if (!net_eq(net, read_pnet(&ct->ct_net))) 759bcb74e13SMarcelo Ricardo Leitner goto drop_ct; 760a21b06e7SXin Long if (nf_ct_zone(ct)->id != p->zone) 761bcb74e13SMarcelo Ricardo Leitner goto drop_ct; 762a21b06e7SXin Long if (p->helper) { 763a21b06e7SXin Long struct nf_conn_help *help; 764a21b06e7SXin Long 765a21b06e7SXin Long help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); 766a21b06e7SXin Long if (help && rcu_access_pointer(help->helper) != p->helper) 767a21b06e7SXin Long goto drop_ct; 768a21b06e7SXin Long } 769b57dc7c1SPaul Blakey 770b57dc7c1SPaul Blakey /* Force conntrack entry direction. */ 771a21b06e7SXin Long if ((p->ct_action & TCA_CT_ACT_FORCE) && 772a21b06e7SXin Long CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 773b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 774b57dc7c1SPaul Blakey nf_ct_kill(ct); 775b57dc7c1SPaul Blakey 776bcb74e13SMarcelo Ricardo Leitner goto drop_ct; 777bcb74e13SMarcelo Ricardo Leitner } 778bcb74e13SMarcelo Ricardo Leitner 779bcb74e13SMarcelo Ricardo Leitner return true; 780bcb74e13SMarcelo Ricardo Leitner 781bcb74e13SMarcelo Ricardo Leitner drop_ct: 782408bdcfcSFlorian Westphal nf_ct_put(ct); 783b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 784b57dc7c1SPaul Blakey 785b57dc7c1SPaul Blakey return false; 786b57dc7c1SPaul Blakey } 787b57dc7c1SPaul Blakey 788b57dc7c1SPaul Blakey static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 789b57dc7c1SPaul Blakey { 790b57dc7c1SPaul Blakey u8 family = NFPROTO_UNSPEC; 791b57dc7c1SPaul Blakey 792d7bf2ebeSToke Høiland-Jørgensen switch (skb_protocol(skb, true)) { 793b57dc7c1SPaul Blakey case htons(ETH_P_IP): 794b57dc7c1SPaul Blakey family = NFPROTO_IPV4; 795b57dc7c1SPaul Blakey break; 796b57dc7c1SPaul Blakey case htons(ETH_P_IPV6): 797b57dc7c1SPaul Blakey family = NFPROTO_IPV6; 798b57dc7c1SPaul Blakey break; 799b57dc7c1SPaul Blakey default: 800b57dc7c1SPaul Blakey break; 801b57dc7c1SPaul Blakey } 802b57dc7c1SPaul Blakey 803b57dc7c1SPaul Blakey return family; 804b57dc7c1SPaul Blakey } 805b57dc7c1SPaul Blakey 806b57dc7c1SPaul Blakey static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 807b57dc7c1SPaul Blakey { 808b57dc7c1SPaul Blakey unsigned int len; 809b57dc7c1SPaul Blakey 810b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct iphdr); 811b57dc7c1SPaul Blakey if (unlikely(skb->len < len)) 812b57dc7c1SPaul Blakey return -EINVAL; 813b57dc7c1SPaul Blakey if (unlikely(!pskb_may_pull(skb, len))) 814b57dc7c1SPaul Blakey return -ENOMEM; 815b57dc7c1SPaul Blakey 816b57dc7c1SPaul Blakey *frag = ip_is_fragment(ip_hdr(skb)); 817b57dc7c1SPaul Blakey return 0; 818b57dc7c1SPaul Blakey } 819b57dc7c1SPaul Blakey 820b57dc7c1SPaul Blakey static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 821b57dc7c1SPaul Blakey { 822b57dc7c1SPaul Blakey unsigned int flags = 0, len, payload_ofs = 0; 823b57dc7c1SPaul Blakey unsigned short frag_off; 824b57dc7c1SPaul Blakey int nexthdr; 825b57dc7c1SPaul Blakey 826b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 827b57dc7c1SPaul Blakey if (unlikely(skb->len < len)) 828b57dc7c1SPaul Blakey return -EINVAL; 829b57dc7c1SPaul Blakey if (unlikely(!pskb_may_pull(skb, len))) 830b57dc7c1SPaul Blakey return -ENOMEM; 831b57dc7c1SPaul Blakey 832b57dc7c1SPaul Blakey nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 833b57dc7c1SPaul Blakey if (unlikely(nexthdr < 0)) 834b57dc7c1SPaul Blakey return -EPROTO; 835b57dc7c1SPaul Blakey 836b57dc7c1SPaul Blakey *frag = flags & IP6_FH_F_FRAG; 837b57dc7c1SPaul Blakey return 0; 838b57dc7c1SPaul Blakey } 839b57dc7c1SPaul Blakey 840b57dc7c1SPaul Blakey static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 841ae372cb1Swenxu u8 family, u16 zone, bool *defrag) 842b57dc7c1SPaul Blakey { 843b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 844b57dc7c1SPaul Blakey struct nf_conn *ct; 845b57dc7c1SPaul Blakey int err = 0; 846b57dc7c1SPaul Blakey bool frag; 8470785407eSXin Long u8 proto; 848ec624fe7SPaul Blakey u16 mru; 849b57dc7c1SPaul Blakey 850b57dc7c1SPaul Blakey /* Previously seen (loopback)? Ignore. */ 851b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 852b57dc7c1SPaul Blakey if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 853b57dc7c1SPaul Blakey return 0; 854b57dc7c1SPaul Blakey 855b57dc7c1SPaul Blakey if (family == NFPROTO_IPV4) 856b57dc7c1SPaul Blakey err = tcf_ct_ipv4_is_fragment(skb, &frag); 857b57dc7c1SPaul Blakey else 858b57dc7c1SPaul Blakey err = tcf_ct_ipv6_is_fragment(skb, &frag); 859b57dc7c1SPaul Blakey if (err || !frag) 860b57dc7c1SPaul Blakey return err; 861b57dc7c1SPaul Blakey 8620785407eSXin Long err = nf_ct_handle_fragments(net, skb, zone, family, &proto, &mru); 863558d95e7SXin Long if (err) 864eda814b9SAlaa Hleihel return err; 865ae372cb1Swenxu 866ae372cb1Swenxu *defrag = true; 867ec624fe7SPaul Blakey tc_skb_cb(skb)->mru = mru; 868b57dc7c1SPaul Blakey 869558d95e7SXin Long return 0; 870b57dc7c1SPaul Blakey } 871b57dc7c1SPaul Blakey 87219138941SXin Long static void tcf_ct_params_free(struct tcf_ct_params *params) 873b57dc7c1SPaul Blakey { 874a21b06e7SXin Long if (params->helper) { 875a21b06e7SXin Long #if IS_ENABLED(CONFIG_NF_NAT) 876a21b06e7SXin Long if (params->ct_action & TCA_CT_ACT_NAT) 877a21b06e7SXin Long nf_nat_helper_put(params->helper); 878a21b06e7SXin Long #endif 879a21b06e7SXin Long nf_conntrack_helper_put(params->helper); 880a21b06e7SXin Long } 88119138941SXin Long if (params->ct_ft) 88219138941SXin Long tcf_ct_flow_table_put(params->ct_ft); 883b57dc7c1SPaul Blakey if (params->tmpl) 884408bdcfcSFlorian Westphal nf_ct_put(params->tmpl); 885b57dc7c1SPaul Blakey kfree(params); 886b57dc7c1SPaul Blakey } 887b57dc7c1SPaul Blakey 88819138941SXin Long static void tcf_ct_params_free_rcu(struct rcu_head *head) 88919138941SXin Long { 89019138941SXin Long struct tcf_ct_params *params; 89119138941SXin Long 89219138941SXin Long params = container_of(head, struct tcf_ct_params, rcu); 89319138941SXin Long tcf_ct_params_free(params); 89419138941SXin Long } 89519138941SXin Long 896b57dc7c1SPaul Blakey static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 897b57dc7c1SPaul Blakey { 898b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 899b57dc7c1SPaul Blakey u32 new_mark; 900b57dc7c1SPaul Blakey 901b57dc7c1SPaul Blakey if (!mask) 902b57dc7c1SPaul Blakey return; 903b57dc7c1SPaul Blakey 90452d1aa8bSDaniel Xu new_mark = mark | (READ_ONCE(ct->mark) & ~(mask)); 90552d1aa8bSDaniel Xu if (READ_ONCE(ct->mark) != new_mark) { 90652d1aa8bSDaniel Xu WRITE_ONCE(ct->mark, new_mark); 907b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 908b57dc7c1SPaul Blakey nf_conntrack_event_cache(IPCT_MARK, ct); 909b57dc7c1SPaul Blakey } 910b57dc7c1SPaul Blakey #endif 911b57dc7c1SPaul Blakey } 912b57dc7c1SPaul Blakey 913b57dc7c1SPaul Blakey static void tcf_ct_act_set_labels(struct nf_conn *ct, 914b57dc7c1SPaul Blakey u32 *labels, 915b57dc7c1SPaul Blakey u32 *labels_m) 916b57dc7c1SPaul Blakey { 917b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 918c593642cSPankaj Bharadiya size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 919b57dc7c1SPaul Blakey 920b57dc7c1SPaul Blakey if (!memchr_inv(labels_m, 0, labels_sz)) 921b57dc7c1SPaul Blakey return; 922b57dc7c1SPaul Blakey 923b57dc7c1SPaul Blakey nf_connlabels_replace(ct, labels, labels_m, 4); 924b57dc7c1SPaul Blakey #endif 925b57dc7c1SPaul Blakey } 926b57dc7c1SPaul Blakey 927b57dc7c1SPaul Blakey static int tcf_ct_act_nat(struct sk_buff *skb, 928b57dc7c1SPaul Blakey struct nf_conn *ct, 929b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo, 930b57dc7c1SPaul Blakey int ct_action, 931b57dc7c1SPaul Blakey struct nf_nat_range2 *range, 932b57dc7c1SPaul Blakey bool commit) 933b57dc7c1SPaul Blakey { 934b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT) 935ebddb140SXin Long int err, action = 0; 936b57dc7c1SPaul Blakey 937b57dc7c1SPaul Blakey if (!(ct_action & TCA_CT_ACT_NAT)) 938b57dc7c1SPaul Blakey return NF_ACCEPT; 939ebddb140SXin Long if (ct_action & TCA_CT_ACT_NAT_SRC) 940ebddb140SXin Long action |= BIT(NF_NAT_MANIP_SRC); 941ebddb140SXin Long if (ct_action & TCA_CT_ACT_NAT_DST) 942ebddb140SXin Long action |= BIT(NF_NAT_MANIP_DST); 943b57dc7c1SPaul Blakey 944ebddb140SXin Long err = nf_ct_nat(skb, ct, ctinfo, &action, range, commit); 945b57dc7c1SPaul Blakey 946ebddb140SXin Long if (action & BIT(NF_NAT_MANIP_SRC)) 947ebddb140SXin Long tc_skb_cb(skb)->post_ct_snat = 1; 948ebddb140SXin Long if (action & BIT(NF_NAT_MANIP_DST)) 949ebddb140SXin Long tc_skb_cb(skb)->post_ct_dnat = 1; 950b57dc7c1SPaul Blakey 95195219afbSAaron Conole return err; 952b57dc7c1SPaul Blakey #else 953b57dc7c1SPaul Blakey return NF_ACCEPT; 954b57dc7c1SPaul Blakey #endif 955b57dc7c1SPaul Blakey } 956b57dc7c1SPaul Blakey 957871cf386SPedro Tammela TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 958b57dc7c1SPaul Blakey struct tcf_result *res) 959b57dc7c1SPaul Blakey { 960b57dc7c1SPaul Blakey struct net *net = dev_net(skb->dev); 961b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 962b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 963b57dc7c1SPaul Blakey struct nf_conn *tmpl = NULL; 964b57dc7c1SPaul Blakey struct nf_hook_state state; 965a21b06e7SXin Long bool cached, commit, clear; 966b57dc7c1SPaul Blakey int nh_ofs, err, retval; 967b57dc7c1SPaul Blakey struct tcf_ct_params *p; 968a21b06e7SXin Long bool add_helper = false; 96946475bb2SPaul Blakey bool skip_add = false; 970ae372cb1Swenxu bool defrag = false; 971b57dc7c1SPaul Blakey struct nf_conn *ct; 972b57dc7c1SPaul Blakey u8 family; 973b57dc7c1SPaul Blakey 974b57dc7c1SPaul Blakey p = rcu_dereference_bh(c->params); 975b57dc7c1SPaul Blakey 976b57dc7c1SPaul Blakey retval = READ_ONCE(c->tcf_action); 977b57dc7c1SPaul Blakey commit = p->ct_action & TCA_CT_ACT_COMMIT; 978b57dc7c1SPaul Blakey clear = p->ct_action & TCA_CT_ACT_CLEAR; 979b57dc7c1SPaul Blakey tmpl = p->tmpl; 980b57dc7c1SPaul Blakey 9818367b3abSwenxu tcf_lastuse_update(&c->tcf_tm); 9822dc4e9e8SPaul Blakey tcf_action_update_bstats(&c->common, skb); 9838367b3abSwenxu 984b57dc7c1SPaul Blakey if (clear) { 985ec624fe7SPaul Blakey tc_skb_cb(skb)->post_ct = false; 986b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 987b57dc7c1SPaul Blakey if (ct) { 988408bdcfcSFlorian Westphal nf_ct_put(ct); 989b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 990b57dc7c1SPaul Blakey } 991b57dc7c1SPaul Blakey 9928ca1b090SMarcelo Ricardo Leitner goto out_clear; 993b57dc7c1SPaul Blakey } 994b57dc7c1SPaul Blakey 995b57dc7c1SPaul Blakey family = tcf_ct_skb_nf_family(skb); 996b57dc7c1SPaul Blakey if (family == NFPROTO_UNSPEC) 997b57dc7c1SPaul Blakey goto drop; 998b57dc7c1SPaul Blakey 999b57dc7c1SPaul Blakey /* The conntrack module expects to be working at L3. 1000b57dc7c1SPaul Blakey * We also try to pull the IPv4/6 header to linear area 1001b57dc7c1SPaul Blakey */ 1002b57dc7c1SPaul Blakey nh_ofs = skb_network_offset(skb); 1003b57dc7c1SPaul Blakey skb_pull_rcsum(skb, nh_ofs); 1004ae372cb1Swenxu err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag); 1005b57dc7c1SPaul Blakey if (err) 100673f7da5fSTao Liu goto out_frag; 1007b57dc7c1SPaul Blakey 100867fc5d7fSXin Long err = nf_ct_skb_network_trim(skb, family); 1009b57dc7c1SPaul Blakey if (err) 1010b57dc7c1SPaul Blakey goto drop; 1011b57dc7c1SPaul Blakey 1012b57dc7c1SPaul Blakey /* If we are recirculating packets to match on ct fields and 1013b57dc7c1SPaul Blakey * committing with a separate ct action, then we don't need to 1014b57dc7c1SPaul Blakey * actually run the packet through conntrack twice unless it's for a 1015b57dc7c1SPaul Blakey * different zone. 1016b57dc7c1SPaul Blakey */ 1017a21b06e7SXin Long cached = tcf_ct_skb_nfct_cached(net, skb, p); 1018b57dc7c1SPaul Blakey if (!cached) { 10190cc254e5SPaul Blakey if (tcf_ct_flow_table_lookup(p, skb, family)) { 102046475bb2SPaul Blakey skip_add = true; 102146475bb2SPaul Blakey goto do_nat; 102246475bb2SPaul Blakey } 102346475bb2SPaul Blakey 1024b57dc7c1SPaul Blakey /* Associate skb with specified zone. */ 1025b57dc7c1SPaul Blakey if (tmpl) { 1026b57dc7c1SPaul Blakey nf_conntrack_put(skb_nfct(skb)); 1027b57dc7c1SPaul Blakey nf_conntrack_get(&tmpl->ct_general); 1028b57dc7c1SPaul Blakey nf_ct_set(skb, tmpl, IP_CT_NEW); 1029b57dc7c1SPaul Blakey } 1030b57dc7c1SPaul Blakey 1031b57dc7c1SPaul Blakey state.hook = NF_INET_PRE_ROUTING; 1032b57dc7c1SPaul Blakey state.net = net; 1033b57dc7c1SPaul Blakey state.pf = family; 1034b57dc7c1SPaul Blakey err = nf_conntrack_in(skb, &state); 1035b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 1036b57dc7c1SPaul Blakey goto out_push; 1037b57dc7c1SPaul Blakey } 1038b57dc7c1SPaul Blakey 103946475bb2SPaul Blakey do_nat: 1040b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 1041b57dc7c1SPaul Blakey if (!ct) 1042b57dc7c1SPaul Blakey goto out_push; 1043b57dc7c1SPaul Blakey nf_ct_deliver_cached_events(ct); 10449795ded7SPaul Blakey nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 1045b57dc7c1SPaul Blakey 1046b57dc7c1SPaul Blakey err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 1047b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 1048b57dc7c1SPaul Blakey goto drop; 1049b57dc7c1SPaul Blakey 1050a21b06e7SXin Long if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) { 1051a21b06e7SXin Long err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC); 1052a21b06e7SXin Long if (err) 1053a21b06e7SXin Long goto drop; 1054a21b06e7SXin Long add_helper = true; 1055a21b06e7SXin Long if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) { 1056a21b06e7SXin Long if (!nfct_seqadj_ext_add(ct)) 1057a21b06e7SXin Long goto drop; 1058a21b06e7SXin Long } 1059a21b06e7SXin Long } 1060a21b06e7SXin Long 1061a21b06e7SXin Long if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) { 1062a21b06e7SXin Long if (nf_ct_helper(skb, ct, ctinfo, family) != NF_ACCEPT) 1063a21b06e7SXin Long goto drop; 1064a21b06e7SXin Long } 1065a21b06e7SXin Long 1066b57dc7c1SPaul Blakey if (commit) { 1067b57dc7c1SPaul Blakey tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 1068b57dc7c1SPaul Blakey tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 1069b57dc7c1SPaul Blakey 10709795ded7SPaul Blakey if (!nf_ct_is_confirmed(ct)) 1071d5a116dbSVlad Buslov nf_conn_act_ct_ext_add(skb, ct, ctinfo); 10729795ded7SPaul Blakey 1073b57dc7c1SPaul Blakey /* This will take care of sending queued events 1074b57dc7c1SPaul Blakey * even if the connection is already confirmed. 1075b57dc7c1SPaul Blakey */ 10768955b90cSwenxu if (nf_conntrack_confirm(skb) != NF_ACCEPT) 10778955b90cSwenxu goto drop; 1078799a3490SChengen Du 1079799a3490SChengen Du /* The ct may be dropped if a clash has been resolved, 1080799a3490SChengen Du * so it's necessary to retrieve it from skb again to 1081799a3490SChengen Du * prevent UAF. 1082799a3490SChengen Du */ 1083799a3490SChengen Du ct = nf_ct_get(skb, &ctinfo); 1084799a3490SChengen Du if (!ct) 1085799a3490SChengen Du skip_add = true; 108646475bb2SPaul Blakey } 108764ff70b8SPaul Blakey 10880cc254e5SPaul Blakey if (!skip_add) 10890cc254e5SPaul Blakey tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 10900cc254e5SPaul Blakey 1091b57dc7c1SPaul Blakey out_push: 1092b57dc7c1SPaul Blakey skb_push_rcsum(skb, nh_ofs); 1093b57dc7c1SPaul Blakey 1094ec624fe7SPaul Blakey tc_skb_cb(skb)->post_ct = true; 109538495958SPaul Blakey tc_skb_cb(skb)->zone = p->zone; 10968ca1b090SMarcelo Ricardo Leitner out_clear: 1097ae372cb1Swenxu if (defrag) 1098ae372cb1Swenxu qdisc_skb_cb(skb)->pkt_len = skb->len; 1099b57dc7c1SPaul Blakey return retval; 1100b57dc7c1SPaul Blakey 110173f7da5fSTao Liu out_frag: 110273f7da5fSTao Liu if (err != -EINPROGRESS) 110373f7da5fSTao Liu tcf_action_inc_drop_qstats(&c->common); 110473f7da5fSTao Liu return TC_ACT_CONSUMED; 110573f7da5fSTao Liu 1106b57dc7c1SPaul Blakey drop: 110726b537a8SVlad Buslov tcf_action_inc_drop_qstats(&c->common); 1108b57dc7c1SPaul Blakey return TC_ACT_SHOT; 1109b57dc7c1SPaul Blakey } 1110b57dc7c1SPaul Blakey 1111b57dc7c1SPaul Blakey static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1112b57dc7c1SPaul Blakey [TCA_CT_ACTION] = { .type = NLA_U16 }, 11138140860cSJohannes Berg [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)), 1114b57dc7c1SPaul Blakey [TCA_CT_ZONE] = { .type = NLA_U16 }, 1115b57dc7c1SPaul Blakey [TCA_CT_MARK] = { .type = NLA_U32 }, 1116b57dc7c1SPaul Blakey [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1117b57dc7c1SPaul Blakey [TCA_CT_LABELS] = { .type = NLA_BINARY, 1118b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1119b57dc7c1SPaul Blakey [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1120b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1121b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1122b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 11238140860cSJohannes Berg [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 11248140860cSJohannes Berg [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1125b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1126b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1127a21b06e7SXin Long [TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN }, 1128a21b06e7SXin Long [TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 }, 1129a21b06e7SXin Long [TCA_CT_HELPER_PROTO] = { .type = NLA_U8 }, 1130b57dc7c1SPaul Blakey }; 1131b57dc7c1SPaul Blakey 1132b57dc7c1SPaul Blakey static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1133b57dc7c1SPaul Blakey struct tc_ct *parm, 1134b57dc7c1SPaul Blakey struct nlattr **tb, 1135b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1136b57dc7c1SPaul Blakey { 1137b57dc7c1SPaul Blakey struct nf_nat_range2 *range; 1138b57dc7c1SPaul Blakey 1139b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1140b57dc7c1SPaul Blakey return 0; 1141b57dc7c1SPaul Blakey 1142b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_NAT)) { 1143b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1144b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1145b57dc7c1SPaul Blakey } 1146b57dc7c1SPaul Blakey 1147b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1148b57dc7c1SPaul Blakey return 0; 1149b57dc7c1SPaul Blakey 1150b57dc7c1SPaul Blakey if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1151b57dc7c1SPaul Blakey (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1152b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1153b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1154b57dc7c1SPaul Blakey } 1155b57dc7c1SPaul Blakey 1156b57dc7c1SPaul Blakey range = &p->range; 1157b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_IPV4_MIN]) { 1158b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1159b57dc7c1SPaul Blakey 1160b57dc7c1SPaul Blakey p->ipv4_range = true; 1161b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1162b57dc7c1SPaul Blakey range->min_addr.ip = 1163b57dc7c1SPaul Blakey nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1164b57dc7c1SPaul Blakey 1165b57dc7c1SPaul Blakey range->max_addr.ip = max_attr ? 1166b57dc7c1SPaul Blakey nla_get_in_addr(max_attr) : 1167b57dc7c1SPaul Blakey range->min_addr.ip; 1168b57dc7c1SPaul Blakey } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1169b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1170b57dc7c1SPaul Blakey 1171b57dc7c1SPaul Blakey p->ipv4_range = false; 1172b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1173b57dc7c1SPaul Blakey range->min_addr.in6 = 1174b57dc7c1SPaul Blakey nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1175b57dc7c1SPaul Blakey 1176b57dc7c1SPaul Blakey range->max_addr.in6 = max_attr ? 1177b57dc7c1SPaul Blakey nla_get_in6_addr(max_attr) : 1178b57dc7c1SPaul Blakey range->min_addr.in6; 1179b57dc7c1SPaul Blakey } 1180b57dc7c1SPaul Blakey 1181b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_PORT_MIN]) { 1182b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1183b57dc7c1SPaul Blakey range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1184b57dc7c1SPaul Blakey 1185b57dc7c1SPaul Blakey range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1186b57dc7c1SPaul Blakey nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1187b57dc7c1SPaul Blakey range->min_proto.all; 1188b57dc7c1SPaul Blakey } 1189b57dc7c1SPaul Blakey 1190b57dc7c1SPaul Blakey return 0; 1191b57dc7c1SPaul Blakey } 1192b57dc7c1SPaul Blakey 1193b57dc7c1SPaul Blakey static void tcf_ct_set_key_val(struct nlattr **tb, 1194b57dc7c1SPaul Blakey void *val, int val_type, 1195b57dc7c1SPaul Blakey void *mask, int mask_type, 1196b57dc7c1SPaul Blakey int len) 1197b57dc7c1SPaul Blakey { 1198b57dc7c1SPaul Blakey if (!tb[val_type]) 1199b57dc7c1SPaul Blakey return; 1200b57dc7c1SPaul Blakey nla_memcpy(val, tb[val_type], len); 1201b57dc7c1SPaul Blakey 1202b57dc7c1SPaul Blakey if (!mask) 1203b57dc7c1SPaul Blakey return; 1204b57dc7c1SPaul Blakey 1205b57dc7c1SPaul Blakey if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1206b57dc7c1SPaul Blakey memset(mask, 0xff, len); 1207b57dc7c1SPaul Blakey else 1208b57dc7c1SPaul Blakey nla_memcpy(mask, tb[mask_type], len); 1209b57dc7c1SPaul Blakey } 1210b57dc7c1SPaul Blakey 1211b57dc7c1SPaul Blakey static int tcf_ct_fill_params(struct net *net, 1212b57dc7c1SPaul Blakey struct tcf_ct_params *p, 1213b57dc7c1SPaul Blakey struct tc_ct *parm, 1214b57dc7c1SPaul Blakey struct nlattr **tb, 1215b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1216b57dc7c1SPaul Blakey { 1217acd0a7abSZhengchao Shao struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1218b57dc7c1SPaul Blakey struct nf_conntrack_zone zone; 1219a21b06e7SXin Long int err, family, proto, len; 1220b57dc7c1SPaul Blakey struct nf_conn *tmpl; 1221a21b06e7SXin Long char *name; 1222b57dc7c1SPaul Blakey 1223b57dc7c1SPaul Blakey p->zone = NF_CT_DEFAULT_ZONE_ID; 1224b57dc7c1SPaul Blakey 1225b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1226b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1227b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1228b57dc7c1SPaul Blakey sizeof(p->ct_action)); 1229b57dc7c1SPaul Blakey 1230b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1231b57dc7c1SPaul Blakey return 0; 1232b57dc7c1SPaul Blakey 1233b57dc7c1SPaul Blakey err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1234b57dc7c1SPaul Blakey if (err) 1235b57dc7c1SPaul Blakey return err; 1236b57dc7c1SPaul Blakey 1237b57dc7c1SPaul Blakey if (tb[TCA_CT_MARK]) { 1238b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1239b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1240b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1241b57dc7c1SPaul Blakey } 1242b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1243b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1244b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1245b57dc7c1SPaul Blakey sizeof(p->mark)); 1246b57dc7c1SPaul Blakey } 1247b57dc7c1SPaul Blakey 1248b57dc7c1SPaul Blakey if (tb[TCA_CT_LABELS]) { 1249b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1250b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1251b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1252b57dc7c1SPaul Blakey } 1253b57dc7c1SPaul Blakey 1254b57dc7c1SPaul Blakey if (!tn->labels) { 1255b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1256b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1257b57dc7c1SPaul Blakey } 1258b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1259b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1260b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1261b57dc7c1SPaul Blakey sizeof(p->labels)); 1262b57dc7c1SPaul Blakey } 1263b57dc7c1SPaul Blakey 1264b57dc7c1SPaul Blakey if (tb[TCA_CT_ZONE]) { 1265b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1266b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1267b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1268b57dc7c1SPaul Blakey } 1269b57dc7c1SPaul Blakey 1270b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1271b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1272b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1273b57dc7c1SPaul Blakey sizeof(p->zone)); 1274b57dc7c1SPaul Blakey } 1275b57dc7c1SPaul Blakey 1276b57dc7c1SPaul Blakey nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1277b57dc7c1SPaul Blakey tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1278b57dc7c1SPaul Blakey if (!tmpl) { 1279b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1280b57dc7c1SPaul Blakey return -ENOMEM; 1281b57dc7c1SPaul Blakey } 1282b57dc7c1SPaul Blakey p->tmpl = tmpl; 1283a21b06e7SXin Long if (tb[TCA_CT_HELPER_NAME]) { 1284a21b06e7SXin Long name = nla_data(tb[TCA_CT_HELPER_NAME]); 1285a21b06e7SXin Long len = nla_len(tb[TCA_CT_HELPER_NAME]); 1286a21b06e7SXin Long if (len > 16 || name[len - 1] != '\0') { 1287a21b06e7SXin Long NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name."); 1288a21b06e7SXin Long err = -EINVAL; 1289a21b06e7SXin Long goto err; 1290a21b06e7SXin Long } 1291a21b06e7SXin Long family = tb[TCA_CT_HELPER_FAMILY] ? nla_get_u8(tb[TCA_CT_HELPER_FAMILY]) : AF_INET; 1292a21b06e7SXin Long proto = tb[TCA_CT_HELPER_PROTO] ? nla_get_u8(tb[TCA_CT_HELPER_PROTO]) : IPPROTO_TCP; 1293a21b06e7SXin Long err = nf_ct_add_helper(tmpl, name, family, proto, 1294a21b06e7SXin Long p->ct_action & TCA_CT_ACT_NAT, &p->helper); 1295a21b06e7SXin Long if (err) { 1296a21b06e7SXin Long NL_SET_ERR_MSG_MOD(extack, "Failed to add helper"); 1297a21b06e7SXin Long goto err; 1298a21b06e7SXin Long } 1299a21b06e7SXin Long } 1300b57dc7c1SPaul Blakey 130176622cedSXin Long if (p->ct_action & TCA_CT_ACT_COMMIT) 1302a21b06e7SXin Long __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1303b57dc7c1SPaul Blakey return 0; 1304a21b06e7SXin Long err: 1305a21b06e7SXin Long nf_ct_put(p->tmpl); 1306a21b06e7SXin Long p->tmpl = NULL; 1307a21b06e7SXin Long return err; 1308b57dc7c1SPaul Blakey } 1309b57dc7c1SPaul Blakey 1310b57dc7c1SPaul Blakey static int tcf_ct_init(struct net *net, struct nlattr *nla, 1311b57dc7c1SPaul Blakey struct nlattr *est, struct tc_action **a, 1312abbb0d33SVlad Buslov struct tcf_proto *tp, u32 flags, 1313b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1314b57dc7c1SPaul Blakey { 1315acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id); 1316695176bfSCong Wang bool bind = flags & TCA_ACT_FLAGS_BIND; 1317b57dc7c1SPaul Blakey struct tcf_ct_params *params = NULL; 1318b57dc7c1SPaul Blakey struct nlattr *tb[TCA_CT_MAX + 1]; 1319b57dc7c1SPaul Blakey struct tcf_chain *goto_ch = NULL; 1320b57dc7c1SPaul Blakey struct tc_ct *parm; 1321b57dc7c1SPaul Blakey struct tcf_ct *c; 1322b57dc7c1SPaul Blakey int err, res = 0; 13237be8ef2cSDmytro Linkin u32 index; 1324b57dc7c1SPaul Blakey 1325b57dc7c1SPaul Blakey if (!nla) { 1326b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1327b57dc7c1SPaul Blakey return -EINVAL; 1328b57dc7c1SPaul Blakey } 1329b57dc7c1SPaul Blakey 1330b57dc7c1SPaul Blakey err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1331b57dc7c1SPaul Blakey if (err < 0) 1332b57dc7c1SPaul Blakey return err; 1333b57dc7c1SPaul Blakey 1334b57dc7c1SPaul Blakey if (!tb[TCA_CT_PARMS]) { 1335b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1336b57dc7c1SPaul Blakey return -EINVAL; 1337b57dc7c1SPaul Blakey } 1338b57dc7c1SPaul Blakey parm = nla_data(tb[TCA_CT_PARMS]); 13397be8ef2cSDmytro Linkin index = parm->index; 13407be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind); 1341b57dc7c1SPaul Blakey if (err < 0) 1342b57dc7c1SPaul Blakey return err; 1343b57dc7c1SPaul Blakey 1344b57dc7c1SPaul Blakey if (!err) { 1345e3822678SVlad Buslov err = tcf_idr_create_from_flags(tn, index, est, a, 1346e3822678SVlad Buslov &act_ct_ops, bind, flags); 1347b57dc7c1SPaul Blakey if (err) { 13487be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 1349b57dc7c1SPaul Blakey return err; 1350b57dc7c1SPaul Blakey } 1351b57dc7c1SPaul Blakey res = ACT_P_CREATED; 1352b57dc7c1SPaul Blakey } else { 1353b57dc7c1SPaul Blakey if (bind) 1354b57dc7c1SPaul Blakey return 0; 1355b57dc7c1SPaul Blakey 1356695176bfSCong Wang if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 1357b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1358b57dc7c1SPaul Blakey return -EEXIST; 1359b57dc7c1SPaul Blakey } 1360b57dc7c1SPaul Blakey } 1361b57dc7c1SPaul Blakey err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1362b57dc7c1SPaul Blakey if (err < 0) 1363b57dc7c1SPaul Blakey goto cleanup; 1364b57dc7c1SPaul Blakey 1365b57dc7c1SPaul Blakey c = to_ct(*a); 1366b57dc7c1SPaul Blakey 1367b57dc7c1SPaul Blakey params = kzalloc(sizeof(*params), GFP_KERNEL); 1368b57dc7c1SPaul Blakey if (unlikely(!params)) { 1369b57dc7c1SPaul Blakey err = -ENOMEM; 1370b57dc7c1SPaul Blakey goto cleanup; 1371b57dc7c1SPaul Blakey } 1372b57dc7c1SPaul Blakey 1373b57dc7c1SPaul Blakey err = tcf_ct_fill_params(net, params, parm, tb, extack); 1374b57dc7c1SPaul Blakey if (err) 1375b57dc7c1SPaul Blakey goto cleanup; 1376b57dc7c1SPaul Blakey 1377fc54d906SVlad Buslov err = tcf_ct_flow_table_get(net, params); 1378c34b961aSPaul Blakey if (err) 137919138941SXin Long goto cleanup; 1380c34b961aSPaul Blakey 1381b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1382b57dc7c1SPaul Blakey goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1383445d3749SPaul E. McKenney params = rcu_replace_pointer(c->params, params, 1384445d3749SPaul E. McKenney lockdep_is_held(&c->tcf_lock)); 1385b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1386b57dc7c1SPaul Blakey 1387b57dc7c1SPaul Blakey if (goto_ch) 1388b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 1389b57dc7c1SPaul Blakey if (params) 139019138941SXin Long call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1391b57dc7c1SPaul Blakey 1392b57dc7c1SPaul Blakey return res; 1393b57dc7c1SPaul Blakey 1394b57dc7c1SPaul Blakey cleanup: 1395b57dc7c1SPaul Blakey if (goto_ch) 1396b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 139719138941SXin Long if (params) 139819138941SXin Long tcf_ct_params_free(params); 1399b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1400b57dc7c1SPaul Blakey return err; 1401b57dc7c1SPaul Blakey } 1402b57dc7c1SPaul Blakey 1403b57dc7c1SPaul Blakey static void tcf_ct_cleanup(struct tc_action *a) 1404b57dc7c1SPaul Blakey { 1405b57dc7c1SPaul Blakey struct tcf_ct_params *params; 1406b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1407b57dc7c1SPaul Blakey 1408b57dc7c1SPaul Blakey params = rcu_dereference_protected(c->params, 1); 1409b57dc7c1SPaul Blakey if (params) 141019138941SXin Long call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1411b57dc7c1SPaul Blakey } 1412b57dc7c1SPaul Blakey 1413b57dc7c1SPaul Blakey static int tcf_ct_dump_key_val(struct sk_buff *skb, 1414b57dc7c1SPaul Blakey void *val, int val_type, 1415b57dc7c1SPaul Blakey void *mask, int mask_type, 1416b57dc7c1SPaul Blakey int len) 1417b57dc7c1SPaul Blakey { 1418b57dc7c1SPaul Blakey int err; 1419b57dc7c1SPaul Blakey 1420b57dc7c1SPaul Blakey if (mask && !memchr_inv(mask, 0, len)) 1421b57dc7c1SPaul Blakey return 0; 1422b57dc7c1SPaul Blakey 1423b57dc7c1SPaul Blakey err = nla_put(skb, val_type, len, val); 1424b57dc7c1SPaul Blakey if (err) 1425b57dc7c1SPaul Blakey return err; 1426b57dc7c1SPaul Blakey 1427b57dc7c1SPaul Blakey if (mask_type != TCA_CT_UNSPEC) { 1428b57dc7c1SPaul Blakey err = nla_put(skb, mask_type, len, mask); 1429b57dc7c1SPaul Blakey if (err) 1430b57dc7c1SPaul Blakey return err; 1431b57dc7c1SPaul Blakey } 1432b57dc7c1SPaul Blakey 1433b57dc7c1SPaul Blakey return 0; 1434b57dc7c1SPaul Blakey } 1435b57dc7c1SPaul Blakey 1436b57dc7c1SPaul Blakey static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1437b57dc7c1SPaul Blakey { 1438b57dc7c1SPaul Blakey struct nf_nat_range2 *range = &p->range; 1439b57dc7c1SPaul Blakey 1440b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1441b57dc7c1SPaul Blakey return 0; 1442b57dc7c1SPaul Blakey 1443b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1444b57dc7c1SPaul Blakey return 0; 1445b57dc7c1SPaul Blakey 1446b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1447b57dc7c1SPaul Blakey if (p->ipv4_range) { 1448b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1449b57dc7c1SPaul Blakey range->min_addr.ip)) 1450b57dc7c1SPaul Blakey return -1; 1451b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1452b57dc7c1SPaul Blakey range->max_addr.ip)) 1453b57dc7c1SPaul Blakey return -1; 1454b57dc7c1SPaul Blakey } else { 1455b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1456b57dc7c1SPaul Blakey &range->min_addr.in6)) 1457b57dc7c1SPaul Blakey return -1; 1458b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1459b57dc7c1SPaul Blakey &range->max_addr.in6)) 1460b57dc7c1SPaul Blakey return -1; 1461b57dc7c1SPaul Blakey } 1462b57dc7c1SPaul Blakey } 1463b57dc7c1SPaul Blakey 1464b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1465b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1466b57dc7c1SPaul Blakey range->min_proto.all)) 1467b57dc7c1SPaul Blakey return -1; 1468b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1469b57dc7c1SPaul Blakey range->max_proto.all)) 1470b57dc7c1SPaul Blakey return -1; 1471b57dc7c1SPaul Blakey } 1472b57dc7c1SPaul Blakey 1473b57dc7c1SPaul Blakey return 0; 1474b57dc7c1SPaul Blakey } 1475b57dc7c1SPaul Blakey 1476a21b06e7SXin Long static int tcf_ct_dump_helper(struct sk_buff *skb, struct nf_conntrack_helper *helper) 1477a21b06e7SXin Long { 1478a21b06e7SXin Long if (!helper) 1479a21b06e7SXin Long return 0; 1480a21b06e7SXin Long 1481a21b06e7SXin Long if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) || 1482a21b06e7SXin Long nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->tuple.src.l3num) || 1483a21b06e7SXin Long nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->tuple.dst.protonum)) 1484a21b06e7SXin Long return -1; 1485a21b06e7SXin Long 1486a21b06e7SXin Long return 0; 1487a21b06e7SXin Long } 1488a21b06e7SXin Long 1489b57dc7c1SPaul Blakey static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1490b57dc7c1SPaul Blakey int bind, int ref) 1491b57dc7c1SPaul Blakey { 1492b57dc7c1SPaul Blakey unsigned char *b = skb_tail_pointer(skb); 1493b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1494b57dc7c1SPaul Blakey struct tcf_ct_params *p; 1495b57dc7c1SPaul Blakey 1496b57dc7c1SPaul Blakey struct tc_ct opt = { 1497b57dc7c1SPaul Blakey .index = c->tcf_index, 1498b57dc7c1SPaul Blakey .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1499b57dc7c1SPaul Blakey .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1500b57dc7c1SPaul Blakey }; 1501b57dc7c1SPaul Blakey struct tcf_t t; 1502b57dc7c1SPaul Blakey 1503b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1504b57dc7c1SPaul Blakey p = rcu_dereference_protected(c->params, 1505b57dc7c1SPaul Blakey lockdep_is_held(&c->tcf_lock)); 1506b57dc7c1SPaul Blakey opt.action = c->tcf_action; 1507b57dc7c1SPaul Blakey 1508b57dc7c1SPaul Blakey if (tcf_ct_dump_key_val(skb, 1509b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1510b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1511b57dc7c1SPaul Blakey sizeof(p->ct_action))) 1512b57dc7c1SPaul Blakey goto nla_put_failure; 1513b57dc7c1SPaul Blakey 1514b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1515b57dc7c1SPaul Blakey goto skip_dump; 1516b57dc7c1SPaul Blakey 1517b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1518b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1519b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1520b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1521b57dc7c1SPaul Blakey sizeof(p->mark))) 1522b57dc7c1SPaul Blakey goto nla_put_failure; 1523b57dc7c1SPaul Blakey 1524b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1525b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1526b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1527b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1528b57dc7c1SPaul Blakey sizeof(p->labels))) 1529b57dc7c1SPaul Blakey goto nla_put_failure; 1530b57dc7c1SPaul Blakey 1531b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1532b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1533b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1534b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1535b57dc7c1SPaul Blakey sizeof(p->zone))) 1536b57dc7c1SPaul Blakey goto nla_put_failure; 1537b57dc7c1SPaul Blakey 1538b57dc7c1SPaul Blakey if (tcf_ct_dump_nat(skb, p)) 1539b57dc7c1SPaul Blakey goto nla_put_failure; 1540b57dc7c1SPaul Blakey 1541a21b06e7SXin Long if (tcf_ct_dump_helper(skb, p->helper)) 1542a21b06e7SXin Long goto nla_put_failure; 1543a21b06e7SXin Long 1544b57dc7c1SPaul Blakey skip_dump: 1545b57dc7c1SPaul Blakey if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1546b57dc7c1SPaul Blakey goto nla_put_failure; 1547b57dc7c1SPaul Blakey 1548b57dc7c1SPaul Blakey tcf_tm_dump(&t, &c->tcf_tm); 1549b57dc7c1SPaul Blakey if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1550b57dc7c1SPaul Blakey goto nla_put_failure; 1551b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1552b57dc7c1SPaul Blakey 1553b57dc7c1SPaul Blakey return skb->len; 1554b57dc7c1SPaul Blakey nla_put_failure: 1555b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1556b57dc7c1SPaul Blakey nlmsg_trim(skb, b); 1557b57dc7c1SPaul Blakey return -1; 1558b57dc7c1SPaul Blakey } 1559b57dc7c1SPaul Blakey 15604b61d3e8SPo Liu static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, 15614b61d3e8SPo Liu u64 drops, u64 lastuse, bool hw) 1562b57dc7c1SPaul Blakey { 1563b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1564b57dc7c1SPaul Blakey 15654b61d3e8SPo Liu tcf_action_update_stats(a, bytes, packets, drops, hw); 1566b57dc7c1SPaul Blakey c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1567b57dc7c1SPaul Blakey } 1568b57dc7c1SPaul Blakey 1569c54e1d92SBaowen Zheng static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data, 1570c2ccf84eSIdo Schimmel u32 *index_inc, bool bind, 1571c2ccf84eSIdo Schimmel struct netlink_ext_ack *extack) 1572c54e1d92SBaowen Zheng { 1573c54e1d92SBaowen Zheng if (bind) { 1574c54e1d92SBaowen Zheng struct flow_action_entry *entry = entry_data; 1575c54e1d92SBaowen Zheng 15760a7c9d1fSXin Long if (tcf_ct_helper(act)) 15770a7c9d1fSXin Long return -EOPNOTSUPP; 15780a7c9d1fSXin Long 1579c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_CT; 1580c54e1d92SBaowen Zheng entry->ct.action = tcf_ct_action(act); 1581c54e1d92SBaowen Zheng entry->ct.zone = tcf_ct_zone(act); 1582c54e1d92SBaowen Zheng entry->ct.flow_table = tcf_ct_ft(act); 1583c54e1d92SBaowen Zheng *index_inc = 1; 1584c54e1d92SBaowen Zheng } else { 15858cbfe939SBaowen Zheng struct flow_offload_action *fl_action = entry_data; 15868cbfe939SBaowen Zheng 15878cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_CT; 1588c54e1d92SBaowen Zheng } 1589c54e1d92SBaowen Zheng 1590c54e1d92SBaowen Zheng return 0; 1591c54e1d92SBaowen Zheng } 1592c54e1d92SBaowen Zheng 1593b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops = { 1594b57dc7c1SPaul Blakey .kind = "ct", 1595b57dc7c1SPaul Blakey .id = TCA_ID_CT, 1596b57dc7c1SPaul Blakey .owner = THIS_MODULE, 1597b57dc7c1SPaul Blakey .act = tcf_ct_act, 1598b57dc7c1SPaul Blakey .dump = tcf_ct_dump, 1599b57dc7c1SPaul Blakey .init = tcf_ct_init, 1600b57dc7c1SPaul Blakey .cleanup = tcf_ct_cleanup, 1601b57dc7c1SPaul Blakey .stats_update = tcf_stats_update, 1602c54e1d92SBaowen Zheng .offload_act_setup = tcf_ct_offload_act_setup, 1603b57dc7c1SPaul Blakey .size = sizeof(struct tcf_ct), 1604b57dc7c1SPaul Blakey }; 1605b57dc7c1SPaul Blakey 1606b57dc7c1SPaul Blakey static __net_init int ct_init_net(struct net *net) 1607b57dc7c1SPaul Blakey { 1608c593642cSPankaj Bharadiya unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1609acd0a7abSZhengchao Shao struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1610b57dc7c1SPaul Blakey 1611b57dc7c1SPaul Blakey if (nf_connlabels_get(net, n_bits - 1)) { 1612b57dc7c1SPaul Blakey tn->labels = false; 1613b57dc7c1SPaul Blakey pr_err("act_ct: Failed to set connlabels length"); 1614b57dc7c1SPaul Blakey } else { 1615b57dc7c1SPaul Blakey tn->labels = true; 1616b57dc7c1SPaul Blakey } 1617b57dc7c1SPaul Blakey 1618981471bdSCong Wang return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1619b57dc7c1SPaul Blakey } 1620b57dc7c1SPaul Blakey 1621b57dc7c1SPaul Blakey static void __net_exit ct_exit_net(struct list_head *net_list) 1622b57dc7c1SPaul Blakey { 1623b57dc7c1SPaul Blakey struct net *net; 1624b57dc7c1SPaul Blakey 1625b57dc7c1SPaul Blakey rtnl_lock(); 1626b57dc7c1SPaul Blakey list_for_each_entry(net, net_list, exit_list) { 1627acd0a7abSZhengchao Shao struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1628b57dc7c1SPaul Blakey 1629b57dc7c1SPaul Blakey if (tn->labels) 1630b57dc7c1SPaul Blakey nf_connlabels_put(net); 1631b57dc7c1SPaul Blakey } 1632b57dc7c1SPaul Blakey rtnl_unlock(); 1633b57dc7c1SPaul Blakey 1634acd0a7abSZhengchao Shao tc_action_net_exit(net_list, act_ct_ops.net_id); 1635b57dc7c1SPaul Blakey } 1636b57dc7c1SPaul Blakey 1637b57dc7c1SPaul Blakey static struct pernet_operations ct_net_ops = { 1638b57dc7c1SPaul Blakey .init = ct_init_net, 1639b57dc7c1SPaul Blakey .exit_batch = ct_exit_net, 1640acd0a7abSZhengchao Shao .id = &act_ct_ops.net_id, 1641b57dc7c1SPaul Blakey .size = sizeof(struct tc_ct_action_net), 1642b57dc7c1SPaul Blakey }; 1643b57dc7c1SPaul Blakey 1644b57dc7c1SPaul Blakey static int __init ct_init_module(void) 1645b57dc7c1SPaul Blakey { 1646c34b961aSPaul Blakey int err; 1647c34b961aSPaul Blakey 1648c34b961aSPaul Blakey act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1649c34b961aSPaul Blakey if (!act_ct_wq) 1650c34b961aSPaul Blakey return -ENOMEM; 1651c34b961aSPaul Blakey 1652c34b961aSPaul Blakey err = tcf_ct_flow_tables_init(); 1653c34b961aSPaul Blakey if (err) 1654c34b961aSPaul Blakey goto err_tbl_init; 1655c34b961aSPaul Blakey 1656c34b961aSPaul Blakey err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1657c34b961aSPaul Blakey if (err) 1658c34b961aSPaul Blakey goto err_register; 1659c34b961aSPaul Blakey 1660c129412fSwenxu static_branch_inc(&tcf_frag_xmit_count); 1661c129412fSwenxu 1662c34b961aSPaul Blakey return 0; 1663c34b961aSPaul Blakey 1664c34b961aSPaul Blakey err_register: 1665c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 16668c5c51f5Sliujian err_tbl_init: 16678c5c51f5Sliujian destroy_workqueue(act_ct_wq); 1668c34b961aSPaul Blakey return err; 1669b57dc7c1SPaul Blakey } 1670b57dc7c1SPaul Blakey 1671b57dc7c1SPaul Blakey static void __exit ct_cleanup_module(void) 1672b57dc7c1SPaul Blakey { 1673c129412fSwenxu static_branch_dec(&tcf_frag_xmit_count); 1674b57dc7c1SPaul Blakey tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1675c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 1676c34b961aSPaul Blakey destroy_workqueue(act_ct_wq); 1677b57dc7c1SPaul Blakey } 1678b57dc7c1SPaul Blakey 1679b57dc7c1SPaul Blakey module_init(ct_init_module); 1680b57dc7c1SPaul Blakey module_exit(ct_cleanup_module); 1681b57dc7c1SPaul Blakey MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1682b57dc7c1SPaul Blakey MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1683b57dc7c1SPaul Blakey MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1684b57dc7c1SPaul Blakey MODULE_DESCRIPTION("Connection tracking action"); 1685b57dc7c1SPaul Blakey MODULE_LICENSE("GPL v2"); 1686