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_q931.c - Q931 received messages handling
28 * --------------------------------------------
30 * $Id: i4b_q931.c,v 1.32 2000/08/24 11:48:58 hm Exp $
32 * $FreeBSD: src/sys/i4b/layer3/i4b_q931.c,v 1.6.2.1 2001/08/10 14:08:42 obrien Exp $
33 * $DragonFly: src/sys/net/i4b/layer3/i4b_q931.c,v 1.3 2003/08/07 21:17:29 dillon Exp $
35 * last edit-date: [Mon May 29 16:56:52 2000]
37 *---------------------------------------------------------------------------*/
40 #include "use_i4bq931.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
52 #include <sys/callout.h>
56 #include <net/i4b/include/machine/i4b_debug.h>
57 #include <net/i4b/include/machine/i4b_ioctl.h>
58 #include <net/i4b/include/machine/i4b_cause.h>
60 #include <i4b/i4b_debug.h>
61 #include <i4b/i4b_ioctl.h>
62 #include <i4b/i4b_cause.h>
65 #include "../include/i4b_isdnq931.h"
66 #include "../include/i4b_l3l4.h"
67 #include "../include/i4b_global.h"
70 #include "i4b_l3fsm.h"
73 #include "../layer4/i4b_l4.h"
76 #define memcpy(d,s,l) bcopy(s,d,l)
79 unsigned int i4b_l3_debug = L3_DEBUG_DEFAULT;
81 call_desc_t call_desc[N_CALL_DESC]; /* call descriptor array */
82 ctrl_desc_t ctrl_desc[MAX_CONTROLLERS]; /* controller description array */
83 int utoc_tab[MAX_CONTROLLERS]; /* unit to controller conversion */
85 /* protocol independent causes -> Q.931 causes */
87 unsigned char cause_tab_q931[CAUSE_I4B_MAX] = {
88 CAUSE_Q850_NCCLR, /* CAUSE_I4B_NORMAL -> normal call clearing */
89 CAUSE_Q850_USRBSY, /* CAUSE_I4B_BUSY -> user busy */
90 CAUSE_Q850_NOCAVAIL, /* CAUSE_I4B_NOCHAN -> no circuit/channel available*/
91 CAUSE_Q850_INCDEST, /* CAUSE_I4B_INCOMP -> incompatible destination */
92 CAUSE_Q850_CALLREJ, /* CAUSE_I4B_REJECT -> call rejected */
93 CAUSE_Q850_DSTOOORDR, /* CAUSE_I4B_OOO -> destination out of order */
94 CAUSE_Q850_TMPFAIL, /* CAUSE_I4B_TMPFAIL -> temporary failure */
95 CAUSE_Q850_USRBSY, /* CAUSE_I4B_L1ERROR -> L1 error / persistent deact XXX */
96 CAUSE_Q850_USRBSY, /* CAUSE_I4B_LLDIAL -> no dialout on leased line XXX */
99 /*---------------------------------------------------------------------------*
100 * setup cr ref flag according to direction
101 *---------------------------------------------------------------------------*/
103 setup_cr(call_desc_t *cd, unsigned char cr)
105 if(cd->crflag == CRF_ORIG)
106 return(cr & 0x7f); /* clear cr ref flag */
107 else if(cd->crflag == CRF_DEST)
108 return(cr | 0x80); /* set cr ref flag */
110 panic("setup_cr: invalid crflag!\n");
113 /*---------------------------------------------------------------------------*
114 * decode and process a Q.931 message
115 *---------------------------------------------------------------------------*/
117 i4b_decode_q931(int unit, int msg_len, u_char *msg_ptr)
120 int codeset = CODESET_0;
121 int old_codeset = CODESET_0;
122 int shift_flag = UNSHIFTED;
130 /* check protocol discriminator */
132 if(*msg_ptr != PD_Q931)
134 static int protoflag = -1; /* print only once .. */
136 if(*msg_ptr != protoflag)
138 NDBGL3(L3_P_ERR, "unknown protocol discriminator 0x%x!", *msg_ptr);
139 protoflag = *msg_ptr;
147 s = SPLI4B(); /* this has to be protected ! */
149 /* extract call reference */
151 crlen = *msg_ptr & CRLENGTH_MASK;
157 crval += *msg_ptr & 0x7f;
158 crflag = (*msg_ptr >> 7) & 0x01;
162 for(i=1; i < crlen; i++)
175 NDBGL3(L3_P_MSG, "Call Ref, len %d, val %d, flag %d", crlen, crval, crflag);
177 /* find or allocate calldescriptor */
179 if((cd = cd_by_unitcr(unit, crval,
180 crflag == CRF_DEST ? CRF_ORIG : CRF_DEST)) == NULL)
182 if(*msg_ptr == SETUP)
184 /* get and init new calldescriptor */
186 cd = reserve_cd(); /* cdid filled in */
187 cd->controller = utoc_tab[unit];
189 cd->crflag = CRF_DEST; /* we are the dest side */
190 cd->ilt = NULL; /* reset link tab ptrs */
195 /*XXX*/ if(crval != 0) /* ignore global call references */
197 NDBGL3(L3_P_ERR, "cannot find calldescriptor for cr = 0x%x, crflag = 0x%x, msg = 0x%x, frame = ", crval, crflag, *msg_ptr);
198 i4b_print_frame(msg_len, msg_ptr);
207 /* decode and handle message type */
209 i4b_decode_q931_message(unit, cd, *msg_ptr);
213 /* process information elements */
217 /* check for shift codeset IE */
219 if((*msg_ptr & 0x80) && ((*msg_ptr & 0xf0) == SOIE_SHIFT))
221 if(!(*msg_ptr & SHIFT_LOCK))
222 shift_flag = SHIFTED;
224 old_codeset = codeset;
225 codeset = *msg_ptr & CODESET_MASK;
227 if((shift_flag != SHIFTED) &&
228 (codeset <= old_codeset))
230 NDBGL3(L3_P_ERR, "Q.931 lockingshift proc violation, shift %d -> %d", old_codeset, codeset);
231 codeset = old_codeset;
237 /* process one IE for selected codeset */
242 offset = i4b_decode_q931_cs0_ie(unit, cd, msg_len, msg_ptr);
248 NDBGL3(L3_P_ERR, "unknown codeset %d, ", codeset);
249 i4b_print_frame(msg_len, msg_ptr);
254 /* check for non-locking shifts */
256 if(shift_flag == SHIFTED)
258 shift_flag = UNSHIFTED;
259 codeset = old_codeset;
262 next_l3state(cd, cd->event);
265 /*---------------------------------------------------------------------------*
266 * decode and process one Q.931 codeset 0 information element
267 *---------------------------------------------------------------------------*/
269 i4b_decode_q931_cs0_ie(int unit, call_desc_t *cd, int msg_len, u_char *msg_ptr)
280 /* single byte IE's */
283 NDBGL3(L3_P_MSG, "IEI_SENDCOMPL");
287 /* multi byte IE's */
289 case IEI_SEGMMSG: /* segmented message */
290 NDBGL3(L3_P_MSG, "IEI_SEGMENTED_MESSAGE");
293 case IEI_BEARERCAP: /* bearer capability */
296 case 0x80: /* speech */
297 case 0x89: /* restricted digital info */
298 case 0x90: /* 3.1KHz audio */
299 /* XXX */ cd->bprot = BPROT_NONE;
300 NDBGL3(L3_P_MSG, "IEI_BEARERCAP - Telephony");
303 case 0x88: /* unrestricted digital info */
304 /* XXX */ cd->bprot = BPROT_RHDLC;
305 NDBGL3(L3_P_MSG, "IEI_BEARERCAP - Raw HDLC");
309 /* XXX */ cd->bprot = BPROT_NONE;
310 NDBGL3(L3_P_ERR, "IEI_BEARERCAP - Unsupported B-Protocol 0x%x", msg_ptr[2]);
315 case IEI_CAUSE: /* cause */
316 if(msg_ptr[2] & 0x80)
318 cd->cause_in = msg_ptr[3] & 0x7f;
319 NDBGL3(L3_P_MSG, "IEI_CAUSE = %d", msg_ptr[3] & 0x7f);
323 cd->cause_in = msg_ptr[4] & 0x7f;
324 NDBGL3(L3_P_MSG, "IEI_CAUSE = %d", msg_ptr[4] & 0x7f);
328 case IEI_CALLID: /* call identity */
329 NDBGL3(L3_P_MSG, "IEI_CALL_IDENTITY");
332 case IEI_CALLSTATE: /* call state */
333 cd->call_state = msg_ptr[2] & 0x3f;
334 NDBGL3(L3_P_MSG, "IEI_CALLSTATE = %d", cd->call_state);
337 case IEI_CHANNELID: /* channel id */
338 if((msg_ptr[2] & 0xf4) != 0x80)
340 cd->channelid = CHAN_NO;
341 NDBGL3(L3_P_ERR, "IEI_CHANNELID, unsupported value 0x%x", msg_ptr[2]);
345 switch(msg_ptr[2] & 0x03)
348 cd->channelid = CHAN_NO;
351 cd->channelid = CHAN_B1;
354 cd->channelid = CHAN_B2;
357 cd->channelid = CHAN_ANY;
360 cd->channelexcl = (msg_ptr[2] & 0x08) >> 3;
362 NDBGL3(L3_P_MSG, "IEI_CHANNELID - channel %d, exclusive = %d", cd->channelid, cd->channelexcl);
364 /* if this is a setup message, reserve channel */
366 if(cd->event == EV_SETUP)
368 if((cd->channelid == CHAN_B1) || (cd->channelid == CHAN_B2))
370 if(ctrl_desc[cd->controller].bch_state[cd->channelid] == BCH_ST_FREE)
371 ctrl_desc[cd->controller].bch_state[cd->channelid] = BCH_ST_RSVD;
373 NDBGL3(L3_P_ERR, "IE ChannelID, Channel NOT free!!");
375 else if(cd->channelid == CHAN_NO)
377 NDBGL3(L3_P_MSG, "IE ChannelID, SETUP with channel = No channel (CW)");
379 else /* cd->channelid == CHAN_ANY */
381 NDBGL3(L3_P_ERR, "ERROR: IE ChannelID, SETUP with channel = Any channel!");
387 case IEI_PROGRESSI: /* progress indicator */
388 NDBGL3(L3_P_MSG, "IEI_PROGRESSINDICATOR");
391 case IEI_NETSPCFAC: /* network specific fac */
392 NDBGL3(L3_P_MSG, "IEI_NETSPCFAC");
395 case IEI_NOTIFIND: /* notification indicator */
396 NDBGL3(L3_P_MSG, "IEI_NOTIFICATION_INDICATOR");
399 case IEI_DISPLAY: /* display */
400 memcpy(cd->display, &msg_ptr[2], min(DISPLAY_MAX, msg_ptr[1]));
401 cd->display[min(DISPLAY_MAX, msg_ptr[1])] = '\0';
402 NDBGL3(L3_P_MSG, "IEI_DISPLAY = %s", cd->display);
405 case IEI_DATETIME: /* date/time */
408 p = &(cd->datetime[0]);
411 for(j = msg_ptr[1]; j > 0; j--, i++)
412 sprintf(p+strlen(p), "%02d", msg_ptr[i]);
414 NDBGL3(L3_P_MSG, "IEI_DATETIME = %s", cd->datetime);
417 case IEI_KEYPAD: /* keypad facility */
418 NDBGL3(L3_P_MSG, "IEI_KEYPAD_FACILITY");
421 case IEI_SIGNAL: /* signal type */
422 NDBGL3(L3_P_MSG, "IEI_SIGNAL = %d", msg_ptr[2]);
425 case IEI_INFRATE: /* information rate */
426 NDBGL3(L3_P_MSG, "IEI_INFORMATION_RATE");
429 case IEI_ETETDEL: /* end to end transit delay */
430 NDBGL3(L3_P_MSG, "IEI_END_TO_END_TRANSIT_DELAY");
433 case IEI_CUG: /* closed user group */
434 NDBGL3(L3_P_MSG, "IEI_CLOSED_USER_GROUP");
437 case IEI_CALLINGPN: /* calling party no */
438 if(msg_ptr[2] & 0x80) /* no presentation/screening indicator ? */
440 memcpy(cd->src_telno, &msg_ptr[3], min(TELNO_MAX, msg_ptr[1]-1));
441 cd->src_telno[min(TELNO_MAX, msg_ptr[1] - 1)] = '\0';
442 cd->scr_ind = SCR_NONE;
443 cd->prs_ind = PRS_NONE;
447 memcpy(cd->src_telno, &msg_ptr[4], min(TELNO_MAX, msg_ptr[1]-2));
448 cd->src_telno[min(TELNO_MAX, msg_ptr[1] - 2)] = '\0';
449 cd->scr_ind = (msg_ptr[3] & 0x03) + SCR_USR_NOSC;
450 cd->prs_ind = ((msg_ptr[3] >> 5) & 0x03) + PRS_ALLOWED;
452 NDBGL3(L3_P_MSG, "IEI_CALLINGPN = %s", cd->src_telno);
455 case IEI_CALLINGPS: /* calling party subaddress */
456 NDBGL3(L3_P_MSG, "IEI_CALLINGPS");
459 case IEI_CALLEDPN: /* called party number */
460 memcpy(cd->dst_telno, &msg_ptr[3], min(TELNO_MAX, msg_ptr[1]-1));
461 cd->dst_telno[min(TELNO_MAX, msg_ptr [1] - 1)] = '\0';
462 NDBGL3(L3_P_MSG, "IEI_CALLED = %s", cd->dst_telno);
465 case IEI_CALLEDPS: /* called party subaddress */
466 NDBGL3(L3_P_MSG, "IEI_CALLEDPS");
469 case IEI_REDIRNO: /* redirecting number */
470 NDBGL3(L3_P_MSG, "IEI_REDIRECTING_NUMBER");
473 case IEI_TRNSEL: /* transit network selection */
474 NDBGL3(L3_P_MSG, "IEI_TRANSIT_NETWORK_SELECTION");
477 case IEI_RESTARTI: /* restart indicator */
478 NDBGL3(L3_P_MSG, "IEI_RESTART_INDICATOR");
481 case IEI_LLCOMPAT: /* low layer compat */
482 NDBGL3(L3_P_MSG, "IEI_LLCOMPAT");
485 case IEI_HLCOMPAT: /* high layer compat */
486 NDBGL3(L3_P_MSG, "IEI_HLCOMPAT");
489 case IEI_USERUSER: /* user-user */
490 NDBGL3(L3_P_MSG, "IEI_USER_USER");
493 case IEI_ESCAPE: /* escape for extension */
494 NDBGL3(L3_P_MSG, "IEI_ESCAPE");
500 case IEI_FACILITY: /* facility */
501 NDBGL3(L3_P_MSG, "IEI_FACILITY");
502 if(i4b_aoc(msg_ptr, cd) > -1)
503 i4b_l4_charging_ind(cd);
509 case IEI_CONCTDNO: /* connected number */
510 NDBGL3(L3_P_MSG, "IEI_CONCTDNO");
515 NDBGL3(L3_P_ERR, "Unknown IE %d - ", *msg_ptr);
516 i4b_print_frame(msg_ptr[1]+2, msg_ptr);
519 return(msg_ptr[1] + 2);
522 /*---------------------------------------------------------------------------*
523 * decode and process one Q.931 codeset 0 information element
524 *---------------------------------------------------------------------------*/
526 i4b_decode_q931_message(int unit, call_desc_t *cd, u_char message_type)
534 /* call establishment */
537 cd->event = EV_ALERT;
541 case CALL_PROCEEDING:
542 cd->event = EV_CALLPRC;
543 m = "CALL_PROCEEDING";
547 cd->event = EV_PROGIND;
553 cd->bprot = BPROT_NONE;
556 cd->dst_telno[0] = '\0';
557 cd->src_telno[0] = '\0';
558 cd->channelid = CHAN_NO;
560 cd->display[0] = '\0';
561 cd->datetime[0] = '\0';
562 cd->event = EV_SETUP;
567 cd->datetime[0] = '\0';
568 cd->event = EV_CONNECT;
571 case SETUP_ACKNOWLEDGE:
572 m = "SETUP_ACKNOWLEDGE";
573 cd->event = EV_SETUPAK;
576 case CONNECT_ACKNOWLEDGE:
577 m = "CONNECT_ACKNOWLEDGE";
578 cd->event = EV_CONACK;
581 /* call information */
583 case USER_INFORMATION:
584 m = "USER_INFORMATION";
588 m = "SUSPEND_REJECT";
607 case HOLD_ACKNOWLEDGE:
608 m = "HOLD_ACKNOWLEDGE";
611 case SUSPEND_ACKNOWLEDGE:
612 m = "SUSPEND_ACKNOWLEDGE";
615 case RESUME_ACKNOWLEDGE:
616 m = "RESUME_ACKNOWLEDGE";
627 case RETRIEVE_ACKNOWLEDGE:
628 m = "RETRIEVE_ACKNOWLEDGE";
631 case RETRIEVE_REJECT:
632 m = "RETRIEVE_REJECT";
639 cd->event = EV_DISCONN;
648 cd->event = EV_RELEASE;
651 case RESTART_ACKNOWLEDGE:
652 m = "RESTART_ACKNOWLEDGE";
655 case RELEASE_COMPLETE:
656 m = "RELEASE_COMPLETE";
657 cd->event = EV_RELCOMP;
668 cd->event = EV_FACILITY;
680 m = "STATUS_ENQUIRY";
681 cd->event = EV_STATENQ;
684 case CONGESTION_CONTROL:
685 m = "CONGESTION_CONTROL";
695 cd->event = EV_STATUS;
699 NDBGL3(L3_P_ERR, "unit %d, cr = 0x%02x, msg = 0x%02x", unit, cd->cr, message_type);
704 NDBGL3(L3_PRIM, "%s: unit %d, cr = 0x%02x\n", m, unit, cd->cr);
708 #endif /* NI4BQ931 > 0 */