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