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