blob: b32ed2631eaa2705f177e841527515edd09cdd9f [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
Andreas Eversberg6cc53012023-11-16 11:58:49 +0100799/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value
800 * In case of a sequence error, the cause is returned with negative sign. */
801static int lapd_acknowledge(struct lapd_msg_ctx *lctx)
rootaf48bed2011-09-26 11:23:06 +0200802{
803 struct lapd_datalink *dl = lctx->dl;
804 uint8_t nr = lctx->n_recv;
Max327e5e92022-10-11 19:48:44 +0300805 int s = 0, rej = 0;
806 bool t200_reset = false;
rootaf48bed2011-09-26 11:23:06 +0200807 int i, h;
808
809 /* supervisory frame ? */
810 if (lctx->format == LAPD_FORM_S)
811 s = 1;
812 /* REJ frame ? */
813 if (s && lctx->s_u == LAPD_S_REJ)
814 rej = 1;
815
816 /* Flush all transmit buffers of acknowledged frames */
817 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
818 h = do_mod(i, dl->range_hist);
819 if (dl->tx_hist[h].msg) {
820 msgb_free(dl->tx_hist[h].msg);
821 dl->tx_hist[h].msg = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200822 LOGDL(dl, LOGL_INFO, "ack frame %d\n", i);
rootaf48bed2011-09-26 11:23:06 +0200823 }
824 }
825
826 if (dl->state != LAPD_STATE_TIMER_RECOV) {
827 /* When not in the timer recovery condition, the data
828 * link layer entity shall reset the timer T200 on
829 * receipt of a valid I frame with N(R) higher than V(A),
830 * or an REJ with an N(R) equal to V(A). */
Max68588c52022-10-11 19:34:43 +0300831 if ((!rej && nr != dl->v_ack) || (rej && nr == dl->v_ack)) {
Max327e5e92022-10-11 19:48:44 +0300832 t200_reset = true;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200833 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200834 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
835 }
836 /* 5.7.4: N(R) sequence error
837 * N(R) is called valid, if and only if
838 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
839 */
Max68588c52022-10-11 19:34:43 +0300840 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 +0200841 LOGDL(dl, LOGL_NOTICE, "N(R) sequence error\n");
Andreas Eversberg6cc53012023-11-16 11:58:49 +0100842 return -MDL_CAUSE_SEQ_ERR;
rootaf48bed2011-09-26 11:23:06 +0200843 }
844 }
845
846 /* V(A) shall be set to the value of N(R) */
847 dl->v_ack = nr;
848
Andreas Eversberg742fc792011-09-27 09:40:25 +0200849 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200850 * and if there are outstanding I frames, restart T200 */
851 if (t200_reset && !rej) {
852 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200853 LOGDL(dl, LOGL_INFO, "start T200, due to unacked I frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200854 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200855 }
856 }
857
858 /* This also does a restart, when I or S frame is received */
859
860 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200861 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200862 /* Start T203, if T200 is not running in MF EST state, if enabled */
Andreas Eversberg49b70872023-11-09 12:33:13 +0100863 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 +0200864 lapd_start_t203(dl);
Andreas Eversberg6cc53012023-11-16 11:58:49 +0100865
866 return 0;
rootaf48bed2011-09-26 11:23:06 +0200867}
868
869/* L1 -> L2 */
870
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200871/* Receive a LAPD U SABM(E) message from L1 */
872static int lapd_rx_u_sabm(struct msgb *msg, struct lapd_msg_ctx *lctx)
rootaf48bed2011-09-26 11:23:06 +0200873{
874 struct lapd_datalink *dl = lctx->dl;
875 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100876 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200877 uint8_t prim, op;
878
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200879 prim = PRIM_DL_EST;
880 op = PRIM_OP_INDICATION;
rootaf48bed2011-09-26 11:23:06 +0200881
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200882 LOGDL(dl, LOGL_INFO, "SABM(E) received in state %s\n", lapd_state_name(dl->state));
883 /* 5.7.1 */
884 dl->seq_err_cond = 0;
885 /* G.2.2 Wrong value of the C/R bit */
886 if (lctx->cr == dl->cr.rem2loc.resp) {
887 LOGDL(dl, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200888 msgb_free(msg);
889 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
890 return -EINVAL;
891 }
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200892
893 /* G.4.5 If SABM is received with L>N201 or with M bit
894 * set, AN MDL-ERROR-INDICATION is sent to MM.
895 */
896 if (lctx->more || length > lctx->n201) {
897 LOGDL(dl, LOGL_ERROR, "SABM too large error\n");
898 msgb_free(msg);
899 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
900 return -EIO;
901 }
902
903 switch (dl->state) {
904 case LAPD_STATE_IDLE:
905 break;
Pau Espin Pedrol76190d32020-10-21 13:05:44 +0200906 case LAPD_STATE_TIMER_RECOV:
907 LOGDL(dl, LOGL_INFO, "SABM command, timer recovery state\n");
908 /* If link is lost on the remote side, we start over
909 * and send DL-ESTABLISH indication again. */
910 /* 3GPP TS 44.006 8.6.3 "Procedures for re-establishment" */
911 if (length) {
912 /* check for contention resoultion */
913 LOGDL(dl, LOGL_ERROR, "SABM L>0 not expected in timer "
914 "recovery state\n");
915 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
916 lapd_send_dm(lctx);
917 msgb_free(msg);
918 return 0;
919 }
920 /* re-establishment, continue below */
921 lapd_stop_t200(dl);
922 break;
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200923 case LAPD_STATE_MF_EST:
924 LOGDL(dl, LOGL_INFO, "SABM command, multiple frame established state\n");
925 /* If link is lost on the remote side, we start over
926 * and send DL-ESTABLISH indication again. */
927 /* Additionally, continue in case of content resoltion
928 * (GSM network). This happens, if the mobile has not
929 * yet received UA or another mobile (collision) tries
930 * to establish connection. The mobile must receive
931 * UA again. */
932 /* 5.4.2.1 */
933 if (!length) {
934 /* If no content resolution, this is a
935 * re-establishment. */
936 LOGDL(dl, LOGL_INFO, "Remote reestablish\n");
937 break;
938 }
939 if (!dl->cont_res) {
940 LOGDL(dl, LOGL_INFO, "SABM command not allowed in state %s\n",
941 lapd_state_name(dl->state));
942 mdl_error(MDL_CAUSE_SABM_MF, lctx);
943 msgb_free(msg);
944 return 0;
945 }
946 /* Ignore SABM if content differs from first SABM. */
947 if (dl->mode == LAPD_MODE_NETWORK && length) {
948#ifdef TEST_CONTENT_RESOLUTION_NETWORK
949 dl->cont_res->data[0] ^= 0x01;
950#endif
951 if (memcmp(dl->cont_res->data, msg->data,
952 length)) {
953 LOGDL(dl, LOGL_INFO, "Another SABM with different content - "
954 "ignoring!\n");
955 msgb_free(msg);
956 return 0;
957 }
958 }
959 /* send UA again */
960 lapd_send_ua(lctx, length, msg->l3h);
961 msgb_free(msg);
962 return 0;
963 case LAPD_STATE_DISC_SENT:
964 /* 5.4.6.2 send DM with F=P */
965 lapd_send_dm(lctx);
966 /* stop Timer T200 */
967 lapd_stop_t200(dl);
968 msgb_free(msg);
969 return send_dl_simple(prim, op, lctx);
970 default:
971 /* collision: Send UA, but still wait for rx UA, then
972 * change to MF_EST state.
973 */
974 /* check for contention resoultion */
975 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
976 LOGDL(dl, LOGL_NOTICE, "SABM not allowed during contention "
977 "resolution (state=%s)\n", lapd_state_name(dl->state));
978 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
979 }
980 lapd_send_ua(lctx, length, msg->l3h);
981 msgb_free(msg);
982 return 0;
983 }
984 /* save message context for further use */
985 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
986#ifndef TEST_CONTENT_RESOLUTION_NETWORK
987 /* send UA response */
988 lapd_send_ua(lctx, length, msg->l3h);
989#endif
990 /* set Vs, Vr and Va to 0 */
991 dl->v_send = dl->v_recv = dl->v_ack = 0;
992 /* clear tx_hist */
993 lapd_dl_flush_hist(dl);
994 /* enter multiple-frame-established state */
995 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
996 /* store content resolution data on network side
997 * Note: cont_res will be removed when changing state again,
998 * so it must be allocated AFTER lapd_dl_newstate(). */
999 if (dl->mode == LAPD_MODE_NETWORK && length) {
1000 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
1001 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
1002 length);
Pau Espin Pedrol28b404f2022-03-02 19:03:32 +01001003 LOGDL(dl, LOGL_INFO, "Store content res.\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001004 }
1005 /* send notification to L3 */
1006 if (length == 0) {
1007 /* 5.4.1.2 Normal establishment procedures */
1008 rc = send_dl_simple(prim, op, lctx);
1009 msgb_free(msg);
1010 } else {
1011 /* 5.4.1.4 Contention resolution establishment */
1012 msgb_trim(msg, length);
1013 rc = send_dl_l3(prim, op, lctx, msg);
1014 }
rootaf48bed2011-09-26 11:23:06 +02001015 return rc;
1016}
1017
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001018/* Receive a LAPD U DM message from L1 */
1019static int lapd_rx_u_dm(struct msgb *msg, struct lapd_msg_ctx *lctx)
1020{
1021 struct lapd_datalink *dl = lctx->dl;
1022 int rc = 0;
1023
1024 LOGDL(dl, LOGL_INFO, "DM received in state %s\n", lapd_state_name(dl->state));
1025 /* G.2.2 Wrong value of the C/R bit */
1026 if (lctx->cr == dl->cr.rem2loc.cmd) {
1027 LOGDL(dl, LOGL_ERROR, "DM command error\n");
1028 msgb_free(msg);
1029 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1030 return -EINVAL;
1031 }
1032 if (!lctx->p_f) {
1033 /* 5.4.1.2 DM responses with the F bit set to "0"
1034 * shall be ignored.
1035 */
1036 msgb_free(msg);
1037 return 0;
1038 }
1039 switch (dl->state) {
1040 case LAPD_STATE_SABM_SENT:
1041 break;
1042 case LAPD_STATE_MF_EST:
1043 if (lctx->p_f) {
1044 LOGDL(dl, LOGL_INFO, "unsolicited DM response\n");
1045 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
1046 } else {
1047 LOGDL(dl, LOGL_INFO, "unsolicited DM response, "
1048 "multiple frame established state\n");
1049 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1050 /* reestablish */
1051 if (!dl->reestablish) {
1052 msgb_free(msg);
1053 return 0;
1054 }
1055 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1056 lapd_reestablish(dl);
1057 }
1058 msgb_free(msg);
1059 return 0;
1060 case LAPD_STATE_TIMER_RECOV:
1061 /* FP = 0 (DM is normal in case PF = 1) */
1062 if (!lctx->p_f) {
1063 LOGDL(dl, LOGL_INFO, "unsolicited DM response, multiple frame "
1064 "established state\n");
1065 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1066 msgb_free(msg);
1067 /* reestablish */
1068 if (!dl->reestablish)
1069 return 0;
1070 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1071 return lapd_reestablish(dl);
1072 }
1073 break;
1074 case LAPD_STATE_DISC_SENT:
1075 /* stop Timer T200 */
1076 lapd_stop_t200(dl);
1077 /* go to idle state */
1078 lapd_dl_flush_tx(dl);
1079 lapd_dl_flush_send(dl);
1080 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1081 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1082 msgb_free(msg);
1083 return 0;
1084 case LAPD_STATE_IDLE:
1085 /* 5.4.5 all other frame types shall be discarded */
1086 default:
1087 LOGDL(dl, LOGL_INFO, "unsolicited DM response! (discarding)\n");
1088 msgb_free(msg);
1089 return 0;
1090 }
1091 /* stop timer T200 */
1092 lapd_stop_t200(dl);
1093 /* go to idle state */
1094 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1095 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1096 msgb_free(msg);
1097 return rc;
1098}
1099
1100/* Receive a LAPD U UI message from L1 */
1101static int lapd_rx_u_ui(struct msgb *msg, struct lapd_msg_ctx *lctx)
1102{
1103 struct lapd_datalink *dl = lctx->dl;
1104 int length = lctx->length;
1105
1106 LOGDL(dl, LOGL_INFO, "UI received\n");
1107 /* G.2.2 Wrong value of the C/R bit */
1108 if (lctx->cr == dl->cr.rem2loc.resp) {
1109 LOGDL(dl, LOGL_ERROR, "UI indicates response error\n");
1110 msgb_free(msg);
1111 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1112 return -EINVAL;
1113 }
1114
1115 /* G.4.5 If UI is received with L>N201 or with M bit
1116 * set, AN MDL-ERROR-INDICATION is sent to MM.
1117 */
1118 if (length > lctx->n201 || lctx->more) {
1119 LOGDL(dl, LOGL_ERROR, "UI too large error (%d > N201(%d) or M=%d)\n",
1120 length, lctx->n201, lctx->more);
1121 msgb_free(msg);
1122 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1123 return -EIO;
1124 }
1125
1126 /* do some length checks */
1127 if (length == 0) {
1128 /* 5.3.3 UI frames received with the length indicator
1129 * set to "0" shall be ignored
1130 */
1131 LOGDL(dl, LOGL_INFO, "length=0 (discarding)\n");
1132 msgb_free(msg);
1133 return 0;
1134 }
1135 msgb_trim(msg, length);
1136 return send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx, msg);
1137}
1138
1139/* Receive a LAPD U DISC message from L1 */
1140static int lapd_rx_u_disc(struct msgb *msg, struct lapd_msg_ctx *lctx)
1141{
1142 struct lapd_datalink *dl = lctx->dl;
1143 int length = lctx->length;
1144 int rc = 0;
1145 uint8_t prim, op;
1146
1147 prim = PRIM_DL_REL;
1148 op = PRIM_OP_INDICATION;
1149
1150 LOGDL(dl, LOGL_INFO, "DISC received in state %s\n", lapd_state_name(dl->state));
1151 /* flush tx and send buffers */
1152 lapd_dl_flush_tx(dl);
1153 lapd_dl_flush_send(dl);
1154 /* 5.7.1 */
1155 dl->seq_err_cond = 0;
1156 /* G.2.2 Wrong value of the C/R bit */
1157 if (lctx->cr == dl->cr.rem2loc.resp) {
1158 LOGDL(dl, LOGL_ERROR, "DISC response error\n");
1159 msgb_free(msg);
1160 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1161 return -EINVAL;
1162 }
1163 if (length > 0 || lctx->more) {
1164 /* G.4.4 If a DISC or DM frame is received with L>0 or
1165 * with the M bit set to "1", an MDL-ERROR-INDICATION
1166 * primitive with cause "U frame with incorrect
1167 * parameters" is sent to the mobile management entity.
1168 */
1169 LOGDL(dl, LOGL_ERROR, "U frame iwth incorrect parameters\n");
1170 msgb_free(msg);
1171 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1172 return -EIO;
1173 }
1174 switch (dl->state) {
1175 case LAPD_STATE_IDLE:
1176 LOGDL(dl, LOGL_INFO, "DISC in idle state\n");
1177 /* send DM with F=P */
1178 msgb_free(msg);
1179 return lapd_send_dm(lctx);
1180 case LAPD_STATE_SABM_SENT:
1181 LOGDL(dl, LOGL_INFO, "DISC in SABM state\n");
1182 /* 5.4.6.2 send DM with F=P */
1183 lapd_send_dm(lctx);
1184 /* stop Timer T200 */
1185 lapd_stop_t200(dl);
1186 /* go to idle state */
1187 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1188 msgb_free(msg);
1189 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1190 lctx);
1191 case LAPD_STATE_MF_EST:
1192 case LAPD_STATE_TIMER_RECOV:
1193 LOGDL(dl, LOGL_INFO, "DISC in est state\n");
1194 break;
1195 case LAPD_STATE_DISC_SENT:
1196 LOGDL(dl, LOGL_INFO, "DISC in disc state\n");
1197 prim = PRIM_DL_REL;
1198 op = PRIM_OP_CONFIRM;
1199 break;
1200 default:
1201 lapd_send_ua(lctx, length, msg->l3h);
1202 msgb_free(msg);
1203 return 0;
1204 }
1205 /* send UA response */
1206 lapd_send_ua(lctx, length, msg->l3h);
1207 /* stop Timer T200 */
1208 lapd_stop_t200(dl);
1209 /* enter idle state, keep tx-buffer with UA response */
1210 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1211 /* send notification to L3 */
1212 rc = send_dl_simple(prim, op, lctx);
1213 msgb_free(msg);
1214 return rc;
1215}
1216
1217/* Receive a LAPD U UA message from L1 */
1218static int lapd_rx_u_ua(struct msgb *msg, struct lapd_msg_ctx *lctx)
1219{
1220 struct lapd_datalink *dl = lctx->dl;
1221 int length = lctx->length;
1222 int rc = 0;
1223
1224 LOGDL(dl, LOGL_INFO, "UA received in state %s\n", lapd_state_name(dl->state));
1225 /* G.2.2 Wrong value of the C/R bit */
1226 if (lctx->cr == dl->cr.rem2loc.cmd) {
1227 LOGDL(dl, LOGL_ERROR, "UA indicates command error\n");
1228 msgb_free(msg);
1229 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1230 return -EINVAL;
1231 }
1232
1233 /* G.4.5 If UA is received with L>N201 or with M bit
1234 * set, AN MDL-ERROR-INDICATION is sent to MM.
1235 */
1236 if (lctx->more || length > lctx->n201) {
1237 LOGDL(dl, LOGL_ERROR, "UA too large error\n");
1238 msgb_free(msg);
1239 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1240 return -EIO;
1241 }
1242
1243 if (!lctx->p_f) {
1244 /* 5.4.1.2 A UA response with the F bit set to "0"
1245 * shall be ignored.
1246 */
1247 LOGDL(dl, LOGL_INFO, "F=0 (discarding)\n");
1248 msgb_free(msg);
1249 return 0;
1250 }
1251 switch (dl->state) {
1252 case LAPD_STATE_SABM_SENT:
1253 break;
1254 case LAPD_STATE_MF_EST:
1255 case LAPD_STATE_TIMER_RECOV:
1256 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1257 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1258 msgb_free(msg);
1259 return 0;
1260 case LAPD_STATE_DISC_SENT:
1261 LOGDL(dl, LOGL_INFO, "UA in disconnect state\n");
1262 /* stop Timer T200 */
1263 lapd_stop_t200(dl);
1264 /* go to idle state */
1265 lapd_dl_flush_tx(dl);
1266 lapd_dl_flush_send(dl);
1267 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1268 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1269 msgb_free(msg);
1270 return 0;
1271 case LAPD_STATE_IDLE:
1272 /* 5.4.5 all other frame types shall be discarded */
1273 default:
1274 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1275 msgb_free(msg);
1276 return 0;
1277 }
1278 LOGDL(dl, LOGL_INFO, "UA in SABM state\n");
1279 /* stop Timer T200 */
1280 lapd_stop_t200(dl);
1281 /* compare UA with SABME if contention resolution is applied */
1282 if (dl->tx_hist[0].msg->len) {
1283 if (length != (dl->tx_hist[0].msg->len)
1284 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1285 length)) {
1286 LOGDL(dl, LOGL_INFO, "**** UA response mismatches ****\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001287 /* go to idle state */
1288 lapd_dl_flush_tx(dl);
1289 lapd_dl_flush_send(dl);
1290 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
Harald Welted2a61172020-12-21 17:43:54 +01001291 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1292 msgb_free(msg);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001293 return 0;
1294 }
1295 }
1296 /* set Vs, Vr and Va to 0 */
1297 dl->v_send = dl->v_recv = dl->v_ack = 0;
1298 /* clear tx_hist */
1299 lapd_dl_flush_hist(dl);
1300 /* enter multiple-frame-established state */
1301 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1302 /* send outstanding frames, if any (resume / reconnect) */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001303 lapd_send_i(dl, __LINE__, false);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001304 /* send notification to L3 */
1305 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1306 msgb_free(msg);
1307 return rc;
1308}
1309
1310/* Receive a LAPD U FRMR message from L1 */
1311static int lapd_rx_u_frmr(struct msgb *msg, struct lapd_msg_ctx *lctx)
1312{
1313 struct lapd_datalink *dl = lctx->dl;
1314
1315 LOGDL(dl, LOGL_NOTICE, "Frame reject received\n");
1316 /* send MDL ERROR INIDCATION to L3 */
1317 mdl_error(MDL_CAUSE_FRMR, lctx);
1318 msgb_free(msg);
1319 /* reestablish */
1320 if (!dl->reestablish)
1321 return 0;
1322 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1323 return lapd_reestablish(dl);
1324}
1325
1326/* Receive a LAPD U (Unnumbered) message from L1 */
1327static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
1328{
1329 switch (lctx->s_u) {
1330 case LAPD_U_SABM:
1331 case LAPD_U_SABME:
1332 return lapd_rx_u_sabm(msg, lctx);
1333 case LAPD_U_DM:
1334 return lapd_rx_u_dm(msg, lctx);
1335 case LAPD_U_UI:
1336 return lapd_rx_u_ui(msg, lctx);
1337 case LAPD_U_DISC:
1338 return lapd_rx_u_disc(msg, lctx);
1339 case LAPD_U_UA:
1340 return lapd_rx_u_ua(msg, lctx);
1341 case LAPD_U_FRMR:
1342 return lapd_rx_u_frmr(msg, lctx);
1343 default:
1344 /* G.3.1 */
1345 LOGDL(lctx->dl, LOGL_NOTICE, "Unnumbered frame not allowed\n");
1346 msgb_free(msg);
1347 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1348 return -EINVAL;
1349 }
1350}
1351
rootaf48bed2011-09-26 11:23:06 +02001352/* Receive a LAPD S (Supervisory) message from L1 */
1353static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1354{
1355 struct lapd_datalink *dl = lctx->dl;
1356 int length = lctx->length;
1357
1358 if (length > 0 || lctx->more) {
1359 /* G.4.3 If a supervisory frame is received with L>0 or
1360 * with the M bit set to "1", an MDL-ERROR-INDICATION
1361 * primitive with cause "S frame with incorrect
1362 * parameters" is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001363 LOGDL(dl, LOGL_ERROR, "S frame with incorrect parameters\n");
rootaf48bed2011-09-26 11:23:06 +02001364 msgb_free(msg);
1365 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1366 return -EIO;
1367 }
1368
1369 if (lctx->cr == dl->cr.rem2loc.resp
1370 && lctx->p_f
1371 && dl->state != LAPD_STATE_TIMER_RECOV) {
1372 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001373 LOGDL(dl, LOGL_NOTICE, "S frame response with F=1 error\n");
rootaf48bed2011-09-26 11:23:06 +02001374 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1375 }
1376
1377 switch (dl->state) {
1378 case LAPD_STATE_IDLE:
1379 /* if P=1, respond DM with F=1 (5.2.2) */
1380 /* 5.4.5 all other frame types shall be discarded */
1381 if (lctx->p_f)
1382 lapd_send_dm(lctx); /* F=P */
1383 /* fall though */
1384 case LAPD_STATE_SABM_SENT:
1385 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001386 LOGDL(dl, LOGL_NOTICE, "S frame ignored in this state\n");
rootaf48bed2011-09-26 11:23:06 +02001387 msgb_free(msg);
1388 return 0;
1389 }
1390 switch (lctx->s_u) {
1391 case LAPD_S_RR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001392 LOGDL(dl, LOGL_INFO, "RR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001393 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1394 lapd_acknowledge(lctx);
1395
1396 /* 5.5.3.2 */
1397 if (lctx->cr == dl->cr.rem2loc.cmd
1398 && lctx->p_f) {
1399 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001400 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1401 "we are not busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001402 lapd_send_rr(lctx, 1, 0);
1403 /* NOTE: In case of sequence error condition,
1404 * the REJ frame has been transmitted when
1405 * entering the condition, so it has not be
1406 * done here
1407 */
1408 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001409 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1410 "we are busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001411 lapd_send_rnr(lctx, 1, 0);
1412 }
1413 } else if (lctx->cr == dl->cr.rem2loc.resp
1414 && lctx->p_f
1415 && dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001416 LOGDL(dl, LOGL_INFO, "RR response with F==1, and we are in timer recovery "
1417 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001418 /* V(S) to the N(R) in the RR frame */
1419 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001420 /* stop Timer T200 */
1421 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001422 /* 5.5.7 Clear timer recovery condition */
1423 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1424 }
1425 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001426 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001427
1428 break;
1429 case LAPD_S_RNR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001430 LOGDL(dl, LOGL_INFO, "RNR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001431 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1432 lapd_acknowledge(lctx);
1433
1434 /* 5.5.5 */
1435 /* Set peer receiver busy condition */
1436 dl->peer_busy = 1;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001437 /* Flush pending messages in TX queue. */
1438 lapd_dl_flush_tx_queue(dl);
1439 /* stop Timer T200 */
1440 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001441
1442 if (lctx->p_f) {
1443 if (lctx->cr == dl->cr.rem2loc.cmd) {
1444 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001445 LOGDL(dl, LOGL_INFO, "RNR poll command and we are not busy, "
1446 "so we reply with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001447 /* Send RR with F=1 */
1448 lapd_send_rr(lctx, 1, 0);
1449 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001450 LOGDL(dl, LOGL_INFO, "RNR poll command and we are busy, so "
1451 "we reply with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001452 /* Send RNR with F=1 */
1453 lapd_send_rnr(lctx, 1, 0);
1454 }
1455 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001456 LOGDL(dl, LOGL_INFO, "RNR poll response and we in timer recovery "
1457 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001458 /* 5.5.7 Clear timer recovery condition */
1459 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1460 /* V(S) to the N(R) in the RNR frame */
1461 dl->v_send = lctx->n_recv;
1462 }
1463 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001464 LOGDL(dl, LOGL_INFO, "RNR not polling/final state received\n");
rootaf48bed2011-09-26 11:23:06 +02001465
1466 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001467 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001468
1469 break;
1470 case LAPD_S_REJ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001471 LOGDL(dl, LOGL_INFO, "REJ received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001472 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1473 lapd_acknowledge(lctx);
1474
1475 /* 5.5.4.1 */
1476 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1477 /* Clear an existing peer receiver busy condition */
1478 dl->peer_busy = 0;
1479 /* V(S) and V(A) to the N(R) in the REJ frame */
1480 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001481 /* Flush pending messages in TX queue. */
1482 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001483 /* stop Timer T200 */
1484 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001485 /* 5.5.3.2 */
1486 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1487 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001488 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1489 "state and not in own busy condition received, so we "
1490 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001491 lapd_send_rr(lctx, 1, 0);
1492 /* NOTE: In case of sequence error
1493 * condition, the REJ frame has been
1494 * transmitted when entering the
1495 * condition, so it has not be done
1496 * here
1497 */
1498 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001499 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1500 "state and in own busy condition received, so we "
1501 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001502 lapd_send_rnr(lctx, 1, 0);
1503 }
1504 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001505 LOGDL(dl, LOGL_INFO, "REJ response or not polling command not "
1506 "in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001507 /* send MDL ERROR INIDCATION to L3 */
1508 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001509 LOGDL(dl, LOGL_ERROR, "unsolicited supervisory response!\n");
rootaf48bed2011-09-26 11:23:06 +02001510 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1511 }
1512
1513 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001514 LOGDL(dl, LOGL_INFO, "REJ poll response in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001515 /* Clear an existing peer receiver busy condition */
1516 dl->peer_busy = 0;
1517 /* V(S) and V(A) to the N(R) in the REJ frame */
1518 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001519 /* Flush pending messages in TX queue. */
1520 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001521 /* stop Timer T200 */
1522 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001523 /* 5.5.7 Clear timer recovery condition */
1524 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1525 } else {
1526 /* Clear an existing peer receiver busy condition */
1527 dl->peer_busy = 0;
1528 /* V(S) and V(A) to the N(R) in the REJ frame */
1529 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001530 /* Flush pending messages in TX queue. */
1531 lapd_dl_flush_tx_queue(dl);
rootaf48bed2011-09-26 11:23:06 +02001532 /* 5.5.3.2 */
1533 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1534 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001535 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1536 "state and not in own busy condition received, so we "
1537 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001538 lapd_send_rr(lctx, 1, 0);
1539 /* NOTE: In case of sequence error
1540 * condition, the REJ frame has been
1541 * transmitted when entering the
1542 * condition, so it has not be done
1543 * here
1544 */
1545 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001546 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1547 "state and in own busy condition received, so we "
1548 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001549 lapd_send_rnr(lctx, 1, 0);
1550 }
1551 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001552 LOGDL(dl, LOGL_INFO, "REJ response or not polling command in "
1553 "timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001554 }
1555
1556 /* FIXME: 5.5.4.2 2) */
1557
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001558 /* Send message, if possible due to acknowledged data and new V(S) and V(A). */
1559 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001560
1561 break;
1562 default:
1563 /* G.3.1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001564 LOGDL(dl, LOGL_ERROR, "Supervisory frame not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001565 msgb_free(msg);
1566 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1567 return -EINVAL;
1568 }
1569 msgb_free(msg);
1570 return 0;
1571}
1572
1573/* Receive a LAPD I (Information) message from L1 */
1574static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1575{
1576 struct lapd_datalink *dl = lctx->dl;
1577 //uint8_t nr = lctx->n_recv;
1578 uint8_t ns = lctx->n_send;
1579 int length = lctx->length;
1580 int rc;
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001581 bool i_frame_in_queue = false;
Andreas Eversberg6cc53012023-11-16 11:58:49 +01001582 int mdl_cause = 0;
rootaf48bed2011-09-26 11:23:06 +02001583
Harald Welte00b2faf2020-05-02 19:56:36 +02001584 LOGDL(dl, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1585 lapd_state_name(dl->state), lctx->sapi);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001586
rootaf48bed2011-09-26 11:23:06 +02001587 /* G.2.2 Wrong value of the C/R bit */
1588 if (lctx->cr == dl->cr.rem2loc.resp) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001589 LOGDL(dl, LOGL_ERROR, "I frame response not allowed (state %s)\n",
1590 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001591 msgb_free(msg);
1592 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1593 return -EINVAL;
1594 }
1595
1596 if (length == 0 || length > lctx->n201) {
1597 /* G.4.2 If the length indicator of an I frame is set
1598 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1599 * primitive with cause "I frame with incorrect length"
1600 * is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001601 LOGDL(dl, LOGL_ERROR, "I frame length not allowed (state %s)\n",
1602 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001603 msgb_free(msg);
1604 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1605 return -EIO;
1606 }
1607
1608 /* G.4.2 If the numerical value of L is L<N201 and the M
1609 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1610 * cause "I frame with incorrect use of M bit" is sent to the
1611 * mobile management entity. */
1612 if (lctx->more && length < lctx->n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001613 LOGDL(dl, LOGL_ERROR, "I frame with M bit too short (state %s)\n",
1614 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001615 msgb_free(msg);
1616 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1617 return -EIO;
1618 }
1619
1620 switch (dl->state) {
1621 case LAPD_STATE_IDLE:
1622 /* if P=1, respond DM with F=1 (5.2.2) */
1623 /* 5.4.5 all other frame types shall be discarded */
1624 if (lctx->p_f)
1625 lapd_send_dm(lctx); /* F=P */
1626 /* fall though */
1627 case LAPD_STATE_SABM_SENT:
1628 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001629 LOGDL(dl, LOGL_NOTICE, "I frame ignored in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001630 msgb_free(msg);
1631 return 0;
1632 }
1633
1634 /* 5.7.1: N(s) sequence error */
1635 if (ns != dl->v_recv) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001636 LOGDL(dl, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, V(R)=%u (state %s)\n",
1637 ns, dl->v_recv, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001638 /* discard data */
1639 msgb_free(msg);
Andreas Eversbergbd2b8972023-11-15 14:33:53 +01001640 /* Send reject, but suppress second reject if LAPD_F_DROP_2ND_REJ flag is set. */
1641 if (dl->seq_err_cond != 1 || !(dl->lapd_flags & LAPD_F_DROP_2ND_REJ)) {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001642 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001643 lapd_send_rej(lctx, lctx->p_f);
1644 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001645 /* If there are two subsequent sequence errors received,
1646 * ignore it. (Ignore every second subsequent error.)
1647 * This happens if our reply with the REJ is too slow,
1648 * so the remote gets a T200 timeout and sends another
1649 * frame with a sequence error.
1650 * Test showed that replying with two subsequent REJ
1651 * messages could the remote L2 process to abort.
1652 * Replying too slow shouldn't happen, but may happen
1653 * over serial link between BB and LAPD.
1654 */
1655 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001656 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001657 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1658 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
Andreas Eversberg6cc53012023-11-16 11:58:49 +01001659 mdl_cause = lapd_acknowledge(lctx); /* V(A) is also set here */
1660 if (mdl_cause < 0)
1661 mdl_error(-mdl_cause, lctx);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001662
1663 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001664 lapd_send_i(dl, __LINE__, false);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001665
1666 return 0;
rootaf48bed2011-09-26 11:23:06 +02001667 }
1668 dl->seq_err_cond = 0;
1669
1670 /* Increment receiver state */
1671 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Harald Welte00b2faf2020-05-02 19:56:36 +02001672 LOGDL(dl, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
rootaf48bed2011-09-26 11:23:06 +02001673
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001674 /* Update all pending frames in the queue to the new V(R) state. */
1675 if (dl->update_pending_frames) {
1676 rc = dl->update_pending_frames(lctx);
1677 if (!rc)
1678 i_frame_in_queue = true;
1679 }
1680
rootaf48bed2011-09-26 11:23:06 +02001681 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
Andreas Eversberg6cc53012023-11-16 11:58:49 +01001682 mdl_cause = lapd_acknowledge(lctx); /* V(A) is also set here */
rootaf48bed2011-09-26 11:23:06 +02001683
1684 /* Only if we are not in own receiver busy condition */
1685 if (!dl->own_busy) {
1686 /* if the frame carries a complete segment */
1687 if (!lctx->more && !dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001688 LOGDL(dl, LOGL_INFO, "message in single I frame\n");
rootaf48bed2011-09-26 11:23:06 +02001689 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001690 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001691 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1692 msg);
1693 } else {
1694 /* create rcv_buffer */
1695 if (!dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001696 LOGDL(dl, LOGL_INFO, "message in multiple I frames (first message)\n");
rootaf48bed2011-09-26 11:23:06 +02001697 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1698 "LAPD RX");
1699 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1700 }
1701 /* concat. rcv_buffer */
1702 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001703 LOGDL(dl, LOGL_NOTICE, "Received frame overflow!\n");
rootaf48bed2011-09-26 11:23:06 +02001704 } else {
1705 memcpy(msgb_put(dl->rcv_buffer, length),
1706 msg->l3h, length);
1707 }
1708 /* if the last segment was received */
1709 if (!lctx->more) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001710 LOGDL(dl, LOGL_INFO, "message in multiple I frames (last message)\n");
rootaf48bed2011-09-26 11:23:06 +02001711 rc = send_dl_l3(PRIM_DL_DATA,
1712 PRIM_OP_INDICATION, lctx,
1713 dl->rcv_buffer);
1714 dl->rcv_buffer = NULL;
1715 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001716 LOGDL(dl, LOGL_INFO, "message in multiple I frames (next message)\n");
rootaf48bed2011-09-26 11:23:06 +02001717 msgb_free(msg);
1718
1719 }
Harald Weltebc1d7152020-07-04 10:52:13 +02001720 /* the L3 or higher (called in-line above via send_dl_l3) might have destroyed the
1721 * data link meanwhile. See OS#1761 */
1722 if (dl->state == LAPD_STATE_NULL)
1723 return 0;
rootaf48bed2011-09-26 11:23:06 +02001724 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001725 LOGDL(dl, LOGL_INFO, "I frame ignored during own receiver busy condition\n");
rootaf48bed2011-09-26 11:23:06 +02001726
Andreas Eversberg6cc53012023-11-16 11:58:49 +01001727 /* Indicate sequence error, if exists. */
1728 if (mdl_cause < 0)
1729 mdl_error(-mdl_cause, lctx);
1730
rootaf48bed2011-09-26 11:23:06 +02001731 /* Check for P bit */
1732 if (lctx->p_f) {
1733 /* 5.5.2.1 */
1734 /* check if we are not in own receiver busy */
1735 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001736 LOGDL(dl, LOGL_INFO, "we are not busy, send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001737 /* Send RR with F=1 */
1738 rc = lapd_send_rr(lctx, 1, 0);
1739 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001740 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001741 /* Send RNR with F=1 */
1742 rc = lapd_send_rnr(lctx, 1, 0);
1743 }
1744 } else {
1745 /* 5.5.2.2 */
1746 /* check if we are not in own receiver busy */
1747 if (!dl->own_busy) {
1748 /* NOTE: V(R) is already set above */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001749 rc = lapd_send_i(dl, __LINE__, false);
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001750 if (rc && !i_frame_in_queue) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001751 LOGDL(dl, LOGL_INFO, "we are not busy and have no pending data, "
1752 "send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001753 /* Send RR with F=0 */
1754 return lapd_send_rr(lctx, 0, 0);
1755 }
1756 /* all I or one RR is sent, we are done */
1757 return 0;
1758 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001759 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001760 /* Send RNR with F=0 */
1761 rc = lapd_send_rnr(lctx, 0, 0);
1762 }
1763 }
1764
1765 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001766 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001767
1768 return rc;
1769}
1770
1771/* Receive a LAPD message from L1 */
1772int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1773{
1774 int rc;
1775
1776 switch (lctx->format) {
1777 case LAPD_FORM_U:
1778 rc = lapd_rx_u(msg, lctx);
1779 break;
1780 case LAPD_FORM_S:
1781 rc = lapd_rx_s(msg, lctx);
1782 break;
1783 case LAPD_FORM_I:
1784 rc = lapd_rx_i(msg, lctx);
1785 break;
1786 default:
Maxc5695262022-10-09 17:41:46 +03001787 LOGDL(lctx->dl, LOGL_NOTICE, "unknown LAPD format 0x%02x\n", lctx->format);
rootaf48bed2011-09-26 11:23:06 +02001788 msgb_free(msg);
1789 rc = -EINVAL;
1790 }
1791 return rc;
1792}
1793
Andreas Eversberg49b70872023-11-09 12:33:13 +01001794/*! Enqueue next LAPD frame and run pending T200. (Must be called when frame is ready to send.)
1795 * The caller (LAPDm code) calls this function before it sends the next frame.
1796 * If there is no frame in the TX queue, LAPD will enqueue next I-frame, if possible.
1797 * If the T200 is pending, it is changed to running state.
1798 * \param[in] lctx LAPD context
1799 * \param[out] rc set to 1, if timer T200 state changed to running, set to 0, if not. */
1800int lapd_ph_rts_ind(struct lapd_msg_ctx *lctx)
1801{
1802 struct lapd_datalink *dl = lctx->dl;
1803
1804 /* If there is no pending frame, try to enqueue next I frame. */
1805 if (llist_empty(&dl->tx_queue) && (dl->state == LAPD_STATE_MF_EST || dl->state == LAPD_STATE_TIMER_RECOV)) {
1806 /* Send an I frame, if there are pending outgoing messages. */
1807 lapd_send_i(dl, __LINE__, true);
1808 }
1809
1810 /* Run T200 at RTS, if pending. Tell caller that is has been started. (rc = 1) */
1811 if (dl->t200_rts == LAPD_T200_RTS_PENDING) {
1812 dl->t200_rts = LAPD_T200_RTS_RUNNING;
1813 return 1;
1814 }
1815
1816 return 0;
1817}
1818
rootaf48bed2011-09-26 11:23:06 +02001819/* L3 -> L2 */
1820
1821/* send unit data */
1822static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1823{
1824 struct lapd_datalink *dl = lctx->dl;
1825 struct msgb *msg = dp->oph.msg;
1826 struct lapd_msg_ctx nctx;
1827
1828 memcpy(&nctx, lctx, sizeof(nctx));
1829 /* keep nctx.ldp */
1830 /* keep nctx.sapi */
1831 /* keep nctx.tei */
1832 nctx.cr = dl->cr.loc2rem.cmd;
1833 nctx.format = LAPD_FORM_U;
1834 nctx.s_u = LAPD_U_UI;
1835 /* keep nctx.p_f */
1836 nctx.length = msg->len;
1837 nctx.more = 0;
1838
1839 return dl->send_ph_data_req(&nctx, msg);
1840}
1841
Max2b283b12022-11-17 22:18:01 +03001842static void msg_to_tx_hist(struct lapd_history *tx_hist, const struct msgb *msg, int length, int more)
1843{
1844 tx_hist->msg = lapd_msgb_alloc(msg->len, "HIST");
1845 tx_hist->more = more;
1846 msgb_put(tx_hist->msg, msg->len);
1847 if (length)
1848 memcpy(tx_hist->msg->data, msg->l3h, msg->len);
1849}
1850
1851static void msg_to_tx_hist0(struct lapd_datalink *dl, const struct msgb *msg)
1852{
1853 return msg_to_tx_hist(&dl->tx_hist[0], msg, msg->len, 0);
1854}
1855
rootaf48bed2011-09-26 11:23:06 +02001856/* request link establishment */
1857static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1858{
1859 struct lapd_datalink *dl = lctx->dl;
1860 struct msgb *msg = dp->oph.msg;
1861 struct lapd_msg_ctx nctx;
1862
1863 if (msg->len)
Harald Welte00b2faf2020-05-02 19:56:36 +02001864 LOGDL(dl, LOGL_INFO, "perform establishment with content (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001865 else
Harald Welte00b2faf2020-05-02 19:56:36 +02001866 LOGDL(dl, LOGL_INFO, "perform normal establishm. (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001867
1868 /* Flush send-queue */
1869 /* Clear send-buffer */
1870 lapd_dl_flush_send(dl);
1871 /* be sure that history is empty */
1872 lapd_dl_flush_hist(dl);
1873
1874 /* save message context for further use */
1875 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1876
1877 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001878 msgb_free(dl->rcv_buffer);
1879 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001880
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_U;
1888 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1889 nctx.p_f = 1;
1890 nctx.length = msg->len;
1891 nctx.more = 0;
1892
1893 /* Transmit-buffer carries exactly one segment */
Max2b283b12022-11-17 22:18:01 +03001894 msg_to_tx_hist0(dl, msg);
1895
rootaf48bed2011-09-26 11:23:06 +02001896 /* set Vs to 0, because it is used as index when resending SABM */
1897 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001898
rootaf48bed2011-09-26 11:23:06 +02001899 /* Set states */
1900 dl->own_busy = dl->peer_busy = 0;
1901 dl->retrans_ctr = 0;
1902 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1903
1904 /* Tramsmit and start T200 */
1905 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001906 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001907
1908 return 0;
1909}
1910
1911/* send data */
1912static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1913{
1914 struct lapd_datalink *dl = lctx->dl;
1915 struct msgb *msg = dp->oph.msg;
1916
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001917 if (msgb_l3len(msg) == 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001918 LOGDL(dl, LOGL_ERROR, "writing an empty message is not possible\n");
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001919 msgb_free(msg);
1920 return -1;
1921 }
1922
Harald Welte00b2faf2020-05-02 19:56:36 +02001923 LOGDL(dl, LOGL_INFO, "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001924
1925 /* Write data into the send queue */
1926 msgb_enqueue(&dl->send_queue, msg);
1927
1928 /* Send message, if possible */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001929 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001930
1931 return 0;
1932}
1933
1934/* Send next I frame from queued/buffered data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001935static int lapd_send_i(struct lapd_datalink *dl, int line, bool rts)
rootaf48bed2011-09-26 11:23:06 +02001936{
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001937 struct lapd_msg_ctx *lctx = &dl->lctx;
rootaf48bed2011-09-26 11:23:06 +02001938 uint8_t k = dl->k;
1939 uint8_t h;
1940 struct msgb *msg;
1941 int length, left;
1942 int rc = - 1; /* we sent nothing */
1943 struct lapd_msg_ctx nctx;
1944
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001945 if (!rts)
1946 LOGDL(dl, LOGL_INFO, "%s() called from line %d\n", __func__, line);
rootaf48bed2011-09-26 11:23:06 +02001947
Andreas Eversberg49b70872023-11-09 12:33:13 +01001948 if ((dl->lapd_flags & LAPD_F_RTS) && !llist_empty(&dl->tx_queue)) {
1949 if (!rts)
1950 LOGDL(dl, LOGL_INFO, "There is a frame in the TX queue, not checking for sending I frame.\n");
1951 return rc;
1952 }
1953
rootaf48bed2011-09-26 11:23:06 +02001954 next_frame:
1955
1956 if (dl->peer_busy) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001957 if (!rts)
1958 LOGDL(dl, LOGL_INFO, "Peer busy, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001959 return rc;
1960 }
1961
1962 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001963 if (!rts)
1964 LOGDL(dl, LOGL_INFO, "Timer recovery, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001965 return rc;
1966 }
1967
1968 /* If the send state variable V(S) is equal to V(A) plus k
1969 * (where k is the maximum number of outstanding I frames - see
1970 * subclause 5.8.4), the data link layer entity shall not transmit any
1971 * new I frames, but shall retransmit an I frame as a result
1972 * of the error recovery procedures as described in subclauses 5.5.4 and
1973 * 5.5.7. */
1974 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001975 if (!rts)
1976 LOGDL(dl, LOGL_INFO, "k frames outstanding, not sending more. (k=%u V(S)=%u V(A)=%u)\n",
1977 k, dl->v_send, dl->v_ack);
rootaf48bed2011-09-26 11:23:06 +02001978 return rc;
1979 }
1980
1981 h = do_mod(dl->v_send, dl->range_hist);
1982
1983 /* if we have no tx_hist yet, we create it */
1984 if (!dl->tx_hist[h].msg) {
1985 /* Get next message into send-buffer, if any */
1986 if (!dl->send_buffer) {
1987 next_message:
1988 dl->send_out = 0;
1989 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1990 /* No more data to be sent */
1991 if (!dl->send_buffer)
1992 return rc;
Harald Welte00b2faf2020-05-02 19:56:36 +02001993 LOGDL(dl, LOGL_INFO, "get message from send-queue\n");
rootaf48bed2011-09-26 11:23:06 +02001994 }
1995
1996 /* How much is left in the send-buffer? */
1997 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1998 /* Segment, if data exceeds N201 */
1999 length = left;
2000 if (length > lctx->n201)
2001 length = lctx->n201;
Harald Welte00b2faf2020-05-02 19:56:36 +02002002 LOGDL(dl, LOGL_INFO, "msg-len %d sent %d left %d N201 %d length %d "
2003 "first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left,
2004 lctx->n201, length, dl->send_buffer->l3h[0]);
rootaf48bed2011-09-26 11:23:06 +02002005 /* If message in send-buffer is completely sent */
2006 if (left == 0) {
2007 msgb_free(dl->send_buffer);
2008 dl->send_buffer = NULL;
2009 goto next_message;
2010 }
2011
Harald Welte00b2faf2020-05-02 19:56:36 +02002012 LOGDL(dl, LOGL_INFO, "send I frame %sV(S)=%d\n",
2013 (left > length) ? "segment " : "", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02002014
2015 /* Create I frame (segment) and transmit-buffer content */
2016 msg = lapd_msgb_alloc(length, "LAPD I");
2017 msg->l3h = msgb_put(msg, length);
2018 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01002019 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02002020 /* keep nctx.ldp */
2021 /* keep nctx.sapi */
2022 /* keep nctx.tei */
2023 nctx.cr = dl->cr.loc2rem.cmd;
2024 nctx.format = LAPD_FORM_I;
2025 nctx.p_f = 0;
2026 nctx.n_send = dl->v_send;
2027 nctx.n_recv = dl->v_recv;
2028 nctx.length = length;
2029 if (left > length)
2030 nctx.more = 1;
2031 else
2032 nctx.more = 0;
2033 if (length)
2034 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
2035 length);
2036 /* store in tx_hist */
Max2b283b12022-11-17 22:18:01 +03002037 msg_to_tx_hist(&dl->tx_hist[h], msg, length, nctx.more);
2038
rootaf48bed2011-09-26 11:23:06 +02002039 /* Add length to track how much is already in the tx buffer */
2040 dl->send_out += length;
2041 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02002042 LOGDL(dl, LOGL_INFO, "resend I frame from tx buffer V(S)=%d\n", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02002043
2044 /* Create I frame (segment) from tx_hist */
2045 length = dl->tx_hist[h].msg->len;
2046 msg = lapd_msgb_alloc(length, "LAPD I resend");
2047 msg->l3h = msgb_put(msg, length);
2048 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01002049 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02002050 /* keep nctx.ldp */
2051 /* keep nctx.sapi */
2052 /* keep nctx.tei */
2053 nctx.cr = dl->cr.loc2rem.cmd;
2054 nctx.format = LAPD_FORM_I;
2055 nctx.p_f = 0;
2056 nctx.n_send = dl->v_send;
2057 nctx.n_recv = dl->v_recv;
2058 nctx.length = length;
2059 nctx.more = dl->tx_hist[h].more;
2060 if (length)
2061 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
2062 }
2063
2064 /* The value of the send state variable V(S) shall be incremented by 1
2065 * at the end of the transmission of the I frame */
2066 dl->v_send = inc_mod(dl->v_send, dl->v_range);
2067
2068 /* If timer T200 is not running at the time right before transmitting a
2069 * frame, when the PH-READY-TO-SEND primitive is received from the
2070 * physical layer., it shall be set. */
Andreas Eversberg49b70872023-11-09 12:33:13 +01002071 if (!lapd_is_t200_started(dl)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02002072 /* stop Timer T203, if running */
2073 lapd_stop_t203(dl);
2074 /* start Timer T200 */
2075 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002076 }
2077
2078 dl->send_ph_data_req(&nctx, msg);
2079
Andreas Eversberg49b70872023-11-09 12:33:13 +01002080 /* When using RTS, we send only one frame. */
2081 if ((dl->lapd_flags & LAPD_F_RTS))
2082 return 0;
2083
2084 rc = 0; /* We sent an I frame, so sending RR frame is not required. */
rootaf48bed2011-09-26 11:23:06 +02002085 goto next_frame;
2086}
2087
2088/* request link suspension */
2089static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2090{
2091 struct lapd_datalink *dl = lctx->dl;
2092 struct msgb *msg = dp->oph.msg;
2093
Harald Welte00b2faf2020-05-02 19:56:36 +02002094 LOGDL(dl, LOGL_INFO, "perform suspension\n");
rootaf48bed2011-09-26 11:23:06 +02002095
2096 /* put back the send-buffer to the send-queue (first position) */
2097 if (dl->send_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002098 LOGDL(dl, LOGL_INFO, "put frame in sendbuffer back to queue\n");
rootaf48bed2011-09-26 11:23:06 +02002099 llist_add(&dl->send_buffer->list, &dl->send_queue);
2100 dl->send_buffer = NULL;
2101 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02002102 LOGDL(dl, LOGL_INFO, "no frame in sendbuffer\n");
rootaf48bed2011-09-26 11:23:06 +02002103
2104 /* Clear transmit buffer, but keep send buffer */
2105 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002106 /* Stop timers (there is no state change, so we must stop all timers */
2107 lapd_stop_t200(dl);
2108 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02002109
2110 msgb_free(msg);
2111
2112 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
2113}
2114
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002115/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002116static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2117{
2118 struct lapd_datalink *dl = lctx->dl;
2119 struct msgb *msg = dp->oph.msg;
2120 struct lapd_msg_ctx nctx;
2121
Harald Welte00b2faf2020-05-02 19:56:36 +02002122 LOGDL(dl, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", msg->len);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002123
rootaf48bed2011-09-26 11:23:06 +02002124 /* be sure that history is empty */
2125 lapd_dl_flush_hist(dl);
2126
2127 /* save message context for further use */
2128 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2129
2130 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002131 msgb_free(dl->send_buffer);
2132 dl->send_buffer = NULL;
2133
rootaf48bed2011-09-26 11:23:06 +02002134 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002135 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002136 /* Write data into the send buffer, to be sent first */
2137 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002138 } else {
2139 msgb_free(msg);
2140 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002141 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002142 }
rootaf48bed2011-09-26 11:23:06 +02002143
2144 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002145 msgb_free(dl->rcv_buffer);
2146 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002147
2148 /* Create new msgb (old one is now free) */
2149 msg = lapd_msgb_alloc(0, "LAPD SABM");
2150 msg->l3h = msg->data;
2151 /* assemble message */
2152 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2153 /* keep nctx.ldp */
2154 /* keep nctx.sapi */
2155 /* keep nctx.tei */
2156 nctx.cr = dl->cr.loc2rem.cmd;
2157 nctx.format = LAPD_FORM_U;
2158 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2159 nctx.p_f = 1;
2160 nctx.length = 0;
2161 nctx.more = 0;
2162
Max2b283b12022-11-17 22:18:01 +03002163 msg_to_tx_hist0(dl, msg);
2164
rootaf48bed2011-09-26 11:23:06 +02002165 /* set Vs to 0, because it is used as index when resending SABM */
2166 dl->v_send = 0;
2167
2168 /* Set states */
2169 dl->own_busy = dl->peer_busy = 0;
2170 dl->retrans_ctr = 0;
2171 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2172
2173 /* Tramsmit and start T200 */
2174 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002175 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002176
2177 return 0;
2178}
2179
2180/* requesst release of link */
2181static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2182{
2183 struct lapd_datalink *dl = lctx->dl;
2184 struct msgb *msg = dp->oph.msg;
2185 struct lapd_msg_ctx nctx;
2186
2187 /* local release */
2188 if (dp->u.rel_req.mode) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002189 LOGDL(dl, LOGL_INFO, "perform local release\n");
rootaf48bed2011-09-26 11:23:06 +02002190 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002191 /* stop Timer T200 */
2192 lapd_stop_t200(dl);
2193 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002194 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2195 /* flush buffers */
2196 lapd_dl_flush_tx(dl);
2197 lapd_dl_flush_send(dl);
2198 /* send notification to L3 */
2199 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2200 }
2201
2202 /* in case we are already disconnecting */
2203 if (dl->state == LAPD_STATE_DISC_SENT)
2204 return -EBUSY;
2205
2206 /* flush tx_hist */
2207 lapd_dl_flush_hist(dl);
2208
Harald Welte00b2faf2020-05-02 19:56:36 +02002209 LOGDL(dl, LOGL_INFO, "perform normal release (DISC)\n");
rootaf48bed2011-09-26 11:23:06 +02002210
2211 /* Push LAPD header on msgb */
2212 /* assemble message */
2213 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2214 /* keep nctx.ldp */
2215 /* keep nctx.sapi */
2216 /* keep nctx.tei */
2217 nctx.cr = dl->cr.loc2rem.cmd;
2218 nctx.format = LAPD_FORM_U;
2219 nctx.s_u = LAPD_U_DISC;
2220 nctx.p_f = 1;
2221 nctx.length = 0;
2222 nctx.more = 0;
2223
Max2b283b12022-11-17 22:18:01 +03002224 msg_to_tx_hist0(dl, msg);
2225
rootaf48bed2011-09-26 11:23:06 +02002226 /* set Vs to 0, because it is used as index when resending DISC */
2227 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002228
rootaf48bed2011-09-26 11:23:06 +02002229 /* Set states */
2230 dl->own_busy = dl->peer_busy = 0;
2231 dl->retrans_ctr = 0;
2232 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2233
2234 /* Tramsmit and start T200 */
2235 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002236 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002237
2238 return 0;
2239}
2240
2241/* request release of link in idle state */
2242static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2243 struct lapd_msg_ctx *lctx)
2244{
2245 struct lapd_datalink *dl = lctx->dl;
2246 struct msgb *msg = dp->oph.msg;
2247
2248 msgb_free(msg);
2249
2250 /* send notification to L3 */
2251 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2252}
2253
2254/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002255static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002256 uint32_t states;
2257 int prim, op;
2258 const char *name;
2259 int (*rout) (struct osmo_dlsap_prim *dp,
2260 struct lapd_msg_ctx *lctx);
2261} l2downstatelist[] = {
2262 /* create and send UI command */
2263 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002264 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002265 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2266
2267 /* create and send SABM command */
2268 {SBIT(LAPD_STATE_IDLE),
2269 PRIM_DL_EST, PRIM_OP_REQUEST,
2270 "DL-ESTABLISH-REQUEST", lapd_est_req},
2271
2272 /* create and send I command */
2273 {SBIT(LAPD_STATE_MF_EST) |
2274 SBIT(LAPD_STATE_TIMER_RECOV),
2275 PRIM_DL_DATA, PRIM_OP_REQUEST,
2276 "DL-DATA-REQUEST", lapd_data_req},
2277
2278 /* suspend datalink */
2279 {SBIT(LAPD_STATE_MF_EST) |
2280 SBIT(LAPD_STATE_TIMER_RECOV),
2281 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2282 "DL-SUSPEND-REQUEST", lapd_susp_req},
2283
2284 /* create and send SABM command (resume) */
2285 {SBIT(LAPD_STATE_MF_EST) |
2286 SBIT(LAPD_STATE_TIMER_RECOV),
2287 PRIM_DL_RES, PRIM_OP_REQUEST,
2288 "DL-RESUME-REQUEST", lapd_res_req},
2289
2290 /* create and send SABM command (reconnect) */
2291 {SBIT(LAPD_STATE_IDLE) |
2292 SBIT(LAPD_STATE_MF_EST) |
2293 SBIT(LAPD_STATE_TIMER_RECOV),
2294 PRIM_DL_RECON, PRIM_OP_REQUEST,
2295 "DL-RECONNECT-REQUEST", lapd_res_req},
2296
2297 /* create and send DISC command */
2298 {SBIT(LAPD_STATE_SABM_SENT) |
2299 SBIT(LAPD_STATE_MF_EST) |
2300 SBIT(LAPD_STATE_TIMER_RECOV) |
2301 SBIT(LAPD_STATE_DISC_SENT),
2302 PRIM_DL_REL, PRIM_OP_REQUEST,
2303 "DL-RELEASE-REQUEST", lapd_rel_req},
2304
2305 /* release in idle state */
2306 {SBIT(LAPD_STATE_IDLE),
2307 PRIM_DL_REL, PRIM_OP_REQUEST,
2308 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2309};
2310
2311#define L2DOWNSLLEN \
2312 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2313
2314int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2315{
2316 struct lapd_datalink *dl = lctx->dl;
2317 int i, supported = 0;
2318 struct msgb *msg = dp->oph.msg;
2319 int rc;
2320
2321 /* find function for current state and message */
2322 for (i = 0; i < L2DOWNSLLEN; i++) {
2323 if (dp->oph.primitive == l2downstatelist[i].prim
2324 && dp->oph.operation == l2downstatelist[i].op) {
2325 supported = 1;
2326 if ((SBIT(dl->state) & l2downstatelist[i].states))
2327 break;
2328 }
2329 }
2330 if (!supported) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002331 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unsupported\n",
2332 dp->oph.primitive, dp->oph.operation);
rootaf48bed2011-09-26 11:23:06 +02002333 msgb_free(msg);
2334 return 0;
2335 }
2336 if (i == L2DOWNSLLEN) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002337 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unhandled at this state %s\n",
2338 dp->oph.primitive, dp->oph.operation, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002339 msgb_free(msg);
2340 return 0;
2341 }
2342
Harald Welte00b2faf2020-05-02 19:56:36 +02002343 LOGDL(dl, LOGL_INFO, "Message %s received in state %s\n",
2344 l2downstatelist[i].name, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002345
2346 rc = l2downstatelist[i].rout(dp, lctx);
2347
2348 return rc;
2349}
2350
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002351/*! @} */