blob: 0d269cd41fe3bf517b55c52069a4064a6a606489 [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>
Harald Welte1a00d822011-02-11 18:34:51 +010034#include <errno.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010035
36#include "lapd.h"
Harald Welted38f1052011-02-05 19:13:00 +010037
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010038#include <osmocom/core/linuxlist.h>
39#include <osmocom/core/talloc.h>
40#include <osmocom/core/msgb.h>
41#include <osmocom/core/timer.h>
Harald Welted38f1052011-02-05 19:13:00 +010042#include <openbsc/debug.h>
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010043
Harald Weltedcf42e62011-02-13 11:58:21 +010044#define SABM_INTERVAL 0, 300000
45
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010046typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010047 LAPD_TEI_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010048 LAPD_TEI_ASSIGNED,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010049 LAPD_TEI_ACTIVE,
50} lapd_tei_state;
51
52const char *lapd_tei_states[] = {
53 "NONE",
54 "ASSIGNED",
55 "ACTIVE",
56};
57
58typedef enum {
Harald Welte30fe6412011-02-04 20:34:08 +010059 LAPD_TYPE_NONE = 0,
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010060
61 LAPD_TYPE_I,
62 LAPD_TYPE_S,
63 LAPD_TYPE_U,
64} lapd_msg_type;
65
66typedef enum {
Harald Welte7e859bc2011-02-04 20:36:50 +010067 /* commands/responses */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010068 LAPD_CMD_NONE = 0,
69
70 LAPD_CMD_I,
71 LAPD_CMD_RR,
72 LAPD_CMD_RNR,
73 LAPD_CMD_REJ,
74
75 LAPD_CMD_SABME,
76 LAPD_CMD_DM,
77 LAPD_CMD_UI,
78 LAPD_CMD_DISC,
79 LAPD_CMD_UA,
80 LAPD_CMD_FRMR,
81 LAPD_CMD_XID,
82} lapd_cmd_type;
83
84const char *lapd_cmd_types[] = {
85 "NONE",
86
87 "I",
88 "RR",
89 "RNR",
90 "REJ",
91
92 "SABME",
93 "DM",
94 "UI",
95 "DISC",
96 "UA",
97 "FRMR",
98 "XID",
99
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100100};
101
Harald Weltedcf42e62011-02-13 11:58:21 +0100102enum lapd_sap_state {
103 SAP_STATE_INACTIVE,
104 SAP_STATE_SABM_RETRANS,
105 SAP_STATE_ACTIVE,
106};
107
108const char *lapd_sap_states[] = {
109 "INACTIVE",
110 "SABM_RETRANS",
111 "ACTIVE",
112};
113
Harald Welte30fe6412011-02-04 20:34:08 +0100114const char *lapd_msg_types = "?ISU";
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100115
Harald Weltedcf42e62011-02-13 11:58:21 +0100116/* structure representing an allocated TEI within a LAPD instance */
Harald Welted38f1052011-02-05 19:13:00 +0100117struct lapd_tei {
118 struct llist_head list;
Harald Weltedcf42e62011-02-13 11:58:21 +0100119 struct lapd_instance *li;
Harald Welted38f1052011-02-05 19:13:00 +0100120 uint8_t tei;
Harald Welte30fe6412011-02-04 20:34:08 +0100121 lapd_tei_state state;
Harald Weltedcf42e62011-02-13 11:58:21 +0100122
123 struct llist_head sap_list;
124};
125
126/* Structure representing a SAP within a TEI. We use this for TE-mode to
127 * re-transmit SABM */
128struct lapd_sap {
129 struct llist_head list;
130 struct lapd_tei *tei;
131 uint8_t sapi;
132 enum lapd_sap_state state;
133
Harald Weltea0fe72d2011-02-14 15:51:57 +0100134 /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */
135 int vs; /* next to be transmitted */
136 int va; /* last acked by peer */
137 int vr; /* next expected to be received */
138
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200139 struct osmo_timer_list sabme_timer; /* timer to re-transmit SABM message */
Harald Welted38f1052011-02-05 19:13:00 +0100140};
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100141
Harald Welte7e859bc2011-02-04 20:36:50 +0100142/* 3.5.2.2 Send state variable V(S)
143 * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame
144 * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can
145 * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each
146 * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of
147 * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127.
148 *
149 * 3.5.2.3 Acknowledge state variable V(A)
150 * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame
151 * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been
152 * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can
153 * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values
154 * 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) ≤
155 * V(S).
156 *
157 * 3.5.2.5 Receive state variable V(R)
158 * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame
159 * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the
160 * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1.
161 * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame
162 * whose N(S) equals V(R).
163 */
Harald Weltea0fe72d2011-02-14 15:51:57 +0100164#define LAPD_NS(sap) (sap->vs)
165#define LAPD_NR(sap) (sap->vr)
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100166
Harald Welte7e859bc2011-02-04 20:36:50 +0100167/* 3.5.2.4 Send sequence number N(S)
168 * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in-
169 * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S).
170 *
171 * 3.5.2.6 Receive sequence number N(R)
172 * All I frames and supervisory frames contain N(R), the expected send sequence number of the next
173 * received I frame. At the time that a frame of the above types is designated for transmission, the value
174 * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has
175 * correctly received all I frames numbered up to and including N(R) − 1.
176 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100177
Harald Weltedcf42e62011-02-13 11:58:21 +0100178/* Resolve TEI structure from given numeric TEI */
Harald Welted38f1052011-02-05 19:13:00 +0100179static struct lapd_tei *teip_from_tei(struct lapd_instance *li, uint8_t tei)
Harald Welte30fe6412011-02-04 20:34:08 +0100180{
Harald Welted38f1052011-02-05 19:13:00 +0100181 struct lapd_tei *lt;
182
183 llist_for_each_entry(lt, &li->tei_list, list) {
184 if (lt->tei == tei)
185 return lt;
186 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100187 return NULL;
188};
189
Harald Welted38f1052011-02-05 19:13:00 +0100190static void lapd_tei_set_state(struct lapd_tei *teip, int newstate)
Harald Welte30fe6412011-02-04 20:34:08 +0100191{
Harald Weltedcf42e62011-02-13 11:58:21 +0100192 DEBUGP(DMI, "state change on TEI %d: %s -> %s\n", teip->tei,
Harald Welte30fe6412011-02-04 20:34:08 +0100193 lapd_tei_states[teip->state], lapd_tei_states[newstate]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100194 teip->state = newstate;
195};
196
Harald Weltedcf42e62011-02-13 11:58:21 +0100197/* Allocate a new TEI */
198struct lapd_tei *lapd_tei_alloc(struct lapd_instance *li, uint8_t tei)
Harald Welte1a00d822011-02-11 18:34:51 +0100199{
200 struct lapd_tei *teip;
201
202 teip = talloc_zero(li, struct lapd_tei);
203 if (!teip)
Harald Weltedcf42e62011-02-13 11:58:21 +0100204 return NULL;
Harald Welte1a00d822011-02-11 18:34:51 +0100205
Harald Weltedcf42e62011-02-13 11:58:21 +0100206 teip->li = li;
Harald Welte1a00d822011-02-11 18:34:51 +0100207 teip->tei = tei;
208 llist_add(&teip->list, &li->tei_list);
Harald Weltedcf42e62011-02-13 11:58:21 +0100209 INIT_LLIST_HEAD(&teip->sap_list);
Harald Welte1a00d822011-02-11 18:34:51 +0100210
211 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
212
Harald Weltedcf42e62011-02-13 11:58:21 +0100213 return teip;
Harald Welte1a00d822011-02-11 18:34:51 +0100214}
215
Harald Weltedcf42e62011-02-13 11:58:21 +0100216/* Find a SAP within a given TEI */
217static struct lapd_sap *lapd_sap_find(struct lapd_tei *teip, uint8_t sapi)
218{
219 struct lapd_sap *sap;
220
221 llist_for_each_entry(sap, &teip->sap_list, list) {
222 if (sap->sapi == sapi)
223 return sap;
224 }
225
226 return NULL;
227}
228
229static void sabme_timer_cb(void *_sap);
230
231/* Allocate a new SAP within a given TEI */
232static struct lapd_sap *lapd_sap_alloc(struct lapd_tei *teip, uint8_t sapi)
233{
234 struct lapd_sap *sap = talloc_zero(teip, struct lapd_sap);
235
236 LOGP(DMI, LOGL_INFO, "Allocating SAP for SAPI=%u / TEI=%u\n",
237 sapi, teip->tei);
238
239 sap->sapi = sapi;
240 sap->tei = teip;
241 sap->sabme_timer.cb = &sabme_timer_cb;
242 sap->sabme_timer.data = sap;
243
244 llist_add(&sap->list, &teip->sap_list);
245
246 return sap;
247}
248
249static void lapd_sap_set_state(struct lapd_tei *teip, uint8_t sapi,
250 enum lapd_sap_state newstate)
251{
252 struct lapd_sap *sap = lapd_sap_find(teip, sapi);
253 if (!sap)
254 return;
255
256 DEBUGP(DMI, "state change on TEI %u / SAPI %u: %s -> %s\n", teip->tei,
257 sapi, lapd_sap_states[sap->state], lapd_sap_states[newstate]);
258 switch (sap->state) {
259 case SAP_STATE_SABM_RETRANS:
260 if (newstate != SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200261 osmo_timer_del(&sap->sabme_timer);
Harald Weltedcf42e62011-02-13 11:58:21 +0100262 break;
263 default:
264 if (newstate == SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200265 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
Harald Weltedcf42e62011-02-13 11:58:21 +0100266 break;
267 }
268
269 sap->state = newstate;
270};
271
272/* Input function into TEI manager */
Harald Welted38f1052011-02-05 19:13:00 +0100273static void lapd_tei_receive(struct lapd_instance *li, uint8_t *data, int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100274{
Harald Welte8fc66a02011-02-05 19:51:05 +0100275 uint8_t entity = data[0];
276 uint8_t ref = data[1];
277 uint8_t mt = data[3];
278 uint8_t action = data[4] >> 1;
279 uint8_t e = data[4] & 1;
Harald Welted38f1052011-02-05 19:13:00 +0100280 uint8_t resp[8];
281 struct lapd_tei *teip;
282
Harald Welte8fc66a02011-02-05 19:51:05 +0100283 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 +0100284
285 switch (mt) {
Harald Welted38f1052011-02-05 19:13:00 +0100286 case 0x01: /* IDENTITY REQUEST */
Harald Welte8fc66a02011-02-05 19:51:05 +0100287 DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action);
Harald Welte716d2a42011-02-05 17:29:05 +0100288
Harald Welte8fc66a02011-02-05 19:51:05 +0100289 teip = teip_from_tei(li, action);
Harald Welted38f1052011-02-05 19:13:00 +0100290 if (!teip) {
Harald Welte8fc66a02011-02-05 19:51:05 +0100291 LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action);
Holger Hans Peter Freyther13673742011-04-04 19:17:58 +0200292 teip = lapd_tei_alloc(li, action);
Harald Welte30fe6412011-02-04 20:34:08 +0100293 }
Harald Welted38f1052011-02-05 19:13:00 +0100294
295 /* Send ACCEPT */
296 memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8);
Harald Welte8fc66a02011-02-05 19:51:05 +0100297 resp[7] = (action << 1) | 1;
Harald Welted38f1052011-02-05 19:13:00 +0100298 li->transmit_cb(resp, 8, li->cbdata);
299
300 if (teip->state == LAPD_TEI_NONE)
301 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
302 break;
Harald Welte30fe6412011-02-04 20:34:08 +0100303 default:
Harald Welte8fc66a02011-02-05 19:51:05 +0100304 LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n",
Harald Welte0ae57552011-02-05 18:33:12 +0100305 mt, action);
306 break;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100307 };
308};
309
Harald Weltedcf42e62011-02-13 11:58:21 +0100310/* General input function for any data received for this LAPD instance */
Harald Welted38f1052011-02-05 19:13:00 +0100311uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len,
312 int *ilen, lapd_mph_type *prim)
Harald Welte30fe6412011-02-04 20:34:08 +0100313{
Harald Welte716d2a42011-02-05 17:29:05 +0100314 uint8_t sapi, cr, tei, command;
315 int pf, ns, nr;
316 uint8_t *contents;
Harald Welted38f1052011-02-05 19:13:00 +0100317 struct lapd_tei *teip;
Harald Weltea0fe72d2011-02-14 15:51:57 +0100318 struct lapd_sap *sap;
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;
Harald Welte52a0b122011-08-09 21:24:47 +0200355 if (len < 4) {
356 LOGP(DMI, LOGL_ERROR, "LAPD I frame, len %d < 4\n", len);
357 return NULL;
358 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100359 ns = data[2] >> 1;
360 nr = data[3] >> 1;
361 pf = data[3] & 1;
362 cmd = LAPD_CMD_I;
363 } else if ((data[2] & 3) == 1) {
364 typ = LAPD_TYPE_S;
Harald Welte52a0b122011-08-09 21:24:47 +0200365 if (len < 4) {
366 LOGP(DMI, LOGL_ERROR, "LAPD S frame, len %d < 4\n", len);
367 return NULL;
368 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100369 nr = data[3] >> 1;
370 pf = data[3] & 1;
371 switch (data[2]) {
Harald Welte30fe6412011-02-04 20:34:08 +0100372 case 0x1:
373 cmd = LAPD_CMD_RR;
374 break;
375 case 0x5:
376 cmd = LAPD_CMD_RNR;
377 break;
378 case 0x9:
379 cmd = LAPD_CMD_REJ;
380 break;
381 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100382 LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]);
383 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100384 };
385 } else if ((data[2] & 3) == 3) {
386 typ = LAPD_TYPE_U;
387 pf = (data[2] >> 4) & 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100388 int val = data[2] & ~(1 << 4);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100389 switch (val) {
Harald Welte30fe6412011-02-04 20:34:08 +0100390 case 0x6f:
391 cmd = LAPD_CMD_SABME;
392 break;
393 case 0x0f:
394 cmd = LAPD_CMD_DM;
395 break;
396 case 0x03:
397 cmd = LAPD_CMD_UI;
398 break;
399 case 0x43:
400 cmd = LAPD_CMD_DISC;
401 break;
402 case 0x63:
403 cmd = LAPD_CMD_UA;
404 break;
405 case 0x87:
406 cmd = LAPD_CMD_FRMR;
407 break;
408 case 0xaf:
409 cmd = LAPD_CMD_XID;
410 break;
411
412 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100413 LOGP(DMI, LOGL_ERROR, "unknown U cmd %x "
414 "(pf %x data %x)\n", val, pf, data[2]);
415 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100416 };
417 };
Harald Welte30fe6412011-02-04 20:34:08 +0100418
Harald Welte716d2a42011-02-05 17:29:05 +0100419 contents = &data[4];
Harald Welte30fe6412011-02-04 20:34:08 +0100420 if (typ == LAPD_TYPE_U)
421 contents--;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100422 *ilen = len - (contents - data);
Harald Welte30fe6412011-02-04 20:34:08 +0100423
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100424 if (tei == 127)
Harald Welted38f1052011-02-05 19:13:00 +0100425 lapd_tei_receive(li, contents, *ilen);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100426
Harald Welted38f1052011-02-05 19:13:00 +0100427 teip = teip_from_tei(li, tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100428 if (!teip) {
429 LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei);
430 return NULL;
431 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100432
Harald Weltea0fe72d2011-02-14 15:51:57 +0100433 sap = lapd_sap_find(teip, sapi);
434 if (!sap) {
435 LOGP(DMI, LOGL_INFO, "No SAP for TEI=%u / SAPI=%u, "
436 "allocating\n", tei, sapi);
437 sap = lapd_sap_alloc(teip, sapi);
438 }
439
440 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
441 "ilen %d teip %p vs %d va %d vr %d len %d\n",
442 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
443 ns, nr, *ilen, teip, sap->vs, sap->va, sap->vr, len);
444
Harald Welte716d2a42011-02-05 17:29:05 +0100445 switch (cmd) {
446 case LAPD_CMD_I:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100447 if (ns != sap->vr) {
448 DEBUGP(DMI, "ns %d != vr %d\n", ns, sap->vr);
449 if (ns == ((sap->vr - 1) & 0x7f)) {
Harald Welte716d2a42011-02-05 17:29:05 +0100450 DEBUGP(DMI, "DOUBLE FRAME, ignoring\n");
451 cmd = 0; // ignore
452 } else {
Harald Welte52a0b122011-08-09 21:24:47 +0200453 LOGP(DMI, LOGL_ERROR, "LAPD: Out of order "
454 "ns %d != vr %d, ignoring\n", ns, sap->vr);
455 return NULL;
Harald Welte716d2a42011-02-05 17:29:05 +0100456 };
457 } else {
458 //printf("IN SEQUENCE\n");
Harald Weltea0fe72d2011-02-14 15:51:57 +0100459 sap->vr = (ns + 1) & 0x7f; // FIXME: hack!
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100460 };
Harald Welte716d2a42011-02-05 17:29:05 +0100461
462 break;
463 case LAPD_CMD_UI:
464 break;
465 case LAPD_CMD_SABME:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100466 sap->vs = 0;
467 sap->vr = 0;
468 sap->va = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100469
470 // ua
471 resp[l++] = data[0];
472 resp[l++] = (tei << 1) | 1;
473 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100474 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100475 if (teip->state != LAPD_TEI_ACTIVE) {
476 if (teip->state == LAPD_TEI_ASSIGNED) {
477 lapd_tei_set_state(teip,
478 LAPD_TEI_ACTIVE);
479 //printf("ASSIGNED and ACTIVE\n");
480 } else {
481#if 0
482 DEBUGP(DMI, "rr in strange state, send rej\n");
483
484 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100485 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100486 resp[l++] = (tei << 1) | 1;
487 resp[l++] = 0x09; //rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100488 resp[l++] = ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100489 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100490 pf = 0; // dont reply
491#endif
492 };
493 };
494
495 *prim = LAPD_MPH_ACTIVATE_IND;
496 break;
Harald Welte1a00d822011-02-11 18:34:51 +0100497 case LAPD_CMD_UA:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100498 sap->vs = 0;
499 sap->vr = 0;
500 sap->va = 0;
Harald Welte1a00d822011-02-11 18:34:51 +0100501 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100502 lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE);
Harald Welte1a00d822011-02-11 18:34:51 +0100503 *prim = LAPD_MPH_ACTIVATE_IND;
504 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100505 case LAPD_CMD_RR:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100506 sap->va = (nr & 0x7f);
Harald Welte716d2a42011-02-05 17:29:05 +0100507#if 0
508 if (teip->state != LAPD_TEI_ACTIVE) {
509 if (teip->state == LAPD_TEI_ASSIGNED) {
510 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
511 *prim = LAPD_MPH_ACTIVATE_IND;
512 //printf("ASSIGNED and ACTIVE\n");
513 } else {
514#if 0
515 DEBUGP(DMI, "rr in strange " "state, send rej\n");
516
517 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100518 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100519 resp[l++] = (tei << 1) | 1;
520 resp[l++] = 0x09; //rej
521 resp[l++] =
Harald Weltea0fe72d2011-02-14 15:51:57 +0100522 ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100523 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100524 pf = 0; // dont reply
525#endif
526 };
527 };
528#endif
529 if (pf) {
530 // interrogating us, send rr
531 resp[l++] = data[0];
532 resp[l++] = (tei << 1) | 1;
533 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100534 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Harald Welte716d2a42011-02-05 17:29:05 +0100535
Harald Welted38f1052011-02-05 19:13:00 +0100536 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100537
538 };
539 break;
540 case LAPD_CMD_FRMR:
541 // frame reject
542#if 0
543 if (teip->state == LAPD_TEI_ACTIVE)
544 *prim = LAPD_MPH_DEACTIVATE_IND;
545 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
546#endif
Harald Welte0ae57552011-02-05 18:33:12 +0100547 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100548 break;
549 case LAPD_CMD_DISC:
550 // disconnect
551 resp[l++] = data[0];
552 resp[l++] = (tei << 1) | 1;
553 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100554 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100555 lapd_tei_set_state(teip, LAPD_TEI_NONE);
556 break;
557 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100558 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
559 tei, cmd);
560 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100561 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100562
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100563 if (typ == LAPD_TYPE_I) {
Harald Welte0ae57552011-02-05 18:33:12 +0100564 /* send rr
565 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
566 * 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
567 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100568
Harald Welte0ae57552011-02-05 18:33:12 +0100569 /* interrogating us, send rr */
Harald Welte0abc11a2011-02-05 17:16:26 +0100570 DEBUGP(DMI, "Sending RR response\n");
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100571 resp[l++] = data[0];
572 resp[l++] = (tei << 1) | 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100573 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100574 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100575
Harald Welted38f1052011-02-05 19:13:00 +0100576 li->transmit_cb(resp, l, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100577
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500578 if (cmd != 0) {
579 *prim = LAPD_DL_DATA_IND;
580 return contents;
581 }
582 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
583 *prim = LAPD_DL_UNITDATA_IND;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100584 return contents;
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500585 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100586
587 return NULL;
588};
589
Harald Weltedcf42e62011-02-13 11:58:21 +0100590/* low-level function to send a single SABM message */
591static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
Harald Welte1a00d822011-02-11 18:34:51 +0100592{
593 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
594 if (!msg)
595 return -ENOMEM;
596
597 DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
598
Harald Welte1a00d822011-02-11 18:34:51 +0100599 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
600 msgb_put_u8(msg, (tei << 1) | 1);
601 msgb_put_u8(msg, 0x7F);
602
603 li->transmit_cb(msg->data, msg->len, li->cbdata);
604
605 msgb_free(msg);
606
607 return 0;
608}
609
Harald Weltedcf42e62011-02-13 11:58:21 +0100610/* timer call-back function for SABM re-transmission */
611static void sabme_timer_cb(void *_sap)
612{
613 struct lapd_sap *sap = _sap;
614
615 lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi);
616
Harald Welte909212f2011-02-13 15:42:07 +0100617 if (sap->state == SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200618 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
Harald Weltedcf42e62011-02-13 11:58:21 +0100619}
620
621/* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
622int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
623{
624 struct lapd_sap *sap;
625 struct lapd_tei *teip;
626
627 teip = teip_from_tei(li, tei);
628 if (!teip)
629 teip = lapd_tei_alloc(li, tei);
630
631 sap = lapd_sap_find(teip, sapi);
632 if (sap)
633 return -EEXIST;
634
635 sap = lapd_sap_alloc(teip, sapi);
636
637 lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS);
638
639 return 0;
640}
641
642/* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
643int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
644{
645 struct lapd_tei *teip;
646 struct lapd_sap *sap;
647
648 teip = teip_from_tei(li, tei);
649 if (!teip)
650 return -ENODEV;
651
652 sap = lapd_sap_find(teip, sapi);
653 if (!sap)
654 return -ENODEV;
655
Harald Welte909212f2011-02-13 15:42:07 +0100656 lapd_sap_set_state(teip, sapi, SAP_STATE_INACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100657
658 llist_del(&sap->list);
659 talloc_free(sap);
660
661 return 0;
662}
663
664/* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100665void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
Harald Welted38f1052011-02-05 19:13:00 +0100666 uint8_t *data, unsigned int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100667{
Harald Welted38f1052011-02-05 19:13:00 +0100668 struct lapd_tei *teip = teip_from_tei(li, tei);
Harald Weltea0fe72d2011-02-14 15:51:57 +0100669 struct lapd_sap *sap;
Harald Welted38f1052011-02-05 19:13:00 +0100670
671 if (!teip) {
672 LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing "
673 "TEI %u\n", tei);
674 return;
675 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100676
Harald Weltea0fe72d2011-02-14 15:51:57 +0100677 sap = lapd_sap_find(teip, sapi);
678 if (!sap) {
679 LOGP(DMI, LOGL_INFO, "Tx on unknown SAPI=%u in TEI=%u, "
680 "allocating\n", sapi, tei);
681 sap = lapd_sap_alloc(teip, sapi);
682 }
683
Harald Welte0ae57552011-02-05 18:33:12 +0100684 /* prepend stuff */
Harald Welte30fe6412011-02-04 20:34:08 +0100685 uint8_t buf[10000];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100686 memset(buf, 0, sizeof(buf));
Harald Welte30fe6412011-02-04 20:34:08 +0100687 memmove(buf + 4, data, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100688 len += 4;
689
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100690 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
691 buf[1] = (tei << 1) | 1;
Harald Weltea0fe72d2011-02-14 15:51:57 +0100692 buf[2] = (LAPD_NS(sap) << 1);
693 buf[3] = (LAPD_NR(sap) << 1) | 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100694
Harald Weltea0fe72d2011-02-14 15:51:57 +0100695 sap->vs = (sap->vs + 1) & 0x7f;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100696
Harald Welted38f1052011-02-05 19:13:00 +0100697 li->transmit_cb(buf, len, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100698};
Harald Welted38f1052011-02-05 19:13:00 +0100699
Harald Weltedcf42e62011-02-13 11:58:21 +0100700/* Allocate a new LAPD instance */
Harald Welte1a00d822011-02-11 18:34:51 +0100701struct lapd_instance *lapd_instance_alloc(int network_side,
702 void (*tx_cb)(uint8_t *data, int len,
Harald Welted38f1052011-02-05 19:13:00 +0100703 void *cbdata), void *cbdata)
704{
705 struct lapd_instance *li;
706
707 li = talloc_zero(NULL, struct lapd_instance);
708 if (!li)
709 return NULL;
710
711 li->transmit_cb = tx_cb;
712 li->cbdata = cbdata;
Harald Welte1a00d822011-02-11 18:34:51 +0100713 li->network_side = network_side;
Harald Welted38f1052011-02-05 19:13:00 +0100714 INIT_LLIST_HEAD(&li->tei_list);
715
716 return li;
717}