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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Contains routines that deal with TLI/XTI endpoints and rpc services.
28 */
29
30 #include <sys/types.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <libintl.h>
35 #include <unistd.h>
36 #include <sys/sysmacros.h>
37 #include <netconfig.h>
38 #include <errno.h>
39 #include <sys/sockio.h>
40 #include "inetd_impl.h"
41
42 uu_list_pool_t *conn_ind_pool = NULL;
43
44 /*
45 * RPC functions.
46 */
47
48 /*
49 * Returns B_TRUE if the non-address components of the 2 rpc_info_t structures
50 * are equivalent, else B_FALSE.
51 */
52 boolean_t
rpc_info_equal(const rpc_info_t * ri,const rpc_info_t * ri2)53 rpc_info_equal(const rpc_info_t *ri, const rpc_info_t *ri2)
54 {
55 return ((ri->prognum == ri2->prognum) &&
56 (ri->lowver == ri2->lowver) &&
57 (ri->highver == ri2->highver) &&
58 (strcmp(ri->netid, ri2->netid) == 0));
59 }
60
61 /*
62 * Determine if we have a configured interface for the specified address
63 * family. This code is a mirror of libnsl's __can_use_af(). We mirror
64 * it because we need an exact duplicate of its behavior, yet the
65 * function isn't exported by libnsl, and this fix is considered short-
66 * term, so it's not worth exporting it.
67 *
68 * We need to duplicate __can_use_af() so we can accurately determine
69 * when getnetconfigent() returns failure for a v6 netid due to no IPv6
70 * interfaces being configured: getnetconfigent() returns failure
71 * if a netid is either 'tcp6' or 'udp6' and __can_use_af() returns 0,
72 * but it doesn't return a return code to uniquely determine this
73 * failure. If we don't accurately determine these failures, we could
74 * output error messages in a case when they weren't justified.
75 */
76 static int
can_use_af(sa_family_t af)77 can_use_af(sa_family_t af)
78 {
79 struct lifnum lifn;
80 int fd;
81
82 if ((fd = open("/dev/udp", O_RDONLY)) < 0) {
83 return (0);
84 }
85 lifn.lifn_family = af;
86 /* LINTED ECONST_EXPR */
87 lifn.lifn_flags = IFF_UP & !(IFF_NOXMIT | IFF_DEPRECATED);
88 if (ioctl(fd, SIOCGLIFNUM, &lifn, sizeof (lifn)) < 0) {
89 lifn.lifn_count = 0;
90 }
91
92 (void) close(fd);
93 return (lifn.lifn_count);
94 }
95
96 static boolean_t
is_v6_netid(const char * netid)97 is_v6_netid(const char *netid)
98 {
99 return ((strcmp(netid, SOCKET_PROTO_TCP6) == 0) ||
100 (strcmp(netid, SOCKET_PROTO_UDP6) == 0));
101 }
102
103 /*
104 * Registers with rpcbind the program number with all versions, from low to
105 * high, with the netid, all specified in 'rpc'. If registration fails,
106 * returns -1, else 0.
107 */
108 int
register_rpc_service(const char * fmri,const rpc_info_t * rpc)109 register_rpc_service(const char *fmri, const rpc_info_t *rpc)
110 {
111 struct netconfig *nconf;
112 int ver;
113
114 if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
115 /*
116 * Check whether getnetconfigent() failed as a result of
117 * having no IPv6 interfaces configured for a v6 netid, or
118 * as a result of a 'real' error, and output an appropriate
119 * message with an appropriate severity.
120 */
121 if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) {
122 warn_msg(gettext(
123 "Couldn't register netid %s for RPC instance %s "
124 "because no IPv6 interfaces are plumbed"),
125 rpc->netid, fmri);
126 } else {
127 error_msg(gettext(
128 "Failed to lookup netid '%s' for instance %s: %s"),
129 rpc->netid, fmri, nc_sperror());
130 }
131 return (-1);
132 }
133
134 for (ver = rpc->lowver; ver <= rpc->highver; ver++) {
135 if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) {
136 error_msg(gettext("Failed to register version %d "
137 "of RPC service instance %s, netid %s"), ver,
138 fmri, rpc->netid);
139
140 for (ver--; ver >= rpc->lowver; ver--)
141 (void) rpcb_unset(rpc->prognum, ver, nconf);
142
143 freenetconfigent(nconf);
144 return (-1);
145 }
146 }
147
148 freenetconfigent(nconf);
149 return (0);
150 }
151
152 /* Unregister all the registrations done by register_rpc_service */
153 void
unregister_rpc_service(const char * fmri,const rpc_info_t * rpc)154 unregister_rpc_service(const char *fmri, const rpc_info_t *rpc)
155 {
156 int ver;
157 struct netconfig *nconf;
158
159 if ((nconf = getnetconfigent(rpc->netid)) == NULL) {
160 /*
161 * Don't output an error message if getnetconfigent() fails for
162 * a v6 netid when an IPv6 interface isn't configured.
163 */
164 if (!(is_v6_netid(rpc->netid) && !can_use_af(AF_INET6))) {
165 error_msg(gettext(
166 "Failed to lookup netid '%s' for instance %s: %s"),
167 rpc->netid, fmri, nc_sperror());
168 }
169 return;
170 }
171
172 for (ver = rpc->lowver; ver <= rpc->highver; ver++)
173 (void) rpcb_unset(rpc->prognum, ver, nconf);
174
175 freenetconfigent(nconf);
176 }
177
178 /*
179 * TLI/XTI functions.
180 */
181
182 int
tlx_init(void)183 tlx_init(void)
184 {
185 if ((conn_ind_pool = uu_list_pool_create("conn_ind_pool",
186 sizeof (tlx_conn_ind_t), offsetof(tlx_conn_ind_t, link),
187 NULL, UU_LIST_POOL_DEBUG)) == NULL) {
188 error_msg("%s: %s", gettext("Failed to create uu pool"),
189 uu_strerror(uu_error()));
190 return (-1);
191 }
192
193 return (0);
194 }
195
196 void
tlx_fini(void)197 tlx_fini(void)
198 {
199 if (conn_ind_pool != NULL) {
200 uu_list_pool_destroy(conn_ind_pool);
201 conn_ind_pool = NULL;
202 }
203 }
204
205 /*
206 * Checks if the contents of the 2 tlx_info_t structures are equivalent.
207 * If 'isrpc' is false, the address components of the two structures are
208 * compared for equality as part of this. If the two structures are
209 * equivalent B_TRUE is returned, else B_FALSE.
210 */
211 boolean_t
tlx_info_equal(const tlx_info_t * ti,const tlx_info_t * ti2,boolean_t isrpc)212 tlx_info_equal(const tlx_info_t *ti, const tlx_info_t *ti2, boolean_t isrpc)
213 {
214 return ((isrpc || (memcmp(ti->local_addr.buf, ti2->local_addr.buf,
215 sizeof (struct sockaddr_storage)) == 0)) &&
216 (strcmp(ti->dev_name, ti2->dev_name) == 0));
217 }
218
219 /*
220 * Attempts to bind an address to the network fd 'fd'. If 'reqaddr' is non-NULL,
221 * it attempts to bind to that requested address, else it binds to a kernel
222 * selected address. In the former case, the function returning success
223 * doesn't guarantee that the requested address was bound (the caller needs to
224 * check). If 'retaddr' is non-NULL, the bound address is returned in it. The
225 * 'qlen' parameter is used to set the connection backlog. If the bind
226 * succeeds 0 is returned, else -1.
227 */
228 static int
tlx_bind(int fd,const struct netbuf * reqaddr,struct netbuf * retaddr,int qlen)229 tlx_bind(int fd, const struct netbuf *reqaddr, struct netbuf *retaddr, int qlen)
230 {
231 struct t_bind breq;
232 struct t_bind bret;
233
234 if (retaddr != NULL) { /* caller requests bound address be returned */
235 bret.addr.buf = retaddr->buf;
236 bret.addr.maxlen = retaddr->maxlen;
237 }
238
239 if (reqaddr != NULL) { /* caller requests specific address */
240 breq.addr.buf = reqaddr->buf;
241 breq.addr.len = reqaddr->len;
242 } else {
243 breq.addr.len = 0;
244 }
245 breq.qlen = qlen;
246
247 if (t_bind(fd, &breq, retaddr != NULL ? &bret : NULL) < 0)
248 return (-1);
249
250 if (retaddr != NULL)
251 retaddr->len = bret.addr.len;
252
253 return (0);
254 }
255
256 static int
tlx_setsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)257 tlx_setsockopt(int fd, int level, int optname, const void *optval,
258 socklen_t optlen)
259 {
260 struct t_optmgmt request, reply;
261 struct {
262 struct opthdr sockopt;
263 char data[256];
264 } optbuf;
265
266 if (optlen > sizeof (optbuf.data)) {
267 error_msg(gettext("t_optmgmt request too long"));
268 return (-1);
269 }
270
271 optbuf.sockopt.level = level;
272 optbuf.sockopt.name = optname;
273 optbuf.sockopt.len = optlen;
274 (void) memcpy(optbuf.data, optval, optlen);
275
276 request.opt.len = sizeof (struct opthdr) + optlen;
277 request.opt.buf = (char *)&optbuf;
278 request.flags = T_NEGOTIATE;
279
280 reply.opt.maxlen = sizeof (struct opthdr) + optlen;
281 reply.opt.buf = (char *)&optbuf;
282 reply.flags = 0;
283
284 if ((t_optmgmt(fd, &request, &reply) == -1) ||
285 (reply.flags != T_SUCCESS)) {
286 error_msg("t_optmgmt: %s", t_strerror(t_errno));
287 return (-1);
288 }
289 return (0);
290 }
291
292 /*
293 * Compare contents of netbuf for equality. Return B_TRUE on a match and
294 * B_FALSE for mismatch.
295 */
296 static boolean_t
netbufs_equal(struct netbuf * n1,struct netbuf * n2)297 netbufs_equal(struct netbuf *n1, struct netbuf *n2)
298 {
299 return ((n1->len == n2->len) &&
300 (memcmp(n1->buf, n2->buf, (size_t)n1->len) == 0));
301 }
302
303 /*
304 * Create a tli/xti endpoint, either bound to the address specified in
305 * 'instance' for non-RPC services, else a kernel chosen address.
306 * Returns -1 on failure, else 0.
307 */
308 int
create_bound_endpoint(const instance_t * inst,tlx_info_t * tlx_info)309 create_bound_endpoint(const instance_t *inst, tlx_info_t *tlx_info)
310 {
311 int fd;
312 int qlen;
313 const char *fmri = inst->fmri;
314 struct netbuf *reqaddr;
315 struct netbuf *retaddr;
316 struct netbuf netbuf;
317 struct sockaddr_storage ss;
318 rpc_info_t *rpc = tlx_info->pr_info.ri;
319
320 if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
321 error_msg(gettext("Failed to open transport %s for "
322 "instance %s, proto %s: %s"), tlx_info->dev_name,
323 fmri, tlx_info->pr_info.proto, t_strerror(t_errno));
324 return (-1);
325 }
326
327 if (tlx_info->pr_info.v6only) {
328 int on = 1;
329
330 /* restrict to IPv6 communications only */
331 if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
332 sizeof (on)) == -1) {
333 (void) t_close(fd);
334 return (-1);
335 }
336 }
337
338 /*
339 * Negotiate for the returning of the remote uid for loopback
340 * transports for RPC services. This needs to be done before the
341 * endpoint is bound using t_bind(), so that any requests to it
342 * contain the uid.
343 */
344 if ((rpc != NULL) && (rpc->is_loopback))
345 svc_fd_negotiate_ucred(fd);
346
347 /*
348 * Bind the service's address to the endpoint and setup connection
349 * backlog. In the case of RPC services, we specify a NULL requested
350 * address and accept what we're given, storing the returned address
351 * for later RPC binding. In the case of non-RPC services we specify
352 * the service's associated address.
353 */
354 if (rpc != NULL) {
355 reqaddr = NULL;
356 retaddr = &(rpc->netbuf);
357 } else {
358 reqaddr = &(tlx_info->local_addr);
359 netbuf.buf = (char *)&ss;
360 netbuf.maxlen = sizeof (ss);
361 retaddr = &netbuf;
362 }
363
364 /* ignored for conn/less services */
365 qlen = inst->config->basic->conn_backlog;
366
367 if ((tlx_bind(fd, reqaddr, retaddr, qlen) == -1) ||
368 ((reqaddr != NULL) && !netbufs_equal(reqaddr, retaddr))) {
369 error_msg(gettext("Failed to bind to the requested address "
370 "for instance %s, proto %s"), fmri,
371 tlx_info->pr_info.proto);
372 (void) t_close(fd);
373 return (-1);
374 }
375
376 return (fd);
377 }
378
379 /*
380 * Takes a connection request off 'fd' in the form of a t_call structure
381 * and returns a pointer to it.
382 * Returns NULL on failure, else pointer to t_call structure on success.
383 */
384 static struct t_call *
get_new_conind(int fd)385 get_new_conind(int fd)
386 {
387 struct t_call *call;
388
389 /* LINTED E_BAD_PTR_CAST_ALIGN */
390 if ((call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL)) == NULL) {
391 error_msg("t_alloc: %s", t_strerror(t_errno));
392 return (NULL);
393 }
394 if (t_listen(fd, call) < 0) {
395 error_msg("t_listen: %s", t_strerror(t_errno));
396 (void) t_free((char *)call, T_CALL);
397 return (NULL);
398 }
399
400 return (call);
401 }
402
403 /* Add 'call' to the connection indication queue 'queue'. */
404 int
queue_conind(uu_list_t * queue,struct t_call * call)405 queue_conind(uu_list_t *queue, struct t_call *call)
406 {
407 tlx_conn_ind_t *ci;
408
409 if ((ci = malloc(sizeof (tlx_conn_ind_t))) == NULL) {
410 error_msg(strerror(errno));
411 return (-1);
412 }
413
414 ci->call = call;
415 uu_list_node_init(ci, &ci->link, conn_ind_pool);
416 (void) uu_list_insert_after(queue, NULL, ci);
417
418 return (0);
419 }
420
421 /*
422 * Remove and return a pointer to the first call on queue 'queue'. However,
423 * if the queue is empty returns NULL.
424 */
425 struct t_call *
dequeue_conind(uu_list_t * queue)426 dequeue_conind(uu_list_t *queue)
427 {
428 struct t_call *ret;
429 tlx_conn_ind_t *ci = uu_list_first(queue);
430
431 if (ci == NULL)
432 return (NULL);
433
434 ret = ci->call;
435 uu_list_remove(queue, ci);
436 free(ci);
437
438 return (ret);
439 }
440
441 /*
442 * Handle a TLOOK notification received during a t_accept() call.
443 * Returns -1 on failure, else 0.
444 */
445 static int
process_tlook(const char * fmri,tlx_info_t * tlx_info)446 process_tlook(const char *fmri, tlx_info_t *tlx_info)
447 {
448 int event;
449 int fd = tlx_info->pr_info.listen_fd;
450
451 switch (event = t_look(fd)) {
452 case T_LISTEN: {
453 struct t_call *call;
454
455 debug_msg("process_tlook: T_LISTEN event");
456 if ((call = get_new_conind(fd)) == NULL)
457 return (-1);
458 if (queue_conind(tlx_info->conn_ind_queue, call) == -1) {
459 error_msg(gettext("Failed to queue connection "
460 "indication for instance %s"), fmri);
461 (void) t_free((char *)call, T_CALL);
462 return (-1);
463 }
464 break;
465 }
466 case T_DISCONNECT: {
467 /*
468 * Note: In Solaris 2.X (SunOS 5.X) bundled
469 * connection-oriented transport drivers
470 * [ e.g /dev/tcp and /dev/ticots and
471 * /dev/ticotsord (tl)] we do not send disconnect
472 * indications to listening endpoints.
473 * So this will not be seen with endpoints on Solaris
474 * bundled transport devices. However, Streams TPI
475 * allows for this (broken?) behavior and so we account
476 * for it here because of the possibility of unbundled
477 * transport drivers causing this.
478 */
479 tlx_conn_ind_t *cip;
480 struct t_discon *discon;
481
482 debug_msg("process_tlook: T_DISCONNECT event");
483
484 /* LINTED */
485 if ((discon = (struct t_discon *)
486 t_alloc(fd, T_DIS, T_ALL)) == NULL) {
487 error_msg("t_alloc: %s", t_strerror(t_errno));
488 return (-1);
489 }
490 if (t_rcvdis(fd, discon) < 0) {
491 error_msg("t_rcvdis: %s", t_strerror(t_errno));
492 (void) t_free((char *)discon, T_DIS);
493 return (-1);
494 }
495
496 /*
497 * Find any queued connection pending that matches this
498 * disconnect notice and remove from the pending queue.
499 */
500 cip = uu_list_first(tlx_info->conn_ind_queue);
501 while ((cip != NULL) &&
502 (cip->call->sequence != discon->sequence)) {
503 cip = uu_list_next(tlx_info->conn_ind_queue, cip);
504 }
505 if (cip != NULL) { /* match found */
506 uu_list_remove(tlx_info->conn_ind_queue, cip);
507 (void) t_free((char *)cip->call, T_CALL);
508 free(cip);
509 }
510
511 (void) t_free((char *)discon, T_DIS);
512 break;
513 }
514 case -1:
515 error_msg("t_look: %s", t_strerror(t_errno));
516 return (-1);
517 default:
518 error_msg(gettext("do_tlook: unexpected t_look event: %d"),
519 event);
520 return (-1);
521 }
522
523 return (0);
524 }
525
526 /*
527 * This call attempts to t_accept() an incoming/pending TLI connection.
528 * If it is thwarted by a TLOOK, it is deferred and whatever is on the
529 * file descriptor, removed after a t_look. (Incoming connect indications
530 * get queued for later processing and disconnect indications remove a
531 * a queued connection request if a match found).
532 * Returns -1 on failure, else 0.
533 */
534 int
tlx_accept(const char * fmri,tlx_info_t * tlx_info,struct sockaddr_storage * remote_addr)535 tlx_accept(const char *fmri, tlx_info_t *tlx_info,
536 struct sockaddr_storage *remote_addr)
537 {
538 tlx_conn_ind_t *conind;
539 struct t_call *call;
540 int fd;
541 int listen_fd = tlx_info->pr_info.listen_fd;
542
543 if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
544 error_msg("t_open: %s", t_strerror(t_errno));
545 return (-1);
546 }
547
548 if (tlx_info->pr_info.v6only) {
549 int on = 1;
550
551 /* restrict to IPv6 communications only */
552 if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
553 sizeof (on)) == -1) {
554 (void) t_close(fd);
555 return (-1);
556 }
557 }
558
559 if (t_bind(fd, NULL, NULL) == -1) {
560 error_msg("t_bind: %s", t_strerror(t_errno));
561 (void) t_close(fd);
562 return (-1);
563 }
564
565 /*
566 * Get the next connection indication - first try the pending
567 * queue, then, if none there, get a new one from the file descriptor.
568 */
569 if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) {
570 debug_msg("taking con off queue");
571 call = conind->call;
572 } else if ((call = get_new_conind(listen_fd)) == NULL) {
573 (void) t_close(fd);
574 return (-1);
575 }
576
577 /*
578 * Accept the connection indication on the newly created endpoint.
579 * If we fail, and it's the result of a tlook, queue the indication
580 * if it isn't already, and go and process the t_look.
581 */
582 if (t_accept(listen_fd, fd, call) == -1) {
583 if (t_errno == TLOOK) {
584 if (uu_list_first(tlx_info->conn_ind_queue) == NULL) {
585 /*
586 * We are first one to have to defer accepting
587 * and start the pending connections list.
588 */
589 if (queue_conind(tlx_info->conn_ind_queue,
590 call) == -1) {
591 error_msg(gettext(
592 "Failed to queue connection "
593 "indication for instance %s"),
594 fmri);
595 (void) t_free((char *)call, T_CALL);
596 return (-1);
597 }
598 }
599 (void) process_tlook(fmri, tlx_info);
600 } else { /* non-TLOOK accept failure */
601 error_msg("%s: %s", "t_accept failed",
602 t_strerror(t_errno));
603 /*
604 * If we were accepting a queued connection, dequeue
605 * it.
606 */
607 if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
608 (void) dequeue_conind(tlx_info->conn_ind_queue);
609 (void) t_free((char *)call, T_CALL);
610 }
611
612 (void) t_close(fd);
613 return (-1);
614 }
615
616 /* Copy remote address into address parameter */
617 (void) memcpy(remote_addr, call->addr.buf,
618 MIN(call->addr.len, sizeof (*remote_addr)));
619
620 /* If we were accepting a queued connection, dequeue it. */
621 if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
622 (void) dequeue_conind(tlx_info->conn_ind_queue);
623 (void) t_free((char *)call, T_CALL);
624
625 return (fd);
626 }
627
628 /* protocol independent network fd close routine */
629 void
close_net_fd(instance_t * inst,int fd)630 close_net_fd(instance_t *inst, int fd)
631 {
632 if (inst->config->basic->istlx) {
633 (void) t_close(fd);
634 } else {
635 (void) close(fd);
636 }
637 }
638
639 /*
640 * Consume some data from the given endpoint of the given wait-based instance.
641 */
642 void
consume_wait_data(instance_t * inst,int fd)643 consume_wait_data(instance_t *inst, int fd)
644 {
645 int flag;
646 char buf[50]; /* same arbitrary size as old inetd */
647
648 if (inst->config->basic->istlx) {
649 (void) t_rcv(fd, buf, sizeof (buf), &flag);
650 } else {
651 (void) recv(fd, buf, sizeof (buf), 0);
652 }
653 }
654