0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/kernel.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/rtnetlink.h>
0015 #include <linux/pkt_cls.h>
0016 #include <linux/ip.h>
0017 #include <linux/ipv6.h>
0018 #include <linux/rhashtable.h>
0019 #include <net/netlink.h>
0020 #include <net/pkt_sched.h>
0021 #include <net/pkt_cls.h>
0022 #include <net/act_api.h>
0023 #include <net/ip.h>
0024 #include <net/ipv6_frag.h>
0025 #include <uapi/linux/tc_act/tc_ct.h>
0026 #include <net/tc_act/tc_ct.h>
0027
0028 #include <net/netfilter/nf_flow_table.h>
0029 #include <net/netfilter/nf_conntrack.h>
0030 #include <net/netfilter/nf_conntrack_core.h>
0031 #include <net/netfilter/nf_conntrack_zones.h>
0032 #include <net/netfilter/nf_conntrack_helper.h>
0033 #include <net/netfilter/nf_conntrack_acct.h>
0034 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
0035 #include <net/netfilter/nf_conntrack_act_ct.h>
0036 #include <uapi/linux/netfilter/nf_nat.h>
0037
0038 static struct workqueue_struct *act_ct_wq;
0039 static struct rhashtable zones_ht;
0040 static DEFINE_MUTEX(zones_mutex);
0041
0042 struct tcf_ct_flow_table {
0043 struct rhash_head node;
0044
0045 struct rcu_work rwork;
0046 struct nf_flowtable nf_ft;
0047 refcount_t ref;
0048 u16 zone;
0049
0050 bool dying;
0051 };
0052
0053 static const struct rhashtable_params zones_params = {
0054 .head_offset = offsetof(struct tcf_ct_flow_table, node),
0055 .key_offset = offsetof(struct tcf_ct_flow_table, zone),
0056 .key_len = sizeof_field(struct tcf_ct_flow_table, zone),
0057 .automatic_shrinking = true,
0058 };
0059
0060 static struct flow_action_entry *
0061 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action)
0062 {
0063 int i = flow_action->num_entries++;
0064
0065 return &flow_action->entries[i];
0066 }
0067
0068 static void tcf_ct_add_mangle_action(struct flow_action *action,
0069 enum flow_action_mangle_base htype,
0070 u32 offset,
0071 u32 mask,
0072 u32 val)
0073 {
0074 struct flow_action_entry *entry;
0075
0076 entry = tcf_ct_flow_table_flow_action_get_next(action);
0077 entry->id = FLOW_ACTION_MANGLE;
0078 entry->mangle.htype = htype;
0079 entry->mangle.mask = ~mask;
0080 entry->mangle.offset = offset;
0081 entry->mangle.val = val;
0082 }
0083
0084
0085
0086
0087
0088 static void
0089 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple,
0090 struct nf_conntrack_tuple target,
0091 struct flow_action *action)
0092 {
0093 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
0094 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
0095 offsetof(struct iphdr, saddr),
0096 0xFFFFFFFF,
0097 be32_to_cpu(target.src.u3.ip));
0098 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
0099 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4,
0100 offsetof(struct iphdr, daddr),
0101 0xFFFFFFFF,
0102 be32_to_cpu(target.dst.u3.ip));
0103 }
0104
0105 static void
0106 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action,
0107 union nf_inet_addr *addr,
0108 u32 offset)
0109 {
0110 int i;
0111
0112 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++)
0113 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
0114 i * sizeof(u32) + offset,
0115 0xFFFFFFFF, be32_to_cpu(addr->ip6[i]));
0116 }
0117
0118 static void
0119 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple,
0120 struct nf_conntrack_tuple target,
0121 struct flow_action *action)
0122 {
0123 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3)))
0124 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3,
0125 offsetof(struct ipv6hdr,
0126 saddr));
0127 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3)))
0128 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3,
0129 offsetof(struct ipv6hdr,
0130 daddr));
0131 }
0132
0133 static void
0134 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple,
0135 struct nf_conntrack_tuple target,
0136 struct flow_action *action)
0137 {
0138 __be16 target_src = target.src.u.tcp.port;
0139 __be16 target_dst = target.dst.u.tcp.port;
0140
0141 if (target_src != tuple->src.u.tcp.port)
0142 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
0143 offsetof(struct tcphdr, source),
0144 0xFFFF, be16_to_cpu(target_src));
0145 if (target_dst != tuple->dst.u.tcp.port)
0146 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP,
0147 offsetof(struct tcphdr, dest),
0148 0xFFFF, be16_to_cpu(target_dst));
0149 }
0150
0151 static void
0152 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,
0153 struct nf_conntrack_tuple target,
0154 struct flow_action *action)
0155 {
0156 __be16 target_src = target.src.u.udp.port;
0157 __be16 target_dst = target.dst.u.udp.port;
0158
0159 if (target_src != tuple->src.u.udp.port)
0160 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
0161 offsetof(struct udphdr, source),
0162 0xFFFF, be16_to_cpu(target_src));
0163 if (target_dst != tuple->dst.u.udp.port)
0164 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP,
0165 offsetof(struct udphdr, dest),
0166 0xFFFF, be16_to_cpu(target_dst));
0167 }
0168
0169 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,
0170 enum ip_conntrack_dir dir,
0171 struct flow_action *action)
0172 {
0173 struct nf_conn_labels *ct_labels;
0174 struct flow_action_entry *entry;
0175 enum ip_conntrack_info ctinfo;
0176 u32 *act_ct_labels;
0177
0178 entry = tcf_ct_flow_table_flow_action_get_next(action);
0179 entry->id = FLOW_ACTION_CT_METADATA;
0180 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
0181 entry->ct_metadata.mark = ct->mark;
0182 #endif
0183 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
0184 IP_CT_ESTABLISHED_REPLY;
0185
0186 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo;
0187 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL;
0188
0189 act_ct_labels = entry->ct_metadata.labels;
0190 ct_labels = nf_ct_labels_find(ct);
0191 if (ct_labels)
0192 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE);
0193 else
0194 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE);
0195 }
0196
0197 static int tcf_ct_flow_table_add_action_nat(struct net *net,
0198 struct nf_conn *ct,
0199 enum ip_conntrack_dir dir,
0200 struct flow_action *action)
0201 {
0202 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
0203 struct nf_conntrack_tuple target;
0204
0205 if (!(ct->status & IPS_NAT_MASK))
0206 return 0;
0207
0208 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
0209
0210 switch (tuple->src.l3num) {
0211 case NFPROTO_IPV4:
0212 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target,
0213 action);
0214 break;
0215 case NFPROTO_IPV6:
0216 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target,
0217 action);
0218 break;
0219 default:
0220 return -EOPNOTSUPP;
0221 }
0222
0223 switch (nf_ct_protonum(ct)) {
0224 case IPPROTO_TCP:
0225 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action);
0226 break;
0227 case IPPROTO_UDP:
0228 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action);
0229 break;
0230 default:
0231 return -EOPNOTSUPP;
0232 }
0233
0234 return 0;
0235 }
0236
0237 static int tcf_ct_flow_table_fill_actions(struct net *net,
0238 const struct flow_offload *flow,
0239 enum flow_offload_tuple_dir tdir,
0240 struct nf_flow_rule *flow_rule)
0241 {
0242 struct flow_action *action = &flow_rule->rule->action;
0243 int num_entries = action->num_entries;
0244 struct nf_conn *ct = flow->ct;
0245 enum ip_conntrack_dir dir;
0246 int i, err;
0247
0248 switch (tdir) {
0249 case FLOW_OFFLOAD_DIR_ORIGINAL:
0250 dir = IP_CT_DIR_ORIGINAL;
0251 break;
0252 case FLOW_OFFLOAD_DIR_REPLY:
0253 dir = IP_CT_DIR_REPLY;
0254 break;
0255 default:
0256 return -EOPNOTSUPP;
0257 }
0258
0259 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action);
0260 if (err)
0261 goto err_nat;
0262
0263 tcf_ct_flow_table_add_action_meta(ct, dir, action);
0264 return 0;
0265
0266 err_nat:
0267
0268 for (i = num_entries; i < action->num_entries; i++)
0269 memset(&action->entries[i], 0, sizeof(action->entries[i]));
0270 action->num_entries = num_entries;
0271
0272 return err;
0273 }
0274
0275 static struct nf_flowtable_type flowtable_ct = {
0276 .action = tcf_ct_flow_table_fill_actions,
0277 .owner = THIS_MODULE,
0278 };
0279
0280 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params)
0281 {
0282 struct tcf_ct_flow_table *ct_ft;
0283 int err = -ENOMEM;
0284
0285 mutex_lock(&zones_mutex);
0286 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params);
0287 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref))
0288 goto out_unlock;
0289
0290 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL);
0291 if (!ct_ft)
0292 goto err_alloc;
0293 refcount_set(&ct_ft->ref, 1);
0294
0295 ct_ft->zone = params->zone;
0296 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params);
0297 if (err)
0298 goto err_insert;
0299
0300 ct_ft->nf_ft.type = &flowtable_ct;
0301 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD |
0302 NF_FLOWTABLE_COUNTER;
0303 err = nf_flow_table_init(&ct_ft->nf_ft);
0304 if (err)
0305 goto err_init;
0306 write_pnet(&ct_ft->nf_ft.net, net);
0307
0308 __module_get(THIS_MODULE);
0309 out_unlock:
0310 params->ct_ft = ct_ft;
0311 params->nf_ft = &ct_ft->nf_ft;
0312 mutex_unlock(&zones_mutex);
0313
0314 return 0;
0315
0316 err_init:
0317 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
0318 err_insert:
0319 kfree(ct_ft);
0320 err_alloc:
0321 mutex_unlock(&zones_mutex);
0322 return err;
0323 }
0324
0325 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
0326 {
0327 struct flow_block_cb *block_cb, *tmp_cb;
0328 struct tcf_ct_flow_table *ct_ft;
0329 struct flow_block *block;
0330
0331 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table,
0332 rwork);
0333 nf_flow_table_free(&ct_ft->nf_ft);
0334
0335
0336 block = &ct_ft->nf_ft.flow_block;
0337 down_write(&ct_ft->nf_ft.flow_block_lock);
0338 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) {
0339 list_del(&block_cb->list);
0340 flow_block_cb_free(block_cb);
0341 }
0342 up_write(&ct_ft->nf_ft.flow_block_lock);
0343 kfree(ct_ft);
0344
0345 module_put(THIS_MODULE);
0346 }
0347
0348 static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
0349 {
0350 struct tcf_ct_flow_table *ct_ft = params->ct_ft;
0351
0352 if (refcount_dec_and_test(¶ms->ct_ft->ref)) {
0353 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
0354 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
0355 queue_rcu_work(act_ct_wq, &ct_ft->rwork);
0356 }
0357 }
0358
0359 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,
0360 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir)
0361 {
0362 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC;
0363 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir];
0364 }
0365
0366 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft,
0367 struct nf_conn *ct,
0368 bool tcp)
0369 {
0370 struct nf_conn_act_ct_ext *act_ct_ext;
0371 struct flow_offload *entry;
0372 int err;
0373
0374 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
0375 return;
0376
0377 entry = flow_offload_alloc(ct);
0378 if (!entry) {
0379 WARN_ON_ONCE(1);
0380 goto err_alloc;
0381 }
0382
0383 if (tcp) {
0384 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
0385 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
0386 }
0387
0388 act_ct_ext = nf_conn_act_ct_ext_find(ct);
0389 if (act_ct_ext) {
0390 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL);
0391 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY);
0392 }
0393
0394 err = flow_offload_add(&ct_ft->nf_ft, entry);
0395 if (err)
0396 goto err_add;
0397
0398 return;
0399
0400 err_add:
0401 flow_offload_free(entry);
0402 err_alloc:
0403 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
0404 }
0405
0406 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,
0407 struct nf_conn *ct,
0408 enum ip_conntrack_info ctinfo)
0409 {
0410 bool tcp = false;
0411
0412 if ((ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) ||
0413 !test_bit(IPS_ASSURED_BIT, &ct->status))
0414 return;
0415
0416 switch (nf_ct_protonum(ct)) {
0417 case IPPROTO_TCP:
0418 tcp = true;
0419 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
0420 return;
0421 break;
0422 case IPPROTO_UDP:
0423 break;
0424 #ifdef CONFIG_NF_CT_PROTO_GRE
0425 case IPPROTO_GRE: {
0426 struct nf_conntrack_tuple *tuple;
0427
0428 if (ct->status & IPS_NAT_MASK)
0429 return;
0430 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
0431
0432 if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
0433 return;
0434 break;
0435 }
0436 #endif
0437 default:
0438 return;
0439 }
0440
0441 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
0442 ct->status & IPS_SEQ_ADJUST)
0443 return;
0444
0445 tcf_ct_flow_table_add(ct_ft, ct, tcp);
0446 }
0447
0448 static bool
0449 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb,
0450 struct flow_offload_tuple *tuple,
0451 struct tcphdr **tcph)
0452 {
0453 struct flow_ports *ports;
0454 unsigned int thoff;
0455 struct iphdr *iph;
0456 size_t hdrsize;
0457 u8 ipproto;
0458
0459 if (!pskb_network_may_pull(skb, sizeof(*iph)))
0460 return false;
0461
0462 iph = ip_hdr(skb);
0463 thoff = iph->ihl * 4;
0464
0465 if (ip_is_fragment(iph) ||
0466 unlikely(thoff != sizeof(struct iphdr)))
0467 return false;
0468
0469 ipproto = iph->protocol;
0470 switch (ipproto) {
0471 case IPPROTO_TCP:
0472 hdrsize = sizeof(struct tcphdr);
0473 break;
0474 case IPPROTO_UDP:
0475 hdrsize = sizeof(*ports);
0476 break;
0477 #ifdef CONFIG_NF_CT_PROTO_GRE
0478 case IPPROTO_GRE:
0479 hdrsize = sizeof(struct gre_base_hdr);
0480 break;
0481 #endif
0482 default:
0483 return false;
0484 }
0485
0486 if (iph->ttl <= 1)
0487 return false;
0488
0489 if (!pskb_network_may_pull(skb, thoff + hdrsize))
0490 return false;
0491
0492 switch (ipproto) {
0493 case IPPROTO_TCP:
0494 *tcph = (void *)(skb_network_header(skb) + thoff);
0495 fallthrough;
0496 case IPPROTO_UDP:
0497 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
0498 tuple->src_port = ports->source;
0499 tuple->dst_port = ports->dest;
0500 break;
0501 case IPPROTO_GRE: {
0502 struct gre_base_hdr *greh;
0503
0504 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
0505 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
0506 return false;
0507 break;
0508 }
0509 }
0510
0511 iph = ip_hdr(skb);
0512
0513 tuple->src_v4.s_addr = iph->saddr;
0514 tuple->dst_v4.s_addr = iph->daddr;
0515 tuple->l3proto = AF_INET;
0516 tuple->l4proto = ipproto;
0517
0518 return true;
0519 }
0520
0521 static bool
0522 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb,
0523 struct flow_offload_tuple *tuple,
0524 struct tcphdr **tcph)
0525 {
0526 struct flow_ports *ports;
0527 struct ipv6hdr *ip6h;
0528 unsigned int thoff;
0529 size_t hdrsize;
0530 u8 nexthdr;
0531
0532 if (!pskb_network_may_pull(skb, sizeof(*ip6h)))
0533 return false;
0534
0535 ip6h = ipv6_hdr(skb);
0536 thoff = sizeof(*ip6h);
0537
0538 nexthdr = ip6h->nexthdr;
0539 switch (nexthdr) {
0540 case IPPROTO_TCP:
0541 hdrsize = sizeof(struct tcphdr);
0542 break;
0543 case IPPROTO_UDP:
0544 hdrsize = sizeof(*ports);
0545 break;
0546 #ifdef CONFIG_NF_CT_PROTO_GRE
0547 case IPPROTO_GRE:
0548 hdrsize = sizeof(struct gre_base_hdr);
0549 break;
0550 #endif
0551 default:
0552 return false;
0553 }
0554
0555 if (ip6h->hop_limit <= 1)
0556 return false;
0557
0558 if (!pskb_network_may_pull(skb, thoff + hdrsize))
0559 return false;
0560
0561 switch (nexthdr) {
0562 case IPPROTO_TCP:
0563 *tcph = (void *)(skb_network_header(skb) + thoff);
0564 fallthrough;
0565 case IPPROTO_UDP:
0566 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
0567 tuple->src_port = ports->source;
0568 tuple->dst_port = ports->dest;
0569 break;
0570 case IPPROTO_GRE: {
0571 struct gre_base_hdr *greh;
0572
0573 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
0574 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
0575 return false;
0576 break;
0577 }
0578 }
0579
0580 ip6h = ipv6_hdr(skb);
0581
0582 tuple->src_v6 = ip6h->saddr;
0583 tuple->dst_v6 = ip6h->daddr;
0584 tuple->l3proto = AF_INET6;
0585 tuple->l4proto = nexthdr;
0586
0587 return true;
0588 }
0589
0590 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p,
0591 struct sk_buff *skb,
0592 u8 family)
0593 {
0594 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft;
0595 struct flow_offload_tuple_rhash *tuplehash;
0596 struct flow_offload_tuple tuple = {};
0597 enum ip_conntrack_info ctinfo;
0598 struct tcphdr *tcph = NULL;
0599 struct flow_offload *flow;
0600 struct nf_conn *ct;
0601 u8 dir;
0602
0603 switch (family) {
0604 case NFPROTO_IPV4:
0605 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph))
0606 return false;
0607 break;
0608 case NFPROTO_IPV6:
0609 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph))
0610 return false;
0611 break;
0612 default:
0613 return false;
0614 }
0615
0616 tuplehash = flow_offload_lookup(nf_ft, &tuple);
0617 if (!tuplehash)
0618 return false;
0619
0620 dir = tuplehash->tuple.dir;
0621 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
0622 ct = flow->ct;
0623
0624 if (tcph && (unlikely(tcph->fin || tcph->rst))) {
0625 flow_offload_teardown(flow);
0626 return false;
0627 }
0628
0629 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED :
0630 IP_CT_ESTABLISHED_REPLY;
0631
0632 flow_offload_refresh(nf_ft, flow);
0633 nf_conntrack_get(&ct->ct_general);
0634 nf_ct_set(skb, ct, ctinfo);
0635 if (nf_ft->flags & NF_FLOWTABLE_COUNTER)
0636 nf_ct_acct_update(ct, dir, skb->len);
0637
0638 return true;
0639 }
0640
0641 static int tcf_ct_flow_tables_init(void)
0642 {
0643 return rhashtable_init(&zones_ht, &zones_params);
0644 }
0645
0646 static void tcf_ct_flow_tables_uninit(void)
0647 {
0648 rhashtable_destroy(&zones_ht);
0649 }
0650
0651 static struct tc_action_ops act_ct_ops;
0652 static unsigned int ct_net_id;
0653
0654 struct tc_ct_action_net {
0655 struct tc_action_net tn;
0656 bool labels;
0657 };
0658
0659
0660 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
0661 u16 zone_id, bool force)
0662 {
0663 enum ip_conntrack_info ctinfo;
0664 struct nf_conn *ct;
0665
0666 ct = nf_ct_get(skb, &ctinfo);
0667 if (!ct)
0668 return false;
0669 if (!net_eq(net, read_pnet(&ct->ct_net)))
0670 goto drop_ct;
0671 if (nf_ct_zone(ct)->id != zone_id)
0672 goto drop_ct;
0673
0674
0675 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
0676 if (nf_ct_is_confirmed(ct))
0677 nf_ct_kill(ct);
0678
0679 goto drop_ct;
0680 }
0681
0682 return true;
0683
0684 drop_ct:
0685 nf_ct_put(ct);
0686 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
0687
0688 return false;
0689 }
0690
0691
0692
0693
0694
0695
0696
0697 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family)
0698 {
0699 unsigned int len;
0700 int err;
0701
0702 switch (family) {
0703 case NFPROTO_IPV4:
0704 len = ntohs(ip_hdr(skb)->tot_len);
0705 break;
0706 case NFPROTO_IPV6:
0707 len = sizeof(struct ipv6hdr)
0708 + ntohs(ipv6_hdr(skb)->payload_len);
0709 break;
0710 default:
0711 len = skb->len;
0712 }
0713
0714 err = pskb_trim_rcsum(skb, len);
0715
0716 return err;
0717 }
0718
0719 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb)
0720 {
0721 u8 family = NFPROTO_UNSPEC;
0722
0723 switch (skb_protocol(skb, true)) {
0724 case htons(ETH_P_IP):
0725 family = NFPROTO_IPV4;
0726 break;
0727 case htons(ETH_P_IPV6):
0728 family = NFPROTO_IPV6;
0729 break;
0730 default:
0731 break;
0732 }
0733
0734 return family;
0735 }
0736
0737 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag)
0738 {
0739 unsigned int len;
0740
0741 len = skb_network_offset(skb) + sizeof(struct iphdr);
0742 if (unlikely(skb->len < len))
0743 return -EINVAL;
0744 if (unlikely(!pskb_may_pull(skb, len)))
0745 return -ENOMEM;
0746
0747 *frag = ip_is_fragment(ip_hdr(skb));
0748 return 0;
0749 }
0750
0751 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag)
0752 {
0753 unsigned int flags = 0, len, payload_ofs = 0;
0754 unsigned short frag_off;
0755 int nexthdr;
0756
0757 len = skb_network_offset(skb) + sizeof(struct ipv6hdr);
0758 if (unlikely(skb->len < len))
0759 return -EINVAL;
0760 if (unlikely(!pskb_may_pull(skb, len)))
0761 return -ENOMEM;
0762
0763 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags);
0764 if (unlikely(nexthdr < 0))
0765 return -EPROTO;
0766
0767 *frag = flags & IP6_FH_F_FRAG;
0768 return 0;
0769 }
0770
0771 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
0772 u8 family, u16 zone, bool *defrag)
0773 {
0774 enum ip_conntrack_info ctinfo;
0775 struct nf_conn *ct;
0776 int err = 0;
0777 bool frag;
0778 u16 mru;
0779
0780
0781 ct = nf_ct_get(skb, &ctinfo);
0782 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED)
0783 return 0;
0784
0785 if (family == NFPROTO_IPV4)
0786 err = tcf_ct_ipv4_is_fragment(skb, &frag);
0787 else
0788 err = tcf_ct_ipv6_is_fragment(skb, &frag);
0789 if (err || !frag)
0790 return err;
0791
0792 skb_get(skb);
0793 mru = tc_skb_cb(skb)->mru;
0794
0795 if (family == NFPROTO_IPV4) {
0796 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
0797
0798 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
0799 local_bh_disable();
0800 err = ip_defrag(net, skb, user);
0801 local_bh_enable();
0802 if (err && err != -EINPROGRESS)
0803 return err;
0804
0805 if (!err) {
0806 *defrag = true;
0807 mru = IPCB(skb)->frag_max_size;
0808 }
0809 } else {
0810 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
0811 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
0812
0813 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
0814 err = nf_ct_frag6_gather(net, skb, user);
0815 if (err && err != -EINPROGRESS)
0816 goto out_free;
0817
0818 if (!err) {
0819 *defrag = true;
0820 mru = IP6CB(skb)->frag_max_size;
0821 }
0822 #else
0823 err = -EOPNOTSUPP;
0824 goto out_free;
0825 #endif
0826 }
0827
0828 if (err != -EINPROGRESS)
0829 tc_skb_cb(skb)->mru = mru;
0830 skb_clear_hash(skb);
0831 skb->ignore_df = 1;
0832 return err;
0833
0834 out_free:
0835 kfree_skb(skb);
0836 return err;
0837 }
0838
0839 static void tcf_ct_params_free(struct rcu_head *head)
0840 {
0841 struct tcf_ct_params *params = container_of(head,
0842 struct tcf_ct_params, rcu);
0843
0844 tcf_ct_flow_table_put(params);
0845
0846 if (params->tmpl)
0847 nf_ct_put(params->tmpl);
0848 kfree(params);
0849 }
0850
0851 #if IS_ENABLED(CONFIG_NF_NAT)
0852
0853
0854
0855
0856 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
0857 enum ip_conntrack_info ctinfo,
0858 const struct nf_nat_range2 *range,
0859 enum nf_nat_manip_type maniptype)
0860 {
0861 __be16 proto = skb_protocol(skb, true);
0862 int hooknum, err = NF_ACCEPT;
0863
0864
0865 if (maniptype == NF_NAT_MANIP_SRC)
0866 hooknum = NF_INET_LOCAL_IN;
0867 else
0868 hooknum = NF_INET_LOCAL_OUT;
0869
0870 switch (ctinfo) {
0871 case IP_CT_RELATED:
0872 case IP_CT_RELATED_REPLY:
0873 if (proto == htons(ETH_P_IP) &&
0874 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
0875 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
0876 hooknum))
0877 err = NF_DROP;
0878 goto out;
0879 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) {
0880 __be16 frag_off;
0881 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
0882 int hdrlen = ipv6_skip_exthdr(skb,
0883 sizeof(struct ipv6hdr),
0884 &nexthdr, &frag_off);
0885
0886 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
0887 if (!nf_nat_icmpv6_reply_translation(skb, ct,
0888 ctinfo,
0889 hooknum,
0890 hdrlen))
0891 err = NF_DROP;
0892 goto out;
0893 }
0894 }
0895
0896 fallthrough;
0897 case IP_CT_NEW:
0898
0899
0900
0901 if (!nf_nat_initialized(ct, maniptype)) {
0902
0903 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
0904
0905
0906
0907 ? nf_nat_setup_info(ct, range, maniptype)
0908 : nf_nat_alloc_null_binding(ct, hooknum);
0909 if (err != NF_ACCEPT)
0910 goto out;
0911 }
0912 break;
0913
0914 case IP_CT_ESTABLISHED:
0915 case IP_CT_ESTABLISHED_REPLY:
0916 break;
0917
0918 default:
0919 err = NF_DROP;
0920 goto out;
0921 }
0922
0923 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
0924 if (err == NF_ACCEPT) {
0925 if (maniptype == NF_NAT_MANIP_SRC)
0926 tc_skb_cb(skb)->post_ct_snat = 1;
0927 if (maniptype == NF_NAT_MANIP_DST)
0928 tc_skb_cb(skb)->post_ct_dnat = 1;
0929 }
0930 out:
0931 return err;
0932 }
0933 #endif
0934
0935 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask)
0936 {
0937 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
0938 u32 new_mark;
0939
0940 if (!mask)
0941 return;
0942
0943 new_mark = mark | (ct->mark & ~(mask));
0944 if (ct->mark != new_mark) {
0945 ct->mark = new_mark;
0946 if (nf_ct_is_confirmed(ct))
0947 nf_conntrack_event_cache(IPCT_MARK, ct);
0948 }
0949 #endif
0950 }
0951
0952 static void tcf_ct_act_set_labels(struct nf_conn *ct,
0953 u32 *labels,
0954 u32 *labels_m)
0955 {
0956 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)
0957 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels);
0958
0959 if (!memchr_inv(labels_m, 0, labels_sz))
0960 return;
0961
0962 nf_connlabels_replace(ct, labels, labels_m, 4);
0963 #endif
0964 }
0965
0966 static int tcf_ct_act_nat(struct sk_buff *skb,
0967 struct nf_conn *ct,
0968 enum ip_conntrack_info ctinfo,
0969 int ct_action,
0970 struct nf_nat_range2 *range,
0971 bool commit)
0972 {
0973 #if IS_ENABLED(CONFIG_NF_NAT)
0974 int err;
0975 enum nf_nat_manip_type maniptype;
0976
0977 if (!(ct_action & TCA_CT_ACT_NAT))
0978 return NF_ACCEPT;
0979
0980
0981 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
0982 return NF_DROP;
0983
0984 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) &&
0985 (ctinfo != IP_CT_RELATED || commit)) {
0986
0987 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
0988
0989
0990
0991
0992 maniptype = ct->status & IPS_SRC_NAT
0993 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
0994 else
0995 maniptype = ct->status & IPS_SRC_NAT
0996 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
0997 } else if (ct_action & TCA_CT_ACT_NAT_SRC) {
0998 maniptype = NF_NAT_MANIP_SRC;
0999 } else if (ct_action & TCA_CT_ACT_NAT_DST) {
1000 maniptype = NF_NAT_MANIP_DST;
1001 } else {
1002 return NF_ACCEPT;
1003 }
1004
1005 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
1006 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
1007 if (ct->status & IPS_SRC_NAT) {
1008 if (maniptype == NF_NAT_MANIP_SRC)
1009 maniptype = NF_NAT_MANIP_DST;
1010 else
1011 maniptype = NF_NAT_MANIP_SRC;
1012
1013 err = ct_nat_execute(skb, ct, ctinfo, range,
1014 maniptype);
1015 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
1016 err = ct_nat_execute(skb, ct, ctinfo, NULL,
1017 NF_NAT_MANIP_SRC);
1018 }
1019 }
1020 return err;
1021 #else
1022 return NF_ACCEPT;
1023 #endif
1024 }
1025
1026 static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
1027 struct tcf_result *res)
1028 {
1029 struct net *net = dev_net(skb->dev);
1030 bool cached, commit, clear, force;
1031 enum ip_conntrack_info ctinfo;
1032 struct tcf_ct *c = to_ct(a);
1033 struct nf_conn *tmpl = NULL;
1034 struct nf_hook_state state;
1035 int nh_ofs, err, retval;
1036 struct tcf_ct_params *p;
1037 bool skip_add = false;
1038 bool defrag = false;
1039 struct nf_conn *ct;
1040 u8 family;
1041
1042 p = rcu_dereference_bh(c->params);
1043
1044 retval = READ_ONCE(c->tcf_action);
1045 commit = p->ct_action & TCA_CT_ACT_COMMIT;
1046 clear = p->ct_action & TCA_CT_ACT_CLEAR;
1047 force = p->ct_action & TCA_CT_ACT_FORCE;
1048 tmpl = p->tmpl;
1049
1050 tcf_lastuse_update(&c->tcf_tm);
1051 tcf_action_update_bstats(&c->common, skb);
1052
1053 if (clear) {
1054 tc_skb_cb(skb)->post_ct = false;
1055 ct = nf_ct_get(skb, &ctinfo);
1056 if (ct) {
1057 nf_ct_put(ct);
1058 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1059 }
1060
1061 goto out_clear;
1062 }
1063
1064 family = tcf_ct_skb_nf_family(skb);
1065 if (family == NFPROTO_UNSPEC)
1066 goto drop;
1067
1068
1069
1070
1071 nh_ofs = skb_network_offset(skb);
1072 skb_pull_rcsum(skb, nh_ofs);
1073 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag);
1074 if (err == -EINPROGRESS) {
1075 retval = TC_ACT_STOLEN;
1076 goto out_clear;
1077 }
1078 if (err)
1079 goto drop;
1080
1081 err = tcf_ct_skb_network_trim(skb, family);
1082 if (err)
1083 goto drop;
1084
1085
1086
1087
1088
1089
1090 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
1091 if (!cached) {
1092 if (tcf_ct_flow_table_lookup(p, skb, family)) {
1093 skip_add = true;
1094 goto do_nat;
1095 }
1096
1097
1098 if (tmpl) {
1099 nf_conntrack_put(skb_nfct(skb));
1100 nf_conntrack_get(&tmpl->ct_general);
1101 nf_ct_set(skb, tmpl, IP_CT_NEW);
1102 }
1103
1104 state.hook = NF_INET_PRE_ROUTING;
1105 state.net = net;
1106 state.pf = family;
1107 err = nf_conntrack_in(skb, &state);
1108 if (err != NF_ACCEPT)
1109 goto out_push;
1110 }
1111
1112 do_nat:
1113 ct = nf_ct_get(skb, &ctinfo);
1114 if (!ct)
1115 goto out_push;
1116 nf_ct_deliver_cached_events(ct);
1117 nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
1118
1119 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit);
1120 if (err != NF_ACCEPT)
1121 goto drop;
1122
1123 if (commit) {
1124 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
1125 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
1126
1127 if (!nf_ct_is_confirmed(ct))
1128 nf_conn_act_ct_ext_add(ct);
1129
1130
1131
1132
1133 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1134 goto drop;
1135 }
1136
1137 if (!skip_add)
1138 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo);
1139
1140 out_push:
1141 skb_push_rcsum(skb, nh_ofs);
1142
1143 tc_skb_cb(skb)->post_ct = true;
1144 tc_skb_cb(skb)->zone = p->zone;
1145 out_clear:
1146 if (defrag)
1147 qdisc_skb_cb(skb)->pkt_len = skb->len;
1148 return retval;
1149
1150 drop:
1151 tcf_action_inc_drop_qstats(&c->common);
1152 return TC_ACT_SHOT;
1153 }
1154
1155 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = {
1156 [TCA_CT_ACTION] = { .type = NLA_U16 },
1157 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)),
1158 [TCA_CT_ZONE] = { .type = NLA_U16 },
1159 [TCA_CT_MARK] = { .type = NLA_U32 },
1160 [TCA_CT_MARK_MASK] = { .type = NLA_U32 },
1161 [TCA_CT_LABELS] = { .type = NLA_BINARY,
1162 .len = 128 / BITS_PER_BYTE },
1163 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY,
1164 .len = 128 / BITS_PER_BYTE },
1165 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 },
1166 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 },
1167 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1168 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
1169 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 },
1170 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 },
1171 };
1172
1173 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p,
1174 struct tc_ct *parm,
1175 struct nlattr **tb,
1176 struct netlink_ext_ack *extack)
1177 {
1178 struct nf_nat_range2 *range;
1179
1180 if (!(p->ct_action & TCA_CT_ACT_NAT))
1181 return 0;
1182
1183 if (!IS_ENABLED(CONFIG_NF_NAT)) {
1184 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel");
1185 return -EOPNOTSUPP;
1186 }
1187
1188 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1189 return 0;
1190
1191 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) &&
1192 (p->ct_action & TCA_CT_ACT_NAT_DST)) {
1193 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time");
1194 return -EOPNOTSUPP;
1195 }
1196
1197 range = &p->range;
1198 if (tb[TCA_CT_NAT_IPV4_MIN]) {
1199 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX];
1200
1201 p->ipv4_range = true;
1202 range->flags |= NF_NAT_RANGE_MAP_IPS;
1203 range->min_addr.ip =
1204 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]);
1205
1206 range->max_addr.ip = max_attr ?
1207 nla_get_in_addr(max_attr) :
1208 range->min_addr.ip;
1209 } else if (tb[TCA_CT_NAT_IPV6_MIN]) {
1210 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX];
1211
1212 p->ipv4_range = false;
1213 range->flags |= NF_NAT_RANGE_MAP_IPS;
1214 range->min_addr.in6 =
1215 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]);
1216
1217 range->max_addr.in6 = max_attr ?
1218 nla_get_in6_addr(max_attr) :
1219 range->min_addr.in6;
1220 }
1221
1222 if (tb[TCA_CT_NAT_PORT_MIN]) {
1223 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1224 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]);
1225
1226 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ?
1227 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) :
1228 range->min_proto.all;
1229 }
1230
1231 return 0;
1232 }
1233
1234 static void tcf_ct_set_key_val(struct nlattr **tb,
1235 void *val, int val_type,
1236 void *mask, int mask_type,
1237 int len)
1238 {
1239 if (!tb[val_type])
1240 return;
1241 nla_memcpy(val, tb[val_type], len);
1242
1243 if (!mask)
1244 return;
1245
1246 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type])
1247 memset(mask, 0xff, len);
1248 else
1249 nla_memcpy(mask, tb[mask_type], len);
1250 }
1251
1252 static int tcf_ct_fill_params(struct net *net,
1253 struct tcf_ct_params *p,
1254 struct tc_ct *parm,
1255 struct nlattr **tb,
1256 struct netlink_ext_ack *extack)
1257 {
1258 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1259 struct nf_conntrack_zone zone;
1260 struct nf_conn *tmpl;
1261 int err;
1262
1263 p->zone = NF_CT_DEFAULT_ZONE_ID;
1264
1265 tcf_ct_set_key_val(tb,
1266 &p->ct_action, TCA_CT_ACTION,
1267 NULL, TCA_CT_UNSPEC,
1268 sizeof(p->ct_action));
1269
1270 if (p->ct_action & TCA_CT_ACT_CLEAR)
1271 return 0;
1272
1273 err = tcf_ct_fill_params_nat(p, parm, tb, extack);
1274 if (err)
1275 return err;
1276
1277 if (tb[TCA_CT_MARK]) {
1278 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1279 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled.");
1280 return -EOPNOTSUPP;
1281 }
1282 tcf_ct_set_key_val(tb,
1283 &p->mark, TCA_CT_MARK,
1284 &p->mark_mask, TCA_CT_MARK_MASK,
1285 sizeof(p->mark));
1286 }
1287
1288 if (tb[TCA_CT_LABELS]) {
1289 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1290 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled.");
1291 return -EOPNOTSUPP;
1292 }
1293
1294 if (!tn->labels) {
1295 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length");
1296 return -EOPNOTSUPP;
1297 }
1298 tcf_ct_set_key_val(tb,
1299 p->labels, TCA_CT_LABELS,
1300 p->labels_mask, TCA_CT_LABELS_MASK,
1301 sizeof(p->labels));
1302 }
1303
1304 if (tb[TCA_CT_ZONE]) {
1305 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1306 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled.");
1307 return -EOPNOTSUPP;
1308 }
1309
1310 tcf_ct_set_key_val(tb,
1311 &p->zone, TCA_CT_ZONE,
1312 NULL, TCA_CT_UNSPEC,
1313 sizeof(p->zone));
1314 }
1315
1316 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0);
1317 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL);
1318 if (!tmpl) {
1319 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template");
1320 return -ENOMEM;
1321 }
1322 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status);
1323 p->tmpl = tmpl;
1324
1325 return 0;
1326 }
1327
1328 static int tcf_ct_init(struct net *net, struct nlattr *nla,
1329 struct nlattr *est, struct tc_action **a,
1330 struct tcf_proto *tp, u32 flags,
1331 struct netlink_ext_ack *extack)
1332 {
1333 struct tc_action_net *tn = net_generic(net, ct_net_id);
1334 bool bind = flags & TCA_ACT_FLAGS_BIND;
1335 struct tcf_ct_params *params = NULL;
1336 struct nlattr *tb[TCA_CT_MAX + 1];
1337 struct tcf_chain *goto_ch = NULL;
1338 struct tc_ct *parm;
1339 struct tcf_ct *c;
1340 int err, res = 0;
1341 u32 index;
1342
1343 if (!nla) {
1344 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed");
1345 return -EINVAL;
1346 }
1347
1348 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack);
1349 if (err < 0)
1350 return err;
1351
1352 if (!tb[TCA_CT_PARMS]) {
1353 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters");
1354 return -EINVAL;
1355 }
1356 parm = nla_data(tb[TCA_CT_PARMS]);
1357 index = parm->index;
1358 err = tcf_idr_check_alloc(tn, &index, a, bind);
1359 if (err < 0)
1360 return err;
1361
1362 if (!err) {
1363 err = tcf_idr_create_from_flags(tn, index, est, a,
1364 &act_ct_ops, bind, flags);
1365 if (err) {
1366 tcf_idr_cleanup(tn, index);
1367 return err;
1368 }
1369 res = ACT_P_CREATED;
1370 } else {
1371 if (bind)
1372 return 0;
1373
1374 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
1375 tcf_idr_release(*a, bind);
1376 return -EEXIST;
1377 }
1378 }
1379 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
1380 if (err < 0)
1381 goto cleanup;
1382
1383 c = to_ct(*a);
1384
1385 params = kzalloc(sizeof(*params), GFP_KERNEL);
1386 if (unlikely(!params)) {
1387 err = -ENOMEM;
1388 goto cleanup;
1389 }
1390
1391 err = tcf_ct_fill_params(net, params, parm, tb, extack);
1392 if (err)
1393 goto cleanup;
1394
1395 err = tcf_ct_flow_table_get(net, params);
1396 if (err)
1397 goto cleanup_params;
1398
1399 spin_lock_bh(&c->tcf_lock);
1400 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1401 params = rcu_replace_pointer(c->params, params,
1402 lockdep_is_held(&c->tcf_lock));
1403 spin_unlock_bh(&c->tcf_lock);
1404
1405 if (goto_ch)
1406 tcf_chain_put_by_act(goto_ch);
1407 if (params)
1408 call_rcu(¶ms->rcu, tcf_ct_params_free);
1409
1410 return res;
1411
1412 cleanup_params:
1413 if (params->tmpl)
1414 nf_ct_put(params->tmpl);
1415 cleanup:
1416 if (goto_ch)
1417 tcf_chain_put_by_act(goto_ch);
1418 kfree(params);
1419 tcf_idr_release(*a, bind);
1420 return err;
1421 }
1422
1423 static void tcf_ct_cleanup(struct tc_action *a)
1424 {
1425 struct tcf_ct_params *params;
1426 struct tcf_ct *c = to_ct(a);
1427
1428 params = rcu_dereference_protected(c->params, 1);
1429 if (params)
1430 call_rcu(¶ms->rcu, tcf_ct_params_free);
1431 }
1432
1433 static int tcf_ct_dump_key_val(struct sk_buff *skb,
1434 void *val, int val_type,
1435 void *mask, int mask_type,
1436 int len)
1437 {
1438 int err;
1439
1440 if (mask && !memchr_inv(mask, 0, len))
1441 return 0;
1442
1443 err = nla_put(skb, val_type, len, val);
1444 if (err)
1445 return err;
1446
1447 if (mask_type != TCA_CT_UNSPEC) {
1448 err = nla_put(skb, mask_type, len, mask);
1449 if (err)
1450 return err;
1451 }
1452
1453 return 0;
1454 }
1455
1456 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p)
1457 {
1458 struct nf_nat_range2 *range = &p->range;
1459
1460 if (!(p->ct_action & TCA_CT_ACT_NAT))
1461 return 0;
1462
1463 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST)))
1464 return 0;
1465
1466 if (range->flags & NF_NAT_RANGE_MAP_IPS) {
1467 if (p->ipv4_range) {
1468 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN,
1469 range->min_addr.ip))
1470 return -1;
1471 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX,
1472 range->max_addr.ip))
1473 return -1;
1474 } else {
1475 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN,
1476 &range->min_addr.in6))
1477 return -1;
1478 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX,
1479 &range->max_addr.in6))
1480 return -1;
1481 }
1482 }
1483
1484 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
1485 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN,
1486 range->min_proto.all))
1487 return -1;
1488 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX,
1489 range->max_proto.all))
1490 return -1;
1491 }
1492
1493 return 0;
1494 }
1495
1496 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a,
1497 int bind, int ref)
1498 {
1499 unsigned char *b = skb_tail_pointer(skb);
1500 struct tcf_ct *c = to_ct(a);
1501 struct tcf_ct_params *p;
1502
1503 struct tc_ct opt = {
1504 .index = c->tcf_index,
1505 .refcnt = refcount_read(&c->tcf_refcnt) - ref,
1506 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind,
1507 };
1508 struct tcf_t t;
1509
1510 spin_lock_bh(&c->tcf_lock);
1511 p = rcu_dereference_protected(c->params,
1512 lockdep_is_held(&c->tcf_lock));
1513 opt.action = c->tcf_action;
1514
1515 if (tcf_ct_dump_key_val(skb,
1516 &p->ct_action, TCA_CT_ACTION,
1517 NULL, TCA_CT_UNSPEC,
1518 sizeof(p->ct_action)))
1519 goto nla_put_failure;
1520
1521 if (p->ct_action & TCA_CT_ACT_CLEAR)
1522 goto skip_dump;
1523
1524 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1525 tcf_ct_dump_key_val(skb,
1526 &p->mark, TCA_CT_MARK,
1527 &p->mark_mask, TCA_CT_MARK_MASK,
1528 sizeof(p->mark)))
1529 goto nla_put_failure;
1530
1531 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1532 tcf_ct_dump_key_val(skb,
1533 p->labels, TCA_CT_LABELS,
1534 p->labels_mask, TCA_CT_LABELS_MASK,
1535 sizeof(p->labels)))
1536 goto nla_put_failure;
1537
1538 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1539 tcf_ct_dump_key_val(skb,
1540 &p->zone, TCA_CT_ZONE,
1541 NULL, TCA_CT_UNSPEC,
1542 sizeof(p->zone)))
1543 goto nla_put_failure;
1544
1545 if (tcf_ct_dump_nat(skb, p))
1546 goto nla_put_failure;
1547
1548 skip_dump:
1549 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt))
1550 goto nla_put_failure;
1551
1552 tcf_tm_dump(&t, &c->tcf_tm);
1553 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD))
1554 goto nla_put_failure;
1555 spin_unlock_bh(&c->tcf_lock);
1556
1557 return skb->len;
1558 nla_put_failure:
1559 spin_unlock_bh(&c->tcf_lock);
1560 nlmsg_trim(skb, b);
1561 return -1;
1562 }
1563
1564 static int tcf_ct_walker(struct net *net, struct sk_buff *skb,
1565 struct netlink_callback *cb, int type,
1566 const struct tc_action_ops *ops,
1567 struct netlink_ext_ack *extack)
1568 {
1569 struct tc_action_net *tn = net_generic(net, ct_net_id);
1570
1571 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
1572 }
1573
1574 static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index)
1575 {
1576 struct tc_action_net *tn = net_generic(net, ct_net_id);
1577
1578 return tcf_idr_search(tn, a, index);
1579 }
1580
1581 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
1582 u64 drops, u64 lastuse, bool hw)
1583 {
1584 struct tcf_ct *c = to_ct(a);
1585
1586 tcf_action_update_stats(a, bytes, packets, drops, hw);
1587 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse);
1588 }
1589
1590 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data,
1591 u32 *index_inc, bool bind,
1592 struct netlink_ext_ack *extack)
1593 {
1594 if (bind) {
1595 struct flow_action_entry *entry = entry_data;
1596
1597 entry->id = FLOW_ACTION_CT;
1598 entry->ct.action = tcf_ct_action(act);
1599 entry->ct.zone = tcf_ct_zone(act);
1600 entry->ct.flow_table = tcf_ct_ft(act);
1601 *index_inc = 1;
1602 } else {
1603 struct flow_offload_action *fl_action = entry_data;
1604
1605 fl_action->id = FLOW_ACTION_CT;
1606 }
1607
1608 return 0;
1609 }
1610
1611 static struct tc_action_ops act_ct_ops = {
1612 .kind = "ct",
1613 .id = TCA_ID_CT,
1614 .owner = THIS_MODULE,
1615 .act = tcf_ct_act,
1616 .dump = tcf_ct_dump,
1617 .init = tcf_ct_init,
1618 .cleanup = tcf_ct_cleanup,
1619 .walk = tcf_ct_walker,
1620 .lookup = tcf_ct_search,
1621 .stats_update = tcf_stats_update,
1622 .offload_act_setup = tcf_ct_offload_act_setup,
1623 .size = sizeof(struct tcf_ct),
1624 };
1625
1626 static __net_init int ct_init_net(struct net *net)
1627 {
1628 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8;
1629 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1630
1631 if (nf_connlabels_get(net, n_bits - 1)) {
1632 tn->labels = false;
1633 pr_err("act_ct: Failed to set connlabels length");
1634 } else {
1635 tn->labels = true;
1636 }
1637
1638 return tc_action_net_init(net, &tn->tn, &act_ct_ops);
1639 }
1640
1641 static void __net_exit ct_exit_net(struct list_head *net_list)
1642 {
1643 struct net *net;
1644
1645 rtnl_lock();
1646 list_for_each_entry(net, net_list, exit_list) {
1647 struct tc_ct_action_net *tn = net_generic(net, ct_net_id);
1648
1649 if (tn->labels)
1650 nf_connlabels_put(net);
1651 }
1652 rtnl_unlock();
1653
1654 tc_action_net_exit(net_list, ct_net_id);
1655 }
1656
1657 static struct pernet_operations ct_net_ops = {
1658 .init = ct_init_net,
1659 .exit_batch = ct_exit_net,
1660 .id = &ct_net_id,
1661 .size = sizeof(struct tc_ct_action_net),
1662 };
1663
1664 static int __init ct_init_module(void)
1665 {
1666 int err;
1667
1668 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0);
1669 if (!act_ct_wq)
1670 return -ENOMEM;
1671
1672 err = tcf_ct_flow_tables_init();
1673 if (err)
1674 goto err_tbl_init;
1675
1676 err = tcf_register_action(&act_ct_ops, &ct_net_ops);
1677 if (err)
1678 goto err_register;
1679
1680 static_branch_inc(&tcf_frag_xmit_count);
1681
1682 return 0;
1683
1684 err_register:
1685 tcf_ct_flow_tables_uninit();
1686 err_tbl_init:
1687 destroy_workqueue(act_ct_wq);
1688 return err;
1689 }
1690
1691 static void __exit ct_cleanup_module(void)
1692 {
1693 static_branch_dec(&tcf_frag_xmit_count);
1694 tcf_unregister_action(&act_ct_ops, &ct_net_ops);
1695 tcf_ct_flow_tables_uninit();
1696 destroy_workqueue(act_ct_wq);
1697 }
1698
1699 module_init(ct_init_module);
1700 module_exit(ct_cleanup_module);
1701 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>");
1702 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>");
1703 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>");
1704 MODULE_DESCRIPTION("Connection tracking action");
1705 MODULE_LICENSE("GPL v2");