NetBSD/sys/netccitt/hd_subr.c

398 lines
9.2 KiB
C
Raw Normal View History

/* $NetBSD: hd_subr.c,v 1.9 1997/06/24 02:26:02 thorpej Exp $ */
/*
* Copyright (c) University of British Columbia, 1984
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Laboratory for Computation Vision and the Computer Science Department
* of the University of British Columbia.
*
* 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.
*
* @(#)hd_subr.c 8.1 (Berkeley) 6/10/93
*/
1993-12-18 03:40:47 +03:00
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
1993-12-18 03:40:47 +03:00
#include <sys/protosw.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
1993-12-18 03:40:47 +03:00
#include <net/if.h>
1993-12-18 03:40:47 +03:00
#include <netccitt/hdlc.h>
#include <netccitt/hd_var.h>
#include <netccitt/x25.h>
#include <netccitt/pk_var.h>
1996-02-14 01:04:21 +03:00
#include <netccitt/pk_extern.h>
1996-02-14 01:04:21 +03:00
void
hd_init()
{
hdintrq.ifq_maxlen = IFQ_MAXLEN;
}
1996-02-14 01:04:21 +03:00
void *
hd_ctlinput(prc, addr, ext)
int prc;
struct sockaddr *addr;
void *ext;
{
1996-02-14 01:04:21 +03:00
register struct x25config *xcp = (struct x25config *) addr;
register struct hdcb *hdp;
register struct ifaddr *ifa;
1996-02-14 01:04:21 +03:00
struct ifnet *ifp;
if (addr->sa_family != AF_CCITT)
1996-02-14 01:04:21 +03:00
return (void *) (EAFNOSUPPORT);
if (xcp->xc_lptype != HDLCPROTO_LAPB)
1996-02-14 01:04:21 +03:00
return (void *) (EPROTONOSUPPORT);
ifa = ifa_ifwithaddr(addr);
if (ifa == 0 || ifa->ifa_addr->sa_family != AF_CCITT ||
(ifp = ifa->ifa_ifp) == 0)
1996-02-14 01:04:21 +03:00
panic("hd_ctlinput");
for (hdp = hdcbhead; hdp; hdp = hdp->hd_next)
if (hdp->hd_ifp == ifp)
break;
if (hdp == 0) { /* new interface */
/* an hdcb is now too big to fit in an mbuf */
1996-02-14 01:04:21 +03:00
MALLOC(hdp, struct hdcb *, sizeof(*hdp), M_PCB, M_DONTWAIT);
if (hdp == 0)
1996-02-14 01:04:21 +03:00
return (void *) (ENOBUFS);
bzero((caddr_t) hdp, sizeof(*hdp));
hdp->hd_pkp =
1996-02-14 01:04:21 +03:00
(caddr_t) pk_newlink((struct x25_ifaddr *) ifa,
(caddr_t) hdp);
((struct x25_ifaddr *) ifa)->ia_pkcb =
(struct pkcb *) hdp->hd_pkp;
1996-02-14 01:04:21 +03:00
if (hdp->hd_pkp == 0) {
free(hdp, M_PCB);
1996-02-14 01:04:21 +03:00
return (void *) (ENOBUFS);
}
hdp->hd_ifp = ifp;
hdp->hd_ifa = ifa;
hdp->hd_xcp = xcp;
hdp->hd_state = INIT;
hdp->hd_output = hd_ifoutput;
hdp->hd_next = hdcbhead;
hdcbhead = hdp;
1996-02-14 01:04:21 +03:00
} else if (hdp->hd_pkp == 0) { /* interface got reconfigured */
hdp->hd_pkp =
1996-02-14 01:04:21 +03:00
(caddr_t) pk_newlink((struct x25_ifaddr *) ifa,
(caddr_t) hdp);
((struct x25_ifaddr *) ifa)->ia_pkcb =
(struct pkcb *) hdp->hd_pkp;
1996-02-14 01:04:21 +03:00
if (hdp->hd_pkp == 0) {
free(hdp, M_PCB);
1996-02-14 01:04:21 +03:00
return (void *) (ENOBUFS);
}
}
switch (prc) {
case PRC_IFUP:
if (xcp->xc_lwsize == 0 ||
1996-02-14 01:04:21 +03:00
xcp->xc_lwsize > MAX_WINDOW_SIZE)
xcp->xc_lwsize = MAX_WINDOW_SIZE;
if (hdp->hd_state == INIT)
1996-02-14 01:04:21 +03:00
SET_TIMER(hdp);
break;
case PRC_IFDOWN:
if (hdp->hd_state == ABM)
1996-02-14 01:04:21 +03:00
hd_message(hdp, "Operator shutdown: link closed");
(void) pk_ctlinput(PRC_LINKDOWN,
(struct sockaddr *) hdp->hd_pkp, NULL);
/* fall thru to ... */
case PRC_DISCONNECT_REQUEST:
/* drop reference to pkcb --- it's dead meat */
hdp->hd_pkp = (caddr_t) 0;
1996-02-14 01:04:21 +03:00
((struct x25_ifaddr *) ifa)->ia_pkcb = (struct pkcb *) 0;
1996-02-14 01:04:21 +03:00
hd_writeinternal(hdp, DISC, POLLON);
hdp->hd_state = DISC_SENT;
1996-02-14 01:04:21 +03:00
SET_TIMER(hdp);
}
1996-02-14 01:04:21 +03:00
return (void *) (0);
}
1996-02-14 01:04:21 +03:00
void
hd_initvars(hdp)
register struct hdcb *hdp;
{
register struct mbuf *m;
1996-02-14 01:04:21 +03:00
register int i;
/* Clear Transmit queue. */
1996-02-14 01:04:21 +03:00
while ((m = hd_remove(&hdp->hd_txq)) != NULL)
m_freem(m);
/* Clear Retransmit queue. */
i = hdp->hd_lastrxnr;
while (i != hdp->hd_retxqi) {
1996-02-14 01:04:21 +03:00
m_freem(hdp->hd_retxq[i]);
i = (i + 1) % MODULUS;
}
hdp->hd_retxqi = 0;
hdp->hd_vs = hdp->hd_vr = 0;
hdp->hd_lasttxnr = hdp->hd_lastrxnr = 0;
hdp->hd_rrtimer = 0;
KILL_TIMER(hdp);
hdp->hd_retxcnt = 0;
hdp->hd_condition = 0;
}
1996-02-14 01:04:21 +03:00
int
hd_decode(hdp, frame)
register struct hdcb *hdp;
struct Hdlc_frame *frame;
{
1996-02-14 01:04:21 +03:00
register int frametype = ILLEGAL;
register struct Hdlc_iframe *iframe = (struct Hdlc_iframe *) frame;
register struct Hdlc_sframe *sframe = (struct Hdlc_sframe *) frame;
register struct Hdlc_uframe *uframe = (struct Hdlc_uframe *) frame;
1996-02-14 01:04:21 +03:00
if (iframe->hdlc_0 == 0) {
frametype = IFRAME;
hdp->hd_iframes_in++;
1996-02-14 01:04:21 +03:00
} else if (sframe->hdlc_01 == 1) {
/* Supervisory format. */
1996-02-14 01:04:21 +03:00
switch (sframe->s2) {
case 0:
frametype = RR;
hdp->hd_rrs_in++;
break;
1996-02-14 01:04:21 +03:00
case 1:
frametype = RNR;
hdp->hd_rnrs_in++;
break;
1996-02-14 01:04:21 +03:00
case 2:
frametype = REJ;
hdp->hd_rejs_in++;
}
1996-02-14 01:04:21 +03:00
} else if (uframe->hdlc_11 == 3) {
/* Unnumbered format. */
1996-02-14 01:04:21 +03:00
switch (uframe->m3) {
case 0:
frametype = DM;
break;
1996-02-14 01:04:21 +03:00
case 1:
frametype = SABM;
break;
1996-02-14 01:04:21 +03:00
case 2:
frametype = DISC;
break;
1996-02-14 01:04:21 +03:00
case 3:
frametype = UA;
break;
1996-02-14 01:04:21 +03:00
case 4:
frametype = FRMR;
hdp->hd_frmrs_in++;
}
}
return (frametype);
}
1996-02-14 01:04:21 +03:00
/*
* This routine is called when the HDLC layer internally generates a command
* or response for the remote machine ( eg. RR, UA etc. ). Only
* supervisory or unnumbered frames are processed.
*/
1996-02-14 01:04:21 +03:00
void
hd_writeinternal(hdp, frametype, pf)
register struct hdcb *hdp;
register int frametype, pf;
{
register struct mbuf *buf;
struct Hdlc_frame *frame;
register struct Hdlc_sframe *sframe;
register struct Hdlc_uframe *uframe;
1996-02-14 01:04:21 +03:00
MGETHDR(buf, M_DONTWAIT, MT_HEADER);
if (buf == 0)
return;
1996-02-14 01:04:21 +03:00
frame = mtod(buf, struct Hdlc_frame *);
sframe = mtod(buf, struct Hdlc_sframe *);
uframe = mtod(buf, struct Hdlc_uframe *);
/* Assume a response - address structure for DTE */
1996-02-14 01:04:21 +03:00
frame->address = ADDRESS_A;
buf->m_len = 2;
buf->m_act = buf->m_next = NULL;
switch (frametype) {
1996-02-14 01:04:21 +03:00
case RR:
frame->control = RR_CONTROL;
hdp->hd_rrs_out++;
break;
1996-02-14 01:04:21 +03:00
case RNR:
frame->control = RNR_CONTROL;
hdp->hd_rnrs_out++;
break;
1996-02-14 01:04:21 +03:00
case REJ:
frame->control = REJ_CONTROL;
hdp->hd_rejs_out++;
break;
1996-02-14 01:04:21 +03:00
case SABM:
frame->control = SABM_CONTROL;
frame->address = ADDRESS_B;
break;
1996-02-14 01:04:21 +03:00
case DISC:
if ((hdp->hd_ifp->if_flags & IFF_UP) == 0) {
hdp->hd_state = DISCONNECTED;
1996-02-14 01:04:21 +03:00
(void) m_freem(buf);
hd_flush(hdp->hd_ifp);
return;
}
1996-02-14 01:04:21 +03:00
frame->control = DISC_CONTROL;
frame->address = ADDRESS_B;
break;
1996-02-14 01:04:21 +03:00
case DM:
frame->control = DM_CONTROL;
break;
1996-02-14 01:04:21 +03:00
case UA:
frame->control = UA_CONTROL;
break;
1996-02-14 01:04:21 +03:00
case FRMR:
frame->control = FRMR_CONTROL;
bcopy((caddr_t) & hd_frmr, (caddr_t) frame->info, 3);
buf->m_len = 5;
hdp->hd_frmrs_out++;
}
1996-02-14 01:04:21 +03:00
if (sframe->hdlc_01 == 1) {
/* Supervisory format - RR, REJ, or RNR. */
1996-02-14 01:04:21 +03:00
sframe->nr = hdp->hd_vr;
sframe->pf = pf;
hdp->hd_lasttxnr = hdp->hd_vr;
hdp->hd_rrtimer = 0;
1996-02-14 01:04:21 +03:00
} else
uframe->pf = pf;
hd_trace(hdp, TX, buf);
1996-02-14 01:04:21 +03:00
buf->m_pkthdr.len = buf->m_len;
(*hdp->hd_output) (buf, hdp, NULL);
}
struct mbuf *
1996-02-14 01:04:21 +03:00
hd_remove(q)
struct hdtxq *q;
{
register struct mbuf *m;
1996-02-14 01:04:21 +03:00
m = q->head;
if (m) {
1996-02-14 01:04:21 +03:00
if ((q->head = m->m_act) == NULL)
q->tail = NULL;
m->m_act = 0;
}
return (m);
}
1996-02-14 01:04:21 +03:00
void
hd_append(q, m)
register struct hdtxq *q;
register struct mbuf *m;
{
1996-02-14 01:04:21 +03:00
m->m_act = NULL;
if (q->tail == NULL)
q->head = m;
else
1996-02-14 01:04:21 +03:00
q->tail->m_act = m;
q->tail = m;
}
1996-02-14 01:04:21 +03:00
void
hd_flush(ifp)
struct ifnet *ifp;
{
register struct mbuf *m;
1996-02-14 01:04:21 +03:00
register int s;
while (1) {
1996-02-14 01:04:21 +03:00
s = splimp();
IF_DEQUEUE(&ifp->if_snd, m);
splx(s);
if (m == 0)
break;
1996-02-14 01:04:21 +03:00
m_freem(m);
}
}
1996-02-14 01:04:21 +03:00
void
hd_message(hdp, msg)
struct hdcb *hdp;
char *msg;
{
1996-02-14 01:04:21 +03:00
if (hdcbhead->hd_next)
1996-10-13 06:10:01 +04:00
printf("HDLC(%s): %s\n", format_ntn(hdp->hd_xcp), msg);
else
1996-10-13 06:10:01 +04:00
printf("HDLC: %s\n", msg);
}
#ifdef HDLCDEBUG
1996-02-14 01:04:21 +03:00
void
hd_status(hdp)
struct hdcb *hdp;
{
1996-10-13 06:10:01 +04:00
printf("HDLC STATUS:\n V(S)=%d, V(R)=%d, retxqi=%d,\n",
1996-10-11 03:02:20 +04:00
hdp->hd_vs, hdp->hd_vr, hdp->hd_retxqi);
1996-10-13 06:10:01 +04:00
printf("Last_rx_nr=%d, Last_tx_nr=%d,\n Condition=%d, Xx=%d\n",
1996-10-11 03:02:20 +04:00
hdp->hd_lastrxnr, hdp->hd_lasttxnr, hdp->hd_condition, hdp->hd_xx);
}
#endif