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