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
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * 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 */
216812Sraf
220Sstevel@tonic-gate /*
23*13055SAnders.Persson@Sun.COM * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate * under license from the Regents of the University of California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/socket.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * XXX The functions in this file are only needed to support transport
45*13055SAnders.Persson@Sun.COM * providers that have not yet been converted to use /etc/sock2path.d.
460Sstevel@tonic-gate * Once all transport providers have been converted this file can be
470Sstevel@tonic-gate * removed.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate static struct netconfig *_s_match_netconf(int family, int type, int proto,
510Sstevel@tonic-gate void **nethandle);
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * The following two string arrays map a number as specified
550Sstevel@tonic-gate * by a user of sockets, to the string as would be returned
560Sstevel@tonic-gate * by a call to getnetconfig().
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * They are used by _s_match_netconf();
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * proto_sw contains protocol entries for which there is a corresponding
610Sstevel@tonic-gate * /dev device. All others would presumably use raw IP and download the
620Sstevel@tonic-gate * desired protocol.
630Sstevel@tonic-gate */
640Sstevel@tonic-gate static char *proto_sw[] = {
650Sstevel@tonic-gate "",
660Sstevel@tonic-gate "icmp", /* 1 = ICMP */
670Sstevel@tonic-gate "",
680Sstevel@tonic-gate "",
690Sstevel@tonic-gate "",
700Sstevel@tonic-gate "",
710Sstevel@tonic-gate "tcp", /* 6 = TCP */
720Sstevel@tonic-gate "",
730Sstevel@tonic-gate "",
740Sstevel@tonic-gate "",
750Sstevel@tonic-gate "",
760Sstevel@tonic-gate "",
770Sstevel@tonic-gate "",
780Sstevel@tonic-gate "",
790Sstevel@tonic-gate "",
800Sstevel@tonic-gate "",
810Sstevel@tonic-gate "",
820Sstevel@tonic-gate "udp", /* 17 = UDP */
830Sstevel@tonic-gate };
840Sstevel@tonic-gate
850Sstevel@tonic-gate static char *family_sw[] = {
860Sstevel@tonic-gate "-", /* 0 = AF_UNSPEC */
870Sstevel@tonic-gate "loopback", /* 1 = AF_UNIX */
880Sstevel@tonic-gate "inet", /* 2 = AF_INET */
890Sstevel@tonic-gate "implink", /* 3 = AF_IMPLINK */
900Sstevel@tonic-gate "pup", /* 4 = AF_PUP */
910Sstevel@tonic-gate "chaos", /* 5 = AF_CHAOS */
920Sstevel@tonic-gate "ns", /* 6 = AF_NS */
930Sstevel@tonic-gate "nbs", /* 7 = AF_NBS */
940Sstevel@tonic-gate "ecma", /* 8 = AF_ECMA */
950Sstevel@tonic-gate "datakit", /* 9 = AF_DATAKIT */
960Sstevel@tonic-gate "ccitt", /* 10 = AF_CCITT */
970Sstevel@tonic-gate "sna", /* 11 = AF_SNA */
980Sstevel@tonic-gate "decnet", /* 12 = AF_DECnet */
990Sstevel@tonic-gate "dli", /* 13 = AF_DLI */
1000Sstevel@tonic-gate "lat", /* 14 = AF_LAT */
1010Sstevel@tonic-gate "hylink", /* 15 = AF_HYLINK */
1020Sstevel@tonic-gate "appletalk", /* 16 = AF_APPLETALK */
1030Sstevel@tonic-gate "nit", /* 17 = AF_NIT */
1040Sstevel@tonic-gate "ieee802", /* 18 = AF_802 */
1050Sstevel@tonic-gate "osi", /* 19 = AF_OSI */
1060Sstevel@tonic-gate "x25", /* 20 = AF_X25 */
1070Sstevel@tonic-gate "osinet", /* 21 = AF_OSINET */
1080Sstevel@tonic-gate "gosip", /* 22 = AF_GOSIP */
1090Sstevel@tonic-gate "ipx", /* 23 = AF_IPX */
1100Sstevel@tonic-gate "route", /* 24 = AF_ROUTE */
1110Sstevel@tonic-gate "link", /* 25 = AF_LINK */
1120Sstevel@tonic-gate "inet6", /* 26 = AF_INET6 */
1130Sstevel@tonic-gate "key", /* 27 = AF_KEY */
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * Lookup family/type/protocol in /etc/netconfig.
1180Sstevel@tonic-gate * Returns the pathname and a prototype value (to be passed into SO_PROTOTYPE)
1190Sstevel@tonic-gate * The path is malloc'ed and has to be freed by the caller.
1200Sstevel@tonic-gate */
1210Sstevel@tonic-gate int
_s_netconfig_path(int family,int type,int protocol,char ** pathp,int * prototype)1220Sstevel@tonic-gate _s_netconfig_path(int family, int type, int protocol,
1230Sstevel@tonic-gate char **pathp, int *prototype)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate struct netconfig *net;
1260Sstevel@tonic-gate void *nethandle;
1270Sstevel@tonic-gate struct stat stats;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate net = _s_match_netconf(family, type, protocol, &nethandle);
1300Sstevel@tonic-gate if (net == NULL)
1310Sstevel@tonic-gate return (-1);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (strcmp(net->nc_proto, NC_NOPROTO) != 0)
1340Sstevel@tonic-gate *prototype = 0;
1350Sstevel@tonic-gate else
1360Sstevel@tonic-gate *prototype = protocol;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate retry:
1396812Sraf if (stat(net->nc_device, &stats) < 0) {
1400Sstevel@tonic-gate switch (errno) {
1410Sstevel@tonic-gate case EINTR:
1420Sstevel@tonic-gate goto retry;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate case ENOENT:
1450Sstevel@tonic-gate case ENOLINK:
1460Sstevel@tonic-gate case ELOOP:
1470Sstevel@tonic-gate case EMULTIHOP:
1480Sstevel@tonic-gate case ENOTDIR:
1490Sstevel@tonic-gate errno = EPFNOSUPPORT;
1500Sstevel@tonic-gate break;
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate endnetconfig(nethandle); /* finished with netconfig struct */
1530Sstevel@tonic-gate return (-1);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate if (!S_ISCHR(stats.st_mode)) {
1560Sstevel@tonic-gate errno = EPFNOSUPPORT;
1570Sstevel@tonic-gate endnetconfig(nethandle); /* finished with netconfig struct */
1580Sstevel@tonic-gate return (-1);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate *pathp = malloc(strlen(net->nc_device) + 1);
1610Sstevel@tonic-gate if (*pathp == NULL) {
1620Sstevel@tonic-gate endnetconfig(nethandle);
1630Sstevel@tonic-gate errno = ENOMEM;
1640Sstevel@tonic-gate return (-1);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate (void) strcpy(*pathp, net->nc_device);
1670Sstevel@tonic-gate endnetconfig(nethandle); /* finished with netconfig struct */
1680Sstevel@tonic-gate return (0);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * Match config entry for protocol
1730Sstevel@tonic-gate * requested.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate static struct netconfig *
_s_match_netconf(int family,int type,int proto,void ** nethandle)1760Sstevel@tonic-gate _s_match_netconf(int family, int type, int proto, void **nethandle)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate struct netconfig *net;
1790Sstevel@tonic-gate struct netconfig *maybe;
1800Sstevel@tonic-gate char *oproto;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (family < 0 ||
1836812Sraf family >= (int)sizeof (family_sw) / (int)sizeof (char *) ||
1846812Sraf proto < 0 || proto >= IPPROTO_MAX) {
1856812Sraf errno = EPROTONOSUPPORT;
1866812Sraf return (NULL);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate if (proto) {
1890Sstevel@tonic-gate if (proto >= (int)sizeof (proto_sw) / (int)sizeof (char *))
1900Sstevel@tonic-gate oproto = "";
1916812Sraf else
1926812Sraf oproto = proto_sw[proto];
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Loop through each entry in netconfig
1970Sstevel@tonic-gate * until one matches or we reach the end.
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate if ((*nethandle = setnetconfig()) == NULL) {
2000Sstevel@tonic-gate return (NULL);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate maybe = NULL;
2040Sstevel@tonic-gate while ((net = getnetconfig(*nethandle)) != NULL) {
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * We make a copy of net->nc_semantics rather than modifying
2070Sstevel@tonic-gate * it in place because the network selection code shares the
2080Sstevel@tonic-gate * structures returned by getnetconfig() among all its callers.
2090Sstevel@tonic-gate * See bug #1160886 for more details.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate unsigned int semantics = net->nc_semantics;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (semantics == NC_TPI_COTS_ORD)
2140Sstevel@tonic-gate semantics = NC_TPI_COTS;
2150Sstevel@tonic-gate if (proto) {
2160Sstevel@tonic-gate if (strcmp(net->nc_protofmly, family_sw[family]) == 0 &&
2170Sstevel@tonic-gate semantics == type &&
2180Sstevel@tonic-gate strcmp(net->nc_proto, oproto) == 0)
2190Sstevel@tonic-gate break;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (strcmp(net->nc_protofmly, family_sw[family]) == 0 &&
2220Sstevel@tonic-gate type == SOCK_RAW &&
2230Sstevel@tonic-gate semantics == SOCK_RAW &&
2240Sstevel@tonic-gate strcmp(net->nc_proto, NC_NOPROTO) == 0 &&
2250Sstevel@tonic-gate maybe == NULL)
2260Sstevel@tonic-gate maybe = net; /* in case no exact match */
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate continue;
2290Sstevel@tonic-gate } else {
2300Sstevel@tonic-gate if (strcmp(net->nc_protofmly, family_sw[family]) == 0 &&
2316812Sraf semantics == type) {
2320Sstevel@tonic-gate break;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate if (net == NULL && maybe)
2370Sstevel@tonic-gate net = maybe;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if (net == NULL) {
2400Sstevel@tonic-gate endnetconfig(*nethandle);
2410Sstevel@tonic-gate errno = EPROTONOSUPPORT;
2420Sstevel@tonic-gate return (NULL);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate return (net);
2460Sstevel@tonic-gate }
247