xref: /titanic_44/usr/src/lib/libnsl/rpc/svc_generic.c (revision 587032cf0967234b39ccb50adca936a367841063)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T */
28 /*	All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * svc_generic.c, Server side for RPC.
34  *
35  */
36 
37 #include "mt.h"
38 #include <stdlib.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <netinet/udp.h>
43 #include <inttypes.h>
44 #include "rpc_mt.h"
45 #include <stdio.h>
46 #include <rpc/rpc.h>
47 #include <sys/types.h>
48 #include <errno.h>
49 #include <syslog.h>
50 #include <rpc/nettype.h>
51 #include <malloc.h>
52 #include <string.h>
53 #include <stropts.h>
54 #include <tsol/label.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfs_acl.h>
57 #include <rpcsvc/mount.h>
58 #include <rpcsvc/nsm_addr.h>
59 #include <rpcsvc/rquota.h>
60 #include <rpcsvc/sm_inter.h>
61 #include <rpcsvc/nlm_prot.h>
62 
63 extern int __svc_vc_setflag(SVCXPRT *, int);
64 
65 extern SVCXPRT *svc_dg_create_private(int, uint_t, uint_t);
66 extern SVCXPRT *svc_vc_create_private(int, uint_t, uint_t);
67 extern SVCXPRT *svc_fd_create_private(int, uint_t, uint_t);
68 
69 extern bool_t __svc_add_to_xlist(SVCXPRT_LIST **, SVCXPRT *, mutex_t *);
70 extern void __svc_free_xlist(SVCXPRT_LIST **, mutex_t *);
71 
72 extern bool_t __rpc_try_doors(const char *, bool_t *);
73 
74 /*
75  * The highest level interface for server creation.
76  * It tries for all the nettokens in that particular class of token
77  * and returns the number of handles it can create and/or find.
78  *
79  * It creates a link list of all the handles it could create.
80  * If svc_create() is called multiple times, it uses the handle
81  * created earlier instead of creating a new handle every time.
82  */
83 
84 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
85 
86 SVCXPRT_LIST *_svc_xprtlist = NULL;
87 extern mutex_t xprtlist_lock;
88 
89 static SVCXPRT * svc_tli_create_common(int, const struct netconfig *,
90     const struct t_bind *, uint_t, uint_t, boolean_t);
91 
92 boolean_t
93 is_multilevel(rpcprog_t prognum)
94 {
95 	/* This is a list of identified multilevel service provider */
96 	if ((prognum == MOUNTPROG) || (prognum == NFS_PROGRAM) ||
97 	    (prognum == NFS_ACL_PROGRAM) || (prognum == NLM_PROG) ||
98 	    (prognum == NSM_ADDR_PROGRAM) || (prognum == RQUOTAPROG) ||
99 	    (prognum == SM_PROG))
100 		return (B_TRUE);
101 
102 	return (B_FALSE);
103 }
104 
105 void
106 __svc_free_xprtlist(void)
107 {
108 	__svc_free_xlist(&_svc_xprtlist, &xprtlist_lock);
109 }
110 
111 int
112 svc_create(void (*dispatch)(), const rpcprog_t prognum, const rpcvers_t versnum,
113 							const char *nettype)
114 {
115 	SVCXPRT_LIST *l;
116 	int num = 0;
117 	SVCXPRT *xprt;
118 	struct netconfig *nconf;
119 	void *handle;
120 	bool_t try_others;
121 
122 	/*
123 	 * Check if service should register over doors transport.
124 	 */
125 	if (__rpc_try_doors(nettype, &try_others)) {
126 		if (svc_door_create(dispatch, prognum, versnum, 0) == NULL)
127 			(void) syslog(LOG_ERR,
128 				"svc_create: could not register over doors");
129 		else
130 			num++;
131 	}
132 	if (!try_others)
133 		return (num);
134 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
135 		(void) syslog(LOG_ERR, "svc_create: unknown protocol");
136 		return (0);
137 	}
138 	while (nconf = __rpc_getconf(handle)) {
139 		(void) mutex_lock(&xprtlist_lock);
140 		for (l = _svc_xprtlist; l; l = l->next) {
141 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
142 				/* Found an old one, use it */
143 				(void) rpcb_unset(prognum, versnum, nconf);
144 				if (svc_reg(l->xprt, prognum, versnum,
145 					dispatch, nconf) == FALSE)
146 					(void) syslog(LOG_ERR,
147 		"svc_create: could not register prog %d vers %d on %s",
148 					prognum, versnum, nconf->nc_netid);
149 				else
150 					num++;
151 				break;
152 			}
153 		}
154 		(void) mutex_unlock(&xprtlist_lock);
155 		if (l == NULL) {
156 			/* It was not found. Now create a new one */
157 			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
158 			if (xprt) {
159 				if (!__svc_add_to_xlist(&_svc_xprtlist, xprt,
160 							&xprtlist_lock)) {
161 					(void) syslog(LOG_ERR,
162 						"svc_create: no memory");
163 					return (0);
164 				}
165 				num++;
166 			}
167 		}
168 	}
169 	__rpc_endconf(handle);
170 	/*
171 	 * In case of num == 0; the error messages are generated by the
172 	 * underlying layers; and hence not needed here.
173 	 */
174 	return (num);
175 }
176 
177 /*
178  * The high level interface to svc_tli_create().
179  * It tries to create a server for "nconf" and registers the service
180  * with the rpcbind. It calls svc_tli_create();
181  */
182 SVCXPRT *
183 svc_tp_create(void (*dispatch)(), const rpcprog_t prognum,
184 			const rpcvers_t versnum, const struct netconfig *nconf)
185 {
186 	SVCXPRT *xprt;
187 	boolean_t anon_mlp = B_FALSE;
188 
189 	if (nconf == NULL) {
190 		(void) syslog(LOG_ERR,
191 	"svc_tp_create: invalid netconfig structure for prog %d vers %d",
192 				prognum, versnum);
193 		return (NULL);
194 	}
195 
196 	/* Some programs need to allocate MLP for multilevel services */
197 	if (is_system_labeled() && is_multilevel(prognum))
198 		anon_mlp = B_TRUE;
199 	xprt = svc_tli_create_common(RPC_ANYFD, nconf, NULL, 0, 0, anon_mlp);
200 	if (xprt == NULL)
201 		return (NULL);
202 	(void) rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
203 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
204 		(void) syslog(LOG_ERR,
205 		"svc_tp_create: Could not register prog %d vers %d on %s",
206 				prognum, versnum, nconf->nc_netid);
207 		SVC_DESTROY(xprt);
208 		return (NULL);
209 	}
210 	return (xprt);
211 }
212 
213 SVCXPRT *
214 svc_tli_create(const int fd, const struct netconfig *nconf,
215     const struct t_bind *bindaddr, const uint_t sendsz, const uint_t recvsz)
216 {
217 	return (svc_tli_create_common(fd, nconf, bindaddr, sendsz, recvsz, 0));
218 }
219 
220 /*
221  * If fd is RPC_ANYFD, then it opens a fd for the given transport
222  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
223  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
224  * NULL bindadr and Connection oriented transports, the value of qlen
225  * is set arbitrarily.
226  *
227  * If sendsz or recvsz are zero, their default values are chosen.
228  */
229 SVCXPRT *
230 svc_tli_create_common(const int ofd, const struct netconfig *nconf,
231 	const struct t_bind *bindaddr, const uint_t sendsz,
232 	const uint_t recvsz, boolean_t mlp_flag)
233 {
234 	SVCXPRT *xprt = NULL;		/* service handle */
235 	struct t_info tinfo;		/* transport info */
236 	struct t_bind *tres = NULL;	/* bind info */
237 	bool_t madefd = FALSE;		/* whether fd opened here  */
238 	int state;			/* state of the transport provider */
239 	int fd = ofd;
240 
241 	if (fd == RPC_ANYFD) {
242 		if (nconf == NULL) {
243 			(void) syslog(LOG_ERR,
244 			    "svc_tli_create: invalid netconfig");
245 			return (NULL);
246 		}
247 		fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
248 		if (fd == -1) {
249 			char errorstr[100];
250 
251 			__tli_sys_strerror(errorstr, sizeof (errorstr),
252 					t_errno, errno);
253 			(void) syslog(LOG_ERR,
254 			"svc_tli_create: could not open connection for %s: %s",
255 					nconf->nc_netid, errorstr);
256 			return (NULL);
257 		}
258 		madefd = TRUE;
259 		state = T_UNBND;
260 	} else {
261 		/*
262 		 * It is an open descriptor. Sync it & get the transport info.
263 		 */
264 		if ((state = t_sync(fd)) == -1) {
265 			char errorstr[100];
266 
267 			__tli_sys_strerror(errorstr, sizeof (errorstr),
268 					t_errno, errno);
269 			(void) syslog(LOG_ERR,
270 		"svc_tli_create: could not do t_sync: %s",
271 					errorstr);
272 			return (NULL);
273 		}
274 		if (t_getinfo(fd, &tinfo) == -1) {
275 			char errorstr[100];
276 
277 			__tli_sys_strerror(errorstr, sizeof (errorstr),
278 					t_errno, errno);
279 			(void) syslog(LOG_ERR,
280 		"svc_tli_create: could not get transport information: %s",
281 				    errorstr);
282 			return (NULL);
283 		}
284 		/* Enable options of returning the ip's for udp */
285 		if (nconf) {
286 		    int ret = 0;
287 		    if (strcmp(nconf->nc_netid, "udp6") == 0) {
288 			ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
289 					IPV6_RECVPKTINFO, 1);
290 			if (ret < 0) {
291 			    char errorstr[100];
292 
293 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
294 					t_errno, errno);
295 			    (void) syslog(LOG_ERR,
296 				"svc_tli_create: IPV6_RECVPKTINFO(1): %s",
297 					errorstr);
298 			    return (NULL);
299 			}
300 		    } else if (strcmp(nconf->nc_netid, "udp") == 0) {
301 			ret = __rpc_tli_set_options(fd, IPPROTO_IP,
302 					IP_RECVDSTADDR, 1);
303 			if (ret < 0) {
304 			    char errorstr[100];
305 
306 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
307 					t_errno, errno);
308 			    (void) syslog(LOG_ERR,
309 				"svc_tli_create: IP_RECVDSTADDR(1): %s",
310 					errorstr);
311 			    return (NULL);
312 			}
313 		    }
314 		}
315 	}
316 
317 	/*
318 	 * If the fd is unbound, try to bind it.
319 	 * In any case, try to get its bound info in tres
320 	 */
321 /* LINTED pointer alignment */
322 	tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
323 	if (tres == NULL) {
324 		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
325 		goto freedata;
326 	}
327 
328 	switch (state) {
329 		bool_t tcp, exclbind;
330 	case T_UNBND:
331 		/* If this is a labeled system, then ask for an MLP */
332 		if (is_system_labeled() &&
333 		    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
334 		    strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
335 			(void) __rpc_tli_set_options(fd, SOL_SOCKET,
336 			    SO_RECVUCRED, 1);
337 			if (mlp_flag)
338 				(void) __rpc_tli_set_options(fd, SOL_SOCKET,
339 				    SO_ANON_MLP, 1);
340 		}
341 
342 		/*
343 		 * {TCP,UDP}_EXCLBIND has the following properties
344 		 *    - an fd bound to port P via IPv4 will prevent an IPv6
345 		 *    bind to port P (and vice versa)
346 		 *    - an fd bound to a wildcard IP address for port P will
347 		 *    prevent a more specific IP address bind to port P
348 		 *    (see {tcp,udp}.c for details)
349 		 *
350 		 * We use the latter property to prevent hijacking of RPC
351 		 * services that reside at non-privileged ports.
352 		 */
353 		tcp = nconf ? (strcmp(nconf->nc_proto, NC_TCP) == 0) : 0;
354 		if (nconf &&
355 		    (tcp || (strcmp(nconf->nc_proto, NC_UDP) == 0)) &&
356 		    rpc_control(__RPC_SVC_EXCLBIND_GET, &exclbind)) {
357 			if (exclbind) {
358 				if (__rpc_tli_set_options(fd,
359 					tcp ? IPPROTO_TCP : IPPROTO_UDP,
360 					tcp ? TCP_EXCLBIND : UDP_EXCLBIND,
361 							1) < 0) {
362 					syslog(LOG_ERR,
363 			    "svc_tli_create: can't set EXCLBIND [netid='%s']",
364 					    nconf->nc_netid);
365 					goto freedata;
366 				}
367 			}
368 		}
369 		if (bindaddr) {
370 			if (t_bind(fd, (struct t_bind *)bindaddr,
371 								tres) == -1) {
372 				char errorstr[100];
373 
374 				__tli_sys_strerror(errorstr, sizeof (errorstr),
375 						t_errno, errno);
376 				(void) syslog(LOG_ERR,
377 					"svc_tli_create: could not bind: %s",
378 					    errorstr);
379 				goto freedata;
380 			}
381 			/*
382 			 * Should compare the addresses only if addr.len
383 			 * was non-zero
384 			 */
385 			if (bindaddr->addr.len &&
386 				(memcmp(bindaddr->addr.buf, tres->addr.buf,
387 					(int)tres->addr.len) != 0)) {
388 				(void) syslog(LOG_ERR,
389 		"svc_tli_create: could not bind to requested address: %s",
390 						"address mismatch");
391 				goto freedata;
392 			}
393 		} else {
394 			tres->qlen = 64; /* Chosen Arbitrarily */
395 			tres->addr.len = 0;
396 			if (t_bind(fd, tres, tres) == -1) {
397 				char errorstr[100];
398 
399 				__tli_sys_strerror(errorstr, sizeof (errorstr),
400 						t_errno, errno);
401 				(void) syslog(LOG_ERR,
402 					"svc_tli_create: could not bind: %s",
403 						errorstr);
404 				goto freedata;
405 			}
406 		}
407 
408 		/* Enable options of returning the ip's for udp */
409 		if (nconf) {
410 		    int ret = 0;
411 		    if (strcmp(nconf->nc_netid, "udp6") == 0) {
412 			ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
413 					IPV6_RECVPKTINFO, 1);
414 			if (ret < 0) {
415 			    char errorstr[100];
416 
417 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
418 					t_errno, errno);
419 			    (void) syslog(LOG_ERR,
420 				"svc_tli_create: IPV6_RECVPKTINFO(2): %s",
421 					errorstr);
422 			    goto freedata;
423 			}
424 		    } else if (strcmp(nconf->nc_netid, "udp") == 0) {
425 			ret = __rpc_tli_set_options(fd, IPPROTO_IP,
426 					IP_RECVDSTADDR, 1);
427 			if (ret < 0) {
428 			    char errorstr[100];
429 
430 			    __tli_sys_strerror(errorstr, sizeof (errorstr),
431 					t_errno, errno);
432 			    (void) syslog(LOG_ERR,
433 				"svc_tli_create: IP_RECVDSTADDR(2): %s",
434 					errorstr);
435 			    goto freedata;
436 			}
437 		    }
438 		}
439 		break;
440 
441 	case T_IDLE:
442 		if (bindaddr) {
443 			/* Copy the entire stuff in tres */
444 			if (tres->addr.maxlen < bindaddr->addr.len) {
445 				(void) syslog(LOG_ERR,
446 				"svc_tli_create: illegal netbuf length");
447 				goto freedata;
448 			}
449 			tres->addr.len = bindaddr->addr.len;
450 			(void) memcpy(tres->addr.buf, bindaddr->addr.buf,
451 					(int)tres->addr.len);
452 		} else
453 			if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
454 				tres->addr.len = 0;
455 		break;
456 	case T_INREL:
457 		(void) t_rcvrel(fd);
458 		(void) t_sndrel(fd);
459 		(void) syslog(LOG_ERR,
460 			"svc_tli_create: other side wants to\
461 release connection");
462 		goto freedata;
463 
464 	case T_INCON:
465 		/* Do nothing here. Assume this is handled in rendezvous */
466 		break;
467 	case T_DATAXFER:
468 		/*
469 		 * This takes care of the case where a fd
470 		 * is passed on which a connection has already
471 		 * been accepted.
472 		 */
473 		if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
474 			tres->addr.len = 0;
475 		break;
476 	default:
477 		(void) syslog(LOG_ERR,
478 		    "svc_tli_create: connection in a wierd state (%d)", state);
479 		goto freedata;
480 	}
481 
482 	/*
483 	 * call transport specific function.
484 	 */
485 	switch (tinfo.servtype) {
486 		case T_COTS_ORD:
487 		case T_COTS:
488 			if (state == T_DATAXFER)
489 				xprt = svc_fd_create_private(fd, sendsz,
490 						recvsz);
491 			else
492 				xprt = svc_vc_create_private(fd, sendsz,
493 						recvsz);
494 			if (!nconf || !xprt)
495 				break;
496 			if ((tinfo.servtype == T_COTS_ORD) &&
497 				(state != T_DATAXFER) &&
498 				(strcmp(nconf->nc_protofmly, "inet") == 0))
499 				(void) __svc_vc_setflag(xprt, TRUE);
500 			break;
501 		case T_CLTS:
502 			xprt = svc_dg_create_private(fd, sendsz, recvsz);
503 			break;
504 		default:
505 			(void) syslog(LOG_ERR,
506 			    "svc_tli_create: bad service type");
507 			goto freedata;
508 	}
509 	if (xprt == NULL)
510 		/*
511 		 * The error messages here are spitted out by the lower layers:
512 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
513 		 */
514 		goto freedata;
515 
516 	/* fill in the other xprt information */
517 
518 	/* Assign the local bind address */
519 	xprt->xp_ltaddr = tres->addr;
520 	/* Fill in type of service */
521 	xprt->xp_type = tinfo.servtype;
522 	tres->addr.buf = NULL;
523 	(void) t_free((char *)tres, T_BIND);
524 	tres = NULL;
525 
526 	xprt->xp_rtaddr.len = 0;
527 	xprt->xp_rtaddr.maxlen = __rpc_get_a_size(tinfo.addr);
528 
529 	/* Allocate space for the remote bind info */
530 	if ((xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen)) == NULL) {
531 		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
532 		goto freedata;
533 	}
534 
535 	if (nconf) {
536 		xprt->xp_netid = strdup(nconf->nc_netid);
537 		if (xprt->xp_netid == NULL) {
538 			if (xprt->xp_rtaddr.buf)
539 				free(xprt->xp_rtaddr.buf);
540 			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
541 			goto freedata;
542 		}
543 		xprt->xp_tp = strdup(nconf->nc_device);
544 		if (xprt->xp_tp == NULL) {
545 			if (xprt->xp_rtaddr.buf)
546 				free(xprt->xp_rtaddr.buf);
547 			if (xprt->xp_netid)
548 				free(xprt->xp_netid);
549 			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
550 			goto freedata;
551 		}
552 	}
553 
554 /*
555  *	if (madefd && (tinfo.servtype == T_CLTS))
556  *		(void) ioctl(fd, I_POP, NULL);
557  */
558 	xprt_register(xprt);
559 	return (xprt);
560 
561 freedata:
562 	if (madefd)
563 		(void) t_close(fd);
564 	if (tres)
565 		(void) t_free((char *)tres, T_BIND);
566 	if (xprt) {
567 		if (!madefd) /* so that svc_destroy doesnt close fd */
568 			xprt->xp_fd = RPC_ANYFD;
569 		SVC_DESTROY(xprt);
570 	}
571 	return (NULL);
572 }
573