blob: 57a02252933c05bdc67a771c933da2ac632fdc74 [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{
207 if (osmo_timer_pending(&dl->t200))
208 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200209 LOGDL(dl, LOGL_INFO, "start T200 (timeout=%d.%06ds)\n",
210 dl->t200_sec, dl->t200_usec);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200211 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
212}
213
214static void lapd_start_t203(struct lapd_datalink *dl)
215{
216 if (osmo_timer_pending(&dl->t203))
217 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200218 LOGDL(dl, LOGL_INFO, "start T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200219 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
220}
221
222static void lapd_stop_t200(struct lapd_datalink *dl)
223{
224 if (!osmo_timer_pending(&dl->t200))
225 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200226 LOGDL(dl, LOGL_INFO, "stop T200\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200227 osmo_timer_del(&dl->t200);
228}
229
230static void lapd_stop_t203(struct lapd_datalink *dl)
231{
232 if (!osmo_timer_pending(&dl->t203))
233 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200234 LOGDL(dl, LOGL_INFO, "stop T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200235 osmo_timer_del(&dl->t203);
236}
237
rootaf48bed2011-09-26 11:23:06 +0200238static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
239{
Harald Welte00b2faf2020-05-02 19:56:36 +0200240 LOGDL(dl, LOGL_INFO, "new state %s -> %s\n",
241 lapd_state_name(dl->state), lapd_state_name(state));
rootaf48bed2011-09-26 11:23:06 +0200242
243 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
244 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200245 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200246 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200247 msgb_free(dl->cont_res);
248 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200249 }
250
251 /* start T203 on entering MF EST state, if enabled */
252 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200253 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
254 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200255
256 dl->state = state;
257}
258
Harald Welte00b2faf2020-05-02 19:56:36 +0200259void *tall_lapd_ctx = NULL;
rootaf48bed2011-09-26 11:23:06 +0200260
Harald Welte00b2faf2020-05-02 19:56:36 +0200261/*! Initialize LAPD datalink instance and allocate history
262 * \param[in] dl caller-allocated datalink structure
263 * \param[in] k maximum number of unacknowledged frames
264 * \param[in] v_range range of sequence numbers
265 * \param[in] maxf maximum frame size (after defragmentation)
266 * \param[in] name human-readable name for this LAPD datalink */
267void lapd_dl_init2(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf,
268 const char *name)
rootaf48bed2011-09-26 11:23:06 +0200269{
270 int m;
271
272 memset(dl, 0, sizeof(*dl));
273 INIT_LLIST_HEAD(&dl->send_queue);
274 INIT_LLIST_HEAD(&dl->tx_queue);
275 dl->reestablish = 1;
276 dl->n200_est_rel = 3;
277 dl->n200 = 3;
278 dl->t200_sec = 1;
279 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200280 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200281 dl->t203_sec = 10;
282 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200283 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200284 dl->maxf = maxf;
285 if (k > v_range - 1)
286 k = v_range - 1;
287 dl->k = k;
288 dl->v_range = v_range;
289
290 /* Calculate modulo for history array:
291 * - The history range must be at least k+1.
292 * - The history range must be 2^x, where x is as low as possible.
293 */
294 k++;
295 for (m = 0x80; m; m >>= 1) {
296 if ((m & k)) {
297 if (k > m)
298 m <<= 1;
299 dl->range_hist = m;
300 break;
301 }
302 }
303
Harald Welte00b2faf2020-05-02 19:56:36 +0200304 if (!tall_lapd_ctx) {
305 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
306 OSMO_ASSERT(tall_lapd_ctx);
307 }
308
309 talloc_free(dl->name);
310 if (name)
311 dl->name = talloc_strdup(tall_lapd_ctx, name);
312 else
313 dl->name = talloc_asprintf(tall_lapd_ctx, "dl=%p", dl);
314
315 LOGDL(dl, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
316 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200317
318 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
319
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100320 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
321 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200322}
323
Harald Welte00b2faf2020-05-02 19:56:36 +0200324/*! Initialize LAPD datalink instance and allocate history
325 * \param[in] dl caller-allocated datalink structure
326 * \param[in] k maximum number of unacknowledged frames
327 * \param[in] v_range range of sequence numbers
328 * \param[in] maxf maximum frame size (after defragmentation) */
329void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf)
330{
331 lapd_dl_init2(dl, k, v_range, maxf, NULL);
332}
333
334void lapd_dl_set_name(struct lapd_datalink *dl, const char *name)
335{
336 if (!name)
337 return;
338 osmo_talloc_replace_string(tall_lapd_ctx, &dl->name, name);
339}
340
rootaf48bed2011-09-26 11:23:06 +0200341/* reset to IDLE state */
342void lapd_dl_reset(struct lapd_datalink *dl)
343{
Harald Welteef5b9b62020-06-07 22:29:53 +0200344 LOGDL(dl, LOGL_INFO, "Resetting LAPD instance\n");
Jean-Francois Dionne893979c2017-03-02 10:45:53 -0500345 /* enter idle state (and remove eventual cont_res) */
346 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200347 /* flush buffer */
348 lapd_dl_flush_tx(dl);
349 lapd_dl_flush_send(dl);
350 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200351 msgb_free(dl->rcv_buffer);
352 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200353 /* stop Timers */
354 lapd_stop_t200(dl);
355 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500356 if (dl->state == LAPD_STATE_IDLE)
357 return;
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500358 /* enter idle state (and remove eventual cont_res) */
359 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200360}
361
362/* reset and de-allocate history buffer */
363void lapd_dl_exit(struct lapd_datalink *dl)
364{
365 /* free all ressources except history buffer */
366 lapd_dl_reset(dl);
Ivan Kluchnikovb9759db2017-05-11 15:19:23 +0300367
368 /* enter null state */
369 lapd_dl_newstate(dl, LAPD_STATE_NULL);
370
rootaf48bed2011-09-26 11:23:06 +0200371 /* free history buffer list */
372 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200373 dl->tx_hist = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200374 talloc_free(dl->name);
375 dl->name = NULL;
rootaf48bed2011-09-26 11:23:06 +0200376}
377
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200378/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200379int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
380{
381 switch (mode) {
382 case LAPD_MODE_USER:
383 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
384 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
385 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
386 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
387 break;
388 case LAPD_MODE_NETWORK:
389 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
390 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
391 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
392 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
393 break;
394 default:
395 return -EINVAL;
396 }
397 dl->mode = mode;
398
399 return 0;
400}
401
402/* send DL message with optional msgb */
403static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
404 struct msgb *msg)
405{
406 struct lapd_datalink *dl = lctx->dl;
407 struct osmo_dlsap_prim dp;
408
409 osmo_prim_init(&dp.oph, 0, prim, op, msg);
410 return dl->send_dlsap(&dp, lctx);
411}
412
413/* send simple DL message */
414static inline int send_dl_simple(uint8_t prim, uint8_t op,
415 struct lapd_msg_ctx *lctx)
416{
Pau Espin Pedrol9dd3bf02016-02-29 08:49:22 -0500417 return send_dl_l3(prim, op, lctx, NULL);
rootaf48bed2011-09-26 11:23:06 +0200418}
419
420/* send MDL-ERROR INDICATION */
421static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
422{
423 struct lapd_datalink *dl = lctx->dl;
424 struct osmo_dlsap_prim dp;
425
Harald Welte00b2faf2020-05-02 19:56:36 +0200426 LOGDL(dl, LOGL_NOTICE,
427 "sending MDL-ERROR-IND cause %d from state %s\n",
428 cause, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200429 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
430 dp.u.error_ind.cause = cause;
431 return dl->send_dlsap(&dp, lctx);
432}
433
434/* send UA response */
435static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
436{
437 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
438 struct lapd_msg_ctx nctx;
439 struct lapd_datalink *dl = lctx->dl;
440
441 memcpy(&nctx, lctx, sizeof(nctx));
442 msg->l3h = msgb_put(msg, len);
443 if (len)
444 memcpy(msg->l3h, data, len);
445 /* keep nctx.ldp */
446 /* keep nctx.sapi */
447 /* keep nctx.tei */
448 nctx.cr = dl->cr.loc2rem.resp;
449 nctx.format = LAPD_FORM_U;
450 nctx.s_u = LAPD_U_UA;
451 /* keep nctx.p_f */
452 nctx.length = len;
453 nctx.more = 0;
454
455 return dl->send_ph_data_req(&nctx, msg);
456}
457
458/* send DM response */
459static int lapd_send_dm(struct lapd_msg_ctx *lctx)
460{
461 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
462 struct lapd_msg_ctx nctx;
463 struct lapd_datalink *dl = lctx->dl;
464
465 memcpy(&nctx, lctx, sizeof(nctx));
466 /* keep nctx.ldp */
467 /* keep nctx.sapi */
468 /* keep nctx.tei */
469 nctx.cr = dl->cr.loc2rem.resp;
470 nctx.format = LAPD_FORM_U;
471 nctx.s_u = LAPD_U_DM;
472 /* keep nctx.p_f */
473 nctx.length = 0;
474 nctx.more = 0;
475
476 return dl->send_ph_data_req(&nctx, msg);
477}
478
479/* send RR response / command */
480static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
481{
482 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
483 struct lapd_msg_ctx nctx;
484 struct lapd_datalink *dl = lctx->dl;
485
486 memcpy(&nctx, lctx, sizeof(nctx));
487 /* keep nctx.ldp */
488 /* keep nctx.sapi */
489 /* keep nctx.tei */
490 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
491 nctx.format = LAPD_FORM_S;
492 nctx.s_u = LAPD_S_RR;
493 nctx.p_f = f_bit;
494 nctx.n_recv = dl->v_recv;
495 nctx.length = 0;
496 nctx.more = 0;
497
498 return dl->send_ph_data_req(&nctx, msg);
499}
500
501/* send RNR response / command */
502static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
503{
504 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
505 struct lapd_msg_ctx nctx;
506 struct lapd_datalink *dl = lctx->dl;
507
508 memcpy(&nctx, lctx, sizeof(nctx));
509 /* keep nctx.ldp */
510 /* keep nctx.sapi */
511 /* keep nctx.tei */
512 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
513 nctx.format = LAPD_FORM_S;
514 nctx.s_u = LAPD_S_RNR;
515 nctx.p_f = f_bit;
516 nctx.n_recv = dl->v_recv;
517 nctx.length = 0;
518 nctx.more = 0;
519
520 return dl->send_ph_data_req(&nctx, msg);
521}
522
523/* send REJ response */
524static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
525{
526 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
527 struct lapd_msg_ctx nctx;
528 struct lapd_datalink *dl = lctx->dl;
529
530 memcpy(&nctx, lctx, sizeof(nctx));
531 /* keep nctx.ldp */
532 /* keep nctx.sapi */
533 /* keep nctx.tei */
534 nctx.cr = dl->cr.loc2rem.resp;
535 nctx.format = LAPD_FORM_S;
536 nctx.s_u = LAPD_S_REJ;
537 nctx.p_f = f_bit;
538 nctx.n_recv = dl->v_recv;
539 nctx.length = 0;
540 nctx.more = 0;
541
542 return dl->send_ph_data_req(&nctx, msg);
543}
544
545/* resend SABM or DISC message */
546static int lapd_send_resend(struct lapd_datalink *dl)
547{
548 struct msgb *msg;
549 uint8_t h = do_mod(dl->v_send, dl->range_hist);
550 int length = dl->tx_hist[h].msg->len;
551 struct lapd_msg_ctx nctx;
552
553 /* assemble message */
554 memcpy(&nctx, &dl->lctx, sizeof(nctx));
555 /* keep nctx.ldp */
556 /* keep nctx.sapi */
557 /* keep nctx.tei */
558 nctx.cr = dl->cr.loc2rem.cmd;
559 nctx.format = LAPD_FORM_U;
560 if (dl->state == LAPD_STATE_SABM_SENT)
561 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
562 else
563 nctx.s_u = LAPD_U_DISC;
564 nctx.p_f = 1;
565 nctx.length = length;
566 nctx.more = 0;
567
568 /* Resend SABM/DISC from tx_hist */
569 msg = lapd_msgb_alloc(length, "LAPD resend");
570 msg->l3h = msgb_put(msg, length);
571 if (length)
572 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
573
574 return dl->send_ph_data_req(&nctx, msg);
575}
576
577/* reestablish link */
578static int lapd_reestablish(struct lapd_datalink *dl)
579{
580 struct osmo_dlsap_prim dp;
581 struct msgb *msg;
582
Harald Welte00b2faf2020-05-02 19:56:36 +0200583 LOGDL(dl, LOGL_DEBUG, "LAPD reestablish\n");
Philipp Maier08177d32016-12-08 17:23:26 +0100584
rootaf48bed2011-09-26 11:23:06 +0200585 msg = lapd_msgb_alloc(0, "DUMMY");
586 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100587
rootaf48bed2011-09-26 11:23:06 +0200588 return lapd_est_req(&dp, &dl->lctx);
589}
590
591/* Timer callback on T200 expiry */
592static void lapd_t200_cb(void *data)
593{
594 struct lapd_datalink *dl = data;
595
Harald Welte00b2faf2020-05-02 19:56:36 +0200596 LOGDL(dl, LOGL_INFO, "Timeout T200 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200597
598 switch (dl->state) {
599 case LAPD_STATE_SABM_SENT:
600 /* 5.4.1.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200601 /* increment re-transmission counter */
602 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200603 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200604 /* flush tx and send buffers */
605 lapd_dl_flush_tx(dl);
606 lapd_dl_flush_send(dl);
607 /* go back to idle state */
608 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
609 /* NOTE: we must not change any other states or buffers
610 * and queues, since we may reconnect after handover
611 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100612 /* send MDL ERROR INIDCATION to L3 */
613 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100614 /* send RELEASE INDICATION to L3 */
615 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
616 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200617 break;
618 }
619 /* retransmit SABM command */
620 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200621 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200622 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200623 break;
624 case LAPD_STATE_DISC_SENT:
625 /* 5.4.4.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200626 /* increment re-transmission counter */
627 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200628 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200629 /* send MDL ERROR INIDCATION to L3 */
630 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
631 /* flush tx and send buffers */
632 lapd_dl_flush_tx(dl);
633 lapd_dl_flush_send(dl);
634 /* go back to idle state */
635 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
636 /* NOTE: we must not change any other states or buffers
637 * and queues, since we may reconnect after handover
638 * failure. the buffered messages is replaced there */
Harald Welted2a61172020-12-21 17:43:54 +0100639 /* send RELEASE INDICATION to L3 */
640 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200641 break;
642 }
643 /* retransmit DISC command */
644 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200645 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200646 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200647 break;
648 case LAPD_STATE_MF_EST:
649 /* 5.5.7 */
650 dl->retrans_ctr = 0;
651 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
652 /* fall through */
653 case LAPD_STATE_TIMER_RECOV:
654 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200655 if (dl->retrans_ctr <= dl->n200) {
rootaf48bed2011-09-26 11:23:06 +0200656 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
657 uint8_t h = do_mod(vs, dl->range_hist);
658 /* retransmit I frame (V_s-1) with P=1, if any */
659 if (dl->tx_hist[h].msg) {
660 struct msgb *msg;
661 int length = dl->tx_hist[h].msg->len;
662 struct lapd_msg_ctx nctx;
663
Harald Welte00b2faf2020-05-02 19:56:36 +0200664 LOGDL(dl, LOGL_INFO, "retransmit last frame V(S)=%d\n", vs);
rootaf48bed2011-09-26 11:23:06 +0200665 /* Create I frame (segment) from tx_hist */
666 memcpy(&nctx, &dl->lctx, sizeof(nctx));
667 /* keep nctx.ldp */
668 /* keep nctx.sapi */
669 /* keep nctx.tei */
670 nctx.cr = dl->cr.loc2rem.cmd;
671 nctx.format = LAPD_FORM_I;
672 nctx.p_f = 1;
673 nctx.n_send = vs;
674 nctx.n_recv = dl->v_recv;
675 nctx.length = length;
676 nctx.more = dl->tx_hist[h].more;
677 msg = lapd_msgb_alloc(length, "LAPD I resend");
678 msg->l3h = msgb_put(msg, length);
679 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
680 length);
681 dl->send_ph_data_req(&nctx, msg);
682 } else {
683 /* OR send appropriate supervision frame with P=1 */
684 if (!dl->own_busy && !dl->seq_err_cond) {
685 lapd_send_rr(&dl->lctx, 1, 1);
686 /* NOTE: In case of sequence error
687 * condition, the REJ frame has been
688 * transmitted when entering the
689 * condition, so it has not be done
690 * here
691 */
692 } else if (dl->own_busy) {
693 lapd_send_rnr(&dl->lctx, 1, 1);
694 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200695 LOGDL(dl, LOGL_INFO, "unhandled, pls. fix\n");
rootaf48bed2011-09-26 11:23:06 +0200696 }
697 }
698 /* 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 } else {
701 /* send MDL ERROR INIDCATION to L3 */
702 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
703 /* reestablish */
704 if (!dl->reestablish)
705 break;
Harald Welte00b2faf2020-05-02 19:56:36 +0200706 LOGDL(dl, LOGL_NOTICE, "N200+1 reached, performingreestablishment\n");
rootaf48bed2011-09-26 11:23:06 +0200707 lapd_reestablish(dl);
708 }
709 break;
710 default:
Harald Welte00b2faf2020-05-02 19:56:36 +0200711 LOGDL(dl, LOGL_INFO, "T200 expired in unexpected dl->state %s)\n",
712 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200713 }
714}
715
716/* Timer callback on T203 expiry */
717static void lapd_t203_cb(void *data)
718{
719 struct lapd_datalink *dl = data;
720
Harald Welte00b2faf2020-05-02 19:56:36 +0200721 LOGDL(dl, LOGL_INFO, "Timeout T203 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200722
723 if (dl->state != LAPD_STATE_MF_EST) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200724 LOGDL(dl, LOGL_ERROR, "T203 fired outside MF EST state, please fix!\n");
rootaf48bed2011-09-26 11:23:06 +0200725 return;
726 }
727
728 /* set retransmission counter to 0 */
729 dl->retrans_ctr = 0;
730 /* enter timer recovery state */
731 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
732 /* transmit a supervisory command with P bit set to 1 as follows: */
733 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200734 LOGDL(dl, LOGL_INFO, "transmit an RR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200735 /* Send RR with P=1 */
736 lapd_send_rr(&dl->lctx, 1, 1);
737 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200738 LOGDL(dl, LOGL_INFO, "transmit an RNR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200739 /* Send RNR with P=1 */
740 lapd_send_rnr(&dl->lctx, 1, 1);
741 }
742 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200743 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200744}
745
746/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
747static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
748{
749 struct lapd_datalink *dl = lctx->dl;
750 uint8_t nr = lctx->n_recv;
Max327e5e92022-10-11 19:48:44 +0300751 int s = 0, rej = 0;
752 bool t200_reset = false;
rootaf48bed2011-09-26 11:23:06 +0200753 int i, h;
754
755 /* supervisory frame ? */
756 if (lctx->format == LAPD_FORM_S)
757 s = 1;
758 /* REJ frame ? */
759 if (s && lctx->s_u == LAPD_S_REJ)
760 rej = 1;
761
762 /* Flush all transmit buffers of acknowledged frames */
763 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
764 h = do_mod(i, dl->range_hist);
765 if (dl->tx_hist[h].msg) {
766 msgb_free(dl->tx_hist[h].msg);
767 dl->tx_hist[h].msg = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200768 LOGDL(dl, LOGL_INFO, "ack frame %d\n", i);
rootaf48bed2011-09-26 11:23:06 +0200769 }
770 }
771
772 if (dl->state != LAPD_STATE_TIMER_RECOV) {
773 /* When not in the timer recovery condition, the data
774 * link layer entity shall reset the timer T200 on
775 * receipt of a valid I frame with N(R) higher than V(A),
776 * or an REJ with an N(R) equal to V(A). */
Max68588c52022-10-11 19:34:43 +0300777 if ((!rej && nr != dl->v_ack) || (rej && nr == dl->v_ack)) {
Max327e5e92022-10-11 19:48:44 +0300778 t200_reset = true;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200779 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200780 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
781 }
782 /* 5.7.4: N(R) sequence error
783 * N(R) is called valid, if and only if
784 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
785 */
Max68588c52022-10-11 19:34:43 +0300786 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 +0200787 LOGDL(dl, LOGL_NOTICE, "N(R) sequence error\n");
rootaf48bed2011-09-26 11:23:06 +0200788 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
789 }
790 }
791
792 /* V(A) shall be set to the value of N(R) */
793 dl->v_ack = nr;
794
Andreas Eversberg742fc792011-09-27 09:40:25 +0200795 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200796 * and if there are outstanding I frames, restart T200 */
797 if (t200_reset && !rej) {
798 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200799 LOGDL(dl, LOGL_INFO, "start T200, due to unacked I frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200800 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200801 }
802 }
803
804 /* This also does a restart, when I or S frame is received */
805
806 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200807 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200808 /* Start T203, if T200 is not running in MF EST state, if enabled */
809 if (!osmo_timer_pending(&dl->t200)
810 && (dl->t203_sec || dl->t203_usec)
811 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200812 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200813 }
814}
815
816/* L1 -> L2 */
817
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200818/* Receive a LAPD U SABM(E) message from L1 */
819static int lapd_rx_u_sabm(struct msgb *msg, struct lapd_msg_ctx *lctx)
rootaf48bed2011-09-26 11:23:06 +0200820{
821 struct lapd_datalink *dl = lctx->dl;
822 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100823 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200824 uint8_t prim, op;
825
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200826 prim = PRIM_DL_EST;
827 op = PRIM_OP_INDICATION;
rootaf48bed2011-09-26 11:23:06 +0200828
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200829 LOGDL(dl, LOGL_INFO, "SABM(E) received in state %s\n", lapd_state_name(dl->state));
830 /* 5.7.1 */
831 dl->seq_err_cond = 0;
832 /* G.2.2 Wrong value of the C/R bit */
833 if (lctx->cr == dl->cr.rem2loc.resp) {
834 LOGDL(dl, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200835 msgb_free(msg);
836 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
837 return -EINVAL;
838 }
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200839
840 /* G.4.5 If SABM is received with L>N201 or with M bit
841 * set, AN MDL-ERROR-INDICATION is sent to MM.
842 */
843 if (lctx->more || length > lctx->n201) {
844 LOGDL(dl, LOGL_ERROR, "SABM too large error\n");
845 msgb_free(msg);
846 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
847 return -EIO;
848 }
849
850 switch (dl->state) {
851 case LAPD_STATE_IDLE:
852 break;
Pau Espin Pedrol76190d32020-10-21 13:05:44 +0200853 case LAPD_STATE_TIMER_RECOV:
854 LOGDL(dl, LOGL_INFO, "SABM command, timer recovery state\n");
855 /* If link is lost on the remote side, we start over
856 * and send DL-ESTABLISH indication again. */
857 /* 3GPP TS 44.006 8.6.3 "Procedures for re-establishment" */
858 if (length) {
859 /* check for contention resoultion */
860 LOGDL(dl, LOGL_ERROR, "SABM L>0 not expected in timer "
861 "recovery state\n");
862 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
863 lapd_send_dm(lctx);
864 msgb_free(msg);
865 return 0;
866 }
867 /* re-establishment, continue below */
868 lapd_stop_t200(dl);
869 break;
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200870 case LAPD_STATE_MF_EST:
871 LOGDL(dl, LOGL_INFO, "SABM command, multiple frame established state\n");
872 /* If link is lost on the remote side, we start over
873 * and send DL-ESTABLISH indication again. */
874 /* Additionally, continue in case of content resoltion
875 * (GSM network). This happens, if the mobile has not
876 * yet received UA or another mobile (collision) tries
877 * to establish connection. The mobile must receive
878 * UA again. */
879 /* 5.4.2.1 */
880 if (!length) {
881 /* If no content resolution, this is a
882 * re-establishment. */
883 LOGDL(dl, LOGL_INFO, "Remote reestablish\n");
884 break;
885 }
886 if (!dl->cont_res) {
887 LOGDL(dl, LOGL_INFO, "SABM command not allowed in state %s\n",
888 lapd_state_name(dl->state));
889 mdl_error(MDL_CAUSE_SABM_MF, lctx);
890 msgb_free(msg);
891 return 0;
892 }
893 /* Ignore SABM if content differs from first SABM. */
894 if (dl->mode == LAPD_MODE_NETWORK && length) {
895#ifdef TEST_CONTENT_RESOLUTION_NETWORK
896 dl->cont_res->data[0] ^= 0x01;
897#endif
898 if (memcmp(dl->cont_res->data, msg->data,
899 length)) {
900 LOGDL(dl, LOGL_INFO, "Another SABM with different content - "
901 "ignoring!\n");
902 msgb_free(msg);
903 return 0;
904 }
905 }
906 /* send UA again */
907 lapd_send_ua(lctx, length, msg->l3h);
908 msgb_free(msg);
909 return 0;
910 case LAPD_STATE_DISC_SENT:
911 /* 5.4.6.2 send DM with F=P */
912 lapd_send_dm(lctx);
913 /* stop Timer T200 */
914 lapd_stop_t200(dl);
915 msgb_free(msg);
916 return send_dl_simple(prim, op, lctx);
917 default:
918 /* collision: Send UA, but still wait for rx UA, then
919 * change to MF_EST state.
920 */
921 /* check for contention resoultion */
922 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
923 LOGDL(dl, LOGL_NOTICE, "SABM not allowed during contention "
924 "resolution (state=%s)\n", lapd_state_name(dl->state));
925 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
926 }
927 lapd_send_ua(lctx, length, msg->l3h);
928 msgb_free(msg);
929 return 0;
930 }
931 /* save message context for further use */
932 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
933#ifndef TEST_CONTENT_RESOLUTION_NETWORK
934 /* send UA response */
935 lapd_send_ua(lctx, length, msg->l3h);
936#endif
937 /* set Vs, Vr and Va to 0 */
938 dl->v_send = dl->v_recv = dl->v_ack = 0;
939 /* clear tx_hist */
940 lapd_dl_flush_hist(dl);
941 /* enter multiple-frame-established state */
942 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
943 /* store content resolution data on network side
944 * Note: cont_res will be removed when changing state again,
945 * so it must be allocated AFTER lapd_dl_newstate(). */
946 if (dl->mode == LAPD_MODE_NETWORK && length) {
947 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
948 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
949 length);
Pau Espin Pedrol28b404f2022-03-02 19:03:32 +0100950 LOGDL(dl, LOGL_INFO, "Store content res.\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200951 }
952 /* send notification to L3 */
953 if (length == 0) {
954 /* 5.4.1.2 Normal establishment procedures */
955 rc = send_dl_simple(prim, op, lctx);
956 msgb_free(msg);
957 } else {
958 /* 5.4.1.4 Contention resolution establishment */
959 msgb_trim(msg, length);
960 rc = send_dl_l3(prim, op, lctx, msg);
961 }
rootaf48bed2011-09-26 11:23:06 +0200962 return rc;
963}
964
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200965/* Receive a LAPD U DM message from L1 */
966static int lapd_rx_u_dm(struct msgb *msg, struct lapd_msg_ctx *lctx)
967{
968 struct lapd_datalink *dl = lctx->dl;
969 int rc = 0;
970
971 LOGDL(dl, LOGL_INFO, "DM received in state %s\n", lapd_state_name(dl->state));
972 /* G.2.2 Wrong value of the C/R bit */
973 if (lctx->cr == dl->cr.rem2loc.cmd) {
974 LOGDL(dl, LOGL_ERROR, "DM command error\n");
975 msgb_free(msg);
976 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
977 return -EINVAL;
978 }
979 if (!lctx->p_f) {
980 /* 5.4.1.2 DM responses with the F bit set to "0"
981 * shall be ignored.
982 */
983 msgb_free(msg);
984 return 0;
985 }
986 switch (dl->state) {
987 case LAPD_STATE_SABM_SENT:
988 break;
989 case LAPD_STATE_MF_EST:
990 if (lctx->p_f) {
991 LOGDL(dl, LOGL_INFO, "unsolicited DM response\n");
992 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
993 } else {
994 LOGDL(dl, LOGL_INFO, "unsolicited DM response, "
995 "multiple frame established state\n");
996 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
997 /* reestablish */
998 if (!dl->reestablish) {
999 msgb_free(msg);
1000 return 0;
1001 }
1002 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1003 lapd_reestablish(dl);
1004 }
1005 msgb_free(msg);
1006 return 0;
1007 case LAPD_STATE_TIMER_RECOV:
1008 /* FP = 0 (DM is normal in case PF = 1) */
1009 if (!lctx->p_f) {
1010 LOGDL(dl, LOGL_INFO, "unsolicited DM response, multiple frame "
1011 "established state\n");
1012 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1013 msgb_free(msg);
1014 /* reestablish */
1015 if (!dl->reestablish)
1016 return 0;
1017 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1018 return lapd_reestablish(dl);
1019 }
1020 break;
1021 case LAPD_STATE_DISC_SENT:
1022 /* stop Timer T200 */
1023 lapd_stop_t200(dl);
1024 /* go to idle state */
1025 lapd_dl_flush_tx(dl);
1026 lapd_dl_flush_send(dl);
1027 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1028 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1029 msgb_free(msg);
1030 return 0;
1031 case LAPD_STATE_IDLE:
1032 /* 5.4.5 all other frame types shall be discarded */
1033 default:
1034 LOGDL(dl, LOGL_INFO, "unsolicited DM response! (discarding)\n");
1035 msgb_free(msg);
1036 return 0;
1037 }
1038 /* stop timer T200 */
1039 lapd_stop_t200(dl);
1040 /* go to idle state */
1041 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1042 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1043 msgb_free(msg);
1044 return rc;
1045}
1046
1047/* Receive a LAPD U UI message from L1 */
1048static int lapd_rx_u_ui(struct msgb *msg, struct lapd_msg_ctx *lctx)
1049{
1050 struct lapd_datalink *dl = lctx->dl;
1051 int length = lctx->length;
1052
1053 LOGDL(dl, LOGL_INFO, "UI received\n");
1054 /* G.2.2 Wrong value of the C/R bit */
1055 if (lctx->cr == dl->cr.rem2loc.resp) {
1056 LOGDL(dl, LOGL_ERROR, "UI indicates response error\n");
1057 msgb_free(msg);
1058 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1059 return -EINVAL;
1060 }
1061
1062 /* G.4.5 If UI is received with L>N201 or with M bit
1063 * set, AN MDL-ERROR-INDICATION is sent to MM.
1064 */
1065 if (length > lctx->n201 || lctx->more) {
1066 LOGDL(dl, LOGL_ERROR, "UI too large error (%d > N201(%d) or M=%d)\n",
1067 length, lctx->n201, lctx->more);
1068 msgb_free(msg);
1069 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1070 return -EIO;
1071 }
1072
1073 /* do some length checks */
1074 if (length == 0) {
1075 /* 5.3.3 UI frames received with the length indicator
1076 * set to "0" shall be ignored
1077 */
1078 LOGDL(dl, LOGL_INFO, "length=0 (discarding)\n");
1079 msgb_free(msg);
1080 return 0;
1081 }
1082 msgb_trim(msg, length);
1083 return send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx, msg);
1084}
1085
1086/* Receive a LAPD U DISC message from L1 */
1087static int lapd_rx_u_disc(struct msgb *msg, struct lapd_msg_ctx *lctx)
1088{
1089 struct lapd_datalink *dl = lctx->dl;
1090 int length = lctx->length;
1091 int rc = 0;
1092 uint8_t prim, op;
1093
1094 prim = PRIM_DL_REL;
1095 op = PRIM_OP_INDICATION;
1096
1097 LOGDL(dl, LOGL_INFO, "DISC received in state %s\n", lapd_state_name(dl->state));
1098 /* flush tx and send buffers */
1099 lapd_dl_flush_tx(dl);
1100 lapd_dl_flush_send(dl);
1101 /* 5.7.1 */
1102 dl->seq_err_cond = 0;
1103 /* G.2.2 Wrong value of the C/R bit */
1104 if (lctx->cr == dl->cr.rem2loc.resp) {
1105 LOGDL(dl, LOGL_ERROR, "DISC response error\n");
1106 msgb_free(msg);
1107 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1108 return -EINVAL;
1109 }
1110 if (length > 0 || lctx->more) {
1111 /* G.4.4 If a DISC or DM frame is received with L>0 or
1112 * with the M bit set to "1", an MDL-ERROR-INDICATION
1113 * primitive with cause "U frame with incorrect
1114 * parameters" is sent to the mobile management entity.
1115 */
1116 LOGDL(dl, LOGL_ERROR, "U frame iwth incorrect parameters\n");
1117 msgb_free(msg);
1118 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1119 return -EIO;
1120 }
1121 switch (dl->state) {
1122 case LAPD_STATE_IDLE:
1123 LOGDL(dl, LOGL_INFO, "DISC in idle state\n");
1124 /* send DM with F=P */
1125 msgb_free(msg);
1126 return lapd_send_dm(lctx);
1127 case LAPD_STATE_SABM_SENT:
1128 LOGDL(dl, LOGL_INFO, "DISC in SABM state\n");
1129 /* 5.4.6.2 send DM with F=P */
1130 lapd_send_dm(lctx);
1131 /* stop Timer T200 */
1132 lapd_stop_t200(dl);
1133 /* go to idle state */
1134 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1135 msgb_free(msg);
1136 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1137 lctx);
1138 case LAPD_STATE_MF_EST:
1139 case LAPD_STATE_TIMER_RECOV:
1140 LOGDL(dl, LOGL_INFO, "DISC in est state\n");
1141 break;
1142 case LAPD_STATE_DISC_SENT:
1143 LOGDL(dl, LOGL_INFO, "DISC in disc state\n");
1144 prim = PRIM_DL_REL;
1145 op = PRIM_OP_CONFIRM;
1146 break;
1147 default:
1148 lapd_send_ua(lctx, length, msg->l3h);
1149 msgb_free(msg);
1150 return 0;
1151 }
1152 /* send UA response */
1153 lapd_send_ua(lctx, length, msg->l3h);
1154 /* stop Timer T200 */
1155 lapd_stop_t200(dl);
1156 /* enter idle state, keep tx-buffer with UA response */
1157 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1158 /* send notification to L3 */
1159 rc = send_dl_simple(prim, op, lctx);
1160 msgb_free(msg);
1161 return rc;
1162}
1163
1164/* Receive a LAPD U UA message from L1 */
1165static int lapd_rx_u_ua(struct msgb *msg, struct lapd_msg_ctx *lctx)
1166{
1167 struct lapd_datalink *dl = lctx->dl;
1168 int length = lctx->length;
1169 int rc = 0;
1170
1171 LOGDL(dl, LOGL_INFO, "UA received in state %s\n", lapd_state_name(dl->state));
1172 /* G.2.2 Wrong value of the C/R bit */
1173 if (lctx->cr == dl->cr.rem2loc.cmd) {
1174 LOGDL(dl, LOGL_ERROR, "UA indicates command error\n");
1175 msgb_free(msg);
1176 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1177 return -EINVAL;
1178 }
1179
1180 /* G.4.5 If UA is received with L>N201 or with M bit
1181 * set, AN MDL-ERROR-INDICATION is sent to MM.
1182 */
1183 if (lctx->more || length > lctx->n201) {
1184 LOGDL(dl, LOGL_ERROR, "UA too large error\n");
1185 msgb_free(msg);
1186 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1187 return -EIO;
1188 }
1189
1190 if (!lctx->p_f) {
1191 /* 5.4.1.2 A UA response with the F bit set to "0"
1192 * shall be ignored.
1193 */
1194 LOGDL(dl, LOGL_INFO, "F=0 (discarding)\n");
1195 msgb_free(msg);
1196 return 0;
1197 }
1198 switch (dl->state) {
1199 case LAPD_STATE_SABM_SENT:
1200 break;
1201 case LAPD_STATE_MF_EST:
1202 case LAPD_STATE_TIMER_RECOV:
1203 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1204 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1205 msgb_free(msg);
1206 return 0;
1207 case LAPD_STATE_DISC_SENT:
1208 LOGDL(dl, LOGL_INFO, "UA in disconnect state\n");
1209 /* stop Timer T200 */
1210 lapd_stop_t200(dl);
1211 /* go to idle state */
1212 lapd_dl_flush_tx(dl);
1213 lapd_dl_flush_send(dl);
1214 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1215 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1216 msgb_free(msg);
1217 return 0;
1218 case LAPD_STATE_IDLE:
1219 /* 5.4.5 all other frame types shall be discarded */
1220 default:
1221 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1222 msgb_free(msg);
1223 return 0;
1224 }
1225 LOGDL(dl, LOGL_INFO, "UA in SABM state\n");
1226 /* stop Timer T200 */
1227 lapd_stop_t200(dl);
1228 /* compare UA with SABME if contention resolution is applied */
1229 if (dl->tx_hist[0].msg->len) {
1230 if (length != (dl->tx_hist[0].msg->len)
1231 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1232 length)) {
1233 LOGDL(dl, LOGL_INFO, "**** UA response mismatches ****\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001234 /* go to idle state */
1235 lapd_dl_flush_tx(dl);
1236 lapd_dl_flush_send(dl);
1237 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
Harald Welted2a61172020-12-21 17:43:54 +01001238 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1239 msgb_free(msg);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001240 return 0;
1241 }
1242 }
1243 /* set Vs, Vr and Va to 0 */
1244 dl->v_send = dl->v_recv = dl->v_ack = 0;
1245 /* clear tx_hist */
1246 lapd_dl_flush_hist(dl);
1247 /* enter multiple-frame-established state */
1248 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1249 /* send outstanding frames, if any (resume / reconnect) */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001250 lapd_send_i(dl, __LINE__, false);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001251 /* send notification to L3 */
1252 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1253 msgb_free(msg);
1254 return rc;
1255}
1256
1257/* Receive a LAPD U FRMR message from L1 */
1258static int lapd_rx_u_frmr(struct msgb *msg, struct lapd_msg_ctx *lctx)
1259{
1260 struct lapd_datalink *dl = lctx->dl;
1261
1262 LOGDL(dl, LOGL_NOTICE, "Frame reject received\n");
1263 /* send MDL ERROR INIDCATION to L3 */
1264 mdl_error(MDL_CAUSE_FRMR, lctx);
1265 msgb_free(msg);
1266 /* reestablish */
1267 if (!dl->reestablish)
1268 return 0;
1269 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1270 return lapd_reestablish(dl);
1271}
1272
1273/* Receive a LAPD U (Unnumbered) message from L1 */
1274static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
1275{
1276 switch (lctx->s_u) {
1277 case LAPD_U_SABM:
1278 case LAPD_U_SABME:
1279 return lapd_rx_u_sabm(msg, lctx);
1280 case LAPD_U_DM:
1281 return lapd_rx_u_dm(msg, lctx);
1282 case LAPD_U_UI:
1283 return lapd_rx_u_ui(msg, lctx);
1284 case LAPD_U_DISC:
1285 return lapd_rx_u_disc(msg, lctx);
1286 case LAPD_U_UA:
1287 return lapd_rx_u_ua(msg, lctx);
1288 case LAPD_U_FRMR:
1289 return lapd_rx_u_frmr(msg, lctx);
1290 default:
1291 /* G.3.1 */
1292 LOGDL(lctx->dl, LOGL_NOTICE, "Unnumbered frame not allowed\n");
1293 msgb_free(msg);
1294 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1295 return -EINVAL;
1296 }
1297}
1298
rootaf48bed2011-09-26 11:23:06 +02001299/* Receive a LAPD S (Supervisory) message from L1 */
1300static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1301{
1302 struct lapd_datalink *dl = lctx->dl;
1303 int length = lctx->length;
1304
1305 if (length > 0 || lctx->more) {
1306 /* G.4.3 If a supervisory frame is received with L>0 or
1307 * with the M bit set to "1", an MDL-ERROR-INDICATION
1308 * primitive with cause "S frame with incorrect
1309 * parameters" is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001310 LOGDL(dl, LOGL_ERROR, "S frame with incorrect parameters\n");
rootaf48bed2011-09-26 11:23:06 +02001311 msgb_free(msg);
1312 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1313 return -EIO;
1314 }
1315
1316 if (lctx->cr == dl->cr.rem2loc.resp
1317 && lctx->p_f
1318 && dl->state != LAPD_STATE_TIMER_RECOV) {
1319 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001320 LOGDL(dl, LOGL_NOTICE, "S frame response with F=1 error\n");
rootaf48bed2011-09-26 11:23:06 +02001321 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1322 }
1323
1324 switch (dl->state) {
1325 case LAPD_STATE_IDLE:
1326 /* if P=1, respond DM with F=1 (5.2.2) */
1327 /* 5.4.5 all other frame types shall be discarded */
1328 if (lctx->p_f)
1329 lapd_send_dm(lctx); /* F=P */
1330 /* fall though */
1331 case LAPD_STATE_SABM_SENT:
1332 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001333 LOGDL(dl, LOGL_NOTICE, "S frame ignored in this state\n");
rootaf48bed2011-09-26 11:23:06 +02001334 msgb_free(msg);
1335 return 0;
1336 }
1337 switch (lctx->s_u) {
1338 case LAPD_S_RR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001339 LOGDL(dl, LOGL_INFO, "RR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001340 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1341 lapd_acknowledge(lctx);
1342
1343 /* 5.5.3.2 */
1344 if (lctx->cr == dl->cr.rem2loc.cmd
1345 && lctx->p_f) {
1346 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001347 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1348 "we are not busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001349 lapd_send_rr(lctx, 1, 0);
1350 /* NOTE: In case of sequence error condition,
1351 * the REJ frame has been transmitted when
1352 * entering the condition, so it has not be
1353 * done here
1354 */
1355 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001356 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1357 "we are busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001358 lapd_send_rnr(lctx, 1, 0);
1359 }
1360 } else if (lctx->cr == dl->cr.rem2loc.resp
1361 && lctx->p_f
1362 && dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001363 LOGDL(dl, LOGL_INFO, "RR response with F==1, and we are in timer recovery "
1364 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001365 /* V(S) to the N(R) in the RR frame */
1366 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001367 /* stop Timer T200 */
1368 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001369 /* 5.5.7 Clear timer recovery condition */
1370 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1371 }
1372 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001373 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001374
1375 break;
1376 case LAPD_S_RNR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001377 LOGDL(dl, LOGL_INFO, "RNR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001378 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1379 lapd_acknowledge(lctx);
1380
1381 /* 5.5.5 */
1382 /* Set peer receiver busy condition */
1383 dl->peer_busy = 1;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001384 /* Flush pending messages in TX queue. */
1385 lapd_dl_flush_tx_queue(dl);
1386 /* stop Timer T200 */
1387 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001388
1389 if (lctx->p_f) {
1390 if (lctx->cr == dl->cr.rem2loc.cmd) {
1391 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001392 LOGDL(dl, LOGL_INFO, "RNR poll command and we are not busy, "
1393 "so we reply with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001394 /* Send RR with F=1 */
1395 lapd_send_rr(lctx, 1, 0);
1396 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001397 LOGDL(dl, LOGL_INFO, "RNR poll command and we are busy, so "
1398 "we reply with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001399 /* Send RNR with F=1 */
1400 lapd_send_rnr(lctx, 1, 0);
1401 }
1402 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001403 LOGDL(dl, LOGL_INFO, "RNR poll response and we in timer recovery "
1404 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001405 /* 5.5.7 Clear timer recovery condition */
1406 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1407 /* V(S) to the N(R) in the RNR frame */
1408 dl->v_send = lctx->n_recv;
1409 }
1410 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001411 LOGDL(dl, LOGL_INFO, "RNR not polling/final state received\n");
rootaf48bed2011-09-26 11:23:06 +02001412
1413 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001414 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001415
1416 break;
1417 case LAPD_S_REJ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001418 LOGDL(dl, LOGL_INFO, "REJ received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001419 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1420 lapd_acknowledge(lctx);
1421
1422 /* 5.5.4.1 */
1423 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1424 /* Clear an existing peer receiver busy condition */
1425 dl->peer_busy = 0;
1426 /* V(S) and V(A) to the N(R) in the REJ frame */
1427 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001428 /* Flush pending messages in TX queue. */
1429 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001430 /* stop Timer T200 */
1431 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001432 /* 5.5.3.2 */
1433 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1434 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001435 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1436 "state and not in own busy condition received, so we "
1437 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001438 lapd_send_rr(lctx, 1, 0);
1439 /* NOTE: In case of sequence error
1440 * condition, the REJ frame has been
1441 * transmitted when entering the
1442 * condition, so it has not be done
1443 * here
1444 */
1445 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001446 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1447 "state and in own busy condition received, so we "
1448 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001449 lapd_send_rnr(lctx, 1, 0);
1450 }
1451 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001452 LOGDL(dl, LOGL_INFO, "REJ response or not polling command not "
1453 "in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001454 /* send MDL ERROR INIDCATION to L3 */
1455 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001456 LOGDL(dl, LOGL_ERROR, "unsolicited supervisory response!\n");
rootaf48bed2011-09-26 11:23:06 +02001457 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1458 }
1459
1460 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001461 LOGDL(dl, LOGL_INFO, "REJ poll response in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001462 /* Clear an existing peer receiver busy condition */
1463 dl->peer_busy = 0;
1464 /* V(S) and V(A) to the N(R) in the REJ frame */
1465 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001466 /* Flush pending messages in TX queue. */
1467 lapd_dl_flush_tx_queue(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001468 /* stop Timer T200 */
1469 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001470 /* 5.5.7 Clear timer recovery condition */
1471 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1472 } else {
1473 /* Clear an existing peer receiver busy condition */
1474 dl->peer_busy = 0;
1475 /* V(S) and V(A) to the N(R) in the REJ frame */
1476 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg913a7832023-11-09 12:03:04 +01001477 /* Flush pending messages in TX queue. */
1478 lapd_dl_flush_tx_queue(dl);
rootaf48bed2011-09-26 11:23:06 +02001479 /* 5.5.3.2 */
1480 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1481 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001482 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1483 "state and not in own busy condition received, so we "
1484 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001485 lapd_send_rr(lctx, 1, 0);
1486 /* NOTE: In case of sequence error
1487 * condition, the REJ frame has been
1488 * transmitted when entering the
1489 * condition, so it has not be done
1490 * here
1491 */
1492 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001493 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1494 "state and in own busy condition received, so we "
1495 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001496 lapd_send_rnr(lctx, 1, 0);
1497 }
1498 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001499 LOGDL(dl, LOGL_INFO, "REJ response or not polling command in "
1500 "timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001501 }
1502
1503 /* FIXME: 5.5.4.2 2) */
1504
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001505 /* Send message, if possible due to acknowledged data and new V(S) and V(A). */
1506 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001507
1508 break;
1509 default:
1510 /* G.3.1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001511 LOGDL(dl, LOGL_ERROR, "Supervisory frame not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001512 msgb_free(msg);
1513 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1514 return -EINVAL;
1515 }
1516 msgb_free(msg);
1517 return 0;
1518}
1519
1520/* Receive a LAPD I (Information) message from L1 */
1521static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1522{
1523 struct lapd_datalink *dl = lctx->dl;
1524 //uint8_t nr = lctx->n_recv;
1525 uint8_t ns = lctx->n_send;
1526 int length = lctx->length;
1527 int rc;
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001528 bool i_frame_in_queue = false;
rootaf48bed2011-09-26 11:23:06 +02001529
Harald Welte00b2faf2020-05-02 19:56:36 +02001530 LOGDL(dl, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1531 lapd_state_name(dl->state), lctx->sapi);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001532
rootaf48bed2011-09-26 11:23:06 +02001533 /* G.2.2 Wrong value of the C/R bit */
1534 if (lctx->cr == dl->cr.rem2loc.resp) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001535 LOGDL(dl, LOGL_ERROR, "I frame response not allowed (state %s)\n",
1536 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001537 msgb_free(msg);
1538 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1539 return -EINVAL;
1540 }
1541
1542 if (length == 0 || length > lctx->n201) {
1543 /* G.4.2 If the length indicator of an I frame is set
1544 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1545 * primitive with cause "I frame with incorrect length"
1546 * is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001547 LOGDL(dl, LOGL_ERROR, "I frame length not allowed (state %s)\n",
1548 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001549 msgb_free(msg);
1550 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1551 return -EIO;
1552 }
1553
1554 /* G.4.2 If the numerical value of L is L<N201 and the M
1555 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1556 * cause "I frame with incorrect use of M bit" is sent to the
1557 * mobile management entity. */
1558 if (lctx->more && length < lctx->n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001559 LOGDL(dl, LOGL_ERROR, "I frame with M bit too short (state %s)\n",
1560 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001561 msgb_free(msg);
1562 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1563 return -EIO;
1564 }
1565
1566 switch (dl->state) {
1567 case LAPD_STATE_IDLE:
1568 /* if P=1, respond DM with F=1 (5.2.2) */
1569 /* 5.4.5 all other frame types shall be discarded */
1570 if (lctx->p_f)
1571 lapd_send_dm(lctx); /* F=P */
1572 /* fall though */
1573 case LAPD_STATE_SABM_SENT:
1574 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001575 LOGDL(dl, LOGL_NOTICE, "I frame ignored in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001576 msgb_free(msg);
1577 return 0;
1578 }
1579
1580 /* 5.7.1: N(s) sequence error */
1581 if (ns != dl->v_recv) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001582 LOGDL(dl, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, V(R)=%u (state %s)\n",
1583 ns, dl->v_recv, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001584 /* discard data */
1585 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001586 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001587 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001588 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001589 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001590 lapd_send_rej(lctx, lctx->p_f);
1591 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001592 /* If there are two subsequent sequence errors received,
1593 * ignore it. (Ignore every second subsequent error.)
1594 * This happens if our reply with the REJ is too slow,
1595 * so the remote gets a T200 timeout and sends another
1596 * frame with a sequence error.
1597 * Test showed that replying with two subsequent REJ
1598 * messages could the remote L2 process to abort.
1599 * Replying too slow shouldn't happen, but may happen
1600 * over serial link between BB and LAPD.
1601 */
1602 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001603 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001604 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1605 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1606 lapd_acknowledge(lctx); /* V(A) is also set here */
1607
1608 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001609 lapd_send_i(dl, __LINE__, false);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001610
1611 return 0;
rootaf48bed2011-09-26 11:23:06 +02001612 }
1613 dl->seq_err_cond = 0;
1614
1615 /* Increment receiver state */
1616 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Harald Welte00b2faf2020-05-02 19:56:36 +02001617 LOGDL(dl, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
rootaf48bed2011-09-26 11:23:06 +02001618
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001619 /* Update all pending frames in the queue to the new V(R) state. */
1620 if (dl->update_pending_frames) {
1621 rc = dl->update_pending_frames(lctx);
1622 if (!rc)
1623 i_frame_in_queue = true;
1624 }
1625
rootaf48bed2011-09-26 11:23:06 +02001626 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1627 lapd_acknowledge(lctx); /* V(A) is also set here */
1628
1629 /* Only if we are not in own receiver busy condition */
1630 if (!dl->own_busy) {
1631 /* if the frame carries a complete segment */
1632 if (!lctx->more && !dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001633 LOGDL(dl, LOGL_INFO, "message in single I frame\n");
rootaf48bed2011-09-26 11:23:06 +02001634 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001635 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001636 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1637 msg);
1638 } else {
1639 /* create rcv_buffer */
1640 if (!dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001641 LOGDL(dl, LOGL_INFO, "message in multiple I frames (first message)\n");
rootaf48bed2011-09-26 11:23:06 +02001642 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1643 "LAPD RX");
1644 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1645 }
1646 /* concat. rcv_buffer */
1647 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001648 LOGDL(dl, LOGL_NOTICE, "Received frame overflow!\n");
rootaf48bed2011-09-26 11:23:06 +02001649 } else {
1650 memcpy(msgb_put(dl->rcv_buffer, length),
1651 msg->l3h, length);
1652 }
1653 /* if the last segment was received */
1654 if (!lctx->more) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001655 LOGDL(dl, LOGL_INFO, "message in multiple I frames (last message)\n");
rootaf48bed2011-09-26 11:23:06 +02001656 rc = send_dl_l3(PRIM_DL_DATA,
1657 PRIM_OP_INDICATION, lctx,
1658 dl->rcv_buffer);
1659 dl->rcv_buffer = NULL;
1660 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001661 LOGDL(dl, LOGL_INFO, "message in multiple I frames (next message)\n");
rootaf48bed2011-09-26 11:23:06 +02001662 msgb_free(msg);
1663
1664 }
Harald Weltebc1d7152020-07-04 10:52:13 +02001665 /* the L3 or higher (called in-line above via send_dl_l3) might have destroyed the
1666 * data link meanwhile. See OS#1761 */
1667 if (dl->state == LAPD_STATE_NULL)
1668 return 0;
rootaf48bed2011-09-26 11:23:06 +02001669 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001670 LOGDL(dl, LOGL_INFO, "I frame ignored during own receiver busy condition\n");
rootaf48bed2011-09-26 11:23:06 +02001671
1672 /* Check for P bit */
1673 if (lctx->p_f) {
1674 /* 5.5.2.1 */
1675 /* check if we are not in own receiver busy */
1676 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001677 LOGDL(dl, LOGL_INFO, "we are not busy, send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001678 /* Send RR with F=1 */
1679 rc = lapd_send_rr(lctx, 1, 0);
1680 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001681 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001682 /* Send RNR with F=1 */
1683 rc = lapd_send_rnr(lctx, 1, 0);
1684 }
1685 } else {
1686 /* 5.5.2.2 */
1687 /* check if we are not in own receiver busy */
1688 if (!dl->own_busy) {
1689 /* NOTE: V(R) is already set above */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001690 rc = lapd_send_i(dl, __LINE__, false);
Andreas Eversberg1bb0b992023-11-09 12:09:21 +01001691 if (rc && !i_frame_in_queue) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001692 LOGDL(dl, LOGL_INFO, "we are not busy and have no pending data, "
1693 "send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001694 /* Send RR with F=0 */
1695 return lapd_send_rr(lctx, 0, 0);
1696 }
1697 /* all I or one RR is sent, we are done */
1698 return 0;
1699 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001700 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001701 /* Send RNR with F=0 */
1702 rc = lapd_send_rnr(lctx, 0, 0);
1703 }
1704 }
1705
1706 /* Send message, if possible due to acknowledged data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001707 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001708
1709 return rc;
1710}
1711
1712/* Receive a LAPD message from L1 */
1713int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1714{
1715 int rc;
1716
1717 switch (lctx->format) {
1718 case LAPD_FORM_U:
1719 rc = lapd_rx_u(msg, lctx);
1720 break;
1721 case LAPD_FORM_S:
1722 rc = lapd_rx_s(msg, lctx);
1723 break;
1724 case LAPD_FORM_I:
1725 rc = lapd_rx_i(msg, lctx);
1726 break;
1727 default:
Maxc5695262022-10-09 17:41:46 +03001728 LOGDL(lctx->dl, LOGL_NOTICE, "unknown LAPD format 0x%02x\n", lctx->format);
rootaf48bed2011-09-26 11:23:06 +02001729 msgb_free(msg);
1730 rc = -EINVAL;
1731 }
1732 return rc;
1733}
1734
1735/* L3 -> L2 */
1736
1737/* send unit data */
1738static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1739{
1740 struct lapd_datalink *dl = lctx->dl;
1741 struct msgb *msg = dp->oph.msg;
1742 struct lapd_msg_ctx nctx;
1743
1744 memcpy(&nctx, lctx, sizeof(nctx));
1745 /* keep nctx.ldp */
1746 /* keep nctx.sapi */
1747 /* keep nctx.tei */
1748 nctx.cr = dl->cr.loc2rem.cmd;
1749 nctx.format = LAPD_FORM_U;
1750 nctx.s_u = LAPD_U_UI;
1751 /* keep nctx.p_f */
1752 nctx.length = msg->len;
1753 nctx.more = 0;
1754
1755 return dl->send_ph_data_req(&nctx, msg);
1756}
1757
Max2b283b12022-11-17 22:18:01 +03001758static void msg_to_tx_hist(struct lapd_history *tx_hist, const struct msgb *msg, int length, int more)
1759{
1760 tx_hist->msg = lapd_msgb_alloc(msg->len, "HIST");
1761 tx_hist->more = more;
1762 msgb_put(tx_hist->msg, msg->len);
1763 if (length)
1764 memcpy(tx_hist->msg->data, msg->l3h, msg->len);
1765}
1766
1767static void msg_to_tx_hist0(struct lapd_datalink *dl, const struct msgb *msg)
1768{
1769 return msg_to_tx_hist(&dl->tx_hist[0], msg, msg->len, 0);
1770}
1771
rootaf48bed2011-09-26 11:23:06 +02001772/* request link establishment */
1773static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1774{
1775 struct lapd_datalink *dl = lctx->dl;
1776 struct msgb *msg = dp->oph.msg;
1777 struct lapd_msg_ctx nctx;
1778
1779 if (msg->len)
Harald Welte00b2faf2020-05-02 19:56:36 +02001780 LOGDL(dl, LOGL_INFO, "perform establishment with content (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001781 else
Harald Welte00b2faf2020-05-02 19:56:36 +02001782 LOGDL(dl, LOGL_INFO, "perform normal establishm. (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001783
1784 /* Flush send-queue */
1785 /* Clear send-buffer */
1786 lapd_dl_flush_send(dl);
1787 /* be sure that history is empty */
1788 lapd_dl_flush_hist(dl);
1789
1790 /* save message context for further use */
1791 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1792
1793 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001794 msgb_free(dl->rcv_buffer);
1795 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001796
1797 /* assemble message */
1798 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1799 /* keep nctx.ldp */
1800 /* keep nctx.sapi */
1801 /* keep nctx.tei */
1802 nctx.cr = dl->cr.loc2rem.cmd;
1803 nctx.format = LAPD_FORM_U;
1804 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1805 nctx.p_f = 1;
1806 nctx.length = msg->len;
1807 nctx.more = 0;
1808
1809 /* Transmit-buffer carries exactly one segment */
Max2b283b12022-11-17 22:18:01 +03001810 msg_to_tx_hist0(dl, msg);
1811
rootaf48bed2011-09-26 11:23:06 +02001812 /* set Vs to 0, because it is used as index when resending SABM */
1813 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001814
rootaf48bed2011-09-26 11:23:06 +02001815 /* Set states */
1816 dl->own_busy = dl->peer_busy = 0;
1817 dl->retrans_ctr = 0;
1818 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1819
1820 /* Tramsmit and start T200 */
1821 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001822 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001823
1824 return 0;
1825}
1826
1827/* send data */
1828static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1829{
1830 struct lapd_datalink *dl = lctx->dl;
1831 struct msgb *msg = dp->oph.msg;
1832
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001833 if (msgb_l3len(msg) == 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001834 LOGDL(dl, LOGL_ERROR, "writing an empty message is not possible\n");
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001835 msgb_free(msg);
1836 return -1;
1837 }
1838
Harald Welte00b2faf2020-05-02 19:56:36 +02001839 LOGDL(dl, LOGL_INFO, "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001840
1841 /* Write data into the send queue */
1842 msgb_enqueue(&dl->send_queue, msg);
1843
1844 /* Send message, if possible */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001845 lapd_send_i(dl, __LINE__, false);
rootaf48bed2011-09-26 11:23:06 +02001846
1847 return 0;
1848}
1849
1850/* Send next I frame from queued/buffered data */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001851static int lapd_send_i(struct lapd_datalink *dl, int line, bool rts)
rootaf48bed2011-09-26 11:23:06 +02001852{
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001853 struct lapd_msg_ctx *lctx = &dl->lctx;
rootaf48bed2011-09-26 11:23:06 +02001854 uint8_t k = dl->k;
1855 uint8_t h;
1856 struct msgb *msg;
1857 int length, left;
1858 int rc = - 1; /* we sent nothing */
1859 struct lapd_msg_ctx nctx;
1860
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001861 if (!rts)
1862 LOGDL(dl, LOGL_INFO, "%s() called from line %d\n", __func__, line);
rootaf48bed2011-09-26 11:23:06 +02001863
1864 next_frame:
1865
1866 if (dl->peer_busy) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001867 if (!rts)
1868 LOGDL(dl, LOGL_INFO, "Peer busy, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001869 return rc;
1870 }
1871
1872 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001873 if (!rts)
1874 LOGDL(dl, LOGL_INFO, "Timer recovery, not sending.\n");
rootaf48bed2011-09-26 11:23:06 +02001875 return rc;
1876 }
1877
1878 /* If the send state variable V(S) is equal to V(A) plus k
1879 * (where k is the maximum number of outstanding I frames - see
1880 * subclause 5.8.4), the data link layer entity shall not transmit any
1881 * new I frames, but shall retransmit an I frame as a result
1882 * of the error recovery procedures as described in subclauses 5.5.4 and
1883 * 5.5.7. */
1884 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001885 if (!rts)
1886 LOGDL(dl, LOGL_INFO, "k frames outstanding, not sending more. (k=%u V(S)=%u V(A)=%u)\n",
1887 k, dl->v_send, dl->v_ack);
rootaf48bed2011-09-26 11:23:06 +02001888 return rc;
1889 }
1890
1891 h = do_mod(dl->v_send, dl->range_hist);
1892
1893 /* if we have no tx_hist yet, we create it */
1894 if (!dl->tx_hist[h].msg) {
1895 /* Get next message into send-buffer, if any */
1896 if (!dl->send_buffer) {
1897 next_message:
1898 dl->send_out = 0;
1899 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1900 /* No more data to be sent */
1901 if (!dl->send_buffer)
1902 return rc;
Harald Welte00b2faf2020-05-02 19:56:36 +02001903 LOGDL(dl, LOGL_INFO, "get message from send-queue\n");
rootaf48bed2011-09-26 11:23:06 +02001904 }
1905
1906 /* How much is left in the send-buffer? */
1907 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1908 /* Segment, if data exceeds N201 */
1909 length = left;
1910 if (length > lctx->n201)
1911 length = lctx->n201;
Harald Welte00b2faf2020-05-02 19:56:36 +02001912 LOGDL(dl, LOGL_INFO, "msg-len %d sent %d left %d N201 %d length %d "
1913 "first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left,
1914 lctx->n201, length, dl->send_buffer->l3h[0]);
rootaf48bed2011-09-26 11:23:06 +02001915 /* If message in send-buffer is completely sent */
1916 if (left == 0) {
1917 msgb_free(dl->send_buffer);
1918 dl->send_buffer = NULL;
1919 goto next_message;
1920 }
1921
Harald Welte00b2faf2020-05-02 19:56:36 +02001922 LOGDL(dl, LOGL_INFO, "send I frame %sV(S)=%d\n",
1923 (left > length) ? "segment " : "", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02001924
1925 /* Create I frame (segment) and transmit-buffer content */
1926 msg = lapd_msgb_alloc(length, "LAPD I");
1927 msg->l3h = msgb_put(msg, length);
1928 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001929 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02001930 /* keep nctx.ldp */
1931 /* keep nctx.sapi */
1932 /* keep nctx.tei */
1933 nctx.cr = dl->cr.loc2rem.cmd;
1934 nctx.format = LAPD_FORM_I;
1935 nctx.p_f = 0;
1936 nctx.n_send = dl->v_send;
1937 nctx.n_recv = dl->v_recv;
1938 nctx.length = length;
1939 if (left > length)
1940 nctx.more = 1;
1941 else
1942 nctx.more = 0;
1943 if (length)
1944 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1945 length);
1946 /* store in tx_hist */
Max2b283b12022-11-17 22:18:01 +03001947 msg_to_tx_hist(&dl->tx_hist[h], msg, length, nctx.more);
1948
rootaf48bed2011-09-26 11:23:06 +02001949 /* Add length to track how much is already in the tx buffer */
1950 dl->send_out += length;
1951 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001952 LOGDL(dl, LOGL_INFO, "resend I frame from tx buffer V(S)=%d\n", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02001953
1954 /* Create I frame (segment) from tx_hist */
1955 length = dl->tx_hist[h].msg->len;
1956 msg = lapd_msgb_alloc(length, "LAPD I resend");
1957 msg->l3h = msgb_put(msg, length);
1958 /* assemble message */
Andreas Eversbergcc63aae2023-11-09 11:53:41 +01001959 memcpy(&nctx, lctx, sizeof(nctx));
rootaf48bed2011-09-26 11:23:06 +02001960 /* keep nctx.ldp */
1961 /* keep nctx.sapi */
1962 /* keep nctx.tei */
1963 nctx.cr = dl->cr.loc2rem.cmd;
1964 nctx.format = LAPD_FORM_I;
1965 nctx.p_f = 0;
1966 nctx.n_send = dl->v_send;
1967 nctx.n_recv = dl->v_recv;
1968 nctx.length = length;
1969 nctx.more = dl->tx_hist[h].more;
1970 if (length)
1971 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1972 }
1973
1974 /* The value of the send state variable V(S) shall be incremented by 1
1975 * at the end of the transmission of the I frame */
1976 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1977
1978 /* If timer T200 is not running at the time right before transmitting a
1979 * frame, when the PH-READY-TO-SEND primitive is received from the
1980 * physical layer., it shall be set. */
1981 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001982 /* stop Timer T203, if running */
1983 lapd_stop_t203(dl);
1984 /* start Timer T200 */
1985 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001986 }
1987
1988 dl->send_ph_data_req(&nctx, msg);
1989
1990 rc = 0; /* we sent something */
1991 goto next_frame;
1992}
1993
1994/* request link suspension */
1995static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1996{
1997 struct lapd_datalink *dl = lctx->dl;
1998 struct msgb *msg = dp->oph.msg;
1999
Harald Welte00b2faf2020-05-02 19:56:36 +02002000 LOGDL(dl, LOGL_INFO, "perform suspension\n");
rootaf48bed2011-09-26 11:23:06 +02002001
2002 /* put back the send-buffer to the send-queue (first position) */
2003 if (dl->send_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002004 LOGDL(dl, LOGL_INFO, "put frame in sendbuffer back to queue\n");
rootaf48bed2011-09-26 11:23:06 +02002005 llist_add(&dl->send_buffer->list, &dl->send_queue);
2006 dl->send_buffer = NULL;
2007 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02002008 LOGDL(dl, LOGL_INFO, "no frame in sendbuffer\n");
rootaf48bed2011-09-26 11:23:06 +02002009
2010 /* Clear transmit buffer, but keep send buffer */
2011 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002012 /* Stop timers (there is no state change, so we must stop all timers */
2013 lapd_stop_t200(dl);
2014 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02002015
2016 msgb_free(msg);
2017
2018 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
2019}
2020
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002021/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002022static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2023{
2024 struct lapd_datalink *dl = lctx->dl;
2025 struct msgb *msg = dp->oph.msg;
2026 struct lapd_msg_ctx nctx;
2027
Harald Welte00b2faf2020-05-02 19:56:36 +02002028 LOGDL(dl, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", msg->len);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002029
rootaf48bed2011-09-26 11:23:06 +02002030 /* be sure that history is empty */
2031 lapd_dl_flush_hist(dl);
2032
2033 /* save message context for further use */
2034 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2035
2036 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002037 msgb_free(dl->send_buffer);
2038 dl->send_buffer = NULL;
2039
rootaf48bed2011-09-26 11:23:06 +02002040 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002041 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002042 /* Write data into the send buffer, to be sent first */
2043 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002044 } else {
2045 msgb_free(msg);
2046 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002047 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002048 }
rootaf48bed2011-09-26 11:23:06 +02002049
2050 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002051 msgb_free(dl->rcv_buffer);
2052 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002053
2054 /* Create new msgb (old one is now free) */
2055 msg = lapd_msgb_alloc(0, "LAPD SABM");
2056 msg->l3h = msg->data;
2057 /* assemble message */
2058 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2059 /* keep nctx.ldp */
2060 /* keep nctx.sapi */
2061 /* keep nctx.tei */
2062 nctx.cr = dl->cr.loc2rem.cmd;
2063 nctx.format = LAPD_FORM_U;
2064 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2065 nctx.p_f = 1;
2066 nctx.length = 0;
2067 nctx.more = 0;
2068
Max2b283b12022-11-17 22:18:01 +03002069 msg_to_tx_hist0(dl, msg);
2070
rootaf48bed2011-09-26 11:23:06 +02002071 /* set Vs to 0, because it is used as index when resending SABM */
2072 dl->v_send = 0;
2073
2074 /* Set states */
2075 dl->own_busy = dl->peer_busy = 0;
2076 dl->retrans_ctr = 0;
2077 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2078
2079 /* Tramsmit and start T200 */
2080 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002081 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002082
2083 return 0;
2084}
2085
2086/* requesst release of link */
2087static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2088{
2089 struct lapd_datalink *dl = lctx->dl;
2090 struct msgb *msg = dp->oph.msg;
2091 struct lapd_msg_ctx nctx;
2092
2093 /* local release */
2094 if (dp->u.rel_req.mode) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002095 LOGDL(dl, LOGL_INFO, "perform local release\n");
rootaf48bed2011-09-26 11:23:06 +02002096 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002097 /* stop Timer T200 */
2098 lapd_stop_t200(dl);
2099 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002100 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2101 /* flush buffers */
2102 lapd_dl_flush_tx(dl);
2103 lapd_dl_flush_send(dl);
2104 /* send notification to L3 */
2105 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2106 }
2107
2108 /* in case we are already disconnecting */
2109 if (dl->state == LAPD_STATE_DISC_SENT)
2110 return -EBUSY;
2111
2112 /* flush tx_hist */
2113 lapd_dl_flush_hist(dl);
2114
Harald Welte00b2faf2020-05-02 19:56:36 +02002115 LOGDL(dl, LOGL_INFO, "perform normal release (DISC)\n");
rootaf48bed2011-09-26 11:23:06 +02002116
2117 /* Push LAPD header on msgb */
2118 /* assemble message */
2119 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2120 /* keep nctx.ldp */
2121 /* keep nctx.sapi */
2122 /* keep nctx.tei */
2123 nctx.cr = dl->cr.loc2rem.cmd;
2124 nctx.format = LAPD_FORM_U;
2125 nctx.s_u = LAPD_U_DISC;
2126 nctx.p_f = 1;
2127 nctx.length = 0;
2128 nctx.more = 0;
2129
Max2b283b12022-11-17 22:18:01 +03002130 msg_to_tx_hist0(dl, msg);
2131
rootaf48bed2011-09-26 11:23:06 +02002132 /* set Vs to 0, because it is used as index when resending DISC */
2133 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002134
rootaf48bed2011-09-26 11:23:06 +02002135 /* Set states */
2136 dl->own_busy = dl->peer_busy = 0;
2137 dl->retrans_ctr = 0;
2138 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2139
2140 /* Tramsmit and start T200 */
2141 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002142 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002143
2144 return 0;
2145}
2146
2147/* request release of link in idle state */
2148static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2149 struct lapd_msg_ctx *lctx)
2150{
2151 struct lapd_datalink *dl = lctx->dl;
2152 struct msgb *msg = dp->oph.msg;
2153
2154 msgb_free(msg);
2155
2156 /* send notification to L3 */
2157 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2158}
2159
2160/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002161static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002162 uint32_t states;
2163 int prim, op;
2164 const char *name;
2165 int (*rout) (struct osmo_dlsap_prim *dp,
2166 struct lapd_msg_ctx *lctx);
2167} l2downstatelist[] = {
2168 /* create and send UI command */
2169 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002170 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002171 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2172
2173 /* create and send SABM command */
2174 {SBIT(LAPD_STATE_IDLE),
2175 PRIM_DL_EST, PRIM_OP_REQUEST,
2176 "DL-ESTABLISH-REQUEST", lapd_est_req},
2177
2178 /* create and send I command */
2179 {SBIT(LAPD_STATE_MF_EST) |
2180 SBIT(LAPD_STATE_TIMER_RECOV),
2181 PRIM_DL_DATA, PRIM_OP_REQUEST,
2182 "DL-DATA-REQUEST", lapd_data_req},
2183
2184 /* suspend datalink */
2185 {SBIT(LAPD_STATE_MF_EST) |
2186 SBIT(LAPD_STATE_TIMER_RECOV),
2187 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2188 "DL-SUSPEND-REQUEST", lapd_susp_req},
2189
2190 /* create and send SABM command (resume) */
2191 {SBIT(LAPD_STATE_MF_EST) |
2192 SBIT(LAPD_STATE_TIMER_RECOV),
2193 PRIM_DL_RES, PRIM_OP_REQUEST,
2194 "DL-RESUME-REQUEST", lapd_res_req},
2195
2196 /* create and send SABM command (reconnect) */
2197 {SBIT(LAPD_STATE_IDLE) |
2198 SBIT(LAPD_STATE_MF_EST) |
2199 SBIT(LAPD_STATE_TIMER_RECOV),
2200 PRIM_DL_RECON, PRIM_OP_REQUEST,
2201 "DL-RECONNECT-REQUEST", lapd_res_req},
2202
2203 /* create and send DISC command */
2204 {SBIT(LAPD_STATE_SABM_SENT) |
2205 SBIT(LAPD_STATE_MF_EST) |
2206 SBIT(LAPD_STATE_TIMER_RECOV) |
2207 SBIT(LAPD_STATE_DISC_SENT),
2208 PRIM_DL_REL, PRIM_OP_REQUEST,
2209 "DL-RELEASE-REQUEST", lapd_rel_req},
2210
2211 /* release in idle state */
2212 {SBIT(LAPD_STATE_IDLE),
2213 PRIM_DL_REL, PRIM_OP_REQUEST,
2214 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2215};
2216
2217#define L2DOWNSLLEN \
2218 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2219
2220int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2221{
2222 struct lapd_datalink *dl = lctx->dl;
2223 int i, supported = 0;
2224 struct msgb *msg = dp->oph.msg;
2225 int rc;
2226
2227 /* find function for current state and message */
2228 for (i = 0; i < L2DOWNSLLEN; i++) {
2229 if (dp->oph.primitive == l2downstatelist[i].prim
2230 && dp->oph.operation == l2downstatelist[i].op) {
2231 supported = 1;
2232 if ((SBIT(dl->state) & l2downstatelist[i].states))
2233 break;
2234 }
2235 }
2236 if (!supported) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002237 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unsupported\n",
2238 dp->oph.primitive, dp->oph.operation);
rootaf48bed2011-09-26 11:23:06 +02002239 msgb_free(msg);
2240 return 0;
2241 }
2242 if (i == L2DOWNSLLEN) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002243 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unhandled at this state %s\n",
2244 dp->oph.primitive, dp->oph.operation, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002245 msgb_free(msg);
2246 return 0;
2247 }
2248
Harald Welte00b2faf2020-05-02 19:56:36 +02002249 LOGDL(dl, LOGL_INFO, "Message %s received in state %s\n",
2250 l2downstatelist[i].name, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002251
2252 rc = l2downstatelist[i].rout(dp, lctx);
2253
2254 return rc;
2255}
2256
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002257/*! @} */