blob: 2e0dd0805931439b441318fbac61745b9708897b [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC minimal LAPD implementation */
2
3/* (C) 2009 by oystein@homelien.no
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
6 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26/* 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
32#include "internal.h"
33
34#include <stdio.h>
35#include <string.h>
36#include <assert.h>
37#include <errno.h>
38
Pablo Neira Ayuso355ce692011-07-05 14:53:37 +020039#include <osmocom/abis/lapd.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020040
41#include <osmocom/core/linuxlist.h>
42#include <osmocom/core/logging.h>
43#include <talloc.h>
44#include <osmocom/core/msgb.h>
45#include <osmocom/core/timer.h>
Pablo Neira Ayuso0b099b22011-06-09 13:14:11 +020046#include <osmocom/abis/logging.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047
48#define SABM_INTERVAL 0, 300000
49
50typedef enum {
51 LAPD_TEI_NONE = 0,
52 LAPD_TEI_ASSIGNED,
53 LAPD_TEI_ACTIVE,
54} lapd_tei_state;
55
56const char *lapd_tei_states[] = {
57 "NONE",
58 "ASSIGNED",
59 "ACTIVE",
60};
61
62typedef enum {
63 LAPD_TYPE_NONE = 0,
64
65 LAPD_TYPE_I,
66 LAPD_TYPE_S,
67 LAPD_TYPE_U,
68} lapd_msg_type;
69
70typedef enum {
71 /* commands/responses */
72 LAPD_CMD_NONE = 0,
73
74 LAPD_CMD_I,
75 LAPD_CMD_RR,
76 LAPD_CMD_RNR,
77 LAPD_CMD_REJ,
78
79 LAPD_CMD_SABME,
80 LAPD_CMD_DM,
81 LAPD_CMD_UI,
82 LAPD_CMD_DISC,
83 LAPD_CMD_UA,
84 LAPD_CMD_FRMR,
85 LAPD_CMD_XID,
86} lapd_cmd_type;
87
88const char *lapd_cmd_types[] = {
89 "NONE",
90
91 "I",
92 "RR",
93 "RNR",
94 "REJ",
95
96 "SABME",
97 "DM",
98 "UI",
99 "DISC",
100 "UA",
101 "FRMR",
102 "XID",
103
104};
105
106enum lapd_sap_state {
107 SAP_STATE_INACTIVE,
108 SAP_STATE_SABM_RETRANS,
109 SAP_STATE_ACTIVE,
110};
111
112const char *lapd_sap_states[] = {
113 "INACTIVE",
114 "SABM_RETRANS",
115 "ACTIVE",
116};
117
118const char *lapd_msg_types = "?ISU";
119
120/* structure representing an allocated TEI within a LAPD instance */
121struct lapd_tei {
122 struct llist_head list;
123 struct lapd_instance *li;
124 uint8_t tei;
125 lapd_tei_state state;
126
127 struct llist_head sap_list;
128};
129
130/* Structure representing a SAP within a TEI. We use this for TE-mode to
131 * re-transmit SABM */
132struct lapd_sap {
133 struct llist_head list;
134 struct lapd_tei *tei;
135 uint8_t sapi;
136 enum lapd_sap_state state;
137
138 /* A valid N(R) value is one that is in the range V(A) ≤ N(R) ≤ V(S). */
139 int vs; /* next to be transmitted */
140 int va; /* last acked by peer */
141 int vr; /* next expected to be received */
142
143 struct osmo_timer_list sabme_timer; /* timer to re-transmit SABM message */
144};
145
146/* 3.5.2.2 Send state variable V(S)
147 * Each point-to-point data link connection endpoint shall have an associated V(S) when using I frame
148 * commands. V(S) denotes the sequence number of the next I frame to be transmitted. The V(S) can
149 * take on the value 0 through n minus 1. The value of V(S) shall be incremented by 1 with each
150 * successive I frame transmission, and shall not exceed V(A) by more than the maximum number of
151 * outstanding I frames k. The value of k may be in the range of 1 ≤ k ≤ 127.
152 *
153 * 3.5.2.3 Acknowledge state variable V(A)
154 * Each point-to-point data link connection endpoint shall have an associated V(A) when using I frame
155 * commands and supervisory frame commands/responses. V(A) identifies the last I frame that has been
156 * acknowledged by its peer [V(A) − 1 equals the N(S) of the last acknowledged I frame]. V(A) can
157 * take on the value 0 through n minus 1. The value of V(A) shall be updated by the valid N(R) values
158 * 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) ≤
159 * V(S).
160 *
161 * 3.5.2.5 Receive state variable V(R)
162 * Each point-to-point data link connection endpoint shall have an associated V(R) when using I frame
163 * commands and supervisory frame commands/responses. V(R) denotes the sequence number of the
164 * next in-sequence I frame expected to be received. V(R) can take on the value 0 through n minus 1.
165 * The value of V(R) shall be incremented by one with the receipt of an error-free, in-sequence I frame
166 * whose N(S) equals V(R).
167 */
168#define LAPD_NS(sap) (sap->vs)
169#define LAPD_NR(sap) (sap->vr)
170
171/* 3.5.2.4 Send sequence number N(S)
172 * Only I frames contain N(S), the send sequence number of transmitted I frames. At the time that an in-
173 * sequence I frame is designated for transmission, the value of N(S) is set equal to V(S).
174 *
175 * 3.5.2.6 Receive sequence number N(R)
176 * All I frames and supervisory frames contain N(R), the expected send sequence number of the next
177 * received I frame. At the time that a frame of the above types is designated for transmission, the value
178 * of N(R) is set equal to V(R). N(R) indicates that the data link layer entity transmitting the N(R) has
179 * correctly received all I frames numbered up to and including N(R) − 1.
180 */
181
182/* Resolve TEI structure from given numeric TEI */
183static struct lapd_tei *teip_from_tei(struct lapd_instance *li, uint8_t tei)
184{
185 struct lapd_tei *lt;
186
187 llist_for_each_entry(lt, &li->tei_list, list) {
188 if (lt->tei == tei)
189 return lt;
190 }
191 return NULL;
192};
193
194static void lapd_tei_set_state(struct lapd_tei *teip, int newstate)
195{
196 DEBUGP(DMI, "state change on TEI %d: %s -> %s\n", teip->tei,
197 lapd_tei_states[teip->state], lapd_tei_states[newstate]);
198 teip->state = newstate;
199};
200
201/* Allocate a new TEI */
202struct lapd_tei *lapd_tei_alloc(struct lapd_instance *li, uint8_t tei)
203{
204 struct lapd_tei *teip;
205
206 teip = talloc_zero(li, struct lapd_tei);
207 if (!teip)
208 return NULL;
209
210 teip->li = li;
211 teip->tei = tei;
212 llist_add(&teip->list, &li->tei_list);
213 INIT_LLIST_HEAD(&teip->sap_list);
214
215 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
216
217 return teip;
218}
219
220/* Find a SAP within a given TEI */
221static struct lapd_sap *lapd_sap_find(struct lapd_tei *teip, uint8_t sapi)
222{
223 struct lapd_sap *sap;
224
225 llist_for_each_entry(sap, &teip->sap_list, list) {
226 if (sap->sapi == sapi)
227 return sap;
228 }
229
230 return NULL;
231}
232
233static void sabme_timer_cb(void *_sap);
234
235/* Allocate a new SAP within a given TEI */
236static struct lapd_sap *lapd_sap_alloc(struct lapd_tei *teip, uint8_t sapi)
237{
238 struct lapd_sap *sap = talloc_zero(teip, struct lapd_sap);
239
240 LOGP(DMI, LOGL_INFO, "Allocating SAP for SAPI=%u / TEI=%u\n",
241 sapi, teip->tei);
242
243 sap->sapi = sapi;
244 sap->tei = teip;
245 sap->sabme_timer.cb = &sabme_timer_cb;
246 sap->sabme_timer.data = sap;
247
248 llist_add(&sap->list, &teip->sap_list);
249
250 return sap;
251}
252
253static void lapd_sap_set_state(struct lapd_tei *teip, uint8_t sapi,
254 enum lapd_sap_state newstate)
255{
256 struct lapd_sap *sap = lapd_sap_find(teip, sapi);
257 if (!sap)
258 return;
259
260 DEBUGP(DMI, "state change on TEI %u / SAPI %u: %s -> %s\n", teip->tei,
261 sapi, lapd_sap_states[sap->state], lapd_sap_states[newstate]);
262 switch (sap->state) {
263 case SAP_STATE_SABM_RETRANS:
264 if (newstate != SAP_STATE_SABM_RETRANS)
265 osmo_timer_del(&sap->sabme_timer);
266 break;
267 default:
268 if (newstate == SAP_STATE_SABM_RETRANS)
269 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
270 break;
271 }
272
273 sap->state = newstate;
274};
275
276/* Input function into TEI manager */
277static void lapd_tei_receive(struct lapd_instance *li, uint8_t *data, int len)
278{
279 uint8_t entity = data[0];
280 uint8_t ref = data[1];
281 uint8_t mt = data[3];
282 uint8_t action = data[4] >> 1;
283 uint8_t e = data[4] & 1;
284 uint8_t resp[8];
285 struct lapd_tei *teip;
286
287 DEBUGP(DMI, "TEIMGR: entity %x, ref %x, mt %x, action %x, e %x\n", entity, ref, mt, action, e);
288
289 switch (mt) {
290 case 0x01: /* IDENTITY REQUEST */
291 DEBUGP(DMI, "TEIMGR: identity request for TEI %u\n", action);
292
293 teip = teip_from_tei(li, action);
294 if (!teip) {
295 LOGP(DMI, LOGL_INFO, "TEI MGR: New TEI %u\n", action);
296 teip = lapd_tei_alloc(li, action);
297 }
298
299 /* Send ACCEPT */
300 memmove(resp, "\xfe\xff\x03\x0f\x00\x00\x02\x00", 8);
301 resp[7] = (action << 1) | 1;
302 li->transmit_cb(resp, 8, li->cbdata);
303
304 if (teip->state == LAPD_TEI_NONE)
305 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
306 break;
307 default:
308 LOGP(DMI, LOGL_NOTICE, "TEIMGR: unknown mt %x action %x\n",
309 mt, action);
310 break;
311 };
312};
313
314/* General input function for any data received for this LAPD instance */
315uint8_t *lapd_receive(struct lapd_instance *li, uint8_t * data, unsigned int len,
316 int *ilen, lapd_mph_type *prim)
317{
318 uint8_t sapi, cr, tei, command;
319 int pf, ns, nr;
320 uint8_t *contents;
321 struct lapd_tei *teip;
322 struct lapd_sap *sap;
323
324 uint8_t resp[8];
325 int l = 0;
326
327 *ilen = 0;
328 *prim = 0;
329
330 if (len < 2) {
331 DEBUGP(DMI, "len %d < 2\n", len);
332 return NULL;
333 };
334
335 if ((data[0] & 1) != 0 || (data[1] & 1) != 1) {
336 DEBUGP(DMI, "address field %x/%x not well formed\n", data[0],
337 data[1]);
338 return NULL;
339 };
340
341 sapi = data[0] >> 2;
342 cr = (data[0] >> 1) & 1;
343 tei = data[1] >> 1;
344 command = li->network_side ^ cr;
345 //DEBUGP(DMI, " address sapi %x tei %d cmd %d cr %d\n", sapi, tei, command, cr);
346
347 if (len < 3) {
348 DEBUGP(DMI, "len %d < 3\n", len);
349 return NULL;
350 };
351
352 lapd_msg_type typ = 0;
353 lapd_cmd_type cmd = 0;
354 pf = -1;
355 ns = -1;
356 nr = -1;
357 if ((data[2] & 1) == 0) {
358 typ = LAPD_TYPE_I;
359 assert(len >= 4);
360 ns = data[2] >> 1;
361 nr = data[3] >> 1;
362 pf = data[3] & 1;
363 cmd = LAPD_CMD_I;
364 } else if ((data[2] & 3) == 1) {
365 typ = LAPD_TYPE_S;
366 assert(len >= 4);
367 nr = data[3] >> 1;
368 pf = data[3] & 1;
369 switch (data[2]) {
370 case 0x1:
371 cmd = LAPD_CMD_RR;
372 break;
373 case 0x5:
374 cmd = LAPD_CMD_RNR;
375 break;
376 case 0x9:
377 cmd = LAPD_CMD_REJ;
378 break;
379 default:
380 LOGP(DMI, LOGL_ERROR, "unknown LAPD S cmd %x\n", data[2]);
381 return NULL;
382 };
383 } else if ((data[2] & 3) == 3) {
384 typ = LAPD_TYPE_U;
385 pf = (data[2] >> 4) & 1;
386 int val = data[2] & ~(1 << 4);
387 switch (val) {
388 case 0x6f:
389 cmd = LAPD_CMD_SABME;
390 break;
391 case 0x0f:
392 cmd = LAPD_CMD_DM;
393 break;
394 case 0x03:
395 cmd = LAPD_CMD_UI;
396 break;
397 case 0x43:
398 cmd = LAPD_CMD_DISC;
399 break;
400 case 0x63:
401 cmd = LAPD_CMD_UA;
402 break;
403 case 0x87:
404 cmd = LAPD_CMD_FRMR;
405 break;
406 case 0xaf:
407 cmd = LAPD_CMD_XID;
408 break;
409
410 default:
411 LOGP(DMI, LOGL_ERROR, "unknown U cmd %x "
412 "(pf %x data %x)\n", val, pf, data[2]);
413 return NULL;
414 };
415 };
416
417 contents = &data[4];
418 if (typ == LAPD_TYPE_U)
419 contents--;
420 *ilen = len - (contents - data);
421
422 if (tei == 127)
423 lapd_tei_receive(li, contents, *ilen);
424
425 teip = teip_from_tei(li, tei);
426 if (!teip) {
427 LOGP(DMI, LOGL_NOTICE, "Unknown TEI %u\n", tei);
428 return NULL;
429 }
430
431 sap = lapd_sap_find(teip, sapi);
432 if (!sap) {
433 LOGP(DMI, LOGL_INFO, "No SAP for TEI=%u / SAPI=%u, "
434 "allocating\n", tei, sapi);
435 sap = lapd_sap_alloc(teip, sapi);
436 }
437
438 DEBUGP(DMI, "<- %c %s sapi %x tei %3d cmd %x pf %x ns %3d nr %3d "
439 "ilen %d teip %p vs %d va %d vr %d len %d\n",
440 lapd_msg_types[typ], lapd_cmd_types[cmd], sapi, tei, command, pf,
441 ns, nr, *ilen, teip, sap->vs, sap->va, sap->vr, len);
442
443 switch (cmd) {
444 case LAPD_CMD_I:
445 if (ns != sap->vr) {
446 DEBUGP(DMI, "ns %d != vr %d\n", ns, sap->vr);
447 if (ns == ((sap->vr - 1) & 0x7f)) {
448 DEBUGP(DMI, "DOUBLE FRAME, ignoring\n");
449 cmd = 0; // ignore
450 } else {
451 assert(0);
452 };
453 } else {
454 //printf("IN SEQUENCE\n");
455 sap->vr = (ns + 1) & 0x7f; // FIXME: hack!
456 };
457
458 break;
459 case LAPD_CMD_UI:
460 break;
461 case LAPD_CMD_SABME:
462 sap->vs = 0;
463 sap->vr = 0;
464 sap->va = 0;
465
466 // ua
467 resp[l++] = data[0];
468 resp[l++] = (tei << 1) | 1;
469 resp[l++] = 0x73;
470 li->transmit_cb(resp, l, li->cbdata);
471 if (teip->state != LAPD_TEI_ACTIVE) {
472 if (teip->state == LAPD_TEI_ASSIGNED) {
473 lapd_tei_set_state(teip,
474 LAPD_TEI_ACTIVE);
475 //printf("ASSIGNED and ACTIVE\n");
476 } else {
477#if 0
478 DEBUGP(DMI, "rr in strange state, send rej\n");
479
480 // rej
481 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
482 resp[l++] = (tei << 1) | 1;
483 resp[l++] = 0x09; //rej
484 resp[l++] = ((sap->vr + 1) << 1) | 0;
485 li->transmit_cb(resp, l, li->cbdata);
486 pf = 0; // dont reply
487#endif
488 };
489 };
490
491 *prim = LAPD_MPH_ACTIVATE_IND;
492 break;
493 case LAPD_CMD_UA:
494 sap->vs = 0;
495 sap->vr = 0;
496 sap->va = 0;
497 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
498 lapd_sap_set_state(teip, sapi, SAP_STATE_ACTIVE);
499 *prim = LAPD_MPH_ACTIVATE_IND;
500 break;
501 case LAPD_CMD_RR:
502 sap->va = (nr & 0x7f);
503#if 0
504 if (teip->state != LAPD_TEI_ACTIVE) {
505 if (teip->state == LAPD_TEI_ASSIGNED) {
506 lapd_tei_set_state(teip, LAPD_TEI_ACTIVE);
507 *prim = LAPD_MPH_ACTIVATE_IND;
508 //printf("ASSIGNED and ACTIVE\n");
509 } else {
510#if 0
511 DEBUGP(DMI, "rr in strange " "state, send rej\n");
512
513 // rej
514 resp[l++] = (sap-> sapi << 2) | (li->network_side ? 0 : 2);
515 resp[l++] = (tei << 1) | 1;
516 resp[l++] = 0x09; //rej
517 resp[l++] =
518 ((sap->vr + 1) << 1) | 0;
519 li->transmit_cb(resp, l, li->cbdata);
520 pf = 0; // dont reply
521#endif
522 };
523 };
524#endif
525 if (pf) {
526 // interrogating us, send rr
527 resp[l++] = data[0];
528 resp[l++] = (tei << 1) | 1;
529 resp[l++] = 0x01; // rr
530 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
531
532 li->transmit_cb(resp, l, li->cbdata);
533
534 };
535 break;
536 case LAPD_CMD_FRMR:
537 // frame reject
538#if 0
539 if (teip->state == LAPD_TEI_ACTIVE)
540 *prim = LAPD_MPH_DEACTIVATE_IND;
541 lapd_tei_set_state(teip, LAPD_TEI_ASSIGNED);
542#endif
543 LOGP(DMI, LOGL_NOTICE, "frame reject, ignoring\n");
544 break;
545 case LAPD_CMD_DISC:
546 // disconnect
547 resp[l++] = data[0];
548 resp[l++] = (tei << 1) | 1;
549 resp[l++] = 0x73;
550 li->transmit_cb(resp, l, li->cbdata);
551 lapd_tei_set_state(teip, LAPD_TEI_NONE);
552 break;
553 default:
554 LOGP(DMI, LOGL_NOTICE, "unknown cmd for tei %d (cmd %x)\n",
555 tei, cmd);
556 break;
557 }
558
559 if (typ == LAPD_TYPE_I) {
560 /* send rr
561 * Thu Jan 22 19:17:13 2009 <4000> sangoma.c:340 read (62/25) 4: fa 33 01 0a
562 * 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
563 */
564
565 /* interrogating us, send rr */
566 DEBUGP(DMI, "Sending RR response\n");
567 resp[l++] = data[0];
568 resp[l++] = (tei << 1) | 1;
569 resp[l++] = 0x01; // rr
570 resp[l++] = (LAPD_NR(sap) << 1) | (data[3] & 1); // pf bit from req
571
572 li->transmit_cb(resp, l, li->cbdata);
573
574 if (cmd != 0) {
575 *prim = LAPD_DL_DATA_IND;
576 return contents;
577 }
578 } else if (tei != 127 && typ == LAPD_TYPE_U && cmd == LAPD_CMD_UI) {
579 *prim = LAPD_DL_UNITDATA_IND;
580 return contents;
581 }
582
583 return NULL;
584};
585
586/* low-level function to send a single SABM message */
587static int lapd_send_sabm(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
588{
589 struct msgb *msg = msgb_alloc_headroom(1024, 128, "LAPD SABM");
590 if (!msg)
591 return -ENOMEM;
592
593 DEBUGP(DMI, "Sending SABM for TEI=%u, SAPI=%u\n", tei, sapi);
594
595 msgb_put_u8(msg, (sapi << 2) | (li->network_side ? 2 : 0));
596 msgb_put_u8(msg, (tei << 1) | 1);
597 msgb_put_u8(msg, 0x7F);
598
599 li->transmit_cb(msg->data, msg->len, li->cbdata);
600
601 msgb_free(msg);
602
603 return 0;
604}
605
606/* timer call-back function for SABM re-transmission */
607static void sabme_timer_cb(void *_sap)
608{
609 struct lapd_sap *sap = _sap;
610
611 lapd_send_sabm(sap->tei->li, sap->tei->tei, sap->sapi);
612
613 if (sap->state == SAP_STATE_SABM_RETRANS)
614 osmo_timer_schedule(&sap->sabme_timer, SABM_INTERVAL);
615}
616
617/* Start a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
618int lapd_sap_start(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
619{
620 struct lapd_sap *sap;
621 struct lapd_tei *teip;
622
623 teip = teip_from_tei(li, tei);
624 if (!teip)
625 teip = lapd_tei_alloc(li, tei);
626
627 sap = lapd_sap_find(teip, sapi);
628 if (sap)
629 return -EEXIST;
630
631 sap = lapd_sap_alloc(teip, sapi);
632
633 lapd_sap_set_state(teip, sapi, SAP_STATE_SABM_RETRANS);
634
635 return 0;
636}
637
638/* Stop a (user-side) SAP for the specified TEI/SAPI on the LAPD instance */
639int lapd_sap_stop(struct lapd_instance *li, uint8_t tei, uint8_t sapi)
640{
641 struct lapd_tei *teip;
642 struct lapd_sap *sap;
643
644 teip = teip_from_tei(li, tei);
645 if (!teip)
646 return -ENODEV;
647
648 sap = lapd_sap_find(teip, sapi);
649 if (!sap)
650 return -ENODEV;
651
652 lapd_sap_set_state(teip, sapi, SAP_STATE_INACTIVE);
653
654 llist_del(&sap->list);
655 talloc_free(sap);
656
657 return 0;
658}
659
660/* Transmit Data (I-Frame) on the given LAPD Instance / TEI / SAPI */
661void lapd_transmit(struct lapd_instance *li, uint8_t tei, uint8_t sapi,
662 uint8_t *data, unsigned int len)
663{
664 struct lapd_tei *teip = teip_from_tei(li, tei);
665 struct lapd_sap *sap;
666
667 if (!teip) {
668 LOGP(DMI, LOGL_ERROR, "Cannot transmit on non-existing "
669 "TEI %u\n", tei);
670 return;
671 }
672
673 sap = lapd_sap_find(teip, sapi);
674 if (!sap) {
675 LOGP(DMI, LOGL_INFO, "Tx on unknown SAPI=%u in TEI=%u, "
676 "allocating\n", sapi, tei);
677 sap = lapd_sap_alloc(teip, sapi);
678 }
679
680 /* prepend stuff */
681 uint8_t buf[10000];
682 memset(buf, 0, sizeof(buf));
683 memmove(buf + 4, data, len);
684 len += 4;
685
686 buf[0] = (sapi << 2) | (li->network_side ? 2 : 0);
687 buf[1] = (tei << 1) | 1;
688 buf[2] = (LAPD_NS(sap) << 1);
689 buf[3] = (LAPD_NR(sap) << 1) | 0;
690
691 sap->vs = (sap->vs + 1) & 0x7f;
692
693 li->transmit_cb(buf, len, li->cbdata);
694};
695
696/* Allocate a new LAPD instance */
697struct lapd_instance *lapd_instance_alloc(int network_side,
698 void (*tx_cb)(uint8_t *data, int len,
699 void *cbdata), void *cbdata)
700{
701 struct lapd_instance *li;
702
703 li = talloc_zero(NULL, struct lapd_instance);
704 if (!li)
705 return NULL;
706
707 li->transmit_cb = tx_cb;
708 li->cbdata = cbdata;
709 li->network_side = network_side;
710 INIT_LLIST_HEAD(&li->tei_list);
711
712 return li;
713}