blob: 0c3b4698fab7a0e3161a31ab2a4a0e67ad48dbd0 [file] [log] [blame]
Harald Welted38f1052011-02-05 19:13:00 +01001/* OpenBSC minimal LAPD implementation */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +01002
Harald Welted38f1052011-02-05 19:13:00 +01003/* (C) 2009 by oystein@homelien.no
4 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010024 */
25
26#include <stdio.h>
27#include <string.h>
28#include <assert.h>
Harald Welte1a00d822011-02-11 18:34:51 +010029#include <errno.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010030
31#include "lapd.h"
Harald Welted38f1052011-02-05 19:13:00 +010032
33#include <osmocore/linuxlist.h>
34#include <osmocore/talloc.h>
Harald Welte1a00d822011-02-11 18:34:51 +010035#include <osmocore/msgb.h>
Harald Welted38f1052011-02-05 19:13:00 +010036#include <openbsc/debug.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010037
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010038typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010039 LAPD_TEI_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010040
41 LAPD_TEI_ASSIGNED,
42
43 LAPD_TEI_ACTIVE,
44} lapd_tei_state;
45
46const char *lapd_tei_states[] = {
47 "NONE",
48 "ASSIGNED",
49 "ACTIVE",
50};
51
52typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010053 LAPD_TYPE_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010054
55 LAPD_TYPE_I,
56 LAPD_TYPE_S,
57 LAPD_TYPE_U,
58} lapd_msg_type;
59
60typedef enum {
Harald Welte7e859bc2011-02-04 20:36:50 +010061 /* commands/responses */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010062 LAPD_CMD_NONE = 0,
63
64 LAPD_CMD_I,
65 LAPD_CMD_RR,
66 LAPD_CMD_RNR,
67 LAPD_CMD_REJ,
68
69 LAPD_CMD_SABME,
70 LAPD_CMD_DM,
71 LAPD_CMD_UI,
72 LAPD_CMD_DISC,
73 LAPD_CMD_UA,
74 LAPD_CMD_FRMR,
75 LAPD_CMD_XID,
76} lapd_cmd_type;
77
78const char *lapd_cmd_types[] = {
79 "NONE",
80
81 "I",
82 "RR",
83 "RNR",
84 "REJ",
85
86 "SABME",
87 "DM",
88 "UI",
89 "DISC",
90 "UA",
91 "FRMR",
92 "XID",
93
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010094};
95
Harald Welte30fe6412011-02-04 20:34:08 +010096const char *lapd_msg_types = "?ISU";
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010097
Harald Welted38f1052011-02-05 19:13:00 +010098struct lapd_tei {
99 struct llist_head list;
100
101 uint8_t tei;
Harald Welte7e859bc2011-02-04 20:36:50 +0100102 /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */
103 int vs; /* next to be transmitted */
104 int va; /* last acked by peer */
105 int vr; /* next expected to be received */
Harald Welte30fe6412011-02-04 20:34:08 +0100106 lapd_tei_state state;
Harald Welted38f1052011-02-05 19:13:00 +0100107};
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100108
Harald Welte7e859bc2011-02-04 20:36:50 +0100109/* 3.5.2.2 Send state variable V(S)
110 * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame
111 * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can
112 * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each
113 * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of
114 * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127.
115 *
116 * 3.5.2.3 Acknowledge state variable V(A)
117 * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame
118 * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been
119 * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can
120 * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values
121 * received from its peer (see 3.5.2.6). A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤
122 * V(S).
123 *
124 * 3.5.2.5 Receive state variable V(R)
125 * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame
126 * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the
127 * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1.
128 * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame
129 * whose N(S) equals V(R).
130 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100131#define LAPD_NS(teip) (teip->vs)
132#define LAPD_NR(teip) (teip->vr)
133
Harald Welte7e859bc2011-02-04 20:36:50 +0100134/* 3.5.2.4 Send sequence number N(S)
135 * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in-
136 * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S).
137 *
138 * 3.5.2.6 Receive sequence number N(R)
139 * All I frames and supervisory frames contain N(R), the expected send sequence number of the next
140 * received I frame. At the time that a frame of the above types is designated for transmission, the value
141 * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has
142 * correctly received all I frames numbered up to and including N(R) − 1.
143 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100144
Harald Welted38f1052011-02-05 19:13:00 +0100145static struct lapd_tei *teip_from_tei(struct lapd_instance *li, uint8_t tei)
Harald Welte30fe6412011-02-04 20:34:08 +0100146{
Harald Welted38f1052011-02-05 19:13:00 +0100147 struct lapd_tei *lt;
148
149 llist_for_each_entry(lt, &li->tei_list, list) {
150 if (lt->tei == tei)
151 return lt;
152 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100153 return NULL;
154};
155
Harald Welted38f1052011-02-05 19:13:00 +0100156static void lapd_tei_set_state(struct lapd_tei *teip, int newstate)
Harald Welte30fe6412011-02-04 20:34:08 +0100157{
Harald Welte0abc11a2011-02-05 17:16:26 +0100158 DEBUGP(DMI, "state change on tei %d: %s -> %s\n", teip->tei,
Harald Welte30fe6412011-02-04 20:34:08 +0100159 lapd_tei_states[teip->state], lapd_tei_states[newstate]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100160 teip->state = newstate;
161};
162
Harald Welte1a00d822011-02-11 18:34:51 +0100163
164int lapd_tei_alloc(struct lapd_instance *li, uint8_t tei)
165{
166 struct lapd_tei *teip;
167
168 teip = talloc_zero(li, struct lapd_tei);
169 if (!teip)
170 return -ENOMEM;
171
172 teip->tei = tei;
173 llist_add(&teip->list, &li->tei_list);
174
175 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
176
177 return 0;
178}
179
Harald Welted38f1052011-02-05 19:13:00 +0100180static void lapd_tei_receive(struct lapd_instance *li, uint8_t *data, int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100181{
Harald Welte8fc66a02011-02-05 19:51:05 +0100182 uint8_t entity = data[0];
183 uint8_t ref = data[1];
184 uint8_t mt = data[3];
185 uint8_t action = data[4] >> 1;
186 uint8_t e = data[4] & 1;
Harald Welted38f1052011-02-05 19:13:00 +0100187 uint8_t resp[8];
188 struct lapd_tei *teip;
189
Harald Welte8fc66a02011-02-05 19:51:05 +0100190 DEBUGP(DMI, "TEIMGR: entity %x, ref %x, mt %x, action %x, e %x\n", entity, ref, mt, action, e);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100191
192 switch (mt) {
Harald Welted38f1052011-02-05 19:13:00 +0100193 case 0x01: /* IDENTITY REQUEST */
Harald Welte8fc66a02011-02-05 19:51:05 +0100194 DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action);
Harald Welte716d2a42011-02-05 17:29:05 +0100195
Harald Welte8fc66a02011-02-05 19:51:05 +0100196 teip = teip_from_tei(li, action);
Harald Welted38f1052011-02-05 19:13:00 +0100197 if (!teip) {
Harald Welte8fc66a02011-02-05 19:51:05 +0100198 LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action);
Harald Welte1a00d822011-02-11 18:34:51 +0100199 lapd_tei_alloc(li, action);
Harald Welte30fe6412011-02-04 20:34:08 +0100200 }
Harald Welted38f1052011-02-05 19:13:00 +0100201
202 /* Send ACCEPT */
203 memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8);
Harald Welte8fc66a02011-02-05 19:51:05 +0100204 resp[7] = (action << 1) | 1;
Harald Welted38f1052011-02-05 19:13:00 +0100205 li->transmit_cb(resp, 8, li->cbdata);
206
207 if (teip->state == LAPD_TEI_NONE)
208 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
209 break;
Harald Welte30fe6412011-02-04 20:34:08 +0100210 default:
Harald Welte8fc66a02011-02-05 19:51:05 +0100211 LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n",
Harald Welte0ae57552011-02-05 18:33:12 +0100212 mt, action);
213 break;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100214 };
215};
216
Harald Welted38f1052011-02-05 19:13:00 +0100217uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len,
218 int *ilen, lapd_mph_type *prim)
Harald Welte30fe6412011-02-04 20:34:08 +0100219{
Harald Welte716d2a42011-02-05 17:29:05 +0100220 uint8_t sapi, cr, tei, command;
221 int pf, ns, nr;
222 uint8_t *contents;
Harald Welted38f1052011-02-05 19:13:00 +0100223 struct lapd_tei *teip;
Harald Welte716d2a42011-02-05 17:29:05 +0100224
225 uint8_t resp[8];
226 int l = 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100227
228 *ilen = 0;
229 *prim = 0;
230
231 if (len < 2) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100232 DEBUGP(DMI, "len %d < 2\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100233 return NULL;
234 };
235
236 if ((data[0] & 1) != 0 || (data[1] & 1) != 1) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100237 DEBUGP(DMI, "address field %x/%x not well formed\n", data[0],
Harald Welte30fe6412011-02-04 20:34:08 +0100238 data[1]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100239 return NULL;
240 };
241
Harald Welte716d2a42011-02-05 17:29:05 +0100242 sapi = data[0] >> 2;
243 cr = (data[0] >> 1) & 1;
244 tei = data[1] >> 1;
Harald Welted38f1052011-02-05 19:13:00 +0100245 command = li->network_side ^ cr;
Harald Welte0abc11a2011-02-05 17:16:26 +0100246 //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100247
248 if (len < 3) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100249 DEBUGP(DMI, "len %d < 3\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100250 return NULL;
251 };
252
253 lapd_msg_type typ = 0;
254 lapd_cmd_type cmd = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100255 pf = -1;
256 ns = -1;
257 nr = -1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100258 if ((data[2] & 1) == 0) {
259 typ = LAPD_TYPE_I;
260 assert(len >= 4);
261 ns = data[2] >> 1;
262 nr = data[3] >> 1;
263 pf = data[3] & 1;
264 cmd = LAPD_CMD_I;
265 } else if ((data[2] & 3) == 1) {
266 typ = LAPD_TYPE_S;
267 assert(len >= 4);
268 nr = data[3] >> 1;
269 pf = data[3] & 1;
270 switch (data[2]) {
Harald Welte30fe6412011-02-04 20:34:08 +0100271 case 0x1:
272 cmd = LAPD_CMD_RR;
273 break;
274 case 0x5:
275 cmd = LAPD_CMD_RNR;
276 break;
277 case 0x9:
278 cmd = LAPD_CMD_REJ;
279 break;
280 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100281 LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]);
282 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100283 };
284 } else if ((data[2] & 3) == 3) {
285 typ = LAPD_TYPE_U;
286 pf = (data[2] >> 4) & 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100287 int val = data[2] & ~(1 << 4);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100288 switch (val) {
Harald Welte30fe6412011-02-04 20:34:08 +0100289 case 0x6f:
290 cmd = LAPD_CMD_SABME;
291 break;
292 case 0x0f:
293 cmd = LAPD_CMD_DM;
294 break;
295 case 0x03:
296 cmd = LAPD_CMD_UI;
297 break;
298 case 0x43:
299 cmd = LAPD_CMD_DISC;
300 break;
301 case 0x63:
302 cmd = LAPD_CMD_UA;
303 break;
304 case 0x87:
305 cmd = LAPD_CMD_FRMR;
306 break;
307 case 0xaf:
308 cmd = LAPD_CMD_XID;
309 break;
310
311 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100312 LOGP(DMI, LOGL_ERROR, "unknown U cmd %x "
313 "(pf %x data %x)\n", val, pf, data[2]);
314 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100315 };
316 };
Harald Welte30fe6412011-02-04 20:34:08 +0100317
Harald Welte716d2a42011-02-05 17:29:05 +0100318 contents = &data[4];
Harald Welte30fe6412011-02-04 20:34:08 +0100319 if (typ == LAPD_TYPE_U)
320 contents--;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100321 *ilen = len - (contents - data);
Harald Welte30fe6412011-02-04 20:34:08 +0100322
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100323 if (tei == 127)
Harald Welted38f1052011-02-05 19:13:00 +0100324 lapd_tei_receive(li, contents, *ilen);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100325
Harald Welted38f1052011-02-05 19:13:00 +0100326 teip = teip_from_tei(li, tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100327
Harald Welte0abc11a2011-02-05 17:16:26 +0100328 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
329 "ilen %d teip %p vs %d va %d vr %d len %d\n",
Harald Welte30fe6412011-02-04 20:34:08 +0100330 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
331 ns, nr, *ilen, teip, teip ? teip->vs : -1, teip ? teip->va : -1,
332 teip ? teip->vr : -1, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100333
Harald Welte716d2a42011-02-05 17:29:05 +0100334 if (!teip) {
335 LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei);
336 return NULL;
337 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100338
Harald Welte716d2a42011-02-05 17:29:05 +0100339 switch (cmd) {
340 case LAPD_CMD_I:
341 if (ns != teip->vr) {
342 DEBUGP(DMI, "ns %d != vr %d\n", ns, teip->vr);
343 if (ns == ((teip->vr - 1) & 0x7f)) {
344 DEBUGP(DMI, "DOUBLE FRAME, ignoring\n");
345 cmd = 0; // ignore
346 } else {
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100347 assert(0);
Harald Welte716d2a42011-02-05 17:29:05 +0100348 };
349 } else {
350 //printf("IN SEQUENCE\n");
351 teip->vr = (ns + 1) & 0x7f; // FIXME: hack!
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100352 };
Harald Welte716d2a42011-02-05 17:29:05 +0100353
354 break;
355 case LAPD_CMD_UI:
356 break;
357 case LAPD_CMD_SABME:
358 teip->vs = 0;
359 teip->vr = 0;
360 teip->va = 0;
361
362 // ua
363 resp[l++] = data[0];
364 resp[l++] = (tei << 1) | 1;
365 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100366 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100367 if (teip->state != LAPD_TEI_ACTIVE) {
368 if (teip->state == LAPD_TEI_ASSIGNED) {
369 lapd_tei_set_state(teip,
370 LAPD_TEI_ACTIVE);
371 //printf("ASSIGNED and ACTIVE\n");
372 } else {
373#if 0
374 DEBUGP(DMI, "rr in strange state, send rej\n");
375
376 // rej
Harald Welted38f1052011-02-05 19:13:00 +0100377 resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100378 resp[l++] = (tei << 1) | 1;
379 resp[l++] = 0x09; //rej
380 resp[l++] = ((teip->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100381 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100382 pf = 0; // dont reply
383#endif
384 };
385 };
386
387 *prim = LAPD_MPH_ACTIVATE_IND;
388 break;
Harald Welte1a00d822011-02-11 18:34:51 +0100389 case LAPD_CMD_UA:
390 teip->vs = 0;
391 teip->vr = 0;
392 teip->va = 0;
393 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
394 *prim = LAPD_MPH_ACTIVATE_IND;
395 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100396 case LAPD_CMD_RR:
397 teip->va = (nr & 0x7f);
398#if 0
399 if (teip->state != LAPD_TEI_ACTIVE) {
400 if (teip->state == LAPD_TEI_ASSIGNED) {
401 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
402 *prim = LAPD_MPH_ACTIVATE_IND;
403 //printf("ASSIGNED and ACTIVE\n");
404 } else {
405#if 0
406 DEBUGP(DMI, "rr in strange " "state, send rej\n");
407
408 // rej
Harald Welted38f1052011-02-05 19:13:00 +0100409 resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100410 resp[l++] = (tei << 1) | 1;
411 resp[l++] = 0x09; //rej
412 resp[l++] =
413 ((teip->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100414 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100415 pf = 0; // dont reply
416#endif
417 };
418 };
419#endif
420 if (pf) {
421 // interrogating us, send rr
422 resp[l++] = data[0];
423 resp[l++] = (tei << 1) | 1;
424 resp[l++] = 0x01; // rr
425 resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req
426
Harald Welted38f1052011-02-05 19:13:00 +0100427 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100428
429 };
430 break;
431 case LAPD_CMD_FRMR:
432 // frame reject
433#if 0
434 if (teip->state == LAPD_TEI_ACTIVE)
435 *prim = LAPD_MPH_DEACTIVATE_IND;
436 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
437#endif
Harald Welte0ae57552011-02-05 18:33:12 +0100438 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100439 break;
440 case LAPD_CMD_DISC:
441 // disconnect
442 resp[l++] = data[0];
443 resp[l++] = (tei << 1) | 1;
444 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100445 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100446 lapd_tei_set_state(teip, LAPD_TEI_NONE);
447 break;
448 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100449 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
450 tei, cmd);
451 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100452 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100453
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100454 if (typ == LAPD_TYPE_I) {
Harald Welte0ae57552011-02-05 18:33:12 +0100455 /* send rr
456 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
457 * lapd <- S RR sapi 3e tei 25 cmd 0 pf 0 ns -1 nr 5 ilen 0 teip 0x613800 vs 7 va 5 vr 2 len 4
458 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100459
Harald Welte0ae57552011-02-05 18:33:12 +0100460 /* interrogating us, send rr */
Harald Welte0abc11a2011-02-05 17:16:26 +0100461 DEBUGP(DMI, "Sending RR response\n");
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100462 resp[l++] = data[0];
463 resp[l++] = (tei << 1) | 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100464 resp[l++] = 0x01; // rr
465 resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100466
Harald Welted38f1052011-02-05 19:13:00 +0100467 li->transmit_cb(resp, l, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100468
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500469 if (cmd != 0) {
470 *prim = LAPD_DL_DATA_IND;
471 return contents;
472 }
473 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
474 *prim = LAPD_DL_UNITDATA_IND;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100475 return contents;
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500476 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100477
478 return NULL;
479};
480
Harald Welte1a00d822011-02-11 18:34:51 +0100481int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
482{
483 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
484 if (!msg)
485 return -ENOMEM;
486
487 DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
488
489 /* make sure we know the TEI at the time the response comes in */
490 if (!teip_from_tei(li, tei))
491 lapd_tei_alloc(li, tei);
492
493 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
494 msgb_put_u8(msg, (tei << 1) | 1);
495 msgb_put_u8(msg, 0x7F);
496
497 li->transmit_cb(msg->data, msg->len, li->cbdata);
498
499 msgb_free(msg);
500
501 return 0;
502}
503
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100504void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
Harald Welted38f1052011-02-05 19:13:00 +0100505 uint8_t *data, unsigned int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100506{
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100507 //printf("lapd_transmit %d, %d\n", tei, len);
508 //hexdump(data, len);
Harald Welted38f1052011-02-05 19:13:00 +0100509 struct lapd_tei *teip = teip_from_tei(li, tei);
510
511 if (!teip) {
512 LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing "
513 "TEI %u\n", tei);
514 return;
515 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100516
Harald Welte0ae57552011-02-05 18:33:12 +0100517 /* prepend stuff */
Harald Welte30fe6412011-02-04 20:34:08 +0100518 uint8_t buf[10000];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100519 memset(buf, 0, sizeof(buf));
Harald Welte30fe6412011-02-04 20:34:08 +0100520 memmove(buf + 4, data, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100521 len += 4;
522
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100523 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
524 buf[1] = (tei << 1) | 1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100525 buf[2] = (LAPD_NS(teip) << 1);
526 buf[3] = (LAPD_NR(teip) << 1) | 0;
527
528 teip->vs = (teip->vs + 1) & 0x7f;
529
Harald Welted38f1052011-02-05 19:13:00 +0100530 li->transmit_cb(buf, len, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100531};
Harald Welted38f1052011-02-05 19:13:00 +0100532
Harald Welte1a00d822011-02-11 18:34:51 +0100533struct lapd_instance *lapd_instance_alloc(int network_side,
534 void (*tx_cb)(uint8_t *data, int len,
Harald Welted38f1052011-02-05 19:13:00 +0100535 void *cbdata), void *cbdata)
536{
537 struct lapd_instance *li;
538
539 li = talloc_zero(NULL, struct lapd_instance);
540 if (!li)
541 return NULL;
542
543 li->transmit_cb = tx_cb;
544 li->cbdata = cbdata;
Harald Welte1a00d822011-02-11 18:34:51 +0100545 li->network_side = network_side;
Harald Welted38f1052011-02-05 19:13:00 +0100546 INIT_LLIST_HEAD(&li->tei_list);
547
548 return li;
549}