blob: 5d8715464af469af415f6a46c8e8356095c9215f [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;
Philipp Maier08177d32016-12-08 17:23:26 +0100206 LOGP(DLLAPD, LOGL_INFO, "start T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200207 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
208}
209
210static void lapd_start_t203(struct lapd_datalink *dl)
211{
212 if (osmo_timer_pending(&dl->t203))
213 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100214 LOGP(DLLAPD, LOGL_INFO, "start T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200215 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
216}
217
218static void lapd_stop_t200(struct lapd_datalink *dl)
219{
220 if (!osmo_timer_pending(&dl->t200))
221 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100222 LOGP(DLLAPD, LOGL_INFO, "stop T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200223 osmo_timer_del(&dl->t200);
224}
225
226static void lapd_stop_t203(struct lapd_datalink *dl)
227{
228 if (!osmo_timer_pending(&dl->t203))
229 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100230 LOGP(DLLAPD, LOGL_INFO, "stop T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200231 osmo_timer_del(&dl->t203);
232}
233
rootaf48bed2011-09-26 11:23:06 +0200234static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
235{
Philipp Maier08177d32016-12-08 17:23:26 +0100236 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100237 lapd_state_name(dl->state), lapd_state_name(state), dl);
rootaf48bed2011-09-26 11:23:06 +0200238
239 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
240 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200241 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200242 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200243 msgb_free(dl->cont_res);
244 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200245 }
246
247 /* start T203 on entering MF EST state, if enabled */
248 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200249 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
250 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200251
252 dl->state = state;
253}
254
rootaf48bed2011-09-26 11:23:06 +0200255static void *tall_lapd_ctx = NULL;
256
257/* init datalink instance and allocate history */
258void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
259 int maxf)
260{
261 int m;
262
263 memset(dl, 0, sizeof(*dl));
264 INIT_LLIST_HEAD(&dl->send_queue);
265 INIT_LLIST_HEAD(&dl->tx_queue);
266 dl->reestablish = 1;
267 dl->n200_est_rel = 3;
268 dl->n200 = 3;
269 dl->t200_sec = 1;
270 dl->t200_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200271 osmo_timer_setup(&dl->t200, lapd_t200_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200272 dl->t203_sec = 10;
273 dl->t203_usec = 0;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200274 osmo_timer_setup(&dl->t203, lapd_t203_cb, dl);
rootaf48bed2011-09-26 11:23:06 +0200275 dl->maxf = maxf;
276 if (k > v_range - 1)
277 k = v_range - 1;
278 dl->k = k;
279 dl->v_range = v_range;
280
281 /* Calculate modulo for history array:
282 * - The history range must be at least k+1.
283 * - The history range must be 2^x, where x is as low as possible.
284 */
285 k++;
286 for (m = 0x80; m; m >>= 1) {
287 if ((m & k)) {
288 if (k > m)
289 m <<= 1;
290 dl->range_hist = m;
291 break;
292 }
293 }
294
295 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
Philipp Maier08177d32016-12-08 17:23:26 +0100296 "history range = %d (dl=%p)\n", dl->v_range, dl->k,
297 dl->range_hist, dl);
rootaf48bed2011-09-26 11:23:06 +0200298
299 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
300
301 if (!tall_lapd_ctx)
302 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100303 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
304 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200305}
306
307/* reset to IDLE state */
308void lapd_dl_reset(struct lapd_datalink *dl)
309{
Jean-Francois Dionne893979c2017-03-02 10:45:53 -0500310 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
311 /* enter idle state (and remove eventual cont_res) */
312 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200313 /* flush buffer */
314 lapd_dl_flush_tx(dl);
315 lapd_dl_flush_send(dl);
316 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200317 msgb_free(dl->rcv_buffer);
318 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200319 /* stop Timers */
320 lapd_stop_t200(dl);
321 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500322 if (dl->state == LAPD_STATE_IDLE)
323 return;
324 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance (dl=%p)\n", dl);
325 /* enter idle state (and remove eventual cont_res) */
326 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200327}
328
329/* reset and de-allocate history buffer */
330void lapd_dl_exit(struct lapd_datalink *dl)
331{
332 /* free all ressources except history buffer */
333 lapd_dl_reset(dl);
Ivan Kluchnikovb9759db2017-05-11 15:19:23 +0300334
335 /* enter null state */
336 lapd_dl_newstate(dl, LAPD_STATE_NULL);
337
rootaf48bed2011-09-26 11:23:06 +0200338 /* free history buffer list */
339 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200340 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200341}
342
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200343/*! Set the \ref lapdm_mode of a LAPDm entity */
rootaf48bed2011-09-26 11:23:06 +0200344int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
345{
346 switch (mode) {
347 case LAPD_MODE_USER:
348 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
349 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
350 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
351 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
352 break;
353 case LAPD_MODE_NETWORK:
354 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
355 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
356 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
357 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
358 break;
359 default:
360 return -EINVAL;
361 }
362 dl->mode = mode;
363
364 return 0;
365}
366
367/* send DL message with optional msgb */
368static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
369 struct msgb *msg)
370{
371 struct lapd_datalink *dl = lctx->dl;
372 struct osmo_dlsap_prim dp;
373
374 osmo_prim_init(&dp.oph, 0, prim, op, msg);
375 return dl->send_dlsap(&dp, lctx);
376}
377
378/* send simple DL message */
379static inline int send_dl_simple(uint8_t prim, uint8_t op,
380 struct lapd_msg_ctx *lctx)
381{
Pau Espin Pedrol9dd3bf02016-02-29 08:49:22 -0500382 return send_dl_l3(prim, op, lctx, NULL);
rootaf48bed2011-09-26 11:23:06 +0200383}
384
385/* send MDL-ERROR INDICATION */
386static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
387{
388 struct lapd_datalink *dl = lctx->dl;
389 struct osmo_dlsap_prim dp;
390
Philipp Maier08177d32016-12-08 17:23:26 +0100391 LOGP(DLLAPD, LOGL_NOTICE,
392 "sending MDL-ERROR-IND cause %d from state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100393 cause, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200394 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
395 dp.u.error_ind.cause = cause;
396 return dl->send_dlsap(&dp, lctx);
397}
398
399/* send UA response */
400static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
401{
402 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
403 struct lapd_msg_ctx nctx;
404 struct lapd_datalink *dl = lctx->dl;
405
406 memcpy(&nctx, lctx, sizeof(nctx));
407 msg->l3h = msgb_put(msg, len);
408 if (len)
409 memcpy(msg->l3h, data, len);
410 /* keep nctx.ldp */
411 /* keep nctx.sapi */
412 /* keep nctx.tei */
413 nctx.cr = dl->cr.loc2rem.resp;
414 nctx.format = LAPD_FORM_U;
415 nctx.s_u = LAPD_U_UA;
416 /* keep nctx.p_f */
417 nctx.length = len;
418 nctx.more = 0;
419
420 return dl->send_ph_data_req(&nctx, msg);
421}
422
423/* send DM response */
424static int lapd_send_dm(struct lapd_msg_ctx *lctx)
425{
426 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
427 struct lapd_msg_ctx nctx;
428 struct lapd_datalink *dl = lctx->dl;
429
430 memcpy(&nctx, lctx, sizeof(nctx));
431 /* keep nctx.ldp */
432 /* keep nctx.sapi */
433 /* keep nctx.tei */
434 nctx.cr = dl->cr.loc2rem.resp;
435 nctx.format = LAPD_FORM_U;
436 nctx.s_u = LAPD_U_DM;
437 /* keep nctx.p_f */
438 nctx.length = 0;
439 nctx.more = 0;
440
441 return dl->send_ph_data_req(&nctx, msg);
442}
443
444/* send RR response / command */
445static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
446{
447 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
448 struct lapd_msg_ctx nctx;
449 struct lapd_datalink *dl = lctx->dl;
450
451 memcpy(&nctx, lctx, sizeof(nctx));
452 /* keep nctx.ldp */
453 /* keep nctx.sapi */
454 /* keep nctx.tei */
455 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
456 nctx.format = LAPD_FORM_S;
457 nctx.s_u = LAPD_S_RR;
458 nctx.p_f = f_bit;
459 nctx.n_recv = dl->v_recv;
460 nctx.length = 0;
461 nctx.more = 0;
462
463 return dl->send_ph_data_req(&nctx, msg);
464}
465
466/* send RNR response / command */
467static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
468{
469 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
470 struct lapd_msg_ctx nctx;
471 struct lapd_datalink *dl = lctx->dl;
472
473 memcpy(&nctx, lctx, sizeof(nctx));
474 /* keep nctx.ldp */
475 /* keep nctx.sapi */
476 /* keep nctx.tei */
477 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
478 nctx.format = LAPD_FORM_S;
479 nctx.s_u = LAPD_S_RNR;
480 nctx.p_f = f_bit;
481 nctx.n_recv = dl->v_recv;
482 nctx.length = 0;
483 nctx.more = 0;
484
485 return dl->send_ph_data_req(&nctx, msg);
486}
487
488/* send REJ response */
489static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
490{
491 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
492 struct lapd_msg_ctx nctx;
493 struct lapd_datalink *dl = lctx->dl;
494
495 memcpy(&nctx, lctx, sizeof(nctx));
496 /* keep nctx.ldp */
497 /* keep nctx.sapi */
498 /* keep nctx.tei */
499 nctx.cr = dl->cr.loc2rem.resp;
500 nctx.format = LAPD_FORM_S;
501 nctx.s_u = LAPD_S_REJ;
502 nctx.p_f = f_bit;
503 nctx.n_recv = dl->v_recv;
504 nctx.length = 0;
505 nctx.more = 0;
506
507 return dl->send_ph_data_req(&nctx, msg);
508}
509
510/* resend SABM or DISC message */
511static int lapd_send_resend(struct lapd_datalink *dl)
512{
513 struct msgb *msg;
514 uint8_t h = do_mod(dl->v_send, dl->range_hist);
515 int length = dl->tx_hist[h].msg->len;
516 struct lapd_msg_ctx nctx;
517
518 /* assemble message */
519 memcpy(&nctx, &dl->lctx, sizeof(nctx));
520 /* keep nctx.ldp */
521 /* keep nctx.sapi */
522 /* keep nctx.tei */
523 nctx.cr = dl->cr.loc2rem.cmd;
524 nctx.format = LAPD_FORM_U;
525 if (dl->state == LAPD_STATE_SABM_SENT)
526 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
527 else
528 nctx.s_u = LAPD_U_DISC;
529 nctx.p_f = 1;
530 nctx.length = length;
531 nctx.more = 0;
532
533 /* Resend SABM/DISC from tx_hist */
534 msg = lapd_msgb_alloc(length, "LAPD resend");
535 msg->l3h = msgb_put(msg, length);
536 if (length)
537 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
538
539 return dl->send_ph_data_req(&nctx, msg);
540}
541
542/* reestablish link */
543static int lapd_reestablish(struct lapd_datalink *dl)
544{
545 struct osmo_dlsap_prim dp;
546 struct msgb *msg;
547
Philipp Maier08177d32016-12-08 17:23:26 +0100548 LOGP(DLLAPD, LOGL_DEBUG, "lapd reestablish (dl=%p)\n", dl);
549
rootaf48bed2011-09-26 11:23:06 +0200550 msg = lapd_msgb_alloc(0, "DUMMY");
551 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +0100552
rootaf48bed2011-09-26 11:23:06 +0200553 return lapd_est_req(&dp, &dl->lctx);
554}
555
556/* Timer callback on T200 expiry */
557static void lapd_t200_cb(void *data)
558{
559 struct lapd_datalink *dl = data;
560
Philipp Maier08177d32016-12-08 17:23:26 +0100561 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100562 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200563
564 switch (dl->state) {
565 case LAPD_STATE_SABM_SENT:
566 /* 5.4.1.3 */
567 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200568 /* flush tx and send buffers */
569 lapd_dl_flush_tx(dl);
570 lapd_dl_flush_send(dl);
571 /* go back to idle state */
572 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
573 /* NOTE: we must not change any other states or buffers
574 * and queues, since we may reconnect after handover
575 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100576 /* send MDL ERROR INIDCATION to L3 */
577 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100578 /* send RELEASE INDICATION to L3 */
579 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
580 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200581 break;
582 }
583 /* retransmit SABM command */
584 lapd_send_resend(dl);
585 /* increment re-transmission counter */
586 dl->retrans_ctr++;
587 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200588 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200589 break;
590 case LAPD_STATE_DISC_SENT:
591 /* 5.4.4.3 */
592 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200593 /* send MDL ERROR INIDCATION to L3 */
594 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maier6b986c22017-02-01 12:00:45 +0100595 /* send RELEASE INDICATION to L3 */
596 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200597 /* flush tx and send buffers */
598 lapd_dl_flush_tx(dl);
599 lapd_dl_flush_send(dl);
600 /* go back to idle state */
601 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
602 /* NOTE: we must not change any other states or buffers
603 * and queues, since we may reconnect after handover
604 * failure. the buffered messages is replaced there */
605 break;
606 }
607 /* retransmit DISC command */
608 lapd_send_resend(dl);
609 /* increment re-transmission counter */
610 dl->retrans_ctr++;
611 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200612 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200613 break;
614 case LAPD_STATE_MF_EST:
615 /* 5.5.7 */
616 dl->retrans_ctr = 0;
617 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
618 /* fall through */
619 case LAPD_STATE_TIMER_RECOV:
620 dl->retrans_ctr++;
621 if (dl->retrans_ctr < dl->n200) {
622 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
623 uint8_t h = do_mod(vs, dl->range_hist);
624 /* retransmit I frame (V_s-1) with P=1, if any */
625 if (dl->tx_hist[h].msg) {
626 struct msgb *msg;
627 int length = dl->tx_hist[h].msg->len;
628 struct lapd_msg_ctx nctx;
629
630 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
Philipp Maier08177d32016-12-08 17:23:26 +0100631 " V(S)=%d (dl=%p)\n", vs, dl);
rootaf48bed2011-09-26 11:23:06 +0200632 /* Create I frame (segment) from tx_hist */
633 memcpy(&nctx, &dl->lctx, sizeof(nctx));
634 /* keep nctx.ldp */
635 /* keep nctx.sapi */
636 /* keep nctx.tei */
637 nctx.cr = dl->cr.loc2rem.cmd;
638 nctx.format = LAPD_FORM_I;
639 nctx.p_f = 1;
640 nctx.n_send = vs;
641 nctx.n_recv = dl->v_recv;
642 nctx.length = length;
643 nctx.more = dl->tx_hist[h].more;
644 msg = lapd_msgb_alloc(length, "LAPD I resend");
645 msg->l3h = msgb_put(msg, length);
646 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
647 length);
648 dl->send_ph_data_req(&nctx, msg);
649 } else {
650 /* OR send appropriate supervision frame with P=1 */
651 if (!dl->own_busy && !dl->seq_err_cond) {
652 lapd_send_rr(&dl->lctx, 1, 1);
653 /* NOTE: In case of sequence error
654 * condition, the REJ frame has been
655 * transmitted when entering the
656 * condition, so it has not be done
657 * here
658 */
659 } else if (dl->own_busy) {
660 lapd_send_rnr(&dl->lctx, 1, 1);
661 } else {
662 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
Philipp Maier08177d32016-12-08 17:23:26 +0100663 "pls. fix (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200664 }
665 }
666 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200667 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200668 } else {
669 /* send MDL ERROR INIDCATION to L3 */
670 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
671 /* reestablish */
672 if (!dl->reestablish)
673 break;
674 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100675 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200676 lapd_reestablish(dl);
677 }
678 break;
679 default:
680 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
Harald Weltec733d142017-03-15 10:20:51 +0100681 "dl->state %s (dl=%p)\n", lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200682 }
683}
684
685/* Timer callback on T203 expiry */
686static void lapd_t203_cb(void *data)
687{
688 struct lapd_datalink *dl = data;
689
Philipp Maier08177d32016-12-08 17:23:26 +0100690 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100691 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200692
693 if (dl->state != LAPD_STATE_MF_EST) {
694 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
Philipp Maier08177d32016-12-08 17:23:26 +0100695 "please fix! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200696 return;
697 }
698
699 /* set retransmission counter to 0 */
700 dl->retrans_ctr = 0;
701 /* enter timer recovery state */
702 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
703 /* transmit a supervisory command with P bit set to 1 as follows: */
704 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +0100705 LOGP(DLLAPD, LOGL_INFO,
706 "transmit an RR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200707 /* Send RR with P=1 */
708 lapd_send_rr(&dl->lctx, 1, 1);
709 } else {
Philipp Maier08177d32016-12-08 17:23:26 +0100710 LOGP(DLLAPD, LOGL_INFO,
711 "transmit an RNR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200712 /* Send RNR with P=1 */
713 lapd_send_rnr(&dl->lctx, 1, 1);
714 }
715 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200716 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200717}
718
719/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
720static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
721{
722 struct lapd_datalink *dl = lctx->dl;
723 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100724 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200725 int i, h;
726
727 /* supervisory frame ? */
728 if (lctx->format == LAPD_FORM_S)
729 s = 1;
730 /* REJ frame ? */
731 if (s && lctx->s_u == LAPD_S_REJ)
732 rej = 1;
733
734 /* Flush all transmit buffers of acknowledged frames */
735 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
736 h = do_mod(i, dl->range_hist);
737 if (dl->tx_hist[h].msg) {
738 msgb_free(dl->tx_hist[h].msg);
739 dl->tx_hist[h].msg = NULL;
740 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
741 }
742 }
743
744 if (dl->state != LAPD_STATE_TIMER_RECOV) {
745 /* When not in the timer recovery condition, the data
746 * link layer entity shall reset the timer T200 on
747 * receipt of a valid I frame with N(R) higher than V(A),
748 * or an REJ with an N(R) equal to V(A). */
749 if ((!rej && nr != dl->v_ack)
750 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200751 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200752 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200753 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
754 }
755 /* 5.7.4: N(R) sequence error
756 * N(R) is called valid, if and only if
757 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
758 */
759 if (sub_mod(nr, dl->v_ack, dl->v_range)
760 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Philipp Maier08177d32016-12-08 17:23:26 +0100761 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200762 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
763 }
764 }
765
766 /* V(A) shall be set to the value of N(R) */
767 dl->v_ack = nr;
768
Andreas Eversberg742fc792011-09-27 09:40:25 +0200769 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200770 * and if there are outstanding I frames, restart T200 */
771 if (t200_reset && !rej) {
772 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
773 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
Philipp Maier08177d32016-12-08 17:23:26 +0100774 "frame(s) (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200775 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200776 }
777 }
778
779 /* This also does a restart, when I or S frame is received */
780
781 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200782 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200783 /* Start T203, if T200 is not running in MF EST state, if enabled */
784 if (!osmo_timer_pending(&dl->t200)
785 && (dl->t203_sec || dl->t203_usec)
786 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200787 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200788 }
789}
790
791/* L1 -> L2 */
792
793/* Receive a LAPD U (Unnumbered) message from L1 */
794static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
795{
796 struct lapd_datalink *dl = lctx->dl;
797 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100798 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200799 uint8_t prim, op;
800
801 switch (lctx->s_u) {
802 case LAPD_U_SABM:
803 case LAPD_U_SABME:
804 prim = PRIM_DL_EST;
805 op = PRIM_OP_INDICATION;
806
Philipp Maier08177d32016-12-08 17:23:26 +0100807 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100808 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200809 /* 5.7.1 */
810 dl->seq_err_cond = 0;
811 /* G.2.2 Wrong value of the C/R bit */
812 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +0100813 LOGP(DLLAPD, LOGL_ERROR,
814 "SABM response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200815 msgb_free(msg);
816 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
817 return -EINVAL;
818 }
819
820 /* G.4.5 If SABM is received with L>N201 or with M bit
821 * set, AN MDL-ERROR-INDICATION is sent to MM.
822 */
823 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +0100824 LOGP(DLLAPD, LOGL_ERROR,
825 "SABM too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200826 msgb_free(msg);
827 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
828 return -EIO;
829 }
830
831 switch (dl->state) {
832 case LAPD_STATE_IDLE:
833 break;
834 case LAPD_STATE_MF_EST:
835 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
Philipp Maier08177d32016-12-08 17:23:26 +0100836 "frame established state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200837 /* If link is lost on the remote side, we start over
838 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100839 /* Additionally, continue in case of content resoltion
840 * (GSM network). This happens, if the mobile has not
841 * yet received UA or another mobile (collision) tries
842 * to establish connection. The mobile must receive
843 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200844 /* 5.4.2.1 */
845 if (!length) {
846 /* If no content resolution, this is a
847 * re-establishment. */
848 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +0100849 "Remote reestablish (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200850 break;
851 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200852 if (!dl->cont_res) {
853 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Philipp Maier08177d32016-12-08 17:23:26 +0100854 "allowed in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100855 lapd_state_name(dl->state), dl);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200856 mdl_error(MDL_CAUSE_SABM_MF, lctx);
857 msgb_free(msg);
858 return 0;
859 }
rootaf48bed2011-09-26 11:23:06 +0200860 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200861 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200862#ifdef TEST_CONTENT_RESOLUTION_NETWORK
863 dl->cont_res->data[0] ^= 0x01;
864#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100865 if (memcmp(dl->cont_res->data, msg->data,
866 length)) {
rootaf48bed2011-09-26 11:23:06 +0200867 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100868 "with different content - "
Philipp Maier08177d32016-12-08 17:23:26 +0100869 "ignoring! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200870 msgb_free(msg);
871 return 0;
872 }
873 }
874 /* send UA again */
875 lapd_send_ua(lctx, length, msg->l3h);
876 msgb_free(msg);
877 return 0;
878 case LAPD_STATE_DISC_SENT:
879 /* 5.4.6.2 send DM with F=P */
880 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200881 /* stop Timer T200 */
882 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200883 msgb_free(msg);
884 return send_dl_simple(prim, op, lctx);
885 default:
886 /* collision: Send UA, but still wait for rx UA, then
887 * change to MF_EST state.
888 */
889 /* check for contention resoultion */
890 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
891 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Philipp Maier08177d32016-12-08 17:23:26 +0100892 "during contention resolution (state=%s, dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100893 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200894 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
895 }
896 lapd_send_ua(lctx, length, msg->l3h);
897 msgb_free(msg);
898 return 0;
899 }
900 /* save message context for further use */
901 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
902#ifndef TEST_CONTENT_RESOLUTION_NETWORK
903 /* send UA response */
904 lapd_send_ua(lctx, length, msg->l3h);
905#endif
906 /* set Vs, Vr and Va to 0 */
907 dl->v_send = dl->v_recv = dl->v_ack = 0;
908 /* clear tx_hist */
909 lapd_dl_flush_hist(dl);
910 /* enter multiple-frame-established state */
911 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
912 /* store content resolution data on network side
913 * Note: cont_res will be removed when changing state again,
914 * so it must be allocated AFTER lapd_dl_newstate(). */
915 if (dl->mode == LAPD_MODE_NETWORK && length) {
916 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
917 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
918 length);
Philipp Maier08177d32016-12-08 17:23:26 +0100919 LOGP(DLLAPD, LOGL_NOTICE,
920 "Store content res. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200921 }
922 /* send notification to L3 */
923 if (length == 0) {
924 /* 5.4.1.2 Normal establishment procedures */
925 rc = send_dl_simple(prim, op, lctx);
926 msgb_free(msg);
927 } else {
928 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200929 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200930 rc = send_dl_l3(prim, op, lctx, msg);
931 }
932 break;
933 case LAPD_U_DM:
Philipp Maier08177d32016-12-08 17:23:26 +0100934 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100935 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200936 /* G.2.2 Wrong value of the C/R bit */
937 if (lctx->cr == dl->cr.rem2loc.cmd) {
Philipp Maier08177d32016-12-08 17:23:26 +0100938 LOGP(DLLAPD, LOGL_ERROR,
939 "DM command error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200940 msgb_free(msg);
941 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
942 return -EINVAL;
943 }
944 if (!lctx->p_f) {
945 /* 5.4.1.2 DM responses with the F bit set to "0"
946 * shall be ignored.
947 */
948 msgb_free(msg);
949 return 0;
950 }
951 switch (dl->state) {
952 case LAPD_STATE_SABM_SENT:
953 break;
954 case LAPD_STATE_MF_EST:
955 if (lctx->p_f) {
956 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
Philipp Maier08177d32016-12-08 17:23:26 +0100957 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200958 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
959 } else {
960 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
961 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100962 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200963 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
964 /* reestablish */
965 if (!dl->reestablish) {
966 msgb_free(msg);
967 return 0;
968 }
969 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100970 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200971 lapd_reestablish(dl);
972 }
973 msgb_free(msg);
974 return 0;
975 case LAPD_STATE_TIMER_RECOV:
976 /* FP = 0 (DM is normal in case PF = 1) */
977 if (!lctx->p_f) {
978 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
979 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100980 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200981 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
982 msgb_free(msg);
983 /* reestablish */
984 if (!dl->reestablish)
985 return 0;
986 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100987 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200988 return lapd_reestablish(dl);
989 }
990 break;
991 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200992 /* stop Timer T200 */
993 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200994 /* go to idle state */
995 lapd_dl_flush_tx(dl);
996 lapd_dl_flush_send(dl);
997 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
998 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
999 msgb_free(msg);
1000 return 0;
1001 case LAPD_STATE_IDLE:
1002 /* 5.4.5 all other frame types shall be discarded */
1003 default:
1004 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001005 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001006 msgb_free(msg);
1007 return 0;
1008 }
Andreas Eversberg742fc792011-09-27 09:40:25 +02001009 /* stop timer T200 */
1010 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001011 /* go to idle state */
1012 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1013 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1014 msgb_free(msg);
1015 break;
1016 case LAPD_U_UI:
Philipp Maier08177d32016-12-08 17:23:26 +01001017 LOGP(DLLAPD, LOGL_INFO, "UI received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001018 /* G.2.2 Wrong value of the C/R bit */
1019 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001020 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
Philipp Maier08177d32016-12-08 17:23:26 +01001021 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001022 msgb_free(msg);
1023 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1024 return -EINVAL;
1025 }
1026
1027 /* G.4.5 If UI is received with L>N201 or with M bit
1028 * set, AN MDL-ERROR-INDICATION is sent to MM.
1029 */
1030 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001031 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
Philipp Maier08177d32016-12-08 17:23:26 +01001032 "(%d > N201(%d) or M=%d) (dl=%p)\n", length,
1033 lctx->n201, lctx->more, dl);
rootaf48bed2011-09-26 11:23:06 +02001034 msgb_free(msg);
1035 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1036 return -EIO;
1037 }
1038
1039 /* do some length checks */
1040 if (length == 0) {
1041 /* 5.3.3 UI frames received with the length indicator
1042 * set to "0" shall be ignored
1043 */
Philipp Maier08177d32016-12-08 17:23:26 +01001044 LOGP(DLLAPD, LOGL_INFO,
1045 "length=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001046 msgb_free(msg);
1047 return 0;
1048 }
Harald Welte087116a2013-06-18 21:41:34 +02001049 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001050 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1051 msg);
1052 break;
1053 case LAPD_U_DISC:
1054 prim = PRIM_DL_REL;
1055 op = PRIM_OP_INDICATION;
1056
Philipp Maier08177d32016-12-08 17:23:26 +01001057 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001058 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001059 /* flush tx and send buffers */
1060 lapd_dl_flush_tx(dl);
1061 lapd_dl_flush_send(dl);
1062 /* 5.7.1 */
1063 dl->seq_err_cond = 0;
1064 /* G.2.2 Wrong value of the C/R bit */
1065 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001066 LOGP(DLLAPD, LOGL_ERROR,
1067 "DISC response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001068 msgb_free(msg);
1069 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1070 return -EINVAL;
1071 }
1072 if (length > 0 || lctx->more) {
1073 /* G.4.4 If a DISC or DM frame is received with L>0 or
1074 * with the M bit set to "1", an MDL-ERROR-INDICATION
1075 * primitive with cause "U frame with incorrect
1076 * parameters" is sent to the mobile management entity.
1077 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001078 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001079 "U frame iwth incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001080 msgb_free(msg);
1081 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1082 return -EIO;
1083 }
1084 switch (dl->state) {
1085 case LAPD_STATE_IDLE:
Philipp Maier08177d32016-12-08 17:23:26 +01001086 LOGP(DLLAPD, LOGL_INFO,
1087 "DISC in idle state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001088 /* send DM with F=P */
1089 msgb_free(msg);
1090 return lapd_send_dm(lctx);
1091 case LAPD_STATE_SABM_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001092 LOGP(DLLAPD, LOGL_INFO,
1093 "DISC in SABM state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001094 /* 5.4.6.2 send DM with F=P */
1095 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001096 /* stop Timer T200 */
1097 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001098 /* go to idle state */
1099 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1100 msgb_free(msg);
1101 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1102 lctx);
1103 case LAPD_STATE_MF_EST:
1104 case LAPD_STATE_TIMER_RECOV:
Philipp Maier08177d32016-12-08 17:23:26 +01001105 LOGP(DLLAPD, LOGL_INFO,
1106 "DISC in est state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001107 break;
1108 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001109 LOGP(DLLAPD, LOGL_INFO,
1110 "DISC in disc state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001111 prim = PRIM_DL_REL;
1112 op = PRIM_OP_CONFIRM;
1113 break;
1114 default:
1115 lapd_send_ua(lctx, length, msg->l3h);
1116 msgb_free(msg);
1117 return 0;
1118 }
1119 /* send UA response */
1120 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001121 /* stop Timer T200 */
1122 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001123 /* enter idle state, keep tx-buffer with UA response */
1124 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1125 /* send notification to L3 */
1126 rc = send_dl_simple(prim, op, lctx);
1127 msgb_free(msg);
1128 break;
1129 case LAPD_U_UA:
Philipp Maier08177d32016-12-08 17:23:26 +01001130 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001131 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001132 /* G.2.2 Wrong value of the C/R bit */
1133 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001134 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
Philipp Maier08177d32016-12-08 17:23:26 +01001135 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001136 msgb_free(msg);
1137 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1138 return -EINVAL;
1139 }
1140
1141 /* G.4.5 If UA is received with L>N201 or with M bit
1142 * set, AN MDL-ERROR-INDICATION is sent to MM.
1143 */
1144 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001145 LOGP(DLLAPD, LOGL_ERROR,
1146 "UA too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001147 msgb_free(msg);
1148 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1149 return -EIO;
1150 }
1151
1152 if (!lctx->p_f) {
1153 /* 5.4.1.2 A UA response with the F bit set to "0"
1154 * shall be ignored.
1155 */
Philipp Maier08177d32016-12-08 17:23:26 +01001156 LOGP(DLLAPD, LOGL_INFO,
1157 "F=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001158 msgb_free(msg);
1159 return 0;
1160 }
1161 switch (dl->state) {
1162 case LAPD_STATE_SABM_SENT:
1163 break;
1164 case LAPD_STATE_MF_EST:
1165 case LAPD_STATE_TIMER_RECOV:
1166 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001167 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001168 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1169 msgb_free(msg);
1170 return 0;
1171 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001172 LOGP(DLLAPD, LOGL_INFO,
1173 "UA in disconnect state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001174 /* stop Timer T200 */
1175 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001176 /* go to idle state */
1177 lapd_dl_flush_tx(dl);
1178 lapd_dl_flush_send(dl);
1179 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1180 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1181 msgb_free(msg);
1182 return 0;
1183 case LAPD_STATE_IDLE:
1184 /* 5.4.5 all other frame types shall be discarded */
1185 default:
1186 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001187 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001188 msgb_free(msg);
1189 return 0;
1190 }
Philipp Maier08177d32016-12-08 17:23:26 +01001191 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001192 /* stop Timer T200 */
1193 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001194 /* compare UA with SABME if contention resolution is applied */
1195 if (dl->tx_hist[0].msg->len) {
1196 if (length != (dl->tx_hist[0].msg->len)
1197 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1198 length)) {
1199 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
Philipp Maier08177d32016-12-08 17:23:26 +01001200 "mismatches **** (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001201 rc = send_dl_simple(PRIM_DL_REL,
1202 PRIM_OP_INDICATION, lctx);
1203 msgb_free(msg);
1204 /* go to idle state */
1205 lapd_dl_flush_tx(dl);
1206 lapd_dl_flush_send(dl);
1207 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1208 return 0;
1209 }
1210 }
1211 /* set Vs, Vr and Va to 0 */
1212 dl->v_send = dl->v_recv = dl->v_ack = 0;
1213 /* clear tx_hist */
1214 lapd_dl_flush_hist(dl);
1215 /* enter multiple-frame-established state */
1216 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1217 /* send outstanding frames, if any (resume / reconnect) */
1218 lapd_send_i(lctx, __LINE__);
1219 /* send notification to L3 */
1220 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1221 msgb_free(msg);
1222 break;
1223 case LAPD_U_FRMR:
Philipp Maier08177d32016-12-08 17:23:26 +01001224 LOGP(DLLAPD, LOGL_NOTICE,
1225 "Frame reject received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001226 /* send MDL ERROR INIDCATION to L3 */
1227 mdl_error(MDL_CAUSE_FRMR, lctx);
1228 msgb_free(msg);
1229 /* reestablish */
1230 if (!dl->reestablish)
1231 break;
Philipp Maier08177d32016-12-08 17:23:26 +01001232 LOGP(DLLAPD, LOGL_NOTICE,
1233 "Performing reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001234 rc = lapd_reestablish(dl);
1235 break;
1236 default:
1237 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001238 LOGP(DLLAPD, LOGL_NOTICE,
1239 "Unnumbered frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001240 msgb_free(msg);
1241 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1242 return -EINVAL;
1243 }
1244 return rc;
1245}
1246
1247/* Receive a LAPD S (Supervisory) message from L1 */
1248static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1249{
1250 struct lapd_datalink *dl = lctx->dl;
1251 int length = lctx->length;
1252
1253 if (length > 0 || lctx->more) {
1254 /* G.4.3 If a supervisory frame is received with L>0 or
1255 * with the M bit set to "1", an MDL-ERROR-INDICATION
1256 * primitive with cause "S frame with incorrect
1257 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001258 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001259 "S frame with incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001260 msgb_free(msg);
1261 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1262 return -EIO;
1263 }
1264
1265 if (lctx->cr == dl->cr.rem2loc.resp
1266 && lctx->p_f
1267 && dl->state != LAPD_STATE_TIMER_RECOV) {
1268 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001269 LOGP(DLLAPD, LOGL_NOTICE,
1270 "S frame response with F=1 error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001271 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1272 }
1273
1274 switch (dl->state) {
1275 case LAPD_STATE_IDLE:
1276 /* if P=1, respond DM with F=1 (5.2.2) */
1277 /* 5.4.5 all other frame types shall be discarded */
1278 if (lctx->p_f)
1279 lapd_send_dm(lctx); /* F=P */
1280 /* fall though */
1281 case LAPD_STATE_SABM_SENT:
1282 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001283 LOGP(DLLAPD, LOGL_NOTICE,
1284 "S frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001285 msgb_free(msg);
1286 return 0;
1287 }
1288 switch (lctx->s_u) {
1289 case LAPD_S_RR:
Philipp Maier08177d32016-12-08 17:23:26 +01001290 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001291 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001292 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1293 lapd_acknowledge(lctx);
1294
1295 /* 5.5.3.2 */
1296 if (lctx->cr == dl->cr.rem2loc.cmd
1297 && lctx->p_f) {
1298 if (!dl->own_busy && !dl->seq_err_cond) {
1299 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001300 "with polling bit set and we are not "
1301 "busy, so we reply with RR frame "
1302 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001303 lapd_send_rr(lctx, 1, 0);
1304 /* NOTE: In case of sequence error condition,
1305 * the REJ frame has been transmitted when
1306 * entering the condition, so it has not be
1307 * done here
1308 */
1309 } else if (dl->own_busy) {
1310 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001311 "with polling bit set and we are busy, "
1312 "so we reply with RR frame response (dl=%p)\n",
1313 dl);
rootaf48bed2011-09-26 11:23:06 +02001314 lapd_send_rnr(lctx, 1, 0);
1315 }
1316 } else if (lctx->cr == dl->cr.rem2loc.resp
1317 && lctx->p_f
1318 && dl->state == LAPD_STATE_TIMER_RECOV) {
1319 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1320 "and we are in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001321 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001322 /* V(S) to the N(R) in the RR frame */
1323 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001324 /* stop Timer T200 */
1325 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001326 /* 5.5.7 Clear timer recovery condition */
1327 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1328 }
1329 /* Send message, if possible due to acknowledged data */
1330 lapd_send_i(lctx, __LINE__);
1331
1332 break;
1333 case LAPD_S_RNR:
Philipp Maier08177d32016-12-08 17:23:26 +01001334 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001335 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001336 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1337 lapd_acknowledge(lctx);
1338
1339 /* 5.5.5 */
1340 /* Set peer receiver busy condition */
1341 dl->peer_busy = 1;
1342
1343 if (lctx->p_f) {
1344 if (lctx->cr == dl->cr.rem2loc.cmd) {
1345 if (!dl->own_busy) {
1346 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1347 "command and we are not busy, "
1348 "so we reply with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001349 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001350 /* Send RR with F=1 */
1351 lapd_send_rr(lctx, 1, 0);
1352 } else {
1353 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1354 "command and we are busy, so "
1355 "we reply with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001356 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001357 /* Send RNR with F=1 */
1358 lapd_send_rnr(lctx, 1, 0);
1359 }
1360 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1361 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1362 "and we in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001363 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001364 /* 5.5.7 Clear timer recovery condition */
1365 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1366 /* V(S) to the N(R) in the RNR frame */
1367 dl->v_send = lctx->n_recv;
1368 }
1369 } else
1370 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
Philipp Maier08177d32016-12-08 17:23:26 +01001371 "received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001372
1373 /* Send message, if possible due to acknowledged data */
1374 lapd_send_i(lctx, __LINE__);
1375
1376 break;
1377 case LAPD_S_REJ:
Philipp Maier08177d32016-12-08 17:23:26 +01001378 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001379 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001380 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1381 lapd_acknowledge(lctx);
1382
1383 /* 5.5.4.1 */
1384 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1385 /* Clear an existing peer receiver busy condition */
1386 dl->peer_busy = 0;
1387 /* V(S) and V(A) to the N(R) in the REJ frame */
1388 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001389 /* stop Timer T200 */
1390 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001391 /* 5.5.3.2 */
1392 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1393 if (!dl->own_busy && !dl->seq_err_cond) {
1394 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1395 "command not in timer recovery "
1396 "state and not in own busy "
1397 "condition received, so we "
1398 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001399 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001400 lapd_send_rr(lctx, 1, 0);
1401 /* NOTE: In case of sequence error
1402 * condition, the REJ frame has been
1403 * transmitted when entering the
1404 * condition, so it has not be done
1405 * here
1406 */
1407 } else if (dl->own_busy) {
1408 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1409 "command not in timer recovery "
1410 "state and in own busy "
1411 "condition received, so we "
1412 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001413 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001414 lapd_send_rnr(lctx, 1, 0);
1415 }
1416 } else
1417 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1418 "polling command not in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001419 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001420 /* send MDL ERROR INIDCATION to L3 */
1421 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Philipp Maier08177d32016-12-08 17:23:26 +01001422 LOGP(DLLAPD, LOGL_ERROR,
1423 "unsolicited supervisory response! (dl=%p)\n",
1424 dl);
rootaf48bed2011-09-26 11:23:06 +02001425 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1426 }
1427
1428 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1429 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
Philipp Maier08177d32016-12-08 17:23:26 +01001430 "recovery state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001431 /* Clear an existing peer receiver busy condition */
1432 dl->peer_busy = 0;
1433 /* V(S) and V(A) to the N(R) in the REJ frame */
1434 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001435 /* stop Timer T200 */
1436 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001437 /* 5.5.7 Clear timer recovery condition */
1438 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1439 } else {
1440 /* Clear an existing peer receiver busy condition */
1441 dl->peer_busy = 0;
1442 /* V(S) and V(A) to the N(R) in the REJ frame */
1443 dl->v_send = dl->v_ack = lctx->n_recv;
1444 /* 5.5.3.2 */
1445 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1446 if (!dl->own_busy && !dl->seq_err_cond) {
1447 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1448 "command in timer recovery "
1449 "state and not in own busy "
1450 "condition received, so we "
1451 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001452 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001453 lapd_send_rr(lctx, 1, 0);
1454 /* NOTE: In case of sequence error
1455 * condition, the REJ frame has been
1456 * transmitted when entering the
1457 * condition, so it has not be done
1458 * here
1459 */
1460 } else if (dl->own_busy) {
1461 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1462 "command in timer recovery "
1463 "state and in own busy "
1464 "condition received, so we "
1465 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001466 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001467 lapd_send_rnr(lctx, 1, 0);
1468 }
1469 } else
1470 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1471 "polling command in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001472 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001473 }
1474
1475 /* FIXME: 5.5.4.2 2) */
1476
1477 /* Send message, if possible due to acknowledged data */
1478 lapd_send_i(lctx, __LINE__);
1479
1480 break;
1481 default:
1482 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001483 LOGP(DLLAPD, LOGL_ERROR,
1484 "Supervisory frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001485 msgb_free(msg);
1486 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1487 return -EINVAL;
1488 }
1489 msgb_free(msg);
1490 return 0;
1491}
1492
1493/* Receive a LAPD I (Information) message from L1 */
1494static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1495{
1496 struct lapd_datalink *dl = lctx->dl;
1497 //uint8_t nr = lctx->n_recv;
1498 uint8_t ns = lctx->n_send;
1499 int length = lctx->length;
1500 int rc;
1501
Philipp Maier08177d32016-12-08 17:23:26 +01001502 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u) (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001503 lapd_state_name(dl->state), lctx->sapi, dl);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001504
rootaf48bed2011-09-26 11:23:06 +02001505 /* G.2.2 Wrong value of the C/R bit */
1506 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001507 LOGP(DLLAPD, LOGL_ERROR,
1508 "I frame response not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001509 msgb_free(msg);
1510 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1511 return -EINVAL;
1512 }
1513
1514 if (length == 0 || length > lctx->n201) {
1515 /* G.4.2 If the length indicator of an I frame is set
1516 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1517 * primitive with cause "I frame with incorrect length"
1518 * is sent to the mobile management entity. */
Philipp Maier08177d32016-12-08 17:23:26 +01001519 LOGP(DLLAPD, LOGL_ERROR,
1520 "I frame length not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001521 msgb_free(msg);
1522 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1523 return -EIO;
1524 }
1525
1526 /* G.4.2 If the numerical value of L is L<N201 and the M
1527 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1528 * cause "I frame with incorrect use of M bit" is sent to the
1529 * mobile management entity. */
1530 if (lctx->more && length < lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001531 LOGP(DLLAPD, LOGL_ERROR,
1532 "I frame with M bit too short (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001533 msgb_free(msg);
1534 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1535 return -EIO;
1536 }
1537
1538 switch (dl->state) {
1539 case LAPD_STATE_IDLE:
1540 /* if P=1, respond DM with F=1 (5.2.2) */
1541 /* 5.4.5 all other frame types shall be discarded */
1542 if (lctx->p_f)
1543 lapd_send_dm(lctx); /* F=P */
1544 /* fall though */
1545 case LAPD_STATE_SABM_SENT:
1546 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001547 LOGP(DLLAPD, LOGL_NOTICE,
1548 "I frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001549 msgb_free(msg);
1550 return 0;
1551 }
1552
1553 /* 5.7.1: N(s) sequence error */
1554 if (ns != dl->v_recv) {
1555 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
Philipp Maier08177d32016-12-08 17:23:26 +01001556 "V(R)=%u (dl=%p)\n", ns, dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001557 /* discard data */
1558 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001559 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001560 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001561 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001562 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001563 lapd_send_rej(lctx, lctx->p_f);
1564 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001565 /* If there are two subsequent sequence errors received,
1566 * ignore it. (Ignore every second subsequent error.)
1567 * This happens if our reply with the REJ is too slow,
1568 * so the remote gets a T200 timeout and sends another
1569 * frame with a sequence error.
1570 * Test showed that replying with two subsequent REJ
1571 * messages could the remote L2 process to abort.
1572 * Replying too slow shouldn't happen, but may happen
1573 * over serial link between BB and LAPD.
1574 */
1575 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001576 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001577 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1578 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1579 lapd_acknowledge(lctx); /* V(A) is also set here */
1580
1581 /* Send message, if possible due to acknowledged data */
1582 lapd_send_i(lctx, __LINE__);
1583
1584 return 0;
rootaf48bed2011-09-26 11:23:06 +02001585 }
1586 dl->seq_err_cond = 0;
1587
1588 /* Increment receiver state */
1589 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Philipp Maier08177d32016-12-08 17:23:26 +01001590 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u (dl=%p)\n",
1591 dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001592
1593 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1594 lapd_acknowledge(lctx); /* V(A) is also set here */
1595
1596 /* Only if we are not in own receiver busy condition */
1597 if (!dl->own_busy) {
1598 /* if the frame carries a complete segment */
1599 if (!lctx->more && !dl->rcv_buffer) {
Philipp Maier08177d32016-12-08 17:23:26 +01001600 LOGP(DLLAPD, LOGL_INFO,
1601 "message in single I frame (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001602 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001603 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001604 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1605 msg);
1606 } else {
1607 /* create rcv_buffer */
1608 if (!dl->rcv_buffer) {
1609 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001610 "I frames (first message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001611 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1612 "LAPD RX");
1613 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1614 }
1615 /* concat. rcv_buffer */
1616 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1617 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
Philipp Maier08177d32016-12-08 17:23:26 +01001618 "overflow! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001619 } else {
1620 memcpy(msgb_put(dl->rcv_buffer, length),
1621 msg->l3h, length);
1622 }
1623 /* if the last segment was received */
1624 if (!lctx->more) {
1625 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001626 "I frames (last message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001627 rc = send_dl_l3(PRIM_DL_DATA,
1628 PRIM_OP_INDICATION, lctx,
1629 dl->rcv_buffer);
1630 dl->rcv_buffer = NULL;
1631 } else
1632 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001633 "I frames (next message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001634 msgb_free(msg);
1635
1636 }
1637 } else
1638 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1639 "busy condition\n");
1640
1641 /* Check for P bit */
1642 if (lctx->p_f) {
1643 /* 5.5.2.1 */
1644 /* check if we are not in own receiver busy */
1645 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001646 LOGP(DLLAPD, LOGL_INFO,
1647 "we are not busy, send RR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001648 /* Send RR with F=1 */
1649 rc = lapd_send_rr(lctx, 1, 0);
1650 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001651 LOGP(DLLAPD, LOGL_INFO,
1652 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001653 /* Send RNR with F=1 */
1654 rc = lapd_send_rnr(lctx, 1, 0);
1655 }
1656 } else {
1657 /* 5.5.2.2 */
1658 /* check if we are not in own receiver busy */
1659 if (!dl->own_busy) {
1660 /* NOTE: V(R) is already set above */
1661 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001662
1663 /* if update_pending_iframe returns 0 it updated
1664 * the lapd header of an iframe in the tx queue */
1665 if (rc && dl->update_pending_frames)
1666 rc = dl->update_pending_frames(lctx);
1667
rootaf48bed2011-09-26 11:23:06 +02001668 if (rc) {
1669 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
Philipp Maier08177d32016-12-08 17:23:26 +01001670 "have no pending data, send RR (dl=%p)\n",
1671 dl);
rootaf48bed2011-09-26 11:23:06 +02001672 /* Send RR with F=0 */
1673 return lapd_send_rr(lctx, 0, 0);
1674 }
1675 /* all I or one RR is sent, we are done */
1676 return 0;
1677 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001678 LOGP(DLLAPD, LOGL_INFO,
1679 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001680 /* Send RNR with F=0 */
1681 rc = lapd_send_rnr(lctx, 0, 0);
1682 }
1683 }
1684
1685 /* Send message, if possible due to acknowledged data */
1686 lapd_send_i(lctx, __LINE__);
1687
1688 return rc;
1689}
1690
1691/* Receive a LAPD message from L1 */
1692int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1693{
1694 int rc;
1695
1696 switch (lctx->format) {
1697 case LAPD_FORM_U:
1698 rc = lapd_rx_u(msg, lctx);
1699 break;
1700 case LAPD_FORM_S:
1701 rc = lapd_rx_s(msg, lctx);
1702 break;
1703 case LAPD_FORM_I:
1704 rc = lapd_rx_i(msg, lctx);
1705 break;
1706 default:
Philipp Maier08177d32016-12-08 17:23:26 +01001707 LOGP(DLLAPD, LOGL_NOTICE,
1708 "unknown LAPD format (dl=%p)\n", lctx->dl);
rootaf48bed2011-09-26 11:23:06 +02001709 msgb_free(msg);
1710 rc = -EINVAL;
1711 }
1712 return rc;
1713}
1714
1715/* L3 -> L2 */
1716
1717/* send unit data */
1718static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1719{
1720 struct lapd_datalink *dl = lctx->dl;
1721 struct msgb *msg = dp->oph.msg;
1722 struct lapd_msg_ctx nctx;
1723
1724 memcpy(&nctx, lctx, sizeof(nctx));
1725 /* keep nctx.ldp */
1726 /* keep nctx.sapi */
1727 /* keep nctx.tei */
1728 nctx.cr = dl->cr.loc2rem.cmd;
1729 nctx.format = LAPD_FORM_U;
1730 nctx.s_u = LAPD_U_UI;
1731 /* keep nctx.p_f */
1732 nctx.length = msg->len;
1733 nctx.more = 0;
1734
1735 return dl->send_ph_data_req(&nctx, msg);
1736}
1737
1738/* request link establishment */
1739static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1740{
1741 struct lapd_datalink *dl = lctx->dl;
1742 struct msgb *msg = dp->oph.msg;
1743 struct lapd_msg_ctx nctx;
1744
1745 if (msg->len)
1746 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
Philipp Maier08177d32016-12-08 17:23:26 +01001747 "(SABM) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001748 else
Philipp Maier08177d32016-12-08 17:23:26 +01001749 LOGP(DLLAPD, LOGL_INFO,
1750 "perform normal establishm. (SABM), (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001751
1752 /* Flush send-queue */
1753 /* Clear send-buffer */
1754 lapd_dl_flush_send(dl);
1755 /* be sure that history is empty */
1756 lapd_dl_flush_hist(dl);
1757
1758 /* save message context for further use */
1759 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1760
1761 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001762 msgb_free(dl->rcv_buffer);
1763 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001764
1765 /* assemble message */
1766 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1767 /* keep nctx.ldp */
1768 /* keep nctx.sapi */
1769 /* keep nctx.tei */
1770 nctx.cr = dl->cr.loc2rem.cmd;
1771 nctx.format = LAPD_FORM_U;
1772 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1773 nctx.p_f = 1;
1774 nctx.length = msg->len;
1775 nctx.more = 0;
1776
1777 /* Transmit-buffer carries exactly one segment */
1778 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1779 msgb_put(dl->tx_hist[0].msg, msg->len);
1780 if (msg->len)
1781 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1782 dl->tx_hist[0].more = 0;
1783 /* set Vs to 0, because it is used as index when resending SABM */
1784 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01001785
rootaf48bed2011-09-26 11:23:06 +02001786 /* Set states */
1787 dl->own_busy = dl->peer_busy = 0;
1788 dl->retrans_ctr = 0;
1789 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1790
1791 /* Tramsmit and start T200 */
1792 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001793 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001794
1795 return 0;
1796}
1797
1798/* send data */
1799static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1800{
1801 struct lapd_datalink *dl = lctx->dl;
1802 struct msgb *msg = dp->oph.msg;
1803
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001804 if (msgb_l3len(msg) == 0) {
1805 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001806 "writing an empty message is not possible. (dl=%p)\n", dl);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001807 msgb_free(msg);
1808 return -1;
1809 }
1810
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001811 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +01001812 "writing message to send-queue: l3len: %d (dl=%p)\n",
1813 msgb_l3len(msg), dl);
rootaf48bed2011-09-26 11:23:06 +02001814
1815 /* Write data into the send queue */
1816 msgb_enqueue(&dl->send_queue, msg);
1817
1818 /* Send message, if possible */
1819 lapd_send_i(&dl->lctx, __LINE__);
1820
1821 return 0;
1822}
1823
1824/* Send next I frame from queued/buffered data */
1825static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1826{
1827 struct lapd_datalink *dl = lctx->dl;
1828 uint8_t k = dl->k;
1829 uint8_t h;
1830 struct msgb *msg;
1831 int length, left;
1832 int rc = - 1; /* we sent nothing */
1833 struct lapd_msg_ctx nctx;
1834
1835
Philipp Maier08177d32016-12-08 17:23:26 +01001836 LOGP(DLLAPD, LOGL_INFO,
1837 "%s() called from line %d (dl=%p)\n", __func__, line, dl);
rootaf48bed2011-09-26 11:23:06 +02001838
1839 next_frame:
1840
1841 if (dl->peer_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001842 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001843 return rc;
1844 }
1845
1846 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Philipp Maier08177d32016-12-08 17:23:26 +01001847 LOGP(DLLAPD, LOGL_INFO,
1848 "timer recovery, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001849 return rc;
1850 }
1851
1852 /* If the send state variable V(S) is equal to V(A) plus k
1853 * (where k is the maximum number of outstanding I frames - see
1854 * subclause 5.8.4), the data link layer entity shall not transmit any
1855 * new I frames, but shall retransmit an I frame as a result
1856 * of the error recovery procedures as described in subclauses 5.5.4 and
1857 * 5.5.7. */
1858 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1859 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
Philipp Maier08177d32016-12-08 17:23:26 +01001860 "more (k=%u V(S)=%u V(A)=%u) (dl=%p)\n", k, dl->v_send,
1861 dl->v_ack, dl);
rootaf48bed2011-09-26 11:23:06 +02001862 return rc;
1863 }
1864
1865 h = do_mod(dl->v_send, dl->range_hist);
1866
1867 /* if we have no tx_hist yet, we create it */
1868 if (!dl->tx_hist[h].msg) {
1869 /* Get next message into send-buffer, if any */
1870 if (!dl->send_buffer) {
1871 next_message:
1872 dl->send_out = 0;
1873 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1874 /* No more data to be sent */
1875 if (!dl->send_buffer)
1876 return rc;
1877 LOGP(DLLAPD, LOGL_INFO, "get message from "
Philipp Maier08177d32016-12-08 17:23:26 +01001878 "send-queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001879 }
1880
1881 /* How much is left in the send-buffer? */
1882 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1883 /* Segment, if data exceeds N201 */
1884 length = left;
1885 if (length > lctx->n201)
1886 length = lctx->n201;
1887 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
Philipp Maier08177d32016-12-08 17:23:26 +01001888 "length %d first byte %02x (dl=%p)\n",
rootaf48bed2011-09-26 11:23:06 +02001889 msgb_l3len(dl->send_buffer), dl->send_out, left,
Philipp Maier08177d32016-12-08 17:23:26 +01001890 lctx->n201, length, dl->send_buffer->l3h[0], dl);
rootaf48bed2011-09-26 11:23:06 +02001891 /* If message in send-buffer is completely sent */
1892 if (left == 0) {
1893 msgb_free(dl->send_buffer);
1894 dl->send_buffer = NULL;
1895 goto next_message;
1896 }
1897
Philipp Maier08177d32016-12-08 17:23:26 +01001898 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d (dl=%p)\n",
1899 (left > length) ? "segment " : "", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001900
1901 /* Create I frame (segment) and transmit-buffer content */
1902 msg = lapd_msgb_alloc(length, "LAPD I");
1903 msg->l3h = msgb_put(msg, length);
1904 /* assemble message */
1905 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1906 /* keep nctx.ldp */
1907 /* keep nctx.sapi */
1908 /* keep nctx.tei */
1909 nctx.cr = dl->cr.loc2rem.cmd;
1910 nctx.format = LAPD_FORM_I;
1911 nctx.p_f = 0;
1912 nctx.n_send = dl->v_send;
1913 nctx.n_recv = dl->v_recv;
1914 nctx.length = length;
1915 if (left > length)
1916 nctx.more = 1;
1917 else
1918 nctx.more = 0;
1919 if (length)
1920 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1921 length);
1922 /* store in tx_hist */
1923 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1924 msgb_put(dl->tx_hist[h].msg, msg->len);
1925 if (length)
1926 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1927 dl->tx_hist[h].more = nctx.more;
1928 /* Add length to track how much is already in the tx buffer */
1929 dl->send_out += length;
1930 } else {
1931 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
Philipp Maier08177d32016-12-08 17:23:26 +01001932 "V(S)=%d (dl=%p)\n", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001933
1934 /* Create I frame (segment) from tx_hist */
1935 length = dl->tx_hist[h].msg->len;
1936 msg = lapd_msgb_alloc(length, "LAPD I resend");
1937 msg->l3h = msgb_put(msg, length);
1938 /* assemble message */
1939 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1940 /* keep nctx.ldp */
1941 /* keep nctx.sapi */
1942 /* keep nctx.tei */
1943 nctx.cr = dl->cr.loc2rem.cmd;
1944 nctx.format = LAPD_FORM_I;
1945 nctx.p_f = 0;
1946 nctx.n_send = dl->v_send;
1947 nctx.n_recv = dl->v_recv;
1948 nctx.length = length;
1949 nctx.more = dl->tx_hist[h].more;
1950 if (length)
1951 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1952 }
1953
1954 /* The value of the send state variable V(S) shall be incremented by 1
1955 * at the end of the transmission of the I frame */
1956 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1957
1958 /* If timer T200 is not running at the time right before transmitting a
1959 * frame, when the PH-READY-TO-SEND primitive is received from the
1960 * physical layer., it shall be set. */
1961 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001962 /* stop Timer T203, if running */
1963 lapd_stop_t203(dl);
1964 /* start Timer T200 */
1965 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001966 }
1967
1968 dl->send_ph_data_req(&nctx, msg);
1969
1970 rc = 0; /* we sent something */
1971 goto next_frame;
1972}
1973
1974/* request link suspension */
1975static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1976{
1977 struct lapd_datalink *dl = lctx->dl;
1978 struct msgb *msg = dp->oph.msg;
1979
Philipp Maier08177d32016-12-08 17:23:26 +01001980 LOGP(DLLAPD, LOGL_INFO, "perform suspension (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001981
1982 /* put back the send-buffer to the send-queue (first position) */
1983 if (dl->send_buffer) {
1984 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
Philipp Maier08177d32016-12-08 17:23:26 +01001985 "queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001986 llist_add(&dl->send_buffer->list, &dl->send_queue);
1987 dl->send_buffer = NULL;
1988 } else
Philipp Maier08177d32016-12-08 17:23:26 +01001989 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001990
1991 /* Clear transmit buffer, but keep send buffer */
1992 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001993 /* Stop timers (there is no state change, so we must stop all timers */
1994 lapd_stop_t200(dl);
1995 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001996
1997 msgb_free(msg);
1998
1999 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
2000}
2001
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01002002/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02002003static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2004{
2005 struct lapd_datalink *dl = lctx->dl;
2006 struct msgb *msg = dp->oph.msg;
2007 struct lapd_msg_ctx nctx;
2008
Philipp Maier08177d32016-12-08 17:23:26 +01002009 LOGP(DLLAPD, LOGL_INFO,
2010 "perform re-establishment (SABM) length=%d (dl=%p)\n",
2011 msg->len, dl);
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002012
rootaf48bed2011-09-26 11:23:06 +02002013 /* be sure that history is empty */
2014 lapd_dl_flush_hist(dl);
2015
2016 /* save message context for further use */
2017 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2018
2019 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002020 msgb_free(dl->send_buffer);
2021 dl->send_buffer = NULL;
2022
rootaf48bed2011-09-26 11:23:06 +02002023 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002024 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002025 /* Write data into the send buffer, to be sent first */
2026 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002027 } else {
2028 msgb_free(msg);
2029 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002030 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002031 }
rootaf48bed2011-09-26 11:23:06 +02002032
2033 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002034 msgb_free(dl->rcv_buffer);
2035 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002036
2037 /* Create new msgb (old one is now free) */
2038 msg = lapd_msgb_alloc(0, "LAPD SABM");
2039 msg->l3h = msg->data;
2040 /* assemble message */
2041 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2042 /* keep nctx.ldp */
2043 /* keep nctx.sapi */
2044 /* keep nctx.tei */
2045 nctx.cr = dl->cr.loc2rem.cmd;
2046 nctx.format = LAPD_FORM_U;
2047 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2048 nctx.p_f = 1;
2049 nctx.length = 0;
2050 nctx.more = 0;
2051
2052 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2053 msgb_put(dl->tx_hist[0].msg, msg->len);
2054 if (msg->len)
2055 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2056 dl->tx_hist[0].more = 0;
2057 /* set Vs to 0, because it is used as index when resending SABM */
2058 dl->v_send = 0;
2059
2060 /* Set states */
2061 dl->own_busy = dl->peer_busy = 0;
2062 dl->retrans_ctr = 0;
2063 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2064
2065 /* Tramsmit and start T200 */
2066 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002067 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002068
2069 return 0;
2070}
2071
2072/* requesst release of link */
2073static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2074{
2075 struct lapd_datalink *dl = lctx->dl;
2076 struct msgb *msg = dp->oph.msg;
2077 struct lapd_msg_ctx nctx;
2078
2079 /* local release */
2080 if (dp->u.rel_req.mode) {
Philipp Maier08177d32016-12-08 17:23:26 +01002081 LOGP(DLLAPD, LOGL_INFO, "perform local release (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002082 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002083 /* stop Timer T200 */
2084 lapd_stop_t200(dl);
2085 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002086 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2087 /* flush buffers */
2088 lapd_dl_flush_tx(dl);
2089 lapd_dl_flush_send(dl);
2090 /* send notification to L3 */
2091 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2092 }
2093
2094 /* in case we are already disconnecting */
2095 if (dl->state == LAPD_STATE_DISC_SENT)
2096 return -EBUSY;
2097
2098 /* flush tx_hist */
2099 lapd_dl_flush_hist(dl);
2100
Philipp Maier08177d32016-12-08 17:23:26 +01002101 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002102
2103 /* Push LAPD header on msgb */
2104 /* assemble message */
2105 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2106 /* keep nctx.ldp */
2107 /* keep nctx.sapi */
2108 /* keep nctx.tei */
2109 nctx.cr = dl->cr.loc2rem.cmd;
2110 nctx.format = LAPD_FORM_U;
2111 nctx.s_u = LAPD_U_DISC;
2112 nctx.p_f = 1;
2113 nctx.length = 0;
2114 nctx.more = 0;
2115
2116 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2117 msgb_put(dl->tx_hist[0].msg, msg->len);
2118 if (msg->len)
2119 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2120 dl->tx_hist[0].more = 0;
2121 /* set Vs to 0, because it is used as index when resending DISC */
2122 dl->v_send = 0;
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002123
rootaf48bed2011-09-26 11:23:06 +02002124 /* Set states */
2125 dl->own_busy = dl->peer_busy = 0;
2126 dl->retrans_ctr = 0;
2127 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2128
2129 /* Tramsmit and start T200 */
2130 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002131 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002132
2133 return 0;
2134}
2135
2136/* request release of link in idle state */
2137static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2138 struct lapd_msg_ctx *lctx)
2139{
2140 struct lapd_datalink *dl = lctx->dl;
2141 struct msgb *msg = dp->oph.msg;
2142
2143 msgb_free(msg);
2144
2145 /* send notification to L3 */
2146 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2147}
2148
2149/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002150static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002151 uint32_t states;
2152 int prim, op;
2153 const char *name;
2154 int (*rout) (struct osmo_dlsap_prim *dp,
2155 struct lapd_msg_ctx *lctx);
2156} l2downstatelist[] = {
2157 /* create and send UI command */
2158 {ALL_STATES,
Pau Espin Pedrola99e1102017-12-08 14:30:47 +01002159 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
rootaf48bed2011-09-26 11:23:06 +02002160 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2161
2162 /* create and send SABM command */
2163 {SBIT(LAPD_STATE_IDLE),
2164 PRIM_DL_EST, PRIM_OP_REQUEST,
2165 "DL-ESTABLISH-REQUEST", lapd_est_req},
2166
2167 /* create and send I command */
2168 {SBIT(LAPD_STATE_MF_EST) |
2169 SBIT(LAPD_STATE_TIMER_RECOV),
2170 PRIM_DL_DATA, PRIM_OP_REQUEST,
2171 "DL-DATA-REQUEST", lapd_data_req},
2172
2173 /* suspend datalink */
2174 {SBIT(LAPD_STATE_MF_EST) |
2175 SBIT(LAPD_STATE_TIMER_RECOV),
2176 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2177 "DL-SUSPEND-REQUEST", lapd_susp_req},
2178
2179 /* create and send SABM command (resume) */
2180 {SBIT(LAPD_STATE_MF_EST) |
2181 SBIT(LAPD_STATE_TIMER_RECOV),
2182 PRIM_DL_RES, PRIM_OP_REQUEST,
2183 "DL-RESUME-REQUEST", lapd_res_req},
2184
2185 /* create and send SABM command (reconnect) */
2186 {SBIT(LAPD_STATE_IDLE) |
2187 SBIT(LAPD_STATE_MF_EST) |
2188 SBIT(LAPD_STATE_TIMER_RECOV),
2189 PRIM_DL_RECON, PRIM_OP_REQUEST,
2190 "DL-RECONNECT-REQUEST", lapd_res_req},
2191
2192 /* create and send DISC command */
2193 {SBIT(LAPD_STATE_SABM_SENT) |
2194 SBIT(LAPD_STATE_MF_EST) |
2195 SBIT(LAPD_STATE_TIMER_RECOV) |
2196 SBIT(LAPD_STATE_DISC_SENT),
2197 PRIM_DL_REL, PRIM_OP_REQUEST,
2198 "DL-RELEASE-REQUEST", lapd_rel_req},
2199
2200 /* release in idle state */
2201 {SBIT(LAPD_STATE_IDLE),
2202 PRIM_DL_REL, PRIM_OP_REQUEST,
2203 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2204};
2205
2206#define L2DOWNSLLEN \
2207 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2208
2209int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2210{
2211 struct lapd_datalink *dl = lctx->dl;
2212 int i, supported = 0;
2213 struct msgb *msg = dp->oph.msg;
2214 int rc;
2215
2216 /* find function for current state and message */
2217 for (i = 0; i < L2DOWNSLLEN; i++) {
2218 if (dp->oph.primitive == l2downstatelist[i].prim
2219 && dp->oph.operation == l2downstatelist[i].op) {
2220 supported = 1;
2221 if ((SBIT(dl->state) & l2downstatelist[i].states))
2222 break;
2223 }
2224 }
2225 if (!supported) {
Philipp Maier08177d32016-12-08 17:23:26 +01002226 LOGP(DLLAPD, LOGL_NOTICE,
2227 "Message %u/%u unsupported. (dl=%p)\n", dp->oph.primitive,
2228 dp->oph.operation, dl);
rootaf48bed2011-09-26 11:23:06 +02002229 msgb_free(msg);
2230 return 0;
2231 }
2232 if (i == L2DOWNSLLEN) {
2233 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
Philipp Maier08177d32016-12-08 17:23:26 +01002234 "state %s. (dl=%p)\n", dp->oph.primitive,
Harald Weltec733d142017-03-15 10:20:51 +01002235 dp->oph.operation, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002236 msgb_free(msg);
2237 return 0;
2238 }
2239
Philipp Maier08177d32016-12-08 17:23:26 +01002240 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01002241 l2downstatelist[i].name, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002242
2243 rc = l2downstatelist[i].rout(dp, lctx);
2244
2245 return rc;
2246}
2247
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002248/*! @} */