Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netisdn / i4b_tei.c
blob7b6e9010a7559f9cbbf978370f84d7d04eb18a7c
1 /*
2 * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
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.
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.
25 *---------------------------------------------------------------------------
27 * i4b_tei.c - tei handling procedures
28 * -----------------------------------
30 * $Id: i4b_tei.c,v 1.10 2007/01/24 13:08:15 hubertf Exp $
32 * $FreeBSD$
34 * last edit-date: [Fri Jan 5 11:33:47 2001]
36 *---------------------------------------------------------------------------*/
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: i4b_tei.c,v 1.9 2006/06/07 22:34:04 kardel Exp $");
41 #ifdef __FreeBSD__
42 #include "i4bq921.h"
43 #else
44 #define NI4BQ921 1
45 #endif
46 #if NI4BQ921 > 0
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>
56 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
57 #include <sys/callout.h>
58 #endif
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
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
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>
82 #include <netisdn/i4b_l2fsm.h>
83 #include <netisdn/i4b_l3l4.h>
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)
91 u_char *ptr = m->m_data;
93 switch(*(ptr + OFF_MT))
95 case MT_ID_ASSIGN:
96 if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
97 (*(ptr + OFF_RIH) == l2sc->last_rih))
99 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
100 l2sc->tei_valid = TEI_VALID;
102 if(l2sc->T202 == TIMER_ACTIVE)
103 i4b_T202_stop(l2sc);
105 i4b_mdl_status_ind(drv, STI_TEIASG, l2sc->tei);
107 log(LOG_INFO, "i4b: isdnif %d, assigned TEI = %d = 0x%02x\n", l2sc->drv->isdnif, l2sc->tei, l2sc->tei);
109 NDBGL2(L2_TEI_MSG, "TEI ID Assign - TEI = %d", l2sc->tei);
111 i4b_next_l2state(l2sc, drv, EV_MDASGRQ);
113 break;
115 case MT_ID_DENY:
116 if( (*(ptr + OFF_RIL) == l2sc->last_ril) &&
117 (*(ptr + OFF_RIH) == l2sc->last_rih))
119 l2sc->tei_valid = TEI_INVALID;
120 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
122 if(l2sc->tei == GROUP_TEI)
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!");
127 else
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);
132 i4b_mdl_status_ind(drv, STI_TEIASG, -1);
133 i4b_next_l2state(l2sc, drv, EV_MDERRRS);
135 break;
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))) ))
142 static int lasttei = -1;
144 if(l2sc->tei != lasttei)
146 NDBGL2(L2_TEI_MSG, "TEI ID Check Req - TEI = %d", l2sc->tei);
147 lasttei = l2sc->tei;
150 if(l2sc->T202 == TIMER_ACTIVE)
151 i4b_T202_stop(l2sc);
152 i4b_tei_chkresp(l2sc);
154 break;
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)))))
161 l2sc->tei_valid = TEI_INVALID;
162 l2sc->tei = GET_TEIFROMAI(*(ptr+OFF_AI));
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);
169 break;
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;
176 i4b_Dfreembuf(m);
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)
185 struct mbuf *m;
187 if((m = i4b_Dgetmbuf(TEI_MGMT_FRM_LEN)) == NULL)
188 return(NULL);
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 */
196 switch(type)
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;
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;
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;
218 default:
219 i4b_Dfreembuf(m);
220 panic("build_tei_mgmt_frame: invalid type");
221 break;
223 l2sc->stat.tx_tei++;
224 return(m);
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)
234 struct mbuf *m;
236 NDBGL2(L2_TEI_MSG, "tx TEI ID_Request");
238 m = build_tei_mgmt_frame(l2sc, MT_ID_REQEST);
240 if(m == NULL)
241 panic("i4b_tei_assign: no mbuf");
243 i4b_T202_start(l2sc);
245 l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
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)
255 struct mbuf *m;
257 NDBGL2(L2_TEI_MSG, "tx TEI ID_Verify");
259 m = build_tei_mgmt_frame(l2sc, MT_ID_VERIFY);
261 if(m == NULL)
262 panic("i4b_tei_verify: no mbuf");
264 i4b_T202_start(l2sc);
266 l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
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)
275 struct mbuf *m;
276 static int lasttei = 0;
278 if(l2sc->tei != lasttei)
280 lasttei = l2sc->tei;
281 NDBGL2(L2_TEI_MSG, "tx TEI ID_Check_Response");
284 m = build_tei_mgmt_frame(l2sc, MT_ID_CHK_RSP);
286 if(m == NULL)
287 panic("i4b_tei_chkresp: no mbuf");
289 l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
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)
299 #if defined(__FreeBSD__)
301 u_short val;
303 #ifdef RANDOMDEV
304 read_random((char *)&val, sizeof(val));
305 #else
306 val = (u_short)random();
307 #endif /* RANDOMDEV */
309 #else
311 register u_short val;
312 register int i;
313 static int called = 42;
314 struct timeval t;
316 val = (l2sc->last_rih << 8) | l2sc->last_ril;
318 val += ++called;
320 for(i=0; i < 50 ; i++, val++)
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);
329 if(val != 0 && val != 0xffff)
330 break;
332 #endif
333 l2sc->last_rih = (val >> 8) & 0x00ff;
334 l2sc->last_ril = val & 0x00ff;
337 #endif /* NI4BQ921 > 0 */