blob: 66ff05e099aaa1c7f4fdec4fddc79a2bb1986969 [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 Welte6e4c26a2011-08-09 21:41:35 +0200192 LOGP(DMI, LOGL_INFO, "LAPD 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
Harald Welte6e4c26a2011-08-09 21:41:35 +0200236 LOGP(DMI, LOGL_INFO, "LAPD Allocating SAP for SAPI=%u / TEI=%u\n",
Harald Weltedcf42e62011-02-13 11:58:21 +0100237 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
Harald Welte6e4c26a2011-08-09 21:41:35 +0200256 LOGP(DMI, LOGL_INFO, "LAPD state change on TEI %u / SAPI %u: "
257 "%s -> %s\n", teip->tei, sapi,
258 lapd_sap_states[sap->state], lapd_sap_states[newstate]);
Harald Weltedcf42e62011-02-13 11:58:21 +0100259 switch (sap->state) {
260 case SAP_STATE_SABM_RETRANS:
261 if (newstate != SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200262 osmo_timer_del(&sap->sabme_timer);
Harald Weltedcf42e62011-02-13 11:58:21 +0100263 break;
264 default:
265 if (newstate == SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200266 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
Harald Weltedcf42e62011-02-13 11:58:21 +0100267 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 Welte6e4c26a2011-08-09 21:41:35 +0200284 DEBUGP(DMI, "LAPD TEIMGR: entity %x, ref %x, mt %x, action %x, e %x\n",
285 entity, ref, mt, action, e);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100286
287 switch (mt) {
Harald Welted38f1052011-02-05 19:13:00 +0100288 case 0x01: /* IDENTITY REQUEST */
Harald Welte6e4c26a2011-08-09 21:41:35 +0200289 DEBUGP(DMI, "LAPD TEIMGR: identity request for TEI %u\n", action);
Harald Welte716d2a42011-02-05 17:29:05 +0100290
Harald Welte8fc66a02011-02-05 19:51:05 +0100291 teip = teip_from_tei(li, action);
Harald Welted38f1052011-02-05 19:13:00 +0100292 if (!teip) {
Harald Welte8fc66a02011-02-05 19:51:05 +0100293 LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action);
Holger Hans Peter Freyther13673742011-04-04 19:17:58 +0200294 teip = lapd_tei_alloc(li, action);
Harald Welte30fe6412011-02-04 20:34:08 +0100295 }
Harald Welted38f1052011-02-05 19:13:00 +0100296
297 /* Send ACCEPT */
298 memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8);
Harald Welte8fc66a02011-02-05 19:51:05 +0100299 resp[7] = (action << 1) | 1;
Harald Welted38f1052011-02-05 19:13:00 +0100300 li->transmit_cb(resp, 8, li->cbdata);
301
302 if (teip->state == LAPD_TEI_NONE)
303 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
304 break;
Harald Welte30fe6412011-02-04 20:34:08 +0100305 default:
Harald Welte6e4c26a2011-08-09 21:41:35 +0200306 LOGP(DMI, LOGL_NOTICE, "LAPD TEIMGR: unknown mt %x action %x\n",
Harald Welte0ae57552011-02-05 18:33:12 +0100307 mt, action);
308 break;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100309 };
310};
311
Harald Weltedcf42e62011-02-13 11:58:21 +0100312/* General input function for any data received for this LAPD instance */
Harald Welted38f1052011-02-05 19:13:00 +0100313uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len,
314 int *ilen, lapd_mph_type *prim)
Harald Welte30fe6412011-02-04 20:34:08 +0100315{
Harald Welte716d2a42011-02-05 17:29:05 +0100316 uint8_t sapi, cr, tei, command;
317 int pf, ns, nr;
318 uint8_t *contents;
Harald Welted38f1052011-02-05 19:13:00 +0100319 struct lapd_tei *teip;
Harald Weltea0fe72d2011-02-14 15:51:57 +0100320 struct lapd_sap *sap;
Harald Welte716d2a42011-02-05 17:29:05 +0100321
322 uint8_t resp[8];
323 int l = 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100324
325 *ilen = 0;
326 *prim = 0;
327
328 if (len < 2) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200329 LOGP(DMI, LOGL_ERROR, "LAPD receive len %d < 2, ignoring\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100330 return NULL;
331 };
332
333 if ((data[0] & 1) != 0 || (data[1] & 1) != 1) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200334 LOGP(DMI, LOGL_ERROR, "LAPD address field %x/%x not well formed\n",
335 data[0], data[1]);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100336 return NULL;
337 };
338
Harald Welte716d2a42011-02-05 17:29:05 +0100339 sapi = data[0] >> 2;
340 cr = (data[0] >> 1) & 1;
341 tei = data[1] >> 1;
Harald Welted38f1052011-02-05 19:13:00 +0100342 command = li->network_side ^ cr;
Harald Welte0abc11a2011-02-05 17:16:26 +0100343 //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100344
345 if (len < 3) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200346 LOGP(DMI, LOGL_ERROR, "LAPD receive len %d < 3, ignoring\n", len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100347 return NULL;
348 };
349
350 lapd_msg_type typ = 0;
351 lapd_cmd_type cmd = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100352 pf = -1;
353 ns = -1;
354 nr = -1;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100355 if ((data[2] & 1) == 0) {
356 typ = LAPD_TYPE_I;
Harald Welte52a0b122011-08-09 21:24:47 +0200357 if (len < 4) {
358 LOGP(DMI, LOGL_ERROR, "LAPD I frame, len %d < 4\n", len);
359 return NULL;
360 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100361 ns = data[2] >> 1;
362 nr = data[3] >> 1;
363 pf = data[3] & 1;
364 cmd = LAPD_CMD_I;
365 } else if ((data[2] & 3) == 1) {
366 typ = LAPD_TYPE_S;
Harald Welte52a0b122011-08-09 21:24:47 +0200367 if (len < 4) {
368 LOGP(DMI, LOGL_ERROR, "LAPD S frame, len %d < 4\n", len);
369 return NULL;
370 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100371 nr = data[3] >> 1;
372 pf = data[3] & 1;
373 switch (data[2]) {
Harald Welte30fe6412011-02-04 20:34:08 +0100374 case 0x1:
375 cmd = LAPD_CMD_RR;
376 break;
377 case 0x5:
378 cmd = LAPD_CMD_RNR;
379 break;
380 case 0x9:
381 cmd = LAPD_CMD_REJ;
382 break;
383 default:
Harald Welte6e4c26a2011-08-09 21:41:35 +0200384 LOGP(DMI, LOGL_ERROR, "LAPD unknown S cmd %x\n", data[2]);
Harald Welte0ae57552011-02-05 18:33:12 +0100385 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100386 };
387 } else if ((data[2] & 3) == 3) {
388 typ = LAPD_TYPE_U;
389 pf = (data[2] >> 4) & 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100390 int val = data[2] & ~(1 << 4);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100391 switch (val) {
Harald Welte30fe6412011-02-04 20:34:08 +0100392 case 0x6f:
393 cmd = LAPD_CMD_SABME;
394 break;
395 case 0x0f:
396 cmd = LAPD_CMD_DM;
397 break;
398 case 0x03:
399 cmd = LAPD_CMD_UI;
400 break;
401 case 0x43:
402 cmd = LAPD_CMD_DISC;
403 break;
404 case 0x63:
405 cmd = LAPD_CMD_UA;
406 break;
407 case 0x87:
408 cmd = LAPD_CMD_FRMR;
409 break;
410 case 0xaf:
411 cmd = LAPD_CMD_XID;
412 break;
413
414 default:
Harald Welte6e4c26a2011-08-09 21:41:35 +0200415 LOGP(DMI, LOGL_ERROR, "LAPD unknown U cmd %x "
Harald Welte0ae57552011-02-05 18:33:12 +0100416 "(pf %x data %x)\n", val, pf, data[2]);
417 return NULL;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100418 };
419 };
Harald Welte30fe6412011-02-04 20:34:08 +0100420
Harald Welte716d2a42011-02-05 17:29:05 +0100421 contents = &data[4];
Harald Welte30fe6412011-02-04 20:34:08 +0100422 if (typ == LAPD_TYPE_U)
423 contents--;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100424 *ilen = len - (contents - data);
Harald Welte30fe6412011-02-04 20:34:08 +0100425
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100426 if (tei == 127)
Harald Welted38f1052011-02-05 19:13:00 +0100427 lapd_tei_receive(li, contents, *ilen);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100428
Harald Welted38f1052011-02-05 19:13:00 +0100429 teip = teip_from_tei(li, tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100430 if (!teip) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200431 LOGP(DMI, LOGL_NOTICE, "LAPD Unknown TEI %u\n", tei);
Harald Welte716d2a42011-02-05 17:29:05 +0100432 return NULL;
433 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100434
Harald Weltea0fe72d2011-02-14 15:51:57 +0100435 sap = lapd_sap_find(teip, sapi);
436 if (!sap) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200437 LOGP(DMI, LOGL_INFO, "LAPD No SAP for TEI=%u / SAPI=%u, "
Harald Weltea0fe72d2011-02-14 15:51:57 +0100438 "allocating\n", tei, sapi);
439 sap = lapd_sap_alloc(teip, sapi);
440 }
441
442 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
443 "ilen %d teip %p vs %d va %d vr %d len %d\n",
444 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
445 ns, nr, *ilen, teip, sap->vs, sap->va, sap->vr, len);
446
Harald Welte716d2a42011-02-05 17:29:05 +0100447 switch (cmd) {
448 case LAPD_CMD_I:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100449 if (ns != sap->vr) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200450 DEBUGP(DMI, "LAPD ns %d != vr %d\n", ns, sap->vr);
Harald Weltea0fe72d2011-02-14 15:51:57 +0100451 if (ns == ((sap->vr - 1) & 0x7f)) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200452 LOGP(DMI, LOGL_NOTICE, "LAPD double frame, "
453 "ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100454 cmd = 0; // ignore
455 } else {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200456 LOGP(DMI, LOGL_ERROR, "LAPD Out of order "
Harald Welte52a0b122011-08-09 21:24:47 +0200457 "ns %d != vr %d, ignoring\n", ns, sap->vr);
458 return NULL;
Harald Welte716d2a42011-02-05 17:29:05 +0100459 };
460 } else {
461 //printf("IN SEQUENCE\n");
Harald Weltea0fe72d2011-02-14 15:51:57 +0100462 sap->vr = (ns + 1) & 0x7f; // FIXME: hack!
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100463 };
Harald Welte716d2a42011-02-05 17:29:05 +0100464
465 break;
466 case LAPD_CMD_UI:
467 break;
468 case LAPD_CMD_SABME:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100469 sap->vs = 0;
470 sap->vr = 0;
471 sap->va = 0;
Harald Welte716d2a42011-02-05 17:29:05 +0100472
473 // ua
474 resp[l++] = data[0];
475 resp[l++] = (tei << 1) | 1;
476 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100477 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100478 if (teip->state != LAPD_TEI_ACTIVE) {
479 if (teip->state == LAPD_TEI_ASSIGNED) {
480 lapd_tei_set_state(teip,
481 LAPD_TEI_ACTIVE);
482 //printf("ASSIGNED and ACTIVE\n");
483 } else {
484#if 0
485 DEBUGP(DMI, "rr in strange state, send rej\n");
486
487 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100488 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100489 resp[l++] = (tei << 1) | 1;
490 resp[l++] = 0x09; //rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100491 resp[l++] = ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100492 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100493 pf = 0; // dont reply
494#endif
495 };
496 };
497
498 *prim = LAPD_MPH_ACTIVATE_IND;
499 break;
Harald Welte1a00d822011-02-11 18:34:51 +0100500 case LAPD_CMD_UA:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100501 sap->vs = 0;
502 sap->vr = 0;
503 sap->va = 0;
Harald Welte1a00d822011-02-11 18:34:51 +0100504 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100505 lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE);
Harald Welte1a00d822011-02-11 18:34:51 +0100506 *prim = LAPD_MPH_ACTIVATE_IND;
507 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100508 case LAPD_CMD_RR:
Harald Weltea0fe72d2011-02-14 15:51:57 +0100509 sap->va = (nr & 0x7f);
Harald Welte716d2a42011-02-05 17:29:05 +0100510#if 0
511 if (teip->state != LAPD_TEI_ACTIVE) {
512 if (teip->state == LAPD_TEI_ASSIGNED) {
513 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
514 *prim = LAPD_MPH_ACTIVATE_IND;
515 //printf("ASSIGNED and ACTIVE\n");
516 } else {
517#if 0
518 DEBUGP(DMI, "rr in strange " "state, send rej\n");
519
520 // rej
Harald Weltea0fe72d2011-02-14 15:51:57 +0100521 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
Harald Welte716d2a42011-02-05 17:29:05 +0100522 resp[l++] = (tei << 1) | 1;
523 resp[l++] = 0x09; //rej
524 resp[l++] =
Harald Weltea0fe72d2011-02-14 15:51:57 +0100525 ((sap->vr + 1) << 1) | 0;
Harald Welted38f1052011-02-05 19:13:00 +0100526 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100527 pf = 0; // dont reply
528#endif
529 };
530 };
531#endif
532 if (pf) {
533 // interrogating us, send rr
534 resp[l++] = data[0];
535 resp[l++] = (tei << 1) | 1;
536 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100537 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Harald Welte716d2a42011-02-05 17:29:05 +0100538
Harald Welted38f1052011-02-05 19:13:00 +0100539 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100540
541 };
542 break;
543 case LAPD_CMD_FRMR:
544 // frame reject
545#if 0
546 if (teip->state == LAPD_TEI_ACTIVE)
547 *prim = LAPD_MPH_DEACTIVATE_IND;
548 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
549#endif
Harald Welte0ae57552011-02-05 18:33:12 +0100550 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
Harald Welte716d2a42011-02-05 17:29:05 +0100551 break;
552 case LAPD_CMD_DISC:
553 // disconnect
554 resp[l++] = data[0];
555 resp[l++] = (tei << 1) | 1;
556 resp[l++] = 0x73;
Harald Welted38f1052011-02-05 19:13:00 +0100557 li->transmit_cb(resp, l, li->cbdata);
Harald Welte716d2a42011-02-05 17:29:05 +0100558 lapd_tei_set_state(teip, LAPD_TEI_NONE);
559 break;
560 default:
Harald Welte0ae57552011-02-05 18:33:12 +0100561 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
562 tei, cmd);
563 break;
Harald Welte716d2a42011-02-05 17:29:05 +0100564 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100565
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100566 if (typ == LAPD_TYPE_I) {
Harald Welte0ae57552011-02-05 18:33:12 +0100567 /* send rr
568 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
569 * 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
570 */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100571
Harald Welte0ae57552011-02-05 18:33:12 +0100572 /* interrogating us, send rr */
Harald Welte6e4c26a2011-08-09 21:41:35 +0200573 DEBUGP(DMI, "LAPD Sending RR response\n");
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100574 resp[l++] = data[0];
575 resp[l++] = (tei << 1) | 1;
Harald Welte30fe6412011-02-04 20:34:08 +0100576 resp[l++] = 0x01; // rr
Harald Weltea0fe72d2011-02-14 15:51:57 +0100577 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100578
Harald Welted38f1052011-02-05 19:13:00 +0100579 li->transmit_cb(resp, l, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100580
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500581 if (cmd != 0) {
582 *prim = LAPD_DL_DATA_IND;
583 return contents;
584 }
585 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
586 *prim = LAPD_DL_UNITDATA_IND;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100587 return contents;
Matthew Fredrickson69245a02010-03-15 12:24:39 -0500588 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100589
590 return NULL;
591};
592
Harald Weltedcf42e62011-02-13 11:58:21 +0100593/* low-level function to send a single SABM message */
594static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
Harald Welte1a00d822011-02-11 18:34:51 +0100595{
596 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
597 if (!msg)
598 return -ENOMEM;
599
Harald Welte6e4c26a2011-08-09 21:41:35 +0200600 LOGP(DMI, LOGL_INFO, "LAPD Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
Harald Welte1a00d822011-02-11 18:34:51 +0100601
Harald Welte1a00d822011-02-11 18:34:51 +0100602 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
603 msgb_put_u8(msg, (tei << 1) | 1);
604 msgb_put_u8(msg, 0x7F);
605
606 li->transmit_cb(msg->data, msg->len, li->cbdata);
607
608 msgb_free(msg);
609
610 return 0;
611}
612
Harald Weltedcf42e62011-02-13 11:58:21 +0100613/* timer call-back function for SABM re-transmission */
614static void sabme_timer_cb(void *_sap)
615{
616 struct lapd_sap *sap = _sap;
617
618 lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi);
619
Harald Welte909212f2011-02-13 15:42:07 +0100620 if (sap->state == SAP_STATE_SABM_RETRANS)
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200621 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
Harald Weltedcf42e62011-02-13 11:58:21 +0100622}
623
624/* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
625int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
626{
627 struct lapd_sap *sap;
628 struct lapd_tei *teip;
629
630 teip = teip_from_tei(li, tei);
631 if (!teip)
632 teip = lapd_tei_alloc(li, tei);
633
634 sap = lapd_sap_find(teip, sapi);
635 if (sap)
636 return -EEXIST;
637
638 sap = lapd_sap_alloc(teip, sapi);
639
640 lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS);
641
642 return 0;
643}
644
645/* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
646int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
647{
648 struct lapd_tei *teip;
649 struct lapd_sap *sap;
650
651 teip = teip_from_tei(li, tei);
652 if (!teip)
653 return -ENODEV;
654
655 sap = lapd_sap_find(teip, sapi);
656 if (!sap)
657 return -ENODEV;
658
Harald Welte909212f2011-02-13 15:42:07 +0100659 lapd_sap_set_state(teip, sapi, SAP_STATE_INACTIVE);
Harald Weltedcf42e62011-02-13 11:58:21 +0100660
661 llist_del(&sap->list);
662 talloc_free(sap);
663
664 return 0;
665}
666
667/* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100668void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
Harald Welted38f1052011-02-05 19:13:00 +0100669 uint8_t *data, unsigned int len)
Harald Welte30fe6412011-02-04 20:34:08 +0100670{
Harald Welted38f1052011-02-05 19:13:00 +0100671 struct lapd_tei *teip = teip_from_tei(li, tei);
Harald Weltea0fe72d2011-02-14 15:51:57 +0100672 struct lapd_sap *sap;
Harald Welted38f1052011-02-05 19:13:00 +0100673
674 if (!teip) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200675 LOGP(DMI, LOGL_ERROR, "LAPD Cannot transmit on "
676 "non-existing TEI %u\n", tei);
Harald Welted38f1052011-02-05 19:13:00 +0100677 return;
678 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100679
Harald Weltea0fe72d2011-02-14 15:51:57 +0100680 sap = lapd_sap_find(teip, sapi);
681 if (!sap) {
Harald Welte6e4c26a2011-08-09 21:41:35 +0200682 LOGP(DMI, LOGL_INFO, "LAPD Tx on unknown SAPI=%u "
683 "in TEI=%u, allocating\n", sapi, tei);
Harald Weltea0fe72d2011-02-14 15:51:57 +0100684 sap = lapd_sap_alloc(teip, sapi);
685 }
686
Harald Welte0ae57552011-02-05 18:33:12 +0100687 /* prepend stuff */
Harald Welte30fe6412011-02-04 20:34:08 +0100688 uint8_t buf[10000];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100689 memset(buf, 0, sizeof(buf));
Harald Welte30fe6412011-02-04 20:34:08 +0100690 memmove(buf + 4, data, len);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100691 len += 4;
692
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100693 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
694 buf[1] = (tei << 1) | 1;
Harald Weltea0fe72d2011-02-14 15:51:57 +0100695 buf[2] = (LAPD_NS(sap) << 1);
696 buf[3] = (LAPD_NR(sap) << 1) | 0;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100697
Harald Weltea0fe72d2011-02-14 15:51:57 +0100698 sap->vs = (sap->vs + 1) & 0x7f;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100699
Harald Welted38f1052011-02-05 19:13:00 +0100700 li->transmit_cb(buf, len, li->cbdata);
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100701};
Harald Welted38f1052011-02-05 19:13:00 +0100702
Harald Weltedcf42e62011-02-13 11:58:21 +0100703/* Allocate a new LAPD instance */
Harald Welte1a00d822011-02-11 18:34:51 +0100704struct lapd_instance *lapd_instance_alloc(int network_side,
705 void (*tx_cb)(uint8_t *data, int len,
Harald Welted38f1052011-02-05 19:13:00 +0100706 void *cbdata), void *cbdata)
707{
708 struct lapd_instance *li;
709
710 li = talloc_zero(NULL, struct lapd_instance);
711 if (!li)
712 return NULL;
713
714 li->transmit_cb = tx_cb;
715 li->cbdata = cbdata;
Harald Welte1a00d822011-02-11 18:34:51 +0100716 li->network_side = network_side;
Harald Welted38f1052011-02-05 19:13:00 +0100717 INIT_LLIST_HEAD(&li->tei_list);
718
719 return li;
720}