1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <ctype.h> 31*0Sstevel@tonic-gate #include <string.h> 32*0Sstevel@tonic-gate #include <strings.h> 33*0Sstevel@tonic-gate #include <stdlib.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/socket.h> 36*0Sstevel@tonic-gate #include <inet/common.h> 37*0Sstevel@tonic-gate #include <net/if.h> 38*0Sstevel@tonic-gate #include <netinet/in.h> 39*0Sstevel@tonic-gate #include <sys/sockio.h> 40*0Sstevel@tonic-gate #include <sys/ioctl.h> 41*0Sstevel@tonic-gate #include <unistd.h> 42*0Sstevel@tonic-gate #include <errno.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #define IPIF_SEPARATOR_CHAR ":" 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate /* 47*0Sstevel@tonic-gate * Given an interface name, this function retrives the associated 48*0Sstevel@tonic-gate * index value. Returns index value if successful, zero otherwise. 49*0Sstevel@tonic-gate * The length of the supplied interface name must be at most 50*0Sstevel@tonic-gate * IF_NAMESIZE-1 bytes 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate uint32_t 53*0Sstevel@tonic-gate if_nametoindex(const char *ifname) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate int s; 56*0Sstevel@tonic-gate struct lifreq lifr; 57*0Sstevel@tonic-gate int save_err; 58*0Sstevel@tonic-gate size_t size; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* Make sure the given name is not NULL */ 62*0Sstevel@tonic-gate if ((ifname == NULL)||(*ifname == '\0')) { 63*0Sstevel@tonic-gate errno = ENXIO; 64*0Sstevel@tonic-gate return (0); 65*0Sstevel@tonic-gate } 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate /* 68*0Sstevel@tonic-gate * Fill up the interface name in the ioctl 69*0Sstevel@tonic-gate * request message. Make sure that the length of 70*0Sstevel@tonic-gate * the given interface name <= (IF_NAMESIZE-1) 71*0Sstevel@tonic-gate */ 72*0Sstevel@tonic-gate size = strlen(ifname); 73*0Sstevel@tonic-gate if (size > (IF_NAMESIZE - 1)) { 74*0Sstevel@tonic-gate errno = EINVAL; 75*0Sstevel@tonic-gate return (0); 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate strncpy(lifr.lifr_name, ifname, size +1); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate /* Check the v4 interfaces first */ 81*0Sstevel@tonic-gate s = socket(AF_INET, SOCK_DGRAM, 0); 82*0Sstevel@tonic-gate if (s >= 0) { 83*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) { 84*0Sstevel@tonic-gate (void) close(s); 85*0Sstevel@tonic-gate return (lifr.lifr_index); 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate (void) close(s); 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate /* Check the v6 interface list */ 91*0Sstevel@tonic-gate s = socket(AF_INET6, SOCK_DGRAM, 0); 92*0Sstevel@tonic-gate if (s < 0) 93*0Sstevel@tonic-gate return (0); 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) < 0) 96*0Sstevel@tonic-gate lifr.lifr_index = 0; 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate save_err = errno; 99*0Sstevel@tonic-gate (void) close(s); 100*0Sstevel@tonic-gate errno = save_err; 101*0Sstevel@tonic-gate return (lifr.lifr_index); 102*0Sstevel@tonic-gate } 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate /* 105*0Sstevel@tonic-gate * Given an index, this function returns the associated interface 106*0Sstevel@tonic-gate * name in the supplied buffer ifname. 107*0Sstevel@tonic-gate * Returns physical interface name if successful, NULL otherwise. 108*0Sstevel@tonic-gate * The interface name returned will be at most IF_NAMESIZE-1 bytes. 109*0Sstevel@tonic-gate */ 110*0Sstevel@tonic-gate char * 111*0Sstevel@tonic-gate if_indextoname(uint32_t ifindex, char *ifname) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate int n; 114*0Sstevel@tonic-gate int s; 115*0Sstevel@tonic-gate char *buf; 116*0Sstevel@tonic-gate uint32_t index; 117*0Sstevel@tonic-gate struct lifnum lifn; 118*0Sstevel@tonic-gate struct lifconf lifc; 119*0Sstevel@tonic-gate struct lifreq *lifrp; 120*0Sstevel@tonic-gate int numifs; 121*0Sstevel@tonic-gate size_t bufsize; 122*0Sstevel@tonic-gate boolean_t found; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* A interface index of 0 is invalid */ 125*0Sstevel@tonic-gate if (ifindex == 0) { 126*0Sstevel@tonic-gate errno = ENXIO; 127*0Sstevel@tonic-gate return (NULL); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate s = socket(AF_INET6, SOCK_DGRAM, 0); 131*0Sstevel@tonic-gate if (s < 0) { 132*0Sstevel@tonic-gate s = socket(AF_INET, SOCK_DGRAM, 0); 133*0Sstevel@tonic-gate if (s < 0) { 134*0Sstevel@tonic-gate return (NULL); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate /* Prepare to send a SIOCGLIFNUM request message */ 139*0Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 140*0Sstevel@tonic-gate lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES; 141*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 142*0Sstevel@tonic-gate int save_err = errno; 143*0Sstevel@tonic-gate (void) close(s); 144*0Sstevel@tonic-gate errno = save_err; 145*0Sstevel@tonic-gate return (NULL); 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate numifs = lifn.lifn_count; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* 150*0Sstevel@tonic-gate * Provide enough buffer to obtain the interface 151*0Sstevel@tonic-gate * list from the kernel as response to a SIOCGLIFCONF 152*0Sstevel@tonic-gate * request 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate bufsize = numifs * sizeof (struct lifreq); 156*0Sstevel@tonic-gate buf = malloc(bufsize); 157*0Sstevel@tonic-gate if (buf == NULL) { 158*0Sstevel@tonic-gate int save_err = errno; 159*0Sstevel@tonic-gate (void) close(s); 160*0Sstevel@tonic-gate errno = save_err; 161*0Sstevel@tonic-gate return (NULL); 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 164*0Sstevel@tonic-gate lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES; 165*0Sstevel@tonic-gate lifc.lifc_len = bufsize; 166*0Sstevel@tonic-gate lifc.lifc_buf = buf; 167*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 168*0Sstevel@tonic-gate int save_err = errno; 169*0Sstevel@tonic-gate (void) close(s); 170*0Sstevel@tonic-gate errno = save_err; 171*0Sstevel@tonic-gate free(buf); 172*0Sstevel@tonic-gate return (NULL); 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate lifrp = lifc.lifc_req; 176*0Sstevel@tonic-gate found = B_FALSE; 177*0Sstevel@tonic-gate for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) { 178*0Sstevel@tonic-gate /* 179*0Sstevel@tonic-gate * Obtain the index value of each interface, and 180*0Sstevel@tonic-gate * match to see if the retrived index value matches 181*0Sstevel@tonic-gate * the given one. If so we have return the corresponding 182*0Sstevel@tonic-gate * device name of that interface. 183*0Sstevel@tonic-gate */ 184*0Sstevel@tonic-gate size_t size; 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate index = if_nametoindex(lifrp->lifr_name); 187*0Sstevel@tonic-gate if (index == 0) 188*0Sstevel@tonic-gate /* Oops the interface just disappeared */ 189*0Sstevel@tonic-gate continue; 190*0Sstevel@tonic-gate if (index == ifindex) { 191*0Sstevel@tonic-gate size = strcspn(lifrp->lifr_name, 192*0Sstevel@tonic-gate (char *)IPIF_SEPARATOR_CHAR); 193*0Sstevel@tonic-gate lifrp->lifr_name[size] = '\0'; 194*0Sstevel@tonic-gate found = B_TRUE; 195*0Sstevel@tonic-gate (void) strncpy(ifname, lifrp->lifr_name, 196*0Sstevel@tonic-gate size + 1); 197*0Sstevel@tonic-gate break; 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate (void) close(s); 201*0Sstevel@tonic-gate free(buf); 202*0Sstevel@tonic-gate if (!found) { 203*0Sstevel@tonic-gate errno = ENXIO; 204*0Sstevel@tonic-gate return (NULL); 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate return (ifname); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate /* 210*0Sstevel@tonic-gate * This function returns all the interface names and indexes 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate struct if_nameindex * 213*0Sstevel@tonic-gate if_nameindex(void) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate int n; 216*0Sstevel@tonic-gate int s; 217*0Sstevel@tonic-gate boolean_t found; 218*0Sstevel@tonic-gate char *buf; 219*0Sstevel@tonic-gate struct lifnum lifn; 220*0Sstevel@tonic-gate struct lifconf lifc; 221*0Sstevel@tonic-gate struct lifreq *lifrp; 222*0Sstevel@tonic-gate int numifs; 223*0Sstevel@tonic-gate int index; 224*0Sstevel@tonic-gate int i; 225*0Sstevel@tonic-gate int physinterf_num; 226*0Sstevel@tonic-gate size_t bufsize; 227*0Sstevel@tonic-gate struct if_nameindex *interface_list; 228*0Sstevel@tonic-gate struct if_nameindex *interface_entry; 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate s = socket(AF_INET6, SOCK_DGRAM, 0); 231*0Sstevel@tonic-gate if (s < 0) { 232*0Sstevel@tonic-gate s = socket(AF_INET, SOCK_DGRAM, 0); 233*0Sstevel@tonic-gate if (s < 0) 234*0Sstevel@tonic-gate return (NULL); 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 238*0Sstevel@tonic-gate lifn.lifn_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES; 239*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) 240*0Sstevel@tonic-gate return (NULL); 241*0Sstevel@tonic-gate numifs = lifn.lifn_count; 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate bufsize = numifs * sizeof (struct lifreq); 244*0Sstevel@tonic-gate buf = malloc(bufsize); 245*0Sstevel@tonic-gate if (buf == NULL) { 246*0Sstevel@tonic-gate int save_err = errno; 247*0Sstevel@tonic-gate (void) close(s); 248*0Sstevel@tonic-gate errno = save_err; 249*0Sstevel@tonic-gate return (NULL); 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 252*0Sstevel@tonic-gate lifc.lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES; 253*0Sstevel@tonic-gate lifc.lifc_len = bufsize; 254*0Sstevel@tonic-gate lifc.lifc_buf = buf; 255*0Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 256*0Sstevel@tonic-gate int save_err = errno; 257*0Sstevel@tonic-gate (void) close(s); 258*0Sstevel@tonic-gate errno = save_err; 259*0Sstevel@tonic-gate free(buf); 260*0Sstevel@tonic-gate return (NULL); 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate lifrp = lifc.lifc_req; 264*0Sstevel@tonic-gate (void) close(s); 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate /* Allocate the array of if_nameindex structure */ 267*0Sstevel@tonic-gate interface_list = malloc((numifs + 1) * sizeof (struct if_nameindex)); 268*0Sstevel@tonic-gate if (!interface_list) { 269*0Sstevel@tonic-gate int save_err = errno; 270*0Sstevel@tonic-gate free(buf); 271*0Sstevel@tonic-gate errno = save_err; 272*0Sstevel@tonic-gate return (NULL); 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate /* 275*0Sstevel@tonic-gate * Make sure that terminator structure automatically 276*0Sstevel@tonic-gate * happens to be all zeroes. 277*0Sstevel@tonic-gate */ 278*0Sstevel@tonic-gate bzero(interface_list, ((numifs + 1) * sizeof (struct if_nameindex))); 279*0Sstevel@tonic-gate interface_entry = interface_list; 280*0Sstevel@tonic-gate physinterf_num = 0; 281*0Sstevel@tonic-gate for (n = numifs; n > 0; n--, lifrp++) { 282*0Sstevel@tonic-gate size_t size; 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate size = strcspn(lifrp->lifr_name, (char *)IPIF_SEPARATOR_CHAR); 285*0Sstevel@tonic-gate lifrp->lifr_name[size] = '\0'; 286*0Sstevel@tonic-gate found = B_FALSE; 287*0Sstevel@tonic-gate /* 288*0Sstevel@tonic-gate * Search the current array to see if this interface 289*0Sstevel@tonic-gate * already exists 290*0Sstevel@tonic-gate */ 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate for (i = 0; i < physinterf_num; i++) { 293*0Sstevel@tonic-gate if (strcmp(interface_entry[i].if_name, 294*0Sstevel@tonic-gate lifrp->lifr_name) == 0) { 295*0Sstevel@tonic-gate found = B_TRUE; 296*0Sstevel@tonic-gate break; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate allocate_new: 301*0Sstevel@tonic-gate /* New one. Allocate an array element and fill it */ 302*0Sstevel@tonic-gate if (!found) { 303*0Sstevel@tonic-gate if ((interface_entry[physinterf_num].if_name = 304*0Sstevel@tonic-gate strdup(lifrp->lifr_name)) == NULL) { 305*0Sstevel@tonic-gate int save_err; 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate if_freenameindex(interface_list); 308*0Sstevel@tonic-gate save_err = errno; 309*0Sstevel@tonic-gate free(buf); 310*0Sstevel@tonic-gate errno = save_err; 311*0Sstevel@tonic-gate return (NULL); 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate /* 315*0Sstevel@tonic-gate * Obtain the index value for the interface 316*0Sstevel@tonic-gate */ 317*0Sstevel@tonic-gate interface_entry[physinterf_num].if_index = 318*0Sstevel@tonic-gate if_nametoindex(lifrp->lifr_name); 319*0Sstevel@tonic-gate physinterf_num++; 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate /* Create the last one of the array */ 324*0Sstevel@tonic-gate interface_entry[physinterf_num].if_name = NULL; 325*0Sstevel@tonic-gate interface_entry[physinterf_num].if_index = 0; 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate /* Free up the excess array space */ 328*0Sstevel@tonic-gate free(buf); 329*0Sstevel@tonic-gate interface_list = realloc(interface_list, ((physinterf_num + 1) * 330*0Sstevel@tonic-gate sizeof (struct if_nameindex))); 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate return (interface_list); 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate /* 336*0Sstevel@tonic-gate * This function frees the the array that is created while 337*0Sstevel@tonic-gate * the if_nameindex function. 338*0Sstevel@tonic-gate */ 339*0Sstevel@tonic-gate void 340*0Sstevel@tonic-gate if_freenameindex(struct if_nameindex *ptr) 341*0Sstevel@tonic-gate { 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate if (ptr == NULL) 344*0Sstevel@tonic-gate return; 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate /* First free the if_name member in each array element */ 348*0Sstevel@tonic-gate while (ptr->if_name != NULL) { 349*0Sstevel@tonic-gate free(ptr->if_name); 350*0Sstevel@tonic-gate ptr++; 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* Now free up the array space */ 354*0Sstevel@tonic-gate free(ptr); 355*0Sstevel@tonic-gate } 356