blob: 9dbdfcf9c5f5051326d59a1afb5255a9f820c5d6 [file] [log] [blame]
rootaf48bed2011-09-26 11:23:06 +02001/* LAPD core implementation */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/*! \addtogroup lapd
25 * @{
26 */
27
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010028/*! \file lapd_core.c */
rootaf48bed2011-09-26 11:23:06 +020029
30/*!
31 * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
32 *
33 * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
34 * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
35 * received while there is an incomplete rcv_buffer, it is appended to it.
36 *
37 * TX data is stored in the send_queue first. When transmitting a frame,
38 * the first message in the send_queue is moved to the send_buffer. There it
39 * resides until all fragments are acknowledged. Fragments to be sent by I
40 * frames are stored in the tx_hist buffer for resend, if required. Also the
41 * current fragment is copied into the tx_queue. There it resides until it is
42 * forwarded to layer 1.
43 *
44 * In case we have SAPI 0, we only have a window size of 1, so the unack-
45 * nowledged message resides always in the send_buffer. In case of a suspend,
46 * it can be written back to the first position of the send_queue.
47 *
48 * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
49 * asynchronous transfer between layer 1 and layer 2 (serial link), we must
50 * send a frame before layer 1 reaches the right timeslot to send it. So we
51 * move the tx_queue to layer 1 when there is not already a pending frame, and
52 * wait until acknowledge after the frame has been sent. If we receive an
53 * acknowledge, we can send the next frame from the buffer, if any.
54 *
55 * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
56 * will trigger next I frame, if possible.
57 *
58 * T203 is optional. It will be stated when entering MF EST state. It will also
59 * be started when I or S frame is received in that state . It will be
60 * restarted in the lapd_acknowledge() function, in case outstanding frames
61 * will not trigger T200. It will be stoped, when T200 is started in MF EST
62 * state. It will also be stoped when leaving MF EST state.
63 *
64 */
65
66/* Enable this to test content resolution on network side:
67 * - The first SABM is received, UA is dropped.
68 * - The phone repeats SABM, but it's content is wrong, so it is ignored
69 * - The phone repeats SABM again, content is right, so UA is sent.
70 */
71//#define TEST_CONTENT_RESOLUTION_NETWORK
72
73#include <stdio.h>
74#include <stdint.h>
75#include <string.h>
76#include <errno.h>
77#include <arpa/inet.h>
78
79#include <osmocom/core/logging.h>
80#include <osmocom/core/timer.h>
81#include <osmocom/core/msgb.h>
82#include <osmocom/core/utils.h>
83#include <osmocom/core/talloc.h>
84#include <osmocom/gsm/lapd_core.h>
85
86/* TS 04.06 Table 4 / Section 3.8.1 */
87#define LAPD_U_SABM 0x7
88#define LAPD_U_SABME 0xf
89#define LAPD_U_DM 0x3
90#define LAPD_U_UI 0x0
91#define LAPD_U_DISC 0x8
92#define LAPD_U_UA 0xC
93#define LAPD_U_FRMR 0x11
94
95#define LAPD_S_RR 0x0
96#define LAPD_S_RNR 0x1
97#define LAPD_S_REJ 0x2
98
99#define CR_USER2NET_CMD 0
100#define CR_USER2NET_RESP 1
101#define CR_NET2USER_CMD 1
102#define CR_NET2USER_RESP 0
103
104#define LAPD_HEADROOM 56
105
106#define SBIT(a) (1 << a)
107#define ALL_STATES 0xffffffff
108
Andreas Eversberg742fc792011-09-27 09:40:25 +0200109static void lapd_t200_cb(void *data);
110static void lapd_t203_cb(void *data);
111static int lapd_send_i(struct lapd_msg_ctx *lctx, int line);
112static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
113
rootaf48bed2011-09-26 11:23:06 +0200114/* UTILITY FUNCTIONS */
115
116struct msgb *lapd_msgb_alloc(int length, const char *name)
117{
118 /* adding space for padding, FIXME: add as an option */
119 if (length < 21)
120 length = 21;
121 return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name);
122}
123
124static inline uint8_t do_mod(uint8_t x, uint8_t m)
125{
126 return x & (m - 1);
127}
128
129static inline uint8_t inc_mod(uint8_t x, uint8_t m)
130{
131 return (x + 1) & (m - 1);
132}
133
134static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m)
135{
136 return (x + y) & (m - 1);
137}
138
139static inline uint8_t sub_mod(uint8_t x, uint8_t y, uint8_t m)
140{
141 return (x - y) & (m - 1); /* handle negative results correctly */
142}
143
144static void lapd_dl_flush_send(struct lapd_datalink *dl)
145{
146 struct msgb *msg;
147
148 /* Flush send-queue */
149 while ((msg = msgb_dequeue(&dl->send_queue)))
150 msgb_free(msg);
151
152 /* Clear send-buffer */
153 if (dl->send_buffer) {
154 msgb_free(dl->send_buffer);
155 dl->send_buffer = NULL;
156 }
157}
158
159static void lapd_dl_flush_hist(struct lapd_datalink *dl)
160{
161 unsigned int i;
162
163 for (i = 0; i < dl->range_hist; i++) {
164 if (dl->tx_hist[i].msg) {
165 msgb_free(dl->tx_hist[i].msg);
166 dl->tx_hist[i].msg = NULL;
167 }
168 }
169}
170
171static void lapd_dl_flush_tx(struct lapd_datalink *dl)
172{
173 struct msgb *msg;
174
175 while ((msg = msgb_dequeue(&dl->tx_queue)))
176 msgb_free(msg);
177 lapd_dl_flush_hist(dl);
178}
179
180/* Figure B.2/Q.921 */
181const char *lapd_state_names[] = {
182 "LAPD_STATE_NULL",
183 "LAPD_STATE_TEI_UNASS",
184 "LAPD_STATE_ASS_TEI_WAIT",
185 "LAPD_STATE_EST_TEI_WAIT",
186 "LAPD_STATE_IDLE",
187 "LAPD_STATE_SABM_SENT",
188 "LAPD_STATE_DISC_SENT",
189 "LAPD_STATE_MF_EST",
190 "LAPD_STATE_TIMER_RECOV",
191
192};
193
Andreas Eversberg742fc792011-09-27 09:40:25 +0200194static void lapd_start_t200(struct lapd_datalink *dl)
195{
196 if (osmo_timer_pending(&dl->t200))
197 return;
198 LOGP(DLLAPD, LOGL_INFO, "start T200\n");
199 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
200}
201
202static void lapd_start_t203(struct lapd_datalink *dl)
203{
204 if (osmo_timer_pending(&dl->t203))
205 return;
206 LOGP(DLLAPD, LOGL_INFO, "start T203\n");
207 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
208}
209
210static void lapd_stop_t200(struct lapd_datalink *dl)
211{
212 if (!osmo_timer_pending(&dl->t200))
213 return;
214 LOGP(DLLAPD, LOGL_INFO, "stop T200\n");
215 osmo_timer_del(&dl->t200);
216}
217
218static void lapd_stop_t203(struct lapd_datalink *dl)
219{
220 if (!osmo_timer_pending(&dl->t203))
221 return;
222 LOGP(DLLAPD, LOGL_INFO, "stop T203\n");
223 osmo_timer_del(&dl->t203);
224}
225
rootaf48bed2011-09-26 11:23:06 +0200226static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
227{
228 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s\n",
229 lapd_state_names[dl->state], lapd_state_names[state]);
230
231 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
232 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200233 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200234 /* remove content res. (network side) on leaving MF EST state */
235 if (dl->cont_res) {
236 msgb_free(dl->cont_res);
237 dl->cont_res = NULL;
238 }
239 }
240
241 /* start T203 on entering MF EST state, if enabled */
242 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200243 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
244 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200245
246 dl->state = state;
247}
248
rootaf48bed2011-09-26 11:23:06 +0200249static void *tall_lapd_ctx = NULL;
250
251/* init datalink instance and allocate history */
252void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
253 int maxf)
254{
255 int m;
256
257 memset(dl, 0, sizeof(*dl));
258 INIT_LLIST_HEAD(&dl->send_queue);
259 INIT_LLIST_HEAD(&dl->tx_queue);
260 dl->reestablish = 1;
261 dl->n200_est_rel = 3;
262 dl->n200 = 3;
263 dl->t200_sec = 1;
264 dl->t200_usec = 0;
265 dl->t200.data = dl;
266 dl->t200.cb = &lapd_t200_cb;
267 dl->t203_sec = 10;
268 dl->t203_usec = 0;
269 dl->t203.data = dl;
270 dl->t203.cb = &lapd_t203_cb;
271 dl->maxf = maxf;
272 if (k > v_range - 1)
273 k = v_range - 1;
274 dl->k = k;
275 dl->v_range = v_range;
276
277 /* Calculate modulo for history array:
278 * - The history range must be at least k+1.
279 * - The history range must be 2^x, where x is as low as possible.
280 */
281 k++;
282 for (m = 0x80; m; m >>= 1) {
283 if ((m & k)) {
284 if (k > m)
285 m <<= 1;
286 dl->range_hist = m;
287 break;
288 }
289 }
290
291 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
292 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
293
294 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
295
296 if (!tall_lapd_ctx)
297 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
298 dl->tx_hist = (struct lapd_history *) talloc_zero_array(tall_lapd_ctx,
299 struct log_info, dl->range_hist);
300}
301
302/* reset to IDLE state */
303void lapd_dl_reset(struct lapd_datalink *dl)
304{
305 if (dl->state == LAPD_STATE_IDLE)
306 return;
307 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
308 /* enter idle state (and remove eventual cont_res) */
309 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
310 /* flush buffer */
311 lapd_dl_flush_tx(dl);
312 lapd_dl_flush_send(dl);
313 /* Discard partly received L3 message */
314 if (dl->rcv_buffer) {
315 msgb_free(dl->rcv_buffer);
316 dl->rcv_buffer = NULL;
317 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200318 /* stop Timers */
319 lapd_stop_t200(dl);
320 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200321}
322
323/* reset and de-allocate history buffer */
324void lapd_dl_exit(struct lapd_datalink *dl)
325{
326 /* free all ressources except history buffer */
327 lapd_dl_reset(dl);
328 /* free history buffer list */
329 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200330 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200331}
332
333/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
334int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
335{
336 switch (mode) {
337 case LAPD_MODE_USER:
338 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
339 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
340 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
341 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
342 break;
343 case LAPD_MODE_NETWORK:
344 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
345 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
346 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
347 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
348 break;
349 default:
350 return -EINVAL;
351 }
352 dl->mode = mode;
353
354 return 0;
355}
356
357/* send DL message with optional msgb */
358static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
359 struct msgb *msg)
360{
361 struct lapd_datalink *dl = lctx->dl;
362 struct osmo_dlsap_prim dp;
363
364 osmo_prim_init(&dp.oph, 0, prim, op, msg);
365 return dl->send_dlsap(&dp, lctx);
366}
367
368/* send simple DL message */
369static inline int send_dl_simple(uint8_t prim, uint8_t op,
370 struct lapd_msg_ctx *lctx)
371{
372 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
373
374 return send_dl_l3(prim, op, lctx, msg);
375}
376
377/* send MDL-ERROR INDICATION */
378static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
379{
380 struct lapd_datalink *dl = lctx->dl;
381 struct osmo_dlsap_prim dp;
382
383 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %d\n",
384 cause);
385 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
386 dp.u.error_ind.cause = cause;
387 return dl->send_dlsap(&dp, lctx);
388}
389
390/* send UA response */
391static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
392{
393 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
394 struct lapd_msg_ctx nctx;
395 struct lapd_datalink *dl = lctx->dl;
396
397 memcpy(&nctx, lctx, sizeof(nctx));
398 msg->l3h = msgb_put(msg, len);
399 if (len)
400 memcpy(msg->l3h, data, len);
401 /* keep nctx.ldp */
402 /* keep nctx.sapi */
403 /* keep nctx.tei */
404 nctx.cr = dl->cr.loc2rem.resp;
405 nctx.format = LAPD_FORM_U;
406 nctx.s_u = LAPD_U_UA;
407 /* keep nctx.p_f */
408 nctx.length = len;
409 nctx.more = 0;
410
411 return dl->send_ph_data_req(&nctx, msg);
412}
413
414/* send DM response */
415static int lapd_send_dm(struct lapd_msg_ctx *lctx)
416{
417 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
418 struct lapd_msg_ctx nctx;
419 struct lapd_datalink *dl = lctx->dl;
420
421 memcpy(&nctx, lctx, sizeof(nctx));
422 /* keep nctx.ldp */
423 /* keep nctx.sapi */
424 /* keep nctx.tei */
425 nctx.cr = dl->cr.loc2rem.resp;
426 nctx.format = LAPD_FORM_U;
427 nctx.s_u = LAPD_U_DM;
428 /* keep nctx.p_f */
429 nctx.length = 0;
430 nctx.more = 0;
431
432 return dl->send_ph_data_req(&nctx, msg);
433}
434
435/* send RR response / command */
436static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
437{
438 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
439 struct lapd_msg_ctx nctx;
440 struct lapd_datalink *dl = lctx->dl;
441
442 memcpy(&nctx, lctx, sizeof(nctx));
443 /* keep nctx.ldp */
444 /* keep nctx.sapi */
445 /* keep nctx.tei */
446 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
447 nctx.format = LAPD_FORM_S;
448 nctx.s_u = LAPD_S_RR;
449 nctx.p_f = f_bit;
450 nctx.n_recv = dl->v_recv;
451 nctx.length = 0;
452 nctx.more = 0;
453
454 return dl->send_ph_data_req(&nctx, msg);
455}
456
457/* send RNR response / command */
458static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
459{
460 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
461 struct lapd_msg_ctx nctx;
462 struct lapd_datalink *dl = lctx->dl;
463
464 memcpy(&nctx, lctx, sizeof(nctx));
465 /* keep nctx.ldp */
466 /* keep nctx.sapi */
467 /* keep nctx.tei */
468 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
469 nctx.format = LAPD_FORM_S;
470 nctx.s_u = LAPD_S_RNR;
471 nctx.p_f = f_bit;
472 nctx.n_recv = dl->v_recv;
473 nctx.length = 0;
474 nctx.more = 0;
475
476 return dl->send_ph_data_req(&nctx, msg);
477}
478
479/* send REJ response */
480static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
481{
482 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
483 struct lapd_msg_ctx nctx;
484 struct lapd_datalink *dl = lctx->dl;
485
486 memcpy(&nctx, lctx, sizeof(nctx));
487 /* keep nctx.ldp */
488 /* keep nctx.sapi */
489 /* keep nctx.tei */
490 nctx.cr = dl->cr.loc2rem.resp;
491 nctx.format = LAPD_FORM_S;
492 nctx.s_u = LAPD_S_REJ;
493 nctx.p_f = f_bit;
494 nctx.n_recv = dl->v_recv;
495 nctx.length = 0;
496 nctx.more = 0;
497
498 return dl->send_ph_data_req(&nctx, msg);
499}
500
501/* resend SABM or DISC message */
502static int lapd_send_resend(struct lapd_datalink *dl)
503{
504 struct msgb *msg;
505 uint8_t h = do_mod(dl->v_send, dl->range_hist);
506 int length = dl->tx_hist[h].msg->len;
507 struct lapd_msg_ctx nctx;
508
509 /* assemble message */
510 memcpy(&nctx, &dl->lctx, sizeof(nctx));
511 /* keep nctx.ldp */
512 /* keep nctx.sapi */
513 /* keep nctx.tei */
514 nctx.cr = dl->cr.loc2rem.cmd;
515 nctx.format = LAPD_FORM_U;
516 if (dl->state == LAPD_STATE_SABM_SENT)
517 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
518 else
519 nctx.s_u = LAPD_U_DISC;
520 nctx.p_f = 1;
521 nctx.length = length;
522 nctx.more = 0;
523
524 /* Resend SABM/DISC from tx_hist */
525 msg = lapd_msgb_alloc(length, "LAPD resend");
526 msg->l3h = msgb_put(msg, length);
527 if (length)
528 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
529
530 return dl->send_ph_data_req(&nctx, msg);
531}
532
533/* reestablish link */
534static int lapd_reestablish(struct lapd_datalink *dl)
535{
536 struct osmo_dlsap_prim dp;
537 struct msgb *msg;
538
539 msg = lapd_msgb_alloc(0, "DUMMY");
540 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
541
542 return lapd_est_req(&dp, &dl->lctx);
543}
544
545/* Timer callback on T200 expiry */
546static void lapd_t200_cb(void *data)
547{
548 struct lapd_datalink *dl = data;
549
Andreas Eversberg742fc792011-09-27 09:40:25 +0200550 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200551 (int) dl->state);
552
553 switch (dl->state) {
554 case LAPD_STATE_SABM_SENT:
555 /* 5.4.1.3 */
556 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
557 /* send RELEASE INDICATION to L3 */
558 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
559 &dl->lctx);
560 /* send MDL ERROR INIDCATION to L3 */
561 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
562 /* flush tx and send buffers */
563 lapd_dl_flush_tx(dl);
564 lapd_dl_flush_send(dl);
565 /* go back to idle state */
566 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
567 /* NOTE: we must not change any other states or buffers
568 * and queues, since we may reconnect after handover
569 * failure. the buffered messages is replaced there */
570 break;
571 }
572 /* retransmit SABM command */
573 lapd_send_resend(dl);
574 /* increment re-transmission counter */
575 dl->retrans_ctr++;
576 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200577 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200578 break;
579 case LAPD_STATE_DISC_SENT:
580 /* 5.4.4.3 */
581 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
582 /* send RELEASE INDICATION to L3 */
583 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
584 /* send MDL ERROR INIDCATION to L3 */
585 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
586 /* flush tx and send buffers */
587 lapd_dl_flush_tx(dl);
588 lapd_dl_flush_send(dl);
589 /* go back to idle state */
590 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
591 /* NOTE: we must not change any other states or buffers
592 * and queues, since we may reconnect after handover
593 * failure. the buffered messages is replaced there */
594 break;
595 }
596 /* retransmit DISC command */
597 lapd_send_resend(dl);
598 /* increment re-transmission counter */
599 dl->retrans_ctr++;
600 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200601 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200602 break;
603 case LAPD_STATE_MF_EST:
604 /* 5.5.7 */
605 dl->retrans_ctr = 0;
606 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
607 /* fall through */
608 case LAPD_STATE_TIMER_RECOV:
609 dl->retrans_ctr++;
610 if (dl->retrans_ctr < dl->n200) {
611 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
612 uint8_t h = do_mod(vs, dl->range_hist);
613 /* retransmit I frame (V_s-1) with P=1, if any */
614 if (dl->tx_hist[h].msg) {
615 struct msgb *msg;
616 int length = dl->tx_hist[h].msg->len;
617 struct lapd_msg_ctx nctx;
618
619 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
620 " V(S)=%d\n", vs);
621 /* Create I frame (segment) from tx_hist */
622 memcpy(&nctx, &dl->lctx, sizeof(nctx));
623 /* keep nctx.ldp */
624 /* keep nctx.sapi */
625 /* keep nctx.tei */
626 nctx.cr = dl->cr.loc2rem.cmd;
627 nctx.format = LAPD_FORM_I;
628 nctx.p_f = 1;
629 nctx.n_send = vs;
630 nctx.n_recv = dl->v_recv;
631 nctx.length = length;
632 nctx.more = dl->tx_hist[h].more;
633 msg = lapd_msgb_alloc(length, "LAPD I resend");
634 msg->l3h = msgb_put(msg, length);
635 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
636 length);
637 dl->send_ph_data_req(&nctx, msg);
638 } else {
639 /* OR send appropriate supervision frame with P=1 */
640 if (!dl->own_busy && !dl->seq_err_cond) {
641 lapd_send_rr(&dl->lctx, 1, 1);
642 /* NOTE: In case of sequence error
643 * condition, the REJ frame has been
644 * transmitted when entering the
645 * condition, so it has not be done
646 * here
647 */
648 } else if (dl->own_busy) {
649 lapd_send_rnr(&dl->lctx, 1, 1);
650 } else {
651 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
652 "pls. fix\n");
653 }
654 }
655 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200656 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200657 } else {
658 /* send MDL ERROR INIDCATION to L3 */
659 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
660 /* reestablish */
661 if (!dl->reestablish)
662 break;
663 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
664 "reestablishment.\n");
665 lapd_reestablish(dl);
666 }
667 break;
668 default:
669 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
670 "dl->state %d\n", (int) dl->state);
671 }
672}
673
674/* Timer callback on T203 expiry */
675static void lapd_t203_cb(void *data)
676{
677 struct lapd_datalink *dl = data;
678
Andreas Eversberg742fc792011-09-27 09:40:25 +0200679 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200680 (int) dl->state);
681
682 if (dl->state != LAPD_STATE_MF_EST) {
683 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
684 "please fix!\n");
685 return;
686 }
687
688 /* set retransmission counter to 0 */
689 dl->retrans_ctr = 0;
690 /* enter timer recovery state */
691 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
692 /* transmit a supervisory command with P bit set to 1 as follows: */
693 if (!dl->own_busy) {
694 LOGP(DLLAPD, LOGL_INFO, "transmit an RR poll command\n");
695 /* Send RR with P=1 */
696 lapd_send_rr(&dl->lctx, 1, 1);
697 } else {
698 LOGP(DLLAPD, LOGL_INFO, "transmit an RNR poll command\n");
699 /* Send RNR with P=1 */
700 lapd_send_rnr(&dl->lctx, 1, 1);
701 }
702 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200703 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200704}
705
706/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
707static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
708{
709 struct lapd_datalink *dl = lctx->dl;
710 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100711 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200712 int i, h;
713
714 /* supervisory frame ? */
715 if (lctx->format == LAPD_FORM_S)
716 s = 1;
717 /* REJ frame ? */
718 if (s && lctx->s_u == LAPD_S_REJ)
719 rej = 1;
720
721 /* Flush all transmit buffers of acknowledged frames */
722 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
723 h = do_mod(i, dl->range_hist);
724 if (dl->tx_hist[h].msg) {
725 msgb_free(dl->tx_hist[h].msg);
726 dl->tx_hist[h].msg = NULL;
727 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
728 }
729 }
730
731 if (dl->state != LAPD_STATE_TIMER_RECOV) {
732 /* When not in the timer recovery condition, the data
733 * link layer entity shall reset the timer T200 on
734 * receipt of a valid I frame with N(R) higher than V(A),
735 * or an REJ with an N(R) equal to V(A). */
736 if ((!rej && nr != dl->v_ack)
737 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200738 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200739 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200740 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
741 }
742 /* 5.7.4: N(R) sequence error
743 * N(R) is called valid, if and only if
744 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
745 */
746 if (sub_mod(nr, dl->v_ack, dl->v_range)
747 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
748 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error\n");
749 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
750 }
751 }
752
753 /* V(A) shall be set to the value of N(R) */
754 dl->v_ack = nr;
755
Andreas Eversberg742fc792011-09-27 09:40:25 +0200756 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200757 * and if there are outstanding I frames, restart T200 */
758 if (t200_reset && !rej) {
759 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
760 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
761 "frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200762 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200763 }
764 }
765
766 /* This also does a restart, when I or S frame is received */
767
768 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200769 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200770 /* Start T203, if T200 is not running in MF EST state, if enabled */
771 if (!osmo_timer_pending(&dl->t200)
772 && (dl->t203_sec || dl->t203_usec)
773 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200774 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200775 }
776}
777
778/* L1 -> L2 */
779
780/* Receive a LAPD U (Unnumbered) message from L1 */
781static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
782{
783 struct lapd_datalink *dl = lctx->dl;
784 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100785 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200786 uint8_t prim, op;
787
788 switch (lctx->s_u) {
789 case LAPD_U_SABM:
790 case LAPD_U_SABME:
791 prim = PRIM_DL_EST;
792 op = PRIM_OP_INDICATION;
793
794 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s\n",
795 lapd_state_names[dl->state]);
796 /* 5.7.1 */
797 dl->seq_err_cond = 0;
798 /* G.2.2 Wrong value of the C/R bit */
799 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100800 LOGP(DLLAPD, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200801 msgb_free(msg);
802 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
803 return -EINVAL;
804 }
805
806 /* G.4.5 If SABM is received with L>N201 or with M bit
807 * set, AN MDL-ERROR-INDICATION is sent to MM.
808 */
809 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100810 LOGP(DLLAPD, LOGL_ERROR, "SABM too large error\n");
rootaf48bed2011-09-26 11:23:06 +0200811 msgb_free(msg);
812 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
813 return -EIO;
814 }
815
816 switch (dl->state) {
817 case LAPD_STATE_IDLE:
818 break;
819 case LAPD_STATE_MF_EST:
820 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
821 "frame established state\n");
822 /* If link is lost on the remote side, we start over
823 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100824 /* Additionally, continue in case of content resoltion
825 * (GSM network). This happens, if the mobile has not
826 * yet received UA or another mobile (collision) tries
827 * to establish connection. The mobile must receive
828 * UA again. */
829 if (!dl->cont_res && dl->v_send != dl->v_recv) {
rootaf48bed2011-09-26 11:23:06 +0200830 LOGP(DLLAPD, LOGL_INFO, "Remote reestablish\n");
831 mdl_error(MDL_CAUSE_SABM_MF, lctx);
832 break;
833 }
834 /* Ignore SABM if content differs from first SABM. */
835 if (dl->mode == LAPD_MODE_NETWORK && length
836 && dl->cont_res) {
837#ifdef TEST_CONTENT_RESOLUTION_NETWORK
838 dl->cont_res->data[0] ^= 0x01;
839#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100840 if (memcmp(dl->cont_res->data, msg->data,
841 length)) {
rootaf48bed2011-09-26 11:23:06 +0200842 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
843 "with diffrent content - "
844 "ignoring!\n");
845 msgb_free(msg);
846 return 0;
847 }
848 }
849 /* send UA again */
850 lapd_send_ua(lctx, length, msg->l3h);
851 msgb_free(msg);
852 return 0;
853 case LAPD_STATE_DISC_SENT:
854 /* 5.4.6.2 send DM with F=P */
855 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200856 /* stop Timer T200 */
857 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200858 msgb_free(msg);
859 return send_dl_simple(prim, op, lctx);
860 default:
861 /* collision: Send UA, but still wait for rx UA, then
862 * change to MF_EST state.
863 */
864 /* check for contention resoultion */
865 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
866 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
867 "during contention resolution\n");
868 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
869 }
870 lapd_send_ua(lctx, length, msg->l3h);
871 msgb_free(msg);
872 return 0;
873 }
874 /* save message context for further use */
875 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
876#ifndef TEST_CONTENT_RESOLUTION_NETWORK
877 /* send UA response */
878 lapd_send_ua(lctx, length, msg->l3h);
879#endif
880 /* set Vs, Vr and Va to 0 */
881 dl->v_send = dl->v_recv = dl->v_ack = 0;
882 /* clear tx_hist */
883 lapd_dl_flush_hist(dl);
884 /* enter multiple-frame-established state */
885 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
886 /* store content resolution data on network side
887 * Note: cont_res will be removed when changing state again,
888 * so it must be allocated AFTER lapd_dl_newstate(). */
889 if (dl->mode == LAPD_MODE_NETWORK && length) {
890 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
891 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
892 length);
893 LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n");
894 }
895 /* send notification to L3 */
896 if (length == 0) {
897 /* 5.4.1.2 Normal establishment procedures */
898 rc = send_dl_simple(prim, op, lctx);
899 msgb_free(msg);
900 } else {
901 /* 5.4.1.4 Contention resolution establishment */
902 rc = send_dl_l3(prim, op, lctx, msg);
903 }
904 break;
905 case LAPD_U_DM:
906 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
907 lapd_state_names[dl->state]);
908 /* G.2.2 Wrong value of the C/R bit */
909 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100910 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200911 msgb_free(msg);
912 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
913 return -EINVAL;
914 }
915 if (!lctx->p_f) {
916 /* 5.4.1.2 DM responses with the F bit set to "0"
917 * shall be ignored.
918 */
919 msgb_free(msg);
920 return 0;
921 }
922 switch (dl->state) {
923 case LAPD_STATE_SABM_SENT:
924 break;
925 case LAPD_STATE_MF_EST:
926 if (lctx->p_f) {
927 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
928 "response\n");
929 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
930 } else {
931 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
932 "response, multiple frame established "
933 "state\n");
934 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
935 /* reestablish */
936 if (!dl->reestablish) {
937 msgb_free(msg);
938 return 0;
939 }
940 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
941 "reestablishment.\n");
942 lapd_reestablish(dl);
943 }
944 msgb_free(msg);
945 return 0;
946 case LAPD_STATE_TIMER_RECOV:
947 /* FP = 0 (DM is normal in case PF = 1) */
948 if (!lctx->p_f) {
949 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
950 "response, multiple frame established "
951 "state\n");
952 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
953 msgb_free(msg);
954 /* reestablish */
955 if (!dl->reestablish)
956 return 0;
957 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
958 "reestablishment.\n");
959 return lapd_reestablish(dl);
960 }
961 break;
962 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200963 /* stop Timer T200 */
964 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200965 /* go to idle state */
966 lapd_dl_flush_tx(dl);
967 lapd_dl_flush_send(dl);
968 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
969 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
970 msgb_free(msg);
971 return 0;
972 case LAPD_STATE_IDLE:
973 /* 5.4.5 all other frame types shall be discarded */
974 default:
975 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
976 "(discarding)\n");
977 msgb_free(msg);
978 return 0;
979 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200980 /* stop timer T200 */
981 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200982 /* go to idle state */
983 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
984 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
985 msgb_free(msg);
986 break;
987 case LAPD_U_UI:
988 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
989 /* G.2.2 Wrong value of the C/R bit */
990 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100991 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +0200992 "error\n");
993 msgb_free(msg);
994 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
995 return -EINVAL;
996 }
997
998 /* G.4.5 If UI is received with L>N201 or with M bit
999 * set, AN MDL-ERROR-INDICATION is sent to MM.
1000 */
1001 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001002 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +02001003 "(%d > N201(%d) or M=%d)\n", length,
1004 lctx->n201, lctx->more);
1005 msgb_free(msg);
1006 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1007 return -EIO;
1008 }
1009
1010 /* do some length checks */
1011 if (length == 0) {
1012 /* 5.3.3 UI frames received with the length indicator
1013 * set to "0" shall be ignored
1014 */
1015 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1016 msgb_free(msg);
1017 return 0;
1018 }
1019 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1020 msg);
1021 break;
1022 case LAPD_U_DISC:
1023 prim = PRIM_DL_REL;
1024 op = PRIM_OP_INDICATION;
1025
1026 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1027 lapd_state_names[dl->state]);
1028 /* flush tx and send buffers */
1029 lapd_dl_flush_tx(dl);
1030 lapd_dl_flush_send(dl);
1031 /* 5.7.1 */
1032 dl->seq_err_cond = 0;
1033 /* G.2.2 Wrong value of the C/R bit */
1034 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001035 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001036 msgb_free(msg);
1037 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1038 return -EINVAL;
1039 }
1040 if (length > 0 || lctx->more) {
1041 /* G.4.4 If a DISC or DM frame is received with L>0 or
1042 * with the M bit set to "1", an MDL-ERROR-INDICATION
1043 * primitive with cause "U frame with incorrect
1044 * parameters" is sent to the mobile management entity.
1045 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001046 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001047 "U frame iwth incorrect parameters ");
1048 msgb_free(msg);
1049 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1050 return -EIO;
1051 }
1052 switch (dl->state) {
1053 case LAPD_STATE_IDLE:
1054 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1055 /* send DM with F=P */
1056 msgb_free(msg);
1057 return lapd_send_dm(lctx);
1058 case LAPD_STATE_SABM_SENT:
1059 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1060 /* 5.4.6.2 send DM with F=P */
1061 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001062 /* stop Timer T200 */
1063 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001064 /* go to idle state */
1065 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1066 msgb_free(msg);
1067 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1068 lctx);
1069 case LAPD_STATE_MF_EST:
1070 case LAPD_STATE_TIMER_RECOV:
1071 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1072 break;
1073 case LAPD_STATE_DISC_SENT:
1074 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1075 prim = PRIM_DL_REL;
1076 op = PRIM_OP_CONFIRM;
1077 break;
1078 default:
1079 lapd_send_ua(lctx, length, msg->l3h);
1080 msgb_free(msg);
1081 return 0;
1082 }
1083 /* send UA response */
1084 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001085 /* stop Timer T200 */
1086 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001087 /* enter idle state, keep tx-buffer with UA response */
1088 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1089 /* send notification to L3 */
1090 rc = send_dl_simple(prim, op, lctx);
1091 msgb_free(msg);
1092 break;
1093 case LAPD_U_UA:
1094 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1095 lapd_state_names[dl->state]);
1096 /* G.2.2 Wrong value of the C/R bit */
1097 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001098 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001099 "error\n");
1100 msgb_free(msg);
1101 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1102 return -EINVAL;
1103 }
1104
1105 /* G.4.5 If UA is received with L>N201 or with M bit
1106 * set, AN MDL-ERROR-INDICATION is sent to MM.
1107 */
1108 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001109 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001110 msgb_free(msg);
1111 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1112 return -EIO;
1113 }
1114
1115 if (!lctx->p_f) {
1116 /* 5.4.1.2 A UA response with the F bit set to "0"
1117 * shall be ignored.
1118 */
1119 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1120 msgb_free(msg);
1121 return 0;
1122 }
1123 switch (dl->state) {
1124 case LAPD_STATE_SABM_SENT:
1125 break;
1126 case LAPD_STATE_MF_EST:
1127 case LAPD_STATE_TIMER_RECOV:
1128 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1129 "(discarding)\n");
1130 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1131 msgb_free(msg);
1132 return 0;
1133 case LAPD_STATE_DISC_SENT:
1134 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001135 /* stop Timer T200 */
1136 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001137 /* go to idle state */
1138 lapd_dl_flush_tx(dl);
1139 lapd_dl_flush_send(dl);
1140 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1141 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1142 msgb_free(msg);
1143 return 0;
1144 case LAPD_STATE_IDLE:
1145 /* 5.4.5 all other frame types shall be discarded */
1146 default:
1147 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1148 "(discarding)\n");
1149 msgb_free(msg);
1150 return 0;
1151 }
1152 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001153 /* stop Timer T200 */
1154 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001155 /* compare UA with SABME if contention resolution is applied */
1156 if (dl->tx_hist[0].msg->len) {
1157 if (length != (dl->tx_hist[0].msg->len)
1158 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1159 length)) {
1160 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1161 "mismatches ****\n");
1162 rc = send_dl_simple(PRIM_DL_REL,
1163 PRIM_OP_INDICATION, lctx);
1164 msgb_free(msg);
1165 /* go to idle state */
1166 lapd_dl_flush_tx(dl);
1167 lapd_dl_flush_send(dl);
1168 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1169 return 0;
1170 }
1171 }
1172 /* set Vs, Vr and Va to 0 */
1173 dl->v_send = dl->v_recv = dl->v_ack = 0;
1174 /* clear tx_hist */
1175 lapd_dl_flush_hist(dl);
1176 /* enter multiple-frame-established state */
1177 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1178 /* send outstanding frames, if any (resume / reconnect) */
1179 lapd_send_i(lctx, __LINE__);
1180 /* send notification to L3 */
1181 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1182 msgb_free(msg);
1183 break;
1184 case LAPD_U_FRMR:
1185 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1186 /* send MDL ERROR INIDCATION to L3 */
1187 mdl_error(MDL_CAUSE_FRMR, lctx);
1188 msgb_free(msg);
1189 /* reestablish */
1190 if (!dl->reestablish)
1191 break;
1192 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1193 rc = lapd_reestablish(dl);
1194 break;
1195 default:
1196 /* G.3.1 */
1197 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1198 msgb_free(msg);
1199 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1200 return -EINVAL;
1201 }
1202 return rc;
1203}
1204
1205/* Receive a LAPD S (Supervisory) message from L1 */
1206static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1207{
1208 struct lapd_datalink *dl = lctx->dl;
1209 int length = lctx->length;
1210
1211 if (length > 0 || lctx->more) {
1212 /* G.4.3 If a supervisory frame is received with L>0 or
1213 * with the M bit set to "1", an MDL-ERROR-INDICATION
1214 * primitive with cause "S frame with incorrect
1215 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001216 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001217 "S frame with incorrect parameters\n");
1218 msgb_free(msg);
1219 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1220 return -EIO;
1221 }
1222
1223 if (lctx->cr == dl->cr.rem2loc.resp
1224 && lctx->p_f
1225 && dl->state != LAPD_STATE_TIMER_RECOV) {
1226 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1227 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1228 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1229 }
1230
1231 switch (dl->state) {
1232 case LAPD_STATE_IDLE:
1233 /* if P=1, respond DM with F=1 (5.2.2) */
1234 /* 5.4.5 all other frame types shall be discarded */
1235 if (lctx->p_f)
1236 lapd_send_dm(lctx); /* F=P */
1237 /* fall though */
1238 case LAPD_STATE_SABM_SENT:
1239 case LAPD_STATE_DISC_SENT:
1240 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1241 msgb_free(msg);
1242 return 0;
1243 }
1244 switch (lctx->s_u) {
1245 case LAPD_S_RR:
1246 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1247 lapd_state_names[dl->state]);
1248 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1249 lapd_acknowledge(lctx);
1250
1251 /* 5.5.3.2 */
1252 if (lctx->cr == dl->cr.rem2loc.cmd
1253 && lctx->p_f) {
1254 if (!dl->own_busy && !dl->seq_err_cond) {
1255 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1256 "with polling bit set and we are not "
1257 "busy, so we reply with RR frame "
1258 "response\n");
1259 lapd_send_rr(lctx, 1, 0);
1260 /* NOTE: In case of sequence error condition,
1261 * the REJ frame has been transmitted when
1262 * entering the condition, so it has not be
1263 * done here
1264 */
1265 } else if (dl->own_busy) {
1266 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1267 "with polling bit set and we are busy, "
1268 "so we reply with RR frame response\n");
1269 lapd_send_rnr(lctx, 1, 0);
1270 }
1271 } else if (lctx->cr == dl->cr.rem2loc.resp
1272 && lctx->p_f
1273 && dl->state == LAPD_STATE_TIMER_RECOV) {
1274 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1275 "and we are in timer recovery state, so "
1276 "we leave that state\n");
1277 /* V(S) to the N(R) in the RR frame */
1278 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001279 /* stop Timer T200 */
1280 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001281 /* 5.5.7 Clear timer recovery condition */
1282 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1283 }
1284 /* Send message, if possible due to acknowledged data */
1285 lapd_send_i(lctx, __LINE__);
1286
1287 break;
1288 case LAPD_S_RNR:
1289 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1290 lapd_state_names[dl->state]);
1291 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1292 lapd_acknowledge(lctx);
1293
1294 /* 5.5.5 */
1295 /* Set peer receiver busy condition */
1296 dl->peer_busy = 1;
1297
1298 if (lctx->p_f) {
1299 if (lctx->cr == dl->cr.rem2loc.cmd) {
1300 if (!dl->own_busy) {
1301 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1302 "command and we are not busy, "
1303 "so we reply with RR final "
1304 "response\n");
1305 /* Send RR with F=1 */
1306 lapd_send_rr(lctx, 1, 0);
1307 } else {
1308 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1309 "command and we are busy, so "
1310 "we reply with RNR final "
1311 "response\n");
1312 /* Send RNR with F=1 */
1313 lapd_send_rnr(lctx, 1, 0);
1314 }
1315 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1316 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1317 "and we in timer recovery state, so "
1318 "we leave that state\n");
1319 /* 5.5.7 Clear timer recovery condition */
1320 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1321 /* V(S) to the N(R) in the RNR frame */
1322 dl->v_send = lctx->n_recv;
1323 }
1324 } else
1325 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1326 "received\n");
1327
1328 /* Send message, if possible due to acknowledged data */
1329 lapd_send_i(lctx, __LINE__);
1330
1331 break;
1332 case LAPD_S_REJ:
1333 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1334 lapd_state_names[dl->state]);
1335 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1336 lapd_acknowledge(lctx);
1337
1338 /* 5.5.4.1 */
1339 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1340 /* Clear an existing peer receiver busy condition */
1341 dl->peer_busy = 0;
1342 /* V(S) and V(A) to the N(R) in the REJ frame */
1343 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001344 /* stop Timer T200 */
1345 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001346 /* 5.5.3.2 */
1347 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1348 if (!dl->own_busy && !dl->seq_err_cond) {
1349 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1350 "command not in timer recovery "
1351 "state and not in own busy "
1352 "condition received, so we "
1353 "respond with RR final "
1354 "response\n");
1355 lapd_send_rr(lctx, 1, 0);
1356 /* NOTE: In case of sequence error
1357 * condition, the REJ frame has been
1358 * transmitted when entering the
1359 * condition, so it has not be done
1360 * here
1361 */
1362 } else if (dl->own_busy) {
1363 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1364 "command not in timer recovery "
1365 "state and in own busy "
1366 "condition received, so we "
1367 "respond with RNR final "
1368 "response\n");
1369 lapd_send_rnr(lctx, 1, 0);
1370 }
1371 } else
1372 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1373 "polling command not in timer recovery "
1374 "state received\n");
1375 /* send MDL ERROR INIDCATION to L3 */
1376 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1377 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1378 }
1379
1380 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1381 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1382 "recovery state received\n");
1383 /* Clear an existing peer receiver busy condition */
1384 dl->peer_busy = 0;
1385 /* V(S) and V(A) to the N(R) in the REJ frame */
1386 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001387 /* stop Timer T200 */
1388 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001389 /* 5.5.7 Clear timer recovery condition */
1390 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1391 } else {
1392 /* Clear an existing peer receiver busy condition */
1393 dl->peer_busy = 0;
1394 /* V(S) and V(A) to the N(R) in the REJ frame */
1395 dl->v_send = dl->v_ack = lctx->n_recv;
1396 /* 5.5.3.2 */
1397 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1398 if (!dl->own_busy && !dl->seq_err_cond) {
1399 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1400 "command in timer recovery "
1401 "state and not in own busy "
1402 "condition received, so we "
1403 "respond with RR final "
1404 "response\n");
1405 lapd_send_rr(lctx, 1, 0);
1406 /* NOTE: In case of sequence error
1407 * condition, the REJ frame has been
1408 * transmitted when entering the
1409 * condition, so it has not be done
1410 * here
1411 */
1412 } else if (dl->own_busy) {
1413 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1414 "command in timer recovery "
1415 "state and in own busy "
1416 "condition received, so we "
1417 "respond with RNR final "
1418 "response\n");
1419 lapd_send_rnr(lctx, 1, 0);
1420 }
1421 } else
1422 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1423 "polling command in timer recovery "
1424 "state received\n");
1425 }
1426
1427 /* FIXME: 5.5.4.2 2) */
1428
1429 /* Send message, if possible due to acknowledged data */
1430 lapd_send_i(lctx, __LINE__);
1431
1432 break;
1433 default:
1434 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001435 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001436 msgb_free(msg);
1437 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1438 return -EINVAL;
1439 }
1440 msgb_free(msg);
1441 return 0;
1442}
1443
1444/* Receive a LAPD I (Information) message from L1 */
1445static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1446{
1447 struct lapd_datalink *dl = lctx->dl;
1448 //uint8_t nr = lctx->n_recv;
1449 uint8_t ns = lctx->n_send;
1450 int length = lctx->length;
1451 int rc;
1452
1453 LOGP(DLLAPD, LOGL_INFO, "I received in state %s\n",
1454 lapd_state_names[dl->state]);
1455
1456 /* G.2.2 Wrong value of the C/R bit */
1457 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001458 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001459 msgb_free(msg);
1460 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1461 return -EINVAL;
1462 }
1463
1464 if (length == 0 || length > lctx->n201) {
1465 /* G.4.2 If the length indicator of an I frame is set
1466 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1467 * primitive with cause "I frame with incorrect length"
1468 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001469 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001470 msgb_free(msg);
1471 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1472 return -EIO;
1473 }
1474
1475 /* G.4.2 If the numerical value of L is L<N201 and the M
1476 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1477 * cause "I frame with incorrect use of M bit" is sent to the
1478 * mobile management entity. */
1479 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001480 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001481 msgb_free(msg);
1482 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1483 return -EIO;
1484 }
1485
1486 switch (dl->state) {
1487 case LAPD_STATE_IDLE:
1488 /* if P=1, respond DM with F=1 (5.2.2) */
1489 /* 5.4.5 all other frame types shall be discarded */
1490 if (lctx->p_f)
1491 lapd_send_dm(lctx); /* F=P */
1492 /* fall though */
1493 case LAPD_STATE_SABM_SENT:
1494 case LAPD_STATE_DISC_SENT:
1495 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1496 msgb_free(msg);
1497 return 0;
1498 }
1499
1500 /* 5.7.1: N(s) sequence error */
1501 if (ns != dl->v_recv) {
1502 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1503 "V(R)=%u\n", ns, dl->v_recv);
1504 /* discard data */
1505 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001506 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001507 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001508 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001509 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001510 lapd_send_rej(lctx, lctx->p_f);
1511 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001512 /* If there are two subsequent sequence errors received,
1513 * ignore it. (Ignore every second subsequent error.)
1514 * This happens if our reply with the REJ is too slow,
1515 * so the remote gets a T200 timeout and sends another
1516 * frame with a sequence error.
1517 * Test showed that replying with two subsequent REJ
1518 * messages could the remote L2 process to abort.
1519 * Replying too slow shouldn't happen, but may happen
1520 * over serial link between BB and LAPD.
1521 */
1522 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001523 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001524 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1525 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1526 lapd_acknowledge(lctx); /* V(A) is also set here */
1527
1528 /* Send message, if possible due to acknowledged data */
1529 lapd_send_i(lctx, __LINE__);
1530
1531 return 0;
rootaf48bed2011-09-26 11:23:06 +02001532 }
1533 dl->seq_err_cond = 0;
1534
1535 /* Increment receiver state */
1536 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1537 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1538
1539 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1540 lapd_acknowledge(lctx); /* V(A) is also set here */
1541
1542 /* Only if we are not in own receiver busy condition */
1543 if (!dl->own_busy) {
1544 /* if the frame carries a complete segment */
1545 if (!lctx->more && !dl->rcv_buffer) {
1546 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1547 /* send a DATA INDICATION to L3 */
1548 msg->len = length;
1549 msg->tail = msg->data + length;
1550 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1551 msg);
1552 } else {
1553 /* create rcv_buffer */
1554 if (!dl->rcv_buffer) {
1555 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1556 "I frames (first message)\n");
1557 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1558 "LAPD RX");
1559 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1560 }
1561 /* concat. rcv_buffer */
1562 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1563 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1564 "overflow!\n");
1565 } else {
1566 memcpy(msgb_put(dl->rcv_buffer, length),
1567 msg->l3h, length);
1568 }
1569 /* if the last segment was received */
1570 if (!lctx->more) {
1571 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1572 "I frames (last message)\n");
1573 rc = send_dl_l3(PRIM_DL_DATA,
1574 PRIM_OP_INDICATION, lctx,
1575 dl->rcv_buffer);
1576 dl->rcv_buffer = NULL;
1577 } else
1578 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1579 "I frames (next message)\n");
1580 msgb_free(msg);
1581
1582 }
1583 } else
1584 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1585 "busy condition\n");
1586
1587 /* Check for P bit */
1588 if (lctx->p_f) {
1589 /* 5.5.2.1 */
1590 /* check if we are not in own receiver busy */
1591 if (!dl->own_busy) {
1592 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1593 /* Send RR with F=1 */
1594 rc = lapd_send_rr(lctx, 1, 0);
1595 } else {
1596 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1597 /* Send RNR with F=1 */
1598 rc = lapd_send_rnr(lctx, 1, 0);
1599 }
1600 } else {
1601 /* 5.5.2.2 */
1602 /* check if we are not in own receiver busy */
1603 if (!dl->own_busy) {
1604 /* NOTE: V(R) is already set above */
1605 rc = lapd_send_i(lctx, __LINE__);
1606 if (rc) {
1607 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1608 "have no pending data, send RR\n");
1609 /* Send RR with F=0 */
1610 return lapd_send_rr(lctx, 0, 0);
1611 }
1612 /* all I or one RR is sent, we are done */
1613 return 0;
1614 } else {
1615 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1616 /* Send RNR with F=0 */
1617 rc = lapd_send_rnr(lctx, 0, 0);
1618 }
1619 }
1620
1621 /* Send message, if possible due to acknowledged data */
1622 lapd_send_i(lctx, __LINE__);
1623
1624 return rc;
1625}
1626
1627/* Receive a LAPD message from L1 */
1628int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1629{
1630 int rc;
1631
1632 switch (lctx->format) {
1633 case LAPD_FORM_U:
1634 rc = lapd_rx_u(msg, lctx);
1635 break;
1636 case LAPD_FORM_S:
1637 rc = lapd_rx_s(msg, lctx);
1638 break;
1639 case LAPD_FORM_I:
1640 rc = lapd_rx_i(msg, lctx);
1641 break;
1642 default:
1643 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1644 msgb_free(msg);
1645 rc = -EINVAL;
1646 }
1647 return rc;
1648}
1649
1650/* L3 -> L2 */
1651
1652/* send unit data */
1653static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1654{
1655 struct lapd_datalink *dl = lctx->dl;
1656 struct msgb *msg = dp->oph.msg;
1657 struct lapd_msg_ctx nctx;
1658
1659 memcpy(&nctx, lctx, sizeof(nctx));
1660 /* keep nctx.ldp */
1661 /* keep nctx.sapi */
1662 /* keep nctx.tei */
1663 nctx.cr = dl->cr.loc2rem.cmd;
1664 nctx.format = LAPD_FORM_U;
1665 nctx.s_u = LAPD_U_UI;
1666 /* keep nctx.p_f */
1667 nctx.length = msg->len;
1668 nctx.more = 0;
1669
1670 return dl->send_ph_data_req(&nctx, msg);
1671}
1672
1673/* request link establishment */
1674static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1675{
1676 struct lapd_datalink *dl = lctx->dl;
1677 struct msgb *msg = dp->oph.msg;
1678 struct lapd_msg_ctx nctx;
1679
1680 if (msg->len)
1681 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1682 "(SABM)\n");
1683 else
1684 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1685
1686 /* Flush send-queue */
1687 /* Clear send-buffer */
1688 lapd_dl_flush_send(dl);
1689 /* be sure that history is empty */
1690 lapd_dl_flush_hist(dl);
1691
1692 /* save message context for further use */
1693 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1694
1695 /* Discard partly received L3 message */
1696 if (dl->rcv_buffer) {
1697 msgb_free(dl->rcv_buffer);
1698 dl->rcv_buffer = NULL;
1699 }
1700
1701 /* assemble message */
1702 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1703 /* keep nctx.ldp */
1704 /* keep nctx.sapi */
1705 /* keep nctx.tei */
1706 nctx.cr = dl->cr.loc2rem.cmd;
1707 nctx.format = LAPD_FORM_U;
1708 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1709 nctx.p_f = 1;
1710 nctx.length = msg->len;
1711 nctx.more = 0;
1712
1713 /* Transmit-buffer carries exactly one segment */
1714 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1715 msgb_put(dl->tx_hist[0].msg, msg->len);
1716 if (msg->len)
1717 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1718 dl->tx_hist[0].more = 0;
1719 /* set Vs to 0, because it is used as index when resending SABM */
1720 dl->v_send = 0;
1721
1722 /* Set states */
1723 dl->own_busy = dl->peer_busy = 0;
1724 dl->retrans_ctr = 0;
1725 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1726
1727 /* Tramsmit and start T200 */
1728 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001729 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001730
1731 return 0;
1732}
1733
1734/* send data */
1735static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1736{
1737 struct lapd_datalink *dl = lctx->dl;
1738 struct msgb *msg = dp->oph.msg;
1739
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001740 if (msgb_l3len(msg) == 0) {
1741 LOGP(DLLAPD, LOGL_ERROR,
1742 "writing an empty message is not possible.\n");
1743 msgb_free(msg);
1744 return -1;
1745 }
1746
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001747 LOGP(DLLAPD, LOGL_INFO,
1748 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001749
1750 /* Write data into the send queue */
1751 msgb_enqueue(&dl->send_queue, msg);
1752
1753 /* Send message, if possible */
1754 lapd_send_i(&dl->lctx, __LINE__);
1755
1756 return 0;
1757}
1758
1759/* Send next I frame from queued/buffered data */
1760static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1761{
1762 struct lapd_datalink *dl = lctx->dl;
1763 uint8_t k = dl->k;
1764 uint8_t h;
1765 struct msgb *msg;
1766 int length, left;
1767 int rc = - 1; /* we sent nothing */
1768 struct lapd_msg_ctx nctx;
1769
1770
1771 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1772
1773 next_frame:
1774
1775 if (dl->peer_busy) {
1776 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1777 return rc;
1778 }
1779
1780 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1781 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1782 return rc;
1783 }
1784
1785 /* If the send state variable V(S) is equal to V(A) plus k
1786 * (where k is the maximum number of outstanding I frames - see
1787 * subclause 5.8.4), the data link layer entity shall not transmit any
1788 * new I frames, but shall retransmit an I frame as a result
1789 * of the error recovery procedures as described in subclauses 5.5.4 and
1790 * 5.5.7. */
1791 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1792 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1793 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1794 dl->v_ack);
1795 return rc;
1796 }
1797
1798 h = do_mod(dl->v_send, dl->range_hist);
1799
1800 /* if we have no tx_hist yet, we create it */
1801 if (!dl->tx_hist[h].msg) {
1802 /* Get next message into send-buffer, if any */
1803 if (!dl->send_buffer) {
1804 next_message:
1805 dl->send_out = 0;
1806 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1807 /* No more data to be sent */
1808 if (!dl->send_buffer)
1809 return rc;
1810 LOGP(DLLAPD, LOGL_INFO, "get message from "
1811 "send-queue\n");
1812 }
1813
1814 /* How much is left in the send-buffer? */
1815 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1816 /* Segment, if data exceeds N201 */
1817 length = left;
1818 if (length > lctx->n201)
1819 length = lctx->n201;
1820 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1821 "length %d first byte %02x\n",
1822 msgb_l3len(dl->send_buffer), dl->send_out, left,
1823 lctx->n201, length, dl->send_buffer->l3h[0]);
1824 /* If message in send-buffer is completely sent */
1825 if (left == 0) {
1826 msgb_free(dl->send_buffer);
1827 dl->send_buffer = NULL;
1828 goto next_message;
1829 }
1830
1831 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1832 (left > length) ? "segment " : "", dl->v_send);
1833
1834 /* Create I frame (segment) and transmit-buffer content */
1835 msg = lapd_msgb_alloc(length, "LAPD I");
1836 msg->l3h = msgb_put(msg, length);
1837 /* assemble message */
1838 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1839 /* keep nctx.ldp */
1840 /* keep nctx.sapi */
1841 /* keep nctx.tei */
1842 nctx.cr = dl->cr.loc2rem.cmd;
1843 nctx.format = LAPD_FORM_I;
1844 nctx.p_f = 0;
1845 nctx.n_send = dl->v_send;
1846 nctx.n_recv = dl->v_recv;
1847 nctx.length = length;
1848 if (left > length)
1849 nctx.more = 1;
1850 else
1851 nctx.more = 0;
1852 if (length)
1853 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1854 length);
1855 /* store in tx_hist */
1856 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1857 msgb_put(dl->tx_hist[h].msg, msg->len);
1858 if (length)
1859 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1860 dl->tx_hist[h].more = nctx.more;
1861 /* Add length to track how much is already in the tx buffer */
1862 dl->send_out += length;
1863 } else {
1864 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1865 "V(S)=%d\n", dl->v_send);
1866
1867 /* Create I frame (segment) from tx_hist */
1868 length = dl->tx_hist[h].msg->len;
1869 msg = lapd_msgb_alloc(length, "LAPD I resend");
1870 msg->l3h = msgb_put(msg, length);
1871 /* assemble message */
1872 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1873 /* keep nctx.ldp */
1874 /* keep nctx.sapi */
1875 /* keep nctx.tei */
1876 nctx.cr = dl->cr.loc2rem.cmd;
1877 nctx.format = LAPD_FORM_I;
1878 nctx.p_f = 0;
1879 nctx.n_send = dl->v_send;
1880 nctx.n_recv = dl->v_recv;
1881 nctx.length = length;
1882 nctx.more = dl->tx_hist[h].more;
1883 if (length)
1884 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1885 }
1886
1887 /* The value of the send state variable V(S) shall be incremented by 1
1888 * at the end of the transmission of the I frame */
1889 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1890
1891 /* If timer T200 is not running at the time right before transmitting a
1892 * frame, when the PH-READY-TO-SEND primitive is received from the
1893 * physical layer., it shall be set. */
1894 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001895 /* stop Timer T203, if running */
1896 lapd_stop_t203(dl);
1897 /* start Timer T200 */
1898 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001899 }
1900
1901 dl->send_ph_data_req(&nctx, msg);
1902
1903 rc = 0; /* we sent something */
1904 goto next_frame;
1905}
1906
1907/* request link suspension */
1908static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1909{
1910 struct lapd_datalink *dl = lctx->dl;
1911 struct msgb *msg = dp->oph.msg;
1912
1913 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1914
1915 /* put back the send-buffer to the send-queue (first position) */
1916 if (dl->send_buffer) {
1917 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1918 "queue\n");
1919 llist_add(&dl->send_buffer->list, &dl->send_queue);
1920 dl->send_buffer = NULL;
1921 } else
1922 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1923
1924 /* Clear transmit buffer, but keep send buffer */
1925 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001926 /* Stop timers (there is no state change, so we must stop all timers */
1927 lapd_stop_t200(dl);
1928 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001929
1930 msgb_free(msg);
1931
1932 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1933}
1934
1935/* requesst resume or reconnect of link */
1936static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1937{
1938 struct lapd_datalink *dl = lctx->dl;
1939 struct msgb *msg = dp->oph.msg;
1940 struct lapd_msg_ctx nctx;
1941
1942 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1943 msg->len);
1944
1945 /* be sure that history is empty */
1946 lapd_dl_flush_hist(dl);
1947
1948 /* save message context for further use */
1949 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1950
1951 /* Replace message in the send-buffer (reconnect) */
1952 if (dl->send_buffer)
1953 msgb_free(dl->send_buffer);
1954 dl->send_out = 0;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001955 if (msg && msg->len)
rootaf48bed2011-09-26 11:23:06 +02001956 /* Write data into the send buffer, to be sent first */
1957 dl->send_buffer = msg;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001958 else
1959 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001960
1961 /* Discard partly received L3 message */
1962 if (dl->rcv_buffer) {
1963 msgb_free(dl->rcv_buffer);
1964 dl->rcv_buffer = NULL;
1965 }
1966
1967 /* Create new msgb (old one is now free) */
1968 msg = lapd_msgb_alloc(0, "LAPD SABM");
1969 msg->l3h = msg->data;
1970 /* assemble message */
1971 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1972 /* keep nctx.ldp */
1973 /* keep nctx.sapi */
1974 /* keep nctx.tei */
1975 nctx.cr = dl->cr.loc2rem.cmd;
1976 nctx.format = LAPD_FORM_U;
1977 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1978 nctx.p_f = 1;
1979 nctx.length = 0;
1980 nctx.more = 0;
1981
1982 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1983 msgb_put(dl->tx_hist[0].msg, msg->len);
1984 if (msg->len)
1985 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1986 dl->tx_hist[0].more = 0;
1987 /* set Vs to 0, because it is used as index when resending SABM */
1988 dl->v_send = 0;
1989
1990 /* Set states */
1991 dl->own_busy = dl->peer_busy = 0;
1992 dl->retrans_ctr = 0;
1993 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1994
1995 /* Tramsmit and start T200 */
1996 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001997 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001998
1999 return 0;
2000}
2001
2002/* requesst release of link */
2003static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2004{
2005 struct lapd_datalink *dl = lctx->dl;
2006 struct msgb *msg = dp->oph.msg;
2007 struct lapd_msg_ctx nctx;
2008
2009 /* local release */
2010 if (dp->u.rel_req.mode) {
2011 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2012 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002013 /* stop Timer T200 */
2014 lapd_stop_t200(dl);
2015 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002016 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2017 /* flush buffers */
2018 lapd_dl_flush_tx(dl);
2019 lapd_dl_flush_send(dl);
2020 /* send notification to L3 */
2021 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2022 }
2023
2024 /* in case we are already disconnecting */
2025 if (dl->state == LAPD_STATE_DISC_SENT)
2026 return -EBUSY;
2027
2028 /* flush tx_hist */
2029 lapd_dl_flush_hist(dl);
2030
2031 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2032
2033 /* Push LAPD header on msgb */
2034 /* assemble message */
2035 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2036 /* keep nctx.ldp */
2037 /* keep nctx.sapi */
2038 /* keep nctx.tei */
2039 nctx.cr = dl->cr.loc2rem.cmd;
2040 nctx.format = LAPD_FORM_U;
2041 nctx.s_u = LAPD_U_DISC;
2042 nctx.p_f = 1;
2043 nctx.length = 0;
2044 nctx.more = 0;
2045
2046 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2047 msgb_put(dl->tx_hist[0].msg, msg->len);
2048 if (msg->len)
2049 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2050 dl->tx_hist[0].more = 0;
2051 /* set Vs to 0, because it is used as index when resending DISC */
2052 dl->v_send = 0;
2053
2054 /* Set states */
2055 dl->own_busy = dl->peer_busy = 0;
2056 dl->retrans_ctr = 0;
2057 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2058
2059 /* Tramsmit and start T200 */
2060 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002061 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002062
2063 return 0;
2064}
2065
2066/* request release of link in idle state */
2067static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2068 struct lapd_msg_ctx *lctx)
2069{
2070 struct lapd_datalink *dl = lctx->dl;
2071 struct msgb *msg = dp->oph.msg;
2072
2073 msgb_free(msg);
2074
2075 /* send notification to L3 */
2076 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2077}
2078
2079/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002080static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002081 uint32_t states;
2082 int prim, op;
2083 const char *name;
2084 int (*rout) (struct osmo_dlsap_prim *dp,
2085 struct lapd_msg_ctx *lctx);
2086} l2downstatelist[] = {
2087 /* create and send UI command */
2088 {ALL_STATES,
2089 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2090 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2091
2092 /* create and send SABM command */
2093 {SBIT(LAPD_STATE_IDLE),
2094 PRIM_DL_EST, PRIM_OP_REQUEST,
2095 "DL-ESTABLISH-REQUEST", lapd_est_req},
2096
2097 /* create and send I command */
2098 {SBIT(LAPD_STATE_MF_EST) |
2099 SBIT(LAPD_STATE_TIMER_RECOV),
2100 PRIM_DL_DATA, PRIM_OP_REQUEST,
2101 "DL-DATA-REQUEST", lapd_data_req},
2102
2103 /* suspend datalink */
2104 {SBIT(LAPD_STATE_MF_EST) |
2105 SBIT(LAPD_STATE_TIMER_RECOV),
2106 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2107 "DL-SUSPEND-REQUEST", lapd_susp_req},
2108
2109 /* create and send SABM command (resume) */
2110 {SBIT(LAPD_STATE_MF_EST) |
2111 SBIT(LAPD_STATE_TIMER_RECOV),
2112 PRIM_DL_RES, PRIM_OP_REQUEST,
2113 "DL-RESUME-REQUEST", lapd_res_req},
2114
2115 /* create and send SABM command (reconnect) */
2116 {SBIT(LAPD_STATE_IDLE) |
2117 SBIT(LAPD_STATE_MF_EST) |
2118 SBIT(LAPD_STATE_TIMER_RECOV),
2119 PRIM_DL_RECON, PRIM_OP_REQUEST,
2120 "DL-RECONNECT-REQUEST", lapd_res_req},
2121
2122 /* create and send DISC command */
2123 {SBIT(LAPD_STATE_SABM_SENT) |
2124 SBIT(LAPD_STATE_MF_EST) |
2125 SBIT(LAPD_STATE_TIMER_RECOV) |
2126 SBIT(LAPD_STATE_DISC_SENT),
2127 PRIM_DL_REL, PRIM_OP_REQUEST,
2128 "DL-RELEASE-REQUEST", lapd_rel_req},
2129
2130 /* release in idle state */
2131 {SBIT(LAPD_STATE_IDLE),
2132 PRIM_DL_REL, PRIM_OP_REQUEST,
2133 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2134};
2135
2136#define L2DOWNSLLEN \
2137 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2138
2139int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2140{
2141 struct lapd_datalink *dl = lctx->dl;
2142 int i, supported = 0;
2143 struct msgb *msg = dp->oph.msg;
2144 int rc;
2145
2146 /* find function for current state and message */
2147 for (i = 0; i < L2DOWNSLLEN; i++) {
2148 if (dp->oph.primitive == l2downstatelist[i].prim
2149 && dp->oph.operation == l2downstatelist[i].op) {
2150 supported = 1;
2151 if ((SBIT(dl->state) & l2downstatelist[i].states))
2152 break;
2153 }
2154 }
2155 if (!supported) {
2156 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2157 dp->oph.primitive, dp->oph.operation);
2158 msgb_free(msg);
2159 return 0;
2160 }
2161 if (i == L2DOWNSLLEN) {
2162 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2163 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2164 lapd_state_names[dl->state]);
2165 msgb_free(msg);
2166 return 0;
2167 }
2168
2169 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2170 l2downstatelist[i].name, lapd_state_names[dl->state]);
2171
2172 rc = l2downstatelist[i].rout(dp, lctx);
2173
2174 return rc;
2175}
2176
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002177/*! @} */