blob: 166bf9a74264f1468e686bfb31f6ea8f11c3db27 [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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25/*! \addtogroup lapd
26 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 *
28 * Osmocom LAPD core, used for Q.921, LAPDm and others.
29 *
rootaf48bed2011-09-26 11:23:06 +020030 * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
31 *
32 * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
33 * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
34 * received while there is an incomplete rcv_buffer, it is appended to it.
35 *
36 * TX data is stored in the send_queue first. When transmitting a frame,
37 * the first message in the send_queue is moved to the send_buffer. There it
38 * resides until all fragments are acknowledged. Fragments to be sent by I
39 * frames are stored in the tx_hist buffer for resend, if required. Also the
40 * current fragment is copied into the tx_queue. There it resides until it is
41 * forwarded to layer 1.
42 *
43 * In case we have SAPI 0, we only have a window size of 1, so the unack-
44 * nowledged message resides always in the send_buffer. In case of a suspend,
45 * it can be written back to the first position of the send_queue.
46 *
47 * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
48 * asynchronous transfer between layer 1 and layer 2 (serial link), we must
49 * send a frame before layer 1 reaches the right timeslot to send it. So we
50 * move the tx_queue to layer 1 when there is not already a pending frame, and
51 * wait until acknowledge after the frame has been sent. If we receive an
52 * acknowledge, we can send the next frame from the buffer, if any.
53 *
54 * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
55 * will trigger next I frame, if possible.
56 *
57 * T203 is optional. It will be stated when entering MF EST state. It will also
58 * be started when I or S frame is received in that state . It will be
59 * restarted in the lapd_acknowledge() function, in case outstanding frames
60 * will not trigger T200. It will be stoped, when T200 is started in MF EST
61 * state. It will also be stoped when leaving MF EST state.
62 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020063 * \file lapd_core.c
rootaf48bed2011-09-26 11:23:06 +020064 */
65
66/* Enable this to test content resolution on network side:
67 * - The first SABM is received, UA is dropped.
68 * - The phone repeats SABM, but it's content is wrong, so it is ignored
69 * - The phone repeats SABM again, content is right, so UA is sent.
70 */
71//#define TEST_CONTENT_RESOLUTION_NETWORK
72
73#include <stdio.h>
74#include <stdint.h>
75#include <string.h>
76#include <errno.h>
rootaf48bed2011-09-26 11:23:06 +020077
78#include <osmocom/core/logging.h>
79#include <osmocom/core/timer.h>
80#include <osmocom/core/msgb.h>
81#include <osmocom/core/utils.h>
82#include <osmocom/core/talloc.h>
83#include <osmocom/gsm/lapd_core.h>
Max2f0b0c92017-01-12 16:47:13 +010084#include <osmocom/gsm/rsl.h>
rootaf48bed2011-09-26 11:23:06 +020085
86/* TS 04.06 Table 4 / Section 3.8.1 */
87#define LAPD_U_SABM 0x7
88#define LAPD_U_SABME 0xf
89#define LAPD_U_DM 0x3
90#define LAPD_U_UI 0x0
91#define LAPD_U_DISC 0x8
92#define LAPD_U_UA 0xC
93#define LAPD_U_FRMR 0x11
94
95#define LAPD_S_RR 0x0
96#define LAPD_S_RNR 0x1
97#define LAPD_S_REJ 0x2
98
99#define CR_USER2NET_CMD 0
100#define CR_USER2NET_RESP 1
101#define CR_NET2USER_CMD 1
102#define CR_NET2USER_RESP 0
103
104#define LAPD_HEADROOM 56
105
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;
121 return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name);
122}
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;
Philipp Maier08177d32016-12-08 17:23:26 +0100204 LOGP(DLLAPD, LOGL_INFO, "start T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200205 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
206}
207
208static void lapd_start_t203(struct lapd_datalink *dl)
209{
210 if (osmo_timer_pending(&dl->t203))
211 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100212 LOGP(DLLAPD, LOGL_INFO, "start T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200213 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
214}
215
216static void lapd_stop_t200(struct lapd_datalink *dl)
217{
218 if (!osmo_timer_pending(&dl->t200))
219 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100220 LOGP(DLLAPD, LOGL_INFO, "stop T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200221 osmo_timer_del(&dl->t200);
222}
223
224static void lapd_stop_t203(struct lapd_datalink *dl)
225{
226 if (!osmo_timer_pending(&dl->t203))
227 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100228 LOGP(DLLAPD, LOGL_INFO, "stop T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200229 osmo_timer_del(&dl->t203);
230}
231
rootaf48bed2011-09-26 11:23:06 +0200232static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
233{
Philipp Maier08177d32016-12-08 17:23:26 +0100234 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100235 lapd_state_name(dl->state), lapd_state_name(state), dl);
rootaf48bed2011-09-26 11:23:06 +0200236
237 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
238 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200239 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200240 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200241 msgb_free(dl->cont_res);
242 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200243 }
244
245 /* start T203 on entering MF EST state, if enabled */
246 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200247 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
248 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200249
250 dl->state = state;
251}
252
rootaf48bed2011-09-26 11:23:06 +0200253static void *tall_lapd_ctx = NULL;
254
255/* init datalink instance and allocate history */
256void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
257 int maxf)
258{
259 int m;
260
261 memset(dl, 0, sizeof(*dl));
262 INIT_LLIST_HEAD(&dl->send_queue);
263 INIT_LLIST_HEAD(&dl->tx_queue);
264 dl->reestablish = 1;
265 dl->n200_est_rel = 3;
266 dl->n200 = 3;
267 dl->t200_sec = 1;
268 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200269 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200270 dl->t203_sec = 10;
271 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200272 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200273 dl->maxf = maxf;
274 if (k > v_range - 1)
275 k = v_range - 1;
276 dl->k = k;
277 dl->v_range = v_range;
278
279 /* Calculate modulo for history array:
280 * - The history range must be at least k+1.
281 * - The history range must be 2^x, where x is as low as possible.
282 */
283 k++;
284 for (m = 0x80; m; m >>= 1) {
285 if ((m & k)) {
286 if (k > m)
287 m <<= 1;
288 dl->range_hist = m;
289 break;
290 }
291 }
292
293 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
Philipp Maier08177d32016-12-08 17:23:26 +0100294 "history range = %d (dl=%p)\n", dl->v_range, dl->k,
295 dl->range_hist, dl);
rootaf48bed2011-09-26 11:23:06 +0200296
297 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
298
299 if (!tall_lapd_ctx)
300 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100301 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
302 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200303}
304
305/* reset to IDLE state */
306void lapd_dl_reset(struct lapd_datalink *dl)
307{
rootaf48bed2011-09-26 11:23:06 +0200308 /* flush buffer */
309 lapd_dl_flush_tx(dl);
310 lapd_dl_flush_send(dl);
311 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200312 msgb_free(dl->rcv_buffer);
313 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200314 /* stop Timers */
315 lapd_stop_t200(dl);
316 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500317 if (dl->state == LAPD_STATE_IDLE)
318 return;
319 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance (dl=%p)\n", dl);
320 /* enter idle state (and remove eventual cont_res) */
321 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200322}
323
324/* reset and de-allocate history buffer */
325void lapd_dl_exit(struct lapd_datalink *dl)
326{
327 /* free all ressources except history buffer */
328 lapd_dl_reset(dl);
329 /* free history buffer list */
330 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200331 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200332}
333
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200334/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200335int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
336{
337 switch (mode) {
338 case LAPD_MODE_USER:
339 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
340 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
341 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
342 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
343 break;
344 case LAPD_MODE_NETWORK:
345 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
346 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
347 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
348 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
349 break;
350 default:
351 return -EINVAL;
352 }
353 dl->mode = mode;
354
355 return 0;
356}
357
358/* send DL message with optional msgb */
359static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
360 struct msgb *msg)
361{
362 struct lapd_datalink *dl = lctx->dl;
363 struct osmo_dlsap_prim dp;
364
365 osmo_prim_init(&dp.oph, 0, prim, op, msg);
366 return dl->send_dlsap(&dp, lctx);
367}
368
369/* send simple DL message */
370static inline int send_dl_simple(uint8_t prim, uint8_t op,
371 struct lapd_msg_ctx *lctx)
372{
373 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
374
375 return send_dl_l3(prim, op, lctx, msg);
376}
377
378/* send MDL-ERROR INDICATION */
379static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
380{
381 struct lapd_datalink *dl = lctx->dl;
382 struct osmo_dlsap_prim dp;
383
Philipp Maier08177d32016-12-08 17:23:26 +0100384 LOGP(DLLAPD, LOGL_NOTICE,
385 "sending MDL-ERROR-IND cause %d from state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100386 cause, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200387 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
388 dp.u.error_ind.cause = cause;
389 return dl->send_dlsap(&dp, lctx);
390}
391
392/* send UA response */
393static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
394{
395 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
396 struct lapd_msg_ctx nctx;
397 struct lapd_datalink *dl = lctx->dl;
398
399 memcpy(&nctx, lctx, sizeof(nctx));
400 msg->l3h = msgb_put(msg, len);
401 if (len)
402 memcpy(msg->l3h, data, len);
403 /* keep nctx.ldp */
404 /* keep nctx.sapi */
405 /* keep nctx.tei */
406 nctx.cr = dl->cr.loc2rem.resp;
407 nctx.format = LAPD_FORM_U;
408 nctx.s_u = LAPD_U_UA;
409 /* keep nctx.p_f */
410 nctx.length = len;
411 nctx.more = 0;
412
413 return dl->send_ph_data_req(&nctx, msg);
414}
415
416/* send DM response */
417static int lapd_send_dm(struct lapd_msg_ctx *lctx)
418{
419 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
420 struct lapd_msg_ctx nctx;
421 struct lapd_datalink *dl = lctx->dl;
422
423 memcpy(&nctx, lctx, sizeof(nctx));
424 /* keep nctx.ldp */
425 /* keep nctx.sapi */
426 /* keep nctx.tei */
427 nctx.cr = dl->cr.loc2rem.resp;
428 nctx.format = LAPD_FORM_U;
429 nctx.s_u = LAPD_U_DM;
430 /* keep nctx.p_f */
431 nctx.length = 0;
432 nctx.more = 0;
433
434 return dl->send_ph_data_req(&nctx, msg);
435}
436
437/* send RR response / command */
438static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
439{
440 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
441 struct lapd_msg_ctx nctx;
442 struct lapd_datalink *dl = lctx->dl;
443
444 memcpy(&nctx, lctx, sizeof(nctx));
445 /* keep nctx.ldp */
446 /* keep nctx.sapi */
447 /* keep nctx.tei */
448 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
449 nctx.format = LAPD_FORM_S;
450 nctx.s_u = LAPD_S_RR;
451 nctx.p_f = f_bit;
452 nctx.n_recv = dl->v_recv;
453 nctx.length = 0;
454 nctx.more = 0;
455
456 return dl->send_ph_data_req(&nctx, msg);
457}
458
459/* send RNR response / command */
460static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
461{
462 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
463 struct lapd_msg_ctx nctx;
464 struct lapd_datalink *dl = lctx->dl;
465
466 memcpy(&nctx, lctx, sizeof(nctx));
467 /* keep nctx.ldp */
468 /* keep nctx.sapi */
469 /* keep nctx.tei */
470 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
471 nctx.format = LAPD_FORM_S;
472 nctx.s_u = LAPD_S_RNR;
473 nctx.p_f = f_bit;
474 nctx.n_recv = dl->v_recv;
475 nctx.length = 0;
476 nctx.more = 0;
477
478 return dl->send_ph_data_req(&nctx, msg);
479}
480
481/* send REJ response */
482static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
483{
484 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
485 struct lapd_msg_ctx nctx;
486 struct lapd_datalink *dl = lctx->dl;
487
488 memcpy(&nctx, lctx, sizeof(nctx));
489 /* keep nctx.ldp */
490 /* keep nctx.sapi */
491 /* keep nctx.tei */
492 nctx.cr = dl->cr.loc2rem.resp;
493 nctx.format = LAPD_FORM_S;
494 nctx.s_u = LAPD_S_REJ;
495 nctx.p_f = f_bit;
496 nctx.n_recv = dl->v_recv;
497 nctx.length = 0;
498 nctx.more = 0;
499
500 return dl->send_ph_data_req(&nctx, msg);
501}
502
503/* resend SABM or DISC message */
504static int lapd_send_resend(struct lapd_datalink *dl)
505{
506 struct msgb *msg;
507 uint8_t h = do_mod(dl->v_send, dl->range_hist);
508 int length = dl->tx_hist[h].msg->len;
509 struct lapd_msg_ctx nctx;
510
511 /* assemble message */
512 memcpy(&nctx, &dl->lctx, sizeof(nctx));
513 /* keep nctx.ldp */
514 /* keep nctx.sapi */
515 /* keep nctx.tei */
516 nctx.cr = dl->cr.loc2rem.cmd;
517 nctx.format = LAPD_FORM_U;
518 if (dl->state == LAPD_STATE_SABM_SENT)
519 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
520 else
521 nctx.s_u = LAPD_U_DISC;
522 nctx.p_f = 1;
523 nctx.length = length;
524 nctx.more = 0;
525
526 /* Resend SABM/DISC from tx_hist */
527 msg = lapd_msgb_alloc(length, "LAPD resend");
528 msg->l3h = msgb_put(msg, length);
529 if (length)
530 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
531
532 return dl->send_ph_data_req(&nctx, msg);
533}
534
535/* reestablish link */
536static int lapd_reestablish(struct lapd_datalink *dl)
537{
538 struct osmo_dlsap_prim dp;
539 struct msgb *msg;
540
Philipp Maier08177d32016-12-08 17:23:26 +0100541 LOGP(DLLAPD, LOGL_DEBUG, "lapd reestablish (dl=%p)\n", dl);
542
rootaf48bed2011-09-26 11:23:06 +0200543 msg = lapd_msgb_alloc(0, "DUMMY");
544 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
545
546 return lapd_est_req(&dp, &dl->lctx);
547}
548
549/* Timer callback on T200 expiry */
550static void lapd_t200_cb(void *data)
551{
552 struct lapd_datalink *dl = data;
553
Philipp Maier08177d32016-12-08 17:23:26 +0100554 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100555 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200556
557 switch (dl->state) {
558 case LAPD_STATE_SABM_SENT:
559 /* 5.4.1.3 */
560 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200561 /* flush tx and send buffers */
562 lapd_dl_flush_tx(dl);
563 lapd_dl_flush_send(dl);
564 /* go back to idle state */
565 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
566 /* NOTE: we must not change any other states or buffers
567 * and queues, since we may reconnect after handover
568 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100569 /* send MDL ERROR INIDCATION to L3 */
570 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100571 /* send RELEASE INDICATION to L3 */
572 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
573 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200574 break;
575 }
576 /* retransmit SABM command */
577 lapd_send_resend(dl);
578 /* increment re-transmission counter */
579 dl->retrans_ctr++;
580 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200581 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200582 break;
583 case LAPD_STATE_DISC_SENT:
584 /* 5.4.4.3 */
585 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200586 /* send MDL ERROR INIDCATION to L3 */
587 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maier6b986c22017-02-01 12:00:45 +0100588 /* send RELEASE INDICATION to L3 */
589 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200590 /* flush tx and send buffers */
591 lapd_dl_flush_tx(dl);
592 lapd_dl_flush_send(dl);
593 /* go back to idle state */
594 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
595 /* NOTE: we must not change any other states or buffers
596 * and queues, since we may reconnect after handover
597 * failure. the buffered messages is replaced there */
598 break;
599 }
600 /* retransmit DISC command */
601 lapd_send_resend(dl);
602 /* increment re-transmission counter */
603 dl->retrans_ctr++;
604 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200605 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200606 break;
607 case LAPD_STATE_MF_EST:
608 /* 5.5.7 */
609 dl->retrans_ctr = 0;
610 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
611 /* fall through */
612 case LAPD_STATE_TIMER_RECOV:
613 dl->retrans_ctr++;
614 if (dl->retrans_ctr < dl->n200) {
615 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
616 uint8_t h = do_mod(vs, dl->range_hist);
617 /* retransmit I frame (V_s-1) with P=1, if any */
618 if (dl->tx_hist[h].msg) {
619 struct msgb *msg;
620 int length = dl->tx_hist[h].msg->len;
621 struct lapd_msg_ctx nctx;
622
623 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
Philipp Maier08177d32016-12-08 17:23:26 +0100624 " V(S)=%d (dl=%p)\n", vs, dl);
rootaf48bed2011-09-26 11:23:06 +0200625 /* Create I frame (segment) from tx_hist */
626 memcpy(&nctx, &dl->lctx, sizeof(nctx));
627 /* keep nctx.ldp */
628 /* keep nctx.sapi */
629 /* keep nctx.tei */
630 nctx.cr = dl->cr.loc2rem.cmd;
631 nctx.format = LAPD_FORM_I;
632 nctx.p_f = 1;
633 nctx.n_send = vs;
634 nctx.n_recv = dl->v_recv;
635 nctx.length = length;
636 nctx.more = dl->tx_hist[h].more;
637 msg = lapd_msgb_alloc(length, "LAPD I resend");
638 msg->l3h = msgb_put(msg, length);
639 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
640 length);
641 dl->send_ph_data_req(&nctx, msg);
642 } else {
643 /* OR send appropriate supervision frame with P=1 */
644 if (!dl->own_busy && !dl->seq_err_cond) {
645 lapd_send_rr(&dl->lctx, 1, 1);
646 /* NOTE: In case of sequence error
647 * condition, the REJ frame has been
648 * transmitted when entering the
649 * condition, so it has not be done
650 * here
651 */
652 } else if (dl->own_busy) {
653 lapd_send_rnr(&dl->lctx, 1, 1);
654 } else {
655 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
Philipp Maier08177d32016-12-08 17:23:26 +0100656 "pls. fix (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200657 }
658 }
659 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200660 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200661 } else {
662 /* send MDL ERROR INIDCATION to L3 */
663 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
664 /* reestablish */
665 if (!dl->reestablish)
666 break;
667 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100668 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200669 lapd_reestablish(dl);
670 }
671 break;
672 default:
673 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
Harald Weltec733d142017-03-15 10:20:51 +0100674 "dl->state %s (dl=%p)\n", lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200675 }
676}
677
678/* Timer callback on T203 expiry */
679static void lapd_t203_cb(void *data)
680{
681 struct lapd_datalink *dl = data;
682
Philipp Maier08177d32016-12-08 17:23:26 +0100683 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100684 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200685
686 if (dl->state != LAPD_STATE_MF_EST) {
687 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
Philipp Maier08177d32016-12-08 17:23:26 +0100688 "please fix! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200689 return;
690 }
691
692 /* set retransmission counter to 0 */
693 dl->retrans_ctr = 0;
694 /* enter timer recovery state */
695 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
696 /* transmit a supervisory command with P bit set to 1 as follows: */
697 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +0100698 LOGP(DLLAPD, LOGL_INFO,
699 "transmit an RR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200700 /* Send RR with P=1 */
701 lapd_send_rr(&dl->lctx, 1, 1);
702 } else {
Philipp Maier08177d32016-12-08 17:23:26 +0100703 LOGP(DLLAPD, LOGL_INFO,
704 "transmit an RNR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200705 /* Send RNR with P=1 */
706 lapd_send_rnr(&dl->lctx, 1, 1);
707 }
708 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200709 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200710}
711
712/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
713static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
714{
715 struct lapd_datalink *dl = lctx->dl;
716 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100717 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200718 int i, h;
719
720 /* supervisory frame ? */
721 if (lctx->format == LAPD_FORM_S)
722 s = 1;
723 /* REJ frame ? */
724 if (s && lctx->s_u == LAPD_S_REJ)
725 rej = 1;
726
727 /* Flush all transmit buffers of acknowledged frames */
728 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
729 h = do_mod(i, dl->range_hist);
730 if (dl->tx_hist[h].msg) {
731 msgb_free(dl->tx_hist[h].msg);
732 dl->tx_hist[h].msg = NULL;
733 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
734 }
735 }
736
737 if (dl->state != LAPD_STATE_TIMER_RECOV) {
738 /* When not in the timer recovery condition, the data
739 * link layer entity shall reset the timer T200 on
740 * receipt of a valid I frame with N(R) higher than V(A),
741 * or an REJ with an N(R) equal to V(A). */
742 if ((!rej && nr != dl->v_ack)
743 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200744 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200745 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200746 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
747 }
748 /* 5.7.4: N(R) sequence error
749 * N(R) is called valid, if and only if
750 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
751 */
752 if (sub_mod(nr, dl->v_ack, dl->v_range)
753 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Philipp Maier08177d32016-12-08 17:23:26 +0100754 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200755 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
756 }
757 }
758
759 /* V(A) shall be set to the value of N(R) */
760 dl->v_ack = nr;
761
Andreas Eversberg742fc792011-09-27 09:40:25 +0200762 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200763 * and if there are outstanding I frames, restart T200 */
764 if (t200_reset && !rej) {
765 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
766 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
Philipp Maier08177d32016-12-08 17:23:26 +0100767 "frame(s) (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200768 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200769 }
770 }
771
772 /* This also does a restart, when I or S frame is received */
773
774 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200775 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200776 /* Start T203, if T200 is not running in MF EST state, if enabled */
777 if (!osmo_timer_pending(&dl->t200)
778 && (dl->t203_sec || dl->t203_usec)
779 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200780 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200781 }
782}
783
784/* L1 -> L2 */
785
786/* Receive a LAPD U (Unnumbered) message from L1 */
787static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
788{
789 struct lapd_datalink *dl = lctx->dl;
790 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100791 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200792 uint8_t prim, op;
793
794 switch (lctx->s_u) {
795 case LAPD_U_SABM:
796 case LAPD_U_SABME:
797 prim = PRIM_DL_EST;
798 op = PRIM_OP_INDICATION;
799
Philipp Maier08177d32016-12-08 17:23:26 +0100800 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100801 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200802 /* 5.7.1 */
803 dl->seq_err_cond = 0;
804 /* G.2.2 Wrong value of the C/R bit */
805 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +0100806 LOGP(DLLAPD, LOGL_ERROR,
807 "SABM response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200808 msgb_free(msg);
809 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
810 return -EINVAL;
811 }
812
813 /* G.4.5 If SABM is received with L>N201 or with M bit
814 * set, AN MDL-ERROR-INDICATION is sent to MM.
815 */
816 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +0100817 LOGP(DLLAPD, LOGL_ERROR,
818 "SABM too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200819 msgb_free(msg);
820 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
821 return -EIO;
822 }
823
824 switch (dl->state) {
825 case LAPD_STATE_IDLE:
826 break;
827 case LAPD_STATE_MF_EST:
828 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
Philipp Maier08177d32016-12-08 17:23:26 +0100829 "frame established state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200830 /* If link is lost on the remote side, we start over
831 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100832 /* Additionally, continue in case of content resoltion
833 * (GSM network). This happens, if the mobile has not
834 * yet received UA or another mobile (collision) tries
835 * to establish connection. The mobile must receive
836 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200837 /* 5.4.2.1 */
838 if (!length) {
839 /* If no content resolution, this is a
840 * re-establishment. */
841 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +0100842 "Remote reestablish (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200843 break;
844 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200845 if (!dl->cont_res) {
846 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Philipp Maier08177d32016-12-08 17:23:26 +0100847 "allowed in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100848 lapd_state_name(dl->state), dl);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200849 mdl_error(MDL_CAUSE_SABM_MF, lctx);
850 msgb_free(msg);
851 return 0;
852 }
rootaf48bed2011-09-26 11:23:06 +0200853 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200854 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200855#ifdef TEST_CONTENT_RESOLUTION_NETWORK
856 dl->cont_res->data[0] ^= 0x01;
857#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100858 if (memcmp(dl->cont_res->data, msg->data,
859 length)) {
rootaf48bed2011-09-26 11:23:06 +0200860 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100861 "with different content - "
Philipp Maier08177d32016-12-08 17:23:26 +0100862 "ignoring! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200863 msgb_free(msg);
864 return 0;
865 }
866 }
867 /* send UA again */
868 lapd_send_ua(lctx, length, msg->l3h);
869 msgb_free(msg);
870 return 0;
871 case LAPD_STATE_DISC_SENT:
872 /* 5.4.6.2 send DM with F=P */
873 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200874 /* stop Timer T200 */
875 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200876 msgb_free(msg);
877 return send_dl_simple(prim, op, lctx);
878 default:
879 /* collision: Send UA, but still wait for rx UA, then
880 * change to MF_EST state.
881 */
882 /* check for contention resoultion */
883 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
884 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Philipp Maier08177d32016-12-08 17:23:26 +0100885 "during contention resolution (state=%s, dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100886 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200887 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
888 }
889 lapd_send_ua(lctx, length, msg->l3h);
890 msgb_free(msg);
891 return 0;
892 }
893 /* save message context for further use */
894 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
895#ifndef TEST_CONTENT_RESOLUTION_NETWORK
896 /* send UA response */
897 lapd_send_ua(lctx, length, msg->l3h);
898#endif
899 /* set Vs, Vr and Va to 0 */
900 dl->v_send = dl->v_recv = dl->v_ack = 0;
901 /* clear tx_hist */
902 lapd_dl_flush_hist(dl);
903 /* enter multiple-frame-established state */
904 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
905 /* store content resolution data on network side
906 * Note: cont_res will be removed when changing state again,
907 * so it must be allocated AFTER lapd_dl_newstate(). */
908 if (dl->mode == LAPD_MODE_NETWORK && length) {
909 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
910 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
911 length);
Philipp Maier08177d32016-12-08 17:23:26 +0100912 LOGP(DLLAPD, LOGL_NOTICE,
913 "Store content res. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200914 }
915 /* send notification to L3 */
916 if (length == 0) {
917 /* 5.4.1.2 Normal establishment procedures */
918 rc = send_dl_simple(prim, op, lctx);
919 msgb_free(msg);
920 } else {
921 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200922 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200923 rc = send_dl_l3(prim, op, lctx, msg);
924 }
925 break;
926 case LAPD_U_DM:
Philipp Maier08177d32016-12-08 17:23:26 +0100927 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100928 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200929 /* G.2.2 Wrong value of the C/R bit */
930 if (lctx->cr == dl->cr.rem2loc.cmd) {
Philipp Maier08177d32016-12-08 17:23:26 +0100931 LOGP(DLLAPD, LOGL_ERROR,
932 "DM command error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200933 msgb_free(msg);
934 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
935 return -EINVAL;
936 }
937 if (!lctx->p_f) {
938 /* 5.4.1.2 DM responses with the F bit set to "0"
939 * shall be ignored.
940 */
941 msgb_free(msg);
942 return 0;
943 }
944 switch (dl->state) {
945 case LAPD_STATE_SABM_SENT:
946 break;
947 case LAPD_STATE_MF_EST:
948 if (lctx->p_f) {
949 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
Philipp Maier08177d32016-12-08 17:23:26 +0100950 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200951 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
952 } else {
953 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
954 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100955 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200956 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
957 /* reestablish */
958 if (!dl->reestablish) {
959 msgb_free(msg);
960 return 0;
961 }
962 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100963 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200964 lapd_reestablish(dl);
965 }
966 msgb_free(msg);
967 return 0;
968 case LAPD_STATE_TIMER_RECOV:
969 /* FP = 0 (DM is normal in case PF = 1) */
970 if (!lctx->p_f) {
971 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
972 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100973 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200974 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
975 msgb_free(msg);
976 /* reestablish */
977 if (!dl->reestablish)
978 return 0;
979 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100980 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200981 return lapd_reestablish(dl);
982 }
983 break;
984 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200985 /* stop Timer T200 */
986 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200987 /* go to idle state */
988 lapd_dl_flush_tx(dl);
989 lapd_dl_flush_send(dl);
990 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
991 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
992 msgb_free(msg);
993 return 0;
994 case LAPD_STATE_IDLE:
995 /* 5.4.5 all other frame types shall be discarded */
996 default:
997 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
Philipp Maier08177d32016-12-08 17:23:26 +0100998 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200999 msgb_free(msg);
1000 return 0;
1001 }
Andreas Eversberg742fc792011-09-27 09:40:25 +02001002 /* stop timer T200 */
1003 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001004 /* go to idle state */
1005 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1006 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1007 msgb_free(msg);
1008 break;
1009 case LAPD_U_UI:
Philipp Maier08177d32016-12-08 17:23:26 +01001010 LOGP(DLLAPD, LOGL_INFO, "UI received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001011 /* G.2.2 Wrong value of the C/R bit */
1012 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001013 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
Philipp Maier08177d32016-12-08 17:23:26 +01001014 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001015 msgb_free(msg);
1016 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1017 return -EINVAL;
1018 }
1019
1020 /* G.4.5 If UI is received with L>N201 or with M bit
1021 * set, AN MDL-ERROR-INDICATION is sent to MM.
1022 */
1023 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001024 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
Philipp Maier08177d32016-12-08 17:23:26 +01001025 "(%d > N201(%d) or M=%d) (dl=%p)\n", length,
1026 lctx->n201, lctx->more, dl);
rootaf48bed2011-09-26 11:23:06 +02001027 msgb_free(msg);
1028 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1029 return -EIO;
1030 }
1031
1032 /* do some length checks */
1033 if (length == 0) {
1034 /* 5.3.3 UI frames received with the length indicator
1035 * set to "0" shall be ignored
1036 */
Philipp Maier08177d32016-12-08 17:23:26 +01001037 LOGP(DLLAPD, LOGL_INFO,
1038 "length=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001039 msgb_free(msg);
1040 return 0;
1041 }
Harald Welte087116a2013-06-18 21:41:34 +02001042 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001043 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1044 msg);
1045 break;
1046 case LAPD_U_DISC:
1047 prim = PRIM_DL_REL;
1048 op = PRIM_OP_INDICATION;
1049
Philipp Maier08177d32016-12-08 17:23:26 +01001050 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001051 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001052 /* flush tx and send buffers */
1053 lapd_dl_flush_tx(dl);
1054 lapd_dl_flush_send(dl);
1055 /* 5.7.1 */
1056 dl->seq_err_cond = 0;
1057 /* G.2.2 Wrong value of the C/R bit */
1058 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001059 LOGP(DLLAPD, LOGL_ERROR,
1060 "DISC response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001061 msgb_free(msg);
1062 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1063 return -EINVAL;
1064 }
1065 if (length > 0 || lctx->more) {
1066 /* G.4.4 If a DISC or DM frame is received with L>0 or
1067 * with the M bit set to "1", an MDL-ERROR-INDICATION
1068 * primitive with cause "U frame with incorrect
1069 * parameters" is sent to the mobile management entity.
1070 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001071 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001072 "U frame iwth incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001073 msgb_free(msg);
1074 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1075 return -EIO;
1076 }
1077 switch (dl->state) {
1078 case LAPD_STATE_IDLE:
Philipp Maier08177d32016-12-08 17:23:26 +01001079 LOGP(DLLAPD, LOGL_INFO,
1080 "DISC in idle state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001081 /* send DM with F=P */
1082 msgb_free(msg);
1083 return lapd_send_dm(lctx);
1084 case LAPD_STATE_SABM_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001085 LOGP(DLLAPD, LOGL_INFO,
1086 "DISC in SABM state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001087 /* 5.4.6.2 send DM with F=P */
1088 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001089 /* stop Timer T200 */
1090 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001091 /* go to idle state */
1092 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1093 msgb_free(msg);
1094 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1095 lctx);
1096 case LAPD_STATE_MF_EST:
1097 case LAPD_STATE_TIMER_RECOV:
Philipp Maier08177d32016-12-08 17:23:26 +01001098 LOGP(DLLAPD, LOGL_INFO,
1099 "DISC in est state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001100 break;
1101 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001102 LOGP(DLLAPD, LOGL_INFO,
1103 "DISC in disc state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001104 prim = PRIM_DL_REL;
1105 op = PRIM_OP_CONFIRM;
1106 break;
1107 default:
1108 lapd_send_ua(lctx, length, msg->l3h);
1109 msgb_free(msg);
1110 return 0;
1111 }
1112 /* send UA response */
1113 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001114 /* stop Timer T200 */
1115 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001116 /* enter idle state, keep tx-buffer with UA response */
1117 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1118 /* send notification to L3 */
1119 rc = send_dl_simple(prim, op, lctx);
1120 msgb_free(msg);
1121 break;
1122 case LAPD_U_UA:
Philipp Maier08177d32016-12-08 17:23:26 +01001123 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001124 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001125 /* G.2.2 Wrong value of the C/R bit */
1126 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001127 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
Philipp Maier08177d32016-12-08 17:23:26 +01001128 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001129 msgb_free(msg);
1130 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1131 return -EINVAL;
1132 }
1133
1134 /* G.4.5 If UA is received with L>N201 or with M bit
1135 * set, AN MDL-ERROR-INDICATION is sent to MM.
1136 */
1137 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001138 LOGP(DLLAPD, LOGL_ERROR,
1139 "UA too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001140 msgb_free(msg);
1141 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1142 return -EIO;
1143 }
1144
1145 if (!lctx->p_f) {
1146 /* 5.4.1.2 A UA response with the F bit set to "0"
1147 * shall be ignored.
1148 */
Philipp Maier08177d32016-12-08 17:23:26 +01001149 LOGP(DLLAPD, LOGL_INFO,
1150 "F=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001151 msgb_free(msg);
1152 return 0;
1153 }
1154 switch (dl->state) {
1155 case LAPD_STATE_SABM_SENT:
1156 break;
1157 case LAPD_STATE_MF_EST:
1158 case LAPD_STATE_TIMER_RECOV:
1159 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001160 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001161 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1162 msgb_free(msg);
1163 return 0;
1164 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001165 LOGP(DLLAPD, LOGL_INFO,
1166 "UA in disconnect state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001167 /* stop Timer T200 */
1168 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001169 /* go to idle state */
1170 lapd_dl_flush_tx(dl);
1171 lapd_dl_flush_send(dl);
1172 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1173 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1174 msgb_free(msg);
1175 return 0;
1176 case LAPD_STATE_IDLE:
1177 /* 5.4.5 all other frame types shall be discarded */
1178 default:
1179 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001180 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001181 msgb_free(msg);
1182 return 0;
1183 }
Philipp Maier08177d32016-12-08 17:23:26 +01001184 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001185 /* stop Timer T200 */
1186 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001187 /* compare UA with SABME if contention resolution is applied */
1188 if (dl->tx_hist[0].msg->len) {
1189 if (length != (dl->tx_hist[0].msg->len)
1190 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1191 length)) {
1192 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
Philipp Maier08177d32016-12-08 17:23:26 +01001193 "mismatches **** (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001194 rc = send_dl_simple(PRIM_DL_REL,
1195 PRIM_OP_INDICATION, lctx);
1196 msgb_free(msg);
1197 /* go to idle state */
1198 lapd_dl_flush_tx(dl);
1199 lapd_dl_flush_send(dl);
1200 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1201 return 0;
1202 }
1203 }
1204 /* set Vs, Vr and Va to 0 */
1205 dl->v_send = dl->v_recv = dl->v_ack = 0;
1206 /* clear tx_hist */
1207 lapd_dl_flush_hist(dl);
1208 /* enter multiple-frame-established state */
1209 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1210 /* send outstanding frames, if any (resume / reconnect) */
1211 lapd_send_i(lctx, __LINE__);
1212 /* send notification to L3 */
1213 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1214 msgb_free(msg);
1215 break;
1216 case LAPD_U_FRMR:
Philipp Maier08177d32016-12-08 17:23:26 +01001217 LOGP(DLLAPD, LOGL_NOTICE,
1218 "Frame reject received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001219 /* send MDL ERROR INIDCATION to L3 */
1220 mdl_error(MDL_CAUSE_FRMR, lctx);
1221 msgb_free(msg);
1222 /* reestablish */
1223 if (!dl->reestablish)
1224 break;
Philipp Maier08177d32016-12-08 17:23:26 +01001225 LOGP(DLLAPD, LOGL_NOTICE,
1226 "Performing reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001227 rc = lapd_reestablish(dl);
1228 break;
1229 default:
1230 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001231 LOGP(DLLAPD, LOGL_NOTICE,
1232 "Unnumbered frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001233 msgb_free(msg);
1234 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1235 return -EINVAL;
1236 }
1237 return rc;
1238}
1239
1240/* Receive a LAPD S (Supervisory) message from L1 */
1241static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1242{
1243 struct lapd_datalink *dl = lctx->dl;
1244 int length = lctx->length;
1245
1246 if (length > 0 || lctx->more) {
1247 /* G.4.3 If a supervisory frame is received with L>0 or
1248 * with the M bit set to "1", an MDL-ERROR-INDICATION
1249 * primitive with cause "S frame with incorrect
1250 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001251 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001252 "S frame with incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001253 msgb_free(msg);
1254 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1255 return -EIO;
1256 }
1257
1258 if (lctx->cr == dl->cr.rem2loc.resp
1259 && lctx->p_f
1260 && dl->state != LAPD_STATE_TIMER_RECOV) {
1261 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001262 LOGP(DLLAPD, LOGL_NOTICE,
1263 "S frame response with F=1 error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001264 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1265 }
1266
1267 switch (dl->state) {
1268 case LAPD_STATE_IDLE:
1269 /* if P=1, respond DM with F=1 (5.2.2) */
1270 /* 5.4.5 all other frame types shall be discarded */
1271 if (lctx->p_f)
1272 lapd_send_dm(lctx); /* F=P */
1273 /* fall though */
1274 case LAPD_STATE_SABM_SENT:
1275 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001276 LOGP(DLLAPD, LOGL_NOTICE,
1277 "S frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001278 msgb_free(msg);
1279 return 0;
1280 }
1281 switch (lctx->s_u) {
1282 case LAPD_S_RR:
Philipp Maier08177d32016-12-08 17:23:26 +01001283 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001284 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001285 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1286 lapd_acknowledge(lctx);
1287
1288 /* 5.5.3.2 */
1289 if (lctx->cr == dl->cr.rem2loc.cmd
1290 && lctx->p_f) {
1291 if (!dl->own_busy && !dl->seq_err_cond) {
1292 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001293 "with polling bit set and we are not "
1294 "busy, so we reply with RR frame "
1295 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001296 lapd_send_rr(lctx, 1, 0);
1297 /* NOTE: In case of sequence error condition,
1298 * the REJ frame has been transmitted when
1299 * entering the condition, so it has not be
1300 * done here
1301 */
1302 } else if (dl->own_busy) {
1303 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001304 "with polling bit set and we are busy, "
1305 "so we reply with RR frame response (dl=%p)\n",
1306 dl);
rootaf48bed2011-09-26 11:23:06 +02001307 lapd_send_rnr(lctx, 1, 0);
1308 }
1309 } else if (lctx->cr == dl->cr.rem2loc.resp
1310 && lctx->p_f
1311 && dl->state == LAPD_STATE_TIMER_RECOV) {
1312 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1313 "and we are in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001314 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001315 /* V(S) to the N(R) in the RR frame */
1316 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001317 /* stop Timer T200 */
1318 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001319 /* 5.5.7 Clear timer recovery condition */
1320 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1321 }
1322 /* Send message, if possible due to acknowledged data */
1323 lapd_send_i(lctx, __LINE__);
1324
1325 break;
1326 case LAPD_S_RNR:
Philipp Maier08177d32016-12-08 17:23:26 +01001327 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001328 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001329 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1330 lapd_acknowledge(lctx);
1331
1332 /* 5.5.5 */
1333 /* Set peer receiver busy condition */
1334 dl->peer_busy = 1;
1335
1336 if (lctx->p_f) {
1337 if (lctx->cr == dl->cr.rem2loc.cmd) {
1338 if (!dl->own_busy) {
1339 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1340 "command and we are not busy, "
1341 "so we reply with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001342 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001343 /* Send RR with F=1 */
1344 lapd_send_rr(lctx, 1, 0);
1345 } else {
1346 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1347 "command and we are busy, so "
1348 "we reply with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001349 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001350 /* Send RNR with F=1 */
1351 lapd_send_rnr(lctx, 1, 0);
1352 }
1353 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1354 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1355 "and we in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001356 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001357 /* 5.5.7 Clear timer recovery condition */
1358 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1359 /* V(S) to the N(R) in the RNR frame */
1360 dl->v_send = lctx->n_recv;
1361 }
1362 } else
1363 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
Philipp Maier08177d32016-12-08 17:23:26 +01001364 "received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001365
1366 /* Send message, if possible due to acknowledged data */
1367 lapd_send_i(lctx, __LINE__);
1368
1369 break;
1370 case LAPD_S_REJ:
Philipp Maier08177d32016-12-08 17:23:26 +01001371 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001372 lapd_state_name(dl->state), dl);
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.4.1 */
1377 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1378 /* Clear an existing peer receiver busy condition */
1379 dl->peer_busy = 0;
1380 /* V(S) and V(A) to the N(R) in the REJ frame */
1381 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001382 /* stop Timer T200 */
1383 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001384 /* 5.5.3.2 */
1385 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1386 if (!dl->own_busy && !dl->seq_err_cond) {
1387 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1388 "command not in timer recovery "
1389 "state and not in own busy "
1390 "condition received, so we "
1391 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001392 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001393 lapd_send_rr(lctx, 1, 0);
1394 /* NOTE: In case of sequence error
1395 * condition, the REJ frame has been
1396 * transmitted when entering the
1397 * condition, so it has not be done
1398 * here
1399 */
1400 } else if (dl->own_busy) {
1401 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1402 "command not in timer recovery "
1403 "state and in own busy "
1404 "condition received, so we "
1405 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001406 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001407 lapd_send_rnr(lctx, 1, 0);
1408 }
1409 } else
1410 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1411 "polling command not in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001412 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001413 /* send MDL ERROR INIDCATION to L3 */
1414 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Philipp Maier08177d32016-12-08 17:23:26 +01001415 LOGP(DLLAPD, LOGL_ERROR,
1416 "unsolicited supervisory response! (dl=%p)\n",
1417 dl);
rootaf48bed2011-09-26 11:23:06 +02001418 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1419 }
1420
1421 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1422 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
Philipp Maier08177d32016-12-08 17:23:26 +01001423 "recovery state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001424 /* Clear an existing peer receiver busy condition */
1425 dl->peer_busy = 0;
1426 /* V(S) and V(A) to the N(R) in the REJ frame */
1427 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001428 /* stop Timer T200 */
1429 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001430 /* 5.5.7 Clear timer recovery condition */
1431 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1432 } else {
1433 /* Clear an existing peer receiver busy condition */
1434 dl->peer_busy = 0;
1435 /* V(S) and V(A) to the N(R) in the REJ frame */
1436 dl->v_send = dl->v_ack = lctx->n_recv;
1437 /* 5.5.3.2 */
1438 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1439 if (!dl->own_busy && !dl->seq_err_cond) {
1440 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1441 "command in timer recovery "
1442 "state and not in own busy "
1443 "condition received, so we "
1444 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001445 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001446 lapd_send_rr(lctx, 1, 0);
1447 /* NOTE: In case of sequence error
1448 * condition, the REJ frame has been
1449 * transmitted when entering the
1450 * condition, so it has not be done
1451 * here
1452 */
1453 } else if (dl->own_busy) {
1454 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1455 "command in timer recovery "
1456 "state and in own busy "
1457 "condition received, so we "
1458 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001459 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001460 lapd_send_rnr(lctx, 1, 0);
1461 }
1462 } else
1463 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1464 "polling command in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001465 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001466 }
1467
1468 /* FIXME: 5.5.4.2 2) */
1469
1470 /* Send message, if possible due to acknowledged data */
1471 lapd_send_i(lctx, __LINE__);
1472
1473 break;
1474 default:
1475 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001476 LOGP(DLLAPD, LOGL_ERROR,
1477 "Supervisory frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001478 msgb_free(msg);
1479 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1480 return -EINVAL;
1481 }
1482 msgb_free(msg);
1483 return 0;
1484}
1485
1486/* Receive a LAPD I (Information) message from L1 */
1487static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1488{
1489 struct lapd_datalink *dl = lctx->dl;
1490 //uint8_t nr = lctx->n_recv;
1491 uint8_t ns = lctx->n_send;
1492 int length = lctx->length;
1493 int rc;
1494
Philipp Maier08177d32016-12-08 17:23:26 +01001495 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u) (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001496 lapd_state_name(dl->state), lctx->sapi, dl);
rootaf48bed2011-09-26 11:23:06 +02001497
1498 /* G.2.2 Wrong value of the C/R bit */
1499 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001500 LOGP(DLLAPD, LOGL_ERROR,
1501 "I frame response not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001502 msgb_free(msg);
1503 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1504 return -EINVAL;
1505 }
1506
1507 if (length == 0 || length > lctx->n201) {
1508 /* G.4.2 If the length indicator of an I frame is set
1509 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1510 * primitive with cause "I frame with incorrect length"
1511 * is sent to the mobile management entity. */
Philipp Maier08177d32016-12-08 17:23:26 +01001512 LOGP(DLLAPD, LOGL_ERROR,
1513 "I frame length not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001514 msgb_free(msg);
1515 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1516 return -EIO;
1517 }
1518
1519 /* G.4.2 If the numerical value of L is L<N201 and the M
1520 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1521 * cause "I frame with incorrect use of M bit" is sent to the
1522 * mobile management entity. */
1523 if (lctx->more && length < lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001524 LOGP(DLLAPD, LOGL_ERROR,
1525 "I frame with M bit too short (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001526 msgb_free(msg);
1527 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1528 return -EIO;
1529 }
1530
1531 switch (dl->state) {
1532 case LAPD_STATE_IDLE:
1533 /* if P=1, respond DM with F=1 (5.2.2) */
1534 /* 5.4.5 all other frame types shall be discarded */
1535 if (lctx->p_f)
1536 lapd_send_dm(lctx); /* F=P */
1537 /* fall though */
1538 case LAPD_STATE_SABM_SENT:
1539 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001540 LOGP(DLLAPD, LOGL_NOTICE,
1541 "I frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001542 msgb_free(msg);
1543 return 0;
1544 }
1545
1546 /* 5.7.1: N(s) sequence error */
1547 if (ns != dl->v_recv) {
1548 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
Philipp Maier08177d32016-12-08 17:23:26 +01001549 "V(R)=%u (dl=%p)\n", ns, dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001550 /* discard data */
1551 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001552 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001553 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001554 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001555 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001556 lapd_send_rej(lctx, lctx->p_f);
1557 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001558 /* If there are two subsequent sequence errors received,
1559 * ignore it. (Ignore every second subsequent error.)
1560 * This happens if our reply with the REJ is too slow,
1561 * so the remote gets a T200 timeout and sends another
1562 * frame with a sequence error.
1563 * Test showed that replying with two subsequent REJ
1564 * messages could the remote L2 process to abort.
1565 * Replying too slow shouldn't happen, but may happen
1566 * over serial link between BB and LAPD.
1567 */
1568 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001569 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001570 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1571 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1572 lapd_acknowledge(lctx); /* V(A) is also set here */
1573
1574 /* Send message, if possible due to acknowledged data */
1575 lapd_send_i(lctx, __LINE__);
1576
1577 return 0;
rootaf48bed2011-09-26 11:23:06 +02001578 }
1579 dl->seq_err_cond = 0;
1580
1581 /* Increment receiver state */
1582 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Philipp Maier08177d32016-12-08 17:23:26 +01001583 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u (dl=%p)\n",
1584 dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001585
1586 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1587 lapd_acknowledge(lctx); /* V(A) is also set here */
1588
1589 /* Only if we are not in own receiver busy condition */
1590 if (!dl->own_busy) {
1591 /* if the frame carries a complete segment */
1592 if (!lctx->more && !dl->rcv_buffer) {
Philipp Maier08177d32016-12-08 17:23:26 +01001593 LOGP(DLLAPD, LOGL_INFO,
1594 "message in single I frame (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001595 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001596 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001597 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1598 msg);
1599 } else {
1600 /* create rcv_buffer */
1601 if (!dl->rcv_buffer) {
1602 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001603 "I frames (first message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001604 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1605 "LAPD RX");
1606 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1607 }
1608 /* concat. rcv_buffer */
1609 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1610 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
Philipp Maier08177d32016-12-08 17:23:26 +01001611 "overflow! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001612 } else {
1613 memcpy(msgb_put(dl->rcv_buffer, length),
1614 msg->l3h, length);
1615 }
1616 /* if the last segment was received */
1617 if (!lctx->more) {
1618 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001619 "I frames (last message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001620 rc = send_dl_l3(PRIM_DL_DATA,
1621 PRIM_OP_INDICATION, lctx,
1622 dl->rcv_buffer);
1623 dl->rcv_buffer = NULL;
1624 } else
1625 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001626 "I frames (next message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001627 msgb_free(msg);
1628
1629 }
1630 } else
1631 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1632 "busy condition\n");
1633
1634 /* Check for P bit */
1635 if (lctx->p_f) {
1636 /* 5.5.2.1 */
1637 /* check if we are not in own receiver busy */
1638 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001639 LOGP(DLLAPD, LOGL_INFO,
1640 "we are not busy, send RR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001641 /* Send RR with F=1 */
1642 rc = lapd_send_rr(lctx, 1, 0);
1643 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001644 LOGP(DLLAPD, LOGL_INFO,
1645 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001646 /* Send RNR with F=1 */
1647 rc = lapd_send_rnr(lctx, 1, 0);
1648 }
1649 } else {
1650 /* 5.5.2.2 */
1651 /* check if we are not in own receiver busy */
1652 if (!dl->own_busy) {
1653 /* NOTE: V(R) is already set above */
1654 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001655
1656 /* if update_pending_iframe returns 0 it updated
1657 * the lapd header of an iframe in the tx queue */
1658 if (rc && dl->update_pending_frames)
1659 rc = dl->update_pending_frames(lctx);
1660
rootaf48bed2011-09-26 11:23:06 +02001661 if (rc) {
1662 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
Philipp Maier08177d32016-12-08 17:23:26 +01001663 "have no pending data, send RR (dl=%p)\n",
1664 dl);
rootaf48bed2011-09-26 11:23:06 +02001665 /* Send RR with F=0 */
1666 return lapd_send_rr(lctx, 0, 0);
1667 }
1668 /* all I or one RR is sent, we are done */
1669 return 0;
1670 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001671 LOGP(DLLAPD, LOGL_INFO,
1672 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001673 /* Send RNR with F=0 */
1674 rc = lapd_send_rnr(lctx, 0, 0);
1675 }
1676 }
1677
1678 /* Send message, if possible due to acknowledged data */
1679 lapd_send_i(lctx, __LINE__);
1680
1681 return rc;
1682}
1683
1684/* Receive a LAPD message from L1 */
1685int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1686{
1687 int rc;
1688
1689 switch (lctx->format) {
1690 case LAPD_FORM_U:
1691 rc = lapd_rx_u(msg, lctx);
1692 break;
1693 case LAPD_FORM_S:
1694 rc = lapd_rx_s(msg, lctx);
1695 break;
1696 case LAPD_FORM_I:
1697 rc = lapd_rx_i(msg, lctx);
1698 break;
1699 default:
Philipp Maier08177d32016-12-08 17:23:26 +01001700 LOGP(DLLAPD, LOGL_NOTICE,
1701 "unknown LAPD format (dl=%p)\n", lctx->dl);
rootaf48bed2011-09-26 11:23:06 +02001702 msgb_free(msg);
1703 rc = -EINVAL;
1704 }
1705 return rc;
1706}
1707
1708/* L3 -> L2 */
1709
1710/* send unit data */
1711static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1712{
1713 struct lapd_datalink *dl = lctx->dl;
1714 struct msgb *msg = dp->oph.msg;
1715 struct lapd_msg_ctx nctx;
1716
1717 memcpy(&nctx, lctx, sizeof(nctx));
1718 /* keep nctx.ldp */
1719 /* keep nctx.sapi */
1720 /* keep nctx.tei */
1721 nctx.cr = dl->cr.loc2rem.cmd;
1722 nctx.format = LAPD_FORM_U;
1723 nctx.s_u = LAPD_U_UI;
1724 /* keep nctx.p_f */
1725 nctx.length = msg->len;
1726 nctx.more = 0;
1727
1728 return dl->send_ph_data_req(&nctx, msg);
1729}
1730
1731/* request link establishment */
1732static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1733{
1734 struct lapd_datalink *dl = lctx->dl;
1735 struct msgb *msg = dp->oph.msg;
1736 struct lapd_msg_ctx nctx;
1737
1738 if (msg->len)
1739 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
Philipp Maier08177d32016-12-08 17:23:26 +01001740 "(SABM) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001741 else
Philipp Maier08177d32016-12-08 17:23:26 +01001742 LOGP(DLLAPD, LOGL_INFO,
1743 "perform normal establishm. (SABM), (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001744
1745 /* Flush send-queue */
1746 /* Clear send-buffer */
1747 lapd_dl_flush_send(dl);
1748 /* be sure that history is empty */
1749 lapd_dl_flush_hist(dl);
1750
1751 /* save message context for further use */
1752 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1753
1754 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001755 msgb_free(dl->rcv_buffer);
1756 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001757
1758 /* assemble message */
1759 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1760 /* keep nctx.ldp */
1761 /* keep nctx.sapi */
1762 /* keep nctx.tei */
1763 nctx.cr = dl->cr.loc2rem.cmd;
1764 nctx.format = LAPD_FORM_U;
1765 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1766 nctx.p_f = 1;
1767 nctx.length = msg->len;
1768 nctx.more = 0;
1769
1770 /* Transmit-buffer carries exactly one segment */
1771 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1772 msgb_put(dl->tx_hist[0].msg, msg->len);
1773 if (msg->len)
1774 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1775 dl->tx_hist[0].more = 0;
1776 /* set Vs to 0, because it is used as index when resending SABM */
1777 dl->v_send = 0;
1778
1779 /* Set states */
1780 dl->own_busy = dl->peer_busy = 0;
1781 dl->retrans_ctr = 0;
1782 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1783
1784 /* Tramsmit and start T200 */
1785 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001786 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001787
1788 return 0;
1789}
1790
1791/* send data */
1792static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1793{
1794 struct lapd_datalink *dl = lctx->dl;
1795 struct msgb *msg = dp->oph.msg;
1796
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001797 if (msgb_l3len(msg) == 0) {
1798 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001799 "writing an empty message is not possible. (dl=%p)\n", dl);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001800 msgb_free(msg);
1801 return -1;
1802 }
1803
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001804 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +01001805 "writing message to send-queue: l3len: %d (dl=%p)\n",
1806 msgb_l3len(msg), dl);
rootaf48bed2011-09-26 11:23:06 +02001807
1808 /* Write data into the send queue */
1809 msgb_enqueue(&dl->send_queue, msg);
1810
1811 /* Send message, if possible */
1812 lapd_send_i(&dl->lctx, __LINE__);
1813
1814 return 0;
1815}
1816
1817/* Send next I frame from queued/buffered data */
1818static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1819{
1820 struct lapd_datalink *dl = lctx->dl;
1821 uint8_t k = dl->k;
1822 uint8_t h;
1823 struct msgb *msg;
1824 int length, left;
1825 int rc = - 1; /* we sent nothing */
1826 struct lapd_msg_ctx nctx;
1827
1828
Philipp Maier08177d32016-12-08 17:23:26 +01001829 LOGP(DLLAPD, LOGL_INFO,
1830 "%s() called from line %d (dl=%p)\n", __func__, line, dl);
rootaf48bed2011-09-26 11:23:06 +02001831
1832 next_frame:
1833
1834 if (dl->peer_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001835 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001836 return rc;
1837 }
1838
1839 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Philipp Maier08177d32016-12-08 17:23:26 +01001840 LOGP(DLLAPD, LOGL_INFO,
1841 "timer recovery, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001842 return rc;
1843 }
1844
1845 /* If the send state variable V(S) is equal to V(A) plus k
1846 * (where k is the maximum number of outstanding I frames - see
1847 * subclause 5.8.4), the data link layer entity shall not transmit any
1848 * new I frames, but shall retransmit an I frame as a result
1849 * of the error recovery procedures as described in subclauses 5.5.4 and
1850 * 5.5.7. */
1851 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1852 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
Philipp Maier08177d32016-12-08 17:23:26 +01001853 "more (k=%u V(S)=%u V(A)=%u) (dl=%p)\n", k, dl->v_send,
1854 dl->v_ack, dl);
rootaf48bed2011-09-26 11:23:06 +02001855 return rc;
1856 }
1857
1858 h = do_mod(dl->v_send, dl->range_hist);
1859
1860 /* if we have no tx_hist yet, we create it */
1861 if (!dl->tx_hist[h].msg) {
1862 /* Get next message into send-buffer, if any */
1863 if (!dl->send_buffer) {
1864 next_message:
1865 dl->send_out = 0;
1866 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1867 /* No more data to be sent */
1868 if (!dl->send_buffer)
1869 return rc;
1870 LOGP(DLLAPD, LOGL_INFO, "get message from "
Philipp Maier08177d32016-12-08 17:23:26 +01001871 "send-queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001872 }
1873
1874 /* How much is left in the send-buffer? */
1875 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1876 /* Segment, if data exceeds N201 */
1877 length = left;
1878 if (length > lctx->n201)
1879 length = lctx->n201;
1880 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
Philipp Maier08177d32016-12-08 17:23:26 +01001881 "length %d first byte %02x (dl=%p)\n",
rootaf48bed2011-09-26 11:23:06 +02001882 msgb_l3len(dl->send_buffer), dl->send_out, left,
Philipp Maier08177d32016-12-08 17:23:26 +01001883 lctx->n201, length, dl->send_buffer->l3h[0], dl);
rootaf48bed2011-09-26 11:23:06 +02001884 /* If message in send-buffer is completely sent */
1885 if (left == 0) {
1886 msgb_free(dl->send_buffer);
1887 dl->send_buffer = NULL;
1888 goto next_message;
1889 }
1890
Philipp Maier08177d32016-12-08 17:23:26 +01001891 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d (dl=%p)\n",
1892 (left > length) ? "segment " : "", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001893
1894 /* Create I frame (segment) and transmit-buffer content */
1895 msg = lapd_msgb_alloc(length, "LAPD I");
1896 msg->l3h = msgb_put(msg, length);
1897 /* assemble message */
1898 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1899 /* keep nctx.ldp */
1900 /* keep nctx.sapi */
1901 /* keep nctx.tei */
1902 nctx.cr = dl->cr.loc2rem.cmd;
1903 nctx.format = LAPD_FORM_I;
1904 nctx.p_f = 0;
1905 nctx.n_send = dl->v_send;
1906 nctx.n_recv = dl->v_recv;
1907 nctx.length = length;
1908 if (left > length)
1909 nctx.more = 1;
1910 else
1911 nctx.more = 0;
1912 if (length)
1913 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1914 length);
1915 /* store in tx_hist */
1916 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1917 msgb_put(dl->tx_hist[h].msg, msg->len);
1918 if (length)
1919 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1920 dl->tx_hist[h].more = nctx.more;
1921 /* Add length to track how much is already in the tx buffer */
1922 dl->send_out += length;
1923 } else {
1924 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
Philipp Maier08177d32016-12-08 17:23:26 +01001925 "V(S)=%d (dl=%p)\n", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001926
1927 /* Create I frame (segment) from tx_hist */
1928 length = dl->tx_hist[h].msg->len;
1929 msg = lapd_msgb_alloc(length, "LAPD I resend");
1930 msg->l3h = msgb_put(msg, length);
1931 /* assemble message */
1932 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1933 /* keep nctx.ldp */
1934 /* keep nctx.sapi */
1935 /* keep nctx.tei */
1936 nctx.cr = dl->cr.loc2rem.cmd;
1937 nctx.format = LAPD_FORM_I;
1938 nctx.p_f = 0;
1939 nctx.n_send = dl->v_send;
1940 nctx.n_recv = dl->v_recv;
1941 nctx.length = length;
1942 nctx.more = dl->tx_hist[h].more;
1943 if (length)
1944 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1945 }
1946
1947 /* The value of the send state variable V(S) shall be incremented by 1
1948 * at the end of the transmission of the I frame */
1949 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1950
1951 /* If timer T200 is not running at the time right before transmitting a
1952 * frame, when the PH-READY-TO-SEND primitive is received from the
1953 * physical layer., it shall be set. */
1954 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001955 /* stop Timer T203, if running */
1956 lapd_stop_t203(dl);
1957 /* start Timer T200 */
1958 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001959 }
1960
1961 dl->send_ph_data_req(&nctx, msg);
1962
1963 rc = 0; /* we sent something */
1964 goto next_frame;
1965}
1966
1967/* request link suspension */
1968static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1969{
1970 struct lapd_datalink *dl = lctx->dl;
1971 struct msgb *msg = dp->oph.msg;
1972
Philipp Maier08177d32016-12-08 17:23:26 +01001973 LOGP(DLLAPD, LOGL_INFO, "perform suspension (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001974
1975 /* put back the send-buffer to the send-queue (first position) */
1976 if (dl->send_buffer) {
1977 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
Philipp Maier08177d32016-12-08 17:23:26 +01001978 "queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001979 llist_add(&dl->send_buffer->list, &dl->send_queue);
1980 dl->send_buffer = NULL;
1981 } else
Philipp Maier08177d32016-12-08 17:23:26 +01001982 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001983
1984 /* Clear transmit buffer, but keep send buffer */
1985 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001986 /* Stop timers (there is no state change, so we must stop all timers */
1987 lapd_stop_t200(dl);
1988 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001989
1990 msgb_free(msg);
1991
1992 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1993}
1994
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001995/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001996static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1997{
1998 struct lapd_datalink *dl = lctx->dl;
1999 struct msgb *msg = dp->oph.msg;
2000 struct lapd_msg_ctx nctx;
2001
Philipp Maier08177d32016-12-08 17:23:26 +01002002 LOGP(DLLAPD, LOGL_INFO,
2003 "perform re-establishment (SABM) length=%d (dl=%p)\n",
2004 msg->len, dl);
rootaf48bed2011-09-26 11:23:06 +02002005
2006 /* be sure that history is empty */
2007 lapd_dl_flush_hist(dl);
2008
2009 /* save message context for further use */
2010 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2011
2012 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002013 msgb_free(dl->send_buffer);
2014 dl->send_buffer = NULL;
2015
rootaf48bed2011-09-26 11:23:06 +02002016 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002017 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002018 /* Write data into the send buffer, to be sent first */
2019 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002020 } else {
2021 msgb_free(msg);
2022 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002023 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002024 }
rootaf48bed2011-09-26 11:23:06 +02002025
2026 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002027 msgb_free(dl->rcv_buffer);
2028 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002029
2030 /* Create new msgb (old one is now free) */
2031 msg = lapd_msgb_alloc(0, "LAPD SABM");
2032 msg->l3h = msg->data;
2033 /* assemble message */
2034 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2035 /* keep nctx.ldp */
2036 /* keep nctx.sapi */
2037 /* keep nctx.tei */
2038 nctx.cr = dl->cr.loc2rem.cmd;
2039 nctx.format = LAPD_FORM_U;
2040 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2041 nctx.p_f = 1;
2042 nctx.length = 0;
2043 nctx.more = 0;
2044
2045 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2046 msgb_put(dl->tx_hist[0].msg, msg->len);
2047 if (msg->len)
2048 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2049 dl->tx_hist[0].more = 0;
2050 /* set Vs to 0, because it is used as index when resending SABM */
2051 dl->v_send = 0;
2052
2053 /* Set states */
2054 dl->own_busy = dl->peer_busy = 0;
2055 dl->retrans_ctr = 0;
2056 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2057
2058 /* Tramsmit and start T200 */
2059 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002060 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002061
2062 return 0;
2063}
2064
2065/* requesst release of link */
2066static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2067{
2068 struct lapd_datalink *dl = lctx->dl;
2069 struct msgb *msg = dp->oph.msg;
2070 struct lapd_msg_ctx nctx;
2071
2072 /* local release */
2073 if (dp->u.rel_req.mode) {
Philipp Maier08177d32016-12-08 17:23:26 +01002074 LOGP(DLLAPD, LOGL_INFO, "perform local release (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002075 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002076 /* stop Timer T200 */
2077 lapd_stop_t200(dl);
2078 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002079 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2080 /* flush buffers */
2081 lapd_dl_flush_tx(dl);
2082 lapd_dl_flush_send(dl);
2083 /* send notification to L3 */
2084 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2085 }
2086
2087 /* in case we are already disconnecting */
2088 if (dl->state == LAPD_STATE_DISC_SENT)
2089 return -EBUSY;
2090
2091 /* flush tx_hist */
2092 lapd_dl_flush_hist(dl);
2093
Philipp Maier08177d32016-12-08 17:23:26 +01002094 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002095
2096 /* Push LAPD header on msgb */
2097 /* assemble message */
2098 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2099 /* keep nctx.ldp */
2100 /* keep nctx.sapi */
2101 /* keep nctx.tei */
2102 nctx.cr = dl->cr.loc2rem.cmd;
2103 nctx.format = LAPD_FORM_U;
2104 nctx.s_u = LAPD_U_DISC;
2105 nctx.p_f = 1;
2106 nctx.length = 0;
2107 nctx.more = 0;
2108
2109 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2110 msgb_put(dl->tx_hist[0].msg, msg->len);
2111 if (msg->len)
2112 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2113 dl->tx_hist[0].more = 0;
2114 /* set Vs to 0, because it is used as index when resending DISC */
2115 dl->v_send = 0;
2116
2117 /* Set states */
2118 dl->own_busy = dl->peer_busy = 0;
2119 dl->retrans_ctr = 0;
2120 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2121
2122 /* Tramsmit and start T200 */
2123 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002124 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002125
2126 return 0;
2127}
2128
2129/* request release of link in idle state */
2130static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2131 struct lapd_msg_ctx *lctx)
2132{
2133 struct lapd_datalink *dl = lctx->dl;
2134 struct msgb *msg = dp->oph.msg;
2135
2136 msgb_free(msg);
2137
2138 /* send notification to L3 */
2139 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2140}
2141
2142/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002143static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002144 uint32_t states;
2145 int prim, op;
2146 const char *name;
2147 int (*rout) (struct osmo_dlsap_prim *dp,
2148 struct lapd_msg_ctx *lctx);
2149} l2downstatelist[] = {
2150 /* create and send UI command */
2151 {ALL_STATES,
2152 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2153 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2154
2155 /* create and send SABM command */
2156 {SBIT(LAPD_STATE_IDLE),
2157 PRIM_DL_EST, PRIM_OP_REQUEST,
2158 "DL-ESTABLISH-REQUEST", lapd_est_req},
2159
2160 /* create and send I command */
2161 {SBIT(LAPD_STATE_MF_EST) |
2162 SBIT(LAPD_STATE_TIMER_RECOV),
2163 PRIM_DL_DATA, PRIM_OP_REQUEST,
2164 "DL-DATA-REQUEST", lapd_data_req},
2165
2166 /* suspend datalink */
2167 {SBIT(LAPD_STATE_MF_EST) |
2168 SBIT(LAPD_STATE_TIMER_RECOV),
2169 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2170 "DL-SUSPEND-REQUEST", lapd_susp_req},
2171
2172 /* create and send SABM command (resume) */
2173 {SBIT(LAPD_STATE_MF_EST) |
2174 SBIT(LAPD_STATE_TIMER_RECOV),
2175 PRIM_DL_RES, PRIM_OP_REQUEST,
2176 "DL-RESUME-REQUEST", lapd_res_req},
2177
2178 /* create and send SABM command (reconnect) */
2179 {SBIT(LAPD_STATE_IDLE) |
2180 SBIT(LAPD_STATE_MF_EST) |
2181 SBIT(LAPD_STATE_TIMER_RECOV),
2182 PRIM_DL_RECON, PRIM_OP_REQUEST,
2183 "DL-RECONNECT-REQUEST", lapd_res_req},
2184
2185 /* create and send DISC command */
2186 {SBIT(LAPD_STATE_SABM_SENT) |
2187 SBIT(LAPD_STATE_MF_EST) |
2188 SBIT(LAPD_STATE_TIMER_RECOV) |
2189 SBIT(LAPD_STATE_DISC_SENT),
2190 PRIM_DL_REL, PRIM_OP_REQUEST,
2191 "DL-RELEASE-REQUEST", lapd_rel_req},
2192
2193 /* release in idle state */
2194 {SBIT(LAPD_STATE_IDLE),
2195 PRIM_DL_REL, PRIM_OP_REQUEST,
2196 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2197};
2198
2199#define L2DOWNSLLEN \
2200 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2201
2202int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2203{
2204 struct lapd_datalink *dl = lctx->dl;
2205 int i, supported = 0;
2206 struct msgb *msg = dp->oph.msg;
2207 int rc;
2208
2209 /* find function for current state and message */
2210 for (i = 0; i < L2DOWNSLLEN; i++) {
2211 if (dp->oph.primitive == l2downstatelist[i].prim
2212 && dp->oph.operation == l2downstatelist[i].op) {
2213 supported = 1;
2214 if ((SBIT(dl->state) & l2downstatelist[i].states))
2215 break;
2216 }
2217 }
2218 if (!supported) {
Philipp Maier08177d32016-12-08 17:23:26 +01002219 LOGP(DLLAPD, LOGL_NOTICE,
2220 "Message %u/%u unsupported. (dl=%p)\n", dp->oph.primitive,
2221 dp->oph.operation, dl);
rootaf48bed2011-09-26 11:23:06 +02002222 msgb_free(msg);
2223 return 0;
2224 }
2225 if (i == L2DOWNSLLEN) {
2226 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
Philipp Maier08177d32016-12-08 17:23:26 +01002227 "state %s. (dl=%p)\n", dp->oph.primitive,
Harald Weltec733d142017-03-15 10:20:51 +01002228 dp->oph.operation, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002229 msgb_free(msg);
2230 return 0;
2231 }
2232
Philipp Maier08177d32016-12-08 17:23:26 +01002233 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01002234 l2downstatelist[i].name, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002235
2236 rc = l2downstatelist[i].rout(dp, lctx);
2237
2238 return rc;
2239}
2240
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002241/*! @} */