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;
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, &class, &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 (class == DATALINK_CLASS_VNIC ||
456 		    class == 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 		goto done;
553 	vnic_created = B_TRUE;
554 
555 	/* Save vnic configuration and its properties */
556 	if (!(flags & DLADM_OPT_PERSIST))
557 		goto done;
558 
559 	status = dladm_vnic_persist_conf(handle, name, &attr, class);
560 	if (status != DLADM_STATUS_OK)
561 		goto done;
562 	conf_set = B_TRUE;
563 
564 	if (proplist != NULL) {
565 		for (i = 0; i < proplist->al_count; i++) {
566 			dladm_arg_info_t	*aip = &proplist->al_info[i];
567 
568 			status = dladm_set_linkprop(handle, vnic_id,
569 			    aip->ai_name, aip->ai_val, aip->ai_count,
570 			    DLADM_OPT_PERSIST);
571 			if (status != DLADM_STATUS_OK) {
572 				char	errmsg[DLADM_STRSIZE];
573 				(void) dladm_errlist_append(errs,
574 				    "failed to set property %s: %s",
575 				    aip->ai_name,
576 				    dladm_status2str(status, errmsg));
577 				break;
578 			}
579 		}
580 	}
581 
582 done:
583 	if (status != DLADM_STATUS_OK) {
584 		if (conf_set)
585 			(void) dladm_remove_conf(handle, vnic_id);
586 		if (vnic_created)
587 			(void) i_dladm_vnic_delete_sys(handle, vnic_id);
588 		(void) dladm_destroy_datalink_id(handle, vnic_id, flags);
589 	} else {
590 		if (vnic_id_out != NULL)
591 			*vnic_id_out = vnic_id;
592 		if (mac_slot != NULL)
593 			*mac_slot = attr.va_mac_slot;
594 	}
595 
596 	if (is_vlan) {
597 		dladm_status_t stat2;
598 
599 		stat2 = dladm_bridge_refresh(handle, linkid);
600 		if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK)
601 			status = stat2;
602 	}
603 	return (status);
604 }
605 
606 /*
607  * Delete a VNIC / VLAN.
608  */
609 dladm_status_t
610 dladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
611 {
612 	dladm_status_t status;
613 	datalink_class_t class;
614 
615 	if (flags == 0)
616 		return (DLADM_STATUS_BADARG);
617 
618 	if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0)
619 	    != DLADM_STATUS_OK))
620 		return (DLADM_STATUS_BADARG);
621 
622 	if ((flags & DLADM_OPT_VLAN) != 0) {
623 		if (class != DATALINK_CLASS_VLAN)
624 			return (DLADM_STATUS_BADARG);
625 	} else {
626 		if (class != DATALINK_CLASS_VNIC &&
627 		    class != DATALINK_CLASS_ETHERSTUB)
628 			return (DLADM_STATUS_BADARG);
629 	}
630 
631 	if ((flags & DLADM_OPT_ACTIVE) != 0) {
632 		status = i_dladm_vnic_delete_sys(handle, linkid);
633 		if (status == DLADM_STATUS_OK) {
634 			(void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0,
635 			    DLADM_OPT_ACTIVE);
636 			(void) dladm_destroy_datalink_id(handle, linkid,
637 			    DLADM_OPT_ACTIVE);
638 		} else if (status != DLADM_STATUS_NOTFOUND ||
639 		    !(flags & DLADM_OPT_PERSIST)) {
640 			return (status);
641 		}
642 	}
643 	if ((flags & DLADM_OPT_PERSIST) != 0) {
644 		(void) dladm_remove_conf(handle, linkid);
645 		(void) dladm_destroy_datalink_id(handle, linkid,
646 		    DLADM_OPT_PERSIST);
647 	}
648 	return (dladm_bridge_refresh(handle, linkid));
649 }
650 
651 static const char *
652 dladm_vnic_macaddr2str(const uchar_t *mac, char *buf)
653 {
654 	static char unknown_mac[] = {0, 0, 0, 0, 0, 0};
655 
656 	if (buf == NULL)
657 		return (NULL);
658 
659 	if (bcmp(unknown_mac, mac, ETHERADDRL) == 0)
660 		(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
661 	else
662 		return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
663 
664 	return (buf);
665 }
666 
667 static dladm_status_t
668 dladm_vnic_str2macaddr(const char *str, uchar_t *buf)
669 {
670 	int len = 0;
671 	uchar_t *b = _link_aton(str, &len);
672 
673 	if (b == NULL || len >= MAXMACADDRLEN)
674 		return (DLADM_STATUS_BADARG);
675 
676 	bcopy(b, buf, len);
677 	free(b);
678 	return (DLADM_STATUS_OK);
679 }
680 
681 
682 static dladm_status_t
683 dladm_vnic_persist_conf(dladm_handle_t handle, const char *name,
684     dladm_vnic_attr_t *attrp, datalink_class_t class)
685 {
686 	dladm_conf_t conf;
687 	dladm_status_t status;
688 	char macstr[ETHERADDRL * 3];
689 	char linkover[MAXLINKNAMELEN];
690 	uint64_t u64;
691 
692 	if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id,
693 	    class, DL_ETHER, &conf)) != DLADM_STATUS_OK)
694 		return (status);
695 
696 	if (attrp->va_link_id != DATALINK_INVALID_LINKID) {
697 		status = dladm_datalink_id2info(handle, attrp->va_link_id, NULL,
698 		    NULL, NULL, linkover, sizeof (linkover));
699 		if (status != DLADM_STATUS_OK)
700 			goto done;
701 		status = dladm_set_conf_field(handle, conf, FLINKOVER,
702 		    DLADM_TYPE_STR, linkover);
703 		if (status != DLADM_STATUS_OK)
704 			goto done;
705 	}
706 
707 	if (class != DATALINK_CLASS_VLAN) {
708 		u64 = attrp->va_mac_addr_type;
709 		status = dladm_set_conf_field(handle, conf, FMADDRTYPE,
710 		    DLADM_TYPE_UINT64, &u64);
711 		if (status != DLADM_STATUS_OK)
712 			goto done;
713 
714 		u64 = attrp->va_vrid;
715 		status = dladm_set_conf_field(handle, conf, FVRID,
716 		    DLADM_TYPE_UINT64, &u64);
717 		if (status != DLADM_STATUS_OK)
718 			goto done;
719 
720 		u64 = attrp->va_af;
721 		status = dladm_set_conf_field(handle, conf, FVRAF,
722 		    DLADM_TYPE_UINT64, &u64);
723 		if (status != DLADM_STATUS_OK)
724 			goto done;
725 
726 		if (attrp->va_mac_len != ETHERADDRL) {
727 			u64 = attrp->va_mac_len;
728 			status = dladm_set_conf_field(handle, conf, FMADDRLEN,
729 			    DLADM_TYPE_UINT64, &u64);
730 			if (status != DLADM_STATUS_OK)
731 				goto done;
732 		}
733 
734 		if (attrp->va_mac_slot != -1) {
735 			u64 = attrp->va_mac_slot;
736 			status = dladm_set_conf_field(handle, conf,
737 			    FMADDRSLOT, DLADM_TYPE_UINT64, &u64);
738 			if (status != DLADM_STATUS_OK)
739 			goto done;
740 		}
741 
742 		if (attrp->va_mac_prefix_len !=
743 		    sizeof (dladm_vnic_def_prefix)) {
744 			u64 = attrp->va_mac_prefix_len;
745 			status = dladm_set_conf_field(handle, conf,
746 			    FMADDRPREFIXLEN, DLADM_TYPE_UINT64, &u64);
747 			if (status != DLADM_STATUS_OK)
748 				goto done;
749 		}
750 
751 		(void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr);
752 		status = dladm_set_conf_field(handle, conf, FMACADDR,
753 		    DLADM_TYPE_STR, macstr);
754 		if (status != DLADM_STATUS_OK)
755 			goto done;
756 	}
757 
758 	if (attrp->va_vid != 0) {
759 		u64 = attrp->va_vid;
760 		status = dladm_set_conf_field(handle, conf, FVLANID,
761 		    DLADM_TYPE_UINT64, &u64);
762 		if (status != DLADM_STATUS_OK)
763 			goto done;
764 	}
765 
766 	/*
767 	 * Commit the link configuration.
768 	 */
769 	status = dladm_write_conf(handle, conf);
770 
771 done:
772 	dladm_destroy_conf(handle, conf);
773 	return (status);
774 }
775 
776 typedef struct dladm_vnic_up_arg_s {
777 	uint32_t	flags;
778 	dladm_status_t	status;
779 } dladm_vnic_up_arg_t;
780 
781 static int
782 i_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg)
783 {
784 	dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status);
785 	dladm_vnic_attr_t attr;
786 	dladm_status_t status;
787 	dladm_arg_list_t *proplist;
788 
789 	bzero(&attr, sizeof (attr));
790 
791 	status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST);
792 	if (status != DLADM_STATUS_OK)
793 		goto done;
794 
795 	/* Get all properties for this vnic */
796 	status = dladm_link_get_proplist(handle, linkid, &proplist);
797 	if (status != DLADM_STATUS_OK)
798 		goto done;
799 
800 	if (proplist != NULL) {
801 		status = dladm_link_proplist_extract(handle, proplist,
802 		    &attr.va_resource_props, DLADM_OPT_BOOT);
803 	}
804 
805 	status = i_dladm_vnic_create_sys(handle, &attr);
806 	if (status == DLADM_STATUS_OK) {
807 		status = dladm_up_datalink_id(handle, linkid);
808 		if (status != DLADM_STATUS_OK)
809 			(void) i_dladm_vnic_delete_sys(handle, linkid);
810 	}
811 
812 done:
813 	*statusp = status;
814 	return (DLADM_WALK_CONTINUE);
815 }
816 
817 dladm_status_t
818 dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
819 {
820 	dladm_vnic_up_arg_t vnic_arg;
821 	datalink_class_t class;
822 
823 	class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN :
824 	    (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB);
825 
826 	if (linkid == DATALINK_ALL_LINKID) {
827 		(void) dladm_walk_datalink_id(i_dladm_vnic_up, handle,
828 		    &vnic_arg, class, DATALINK_ANY_MEDIATYPE,
829 		    DLADM_OPT_PERSIST);
830 		return (DLADM_STATUS_OK);
831 	} else {
832 		(void) i_dladm_vnic_up(handle, linkid, &vnic_arg);
833 		return (vnic_arg.status);
834 	}
835 }
836