The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netisdn/i4b_tei.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  *
   13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   23  * SUCH DAMAGE.
   24  *
   25  *---------------------------------------------------------------------------
   26  *
   27  *      i4b_tei.c - tei handling procedures
   28  *      -----------------------------------
   29  *
   30  *      $Id: i4b_tei.c,v 1.10 2007/01/24 13:08:15 hubertf Exp $
   31  *
   32  * $FreeBSD$
   33  *
   34  *      last edit-date: [Fri Jan  5 11:33:47 2001]
   35  *
   36  *---------------------------------------------------------------------------*/
   37 
   38 #include <sys/cdefs.h>
   39 __KERNEL_RCSID(0, "$NetBSD: i4b_tei.c,v 1.10 2007/01/24 13:08:15 hubertf Exp $");
   40 
   41 #ifdef __FreeBSD__
   42 #include "i4bq921.h"
   43 #else
   44 #define NI4BQ921        1
   45 #endif
   46 #if NI4BQ921 > 0
   47 
   48 #include <sys/param.h>
   49 #include <sys/kernel.h>
   50 #include <sys/systm.h>
   51 #include <sys/syslog.h>
   52 #include <sys/mbuf.h>
   53 #include <sys/socket.h>
   54 #include <net/if.h>
   55 
   56 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
   57 #include <sys/callout.h>
   58 #endif
   59 
   60 #if defined(__FreeBSD__)
   61 #if defined (__FreeBSD_version) && __FreeBSD_version <= 400000
   62 #include <machine/random.h>
   63 #else
   64 #include <sys/random.h>
   65 #endif
   66 #endif
   67 
   68 #ifdef __FreeBSD__
   69 #include <machine/i4b_debug.h>
   70 #include <machine/i4b_ioctl.h>
   71 #else
   72 #include <netisdn/i4b_debug.h>
   73 #include <netisdn/i4b_ioctl.h>
   74 #endif
   75 
   76 #include <netisdn/i4b_global.h>
   77 #include <netisdn/i4b_l2.h>
   78 #include <netisdn/i4b_l1l2.h>
   79 #include <netisdn/i4b_isdnq931.h>
   80 #include <netisdn/i4b_mbuf.h>
   81 
   82 #include <netisdn/i4b_l2fsm.h>
   83 #include <netisdn/i4b_l3l4.h>
   84 
   85 /*---------------------------------------------------------------------------*
   86  *      handle a received TEI management frame
   87  *---------------------------------------------------------------------------*/
   88 void
   89 i4b_tei_rxframe(l2_softc_t *l2sc, struct isdn_l3_driver *drv, struct mbuf *m)
   90 {
   91         u_char *ptr = m->m_data;
   92 
   93         switch(*(ptr + OFF_MT))
   94         {
   95                 case MT_ID_ASSIGN:
   96                         if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
   97                             (*(ptr + OFF_RIH) == l2sc->last_rih))
   98                         {
   99                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  100                                 l2sc->tei_valid = TEI_VALID;
  101 
  102                                 if(l2sc->T202 == TIMER_ACTIVE)
  103                                         i4b_T202_stop(l2sc);
  104 
  105                                 i4b_mdl_status_ind(drv, STI_TEIASG, l2sc->tei);
  106 
  107                                 log(LOG_INFO, "i4b: isdnif %d, assigned TEI = %d = 0x%02x\n", l2sc->drv->isdnif, l2sc->tei, l2sc->tei);
  108 
  109                                 NDBGL2(L2_TEI_MSG, "TEI ID Assign - TEI = %d", l2sc->tei);
  110 
  111                                 i4b_next_l2state(l2sc, drv, EV_MDASGRQ);
  112                         }
  113                         break;
  114 
  115                 case MT_ID_DENY:
  116                         if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
  117                             (*(ptr + OFF_RIH) == l2sc->last_rih))
  118                         {
  119                                 l2sc->tei_valid = TEI_INVALID;
  120                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  121 
  122                                 if(l2sc->tei == GROUP_TEI)
  123                                 {
  124                                         log(LOG_WARNING, "i4b: isdnif %d, denied TEI, no TEI values available from exchange!\n", l2sc->drv->isdnif);
  125                                         NDBGL2(L2_TEI_ERR, "TEI ID Denied, No TEI values available from exchange!");
  126                                 }
  127                                 else
  128                                 {
  129                                         log(LOG_WARNING, "i4b: isdnif %d, denied TEI = %d = 0x%02x\n", l2sc->drv->isdnif, l2sc->tei, l2sc->tei);
  130                                         NDBGL2(L2_TEI_ERR, "TEI ID Denied - TEI = %d", l2sc->tei);
  131                                 }
  132                                 i4b_mdl_status_ind(drv, STI_TEIASG, -1);
  133                                 i4b_next_l2state(l2sc, drv, EV_MDERRRS);
  134                         }
  135                         break;
  136 
  137                 case MT_ID_CHK_REQ:
  138                         if( (l2sc->tei_valid == TEI_VALID) &&
  139                             ( (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI))) ||
  140                               (GROUP_TEI == GET_TEIFROMAI(*(ptr+OFF_AI))) ))
  141                         {
  142                                 static int lasttei = -1;
  143 
  144                                 if(l2sc->tei != lasttei)
  145                                 {
  146                                         NDBGL2(L2_TEI_MSG, "TEI ID Check Req - TEI = %d", l2sc->tei);
  147                                         lasttei = l2sc->tei;
  148                                 }
  149 
  150                                 if(l2sc->T202 == TIMER_ACTIVE)
  151                                         i4b_T202_stop(l2sc);
  152                                 i4b_tei_chkresp(l2sc);
  153                         }
  154                         break;
  155 
  156                 case MT_ID_REMOVE:
  157                         if( (l2sc->tei_valid == TEI_VALID) &&
  158                             ( (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI))) ||
  159                               (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI)))))
  160                         {
  161                                 l2sc->tei_valid = TEI_INVALID;
  162                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  163 
  164                                 log(LOG_INFO, "i4b: isdnif %d, removed TEI = %d = 0x%02x\n", drv->isdnif, l2sc->tei, l2sc->tei);
  165                                 NDBGL2(L2_TEI_MSG, "TEI ID Remove - TEI = %d", l2sc->tei);
  166                                 i4b_mdl_status_ind(drv, STI_TEIASG, -1);
  167                                 i4b_next_l2state(l2sc, drv, EV_MDREMRQ);
  168                         }
  169                         break;
  170 
  171                 default:
  172                         NDBGL2(L2_TEI_ERR, "UNKNOWN TEI MGMT Frame, type = 0x%x", *(ptr + OFF_MT));
  173                         i4b_print_frame(m->m_len, m->m_data);
  174                         break;
  175         }
  176         i4b_Dfreembuf(m);
  177 }
  178 
  179 /*---------------------------------------------------------------------------*
  180  *      allocate and fill up a TEI management frame for sending
  181  *---------------------------------------------------------------------------*/
  182 static struct mbuf *
  183 build_tei_mgmt_frame(l2_softc_t *l2sc, unsigned char type)
  184 {
  185         struct mbuf *m;
  186 
  187         if((m = i4b_Dgetmbuf(TEI_MGMT_FRM_LEN)) == NULL)
  188                 return(NULL);
  189 
  190         m->m_data[TEIM_SAPIO] = 0xfc;   /* SAPI = 63, CR = 0, EA = 0 */
  191         m->m_data[TEIM_TEIO]  = 0xff;   /* TEI = 127, EA = 1 */
  192         m->m_data[TEIM_UIO]   = UI;     /* UI */
  193         m->m_data[TEIM_MEIO]  = MEI;    /* MEI */
  194         m->m_data[TEIM_MTO]   = type;   /* message type */
  195 
  196         switch(type)
  197         {
  198                 case MT_ID_REQEST:
  199                         i4b_make_rand_ri(l2sc);
  200                         m->m_data[TEIM_RILO] = l2sc->last_ril;
  201                         m->m_data[TEIM_RIHO] = l2sc->last_rih;
  202                         m->m_data[TEIM_AIO] = (GROUP_TEI << 1) | 0x01;
  203                         break;
  204 
  205                 case MT_ID_CHK_RSP:
  206                         i4b_make_rand_ri(l2sc);
  207                         m->m_data[TEIM_RILO] = l2sc->last_ril;
  208                         m->m_data[TEIM_RIHO] = l2sc->last_rih;
  209                         m->m_data[TEIM_AIO] = (l2sc->tei << 1) | 0x01;
  210                         break;
  211 
  212                 case MT_ID_VERIFY:
  213                         m->m_data[TEIM_RILO] = 0;
  214                         m->m_data[TEIM_RIHO] = 0;
  215                         m->m_data[TEIM_AIO] = (l2sc->tei << 1) | 0x01;
  216                         break;
  217 
  218                 default:
  219                         i4b_Dfreembuf(m);
  220                         panic("build_tei_mgmt_frame: invalid type");
  221                         break;
  222         }
  223         l2sc->stat.tx_tei++;
  224         return(m);
  225 }
  226 
  227 /*---------------------------------------------------------------------------*
  228  *      i4b_tei_assign - TEI assignment procedure (Q.921, 5.3.2, pp 24)
  229  *      T202func and N202 _MUST_ be set prior to calling this function !
  230  *---------------------------------------------------------------------------*/
  231 void
  232 i4b_tei_assign(l2_softc_t *l2sc)
  233 {
  234         struct mbuf *m;
  235 
  236         NDBGL2(L2_TEI_MSG, "tx TEI ID_Request");
  237 
  238         m = build_tei_mgmt_frame(l2sc, MT_ID_REQEST);
  239 
  240         if(m == NULL)
  241                 panic("i4b_tei_assign: no mbuf");
  242 
  243         i4b_T202_start(l2sc);
  244 
  245         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  246 }
  247 
  248 /*---------------------------------------------------------------------------*
  249  *      i4b_tei_assign - TEI verify procedure (Q.921, 5.3.5, pp 29)
  250  *      T202func and N202 _MUST_ be set prior to calling this function !
  251  *---------------------------------------------------------------------------*/
  252 void
  253 i4b_tei_verify(l2_softc_t *l2sc)
  254 {
  255         struct mbuf *m;
  256 
  257         NDBGL2(L2_TEI_MSG, "tx TEI ID_Verify");
  258 
  259         m = build_tei_mgmt_frame(l2sc, MT_ID_VERIFY);
  260 
  261         if(m == NULL)
  262                 panic("i4b_tei_verify: no mbuf");
  263 
  264         i4b_T202_start(l2sc);
  265 
  266         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  267 }
  268 
  269 /*---------------------------------------------------------------------------*
  270  *      i4b_tei_chkresp - TEI check response procedure (Q.921, 5.3.5, pp 29)
  271  *---------------------------------------------------------------------------*/
  272 void
  273 i4b_tei_chkresp(l2_softc_t *l2sc)
  274 {
  275         struct mbuf *m;
  276         static int lasttei = 0;
  277 
  278         if(l2sc->tei != lasttei)
  279         {
  280                 lasttei = l2sc->tei;
  281                 NDBGL2(L2_TEI_MSG, "tx TEI ID_Check_Response");
  282         }
  283 
  284         m = build_tei_mgmt_frame(l2sc, MT_ID_CHK_RSP);
  285 
  286         if(m == NULL)
  287                 panic("i4b_tei_chkresp: no mbuf");
  288 
  289         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  290 }
  291 
  292 /*---------------------------------------------------------------------------*
  293  *      generate some 16 bit "random" number used for TEI mgmt Ri field
  294  *---------------------------------------------------------------------------*/
  295 void
  296 i4b_make_rand_ri(l2_softc_t *l2sc)
  297 {
  298 
  299 #if defined(__FreeBSD__)
  300 
  301         u_short val;
  302 
  303 #ifdef RANDOMDEV
  304         read_random((char *)&val, sizeof(val));
  305 #else
  306         val = (u_short)random();
  307 #endif /* RANDOMDEV */
  308 
  309 #else
  310 
  311         register u_short val;
  312         register int i;
  313         static int called = 42;
  314         struct timeval t;
  315 
  316         val = (l2sc->last_rih << 8) | l2sc->last_ril;
  317 
  318         val += ++called;
  319 
  320         for(i=0; i < 50 ; i++, val++)
  321         {
  322                 getmicrotime(&t);
  323                 val |= l2sc->drv->isdnif+i;
  324                 val <<= i;
  325                 val ^= (t.tv_sec >> 16) ^ t.tv_usec;
  326                 val <<= i;
  327                 val ^= t.tv_sec ^ (t.tv_usec >> 16);
  328 
  329                 if(val != 0 && val != 0xffff)
  330                         break;
  331         }
  332 #endif
  333         l2sc->last_rih = (val >> 8) & 0x00ff;
  334         l2sc->last_ril = val & 0x00ff;
  335 }
  336 
  337 #endif /* NI4BQ921 > 0 */

Cache object: b63de2007baefd7fdd7e212ce078df62


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.