xref: /onnv-gate/usr/src/uts/common/ktli/t_kutil.c (revision 6990:d24af98bb8ea)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6990Sgd78059  * Common Development and Distribution License (the "License").
6*6990Sgd78059  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6990Sgd78059  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Contains the following utility functions:
430Sstevel@tonic-gate  * 	tli_send:
440Sstevel@tonic-gate  * 	tli_recv:
450Sstevel@tonic-gate  * 	get_ok_ack:
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Returns:
480Sstevel@tonic-gate  * 	See individual functions.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <sys/param.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/proc.h>
540Sstevel@tonic-gate #include <sys/file.h>
550Sstevel@tonic-gate #include <sys/user.h>
560Sstevel@tonic-gate #include <sys/vnode.h>
570Sstevel@tonic-gate #include <sys/errno.h>
580Sstevel@tonic-gate #include <sys/stream.h>
590Sstevel@tonic-gate #include <sys/ioctl.h>
600Sstevel@tonic-gate #include <sys/stropts.h>
610Sstevel@tonic-gate #include <sys/strsubr.h>
620Sstevel@tonic-gate #include <sys/tihdr.h>
630Sstevel@tonic-gate #include <sys/timod.h>
640Sstevel@tonic-gate #include <sys/tiuser.h>
650Sstevel@tonic-gate #include <sys/t_kuser.h>
66*6990Sgd78059 #include <sys/strsun.h>
670Sstevel@tonic-gate #include <inet/common.h>
680Sstevel@tonic-gate #include <inet/mi.h>
690Sstevel@tonic-gate #include <netinet/ip6.h>
700Sstevel@tonic-gate #include <inet/ip.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate extern int getiocseqno(void);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int
750Sstevel@tonic-gate tli_send(TIUSER *tiptr, mblk_t *bp, int fmode)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	vnode_t	*vp;
780Sstevel@tonic-gate 	int	error;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * Send data honoring flow control and errors
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	error = kstrputmsg(vp, bp, NULL, 0, 0, MSG_BAND | MSG_HOLDSIG, fmode);
860Sstevel@tonic-gate 	return (error);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate int
900Sstevel@tonic-gate tli_recv(TIUSER *tiptr, mblk_t **bp, int fmode)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	vnode_t		*vp;
930Sstevel@tonic-gate 	int		error;
940Sstevel@tonic-gate 	uchar_t 	pri;
950Sstevel@tonic-gate 	int 		pflag;
960Sstevel@tonic-gate 	rval_t		rval;
970Sstevel@tonic-gate 	clock_t		timout;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
1000Sstevel@tonic-gate 	if (fmode & (FNDELAY|FNONBLOCK))
1010Sstevel@tonic-gate 		timout = 0;
1020Sstevel@tonic-gate 	else
1030Sstevel@tonic-gate 		timout = -1;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	pflag = MSG_ANY;
1060Sstevel@tonic-gate 	pri = 0;
1070Sstevel@tonic-gate 	*bp = NULL;
1080Sstevel@tonic-gate 	error = kstrgetmsg(vp, bp, NULL, &pri, &pflag, timout, &rval);
1090Sstevel@tonic-gate 	if (error == ETIME)
1100Sstevel@tonic-gate 		error = EAGAIN;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	return (error);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int
1160Sstevel@tonic-gate get_ok_ack(TIUSER *tiptr, int type, int fmode)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int			msgsz;
1190Sstevel@tonic-gate 	union T_primitives	*pptr;
1200Sstevel@tonic-gate 	mblk_t			*bp;
1210Sstevel@tonic-gate 	int			error;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	error = 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * wait for ack
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	bp = NULL;
1290Sstevel@tonic-gate 	if ((error = tli_recv(tiptr, &bp, fmode)) != 0)
1300Sstevel@tonic-gate 		return (error);
1310Sstevel@tonic-gate 
132*6990Sgd78059 	if ((msgsz = (int)MBLKL(bp)) < sizeof (int)) {
1330Sstevel@tonic-gate 		freemsg(bp);
1340Sstevel@tonic-gate 		return (EPROTO);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
137*6990Sgd78059 	pptr = (void *)bp->b_rptr;
1380Sstevel@tonic-gate 	switch (pptr->type) {
1390Sstevel@tonic-gate 	case T_OK_ACK:
1400Sstevel@tonic-gate 		if (msgsz < TOKACKSZ || pptr->ok_ack.CORRECT_prim != type)
1410Sstevel@tonic-gate 			error = EPROTO;
1420Sstevel@tonic-gate 		break;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	case T_ERROR_ACK:
1450Sstevel@tonic-gate 		if (msgsz < TERRORACKSZ) {
1460Sstevel@tonic-gate 			error = EPROTO;
1470Sstevel@tonic-gate 			break;
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		if (pptr->error_ack.TLI_error == TSYSERR)
1510Sstevel@tonic-gate 			error = pptr->error_ack.UNIX_error;
1520Sstevel@tonic-gate 		else
1530Sstevel@tonic-gate 			error = t_tlitosyserr(pptr->error_ack.TLI_error);
1540Sstevel@tonic-gate 		break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	default:
1570Sstevel@tonic-gate 		error = EPROTO;
1580Sstevel@tonic-gate 		break;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	freemsg(bp);
1610Sstevel@tonic-gate 	return (error);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Translate a TLI error into a system error as best we can.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate static const int tli_errs[] = {
1680Sstevel@tonic-gate 	0,		/* no error	*/
1690Sstevel@tonic-gate 	EADDRNOTAVAIL,  /* TBADADDR	*/
1700Sstevel@tonic-gate 	ENOPROTOOPT,    /* TBADOPT	*/
1710Sstevel@tonic-gate 	EACCES,		/* TACCES	*/
1720Sstevel@tonic-gate 	EBADF,		/* TBADF	*/
1730Sstevel@tonic-gate 	EADDRNOTAVAIL,	/* TNOADDR	*/
1740Sstevel@tonic-gate 	EPROTO,		/* TOUTSTATE	*/
1750Sstevel@tonic-gate 	EPROTO,		/* TBADSEQ	*/
1760Sstevel@tonic-gate 	0,		/* TSYSERR - will never get */
1770Sstevel@tonic-gate 	EPROTO,		/* TLOOK - should never be sent by transport */
1780Sstevel@tonic-gate 	EMSGSIZE,	/* TBADDATA	*/
1790Sstevel@tonic-gate 	EMSGSIZE,	/* TBUFOVFLW	*/
1800Sstevel@tonic-gate 	EPROTO,		/* TFLOW	*/
1810Sstevel@tonic-gate 	EWOULDBLOCK,    /* TNODATA	*/
1820Sstevel@tonic-gate 	EPROTO,		/* TNODIS	*/
1830Sstevel@tonic-gate 	EPROTO,		/* TNOUDERR	*/
1840Sstevel@tonic-gate 	EINVAL,		/* TBADFLAG	*/
1850Sstevel@tonic-gate 	EPROTO,		/* TNOREL	*/
1860Sstevel@tonic-gate 	EOPNOTSUPP,	/* TNOTSUPPORT	*/
1870Sstevel@tonic-gate 	EPROTO,		/* TSTATECHNG	*/
1880Sstevel@tonic-gate };
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
1910Sstevel@tonic-gate t_tlitosyserr(int terr)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	if (terr < 0 || (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0]))))
1940Sstevel@tonic-gate 		return (EPROTO);
1950Sstevel@tonic-gate 	else
1960Sstevel@tonic-gate 		return (tli_errs[terr]);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * Notify transport that we are having trouble with this connection.
2010Sstevel@tonic-gate  * If transport is TCP/IP, IP should delete the IRE and start over.
2020Sstevel@tonic-gate  */
2030Sstevel@tonic-gate void
2040Sstevel@tonic-gate t_kadvise(TIUSER *tiptr, uchar_t *addr, int addr_len)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	file_t		*fp;
2070Sstevel@tonic-gate 	vnode_t		*vp;
2080Sstevel@tonic-gate 	struct iocblk	*iocp;
2090Sstevel@tonic-gate 	ipid_t		*ipid;
2100Sstevel@tonic-gate 	mblk_t		*mp;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	fp = tiptr->fp;
2130Sstevel@tonic-gate 	vp = fp->f_vnode;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	mp = mkiocb(IP_IOCTL);
2160Sstevel@tonic-gate 	if (!mp)
2170Sstevel@tonic-gate 		return;
2180Sstevel@tonic-gate 
219*6990Sgd78059 	iocp = (void *)mp->b_rptr;
2200Sstevel@tonic-gate 	iocp->ioc_count = sizeof (ipid_t) + addr_len;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	mp->b_cont = allocb(iocp->ioc_count, BPRI_HI);
2230Sstevel@tonic-gate 	if (!mp->b_cont) {
2240Sstevel@tonic-gate 		freeb(mp);
2250Sstevel@tonic-gate 		return;
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
228*6990Sgd78059 	ipid = (void *)mp->b_cont->b_rptr;
2290Sstevel@tonic-gate 	mp->b_cont->b_wptr += iocp->ioc_count;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	bzero(ipid, sizeof (*ipid));
2320Sstevel@tonic-gate 	ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY;
2330Sstevel@tonic-gate 	ipid->ipid_ire_type = IRE_CACHE;
2340Sstevel@tonic-gate 	ipid->ipid_addr_offset = sizeof (ipid_t);
2350Sstevel@tonic-gate 	ipid->ipid_addr_length = addr_len;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	bcopy(addr, &ipid[1], addr_len);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* Ignore flow control, signals and errors */
2400Sstevel@tonic-gate 	(void) kstrputmsg(vp, mp, NULL, 0, 0,
2410Sstevel@tonic-gate 	    MSG_BAND | MSG_IGNFLOW | MSG_HOLDSIG | MSG_IGNERROR, 0);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate #ifdef KTLIDEBUG
2450Sstevel@tonic-gate int ktlilog = 0;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Kernel level debugging aid. The global variable "ktlilog" is a bit
2490Sstevel@tonic-gate  * mask which allows various types of debugging messages to be printed
2500Sstevel@tonic-gate  * out.
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  *	ktlilog & 1 	will cause actual failures to be printed.
2530Sstevel@tonic-gate  *	ktlilog & 2	will cause informational messages to be
2540Sstevel@tonic-gate  *			printed.
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate int
2570Sstevel@tonic-gate ktli_log(int level, char *str, int a1)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	if (level & ktlilog)
2600Sstevel@tonic-gate 		printf(str, a1);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate #endif
263