blob: a0f3c2b41f32d678153523b199b0d7a4151fc1d5 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file lapd_core.c
2 * LAPD core implementation */
3/*
4 * (C) 2010-2011 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 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
27/*! \addtogroup lapd
28 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 *
30 * Osmocom LAPD core, used for Q.921, LAPDm and others.
31 *
rootaf48bed2011-09-26 11:23:06 +020032 * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
33 *
34 * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
35 * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
36 * received while there is an incomplete rcv_buffer, it is appended to it.
37 *
38 * TX data is stored in the send_queue first. When transmitting a frame,
39 * the first message in the send_queue is moved to the send_buffer. There it
40 * resides until all fragments are acknowledged. Fragments to be sent by I
41 * frames are stored in the tx_hist buffer for resend, if required. Also the
42 * current fragment is copied into the tx_queue. There it resides until it is
43 * forwarded to layer 1.
44 *
45 * In case we have SAPI 0, we only have a window size of 1, so the unack-
46 * nowledged message resides always in the send_buffer. In case of a suspend,
47 * it can be written back to the first position of the send_queue.
48 *
49 * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
50 * asynchronous transfer between layer 1 and layer 2 (serial link), we must
51 * send a frame before layer 1 reaches the right timeslot to send it. So we
52 * move the tx_queue to layer 1 when there is not already a pending frame, and
53 * wait until acknowledge after the frame has been sent. If we receive an
54 * acknowledge, we can send the next frame from the buffer, if any.
55 *
56 * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
57 * will trigger next I frame, if possible.
58 *
59 * T203 is optional. It will be stated when entering MF EST state. It will also
60 * be started when I or S frame is received in that state . It will be
61 * restarted in the lapd_acknowledge() function, in case outstanding frames
62 * will not trigger T200. It will be stoped, when T200 is started in MF EST
63 * state. It will also be stoped when leaving MF EST state.
64 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020065 * \file lapd_core.c
rootaf48bed2011-09-26 11:23:06 +020066 */
67
68/* Enable this to test content resolution on network side:
69 * - The first SABM is received, UA is dropped.
70 * - The phone repeats SABM, but it's content is wrong, so it is ignored
71 * - The phone repeats SABM again, content is right, so UA is sent.
72 */
73//#define TEST_CONTENT_RESOLUTION_NETWORK
74
75#include <stdio.h>
76#include <stdint.h>
77#include <string.h>
78#include <errno.h>
rootaf48bed2011-09-26 11:23:06 +020079
80#include <osmocom/core/logging.h>
81#include <osmocom/core/timer.h>
82#include <osmocom/core/msgb.h>
83#include <osmocom/core/utils.h>
84#include <osmocom/core/talloc.h>
85#include <osmocom/gsm/lapd_core.h>
Max2f0b0c92017-01-12 16:47:13 +010086#include <osmocom/gsm/rsl.h>
rootaf48bed2011-09-26 11:23:06 +020087
88/* TS 04.06 Table 4 / Section 3.8.1 */
89#define LAPD_U_SABM 0x7
90#define LAPD_U_SABME 0xf
91#define LAPD_U_DM 0x3
92#define LAPD_U_UI 0x0
93#define LAPD_U_DISC 0x8
94#define LAPD_U_UA 0xC
95#define LAPD_U_FRMR 0x11
96
97#define LAPD_S_RR 0x0
98#define LAPD_S_RNR 0x1
99#define LAPD_S_REJ 0x2
100
101#define CR_USER2NET_CMD 0
102#define CR_USER2NET_RESP 1
103#define CR_NET2USER_CMD 1
104#define CR_NET2USER_RESP 0
105
106#define LAPD_HEADROOM 56
107
108#define SBIT(a) (1 << a)
109#define ALL_STATES 0xffffffff
110
Andreas Eversberg742fc792011-09-27 09:40:25 +0200111static void lapd_t200_cb(void *data);
112static void lapd_t203_cb(void *data);
113static int lapd_send_i(struct lapd_msg_ctx *lctx, int line);
114static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
115
rootaf48bed2011-09-26 11:23:06 +0200116/* UTILITY FUNCTIONS */
117
118struct msgb *lapd_msgb_alloc(int length, const char *name)
119{
120 /* adding space for padding, FIXME: add as an option */
121 if (length < 21)
122 length = 21;
123 return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name);
124}
125
126static inline uint8_t do_mod(uint8_t x, uint8_t m)
127{
128 return x & (m - 1);
129}
130
131static inline uint8_t inc_mod(uint8_t x, uint8_t m)
132{
133 return (x + 1) & (m - 1);
134}
135
136static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m)
137{
138 return (x + y) & (m - 1);
139}
140
141static inline uint8_t sub_mod(uint8_t x, uint8_t y, uint8_t m)
142{
143 return (x - y) & (m - 1); /* handle negative results correctly */
144}
145
146static void lapd_dl_flush_send(struct lapd_datalink *dl)
147{
148 struct msgb *msg;
149
150 /* Flush send-queue */
151 while ((msg = msgb_dequeue(&dl->send_queue)))
152 msgb_free(msg);
153
154 /* Clear send-buffer */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200155 msgb_free(dl->send_buffer);
156 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +0200157}
158
159static void lapd_dl_flush_hist(struct lapd_datalink *dl)
160{
161 unsigned int i;
162
Harald Weltef92e44c2016-08-01 00:24:19 +0200163 if (!dl->range_hist || !dl->tx_hist)
Harald Welte0ee90f82016-07-03 20:45:21 +0200164 return;
165
rootaf48bed2011-09-26 11:23:06 +0200166 for (i = 0; i < dl->range_hist; i++) {
167 if (dl->tx_hist[i].msg) {
168 msgb_free(dl->tx_hist[i].msg);
169 dl->tx_hist[i].msg = NULL;
170 }
171 }
172}
173
174static void lapd_dl_flush_tx(struct lapd_datalink *dl)
175{
176 struct msgb *msg;
177
178 while ((msg = msgb_dequeue(&dl->tx_queue)))
179 msgb_free(msg);
180 lapd_dl_flush_hist(dl);
181}
182
183/* Figure B.2/Q.921 */
Harald Weltec733d142017-03-15 10:20:51 +0100184const struct value_string lapd_state_names[] = {
185 OSMO_VALUE_STRING(LAPD_STATE_NULL),
186 OSMO_VALUE_STRING(LAPD_STATE_TEI_UNASS),
187 OSMO_VALUE_STRING(LAPD_STATE_ASS_TEI_WAIT),
188 OSMO_VALUE_STRING(LAPD_STATE_EST_TEI_WAIT),
189 OSMO_VALUE_STRING(LAPD_STATE_IDLE),
190 OSMO_VALUE_STRING(LAPD_STATE_SABM_SENT),
191 OSMO_VALUE_STRING(LAPD_STATE_DISC_SENT),
192 OSMO_VALUE_STRING(LAPD_STATE_MF_EST),
193 OSMO_VALUE_STRING(LAPD_STATE_TIMER_RECOV),
194 { 0, NULL }
rootaf48bed2011-09-26 11:23:06 +0200195};
196
Harald Weltec733d142017-03-15 10:20:51 +0100197static inline const char *lapd_state_name(enum lapd_state state)
198{
199 return get_value_string(lapd_state_names, state);
200}
201
Andreas Eversberg742fc792011-09-27 09:40:25 +0200202static void lapd_start_t200(struct lapd_datalink *dl)
203{
204 if (osmo_timer_pending(&dl->t200))
205 return;
Pau Espin Pedrol28a198f2019-06-21 15:30:50 +0200206 LOGP(DLLAPD, LOGL_INFO, "start T200 (dl=%p, timeout=%d.%06ds)\n",
207 dl, dl->t200_sec, dl->t200_usec);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200208 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
209}
210
211static void lapd_start_t203(struct lapd_datalink *dl)
212{
213 if (osmo_timer_pending(&dl->t203))
214 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100215 LOGP(DLLAPD, LOGL_INFO, "start T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200216 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
217}
218
219static void lapd_stop_t200(struct lapd_datalink *dl)
220{
221 if (!osmo_timer_pending(&dl->t200))
222 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100223 LOGP(DLLAPD, LOGL_INFO, "stop T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200224 osmo_timer_del(&dl->t200);
225}
226
227static void lapd_stop_t203(struct lapd_datalink *dl)
228{
229 if (!osmo_timer_pending(&dl->t203))
230 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100231 LOGP(DLLAPD, LOGL_INFO, "stop T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200232 osmo_timer_del(&dl->t203);
233}
234
rootaf48bed2011-09-26 11:23:06 +0200235static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
236{
Philipp Maier08177d32016-12-08 17:23:26 +0100237 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100238 lapd_state_name(dl->state), lapd_state_name(state), dl);
rootaf48bed2011-09-26 11:23:06 +0200239
240 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
241 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200242 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200243 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200244 msgb_free(dl->cont_res);
245 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200246 }
247
248 /* start T203 on entering MF EST state, if enabled */
249 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200250 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
251 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200252
253 dl->state = state;
254}
255
rootaf48bed2011-09-26 11:23:06 +0200256static void *tall_lapd_ctx = NULL;
257
258/* init datalink instance and allocate history */
259void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
260 int maxf)
261{
262 int m;
263
264 memset(dl, 0, sizeof(*dl));
265 INIT_LLIST_HEAD(&dl->send_queue);
266 INIT_LLIST_HEAD(&dl->tx_queue);
267 dl->reestablish = 1;
268 dl->n200_est_rel = 3;
269 dl->n200 = 3;
270 dl->t200_sec = 1;
271 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200272 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200273 dl->t203_sec = 10;
274 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200275 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200276 dl->maxf = maxf;
277 if (k > v_range - 1)
278 k = v_range - 1;
279 dl->k = k;
280 dl->v_range = v_range;
281
282 /* Calculate modulo for history array:
283 * - The history range must be at least k+1.
284 * - The history range must be 2^x, where x is as low as possible.
285 */
286 k++;
287 for (m = 0x80; m; m >>= 1) {
288 if ((m & k)) {
289 if (k > m)
290 m <<= 1;
291 dl->range_hist = m;
292 break;
293 }
294 }
295
296 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
Philipp Maier08177d32016-12-08 17:23:26 +0100297 "history range = %d (dl=%p)\n", dl->v_range, dl->k,
298 dl->range_hist, dl);
rootaf48bed2011-09-26 11:23:06 +0200299
300 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
301
302 if (!tall_lapd_ctx)
303 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100304 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
305 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200306}
307
308/* reset to IDLE state */
309void lapd_dl_reset(struct lapd_datalink *dl)
310{
Jean-Francois Dionne893979c2017-03-02 10:45:53 -0500311 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
312 /* enter idle state (and remove eventual cont_res) */
313 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200314 /* flush buffer */
315 lapd_dl_flush_tx(dl);
316 lapd_dl_flush_send(dl);
317 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200318 msgb_free(dl->rcv_buffer);
319 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200320 /* stop Timers */
321 lapd_stop_t200(dl);
322 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500323 if (dl->state == LAPD_STATE_IDLE)
324 return;
325 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance (dl=%p)\n", dl);
326 /* enter idle state (and remove eventual cont_res) */
327 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200328}
329
330/* reset and de-allocate history buffer */
331void lapd_dl_exit(struct lapd_datalink *dl)
332{
333 /* free all ressources except history buffer */
334 lapd_dl_reset(dl);
Ivan Kluchnikovb9759db2017-05-11 15:19:23 +0300335
336 /* enter null state */
337 lapd_dl_newstate(dl, LAPD_STATE_NULL);
338
rootaf48bed2011-09-26 11:23:06 +0200339 /* free history buffer list */
340 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200341 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200342}
343
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200344/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200345int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
346{
347 switch (mode) {
348 case LAPD_MODE_USER:
349 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
350 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
351 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
352 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
353 break;
354 case LAPD_MODE_NETWORK:
355 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
356 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
357 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
358 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
359 break;
360 default:
361 return -EINVAL;
362 }
363 dl->mode = mode;
364
365 return 0;
366}
367
368/* send DL message with optional msgb */
369static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
370 struct msgb *msg)
371{
372 struct lapd_datalink *dl = lctx->dl;
373 struct osmo_dlsap_prim dp;
374
375 osmo_prim_init(&dp.oph, 0, prim, op, msg);
376 return dl->send_dlsap(&dp, lctx);
377}
378
379/* send simple DL message */
380static inline int send_dl_simple(uint8_t prim, uint8_t op,
381 struct lapd_msg_ctx *lctx)
382{
Pau Espin Pedrol9dd3bf02016-02-29 08:49:22 -0500383 return send_dl_l3(prim, op, lctx, NULL);
rootaf48bed2011-09-26 11:23:06 +0200384}
385
386/* send MDL-ERROR INDICATION */
387static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
388{
389 struct lapd_datalink *dl = lctx->dl;
390 struct osmo_dlsap_prim dp;
391
Philipp Maier08177d32016-12-08 17:23:26 +0100392 LOGP(DLLAPD, LOGL_NOTICE,
393 "sending MDL-ERROR-IND cause %d from state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100394 cause, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200395 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
396 dp.u.error_ind.cause = cause;
397 return dl->send_dlsap(&dp, lctx);
398}
399
400/* send UA response */
401static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
402{
403 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
404 struct lapd_msg_ctx nctx;
405 struct lapd_datalink *dl = lctx->dl;
406
407 memcpy(&nctx, lctx, sizeof(nctx));
408 msg->l3h = msgb_put(msg, len);
409 if (len)
410 memcpy(msg->l3h, data, len);
411 /* keep nctx.ldp */
412 /* keep nctx.sapi */
413 /* keep nctx.tei */
414 nctx.cr = dl->cr.loc2rem.resp;
415 nctx.format = LAPD_FORM_U;
416 nctx.s_u = LAPD_U_UA;
417 /* keep nctx.p_f */
418 nctx.length = len;
419 nctx.more = 0;
420
421 return dl->send_ph_data_req(&nctx, msg);
422}
423
424/* send DM response */
425static int lapd_send_dm(struct lapd_msg_ctx *lctx)
426{
427 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
428 struct lapd_msg_ctx nctx;
429 struct lapd_datalink *dl = lctx->dl;
430
431 memcpy(&nctx, lctx, sizeof(nctx));
432 /* keep nctx.ldp */
433 /* keep nctx.sapi */
434 /* keep nctx.tei */
435 nctx.cr = dl->cr.loc2rem.resp;
436 nctx.format = LAPD_FORM_U;
437 nctx.s_u = LAPD_U_DM;
438 /* keep nctx.p_f */
439 nctx.length = 0;
440 nctx.more = 0;
441
442 return dl->send_ph_data_req(&nctx, msg);
443}
444
445/* send RR response / command */
446static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
447{
448 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
449 struct lapd_msg_ctx nctx;
450 struct lapd_datalink *dl = lctx->dl;
451
452 memcpy(&nctx, lctx, sizeof(nctx));
453 /* keep nctx.ldp */
454 /* keep nctx.sapi */
455 /* keep nctx.tei */
456 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
457 nctx.format = LAPD_FORM_S;
458 nctx.s_u = LAPD_S_RR;
459 nctx.p_f = f_bit;
460 nctx.n_recv = dl->v_recv;
461 nctx.length = 0;
462 nctx.more = 0;
463
464 return dl->send_ph_data_req(&nctx, msg);
465}
466
467/* send RNR response / command */
468static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
469{
470 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
471 struct lapd_msg_ctx nctx;
472 struct lapd_datalink *dl = lctx->dl;
473
474 memcpy(&nctx, lctx, sizeof(nctx));
475 /* keep nctx.ldp */
476 /* keep nctx.sapi */
477 /* keep nctx.tei */
478 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
479 nctx.format = LAPD_FORM_S;
480 nctx.s_u = LAPD_S_RNR;
481 nctx.p_f = f_bit;
482 nctx.n_recv = dl->v_recv;
483 nctx.length = 0;
484 nctx.more = 0;
485
486 return dl->send_ph_data_req(&nctx, msg);
487}
488
489/* send REJ response */
490static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
491{
492 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
493 struct lapd_msg_ctx nctx;
494 struct lapd_datalink *dl = lctx->dl;
495
496 memcpy(&nctx, lctx, sizeof(nctx));
497 /* keep nctx.ldp */
498 /* keep nctx.sapi */
499 /* keep nctx.tei */
500 nctx.cr = dl->cr.loc2rem.resp;
501 nctx.format = LAPD_FORM_S;
502 nctx.s_u = LAPD_S_REJ;
503 nctx.p_f = f_bit;
504 nctx.n_recv = dl->v_recv;
505 nctx.length = 0;
506 nctx.more = 0;
507
508 return dl->send_ph_data_req(&nctx, msg);
509}
510
511/* resend SABM or DISC message */
512static int lapd_send_resend(struct lapd_datalink *dl)
513{
514 struct msgb *msg;
515 uint8_t h = do_mod(dl->v_send, dl->range_hist);
516 int length = dl->tx_hist[h].msg->len;
517 struct lapd_msg_ctx nctx;
518
519 /* assemble message */
520 memcpy(&nctx, &dl->lctx, sizeof(nctx));
521 /* keep nctx.ldp */
522 /* keep nctx.sapi */
523 /* keep nctx.tei */
524 nctx.cr = dl->cr.loc2rem.cmd;
525 nctx.format = LAPD_FORM_U;
526 if (dl->state == LAPD_STATE_SABM_SENT)
527 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
528 else
529 nctx.s_u = LAPD_U_DISC;
530 nctx.p_f = 1;
531 nctx.length = length;
532 nctx.more = 0;
533
534 /* Resend SABM/DISC from tx_hist */
535 msg = lapd_msgb_alloc(length, "LAPD resend");
536 msg->l3h = msgb_put(msg, length);
537 if (length)
538 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
539
540 return dl->send_ph_data_req(&nctx, msg);
541}
542
543/* reestablish link */
544static int lapd_reestablish(struct lapd_datalink *dl)
545{
546 struct osmo_dlsap_prim dp;
547 struct msgb *msg;
548
Philipp Maier08177d32016-12-08 17:23:26 +0100549 LOGP(DLLAPD, LOGL_DEBUG, "lapd reestablish (dl=%p)\n", dl);
550
rootaf48bed2011-09-26 11:23:06 +0200551 msg = lapd_msgb_alloc(0, "DUMMY");
552 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100553
rootaf48bed2011-09-26 11:23:06 +0200554 return lapd_est_req(&dp, &dl->lctx);
555}
556
557/* Timer callback on T200 expiry */
558static void lapd_t200_cb(void *data)
559{
560 struct lapd_datalink *dl = data;
561
Philipp Maier08177d32016-12-08 17:23:26 +0100562 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100563 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200564
565 switch (dl->state) {
566 case LAPD_STATE_SABM_SENT:
567 /* 5.4.1.3 */
Harald Welte7a569522019-06-02 22:37:21 +0200568 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200569 /* flush tx and send buffers */
570 lapd_dl_flush_tx(dl);
571 lapd_dl_flush_send(dl);
572 /* go back to idle state */
573 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
574 /* NOTE: we must not change any other states or buffers
575 * and queues, since we may reconnect after handover
576 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100577 /* send MDL ERROR INIDCATION to L3 */
578 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100579 /* send RELEASE INDICATION to L3 */
580 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
581 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200582 break;
583 }
584 /* retransmit SABM command */
585 lapd_send_resend(dl);
586 /* increment re-transmission counter */
587 dl->retrans_ctr++;
588 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200589 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200590 break;
591 case LAPD_STATE_DISC_SENT:
592 /* 5.4.4.3 */
Harald Welte7a569522019-06-02 22:37:21 +0200593 if (dl->retrans_ctr >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200594 /* send MDL ERROR INIDCATION to L3 */
595 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maier6b986c22017-02-01 12:00:45 +0100596 /* send RELEASE INDICATION to L3 */
597 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200598 /* flush tx and send buffers */
599 lapd_dl_flush_tx(dl);
600 lapd_dl_flush_send(dl);
601 /* go back to idle state */
602 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
603 /* NOTE: we must not change any other states or buffers
604 * and queues, since we may reconnect after handover
605 * failure. the buffered messages is replaced there */
606 break;
607 }
608 /* retransmit DISC command */
609 lapd_send_resend(dl);
610 /* increment re-transmission counter */
611 dl->retrans_ctr++;
612 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200613 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200614 break;
615 case LAPD_STATE_MF_EST:
616 /* 5.5.7 */
617 dl->retrans_ctr = 0;
618 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
619 /* fall through */
620 case LAPD_STATE_TIMER_RECOV:
621 dl->retrans_ctr++;
Harald Welte7a569522019-06-02 22:37:21 +0200622 if (dl->retrans_ctr <= dl->n200) {
rootaf48bed2011-09-26 11:23:06 +0200623 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
624 uint8_t h = do_mod(vs, dl->range_hist);
625 /* retransmit I frame (V_s-1) with P=1, if any */
626 if (dl->tx_hist[h].msg) {
627 struct msgb *msg;
628 int length = dl->tx_hist[h].msg->len;
629 struct lapd_msg_ctx nctx;
630
631 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
Philipp Maier08177d32016-12-08 17:23:26 +0100632 " V(S)=%d (dl=%p)\n", vs, dl);
rootaf48bed2011-09-26 11:23:06 +0200633 /* Create I frame (segment) from tx_hist */
634 memcpy(&nctx, &dl->lctx, sizeof(nctx));
635 /* keep nctx.ldp */
636 /* keep nctx.sapi */
637 /* keep nctx.tei */
638 nctx.cr = dl->cr.loc2rem.cmd;
639 nctx.format = LAPD_FORM_I;
640 nctx.p_f = 1;
641 nctx.n_send = vs;
642 nctx.n_recv = dl->v_recv;
643 nctx.length = length;
644 nctx.more = dl->tx_hist[h].more;
645 msg = lapd_msgb_alloc(length, "LAPD I resend");
646 msg->l3h = msgb_put(msg, length);
647 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
648 length);
649 dl->send_ph_data_req(&nctx, msg);
650 } else {
651 /* OR send appropriate supervision frame with P=1 */
652 if (!dl->own_busy && !dl->seq_err_cond) {
653 lapd_send_rr(&dl->lctx, 1, 1);
654 /* NOTE: In case of sequence error
655 * condition, the REJ frame has been
656 * transmitted when entering the
657 * condition, so it has not be done
658 * here
659 */
660 } else if (dl->own_busy) {
661 lapd_send_rnr(&dl->lctx, 1, 1);
662 } else {
663 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
Philipp Maier08177d32016-12-08 17:23:26 +0100664 "pls. fix (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200665 }
666 }
667 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200668 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200669 } else {
670 /* send MDL ERROR INIDCATION to L3 */
671 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
672 /* reestablish */
673 if (!dl->reestablish)
674 break;
Harald Welte7a569522019-06-02 22:37:21 +0200675 LOGP(DLLAPD, LOGL_NOTICE, "N200+1 reached, performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100676 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200677 lapd_reestablish(dl);
678 }
679 break;
680 default:
681 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
Harald Weltec733d142017-03-15 10:20:51 +0100682 "dl->state %s (dl=%p)\n", lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200683 }
684}
685
686/* Timer callback on T203 expiry */
687static void lapd_t203_cb(void *data)
688{
689 struct lapd_datalink *dl = data;
690
Philipp Maier08177d32016-12-08 17:23:26 +0100691 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100692 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200693
694 if (dl->state != LAPD_STATE_MF_EST) {
695 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
Philipp Maier08177d32016-12-08 17:23:26 +0100696 "please fix! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200697 return;
698 }
699
700 /* set retransmission counter to 0 */
701 dl->retrans_ctr = 0;
702 /* enter timer recovery state */
703 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
704 /* transmit a supervisory command with P bit set to 1 as follows: */
705 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +0100706 LOGP(DLLAPD, LOGL_INFO,
707 "transmit an RR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200708 /* Send RR with P=1 */
709 lapd_send_rr(&dl->lctx, 1, 1);
710 } else {
Philipp Maier08177d32016-12-08 17:23:26 +0100711 LOGP(DLLAPD, LOGL_INFO,
712 "transmit an RNR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200713 /* Send RNR with P=1 */
714 lapd_send_rnr(&dl->lctx, 1, 1);
715 }
716 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200717 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200718}
719
720/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
721static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
722{
723 struct lapd_datalink *dl = lctx->dl;
724 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100725 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200726 int i, h;
727
728 /* supervisory frame ? */
729 if (lctx->format == LAPD_FORM_S)
730 s = 1;
731 /* REJ frame ? */
732 if (s && lctx->s_u == LAPD_S_REJ)
733 rej = 1;
734
735 /* Flush all transmit buffers of acknowledged frames */
736 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
737 h = do_mod(i, dl->range_hist);
738 if (dl->tx_hist[h].msg) {
739 msgb_free(dl->tx_hist[h].msg);
740 dl->tx_hist[h].msg = NULL;
741 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
742 }
743 }
744
745 if (dl->state != LAPD_STATE_TIMER_RECOV) {
746 /* When not in the timer recovery condition, the data
747 * link layer entity shall reset the timer T200 on
748 * receipt of a valid I frame with N(R) higher than V(A),
749 * or an REJ with an N(R) equal to V(A). */
750 if ((!rej && nr != dl->v_ack)
751 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200752 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200753 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200754 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
755 }
756 /* 5.7.4: N(R) sequence error
757 * N(R) is called valid, if and only if
758 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
759 */
760 if (sub_mod(nr, dl->v_ack, dl->v_range)
761 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Philipp Maier08177d32016-12-08 17:23:26 +0100762 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200763 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
764 }
765 }
766
767 /* V(A) shall be set to the value of N(R) */
768 dl->v_ack = nr;
769
Andreas Eversberg742fc792011-09-27 09:40:25 +0200770 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200771 * and if there are outstanding I frames, restart T200 */
772 if (t200_reset && !rej) {
773 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
774 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
Philipp Maier08177d32016-12-08 17:23:26 +0100775 "frame(s) (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200776 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200777 }
778 }
779
780 /* This also does a restart, when I or S frame is received */
781
782 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200783 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200784 /* Start T203, if T200 is not running in MF EST state, if enabled */
785 if (!osmo_timer_pending(&dl->t200)
786 && (dl->t203_sec || dl->t203_usec)
787 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200788 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200789 }
790}
791
792/* L1 -> L2 */
793
794/* Receive a LAPD U (Unnumbered) message from L1 */
795static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
796{
797 struct lapd_datalink *dl = lctx->dl;
798 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100799 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200800 uint8_t prim, op;
801
802 switch (lctx->s_u) {
803 case LAPD_U_SABM:
804 case LAPD_U_SABME:
805 prim = PRIM_DL_EST;
806 op = PRIM_OP_INDICATION;
807
Philipp Maier08177d32016-12-08 17:23:26 +0100808 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100809 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200810 /* 5.7.1 */
811 dl->seq_err_cond = 0;
812 /* G.2.2 Wrong value of the C/R bit */
813 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +0100814 LOGP(DLLAPD, LOGL_ERROR,
815 "SABM response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200816 msgb_free(msg);
817 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
818 return -EINVAL;
819 }
820
821 /* G.4.5 If SABM is received with L>N201 or with M bit
822 * set, AN MDL-ERROR-INDICATION is sent to MM.
823 */
824 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +0100825 LOGP(DLLAPD, LOGL_ERROR,
826 "SABM too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200827 msgb_free(msg);
828 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
829 return -EIO;
830 }
831
832 switch (dl->state) {
833 case LAPD_STATE_IDLE:
834 break;
835 case LAPD_STATE_MF_EST:
836 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
Philipp Maier08177d32016-12-08 17:23:26 +0100837 "frame established state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200838 /* If link is lost on the remote side, we start over
839 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100840 /* Additionally, continue in case of content resoltion
841 * (GSM network). This happens, if the mobile has not
842 * yet received UA or another mobile (collision) tries
843 * to establish connection. The mobile must receive
844 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200845 /* 5.4.2.1 */
846 if (!length) {
847 /* If no content resolution, this is a
848 * re-establishment. */
849 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +0100850 "Remote reestablish (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200851 break;
852 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200853 if (!dl->cont_res) {
854 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Philipp Maier08177d32016-12-08 17:23:26 +0100855 "allowed in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100856 lapd_state_name(dl->state), dl);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200857 mdl_error(MDL_CAUSE_SABM_MF, lctx);
858 msgb_free(msg);
859 return 0;
860 }
rootaf48bed2011-09-26 11:23:06 +0200861 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200862 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200863#ifdef TEST_CONTENT_RESOLUTION_NETWORK
864 dl->cont_res->data[0] ^= 0x01;
865#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100866 if (memcmp(dl->cont_res->data, msg->data,
867 length)) {
rootaf48bed2011-09-26 11:23:06 +0200868 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100869 "with different content - "
Philipp Maier08177d32016-12-08 17:23:26 +0100870 "ignoring! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200871 msgb_free(msg);
872 return 0;
873 }
874 }
875 /* send UA again */
876 lapd_send_ua(lctx, length, msg->l3h);
877 msgb_free(msg);
878 return 0;
879 case LAPD_STATE_DISC_SENT:
880 /* 5.4.6.2 send DM with F=P */
881 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200882 /* stop Timer T200 */
883 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200884 msgb_free(msg);
885 return send_dl_simple(prim, op, lctx);
886 default:
887 /* collision: Send UA, but still wait for rx UA, then
888 * change to MF_EST state.
889 */
890 /* check for contention resoultion */
891 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
892 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Philipp Maier08177d32016-12-08 17:23:26 +0100893 "during contention resolution (state=%s, dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100894 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200895 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
896 }
897 lapd_send_ua(lctx, length, msg->l3h);
898 msgb_free(msg);
899 return 0;
900 }
901 /* save message context for further use */
902 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
903#ifndef TEST_CONTENT_RESOLUTION_NETWORK
904 /* send UA response */
905 lapd_send_ua(lctx, length, msg->l3h);
906#endif
907 /* set Vs, Vr and Va to 0 */
908 dl->v_send = dl->v_recv = dl->v_ack = 0;
909 /* clear tx_hist */
910 lapd_dl_flush_hist(dl);
911 /* enter multiple-frame-established state */
912 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
913 /* store content resolution data on network side
914 * Note: cont_res will be removed when changing state again,
915 * so it must be allocated AFTER lapd_dl_newstate(). */
916 if (dl->mode == LAPD_MODE_NETWORK && length) {
917 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
918 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
919 length);
Philipp Maier08177d32016-12-08 17:23:26 +0100920 LOGP(DLLAPD, LOGL_NOTICE,
921 "Store content res. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200922 }
923 /* send notification to L3 */
924 if (length == 0) {
925 /* 5.4.1.2 Normal establishment procedures */
926 rc = send_dl_simple(prim, op, lctx);
927 msgb_free(msg);
928 } else {
929 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200930 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200931 rc = send_dl_l3(prim, op, lctx, msg);
932 }
933 break;
934 case LAPD_U_DM:
Philipp Maier08177d32016-12-08 17:23:26 +0100935 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100936 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200937 /* G.2.2 Wrong value of the C/R bit */
938 if (lctx->cr == dl->cr.rem2loc.cmd) {
Philipp Maier08177d32016-12-08 17:23:26 +0100939 LOGP(DLLAPD, LOGL_ERROR,
940 "DM command error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200941 msgb_free(msg);
942 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
943 return -EINVAL;
944 }
945 if (!lctx->p_f) {
946 /* 5.4.1.2 DM responses with the F bit set to "0"
947 * shall be ignored.
948 */
949 msgb_free(msg);
950 return 0;
951 }
952 switch (dl->state) {
953 case LAPD_STATE_SABM_SENT:
954 break;
955 case LAPD_STATE_MF_EST:
956 if (lctx->p_f) {
957 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
Philipp Maier08177d32016-12-08 17:23:26 +0100958 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200959 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
960 } else {
961 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
962 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100963 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200964 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
965 /* reestablish */
966 if (!dl->reestablish) {
967 msgb_free(msg);
968 return 0;
969 }
970 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100971 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200972 lapd_reestablish(dl);
973 }
974 msgb_free(msg);
975 return 0;
976 case LAPD_STATE_TIMER_RECOV:
977 /* FP = 0 (DM is normal in case PF = 1) */
978 if (!lctx->p_f) {
979 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
980 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100981 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200982 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
983 msgb_free(msg);
984 /* reestablish */
985 if (!dl->reestablish)
986 return 0;
987 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100988 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200989 return lapd_reestablish(dl);
990 }
991 break;
992 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200993 /* stop Timer T200 */
994 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200995 /* go to idle state */
996 lapd_dl_flush_tx(dl);
997 lapd_dl_flush_send(dl);
998 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
999 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1000 msgb_free(msg);
1001 return 0;
1002 case LAPD_STATE_IDLE:
1003 /* 5.4.5 all other frame types shall be discarded */
1004 default:
1005 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001006 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001007 msgb_free(msg);
1008 return 0;
1009 }
Andreas Eversberg742fc792011-09-27 09:40:25 +02001010 /* stop timer T200 */
1011 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001012 /* go to idle state */
1013 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1014 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1015 msgb_free(msg);
1016 break;
1017 case LAPD_U_UI:
Philipp Maier08177d32016-12-08 17:23:26 +01001018 LOGP(DLLAPD, LOGL_INFO, "UI received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001019 /* G.2.2 Wrong value of the C/R bit */
1020 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001021 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
Philipp Maier08177d32016-12-08 17:23:26 +01001022 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001023 msgb_free(msg);
1024 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1025 return -EINVAL;
1026 }
1027
1028 /* G.4.5 If UI is received with L>N201 or with M bit
1029 * set, AN MDL-ERROR-INDICATION is sent to MM.
1030 */
1031 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001032 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
Philipp Maier08177d32016-12-08 17:23:26 +01001033 "(%d > N201(%d) or M=%d) (dl=%p)\n", length,
1034 lctx->n201, lctx->more, dl);
rootaf48bed2011-09-26 11:23:06 +02001035 msgb_free(msg);
1036 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1037 return -EIO;
1038 }
1039
1040 /* do some length checks */
1041 if (length == 0) {
1042 /* 5.3.3 UI frames received with the length indicator
1043 * set to "0" shall be ignored
1044 */
Philipp Maier08177d32016-12-08 17:23:26 +01001045 LOGP(DLLAPD, LOGL_INFO,
1046 "length=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001047 msgb_free(msg);
1048 return 0;
1049 }
Harald Welte087116a2013-06-18 21:41:34 +02001050 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001051 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1052 msg);
1053 break;
1054 case LAPD_U_DISC:
1055 prim = PRIM_DL_REL;
1056 op = PRIM_OP_INDICATION;
1057
Philipp Maier08177d32016-12-08 17:23:26 +01001058 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001059 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001060 /* flush tx and send buffers */
1061 lapd_dl_flush_tx(dl);
1062 lapd_dl_flush_send(dl);
1063 /* 5.7.1 */
1064 dl->seq_err_cond = 0;
1065 /* G.2.2 Wrong value of the C/R bit */
1066 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001067 LOGP(DLLAPD, LOGL_ERROR,
1068 "DISC response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001069 msgb_free(msg);
1070 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1071 return -EINVAL;
1072 }
1073 if (length > 0 || lctx->more) {
1074 /* G.4.4 If a DISC or DM frame is received with L>0 or
1075 * with the M bit set to "1", an MDL-ERROR-INDICATION
1076 * primitive with cause "U frame with incorrect
1077 * parameters" is sent to the mobile management entity.
1078 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001079 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001080 "U frame iwth incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001081 msgb_free(msg);
1082 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1083 return -EIO;
1084 }
1085 switch (dl->state) {
1086 case LAPD_STATE_IDLE:
Philipp Maier08177d32016-12-08 17:23:26 +01001087 LOGP(DLLAPD, LOGL_INFO,
1088 "DISC in idle state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001089 /* send DM with F=P */
1090 msgb_free(msg);
1091 return lapd_send_dm(lctx);
1092 case LAPD_STATE_SABM_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001093 LOGP(DLLAPD, LOGL_INFO,
1094 "DISC in SABM state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001095 /* 5.4.6.2 send DM with F=P */
1096 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001097 /* stop Timer T200 */
1098 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001099 /* go to idle state */
1100 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1101 msgb_free(msg);
1102 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1103 lctx);
1104 case LAPD_STATE_MF_EST:
1105 case LAPD_STATE_TIMER_RECOV:
Philipp Maier08177d32016-12-08 17:23:26 +01001106 LOGP(DLLAPD, LOGL_INFO,
1107 "DISC in est state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001108 break;
1109 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001110 LOGP(DLLAPD, LOGL_INFO,
1111 "DISC in disc state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001112 prim = PRIM_DL_REL;
1113 op = PRIM_OP_CONFIRM;
1114 break;
1115 default:
1116 lapd_send_ua(lctx, length, msg->l3h);
1117 msgb_free(msg);
1118 return 0;
1119 }
1120 /* send UA response */
1121 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001122 /* stop Timer T200 */
1123 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001124 /* enter idle state, keep tx-buffer with UA response */
1125 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1126 /* send notification to L3 */
1127 rc = send_dl_simple(prim, op, lctx);
1128 msgb_free(msg);
1129 break;
1130 case LAPD_U_UA:
Philipp Maier08177d32016-12-08 17:23:26 +01001131 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001132 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001133 /* G.2.2 Wrong value of the C/R bit */
1134 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001135 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
Philipp Maier08177d32016-12-08 17:23:26 +01001136 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001137 msgb_free(msg);
1138 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1139 return -EINVAL;
1140 }
1141
1142 /* G.4.5 If UA is received with L>N201 or with M bit
1143 * set, AN MDL-ERROR-INDICATION is sent to MM.
1144 */
1145 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001146 LOGP(DLLAPD, LOGL_ERROR,
1147 "UA too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001148 msgb_free(msg);
1149 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1150 return -EIO;
1151 }
1152
1153 if (!lctx->p_f) {
1154 /* 5.4.1.2 A UA response with the F bit set to "0"
1155 * shall be ignored.
1156 */
Philipp Maier08177d32016-12-08 17:23:26 +01001157 LOGP(DLLAPD, LOGL_INFO,
1158 "F=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001159 msgb_free(msg);
1160 return 0;
1161 }
1162 switch (dl->state) {
1163 case LAPD_STATE_SABM_SENT:
1164 break;
1165 case LAPD_STATE_MF_EST:
1166 case LAPD_STATE_TIMER_RECOV:
1167 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001168 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001169 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1170 msgb_free(msg);
1171 return 0;
1172 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001173 LOGP(DLLAPD, LOGL_INFO,
1174 "UA in disconnect state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001175 /* stop Timer T200 */
1176 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001177 /* go to idle state */
1178 lapd_dl_flush_tx(dl);
1179 lapd_dl_flush_send(dl);
1180 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1181 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1182 msgb_free(msg);
1183 return 0;
1184 case LAPD_STATE_IDLE:
1185 /* 5.4.5 all other frame types shall be discarded */
1186 default:
1187 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001188 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001189 msgb_free(msg);
1190 return 0;
1191 }
Philipp Maier08177d32016-12-08 17:23:26 +01001192 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001193 /* stop Timer T200 */
1194 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001195 /* compare UA with SABME if contention resolution is applied */
1196 if (dl->tx_hist[0].msg->len) {
1197 if (length != (dl->tx_hist[0].msg->len)
1198 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1199 length)) {
1200 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
Philipp Maier08177d32016-12-08 17:23:26 +01001201 "mismatches **** (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001202 rc = send_dl_simple(PRIM_DL_REL,
1203 PRIM_OP_INDICATION, lctx);
1204 msgb_free(msg);
1205 /* go to idle state */
1206 lapd_dl_flush_tx(dl);
1207 lapd_dl_flush_send(dl);
1208 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1209 return 0;
1210 }
1211 }
1212 /* set Vs, Vr and Va to 0 */
1213 dl->v_send = dl->v_recv = dl->v_ack = 0;
1214 /* clear tx_hist */
1215 lapd_dl_flush_hist(dl);
1216 /* enter multiple-frame-established state */
1217 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1218 /* send outstanding frames, if any (resume / reconnect) */
1219 lapd_send_i(lctx, __LINE__);
1220 /* send notification to L3 */
1221 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1222 msgb_free(msg);
1223 break;
1224 case LAPD_U_FRMR:
Philipp Maier08177d32016-12-08 17:23:26 +01001225 LOGP(DLLAPD, LOGL_NOTICE,
1226 "Frame reject received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001227 /* send MDL ERROR INIDCATION to L3 */
1228 mdl_error(MDL_CAUSE_FRMR, lctx);
1229 msgb_free(msg);
1230 /* reestablish */
1231 if (!dl->reestablish)
1232 break;
Philipp Maier08177d32016-12-08 17:23:26 +01001233 LOGP(DLLAPD, LOGL_NOTICE,
1234 "Performing reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001235 rc = lapd_reestablish(dl);
1236 break;
1237 default:
1238 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001239 LOGP(DLLAPD, LOGL_NOTICE,
1240 "Unnumbered frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001241 msgb_free(msg);
1242 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1243 return -EINVAL;
1244 }
1245 return rc;
1246}
1247
1248/* Receive a LAPD S (Supervisory) message from L1 */
1249static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1250{
1251 struct lapd_datalink *dl = lctx->dl;
1252 int length = lctx->length;
1253
1254 if (length > 0 || lctx->more) {
1255 /* G.4.3 If a supervisory frame is received with L>0 or
1256 * with the M bit set to "1", an MDL-ERROR-INDICATION
1257 * primitive with cause "S frame with incorrect
1258 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001259 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001260 "S frame with incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001261 msgb_free(msg);
1262 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1263 return -EIO;
1264 }
1265
1266 if (lctx->cr == dl->cr.rem2loc.resp
1267 && lctx->p_f
1268 && dl->state != LAPD_STATE_TIMER_RECOV) {
1269 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001270 LOGP(DLLAPD, LOGL_NOTICE,
1271 "S frame response with F=1 error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001272 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1273 }
1274
1275 switch (dl->state) {
1276 case LAPD_STATE_IDLE:
1277 /* if P=1, respond DM with F=1 (5.2.2) */
1278 /* 5.4.5 all other frame types shall be discarded */
1279 if (lctx->p_f)
1280 lapd_send_dm(lctx); /* F=P */
1281 /* fall though */
1282 case LAPD_STATE_SABM_SENT:
1283 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001284 LOGP(DLLAPD, LOGL_NOTICE,
1285 "S frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001286 msgb_free(msg);
1287 return 0;
1288 }
1289 switch (lctx->s_u) {
1290 case LAPD_S_RR:
Philipp Maier08177d32016-12-08 17:23:26 +01001291 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001292 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001293 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1294 lapd_acknowledge(lctx);
1295
1296 /* 5.5.3.2 */
1297 if (lctx->cr == dl->cr.rem2loc.cmd
1298 && lctx->p_f) {
1299 if (!dl->own_busy && !dl->seq_err_cond) {
1300 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001301 "with polling bit set and we are not "
1302 "busy, so we reply with RR frame "
1303 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001304 lapd_send_rr(lctx, 1, 0);
1305 /* NOTE: In case of sequence error condition,
1306 * the REJ frame has been transmitted when
1307 * entering the condition, so it has not be
1308 * done here
1309 */
1310 } else if (dl->own_busy) {
1311 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001312 "with polling bit set and we are busy, "
1313 "so we reply with RR frame response (dl=%p)\n",
1314 dl);
rootaf48bed2011-09-26 11:23:06 +02001315 lapd_send_rnr(lctx, 1, 0);
1316 }
1317 } else if (lctx->cr == dl->cr.rem2loc.resp
1318 && lctx->p_f
1319 && dl->state == LAPD_STATE_TIMER_RECOV) {
1320 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1321 "and we are in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001322 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001323 /* V(S) to the N(R) in the RR frame */
1324 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001325 /* stop Timer T200 */
1326 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001327 /* 5.5.7 Clear timer recovery condition */
1328 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1329 }
1330 /* Send message, if possible due to acknowledged data */
1331 lapd_send_i(lctx, __LINE__);
1332
1333 break;
1334 case LAPD_S_RNR:
Philipp Maier08177d32016-12-08 17:23:26 +01001335 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001336 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001337 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1338 lapd_acknowledge(lctx);
1339
1340 /* 5.5.5 */
1341 /* Set peer receiver busy condition */
1342 dl->peer_busy = 1;
1343
1344 if (lctx->p_f) {
1345 if (lctx->cr == dl->cr.rem2loc.cmd) {
1346 if (!dl->own_busy) {
1347 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1348 "command and we are not busy, "
1349 "so we reply with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001350 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001351 /* Send RR with F=1 */
1352 lapd_send_rr(lctx, 1, 0);
1353 } else {
1354 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1355 "command and we are busy, so "
1356 "we reply with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001357 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001358 /* Send RNR with F=1 */
1359 lapd_send_rnr(lctx, 1, 0);
1360 }
1361 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1362 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1363 "and we in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001364 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001365 /* 5.5.7 Clear timer recovery condition */
1366 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1367 /* V(S) to the N(R) in the RNR frame */
1368 dl->v_send = lctx->n_recv;
1369 }
1370 } else
1371 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
Philipp Maier08177d32016-12-08 17:23:26 +01001372 "received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001373
1374 /* Send message, if possible due to acknowledged data */
1375 lapd_send_i(lctx, __LINE__);
1376
1377 break;
1378 case LAPD_S_REJ:
Philipp Maier08177d32016-12-08 17:23:26 +01001379 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001380 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001381 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1382 lapd_acknowledge(lctx);
1383
1384 /* 5.5.4.1 */
1385 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1386 /* Clear an existing peer receiver busy condition */
1387 dl->peer_busy = 0;
1388 /* V(S) and V(A) to the N(R) in the REJ frame */
1389 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001390 /* stop Timer T200 */
1391 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001392 /* 5.5.3.2 */
1393 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1394 if (!dl->own_busy && !dl->seq_err_cond) {
1395 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1396 "command not in timer recovery "
1397 "state and not in own busy "
1398 "condition received, so we "
1399 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001400 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001401 lapd_send_rr(lctx, 1, 0);
1402 /* NOTE: In case of sequence error
1403 * condition, the REJ frame has been
1404 * transmitted when entering the
1405 * condition, so it has not be done
1406 * here
1407 */
1408 } else if (dl->own_busy) {
1409 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1410 "command not in timer recovery "
1411 "state and in own busy "
1412 "condition received, so we "
1413 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001414 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001415 lapd_send_rnr(lctx, 1, 0);
1416 }
1417 } else
1418 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1419 "polling command not in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001420 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001421 /* send MDL ERROR INIDCATION to L3 */
1422 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Philipp Maier08177d32016-12-08 17:23:26 +01001423 LOGP(DLLAPD, LOGL_ERROR,
1424 "unsolicited supervisory response! (dl=%p)\n",
1425 dl);
rootaf48bed2011-09-26 11:23:06 +02001426 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1427 }
1428
1429 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1430 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
Philipp Maier08177d32016-12-08 17:23:26 +01001431 "recovery state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001432 /* Clear an existing peer receiver busy condition */
1433 dl->peer_busy = 0;
1434 /* V(S) and V(A) to the N(R) in the REJ frame */
1435 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001436 /* stop Timer T200 */
1437 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001438 /* 5.5.7 Clear timer recovery condition */
1439 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1440 } else {
1441 /* Clear an existing peer receiver busy condition */
1442 dl->peer_busy = 0;
1443 /* V(S) and V(A) to the N(R) in the REJ frame */
1444 dl->v_send = dl->v_ack = lctx->n_recv;
1445 /* 5.5.3.2 */
1446 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1447 if (!dl->own_busy && !dl->seq_err_cond) {
1448 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1449 "command in timer recovery "
1450 "state and not in own busy "
1451 "condition received, so we "
1452 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001453 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001454 lapd_send_rr(lctx, 1, 0);
1455 /* NOTE: In case of sequence error
1456 * condition, the REJ frame has been
1457 * transmitted when entering the
1458 * condition, so it has not be done
1459 * here
1460 */
1461 } else if (dl->own_busy) {
1462 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1463 "command in timer recovery "
1464 "state and in own busy "
1465 "condition received, so we "
1466 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001467 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001468 lapd_send_rnr(lctx, 1, 0);
1469 }
1470 } else
1471 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1472 "polling command in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001473 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001474 }
1475
1476 /* FIXME: 5.5.4.2 2) */
1477
1478 /* Send message, if possible due to acknowledged data */
1479 lapd_send_i(lctx, __LINE__);
1480
1481 break;
1482 default:
1483 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001484 LOGP(DLLAPD, LOGL_ERROR,
1485 "Supervisory frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001486 msgb_free(msg);
1487 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1488 return -EINVAL;
1489 }
1490 msgb_free(msg);
1491 return 0;
1492}
1493
1494/* Receive a LAPD I (Information) message from L1 */
1495static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1496{
1497 struct lapd_datalink *dl = lctx->dl;
1498 //uint8_t nr = lctx->n_recv;
1499 uint8_t ns = lctx->n_send;
1500 int length = lctx->length;
1501 int rc;
1502
Philipp Maier08177d32016-12-08 17:23:26 +01001503 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u) (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001504 lapd_state_name(dl->state), lctx->sapi, dl);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001505
rootaf48bed2011-09-26 11:23:06 +02001506 /* G.2.2 Wrong value of the C/R bit */
1507 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001508 LOGP(DLLAPD, LOGL_ERROR,
Maxd00a43f2017-12-04 16:29:27 +01001509 "I frame response not allowed (dl=%p state %s)\n", dl, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001510 msgb_free(msg);
1511 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1512 return -EINVAL;
1513 }
1514
1515 if (length == 0 || length > lctx->n201) {
1516 /* G.4.2 If the length indicator of an I frame is set
1517 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1518 * primitive with cause "I frame with incorrect length"
1519 * is sent to the mobile management entity. */
Philipp Maier08177d32016-12-08 17:23:26 +01001520 LOGP(DLLAPD, LOGL_ERROR,
Maxd00a43f2017-12-04 16:29:27 +01001521 "I frame length not allowed (dl=%p state %s)\n", dl, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001522 msgb_free(msg);
1523 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1524 return -EIO;
1525 }
1526
1527 /* G.4.2 If the numerical value of L is L<N201 and the M
1528 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1529 * cause "I frame with incorrect use of M bit" is sent to the
1530 * mobile management entity. */
1531 if (lctx->more && length < lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001532 LOGP(DLLAPD, LOGL_ERROR,
Maxd00a43f2017-12-04 16:29:27 +01001533 "I frame with M bit too short (dl=%p state %s)\n", dl, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001534 msgb_free(msg);
1535 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1536 return -EIO;
1537 }
1538
1539 switch (dl->state) {
1540 case LAPD_STATE_IDLE:
1541 /* if P=1, respond DM with F=1 (5.2.2) */
1542 /* 5.4.5 all other frame types shall be discarded */
1543 if (lctx->p_f)
1544 lapd_send_dm(lctx); /* F=P */
1545 /* fall though */
1546 case LAPD_STATE_SABM_SENT:
1547 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001548 LOGP(DLLAPD, LOGL_NOTICE,
Maxd00a43f2017-12-04 16:29:27 +01001549 "I frame ignored in state %s (dl=%p)\n", lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001550 msgb_free(msg);
1551 return 0;
1552 }
1553
1554 /* 5.7.1: N(s) sequence error */
1555 if (ns != dl->v_recv) {
1556 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
Maxd00a43f2017-12-04 16:29:27 +01001557 "V(R)=%u (dl=%p state %s)\n", ns, dl->v_recv, dl, lapd_state_name(dl->state));
rootaf48bed2011-09-26 11:23:06 +02001558 /* discard data */
1559 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001560 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001561 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001562 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001563 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001564 lapd_send_rej(lctx, lctx->p_f);
1565 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001566 /* If there are two subsequent sequence errors received,
1567 * ignore it. (Ignore every second subsequent error.)
1568 * This happens if our reply with the REJ is too slow,
1569 * so the remote gets a T200 timeout and sends another
1570 * frame with a sequence error.
1571 * Test showed that replying with two subsequent REJ
1572 * messages could the remote L2 process to abort.
1573 * Replying too slow shouldn't happen, but may happen
1574 * over serial link between BB and LAPD.
1575 */
1576 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001577 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001578 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1579 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1580 lapd_acknowledge(lctx); /* V(A) is also set here */
1581
1582 /* Send message, if possible due to acknowledged data */
1583 lapd_send_i(lctx, __LINE__);
1584
1585 return 0;
rootaf48bed2011-09-26 11:23:06 +02001586 }
1587 dl->seq_err_cond = 0;
1588
1589 /* Increment receiver state */
1590 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Philipp Maier08177d32016-12-08 17:23:26 +01001591 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u (dl=%p)\n",
1592 dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001593
1594 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1595 lapd_acknowledge(lctx); /* V(A) is also set here */
1596
1597 /* Only if we are not in own receiver busy condition */
1598 if (!dl->own_busy) {
1599 /* if the frame carries a complete segment */
1600 if (!lctx->more && !dl->rcv_buffer) {
Philipp Maier08177d32016-12-08 17:23:26 +01001601 LOGP(DLLAPD, LOGL_INFO,
1602 "message in single I frame (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001603 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001604 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001605 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1606 msg);
1607 } else {
1608 /* create rcv_buffer */
1609 if (!dl->rcv_buffer) {
1610 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001611 "I frames (first message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001612 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1613 "LAPD RX");
1614 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1615 }
1616 /* concat. rcv_buffer */
1617 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1618 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
Philipp Maier08177d32016-12-08 17:23:26 +01001619 "overflow! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001620 } else {
1621 memcpy(msgb_put(dl->rcv_buffer, length),
1622 msg->l3h, length);
1623 }
1624 /* if the last segment was received */
1625 if (!lctx->more) {
1626 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001627 "I frames (last message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001628 rc = send_dl_l3(PRIM_DL_DATA,
1629 PRIM_OP_INDICATION, lctx,
1630 dl->rcv_buffer);
1631 dl->rcv_buffer = NULL;
1632 } else
1633 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001634 "I frames (next message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001635 msgb_free(msg);
1636
1637 }
1638 } else
1639 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1640 "busy condition\n");
1641
1642 /* Check for P bit */
1643 if (lctx->p_f) {
1644 /* 5.5.2.1 */
1645 /* check if we are not in own receiver busy */
1646 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001647 LOGP(DLLAPD, LOGL_INFO,
1648 "we are not busy, send RR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001649 /* Send RR with F=1 */
1650 rc = lapd_send_rr(lctx, 1, 0);
1651 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001652 LOGP(DLLAPD, LOGL_INFO,
1653 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001654 /* Send RNR with F=1 */
1655 rc = lapd_send_rnr(lctx, 1, 0);
1656 }
1657 } else {
1658 /* 5.5.2.2 */
1659 /* check if we are not in own receiver busy */
1660 if (!dl->own_busy) {
1661 /* NOTE: V(R) is already set above */
1662 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001663
1664 /* if update_pending_iframe returns 0 it updated
1665 * the lapd header of an iframe in the tx queue */
1666 if (rc && dl->update_pending_frames)
1667 rc = dl->update_pending_frames(lctx);
1668
rootaf48bed2011-09-26 11:23:06 +02001669 if (rc) {
1670 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
Philipp Maier08177d32016-12-08 17:23:26 +01001671 "have no pending data, send RR (dl=%p)\n",
1672 dl);
rootaf48bed2011-09-26 11:23:06 +02001673 /* Send RR with F=0 */
1674 return lapd_send_rr(lctx, 0, 0);
1675 }
1676 /* all I or one RR is sent, we are done */
1677 return 0;
1678 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001679 LOGP(DLLAPD, LOGL_INFO,
1680 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001681 /* Send RNR with F=0 */
1682 rc = lapd_send_rnr(lctx, 0, 0);
1683 }
1684 }
1685
1686 /* Send message, if possible due to acknowledged data */
1687 lapd_send_i(lctx, __LINE__);
1688
1689 return rc;
1690}
1691
1692/* Receive a LAPD message from L1 */
1693int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1694{
1695 int rc;
1696
1697 switch (lctx->format) {
1698 case LAPD_FORM_U:
1699 rc = lapd_rx_u(msg, lctx);
1700 break;
1701 case LAPD_FORM_S:
1702 rc = lapd_rx_s(msg, lctx);
1703 break;
1704 case LAPD_FORM_I:
1705 rc = lapd_rx_i(msg, lctx);
1706 break;
1707 default:
Philipp Maier08177d32016-12-08 17:23:26 +01001708 LOGP(DLLAPD, LOGL_NOTICE,
1709 "unknown LAPD format (dl=%p)\n", lctx->dl);
rootaf48bed2011-09-26 11:23:06 +02001710 msgb_free(msg);
1711 rc = -EINVAL;
1712 }
1713 return rc;
1714}
1715
1716/* L3 -> L2 */
1717
1718/* send unit data */
1719static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1720{
1721 struct lapd_datalink *dl = lctx->dl;
1722 struct msgb *msg = dp->oph.msg;
1723 struct lapd_msg_ctx nctx;
1724
1725 memcpy(&nctx, lctx, sizeof(nctx));
1726 /* keep nctx.ldp */
1727 /* keep nctx.sapi */
1728 /* keep nctx.tei */
1729 nctx.cr = dl->cr.loc2rem.cmd;
1730 nctx.format = LAPD_FORM_U;
1731 nctx.s_u = LAPD_U_UI;
1732 /* keep nctx.p_f */
1733 nctx.length = msg->len;
1734 nctx.more = 0;
1735
1736 return dl->send_ph_data_req(&nctx, msg);
1737}
1738
1739/* request link establishment */
1740static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1741{
1742 struct lapd_datalink *dl = lctx->dl;
1743 struct msgb *msg = dp->oph.msg;
1744 struct lapd_msg_ctx nctx;
1745
1746 if (msg->len)
1747 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
Philipp Maier08177d32016-12-08 17:23:26 +01001748 "(SABM) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001749 else
Philipp Maier08177d32016-12-08 17:23:26 +01001750 LOGP(DLLAPD, LOGL_INFO,
1751 "perform normal establishm. (SABM), (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001752
1753 /* Flush send-queue */
1754 /* Clear send-buffer */
1755 lapd_dl_flush_send(dl);
1756 /* be sure that history is empty */
1757 lapd_dl_flush_hist(dl);
1758
1759 /* save message context for further use */
1760 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1761
1762 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001763 msgb_free(dl->rcv_buffer);
1764 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001765
1766 /* assemble message */
1767 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1768 /* keep nctx.ldp */
1769 /* keep nctx.sapi */
1770 /* keep nctx.tei */
1771 nctx.cr = dl->cr.loc2rem.cmd;
1772 nctx.format = LAPD_FORM_U;
1773 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1774 nctx.p_f = 1;
1775 nctx.length = msg->len;
1776 nctx.more = 0;
1777
1778 /* Transmit-buffer carries exactly one segment */
1779 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1780 msgb_put(dl->tx_hist[0].msg, msg->len);
1781 if (msg->len)
1782 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1783 dl->tx_hist[0].more = 0;
1784 /* set Vs to 0, because it is used as index when resending SABM */
1785 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001786
rootaf48bed2011-09-26 11:23:06 +02001787 /* Set states */
1788 dl->own_busy = dl->peer_busy = 0;
1789 dl->retrans_ctr = 0;
1790 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1791
1792 /* Tramsmit and start T200 */
1793 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001794 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001795
1796 return 0;
1797}
1798
1799/* send data */
1800static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1801{
1802 struct lapd_datalink *dl = lctx->dl;
1803 struct msgb *msg = dp->oph.msg;
1804
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001805 if (msgb_l3len(msg) == 0) {
1806 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001807 "writing an empty message is not possible. (dl=%p)\n", dl);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001808 msgb_free(msg);
1809 return -1;
1810 }
1811
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001812 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +01001813 "writing message to send-queue: l3len: %d (dl=%p)\n",
1814 msgb_l3len(msg), dl);
rootaf48bed2011-09-26 11:23:06 +02001815
1816 /* Write data into the send queue */
1817 msgb_enqueue(&dl->send_queue, msg);
1818
1819 /* Send message, if possible */
1820 lapd_send_i(&dl->lctx, __LINE__);
1821
1822 return 0;
1823}
1824
1825/* Send next I frame from queued/buffered data */
1826static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1827{
1828 struct lapd_datalink *dl = lctx->dl;
1829 uint8_t k = dl->k;
1830 uint8_t h;
1831 struct msgb *msg;
1832 int length, left;
1833 int rc = - 1; /* we sent nothing */
1834 struct lapd_msg_ctx nctx;
1835
1836
Philipp Maier08177d32016-12-08 17:23:26 +01001837 LOGP(DLLAPD, LOGL_INFO,
1838 "%s() called from line %d (dl=%p)\n", __func__, line, dl);
rootaf48bed2011-09-26 11:23:06 +02001839
1840 next_frame:
1841
1842 if (dl->peer_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001843 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001844 return rc;
1845 }
1846
1847 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Philipp Maier08177d32016-12-08 17:23:26 +01001848 LOGP(DLLAPD, LOGL_INFO,
1849 "timer recovery, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001850 return rc;
1851 }
1852
1853 /* If the send state variable V(S) is equal to V(A) plus k
1854 * (where k is the maximum number of outstanding I frames - see
1855 * subclause 5.8.4), the data link layer entity shall not transmit any
1856 * new I frames, but shall retransmit an I frame as a result
1857 * of the error recovery procedures as described in subclauses 5.5.4 and
1858 * 5.5.7. */
1859 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1860 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
Philipp Maier08177d32016-12-08 17:23:26 +01001861 "more (k=%u V(S)=%u V(A)=%u) (dl=%p)\n", k, dl->v_send,
1862 dl->v_ack, dl);
rootaf48bed2011-09-26 11:23:06 +02001863 return rc;
1864 }
1865
1866 h = do_mod(dl->v_send, dl->range_hist);
1867
1868 /* if we have no tx_hist yet, we create it */
1869 if (!dl->tx_hist[h].msg) {
1870 /* Get next message into send-buffer, if any */
1871 if (!dl->send_buffer) {
1872 next_message:
1873 dl->send_out = 0;
1874 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1875 /* No more data to be sent */
1876 if (!dl->send_buffer)
1877 return rc;
1878 LOGP(DLLAPD, LOGL_INFO, "get message from "
Philipp Maier08177d32016-12-08 17:23:26 +01001879 "send-queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001880 }
1881
1882 /* How much is left in the send-buffer? */
1883 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1884 /* Segment, if data exceeds N201 */
1885 length = left;
1886 if (length > lctx->n201)
1887 length = lctx->n201;
1888 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
Philipp Maier08177d32016-12-08 17:23:26 +01001889 "length %d first byte %02x (dl=%p)\n",
rootaf48bed2011-09-26 11:23:06 +02001890 msgb_l3len(dl->send_buffer), dl->send_out, left,
Philipp Maier08177d32016-12-08 17:23:26 +01001891 lctx->n201, length, dl->send_buffer->l3h[0], dl);
rootaf48bed2011-09-26 11:23:06 +02001892 /* If message in send-buffer is completely sent */
1893 if (left == 0) {
1894 msgb_free(dl->send_buffer);
1895 dl->send_buffer = NULL;
1896 goto next_message;
1897 }
1898
Philipp Maier08177d32016-12-08 17:23:26 +01001899 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d (dl=%p)\n",
1900 (left > length) ? "segment " : "", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001901
1902 /* Create I frame (segment) and transmit-buffer content */
1903 msg = lapd_msgb_alloc(length, "LAPD I");
1904 msg->l3h = msgb_put(msg, length);
1905 /* assemble message */
1906 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1907 /* keep nctx.ldp */
1908 /* keep nctx.sapi */
1909 /* keep nctx.tei */
1910 nctx.cr = dl->cr.loc2rem.cmd;
1911 nctx.format = LAPD_FORM_I;
1912 nctx.p_f = 0;
1913 nctx.n_send = dl->v_send;
1914 nctx.n_recv = dl->v_recv;
1915 nctx.length = length;
1916 if (left > length)
1917 nctx.more = 1;
1918 else
1919 nctx.more = 0;
1920 if (length)
1921 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1922 length);
1923 /* store in tx_hist */
1924 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1925 msgb_put(dl->tx_hist[h].msg, msg->len);
1926 if (length)
1927 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1928 dl->tx_hist[h].more = nctx.more;
1929 /* Add length to track how much is already in the tx buffer */
1930 dl->send_out += length;
1931 } else {
1932 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
Philipp Maier08177d32016-12-08 17:23:26 +01001933 "V(S)=%d (dl=%p)\n", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001934
1935 /* Create I frame (segment) from tx_hist */
1936 length = dl->tx_hist[h].msg->len;
1937 msg = lapd_msgb_alloc(length, "LAPD I resend");
1938 msg->l3h = msgb_put(msg, length);
1939 /* assemble message */
1940 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1941 /* keep nctx.ldp */
1942 /* keep nctx.sapi */
1943 /* keep nctx.tei */
1944 nctx.cr = dl->cr.loc2rem.cmd;
1945 nctx.format = LAPD_FORM_I;
1946 nctx.p_f = 0;
1947 nctx.n_send = dl->v_send;
1948 nctx.n_recv = dl->v_recv;
1949 nctx.length = length;
1950 nctx.more = dl->tx_hist[h].more;
1951 if (length)
1952 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1953 }
1954
1955 /* The value of the send state variable V(S) shall be incremented by 1
1956 * at the end of the transmission of the I frame */
1957 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1958
1959 /* If timer T200 is not running at the time right before transmitting a
1960 * frame, when the PH-READY-TO-SEND primitive is received from the
1961 * physical layer., it shall be set. */
1962 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001963 /* stop Timer T203, if running */
1964 lapd_stop_t203(dl);
1965 /* start Timer T200 */
1966 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001967 }
1968
1969 dl->send_ph_data_req(&nctx, msg);
1970
1971 rc = 0; /* we sent something */
1972 goto next_frame;
1973}
1974
1975/* request link suspension */
1976static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1977{
1978 struct lapd_datalink *dl = lctx->dl;
1979 struct msgb *msg = dp->oph.msg;
1980
Philipp Maier08177d32016-12-08 17:23:26 +01001981 LOGP(DLLAPD, LOGL_INFO, "perform suspension (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001982
1983 /* put back the send-buffer to the send-queue (first position) */
1984 if (dl->send_buffer) {
1985 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
Philipp Maier08177d32016-12-08 17:23:26 +01001986 "queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001987 llist_add(&dl->send_buffer->list, &dl->send_queue);
1988 dl->send_buffer = NULL;
1989 } else
Philipp Maier08177d32016-12-08 17:23:26 +01001990 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001991
1992 /* Clear transmit buffer, but keep send buffer */
1993 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001994 /* Stop timers (there is no state change, so we must stop all timers */
1995 lapd_stop_t200(dl);
1996 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001997
1998 msgb_free(msg);
1999
2000 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
2001}
2002
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002003/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002004static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2005{
2006 struct lapd_datalink *dl = lctx->dl;
2007 struct msgb *msg = dp->oph.msg;
2008 struct lapd_msg_ctx nctx;
2009
Philipp Maier08177d32016-12-08 17:23:26 +01002010 LOGP(DLLAPD, LOGL_INFO,
2011 "perform re-establishment (SABM) length=%d (dl=%p)\n",
2012 msg->len, dl);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002013
rootaf48bed2011-09-26 11:23:06 +02002014 /* be sure that history is empty */
2015 lapd_dl_flush_hist(dl);
2016
2017 /* save message context for further use */
2018 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2019
2020 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002021 msgb_free(dl->send_buffer);
2022 dl->send_buffer = NULL;
2023
rootaf48bed2011-09-26 11:23:06 +02002024 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002025 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002026 /* Write data into the send buffer, to be sent first */
2027 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002028 } else {
2029 msgb_free(msg);
2030 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002031 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002032 }
rootaf48bed2011-09-26 11:23:06 +02002033
2034 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002035 msgb_free(dl->rcv_buffer);
2036 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002037
2038 /* Create new msgb (old one is now free) */
2039 msg = lapd_msgb_alloc(0, "LAPD SABM");
2040 msg->l3h = msg->data;
2041 /* assemble message */
2042 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2043 /* keep nctx.ldp */
2044 /* keep nctx.sapi */
2045 /* keep nctx.tei */
2046 nctx.cr = dl->cr.loc2rem.cmd;
2047 nctx.format = LAPD_FORM_U;
2048 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2049 nctx.p_f = 1;
2050 nctx.length = 0;
2051 nctx.more = 0;
2052
2053 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2054 msgb_put(dl->tx_hist[0].msg, msg->len);
2055 if (msg->len)
2056 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2057 dl->tx_hist[0].more = 0;
2058 /* set Vs to 0, because it is used as index when resending SABM */
2059 dl->v_send = 0;
2060
2061 /* Set states */
2062 dl->own_busy = dl->peer_busy = 0;
2063 dl->retrans_ctr = 0;
2064 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2065
2066 /* Tramsmit and start T200 */
2067 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002068 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002069
2070 return 0;
2071}
2072
2073/* requesst release of link */
2074static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2075{
2076 struct lapd_datalink *dl = lctx->dl;
2077 struct msgb *msg = dp->oph.msg;
2078 struct lapd_msg_ctx nctx;
2079
2080 /* local release */
2081 if (dp->u.rel_req.mode) {
Philipp Maier08177d32016-12-08 17:23:26 +01002082 LOGP(DLLAPD, LOGL_INFO, "perform local release (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002083 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002084 /* stop Timer T200 */
2085 lapd_stop_t200(dl);
2086 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002087 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2088 /* flush buffers */
2089 lapd_dl_flush_tx(dl);
2090 lapd_dl_flush_send(dl);
2091 /* send notification to L3 */
2092 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2093 }
2094
2095 /* in case we are already disconnecting */
2096 if (dl->state == LAPD_STATE_DISC_SENT)
2097 return -EBUSY;
2098
2099 /* flush tx_hist */
2100 lapd_dl_flush_hist(dl);
2101
Philipp Maier08177d32016-12-08 17:23:26 +01002102 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002103
2104 /* Push LAPD header on msgb */
2105 /* assemble message */
2106 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2107 /* keep nctx.ldp */
2108 /* keep nctx.sapi */
2109 /* keep nctx.tei */
2110 nctx.cr = dl->cr.loc2rem.cmd;
2111 nctx.format = LAPD_FORM_U;
2112 nctx.s_u = LAPD_U_DISC;
2113 nctx.p_f = 1;
2114 nctx.length = 0;
2115 nctx.more = 0;
2116
2117 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2118 msgb_put(dl->tx_hist[0].msg, msg->len);
2119 if (msg->len)
2120 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2121 dl->tx_hist[0].more = 0;
2122 /* set Vs to 0, because it is used as index when resending DISC */
2123 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002124
rootaf48bed2011-09-26 11:23:06 +02002125 /* Set states */
2126 dl->own_busy = dl->peer_busy = 0;
2127 dl->retrans_ctr = 0;
2128 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2129
2130 /* Tramsmit and start T200 */
2131 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002132 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002133
2134 return 0;
2135}
2136
2137/* request release of link in idle state */
2138static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2139 struct lapd_msg_ctx *lctx)
2140{
2141 struct lapd_datalink *dl = lctx->dl;
2142 struct msgb *msg = dp->oph.msg;
2143
2144 msgb_free(msg);
2145
2146 /* send notification to L3 */
2147 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2148}
2149
2150/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002151static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002152 uint32_t states;
2153 int prim, op;
2154 const char *name;
2155 int (*rout) (struct osmo_dlsap_prim *dp,
2156 struct lapd_msg_ctx *lctx);
2157} l2downstatelist[] = {
2158 /* create and send UI command */
2159 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002160 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002161 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2162
2163 /* create and send SABM command */
2164 {SBIT(LAPD_STATE_IDLE),
2165 PRIM_DL_EST, PRIM_OP_REQUEST,
2166 "DL-ESTABLISH-REQUEST", lapd_est_req},
2167
2168 /* create and send I command */
2169 {SBIT(LAPD_STATE_MF_EST) |
2170 SBIT(LAPD_STATE_TIMER_RECOV),
2171 PRIM_DL_DATA, PRIM_OP_REQUEST,
2172 "DL-DATA-REQUEST", lapd_data_req},
2173
2174 /* suspend datalink */
2175 {SBIT(LAPD_STATE_MF_EST) |
2176 SBIT(LAPD_STATE_TIMER_RECOV),
2177 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2178 "DL-SUSPEND-REQUEST", lapd_susp_req},
2179
2180 /* create and send SABM command (resume) */
2181 {SBIT(LAPD_STATE_MF_EST) |
2182 SBIT(LAPD_STATE_TIMER_RECOV),
2183 PRIM_DL_RES, PRIM_OP_REQUEST,
2184 "DL-RESUME-REQUEST", lapd_res_req},
2185
2186 /* create and send SABM command (reconnect) */
2187 {SBIT(LAPD_STATE_IDLE) |
2188 SBIT(LAPD_STATE_MF_EST) |
2189 SBIT(LAPD_STATE_TIMER_RECOV),
2190 PRIM_DL_RECON, PRIM_OP_REQUEST,
2191 "DL-RECONNECT-REQUEST", lapd_res_req},
2192
2193 /* create and send DISC command */
2194 {SBIT(LAPD_STATE_SABM_SENT) |
2195 SBIT(LAPD_STATE_MF_EST) |
2196 SBIT(LAPD_STATE_TIMER_RECOV) |
2197 SBIT(LAPD_STATE_DISC_SENT),
2198 PRIM_DL_REL, PRIM_OP_REQUEST,
2199 "DL-RELEASE-REQUEST", lapd_rel_req},
2200
2201 /* release in idle state */
2202 {SBIT(LAPD_STATE_IDLE),
2203 PRIM_DL_REL, PRIM_OP_REQUEST,
2204 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2205};
2206
2207#define L2DOWNSLLEN \
2208 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2209
2210int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2211{
2212 struct lapd_datalink *dl = lctx->dl;
2213 int i, supported = 0;
2214 struct msgb *msg = dp->oph.msg;
2215 int rc;
2216
2217 /* find function for current state and message */
2218 for (i = 0; i < L2DOWNSLLEN; i++) {
2219 if (dp->oph.primitive == l2downstatelist[i].prim
2220 && dp->oph.operation == l2downstatelist[i].op) {
2221 supported = 1;
2222 if ((SBIT(dl->state) & l2downstatelist[i].states))
2223 break;
2224 }
2225 }
2226 if (!supported) {
Philipp Maier08177d32016-12-08 17:23:26 +01002227 LOGP(DLLAPD, LOGL_NOTICE,
2228 "Message %u/%u unsupported. (dl=%p)\n", dp->oph.primitive,
2229 dp->oph.operation, dl);
rootaf48bed2011-09-26 11:23:06 +02002230 msgb_free(msg);
2231 return 0;
2232 }
2233 if (i == L2DOWNSLLEN) {
2234 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
Philipp Maier08177d32016-12-08 17:23:26 +01002235 "state %s. (dl=%p)\n", dp->oph.primitive,
Harald Weltec733d142017-03-15 10:20:51 +01002236 dp->oph.operation, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002237 msgb_free(msg);
2238 return 0;
2239 }
2240
Philipp Maier08177d32016-12-08 17:23:26 +01002241 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01002242 l2downstatelist[i].name, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002243
2244 rc = l2downstatelist[i].rout(dp, lctx);
2245
2246 return rc;
2247}
2248
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002249/*! @} */