Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libc / port / nsl / svc_generic.c
blob441c6a0d99da6bcf5bcebf78fae597460828450d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
31 * svc_generic.c, Server side for RPC.
35 #include "mt.h"
36 #include <stdlib.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <netinet/udp.h>
41 #include <inttypes.h>
42 #include "rpc_mt.h"
43 #include <stdio.h>
44 #include <rpc/rpc.h>
45 #include <sys/types.h>
46 #include <errno.h>
47 #include <syslog.h>
48 #include <rpc/nettype.h>
49 #include <malloc.h>
50 #include <string.h>
51 #include <stropts.h>
52 #include <nfs/nfs.h>
53 #include <nfs/nfs_acl.h>
54 #include <rpcsvc/mount.h>
55 #include <rpcsvc/nsm_addr.h>
56 #include <rpcsvc/rquota.h>
57 #include <rpcsvc/sm_inter.h>
58 #include <rpcsvc/nlm_prot.h>
60 extern int __svc_vc_setflag(SVCXPRT *, int);
62 extern SVCXPRT *svc_dg_create_private(int, uint_t, uint_t);
63 extern SVCXPRT *svc_vc_create_private(int, uint_t, uint_t);
64 extern SVCXPRT *svc_fd_create_private(int, uint_t, uint_t);
66 extern bool_t __svc_add_to_xlist(SVCXPRT_LIST **, SVCXPRT *, mutex_t *);
67 extern void __svc_free_xlist(SVCXPRT_LIST **, mutex_t *);
69 extern bool_t __rpc_try_doors(const char *, bool_t *);
72 * The highest level interface for server creation.
73 * It tries for all the nettokens in that particular class of token
74 * and returns the number of handles it can create and/or find.
76 * It creates a link list of all the handles it could create.
77 * If svc_create() is called multiple times, it uses the handle
78 * created earlier instead of creating a new handle every time.
81 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
83 SVCXPRT_LIST *_svc_xprtlist = NULL;
84 extern mutex_t xprtlist_lock;
86 static SVCXPRT * svc_tli_create_common(int, const struct netconfig *,
87 const struct t_bind *, uint_t, uint_t);
89 static SVCXPRT *svc_tp_create_bind(void (*dispatch)(),
90 const rpcprog_t, const rpcvers_t,
91 const struct netconfig *, const struct t_bind *);
93 void
94 __svc_free_xprtlist(void)
96 __svc_free_xlist(&_svc_xprtlist, &xprtlist_lock);
99 int
100 svc_create(void (*dispatch)(), const rpcprog_t prognum, const rpcvers_t versnum,
101 const char *nettype)
103 SVCXPRT_LIST *l;
104 int num = 0;
105 SVCXPRT *xprt;
106 struct netconfig *nconf;
107 void *handle;
108 bool_t try_others;
111 * Check if service should register over doors transport.
113 if (__rpc_try_doors(nettype, &try_others)) {
114 if (svc_door_create(dispatch, prognum, versnum, 0) == NULL)
115 (void) syslog(LOG_ERR,
116 "svc_create: could not register over doors");
117 else
118 num++;
120 if (!try_others)
121 return (num);
122 if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
123 (void) syslog(LOG_ERR, "svc_create: unknown protocol");
124 return (0);
126 while (nconf = __rpc_getconf(handle)) {
127 (void) mutex_lock(&xprtlist_lock);
128 for (l = _svc_xprtlist; l; l = l->next) {
129 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
130 /* Found an old one, use it */
131 (void) rpcb_unset(prognum, versnum, nconf);
132 if (svc_reg(l->xprt, prognum, versnum,
133 dispatch, nconf) == FALSE)
134 (void) syslog(LOG_ERR, "svc_create: "
135 "could not register prog %d vers "
136 "%d on %s",
137 prognum, versnum, nconf->nc_netid);
138 else
139 num++;
140 break;
143 (void) mutex_unlock(&xprtlist_lock);
144 if (l == NULL) {
145 /* It was not found. Now create a new one */
146 xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
147 if (xprt) {
148 if (!__svc_add_to_xlist(&_svc_xprtlist, xprt,
149 &xprtlist_lock)) {
150 (void) syslog(LOG_ERR,
151 "svc_create: no memory");
152 return (0);
154 num++;
158 __rpc_endconf(handle);
160 * In case of num == 0; the error messages are generated by the
161 * underlying layers; and hence not needed here.
163 return (num);
167 * The high level interface to svc_tli_create().
168 * It tries to create a server for "nconf" and registers the service
169 * with the rpcbind.
171 SVCXPRT *
172 svc_tp_create(void (*dispatch)(), const rpcprog_t prognum,
173 const rpcvers_t versnum, const struct netconfig *nconf)
175 return (svc_tp_create_bind(dispatch, prognum, versnum, nconf, NULL));
179 * svc_tp_create_addr()
180 * Variant of svc_tp_create() that allows specifying just the
181 * the binding address, for convenience.
183 SVCXPRT *
184 svc_tp_create_addr(void (*dispatch)(), const rpcprog_t prognum,
185 const rpcvers_t versnum, const struct netconfig *nconf,
186 const struct netbuf *addr)
188 struct t_bind bind;
189 struct t_bind *bindp = NULL;
191 if (addr != NULL) {
193 bind.addr = *addr;
194 if (!rpc_control(__RPC_SVC_LSTNBKLOG_GET, &bind.qlen)) {
195 syslog(LOG_ERR,
196 "svc_tp_create: can't get listen backlog");
197 return (NULL);
199 bindp = &bind;
203 * When bindp == NULL, this is the same as svc_tp_create().
205 return (svc_tp_create_bind(dispatch, prognum, versnum,
206 nconf, bindp));
209 static SVCXPRT *
210 svc_tp_create_bind(void (*dispatch)(), const rpcprog_t prognum,
211 const rpcvers_t versnum, const struct netconfig *nconf,
212 const struct t_bind *bindaddr)
214 SVCXPRT *xprt;
216 if (nconf == NULL) {
217 (void) syslog(LOG_ERR, "svc_tp_create: invalid netconfig "
218 "structure for prog %d vers %d", prognum, versnum);
219 return (NULL);
222 xprt = svc_tli_create_common(RPC_ANYFD, nconf, bindaddr, 0, 0);
223 if (xprt == NULL)
224 return (NULL);
226 (void) rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
227 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
228 (void) syslog(LOG_ERR,
229 "svc_tp_create: Could not register prog %d vers %d on %s",
230 prognum, versnum, nconf->nc_netid);
231 SVC_DESTROY(xprt);
232 return (NULL);
234 return (xprt);
237 SVCXPRT *
238 svc_tli_create(const int fd, const struct netconfig *nconf,
239 const struct t_bind *bindaddr, const uint_t sendsz, const uint_t recvsz)
241 return (svc_tli_create_common(fd, nconf, bindaddr, sendsz, recvsz));
245 * If fd is RPC_ANYFD, then it opens a fd for the given transport
246 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
247 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
248 * NULL bindadr and Connection oriented transports, the value of qlen
249 * is set arbitrarily.
251 * If sendsz or recvsz are zero, their default values are chosen.
253 SVCXPRT *
254 svc_tli_create_common(const int ofd, const struct netconfig *nconf,
255 const struct t_bind *bindaddr, const uint_t sendsz,
256 const uint_t recvsz)
258 SVCXPRT *xprt = NULL; /* service handle */
259 struct t_info tinfo; /* transport info */
260 struct t_bind *tres = NULL; /* bind info */
261 bool_t madefd = FALSE; /* whether fd opened here */
262 int state; /* state of the transport provider */
263 int fd = ofd;
265 if (fd == RPC_ANYFD) {
266 if (nconf == NULL) {
267 (void) syslog(LOG_ERR,
268 "svc_tli_create: invalid netconfig");
269 return (NULL);
271 fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
272 if (fd == -1) {
273 char errorstr[100];
275 __tli_sys_strerror(errorstr, sizeof (errorstr),
276 t_errno, errno);
277 (void) syslog(LOG_ERR, "svc_tli_create: could not open "
278 "connection for %s: %s", nconf->nc_netid, errorstr);
279 return (NULL);
281 madefd = TRUE;
282 state = T_UNBND;
283 } else {
285 * It is an open descriptor. Sync it & get the transport info.
287 if ((state = t_sync(fd)) == -1) {
288 char errorstr[100];
290 __tli_sys_strerror(errorstr, sizeof (errorstr),
291 t_errno, errno);
292 (void) syslog(LOG_ERR,
293 "svc_tli_create: could not do t_sync: %s",
294 errorstr);
295 return (NULL);
297 if (t_getinfo(fd, &tinfo) == -1) {
298 char errorstr[100];
300 __tli_sys_strerror(errorstr, sizeof (errorstr),
301 t_errno, errno);
302 (void) syslog(LOG_ERR, "svc_tli_create: could not get "
303 "transport information: %s", errorstr);
304 return (NULL);
306 /* Enable options of returning the ip's for udp */
307 if (nconf) {
308 int ret = 0;
309 if (strcmp(nconf->nc_netid, "udp6") == 0) {
310 ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
311 IPV6_RECVPKTINFO, 1);
312 if (ret < 0) {
313 char errorstr[100];
315 __tli_sys_strerror(errorstr,
316 sizeof (errorstr), t_errno, errno);
317 (void) syslog(LOG_ERR,
318 "svc_tli_create: "
319 "IPV6_RECVPKTINFO(1): %s",
320 errorstr);
321 return (NULL);
323 } else if (strcmp(nconf->nc_netid, "udp") == 0) {
324 ret = __rpc_tli_set_options(fd, IPPROTO_IP,
325 IP_RECVDSTADDR, 1);
326 if (ret < 0) {
327 char errorstr[100];
329 __tli_sys_strerror(errorstr,
330 sizeof (errorstr), t_errno, errno);
331 (void) syslog(LOG_ERR,
332 "svc_tli_create: "
333 "IP_RECVDSTADDR(1): %s", errorstr);
334 return (NULL);
341 * If the fd is unbound, try to bind it.
342 * In any case, try to get its bound info in tres
344 /* LINTED pointer alignment */
345 tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
346 if (tres == NULL) {
347 (void) syslog(LOG_ERR, "svc_tli_create: No memory!");
348 goto freedata;
351 switch (state) {
352 case T_UNBND:
353 if (bindaddr) {
355 * Services that specify a bind address typically
356 * use a fixed service (IP port) so we need to set
357 * SO_REUSEADDR to prevent bind errors on restart.
359 if (bindaddr->addr.len != 0)
360 (void) __rpc_tli_set_options(fd, SOL_SOCKET,
361 SO_REUSEADDR, 1);
362 if (t_bind(fd, (struct t_bind *)bindaddr, tres) == -1) {
363 char errorstr[100];
365 __tli_sys_strerror(errorstr, sizeof (errorstr),
366 t_errno, errno);
367 (void) syslog(LOG_ERR,
368 "svc_tli_create: could not bind: %s",
369 errorstr);
370 goto freedata;
373 * Should compare the addresses only if addr.len
374 * was non-zero
376 if (bindaddr->addr.len &&
377 (memcmp(bindaddr->addr.buf, tres->addr.buf,
378 (int)tres->addr.len) != 0)) {
379 (void) syslog(LOG_ERR, "svc_tli_create: could "
380 "not bind to requested address: address "
381 "mismatch");
382 goto freedata;
384 } else {
385 if (rpc_control(__RPC_SVC_LSTNBKLOG_GET, &tres->qlen)
386 == FALSE) {
387 syslog(LOG_ERR,
388 "svc_tli_create: can't get listen backlog");
389 goto freedata;
391 tres->addr.len = 0;
392 if (t_bind(fd, tres, tres) == -1) {
393 char errorstr[100];
395 __tli_sys_strerror(errorstr, sizeof (errorstr),
396 t_errno, errno);
397 (void) syslog(LOG_ERR,
398 "svc_tli_create: could not bind: %s",
399 errorstr);
400 goto freedata;
405 * If requested, set SO_EXCLBIND on each binding.
407 * SO_EXCLBIND has the following properties
408 * - an fd bound to port P via IPv4 will prevent an IPv6
409 * bind to port P (and vice versa)
410 * - an fd bound to a wildcard IP address for port P will
411 * prevent a more specific IP address bind to port P
412 * (see {tcp,udp}.c for details)
414 * We use the latter property to prevent hijacking of RPC
415 * services that reside at non-privileged ports.
417 * When the bind address is not specified, each bind gets a
418 * new port number, and (for IP transports) we should set
419 * the exclusive flag after every IP bind. That's the
420 * strcmp nc_proto part of the expression below.
422 * When the bind address IS specified, we need to set the
423 * exclusive flag only after we've bound both IPv6+IPv4,
424 * or the IPv4 bind will fail. Setting the exclusive flag
425 * after the "tcp" or "udp" transport bind does that.
426 * That's the strcmp nc_netid part below.
428 if (nconf != NULL && ((bindaddr == NULL &&
429 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
430 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
431 (strcmp(nconf->nc_netid, "tcp") == 0 ||
432 strcmp(nconf->nc_netid, "udp") == 0))) {
433 bool_t exclbind = FALSE;
434 (void) rpc_control(__RPC_SVC_EXCLBIND_GET, &exclbind);
435 if (exclbind &&
436 __rpc_tli_set_options(fd, SOL_SOCKET,
437 SO_EXCLBIND, 1) < 0) {
438 syslog(LOG_ERR,
439 "svc_tli_create: can't set EXCLBIND [netid='%s']",
440 nconf->nc_netid);
441 goto freedata;
445 /* Enable options of returning the ip's for udp */
446 if (nconf) {
447 int ret = 0;
448 if (strcmp(nconf->nc_netid, "udp6") == 0) {
449 ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
450 IPV6_RECVPKTINFO, 1);
451 if (ret < 0) {
452 char errorstr[100];
454 __tli_sys_strerror(errorstr,
455 sizeof (errorstr), t_errno, errno);
456 (void) syslog(LOG_ERR,
457 "svc_tli_create: "
458 "IPV6_RECVPKTINFO(2): %s",
459 errorstr);
460 goto freedata;
462 } else if (strcmp(nconf->nc_netid, "udp") == 0) {
463 ret = __rpc_tli_set_options(fd, IPPROTO_IP,
464 IP_RECVDSTADDR, 1);
465 if (ret < 0) {
466 char errorstr[100];
468 __tli_sys_strerror(errorstr,
469 sizeof (errorstr), t_errno, errno);
470 (void) syslog(LOG_ERR,
471 "svc_tli_create: "
472 "IP_RECVDSTADDR(2): %s", errorstr);
473 goto freedata;
477 break;
479 case T_IDLE:
480 if (bindaddr) {
481 /* Copy the entire stuff in tres */
482 if (tres->addr.maxlen < bindaddr->addr.len) {
483 (void) syslog(LOG_ERR,
484 "svc_tli_create: illegal netbuf length");
485 goto freedata;
487 tres->addr.len = bindaddr->addr.len;
488 (void) memcpy(tres->addr.buf, bindaddr->addr.buf,
489 (int)tres->addr.len);
490 } else
491 if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
492 tres->addr.len = 0;
493 break;
494 case T_INREL:
495 (void) t_rcvrel(fd);
496 (void) t_sndrel(fd);
497 (void) syslog(LOG_ERR, "svc_tli_create: other side wants to "
498 "release connection");
499 goto freedata;
501 case T_INCON:
502 /* Do nothing here. Assume this is handled in rendezvous */
503 break;
504 case T_DATAXFER:
506 * This takes care of the case where a fd
507 * is passed on which a connection has already
508 * been accepted.
510 if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
511 tres->addr.len = 0;
512 break;
513 default:
514 (void) syslog(LOG_ERR,
515 "svc_tli_create: connection in a wierd state (%d)", state);
516 goto freedata;
520 * call transport specific function.
522 switch (tinfo.servtype) {
523 case T_COTS_ORD:
524 case T_COTS:
525 if (state == T_DATAXFER)
526 xprt = svc_fd_create_private(fd, sendsz,
527 recvsz);
528 else
529 xprt = svc_vc_create_private(fd, sendsz,
530 recvsz);
531 if (!nconf || !xprt)
532 break;
533 if ((tinfo.servtype == T_COTS_ORD) &&
534 (state != T_DATAXFER) &&
535 (strcmp(nconf->nc_protofmly, "inet") == 0))
536 (void) __svc_vc_setflag(xprt, TRUE);
537 break;
538 case T_CLTS:
539 xprt = svc_dg_create_private(fd, sendsz, recvsz);
540 break;
541 default:
542 (void) syslog(LOG_ERR,
543 "svc_tli_create: bad service type");
544 goto freedata;
546 if (xprt == NULL)
548 * The error messages here are spitted out by the lower layers:
549 * svc_vc_create(), svc_fd_create() and svc_dg_create().
551 goto freedata;
553 /* fill in the other xprt information */
555 /* Assign the local bind address */
556 xprt->xp_ltaddr = tres->addr;
557 /* Fill in type of service */
558 xprt->xp_type = tinfo.servtype;
559 tres->addr.buf = NULL;
560 (void) t_free((char *)tres, T_BIND);
561 tres = NULL;
563 xprt->xp_rtaddr.len = 0;
564 xprt->xp_rtaddr.maxlen = __rpc_get_a_size(tinfo.addr);
566 /* Allocate space for the remote bind info */
567 if ((xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen)) == NULL) {
568 (void) syslog(LOG_ERR, "svc_tli_create: No memory!");
569 goto freedata;
572 if (nconf) {
573 xprt->xp_netid = strdup(nconf->nc_netid);
574 if (xprt->xp_netid == NULL) {
575 free(xprt->xp_rtaddr.buf);
576 syslog(LOG_ERR, "svc_tli_create: strdup failed!");
577 goto freedata;
579 xprt->xp_tp = strdup(nconf->nc_device);
580 if (xprt->xp_tp == NULL) {
581 free(xprt->xp_rtaddr.buf);
582 free(xprt->xp_netid);
583 syslog(LOG_ERR, "svc_tli_create: strdup failed!");
584 goto freedata;
589 * if (madefd && (tinfo.servtype == T_CLTS))
590 * (void) ioctl(fd, I_POP, NULL);
592 xprt_register(xprt);
593 return (xprt);
595 freedata:
596 if (madefd)
597 (void) t_close(fd);
598 if (tres)
599 (void) t_free((char *)tres, T_BIND);
600 if (xprt) {
601 if (!madefd) /* so that svc_destroy doesnt close fd */
602 xprt->xp_fd = RPC_ANYFD;
603 SVC_DESTROY(xprt);
605 return (NULL);