xref: /titanic_52/usr/src/uts/common/ktli/t_kutil.c (revision bd670b35a010421b6e1a5536c34453a827007c81)
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
522eb7cb5Sgd78059  * Common Development and Distribution License (the "License").
622eb7cb5Sgd78059  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*bd670b35SErik Nordmark  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Contains the following utility functions:
417c478bd9Sstevel@tonic-gate  * 	tli_send:
427c478bd9Sstevel@tonic-gate  * 	tli_recv:
437c478bd9Sstevel@tonic-gate  * 	get_ok_ack:
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Returns:
467c478bd9Sstevel@tonic-gate  * 	See individual functions.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/file.h>
537c478bd9Sstevel@tonic-gate #include <sys/user.h>
547c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
557c478bd9Sstevel@tonic-gate #include <sys/errno.h>
567c478bd9Sstevel@tonic-gate #include <sys/stream.h>
577c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
597c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
607c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
617c478bd9Sstevel@tonic-gate #include <sys/timod.h>
627c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
637c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
6422eb7cb5Sgd78059 #include <sys/strsun.h>
657c478bd9Sstevel@tonic-gate #include <inet/common.h>
667c478bd9Sstevel@tonic-gate #include <inet/mi.h>
677c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
687c478bd9Sstevel@tonic-gate #include <inet/ip.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate extern int getiocseqno(void);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int
737c478bd9Sstevel@tonic-gate tli_send(TIUSER *tiptr, mblk_t *bp, int fmode)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	vnode_t	*vp;
767c478bd9Sstevel@tonic-gate 	int	error;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * Send data honoring flow control and errors
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	error = kstrputmsg(vp, bp, NULL, 0, 0, MSG_BAND | MSG_HOLDSIG, fmode);
847c478bd9Sstevel@tonic-gate 	return (error);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate int
887c478bd9Sstevel@tonic-gate tli_recv(TIUSER *tiptr, mblk_t **bp, int fmode)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
917c478bd9Sstevel@tonic-gate 	int		error;
927c478bd9Sstevel@tonic-gate 	uchar_t 	pri;
937c478bd9Sstevel@tonic-gate 	int 		pflag;
947c478bd9Sstevel@tonic-gate 	rval_t		rval;
957c478bd9Sstevel@tonic-gate 	clock_t		timout;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
987c478bd9Sstevel@tonic-gate 	if (fmode & (FNDELAY|FNONBLOCK))
997c478bd9Sstevel@tonic-gate 		timout = 0;
1007c478bd9Sstevel@tonic-gate 	else
1017c478bd9Sstevel@tonic-gate 		timout = -1;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	pflag = MSG_ANY;
1047c478bd9Sstevel@tonic-gate 	pri = 0;
1057c478bd9Sstevel@tonic-gate 	*bp = NULL;
1067c478bd9Sstevel@tonic-gate 	error = kstrgetmsg(vp, bp, NULL, &pri, &pflag, timout, &rval);
1077c478bd9Sstevel@tonic-gate 	if (error == ETIME)
1087c478bd9Sstevel@tonic-gate 		error = EAGAIN;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (error);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
1147c478bd9Sstevel@tonic-gate get_ok_ack(TIUSER *tiptr, int type, int fmode)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int			msgsz;
1177c478bd9Sstevel@tonic-gate 	union T_primitives	*pptr;
1187c478bd9Sstevel@tonic-gate 	mblk_t			*bp;
1197c478bd9Sstevel@tonic-gate 	int			error;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	error = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * wait for ack
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	bp = NULL;
1277c478bd9Sstevel@tonic-gate 	if ((error = tli_recv(tiptr, &bp, fmode)) != 0)
1287c478bd9Sstevel@tonic-gate 		return (error);
1297c478bd9Sstevel@tonic-gate 
13022eb7cb5Sgd78059 	if ((msgsz = (int)MBLKL(bp)) < sizeof (int)) {
1317c478bd9Sstevel@tonic-gate 		freemsg(bp);
1327c478bd9Sstevel@tonic-gate 		return (EPROTO);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
13522eb7cb5Sgd78059 	pptr = (void *)bp->b_rptr;
1367c478bd9Sstevel@tonic-gate 	switch (pptr->type) {
1377c478bd9Sstevel@tonic-gate 	case T_OK_ACK:
1387c478bd9Sstevel@tonic-gate 		if (msgsz < TOKACKSZ || pptr->ok_ack.CORRECT_prim != type)
1397c478bd9Sstevel@tonic-gate 			error = EPROTO;
1407c478bd9Sstevel@tonic-gate 		break;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	case T_ERROR_ACK:
1437c478bd9Sstevel@tonic-gate 		if (msgsz < TERRORACKSZ) {
1447c478bd9Sstevel@tonic-gate 			error = EPROTO;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		if (pptr->error_ack.TLI_error == TSYSERR)
1497c478bd9Sstevel@tonic-gate 			error = pptr->error_ack.UNIX_error;
1507c478bd9Sstevel@tonic-gate 		else
1517c478bd9Sstevel@tonic-gate 			error = t_tlitosyserr(pptr->error_ack.TLI_error);
1527c478bd9Sstevel@tonic-gate 		break;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	default:
1557c478bd9Sstevel@tonic-gate 		error = EPROTO;
1567c478bd9Sstevel@tonic-gate 		break;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	freemsg(bp);
1597c478bd9Sstevel@tonic-gate 	return (error);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Translate a TLI error into a system error as best we can.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static const int tli_errs[] = {
1667c478bd9Sstevel@tonic-gate 	0,		/* no error	*/
1677c478bd9Sstevel@tonic-gate 	EADDRNOTAVAIL,  /* TBADADDR	*/
1687c478bd9Sstevel@tonic-gate 	ENOPROTOOPT,    /* TBADOPT	*/
1697c478bd9Sstevel@tonic-gate 	EACCES,		/* TACCES	*/
1707c478bd9Sstevel@tonic-gate 	EBADF,		/* TBADF	*/
1717c478bd9Sstevel@tonic-gate 	EADDRNOTAVAIL,	/* TNOADDR	*/
1727c478bd9Sstevel@tonic-gate 	EPROTO,		/* TOUTSTATE	*/
1737c478bd9Sstevel@tonic-gate 	EPROTO,		/* TBADSEQ	*/
1747c478bd9Sstevel@tonic-gate 	0,		/* TSYSERR - will never get */
1757c478bd9Sstevel@tonic-gate 	EPROTO,		/* TLOOK - should never be sent by transport */
1767c478bd9Sstevel@tonic-gate 	EMSGSIZE,	/* TBADDATA	*/
1777c478bd9Sstevel@tonic-gate 	EMSGSIZE,	/* TBUFOVFLW	*/
1787c478bd9Sstevel@tonic-gate 	EPROTO,		/* TFLOW	*/
1797c478bd9Sstevel@tonic-gate 	EWOULDBLOCK,    /* TNODATA	*/
1807c478bd9Sstevel@tonic-gate 	EPROTO,		/* TNODIS	*/
1817c478bd9Sstevel@tonic-gate 	EPROTO,		/* TNOUDERR	*/
1827c478bd9Sstevel@tonic-gate 	EINVAL,		/* TBADFLAG	*/
1837c478bd9Sstevel@tonic-gate 	EPROTO,		/* TNOREL	*/
1847c478bd9Sstevel@tonic-gate 	EOPNOTSUPP,	/* TNOTSUPPORT	*/
1857c478bd9Sstevel@tonic-gate 	EPROTO,		/* TSTATECHNG	*/
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
1897c478bd9Sstevel@tonic-gate t_tlitosyserr(int terr)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	if (terr < 0 || (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0]))))
1927c478bd9Sstevel@tonic-gate 		return (EPROTO);
1937c478bd9Sstevel@tonic-gate 	else
1947c478bd9Sstevel@tonic-gate 		return (tli_errs[terr]);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Notify transport that we are having trouble with this connection.
1997c478bd9Sstevel@tonic-gate  * If transport is TCP/IP, IP should delete the IRE and start over.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate void
2027c478bd9Sstevel@tonic-gate t_kadvise(TIUSER *tiptr, uchar_t *addr, int addr_len)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	file_t		*fp;
2057c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
2067c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
2077c478bd9Sstevel@tonic-gate 	ipid_t		*ipid;
2087c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	fp = tiptr->fp;
2117c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	mp = mkiocb(IP_IOCTL);
2147c478bd9Sstevel@tonic-gate 	if (!mp)
2157c478bd9Sstevel@tonic-gate 		return;
2167c478bd9Sstevel@tonic-gate 
21722eb7cb5Sgd78059 	iocp = (void *)mp->b_rptr;
2187c478bd9Sstevel@tonic-gate 	iocp->ioc_count = sizeof (ipid_t) + addr_len;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	mp->b_cont = allocb(iocp->ioc_count, BPRI_HI);
2217c478bd9Sstevel@tonic-gate 	if (!mp->b_cont) {
2227c478bd9Sstevel@tonic-gate 		freeb(mp);
2237c478bd9Sstevel@tonic-gate 		return;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
22622eb7cb5Sgd78059 	ipid = (void *)mp->b_cont->b_rptr;
2277c478bd9Sstevel@tonic-gate 	mp->b_cont->b_wptr += iocp->ioc_count;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	bzero(ipid, sizeof (*ipid));
2307c478bd9Sstevel@tonic-gate 	ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY;
231*bd670b35SErik Nordmark 	ipid->ipid_ire_type = 0;
2327c478bd9Sstevel@tonic-gate 	ipid->ipid_addr_offset = sizeof (ipid_t);
2337c478bd9Sstevel@tonic-gate 	ipid->ipid_addr_length = addr_len;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	bcopy(addr, &ipid[1], addr_len);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* Ignore flow control, signals and errors */
2387c478bd9Sstevel@tonic-gate 	(void) kstrputmsg(vp, mp, NULL, 0, 0,
2397c478bd9Sstevel@tonic-gate 	    MSG_BAND | MSG_IGNFLOW | MSG_HOLDSIG | MSG_IGNERROR, 0);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #ifdef KTLIDEBUG
2437c478bd9Sstevel@tonic-gate int ktlilog = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * Kernel level debugging aid. The global variable "ktlilog" is a bit
2477c478bd9Sstevel@tonic-gate  * mask which allows various types of debugging messages to be printed
2487c478bd9Sstevel@tonic-gate  * out.
2497c478bd9Sstevel@tonic-gate  *
2507c478bd9Sstevel@tonic-gate  *	ktlilog & 1 	will cause actual failures to be printed.
2517c478bd9Sstevel@tonic-gate  *	ktlilog & 2	will cause informational messages to be
2527c478bd9Sstevel@tonic-gate  *			printed.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate int
2557c478bd9Sstevel@tonic-gate ktli_log(int level, char *str, int a1)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	if (level & ktlilog)
2587c478bd9Sstevel@tonic-gate 		printf(str, a1);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate #endif
261