15084Sjohnlev /* 25084Sjohnlev * CDDL HEADER START 35084Sjohnlev * 45084Sjohnlev * The contents of this file are subject to the terms of the 55084Sjohnlev * Common Development and Distribution License (the "License"). 65084Sjohnlev * You may not use this file except in compliance with the License. 75084Sjohnlev * 85084Sjohnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95084Sjohnlev * or http://www.opensolaris.org/os/licensing. 105084Sjohnlev * See the License for the specific language governing permissions 115084Sjohnlev * and limitations under the License. 125084Sjohnlev * 135084Sjohnlev * When distributing Covered Code, include this CDDL HEADER in each 145084Sjohnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155084Sjohnlev * If applicable, add the following below this CDDL HEADER, with the 165084Sjohnlev * fields enclosed by brackets "[]" replaced with your own identifying 175084Sjohnlev * information: Portions Copyright [yyyy] [name of copyright owner] 185084Sjohnlev * 195084Sjohnlev * CDDL HEADER END 205084Sjohnlev */ 215084Sjohnlev /* 22*10491SRishi.Srivatsavai@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 235084Sjohnlev * Use is subject to license terms. 245084Sjohnlev */ 255084Sjohnlev 265084Sjohnlev #include <stdio.h> 275084Sjohnlev #include <sys/types.h> 285084Sjohnlev #include <sys/stat.h> 295084Sjohnlev #include <string.h> 305084Sjohnlev #include <fcntl.h> 315084Sjohnlev #include <unistd.h> 325084Sjohnlev #include <stropts.h> 335084Sjohnlev #include <stdlib.h> 345084Sjohnlev #include <errno.h> 355084Sjohnlev #include <strings.h> 365084Sjohnlev #include <libintl.h> 375084Sjohnlev #include <net/if_types.h> 385084Sjohnlev #include <net/if_dl.h> 398275SEric Cheng #include <sys/dld.h> 405084Sjohnlev #include <libdladm_impl.h> 415895Syz147064 #include <libdllink.h> 42*10491SRishi.Srivatsavai@Sun.COM #include <libdlbridge.h> 435084Sjohnlev #include <libdlvnic.h> 445084Sjohnlev 455084Sjohnlev /* 465084Sjohnlev * VNIC administration library. 475084Sjohnlev */ 485084Sjohnlev 498275SEric Cheng /* 508275SEric Cheng * Default random MAC address prefix (locally administered). 518275SEric Cheng */ 528275SEric Cheng static char dladm_vnic_def_prefix[] = {0x02, 0x08, 0x20}; 538275SEric Cheng 548453SAnurag.Maskey@Sun.COM static dladm_status_t dladm_vnic_persist_conf(dladm_handle_t, 558453SAnurag.Maskey@Sun.COM const char *name, dladm_vnic_attr_t *, 568453SAnurag.Maskey@Sun.COM datalink_class_t); 578275SEric Cheng static const char *dladm_vnic_macaddr2str(const uchar_t *, char *); 588275SEric Cheng static dladm_status_t dladm_vnic_str2macaddr(const char *, uchar_t *); 595084Sjohnlev 608275SEric Cheng /* 618275SEric Cheng * Convert a diagnostic returned by the kernel into a dladm_status_t. 628275SEric Cheng */ 638275SEric Cheng static dladm_status_t 648275SEric Cheng dladm_vnic_diag2status(vnic_ioc_diag_t ioc_diag) 658275SEric Cheng { 668275SEric Cheng switch (ioc_diag) { 678275SEric Cheng case VNIC_IOC_DIAG_MACADDR_INVALID: 688275SEric Cheng return (DLADM_STATUS_INVALIDMACADDR); 698275SEric Cheng case VNIC_IOC_DIAG_MACADDRLEN_INVALID: 708275SEric Cheng return (DLADM_STATUS_INVALIDMACADDRLEN); 718275SEric Cheng case VNIC_IOC_DIAG_MACADDR_NIC: 728275SEric Cheng return (DLADM_STATUS_INVALIDMACADDRNIC); 738275SEric Cheng case VNIC_IOC_DIAG_MACADDR_INUSE: 748275SEric Cheng return (DLADM_STATUS_INVALIDMACADDRINUSE); 758275SEric Cheng case VNIC_IOC_DIAG_MACFACTORYSLOTINVALID: 768275SEric Cheng return (DLADM_STATUS_MACFACTORYSLOTINVALID); 778275SEric Cheng case VNIC_IOC_DIAG_MACFACTORYSLOTUSED: 788275SEric Cheng return (DLADM_STATUS_MACFACTORYSLOTUSED); 798275SEric Cheng case VNIC_IOC_DIAG_MACFACTORYSLOTALLUSED: 808275SEric Cheng return (DLADM_STATUS_MACFACTORYSLOTALLUSED); 818275SEric Cheng case VNIC_IOC_DIAG_MACFACTORYNOTSUP: 828275SEric Cheng return (DLADM_STATUS_MACFACTORYNOTSUP); 838275SEric Cheng case VNIC_IOC_DIAG_MACPREFIX_INVALID: 848275SEric Cheng return (DLADM_STATUS_INVALIDMACPREFIX); 858275SEric Cheng case VNIC_IOC_DIAG_MACPREFIXLEN_INVALID: 868275SEric Cheng return (DLADM_STATUS_INVALIDMACPREFIXLEN); 878275SEric Cheng case VNIC_IOC_DIAG_MACMARGIN_INVALID: 888275SEric Cheng return (DLADM_STATUS_INVALID_MACMARGIN); 898275SEric Cheng case VNIC_IOC_DIAG_NO_HWRINGS: 908275SEric Cheng return (DLADM_STATUS_NO_HWRINGS); 918275SEric Cheng } 928275SEric Cheng return (DLADM_STATUS_OK); 938275SEric Cheng } 945084Sjohnlev 955084Sjohnlev /* 965084Sjohnlev * Send a create command to the VNIC driver. 975084Sjohnlev */ 988275SEric Cheng dladm_status_t 998453SAnurag.Maskey@Sun.COM i_dladm_vnic_create_sys(dladm_handle_t handle, dladm_vnic_attr_t *attr) 1005084Sjohnlev { 1018453SAnurag.Maskey@Sun.COM int rc; 1025084Sjohnlev vnic_ioc_create_t ioc; 1038275SEric Cheng dladm_status_t status = DLADM_STATUS_OK; 1045084Sjohnlev 1058275SEric Cheng bzero(&ioc, sizeof (ioc)); 1068275SEric Cheng ioc.vc_vnic_id = attr->va_vnic_id; 1078275SEric Cheng ioc.vc_link_id = attr->va_link_id; 1088275SEric Cheng ioc.vc_mac_addr_type = attr->va_mac_addr_type; 1098275SEric Cheng ioc.vc_mac_len = attr->va_mac_len; 1108275SEric Cheng ioc.vc_mac_slot = attr->va_mac_slot; 1118275SEric Cheng ioc.vc_mac_prefix_len = attr->va_mac_prefix_len; 1128275SEric Cheng ioc.vc_vid = attr->va_vid; 1138275SEric Cheng ioc.vc_flags = attr->va_force ? VNIC_IOC_CREATE_FORCE : 0; 1148275SEric Cheng ioc.vc_flags |= attr->va_hwrings ? VNIC_IOC_CREATE_REQ_HWRINGS : 0; 1155084Sjohnlev 1168275SEric Cheng if (attr->va_mac_len > 0 || ioc.vc_mac_prefix_len > 0) 1178275SEric Cheng bcopy(attr->va_mac_addr, ioc.vc_mac_addr, MAXMACADDRLEN); 1188275SEric Cheng bcopy(&attr->va_resource_props, &ioc.vc_resource_props, 1198275SEric Cheng sizeof (mac_resource_props_t)); 1208275SEric Cheng if (attr->va_link_id == DATALINK_INVALID_LINKID) 1218275SEric Cheng ioc.vc_flags |= VNIC_IOC_CREATE_ANCHOR; 1225084Sjohnlev 1238453SAnurag.Maskey@Sun.COM rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_CREATE, &ioc); 1248275SEric Cheng if (rc < 0) 1258275SEric Cheng status = dladm_errno2status(errno); 1268275SEric Cheng 1278275SEric Cheng if (status != DLADM_STATUS_OK) { 1288275SEric Cheng if (ioc.vc_diag != VNIC_IOC_DIAG_NONE) 1298275SEric Cheng status = dladm_vnic_diag2status(ioc.vc_diag); 1308275SEric Cheng } 1318275SEric Cheng if (status != DLADM_STATUS_OK) 1328275SEric Cheng return (status); 1338275SEric Cheng 1348275SEric Cheng attr->va_mac_addr_type = ioc.vc_mac_addr_type; 1358275SEric Cheng switch (ioc.vc_mac_addr_type) { 1368275SEric Cheng case VNIC_MAC_ADDR_TYPE_FACTORY: 1378275SEric Cheng attr->va_mac_slot = ioc.vc_mac_slot; 1388275SEric Cheng break; 1398275SEric Cheng case VNIC_MAC_ADDR_TYPE_RANDOM: 1408275SEric Cheng bcopy(ioc.vc_mac_addr, attr->va_mac_addr, MAXMACADDRLEN); 1418275SEric Cheng attr->va_mac_len = ioc.vc_mac_len; 1428275SEric Cheng break; 1438275SEric Cheng } 1448275SEric Cheng return (status); 1458275SEric Cheng } 1468275SEric Cheng 1478275SEric Cheng /* 1488275SEric Cheng * Get the configuration information of the given VNIC. 1498275SEric Cheng */ 1508275SEric Cheng static dladm_status_t 1518453SAnurag.Maskey@Sun.COM i_dladm_vnic_info_active(dladm_handle_t handle, datalink_id_t linkid, 1528453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp) 1538275SEric Cheng { 1548275SEric Cheng vnic_ioc_info_t ioc; 1558275SEric Cheng vnic_info_t *vnic; 1568453SAnurag.Maskey@Sun.COM int rc; 1578275SEric Cheng dladm_status_t status = DLADM_STATUS_OK; 1588275SEric Cheng 1598275SEric Cheng bzero(&ioc, sizeof (ioc)); 1608275SEric Cheng vnic = &ioc.vi_info; 1618275SEric Cheng vnic->vn_vnic_id = linkid; 1628275SEric Cheng 1638453SAnurag.Maskey@Sun.COM rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_INFO, &ioc); 1648275SEric Cheng if (rc != 0) { 1658275SEric Cheng status = dladm_errno2status(errno); 1668275SEric Cheng goto bail; 1678275SEric Cheng } 1688275SEric Cheng 1698275SEric Cheng attrp->va_vnic_id = vnic->vn_vnic_id; 1708275SEric Cheng attrp->va_link_id = vnic->vn_link_id; 1718275SEric Cheng attrp->va_mac_addr_type = vnic->vn_mac_addr_type; 1728275SEric Cheng bcopy(vnic->vn_mac_addr, attrp->va_mac_addr, MAXMACADDRLEN); 1738275SEric Cheng attrp->va_mac_len = vnic->vn_mac_len; 1748275SEric Cheng attrp->va_mac_slot = vnic->vn_mac_slot; 1758275SEric Cheng attrp->va_mac_prefix_len = vnic->vn_mac_prefix_len; 1768275SEric Cheng attrp->va_vid = vnic->vn_vid; 1778275SEric Cheng attrp->va_force = vnic->vn_force; 1788275SEric Cheng 1798275SEric Cheng bail: 1808275SEric Cheng return (status); 1818275SEric Cheng } 1828275SEric Cheng 1838275SEric Cheng static dladm_status_t 1848453SAnurag.Maskey@Sun.COM i_dladm_vnic_info_persist(dladm_handle_t handle, datalink_id_t linkid, 1858453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp) 1868275SEric Cheng { 1878275SEric Cheng dladm_conf_t conf; 1888275SEric Cheng dladm_status_t status; 1898275SEric Cheng char macstr[ETHERADDRL * 3]; 1908275SEric Cheng uint64_t u64; 1918275SEric Cheng datalink_class_t class; 1928275SEric Cheng 1938275SEric Cheng attrp->va_vnic_id = linkid; 1948453SAnurag.Maskey@Sun.COM if ((status = dladm_read_conf(handle, linkid, &conf)) != 1958453SAnurag.Maskey@Sun.COM DLADM_STATUS_OK) 1968275SEric Cheng return (status); 1978275SEric Cheng 1988453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FLINKOVER, &u64, 1998453SAnurag.Maskey@Sun.COM sizeof (u64)); 2008275SEric Cheng attrp->va_link_id = ((status == DLADM_STATUS_OK) ? 2018275SEric Cheng (datalink_id_t)u64 : DATALINK_INVALID_LINKID); 2028275SEric Cheng 2038453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FHWRINGS, 2048453SAnurag.Maskey@Sun.COM &attrp->va_hwrings, sizeof (boolean_t)); 2058275SEric Cheng 2068275SEric Cheng if (status != DLADM_STATUS_OK && status != DLADM_STATUS_NOTFOUND) 2078275SEric Cheng goto done; 2088275SEric Cheng if (status == DLADM_STATUS_NOTFOUND) 2098275SEric Cheng attrp->va_hwrings = B_FALSE; 2108275SEric Cheng 2118453SAnurag.Maskey@Sun.COM if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class, 2128275SEric Cheng NULL, NULL, 0)) != DLADM_STATUS_OK) 2138275SEric Cheng goto done; 2148275SEric Cheng 2158275SEric Cheng if (class == DATALINK_CLASS_VLAN) { 2168275SEric Cheng if (attrp->va_link_id == DATALINK_INVALID_LINKID) { 2178275SEric Cheng status = DLADM_STATUS_BADARG; 2188275SEric Cheng goto done; 2198275SEric Cheng } 2208275SEric Cheng attrp->va_mac_addr_type = VNIC_MAC_ADDR_TYPE_PRIMARY; 2218275SEric Cheng attrp->va_mac_len = 0; 2228275SEric Cheng } else { 2238453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRTYPE, &u64, 2248275SEric Cheng sizeof (u64)); 2258275SEric Cheng if (status != DLADM_STATUS_OK) 2268275SEric Cheng goto done; 2278275SEric Cheng 2288275SEric Cheng attrp->va_mac_addr_type = (vnic_mac_addr_type_t)u64; 2298275SEric Cheng 2308453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRLEN, &u64, 2318275SEric Cheng sizeof (u64)); 2328275SEric Cheng attrp->va_mac_len = ((status == DLADM_STATUS_OK) ? 2338275SEric Cheng (uint_t)u64 : ETHERADDRL); 2348275SEric Cheng 2358453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRSLOT, &u64, 2368275SEric Cheng sizeof (u64)); 2378275SEric Cheng attrp->va_mac_slot = ((status == DLADM_STATUS_OK) ? 2388275SEric Cheng (int)u64 : -1); 2398275SEric Cheng 2408453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRPREFIXLEN, 2418453SAnurag.Maskey@Sun.COM &u64, sizeof (u64)); 2428275SEric Cheng attrp->va_mac_prefix_len = ((status == DLADM_STATUS_OK) ? 2438275SEric Cheng (uint_t)u64 : sizeof (dladm_vnic_def_prefix)); 2448275SEric Cheng 2458453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMACADDR, macstr, 2468275SEric Cheng sizeof (macstr)); 2478275SEric Cheng if (status != DLADM_STATUS_OK) 2488275SEric Cheng goto done; 2498275SEric Cheng 2508275SEric Cheng status = dladm_vnic_str2macaddr(macstr, attrp->va_mac_addr); 2518275SEric Cheng if (status != DLADM_STATUS_OK) 2528275SEric Cheng goto done; 2538275SEric Cheng } 2548275SEric Cheng 2558453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FVLANID, &u64, 2568453SAnurag.Maskey@Sun.COM sizeof (u64)); 2578275SEric Cheng attrp->va_vid = ((status == DLADM_STATUS_OK) ? (uint16_t)u64 : 0); 2588275SEric Cheng 2598275SEric Cheng 2608275SEric Cheng status = DLADM_STATUS_OK; 2618275SEric Cheng done: 2628453SAnurag.Maskey@Sun.COM dladm_destroy_conf(handle, conf); 2638275SEric Cheng return (status); 2648275SEric Cheng } 2658275SEric Cheng 2668275SEric Cheng dladm_status_t 2678453SAnurag.Maskey@Sun.COM dladm_vnic_info(dladm_handle_t handle, datalink_id_t linkid, 2688453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp, uint32_t flags) 2698275SEric Cheng { 2708275SEric Cheng if (flags == DLADM_OPT_ACTIVE) 2718453SAnurag.Maskey@Sun.COM return (i_dladm_vnic_info_active(handle, linkid, attrp)); 2728275SEric Cheng else if (flags == DLADM_OPT_PERSIST) 2738453SAnurag.Maskey@Sun.COM return (i_dladm_vnic_info_persist(handle, linkid, attrp)); 2748275SEric Cheng else 2758275SEric Cheng return (DLADM_STATUS_BADARG); 2768275SEric Cheng } 2778275SEric Cheng 2788275SEric Cheng /* 2798275SEric Cheng * Remove a VNIC from the kernel. 2808275SEric Cheng */ 2818275SEric Cheng dladm_status_t 2828453SAnurag.Maskey@Sun.COM i_dladm_vnic_delete_sys(dladm_handle_t handle, datalink_id_t linkid) 2838275SEric Cheng { 2848275SEric Cheng vnic_ioc_delete_t ioc; 2858275SEric Cheng dladm_status_t status = DLADM_STATUS_OK; 2868453SAnurag.Maskey@Sun.COM int rc; 2878275SEric Cheng 2888275SEric Cheng ioc.vd_vnic_id = linkid; 2898275SEric Cheng 2908453SAnurag.Maskey@Sun.COM rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_DELETE, &ioc); 2918275SEric Cheng if (rc < 0) 2927408SSebastien.Roy@Sun.COM status = dladm_errno2status(errno); 2935084Sjohnlev 2947408SSebastien.Roy@Sun.COM return (status); 2955084Sjohnlev } 2965084Sjohnlev 2975084Sjohnlev /* 2985084Sjohnlev * Convert between MAC address types and their string representations. 2995084Sjohnlev */ 3005084Sjohnlev 3015084Sjohnlev typedef struct dladm_vnic_addr_type_s { 3028275SEric Cheng const char *va_str; 3038275SEric Cheng vnic_mac_addr_type_t va_type; 3045084Sjohnlev } dladm_vnic_addr_type_t; 3055084Sjohnlev 3065084Sjohnlev static dladm_vnic_addr_type_t addr_types[] = { 3075084Sjohnlev {"fixed", VNIC_MAC_ADDR_TYPE_FIXED}, 3088275SEric Cheng {"random", VNIC_MAC_ADDR_TYPE_RANDOM}, 3098275SEric Cheng {"factory", VNIC_MAC_ADDR_TYPE_FACTORY}, 3108275SEric Cheng {"auto", VNIC_MAC_ADDR_TYPE_AUTO}, 3118275SEric Cheng {"fixed", VNIC_MAC_ADDR_TYPE_PRIMARY} 3125084Sjohnlev }; 3135084Sjohnlev 3145084Sjohnlev #define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t)) 3155084Sjohnlev 3168275SEric Cheng static const char * 3178275SEric Cheng dladm_vnic_macaddrtype2str(vnic_mac_addr_type_t type) 3188275SEric Cheng { 3198275SEric Cheng int i; 3208275SEric Cheng 3218275SEric Cheng for (i = 0; i < NADDR_TYPES; i++) { 3228275SEric Cheng if (type == addr_types[i].va_type) 3238275SEric Cheng return (addr_types[i].va_str); 3248275SEric Cheng } 3258275SEric Cheng return (NULL); 3268275SEric Cheng } 3278275SEric Cheng 3285895Syz147064 dladm_status_t 3295895Syz147064 dladm_vnic_str2macaddrtype(const char *str, vnic_mac_addr_type_t *val) 3305084Sjohnlev { 3315084Sjohnlev int i; 3325084Sjohnlev dladm_vnic_addr_type_t *type; 3335084Sjohnlev 3345084Sjohnlev for (i = 0; i < NADDR_TYPES; i++) { 3355084Sjohnlev type = &addr_types[i]; 3365084Sjohnlev if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) { 3375084Sjohnlev *val = type->va_type; 3385895Syz147064 return (DLADM_STATUS_OK); 3395084Sjohnlev } 3405084Sjohnlev } 3415895Syz147064 return (DLADM_STATUS_BADARG); 3425084Sjohnlev } 3435084Sjohnlev 3445084Sjohnlev /* 3458275SEric Cheng * Create a new VNIC / VLAN. Update the configuration file and bring it up. 3465084Sjohnlev */ 3475084Sjohnlev dladm_status_t 3488453SAnurag.Maskey@Sun.COM dladm_vnic_create(dladm_handle_t handle, const char *vnic, datalink_id_t linkid, 3495084Sjohnlev vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, int mac_len, 3508275SEric Cheng int *mac_slot, uint_t mac_prefix_len, uint16_t vid, 3518275SEric Cheng datalink_id_t *vnic_id_out, dladm_arg_list_t *proplist, uint32_t flags) 3525084Sjohnlev { 3538275SEric Cheng dladm_vnic_attr_t attr; 3545895Syz147064 datalink_id_t vnic_id; 3555895Syz147064 datalink_class_t class; 3568275SEric Cheng uint32_t media = DL_ETHER; 3578275SEric Cheng char name[MAXLINKNAMELEN]; 3588275SEric Cheng uchar_t tmp_addr[MAXMACADDRLEN]; 3595084Sjohnlev dladm_status_t status; 3608275SEric Cheng boolean_t is_vlan; 3618275SEric Cheng boolean_t is_etherstub; 3628275SEric Cheng int i; 363*10491SRishi.Srivatsavai@Sun.COM boolean_t vnic_created = B_FALSE; 364*10491SRishi.Srivatsavai@Sun.COM boolean_t conf_set = B_FALSE; 3655084Sjohnlev 3665084Sjohnlev /* 3675084Sjohnlev * Sanity test arguments. 3685084Sjohnlev */ 3698275SEric Cheng if ((flags & DLADM_OPT_ACTIVE) == 0) 3708275SEric Cheng return (DLADM_STATUS_NOTSUP); 3718275SEric Cheng 3728275SEric Cheng is_vlan = ((flags & DLADM_OPT_VLAN) != 0); 3738275SEric Cheng if (is_vlan && ((vid < 1 || vid > 4094))) 3748275SEric Cheng return (DLADM_STATUS_VIDINVAL); 3758275SEric Cheng 3768275SEric Cheng is_etherstub = (linkid == DATALINK_INVALID_LINKID); 3775084Sjohnlev 3785084Sjohnlev if (mac_len > MAXMACADDRLEN) 3795084Sjohnlev return (DLADM_STATUS_INVALIDMACADDRLEN); 3805084Sjohnlev 3818275SEric Cheng if (!dladm_vnic_macaddrtype2str(mac_addr_type)) 3825084Sjohnlev return (DLADM_STATUS_INVALIDMACADDRTYPE); 3835084Sjohnlev 3848275SEric Cheng /* 3858275SEric Cheng * If a random address might be generated, but no prefix 3868275SEric Cheng * was specified by the caller, use the default MAC address 3878275SEric Cheng * prefix. 3888275SEric Cheng */ 3898275SEric Cheng if ((mac_addr_type == VNIC_MAC_ADDR_TYPE_RANDOM || 3908275SEric Cheng mac_addr_type == VNIC_MAC_ADDR_TYPE_AUTO) && 3918275SEric Cheng mac_prefix_len == 0) { 3928275SEric Cheng mac_prefix_len = sizeof (dladm_vnic_def_prefix); 3938275SEric Cheng mac_addr = tmp_addr; 3948275SEric Cheng bcopy(dladm_vnic_def_prefix, mac_addr, mac_prefix_len); 3955895Syz147064 } 3965895Syz147064 3978275SEric Cheng if ((flags & DLADM_OPT_ANCHOR) == 0) { 3988453SAnurag.Maskey@Sun.COM if ((status = dladm_datalink_id2info(handle, linkid, NULL, 3998453SAnurag.Maskey@Sun.COM &class, &media, NULL, 0)) != DLADM_STATUS_OK) 4008275SEric Cheng return (status); 4018275SEric Cheng 4028275SEric Cheng if (class == DATALINK_CLASS_VNIC || 4038275SEric Cheng class == DATALINK_CLASS_VLAN) 4048275SEric Cheng return (DLADM_STATUS_BADARG); 4058275SEric Cheng } else { 4068275SEric Cheng /* it's an anchor VNIC */ 4078275SEric Cheng if (linkid != DATALINK_INVALID_LINKID || vid != 0) 4088275SEric Cheng return (DLADM_STATUS_BADARG); 4098275SEric Cheng } 4105084Sjohnlev 4115895Syz147064 if (vnic == NULL) { 4125895Syz147064 flags |= DLADM_OPT_PREFIX; 4138275SEric Cheng (void) strlcpy(name, "vnic", sizeof (name)); 4148275SEric Cheng } else { 4158275SEric Cheng (void) strlcpy(name, vnic, sizeof (name)); 4165895Syz147064 } 4175895Syz147064 4188275SEric Cheng class = is_vlan ? DATALINK_CLASS_VLAN : 4198275SEric Cheng (is_etherstub ? DATALINK_CLASS_ETHERSTUB : DATALINK_CLASS_VNIC); 4208453SAnurag.Maskey@Sun.COM if ((status = dladm_create_datalink_id(handle, name, class, 4218275SEric Cheng media, flags, &vnic_id)) != DLADM_STATUS_OK) 4225895Syz147064 return (status); 4238275SEric Cheng 4248275SEric Cheng if ((flags & DLADM_OPT_PREFIX) != 0) { 4258275SEric Cheng (void) snprintf(name + 4, sizeof (name), "%llu", vnic_id); 4268275SEric Cheng flags &= ~DLADM_OPT_PREFIX; 4275084Sjohnlev } 4285084Sjohnlev 4295084Sjohnlev bzero(&attr, sizeof (attr)); 4308275SEric Cheng 4318275SEric Cheng /* Extract resource_ctl and cpu_list from proplist */ 4328275SEric Cheng if (proplist != NULL) { 4338453SAnurag.Maskey@Sun.COM status = dladm_link_proplist_extract(handle, proplist, 4348275SEric Cheng &attr.va_resource_props); 4358275SEric Cheng if (status != DLADM_STATUS_OK) 4368275SEric Cheng goto done; 4378275SEric Cheng } 4385084Sjohnlev 4398275SEric Cheng attr.va_vnic_id = vnic_id; 4408275SEric Cheng attr.va_link_id = linkid; 4418275SEric Cheng attr.va_mac_addr_type = mac_addr_type; 4428275SEric Cheng attr.va_mac_len = mac_len; 4438275SEric Cheng if (mac_slot != NULL) 4448275SEric Cheng attr.va_mac_slot = *mac_slot; 4458275SEric Cheng if (mac_len > 0) 4468275SEric Cheng bcopy(mac_addr, attr.va_mac_addr, mac_len); 4478275SEric Cheng else if (mac_prefix_len > 0) 4488275SEric Cheng bcopy(mac_addr, attr.va_mac_addr, mac_prefix_len); 4498275SEric Cheng attr.va_mac_prefix_len = mac_prefix_len; 4508275SEric Cheng attr.va_vid = vid; 4518275SEric Cheng attr.va_force = (flags & DLADM_OPT_FORCE) != 0; 4528275SEric Cheng attr.va_hwrings = (flags & DLADM_OPT_HWRINGS) != 0; 4538275SEric Cheng 4548453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_create_sys(handle, &attr); 4558275SEric Cheng if (status != DLADM_STATUS_OK) 4568275SEric Cheng goto done; 457*10491SRishi.Srivatsavai@Sun.COM vnic_created = B_TRUE; 4588275SEric Cheng 4598275SEric Cheng /* Save vnic configuration and its properties */ 4608275SEric Cheng if (!(flags & DLADM_OPT_PERSIST)) 4618275SEric Cheng goto done; 4628275SEric Cheng 4638453SAnurag.Maskey@Sun.COM status = dladm_vnic_persist_conf(handle, name, &attr, class); 464*10491SRishi.Srivatsavai@Sun.COM if (status != DLADM_STATUS_OK) 4655895Syz147064 goto done; 466*10491SRishi.Srivatsavai@Sun.COM conf_set = B_TRUE; 4675895Syz147064 4688275SEric Cheng if (proplist != NULL) { 4698275SEric Cheng for (i = 0; i < proplist->al_count; i++) { 4708275SEric Cheng dladm_arg_info_t *aip = &proplist->al_info[i]; 4718275SEric Cheng 4728453SAnurag.Maskey@Sun.COM status = dladm_set_linkprop(handle, vnic_id, 4738453SAnurag.Maskey@Sun.COM aip->ai_name, aip->ai_val, aip->ai_count, 4748453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 4758275SEric Cheng if (status != DLADM_STATUS_OK) 4768275SEric Cheng break; 4778275SEric Cheng } 4788275SEric Cheng } 4795084Sjohnlev 4805895Syz147064 done: 4815895Syz147064 if (status != DLADM_STATUS_OK) { 482*10491SRishi.Srivatsavai@Sun.COM if (conf_set) 483*10491SRishi.Srivatsavai@Sun.COM (void) dladm_remove_conf(handle, vnic_id); 484*10491SRishi.Srivatsavai@Sun.COM if (vnic_created) 485*10491SRishi.Srivatsavai@Sun.COM (void) i_dladm_vnic_delete_sys(handle, vnic_id); 4868453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, vnic_id, flags); 4875895Syz147064 } else { 4888275SEric Cheng if (vnic_id_out != NULL) 4898275SEric Cheng *vnic_id_out = vnic_id; 4908275SEric Cheng if (mac_slot != NULL) 4918275SEric Cheng *mac_slot = attr.va_mac_slot; 4925895Syz147064 } 493*10491SRishi.Srivatsavai@Sun.COM 494*10491SRishi.Srivatsavai@Sun.COM if (is_vlan) { 495*10491SRishi.Srivatsavai@Sun.COM dladm_status_t stat2; 496*10491SRishi.Srivatsavai@Sun.COM 497*10491SRishi.Srivatsavai@Sun.COM stat2 = dladm_bridge_refresh(handle, linkid); 498*10491SRishi.Srivatsavai@Sun.COM if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK) 499*10491SRishi.Srivatsavai@Sun.COM status = stat2; 500*10491SRishi.Srivatsavai@Sun.COM } 5015084Sjohnlev return (status); 5025084Sjohnlev } 5035084Sjohnlev 5045084Sjohnlev /* 5058275SEric Cheng * Delete a VNIC / VLAN. 5065084Sjohnlev */ 5075084Sjohnlev dladm_status_t 5088453SAnurag.Maskey@Sun.COM dladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 5095084Sjohnlev { 5108275SEric Cheng dladm_status_t status; 5118275SEric Cheng datalink_class_t class; 512*10491SRishi.Srivatsavai@Sun.COM dladm_vnic_attr_t attr; 5138275SEric Cheng 5148275SEric Cheng if (flags == 0) 5158275SEric Cheng return (DLADM_STATUS_BADARG); 5165084Sjohnlev 5178453SAnurag.Maskey@Sun.COM if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0) 5188453SAnurag.Maskey@Sun.COM != DLADM_STATUS_OK)) 5198275SEric Cheng return (DLADM_STATUS_BADARG); 5205084Sjohnlev 5218275SEric Cheng if ((flags & DLADM_OPT_VLAN) != 0) { 5228275SEric Cheng if (class != DATALINK_CLASS_VLAN) 5238275SEric Cheng return (DLADM_STATUS_BADARG); 5248275SEric Cheng } else { 5258275SEric Cheng if (class != DATALINK_CLASS_VNIC && 5268275SEric Cheng class != DATALINK_CLASS_ETHERSTUB) 5278275SEric Cheng return (DLADM_STATUS_BADARG); 5285084Sjohnlev } 5295084Sjohnlev 5308275SEric Cheng if ((flags & DLADM_OPT_ACTIVE) != 0) { 531*10491SRishi.Srivatsavai@Sun.COM status = dladm_vnic_info(handle, linkid, &attr, 532*10491SRishi.Srivatsavai@Sun.COM DLADM_OPT_ACTIVE); 533*10491SRishi.Srivatsavai@Sun.COM if (status != DLADM_STATUS_OK) 534*10491SRishi.Srivatsavai@Sun.COM return (status); 5358453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_delete_sys(handle, linkid); 5368275SEric Cheng if (status == DLADM_STATUS_OK) { 5378453SAnurag.Maskey@Sun.COM (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0, 5388275SEric Cheng DLADM_OPT_ACTIVE); 5398453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, linkid, 5408275SEric Cheng DLADM_OPT_ACTIVE); 5418275SEric Cheng } else if (status != DLADM_STATUS_NOTFOUND || 5428275SEric Cheng !(flags & DLADM_OPT_PERSIST)) { 5438275SEric Cheng return (status); 5448275SEric Cheng } 5458275SEric Cheng } 5468275SEric Cheng if ((flags & DLADM_OPT_PERSIST) != 0) { 5478453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, linkid, 5488453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 5498453SAnurag.Maskey@Sun.COM (void) dladm_remove_conf(handle, linkid); 5508275SEric Cheng } 551*10491SRishi.Srivatsavai@Sun.COM return (dladm_bridge_refresh(handle, linkid)); 5528275SEric Cheng } 5538275SEric Cheng 5548275SEric Cheng static const char * 5558275SEric Cheng dladm_vnic_macaddr2str(const uchar_t *mac, char *buf) 5568275SEric Cheng { 5578275SEric Cheng static char unknown_mac[] = {0, 0, 0, 0, 0, 0}; 5588275SEric Cheng 5598275SEric Cheng if (buf == NULL) 5608275SEric Cheng return (NULL); 5618275SEric Cheng 5628275SEric Cheng if (bcmp(unknown_mac, mac, ETHERADDRL) == 0) 5638275SEric Cheng (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 5648275SEric Cheng else 5658275SEric Cheng return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER)); 5668275SEric Cheng 5678275SEric Cheng return (buf); 5688275SEric Cheng } 5698275SEric Cheng 5708275SEric Cheng static dladm_status_t 5718275SEric Cheng dladm_vnic_str2macaddr(const char *str, uchar_t *buf) 5728275SEric Cheng { 5738275SEric Cheng int len = 0; 5748275SEric Cheng uchar_t *b = _link_aton(str, &len); 5758275SEric Cheng 5768275SEric Cheng if (b == NULL || len >= MAXMACADDRLEN) 5778275SEric Cheng return (DLADM_STATUS_BADARG); 5788275SEric Cheng 5798275SEric Cheng bcopy(b, buf, len); 5808275SEric Cheng free(b); 5818275SEric Cheng return (DLADM_STATUS_OK); 5825084Sjohnlev } 5835084Sjohnlev 5845084Sjohnlev 5858275SEric Cheng static dladm_status_t 5868453SAnurag.Maskey@Sun.COM dladm_vnic_persist_conf(dladm_handle_t handle, const char *name, 5878453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp, datalink_class_t class) 5888275SEric Cheng { 5898275SEric Cheng dladm_conf_t conf = DLADM_INVALID_CONF; 5908275SEric Cheng dladm_status_t status; 5918275SEric Cheng char macstr[ETHERADDRL * 3]; 5928275SEric Cheng uint64_t u64; 5935084Sjohnlev 5948453SAnurag.Maskey@Sun.COM if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id, 5958275SEric Cheng class, DL_ETHER, &conf)) != DLADM_STATUS_OK) 5965895Syz147064 return (status); 5975895Syz147064 5988275SEric Cheng if (attrp->va_link_id != DATALINK_INVALID_LINKID) { 5998275SEric Cheng u64 = attrp->va_link_id; 6008453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FLINKOVER, 6018275SEric Cheng DLADM_TYPE_UINT64, &u64); 6028275SEric Cheng if (status != DLADM_STATUS_OK) 6038275SEric Cheng goto done; 6048275SEric Cheng } 6058275SEric Cheng 6068275SEric Cheng if (class != DATALINK_CLASS_VLAN) { 6078275SEric Cheng u64 = attrp->va_mac_addr_type; 6088453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRTYPE, 6098275SEric Cheng DLADM_TYPE_UINT64, &u64); 6108275SEric Cheng if (status != DLADM_STATUS_OK) 6118275SEric Cheng goto done; 6128275SEric Cheng 6138275SEric Cheng if (attrp->va_mac_len != ETHERADDRL) { 6148275SEric Cheng u64 = attrp->va_mac_len; 6158453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRLEN, 6168275SEric Cheng DLADM_TYPE_UINT64, &u64); 6178275SEric Cheng if (status != DLADM_STATUS_OK) 6188275SEric Cheng goto done; 6198275SEric Cheng } 6208275SEric Cheng } 6218275SEric Cheng 6228275SEric Cheng if (attrp->va_hwrings) { 6238275SEric Cheng boolean_t hwrings = attrp->va_hwrings; 6248453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FHWRINGS, 6258275SEric Cheng DLADM_TYPE_BOOLEAN, &hwrings); 6268275SEric Cheng if (status != DLADM_STATUS_OK) 6278275SEric Cheng goto done; 6288275SEric Cheng } 6298275SEric Cheng 6308275SEric Cheng if (class != DATALINK_CLASS_VLAN) { 6318275SEric Cheng if (attrp->va_mac_slot != -1) { 6328275SEric Cheng u64 = attrp->va_mac_slot; 6338453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRSLOT, 6348275SEric Cheng DLADM_TYPE_UINT64, &u64); 6358275SEric Cheng if (status != DLADM_STATUS_OK) 6368275SEric Cheng goto done; 6378275SEric Cheng } 6388275SEric Cheng 6398275SEric Cheng if (attrp->va_mac_prefix_len != 6408275SEric Cheng sizeof (dladm_vnic_def_prefix)) { 6418275SEric Cheng u64 = attrp->va_mac_prefix_len; 6428453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, 6438453SAnurag.Maskey@Sun.COM FMADDRPREFIXLEN, DLADM_TYPE_UINT64, &u64); 6448275SEric Cheng if (status != DLADM_STATUS_OK) 6458275SEric Cheng goto done; 6468275SEric Cheng } 6478275SEric Cheng 6488275SEric Cheng (void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr); 6498453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMACADDR, 6508453SAnurag.Maskey@Sun.COM DLADM_TYPE_STR, macstr); 6518275SEric Cheng if (status != DLADM_STATUS_OK) 6528275SEric Cheng goto done; 6538275SEric Cheng } 6548275SEric Cheng 6558275SEric Cheng if (attrp->va_vid != 0) { 6568275SEric Cheng u64 = attrp->va_vid; 6578453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FVLANID, 6588275SEric Cheng DLADM_TYPE_UINT64, &u64); 6598275SEric Cheng if (status != DLADM_STATUS_OK) 6608275SEric Cheng goto done; 6618275SEric Cheng } 6628275SEric Cheng 6638275SEric Cheng /* 6648275SEric Cheng * Commit the link configuration. 6658275SEric Cheng */ 6668453SAnurag.Maskey@Sun.COM status = dladm_write_conf(handle, conf); 6678275SEric Cheng 6688275SEric Cheng done: 6698453SAnurag.Maskey@Sun.COM dladm_destroy_conf(handle, conf); 6705895Syz147064 return (status); 6715084Sjohnlev } 6728275SEric Cheng 6738275SEric Cheng typedef struct dladm_vnic_up_arg_s { 6748275SEric Cheng uint32_t flags; 6758275SEric Cheng dladm_status_t status; 6768275SEric Cheng } dladm_vnic_up_arg_t; 6778275SEric Cheng 6788275SEric Cheng #define DLADM_VNIC_UP_FIRST_WALK 0x1 6798275SEric Cheng #define DLADM_VNIC_UP_SECOND_WALK 0x2 6808275SEric Cheng 6818275SEric Cheng static int 6828453SAnurag.Maskey@Sun.COM i_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg) 6838275SEric Cheng { 6848275SEric Cheng dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status); 6858275SEric Cheng dladm_vnic_attr_t attr; 6868275SEric Cheng dladm_status_t status; 6878275SEric Cheng dladm_arg_list_t *proplist; 6888275SEric Cheng uint32_t flags = ((dladm_vnic_up_arg_t *)arg)->flags; 6898275SEric Cheng 6908275SEric Cheng bzero(&attr, sizeof (attr)); 6918275SEric Cheng 6928453SAnurag.Maskey@Sun.COM status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST); 6938275SEric Cheng if (status != DLADM_STATUS_OK) 6948275SEric Cheng goto done; 6958275SEric Cheng 6968275SEric Cheng /* 6978275SEric Cheng * Create the vnics that request hardware group first 6988275SEric Cheng * Create the vnics that don't request hardware group in the second walk 6998275SEric Cheng */ 7008275SEric Cheng if ((flags == DLADM_VNIC_UP_FIRST_WALK && !attr.va_hwrings) || 7018275SEric Cheng (flags == DLADM_VNIC_UP_SECOND_WALK && attr.va_hwrings)) 7028275SEric Cheng goto done; 7038275SEric Cheng 7048275SEric Cheng /* Get all properties for this vnic */ 7058453SAnurag.Maskey@Sun.COM status = dladm_link_get_proplist(handle, linkid, &proplist); 7068275SEric Cheng if (status != DLADM_STATUS_OK) 7078275SEric Cheng goto done; 7088275SEric Cheng 7098275SEric Cheng if (proplist != NULL) { 7108453SAnurag.Maskey@Sun.COM status = dladm_link_proplist_extract(handle, proplist, 7118275SEric Cheng &attr.va_resource_props); 7128275SEric Cheng } 7138275SEric Cheng 7148453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_create_sys(handle, &attr); 715*10491SRishi.Srivatsavai@Sun.COM if (status == DLADM_STATUS_OK) { 716*10491SRishi.Srivatsavai@Sun.COM status = dladm_up_datalink_id(handle, linkid); 717*10491SRishi.Srivatsavai@Sun.COM if (status != DLADM_STATUS_OK) 718*10491SRishi.Srivatsavai@Sun.COM (void) i_dladm_vnic_delete_sys(handle, linkid); 719*10491SRishi.Srivatsavai@Sun.COM } 7208275SEric Cheng 7218275SEric Cheng done: 7228275SEric Cheng *statusp = status; 7238275SEric Cheng return (DLADM_WALK_CONTINUE); 7248275SEric Cheng } 7258275SEric Cheng 7268275SEric Cheng dladm_status_t 7278453SAnurag.Maskey@Sun.COM dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 7288275SEric Cheng { 7298275SEric Cheng dladm_vnic_up_arg_t vnic_arg; 7308275SEric Cheng datalink_class_t class; 7318275SEric Cheng 7328275SEric Cheng class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN : 7338275SEric Cheng (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB); 7348275SEric Cheng 7358275SEric Cheng if (linkid == DATALINK_ALL_LINKID) { 7368275SEric Cheng vnic_arg.flags = DLADM_VNIC_UP_FIRST_WALK; 7378453SAnurag.Maskey@Sun.COM (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle, 7388453SAnurag.Maskey@Sun.COM &vnic_arg, class, DATALINK_ANY_MEDIATYPE, 7398453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 7408275SEric Cheng vnic_arg.flags = DLADM_VNIC_UP_SECOND_WALK; 7418453SAnurag.Maskey@Sun.COM (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle, 7428453SAnurag.Maskey@Sun.COM &vnic_arg, class, DATALINK_ANY_MEDIATYPE, 7438453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 7448275SEric Cheng return (DLADM_STATUS_OK); 7458275SEric Cheng } else { 7468453SAnurag.Maskey@Sun.COM (void) i_dladm_vnic_up(handle, linkid, &vnic_arg); 7478275SEric Cheng return (vnic_arg.status); 7488275SEric Cheng } 7498275SEric Cheng } 750