1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 24 */ 25 26 #include <stdio.h> 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <string.h> 30 #include <fcntl.h> 31 #include <unistd.h> 32 #include <stropts.h> 33 #include <stdlib.h> 34 #include <errno.h> 35 #include <strings.h> 36 #include <libintl.h> 37 #include <net/if_types.h> 38 #include <net/if_dl.h> 39 #include <sys/dld.h> 40 #include <libdladm_impl.h> 41 #include <libvrrpadm.h> 42 #include <libdllink.h> 43 #include <libdlbridge.h> 44 #include <libdlvnic.h> 45 46 /* 47 * VNIC administration library. 48 */ 49 50 /* 51 * Default random MAC address prefix (locally administered). 52 */ 53 static char dladm_vnic_def_prefix[] = {0x02, 0x08, 0x20}; 54 55 static dladm_status_t dladm_vnic_persist_conf(dladm_handle_t, 56 const char *name, dladm_vnic_attr_t *, 57 datalink_class_t); 58 static const char *dladm_vnic_macaddr2str(const uchar_t *, char *); 59 static dladm_status_t dladm_vnic_str2macaddr(const char *, uchar_t *); 60 61 /* 62 * Convert a diagnostic returned by the kernel into a dladm_status_t. 63 */ 64 static dladm_status_t 65 dladm_vnic_diag2status(vnic_ioc_diag_t ioc_diag) 66 { 67 switch (ioc_diag) { 68 case VNIC_IOC_DIAG_NONE: 69 return (DLADM_STATUS_OK); 70 case VNIC_IOC_DIAG_MACADDRLEN_INVALID: 71 return (DLADM_STATUS_INVALIDMACADDRLEN); 72 case VNIC_IOC_DIAG_MACADDR_NIC: 73 return (DLADM_STATUS_INVALIDMACADDRNIC); 74 case VNIC_IOC_DIAG_MACADDR_INUSE: 75 return (DLADM_STATUS_INVALIDMACADDRINUSE); 76 case VNIC_IOC_DIAG_MACFACTORYSLOTINVALID: 77 return (DLADM_STATUS_MACFACTORYSLOTINVALID); 78 case VNIC_IOC_DIAG_MACFACTORYSLOTUSED: 79 return (DLADM_STATUS_MACFACTORYSLOTUSED); 80 case VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED: 81 return (DLADM_STATUS_MACFACTORYSLOTALLUSED); 82 case VNIC_IOC_DIAG_MACFACTORYNOTSUP: 83 return (DLADM_STATUS_MACFACTORYNOTSUP); 84 case VNIC_IOC_DIAG_MACPREFIX_INVALID: 85 return (DLADM_STATUS_INVALIDMACPREFIX); 86 case VNIC_IOC_DIAG_MACPREFIXLEN_INVALID: 87 return (DLADM_STATUS_INVALIDMACPREFIXLEN); 88 case VNIC_IOC_DIAG_MACMARGIN_INVALID: 89 return (DLADM_STATUS_INVALID_MACMARGIN); 90 case VNIC_IOC_DIAG_NO_HWRINGS: 91 return (DLADM_STATUS_NO_HWRINGS); 92 case VNIC_IOC_DIAG_MACADDR_INVALID: 93 return (DLADM_STATUS_INVALIDMACADDR); 94 case VNIC_IOC_DIAG_MACMTU_INVALID: 95 return (DLADM_STATUS_INVALID_MTU); 96 default: 97 return (DLADM_STATUS_FAILED); 98 } 99 } 100 101 /* 102 * Send a create command to the VNIC driver. 103 */ 104 dladm_status_t 105 i_dladm_vnic_create_sys(dladm_handle_t handle, dladm_vnic_attr_t *attr) 106 { 107 int rc; 108 vnic_ioc_create_t ioc; 109 dladm_status_t status = DLADM_STATUS_OK; 110 111 bzero(&ioc, sizeof (ioc)); 112 ioc.vc_vnic_id = attr->va_vnic_id; 113 ioc.vc_link_id = attr->va_link_id; 114 ioc.vc_mac_addr_type = attr->va_mac_addr_type; 115 ioc.vc_mac_len = attr->va_mac_len; 116 ioc.vc_mac_slot = attr->va_mac_slot; 117 ioc.vc_mac_prefix_len = attr->va_mac_prefix_len; 118 ioc.vc_vid = attr->va_vid; 119 ioc.vc_vrid = attr->va_vrid; 120 ioc.vc_af = attr->va_af; 121 ioc.vc_flags = attr->va_force ? VNIC_IOC_CREATE_FORCE : 0; 122 123 if (attr->va_mac_len > 0 || ioc.vc_mac_prefix_len > 0) 124 bcopy(attr->va_mac_addr, ioc.vc_mac_addr, MAXMACADDRLEN); 125 bcopy(&attr->va_resource_props, &ioc.vc_resource_props, 126 sizeof (mac_resource_props_t)); 127 if (attr->va_link_id == DATALINK_INVALID_LINKID) 128 ioc.vc_flags |= VNIC_IOC_CREATE_ANCHOR; 129 130 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_CREATE, &ioc); 131 if (rc < 0) 132 status = dladm_errno2status(errno); 133 134 if (status != DLADM_STATUS_OK) { 135 if (ioc.vc_diag != VNIC_IOC_DIAG_NONE) 136 status = dladm_vnic_diag2status(ioc.vc_diag); 137 } 138 if (status != DLADM_STATUS_OK) 139 return (status); 140 141 attr->va_mac_addr_type = ioc.vc_mac_addr_type; 142 switch (ioc.vc_mac_addr_type) { 143 case VNIC_MAC_ADDR_TYPE_FACTORY: 144 attr->va_mac_slot = ioc.vc_mac_slot; 145 break; 146 case VNIC_MAC_ADDR_TYPE_RANDOM: 147 bcopy(ioc.vc_mac_addr, attr->va_mac_addr, MAXMACADDRLEN); 148 attr->va_mac_len = ioc.vc_mac_len; 149 break; 150 } 151 return (status); 152 } 153 154 /* 155 * Get the configuration information of the given VNIC. 156 */ 157 static dladm_status_t 158 i_dladm_vnic_info_active(dladm_handle_t handle, datalink_id_t linkid, 159 dladm_vnic_attr_t *attrp) 160 { 161 vnic_ioc_info_t ioc; 162 vnic_info_t *vnic; 163 int rc; 164 dladm_status_t status = DLADM_STATUS_OK; 165 166 bzero(&ioc, sizeof (ioc)); 167 vnic = &ioc.vi_info; 168 vnic->vn_vnic_id = linkid; 169 170 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_INFO, &ioc); 171 if (rc != 0) { 172 status = dladm_errno2status(errno); 173 goto bail; 174 } 175 176 attrp->va_vnic_id = vnic->vn_vnic_id; 177 attrp->va_link_id = vnic->vn_link_id; 178 attrp->va_mac_addr_type = vnic->vn_mac_addr_type; 179 bcopy(vnic->vn_mac_addr, attrp->va_mac_addr, MAXMACADDRLEN); 180 attrp->va_mac_len = vnic->vn_mac_len; 181 attrp->va_mac_slot = vnic->vn_mac_slot; 182 attrp->va_mac_prefix_len = vnic->vn_mac_prefix_len; 183 attrp->va_vid = vnic->vn_vid; 184 attrp->va_vrid = vnic->vn_vrid; 185 attrp->va_af = vnic->vn_af; 186 attrp->va_force = vnic->vn_force; 187 188 bail: 189 return (status); 190 } 191 192 static dladm_status_t 193 i_dladm_vnic_info_persist(dladm_handle_t handle, datalink_id_t linkid, 194 dladm_vnic_attr_t *attrp) 195 { 196 dladm_conf_t conf; 197 dladm_status_t status; 198 char macstr[ETHERADDRL * 3]; 199 char linkover[MAXLINKNAMELEN]; 200 uint64_t u64; 201 datalink_class_t class; 202 203 attrp->va_vnic_id = linkid; 204 if ((status = dladm_getsnap_conf(handle, linkid, &conf)) != 205 DLADM_STATUS_OK) 206 return (status); 207 208 status = dladm_get_conf_field(handle, conf, FLINKOVER, linkover, 209 sizeof (linkover)); 210 if (status != DLADM_STATUS_OK) { 211 /* 212 * This isn't an error, etherstubs don't have a FLINKOVER 213 * property. 214 */ 215 attrp->va_link_id = DATALINK_INVALID_LINKID; 216 } else { 217 if ((status = dladm_name2info(handle, linkover, 218 &attrp->va_link_id, NULL, NULL, NULL)) != DLADM_STATUS_OK) 219 goto done; 220 } 221 222 if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class, 223 NULL, NULL, 0)) != DLADM_STATUS_OK) 224 goto done; 225 226 if (class == DATALINK_CLASS_VLAN) { 227 if (attrp->va_link_id == DATALINK_INVALID_LINKID) { 228 status = DLADM_STATUS_BADARG; 229 goto done; 230 } 231 attrp->va_mac_addr_type = VNIC_MAC_ADDR_TYPE_PRIMARY; 232 attrp->va_mac_len = 0; 233 } else { 234 status = dladm_get_conf_field(handle, conf, FMADDRTYPE, &u64, 235 sizeof (u64)); 236 if (status != DLADM_STATUS_OK) 237 goto done; 238 239 attrp->va_mac_addr_type = (vnic_mac_addr_type_t)u64; 240 241 if ((status = dladm_get_conf_field(handle, conf, FVRID, 242 &u64, sizeof (u64))) != DLADM_STATUS_OK) { 243 attrp->va_vrid = VRRP_VRID_NONE; 244 } else { 245 attrp->va_vrid = (vrid_t)u64; 246 } 247 248 if ((status = dladm_get_conf_field(handle, conf, FVRAF, 249 &u64, sizeof (u64))) != DLADM_STATUS_OK) { 250 attrp->va_af = AF_UNSPEC; 251 } else { 252 attrp->va_af = (int)u64; 253 } 254 255 status = dladm_get_conf_field(handle, conf, FMADDRLEN, &u64, 256 sizeof (u64)); 257 attrp->va_mac_len = ((status == DLADM_STATUS_OK) ? 258 (uint_t)u64 : ETHERADDRL); 259 260 status = dladm_get_conf_field(handle, conf, FMADDRSLOT, &u64, 261 sizeof (u64)); 262 attrp->va_mac_slot = ((status == DLADM_STATUS_OK) ? 263 (int)u64 : -1); 264 265 status = dladm_get_conf_field(handle, conf, FMADDRPREFIXLEN, 266 &u64, sizeof (u64)); 267 attrp->va_mac_prefix_len = ((status == DLADM_STATUS_OK) ? 268 (uint_t)u64 : sizeof (dladm_vnic_def_prefix)); 269 270 status = dladm_get_conf_field(handle, conf, FMACADDR, macstr, 271 sizeof (macstr)); 272 if (status != DLADM_STATUS_OK) 273 goto done; 274 275 status = dladm_vnic_str2macaddr(macstr, attrp->va_mac_addr); 276 if (status != DLADM_STATUS_OK) 277 goto done; 278 } 279 280 status = dladm_get_conf_field(handle, conf, FVLANID, &u64, 281 sizeof (u64)); 282 attrp->va_vid = ((status == DLADM_STATUS_OK) ? (uint16_t)u64 : 0); 283 284 status = DLADM_STATUS_OK; 285 done: 286 dladm_destroy_conf(handle, conf); 287 return (status); 288 } 289 290 dladm_status_t 291 dladm_vnic_info(dladm_handle_t handle, datalink_id_t linkid, 292 dladm_vnic_attr_t *attrp, uint32_t flags) 293 { 294 if (flags == DLADM_OPT_ACTIVE) 295 return (i_dladm_vnic_info_active(handle, linkid, attrp)); 296 else if (flags == DLADM_OPT_PERSIST) 297 return (i_dladm_vnic_info_persist(handle, linkid, attrp)); 298 else 299 return (DLADM_STATUS_BADARG); 300 } 301 302 /* 303 * Remove a VNIC from the kernel. 304 */ 305 dladm_status_t 306 i_dladm_vnic_delete_sys(dladm_handle_t handle, datalink_id_t linkid) 307 { 308 vnic_ioc_delete_t ioc; 309 dladm_status_t status = DLADM_STATUS_OK; 310 int rc; 311 312 ioc.vd_vnic_id = linkid; 313 314 rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_DELETE, &ioc); 315 if (rc < 0) 316 status = dladm_errno2status(errno); 317 318 return (status); 319 } 320 321 /* 322 * Convert between MAC address types and their string representations. 323 */ 324 325 typedef struct dladm_vnic_addr_type_s { 326 const char *va_str; 327 vnic_mac_addr_type_t va_type; 328 } dladm_vnic_addr_type_t; 329 330 static dladm_vnic_addr_type_t addr_types[] = { 331 {"fixed", VNIC_MAC_ADDR_TYPE_FIXED}, 332 {"random", VNIC_MAC_ADDR_TYPE_RANDOM}, 333 {"factory", VNIC_MAC_ADDR_TYPE_FACTORY}, 334 {"auto", VNIC_MAC_ADDR_TYPE_AUTO}, 335 {"fixed", VNIC_MAC_ADDR_TYPE_PRIMARY}, 336 {"vrrp", VNIC_MAC_ADDR_TYPE_VRID} 337 }; 338 339 #define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t)) 340 341 static const char * 342 dladm_vnic_macaddrtype2str(vnic_mac_addr_type_t type) 343 { 344 int i; 345 346 for (i = 0; i < NADDR_TYPES; i++) { 347 if (type == addr_types[i].va_type) 348 return (addr_types[i].va_str); 349 } 350 return (NULL); 351 } 352 353 dladm_status_t 354 dladm_vnic_str2macaddrtype(const char *str, vnic_mac_addr_type_t *val) 355 { 356 int i; 357 dladm_vnic_addr_type_t *type; 358 359 for (i = 0; i < NADDR_TYPES; i++) { 360 type = &addr_types[i]; 361 if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) { 362 *val = type->va_type; 363 return (DLADM_STATUS_OK); 364 } 365 } 366 return (DLADM_STATUS_BADARG); 367 } 368 369 /* 370 * Based on the VRRP specification, the virtual router MAC address associated 371 * with a virtual router is an IEEE 802 MAC address in the following format: 372 * 373 * IPv4 case: 00-00-5E-00-01-{VRID} (in hex in internet standard bit-order) 374 * 375 * IPv6 case: 00-00-5E-00-02-{VRID} (in hex in internet standard bit-order) 376 */ 377 static dladm_status_t 378 i_dladm_vnic_vrrp_mac(vrid_t vrid, int af, uint8_t *mac, uint_t maclen) 379 { 380 if (maclen < ETHERADDRL || vrid < VRRP_VRID_MIN || 381 vrid > VRRP_VRID_MAX || (af != AF_INET && af != AF_INET6)) { 382 return (DLADM_STATUS_BADARG); 383 } 384 385 mac[0] = mac[1] = mac[3] = 0x0; 386 mac[2] = 0x5e; 387 mac[4] = (af == AF_INET) ? 0x01 : 0x02; 388 mac[5] = vrid; 389 return (DLADM_STATUS_OK); 390 } 391 392 /* 393 * Create a new VNIC / VLAN. Update the configuration file and bring it up. 394 * The "vrid" and "af" arguments are only required if the mac_addr_type is 395 * VNIC_MAC_ADDR_TYPE_VRID. In that case, the MAC address will be caculated 396 * based on the above algorithm. 397 */ 398 dladm_status_t 399 dladm_vnic_create(dladm_handle_t handle, const char *vnic, datalink_id_t linkid, 400 vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, uint_t mac_len, 401 int *mac_slot, uint_t mac_prefix_len, uint16_t vid, vrid_t vrid, 402 int af, datalink_id_t *vnic_id_out, dladm_arg_list_t *proplist, 403 uint32_t flags) 404 { 405 dladm_vnic_attr_t attr; 406 datalink_id_t vnic_id; 407 datalink_class_t class; 408 uint32_t media = DL_ETHER; 409 uint32_t link_flags; 410 char name[MAXLINKNAMELEN]; 411 uchar_t tmp_addr[MAXMACADDRLEN]; 412 dladm_status_t status; 413 boolean_t is_vlan; 414 boolean_t is_etherstub; 415 int i; 416 boolean_t vnic_created = B_FALSE; 417 boolean_t conf_set = B_FALSE; 418 419 /* 420 * Sanity test arguments. 421 */ 422 if ((flags & DLADM_OPT_ACTIVE) == 0) 423 return (DLADM_STATUS_NOTSUP); 424 425 /* 426 * It's an anchor VNIC - linkid must be set to DATALINK_INVALID_LINKID 427 * and the VLAN id must be 0 428 */ 429 if ((flags & DLADM_OPT_ANCHOR) != 0 && 430 (linkid != DATALINK_INVALID_LINKID || vid != 0)) { 431 return (DLADM_STATUS_BADARG); 432 } 433 434 is_vlan = ((flags & DLADM_OPT_VLAN) != 0); 435 if (is_vlan && ((vid < 1 || vid > 4094))) 436 return (DLADM_STATUS_VIDINVAL); 437 438 is_etherstub = (linkid == DATALINK_INVALID_LINKID); 439 440 if (!dladm_vnic_macaddrtype2str(mac_addr_type)) 441 return (DLADM_STATUS_INVALIDMACADDRTYPE); 442 443 if (!is_etherstub) { 444 if ((status = dladm_datalink_id2info(handle, linkid, 445 &link_flags, &class, &media, NULL, 0)) != DLADM_STATUS_OK) 446 return (status); 447 448 /* Disallow persistent objects on top of temporary ones */ 449 if ((flags & DLADM_OPT_PERSIST) != 0 && 450 (link_flags & DLMGMT_PERSIST) == 0) 451 return (DLADM_STATUS_PERSIST_ON_TEMP); 452 453 /* Links cannot be created on top of these object types */ 454 if (class == DATALINK_CLASS_VNIC || 455 class == DATALINK_CLASS_VLAN) 456 return (DLADM_STATUS_BADARG); 457 } 458 459 /* 460 * Only VRRP VNIC need VRID and address family specified. 461 */ 462 if (mac_addr_type != VNIC_MAC_ADDR_TYPE_VRID && 463 (af != AF_UNSPEC || vrid != VRRP_VRID_NONE)) { 464 return (DLADM_STATUS_BADARG); 465 } 466 467 /* 468 * If a random address might be generated, but no prefix 469 * was specified by the caller, use the default MAC address 470 * prefix. 471 */ 472 if ((mac_addr_type == VNIC_MAC_ADDR_TYPE_RANDOM || 473 mac_addr_type == VNIC_MAC_ADDR_TYPE_AUTO) && 474 mac_prefix_len == 0) { 475 mac_prefix_len = sizeof (dladm_vnic_def_prefix); 476 mac_addr = tmp_addr; 477 bcopy(dladm_vnic_def_prefix, mac_addr, mac_prefix_len); 478 } 479 480 /* 481 * If this is a VRRP VNIC, generate its MAC address using the given 482 * VRID and address family. 483 */ 484 if (mac_addr_type == VNIC_MAC_ADDR_TYPE_VRID) { 485 /* 486 * VRRP VNICs must be created over ethernet data-links. 487 */ 488 if (vrid < VRRP_VRID_MIN || vrid > VRRP_VRID_MAX || 489 (af != AF_INET && af != AF_INET6) || mac_addr != NULL || 490 mac_len != 0 || mac_prefix_len != 0 || 491 (mac_slot != NULL && *mac_slot != -1) || is_etherstub || 492 media != DL_ETHER) { 493 return (DLADM_STATUS_BADARG); 494 } 495 mac_len = ETHERADDRL; 496 mac_addr = tmp_addr; 497 status = i_dladm_vnic_vrrp_mac(vrid, af, mac_addr, mac_len); 498 if (status != DLADM_STATUS_OK) 499 return (status); 500 } 501 502 if (mac_len > MAXMACADDRLEN) 503 return (DLADM_STATUS_INVALIDMACADDRLEN); 504 505 if (vnic == NULL) { 506 flags |= DLADM_OPT_PREFIX; 507 (void) strlcpy(name, "vnic", sizeof (name)); 508 } else { 509 (void) strlcpy(name, vnic, sizeof (name)); 510 } 511 512 class = is_vlan ? DATALINK_CLASS_VLAN : 513 (is_etherstub ? DATALINK_CLASS_ETHERSTUB : DATALINK_CLASS_VNIC); 514 if ((status = dladm_create_datalink_id(handle, name, class, 515 media, flags, &vnic_id)) != DLADM_STATUS_OK) 516 return (status); 517 518 if ((flags & DLADM_OPT_PREFIX) != 0) { 519 (void) snprintf(name + 4, sizeof (name), "%llu", vnic_id); 520 flags &= ~DLADM_OPT_PREFIX; 521 } 522 523 bzero(&attr, sizeof (attr)); 524 525 /* Extract resource_ctl and cpu_list from proplist */ 526 if (proplist != NULL) { 527 status = dladm_link_proplist_extract(handle, proplist, 528 &attr.va_resource_props, 0); 529 if (status != DLADM_STATUS_OK) 530 goto done; 531 } 532 533 attr.va_vnic_id = vnic_id; 534 attr.va_link_id = linkid; 535 attr.va_mac_addr_type = mac_addr_type; 536 attr.va_mac_len = mac_len; 537 if (mac_slot != NULL) 538 attr.va_mac_slot = *mac_slot; 539 if (mac_len > 0) 540 bcopy(mac_addr, attr.va_mac_addr, mac_len); 541 else if (mac_prefix_len > 0) 542 bcopy(mac_addr, attr.va_mac_addr, mac_prefix_len); 543 attr.va_mac_prefix_len = mac_prefix_len; 544 attr.va_vid = vid; 545 attr.va_vrid = vrid; 546 attr.va_af = af; 547 attr.va_force = (flags & DLADM_OPT_FORCE) != 0; 548 549 status = i_dladm_vnic_create_sys(handle, &attr); 550 if (status != DLADM_STATUS_OK) 551 goto done; 552 vnic_created = B_TRUE; 553 554 /* Save vnic configuration and its properties */ 555 if (!(flags & DLADM_OPT_PERSIST)) 556 goto done; 557 558 status = dladm_vnic_persist_conf(handle, name, &attr, class); 559 if (status != DLADM_STATUS_OK) 560 goto done; 561 conf_set = B_TRUE; 562 563 if (proplist != NULL) { 564 for (i = 0; i < proplist->al_count; i++) { 565 dladm_arg_info_t *aip = &proplist->al_info[i]; 566 567 status = dladm_set_linkprop(handle, vnic_id, 568 aip->ai_name, aip->ai_val, aip->ai_count, 569 DLADM_OPT_PERSIST); 570 if (status != DLADM_STATUS_OK) 571 break; 572 } 573 } 574 575 done: 576 if (status != DLADM_STATUS_OK) { 577 if (conf_set) 578 (void) dladm_remove_conf(handle, vnic_id); 579 if (vnic_created) 580 (void) i_dladm_vnic_delete_sys(handle, vnic_id); 581 (void) dladm_destroy_datalink_id(handle, vnic_id, flags); 582 } else { 583 if (vnic_id_out != NULL) 584 *vnic_id_out = vnic_id; 585 if (mac_slot != NULL) 586 *mac_slot = attr.va_mac_slot; 587 } 588 589 if (is_vlan) { 590 dladm_status_t stat2; 591 592 stat2 = dladm_bridge_refresh(handle, linkid); 593 if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK) 594 status = stat2; 595 } 596 return (status); 597 } 598 599 /* 600 * Delete a VNIC / VLAN. 601 */ 602 dladm_status_t 603 dladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 604 { 605 dladm_status_t status; 606 datalink_class_t class; 607 608 if (flags == 0) 609 return (DLADM_STATUS_BADARG); 610 611 if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0) 612 != DLADM_STATUS_OK)) 613 return (DLADM_STATUS_BADARG); 614 615 if ((flags & DLADM_OPT_VLAN) != 0) { 616 if (class != DATALINK_CLASS_VLAN) 617 return (DLADM_STATUS_BADARG); 618 } else { 619 if (class != DATALINK_CLASS_VNIC && 620 class != DATALINK_CLASS_ETHERSTUB) 621 return (DLADM_STATUS_BADARG); 622 } 623 624 if ((flags & DLADM_OPT_ACTIVE) != 0) { 625 status = i_dladm_vnic_delete_sys(handle, linkid); 626 if (status == DLADM_STATUS_OK) { 627 (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0, 628 DLADM_OPT_ACTIVE); 629 (void) dladm_destroy_datalink_id(handle, linkid, 630 DLADM_OPT_ACTIVE); 631 } else if (status != DLADM_STATUS_NOTFOUND || 632 !(flags & DLADM_OPT_PERSIST)) { 633 return (status); 634 } 635 } 636 if ((flags & DLADM_OPT_PERSIST) != 0) { 637 (void) dladm_remove_conf(handle, linkid); 638 (void) dladm_destroy_datalink_id(handle, linkid, 639 DLADM_OPT_PERSIST); 640 } 641 return (dladm_bridge_refresh(handle, linkid)); 642 } 643 644 static const char * 645 dladm_vnic_macaddr2str(const uchar_t *mac, char *buf) 646 { 647 static char unknown_mac[] = {0, 0, 0, 0, 0, 0}; 648 649 if (buf == NULL) 650 return (NULL); 651 652 if (bcmp(unknown_mac, mac, ETHERADDRL) == 0) 653 (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 654 else 655 return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER)); 656 657 return (buf); 658 } 659 660 static dladm_status_t 661 dladm_vnic_str2macaddr(const char *str, uchar_t *buf) 662 { 663 int len = 0; 664 uchar_t *b = _link_aton(str, &len); 665 666 if (b == NULL || len >= MAXMACADDRLEN) 667 return (DLADM_STATUS_BADARG); 668 669 bcopy(b, buf, len); 670 free(b); 671 return (DLADM_STATUS_OK); 672 } 673 674 675 static dladm_status_t 676 dladm_vnic_persist_conf(dladm_handle_t handle, const char *name, 677 dladm_vnic_attr_t *attrp, datalink_class_t class) 678 { 679 dladm_conf_t conf; 680 dladm_status_t status; 681 char macstr[ETHERADDRL * 3]; 682 char linkover[MAXLINKNAMELEN]; 683 uint64_t u64; 684 685 if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id, 686 class, DL_ETHER, &conf)) != DLADM_STATUS_OK) 687 return (status); 688 689 if (attrp->va_link_id != DATALINK_INVALID_LINKID) { 690 status = dladm_datalink_id2info(handle, attrp->va_link_id, NULL, 691 NULL, NULL, linkover, sizeof (linkover)); 692 if (status != DLADM_STATUS_OK) 693 goto done; 694 status = dladm_set_conf_field(handle, conf, FLINKOVER, 695 DLADM_TYPE_STR, linkover); 696 if (status != DLADM_STATUS_OK) 697 goto done; 698 } 699 700 if (class != DATALINK_CLASS_VLAN) { 701 u64 = attrp->va_mac_addr_type; 702 status = dladm_set_conf_field(handle, conf, FMADDRTYPE, 703 DLADM_TYPE_UINT64, &u64); 704 if (status != DLADM_STATUS_OK) 705 goto done; 706 707 u64 = attrp->va_vrid; 708 status = dladm_set_conf_field(handle, conf, FVRID, 709 DLADM_TYPE_UINT64, &u64); 710 if (status != DLADM_STATUS_OK) 711 goto done; 712 713 u64 = attrp->va_af; 714 status = dladm_set_conf_field(handle, conf, FVRAF, 715 DLADM_TYPE_UINT64, &u64); 716 if (status != DLADM_STATUS_OK) 717 goto done; 718 719 if (attrp->va_mac_len != ETHERADDRL) { 720 u64 = attrp->va_mac_len; 721 status = dladm_set_conf_field(handle, conf, FMADDRLEN, 722 DLADM_TYPE_UINT64, &u64); 723 if (status != DLADM_STATUS_OK) 724 goto done; 725 } 726 727 if (attrp->va_mac_slot != -1) { 728 u64 = attrp->va_mac_slot; 729 status = dladm_set_conf_field(handle, conf, 730 FMADDRSLOT, DLADM_TYPE_UINT64, &u64); 731 if (status != DLADM_STATUS_OK) 732 goto done; 733 } 734 735 if (attrp->va_mac_prefix_len != 736 sizeof (dladm_vnic_def_prefix)) { 737 u64 = attrp->va_mac_prefix_len; 738 status = dladm_set_conf_field(handle, conf, 739 FMADDRPREFIXLEN, DLADM_TYPE_UINT64, &u64); 740 if (status != DLADM_STATUS_OK) 741 goto done; 742 } 743 744 (void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr); 745 status = dladm_set_conf_field(handle, conf, FMACADDR, 746 DLADM_TYPE_STR, macstr); 747 if (status != DLADM_STATUS_OK) 748 goto done; 749 } 750 751 if (attrp->va_vid != 0) { 752 u64 = attrp->va_vid; 753 status = dladm_set_conf_field(handle, conf, FVLANID, 754 DLADM_TYPE_UINT64, &u64); 755 if (status != DLADM_STATUS_OK) 756 goto done; 757 } 758 759 /* 760 * Commit the link configuration. 761 */ 762 status = dladm_write_conf(handle, conf); 763 764 done: 765 dladm_destroy_conf(handle, conf); 766 return (status); 767 } 768 769 typedef struct dladm_vnic_up_arg_s { 770 uint32_t flags; 771 dladm_status_t status; 772 } dladm_vnic_up_arg_t; 773 774 static int 775 i_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg) 776 { 777 dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status); 778 dladm_vnic_attr_t attr; 779 dladm_status_t status; 780 dladm_arg_list_t *proplist; 781 782 bzero(&attr, sizeof (attr)); 783 784 status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST); 785 if (status != DLADM_STATUS_OK) 786 goto done; 787 788 /* Get all properties for this vnic */ 789 status = dladm_link_get_proplist(handle, linkid, &proplist); 790 if (status != DLADM_STATUS_OK) 791 goto done; 792 793 if (proplist != NULL) { 794 status = dladm_link_proplist_extract(handle, proplist, 795 &attr.va_resource_props, DLADM_OPT_BOOT); 796 } 797 798 status = i_dladm_vnic_create_sys(handle, &attr); 799 if (status == DLADM_STATUS_OK) { 800 status = dladm_up_datalink_id(handle, linkid); 801 if (status != DLADM_STATUS_OK) 802 (void) i_dladm_vnic_delete_sys(handle, linkid); 803 } 804 805 done: 806 *statusp = status; 807 return (DLADM_WALK_CONTINUE); 808 } 809 810 dladm_status_t 811 dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 812 { 813 dladm_vnic_up_arg_t vnic_arg; 814 datalink_class_t class; 815 816 class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN : 817 (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB); 818 819 if (linkid == DATALINK_ALL_LINKID) { 820 (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle, 821 &vnic_arg, class, DATALINK_ANY_MEDIATYPE, 822 DLADM_OPT_PERSIST); 823 return (DLADM_STATUS_OK); 824 } else { 825 (void) i_dladm_vnic_up(handle, linkid, &vnic_arg); 826 return (vnic_arg.status); 827 } 828 } 829