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
56990Sgd78059 * Common Development and Distribution License (the "License").
66990Sgd78059 * 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*11042SErik.Nordmark@Sun.COM * Copyright 2009 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 /*
400Sstevel@tonic-gate * Contains the following utility functions:
410Sstevel@tonic-gate * tli_send:
420Sstevel@tonic-gate * tli_recv:
430Sstevel@tonic-gate * get_ok_ack:
440Sstevel@tonic-gate *
450Sstevel@tonic-gate * Returns:
460Sstevel@tonic-gate * See individual functions.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include <sys/proc.h>
520Sstevel@tonic-gate #include <sys/file.h>
530Sstevel@tonic-gate #include <sys/user.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/errno.h>
560Sstevel@tonic-gate #include <sys/stream.h>
570Sstevel@tonic-gate #include <sys/ioctl.h>
580Sstevel@tonic-gate #include <sys/stropts.h>
590Sstevel@tonic-gate #include <sys/strsubr.h>
600Sstevel@tonic-gate #include <sys/tihdr.h>
610Sstevel@tonic-gate #include <sys/timod.h>
620Sstevel@tonic-gate #include <sys/tiuser.h>
630Sstevel@tonic-gate #include <sys/t_kuser.h>
646990Sgd78059 #include <sys/strsun.h>
650Sstevel@tonic-gate #include <inet/common.h>
660Sstevel@tonic-gate #include <inet/mi.h>
670Sstevel@tonic-gate #include <netinet/ip6.h>
680Sstevel@tonic-gate #include <inet/ip.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate extern int getiocseqno(void);
710Sstevel@tonic-gate
720Sstevel@tonic-gate int
tli_send(TIUSER * tiptr,mblk_t * bp,int fmode)730Sstevel@tonic-gate tli_send(TIUSER *tiptr, mblk_t *bp, int fmode)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate vnode_t *vp;
760Sstevel@tonic-gate int error;
770Sstevel@tonic-gate
780Sstevel@tonic-gate vp = tiptr->fp->f_vnode;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Send data honoring flow control and errors
820Sstevel@tonic-gate */
830Sstevel@tonic-gate error = kstrputmsg(vp, bp, NULL, 0, 0, MSG_BAND | MSG_HOLDSIG, fmode);
840Sstevel@tonic-gate return (error);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate int
tli_recv(TIUSER * tiptr,mblk_t ** bp,int fmode)880Sstevel@tonic-gate tli_recv(TIUSER *tiptr, mblk_t **bp, int fmode)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate vnode_t *vp;
910Sstevel@tonic-gate int error;
920Sstevel@tonic-gate uchar_t pri;
930Sstevel@tonic-gate int pflag;
940Sstevel@tonic-gate rval_t rval;
950Sstevel@tonic-gate clock_t timout;
960Sstevel@tonic-gate
970Sstevel@tonic-gate vp = tiptr->fp->f_vnode;
980Sstevel@tonic-gate if (fmode & (FNDELAY|FNONBLOCK))
990Sstevel@tonic-gate timout = 0;
1000Sstevel@tonic-gate else
1010Sstevel@tonic-gate timout = -1;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate pflag = MSG_ANY;
1040Sstevel@tonic-gate pri = 0;
1050Sstevel@tonic-gate *bp = NULL;
1060Sstevel@tonic-gate error = kstrgetmsg(vp, bp, NULL, &pri, &pflag, timout, &rval);
1070Sstevel@tonic-gate if (error == ETIME)
1080Sstevel@tonic-gate error = EAGAIN;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate return (error);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate int
get_ok_ack(TIUSER * tiptr,int type,int fmode)1140Sstevel@tonic-gate get_ok_ack(TIUSER *tiptr, int type, int fmode)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate int msgsz;
1170Sstevel@tonic-gate union T_primitives *pptr;
1180Sstevel@tonic-gate mblk_t *bp;
1190Sstevel@tonic-gate int error;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate error = 0;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * wait for ack
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate bp = NULL;
1270Sstevel@tonic-gate if ((error = tli_recv(tiptr, &bp, fmode)) != 0)
1280Sstevel@tonic-gate return (error);
1290Sstevel@tonic-gate
1306990Sgd78059 if ((msgsz = (int)MBLKL(bp)) < sizeof (int)) {
1310Sstevel@tonic-gate freemsg(bp);
1320Sstevel@tonic-gate return (EPROTO);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1356990Sgd78059 pptr = (void *)bp->b_rptr;
1360Sstevel@tonic-gate switch (pptr->type) {
1370Sstevel@tonic-gate case T_OK_ACK:
1380Sstevel@tonic-gate if (msgsz < TOKACKSZ || pptr->ok_ack.CORRECT_prim != type)
1390Sstevel@tonic-gate error = EPROTO;
1400Sstevel@tonic-gate break;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate case T_ERROR_ACK:
1430Sstevel@tonic-gate if (msgsz < TERRORACKSZ) {
1440Sstevel@tonic-gate error = EPROTO;
1450Sstevel@tonic-gate break;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (pptr->error_ack.TLI_error == TSYSERR)
1490Sstevel@tonic-gate error = pptr->error_ack.UNIX_error;
1500Sstevel@tonic-gate else
1510Sstevel@tonic-gate error = t_tlitosyserr(pptr->error_ack.TLI_error);
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate default:
1550Sstevel@tonic-gate error = EPROTO;
1560Sstevel@tonic-gate break;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate freemsg(bp);
1590Sstevel@tonic-gate return (error);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * Translate a TLI error into a system error as best we can.
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate static const int tli_errs[] = {
1660Sstevel@tonic-gate 0, /* no error */
1670Sstevel@tonic-gate EADDRNOTAVAIL, /* TBADADDR */
1680Sstevel@tonic-gate ENOPROTOOPT, /* TBADOPT */
1690Sstevel@tonic-gate EACCES, /* TACCES */
1700Sstevel@tonic-gate EBADF, /* TBADF */
1710Sstevel@tonic-gate EADDRNOTAVAIL, /* TNOADDR */
1720Sstevel@tonic-gate EPROTO, /* TOUTSTATE */
1730Sstevel@tonic-gate EPROTO, /* TBADSEQ */
1740Sstevel@tonic-gate 0, /* TSYSERR - will never get */
1750Sstevel@tonic-gate EPROTO, /* TLOOK - should never be sent by transport */
1760Sstevel@tonic-gate EMSGSIZE, /* TBADDATA */
1770Sstevel@tonic-gate EMSGSIZE, /* TBUFOVFLW */
1780Sstevel@tonic-gate EPROTO, /* TFLOW */
1790Sstevel@tonic-gate EWOULDBLOCK, /* TNODATA */
1800Sstevel@tonic-gate EPROTO, /* TNODIS */
1810Sstevel@tonic-gate EPROTO, /* TNOUDERR */
1820Sstevel@tonic-gate EINVAL, /* TBADFLAG */
1830Sstevel@tonic-gate EPROTO, /* TNOREL */
1840Sstevel@tonic-gate EOPNOTSUPP, /* TNOTSUPPORT */
1850Sstevel@tonic-gate EPROTO, /* TSTATECHNG */
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate int
t_tlitosyserr(int terr)1890Sstevel@tonic-gate t_tlitosyserr(int terr)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate if (terr < 0 || (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0]))))
1920Sstevel@tonic-gate return (EPROTO);
1930Sstevel@tonic-gate else
1940Sstevel@tonic-gate return (tli_errs[terr]);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate * Notify transport that we are having trouble with this connection.
1990Sstevel@tonic-gate * If transport is TCP/IP, IP should delete the IRE and start over.
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate void
t_kadvise(TIUSER * tiptr,uchar_t * addr,int addr_len)2020Sstevel@tonic-gate t_kadvise(TIUSER *tiptr, uchar_t *addr, int addr_len)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate file_t *fp;
2050Sstevel@tonic-gate vnode_t *vp;
2060Sstevel@tonic-gate struct iocblk *iocp;
2070Sstevel@tonic-gate ipid_t *ipid;
2080Sstevel@tonic-gate mblk_t *mp;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate fp = tiptr->fp;
2110Sstevel@tonic-gate vp = fp->f_vnode;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate mp = mkiocb(IP_IOCTL);
2140Sstevel@tonic-gate if (!mp)
2150Sstevel@tonic-gate return;
2160Sstevel@tonic-gate
2176990Sgd78059 iocp = (void *)mp->b_rptr;
2180Sstevel@tonic-gate iocp->ioc_count = sizeof (ipid_t) + addr_len;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate mp->b_cont = allocb(iocp->ioc_count, BPRI_HI);
2210Sstevel@tonic-gate if (!mp->b_cont) {
2220Sstevel@tonic-gate freeb(mp);
2230Sstevel@tonic-gate return;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2266990Sgd78059 ipid = (void *)mp->b_cont->b_rptr;
2270Sstevel@tonic-gate mp->b_cont->b_wptr += iocp->ioc_count;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate bzero(ipid, sizeof (*ipid));
2300Sstevel@tonic-gate ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY;
231*11042SErik.Nordmark@Sun.COM ipid->ipid_ire_type = 0;
2320Sstevel@tonic-gate ipid->ipid_addr_offset = sizeof (ipid_t);
2330Sstevel@tonic-gate ipid->ipid_addr_length = addr_len;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate bcopy(addr, &ipid[1], addr_len);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /* Ignore flow control, signals and errors */
2380Sstevel@tonic-gate (void) kstrputmsg(vp, mp, NULL, 0, 0,
2390Sstevel@tonic-gate MSG_BAND | MSG_IGNFLOW | MSG_HOLDSIG | MSG_IGNERROR, 0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate #ifdef KTLIDEBUG
2430Sstevel@tonic-gate int ktlilog = 0;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * Kernel level debugging aid. The global variable "ktlilog" is a bit
2470Sstevel@tonic-gate * mask which allows various types of debugging messages to be printed
2480Sstevel@tonic-gate * out.
2490Sstevel@tonic-gate *
2500Sstevel@tonic-gate * ktlilog & 1 will cause actual failures to be printed.
2510Sstevel@tonic-gate * ktlilog & 2 will cause informational messages to be
2520Sstevel@tonic-gate * printed.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate int
ktli_log(int level,char * str,int a1)2550Sstevel@tonic-gate ktli_log(int level, char *str, int a1)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate if (level & ktlilog)
2580Sstevel@tonic-gate printf(str, a1);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate #endif
261