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