blob: 7bce6cc5195608748632f2ca98911ce33f93de92 [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 uint8_t tei;
Harald Welte30fe6412011-02-04 20:34:08 +0100122 lapd_tei_state state;
Harald Weltedcf42e62011-02-13 11:58:21 +0100123
124 struct llist_head sap_list;
125};
126
127/* Structure representing a SAP within a TEI. We use this for TE-mode to
128 * re-transmit SABM */
129struct lapd_sap {
130 struct llist_head list;
131 struct lapd_tei *tei;
132 uint8_t sapi;
133 enum lapd_sap_state state;
134
Harald Weltea0fe72d2011-02-14 15:51:57 +0100135 /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */
136 int vs; /* next to be transmitted */
137 int va; /* last acked by peer */
138 int vr; /* next expected to be received */
139
Harald Weltedcf42e62011-02-13 11:58:21 +0100140 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 */
Harald Weltea0fe72d2011-02-14 15:51:57 +0100165#define LAPD_NS(sap) (sap->vs)
166#define LAPD_NR(sap) (sap->vr)
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100167
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 Weltea0fe72d2011-02-14 15:51:57 +0100319 struct lapd_sap *sap;
Harald Welte716d2a42011-02-05 17:29:05 +0100320
321 uint8_t resp[8];
322 int l = 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100323
324 *ilen = 0;
325 *prim = 0;
326
327 if (len < 2) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100328 DEBUGP(DMI, "len %d < 2\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100329 return NULL;
330 };
331
332 if ((data[0] & 1) != 0 || (data[1] & 1) != 1) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100333 DEBUGP(DMI, "address field %x/%x not well formed\n", data[0],
Harald Welte30fe6412011-02-04 20:34:08 +0100334 data[1]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100335 return NULL;
336 };
337
Harald Welte716d2a42011-02-05 17:29:05 +0100338 sapi = data[0] >> 2;
339 cr = (data[0] >> 1) & 1;
340 tei = data[1] >> 1;
Harald Welted38f1052011-02-05 19:13:00 +0100341 command = li->network_side ^ cr;
Harald Welte0abc11a2011-02-05 17:16:26 +0100342 //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100343
344 if (len < 3) {
Harald Welte0abc11a2011-02-05 17:16:26 +0100345 DEBUGP(DMI, "len %d < 3\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100346 return NULL;
347 };
348
349 lapd_msg_type typ = 0;
350 lapd_cmd_type cmd = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100351 pf = -1;
352 ns = -1;
353 nr = -1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100354 if ((data[2] & 1) == 0) {
355 typ = LAPD_TYPE_I;
356 assert(len >= 4);
357 ns = data[2] >> 1;
358 nr = data[3] >> 1;
359 pf = data[3] & 1;
360 cmd = LAPD_CMD_I;
361 } else if ((data[2] & 3) == 1) {
362 typ = LAPD_TYPE_S;
363 assert(len >= 4);
364 nr = data[3] >> 1;
365 pf = data[3] & 1;
366 switch (data[2]) {
Harald Welte30fe6412011-02-04 20:34:08 +0100367 case 0x1:
368 cmd = LAPD_CMD_RR;
369 break;
370 case 0x5:
371 cmd = LAPD_CMD_RNR;
372 break;
373 case 0x9:
374 cmd = LAPD_CMD_REJ;
375 break;
376 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100377 LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]);
378 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100379 };
380 } else if ((data[2] & 3) == 3) {
381 typ = LAPD_TYPE_U;
382 pf = (data[2] >> 4) & 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100383 int val = data[2] & ~(1 << 4);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100384 switch (val) {
Harald Welte30fe6412011-02-04 20:34:08 +0100385 case 0x6f:
386 cmd = LAPD_CMD_SABME;
387 break;
388 case 0x0f:
389 cmd = LAPD_CMD_DM;
390 break;
391 case 0x03:
392 cmd = LAPD_CMD_UI;
393 break;
394 case 0x43:
395 cmd = LAPD_CMD_DISC;
396 break;
397 case 0x63:
398 cmd = LAPD_CMD_UA;
399 break;
400 case 0x87:
401 cmd = LAPD_CMD_FRMR;
402 break;
403 case 0xaf:
404 cmd = LAPD_CMD_XID;
405 break;
406
407 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100408 LOGP(DMI, LOGL_ERROR, "unknown U cmd %x "
409 "(pf %x data %x)\n", val, pf, data[2]);
410 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100411 };
412 };
Harald Welte30fe6412011-02-04 20:34:08 +0100413
Harald Welte716d2a42011-02-05 17:29:05 +0100414 contents = &data[4];
Harald Welte30fe6412011-02-04 20:34:08 +0100415 if (typ == LAPD_TYPE_U)
416 contents--;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100417 *ilen = len - (contents - data);
Harald Welte30fe6412011-02-04 20:34:08 +0100418
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100419 if (tei == 127)
Harald Welted38f1052011-02-05 19:13:00 +0100420 lapd_tei_receive(li, contents, *ilen);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100421
Harald Welted38f1052011-02-05 19:13:00 +0100422 teip = teip_from_tei(li, tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100423 if (!teip) {
424 LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei);
425 return NULL;
426 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100427
Harald Weltea0fe72d2011-02-14 15:51:57 +0100428 sap = lapd_sap_find(teip, sapi);
429 if (!sap) {
430 LOGP(DMI, LOGL_INFO, "No SAP for TEI=%u / SAPI=%u, "
431 "allocating\n", tei, sapi);
432 sap = lapd_sap_alloc(teip, sapi);
433 }
434
435 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
436 "ilen %d teip %p vs %d va %d vr %d len %d\n",
437 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
438 ns, nr, *ilen, teip, sap->vs, sap->va, sap->vr, len);
439
Harald Welte716d2a42011-02-05 17:29:05 +0100440 switch (cmd) {
441 case LAPD_CMD_I:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100442 if (ns != sap->vr) {
443 DEBUGP(DMI, "ns %d != vr %d\n", ns, sap->vr);
444 if (ns == ((sap->vr - 1) & 0x7f)) {
Harald Welte716d2a42011-02-05 17:29:05 +0100445 DEBUGP(DMI, "DOUBLE FRAME, ignoring\n");
446 cmd = 0; // ignore
447 } else {
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100448 assert(0);
Harald Welte716d2a42011-02-05 17:29:05 +0100449 };
450 } else {
451 //printf("IN SEQUENCE\n");
Harald Weltea0fe72d2011-02-14 15:51:57 +0100452 sap->vr = (ns + 1) & 0x7f; // FIXME: hack!
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100453 };
Harald Welte716d2a42011-02-05 17:29:05 +0100454
455 break;
456 case LAPD_CMD_UI:
457 break;
458 case LAPD_CMD_SABME:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100459 sap->vs = 0;
460 sap->vr = 0;
461 sap->va = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100462
463 // ua
464 resp[l++] = data[0];
465 resp[l++] = (tei << 1) | 1;
466 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100467 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100468 if (teip->state != LAPD_TEI_ACTIVE) {
469 if (teip->state == LAPD_TEI_ASSIGNED) {
470 lapd_tei_set_state(teip,
471 LAPD_TEI_ACTIVE);
472 //printf("ASSIGNED and ACTIVE\n");
473 } else {
474#if 0
475 DEBUGP(DMI, "rr in strange state, send rej\n");
476
477 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100478 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100479 resp[l++] = (tei << 1) | 1;
480 resp[l++] = 0x09; //rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100481 resp[l++] = ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100482 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100483 pf = 0; // dont reply
484#endif
485 };
486 };
487
488 *prim = LAPD_MPH_ACTIVATE_IND;
489 break;
Harald Welte1a00d822011-02-11 18:34:51 +0100490 case LAPD_CMD_UA:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100491 sap->vs = 0;
492 sap->vr = 0;
493 sap->va = 0;
Harald Welte1a00d822011-02-11 18:34:51 +0100494 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100495 lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE);
Harald Welte1a00d822011-02-11 18:34:51 +0100496 *prim = LAPD_MPH_ACTIVATE_IND;
497 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100498 case LAPD_CMD_RR:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100499 sap->va = (nr & 0x7f);
Harald Welte716d2a42011-02-05 17:29:05 +0100500#if 0
501 if (teip->state != LAPD_TEI_ACTIVE) {
502 if (teip->state == LAPD_TEI_ASSIGNED) {
503 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
504 *prim = LAPD_MPH_ACTIVATE_IND;
505 //printf("ASSIGNED and ACTIVE\n");
506 } else {
507#if 0
508 DEBUGP(DMI, "rr in strange " "state, send rej\n");
509
510 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100511 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100512 resp[l++] = (tei << 1) | 1;
513 resp[l++] = 0x09; //rej
514 resp[l++] =
Harald Weltea0fe72d2011-02-14 15:51:57 +0100515 ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100516 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100517 pf = 0; // dont reply
518#endif
519 };
520 };
521#endif
522 if (pf) {
523 // interrogating us, send rr
524 resp[l++] = data[0];
525 resp[l++] = (tei << 1) | 1;
526 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100527 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Harald Welte716d2a42011-02-05 17:29:05 +0100528
Harald Welted38f1052011-02-05 19:13:00 +0100529 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100530
531 };
532 break;
533 case LAPD_CMD_FRMR:
534 // frame reject
535#if 0
536 if (teip->state == LAPD_TEI_ACTIVE)
537 *prim = LAPD_MPH_DEACTIVATE_IND;
538 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
539#endif
Harald Welte0ae57552011-02-05 18:33:12 +0100540 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100541 break;
542 case LAPD_CMD_DISC:
543 // disconnect
544 resp[l++] = data[0];
545 resp[l++] = (tei << 1) | 1;
546 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100547 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100548 lapd_tei_set_state(teip, LAPD_TEI_NONE);
549 break;
550 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100551 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
552 tei, cmd);
553 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100554 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100555
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100556 if (typ == LAPD_TYPE_I) {
Harald Welte0ae57552011-02-05 18:33:12 +0100557 /* send rr
558 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
559 * 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
560 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100561
Harald Welte0ae57552011-02-05 18:33:12 +0100562 /* interrogating us, send rr */
Harald Welte0abc11a2011-02-05 17:16:26 +0100563 DEBUGP(DMI, "Sending RR response\n");
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100564 resp[l++] = data[0];
565 resp[l++] = (tei << 1) | 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100566 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100567 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100568
Harald Welted38f1052011-02-05 19:13:00 +0100569 li->transmit_cb(resp, l, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100570
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500571 if (cmd != 0) {
572 *prim = LAPD_DL_DATA_IND;
573 return contents;
574 }
575 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
576 *prim = LAPD_DL_UNITDATA_IND;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100577 return contents;
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500578 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100579
580 return NULL;
581};
582
Harald Weltedcf42e62011-02-13 11:58:21 +0100583/* low-level function to send a single SABM message */
584static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
Harald Welte1a00d822011-02-11 18:34:51 +0100585{
586 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
587 if (!msg)
588 return -ENOMEM;
589
590 DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
591
Harald Welte1a00d822011-02-11 18:34:51 +0100592 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
593 msgb_put_u8(msg, (tei << 1) | 1);
594 msgb_put_u8(msg, 0x7F);
595
596 li->transmit_cb(msg->data, msg->len, li->cbdata);
597
598 msgb_free(msg);
599
600 return 0;
601}
602
Harald Weltedcf42e62011-02-13 11:58:21 +0100603/* timer call-back function for SABM re-transmission */
604static void sabme_timer_cb(void *_sap)
605{
606 struct lapd_sap *sap = _sap;
607
608 lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi);
609
Harald Welte909212f2011-02-13 15:42:07 +0100610 if (sap->state == SAP_STATE_SABM_RETRANS)
611 bsc_schedule_timer(&sap->sabme_timer, SABM_INTERVAL);
Harald Weltedcf42e62011-02-13 11:58:21 +0100612}
613
614/* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
615int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
616{
617 struct lapd_sap *sap;
618 struct lapd_tei *teip;
619
620 teip = teip_from_tei(li, tei);
621 if (!teip)
622 teip = lapd_tei_alloc(li, tei);
623
624 sap = lapd_sap_find(teip, sapi);
625 if (sap)
626 return -EEXIST;
627
628 sap = lapd_sap_alloc(teip, sapi);
629
630 lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS);
631
632 return 0;
633}
634
635/* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
636int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
637{
638 struct lapd_tei *teip;
639 struct lapd_sap *sap;
640
641 teip = teip_from_tei(li, tei);
642 if (!teip)
643 return -ENODEV;
644
645 sap = lapd_sap_find(teip, sapi);
646 if (!sap)
647 return -ENODEV;
648
Harald Welte909212f2011-02-13 15:42:07 +0100649 lapd_sap_set_state(teip, sapi, SAP_STATE_INACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100650
651 llist_del(&sap->list);
652 talloc_free(sap);
653
654 return 0;
655}
656
657/* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100658void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
Harald Welted38f1052011-02-05 19:13:00 +0100659 uint8_t *data, unsigned int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100660{
Harald Welted38f1052011-02-05 19:13:00 +0100661 struct lapd_tei *teip = teip_from_tei(li, tei);
Harald Weltea0fe72d2011-02-14 15:51:57 +0100662 struct lapd_sap *sap;
Harald Welted38f1052011-02-05 19:13:00 +0100663
664 if (!teip) {
665 LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing "
666 "TEI %u\n", tei);
667 return;
668 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100669
Harald Weltea0fe72d2011-02-14 15:51:57 +0100670 sap = lapd_sap_find(teip, sapi);
671 if (!sap) {
672 LOGP(DMI, LOGL_INFO, "Tx on unknown SAPI=%u in TEI=%u, "
673 "allocating\n", sapi, tei);
674 sap = lapd_sap_alloc(teip, sapi);
675 }
676
Harald Welte0ae57552011-02-05 18:33:12 +0100677 /* prepend stuff */
Harald Welte30fe6412011-02-04 20:34:08 +0100678 uint8_t buf[10000];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100679 memset(buf, 0, sizeof(buf));
Harald Welte30fe6412011-02-04 20:34:08 +0100680 memmove(buf + 4, data, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100681 len += 4;
682
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100683 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
684 buf[1] = (tei << 1) | 1;
Harald Weltea0fe72d2011-02-14 15:51:57 +0100685 buf[2] = (LAPD_NS(sap) << 1);
686 buf[3] = (LAPD_NR(sap) << 1) | 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100687
Harald Weltea0fe72d2011-02-14 15:51:57 +0100688 sap->vs = (sap->vs + 1) & 0x7f;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100689
Harald Welted38f1052011-02-05 19:13:00 +0100690 li->transmit_cb(buf, len, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100691};
Harald Welted38f1052011-02-05 19:13:00 +0100692
Harald Weltedcf42e62011-02-13 11:58:21 +0100693/* Allocate a new LAPD instance */
Harald Welte1a00d822011-02-11 18:34:51 +0100694struct lapd_instance *lapd_instance_alloc(int network_side,
695 void (*tx_cb)(uint8_t *data, int len,
Harald Welted38f1052011-02-05 19:13:00 +0100696 void *cbdata), void *cbdata)
697{
698 struct lapd_instance *li;
699
700 li = talloc_zero(NULL, struct lapd_instance);
701 if (!li)
702 return NULL;
703
704 li->transmit_cb = tx_cb;
705 li->cbdata = cbdata;
Harald Welte1a00d822011-02-11 18:34:51 +0100706 li->network_side = network_side;
Harald Welted38f1052011-02-05 19:13:00 +0100707 INIT_LLIST_HEAD(&li->tei_list);
708
709 return li;
710}