2 * Copyright (c) 1997, 2001 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_l2if.c - Layer 3 interface to Layer 2
28 * -------------------------------------------
30 * $Id: i4b_l2if.c,v 1.23 2000/08/24 11:48:58 hm Exp $
32 * $FreeBSD: src/sys/i4b/layer3/i4b_l2if.c,v 1.6.2.2 2001/12/16 15:12:58 hm Exp $
34 * last edit-date: [Mon May 29 16:56:22 2000]
36 *---------------------------------------------------------------------------*/
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <machine/i4b_debug.h>
46 #include <machine/i4b_ioctl.h>
47 #include <machine/i4b_cause.h>
49 #include <i4b/include/i4b_isdnq931.h>
50 #include <i4b/include/i4b_l2l3.h>
51 #include <i4b/include/i4b_l3l4.h>
52 #include <i4b/include/i4b_mbuf.h>
54 #include <i4b/layer3/i4b_l3.h>
55 #include <i4b/layer3/i4b_l3fsm.h>
56 #include <i4b/layer3/i4b_q931.h>
59 static unsigned char make_q931_cause(cause_t cause);
61 /*---------------------------------------------------------------------------*
62 * this converts our internal state (number) to the number specified
63 * in Q.931 and is used for reporting our state in STATUS messages.
64 *---------------------------------------------------------------------------*/
65 int i4b_status_tab[] = {
84 /*---------------------------------------------------------------------------*
85 * return a valid q.931/q.850 cause from any of the internal causes
86 *---------------------------------------------------------------------------*/
88 make_q931_cause(cause_t cause)
90 register unsigned char ret;
92 switch(GET_CAUSE_TYPE(cause))
95 ret = GET_CAUSE_VAL(cause);
98 ret = cause_tab_q931[GET_CAUSE_VAL(cause)];
101 panic("make_q931_cause: unknown cause type!");
108 /*---------------------------------------------------------------------------*
109 * return status of data link
110 *---------------------------------------------------------------------------*/
112 i4b_get_dl_stat(call_desc_t *cd)
114 return(ctrl_desc[cd->controller].dl_est);
117 /*---------------------------------------------------------------------------*
118 * DL ESTABLISH INDICATION from Layer 2
119 *---------------------------------------------------------------------------*/
121 i4b_dl_establish_ind(int unit)
126 NDBGL2(L2_PRIM, "DL-ESTABLISH-IND unit %d",unit);
128 /* first set DL up in controller descriptor */
130 for(i=0; i < nctrl; i++)
132 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
133 (ctrl_desc[i].unit == unit))
135 NDBGL3(L3_MSG, "unit=%d DL established!",unit);
136 ctrl_desc[i].dl_est = DL_UP;
143 NDBGL3(L3_ERR, "ERROR, controller not found for unit=%d!",unit);
149 /* second, inform all (!) active call of the event */
151 for(i=0; i < N_CALL_DESC; i++)
153 if( (call_desc[i].cdid != 0) &&
154 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
155 (ctrl_desc[call_desc[i].controller].unit == unit))
157 NDBGL3(L3_MSG, "unit=%d, index=%d cdid=%u cr=%d",
158 unit, i, call_desc[i].cdid, call_desc[i].cr);
159 next_l3state(&call_desc[i], EV_DLESTIN);
166 NDBGL3(L3_ERR, "ERROR, no cdid for unit %d found!", unit);
175 /*---------------------------------------------------------------------------*
176 * DL ESTABLISH CONFIRM from Layer 2
177 *---------------------------------------------------------------------------*/
179 i4b_dl_establish_cnf(int unit)
184 NDBGL2(L2_PRIM, "DL-ESTABLISH-CONF unit %d",unit);
186 for(i=0; i < N_CALL_DESC; i++)
188 if( (call_desc[i].cdid != 0) &&
189 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
190 (ctrl_desc[call_desc[i].controller].unit == unit))
192 ctrl_desc[call_desc[i].controller].dl_est = DL_UP;
194 NDBGL3(L3_MSG, "unit=%d, index=%d cdid=%u cr=%d",
195 unit, i, call_desc[i].cdid, call_desc[i].cr);
197 next_l3state(&call_desc[i], EV_DLESTCF);
204 NDBGL3(L3_ERR, "ERROR, no cdid for unit %d found!", unit);
213 /*---------------------------------------------------------------------------*
214 * DL RELEASE INDICATION from Layer 2
215 *---------------------------------------------------------------------------*/
217 i4b_dl_release_ind(int unit)
222 NDBGL2(L2_PRIM, "DL-RELEASE-IND unit %d",unit);
224 /* first set controller to down */
226 for(i=0; i < nctrl; i++)
228 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
229 (ctrl_desc[i].unit == unit))
231 NDBGL3(L3_MSG, "unit=%d DL released!",unit);
232 ctrl_desc[i].dl_est = DL_DOWN;
239 NDBGL3(L3_ERR, "ERROR, controller not found for unit=%d!",unit);
245 /* second, inform all (!) active calls of the event */
247 for(i=0; i < N_CALL_DESC; i++)
249 if( (call_desc[i].cdid != 0) &&
250 (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
251 (ctrl_desc[call_desc[i].controller].unit == unit))
253 NDBGL3(L3_MSG, "unit=%d, index=%d cdid=%u cr=%d",
254 unit, i, call_desc[i].cdid, call_desc[i].cr);
255 next_l3state(&call_desc[i], EV_DLRELIN);
262 /* this is not an error since it might be a normal call end */
263 NDBGL3(L3_MSG, "no cdid for unit %d found", unit);
268 /*---------------------------------------------------------------------------*
269 * DL RELEASE CONFIRM from Layer 2
270 *---------------------------------------------------------------------------*/
272 i4b_dl_release_cnf(int unit)
276 NDBGL2(L2_PRIM, "DL-RELEASE-CONF unit %d",unit);
278 for(i=0; i < nctrl; i++)
280 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
281 (ctrl_desc[i].unit == unit))
283 NDBGL3(L3_MSG, "unit=%d DL released!",unit);
284 ctrl_desc[i].dl_est = DL_DOWN;
288 NDBGL3(L3_ERR, "ERROR, controller not found for unit=%d!",unit);
292 /*---------------------------------------------------------------------------*
293 * i4b_dl_data_ind - process a rx'd I-frame got from layer 2
294 *---------------------------------------------------------------------------*/
296 i4b_dl_data_ind(int unit, struct mbuf *m)
299 NDBGL2(L2_PRIM, "DL-DATA-IND unit %d",unit);
301 i4b_decode_q931(unit, m->m_len, m->m_data);
306 /*---------------------------------------------------------------------------*
307 * dl_unit_data_ind - process a rx'd U-frame got from layer 2
308 *---------------------------------------------------------------------------*/
310 i4b_dl_unit_data_ind(int unit, struct mbuf *m)
313 NDBGL2(L2_PRIM, "DL-UNIT-DATA-IND unit %d",unit);
315 i4b_decode_q931(unit, m->m_len, m->m_data);
320 /*---------------------------------------------------------------------------*
321 * send CONNECT message
322 *---------------------------------------------------------------------------*/
324 i4b_l3_tx_connect(call_desc_t *cd)
329 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
331 if((m = i4b_Dgetmbuf(I_FRAME_HDRLEN + MSG_CONNECT_LEN)) == NULL)
332 panic("i4b_l3_tx_connect: can't allocate mbuf\n");
334 ptr = m->m_data + I_FRAME_HDRLEN;
336 *ptr++ = PD_Q931; /* protocol discriminator */
337 *ptr++ = 0x01; /* call reference length */
338 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
339 *ptr++ = CONNECT; /* message type = connect */
341 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
344 /*---------------------------------------------------------------------------*
345 * send RELEASE COMPLETE message
346 *---------------------------------------------------------------------------*/
348 i4b_l3_tx_release_complete(call_desc_t *cd, int send_cause_flag)
352 int len = I_FRAME_HDRLEN + MSG_RELEASE_COMPLETE_LEN;
354 if(send_cause_flag == 0)
357 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x",
358 ctrl_desc[cd->controller].unit, cd->cr);
362 NDBGL3(L3_PRIM, "unit=%d, cr=0x%02x, cause=0x%x",
363 ctrl_desc[cd->controller].unit, cd->cr, cd->cause_out);
366 if((m = i4b_Dgetmbuf(len)) == NULL)
367 panic("i4b_l3_tx_release_complete: can't allocate mbuf\n");
369 ptr = m->m_data + I_FRAME_HDRLEN;
371 *ptr++ = PD_Q931; /* protocol discriminator */
372 *ptr++ = 0x01; /* call reference length */
373 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
374 *ptr++ = RELEASE_COMPLETE; /* message type = release complete */
378 *ptr++ = IEI_CAUSE; /* cause ie */
380 *ptr++ = CAUSE_STD_LOC_OUT;
381 *ptr++ = make_q931_cause(cd->cause_out);
384 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
387 /*---------------------------------------------------------------------------*
388 * send DISCONNECT message
389 *---------------------------------------------------------------------------*/
391 i4b_l3_tx_disconnect(call_desc_t *cd)
396 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
398 if((m = i4b_Dgetmbuf(I_FRAME_HDRLEN + MSG_DISCONNECT_LEN)) == NULL)
399 panic("i4b_l3_tx_disconnect: can't allocate mbuf\n");
401 ptr = m->m_data + I_FRAME_HDRLEN;
403 *ptr++ = PD_Q931; /* protocol discriminator */
404 *ptr++ = 0x01; /* call ref length */
405 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
406 *ptr++ = DISCONNECT; /* message type = disconnect */
408 *ptr++ = IEI_CAUSE; /* cause ie */
410 *ptr++ = CAUSE_STD_LOC_OUT;
411 *ptr++ = make_q931_cause(cd->cause_out);
413 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
416 /*---------------------------------------------------------------------------*
418 *---------------------------------------------------------------------------*/
420 i4b_l3_tx_setup(call_desc_t *cd)
425 int slen = strlen(cd->src_telno);
426 int dlen = strlen(cd->dst_telno);
427 int klen = strlen(cd->keypad);
430 * there is one additional octet if cd->bprot == BPROT_NONE
431 * NOTE: the selection of a bearer capability by a B L1
432 * protocol is highly questionable and a better
433 * mechanism should be used in future. (-hm)
436 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
438 len = I_FRAME_HDRLEN +
440 (slen ? (3+slen) : 0) +
441 (dlen ? (3+dlen) : 0) +
442 (klen ? (2+klen) : 0) +
443 (cd->bprot == BPROT_NONE ? 1 : 0);
445 if((m = i4b_Dgetmbuf(len)) == NULL)
447 panic("i4b_l3_tx_setup: can't allocate mbuf\n");
450 cd->crflag = CRF_ORIG; /* we are the originating side */
452 ptr = m->m_data + I_FRAME_HDRLEN;
454 *ptr++ = PD_Q931; /* protocol discriminator */
455 *ptr++ = 0x01; /* call ref length */
456 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
457 *ptr++ = SETUP; /* message type = setup */
459 *ptr++ = IEI_SENDCOMPL; /* sending complete */
461 *ptr++ = IEI_BEARERCAP; /* bearer capability */
464 * currently i have no idea if this should be switched by
465 * the choosen B channel protocol or if there should be a
466 * separate configuration item for the bearer capability.
467 * For now, it is switched by the choosen b protocol (-hm)
472 case BPROT_NONE: /* telephony */
473 *ptr++ = IEI_BEARERCAP_LEN+1;
474 *ptr++ = IT_CAP_SPEECH;
475 *ptr++ = IT_RATE_64K;
476 *ptr++ = IT_UL1_G711A;
479 case BPROT_RHDLC: /* raw HDLC */
480 *ptr++ = IEI_BEARERCAP_LEN;
481 *ptr++ = IT_CAP_UNR_DIG_INFO;
482 *ptr++ = IT_RATE_64K;
486 *ptr++ = IEI_BEARERCAP_LEN;
487 *ptr++ = IT_CAP_UNR_DIG_INFO;
488 *ptr++ = IT_RATE_64K;
492 *ptr++ = IEI_CHANNELID; /* channel id */
493 *ptr++ = IEI_CHANNELID_LEN; /* channel id length */
495 switch(cd->channelid)
498 *ptr++ = CHANNELID_B1;
501 *ptr++ = CHANNELID_B2;
504 *ptr++ = CHANNELID_ANY;
510 *ptr++ = IEI_KEYPAD; /* keypad facility */
511 *ptr++ = klen; /* keypad facility length */
512 strncpy(ptr, cd->keypad, klen);
518 *ptr++ = IEI_CALLINGPN; /* calling party no */
519 *ptr++ = IEI_CALLINGPN_LEN+slen;/* calling party no length */
520 *ptr++ = NUMBER_TYPEPLAN; /* type of number, number plan id */
521 strncpy(ptr, cd->src_telno, slen);
527 *ptr++ = IEI_CALLEDPN; /* called party no */
528 *ptr++ = IEI_CALLEDPN_LEN+dlen; /* called party no length */
529 *ptr++ = NUMBER_TYPEPLAN; /* type of number, number plan id */
530 strncpy(ptr, cd->dst_telno, dlen);
534 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
537 /*---------------------------------------------------------------------------*
538 * send CONNECT ACKNOWLEDGE message
539 *---------------------------------------------------------------------------*/
541 i4b_l3_tx_connect_ack(call_desc_t *cd)
546 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
548 if((m = i4b_Dgetmbuf(I_FRAME_HDRLEN + MSG_CONNECT_ACK_LEN)) == NULL)
549 panic("i4b_l3_tx_connect_ack: can't allocate mbuf\n");
551 ptr = m->m_data + I_FRAME_HDRLEN;
553 *ptr++ = PD_Q931; /* protocol discriminator */
554 *ptr++ = 0x01; /* call reference length */
555 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
556 *ptr++ = CONNECT_ACKNOWLEDGE; /* message type = connect ack */
558 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
561 /*---------------------------------------------------------------------------*
562 * send STATUS message
563 *---------------------------------------------------------------------------*/
565 i4b_l3_tx_status(call_desc_t *cd, u_char q850cause)
570 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
572 if((m = i4b_Dgetmbuf(I_FRAME_HDRLEN + MSG_STATUS_LEN)) == NULL)
573 panic("i4b_l3_tx_status: can't allocate mbuf\n");
575 ptr = m->m_data + I_FRAME_HDRLEN;
577 *ptr++ = PD_Q931; /* protocol discriminator */
578 *ptr++ = 0x01; /* call reference length */
579 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
580 *ptr++ = STATUS; /* message type = connect ack */
582 *ptr++ = IEI_CAUSE; /* cause ie */
584 *ptr++ = CAUSE_STD_LOC_OUT;
585 *ptr++ = q850cause | EXT_LAST;
587 *ptr++ = IEI_CALLSTATE; /* call state ie */
588 *ptr++ = CALLSTATE_LEN;
589 *ptr++ = i4b_status_tab[cd->Q931state];
591 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
594 /*---------------------------------------------------------------------------*
595 * send RELEASE message
596 *---------------------------------------------------------------------------*/
598 i4b_l3_tx_release(call_desc_t *cd, int send_cause_flag)
602 int len = I_FRAME_HDRLEN + MSG_RELEASE_LEN;
604 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
606 if(send_cause_flag == 0)
609 if((m = i4b_Dgetmbuf(len)) == NULL)
610 panic("i4b_l3_tx_release: can't allocate mbuf\n");
612 ptr = m->m_data + I_FRAME_HDRLEN;
614 *ptr++ = PD_Q931; /* protocol discriminator */
615 *ptr++ = 0x01; /* call reference length */
616 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
617 *ptr++ = RELEASE; /* message type = release complete */
621 *ptr++ = IEI_CAUSE; /* cause ie */
623 *ptr++ = CAUSE_STD_LOC_OUT;
624 *ptr++ = make_q931_cause(cd->cause_out);
627 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
630 /*---------------------------------------------------------------------------*
631 * send ALERTING message
632 *---------------------------------------------------------------------------*/
634 i4b_l3_tx_alert(call_desc_t *cd)
639 if((m = i4b_Dgetmbuf(I_FRAME_HDRLEN + MSG_ALERT_LEN)) == NULL)
640 panic("i4b_l3_tx_alert: can't allocate mbuf\n");
642 NDBGL3(L3_PRIM, "unit %d, cr = 0x%02x", ctrl_desc[cd->controller].unit, cd->cr);
644 ptr = m->m_data + I_FRAME_HDRLEN;
646 *ptr++ = PD_Q931; /* protocol discriminator */
647 *ptr++ = 0x01; /* call reference length */
648 *ptr++ = setup_cr(cd, cd->cr); /* call reference value */
649 *ptr++ = ALERT; /* message type = alert */
651 DL_Data_Req(ctrl_desc[cd->controller].unit, m);
654 #endif /* NI4BQ931 > 0 */