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