📄 if_cons.c
字号:
/*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)if_cons.c 8.1 (Berkeley) 6/10/93 *//*********************************************************** Copyright IBM Corporation 1987 All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of IBM not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLIBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison *//* * $Header: if_cons.c,v 4.7 88/08/11 15:52:55 nhall Exp $ * $Source: /usr/argo/sys/netiso/RCS/if_cons.c,v $ * * cons.c - Connection Oriented Network Service: * including support for a) user transport-level service, * b) COSNS below CLNP, and c) CONS below TP. */#ifdef TPCONS#ifdef KERNEL#ifdef ARGO_DEBUG#define Static unsigned LAST_CALL_PCB;#else /* ARGO_DEBUG */#define Static static#endif /* ARGO_DEBUG */#ifndef SOCK_STREAM#include <sys/param.h>#include <sys/systm.h>#include <sys/mbuf.h>#include <sys/protosw.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/errno.h>#include <sys/ioctl.h>#include <sys/tsleep.h>#include <net/if.h>#include <net/netisr.h>#include <net/route.h>#include <netiso/iso_errno.h>#include <netiso/argo_debug.h>#include <netiso/tp_trace.h>#include <netiso/iso.h>#include <netiso/cons.h>#include <netiso/iso_pcb.h>#include <netccitt/x25.h>#include <netccitt/pk.h>#include <netccitt/pk_var.h>#endif#ifdef ARGO_DEBUG#define MT_XCONN 0x50#define MT_XCLOSE 0x51#define MT_XCONFIRM 0x52#define MT_XDATA 0x53#define MT_XHEADER 0x54#else#define MT_XCONN MT_DATA#define MT_XCLOSE MT_DATA#define MT_XCONFIRM MT_DATA#define MT_XDATA MT_DATA#define MT_XHEADER MT_HEADER#endif /* ARGO_DEBUG */#define DONTCLEAR -1/********************************************************************* * cons.c - CONS interface to the x.25 layer * * TODO: figure out what resources we might run out of besides mbufs. * If we run out of any of them (including mbufs) close and recycle * lru x% of the connections, for some parameter x. * * There are 2 interfaces from above: * 1) from TP0: * cons CO network service * TP associates a transport connection with a network connection. * cons_output( isop, m, len, isdgm==0 ) * co_flags == 0 * 2) from TP4: * It's a datagram service, like clnp is. - even though it calls * cons_output( isop, m, len, isdgm==1 ) * it eventually goes through * cosns_output(ifp, m, dst). * TP4 permits multiplexing (reuse, possibly simultaneously) of the * network connections. * This means that many sockets (many tpcbs) may be associated with * this pklcd, hence cannot have a back ptr from pklcd to a tpcb. * co_flags & CONSF_DGM * co_socket is null since there may be many sockets that use this pklcd. *NOTE: streams would really be nice. sigh.NOTE: PVCs could be handled by config-ing a cons with an address and with the IFF_POINTTOPOINT flag on. This code would then have to skip the connection setup stuff for pt-to-pt links. *********************************************************************/#define CONS_IFQMAXLEN 5/* protosw pointers for getting to higher layer */Static struct protosw *CLNP_proto;Static struct protosw *TP_proto;Static struct protosw *X25_proto;Static int issue_clear_req();#ifndef PHASEONEextern struct ifaddr *ifa_ifwithnet();#endif /* PHASEONE */extern struct ifaddr *ifa_ifwithaddr();extern struct isopcb tp_isopcb; /* chain of all TP pcbs */Static int parse_facil(), NSAPtoDTE(), make_partial_x25_packet();Static int FACILtoNSAP(), DTEtoNSAP();Static struct pklcd *cons_chan_to_pcb();#define HIGH_NIBBLE 1#define LOW_NIBBLE 0/* * NAME: nibble_copy() * FUNCTION and ARGUMENTS: * copies (len) nibbles from (src_octet), high or low nibble * to (dst_octet), high or low nibble, * src_nibble & dst_nibble should be: * HIGH_NIBBLE (1) if leftmost 4 bits/ most significant nibble * LOW_NIBBLE (0) if rightmost 4 bits/ least significant nibble * RETURNS: VOID */voidnibble_copy(src_octet, src_nibble, dst_octet, dst_nibble, len) register char *src_octet; register char *dst_octet; register unsigned src_nibble; register unsigned dst_nibble; int len;{ register i; register unsigned dshift, sshift; IFDEBUG(D_CADDR) printf("nibble_copy ( 0x%x, 0x%x, 0x%x, 0x%x 0x%x)\n", src_octet, src_nibble, dst_octet, dst_nibble, len); ENDDEBUG#define SHIFT 0x4 dshift = dst_nibble << 2; sshift = src_nibble << 2; for (i=0; i<len; i++) { /* clear dst_nibble */ *dst_octet &= ~(0xf<< dshift); /* set dst nibble */ *dst_octet |= ( 0xf & (*src_octet >> sshift))<< dshift; dshift ^= SHIFT; sshift ^= SHIFT; src_nibble = 1-src_nibble; dst_nibble = 1-dst_nibble; src_octet += src_nibble; dst_octet += dst_nibble; } IFDEBUG(D_CADDR) printf("nibble_copy DONE\n"); ENDDEBUG}/* * NAME: nibble_match() * FUNCTION and ARGUMENTS: * compares src_octet/src_nibble and dst_octet/dst_nibble for len nibbles. * RETURNS: 0 if they differ, 1 if they are the same. */intnibble_match( src_octet, src_nibble, dst_octet, dst_nibble, len) register char *src_octet; register char *dst_octet; register unsigned src_nibble; register unsigned dst_nibble; int len;{ register i; register unsigned dshift, sshift; u_char nibble_a, nibble_b; IFDEBUG(D_CADDR) printf("nibble_match ( 0x%x, 0x%x, 0x%x, 0x%x 0x%x)\n", src_octet, src_nibble, dst_octet, dst_nibble, len); ENDDEBUG#define SHIFT 0x4 dshift = dst_nibble << 2; sshift = src_nibble << 2; for (i=0; i<len; i++) { nibble_b = ((*dst_octet)>>dshift) & 0xf; nibble_a = ( 0xf & (*src_octet >> sshift)); if (nibble_b != nibble_a) return 0; dshift ^= SHIFT; sshift ^= SHIFT; src_nibble = 1-src_nibble; dst_nibble = 1-dst_nibble; src_octet += src_nibble; dst_octet += dst_nibble; } IFDEBUG(D_CADDR) printf("nibble_match DONE\n"); ENDDEBUG return 1;}/* **************************** NET PROTOCOL cons *************************** *//* * NAME: cons_init() * CALLED FROM: * autoconf * FUNCTION: * initialize the protocol */cons_init(){ int tp_incoming(), clnp_incoming(); CLNP_proto = pffindproto(AF_ISO, ISOPROTO_CLNP, SOCK_DGRAM); X25_proto = pffindproto(AF_ISO, ISOPROTO_X25, SOCK_STREAM); TP_proto = pffindproto(AF_ISO, ISOPROTO_TP0, SOCK_SEQPACKET); IFDEBUG(D_CCONS) printf("cons_init end : cnlp_proto 0x%x cons proto 0x%x tp proto 0x%x\n", CLNP_proto, X25_proto, TP_proto); ENDDEBUG#ifdef notdef pk_protolisten(0x81, 0, clnp_incoming); pk_protolisten(0x82, 0, esis_incoming); pk_protolisten(0x84, 0, tp8878_A_incoming); pk_protolisten(0, 0, tp_incoming);#endif}tp_incoming(lcp, m)struct pklcd *lcp;register struct mbuf *m;{ register struct isopcb *isop; int cons_tpinput(); if (iso_pcballoc((struct socket *)0, &tp_isopcb)) { pk_close(lcp); return; } isop = tp_isopcb.isop_next; lcp->lcd_upper = cons_tpinput; lcp->lcd_upnext = (caddr_t)isop; lcp->lcd_send(lcp); /* Confirms call */ isop->isop_chan = (caddr_t)lcp; isop->isop_laddr = &isop->isop_sladdr; isop->isop_faddr = &isop->isop_sfaddr; DTEtoNSAP(isop->isop_laddr, &lcp->lcd_laddr); DTEtoNSAP(isop->isop_faddr, &lcp->lcd_faddr); parse_facil(lcp, isop, &(mtod(m, struct x25_packet *)->packet_data), m->m_pkthdr.len - PKHEADERLN);}cons_tpinput(lcp, m0)struct mbuf *m0;struct pklcd *lcp;{ register struct isopcb *isop = (struct isopcb *)lcp->lcd_upnext; register struct x25_packet *xp; int cmd, ptype = CLEAR; if (isop == 0) return; if (m0 == 0) goto dead; switch(m0->m_type) { case MT_DATA: case MT_OOBDATA: tpcons_input(m0, isop->isop_faddr, isop->isop_laddr, (caddr_t)lcp); return; case MT_CONTROL: switch (ptype = pk_decode(mtod(m0, struct x25_packet *))) { case RR: cmd = PRC_CONS_SEND_DONE; break; case CALL_ACCEPTED: if (lcp->lcd_sb.sb_mb) lcp->lcd_send(lcp); /* XXX - fix this */ /*FALLTHROUGH*/ default: return; dead: case CLEAR: case CLEAR_CONF: lcp->lcd_upper = 0; lcp->lcd_upnext = 0; isop->isop_chan = 0; case RESET: cmd = PRC_ROUTEDEAD; } tpcons_ctlinput(cmd, isop->isop_faddr, isop); if (cmd = PRC_ROUTEDEAD && isop->isop_refcnt == 0) iso_pcbdetach(isop); }}/* * NAME: cons_connect() * CALLED FROM: * tpcons_pcbconnect() when opening a new connection. * FUNCTION anD ARGUMENTS: * Figures out which device to use, finding a route if one doesn't * already exist. * RETURN VALUE: * returns E* */cons_connect(isop) register struct isopcb *isop;{ register struct pklcd *lcp = (struct pklcd *)isop->isop_chan; register struct mbuf *m; struct ifaddr *ifa; int error; IFDEBUG(D_CCONN) printf("cons_connect(0x%x): ", isop); dump_isoaddr(isop->isop_faddr); printf("myaddr: "); dump_isoaddr(isop->isop_laddr); printf("\n" ); ENDDEBUG NSAPtoDTE(isop->isop_faddr, &lcp->lcd_faddr); lcp->lcd_upper = cons_tpinput; lcp->lcd_upnext = (caddr_t)isop; IFDEBUG(D_CCONN) printf( "calling make_partial_x25_packet( 0x%x, 0x%x, 0x%x)\n", &lcp->lcd_faddr, &lcp->lcd_laddr, isop->isop_socket->so_proto->pr_protocol); ENDDEBUG if ((error = make_partial_x25_packet(isop, lcp, m)) == 0) error = pk_connect(lcp, &lcp->lcd_faddr); return error;}/* **************************** DEVICE cons *************************** *//* * NAME: cons_ctlinput() * CALLED FROM: * lower layer when ECN_CLEAR occurs : this routine is here * for consistency - cons subnet service calls its higher layer * through the protosw entry. * FUNCTION & ARGUMENTS: * cmd is a PRC_* command, list found in ../sys/protosw.h * copcb is the obvious. * This serves the higher-layer cons service. * NOTE: this takes 3rd arg. because cons uses it to inform itself * of things (timeouts, etc) but has a pcb instead of an address. */cons_ctlinput(cmd, sa, copcb) int cmd; struct sockaddr *sa; register struct pklcd *copcb;{}find_error_reason( xp ) register struct x25_packet *xp;{ extern u_char x25_error_stats[]; int error, cause; if (xp) { cause = 4[(char *)xp]; switch (cause) { case 0x00: case 0x80: /* DTE originated; look at the diagnostic */ error = (CONL_ERROR_MASK | cause); goto done; case 0x01: /* number busy */ case 0x81: case 0x09: /* Out of order */ case 0x89: case 0x11: /* Remot Procedure Error */ case 0x91: case 0x19: /* reverse charging accept not subscribed */ case 0x99: case 0x21: /* Incampat destination */ case 0xa1: case 0x29: /* fast select accept not subscribed */ case 0xa9: case 0x39: /* ship absent */ case 0xb9: case 0x03: /* invalid facil request */ case 0x83: case 0x0b: /* access barred */ case 0x8b: case 0x13: /* local procedure error */ case 0x93:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -