summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/rpc/svc_generic.c
blob: ca9322b45d2a4b5414181991a8fd090f0edb2cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
 */

/*	Copyright (c) 1988 AT&T */
/*	All Rights Reserved   */

/*
 * svc_generic.c, Server side for RPC.
 *
 */

#include "mt.h"
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <inttypes.h>
#include "rpc_mt.h"
#include <stdio.h>
#include <rpc/rpc.h>
#include <sys/types.h>
#include <errno.h>
#include <syslog.h>
#include <rpc/nettype.h>
#include <malloc.h>
#include <string.h>
#include <stropts.h>
#include <tsol/label.h>
#include <nfs/nfs.h>
#include <nfs/nfs_acl.h>
#include <rpcsvc/mount.h>
#include <rpcsvc/nsm_addr.h>
#include <rpcsvc/rquota.h>
#include <rpcsvc/sm_inter.h>
#include <rpcsvc/nlm_prot.h>

extern int __svc_vc_setflag(SVCXPRT *, int);

extern SVCXPRT *svc_dg_create_private(int, uint_t, uint_t);
extern SVCXPRT *svc_vc_create_private(int, uint_t, uint_t);
extern SVCXPRT *svc_fd_create_private(int, uint_t, uint_t);

extern bool_t __svc_add_to_xlist(SVCXPRT_LIST **, SVCXPRT *, mutex_t *);
extern void __svc_free_xlist(SVCXPRT_LIST **, mutex_t *);

extern bool_t __rpc_try_doors(const char *, bool_t *);

/*
 * The highest level interface for server creation.
 * It tries for all the nettokens in that particular class of token
 * and returns the number of handles it can create and/or find.
 *
 * It creates a link list of all the handles it could create.
 * If svc_create() is called multiple times, it uses the handle
 * created earlier instead of creating a new handle every time.
 */

/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */

SVCXPRT_LIST *_svc_xprtlist = NULL;
extern mutex_t xprtlist_lock;

static SVCXPRT * svc_tli_create_common(int, const struct netconfig *,
    const struct t_bind *, uint_t, uint_t, boolean_t);

boolean_t
is_multilevel(rpcprog_t prognum)
{
	/* This is a list of identified multilevel service provider */
	if ((prognum == MOUNTPROG) || (prognum == NFS_PROGRAM) ||
	    (prognum == NFS_ACL_PROGRAM) || (prognum == NLM_PROG) ||
	    (prognum == NSM_ADDR_PROGRAM) || (prognum == RQUOTAPROG) ||
	    (prognum == SM_PROG))
		return (B_TRUE);

	return (B_FALSE);
}

void
__svc_free_xprtlist(void)
{
	__svc_free_xlist(&_svc_xprtlist, &xprtlist_lock);
}

int
svc_create(void (*dispatch)(), const rpcprog_t prognum, const rpcvers_t versnum,
							const char *nettype)
{
	SVCXPRT_LIST *l;
	int num = 0;
	SVCXPRT *xprt;
	struct netconfig *nconf;
	void *handle;
	bool_t try_others;

	/*
	 * Check if service should register over doors transport.
	 */
	if (__rpc_try_doors(nettype, &try_others)) {
		if (svc_door_create(dispatch, prognum, versnum, 0) == NULL)
			(void) syslog(LOG_ERR,
			    "svc_create: could not register over doors");
		else
			num++;
	}
	if (!try_others)
		return (num);
	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
		(void) syslog(LOG_ERR, "svc_create: unknown protocol");
		return (0);
	}
	while (nconf = __rpc_getconf(handle)) {
		(void) mutex_lock(&xprtlist_lock);
		for (l = _svc_xprtlist; l; l = l->next) {
			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
				/* Found an old one, use it */
				(void) rpcb_unset(prognum, versnum, nconf);
				if (svc_reg(l->xprt, prognum, versnum,
				    dispatch, nconf) == FALSE)
					(void) syslog(LOG_ERR, "svc_create: "
					    "could not register prog %d vers "
					    "%d on %s",
					    prognum, versnum, nconf->nc_netid);
				else
					num++;
				break;
			}
		}
		(void) mutex_unlock(&xprtlist_lock);
		if (l == NULL) {
			/* It was not found. Now create a new one */
			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
			if (xprt) {
				if (!__svc_add_to_xlist(&_svc_xprtlist, xprt,
				    &xprtlist_lock)) {
					(void) syslog(LOG_ERR,
					    "svc_create: no memory");
					return (0);
				}
				num++;
			}
		}
	}
	__rpc_endconf(handle);
	/*
	 * In case of num == 0; the error messages are generated by the
	 * underlying layers; and hence not needed here.
	 */
	return (num);
}

