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