2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1989, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * svc_generic.c, Server side for RPC.
2N/A *
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include <stdlib.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <netinet/tcp.h>
2N/A#include <netinet/udp.h>
2N/A#include <inttypes.h>
2N/A#include "rpc_mt.h"
2N/A#include <stdio.h>
2N/A#include <rpc/rpc.h>
2N/A#include <sys/types.h>
2N/A#include <errno.h>
2N/A#include <syslog.h>
2N/A#include <rpc/nettype.h>
2N/A#include <malloc.h>
2N/A#include <string.h>
2N/A#include <stropts.h>
2N/A#include <tsol/label.h>
2N/A#include <nfs/nfs.h>
2N/A#include <nfs/nfs_acl.h>
2N/A#include <rpcsvc/mount.h>
2N/A#include <rpcsvc/nsm_addr.h>
2N/A#include <rpcsvc/rquota.h>
2N/A#include <rpcsvc/sm_inter.h>
2N/A#include <rpcsvc/nlm_prot.h>
2N/A
2N/Aextern int __svc_vc_setflag(SVCXPRT *, int);
2N/A
2N/Aextern SVCXPRT *svc_dg_create_private(int, uint_t, uint_t);
2N/Aextern SVCXPRT *svc_vc_create_private(int, uint_t, uint_t);
2N/Aextern SVCXPRT *svc_fd_create_private(int, uint_t, uint_t);
2N/A
2N/Aextern bool_t __svc_add_to_xlist(SVCXPRT_LIST **, SVCXPRT *, mutex_t *);
2N/Aextern void __svc_free_xlist(SVCXPRT_LIST **, mutex_t *);
2N/A
2N/Aextern bool_t __rpc_try_doors(const char *, bool_t *);
2N/A
2N/A/*
2N/A * The highest level interface for server creation.
2N/A * It tries for all the nettokens in that particular class of token
2N/A * and returns the number of handles it can create and/or find.
2N/A *
2N/A * It creates a link list of all the handles it could create.
2N/A * If svc_create() is called multiple times, it uses the handle
2N/A * created earlier instead of creating a new handle every time.
2N/A */
2N/A
2N/A/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
2N/A
2N/ASVCXPRT_LIST *_svc_xprtlist = NULL;
2N/Aextern mutex_t xprtlist_lock;
2N/A
2N/Astatic SVCXPRT * svc_tli_create_common(int, const struct netconfig *,
2N/A const struct t_bind *, uint_t, uint_t, boolean_t);
2N/A
2N/Aboolean_t
2N/Ais_multilevel(rpcprog_t prognum)
2N/A{
2N/A /* This is a list of identified multilevel service provider */
2N/A if ((prognum == MOUNTPROG) || (prognum == NFS_PROGRAM) ||
2N/A (prognum == NFS_ACL_PROGRAM) || (prognum == NLM_PROG) ||
2N/A (prognum == NSM_ADDR_PROGRAM) || (prognum == RQUOTAPROG) ||
2N/A (prognum == SM_PROG))
2N/A return (B_TRUE);
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/Avoid
2N/A__svc_free_xprtlist(void)
2N/A{
2N/A __svc_free_xlist(&_svc_xprtlist, &xprtlist_lock);
2N/A}
2N/A
2N/Aint
2N/Asvc_create(void (*dispatch)(), const rpcprog_t prognum, const rpcvers_t versnum,
2N/A const char *nettype)
2N/A{
2N/A SVCXPRT_LIST *l;
2N/A int num = 0;
2N/A SVCXPRT *xprt;
2N/A struct netconfig *nconf;
2N/A void *handle;
2N/A bool_t try_others;
2N/A
2N/A /*
2N/A * Check if service should register over doors transport.
2N/A */
2N/A if (__rpc_try_doors(nettype, &try_others)) {
2N/A if (svc_door_create(dispatch, prognum, versnum, 0) == NULL)
2N/A (void) syslog(LOG_ERR,
2N/A "svc_create: could not register over doors");
2N/A else
2N/A num++;
2N/A }
2N/A if (!try_others)
2N/A return (num);
2N/A if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
2N/A (void) syslog(LOG_ERR, "svc_create: unknown protocol");
2N/A return (0);
2N/A }
2N/A while (nconf = __rpc_getconf(handle)) {
2N/A (void) mutex_lock(&xprtlist_lock);
2N/A for (l = _svc_xprtlist; l; l = l->next) {
2N/A if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
2N/A /* Found an old one, use it */
2N/A (void) rpcb_unset(prognum, versnum, nconf);
2N/A if (svc_reg(l->xprt, prognum, versnum,
2N/A dispatch, nconf) == FALSE)
2N/A (void) syslog(LOG_ERR,
2N/A "svc_create: could not register prog %d vers %d on %s",
2N/A prognum, versnum, nconf->nc_netid);
2N/A else
2N/A num++;
2N/A break;
2N/A }
2N/A }
2N/A (void) mutex_unlock(&xprtlist_lock);
2N/A if (l == NULL) {
2N/A /* It was not found. Now create a new one */
2N/A xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
2N/A if (xprt) {
2N/A if (!__svc_add_to_xlist(&_svc_xprtlist, xprt,
2N/A &xprtlist_lock)) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_create: no memory");
2N/A return (0);
2N/A }
2N/A num++;
2N/A }
2N/A }
2N/A }
2N/A __rpc_endconf(handle);
2N/A /*
2N/A * In case of num == 0; the error messages are generated by the
2N/A * underlying layers; and hence not needed here.
2N/A */
2N/A return (num);
2N/A}
2N/A
2N/A/*
2N/A * The high level interface to svc_tli_create().
2N/A * It tries to create a server for "nconf" and registers the service
2N/A * with the rpcbind. It calls svc_tli_create();
2N/A */
2N/ASVCXPRT *
2N/Asvc_tp_create(void (*dispatch)(), const rpcprog_t prognum,
2N/A const rpcvers_t versnum, const struct netconfig *nconf)
2N/A{
2N/A SVCXPRT *xprt;
2N/A boolean_t anon_mlp = B_FALSE;
2N/A
2N/A if (nconf == NULL) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tp_create: invalid netconfig structure for prog %d vers %d",
2N/A prognum, versnum);
2N/A return (NULL);
2N/A }
2N/A
2N/A /* Some programs need to allocate MLP for multilevel services */
2N/A if (is_system_labeled() && is_multilevel(prognum))
2N/A anon_mlp = B_TRUE;
2N/A xprt = svc_tli_create_common(RPC_ANYFD, nconf, NULL, 0, 0, anon_mlp);
2N/A if (xprt == NULL)
2N/A return (NULL);
2N/A
2N/A (void) rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
2N/A if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tp_create: Could not register prog %d vers %d on %s",
2N/A prognum, versnum, nconf->nc_netid);
2N/A SVC_DESTROY(xprt);
2N/A return (NULL);
2N/A }
2N/A return (xprt);
2N/A}
2N/A
2N/ASVCXPRT *
2N/Asvc_tli_create(const int fd, const struct netconfig *nconf,
2N/A const struct t_bind *bindaddr, const uint_t sendsz, const uint_t recvsz)
2N/A{
2N/A return (svc_tli_create_common(fd, nconf, bindaddr, sendsz, recvsz, 0));
2N/A}
2N/A
2N/A/*
2N/A * If fd is RPC_ANYFD, then it opens a fd for the given transport
2N/A * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
2N/A * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
2N/A * NULL bindadr and Connection oriented transports, the value of qlen
2N/A * is set arbitrarily.
2N/A *
2N/A * If sendsz or recvsz are zero, their default values are chosen.
2N/A */
2N/ASVCXPRT *
2N/Asvc_tli_create_common(const int ofd, const struct netconfig *nconf,
2N/A const struct t_bind *bindaddr, const uint_t sendsz,
2N/A const uint_t recvsz, boolean_t mlp_flag)
2N/A{
2N/A SVCXPRT *xprt = NULL; /* service handle */
2N/A struct t_info tinfo; /* transport info */
2N/A struct t_bind *tres = NULL; /* bind info */
2N/A bool_t madefd = FALSE; /* whether fd opened here */
2N/A int state; /* state of the transport provider */
2N/A int fd = ofd;
2N/A
2N/A if (fd == RPC_ANYFD) {
2N/A if (nconf == NULL) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: invalid netconfig");
2N/A return (NULL);
2N/A }
2N/A fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
2N/A if (fd == -1) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not open connection for %s: %s",
2N/A nconf->nc_netid, errorstr);
2N/A return (NULL);
2N/A }
2N/A madefd = TRUE;
2N/A state = T_UNBND;
2N/A } else {
2N/A /*
2N/A * It is an open descriptor. Sync it & get the transport info.
2N/A */
2N/A if ((state = t_sync(fd)) == -1) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not do t_sync: %s",
2N/A errorstr);
2N/A return (NULL);
2N/A }
2N/A if (t_getinfo(fd, &tinfo) == -1) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not get transport information: %s",
2N/A errorstr);
2N/A return (NULL);
2N/A }
2N/A /* Enable options of returning the ip's for udp */
2N/A if (nconf) {
2N/A int ret = 0;
2N/A if (strcmp(nconf->nc_netid, "udp6") == 0) {
2N/A ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
2N/A IPV6_RECVPKTINFO, 1);
2N/A if (ret < 0) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: IPV6_RECVPKTINFO(1): %s",
2N/A errorstr);
2N/A return (NULL);
2N/A }
2N/A } else if (strcmp(nconf->nc_netid, "udp") == 0) {
2N/A ret = __rpc_tli_set_options(fd, IPPROTO_IP,
2N/A IP_RECVDSTADDR, 1);
2N/A if (ret < 0) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: IP_RECVDSTADDR(1): %s",
2N/A errorstr);
2N/A return (NULL);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If the fd is unbound, try to bind it.
2N/A * In any case, try to get its bound info in tres
2N/A */
2N/A tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
2N/A if (tres == NULL) {
2N/A (void) syslog(LOG_ERR, "svc_tli_create: No memory!");
2N/A goto freedata;
2N/A }
2N/A
2N/A switch (state) {
2N/A bool_t tcp, exclbind;
2N/A case T_UNBND:
2N/A /* If this is a labeled system, then ask for an MLP */
2N/A if (is_system_labeled() &&
2N/A (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
2N/A strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
2N/A (void) __rpc_tli_set_options(fd, SOL_SOCKET,
2N/A SO_RECVUCRED, 1);
2N/A if (mlp_flag)
2N/A (void) __rpc_tli_set_options(fd, SOL_SOCKET,
2N/A SO_ANON_MLP, 1);
2N/A }
2N/A
2N/A /*
2N/A * SO_EXCLBIND has the following properties
2N/A * - an fd bound to port P via IPv4 will prevent an IPv6
2N/A * bind to port P (and vice versa)
2N/A * - an fd bound to a wildcard IP address for port P will
2N/A * prevent a more specific IP address bind to port P
2N/A * (see {tcp,udp}.c for details)
2N/A *
2N/A * We use the latter property to prevent hijacking of RPC
2N/A * services that reside at non-privileged ports.
2N/A */
2N/A tcp = nconf ? (strcmp(nconf->nc_proto, NC_TCP) == 0) : 0;
2N/A if (nconf &&
2N/A (tcp || (strcmp(nconf->nc_proto, NC_UDP) == 0)) &&
2N/A rpc_control(__RPC_SVC_EXCLBIND_GET, &exclbind)) {
2N/A if (exclbind) {
2N/A if (__rpc_tli_set_options(fd, SOL_SOCKET,
2N/A SO_EXCLBIND, 1) < 0) {
2N/A syslog(LOG_ERR,
2N/A "svc_tli_create: can't set EXCLBIND [netid='%s']",
2N/A nconf->nc_netid);
2N/A goto freedata;
2N/A }
2N/A }
2N/A }
2N/A if (bindaddr) {
2N/A if (t_bind(fd, (struct t_bind *)bindaddr,
2N/A tres) == -1) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not bind: %s",
2N/A errorstr);
2N/A goto freedata;
2N/A }
2N/A /*
2N/A * Should compare the addresses only if addr.len
2N/A * was non-zero
2N/A */
2N/A if (bindaddr->addr.len &&
2N/A (memcmp(bindaddr->addr.buf, tres->addr.buf,
2N/A (int)tres->addr.len) != 0)) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not bind to requested address: %s",
2N/A "address mismatch");
2N/A goto freedata;
2N/A }
2N/A } else {
2N/A tres->qlen = 64; /* Chosen Arbitrarily */
2N/A tres->addr.len = 0;
2N/A if (t_bind(fd, tres, tres) == -1) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: could not bind: %s",
2N/A errorstr);
2N/A goto freedata;
2N/A }
2N/A }
2N/A
2N/A /* Enable options of returning the ip's for udp */
2N/A if (nconf) {
2N/A int ret = 0;
2N/A if (strcmp(nconf->nc_netid, "udp6") == 0) {
2N/A ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
2N/A IPV6_RECVPKTINFO, 1);
2N/A if (ret < 0) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: IPV6_RECVPKTINFO(2): %s",
2N/A errorstr);
2N/A goto freedata;
2N/A }
2N/A } else if (strcmp(nconf->nc_netid, "udp") == 0) {
2N/A ret = __rpc_tli_set_options(fd, IPPROTO_IP,
2N/A IP_RECVDSTADDR, 1);
2N/A if (ret < 0) {
2N/A char errorstr[100];
2N/A
2N/A __tli_sys_strerror(errorstr, sizeof (errorstr),
2N/A t_errno, errno);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: IP_RECVDSTADDR(2): %s",
2N/A errorstr);
2N/A goto freedata;
2N/A }
2N/A }
2N/A }
2N/A break;
2N/A
2N/A case T_IDLE:
2N/A if (bindaddr) {
2N/A /* Copy the entire stuff in tres */
2N/A if (tres->addr.maxlen < bindaddr->addr.len) {
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: illegal netbuf length");
2N/A goto freedata;
2N/A }
2N/A tres->addr.len = bindaddr->addr.len;
2N/A (void) memcpy(tres->addr.buf, bindaddr->addr.buf,
2N/A (int)tres->addr.len);
2N/A } else
2N/A if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
2N/A tres->addr.len = 0;
2N/A break;
2N/A case T_INREL:
2N/A (void) t_rcvrel(fd);
2N/A (void) t_sndrel(fd);
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: other side wants to\
2N/Arelease connection");
2N/A goto freedata;
2N/A
2N/A case T_INCON:
2N/A /* Do nothing here. Assume this is handled in rendezvous */
2N/A break;
2N/A case T_DATAXFER:
2N/A /*
2N/A * This takes care of the case where a fd
2N/A * is passed on which a connection has already
2N/A * been accepted.
2N/A */
2N/A if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
2N/A tres->addr.len = 0;
2N/A break;
2N/A default:
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: connection in a wierd state (%d)", state);
2N/A goto freedata;
2N/A }
2N/A
2N/A /*
2N/A * call transport specific function.
2N/A */
2N/A switch (tinfo.servtype) {
2N/A case T_COTS_ORD:
2N/A case T_COTS:
2N/A if (state == T_DATAXFER)
2N/A xprt = svc_fd_create_private(fd, sendsz,
2N/A recvsz);
2N/A else
2N/A xprt = svc_vc_create_private(fd, sendsz,
2N/A recvsz);
2N/A if (!nconf || !xprt)
2N/A break;
2N/A if ((tinfo.servtype == T_COTS_ORD) &&
2N/A (state != T_DATAXFER) &&
2N/A (strcmp(nconf->nc_protofmly, "inet") == 0))
2N/A (void) __svc_vc_setflag(xprt, TRUE);
2N/A break;
2N/A case T_CLTS:
2N/A xprt = svc_dg_create_private(fd, sendsz, recvsz);
2N/A break;
2N/A default:
2N/A (void) syslog(LOG_ERR,
2N/A "svc_tli_create: bad service type");
2N/A goto freedata;
2N/A }
2N/A if (xprt == NULL)
2N/A /*
2N/A * The error messages here are spitted out by the lower layers:
2N/A * svc_vc_create(), svc_fd_create() and svc_dg_create().
2N/A */
2N/A goto freedata;
2N/A
2N/A /* fill in the other xprt information */
2N/A
2N/A /* Assign the local bind address */
2N/A xprt->xp_ltaddr = tres->addr;
2N/A /* Fill in type of service */
2N/A xprt->xp_type = tinfo.servtype;
2N/A tres->addr.buf = NULL;
2N/A (void) t_free((char *)tres, T_BIND);
2N/A tres = NULL;
2N/A
2N/A xprt->xp_rtaddr.len = 0;
2N/A xprt->xp_rtaddr.maxlen = __rpc_get_a_size(tinfo.addr);
2N/A
2N/A /* Allocate space for the remote bind info */
2N/A if ((xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen)) == NULL) {
2N/A (void) syslog(LOG_ERR, "svc_tli_create: No memory!");
2N/A goto freedata;
2N/A }
2N/A
2N/A if (nconf) {
2N/A xprt->xp_netid = strdup(nconf->nc_netid);
2N/A if (xprt->xp_netid == NULL) {
2N/A if (xprt->xp_rtaddr.buf)
2N/A free(xprt->xp_rtaddr.buf);
2N/A syslog(LOG_ERR, "svc_tli_create: strdup failed!");
2N/A goto freedata;
2N/A }
2N/A xprt->xp_tp = strdup(nconf->nc_device);
2N/A if (xprt->xp_tp == NULL) {
2N/A if (xprt->xp_rtaddr.buf)
2N/A free(xprt->xp_rtaddr.buf);
2N/A if (xprt->xp_netid)
2N/A free(xprt->xp_netid);
2N/A syslog(LOG_ERR, "svc_tli_create: strdup failed!");
2N/A goto freedata;
2N/A }
2N/A }
2N/A
2N/A/*
2N/A * if (madefd && (tinfo.servtype == T_CLTS))
2N/A * (void) ioctl(fd, I_POP, NULL);
2N/A */
2N/A xprt_register(xprt);
2N/A return (xprt);
2N/A
2N/Afreedata:
2N/A if (madefd)
2N/A (void) t_close(fd);
2N/A if (tres)
2N/A (void) t_free((char *)tres, T_BIND);
2N/A if (xprt) {
2N/A if (!madefd) /* so that svc_destroy doesnt close fd */
2N/A xprt->xp_fd = RPC_ANYFD;
2N/A SVC_DESTROY(xprt);
2N/A }
2N/A return (NULL);
2N/A}