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
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
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 $
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 $");
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/syslog.h>
53 #include <sys/socket.h>
56 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
57 #include <sys/callout.h>
60 #if defined(__FreeBSD__)
61 #if defined (__FreeBSD_version) && __FreeBSD_version <= 400000
62 #include <machine/random.h>
64 #include <sys/random.h>
69 #include <machine/i4b_debug.h>
70 #include <machine/i4b_ioctl.h>
72 #include <netisdn/i4b_debug.h>
73 #include <netisdn/i4b_ioctl.h>
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 *---------------------------------------------------------------------------*/
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
))
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
)
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
);
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!");
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
);
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
);
150 if(l2sc
->T202
== TIMER_ACTIVE
)
152 i4b_tei_chkresp(l2sc
);
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
);
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
);
179 /*---------------------------------------------------------------------------*
180 * allocate and fill up a TEI management frame for sending
181 *---------------------------------------------------------------------------*/
183 build_tei_mgmt_frame(l2_softc_t
*l2sc
, unsigned char type
)
187 if((m
= i4b_Dgetmbuf(TEI_MGMT_FRM_LEN
)) == 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 */
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;
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;
213 m
->m_data
[TEIM_RILO
] = 0;
214 m
->m_data
[TEIM_RIHO
] = 0;
215 m
->m_data
[TEIM_AIO
] = (l2sc
->tei
<< 1) | 0x01;
220 panic("build_tei_mgmt_frame: invalid type");
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 *---------------------------------------------------------------------------*/
232 i4b_tei_assign(l2_softc_t
*l2sc
)
236 NDBGL2(L2_TEI_MSG
, "tx TEI ID_Request");
238 m
= build_tei_mgmt_frame(l2sc
, MT_ID_REQEST
);
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 *---------------------------------------------------------------------------*/
253 i4b_tei_verify(l2_softc_t
*l2sc
)
257 NDBGL2(L2_TEI_MSG
, "tx TEI ID_Verify");
259 m
= build_tei_mgmt_frame(l2sc
, MT_ID_VERIFY
);
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 *---------------------------------------------------------------------------*/
273 i4b_tei_chkresp(l2_softc_t
*l2sc
)
276 static int lasttei
= 0;
278 if(l2sc
->tei
!= lasttei
)
281 NDBGL2(L2_TEI_MSG
, "tx TEI ID_Check_Response");
284 m
= build_tei_mgmt_frame(l2sc
, MT_ID_CHK_RSP
);
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 *---------------------------------------------------------------------------*/
296 i4b_make_rand_ri(l2_softc_t
*l2sc
)
299 #if defined(__FreeBSD__)
304 read_random((char *)&val
, sizeof(val
));
306 val
= (u_short
)random();
307 #endif /* RANDOMDEV */
311 register u_short val
;
313 static int called
= 42;
316 val
= (l2sc
->last_rih
<< 8) | l2sc
->last_ril
;
320 for(i
=0; i
< 50 ; i
++, val
++)
323 val
|= l2sc
->drv
->isdnif
+i
;
325 val
^= (t
.tv_sec
>> 16) ^ t
.tv_usec
;
327 val
^= t
.tv_sec
^ (t
.tv_usec
>> 16);
329 if(val
!= 0 && val
!= 0xffff)
333 l2sc
->last_rih
= (val
>> 8) & 0x00ff;
334 l2sc
->last_ril
= val
& 0x00ff;
337 #endif /* NI4BQ921 > 0 */