blob: 34748a49f59af00c2d894b450e2e908f54c0dd27 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapd_core.c
2 * LAPD core implementation */
3/*
Harald Welte00b2faf2020-05-02 19:56:36 +02004 * (C) 2010-2020 by Harald Welte <laforge@gnumonks.org>
rootaf48bed2011-09-26 11:23:06 +02005 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
6 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
rootaf48bed2011-09-26 11:23:06 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
rootaf48bed2011-09-26 11:23:06 +020021 */
22
23/*! \addtogroup lapd
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 *
26 * Osmocom LAPD core, used for Q.921, LAPDm and others.
27 *
rootaf48bed2011-09-26 11:23:06 +020028 * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
29 *
30 * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
31 * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
32 * received while there is an incomplete rcv_buffer, it is appended to it.
33 *
34 * TX data is stored in the send_queue first. When transmitting a frame,
35 * the first message in the send_queue is moved to the send_buffer. There it
36 * resides until all fragments are acknowledged. Fragments to be sent by I
37 * frames are stored in the tx_hist buffer for resend, if required. Also the
38 * current fragment is copied into the tx_queue. There it resides until it is
39 * forwarded to layer 1.
40 *
41 * In case we have SAPI 0, we only have a window size of 1, so the unack-
42 * nowledged message resides always in the send_buffer. In case of a suspend,
43 * it can be written back to the first position of the send_queue.
44 *
45 * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
46 * asynchronous transfer between layer 1 and layer 2 (serial link), we must
47 * send a frame before layer 1 reaches the right timeslot to send it. So we
48 * move the tx_queue to layer 1 when there is not already a pending frame, and
49 * wait until acknowledge after the frame has been sent. If we receive an
50 * acknowledge, we can send the next frame from the buffer, if any.
51 *
52 * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
53 * will trigger next I frame, if possible.
54 *
55 * T203 is optional. It will be stated when entering MF EST state. It will also
56 * be started when I or S frame is received in that state . It will be
57 * restarted in the lapd_acknowledge() function, in case outstanding frames
58 * will not trigger T200. It will be stoped, when T200 is started in MF EST
59 * state. It will also be stoped when leaving MF EST state.
60 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020061 * \file lapd_core.c
rootaf48bed2011-09-26 11:23:06 +020062 */
63
64/* Enable this to test content resolution on network side:
65 * - The first SABM is received, UA is dropped.
66 * - The phone repeats SABM, but it's content is wrong, so it is ignored
67 * - The phone repeats SABM again, content is right, so UA is sent.
68 */
69//#define TEST_CONTENT_RESOLUTION_NETWORK
70
71#include <stdio.h>
Max327e5e92022-10-11 19:48:44 +030072#include <stdbool.h>
rootaf48bed2011-09-26 11:23:06 +020073#include <stdint.h>
74#include <string.h>
75#include <errno.h>
rootaf48bed2011-09-26 11:23:06 +020076
77#include <osmocom/core/logging.h>
78#include <osmocom/core/timer.h>
79#include <osmocom/core/msgb.h>
80#include <osmocom/core/utils.h>
81#include <osmocom/core/talloc.h>
Harald Welted55a2092022-11-29 22:33:54 +010082#include <osmocom/isdn/lapd_core.h>
Max2f0b0c92017-01-12 16:47:13 +010083#include <osmocom/gsm/rsl.h>
rootaf48bed2011-09-26 11:23:06 +020084
85/* TS 04.06 Table 4 / Section 3.8.1 */
86#define LAPD_U_SABM 0x7
87#define LAPD_U_SABME 0xf
88#define LAPD_U_DM 0x3
89#define LAPD_U_UI 0x0
90#define LAPD_U_DISC 0x8
91#define LAPD_U_UA 0xC
92#define LAPD_U_FRMR 0x11
93
94#define LAPD_S_RR 0x0
95#define LAPD_S_RNR 0x1
96#define LAPD_S_REJ 0x2
97
98#define CR_USER2NET_CMD 0
99#define CR_USER2NET_RESP 1
100#define CR_NET2USER_CMD 1
101#define CR_NET2USER_RESP 0
102
103#define LAPD_HEADROOM 56
Harald Welte8617d092020-07-03 19:28:53 +0200104#define LAPD_TAILROOM 16
rootaf48bed2011-09-26 11:23:06 +0200105
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);
Andreas Eversbergcc63aae2023-11-09 11:53:41 +0100111static int lapd_send_i(struct lapd_datalink *dl, int line, bool rts);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200112static 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;
Harald Welte8617d092020-07-03 19:28:53 +0200121 return msgb_alloc_headroom(length + LAPD_HEADROOM + LAPD_TAILROOM, LAPD_HEADROOM, name);
rootaf48bed2011-09-26 11:23:06 +0200122}
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 */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200153 msgb_free(dl->send_buffer);
154 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +0200155}
156
157static void lapd_dl_flush_hist(struct lapd_datalink *dl)
158{
159 unsigned int i;
160
Harald Weltef92e44c2016-08-01 00:24:19 +0200161 if (!dl->range_hist || !dl->tx_hist)
Harald Welte0ee90f82016-07-03 20:45:21 +0200162 return;
163
rootaf48bed2011-09-26 11:23:06 +0200164 for (i = 0; i < dl->range_hist; i++) {
165 if (dl->tx_hist[i].msg) {
166 msgb_free(dl->tx_hist[i].msg);
167 dl->tx_hist[i].msg = NULL;
168 }
169 }
170}
171
Andreas Eversberg913a7832023-11-09 12:03:04 +0100172static void lapd_dl_flush_tx_queue(struct lapd_datalink *dl)
rootaf48bed2011-09-26 11:23:06 +0200173{
174 struct msgb *msg;
175
176 while ((msg = msgb_dequeue(&dl->tx_queue)))
177 msgb_free(msg);
Andreas Eversberg913a7832023-11-09 12:03:04 +0100178}
179
180static void lapd_dl_flush_tx(struct lapd_datalink *dl)
181{
182 lapd_dl_flush_tx_queue(dl);
rootaf48bed2011-09-26 11:23:06 +0200183 lapd_dl_flush_hist(dl);
184}
185
186/* Figure B.2/Q.921 */
Harald Weltec733d142017-03-15 10:20:51 +0100187const struct value_string lapd_state_names[] = {
188 OSMO_VALUE_STRING(LAPD_STATE_NULL),
189 OSMO_VALUE_STRING(LAPD_STATE_TEI_UNASS),
190 OSMO_VALUE_STRING(LAPD_STATE_ASS_TEI_WAIT),
191 OSMO_VALUE_STRING(LAPD_STATE_EST_TEI_WAIT),
192 OSMO_VALUE_STRING(LAPD_STATE_IDLE),
193 OSMO_VALUE_STRING(LAPD_STATE_SABM_SENT),
194 OSMO_VALUE_STRING(LAPD_STATE_DISC_SENT),
195 OSMO_VALUE_STRING(LAPD_STATE_MF_EST),
196 OSMO_VALUE_STRING(LAPD_STATE_TIMER_RECOV),
197 { 0, NULL }
rootaf48bed2011-09-26 11:23:06 +0200198};
199
Harald Weltec733d142017-03-15 10:20:51 +0100200static inline const char *lapd_state_name(enum lapd_state state)
201{
202 return get_value_string(lapd_state_names, state);
203}
204
Andreas Eversberg742fc792011-09-27 09:40:25 +0200205static void lapd_start_t200(struct lapd_datalink *dl)
206{
Andreas Eversberg49b70872023-11-09 12:33:13 +0100207 if ((dl->lapd_flags & LAPD_F_RTS)) {
208 if (dl->t200_rts != LAPD_T200_RTS_OFF)
209 return;
210 LOGDL(dl, LOGL_INFO, "Start T200. (pending until triggered by RTS)\n");
211 dl->t200_rts = LAPD_T200_RTS_PENDING;
212 } else {
213 if (osmo_timer_pending(&dl->t200))
214 return;
215 LOGDL(dl, LOGL_INFO, "Start T200 (timeout=%d.%06ds).\n", dl->t200_sec, dl->t200_usec);
216 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
217 }
218}
219
220/*! Handle timeout condition of T200 in RTS mode.
221 * The caller (LAPDm code) implements the T200 timer and must detect timeout condition.
222 * The function gets called by LAPDm code when it detects a timeout of T200.
223 * \param[in] dl caller-allocated datalink structure */
224int lapd_t200_timeout(struct lapd_datalink *dl)
225{
226 OSMO_ASSERT((dl->lapd_flags & LAPD_F_RTS));
227
228 if (dl->t200_rts != LAPD_T200_RTS_RUNNING)
229 return -EINVAL;
230
231 dl->t200_rts = LAPD_T200_RTS_OFF;
232
233 lapd_t200_cb(dl);
234
235 return 0;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200236}
237
238static void lapd_start_t203(struct lapd_datalink *dl)
239{
240 if (osmo_timer_pending(&dl->t203))
241 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200242 LOGDL(dl, LOGL_INFO, "start T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200243 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
244}
245
246static void lapd_stop_t200(struct lapd_datalink *dl)
247{
Andreas Eversberg49b70872023-11-09 12:33:13 +0100248 if ((dl->lapd_flags & LAPD_F_RTS)) {
249 if (dl->t200_rts == LAPD_T200_RTS_OFF)
250 return;
251 dl->t200_rts = LAPD_T200_RTS_OFF;
252 } else {
253 if (!osmo_timer_pending(&dl->t200))
254 return;
255 osmo_timer_del(&dl->t200);
256 }
Harald Welte00b2faf2020-05-02 19:56:36 +0200257 LOGDL(dl, LOGL_INFO, "stop T200\n");
Andreas Eversberg49b70872023-11-09 12:33:13 +0100258}
259
260static bool lapd_is_t200_started(struct lapd_datalink *dl)
261{
262 if ((dl->lapd_flags & LAPD_F_RTS))
263 return (dl->t200_rts != LAPD_T200_RTS_OFF);
264 else
265 return osmo_timer_pending(&dl->t200);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200266}
267
268static void lapd_stop_t203(struct lapd_datalink *dl)
269{
270 if (!osmo_timer_pending(&dl->t203))
271 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200272 LOGDL(dl, LOGL_INFO, "stop T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200273 osmo_timer_del(&dl->t203);
274}
275
rootaf48bed2011-09-26 11:23:06 +0200276static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
277{
Harald Welte00b2faf2020-05-02 19:56:36 +0200278 LOGDL(dl, LOGL_INFO, "new state %s -> %s\n",
279 lapd_state_name(dl->state), lapd_state_name(state));
rootaf48bed2011-09-26 11:23:06 +0200280
281 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
282 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200283 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200284 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200285 msgb_free(dl->cont_res);
286 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200287 }
288
289 /* start T203 on entering MF EST state, if enabled */
290 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200291 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
292 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200293
294 dl->state = state;
295}
296
Harald Welte00b2faf2020-05-02 19:56:36 +0200297void *tall_lapd_ctx = NULL;
rootaf48bed2011-09-26 11:23:06 +0200298
Harald Welte00b2faf2020-05-02 19:56:36 +0200299/*! Initialize LAPD datalink instance and allocate history
300 * \param[in] dl caller-allocated datalink structure
301 * \param[in] k maximum number of unacknowledged frames
302 * \param[in] v_range range of sequence numbers
303 * \param[in] maxf maximum frame size (after defragmentation)
304 * \param[in] name human-readable name for this LAPD datalink */
305void lapd_dl_init2(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf,
306 const char *name)
rootaf48bed2011-09-26 11:23:06 +0200307{
308 int m;
309
310 memset(dl, 0, sizeof(*dl));
311 INIT_LLIST_HEAD(&dl->send_queue);
312 INIT_LLIST_HEAD(&dl->tx_queue);
313 dl->reestablish = 1;
314 dl->n200_est_rel = 3;
315 dl->n200 = 3;
316 dl->t200_sec = 1;
317 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200318 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200319 dl->t203_sec = 10;
320 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200321 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200322 dl->maxf = maxf;
323 if (k > v_range - 1)
324 k = v_range - 1;
325 dl->k = k;
326 dl->v_range = v_range;
327
328 /* Calculate modulo for history array:
329 * - The history range must be at least k+1.
330 * - The history range must be 2^x, where x is as low as possible.
331 */
332 k++;
333 for (m = 0x80; m; m >>= 1) {
334 if ((m & k)) {
335 if (k > m)
336 m <<= 1;
337 dl->range_hist = m;
338 break;
339 }
340 }
341
Harald Welte00b2faf2020-05-02 19:56:36 +0200342 if (!tall_lapd_ctx) {
343 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
344 OSMO_ASSERT(tall_lapd_ctx);
345 }
346
347 talloc_free(dl->name);
348 if (name)
349 dl->name = talloc_strdup(tall_lapd_ctx, name);
350 else
351 dl->name = talloc_asprintf(tall_lapd_ctx, "dl=%p", dl);
352
353 LOGDL(dl, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
354 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200355
356 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
357
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100358 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
359 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200360}
361
Harald Welte00b2faf2020-05-02 19:56:36 +0200362/*! Initialize LAPD datalink instance and allocate history
363 * \param[in] dl caller-allocated datalink structure
364 * \param[in] k maximum number of unacknowledged frames
365 * \param[in] v_range range of sequence numbers
366 * \param[in] maxf maximum frame size (after defragmentation) */
367void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf)
368{
369 lapd_dl_init2(dl, k, v_range, maxf, NULL);
370}
371
372void lapd_dl_set_name(struct lapd_datalink *dl, const char *name)
373{
374 if (!name)
375 return;
376 osmo_talloc_replace_string(tall_lapd_ctx, &dl->name, name);
377}
378
rootaf48bed2011-09-26 11:23:06 +0200379/* reset to IDLE state */
380void lapd_dl_reset(struct lapd_datalink *dl)
381{
Harald Welteef5b9b62020-06-07 22:29:53 +0200382 LOGDL(dl, LOGL_INFO, "Resetting LAPD instance\n");
Jean-Francois Dionne893979c2017-03-02 10:45:53 -0500383 /* enter idle state (and remove eventual cont_res) */
384 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200385 /* flush buffer */
386 lapd_dl_flush_tx(dl);
387 lapd_dl_flush_send(dl);
388 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200389 msgb_free(dl->rcv_buffer);
390 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200391 /* stop Timers */
392 lapd_stop_t200(dl);
393 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500394 if (dl->state == LAPD_STATE_IDLE)
395 return;
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500396 /* enter idle state (and remove eventual cont_res) */
397 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200398}
399
Andreas Eversberg49b70872023-11-09 12:33:13 +0100400/*! Set lapd_flags to change behaviour
401 * \param[in] dl \ref lapd_datalink instance
402 * \param[in] flags \ref lapd_flags */
403int lapd_dl_set_flags(struct lapd_datalink *dl, unsigned int flags)
404{
405 if (lapd_is_t200_started(dl) && (flags & LAPD_F_RTS) != (dl->lapd_flags & LAPD_F_RTS)) {
406 LOGDL(dl, LOGL_ERROR, "Changing RTS flag not allowed while T200 is running.\n");
407 return -EINVAL;
408 }
409
410 dl->lapd_flags = flags;
411
412 return 0;
413}
414
rootaf48bed2011-09-26 11:23:06 +0200415/* reset and de-allocate history buffer */
416void lapd_dl_exit(struct lapd_datalink *dl)
417{
418 /* free all ressources except history buffer */
419 lapd_dl_reset(dl);
Ivan Kluchnikovb9759db2017-05-11 15:19:23 +0300420
421 /* enter null state */
422 lapd_dl_newstate(dl, LAPD_STATE_NULL);
423
rootaf48bed2011-09-26 11:23:06 +0200424 /* free history buffer list */
425 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200426 dl->tx_hist = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200427 talloc_free(dl->name);
428 dl->name = NULL;
rootaf48bed2011-09-26 11:23:06 +0200429}
430
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200431/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200432int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
433{
434 switch (mode) {
435 case LAPD_MODE_USER:
436 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
437 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
438 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
439 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
440 break;
441 case LAPD_MODE_NETWORK:
442 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
443 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
444 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
445 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
446 break;
447 default:
448 return -EINVAL;
449 }
450 dl->mode = mode;
451
452 return 0;
453}
454
455/* send DL message with optional msgb */
456static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
457 struct msgb *msg)
458{
459 struct lapd_datalink *dl = lctx->dl;
460 struct osmo_dlsap_prim dp;
461
462 osmo_prim_init(&dp.oph, 0, prim, op, msg);
463 return dl->send_dlsap(&dp, lctx);
464}
465
466/* send simple DL message */
467static inline int send_dl_simple(uint8_t prim, uint8_t op,
468 struct lapd_msg_ctx *lctx)
469{
Pau Espin Pedrol9dd3bf02016-02-29 08:49:22 -0500470 return send_dl_l3(prim, op, lctx, NULL);
rootaf48bed2011-09-26 11:23:06 +0200471}
472
473/* send MDL-ERROR INDICATION */
474static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
475{
476 struct lapd_datalink *dl = lctx->dl;
477 struct osmo_dlsap_prim dp;
478
Harald Welte00b2faf2020-05-02 19:56:36 +0200479 LOGDL(dl, LOGL_NOTICE,
480 "sending MDL-ERROR-IND cause %d from state %s\n",
481 cause, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200482 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
483 dp.u.error_ind.cause = cause;
484 return dl->send_dlsap(&dp, lctx);
485}
486
487/* send UA response */
488static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
489{
490 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
491 struct lapd_msg_ctx nctx;
492 struct lapd_datalink *dl = lctx->dl;
493
494 memcpy(&nctx, lctx, sizeof(nctx));
495 msg->l3h = msgb_put(msg, len);
496 if (len)
497 memcpy(msg->l3h, data, len);
498 /* keep nctx.ldp */
499 /* keep nctx.sapi */
500 /* keep nctx.tei */
501 nctx.cr = dl->cr.loc2rem.resp;
502 nctx.format = LAPD_FORM_U;
503 nctx.s_u = LAPD_U_UA;
504 /* keep nctx.p_f */
505 nctx.length = len;
506 nctx.more = 0;
507
508 return dl->send_ph_data_req(&nctx, msg);
509}
510
511/* send DM response */
512static int lapd_send_dm(struct lapd_msg_ctx *lctx)
513{
514 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
515 struct lapd_msg_ctx nctx;
516 struct lapd_datalink *dl = lctx->dl;
517
518 memcpy(&nctx, lctx, sizeof(nctx));
519 /* keep nctx.ldp */
520 /* keep nctx.sapi */
521 /* keep nctx.tei */
522 nctx.cr = dl->cr.loc2rem.resp;
523 nctx.format = LAPD_FORM_U;
524 nctx.s_u = LAPD_U_DM;
525 /* keep nctx.p_f */
526 nctx.length = 0;
527 nctx.more = 0;
528
529 return dl->send_ph_data_req(&nctx, msg);
530}
531
532/* send RR response / command */
533static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
534{
535 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
536 struct lapd_msg_ctx nctx;
537 struct lapd_datalink *dl = lctx->dl;
538
539 memcpy(&nctx, lctx, sizeof(nctx));
540 /* keep nctx.ldp */
541 /* keep nctx.sapi */
542 /* keep nctx.tei */
543 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
544 nctx.format = LAPD_FORM_S;
545 nctx.s_u = LAPD_S_RR;
546 nctx.p_f = f_bit;
547 nctx.n_recv = dl->v_recv;
548 nctx.length = 0;
549 nctx.more = 0;
550
551 return dl->send_ph_data_req(&nctx, msg);
552}
553
554/* send RNR response / command */
555static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
556{
557 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
558 struct lapd_msg_ctx nctx;
559 struct lapd_datalink *dl = lctx->dl;
560
561 memcpy(&nctx, lctx, sizeof(nctx));
562 /* keep nctx.ldp */
563 /* keep nctx.sapi */
564 /* keep nctx.tei */
565 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
566 nctx.format = LAPD_FORM_S;
567 nctx.s_u = LAPD_S_RNR;
568 nctx.p_f = f_bit;
569 nctx.n_recv = dl->v_recv;
570 nctx.length = 0;
571 nctx.more = 0;
572
573 return dl->send_ph_data_req(&nctx, msg);
574}
575
576/* send REJ response */
577static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
578{
579 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
580 struct lapd_msg_ctx nctx;
581 struct lapd_datalink *dl = lctx->dl;
582
583 memcpy(&nctx, lctx, sizeof(nctx));
584 /* keep nctx.ldp */
585 /* keep nctx.sapi */
586 /* keep nctx.tei */
587 nctx.cr = dl->cr.loc2rem.resp;
588 nctx.format = LAPD_FORM_S;
589 nctx.s_u = LAPD_S_REJ;
590 nctx.p_f = f_bit;
591 nctx.n_recv = dl->v_recv;
592 nctx.length = 0;
593 nctx.more = 0;
594
595 return dl->send_ph_data_req(&nctx, msg);
596}
597
598/* resend SABM or DISC message */
599static int lapd_send_resend(struct lapd_datalink *dl)
600{
601 struct msgb *msg;
602 uint8_t h = do_mod(dl->v_send, dl->range_hist);
603 int length = dl->tx_hist[h].msg->len;
604 struct lapd_msg_ctx nctx;
605
606 /* assemble message */
607 memcpy(&nctx, &dl->lctx, sizeof(nctx));
608 /* keep nctx.ldp */
609 /* keep nctx.sapi */
610 /* keep nctx.tei */
611 nctx.cr = dl->cr.loc2rem.cmd;
612 nctx.format = LAPD_FORM_U;
613 if (dl->state == LAPD_STATE_SABM_SENT)
614 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
615 else
616 nctx.s_u = LAPD_U_DISC;
617 nctx.p_f = 1;
618 nctx.length = length;
619 nctx.more = 0;
620
621 /* Resend SABM/DISC from tx_hist */
622 msg = lapd_msgb_alloc(length, "LAPD resend");
623 msg->l3h = msgb_put(msg, length);
624 if (length)
625 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
626
627 return dl->send_ph_data_req(&nctx, msg);
628}
629
630/* reestablish link */
631static int lapd_reestablish(struct lapd_datalink *dl)
632{
633 struct osmo_dlsap_prim dp;
634 struct msgb *msg;
635
Harald Welte00b2faf2020-05-02 19:56:36 +0200636 LOGDL(dl, LOGL_DEBUG, "LAPD reestablish\n");
Philipp Maier08177d32016-12-08 17:23:26 +0100637
rootaf48bed2011-09-26 11:23:06 +0200638 msg = lapd_msgb_alloc(0, "DUMMY");
639 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100640
rootaf48bed2011-09-26 11:23:06 +0200641 return lapd_est_req(&dp, &dl->lctx);
642}
643
644/* Timer callback on T200 expiry */
645static void lapd_t200_cb(void *data)
646{
647 struct lapd_datalink *dl = data;
648
Harald Welte00b2faf2020-05-02 19:56:36 +0200649 LOGDL(dl, LOGL_INFO, "Timeout T200 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200650
651 switch (dl->state) {
652 case LAPD_STATE_SABM_SENT:
653 /* 5.4.1.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200654 /* increment re-transmission counter */
655 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200656 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200657 /* flush tx and send buffers */
658 lapd_dl_flush_tx(dl);
659 lapd_dl_flush_send(dl);
660 /* go back to idle state */
661 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
662 /* NOTE: we must not change any other states or buffers
663 * and queues, since we may reconnect after handover
664 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100665 /* send MDL ERROR INIDCATION to L3 */
666 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100667 /* send RELEASE INDICATION to L3 */
668 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
669 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200670 break;
671 }
672 /* retransmit SABM command */
673 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200674 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200675 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200676 break;
677 case LAPD_STATE_DISC_SENT:
678 /* 5.4.4.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200679 /* increment re-transmission counter */
680 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200681 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200682 /* send MDL ERROR INIDCATION to L3 */
683 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
684 /* flush tx and send buffers */
685 lapd_dl_flush_tx(dl);
686 lapd_dl_flush_send(dl);
687 /* go back to idle state */
688 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
689 /* NOTE: we must not change any other states or buffers
690 * and queues, since we may reconnect after handover
691 * failure. the buffered messages is replaced there */
Harald Welted2a61172020-12-21 17:43:54 +0100692 /* send RELEASE INDICATION to L3 */
693 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200694 break;
695 }
696 /* retransmit DISC command */
697 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200698 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200699 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200700 break;
701 case LAPD_STATE_MF_EST:
702 /* 5.5.7 */
703 dl->retrans_ctr = 0;
704 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
705 /* fall through */
706 case LAPD_STATE_TIMER_RECOV:
707 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200708 if (dl->retrans_ctr <= dl->n200) {
rootaf48bed2011-09-26 11:23:06 +0200709 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
710 uint8_t h = do_mod(vs, dl->range_hist);
711 /* retransmit I frame (V_s-1) with P=1, if any */
712 if (dl->tx_hist[h].msg) {
713 struct msgb *msg;
714 int length = dl->tx_hist[h].msg->len;
715 struct lapd_msg_ctx nctx;
716
Harald Welte00b2faf2020-05-02 19:56:36 +0200717 LOGDL(dl, LOGL_INFO, "retransmit last frame V(S)=%d\n", vs);
rootaf48bed2011-09-26 11:23:06 +0200718 /* Create I frame (segment) from tx_hist */
719 memcpy(&nctx, &dl->lctx, sizeof(nctx));
720 /* keep nctx.ldp */
721 /* keep nctx.sapi */
722 /* keep nctx.tei */
723 nctx.cr = dl->cr.loc2rem.cmd;
724 nctx.format = LAPD_FORM_I;
725 nctx.p_f = 1;
726 nctx.n_send = vs;
727 nctx.n_recv = dl->v_recv;
728 nctx.length = length;
729 nctx.more = dl->tx_hist[h].more;
730 msg = lapd_msgb_alloc(length, "LAPD I resend");
731 msg->l3h = msgb_put(msg, length);
732 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
733 length);
734 dl->send_ph_data_req(&nctx, msg);
735 } else {
736 /* OR send appropriate supervision frame with P=1 */
737 if (!dl->own_busy && !dl->seq_err_cond) {
738 lapd_send_rr(&dl->lctx, 1, 1);
739 /* NOTE: In case of sequence error
740 * condition, the REJ frame has been
741 * transmitted when entering the
742 * condition, so it has not be done
743 * here
744 */
745 } else if (dl->own_busy) {
746 lapd_send_rnr(&dl->lctx, 1, 1);
747 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200748 LOGDL(dl, LOGL_INFO, "unhandled, pls. fix\n");
rootaf48bed2011-09-26 11:23:06 +0200749 }
750 }
751 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200752 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200753 } else {
754 /* send MDL ERROR INIDCATION to L3 */
755 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
756 /* reestablish */
757 if (!dl->reestablish)
758 break;
Harald Welte00b2faf2020-05-02 19:56:36 +0200759 LOGDL(dl, LOGL_NOTICE, "N200+1 reached, performingreestablishment\n");
rootaf48bed2011-09-26 11:23:06 +0200760 lapd_reestablish(dl);
761 }
762 break;
763 default:
Harald Welte00b2faf2020-05-02 19:56:36 +0200764 LOGDL(dl, LOGL_INFO, "T200 expired in unexpected dl->state %s)\n",
765 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200766 }
767}
768
769/* Timer callback on T203 expiry */
770static void lapd_t203_cb(void *data)
771{
772 struct lapd_datalink *dl = data;
773
Harald Welte00b2faf2020-05-02 19:56:36 +0200774 LOGDL(dl, LOGL_INFO, "Timeout T203 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200775
776 if (dl->state != LAPD_STATE_MF_EST) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200777 LOGDL(dl, LOGL_ERROR, "T203 fired outside MF EST state, please fix!\n");
rootaf48bed2011-09-26 11:23:06 +0200778 return;
779 }
780
781 /* set retransmission counter to 0 */
782 dl->retrans_ctr = 0;
783 /* enter timer recovery state */
784 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
785 /* transmit a supervisory command with P bit set to 1 as follows: */
786 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200787 LOGDL(dl, LOGL_INFO, "transmit an RR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200788 /* Send RR with P=1 */
789 lapd_send_rr(&dl->lctx, 1, 1);
790 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200791 LOGDL(dl, LOGL_INFO, "transmit an RNR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200792 /* Send RNR with P=1 */
793 lapd_send_rnr(&dl->lctx, 1, 1);
794 }
795 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200796 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200797}
798
799/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
800static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
801{
802 struct lapd_datalink *dl = lctx->dl;
803 uint8_t nr = lctx->n_recv;
Max327e5e92022-10-11 19:48:44 +0300804 int s = 0, rej = 0;
805 bool t200_reset = false;
rootaf48bed2011-09-26 11:23:06 +0200806 int i, h;
807
808 /* supervisory frame ? */
809 if (lctx->format == LAPD_FORM_S)
810 s = 1;
811 /* REJ frame ? */
812 if (s && lctx->s_u == LAPD_S_REJ)
813 rej = 1;
814
815 /* Flush all transmit buffers of acknowledged frames */
816 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
817 h = do_mod(i, dl->range_hist);
818 if (dl->tx_hist[h].msg) {
819 msgb_free(dl->tx_hist[h].msg);
820 dl->tx_hist[h].msg = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200821 LOGDL(dl, LOGL_INFO, "ack frame %d\n", i);
rootaf48bed2011-09-26 11:23:06 +0200822 }
823 }
824
825 if (dl->state != LAPD_STATE_TIMER_RECOV) {
826 /* When not in the timer recovery condition, the data
827 * link layer entity shall reset the timer T200 on
828 * receipt of a valid I frame with N(R) higher than V(A),
829 * or an REJ with an N(R) equal to V(A). */
Max68588c52022-10-11 19:34:43 +0300830 if ((!rej && nr != dl->v_ack) || (rej && nr == dl->v_ack)) {
Max327e5e92022-10-11 19:48:44 +0300831 t200_reset = true;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200832 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200833 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
834 }
835 /* 5.7.4: N(R) sequence error
836 * N(R) is called valid, if and only if
837 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
838 */
Max68588c52022-10-11 19:34:43 +0300839 if (sub_mod(nr, dl->v_ack, dl->v_range) > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200840 LOGDL(dl, LOGL_NOTICE, "N(R) sequence error\n");
rootaf48bed2011-09-26 11:23:06 +0200841 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
842 }
843 }
844
845 /* V(A) shall be set to the value of N(R) */
846 dl->v_ack = nr;
847
Andreas Eversberg742fc792011-09-27 09:40:25 +0200848 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200849 * and if there are outstanding I frames, restart T200 */
850 if (t200_reset && !rej) {
851 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200852 LOGDL(dl, LOGL_INFO, "start T200, due to unacked I frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200853 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200854 }
855 }
856
857 /* This also does a restart, when I or S frame is received */
858
859 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200860 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200861 /* Start T203, if T200 is not running in MF EST state, if enabled */
Andreas Eversberg49b70872023-11-09 12:33:13 +0100862 if (!lapd_is_t200_started(dl) && (dl->t203_sec || dl->t203_usec) && (dl->state == LAPD_STATE_MF_EST))
Andreas Eversberg742fc792011-09-27 09:40:25 +0200863 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200864}
865
866/* L1 -> L2 */
867
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200868/* Receive a LAPD U SABM(E) message from L1 */
869static int lapd_rx_u_sabm(struct msgb *msg, struct lapd_msg_ctx *lctx)
rootaf48bed2011-09-26 11:23:06 +0200870{
871 struct lapd_datalink *dl = lctx->dl;
872 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100873 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200874 uint8_t prim, op;
875
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200876 prim = PRIM_DL_EST;
877 op = PRIM_OP_INDICATION;
rootaf48bed2011-09-26 11:23:06 +0200878
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200879 LOGDL(dl, LOGL_INFO, "SABM(E) received in state %s\n", lapd_state_name(dl->state));
880 /* 5.7.1 */
881 dl->seq_err_cond = 0;
882 /* G.2.2 Wrong value of the C/R bit */
883 if (lctx->cr == dl->cr.rem2loc.resp) {
884 LOGDL(dl, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200885 msgb_free(msg);
886 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
887 return -EINVAL;
888 }
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200889
890 /* G.4.5 If SABM is received with L>N201 or with M bit
891 * set, AN MDL-ERROR-INDICATION is sent to MM.
892 */
893 if (lctx->more || length > lctx->n201) {
894 LOGDL(dl, LOGL_ERROR, "SABM too large error\n");
895 msgb_free(msg);
896 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
897 return -EIO;
898 }
899
900 switch (dl->state) {
901 case LAPD_STATE_IDLE:
902 break;
Pau Espin Pedrol76190d32020-10-21 13:05:44 +0200903 case LAPD_STATE_TIMER_RECOV:
904 LOGDL(dl, LOGL_INFO, "SABM command, timer recovery state\n");
905 /* If link is lost on the remote side, we start over
906 * and send DL-ESTABLISH indication again. */
907 /* 3GPP TS 44.006 8.6.3 "Procedures for re-establishment" */
908 if (length) {
909 /* check for contention resoultion */
910 LOGDL(dl, LOGL_ERROR, "SABM L>0 not expected in timer "
911 "recovery state\n");
912 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
913 lapd_send_dm(lctx);
914 msgb_free(msg);
915 return 0;
916 }
917 /* re-establishment, continue below */
918 lapd_stop_t200(dl);
919 break;
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200920 case LAPD_STATE_MF_EST:
921 LOGDL(dl, LOGL_INFO, "SABM command, multiple frame established state\n");
922 /* If link is lost on the remote side, we start over
923 * and send DL-ESTABLISH indication again. */
924 /* Additionally, continue in case of content resoltion
925 * (GSM network). This happens, if the mobile has not
926 * yet received UA or another mobile (collision) tries
927 * to establish connection. The mobile must receive
928 * UA again. */
929 /* 5.4.2.1 */
930 if (!length) {
931 /* If no content resolution, this is a
932 * re-establishment. */
933 LOGDL(dl, LOGL_INFO, "Remote reestablish\n");
934 break;
935 }
936 if (!dl->cont_res) {
937 LOGDL(dl, LOGL_INFO, "SABM command not allowed in state %s\n",
938 lapd_state_name(dl->state));
939 mdl_error(MDL_CAUSE_SABM_MF, lctx);
940 msgb_free(msg);
941 return 0;
942 }
943 /* Ignore SABM if content differs from first SABM. */
944 if (dl->mode == LAPD_MODE_NETWORK && length) {
945#ifdef TEST_CONTENT_RESOLUTION_NETWORK
946 dl->cont_res->data[0] ^= 0x01;
947#endif
948 if (memcmp(dl->cont_res->data, msg->data,
949 length)) {
950 LOGDL(dl, LOGL_INFO, "Another SABM with different content - "
951 "ignoring!\n");
952 msgb_free(msg);
953 return 0;
954 }
955 }
956 /* send UA again */
957 lapd_send_ua(lctx, length, msg->l3h);
958 msgb_free(msg);
959 return 0;
960 case LAPD_STATE_DISC_SENT:
961 /* 5.4.6.2 send DM with F=P */
962 lapd_send_dm(lctx);
963 /* stop Timer T200 */
964 lapd_stop_t200(dl);
965 msgb_free(msg);
966 return send_dl_simple(prim, op, lctx);
967 default:
968 /* collision: Send UA, but still wait for rx UA, then
969 * change to MF_EST state.
970 */
971 /* check for contention resoultion */
972 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
973 LOGDL(dl, LOGL_NOTICE, "SABM not allowed during contention "
974 "resolution (state=%s)\n", lapd_state_name(dl->state));
975 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
976 }
977 lapd_send_ua(lctx, length, msg->l3h);
978 msgb_free(msg);
979 return 0;
980 }
981 /* save message context for further use */
982 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
983#ifndef TEST_CONTENT_RESOLUTION_NETWORK
984 /* send UA response */
985 lapd_send_ua(lctx, length, msg->l3h);
986#endif
987 /* set Vs, Vr and Va to 0 */
988 dl->v_send = dl->v_recv = dl->v_ack = 0;
989 /* clear tx_hist */
990 lapd_dl_flush_hist(dl);
991 /* enter multiple-frame-established state */
992 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
993 /* store content resolution data on network side
994 * Note: cont_res will be removed when changing state again,
995 * so it must be allocated AFTER lapd_dl_newstate(). */
996 if (dl->mode == LAPD_MODE_NETWORK && length) {
997 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
998 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
999 length);
Pau Espin Pedrol28b404f2022-03-02 19:03:32 +01001000 LOGDL(dl, LOGL_INFO, "Store content res.\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001001 }
1002 /* send notification to L3 */
1003 if (length == 0) {
1004 /* 5.4.1.2 Normal establishment procedures */
1005 rc = send_dl_simple(prim, op, lctx);
1006 msgb_free(msg);
1007 } else {
1008 /* 5.4.1.4 Contention resolution establishment */
1009 msgb_trim(msg, length);
1010 rc = send_dl_l3(prim, op, lctx, msg);
1011 }
rootaf48bed2011-09-26 11:23:06 +02001012 return rc;
1013}
1014
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001015/* Receive a LAPD U DM message from L1 */
1016static int lapd_rx_u_dm(struct msgb *msg, struct lapd_msg_ctx *lctx)
1017{
1018 struct lapd_datalink *dl = lctx->dl;
1019 int rc = 0;
1020
1021 LOGDL(dl, LOGL_INFO, "DM received in state %s\n", lapd_state_name(dl->state));
1022 /* G.2.2 Wrong value of the C/R bit */
1023 if (lctx->cr == dl->cr.rem2loc.cmd) {
1024 LOGDL(dl, LOGL_ERROR, "DM command error\n");
1025 msgb_free(msg);
1026 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1027 return -EINVAL;
1028 }
1029 if (!lctx->p_f) {
1030 /* 5.4.1.2 DM responses with the F bit set to "0"
1031 * shall be ignored.
1032 */
1033 msgb_free(msg);
1034 return 0;
1035 }
1036 switch (dl->state) {
1037 case LAPD_STATE_SABM_SENT:
1038 break;
1039 case LAPD_STATE_MF_EST:
1040 if (lctx->p_f) {
1041 LOGDL(dl, LOGL_INFO, "unsolicited DM response\n");
1042 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
1043 } else {
1044 LOGDL(dl, LOGL_INFO, "unsolicited DM response, "
1045 "multiple frame established state\n");
1046 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1047 /* reestablish */
1048 if (!dl->reestablish) {
1049 msgb_free(msg);
1050 return 0;
1051 }
1052 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1053 lapd_reestablish(dl);
1054 }
1055 msgb_free(msg);
1056 return 0;
1057 case LAPD_STATE_TIMER_RECOV:
1058 /* FP = 0 (DM is normal in case PF = 1) */
1059 if (!lctx->p_f) {
1060 LOGDL(dl, LOGL_INFO, "unsolicited DM response, multiple frame "
1061 "established state\n");
1062 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1063 msgb_free(msg);
1064 /* reestablish */
1065 if (!dl->reestablish)
1066 return 0;
1067 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1068 return lapd_reestablish(dl);
1069 }
1070 break;
1071 case LAPD_STATE_DISC_SENT:
1072 /* stop Timer T200 */
1073 lapd_stop_t200(dl);
1074 /* go to idle state */
1075 lapd_dl_flush_tx(dl);
1076 lapd_dl_flush_send(dl);
1077 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1078 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1079 msgb_free(msg);
1080 return 0;
1081 case LAPD_STATE_IDLE:
1082 /* 5.4.5 all other frame types shall be discarded */
1083 default:
1084 LOGDL(dl, LOGL_INFO, "unsolicited DM response! (discarding)\n");
1085 msgb_free(msg);
1086 return 0;
1087 }
1088 /* stop timer T200 */
1089 lapd_stop_t200(dl);
1090 /* go to idle state */
1091 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1092 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1093 msgb_free(msg);
1094 return rc;
1095}
1096
1097/* Receive a LAPD U UI message from L1 */
1098static int lapd_rx_u_ui(struct msgb *msg, struct lapd_msg_ctx *lctx)
1099{
1100 struct lapd_datalink *dl = lctx->dl;
1101 int length = lctx->length;
1102
1103 LOGDL(dl, LOGL_INFO, "UI received\n");
1104 /* G.2.2 Wrong value of the C/R bit */
1105 if (lctx->cr == dl->cr.rem2loc.resp) {
1106 LOGDL(dl, LOGL_ERROR, "UI indicates response error\n");
1107 msgb_free(msg);
1108 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1109 return -EINVAL;
1110 }
1111
1112 /* G.4.5 If UI is received with L>N201 or with M bit
1113 * set, AN MDL-ERROR-INDICATION is sent to MM.
1114 */
1115 if (length > lctx->n201 || lctx->more) {
1116 LOGDL(dl, LOGL_ERROR, "UI too large error (%d > N201(%d) or M=%d)\n",
1117 length, lctx->n201, lctx->more);
1118 msgb_free(msg);
1119 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1120 return -EIO;
1121 }
1122
1123 /* do some length checks */
1124 if (length == 0) {
1125 /* 5.3.3 UI frames received with the length indicator
1126 * set to "0" shall be ignored
1127 */
1128 LOGDL(dl, LOGL_INFO, "length=0 (discarding)\n");
1129 msgb_free(msg);
1130 return 0;
1131 }
1132 msgb_trim(msg, length);
1133 return send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx, msg);
1134}
1135
1136/* Receive a LAPD U DISC message from L1 */
1137static int lapd_rx_u_disc(struct msgb *msg, struct lapd_msg_ctx *lctx)
1138{
1139 struct lapd_datalink *dl = lctx->dl;
1140 int length = lctx->length;
1141 int rc = 0;
1142 uint8_t prim, op;
1143
1144 prim = PRIM_DL_REL;
1145 op = PRIM_OP_INDICATION;
1146
1147 LOGDL(dl, LOGL_INFO, "DISC received in state %s\n", lapd_state_name(dl->state));
1148 /* flush tx and send buffers */
1149 lapd_dl_flush_tx(dl);
1150 lapd_dl_flush_send(dl);
1151 /* 5.7.1 */
1152 dl->seq_err_cond = 0;
1153 /* G.2.2 Wrong value of the C/R bit */
1154 if (lctx->cr == dl->cr.rem2loc.resp) {
1155 LOGDL(dl, LOGL_ERROR, "DISC response error\n");
1156 msgb_free(msg);
1157 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1158 return -EINVAL;
1159 }
1160 if (length > 0 || lctx->more) {
1161 /* G.4.4 If a DISC or DM frame is received with L>0 or
1162 * with the M bit set to "1", an MDL-ERROR-INDICATION
1163 * primitive with cause "U frame with incorrect
1164 * parameters" is sent to the mobile management entity.
1165 */
1166 LOGDL(dl, LOGL_ERROR, "U frame iwth incorrect parameters\n");
1167 msgb_free(msg);
1168 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1169 return -EIO;
1170 }
1171 switch (dl->state) {
1172 case LAPD_STATE_IDLE:
1173 LOGDL(dl, LOGL_INFO, "DISC in idle state\n");
1174 /* send DM with F=P */
1175 msgb_free(msg);
1176 return lapd_send_dm(lctx);
1177 case LAPD_STATE_SABM_SENT:
1178 LOGDL(dl, LOGL_INFO, "DISC in SABM state\n");
1179 /* 5.4.6.2 send DM with F=P */
1180 lapd_send_dm(lctx);
1181 /* stop Timer T200 */
1182 lapd_stop_t200(dl);
1183 /* go to idle state */
1184 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1185 msgb_free(msg);
1186 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1187 lctx);
1188 case LAPD_STATE_MF_EST:
1189 case LAPD_STATE_TIMER_RECOV:
1190 LOGDL(dl, LOGL_INFO, "DISC in est state\n");
1191 break;
1192 case LAPD_STATE_DISC_SENT:
1193 LOGDL(dl, LOGL_INFO, "DISC in disc state\n");
1194 prim = PRIM_DL_REL;
1195 op = PRIM_OP_CONFIRM;
1196 break;
1197 default:
1198 lapd_send_ua(lctx, length, msg->l3h);
1199 msgb_free(msg);
1200 return 0;
1201 }
1202 /* send UA response */
1203 lapd_send_ua(lctx, length, msg->l3h);
1204 /* stop Timer T200 */
1205 lapd_stop_t200(dl);
1206 /* enter idle state, keep tx-buffer with UA response */
1207 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1208 /* send notification to L3 */
1209 rc = send_dl_simple(prim, op, lctx);
1210 msgb_free(msg);
1211 return rc;
1212}
1213
1214/* Receive a LAPD U UA message from L1 */
1215static int lapd_rx_u_ua(struct msgb *msg, struct lapd_msg_ctx *lctx)
1216{
1217 struct lapd_datalink *dl = lctx->dl;
1218 int length = lctx->length;
1219 int rc = 0;
1220
1221 LOGDL(dl, LOGL_INFO, "UA received in state %s\n", lapd_state_name(dl->state));
1222 /* G.2.2 Wrong value of the C/R bit */
1223 if (lctx->cr == dl->cr.rem2loc.cmd) {
1224 LOGDL(dl, LOGL_ERROR, "UA indicates command error\n");
1225 msgb_free(msg);
1226 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1227 return -EINVAL;
1228 }
1229
1230 /* G.4.5 If UA is received with L>N201 or with M bit
1231 * set, AN MDL-ERROR-INDICATION is sent to MM.
1232 */
1233 if (lctx->more || length > lctx->n201) {
1234 LOGDL(dl, LOGL_ERROR, "UA too large error\n");
1235 msgb_free(msg);
1236 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1237 return -EIO;
1238 }
1239
1240 if (!lctx->p_f) {
1241 /* 5.4.1.2 A UA response with the F bit set to "0"
1242 * shall be ignored.
1243 */
1244 LOGDL(dl, LOGL_INFO, "F=0 (discarding)\n");
1245 msgb_free(msg);
1246 return 0;
1247 }
1248 switch (dl->state) {
1249 case LAPD_STATE_SABM_SENT:
1250 break;
1251 case LAPD_STATE_MF_EST:
1252 case LAPD_STATE_TIMER_RECOV:
1253 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1254 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1255 msgb_free(msg);
1256 return 0;
1257 case LAPD_STATE_DISC_SENT:
1258 LOGDL(dl, LOGL_INFO, "UA in disconnect state\n");
1259 /* stop Timer T200 */
1260 lapd_stop_t200(dl);
1261 /* go to idle state */
1262 lapd_dl_flush_tx(dl);
1263 lapd_dl_flush_send(dl);
1264 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1265 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1266 msgb_free(msg);
1267 return 0;
1268 case LAPD_STATE_IDLE:
1269 /* 5.4.5 all other frame types shall be discarded */
1270 default:
1271 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1272 msgb_free(msg);
1273 return 0;
1274 }
1275 LOGDL(dl, LOGL_INFO, "UA in SABM state\n");
1276 /* stop Timer T200 */
1277 lapd_stop_t200(dl);
1278 /* compare UA with SABME if contention resolution is applied */
1279 if (dl->tx_hist[0].msg->len) {
1280 if (length != (dl->tx_hist[0].msg->len)
1281 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1282 length)) {
1283 LOGDL(dl, LOGL_INFO, "**** UA response mismatches ****\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001284 /* go to idle state */
1285 lapd_dl_flush_tx(dl);
1286 lapd_dl_flush_send(dl);
1287 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
Harald Welted2a61172020-12-21 17:43:54 +01001288 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1289 msgb_free(msg);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001290 return 0;
1291 }
1292 }
1293 /* set Vs, Vr and Va to 0 */
1294 dl->v_send = dl->v_recv = dl->v_ack = 0;
1295 /* clear tx_hist */
1296 lapd_dl_flush_hist(dl);
1297 /* enter multiple-frame-established state */
1298 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1299 /* send outstanding frames, if any (resume / reconnect) */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001300 lapd_send_i(dl, __LINE__, false);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001301 /* send notification to L3 */
1302 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1303 msgb_free(msg);
1304 return rc;
1305}
1306
1307/* Receive a LAPD U FRMR message from L1 */
1308static int lapd_rx_u_frmr(struct msgb *msg, struct lapd_msg_ctx *lctx)
1309{
1310 struct lapd_datalink *dl = lctx->dl;
1311
1312 LOGDL(dl, LOGL_NOTICE, "Frame reject received\n");
1313 /* send MDL ERROR INIDCATION to L3 */
1314 mdl_error(MDL_CAUSE_FRMR, lctx);
1315 msgb_free(msg);
1316 /* reestablish */
1317 if (!dl->reestablish)
1318 return 0;
1319 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1320 return lapd_reestablish(dl);
1321}
1322
1323/* Receive a LAPD U (Unnumbered) message from L1 */
1324static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
1325{
1326 switch (lctx->s_u) {
1327 case LAPD_U_SABM:
1328 case LAPD_U_SABME:
1329 return lapd_rx_u_sabm(msg, lctx);
1330 case LAPD_U_DM:
1331 return lapd_rx_u_dm(msg, lctx);
1332 case LAPD_U_UI:
1333 return lapd_rx_u_ui(msg, lctx);
1334 case LAPD_U_DISC:
1335 return lapd_rx_u_disc(msg, lctx);
1336 case LAPD_U_UA:
1337 return lapd_rx_u_ua(msg, lctx);
1338 case LAPD_U_FRMR:
1339 return lapd_rx_u_frmr(msg, lctx);
1340 default:
1341 /* G.3.1 */
1342 LOGDL(lctx->dl, LOGL_NOTICE, "Unnumbered frame not allowed\n");
1343 msgb_free(msg);
1344 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1345 return -EINVAL;
1346 }
1347}
1348
rootaf48bed2011-09-26 11:23:06 +02001349/* Receive a LAPD S (Supervisory) message from L1 */
1350static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1351{
1352 struct lapd_datalink *dl = lctx->dl;
1353 int length = lctx->length;
1354
1355 if (length > 0 || lctx->more) {
1356 /* G.4.3 If a supervisory frame is received with L>0 or
1357 * with the M bit set to "1", an MDL-ERROR-INDICATION
1358 * primitive with cause "S frame with incorrect
1359 * parameters" is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001360 LOGDL(dl, LOGL_ERROR, "S frame with incorrect parameters\n");
rootaf48bed2011-09-26 11:23:06 +02001361 msgb_free(msg);
1362 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1363 return -EIO;
1364 }
1365
1366 if (lctx->cr == dl->cr.rem2loc.resp
1367 && lctx->p_f
1368 && dl->state != LAPD_STATE_TIMER_RECOV) {
1369 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001370 LOGDL(dl, LOGL_NOTICE, "S frame response with F=1 error\n");
rootaf48bed2011-09-26 11:23:06 +02001371 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1372 }
1373
1374 switch (dl->state) {
1375 case LAPD_STATE_IDLE:
1376 /* if P=1, respond DM with F=1 (5.2.2) */
1377 /* 5.4.5 all other frame types shall be discarded */
1378 if (lctx->p_f)
1379 lapd_send_dm(lctx); /* F=P */
1380 /* fall though */
1381 case LAPD_STATE_SABM_SENT:
1382 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001383 LOGDL(dl, LOGL_NOTICE, "S frame ignored in this state\n");
rootaf48bed2011-09-26 11:23:06 +02001384 msgb_free(msg);
1385 return 0;
1386 }
1387 switch (lctx->s_u) {
1388 case LAPD_S_RR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001389 LOGDL(dl, LOGL_INFO, "RR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001390 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1391 lapd_acknowledge(lctx);
1392
1393 /* 5.5.3.2 */
1394 if (lctx->cr == dl->cr.rem2loc.cmd
1395 && lctx->p_f) {
1396 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001397 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1398 "we are not busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001399 lapd_send_rr(lctx, 1, 0);
1400 /* NOTE: In case of sequence error condition,
1401 * the REJ frame has been transmitted when
1402 * entering the condition, so it has not be
1403 * done here
1404 */
1405 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001406 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1407 "we are busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001408 lapd_send_rnr(lctx, 1, 0);
1409 }
1410 } else if (lctx->cr == dl->cr.rem2loc.resp
1411 && lctx->p_f
1412 && dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001413 LOGDL(dl, LOGL_INFO, "RR response with F==1, and we are in timer recovery "
1414 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001415 /* V(S) to the N(R) in the RR frame */
1416 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001417 /* stop Timer T200 */
1418 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001419 /* 5.5.7 Clear timer recovery condition */
1420 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1421 }
1422 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001423 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001424
1425 break;
1426 case LAPD_S_RNR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001427 LOGDL(dl, LOGL_INFO, "RNR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001428 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1429 lapd_acknowledge(lctx);
1430
1431 /* 5.5.5 */
1432 /* Set peer receiver busy condition */
1433 dl->peer_busy = 1;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001434 /* Flush pending messages in TX queue. */
1435 lapd_dl_flush_tx_queue(dl);
1436 /* stop Timer T200 */
1437 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001438
1439 if (lctx->p_f) {
1440 if (lctx->cr == dl->cr.rem2loc.cmd) {
1441 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001442 LOGDL(dl, LOGL_INFO, "RNR poll command and we are not busy, "
1443 "so we reply with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001444 /* Send RR with F=1 */
1445 lapd_send_rr(lctx, 1, 0);
1446 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001447 LOGDL(dl, LOGL_INFO, "RNR poll command and we are busy, so "
1448 "we reply with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001449 /* Send RNR with F=1 */
1450 lapd_send_rnr(lctx, 1, 0);
1451 }
1452 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001453 LOGDL(dl, LOGL_INFO, "RNR poll response and we in timer recovery "
1454 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001455 /* 5.5.7 Clear timer recovery condition */
1456 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1457 /* V(S) to the N(R) in the RNR frame */
1458 dl->v_send = lctx->n_recv;
1459 }
1460 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001461 LOGDL(dl, LOGL_INFO, "RNR not polling/final state received\n");
rootaf48bed2011-09-26 11:23:06 +02001462
1463 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001464 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001465
1466 break;
1467 case LAPD_S_REJ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001468 LOGDL(dl, LOGL_INFO, "REJ received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001469 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1470 lapd_acknowledge(lctx);
1471
1472 /* 5.5.4.1 */
1473 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1474 /* Clear an existing peer receiver busy condition */
1475 dl->peer_busy = 0;
1476 /* V(S) and V(A) to the N(R) in the REJ frame */
1477 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001478 /* Flush pending messages in TX queue. */
1479 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001480 /* stop Timer T200 */
1481 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001482 /* 5.5.3.2 */
1483 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1484 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001485 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1486 "state and not in own busy condition received, so we "
1487 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001488 lapd_send_rr(lctx, 1, 0);
1489 /* NOTE: In case of sequence error
1490 * condition, the REJ frame has been
1491 * transmitted when entering the
1492 * condition, so it has not be done
1493 * here
1494 */
1495 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001496 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1497 "state and in own busy condition received, so we "
1498 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001499 lapd_send_rnr(lctx, 1, 0);
1500 }
1501 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001502 LOGDL(dl, LOGL_INFO, "REJ response or not polling command not "
1503 "in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001504 /* send MDL ERROR INIDCATION to L3 */
1505 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001506 LOGDL(dl, LOGL_ERROR, "unsolicited supervisory response!\n");
rootaf48bed2011-09-26 11:23:06 +02001507 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1508 }
1509
1510 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001511 LOGDL(dl, LOGL_INFO, "REJ poll response in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001512 /* Clear an existing peer receiver busy condition */
1513 dl->peer_busy = 0;
1514 /* V(S) and V(A) to the N(R) in the REJ frame */
1515 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001516 /* Flush pending messages in TX queue. */
1517 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001518 /* stop Timer T200 */
1519 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001520 /* 5.5.7 Clear timer recovery condition */
1521 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1522 } else {
1523 /* Clear an existing peer receiver busy condition */
1524 dl->peer_busy = 0;
1525 /* V(S) and V(A) to the N(R) in the REJ frame */
1526 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001527 /* Flush pending messages in TX queue. */
1528 lapd_dl_flush_tx_queue(dl);
rootaf48bed2011-09-26 11:23:06 +02001529 /* 5.5.3.2 */
1530 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1531 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001532 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1533 "state and not in own busy condition received, so we "
1534 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001535 lapd_send_rr(lctx, 1, 0);
1536 /* NOTE: In case of sequence error
1537 * condition, the REJ frame has been
1538 * transmitted when entering the
1539 * condition, so it has not be done
1540 * here
1541 */
1542 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001543 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1544 "state and in own busy condition received, so we "
1545 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001546 lapd_send_rnr(lctx, 1, 0);
1547 }
1548 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001549 LOGDL(dl, LOGL_INFO, "REJ response or not polling command in "
1550 "timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001551 }
1552
1553 /* FIXME: 5.5.4.2 2) */
1554
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001555 /* Send message, if possible due to acknowledged data and new V(S) and V(A). */
1556 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001557
1558 break;
1559 default:
1560 /* G.3.1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001561 LOGDL(dl, LOGL_ERROR, "Supervisory frame not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001562 msgb_free(msg);
1563 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1564 return -EINVAL;
1565 }
1566 msgb_free(msg);
1567 return 0;
1568}
1569
1570/* Receive a LAPD I (Information) message from L1 */
1571static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1572{
1573 struct lapd_datalink *dl = lctx->dl;
1574 //uint8_t nr = lctx->n_recv;
1575 uint8_t ns = lctx->n_send;
1576 int length = lctx->length;
1577 int rc;
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001578 bool i_frame_in_queue = false;
rootaf48bed2011-09-26 11:23:06 +02001579
Harald Welte00b2faf2020-05-02 19:56:36 +02001580 LOGDL(dl, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1581 lapd_state_name(dl->state), lctx->sapi);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001582
rootaf48bed2011-09-26 11:23:06 +02001583 /* G.2.2 Wrong value of the C/R bit */
1584 if (lctx->cr == dl->cr.rem2loc.resp) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001585 LOGDL(dl, LOGL_ERROR, "I frame response not allowed (state %s)\n",
1586 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001587 msgb_free(msg);
1588 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1589 return -EINVAL;
1590 }
1591
1592 if (length == 0 || length > lctx->n201) {
1593 /* G.4.2 If the length indicator of an I frame is set
1594 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1595 * primitive with cause "I frame with incorrect length"
1596 * is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001597 LOGDL(dl, LOGL_ERROR, "I frame length not allowed (state %s)\n",
1598 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001599 msgb_free(msg);
1600 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1601 return -EIO;
1602 }
1603
1604 /* G.4.2 If the numerical value of L is L<N201 and the M
1605 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1606 * cause "I frame with incorrect use of M bit" is sent to the
1607 * mobile management entity. */
1608 if (lctx->more && length < lctx->n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001609 LOGDL(dl, LOGL_ERROR, "I frame with M bit too short (state %s)\n",
1610 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001611 msgb_free(msg);
1612 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1613 return -EIO;
1614 }
1615
1616 switch (dl->state) {
1617 case LAPD_STATE_IDLE:
1618 /* if P=1, respond DM with F=1 (5.2.2) */
1619 /* 5.4.5 all other frame types shall be discarded */
1620 if (lctx->p_f)
1621 lapd_send_dm(lctx); /* F=P */
1622 /* fall though */
1623 case LAPD_STATE_SABM_SENT:
1624 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001625 LOGDL(dl, LOGL_NOTICE, "I frame ignored in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001626 msgb_free(msg);
1627 return 0;
1628 }
1629
1630 /* 5.7.1: N(s) sequence error */
1631 if (ns != dl->v_recv) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001632 LOGDL(dl, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, V(R)=%u (state %s)\n",
1633 ns, dl->v_recv, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001634 /* discard data */
1635 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001636 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001637 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001638 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001639 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001640 lapd_send_rej(lctx, lctx->p_f);
1641 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001642 /* If there are two subsequent sequence errors received,
1643 * ignore it. (Ignore every second subsequent error.)
1644 * This happens if our reply with the REJ is too slow,
1645 * so the remote gets a T200 timeout and sends another
1646 * frame with a sequence error.
1647 * Test showed that replying with two subsequent REJ
1648 * messages could the remote L2 process to abort.
1649 * Replying too slow shouldn't happen, but may happen
1650 * over serial link between BB and LAPD.
1651 */
1652 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001653 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001654 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1655 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1656 lapd_acknowledge(lctx); /* V(A) is also set here */
1657
1658 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001659 lapd_send_i(dl, __LINE__, false);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001660
1661 return 0;
rootaf48bed2011-09-26 11:23:06 +02001662 }
1663 dl->seq_err_cond = 0;
1664
1665 /* Increment receiver state */
1666 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Harald Welte00b2faf2020-05-02 19:56:36 +02001667 LOGDL(dl, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
rootaf48bed2011-09-26 11:23:06 +02001668
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001669 /* Update all pending frames in the queue to the new V(R) state. */
1670 if (dl->update_pending_frames) {
1671 rc = dl->update_pending_frames(lctx);
1672 if (!rc)
1673 i_frame_in_queue = true;
1674 }
1675
rootaf48bed2011-09-26 11:23:06 +02001676 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1677 lapd_acknowledge(lctx); /* V(A) is also set here */
1678
1679 /* Only if we are not in own receiver busy condition */
1680 if (!dl->own_busy) {
1681 /* if the frame carries a complete segment */
1682 if (!lctx->more && !dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001683 LOGDL(dl, LOGL_INFO, "message in single I frame\n");
rootaf48bed2011-09-26 11:23:06 +02001684 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001685 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001686 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1687 msg);
1688 } else {
1689 /* create rcv_buffer */
1690 if (!dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001691 LOGDL(dl, LOGL_INFO, "message in multiple I frames (first message)\n");
rootaf48bed2011-09-26 11:23:06 +02001692 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1693 "LAPD RX");
1694 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1695 }
1696 /* concat. rcv_buffer */
1697 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001698 LOGDL(dl, LOGL_NOTICE, "Received frame overflow!\n");
rootaf48bed2011-09-26 11:23:06 +02001699 } else {
1700 memcpy(msgb_put(dl->rcv_buffer, length),
1701 msg->l3h, length);
1702 }
1703 /* if the last segment was received */
1704 if (!lctx->more) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001705 LOGDL(dl, LOGL_INFO, "message in multiple I frames (last message)\n");
rootaf48bed2011-09-26 11:23:06 +02001706 rc = send_dl_l3(PRIM_DL_DATA,
1707 PRIM_OP_INDICATION, lctx,
1708 dl->rcv_buffer);
1709 dl->rcv_buffer = NULL;
1710 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001711 LOGDL(dl, LOGL_INFO, "message in multiple I frames (next message)\n");
rootaf48bed2011-09-26 11:23:06 +02001712 msgb_free(msg);
1713
1714 }
Harald Weltebc1d7152020-07-04 10:52:13 +02001715 /* the L3 or higher (called in-line above via send_dl_l3) might have destroyed the
1716 * data link meanwhile. See OS#1761 */
1717 if (dl->state == LAPD_STATE_NULL)
1718 return 0;
rootaf48bed2011-09-26 11:23:06 +02001719 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001720 LOGDL(dl, LOGL_INFO, "I frame ignored during own receiver busy condition\n");
rootaf48bed2011-09-26 11:23:06 +02001721
1722 /* Check for P bit */
1723 if (lctx->p_f) {
1724 /* 5.5.2.1 */
1725 /* check if we are not in own receiver busy */
1726 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001727 LOGDL(dl, LOGL_INFO, "we are not busy, send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001728 /* Send RR with F=1 */
1729 rc = lapd_send_rr(lctx, 1, 0);
1730 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001731 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001732 /* Send RNR with F=1 */
1733 rc = lapd_send_rnr(lctx, 1, 0);
1734 }
1735 } else {
1736 /* 5.5.2.2 */
1737 /* check if we are not in own receiver busy */
1738 if (!dl->own_busy) {
1739 /* NOTE: V(R) is already set above */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001740 rc = lapd_send_i(dl, __LINE__, false);
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001741 if (rc && !i_frame_in_queue) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001742 LOGDL(dl, LOGL_INFO, "we are not busy and have no pending data, "
1743 "send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001744 /* Send RR with F=0 */
1745 return lapd_send_rr(lctx, 0, 0);
1746 }
1747 /* all I or one RR is sent, we are done */
1748 return 0;
1749 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001750 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001751 /* Send RNR with F=0 */
1752 rc = lapd_send_rnr(lctx, 0, 0);
1753 }
1754 }
1755
1756 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001757 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001758
1759 return rc;
1760}
1761
1762/* Receive a LAPD message from L1 */
1763int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1764{
1765 int rc;
1766
1767 switch (lctx->format) {
1768 case LAPD_FORM_U:
1769 rc = lapd_rx_u(msg, lctx);
1770 break;
1771 case LAPD_FORM_S:
1772 rc = lapd_rx_s(msg, lctx);
1773 break;
1774 case LAPD_FORM_I:
1775 rc = lapd_rx_i(msg, lctx);
1776 break;
1777 default:
Maxc5695262022-10-09 17:41:46 +03001778 LOGDL(lctx->dl, LOGL_NOTICE, "unknown LAPD format 0x%02x\n", lctx->format);
rootaf48bed2011-09-26 11:23:06 +02001779 msgb_free(msg);
1780 rc = -EINVAL;
1781 }
1782 return rc;
1783}
1784
Andreas Eversberg49b70872023-11-09 12:33:13 +01001785/*! Enqueue next LAPD frame and run pending T200. (Must be called when frame is ready to send.)
1786 * The caller (LAPDm code) calls this function before it sends the next frame.
1787 * If there is no frame in the TX queue, LAPD will enqueue next I-frame, if possible.
1788 * If the T200 is pending, it is changed to running state.
1789 * \param[in] lctx LAPD context
1790 * \param[out] rc set to 1, if timer T200 state changed to running, set to 0, if not. */
1791int lapd_ph_rts_ind(struct lapd_msg_ctx *lctx)
1792{
1793 struct lapd_datalink *dl = lctx->dl;
1794
1795 /* If there is no pending frame, try to enqueue next I frame. */
1796 if (llist_empty(&dl->tx_queue) && (dl->state == LAPD_STATE_MF_EST || dl->state == LAPD_STATE_TIMER_RECOV)) {
1797 /* Send an I frame, if there are pending outgoing messages. */
1798 lapd_send_i(dl, __LINE__, true);
1799 }
1800
1801 /* Run T200 at RTS, if pending. Tell caller that is has been started. (rc = 1) */
1802 if (dl->t200_rts == LAPD_T200_RTS_PENDING) {
1803 dl->t200_rts = LAPD_T200_RTS_RUNNING;
1804 return 1;
1805 }
1806
1807 return 0;
1808}
1809
rootaf48bed2011-09-26 11:23:06 +02001810/* L3 -> L2 */
1811
1812/* send unit data */
1813static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1814{
1815 struct lapd_datalink *dl = lctx->dl;
1816 struct msgb *msg = dp->oph.msg;
1817 struct lapd_msg_ctx nctx;
1818
1819 memcpy(&nctx, lctx, sizeof(nctx));
1820 /* keep nctx.ldp */
1821 /* keep nctx.sapi */
1822 /* keep nctx.tei */
1823 nctx.cr = dl->cr.loc2rem.cmd;
1824 nctx.format = LAPD_FORM_U;
1825 nctx.s_u = LAPD_U_UI;
1826 /* keep nctx.p_f */
1827 nctx.length = msg->len;
1828 nctx.more = 0;
1829
1830 return dl->send_ph_data_req(&nctx, msg);
1831}
1832
Max2b283b12022-11-17 22:18:01 +03001833static void msg_to_tx_hist(struct lapd_history *tx_hist, const struct msgb *msg, int length, int more)
1834{
1835 tx_hist->msg = lapd_msgb_alloc(msg->len, "HIST");
1836 tx_hist->more = more;
1837 msgb_put(tx_hist->msg, msg->len);
1838 if (length)
1839 memcpy(tx_hist->msg->data, msg->l3h, msg->len);
1840}
1841
1842static void msg_to_tx_hist0(struct lapd_datalink *dl, const struct msgb *msg)
1843{
1844 return msg_to_tx_hist(&dl->tx_hist[0], msg, msg->len, 0);
1845}
1846
rootaf48bed2011-09-26 11:23:06 +02001847/* request link establishment */
1848static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1849{
1850 struct lapd_datalink *dl = lctx->dl;
1851 struct msgb *msg = dp->oph.msg;
1852 struct lapd_msg_ctx nctx;
1853
1854 if (msg->len)
Harald Welte00b2faf2020-05-02 19:56:36 +02001855 LOGDL(dl, LOGL_INFO, "perform establishment with content (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001856 else
Harald Welte00b2faf2020-05-02 19:56:36 +02001857 LOGDL(dl, LOGL_INFO, "perform normal establishm. (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001858
1859 /* Flush send-queue */
1860 /* Clear send-buffer */
1861 lapd_dl_flush_send(dl);
1862 /* be sure that history is empty */
1863 lapd_dl_flush_hist(dl);
1864
1865 /* save message context for further use */
1866 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1867
1868 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001869 msgb_free(dl->rcv_buffer);
1870 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001871
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_U;
1879 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1880 nctx.p_f = 1;
1881 nctx.length = msg->len;
1882 nctx.more = 0;
1883
1884 /* Transmit-buffer carries exactly one segment */
Max2b283b12022-11-17 22:18:01 +03001885 msg_to_tx_hist0(dl, msg);
1886
rootaf48bed2011-09-26 11:23:06 +02001887 /* set Vs to 0, because it is used as index when resending SABM */
1888 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001889
rootaf48bed2011-09-26 11:23:06 +02001890 /* Set states */
1891 dl->own_busy = dl->peer_busy = 0;
1892 dl->retrans_ctr = 0;
1893 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1894
1895 /* Tramsmit and start T200 */
1896 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001897 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001898
1899 return 0;
1900}
1901
1902/* send data */
1903static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1904{
1905 struct lapd_datalink *dl = lctx->dl;
1906 struct msgb *msg = dp->oph.msg;
1907
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001908 if (msgb_l3len(msg) == 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001909 LOGDL(dl, LOGL_ERROR, "writing an empty message is not possible\n");
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001910 msgb_free(msg);
1911 return -1;
1912 }
1913
Harald Welte00b2faf2020-05-02 19:56:36 +02001914 LOGDL(dl, LOGL_INFO, "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001915
1916 /* Write data into the send queue */
1917 msgb_enqueue(&dl->send_queue, msg);
1918
1919 /* Send message, if possible */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001920 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001921
1922 return 0;
1923}
1924
1925/* Send next I frame from queued/buffered data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001926static int lapd_send_i(struct lapd_datalink *dl, int line, bool rts)
rootaf48bed2011-09-26 11:23:06 +02001927{
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001928 struct lapd_msg_ctx *lctx = &dl->lctx;
rootaf48bed2011-09-26 11:23:06 +02001929 uint8_t k = dl->k;
1930 uint8_t h;
1931 struct msgb *msg;
1932 int length, left;
1933 int rc = - 1; /* we sent nothing */
1934 struct lapd_msg_ctx nctx;
1935
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001936 if (!rts)
1937 LOGDL(dl, LOGL_INFO, "%s() called from line %d\n", __func__, line);
rootaf48bed2011-09-26 11:23:06 +02001938
Andreas Eversberg49b70872023-11-09 12:33:13 +01001939 if ((dl->lapd_flags & LAPD_F_RTS) && !llist_empty(&dl->tx_queue)) {
1940 if (!rts)
1941 LOGDL(dl, LOGL_INFO, "There is a frame in the TX queue, not checking for sending I frame.\n");
1942 return rc;
1943 }
1944
rootaf48bed2011-09-26 11:23:06 +02001945 next_frame:
1946
1947 if (dl->peer_busy) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001948 if (!rts)
1949 LOGDL(dl, LOGL_INFO, "Peer busy, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001950 return rc;
1951 }
1952
1953 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001954 if (!rts)
1955 LOGDL(dl, LOGL_INFO, "Timer recovery, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001956 return rc;
1957 }
1958
1959 /* If the send state variable V(S) is equal to V(A) plus k
1960 * (where k is the maximum number of outstanding I frames - see
1961 * subclause 5.8.4), the data link layer entity shall not transmit any
1962 * new I frames, but shall retransmit an I frame as a result
1963 * of the error recovery procedures as described in subclauses 5.5.4 and
1964 * 5.5.7. */
1965 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001966 if (!rts)
1967 LOGDL(dl, LOGL_INFO, "k frames outstanding, not sending more. (k=%u V(S)=%u V(A)=%u)\n",
1968 k, dl->v_send, dl->v_ack);
rootaf48bed2011-09-26 11:23:06 +02001969 return rc;
1970 }
1971
1972 h = do_mod(dl->v_send, dl->range_hist);
1973
1974 /* if we have no tx_hist yet, we create it */
1975 if (!dl->tx_hist[h].msg) {
1976 /* Get next message into send-buffer, if any */
1977 if (!dl->send_buffer) {
1978 next_message:
1979 dl->send_out = 0;
1980 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1981 /* No more data to be sent */
1982 if (!dl->send_buffer)
1983 return rc;
Harald Welte00b2faf2020-05-02 19:56:36 +02001984 LOGDL(dl, LOGL_INFO, "get message from send-queue\n");
rootaf48bed2011-09-26 11:23:06 +02001985 }
1986
1987 /* How much is left in the send-buffer? */
1988 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1989 /* Segment, if data exceeds N201 */
1990 length = left;
1991 if (length > lctx->n201)
1992 length = lctx->n201;
Harald Welte00b2faf2020-05-02 19:56:36 +02001993 LOGDL(dl, LOGL_INFO, "msg-len %d sent %d left %d N201 %d length %d "
1994 "first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left,
1995 lctx->n201, length, dl->send_buffer->l3h[0]);
rootaf48bed2011-09-26 11:23:06 +02001996 /* If message in send-buffer is completely sent */
1997 if (left == 0) {
1998 msgb_free(dl->send_buffer);
1999 dl->send_buffer = NULL;
2000 goto next_message;
2001 }
2002
Harald Welte00b2faf2020-05-02 19:56:36 +02002003 LOGDL(dl, LOGL_INFO, "send I frame %sV(S)=%d\n",
2004 (left > length) ? "segment " : "", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02002005
2006 /* Create I frame (segment) and transmit-buffer content */
2007 msg = lapd_msgb_alloc(length, "LAPD I");
2008 msg->l3h = msgb_put(msg, length);
2009 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01002010 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02002011 /* keep nctx.ldp */
2012 /* keep nctx.sapi */
2013 /* keep nctx.tei */
2014 nctx.cr = dl->cr.loc2rem.cmd;
2015 nctx.format = LAPD_FORM_I;
2016 nctx.p_f = 0;
2017 nctx.n_send = dl->v_send;
2018 nctx.n_recv = dl->v_recv;
2019 nctx.length = length;
2020 if (left > length)
2021 nctx.more = 1;
2022 else
2023 nctx.more = 0;
2024 if (length)
2025 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
2026 length);
2027 /* store in tx_hist */
Max2b283b12022-11-17 22:18:01 +03002028 msg_to_tx_hist(&dl->tx_hist[h], msg, length, nctx.more);
2029
rootaf48bed2011-09-26 11:23:06 +02002030 /* Add length to track how much is already in the tx buffer */
2031 dl->send_out += length;
2032 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02002033 LOGDL(dl, LOGL_INFO, "resend I frame from tx buffer V(S)=%d\n", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02002034
2035 /* Create I frame (segment) from tx_hist */
2036 length = dl->tx_hist[h].msg->len;
2037 msg = lapd_msgb_alloc(length, "LAPD I resend");
2038 msg->l3h = msgb_put(msg, length);
2039 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01002040 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02002041 /* keep nctx.ldp */
2042 /* keep nctx.sapi */
2043 /* keep nctx.tei */
2044 nctx.cr = dl->cr.loc2rem.cmd;
2045 nctx.format = LAPD_FORM_I;
2046 nctx.p_f = 0;
2047 nctx.n_send = dl->v_send;
2048 nctx.n_recv = dl->v_recv;
2049 nctx.length = length;
2050 nctx.more = dl->tx_hist[h].more;
2051 if (length)
2052 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
2053 }
2054
2055 /* The value of the send state variable V(S) shall be incremented by 1
2056 * at the end of the transmission of the I frame */
2057 dl->v_send = inc_mod(dl->v_send, dl->v_range);
2058
2059 /* If timer T200 is not running at the time right before transmitting a
2060 * frame, when the PH-READY-TO-SEND primitive is received from the
2061 * physical layer., it shall be set. */
Andreas Eversberg49b70872023-11-09 12:33:13 +01002062 if (!lapd_is_t200_started(dl)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02002063 /* stop Timer T203, if running */
2064 lapd_stop_t203(dl);
2065 /* start Timer T200 */
2066 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002067 }
2068
2069 dl->send_ph_data_req(&nctx, msg);
2070
Andreas Eversberg49b70872023-11-09 12:33:13 +01002071 /* When using RTS, we send only one frame. */
2072 if ((dl->lapd_flags & LAPD_F_RTS))
2073 return 0;
2074
2075 rc = 0; /* We sent an I frame, so sending RR frame is not required. */
rootaf48bed2011-09-26 11:23:06 +02002076 goto next_frame;
2077}
2078
2079/* request link suspension */
2080static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2081{
2082 struct lapd_datalink *dl = lctx->dl;
2083 struct msgb *msg = dp->oph.msg;
2084
Harald Welte00b2faf2020-05-02 19:56:36 +02002085 LOGDL(dl, LOGL_INFO, "perform suspension\n");
rootaf48bed2011-09-26 11:23:06 +02002086
2087 /* put back the send-buffer to the send-queue (first position) */
2088 if (dl->send_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002089 LOGDL(dl, LOGL_INFO, "put frame in sendbuffer back to queue\n");
rootaf48bed2011-09-26 11:23:06 +02002090 llist_add(&dl->send_buffer->list, &dl->send_queue);
2091 dl->send_buffer = NULL;
2092 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02002093 LOGDL(dl, LOGL_INFO, "no frame in sendbuffer\n");
rootaf48bed2011-09-26 11:23:06 +02002094
2095 /* Clear transmit buffer, but keep send buffer */
2096 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002097 /* Stop timers (there is no state change, so we must stop all timers */
2098 lapd_stop_t200(dl);
2099 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02002100
2101 msgb_free(msg);
2102
2103 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
2104}
2105
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002106/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002107static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2108{
2109 struct lapd_datalink *dl = lctx->dl;
2110 struct msgb *msg = dp->oph.msg;
2111 struct lapd_msg_ctx nctx;
2112
Harald Welte00b2faf2020-05-02 19:56:36 +02002113 LOGDL(dl, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", msg->len);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002114
rootaf48bed2011-09-26 11:23:06 +02002115 /* be sure that history is empty */
2116 lapd_dl_flush_hist(dl);
2117
2118 /* save message context for further use */
2119 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2120
2121 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002122 msgb_free(dl->send_buffer);
2123 dl->send_buffer = NULL;
2124
rootaf48bed2011-09-26 11:23:06 +02002125 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002126 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002127 /* Write data into the send buffer, to be sent first */
2128 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002129 } else {
2130 msgb_free(msg);
2131 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002132 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002133 }
rootaf48bed2011-09-26 11:23:06 +02002134
2135 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002136 msgb_free(dl->rcv_buffer);
2137 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002138
2139 /* Create new msgb (old one is now free) */
2140 msg = lapd_msgb_alloc(0, "LAPD SABM");
2141 msg->l3h = msg->data;
2142 /* assemble message */
2143 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2144 /* keep nctx.ldp */
2145 /* keep nctx.sapi */
2146 /* keep nctx.tei */
2147 nctx.cr = dl->cr.loc2rem.cmd;
2148 nctx.format = LAPD_FORM_U;
2149 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2150 nctx.p_f = 1;
2151 nctx.length = 0;
2152 nctx.more = 0;
2153
Max2b283b12022-11-17 22:18:01 +03002154 msg_to_tx_hist0(dl, msg);
2155
rootaf48bed2011-09-26 11:23:06 +02002156 /* set Vs to 0, because it is used as index when resending SABM */
2157 dl->v_send = 0;
2158
2159 /* Set states */
2160 dl->own_busy = dl->peer_busy = 0;
2161 dl->retrans_ctr = 0;
2162 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2163
2164 /* Tramsmit and start T200 */
2165 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002166 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002167
2168 return 0;
2169}
2170
2171/* requesst release of link */
2172static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2173{
2174 struct lapd_datalink *dl = lctx->dl;
2175 struct msgb *msg = dp->oph.msg;
2176 struct lapd_msg_ctx nctx;
2177
2178 /* local release */
2179 if (dp->u.rel_req.mode) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002180 LOGDL(dl, LOGL_INFO, "perform local release\n");
rootaf48bed2011-09-26 11:23:06 +02002181 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002182 /* stop Timer T200 */
2183 lapd_stop_t200(dl);
2184 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002185 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2186 /* flush buffers */
2187 lapd_dl_flush_tx(dl);
2188 lapd_dl_flush_send(dl);
2189 /* send notification to L3 */
2190 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2191 }
2192
2193 /* in case we are already disconnecting */
2194 if (dl->state == LAPD_STATE_DISC_SENT)
2195 return -EBUSY;
2196
2197 /* flush tx_hist */
2198 lapd_dl_flush_hist(dl);
2199
Harald Welte00b2faf2020-05-02 19:56:36 +02002200 LOGDL(dl, LOGL_INFO, "perform normal release (DISC)\n");
rootaf48bed2011-09-26 11:23:06 +02002201
2202 /* Push LAPD header on msgb */
2203 /* assemble message */
2204 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2205 /* keep nctx.ldp */
2206 /* keep nctx.sapi */
2207 /* keep nctx.tei */
2208 nctx.cr = dl->cr.loc2rem.cmd;
2209 nctx.format = LAPD_FORM_U;
2210 nctx.s_u = LAPD_U_DISC;
2211 nctx.p_f = 1;
2212 nctx.length = 0;
2213 nctx.more = 0;
2214
Max2b283b12022-11-17 22:18:01 +03002215 msg_to_tx_hist0(dl, msg);
2216
rootaf48bed2011-09-26 11:23:06 +02002217 /* set Vs to 0, because it is used as index when resending DISC */
2218 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002219
rootaf48bed2011-09-26 11:23:06 +02002220 /* Set states */
2221 dl->own_busy = dl->peer_busy = 0;
2222 dl->retrans_ctr = 0;
2223 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2224
2225 /* Tramsmit and start T200 */
2226 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002227 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002228
2229 return 0;
2230}
2231
2232/* request release of link in idle state */
2233static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2234 struct lapd_msg_ctx *lctx)
2235{
2236 struct lapd_datalink *dl = lctx->dl;
2237 struct msgb *msg = dp->oph.msg;
2238
2239 msgb_free(msg);
2240
2241 /* send notification to L3 */
2242 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2243}
2244
2245/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002246static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002247 uint32_t states;
2248 int prim, op;
2249 const char *name;
2250 int (*rout) (struct osmo_dlsap_prim *dp,
2251 struct lapd_msg_ctx *lctx);
2252} l2downstatelist[] = {
2253 /* create and send UI command */
2254 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002255 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002256 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2257
2258 /* create and send SABM command */
2259 {SBIT(LAPD_STATE_IDLE),
2260 PRIM_DL_EST, PRIM_OP_REQUEST,
2261 "DL-ESTABLISH-REQUEST", lapd_est_req},
2262
2263 /* create and send I command */
2264 {SBIT(LAPD_STATE_MF_EST) |
2265 SBIT(LAPD_STATE_TIMER_RECOV),
2266 PRIM_DL_DATA, PRIM_OP_REQUEST,
2267 "DL-DATA-REQUEST", lapd_data_req},
2268
2269 /* suspend datalink */
2270 {SBIT(LAPD_STATE_MF_EST) |
2271 SBIT(LAPD_STATE_TIMER_RECOV),
2272 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2273 "DL-SUSPEND-REQUEST", lapd_susp_req},
2274
2275 /* create and send SABM command (resume) */
2276 {SBIT(LAPD_STATE_MF_EST) |
2277 SBIT(LAPD_STATE_TIMER_RECOV),
2278 PRIM_DL_RES, PRIM_OP_REQUEST,
2279 "DL-RESUME-REQUEST", lapd_res_req},
2280
2281 /* create and send SABM command (reconnect) */
2282 {SBIT(LAPD_STATE_IDLE) |
2283 SBIT(LAPD_STATE_MF_EST) |
2284 SBIT(LAPD_STATE_TIMER_RECOV),
2285 PRIM_DL_RECON, PRIM_OP_REQUEST,
2286 "DL-RECONNECT-REQUEST", lapd_res_req},
2287
2288 /* create and send DISC command */
2289 {SBIT(LAPD_STATE_SABM_SENT) |
2290 SBIT(LAPD_STATE_MF_EST) |
2291 SBIT(LAPD_STATE_TIMER_RECOV) |
2292 SBIT(LAPD_STATE_DISC_SENT),
2293 PRIM_DL_REL, PRIM_OP_REQUEST,
2294 "DL-RELEASE-REQUEST", lapd_rel_req},
2295
2296 /* release in idle state */
2297 {SBIT(LAPD_STATE_IDLE),
2298 PRIM_DL_REL, PRIM_OP_REQUEST,
2299 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2300};
2301
2302#define L2DOWNSLLEN \
2303 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2304
2305int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2306{
2307 struct lapd_datalink *dl = lctx->dl;
2308 int i, supported = 0;
2309 struct msgb *msg = dp->oph.msg;
2310 int rc;
2311
2312 /* find function for current state and message */
2313 for (i = 0; i < L2DOWNSLLEN; i++) {
2314 if (dp->oph.primitive == l2downstatelist[i].prim
2315 && dp->oph.operation == l2downstatelist[i].op) {
2316 supported = 1;
2317 if ((SBIT(dl->state) & l2downstatelist[i].states))
2318 break;
2319 }
2320 }
2321 if (!supported) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002322 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unsupported\n",
2323 dp->oph.primitive, dp->oph.operation);
rootaf48bed2011-09-26 11:23:06 +02002324 msgb_free(msg);
2325 return 0;
2326 }
2327 if (i == L2DOWNSLLEN) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002328 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unhandled at this state %s\n",
2329 dp->oph.primitive, dp->oph.operation, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002330 msgb_free(msg);
2331 return 0;
2332 }
2333
Harald Welte00b2faf2020-05-02 19:56:36 +02002334 LOGDL(dl, LOGL_INFO, "Message %s received in state %s\n",
2335 l2downstatelist[i].name, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002336
2337 rc = l2downstatelist[i].rout(dp, lctx);
2338
2339 return rc;
2340}
2341
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002342/*! @} */