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*1914Scasper * Common Development and Distribution License (the "License").
6*1914Scasper * 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 */
210Sstevel@tonic-gate /*
22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Shim library which should be LD_PRELOADed before running applications
300Sstevel@tonic-gate * that interact with NCA but do not explicitly use the AF_NCA family.
310Sstevel@tonic-gate * This library overloads AF_INET's version of bind(3SOCKET) with AF_NCA's
320Sstevel@tonic-gate * version. The new version of bind checks to see if that the port is one
330Sstevel@tonic-gate * NCA is listening on, closes the socket(3SOCKET), and opens a new one
340Sstevel@tonic-gate * the family AF_NCA. Afterwards, the real bind(3SOCKET) is called
350Sstevel@tonic-gate * descriptors, etc. *
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * Compile: cc -Kpic -G -o ncad_addr.so ncad_addr.c -lsocket -lnsl
380Sstevel@tonic-gate * Use: LD_PRELOAD=/path/to/ncad_addr.so my_program
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <assert.h>
430Sstevel@tonic-gate #include <dlfcn.h>
440Sstevel@tonic-gate #include <door.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <inet/nd.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <stropts.h>
500Sstevel@tonic-gate #include <sys/stat.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <sys/mman.h>
540Sstevel@tonic-gate #include <netdb.h>
550Sstevel@tonic-gate #include <ctype.h>
560Sstevel@tonic-gate #include <sys/types.h>
570Sstevel@tonic-gate #include <sys/socket.h>
580Sstevel@tonic-gate #include <netinet/in.h>
590Sstevel@tonic-gate #include <arpa/inet.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate #pragma weak bind = nca_bind
620Sstevel@tonic-gate #pragma init(ncad_init)
630Sstevel@tonic-gate #pragma fini(ncad_fini)
640Sstevel@tonic-gate
650Sstevel@tonic-gate #define SEPARATOR '/'
660Sstevel@tonic-gate
670Sstevel@tonic-gate typedef int sfunc1_t(int, int, int);
680Sstevel@tonic-gate typedef int sfunc2_t(int, const struct sockaddr *, socklen_t);
690Sstevel@tonic-gate
700Sstevel@tonic-gate static sfunc1_t *real_socket;
710Sstevel@tonic-gate static sfunc2_t *real_bind;
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * It is used to represent an address NCA is willing to handle.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate typedef struct nca_address_s {
770Sstevel@tonic-gate uint16_t port; /* port, in network byte order */
780Sstevel@tonic-gate ipaddr_t ipaddr; /* IP address, in network byte order */
790Sstevel@tonic-gate } nca_address_t;
800Sstevel@tonic-gate
810Sstevel@tonic-gate static uint32_t addrcount; /* current address count */
820Sstevel@tonic-gate static uint32_t addrcapacity; /* capacity of ncaaddrs */
830Sstevel@tonic-gate static nca_address_t *ncaaddrs; /* array for all addresses */
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * It loads all NCA addresses from a configuration file. A NCA address
870Sstevel@tonic-gate * entry is: ncaport=IPaddress:port. The line above can be repeatly for other
880Sstevel@tonic-gate * addresses. If IPaddress is '*', then it is translated into INADDR_ANY.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate static void
ncad_init(void)910Sstevel@tonic-gate ncad_init(void)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate uint16_t port;
940Sstevel@tonic-gate ipaddr_t addr;
950Sstevel@tonic-gate FILE *fp;
960Sstevel@tonic-gate char *s, *p, *q;
970Sstevel@tonic-gate char buffer[1024];
980Sstevel@tonic-gate const char *filename = "/etc/nca/ncaport.conf";
990Sstevel@tonic-gate
1000Sstevel@tonic-gate real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket");
1010Sstevel@tonic-gate real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind");
1020Sstevel@tonic-gate
103*1914Scasper if ((fp = fopen(filename, "rF")) == NULL) {
1040Sstevel@tonic-gate (void) fprintf(stderr, "Failed to open file %s for reading in "
1050Sstevel@tonic-gate " ncad_addr.so. Error = %s\n",
1060Sstevel@tonic-gate filename,
1070Sstevel@tonic-gate (p = strerror(errno)) ? p : "unknown error");
1080Sstevel@tonic-gate return;
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate while (fgets(buffer, sizeof (buffer), fp) != NULL) {
1120Sstevel@tonic-gate s = buffer;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /* remove '\n' at the end from fgets() */
1150Sstevel@tonic-gate p = strchr(s, '\n');
1160Sstevel@tonic-gate if (p != NULL)
1170Sstevel@tonic-gate *p = '\0';
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* remove spaces from the front */
1200Sstevel@tonic-gate while (*s != '\0' && isspace(*s))
1210Sstevel@tonic-gate s++;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (*s == '\0' || *s == '#')
1240Sstevel@tonic-gate continue;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate /* it should start with ncaport= */
1270Sstevel@tonic-gate p = strchr(s, '=');
1280Sstevel@tonic-gate if (p == NULL || strncasecmp(s, "ncaport", 7) != 0)
1290Sstevel@tonic-gate continue;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate p++;
1320Sstevel@tonic-gate while (*p != '\0' && isspace(*p))
1330Sstevel@tonic-gate p++;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate q = strchr(p, SEPARATOR);
1360Sstevel@tonic-gate if (q == NULL)
1370Sstevel@tonic-gate continue;
1380Sstevel@tonic-gate *q++ = '\0';
1390Sstevel@tonic-gate if (strcmp(p, "*") == 0) {
1400Sstevel@tonic-gate addr = INADDR_ANY;
1410Sstevel@tonic-gate } else {
1420Sstevel@tonic-gate if (inet_pton(AF_INET, p, &addr) != 1) {
1430Sstevel@tonic-gate struct in6_addr addr6;
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate if (inet_pton(AF_INET6, p, &addr6) == 1) {
1460Sstevel@tonic-gate (void) fprintf(stderr,
1470Sstevel@tonic-gate "NCA does not support IPv6\n");
1480Sstevel@tonic-gate } else {
1490Sstevel@tonic-gate (void) fprintf(stderr,
1500Sstevel@tonic-gate "Invalid IP address: %s\n", p);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate continue;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate port = atoi(q);
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /* array is full, expand it */
1580Sstevel@tonic-gate if (addrcount == addrcapacity) {
1590Sstevel@tonic-gate if (addrcapacity == 0)
1600Sstevel@tonic-gate addrcapacity = 64;
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate addrcapacity *= 2;
1630Sstevel@tonic-gate ncaaddrs = realloc(ncaaddrs,
1640Sstevel@tonic-gate addrcapacity * sizeof (nca_address_t));
1650Sstevel@tonic-gate if (ncaaddrs == NULL) {
1660Sstevel@tonic-gate (void) fprintf(stderr, "out of memory");
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate ncaaddrs[addrcount].ipaddr = addr;
1720Sstevel@tonic-gate ncaaddrs[addrcount].port = htons(port);
1730Sstevel@tonic-gate addrcount++;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate (void) fclose(fp);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * It destroys memory at the end of program.
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate static void
ncad_fini(void)1830Sstevel@tonic-gate ncad_fini(void)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate if (ncaaddrs != NULL) {
1860Sstevel@tonic-gate free(ncaaddrs);
1870Sstevel@tonic-gate ncaaddrs = NULL;
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * If the bind is happening on a port NCA is listening on, close
1930Sstevel@tonic-gate * the socket and open a new one with family AF_NCA.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate static int
nca_bind(int sock,const struct sockaddr * name,socklen_t namelen)1960Sstevel@tonic-gate nca_bind(int sock, const struct sockaddr *name, socklen_t namelen)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate struct sockaddr_in sin;
1990Sstevel@tonic-gate int new_sock;
2000Sstevel@tonic-gate int i;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate if (sock < 0) {
2030Sstevel@tonic-gate errno = EBADF;
2040Sstevel@tonic-gate return (-1);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (real_socket == NULL) {
2080Sstevel@tonic-gate if ((real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket"))
2090Sstevel@tonic-gate == NULL) {
2100Sstevel@tonic-gate errno = EAGAIN;
2110Sstevel@tonic-gate exit(-1);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if (real_bind == NULL) {
2160Sstevel@tonic-gate if ((real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind"))
2170Sstevel@tonic-gate == NULL) {
2180Sstevel@tonic-gate errno = EAGAIN;
2190Sstevel@tonic-gate exit(-1);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate if (name == NULL ||
2240Sstevel@tonic-gate ncaaddrs == NULL ||
2250Sstevel@tonic-gate name->sa_family != AF_INET ||
2260Sstevel@tonic-gate namelen != sizeof (sin)) {
2270Sstevel@tonic-gate return (real_bind(sock, name, namelen));
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate (void) memcpy(&sin, name, sizeof (sin));
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * If it is one of the addresses NCA is handling, convert it
2340Sstevel@tonic-gate * to NCA socket.
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate for (i = 0; i < addrcount; i++) {
2370Sstevel@tonic-gate if (sin.sin_port == ncaaddrs[i].port &&
2380Sstevel@tonic-gate (sin.sin_addr.s_addr == ncaaddrs[i].ipaddr ||
2390Sstevel@tonic-gate ncaaddrs[i].ipaddr == INADDR_ANY)) {
2400Sstevel@tonic-gate /* convert to NCA socket */
2410Sstevel@tonic-gate new_sock = real_socket(AF_NCA, SOCK_STREAM, 0);
2420Sstevel@tonic-gate if (new_sock >= 0) {
2430Sstevel@tonic-gate (void) dup2(new_sock, sock);
2440Sstevel@tonic-gate (void) close(new_sock);
2450Sstevel@tonic-gate sin.sin_family = AF_NCA;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate break;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate return (real_bind(sock, (struct sockaddr *)&sin, namelen));
2520Sstevel@tonic-gate }
253