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 <net/ip.h> 17 #include <net/netfilter/nf_conntrack_core.h> 18 #include <net/netfilter/nf_conntrack_helper.h> 19 #include <net/netfilter/nf_conntrack_labels.h> 20 #include <net/netfilter/nf_conntrack_zones.h> 21 #include <net/netfilter/ipv6/nf_defrag_ipv6.h> 22 23 #include "datapath.h" 24 #include "conntrack.h" 25 #include "flow.h" 26 #include "flow_netlink.h" 27 28 struct ovs_ct_len_tbl { 29 size_t maxlen; 30 size_t minlen; 31 }; 32 33 /* Metadata mark for masked write to conntrack mark */ 34 struct md_mark { 35 u32 value; 36 u32 mask; 37 }; 38 39 /* Metadata label for masked write to conntrack label. */ 40 struct md_labels { 41 struct ovs_key_ct_labels value; 42 struct ovs_key_ct_labels mask; 43 }; 44 45 /* Conntrack action context for execution. */ 46 struct ovs_conntrack_info { 47 struct nf_conntrack_helper *helper; 48 struct nf_conntrack_zone zone; 49 struct nf_conn *ct; 50 u8 commit : 1; 51 u16 family; 52 struct md_mark mark; 53 struct md_labels labels; 54 }; 55 56 static u16 key_to_nfproto(const struct sw_flow_key *key) 57 { 58 switch (ntohs(key->eth.type)) { 59 case ETH_P_IP: 60 return NFPROTO_IPV4; 61 case ETH_P_IPV6: 62 return NFPROTO_IPV6; 63 default: 64 return NFPROTO_UNSPEC; 65 } 66 } 67 68 /* Map SKB connection state into the values used by flow definition. */ 69 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) 70 { 71 u8 ct_state = OVS_CS_F_TRACKED; 72 73 switch (ctinfo) { 74 case IP_CT_ESTABLISHED_REPLY: 75 case IP_CT_RELATED_REPLY: 76 case IP_CT_NEW_REPLY: 77 ct_state |= OVS_CS_F_REPLY_DIR; 78 break; 79 default: 80 break; 81 } 82 83 switch (ctinfo) { 84 case IP_CT_ESTABLISHED: 85 case IP_CT_ESTABLISHED_REPLY: 86 ct_state |= OVS_CS_F_ESTABLISHED; 87 break; 88 case IP_CT_RELATED: 89 case IP_CT_RELATED_REPLY: 90 ct_state |= OVS_CS_F_RELATED; 91 break; 92 case IP_CT_NEW: 93 case IP_CT_NEW_REPLY: 94 ct_state |= OVS_CS_F_NEW; 95 break; 96 default: 97 break; 98 } 99 100 return ct_state; 101 } 102 103 static u32 ovs_ct_get_mark(const struct nf_conn *ct) 104 { 105 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 106 return ct ? ct->mark : 0; 107 #else 108 return 0; 109 #endif 110 } 111 112 static void ovs_ct_get_labels(const struct nf_conn *ct, 113 struct ovs_key_ct_labels *labels) 114 { 115 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; 116 117 if (cl) { 118 size_t len = cl->words * sizeof(long); 119 120 if (len > OVS_CT_LABELS_LEN) 121 len = OVS_CT_LABELS_LEN; 122 else if (len < OVS_CT_LABELS_LEN) 123 memset(labels, 0, OVS_CT_LABELS_LEN); 124 memcpy(labels, cl->bits, len); 125 } else { 126 memset(labels, 0, OVS_CT_LABELS_LEN); 127 } 128 } 129 130 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, 131 const struct nf_conntrack_zone *zone, 132 const struct nf_conn *ct) 133 { 134 key->ct.state = state; 135 key->ct.zone = zone->id; 136 key->ct.mark = ovs_ct_get_mark(ct); 137 ovs_ct_get_labels(ct, &key->ct.labels); 138 } 139 140 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has 141 * previously sent the packet to conntrack via the ct action. 142 */ 143 static void ovs_ct_update_key(const struct sk_buff *skb, 144 struct sw_flow_key *key, bool post_ct) 145 { 146 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; 147 enum ip_conntrack_info ctinfo; 148 struct nf_conn *ct; 149 u8 state = 0; 150 151 ct = nf_ct_get(skb, &ctinfo); 152 if (ct) { 153 state = ovs_ct_get_state(ctinfo); 154 if (!nf_ct_is_confirmed(ct)) 155 state |= OVS_CS_F_NEW; 156 if (ct->master) 157 state |= OVS_CS_F_RELATED; 158 zone = nf_ct_zone(ct); 159 } else if (post_ct) { 160 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; 161 } 162 __ovs_ct_update_key(key, state, zone, ct); 163 } 164 165 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key) 166 { 167 ovs_ct_update_key(skb, key, false); 168 } 169 170 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb) 171 { 172 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state)) 173 return -EMSGSIZE; 174 175 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 176 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone)) 177 return -EMSGSIZE; 178 179 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 180 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark)) 181 return -EMSGSIZE; 182 183 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 184 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels), 185 &key->ct.labels)) 186 return -EMSGSIZE; 187 188 return 0; 189 } 190 191 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key, 192 u32 ct_mark, u32 mask) 193 { 194 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 195 enum ip_conntrack_info ctinfo; 196 struct nf_conn *ct; 197 u32 new_mark; 198 199 200 /* The connection could be invalid, in which case set_mark is no-op. */ 201 ct = nf_ct_get(skb, &ctinfo); 202 if (!ct) 203 return 0; 204 205 new_mark = ct_mark | (ct->mark & ~(mask)); 206 if (ct->mark != new_mark) { 207 ct->mark = new_mark; 208 nf_conntrack_event_cache(IPCT_MARK, ct); 209 key->ct.mark = new_mark; 210 } 211 212 return 0; 213 #else 214 return -ENOTSUPP; 215 #endif 216 } 217 218 static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key, 219 const struct ovs_key_ct_labels *labels, 220 const struct ovs_key_ct_labels *mask) 221 { 222 enum ip_conntrack_info ctinfo; 223 struct nf_conn_labels *cl; 224 struct nf_conn *ct; 225 int err; 226 227 /* The connection could be invalid, in which case set_label is no-op.*/ 228 ct = nf_ct_get(skb, &ctinfo); 229 if (!ct) 230 return 0; 231 232 cl = nf_ct_labels_find(ct); 233 if (!cl) { 234 nf_ct_labels_ext_add(ct); 235 cl = nf_ct_labels_find(ct); 236 } 237 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN) 238 return -ENOSPC; 239 240 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask, 241 OVS_CT_LABELS_LEN / sizeof(u32)); 242 if (err) 243 return err; 244 245 ovs_ct_get_labels(ct, &key->ct.labels); 246 return 0; 247 } 248 249 /* 'skb' should already be pulled to nh_ofs. */ 250 static int ovs_ct_helper(struct sk_buff *skb, u16 proto) 251 { 252 const struct nf_conntrack_helper *helper; 253 const struct nf_conn_help *help; 254 enum ip_conntrack_info ctinfo; 255 unsigned int protoff; 256 struct nf_conn *ct; 257 258 ct = nf_ct_get(skb, &ctinfo); 259 if (!ct || ctinfo == IP_CT_RELATED_REPLY) 260 return NF_ACCEPT; 261 262 help = nfct_help(ct); 263 if (!help) 264 return NF_ACCEPT; 265 266 helper = rcu_dereference(help->helper); 267 if (!helper) 268 return NF_ACCEPT; 269 270 switch (proto) { 271 case NFPROTO_IPV4: 272 protoff = ip_hdrlen(skb); 273 break; 274 case NFPROTO_IPV6: { 275 u8 nexthdr = ipv6_hdr(skb)->nexthdr; 276 __be16 frag_off; 277 int ofs; 278 279 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, 280 &frag_off); 281 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { 282 pr_debug("proto header not found\n"); 283 return NF_ACCEPT; 284 } 285 protoff = ofs; 286 break; 287 } 288 default: 289 WARN_ONCE(1, "helper invoked on non-IP family!"); 290 return NF_DROP; 291 } 292 293 return helper->help(skb, protoff, ct, ctinfo); 294 } 295 296 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero 297 * value if 'skb' is freed. 298 */ 299 static int handle_fragments(struct net *net, struct sw_flow_key *key, 300 u16 zone, struct sk_buff *skb) 301 { 302 struct ovs_skb_cb ovs_cb = *OVS_CB(skb); 303 int err; 304 305 if (key->eth.type == htons(ETH_P_IP)) { 306 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; 307 308 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 309 err = ip_defrag(net, skb, user); 310 if (err) 311 return err; 312 313 ovs_cb.mru = IPCB(skb)->frag_max_size; 314 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 315 } else if (key->eth.type == htons(ETH_P_IPV6)) { 316 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; 317 318 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 319 err = nf_ct_frag6_gather(net, skb, user); 320 if (err) 321 return err; 322 323 key->ip.proto = ipv6_hdr(skb)->nexthdr; 324 ovs_cb.mru = IP6CB(skb)->frag_max_size; 325 #endif 326 } else { 327 kfree_skb(skb); 328 return -EPFNOSUPPORT; 329 } 330 331 key->ip.frag = OVS_FRAG_TYPE_NONE; 332 skb_clear_hash(skb); 333 skb->ignore_df = 1; 334 *OVS_CB(skb) = ovs_cb; 335 336 return 0; 337 } 338 339 static struct nf_conntrack_expect * 340 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, 341 u16 proto, const struct sk_buff *skb) 342 { 343 struct nf_conntrack_tuple tuple; 344 345 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) 346 return NULL; 347 return __nf_ct_expect_find(net, zone, &tuple); 348 } 349 350 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */ 351 static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb, 352 const struct ovs_conntrack_info *info) 353 { 354 enum ip_conntrack_info ctinfo; 355 struct nf_conn *ct; 356 357 ct = nf_ct_get(skb, &ctinfo); 358 if (!ct) 359 return false; 360 if (!net_eq(net, read_pnet(&ct->ct_net))) 361 return false; 362 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) 363 return false; 364 if (info->helper) { 365 struct nf_conn_help *help; 366 367 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); 368 if (help && rcu_access_pointer(help->helper) != info->helper) 369 return false; 370 } 371 372 return true; 373 } 374 375 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, 376 const struct ovs_conntrack_info *info, 377 struct sk_buff *skb) 378 { 379 /* If we are recirculating packets to match on conntrack fields and 380 * committing with a separate conntrack action, then we don't need to 381 * actually run the packet through conntrack twice unless it's for a 382 * different zone. 383 */ 384 if (!skb_nfct_cached(net, skb, info)) { 385 struct nf_conn *tmpl = info->ct; 386 387 /* Associate skb with specified zone. */ 388 if (tmpl) { 389 if (skb->nfct) 390 nf_conntrack_put(skb->nfct); 391 nf_conntrack_get(&tmpl->ct_general); 392 skb->nfct = &tmpl->ct_general; 393 skb->nfctinfo = IP_CT_NEW; 394 } 395 396 if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING, 397 skb) != NF_ACCEPT) 398 return -ENOENT; 399 400 if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) { 401 WARN_ONCE(1, "helper rejected packet"); 402 return -EINVAL; 403 } 404 } 405 406 ovs_ct_update_key(skb, key, true); 407 408 return 0; 409 } 410 411 /* Lookup connection and read fields into key. */ 412 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, 413 const struct ovs_conntrack_info *info, 414 struct sk_buff *skb) 415 { 416 struct nf_conntrack_expect *exp; 417 418 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); 419 if (exp) { 420 u8 state; 421 422 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; 423 __ovs_ct_update_key(key, state, &info->zone, exp->master); 424 } else { 425 int err; 426 427 err = __ovs_ct_lookup(net, key, info, skb); 428 if (err) 429 return err; 430 } 431 432 return 0; 433 } 434 435 /* Lookup connection and confirm if unconfirmed. */ 436 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, 437 const struct ovs_conntrack_info *info, 438 struct sk_buff *skb) 439 { 440 u8 state; 441 int err; 442 443 state = key->ct.state; 444 if (key->ct.zone == info->zone.id && 445 ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) { 446 /* Previous lookup has shown that this connection is already 447 * tracked and committed. Skip committing. 448 */ 449 return 0; 450 } 451 452 err = __ovs_ct_lookup(net, key, info, skb); 453 if (err) 454 return err; 455 if (nf_conntrack_confirm(skb) != NF_ACCEPT) 456 return -EINVAL; 457 458 return 0; 459 } 460 461 static bool labels_nonzero(const struct ovs_key_ct_labels *labels) 462 { 463 size_t i; 464 465 for (i = 0; i < sizeof(*labels); i++) 466 if (labels->ct_labels[i]) 467 return true; 468 469 return false; 470 } 471 472 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero 473 * value if 'skb' is freed. 474 */ 475 int ovs_ct_execute(struct net *net, struct sk_buff *skb, 476 struct sw_flow_key *key, 477 const struct ovs_conntrack_info *info) 478 { 479 int nh_ofs; 480 int err; 481 482 /* The conntrack module expects to be working at L3. */ 483 nh_ofs = skb_network_offset(skb); 484 skb_pull(skb, nh_ofs); 485 486 if (key->ip.frag != OVS_FRAG_TYPE_NONE) { 487 err = handle_fragments(net, key, info->zone.id, skb); 488 if (err) 489 return err; 490 } 491 492 if (info->commit) 493 err = ovs_ct_commit(net, key, info, skb); 494 else 495 err = ovs_ct_lookup(net, key, info, skb); 496 if (err) 497 goto err; 498 499 if (info->mark.mask) { 500 err = ovs_ct_set_mark(skb, key, info->mark.value, 501 info->mark.mask); 502 if (err) 503 goto err; 504 } 505 if (labels_nonzero(&info->labels.mask)) 506 err = ovs_ct_set_labels(skb, key, &info->labels.value, 507 &info->labels.mask); 508 err: 509 skb_push(skb, nh_ofs); 510 if (err) 511 kfree_skb(skb); 512 return err; 513 } 514 515 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, 516 const struct sw_flow_key *key, bool log) 517 { 518 struct nf_conntrack_helper *helper; 519 struct nf_conn_help *help; 520 521 helper = nf_conntrack_helper_try_module_get(name, info->family, 522 key->ip.proto); 523 if (!helper) { 524 OVS_NLERR(log, "Unknown helper \"%s\"", name); 525 return -EINVAL; 526 } 527 528 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); 529 if (!help) { 530 module_put(helper->me); 531 return -ENOMEM; 532 } 533 534 rcu_assign_pointer(help->helper, helper); 535 info->helper = helper; 536 return 0; 537 } 538 539 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { 540 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, 541 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), 542 .maxlen = sizeof(u16) }, 543 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), 544 .maxlen = sizeof(struct md_mark) }, 545 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), 546 .maxlen = sizeof(struct md_labels) }, 547 [OVS_CT_ATTR_HELPER] = { .minlen = 1, 548 .maxlen = NF_CT_HELPER_NAME_LEN } 549 }; 550 551 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, 552 const char **helper, bool log) 553 { 554 struct nlattr *a; 555 int rem; 556 557 nla_for_each_nested(a, attr, rem) { 558 int type = nla_type(a); 559 int maxlen = ovs_ct_attr_lens[type].maxlen; 560 int minlen = ovs_ct_attr_lens[type].minlen; 561 562 if (type > OVS_CT_ATTR_MAX) { 563 OVS_NLERR(log, 564 "Unknown conntrack attr (type=%d, max=%d)", 565 type, OVS_CT_ATTR_MAX); 566 return -EINVAL; 567 } 568 if (nla_len(a) < minlen || nla_len(a) > maxlen) { 569 OVS_NLERR(log, 570 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", 571 type, nla_len(a), maxlen); 572 return -EINVAL; 573 } 574 575 switch (type) { 576 case OVS_CT_ATTR_COMMIT: 577 info->commit = true; 578 break; 579 #ifdef CONFIG_NF_CONNTRACK_ZONES 580 case OVS_CT_ATTR_ZONE: 581 info->zone.id = nla_get_u16(a); 582 break; 583 #endif 584 #ifdef CONFIG_NF_CONNTRACK_MARK 585 case OVS_CT_ATTR_MARK: { 586 struct md_mark *mark = nla_data(a); 587 588 if (!mark->mask) { 589 OVS_NLERR(log, "ct_mark mask cannot be 0"); 590 return -EINVAL; 591 } 592 info->mark = *mark; 593 break; 594 } 595 #endif 596 #ifdef CONFIG_NF_CONNTRACK_LABELS 597 case OVS_CT_ATTR_LABELS: { 598 struct md_labels *labels = nla_data(a); 599 600 if (!labels_nonzero(&labels->mask)) { 601 OVS_NLERR(log, "ct_labels mask cannot be 0"); 602 return -EINVAL; 603 } 604 info->labels = *labels; 605 break; 606 } 607 #endif 608 case OVS_CT_ATTR_HELPER: 609 *helper = nla_data(a); 610 if (!memchr(*helper, '\0', nla_len(a))) { 611 OVS_NLERR(log, "Invalid conntrack helper"); 612 return -EINVAL; 613 } 614 break; 615 default: 616 OVS_NLERR(log, "Unknown conntrack attr (%d)", 617 type); 618 return -EINVAL; 619 } 620 } 621 622 if (rem > 0) { 623 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); 624 return -EINVAL; 625 } 626 627 return 0; 628 } 629 630 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) 631 { 632 if (attr == OVS_KEY_ATTR_CT_STATE) 633 return true; 634 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 635 attr == OVS_KEY_ATTR_CT_ZONE) 636 return true; 637 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 638 attr == OVS_KEY_ATTR_CT_MARK) 639 return true; 640 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 641 attr == OVS_KEY_ATTR_CT_LABELS) { 642 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 643 644 return ovs_net->xt_label; 645 } 646 647 return false; 648 } 649 650 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, 651 const struct sw_flow_key *key, 652 struct sw_flow_actions **sfa, bool log) 653 { 654 struct ovs_conntrack_info ct_info; 655 const char *helper = NULL; 656 u16 family; 657 int err; 658 659 family = key_to_nfproto(key); 660 if (family == NFPROTO_UNSPEC) { 661 OVS_NLERR(log, "ct family unspecified"); 662 return -EINVAL; 663 } 664 665 memset(&ct_info, 0, sizeof(ct_info)); 666 ct_info.family = family; 667 668 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, 669 NF_CT_DEFAULT_ZONE_DIR, 0); 670 671 err = parse_ct(attr, &ct_info, &helper, log); 672 if (err) 673 return err; 674 675 /* Set up template for tracking connections in specific zones. */ 676 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); 677 if (!ct_info.ct) { 678 OVS_NLERR(log, "Failed to allocate conntrack template"); 679 return -ENOMEM; 680 } 681 if (helper) { 682 err = ovs_ct_add_helper(&ct_info, helper, key, log); 683 if (err) 684 goto err_free_ct; 685 } 686 687 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, 688 sizeof(ct_info), log); 689 if (err) 690 goto err_free_ct; 691 692 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); 693 nf_conntrack_get(&ct_info.ct->ct_general); 694 return 0; 695 err_free_ct: 696 nf_conntrack_free(ct_info.ct); 697 return err; 698 } 699 700 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, 701 struct sk_buff *skb) 702 { 703 struct nlattr *start; 704 705 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); 706 if (!start) 707 return -EMSGSIZE; 708 709 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT)) 710 return -EMSGSIZE; 711 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 712 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) 713 return -EMSGSIZE; 714 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && 715 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), 716 &ct_info->mark)) 717 return -EMSGSIZE; 718 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 719 labels_nonzero(&ct_info->labels.mask) && 720 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), 721 &ct_info->labels)) 722 return -EMSGSIZE; 723 if (ct_info->helper) { 724 if (nla_put_string(skb, OVS_CT_ATTR_HELPER, 725 ct_info->helper->name)) 726 return -EMSGSIZE; 727 } 728 729 nla_nest_end(skb, start); 730 731 return 0; 732 } 733 734 void ovs_ct_free_action(const struct nlattr *a) 735 { 736 struct ovs_conntrack_info *ct_info = nla_data(a); 737 738 if (ct_info->helper) 739 module_put(ct_info->helper->me); 740 if (ct_info->ct) 741 nf_ct_put(ct_info->ct); 742 } 743 744 void ovs_ct_init(struct net *net) 745 { 746 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; 747 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 748 749 if (nf_connlabels_get(net, n_bits)) { 750 ovs_net->xt_label = false; 751 OVS_NLERR(true, "Failed to set connlabel length"); 752 } else { 753 ovs_net->xt_label = true; 754 } 755 } 756 757 void ovs_ct_exit(struct net *net) 758 { 759 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 760 761 if (ovs_net->xt_label) 762 nf_connlabels_put(net); 763 } 764