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 5*004388ebScasper * Common Development and Distribution License (the "License"). 6*004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*004388ebScasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Shim library which should be LD_PRELOADed before running applications 307c478bd9Sstevel@tonic-gate * that interact with NCA but do not explicitly use the AF_NCA family. 317c478bd9Sstevel@tonic-gate * This library overloads AF_INET's version of bind(3SOCKET) with AF_NCA's 327c478bd9Sstevel@tonic-gate * version. The new version of bind checks to see if that the port is one 337c478bd9Sstevel@tonic-gate * NCA is listening on, closes the socket(3SOCKET), and opens a new one 347c478bd9Sstevel@tonic-gate * the family AF_NCA. Afterwards, the real bind(3SOCKET) is called 357c478bd9Sstevel@tonic-gate * descriptors, etc. * 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * Compile: cc -Kpic -G -o ncad_addr.so ncad_addr.c -lsocket -lnsl 387c478bd9Sstevel@tonic-gate * Use: LD_PRELOAD=/path/to/ncad_addr.so my_program 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <stdio.h> 427c478bd9Sstevel@tonic-gate #include <assert.h> 437c478bd9Sstevel@tonic-gate #include <dlfcn.h> 447c478bd9Sstevel@tonic-gate #include <door.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 467c478bd9Sstevel@tonic-gate #include <fcntl.h> 477c478bd9Sstevel@tonic-gate #include <inet/nd.h> 487c478bd9Sstevel@tonic-gate #include <unistd.h> 497c478bd9Sstevel@tonic-gate #include <stropts.h> 507c478bd9Sstevel@tonic-gate #include <sys/stat.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <sys/mman.h> 547c478bd9Sstevel@tonic-gate #include <netdb.h> 557c478bd9Sstevel@tonic-gate #include <ctype.h> 567c478bd9Sstevel@tonic-gate #include <sys/types.h> 577c478bd9Sstevel@tonic-gate #include <sys/socket.h> 587c478bd9Sstevel@tonic-gate #include <netinet/in.h> 597c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #pragma weak bind = nca_bind 627c478bd9Sstevel@tonic-gate #pragma init(ncad_init) 637c478bd9Sstevel@tonic-gate #pragma fini(ncad_fini) 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define SEPARATOR '/' 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate typedef int sfunc1_t(int, int, int); 687c478bd9Sstevel@tonic-gate typedef int sfunc2_t(int, const struct sockaddr *, socklen_t); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static sfunc1_t *real_socket; 717c478bd9Sstevel@tonic-gate static sfunc2_t *real_bind; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * It is used to represent an address NCA is willing to handle. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate typedef struct nca_address_s { 777c478bd9Sstevel@tonic-gate uint16_t port; /* port, in network byte order */ 787c478bd9Sstevel@tonic-gate ipaddr_t ipaddr; /* IP address, in network byte order */ 797c478bd9Sstevel@tonic-gate } nca_address_t; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static uint32_t addrcount; /* current address count */ 827c478bd9Sstevel@tonic-gate static uint32_t addrcapacity; /* capacity of ncaaddrs */ 837c478bd9Sstevel@tonic-gate static nca_address_t *ncaaddrs; /* array for all addresses */ 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * It loads all NCA addresses from a configuration file. A NCA address 877c478bd9Sstevel@tonic-gate * entry is: ncaport=IPaddress:port. The line above can be repeatly for other 887c478bd9Sstevel@tonic-gate * addresses. If IPaddress is '*', then it is translated into INADDR_ANY. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate static void 917c478bd9Sstevel@tonic-gate ncad_init(void) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate uint16_t port; 947c478bd9Sstevel@tonic-gate ipaddr_t addr; 957c478bd9Sstevel@tonic-gate FILE *fp; 967c478bd9Sstevel@tonic-gate char *s, *p, *q; 977c478bd9Sstevel@tonic-gate char buffer[1024]; 987c478bd9Sstevel@tonic-gate const char *filename = "/etc/nca/ncaport.conf"; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket"); 1017c478bd9Sstevel@tonic-gate real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind"); 1027c478bd9Sstevel@tonic-gate 103*004388ebScasper if ((fp = fopen(filename, "rF")) == NULL) { 1047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Failed to open file %s for reading in " 1057c478bd9Sstevel@tonic-gate " ncad_addr.so. Error = %s\n", 1067c478bd9Sstevel@tonic-gate filename, 1077c478bd9Sstevel@tonic-gate (p = strerror(errno)) ? p : "unknown error"); 1087c478bd9Sstevel@tonic-gate return; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate while (fgets(buffer, sizeof (buffer), fp) != NULL) { 1127c478bd9Sstevel@tonic-gate s = buffer; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* remove '\n' at the end from fgets() */ 1157c478bd9Sstevel@tonic-gate p = strchr(s, '\n'); 1167c478bd9Sstevel@tonic-gate if (p != NULL) 1177c478bd9Sstevel@tonic-gate *p = '\0'; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* remove spaces from the front */ 1207c478bd9Sstevel@tonic-gate while (*s != '\0' && isspace(*s)) 1217c478bd9Sstevel@tonic-gate s++; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (*s == '\0' || *s == '#') 1247c478bd9Sstevel@tonic-gate continue; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* it should start with ncaport= */ 1277c478bd9Sstevel@tonic-gate p = strchr(s, '='); 1287c478bd9Sstevel@tonic-gate if (p == NULL || strncasecmp(s, "ncaport", 7) != 0) 1297c478bd9Sstevel@tonic-gate continue; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate p++; 1327c478bd9Sstevel@tonic-gate while (*p != '\0' && isspace(*p)) 1337c478bd9Sstevel@tonic-gate p++; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate q = strchr(p, SEPARATOR); 1367c478bd9Sstevel@tonic-gate if (q == NULL) 1377c478bd9Sstevel@tonic-gate continue; 1387c478bd9Sstevel@tonic-gate *q++ = '\0'; 1397c478bd9Sstevel@tonic-gate if (strcmp(p, "*") == 0) { 1407c478bd9Sstevel@tonic-gate addr = INADDR_ANY; 1417c478bd9Sstevel@tonic-gate } else { 1427c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET, p, &addr) != 1) { 1437c478bd9Sstevel@tonic-gate struct in6_addr addr6; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, p, &addr6) == 1) { 1467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1477c478bd9Sstevel@tonic-gate "NCA does not support IPv6\n"); 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1507c478bd9Sstevel@tonic-gate "Invalid IP address: %s\n", p); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate continue; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate port = atoi(q); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* array is full, expand it */ 1587c478bd9Sstevel@tonic-gate if (addrcount == addrcapacity) { 1597c478bd9Sstevel@tonic-gate if (addrcapacity == 0) 1607c478bd9Sstevel@tonic-gate addrcapacity = 64; 1617c478bd9Sstevel@tonic-gate else 1627c478bd9Sstevel@tonic-gate addrcapacity *= 2; 1637c478bd9Sstevel@tonic-gate ncaaddrs = realloc(ncaaddrs, 1647c478bd9Sstevel@tonic-gate addrcapacity * sizeof (nca_address_t)); 1657c478bd9Sstevel@tonic-gate if (ncaaddrs == NULL) { 1667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "out of memory"); 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate ncaaddrs[addrcount].ipaddr = addr; 1727c478bd9Sstevel@tonic-gate ncaaddrs[addrcount].port = htons(port); 1737c478bd9Sstevel@tonic-gate addrcount++; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate (void) fclose(fp); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * It destroys memory at the end of program. 1817c478bd9Sstevel@tonic-gate */ 1827c478bd9Sstevel@tonic-gate static void 1837c478bd9Sstevel@tonic-gate ncad_fini(void) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate if (ncaaddrs != NULL) { 1867c478bd9Sstevel@tonic-gate free(ncaaddrs); 1877c478bd9Sstevel@tonic-gate ncaaddrs = NULL; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate /* 1927c478bd9Sstevel@tonic-gate * If the bind is happening on a port NCA is listening on, close 1937c478bd9Sstevel@tonic-gate * the socket and open a new one with family AF_NCA. 1947c478bd9Sstevel@tonic-gate */ 1957c478bd9Sstevel@tonic-gate static int 1967c478bd9Sstevel@tonic-gate nca_bind(int sock, const struct sockaddr *name, socklen_t namelen) 1977c478bd9Sstevel@tonic-gate { 1987c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 1997c478bd9Sstevel@tonic-gate int new_sock; 2007c478bd9Sstevel@tonic-gate int i; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate if (sock < 0) { 2037c478bd9Sstevel@tonic-gate errno = EBADF; 2047c478bd9Sstevel@tonic-gate return (-1); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (real_socket == NULL) { 2087c478bd9Sstevel@tonic-gate if ((real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket")) 2097c478bd9Sstevel@tonic-gate == NULL) { 2107c478bd9Sstevel@tonic-gate errno = EAGAIN; 2117c478bd9Sstevel@tonic-gate exit(-1); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate if (real_bind == NULL) { 2167c478bd9Sstevel@tonic-gate if ((real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind")) 2177c478bd9Sstevel@tonic-gate == NULL) { 2187c478bd9Sstevel@tonic-gate errno = EAGAIN; 2197c478bd9Sstevel@tonic-gate exit(-1); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (name == NULL || 2247c478bd9Sstevel@tonic-gate ncaaddrs == NULL || 2257c478bd9Sstevel@tonic-gate name->sa_family != AF_INET || 2267c478bd9Sstevel@tonic-gate namelen != sizeof (sin)) { 2277c478bd9Sstevel@tonic-gate return (real_bind(sock, name, namelen)); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate (void) memcpy(&sin, name, sizeof (sin)); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* 2337c478bd9Sstevel@tonic-gate * If it is one of the addresses NCA is handling, convert it 2347c478bd9Sstevel@tonic-gate * to NCA socket. 2357c478bd9Sstevel@tonic-gate */ 2367c478bd9Sstevel@tonic-gate for (i = 0; i < addrcount; i++) { 2377c478bd9Sstevel@tonic-gate if (sin.sin_port == ncaaddrs[i].port && 2387c478bd9Sstevel@tonic-gate (sin.sin_addr.s_addr == ncaaddrs[i].ipaddr || 2397c478bd9Sstevel@tonic-gate ncaaddrs[i].ipaddr == INADDR_ANY)) { 2407c478bd9Sstevel@tonic-gate /* convert to NCA socket */ 2417c478bd9Sstevel@tonic-gate new_sock = real_socket(AF_NCA, SOCK_STREAM, 0); 2427c478bd9Sstevel@tonic-gate if (new_sock >= 0) { 2437c478bd9Sstevel@tonic-gate (void) dup2(new_sock, sock); 2447c478bd9Sstevel@tonic-gate (void) close(new_sock); 2457c478bd9Sstevel@tonic-gate sin.sin_family = AF_NCA; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate break; 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate return (real_bind(sock, (struct sockaddr *)&sin, namelen)); 2527c478bd9Sstevel@tonic-gate } 253