blob: 5674c661a24c57a380bc73155ef48ca0065890ca [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);
111static int lapd_send_i(struct lapd_msg_ctx *lctx, int line);
112static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
113
rootaf48bed2011-09-26 11:23:06 +0200114/* UTILITY FUNCTIONS */
115
116struct msgb *lapd_msgb_alloc(int length, const char *name)
117{
118 /* adding space for padding, FIXME: add as an option */
119 if (length < 21)
120 length = 21;
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
172static void lapd_dl_flush_tx(struct lapd_datalink *dl)
173{
174 struct msgb *msg;
175
176 while ((msg = msgb_dequeue(&dl->tx_queue)))
177 msgb_free(msg);
178 lapd_dl_flush_hist(dl);
179}
180
181/* Figure B.2/Q.921 */
Harald Weltec733d142017-03-15 10:20:51 +0100182const struct value_string lapd_state_names[] = {
183 OSMO_VALUE_STRING(LAPD_STATE_NULL),
184 OSMO_VALUE_STRING(LAPD_STATE_TEI_UNASS),
185 OSMO_VALUE_STRING(LAPD_STATE_ASS_TEI_WAIT),
186 OSMO_VALUE_STRING(LAPD_STATE_EST_TEI_WAIT),
187 OSMO_VALUE_STRING(LAPD_STATE_IDLE),
188 OSMO_VALUE_STRING(LAPD_STATE_SABM_SENT),
189 OSMO_VALUE_STRING(LAPD_STATE_DISC_SENT),
190 OSMO_VALUE_STRING(LAPD_STATE_MF_EST),
191 OSMO_VALUE_STRING(LAPD_STATE_TIMER_RECOV),
192 { 0, NULL }
rootaf48bed2011-09-26 11:23:06 +0200193};
194
Harald Weltec733d142017-03-15 10:20:51 +0100195static inline const char *lapd_state_name(enum lapd_state state)
196{
197 return get_value_string(lapd_state_names, state);
198}
199
Andreas Eversberg742fc792011-09-27 09:40:25 +0200200static void lapd_start_t200(struct lapd_datalink *dl)
201{
202 if (osmo_timer_pending(&dl->t200))
203 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200204 LOGDL(dl, LOGL_INFO, "start T200 (timeout=%d.%06ds)\n",
205 dl->t200_sec, dl->t200_usec);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200206 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
207}
208
209static void lapd_start_t203(struct lapd_datalink *dl)
210{
211 if (osmo_timer_pending(&dl->t203))
212 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200213 LOGDL(dl, LOGL_INFO, "start T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200214 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
215}
216
217static void lapd_stop_t200(struct lapd_datalink *dl)
218{
219 if (!osmo_timer_pending(&dl->t200))
220 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200221 LOGDL(dl, LOGL_INFO, "stop T200\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200222 osmo_timer_del(&dl->t200);
223}
224
225static void lapd_stop_t203(struct lapd_datalink *dl)
226{
227 if (!osmo_timer_pending(&dl->t203))
228 return;
Harald Welte00b2faf2020-05-02 19:56:36 +0200229 LOGDL(dl, LOGL_INFO, "stop T203\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200230 osmo_timer_del(&dl->t203);
231}
232
rootaf48bed2011-09-26 11:23:06 +0200233static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
234{
Harald Welte00b2faf2020-05-02 19:56:36 +0200235 LOGDL(dl, LOGL_INFO, "new state %s -> %s\n",
236 lapd_state_name(dl->state), lapd_state_name(state));
rootaf48bed2011-09-26 11:23:06 +0200237
238 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
239 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200240 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200241 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200242 msgb_free(dl->cont_res);
243 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200244 }
245
246 /* start T203 on entering MF EST state, if enabled */
247 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200248 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
249 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200250
251 dl->state = state;
252}
253
Harald Welte00b2faf2020-05-02 19:56:36 +0200254void *tall_lapd_ctx = NULL;
rootaf48bed2011-09-26 11:23:06 +0200255
Harald Welte00b2faf2020-05-02 19:56:36 +0200256/*! Initialize LAPD datalink instance and allocate history
257 * \param[in] dl caller-allocated datalink structure
258 * \param[in] k maximum number of unacknowledged frames
259 * \param[in] v_range range of sequence numbers
260 * \param[in] maxf maximum frame size (after defragmentation)
261 * \param[in] name human-readable name for this LAPD datalink */
262void lapd_dl_init2(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf,
263 const char *name)
rootaf48bed2011-09-26 11:23:06 +0200264{
265 int m;
266
267 memset(dl, 0, sizeof(*dl));
268 INIT_LLIST_HEAD(&dl->send_queue);
269 INIT_LLIST_HEAD(&dl->tx_queue);
270 dl->reestablish = 1;
271 dl->n200_est_rel = 3;
272 dl->n200 = 3;
273 dl->t200_sec = 1;
274 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200275 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200276 dl->t203_sec = 10;
277 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200278 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200279 dl->maxf = maxf;
280 if (k > v_range - 1)
281 k = v_range - 1;
282 dl->k = k;
283 dl->v_range = v_range;
284
285 /* Calculate modulo for history array:
286 * - The history range must be at least k+1.
287 * - The history range must be 2^x, where x is as low as possible.
288 */
289 k++;
290 for (m = 0x80; m; m >>= 1) {
291 if ((m & k)) {
292 if (k > m)
293 m <<= 1;
294 dl->range_hist = m;
295 break;
296 }
297 }
298
Harald Welte00b2faf2020-05-02 19:56:36 +0200299 if (!tall_lapd_ctx) {
300 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
301 OSMO_ASSERT(tall_lapd_ctx);
302 }
303
304 talloc_free(dl->name);
305 if (name)
306 dl->name = talloc_strdup(tall_lapd_ctx, name);
307 else
308 dl->name = talloc_asprintf(tall_lapd_ctx, "dl=%p", dl);
309
310 LOGDL(dl, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
311 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200312
313 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
314
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100315 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
316 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200317}
318
Harald Welte00b2faf2020-05-02 19:56:36 +0200319/*! Initialize LAPD datalink instance and allocate history
320 * \param[in] dl caller-allocated datalink structure
321 * \param[in] k maximum number of unacknowledged frames
322 * \param[in] v_range range of sequence numbers
323 * \param[in] maxf maximum frame size (after defragmentation) */
324void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range, int maxf)
325{
326 lapd_dl_init2(dl, k, v_range, maxf, NULL);
327}
328
329void lapd_dl_set_name(struct lapd_datalink *dl, const char *name)
330{
331 if (!name)
332 return;
333 osmo_talloc_replace_string(tall_lapd_ctx, &dl->name, name);
334}
335
rootaf48bed2011-09-26 11:23:06 +0200336/* reset to IDLE state */
337void lapd_dl_reset(struct lapd_datalink *dl)
338{
Harald Welteef5b9b62020-06-07 22:29:53 +0200339 LOGDL(dl, LOGL_INFO, "Resetting LAPD instance\n");
Jean-Francois Dionne893979c2017-03-02 10:45:53 -0500340 /* enter idle state (and remove eventual cont_res) */
341 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200342 /* flush buffer */
343 lapd_dl_flush_tx(dl);
344 lapd_dl_flush_send(dl);
345 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200346 msgb_free(dl->rcv_buffer);
347 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200348 /* stop Timers */
349 lapd_stop_t200(dl);
350 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500351 if (dl->state == LAPD_STATE_IDLE)
352 return;
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500353 /* enter idle state (and remove eventual cont_res) */
354 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200355}
356
357/* reset and de-allocate history buffer */
358void lapd_dl_exit(struct lapd_datalink *dl)
359{
360 /* free all ressources except history buffer */
361 lapd_dl_reset(dl);
Ivan Kluchnikovb9759db2017-05-11 15:19:23 +0300362
363 /* enter null state */
364 lapd_dl_newstate(dl, LAPD_STATE_NULL);
365
rootaf48bed2011-09-26 11:23:06 +0200366 /* free history buffer list */
367 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200368 dl->tx_hist = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200369 talloc_free(dl->name);
370 dl->name = NULL;
rootaf48bed2011-09-26 11:23:06 +0200371}
372
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200373/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200374int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
375{
376 switch (mode) {
377 case LAPD_MODE_USER:
378 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
379 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
380 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
381 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
382 break;
383 case LAPD_MODE_NETWORK:
384 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
385 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
386 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
387 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
388 break;
389 default:
390 return -EINVAL;
391 }
392 dl->mode = mode;
393
394 return 0;
395}
396
397/* send DL message with optional msgb */
398static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
399 struct msgb *msg)
400{
401 struct lapd_datalink *dl = lctx->dl;
402 struct osmo_dlsap_prim dp;
403
404 osmo_prim_init(&dp.oph, 0, prim, op, msg);
405 return dl->send_dlsap(&dp, lctx);
406}
407
408/* send simple DL message */
409static inline int send_dl_simple(uint8_t prim, uint8_t op,
410 struct lapd_msg_ctx *lctx)
411{
Pau Espin Pedrol9dd3bf02016-02-29 08:49:22 -0500412 return send_dl_l3(prim, op, lctx, NULL);
rootaf48bed2011-09-26 11:23:06 +0200413}
414
415/* send MDL-ERROR INDICATION */
416static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
417{
418 struct lapd_datalink *dl = lctx->dl;
419 struct osmo_dlsap_prim dp;
420
Harald Welte00b2faf2020-05-02 19:56:36 +0200421 LOGDL(dl, LOGL_NOTICE,
422 "sending MDL-ERROR-IND cause %d from state %s\n",
423 cause, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200424 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
425 dp.u.error_ind.cause = cause;
426 return dl->send_dlsap(&dp, lctx);
427}
428
429/* send UA response */
430static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
431{
432 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
433 struct lapd_msg_ctx nctx;
434 struct lapd_datalink *dl = lctx->dl;
435
436 memcpy(&nctx, lctx, sizeof(nctx));
437 msg->l3h = msgb_put(msg, len);
438 if (len)
439 memcpy(msg->l3h, data, len);
440 /* keep nctx.ldp */
441 /* keep nctx.sapi */
442 /* keep nctx.tei */
443 nctx.cr = dl->cr.loc2rem.resp;
444 nctx.format = LAPD_FORM_U;
445 nctx.s_u = LAPD_U_UA;
446 /* keep nctx.p_f */
447 nctx.length = len;
448 nctx.more = 0;
449
450 return dl->send_ph_data_req(&nctx, msg);
451}
452
453/* send DM response */
454static int lapd_send_dm(struct lapd_msg_ctx *lctx)
455{
456 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
457 struct lapd_msg_ctx nctx;
458 struct lapd_datalink *dl = lctx->dl;
459
460 memcpy(&nctx, lctx, sizeof(nctx));
461 /* keep nctx.ldp */
462 /* keep nctx.sapi */
463 /* keep nctx.tei */
464 nctx.cr = dl->cr.loc2rem.resp;
465 nctx.format = LAPD_FORM_U;
466 nctx.s_u = LAPD_U_DM;
467 /* keep nctx.p_f */
468 nctx.length = 0;
469 nctx.more = 0;
470
471 return dl->send_ph_data_req(&nctx, msg);
472}
473
474/* send RR response / command */
475static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
476{
477 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
478 struct lapd_msg_ctx nctx;
479 struct lapd_datalink *dl = lctx->dl;
480
481 memcpy(&nctx, lctx, sizeof(nctx));
482 /* keep nctx.ldp */
483 /* keep nctx.sapi */
484 /* keep nctx.tei */
485 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
486 nctx.format = LAPD_FORM_S;
487 nctx.s_u = LAPD_S_RR;
488 nctx.p_f = f_bit;
489 nctx.n_recv = dl->v_recv;
490 nctx.length = 0;
491 nctx.more = 0;
492
493 return dl->send_ph_data_req(&nctx, msg);
494}
495
496/* send RNR response / command */
497static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
498{
499 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
500 struct lapd_msg_ctx nctx;
501 struct lapd_datalink *dl = lctx->dl;
502
503 memcpy(&nctx, lctx, sizeof(nctx));
504 /* keep nctx.ldp */
505 /* keep nctx.sapi */
506 /* keep nctx.tei */
507 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
508 nctx.format = LAPD_FORM_S;
509 nctx.s_u = LAPD_S_RNR;
510 nctx.p_f = f_bit;
511 nctx.n_recv = dl->v_recv;
512 nctx.length = 0;
513 nctx.more = 0;
514
515 return dl->send_ph_data_req(&nctx, msg);
516}
517
518/* send REJ response */
519static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
520{
521 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
522 struct lapd_msg_ctx nctx;
523 struct lapd_datalink *dl = lctx->dl;
524
525 memcpy(&nctx, lctx, sizeof(nctx));
526 /* keep nctx.ldp */
527 /* keep nctx.sapi */
528 /* keep nctx.tei */
529 nctx.cr = dl->cr.loc2rem.resp;
530 nctx.format = LAPD_FORM_S;
531 nctx.s_u = LAPD_S_REJ;
532 nctx.p_f = f_bit;
533 nctx.n_recv = dl->v_recv;
534 nctx.length = 0;
535 nctx.more = 0;
536
537 return dl->send_ph_data_req(&nctx, msg);
538}
539
540/* resend SABM or DISC message */
541static int lapd_send_resend(struct lapd_datalink *dl)
542{
543 struct msgb *msg;
544 uint8_t h = do_mod(dl->v_send, dl->range_hist);
545 int length = dl->tx_hist[h].msg->len;
546 struct lapd_msg_ctx nctx;
547
548 /* assemble message */
549 memcpy(&nctx, &dl->lctx, sizeof(nctx));
550 /* keep nctx.ldp */
551 /* keep nctx.sapi */
552 /* keep nctx.tei */
553 nctx.cr = dl->cr.loc2rem.cmd;
554 nctx.format = LAPD_FORM_U;
555 if (dl->state == LAPD_STATE_SABM_SENT)
556 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
557 else
558 nctx.s_u = LAPD_U_DISC;
559 nctx.p_f = 1;
560 nctx.length = length;
561 nctx.more = 0;
562
563 /* Resend SABM/DISC from tx_hist */
564 msg = lapd_msgb_alloc(length, "LAPD resend");
565 msg->l3h = msgb_put(msg, length);
566 if (length)
567 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
568
569 return dl->send_ph_data_req(&nctx, msg);
570}
571
572/* reestablish link */
573static int lapd_reestablish(struct lapd_datalink *dl)
574{
575 struct osmo_dlsap_prim dp;
576 struct msgb *msg;
577
Harald Welte00b2faf2020-05-02 19:56:36 +0200578 LOGDL(dl, LOGL_DEBUG, "LAPD reestablish\n");
Philipp Maier08177d32016-12-08 17:23:26 +0100579
rootaf48bed2011-09-26 11:23:06 +0200580 msg = lapd_msgb_alloc(0, "DUMMY");
581 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100582
rootaf48bed2011-09-26 11:23:06 +0200583 return lapd_est_req(&dp, &dl->lctx);
584}
585
586/* Timer callback on T200 expiry */
587static void lapd_t200_cb(void *data)
588{
589 struct lapd_datalink *dl = data;
590
Harald Welte00b2faf2020-05-02 19:56:36 +0200591 LOGDL(dl, LOGL_INFO, "Timeout T200 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200592
593 switch (dl->state) {
594 case LAPD_STATE_SABM_SENT:
595 /* 5.4.1.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200596 /* increment re-transmission counter */
597 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200598 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200599 /* flush tx and send buffers */
600 lapd_dl_flush_tx(dl);
601 lapd_dl_flush_send(dl);
602 /* go back to idle state */
603 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
604 /* NOTE: we must not change any other states or buffers
605 * and queues, since we may reconnect after handover
606 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100607 /* send MDL ERROR INIDCATION to L3 */
608 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100609 /* send RELEASE INDICATION to L3 */
610 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
611 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200612 break;
613 }
614 /* retransmit SABM command */
615 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200616 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200617 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200618 break;
619 case LAPD_STATE_DISC_SENT:
620 /* 5.4.4.3 */
Andreas Eversberg676b3612023-10-20 14:22:40 +0200621 /* increment re-transmission counter */
622 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200623 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200624 /* send MDL ERROR INIDCATION to L3 */
625 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
626 /* flush tx and send buffers */
627 lapd_dl_flush_tx(dl);
628 lapd_dl_flush_send(dl);
629 /* go back to idle state */
630 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
631 /* NOTE: we must not change any other states or buffers
632 * and queues, since we may reconnect after handover
633 * failure. the buffered messages is replaced there */
Harald Welted2a61172020-12-21 17:43:54 +0100634 /* send RELEASE INDICATION to L3 */
635 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200636 break;
637 }
638 /* retransmit DISC command */
639 lapd_send_resend(dl);
rootaf48bed2011-09-26 11:23:06 +0200640 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200641 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200642 break;
643 case LAPD_STATE_MF_EST:
644 /* 5.5.7 */
645 dl->retrans_ctr = 0;
646 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
647 /* fall through */
648 case LAPD_STATE_TIMER_RECOV:
649 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200650 if (dl->retrans_ctr <= dl->n200) {
rootaf48bed2011-09-26 11:23:06 +0200651 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
652 uint8_t h = do_mod(vs, dl->range_hist);
653 /* retransmit I frame (V_s-1) with P=1, if any */
654 if (dl->tx_hist[h].msg) {
655 struct msgb *msg;
656 int length = dl->tx_hist[h].msg->len;
657 struct lapd_msg_ctx nctx;
658
Harald Welte00b2faf2020-05-02 19:56:36 +0200659 LOGDL(dl, LOGL_INFO, "retransmit last frame V(S)=%d\n", vs);
rootaf48bed2011-09-26 11:23:06 +0200660 /* Create I frame (segment) from tx_hist */
661 memcpy(&nctx, &dl->lctx, sizeof(nctx));
662 /* keep nctx.ldp */
663 /* keep nctx.sapi */
664 /* keep nctx.tei */
665 nctx.cr = dl->cr.loc2rem.cmd;
666 nctx.format = LAPD_FORM_I;
667 nctx.p_f = 1;
668 nctx.n_send = vs;
669 nctx.n_recv = dl->v_recv;
670 nctx.length = length;
671 nctx.more = dl->tx_hist[h].more;
672 msg = lapd_msgb_alloc(length, "LAPD I resend");
673 msg->l3h = msgb_put(msg, length);
674 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
675 length);
676 dl->send_ph_data_req(&nctx, msg);
677 } else {
678 /* OR send appropriate supervision frame with P=1 */
679 if (!dl->own_busy && !dl->seq_err_cond) {
680 lapd_send_rr(&dl->lctx, 1, 1);
681 /* NOTE: In case of sequence error
682 * condition, the REJ frame has been
683 * transmitted when entering the
684 * condition, so it has not be done
685 * here
686 */
687 } else if (dl->own_busy) {
688 lapd_send_rnr(&dl->lctx, 1, 1);
689 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200690 LOGDL(dl, LOGL_INFO, "unhandled, pls. fix\n");
rootaf48bed2011-09-26 11:23:06 +0200691 }
692 }
693 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200694 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200695 } else {
696 /* send MDL ERROR INIDCATION to L3 */
697 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
698 /* reestablish */
699 if (!dl->reestablish)
700 break;
Harald Welte00b2faf2020-05-02 19:56:36 +0200701 LOGDL(dl, LOGL_NOTICE, "N200+1 reached, performingreestablishment\n");
rootaf48bed2011-09-26 11:23:06 +0200702 lapd_reestablish(dl);
703 }
704 break;
705 default:
Harald Welte00b2faf2020-05-02 19:56:36 +0200706 LOGDL(dl, LOGL_INFO, "T200 expired in unexpected dl->state %s)\n",
707 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200708 }
709}
710
711/* Timer callback on T203 expiry */
712static void lapd_t203_cb(void *data)
713{
714 struct lapd_datalink *dl = data;
715
Harald Welte00b2faf2020-05-02 19:56:36 +0200716 LOGDL(dl, LOGL_INFO, "Timeout T203 state=%s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +0200717
718 if (dl->state != LAPD_STATE_MF_EST) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200719 LOGDL(dl, LOGL_ERROR, "T203 fired outside MF EST state, please fix!\n");
rootaf48bed2011-09-26 11:23:06 +0200720 return;
721 }
722
723 /* set retransmission counter to 0 */
724 dl->retrans_ctr = 0;
725 /* enter timer recovery state */
726 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
727 /* transmit a supervisory command with P bit set to 1 as follows: */
728 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200729 LOGDL(dl, LOGL_INFO, "transmit an RR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200730 /* Send RR with P=1 */
731 lapd_send_rr(&dl->lctx, 1, 1);
732 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +0200733 LOGDL(dl, LOGL_INFO, "transmit an RNR poll command\n");
rootaf48bed2011-09-26 11:23:06 +0200734 /* Send RNR with P=1 */
735 lapd_send_rnr(&dl->lctx, 1, 1);
736 }
737 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200738 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200739}
740
741/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
742static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
743{
744 struct lapd_datalink *dl = lctx->dl;
745 uint8_t nr = lctx->n_recv;
Max327e5e92022-10-11 19:48:44 +0300746 int s = 0, rej = 0;
747 bool t200_reset = false;
rootaf48bed2011-09-26 11:23:06 +0200748 int i, h;
749
750 /* supervisory frame ? */
751 if (lctx->format == LAPD_FORM_S)
752 s = 1;
753 /* REJ frame ? */
754 if (s && lctx->s_u == LAPD_S_REJ)
755 rej = 1;
756
757 /* Flush all transmit buffers of acknowledged frames */
758 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
759 h = do_mod(i, dl->range_hist);
760 if (dl->tx_hist[h].msg) {
761 msgb_free(dl->tx_hist[h].msg);
762 dl->tx_hist[h].msg = NULL;
Harald Welte00b2faf2020-05-02 19:56:36 +0200763 LOGDL(dl, LOGL_INFO, "ack frame %d\n", i);
rootaf48bed2011-09-26 11:23:06 +0200764 }
765 }
766
767 if (dl->state != LAPD_STATE_TIMER_RECOV) {
768 /* When not in the timer recovery condition, the data
769 * link layer entity shall reset the timer T200 on
770 * receipt of a valid I frame with N(R) higher than V(A),
771 * or an REJ with an N(R) equal to V(A). */
Max68588c52022-10-11 19:34:43 +0300772 if ((!rej && nr != dl->v_ack) || (rej && nr == dl->v_ack)) {
Max327e5e92022-10-11 19:48:44 +0300773 t200_reset = true;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200774 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200775 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
776 }
777 /* 5.7.4: N(R) sequence error
778 * N(R) is called valid, if and only if
779 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
780 */
Max68588c52022-10-11 19:34:43 +0300781 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 +0200782 LOGDL(dl, LOGL_NOTICE, "N(R) sequence error\n");
rootaf48bed2011-09-26 11:23:06 +0200783 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
784 }
785 }
786
787 /* V(A) shall be set to the value of N(R) */
788 dl->v_ack = nr;
789
Andreas Eversberg742fc792011-09-27 09:40:25 +0200790 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200791 * and if there are outstanding I frames, restart T200 */
792 if (t200_reset && !rej) {
793 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
Harald Welte00b2faf2020-05-02 19:56:36 +0200794 LOGDL(dl, LOGL_INFO, "start T200, due to unacked I frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200795 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200796 }
797 }
798
799 /* This also does a restart, when I or S frame is received */
800
801 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200802 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200803 /* Start T203, if T200 is not running in MF EST state, if enabled */
804 if (!osmo_timer_pending(&dl->t200)
805 && (dl->t203_sec || dl->t203_usec)
806 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200807 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200808 }
809}
810
811/* L1 -> L2 */
812
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200813/* Receive a LAPD U SABM(E) message from L1 */
814static int lapd_rx_u_sabm(struct msgb *msg, struct lapd_msg_ctx *lctx)
rootaf48bed2011-09-26 11:23:06 +0200815{
816 struct lapd_datalink *dl = lctx->dl;
817 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100818 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200819 uint8_t prim, op;
820
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200821 prim = PRIM_DL_EST;
822 op = PRIM_OP_INDICATION;
rootaf48bed2011-09-26 11:23:06 +0200823
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200824 LOGDL(dl, LOGL_INFO, "SABM(E) received in state %s\n", lapd_state_name(dl->state));
825 /* 5.7.1 */
826 dl->seq_err_cond = 0;
827 /* G.2.2 Wrong value of the C/R bit */
828 if (lctx->cr == dl->cr.rem2loc.resp) {
829 LOGDL(dl, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200830 msgb_free(msg);
831 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
832 return -EINVAL;
833 }
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200834
835 /* G.4.5 If SABM is received with L>N201 or with M bit
836 * set, AN MDL-ERROR-INDICATION is sent to MM.
837 */
838 if (lctx->more || length > lctx->n201) {
839 LOGDL(dl, LOGL_ERROR, "SABM too large error\n");
840 msgb_free(msg);
841 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
842 return -EIO;
843 }
844
845 switch (dl->state) {
846 case LAPD_STATE_IDLE:
847 break;
Pau Espin Pedrol76190d32020-10-21 13:05:44 +0200848 case LAPD_STATE_TIMER_RECOV:
849 LOGDL(dl, LOGL_INFO, "SABM command, timer recovery state\n");
850 /* If link is lost on the remote side, we start over
851 * and send DL-ESTABLISH indication again. */
852 /* 3GPP TS 44.006 8.6.3 "Procedures for re-establishment" */
853 if (length) {
854 /* check for contention resoultion */
855 LOGDL(dl, LOGL_ERROR, "SABM L>0 not expected in timer "
856 "recovery state\n");
857 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
858 lapd_send_dm(lctx);
859 msgb_free(msg);
860 return 0;
861 }
862 /* re-establishment, continue below */
863 lapd_stop_t200(dl);
864 break;
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200865 case LAPD_STATE_MF_EST:
866 LOGDL(dl, LOGL_INFO, "SABM command, multiple frame established state\n");
867 /* If link is lost on the remote side, we start over
868 * and send DL-ESTABLISH indication again. */
869 /* Additionally, continue in case of content resoltion
870 * (GSM network). This happens, if the mobile has not
871 * yet received UA or another mobile (collision) tries
872 * to establish connection. The mobile must receive
873 * UA again. */
874 /* 5.4.2.1 */
875 if (!length) {
876 /* If no content resolution, this is a
877 * re-establishment. */
878 LOGDL(dl, LOGL_INFO, "Remote reestablish\n");
879 break;
880 }
881 if (!dl->cont_res) {
882 LOGDL(dl, LOGL_INFO, "SABM command not allowed in state %s\n",
883 lapd_state_name(dl->state));
884 mdl_error(MDL_CAUSE_SABM_MF, lctx);
885 msgb_free(msg);
886 return 0;
887 }
888 /* Ignore SABM if content differs from first SABM. */
889 if (dl->mode == LAPD_MODE_NETWORK && length) {
890#ifdef TEST_CONTENT_RESOLUTION_NETWORK
891 dl->cont_res->data[0] ^= 0x01;
892#endif
893 if (memcmp(dl->cont_res->data, msg->data,
894 length)) {
895 LOGDL(dl, LOGL_INFO, "Another SABM with different content - "
896 "ignoring!\n");
897 msgb_free(msg);
898 return 0;
899 }
900 }
901 /* send UA again */
902 lapd_send_ua(lctx, length, msg->l3h);
903 msgb_free(msg);
904 return 0;
905 case LAPD_STATE_DISC_SENT:
906 /* 5.4.6.2 send DM with F=P */
907 lapd_send_dm(lctx);
908 /* stop Timer T200 */
909 lapd_stop_t200(dl);
910 msgb_free(msg);
911 return send_dl_simple(prim, op, lctx);
912 default:
913 /* collision: Send UA, but still wait for rx UA, then
914 * change to MF_EST state.
915 */
916 /* check for contention resoultion */
917 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
918 LOGDL(dl, LOGL_NOTICE, "SABM not allowed during contention "
919 "resolution (state=%s)\n", lapd_state_name(dl->state));
920 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
921 }
922 lapd_send_ua(lctx, length, msg->l3h);
923 msgb_free(msg);
924 return 0;
925 }
926 /* save message context for further use */
927 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
928#ifndef TEST_CONTENT_RESOLUTION_NETWORK
929 /* send UA response */
930 lapd_send_ua(lctx, length, msg->l3h);
931#endif
932 /* set Vs, Vr and Va to 0 */
933 dl->v_send = dl->v_recv = dl->v_ack = 0;
934 /* clear tx_hist */
935 lapd_dl_flush_hist(dl);
936 /* enter multiple-frame-established state */
937 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
938 /* store content resolution data on network side
939 * Note: cont_res will be removed when changing state again,
940 * so it must be allocated AFTER lapd_dl_newstate(). */
941 if (dl->mode == LAPD_MODE_NETWORK && length) {
942 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
943 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
944 length);
Pau Espin Pedrol28b404f2022-03-02 19:03:32 +0100945 LOGDL(dl, LOGL_INFO, "Store content res.\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200946 }
947 /* send notification to L3 */
948 if (length == 0) {
949 /* 5.4.1.2 Normal establishment procedures */
950 rc = send_dl_simple(prim, op, lctx);
951 msgb_free(msg);
952 } else {
953 /* 5.4.1.4 Contention resolution establishment */
954 msgb_trim(msg, length);
955 rc = send_dl_l3(prim, op, lctx, msg);
956 }
rootaf48bed2011-09-26 11:23:06 +0200957 return rc;
958}
959
Pau Espin Pedrold5f71472020-10-21 13:02:43 +0200960/* Receive a LAPD U DM message from L1 */
961static int lapd_rx_u_dm(struct msgb *msg, struct lapd_msg_ctx *lctx)
962{
963 struct lapd_datalink *dl = lctx->dl;
964 int rc = 0;
965
966 LOGDL(dl, LOGL_INFO, "DM received in state %s\n", lapd_state_name(dl->state));
967 /* G.2.2 Wrong value of the C/R bit */
968 if (lctx->cr == dl->cr.rem2loc.cmd) {
969 LOGDL(dl, LOGL_ERROR, "DM command error\n");
970 msgb_free(msg);
971 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
972 return -EINVAL;
973 }
974 if (!lctx->p_f) {
975 /* 5.4.1.2 DM responses with the F bit set to "0"
976 * shall be ignored.
977 */
978 msgb_free(msg);
979 return 0;
980 }
981 switch (dl->state) {
982 case LAPD_STATE_SABM_SENT:
983 break;
984 case LAPD_STATE_MF_EST:
985 if (lctx->p_f) {
986 LOGDL(dl, LOGL_INFO, "unsolicited DM response\n");
987 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
988 } else {
989 LOGDL(dl, LOGL_INFO, "unsolicited DM response, "
990 "multiple frame established state\n");
991 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
992 /* reestablish */
993 if (!dl->reestablish) {
994 msgb_free(msg);
995 return 0;
996 }
997 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
998 lapd_reestablish(dl);
999 }
1000 msgb_free(msg);
1001 return 0;
1002 case LAPD_STATE_TIMER_RECOV:
1003 /* FP = 0 (DM is normal in case PF = 1) */
1004 if (!lctx->p_f) {
1005 LOGDL(dl, LOGL_INFO, "unsolicited DM response, multiple frame "
1006 "established state\n");
1007 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
1008 msgb_free(msg);
1009 /* reestablish */
1010 if (!dl->reestablish)
1011 return 0;
1012 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1013 return lapd_reestablish(dl);
1014 }
1015 break;
1016 case LAPD_STATE_DISC_SENT:
1017 /* stop Timer T200 */
1018 lapd_stop_t200(dl);
1019 /* go to idle state */
1020 lapd_dl_flush_tx(dl);
1021 lapd_dl_flush_send(dl);
1022 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1023 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1024 msgb_free(msg);
1025 return 0;
1026 case LAPD_STATE_IDLE:
1027 /* 5.4.5 all other frame types shall be discarded */
1028 default:
1029 LOGDL(dl, LOGL_INFO, "unsolicited DM response! (discarding)\n");
1030 msgb_free(msg);
1031 return 0;
1032 }
1033 /* stop timer T200 */
1034 lapd_stop_t200(dl);
1035 /* go to idle state */
1036 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1037 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1038 msgb_free(msg);
1039 return rc;
1040}
1041
1042/* Receive a LAPD U UI message from L1 */
1043static int lapd_rx_u_ui(struct msgb *msg, struct lapd_msg_ctx *lctx)
1044{
1045 struct lapd_datalink *dl = lctx->dl;
1046 int length = lctx->length;
1047
1048 LOGDL(dl, LOGL_INFO, "UI received\n");
1049 /* G.2.2 Wrong value of the C/R bit */
1050 if (lctx->cr == dl->cr.rem2loc.resp) {
1051 LOGDL(dl, LOGL_ERROR, "UI indicates response error\n");
1052 msgb_free(msg);
1053 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1054 return -EINVAL;
1055 }
1056
1057 /* G.4.5 If UI is received with L>N201 or with M bit
1058 * set, AN MDL-ERROR-INDICATION is sent to MM.
1059 */
1060 if (length > lctx->n201 || lctx->more) {
1061 LOGDL(dl, LOGL_ERROR, "UI too large error (%d > N201(%d) or M=%d)\n",
1062 length, lctx->n201, lctx->more);
1063 msgb_free(msg);
1064 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1065 return -EIO;
1066 }
1067
1068 /* do some length checks */
1069 if (length == 0) {
1070 /* 5.3.3 UI frames received with the length indicator
1071 * set to "0" shall be ignored
1072 */
1073 LOGDL(dl, LOGL_INFO, "length=0 (discarding)\n");
1074 msgb_free(msg);
1075 return 0;
1076 }
1077 msgb_trim(msg, length);
1078 return send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx, msg);
1079}
1080
1081/* Receive a LAPD U DISC message from L1 */
1082static int lapd_rx_u_disc(struct msgb *msg, struct lapd_msg_ctx *lctx)
1083{
1084 struct lapd_datalink *dl = lctx->dl;
1085 int length = lctx->length;
1086 int rc = 0;
1087 uint8_t prim, op;
1088
1089 prim = PRIM_DL_REL;
1090 op = PRIM_OP_INDICATION;
1091
1092 LOGDL(dl, LOGL_INFO, "DISC received in state %s\n", lapd_state_name(dl->state));
1093 /* flush tx and send buffers */
1094 lapd_dl_flush_tx(dl);
1095 lapd_dl_flush_send(dl);
1096 /* 5.7.1 */
1097 dl->seq_err_cond = 0;
1098 /* G.2.2 Wrong value of the C/R bit */
1099 if (lctx->cr == dl->cr.rem2loc.resp) {
1100 LOGDL(dl, LOGL_ERROR, "DISC response error\n");
1101 msgb_free(msg);
1102 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1103 return -EINVAL;
1104 }
1105 if (length > 0 || lctx->more) {
1106 /* G.4.4 If a DISC or DM frame is received with L>0 or
1107 * with the M bit set to "1", an MDL-ERROR-INDICATION
1108 * primitive with cause "U frame with incorrect
1109 * parameters" is sent to the mobile management entity.
1110 */
1111 LOGDL(dl, LOGL_ERROR, "U frame iwth incorrect parameters\n");
1112 msgb_free(msg);
1113 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1114 return -EIO;
1115 }
1116 switch (dl->state) {
1117 case LAPD_STATE_IDLE:
1118 LOGDL(dl, LOGL_INFO, "DISC in idle state\n");
1119 /* send DM with F=P */
1120 msgb_free(msg);
1121 return lapd_send_dm(lctx);
1122 case LAPD_STATE_SABM_SENT:
1123 LOGDL(dl, LOGL_INFO, "DISC in SABM state\n");
1124 /* 5.4.6.2 send DM with F=P */
1125 lapd_send_dm(lctx);
1126 /* stop Timer T200 */
1127 lapd_stop_t200(dl);
1128 /* go to idle state */
1129 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1130 msgb_free(msg);
1131 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1132 lctx);
1133 case LAPD_STATE_MF_EST:
1134 case LAPD_STATE_TIMER_RECOV:
1135 LOGDL(dl, LOGL_INFO, "DISC in est state\n");
1136 break;
1137 case LAPD_STATE_DISC_SENT:
1138 LOGDL(dl, LOGL_INFO, "DISC in disc state\n");
1139 prim = PRIM_DL_REL;
1140 op = PRIM_OP_CONFIRM;
1141 break;
1142 default:
1143 lapd_send_ua(lctx, length, msg->l3h);
1144 msgb_free(msg);
1145 return 0;
1146 }
1147 /* send UA response */
1148 lapd_send_ua(lctx, length, msg->l3h);
1149 /* stop Timer T200 */
1150 lapd_stop_t200(dl);
1151 /* enter idle state, keep tx-buffer with UA response */
1152 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1153 /* send notification to L3 */
1154 rc = send_dl_simple(prim, op, lctx);
1155 msgb_free(msg);
1156 return rc;
1157}
1158
1159/* Receive a LAPD U UA message from L1 */
1160static int lapd_rx_u_ua(struct msgb *msg, struct lapd_msg_ctx *lctx)
1161{
1162 struct lapd_datalink *dl = lctx->dl;
1163 int length = lctx->length;
1164 int rc = 0;
1165
1166 LOGDL(dl, LOGL_INFO, "UA received in state %s\n", lapd_state_name(dl->state));
1167 /* G.2.2 Wrong value of the C/R bit */
1168 if (lctx->cr == dl->cr.rem2loc.cmd) {
1169 LOGDL(dl, LOGL_ERROR, "UA indicates command error\n");
1170 msgb_free(msg);
1171 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1172 return -EINVAL;
1173 }
1174
1175 /* G.4.5 If UA is received with L>N201 or with M bit
1176 * set, AN MDL-ERROR-INDICATION is sent to MM.
1177 */
1178 if (lctx->more || length > lctx->n201) {
1179 LOGDL(dl, LOGL_ERROR, "UA too large error\n");
1180 msgb_free(msg);
1181 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1182 return -EIO;
1183 }
1184
1185 if (!lctx->p_f) {
1186 /* 5.4.1.2 A UA response with the F bit set to "0"
1187 * shall be ignored.
1188 */
1189 LOGDL(dl, LOGL_INFO, "F=0 (discarding)\n");
1190 msgb_free(msg);
1191 return 0;
1192 }
1193 switch (dl->state) {
1194 case LAPD_STATE_SABM_SENT:
1195 break;
1196 case LAPD_STATE_MF_EST:
1197 case LAPD_STATE_TIMER_RECOV:
1198 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1199 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1200 msgb_free(msg);
1201 return 0;
1202 case LAPD_STATE_DISC_SENT:
1203 LOGDL(dl, LOGL_INFO, "UA in disconnect state\n");
1204 /* stop Timer T200 */
1205 lapd_stop_t200(dl);
1206 /* go to idle state */
1207 lapd_dl_flush_tx(dl);
1208 lapd_dl_flush_send(dl);
1209 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1210 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1211 msgb_free(msg);
1212 return 0;
1213 case LAPD_STATE_IDLE:
1214 /* 5.4.5 all other frame types shall be discarded */
1215 default:
1216 LOGDL(dl, LOGL_INFO, "unsolicited UA response! (discarding)\n");
1217 msgb_free(msg);
1218 return 0;
1219 }
1220 LOGDL(dl, LOGL_INFO, "UA in SABM state\n");
1221 /* stop Timer T200 */
1222 lapd_stop_t200(dl);
1223 /* compare UA with SABME if contention resolution is applied */
1224 if (dl->tx_hist[0].msg->len) {
1225 if (length != (dl->tx_hist[0].msg->len)
1226 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1227 length)) {
1228 LOGDL(dl, LOGL_INFO, "**** UA response mismatches ****\n");
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001229 /* go to idle state */
1230 lapd_dl_flush_tx(dl);
1231 lapd_dl_flush_send(dl);
1232 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
Harald Welted2a61172020-12-21 17:43:54 +01001233 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1234 msgb_free(msg);
Pau Espin Pedrold5f71472020-10-21 13:02:43 +02001235 return 0;
1236 }
1237 }
1238 /* set Vs, Vr and Va to 0 */
1239 dl->v_send = dl->v_recv = dl->v_ack = 0;
1240 /* clear tx_hist */
1241 lapd_dl_flush_hist(dl);
1242 /* enter multiple-frame-established state */
1243 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1244 /* send outstanding frames, if any (resume / reconnect) */
1245 lapd_send_i(lctx, __LINE__);
1246 /* send notification to L3 */
1247 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1248 msgb_free(msg);
1249 return rc;
1250}
1251
1252/* Receive a LAPD U FRMR message from L1 */
1253static int lapd_rx_u_frmr(struct msgb *msg, struct lapd_msg_ctx *lctx)
1254{
1255 struct lapd_datalink *dl = lctx->dl;
1256
1257 LOGDL(dl, LOGL_NOTICE, "Frame reject received\n");
1258 /* send MDL ERROR INIDCATION to L3 */
1259 mdl_error(MDL_CAUSE_FRMR, lctx);
1260 msgb_free(msg);
1261 /* reestablish */
1262 if (!dl->reestablish)
1263 return 0;
1264 LOGDL(dl, LOGL_NOTICE, "Performing reestablishment\n");
1265 return lapd_reestablish(dl);
1266}
1267
1268/* Receive a LAPD U (Unnumbered) message from L1 */
1269static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
1270{
1271 switch (lctx->s_u) {
1272 case LAPD_U_SABM:
1273 case LAPD_U_SABME:
1274 return lapd_rx_u_sabm(msg, lctx);
1275 case LAPD_U_DM:
1276 return lapd_rx_u_dm(msg, lctx);
1277 case LAPD_U_UI:
1278 return lapd_rx_u_ui(msg, lctx);
1279 case LAPD_U_DISC:
1280 return lapd_rx_u_disc(msg, lctx);
1281 case LAPD_U_UA:
1282 return lapd_rx_u_ua(msg, lctx);
1283 case LAPD_U_FRMR:
1284 return lapd_rx_u_frmr(msg, lctx);
1285 default:
1286 /* G.3.1 */
1287 LOGDL(lctx->dl, LOGL_NOTICE, "Unnumbered frame not allowed\n");
1288 msgb_free(msg);
1289 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1290 return -EINVAL;
1291 }
1292}
1293
rootaf48bed2011-09-26 11:23:06 +02001294/* Receive a LAPD S (Supervisory) message from L1 */
1295static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1296{
1297 struct lapd_datalink *dl = lctx->dl;
1298 int length = lctx->length;
1299
1300 if (length > 0 || lctx->more) {
1301 /* G.4.3 If a supervisory frame is received with L>0 or
1302 * with the M bit set to "1", an MDL-ERROR-INDICATION
1303 * primitive with cause "S frame with incorrect
1304 * parameters" is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001305 LOGDL(dl, LOGL_ERROR, "S frame with incorrect parameters\n");
rootaf48bed2011-09-26 11:23:06 +02001306 msgb_free(msg);
1307 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1308 return -EIO;
1309 }
1310
1311 if (lctx->cr == dl->cr.rem2loc.resp
1312 && lctx->p_f
1313 && dl->state != LAPD_STATE_TIMER_RECOV) {
1314 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001315 LOGDL(dl, LOGL_NOTICE, "S frame response with F=1 error\n");
rootaf48bed2011-09-26 11:23:06 +02001316 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1317 }
1318
1319 switch (dl->state) {
1320 case LAPD_STATE_IDLE:
1321 /* if P=1, respond DM with F=1 (5.2.2) */
1322 /* 5.4.5 all other frame types shall be discarded */
1323 if (lctx->p_f)
1324 lapd_send_dm(lctx); /* F=P */
1325 /* fall though */
1326 case LAPD_STATE_SABM_SENT:
1327 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001328 LOGDL(dl, LOGL_NOTICE, "S frame ignored in this state\n");
rootaf48bed2011-09-26 11:23:06 +02001329 msgb_free(msg);
1330 return 0;
1331 }
1332 switch (lctx->s_u) {
1333 case LAPD_S_RR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001334 LOGDL(dl, LOGL_INFO, "RR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001335 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1336 lapd_acknowledge(lctx);
1337
1338 /* 5.5.3.2 */
1339 if (lctx->cr == dl->cr.rem2loc.cmd
1340 && lctx->p_f) {
1341 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001342 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1343 "we are not busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001344 lapd_send_rr(lctx, 1, 0);
1345 /* NOTE: In case of sequence error condition,
1346 * the REJ frame has been transmitted when
1347 * entering the condition, so it has not be
1348 * done here
1349 */
1350 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001351 LOGDL(dl, LOGL_INFO, "RR frame command with polling bit set and "
1352 "we are busy, so we reply with RR frame response\n");
rootaf48bed2011-09-26 11:23:06 +02001353 lapd_send_rnr(lctx, 1, 0);
1354 }
1355 } else if (lctx->cr == dl->cr.rem2loc.resp
1356 && lctx->p_f
1357 && dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001358 LOGDL(dl, LOGL_INFO, "RR response with F==1, and we are in timer recovery "
1359 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001360 /* V(S) to the N(R) in the RR frame */
1361 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001362 /* stop Timer T200 */
1363 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001364 /* 5.5.7 Clear timer recovery condition */
1365 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1366 }
1367 /* Send message, if possible due to acknowledged data */
1368 lapd_send_i(lctx, __LINE__);
1369
1370 break;
1371 case LAPD_S_RNR:
Harald Welte00b2faf2020-05-02 19:56:36 +02001372 LOGDL(dl, LOGL_INFO, "RNR received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001373 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1374 lapd_acknowledge(lctx);
1375
1376 /* 5.5.5 */
1377 /* Set peer receiver busy condition */
1378 dl->peer_busy = 1;
1379
1380 if (lctx->p_f) {
1381 if (lctx->cr == dl->cr.rem2loc.cmd) {
1382 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001383 LOGDL(dl, LOGL_INFO, "RNR poll command and we are not busy, "
1384 "so we reply with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001385 /* Send RR with F=1 */
1386 lapd_send_rr(lctx, 1, 0);
1387 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001388 LOGDL(dl, LOGL_INFO, "RNR poll command and we are busy, so "
1389 "we reply with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001390 /* Send RNR with F=1 */
1391 lapd_send_rnr(lctx, 1, 0);
1392 }
1393 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001394 LOGDL(dl, LOGL_INFO, "RNR poll response and we in timer recovery "
1395 "state, so we leave that state\n");
rootaf48bed2011-09-26 11:23:06 +02001396 /* 5.5.7 Clear timer recovery condition */
1397 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1398 /* V(S) to the N(R) in the RNR frame */
1399 dl->v_send = lctx->n_recv;
1400 }
1401 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001402 LOGDL(dl, LOGL_INFO, "RNR not polling/final state received\n");
rootaf48bed2011-09-26 11:23:06 +02001403
1404 /* Send message, if possible due to acknowledged data */
1405 lapd_send_i(lctx, __LINE__);
1406
1407 break;
1408 case LAPD_S_REJ:
Harald Welte00b2faf2020-05-02 19:56:36 +02001409 LOGDL(dl, LOGL_INFO, "REJ received in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001410 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1411 lapd_acknowledge(lctx);
1412
1413 /* 5.5.4.1 */
1414 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1415 /* Clear an existing peer receiver busy condition */
1416 dl->peer_busy = 0;
1417 /* V(S) and V(A) to the N(R) in the REJ frame */
1418 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001419 /* stop Timer T200 */
1420 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001421 /* 5.5.3.2 */
1422 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1423 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001424 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1425 "state and not in own busy condition received, so we "
1426 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001427 lapd_send_rr(lctx, 1, 0);
1428 /* NOTE: In case of sequence error
1429 * condition, the REJ frame has been
1430 * transmitted when entering the
1431 * condition, so it has not be done
1432 * here
1433 */
1434 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001435 LOGDL(dl, LOGL_INFO, "REJ poll command not in timer recovery "
1436 "state and in own busy condition received, so we "
1437 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001438 lapd_send_rnr(lctx, 1, 0);
1439 }
1440 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001441 LOGDL(dl, LOGL_INFO, "REJ response or not polling command not "
1442 "in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001443 /* send MDL ERROR INIDCATION to L3 */
1444 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001445 LOGDL(dl, LOGL_ERROR, "unsolicited supervisory response!\n");
rootaf48bed2011-09-26 11:23:06 +02001446 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1447 }
1448
1449 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001450 LOGDL(dl, LOGL_INFO, "REJ poll response in timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001451 /* Clear an existing peer receiver busy condition */
1452 dl->peer_busy = 0;
1453 /* V(S) and V(A) to the N(R) in the REJ frame */
1454 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001455 /* stop Timer T200 */
1456 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001457 /* 5.5.7 Clear timer recovery condition */
1458 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1459 } else {
1460 /* Clear an existing peer receiver busy condition */
1461 dl->peer_busy = 0;
1462 /* V(S) and V(A) to the N(R) in the REJ frame */
1463 dl->v_send = dl->v_ack = lctx->n_recv;
1464 /* 5.5.3.2 */
1465 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1466 if (!dl->own_busy && !dl->seq_err_cond) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001467 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1468 "state and not in own busy condition received, so we "
1469 "respond with RR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001470 lapd_send_rr(lctx, 1, 0);
1471 /* NOTE: In case of sequence error
1472 * condition, the REJ frame has been
1473 * transmitted when entering the
1474 * condition, so it has not be done
1475 * here
1476 */
1477 } else if (dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001478 LOGDL(dl, LOGL_INFO, "REJ poll command in timer recovery "
1479 "state and in own busy condition received, so we "
1480 "respond with RNR final response\n");
rootaf48bed2011-09-26 11:23:06 +02001481 lapd_send_rnr(lctx, 1, 0);
1482 }
1483 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001484 LOGDL(dl, LOGL_INFO, "REJ response or not polling command in "
1485 "timer recovery state received\n");
rootaf48bed2011-09-26 11:23:06 +02001486 }
1487
1488 /* FIXME: 5.5.4.2 2) */
1489
1490 /* Send message, if possible due to acknowledged data */
1491 lapd_send_i(lctx, __LINE__);
1492
1493 break;
1494 default:
1495 /* G.3.1 */
Harald Welte00b2faf2020-05-02 19:56:36 +02001496 LOGDL(dl, LOGL_ERROR, "Supervisory frame not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001497 msgb_free(msg);
1498 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1499 return -EINVAL;
1500 }
1501 msgb_free(msg);
1502 return 0;
1503}
1504
1505/* Receive a LAPD I (Information) message from L1 */
1506static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1507{
1508 struct lapd_datalink *dl = lctx->dl;
1509 //uint8_t nr = lctx->n_recv;
1510 uint8_t ns = lctx->n_send;
1511 int length = lctx->length;
1512 int rc;
1513
Harald Welte00b2faf2020-05-02 19:56:36 +02001514 LOGDL(dl, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1515 lapd_state_name(dl->state), lctx->sapi);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001516
rootaf48bed2011-09-26 11:23:06 +02001517 /* G.2.2 Wrong value of the C/R bit */
1518 if (lctx->cr == dl->cr.rem2loc.resp) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001519 LOGDL(dl, LOGL_ERROR, "I frame response not allowed (state %s)\n",
1520 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001521 msgb_free(msg);
1522 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1523 return -EINVAL;
1524 }
1525
1526 if (length == 0 || length > lctx->n201) {
1527 /* G.4.2 If the length indicator of an I frame is set
1528 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1529 * primitive with cause "I frame with incorrect length"
1530 * is sent to the mobile management entity. */
Harald Welte00b2faf2020-05-02 19:56:36 +02001531 LOGDL(dl, LOGL_ERROR, "I frame length not allowed (state %s)\n",
1532 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001533 msgb_free(msg);
1534 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1535 return -EIO;
1536 }
1537
1538 /* G.4.2 If the numerical value of L is L<N201 and the M
1539 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1540 * cause "I frame with incorrect use of M bit" is sent to the
1541 * mobile management entity. */
1542 if (lctx->more && length < lctx->n201) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001543 LOGDL(dl, LOGL_ERROR, "I frame with M bit too short (state %s)\n",
1544 lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001545 msgb_free(msg);
1546 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1547 return -EIO;
1548 }
1549
1550 switch (dl->state) {
1551 case LAPD_STATE_IDLE:
1552 /* if P=1, respond DM with F=1 (5.2.2) */
1553 /* 5.4.5 all other frame types shall be discarded */
1554 if (lctx->p_f)
1555 lapd_send_dm(lctx); /* F=P */
1556 /* fall though */
1557 case LAPD_STATE_SABM_SENT:
1558 case LAPD_STATE_DISC_SENT:
Harald Welte00b2faf2020-05-02 19:56:36 +02001559 LOGDL(dl, LOGL_NOTICE, "I frame ignored in state %s\n", lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001560 msgb_free(msg);
1561 return 0;
1562 }
1563
1564 /* 5.7.1: N(s) sequence error */
1565 if (ns != dl->v_recv) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001566 LOGDL(dl, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, V(R)=%u (state %s)\n",
1567 ns, dl->v_recv, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001568 /* discard data */
1569 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001570 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001571 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001572 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001573 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001574 lapd_send_rej(lctx, lctx->p_f);
1575 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001576 /* If there are two subsequent sequence errors received,
1577 * ignore it. (Ignore every second subsequent error.)
1578 * This happens if our reply with the REJ is too slow,
1579 * so the remote gets a T200 timeout and sends another
1580 * frame with a sequence error.
1581 * Test showed that replying with two subsequent REJ
1582 * messages could the remote L2 process to abort.
1583 * Replying too slow shouldn't happen, but may happen
1584 * over serial link between BB and LAPD.
1585 */
1586 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001587 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001588 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1589 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1590 lapd_acknowledge(lctx); /* V(A) is also set here */
1591
1592 /* Send message, if possible due to acknowledged data */
1593 lapd_send_i(lctx, __LINE__);
1594
1595 return 0;
rootaf48bed2011-09-26 11:23:06 +02001596 }
1597 dl->seq_err_cond = 0;
1598
1599 /* Increment receiver state */
1600 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Harald Welte00b2faf2020-05-02 19:56:36 +02001601 LOGDL(dl, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
rootaf48bed2011-09-26 11:23:06 +02001602
1603 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1604 lapd_acknowledge(lctx); /* V(A) is also set here */
1605
1606 /* Only if we are not in own receiver busy condition */
1607 if (!dl->own_busy) {
1608 /* if the frame carries a complete segment */
1609 if (!lctx->more && !dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001610 LOGDL(dl, LOGL_INFO, "message in single I frame\n");
rootaf48bed2011-09-26 11:23:06 +02001611 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001612 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001613 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1614 msg);
1615 } else {
1616 /* create rcv_buffer */
1617 if (!dl->rcv_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001618 LOGDL(dl, LOGL_INFO, "message in multiple I frames (first message)\n");
rootaf48bed2011-09-26 11:23:06 +02001619 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1620 "LAPD RX");
1621 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1622 }
1623 /* concat. rcv_buffer */
1624 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001625 LOGDL(dl, LOGL_NOTICE, "Received frame overflow!\n");
rootaf48bed2011-09-26 11:23:06 +02001626 } else {
1627 memcpy(msgb_put(dl->rcv_buffer, length),
1628 msg->l3h, length);
1629 }
1630 /* if the last segment was received */
1631 if (!lctx->more) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001632 LOGDL(dl, LOGL_INFO, "message in multiple I frames (last message)\n");
rootaf48bed2011-09-26 11:23:06 +02001633 rc = send_dl_l3(PRIM_DL_DATA,
1634 PRIM_OP_INDICATION, lctx,
1635 dl->rcv_buffer);
1636 dl->rcv_buffer = NULL;
1637 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001638 LOGDL(dl, LOGL_INFO, "message in multiple I frames (next message)\n");
rootaf48bed2011-09-26 11:23:06 +02001639 msgb_free(msg);
1640
1641 }
Harald Weltebc1d7152020-07-04 10:52:13 +02001642 /* the L3 or higher (called in-line above via send_dl_l3) might have destroyed the
1643 * data link meanwhile. See OS#1761 */
1644 if (dl->state == LAPD_STATE_NULL)
1645 return 0;
rootaf48bed2011-09-26 11:23:06 +02001646 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001647 LOGDL(dl, LOGL_INFO, "I frame ignored during own receiver busy condition\n");
rootaf48bed2011-09-26 11:23:06 +02001648
1649 /* Check for P bit */
1650 if (lctx->p_f) {
1651 /* 5.5.2.1 */
1652 /* check if we are not in own receiver busy */
1653 if (!dl->own_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001654 LOGDL(dl, LOGL_INFO, "we are not busy, send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001655 /* Send RR with F=1 */
1656 rc = lapd_send_rr(lctx, 1, 0);
1657 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001658 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001659 /* Send RNR with F=1 */
1660 rc = lapd_send_rnr(lctx, 1, 0);
1661 }
1662 } else {
1663 /* 5.5.2.2 */
1664 /* check if we are not in own receiver busy */
1665 if (!dl->own_busy) {
1666 /* NOTE: V(R) is already set above */
1667 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001668
1669 /* if update_pending_iframe returns 0 it updated
1670 * the lapd header of an iframe in the tx queue */
1671 if (rc && dl->update_pending_frames)
1672 rc = dl->update_pending_frames(lctx);
1673
rootaf48bed2011-09-26 11:23:06 +02001674 if (rc) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001675 LOGDL(dl, LOGL_INFO, "we are not busy and have no pending data, "
1676 "send RR\n");
rootaf48bed2011-09-26 11:23:06 +02001677 /* Send RR with F=0 */
1678 return lapd_send_rr(lctx, 0, 0);
1679 }
1680 /* all I or one RR is sent, we are done */
1681 return 0;
1682 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001683 LOGDL(dl, LOGL_INFO, "we are busy, send RNR\n");
rootaf48bed2011-09-26 11:23:06 +02001684 /* Send RNR with F=0 */
1685 rc = lapd_send_rnr(lctx, 0, 0);
1686 }
1687 }
1688
1689 /* Send message, if possible due to acknowledged data */
1690 lapd_send_i(lctx, __LINE__);
1691
1692 return rc;
1693}
1694
1695/* Receive a LAPD message from L1 */
1696int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1697{
1698 int rc;
1699
1700 switch (lctx->format) {
1701 case LAPD_FORM_U:
1702 rc = lapd_rx_u(msg, lctx);
1703 break;
1704 case LAPD_FORM_S:
1705 rc = lapd_rx_s(msg, lctx);
1706 break;
1707 case LAPD_FORM_I:
1708 rc = lapd_rx_i(msg, lctx);
1709 break;
1710 default:
Maxc5695262022-10-09 17:41:46 +03001711 LOGDL(lctx->dl, LOGL_NOTICE, "unknown LAPD format 0x%02x\n", lctx->format);
rootaf48bed2011-09-26 11:23:06 +02001712 msgb_free(msg);
1713 rc = -EINVAL;
1714 }
1715 return rc;
1716}
1717
1718/* L3 -> L2 */
1719
1720/* send unit data */
1721static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1722{
1723 struct lapd_datalink *dl = lctx->dl;
1724 struct msgb *msg = dp->oph.msg;
1725 struct lapd_msg_ctx nctx;
1726
1727 memcpy(&nctx, lctx, sizeof(nctx));
1728 /* keep nctx.ldp */
1729 /* keep nctx.sapi */
1730 /* keep nctx.tei */
1731 nctx.cr = dl->cr.loc2rem.cmd;
1732 nctx.format = LAPD_FORM_U;
1733 nctx.s_u = LAPD_U_UI;
1734 /* keep nctx.p_f */
1735 nctx.length = msg->len;
1736 nctx.more = 0;
1737
1738 return dl->send_ph_data_req(&nctx, msg);
1739}
1740
Max2b283b12022-11-17 22:18:01 +03001741static void msg_to_tx_hist(struct lapd_history *tx_hist, const struct msgb *msg, int length, int more)
1742{
1743 tx_hist->msg = lapd_msgb_alloc(msg->len, "HIST");
1744 tx_hist->more = more;
1745 msgb_put(tx_hist->msg, msg->len);
1746 if (length)
1747 memcpy(tx_hist->msg->data, msg->l3h, msg->len);
1748}
1749
1750static void msg_to_tx_hist0(struct lapd_datalink *dl, const struct msgb *msg)
1751{
1752 return msg_to_tx_hist(&dl->tx_hist[0], msg, msg->len, 0);
1753}
1754
rootaf48bed2011-09-26 11:23:06 +02001755/* request link establishment */
1756static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1757{
1758 struct lapd_datalink *dl = lctx->dl;
1759 struct msgb *msg = dp->oph.msg;
1760 struct lapd_msg_ctx nctx;
1761
1762 if (msg->len)
Harald Welte00b2faf2020-05-02 19:56:36 +02001763 LOGDL(dl, LOGL_INFO, "perform establishment with content (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001764 else
Harald Welte00b2faf2020-05-02 19:56:36 +02001765 LOGDL(dl, LOGL_INFO, "perform normal establishm. (SABM)\n");
rootaf48bed2011-09-26 11:23:06 +02001766
1767 /* Flush send-queue */
1768 /* Clear send-buffer */
1769 lapd_dl_flush_send(dl);
1770 /* be sure that history is empty */
1771 lapd_dl_flush_hist(dl);
1772
1773 /* save message context for further use */
1774 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1775
1776 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001777 msgb_free(dl->rcv_buffer);
1778 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001779
1780 /* assemble message */
1781 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1782 /* keep nctx.ldp */
1783 /* keep nctx.sapi */
1784 /* keep nctx.tei */
1785 nctx.cr = dl->cr.loc2rem.cmd;
1786 nctx.format = LAPD_FORM_U;
1787 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1788 nctx.p_f = 1;
1789 nctx.length = msg->len;
1790 nctx.more = 0;
1791
1792 /* Transmit-buffer carries exactly one segment */
Max2b283b12022-11-17 22:18:01 +03001793 msg_to_tx_hist0(dl, msg);
1794
rootaf48bed2011-09-26 11:23:06 +02001795 /* set Vs to 0, because it is used as index when resending SABM */
1796 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001797
rootaf48bed2011-09-26 11:23:06 +02001798 /* Set states */
1799 dl->own_busy = dl->peer_busy = 0;
1800 dl->retrans_ctr = 0;
1801 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1802
1803 /* Tramsmit and start T200 */
1804 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001805 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001806
1807 return 0;
1808}
1809
1810/* send data */
1811static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1812{
1813 struct lapd_datalink *dl = lctx->dl;
1814 struct msgb *msg = dp->oph.msg;
1815
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001816 if (msgb_l3len(msg) == 0) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001817 LOGDL(dl, LOGL_ERROR, "writing an empty message is not possible\n");
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001818 msgb_free(msg);
1819 return -1;
1820 }
1821
Harald Welte00b2faf2020-05-02 19:56:36 +02001822 LOGDL(dl, LOGL_INFO, "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001823
1824 /* Write data into the send queue */
1825 msgb_enqueue(&dl->send_queue, msg);
1826
1827 /* Send message, if possible */
1828 lapd_send_i(&dl->lctx, __LINE__);
1829
1830 return 0;
1831}
1832
1833/* Send next I frame from queued/buffered data */
1834static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1835{
1836 struct lapd_datalink *dl = lctx->dl;
1837 uint8_t k = dl->k;
1838 uint8_t h;
1839 struct msgb *msg;
1840 int length, left;
1841 int rc = - 1; /* we sent nothing */
1842 struct lapd_msg_ctx nctx;
1843
1844
Harald Welte00b2faf2020-05-02 19:56:36 +02001845 LOGDL(dl, LOGL_INFO, "%s() called from line %d\n", __func__, line);
rootaf48bed2011-09-26 11:23:06 +02001846
1847 next_frame:
1848
1849 if (dl->peer_busy) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001850 LOGDL(dl, LOGL_INFO, "peer busy, not sending\n");
rootaf48bed2011-09-26 11:23:06 +02001851 return rc;
1852 }
1853
1854 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001855 LOGDL(dl, LOGL_INFO, "timer recovery, not sending\n");
rootaf48bed2011-09-26 11:23:06 +02001856 return rc;
1857 }
1858
1859 /* If the send state variable V(S) is equal to V(A) plus k
1860 * (where k is the maximum number of outstanding I frames - see
1861 * subclause 5.8.4), the data link layer entity shall not transmit any
1862 * new I frames, but shall retransmit an I frame as a result
1863 * of the error recovery procedures as described in subclauses 5.5.4 and
1864 * 5.5.7. */
1865 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001866 LOGDL(dl, LOGL_INFO, "k frames outstanding, not sending more "
1867 "(k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send, dl->v_ack);
rootaf48bed2011-09-26 11:23:06 +02001868 return rc;
1869 }
1870
1871 h = do_mod(dl->v_send, dl->range_hist);
1872
1873 /* if we have no tx_hist yet, we create it */
1874 if (!dl->tx_hist[h].msg) {
1875 /* Get next message into send-buffer, if any */
1876 if (!dl->send_buffer) {
1877 next_message:
1878 dl->send_out = 0;
1879 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1880 /* No more data to be sent */
1881 if (!dl->send_buffer)
1882 return rc;
Harald Welte00b2faf2020-05-02 19:56:36 +02001883 LOGDL(dl, LOGL_INFO, "get message from send-queue\n");
rootaf48bed2011-09-26 11:23:06 +02001884 }
1885
1886 /* How much is left in the send-buffer? */
1887 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1888 /* Segment, if data exceeds N201 */
1889 length = left;
1890 if (length > lctx->n201)
1891 length = lctx->n201;
Harald Welte00b2faf2020-05-02 19:56:36 +02001892 LOGDL(dl, LOGL_INFO, "msg-len %d sent %d left %d N201 %d length %d "
1893 "first byte %02x\n", msgb_l3len(dl->send_buffer), dl->send_out, left,
1894 lctx->n201, length, dl->send_buffer->l3h[0]);
rootaf48bed2011-09-26 11:23:06 +02001895 /* If message in send-buffer is completely sent */
1896 if (left == 0) {
1897 msgb_free(dl->send_buffer);
1898 dl->send_buffer = NULL;
1899 goto next_message;
1900 }
1901
Harald Welte00b2faf2020-05-02 19:56:36 +02001902 LOGDL(dl, LOGL_INFO, "send I frame %sV(S)=%d\n",
1903 (left > length) ? "segment " : "", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02001904
1905 /* Create I frame (segment) and transmit-buffer content */
1906 msg = lapd_msgb_alloc(length, "LAPD I");
1907 msg->l3h = msgb_put(msg, length);
1908 /* assemble message */
1909 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1910 /* keep nctx.ldp */
1911 /* keep nctx.sapi */
1912 /* keep nctx.tei */
1913 nctx.cr = dl->cr.loc2rem.cmd;
1914 nctx.format = LAPD_FORM_I;
1915 nctx.p_f = 0;
1916 nctx.n_send = dl->v_send;
1917 nctx.n_recv = dl->v_recv;
1918 nctx.length = length;
1919 if (left > length)
1920 nctx.more = 1;
1921 else
1922 nctx.more = 0;
1923 if (length)
1924 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1925 length);
1926 /* store in tx_hist */
Max2b283b12022-11-17 22:18:01 +03001927 msg_to_tx_hist(&dl->tx_hist[h], msg, length, nctx.more);
1928
rootaf48bed2011-09-26 11:23:06 +02001929 /* Add length to track how much is already in the tx buffer */
1930 dl->send_out += length;
1931 } else {
Harald Welte00b2faf2020-05-02 19:56:36 +02001932 LOGDL(dl, LOGL_INFO, "resend I frame from tx buffer V(S)=%d\n", dl->v_send);
rootaf48bed2011-09-26 11:23:06 +02001933
1934 /* Create I frame (segment) from tx_hist */
1935 length = dl->tx_hist[h].msg->len;
1936 msg = lapd_msgb_alloc(length, "LAPD I resend");
1937 msg->l3h = msgb_put(msg, length);
1938 /* assemble message */
1939 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1940 /* keep nctx.ldp */
1941 /* keep nctx.sapi */
1942 /* keep nctx.tei */
1943 nctx.cr = dl->cr.loc2rem.cmd;
1944 nctx.format = LAPD_FORM_I;
1945 nctx.p_f = 0;
1946 nctx.n_send = dl->v_send;
1947 nctx.n_recv = dl->v_recv;
1948 nctx.length = length;
1949 nctx.more = dl->tx_hist[h].more;
1950 if (length)
1951 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1952 }
1953
1954 /* The value of the send state variable V(S) shall be incremented by 1
1955 * at the end of the transmission of the I frame */
1956 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1957
1958 /* If timer T200 is not running at the time right before transmitting a
1959 * frame, when the PH-READY-TO-SEND primitive is received from the
1960 * physical layer., it shall be set. */
1961 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001962 /* stop Timer T203, if running */
1963 lapd_stop_t203(dl);
1964 /* start Timer T200 */
1965 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001966 }
1967
1968 dl->send_ph_data_req(&nctx, msg);
1969
1970 rc = 0; /* we sent something */
1971 goto next_frame;
1972}
1973
1974/* request link suspension */
1975static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1976{
1977 struct lapd_datalink *dl = lctx->dl;
1978 struct msgb *msg = dp->oph.msg;
1979
Harald Welte00b2faf2020-05-02 19:56:36 +02001980 LOGDL(dl, LOGL_INFO, "perform suspension\n");
rootaf48bed2011-09-26 11:23:06 +02001981
1982 /* put back the send-buffer to the send-queue (first position) */
1983 if (dl->send_buffer) {
Harald Welte00b2faf2020-05-02 19:56:36 +02001984 LOGDL(dl, LOGL_INFO, "put frame in sendbuffer back to queue\n");
rootaf48bed2011-09-26 11:23:06 +02001985 llist_add(&dl->send_buffer->list, &dl->send_queue);
1986 dl->send_buffer = NULL;
1987 } else
Harald Welte00b2faf2020-05-02 19:56:36 +02001988 LOGDL(dl, LOGL_INFO, "no frame in sendbuffer\n");
rootaf48bed2011-09-26 11:23:06 +02001989
1990 /* Clear transmit buffer, but keep send buffer */
1991 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001992 /* Stop timers (there is no state change, so we must stop all timers */
1993 lapd_stop_t200(dl);
1994 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001995
1996 msgb_free(msg);
1997
1998 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1999}
2000
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002001/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002002static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2003{
2004 struct lapd_datalink *dl = lctx->dl;
2005 struct msgb *msg = dp->oph.msg;
2006 struct lapd_msg_ctx nctx;
2007
Harald Welte00b2faf2020-05-02 19:56:36 +02002008 LOGDL(dl, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", msg->len);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002009
rootaf48bed2011-09-26 11:23:06 +02002010 /* be sure that history is empty */
2011 lapd_dl_flush_hist(dl);
2012
2013 /* save message context for further use */
2014 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2015
2016 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002017 msgb_free(dl->send_buffer);
2018 dl->send_buffer = NULL;
2019
rootaf48bed2011-09-26 11:23:06 +02002020 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002021 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002022 /* Write data into the send buffer, to be sent first */
2023 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002024 } else {
2025 msgb_free(msg);
2026 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002027 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002028 }
rootaf48bed2011-09-26 11:23:06 +02002029
2030 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002031 msgb_free(dl->rcv_buffer);
2032 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002033
2034 /* Create new msgb (old one is now free) */
2035 msg = lapd_msgb_alloc(0, "LAPD SABM");
2036 msg->l3h = msg->data;
2037 /* assemble message */
2038 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2039 /* keep nctx.ldp */
2040 /* keep nctx.sapi */
2041 /* keep nctx.tei */
2042 nctx.cr = dl->cr.loc2rem.cmd;
2043 nctx.format = LAPD_FORM_U;
2044 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2045 nctx.p_f = 1;
2046 nctx.length = 0;
2047 nctx.more = 0;
2048
Max2b283b12022-11-17 22:18:01 +03002049 msg_to_tx_hist0(dl, msg);
2050
rootaf48bed2011-09-26 11:23:06 +02002051 /* set Vs to 0, because it is used as index when resending SABM */
2052 dl->v_send = 0;
2053
2054 /* Set states */
2055 dl->own_busy = dl->peer_busy = 0;
2056 dl->retrans_ctr = 0;
2057 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2058
2059 /* Tramsmit and start T200 */
2060 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002061 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002062
2063 return 0;
2064}
2065
2066/* requesst release of link */
2067static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2068{
2069 struct lapd_datalink *dl = lctx->dl;
2070 struct msgb *msg = dp->oph.msg;
2071 struct lapd_msg_ctx nctx;
2072
2073 /* local release */
2074 if (dp->u.rel_req.mode) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002075 LOGDL(dl, LOGL_INFO, "perform local release\n");
rootaf48bed2011-09-26 11:23:06 +02002076 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002077 /* stop Timer T200 */
2078 lapd_stop_t200(dl);
2079 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002080 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2081 /* flush buffers */
2082 lapd_dl_flush_tx(dl);
2083 lapd_dl_flush_send(dl);
2084 /* send notification to L3 */
2085 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2086 }
2087
2088 /* in case we are already disconnecting */
2089 if (dl->state == LAPD_STATE_DISC_SENT)
2090 return -EBUSY;
2091
2092 /* flush tx_hist */
2093 lapd_dl_flush_hist(dl);
2094
Harald Welte00b2faf2020-05-02 19:56:36 +02002095 LOGDL(dl, LOGL_INFO, "perform normal release (DISC)\n");
rootaf48bed2011-09-26 11:23:06 +02002096
2097 /* Push LAPD header on msgb */
2098 /* assemble message */
2099 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2100 /* keep nctx.ldp */
2101 /* keep nctx.sapi */
2102 /* keep nctx.tei */
2103 nctx.cr = dl->cr.loc2rem.cmd;
2104 nctx.format = LAPD_FORM_U;
2105 nctx.s_u = LAPD_U_DISC;
2106 nctx.p_f = 1;
2107 nctx.length = 0;
2108 nctx.more = 0;
2109
Max2b283b12022-11-17 22:18:01 +03002110 msg_to_tx_hist0(dl, msg);
2111
rootaf48bed2011-09-26 11:23:06 +02002112 /* set Vs to 0, because it is used as index when resending DISC */
2113 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002114
rootaf48bed2011-09-26 11:23:06 +02002115 /* Set states */
2116 dl->own_busy = dl->peer_busy = 0;
2117 dl->retrans_ctr = 0;
2118 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2119
2120 /* Tramsmit and start T200 */
2121 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002122 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002123
2124 return 0;
2125}
2126
2127/* request release of link in idle state */
2128static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2129 struct lapd_msg_ctx *lctx)
2130{
2131 struct lapd_datalink *dl = lctx->dl;
2132 struct msgb *msg = dp->oph.msg;
2133
2134 msgb_free(msg);
2135
2136 /* send notification to L3 */
2137 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2138}
2139
2140/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002141static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002142 uint32_t states;
2143 int prim, op;
2144 const char *name;
2145 int (*rout) (struct osmo_dlsap_prim *dp,
2146 struct lapd_msg_ctx *lctx);
2147} l2downstatelist[] = {
2148 /* create and send UI command */
2149 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002150 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002151 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2152
2153 /* create and send SABM command */
2154 {SBIT(LAPD_STATE_IDLE),
2155 PRIM_DL_EST, PRIM_OP_REQUEST,
2156 "DL-ESTABLISH-REQUEST", lapd_est_req},
2157
2158 /* create and send I command */
2159 {SBIT(LAPD_STATE_MF_EST) |
2160 SBIT(LAPD_STATE_TIMER_RECOV),
2161 PRIM_DL_DATA, PRIM_OP_REQUEST,
2162 "DL-DATA-REQUEST", lapd_data_req},
2163
2164 /* suspend datalink */
2165 {SBIT(LAPD_STATE_MF_EST) |
2166 SBIT(LAPD_STATE_TIMER_RECOV),
2167 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2168 "DL-SUSPEND-REQUEST", lapd_susp_req},
2169
2170 /* create and send SABM command (resume) */
2171 {SBIT(LAPD_STATE_MF_EST) |
2172 SBIT(LAPD_STATE_TIMER_RECOV),
2173 PRIM_DL_RES, PRIM_OP_REQUEST,
2174 "DL-RESUME-REQUEST", lapd_res_req},
2175
2176 /* create and send SABM command (reconnect) */
2177 {SBIT(LAPD_STATE_IDLE) |
2178 SBIT(LAPD_STATE_MF_EST) |
2179 SBIT(LAPD_STATE_TIMER_RECOV),
2180 PRIM_DL_RECON, PRIM_OP_REQUEST,
2181 "DL-RECONNECT-REQUEST", lapd_res_req},
2182
2183 /* create and send DISC command */
2184 {SBIT(LAPD_STATE_SABM_SENT) |
2185 SBIT(LAPD_STATE_MF_EST) |
2186 SBIT(LAPD_STATE_TIMER_RECOV) |
2187 SBIT(LAPD_STATE_DISC_SENT),
2188 PRIM_DL_REL, PRIM_OP_REQUEST,
2189 "DL-RELEASE-REQUEST", lapd_rel_req},
2190
2191 /* release in idle state */
2192 {SBIT(LAPD_STATE_IDLE),
2193 PRIM_DL_REL, PRIM_OP_REQUEST,
2194 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2195};
2196
2197#define L2DOWNSLLEN \
2198 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2199
2200int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2201{
2202 struct lapd_datalink *dl = lctx->dl;
2203 int i, supported = 0;
2204 struct msgb *msg = dp->oph.msg;
2205 int rc;
2206
2207 /* find function for current state and message */
2208 for (i = 0; i < L2DOWNSLLEN; i++) {
2209 if (dp->oph.primitive == l2downstatelist[i].prim
2210 && dp->oph.operation == l2downstatelist[i].op) {
2211 supported = 1;
2212 if ((SBIT(dl->state) & l2downstatelist[i].states))
2213 break;
2214 }
2215 }
2216 if (!supported) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002217 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unsupported\n",
2218 dp->oph.primitive, dp->oph.operation);
rootaf48bed2011-09-26 11:23:06 +02002219 msgb_free(msg);
2220 return 0;
2221 }
2222 if (i == L2DOWNSLLEN) {
Harald Welte00b2faf2020-05-02 19:56:36 +02002223 LOGDL(dl, LOGL_NOTICE, "Message %u/%u unhandled at this state %s\n",
2224 dp->oph.primitive, dp->oph.operation, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002225 msgb_free(msg);
2226 return 0;
2227 }
2228
Harald Welte00b2faf2020-05-02 19:56:36 +02002229 LOGDL(dl, LOGL_INFO, "Message %s received in state %s\n",
2230 l2downstatelist[i].name, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02002231
2232 rc = l2downstatelist[i].rout(dp, lctx);
2233
2234 return rc;
2235}
2236
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002237/*! @} */