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 /* 2210491SRishi.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> 4210491SRishi.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]; 190*10616SSebastien.Roy@Sun.COM char linkover[MAXLINKNAMELEN]; 1918275SEric Cheng uint64_t u64; 1928275SEric Cheng datalink_class_t class; 1938275SEric Cheng 1948275SEric Cheng attrp->va_vnic_id = linkid; 1958453SAnurag.Maskey@Sun.COM if ((status = dladm_read_conf(handle, linkid, &conf)) != 1968453SAnurag.Maskey@Sun.COM DLADM_STATUS_OK) 1978275SEric Cheng return (status); 1988275SEric Cheng 199*10616SSebastien.Roy@Sun.COM status = dladm_get_conf_field(handle, conf, FLINKOVER, linkover, 200*10616SSebastien.Roy@Sun.COM sizeof (linkover)); 201*10616SSebastien.Roy@Sun.COM if (status != DLADM_STATUS_OK) { 202*10616SSebastien.Roy@Sun.COM /* 203*10616SSebastien.Roy@Sun.COM * This isn't an error, etherstubs don't have a FLINKOVER 204*10616SSebastien.Roy@Sun.COM * property. 205*10616SSebastien.Roy@Sun.COM */ 206*10616SSebastien.Roy@Sun.COM attrp->va_link_id = DATALINK_INVALID_LINKID; 207*10616SSebastien.Roy@Sun.COM } else { 208*10616SSebastien.Roy@Sun.COM if ((status = dladm_name2info(handle, linkover, 209*10616SSebastien.Roy@Sun.COM &attrp->va_link_id, NULL, NULL, NULL)) != DLADM_STATUS_OK) 210*10616SSebastien.Roy@Sun.COM goto done; 211*10616SSebastien.Roy@Sun.COM } 2128275SEric Cheng 2138453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FHWRINGS, 2148453SAnurag.Maskey@Sun.COM &attrp->va_hwrings, sizeof (boolean_t)); 2158275SEric Cheng 2168275SEric Cheng if (status != DLADM_STATUS_OK && status != DLADM_STATUS_NOTFOUND) 2178275SEric Cheng goto done; 2188275SEric Cheng if (status == DLADM_STATUS_NOTFOUND) 2198275SEric Cheng attrp->va_hwrings = B_FALSE; 2208275SEric Cheng 2218453SAnurag.Maskey@Sun.COM if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class, 2228275SEric Cheng NULL, NULL, 0)) != DLADM_STATUS_OK) 2238275SEric Cheng goto done; 2248275SEric Cheng 2258275SEric Cheng if (class == DATALINK_CLASS_VLAN) { 2268275SEric Cheng if (attrp->va_link_id == DATALINK_INVALID_LINKID) { 2278275SEric Cheng status = DLADM_STATUS_BADARG; 2288275SEric Cheng goto done; 2298275SEric Cheng } 2308275SEric Cheng attrp->va_mac_addr_type = VNIC_MAC_ADDR_TYPE_PRIMARY; 2318275SEric Cheng attrp->va_mac_len = 0; 2328275SEric Cheng } else { 2338453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRTYPE, &u64, 2348275SEric Cheng sizeof (u64)); 2358275SEric Cheng if (status != DLADM_STATUS_OK) 2368275SEric Cheng goto done; 2378275SEric Cheng 2388275SEric Cheng attrp->va_mac_addr_type = (vnic_mac_addr_type_t)u64; 2398275SEric Cheng 2408453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRLEN, &u64, 2418275SEric Cheng sizeof (u64)); 2428275SEric Cheng attrp->va_mac_len = ((status == DLADM_STATUS_OK) ? 2438275SEric Cheng (uint_t)u64 : ETHERADDRL); 2448275SEric Cheng 2458453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRSLOT, &u64, 2468275SEric Cheng sizeof (u64)); 2478275SEric Cheng attrp->va_mac_slot = ((status == DLADM_STATUS_OK) ? 2488275SEric Cheng (int)u64 : -1); 2498275SEric Cheng 2508453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMADDRPREFIXLEN, 2518453SAnurag.Maskey@Sun.COM &u64, sizeof (u64)); 2528275SEric Cheng attrp->va_mac_prefix_len = ((status == DLADM_STATUS_OK) ? 2538275SEric Cheng (uint_t)u64 : sizeof (dladm_vnic_def_prefix)); 2548275SEric Cheng 2558453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FMACADDR, macstr, 2568275SEric Cheng sizeof (macstr)); 2578275SEric Cheng if (status != DLADM_STATUS_OK) 2588275SEric Cheng goto done; 2598275SEric Cheng 2608275SEric Cheng status = dladm_vnic_str2macaddr(macstr, attrp->va_mac_addr); 2618275SEric Cheng if (status != DLADM_STATUS_OK) 2628275SEric Cheng goto done; 2638275SEric Cheng } 2648275SEric Cheng 2658453SAnurag.Maskey@Sun.COM status = dladm_get_conf_field(handle, conf, FVLANID, &u64, 2668453SAnurag.Maskey@Sun.COM sizeof (u64)); 2678275SEric Cheng attrp->va_vid = ((status == DLADM_STATUS_OK) ? (uint16_t)u64 : 0); 2688275SEric Cheng 2698275SEric Cheng 2708275SEric Cheng status = DLADM_STATUS_OK; 2718275SEric Cheng done: 2728453SAnurag.Maskey@Sun.COM dladm_destroy_conf(handle, conf); 2738275SEric Cheng return (status); 2748275SEric Cheng } 2758275SEric Cheng 2768275SEric Cheng dladm_status_t 2778453SAnurag.Maskey@Sun.COM dladm_vnic_info(dladm_handle_t handle, datalink_id_t linkid, 2788453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp, uint32_t flags) 2798275SEric Cheng { 2808275SEric Cheng if (flags == DLADM_OPT_ACTIVE) 2818453SAnurag.Maskey@Sun.COM return (i_dladm_vnic_info_active(handle, linkid, attrp)); 2828275SEric Cheng else if (flags == DLADM_OPT_PERSIST) 2838453SAnurag.Maskey@Sun.COM return (i_dladm_vnic_info_persist(handle, linkid, attrp)); 2848275SEric Cheng else 2858275SEric Cheng return (DLADM_STATUS_BADARG); 2868275SEric Cheng } 2878275SEric Cheng 2888275SEric Cheng /* 2898275SEric Cheng * Remove a VNIC from the kernel. 2908275SEric Cheng */ 2918275SEric Cheng dladm_status_t 2928453SAnurag.Maskey@Sun.COM i_dladm_vnic_delete_sys(dladm_handle_t handle, datalink_id_t linkid) 2938275SEric Cheng { 2948275SEric Cheng vnic_ioc_delete_t ioc; 2958275SEric Cheng dladm_status_t status = DLADM_STATUS_OK; 2968453SAnurag.Maskey@Sun.COM int rc; 2978275SEric Cheng 2988275SEric Cheng ioc.vd_vnic_id = linkid; 2998275SEric Cheng 3008453SAnurag.Maskey@Sun.COM rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_DELETE, &ioc); 3018275SEric Cheng if (rc < 0) 3027408SSebastien.Roy@Sun.COM status = dladm_errno2status(errno); 3035084Sjohnlev 3047408SSebastien.Roy@Sun.COM return (status); 3055084Sjohnlev } 3065084Sjohnlev 3075084Sjohnlev /* 3085084Sjohnlev * Convert between MAC address types and their string representations. 3095084Sjohnlev */ 3105084Sjohnlev 3115084Sjohnlev typedef struct dladm_vnic_addr_type_s { 3128275SEric Cheng const char *va_str; 3138275SEric Cheng vnic_mac_addr_type_t va_type; 3145084Sjohnlev } dladm_vnic_addr_type_t; 3155084Sjohnlev 3165084Sjohnlev static dladm_vnic_addr_type_t addr_types[] = { 3175084Sjohnlev {"fixed", VNIC_MAC_ADDR_TYPE_FIXED}, 3188275SEric Cheng {"random", VNIC_MAC_ADDR_TYPE_RANDOM}, 3198275SEric Cheng {"factory", VNIC_MAC_ADDR_TYPE_FACTORY}, 3208275SEric Cheng {"auto", VNIC_MAC_ADDR_TYPE_AUTO}, 3218275SEric Cheng {"fixed", VNIC_MAC_ADDR_TYPE_PRIMARY} 3225084Sjohnlev }; 3235084Sjohnlev 3245084Sjohnlev #define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t)) 3255084Sjohnlev 3268275SEric Cheng static const char * 3278275SEric Cheng dladm_vnic_macaddrtype2str(vnic_mac_addr_type_t type) 3288275SEric Cheng { 3298275SEric Cheng int i; 3308275SEric Cheng 3318275SEric Cheng for (i = 0; i < NADDR_TYPES; i++) { 3328275SEric Cheng if (type == addr_types[i].va_type) 3338275SEric Cheng return (addr_types[i].va_str); 3348275SEric Cheng } 3358275SEric Cheng return (NULL); 3368275SEric Cheng } 3378275SEric Cheng 3385895Syz147064 dladm_status_t 3395895Syz147064 dladm_vnic_str2macaddrtype(const char *str, vnic_mac_addr_type_t *val) 3405084Sjohnlev { 3415084Sjohnlev int i; 3425084Sjohnlev dladm_vnic_addr_type_t *type; 3435084Sjohnlev 3445084Sjohnlev for (i = 0; i < NADDR_TYPES; i++) { 3455084Sjohnlev type = &addr_types[i]; 3465084Sjohnlev if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) { 3475084Sjohnlev *val = type->va_type; 3485895Syz147064 return (DLADM_STATUS_OK); 3495084Sjohnlev } 3505084Sjohnlev } 3515895Syz147064 return (DLADM_STATUS_BADARG); 3525084Sjohnlev } 3535084Sjohnlev 3545084Sjohnlev /* 3558275SEric Cheng * Create a new VNIC / VLAN. Update the configuration file and bring it up. 3565084Sjohnlev */ 3575084Sjohnlev dladm_status_t 3588453SAnurag.Maskey@Sun.COM dladm_vnic_create(dladm_handle_t handle, const char *vnic, datalink_id_t linkid, 3595084Sjohnlev vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, int mac_len, 3608275SEric Cheng int *mac_slot, uint_t mac_prefix_len, uint16_t vid, 3618275SEric Cheng datalink_id_t *vnic_id_out, dladm_arg_list_t *proplist, uint32_t flags) 3625084Sjohnlev { 3638275SEric Cheng dladm_vnic_attr_t attr; 3645895Syz147064 datalink_id_t vnic_id; 3655895Syz147064 datalink_class_t class; 3668275SEric Cheng uint32_t media = DL_ETHER; 3678275SEric Cheng char name[MAXLINKNAMELEN]; 3688275SEric Cheng uchar_t tmp_addr[MAXMACADDRLEN]; 3695084Sjohnlev dladm_status_t status; 3708275SEric Cheng boolean_t is_vlan; 3718275SEric Cheng boolean_t is_etherstub; 3728275SEric Cheng int i; 37310491SRishi.Srivatsavai@Sun.COM boolean_t vnic_created = B_FALSE; 37410491SRishi.Srivatsavai@Sun.COM boolean_t conf_set = B_FALSE; 3755084Sjohnlev 3765084Sjohnlev /* 3775084Sjohnlev * Sanity test arguments. 3785084Sjohnlev */ 3798275SEric Cheng if ((flags & DLADM_OPT_ACTIVE) == 0) 3808275SEric Cheng return (DLADM_STATUS_NOTSUP); 3818275SEric Cheng 3828275SEric Cheng is_vlan = ((flags & DLADM_OPT_VLAN) != 0); 3838275SEric Cheng if (is_vlan && ((vid < 1 || vid > 4094))) 3848275SEric Cheng return (DLADM_STATUS_VIDINVAL); 3858275SEric Cheng 3868275SEric Cheng is_etherstub = (linkid == DATALINK_INVALID_LINKID); 3875084Sjohnlev 3885084Sjohnlev if (mac_len > MAXMACADDRLEN) 3895084Sjohnlev return (DLADM_STATUS_INVALIDMACADDRLEN); 3905084Sjohnlev 3918275SEric Cheng if (!dladm_vnic_macaddrtype2str(mac_addr_type)) 3925084Sjohnlev return (DLADM_STATUS_INVALIDMACADDRTYPE); 3935084Sjohnlev 3948275SEric Cheng /* 3958275SEric Cheng * If a random address might be generated, but no prefix 3968275SEric Cheng * was specified by the caller, use the default MAC address 3978275SEric Cheng * prefix. 3988275SEric Cheng */ 3998275SEric Cheng if ((mac_addr_type == VNIC_MAC_ADDR_TYPE_RANDOM || 4008275SEric Cheng mac_addr_type == VNIC_MAC_ADDR_TYPE_AUTO) && 4018275SEric Cheng mac_prefix_len == 0) { 4028275SEric Cheng mac_prefix_len = sizeof (dladm_vnic_def_prefix); 4038275SEric Cheng mac_addr = tmp_addr; 4048275SEric Cheng bcopy(dladm_vnic_def_prefix, mac_addr, mac_prefix_len); 4055895Syz147064 } 4065895Syz147064 4078275SEric Cheng if ((flags & DLADM_OPT_ANCHOR) == 0) { 4088453SAnurag.Maskey@Sun.COM if ((status = dladm_datalink_id2info(handle, linkid, NULL, 4098453SAnurag.Maskey@Sun.COM &class, &media, NULL, 0)) != DLADM_STATUS_OK) 4108275SEric Cheng return (status); 4118275SEric Cheng 4128275SEric Cheng if (class == DATALINK_CLASS_VNIC || 4138275SEric Cheng class == DATALINK_CLASS_VLAN) 4148275SEric Cheng return (DLADM_STATUS_BADARG); 4158275SEric Cheng } else { 4168275SEric Cheng /* it's an anchor VNIC */ 4178275SEric Cheng if (linkid != DATALINK_INVALID_LINKID || vid != 0) 4188275SEric Cheng return (DLADM_STATUS_BADARG); 4198275SEric Cheng } 4205084Sjohnlev 4215895Syz147064 if (vnic == NULL) { 4225895Syz147064 flags |= DLADM_OPT_PREFIX; 4238275SEric Cheng (void) strlcpy(name, "vnic", sizeof (name)); 4248275SEric Cheng } else { 4258275SEric Cheng (void) strlcpy(name, vnic, sizeof (name)); 4265895Syz147064 } 4275895Syz147064 4288275SEric Cheng class = is_vlan ? DATALINK_CLASS_VLAN : 4298275SEric Cheng (is_etherstub ? DATALINK_CLASS_ETHERSTUB : DATALINK_CLASS_VNIC); 4308453SAnurag.Maskey@Sun.COM if ((status = dladm_create_datalink_id(handle, name, class, 4318275SEric Cheng media, flags, &vnic_id)) != DLADM_STATUS_OK) 4325895Syz147064 return (status); 4338275SEric Cheng 4348275SEric Cheng if ((flags & DLADM_OPT_PREFIX) != 0) { 4358275SEric Cheng (void) snprintf(name + 4, sizeof (name), "%llu", vnic_id); 4368275SEric Cheng flags &= ~DLADM_OPT_PREFIX; 4375084Sjohnlev } 4385084Sjohnlev 4395084Sjohnlev bzero(&attr, sizeof (attr)); 4408275SEric Cheng 4418275SEric Cheng /* Extract resource_ctl and cpu_list from proplist */ 4428275SEric Cheng if (proplist != NULL) { 4438453SAnurag.Maskey@Sun.COM status = dladm_link_proplist_extract(handle, proplist, 4448275SEric Cheng &attr.va_resource_props); 4458275SEric Cheng if (status != DLADM_STATUS_OK) 4468275SEric Cheng goto done; 4478275SEric Cheng } 4485084Sjohnlev 4498275SEric Cheng attr.va_vnic_id = vnic_id; 4508275SEric Cheng attr.va_link_id = linkid; 4518275SEric Cheng attr.va_mac_addr_type = mac_addr_type; 4528275SEric Cheng attr.va_mac_len = mac_len; 4538275SEric Cheng if (mac_slot != NULL) 4548275SEric Cheng attr.va_mac_slot = *mac_slot; 4558275SEric Cheng if (mac_len > 0) 4568275SEric Cheng bcopy(mac_addr, attr.va_mac_addr, mac_len); 4578275SEric Cheng else if (mac_prefix_len > 0) 4588275SEric Cheng bcopy(mac_addr, attr.va_mac_addr, mac_prefix_len); 4598275SEric Cheng attr.va_mac_prefix_len = mac_prefix_len; 4608275SEric Cheng attr.va_vid = vid; 4618275SEric Cheng attr.va_force = (flags & DLADM_OPT_FORCE) != 0; 4628275SEric Cheng attr.va_hwrings = (flags & DLADM_OPT_HWRINGS) != 0; 4638275SEric Cheng 4648453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_create_sys(handle, &attr); 4658275SEric Cheng if (status != DLADM_STATUS_OK) 4668275SEric Cheng goto done; 46710491SRishi.Srivatsavai@Sun.COM vnic_created = B_TRUE; 4688275SEric Cheng 4698275SEric Cheng /* Save vnic configuration and its properties */ 4708275SEric Cheng if (!(flags & DLADM_OPT_PERSIST)) 4718275SEric Cheng goto done; 4728275SEric Cheng 4738453SAnurag.Maskey@Sun.COM status = dladm_vnic_persist_conf(handle, name, &attr, class); 47410491SRishi.Srivatsavai@Sun.COM if (status != DLADM_STATUS_OK) 4755895Syz147064 goto done; 47610491SRishi.Srivatsavai@Sun.COM conf_set = B_TRUE; 4775895Syz147064 4788275SEric Cheng if (proplist != NULL) { 4798275SEric Cheng for (i = 0; i < proplist->al_count; i++) { 4808275SEric Cheng dladm_arg_info_t *aip = &proplist->al_info[i]; 4818275SEric Cheng 4828453SAnurag.Maskey@Sun.COM status = dladm_set_linkprop(handle, vnic_id, 4838453SAnurag.Maskey@Sun.COM aip->ai_name, aip->ai_val, aip->ai_count, 4848453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 4858275SEric Cheng if (status != DLADM_STATUS_OK) 4868275SEric Cheng break; 4878275SEric Cheng } 4888275SEric Cheng } 4895084Sjohnlev 4905895Syz147064 done: 4915895Syz147064 if (status != DLADM_STATUS_OK) { 49210491SRishi.Srivatsavai@Sun.COM if (conf_set) 49310491SRishi.Srivatsavai@Sun.COM (void) dladm_remove_conf(handle, vnic_id); 49410491SRishi.Srivatsavai@Sun.COM if (vnic_created) 49510491SRishi.Srivatsavai@Sun.COM (void) i_dladm_vnic_delete_sys(handle, vnic_id); 4968453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, vnic_id, flags); 4975895Syz147064 } else { 4988275SEric Cheng if (vnic_id_out != NULL) 4998275SEric Cheng *vnic_id_out = vnic_id; 5008275SEric Cheng if (mac_slot != NULL) 5018275SEric Cheng *mac_slot = attr.va_mac_slot; 5025895Syz147064 } 50310491SRishi.Srivatsavai@Sun.COM 50410491SRishi.Srivatsavai@Sun.COM if (is_vlan) { 50510491SRishi.Srivatsavai@Sun.COM dladm_status_t stat2; 50610491SRishi.Srivatsavai@Sun.COM 50710491SRishi.Srivatsavai@Sun.COM stat2 = dladm_bridge_refresh(handle, linkid); 50810491SRishi.Srivatsavai@Sun.COM if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK) 50910491SRishi.Srivatsavai@Sun.COM status = stat2; 51010491SRishi.Srivatsavai@Sun.COM } 5115084Sjohnlev return (status); 5125084Sjohnlev } 5135084Sjohnlev 5145084Sjohnlev /* 5158275SEric Cheng * Delete a VNIC / VLAN. 5165084Sjohnlev */ 5175084Sjohnlev dladm_status_t 5188453SAnurag.Maskey@Sun.COM dladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 5195084Sjohnlev { 5208275SEric Cheng dladm_status_t status; 5218275SEric Cheng datalink_class_t class; 5228275SEric Cheng 5238275SEric Cheng if (flags == 0) 5248275SEric Cheng return (DLADM_STATUS_BADARG); 5255084Sjohnlev 5268453SAnurag.Maskey@Sun.COM if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0) 5278453SAnurag.Maskey@Sun.COM != DLADM_STATUS_OK)) 5288275SEric Cheng return (DLADM_STATUS_BADARG); 5295084Sjohnlev 5308275SEric Cheng if ((flags & DLADM_OPT_VLAN) != 0) { 5318275SEric Cheng if (class != DATALINK_CLASS_VLAN) 5328275SEric Cheng return (DLADM_STATUS_BADARG); 5338275SEric Cheng } else { 5348275SEric Cheng if (class != DATALINK_CLASS_VNIC && 5358275SEric Cheng class != DATALINK_CLASS_ETHERSTUB) 5368275SEric Cheng return (DLADM_STATUS_BADARG); 5375084Sjohnlev } 5385084Sjohnlev 5398275SEric Cheng if ((flags & DLADM_OPT_ACTIVE) != 0) { 5408453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_delete_sys(handle, linkid); 5418275SEric Cheng if (status == DLADM_STATUS_OK) { 5428453SAnurag.Maskey@Sun.COM (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0, 5438275SEric Cheng DLADM_OPT_ACTIVE); 5448453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, linkid, 5458275SEric Cheng DLADM_OPT_ACTIVE); 5468275SEric Cheng } else if (status != DLADM_STATUS_NOTFOUND || 5478275SEric Cheng !(flags & DLADM_OPT_PERSIST)) { 5488275SEric Cheng return (status); 5498275SEric Cheng } 5508275SEric Cheng } 5518275SEric Cheng if ((flags & DLADM_OPT_PERSIST) != 0) { 552*10616SSebastien.Roy@Sun.COM (void) dladm_remove_conf(handle, linkid); 5538453SAnurag.Maskey@Sun.COM (void) dladm_destroy_datalink_id(handle, linkid, 5548453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 5558275SEric Cheng } 55610491SRishi.Srivatsavai@Sun.COM return (dladm_bridge_refresh(handle, linkid)); 5578275SEric Cheng } 5588275SEric Cheng 5598275SEric Cheng static const char * 5608275SEric Cheng dladm_vnic_macaddr2str(const uchar_t *mac, char *buf) 5618275SEric Cheng { 5628275SEric Cheng static char unknown_mac[] = {0, 0, 0, 0, 0, 0}; 5638275SEric Cheng 5648275SEric Cheng if (buf == NULL) 5658275SEric Cheng return (NULL); 5668275SEric Cheng 5678275SEric Cheng if (bcmp(unknown_mac, mac, ETHERADDRL) == 0) 5688275SEric Cheng (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 5698275SEric Cheng else 5708275SEric Cheng return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER)); 5718275SEric Cheng 5728275SEric Cheng return (buf); 5738275SEric Cheng } 5748275SEric Cheng 5758275SEric Cheng static dladm_status_t 5768275SEric Cheng dladm_vnic_str2macaddr(const char *str, uchar_t *buf) 5778275SEric Cheng { 5788275SEric Cheng int len = 0; 5798275SEric Cheng uchar_t *b = _link_aton(str, &len); 5808275SEric Cheng 5818275SEric Cheng if (b == NULL || len >= MAXMACADDRLEN) 5828275SEric Cheng return (DLADM_STATUS_BADARG); 5838275SEric Cheng 5848275SEric Cheng bcopy(b, buf, len); 5858275SEric Cheng free(b); 5868275SEric Cheng return (DLADM_STATUS_OK); 5875084Sjohnlev } 5885084Sjohnlev 5895084Sjohnlev 5908275SEric Cheng static dladm_status_t 5918453SAnurag.Maskey@Sun.COM dladm_vnic_persist_conf(dladm_handle_t handle, const char *name, 5928453SAnurag.Maskey@Sun.COM dladm_vnic_attr_t *attrp, datalink_class_t class) 5938275SEric Cheng { 5948275SEric Cheng dladm_conf_t conf = DLADM_INVALID_CONF; 5958275SEric Cheng dladm_status_t status; 5968275SEric Cheng char macstr[ETHERADDRL * 3]; 597*10616SSebastien.Roy@Sun.COM char linkover[MAXLINKNAMELEN]; 5988275SEric Cheng uint64_t u64; 5995084Sjohnlev 6008453SAnurag.Maskey@Sun.COM if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id, 6018275SEric Cheng class, DL_ETHER, &conf)) != DLADM_STATUS_OK) 6025895Syz147064 return (status); 6035895Syz147064 6048275SEric Cheng if (attrp->va_link_id != DATALINK_INVALID_LINKID) { 605*10616SSebastien.Roy@Sun.COM status = dladm_datalink_id2info(handle, attrp->va_link_id, NULL, 606*10616SSebastien.Roy@Sun.COM NULL, NULL, linkover, sizeof (linkover)); 607*10616SSebastien.Roy@Sun.COM if (status != DLADM_STATUS_OK) 608*10616SSebastien.Roy@Sun.COM goto done; 6098453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FLINKOVER, 610*10616SSebastien.Roy@Sun.COM DLADM_TYPE_STR, linkover); 6118275SEric Cheng if (status != DLADM_STATUS_OK) 6128275SEric Cheng goto done; 6138275SEric Cheng } 6148275SEric Cheng 6158275SEric Cheng if (class != DATALINK_CLASS_VLAN) { 6168275SEric Cheng u64 = attrp->va_mac_addr_type; 6178453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRTYPE, 6188275SEric Cheng DLADM_TYPE_UINT64, &u64); 6198275SEric Cheng if (status != DLADM_STATUS_OK) 6208275SEric Cheng goto done; 6218275SEric Cheng 6228275SEric Cheng if (attrp->va_mac_len != ETHERADDRL) { 6238275SEric Cheng u64 = attrp->va_mac_len; 6248453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRLEN, 6258275SEric Cheng DLADM_TYPE_UINT64, &u64); 6268275SEric Cheng if (status != DLADM_STATUS_OK) 6278275SEric Cheng goto done; 6288275SEric Cheng } 6298275SEric Cheng } 6308275SEric Cheng 6318275SEric Cheng if (attrp->va_hwrings) { 6328275SEric Cheng boolean_t hwrings = attrp->va_hwrings; 6338453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FHWRINGS, 6348275SEric Cheng DLADM_TYPE_BOOLEAN, &hwrings); 6358275SEric Cheng if (status != DLADM_STATUS_OK) 6368275SEric Cheng goto done; 6378275SEric Cheng } 6388275SEric Cheng 6398275SEric Cheng if (class != DATALINK_CLASS_VLAN) { 6408275SEric Cheng if (attrp->va_mac_slot != -1) { 6418275SEric Cheng u64 = attrp->va_mac_slot; 6428453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMADDRSLOT, 6438275SEric Cheng DLADM_TYPE_UINT64, &u64); 6448275SEric Cheng if (status != DLADM_STATUS_OK) 6458275SEric Cheng goto done; 6468275SEric Cheng } 6478275SEric Cheng 6488275SEric Cheng if (attrp->va_mac_prefix_len != 6498275SEric Cheng sizeof (dladm_vnic_def_prefix)) { 6508275SEric Cheng u64 = attrp->va_mac_prefix_len; 6518453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, 6528453SAnurag.Maskey@Sun.COM FMADDRPREFIXLEN, DLADM_TYPE_UINT64, &u64); 6538275SEric Cheng if (status != DLADM_STATUS_OK) 6548275SEric Cheng goto done; 6558275SEric Cheng } 6568275SEric Cheng 6578275SEric Cheng (void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr); 6588453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FMACADDR, 6598453SAnurag.Maskey@Sun.COM DLADM_TYPE_STR, macstr); 6608275SEric Cheng if (status != DLADM_STATUS_OK) 6618275SEric Cheng goto done; 6628275SEric Cheng } 6638275SEric Cheng 6648275SEric Cheng if (attrp->va_vid != 0) { 6658275SEric Cheng u64 = attrp->va_vid; 6668453SAnurag.Maskey@Sun.COM status = dladm_set_conf_field(handle, conf, FVLANID, 6678275SEric Cheng DLADM_TYPE_UINT64, &u64); 6688275SEric Cheng if (status != DLADM_STATUS_OK) 6698275SEric Cheng goto done; 6708275SEric Cheng } 6718275SEric Cheng 6728275SEric Cheng /* 6738275SEric Cheng * Commit the link configuration. 6748275SEric Cheng */ 6758453SAnurag.Maskey@Sun.COM status = dladm_write_conf(handle, conf); 6768275SEric Cheng 6778275SEric Cheng done: 6788453SAnurag.Maskey@Sun.COM dladm_destroy_conf(handle, conf); 6795895Syz147064 return (status); 6805084Sjohnlev } 6818275SEric Cheng 6828275SEric Cheng typedef struct dladm_vnic_up_arg_s { 6838275SEric Cheng uint32_t flags; 6848275SEric Cheng dladm_status_t status; 6858275SEric Cheng } dladm_vnic_up_arg_t; 6868275SEric Cheng 6878275SEric Cheng #define DLADM_VNIC_UP_FIRST_WALK 0x1 6888275SEric Cheng #define DLADM_VNIC_UP_SECOND_WALK 0x2 6898275SEric Cheng 6908275SEric Cheng static int 6918453SAnurag.Maskey@Sun.COM i_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg) 6928275SEric Cheng { 6938275SEric Cheng dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status); 6948275SEric Cheng dladm_vnic_attr_t attr; 6958275SEric Cheng dladm_status_t status; 6968275SEric Cheng dladm_arg_list_t *proplist; 6978275SEric Cheng uint32_t flags = ((dladm_vnic_up_arg_t *)arg)->flags; 6988275SEric Cheng 6998275SEric Cheng bzero(&attr, sizeof (attr)); 7008275SEric Cheng 7018453SAnurag.Maskey@Sun.COM status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST); 7028275SEric Cheng if (status != DLADM_STATUS_OK) 7038275SEric Cheng goto done; 7048275SEric Cheng 7058275SEric Cheng /* 7068275SEric Cheng * Create the vnics that request hardware group first 7078275SEric Cheng * Create the vnics that don't request hardware group in the second walk 7088275SEric Cheng */ 7098275SEric Cheng if ((flags == DLADM_VNIC_UP_FIRST_WALK && !attr.va_hwrings) || 7108275SEric Cheng (flags == DLADM_VNIC_UP_SECOND_WALK && attr.va_hwrings)) 7118275SEric Cheng goto done; 7128275SEric Cheng 7138275SEric Cheng /* Get all properties for this vnic */ 7148453SAnurag.Maskey@Sun.COM status = dladm_link_get_proplist(handle, linkid, &proplist); 7158275SEric Cheng if (status != DLADM_STATUS_OK) 7168275SEric Cheng goto done; 7178275SEric Cheng 7188275SEric Cheng if (proplist != NULL) { 7198453SAnurag.Maskey@Sun.COM status = dladm_link_proplist_extract(handle, proplist, 7208275SEric Cheng &attr.va_resource_props); 7218275SEric Cheng } 7228275SEric Cheng 7238453SAnurag.Maskey@Sun.COM status = i_dladm_vnic_create_sys(handle, &attr); 72410491SRishi.Srivatsavai@Sun.COM if (status == DLADM_STATUS_OK) { 72510491SRishi.Srivatsavai@Sun.COM status = dladm_up_datalink_id(handle, linkid); 72610491SRishi.Srivatsavai@Sun.COM if (status != DLADM_STATUS_OK) 72710491SRishi.Srivatsavai@Sun.COM (void) i_dladm_vnic_delete_sys(handle, linkid); 72810491SRishi.Srivatsavai@Sun.COM } 7298275SEric Cheng 7308275SEric Cheng done: 7318275SEric Cheng *statusp = status; 7328275SEric Cheng return (DLADM_WALK_CONTINUE); 7338275SEric Cheng } 7348275SEric Cheng 7358275SEric Cheng dladm_status_t 7368453SAnurag.Maskey@Sun.COM dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags) 7378275SEric Cheng { 7388275SEric Cheng dladm_vnic_up_arg_t vnic_arg; 7398275SEric Cheng datalink_class_t class; 7408275SEric Cheng 7418275SEric Cheng class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN : 7428275SEric Cheng (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB); 7438275SEric Cheng 7448275SEric Cheng if (linkid == DATALINK_ALL_LINKID) { 7458275SEric Cheng vnic_arg.flags = DLADM_VNIC_UP_FIRST_WALK; 7468453SAnurag.Maskey@Sun.COM (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle, 7478453SAnurag.Maskey@Sun.COM &vnic_arg, class, DATALINK_ANY_MEDIATYPE, 7488453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 7498275SEric Cheng vnic_arg.flags = DLADM_VNIC_UP_SECOND_WALK; 7508453SAnurag.Maskey@Sun.COM (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle, 7518453SAnurag.Maskey@Sun.COM &vnic_arg, class, DATALINK_ANY_MEDIATYPE, 7528453SAnurag.Maskey@Sun.COM DLADM_OPT_PERSIST); 7538275SEric Cheng return (DLADM_STATUS_OK); 7548275SEric Cheng } else { 7558453SAnurag.Maskey@Sun.COM (void) i_dladm_vnic_up(handle, linkid, &vnic_arg); 7568275SEric Cheng return (vnic_arg.status); 7578275SEric Cheng } 7588275SEric Cheng } 759