/*
 * The high level interface to svc_tli_create().
 * It tries to create a server for "nconf" and registers the service
 * with the rpcbind. It calls svc_tli_create();
 */
SVCXPRT *
svc_tp_create(void (*dispatch)(), const rpcprog_t prognum,
			const rpcvers_t versnum, const struct netconfig *nconf)
{
	SVCXPRT *xprt;
	boolean_t anon_mlp = B_FALSE;

	if (nconf == NULL) {
		(void) syslog(LOG_ERR, "svc_tp_create: invalid netconfig "
		    "structure for prog %d vers %d", prognum, versnum);
		return (NULL);
	}

	/* Some programs need to allocate MLP for multilevel services */
	if (is_system_labeled() && is_multilevel(prognum))
		anon_mlp = B_TRUE;
	xprt = svc_tli_create_common(RPC_ANYFD, nconf, NULL, 0, 0, anon_mlp);
	if (xprt == NULL)
		return (NULL);

	(void) rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
		(void) syslog(LOG_ERR,
		    "svc_tp_create: Could not register prog %d vers %d on %s",
		    prognum, versnum, nconf->nc_netid);
		SVC_DESTROY(xprt);
		return (NULL);
	}
	return (xprt);
}

SVCXPRT *
svc_tli_create(const int fd, const struct netconfig *nconf,
    const struct t_bind *bindaddr, const uint_t sendsz, const uint_t recvsz)
{
	return (svc_tli_create_common(fd, nconf, bindaddr, sendsz, recvsz, 0));
}

/*
 * If fd is RPC_ANYFD, then it opens a fd for the given transport
 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
 * NULL bindadr and Connection oriented transports, the value of qlen
 * is set arbitrarily.
 *
 * If sendsz or recvsz are zero, their default values are chosen.
 */
SVCXPRT *
svc_tli_create_common(const int ofd, const struct netconfig *nconf,
	const struct t_bind *bindaddr, const uint_t sendsz,
	const uint_t recvsz, boolean_t mlp_flag)
{
	SVCXPRT *xprt = NULL;		/* service handle */
	struct t_info tinfo;		/* transport info */
	struct t_bind *tres = NULL;	/* bind info */
	bool_t madefd = FALSE;		/* whether fd opened here  */
	int state;			/* state of the transport provider */
	int fd = ofd;

	if (fd == RPC_ANYFD) {
		if (nconf == NULL) {
			(void) syslog(LOG_ERR,
			    "svc_tli_create: invalid netconfig");
			return (NULL);
		}
		fd = t_open(nconf->nc_device, O_RDWR, &tinfo);
		if (fd == -1) {
			char errorstr[100];

			__tli_sys_strerror(errorstr, sizeof (errorstr),
			    t_errno, errno);
			(void) syslog(LOG_ERR, "svc_tli_create: could not open "
			    "connection for %s: %s", nconf->nc_netid, errorstr);
			return (NULL);
		}
		madefd = TRUE;
		state = T_UNBND;
	} else {
		/*
		 * It is an open descriptor. Sync it & get the transport info.
		 */
		if ((state = t_sync(fd)) == -1) {
			char errorstr[100];

			__tli_sys_strerror(errorstr, sizeof (errorstr),
			    t_errno, errno);
			(void) syslog(LOG_ERR,
			    "svc_tli_create: could not do t_sync: %s",
			    errorstr);
			return (NULL);
		}
		if (t_getinfo(fd, &tinfo) == -1) {
			char errorstr[100];

			__tli_sys_strerror(errorstr, sizeof (errorstr),
			    t_errno, errno);
			(void) syslog(LOG_ERR, "svc_tli_create: could not get "
			    "transport information: %s", errorstr);
			return (NULL);
		}
		/* Enable options of returning the ip's for udp */
		if (nconf) {
			int ret = 0;
			if (strcmp(nconf->nc_netid, "udp6") == 0) {
				ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
				    IPV6_RECVPKTINFO, 1);
				if (ret < 0) {
					char errorstr[100];

					__tli_sys_strerror(errorstr,
					    sizeof (errorstr), t_errno, errno);
					(void) syslog(LOG_ERR,
					    "svc_tli_create: "
					    "IPV6_RECVPKTINFO(1): %s",
					    errorstr);
					return (NULL);
				}
			} else if (strcmp(nconf->nc_netid, "udp") == 0) {
				ret = __rpc_tli_set_options(fd, IPPROTO_IP,
				    IP_RECVDSTADDR, 1);
				if (ret < 0) {
					char errorstr[100];

					__tli_sys_strerror(errorstr,
					    sizeof (errorstr), t_errno, errno);
					(void) syslog(LOG_ERR,
					    "svc_tli_create: "
					    "IP_RECVDSTADDR(1): %s", errorstr);
					return (NULL);
				}
			}
		}
	}

	/*
	 * If the fd is unbound, try to bind it.
	 * In any case, try to get its bound info in tres
	 */
