xref: /titanic_50/usr/src/cmd/cvcd/sparc/sun4u/starfire/cvcd.c (revision 360e6f5e7a29d5950aa1985f56811731715da7e5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*360e6f5eSmathue  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This code implements the Starfire Virtual Console host daemon
307c478bd9Sstevel@tonic-gate  * (see cvcd(1M)).  It accepts a connection from netcon_server
317c478bd9Sstevel@tonic-gate  * and transfers console I/O to/from the SSP across the
327c478bd9Sstevel@tonic-gate  * network via TLI.  The I/O is sent to the cvcredir device
337c478bd9Sstevel@tonic-gate  * on the host (see cvc(7) and cvcredir(7)).  It also sends
347c478bd9Sstevel@tonic-gate  * disconnect and break ioctl's to the kernel CVC drivers.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <tiuser.h>
457c478bd9Sstevel@tonic-gate #include <sys/timod.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <stropts.h>
527c478bd9Sstevel@tonic-gate #include <sys/conf.h>
537c478bd9Sstevel@tonic-gate #include <pwd.h>
547c478bd9Sstevel@tonic-gate #include <errno.h>
557c478bd9Sstevel@tonic-gate #include <sys/socket.h>
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate #include <locale.h>
587c478bd9Sstevel@tonic-gate #include <termio.h>
597c478bd9Sstevel@tonic-gate #include <signal.h>
607c478bd9Sstevel@tonic-gate #include <sys/cvc.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <string.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
657c478bd9Sstevel@tonic-gate #include <sys/file.h>
667c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <netdb.h>
717c478bd9Sstevel@tonic-gate #include <net/if.h>
727c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <inet/common.h>
757c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Process priority control */
787c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
797c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h>
807c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  *  Misc. defines.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate #define	CONREF "connection request from illegal host"
867c478bd9Sstevel@tonic-gate #define	SSPHOSTNAMEFILE	"/etc/ssphostname"
877c478bd9Sstevel@tonic-gate #define	NODENAME	"/etc/nodename"
887c478bd9Sstevel@tonic-gate #define	MAXIFS		256
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Function prototypes
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate static void cvcd_connect(int fd, struct pollfd *);
947c478bd9Sstevel@tonic-gate static void cvcd_reject(int fd);
957c478bd9Sstevel@tonic-gate static void cvcd_read(struct pollfd *);
967c478bd9Sstevel@tonic-gate static void cvcd_write(char *data, int size);
977c478bd9Sstevel@tonic-gate static void cvcd_status(int fd);
987c478bd9Sstevel@tonic-gate static void cvcd_winch(int, char *, int);
997c478bd9Sstevel@tonic-gate static void cvcd_ioctl(int fd, int cmd);
1007c478bd9Sstevel@tonic-gate static void cvcd_err(int code, char *format, ...);
1017c478bd9Sstevel@tonic-gate static void usage(void);
1027c478bd9Sstevel@tonic-gate static id_t schedinfo(char *name, short *maxpri);
1037c478bd9Sstevel@tonic-gate static void cvcd_setopt(int fd, int name);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  *  Globals
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static int		rconsfd;	/* Console redirection driver */
1097c478bd9Sstevel@tonic-gate static char		progname[MAXPATHLEN];
1107c478bd9Sstevel@tonic-gate static char		ssphostname[MAXPATHLEN];
1117c478bd9Sstevel@tonic-gate static int		debug = 0;
1127c478bd9Sstevel@tonic-gate static int		connected = 0;
1137c478bd9Sstevel@tonic-gate static int		peercheck = 1;
1147c478bd9Sstevel@tonic-gate static char		nic_name[32];
1157c478bd9Sstevel@tonic-gate 
116*360e6f5eSmathue int
main(int argc,char ** argv)1177c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int			opt;
1207c478bd9Sstevel@tonic-gate 	int			tport = 0;
1217c478bd9Sstevel@tonic-gate 	char			*hostname;
1227c478bd9Sstevel@tonic-gate 	struct utsname		utsname;
1237c478bd9Sstevel@tonic-gate 	struct t_info		tinfo;
1247c478bd9Sstevel@tonic-gate 	int			cvcd_ssp;
1257c478bd9Sstevel@tonic-gate 	int			nfd;
1267c478bd9Sstevel@tonic-gate 	struct pollfd		*cvcd_pfd;
1277c478bd9Sstevel@tonic-gate 	int			i;
1287c478bd9Sstevel@tonic-gate 	int			j;
1297c478bd9Sstevel@tonic-gate 	struct servent		*se;
1307c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin;
1317c478bd9Sstevel@tonic-gate 	struct t_bind		*reqb;
1327c478bd9Sstevel@tonic-gate 	struct t_bind		*retb;
1337c478bd9Sstevel@tonic-gate 	struct t_optmgmt	*topt, *tropt;
1347c478bd9Sstevel@tonic-gate 	struct opthdr		*sockopt;
1357c478bd9Sstevel@tonic-gate 	int			on = 1;
1367c478bd9Sstevel@tonic-gate 	int			tmperr = 0;
1377c478bd9Sstevel@tonic-gate 	int			event;
1387c478bd9Sstevel@tonic-gate 	char 			prefix[256];
1397c478bd9Sstevel@tonic-gate 	pcparms_t	pcparms;
1407c478bd9Sstevel@tonic-gate 	tsparms_t	*tsparmsp;
1417c478bd9Sstevel@tonic-gate 	id_t	pid, tsID;
1427c478bd9Sstevel@tonic-gate 	short	tsmaxpri;
1437c478bd9Sstevel@tonic-gate 	static int netcon_fail = 0;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1467c478bd9Sstevel@tonic-gate 	(void) strcpy(progname, argv[0]);
1477c478bd9Sstevel@tonic-gate 	(void) memset(ssphostname, 0, MAXPATHLEN);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if ((cvcd_ssp = open(SSPHOSTNAMEFILE, O_RDONLY)) < 0) {
1507c478bd9Sstevel@tonic-gate 		/*
1517c478bd9Sstevel@tonic-gate 		 * If there is no /etc/ssphostname disable peer check after
1527c478bd9Sstevel@tonic-gate 		 * issuing warning.
1537c478bd9Sstevel@tonic-gate 		 */
1547c478bd9Sstevel@tonic-gate 		tmperr = errno;
1557c478bd9Sstevel@tonic-gate 		peercheck = 0;
1567c478bd9Sstevel@tonic-gate 	} else {
1577c478bd9Sstevel@tonic-gate 		if ((i = read(cvcd_ssp, ssphostname, MAXPATHLEN)) < 0) {
1587c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "failed to read ssphostname");
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		 * The ssp-config(1M) command newline terminates the
1627c478bd9Sstevel@tonic-gate 		 * ssphostname in the /etc/ssphostname file
1637c478bd9Sstevel@tonic-gate 		 */
1647c478bd9Sstevel@tonic-gate 		ssphostname[i-1] = '\0';
1657c478bd9Sstevel@tonic-gate 		(void) close(cvcd_ssp);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		(void) memset(nic_name, 0, sizeof (nic_name));
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #if defined(DEBUG)
1717c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "dp:r:")) != EOF) {
1727c478bd9Sstevel@tonic-gate #else
1737c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "r:")) != EOF) {
1747c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
1757c478bd9Sstevel@tonic-gate 		switch (opt) {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #if defined(DEBUG)
1787c478bd9Sstevel@tonic-gate 			case 'd' :	debug = 1;
1797c478bd9Sstevel@tonic-gate 					break;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			case 'p' :	tport = atoi(optarg);
1827c478bd9Sstevel@tonic-gate 					break;
1837c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 			case 'r' :	(void) strcpy(ssphostname, optarg);
1867c478bd9Sstevel@tonic-gate 					break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 			default  :	usage();
1897c478bd9Sstevel@tonic-gate 					exit(1);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (uname(&utsname) == -1) {
1947c478bd9Sstevel@tonic-gate 		perror("HOSTNAME not defined");
1957c478bd9Sstevel@tonic-gate 		exit(1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	hostname = utsname.nodename;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * hostname may still be NULL, depends on when cvcd was started
2017c478bd9Sstevel@tonic-gate 	 * in the boot sequence.  If it is NULL, try one more time
2027c478bd9Sstevel@tonic-gate 	 * to get a hostname -> look in the /etc/nodename file.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	if (!strlen(hostname)) {
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * try to get the hostname from the /etc/nodename file
2077c478bd9Sstevel@tonic-gate 		 * we reuse the utsname.nodename buffer here!  hostname
2087c478bd9Sstevel@tonic-gate 		 * already points to it.
2097c478bd9Sstevel@tonic-gate 		 */
2107c478bd9Sstevel@tonic-gate 		if ((nfd = open(NODENAME, O_RDONLY)) > 0) {
2117c478bd9Sstevel@tonic-gate 			if ((i = read(nfd, utsname.nodename, SYS_NMLN)) <= 0) {
2127c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_WARNING,
2137c478bd9Sstevel@tonic-gate 				"failed to acquire hostname");
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			utsname.nodename[i-1] = '\0';
2167c478bd9Sstevel@tonic-gate 			(void) close(nfd);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * Must be root.
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 	if (debug == 0 && geteuid() != 0) {
2247c478bd9Sstevel@tonic-gate 		fprintf(stderr, "cvcd: Must be root");
2257c478bd9Sstevel@tonic-gate 		exit(1);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Daemonize...
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (debug == 0) {
2327c478bd9Sstevel@tonic-gate 		for (i = 0; i < NOFILE; i++) {
2337c478bd9Sstevel@tonic-gate 			(void) close(i);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		(void) chdir("/");
2367c478bd9Sstevel@tonic-gate 		(void) umask(0);
2377c478bd9Sstevel@tonic-gate 		if (fork() != 0) {
2387c478bd9Sstevel@tonic-gate 			exit(0);
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		(void) setpgrp();
2417c478bd9Sstevel@tonic-gate 		(void) sprintf(prefix, "%s-(HOSTNAME:%s)", progname, hostname);
2427c478bd9Sstevel@tonic-gate 		openlog(prefix, LOG_CONS | LOG_NDELAY, LOG_LOCAL0);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (peercheck == 0) {
2457c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "open(SSPHOSTNAMEFILE):%s",
2467c478bd9Sstevel@tonic-gate 		    strerror(tmperr));
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	cvcd_pfd = (struct pollfd *)malloc(3*sizeof (struct pollfd));
2507c478bd9Sstevel@tonic-gate 	if (cvcd_pfd == (struct pollfd *)NULL) {
2517c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "malloc:", strerror(errno));
2527c478bd9Sstevel@tonic-gate 		exit(1);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	(void) memset((void *)cvcd_pfd, 0, 3*sizeof (struct pollfd));
2557c478bd9Sstevel@tonic-gate 	cvcd_pfd[0].fd = -1;
2567c478bd9Sstevel@tonic-gate 	cvcd_pfd[1].fd = -1;
2577c478bd9Sstevel@tonic-gate 	cvcd_pfd[2].fd = -1;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* SPR 94004 */
2607c478bd9Sstevel@tonic-gate 	(void) sigignore(SIGTERM);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * SPR 83644: cvc and kadb are not compatible under heavy loads.
2647c478bd9Sstevel@tonic-gate 	 *	Fix: will give cvcd highest TS priority at execution time.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	pid = getpid();
2677c478bd9Sstevel@tonic-gate 	pcparms.pc_cid = PC_CLNULL;
2687c478bd9Sstevel@tonic-gate 	tsparmsp = (tsparms_t *)pcparms.pc_clparms;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/* Get scheduler properties for this PID */
2717c478bd9Sstevel@tonic-gate 	if (priocntl(P_PID, pid, PC_GETPARMS, (caddr_t)&pcparms) == -1L) {
2727c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR,
2737c478bd9Sstevel@tonic-gate 			"cvcd: GETPARMS failed. Warning: can't get ",
2747c478bd9Sstevel@tonic-gate 			"TS priorities.");
2757c478bd9Sstevel@tonic-gate 	} else {
2767c478bd9Sstevel@tonic-gate 		/* Get class IDs and maximum priorities for a TS process */
2777c478bd9Sstevel@tonic-gate 		if ((tsID = schedinfo("TS", &tsmaxpri)) == -1) {
2787c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "cvcd: Warning, can't get ",
2797c478bd9Sstevel@tonic-gate 				"TS scheduler info.");
2807c478bd9Sstevel@tonic-gate 		} else {
2817c478bd9Sstevel@tonic-gate 			if (debug) {	/* Print priority info */
2827c478bd9Sstevel@tonic-gate 				if (pcparms.pc_cid == tsID) {
2837c478bd9Sstevel@tonic-gate 					cvcd_err(LOG_DEBUG, "%s%d%s%d%s%d\n",
2847c478bd9Sstevel@tonic-gate 						"cvcd:: PID:", pid,
2857c478bd9Sstevel@tonic-gate 						", TS priority:",
2867c478bd9Sstevel@tonic-gate 						tsparmsp->ts_upri,
2877c478bd9Sstevel@tonic-gate 						", TS max_pri:", tsmaxpri);
2887c478bd9Sstevel@tonic-gate 				}
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 			/* Change proc's priority to maxtspri */
2917c478bd9Sstevel@tonic-gate 			pcparms.pc_cid = tsID;
2927c478bd9Sstevel@tonic-gate 			tsparmsp = (struct tsparms *)pcparms.pc_clparms;
2937c478bd9Sstevel@tonic-gate 			tsparmsp->ts_upri = tsmaxpri;
2947c478bd9Sstevel@tonic-gate 			tsparmsp->ts_uprilim = tsmaxpri;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 			if (priocntl(P_PID, pid, PC_SETPARMS,
2977c478bd9Sstevel@tonic-gate 				(caddr_t)&pcparms) == -1L) {
2987c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_ERR, "cvcd: Warning, ",
2997c478bd9Sstevel@tonic-gate 					"can't set TS maximum priority.");
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			/* Done */
3027c478bd9Sstevel@tonic-gate 			if (debug) { /* Get new scheduler properties for PID */
3037c478bd9Sstevel@tonic-gate 				if (priocntl(P_PID, pid, PC_GETPARMS,
3047c478bd9Sstevel@tonic-gate 					(caddr_t)&pcparms) == -1L) {
3057c478bd9Sstevel@tonic-gate 					cvcd_err(LOG_DEBUG, "GETPARMS failed");
3067c478bd9Sstevel@tonic-gate 					exit(1);
3077c478bd9Sstevel@tonic-gate 				} else {
3087c478bd9Sstevel@tonic-gate 					cvcd_err(LOG_DEBUG, "%s%d%s%d%s%d\n",
3097c478bd9Sstevel@tonic-gate 						"cvcd:: PID:", pid,
3107c478bd9Sstevel@tonic-gate 						", New TS priority:",
3117c478bd9Sstevel@tonic-gate 						tsparmsp->ts_upri,
3127c478bd9Sstevel@tonic-gate 						", TS max_pri:", tsmaxpri);
3137c478bd9Sstevel@tonic-gate 				}
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (debug == 1) {
3197c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_DEBUG, "tport = %d, debug = %d", tport, debug);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (tport == 0) {
3237c478bd9Sstevel@tonic-gate 		if ((se = getservbyname(CVCD_SERVICE, "tcp")) == NULL) {
3247c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "getservbyname(%s) not found",
3257c478bd9Sstevel@tonic-gate 				CVCD_SERVICE);
3267c478bd9Sstevel@tonic-gate 			exit(1);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		tport = se->s_port;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	cvcd_ssp = t_open(TCP_DEV, O_RDWR, &tinfo);
3327c478bd9Sstevel@tonic-gate 	if (cvcd_ssp == -1) {
3337c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "t_open: %s", t_errlist[t_errno]);
3347c478bd9Sstevel@tonic-gate 		exit(1);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * Set the SO_REUSEADDR option for this TLI endpoint.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate 	cvcd_setopt(cvcd_ssp, SO_REUSEADDR);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Set the SO_DONTROUTE option for this TLI endpoint, if
3447c478bd9Sstevel@tonic-gate 	 * /etc/ssphostname exists.
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	if (peercheck == 1)
3477c478bd9Sstevel@tonic-gate 		cvcd_setopt(cvcd_ssp, SO_DONTROUTE);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Bind it.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	if (((reqb = (struct t_bind *)t_alloc(cvcd_ssp, T_BIND, T_ALL))
3537c478bd9Sstevel@tonic-gate 		== (struct t_bind *)NULL)) {
3547c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "%s", t_errlist[t_errno]);
3557c478bd9Sstevel@tonic-gate 			exit(1);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	if (((retb = (struct t_bind *)t_alloc(cvcd_ssp, T_BIND, T_ALL))
3587c478bd9Sstevel@tonic-gate 		== (struct t_bind *)NULL)) {
3597c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "%s", t_errlist[t_errno]);
3607c478bd9Sstevel@tonic-gate 			exit(1);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	reqb->qlen = 1;
3637c478bd9Sstevel@tonic-gate 	reqb->addr.len = sizeof (struct sockaddr_in);
3647c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)reqb->addr.buf;
3657c478bd9Sstevel@tonic-gate 	(void) memset((void *)sin, 0, sizeof (struct sockaddr_in));
3667c478bd9Sstevel@tonic-gate 	sin->sin_family = AF_INET;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	sin->sin_addr.s_addr = htonl(INADDR_ANY);
3707c478bd9Sstevel@tonic-gate 	sin->sin_port = htons(tport);
3717c478bd9Sstevel@tonic-gate 	if (t_bind(cvcd_ssp, reqb, retb) == -1) {
3727c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "t_bind: %s", t_errlist[t_errno]);
3737c478bd9Sstevel@tonic-gate 		exit(1);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)retb->addr.buf;
3767c478bd9Sstevel@tonic-gate 	if (sin->sin_port != htons(tport)) {
3777c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "t_bind: bound to wrong port");
3787c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "Wanted %d, got %d", tport,
3797c478bd9Sstevel@tonic-gate 			ntohs(sin->sin_port));
3807c478bd9Sstevel@tonic-gate 		exit(1);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	t_free((char *)reqb, T_BIND);
3847c478bd9Sstevel@tonic-gate 	t_free((char *)retb, T_BIND);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 *  Wait for connect from OBP.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	cvcd_pfd[2].fd = cvcd_ssp;
3917c478bd9Sstevel@tonic-gate 	cvcd_pfd[2].events = POLLIN|POLLPRI;
3927c478bd9Sstevel@tonic-gate 	if ((event = poll(&cvcd_pfd[2], 1, -1)) == -1) {
3937c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "poll: %s", strerror(errno));
3947c478bd9Sstevel@tonic-gate 			exit(1);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * cvcd_connect sets global
3987c478bd9Sstevel@tonic-gate 	 * connected = 1 if successful.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	cvcd_connect(cvcd_ssp, cvcd_pfd);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Now set up the Network Console redirection driver.
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	rconsfd = open(CVCREDIR_DEV, O_RDWR|O_NDELAY);
4067c478bd9Sstevel@tonic-gate 	if (rconsfd < 0) {
4077c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "open: %s", strerror(errno));
4087c478bd9Sstevel@tonic-gate 		exit(1);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * cvcd_pfd holds three file descriptors we need to poll from:
4137c478bd9Sstevel@tonic-gate 	 * 0 will be connected to in_cvcd;  1 is the CVC Redirection driver;
4147c478bd9Sstevel@tonic-gate 	 * and 2 is the listen endpoint for new connections.
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	cvcd_pfd[1].fd = rconsfd;
4177c478bd9Sstevel@tonic-gate 	cvcd_pfd[1].events = POLLIN;
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 *  Loop through main service routine.  We check for inbound in.cvcd
4207c478bd9Sstevel@tonic-gate 	 * connection and data xfer between host and in.cvcd.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	for (;;) {
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		char	buf[MAXPKTSZ];
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		/*
4277c478bd9Sstevel@tonic-gate 		 * Check for in_cvcd connect requests.
4287c478bd9Sstevel@tonic-gate 		 */
4297c478bd9Sstevel@tonic-gate 		switch ((event = t_look(cvcd_ssp))) {
4307c478bd9Sstevel@tonic-gate 			case -1 :
4317c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_ERR, "%s", t_errlist[t_errno]);
4327c478bd9Sstevel@tonic-gate 				exit(1);
4337c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
4347c478bd9Sstevel@tonic-gate 				break;
4357c478bd9Sstevel@tonic-gate 			case 0  : /* Nothing to do */
4367c478bd9Sstevel@tonic-gate 				break;
4377c478bd9Sstevel@tonic-gate 			case T_LISTEN :
4387c478bd9Sstevel@tonic-gate 				if (connected == 1) {
4397c478bd9Sstevel@tonic-gate 					/*
4407c478bd9Sstevel@tonic-gate 					 * Someone already connected.
4417c478bd9Sstevel@tonic-gate 					 */
4427c478bd9Sstevel@tonic-gate 					cvcd_reject(cvcd_ssp);
4437c478bd9Sstevel@tonic-gate 				} else {
4447c478bd9Sstevel@tonic-gate 					/*
4457c478bd9Sstevel@tonic-gate 					 * cvcd_connect sets global
4467c478bd9Sstevel@tonic-gate 					 * connected = 1 if successful.
4477c478bd9Sstevel@tonic-gate 					 */
4487c478bd9Sstevel@tonic-gate 					cvcd_connect(cvcd_ssp, cvcd_pfd);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 					/*
4517c478bd9Sstevel@tonic-gate 					 * Re-open the cvcredir driver if
4527c478bd9Sstevel@tonic-gate 					 * the netcon_fail is true.  This
4537c478bd9Sstevel@tonic-gate 					 * indicates there was a previous
4547c478bd9Sstevel@tonic-gate 					 * network connection that got closed.
4557c478bd9Sstevel@tonic-gate 					 */
4567c478bd9Sstevel@tonic-gate 					if (netcon_fail) {
4577c478bd9Sstevel@tonic-gate 						rconsfd = open(CVCREDIR_DEV,
4587c478bd9Sstevel@tonic-gate 							O_RDWR|O_NDELAY);
4597c478bd9Sstevel@tonic-gate 						if (rconsfd < 0) {
4607c478bd9Sstevel@tonic-gate 							cvcd_err(LOG_ERR,
4617c478bd9Sstevel@tonic-gate 							"open: %s",
4627c478bd9Sstevel@tonic-gate 							strerror(errno));
4637c478bd9Sstevel@tonic-gate 							exit(1);
4647c478bd9Sstevel@tonic-gate 						}
4657c478bd9Sstevel@tonic-gate 						cvcd_pfd[1].fd = rconsfd;
4667c478bd9Sstevel@tonic-gate 						cvcd_pfd[1].events = POLLIN;
4677c478bd9Sstevel@tonic-gate 						netcon_fail = 0;
4687c478bd9Sstevel@tonic-gate 					}
4697c478bd9Sstevel@tonic-gate 				}
4707c478bd9Sstevel@tonic-gate 				break;
4717c478bd9Sstevel@tonic-gate 			default :
4727c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_ERR,
4737c478bd9Sstevel@tonic-gate 					"Illegal event %d for cvcd_ssp", event);
4747c478bd9Sstevel@tonic-gate 				exit(1);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		/*
4777c478bd9Sstevel@tonic-gate 		 * Take a look for console I/O or connect request.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		if ((event = poll(cvcd_pfd, 3, -1)) == -1) {
4807c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "poll: %s", strerror(errno));
4817c478bd9Sstevel@tonic-gate 			exit(1);
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		/*
4857c478bd9Sstevel@tonic-gate 		 * The following for loop is to detect any bad
4867c478bd9Sstevel@tonic-gate 		 * things(ie hangup,errors,invalid fd) have happened
4877c478bd9Sstevel@tonic-gate 		 * to the file descriptors we're interested in.
4887c478bd9Sstevel@tonic-gate 		 * If so, disconnect current network console connection.
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		for (j = 0; j < 3; j++) {
4917c478bd9Sstevel@tonic-gate 			if (cvcd_pfd[j].revents & (POLLERR|POLLHUP|POLLNVAL)) {
4927c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_WARNING,
4937c478bd9Sstevel@tonic-gate 					"poll: status on %s fd:%s%s%s",
4947c478bd9Sstevel@tonic-gate 					((j == 2) ? "listen" :
4957c478bd9Sstevel@tonic-gate 					((j == 0) ? "network" : "redir")),
4967c478bd9Sstevel@tonic-gate 					(cvcd_pfd[j].revents & POLLERR) ?
4977c478bd9Sstevel@tonic-gate 						" error" : "",
4987c478bd9Sstevel@tonic-gate 					(cvcd_pfd[j].revents & POLLHUP) ?
4997c478bd9Sstevel@tonic-gate 						" hangup" : "",
5007c478bd9Sstevel@tonic-gate 					(cvcd_pfd[j].revents & POLLNVAL) ?
5017c478bd9Sstevel@tonic-gate 						" bad fd" : "");
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 				(void) t_close(cvcd_pfd[0].fd);
5047c478bd9Sstevel@tonic-gate 				cvcd_pfd[0].fd = -1;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 				(void) close(cvcd_pfd[1].fd);
5077c478bd9Sstevel@tonic-gate 				cvcd_pfd[1].fd = -1;
5087c478bd9Sstevel@tonic-gate 				connected = 0;
5097c478bd9Sstevel@tonic-gate 				netcon_fail = 1;
5107c478bd9Sstevel@tonic-gate 				break;
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		/*
5157c478bd9Sstevel@tonic-gate 		 * Check if dummy netcon_fail flag is set, if set returns
5167c478bd9Sstevel@tonic-gate 		 * to the beginning of the main service routine.
5177c478bd9Sstevel@tonic-gate 		 */
5187c478bd9Sstevel@tonic-gate 		if (netcon_fail)
5197c478bd9Sstevel@tonic-gate 			continue;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		if (event != 0) {
5227c478bd9Sstevel@tonic-gate 			if (cvcd_pfd[0].revents == POLLIN) {
5237c478bd9Sstevel@tonic-gate 				/*
5247c478bd9Sstevel@tonic-gate 				 * Process cvcd_ssp data and commands.
5257c478bd9Sstevel@tonic-gate 				 */
5267c478bd9Sstevel@tonic-gate 				cvcd_read(cvcd_pfd);
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 			if (cvcd_pfd[1].revents == POLLIN) {
5297c478bd9Sstevel@tonic-gate 				int s;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 				if ((s = read(rconsfd, buf, MAXPKTSZ)) == -1) {
5327c478bd9Sstevel@tonic-gate 					cvcd_err(LOG_ERR, "read: %s",
5337c478bd9Sstevel@tonic-gate 						strerror(errno));
5347c478bd9Sstevel@tonic-gate 					exit(1);
5357c478bd9Sstevel@tonic-gate 				}
5367c478bd9Sstevel@tonic-gate 				if ((s > 0) && (connected == 1)) {
5377c478bd9Sstevel@tonic-gate 					if (write(cvcd_pfd[0].fd, buf, s) !=
5387c478bd9Sstevel@tonic-gate 					    s) {
5397c478bd9Sstevel@tonic-gate 						cvcd_err(LOG_ERR,
5407c478bd9Sstevel@tonic-gate 							"lost data output");
5417c478bd9Sstevel@tonic-gate 					}
5427c478bd9Sstevel@tonic-gate 				}
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	} /* End forever loop */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate #ifdef lint
5487c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5497c478bd9Sstevel@tonic-gate 	return (1);
5507c478bd9Sstevel@tonic-gate #endif /* lint */
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static void
5547c478bd9Sstevel@tonic-gate cvcd_reject(int fd)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	struct t_call		*tcall;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	tcall = (struct t_call *)t_alloc(fd, T_CALL, T_ALL);
5597c478bd9Sstevel@tonic-gate 	if (tcall == (struct t_call *)NULL) {
5607c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_reject: t_alloc: %s",
5617c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
5627c478bd9Sstevel@tonic-gate 		return;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	if (t_listen(fd, tcall) == -1) {
5657c478bd9Sstevel@tonic-gate 		if (t_errno == TNODATA) {
5667c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "cvcd_reject: No client data!");
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_reject: t_listen: %s",
5697c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
5707c478bd9Sstevel@tonic-gate 		t_free((char *)tcall, T_CALL);
5717c478bd9Sstevel@tonic-gate 		return;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	if (t_snddis(fd, tcall) < 0) {
5747c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_reject: t_snddis: %s",
5757c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	t_free((char *)tcall, T_CALL);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate static void
5817c478bd9Sstevel@tonic-gate cvcd_connect(int fd, struct pollfd *pfd)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	struct t_call		*tcall;
5847c478bd9Sstevel@tonic-gate 	int			newfd;
5857c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*peer;
5867c478bd9Sstevel@tonic-gate 	int			badpeer = 1;
5877c478bd9Sstevel@tonic-gate 	struct hostent		*he;
5887c478bd9Sstevel@tonic-gate 	struct netbuf		netbuf;
5897c478bd9Sstevel@tonic-gate 	char			addr[100];
5907c478bd9Sstevel@tonic-gate 	ulong_t			tmpaddr;	/* network byte order */
5917c478bd9Sstevel@tonic-gate 	char			**pp;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	tcall = (struct t_call *)t_alloc(fd, T_CALL, T_ALL);
5947c478bd9Sstevel@tonic-gate 	if (tcall == (struct t_call *)NULL) {
5957c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_connect: t_alloc: %s",
5967c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
5977c478bd9Sstevel@tonic-gate 		return;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	if (t_listen(fd, tcall) == -1) {
6007c478bd9Sstevel@tonic-gate 		if (t_errno == TNODATA) {
6017c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "cvcd_connect: No client data!");
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cnctip_connect: t_listen: %s",
6047c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
6057c478bd9Sstevel@tonic-gate 		t_free((char *)tcall, T_CALL);
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	if (pfd[0].fd != -1) {
6097c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_connect: no free file descriptors!");
6107c478bd9Sstevel@tonic-gate 		t_free((char *)tcall, T_CALL);
6117c478bd9Sstevel@tonic-gate 		return;
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 	newfd = t_open(TCP_DEV, O_RDWR|O_NDELAY, NULL);
6147c478bd9Sstevel@tonic-gate 	if (newfd == -1) {
6157c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_connect: t_open: %s",
6167c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
6177c478bd9Sstevel@tonic-gate 		t_free((char *)tcall, T_CALL);
6187c478bd9Sstevel@tonic-gate 		return;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	if (t_accept(fd, newfd, tcall) < 0) {
6217c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_connect: t_accept: %s",
6227c478bd9Sstevel@tonic-gate 			t_errlist[t_errno]);
6237c478bd9Sstevel@tonic-gate 		t_close(newfd);
6247c478bd9Sstevel@tonic-gate 		t_free((char *)tcall, T_CALL);
6257c478bd9Sstevel@tonic-gate 		return;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 	t_free((char *)tcall, T_CALL);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 *  If /etc/ssphostname doesnt exists, dont bother verifying
6317c478bd9Sstevel@tonic-gate 	 * peer since we cant do gethostbyname.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if (peercheck == 1) {
6347c478bd9Sstevel@tonic-gate 		he = gethostbyname(ssphostname);
6357c478bd9Sstevel@tonic-gate 		if (he == NULL) {
6367c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "gethostbyname: %s",
6377c478bd9Sstevel@tonic-gate 			    strerror(h_errno));
6387c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "unable to get SSP name %s!",
6397c478bd9Sstevel@tonic-gate 			    ssphostname);
6407c478bd9Sstevel@tonic-gate 			exit(1);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 *  Verify peer is from specified host by comparing the
6447c478bd9Sstevel@tonic-gate 		 *  address (in network byte order) of the TLI endpoint
6457c478bd9Sstevel@tonic-gate 		 *  and the address (in network byte order) of the ssp
6467c478bd9Sstevel@tonic-gate 		 *  (using the hostname found in /etc/ssphostname).
6477c478bd9Sstevel@tonic-gate 		 */
6487c478bd9Sstevel@tonic-gate 		(void) memset(addr, 0, 100);
6497c478bd9Sstevel@tonic-gate 		netbuf.buf = addr;
6507c478bd9Sstevel@tonic-gate 		netbuf.len = 0;
6517c478bd9Sstevel@tonic-gate 		netbuf.maxlen = 100;
6527c478bd9Sstevel@tonic-gate 		if (ioctl(newfd, TI_GETPEERNAME, &netbuf) < 0) {
6537c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "ioctl(TI_GETPEERNAME): %s",
6547c478bd9Sstevel@tonic-gate 			    strerror(errno));
6557c478bd9Sstevel@tonic-gate 			t_close(newfd);
6567c478bd9Sstevel@tonic-gate 			return;
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * cvcd doesn't check multi-homed ssphostname
6617c478bd9Sstevel@tonic-gate 		 * properly (only checks 1st address)
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		peer = (struct sockaddr_in *)addr;
6647c478bd9Sstevel@tonic-gate 		for (pp = he->h_addr_list; *pp != 0; pp++) {
6657c478bd9Sstevel@tonic-gate 			tmpaddr = htonl(*(ulong_t *)*pp);
6667c478bd9Sstevel@tonic-gate 			if (memcmp(&peer->sin_addr.s_addr, &tmpaddr,
6677c478bd9Sstevel@tonic-gate 				he->h_length) == 0) {
6687c478bd9Sstevel@tonic-gate 				badpeer = 0;
6697c478bd9Sstevel@tonic-gate 				break;
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (badpeer) {
6747c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, CONREF);
6757c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "remote host = %s.",
6767c478bd9Sstevel@tonic-gate 				inet_ntoa(peer->sin_addr));
6777c478bd9Sstevel@tonic-gate 			t_close(newfd);
6787c478bd9Sstevel@tonic-gate 			return;
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 	pfd[0].fd = newfd;
6827c478bd9Sstevel@tonic-gate 	pfd[0].events = POLLIN;
6837c478bd9Sstevel@tonic-gate 	connected = 1;
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  *  Read in data from client.
6887c478bd9Sstevel@tonic-gate  */
6897c478bd9Sstevel@tonic-gate static void
6907c478bd9Sstevel@tonic-gate cvcd_read(struct pollfd *pd)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	register char *data;
6937c478bd9Sstevel@tonic-gate 	register int fd = pd[0].fd;
6947c478bd9Sstevel@tonic-gate 	char	buf[MAXPKTSZ];
6957c478bd9Sstevel@tonic-gate 	int	flags = 0;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	data = buf;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (pd[0].revents & POLLIN) {
7007c478bd9Sstevel@tonic-gate 		int	n;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if ((n = t_rcv(fd, data, MAXPKTSZ, &flags)) == -1) {
7037c478bd9Sstevel@tonic-gate 			cvcd_err(LOG_ERR, "cvcd_read: t_rcv: %s",
7047c478bd9Sstevel@tonic-gate 				t_errlist[t_errno]);
7057c478bd9Sstevel@tonic-gate 			(void) t_close(pd[0].fd);
7067c478bd9Sstevel@tonic-gate 			pd[0].fd = -1;
7077c478bd9Sstevel@tonic-gate 			connected = 0;
7087c478bd9Sstevel@tonic-gate 			return;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 		if (flags & T_EXPEDITED) {
7117c478bd9Sstevel@tonic-gate 			if (n != 1) {
7127c478bd9Sstevel@tonic-gate 				cvcd_err(LOG_ERR,
7137c478bd9Sstevel@tonic-gate 					"cvcd_read: %d bytes EXD!!",
7147c478bd9Sstevel@tonic-gate 					n);
7157c478bd9Sstevel@tonic-gate 			}
7167c478bd9Sstevel@tonic-gate 			/*
7177c478bd9Sstevel@tonic-gate 			 * Deal with cvcd_ssp_commands.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			switch (data[n-1]) {
7207c478bd9Sstevel@tonic-gate 				case CVC_CONN_BREAK :
7217c478bd9Sstevel@tonic-gate 					cvcd_ioctl(rconsfd, CVC_BREAK);
7227c478bd9Sstevel@tonic-gate 					break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 				case CVC_CONN_DIS :
7257c478bd9Sstevel@tonic-gate 					(void) t_close(pd[0].fd);
7267c478bd9Sstevel@tonic-gate 					pd[0].fd = -1;
7277c478bd9Sstevel@tonic-gate 					cvcd_ioctl(rconsfd, CVC_DISCONNECT);
7287c478bd9Sstevel@tonic-gate 					connected = 0;
7297c478bd9Sstevel@tonic-gate 					break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 				case CVC_CONN_STAT :
7327c478bd9Sstevel@tonic-gate 					cvcd_status(fd);
7337c478bd9Sstevel@tonic-gate 					break;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 				default :
7367c478bd9Sstevel@tonic-gate 					cvcd_err(LOG_ERR,
7377c478bd9Sstevel@tonic-gate 						"Illegal cmd 0x%x", buf[n-1]);
7387c478bd9Sstevel@tonic-gate 					break;
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 		} else {
7417c478bd9Sstevel@tonic-gate 			if (((data[0] & 0377) == 0377) &&
7427c478bd9Sstevel@tonic-gate 			    ((data[1] & 0377) == 0377)) {
7437c478bd9Sstevel@tonic-gate 				/*
7447c478bd9Sstevel@tonic-gate 				 * Pass on window size changes (TIOCSWINSZ).
7457c478bd9Sstevel@tonic-gate 				 */
7467c478bd9Sstevel@tonic-gate 				cvcd_winch(rconsfd, data, n);
7477c478bd9Sstevel@tonic-gate 				(void) memset(data, 0, n);
7487c478bd9Sstevel@tonic-gate 			} else {
7497c478bd9Sstevel@tonic-gate 				cvcd_write(buf, n);
7507c478bd9Sstevel@tonic-gate 			}
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static void
7577c478bd9Sstevel@tonic-gate cvcd_ioctl(int fd, int flags)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	struct strioctl cmd;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	cmd.ic_cmd = flags;
7627c478bd9Sstevel@tonic-gate 	cmd.ic_timout = 0;
7637c478bd9Sstevel@tonic-gate 	cmd.ic_len = 0;
7647c478bd9Sstevel@tonic-gate 	cmd.ic_dp = NULL;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &cmd) == -1) {
7677c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_ioctl: %s", strerror(errno));
7687c478bd9Sstevel@tonic-gate 		exit(1);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /* ARGSUSED */
7747c478bd9Sstevel@tonic-gate static void
7757c478bd9Sstevel@tonic-gate cvcd_status(int fd)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate  * Write input to console - called from cvcd_read.
7827c478bd9Sstevel@tonic-gate  */
7837c478bd9Sstevel@tonic-gate static void
7847c478bd9Sstevel@tonic-gate cvcd_write(char *data, int size)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	int n;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if ((n = write(rconsfd, data, size)) == -1) {
7897c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_write: write: %s", strerror(errno));
7907c478bd9Sstevel@tonic-gate 		exit(1);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 	if (n != size) {
7937c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "cvcd_write: wrote %d of %d bytes", n, size);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static void
7987c478bd9Sstevel@tonic-gate usage()
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate #if defined(DEBUG)
8017c478bd9Sstevel@tonic-gate 	(void) printf("%s [-d] [-p port]\n", progname);
8027c478bd9Sstevel@tonic-gate #else
8037c478bd9Sstevel@tonic-gate 	(void) printf("%s -r [ssp host]\n", progname);
8047c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate  * cvcd_err ()
8097c478bd9Sstevel@tonic-gate  *
8107c478bd9Sstevel@tonic-gate  * Description:
8117c478bd9Sstevel@tonic-gate  * Log messages via syslog daemon.
8127c478bd9Sstevel@tonic-gate  *
8137c478bd9Sstevel@tonic-gate  * Input:
8147c478bd9Sstevel@tonic-gate  * code - logging code
8157c478bd9Sstevel@tonic-gate  * format - messages to log
8167c478bd9Sstevel@tonic-gate  *
8177c478bd9Sstevel@tonic-gate  * Output:
8187c478bd9Sstevel@tonic-gate  * void
8197c478bd9Sstevel@tonic-gate  *
8207c478bd9Sstevel@tonic-gate  */
8217c478bd9Sstevel@tonic-gate static void
8227c478bd9Sstevel@tonic-gate cvcd_err(int code, char *format, ...)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	va_list	varg_ptr;
8257c478bd9Sstevel@tonic-gate 	char	buf[MAXPKTSZ];
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	va_start(varg_ptr, format);
8287c478bd9Sstevel@tonic-gate 	(void) vsprintf(buf, format, varg_ptr);
8297c478bd9Sstevel@tonic-gate 	va_end(varg_ptr);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (debug == 0)
8327c478bd9Sstevel@tonic-gate 		syslog(code, buf);
8337c478bd9Sstevel@tonic-gate 	else
8347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", progname, buf);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate  * Handle a "control" request (signaled by magic being present)
8397c478bd9Sstevel@tonic-gate  * in the data stream.  For now, we are only willing to handle
8407c478bd9Sstevel@tonic-gate  * window size changes.
8417c478bd9Sstevel@tonic-gate  */
8427c478bd9Sstevel@tonic-gate void
8437c478bd9Sstevel@tonic-gate cvcd_winch(int pty, char *cp, int n)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	struct	winsize	w;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
8487c478bd9Sstevel@tonic-gate 		return;
8497c478bd9Sstevel@tonic-gate 	(void) memcpy(&w, cp + 4, sizeof (w));
8507c478bd9Sstevel@tonic-gate 	w.ws_row = ntohs(w.ws_row);
8517c478bd9Sstevel@tonic-gate 	w.ws_col = ntohs(w.ws_col);
8527c478bd9Sstevel@tonic-gate 	w.ws_xpixel = ntohs(w.ws_xpixel);
8537c478bd9Sstevel@tonic-gate 	w.ws_ypixel = ntohs(w.ws_ypixel);
8547c478bd9Sstevel@tonic-gate 	(void) ioctl(pty, TIOCSWINSZ, &w);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * Return class ID and maximum priority of it.
8607c478bd9Sstevel@tonic-gate  * Input:
8617c478bd9Sstevel@tonic-gate  *	name: is class name (either TS or RT).
8627c478bd9Sstevel@tonic-gate  *	maxpri: maximum priority for the class, returned in *maxpri.
8637c478bd9Sstevel@tonic-gate  * Output:
8647c478bd9Sstevel@tonic-gate  *	pc_cid: class ID
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate static id_t
8677c478bd9Sstevel@tonic-gate schedinfo(char *name, short *maxpri)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	pcinfo_t info;
8707c478bd9Sstevel@tonic-gate 	tsinfo_t *tsinfop;
8717c478bd9Sstevel@tonic-gate 	rtinfo_t *rtinfop;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	(void) strcpy(info.pc_clname, name);
8747c478bd9Sstevel@tonic-gate 	if (priocntl(0L, 0L, PC_GETCID, (caddr_t)&info) == -1L) {
8757c478bd9Sstevel@tonic-gate 		return (-1);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 	if (strcmp(name, "TS") == 0) {	/* Time Shared */
8787c478bd9Sstevel@tonic-gate 		tsinfop = (struct tsinfo *)info.pc_clinfo;
8797c478bd9Sstevel@tonic-gate 		*maxpri = tsinfop->ts_maxupri;
8807c478bd9Sstevel@tonic-gate 	} else if (strcmp(name, "RT") == 0) {	/* Real Time */
8817c478bd9Sstevel@tonic-gate 		rtinfop = (struct rtinfo *)info.pc_clinfo;
8827c478bd9Sstevel@tonic-gate 		*maxpri = rtinfop->rt_maxpri;
8837c478bd9Sstevel@tonic-gate 	} else {
8847c478bd9Sstevel@tonic-gate 		return (-1);
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	return (info.pc_cid);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * set the tli options for the given endpoint represented by fd
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate static void
8947c478bd9Sstevel@tonic-gate cvcd_setopt(int fd, int name)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	struct t_optmgmt	*topt, *tropt;
8977c478bd9Sstevel@tonic-gate 	struct opthdr		*sockopt;
8987c478bd9Sstevel@tonic-gate 	int			on = 1;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	topt = (struct t_optmgmt *)t_alloc(fd, T_OPTMGMT, 0);
9017c478bd9Sstevel@tonic-gate 	if (topt == NULL) {
9027c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "t_alloc: %s", t_errlist[t_errno]);
9037c478bd9Sstevel@tonic-gate 		exit(1);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 	tropt = (struct t_optmgmt *)t_alloc(fd, T_OPTMGMT, 0);
9067c478bd9Sstevel@tonic-gate 	if (tropt == NULL) {
9077c478bd9Sstevel@tonic-gate 		cvcd_err(LOG_ERR, "t_alloc: %s", t_errlist[t_errno]);
9087c478bd9Sstevel@tonic-gate 		exit(1);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 	topt->opt.buf = (char *)malloc(sizeof (struct opthdr) + sizeof (int));
9117c478bd9Sstevel@tonic-gate 	topt->opt.maxlen = 0;
9127c478bd9Sstevel@tonic-gate 	topt->opt.len = sizeof (struct opthdr) + sizeof (int);
9137c478bd9Sstevel@tonic-gate 	topt->flags = T_NEGOTIATE;
9147c478bd9Sstevel@tonic-gate 	sockopt = (struct opthdr *)topt->opt.buf;
9157c478bd9Sstevel@tonic-gate 	sockopt->level = SOL_SOCKET;
9167c478bd9Sstevel@tonic-gate 	sockopt->name = name;
9177c478bd9Sstevel@tonic-gate 	sockopt->len = sizeof (int);
9187c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)(topt->opt.buf + sizeof (struct opthdr)),
9197c478bd9Sstevel@tonic-gate 		(char *)&on, sizeof (on));
9207c478bd9Sstevel@tonic-gate 	tropt->opt.buf = (char *)malloc(sizeof (struct opthdr) + sizeof (int));
9217c478bd9Sstevel@tonic-gate 	tropt->opt.maxlen = sizeof (struct opthdr) + sizeof (int);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, topt, tropt) == -1) {
9247c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
9257c478bd9Sstevel@tonic-gate 		exit(1);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	t_free((char *)topt, T_OPTMGMT);
9297c478bd9Sstevel@tonic-gate 	t_free((char *)tropt, T_OPTMGMT);
9307c478bd9Sstevel@tonic-gate }
931