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 #include <net/tc_wrapper.h> 28 29 #include <net/netfilter/nf_flow_table.h> 30 #include <net/netfilter/nf_conntrack.h> 31 #include <net/netfilter/nf_conntrack_core.h> 32 #include <net/netfilter/nf_conntrack_zones.h> 33 #include <net/netfilter/nf_conntrack_helper.h> 34 #include <net/netfilter/nf_conntrack_acct.h> 35 #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 36 #include <net/netfilter/nf_conntrack_act_ct.h> 37 #include <net/netfilter/nf_conntrack_seqadj.h> 38 #include <uapi/linux/netfilter/nf_nat.h> 39 40 static struct workqueue_struct *act_ct_wq; 41 static struct rhashtable zones_ht; 42 static DEFINE_MUTEX(zones_mutex); 43 44 struct tcf_ct_flow_table { 45 struct rhash_head node; /* In zones tables */ 46 47 struct rcu_work rwork; 48 struct nf_flowtable nf_ft; 49 refcount_t ref; 50 u16 zone; 51 52 bool dying; 53 }; 54 55 static const struct rhashtable_params zones_params = { 56 .head_offset = offsetof(struct tcf_ct_flow_table, node), 57 .key_offset = offsetof(struct tcf_ct_flow_table, zone), 58 .key_len = sizeof_field(struct tcf_ct_flow_table, zone), 59 .automatic_shrinking = true, 60 }; 61 62 static struct flow_action_entry * 63 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 64 { 65 int i = flow_action->num_entries++; 66 67 return &flow_action->entries[i]; 68 } 69 70 static void tcf_ct_add_mangle_action(struct flow_action *action, 71 enum flow_action_mangle_base htype, 72 u32 offset, 73 u32 mask, 74 u32 val) 75 { 76 struct flow_action_entry *entry; 77 78 entry = tcf_ct_flow_table_flow_action_get_next(action); 79 entry->id = FLOW_ACTION_MANGLE; 80 entry->mangle.htype = htype; 81 entry->mangle.mask = ~mask; 82 entry->mangle.offset = offset; 83 entry->mangle.val = val; 84 } 85 86 /* The following nat helper functions check if the inverted reverse tuple 87 * (target) is different then the current dir tuple - meaning nat for ports 88 * and/or ip is needed, and add the relevant mangle actions. 89 */ 90 static void 91 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 92 struct nf_conntrack_tuple target, 93 struct flow_action *action) 94 { 95 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 96 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 97 offsetof(struct iphdr, saddr), 98 0xFFFFFFFF, 99 be32_to_cpu(target.src.u3.ip)); 100 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 101 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 102 offsetof(struct iphdr, daddr), 103 0xFFFFFFFF, 104 be32_to_cpu(target.dst.u3.ip)); 105 } 106 107 static void 108 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 109 union nf_inet_addr *addr, 110 u32 offset) 111 { 112 int i; 113 114 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 115 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 116 i * sizeof(u32) + offset, 117 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 118 } 119 120 static void 121 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 122 struct nf_conntrack_tuple target, 123 struct flow_action *action) 124 { 125 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 126 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 127 offsetof(struct ipv6hdr, 128 saddr)); 129 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 130 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 131 offsetof(struct ipv6hdr, 132 daddr)); 133 } 134 135 static void 136 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 137 struct nf_conntrack_tuple target, 138 struct flow_action *action) 139 { 140 __be16 target_src = target.src.u.tcp.port; 141 __be16 target_dst = target.dst.u.tcp.port; 142 143 if (target_src != tuple->src.u.tcp.port) 144 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 145 offsetof(struct tcphdr, source), 146 0xFFFF, be16_to_cpu(target_src)); 147 if (target_dst != tuple->dst.u.tcp.port) 148 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 149 offsetof(struct tcphdr, dest), 150 0xFFFF, be16_to_cpu(target_dst)); 151 } 152 153 static void 154 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 155 struct nf_conntrack_tuple target, 156 struct flow_action *action) 157 { 158 __be16 target_src = target.src.u.udp.port; 159 __be16 target_dst = target.dst.u.udp.port; 160 161 if (target_src != tuple->src.u.udp.port) 162 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 163 offsetof(struct udphdr, source), 164 0xFFFF, be16_to_cpu(target_src)); 165 if (target_dst != tuple->dst.u.udp.port) 166 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 167 offsetof(struct udphdr, dest), 168 0xFFFF, be16_to_cpu(target_dst)); 169 } 170 171 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 172 enum ip_conntrack_dir dir, 173 struct flow_action *action) 174 { 175 struct nf_conn_labels *ct_labels; 176 struct flow_action_entry *entry; 177 enum ip_conntrack_info ctinfo; 178 u32 *act_ct_labels; 179 180 entry = tcf_ct_flow_table_flow_action_get_next(action); 181 entry->id = FLOW_ACTION_CT_METADATA; 182 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 183 entry->ct_metadata.mark = READ_ONCE(ct->mark); 184 #endif 185 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 186 IP_CT_ESTABLISHED_REPLY; 187 /* aligns with the CT reference on the SKB nf_ct_set */ 188 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo; 189 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL; 190 191 act_ct_labels = entry->ct_metadata.labels; 192 ct_labels = nf_ct_labels_find(ct); 193 if (ct_labels) 194 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 195 else 196 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 197 } 198 199 static int tcf_ct_flow_table_add_action_nat(struct net *net, 200 struct nf_conn *ct, 201 enum ip_conntrack_dir dir, 202 struct flow_action *action) 203 { 204 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 205 struct nf_conntrack_tuple target; 206 207 if (!(ct->status & IPS_NAT_MASK)) 208 return 0; 209 210 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 211 212 switch (tuple->src.l3num) { 213 case NFPROTO_IPV4: 214 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 215 action); 216 break; 217 case NFPROTO_IPV6: 218 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 219 action); 220 break; 221 default: 222 return -EOPNOTSUPP; 223 } 224 225 switch (nf_ct_protonum(ct)) { 226 case IPPROTO_TCP: 227 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 228 break; 229 case IPPROTO_UDP: 230 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 231 break; 232 default: 233 return -EOPNOTSUPP; 234 } 235 236 return 0; 237 } 238 239 static int tcf_ct_flow_table_fill_actions(struct net *net, 240 const struct flow_offload *flow, 241 enum flow_offload_tuple_dir tdir, 242 struct nf_flow_rule *flow_rule) 243 { 244 struct flow_action *action = &flow_rule->rule->action; 245 int num_entries = action->num_entries; 246 struct nf_conn *ct = flow->ct; 247 enum ip_conntrack_dir dir; 248 int i, err; 249 250 switch (tdir) { 251 case FLOW_OFFLOAD_DIR_ORIGINAL: 252 dir = IP_CT_DIR_ORIGINAL; 253 break; 254 case FLOW_OFFLOAD_DIR_REPLY: 255 dir = IP_CT_DIR_REPLY; 256 break; 257 default: 258 return -EOPNOTSUPP; 259 } 260 261 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 262 if (err) 263 goto err_nat; 264 265 tcf_ct_flow_table_add_action_meta(ct, dir, action); 266 return 0; 267 268 err_nat: 269 /* Clear filled actions */ 270 for (i = num_entries; i < action->num_entries; i++) 271 memset(&action->entries[i], 0, sizeof(action->entries[i])); 272 action->num_entries = num_entries; 273 274 return err; 275 } 276 277 static struct nf_flowtable_type flowtable_ct = { 278 .action = tcf_ct_flow_table_fill_actions, 279 .owner = THIS_MODULE, 280 }; 281 282 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params) 283 { 284 struct tcf_ct_flow_table *ct_ft; 285 int err = -ENOMEM; 286 287 mutex_lock(&zones_mutex); 288 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 289 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 290 goto out_unlock; 291 292 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 293 if (!ct_ft) 294 goto err_alloc; 295 refcount_set(&ct_ft->ref, 1); 296 297 ct_ft->zone = params->zone; 298 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 299 if (err) 300 goto err_insert; 301 302 ct_ft->nf_ft.type = &flowtable_ct; 303 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 304 NF_FLOWTABLE_COUNTER; 305 err = nf_flow_table_init(&ct_ft->nf_ft); 306 if (err) 307 goto err_init; 308 write_pnet(&ct_ft->nf_ft.net, net); 309 310 __module_get(THIS_MODULE); 311 out_unlock: 312 params->ct_ft = ct_ft; 313 params->nf_ft = &ct_ft->nf_ft; 314 mutex_unlock(&zones_mutex); 315 316 return 0; 317 318 err_init: 319 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 320 err_insert: 321 kfree(ct_ft); 322 err_alloc: 323 mutex_unlock(&zones_mutex); 324 return err; 325 } 326 327 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 328 { 329 struct flow_block_cb *block_cb, *tmp_cb; 330 struct tcf_ct_flow_table *ct_ft; 331 struct flow_block *block; 332 333 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 334 rwork); 335 nf_flow_table_free(&ct_ft->nf_ft); 336 337 /* Remove any remaining callbacks before cleanup */ 338 block = &ct_ft->nf_ft.flow_block; 339 down_write(&ct_ft->nf_ft.flow_block_lock); 340 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) { 341 list_del(&block_cb->list); 342 flow_block_cb_free(block_cb); 343 } 344 up_write(&ct_ft->nf_ft.flow_block_lock); 345 kfree(ct_ft); 346 347 module_put(THIS_MODULE); 348 } 349 350 static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft) 351 { 352 if (refcount_dec_and_test(&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 653 struct tc_ct_action_net { 654 struct tc_action_net tn; /* Must be first */ 655 bool labels; 656 }; 657 658 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 659 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 660 struct tcf_ct_params *p) 661 { 662 enum ip_conntrack_info ctinfo; 663 struct nf_conn *ct; 664 665 ct = nf_ct_get(skb, &ctinfo); 666 if (!ct) 667 return false; 668 if (!net_eq(net, read_pnet(&ct->ct_net))) 669 goto drop_ct; 670 if (nf_ct_zone(ct)->id != p->zone) 671 goto drop_ct; 672 if (p->helper) { 673 struct nf_conn_help *help; 674 675 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); 676 if (help && rcu_access_pointer(help->helper) != p->helper) 677 goto drop_ct; 678 } 679 680 /* Force conntrack entry direction. */ 681 if ((p->ct_action & TCA_CT_ACT_FORCE) && 682 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 683 if (nf_ct_is_confirmed(ct)) 684 nf_ct_kill(ct); 685 686 goto drop_ct; 687 } 688 689 return true; 690 691 drop_ct: 692 nf_ct_put(ct); 693 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 694 695 return false; 696 } 697 698 /* Trim the skb to the length specified by the IP/IPv6 header, 699 * removing any trailing lower-layer padding. This prepares the skb 700 * for higher-layer processing that assumes skb->len excludes padding 701 * (such as nf_ip_checksum). The caller needs to pull the skb to the 702 * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 703 */ 704 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family) 705 { 706 unsigned int len; 707 708 switch (family) { 709 case NFPROTO_IPV4: 710 len = ntohs(ip_hdr(skb)->tot_len); 711 break; 712 case NFPROTO_IPV6: 713 len = sizeof(struct ipv6hdr) 714 + ntohs(ipv6_hdr(skb)->payload_len); 715 break; 716 default: 717 len = skb->len; 718 } 719 720 return pskb_trim_rcsum(skb, len); 721 } 722 723 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 724 { 725 u8 family = NFPROTO_UNSPEC; 726 727 switch (skb_protocol(skb, true)) { 728 case htons(ETH_P_IP): 729 family = NFPROTO_IPV4; 730 break; 731 case htons(ETH_P_IPV6): 732 family = NFPROTO_IPV6; 733 break; 734 default: 735 break; 736 } 737 738 return family; 739 } 740 741 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 742 { 743 unsigned int len; 744 745 len = skb_network_offset(skb) + sizeof(struct iphdr); 746 if (unlikely(skb->len < len)) 747 return -EINVAL; 748 if (unlikely(!pskb_may_pull(skb, len))) 749 return -ENOMEM; 750 751 *frag = ip_is_fragment(ip_hdr(skb)); 752 return 0; 753 } 754 755 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 756 { 757 unsigned int flags = 0, len, payload_ofs = 0; 758 unsigned short frag_off; 759 int nexthdr; 760 761 len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 762 if (unlikely(skb->len < len)) 763 return -EINVAL; 764 if (unlikely(!pskb_may_pull(skb, len))) 765 return -ENOMEM; 766 767 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 768 if (unlikely(nexthdr < 0)) 769 return -EPROTO; 770 771 *frag = flags & IP6_FH_F_FRAG; 772 return 0; 773 } 774 775 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 776 u8 family, u16 zone, bool *defrag) 777 { 778 enum ip_conntrack_info ctinfo; 779 struct nf_conn *ct; 780 int err = 0; 781 bool frag; 782 u16 mru; 783 784 /* Previously seen (loopback)? Ignore. */ 785 ct = nf_ct_get(skb, &ctinfo); 786 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 787 return 0; 788 789 if (family == NFPROTO_IPV4) 790 err = tcf_ct_ipv4_is_fragment(skb, &frag); 791 else 792 err = tcf_ct_ipv6_is_fragment(skb, &frag); 793 if (err || !frag) 794 return err; 795 796 skb_get(skb); 797 mru = tc_skb_cb(skb)->mru; 798 799 if (family == NFPROTO_IPV4) { 800 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 801 802 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 803 local_bh_disable(); 804 err = ip_defrag(net, skb, user); 805 local_bh_enable(); 806 if (err && err != -EINPROGRESS) 807 return err; 808 809 if (!err) { 810 *defrag = true; 811 mru = IPCB(skb)->frag_max_size; 812 } 813 } else { /* NFPROTO_IPV6 */ 814 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 815 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 816 817 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 818 err = nf_ct_frag6_gather(net, skb, user); 819 if (err && err != -EINPROGRESS) 820 goto out_free; 821 822 if (!err) { 823 *defrag = true; 824 mru = IP6CB(skb)->frag_max_size; 825 } 826 #else 827 err = -EOPNOTSUPP; 828 goto out_free; 829 #endif 830 } 831 832 if (err != -EINPROGRESS) 833 tc_skb_cb(skb)->mru = mru; 834 skb_clear_hash(skb); 835 skb->ignore_df = 1; 836 return err; 837 838 out_free: 839 kfree_skb(skb); 840 return err; 841 } 842 843 static void tcf_ct_params_free(struct tcf_ct_params *params) 844 { 845 if (params->helper) { 846 #if IS_ENABLED(CONFIG_NF_NAT) 847 if (params->ct_action & TCA_CT_ACT_NAT) 848 nf_nat_helper_put(params->helper); 849 #endif 850 nf_conntrack_helper_put(params->helper); 851 } 852 if (params->ct_ft) 853 tcf_ct_flow_table_put(params->ct_ft); 854 if (params->tmpl) 855 nf_ct_put(params->tmpl); 856 kfree(params); 857 } 858 859 static void tcf_ct_params_free_rcu(struct rcu_head *head) 860 { 861 struct tcf_ct_params *params; 862 863 params = container_of(head, struct tcf_ct_params, rcu); 864 tcf_ct_params_free(params); 865 } 866 867 #if IS_ENABLED(CONFIG_NF_NAT) 868 /* Modelled after nf_nat_ipv[46]_fn(). 869 * range is only used for new, uninitialized NAT state. 870 * Returns either NF_ACCEPT or NF_DROP. 871 */ 872 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 873 enum ip_conntrack_info ctinfo, 874 const struct nf_nat_range2 *range, 875 enum nf_nat_manip_type maniptype) 876 { 877 __be16 proto = skb_protocol(skb, true); 878 int hooknum, err = NF_ACCEPT; 879 880 /* See HOOK2MANIP(). */ 881 if (maniptype == NF_NAT_MANIP_SRC) 882 hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 883 else 884 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 885 886 switch (ctinfo) { 887 case IP_CT_RELATED: 888 case IP_CT_RELATED_REPLY: 889 if (proto == htons(ETH_P_IP) && 890 ip_hdr(skb)->protocol == IPPROTO_ICMP) { 891 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 892 hooknum)) 893 err = NF_DROP; 894 goto out; 895 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) { 896 __be16 frag_off; 897 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 898 int hdrlen = ipv6_skip_exthdr(skb, 899 sizeof(struct ipv6hdr), 900 &nexthdr, &frag_off); 901 902 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 903 if (!nf_nat_icmpv6_reply_translation(skb, ct, 904 ctinfo, 905 hooknum, 906 hdrlen)) 907 err = NF_DROP; 908 goto out; 909 } 910 } 911 /* Non-ICMP, fall thru to initialize if needed. */ 912 fallthrough; 913 case IP_CT_NEW: 914 /* Seen it before? This can happen for loopback, retrans, 915 * or local packets. 916 */ 917 if (!nf_nat_initialized(ct, maniptype)) { 918 /* Initialize according to the NAT action. */ 919 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 920 /* Action is set up to establish a new 921 * mapping. 922 */ 923 ? nf_nat_setup_info(ct, range, maniptype) 924 : nf_nat_alloc_null_binding(ct, hooknum); 925 if (err != NF_ACCEPT) 926 goto out; 927 } 928 break; 929 930 case IP_CT_ESTABLISHED: 931 case IP_CT_ESTABLISHED_REPLY: 932 break; 933 934 default: 935 err = NF_DROP; 936 goto out; 937 } 938 939 err = nf_nat_packet(ct, ctinfo, hooknum, skb); 940 if (err == NF_ACCEPT) { 941 if (maniptype == NF_NAT_MANIP_SRC) 942 tc_skb_cb(skb)->post_ct_snat = 1; 943 if (maniptype == NF_NAT_MANIP_DST) 944 tc_skb_cb(skb)->post_ct_dnat = 1; 945 } 946 out: 947 return err; 948 } 949 #endif /* CONFIG_NF_NAT */ 950 951 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 952 { 953 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 954 u32 new_mark; 955 956 if (!mask) 957 return; 958 959 new_mark = mark | (READ_ONCE(ct->mark) & ~(mask)); 960 if (READ_ONCE(ct->mark) != new_mark) { 961 WRITE_ONCE(ct->mark, new_mark); 962 if (nf_ct_is_confirmed(ct)) 963 nf_conntrack_event_cache(IPCT_MARK, ct); 964 } 965 #endif 966 } 967 968 static void tcf_ct_act_set_labels(struct nf_conn *ct, 969 u32 *labels, 970 u32 *labels_m) 971 { 972 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 973 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 974 975 if (!memchr_inv(labels_m, 0, labels_sz)) 976 return; 977 978 nf_connlabels_replace(ct, labels, labels_m, 4); 979 #endif 980 } 981 982 static int tcf_ct_act_nat(struct sk_buff *skb, 983 struct nf_conn *ct, 984 enum ip_conntrack_info ctinfo, 985 int ct_action, 986 struct nf_nat_range2 *range, 987 bool commit) 988 { 989 #if IS_ENABLED(CONFIG_NF_NAT) 990 int err; 991 enum nf_nat_manip_type maniptype; 992 993 if (!(ct_action & TCA_CT_ACT_NAT)) 994 return NF_ACCEPT; 995 996 /* Add NAT extension if not confirmed yet. */ 997 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 998 return NF_DROP; /* Can't NAT. */ 999 1000 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) && 1001 (ctinfo != IP_CT_RELATED || commit)) { 1002 /* NAT an established or related connection like before. */ 1003 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 1004 /* This is the REPLY direction for a connection 1005 * for which NAT was applied in the forward 1006 * direction. Do the reverse NAT. 1007 */ 1008 maniptype = ct->status & IPS_SRC_NAT 1009 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 1010 else 1011 maniptype = ct->status & IPS_SRC_NAT 1012 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 1013 } else if (ct_action & TCA_CT_ACT_NAT_SRC) { 1014 maniptype = NF_NAT_MANIP_SRC; 1015 } else if (ct_action & TCA_CT_ACT_NAT_DST) { 1016 maniptype = NF_NAT_MANIP_DST; 1017 } else { 1018 return NF_ACCEPT; 1019 } 1020 1021 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 1022 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) { 1023 if (ct->status & IPS_SRC_NAT) { 1024 if (maniptype == NF_NAT_MANIP_SRC) 1025 maniptype = NF_NAT_MANIP_DST; 1026 else 1027 maniptype = NF_NAT_MANIP_SRC; 1028 1029 err = ct_nat_execute(skb, ct, ctinfo, range, 1030 maniptype); 1031 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) { 1032 err = ct_nat_execute(skb, ct, ctinfo, NULL, 1033 NF_NAT_MANIP_SRC); 1034 } 1035 } 1036 return err; 1037 #else 1038 return NF_ACCEPT; 1039 #endif 1040 } 1041 1042 TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 1043 struct tcf_result *res) 1044 { 1045 struct net *net = dev_net(skb->dev); 1046 enum ip_conntrack_info ctinfo; 1047 struct tcf_ct *c = to_ct(a); 1048 struct nf_conn *tmpl = NULL; 1049 struct nf_hook_state state; 1050 bool cached, commit, clear; 1051 int nh_ofs, err, retval; 1052 struct tcf_ct_params *p; 1053 bool add_helper = false; 1054 bool skip_add = false; 1055 bool defrag = false; 1056 struct nf_conn *ct; 1057 u8 family; 1058 1059 p = rcu_dereference_bh(c->params); 1060 1061 retval = READ_ONCE(c->tcf_action); 1062 commit = p->ct_action & TCA_CT_ACT_COMMIT; 1063 clear = p->ct_action & TCA_CT_ACT_CLEAR; 1064 tmpl = p->tmpl; 1065 1066 tcf_lastuse_update(&c->tcf_tm); 1067 tcf_action_update_bstats(&c->common, skb); 1068 1069 if (clear) { 1070 tc_skb_cb(skb)->post_ct = false; 1071 ct = nf_ct_get(skb, &ctinfo); 1072 if (ct) { 1073 nf_ct_put(ct); 1074 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 1075 } 1076 1077 goto out_clear; 1078 } 1079 1080 family = tcf_ct_skb_nf_family(skb); 1081 if (family == NFPROTO_UNSPEC) 1082 goto drop; 1083 1084 /* The conntrack module expects to be working at L3. 1085 * We also try to pull the IPv4/6 header to linear area 1086 */ 1087 nh_ofs = skb_network_offset(skb); 1088 skb_pull_rcsum(skb, nh_ofs); 1089 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag); 1090 if (err == -EINPROGRESS) { 1091 retval = TC_ACT_STOLEN; 1092 goto out_clear; 1093 } 1094 if (err) 1095 goto drop; 1096 1097 err = tcf_ct_skb_network_trim(skb, family); 1098 if (err) 1099 goto drop; 1100 1101 /* If we are recirculating packets to match on ct fields and 1102 * committing with a separate ct action, then we don't need to 1103 * actually run the packet through conntrack twice unless it's for a 1104 * different zone. 1105 */ 1106 cached = tcf_ct_skb_nfct_cached(net, skb, p); 1107 if (!cached) { 1108 if (tcf_ct_flow_table_lookup(p, skb, family)) { 1109 skip_add = true; 1110 goto do_nat; 1111 } 1112 1113 /* Associate skb with specified zone. */ 1114 if (tmpl) { 1115 nf_conntrack_put(skb_nfct(skb)); 1116 nf_conntrack_get(&tmpl->ct_general); 1117 nf_ct_set(skb, tmpl, IP_CT_NEW); 1118 } 1119 1120 state.hook = NF_INET_PRE_ROUTING; 1121 state.net = net; 1122 state.pf = family; 1123 err = nf_conntrack_in(skb, &state); 1124 if (err != NF_ACCEPT) 1125 goto out_push; 1126 } 1127 1128 do_nat: 1129 ct = nf_ct_get(skb, &ctinfo); 1130 if (!ct) 1131 goto out_push; 1132 nf_ct_deliver_cached_events(ct); 1133 nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 1134 1135 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 1136 if (err != NF_ACCEPT) 1137 goto drop; 1138 1139 if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) { 1140 err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC); 1141 if (err) 1142 goto drop; 1143 add_helper = true; 1144 if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) { 1145 if (!nfct_seqadj_ext_add(ct)) 1146 goto drop; 1147 } 1148 } 1149 1150 if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) { 1151 if (nf_ct_helper(skb, ct, ctinfo, family) != NF_ACCEPT) 1152 goto drop; 1153 } 1154 1155 if (commit) { 1156 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 1157 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 1158 1159 if (!nf_ct_is_confirmed(ct)) 1160 nf_conn_act_ct_ext_add(ct); 1161 1162 /* This will take care of sending queued events 1163 * even if the connection is already confirmed. 1164 */ 1165 if (nf_conntrack_confirm(skb) != NF_ACCEPT) 1166 goto drop; 1167 } 1168 1169 if (!skip_add) 1170 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 1171 1172 out_push: 1173 skb_push_rcsum(skb, nh_ofs); 1174 1175 tc_skb_cb(skb)->post_ct = true; 1176 tc_skb_cb(skb)->zone = p->zone; 1177 out_clear: 1178 if (defrag) 1179 qdisc_skb_cb(skb)->pkt_len = skb->len; 1180 return retval; 1181 1182 drop: 1183 tcf_action_inc_drop_qstats(&c->common); 1184 return TC_ACT_SHOT; 1185 } 1186 1187 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1188 [TCA_CT_ACTION] = { .type = NLA_U16 }, 1189 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)), 1190 [TCA_CT_ZONE] = { .type = NLA_U16 }, 1191 [TCA_CT_MARK] = { .type = NLA_U32 }, 1192 [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1193 [TCA_CT_LABELS] = { .type = NLA_BINARY, 1194 .len = 128 / BITS_PER_BYTE }, 1195 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1196 .len = 128 / BITS_PER_BYTE }, 1197 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1198 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 1199 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1200 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1201 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1202 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1203 [TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN }, 1204 [TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 }, 1205 [TCA_CT_HELPER_PROTO] = { .type = NLA_U8 }, 1206 }; 1207 1208 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1209 struct tc_ct *parm, 1210 struct nlattr **tb, 1211 struct netlink_ext_ack *extack) 1212 { 1213 struct nf_nat_range2 *range; 1214 1215 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1216 return 0; 1217 1218 if (!IS_ENABLED(CONFIG_NF_NAT)) { 1219 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1220 return -EOPNOTSUPP; 1221 } 1222 1223 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1224 return 0; 1225 1226 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1227 (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1228 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1229 return -EOPNOTSUPP; 1230 } 1231 1232 range = &p->range; 1233 if (tb[TCA_CT_NAT_IPV4_MIN]) { 1234 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1235 1236 p->ipv4_range = true; 1237 range->flags |= NF_NAT_RANGE_MAP_IPS; 1238 range->min_addr.ip = 1239 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1240 1241 range->max_addr.ip = max_attr ? 1242 nla_get_in_addr(max_attr) : 1243 range->min_addr.ip; 1244 } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1245 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1246 1247 p->ipv4_range = false; 1248 range->flags |= NF_NAT_RANGE_MAP_IPS; 1249 range->min_addr.in6 = 1250 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1251 1252 range->max_addr.in6 = max_attr ? 1253 nla_get_in6_addr(max_attr) : 1254 range->min_addr.in6; 1255 } 1256 1257 if (tb[TCA_CT_NAT_PORT_MIN]) { 1258 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1259 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1260 1261 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1262 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1263 range->min_proto.all; 1264 } 1265 1266 return 0; 1267 } 1268 1269 static void tcf_ct_set_key_val(struct nlattr **tb, 1270 void *val, int val_type, 1271 void *mask, int mask_type, 1272 int len) 1273 { 1274 if (!tb[val_type]) 1275 return; 1276 nla_memcpy(val, tb[val_type], len); 1277 1278 if (!mask) 1279 return; 1280 1281 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1282 memset(mask, 0xff, len); 1283 else 1284 nla_memcpy(mask, tb[mask_type], len); 1285 } 1286 1287 static int tcf_ct_fill_params(struct net *net, 1288 struct tcf_ct_params *p, 1289 struct tc_ct *parm, 1290 struct nlattr **tb, 1291 struct netlink_ext_ack *extack) 1292 { 1293 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1294 struct nf_conntrack_zone zone; 1295 int err, family, proto, len; 1296 struct nf_conn *tmpl; 1297 char *name; 1298 1299 p->zone = NF_CT_DEFAULT_ZONE_ID; 1300 1301 tcf_ct_set_key_val(tb, 1302 &p->ct_action, TCA_CT_ACTION, 1303 NULL, TCA_CT_UNSPEC, 1304 sizeof(p->ct_action)); 1305 1306 if (p->ct_action & TCA_CT_ACT_CLEAR) 1307 return 0; 1308 1309 err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1310 if (err) 1311 return err; 1312 1313 if (tb[TCA_CT_MARK]) { 1314 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1315 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1316 return -EOPNOTSUPP; 1317 } 1318 tcf_ct_set_key_val(tb, 1319 &p->mark, TCA_CT_MARK, 1320 &p->mark_mask, TCA_CT_MARK_MASK, 1321 sizeof(p->mark)); 1322 } 1323 1324 if (tb[TCA_CT_LABELS]) { 1325 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1326 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1327 return -EOPNOTSUPP; 1328 } 1329 1330 if (!tn->labels) { 1331 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1332 return -EOPNOTSUPP; 1333 } 1334 tcf_ct_set_key_val(tb, 1335 p->labels, TCA_CT_LABELS, 1336 p->labels_mask, TCA_CT_LABELS_MASK, 1337 sizeof(p->labels)); 1338 } 1339 1340 if (tb[TCA_CT_ZONE]) { 1341 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1342 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1343 return -EOPNOTSUPP; 1344 } 1345 1346 tcf_ct_set_key_val(tb, 1347 &p->zone, TCA_CT_ZONE, 1348 NULL, TCA_CT_UNSPEC, 1349 sizeof(p->zone)); 1350 } 1351 1352 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1353 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1354 if (!tmpl) { 1355 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1356 return -ENOMEM; 1357 } 1358 p->tmpl = tmpl; 1359 if (tb[TCA_CT_HELPER_NAME]) { 1360 name = nla_data(tb[TCA_CT_HELPER_NAME]); 1361 len = nla_len(tb[TCA_CT_HELPER_NAME]); 1362 if (len > 16 || name[len - 1] != '\0') { 1363 NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name."); 1364 err = -EINVAL; 1365 goto err; 1366 } 1367 family = tb[TCA_CT_HELPER_FAMILY] ? nla_get_u8(tb[TCA_CT_HELPER_FAMILY]) : AF_INET; 1368 proto = tb[TCA_CT_HELPER_PROTO] ? nla_get_u8(tb[TCA_CT_HELPER_PROTO]) : IPPROTO_TCP; 1369 err = nf_ct_add_helper(tmpl, name, family, proto, 1370 p->ct_action & TCA_CT_ACT_NAT, &p->helper); 1371 if (err) { 1372 NL_SET_ERR_MSG_MOD(extack, "Failed to add helper"); 1373 goto err; 1374 } 1375 } 1376 1377 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1378 return 0; 1379 err: 1380 nf_ct_put(p->tmpl); 1381 p->tmpl = NULL; 1382 return err; 1383 } 1384 1385 static int tcf_ct_init(struct net *net, struct nlattr *nla, 1386 struct nlattr *est, struct tc_action **a, 1387 struct tcf_proto *tp, u32 flags, 1388 struct netlink_ext_ack *extack) 1389 { 1390 struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id); 1391 bool bind = flags & TCA_ACT_FLAGS_BIND; 1392 struct tcf_ct_params *params = NULL; 1393 struct nlattr *tb[TCA_CT_MAX + 1]; 1394 struct tcf_chain *goto_ch = NULL; 1395 struct tc_ct *parm; 1396 struct tcf_ct *c; 1397 int err, res = 0; 1398 u32 index; 1399 1400 if (!nla) { 1401 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1402 return -EINVAL; 1403 } 1404 1405 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1406 if (err < 0) 1407 return err; 1408 1409 if (!tb[TCA_CT_PARMS]) { 1410 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1411 return -EINVAL; 1412 } 1413 parm = nla_data(tb[TCA_CT_PARMS]); 1414 index = parm->index; 1415 err = tcf_idr_check_alloc(tn, &index, a, bind); 1416 if (err < 0) 1417 return err; 1418 1419 if (!err) { 1420 err = tcf_idr_create_from_flags(tn, index, est, a, 1421 &act_ct_ops, bind, flags); 1422 if (err) { 1423 tcf_idr_cleanup(tn, index); 1424 return err; 1425 } 1426 res = ACT_P_CREATED; 1427 } else { 1428 if (bind) 1429 return 0; 1430 1431 if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 1432 tcf_idr_release(*a, bind); 1433 return -EEXIST; 1434 } 1435 } 1436 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1437 if (err < 0) 1438 goto cleanup; 1439 1440 c = to_ct(*a); 1441 1442 params = kzalloc(sizeof(*params), GFP_KERNEL); 1443 if (unlikely(!params)) { 1444 err = -ENOMEM; 1445 goto cleanup; 1446 } 1447 1448 err = tcf_ct_fill_params(net, params, parm, tb, extack); 1449 if (err) 1450 goto cleanup; 1451 1452 err = tcf_ct_flow_table_get(net, params); 1453 if (err) 1454 goto cleanup; 1455 1456 spin_lock_bh(&c->tcf_lock); 1457 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1458 params = rcu_replace_pointer(c->params, params, 1459 lockdep_is_held(&c->tcf_lock)); 1460 spin_unlock_bh(&c->tcf_lock); 1461 1462 if (goto_ch) 1463 tcf_chain_put_by_act(goto_ch); 1464 if (params) 1465 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1466 1467 return res; 1468 1469 cleanup: 1470 if (goto_ch) 1471 tcf_chain_put_by_act(goto_ch); 1472 if (params) 1473 tcf_ct_params_free(params); 1474 tcf_idr_release(*a, bind); 1475 return err; 1476 } 1477 1478 static void tcf_ct_cleanup(struct tc_action *a) 1479 { 1480 struct tcf_ct_params *params; 1481 struct tcf_ct *c = to_ct(a); 1482 1483 params = rcu_dereference_protected(c->params, 1); 1484 if (params) 1485 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1486 } 1487 1488 static int tcf_ct_dump_key_val(struct sk_buff *skb, 1489 void *val, int val_type, 1490 void *mask, int mask_type, 1491 int len) 1492 { 1493 int err; 1494 1495 if (mask && !memchr_inv(mask, 0, len)) 1496 return 0; 1497 1498 err = nla_put(skb, val_type, len, val); 1499 if (err) 1500 return err; 1501 1502 if (mask_type != TCA_CT_UNSPEC) { 1503 err = nla_put(skb, mask_type, len, mask); 1504 if (err) 1505 return err; 1506 } 1507 1508 return 0; 1509 } 1510 1511 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1512 { 1513 struct nf_nat_range2 *range = &p->range; 1514 1515 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1516 return 0; 1517 1518 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1519 return 0; 1520 1521 if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1522 if (p->ipv4_range) { 1523 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1524 range->min_addr.ip)) 1525 return -1; 1526 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1527 range->max_addr.ip)) 1528 return -1; 1529 } else { 1530 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1531 &range->min_addr.in6)) 1532 return -1; 1533 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1534 &range->max_addr.in6)) 1535 return -1; 1536 } 1537 } 1538 1539 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1540 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1541 range->min_proto.all)) 1542 return -1; 1543 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1544 range->max_proto.all)) 1545 return -1; 1546 } 1547 1548 return 0; 1549 } 1550 1551 static int tcf_ct_dump_helper(struct sk_buff *skb, struct nf_conntrack_helper *helper) 1552 { 1553 if (!helper) 1554 return 0; 1555 1556 if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) || 1557 nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->tuple.src.l3num) || 1558 nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->tuple.dst.protonum)) 1559 return -1; 1560 1561 return 0; 1562 } 1563 1564 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1565 int bind, int ref) 1566 { 1567 unsigned char *b = skb_tail_pointer(skb); 1568 struct tcf_ct *c = to_ct(a); 1569 struct tcf_ct_params *p; 1570 1571 struct tc_ct opt = { 1572 .index = c->tcf_index, 1573 .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1574 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1575 }; 1576 struct tcf_t t; 1577 1578 spin_lock_bh(&c->tcf_lock); 1579 p = rcu_dereference_protected(c->params, 1580 lockdep_is_held(&c->tcf_lock)); 1581 opt.action = c->tcf_action; 1582 1583 if (tcf_ct_dump_key_val(skb, 1584 &p->ct_action, TCA_CT_ACTION, 1585 NULL, TCA_CT_UNSPEC, 1586 sizeof(p->ct_action))) 1587 goto nla_put_failure; 1588 1589 if (p->ct_action & TCA_CT_ACT_CLEAR) 1590 goto skip_dump; 1591 1592 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1593 tcf_ct_dump_key_val(skb, 1594 &p->mark, TCA_CT_MARK, 1595 &p->mark_mask, TCA_CT_MARK_MASK, 1596 sizeof(p->mark))) 1597 goto nla_put_failure; 1598 1599 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1600 tcf_ct_dump_key_val(skb, 1601 p->labels, TCA_CT_LABELS, 1602 p->labels_mask, TCA_CT_LABELS_MASK, 1603 sizeof(p->labels))) 1604 goto nla_put_failure; 1605 1606 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1607 tcf_ct_dump_key_val(skb, 1608 &p->zone, TCA_CT_ZONE, 1609 NULL, TCA_CT_UNSPEC, 1610 sizeof(p->zone))) 1611 goto nla_put_failure; 1612 1613 if (tcf_ct_dump_nat(skb, p)) 1614 goto nla_put_failure; 1615 1616 if (tcf_ct_dump_helper(skb, p->helper)) 1617 goto nla_put_failure; 1618 1619 skip_dump: 1620 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1621 goto nla_put_failure; 1622 1623 tcf_tm_dump(&t, &c->tcf_tm); 1624 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1625 goto nla_put_failure; 1626 spin_unlock_bh(&c->tcf_lock); 1627 1628 return skb->len; 1629 nla_put_failure: 1630 spin_unlock_bh(&c->tcf_lock); 1631 nlmsg_trim(skb, b); 1632 return -1; 1633 } 1634 1635 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, 1636 u64 drops, u64 lastuse, bool hw) 1637 { 1638 struct tcf_ct *c = to_ct(a); 1639 1640 tcf_action_update_stats(a, bytes, packets, drops, hw); 1641 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1642 } 1643 1644 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data, 1645 u32 *index_inc, bool bind, 1646 struct netlink_ext_ack *extack) 1647 { 1648 if (bind) { 1649 struct flow_action_entry *entry = entry_data; 1650 1651 entry->id = FLOW_ACTION_CT; 1652 entry->ct.action = tcf_ct_action(act); 1653 entry->ct.zone = tcf_ct_zone(act); 1654 entry->ct.flow_table = tcf_ct_ft(act); 1655 *index_inc = 1; 1656 } else { 1657 struct flow_offload_action *fl_action = entry_data; 1658 1659 fl_action->id = FLOW_ACTION_CT; 1660 } 1661 1662 return 0; 1663 } 1664 1665 static struct tc_action_ops act_ct_ops = { 1666 .kind = "ct", 1667 .id = TCA_ID_CT, 1668 .owner = THIS_MODULE, 1669 .act = tcf_ct_act, 1670 .dump = tcf_ct_dump, 1671 .init = tcf_ct_init, 1672 .cleanup = tcf_ct_cleanup, 1673 .stats_update = tcf_stats_update, 1674 .offload_act_setup = tcf_ct_offload_act_setup, 1675 .size = sizeof(struct tcf_ct), 1676 }; 1677 1678 static __net_init int ct_init_net(struct net *net) 1679 { 1680 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1681 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1682 1683 if (nf_connlabels_get(net, n_bits - 1)) { 1684 tn->labels = false; 1685 pr_err("act_ct: Failed to set connlabels length"); 1686 } else { 1687 tn->labels = true; 1688 } 1689 1690 return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1691 } 1692 1693 static void __net_exit ct_exit_net(struct list_head *net_list) 1694 { 1695 struct net *net; 1696 1697 rtnl_lock(); 1698 list_for_each_entry(net, net_list, exit_list) { 1699 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1700 1701 if (tn->labels) 1702 nf_connlabels_put(net); 1703 } 1704 rtnl_unlock(); 1705 1706 tc_action_net_exit(net_list, act_ct_ops.net_id); 1707 } 1708 1709 static struct pernet_operations ct_net_ops = { 1710 .init = ct_init_net, 1711 .exit_batch = ct_exit_net, 1712 .id = &act_ct_ops.net_id, 1713 .size = sizeof(struct tc_ct_action_net), 1714 }; 1715 1716 static int __init ct_init_module(void) 1717 { 1718 int err; 1719 1720 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1721 if (!act_ct_wq) 1722 return -ENOMEM; 1723 1724 err = tcf_ct_flow_tables_init(); 1725 if (err) 1726 goto err_tbl_init; 1727 1728 err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1729 if (err) 1730 goto err_register; 1731 1732 static_branch_inc(&tcf_frag_xmit_count); 1733 1734 return 0; 1735 1736 err_register: 1737 tcf_ct_flow_tables_uninit(); 1738 err_tbl_init: 1739 destroy_workqueue(act_ct_wq); 1740 return err; 1741 } 1742 1743 static void __exit ct_cleanup_module(void) 1744 { 1745 static_branch_dec(&tcf_frag_xmit_count); 1746 tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1747 tcf_ct_flow_tables_uninit(); 1748 destroy_workqueue(act_ct_wq); 1749 } 1750 1751 module_init(ct_init_module); 1752 module_exit(ct_cleanup_module); 1753 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1754 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1755 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1756 MODULE_DESCRIPTION("Connection tracking action"); 1757 MODULE_LICENSE("GPL v2"); 1758