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