blob: 1212fb36965890f10b350168ea26cfc86c9be1f4 [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
Harald Welted38f1052011-02-05 19:13:00 +01004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
Harald Weltedcf42e62011-02-13 11:58:21 +01006 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
Harald Welted38f1052011-02-05 19:13:00 +01007 *
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
Harald Weltedcf42e62011-02-13 11:58:21 +010026/* TODO:
27 * detect RR timeout and set SAP state back to SABM_RETRANSMIT
28 * use of value_string
29 * further code cleanup (spaghetti)
30 */
31
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010032#include <stdio.h>
33#include <string.h>
34#include <assert.h>
Harald Welte1a00d822011-02-11 18:34:51 +010035#include <errno.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010036
37#include "lapd.h"
Harald Welted38f1052011-02-05 19:13:00 +010038
39#include <osmocore/linuxlist.h>
40#include <osmocore/talloc.h>
Harald Welte1a00d822011-02-11 18:34:51 +010041#include <osmocore/msgb.h>
Harald Weltedcf42e62011-02-13 11:58:21 +010042#include <osmocore/timer.h>
Harald Welted38f1052011-02-05 19:13:00 +010043#include <openbsc/debug.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010044
Harald Weltedcf42e62011-02-13 11:58:21 +010045#define SABM_INTERVAL 0, 300000
46
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010047typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010048 LAPD_TEI_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010049 LAPD_TEI_ASSIGNED,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010050 LAPD_TEI_ACTIVE,
51} lapd_tei_state;
52
53const char *lapd_tei_states[] = {
54 "NONE",
55 "ASSIGNED",
56 "ACTIVE",
57};
58
59typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010060 LAPD_TYPE_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010061
62 LAPD_TYPE_I,
63 LAPD_TYPE_S,
64 LAPD_TYPE_U,
65} lapd_msg_type;
66
67typedef enum {
Harald Welte7e859bc2011-02-04 20:36:50 +010068 /* commands/responses */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010069 LAPD_CMD_NONE = 0,
70
71 LAPD_CMD_I,
72 LAPD_CMD_RR,
73 LAPD_CMD_RNR,
74 LAPD_CMD_REJ,
75
76 LAPD_CMD_SABME,
77 LAPD_CMD_DM,
78 LAPD_CMD_UI,
79 LAPD_CMD_DISC,
80 LAPD_CMD_UA,
81 LAPD_CMD_FRMR,
82 LAPD_CMD_XID,
83} lapd_cmd_type;
84
85const char *lapd_cmd_types[] = {
86 "NONE",
87
88 "I",
89 "RR",
90 "RNR",
91 "REJ",
92
93 "SABME",
94 "DM",
95 "UI",
96 "DISC",
97 "UA",
98 "FRMR",
99 "XID",
100
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100101};
102
Harald Weltedcf42e62011-02-13 11:58:21 +0100103enum lapd_sap_state {
104 SAP_STATE_INACTIVE,
105 SAP_STATE_SABM_RETRANS,
106 SAP_STATE_ACTIVE,
107};
108
109const char *lapd_sap_states[] = {
110 "INACTIVE",
111 "SABM_RETRANS",
112 "ACTIVE",
113};
114
Harald Welte30fe6412011-02-04 20:34:08 +0100115const char *lapd_msg_types = "?ISU";
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100116
Harald Weltedcf42e62011-02-13 11:58:21 +0100117/* structure representing an allocated TEI within a LAPD instance */
Harald Welted38f1052011-02-05 19:13:00 +0100118struct lapd_tei {
119 struct llist_head list;
Harald Weltedcf42e62011-02-13 11:58:21 +0100120 struct lapd_instance *li;
Harald Welted38f1052011-02-05 19:13:00 +0100121
122 uint8_t tei;
Harald Welte7e859bc2011-02-04 20:36:50 +0100123 /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */
124 int vs; /* next to be transmitted */
125 int va; /* last acked by peer */
126 int vr; /* next expected to be received */
Harald Welte30fe6412011-02-04 20:34:08 +0100127 lapd_tei_state state;
Harald Weltedcf42e62011-02-13 11:58:21 +0100128
129 struct llist_head sap_list;
130};
131
132/* Structure representing a SAP within a TEI. We use this for TE-mode to
133 * re-transmit SABM */
134struct lapd_sap {
135 struct llist_head list;
136 struct lapd_tei *tei;
137 uint8_t sapi;
138 enum lapd_sap_state state;
139
140 struct timer_list sabme_timer; /* timer to re-transmit SABM message */
Harald Welted38f1052011-02-05 19:13:00 +0100141};
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100142
Harald Welte7e859bc2011-02-04 20:36:50 +0100143/* 3.5.2.2 Send state variable V(S)
144 * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame
145 * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can
146 * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each
147 * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of
148 * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127.
149 *
150 * 3.5.2.3 Acknowledge state variable V(A)
151 * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame
152 * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been
153 * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can
154 * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values
155 * 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) ≤
156 * V(S).
157 *
158 * 3.5.2.5 Receive state variable V(R)
159 * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame
160 * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the
161 * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1.
162 * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame
163 * whose N(S) equals V(R).
164 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100165#define LAPD_NS(teip) (teip->vs)
166#define LAPD_NR(teip) (teip->vr)
167
Harald Welte7e859bc2011-02-04 20:36:50 +0100168/* 3.5.2.4 Send sequence number N(S)
169 * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in-
170 * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S).
171 *
172 * 3.5.2.6 Receive sequence number N(R)
173 * All I frames and supervisory frames contain N(R), the expected send sequence number of the next
174 * received I frame. At the time that a frame of the above types is designated for transmission, the value
175 * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has
176 * correctly received all I frames numbered up to and including N(R) − 1.
177 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100178
Harald Weltedcf42e62011-02-13 11:58:21 +0100179/* Resolve TEI structure from given numeric TEI */
Harald Welted38f1052011-02-05 19:13:00 +0100180static struct lapd_tei *teip_from_tei(struct lapd_instance *li, uint8_t tei)
Harald Welte30fe6412011-02-04 20:34:08 +0100181{
Harald Welted38f1052011-02-05 19:13:00 +0100182 struct lapd_tei *lt;
183
184 llist_for_each_entry(lt, &li->tei_list, list) {
185 if (lt->tei == tei)
186 return lt;
187 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100188 return NULL;
189};
190
Harald Welted38f1052011-02-05 19:13:00 +0100191static void lapd_tei_set_state(struct lapd_tei *teip, int newstate)
Harald Welte30fe6412011-02-04 20:34:08 +0100192{
Harald Weltedcf42e62011-02-13 11:58:21 +0100193 DEBUGP(DMI, "state change on TEI %d: %s -> %s\n", teip->tei,
Harald Welte30fe6412011-02-04 20:34:08 +0100194 lapd_tei_states[teip->state], lapd_tei_states[newstate]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100195 teip->state = newstate;
196};
197
Harald Weltedcf42e62011-02-13 11:58:21 +0100198/* Allocate a new TEI */
199struct lapd_tei *lapd_tei_alloc(struct lapd_instance *li, uint8_t tei)
Harald Welte1a00d822011-02-11 18:34:51 +0100200{
201 struct lapd_tei *teip;
202
203 teip = talloc_zero(li, struct lapd_tei);
204 if (!teip)
Harald Weltedcf42e62011-02-13 11:58:21 +0100205 return NULL;
Harald Welte1a00d822011-02-11 18:34:51 +0100206
Harald Weltedcf42e62011-02-13 11:58:21 +0100207 teip->li = li;
Harald Welte1a00d822011-02-11 18:34:51 +0100208 teip->tei = tei;
209 llist_add(&teip->list, &li->tei_list);
Harald Weltedcf42e62011-02-13 11:58:21 +0100210 INIT_LLIST_HEAD(&teip->sap_list);
Harald Welte1a00d822011-02-11 18:34:51 +0100211
212 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
213
Harald Weltedcf42e62011-02-13 11:58:21 +0100214 return teip;
Harald Welte1a00d822011-02-11 18:34:51 +0100215}
216
Harald Weltedcf42e62011-02-13 11:58:21 +0100217/* Find a SAP within a given TEI */
218static struct lapd_sap *lapd_sap_find(struct lapd_tei *teip, uint8_t sapi)
219{
220 struct lapd_sap *sap;
221
222 llist_for_each_entry(sap, &teip->sap_list, list) {
223 if (sap->sapi == sapi)
224 return sap;
225 }
226
227 return NULL;
228}
229
230static void sabme_timer_cb(void *_sap);
231
232/* Allocate a new SAP within a given TEI */
233static struct lapd_sap *lapd_sap_alloc(struct lapd_tei *teip, uint8_t sapi)
234{
235 struct lapd_sap *sap = talloc_zero(teip, struct lapd_sap);
236
237 LOGP(DMI, LOGL_INFO, "Allocating SAP for SAPI=%u / TEI=%u\n",
238 sapi, teip->tei);
239
240 sap->sapi = sapi;
241 sap->tei = teip;
242 sap->sabme_timer.cb = &sabme_timer_cb;
243 sap->sabme_timer.data = sap;
244
245 llist_add(&sap->list, &teip->sap_list);
246
247 return sap;
248}
249
250static void lapd_sap_set_state(struct lapd_tei *teip, uint8_t sapi,
251 enum lapd_sap_state newstate)
252{
253 struct lapd_sap *sap = lapd_sap_find(teip, sapi);
254 if (!sap)
255 return;
256
257 DEBUGP(DMI, "state change on TEI %u / SAPI %u: %s -> %s\n", teip->tei,
258 sapi, lapd_sap_states[sap->state], lapd_sap_states[newstate]);
259 switch (sap->state) {
260 case SAP_STATE_SABM_RETRANS:
261 if (newstate != SAP_STATE_SABM_RETRANS)
262 bsc_del_timer(&sap->sabme_timer);
263 break;
264 default:
265 if (newstate == SAP_STATE_SABM_RETRANS)
266 bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL);
267 break;
268 }
269
270 sap->state = newstate;
271};
272
273/* Input function into TEI manager */
Harald Welted38f1052011-02-05 19:13:00 +0100274static void lapd_tei_receive(struct lapd_instance *li, uint8_t *data, int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100275{
Harald Welte8fc66a02011-02-05 19:51:05 +0100276 uint8_t entity = data[0];
277 uint8_t ref = data[1];
278 uint8_t mt = data[3];
279 uint8_t action = data[4] >> 1;
280 uint8_t e = data[4] & 1;
Harald Welted38f1052011-02-05 19:13:00 +0100281 uint8_t resp[8];
282 struct lapd_tei *teip;
283
Harald Welte8fc66a02011-02-05 19:51:05 +0100284 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 +0100285
286 switch (mt) {
Harald Welted38f1052011-02-05 19:13:00 +0100287 case 0x01: /* IDENTITY REQUEST */
Harald Welte8fc66a02011-02-05 19:51:05 +0100288 DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action);
Harald Welte716d2a42011-02-05 17:29:05 +0100289
Harald Welte8fc66a02011-02-05 19:51:05 +0100290 teip = teip_from_tei(li, action);
Harald Welted38f1052011-02-05 19:13:00 +0100291 if (!teip) {
Harald Welte8fc66a02011-02-05 19:51:05 +0100292 LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action);
Harald Welte1a00d822011-02-11 18:34:51 +0100293 lapd_tei_alloc(li, action);
Harald Welte30fe6412011-02-04 20:34:08 +0100294 }
Harald Welted38f1052011-02-05 19:13:00 +0100295
296 /* Send ACCEPT */
297 memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8);
Harald Welte8fc66a02011-02-05 19:51:05 +0100298 resp[7] = (action << 1) | 1;
Harald Welted38f1052011-02-05 19:13:00 +0100299 li->transmit_cb(resp, 8, li->cbdata);
300
301 if (teip->state == LAPD_TEI_NONE)
302 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
303 break;
Harald Welte30fe6412011-02-04 20:34:08 +0100304 default:
Harald Welte8fc66a02011-02-05 19:51:05 +0100305 LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n",
Harald Welte0ae57552011-02-05 18:33:12 +0100306 mt, action);
307 break;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100308 };
309};
310
Harald Weltedcf42e62011-02-13 11:58:21 +0100311/* General input function for any data received for this LAPD instance */
Harald Welted38f1052011-02-05 19:13:00 +0100312uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len,
313 int *ilen, lapd_mph_type *prim)
Harald Welte30fe6412011-02-04 20:34:08 +0100314{
Harald Welte716d2a42011-02-05 17:29:05 +0100315 uint8_t sapi, cr, tei, command;
316 int pf, ns, nr;
317 uint8_t *contents;
Harald Welted38f1052011-02-05 19:13:00 +0100318 struct lapd_tei *teip;
Harald Welte716d2a42011-02-05 17:29:05 +0100319
320 uint8_t resp[8];
321 int l = 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100322
323 *ilen = 0;
324 *prim = 0;
325
326 if (len < 2) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100327 DEBUGP(DMI, "len %d < 2\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100328 return NULL;
329 };
330
331 if ((data[0] & 1) != 0 || (data[1] & 1) != 1) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100332 DEBUGP(DMI, "address field %x/%x not well formed\n", data[0],
Harald Welte30fe6412011-02-04 20:34:08 +0100333 data[1]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100334 return NULL;
335 };
336
Harald Welte716d2a42011-02-05 17:29:05 +0100337 sapi = data[0] >> 2;
338 cr = (data[0] >> 1) & 1;
339 tei = data[1] >> 1;
Harald Welted38f1052011-02-05 19:13:00 +0100340 command = li->network_side ^ cr;
Harald Welte0abc11a2011-02-05 17:16:26 +0100341 //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100342
343 if (len < 3) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100344 DEBUGP(DMI, "len %d < 3\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100345 return NULL;
346 };
347
348 lapd_msg_type typ = 0;
349 lapd_cmd_type cmd = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100350 pf = -1;
351 ns = -1;
352 nr = -1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100353 if ((data[2] & 1) == 0) {
354 typ = LAPD_TYPE_I;
355 assert(len >= 4);
356 ns = data[2] >> 1;
357 nr = data[3] >> 1;
358 pf = data[3] & 1;
359 cmd = LAPD_CMD_I;
360 } else if ((data[2] & 3) == 1) {
361 typ = LAPD_TYPE_S;
362 assert(len >= 4);
363 nr = data[3] >> 1;
364 pf = data[3] & 1;
365 switch (data[2]) {
Harald Welte30fe6412011-02-04 20:34:08 +0100366 case 0x1:
367 cmd = LAPD_CMD_RR;
368 break;
369 case 0x5:
370 cmd = LAPD_CMD_RNR;
371 break;
372 case 0x9:
373 cmd = LAPD_CMD_REJ;
374 break;
375 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100376 LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]);
377 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100378 };
379 } else if ((data[2] & 3) == 3) {
380 typ = LAPD_TYPE_U;
381 pf = (data[2] >> 4) & 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100382 int val = data[2] & ~(1 << 4);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100383 switch (val) {
Harald Welte30fe6412011-02-04 20:34:08 +0100384 case 0x6f:
385 cmd = LAPD_CMD_SABME;
386 break;
387 case 0x0f:
388 cmd = LAPD_CMD_DM;
389 break;
390 case 0x03:
391 cmd = LAPD_CMD_UI;
392 break;
393 case 0x43:
394 cmd = LAPD_CMD_DISC;
395 break;
396 case 0x63:
397 cmd = LAPD_CMD_UA;
398 break;
399 case 0x87:
400 cmd = LAPD_CMD_FRMR;
401 break;
402 case 0xaf:
403 cmd = LAPD_CMD_XID;
404 break;
405
406 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100407 LOGP(DMI, LOGL_ERROR, "unknown U cmd %x "
408 "(pf %x data %x)\n", val, pf, data[2]);
409 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100410 };
411 };
Harald Welte30fe6412011-02-04 20:34:08 +0100412
Harald Welte716d2a42011-02-05 17:29:05 +0100413 contents = &data[4];
Harald Welte30fe6412011-02-04 20:34:08 +0100414 if (typ == LAPD_TYPE_U)
415 contents--;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100416 *ilen = len - (contents - data);
Harald Welte30fe6412011-02-04 20:34:08 +0100417
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100418 if (tei == 127)
Harald Welted38f1052011-02-05 19:13:00 +0100419 lapd_tei_receive(li, contents, *ilen);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100420
Harald Welted38f1052011-02-05 19:13:00 +0100421 teip = teip_from_tei(li, tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100422
Harald Welte0abc11a2011-02-05 17:16:26 +0100423 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
424 "ilen %d teip %p vs %d va %d vr %d len %d\n",
Harald Welte30fe6412011-02-04 20:34:08 +0100425 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
426 ns, nr, *ilen, teip, teip ? teip->vs : -1, teip ? teip->va : -1,
427 teip ? teip->vr : -1, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100428
Harald Welte716d2a42011-02-05 17:29:05 +0100429 if (!teip) {
430 LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei);
431 return NULL;
432 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100433
Harald Welte716d2a42011-02-05 17:29:05 +0100434 switch (cmd) {
435 case LAPD_CMD_I:
436 if (ns != teip->vr) {
437 DEBUGP(DMI, "ns %d != vr %d\n", ns, teip->vr);
438 if (ns == ((teip->vr - 1) & 0x7f)) {
439 DEBUGP(DMI, "DOUBLE FRAME, ignoring\n");
440 cmd = 0; // ignore
441 } else {
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100442 assert(0);
Harald Welte716d2a42011-02-05 17:29:05 +0100443 };
444 } else {
445 //printf("IN SEQUENCE\n");
446 teip->vr = (ns + 1) & 0x7f; // FIXME: hack!
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100447 };
Harald Welte716d2a42011-02-05 17:29:05 +0100448
449 break;
450 case LAPD_CMD_UI:
451 break;
452 case LAPD_CMD_SABME:
453 teip->vs = 0;
454 teip->vr = 0;
455 teip->va = 0;
456
457 // ua
458 resp[l++] = data[0];
459 resp[l++] = (tei << 1) | 1;
460 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100461 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100462 if (teip->state != LAPD_TEI_ACTIVE) {
463 if (teip->state == LAPD_TEI_ASSIGNED) {
464 lapd_tei_set_state(teip,
465 LAPD_TEI_ACTIVE);
466 //printf("ASSIGNED and ACTIVE\n");
467 } else {
468#if 0
469 DEBUGP(DMI, "rr in strange state, send rej\n");
470
471 // rej
Harald Welted38f1052011-02-05 19:13:00 +0100472 resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100473 resp[l++] = (tei << 1) | 1;
474 resp[l++] = 0x09; //rej
475 resp[l++] = ((teip->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100476 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100477 pf = 0; // dont reply
478#endif
479 };
480 };
481
482 *prim = LAPD_MPH_ACTIVATE_IND;
483 break;
Harald Welte1a00d822011-02-11 18:34:51 +0100484 case LAPD_CMD_UA:
485 teip->vs = 0;
486 teip->vr = 0;
487 teip->va = 0;
488 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100489 lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE);
Harald Welte1a00d822011-02-11 18:34:51 +0100490 *prim = LAPD_MPH_ACTIVATE_IND;
491 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100492 case LAPD_CMD_RR:
493 teip->va = (nr & 0x7f);
494#if 0
495 if (teip->state != LAPD_TEI_ACTIVE) {
496 if (teip->state == LAPD_TEI_ASSIGNED) {
497 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
498 *prim = LAPD_MPH_ACTIVATE_IND;
499 //printf("ASSIGNED and ACTIVE\n");
500 } else {
501#if 0
502 DEBUGP(DMI, "rr in strange " "state, send rej\n");
503
504 // rej
Harald Welted38f1052011-02-05 19:13:00 +0100505 resp[l++] = (teip-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100506 resp[l++] = (tei << 1) | 1;
507 resp[l++] = 0x09; //rej
508 resp[l++] =
509 ((teip->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100510 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100511 pf = 0; // dont reply
512#endif
513 };
514 };
515#endif
516 if (pf) {
517 // interrogating us, send rr
518 resp[l++] = data[0];
519 resp[l++] = (tei << 1) | 1;
520 resp[l++] = 0x01; // rr
521 resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req
522
Harald Welted38f1052011-02-05 19:13:00 +0100523 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100524
525 };
526 break;
527 case LAPD_CMD_FRMR:
528 // frame reject
529#if 0
530 if (teip->state == LAPD_TEI_ACTIVE)
531 *prim = LAPD_MPH_DEACTIVATE_IND;
532 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
533#endif
Harald Welte0ae57552011-02-05 18:33:12 +0100534 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100535 break;
536 case LAPD_CMD_DISC:
537 // disconnect
538 resp[l++] = data[0];
539 resp[l++] = (tei << 1) | 1;
540 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100541 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100542 lapd_tei_set_state(teip, LAPD_TEI_NONE);
543 break;
544 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100545 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
546 tei, cmd);
547 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100548 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100549
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100550 if (typ == LAPD_TYPE_I) {
Harald Welte0ae57552011-02-05 18:33:12 +0100551 /* send rr
552 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
553 * 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
554 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100555
Harald Welte0ae57552011-02-05 18:33:12 +0100556 /* interrogating us, send rr */
Harald Welte0abc11a2011-02-05 17:16:26 +0100557 DEBUGP(DMI, "Sending RR response\n");
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100558 resp[l++] = data[0];
559 resp[l++] = (tei << 1) | 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100560 resp[l++] = 0x01; // rr
561 resp[l++] = (LAPD_NR(teip) << 1) | (data[3] & 1); // pf bit from req
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100562
Harald Welted38f1052011-02-05 19:13:00 +0100563 li->transmit_cb(resp, l, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100564
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500565 if (cmd != 0) {
566 *prim = LAPD_DL_DATA_IND;
567 return contents;
568 }
569 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
570 *prim = LAPD_DL_UNITDATA_IND;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100571 return contents;
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500572 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100573
574 return NULL;
575};
576
Harald Weltedcf42e62011-02-13 11:58:21 +0100577/* low-level function to send a single SABM message */
578static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
Harald Welte1a00d822011-02-11 18:34:51 +0100579{
580 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
581 if (!msg)
582 return -ENOMEM;
583
584 DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
585
Harald Welte1a00d822011-02-11 18:34:51 +0100586 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
587 msgb_put_u8(msg, (tei << 1) | 1);
588 msgb_put_u8(msg, 0x7F);
589
590 li->transmit_cb(msg->data, msg->len, li->cbdata);
591
592 msgb_free(msg);
593
594 return 0;
595}
596
Harald Weltedcf42e62011-02-13 11:58:21 +0100597/* timer call-back function for SABM re-transmission */
598static void sabme_timer_cb(void *_sap)
599{
600 struct lapd_sap *sap = _sap;
601
602 lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi);
603
604 bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL);
605}
606
607/* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
608int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
609{
610 struct lapd_sap *sap;
611 struct lapd_tei *teip;
612
613 teip = teip_from_tei(li, tei);
614 if (!teip)
615 teip = lapd_tei_alloc(li, tei);
616
617 sap = lapd_sap_find(teip, sapi);
618 if (sap)
619 return -EEXIST;
620
621 sap = lapd_sap_alloc(teip, sapi);
622
623 lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS);
624
625 return 0;
626}
627
628/* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
629int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
630{
631 struct lapd_tei *teip;
632 struct lapd_sap *sap;
633
634 teip = teip_from_tei(li, tei);
635 if (!teip)
636 return -ENODEV;
637
638 sap = lapd_sap_find(teip, sapi);
639 if (!sap)
640 return -ENODEV;
641
642 bsc_del_timer(&sap->sabme_timer);
643
644 llist_del(&sap->list);
645 talloc_free(sap);
646
647 return 0;
648}
649
650/* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100651void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
Harald Welted38f1052011-02-05 19:13:00 +0100652 uint8_t *data, unsigned int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100653{
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100654 //printf("lapd_transmit %d, %d\n", tei, len);
655 //hexdump(data, len);
Harald Welted38f1052011-02-05 19:13:00 +0100656 struct lapd_tei *teip = teip_from_tei(li, tei);
657
658 if (!teip) {
659 LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing "
660 "TEI %u\n", tei);
661 return;
662 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100663
Harald Welte0ae57552011-02-05 18:33:12 +0100664 /* prepend stuff */
Harald Welte30fe6412011-02-04 20:34:08 +0100665 uint8_t buf[10000];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100666 memset(buf, 0, sizeof(buf));
Harald Welte30fe6412011-02-04 20:34:08 +0100667 memmove(buf + 4, data, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100668 len += 4;
669
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100670 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
671 buf[1] = (tei << 1) | 1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100672 buf[2] = (LAPD_NS(teip) << 1);
673 buf[3] = (LAPD_NR(teip) << 1) | 0;
674
675 teip->vs = (teip->vs + 1) & 0x7f;
676
Harald Welted38f1052011-02-05 19:13:00 +0100677 li->transmit_cb(buf, len, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100678};
Harald Welted38f1052011-02-05 19:13:00 +0100679
Harald Weltedcf42e62011-02-13 11:58:21 +0100680/* Allocate a new LAPD instance */
Harald Welte1a00d822011-02-11 18:34:51 +0100681struct lapd_instance *lapd_instance_alloc(int network_side,
682 void (*tx_cb)(uint8_t *data, int len,
Harald Welted38f1052011-02-05 19:13:00 +0100683 void *cbdata), void *cbdata)
684{
685 struct lapd_instance *li;
686
687 li = talloc_zero(NULL, struct lapd_instance);
688 if (!li)
689 return NULL;
690
691 li->transmit_cb = tx_cb;
692 li->cbdata = cbdata;
Harald Welte1a00d822011-02-11 18:34:51 +0100693 li->network_side = network_side;
Harald Welted38f1052011-02-05 19:13:00 +0100694 INIT_LLIST_HEAD(&li->tei_list);
695
696 return li;
697}