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