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> 33b57dc7c1SPaul Blakey #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 3440d102cdSJeremy Sowden #include <uapi/linux/netfilter/nf_nat.h> 35b57dc7c1SPaul Blakey 36c34b961aSPaul Blakey static struct workqueue_struct *act_ct_wq; 37c34b961aSPaul Blakey static struct rhashtable zones_ht; 38138470a9SEric Dumazet static DEFINE_MUTEX(zones_mutex); 39c34b961aSPaul Blakey 40c34b961aSPaul Blakey struct tcf_ct_flow_table { 41c34b961aSPaul Blakey struct rhash_head node; /* In zones tables */ 42c34b961aSPaul Blakey 43c34b961aSPaul Blakey struct rcu_work rwork; 44c34b961aSPaul Blakey struct nf_flowtable nf_ft; 45138470a9SEric Dumazet refcount_t ref; 46c34b961aSPaul Blakey u16 zone; 47c34b961aSPaul Blakey 48c34b961aSPaul Blakey bool dying; 49c34b961aSPaul Blakey }; 50c34b961aSPaul Blakey 51c34b961aSPaul Blakey static const struct rhashtable_params zones_params = { 52c34b961aSPaul Blakey .head_offset = offsetof(struct tcf_ct_flow_table, node), 53c34b961aSPaul Blakey .key_offset = offsetof(struct tcf_ct_flow_table, zone), 54c34b961aSPaul Blakey .key_len = sizeof_field(struct tcf_ct_flow_table, zone), 55c34b961aSPaul Blakey .automatic_shrinking = true, 56c34b961aSPaul Blakey }; 57c34b961aSPaul Blakey 58*9c26ba9bSPaul Blakey static struct flow_action_entry * 59*9c26ba9bSPaul Blakey tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 60*9c26ba9bSPaul Blakey { 61*9c26ba9bSPaul Blakey int i = flow_action->num_entries++; 62*9c26ba9bSPaul Blakey 63*9c26ba9bSPaul Blakey return &flow_action->entries[i]; 64*9c26ba9bSPaul Blakey } 65*9c26ba9bSPaul Blakey 66*9c26ba9bSPaul Blakey static void tcf_ct_add_mangle_action(struct flow_action *action, 67*9c26ba9bSPaul Blakey enum flow_action_mangle_base htype, 68*9c26ba9bSPaul Blakey u32 offset, 69*9c26ba9bSPaul Blakey u32 mask, 70*9c26ba9bSPaul Blakey u32 val) 71*9c26ba9bSPaul Blakey { 72*9c26ba9bSPaul Blakey struct flow_action_entry *entry; 73*9c26ba9bSPaul Blakey 74*9c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 75*9c26ba9bSPaul Blakey entry->id = FLOW_ACTION_MANGLE; 76*9c26ba9bSPaul Blakey entry->mangle.htype = htype; 77*9c26ba9bSPaul Blakey entry->mangle.mask = ~mask; 78*9c26ba9bSPaul Blakey entry->mangle.offset = offset; 79*9c26ba9bSPaul Blakey entry->mangle.val = val; 80*9c26ba9bSPaul Blakey } 81*9c26ba9bSPaul Blakey 82*9c26ba9bSPaul Blakey /* The following nat helper functions check if the inverted reverse tuple 83*9c26ba9bSPaul Blakey * (target) is different then the current dir tuple - meaning nat for ports 84*9c26ba9bSPaul Blakey * and/or ip is needed, and add the relevant mangle actions. 85*9c26ba9bSPaul Blakey */ 86*9c26ba9bSPaul Blakey static void 87*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 88*9c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 89*9c26ba9bSPaul Blakey struct flow_action *action) 90*9c26ba9bSPaul Blakey { 91*9c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 92*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 93*9c26ba9bSPaul Blakey offsetof(struct iphdr, saddr), 94*9c26ba9bSPaul Blakey 0xFFFFFFFF, 95*9c26ba9bSPaul Blakey be32_to_cpu(target.src.u3.ip)); 96*9c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 97*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 98*9c26ba9bSPaul Blakey offsetof(struct iphdr, daddr), 99*9c26ba9bSPaul Blakey 0xFFFFFFFF, 100*9c26ba9bSPaul Blakey be32_to_cpu(target.dst.u3.ip)); 101*9c26ba9bSPaul Blakey } 102*9c26ba9bSPaul Blakey 103*9c26ba9bSPaul Blakey static void 104*9c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 105*9c26ba9bSPaul Blakey union nf_inet_addr *addr, 106*9c26ba9bSPaul Blakey u32 offset) 107*9c26ba9bSPaul Blakey { 108*9c26ba9bSPaul Blakey int i; 109*9c26ba9bSPaul Blakey 110*9c26ba9bSPaul Blakey for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 111*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 112*9c26ba9bSPaul Blakey i * sizeof(u32) + offset, 113*9c26ba9bSPaul Blakey 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 114*9c26ba9bSPaul Blakey } 115*9c26ba9bSPaul Blakey 116*9c26ba9bSPaul Blakey static void 117*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 118*9c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 119*9c26ba9bSPaul Blakey struct flow_action *action) 120*9c26ba9bSPaul Blakey { 121*9c26ba9bSPaul Blakey if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 122*9c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 123*9c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 124*9c26ba9bSPaul Blakey saddr)); 125*9c26ba9bSPaul Blakey if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 126*9c26ba9bSPaul Blakey tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 127*9c26ba9bSPaul Blakey offsetof(struct ipv6hdr, 128*9c26ba9bSPaul Blakey daddr)); 129*9c26ba9bSPaul Blakey } 130*9c26ba9bSPaul Blakey 131*9c26ba9bSPaul Blakey static void 132*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 133*9c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 134*9c26ba9bSPaul Blakey struct flow_action *action) 135*9c26ba9bSPaul Blakey { 136*9c26ba9bSPaul Blakey __be16 target_src = target.src.u.tcp.port; 137*9c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.tcp.port; 138*9c26ba9bSPaul Blakey 139*9c26ba9bSPaul Blakey if (target_src != tuple->src.u.tcp.port) 140*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 141*9c26ba9bSPaul Blakey offsetof(struct tcphdr, source), 142*9c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 143*9c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.tcp.port) 144*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 145*9c26ba9bSPaul Blakey offsetof(struct tcphdr, dest), 146*9c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 147*9c26ba9bSPaul Blakey } 148*9c26ba9bSPaul Blakey 149*9c26ba9bSPaul Blakey static void 150*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 151*9c26ba9bSPaul Blakey struct nf_conntrack_tuple target, 152*9c26ba9bSPaul Blakey struct flow_action *action) 153*9c26ba9bSPaul Blakey { 154*9c26ba9bSPaul Blakey __be16 target_src = target.src.u.udp.port; 155*9c26ba9bSPaul Blakey __be16 target_dst = target.dst.u.udp.port; 156*9c26ba9bSPaul Blakey 157*9c26ba9bSPaul Blakey if (target_src != tuple->src.u.udp.port) 158*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 159*9c26ba9bSPaul Blakey offsetof(struct udphdr, source), 160*9c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_src)); 161*9c26ba9bSPaul Blakey if (target_dst != tuple->dst.u.udp.port) 162*9c26ba9bSPaul Blakey tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 163*9c26ba9bSPaul Blakey offsetof(struct udphdr, dest), 164*9c26ba9bSPaul Blakey 0xFFFF, be16_to_cpu(target_dst)); 165*9c26ba9bSPaul Blakey } 166*9c26ba9bSPaul Blakey 167*9c26ba9bSPaul Blakey static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 168*9c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 169*9c26ba9bSPaul Blakey struct flow_action *action) 170*9c26ba9bSPaul Blakey { 171*9c26ba9bSPaul Blakey struct nf_conn_labels *ct_labels; 172*9c26ba9bSPaul Blakey struct flow_action_entry *entry; 173*9c26ba9bSPaul Blakey u32 *act_ct_labels; 174*9c26ba9bSPaul Blakey 175*9c26ba9bSPaul Blakey entry = tcf_ct_flow_table_flow_action_get_next(action); 176*9c26ba9bSPaul Blakey entry->id = FLOW_ACTION_CT_METADATA; 177*9c26ba9bSPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 178*9c26ba9bSPaul Blakey entry->ct_metadata.mark = ct->mark; 179*9c26ba9bSPaul Blakey #endif 180*9c26ba9bSPaul Blakey 181*9c26ba9bSPaul Blakey act_ct_labels = entry->ct_metadata.labels; 182*9c26ba9bSPaul Blakey ct_labels = nf_ct_labels_find(ct); 183*9c26ba9bSPaul Blakey if (ct_labels) 184*9c26ba9bSPaul Blakey memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 185*9c26ba9bSPaul Blakey else 186*9c26ba9bSPaul Blakey memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 187*9c26ba9bSPaul Blakey } 188*9c26ba9bSPaul Blakey 189*9c26ba9bSPaul Blakey static int tcf_ct_flow_table_add_action_nat(struct net *net, 190*9c26ba9bSPaul Blakey struct nf_conn *ct, 191*9c26ba9bSPaul Blakey enum ip_conntrack_dir dir, 192*9c26ba9bSPaul Blakey struct flow_action *action) 193*9c26ba9bSPaul Blakey { 194*9c26ba9bSPaul Blakey const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 195*9c26ba9bSPaul Blakey struct nf_conntrack_tuple target; 196*9c26ba9bSPaul Blakey 197*9c26ba9bSPaul Blakey nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 198*9c26ba9bSPaul Blakey 199*9c26ba9bSPaul Blakey switch (tuple->src.l3num) { 200*9c26ba9bSPaul Blakey case NFPROTO_IPV4: 201*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 202*9c26ba9bSPaul Blakey action); 203*9c26ba9bSPaul Blakey break; 204*9c26ba9bSPaul Blakey case NFPROTO_IPV6: 205*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 206*9c26ba9bSPaul Blakey action); 207*9c26ba9bSPaul Blakey break; 208*9c26ba9bSPaul Blakey default: 209*9c26ba9bSPaul Blakey return -EOPNOTSUPP; 210*9c26ba9bSPaul Blakey } 211*9c26ba9bSPaul Blakey 212*9c26ba9bSPaul Blakey switch (nf_ct_protonum(ct)) { 213*9c26ba9bSPaul Blakey case IPPROTO_TCP: 214*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 215*9c26ba9bSPaul Blakey break; 216*9c26ba9bSPaul Blakey case IPPROTO_UDP: 217*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 218*9c26ba9bSPaul Blakey break; 219*9c26ba9bSPaul Blakey default: 220*9c26ba9bSPaul Blakey return -EOPNOTSUPP; 221*9c26ba9bSPaul Blakey } 222*9c26ba9bSPaul Blakey 223*9c26ba9bSPaul Blakey return 0; 224*9c26ba9bSPaul Blakey } 225*9c26ba9bSPaul Blakey 226*9c26ba9bSPaul Blakey static int tcf_ct_flow_table_fill_actions(struct net *net, 227*9c26ba9bSPaul Blakey const struct flow_offload *flow, 228*9c26ba9bSPaul Blakey enum flow_offload_tuple_dir tdir, 229*9c26ba9bSPaul Blakey struct nf_flow_rule *flow_rule) 230*9c26ba9bSPaul Blakey { 231*9c26ba9bSPaul Blakey struct flow_action *action = &flow_rule->rule->action; 232*9c26ba9bSPaul Blakey int num_entries = action->num_entries; 233*9c26ba9bSPaul Blakey struct nf_conn *ct = flow->ct; 234*9c26ba9bSPaul Blakey enum ip_conntrack_dir dir; 235*9c26ba9bSPaul Blakey int i, err; 236*9c26ba9bSPaul Blakey 237*9c26ba9bSPaul Blakey switch (tdir) { 238*9c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_ORIGINAL: 239*9c26ba9bSPaul Blakey dir = IP_CT_DIR_ORIGINAL; 240*9c26ba9bSPaul Blakey break; 241*9c26ba9bSPaul Blakey case FLOW_OFFLOAD_DIR_REPLY: 242*9c26ba9bSPaul Blakey dir = IP_CT_DIR_REPLY; 243*9c26ba9bSPaul Blakey break; 244*9c26ba9bSPaul Blakey default: 245*9c26ba9bSPaul Blakey return -EOPNOTSUPP; 246*9c26ba9bSPaul Blakey } 247*9c26ba9bSPaul Blakey 248*9c26ba9bSPaul Blakey err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 249*9c26ba9bSPaul Blakey if (err) 250*9c26ba9bSPaul Blakey goto err_nat; 251*9c26ba9bSPaul Blakey 252*9c26ba9bSPaul Blakey tcf_ct_flow_table_add_action_meta(ct, dir, action); 253*9c26ba9bSPaul Blakey return 0; 254*9c26ba9bSPaul Blakey 255*9c26ba9bSPaul Blakey err_nat: 256*9c26ba9bSPaul Blakey /* Clear filled actions */ 257*9c26ba9bSPaul Blakey for (i = num_entries; i < action->num_entries; i++) 258*9c26ba9bSPaul Blakey memset(&action->entries[i], 0, sizeof(action->entries[i])); 259*9c26ba9bSPaul Blakey action->num_entries = num_entries; 260*9c26ba9bSPaul Blakey 261*9c26ba9bSPaul Blakey return err; 262*9c26ba9bSPaul Blakey } 263*9c26ba9bSPaul Blakey 264c34b961aSPaul Blakey static struct nf_flowtable_type flowtable_ct = { 265*9c26ba9bSPaul Blakey .action = tcf_ct_flow_table_fill_actions, 266c34b961aSPaul Blakey .owner = THIS_MODULE, 267c34b961aSPaul Blakey }; 268c34b961aSPaul Blakey 269c34b961aSPaul Blakey static int tcf_ct_flow_table_get(struct tcf_ct_params *params) 270c34b961aSPaul Blakey { 271c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 272c34b961aSPaul Blakey int err = -ENOMEM; 273c34b961aSPaul Blakey 274138470a9SEric Dumazet mutex_lock(&zones_mutex); 275c34b961aSPaul Blakey ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 276138470a9SEric Dumazet if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 277138470a9SEric Dumazet goto out_unlock; 278c34b961aSPaul Blakey 279138470a9SEric Dumazet ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 280c34b961aSPaul Blakey if (!ct_ft) 281c34b961aSPaul Blakey goto err_alloc; 282138470a9SEric Dumazet refcount_set(&ct_ft->ref, 1); 283c34b961aSPaul Blakey 284c34b961aSPaul Blakey ct_ft->zone = params->zone; 285c34b961aSPaul Blakey err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 286c34b961aSPaul Blakey if (err) 287c34b961aSPaul Blakey goto err_insert; 288c34b961aSPaul Blakey 289c34b961aSPaul Blakey ct_ft->nf_ft.type = &flowtable_ct; 290c34b961aSPaul Blakey err = nf_flow_table_init(&ct_ft->nf_ft); 291c34b961aSPaul Blakey if (err) 292c34b961aSPaul Blakey goto err_init; 293c34b961aSPaul Blakey 294c34b961aSPaul Blakey __module_get(THIS_MODULE); 295138470a9SEric Dumazet out_unlock: 296c34b961aSPaul Blakey params->ct_ft = ct_ft; 297138470a9SEric Dumazet mutex_unlock(&zones_mutex); 298c34b961aSPaul Blakey 299c34b961aSPaul Blakey return 0; 300c34b961aSPaul Blakey 301c34b961aSPaul Blakey err_init: 302c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 303c34b961aSPaul Blakey err_insert: 304c34b961aSPaul Blakey kfree(ct_ft); 305c34b961aSPaul Blakey err_alloc: 306138470a9SEric Dumazet mutex_unlock(&zones_mutex); 307c34b961aSPaul Blakey return err; 308c34b961aSPaul Blakey } 309c34b961aSPaul Blakey 310c34b961aSPaul Blakey static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 311c34b961aSPaul Blakey { 312c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft; 313c34b961aSPaul Blakey 314c34b961aSPaul Blakey ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 315c34b961aSPaul Blakey rwork); 316c34b961aSPaul Blakey nf_flow_table_free(&ct_ft->nf_ft); 317c34b961aSPaul Blakey kfree(ct_ft); 318c34b961aSPaul Blakey 319c34b961aSPaul Blakey module_put(THIS_MODULE); 320c34b961aSPaul Blakey } 321c34b961aSPaul Blakey 322c34b961aSPaul Blakey static void tcf_ct_flow_table_put(struct tcf_ct_params *params) 323c34b961aSPaul Blakey { 324c34b961aSPaul Blakey struct tcf_ct_flow_table *ct_ft = params->ct_ft; 325c34b961aSPaul Blakey 326138470a9SEric Dumazet if (refcount_dec_and_test(¶ms->ct_ft->ref)) { 327c34b961aSPaul Blakey rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 328c34b961aSPaul Blakey INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 329c34b961aSPaul Blakey queue_rcu_work(act_ct_wq, &ct_ft->rwork); 330c34b961aSPaul Blakey } 331c34b961aSPaul Blakey } 332c34b961aSPaul Blakey 33364ff70b8SPaul Blakey static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 33464ff70b8SPaul Blakey struct nf_conn *ct, 33564ff70b8SPaul Blakey bool tcp) 33664ff70b8SPaul Blakey { 33764ff70b8SPaul Blakey struct flow_offload *entry; 33864ff70b8SPaul Blakey int err; 33964ff70b8SPaul Blakey 34064ff70b8SPaul Blakey if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 34164ff70b8SPaul Blakey return; 34264ff70b8SPaul Blakey 34364ff70b8SPaul Blakey entry = flow_offload_alloc(ct); 34464ff70b8SPaul Blakey if (!entry) { 34564ff70b8SPaul Blakey WARN_ON_ONCE(1); 34664ff70b8SPaul Blakey goto err_alloc; 34764ff70b8SPaul Blakey } 34864ff70b8SPaul Blakey 34964ff70b8SPaul Blakey if (tcp) { 35064ff70b8SPaul Blakey ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 35164ff70b8SPaul Blakey ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 35264ff70b8SPaul Blakey } 35364ff70b8SPaul Blakey 35464ff70b8SPaul Blakey err = flow_offload_add(&ct_ft->nf_ft, entry); 35564ff70b8SPaul Blakey if (err) 35664ff70b8SPaul Blakey goto err_add; 35764ff70b8SPaul Blakey 35864ff70b8SPaul Blakey return; 35964ff70b8SPaul Blakey 36064ff70b8SPaul Blakey err_add: 36164ff70b8SPaul Blakey flow_offload_free(entry); 36264ff70b8SPaul Blakey err_alloc: 36364ff70b8SPaul Blakey clear_bit(IPS_OFFLOAD_BIT, &ct->status); 36464ff70b8SPaul Blakey } 36564ff70b8SPaul Blakey 36664ff70b8SPaul Blakey static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 36764ff70b8SPaul Blakey struct nf_conn *ct, 36864ff70b8SPaul Blakey enum ip_conntrack_info ctinfo) 36964ff70b8SPaul Blakey { 37064ff70b8SPaul Blakey bool tcp = false; 37164ff70b8SPaul Blakey 37264ff70b8SPaul Blakey if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) 37364ff70b8SPaul Blakey return; 37464ff70b8SPaul Blakey 37564ff70b8SPaul Blakey switch (nf_ct_protonum(ct)) { 37664ff70b8SPaul Blakey case IPPROTO_TCP: 37764ff70b8SPaul Blakey tcp = true; 37864ff70b8SPaul Blakey if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 37964ff70b8SPaul Blakey return; 38064ff70b8SPaul Blakey break; 38164ff70b8SPaul Blakey case IPPROTO_UDP: 38264ff70b8SPaul Blakey break; 38364ff70b8SPaul Blakey default: 38464ff70b8SPaul Blakey return; 38564ff70b8SPaul Blakey } 38664ff70b8SPaul Blakey 38764ff70b8SPaul Blakey if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 38864ff70b8SPaul Blakey ct->status & IPS_SEQ_ADJUST) 38964ff70b8SPaul Blakey return; 39064ff70b8SPaul Blakey 39164ff70b8SPaul Blakey tcf_ct_flow_table_add(ct_ft, ct, tcp); 39264ff70b8SPaul Blakey } 39364ff70b8SPaul Blakey 39446475bb2SPaul Blakey static bool 39546475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 39607ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 39707ac9d16SPaul Blakey struct tcphdr **tcph) 39846475bb2SPaul Blakey { 39946475bb2SPaul Blakey struct flow_ports *ports; 40046475bb2SPaul Blakey unsigned int thoff; 40146475bb2SPaul Blakey struct iphdr *iph; 40246475bb2SPaul Blakey 4034cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*iph))) 40446475bb2SPaul Blakey return false; 40546475bb2SPaul Blakey 40646475bb2SPaul Blakey iph = ip_hdr(skb); 40746475bb2SPaul Blakey thoff = iph->ihl * 4; 40846475bb2SPaul Blakey 40946475bb2SPaul Blakey if (ip_is_fragment(iph) || 41046475bb2SPaul Blakey unlikely(thoff != sizeof(struct iphdr))) 41146475bb2SPaul Blakey return false; 41246475bb2SPaul Blakey 41346475bb2SPaul Blakey if (iph->protocol != IPPROTO_TCP && 41446475bb2SPaul Blakey iph->protocol != IPPROTO_UDP) 41546475bb2SPaul Blakey return false; 41646475bb2SPaul Blakey 41746475bb2SPaul Blakey if (iph->ttl <= 1) 41846475bb2SPaul Blakey return false; 41946475bb2SPaul Blakey 4204cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ? 42107ac9d16SPaul Blakey thoff + sizeof(struct tcphdr) : 42207ac9d16SPaul Blakey thoff + sizeof(*ports))) 42346475bb2SPaul Blakey return false; 42446475bb2SPaul Blakey 42507ac9d16SPaul Blakey iph = ip_hdr(skb); 42607ac9d16SPaul Blakey if (iph->protocol == IPPROTO_TCP) 42707ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 42846475bb2SPaul Blakey 42907ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 43046475bb2SPaul Blakey tuple->src_v4.s_addr = iph->saddr; 43146475bb2SPaul Blakey tuple->dst_v4.s_addr = iph->daddr; 43246475bb2SPaul Blakey tuple->src_port = ports->source; 43346475bb2SPaul Blakey tuple->dst_port = ports->dest; 43446475bb2SPaul Blakey tuple->l3proto = AF_INET; 43546475bb2SPaul Blakey tuple->l4proto = iph->protocol; 43646475bb2SPaul Blakey 43746475bb2SPaul Blakey return true; 43846475bb2SPaul Blakey } 43946475bb2SPaul Blakey 44046475bb2SPaul Blakey static bool 44146475bb2SPaul Blakey tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 44207ac9d16SPaul Blakey struct flow_offload_tuple *tuple, 44307ac9d16SPaul Blakey struct tcphdr **tcph) 44446475bb2SPaul Blakey { 44546475bb2SPaul Blakey struct flow_ports *ports; 44646475bb2SPaul Blakey struct ipv6hdr *ip6h; 44746475bb2SPaul Blakey unsigned int thoff; 44846475bb2SPaul Blakey 4494cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 45046475bb2SPaul Blakey return false; 45146475bb2SPaul Blakey 45246475bb2SPaul Blakey ip6h = ipv6_hdr(skb); 45346475bb2SPaul Blakey 45446475bb2SPaul Blakey if (ip6h->nexthdr != IPPROTO_TCP && 45546475bb2SPaul Blakey ip6h->nexthdr != IPPROTO_UDP) 45646475bb2SPaul Blakey return false; 45746475bb2SPaul Blakey 45846475bb2SPaul Blakey if (ip6h->hop_limit <= 1) 45946475bb2SPaul Blakey return false; 46046475bb2SPaul Blakey 46146475bb2SPaul Blakey thoff = sizeof(*ip6h); 4624cc5fdecSPaul Blakey if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ? 46307ac9d16SPaul Blakey thoff + sizeof(struct tcphdr) : 46407ac9d16SPaul Blakey thoff + sizeof(*ports))) 46546475bb2SPaul Blakey return false; 46646475bb2SPaul Blakey 46707ac9d16SPaul Blakey ip6h = ipv6_hdr(skb); 46807ac9d16SPaul Blakey if (ip6h->nexthdr == IPPROTO_TCP) 46907ac9d16SPaul Blakey *tcph = (void *)(skb_network_header(skb) + thoff); 47046475bb2SPaul Blakey 47107ac9d16SPaul Blakey ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 47246475bb2SPaul Blakey tuple->src_v6 = ip6h->saddr; 47346475bb2SPaul Blakey tuple->dst_v6 = ip6h->daddr; 47446475bb2SPaul Blakey tuple->src_port = ports->source; 47546475bb2SPaul Blakey tuple->dst_port = ports->dest; 47646475bb2SPaul Blakey tuple->l3proto = AF_INET6; 47746475bb2SPaul Blakey tuple->l4proto = ip6h->nexthdr; 47846475bb2SPaul Blakey 47946475bb2SPaul Blakey return true; 48046475bb2SPaul Blakey } 48146475bb2SPaul Blakey 48246475bb2SPaul Blakey static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 48346475bb2SPaul Blakey struct sk_buff *skb, 48446475bb2SPaul Blakey u8 family) 48546475bb2SPaul Blakey { 48646475bb2SPaul Blakey struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 48746475bb2SPaul Blakey struct flow_offload_tuple_rhash *tuplehash; 48846475bb2SPaul Blakey struct flow_offload_tuple tuple = {}; 48946475bb2SPaul Blakey enum ip_conntrack_info ctinfo; 49007ac9d16SPaul Blakey struct tcphdr *tcph = NULL; 49146475bb2SPaul Blakey struct flow_offload *flow; 49246475bb2SPaul Blakey struct nf_conn *ct; 49346475bb2SPaul Blakey u8 dir; 49446475bb2SPaul Blakey 49546475bb2SPaul Blakey /* Previously seen or loopback */ 49646475bb2SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 49746475bb2SPaul Blakey if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 49846475bb2SPaul Blakey return false; 49946475bb2SPaul Blakey 50046475bb2SPaul Blakey switch (family) { 50146475bb2SPaul Blakey case NFPROTO_IPV4: 50207ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 50346475bb2SPaul Blakey return false; 50446475bb2SPaul Blakey break; 50546475bb2SPaul Blakey case NFPROTO_IPV6: 50607ac9d16SPaul Blakey if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 50746475bb2SPaul Blakey return false; 50846475bb2SPaul Blakey break; 50946475bb2SPaul Blakey default: 51046475bb2SPaul Blakey return false; 51146475bb2SPaul Blakey } 51246475bb2SPaul Blakey 51346475bb2SPaul Blakey tuplehash = flow_offload_lookup(nf_ft, &tuple); 51446475bb2SPaul Blakey if (!tuplehash) 51546475bb2SPaul Blakey return false; 51646475bb2SPaul Blakey 51746475bb2SPaul Blakey dir = tuplehash->tuple.dir; 51846475bb2SPaul Blakey flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 51946475bb2SPaul Blakey ct = flow->ct; 52046475bb2SPaul Blakey 52107ac9d16SPaul Blakey if (tcph && (unlikely(tcph->fin || tcph->rst))) { 52207ac9d16SPaul Blakey flow_offload_teardown(flow); 52307ac9d16SPaul Blakey return false; 52407ac9d16SPaul Blakey } 52507ac9d16SPaul Blakey 52646475bb2SPaul Blakey ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 52746475bb2SPaul Blakey IP_CT_ESTABLISHED_REPLY; 52846475bb2SPaul Blakey 52946475bb2SPaul Blakey nf_conntrack_get(&ct->ct_general); 53046475bb2SPaul Blakey nf_ct_set(skb, ct, ctinfo); 53146475bb2SPaul Blakey 53246475bb2SPaul Blakey return true; 53346475bb2SPaul Blakey } 53446475bb2SPaul Blakey 535c34b961aSPaul Blakey static int tcf_ct_flow_tables_init(void) 536c34b961aSPaul Blakey { 537c34b961aSPaul Blakey return rhashtable_init(&zones_ht, &zones_params); 538c34b961aSPaul Blakey } 539c34b961aSPaul Blakey 540c34b961aSPaul Blakey static void tcf_ct_flow_tables_uninit(void) 541c34b961aSPaul Blakey { 542c34b961aSPaul Blakey rhashtable_destroy(&zones_ht); 543c34b961aSPaul Blakey } 544c34b961aSPaul Blakey 545b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops; 546b57dc7c1SPaul Blakey static unsigned int ct_net_id; 547b57dc7c1SPaul Blakey 548b57dc7c1SPaul Blakey struct tc_ct_action_net { 549b57dc7c1SPaul Blakey struct tc_action_net tn; /* Must be first */ 550b57dc7c1SPaul Blakey bool labels; 551b57dc7c1SPaul Blakey }; 552b57dc7c1SPaul Blakey 553b57dc7c1SPaul Blakey /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 554b57dc7c1SPaul Blakey static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 555b57dc7c1SPaul Blakey u16 zone_id, bool force) 556b57dc7c1SPaul Blakey { 557b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 558b57dc7c1SPaul Blakey struct nf_conn *ct; 559b57dc7c1SPaul Blakey 560b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 561b57dc7c1SPaul Blakey if (!ct) 562b57dc7c1SPaul Blakey return false; 563b57dc7c1SPaul Blakey if (!net_eq(net, read_pnet(&ct->ct_net))) 564b57dc7c1SPaul Blakey return false; 565b57dc7c1SPaul Blakey if (nf_ct_zone(ct)->id != zone_id) 566b57dc7c1SPaul Blakey return false; 567b57dc7c1SPaul Blakey 568b57dc7c1SPaul Blakey /* Force conntrack entry direction. */ 569b57dc7c1SPaul Blakey if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 570b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 571b57dc7c1SPaul Blakey nf_ct_kill(ct); 572b57dc7c1SPaul Blakey 573b57dc7c1SPaul Blakey nf_conntrack_put(&ct->ct_general); 574b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 575b57dc7c1SPaul Blakey 576b57dc7c1SPaul Blakey return false; 577b57dc7c1SPaul Blakey } 578b57dc7c1SPaul Blakey 579b57dc7c1SPaul Blakey return true; 580b57dc7c1SPaul Blakey } 581b57dc7c1SPaul Blakey 582b57dc7c1SPaul Blakey /* Trim the skb to the length specified by the IP/IPv6 header, 583b57dc7c1SPaul Blakey * removing any trailing lower-layer padding. This prepares the skb 584b57dc7c1SPaul Blakey * for higher-layer processing that assumes skb->len excludes padding 585b57dc7c1SPaul Blakey * (such as nf_ip_checksum). The caller needs to pull the skb to the 586b57dc7c1SPaul Blakey * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 587b57dc7c1SPaul Blakey */ 588b57dc7c1SPaul Blakey static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family) 589b57dc7c1SPaul Blakey { 590b57dc7c1SPaul Blakey unsigned int len; 591b57dc7c1SPaul Blakey int err; 592b57dc7c1SPaul Blakey 593b57dc7c1SPaul Blakey switch (family) { 594b57dc7c1SPaul Blakey case NFPROTO_IPV4: 595b57dc7c1SPaul Blakey len = ntohs(ip_hdr(skb)->tot_len); 596b57dc7c1SPaul Blakey break; 597b57dc7c1SPaul Blakey case NFPROTO_IPV6: 598b57dc7c1SPaul Blakey len = sizeof(struct ipv6hdr) 599b57dc7c1SPaul Blakey + ntohs(ipv6_hdr(skb)->payload_len); 600b57dc7c1SPaul Blakey break; 601b57dc7c1SPaul Blakey default: 602b57dc7c1SPaul Blakey len = skb->len; 603b57dc7c1SPaul Blakey } 604b57dc7c1SPaul Blakey 605b57dc7c1SPaul Blakey err = pskb_trim_rcsum(skb, len); 606b57dc7c1SPaul Blakey 607b57dc7c1SPaul Blakey return err; 608b57dc7c1SPaul Blakey } 609b57dc7c1SPaul Blakey 610b57dc7c1SPaul Blakey static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 611b57dc7c1SPaul Blakey { 612b57dc7c1SPaul Blakey u8 family = NFPROTO_UNSPEC; 613b57dc7c1SPaul Blakey 614b57dc7c1SPaul Blakey switch (skb->protocol) { 615b57dc7c1SPaul Blakey case htons(ETH_P_IP): 616b57dc7c1SPaul Blakey family = NFPROTO_IPV4; 617b57dc7c1SPaul Blakey break; 618b57dc7c1SPaul Blakey case htons(ETH_P_IPV6): 619b57dc7c1SPaul Blakey family = NFPROTO_IPV6; 620b57dc7c1SPaul Blakey break; 621b57dc7c1SPaul Blakey default: 622b57dc7c1SPaul Blakey break; 623b57dc7c1SPaul Blakey } 624b57dc7c1SPaul Blakey 625b57dc7c1SPaul Blakey return family; 626b57dc7c1SPaul Blakey } 627b57dc7c1SPaul Blakey 628b57dc7c1SPaul Blakey static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 629b57dc7c1SPaul Blakey { 630b57dc7c1SPaul Blakey unsigned int len; 631b57dc7c1SPaul Blakey 632b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct iphdr); 633b57dc7c1SPaul Blakey if (unlikely(skb->len < len)) 634b57dc7c1SPaul Blakey return -EINVAL; 635b57dc7c1SPaul Blakey if (unlikely(!pskb_may_pull(skb, len))) 636b57dc7c1SPaul Blakey return -ENOMEM; 637b57dc7c1SPaul Blakey 638b57dc7c1SPaul Blakey *frag = ip_is_fragment(ip_hdr(skb)); 639b57dc7c1SPaul Blakey return 0; 640b57dc7c1SPaul Blakey } 641b57dc7c1SPaul Blakey 642b57dc7c1SPaul Blakey static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 643b57dc7c1SPaul Blakey { 644b57dc7c1SPaul Blakey unsigned int flags = 0, len, payload_ofs = 0; 645b57dc7c1SPaul Blakey unsigned short frag_off; 646b57dc7c1SPaul Blakey int nexthdr; 647b57dc7c1SPaul Blakey 648b57dc7c1SPaul Blakey len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 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 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 655b57dc7c1SPaul Blakey if (unlikely(nexthdr < 0)) 656b57dc7c1SPaul Blakey return -EPROTO; 657b57dc7c1SPaul Blakey 658b57dc7c1SPaul Blakey *frag = flags & IP6_FH_F_FRAG; 659b57dc7c1SPaul Blakey return 0; 660b57dc7c1SPaul Blakey } 661b57dc7c1SPaul Blakey 662b57dc7c1SPaul Blakey static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 663b57dc7c1SPaul Blakey u8 family, u16 zone) 664b57dc7c1SPaul Blakey { 665b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 666b57dc7c1SPaul Blakey struct nf_conn *ct; 667b57dc7c1SPaul Blakey int err = 0; 668b57dc7c1SPaul Blakey bool frag; 669b57dc7c1SPaul Blakey 670b57dc7c1SPaul Blakey /* Previously seen (loopback)? Ignore. */ 671b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 672b57dc7c1SPaul Blakey if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 673b57dc7c1SPaul Blakey return 0; 674b57dc7c1SPaul Blakey 675b57dc7c1SPaul Blakey if (family == NFPROTO_IPV4) 676b57dc7c1SPaul Blakey err = tcf_ct_ipv4_is_fragment(skb, &frag); 677b57dc7c1SPaul Blakey else 678b57dc7c1SPaul Blakey err = tcf_ct_ipv6_is_fragment(skb, &frag); 679b57dc7c1SPaul Blakey if (err || !frag) 680b57dc7c1SPaul Blakey return err; 681b57dc7c1SPaul Blakey 682b57dc7c1SPaul Blakey skb_get(skb); 683b57dc7c1SPaul Blakey 684b57dc7c1SPaul Blakey if (family == NFPROTO_IPV4) { 685b57dc7c1SPaul Blakey enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 686b57dc7c1SPaul Blakey 687b57dc7c1SPaul Blakey memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 688b57dc7c1SPaul Blakey local_bh_disable(); 689b57dc7c1SPaul Blakey err = ip_defrag(net, skb, user); 690b57dc7c1SPaul Blakey local_bh_enable(); 691b57dc7c1SPaul Blakey if (err && err != -EINPROGRESS) 692b57dc7c1SPaul Blakey goto out_free; 693b57dc7c1SPaul Blakey } else { /* NFPROTO_IPV6 */ 694b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 695b57dc7c1SPaul Blakey enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 696b57dc7c1SPaul Blakey 697b57dc7c1SPaul Blakey memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 698b57dc7c1SPaul Blakey err = nf_ct_frag6_gather(net, skb, user); 699b57dc7c1SPaul Blakey if (err && err != -EINPROGRESS) 700b57dc7c1SPaul Blakey goto out_free; 701b57dc7c1SPaul Blakey #else 702b57dc7c1SPaul Blakey err = -EOPNOTSUPP; 703b57dc7c1SPaul Blakey goto out_free; 704b57dc7c1SPaul Blakey #endif 705b57dc7c1SPaul Blakey } 706b57dc7c1SPaul Blakey 707b57dc7c1SPaul Blakey skb_clear_hash(skb); 708b57dc7c1SPaul Blakey skb->ignore_df = 1; 709b57dc7c1SPaul Blakey return err; 710b57dc7c1SPaul Blakey 711b57dc7c1SPaul Blakey out_free: 712b57dc7c1SPaul Blakey kfree_skb(skb); 713b57dc7c1SPaul Blakey return err; 714b57dc7c1SPaul Blakey } 715b57dc7c1SPaul Blakey 716b57dc7c1SPaul Blakey static void tcf_ct_params_free(struct rcu_head *head) 717b57dc7c1SPaul Blakey { 718b57dc7c1SPaul Blakey struct tcf_ct_params *params = container_of(head, 719b57dc7c1SPaul Blakey struct tcf_ct_params, rcu); 720b57dc7c1SPaul Blakey 721c34b961aSPaul Blakey tcf_ct_flow_table_put(params); 722c34b961aSPaul Blakey 723b57dc7c1SPaul Blakey if (params->tmpl) 724b57dc7c1SPaul Blakey nf_conntrack_put(¶ms->tmpl->ct_general); 725b57dc7c1SPaul Blakey kfree(params); 726b57dc7c1SPaul Blakey } 727b57dc7c1SPaul Blakey 728b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT) 729b57dc7c1SPaul Blakey /* Modelled after nf_nat_ipv[46]_fn(). 730b57dc7c1SPaul Blakey * range is only used for new, uninitialized NAT state. 731b57dc7c1SPaul Blakey * Returns either NF_ACCEPT or NF_DROP. 732b57dc7c1SPaul Blakey */ 733b57dc7c1SPaul Blakey static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 734b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo, 735b57dc7c1SPaul Blakey const struct nf_nat_range2 *range, 736b57dc7c1SPaul Blakey enum nf_nat_manip_type maniptype) 737b57dc7c1SPaul Blakey { 738b57dc7c1SPaul Blakey int hooknum, err = NF_ACCEPT; 739b57dc7c1SPaul Blakey 740b57dc7c1SPaul Blakey /* See HOOK2MANIP(). */ 741b57dc7c1SPaul Blakey if (maniptype == NF_NAT_MANIP_SRC) 742b57dc7c1SPaul Blakey hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 743b57dc7c1SPaul Blakey else 744b57dc7c1SPaul Blakey hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 745b57dc7c1SPaul Blakey 746b57dc7c1SPaul Blakey switch (ctinfo) { 747b57dc7c1SPaul Blakey case IP_CT_RELATED: 748b57dc7c1SPaul Blakey case IP_CT_RELATED_REPLY: 749b57dc7c1SPaul Blakey if (skb->protocol == htons(ETH_P_IP) && 750b57dc7c1SPaul Blakey ip_hdr(skb)->protocol == IPPROTO_ICMP) { 751b57dc7c1SPaul Blakey if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 752b57dc7c1SPaul Blakey hooknum)) 753b57dc7c1SPaul Blakey err = NF_DROP; 754b57dc7c1SPaul Blakey goto out; 755b57dc7c1SPaul Blakey } else if (IS_ENABLED(CONFIG_IPV6) && 756b57dc7c1SPaul Blakey skb->protocol == htons(ETH_P_IPV6)) { 757b57dc7c1SPaul Blakey __be16 frag_off; 758b57dc7c1SPaul Blakey u8 nexthdr = ipv6_hdr(skb)->nexthdr; 759b57dc7c1SPaul Blakey int hdrlen = ipv6_skip_exthdr(skb, 760b57dc7c1SPaul Blakey sizeof(struct ipv6hdr), 761b57dc7c1SPaul Blakey &nexthdr, &frag_off); 762b57dc7c1SPaul Blakey 763b57dc7c1SPaul Blakey if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 764b57dc7c1SPaul Blakey if (!nf_nat_icmpv6_reply_translation(skb, ct, 765b57dc7c1SPaul Blakey ctinfo, 766b57dc7c1SPaul Blakey hooknum, 767b57dc7c1SPaul Blakey hdrlen)) 768b57dc7c1SPaul Blakey err = NF_DROP; 769b57dc7c1SPaul Blakey goto out; 770b57dc7c1SPaul Blakey } 771b57dc7c1SPaul Blakey } 772b57dc7c1SPaul Blakey /* Non-ICMP, fall thru to initialize if needed. */ 773b57dc7c1SPaul Blakey /* fall through */ 774b57dc7c1SPaul Blakey case IP_CT_NEW: 775b57dc7c1SPaul Blakey /* Seen it before? This can happen for loopback, retrans, 776b57dc7c1SPaul Blakey * or local packets. 777b57dc7c1SPaul Blakey */ 778b57dc7c1SPaul Blakey if (!nf_nat_initialized(ct, maniptype)) { 779b57dc7c1SPaul Blakey /* Initialize according to the NAT action. */ 780b57dc7c1SPaul Blakey err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 781b57dc7c1SPaul Blakey /* Action is set up to establish a new 782b57dc7c1SPaul Blakey * mapping. 783b57dc7c1SPaul Blakey */ 784b57dc7c1SPaul Blakey ? nf_nat_setup_info(ct, range, maniptype) 785b57dc7c1SPaul Blakey : nf_nat_alloc_null_binding(ct, hooknum); 786b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 787b57dc7c1SPaul Blakey goto out; 788b57dc7c1SPaul Blakey } 789b57dc7c1SPaul Blakey break; 790b57dc7c1SPaul Blakey 791b57dc7c1SPaul Blakey case IP_CT_ESTABLISHED: 792b57dc7c1SPaul Blakey case IP_CT_ESTABLISHED_REPLY: 793b57dc7c1SPaul Blakey break; 794b57dc7c1SPaul Blakey 795b57dc7c1SPaul Blakey default: 796b57dc7c1SPaul Blakey err = NF_DROP; 797b57dc7c1SPaul Blakey goto out; 798b57dc7c1SPaul Blakey } 799b57dc7c1SPaul Blakey 800b57dc7c1SPaul Blakey err = nf_nat_packet(ct, ctinfo, hooknum, skb); 801b57dc7c1SPaul Blakey out: 802b57dc7c1SPaul Blakey return err; 803b57dc7c1SPaul Blakey } 804b57dc7c1SPaul Blakey #endif /* CONFIG_NF_NAT */ 805b57dc7c1SPaul Blakey 806b57dc7c1SPaul Blakey static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 807b57dc7c1SPaul Blakey { 808b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 809b57dc7c1SPaul Blakey u32 new_mark; 810b57dc7c1SPaul Blakey 811b57dc7c1SPaul Blakey if (!mask) 812b57dc7c1SPaul Blakey return; 813b57dc7c1SPaul Blakey 814b57dc7c1SPaul Blakey new_mark = mark | (ct->mark & ~(mask)); 815b57dc7c1SPaul Blakey if (ct->mark != new_mark) { 816b57dc7c1SPaul Blakey ct->mark = new_mark; 817b57dc7c1SPaul Blakey if (nf_ct_is_confirmed(ct)) 818b57dc7c1SPaul Blakey nf_conntrack_event_cache(IPCT_MARK, ct); 819b57dc7c1SPaul Blakey } 820b57dc7c1SPaul Blakey #endif 821b57dc7c1SPaul Blakey } 822b57dc7c1SPaul Blakey 823b57dc7c1SPaul Blakey static void tcf_ct_act_set_labels(struct nf_conn *ct, 824b57dc7c1SPaul Blakey u32 *labels, 825b57dc7c1SPaul Blakey u32 *labels_m) 826b57dc7c1SPaul Blakey { 827b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 828c593642cSPankaj Bharadiya size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 829b57dc7c1SPaul Blakey 830b57dc7c1SPaul Blakey if (!memchr_inv(labels_m, 0, labels_sz)) 831b57dc7c1SPaul Blakey return; 832b57dc7c1SPaul Blakey 833b57dc7c1SPaul Blakey nf_connlabels_replace(ct, labels, labels_m, 4); 834b57dc7c1SPaul Blakey #endif 835b57dc7c1SPaul Blakey } 836b57dc7c1SPaul Blakey 837b57dc7c1SPaul Blakey static int tcf_ct_act_nat(struct sk_buff *skb, 838b57dc7c1SPaul Blakey struct nf_conn *ct, 839b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo, 840b57dc7c1SPaul Blakey int ct_action, 841b57dc7c1SPaul Blakey struct nf_nat_range2 *range, 842b57dc7c1SPaul Blakey bool commit) 843b57dc7c1SPaul Blakey { 844b57dc7c1SPaul Blakey #if IS_ENABLED(CONFIG_NF_NAT) 84595219afbSAaron Conole int err; 846b57dc7c1SPaul Blakey enum nf_nat_manip_type maniptype; 847b57dc7c1SPaul Blakey 848b57dc7c1SPaul Blakey if (!(ct_action & TCA_CT_ACT_NAT)) 849b57dc7c1SPaul Blakey return NF_ACCEPT; 850b57dc7c1SPaul Blakey 851b57dc7c1SPaul Blakey /* Add NAT extension if not confirmed yet. */ 852b57dc7c1SPaul Blakey if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 853b57dc7c1SPaul Blakey return NF_DROP; /* Can't NAT. */ 854b57dc7c1SPaul Blakey 855b57dc7c1SPaul Blakey if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) && 856b57dc7c1SPaul Blakey (ctinfo != IP_CT_RELATED || commit)) { 857b57dc7c1SPaul Blakey /* NAT an established or related connection like before. */ 858b57dc7c1SPaul Blakey if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 859b57dc7c1SPaul Blakey /* This is the REPLY direction for a connection 860b57dc7c1SPaul Blakey * for which NAT was applied in the forward 861b57dc7c1SPaul Blakey * direction. Do the reverse NAT. 862b57dc7c1SPaul Blakey */ 863b57dc7c1SPaul Blakey maniptype = ct->status & IPS_SRC_NAT 864b57dc7c1SPaul Blakey ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 865b57dc7c1SPaul Blakey else 866b57dc7c1SPaul Blakey maniptype = ct->status & IPS_SRC_NAT 867b57dc7c1SPaul Blakey ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 868b57dc7c1SPaul Blakey } else if (ct_action & TCA_CT_ACT_NAT_SRC) { 869b57dc7c1SPaul Blakey maniptype = NF_NAT_MANIP_SRC; 870b57dc7c1SPaul Blakey } else if (ct_action & TCA_CT_ACT_NAT_DST) { 871b57dc7c1SPaul Blakey maniptype = NF_NAT_MANIP_DST; 872b57dc7c1SPaul Blakey } else { 873b57dc7c1SPaul Blakey return NF_ACCEPT; 874b57dc7c1SPaul Blakey } 875b57dc7c1SPaul Blakey 87695219afbSAaron Conole err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 87795219afbSAaron Conole if (err == NF_ACCEPT && 87895219afbSAaron Conole ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) { 87995219afbSAaron Conole if (maniptype == NF_NAT_MANIP_SRC) 88095219afbSAaron Conole maniptype = NF_NAT_MANIP_DST; 88195219afbSAaron Conole else 88295219afbSAaron Conole maniptype = NF_NAT_MANIP_SRC; 88395219afbSAaron Conole 88495219afbSAaron Conole err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 88595219afbSAaron Conole } 88695219afbSAaron Conole return err; 887b57dc7c1SPaul Blakey #else 888b57dc7c1SPaul Blakey return NF_ACCEPT; 889b57dc7c1SPaul Blakey #endif 890b57dc7c1SPaul Blakey } 891b57dc7c1SPaul Blakey 892b57dc7c1SPaul Blakey static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 893b57dc7c1SPaul Blakey struct tcf_result *res) 894b57dc7c1SPaul Blakey { 895b57dc7c1SPaul Blakey struct net *net = dev_net(skb->dev); 896b57dc7c1SPaul Blakey bool cached, commit, clear, force; 897b57dc7c1SPaul Blakey enum ip_conntrack_info ctinfo; 898b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 899b57dc7c1SPaul Blakey struct nf_conn *tmpl = NULL; 900b57dc7c1SPaul Blakey struct nf_hook_state state; 901b57dc7c1SPaul Blakey int nh_ofs, err, retval; 902b57dc7c1SPaul Blakey struct tcf_ct_params *p; 90346475bb2SPaul Blakey bool skip_add = false; 904b57dc7c1SPaul Blakey struct nf_conn *ct; 905b57dc7c1SPaul Blakey u8 family; 906b57dc7c1SPaul Blakey 907b57dc7c1SPaul Blakey p = rcu_dereference_bh(c->params); 908b57dc7c1SPaul Blakey 909b57dc7c1SPaul Blakey retval = READ_ONCE(c->tcf_action); 910b57dc7c1SPaul Blakey commit = p->ct_action & TCA_CT_ACT_COMMIT; 911b57dc7c1SPaul Blakey clear = p->ct_action & TCA_CT_ACT_CLEAR; 912b57dc7c1SPaul Blakey force = p->ct_action & TCA_CT_ACT_FORCE; 913b57dc7c1SPaul Blakey tmpl = p->tmpl; 914b57dc7c1SPaul Blakey 915b57dc7c1SPaul Blakey if (clear) { 916b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 917b57dc7c1SPaul Blakey if (ct) { 918b57dc7c1SPaul Blakey nf_conntrack_put(&ct->ct_general); 919b57dc7c1SPaul Blakey nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 920b57dc7c1SPaul Blakey } 921b57dc7c1SPaul Blakey 922b57dc7c1SPaul Blakey goto out; 923b57dc7c1SPaul Blakey } 924b57dc7c1SPaul Blakey 925b57dc7c1SPaul Blakey family = tcf_ct_skb_nf_family(skb); 926b57dc7c1SPaul Blakey if (family == NFPROTO_UNSPEC) 927b57dc7c1SPaul Blakey goto drop; 928b57dc7c1SPaul Blakey 929b57dc7c1SPaul Blakey /* The conntrack module expects to be working at L3. 930b57dc7c1SPaul Blakey * We also try to pull the IPv4/6 header to linear area 931b57dc7c1SPaul Blakey */ 932b57dc7c1SPaul Blakey nh_ofs = skb_network_offset(skb); 933b57dc7c1SPaul Blakey skb_pull_rcsum(skb, nh_ofs); 934b57dc7c1SPaul Blakey err = tcf_ct_handle_fragments(net, skb, family, p->zone); 935b57dc7c1SPaul Blakey if (err == -EINPROGRESS) { 936b57dc7c1SPaul Blakey retval = TC_ACT_STOLEN; 937b57dc7c1SPaul Blakey goto out; 938b57dc7c1SPaul Blakey } 939b57dc7c1SPaul Blakey if (err) 940b57dc7c1SPaul Blakey goto drop; 941b57dc7c1SPaul Blakey 942b57dc7c1SPaul Blakey err = tcf_ct_skb_network_trim(skb, family); 943b57dc7c1SPaul Blakey if (err) 944b57dc7c1SPaul Blakey goto drop; 945b57dc7c1SPaul Blakey 946b57dc7c1SPaul Blakey /* If we are recirculating packets to match on ct fields and 947b57dc7c1SPaul Blakey * committing with a separate ct action, then we don't need to 948b57dc7c1SPaul Blakey * actually run the packet through conntrack twice unless it's for a 949b57dc7c1SPaul Blakey * different zone. 950b57dc7c1SPaul Blakey */ 951b57dc7c1SPaul Blakey cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force); 952b57dc7c1SPaul Blakey if (!cached) { 95346475bb2SPaul Blakey if (!commit && tcf_ct_flow_table_lookup(p, skb, family)) { 95446475bb2SPaul Blakey skip_add = true; 95546475bb2SPaul Blakey goto do_nat; 95646475bb2SPaul Blakey } 95746475bb2SPaul Blakey 958b57dc7c1SPaul Blakey /* Associate skb with specified zone. */ 959b57dc7c1SPaul Blakey if (tmpl) { 960b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 961b57dc7c1SPaul Blakey if (skb_nfct(skb)) 962b57dc7c1SPaul Blakey nf_conntrack_put(skb_nfct(skb)); 963b57dc7c1SPaul Blakey nf_conntrack_get(&tmpl->ct_general); 964b57dc7c1SPaul Blakey nf_ct_set(skb, tmpl, IP_CT_NEW); 965b57dc7c1SPaul Blakey } 966b57dc7c1SPaul Blakey 967b57dc7c1SPaul Blakey state.hook = NF_INET_PRE_ROUTING; 968b57dc7c1SPaul Blakey state.net = net; 969b57dc7c1SPaul Blakey state.pf = family; 970b57dc7c1SPaul Blakey err = nf_conntrack_in(skb, &state); 971b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 972b57dc7c1SPaul Blakey goto out_push; 973b57dc7c1SPaul Blakey } 974b57dc7c1SPaul Blakey 97546475bb2SPaul Blakey do_nat: 976b57dc7c1SPaul Blakey ct = nf_ct_get(skb, &ctinfo); 977b57dc7c1SPaul Blakey if (!ct) 978b57dc7c1SPaul Blakey goto out_push; 979b57dc7c1SPaul Blakey nf_ct_deliver_cached_events(ct); 980b57dc7c1SPaul Blakey 981b57dc7c1SPaul Blakey err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 982b57dc7c1SPaul Blakey if (err != NF_ACCEPT) 983b57dc7c1SPaul Blakey goto drop; 984b57dc7c1SPaul Blakey 985b57dc7c1SPaul Blakey if (commit) { 986b57dc7c1SPaul Blakey tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 987b57dc7c1SPaul Blakey tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 988b57dc7c1SPaul Blakey 989b57dc7c1SPaul Blakey /* This will take care of sending queued events 990b57dc7c1SPaul Blakey * even if the connection is already confirmed. 991b57dc7c1SPaul Blakey */ 992b57dc7c1SPaul Blakey nf_conntrack_confirm(skb); 99346475bb2SPaul Blakey } else if (!skip_add) { 99464ff70b8SPaul Blakey tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 99546475bb2SPaul Blakey } 99664ff70b8SPaul Blakey 997b57dc7c1SPaul Blakey out_push: 998b57dc7c1SPaul Blakey skb_push_rcsum(skb, nh_ofs); 999b57dc7c1SPaul Blakey 1000b57dc7c1SPaul Blakey out: 10015e1ad95bSVlad Buslov tcf_action_update_bstats(&c->common, skb); 1002b57dc7c1SPaul Blakey return retval; 1003b57dc7c1SPaul Blakey 1004b57dc7c1SPaul Blakey drop: 100526b537a8SVlad Buslov tcf_action_inc_drop_qstats(&c->common); 1006b57dc7c1SPaul Blakey return TC_ACT_SHOT; 1007b57dc7c1SPaul Blakey } 1008b57dc7c1SPaul Blakey 1009b57dc7c1SPaul Blakey static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1010b57dc7c1SPaul Blakey [TCA_CT_ACTION] = { .type = NLA_U16 }, 1011b57dc7c1SPaul Blakey [TCA_CT_PARMS] = { .type = NLA_EXACT_LEN, .len = sizeof(struct tc_ct) }, 1012b57dc7c1SPaul Blakey [TCA_CT_ZONE] = { .type = NLA_U16 }, 1013b57dc7c1SPaul Blakey [TCA_CT_MARK] = { .type = NLA_U32 }, 1014b57dc7c1SPaul Blakey [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1015b57dc7c1SPaul Blakey [TCA_CT_LABELS] = { .type = NLA_BINARY, 1016b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1017b57dc7c1SPaul Blakey [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1018b57dc7c1SPaul Blakey .len = 128 / BITS_PER_BYTE }, 1019b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1020b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 1021b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV6_MIN] = { .type = NLA_EXACT_LEN, 1022b57dc7c1SPaul Blakey .len = sizeof(struct in6_addr) }, 1023b57dc7c1SPaul Blakey [TCA_CT_NAT_IPV6_MAX] = { .type = NLA_EXACT_LEN, 1024b57dc7c1SPaul Blakey .len = sizeof(struct in6_addr) }, 1025b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1026b57dc7c1SPaul Blakey [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1027b57dc7c1SPaul Blakey }; 1028b57dc7c1SPaul Blakey 1029b57dc7c1SPaul Blakey static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1030b57dc7c1SPaul Blakey struct tc_ct *parm, 1031b57dc7c1SPaul Blakey struct nlattr **tb, 1032b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1033b57dc7c1SPaul Blakey { 1034b57dc7c1SPaul Blakey struct nf_nat_range2 *range; 1035b57dc7c1SPaul Blakey 1036b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1037b57dc7c1SPaul Blakey return 0; 1038b57dc7c1SPaul Blakey 1039b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_NAT)) { 1040b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1041b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1042b57dc7c1SPaul Blakey } 1043b57dc7c1SPaul Blakey 1044b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1045b57dc7c1SPaul Blakey return 0; 1046b57dc7c1SPaul Blakey 1047b57dc7c1SPaul Blakey if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1048b57dc7c1SPaul Blakey (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1049b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1050b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1051b57dc7c1SPaul Blakey } 1052b57dc7c1SPaul Blakey 1053b57dc7c1SPaul Blakey range = &p->range; 1054b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_IPV4_MIN]) { 1055b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1056b57dc7c1SPaul Blakey 1057b57dc7c1SPaul Blakey p->ipv4_range = true; 1058b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1059b57dc7c1SPaul Blakey range->min_addr.ip = 1060b57dc7c1SPaul Blakey nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1061b57dc7c1SPaul Blakey 1062b57dc7c1SPaul Blakey range->max_addr.ip = max_attr ? 1063b57dc7c1SPaul Blakey nla_get_in_addr(max_attr) : 1064b57dc7c1SPaul Blakey range->min_addr.ip; 1065b57dc7c1SPaul Blakey } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1066b57dc7c1SPaul Blakey struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1067b57dc7c1SPaul Blakey 1068b57dc7c1SPaul Blakey p->ipv4_range = false; 1069b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_MAP_IPS; 1070b57dc7c1SPaul Blakey range->min_addr.in6 = 1071b57dc7c1SPaul Blakey nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1072b57dc7c1SPaul Blakey 1073b57dc7c1SPaul Blakey range->max_addr.in6 = max_attr ? 1074b57dc7c1SPaul Blakey nla_get_in6_addr(max_attr) : 1075b57dc7c1SPaul Blakey range->min_addr.in6; 1076b57dc7c1SPaul Blakey } 1077b57dc7c1SPaul Blakey 1078b57dc7c1SPaul Blakey if (tb[TCA_CT_NAT_PORT_MIN]) { 1079b57dc7c1SPaul Blakey range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1080b57dc7c1SPaul Blakey range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1081b57dc7c1SPaul Blakey 1082b57dc7c1SPaul Blakey range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1083b57dc7c1SPaul Blakey nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1084b57dc7c1SPaul Blakey range->min_proto.all; 1085b57dc7c1SPaul Blakey } 1086b57dc7c1SPaul Blakey 1087b57dc7c1SPaul Blakey return 0; 1088b57dc7c1SPaul Blakey } 1089b57dc7c1SPaul Blakey 1090b57dc7c1SPaul Blakey static void tcf_ct_set_key_val(struct nlattr **tb, 1091b57dc7c1SPaul Blakey void *val, int val_type, 1092b57dc7c1SPaul Blakey void *mask, int mask_type, 1093b57dc7c1SPaul Blakey int len) 1094b57dc7c1SPaul Blakey { 1095b57dc7c1SPaul Blakey if (!tb[val_type]) 1096b57dc7c1SPaul Blakey return; 1097b57dc7c1SPaul Blakey nla_memcpy(val, tb[val_type], len); 1098b57dc7c1SPaul Blakey 1099b57dc7c1SPaul Blakey if (!mask) 1100b57dc7c1SPaul Blakey return; 1101b57dc7c1SPaul Blakey 1102b57dc7c1SPaul Blakey if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1103b57dc7c1SPaul Blakey memset(mask, 0xff, len); 1104b57dc7c1SPaul Blakey else 1105b57dc7c1SPaul Blakey nla_memcpy(mask, tb[mask_type], len); 1106b57dc7c1SPaul Blakey } 1107b57dc7c1SPaul Blakey 1108b57dc7c1SPaul Blakey static int tcf_ct_fill_params(struct net *net, 1109b57dc7c1SPaul Blakey struct tcf_ct_params *p, 1110b57dc7c1SPaul Blakey struct tc_ct *parm, 1111b57dc7c1SPaul Blakey struct nlattr **tb, 1112b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1113b57dc7c1SPaul Blakey { 1114b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1115b57dc7c1SPaul Blakey struct nf_conntrack_zone zone; 1116b57dc7c1SPaul Blakey struct nf_conn *tmpl; 1117b57dc7c1SPaul Blakey int err; 1118b57dc7c1SPaul Blakey 1119b57dc7c1SPaul Blakey p->zone = NF_CT_DEFAULT_ZONE_ID; 1120b57dc7c1SPaul Blakey 1121b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1122b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1123b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1124b57dc7c1SPaul Blakey sizeof(p->ct_action)); 1125b57dc7c1SPaul Blakey 1126b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1127b57dc7c1SPaul Blakey return 0; 1128b57dc7c1SPaul Blakey 1129b57dc7c1SPaul Blakey err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1130b57dc7c1SPaul Blakey if (err) 1131b57dc7c1SPaul Blakey return err; 1132b57dc7c1SPaul Blakey 1133b57dc7c1SPaul Blakey if (tb[TCA_CT_MARK]) { 1134b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1135b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1136b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1137b57dc7c1SPaul Blakey } 1138b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1139b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1140b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1141b57dc7c1SPaul Blakey sizeof(p->mark)); 1142b57dc7c1SPaul Blakey } 1143b57dc7c1SPaul Blakey 1144b57dc7c1SPaul Blakey if (tb[TCA_CT_LABELS]) { 1145b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1146b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1147b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1148b57dc7c1SPaul Blakey } 1149b57dc7c1SPaul Blakey 1150b57dc7c1SPaul Blakey if (!tn->labels) { 1151b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1152b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1153b57dc7c1SPaul Blakey } 1154b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1155b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1156b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1157b57dc7c1SPaul Blakey sizeof(p->labels)); 1158b57dc7c1SPaul Blakey } 1159b57dc7c1SPaul Blakey 1160b57dc7c1SPaul Blakey if (tb[TCA_CT_ZONE]) { 1161b57dc7c1SPaul Blakey if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1162b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1163b57dc7c1SPaul Blakey return -EOPNOTSUPP; 1164b57dc7c1SPaul Blakey } 1165b57dc7c1SPaul Blakey 1166b57dc7c1SPaul Blakey tcf_ct_set_key_val(tb, 1167b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1168b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1169b57dc7c1SPaul Blakey sizeof(p->zone)); 1170b57dc7c1SPaul Blakey } 1171b57dc7c1SPaul Blakey 1172b57dc7c1SPaul Blakey if (p->zone == NF_CT_DEFAULT_ZONE_ID) 1173b57dc7c1SPaul Blakey return 0; 1174b57dc7c1SPaul Blakey 1175b57dc7c1SPaul Blakey nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1176b57dc7c1SPaul Blakey tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1177b57dc7c1SPaul Blakey if (!tmpl) { 1178b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1179b57dc7c1SPaul Blakey return -ENOMEM; 1180b57dc7c1SPaul Blakey } 1181b57dc7c1SPaul Blakey __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1182b57dc7c1SPaul Blakey nf_conntrack_get(&tmpl->ct_general); 1183b57dc7c1SPaul Blakey p->tmpl = tmpl; 1184b57dc7c1SPaul Blakey 1185b57dc7c1SPaul Blakey return 0; 1186b57dc7c1SPaul Blakey } 1187b57dc7c1SPaul Blakey 1188b57dc7c1SPaul Blakey static int tcf_ct_init(struct net *net, struct nlattr *nla, 1189b57dc7c1SPaul Blakey struct nlattr *est, struct tc_action **a, 1190b57dc7c1SPaul Blakey int replace, int bind, bool rtnl_held, 1191abbb0d33SVlad Buslov struct tcf_proto *tp, u32 flags, 1192b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1193b57dc7c1SPaul Blakey { 1194b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1195b57dc7c1SPaul Blakey struct tcf_ct_params *params = NULL; 1196b57dc7c1SPaul Blakey struct nlattr *tb[TCA_CT_MAX + 1]; 1197b57dc7c1SPaul Blakey struct tcf_chain *goto_ch = NULL; 1198b57dc7c1SPaul Blakey struct tc_ct *parm; 1199b57dc7c1SPaul Blakey struct tcf_ct *c; 1200b57dc7c1SPaul Blakey int err, res = 0; 12017be8ef2cSDmytro Linkin u32 index; 1202b57dc7c1SPaul Blakey 1203b57dc7c1SPaul Blakey if (!nla) { 1204b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1205b57dc7c1SPaul Blakey return -EINVAL; 1206b57dc7c1SPaul Blakey } 1207b57dc7c1SPaul Blakey 1208b57dc7c1SPaul Blakey err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1209b57dc7c1SPaul Blakey if (err < 0) 1210b57dc7c1SPaul Blakey return err; 1211b57dc7c1SPaul Blakey 1212b57dc7c1SPaul Blakey if (!tb[TCA_CT_PARMS]) { 1213b57dc7c1SPaul Blakey NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1214b57dc7c1SPaul Blakey return -EINVAL; 1215b57dc7c1SPaul Blakey } 1216b57dc7c1SPaul Blakey parm = nla_data(tb[TCA_CT_PARMS]); 12177be8ef2cSDmytro Linkin index = parm->index; 12187be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind); 1219b57dc7c1SPaul Blakey if (err < 0) 1220b57dc7c1SPaul Blakey return err; 1221b57dc7c1SPaul Blakey 1222b57dc7c1SPaul Blakey if (!err) { 1223e3822678SVlad Buslov err = tcf_idr_create_from_flags(tn, index, est, a, 1224e3822678SVlad Buslov &act_ct_ops, bind, flags); 1225b57dc7c1SPaul Blakey if (err) { 12267be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 1227b57dc7c1SPaul Blakey return err; 1228b57dc7c1SPaul Blakey } 1229b57dc7c1SPaul Blakey res = ACT_P_CREATED; 1230b57dc7c1SPaul Blakey } else { 1231b57dc7c1SPaul Blakey if (bind) 1232b57dc7c1SPaul Blakey return 0; 1233b57dc7c1SPaul Blakey 1234b57dc7c1SPaul Blakey if (!replace) { 1235b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1236b57dc7c1SPaul Blakey return -EEXIST; 1237b57dc7c1SPaul Blakey } 1238b57dc7c1SPaul Blakey } 1239b57dc7c1SPaul Blakey err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1240b57dc7c1SPaul Blakey if (err < 0) 1241b57dc7c1SPaul Blakey goto cleanup; 1242b57dc7c1SPaul Blakey 1243b57dc7c1SPaul Blakey c = to_ct(*a); 1244b57dc7c1SPaul Blakey 1245b57dc7c1SPaul Blakey params = kzalloc(sizeof(*params), GFP_KERNEL); 1246b57dc7c1SPaul Blakey if (unlikely(!params)) { 1247b57dc7c1SPaul Blakey err = -ENOMEM; 1248b57dc7c1SPaul Blakey goto cleanup; 1249b57dc7c1SPaul Blakey } 1250b57dc7c1SPaul Blakey 1251b57dc7c1SPaul Blakey err = tcf_ct_fill_params(net, params, parm, tb, extack); 1252b57dc7c1SPaul Blakey if (err) 1253b57dc7c1SPaul Blakey goto cleanup; 1254b57dc7c1SPaul Blakey 1255c34b961aSPaul Blakey err = tcf_ct_flow_table_get(params); 1256c34b961aSPaul Blakey if (err) 1257c34b961aSPaul Blakey goto cleanup; 1258c34b961aSPaul Blakey 1259b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1260b57dc7c1SPaul Blakey goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1261445d3749SPaul E. McKenney params = rcu_replace_pointer(c->params, params, 1262445d3749SPaul E. McKenney lockdep_is_held(&c->tcf_lock)); 1263b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1264b57dc7c1SPaul Blakey 1265b57dc7c1SPaul Blakey if (goto_ch) 1266b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 1267b57dc7c1SPaul Blakey if (params) 1268b57dc7c1SPaul Blakey kfree_rcu(params, rcu); 1269b57dc7c1SPaul Blakey if (res == ACT_P_CREATED) 1270b57dc7c1SPaul Blakey tcf_idr_insert(tn, *a); 1271b57dc7c1SPaul Blakey 1272b57dc7c1SPaul Blakey return res; 1273b57dc7c1SPaul Blakey 1274b57dc7c1SPaul Blakey cleanup: 1275b57dc7c1SPaul Blakey if (goto_ch) 1276b57dc7c1SPaul Blakey tcf_chain_put_by_act(goto_ch); 1277b57dc7c1SPaul Blakey kfree(params); 1278b57dc7c1SPaul Blakey tcf_idr_release(*a, bind); 1279b57dc7c1SPaul Blakey return err; 1280b57dc7c1SPaul Blakey } 1281b57dc7c1SPaul Blakey 1282b57dc7c1SPaul Blakey static void tcf_ct_cleanup(struct tc_action *a) 1283b57dc7c1SPaul Blakey { 1284b57dc7c1SPaul Blakey struct tcf_ct_params *params; 1285b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1286b57dc7c1SPaul Blakey 1287b57dc7c1SPaul Blakey params = rcu_dereference_protected(c->params, 1); 1288b57dc7c1SPaul Blakey if (params) 1289b57dc7c1SPaul Blakey call_rcu(¶ms->rcu, tcf_ct_params_free); 1290b57dc7c1SPaul Blakey } 1291b57dc7c1SPaul Blakey 1292b57dc7c1SPaul Blakey static int tcf_ct_dump_key_val(struct sk_buff *skb, 1293b57dc7c1SPaul Blakey void *val, int val_type, 1294b57dc7c1SPaul Blakey void *mask, int mask_type, 1295b57dc7c1SPaul Blakey int len) 1296b57dc7c1SPaul Blakey { 1297b57dc7c1SPaul Blakey int err; 1298b57dc7c1SPaul Blakey 1299b57dc7c1SPaul Blakey if (mask && !memchr_inv(mask, 0, len)) 1300b57dc7c1SPaul Blakey return 0; 1301b57dc7c1SPaul Blakey 1302b57dc7c1SPaul Blakey err = nla_put(skb, val_type, len, val); 1303b57dc7c1SPaul Blakey if (err) 1304b57dc7c1SPaul Blakey return err; 1305b57dc7c1SPaul Blakey 1306b57dc7c1SPaul Blakey if (mask_type != TCA_CT_UNSPEC) { 1307b57dc7c1SPaul Blakey err = nla_put(skb, mask_type, len, mask); 1308b57dc7c1SPaul Blakey if (err) 1309b57dc7c1SPaul Blakey return err; 1310b57dc7c1SPaul Blakey } 1311b57dc7c1SPaul Blakey 1312b57dc7c1SPaul Blakey return 0; 1313b57dc7c1SPaul Blakey } 1314b57dc7c1SPaul Blakey 1315b57dc7c1SPaul Blakey static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1316b57dc7c1SPaul Blakey { 1317b57dc7c1SPaul Blakey struct nf_nat_range2 *range = &p->range; 1318b57dc7c1SPaul Blakey 1319b57dc7c1SPaul Blakey if (!(p->ct_action & TCA_CT_ACT_NAT)) 1320b57dc7c1SPaul Blakey return 0; 1321b57dc7c1SPaul Blakey 1322b57dc7c1SPaul Blakey if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1323b57dc7c1SPaul Blakey return 0; 1324b57dc7c1SPaul Blakey 1325b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1326b57dc7c1SPaul Blakey if (p->ipv4_range) { 1327b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1328b57dc7c1SPaul Blakey range->min_addr.ip)) 1329b57dc7c1SPaul Blakey return -1; 1330b57dc7c1SPaul Blakey if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1331b57dc7c1SPaul Blakey range->max_addr.ip)) 1332b57dc7c1SPaul Blakey return -1; 1333b57dc7c1SPaul Blakey } else { 1334b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1335b57dc7c1SPaul Blakey &range->min_addr.in6)) 1336b57dc7c1SPaul Blakey return -1; 1337b57dc7c1SPaul Blakey if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1338b57dc7c1SPaul Blakey &range->max_addr.in6)) 1339b57dc7c1SPaul Blakey return -1; 1340b57dc7c1SPaul Blakey } 1341b57dc7c1SPaul Blakey } 1342b57dc7c1SPaul Blakey 1343b57dc7c1SPaul Blakey if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1344b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1345b57dc7c1SPaul Blakey range->min_proto.all)) 1346b57dc7c1SPaul Blakey return -1; 1347b57dc7c1SPaul Blakey if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1348b57dc7c1SPaul Blakey range->max_proto.all)) 1349b57dc7c1SPaul Blakey return -1; 1350b57dc7c1SPaul Blakey } 1351b57dc7c1SPaul Blakey 1352b57dc7c1SPaul Blakey return 0; 1353b57dc7c1SPaul Blakey } 1354b57dc7c1SPaul Blakey 1355b57dc7c1SPaul Blakey static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1356b57dc7c1SPaul Blakey int bind, int ref) 1357b57dc7c1SPaul Blakey { 1358b57dc7c1SPaul Blakey unsigned char *b = skb_tail_pointer(skb); 1359b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1360b57dc7c1SPaul Blakey struct tcf_ct_params *p; 1361b57dc7c1SPaul Blakey 1362b57dc7c1SPaul Blakey struct tc_ct opt = { 1363b57dc7c1SPaul Blakey .index = c->tcf_index, 1364b57dc7c1SPaul Blakey .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1365b57dc7c1SPaul Blakey .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1366b57dc7c1SPaul Blakey }; 1367b57dc7c1SPaul Blakey struct tcf_t t; 1368b57dc7c1SPaul Blakey 1369b57dc7c1SPaul Blakey spin_lock_bh(&c->tcf_lock); 1370b57dc7c1SPaul Blakey p = rcu_dereference_protected(c->params, 1371b57dc7c1SPaul Blakey lockdep_is_held(&c->tcf_lock)); 1372b57dc7c1SPaul Blakey opt.action = c->tcf_action; 1373b57dc7c1SPaul Blakey 1374b57dc7c1SPaul Blakey if (tcf_ct_dump_key_val(skb, 1375b57dc7c1SPaul Blakey &p->ct_action, TCA_CT_ACTION, 1376b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1377b57dc7c1SPaul Blakey sizeof(p->ct_action))) 1378b57dc7c1SPaul Blakey goto nla_put_failure; 1379b57dc7c1SPaul Blakey 1380b57dc7c1SPaul Blakey if (p->ct_action & TCA_CT_ACT_CLEAR) 1381b57dc7c1SPaul Blakey goto skip_dump; 1382b57dc7c1SPaul Blakey 1383b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1384b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1385b57dc7c1SPaul Blakey &p->mark, TCA_CT_MARK, 1386b57dc7c1SPaul Blakey &p->mark_mask, TCA_CT_MARK_MASK, 1387b57dc7c1SPaul Blakey sizeof(p->mark))) 1388b57dc7c1SPaul Blakey goto nla_put_failure; 1389b57dc7c1SPaul Blakey 1390b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1391b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1392b57dc7c1SPaul Blakey p->labels, TCA_CT_LABELS, 1393b57dc7c1SPaul Blakey p->labels_mask, TCA_CT_LABELS_MASK, 1394b57dc7c1SPaul Blakey sizeof(p->labels))) 1395b57dc7c1SPaul Blakey goto nla_put_failure; 1396b57dc7c1SPaul Blakey 1397b57dc7c1SPaul Blakey if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1398b57dc7c1SPaul Blakey tcf_ct_dump_key_val(skb, 1399b57dc7c1SPaul Blakey &p->zone, TCA_CT_ZONE, 1400b57dc7c1SPaul Blakey NULL, TCA_CT_UNSPEC, 1401b57dc7c1SPaul Blakey sizeof(p->zone))) 1402b57dc7c1SPaul Blakey goto nla_put_failure; 1403b57dc7c1SPaul Blakey 1404b57dc7c1SPaul Blakey if (tcf_ct_dump_nat(skb, p)) 1405b57dc7c1SPaul Blakey goto nla_put_failure; 1406b57dc7c1SPaul Blakey 1407b57dc7c1SPaul Blakey skip_dump: 1408b57dc7c1SPaul Blakey if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1409b57dc7c1SPaul Blakey goto nla_put_failure; 1410b57dc7c1SPaul Blakey 1411b57dc7c1SPaul Blakey tcf_tm_dump(&t, &c->tcf_tm); 1412b57dc7c1SPaul Blakey if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1413b57dc7c1SPaul Blakey goto nla_put_failure; 1414b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1415b57dc7c1SPaul Blakey 1416b57dc7c1SPaul Blakey return skb->len; 1417b57dc7c1SPaul Blakey nla_put_failure: 1418b57dc7c1SPaul Blakey spin_unlock_bh(&c->tcf_lock); 1419b57dc7c1SPaul Blakey nlmsg_trim(skb, b); 1420b57dc7c1SPaul Blakey return -1; 1421b57dc7c1SPaul Blakey } 1422b57dc7c1SPaul Blakey 1423b57dc7c1SPaul Blakey static int tcf_ct_walker(struct net *net, struct sk_buff *skb, 1424b57dc7c1SPaul Blakey struct netlink_callback *cb, int type, 1425b57dc7c1SPaul Blakey const struct tc_action_ops *ops, 1426b57dc7c1SPaul Blakey struct netlink_ext_ack *extack) 1427b57dc7c1SPaul Blakey { 1428b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1429b57dc7c1SPaul Blakey 1430b57dc7c1SPaul Blakey return tcf_generic_walker(tn, skb, cb, type, ops, extack); 1431b57dc7c1SPaul Blakey } 1432b57dc7c1SPaul Blakey 1433b57dc7c1SPaul Blakey static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index) 1434b57dc7c1SPaul Blakey { 1435b57dc7c1SPaul Blakey struct tc_action_net *tn = net_generic(net, ct_net_id); 1436b57dc7c1SPaul Blakey 1437b57dc7c1SPaul Blakey return tcf_idr_search(tn, a, index); 1438b57dc7c1SPaul Blakey } 1439b57dc7c1SPaul Blakey 1440b57dc7c1SPaul Blakey static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets, 1441b57dc7c1SPaul Blakey u64 lastuse, bool hw) 1442b57dc7c1SPaul Blakey { 1443b57dc7c1SPaul Blakey struct tcf_ct *c = to_ct(a); 1444b57dc7c1SPaul Blakey 1445c8ecebd0SVlad Buslov tcf_action_update_stats(a, bytes, packets, false, hw); 1446b57dc7c1SPaul Blakey c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1447b57dc7c1SPaul Blakey } 1448b57dc7c1SPaul Blakey 1449b57dc7c1SPaul Blakey static struct tc_action_ops act_ct_ops = { 1450b57dc7c1SPaul Blakey .kind = "ct", 1451b57dc7c1SPaul Blakey .id = TCA_ID_CT, 1452b57dc7c1SPaul Blakey .owner = THIS_MODULE, 1453b57dc7c1SPaul Blakey .act = tcf_ct_act, 1454b57dc7c1SPaul Blakey .dump = tcf_ct_dump, 1455b57dc7c1SPaul Blakey .init = tcf_ct_init, 1456b57dc7c1SPaul Blakey .cleanup = tcf_ct_cleanup, 1457b57dc7c1SPaul Blakey .walk = tcf_ct_walker, 1458b57dc7c1SPaul Blakey .lookup = tcf_ct_search, 1459b57dc7c1SPaul Blakey .stats_update = tcf_stats_update, 1460b57dc7c1SPaul Blakey .size = sizeof(struct tcf_ct), 1461b57dc7c1SPaul Blakey }; 1462b57dc7c1SPaul Blakey 1463b57dc7c1SPaul Blakey static __net_init int ct_init_net(struct net *net) 1464b57dc7c1SPaul Blakey { 1465c593642cSPankaj Bharadiya unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1466b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1467b57dc7c1SPaul Blakey 1468b57dc7c1SPaul Blakey if (nf_connlabels_get(net, n_bits - 1)) { 1469b57dc7c1SPaul Blakey tn->labels = false; 1470b57dc7c1SPaul Blakey pr_err("act_ct: Failed to set connlabels length"); 1471b57dc7c1SPaul Blakey } else { 1472b57dc7c1SPaul Blakey tn->labels = true; 1473b57dc7c1SPaul Blakey } 1474b57dc7c1SPaul Blakey 1475981471bdSCong Wang return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1476b57dc7c1SPaul Blakey } 1477b57dc7c1SPaul Blakey 1478b57dc7c1SPaul Blakey static void __net_exit ct_exit_net(struct list_head *net_list) 1479b57dc7c1SPaul Blakey { 1480b57dc7c1SPaul Blakey struct net *net; 1481b57dc7c1SPaul Blakey 1482b57dc7c1SPaul Blakey rtnl_lock(); 1483b57dc7c1SPaul Blakey list_for_each_entry(net, net_list, exit_list) { 1484b57dc7c1SPaul Blakey struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1485b57dc7c1SPaul Blakey 1486b57dc7c1SPaul Blakey if (tn->labels) 1487b57dc7c1SPaul Blakey nf_connlabels_put(net); 1488b57dc7c1SPaul Blakey } 1489b57dc7c1SPaul Blakey rtnl_unlock(); 1490b57dc7c1SPaul Blakey 1491b57dc7c1SPaul Blakey tc_action_net_exit(net_list, ct_net_id); 1492b57dc7c1SPaul Blakey } 1493b57dc7c1SPaul Blakey 1494b57dc7c1SPaul Blakey static struct pernet_operations ct_net_ops = { 1495b57dc7c1SPaul Blakey .init = ct_init_net, 1496b57dc7c1SPaul Blakey .exit_batch = ct_exit_net, 1497b57dc7c1SPaul Blakey .id = &ct_net_id, 1498b57dc7c1SPaul Blakey .size = sizeof(struct tc_ct_action_net), 1499b57dc7c1SPaul Blakey }; 1500b57dc7c1SPaul Blakey 1501b57dc7c1SPaul Blakey static int __init ct_init_module(void) 1502b57dc7c1SPaul Blakey { 1503c34b961aSPaul Blakey int err; 1504c34b961aSPaul Blakey 1505c34b961aSPaul Blakey act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1506c34b961aSPaul Blakey if (!act_ct_wq) 1507c34b961aSPaul Blakey return -ENOMEM; 1508c34b961aSPaul Blakey 1509c34b961aSPaul Blakey err = tcf_ct_flow_tables_init(); 1510c34b961aSPaul Blakey if (err) 1511c34b961aSPaul Blakey goto err_tbl_init; 1512c34b961aSPaul Blakey 1513c34b961aSPaul Blakey err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1514c34b961aSPaul Blakey if (err) 1515c34b961aSPaul Blakey goto err_register; 1516c34b961aSPaul Blakey 1517c34b961aSPaul Blakey return 0; 1518c34b961aSPaul Blakey 1519c34b961aSPaul Blakey err_tbl_init: 1520c34b961aSPaul Blakey destroy_workqueue(act_ct_wq); 1521c34b961aSPaul Blakey err_register: 1522c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 1523c34b961aSPaul Blakey return err; 1524b57dc7c1SPaul Blakey } 1525b57dc7c1SPaul Blakey 1526b57dc7c1SPaul Blakey static void __exit ct_cleanup_module(void) 1527b57dc7c1SPaul Blakey { 1528b57dc7c1SPaul Blakey tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1529c34b961aSPaul Blakey tcf_ct_flow_tables_uninit(); 1530c34b961aSPaul Blakey destroy_workqueue(act_ct_wq); 1531b57dc7c1SPaul Blakey } 1532b57dc7c1SPaul Blakey 1533b57dc7c1SPaul Blakey module_init(ct_init_module); 1534b57dc7c1SPaul Blakey module_exit(ct_cleanup_module); 1535b57dc7c1SPaul Blakey MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1536b57dc7c1SPaul Blakey MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1537b57dc7c1SPaul Blakey MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1538b57dc7c1SPaul Blakey MODULE_DESCRIPTION("Connection tracking action"); 1539b57dc7c1SPaul Blakey MODULE_LICENSE("GPL v2"); 1540b57dc7c1SPaul Blakey 1541