1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* - 3 * net/sched/act_ct.c Connection Tracking action 4 * 5 * Authors: Paul Blakey <paulb@mellanox.com> 6 * Yossi Kuperman <yossiku@mellanox.com> 7 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/skbuff.h> 14 #include <linux/rtnetlink.h> 15 #include <linux/pkt_cls.h> 16 #include <linux/ip.h> 17 #include <linux/ipv6.h> 18 #include <linux/rhashtable.h> 19 #include <net/netlink.h> 20 #include <net/pkt_sched.h> 21 #include <net/pkt_cls.h> 22 #include <net/act_api.h> 23 #include <net/ip.h> 24 #include <net/ipv6_frag.h> 25 #include <uapi/linux/tc_act/tc_ct.h> 26 #include <net/tc_act/tc_ct.h> 27 28 #include <net/netfilter/nf_flow_table.h> 29 #include <net/netfilter/nf_conntrack.h> 30 #include <net/netfilter/nf_conntrack_core.h> 31 #include <net/netfilter/nf_conntrack_zones.h> 32 #include <net/netfilter/nf_conntrack_helper.h> 33 #include <net/netfilter/nf_conntrack_acct.h> 34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 35 #include <net/netfilter/nf_conntrack_act_ct.h> 36 #include <uapi/linux/netfilter/nf_nat.h> 37 38 static struct workqueue_struct *act_ct_wq; 39 static struct rhashtable zones_ht; 40 static DEFINE_MUTEX(zones_mutex); 41 42 struct tcf_ct_flow_table { 43 struct rhash_head node; /* In zones tables */ 44 45 struct rcu_work rwork; 46 struct nf_flowtable nf_ft; 47 refcount_t ref; 48 u16 zone; 49 50 bool dying; 51 }; 52 53 static const struct rhashtable_params zones_params = { 54 .head_offset = offsetof(struct tcf_ct_flow_table, node), 55 .key_offset = offsetof(struct tcf_ct_flow_table, zone), 56 .key_len = sizeof_field(struct tcf_ct_flow_table, zone), 57 .automatic_shrinking = true, 58 }; 59 60 static struct nf_ct_ext_type act_ct_extend __read_mostly = { 61 .len = sizeof(struct nf_conn_act_ct_ext), 62 .align = __alignof__(struct nf_conn_act_ct_ext), 63 .id = NF_CT_EXT_ACT_CT, 64 }; 65 66 static struct flow_action_entry * 67 tcf_ct_flow_table_flow_action_get_next(struct flow_action *flow_action) 68 { 69 int i = flow_action->num_entries++; 70 71 return &flow_action->entries[i]; 72 } 73 74 static void tcf_ct_add_mangle_action(struct flow_action *action, 75 enum flow_action_mangle_base htype, 76 u32 offset, 77 u32 mask, 78 u32 val) 79 { 80 struct flow_action_entry *entry; 81 82 entry = tcf_ct_flow_table_flow_action_get_next(action); 83 entry->id = FLOW_ACTION_MANGLE; 84 entry->mangle.htype = htype; 85 entry->mangle.mask = ~mask; 86 entry->mangle.offset = offset; 87 entry->mangle.val = val; 88 } 89 90 /* The following nat helper functions check if the inverted reverse tuple 91 * (target) is different then the current dir tuple - meaning nat for ports 92 * and/or ip is needed, and add the relevant mangle actions. 93 */ 94 static void 95 tcf_ct_flow_table_add_action_nat_ipv4(const struct nf_conntrack_tuple *tuple, 96 struct nf_conntrack_tuple target, 97 struct flow_action *action) 98 { 99 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 100 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 101 offsetof(struct iphdr, saddr), 102 0xFFFFFFFF, 103 be32_to_cpu(target.src.u3.ip)); 104 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 105 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP4, 106 offsetof(struct iphdr, daddr), 107 0xFFFFFFFF, 108 be32_to_cpu(target.dst.u3.ip)); 109 } 110 111 static void 112 tcf_ct_add_ipv6_addr_mangle_action(struct flow_action *action, 113 union nf_inet_addr *addr, 114 u32 offset) 115 { 116 int i; 117 118 for (i = 0; i < sizeof(struct in6_addr) / sizeof(u32); i++) 119 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_IP6, 120 i * sizeof(u32) + offset, 121 0xFFFFFFFF, be32_to_cpu(addr->ip6[i])); 122 } 123 124 static void 125 tcf_ct_flow_table_add_action_nat_ipv6(const struct nf_conntrack_tuple *tuple, 126 struct nf_conntrack_tuple target, 127 struct flow_action *action) 128 { 129 if (memcmp(&target.src.u3, &tuple->src.u3, sizeof(target.src.u3))) 130 tcf_ct_add_ipv6_addr_mangle_action(action, &target.src.u3, 131 offsetof(struct ipv6hdr, 132 saddr)); 133 if (memcmp(&target.dst.u3, &tuple->dst.u3, sizeof(target.dst.u3))) 134 tcf_ct_add_ipv6_addr_mangle_action(action, &target.dst.u3, 135 offsetof(struct ipv6hdr, 136 daddr)); 137 } 138 139 static void 140 tcf_ct_flow_table_add_action_nat_tcp(const struct nf_conntrack_tuple *tuple, 141 struct nf_conntrack_tuple target, 142 struct flow_action *action) 143 { 144 __be16 target_src = target.src.u.tcp.port; 145 __be16 target_dst = target.dst.u.tcp.port; 146 147 if (target_src != tuple->src.u.tcp.port) 148 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 149 offsetof(struct tcphdr, source), 150 0xFFFF, be16_to_cpu(target_src)); 151 if (target_dst != tuple->dst.u.tcp.port) 152 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_TCP, 153 offsetof(struct tcphdr, dest), 154 0xFFFF, be16_to_cpu(target_dst)); 155 } 156 157 static void 158 tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple, 159 struct nf_conntrack_tuple target, 160 struct flow_action *action) 161 { 162 __be16 target_src = target.src.u.udp.port; 163 __be16 target_dst = target.dst.u.udp.port; 164 165 if (target_src != tuple->src.u.udp.port) 166 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 167 offsetof(struct udphdr, source), 168 0xFFFF, be16_to_cpu(target_src)); 169 if (target_dst != tuple->dst.u.udp.port) 170 tcf_ct_add_mangle_action(action, FLOW_ACT_MANGLE_HDR_TYPE_UDP, 171 offsetof(struct udphdr, dest), 172 0xFFFF, be16_to_cpu(target_dst)); 173 } 174 175 static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct, 176 enum ip_conntrack_dir dir, 177 struct flow_action *action) 178 { 179 struct nf_conn_labels *ct_labels; 180 struct flow_action_entry *entry; 181 enum ip_conntrack_info ctinfo; 182 u32 *act_ct_labels; 183 184 entry = tcf_ct_flow_table_flow_action_get_next(action); 185 entry->id = FLOW_ACTION_CT_METADATA; 186 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 187 entry->ct_metadata.mark = ct->mark; 188 #endif 189 ctinfo = dir == IP_CT_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 190 IP_CT_ESTABLISHED_REPLY; 191 /* aligns with the CT reference on the SKB nf_ct_set */ 192 entry->ct_metadata.cookie = (unsigned long)ct | ctinfo; 193 entry->ct_metadata.orig_dir = dir == IP_CT_DIR_ORIGINAL; 194 195 act_ct_labels = entry->ct_metadata.labels; 196 ct_labels = nf_ct_labels_find(ct); 197 if (ct_labels) 198 memcpy(act_ct_labels, ct_labels->bits, NF_CT_LABELS_MAX_SIZE); 199 else 200 memset(act_ct_labels, 0, NF_CT_LABELS_MAX_SIZE); 201 } 202 203 static int tcf_ct_flow_table_add_action_nat(struct net *net, 204 struct nf_conn *ct, 205 enum ip_conntrack_dir dir, 206 struct flow_action *action) 207 { 208 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple; 209 struct nf_conntrack_tuple target; 210 211 if (!(ct->status & IPS_NAT_MASK)) 212 return 0; 213 214 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); 215 216 switch (tuple->src.l3num) { 217 case NFPROTO_IPV4: 218 tcf_ct_flow_table_add_action_nat_ipv4(tuple, target, 219 action); 220 break; 221 case NFPROTO_IPV6: 222 tcf_ct_flow_table_add_action_nat_ipv6(tuple, target, 223 action); 224 break; 225 default: 226 return -EOPNOTSUPP; 227 } 228 229 switch (nf_ct_protonum(ct)) { 230 case IPPROTO_TCP: 231 tcf_ct_flow_table_add_action_nat_tcp(tuple, target, action); 232 break; 233 case IPPROTO_UDP: 234 tcf_ct_flow_table_add_action_nat_udp(tuple, target, action); 235 break; 236 default: 237 return -EOPNOTSUPP; 238 } 239 240 return 0; 241 } 242 243 static int tcf_ct_flow_table_fill_actions(struct net *net, 244 const struct flow_offload *flow, 245 enum flow_offload_tuple_dir tdir, 246 struct nf_flow_rule *flow_rule) 247 { 248 struct flow_action *action = &flow_rule->rule->action; 249 int num_entries = action->num_entries; 250 struct nf_conn *ct = flow->ct; 251 enum ip_conntrack_dir dir; 252 int i, err; 253 254 switch (tdir) { 255 case FLOW_OFFLOAD_DIR_ORIGINAL: 256 dir = IP_CT_DIR_ORIGINAL; 257 break; 258 case FLOW_OFFLOAD_DIR_REPLY: 259 dir = IP_CT_DIR_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, 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 struct nf_flowtable_type flowtable_ct = { 282 .action = tcf_ct_flow_table_fill_actions, 283 .owner = THIS_MODULE, 284 }; 285 286 static int tcf_ct_flow_table_get(struct tcf_ct_params *params) 287 { 288 struct tcf_ct_flow_table *ct_ft; 289 int err = -ENOMEM; 290 291 mutex_lock(&zones_mutex); 292 ct_ft = rhashtable_lookup_fast(&zones_ht, ¶ms->zone, zones_params); 293 if (ct_ft && refcount_inc_not_zero(&ct_ft->ref)) 294 goto out_unlock; 295 296 ct_ft = kzalloc(sizeof(*ct_ft), GFP_KERNEL); 297 if (!ct_ft) 298 goto err_alloc; 299 refcount_set(&ct_ft->ref, 1); 300 301 ct_ft->zone = params->zone; 302 err = rhashtable_insert_fast(&zones_ht, &ct_ft->node, zones_params); 303 if (err) 304 goto err_insert; 305 306 ct_ft->nf_ft.type = &flowtable_ct; 307 ct_ft->nf_ft.flags |= NF_FLOWTABLE_HW_OFFLOAD | 308 NF_FLOWTABLE_COUNTER; 309 err = nf_flow_table_init(&ct_ft->nf_ft); 310 if (err) 311 goto err_init; 312 313 __module_get(THIS_MODULE); 314 out_unlock: 315 params->ct_ft = ct_ft; 316 params->nf_ft = &ct_ft->nf_ft; 317 mutex_unlock(&zones_mutex); 318 319 return 0; 320 321 err_init: 322 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 323 err_insert: 324 kfree(ct_ft); 325 err_alloc: 326 mutex_unlock(&zones_mutex); 327 return err; 328 } 329 330 static void tcf_ct_flow_table_cleanup_work(struct work_struct *work) 331 { 332 struct flow_block_cb *block_cb, *tmp_cb; 333 struct tcf_ct_flow_table *ct_ft; 334 struct flow_block *block; 335 336 ct_ft = container_of(to_rcu_work(work), struct tcf_ct_flow_table, 337 rwork); 338 nf_flow_table_free(&ct_ft->nf_ft); 339 340 /* Remove any remaining callbacks before cleanup */ 341 block = &ct_ft->nf_ft.flow_block; 342 down_write(&ct_ft->nf_ft.flow_block_lock); 343 list_for_each_entry_safe(block_cb, tmp_cb, &block->cb_list, list) { 344 list_del(&block_cb->list); 345 flow_block_cb_free(block_cb); 346 } 347 up_write(&ct_ft->nf_ft.flow_block_lock); 348 kfree(ct_ft); 349 350 module_put(THIS_MODULE); 351 } 352 353 static void tcf_ct_flow_table_put(struct tcf_ct_params *params) 354 { 355 struct tcf_ct_flow_table *ct_ft = params->ct_ft; 356 357 if (refcount_dec_and_test(¶ms->ct_ft->ref)) { 358 rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params); 359 INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work); 360 queue_rcu_work(act_ct_wq, &ct_ft->rwork); 361 } 362 } 363 364 static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry, 365 struct nf_conn_act_ct_ext *act_ct_ext, u8 dir) 366 { 367 entry->tuplehash[dir].tuple.xmit_type = FLOW_OFFLOAD_XMIT_TC; 368 entry->tuplehash[dir].tuple.tc.iifidx = act_ct_ext->ifindex[dir]; 369 } 370 371 static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, 372 struct nf_conn *ct, 373 bool tcp) 374 { 375 struct nf_conn_act_ct_ext *act_ct_ext; 376 struct flow_offload *entry; 377 int err; 378 379 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status)) 380 return; 381 382 entry = flow_offload_alloc(ct); 383 if (!entry) { 384 WARN_ON_ONCE(1); 385 goto err_alloc; 386 } 387 388 if (tcp) { 389 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 390 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 391 } 392 393 act_ct_ext = nf_conn_act_ct_ext_find(ct); 394 if (act_ct_ext) { 395 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_ORIGINAL); 396 tcf_ct_flow_tc_ifidx(entry, act_ct_ext, FLOW_OFFLOAD_DIR_REPLY); 397 } 398 399 err = flow_offload_add(&ct_ft->nf_ft, entry); 400 if (err) 401 goto err_add; 402 403 return; 404 405 err_add: 406 flow_offload_free(entry); 407 err_alloc: 408 clear_bit(IPS_OFFLOAD_BIT, &ct->status); 409 } 410 411 static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, 412 struct nf_conn *ct, 413 enum ip_conntrack_info ctinfo) 414 { 415 bool tcp = false; 416 417 if ((ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY) || 418 !test_bit(IPS_ASSURED_BIT, &ct->status)) 419 return; 420 421 switch (nf_ct_protonum(ct)) { 422 case IPPROTO_TCP: 423 tcp = true; 424 if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) 425 return; 426 break; 427 case IPPROTO_UDP: 428 break; 429 default: 430 return; 431 } 432 433 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) || 434 ct->status & IPS_SEQ_ADJUST) 435 return; 436 437 tcf_ct_flow_table_add(ct_ft, ct, tcp); 438 } 439 440 static bool 441 tcf_ct_flow_table_fill_tuple_ipv4(struct sk_buff *skb, 442 struct flow_offload_tuple *tuple, 443 struct tcphdr **tcph) 444 { 445 struct flow_ports *ports; 446 unsigned int thoff; 447 struct iphdr *iph; 448 449 if (!pskb_network_may_pull(skb, sizeof(*iph))) 450 return false; 451 452 iph = ip_hdr(skb); 453 thoff = iph->ihl * 4; 454 455 if (ip_is_fragment(iph) || 456 unlikely(thoff != sizeof(struct iphdr))) 457 return false; 458 459 if (iph->protocol != IPPROTO_TCP && 460 iph->protocol != IPPROTO_UDP) 461 return false; 462 463 if (iph->ttl <= 1) 464 return false; 465 466 if (!pskb_network_may_pull(skb, iph->protocol == IPPROTO_TCP ? 467 thoff + sizeof(struct tcphdr) : 468 thoff + sizeof(*ports))) 469 return false; 470 471 iph = ip_hdr(skb); 472 if (iph->protocol == IPPROTO_TCP) 473 *tcph = (void *)(skb_network_header(skb) + thoff); 474 475 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 476 tuple->src_v4.s_addr = iph->saddr; 477 tuple->dst_v4.s_addr = iph->daddr; 478 tuple->src_port = ports->source; 479 tuple->dst_port = ports->dest; 480 tuple->l3proto = AF_INET; 481 tuple->l4proto = iph->protocol; 482 483 return true; 484 } 485 486 static bool 487 tcf_ct_flow_table_fill_tuple_ipv6(struct sk_buff *skb, 488 struct flow_offload_tuple *tuple, 489 struct tcphdr **tcph) 490 { 491 struct flow_ports *ports; 492 struct ipv6hdr *ip6h; 493 unsigned int thoff; 494 495 if (!pskb_network_may_pull(skb, sizeof(*ip6h))) 496 return false; 497 498 ip6h = ipv6_hdr(skb); 499 500 if (ip6h->nexthdr != IPPROTO_TCP && 501 ip6h->nexthdr != IPPROTO_UDP) 502 return false; 503 504 if (ip6h->hop_limit <= 1) 505 return false; 506 507 thoff = sizeof(*ip6h); 508 if (!pskb_network_may_pull(skb, ip6h->nexthdr == IPPROTO_TCP ? 509 thoff + sizeof(struct tcphdr) : 510 thoff + sizeof(*ports))) 511 return false; 512 513 ip6h = ipv6_hdr(skb); 514 if (ip6h->nexthdr == IPPROTO_TCP) 515 *tcph = (void *)(skb_network_header(skb) + thoff); 516 517 ports = (struct flow_ports *)(skb_network_header(skb) + thoff); 518 tuple->src_v6 = ip6h->saddr; 519 tuple->dst_v6 = ip6h->daddr; 520 tuple->src_port = ports->source; 521 tuple->dst_port = ports->dest; 522 tuple->l3proto = AF_INET6; 523 tuple->l4proto = ip6h->nexthdr; 524 525 return true; 526 } 527 528 static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, 529 struct sk_buff *skb, 530 u8 family) 531 { 532 struct nf_flowtable *nf_ft = &p->ct_ft->nf_ft; 533 struct flow_offload_tuple_rhash *tuplehash; 534 struct flow_offload_tuple tuple = {}; 535 enum ip_conntrack_info ctinfo; 536 struct tcphdr *tcph = NULL; 537 struct flow_offload *flow; 538 struct nf_conn *ct; 539 u8 dir; 540 541 switch (family) { 542 case NFPROTO_IPV4: 543 if (!tcf_ct_flow_table_fill_tuple_ipv4(skb, &tuple, &tcph)) 544 return false; 545 break; 546 case NFPROTO_IPV6: 547 if (!tcf_ct_flow_table_fill_tuple_ipv6(skb, &tuple, &tcph)) 548 return false; 549 break; 550 default: 551 return false; 552 } 553 554 tuplehash = flow_offload_lookup(nf_ft, &tuple); 555 if (!tuplehash) 556 return false; 557 558 dir = tuplehash->tuple.dir; 559 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); 560 ct = flow->ct; 561 562 if (tcph && (unlikely(tcph->fin || tcph->rst))) { 563 flow_offload_teardown(flow); 564 return false; 565 } 566 567 ctinfo = dir == FLOW_OFFLOAD_DIR_ORIGINAL ? IP_CT_ESTABLISHED : 568 IP_CT_ESTABLISHED_REPLY; 569 570 flow_offload_refresh(nf_ft, flow); 571 nf_conntrack_get(&ct->ct_general); 572 nf_ct_set(skb, ct, ctinfo); 573 if (nf_ft->flags & NF_FLOWTABLE_COUNTER) 574 nf_ct_acct_update(ct, dir, skb->len); 575 576 return true; 577 } 578 579 static int tcf_ct_flow_tables_init(void) 580 { 581 return rhashtable_init(&zones_ht, &zones_params); 582 } 583 584 static void tcf_ct_flow_tables_uninit(void) 585 { 586 rhashtable_destroy(&zones_ht); 587 } 588 589 static struct tc_action_ops act_ct_ops; 590 static unsigned int ct_net_id; 591 592 struct tc_ct_action_net { 593 struct tc_action_net tn; /* Must be first */ 594 bool labels; 595 }; 596 597 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 598 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb, 599 u16 zone_id, bool force) 600 { 601 enum ip_conntrack_info ctinfo; 602 struct nf_conn *ct; 603 604 ct = nf_ct_get(skb, &ctinfo); 605 if (!ct) 606 return false; 607 if (!net_eq(net, read_pnet(&ct->ct_net))) 608 return false; 609 if (nf_ct_zone(ct)->id != zone_id) 610 return false; 611 612 /* Force conntrack entry direction. */ 613 if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 614 if (nf_ct_is_confirmed(ct)) 615 nf_ct_kill(ct); 616 617 nf_ct_put(ct); 618 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 619 620 return false; 621 } 622 623 return true; 624 } 625 626 /* Trim the skb to the length specified by the IP/IPv6 header, 627 * removing any trailing lower-layer padding. This prepares the skb 628 * for higher-layer processing that assumes skb->len excludes padding 629 * (such as nf_ip_checksum). The caller needs to pull the skb to the 630 * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 631 */ 632 static int tcf_ct_skb_network_trim(struct sk_buff *skb, int family) 633 { 634 unsigned int len; 635 int err; 636 637 switch (family) { 638 case NFPROTO_IPV4: 639 len = ntohs(ip_hdr(skb)->tot_len); 640 break; 641 case NFPROTO_IPV6: 642 len = sizeof(struct ipv6hdr) 643 + ntohs(ipv6_hdr(skb)->payload_len); 644 break; 645 default: 646 len = skb->len; 647 } 648 649 err = pskb_trim_rcsum(skb, len); 650 651 return err; 652 } 653 654 static u8 tcf_ct_skb_nf_family(struct sk_buff *skb) 655 { 656 u8 family = NFPROTO_UNSPEC; 657 658 switch (skb_protocol(skb, true)) { 659 case htons(ETH_P_IP): 660 family = NFPROTO_IPV4; 661 break; 662 case htons(ETH_P_IPV6): 663 family = NFPROTO_IPV6; 664 break; 665 default: 666 break; 667 } 668 669 return family; 670 } 671 672 static int tcf_ct_ipv4_is_fragment(struct sk_buff *skb, bool *frag) 673 { 674 unsigned int len; 675 676 len = skb_network_offset(skb) + sizeof(struct iphdr); 677 if (unlikely(skb->len < len)) 678 return -EINVAL; 679 if (unlikely(!pskb_may_pull(skb, len))) 680 return -ENOMEM; 681 682 *frag = ip_is_fragment(ip_hdr(skb)); 683 return 0; 684 } 685 686 static int tcf_ct_ipv6_is_fragment(struct sk_buff *skb, bool *frag) 687 { 688 unsigned int flags = 0, len, payload_ofs = 0; 689 unsigned short frag_off; 690 int nexthdr; 691 692 len = skb_network_offset(skb) + sizeof(struct ipv6hdr); 693 if (unlikely(skb->len < len)) 694 return -EINVAL; 695 if (unlikely(!pskb_may_pull(skb, len))) 696 return -ENOMEM; 697 698 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 699 if (unlikely(nexthdr < 0)) 700 return -EPROTO; 701 702 *frag = flags & IP6_FH_F_FRAG; 703 return 0; 704 } 705 706 static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, 707 u8 family, u16 zone, bool *defrag) 708 { 709 enum ip_conntrack_info ctinfo; 710 struct nf_conn *ct; 711 int err = 0; 712 bool frag; 713 u16 mru; 714 715 /* Previously seen (loopback)? Ignore. */ 716 ct = nf_ct_get(skb, &ctinfo); 717 if ((ct && !nf_ct_is_template(ct)) || ctinfo == IP_CT_UNTRACKED) 718 return 0; 719 720 if (family == NFPROTO_IPV4) 721 err = tcf_ct_ipv4_is_fragment(skb, &frag); 722 else 723 err = tcf_ct_ipv6_is_fragment(skb, &frag); 724 if (err || !frag) 725 return err; 726 727 skb_get(skb); 728 mru = tc_skb_cb(skb)->mru; 729 730 if (family == NFPROTO_IPV4) { 731 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 732 733 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 734 local_bh_disable(); 735 err = ip_defrag(net, skb, user); 736 local_bh_enable(); 737 if (err && err != -EINPROGRESS) 738 return err; 739 740 if (!err) { 741 *defrag = true; 742 mru = IPCB(skb)->frag_max_size; 743 } 744 } else { /* NFPROTO_IPV6 */ 745 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 746 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 747 748 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 749 err = nf_ct_frag6_gather(net, skb, user); 750 if (err && err != -EINPROGRESS) 751 goto out_free; 752 753 if (!err) { 754 *defrag = true; 755 mru = IP6CB(skb)->frag_max_size; 756 } 757 #else 758 err = -EOPNOTSUPP; 759 goto out_free; 760 #endif 761 } 762 763 if (err != -EINPROGRESS) 764 tc_skb_cb(skb)->mru = mru; 765 skb_clear_hash(skb); 766 skb->ignore_df = 1; 767 return err; 768 769 out_free: 770 kfree_skb(skb); 771 return err; 772 } 773 774 static void tcf_ct_params_free(struct rcu_head *head) 775 { 776 struct tcf_ct_params *params = container_of(head, 777 struct tcf_ct_params, rcu); 778 779 tcf_ct_flow_table_put(params); 780 781 if (params->tmpl) 782 nf_ct_put(params->tmpl); 783 kfree(params); 784 } 785 786 #if IS_ENABLED(CONFIG_NF_NAT) 787 /* Modelled after nf_nat_ipv[46]_fn(). 788 * range is only used for new, uninitialized NAT state. 789 * Returns either NF_ACCEPT or NF_DROP. 790 */ 791 static int ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 792 enum ip_conntrack_info ctinfo, 793 const struct nf_nat_range2 *range, 794 enum nf_nat_manip_type maniptype) 795 { 796 __be16 proto = skb_protocol(skb, true); 797 int hooknum, err = NF_ACCEPT; 798 799 /* See HOOK2MANIP(). */ 800 if (maniptype == NF_NAT_MANIP_SRC) 801 hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 802 else 803 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 804 805 switch (ctinfo) { 806 case IP_CT_RELATED: 807 case IP_CT_RELATED_REPLY: 808 if (proto == htons(ETH_P_IP) && 809 ip_hdr(skb)->protocol == IPPROTO_ICMP) { 810 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 811 hooknum)) 812 err = NF_DROP; 813 goto out; 814 } else if (IS_ENABLED(CONFIG_IPV6) && proto == htons(ETH_P_IPV6)) { 815 __be16 frag_off; 816 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 817 int hdrlen = ipv6_skip_exthdr(skb, 818 sizeof(struct ipv6hdr), 819 &nexthdr, &frag_off); 820 821 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 822 if (!nf_nat_icmpv6_reply_translation(skb, ct, 823 ctinfo, 824 hooknum, 825 hdrlen)) 826 err = NF_DROP; 827 goto out; 828 } 829 } 830 /* Non-ICMP, fall thru to initialize if needed. */ 831 fallthrough; 832 case IP_CT_NEW: 833 /* Seen it before? This can happen for loopback, retrans, 834 * or local packets. 835 */ 836 if (!nf_nat_initialized(ct, maniptype)) { 837 /* Initialize according to the NAT action. */ 838 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 839 /* Action is set up to establish a new 840 * mapping. 841 */ 842 ? nf_nat_setup_info(ct, range, maniptype) 843 : nf_nat_alloc_null_binding(ct, hooknum); 844 if (err != NF_ACCEPT) 845 goto out; 846 } 847 break; 848 849 case IP_CT_ESTABLISHED: 850 case IP_CT_ESTABLISHED_REPLY: 851 break; 852 853 default: 854 err = NF_DROP; 855 goto out; 856 } 857 858 err = nf_nat_packet(ct, ctinfo, hooknum, skb); 859 if (err == NF_ACCEPT) { 860 if (maniptype == NF_NAT_MANIP_SRC) 861 tc_skb_cb(skb)->post_ct_snat = 1; 862 if (maniptype == NF_NAT_MANIP_DST) 863 tc_skb_cb(skb)->post_ct_dnat = 1; 864 } 865 out: 866 return err; 867 } 868 #endif /* CONFIG_NF_NAT */ 869 870 static void tcf_ct_act_set_mark(struct nf_conn *ct, u32 mark, u32 mask) 871 { 872 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 873 u32 new_mark; 874 875 if (!mask) 876 return; 877 878 new_mark = mark | (ct->mark & ~(mask)); 879 if (ct->mark != new_mark) { 880 ct->mark = new_mark; 881 if (nf_ct_is_confirmed(ct)) 882 nf_conntrack_event_cache(IPCT_MARK, ct); 883 } 884 #endif 885 } 886 887 static void tcf_ct_act_set_labels(struct nf_conn *ct, 888 u32 *labels, 889 u32 *labels_m) 890 { 891 #if IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) 892 size_t labels_sz = sizeof_field(struct tcf_ct_params, labels); 893 894 if (!memchr_inv(labels_m, 0, labels_sz)) 895 return; 896 897 nf_connlabels_replace(ct, labels, labels_m, 4); 898 #endif 899 } 900 901 static int tcf_ct_act_nat(struct sk_buff *skb, 902 struct nf_conn *ct, 903 enum ip_conntrack_info ctinfo, 904 int ct_action, 905 struct nf_nat_range2 *range, 906 bool commit) 907 { 908 #if IS_ENABLED(CONFIG_NF_NAT) 909 int err; 910 enum nf_nat_manip_type maniptype; 911 912 if (!(ct_action & TCA_CT_ACT_NAT)) 913 return NF_ACCEPT; 914 915 /* Add NAT extension if not confirmed yet. */ 916 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 917 return NF_DROP; /* Can't NAT. */ 918 919 if (ctinfo != IP_CT_NEW && (ct->status & IPS_NAT_MASK) && 920 (ctinfo != IP_CT_RELATED || commit)) { 921 /* NAT an established or related connection like before. */ 922 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 923 /* This is the REPLY direction for a connection 924 * for which NAT was applied in the forward 925 * direction. Do the reverse NAT. 926 */ 927 maniptype = ct->status & IPS_SRC_NAT 928 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 929 else 930 maniptype = ct->status & IPS_SRC_NAT 931 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 932 } else if (ct_action & TCA_CT_ACT_NAT_SRC) { 933 maniptype = NF_NAT_MANIP_SRC; 934 } else if (ct_action & TCA_CT_ACT_NAT_DST) { 935 maniptype = NF_NAT_MANIP_DST; 936 } else { 937 return NF_ACCEPT; 938 } 939 940 err = ct_nat_execute(skb, ct, ctinfo, range, maniptype); 941 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) { 942 if (ct->status & IPS_SRC_NAT) { 943 if (maniptype == NF_NAT_MANIP_SRC) 944 maniptype = NF_NAT_MANIP_DST; 945 else 946 maniptype = NF_NAT_MANIP_SRC; 947 948 err = ct_nat_execute(skb, ct, ctinfo, range, 949 maniptype); 950 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) { 951 err = ct_nat_execute(skb, ct, ctinfo, NULL, 952 NF_NAT_MANIP_SRC); 953 } 954 } 955 return err; 956 #else 957 return NF_ACCEPT; 958 #endif 959 } 960 961 static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, 962 struct tcf_result *res) 963 { 964 struct net *net = dev_net(skb->dev); 965 bool cached, commit, clear, force; 966 enum ip_conntrack_info ctinfo; 967 struct tcf_ct *c = to_ct(a); 968 struct nf_conn *tmpl = NULL; 969 struct nf_hook_state state; 970 int nh_ofs, err, retval; 971 struct tcf_ct_params *p; 972 bool skip_add = false; 973 bool defrag = false; 974 struct nf_conn *ct; 975 u8 family; 976 977 p = rcu_dereference_bh(c->params); 978 979 retval = READ_ONCE(c->tcf_action); 980 commit = p->ct_action & TCA_CT_ACT_COMMIT; 981 clear = p->ct_action & TCA_CT_ACT_CLEAR; 982 force = p->ct_action & TCA_CT_ACT_FORCE; 983 tmpl = p->tmpl; 984 985 tcf_lastuse_update(&c->tcf_tm); 986 tcf_action_update_bstats(&c->common, skb); 987 988 if (clear) { 989 tc_skb_cb(skb)->post_ct = false; 990 ct = nf_ct_get(skb, &ctinfo); 991 if (ct) { 992 nf_ct_put(ct); 993 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 994 } 995 996 goto out_clear; 997 } 998 999 family = tcf_ct_skb_nf_family(skb); 1000 if (family == NFPROTO_UNSPEC) 1001 goto drop; 1002 1003 /* The conntrack module expects to be working at L3. 1004 * We also try to pull the IPv4/6 header to linear area 1005 */ 1006 nh_ofs = skb_network_offset(skb); 1007 skb_pull_rcsum(skb, nh_ofs); 1008 err = tcf_ct_handle_fragments(net, skb, family, p->zone, &defrag); 1009 if (err == -EINPROGRESS) { 1010 retval = TC_ACT_STOLEN; 1011 goto out_clear; 1012 } 1013 if (err) 1014 goto drop; 1015 1016 err = tcf_ct_skb_network_trim(skb, family); 1017 if (err) 1018 goto drop; 1019 1020 /* If we are recirculating packets to match on ct fields and 1021 * committing with a separate ct action, then we don't need to 1022 * actually run the packet through conntrack twice unless it's for a 1023 * different zone. 1024 */ 1025 cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force); 1026 if (!cached) { 1027 if (tcf_ct_flow_table_lookup(p, skb, family)) { 1028 skip_add = true; 1029 goto do_nat; 1030 } 1031 1032 /* Associate skb with specified zone. */ 1033 if (tmpl) { 1034 nf_conntrack_put(skb_nfct(skb)); 1035 nf_conntrack_get(&tmpl->ct_general); 1036 nf_ct_set(skb, tmpl, IP_CT_NEW); 1037 } 1038 1039 state.hook = NF_INET_PRE_ROUTING; 1040 state.net = net; 1041 state.pf = family; 1042 err = nf_conntrack_in(skb, &state); 1043 if (err != NF_ACCEPT) 1044 goto out_push; 1045 } 1046 1047 do_nat: 1048 ct = nf_ct_get(skb, &ctinfo); 1049 if (!ct) 1050 goto out_push; 1051 nf_ct_deliver_cached_events(ct); 1052 nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 1053 1054 err = tcf_ct_act_nat(skb, ct, ctinfo, p->ct_action, &p->range, commit); 1055 if (err != NF_ACCEPT) 1056 goto drop; 1057 1058 if (commit) { 1059 tcf_ct_act_set_mark(ct, p->mark, p->mark_mask); 1060 tcf_ct_act_set_labels(ct, p->labels, p->labels_mask); 1061 1062 if (!nf_ct_is_confirmed(ct)) 1063 nf_conn_act_ct_ext_add(ct); 1064 1065 /* This will take care of sending queued events 1066 * even if the connection is already confirmed. 1067 */ 1068 if (nf_conntrack_confirm(skb) != NF_ACCEPT) 1069 goto drop; 1070 } 1071 1072 if (!skip_add) 1073 tcf_ct_flow_table_process_conn(p->ct_ft, ct, ctinfo); 1074 1075 out_push: 1076 skb_push_rcsum(skb, nh_ofs); 1077 1078 tc_skb_cb(skb)->post_ct = true; 1079 tc_skb_cb(skb)->zone = p->zone; 1080 out_clear: 1081 if (defrag) 1082 qdisc_skb_cb(skb)->pkt_len = skb->len; 1083 return retval; 1084 1085 drop: 1086 tcf_action_inc_drop_qstats(&c->common); 1087 return TC_ACT_SHOT; 1088 } 1089 1090 static const struct nla_policy ct_policy[TCA_CT_MAX + 1] = { 1091 [TCA_CT_ACTION] = { .type = NLA_U16 }, 1092 [TCA_CT_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_ct)), 1093 [TCA_CT_ZONE] = { .type = NLA_U16 }, 1094 [TCA_CT_MARK] = { .type = NLA_U32 }, 1095 [TCA_CT_MARK_MASK] = { .type = NLA_U32 }, 1096 [TCA_CT_LABELS] = { .type = NLA_BINARY, 1097 .len = 128 / BITS_PER_BYTE }, 1098 [TCA_CT_LABELS_MASK] = { .type = NLA_BINARY, 1099 .len = 128 / BITS_PER_BYTE }, 1100 [TCA_CT_NAT_IPV4_MIN] = { .type = NLA_U32 }, 1101 [TCA_CT_NAT_IPV4_MAX] = { .type = NLA_U32 }, 1102 [TCA_CT_NAT_IPV6_MIN] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1103 [TCA_CT_NAT_IPV6_MAX] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 1104 [TCA_CT_NAT_PORT_MIN] = { .type = NLA_U16 }, 1105 [TCA_CT_NAT_PORT_MAX] = { .type = NLA_U16 }, 1106 }; 1107 1108 static int tcf_ct_fill_params_nat(struct tcf_ct_params *p, 1109 struct tc_ct *parm, 1110 struct nlattr **tb, 1111 struct netlink_ext_ack *extack) 1112 { 1113 struct nf_nat_range2 *range; 1114 1115 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1116 return 0; 1117 1118 if (!IS_ENABLED(CONFIG_NF_NAT)) { 1119 NL_SET_ERR_MSG_MOD(extack, "Netfilter nat isn't enabled in kernel"); 1120 return -EOPNOTSUPP; 1121 } 1122 1123 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1124 return 0; 1125 1126 if ((p->ct_action & TCA_CT_ACT_NAT_SRC) && 1127 (p->ct_action & TCA_CT_ACT_NAT_DST)) { 1128 NL_SET_ERR_MSG_MOD(extack, "dnat and snat can't be enabled at the same time"); 1129 return -EOPNOTSUPP; 1130 } 1131 1132 range = &p->range; 1133 if (tb[TCA_CT_NAT_IPV4_MIN]) { 1134 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV4_MAX]; 1135 1136 p->ipv4_range = true; 1137 range->flags |= NF_NAT_RANGE_MAP_IPS; 1138 range->min_addr.ip = 1139 nla_get_in_addr(tb[TCA_CT_NAT_IPV4_MIN]); 1140 1141 range->max_addr.ip = max_attr ? 1142 nla_get_in_addr(max_attr) : 1143 range->min_addr.ip; 1144 } else if (tb[TCA_CT_NAT_IPV6_MIN]) { 1145 struct nlattr *max_attr = tb[TCA_CT_NAT_IPV6_MAX]; 1146 1147 p->ipv4_range = false; 1148 range->flags |= NF_NAT_RANGE_MAP_IPS; 1149 range->min_addr.in6 = 1150 nla_get_in6_addr(tb[TCA_CT_NAT_IPV6_MIN]); 1151 1152 range->max_addr.in6 = max_attr ? 1153 nla_get_in6_addr(max_attr) : 1154 range->min_addr.in6; 1155 } 1156 1157 if (tb[TCA_CT_NAT_PORT_MIN]) { 1158 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1159 range->min_proto.all = nla_get_be16(tb[TCA_CT_NAT_PORT_MIN]); 1160 1161 range->max_proto.all = tb[TCA_CT_NAT_PORT_MAX] ? 1162 nla_get_be16(tb[TCA_CT_NAT_PORT_MAX]) : 1163 range->min_proto.all; 1164 } 1165 1166 return 0; 1167 } 1168 1169 static void tcf_ct_set_key_val(struct nlattr **tb, 1170 void *val, int val_type, 1171 void *mask, int mask_type, 1172 int len) 1173 { 1174 if (!tb[val_type]) 1175 return; 1176 nla_memcpy(val, tb[val_type], len); 1177 1178 if (!mask) 1179 return; 1180 1181 if (mask_type == TCA_CT_UNSPEC || !tb[mask_type]) 1182 memset(mask, 0xff, len); 1183 else 1184 nla_memcpy(mask, tb[mask_type], len); 1185 } 1186 1187 static int tcf_ct_fill_params(struct net *net, 1188 struct tcf_ct_params *p, 1189 struct tc_ct *parm, 1190 struct nlattr **tb, 1191 struct netlink_ext_ack *extack) 1192 { 1193 struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1194 struct nf_conntrack_zone zone; 1195 struct nf_conn *tmpl; 1196 int err; 1197 1198 p->zone = NF_CT_DEFAULT_ZONE_ID; 1199 1200 tcf_ct_set_key_val(tb, 1201 &p->ct_action, TCA_CT_ACTION, 1202 NULL, TCA_CT_UNSPEC, 1203 sizeof(p->ct_action)); 1204 1205 if (p->ct_action & TCA_CT_ACT_CLEAR) 1206 return 0; 1207 1208 err = tcf_ct_fill_params_nat(p, parm, tb, extack); 1209 if (err) 1210 return err; 1211 1212 if (tb[TCA_CT_MARK]) { 1213 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1214 NL_SET_ERR_MSG_MOD(extack, "Conntrack mark isn't enabled."); 1215 return -EOPNOTSUPP; 1216 } 1217 tcf_ct_set_key_val(tb, 1218 &p->mark, TCA_CT_MARK, 1219 &p->mark_mask, TCA_CT_MARK_MASK, 1220 sizeof(p->mark)); 1221 } 1222 1223 if (tb[TCA_CT_LABELS]) { 1224 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1225 NL_SET_ERR_MSG_MOD(extack, "Conntrack labels isn't enabled."); 1226 return -EOPNOTSUPP; 1227 } 1228 1229 if (!tn->labels) { 1230 NL_SET_ERR_MSG_MOD(extack, "Failed to set connlabel length"); 1231 return -EOPNOTSUPP; 1232 } 1233 tcf_ct_set_key_val(tb, 1234 p->labels, TCA_CT_LABELS, 1235 p->labels_mask, TCA_CT_LABELS_MASK, 1236 sizeof(p->labels)); 1237 } 1238 1239 if (tb[TCA_CT_ZONE]) { 1240 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1241 NL_SET_ERR_MSG_MOD(extack, "Conntrack zones isn't enabled."); 1242 return -EOPNOTSUPP; 1243 } 1244 1245 tcf_ct_set_key_val(tb, 1246 &p->zone, TCA_CT_ZONE, 1247 NULL, TCA_CT_UNSPEC, 1248 sizeof(p->zone)); 1249 } 1250 1251 nf_ct_zone_init(&zone, p->zone, NF_CT_DEFAULT_ZONE_DIR, 0); 1252 tmpl = nf_ct_tmpl_alloc(net, &zone, GFP_KERNEL); 1253 if (!tmpl) { 1254 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate conntrack template"); 1255 return -ENOMEM; 1256 } 1257 __set_bit(IPS_CONFIRMED_BIT, &tmpl->status); 1258 p->tmpl = tmpl; 1259 1260 return 0; 1261 } 1262 1263 static int tcf_ct_init(struct net *net, struct nlattr *nla, 1264 struct nlattr *est, struct tc_action **a, 1265 struct tcf_proto *tp, u32 flags, 1266 struct netlink_ext_ack *extack) 1267 { 1268 struct tc_action_net *tn = net_generic(net, ct_net_id); 1269 bool bind = flags & TCA_ACT_FLAGS_BIND; 1270 struct tcf_ct_params *params = NULL; 1271 struct nlattr *tb[TCA_CT_MAX + 1]; 1272 struct tcf_chain *goto_ch = NULL; 1273 struct tc_ct *parm; 1274 struct tcf_ct *c; 1275 int err, res = 0; 1276 u32 index; 1277 1278 if (!nla) { 1279 NL_SET_ERR_MSG_MOD(extack, "Ct requires attributes to be passed"); 1280 return -EINVAL; 1281 } 1282 1283 err = nla_parse_nested(tb, TCA_CT_MAX, nla, ct_policy, extack); 1284 if (err < 0) 1285 return err; 1286 1287 if (!tb[TCA_CT_PARMS]) { 1288 NL_SET_ERR_MSG_MOD(extack, "Missing required ct parameters"); 1289 return -EINVAL; 1290 } 1291 parm = nla_data(tb[TCA_CT_PARMS]); 1292 index = parm->index; 1293 err = tcf_idr_check_alloc(tn, &index, a, bind); 1294 if (err < 0) 1295 return err; 1296 1297 if (!err) { 1298 err = tcf_idr_create_from_flags(tn, index, est, a, 1299 &act_ct_ops, bind, flags); 1300 if (err) { 1301 tcf_idr_cleanup(tn, index); 1302 return err; 1303 } 1304 res = ACT_P_CREATED; 1305 } else { 1306 if (bind) 1307 return 0; 1308 1309 if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 1310 tcf_idr_release(*a, bind); 1311 return -EEXIST; 1312 } 1313 } 1314 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 1315 if (err < 0) 1316 goto cleanup; 1317 1318 c = to_ct(*a); 1319 1320 params = kzalloc(sizeof(*params), GFP_KERNEL); 1321 if (unlikely(!params)) { 1322 err = -ENOMEM; 1323 goto cleanup; 1324 } 1325 1326 err = tcf_ct_fill_params(net, params, parm, tb, extack); 1327 if (err) 1328 goto cleanup; 1329 1330 err = tcf_ct_flow_table_get(params); 1331 if (err) 1332 goto cleanup; 1333 1334 spin_lock_bh(&c->tcf_lock); 1335 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 1336 params = rcu_replace_pointer(c->params, params, 1337 lockdep_is_held(&c->tcf_lock)); 1338 spin_unlock_bh(&c->tcf_lock); 1339 1340 if (goto_ch) 1341 tcf_chain_put_by_act(goto_ch); 1342 if (params) 1343 call_rcu(¶ms->rcu, tcf_ct_params_free); 1344 1345 return res; 1346 1347 cleanup: 1348 if (goto_ch) 1349 tcf_chain_put_by_act(goto_ch); 1350 kfree(params); 1351 tcf_idr_release(*a, bind); 1352 return err; 1353 } 1354 1355 static void tcf_ct_cleanup(struct tc_action *a) 1356 { 1357 struct tcf_ct_params *params; 1358 struct tcf_ct *c = to_ct(a); 1359 1360 params = rcu_dereference_protected(c->params, 1); 1361 if (params) 1362 call_rcu(¶ms->rcu, tcf_ct_params_free); 1363 } 1364 1365 static int tcf_ct_dump_key_val(struct sk_buff *skb, 1366 void *val, int val_type, 1367 void *mask, int mask_type, 1368 int len) 1369 { 1370 int err; 1371 1372 if (mask && !memchr_inv(mask, 0, len)) 1373 return 0; 1374 1375 err = nla_put(skb, val_type, len, val); 1376 if (err) 1377 return err; 1378 1379 if (mask_type != TCA_CT_UNSPEC) { 1380 err = nla_put(skb, mask_type, len, mask); 1381 if (err) 1382 return err; 1383 } 1384 1385 return 0; 1386 } 1387 1388 static int tcf_ct_dump_nat(struct sk_buff *skb, struct tcf_ct_params *p) 1389 { 1390 struct nf_nat_range2 *range = &p->range; 1391 1392 if (!(p->ct_action & TCA_CT_ACT_NAT)) 1393 return 0; 1394 1395 if (!(p->ct_action & (TCA_CT_ACT_NAT_SRC | TCA_CT_ACT_NAT_DST))) 1396 return 0; 1397 1398 if (range->flags & NF_NAT_RANGE_MAP_IPS) { 1399 if (p->ipv4_range) { 1400 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MIN, 1401 range->min_addr.ip)) 1402 return -1; 1403 if (nla_put_in_addr(skb, TCA_CT_NAT_IPV4_MAX, 1404 range->max_addr.ip)) 1405 return -1; 1406 } else { 1407 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MIN, 1408 &range->min_addr.in6)) 1409 return -1; 1410 if (nla_put_in6_addr(skb, TCA_CT_NAT_IPV6_MAX, 1411 &range->max_addr.in6)) 1412 return -1; 1413 } 1414 } 1415 1416 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) { 1417 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MIN, 1418 range->min_proto.all)) 1419 return -1; 1420 if (nla_put_be16(skb, TCA_CT_NAT_PORT_MAX, 1421 range->max_proto.all)) 1422 return -1; 1423 } 1424 1425 return 0; 1426 } 1427 1428 static inline int tcf_ct_dump(struct sk_buff *skb, struct tc_action *a, 1429 int bind, int ref) 1430 { 1431 unsigned char *b = skb_tail_pointer(skb); 1432 struct tcf_ct *c = to_ct(a); 1433 struct tcf_ct_params *p; 1434 1435 struct tc_ct opt = { 1436 .index = c->tcf_index, 1437 .refcnt = refcount_read(&c->tcf_refcnt) - ref, 1438 .bindcnt = atomic_read(&c->tcf_bindcnt) - bind, 1439 }; 1440 struct tcf_t t; 1441 1442 spin_lock_bh(&c->tcf_lock); 1443 p = rcu_dereference_protected(c->params, 1444 lockdep_is_held(&c->tcf_lock)); 1445 opt.action = c->tcf_action; 1446 1447 if (tcf_ct_dump_key_val(skb, 1448 &p->ct_action, TCA_CT_ACTION, 1449 NULL, TCA_CT_UNSPEC, 1450 sizeof(p->ct_action))) 1451 goto nla_put_failure; 1452 1453 if (p->ct_action & TCA_CT_ACT_CLEAR) 1454 goto skip_dump; 1455 1456 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1457 tcf_ct_dump_key_val(skb, 1458 &p->mark, TCA_CT_MARK, 1459 &p->mark_mask, TCA_CT_MARK_MASK, 1460 sizeof(p->mark))) 1461 goto nla_put_failure; 1462 1463 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1464 tcf_ct_dump_key_val(skb, 1465 p->labels, TCA_CT_LABELS, 1466 p->labels_mask, TCA_CT_LABELS_MASK, 1467 sizeof(p->labels))) 1468 goto nla_put_failure; 1469 1470 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1471 tcf_ct_dump_key_val(skb, 1472 &p->zone, TCA_CT_ZONE, 1473 NULL, TCA_CT_UNSPEC, 1474 sizeof(p->zone))) 1475 goto nla_put_failure; 1476 1477 if (tcf_ct_dump_nat(skb, p)) 1478 goto nla_put_failure; 1479 1480 skip_dump: 1481 if (nla_put(skb, TCA_CT_PARMS, sizeof(opt), &opt)) 1482 goto nla_put_failure; 1483 1484 tcf_tm_dump(&t, &c->tcf_tm); 1485 if (nla_put_64bit(skb, TCA_CT_TM, sizeof(t), &t, TCA_CT_PAD)) 1486 goto nla_put_failure; 1487 spin_unlock_bh(&c->tcf_lock); 1488 1489 return skb->len; 1490 nla_put_failure: 1491 spin_unlock_bh(&c->tcf_lock); 1492 nlmsg_trim(skb, b); 1493 return -1; 1494 } 1495 1496 static int tcf_ct_walker(struct net *net, struct sk_buff *skb, 1497 struct netlink_callback *cb, int type, 1498 const struct tc_action_ops *ops, 1499 struct netlink_ext_ack *extack) 1500 { 1501 struct tc_action_net *tn = net_generic(net, ct_net_id); 1502 1503 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 1504 } 1505 1506 static int tcf_ct_search(struct net *net, struct tc_action **a, u32 index) 1507 { 1508 struct tc_action_net *tn = net_generic(net, ct_net_id); 1509 1510 return tcf_idr_search(tn, a, index); 1511 } 1512 1513 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, 1514 u64 drops, u64 lastuse, bool hw) 1515 { 1516 struct tcf_ct *c = to_ct(a); 1517 1518 tcf_action_update_stats(a, bytes, packets, drops, hw); 1519 c->tcf_tm.lastuse = max_t(u64, c->tcf_tm.lastuse, lastuse); 1520 } 1521 1522 static int tcf_ct_offload_act_setup(struct tc_action *act, void *entry_data, 1523 u32 *index_inc, bool bind) 1524 { 1525 if (bind) { 1526 struct flow_action_entry *entry = entry_data; 1527 1528 entry->id = FLOW_ACTION_CT; 1529 entry->ct.action = tcf_ct_action(act); 1530 entry->ct.zone = tcf_ct_zone(act); 1531 entry->ct.flow_table = tcf_ct_ft(act); 1532 *index_inc = 1; 1533 } else { 1534 struct flow_offload_action *fl_action = entry_data; 1535 1536 fl_action->id = FLOW_ACTION_CT; 1537 } 1538 1539 return 0; 1540 } 1541 1542 static struct tc_action_ops act_ct_ops = { 1543 .kind = "ct", 1544 .id = TCA_ID_CT, 1545 .owner = THIS_MODULE, 1546 .act = tcf_ct_act, 1547 .dump = tcf_ct_dump, 1548 .init = tcf_ct_init, 1549 .cleanup = tcf_ct_cleanup, 1550 .walk = tcf_ct_walker, 1551 .lookup = tcf_ct_search, 1552 .stats_update = tcf_stats_update, 1553 .offload_act_setup = tcf_ct_offload_act_setup, 1554 .size = sizeof(struct tcf_ct), 1555 }; 1556 1557 static __net_init int ct_init_net(struct net *net) 1558 { 1559 unsigned int n_bits = sizeof_field(struct tcf_ct_params, labels) * 8; 1560 struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1561 1562 if (nf_connlabels_get(net, n_bits - 1)) { 1563 tn->labels = false; 1564 pr_err("act_ct: Failed to set connlabels length"); 1565 } else { 1566 tn->labels = true; 1567 } 1568 1569 return tc_action_net_init(net, &tn->tn, &act_ct_ops); 1570 } 1571 1572 static void __net_exit ct_exit_net(struct list_head *net_list) 1573 { 1574 struct net *net; 1575 1576 rtnl_lock(); 1577 list_for_each_entry(net, net_list, exit_list) { 1578 struct tc_ct_action_net *tn = net_generic(net, ct_net_id); 1579 1580 if (tn->labels) 1581 nf_connlabels_put(net); 1582 } 1583 rtnl_unlock(); 1584 1585 tc_action_net_exit(net_list, ct_net_id); 1586 } 1587 1588 static struct pernet_operations ct_net_ops = { 1589 .init = ct_init_net, 1590 .exit_batch = ct_exit_net, 1591 .id = &ct_net_id, 1592 .size = sizeof(struct tc_ct_action_net), 1593 }; 1594 1595 static int __init ct_init_module(void) 1596 { 1597 int err; 1598 1599 act_ct_wq = alloc_ordered_workqueue("act_ct_workqueue", 0); 1600 if (!act_ct_wq) 1601 return -ENOMEM; 1602 1603 err = tcf_ct_flow_tables_init(); 1604 if (err) 1605 goto err_tbl_init; 1606 1607 err = tcf_register_action(&act_ct_ops, &ct_net_ops); 1608 if (err) 1609 goto err_register; 1610 1611 err = nf_ct_extend_register(&act_ct_extend); 1612 if (err) 1613 goto err_register_extend; 1614 1615 static_branch_inc(&tcf_frag_xmit_count); 1616 1617 return 0; 1618 1619 err_register_extend: 1620 tcf_unregister_action(&act_ct_ops, &ct_net_ops); 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 nf_ct_extend_unregister(&act_ct_extend); 1632 tcf_unregister_action(&act_ct_ops, &ct_net_ops); 1633 tcf_ct_flow_tables_uninit(); 1634 destroy_workqueue(act_ct_wq); 1635 } 1636 1637 module_init(ct_init_module); 1638 module_exit(ct_cleanup_module); 1639 MODULE_AUTHOR("Paul Blakey <paulb@mellanox.com>"); 1640 MODULE_AUTHOR("Yossi Kuperman <yossiku@mellanox.com>"); 1641 MODULE_AUTHOR("Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>"); 1642 MODULE_DESCRIPTION("Connection tracking action"); 1643 MODULE_LICENSE("GPL v2"); 1644