1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* - 3 * net/sched/act_ct.c Connection Tracking action 4 * 5 * Authors: Paul Blakey <paulb@mellanox.com> 6 * Yossi Kuperman <yossiku@mellanox.com> 7 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/skbuff.h> 14 #include <linux/rtnetlink.h> 15 #include <linux/pkt_cls.h> 16 #include <linux/ip.h> 17 #include <linux/ipv6.h> 18 #include <linux/rhashtable.h> 19 #include <net/netlink.h> 20 #include <net/pkt_sched.h> 21 #include <net/pkt_cls.h> 22 #include <net/act_api.h> 23 #include <net/ip.h> 24 #include <net/ipv6_frag.h> 25 #include <uapi/linux/tc_act/tc_ct.h> 26 #include <net/tc_act/tc_ct.h> 27 28 #include <net/netfilter/nf_flow_table.h> 29 #include <net/netfilter/nf_conntrack.h> 30 #include <net/netfilter/nf_conntrack_core.h> 31 #include <net/netfilter/nf_conntrack_zones.h> 32 #include <net/netfilter/nf_conntrack_helper.h> 33 #include <net/netfilter/nf_conntrack_acct.h> 34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 35 #include <net/netfilter/nf_conntrack_act_ct.h> 36 #include <uapi/linux/netfilter/nf_nat.h> 37 38 static struct workqueue_struct *act_ct_wq; 39 static struct rhashtable zones_ht; 40 static DEFINE_MUTEX(zones_mutex); 41 42 struct tcf_ct_flow_table { 43 struct rhash_head node; /* In zones tables */ 44 45 struct rcu_work rwork; 46 struct nf_flowtable nf_ft; 47 refcount_t ref; 48 u16 zone; 49 50 bool dying; 51 }; 52 53 static const struct rhashtable_params zones_params = { 54 .head_offset = offsetof(struct tcf_ct_flow_table, node), 55 .key_offset = offsetof(struct tcf_ct_flow_table, zone), 56 .key_len = sizeof_field(struct tcf_ct_flow_table, zone), 57 .automatic_shrinking = true, 58 }; 59 60 static struct flow_action_entry * 61 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 62 { 63 int i = flow_action->num_entries++; 64 65 return &flow_action->entries[i]; 66 } 67 68 static void tcf_ct_add_mangle_action(struct flow_action *action, 69 enum flow_action_mangle_base htype, 70 u32 offset, 71 u32 mask, 72 u32 val) 73 { 74 struct flow_action_entry *entry; 75 76 entry = tcf_ct_flow_table_flow_action_get_next(action); 77 entry->id = FLOW_ACTION_MANGLE; 78 entry->mangle.htype = htype; 79 entry->mangle.mask = ~mask; 80 entry->mangle.offset = offset; 81 entry->mangle.val = val; 82 } 83 84 /* The following nat helper functions check if the inverted reverse tuple 85 * (target) is different then the current dir tuple - meaning nat for ports 86 * and/or ip is needed, and add the relevant mangle actions. 87 */ 88 static void 89 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 90 struct nf_conntrack_tuple target, 91 struct flow_action *action) 92 { 93 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 94 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 95 offsetof(struct iphdr, saddr), 96 0xFFFFFFFF, 97 be32_to_cpu(target.src.u3.ip)); 98 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 99 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 100 offsetof(struct iphdr, daddr), 101 0xFFFFFFFF, 102 be32_to_cpu(target.dst.u3.ip)); 103 } 104 105 static void 106 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 107 union nf_inet_addr *addr, 108 u32 offset) 109 { 110 int i; 111 112 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 113 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 114 i * sizeof(u32) + offset, 115 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 116 } 117 118 static void 119 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 120 struct nf_conntrack_tuple target, 121 struct flow_action *action) 122 { 123 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 124 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 125 offsetof(struct ipv6hdr, 126 saddr)); 127 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 128 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 129 offsetof(struct ipv6hdr, 130 daddr)); 131 } 132 133 static void 134 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 135 struct nf_conntrack_tuple target, 136 struct flow_action *action) 137 { 138 __be16 target_src = target.src.u.tcp.port; 139 __be16 target_dst = target.dst.u.tcp.port; 140 141 if (target_src != tuple->src.u.tcp.port) 142 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 143 offsetof(struct tcphdr, source), 144 0xFFFF, be16_to_cpu(target_src)); 145 if (target_dst != tuple->dst.u.tcp.port) 146 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 147 offsetof(struct tcphdr, dest), 148 0xFFFF, be16_to_cpu(target_dst)); 149 } 150 151 static void 152 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 153 struct nf_conntrack_tuple target, 154 struct flow_action *action) 155 { 156 __be16 target_src = target.src.u.udp.port; 157 __be16 target_dst = target.dst.u.udp.port; 158 159 if (target_src != tuple->src.u.udp.port) 160 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 161 offsetof(struct udphdr, source), 162 0xFFFF, be16_to_cpu(target_src)); 163 if (target_dst != tuple->dst.u.udp.port) 164 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 165 offsetof(struct udphdr, dest), 166 0xFFFF, be16_to_cpu(target_dst)); 167 } 168 169 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 170 enum ip_conntrack_dir dir, 171 struct flow_action *action) 172 { 173 struct nf_conn_labels *ct_labels; 174 struct flow_action_entry *entry; 175 enum ip_conntrack_info ctinfo; 176 u32 *act_ct_labels; 177 178 entry = tcf_ct_flow_table_flow_action_get_next(action); 179 entry->id = FLOW_ACTION_CT_METADATA; 180 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 181 entry->ct_metadata.mark = ct->mark; 182 #endif 183 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 184 IP_CT_ESTABLISHED_REPLY; 185 /* aligns with the CT reference on the SKB nf_ct_set */ 186 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo; 187 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL; 188 189 act_ct_labels = entry->ct_metadata.labels; 190 ct_labels = nf_ct_labels_find(ct); 191 if (ct_labels) 192 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 193 else 194 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 195 } 196 197 static int tcf_ct_flow_table_add_action_nat(struct net *net, 198 struct nf_conn *ct, 199 enum ip_conntrack_dir dir, 200 struct flow_action *action) 201 { 202 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 203 struct nf_conntrack_tuple target; 204 205 if (!(ct->status & IPS_NAT_MASK)) 206 return 0; 207 208 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 209 210 switch (tuple->src.l3num) { 211 case NFPROTO_IPV4: 212 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 213 action); 214 break; 215 case NFPROTO_IPV6: 216 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 217 action); 218 break; 219 default: 220 return -EOPNOTSUPP; 221 } 222 223 switch (nf_ct_protonum(ct)) { 224 case IPPROTO_TCP: 225 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 226 break; 227 case IPPROTO_UDP: 228 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 229 break; 230 default: 231 return -EOPNOTSUPP; 232 } 233 234 return 0; 235 } 236 237 static int tcf_ct_flow_table_fill_actions(struct net *net, 238 const struct flow_offload *flow, 239 enum flow_offload_tuple_dir tdir, 240 struct nf_flow_rule *flow_rule) 241 { 242 struct flow_action *action = &flow_rule->rule->action; 243 int num_entries = action->num_entries; 244 struct nf_conn *ct = flow->ct; 245 enum ip_conntrack_dir dir; 246 int i, err; 247 248 switch (tdir) { 249 case FLOW_OFFLOAD_DIR_ORIGINAL: 250 dir = IP_CT_DIR_ORIGINAL; 251 break; 252 case FLOW_OFFLOAD_DIR_REPLY: 253 dir = IP_CT_DIR_REPLY; 254 break; 255 default: 256 return -EOPNOTSUPP; 257 } 258 259 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 260 if (err) 261 goto err_nat; 262 263 tcf_ct_flow_table_add_action_meta(ct, dir, action); 264 return 0; 265 266 err_nat: 267 /* Clear filled actions */ 268 for (i = num_entries; i < action->num_entries; i++) 269 memset(&action->entries[i], 0, sizeof(action->entries[i])); 270 action->num_entries = num_entries; 271 272 return err; 273 } 274 275 static struct nf_flowtable_type flowtable_ct = { 276 .action = tcf_ct_flow_table_fill_actions, 277 .owner = THIS_MODULE, 278 }; 279 280 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params) 281 { 282 struct tcf_ct_flow_table *ct_ft; 283 int err = -ENOMEM; 284 285 mutex_lock(&zones_mutex); 286 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 287 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 288 goto out_unlock; 289 290 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 291 if (!ct_ft) 292 goto err_alloc; 293 refcount_set(&ct_ft->ref, 1); 294 295 ct_ft->zone = params->zone; 296 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 297 if (err) 298 goto err_insert; 299 300 ct_ft->nf_ft.type = &flowtable_ct; 301 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 302 NF_FLOWTABLE_COUNTER; 303 err = nf_flow_table_init(&ct_ft->nf_ft); 304 if (err) 305 goto err_init; 306 write_pnet(&ct_ft->nf_ft.net, net); 307 308 __module_get(THIS_MODULE); 309 out_unlock: 310 params->ct_ft = ct_ft; 311 params->nf_ft = &ct_ft->nf_ft; 312 mutex_unlock(&zones_mutex); 313 314 return 0; 315 316 err_init: 317 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 318 err_insert: 319 kfree(ct_ft); 320 err_alloc: 321 mutex_unlock(&zones_mutex); 322 return err; 323 } 324 325 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 326 { 327 struct flow_block_cb *block_cb, *tmp_cb; 328 struct tcf_ct_flow_table *ct_ft; 329 struct flow_block *block; 330 331 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 332 rwork); 333 nf_flow_table_free(&ct_ft->nf_ft); 334 335 /* Remove any remaining callbacks before cleanup */ 336 block = &ct_ft->nf_ft.flow_block; 337 down_write(&ct_ft->nf_ft.flow_block_lock); 338 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) { 339 list_del(&block_cb->list); 340 flow_block_cb_free(block_cb); 341 } 342 up_write(&ct_ft->nf_ft.flow_block_lock); 343 kfree(ct_ft); 344 345 module_put(THIS_MODULE); 346 } 347 348 static void tcf_ct_flow_table_put(struct tcf_ct_params *params) 349 { 350 struct tcf_ct_flow_table *ct_ft = params->ct_ft; 351 352 if (refcount_dec_and_test(¶ms->ct_ft->ref)) { 353 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 354 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 355 queue_rcu_work(act_ct_wq, &ct_ft->rwork); 356 } 357 } 358 359 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry, 360 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir) 361 { 362 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC; 363 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir]; 364 } 365 366 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 367 struct nf_conn *ct, 368 bool tcp) 369 { 370 struct nf_conn_act_ct_ext *act_ct_ext; 371 struct flow_offload *entry; 372 int err; 373 374 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 375 return; 376 377 entry = flow_offload_alloc(ct); 378 if (!entry) { 379 WARN_ON_ONCE(1); 380 goto err_alloc; 381 } 382 383 if (tcp) { 384 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 385 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 386 } 387 388 act_ct_ext = nf_conn_act_ct_ext_find(ct); 389 if (act_ct_ext) { 390 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 391 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 392 } 393 394 err = flow_offload_add(&ct_ft->nf_ft, entry); 395 if (err) 396 goto err_add; 397 398 return; 399 400 err_add: 401 flow_offload_free(entry); 402 err_alloc: 403 clear_bit(IPS_OFFLOAD_BIT, &ct->status); 404 } 405 406 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 407 struct nf_conn *ct, 408 enum ip_conntrack_info ctinfo) 409 { 410 bool tcp = false; 411 412 if ((ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) || 413 !test_bit(IPS_ASSURED_BIT, &ct->status)) 414 return; 415 416 switch (nf_ct_protonum(ct)) { 417 case IPPROTO_TCP: 418 tcp = true; 419 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 420 return; 421 break; 422 case IPPROTO_UDP: 423 break; 424 #ifdef CONFIG_NF_CT_PROTO_GRE 425 case IPPROTO_GRE: { 426 struct nf_conntrack_tuple *tuple; 427 428 if (ct->status & IPS_NAT_MASK) 429 return; 430 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; 431 /* No support for GRE v1 */ 432 if (tuple->src.u.gre.key || tuple->dst.u.gre.key) 433 return; 434 break; 435 } 436 #endif 437 default: 438 return; 439 } 440 441 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 442 ct->status & IPS_SEQ_ADJUST) 443 return; 444 445 tcf_ct_flow_table_add(ct_ft, ct, tcp); 446 } 447 448 static bool 449 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 450 struct flow_offload_tuple *tuple, 451 struct tcphdr **tcph) 452 { 453 struct flow_ports *ports; 454 unsigned int thoff; 455 struct iphdr *iph; 456 size_t hdrsize; 457 u8 ipproto; 458 459 if (!pskb_network_may_pull(skb, sizeof(*iph))) 460 return false; 461 462 iph = ip_hdr(skb); 463 thoff = iph->ihl * 4; 464 465 if (ip_is_fragment(iph) || 466 unlikely(thoff != sizeof(struct iphdr))) 467 return false; 468 469 ipproto = iph->protocol; 470 switch (ipproto) { 471 case IPPROTO_TCP: 472 hdrsize = sizeof(struct tcphdr); 473 break; 474 case IPPROTO_UDP: 475 hdrsize = sizeof(*ports); 476 break; 477 #ifdef CONFIG_NF_CT_PROTO_GRE 478 case IPPROTO_GRE: 479 hdrsize = sizeof(struct gre_base_hdr); 480 break; 481 #endif 482 default: 483 return false; 484 } 485 486 if (iph->ttl <= 1) 487 return false; 488 489 if (!pskb_network_may_pull(skb, thoff + hdrsize)) 490 return false; 491 492 switch (ipproto) { 493 case IPPROTO_TCP: 494 *tcph = (void *)(skb_network_header(skb) + thoff); 495 fallthrough; 496 case IPPROTO_UDP: 497 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 498 tuple->src_port = ports->source; 499 tuple->dst_port = ports->dest; 500 break; 501 case IPPROTO_GRE: { 502 struct gre_base_hdr *greh; 503 504 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 505 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 506 return false; 507 break; 508 } 509 } 510 511 iph = ip_hdr(skb); 512 513 tuple->src_v4.s_addr = iph->saddr; 514 tuple->dst_v4.s_addr = iph->daddr; 515 tuple->l3proto = AF_INET; 516 tuple->l4proto = ipproto; 517 518 return true; 519 } 520 521 static bool 522 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 523 struct flow_offload_tuple *tuple, 524 struct tcphdr **tcph) 525 { 526 struct flow_ports *ports; 527 struct ipv6hdr *ip6h; 528 unsigned int thoff; 529 size_t hdrsize; 530 u8 nexthdr; 531 532 if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 533 return false; 534 535 ip6h = ipv6_hdr(skb); 536 thoff = sizeof(*ip6h); 537 538 nexthdr = ip6h->nexthdr; 539 switch (nexthdr) { 540 case IPPROTO_TCP: 541 hdrsize = sizeof(struct tcphdr); 542 break; 543 case IPPROTO_UDP: 544 hdrsize = sizeof(*ports); 545 break; 546 #ifdef CONFIG_NF_CT_PROTO_GRE 547 case IPPROTO_GRE: 548 hdrsize = sizeof(struct gre_base_hdr); 549 break; 550 #endif 551 default: 552 return false; 553 } 554 555 if (ip6h->hop_limit <= 1) 556 return false; 557 558 if (!pskb_network_may_pull(skb, thoff + hdrsize)) 559 return false; 560 561 switch (nexthdr) { 562 case IPPROTO_TCP: 563 *tcph = (void *)(skb_network_header(skb) + thoff); 564 fallthrough; 565 case IPPROTO_UDP: 566 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 567 tuple->src_port = ports->source; 568 tuple->dst_port = ports->dest; 569 break; 570 case IPPROTO_GRE: { 571 struct gre_base_hdr *greh; 572 573 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 574 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 575 return false; 576 break; 577 } 578 } 579 580 ip6h = ipv6_hdr(skb); 581 582 tuple->src_v6 = ip6h->saddr; 583 tuple->dst_v6 = ip6h->daddr; 584 tuple->l3proto = AF_INET6; 585 tuple->l4proto = nexthdr; 586 587 return true; 588 } 589 590 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 591 struct sk_buff *skb, 592 u8 family) 593 { 594 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 595 struct flow_offload_tuple_rhash *tuplehash; 596 struct flow_offload_tuple tuple = {}; 597 enum ip_conntrack_info ctinfo; 598 struct tcphdr *tcph = NULL; 599 struct flow_offload *flow; 600 struct nf_conn *ct; 601 u8 dir; 602 603 switch (family) { 604 case NFPROTO_IPV4: 605 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 606 return false; 607 break; 608 case NFPROTO_IPV6: 609 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 610 return false; 611 break; 612 default: 613 return false; 614 } 615 616 tuplehash = flow_offload_lookup(nf_ft, &tuple); 617 if (!tuplehash) 618 return false; 619 620 dir = tuplehash->tuple.dir; 621 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 622 ct = flow->ct; 623 624 if (tcph && (unlikely(tcph->fin || tcph->rst))) { 625 flow_offload_teardown(flow); 626 return false; 627 } 628 629 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 630 IP_CT_ESTABLISHED_REPLY; 631 632 flow_offload_refresh(nf_ft, flow); 633 nf_conntrack_get(&ct->ct_general); 634 nf_ct_set(skb, ct, ctinfo); 635 if (nf_ft->flags & NF_FLOWTABLE_COUNTER) 636 nf_ct_acct_update(ct, dir, skb->len); 637 638 return true; 639 } 640 641 static int tcf_ct_flow_tables_init(void) 642 { 643 return rhashtable_init(&zones_ht, &zones_params); 644 } 645 646 static void tcf_ct_flow_tables_uninit(void) 647 { 648 rhashtable_destroy(&zones_ht); 649 } 650 651 static struct tc_action_ops act_ct_ops; 652 static unsigned int ct_net_id; 653 654 struct tc_ct_action_net { 655 struct tc_action_net tn; /* Must be first */ 656 bool labels; 657 }; 658 659 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 660 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 661 u16 zone_id, bool force) 662 { 663 enum ip_conntrack_info ctinfo; 664 struct nf_conn *ct; 665 666 ct = nf_ct_get(skb, &ctinfo); 667 if (!ct) 668 return false; 669 if (!net_eq(net, read_pnet(&ct->ct_net))) 670 goto drop_ct; 671 if (nf_ct_zone(ct)->id != zone_id) 672 goto drop_ct; 673 674 /* Force conntrack entry direction. */ 675 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 676 if (nf_ct_is_confirmed(ct)) 677 nf_ct_kill(ct); 678 679 goto drop_ct; 680 } 681 682 return true; 683 684 drop_ct: 685 nf_ct_put(ct); 686 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 687 688 return false; 689 } 690 691 /* Trim the skb to the length specified by the IP/IPv6 header, 692 * removing any trailing lower-layer padding. This prepares the skb 693 * for higher-layer processing that assumes skb->len excludes padding 694 * (such as nf_ip_checksum). The caller needs to pull the skb to the 695 * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 696 */ 697 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family) 698 { 699 unsigned int len; 700 int err; 701 702 switch (family) { 703 case NFPROTO_IPV4: 704 len = ntohs(ip_hdr(skb)->tot_len); 705 break; 706 case NFPROTO_IPV6: 707 len = sizeof(struct ipv6hdr) 708 + ntohs(ipv6_hdr(skb)->payload_len); 709 break; 710 default: 711 len = skb->len; 712 } 713 714 err = pskb_trim_rcsum(skb, len); 715 716 return err; 717 } 718 719 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 720 { 721 u8 family = NFPROTO_UNSPEC; 722 723 switch (skb_protocol(skb, true)) { 724 case htons(ETH_P_IP): 725 family = NFPROTO_IPV4; 726 break; 727 case htons(ETH_P_IPV6): 728 family = NFPROTO_IPV6; 729 break; 730 default: 731 break; 732 } 733 734 return family; 735 } 736 737 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 738 { 739 unsigned int len; 740 741 len = skb_network_offset(skb) + sizeof(struct iphdr); 742 if (unlikely(skb->len < len)) 743 return -EINVAL; 744 if (unlikely(!pskb_may_pull(skb, len))) 745 return -ENOMEM; 746 747 *frag = ip_is_fragment(ip_hdr(skb)); 748 return 0; 749 } 750 751 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 752 { 753 unsigned int flags = 0, len, payload_ofs = 0; 754 unsigned short frag_off; 755 int nexthdr; 756 757 len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 758 if (unlikely(skb->len < len)) 759 return -EINVAL; 760 if (unlikely(!pskb_may_pull(skb, len))) 761 return -ENOMEM; 762 763 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 764 if (unlikely(nexthdr < 0)) 765 return -EPROTO; 766 767 *frag = flags & IP6_FH_F_FRAG; 768 return 0; 769 } 770 771 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 772 u8 family, u16 zone, bool *defrag) 773 { 774 enum ip_conntrack_info ctinfo; 775 struct nf_conn *ct; 776 int err = 0; 777 bool frag; 778 u16 mru; 779 780 /* Previously seen (loopback)? Ignore. */ 781 ct = nf_ct_get(skb, &ctinfo); 782 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 783 return 0; 784 785 if (family == NFPROTO_IPV4) 786 err = tcf_ct_ipv4_is_fragment(skb, &frag); 787 else 788 err = tcf_ct_ipv6_is_fragment(skb, &frag); 789 if (err || !frag) 790 return err; 791 792 skb_get(skb); 793 mru = tc_skb_cb(skb)->mru; 794 795 if (family == NFPROTO_IPV4) { 796 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 797 798 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 799 local_bh_disable(); 800 err = ip_defrag(net, skb, user); 801 local_bh_enable(); 802 if (err && err != -EINPROGRESS) 803 return err; 804 805 if (!err) { 806 *defrag = true; 807 mru = IPCB(skb)->frag_max_size; 808 } 809 } else { /* NFPROTO_IPV6 */ 810 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 811 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 812 813 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 814 err = nf_ct_frag6_gather(net, skb, user); 815 if (err && err != -EINPROGRESS) 816 goto out_free; 817 818 if (!err) { 819 *defrag = true; 820 mru = IP6CB(skb)->frag_max_size; 821 } 822 #else 823 err = -EOPNOTSUPP; 824 goto out_free; 825 #endif 826 } 827 828 if (err != -EINPROGRESS) 829 tc_skb_cb(skb)->mru = mru; 830 skb_clear_hash(skb); 831 skb->ignore_df = 1; 832 return err; 833 834 out_free: 835 kfree_skb(skb); 836 return err; 837 } 838 839 static void tcf_ct_params_free(struct rcu_head *head) 840 { 841 struct tcf_ct_params *params = container_of(head, 842 struct tcf_ct_params, rcu); 843 844 tcf_ct_flow_table_put(params); 845 846 if (params->tmpl) 847 nf_ct_put(params->tmpl); 848 kfree(params); 849 } 850 851 #if IS_ENABLED(CONFIG_NF_NAT) 852 /* Modelled after nf_nat_ipv[46]_fn(). 853 * range is only used for new, uninitialized NAT state. 854 * Returns either NF_ACCEPT or NF_DROP. 855 */ 856 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 857 enum ip_conntrack_info ctinfo, 858 const struct nf_nat_range2 *range, 859 enum nf_nat_manip_type maniptype) 860 { 861 __be16 proto = skb_protocol(skb, true); 862 int hooknum, err = NF_ACCEPT; 863 864 /* See HOOK2MANIP(). */ 865 if (maniptype == NF_NAT_MANIP_SRC) 866 hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 867 else 868 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 869 870 switch (ctinfo) { 871 case IP_CT_RELATED: 872 case IP_CT_RELATED_REPLY: 873 if (proto == htons(ETH_P_IP) && 874 ip_hdr(skb)->protocol == IPPROTO_ICMP) { 875 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 876 hooknum)) 877 err = NF_DROP; 878 goto out; 879 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) { 880 __be16 frag_off; 881 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 882 int hdrlen = ipv6_skip_exthdr(skb, 883 sizeof(struct ipv6hdr), 884 &nexthdr, &frag_off); 885 886 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 887 if (!nf_nat_icmpv6_reply_translation(skb, ct, 888 ctinfo, 889 hooknum, 890 hdrlen)) 891 err = NF_DROP; 892 goto out; 893 } 894 } 895 /* Non-ICMP, fall thru to initialize if needed. */ 896 fallthrough; 897 case IP_CT_NEW: 898 /* Seen it before? This can happen for loopback, retrans, 899 * or local packets. 900 */ 901 if (!nf_nat_initialized(ct, maniptype)) { 902 /* Initialize according to the NAT action. */ 903 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 904 /* Action is set up to establish a new 905 * mapping. 906 */ 907 ? nf_nat_setup_info(ct, range, maniptype) 908 : nf_nat_alloc_null_binding(ct, hooknum); 909 if (err != NF_ACCEPT) 910 goto out; 911 } 912 break; 913 914 case IP_CT_ESTABLISHED: 915 case IP_CT_ESTABLISHED_REPLY: 916 break; 917 918 default: 919 err = NF_DROP; 920 goto out; 921 } 922 923 err = nf_nat_packet(ct, ctinfo, hooknum, skb); 924 if (err == NF_ACCEPT) { 925 if (maniptype == NF_NAT_MANIP_SRC) 926 tc_skb_cb(skb)->post_ct_snat = 1; 927 if (maniptype == NF_NAT_MANIP_DST) 928 tc_skb_cb(skb)->post_ct_dnat = 1; 929 } 930 out: 931 return err; 932 } 933 #endif /* CONFIG_NF_NAT */ 934 935 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 936 { 937 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 938 u32 new_mark; 939 940 if (!mask) 941 return; 942 943 new_mark = mark | (ct->mark & ~(mask)); 944 if (ct->mark != new_mark) { 945 ct->mark = new_mark; 946 if (nf_ct_is_confirmed(ct)) 947 nf_conntrack_event_cache(IPCT_MARK, ct); 948 } 949 #endif 950 } 951 952 static void tcf_ct_act_set_labels(struct nf_conn *ct, 953 u32 *labels, 954 u32 *labels_m) 955 { 956 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 957 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 958 959 if (!memchr_inv(labels_m, 0, labels_sz)) 960 return; 961 962 nf_connlabels_replace(ct, labels, labels_m, 4); 963 #endif 964 } 965 966 static int tcf_ct_act_nat(struct sk_buff *skb, 967 struct nf_conn *ct, 968 enum ip_conntrack_info ctinfo, 969 int ct_action, 970 struct nf_nat_range2 *range, 971 bool commit) 972 { 973 #if IS_ENABLED(CONFIG_NF_NAT) 974 int err; 975 enum nf_nat_manip_type maniptype; 976 977 if (!(ct_action & TCA_CT_ACT_NAT)) 978 return NF_ACCEPT; 979 980 /* Add NAT extension if not confirmed yet. */ 981 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 982 return NF_DROP; /* Can't NAT. */ 983 984 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) && 985 (ctinfo != IP_CT_RELATED || commit)) { 986 /* NAT an established or related connection like before. */ 987 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 988 /* This is the REPLY direction for a connection 989 * for which NAT was applied in the forward 990 * direction. Do the reverse NAT. 991 */ 992 maniptype = ct->status & IPS_SRC_NAT 993 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 994 else 995 maniptype = ct->status & IPS_SRC_NAT 996 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 997 } else if (ct_action & TCA_CT_ACT_NAT_SRC) { 998 maniptype = NF_NAT_MANIP_SRC; 999 } 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 /* The conntrack module expects to be working at L3. 1069 * We also try to pull the IPv4/6 header to linear area 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 /* If we are recirculating packets to match on ct fields and 1086 * committing with a separate ct action, then we don't need to 1087 * actually run the packet through conntrack twice unless it's for a 1088 * different zone. 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 /* Associate skb with specified zone. */ 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 /* This will take care of sending queued events 1131 * even if the connection is already confirmed. 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"); 1706