2001-01-05 15:49:52 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1997, 2000 Hellmuth Michaelis. 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* i4b_util.c - layer 2 utility routines
|
|
|
|
* -------------------------------------
|
|
|
|
*
|
2006-10-16 16:23:00 +04:00
|
|
|
* $Id: i4b_util.c,v 1.10 2006/10/16 12:23:00 pooka Exp $
|
2001-01-05 15:49:52 +03:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*
|
|
|
|
* last edit-date: [Fri Jan 5 11:33:47 2001]
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-10-16 16:23:00 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: i4b_util.c,v 1.10 2006/10/16 12:23:00 pooka Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include "i4bq921.h"
|
|
|
|
#else
|
|
|
|
#define NI4BQ921 1
|
|
|
|
#endif
|
|
|
|
#if NI4BQ921 > 0
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
|
|
|
|
#include <sys/callout.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <machine/i4b_debug.h>
|
|
|
|
#include <machine/i4b_ioctl.h>
|
|
|
|
#else
|
|
|
|
#include <netisdn/i4b_debug.h>
|
|
|
|
#include <netisdn/i4b_ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netisdn/i4b_global.h>
|
2002-03-24 23:35:43 +03:00
|
|
|
#include <netisdn/i4b_l2.h>
|
2001-01-05 15:49:52 +03:00
|
|
|
#include <netisdn/i4b_l1l2.h>
|
|
|
|
#include <netisdn/i4b_isdnq931.h>
|
|
|
|
#include <netisdn/i4b_mbuf.h>
|
|
|
|
#include <netisdn/i4b_l2fsm.h>
|
2002-05-21 14:31:10 +04:00
|
|
|
#include <netisdn/i4b_l3l4.h>
|
2001-01-05 15:49:52 +03:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_establish_data_link(l2_softc_t *l2sc)
|
|
|
|
{
|
2005-02-27 01:31:44 +03:00
|
|
|
i4b_l1_activate(l2sc);
|
2001-01-05 15:49:52 +03:00
|
|
|
|
|
|
|
i4b_clear_exception_conditions(l2sc);
|
|
|
|
|
|
|
|
l2sc->RC = 0;
|
|
|
|
|
|
|
|
i4b_tx_sabme(l2sc, P1);
|
|
|
|
|
|
|
|
i4b_T200_restart(l2sc);
|
|
|
|
|
2005-02-27 01:31:44 +03:00
|
|
|
i4b_T203_stop(l2sc);
|
2001-01-05 15:49:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_clear_exception_conditions(l2_softc_t *l2sc)
|
|
|
|
{
|
2001-01-19 15:44:44 +03:00
|
|
|
int s;
|
2001-01-05 15:49:52 +03:00
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
s = splnet();
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
/*XXX -------------------------------------------------------------- */
|
|
|
|
/*XXX is this really appropriate here or should it moved elsewhere ? */
|
|
|
|
|
|
|
|
i4b_Dcleanifq(&l2sc->i_queue);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
if(l2sc->ua_num != UA_EMPTY)
|
|
|
|
{
|
|
|
|
i4b_Dfreembuf(l2sc->ua_frame);
|
|
|
|
l2sc->ua_num = UA_EMPTY;
|
|
|
|
}
|
|
|
|
/*XXX -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
l2sc->peer_busy = 0;
|
|
|
|
|
|
|
|
l2sc->rej_excpt = 0;
|
|
|
|
|
|
|
|
l2sc->own_busy = 0;
|
|
|
|
|
2005-02-27 01:31:44 +03:00
|
|
|
l2sc->ack_pend = 0;
|
2001-01-05 15:49:52 +03:00
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
splx(s);
|
2001-01-05 15:49:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_transmit_enquire(l2_softc_t *l2sc)
|
|
|
|
{
|
|
|
|
if(l2sc->own_busy)
|
|
|
|
i4b_tx_rnr_command(l2sc, P1);
|
|
|
|
else
|
|
|
|
i4b_tx_rr_command(l2sc, P1);
|
|
|
|
|
|
|
|
l2sc->ack_pend = 0;
|
|
|
|
|
|
|
|
i4b_T200_start(l2sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine NR ERROR RECOVERY (Q.921 03/93 page 83)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_nr_error_recovery(l2_softc_t *l2sc)
|
|
|
|
{
|
|
|
|
i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
|
|
|
|
|
|
|
|
i4b_establish_data_link(l2sc);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
l2sc->l3initiated = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_enquiry_response(l2_softc_t *l2sc)
|
|
|
|
{
|
|
|
|
if(l2sc->own_busy)
|
|
|
|
i4b_tx_rnr_response(l2sc, F1);
|
|
|
|
else
|
|
|
|
i4b_tx_rr_response(l2sc, F1);
|
|
|
|
|
|
|
|
l2sc->ack_pend = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
|
|
|
|
{
|
2001-01-19 15:44:44 +03:00
|
|
|
int s;
|
2001-01-05 15:49:52 +03:00
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
s = splnet();
|
2001-01-05 15:49:52 +03:00
|
|
|
|
|
|
|
NDBGL2(L2_ERROR, "nr = %d", nr );
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
while(l2sc->vs != nr)
|
|
|
|
{
|
|
|
|
NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
|
|
|
|
|
|
|
|
M128DEC(l2sc->vs);
|
|
|
|
|
|
|
|
/* XXXXXXXXXXXXXXXXX */
|
|
|
|
|
|
|
|
if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
|
|
|
|
{
|
|
|
|
if(IF_QFULL(&l2sc->i_queue))
|
|
|
|
{
|
|
|
|
NDBGL2(L2_ERROR, "ERROR, I-queue full!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
|
|
|
|
l2sc->ua_num = UA_EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXXXXXXXXXXXXXXXX */
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
i4b_i_frame_queued_up(l2sc);
|
|
|
|
}
|
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
splx(s);
|
2001-01-05 15:49:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_acknowledge_pending(l2_softc_t *l2sc)
|
|
|
|
{
|
|
|
|
if(l2sc->ack_pend)
|
|
|
|
{
|
|
|
|
l2sc->ack_pend = 0;
|
|
|
|
i4b_tx_rr_response(l2sc, F0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* i4b_print_frame - just print the hex contents of a frame
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_print_frame(int len, u_char *buf)
|
|
|
|
{
|
|
|
|
#if DO_I4B_DEBUG
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(i4b_l2_debug & L2_ERROR)) /* XXXXXXXXXXXXXXXXXXXXX */
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
printf(" 0x%x", buf[i]);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* i4b_print_l2var - print some l2softc vars
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_print_l2var(l2_softc_t *l2sc)
|
|
|
|
{
|
2003-10-03 20:38:44 +04:00
|
|
|
NDBGL2(L2_ERROR, "isdnif %d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
|
|
|
|
l2sc->drv->isdnif,
|
2001-01-05 15:49:52 +03:00
|
|
|
l2sc->vr,
|
|
|
|
l2sc->vs,
|
|
|
|
l2sc->va,
|
|
|
|
l2sc->ack_pend,
|
|
|
|
l2sc->peer_busy,
|
|
|
|
l2sc->own_busy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* got s or i frame, check if valid ack for last sent frame
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2006-10-16 16:23:00 +04:00
|
|
|
i4b_rxd_ack(l2_softc_t *l2sc, struct isdn_l3_driver *drv __unused, int nr)
|
2001-01-05 15:49:52 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef NOTDEF
|
|
|
|
NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
|
|
|
|
nr,
|
|
|
|
l2sc->ua_num,
|
|
|
|
l2sc->vr,
|
|
|
|
l2sc->vs,
|
|
|
|
l2sc->va);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(l2sc->ua_num != UA_EMPTY)
|
|
|
|
{
|
2001-01-19 15:44:44 +03:00
|
|
|
int s;
|
2001-01-05 15:49:52 +03:00
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
s = splnet();
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
M128DEC(nr);
|
|
|
|
|
|
|
|
if(l2sc->ua_num != nr)
|
|
|
|
NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
i4b_Dfreembuf(l2sc->ua_frame);
|
|
|
|
l2sc->ua_num = UA_EMPTY;
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-19 15:44:44 +03:00
|
|
|
splx(s);
|
2001-01-05 15:49:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* if not already active, activate layer 1
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
i4b_l1_activate(l2_softc_t *l2sc)
|
|
|
|
{
|
|
|
|
if(l2sc->ph_active == PH_INACTIVE)
|
|
|
|
{
|
|
|
|
l2sc->ph_active = PH_ACTIVEPEND;
|
2001-03-24 15:40:29 +03:00
|
|
|
l2sc->driver->ph_activate_req(l2sc->l1_token);
|
2001-01-05 15:49:52 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* check for v(a) <= n(r) <= v(s)
|
|
|
|
* nr = receive sequence frame counter, va = acknowledge sequence frame
|
|
|
|
* counter and vs = transmit sequence frame counter
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
i4b_l2_nr_ok(int nr, int va, int vs)
|
|
|
|
{
|
|
|
|
if((va > nr) && ((nr != 0) || (va != 127)))
|
|
|
|
{
|
|
|
|
NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
|
|
|
|
return 0; /* fail */
|
|
|
|
}
|
|
|
|
|
|
|
|
if((nr > vs) && ((vs != 0) || (nr != 127)))
|
|
|
|
{
|
|
|
|
NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
|
|
|
|
return 0; /* fail */
|
|
|
|
}
|
|
|
|
return 1; /* good */
|
|
|
|
}
|
2005-02-27 01:31:44 +03:00
|
|
|
|
2001-01-05 15:49:52 +03:00
|
|
|
#endif /* NI4BQ921 > 0 */
|
|
|
|
|