10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*8485SPeter.Memishian@Sun.COM * Common Development and Distribution License (the "License"). 6*8485SPeter.Memishian@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21132Srobinson 220Sstevel@tonic-gate /* 23*8485SPeter.Memishian@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * lib/libnsl/nss/netdir_inet_sundry.c 270Sstevel@tonic-gate * 280Sstevel@tonic-gate * This file contains inet-specific implementations of netdir_options, 290Sstevel@tonic-gate * uaddr2taddr, and taddr2uaddr. These implementations 300Sstevel@tonic-gate * used to be in both tcpip.so and switch.so (identical copies). 310Sstevel@tonic-gate * Since we got rid of those, and also it's a good idea to build-in 320Sstevel@tonic-gate * inet-specific implementations in one place, we decided to put 330Sstevel@tonic-gate * them in this file with a not-so glorious name. These are INET-SPECIFIC 340Sstevel@tonic-gate * only, and will not be used for non-inet transports or by third-parties 350Sstevel@tonic-gate * that decide to provide their own nametoaddr libs for inet transports 360Sstevel@tonic-gate * (they are on their own for these as well => they get flexibility). 370Sstevel@tonic-gate * 380Sstevel@tonic-gate * Copied mostly from erstwhile lib/nametoaddr/tcpip/tcpip.c. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include "mt.h" 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <stdio.h> 440Sstevel@tonic-gate #include <string.h> 45132Srobinson #include <strings.h> 460Sstevel@tonic-gate #include <unistd.h> 470Sstevel@tonic-gate #include <sys/types.h> 480Sstevel@tonic-gate #include <sys/stat.h> 490Sstevel@tonic-gate #include <fcntl.h> 500Sstevel@tonic-gate #include <errno.h> 510Sstevel@tonic-gate #include <thread.h> 520Sstevel@tonic-gate #include <netconfig.h> 530Sstevel@tonic-gate #include <netdir.h> 540Sstevel@tonic-gate #include <nss_netdir.h> 550Sstevel@tonic-gate #include <tiuser.h> 560Sstevel@tonic-gate #include <sys/socket.h> 570Sstevel@tonic-gate #include <net/if.h> 580Sstevel@tonic-gate #include <sys/sockio.h> 590Sstevel@tonic-gate #include <sys/fcntl.h> 600Sstevel@tonic-gate #include <netinet/in.h> 610Sstevel@tonic-gate #include <netinet/tcp.h> 620Sstevel@tonic-gate #include <netinet/udp.h> 630Sstevel@tonic-gate #include <arpa/inet.h> 640Sstevel@tonic-gate #include <rpc/types.h> 650Sstevel@tonic-gate #include <rpc/rpc_com.h> 660Sstevel@tonic-gate #include <syslog.h> 670Sstevel@tonic-gate #include <values.h> 680Sstevel@tonic-gate #include <limits.h> 690Sstevel@tonic-gate #include <nss_dbdefs.h> 700Sstevel@tonic-gate #include "nss.h" 710Sstevel@tonic-gate 720Sstevel@tonic-gate #define MAXIFS 32 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Extracted from socketvar.h 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate #define SOV_DEFAULT 1 /* Select based on so_default_version */ 780Sstevel@tonic-gate #define SOV_SOCKBSD 3 /* Socket with no streams operations */ 790Sstevel@tonic-gate 80132Srobinson extern int _so_socket(int, int, int, char *, int); 81132Srobinson extern int _so_connect(int, struct sockaddr *, socklen_t, int); 82132Srobinson extern int _so_getsockname(int, struct sockaddr *, socklen_t *, int); 830Sstevel@tonic-gate 840Sstevel@tonic-gate 850Sstevel@tonic-gate static char *inet_netdir_mergeaddr(struct netconfig *, char *, char *); 860Sstevel@tonic-gate static int bindresvport(struct netconfig *, int, struct netbuf *); 870Sstevel@tonic-gate static int checkresvport(struct netbuf *); 880Sstevel@tonic-gate static struct netbuf *ip_uaddr2taddr(char *); 890Sstevel@tonic-gate static struct netbuf *ipv6_uaddr2taddr(char *); 900Sstevel@tonic-gate 910Sstevel@tonic-gate 920Sstevel@tonic-gate extern char *inet_ntoa_r(struct in_addr, char *); 930Sstevel@tonic-gate 940Sstevel@tonic-gate int 95132Srobinson __inet_netdir_options(struct netconfig *tp, int opts, int fd, char *par) 960Sstevel@tonic-gate { 970Sstevel@tonic-gate struct nd_mergearg *ma; 980Sstevel@tonic-gate 990Sstevel@tonic-gate switch (opts) { 1000Sstevel@tonic-gate case ND_SET_BROADCAST: 1010Sstevel@tonic-gate /* Every one is allowed to broadcast without asking */ 1020Sstevel@tonic-gate return (ND_OK); 1030Sstevel@tonic-gate case ND_SET_RESERVEDPORT: /* bind to a resered port */ 104132Srobinson /* LINTED pointer cast */ 1050Sstevel@tonic-gate return (bindresvport(tp, fd, (struct netbuf *)par)); 1060Sstevel@tonic-gate case ND_CHECK_RESERVEDPORT: /* check if reserved prot */ 107132Srobinson /* LINTED pointer cast */ 1080Sstevel@tonic-gate return (checkresvport((struct netbuf *)par)); 1090Sstevel@tonic-gate case ND_MERGEADDR: /* Merge two addresses */ 110132Srobinson /* LINTED pointer cast */ 1110Sstevel@tonic-gate ma = (struct nd_mergearg *)(par); 1120Sstevel@tonic-gate ma->m_uaddr = inet_netdir_mergeaddr(tp, ma->c_uaddr, 1130Sstevel@tonic-gate ma->s_uaddr); 1140Sstevel@tonic-gate return (_nderror); 1150Sstevel@tonic-gate default: 1160Sstevel@tonic-gate return (ND_NOCTRL); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * This routine will convert a TCP/IP internal format address 1230Sstevel@tonic-gate * into a "universal" format address. In our case it prints out the 1240Sstevel@tonic-gate * decimal dot equivalent. h1.h2.h3.h4.p1.p2 where h1-h4 are the host 1250Sstevel@tonic-gate * address and p1-p2 are the port number. 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate char * 128132Srobinson __inet_taddr2uaddr(struct netconfig *tp, struct netbuf *addr) 1290Sstevel@tonic-gate { 1300Sstevel@tonic-gate struct sockaddr_in *sa; /* our internal format */ 1310Sstevel@tonic-gate struct sockaddr_in6 *sa6; /* our internal format */ 1320Sstevel@tonic-gate char tmp[RPC_INET6_MAXUADDRSIZE]; 1330Sstevel@tonic-gate unsigned short myport; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate if (addr == NULL || tp == NULL || addr->buf == NULL) { 1360Sstevel@tonic-gate _nderror = ND_BADARG; 1370Sstevel@tonic-gate return (NULL); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate if (strcmp(tp->nc_protofmly, NC_INET) == 0) { 140132Srobinson /* LINTED pointer cast */ 1410Sstevel@tonic-gate sa = (struct sockaddr_in *)(addr->buf); 1420Sstevel@tonic-gate myport = ntohs(sa->sin_port); 143132Srobinson (void) inet_ntoa_r(sa->sin_addr, tmp); 1440Sstevel@tonic-gate } else { 145132Srobinson /* LINTED pointer cast */ 1460Sstevel@tonic-gate sa6 = (struct sockaddr_in6 *)(addr->buf); 1470Sstevel@tonic-gate myport = ntohs(sa6->sin6_port); 148*8485SPeter.Memishian@Sun.COM if (inet_ntop(AF_INET6, sa6->sin6_addr.s6_addr, tmp, 149*8485SPeter.Memishian@Sun.COM sizeof (tmp)) == NULL) { 1500Sstevel@tonic-gate _nderror = ND_BADARG; 1510Sstevel@tonic-gate return (NULL); 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate (void) sprintf(tmp + strlen(tmp), ".%d.%d", myport >> 8, myport & 255); 1560Sstevel@tonic-gate return (strdup(tmp)); /* Doesn't return static data ! */ 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * This internal routine will convert one of those "universal" addresses 1610Sstevel@tonic-gate * to the internal format used by the Sun TLI TCP/IP provider. 1620Sstevel@tonic-gate */ 1630Sstevel@tonic-gate struct netbuf * 164132Srobinson __inet_uaddr2taddr(struct netconfig *tp, char *addr) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate if (!addr || !tp) { 1670Sstevel@tonic-gate _nderror = ND_BADARG; 1680Sstevel@tonic-gate return (NULL); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate if (strcmp(tp->nc_protofmly, NC_INET) == 0) 1710Sstevel@tonic-gate return (ip_uaddr2taddr(addr)); 1720Sstevel@tonic-gate else 1730Sstevel@tonic-gate return (ipv6_uaddr2taddr(addr)); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static struct netbuf * 1770Sstevel@tonic-gate ip_uaddr2taddr(char *addr) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate struct sockaddr_in *sa; 1810Sstevel@tonic-gate uint32_t inaddr; 1820Sstevel@tonic-gate unsigned short inport; 1830Sstevel@tonic-gate int h1, h2, h3, h4, p1, p2; 1840Sstevel@tonic-gate struct netbuf *result; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate result = malloc(sizeof (struct netbuf)); 1870Sstevel@tonic-gate if (!result) { 1880Sstevel@tonic-gate _nderror = ND_NOMEM; 1890Sstevel@tonic-gate return (NULL); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate sa = calloc(1, sizeof (*sa)); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if (!sa) { 1950Sstevel@tonic-gate free(result); 1960Sstevel@tonic-gate _nderror = ND_NOMEM; 1970Sstevel@tonic-gate return (NULL); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate result->buf = (char *)(sa); 2010Sstevel@tonic-gate result->maxlen = sizeof (struct sockaddr_in); 2020Sstevel@tonic-gate result->len = sizeof (struct sockaddr_in); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate /* XXX there is probably a better way to do this. */ 2050Sstevel@tonic-gate if (sscanf(addr, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3, &h4, 2060Sstevel@tonic-gate &p1, &p2) != 6) { 2070Sstevel@tonic-gate free(result); 2080Sstevel@tonic-gate _nderror = ND_NO_RECOVERY; 2090Sstevel@tonic-gate return (NULL); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* convert the host address first */ 2130Sstevel@tonic-gate inaddr = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4; 2140Sstevel@tonic-gate sa->sin_addr.s_addr = htonl(inaddr); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* convert the port */ 2170Sstevel@tonic-gate inport = (p1 << 8) + p2; 2180Sstevel@tonic-gate sa->sin_port = htons(inport); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate sa->sin_family = AF_INET; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate return (result); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate static struct netbuf * 2260Sstevel@tonic-gate ipv6_uaddr2taddr(char *addr) 2270Sstevel@tonic-gate { 2280Sstevel@tonic-gate struct sockaddr_in6 *sa; 2290Sstevel@tonic-gate unsigned short inport; 2300Sstevel@tonic-gate int p1, p2; 2310Sstevel@tonic-gate struct netbuf *result; 2320Sstevel@tonic-gate char tmpaddr[RPC_INET6_MAXUADDRSIZE]; 2330Sstevel@tonic-gate char *dot; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate result = malloc(sizeof (struct netbuf)); 2360Sstevel@tonic-gate if (!result) { 2370Sstevel@tonic-gate _nderror = ND_NOMEM; 2380Sstevel@tonic-gate return (NULL); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate sa = calloc(1, sizeof (struct sockaddr_in6)); 2420Sstevel@tonic-gate if (!sa) { 2430Sstevel@tonic-gate free(result); 2440Sstevel@tonic-gate _nderror = ND_NOMEM; 2450Sstevel@tonic-gate return (NULL); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate result->buf = (char *)(sa); 2480Sstevel@tonic-gate result->maxlen = sizeof (struct sockaddr_in6); 2490Sstevel@tonic-gate result->len = sizeof (struct sockaddr_in6); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* retrieve the ipv6 address and port info */ 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if (strlen(addr) > sizeof (tmpaddr) - 1) { 2540Sstevel@tonic-gate free(result); 2550Sstevel@tonic-gate _nderror = ND_NOMEM; 2560Sstevel@tonic-gate return (NULL); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 259132Srobinson (void) strcpy(tmpaddr, addr); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate if ((dot = strrchr(tmpaddr, '.')) != 0) { 2620Sstevel@tonic-gate *dot = '\0'; 2630Sstevel@tonic-gate p2 = atoi(dot+1); 2640Sstevel@tonic-gate if ((dot = strrchr(tmpaddr, '.')) != 0) { 2650Sstevel@tonic-gate *dot = '\0'; 2660Sstevel@tonic-gate p1 = atoi(dot+1); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (dot == 0) { 2710Sstevel@tonic-gate free(result); 2720Sstevel@tonic-gate _nderror = ND_NOMEM; 2730Sstevel@tonic-gate return (NULL); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (inet_pton(AF_INET6, tmpaddr, sa->sin6_addr.s6_addr) == 0) { 2770Sstevel@tonic-gate free(result); 2780Sstevel@tonic-gate _nderror = ND_NOMEM; 2790Sstevel@tonic-gate return (NULL); 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* convert the port */ 2830Sstevel@tonic-gate inport = (p1 << 8) + p2; 2840Sstevel@tonic-gate sa->sin6_port = htons(inport); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate sa->sin6_family = AF_INET6; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate return (result); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * Interface caching routines. The cache is refreshed every 2930Sstevel@tonic-gate * IF_CACHE_REFRESH_TIME seconds. A read-write lock is used to 2940Sstevel@tonic-gate * protect the cache. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate #define IF_CACHE_REFRESH_TIME 10 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate static int if_cache_refresh_time = IF_CACHE_REFRESH_TIME; 2990Sstevel@tonic-gate static rwlock_t iflock = DEFAULTRWLOCK; 3000Sstevel@tonic-gate static time_t last_updated = 0; /* protected by iflock */ 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* 3030Sstevel@tonic-gate * Changing the data type of if_flags from uint_t to uint64_t to accomodate 3040Sstevel@tonic-gate * extra flags. Refer <net/if.h> for the extra flags. 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate typedef struct if_info_s { 3070Sstevel@tonic-gate struct in_addr if_netmask; /* netmask in network order */ 3080Sstevel@tonic-gate struct in_addr if_address; /* address in network order */ 3090Sstevel@tonic-gate uint64_t if_flags; /* interface flags */ 3100Sstevel@tonic-gate } if_info_t; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate static if_info_t *if_info = NULL; /* if cache, protected by iflock */ 3130Sstevel@tonic-gate static int n_ifs = 0; /* number of cached interfaces */ 3140Sstevel@tonic-gate static int numifs_last = 0; /* number of interfaces last seen */ 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * Builds the interface cache. Write lock on iflock is needed 3180Sstevel@tonic-gate * for calling this routine. It sets _nderror for error returns. 3190Sstevel@tonic-gate * Returns TRUE if successful, FALSE otherwise. 3200Sstevel@tonic-gate * Changing the structures ifreq and ifconf to lifreq and lifconf to 3210Sstevel@tonic-gate * have larger flag field. This is to accomodate the extra flags associated 3220Sstevel@tonic-gate * with the interface. Also introducing lifn which will contain the number 3230Sstevel@tonic-gate * of IPV4 interfaces present. 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate static bool_t 326132Srobinson get_if_info(void) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate size_t needed; 3290Sstevel@tonic-gate struct lifreq *buf = NULL; 3300Sstevel@tonic-gate int numifs; 3310Sstevel@tonic-gate struct lifconf lifc; 3320Sstevel@tonic-gate struct lifreq *lifr; 3330Sstevel@tonic-gate struct lifnum lifn; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate lifn.lifn_family = AF_INET; 3360Sstevel@tonic-gate lifn.lifn_flags = 0; 3370Sstevel@tonic-gate getifnum: 3380Sstevel@tonic-gate if (nss_ioctl(AF_INET, SIOCGLIFNUM, &lifn) == -1) { 3390Sstevel@tonic-gate numifs = MAXIFS; 3400Sstevel@tonic-gate } else { 3410Sstevel@tonic-gate numifs = lifn.lifn_count; 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate /* 3440Sstevel@tonic-gate * Add a small fudge factor in case interfaces are plumbed 3450Sstevel@tonic-gate * between the SIOCGLIFNUM and SIOCGLIFCONF. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate needed = (numifs + 4) * sizeof (struct lifreq); 3480Sstevel@tonic-gate if (buf == NULL) 3490Sstevel@tonic-gate buf = malloc(needed); 3500Sstevel@tonic-gate else 3510Sstevel@tonic-gate buf = realloc(buf, needed); 3520Sstevel@tonic-gate if (buf == NULL) { 3530Sstevel@tonic-gate _nderror = ND_NOMEM; 3540Sstevel@tonic-gate return (FALSE); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate lifc.lifc_family = AF_INET; 3580Sstevel@tonic-gate lifc.lifc_flags = 0; 3590Sstevel@tonic-gate lifc.lifc_len = needed; 3600Sstevel@tonic-gate lifc.lifc_buf = (char *)buf; 3610Sstevel@tonic-gate if (nss_ioctl(AF_INET, SIOCGLIFCONF, &lifc) == -1) { 3620Sstevel@tonic-gate /* 3630Sstevel@tonic-gate * IP returns EINVAL if the buffer was too small to fit 3640Sstevel@tonic-gate * all of the entries. If that's the case, go back and 3650Sstevel@tonic-gate * try again. 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate if (errno == EINVAL) 3680Sstevel@tonic-gate goto getifnum; 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate free(buf); 3710Sstevel@tonic-gate free(if_info); 3720Sstevel@tonic-gate if_info = NULL; 3730Sstevel@tonic-gate _nderror = ND_SYSTEM; 3740Sstevel@tonic-gate return (FALSE); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate numifs = lifc.lifc_len / (int)sizeof (struct lifreq); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate if (if_info == NULL || numifs > numifs_last) { 3790Sstevel@tonic-gate if (if_info == NULL) 3800Sstevel@tonic-gate if_info = malloc(numifs * sizeof (if_info_t)); 3810Sstevel@tonic-gate else 3820Sstevel@tonic-gate if_info = realloc(if_info, numifs * sizeof (if_info_t)); 3830Sstevel@tonic-gate if (if_info == NULL) { 3840Sstevel@tonic-gate free(buf); 3850Sstevel@tonic-gate _nderror = ND_NOMEM; 3860Sstevel@tonic-gate return (FALSE); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate numifs_last = numifs; 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate n_ifs = 0; 3920Sstevel@tonic-gate for (lifr = buf; lifr < (buf + numifs); lifr++) { 3930Sstevel@tonic-gate if (lifr->lifr_addr.ss_family != AF_INET) 3940Sstevel@tonic-gate continue; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate if_info[n_ifs].if_address = 397*8485SPeter.Memishian@Sun.COM ((struct sockaddr_in *)&lifr->lifr_addr)->sin_addr; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate if (nss_ioctl(AF_INET, SIOCGLIFFLAGS, lifr) < 0) 4000Sstevel@tonic-gate continue; 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate if ((lifr->lifr_flags & IFF_UP) == 0) 4030Sstevel@tonic-gate continue; 4040Sstevel@tonic-gate if_info[n_ifs].if_flags = lifr->lifr_flags; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate if (nss_ioctl(AF_INET, SIOCGLIFNETMASK, lifr) < 0) 4070Sstevel@tonic-gate continue; 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate if_info[n_ifs].if_netmask = 410*8485SPeter.Memishian@Sun.COM ((struct sockaddr_in *)&lifr->lifr_addr)->sin_addr; 4110Sstevel@tonic-gate n_ifs++; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate free(buf); 4140Sstevel@tonic-gate return (TRUE); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * Update the interface cache based on last update time. 4200Sstevel@tonic-gate */ 4210Sstevel@tonic-gate static bool_t 422132Srobinson update_if_cache(void) 4230Sstevel@tonic-gate { 4240Sstevel@tonic-gate time_t curtime; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate (void) rw_wrlock(&iflock); 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * Check if some other thread has beaten this one to it. 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate (void) time(&curtime); 4310Sstevel@tonic-gate if ((curtime - last_updated) >= if_cache_refresh_time) { 4320Sstevel@tonic-gate if (!get_if_info()) { 4330Sstevel@tonic-gate (void) rw_unlock(&iflock); 4340Sstevel@tonic-gate return (FALSE); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate (void) time(&last_updated); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate (void) rw_unlock(&iflock); 4390Sstevel@tonic-gate return (TRUE); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * Given an IP address, check if this matches any of the interface 4450Sstevel@tonic-gate * addresses. If an error occurs, return FALSE so that the caller 4460Sstevel@tonic-gate * will not assume that this address belongs to this machine. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate static bool_t 449132Srobinson is_my_address(struct in_addr addr) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate time_t curtime; 4520Sstevel@tonic-gate if_info_t *ifn; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate (void) time(&curtime); 4550Sstevel@tonic-gate if ((curtime - last_updated) >= if_cache_refresh_time) { 4560Sstevel@tonic-gate /* 4570Sstevel@tonic-gate * Cache needs to be refreshed. 4580Sstevel@tonic-gate */ 4590Sstevel@tonic-gate if (!update_if_cache()) 4600Sstevel@tonic-gate return (FALSE); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate (void) rw_rdlock(&iflock); 4630Sstevel@tonic-gate for (ifn = if_info; ifn < (if_info + n_ifs); ifn++) { 4640Sstevel@tonic-gate if (addr.s_addr == ifn->if_address.s_addr) { 4650Sstevel@tonic-gate (void) rw_unlock(&iflock); 4660Sstevel@tonic-gate return (TRUE); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate (void) rw_unlock(&iflock); 4700Sstevel@tonic-gate return (FALSE); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * Given a host name, check if it is this host. 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate bool_t 478132Srobinson __inet_netdir_is_my_host(const char *host) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate int error; 4810Sstevel@tonic-gate char buf[NSS_BUFLEN_HOSTS]; 4820Sstevel@tonic-gate struct hostent res, *h; 4830Sstevel@tonic-gate char **c; 4840Sstevel@tonic-gate struct in_addr in; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate h = gethostbyname_r(host, (void *)&res, buf, sizeof (buf), &error); 4870Sstevel@tonic-gate if (h == NULL) 4880Sstevel@tonic-gate return (FALSE); 4890Sstevel@tonic-gate if (h->h_addrtype != AF_INET) 4900Sstevel@tonic-gate return (FALSE); 4910Sstevel@tonic-gate for (c = h->h_addr_list; *c != NULL; c++) { 492132Srobinson (void) memcpy(&in.s_addr, *c, sizeof (in.s_addr)); 4930Sstevel@tonic-gate if (is_my_address(in)) 4940Sstevel@tonic-gate return (TRUE); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate return (FALSE); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * Given an IP address, find the interface address that has the best 5020Sstevel@tonic-gate * prefix match. Return the address in network order. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate static uint32_t 505132Srobinson get_best_match(struct in_addr addr) 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate if_info_t *bestmatch, *ifn; 5080Sstevel@tonic-gate int bestcount, count, limit; 5090Sstevel@tonic-gate uint32_t mask, netmask, clnt_addr, if_addr; 5100Sstevel@tonic-gate bool_t found, subnet_match; 5110Sstevel@tonic-gate int subnet_count; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate bestmatch = NULL; /* no match yet */ 5140Sstevel@tonic-gate bestcount = BITSPERBYTE * sizeof (uint32_t); /* worst match */ 5150Sstevel@tonic-gate clnt_addr = ntohl(addr.s_addr); /* host order */ 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate subnet_match = FALSE; /* subnet match not found yet */ 5180Sstevel@tonic-gate subnet_count = bestcount; /* worst subnet match */ 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate for (ifn = if_info; ifn < (if_info + n_ifs); ifn++) { 5210Sstevel@tonic-gate netmask = ntohl(ifn->if_netmask.s_addr); /* host order */ 5220Sstevel@tonic-gate if_addr = ntohl(ifn->if_address.s_addr); /* host order */ 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * set initial count to first bit set in netmask, with 5260Sstevel@tonic-gate * zero being the number of the least significant bit. 5270Sstevel@tonic-gate */ 528*8485SPeter.Memishian@Sun.COM count = 0; 529*8485SPeter.Memishian@Sun.COM for (mask = netmask; mask && ((mask & 1) == 0); mask >>= 1) 530*8485SPeter.Memishian@Sun.COM count++; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * Set limit so that we don't try to match prefixes shorter 5340Sstevel@tonic-gate * than the inherent netmask for the class (A, B, C, etc). 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate if (IN_CLASSC(if_addr)) 5370Sstevel@tonic-gate limit = IN_CLASSC_NSHIFT; 5380Sstevel@tonic-gate else if (IN_CLASSB(if_addr)) 5390Sstevel@tonic-gate limit = IN_CLASSB_NSHIFT; 5400Sstevel@tonic-gate else if (IN_CLASSA(if_addr)) 5410Sstevel@tonic-gate limit = IN_CLASSA_NSHIFT; 5420Sstevel@tonic-gate else 5430Sstevel@tonic-gate limit = 0; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * We assume that the netmask consists of a contiguous 5470Sstevel@tonic-gate * sequence of 1-bits starting with the most significant bit. 5480Sstevel@tonic-gate * Prefix comparison starts at the subnet mask level. 5490Sstevel@tonic-gate * The prefix mask used for comparison is progressively 5500Sstevel@tonic-gate * reduced until it equals the inherent mask for the 5510Sstevel@tonic-gate * interface address class. The algorithm finds an 5520Sstevel@tonic-gate * interface in the following order of preference: 5530Sstevel@tonic-gate * 5540Sstevel@tonic-gate * (1) the longest subnet match 5550Sstevel@tonic-gate * (2) the best partial subnet match 5560Sstevel@tonic-gate * (3) the first non-loopback && non-PPP interface 5570Sstevel@tonic-gate * (4) the first non-loopback interface (PPP is OK) 5580Sstevel@tonic-gate */ 5590Sstevel@tonic-gate found = FALSE; 5600Sstevel@tonic-gate while (netmask && count < subnet_count) { 5610Sstevel@tonic-gate if ((netmask & clnt_addr) == (netmask & if_addr)) { 5620Sstevel@tonic-gate bestcount = count; 5630Sstevel@tonic-gate bestmatch = ifn; 5640Sstevel@tonic-gate found = TRUE; 5650Sstevel@tonic-gate break; 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate netmask <<= 1; 5680Sstevel@tonic-gate count++; 5690Sstevel@tonic-gate if (count >= bestcount || count > limit || subnet_match) 5700Sstevel@tonic-gate break; 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * If a subnet level match occurred, note this for 5740Sstevel@tonic-gate * comparison with future subnet matches. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate if (found && (netmask == ntohl(ifn->if_netmask.s_addr))) { 5770Sstevel@tonic-gate subnet_match = TRUE; 5780Sstevel@tonic-gate subnet_count = count; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * If we don't have a match, select the first interface that 5840Sstevel@tonic-gate * is not a loopback interface (and preferably not a PPP interface) 5850Sstevel@tonic-gate * as the best match. 5860Sstevel@tonic-gate */ 5870Sstevel@tonic-gate if (bestmatch == NULL) { 5880Sstevel@tonic-gate for (ifn = if_info; ifn < (if_info + n_ifs); ifn++) { 589*8485SPeter.Memishian@Sun.COM if ((ifn->if_flags & IFF_LOOPBACK) == 0) { 5900Sstevel@tonic-gate bestmatch = ifn; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * If this isn't a PPP interface, we're 5940Sstevel@tonic-gate * done. Otherwise, keep walking through 5950Sstevel@tonic-gate * the list in case we have a non-loopback 5960Sstevel@tonic-gate * iface that ISN'T a PPP further down our 5970Sstevel@tonic-gate * list... 5980Sstevel@tonic-gate */ 5990Sstevel@tonic-gate if ((ifn->if_flags & IFF_POINTOPOINT) == 0) { 6000Sstevel@tonic-gate break; 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate if (bestmatch != NULL) 6070Sstevel@tonic-gate return (bestmatch->if_address.s_addr); 6080Sstevel@tonic-gate else 6090Sstevel@tonic-gate return (0); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate static int 6130Sstevel@tonic-gate is_myself(struct sockaddr_in6 *sa6) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate struct sioc_addrreq areq; 6160Sstevel@tonic-gate int s; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate if ((s = open("/dev/udp6", O_RDONLY)) < 0) { 6190Sstevel@tonic-gate syslog(LOG_ERR, "is_myself: can't open /dev/udp6: %m"); 6200Sstevel@tonic-gate return (0); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 623132Srobinson (void) memcpy(&areq.sa_addr, sa6, sizeof (struct sockaddr_storage)); 6240Sstevel@tonic-gate areq.sa_res = -1; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate if (ioctl(s, SIOCTMYADDR, (caddr_t)&areq) < 0) { 6270Sstevel@tonic-gate syslog(LOG_ERR, "is_myself:SIOCTMYADDR failed: %m"); 628132Srobinson (void) close(s); 6290Sstevel@tonic-gate return (0); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 632132Srobinson (void) close(s); 6330Sstevel@tonic-gate return (areq.sa_res); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate /* 6370Sstevel@tonic-gate * For a given destination address, determine a source address to use. 6380Sstevel@tonic-gate * Returns wildcard address if it cannot determine the source address. 6390Sstevel@tonic-gate * copied from ping.c. 6400Sstevel@tonic-gate */ 6410Sstevel@tonic-gate union any_in_addr { 6420Sstevel@tonic-gate struct in6_addr addr6; 6430Sstevel@tonic-gate struct in_addr addr; 6440Sstevel@tonic-gate }; 645132Srobinson 6460Sstevel@tonic-gate static bool_t 6470Sstevel@tonic-gate select_server_addr(union any_in_addr *dst_addr, int family, 6480Sstevel@tonic-gate union any_in_addr *src_addr) 6490Sstevel@tonic-gate { 6500Sstevel@tonic-gate struct sockaddr *sock; 6510Sstevel@tonic-gate struct sockaddr_in *sin; 6520Sstevel@tonic-gate struct sockaddr_in6 *sin6; 6530Sstevel@tonic-gate int tmp_fd; 654132Srobinson socklen_t sock_len; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate sock = calloc(1, sizeof (struct sockaddr_in6)); 6570Sstevel@tonic-gate if (sock == NULL) { 6580Sstevel@tonic-gate return (FALSE); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if (family == AF_INET) { 662132Srobinson /* LINTED pointer cast */ 6630Sstevel@tonic-gate sin = (struct sockaddr_in *)sock; 6640Sstevel@tonic-gate sin->sin_family = AF_INET; 6650Sstevel@tonic-gate sin->sin_port = 111; 6660Sstevel@tonic-gate sin->sin_addr = dst_addr->addr; 6670Sstevel@tonic-gate sock_len = sizeof (struct sockaddr_in); 6680Sstevel@tonic-gate } else { 669132Srobinson /* LINTED pointer cast */ 6700Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sock; 6710Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 6720Sstevel@tonic-gate sin6->sin6_port = 111; 6730Sstevel@tonic-gate sin6->sin6_addr = dst_addr->addr6; 6740Sstevel@tonic-gate sock_len = sizeof (struct sockaddr_in6); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* open a UDP socket */ 678*8485SPeter.Memishian@Sun.COM tmp_fd = _so_socket(family, SOCK_DGRAM, 0, NULL, SOV_SOCKBSD); 679*8485SPeter.Memishian@Sun.COM if (tmp_fd < 0) { 680*8485SPeter.Memishian@Sun.COM syslog(LOG_ERR, "select_server_addr: connect failed\n"); 6810Sstevel@tonic-gate return (FALSE); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate /* connect it */ 6850Sstevel@tonic-gate if (_so_connect(tmp_fd, sock, sock_len, SOV_SOCKBSD) < 0) { 6860Sstevel@tonic-gate /* 6870Sstevel@tonic-gate * If there's no route to the destination, this connect() call 6880Sstevel@tonic-gate * fails. We just return all-zero (wildcard) as the source 6890Sstevel@tonic-gate * address, so that user can get to see "no route to dest" 6900Sstevel@tonic-gate * message, as it'll try to send the probe packet out and will 6910Sstevel@tonic-gate * receive ICMP unreachable. 6920Sstevel@tonic-gate */ 693*8485SPeter.Memishian@Sun.COM if (family == AF_INET) { 6940Sstevel@tonic-gate src_addr->addr.s_addr = INADDR_ANY; 695*8485SPeter.Memishian@Sun.COM } else { 6960Sstevel@tonic-gate /* 6970Sstevel@tonic-gate * Since in6addr_any is not in the scope 6980Sstevel@tonic-gate * use the following hack 6990Sstevel@tonic-gate */ 700132Srobinson (void) memset(src_addr->addr6.s6_addr, 701*8485SPeter.Memishian@Sun.COM 0, sizeof (struct in6_addr)); 702*8485SPeter.Memishian@Sun.COM } 7030Sstevel@tonic-gate (void) close(tmp_fd); 7040Sstevel@tonic-gate free(sock); 7050Sstevel@tonic-gate return (FALSE); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate /* get the local sock info */ 7090Sstevel@tonic-gate if (_so_getsockname(tmp_fd, sock, &sock_len, SOV_DEFAULT) < 0) { 710*8485SPeter.Memishian@Sun.COM syslog(LOG_ERR, "select_server_addr: getsockname failed\n"); 7110Sstevel@tonic-gate (void) close(tmp_fd); 7120Sstevel@tonic-gate free(sock); 7130Sstevel@tonic-gate return (FALSE); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if (family == AF_INET) { 717132Srobinson /* LINTED pointer cast */ 7180Sstevel@tonic-gate sin = (struct sockaddr_in *)sock; 7190Sstevel@tonic-gate src_addr->addr = sin->sin_addr; 7200Sstevel@tonic-gate } else { 721132Srobinson /* LINTED pointer cast */ 7220Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)sock; 7230Sstevel@tonic-gate src_addr->addr6 = sin6->sin6_addr; 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate (void) close(tmp_fd); 7270Sstevel@tonic-gate free(sock); 7280Sstevel@tonic-gate return (TRUE); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * This internal routine will merge one of those "universal" addresses 7330Sstevel@tonic-gate * to the one which will make sense to the remote caller. 7340Sstevel@tonic-gate */ 7350Sstevel@tonic-gate static char * 736132Srobinson inet_netdir_mergeaddr(struct netconfig *tp, char *ruaddr, char *uaddr) 7370Sstevel@tonic-gate { 7380Sstevel@tonic-gate char tmp[SYS_NMLN], *cp; 7390Sstevel@tonic-gate int j; 7400Sstevel@tonic-gate struct in_addr clientaddr, bestmatch; 7410Sstevel@tonic-gate time_t curtime; 7420Sstevel@tonic-gate int af; 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate if (!uaddr || !ruaddr || !tp) { 7450Sstevel@tonic-gate _nderror = ND_BADARG; 7460Sstevel@tonic-gate return (NULL); 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate (void) bzero(tmp, SYS_NMLN); 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate if (strcmp(tp->nc_protofmly, NC_INET) == 0) 7510Sstevel@tonic-gate af = AF_INET; 7520Sstevel@tonic-gate else 7530Sstevel@tonic-gate af = AF_INET6; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate if (af == AF_INET) { 7560Sstevel@tonic-gate if (strncmp(ruaddr, "0.0.0.0.", strlen("0.0.0.0.")) == 0) 7570Sstevel@tonic-gate /* thats me: return the way it is */ 7580Sstevel@tonic-gate return (strdup(uaddr)); 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * Convert remote uaddr into an in_addr so that we can compare 7620Sstevel@tonic-gate * to it. Shave off last two dotted-decimal values. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate for (cp = ruaddr, j = 0; j < 4; j++, cp++) 7650Sstevel@tonic-gate if ((cp = strchr(cp, '.')) == NULL) 7660Sstevel@tonic-gate break; 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate if (cp != NULL) 7690Sstevel@tonic-gate *--cp = '\0'; /* null out the dot after the IP addr */ 7700Sstevel@tonic-gate else { 7710Sstevel@tonic-gate _nderror = ND_NOHOST; 7720Sstevel@tonic-gate return (NULL); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate clientaddr.s_addr = inet_addr(ruaddr); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate /* We know cp is not NULL due to the check above */ 7780Sstevel@tonic-gate *cp = '.'; /* Put the dot back in the IP addr */ 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate (void) time(&curtime); 7810Sstevel@tonic-gate if ((curtime - last_updated) >= if_cache_refresh_time) { 7820Sstevel@tonic-gate /* 7830Sstevel@tonic-gate * Cache needs to be refreshed. 7840Sstevel@tonic-gate */ 7850Sstevel@tonic-gate if (!update_if_cache()) 7860Sstevel@tonic-gate return (NULL); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * Find the best match now. 7910Sstevel@tonic-gate */ 7920Sstevel@tonic-gate (void) rw_rdlock(&iflock); 7930Sstevel@tonic-gate bestmatch.s_addr = get_best_match(clientaddr); 7940Sstevel@tonic-gate (void) rw_unlock(&iflock); 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate if (bestmatch.s_addr) 7970Sstevel@tonic-gate _nderror = ND_OK; 7980Sstevel@tonic-gate else { 7990Sstevel@tonic-gate _nderror = ND_NOHOST; 8000Sstevel@tonic-gate return (NULL); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* prepare the reply */ 8040Sstevel@tonic-gate (void) memset(tmp, '\0', sizeof (tmp)); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* reply consists of the IP addr of the closest interface */ 8070Sstevel@tonic-gate (void) strcpy(tmp, inet_ntoa(bestmatch)); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate /* 8100Sstevel@tonic-gate * ... and the port number part (last two dotted-decimal values) 8110Sstevel@tonic-gate * of uaddr 8120Sstevel@tonic-gate */ 8130Sstevel@tonic-gate for (cp = uaddr, j = 0; j < 4; j++, cp++) 8140Sstevel@tonic-gate cp = strchr(cp, '.'); 8150Sstevel@tonic-gate (void) strcat(tmp, --cp); 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate } else { 8180Sstevel@tonic-gate /* IPv6 */ 8190Sstevel@tonic-gate char *dot; 8200Sstevel@tonic-gate char *truaddr; 8210Sstevel@tonic-gate struct sockaddr_in6 sa; 8220Sstevel@tonic-gate struct sockaddr_in6 server_addr; 8230Sstevel@tonic-gate union any_in_addr in_addr, out_addr; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (strncmp(ruaddr, "::", strlen("::")) == 0) 8260Sstevel@tonic-gate if (*(ruaddr + strlen("::")) == '\0') 8270Sstevel@tonic-gate /* thats me: return the way it is */ 8280Sstevel@tonic-gate return (strdup(uaddr)); 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate bzero(&sa, sizeof (sa)); 8310Sstevel@tonic-gate bzero(&server_addr, sizeof (server_addr)); 8320Sstevel@tonic-gate truaddr = &tmp[0]; 833132Srobinson (void) strcpy(truaddr, ruaddr); 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * now extract the server ip address from 8370Sstevel@tonic-gate * the address supplied by client. It can be 8380Sstevel@tonic-gate * client's own IP address. 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate if ((dot = strrchr(truaddr, '.')) != 0) { 8420Sstevel@tonic-gate *dot = '\0'; 8430Sstevel@tonic-gate if ((dot = strrchr(truaddr, '.')) != 0) 8440Sstevel@tonic-gate *dot = '\0'; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate if (dot == 0) { 8480Sstevel@tonic-gate _nderror = ND_NOHOST; 8490Sstevel@tonic-gate return (NULL); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate if (inet_pton(af, truaddr, sa.sin6_addr.s6_addr) 8530Sstevel@tonic-gate != 1) { 8540Sstevel@tonic-gate _nderror = ND_NOHOST; 8550Sstevel@tonic-gate return (NULL); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate in_addr.addr6 = sa.sin6_addr; 8590Sstevel@tonic-gate sa.sin6_family = AF_INET6; 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate /* is it my IP address */ 8620Sstevel@tonic-gate if (!is_myself(&sa)) { 8630Sstevel@tonic-gate /* have the kernel select one for me */ 8640Sstevel@tonic-gate if (select_server_addr(&in_addr, af, &out_addr) == 8650Sstevel@tonic-gate FALSE) 8660Sstevel@tonic-gate return (NULL); 8670Sstevel@tonic-gate server_addr.sin6_addr = out_addr.addr6; 868*8485SPeter.Memishian@Sun.COM } else { 869*8485SPeter.Memishian@Sun.COM (void) memcpy(&server_addr, &sa, sizeof (server_addr)); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 872*8485SPeter.Memishian@Sun.COM if (inet_ntop(af, server_addr.sin6_addr.s6_addr, tmp, 873*8485SPeter.Memishian@Sun.COM sizeof (tmp)) == NULL) { 8740Sstevel@tonic-gate _nderror = ND_NOHOST; 8750Sstevel@tonic-gate return (NULL); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate /* now extract the port info */ 8790Sstevel@tonic-gate if ((dot = strrchr(uaddr, '.')) != 0) { 880*8485SPeter.Memishian@Sun.COM char *p = --dot; 8810Sstevel@tonic-gate 882*8485SPeter.Memishian@Sun.COM while (*p-- != '.') 883*8485SPeter.Memishian@Sun.COM ; 8840Sstevel@tonic-gate p++; 885132Srobinson (void) strcat(tmp + strlen(tmp), p); 8860Sstevel@tonic-gate _nderror = ND_OK; 8870Sstevel@tonic-gate } else { 8880Sstevel@tonic-gate _nderror = ND_NOHOST; 8890Sstevel@tonic-gate return (NULL); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate return (strdup(tmp)); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate static int 897132Srobinson bindresvport(struct netconfig *nconf, int fd, struct netbuf *addr) 8980Sstevel@tonic-gate { 8990Sstevel@tonic-gate int res; 9000Sstevel@tonic-gate struct sockaddr_in myaddr; 9010Sstevel@tonic-gate struct sockaddr_in6 myaddr6; 9020Sstevel@tonic-gate struct sockaddr_in *sin; 9030Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9040Sstevel@tonic-gate int i; 9050Sstevel@tonic-gate struct t_bind tbindstr, *tres; 9060Sstevel@tonic-gate struct t_info tinfo; 9070Sstevel@tonic-gate struct t_optmgmt req, resp; 9080Sstevel@tonic-gate struct opthdr *opt; 9090Sstevel@tonic-gate int reqbuf[64/sizeof (int)]; 9100Sstevel@tonic-gate int *optval; 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate union { 9130Sstevel@tonic-gate struct sockaddr_in *sin; 9140Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9150Sstevel@tonic-gate char *buf; 9160Sstevel@tonic-gate } u; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate _nderror = ND_SYSTEM; 9190Sstevel@tonic-gate if (geteuid()) { 9200Sstevel@tonic-gate errno = EACCES; 9210Sstevel@tonic-gate return (-1); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate if ((i = t_getstate(fd)) != T_UNBND) { 9240Sstevel@tonic-gate if (t_errno == TBADF) 9250Sstevel@tonic-gate errno = EBADF; 9260Sstevel@tonic-gate if (i != -1) 9270Sstevel@tonic-gate errno = EISCONN; 9280Sstevel@tonic-gate return (-1); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 9320Sstevel@tonic-gate if (addr == NULL) { 9330Sstevel@tonic-gate sin = &myaddr; 934132Srobinson (void) memset(sin, 0, sizeof (*sin)); 9350Sstevel@tonic-gate sin->sin_family = AF_INET; 9360Sstevel@tonic-gate u.buf = (char *)sin; 9370Sstevel@tonic-gate } else 9380Sstevel@tonic-gate u.buf = (char *)addr->buf; 9390Sstevel@tonic-gate } else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) { 9400Sstevel@tonic-gate if (addr == NULL) { 9410Sstevel@tonic-gate sin6 = &myaddr6; 942132Srobinson (void) memset(sin6, 0, sizeof (*sin6)); 9430Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 9440Sstevel@tonic-gate u.buf = (char *)sin6; 9450Sstevel@tonic-gate } else 9460Sstevel@tonic-gate u.buf = addr->buf; 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate } else { 9490Sstevel@tonic-gate errno = EPFNOSUPPORT; 9500Sstevel@tonic-gate return (-1); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate /* Transform sockaddr_in to netbuf */ 9540Sstevel@tonic-gate if (t_getinfo(fd, &tinfo) == -1) 9550Sstevel@tonic-gate return (-1); 956132Srobinson /* LINTED pointer cast */ 9570Sstevel@tonic-gate tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR); 9580Sstevel@tonic-gate if (tres == NULL) { 9590Sstevel@tonic-gate _nderror = ND_NOMEM; 9600Sstevel@tonic-gate return (-1); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate tbindstr.qlen = 0; /* Always 0; user should change if he wants to */ 9640Sstevel@tonic-gate tbindstr.addr.buf = (char *)u.buf; 9650Sstevel@tonic-gate tbindstr.addr.len = tbindstr.addr.maxlen = __rpc_get_a_size(tinfo.addr); 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* 9680Sstevel@tonic-gate * Use *_ANONPRIVBIND to ask the kernel to pick a port in the 9690Sstevel@tonic-gate * priviledged range for us. 9700Sstevel@tonic-gate */ 9710Sstevel@tonic-gate opt = (struct opthdr *)reqbuf; 9720Sstevel@tonic-gate if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 9730Sstevel@tonic-gate opt->level = IPPROTO_TCP; 9740Sstevel@tonic-gate opt->name = TCP_ANONPRIVBIND; 9750Sstevel@tonic-gate } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) { 9760Sstevel@tonic-gate opt->level = IPPROTO_UDP; 9770Sstevel@tonic-gate opt->name = UDP_ANONPRIVBIND; 9780Sstevel@tonic-gate } else { 9790Sstevel@tonic-gate errno = EPROTONOSUPPORT; 9800Sstevel@tonic-gate (void) t_free((char *)tres, T_BIND); 9810Sstevel@tonic-gate return (-1); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate opt->len = sizeof (int); 9850Sstevel@tonic-gate req.flags = T_NEGOTIATE; 9860Sstevel@tonic-gate req.opt.len = sizeof (struct opthdr) + opt->len; 9870Sstevel@tonic-gate req.opt.buf = (char *)opt; 988132Srobinson /* LINTED pointer cast */ 9890Sstevel@tonic-gate optval = (int *)((char *)reqbuf + sizeof (struct opthdr)); 9900Sstevel@tonic-gate *optval = 1; 9910Sstevel@tonic-gate resp.flags = 0; 9920Sstevel@tonic-gate resp.opt.buf = (char *)reqbuf; 9930Sstevel@tonic-gate resp.opt.maxlen = sizeof (reqbuf); 9940Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 9950Sstevel@tonic-gate (void) t_free((char *)tres, T_BIND); 9960Sstevel@tonic-gate return (-1); 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate if (u.sin->sin_family == AF_INET) 10000Sstevel@tonic-gate u.sin->sin_port = htons(0); 10010Sstevel@tonic-gate else 10020Sstevel@tonic-gate u.sin6->sin6_port = htons(0); 10030Sstevel@tonic-gate res = t_bind(fd, &tbindstr, tres); 10040Sstevel@tonic-gate if (res != 0) { 10050Sstevel@tonic-gate if (t_errno == TNOADDR) { 10060Sstevel@tonic-gate _nderror = ND_FAILCTRL; 10070Sstevel@tonic-gate res = 1; 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate } else { 10100Sstevel@tonic-gate _nderror = ND_OK; 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate /* 10140Sstevel@tonic-gate * Always turn off the option when we are done. Note that by doing 10150Sstevel@tonic-gate * this, if the caller has set this option before calling 10160Sstevel@tonic-gate * bindresvport(), it will be unset. Better be safe... 10170Sstevel@tonic-gate */ 1018*8485SPeter.Memishian@Sun.COM *optval = 0; 10190Sstevel@tonic-gate resp.flags = 0; 10200Sstevel@tonic-gate resp.opt.buf = (char *)reqbuf; 10210Sstevel@tonic-gate resp.opt.maxlen = sizeof (reqbuf); 10220Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 10230Sstevel@tonic-gate (void) t_free((char *)tres, T_BIND); 10240Sstevel@tonic-gate if (res == 0) 10250Sstevel@tonic-gate (void) t_unbind(fd); 10260Sstevel@tonic-gate _nderror = ND_FAILCTRL; 10270Sstevel@tonic-gate return (-1); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate (void) t_free((char *)tres, T_BIND); 10310Sstevel@tonic-gate return (res); 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate static int 1035132Srobinson checkresvport(struct netbuf *addr) 10360Sstevel@tonic-gate { 10370Sstevel@tonic-gate struct sockaddr_in *sin; 10380Sstevel@tonic-gate unsigned short port; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate if (addr == NULL) { 10410Sstevel@tonic-gate _nderror = ND_FAILCTRL; 10420Sstevel@tonic-gate return (-1); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate /* 10450Sstevel@tonic-gate * Still works for IPv6 since the first two memebers of 10460Sstevel@tonic-gate * both address structure point to family and port # respectively 10470Sstevel@tonic-gate */ 1048132Srobinson /* LINTED pointer cast */ 10490Sstevel@tonic-gate sin = (struct sockaddr_in *)(addr->buf); 10500Sstevel@tonic-gate port = ntohs(sin->sin_port); 10510Sstevel@tonic-gate if (port < IPPORT_RESERVED) 10520Sstevel@tonic-gate return (0); 10530Sstevel@tonic-gate return (1); 10540Sstevel@tonic-gate } 1055