2001-04-14 03:29:55 +04:00
|
|
|
/* $NetBSD: hd_output.c,v 1.13 2001/04/13 23:30:19 thorpej Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1993-04-09 16:00:07 +04:00
|
|
|
/*
|
1998-09-13 20:21:16 +04:00
|
|
|
* Copyright (c) 1984 University of British Columbia.
|
1994-05-13 10:02:48 +04:00
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:00:07 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* @(#)hd_output.c 8.1 (Berkeley) 6/10/93
|
1993-04-09 16:00:07 +04:00
|
|
|
*/
|
|
|
|
|
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/syslog.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1993-12-18 03:40:47 +03:00
|
|
|
#include <net/if.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1993-12-18 03:40:47 +03:00
|
|
|
#include <netccitt/hdlc.h>
|
|
|
|
#include <netccitt/hd_var.h>
|
|
|
|
#include <netccitt/x25.h>
|
1996-02-14 01:04:21 +03:00
|
|
|
#include <netccitt/pk_extern.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HDLC OUTPUT INTERFACE
|
|
|
|
*
|
|
|
|
* This routine is called when the X.25 packet layer output routine
|
1996-02-14 01:04:21 +03:00
|
|
|
* has a information frame (iframe) to write. It is also called
|
1993-04-09 16:00:07 +04:00
|
|
|
* by the input and control routines of the HDLC layer.
|
|
|
|
*/
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
int
|
|
|
|
#if __STDC__
|
|
|
|
hd_output(struct mbuf *m0, ...)
|
|
|
|
#else
|
|
|
|
hd_output(m0, va_alist)
|
|
|
|
struct mbuf *m0;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 17:53:32 +04:00
|
|
|
struct hdcb *hdp;
|
|
|
|
struct mbuf *m = m0;
|
1996-02-14 01:04:21 +03:00
|
|
|
int len;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, m0);
|
|
|
|
hdp = va_arg(ap, struct hdcb *);
|
|
|
|
va_end(ap);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
if (m == NULL)
|
1996-02-14 01:04:21 +03:00
|
|
|
panic("hd_output");
|
1994-05-13 10:02:48 +04:00
|
|
|
if ((m->m_flags & M_PKTHDR) == 0)
|
1996-02-14 01:04:21 +03:00
|
|
|
panic("hd_output 2");
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
if (hdp->hd_state != ABM) {
|
1996-02-14 01:04:21 +03:00
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Make room for the hdlc header either by prepending
|
|
|
|
* another mbuf, or by adjusting the offset and length
|
|
|
|
* of the first mbuf in the mbuf chain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
M_PREPEND(m, HDHEADERLN, M_DONTWAIT);
|
|
|
|
if (m == NULL)
|
1996-02-14 01:04:21 +03:00
|
|
|
return 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
for (len = 0; m; m = m->m_next)
|
|
|
|
len += m->m_len;
|
|
|
|
m = m0;
|
|
|
|
m->m_pkthdr.len = len;
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
hd_append(&hdp->hd_txq, m);
|
|
|
|
hd_start(hdp);
|
|
|
|
return 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
void
|
|
|
|
hd_start(hdp)
|
2000-03-30 17:53:32 +04:00
|
|
|
struct hdcb *hdp;
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 17:53:32 +04:00
|
|
|
struct mbuf *m;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
/*
|
1993-04-09 16:00:07 +04:00
|
|
|
* The iframe is only transmitted if all these conditions are FALSE.
|
|
|
|
* The iframe remains queued (hdp->hd_txq) however and will be
|
|
|
|
* transmitted as soon as these conditions are cleared.
|
|
|
|
*/
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
while (!(hdp->hd_condition & (TIMER_RECOVERY_CONDITION |
|
|
|
|
REMOTE_RNR_CONDITION | REJ_CONDITION))) {
|
|
|
|
if (hdp->hd_vs ==
|
|
|
|
(hdp->hd_lastrxnr + hdp->hd_xcp->xc_lwsize) % MODULUS) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have now exceeded the maximum number of
|
|
|
|
* outstanding iframes. Therefore, we must wait
|
|
|
|
* until at least one is acknowledged if this
|
|
|
|
* condition is not turned off before we are
|
|
|
|
* requested to write another iframe.
|
|
|
|
*/
|
1993-04-09 16:00:07 +04:00
|
|
|
hdp->hd_window_condition++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* hd_remove top iframe from transmit queue. */
|
1996-02-14 01:04:21 +03:00
|
|
|
if ((m = hd_remove(&hdp->hd_txq)) == NULL)
|
1993-04-09 16:00:07 +04:00
|
|
|
break;
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
hd_send_iframe(hdp, m, POLLOFF);
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
/*
|
|
|
|
* This procedure is passed a buffer descriptor for an iframe. It builds the
|
|
|
|
* rest of the control part of the frame and then writes it out. It also
|
|
|
|
* starts the acknowledgement timer and keeps the iframe in the Retransmit
|
|
|
|
* queue (Retxq) just in case we have to do this again.
|
|
|
|
*
|
|
|
|
* Note: This routine is also called from hd_input.c when retransmission of old
|
|
|
|
* frames is required.
|
1993-04-09 16:00:07 +04:00
|
|
|
*/
|
1996-02-14 01:04:21 +03:00
|
|
|
void
|
|
|
|
hd_send_iframe(hdp, buf, poll_bit)
|
2000-03-30 17:53:32 +04:00
|
|
|
struct hdcb *hdp;
|
|
|
|
struct mbuf *buf;
|
1996-02-14 01:04:21 +03:00
|
|
|
int poll_bit;
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 17:53:32 +04:00
|
|
|
struct Hdlc_iframe *iframe;
|
1996-02-14 01:04:21 +03:00
|
|
|
struct mbuf *m;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
KILL_TIMER(hdp);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
if (buf == 0) {
|
1996-10-13 06:10:01 +04:00
|
|
|
printf("hd_send_iframe: zero arg\n");
|
1993-04-09 16:00:07 +04:00
|
|
|
#ifdef HDLCDEBUG
|
1996-02-14 01:04:21 +03:00
|
|
|
hd_status(hdp);
|
|
|
|
hd_dumptrace(hdp);
|
1993-04-09 16:00:07 +04:00
|
|
|
#endif
|
|
|
|
hdp->hd_vs = (hdp->hd_vs + 7) % MODULUS;
|
|
|
|
return;
|
|
|
|
}
|
1996-02-14 01:04:21 +03:00
|
|
|
iframe = mtod(buf, struct Hdlc_iframe *);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
iframe->hdlc_0 = 0;
|
|
|
|
iframe->nr = hdp->hd_vr;
|
|
|
|
iframe->pf = poll_bit;
|
|
|
|
iframe->ns = hdp->hd_vs;
|
|
|
|
iframe->address = ADDRESS_B;
|
1993-04-09 16:00:07 +04:00
|
|
|
hdp->hd_lasttxnr = hdp->hd_vr;
|
|
|
|
hdp->hd_rrtimer = 0;
|
|
|
|
|
|
|
|
if (hdp->hd_vs == hdp->hd_retxqi) {
|
|
|
|
/* Check for retransmissions. */
|
|
|
|
/* Put iframe only once in the Retransmission queue. */
|
1996-02-14 01:04:21 +03:00
|
|
|
hdp->hd_retxq[(u_char) hdp->hd_retxqi] = buf;
|
1993-04-09 16:00:07 +04:00
|
|
|
hdp->hd_retxqi = (hdp->hd_retxqi + 1) % MODULUS;
|
|
|
|
hdp->hd_iframes_out++;
|
|
|
|
}
|
|
|
|
hdp->hd_vs = (hdp->hd_vs + 1) % MODULUS;
|
|
|
|
|
1997-06-24 06:25:59 +04:00
|
|
|
hd_trace(hdp, TX, buf);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
/* Write buffer on device. */
|
1996-02-14 01:04:21 +03:00
|
|
|
m = hdp->hd_dontcopy ? buf : m_copy(buf, 0, (int) M_COPYALL);
|
1993-04-09 16:00:07 +04:00
|
|
|
if (m == 0) {
|
1996-10-13 06:10:01 +04:00
|
|
|
printf("hdlc: out of mbufs\n");
|
1993-04-09 16:00:07 +04:00
|
|
|
return;
|
|
|
|
}
|
1996-02-14 01:04:21 +03:00
|
|
|
(*hdp->hd_output) (m, hdp);
|
|
|
|
SET_TIMER(hdp);
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
int
|
|
|
|
#if __STDC__
|
|
|
|
hd_ifoutput(struct mbuf *m, ...)
|
|
|
|
#else
|
|
|
|
hd_ifoutput(m, va_alist)
|
|
|
|
struct mbuf *m;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 17:53:32 +04:00
|
|
|
struct hdcb *hdp;
|
|
|
|
struct ifnet *ifp;
|
2001-04-14 03:29:55 +04:00
|
|
|
int s = splnet();
|
1996-02-14 01:04:21 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, m);
|
|
|
|
hdp = va_arg(ap, struct hdcb *);
|
|
|
|
va_end(ap);
|
|
|
|
ifp = hdp->hd_ifp;
|
|
|
|
|
1993-04-09 16:00:07 +04:00
|
|
|
/*
|
|
|
|
* Queue message on interface, and start output if interface
|
|
|
|
* not yet active.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (IF_QFULL(&ifp->if_snd)) {
|
|
|
|
IF_DROP(&ifp->if_snd);
|
1996-10-11 03:02:20 +04:00
|
|
|
#if 0
|
1996-10-13 06:10:01 +04:00
|
|
|
printf("%s: HDLC says OK to send but queue full, may hang\n",
|
1996-10-11 03:02:20 +04:00
|
|
|
ifp->if_xname);
|
|
|
|
#endif
|
1993-04-09 16:00:07 +04:00
|
|
|
m_freem(m);
|
|
|
|
} else {
|
|
|
|
IF_ENQUEUE(&ifp->if_snd, m);
|
|
|
|
if ((ifp->if_flags & IFF_OACTIVE) == 0)
|
1996-02-14 01:04:21 +03:00
|
|
|
(*ifp->if_start) (ifp);
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
splx(s);
|
1996-02-14 01:04:21 +03:00
|
|
|
return 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
/*
|
|
|
|
* This routine gets control when the timer expires because we have not
|
|
|
|
* received an acknowledgement for a iframe.
|
1993-04-09 16:00:07 +04:00
|
|
|
*/
|
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
void
|
|
|
|
hd_resend_iframe(hdp)
|
2000-03-30 17:53:32 +04:00
|
|
|
struct hdcb *hdp;
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (hdp->hd_retxcnt++ < hd_n2) {
|
|
|
|
if (!(hdp->hd_condition & TIMER_RECOVERY_CONDITION)) {
|
|
|
|
hdp->hd_xx = hdp->hd_vs;
|
|
|
|
hdp->hd_condition |= TIMER_RECOVERY_CONDITION;
|
|
|
|
}
|
|
|
|
hdp->hd_vs = hdp->hd_lastrxnr;
|
1996-02-14 01:04:21 +03:00
|
|
|
hd_send_iframe(hdp, hdp->hd_retxq[(u_char)hdp->hd_vs], POLLON);
|
1993-04-09 16:00:07 +04:00
|
|
|
} else {
|
1996-02-14 01:04:21 +03:00
|
|
|
/*
|
|
|
|
* At this point we have not received a RR even after N2
|
|
|
|
* retries - attempt to reset link.
|
|
|
|
*/
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1996-02-14 01:04:21 +03:00
|
|
|
hd_initvars(hdp);
|
|
|
|
hd_writeinternal(hdp, SABM, POLLOFF);
|
1993-04-09 16:00:07 +04:00
|
|
|
hdp->hd_state = WAIT_UA;
|
1996-02-14 01:04:21 +03:00
|
|
|
SET_TIMER(hdp);
|
|
|
|
hd_message(hdp, "Timer recovery failed: link down");
|
|
|
|
(void) pk_ctlinput(PRC_LINKDOWN,
|
|
|
|
(struct sockaddr *)hdp->hd_pkp, NULL);
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
}
|