17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <fcntl.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <stropts.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 387c478bd9Sstevel@tonic-gate #include <libdlpi.h> 397c478bd9Sstevel@tonic-gate #include <libdladm.h> 407c478bd9Sstevel@tonic-gate #include <sys/dld.h> 417c478bd9Sstevel@tonic-gate #include <net/if.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * Issue an ioctl to the specified file descriptor attached to the 457c478bd9Sstevel@tonic-gate * DLD control driver interface. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate static int 48*210db224Sericheng i_dladm_ioctl(int fd, int ic_cmd, void *ic_dp, int ic_len) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate struct strioctl iocb; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate iocb.ic_cmd = ic_cmd; 537c478bd9Sstevel@tonic-gate iocb.ic_timout = 0; 547c478bd9Sstevel@tonic-gate iocb.ic_len = ic_len; 55*210db224Sericheng iocb.ic_dp = (char *)ic_dp; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate return (ioctl(fd, I_STR, &iocb)); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Return the attributes of the specified datalink from the DLD driver. 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate static int 647c478bd9Sstevel@tonic-gate i_dladm_info(int fd, const char *name, dladm_attr_t *dap) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate dld_ioc_attr_t dia; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate if (strlen(name) >= IFNAMSIZ) { 697c478bd9Sstevel@tonic-gate errno = EINVAL; 707c478bd9Sstevel@tonic-gate return (-1); 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate (void) strlcpy(dia.dia_name, name, IFNAMSIZ); 747c478bd9Sstevel@tonic-gate 75*210db224Sericheng if (i_dladm_ioctl(fd, DLDIOCATTR, &dia, sizeof (dia)) < 0) 767c478bd9Sstevel@tonic-gate return (-1); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate (void) strlcpy(dap->da_dev, dia.dia_dev, MAXNAMELEN); 79*210db224Sericheng dap->da_max_sdu = dia.dia_max_sdu; 807c478bd9Sstevel@tonic-gate dap->da_port = dia.dia_port; 817c478bd9Sstevel@tonic-gate dap->da_vid = dia.dia_vid; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate return (0); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Callback function used to count the number of DDI_NT_NET. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate /* ARGSUSED */ 907c478bd9Sstevel@tonic-gate static int 917c478bd9Sstevel@tonic-gate i_dladm_nt_net_count(di_node_t node, di_minor_t minor, void *arg) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate uint_t *countp = arg; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate (*countp)++; 967c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Adds a datalink to the array corresponding to arg. 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate static void 1037c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(void *arg, char *name) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate char **array = arg; 1067c478bd9Sstevel@tonic-gate char *elem; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate for (;;) { 1097c478bd9Sstevel@tonic-gate elem = *(array++); 1107c478bd9Sstevel@tonic-gate if (elem[0] == '\0') 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate if (strcmp(elem, name) == 0) 1137c478bd9Sstevel@tonic-gate return; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate (void) strlcpy(elem, name, MAXNAMELEN); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Walker callback invoked for each DDI_NT_NET node. 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate static int 1237c478bd9Sstevel@tonic-gate i_dladm_nt_net_walk(di_node_t node, di_minor_t minor, void *arg) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate dl_info_ack_t dlia; 1267c478bd9Sstevel@tonic-gate char name[IFNAMSIZ]; 1277c478bd9Sstevel@tonic-gate int fd; 1287c478bd9Sstevel@tonic-gate char *provider; 1297c478bd9Sstevel@tonic-gate uint_t ppa; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate provider = di_minor_name(minor); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if ((fd = dlpi_open(provider)) < 0) 1347c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0) { 1377c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1387c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (dlia.dl_provider_style == DL_STYLE1) { 1427c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(arg, provider); 1437c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1447c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate ppa = di_instance(node); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (dlpi_attach(fd, -1, ppa) < 0) { 1507c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1517c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate (void) snprintf(name, IFNAMSIZ - 1, "%s%d", provider, ppa); 1557c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(arg, name); 1567c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1577c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* 1617c478bd9Sstevel@tonic-gate * Invoke the specified callback function for each active DDI_NT_NET 1627c478bd9Sstevel@tonic-gate * node. 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate int 1657c478bd9Sstevel@tonic-gate dladm_walk(void (*fn)(void *, const char *), void *arg) 1667c478bd9Sstevel@tonic-gate { 1677c478bd9Sstevel@tonic-gate di_node_t root; 1687c478bd9Sstevel@tonic-gate uint_t count; 1697c478bd9Sstevel@tonic-gate char **array; 1707c478bd9Sstevel@tonic-gate char *elem; 1717c478bd9Sstevel@tonic-gate int i; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) { 1747c478bd9Sstevel@tonic-gate errno = EFAULT; 1757c478bd9Sstevel@tonic-gate return (-1); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate count = 0; 1797c478bd9Sstevel@tonic-gate (void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, (void *)&count, 1807c478bd9Sstevel@tonic-gate i_dladm_nt_net_count); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (count == 0) 183*210db224Sericheng return (dladm_walk_vlan(fn, arg)); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if ((array = malloc(count * sizeof (char *))) == NULL) 1867c478bd9Sstevel@tonic-gate goto done; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) { 1897c478bd9Sstevel@tonic-gate if ((array[i] = malloc(IFNAMSIZ)) != NULL) { 1907c478bd9Sstevel@tonic-gate (void) memset(array[i], '\0', IFNAMSIZ); 1917c478bd9Sstevel@tonic-gate continue; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate while (--i >= 0) 1957c478bd9Sstevel@tonic-gate free(array[i]); 1967c478bd9Sstevel@tonic-gate goto done; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate (void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, (void *)array, 2007c478bd9Sstevel@tonic-gate i_dladm_nt_net_walk); 2017c478bd9Sstevel@tonic-gate di_fini(root); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) { 2047c478bd9Sstevel@tonic-gate elem = array[i]; 2057c478bd9Sstevel@tonic-gate if (elem[0] != '\0') 2067c478bd9Sstevel@tonic-gate fn(arg, (const char *)elem); 2077c478bd9Sstevel@tonic-gate free(elem); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate done: 2117c478bd9Sstevel@tonic-gate free(array); 212*210db224Sericheng return (dladm_walk_vlan(fn, arg)); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* 216*210db224Sericheng * Invoke the specified callback function for each vlan managed by dld 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate int 219*210db224Sericheng dladm_walk_vlan(void (*fn)(void *, const char *), void *arg) 2207c478bd9Sstevel@tonic-gate { 221*210db224Sericheng int fd, bufsize, rc, i; 222*210db224Sericheng int nvlan = 512; 223*210db224Sericheng dld_ioc_vlan_t *iocp = NULL; 224*210db224Sericheng dld_vlan_info_t *dvip; 2257c478bd9Sstevel@tonic-gate 226*210db224Sericheng if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 2277c478bd9Sstevel@tonic-gate return (-1); 228*210db224Sericheng 229*210db224Sericheng for (;;) { 230*210db224Sericheng bufsize = sizeof (dld_ioc_vlan_t) + 231*210db224Sericheng nvlan * sizeof (dld_vlan_info_t); 232*210db224Sericheng 233*210db224Sericheng iocp = (dld_ioc_vlan_t *)calloc(1, bufsize); 234*210db224Sericheng if (iocp == NULL) 235*210db224Sericheng goto done; 236*210db224Sericheng 237*210db224Sericheng rc = i_dladm_ioctl(fd, DLDIOCVLAN, iocp, bufsize); 238*210db224Sericheng if (rc == 0) 239*210db224Sericheng break; 240*210db224Sericheng 241*210db224Sericheng if (errno == ENOSPC) { 242*210db224Sericheng nvlan *= 2; 243*210db224Sericheng free(iocp); 244*210db224Sericheng continue; 245*210db224Sericheng } 246*210db224Sericheng goto done; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 249*210db224Sericheng dvip = (dld_vlan_info_t *)(iocp + 1); 250*210db224Sericheng 251*210db224Sericheng for (i = 0; i < iocp->div_count; i++) { 252*210db224Sericheng if (dvip[i].dvi_vid != 0) 253*210db224Sericheng (*fn)(arg, dvip[i].dvi_name); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 256*210db224Sericheng done: 257*210db224Sericheng free(iocp); 2587c478bd9Sstevel@tonic-gate (void) close(fd); 2597c478bd9Sstevel@tonic-gate return (0); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * Returns the current attributes of the specified datalink. 2657c478bd9Sstevel@tonic-gate */ 2667c478bd9Sstevel@tonic-gate int 2677c478bd9Sstevel@tonic-gate dladm_info(const char *name, dladm_attr_t *dap) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate int fd; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 2727c478bd9Sstevel@tonic-gate return (-1); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (i_dladm_info(fd, name, dap) < 0) 2757c478bd9Sstevel@tonic-gate goto failed; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate (void) close(fd); 2787c478bd9Sstevel@tonic-gate return (0); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate failed: 2817c478bd9Sstevel@tonic-gate (void) close(fd); 2827c478bd9Sstevel@tonic-gate return (-1); 2837c478bd9Sstevel@tonic-gate } 284