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.6 2003/10/03 16:38:44 pooka 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.6 2003/10/03 16:38:44 pooka 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_l2.h>
   83 #include <netisdn/i4b_l2fsm.h>
   84 #include <netisdn/i4b_l3l4.h>
   85 
   86 /*---------------------------------------------------------------------------*
   87  *      handle a received TEI management frame
   88  *---------------------------------------------------------------------------*/
   89 void
   90 i4b_tei_rxframe(l2_softc_t *l2sc, struct isdn_l3_driver *drv, struct mbuf *m)
   91 {
   92         u_char *ptr = m->m_data;
   93         
   94         switch(*(ptr + OFF_MT))
   95         {
   96                 case MT_ID_ASSIGN:
   97                         if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
   98                             (*(ptr + OFF_RIH) == l2sc->last_rih))
   99                         {
  100                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  101                                 l2sc->tei_valid = TEI_VALID;
  102 
  103                                 if(l2sc->T202 == TIMER_ACTIVE)
  104                                         i4b_T202_stop(l2sc);
  105 
  106                                 i4b_mdl_status_ind(drv, STI_TEIASG, l2sc->tei);
  107 
  108                                 log(LOG_INFO, "i4b: isdnif %d, assigned TEI = %d = 0x%02x\n", l2sc->drv->isdnif, l2sc->tei, l2sc->tei);
  109 
  110                                 NDBGL2(L2_TEI_MSG, "TEI ID Assign - TEI = %d", l2sc->tei);
  111 
  112                                 i4b_next_l2state(l2sc, drv, EV_MDASGRQ);
  113                         }
  114                         break;
  115                         
  116                 case MT_ID_DENY:
  117                         if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
  118                             (*(ptr + OFF_RIH) == l2sc->last_rih))
  119                         {
  120                                 l2sc->tei_valid = TEI_INVALID;
  121                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  122 
  123                                 if(l2sc->tei == GROUP_TEI)
  124                                 {
  125                                         log(LOG_WARNING, "i4b: isdnif %d, denied TEI, no TEI values available from exchange!\n", l2sc->drv->isdnif);
  126                                         NDBGL2(L2_TEI_ERR, "TEI ID Denied, No TEI values available from exchange!");
  127                                 }
  128                                 else
  129                                 {
  130                                         log(LOG_WARNING, "i4b: isdnif %d, denied TEI = %d = 0x%02x\n", l2sc->drv->isdnif, l2sc->tei, l2sc->tei);
  131                                         NDBGL2(L2_TEI_ERR, "TEI ID Denied - TEI = %d", l2sc->tei);
  132                                 }                                       
  133                                 i4b_mdl_status_ind(drv, STI_TEIASG, -1);
  134                                 i4b_next_l2state(l2sc, drv, EV_MDERRRS);
  135                         }
  136                         break;
  137                         
  138                 case MT_ID_CHK_REQ:
  139                         if( (l2sc->tei_valid == TEI_VALID) &&
  140                             ( (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI))) ||
  141                               (GROUP_TEI == GET_TEIFROMAI(*(ptr+OFF_AI))) ))
  142                         {
  143                                 static int lasttei = -1;
  144 
  145                                 if(l2sc->tei != lasttei)
  146                                 {
  147                                         NDBGL2(L2_TEI_MSG, "TEI ID Check Req - TEI = %d", l2sc->tei);
  148                                         lasttei = l2sc->tei;
  149                                 }
  150                                 
  151                                 if(l2sc->T202 == TIMER_ACTIVE)
  152                                         i4b_T202_stop(l2sc);
  153                                 i4b_tei_chkresp(l2sc);
  154                         }
  155                         break;
  156                         
  157                 case MT_ID_REMOVE:
  158                         if( (l2sc->tei_valid == TEI_VALID) &&
  159                             ( (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI))) ||
  160                               (l2sc->tei == GET_TEIFROMAI(*(ptr+OFF_AI)))))
  161                         {
  162                                 l2sc->tei_valid = TEI_INVALID;
  163                                 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
  164 
  165                                 log(LOG_INFO, "i4b: isdnif %d, removed TEI = %d = 0x%02x\n", drv->isdnif, l2sc->tei, l2sc->tei);
  166                                 NDBGL2(L2_TEI_MSG, "TEI ID Remove - TEI = %d", l2sc->tei);
  167                                 i4b_mdl_status_ind(drv, STI_TEIASG, -1);
  168                                 i4b_next_l2state(l2sc, drv, EV_MDREMRQ);
  169                         }
  170                         break;
  171                         
  172                 default:
  173                         NDBGL2(L2_TEI_ERR, "UNKNOWN TEI MGMT Frame, type = 0x%x", *(ptr + OFF_MT));
  174                         i4b_print_frame(m->m_len, m->m_data);
  175                         break;
  176         }
  177         i4b_Dfreembuf(m);
  178 }
  179 
  180 /*---------------------------------------------------------------------------*
  181  *      allocate and fill up a TEI management frame for sending
  182  *---------------------------------------------------------------------------*/
  183 static struct mbuf *
  184 build_tei_mgmt_frame(l2_softc_t *l2sc, unsigned char type)
  185 {
  186         struct mbuf *m;
  187         
  188         if((m = i4b_Dgetmbuf(TEI_MGMT_FRM_LEN)) == NULL)
  189                 return(NULL);
  190 
  191         m->m_data[TEIM_SAPIO] = 0xfc;   /* SAPI = 63, CR = 0, EA = 0 */
  192         m->m_data[TEIM_TEIO]  = 0xff;   /* TEI = 127, EA = 1 */
  193         m->m_data[TEIM_UIO]   = UI;     /* UI */
  194         m->m_data[TEIM_MEIO]  = MEI;    /* MEI */
  195         m->m_data[TEIM_MTO]   = type;   /* message type */
  196         
  197         switch(type)
  198         {
  199                 case MT_ID_REQEST:
  200                         i4b_make_rand_ri(l2sc);
  201                         m->m_data[TEIM_RILO] = l2sc->last_ril;
  202                         m->m_data[TEIM_RIHO] = l2sc->last_rih;
  203                         m->m_data[TEIM_AIO] = (GROUP_TEI << 1) | 0x01;
  204                         break;
  205 
  206                 case MT_ID_CHK_RSP:
  207                         i4b_make_rand_ri(l2sc);
  208                         m->m_data[TEIM_RILO] = l2sc->last_ril;
  209                         m->m_data[TEIM_RIHO] = l2sc->last_rih;
  210                         m->m_data[TEIM_AIO] = (l2sc->tei << 1) | 0x01;
  211                         break;
  212                         
  213                 case MT_ID_VERIFY:
  214                         m->m_data[TEIM_RILO] = 0;
  215                         m->m_data[TEIM_RIHO] = 0;
  216                         m->m_data[TEIM_AIO] = (l2sc->tei << 1) | 0x01;
  217                         break;
  218 
  219                 default:
  220                         i4b_Dfreembuf(m);
  221                         panic("build_tei_mgmt_frame: invalid type");
  222                         break;
  223         }
  224         l2sc->stat.tx_tei++;
  225         return(m);
  226 }
  227 
  228 /*---------------------------------------------------------------------------*
  229  *      i4b_tei_assign - TEI assignment procedure (Q.921, 5.3.2, pp 24)
  230  *      T202func and N202 _MUST_ be set prior to calling this function !
  231  *---------------------------------------------------------------------------*/
  232 void
  233 i4b_tei_assign(l2_softc_t *l2sc)
  234 {
  235         struct mbuf *m;
  236 
  237         NDBGL2(L2_TEI_MSG, "tx TEI ID_Request");
  238         
  239         m = build_tei_mgmt_frame(l2sc, MT_ID_REQEST);
  240 
  241         if(m == NULL)
  242                 panic("i4b_tei_assign: no mbuf");               
  243 
  244         i4b_T202_start(l2sc);
  245 
  246         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  247 }
  248 
  249 /*---------------------------------------------------------------------------*
  250  *      i4b_tei_assign - TEI verify procedure (Q.921, 5.3.5, pp 29)
  251  *      T202func and N202 _MUST_ be set prior to calling this function !
  252  *---------------------------------------------------------------------------*/
  253 void
  254 i4b_tei_verify(l2_softc_t *l2sc)
  255 {
  256         struct mbuf *m;
  257 
  258         NDBGL2(L2_TEI_MSG, "tx TEI ID_Verify");
  259 
  260         m = build_tei_mgmt_frame(l2sc, MT_ID_VERIFY);
  261 
  262         if(m == NULL)
  263                 panic("i4b_tei_verify: no mbuf");               
  264 
  265         i4b_T202_start(l2sc);
  266 
  267         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  268 }
  269 
  270 /*---------------------------------------------------------------------------*
  271  *      i4b_tei_chkresp - TEI check response procedure (Q.921, 5.3.5, pp 29)
  272  *---------------------------------------------------------------------------*/
  273 void
  274 i4b_tei_chkresp(l2_softc_t *l2sc)
  275 {
  276         struct mbuf *m;
  277         static int lasttei = 0;
  278 
  279         if(l2sc->tei != lasttei)
  280         {
  281                 lasttei = l2sc->tei;
  282                 NDBGL2(L2_TEI_MSG, "tx TEI ID_Check_Response");
  283         }
  284 
  285         m = build_tei_mgmt_frame(l2sc, MT_ID_CHK_RSP);
  286 
  287         if(m == NULL)
  288                 panic("i4b_tei_chkresp: no mbuf");              
  289 
  290         l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
  291 }
  292 
  293 /*---------------------------------------------------------------------------*
  294  *      generate some 16 bit "random" number used for TEI mgmt Ri field
  295  *---------------------------------------------------------------------------*/
  296 void
  297 i4b_make_rand_ri(l2_softc_t *l2sc)
  298 {
  299 
  300 #if defined(__FreeBSD__)
  301 
  302         u_short val;
  303 
  304 #ifdef RANDOMDEV
  305         read_random((char *)&val, sizeof(val));
  306 #else
  307         val = (u_short)random();
  308 #endif /* RANDOMDEV */ 
  309 
  310 #else
  311 
  312         register u_short val;
  313         register int i;
  314         static int called = 42;
  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                 val |= l2sc->drv->isdnif+i;
  323                 val <<= i;
  324                 val ^= (time.tv_sec >> 16) ^ time.tv_usec;
  325                 val <<= i;
  326                 val ^= time.tv_sec ^ (time.tv_usec >> 16);
  327 
  328                 if(val != 0 && val != 0xffff)
  329                         break;
  330         }
  331 #endif
  332         l2sc->last_rih = (val >> 8) & 0x00ff;
  333         l2sc->last_ril = val & 0x00ff;
  334 }
  335 
  336 #endif /* NI4BQ921 > 0 */

Cache object: 09ffbea76b99235de5070c5fa2ab4f64


[ 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.