1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 Nicira, Inc. 4 */ 5 6 #include <linux/module.h> 7 #include <linux/openvswitch.h> 8 #include <linux/tcp.h> 9 #include <linux/udp.h> 10 #include <linux/sctp.h> 11 #include <linux/static_key.h> 12 #include <net/ip.h> 13 #include <net/genetlink.h> 14 #include <net/netfilter/nf_conntrack_core.h> 15 #include <net/netfilter/nf_conntrack_count.h> 16 #include <net/netfilter/nf_conntrack_helper.h> 17 #include <net/netfilter/nf_conntrack_labels.h> 18 #include <net/netfilter/nf_conntrack_seqadj.h> 19 #include <net/netfilter/nf_conntrack_timeout.h> 20 #include <net/netfilter/nf_conntrack_zones.h> 21 #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 22 #include <net/ipv6_frag.h> 23 24 #if IS_ENABLED(CONFIG_NF_NAT) 25 #include <net/netfilter/nf_nat.h> 26 #endif 27 28 #include <net/netfilter/nf_conntrack_act_ct.h> 29 30 #include "datapath.h" 31 #include "conntrack.h" 32 #include "flow.h" 33 #include "flow_netlink.h" 34 35 struct ovs_ct_len_tbl { 36 int maxlen; 37 int minlen; 38 }; 39 40 /* Metadata mark for masked write to conntrack mark */ 41 struct md_mark { 42 u32 value; 43 u32 mask; 44 }; 45 46 /* Metadata label for masked write to conntrack label. */ 47 struct md_labels { 48 struct ovs_key_ct_labels value; 49 struct ovs_key_ct_labels mask; 50 }; 51 52 enum ovs_ct_nat { 53 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */ 54 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */ 55 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */ 56 }; 57 58 /* Conntrack action context for execution. */ 59 struct ovs_conntrack_info { 60 struct nf_conntrack_helper *helper; 61 struct nf_conntrack_zone zone; 62 struct nf_conn *ct; 63 u8 commit : 1; 64 u8 nat : 3; /* enum ovs_ct_nat */ 65 u8 force : 1; 66 u8 have_eventmask : 1; 67 u16 family; 68 u32 eventmask; /* Mask of 1 << IPCT_*. */ 69 struct md_mark mark; 70 struct md_labels labels; 71 char timeout[CTNL_TIMEOUT_NAME_MAX]; 72 struct nf_ct_timeout *nf_ct_timeout; 73 #if IS_ENABLED(CONFIG_NF_NAT) 74 struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */ 75 #endif 76 }; 77 78 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 79 #define OVS_CT_LIMIT_UNLIMITED 0 80 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED 81 #define CT_LIMIT_HASH_BUCKETS 512 82 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled); 83 84 struct ovs_ct_limit { 85 /* Elements in ovs_ct_limit_info->limits hash table */ 86 struct hlist_node hlist_node; 87 struct rcu_head rcu; 88 u16 zone; 89 u32 limit; 90 }; 91 92 struct ovs_ct_limit_info { 93 u32 default_limit; 94 struct hlist_head *limits; 95 struct nf_conncount_data *data; 96 }; 97 98 static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = { 99 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, }, 100 }; 101 #endif 102 103 static bool labels_nonzero(const struct ovs_key_ct_labels *labels); 104 105 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info); 106 107 static u16 key_to_nfproto(const struct sw_flow_key *key) 108 { 109 switch (ntohs(key->eth.type)) { 110 case ETH_P_IP: 111 return NFPROTO_IPV4; 112 case ETH_P_IPV6: 113 return NFPROTO_IPV6; 114 default: 115 return NFPROTO_UNSPEC; 116 } 117 } 118 119 /* Map SKB connection state into the values used by flow definition. */ 120 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) 121 { 122 u8 ct_state = OVS_CS_F_TRACKED; 123 124 switch (ctinfo) { 125 case IP_CT_ESTABLISHED_REPLY: 126 case IP_CT_RELATED_REPLY: 127 ct_state |= OVS_CS_F_REPLY_DIR; 128 break; 129 default: 130 break; 131 } 132 133 switch (ctinfo) { 134 case IP_CT_ESTABLISHED: 135 case IP_CT_ESTABLISHED_REPLY: 136 ct_state |= OVS_CS_F_ESTABLISHED; 137 break; 138 case IP_CT_RELATED: 139 case IP_CT_RELATED_REPLY: 140 ct_state |= OVS_CS_F_RELATED; 141 break; 142 case IP_CT_NEW: 143 ct_state |= OVS_CS_F_NEW; 144 break; 145 default: 146 break; 147 } 148 149 return ct_state; 150 } 151 152 static u32 ovs_ct_get_mark(const struct nf_conn *ct) 153 { 154 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 155 return ct ? ct->mark : 0; 156 #else 157 return 0; 158 #endif 159 } 160 161 /* Guard against conntrack labels max size shrinking below 128 bits. */ 162 #if NF_CT_LABELS_MAX_SIZE < 16 163 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes 164 #endif 165 166 static void ovs_ct_get_labels(const struct nf_conn *ct, 167 struct ovs_key_ct_labels *labels) 168 { 169 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; 170 171 if (cl) 172 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN); 173 else 174 memset(labels, 0, OVS_CT_LABELS_LEN); 175 } 176 177 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key, 178 const struct nf_conntrack_tuple *orig, 179 u8 icmp_proto) 180 { 181 key->ct_orig_proto = orig->dst.protonum; 182 if (orig->dst.protonum == icmp_proto) { 183 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type); 184 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code); 185 } else { 186 key->ct.orig_tp.src = orig->src.u.all; 187 key->ct.orig_tp.dst = orig->dst.u.all; 188 } 189 } 190 191 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, 192 const struct nf_conntrack_zone *zone, 193 const struct nf_conn *ct) 194 { 195 key->ct_state = state; 196 key->ct_zone = zone->id; 197 key->ct.mark = ovs_ct_get_mark(ct); 198 ovs_ct_get_labels(ct, &key->ct.labels); 199 200 if (ct) { 201 const struct nf_conntrack_tuple *orig; 202 203 /* Use the master if we have one. */ 204 if (ct->master) 205 ct = ct->master; 206 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; 207 208 /* IP version must match with the master connection. */ 209 if (key->eth.type == htons(ETH_P_IP) && 210 nf_ct_l3num(ct) == NFPROTO_IPV4) { 211 key->ipv4.ct_orig.src = orig->src.u3.ip; 212 key->ipv4.ct_orig.dst = orig->dst.u3.ip; 213 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP); 214 return; 215 } else if (key->eth.type == htons(ETH_P_IPV6) && 216 !sw_flow_key_is_nd(key) && 217 nf_ct_l3num(ct) == NFPROTO_IPV6) { 218 key->ipv6.ct_orig.src = orig->src.u3.in6; 219 key->ipv6.ct_orig.dst = orig->dst.u3.in6; 220 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP); 221 return; 222 } 223 } 224 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack 225 * original direction key fields. 226 */ 227 key->ct_orig_proto = 0; 228 } 229 230 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has 231 * previously sent the packet to conntrack via the ct action. If 232 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are 233 * initialized from the connection status. 234 */ 235 static void ovs_ct_update_key(const struct sk_buff *skb, 236 const struct ovs_conntrack_info *info, 237 struct sw_flow_key *key, bool post_ct, 238 bool keep_nat_flags) 239 { 240 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; 241 enum ip_conntrack_info ctinfo; 242 struct nf_conn *ct; 243 u8 state = 0; 244 245 ct = nf_ct_get(skb, &ctinfo); 246 if (ct) { 247 state = ovs_ct_get_state(ctinfo); 248 /* All unconfirmed entries are NEW connections. */ 249 if (!nf_ct_is_confirmed(ct)) 250 state |= OVS_CS_F_NEW; 251 /* OVS persists the related flag for the duration of the 252 * connection. 253 */ 254 if (ct->master) 255 state |= OVS_CS_F_RELATED; 256 if (keep_nat_flags) { 257 state |= key->ct_state & OVS_CS_F_NAT_MASK; 258 } else { 259 if (ct->status & IPS_SRC_NAT) 260 state |= OVS_CS_F_SRC_NAT; 261 if (ct->status & IPS_DST_NAT) 262 state |= OVS_CS_F_DST_NAT; 263 } 264 zone = nf_ct_zone(ct); 265 } else if (post_ct) { 266 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; 267 if (info) 268 zone = &info->zone; 269 } 270 __ovs_ct_update_key(key, state, zone, ct); 271 } 272 273 /* This is called to initialize CT key fields possibly coming in from the local 274 * stack. 275 */ 276 void ovs_ct_fill_key(const struct sk_buff *skb, 277 struct sw_flow_key *key, 278 bool post_ct) 279 { 280 ovs_ct_update_key(skb, NULL, key, post_ct, false); 281 } 282 283 int ovs_ct_put_key(const struct sw_flow_key *swkey, 284 const struct sw_flow_key *output, struct sk_buff *skb) 285 { 286 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state)) 287 return -EMSGSIZE; 288 289 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 290 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone)) 291 return -EMSGSIZE; 292 293 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 294 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark)) 295 return -EMSGSIZE; 296 297 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 298 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels), 299 &output->ct.labels)) 300 return -EMSGSIZE; 301 302 if (swkey->ct_orig_proto) { 303 if (swkey->eth.type == htons(ETH_P_IP)) { 304 struct ovs_key_ct_tuple_ipv4 orig; 305 306 memset(&orig, 0, sizeof(orig)); 307 orig.ipv4_src = output->ipv4.ct_orig.src; 308 orig.ipv4_dst = output->ipv4.ct_orig.dst; 309 orig.src_port = output->ct.orig_tp.src; 310 orig.dst_port = output->ct.orig_tp.dst; 311 orig.ipv4_proto = output->ct_orig_proto; 312 313 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, 314 sizeof(orig), &orig)) 315 return -EMSGSIZE; 316 } else if (swkey->eth.type == htons(ETH_P_IPV6)) { 317 struct ovs_key_ct_tuple_ipv6 orig; 318 319 memset(&orig, 0, sizeof(orig)); 320 memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32, 321 sizeof(orig.ipv6_src)); 322 memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32, 323 sizeof(orig.ipv6_dst)); 324 orig.src_port = output->ct.orig_tp.src; 325 orig.dst_port = output->ct.orig_tp.dst; 326 orig.ipv6_proto = output->ct_orig_proto; 327 328 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, 329 sizeof(orig), &orig)) 330 return -EMSGSIZE; 331 } 332 } 333 334 return 0; 335 } 336 337 static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key, 338 u32 ct_mark, u32 mask) 339 { 340 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 341 u32 new_mark; 342 343 new_mark = ct_mark | (ct->mark & ~(mask)); 344 if (ct->mark != new_mark) { 345 ct->mark = new_mark; 346 if (nf_ct_is_confirmed(ct)) 347 nf_conntrack_event_cache(IPCT_MARK, ct); 348 key->ct.mark = new_mark; 349 } 350 351 return 0; 352 #else 353 return -ENOTSUPP; 354 #endif 355 } 356 357 static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct) 358 { 359 struct nf_conn_labels *cl; 360 361 cl = nf_ct_labels_find(ct); 362 if (!cl) { 363 nf_ct_labels_ext_add(ct); 364 cl = nf_ct_labels_find(ct); 365 } 366 367 return cl; 368 } 369 370 /* Initialize labels for a new, yet to be committed conntrack entry. Note that 371 * since the new connection is not yet confirmed, and thus no-one else has 372 * access to it's labels, we simply write them over. 373 */ 374 static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key, 375 const struct ovs_key_ct_labels *labels, 376 const struct ovs_key_ct_labels *mask) 377 { 378 struct nf_conn_labels *cl, *master_cl; 379 bool have_mask = labels_nonzero(mask); 380 381 /* Inherit master's labels to the related connection? */ 382 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL; 383 384 if (!master_cl && !have_mask) 385 return 0; /* Nothing to do. */ 386 387 cl = ovs_ct_get_conn_labels(ct); 388 if (!cl) 389 return -ENOSPC; 390 391 /* Inherit the master's labels, if any. */ 392 if (master_cl) 393 *cl = *master_cl; 394 395 if (have_mask) { 396 u32 *dst = (u32 *)cl->bits; 397 int i; 398 399 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) 400 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) | 401 (labels->ct_labels_32[i] 402 & mask->ct_labels_32[i]); 403 } 404 405 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the 406 * IPCT_LABEL bit is set in the event cache. 407 */ 408 nf_conntrack_event_cache(IPCT_LABEL, ct); 409 410 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN); 411 412 return 0; 413 } 414 415 static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key, 416 const struct ovs_key_ct_labels *labels, 417 const struct ovs_key_ct_labels *mask) 418 { 419 struct nf_conn_labels *cl; 420 int err; 421 422 cl = ovs_ct_get_conn_labels(ct); 423 if (!cl) 424 return -ENOSPC; 425 426 err = nf_connlabels_replace(ct, labels->ct_labels_32, 427 mask->ct_labels_32, 428 OVS_CT_LABELS_LEN_32); 429 if (err) 430 return err; 431 432 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN); 433 434 return 0; 435 } 436 437 /* 'skb' should already be pulled to nh_ofs. */ 438 static int ovs_ct_helper(struct sk_buff *skb, u16 proto) 439 { 440 const struct nf_conntrack_helper *helper; 441 const struct nf_conn_help *help; 442 enum ip_conntrack_info ctinfo; 443 unsigned int protoff; 444 struct nf_conn *ct; 445 int err; 446 447 ct = nf_ct_get(skb, &ctinfo); 448 if (!ct || ctinfo == IP_CT_RELATED_REPLY) 449 return NF_ACCEPT; 450 451 help = nfct_help(ct); 452 if (!help) 453 return NF_ACCEPT; 454 455 helper = rcu_dereference(help->helper); 456 if (!helper) 457 return NF_ACCEPT; 458 459 switch (proto) { 460 case NFPROTO_IPV4: 461 protoff = ip_hdrlen(skb); 462 break; 463 case NFPROTO_IPV6: { 464 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 465 __be16 frag_off; 466 int ofs; 467 468 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, 469 &frag_off); 470 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { 471 pr_debug("proto header not found\n"); 472 return NF_ACCEPT; 473 } 474 protoff = ofs; 475 break; 476 } 477 default: 478 WARN_ONCE(1, "helper invoked on non-IP family!"); 479 return NF_DROP; 480 } 481 482 err = helper->help(skb, protoff, ct, ctinfo); 483 if (err != NF_ACCEPT) 484 return err; 485 486 /* Adjust seqs after helper. This is needed due to some helpers (e.g., 487 * FTP with NAT) adusting the TCP payload size when mangling IP 488 * addresses and/or port numbers in the text-based control connection. 489 */ 490 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && 491 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) 492 return NF_DROP; 493 return NF_ACCEPT; 494 } 495 496 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero 497 * value if 'skb' is freed. 498 */ 499 static int handle_fragments(struct net *net, struct sw_flow_key *key, 500 u16 zone, struct sk_buff *skb) 501 { 502 struct ovs_skb_cb ovs_cb = *OVS_CB(skb); 503 int err; 504 505 if (key->eth.type == htons(ETH_P_IP)) { 506 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 507 508 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 509 err = ip_defrag(net, skb, user); 510 if (err) 511 return err; 512 513 ovs_cb.mru = IPCB(skb)->frag_max_size; 514 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 515 } else if (key->eth.type == htons(ETH_P_IPV6)) { 516 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 517 518 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 519 err = nf_ct_frag6_gather(net, skb, user); 520 if (err) { 521 if (err != -EINPROGRESS) 522 kfree_skb(skb); 523 return err; 524 } 525 526 key->ip.proto = ipv6_hdr(skb)->nexthdr; 527 ovs_cb.mru = IP6CB(skb)->frag_max_size; 528 #endif 529 } else { 530 kfree_skb(skb); 531 return -EPFNOSUPPORT; 532 } 533 534 /* The key extracted from the fragment that completed this datagram 535 * likely didn't have an L4 header, so regenerate it. 536 */ 537 ovs_flow_key_update_l3l4(skb, key); 538 539 key->ip.frag = OVS_FRAG_TYPE_NONE; 540 skb_clear_hash(skb); 541 skb->ignore_df = 1; 542 *OVS_CB(skb) = ovs_cb; 543 544 return 0; 545 } 546 547 static struct nf_conntrack_expect * 548 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, 549 u16 proto, const struct sk_buff *skb) 550 { 551 struct nf_conntrack_tuple tuple; 552 struct nf_conntrack_expect *exp; 553 554 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) 555 return NULL; 556 557 exp = __nf_ct_expect_find(net, zone, &tuple); 558 if (exp) { 559 struct nf_conntrack_tuple_hash *h; 560 561 /* Delete existing conntrack entry, if it clashes with the 562 * expectation. This can happen since conntrack ALGs do not 563 * check for clashes between (new) expectations and existing 564 * conntrack entries. nf_conntrack_in() will check the 565 * expectations only if a conntrack entry can not be found, 566 * which can lead to OVS finding the expectation (here) in the 567 * init direction, but which will not be removed by the 568 * nf_conntrack_in() call, if a matching conntrack entry is 569 * found instead. In this case all init direction packets 570 * would be reported as new related packets, while reply 571 * direction packets would be reported as un-related 572 * established packets. 573 */ 574 h = nf_conntrack_find_get(net, zone, &tuple); 575 if (h) { 576 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); 577 578 nf_ct_delete(ct, 0, 0); 579 nf_conntrack_put(&ct->ct_general); 580 } 581 } 582 583 return exp; 584 } 585 586 /* This replicates logic from nf_conntrack_core.c that is not exported. */ 587 static enum ip_conntrack_info 588 ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h) 589 { 590 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); 591 592 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) 593 return IP_CT_ESTABLISHED_REPLY; 594 /* Once we've had two way comms, always ESTABLISHED. */ 595 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) 596 return IP_CT_ESTABLISHED; 597 if (test_bit(IPS_EXPECTED_BIT, &ct->status)) 598 return IP_CT_RELATED; 599 return IP_CT_NEW; 600 } 601 602 /* Find an existing connection which this packet belongs to without 603 * re-attributing statistics or modifying the connection state. This allows an 604 * skb->_nfct lost due to an upcall to be recovered during actions execution. 605 * 606 * Must be called with rcu_read_lock. 607 * 608 * On success, populates skb->_nfct and returns the connection. Returns NULL 609 * if there is no existing entry. 610 */ 611 static struct nf_conn * 612 ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone, 613 u8 l3num, struct sk_buff *skb, bool natted) 614 { 615 struct nf_conntrack_tuple tuple; 616 struct nf_conntrack_tuple_hash *h; 617 struct nf_conn *ct; 618 619 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num, 620 net, &tuple)) { 621 pr_debug("ovs_ct_find_existing: Can't get tuple\n"); 622 return NULL; 623 } 624 625 /* Must invert the tuple if skb has been transformed by NAT. */ 626 if (natted) { 627 struct nf_conntrack_tuple inverse; 628 629 if (!nf_ct_invert_tuple(&inverse, &tuple)) { 630 pr_debug("ovs_ct_find_existing: Inversion failed!\n"); 631 return NULL; 632 } 633 tuple = inverse; 634 } 635 636 /* look for tuple match */ 637 h = nf_conntrack_find_get(net, zone, &tuple); 638 if (!h) 639 return NULL; /* Not found. */ 640 641 ct = nf_ct_tuplehash_to_ctrack(h); 642 643 /* Inverted packet tuple matches the reverse direction conntrack tuple, 644 * select the other tuplehash to get the right 'ctinfo' bits for this 645 * packet. 646 */ 647 if (natted) 648 h = &ct->tuplehash[!h->tuple.dst.dir]; 649 650 nf_ct_set(skb, ct, ovs_ct_get_info(h)); 651 return ct; 652 } 653 654 static 655 struct nf_conn *ovs_ct_executed(struct net *net, 656 const struct sw_flow_key *key, 657 const struct ovs_conntrack_info *info, 658 struct sk_buff *skb, 659 bool *ct_executed) 660 { 661 struct nf_conn *ct = NULL; 662 663 /* If no ct, check if we have evidence that an existing conntrack entry 664 * might be found for this skb. This happens when we lose a skb->_nfct 665 * due to an upcall, or if the direction is being forced. If the 666 * connection was not confirmed, it is not cached and needs to be run 667 * through conntrack again. 668 */ 669 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) && 670 !(key->ct_state & OVS_CS_F_INVALID) && 671 (key->ct_zone == info->zone.id); 672 673 if (*ct_executed || (!key->ct_state && info->force)) { 674 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb, 675 !!(key->ct_state & 676 OVS_CS_F_NAT_MASK)); 677 } 678 679 return ct; 680 } 681 682 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ 683 static bool skb_nfct_cached(struct net *net, 684 const struct sw_flow_key *key, 685 const struct ovs_conntrack_info *info, 686 struct sk_buff *skb) 687 { 688 enum ip_conntrack_info ctinfo; 689 struct nf_conn *ct; 690 bool ct_executed = true; 691 692 ct = nf_ct_get(skb, &ctinfo); 693 if (!ct) 694 ct = ovs_ct_executed(net, key, info, skb, &ct_executed); 695 696 if (ct) 697 nf_ct_get(skb, &ctinfo); 698 else 699 return false; 700 701 if (!net_eq(net, read_pnet(&ct->ct_net))) 702 return false; 703 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) 704 return false; 705 if (info->helper) { 706 struct nf_conn_help *help; 707 708 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); 709 if (help && rcu_access_pointer(help->helper) != info->helper) 710 return false; 711 } 712 if (info->nf_ct_timeout) { 713 struct nf_conn_timeout *timeout_ext; 714 715 timeout_ext = nf_ct_timeout_find(ct); 716 if (!timeout_ext || info->nf_ct_timeout != 717 rcu_dereference(timeout_ext->timeout)) 718 return false; 719 } 720 /* Force conntrack entry direction to the current packet? */ 721 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { 722 /* Delete the conntrack entry if confirmed, else just release 723 * the reference. 724 */ 725 if (nf_ct_is_confirmed(ct)) 726 nf_ct_delete(ct, 0, 0); 727 728 nf_conntrack_put(&ct->ct_general); 729 nf_ct_set(skb, NULL, 0); 730 return false; 731 } 732 733 return ct_executed; 734 } 735 736 #if IS_ENABLED(CONFIG_NF_NAT) 737 /* Modelled after nf_nat_ipv[46]_fn(). 738 * range is only used for new, uninitialized NAT state. 739 * Returns either NF_ACCEPT or NF_DROP. 740 */ 741 static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, 742 enum ip_conntrack_info ctinfo, 743 const struct nf_nat_range2 *range, 744 enum nf_nat_manip_type maniptype) 745 { 746 int hooknum, nh_off, err = NF_ACCEPT; 747 748 nh_off = skb_network_offset(skb); 749 skb_pull_rcsum(skb, nh_off); 750 751 /* See HOOK2MANIP(). */ 752 if (maniptype == NF_NAT_MANIP_SRC) 753 hooknum = NF_INET_LOCAL_IN; /* Source NAT */ 754 else 755 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ 756 757 switch (ctinfo) { 758 case IP_CT_RELATED: 759 case IP_CT_RELATED_REPLY: 760 if (IS_ENABLED(CONFIG_NF_NAT) && 761 skb->protocol == htons(ETH_P_IP) && 762 ip_hdr(skb)->protocol == IPPROTO_ICMP) { 763 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 764 hooknum)) 765 err = NF_DROP; 766 goto push; 767 } else if (IS_ENABLED(CONFIG_IPV6) && 768 skb->protocol == htons(ETH_P_IPV6)) { 769 __be16 frag_off; 770 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 771 int hdrlen = ipv6_skip_exthdr(skb, 772 sizeof(struct ipv6hdr), 773 &nexthdr, &frag_off); 774 775 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 776 if (!nf_nat_icmpv6_reply_translation(skb, ct, 777 ctinfo, 778 hooknum, 779 hdrlen)) 780 err = NF_DROP; 781 goto push; 782 } 783 } 784 /* Non-ICMP, fall thru to initialize if needed. */ 785 fallthrough; 786 case IP_CT_NEW: 787 /* Seen it before? This can happen for loopback, retrans, 788 * or local packets. 789 */ 790 if (!nf_nat_initialized(ct, maniptype)) { 791 /* Initialize according to the NAT action. */ 792 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) 793 /* Action is set up to establish a new 794 * mapping. 795 */ 796 ? nf_nat_setup_info(ct, range, maniptype) 797 : nf_nat_alloc_null_binding(ct, hooknum); 798 if (err != NF_ACCEPT) 799 goto push; 800 } 801 break; 802 803 case IP_CT_ESTABLISHED: 804 case IP_CT_ESTABLISHED_REPLY: 805 break; 806 807 default: 808 err = NF_DROP; 809 goto push; 810 } 811 812 err = nf_nat_packet(ct, ctinfo, hooknum, skb); 813 push: 814 skb_push_rcsum(skb, nh_off); 815 816 return err; 817 } 818 819 static void ovs_nat_update_key(struct sw_flow_key *key, 820 const struct sk_buff *skb, 821 enum nf_nat_manip_type maniptype) 822 { 823 if (maniptype == NF_NAT_MANIP_SRC) { 824 __be16 src; 825 826 key->ct_state |= OVS_CS_F_SRC_NAT; 827 if (key->eth.type == htons(ETH_P_IP)) 828 key->ipv4.addr.src = ip_hdr(skb)->saddr; 829 else if (key->eth.type == htons(ETH_P_IPV6)) 830 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr, 831 sizeof(key->ipv6.addr.src)); 832 else 833 return; 834 835 if (key->ip.proto == IPPROTO_UDP) 836 src = udp_hdr(skb)->source; 837 else if (key->ip.proto == IPPROTO_TCP) 838 src = tcp_hdr(skb)->source; 839 else if (key->ip.proto == IPPROTO_SCTP) 840 src = sctp_hdr(skb)->source; 841 else 842 return; 843 844 key->tp.src = src; 845 } else { 846 __be16 dst; 847 848 key->ct_state |= OVS_CS_F_DST_NAT; 849 if (key->eth.type == htons(ETH_P_IP)) 850 key->ipv4.addr.dst = ip_hdr(skb)->daddr; 851 else if (key->eth.type == htons(ETH_P_IPV6)) 852 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr, 853 sizeof(key->ipv6.addr.dst)); 854 else 855 return; 856 857 if (key->ip.proto == IPPROTO_UDP) 858 dst = udp_hdr(skb)->dest; 859 else if (key->ip.proto == IPPROTO_TCP) 860 dst = tcp_hdr(skb)->dest; 861 else if (key->ip.proto == IPPROTO_SCTP) 862 dst = sctp_hdr(skb)->dest; 863 else 864 return; 865 866 key->tp.dst = dst; 867 } 868 } 869 870 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */ 871 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, 872 const struct ovs_conntrack_info *info, 873 struct sk_buff *skb, struct nf_conn *ct, 874 enum ip_conntrack_info ctinfo) 875 { 876 enum nf_nat_manip_type maniptype; 877 int err; 878 879 /* Add NAT extension if not confirmed yet. */ 880 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) 881 return NF_ACCEPT; /* Can't NAT. */ 882 883 /* Determine NAT type. 884 * Check if the NAT type can be deduced from the tracked connection. 885 * Make sure new expected connections (IP_CT_RELATED) are NATted only 886 * when committing. 887 */ 888 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW && 889 ct->status & IPS_NAT_MASK && 890 (ctinfo != IP_CT_RELATED || info->commit)) { 891 /* NAT an established or related connection like before. */ 892 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) 893 /* This is the REPLY direction for a connection 894 * for which NAT was applied in the forward 895 * direction. Do the reverse NAT. 896 */ 897 maniptype = ct->status & IPS_SRC_NAT 898 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; 899 else 900 maniptype = ct->status & IPS_SRC_NAT 901 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; 902 } else if (info->nat & OVS_CT_SRC_NAT) { 903 maniptype = NF_NAT_MANIP_SRC; 904 } else if (info->nat & OVS_CT_DST_NAT) { 905 maniptype = NF_NAT_MANIP_DST; 906 } else { 907 return NF_ACCEPT; /* Connection is not NATed. */ 908 } 909 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype); 910 911 if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) { 912 if (ct->status & IPS_SRC_NAT) { 913 if (maniptype == NF_NAT_MANIP_SRC) 914 maniptype = NF_NAT_MANIP_DST; 915 else 916 maniptype = NF_NAT_MANIP_SRC; 917 918 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, 919 maniptype); 920 } else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) { 921 err = ovs_ct_nat_execute(skb, ct, ctinfo, NULL, 922 NF_NAT_MANIP_SRC); 923 } 924 } 925 926 /* Mark NAT done if successful and update the flow key. */ 927 if (err == NF_ACCEPT) 928 ovs_nat_update_key(key, skb, maniptype); 929 930 return err; 931 } 932 #else /* !CONFIG_NF_NAT */ 933 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, 934 const struct ovs_conntrack_info *info, 935 struct sk_buff *skb, struct nf_conn *ct, 936 enum ip_conntrack_info ctinfo) 937 { 938 return NF_ACCEPT; 939 } 940 #endif 941 942 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if 943 * not done already. Update key with new CT state after passing the packet 944 * through conntrack. 945 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be 946 * set to NULL and 0 will be returned. 947 */ 948 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, 949 const struct ovs_conntrack_info *info, 950 struct sk_buff *skb) 951 { 952 /* If we are recirculating packets to match on conntrack fields and 953 * committing with a separate conntrack action, then we don't need to 954 * actually run the packet through conntrack twice unless it's for a 955 * different zone. 956 */ 957 bool cached = skb_nfct_cached(net, key, info, skb); 958 enum ip_conntrack_info ctinfo; 959 struct nf_conn *ct; 960 961 if (!cached) { 962 struct nf_hook_state state = { 963 .hook = NF_INET_PRE_ROUTING, 964 .pf = info->family, 965 .net = net, 966 }; 967 struct nf_conn *tmpl = info->ct; 968 int err; 969 970 /* Associate skb with specified zone. */ 971 if (tmpl) { 972 nf_conntrack_put(skb_nfct(skb)); 973 nf_conntrack_get(&tmpl->ct_general); 974 nf_ct_set(skb, tmpl, IP_CT_NEW); 975 } 976 977 err = nf_conntrack_in(skb, &state); 978 if (err != NF_ACCEPT) 979 return -ENOENT; 980 981 /* Clear CT state NAT flags to mark that we have not yet done 982 * NAT after the nf_conntrack_in() call. We can actually clear 983 * the whole state, as it will be re-initialized below. 984 */ 985 key->ct_state = 0; 986 987 /* Update the key, but keep the NAT flags. */ 988 ovs_ct_update_key(skb, info, key, true, true); 989 } 990 991 ct = nf_ct_get(skb, &ctinfo); 992 if (ct) { 993 bool add_helper = false; 994 995 /* Packets starting a new connection must be NATted before the 996 * helper, so that the helper knows about the NAT. We enforce 997 * this by delaying both NAT and helper calls for unconfirmed 998 * connections until the committing CT action. For later 999 * packets NAT and Helper may be called in either order. 1000 * 1001 * NAT will be done only if the CT action has NAT, and only 1002 * once per packet (per zone), as guarded by the NAT bits in 1003 * the key->ct_state. 1004 */ 1005 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) && 1006 (nf_ct_is_confirmed(ct) || info->commit) && 1007 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) { 1008 return -EINVAL; 1009 } 1010 1011 /* Userspace may decide to perform a ct lookup without a helper 1012 * specified followed by a (recirculate and) commit with one, 1013 * or attach a helper in a later commit. Therefore, for 1014 * connections which we will commit, we may need to attach 1015 * the helper here. 1016 */ 1017 if (info->commit && info->helper && !nfct_help(ct)) { 1018 int err = __nf_ct_try_assign_helper(ct, info->ct, 1019 GFP_ATOMIC); 1020 if (err) 1021 return err; 1022 add_helper = true; 1023 1024 /* helper installed, add seqadj if NAT is required */ 1025 if (info->nat && !nfct_seqadj(ct)) { 1026 if (!nfct_seqadj_ext_add(ct)) 1027 return -EINVAL; 1028 } 1029 } 1030 1031 /* Call the helper only if: 1032 * - nf_conntrack_in() was executed above ("!cached") or a 1033 * helper was just attached ("add_helper") for a confirmed 1034 * connection, or 1035 * - When committing an unconfirmed connection. 1036 */ 1037 if ((nf_ct_is_confirmed(ct) ? !cached || add_helper : 1038 info->commit) && 1039 ovs_ct_helper(skb, info->family) != NF_ACCEPT) { 1040 return -EINVAL; 1041 } 1042 1043 if (nf_ct_protonum(ct) == IPPROTO_TCP && 1044 nf_ct_is_confirmed(ct) && nf_conntrack_tcp_established(ct)) { 1045 /* Be liberal for tcp packets so that out-of-window 1046 * packets are not marked invalid. 1047 */ 1048 nf_ct_set_tcp_be_liberal(ct); 1049 } 1050 1051 nf_conn_act_ct_ext_fill(skb, ct, ctinfo); 1052 } 1053 1054 return 0; 1055 } 1056 1057 /* Lookup connection and read fields into key. */ 1058 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, 1059 const struct ovs_conntrack_info *info, 1060 struct sk_buff *skb) 1061 { 1062 struct nf_conntrack_expect *exp; 1063 1064 /* If we pass an expected packet through nf_conntrack_in() the 1065 * expectation is typically removed, but the packet could still be 1066 * lost in upcall processing. To prevent this from happening we 1067 * perform an explicit expectation lookup. Expected connections are 1068 * always new, and will be passed through conntrack only when they are 1069 * committed, as it is OK to remove the expectation at that time. 1070 */ 1071 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); 1072 if (exp) { 1073 u8 state; 1074 1075 /* NOTE: New connections are NATted and Helped only when 1076 * committed, so we are not calling into NAT here. 1077 */ 1078 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; 1079 __ovs_ct_update_key(key, state, &info->zone, exp->master); 1080 } else { 1081 struct nf_conn *ct; 1082 int err; 1083 1084 err = __ovs_ct_lookup(net, key, info, skb); 1085 if (err) 1086 return err; 1087 1088 ct = (struct nf_conn *)skb_nfct(skb); 1089 if (ct) 1090 nf_ct_deliver_cached_events(ct); 1091 } 1092 1093 return 0; 1094 } 1095 1096 static bool labels_nonzero(const struct ovs_key_ct_labels *labels) 1097 { 1098 size_t i; 1099 1100 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) 1101 if (labels->ct_labels_32[i]) 1102 return true; 1103 1104 return false; 1105 } 1106 1107 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 1108 static struct hlist_head *ct_limit_hash_bucket( 1109 const struct ovs_ct_limit_info *info, u16 zone) 1110 { 1111 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)]; 1112 } 1113 1114 /* Call with ovs_mutex */ 1115 static void ct_limit_set(const struct ovs_ct_limit_info *info, 1116 struct ovs_ct_limit *new_ct_limit) 1117 { 1118 struct ovs_ct_limit *ct_limit; 1119 struct hlist_head *head; 1120 1121 head = ct_limit_hash_bucket(info, new_ct_limit->zone); 1122 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { 1123 if (ct_limit->zone == new_ct_limit->zone) { 1124 hlist_replace_rcu(&ct_limit->hlist_node, 1125 &new_ct_limit->hlist_node); 1126 kfree_rcu(ct_limit, rcu); 1127 return; 1128 } 1129 } 1130 1131 hlist_add_head_rcu(&new_ct_limit->hlist_node, head); 1132 } 1133 1134 /* Call with ovs_mutex */ 1135 static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone) 1136 { 1137 struct ovs_ct_limit *ct_limit; 1138 struct hlist_head *head; 1139 struct hlist_node *n; 1140 1141 head = ct_limit_hash_bucket(info, zone); 1142 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) { 1143 if (ct_limit->zone == zone) { 1144 hlist_del_rcu(&ct_limit->hlist_node); 1145 kfree_rcu(ct_limit, rcu); 1146 return; 1147 } 1148 } 1149 } 1150 1151 /* Call with RCU read lock */ 1152 static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone) 1153 { 1154 struct ovs_ct_limit *ct_limit; 1155 struct hlist_head *head; 1156 1157 head = ct_limit_hash_bucket(info, zone); 1158 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { 1159 if (ct_limit->zone == zone) 1160 return ct_limit->limit; 1161 } 1162 1163 return info->default_limit; 1164 } 1165 1166 static int ovs_ct_check_limit(struct net *net, 1167 const struct ovs_conntrack_info *info, 1168 const struct nf_conntrack_tuple *tuple) 1169 { 1170 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1171 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; 1172 u32 per_zone_limit, connections; 1173 u32 conncount_key; 1174 1175 conncount_key = info->zone.id; 1176 1177 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id); 1178 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED) 1179 return 0; 1180 1181 connections = nf_conncount_count(net, ct_limit_info->data, 1182 &conncount_key, tuple, &info->zone); 1183 if (connections > per_zone_limit) 1184 return -ENOMEM; 1185 1186 return 0; 1187 } 1188 #endif 1189 1190 /* Lookup connection and confirm if unconfirmed. */ 1191 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, 1192 const struct ovs_conntrack_info *info, 1193 struct sk_buff *skb) 1194 { 1195 enum ip_conntrack_info ctinfo; 1196 struct nf_conn *ct; 1197 int err; 1198 1199 err = __ovs_ct_lookup(net, key, info, skb); 1200 if (err) 1201 return err; 1202 1203 /* The connection could be invalid, in which case this is a no-op.*/ 1204 ct = nf_ct_get(skb, &ctinfo); 1205 if (!ct) 1206 return 0; 1207 1208 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 1209 if (static_branch_unlikely(&ovs_ct_limit_enabled)) { 1210 if (!nf_ct_is_confirmed(ct)) { 1211 err = ovs_ct_check_limit(net, info, 1212 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); 1213 if (err) { 1214 net_warn_ratelimited("openvswitch: zone: %u " 1215 "exceeds conntrack limit\n", 1216 info->zone.id); 1217 return err; 1218 } 1219 } 1220 } 1221 #endif 1222 1223 /* Set the conntrack event mask if given. NEW and DELETE events have 1224 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener 1225 * typically would receive many kinds of updates. Setting the event 1226 * mask allows those events to be filtered. The set event mask will 1227 * remain in effect for the lifetime of the connection unless changed 1228 * by a further CT action with both the commit flag and the eventmask 1229 * option. */ 1230 if (info->have_eventmask) { 1231 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct); 1232 1233 if (cache) 1234 cache->ctmask = info->eventmask; 1235 } 1236 1237 /* Apply changes before confirming the connection so that the initial 1238 * conntrack NEW netlink event carries the values given in the CT 1239 * action. 1240 */ 1241 if (info->mark.mask) { 1242 err = ovs_ct_set_mark(ct, key, info->mark.value, 1243 info->mark.mask); 1244 if (err) 1245 return err; 1246 } 1247 if (!nf_ct_is_confirmed(ct)) { 1248 err = ovs_ct_init_labels(ct, key, &info->labels.value, 1249 &info->labels.mask); 1250 if (err) 1251 return err; 1252 1253 nf_conn_act_ct_ext_add(ct); 1254 } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1255 labels_nonzero(&info->labels.mask)) { 1256 err = ovs_ct_set_labels(ct, key, &info->labels.value, 1257 &info->labels.mask); 1258 if (err) 1259 return err; 1260 } 1261 /* This will take care of sending queued events even if the connection 1262 * is already confirmed. 1263 */ 1264 if (nf_conntrack_confirm(skb) != NF_ACCEPT) 1265 return -EINVAL; 1266 1267 return 0; 1268 } 1269 1270 /* Trim the skb to the length specified by the IP/IPv6 header, 1271 * removing any trailing lower-layer padding. This prepares the skb 1272 * for higher-layer processing that assumes skb->len excludes padding 1273 * (such as nf_ip_checksum). The caller needs to pull the skb to the 1274 * network header, and ensure ip_hdr/ipv6_hdr points to valid data. 1275 */ 1276 static int ovs_skb_network_trim(struct sk_buff *skb) 1277 { 1278 unsigned int len; 1279 int err; 1280 1281 switch (skb->protocol) { 1282 case htons(ETH_P_IP): 1283 len = ntohs(ip_hdr(skb)->tot_len); 1284 break; 1285 case htons(ETH_P_IPV6): 1286 len = sizeof(struct ipv6hdr) 1287 + ntohs(ipv6_hdr(skb)->payload_len); 1288 break; 1289 default: 1290 len = skb->len; 1291 } 1292 1293 err = pskb_trim_rcsum(skb, len); 1294 if (err) 1295 kfree_skb(skb); 1296 1297 return err; 1298 } 1299 1300 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero 1301 * value if 'skb' is freed. 1302 */ 1303 int ovs_ct_execute(struct net *net, struct sk_buff *skb, 1304 struct sw_flow_key *key, 1305 const struct ovs_conntrack_info *info) 1306 { 1307 int nh_ofs; 1308 int err; 1309 1310 /* The conntrack module expects to be working at L3. */ 1311 nh_ofs = skb_network_offset(skb); 1312 skb_pull_rcsum(skb, nh_ofs); 1313 1314 err = ovs_skb_network_trim(skb); 1315 if (err) 1316 return err; 1317 1318 if (key->ip.frag != OVS_FRAG_TYPE_NONE) { 1319 err = handle_fragments(net, key, info->zone.id, skb); 1320 if (err) 1321 return err; 1322 } 1323 1324 if (info->commit) 1325 err = ovs_ct_commit(net, key, info, skb); 1326 else 1327 err = ovs_ct_lookup(net, key, info, skb); 1328 1329 skb_push_rcsum(skb, nh_ofs); 1330 if (err) 1331 kfree_skb(skb); 1332 return err; 1333 } 1334 1335 int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key) 1336 { 1337 nf_conntrack_put(skb_nfct(skb)); 1338 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 1339 ovs_ct_fill_key(skb, key, false); 1340 1341 return 0; 1342 } 1343 1344 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, 1345 const struct sw_flow_key *key, bool log) 1346 { 1347 struct nf_conntrack_helper *helper; 1348 struct nf_conn_help *help; 1349 int ret = 0; 1350 1351 helper = nf_conntrack_helper_try_module_get(name, info->family, 1352 key->ip.proto); 1353 if (!helper) { 1354 OVS_NLERR(log, "Unknown helper \"%s\"", name); 1355 return -EINVAL; 1356 } 1357 1358 help = nf_ct_helper_ext_add(info->ct, GFP_KERNEL); 1359 if (!help) { 1360 nf_conntrack_helper_put(helper); 1361 return -ENOMEM; 1362 } 1363 1364 #if IS_ENABLED(CONFIG_NF_NAT) 1365 if (info->nat) { 1366 ret = nf_nat_helper_try_module_get(name, info->family, 1367 key->ip.proto); 1368 if (ret) { 1369 nf_conntrack_helper_put(helper); 1370 OVS_NLERR(log, "Failed to load \"%s\" NAT helper, error: %d", 1371 name, ret); 1372 return ret; 1373 } 1374 } 1375 #endif 1376 rcu_assign_pointer(help->helper, helper); 1377 info->helper = helper; 1378 return ret; 1379 } 1380 1381 #if IS_ENABLED(CONFIG_NF_NAT) 1382 static int parse_nat(const struct nlattr *attr, 1383 struct ovs_conntrack_info *info, bool log) 1384 { 1385 struct nlattr *a; 1386 int rem; 1387 bool have_ip_max = false; 1388 bool have_proto_max = false; 1389 bool ip_vers = (info->family == NFPROTO_IPV6); 1390 1391 nla_for_each_nested(a, attr, rem) { 1392 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = { 1393 [OVS_NAT_ATTR_SRC] = {0, 0}, 1394 [OVS_NAT_ATTR_DST] = {0, 0}, 1395 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr), 1396 sizeof(struct in6_addr)}, 1397 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr), 1398 sizeof(struct in6_addr)}, 1399 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)}, 1400 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)}, 1401 [OVS_NAT_ATTR_PERSISTENT] = {0, 0}, 1402 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0}, 1403 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0}, 1404 }; 1405 int type = nla_type(a); 1406 1407 if (type > OVS_NAT_ATTR_MAX) { 1408 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)", 1409 type, OVS_NAT_ATTR_MAX); 1410 return -EINVAL; 1411 } 1412 1413 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) { 1414 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)", 1415 type, nla_len(a), 1416 ovs_nat_attr_lens[type][ip_vers]); 1417 return -EINVAL; 1418 } 1419 1420 switch (type) { 1421 case OVS_NAT_ATTR_SRC: 1422 case OVS_NAT_ATTR_DST: 1423 if (info->nat) { 1424 OVS_NLERR(log, "Only one type of NAT may be specified"); 1425 return -ERANGE; 1426 } 1427 info->nat |= OVS_CT_NAT; 1428 info->nat |= ((type == OVS_NAT_ATTR_SRC) 1429 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT); 1430 break; 1431 1432 case OVS_NAT_ATTR_IP_MIN: 1433 nla_memcpy(&info->range.min_addr, a, 1434 sizeof(info->range.min_addr)); 1435 info->range.flags |= NF_NAT_RANGE_MAP_IPS; 1436 break; 1437 1438 case OVS_NAT_ATTR_IP_MAX: 1439 have_ip_max = true; 1440 nla_memcpy(&info->range.max_addr, a, 1441 sizeof(info->range.max_addr)); 1442 info->range.flags |= NF_NAT_RANGE_MAP_IPS; 1443 break; 1444 1445 case OVS_NAT_ATTR_PROTO_MIN: 1446 info->range.min_proto.all = htons(nla_get_u16(a)); 1447 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1448 break; 1449 1450 case OVS_NAT_ATTR_PROTO_MAX: 1451 have_proto_max = true; 1452 info->range.max_proto.all = htons(nla_get_u16(a)); 1453 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 1454 break; 1455 1456 case OVS_NAT_ATTR_PERSISTENT: 1457 info->range.flags |= NF_NAT_RANGE_PERSISTENT; 1458 break; 1459 1460 case OVS_NAT_ATTR_PROTO_HASH: 1461 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; 1462 break; 1463 1464 case OVS_NAT_ATTR_PROTO_RANDOM: 1465 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY; 1466 break; 1467 1468 default: 1469 OVS_NLERR(log, "Unknown nat attribute (%d)", type); 1470 return -EINVAL; 1471 } 1472 } 1473 1474 if (rem > 0) { 1475 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem); 1476 return -EINVAL; 1477 } 1478 if (!info->nat) { 1479 /* Do not allow flags if no type is given. */ 1480 if (info->range.flags) { 1481 OVS_NLERR(log, 1482 "NAT flags may be given only when NAT range (SRC or DST) is also specified." 1483 ); 1484 return -EINVAL; 1485 } 1486 info->nat = OVS_CT_NAT; /* NAT existing connections. */ 1487 } else if (!info->commit) { 1488 OVS_NLERR(log, 1489 "NAT attributes may be specified only when CT COMMIT flag is also specified." 1490 ); 1491 return -EINVAL; 1492 } 1493 /* Allow missing IP_MAX. */ 1494 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) { 1495 memcpy(&info->range.max_addr, &info->range.min_addr, 1496 sizeof(info->range.max_addr)); 1497 } 1498 /* Allow missing PROTO_MAX. */ 1499 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && 1500 !have_proto_max) { 1501 info->range.max_proto.all = info->range.min_proto.all; 1502 } 1503 return 0; 1504 } 1505 #endif 1506 1507 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { 1508 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, 1509 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 }, 1510 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), 1511 .maxlen = sizeof(u16) }, 1512 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), 1513 .maxlen = sizeof(struct md_mark) }, 1514 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), 1515 .maxlen = sizeof(struct md_labels) }, 1516 [OVS_CT_ATTR_HELPER] = { .minlen = 1, 1517 .maxlen = NF_CT_HELPER_NAME_LEN }, 1518 #if IS_ENABLED(CONFIG_NF_NAT) 1519 /* NAT length is checked when parsing the nested attributes. */ 1520 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX }, 1521 #endif 1522 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32), 1523 .maxlen = sizeof(u32) }, 1524 [OVS_CT_ATTR_TIMEOUT] = { .minlen = 1, 1525 .maxlen = CTNL_TIMEOUT_NAME_MAX }, 1526 }; 1527 1528 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, 1529 const char **helper, bool log) 1530 { 1531 struct nlattr *a; 1532 int rem; 1533 1534 nla_for_each_nested(a, attr, rem) { 1535 int type = nla_type(a); 1536 int maxlen; 1537 int minlen; 1538 1539 if (type > OVS_CT_ATTR_MAX) { 1540 OVS_NLERR(log, 1541 "Unknown conntrack attr (type=%d, max=%d)", 1542 type, OVS_CT_ATTR_MAX); 1543 return -EINVAL; 1544 } 1545 1546 maxlen = ovs_ct_attr_lens[type].maxlen; 1547 minlen = ovs_ct_attr_lens[type].minlen; 1548 if (nla_len(a) < minlen || nla_len(a) > maxlen) { 1549 OVS_NLERR(log, 1550 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", 1551 type, nla_len(a), maxlen); 1552 return -EINVAL; 1553 } 1554 1555 switch (type) { 1556 case OVS_CT_ATTR_FORCE_COMMIT: 1557 info->force = true; 1558 fallthrough; 1559 case OVS_CT_ATTR_COMMIT: 1560 info->commit = true; 1561 break; 1562 #ifdef CONFIG_NF_CONNTRACK_ZONES 1563 case OVS_CT_ATTR_ZONE: 1564 info->zone.id = nla_get_u16(a); 1565 break; 1566 #endif 1567 #ifdef CONFIG_NF_CONNTRACK_MARK 1568 case OVS_CT_ATTR_MARK: { 1569 struct md_mark *mark = nla_data(a); 1570 1571 if (!mark->mask) { 1572 OVS_NLERR(log, "ct_mark mask cannot be 0"); 1573 return -EINVAL; 1574 } 1575 info->mark = *mark; 1576 break; 1577 } 1578 #endif 1579 #ifdef CONFIG_NF_CONNTRACK_LABELS 1580 case OVS_CT_ATTR_LABELS: { 1581 struct md_labels *labels = nla_data(a); 1582 1583 if (!labels_nonzero(&labels->mask)) { 1584 OVS_NLERR(log, "ct_labels mask cannot be 0"); 1585 return -EINVAL; 1586 } 1587 info->labels = *labels; 1588 break; 1589 } 1590 #endif 1591 case OVS_CT_ATTR_HELPER: 1592 *helper = nla_data(a); 1593 if (!memchr(*helper, '\0', nla_len(a))) { 1594 OVS_NLERR(log, "Invalid conntrack helper"); 1595 return -EINVAL; 1596 } 1597 break; 1598 #if IS_ENABLED(CONFIG_NF_NAT) 1599 case OVS_CT_ATTR_NAT: { 1600 int err = parse_nat(a, info, log); 1601 1602 if (err) 1603 return err; 1604 break; 1605 } 1606 #endif 1607 case OVS_CT_ATTR_EVENTMASK: 1608 info->have_eventmask = true; 1609 info->eventmask = nla_get_u32(a); 1610 break; 1611 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT 1612 case OVS_CT_ATTR_TIMEOUT: 1613 memcpy(info->timeout, nla_data(a), nla_len(a)); 1614 if (!memchr(info->timeout, '\0', nla_len(a))) { 1615 OVS_NLERR(log, "Invalid conntrack timeout"); 1616 return -EINVAL; 1617 } 1618 break; 1619 #endif 1620 1621 default: 1622 OVS_NLERR(log, "Unknown conntrack attr (%d)", 1623 type); 1624 return -EINVAL; 1625 } 1626 } 1627 1628 #ifdef CONFIG_NF_CONNTRACK_MARK 1629 if (!info->commit && info->mark.mask) { 1630 OVS_NLERR(log, 1631 "Setting conntrack mark requires 'commit' flag."); 1632 return -EINVAL; 1633 } 1634 #endif 1635 #ifdef CONFIG_NF_CONNTRACK_LABELS 1636 if (!info->commit && labels_nonzero(&info->labels.mask)) { 1637 OVS_NLERR(log, 1638 "Setting conntrack labels requires 'commit' flag."); 1639 return -EINVAL; 1640 } 1641 #endif 1642 if (rem > 0) { 1643 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); 1644 return -EINVAL; 1645 } 1646 1647 return 0; 1648 } 1649 1650 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) 1651 { 1652 if (attr == OVS_KEY_ATTR_CT_STATE) 1653 return true; 1654 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1655 attr == OVS_KEY_ATTR_CT_ZONE) 1656 return true; 1657 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 1658 attr == OVS_KEY_ATTR_CT_MARK) 1659 return true; 1660 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1661 attr == OVS_KEY_ATTR_CT_LABELS) { 1662 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1663 1664 return ovs_net->xt_label; 1665 } 1666 1667 return false; 1668 } 1669 1670 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, 1671 const struct sw_flow_key *key, 1672 struct sw_flow_actions **sfa, bool log) 1673 { 1674 struct ovs_conntrack_info ct_info; 1675 const char *helper = NULL; 1676 u16 family; 1677 int err; 1678 1679 family = key_to_nfproto(key); 1680 if (family == NFPROTO_UNSPEC) { 1681 OVS_NLERR(log, "ct family unspecified"); 1682 return -EINVAL; 1683 } 1684 1685 memset(&ct_info, 0, sizeof(ct_info)); 1686 ct_info.family = family; 1687 1688 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, 1689 NF_CT_DEFAULT_ZONE_DIR, 0); 1690 1691 err = parse_ct(attr, &ct_info, &helper, log); 1692 if (err) 1693 return err; 1694 1695 /* Set up template for tracking connections in specific zones. */ 1696 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); 1697 if (!ct_info.ct) { 1698 OVS_NLERR(log, "Failed to allocate conntrack template"); 1699 return -ENOMEM; 1700 } 1701 1702 if (ct_info.timeout[0]) { 1703 if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto, 1704 ct_info.timeout)) 1705 pr_info_ratelimited("Failed to associated timeout " 1706 "policy `%s'\n", ct_info.timeout); 1707 else 1708 ct_info.nf_ct_timeout = rcu_dereference( 1709 nf_ct_timeout_find(ct_info.ct)->timeout); 1710 1711 } 1712 1713 if (helper) { 1714 err = ovs_ct_add_helper(&ct_info, helper, key, log); 1715 if (err) 1716 goto err_free_ct; 1717 } 1718 1719 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, 1720 sizeof(ct_info), log); 1721 if (err) 1722 goto err_free_ct; 1723 1724 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); 1725 nf_conntrack_get(&ct_info.ct->ct_general); 1726 return 0; 1727 err_free_ct: 1728 __ovs_ct_free_action(&ct_info); 1729 return err; 1730 } 1731 1732 #if IS_ENABLED(CONFIG_NF_NAT) 1733 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info, 1734 struct sk_buff *skb) 1735 { 1736 struct nlattr *start; 1737 1738 start = nla_nest_start_noflag(skb, OVS_CT_ATTR_NAT); 1739 if (!start) 1740 return false; 1741 1742 if (info->nat & OVS_CT_SRC_NAT) { 1743 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC)) 1744 return false; 1745 } else if (info->nat & OVS_CT_DST_NAT) { 1746 if (nla_put_flag(skb, OVS_NAT_ATTR_DST)) 1747 return false; 1748 } else { 1749 goto out; 1750 } 1751 1752 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) { 1753 if (IS_ENABLED(CONFIG_NF_NAT) && 1754 info->family == NFPROTO_IPV4) { 1755 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN, 1756 info->range.min_addr.ip) || 1757 (info->range.max_addr.ip 1758 != info->range.min_addr.ip && 1759 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX, 1760 info->range.max_addr.ip)))) 1761 return false; 1762 } else if (IS_ENABLED(CONFIG_IPV6) && 1763 info->family == NFPROTO_IPV6) { 1764 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN, 1765 &info->range.min_addr.in6) || 1766 (memcmp(&info->range.max_addr.in6, 1767 &info->range.min_addr.in6, 1768 sizeof(info->range.max_addr.in6)) && 1769 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX, 1770 &info->range.max_addr.in6)))) 1771 return false; 1772 } else { 1773 return false; 1774 } 1775 } 1776 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && 1777 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN, 1778 ntohs(info->range.min_proto.all)) || 1779 (info->range.max_proto.all != info->range.min_proto.all && 1780 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX, 1781 ntohs(info->range.max_proto.all))))) 1782 return false; 1783 1784 if (info->range.flags & NF_NAT_RANGE_PERSISTENT && 1785 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT)) 1786 return false; 1787 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM && 1788 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH)) 1789 return false; 1790 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY && 1791 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM)) 1792 return false; 1793 out: 1794 nla_nest_end(skb, start); 1795 1796 return true; 1797 } 1798 #endif 1799 1800 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, 1801 struct sk_buff *skb) 1802 { 1803 struct nlattr *start; 1804 1805 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CT); 1806 if (!start) 1807 return -EMSGSIZE; 1808 1809 if (ct_info->commit && nla_put_flag(skb, ct_info->force 1810 ? OVS_CT_ATTR_FORCE_COMMIT 1811 : OVS_CT_ATTR_COMMIT)) 1812 return -EMSGSIZE; 1813 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 1814 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) 1815 return -EMSGSIZE; 1816 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && 1817 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), 1818 &ct_info->mark)) 1819 return -EMSGSIZE; 1820 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 1821 labels_nonzero(&ct_info->labels.mask) && 1822 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), 1823 &ct_info->labels)) 1824 return -EMSGSIZE; 1825 if (ct_info->helper) { 1826 if (nla_put_string(skb, OVS_CT_ATTR_HELPER, 1827 ct_info->helper->name)) 1828 return -EMSGSIZE; 1829 } 1830 if (ct_info->have_eventmask && 1831 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask)) 1832 return -EMSGSIZE; 1833 if (ct_info->timeout[0]) { 1834 if (nla_put_string(skb, OVS_CT_ATTR_TIMEOUT, ct_info->timeout)) 1835 return -EMSGSIZE; 1836 } 1837 1838 #if IS_ENABLED(CONFIG_NF_NAT) 1839 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb)) 1840 return -EMSGSIZE; 1841 #endif 1842 nla_nest_end(skb, start); 1843 1844 return 0; 1845 } 1846 1847 void ovs_ct_free_action(const struct nlattr *a) 1848 { 1849 struct ovs_conntrack_info *ct_info = nla_data(a); 1850 1851 __ovs_ct_free_action(ct_info); 1852 } 1853 1854 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) 1855 { 1856 if (ct_info->helper) { 1857 #if IS_ENABLED(CONFIG_NF_NAT) 1858 if (ct_info->nat) 1859 nf_nat_helper_put(ct_info->helper); 1860 #endif 1861 nf_conntrack_helper_put(ct_info->helper); 1862 } 1863 if (ct_info->ct) { 1864 if (ct_info->timeout[0]) 1865 nf_ct_destroy_timeout(ct_info->ct); 1866 nf_ct_tmpl_free(ct_info->ct); 1867 } 1868 } 1869 1870 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 1871 static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net) 1872 { 1873 int i, err; 1874 1875 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info), 1876 GFP_KERNEL); 1877 if (!ovs_net->ct_limit_info) 1878 return -ENOMEM; 1879 1880 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT; 1881 ovs_net->ct_limit_info->limits = 1882 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head), 1883 GFP_KERNEL); 1884 if (!ovs_net->ct_limit_info->limits) { 1885 kfree(ovs_net->ct_limit_info); 1886 return -ENOMEM; 1887 } 1888 1889 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++) 1890 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]); 1891 1892 ovs_net->ct_limit_info->data = 1893 nf_conncount_init(net, NFPROTO_INET, sizeof(u32)); 1894 1895 if (IS_ERR(ovs_net->ct_limit_info->data)) { 1896 err = PTR_ERR(ovs_net->ct_limit_info->data); 1897 kfree(ovs_net->ct_limit_info->limits); 1898 kfree(ovs_net->ct_limit_info); 1899 pr_err("openvswitch: failed to init nf_conncount %d\n", err); 1900 return err; 1901 } 1902 return 0; 1903 } 1904 1905 static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net) 1906 { 1907 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info; 1908 int i; 1909 1910 nf_conncount_destroy(net, NFPROTO_INET, info->data); 1911 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { 1912 struct hlist_head *head = &info->limits[i]; 1913 struct ovs_ct_limit *ct_limit; 1914 1915 hlist_for_each_entry_rcu(ct_limit, head, hlist_node, 1916 lockdep_ovsl_is_held()) 1917 kfree_rcu(ct_limit, rcu); 1918 } 1919 kfree(info->limits); 1920 kfree(info); 1921 } 1922 1923 static struct sk_buff * 1924 ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd, 1925 struct ovs_header **ovs_reply_header) 1926 { 1927 struct ovs_header *ovs_header = info->userhdr; 1928 struct sk_buff *skb; 1929 1930 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1931 if (!skb) 1932 return ERR_PTR(-ENOMEM); 1933 1934 *ovs_reply_header = genlmsg_put(skb, info->snd_portid, 1935 info->snd_seq, 1936 &dp_ct_limit_genl_family, 0, cmd); 1937 1938 if (!*ovs_reply_header) { 1939 nlmsg_free(skb); 1940 return ERR_PTR(-EMSGSIZE); 1941 } 1942 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex; 1943 1944 return skb; 1945 } 1946 1947 static bool check_zone_id(int zone_id, u16 *pzone) 1948 { 1949 if (zone_id >= 0 && zone_id <= 65535) { 1950 *pzone = (u16)zone_id; 1951 return true; 1952 } 1953 return false; 1954 } 1955 1956 static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit, 1957 struct ovs_ct_limit_info *info) 1958 { 1959 struct ovs_zone_limit *zone_limit; 1960 int rem; 1961 u16 zone; 1962 1963 rem = NLA_ALIGN(nla_len(nla_zone_limit)); 1964 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); 1965 1966 while (rem >= sizeof(*zone_limit)) { 1967 if (unlikely(zone_limit->zone_id == 1968 OVS_ZONE_LIMIT_DEFAULT_ZONE)) { 1969 ovs_lock(); 1970 info->default_limit = zone_limit->limit; 1971 ovs_unlock(); 1972 } else if (unlikely(!check_zone_id( 1973 zone_limit->zone_id, &zone))) { 1974 OVS_NLERR(true, "zone id is out of range"); 1975 } else { 1976 struct ovs_ct_limit *ct_limit; 1977 1978 ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL); 1979 if (!ct_limit) 1980 return -ENOMEM; 1981 1982 ct_limit->zone = zone; 1983 ct_limit->limit = zone_limit->limit; 1984 1985 ovs_lock(); 1986 ct_limit_set(info, ct_limit); 1987 ovs_unlock(); 1988 } 1989 rem -= NLA_ALIGN(sizeof(*zone_limit)); 1990 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + 1991 NLA_ALIGN(sizeof(*zone_limit))); 1992 } 1993 1994 if (rem) 1995 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem); 1996 1997 return 0; 1998 } 1999 2000 static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit, 2001 struct ovs_ct_limit_info *info) 2002 { 2003 struct ovs_zone_limit *zone_limit; 2004 int rem; 2005 u16 zone; 2006 2007 rem = NLA_ALIGN(nla_len(nla_zone_limit)); 2008 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); 2009 2010 while (rem >= sizeof(*zone_limit)) { 2011 if (unlikely(zone_limit->zone_id == 2012 OVS_ZONE_LIMIT_DEFAULT_ZONE)) { 2013 ovs_lock(); 2014 info->default_limit = OVS_CT_LIMIT_DEFAULT; 2015 ovs_unlock(); 2016 } else if (unlikely(!check_zone_id( 2017 zone_limit->zone_id, &zone))) { 2018 OVS_NLERR(true, "zone id is out of range"); 2019 } else { 2020 ovs_lock(); 2021 ct_limit_del(info, zone); 2022 ovs_unlock(); 2023 } 2024 rem -= NLA_ALIGN(sizeof(*zone_limit)); 2025 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + 2026 NLA_ALIGN(sizeof(*zone_limit))); 2027 } 2028 2029 if (rem) 2030 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem); 2031 2032 return 0; 2033 } 2034 2035 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info, 2036 struct sk_buff *reply) 2037 { 2038 struct ovs_zone_limit zone_limit = { 2039 .zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE, 2040 .limit = info->default_limit, 2041 }; 2042 2043 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); 2044 } 2045 2046 static int __ovs_ct_limit_get_zone_limit(struct net *net, 2047 struct nf_conncount_data *data, 2048 u16 zone_id, u32 limit, 2049 struct sk_buff *reply) 2050 { 2051 struct nf_conntrack_zone ct_zone; 2052 struct ovs_zone_limit zone_limit; 2053 u32 conncount_key = zone_id; 2054 2055 zone_limit.zone_id = zone_id; 2056 zone_limit.limit = limit; 2057 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0); 2058 2059 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL, 2060 &ct_zone); 2061 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); 2062 } 2063 2064 static int ovs_ct_limit_get_zone_limit(struct net *net, 2065 struct nlattr *nla_zone_limit, 2066 struct ovs_ct_limit_info *info, 2067 struct sk_buff *reply) 2068 { 2069 struct ovs_zone_limit *zone_limit; 2070 int rem, err; 2071 u32 limit; 2072 u16 zone; 2073 2074 rem = NLA_ALIGN(nla_len(nla_zone_limit)); 2075 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); 2076 2077 while (rem >= sizeof(*zone_limit)) { 2078 if (unlikely(zone_limit->zone_id == 2079 OVS_ZONE_LIMIT_DEFAULT_ZONE)) { 2080 err = ovs_ct_limit_get_default_limit(info, reply); 2081 if (err) 2082 return err; 2083 } else if (unlikely(!check_zone_id(zone_limit->zone_id, 2084 &zone))) { 2085 OVS_NLERR(true, "zone id is out of range"); 2086 } else { 2087 rcu_read_lock(); 2088 limit = ct_limit_get(info, zone); 2089 rcu_read_unlock(); 2090 2091 err = __ovs_ct_limit_get_zone_limit( 2092 net, info->data, zone, limit, reply); 2093 if (err) 2094 return err; 2095 } 2096 rem -= NLA_ALIGN(sizeof(*zone_limit)); 2097 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + 2098 NLA_ALIGN(sizeof(*zone_limit))); 2099 } 2100 2101 if (rem) 2102 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem); 2103 2104 return 0; 2105 } 2106 2107 static int ovs_ct_limit_get_all_zone_limit(struct net *net, 2108 struct ovs_ct_limit_info *info, 2109 struct sk_buff *reply) 2110 { 2111 struct ovs_ct_limit *ct_limit; 2112 struct hlist_head *head; 2113 int i, err = 0; 2114 2115 err = ovs_ct_limit_get_default_limit(info, reply); 2116 if (err) 2117 return err; 2118 2119 rcu_read_lock(); 2120 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { 2121 head = &info->limits[i]; 2122 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { 2123 err = __ovs_ct_limit_get_zone_limit(net, info->data, 2124 ct_limit->zone, ct_limit->limit, reply); 2125 if (err) 2126 goto exit_err; 2127 } 2128 } 2129 2130 exit_err: 2131 rcu_read_unlock(); 2132 return err; 2133 } 2134 2135 static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info) 2136 { 2137 struct nlattr **a = info->attrs; 2138 struct sk_buff *reply; 2139 struct ovs_header *ovs_reply_header; 2140 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 2141 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; 2142 int err; 2143 2144 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET, 2145 &ovs_reply_header); 2146 if (IS_ERR(reply)) 2147 return PTR_ERR(reply); 2148 2149 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { 2150 err = -EINVAL; 2151 goto exit_err; 2152 } 2153 2154 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], 2155 ct_limit_info); 2156 if (err) 2157 goto exit_err; 2158 2159 static_branch_enable(&ovs_ct_limit_enabled); 2160 2161 genlmsg_end(reply, ovs_reply_header); 2162 return genlmsg_reply(reply, info); 2163 2164 exit_err: 2165 nlmsg_free(reply); 2166 return err; 2167 } 2168 2169 static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info) 2170 { 2171 struct nlattr **a = info->attrs; 2172 struct sk_buff *reply; 2173 struct ovs_header *ovs_reply_header; 2174 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 2175 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; 2176 int err; 2177 2178 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL, 2179 &ovs_reply_header); 2180 if (IS_ERR(reply)) 2181 return PTR_ERR(reply); 2182 2183 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { 2184 err = -EINVAL; 2185 goto exit_err; 2186 } 2187 2188 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], 2189 ct_limit_info); 2190 if (err) 2191 goto exit_err; 2192 2193 genlmsg_end(reply, ovs_reply_header); 2194 return genlmsg_reply(reply, info); 2195 2196 exit_err: 2197 nlmsg_free(reply); 2198 return err; 2199 } 2200 2201 static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info) 2202 { 2203 struct nlattr **a = info->attrs; 2204 struct nlattr *nla_reply; 2205 struct sk_buff *reply; 2206 struct ovs_header *ovs_reply_header; 2207 struct net *net = sock_net(skb->sk); 2208 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2209 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; 2210 int err; 2211 2212 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET, 2213 &ovs_reply_header); 2214 if (IS_ERR(reply)) 2215 return PTR_ERR(reply); 2216 2217 nla_reply = nla_nest_start_noflag(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT); 2218 if (!nla_reply) { 2219 err = -EMSGSIZE; 2220 goto exit_err; 2221 } 2222 2223 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { 2224 err = ovs_ct_limit_get_zone_limit( 2225 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info, 2226 reply); 2227 if (err) 2228 goto exit_err; 2229 } else { 2230 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info, 2231 reply); 2232 if (err) 2233 goto exit_err; 2234 } 2235 2236 nla_nest_end(reply, nla_reply); 2237 genlmsg_end(reply, ovs_reply_header); 2238 return genlmsg_reply(reply, info); 2239 2240 exit_err: 2241 nlmsg_free(reply); 2242 return err; 2243 } 2244 2245 static const struct genl_small_ops ct_limit_genl_ops[] = { 2246 { .cmd = OVS_CT_LIMIT_CMD_SET, 2247 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2248 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN 2249 * privilege. */ 2250 .doit = ovs_ct_limit_cmd_set, 2251 }, 2252 { .cmd = OVS_CT_LIMIT_CMD_DEL, 2253 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2254 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN 2255 * privilege. */ 2256 .doit = ovs_ct_limit_cmd_del, 2257 }, 2258 { .cmd = OVS_CT_LIMIT_CMD_GET, 2259 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2260 .flags = 0, /* OK for unprivileged users. */ 2261 .doit = ovs_ct_limit_cmd_get, 2262 }, 2263 }; 2264 2265 static const struct genl_multicast_group ovs_ct_limit_multicast_group = { 2266 .name = OVS_CT_LIMIT_MCGROUP, 2267 }; 2268 2269 struct genl_family dp_ct_limit_genl_family __ro_after_init = { 2270 .hdrsize = sizeof(struct ovs_header), 2271 .name = OVS_CT_LIMIT_FAMILY, 2272 .version = OVS_CT_LIMIT_VERSION, 2273 .maxattr = OVS_CT_LIMIT_ATTR_MAX, 2274 .policy = ct_limit_policy, 2275 .netnsok = true, 2276 .parallel_ops = true, 2277 .small_ops = ct_limit_genl_ops, 2278 .n_small_ops = ARRAY_SIZE(ct_limit_genl_ops), 2279 .mcgrps = &ovs_ct_limit_multicast_group, 2280 .n_mcgrps = 1, 2281 .module = THIS_MODULE, 2282 }; 2283 #endif 2284 2285 int ovs_ct_init(struct net *net) 2286 { 2287 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; 2288 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2289 2290 if (nf_connlabels_get(net, n_bits - 1)) { 2291 ovs_net->xt_label = false; 2292 OVS_NLERR(true, "Failed to set connlabel length"); 2293 } else { 2294 ovs_net->xt_label = true; 2295 } 2296 2297 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 2298 return ovs_ct_limit_init(net, ovs_net); 2299 #else 2300 return 0; 2301 #endif 2302 } 2303 2304 void ovs_ct_exit(struct net *net) 2305 { 2306 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2307 2308 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 2309 ovs_ct_limit_exit(net, ovs_net); 2310 #endif 2311 2312 if (ovs_net->xt_label) 2313 nf_connlabels_put(net); 2314 } 2315