blob: 116e3116d871986c0488fb04f55569a845cf6244 [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 */
Harald Welte087116a2013-06-18 21:41:34 +0200902 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200903 rc = send_dl_l3(prim, op, lctx, msg);
904 }
905 break;
906 case LAPD_U_DM:
907 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
908 lapd_state_names[dl->state]);
909 /* G.2.2 Wrong value of the C/R bit */
910 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100911 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200912 msgb_free(msg);
913 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
914 return -EINVAL;
915 }
916 if (!lctx->p_f) {
917 /* 5.4.1.2 DM responses with the F bit set to "0"
918 * shall be ignored.
919 */
920 msgb_free(msg);
921 return 0;
922 }
923 switch (dl->state) {
924 case LAPD_STATE_SABM_SENT:
925 break;
926 case LAPD_STATE_MF_EST:
927 if (lctx->p_f) {
928 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
929 "response\n");
930 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
931 } else {
932 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
933 "response, multiple frame established "
934 "state\n");
935 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
936 /* reestablish */
937 if (!dl->reestablish) {
938 msgb_free(msg);
939 return 0;
940 }
941 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
942 "reestablishment.\n");
943 lapd_reestablish(dl);
944 }
945 msgb_free(msg);
946 return 0;
947 case LAPD_STATE_TIMER_RECOV:
948 /* FP = 0 (DM is normal in case PF = 1) */
949 if (!lctx->p_f) {
950 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
951 "response, multiple frame established "
952 "state\n");
953 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
954 msgb_free(msg);
955 /* reestablish */
956 if (!dl->reestablish)
957 return 0;
958 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
959 "reestablishment.\n");
960 return lapd_reestablish(dl);
961 }
962 break;
963 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200964 /* stop Timer T200 */
965 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200966 /* go to idle state */
967 lapd_dl_flush_tx(dl);
968 lapd_dl_flush_send(dl);
969 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
970 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
971 msgb_free(msg);
972 return 0;
973 case LAPD_STATE_IDLE:
974 /* 5.4.5 all other frame types shall be discarded */
975 default:
976 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
977 "(discarding)\n");
978 msgb_free(msg);
979 return 0;
980 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200981 /* stop timer T200 */
982 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200983 /* go to idle state */
984 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
985 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
986 msgb_free(msg);
987 break;
988 case LAPD_U_UI:
989 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
990 /* G.2.2 Wrong value of the C/R bit */
991 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100992 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +0200993 "error\n");
994 msgb_free(msg);
995 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
996 return -EINVAL;
997 }
998
999 /* G.4.5 If UI is received with L>N201 or with M bit
1000 * set, AN MDL-ERROR-INDICATION is sent to MM.
1001 */
1002 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001003 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +02001004 "(%d > N201(%d) or M=%d)\n", length,
1005 lctx->n201, lctx->more);
1006 msgb_free(msg);
1007 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1008 return -EIO;
1009 }
1010
1011 /* do some length checks */
1012 if (length == 0) {
1013 /* 5.3.3 UI frames received with the length indicator
1014 * set to "0" shall be ignored
1015 */
1016 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1017 msgb_free(msg);
1018 return 0;
1019 }
Harald Welte087116a2013-06-18 21:41:34 +02001020 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001021 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1022 msg);
1023 break;
1024 case LAPD_U_DISC:
1025 prim = PRIM_DL_REL;
1026 op = PRIM_OP_INDICATION;
1027
1028 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1029 lapd_state_names[dl->state]);
1030 /* flush tx and send buffers */
1031 lapd_dl_flush_tx(dl);
1032 lapd_dl_flush_send(dl);
1033 /* 5.7.1 */
1034 dl->seq_err_cond = 0;
1035 /* G.2.2 Wrong value of the C/R bit */
1036 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001037 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001038 msgb_free(msg);
1039 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1040 return -EINVAL;
1041 }
1042 if (length > 0 || lctx->more) {
1043 /* G.4.4 If a DISC or DM frame is received with L>0 or
1044 * with the M bit set to "1", an MDL-ERROR-INDICATION
1045 * primitive with cause "U frame with incorrect
1046 * parameters" is sent to the mobile management entity.
1047 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001048 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001049 "U frame iwth incorrect parameters ");
1050 msgb_free(msg);
1051 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1052 return -EIO;
1053 }
1054 switch (dl->state) {
1055 case LAPD_STATE_IDLE:
1056 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1057 /* send DM with F=P */
1058 msgb_free(msg);
1059 return lapd_send_dm(lctx);
1060 case LAPD_STATE_SABM_SENT:
1061 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1062 /* 5.4.6.2 send DM with F=P */
1063 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001064 /* stop Timer T200 */
1065 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001066 /* go to idle state */
1067 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1068 msgb_free(msg);
1069 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1070 lctx);
1071 case LAPD_STATE_MF_EST:
1072 case LAPD_STATE_TIMER_RECOV:
1073 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1074 break;
1075 case LAPD_STATE_DISC_SENT:
1076 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1077 prim = PRIM_DL_REL;
1078 op = PRIM_OP_CONFIRM;
1079 break;
1080 default:
1081 lapd_send_ua(lctx, length, msg->l3h);
1082 msgb_free(msg);
1083 return 0;
1084 }
1085 /* send UA response */
1086 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001087 /* stop Timer T200 */
1088 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001089 /* enter idle state, keep tx-buffer with UA response */
1090 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1091 /* send notification to L3 */
1092 rc = send_dl_simple(prim, op, lctx);
1093 msgb_free(msg);
1094 break;
1095 case LAPD_U_UA:
1096 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1097 lapd_state_names[dl->state]);
1098 /* G.2.2 Wrong value of the C/R bit */
1099 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001100 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001101 "error\n");
1102 msgb_free(msg);
1103 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1104 return -EINVAL;
1105 }
1106
1107 /* G.4.5 If UA is received with L>N201 or with M bit
1108 * set, AN MDL-ERROR-INDICATION is sent to MM.
1109 */
1110 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001111 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001112 msgb_free(msg);
1113 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1114 return -EIO;
1115 }
1116
1117 if (!lctx->p_f) {
1118 /* 5.4.1.2 A UA response with the F bit set to "0"
1119 * shall be ignored.
1120 */
1121 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1122 msgb_free(msg);
1123 return 0;
1124 }
1125 switch (dl->state) {
1126 case LAPD_STATE_SABM_SENT:
1127 break;
1128 case LAPD_STATE_MF_EST:
1129 case LAPD_STATE_TIMER_RECOV:
1130 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1131 "(discarding)\n");
1132 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1133 msgb_free(msg);
1134 return 0;
1135 case LAPD_STATE_DISC_SENT:
1136 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001137 /* stop Timer T200 */
1138 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001139 /* go to idle state */
1140 lapd_dl_flush_tx(dl);
1141 lapd_dl_flush_send(dl);
1142 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1143 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1144 msgb_free(msg);
1145 return 0;
1146 case LAPD_STATE_IDLE:
1147 /* 5.4.5 all other frame types shall be discarded */
1148 default:
1149 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1150 "(discarding)\n");
1151 msgb_free(msg);
1152 return 0;
1153 }
1154 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001155 /* stop Timer T200 */
1156 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001157 /* compare UA with SABME if contention resolution is applied */
1158 if (dl->tx_hist[0].msg->len) {
1159 if (length != (dl->tx_hist[0].msg->len)
1160 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1161 length)) {
1162 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1163 "mismatches ****\n");
1164 rc = send_dl_simple(PRIM_DL_REL,
1165 PRIM_OP_INDICATION, lctx);
1166 msgb_free(msg);
1167 /* go to idle state */
1168 lapd_dl_flush_tx(dl);
1169 lapd_dl_flush_send(dl);
1170 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1171 return 0;
1172 }
1173 }
1174 /* set Vs, Vr and Va to 0 */
1175 dl->v_send = dl->v_recv = dl->v_ack = 0;
1176 /* clear tx_hist */
1177 lapd_dl_flush_hist(dl);
1178 /* enter multiple-frame-established state */
1179 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1180 /* send outstanding frames, if any (resume / reconnect) */
1181 lapd_send_i(lctx, __LINE__);
1182 /* send notification to L3 */
1183 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1184 msgb_free(msg);
1185 break;
1186 case LAPD_U_FRMR:
1187 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1188 /* send MDL ERROR INIDCATION to L3 */
1189 mdl_error(MDL_CAUSE_FRMR, lctx);
1190 msgb_free(msg);
1191 /* reestablish */
1192 if (!dl->reestablish)
1193 break;
1194 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1195 rc = lapd_reestablish(dl);
1196 break;
1197 default:
1198 /* G.3.1 */
1199 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1200 msgb_free(msg);
1201 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1202 return -EINVAL;
1203 }
1204 return rc;
1205}
1206
1207/* Receive a LAPD S (Supervisory) message from L1 */
1208static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1209{
1210 struct lapd_datalink *dl = lctx->dl;
1211 int length = lctx->length;
1212
1213 if (length > 0 || lctx->more) {
1214 /* G.4.3 If a supervisory frame is received with L>0 or
1215 * with the M bit set to "1", an MDL-ERROR-INDICATION
1216 * primitive with cause "S frame with incorrect
1217 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001218 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001219 "S frame with incorrect parameters\n");
1220 msgb_free(msg);
1221 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1222 return -EIO;
1223 }
1224
1225 if (lctx->cr == dl->cr.rem2loc.resp
1226 && lctx->p_f
1227 && dl->state != LAPD_STATE_TIMER_RECOV) {
1228 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1229 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1230 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1231 }
1232
1233 switch (dl->state) {
1234 case LAPD_STATE_IDLE:
1235 /* if P=1, respond DM with F=1 (5.2.2) */
1236 /* 5.4.5 all other frame types shall be discarded */
1237 if (lctx->p_f)
1238 lapd_send_dm(lctx); /* F=P */
1239 /* fall though */
1240 case LAPD_STATE_SABM_SENT:
1241 case LAPD_STATE_DISC_SENT:
1242 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1243 msgb_free(msg);
1244 return 0;
1245 }
1246 switch (lctx->s_u) {
1247 case LAPD_S_RR:
1248 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1249 lapd_state_names[dl->state]);
1250 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1251 lapd_acknowledge(lctx);
1252
1253 /* 5.5.3.2 */
1254 if (lctx->cr == dl->cr.rem2loc.cmd
1255 && lctx->p_f) {
1256 if (!dl->own_busy && !dl->seq_err_cond) {
1257 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1258 "with polling bit set and we are not "
1259 "busy, so we reply with RR frame "
1260 "response\n");
1261 lapd_send_rr(lctx, 1, 0);
1262 /* NOTE: In case of sequence error condition,
1263 * the REJ frame has been transmitted when
1264 * entering the condition, so it has not be
1265 * done here
1266 */
1267 } else if (dl->own_busy) {
1268 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1269 "with polling bit set and we are busy, "
1270 "so we reply with RR frame response\n");
1271 lapd_send_rnr(lctx, 1, 0);
1272 }
1273 } else if (lctx->cr == dl->cr.rem2loc.resp
1274 && lctx->p_f
1275 && dl->state == LAPD_STATE_TIMER_RECOV) {
1276 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1277 "and we are in timer recovery state, so "
1278 "we leave that state\n");
1279 /* V(S) to the N(R) in the RR frame */
1280 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001281 /* stop Timer T200 */
1282 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001283 /* 5.5.7 Clear timer recovery condition */
1284 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1285 }
1286 /* Send message, if possible due to acknowledged data */
1287 lapd_send_i(lctx, __LINE__);
1288
1289 break;
1290 case LAPD_S_RNR:
1291 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1292 lapd_state_names[dl->state]);
1293 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1294 lapd_acknowledge(lctx);
1295
1296 /* 5.5.5 */
1297 /* Set peer receiver busy condition */
1298 dl->peer_busy = 1;
1299
1300 if (lctx->p_f) {
1301 if (lctx->cr == dl->cr.rem2loc.cmd) {
1302 if (!dl->own_busy) {
1303 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1304 "command and we are not busy, "
1305 "so we reply with RR final "
1306 "response\n");
1307 /* Send RR with F=1 */
1308 lapd_send_rr(lctx, 1, 0);
1309 } else {
1310 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1311 "command and we are busy, so "
1312 "we reply with RNR final "
1313 "response\n");
1314 /* Send RNR with F=1 */
1315 lapd_send_rnr(lctx, 1, 0);
1316 }
1317 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1318 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1319 "and we in timer recovery state, so "
1320 "we leave that state\n");
1321 /* 5.5.7 Clear timer recovery condition */
1322 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1323 /* V(S) to the N(R) in the RNR frame */
1324 dl->v_send = lctx->n_recv;
1325 }
1326 } else
1327 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1328 "received\n");
1329
1330 /* Send message, if possible due to acknowledged data */
1331 lapd_send_i(lctx, __LINE__);
1332
1333 break;
1334 case LAPD_S_REJ:
1335 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1336 lapd_state_names[dl->state]);
1337 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1338 lapd_acknowledge(lctx);
1339
1340 /* 5.5.4.1 */
1341 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1342 /* Clear an existing peer receiver busy condition */
1343 dl->peer_busy = 0;
1344 /* V(S) and V(A) to the N(R) in the REJ frame */
1345 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001346 /* stop Timer T200 */
1347 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001348 /* 5.5.3.2 */
1349 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1350 if (!dl->own_busy && !dl->seq_err_cond) {
1351 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1352 "command not in timer recovery "
1353 "state and not in own busy "
1354 "condition received, so we "
1355 "respond with RR final "
1356 "response\n");
1357 lapd_send_rr(lctx, 1, 0);
1358 /* NOTE: In case of sequence error
1359 * condition, the REJ frame has been
1360 * transmitted when entering the
1361 * condition, so it has not be done
1362 * here
1363 */
1364 } else if (dl->own_busy) {
1365 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1366 "command not in timer recovery "
1367 "state and in own busy "
1368 "condition received, so we "
1369 "respond with RNR final "
1370 "response\n");
1371 lapd_send_rnr(lctx, 1, 0);
1372 }
1373 } else
1374 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1375 "polling command not in timer recovery "
1376 "state received\n");
1377 /* send MDL ERROR INIDCATION to L3 */
1378 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1379 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1380 }
1381
1382 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1383 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1384 "recovery state received\n");
1385 /* Clear an existing peer receiver busy condition */
1386 dl->peer_busy = 0;
1387 /* V(S) and V(A) to the N(R) in the REJ frame */
1388 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001389 /* stop Timer T200 */
1390 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001391 /* 5.5.7 Clear timer recovery condition */
1392 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1393 } else {
1394 /* Clear an existing peer receiver busy condition */
1395 dl->peer_busy = 0;
1396 /* V(S) and V(A) to the N(R) in the REJ frame */
1397 dl->v_send = dl->v_ack = lctx->n_recv;
1398 /* 5.5.3.2 */
1399 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1400 if (!dl->own_busy && !dl->seq_err_cond) {
1401 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1402 "command in timer recovery "
1403 "state and not in own busy "
1404 "condition received, so we "
1405 "respond with RR final "
1406 "response\n");
1407 lapd_send_rr(lctx, 1, 0);
1408 /* NOTE: In case of sequence error
1409 * condition, the REJ frame has been
1410 * transmitted when entering the
1411 * condition, so it has not be done
1412 * here
1413 */
1414 } else if (dl->own_busy) {
1415 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1416 "command in timer recovery "
1417 "state and in own busy "
1418 "condition received, so we "
1419 "respond with RNR final "
1420 "response\n");
1421 lapd_send_rnr(lctx, 1, 0);
1422 }
1423 } else
1424 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1425 "polling command in timer recovery "
1426 "state received\n");
1427 }
1428
1429 /* FIXME: 5.5.4.2 2) */
1430
1431 /* Send message, if possible due to acknowledged data */
1432 lapd_send_i(lctx, __LINE__);
1433
1434 break;
1435 default:
1436 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001437 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001438 msgb_free(msg);
1439 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1440 return -EINVAL;
1441 }
1442 msgb_free(msg);
1443 return 0;
1444}
1445
1446/* Receive a LAPD I (Information) message from L1 */
1447static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1448{
1449 struct lapd_datalink *dl = lctx->dl;
1450 //uint8_t nr = lctx->n_recv;
1451 uint8_t ns = lctx->n_send;
1452 int length = lctx->length;
1453 int rc;
1454
1455 LOGP(DLLAPD, LOGL_INFO, "I received in state %s\n",
1456 lapd_state_names[dl->state]);
1457
1458 /* G.2.2 Wrong value of the C/R bit */
1459 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001460 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001461 msgb_free(msg);
1462 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1463 return -EINVAL;
1464 }
1465
1466 if (length == 0 || length > lctx->n201) {
1467 /* G.4.2 If the length indicator of an I frame is set
1468 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1469 * primitive with cause "I frame with incorrect length"
1470 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001471 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001472 msgb_free(msg);
1473 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1474 return -EIO;
1475 }
1476
1477 /* G.4.2 If the numerical value of L is L<N201 and the M
1478 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1479 * cause "I frame with incorrect use of M bit" is sent to the
1480 * mobile management entity. */
1481 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001482 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001483 msgb_free(msg);
1484 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1485 return -EIO;
1486 }
1487
1488 switch (dl->state) {
1489 case LAPD_STATE_IDLE:
1490 /* if P=1, respond DM with F=1 (5.2.2) */
1491 /* 5.4.5 all other frame types shall be discarded */
1492 if (lctx->p_f)
1493 lapd_send_dm(lctx); /* F=P */
1494 /* fall though */
1495 case LAPD_STATE_SABM_SENT:
1496 case LAPD_STATE_DISC_SENT:
1497 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1498 msgb_free(msg);
1499 return 0;
1500 }
1501
1502 /* 5.7.1: N(s) sequence error */
1503 if (ns != dl->v_recv) {
1504 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1505 "V(R)=%u\n", ns, dl->v_recv);
1506 /* discard data */
1507 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001508 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001509 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001510 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001511 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001512 lapd_send_rej(lctx, lctx->p_f);
1513 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001514 /* If there are two subsequent sequence errors received,
1515 * ignore it. (Ignore every second subsequent error.)
1516 * This happens if our reply with the REJ is too slow,
1517 * so the remote gets a T200 timeout and sends another
1518 * frame with a sequence error.
1519 * Test showed that replying with two subsequent REJ
1520 * messages could the remote L2 process to abort.
1521 * Replying too slow shouldn't happen, but may happen
1522 * over serial link between BB and LAPD.
1523 */
1524 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001525 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001526 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1527 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1528 lapd_acknowledge(lctx); /* V(A) is also set here */
1529
1530 /* Send message, if possible due to acknowledged data */
1531 lapd_send_i(lctx, __LINE__);
1532
1533 return 0;
rootaf48bed2011-09-26 11:23:06 +02001534 }
1535 dl->seq_err_cond = 0;
1536
1537 /* Increment receiver state */
1538 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1539 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1540
1541 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1542 lapd_acknowledge(lctx); /* V(A) is also set here */
1543
1544 /* Only if we are not in own receiver busy condition */
1545 if (!dl->own_busy) {
1546 /* if the frame carries a complete segment */
1547 if (!lctx->more && !dl->rcv_buffer) {
1548 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1549 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001550 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001551 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1552 msg);
1553 } else {
1554 /* create rcv_buffer */
1555 if (!dl->rcv_buffer) {
1556 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1557 "I frames (first message)\n");
1558 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1559 "LAPD RX");
1560 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1561 }
1562 /* concat. rcv_buffer */
1563 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1564 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1565 "overflow!\n");
1566 } else {
1567 memcpy(msgb_put(dl->rcv_buffer, length),
1568 msg->l3h, length);
1569 }
1570 /* if the last segment was received */
1571 if (!lctx->more) {
1572 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1573 "I frames (last message)\n");
1574 rc = send_dl_l3(PRIM_DL_DATA,
1575 PRIM_OP_INDICATION, lctx,
1576 dl->rcv_buffer);
1577 dl->rcv_buffer = NULL;
1578 } else
1579 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1580 "I frames (next message)\n");
1581 msgb_free(msg);
1582
1583 }
1584 } else
1585 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1586 "busy condition\n");
1587
1588 /* Check for P bit */
1589 if (lctx->p_f) {
1590 /* 5.5.2.1 */
1591 /* check if we are not in own receiver busy */
1592 if (!dl->own_busy) {
1593 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1594 /* Send RR with F=1 */
1595 rc = lapd_send_rr(lctx, 1, 0);
1596 } else {
1597 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1598 /* Send RNR with F=1 */
1599 rc = lapd_send_rnr(lctx, 1, 0);
1600 }
1601 } else {
1602 /* 5.5.2.2 */
1603 /* check if we are not in own receiver busy */
1604 if (!dl->own_busy) {
1605 /* NOTE: V(R) is already set above */
1606 rc = lapd_send_i(lctx, __LINE__);
1607 if (rc) {
1608 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1609 "have no pending data, send RR\n");
1610 /* Send RR with F=0 */
1611 return lapd_send_rr(lctx, 0, 0);
1612 }
1613 /* all I or one RR is sent, we are done */
1614 return 0;
1615 } else {
1616 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1617 /* Send RNR with F=0 */
1618 rc = lapd_send_rnr(lctx, 0, 0);
1619 }
1620 }
1621
1622 /* Send message, if possible due to acknowledged data */
1623 lapd_send_i(lctx, __LINE__);
1624
1625 return rc;
1626}
1627
1628/* Receive a LAPD message from L1 */
1629int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1630{
1631 int rc;
1632
1633 switch (lctx->format) {
1634 case LAPD_FORM_U:
1635 rc = lapd_rx_u(msg, lctx);
1636 break;
1637 case LAPD_FORM_S:
1638 rc = lapd_rx_s(msg, lctx);
1639 break;
1640 case LAPD_FORM_I:
1641 rc = lapd_rx_i(msg, lctx);
1642 break;
1643 default:
1644 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1645 msgb_free(msg);
1646 rc = -EINVAL;
1647 }
1648 return rc;
1649}
1650
1651/* L3 -> L2 */
1652
1653/* send unit data */
1654static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1655{
1656 struct lapd_datalink *dl = lctx->dl;
1657 struct msgb *msg = dp->oph.msg;
1658 struct lapd_msg_ctx nctx;
1659
1660 memcpy(&nctx, lctx, sizeof(nctx));
1661 /* keep nctx.ldp */
1662 /* keep nctx.sapi */
1663 /* keep nctx.tei */
1664 nctx.cr = dl->cr.loc2rem.cmd;
1665 nctx.format = LAPD_FORM_U;
1666 nctx.s_u = LAPD_U_UI;
1667 /* keep nctx.p_f */
1668 nctx.length = msg->len;
1669 nctx.more = 0;
1670
1671 return dl->send_ph_data_req(&nctx, msg);
1672}
1673
1674/* request link establishment */
1675static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1676{
1677 struct lapd_datalink *dl = lctx->dl;
1678 struct msgb *msg = dp->oph.msg;
1679 struct lapd_msg_ctx nctx;
1680
1681 if (msg->len)
1682 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1683 "(SABM)\n");
1684 else
1685 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1686
1687 /* Flush send-queue */
1688 /* Clear send-buffer */
1689 lapd_dl_flush_send(dl);
1690 /* be sure that history is empty */
1691 lapd_dl_flush_hist(dl);
1692
1693 /* save message context for further use */
1694 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1695
1696 /* Discard partly received L3 message */
1697 if (dl->rcv_buffer) {
1698 msgb_free(dl->rcv_buffer);
1699 dl->rcv_buffer = NULL;
1700 }
1701
1702 /* assemble message */
1703 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1704 /* keep nctx.ldp */
1705 /* keep nctx.sapi */
1706 /* keep nctx.tei */
1707 nctx.cr = dl->cr.loc2rem.cmd;
1708 nctx.format = LAPD_FORM_U;
1709 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1710 nctx.p_f = 1;
1711 nctx.length = msg->len;
1712 nctx.more = 0;
1713
1714 /* Transmit-buffer carries exactly one segment */
1715 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1716 msgb_put(dl->tx_hist[0].msg, msg->len);
1717 if (msg->len)
1718 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1719 dl->tx_hist[0].more = 0;
1720 /* set Vs to 0, because it is used as index when resending SABM */
1721 dl->v_send = 0;
1722
1723 /* Set states */
1724 dl->own_busy = dl->peer_busy = 0;
1725 dl->retrans_ctr = 0;
1726 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1727
1728 /* Tramsmit and start T200 */
1729 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001730 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001731
1732 return 0;
1733}
1734
1735/* send data */
1736static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1737{
1738 struct lapd_datalink *dl = lctx->dl;
1739 struct msgb *msg = dp->oph.msg;
1740
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001741 if (msgb_l3len(msg) == 0) {
1742 LOGP(DLLAPD, LOGL_ERROR,
1743 "writing an empty message is not possible.\n");
1744 msgb_free(msg);
1745 return -1;
1746 }
1747
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001748 LOGP(DLLAPD, LOGL_INFO,
1749 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001750
1751 /* Write data into the send queue */
1752 msgb_enqueue(&dl->send_queue, msg);
1753
1754 /* Send message, if possible */
1755 lapd_send_i(&dl->lctx, __LINE__);
1756
1757 return 0;
1758}
1759
1760/* Send next I frame from queued/buffered data */
1761static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1762{
1763 struct lapd_datalink *dl = lctx->dl;
1764 uint8_t k = dl->k;
1765 uint8_t h;
1766 struct msgb *msg;
1767 int length, left;
1768 int rc = - 1; /* we sent nothing */
1769 struct lapd_msg_ctx nctx;
1770
1771
1772 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1773
1774 next_frame:
1775
1776 if (dl->peer_busy) {
1777 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1778 return rc;
1779 }
1780
1781 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1782 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1783 return rc;
1784 }
1785
1786 /* If the send state variable V(S) is equal to V(A) plus k
1787 * (where k is the maximum number of outstanding I frames - see
1788 * subclause 5.8.4), the data link layer entity shall not transmit any
1789 * new I frames, but shall retransmit an I frame as a result
1790 * of the error recovery procedures as described in subclauses 5.5.4 and
1791 * 5.5.7. */
1792 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1793 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1794 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1795 dl->v_ack);
1796 return rc;
1797 }
1798
1799 h = do_mod(dl->v_send, dl->range_hist);
1800
1801 /* if we have no tx_hist yet, we create it */
1802 if (!dl->tx_hist[h].msg) {
1803 /* Get next message into send-buffer, if any */
1804 if (!dl->send_buffer) {
1805 next_message:
1806 dl->send_out = 0;
1807 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1808 /* No more data to be sent */
1809 if (!dl->send_buffer)
1810 return rc;
1811 LOGP(DLLAPD, LOGL_INFO, "get message from "
1812 "send-queue\n");
1813 }
1814
1815 /* How much is left in the send-buffer? */
1816 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1817 /* Segment, if data exceeds N201 */
1818 length = left;
1819 if (length > lctx->n201)
1820 length = lctx->n201;
1821 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1822 "length %d first byte %02x\n",
1823 msgb_l3len(dl->send_buffer), dl->send_out, left,
1824 lctx->n201, length, dl->send_buffer->l3h[0]);
1825 /* If message in send-buffer is completely sent */
1826 if (left == 0) {
1827 msgb_free(dl->send_buffer);
1828 dl->send_buffer = NULL;
1829 goto next_message;
1830 }
1831
1832 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1833 (left > length) ? "segment " : "", dl->v_send);
1834
1835 /* Create I frame (segment) and transmit-buffer content */
1836 msg = lapd_msgb_alloc(length, "LAPD I");
1837 msg->l3h = msgb_put(msg, length);
1838 /* assemble message */
1839 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1840 /* keep nctx.ldp */
1841 /* keep nctx.sapi */
1842 /* keep nctx.tei */
1843 nctx.cr = dl->cr.loc2rem.cmd;
1844 nctx.format = LAPD_FORM_I;
1845 nctx.p_f = 0;
1846 nctx.n_send = dl->v_send;
1847 nctx.n_recv = dl->v_recv;
1848 nctx.length = length;
1849 if (left > length)
1850 nctx.more = 1;
1851 else
1852 nctx.more = 0;
1853 if (length)
1854 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1855 length);
1856 /* store in tx_hist */
1857 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1858 msgb_put(dl->tx_hist[h].msg, msg->len);
1859 if (length)
1860 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1861 dl->tx_hist[h].more = nctx.more;
1862 /* Add length to track how much is already in the tx buffer */
1863 dl->send_out += length;
1864 } else {
1865 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1866 "V(S)=%d\n", dl->v_send);
1867
1868 /* Create I frame (segment) from tx_hist */
1869 length = dl->tx_hist[h].msg->len;
1870 msg = lapd_msgb_alloc(length, "LAPD I resend");
1871 msg->l3h = msgb_put(msg, length);
1872 /* assemble message */
1873 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1874 /* keep nctx.ldp */
1875 /* keep nctx.sapi */
1876 /* keep nctx.tei */
1877 nctx.cr = dl->cr.loc2rem.cmd;
1878 nctx.format = LAPD_FORM_I;
1879 nctx.p_f = 0;
1880 nctx.n_send = dl->v_send;
1881 nctx.n_recv = dl->v_recv;
1882 nctx.length = length;
1883 nctx.more = dl->tx_hist[h].more;
1884 if (length)
1885 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1886 }
1887
1888 /* The value of the send state variable V(S) shall be incremented by 1
1889 * at the end of the transmission of the I frame */
1890 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1891
1892 /* If timer T200 is not running at the time right before transmitting a
1893 * frame, when the PH-READY-TO-SEND primitive is received from the
1894 * physical layer., it shall be set. */
1895 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001896 /* stop Timer T203, if running */
1897 lapd_stop_t203(dl);
1898 /* start Timer T200 */
1899 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001900 }
1901
1902 dl->send_ph_data_req(&nctx, msg);
1903
1904 rc = 0; /* we sent something */
1905 goto next_frame;
1906}
1907
1908/* request link suspension */
1909static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1910{
1911 struct lapd_datalink *dl = lctx->dl;
1912 struct msgb *msg = dp->oph.msg;
1913
1914 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1915
1916 /* put back the send-buffer to the send-queue (first position) */
1917 if (dl->send_buffer) {
1918 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1919 "queue\n");
1920 llist_add(&dl->send_buffer->list, &dl->send_queue);
1921 dl->send_buffer = NULL;
1922 } else
1923 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1924
1925 /* Clear transmit buffer, but keep send buffer */
1926 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001927 /* Stop timers (there is no state change, so we must stop all timers */
1928 lapd_stop_t200(dl);
1929 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001930
1931 msgb_free(msg);
1932
1933 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1934}
1935
1936/* requesst resume or reconnect of link */
1937static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1938{
1939 struct lapd_datalink *dl = lctx->dl;
1940 struct msgb *msg = dp->oph.msg;
1941 struct lapd_msg_ctx nctx;
1942
1943 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1944 msg->len);
1945
1946 /* be sure that history is empty */
1947 lapd_dl_flush_hist(dl);
1948
1949 /* save message context for further use */
1950 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1951
1952 /* Replace message in the send-buffer (reconnect) */
1953 if (dl->send_buffer)
1954 msgb_free(dl->send_buffer);
1955 dl->send_out = 0;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001956 if (msg && msg->len)
rootaf48bed2011-09-26 11:23:06 +02001957 /* Write data into the send buffer, to be sent first */
1958 dl->send_buffer = msg;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001959 else
1960 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001961
1962 /* Discard partly received L3 message */
1963 if (dl->rcv_buffer) {
1964 msgb_free(dl->rcv_buffer);
1965 dl->rcv_buffer = NULL;
1966 }
1967
1968 /* Create new msgb (old one is now free) */
1969 msg = lapd_msgb_alloc(0, "LAPD SABM");
1970 msg->l3h = msg->data;
1971 /* assemble message */
1972 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1973 /* keep nctx.ldp */
1974 /* keep nctx.sapi */
1975 /* keep nctx.tei */
1976 nctx.cr = dl->cr.loc2rem.cmd;
1977 nctx.format = LAPD_FORM_U;
1978 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1979 nctx.p_f = 1;
1980 nctx.length = 0;
1981 nctx.more = 0;
1982
1983 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1984 msgb_put(dl->tx_hist[0].msg, msg->len);
1985 if (msg->len)
1986 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1987 dl->tx_hist[0].more = 0;
1988 /* set Vs to 0, because it is used as index when resending SABM */
1989 dl->v_send = 0;
1990
1991 /* Set states */
1992 dl->own_busy = dl->peer_busy = 0;
1993 dl->retrans_ctr = 0;
1994 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1995
1996 /* Tramsmit and start T200 */
1997 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001998 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001999
2000 return 0;
2001}
2002
2003/* requesst release of link */
2004static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2005{
2006 struct lapd_datalink *dl = lctx->dl;
2007 struct msgb *msg = dp->oph.msg;
2008 struct lapd_msg_ctx nctx;
2009
2010 /* local release */
2011 if (dp->u.rel_req.mode) {
2012 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2013 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002014 /* stop Timer T200 */
2015 lapd_stop_t200(dl);
2016 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002017 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2018 /* flush buffers */
2019 lapd_dl_flush_tx(dl);
2020 lapd_dl_flush_send(dl);
2021 /* send notification to L3 */
2022 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2023 }
2024
2025 /* in case we are already disconnecting */
2026 if (dl->state == LAPD_STATE_DISC_SENT)
2027 return -EBUSY;
2028
2029 /* flush tx_hist */
2030 lapd_dl_flush_hist(dl);
2031
2032 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2033
2034 /* Push LAPD header on msgb */
2035 /* assemble message */
2036 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2037 /* keep nctx.ldp */
2038 /* keep nctx.sapi */
2039 /* keep nctx.tei */
2040 nctx.cr = dl->cr.loc2rem.cmd;
2041 nctx.format = LAPD_FORM_U;
2042 nctx.s_u = LAPD_U_DISC;
2043 nctx.p_f = 1;
2044 nctx.length = 0;
2045 nctx.more = 0;
2046
2047 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2048 msgb_put(dl->tx_hist[0].msg, msg->len);
2049 if (msg->len)
2050 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2051 dl->tx_hist[0].more = 0;
2052 /* set Vs to 0, because it is used as index when resending DISC */
2053 dl->v_send = 0;
2054
2055 /* Set states */
2056 dl->own_busy = dl->peer_busy = 0;
2057 dl->retrans_ctr = 0;
2058 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2059
2060 /* Tramsmit and start T200 */
2061 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002062 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002063
2064 return 0;
2065}
2066
2067/* request release of link in idle state */
2068static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2069 struct lapd_msg_ctx *lctx)
2070{
2071 struct lapd_datalink *dl = lctx->dl;
2072 struct msgb *msg = dp->oph.msg;
2073
2074 msgb_free(msg);
2075
2076 /* send notification to L3 */
2077 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2078}
2079
2080/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002081static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002082 uint32_t states;
2083 int prim, op;
2084 const char *name;
2085 int (*rout) (struct osmo_dlsap_prim *dp,
2086 struct lapd_msg_ctx *lctx);
2087} l2downstatelist[] = {
2088 /* create and send UI command */
2089 {ALL_STATES,
2090 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2091 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2092
2093 /* create and send SABM command */
2094 {SBIT(LAPD_STATE_IDLE),
2095 PRIM_DL_EST, PRIM_OP_REQUEST,
2096 "DL-ESTABLISH-REQUEST", lapd_est_req},
2097
2098 /* create and send I command */
2099 {SBIT(LAPD_STATE_MF_EST) |
2100 SBIT(LAPD_STATE_TIMER_RECOV),
2101 PRIM_DL_DATA, PRIM_OP_REQUEST,
2102 "DL-DATA-REQUEST", lapd_data_req},
2103
2104 /* suspend datalink */
2105 {SBIT(LAPD_STATE_MF_EST) |
2106 SBIT(LAPD_STATE_TIMER_RECOV),
2107 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2108 "DL-SUSPEND-REQUEST", lapd_susp_req},
2109
2110 /* create and send SABM command (resume) */
2111 {SBIT(LAPD_STATE_MF_EST) |
2112 SBIT(LAPD_STATE_TIMER_RECOV),
2113 PRIM_DL_RES, PRIM_OP_REQUEST,
2114 "DL-RESUME-REQUEST", lapd_res_req},
2115
2116 /* create and send SABM command (reconnect) */
2117 {SBIT(LAPD_STATE_IDLE) |
2118 SBIT(LAPD_STATE_MF_EST) |
2119 SBIT(LAPD_STATE_TIMER_RECOV),
2120 PRIM_DL_RECON, PRIM_OP_REQUEST,
2121 "DL-RECONNECT-REQUEST", lapd_res_req},
2122
2123 /* create and send DISC command */
2124 {SBIT(LAPD_STATE_SABM_SENT) |
2125 SBIT(LAPD_STATE_MF_EST) |
2126 SBIT(LAPD_STATE_TIMER_RECOV) |
2127 SBIT(LAPD_STATE_DISC_SENT),
2128 PRIM_DL_REL, PRIM_OP_REQUEST,
2129 "DL-RELEASE-REQUEST", lapd_rel_req},
2130
2131 /* release in idle state */
2132 {SBIT(LAPD_STATE_IDLE),
2133 PRIM_DL_REL, PRIM_OP_REQUEST,
2134 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2135};
2136
2137#define L2DOWNSLLEN \
2138 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2139
2140int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2141{
2142 struct lapd_datalink *dl = lctx->dl;
2143 int i, supported = 0;
2144 struct msgb *msg = dp->oph.msg;
2145 int rc;
2146
2147 /* find function for current state and message */
2148 for (i = 0; i < L2DOWNSLLEN; i++) {
2149 if (dp->oph.primitive == l2downstatelist[i].prim
2150 && dp->oph.operation == l2downstatelist[i].op) {
2151 supported = 1;
2152 if ((SBIT(dl->state) & l2downstatelist[i].states))
2153 break;
2154 }
2155 }
2156 if (!supported) {
2157 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2158 dp->oph.primitive, dp->oph.operation);
2159 msgb_free(msg);
2160 return 0;
2161 }
2162 if (i == L2DOWNSLLEN) {
2163 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2164 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2165 lapd_state_names[dl->state]);
2166 msgb_free(msg);
2167 return 0;
2168 }
2169
2170 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2171 l2downstatelist[i].name, lapd_state_names[dl->state]);
2172
2173 rc = l2downstatelist[i].rout(dp, lctx);
2174
2175 return rc;
2176}
2177
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002178/*! @} */