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 enum ip_conntrack_info ctinfo, 174 struct flow_action *action) 175 { 176 struct nf_conn_labels *ct_labels; 177 struct flow_action_entry *entry; 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 /* 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 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_info ctinfo; 246 enum ip_conntrack_dir dir; 247 int i, err; 248 249 switch (tdir) { 250 case FLOW_OFFLOAD_DIR_ORIGINAL: 251 dir = IP_CT_DIR_ORIGINAL; 252 ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ? 253 IP_CT_ESTABLISHED : IP_CT_NEW; 254 if (ctinfo == IP_CT_ESTABLISHED) 255 set_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags); 256 break; 257 case FLOW_OFFLOAD_DIR_REPLY: 258 dir = IP_CT_DIR_REPLY; 259 ctinfo = IP_CT_ESTABLISHED_REPLY; 260 break; 261 default: 262 return -EOPNOTSUPP; 263 } 264 265 err = tcf_ct_flow_table_add_action_nat(net, ct, dir, action); 266 if (err) 267 goto err_nat; 268 269 tcf_ct_flow_table_add_action_meta(ct, dir, ctinfo, action); 270 return 0; 271 272 err_nat: 273 /* Clear filled actions */ 274 for (i = num_entries; i < action->num_entries; i++) 275 memset(&action->entries[i], 0, sizeof(action->entries[i])); 276 action->num_entries = num_entries; 277 278 return err; 279 } 280 281 static bool tcf_ct_flow_is_outdated(const struct flow_offload *flow) 282 { 283 return test_bit(IPS_SEEN_REPLY_BIT, &flow->ct->status) && 284 test_bit(IPS_HW_OFFLOAD_BIT, &flow->ct->status) && 285 !test_bit(NF_FLOW_HW_PENDING, &flow->flags) && 286 !test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags); 287 } 288 289 static struct nf_flowtable_type flowtable_ct = { 290 .gc = tcf_ct_flow_is_outdated, 291 .action = tcf_ct_flow_table_fill_actions, 292 .owner = THIS_MODULE, 293 }; 294 295 static int tcf_ct_flow_table_get(struct net *net, struct tcf_ct_params *params) 296 { 297 struct tcf_ct_flow_table *ct_ft; 298 int err = -ENOMEM; 299 300 mutex_lock(&zones_mutex); 301 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 302 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 303 goto out_unlock; 304 305 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 306 if (!ct_ft) 307 goto err_alloc; 308 refcount_set(&ct_ft->ref, 1); 309 310 ct_ft->zone = params->zone; 311 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 312 if (err) 313 goto err_insert; 314 315 ct_ft->nf_ft.type = &flowtable_ct; 316 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 317 NF_FLOWTABLE_COUNTER; 318 err = nf_flow_table_init(&ct_ft->nf_ft); 319 if (err) 320 goto err_init; 321 write_pnet(&ct_ft->nf_ft.net, net); 322 323 __module_get(THIS_MODULE); 324 out_unlock: 325 params->ct_ft = ct_ft; 326 params->nf_ft = &ct_ft->nf_ft; 327 mutex_unlock(&zones_mutex); 328 329 return 0; 330 331 err_init: 332 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 333 err_insert: 334 kfree(ct_ft); 335 err_alloc: 336 mutex_unlock(&zones_mutex); 337 return err; 338 } 339 340 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 341 { 342 struct flow_block_cb *block_cb, *tmp_cb; 343 struct tcf_ct_flow_table *ct_ft; 344 struct flow_block *block; 345 346 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 347 rwork); 348 nf_flow_table_free(&ct_ft->nf_ft); 349 350 /* Remove any remaining callbacks before cleanup */ 351 block = &ct_ft->nf_ft.flow_block; 352 down_write(&ct_ft->nf_ft.flow_block_lock); 353 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) { 354 list_del(&block_cb->list); 355 flow_block_cb_free(block_cb); 356 } 357 up_write(&ct_ft->nf_ft.flow_block_lock); 358 kfree(ct_ft); 359 360 module_put(THIS_MODULE); 361 } 362 363 static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft) 364 { 365 if (refcount_dec_and_test(&ct_ft->ref)) { 366 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 367 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 368 queue_rcu_work(act_ct_wq, &ct_ft->rwork); 369 } 370 } 371 372 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry, 373 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir) 374 { 375 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC; 376 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir]; 377 } 378 379 static void tcf_ct_flow_ct_ext_ifidx_update(struct flow_offload *entry) 380 { 381 struct nf_conn_act_ct_ext *act_ct_ext; 382 383 act_ct_ext = nf_conn_act_ct_ext_find(entry->ct); 384 if (act_ct_ext) { 385 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 386 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 387 } 388 } 389 390 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 391 struct nf_conn *ct, 392 bool tcp, bool bidirectional) 393 { 394 struct nf_conn_act_ct_ext *act_ct_ext; 395 struct flow_offload *entry; 396 int err; 397 398 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 399 return; 400 401 entry = flow_offload_alloc(ct); 402 if (!entry) { 403 WARN_ON_ONCE(1); 404 goto err_alloc; 405 } 406 407 if (tcp) { 408 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 409 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 410 } 411 if (bidirectional) 412 __set_bit(NF_FLOW_HW_BIDIRECTIONAL, &entry->flags); 413 414 act_ct_ext = nf_conn_act_ct_ext_find(ct); 415 if (act_ct_ext) { 416 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 417 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 418 } 419 420 err = flow_offload_add(&ct_ft->nf_ft, entry); 421 if (err) 422 goto err_add; 423 424 return; 425 426 err_add: 427 flow_offload_free(entry); 428 err_alloc: 429 clear_bit(IPS_OFFLOAD_BIT, &ct->status); 430 } 431 432 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 433 struct nf_conn *ct, 434 enum ip_conntrack_info ctinfo) 435 { 436 bool tcp = false, bidirectional = true; 437 438 switch (nf_ct_protonum(ct)) { 439 case IPPROTO_TCP: 440 if ((ctinfo != IP_CT_ESTABLISHED && 441 ctinfo != IP_CT_ESTABLISHED_REPLY) || 442 !test_bit(IPS_ASSURED_BIT, &ct->status) || 443 ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 444 return; 445 446 tcp = true; 447 break; 448 case IPPROTO_UDP: 449 if (!nf_ct_is_confirmed(ct)) 450 return; 451 if (!test_bit(IPS_ASSURED_BIT, &ct->status)) 452 bidirectional = false; 453 break; 454 #ifdef CONFIG_NF_CT_PROTO_GRE 455 case IPPROTO_GRE: { 456 struct nf_conntrack_tuple *tuple; 457 458 if ((ctinfo != IP_CT_ESTABLISHED && 459 ctinfo != IP_CT_ESTABLISHED_REPLY) || 460 !test_bit(IPS_ASSURED_BIT, &ct->status) || 461 ct->status & IPS_NAT_MASK) 462 return; 463 464 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; 465 /* No support for GRE v1 */ 466 if (tuple->src.u.gre.key || tuple->dst.u.gre.key) 467 return; 468 break; 469 } 470 #endif 471 default: 472 return; 473 } 474 475 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 476 ct->status & IPS_SEQ_ADJUST) 477 return; 478 479 tcf_ct_flow_table_add(ct_ft, ct, tcp, bidirectional); 480 } 481 482 static bool 483 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 484 struct flow_offload_tuple *tuple, 485 struct tcphdr **tcph) 486 { 487 struct flow_ports *ports; 488 unsigned int thoff; 489 struct iphdr *iph; 490 size_t hdrsize; 491 u8 ipproto; 492 493 if (!pskb_network_may_pull(skb, sizeof(*iph))) 494 return false; 495 496 iph = ip_hdr(skb); 497 thoff = iph->ihl * 4; 498 499 if (ip_is_fragment(iph) || 500 unlikely(thoff != sizeof(struct iphdr))) 501 return false; 502 503 ipproto = iph->protocol; 504 switch (ipproto) { 505 case IPPROTO_TCP: 506 hdrsize = sizeof(struct tcphdr); 507 break; 508 case IPPROTO_UDP: 509 hdrsize = sizeof(*ports); 510 break; 511 #ifdef CONFIG_NF_CT_PROTO_GRE 512 case IPPROTO_GRE: 513 hdrsize = sizeof(struct gre_base_hdr); 514 break; 515 #endif 516 default: 517 return false; 518 } 519 520 if (iph->ttl <= 1) 521 return false; 522 523 if (!pskb_network_may_pull(skb, thoff + hdrsize)) 524 return false; 525 526 switch (ipproto) { 527 case IPPROTO_TCP: 528 *tcph = (void *)(skb_network_header(skb) + thoff); 529 fallthrough; 530 case IPPROTO_UDP: 531 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 532 tuple->src_port = ports->source; 533 tuple->dst_port = ports->dest; 534 break; 535 case IPPROTO_GRE: { 536 struct gre_base_hdr *greh; 537 538 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 539 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 540 return false; 541 break; 542 } 543 } 544 545 iph = ip_hdr(skb); 546 547 tuple->src_v4.s_addr = iph->saddr; 548 tuple->dst_v4.s_addr = iph->daddr; 549 tuple->l3proto = AF_INET; 550 tuple->l4proto = ipproto; 551 552 return true; 553 } 554 555 static bool 556 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 557 struct flow_offload_tuple *tuple, 558 struct tcphdr **tcph) 559 { 560 struct flow_ports *ports; 561 struct ipv6hdr *ip6h; 562 unsigned int thoff; 563 size_t hdrsize; 564 u8 nexthdr; 565 566 if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 567 return false; 568 569 ip6h = ipv6_hdr(skb); 570 thoff = sizeof(*ip6h); 571 572 nexthdr = ip6h->nexthdr; 573 switch (nexthdr) { 574 case IPPROTO_TCP: 575 hdrsize = sizeof(struct tcphdr); 576 break; 577 case IPPROTO_UDP: 578 hdrsize = sizeof(*ports); 579 break; 580 #ifdef CONFIG_NF_CT_PROTO_GRE 581 case IPPROTO_GRE: 582 hdrsize = sizeof(struct gre_base_hdr); 583 break; 584 #endif 585 default: 586 return false; 587 } 588 589 if (ip6h->hop_limit <= 1) 590 return false; 591 592 if (!pskb_network_may_pull(skb, thoff + hdrsize)) 593 return false; 594 595 switch (nexthdr) { 596 case IPPROTO_TCP: 597 *tcph = (void *)(skb_network_header(skb) + thoff); 598 fallthrough; 599 case IPPROTO_UDP: 600 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 601 tuple->src_port = ports->source; 602 tuple->dst_port = ports->dest; 603 break; 604 case IPPROTO_GRE: { 605 struct gre_base_hdr *greh; 606 607 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff); 608 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0) 609 return false; 610 break; 611 } 612 } 613 614 ip6h = ipv6_hdr(skb); 615 616 tuple->src_v6 = ip6h->saddr; 617 tuple->dst_v6 = ip6h->daddr; 618 tuple->l3proto = AF_INET6; 619 tuple->l4proto = nexthdr; 620 621 return true; 622 } 623 624 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 625 struct sk_buff *skb, 626 u8 family) 627 { 628 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 629 struct flow_offload_tuple_rhash *tuplehash; 630 struct flow_offload_tuple tuple = {}; 631 enum ip_conntrack_info ctinfo; 632 struct tcphdr *tcph = NULL; 633 bool force_refresh = false; 634 struct flow_offload *flow; 635 struct nf_conn *ct; 636 u8 dir; 637 638 switch (family) { 639 case NFPROTO_IPV4: 640 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 641 return false; 642 break; 643 case NFPROTO_IPV6: 644 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 645 return false; 646 break; 647 default: 648 return false; 649 } 650 651 tuplehash = flow_offload_lookup(nf_ft, &tuple); 652 if (!tuplehash) 653 return false; 654 655 dir = tuplehash->tuple.dir; 656 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 657 ct = flow->ct; 658 659 if (dir == FLOW_OFFLOAD_DIR_REPLY && 660 !test_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags)) { 661 /* Only offload reply direction after connection became 662 * assured. 663 */ 664 if (test_bit(IPS_ASSURED_BIT, &ct->status)) 665 set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags); 666 else if (test_bit(NF_FLOW_HW_ESTABLISHED, &flow->flags)) 667 /* If flow_table flow has already been updated to the 668 * established state, then don't refresh. 669 */ 670 return false; 671 force_refresh = true; 672 } 673 674 if (tcph && (unlikely(tcph->fin || tcph->rst))) { 675 flow_offload_teardown(flow); 676 return false; 677 } 678 679 if (dir == FLOW_OFFLOAD_DIR_ORIGINAL) 680 ctinfo = test_bit(IPS_SEEN_REPLY_BIT, &ct->status) ? 681 IP_CT_ESTABLISHED : IP_CT_NEW; 682 else 683 ctinfo = IP_CT_ESTABLISHED_REPLY; 684 685 nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 686 tcf_ct_flow_ct_ext_ifidx_update(flow); 687 flow_offload_refresh(nf_ft, flow, force_refresh); 688 if (!test_bit(IPS_ASSURED_BIT, &ct->status)) { 689 /* Process this flow in SW to allow promoting to ASSURED */ 690 return false; 691 } 692 693 nf_conntrack_get(&ct->ct_general); 694 nf_ct_set(skb, ct, ctinfo); 695 if (nf_ft->flags & NF_FLOWTABLE_COUNTER) 696 nf_ct_acct_update(ct, dir, skb->len); 697 698 return true; 699 } 700 701 static int tcf_ct_flow_tables_init(void) 702 { 703 return rhashtable_init(&zones_ht, &zones_params); 704 } 705 706 static void tcf_ct_flow_tables_uninit(void) 707 { 708 rhashtable_destroy(&zones_ht); 709 } 710 711 static struct tc_action_ops act_ct_ops; 712 713 struct tc_ct_action_net { 714 struct tc_action_net tn; /* Must be first */ 715 }; 716 717 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 718 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 719 struct tcf_ct_params *p) 720 { 721 enum ip_conntrack_info ctinfo; 722 struct nf_conn *ct; 723 724 ct = nf_ct_get(skb, &ctinfo); 725 if (!ct) 726 return false; 727 if (!net_eq(net, read_pnet(&ct->ct_net))) 728 goto drop_ct; 729 if (nf_ct_zone(ct)->id != p->zone) 730 goto drop_ct; 731 if (p->helper) { 732 struct nf_conn_help *help; 733 734 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); 735 if (help && rcu_access_pointer(help->helper) != p->helper) 736 goto drop_ct; 737 } 738 739 /* Force conntrack entry direction. */ 740 if ((p->ct_action & TCA_CT_ACT_FORCE) && 741 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 742 if (nf_ct_is_confirmed(ct)) 743 nf_ct_kill(ct); 744 745 goto drop_ct; 746 } 747 748 return true; 749 750 drop_ct: 751 nf_ct_put(ct); 752 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 753 754 return false; 755 } 756 757 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 758 { 759 u8 family = NFPROTO_UNSPEC; 760 761 switch (skb_protocol(skb, true)) { 762 case htons(ETH_P_IP): 763 family = NFPROTO_IPV4; 764 break; 765 case htons(ETH_P_IPV6): 766 family = NFPROTO_IPV6; 767 break; 768 default: 769 break; 770 } 771 772 return family; 773 } 774 775 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 776 { 777 unsigned int len; 778 779 len = skb_network_offset(skb) + sizeof(struct iphdr); 780 if (unlikely(skb->len < len)) 781 return -EINVAL; 782 if (unlikely(!pskb_may_pull(skb, len))) 783 return -ENOMEM; 784 785 *frag = ip_is_fragment(ip_hdr(skb)); 786 return 0; 787 } 788 789 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 790 { 791 unsigned int flags = 0, len, payload_ofs = 0; 792 unsigned short frag_off; 793 int nexthdr; 794 795 len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 796 if (unlikely(skb->len < len)) 797 return -EINVAL; 798 if (unlikely(!pskb_may_pull(skb, len))) 799 return -ENOMEM; 800 801 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 802 if (unlikely(nexthdr < 0)) 803 return -EPROTO; 804 805 *frag = flags & IP6_FH_F_FRAG; 806 return 0; 807 } 808 809 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 810 u8 family, u16 zone, bool *defrag) 811 { 812 enum ip_conntrack_info ctinfo; 813 struct nf_conn *ct; 814 int err = 0; 815 bool frag; 816 u8 proto; 817 u16 mru; 818 819 /* Previously seen (loopback)? Ignore. */ 820 ct = nf_ct_get(skb, &ctinfo); 821 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 822 return 0; 823 824 if (family == NFPROTO_IPV4) 825 err = tcf_ct_ipv4_is_fragment(skb, &frag); 826 else 827 err = tcf_ct_ipv6_is_fragment(skb, &frag); 828 if (err || !frag) 829 return err; 830 831 skb_get(skb); 832 err = nf_ct_handle_fragments(net, skb, zone, family, &proto, &mru); 833 if (err) 834 return err; 835 836 *defrag = true; 837 tc_skb_cb(skb)->mru = mru; 838 839 return 0; 840 } 841 842 static void tcf_ct_params_free(struct tcf_ct_params *params) 843 { 844 if (params->helper) { 845 #if IS_ENABLED(CONFIG_NF_NAT) 846 if (params->ct_action & TCA_CT_ACT_NAT) 847 nf_nat_helper_put(params->helper); 848 #endif 849 nf_conntrack_helper_put(params->helper); 850 } 851 if (params->ct_ft) 852 tcf_ct_flow_table_put(params->ct_ft); 853 if (params->tmpl) { 854 if (params->put_labels) 855 nf_connlabels_put(nf_ct_net(params->tmpl)); 856 857 nf_ct_put(params->tmpl); 858 } 859 860 kfree(params); 861 } 862 863 static void tcf_ct_params_free_rcu(struct rcu_head *head) 864 { 865 struct tcf_ct_params *params; 866 867 params = container_of(head, struct tcf_ct_params, rcu); 868 tcf_ct_params_free(params); 869 } 870 871 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 872 { 873 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 874 u32 new_mark; 875 876 if (!mask) 877 return; 878 879 new_mark = mark | (READ_ONCE(ct->mark) & ~(mask)); 880 if (READ_ONCE(ct->mark) != new_mark) { 881 WRITE_ONCE(ct->mark, new_mark); 882 if (nf_ct_is_confirmed(ct)) 883 nf_conntrack_event_cache(IPCT_MARK, ct); 884 } 885 #endif 886 } 887 888 static void tcf_ct_act_set_labels(struct nf_conn *ct, 889 u32 *labels, 890 u32 *labels_m) 891 { 892 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 893 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 894 895 if (!memchr_inv(labels_m, 0, labels_sz)) 896 return; 897 898 nf_connlabels_replace(ct, labels, labels_m, 4); 899 #endif 900 } 901 902 static int tcf_ct_act_nat(struct sk_buff *skb, 903 struct nf_conn *ct, 904 enum ip_conntrack_info ctinfo, 905 int ct_action, 906 struct nf_nat_range2 *range, 907 bool commit) 908 { 909 #if IS_ENABLED(CONFIG_NF_NAT) 910 int err, action = 0; 911 912 if (!(ct_action & TCA_CT_ACT_NAT)) 913 return NF_ACCEPT; 914 if (ct_action & TCA_CT_ACT_NAT_SRC) 915 action |= BIT(NF_NAT_MANIP_SRC); 916 if (ct_action & TCA_CT_ACT_NAT_DST) 917 action |= BIT(NF_NAT_MANIP_DST); 918 919 err = nf_ct_nat(skb, ct, ctinfo, &action, range, commit); 920 921 if (action & BIT(NF_NAT_MANIP_SRC)) 922 tc_skb_cb(skb)->post_ct_snat = 1; 923 if (action & BIT(NF_NAT_MANIP_DST)) 924 tc_skb_cb(skb)->post_ct_dnat = 1; 925 926 return err; 927 #else 928 return NF_ACCEPT; 929 #endif 930 } 931 932 TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 933 struct tcf_result *res) 934 { 935 struct net *net = dev_net(skb->dev); 936 enum ip_conntrack_info ctinfo; 937 struct tcf_ct *c = to_ct(a); 938 struct nf_conn *tmpl = NULL; 939 struct nf_hook_state state; 940 bool cached, commit, clear; 941 int nh_ofs, err, retval; 942 struct tcf_ct_params *p; 943 bool add_helper = false; 944 bool skip_add = false; 945 bool defrag = false; 946 struct nf_conn *ct; 947 u8 family; 948 949 p = rcu_dereference_bh(c->params); 950 951 retval = READ_ONCE(c->tcf_action); 952 commit = p->ct_action & TCA_CT_ACT_COMMIT; 953 clear = p->ct_action & TCA_CT_ACT_CLEAR; 954 tmpl = p->tmpl; 955 956 tcf_lastuse_update(&c->tcf_tm); 957 tcf_action_update_bstats(&c->common, skb); 958 959 if (clear) { 960 tc_skb_cb(skb)->post_ct = false; 961 ct = nf_ct_get(skb, &ctinfo); 962 if (ct) { 963 nf_ct_put(ct); 964 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 965 } 966 967 goto out_clear; 968 } 969 970 family = tcf_ct_skb_nf_family(skb); 971 if (family == NFPROTO_UNSPEC) 972 goto drop; 973 974 /* The conntrack module expects to be working at L3. 975 * We also try to pull the IPv4/6 header to linear area 976 */ 977 nh_ofs = skb_network_offset(skb); 978 skb_pull_rcsum(skb, nh_ofs); 979 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag); 980 if (err == -EINPROGRESS) { 981 retval = TC_ACT_STOLEN; 982 goto out_clear; 983 } 984 if (err) 985 goto drop; 986 987 err = nf_ct_skb_network_trim(skb, family); 988 if (err) 989 goto drop; 990 991 /* If we are recirculating packets to match on ct fields and 992 * committing with a separate ct action, then we don't need to 993 * actually run the packet through conntrack twice unless it's for a 994 * different zone. 995 */ 996 cached = tcf_ct_skb_nfct_cached(net, skb, p); 997 if (!cached) { 998 if (tcf_ct_flow_table_lookup(p, skb, family)) { 999 skip_add = true; 1000 goto do_nat; 1001 } 1002 1003 /* Associate skb with specified zone. */ 1004 if (tmpl) { 1005 nf_conntrack_put(skb_nfct(skb)); 1006 nf_conntrack_get(&tmpl->ct_general); 1007 nf_ct_set(skb, tmpl, IP_CT_NEW); 1008 } 1009 1010 state.hook = NF_INET_PRE_ROUTING; 1011 state.net = net; 1012 state.pf = family; 1013 err = nf_conntrack_in(skb, &state); 1014 if (err != NF_ACCEPT) 1015 goto out_push; 1016 } 1017 1018 do_nat: 1019 ct = nf_ct_get(skb, &ctinfo); 1020 if (!ct) 1021 goto out_push; 1022 nf_ct_deliver_cached_events(ct); 1023 nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 1024 1025 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 1026 if (err != NF_ACCEPT) 1027 goto drop; 1028 1029 if (!nf_ct_is_confirmed(ct) && commit && p->helper && !nfct_help(ct)) { 1030 err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC); 1031 if (err) 1032 goto drop; 1033 add_helper = true; 1034 if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) { 1035 if (!nfct_seqadj_ext_add(ct)) 1036 goto drop; 1037 } 1038 } 1039 1040 if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) { 1041 if (nf_ct_helper(skb, ct, ctinfo, family) != NF_ACCEPT) 1042 goto drop; 1043 } 1044 1045 if (commit) { 1046 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 1047 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 1048 1049 if (!nf_ct_is_confirmed(ct)) 1050 nf_conn_act_ct_ext_add(skb, ct, ctinfo); 1051 1052 /* This will take care of sending queued events 1053 * even if the connection is already confirmed. 1054 */ 1055 if (nf_conntrack_confirm(skb) != NF_ACCEPT) 1056 goto drop; 1057 } 1058 1059 if (!skip_add) 1060 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 1061 1062 out_push: 1063 skb_push_rcsum(skb, nh_ofs); 1064 1065 tc_skb_cb(skb)->post_ct = true; 1066 tc_skb_cb(skb)->zone = p->zone; 1067 out_clear: 1068 if (defrag) 1069 qdisc_skb_cb(skb)->pkt_len = skb->len; 1070 return retval; 1071 1072 drop: 1073 tcf_action_inc_drop_qstats(&c->common); 1074 return TC_ACT_SHOT; 1075 } 1076 1077 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1078 [TCA_CT_ACTION] = { .type = NLA_U16 }, 1079 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)), 1080 [TCA_CT_ZONE] = { .type = NLA_U16 }, 1081 [TCA_CT_MARK] = { .type = NLA_U32 }, 1082 [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1083 [TCA_CT_LABELS] = { .type = NLA_BINARY, 1084 .len = 128 / BITS_PER_BYTE }, 1085 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1086 .len = 128 / BITS_PER_BYTE }, 1087 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1088 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 1089 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1090 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1091 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1092 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1093 [TCA_CT_HELPER_NAME] = { .type = NLA_STRING, .len = NF_CT_HELPER_NAME_LEN }, 1094 [TCA_CT_HELPER_FAMILY] = { .type = NLA_U8 }, 1095 [TCA_CT_HELPER_PROTO] = { .type = NLA_U8 }, 1096 }; 1097 1098 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1099 struct tc_ct *parm, 1100 struct nlattr **tb, 1101 struct netlink_ext_ack *extack) 1102 { 1103 struct nf_nat_range2 *range; 1104 1105 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1106 return 0; 1107 1108 if (!IS_ENABLED(CONFIG_NF_NAT)) { 1109 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1110 return -EOPNOTSUPP; 1111 } 1112 1113 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1114 return 0; 1115 1116 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1117 (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1118 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1119 return -EOPNOTSUPP; 1120 } 1121 1122 range = &p->range; 1123 if (tb[TCA_CT_NAT_IPV4_MIN]) { 1124 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1125 1126 p->ipv4_range = true; 1127 range->flags |= NF_NAT_RANGE_MAP_IPS; 1128 range->min_addr.ip = 1129 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1130 1131 range->max_addr.ip = max_attr ? 1132 nla_get_in_addr(max_attr) : 1133 range->min_addr.ip; 1134 } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1135 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1136 1137 p->ipv4_range = false; 1138 range->flags |= NF_NAT_RANGE_MAP_IPS; 1139 range->min_addr.in6 = 1140 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1141 1142 range->max_addr.in6 = max_attr ? 1143 nla_get_in6_addr(max_attr) : 1144 range->min_addr.in6; 1145 } 1146 1147 if (tb[TCA_CT_NAT_PORT_MIN]) { 1148 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1149 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1150 1151 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1152 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1153 range->min_proto.all; 1154 } 1155 1156 return 0; 1157 } 1158 1159 static void tcf_ct_set_key_val(struct nlattr **tb, 1160 void *val, int val_type, 1161 void *mask, int mask_type, 1162 int len) 1163 { 1164 if (!tb[val_type]) 1165 return; 1166 nla_memcpy(val, tb[val_type], len); 1167 1168 if (!mask) 1169 return; 1170 1171 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1172 memset(mask, 0xff, len); 1173 else 1174 nla_memcpy(mask, tb[mask_type], len); 1175 } 1176 1177 static int tcf_ct_fill_params(struct net *net, 1178 struct tcf_ct_params *p, 1179 struct tc_ct *parm, 1180 struct nlattr **tb, 1181 struct netlink_ext_ack *extack) 1182 { 1183 struct nf_conntrack_zone zone; 1184 int err, family, proto, len; 1185 bool put_labels = false; 1186 struct nf_conn *tmpl; 1187 char *name; 1188 1189 p->zone = NF_CT_DEFAULT_ZONE_ID; 1190 1191 tcf_ct_set_key_val(tb, 1192 &p->ct_action, TCA_CT_ACTION, 1193 NULL, TCA_CT_UNSPEC, 1194 sizeof(p->ct_action)); 1195 1196 if (p->ct_action & TCA_CT_ACT_CLEAR) 1197 return 0; 1198 1199 err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1200 if (err) 1201 return err; 1202 1203 if (tb[TCA_CT_MARK]) { 1204 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1205 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1206 return -EOPNOTSUPP; 1207 } 1208 tcf_ct_set_key_val(tb, 1209 &p->mark, TCA_CT_MARK, 1210 &p->mark_mask, TCA_CT_MARK_MASK, 1211 sizeof(p->mark)); 1212 } 1213 1214 if (tb[TCA_CT_LABELS]) { 1215 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1216 1217 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1218 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1219 return -EOPNOTSUPP; 1220 } 1221 1222 if (nf_connlabels_get(net, n_bits - 1)) { 1223 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1224 return -EOPNOTSUPP; 1225 } else { 1226 put_labels = true; 1227 } 1228 1229 tcf_ct_set_key_val(tb, 1230 p->labels, TCA_CT_LABELS, 1231 p->labels_mask, TCA_CT_LABELS_MASK, 1232 sizeof(p->labels)); 1233 } 1234 1235 if (tb[TCA_CT_ZONE]) { 1236 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1237 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1238 return -EOPNOTSUPP; 1239 } 1240 1241 tcf_ct_set_key_val(tb, 1242 &p->zone, TCA_CT_ZONE, 1243 NULL, TCA_CT_UNSPEC, 1244 sizeof(p->zone)); 1245 } 1246 1247 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1248 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1249 if (!tmpl) { 1250 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1251 return -ENOMEM; 1252 } 1253 p->tmpl = tmpl; 1254 if (tb[TCA_CT_HELPER_NAME]) { 1255 name = nla_data(tb[TCA_CT_HELPER_NAME]); 1256 len = nla_len(tb[TCA_CT_HELPER_NAME]); 1257 if (len > 16 || name[len - 1] != '\0') { 1258 NL_SET_ERR_MSG_MOD(extack, "Failed to parse helper name."); 1259 err = -EINVAL; 1260 goto err; 1261 } 1262 family = tb[TCA_CT_HELPER_FAMILY] ? nla_get_u8(tb[TCA_CT_HELPER_FAMILY]) : AF_INET; 1263 proto = tb[TCA_CT_HELPER_PROTO] ? nla_get_u8(tb[TCA_CT_HELPER_PROTO]) : IPPROTO_TCP; 1264 err = nf_ct_add_helper(tmpl, name, family, proto, 1265 p->ct_action & TCA_CT_ACT_NAT, &p->helper); 1266 if (err) { 1267 NL_SET_ERR_MSG_MOD(extack, "Failed to add helper"); 1268 goto err; 1269 } 1270 } 1271 1272 p->put_labels = put_labels; 1273 1274 if (p->ct_action & TCA_CT_ACT_COMMIT) 1275 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1276 return 0; 1277 err: 1278 if (put_labels) 1279 nf_connlabels_put(net); 1280 1281 nf_ct_put(p->tmpl); 1282 p->tmpl = NULL; 1283 return err; 1284 } 1285 1286 static int tcf_ct_init(struct net *net, struct nlattr *nla, 1287 struct nlattr *est, struct tc_action **a, 1288 struct tcf_proto *tp, u32 flags, 1289 struct netlink_ext_ack *extack) 1290 { 1291 struct tc_action_net *tn = net_generic(net, act_ct_ops.net_id); 1292 bool bind = flags & TCA_ACT_FLAGS_BIND; 1293 struct tcf_ct_params *params = NULL; 1294 struct nlattr *tb[TCA_CT_MAX + 1]; 1295 struct tcf_chain *goto_ch = NULL; 1296 struct tc_ct *parm; 1297 struct tcf_ct *c; 1298 int err, res = 0; 1299 u32 index; 1300 1301 if (!nla) { 1302 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1303 return -EINVAL; 1304 } 1305 1306 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1307 if (err < 0) 1308 return err; 1309 1310 if (!tb[TCA_CT_PARMS]) { 1311 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1312 return -EINVAL; 1313 } 1314 parm = nla_data(tb[TCA_CT_PARMS]); 1315 index = parm->index; 1316 err = tcf_idr_check_alloc(tn, &index, a, bind); 1317 if (err < 0) 1318 return err; 1319 1320 if (!err) { 1321 err = tcf_idr_create_from_flags(tn, index, est, a, 1322 &act_ct_ops, bind, flags); 1323 if (err) { 1324 tcf_idr_cleanup(tn, index); 1325 return err; 1326 } 1327 res = ACT_P_CREATED; 1328 } else { 1329 if (bind) 1330 return 0; 1331 1332 if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 1333 tcf_idr_release(*a, bind); 1334 return -EEXIST; 1335 } 1336 } 1337 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1338 if (err < 0) 1339 goto cleanup; 1340 1341 c = to_ct(*a); 1342 1343 params = kzalloc(sizeof(*params), GFP_KERNEL); 1344 if (unlikely(!params)) { 1345 err = -ENOMEM; 1346 goto cleanup; 1347 } 1348 1349 err = tcf_ct_fill_params(net, params, parm, tb, extack); 1350 if (err) 1351 goto cleanup; 1352 1353 err = tcf_ct_flow_table_get(net, params); 1354 if (err) 1355 goto cleanup; 1356 1357 spin_lock_bh(&c->tcf_lock); 1358 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1359 params = rcu_replace_pointer(c->params, params, 1360 lockdep_is_held(&c->tcf_lock)); 1361 spin_unlock_bh(&c->tcf_lock); 1362 1363 if (goto_ch) 1364 tcf_chain_put_by_act(goto_ch); 1365 if (params) 1366 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1367 1368 return res; 1369 1370 cleanup: 1371 if (goto_ch) 1372 tcf_chain_put_by_act(goto_ch); 1373 if (params) 1374 tcf_ct_params_free(params); 1375 tcf_idr_release(*a, bind); 1376 return err; 1377 } 1378 1379 static void tcf_ct_cleanup(struct tc_action *a) 1380 { 1381 struct tcf_ct_params *params; 1382 struct tcf_ct *c = to_ct(a); 1383 1384 params = rcu_dereference_protected(c->params, 1); 1385 if (params) 1386 call_rcu(¶ms->rcu, tcf_ct_params_free_rcu); 1387 } 1388 1389 static int tcf_ct_dump_key_val(struct sk_buff *skb, 1390 void *val, int val_type, 1391 void *mask, int mask_type, 1392 int len) 1393 { 1394 int err; 1395 1396 if (mask && !memchr_inv(mask, 0, len)) 1397 return 0; 1398 1399 err = nla_put(skb, val_type, len, val); 1400 if (err) 1401 return err; 1402 1403 if (mask_type != TCA_CT_UNSPEC) { 1404 err = nla_put(skb, mask_type, len, mask); 1405 if (err) 1406 return err; 1407 } 1408 1409 return 0; 1410 } 1411 1412 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1413 { 1414 struct nf_nat_range2 *range = &p->range; 1415 1416 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1417 return 0; 1418 1419 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1420 return 0; 1421 1422 if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1423 if (p->ipv4_range) { 1424 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1425 range->min_addr.ip)) 1426 return -1; 1427 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1428 range->max_addr.ip)) 1429 return -1; 1430 } else { 1431 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1432 &range->min_addr.in6)) 1433 return -1; 1434 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1435 &range->max_addr.in6)) 1436 return -1; 1437 } 1438 } 1439 1440 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1441 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1442 range->min_proto.all)) 1443 return -1; 1444 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1445 range->max_proto.all)) 1446 return -1; 1447 } 1448 1449 return 0; 1450 } 1451 1452 static int tcf_ct_dump_helper(struct sk_buff *skb, struct nf_conntrack_helper *helper) 1453 { 1454 if (!helper) 1455 return 0; 1456 1457 if (nla_put_string(skb, TCA_CT_HELPER_NAME, helper->name) || 1458 nla_put_u8(skb, TCA_CT_HELPER_FAMILY, helper->tuple.src.l3num) || 1459 nla_put_u8(skb, TCA_CT_HELPER_PROTO, helper->tuple.dst.protonum)) 1460 return -1; 1461 1462 return 0; 1463 } 1464 1465 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1466 int bind, int ref) 1467 { 1468 unsigned char *b = skb_tail_pointer(skb); 1469 struct tcf_ct *c = to_ct(a); 1470 struct tcf_ct_params *p; 1471 1472 struct tc_ct opt = { 1473 .index = c->tcf_index, 1474 .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1475 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1476 }; 1477 struct tcf_t t; 1478 1479 spin_lock_bh(&c->tcf_lock); 1480 p = rcu_dereference_protected(c->params, 1481 lockdep_is_held(&c->tcf_lock)); 1482 opt.action = c->tcf_action; 1483 1484 if (tcf_ct_dump_key_val(skb, 1485 &p->ct_action, TCA_CT_ACTION, 1486 NULL, TCA_CT_UNSPEC, 1487 sizeof(p->ct_action))) 1488 goto nla_put_failure; 1489 1490 if (p->ct_action & TCA_CT_ACT_CLEAR) 1491 goto skip_dump; 1492 1493 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1494 tcf_ct_dump_key_val(skb, 1495 &p->mark, TCA_CT_MARK, 1496 &p->mark_mask, TCA_CT_MARK_MASK, 1497 sizeof(p->mark))) 1498 goto nla_put_failure; 1499 1500 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1501 tcf_ct_dump_key_val(skb, 1502 p->labels, TCA_CT_LABELS, 1503 p->labels_mask, TCA_CT_LABELS_MASK, 1504 sizeof(p->labels))) 1505 goto nla_put_failure; 1506 1507 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1508 tcf_ct_dump_key_val(skb, 1509 &p->zone, TCA_CT_ZONE, 1510 NULL, TCA_CT_UNSPEC, 1511 sizeof(p->zone))) 1512 goto nla_put_failure; 1513 1514 if (tcf_ct_dump_nat(skb, p)) 1515 goto nla_put_failure; 1516 1517 if (tcf_ct_dump_helper(skb, p->helper)) 1518 goto nla_put_failure; 1519 1520 skip_dump: 1521 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1522 goto nla_put_failure; 1523 1524 tcf_tm_dump(&t, &c->tcf_tm); 1525 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1526 goto nla_put_failure; 1527 spin_unlock_bh(&c->tcf_lock); 1528 1529 return skb->len; 1530 nla_put_failure: 1531 spin_unlock_bh(&c->tcf_lock); 1532 nlmsg_trim(skb, b); 1533 return -1; 1534 } 1535 1536 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, 1537 u64 drops, u64 lastuse, bool hw) 1538 { 1539 struct tcf_ct *c = to_ct(a); 1540 1541 tcf_action_update_stats(a, bytes, packets, drops, hw); 1542 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1543 } 1544 1545 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data, 1546 u32 *index_inc, bool bind, 1547 struct netlink_ext_ack *extack) 1548 { 1549 if (bind) { 1550 struct flow_action_entry *entry = entry_data; 1551 1552 if (tcf_ct_helper(act)) 1553 return -EOPNOTSUPP; 1554 1555 entry->id = FLOW_ACTION_CT; 1556 entry->ct.action = tcf_ct_action(act); 1557 entry->ct.zone = tcf_ct_zone(act); 1558 entry->ct.flow_table = tcf_ct_ft(act); 1559 *index_inc = 1; 1560 } else { 1561 struct flow_offload_action *fl_action = entry_data; 1562 1563 fl_action->id = FLOW_ACTION_CT; 1564 } 1565 1566 return 0; 1567 } 1568 1569 static struct tc_action_ops act_ct_ops = { 1570 .kind = "ct", 1571 .id = TCA_ID_CT, 1572 .owner = THIS_MODULE, 1573 .act = tcf_ct_act, 1574 .dump = tcf_ct_dump, 1575 .init = tcf_ct_init, 1576 .cleanup = tcf_ct_cleanup, 1577 .stats_update = tcf_stats_update, 1578 .offload_act_setup = tcf_ct_offload_act_setup, 1579 .size = sizeof(struct tcf_ct), 1580 }; 1581 1582 static __net_init int ct_init_net(struct net *net) 1583 { 1584 struct tc_ct_action_net *tn = net_generic(net, act_ct_ops.net_id); 1585 1586 return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1587 } 1588 1589 static void __net_exit ct_exit_net(struct list_head *net_list) 1590 { 1591 tc_action_net_exit(net_list, act_ct_ops.net_id); 1592 } 1593 1594 static struct pernet_operations ct_net_ops = { 1595 .init = ct_init_net, 1596 .exit_batch = ct_exit_net, 1597 .id = &act_ct_ops.net_id, 1598 .size = sizeof(struct tc_ct_action_net), 1599 }; 1600 1601 static int __init ct_init_module(void) 1602 { 1603 int err; 1604 1605 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1606 if (!act_ct_wq) 1607 return -ENOMEM; 1608 1609 err = tcf_ct_flow_tables_init(); 1610 if (err) 1611 goto err_tbl_init; 1612 1613 err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1614 if (err) 1615 goto err_register; 1616 1617 static_branch_inc(&tcf_frag_xmit_count); 1618 1619 return 0; 1620 1621 err_register: 1622 tcf_ct_flow_tables_uninit(); 1623 err_tbl_init: 1624 destroy_workqueue(act_ct_wq); 1625 return err; 1626 } 1627 1628 static void __exit ct_cleanup_module(void) 1629 { 1630 static_branch_dec(&tcf_frag_xmit_count); 1631 tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1632 tcf_ct_flow_tables_uninit(); 1633 destroy_workqueue(act_ct_wq); 1634 } 1635 1636 module_init(ct_init_module); 1637 module_exit(ct_cleanup_module); 1638 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1639 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1640 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1641 MODULE_DESCRIPTION("Connection tracking action"); 1642 MODULE_LICENSE("GPL v2"); 1643