xref: /onnv-gate/usr/src/uts/common/ktli/t_kutil.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Contains the following utility functions:
44*0Sstevel@tonic-gate  * 	tli_send:
45*0Sstevel@tonic-gate  * 	tli_recv:
46*0Sstevel@tonic-gate  * 	get_ok_ack:
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  * Returns:
49*0Sstevel@tonic-gate  * 	See individual functions.
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include <sys/param.h>
53*0Sstevel@tonic-gate #include <sys/types.h>
54*0Sstevel@tonic-gate #include <sys/proc.h>
55*0Sstevel@tonic-gate #include <sys/file.h>
56*0Sstevel@tonic-gate #include <sys/user.h>
57*0Sstevel@tonic-gate #include <sys/vnode.h>
58*0Sstevel@tonic-gate #include <sys/errno.h>
59*0Sstevel@tonic-gate #include <sys/stream.h>
60*0Sstevel@tonic-gate #include <sys/ioctl.h>
61*0Sstevel@tonic-gate #include <sys/stropts.h>
62*0Sstevel@tonic-gate #include <sys/strsubr.h>
63*0Sstevel@tonic-gate #include <sys/tihdr.h>
64*0Sstevel@tonic-gate #include <sys/timod.h>
65*0Sstevel@tonic-gate #include <sys/tiuser.h>
66*0Sstevel@tonic-gate #include <sys/t_kuser.h>
67*0Sstevel@tonic-gate #include <inet/common.h>
68*0Sstevel@tonic-gate #include <inet/mi.h>
69*0Sstevel@tonic-gate #include <netinet/ip6.h>
70*0Sstevel@tonic-gate #include <inet/ip.h>
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate extern int getiocseqno(void);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate int
75*0Sstevel@tonic-gate tli_send(TIUSER *tiptr, mblk_t *bp, int fmode)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	vnode_t	*vp;
78*0Sstevel@tonic-gate 	int	error;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/*
83*0Sstevel@tonic-gate 	 * Send data honoring flow control and errors
84*0Sstevel@tonic-gate 	 */
85*0Sstevel@tonic-gate 	error = kstrputmsg(vp, bp, NULL, 0, 0, MSG_BAND | MSG_HOLDSIG, fmode);
86*0Sstevel@tonic-gate 	return (error);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate int
90*0Sstevel@tonic-gate tli_recv(TIUSER *tiptr, mblk_t **bp, int fmode)
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate 	vnode_t		*vp;
93*0Sstevel@tonic-gate 	int		error;
94*0Sstevel@tonic-gate 	uchar_t 	pri;
95*0Sstevel@tonic-gate 	int 		pflag;
96*0Sstevel@tonic-gate 	rval_t		rval;
97*0Sstevel@tonic-gate 	clock_t		timout;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	vp = tiptr->fp->f_vnode;
100*0Sstevel@tonic-gate 	if (fmode & (FNDELAY|FNONBLOCK))
101*0Sstevel@tonic-gate 		timout = 0;
102*0Sstevel@tonic-gate 	else
103*0Sstevel@tonic-gate 		timout = -1;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	pflag = MSG_ANY;
106*0Sstevel@tonic-gate 	pri = 0;
107*0Sstevel@tonic-gate 	*bp = NULL;
108*0Sstevel@tonic-gate 	error = kstrgetmsg(vp, bp, NULL, &pri, &pflag, timout, &rval);
109*0Sstevel@tonic-gate 	if (error == ETIME)
110*0Sstevel@tonic-gate 		error = EAGAIN;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	return (error);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate int
116*0Sstevel@tonic-gate get_ok_ack(TIUSER *tiptr, int type, int fmode)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	int			msgsz;
119*0Sstevel@tonic-gate 	union T_primitives	*pptr;
120*0Sstevel@tonic-gate 	mblk_t			*bp;
121*0Sstevel@tonic-gate 	int			error;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	error = 0;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/*
126*0Sstevel@tonic-gate 	 * wait for ack
127*0Sstevel@tonic-gate 	 */
128*0Sstevel@tonic-gate 	bp = NULL;
129*0Sstevel@tonic-gate 	if ((error = tli_recv(tiptr, &bp, fmode)) != 0)
130*0Sstevel@tonic-gate 		return (error);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ((msgsz = (int)(bp->b_wptr - bp->b_rptr)) < sizeof (int)) {
133*0Sstevel@tonic-gate 		freemsg(bp);
134*0Sstevel@tonic-gate 		return (EPROTO);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	pptr = (union T_primitives *)bp->b_rptr;
138*0Sstevel@tonic-gate 	switch (pptr->type) {
139*0Sstevel@tonic-gate 	case T_OK_ACK:
140*0Sstevel@tonic-gate 		if (msgsz < TOKACKSZ || pptr->ok_ack.CORRECT_prim != type)
141*0Sstevel@tonic-gate 			error = EPROTO;
142*0Sstevel@tonic-gate 		break;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	case T_ERROR_ACK:
145*0Sstevel@tonic-gate 		if (msgsz < TERRORACKSZ) {
146*0Sstevel@tonic-gate 			error = EPROTO;
147*0Sstevel@tonic-gate 			break;
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		if (pptr->error_ack.TLI_error == TSYSERR)
151*0Sstevel@tonic-gate 			error = pptr->error_ack.UNIX_error;
152*0Sstevel@tonic-gate 		else
153*0Sstevel@tonic-gate 			error = t_tlitosyserr(pptr->error_ack.TLI_error);
154*0Sstevel@tonic-gate 		break;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	default:
157*0Sstevel@tonic-gate 		error = EPROTO;
158*0Sstevel@tonic-gate 		break;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	freemsg(bp);
161*0Sstevel@tonic-gate 	return (error);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Translate a TLI error into a system error as best we can.
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate static const int tli_errs[] = {
168*0Sstevel@tonic-gate 	0,		/* no error	*/
169*0Sstevel@tonic-gate 	EADDRNOTAVAIL,  /* TBADADDR	*/
170*0Sstevel@tonic-gate 	ENOPROTOOPT,    /* TBADOPT	*/
171*0Sstevel@tonic-gate 	EACCES,		/* TACCES	*/
172*0Sstevel@tonic-gate 	EBADF,		/* TBADF	*/
173*0Sstevel@tonic-gate 	EADDRNOTAVAIL,	/* TNOADDR	*/
174*0Sstevel@tonic-gate 	EPROTO,		/* TOUTSTATE	*/
175*0Sstevel@tonic-gate 	EPROTO,		/* TBADSEQ	*/
176*0Sstevel@tonic-gate 	0,		/* TSYSERR - will never get */
177*0Sstevel@tonic-gate 	EPROTO,		/* TLOOK - should never be sent by transport */
178*0Sstevel@tonic-gate 	EMSGSIZE,	/* TBADDATA	*/
179*0Sstevel@tonic-gate 	EMSGSIZE,	/* TBUFOVFLW	*/
180*0Sstevel@tonic-gate 	EPROTO,		/* TFLOW	*/
181*0Sstevel@tonic-gate 	EWOULDBLOCK,    /* TNODATA	*/
182*0Sstevel@tonic-gate 	EPROTO,		/* TNODIS	*/
183*0Sstevel@tonic-gate 	EPROTO,		/* TNOUDERR	*/
184*0Sstevel@tonic-gate 	EINVAL,		/* TBADFLAG	*/
185*0Sstevel@tonic-gate 	EPROTO,		/* TNOREL	*/
186*0Sstevel@tonic-gate 	EOPNOTSUPP,	/* TNOTSUPPORT	*/
187*0Sstevel@tonic-gate 	EPROTO,		/* TSTATECHNG	*/
188*0Sstevel@tonic-gate };
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate int
191*0Sstevel@tonic-gate t_tlitosyserr(int terr)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	if (terr < 0 || (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0]))))
194*0Sstevel@tonic-gate 		return (EPROTO);
195*0Sstevel@tonic-gate 	else
196*0Sstevel@tonic-gate 		return (tli_errs[terr]);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate /*
200*0Sstevel@tonic-gate  * Notify transport that we are having trouble with this connection.
201*0Sstevel@tonic-gate  * If transport is TCP/IP, IP should delete the IRE and start over.
202*0Sstevel@tonic-gate  */
203*0Sstevel@tonic-gate void
204*0Sstevel@tonic-gate t_kadvise(TIUSER *tiptr, uchar_t *addr, int addr_len)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate 	file_t		*fp;
207*0Sstevel@tonic-gate 	vnode_t		*vp;
208*0Sstevel@tonic-gate 	struct iocblk	*iocp;
209*0Sstevel@tonic-gate 	ipid_t		*ipid;
210*0Sstevel@tonic-gate 	mblk_t		*mp;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	fp = tiptr->fp;
213*0Sstevel@tonic-gate 	vp = fp->f_vnode;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	mp = mkiocb(IP_IOCTL);
216*0Sstevel@tonic-gate 	if (!mp)
217*0Sstevel@tonic-gate 		return;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
220*0Sstevel@tonic-gate 	iocp->ioc_count = sizeof (ipid_t) + addr_len;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	mp->b_cont = allocb(iocp->ioc_count, BPRI_HI);
223*0Sstevel@tonic-gate 	if (!mp->b_cont) {
224*0Sstevel@tonic-gate 		freeb(mp);
225*0Sstevel@tonic-gate 		return;
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	ipid = (ipid_t *)mp->b_cont->b_rptr;
229*0Sstevel@tonic-gate 	mp->b_cont->b_wptr += iocp->ioc_count;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	bzero(ipid, sizeof (*ipid));
232*0Sstevel@tonic-gate 	ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY;
233*0Sstevel@tonic-gate 	ipid->ipid_ire_type = IRE_CACHE;
234*0Sstevel@tonic-gate 	ipid->ipid_addr_offset = sizeof (ipid_t);
235*0Sstevel@tonic-gate 	ipid->ipid_addr_length = addr_len;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	bcopy(addr, &ipid[1], addr_len);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* Ignore flow control, signals and errors */
240*0Sstevel@tonic-gate 	(void) kstrputmsg(vp, mp, NULL, 0, 0,
241*0Sstevel@tonic-gate 	    MSG_BAND | MSG_IGNFLOW | MSG_HOLDSIG | MSG_IGNERROR, 0);
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate #ifdef KTLIDEBUG
245*0Sstevel@tonic-gate int ktlilog = 0;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Kernel level debugging aid. The global variable "ktlilog" is a bit
249*0Sstevel@tonic-gate  * mask which allows various types of debugging messages to be printed
250*0Sstevel@tonic-gate  * out.
251*0Sstevel@tonic-gate  *
252*0Sstevel@tonic-gate  *	ktlilog & 1 	will cause actual failures to be printed.
253*0Sstevel@tonic-gate  *	ktlilog & 2	will cause informational messages to be
254*0Sstevel@tonic-gate  *			printed.
255*0Sstevel@tonic-gate  */
256*0Sstevel@tonic-gate int
257*0Sstevel@tonic-gate ktli_log(int level, char *str, int a1)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	if (level & ktlilog)
260*0Sstevel@tonic-gate 		printf(str, a1);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate #endif
263