/* LINTED pointer alignment */
	tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
	if (tres == NULL) {
		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
		goto freedata;
	}

	switch (state) {
		bool_t tcp, exclbind;
	case T_UNBND:
		/* If this is a labeled system, then ask for an MLP */
		if (is_system_labeled() &&
		    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
		    strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
			(void) __rpc_tli_set_options(fd, SOL_SOCKET,
			    SO_RECVUCRED, 1);
			if (mlp_flag)
				(void) __rpc_tli_set_options(fd, SOL_SOCKET,
				    SO_ANON_MLP, 1);
		}

		/*
		 * SO_EXCLBIND has the following properties
		 *    - an fd bound to port P via IPv4 will prevent an IPv6
		 *    bind to port P (and vice versa)
		 *    - an fd bound to a wildcard IP address for port P will
		 *    prevent a more specific IP address bind to port P
		 *    (see {tcp,udp}.c for details)
		 *
		 * We use the latter property to prevent hijacking of RPC
		 * services that reside at non-privileged ports.
		 */
		tcp = nconf ? (strcmp(nconf->nc_proto, NC_TCP) == 0) : 0;
		if (nconf &&
		    (tcp || (strcmp(nconf->nc_proto, NC_UDP) == 0)) &&
		    rpc_control(__RPC_SVC_EXCLBIND_GET, &exclbind)) {
			if (exclbind) {
				if (__rpc_tli_set_options(fd, SOL_SOCKET,
				    SO_EXCLBIND, 1) < 0) {
					syslog(LOG_ERR,
			    "svc_tli_create: can't set EXCLBIND [netid='%s']",
					    nconf->nc_netid);
					goto freedata;
				}
			}
		}
		if (bindaddr) {
			if (t_bind(fd, (struct t_bind *)bindaddr, tres) == -1) {
				char errorstr[100];

				__tli_sys_strerror(errorstr, sizeof (errorstr),
				    t_errno, errno);
				(void) syslog(LOG_ERR,
				    "svc_tli_create: could not bind: %s",
				    errorstr);
				goto freedata;
			}
			/*
			 * Should compare the addresses only if addr.len
			 * was non-zero
			 */
			if (bindaddr->addr.len &&
			    (memcmp(bindaddr->addr.buf, tres->addr.buf,
			    (int)tres->addr.len) != 0)) {
				(void) syslog(LOG_ERR, "svc_tli_create: could "
				    "not bind to requested address: address "
				    "mismatch");
				goto freedata;
			}
		} else {
			if (rpc_control(__RPC_SVC_LSTNBKLOG_GET, &tres->qlen)
			    == FALSE) {
				syslog(LOG_ERR,
				    "svc_tli_create: can't get listen backlog");
				goto freedata;
			}
			tres->addr.len = 0;
			if (t_bind(fd, tres, tres) == -1) {
				char errorstr[100];

				__tli_sys_strerror(errorstr, sizeof (errorstr),
				    t_errno, errno);
				(void) syslog(LOG_ERR,
				    "svc_tli_create: could not bind: %s",
				    errorstr);
				goto freedata;
			}
		}

		/* Enable options of returning the ip's for udp */
		if (nconf) {
			int ret = 0;
			if (strcmp(nconf->nc_netid, "udp6") == 0) {
				ret = __rpc_tli_set_options(fd, IPPROTO_IPV6,
				    IPV6_RECVPKTINFO, 1);
				if (ret < 0) {
					char errorstr[100];

					__tli_sys_strerror(errorstr,
					    sizeof (errorstr), t_errno, errno);
					(void) syslog(LOG_ERR,
					    "svc_tli_create: "
					    "IPV6_RECVPKTINFO(2): %s",
					    errorstr);
					goto freedata;
				}
			} else if (strcmp(nconf->nc_netid, "udp") == 0) {
				ret = __rpc_tli_set_options(fd, IPPROTO_IP,
				    IP_RECVDSTADDR, 1);
				if (ret < 0) {
					char errorstr[100];

					__tli_sys_strerror(errorstr,
					    sizeof (errorstr), t_errno, errno);
					(void) syslog(LOG_ERR,
					    "svc_tli_create: "
					    "IP_RECVDSTADDR(2): %s", errorstr);
					goto freedata;
				}
			}
		}
		break;

	case T_IDLE:
		if (bindaddr) {
			/* Copy the entire stuff in tres */
			if (tres->addr.maxlen < bindaddr->addr.len) {
				(void) syslog(LOG_ERR,
				"svc_tli_create: illegal netbuf length");
				goto freedata;
			}
			tres->addr.len = bindaddr->addr.len;
			(void) memcpy(tres->addr.buf, bindaddr->addr.buf,
			    (int)tres->addr.len);
		} else
			if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
				tres->addr.len = 0;
		break;
	case T_INREL:
		(void) t_rcvrel(fd);
		(void) t_sndrel(fd);
		(void) syslog(LOG_ERR, "svc_tli_create: other side wants to "
		    "release connection");
		goto freedata;

	case T_INCON:
		/* Do nothing here. Assume this is handled in rendezvous */
		break;
	case T_DATAXFER:
		/*
		 * This takes care of the case where a fd
		 * is passed on which a connection has already
		 * been accepted.
		 */
		if (t_getname(fd, &(tres->addr), LOCALNAME) == -1)
			tres->addr.len = 0;
		break;
	default:
		(void) syslog(LOG_ERR,
		    "svc_tli_create: connection in a wierd state (%d)", state);
		goto freedata;
	}

	/*
	 * call transport specific function.
	 */
	switch (tinfo.servtype) {
		case T_COTS_ORD:
		case T_COTS:
			if (state == T_DATAXFER)
				xprt = svc_fd_create_private(fd, sendsz,
				    recvsz);
			else
				xprt = svc_vc_create_private(fd, sendsz,
				    recvsz);
			if (!nconf || !xprt)
				break;
			if ((tinfo.servtype == T_COTS_ORD) &&
			    (state != T_DATAXFER) &&
			    (strcmp(nconf->nc_protofmly, "inet") == 0))
				(void) __svc_vc_setflag(xprt, TRUE);
			break;
		case T_CLTS:
			xprt = svc_dg_create_private(fd, sendsz, recvsz);
			break;
		default:
			(void) syslog(LOG_ERR,
			    "svc_tli_create: bad service type");
			goto freedata;
	}
	if (xprt == NULL)
		/*
		 * The error messages here are spitted out by the lower layers:
		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
		 */
		goto freedata;

	/* fill in the other xprt information */

	/* Assign the local bind address */
	xprt->xp_ltaddr = tres->addr;
	/* Fill in type of service */
	xprt->xp_type = tinfo.servtype;
	tres->addr.buf = NULL;
	(void) t_free((char *)tres, T_BIND);
	tres = NULL;

	xprt->xp_rtaddr.len = 0;
	xprt->xp_rtaddr.maxlen = __rpc_get_a_size(tinfo.addr);

	/* Allocate space for the remote bind info */
	if ((xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen)) == NULL) {
		(void) syslog(LOG_ERR, "svc_tli_create: No memory!");
		goto freedata;
	}

	if (nconf) {
		xprt->xp_netid = strdup(nconf->nc_netid);
		if (xprt->xp_netid == NULL) {
			if (xprt->xp_rtaddr.buf)
				free(xprt->xp_rtaddr.buf);
			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
			goto freedata;
		}
		xprt->xp_tp = strdup(nconf->nc_device);
		if (xprt->xp_tp == NULL) {
			if (xprt->xp_rtaddr.buf)
				free(xprt->xp_rtaddr.buf);
			if (xprt->xp_netid)
				free(xprt->xp_netid);
			syslog(LOG_ERR, "svc_tli_create: strdup failed!");
			goto freedata;
		}
	}

/*
 *	if (madefd && (tinfo.servtype == T_CLTS))
 *		(void) ioctl(fd, I_POP, NULL);
 */
	xprt_register(xprt);
	return (xprt);

freedata:
	if (madefd)
		(void) t_close(fd);
	if (tres)
		(void) t_free((char *)tres, T_BIND);
	if (xprt) {
		if (!madefd) /* so that svc_destroy doesnt close fd */
			xprt->xp_fd = RPC_ANYFD;
		SVC_DESTROY(xprt);
	}
	return (NULL);
}