1 /*
2  * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
3  *
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.
12  *
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.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *	i4b_uframe.c - routines for handling U-frames
28  *	-----------------------------------------------
29  *
30  *	$Id: i4b_uframe.c,v 1.1 2003/04/06 04:37:51 tg Exp $
31  *
32  * $FreeBSD$
33  *
34  *      last edit-date: [Fri Jan  5 11:33:47 2001]
35  *
36  *---------------------------------------------------------------------------*/
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: i4b_uframe.c,v 1.5 2002/05/21 10:31:11 martin Exp $");
40 
41 #ifdef __FreeBSD__
42 #include "i4bq921.h"
43 #else
44 #define	NI4BQ921	1
45 #endif
46 #if NI4BQ921 > 0
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
54 
55 #include <sys/timeout.h>
56 
57 #ifdef __FreeBSD__
58 #include <machine/i4b_debug.h>
59 #include <machine/i4b_ioctl.h>
60 #else
61 #include <netisdn/i4b_debug.h>
62 #include <netisdn/i4b_ioctl.h>
63 #endif
64 
65 #include <netisdn/i4b_l2.h>
66 #include <netisdn/i4b_l1l2.h>
67 #include <netisdn/i4b_isdnq931.h>
68 #include <netisdn/i4b_mbuf.h>
69 
70 #include <netisdn/i4b_l2.h>
71 #include <netisdn/i4b_l2fsm.h>
72 
73 /*---------------------------------------------------------------------------*
74  *	process a received U-frame
75  *---------------------------------------------------------------------------*/
76 void
i4b_rxd_u_frame(l2_softc_t * l2sc,struct isdn_l3_driver * drv,struct mbuf * m)77 i4b_rxd_u_frame(l2_softc_t *l2sc, struct isdn_l3_driver *drv, struct mbuf *m)
78 {
79 	u_char *ptr = m->m_data;
80 
81 	int sapi = GETSAPI(*(ptr + OFF_SAPI));
82 	int tei = GETTEI(*(ptr + OFF_TEI));
83 	int pfbit = GETUPF(*(ptr + OFF_CNTL));
84 
85 	switch(*(ptr + OFF_CNTL) & ~UPFBIT)
86 	{
87 		/* commands */
88 
89 		case SABME:
90 			if((l2sc->tei_valid == TEI_VALID) &&
91 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
92 			{
93 				l2sc->stat.rx_sabme++;
94 				NDBGL2(L2_U_MSG, "SABME, sapi = %d, tei = %d", sapi, tei);
95 				l2sc->rxd_PF = pfbit;
96 				i4b_next_l2state(l2sc, drv, EV_RXSABME);
97 			}
98 			i4b_Dfreembuf(m);
99 			break;
100 
101 		case UI:
102 			if(sapi == SAPI_L2M &&
103 			   tei == GROUP_TEI &&
104 			   *(ptr + OFF_MEI) == MEI)
105 			{
106 				/* layer 2 management (SAPI = 63) */
107 				l2sc->stat.rx_tei++;
108 				i4b_tei_rxframe(l2sc, drv, m);
109 			}
110 			else if(sapi == SAPI_CCP && tei == GROUP_TEI)
111 			{
112 				/* call control (SAPI = 0) */
113 				l2sc->stat.rx_ui++;
114 				/* strip ui header */
115 				m_adj(m, UI_HDR_LEN);
116 				/* to upper layer */
117 				i4b_dl_unit_data_ind(l2sc->drv, m);
118 			}
119 			else
120 			{
121 				l2sc->stat.err_rx_badui++;
122 				NDBGL2(L2_U_ERR, "unknown UI frame!");
123 				i4b_Dfreembuf(m);
124 			}
125 			break;
126 
127 		case DISC:
128 			if((l2sc->tei_valid == TEI_VALID) &&
129 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
130 			{
131 				l2sc->stat.rx_disc++;
132 				NDBGL2(L2_U_MSG, "DISC, sapi = %d, tei = %d", sapi, tei);
133 				l2sc->rxd_PF = pfbit;
134 				i4b_next_l2state(l2sc, drv, EV_RXDISC);
135 			}
136 			i4b_Dfreembuf(m);
137 			break;
138 
139 		case XID:
140 			if((l2sc->tei_valid == TEI_VALID) &&
141 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
142 			{
143 				l2sc->stat.rx_xid++;
144 				NDBGL2(L2_U_MSG, "XID, sapi = %d, tei = %d", sapi, tei);
145 			}
146 			i4b_Dfreembuf(m);
147 			break;
148 
149 		/* responses */
150 
151 		case DM:
152 			if((l2sc->tei_valid == TEI_VALID) &&
153 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
154 			{
155 				l2sc->stat.rx_dm++;
156 				NDBGL2(L2_U_MSG, "DM, sapi = %d, tei = %d", sapi, tei);
157 				i4b_print_frame(m->m_len, m->m_data);
158 				l2sc->rxd_PF = pfbit;
159 				i4b_next_l2state(l2sc, drv, EV_RXDM);
160 			}
161 			i4b_Dfreembuf(m);
162 			break;
163 
164 		case UA:
165 			if((l2sc->tei_valid == TEI_VALID) &&
166 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
167 			{
168 				l2sc->stat.rx_ua++;
169 				NDBGL2(L2_U_MSG, "UA, sapi = %d, tei = %d", sapi, tei);
170 				l2sc->rxd_PF = pfbit;
171 				i4b_next_l2state(l2sc, drv, EV_RXUA);
172 			}
173 			i4b_Dfreembuf(m);
174 			break;
175 
176 		case FRMR:
177 			if((l2sc->tei_valid == TEI_VALID) &&
178 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
179 			{
180 				l2sc->stat.rx_frmr++;
181 				NDBGL2(L2_U_MSG, "FRMR, sapi = %d, tei = %d", sapi, tei);
182 				l2sc->rxd_PF = pfbit;
183 				i4b_next_l2state(l2sc, drv, EV_RXFRMR);
184 			}
185 			i4b_Dfreembuf(m);
186 			break;
187 
188 		default:
189 			if((l2sc->tei_valid == TEI_VALID) &&
190 			   (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
191 			{
192 				NDBGL2(L2_U_ERR, "UNKNOWN TYPE ERROR, sapi = %d, tei = %d, frame = ", sapi, tei);
193 				i4b_print_frame(m->m_len, m->m_data);
194 			}
195 			else
196 			{
197 				NDBGL2(L2_U_ERR, "not mine -  UNKNOWN TYPE ERROR, sapi = %d, tei = %d, frame = ", sapi, tei);
198 				i4b_print_frame(m->m_len, m->m_data);
199 			}
200 			l2sc->stat.err_rx_badui++;
201 			i4b_Dfreembuf(m);
202 			break;
203 	}
204 }
205 
206 /*---------------------------------------------------------------------------*
207  *	build U-frame for sending
208  *---------------------------------------------------------------------------*/
209 struct mbuf *
i4b_build_u_frame(l2_softc_t * l2sc,crbit_to_nt_t crbit,pbit_t pbit,u_char type)210 i4b_build_u_frame(l2_softc_t *l2sc, crbit_to_nt_t crbit, pbit_t pbit, u_char type)
211 {
212 	struct mbuf *m;
213 
214 	if((m = i4b_Dgetmbuf(U_FRAME_LEN)) == NULL)
215 		return(NULL);
216 
217 	PUTSAPI(SAPI_CCP, crbit, m->m_data[OFF_SAPI]);
218 
219 	PUTTEI(l2sc->tei, m->m_data[OFF_TEI]);
220 
221 	if(pbit)
222 		m->m_data[OFF_CNTL] = type | UPBITSET;
223 	else
224 		m->m_data[OFF_CNTL] = type & ~UPBITSET;
225 
226 	return(m);
227 }
228 
229 /*---------------------------------------------------------------------------*
230  *	transmit SABME command
231  *---------------------------------------------------------------------------*/
232 void
i4b_tx_sabme(l2_softc_t * l2sc,pbit_t pbit)233 i4b_tx_sabme(l2_softc_t *l2sc, pbit_t pbit)
234 {
235 	struct mbuf *m;
236 
237 	l2sc->stat.tx_sabme++;
238 	NDBGL2(L2_U_MSG, "tx SABME, tei = %d", l2sc->tei);
239 	m = i4b_build_u_frame(l2sc, CR_CMD_TO_NT, pbit, SABME);
240 	l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
241 }
242 
243 /*---------------------------------------------------------------------------*
244  *	transmit DM response
245  *---------------------------------------------------------------------------*/
246 void
i4b_tx_dm(l2_softc_t * l2sc,fbit_t fbit)247 i4b_tx_dm(l2_softc_t *l2sc, fbit_t fbit)
248 {
249 	struct mbuf *m;
250 
251 	l2sc->stat.tx_dm++;
252 	NDBGL2(L2_U_MSG, "tx DM, tei = %d", l2sc->tei);
253 	m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, DM);
254 	l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
255 }
256 
257 /*---------------------------------------------------------------------------*
258  *	transmit DISC command
259  *---------------------------------------------------------------------------*/
260 void
i4b_tx_disc(l2_softc_t * l2sc,pbit_t pbit)261 i4b_tx_disc(l2_softc_t *l2sc, pbit_t pbit)
262 {
263 	struct mbuf *m;
264 
265 	l2sc->stat.tx_disc++;
266 	NDBGL2(L2_U_MSG, "tx DISC, tei = %d", l2sc->tei);
267 	m = i4b_build_u_frame(l2sc, CR_CMD_TO_NT, pbit, DISC);
268 	l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
269 }
270 
271 /*---------------------------------------------------------------------------*
272  *	transmit UA response
273  *---------------------------------------------------------------------------*/
274 void
i4b_tx_ua(l2_softc_t * l2sc,fbit_t fbit)275 i4b_tx_ua(l2_softc_t *l2sc, fbit_t fbit)
276 {
277 	struct mbuf *m;
278 
279 	l2sc->stat.tx_ua++;
280 	NDBGL2(L2_U_MSG, "tx UA, tei = %d", l2sc->tei);
281 	m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, UA);
282 	l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
283 }
284 
285 /*---------------------------------------------------------------------------*
286  *	transmit FRMR response
287  *---------------------------------------------------------------------------*/
288 void
i4b_tx_frmr(l2_softc_t * l2sc,fbit_t fbit)289 i4b_tx_frmr(l2_softc_t *l2sc, fbit_t fbit)
290 {
291 	struct mbuf *m;
292 
293 	l2sc->stat.tx_frmr++;
294 	NDBGL2(L2_U_MSG, "tx FRMR, tei = %d", l2sc->tei);
295 	m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, FRMR);
296 	l2sc->driver->ph_data_req(l2sc->l1_token, m, MBUF_FREE);
297 }
298 
299 #endif /* NI4BQ921 > 0 */
300