2 * Copyright (c) 1997, 2002 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
26 /*---------------------------------------------------------------------------
28 * i4b_q931.c - Q931 received messages handling
29 * --------------------------------------------
30 * last edit-date: [Sun Aug 11 19:18:08 2002]
32 *---------------------------------------------------------------------------*/
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <i4b/include/i4b_debug.h>
42 #include <i4b/include/i4b_ioctl.h>
43 #include <i4b/include/i4b_cause.h>
45 #include <i4b/include/i4b_isdnq931.h>
46 #include <i4b/include/i4b_l3l4.h>
47 #include <i4b/include/i4b_global.h>
49 #include <i4b/layer3/i4b_l3.h>
50 #include <i4b/layer3/i4b_l3fsm.h>
51 #include <i4b/layer3/i4b_q931.h>
53 #include <i4b/layer4/i4b_l4.h>
55 unsigned int i4b_l3_debug = L3_DEBUG_DEFAULT;
57 ctrl_desc_t ctrl_desc[MAX_CONTROLLERS]; /* controller description array */
58 int utoc_tab[MAX_CONTROLLERS]; /* unit to controller conversion */
60 /* protocol independent causes -> Q.931 causes */
62 unsigned char cause_tab_q931[CAUSE_I4B_MAX] = {
63 CAUSE_Q850_NCCLR, /* CAUSE_I4B_NORMAL -> normal call clearing */
64 CAUSE_Q850_USRBSY, /* CAUSE_I4B_BUSY -> user busy */
65 CAUSE_Q850_NOCAVAIL, /* CAUSE_I4B_NOCHAN -> no circuit/channel available*/
66 CAUSE_Q850_INCDEST, /* CAUSE_I4B_INCOMP -> incompatible destination */
67 CAUSE_Q850_CALLREJ, /* CAUSE_I4B_REJECT -> call rejected */
68 CAUSE_Q850_DSTOOORDR, /* CAUSE_I4B_OOO -> destination out of order */
69 CAUSE_Q850_TMPFAIL, /* CAUSE_I4B_TMPFAIL -> temporary failure */
70 CAUSE_Q850_USRBSY, /* CAUSE_I4B_L1ERROR -> L1 error / persistent deact XXX */
71 CAUSE_Q850_USRBSY, /* CAUSE_I4B_LLDIAL -> no dialout on leased line XXX */
74 /*---------------------------------------------------------------------------*
75 * setup cr ref flag according to direction
76 *---------------------------------------------------------------------------*/
78 setup_cr(call_desc_t *cd, unsigned char cr)
80 if(cd->crflag == CRF_ORIG)
81 return(cr & 0x7f); /* clear cr ref flag */
82 else if(cd->crflag == CRF_DEST)
83 return(cr | 0x80); /* set cr ref flag */
85 panic("setup_cr: invalid crflag!\n");
88 /*---------------------------------------------------------------------------*
89 * decode and process a Q.931 message
90 *---------------------------------------------------------------------------*/
92 i4b_decode_q931(int unit, int msg_len, u_char *msg_ptr)
95 int codeset = CODESET_0;
96 int old_codeset = CODESET_0;
97 int shift_flag = UNSHIFTED;
105 /* check protocol discriminator */
107 if(*msg_ptr != PD_Q931)
109 static int protoflag = -1; /* print only once .. */
111 if(*msg_ptr != protoflag)
113 NDBGL3(L3_P_ERR, "unknown protocol discriminator 0x%x!", *msg_ptr);
114 protoflag = *msg_ptr;
122 s = SPLI4B(); /* this has to be protected ! */
124 /* extract call reference */
126 crlen = *msg_ptr & CRLENGTH_MASK;
132 crval += *msg_ptr & 0x7f;
133 crflag = (*msg_ptr >> 7) & 0x01;
137 for(i=1; i < crlen; i++)
150 NDBGL3(L3_P_MSG, "Call Ref, len %d, val %d, flag %d", crlen, crval, crflag);
152 /* find or allocate calldescriptor */
154 if((cd = cd_by_unitcr(unit, crval,
155 crflag == CRF_DEST ? CRF_ORIG : CRF_DEST)) == NULL)
157 if(*msg_ptr == SETUP)
159 /* get and init new calldescriptor */
161 cd = reserve_cd(); /* cdid filled in */
162 cd->controller = utoc_tab[unit];
164 cd->crflag = CRF_DEST; /* we are the dest side */
165 cd->ilt = NULL; /* reset link tab ptrs */
170 /*XXX*/ if(crval != 0) /* ignore global call references */
172 NDBGL3(L3_P_ERR, "cannot find calldescriptor for cr = 0x%x, crflag = 0x%x, msg = 0x%x, frame = ", crval, crflag, *msg_ptr);
173 i4b_print_frame(msg_len, msg_ptr);
182 /* decode and handle message type */
184 i4b_decode_q931_message(unit, cd, *msg_ptr);
188 /* process information elements */
192 /* check for shift codeset IE */
194 if((*msg_ptr & 0x80) && ((*msg_ptr & 0xf0) == SOIE_SHIFT))
196 if(!(*msg_ptr & SHIFT_LOCK))
197 shift_flag = SHIFTED;
199 old_codeset = codeset;
200 codeset = *msg_ptr & CODESET_MASK;
202 if((shift_flag != SHIFTED) &&
203 (codeset <= old_codeset))
205 NDBGL3(L3_P_ERR, "Q.931 lockingshift proc violation, shift %d -> %d", old_codeset, codeset);
206 codeset = old_codeset;
212 /* process one IE for selected codeset */
217 offset = i4b_decode_q931_cs0_ie(unit, cd, msg_len, msg_ptr);
223 NDBGL3(L3_P_ERR, "unknown codeset %d, ", codeset);
224 i4b_print_frame(msg_len, msg_ptr);
229 /* check for non-locking shifts */
231 if(shift_flag == SHIFTED)
233 shift_flag = UNSHIFTED;
234 codeset = old_codeset;
237 next_l3state(cd, cd->event);
240 /*---------------------------------------------------------------------------*
241 * decode and process one Q.931 codeset 0 information element
242 *---------------------------------------------------------------------------*/
244 i4b_decode_q931_cs0_ie(int unit, call_desc_t *cd, int msg_len, u_char *msg_ptr)
255 /* single byte IE's */
258 NDBGL3(L3_P_MSG, "IEI_SENDCOMPL");
262 /* multi byte IE's */
264 case IEI_SEGMMSG: /* segmented message */
265 NDBGL3(L3_P_MSG, "IEI_SEGMENTED_MESSAGE");
268 case IEI_BEARERCAP: /* bearer capability */
271 case 0x80: /* speech */
272 case 0x89: /* restricted digital info */
273 case 0x90: /* 3.1KHz audio */
274 /* XXX */ cd->bprot = BPROT_NONE;
275 NDBGL3(L3_P_MSG, "IEI_BEARERCAP - Telephony");
278 case 0x88: /* unrestricted digital info */
279 /* XXX */ cd->bprot = BPROT_RHDLC;
280 NDBGL3(L3_P_MSG, "IEI_BEARERCAP - Raw HDLC");
284 /* XXX */ cd->bprot = BPROT_NONE;
285 NDBGL3(L3_P_ERR, "IEI_BEARERCAP - Unsupported B-Protocol 0x%x", msg_ptr[2]);
290 case IEI_CAUSE: /* cause */
291 if(msg_ptr[2] & 0x80)
293 cd->cause_in = msg_ptr[3] & 0x7f;
294 NDBGL3(L3_P_MSG, "IEI_CAUSE = %d", msg_ptr[3] & 0x7f);
298 cd->cause_in = msg_ptr[4] & 0x7f;
299 NDBGL3(L3_P_MSG, "IEI_CAUSE = %d", msg_ptr[4] & 0x7f);
303 case IEI_CALLID: /* call identity */
304 NDBGL3(L3_P_MSG, "IEI_CALL_IDENTITY");
307 case IEI_CALLSTATE: /* call state */
308 cd->call_state = msg_ptr[2] & 0x3f;
309 NDBGL3(L3_P_MSG, "IEI_CALLSTATE = %d", cd->call_state);
312 case IEI_CHANNELID: /* channel id */
313 if((msg_ptr[2] & 0xf4) != 0x80)
315 cd->channelid = CHAN_NO;
316 NDBGL3(L3_P_ERR, "IEI_CHANNELID, unsupported value 0x%x", msg_ptr[2]);
320 switch(msg_ptr[2] & 0x03)
323 cd->channelid = CHAN_NO;
326 cd->channelid = CHAN_B1;
329 cd->channelid = CHAN_B2;
332 cd->channelid = CHAN_ANY;
335 cd->channelexcl = (msg_ptr[2] & 0x08) >> 3;
337 NDBGL3(L3_P_MSG, "IEI_CHANNELID - channel %d, exclusive = %d", cd->channelid, cd->channelexcl);
339 /* if this is a setup message, reserve channel */
341 if(cd->event == EV_SETUP)
343 if((cd->channelid == CHAN_B1) || (cd->channelid == CHAN_B2))
345 if(ctrl_desc[cd->controller].bch_state[cd->channelid] == BCH_ST_FREE)
346 ctrl_desc[cd->controller].bch_state[cd->channelid] = BCH_ST_RSVD;
348 NDBGL3(L3_P_ERR, "IE ChannelID, Channel NOT free!!");
350 else if(cd->channelid == CHAN_NO)
352 NDBGL3(L3_P_MSG, "IE ChannelID, SETUP with channel = No channel (CW)");
354 else /* cd->channelid == CHAN_ANY */
356 NDBGL3(L3_P_ERR, "ERROR: IE ChannelID, SETUP with channel = Any channel!");
362 case IEI_PROGRESSI: /* progress indicator */
363 NDBGL3(L3_P_MSG, "IEI_PROGRESSINDICATOR");
366 case IEI_NETSPCFAC: /* network specific fac */
367 NDBGL3(L3_P_MSG, "IEI_NETSPCFAC");
370 case IEI_NOTIFIND: /* notification indicator */
371 NDBGL3(L3_P_MSG, "IEI_NOTIFICATION_INDICATOR");
374 case IEI_DISPLAY: /* display */
375 memcpy(cd->display, &msg_ptr[2], min(DISPLAY_MAX, msg_ptr[1]));
376 cd->display[min(DISPLAY_MAX, msg_ptr[1])] = '\0';
377 NDBGL3(L3_P_MSG, "IEI_DISPLAY = %s", cd->display);
380 case IEI_DATETIME: /* date/time */
383 p = &(cd->datetime[0]);
386 for(j = msg_ptr[1]; j > 0; j--, i++)
387 sprintf(p+strlen(p), "%02d", msg_ptr[i]);
389 NDBGL3(L3_P_MSG, "IEI_DATETIME = %s", cd->datetime);
392 case IEI_KEYPAD: /* keypad facility */
393 NDBGL3(L3_P_MSG, "IEI_KEYPAD_FACILITY");
396 case IEI_SIGNAL: /* signal type */
397 NDBGL3(L3_P_MSG, "IEI_SIGNAL = %d", msg_ptr[2]);
400 case IEI_INFRATE: /* information rate */
401 NDBGL3(L3_P_MSG, "IEI_INFORMATION_RATE");
404 case IEI_ETETDEL: /* end to end transit delay */
405 NDBGL3(L3_P_MSG, "IEI_END_TO_END_TRANSIT_DELAY");
408 case IEI_CUG: /* closed user group */
409 NDBGL3(L3_P_MSG, "IEI_CLOSED_USER_GROUP");
412 case IEI_CALLINGPN: /* calling party no */
413 if(msg_ptr[2] & 0x80) /* no presentation/screening indicator ? */
415 memcpy(cd->src_telno, &msg_ptr[3], min(TELNO_MAX, msg_ptr[1]-1));
416 cd->src_telno[min(TELNO_MAX, msg_ptr[1] - 1)] = '\0';
417 cd->scr_ind = SCR_NONE;
418 cd->prs_ind = PRS_NONE;
422 memcpy(cd->src_telno, &msg_ptr[4], min(TELNO_MAX, msg_ptr[1]-2));
423 cd->src_telno[min(TELNO_MAX, msg_ptr[1] - 2)] = '\0';
424 cd->scr_ind = (msg_ptr[3] & 0x03) + SCR_USR_NOSC;
425 cd->prs_ind = ((msg_ptr[3] >> 5) & 0x03) + PRS_ALLOWED;
428 /* type of number (source) */
429 switch ((msg_ptr[2] & 0x70) >> 4)
432 cd->src_ton = TON_INTERNAT;
435 cd->src_ton = TON_NATIONAL;
438 cd->src_ton = TON_OTHER;
441 NDBGL3(L3_P_MSG, "IEI_CALLINGPN = %s", cd->src_telno);
444 case IEI_CALLINGPS: /* calling party subaddress */
445 memcpy(cd->src_subaddr, &msg_ptr[3], min(SUBADDR_MAX, msg_ptr[1]-1));
446 cd->src_subaddr[min(SUBADDR_MAX, msg_ptr[1] - 1)] = '\0';
447 NDBGL3(L3_P_MSG, "IEI_CALLINGPS = %s", cd->src_subaddr);
450 case IEI_CALLEDPN: /* called party number */
451 memcpy(cd->dst_telno, &msg_ptr[3], min(TELNO_MAX, msg_ptr[1]-1));
452 cd->dst_telno[min(TELNO_MAX, msg_ptr[1] - 1)] = '\0';
454 /* type of number (destination) */
455 switch ((msg_ptr[2] & 0x70) >> 4)
458 cd->dst_ton = TON_INTERNAT;
461 cd->dst_ton = TON_NATIONAL;
464 cd->dst_ton = TON_OTHER;
468 NDBGL3(L3_P_MSG, "IEI_CALLED = %s", cd->dst_telno);
471 case IEI_CALLEDPS: /* called party subaddress */
472 memcpy(cd->dst_subaddr, &msg_ptr[3], min(SUBADDR_MAX, msg_ptr[1]-1));
473 cd->dst_subaddr[min(SUBADDR_MAX, msg_ptr[1] - 1)] = '\0';
474 NDBGL3(L3_P_MSG, "IEI_CALLEDPS = %s", cd->dst_subaddr);
477 case IEI_REDIRNO: /* redirecting number */
478 NDBGL3(L3_P_MSG, "IEI_REDIRECTING_NUMBER");
481 case IEI_TRNSEL: /* transit network selection */
482 NDBGL3(L3_P_MSG, "IEI_TRANSIT_NETWORK_SELECTION");
485 case IEI_RESTARTI: /* restart indicator */
486 NDBGL3(L3_P_MSG, "IEI_RESTART_INDICATOR");
489 case IEI_LLCOMPAT: /* low layer compat */
490 NDBGL3(L3_P_MSG, "IEI_LLCOMPAT");
493 case IEI_HLCOMPAT: /* high layer compat */
494 NDBGL3(L3_P_MSG, "IEI_HLCOMPAT");
497 case IEI_USERUSER: /* user-user */
498 NDBGL3(L3_P_MSG, "IEI_USER_USER");
501 case IEI_ESCAPE: /* escape for extension */
502 NDBGL3(L3_P_MSG, "IEI_ESCAPE");
508 case IEI_FACILITY: /* facility */
509 NDBGL3(L3_P_MSG, "IEI_FACILITY");
510 if(i4b_aoc(msg_ptr, cd) > -1)
511 i4b_l4_charging_ind(cd);
517 case IEI_CONCTDNO: /* connected number */
518 NDBGL3(L3_P_MSG, "IEI_CONCTDNO");
523 NDBGL3(L3_P_ERR, "Unknown IE %d - ", *msg_ptr);
524 i4b_print_frame(msg_ptr[1]+2, msg_ptr);
527 return(msg_ptr[1] + 2);
530 /*---------------------------------------------------------------------------*
531 * decode and process one Q.931 codeset 0 information element
532 *---------------------------------------------------------------------------*/
534 i4b_decode_q931_message(int unit, call_desc_t *cd, u_char message_type)
542 /* call establishment */
545 cd->event = EV_ALERT;
549 case CALL_PROCEEDING:
550 cd->event = EV_CALLPRC;
551 m = "CALL_PROCEEDING";
555 cd->event = EV_PROGIND;
561 cd->bprot = BPROT_NONE;
564 cd->dst_telno[0] = '\0';
565 cd->src_telno[0] = '\0';
566 cd->channelid = CHAN_NO;
568 cd->display[0] = '\0';
569 cd->datetime[0] = '\0';
570 cd->event = EV_SETUP;
575 cd->datetime[0] = '\0';
576 cd->event = EV_CONNECT;
579 case SETUP_ACKNOWLEDGE:
580 m = "SETUP_ACKNOWLEDGE";
581 cd->event = EV_SETUPAK;
584 case CONNECT_ACKNOWLEDGE:
585 m = "CONNECT_ACKNOWLEDGE";
586 cd->event = EV_CONACK;
589 /* call information */
591 case USER_INFORMATION:
592 m = "USER_INFORMATION";
596 m = "SUSPEND_REJECT";
615 case HOLD_ACKNOWLEDGE:
616 m = "HOLD_ACKNOWLEDGE";
619 case SUSPEND_ACKNOWLEDGE:
620 m = "SUSPEND_ACKNOWLEDGE";
623 case RESUME_ACKNOWLEDGE:
624 m = "RESUME_ACKNOWLEDGE";
635 case RETRIEVE_ACKNOWLEDGE:
636 m = "RETRIEVE_ACKNOWLEDGE";
639 case RETRIEVE_REJECT:
640 m = "RETRIEVE_REJECT";
647 cd->event = EV_DISCONN;
656 cd->event = EV_RELEASE;
659 case RESTART_ACKNOWLEDGE:
660 m = "RESTART_ACKNOWLEDGE";
663 case RELEASE_COMPLETE:
664 m = "RELEASE_COMPLETE";
665 cd->event = EV_RELCOMP;
676 cd->event = EV_FACILITY;
688 m = "STATUS_ENQUIRY";
689 cd->event = EV_STATENQ;
692 case CONGESTION_CONTROL:
693 m = "CONGESTION_CONTROL";
703 cd->event = EV_STATUS;
707 NDBGL3(L3_P_ERR, "unit %d, cr = 0x%02x, msg = 0x%02x", unit, cd->cr, message_type);
712 NDBGL3(L3_PRIM, "%s: unit %d, cr = 0x%02x\n", m, unit, cd->cr);