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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*d762223fSYuri Pankov * 227c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*d762223fSYuri Pankov * 25*d762223fSYuri Pankov * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 337c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * XXX This routine should be changed to use 387c478bd9Sstevel@tonic-gate * ND_CHECK_RESERVED_PORT and ND_SET_RESERVED_PORT 397c478bd9Sstevel@tonic-gate * which can be invoked via netdir_options. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate #include <stdio.h> 427c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 437c478bd9Sstevel@tonic-gate #include <netinet/in.h> 447c478bd9Sstevel@tonic-gate #include <sys/socket.h> 457c478bd9Sstevel@tonic-gate #include <netdb.h> 467c478bd9Sstevel@tonic-gate #include <errno.h> 477c478bd9Sstevel@tonic-gate #include <rpc/nettype.h> 487c478bd9Sstevel@tonic-gate #include <stropts.h> 497c478bd9Sstevel@tonic-gate #include <string.h> 507c478bd9Sstevel@tonic-gate #include <tiuser.h> 517c478bd9Sstevel@tonic-gate #include <unistd.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define STARTPORT 600 547c478bd9Sstevel@tonic-gate #define ENDPORT (IPPORT_RESERVED - 1) 557c478bd9Sstevel@tonic-gate #define NPORTS (ENDPORT - STARTPORT + 1) 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * The argument is a client handle for a UDP connection. 597c478bd9Sstevel@tonic-gate * Unbind its transport endpoint from the existing port 607c478bd9Sstevel@tonic-gate * and rebind it to a reserved port. 617c478bd9Sstevel@tonic-gate * On failure, the client handle can be unbound even if it 627c478bd9Sstevel@tonic-gate * was previously bound. Callers should destroy the client 637c478bd9Sstevel@tonic-gate * handle after a failure. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate int 667c478bd9Sstevel@tonic-gate __clnt_bindresvport(cl) 677c478bd9Sstevel@tonic-gate CLIENT *cl; 687c478bd9Sstevel@tonic-gate { 697c478bd9Sstevel@tonic-gate int fd; 707c478bd9Sstevel@tonic-gate int res; 717c478bd9Sstevel@tonic-gate short port; 727c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 737c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 747c478bd9Sstevel@tonic-gate extern int errno; 757c478bd9Sstevel@tonic-gate /* extern int t_errno; */ 767c478bd9Sstevel@tonic-gate struct t_bind *tbind, *tres; 777c478bd9Sstevel@tonic-gate int i; 787c478bd9Sstevel@tonic-gate bool_t ipv6_fl = FALSE; 797c478bd9Sstevel@tonic-gate struct netconfig *nconf; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* make sure it's a UDP connection */ 827c478bd9Sstevel@tonic-gate nconf = getnetconfigent(cl->cl_netid); 837c478bd9Sstevel@tonic-gate if (nconf == NULL) 847c478bd9Sstevel@tonic-gate return (-1); 857c478bd9Sstevel@tonic-gate if ((nconf->nc_semantics != NC_TPI_CLTS) || 867c478bd9Sstevel@tonic-gate (strcmp(nconf->nc_protofmly, NC_INET) && 87*d762223fSYuri Pankov strcmp(nconf->nc_protofmly, NC_INET6)) || 887c478bd9Sstevel@tonic-gate strcmp(nconf->nc_proto, NC_UDP)) { 897c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 907c478bd9Sstevel@tonic-gate return (0); /* not udp - don't need resv port */ 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) 937c478bd9Sstevel@tonic-gate ipv6_fl = TRUE; 947c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate if (!clnt_control(cl, CLGET_FD, (char *)&fd)) { 977c478bd9Sstevel@tonic-gate return (-1); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* If fd is already bound - unbind it */ 1017c478bd9Sstevel@tonic-gate if (t_getstate(fd) != T_UNBND) { 1027c478bd9Sstevel@tonic-gate while ((t_unbind(fd) < 0) && (t_errno == TLOOK)) { 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * If there is a message queued to this descriptor, 1057c478bd9Sstevel@tonic-gate * remove it. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate struct strbuf ctl[1], data[1]; 1087c478bd9Sstevel@tonic-gate char ctlbuf[sizeof (union T_primitives) + 32]; 1097c478bd9Sstevel@tonic-gate char databuf[256]; 1107c478bd9Sstevel@tonic-gate int flags; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate ctl->maxlen = sizeof (ctlbuf); 1137c478bd9Sstevel@tonic-gate ctl->buf = ctlbuf; 1147c478bd9Sstevel@tonic-gate data->maxlen = sizeof (databuf); 1157c478bd9Sstevel@tonic-gate data->buf = databuf; 1167c478bd9Sstevel@tonic-gate flags = 0; 1177c478bd9Sstevel@tonic-gate if (getmsg(fd, ctl, data, &flags) < 0) 1187c478bd9Sstevel@tonic-gate return (-1); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate if (t_getstate(fd) != T_UNBND) 1227c478bd9Sstevel@tonic-gate return (-1); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR); 1267c478bd9Sstevel@tonic-gate if (tbind == NULL) { 1277c478bd9Sstevel@tonic-gate if (t_errno == TBADF) 1287c478bd9Sstevel@tonic-gate errno = EBADF; 1297c478bd9Sstevel@tonic-gate return (-1); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR); 1327c478bd9Sstevel@tonic-gate if (tres == NULL) { 1337c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 1347c478bd9Sstevel@tonic-gate return (-1); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate (void) memset((char *)tbind->addr.buf, 0, tbind->addr.len); 1387c478bd9Sstevel@tonic-gate /* warning: this sockaddr_in is truncated to 8 bytes */ 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (ipv6_fl == TRUE) { 1417c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)tbind->addr.buf; 1427c478bd9Sstevel@tonic-gate sin6->sin6_family = AF_INET6; 1437c478bd9Sstevel@tonic-gate } else { 1447c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)tbind->addr.buf; 1457c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate tbind->qlen = 0; 1497c478bd9Sstevel@tonic-gate tbind->addr.len = tbind->addr.maxlen; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * Need to find a reserved port in the interval 1537c478bd9Sstevel@tonic-gate * STARTPORT - ENDPORT. Choose a random starting 1547c478bd9Sstevel@tonic-gate * place in the interval based on the process pid 1557c478bd9Sstevel@tonic-gate * and sequentially search the ports for one 1567c478bd9Sstevel@tonic-gate * that is available. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate port = (getpid() % NPORTS) + STARTPORT; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate for (i = 0; i < NPORTS; i++) { 161*d762223fSYuri Pankov if (ipv6_fl == TRUE) 162*d762223fSYuri Pankov sin6->sin6_port = htons(port++); 163*d762223fSYuri Pankov else 1647c478bd9Sstevel@tonic-gate sin->sin_port = htons(port++); 1657c478bd9Sstevel@tonic-gate if (port > ENDPORT) 1667c478bd9Sstevel@tonic-gate port = STARTPORT; 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * Try to bind to the requested address. If 1697c478bd9Sstevel@tonic-gate * the call to t_bind succeeds, then we need 1707c478bd9Sstevel@tonic-gate * to make sure that the address that we bound 1717c478bd9Sstevel@tonic-gate * to was the address that we requested. If it 1727c478bd9Sstevel@tonic-gate * was, then we are done. If not, we fake an 1737c478bd9Sstevel@tonic-gate * EADDRINUSE error by setting res, t_errno, 1747c478bd9Sstevel@tonic-gate * and errno to indicate that a bind failure 1757c478bd9Sstevel@tonic-gate * occurred. Otherwise, if the t_bind call 1767c478bd9Sstevel@tonic-gate * failed, we check to see whether it makes 1777c478bd9Sstevel@tonic-gate * sense to continue trying to t_bind requests. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate res = t_bind(fd, tbind, tres); 1807c478bd9Sstevel@tonic-gate if (res == 0) { 1817c478bd9Sstevel@tonic-gate if (memcmp(tbind->addr.buf, tres->addr.buf, 1827c478bd9Sstevel@tonic-gate (int)tres->addr.len) == 0) 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate (void) t_unbind(fd); 1857c478bd9Sstevel@tonic-gate res = -1; 1867c478bd9Sstevel@tonic-gate t_errno = TSYSERR; 1877c478bd9Sstevel@tonic-gate errno = EADDRINUSE; 1887c478bd9Sstevel@tonic-gate } else if (t_errno != TSYSERR || errno != EADDRINUSE) { 1897c478bd9Sstevel@tonic-gate if (t_errno == TACCES) 1907c478bd9Sstevel@tonic-gate errno = EACCES; 1917c478bd9Sstevel@tonic-gate break; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate (void) t_free((char *)tbind, T_BIND); 1967c478bd9Sstevel@tonic-gate (void) t_free((char *)tres, T_BIND); 1977c478bd9Sstevel@tonic-gate return (res); 1987c478bd9Sstevel@tonic-gate } 199