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