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