blob: e0bbcabf05fcf5b71bb9272e45e1e59e625ee639 [file] [log] [blame]
rootaf48bed2011-09-26 11:23:06 +02001/* LAPD core implementation */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2011 by Andreas Eversberg <jolly@eversberg.eu>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/*! \addtogroup lapd
25 * @{
26 */
27
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010028/*! \file lapd_core.c */
rootaf48bed2011-09-26 11:23:06 +020029
30/*!
31 * Notes on Buffering: rcv_buffer, tx_queue, tx_hist, send_buffer, send_queue
32 *
33 * RX data is stored in the rcv_buffer (pointer). If the message is complete, it
34 * is removed from rcv_buffer pointer and forwarded to L3. If the RX data is
35 * received while there is an incomplete rcv_buffer, it is appended to it.
36 *
37 * TX data is stored in the send_queue first. When transmitting a frame,
38 * the first message in the send_queue is moved to the send_buffer. There it
39 * resides until all fragments are acknowledged. Fragments to be sent by I
40 * frames are stored in the tx_hist buffer for resend, if required. Also the
41 * current fragment is copied into the tx_queue. There it resides until it is
42 * forwarded to layer 1.
43 *
44 * In case we have SAPI 0, we only have a window size of 1, so the unack-
45 * nowledged message resides always in the send_buffer. In case of a suspend,
46 * it can be written back to the first position of the send_queue.
47 *
48 * The layer 1 normally sends a PH-READY-TO-SEND. But because we use
49 * asynchronous transfer between layer 1 and layer 2 (serial link), we must
50 * send a frame before layer 1 reaches the right timeslot to send it. So we
51 * move the tx_queue to layer 1 when there is not already a pending frame, and
52 * wait until acknowledge after the frame has been sent. If we receive an
53 * acknowledge, we can send the next frame from the buffer, if any.
54 *
55 * The moving of tx_queue to layer 1 may also trigger T200, if desired. Also it
56 * will trigger next I frame, if possible.
57 *
58 * T203 is optional. It will be stated when entering MF EST state. It will also
59 * be started when I or S frame is received in that state . It will be
60 * restarted in the lapd_acknowledge() function, in case outstanding frames
61 * will not trigger T200. It will be stoped, when T200 is started in MF EST
62 * state. It will also be stoped when leaving MF EST state.
63 *
64 */
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>
77#include <arpa/inet.h>
78
79#include <osmocom/core/logging.h>
80#include <osmocom/core/timer.h>
81#include <osmocom/core/msgb.h>
82#include <osmocom/core/utils.h>
83#include <osmocom/core/talloc.h>
84#include <osmocom/gsm/lapd_core.h>
Max2f0b0c92017-01-12 16:47:13 +010085#include <osmocom/gsm/rsl.h>
rootaf48bed2011-09-26 11:23:06 +020086
87/* TS 04.06 Table 4 / Section 3.8.1 */
88#define LAPD_U_SABM 0x7
89#define LAPD_U_SABME 0xf
90#define LAPD_U_DM 0x3
91#define LAPD_U_UI 0x0
92#define LAPD_U_DISC 0x8
93#define LAPD_U_UA 0xC
94#define LAPD_U_FRMR 0x11
95
96#define LAPD_S_RR 0x0
97#define LAPD_S_RNR 0x1
98#define LAPD_S_REJ 0x2
99
100#define CR_USER2NET_CMD 0
101#define CR_USER2NET_RESP 1
102#define CR_NET2USER_CMD 1
103#define CR_NET2USER_RESP 0
104
105#define LAPD_HEADROOM 56
106
107#define SBIT(a) (1 << a)
108#define ALL_STATES 0xffffffff
109
Andreas Eversberg742fc792011-09-27 09:40:25 +0200110static void lapd_t200_cb(void *data);
111static void lapd_t203_cb(void *data);
112static int lapd_send_i(struct lapd_msg_ctx *lctx, int line);
113static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
114
rootaf48bed2011-09-26 11:23:06 +0200115/* UTILITY FUNCTIONS */
116
117struct msgb *lapd_msgb_alloc(int length, const char *name)
118{
119 /* adding space for padding, FIXME: add as an option */
120 if (length < 21)
121 length = 21;
122 return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name);
123}
124
125static inline uint8_t do_mod(uint8_t x, uint8_t m)
126{
127 return x & (m - 1);
128}
129
130static inline uint8_t inc_mod(uint8_t x, uint8_t m)
131{
132 return (x + 1) & (m - 1);
133}
134
135static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m)
136{
137 return (x + y) & (m - 1);
138}
139
140static inline uint8_t sub_mod(uint8_t x, uint8_t y, uint8_t m)
141{
142 return (x - y) & (m - 1); /* handle negative results correctly */
143}
144
145static void lapd_dl_flush_send(struct lapd_datalink *dl)
146{
147 struct msgb *msg;
148
149 /* Flush send-queue */
150 while ((msg = msgb_dequeue(&dl->send_queue)))
151 msgb_free(msg);
152
153 /* Clear send-buffer */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200154 msgb_free(dl->send_buffer);
155 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +0200156}
157
158static void lapd_dl_flush_hist(struct lapd_datalink *dl)
159{
160 unsigned int i;
161
Harald Weltef92e44c2016-08-01 00:24:19 +0200162 if (!dl->range_hist || !dl->tx_hist)
Harald Welte0ee90f82016-07-03 20:45:21 +0200163 return;
164
rootaf48bed2011-09-26 11:23:06 +0200165 for (i = 0; i < dl->range_hist; i++) {
166 if (dl->tx_hist[i].msg) {
167 msgb_free(dl->tx_hist[i].msg);
168 dl->tx_hist[i].msg = NULL;
169 }
170 }
171}
172
173static void lapd_dl_flush_tx(struct lapd_datalink *dl)
174{
175 struct msgb *msg;
176
177 while ((msg = msgb_dequeue(&dl->tx_queue)))
178 msgb_free(msg);
179 lapd_dl_flush_hist(dl);
180}
181
182/* Figure B.2/Q.921 */
Harald Weltec733d142017-03-15 10:20:51 +0100183const struct value_string lapd_state_names[] = {
184 OSMO_VALUE_STRING(LAPD_STATE_NULL),
185 OSMO_VALUE_STRING(LAPD_STATE_TEI_UNASS),
186 OSMO_VALUE_STRING(LAPD_STATE_ASS_TEI_WAIT),
187 OSMO_VALUE_STRING(LAPD_STATE_EST_TEI_WAIT),
188 OSMO_VALUE_STRING(LAPD_STATE_IDLE),
189 OSMO_VALUE_STRING(LAPD_STATE_SABM_SENT),
190 OSMO_VALUE_STRING(LAPD_STATE_DISC_SENT),
191 OSMO_VALUE_STRING(LAPD_STATE_MF_EST),
192 OSMO_VALUE_STRING(LAPD_STATE_TIMER_RECOV),
193 { 0, NULL }
rootaf48bed2011-09-26 11:23:06 +0200194};
195
Harald Weltec733d142017-03-15 10:20:51 +0100196static inline const char *lapd_state_name(enum lapd_state state)
197{
198 return get_value_string(lapd_state_names, state);
199}
200
Andreas Eversberg742fc792011-09-27 09:40:25 +0200201static void lapd_start_t200(struct lapd_datalink *dl)
202{
203 if (osmo_timer_pending(&dl->t200))
204 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100205 LOGP(DLLAPD, LOGL_INFO, "start T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200206 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
207}
208
209static void lapd_start_t203(struct lapd_datalink *dl)
210{
211 if (osmo_timer_pending(&dl->t203))
212 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100213 LOGP(DLLAPD, LOGL_INFO, "start T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200214 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
215}
216
217static void lapd_stop_t200(struct lapd_datalink *dl)
218{
219 if (!osmo_timer_pending(&dl->t200))
220 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100221 LOGP(DLLAPD, LOGL_INFO, "stop T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200222 osmo_timer_del(&dl->t200);
223}
224
225static void lapd_stop_t203(struct lapd_datalink *dl)
226{
227 if (!osmo_timer_pending(&dl->t203))
228 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100229 LOGP(DLLAPD, LOGL_INFO, "stop T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200230 osmo_timer_del(&dl->t203);
231}
232
rootaf48bed2011-09-26 11:23:06 +0200233static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
234{
Philipp Maier08177d32016-12-08 17:23:26 +0100235 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100236 lapd_state_name(dl->state), lapd_state_name(state), dl);
rootaf48bed2011-09-26 11:23:06 +0200237
238 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
239 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200240 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200241 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200242 msgb_free(dl->cont_res);
243 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200244 }
245
246 /* start T203 on entering MF EST state, if enabled */
247 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200248 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
249 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200250
251 dl->state = state;
252}
253
rootaf48bed2011-09-26 11:23:06 +0200254static void *tall_lapd_ctx = NULL;
255
256/* init datalink instance and allocate history */
257void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
258 int maxf)
259{
260 int m;
261
262 memset(dl, 0, sizeof(*dl));
263 INIT_LLIST_HEAD(&dl->send_queue);
264 INIT_LLIST_HEAD(&dl->tx_queue);
265 dl->reestablish = 1;
266 dl->n200_est_rel = 3;
267 dl->n200 = 3;
268 dl->t200_sec = 1;
269 dl->t200_usec = 0;
270 dl->t200.data = dl;
271 dl->t200.cb = &lapd_t200_cb;
272 dl->t203_sec = 10;
273 dl->t203_usec = 0;
274 dl->t203.data = dl;
275 dl->t203.cb = &lapd_t203_cb;
276 dl->maxf = maxf;
277 if (k > v_range - 1)
278 k = v_range - 1;
279 dl->k = k;
280 dl->v_range = v_range;
281
282 /* Calculate modulo for history array:
283 * - The history range must be at least k+1.
284 * - The history range must be 2^x, where x is as low as possible.
285 */
286 k++;
287 for (m = 0x80; m; m >>= 1) {
288 if ((m & k)) {
289 if (k > m)
290 m <<= 1;
291 dl->range_hist = m;
292 break;
293 }
294 }
295
296 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
Philipp Maier08177d32016-12-08 17:23:26 +0100297 "history range = %d (dl=%p)\n", dl->v_range, dl->k,
298 dl->range_hist, dl);
rootaf48bed2011-09-26 11:23:06 +0200299
300 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
301
302 if (!tall_lapd_ctx)
303 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100304 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
305 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200306}
307
308/* reset to IDLE state */
309void lapd_dl_reset(struct lapd_datalink *dl)
310{
rootaf48bed2011-09-26 11:23:06 +0200311 /* flush buffer */
312 lapd_dl_flush_tx(dl);
313 lapd_dl_flush_send(dl);
314 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200315 msgb_free(dl->rcv_buffer);
316 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200317 /* stop Timers */
318 lapd_stop_t200(dl);
319 lapd_stop_t203(dl);
Jean-Francois Dionned78c9732017-03-06 14:33:20 -0500320 if (dl->state == LAPD_STATE_IDLE)
321 return;
322 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance (dl=%p)\n", dl);
323 /* enter idle state (and remove eventual cont_res) */
324 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
rootaf48bed2011-09-26 11:23:06 +0200325}
326
327/* reset and de-allocate history buffer */
328void lapd_dl_exit(struct lapd_datalink *dl)
329{
330 /* free all ressources except history buffer */
331 lapd_dl_reset(dl);
332 /* free history buffer list */
333 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200334 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200335}
336
337/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
338int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
339{
340 switch (mode) {
341 case LAPD_MODE_USER:
342 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
343 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
344 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
345 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
346 break;
347 case LAPD_MODE_NETWORK:
348 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
349 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
350 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
351 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
352 break;
353 default:
354 return -EINVAL;
355 }
356 dl->mode = mode;
357
358 return 0;
359}
360
361/* send DL message with optional msgb */
362static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
363 struct msgb *msg)
364{
365 struct lapd_datalink *dl = lctx->dl;
366 struct osmo_dlsap_prim dp;
367
368 osmo_prim_init(&dp.oph, 0, prim, op, msg);
369 return dl->send_dlsap(&dp, lctx);
370}
371
372/* send simple DL message */
373static inline int send_dl_simple(uint8_t prim, uint8_t op,
374 struct lapd_msg_ctx *lctx)
375{
376 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
377
378 return send_dl_l3(prim, op, lctx, msg);
379}
380
381/* send MDL-ERROR INDICATION */
382static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
383{
384 struct lapd_datalink *dl = lctx->dl;
385 struct osmo_dlsap_prim dp;
386
Philipp Maier08177d32016-12-08 17:23:26 +0100387 LOGP(DLLAPD, LOGL_NOTICE,
388 "sending MDL-ERROR-IND cause %d from state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100389 cause, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200390 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
391 dp.u.error_ind.cause = cause;
392 return dl->send_dlsap(&dp, lctx);
393}
394
395/* send UA response */
396static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
397{
398 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
399 struct lapd_msg_ctx nctx;
400 struct lapd_datalink *dl = lctx->dl;
401
402 memcpy(&nctx, lctx, sizeof(nctx));
403 msg->l3h = msgb_put(msg, len);
404 if (len)
405 memcpy(msg->l3h, data, len);
406 /* keep nctx.ldp */
407 /* keep nctx.sapi */
408 /* keep nctx.tei */
409 nctx.cr = dl->cr.loc2rem.resp;
410 nctx.format = LAPD_FORM_U;
411 nctx.s_u = LAPD_U_UA;
412 /* keep nctx.p_f */
413 nctx.length = len;
414 nctx.more = 0;
415
416 return dl->send_ph_data_req(&nctx, msg);
417}
418
419/* send DM response */
420static int lapd_send_dm(struct lapd_msg_ctx *lctx)
421{
422 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
423 struct lapd_msg_ctx nctx;
424 struct lapd_datalink *dl = lctx->dl;
425
426 memcpy(&nctx, lctx, sizeof(nctx));
427 /* keep nctx.ldp */
428 /* keep nctx.sapi */
429 /* keep nctx.tei */
430 nctx.cr = dl->cr.loc2rem.resp;
431 nctx.format = LAPD_FORM_U;
432 nctx.s_u = LAPD_U_DM;
433 /* keep nctx.p_f */
434 nctx.length = 0;
435 nctx.more = 0;
436
437 return dl->send_ph_data_req(&nctx, msg);
438}
439
440/* send RR response / command */
441static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
442{
443 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
444 struct lapd_msg_ctx nctx;
445 struct lapd_datalink *dl = lctx->dl;
446
447 memcpy(&nctx, lctx, sizeof(nctx));
448 /* keep nctx.ldp */
449 /* keep nctx.sapi */
450 /* keep nctx.tei */
451 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
452 nctx.format = LAPD_FORM_S;
453 nctx.s_u = LAPD_S_RR;
454 nctx.p_f = f_bit;
455 nctx.n_recv = dl->v_recv;
456 nctx.length = 0;
457 nctx.more = 0;
458
459 return dl->send_ph_data_req(&nctx, msg);
460}
461
462/* send RNR response / command */
463static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
464{
465 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
466 struct lapd_msg_ctx nctx;
467 struct lapd_datalink *dl = lctx->dl;
468
469 memcpy(&nctx, lctx, sizeof(nctx));
470 /* keep nctx.ldp */
471 /* keep nctx.sapi */
472 /* keep nctx.tei */
473 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
474 nctx.format = LAPD_FORM_S;
475 nctx.s_u = LAPD_S_RNR;
476 nctx.p_f = f_bit;
477 nctx.n_recv = dl->v_recv;
478 nctx.length = 0;
479 nctx.more = 0;
480
481 return dl->send_ph_data_req(&nctx, msg);
482}
483
484/* send REJ response */
485static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
486{
487 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
488 struct lapd_msg_ctx nctx;
489 struct lapd_datalink *dl = lctx->dl;
490
491 memcpy(&nctx, lctx, sizeof(nctx));
492 /* keep nctx.ldp */
493 /* keep nctx.sapi */
494 /* keep nctx.tei */
495 nctx.cr = dl->cr.loc2rem.resp;
496 nctx.format = LAPD_FORM_S;
497 nctx.s_u = LAPD_S_REJ;
498 nctx.p_f = f_bit;
499 nctx.n_recv = dl->v_recv;
500 nctx.length = 0;
501 nctx.more = 0;
502
503 return dl->send_ph_data_req(&nctx, msg);
504}
505
506/* resend SABM or DISC message */
507static int lapd_send_resend(struct lapd_datalink *dl)
508{
509 struct msgb *msg;
510 uint8_t h = do_mod(dl->v_send, dl->range_hist);
511 int length = dl->tx_hist[h].msg->len;
512 struct lapd_msg_ctx nctx;
513
514 /* assemble message */
515 memcpy(&nctx, &dl->lctx, sizeof(nctx));
516 /* keep nctx.ldp */
517 /* keep nctx.sapi */
518 /* keep nctx.tei */
519 nctx.cr = dl->cr.loc2rem.cmd;
520 nctx.format = LAPD_FORM_U;
521 if (dl->state == LAPD_STATE_SABM_SENT)
522 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
523 else
524 nctx.s_u = LAPD_U_DISC;
525 nctx.p_f = 1;
526 nctx.length = length;
527 nctx.more = 0;
528
529 /* Resend SABM/DISC from tx_hist */
530 msg = lapd_msgb_alloc(length, "LAPD resend");
531 msg->l3h = msgb_put(msg, length);
532 if (length)
533 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
534
535 return dl->send_ph_data_req(&nctx, msg);
536}
537
538/* reestablish link */
539static int lapd_reestablish(struct lapd_datalink *dl)
540{
541 struct osmo_dlsap_prim dp;
542 struct msgb *msg;
543
Philipp Maier08177d32016-12-08 17:23:26 +0100544 LOGP(DLLAPD, LOGL_DEBUG, "lapd reestablish (dl=%p)\n", dl);
545
rootaf48bed2011-09-26 11:23:06 +0200546 msg = lapd_msgb_alloc(0, "DUMMY");
547 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
548
549 return lapd_est_req(&dp, &dl->lctx);
550}
551
552/* Timer callback on T200 expiry */
553static void lapd_t200_cb(void *data)
554{
555 struct lapd_datalink *dl = data;
556
Philipp Maier08177d32016-12-08 17:23:26 +0100557 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100558 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200559
560 switch (dl->state) {
561 case LAPD_STATE_SABM_SENT:
562 /* 5.4.1.3 */
563 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200564 /* flush tx and send buffers */
565 lapd_dl_flush_tx(dl);
566 lapd_dl_flush_send(dl);
567 /* go back to idle state */
568 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
569 /* NOTE: we must not change any other states or buffers
570 * and queues, since we may reconnect after handover
571 * failure. the buffered messages is replaced there */
Philipp Maier6b986c22017-02-01 12:00:45 +0100572 /* send MDL ERROR INIDCATION to L3 */
573 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maierd9f61292016-12-08 10:45:06 +0100574 /* send RELEASE INDICATION to L3 */
575 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
576 &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200577 break;
578 }
579 /* retransmit SABM command */
580 lapd_send_resend(dl);
581 /* increment re-transmission counter */
582 dl->retrans_ctr++;
583 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200584 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200585 break;
586 case LAPD_STATE_DISC_SENT:
587 /* 5.4.4.3 */
588 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200589 /* send MDL ERROR INIDCATION to L3 */
590 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
Philipp Maier6b986c22017-02-01 12:00:45 +0100591 /* send RELEASE INDICATION to L3 */
592 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200593 /* flush tx and send buffers */
594 lapd_dl_flush_tx(dl);
595 lapd_dl_flush_send(dl);
596 /* go back to idle state */
597 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
598 /* NOTE: we must not change any other states or buffers
599 * and queues, since we may reconnect after handover
600 * failure. the buffered messages is replaced there */
601 break;
602 }
603 /* retransmit DISC command */
604 lapd_send_resend(dl);
605 /* increment re-transmission counter */
606 dl->retrans_ctr++;
607 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200608 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200609 break;
610 case LAPD_STATE_MF_EST:
611 /* 5.5.7 */
612 dl->retrans_ctr = 0;
613 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
614 /* fall through */
615 case LAPD_STATE_TIMER_RECOV:
616 dl->retrans_ctr++;
617 if (dl->retrans_ctr < dl->n200) {
618 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
619 uint8_t h = do_mod(vs, dl->range_hist);
620 /* retransmit I frame (V_s-1) with P=1, if any */
621 if (dl->tx_hist[h].msg) {
622 struct msgb *msg;
623 int length = dl->tx_hist[h].msg->len;
624 struct lapd_msg_ctx nctx;
625
626 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
Philipp Maier08177d32016-12-08 17:23:26 +0100627 " V(S)=%d (dl=%p)\n", vs, dl);
rootaf48bed2011-09-26 11:23:06 +0200628 /* Create I frame (segment) from tx_hist */
629 memcpy(&nctx, &dl->lctx, sizeof(nctx));
630 /* keep nctx.ldp */
631 /* keep nctx.sapi */
632 /* keep nctx.tei */
633 nctx.cr = dl->cr.loc2rem.cmd;
634 nctx.format = LAPD_FORM_I;
635 nctx.p_f = 1;
636 nctx.n_send = vs;
637 nctx.n_recv = dl->v_recv;
638 nctx.length = length;
639 nctx.more = dl->tx_hist[h].more;
640 msg = lapd_msgb_alloc(length, "LAPD I resend");
641 msg->l3h = msgb_put(msg, length);
642 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
643 length);
644 dl->send_ph_data_req(&nctx, msg);
645 } else {
646 /* OR send appropriate supervision frame with P=1 */
647 if (!dl->own_busy && !dl->seq_err_cond) {
648 lapd_send_rr(&dl->lctx, 1, 1);
649 /* NOTE: In case of sequence error
650 * condition, the REJ frame has been
651 * transmitted when entering the
652 * condition, so it has not be done
653 * here
654 */
655 } else if (dl->own_busy) {
656 lapd_send_rnr(&dl->lctx, 1, 1);
657 } else {
658 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
Philipp Maier08177d32016-12-08 17:23:26 +0100659 "pls. fix (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200660 }
661 }
662 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200663 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200664 } else {
665 /* send MDL ERROR INIDCATION to L3 */
666 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
667 /* reestablish */
668 if (!dl->reestablish)
669 break;
670 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100671 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200672 lapd_reestablish(dl);
673 }
674 break;
675 default:
676 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
Harald Weltec733d142017-03-15 10:20:51 +0100677 "dl->state %s (dl=%p)\n", lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200678 }
679}
680
681/* Timer callback on T203 expiry */
682static void lapd_t203_cb(void *data)
683{
684 struct lapd_datalink *dl = data;
685
Philipp Maier08177d32016-12-08 17:23:26 +0100686 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 state=%s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100687 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200688
689 if (dl->state != LAPD_STATE_MF_EST) {
690 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
Philipp Maier08177d32016-12-08 17:23:26 +0100691 "please fix! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200692 return;
693 }
694
695 /* set retransmission counter to 0 */
696 dl->retrans_ctr = 0;
697 /* enter timer recovery state */
698 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
699 /* transmit a supervisory command with P bit set to 1 as follows: */
700 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +0100701 LOGP(DLLAPD, LOGL_INFO,
702 "transmit an RR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200703 /* Send RR with P=1 */
704 lapd_send_rr(&dl->lctx, 1, 1);
705 } else {
Philipp Maier08177d32016-12-08 17:23:26 +0100706 LOGP(DLLAPD, LOGL_INFO,
707 "transmit an RNR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200708 /* Send RNR with P=1 */
709 lapd_send_rnr(&dl->lctx, 1, 1);
710 }
711 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200712 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200713}
714
715/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
716static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
717{
718 struct lapd_datalink *dl = lctx->dl;
719 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100720 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200721 int i, h;
722
723 /* supervisory frame ? */
724 if (lctx->format == LAPD_FORM_S)
725 s = 1;
726 /* REJ frame ? */
727 if (s && lctx->s_u == LAPD_S_REJ)
728 rej = 1;
729
730 /* Flush all transmit buffers of acknowledged frames */
731 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
732 h = do_mod(i, dl->range_hist);
733 if (dl->tx_hist[h].msg) {
734 msgb_free(dl->tx_hist[h].msg);
735 dl->tx_hist[h].msg = NULL;
736 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
737 }
738 }
739
740 if (dl->state != LAPD_STATE_TIMER_RECOV) {
741 /* When not in the timer recovery condition, the data
742 * link layer entity shall reset the timer T200 on
743 * receipt of a valid I frame with N(R) higher than V(A),
744 * or an REJ with an N(R) equal to V(A). */
745 if ((!rej && nr != dl->v_ack)
746 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200747 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200748 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200749 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
750 }
751 /* 5.7.4: N(R) sequence error
752 * N(R) is called valid, if and only if
753 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
754 */
755 if (sub_mod(nr, dl->v_ack, dl->v_range)
756 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Philipp Maier08177d32016-12-08 17:23:26 +0100757 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200758 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
759 }
760 }
761
762 /* V(A) shall be set to the value of N(R) */
763 dl->v_ack = nr;
764
Andreas Eversberg742fc792011-09-27 09:40:25 +0200765 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200766 * and if there are outstanding I frames, restart T200 */
767 if (t200_reset && !rej) {
768 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
769 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
Philipp Maier08177d32016-12-08 17:23:26 +0100770 "frame(s) (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200771 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200772 }
773 }
774
775 /* This also does a restart, when I or S frame is received */
776
777 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200778 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200779 /* Start T203, if T200 is not running in MF EST state, if enabled */
780 if (!osmo_timer_pending(&dl->t200)
781 && (dl->t203_sec || dl->t203_usec)
782 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200783 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200784 }
785}
786
787/* L1 -> L2 */
788
789/* Receive a LAPD U (Unnumbered) message from L1 */
790static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
791{
792 struct lapd_datalink *dl = lctx->dl;
793 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100794 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200795 uint8_t prim, op;
796
797 switch (lctx->s_u) {
798 case LAPD_U_SABM:
799 case LAPD_U_SABME:
800 prim = PRIM_DL_EST;
801 op = PRIM_OP_INDICATION;
802
Philipp Maier08177d32016-12-08 17:23:26 +0100803 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100804 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200805 /* 5.7.1 */
806 dl->seq_err_cond = 0;
807 /* G.2.2 Wrong value of the C/R bit */
808 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +0100809 LOGP(DLLAPD, LOGL_ERROR,
810 "SABM response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200811 msgb_free(msg);
812 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
813 return -EINVAL;
814 }
815
816 /* G.4.5 If SABM is received with L>N201 or with M bit
817 * set, AN MDL-ERROR-INDICATION is sent to MM.
818 */
819 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +0100820 LOGP(DLLAPD, LOGL_ERROR,
821 "SABM too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200822 msgb_free(msg);
823 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
824 return -EIO;
825 }
826
827 switch (dl->state) {
828 case LAPD_STATE_IDLE:
829 break;
830 case LAPD_STATE_MF_EST:
831 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
Philipp Maier08177d32016-12-08 17:23:26 +0100832 "frame established state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200833 /* If link is lost on the remote side, we start over
834 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100835 /* Additionally, continue in case of content resoltion
836 * (GSM network). This happens, if the mobile has not
837 * yet received UA or another mobile (collision) tries
838 * to establish connection. The mobile must receive
839 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200840 /* 5.4.2.1 */
841 if (!length) {
842 /* If no content resolution, this is a
843 * re-establishment. */
844 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +0100845 "Remote reestablish (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200846 break;
847 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200848 if (!dl->cont_res) {
849 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Philipp Maier08177d32016-12-08 17:23:26 +0100850 "allowed in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100851 lapd_state_name(dl->state), dl);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200852 mdl_error(MDL_CAUSE_SABM_MF, lctx);
853 msgb_free(msg);
854 return 0;
855 }
rootaf48bed2011-09-26 11:23:06 +0200856 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200857 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200858#ifdef TEST_CONTENT_RESOLUTION_NETWORK
859 dl->cont_res->data[0] ^= 0x01;
860#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100861 if (memcmp(dl->cont_res->data, msg->data,
862 length)) {
rootaf48bed2011-09-26 11:23:06 +0200863 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100864 "with different content - "
Philipp Maier08177d32016-12-08 17:23:26 +0100865 "ignoring! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200866 msgb_free(msg);
867 return 0;
868 }
869 }
870 /* send UA again */
871 lapd_send_ua(lctx, length, msg->l3h);
872 msgb_free(msg);
873 return 0;
874 case LAPD_STATE_DISC_SENT:
875 /* 5.4.6.2 send DM with F=P */
876 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200877 /* stop Timer T200 */
878 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200879 msgb_free(msg);
880 return send_dl_simple(prim, op, lctx);
881 default:
882 /* collision: Send UA, but still wait for rx UA, then
883 * change to MF_EST state.
884 */
885 /* check for contention resoultion */
886 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
887 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Philipp Maier08177d32016-12-08 17:23:26 +0100888 "during contention resolution (state=%s, dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100889 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200890 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
891 }
892 lapd_send_ua(lctx, length, msg->l3h);
893 msgb_free(msg);
894 return 0;
895 }
896 /* save message context for further use */
897 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
898#ifndef TEST_CONTENT_RESOLUTION_NETWORK
899 /* send UA response */
900 lapd_send_ua(lctx, length, msg->l3h);
901#endif
902 /* set Vs, Vr and Va to 0 */
903 dl->v_send = dl->v_recv = dl->v_ack = 0;
904 /* clear tx_hist */
905 lapd_dl_flush_hist(dl);
906 /* enter multiple-frame-established state */
907 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
908 /* store content resolution data on network side
909 * Note: cont_res will be removed when changing state again,
910 * so it must be allocated AFTER lapd_dl_newstate(). */
911 if (dl->mode == LAPD_MODE_NETWORK && length) {
912 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
913 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
914 length);
Philipp Maier08177d32016-12-08 17:23:26 +0100915 LOGP(DLLAPD, LOGL_NOTICE,
916 "Store content res. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200917 }
918 /* send notification to L3 */
919 if (length == 0) {
920 /* 5.4.1.2 Normal establishment procedures */
921 rc = send_dl_simple(prim, op, lctx);
922 msgb_free(msg);
923 } else {
924 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200925 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200926 rc = send_dl_l3(prim, op, lctx, msg);
927 }
928 break;
929 case LAPD_U_DM:
Philipp Maier08177d32016-12-08 17:23:26 +0100930 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +0100931 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +0200932 /* G.2.2 Wrong value of the C/R bit */
933 if (lctx->cr == dl->cr.rem2loc.cmd) {
Philipp Maier08177d32016-12-08 17:23:26 +0100934 LOGP(DLLAPD, LOGL_ERROR,
935 "DM command error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200936 msgb_free(msg);
937 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
938 return -EINVAL;
939 }
940 if (!lctx->p_f) {
941 /* 5.4.1.2 DM responses with the F bit set to "0"
942 * shall be ignored.
943 */
944 msgb_free(msg);
945 return 0;
946 }
947 switch (dl->state) {
948 case LAPD_STATE_SABM_SENT:
949 break;
950 case LAPD_STATE_MF_EST:
951 if (lctx->p_f) {
952 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
Philipp Maier08177d32016-12-08 17:23:26 +0100953 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200954 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
955 } else {
956 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
957 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100958 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200959 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
960 /* reestablish */
961 if (!dl->reestablish) {
962 msgb_free(msg);
963 return 0;
964 }
965 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100966 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200967 lapd_reestablish(dl);
968 }
969 msgb_free(msg);
970 return 0;
971 case LAPD_STATE_TIMER_RECOV:
972 /* FP = 0 (DM is normal in case PF = 1) */
973 if (!lctx->p_f) {
974 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
975 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100976 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200977 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
978 msgb_free(msg);
979 /* reestablish */
980 if (!dl->reestablish)
981 return 0;
982 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100983 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200984 return lapd_reestablish(dl);
985 }
986 break;
987 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200988 /* stop Timer T200 */
989 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200990 /* go to idle state */
991 lapd_dl_flush_tx(dl);
992 lapd_dl_flush_send(dl);
993 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
994 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
995 msgb_free(msg);
996 return 0;
997 case LAPD_STATE_IDLE:
998 /* 5.4.5 all other frame types shall be discarded */
999 default:
1000 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001001 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001002 msgb_free(msg);
1003 return 0;
1004 }
Andreas Eversberg742fc792011-09-27 09:40:25 +02001005 /* stop timer T200 */
1006 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001007 /* go to idle state */
1008 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1009 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1010 msgb_free(msg);
1011 break;
1012 case LAPD_U_UI:
Philipp Maier08177d32016-12-08 17:23:26 +01001013 LOGP(DLLAPD, LOGL_INFO, "UI received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001014 /* G.2.2 Wrong value of the C/R bit */
1015 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001016 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
Philipp Maier08177d32016-12-08 17:23:26 +01001017 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001018 msgb_free(msg);
1019 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1020 return -EINVAL;
1021 }
1022
1023 /* G.4.5 If UI is received with L>N201 or with M bit
1024 * set, AN MDL-ERROR-INDICATION is sent to MM.
1025 */
1026 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001027 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
Philipp Maier08177d32016-12-08 17:23:26 +01001028 "(%d > N201(%d) or M=%d) (dl=%p)\n", length,
1029 lctx->n201, lctx->more, dl);
rootaf48bed2011-09-26 11:23:06 +02001030 msgb_free(msg);
1031 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1032 return -EIO;
1033 }
1034
1035 /* do some length checks */
1036 if (length == 0) {
1037 /* 5.3.3 UI frames received with the length indicator
1038 * set to "0" shall be ignored
1039 */
Philipp Maier08177d32016-12-08 17:23:26 +01001040 LOGP(DLLAPD, LOGL_INFO,
1041 "length=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001042 msgb_free(msg);
1043 return 0;
1044 }
Harald Welte087116a2013-06-18 21:41:34 +02001045 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001046 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1047 msg);
1048 break;
1049 case LAPD_U_DISC:
1050 prim = PRIM_DL_REL;
1051 op = PRIM_OP_INDICATION;
1052
Philipp Maier08177d32016-12-08 17:23:26 +01001053 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001054 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001055 /* flush tx and send buffers */
1056 lapd_dl_flush_tx(dl);
1057 lapd_dl_flush_send(dl);
1058 /* 5.7.1 */
1059 dl->seq_err_cond = 0;
1060 /* G.2.2 Wrong value of the C/R bit */
1061 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001062 LOGP(DLLAPD, LOGL_ERROR,
1063 "DISC response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001064 msgb_free(msg);
1065 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1066 return -EINVAL;
1067 }
1068 if (length > 0 || lctx->more) {
1069 /* G.4.4 If a DISC or DM frame is received with L>0 or
1070 * with the M bit set to "1", an MDL-ERROR-INDICATION
1071 * primitive with cause "U frame with incorrect
1072 * parameters" is sent to the mobile management entity.
1073 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001074 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001075 "U frame iwth incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001076 msgb_free(msg);
1077 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1078 return -EIO;
1079 }
1080 switch (dl->state) {
1081 case LAPD_STATE_IDLE:
Philipp Maier08177d32016-12-08 17:23:26 +01001082 LOGP(DLLAPD, LOGL_INFO,
1083 "DISC in idle state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001084 /* send DM with F=P */
1085 msgb_free(msg);
1086 return lapd_send_dm(lctx);
1087 case LAPD_STATE_SABM_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001088 LOGP(DLLAPD, LOGL_INFO,
1089 "DISC in SABM state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001090 /* 5.4.6.2 send DM with F=P */
1091 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001092 /* stop Timer T200 */
1093 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001094 /* go to idle state */
1095 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1096 msgb_free(msg);
1097 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1098 lctx);
1099 case LAPD_STATE_MF_EST:
1100 case LAPD_STATE_TIMER_RECOV:
Philipp Maier08177d32016-12-08 17:23:26 +01001101 LOGP(DLLAPD, LOGL_INFO,
1102 "DISC in est state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001103 break;
1104 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001105 LOGP(DLLAPD, LOGL_INFO,
1106 "DISC in disc state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001107 prim = PRIM_DL_REL;
1108 op = PRIM_OP_CONFIRM;
1109 break;
1110 default:
1111 lapd_send_ua(lctx, length, msg->l3h);
1112 msgb_free(msg);
1113 return 0;
1114 }
1115 /* send UA response */
1116 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001117 /* stop Timer T200 */
1118 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001119 /* enter idle state, keep tx-buffer with UA response */
1120 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1121 /* send notification to L3 */
1122 rc = send_dl_simple(prim, op, lctx);
1123 msgb_free(msg);
1124 break;
1125 case LAPD_U_UA:
Philipp Maier08177d32016-12-08 17:23:26 +01001126 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001127 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001128 /* G.2.2 Wrong value of the C/R bit */
1129 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001130 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
Philipp Maier08177d32016-12-08 17:23:26 +01001131 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001132 msgb_free(msg);
1133 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1134 return -EINVAL;
1135 }
1136
1137 /* G.4.5 If UA is received with L>N201 or with M bit
1138 * set, AN MDL-ERROR-INDICATION is sent to MM.
1139 */
1140 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001141 LOGP(DLLAPD, LOGL_ERROR,
1142 "UA too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001143 msgb_free(msg);
1144 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1145 return -EIO;
1146 }
1147
1148 if (!lctx->p_f) {
1149 /* 5.4.1.2 A UA response with the F bit set to "0"
1150 * shall be ignored.
1151 */
Philipp Maier08177d32016-12-08 17:23:26 +01001152 LOGP(DLLAPD, LOGL_INFO,
1153 "F=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001154 msgb_free(msg);
1155 return 0;
1156 }
1157 switch (dl->state) {
1158 case LAPD_STATE_SABM_SENT:
1159 break;
1160 case LAPD_STATE_MF_EST:
1161 case LAPD_STATE_TIMER_RECOV:
1162 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001163 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001164 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1165 msgb_free(msg);
1166 return 0;
1167 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001168 LOGP(DLLAPD, LOGL_INFO,
1169 "UA in disconnect state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001170 /* stop Timer T200 */
1171 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001172 /* go to idle state */
1173 lapd_dl_flush_tx(dl);
1174 lapd_dl_flush_send(dl);
1175 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1176 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1177 msgb_free(msg);
1178 return 0;
1179 case LAPD_STATE_IDLE:
1180 /* 5.4.5 all other frame types shall be discarded */
1181 default:
1182 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001183 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001184 msgb_free(msg);
1185 return 0;
1186 }
Philipp Maier08177d32016-12-08 17:23:26 +01001187 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001188 /* stop Timer T200 */
1189 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001190 /* compare UA with SABME if contention resolution is applied */
1191 if (dl->tx_hist[0].msg->len) {
1192 if (length != (dl->tx_hist[0].msg->len)
1193 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1194 length)) {
1195 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
Philipp Maier08177d32016-12-08 17:23:26 +01001196 "mismatches **** (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001197 rc = send_dl_simple(PRIM_DL_REL,
1198 PRIM_OP_INDICATION, lctx);
1199 msgb_free(msg);
1200 /* go to idle state */
1201 lapd_dl_flush_tx(dl);
1202 lapd_dl_flush_send(dl);
1203 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1204 return 0;
1205 }
1206 }
1207 /* set Vs, Vr and Va to 0 */
1208 dl->v_send = dl->v_recv = dl->v_ack = 0;
1209 /* clear tx_hist */
1210 lapd_dl_flush_hist(dl);
1211 /* enter multiple-frame-established state */
1212 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1213 /* send outstanding frames, if any (resume / reconnect) */
1214 lapd_send_i(lctx, __LINE__);
1215 /* send notification to L3 */
1216 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1217 msgb_free(msg);
1218 break;
1219 case LAPD_U_FRMR:
Philipp Maier08177d32016-12-08 17:23:26 +01001220 LOGP(DLLAPD, LOGL_NOTICE,
1221 "Frame reject received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001222 /* send MDL ERROR INIDCATION to L3 */
1223 mdl_error(MDL_CAUSE_FRMR, lctx);
1224 msgb_free(msg);
1225 /* reestablish */
1226 if (!dl->reestablish)
1227 break;
Philipp Maier08177d32016-12-08 17:23:26 +01001228 LOGP(DLLAPD, LOGL_NOTICE,
1229 "Performing reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001230 rc = lapd_reestablish(dl);
1231 break;
1232 default:
1233 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001234 LOGP(DLLAPD, LOGL_NOTICE,
1235 "Unnumbered frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001236 msgb_free(msg);
1237 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1238 return -EINVAL;
1239 }
1240 return rc;
1241}
1242
1243/* Receive a LAPD S (Supervisory) message from L1 */
1244static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1245{
1246 struct lapd_datalink *dl = lctx->dl;
1247 int length = lctx->length;
1248
1249 if (length > 0 || lctx->more) {
1250 /* G.4.3 If a supervisory frame is received with L>0 or
1251 * with the M bit set to "1", an MDL-ERROR-INDICATION
1252 * primitive with cause "S frame with incorrect
1253 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001254 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001255 "S frame with incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001256 msgb_free(msg);
1257 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1258 return -EIO;
1259 }
1260
1261 if (lctx->cr == dl->cr.rem2loc.resp
1262 && lctx->p_f
1263 && dl->state != LAPD_STATE_TIMER_RECOV) {
1264 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001265 LOGP(DLLAPD, LOGL_NOTICE,
1266 "S frame response with F=1 error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001267 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1268 }
1269
1270 switch (dl->state) {
1271 case LAPD_STATE_IDLE:
1272 /* if P=1, respond DM with F=1 (5.2.2) */
1273 /* 5.4.5 all other frame types shall be discarded */
1274 if (lctx->p_f)
1275 lapd_send_dm(lctx); /* F=P */
1276 /* fall though */
1277 case LAPD_STATE_SABM_SENT:
1278 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001279 LOGP(DLLAPD, LOGL_NOTICE,
1280 "S frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001281 msgb_free(msg);
1282 return 0;
1283 }
1284 switch (lctx->s_u) {
1285 case LAPD_S_RR:
Philipp Maier08177d32016-12-08 17:23:26 +01001286 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001287 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001288 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1289 lapd_acknowledge(lctx);
1290
1291 /* 5.5.3.2 */
1292 if (lctx->cr == dl->cr.rem2loc.cmd
1293 && lctx->p_f) {
1294 if (!dl->own_busy && !dl->seq_err_cond) {
1295 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001296 "with polling bit set and we are not "
1297 "busy, so we reply with RR frame "
1298 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001299 lapd_send_rr(lctx, 1, 0);
1300 /* NOTE: In case of sequence error condition,
1301 * the REJ frame has been transmitted when
1302 * entering the condition, so it has not be
1303 * done here
1304 */
1305 } else if (dl->own_busy) {
1306 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001307 "with polling bit set and we are busy, "
1308 "so we reply with RR frame response (dl=%p)\n",
1309 dl);
rootaf48bed2011-09-26 11:23:06 +02001310 lapd_send_rnr(lctx, 1, 0);
1311 }
1312 } else if (lctx->cr == dl->cr.rem2loc.resp
1313 && lctx->p_f
1314 && dl->state == LAPD_STATE_TIMER_RECOV) {
1315 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1316 "and we are in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001317 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001318 /* V(S) to the N(R) in the RR frame */
1319 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001320 /* stop Timer T200 */
1321 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001322 /* 5.5.7 Clear timer recovery condition */
1323 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1324 }
1325 /* Send message, if possible due to acknowledged data */
1326 lapd_send_i(lctx, __LINE__);
1327
1328 break;
1329 case LAPD_S_RNR:
Philipp Maier08177d32016-12-08 17:23:26 +01001330 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001331 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001332 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1333 lapd_acknowledge(lctx);
1334
1335 /* 5.5.5 */
1336 /* Set peer receiver busy condition */
1337 dl->peer_busy = 1;
1338
1339 if (lctx->p_f) {
1340 if (lctx->cr == dl->cr.rem2loc.cmd) {
1341 if (!dl->own_busy) {
1342 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1343 "command and we are not busy, "
1344 "so we reply with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001345 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001346 /* Send RR with F=1 */
1347 lapd_send_rr(lctx, 1, 0);
1348 } else {
1349 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1350 "command and we are busy, so "
1351 "we reply with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001352 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001353 /* Send RNR with F=1 */
1354 lapd_send_rnr(lctx, 1, 0);
1355 }
1356 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1357 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1358 "and we in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001359 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001360 /* 5.5.7 Clear timer recovery condition */
1361 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1362 /* V(S) to the N(R) in the RNR frame */
1363 dl->v_send = lctx->n_recv;
1364 }
1365 } else
1366 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
Philipp Maier08177d32016-12-08 17:23:26 +01001367 "received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001368
1369 /* Send message, if possible due to acknowledged data */
1370 lapd_send_i(lctx, __LINE__);
1371
1372 break;
1373 case LAPD_S_REJ:
Philipp Maier08177d32016-12-08 17:23:26 +01001374 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001375 lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02001376 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1377 lapd_acknowledge(lctx);
1378
1379 /* 5.5.4.1 */
1380 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1381 /* Clear an existing peer receiver busy condition */
1382 dl->peer_busy = 0;
1383 /* V(S) and V(A) to the N(R) in the REJ frame */
1384 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001385 /* stop Timer T200 */
1386 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001387 /* 5.5.3.2 */
1388 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1389 if (!dl->own_busy && !dl->seq_err_cond) {
1390 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1391 "command not in timer recovery "
1392 "state and not in own busy "
1393 "condition received, so we "
1394 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001395 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001396 lapd_send_rr(lctx, 1, 0);
1397 /* NOTE: In case of sequence error
1398 * condition, the REJ frame has been
1399 * transmitted when entering the
1400 * condition, so it has not be done
1401 * here
1402 */
1403 } else if (dl->own_busy) {
1404 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1405 "command not in timer recovery "
1406 "state and in own busy "
1407 "condition received, so we "
1408 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001409 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001410 lapd_send_rnr(lctx, 1, 0);
1411 }
1412 } else
1413 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1414 "polling command not in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001415 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001416 /* send MDL ERROR INIDCATION to L3 */
1417 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Philipp Maier08177d32016-12-08 17:23:26 +01001418 LOGP(DLLAPD, LOGL_ERROR,
1419 "unsolicited supervisory response! (dl=%p)\n",
1420 dl);
rootaf48bed2011-09-26 11:23:06 +02001421 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1422 }
1423
1424 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1425 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
Philipp Maier08177d32016-12-08 17:23:26 +01001426 "recovery state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001427 /* Clear an existing peer receiver busy condition */
1428 dl->peer_busy = 0;
1429 /* V(S) and V(A) to the N(R) in the REJ frame */
1430 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001431 /* stop Timer T200 */
1432 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001433 /* 5.5.7 Clear timer recovery condition */
1434 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1435 } else {
1436 /* Clear an existing peer receiver busy condition */
1437 dl->peer_busy = 0;
1438 /* V(S) and V(A) to the N(R) in the REJ frame */
1439 dl->v_send = dl->v_ack = lctx->n_recv;
1440 /* 5.5.3.2 */
1441 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1442 if (!dl->own_busy && !dl->seq_err_cond) {
1443 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1444 "command in timer recovery "
1445 "state and not in own busy "
1446 "condition received, so we "
1447 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001448 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001449 lapd_send_rr(lctx, 1, 0);
1450 /* NOTE: In case of sequence error
1451 * condition, the REJ frame has been
1452 * transmitted when entering the
1453 * condition, so it has not be done
1454 * here
1455 */
1456 } else if (dl->own_busy) {
1457 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1458 "command in timer recovery "
1459 "state and in own busy "
1460 "condition received, so we "
1461 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001462 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001463 lapd_send_rnr(lctx, 1, 0);
1464 }
1465 } else
1466 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1467 "polling command in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001468 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001469 }
1470
1471 /* FIXME: 5.5.4.2 2) */
1472
1473 /* Send message, if possible due to acknowledged data */
1474 lapd_send_i(lctx, __LINE__);
1475
1476 break;
1477 default:
1478 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001479 LOGP(DLLAPD, LOGL_ERROR,
1480 "Supervisory frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001481 msgb_free(msg);
1482 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1483 return -EINVAL;
1484 }
1485 msgb_free(msg);
1486 return 0;
1487}
1488
1489/* Receive a LAPD I (Information) message from L1 */
1490static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1491{
1492 struct lapd_datalink *dl = lctx->dl;
1493 //uint8_t nr = lctx->n_recv;
1494 uint8_t ns = lctx->n_send;
1495 int length = lctx->length;
1496 int rc;
1497
Philipp Maier08177d32016-12-08 17:23:26 +01001498 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u) (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01001499 lapd_state_name(dl->state), lctx->sapi, dl);
rootaf48bed2011-09-26 11:23:06 +02001500
1501 /* G.2.2 Wrong value of the C/R bit */
1502 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001503 LOGP(DLLAPD, LOGL_ERROR,
1504 "I frame response not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001505 msgb_free(msg);
1506 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1507 return -EINVAL;
1508 }
1509
1510 if (length == 0 || length > lctx->n201) {
1511 /* G.4.2 If the length indicator of an I frame is set
1512 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1513 * primitive with cause "I frame with incorrect length"
1514 * is sent to the mobile management entity. */
Philipp Maier08177d32016-12-08 17:23:26 +01001515 LOGP(DLLAPD, LOGL_ERROR,
1516 "I frame length not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001517 msgb_free(msg);
1518 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1519 return -EIO;
1520 }
1521
1522 /* G.4.2 If the numerical value of L is L<N201 and the M
1523 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1524 * cause "I frame with incorrect use of M bit" is sent to the
1525 * mobile management entity. */
1526 if (lctx->more && length < lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001527 LOGP(DLLAPD, LOGL_ERROR,
1528 "I frame with M bit too short (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001529 msgb_free(msg);
1530 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1531 return -EIO;
1532 }
1533
1534 switch (dl->state) {
1535 case LAPD_STATE_IDLE:
1536 /* if P=1, respond DM with F=1 (5.2.2) */
1537 /* 5.4.5 all other frame types shall be discarded */
1538 if (lctx->p_f)
1539 lapd_send_dm(lctx); /* F=P */
1540 /* fall though */
1541 case LAPD_STATE_SABM_SENT:
1542 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001543 LOGP(DLLAPD, LOGL_NOTICE,
1544 "I frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001545 msgb_free(msg);
1546 return 0;
1547 }
1548
1549 /* 5.7.1: N(s) sequence error */
1550 if (ns != dl->v_recv) {
1551 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
Philipp Maier08177d32016-12-08 17:23:26 +01001552 "V(R)=%u (dl=%p)\n", ns, dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001553 /* discard data */
1554 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001555 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001556 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001557 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001558 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001559 lapd_send_rej(lctx, lctx->p_f);
1560 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001561 /* If there are two subsequent sequence errors received,
1562 * ignore it. (Ignore every second subsequent error.)
1563 * This happens if our reply with the REJ is too slow,
1564 * so the remote gets a T200 timeout and sends another
1565 * frame with a sequence error.
1566 * Test showed that replying with two subsequent REJ
1567 * messages could the remote L2 process to abort.
1568 * Replying too slow shouldn't happen, but may happen
1569 * over serial link between BB and LAPD.
1570 */
1571 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001572 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001573 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1574 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1575 lapd_acknowledge(lctx); /* V(A) is also set here */
1576
1577 /* Send message, if possible due to acknowledged data */
1578 lapd_send_i(lctx, __LINE__);
1579
1580 return 0;
rootaf48bed2011-09-26 11:23:06 +02001581 }
1582 dl->seq_err_cond = 0;
1583
1584 /* Increment receiver state */
1585 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Philipp Maier08177d32016-12-08 17:23:26 +01001586 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u (dl=%p)\n",
1587 dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001588
1589 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1590 lapd_acknowledge(lctx); /* V(A) is also set here */
1591
1592 /* Only if we are not in own receiver busy condition */
1593 if (!dl->own_busy) {
1594 /* if the frame carries a complete segment */
1595 if (!lctx->more && !dl->rcv_buffer) {
Philipp Maier08177d32016-12-08 17:23:26 +01001596 LOGP(DLLAPD, LOGL_INFO,
1597 "message in single I frame (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001598 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001599 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001600 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1601 msg);
1602 } else {
1603 /* create rcv_buffer */
1604 if (!dl->rcv_buffer) {
1605 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001606 "I frames (first message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001607 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1608 "LAPD RX");
1609 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1610 }
1611 /* concat. rcv_buffer */
1612 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1613 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
Philipp Maier08177d32016-12-08 17:23:26 +01001614 "overflow! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001615 } else {
1616 memcpy(msgb_put(dl->rcv_buffer, length),
1617 msg->l3h, length);
1618 }
1619 /* if the last segment was received */
1620 if (!lctx->more) {
1621 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001622 "I frames (last message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001623 rc = send_dl_l3(PRIM_DL_DATA,
1624 PRIM_OP_INDICATION, lctx,
1625 dl->rcv_buffer);
1626 dl->rcv_buffer = NULL;
1627 } else
1628 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001629 "I frames (next message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001630 msgb_free(msg);
1631
1632 }
1633 } else
1634 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1635 "busy condition\n");
1636
1637 /* Check for P bit */
1638 if (lctx->p_f) {
1639 /* 5.5.2.1 */
1640 /* check if we are not in own receiver busy */
1641 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001642 LOGP(DLLAPD, LOGL_INFO,
1643 "we are not busy, send RR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001644 /* Send RR with F=1 */
1645 rc = lapd_send_rr(lctx, 1, 0);
1646 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001647 LOGP(DLLAPD, LOGL_INFO,
1648 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001649 /* Send RNR with F=1 */
1650 rc = lapd_send_rnr(lctx, 1, 0);
1651 }
1652 } else {
1653 /* 5.5.2.2 */
1654 /* check if we are not in own receiver busy */
1655 if (!dl->own_busy) {
1656 /* NOTE: V(R) is already set above */
1657 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001658
1659 /* if update_pending_iframe returns 0 it updated
1660 * the lapd header of an iframe in the tx queue */
1661 if (rc && dl->update_pending_frames)
1662 rc = dl->update_pending_frames(lctx);
1663
rootaf48bed2011-09-26 11:23:06 +02001664 if (rc) {
1665 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
Philipp Maier08177d32016-12-08 17:23:26 +01001666 "have no pending data, send RR (dl=%p)\n",
1667 dl);
rootaf48bed2011-09-26 11:23:06 +02001668 /* Send RR with F=0 */
1669 return lapd_send_rr(lctx, 0, 0);
1670 }
1671 /* all I or one RR is sent, we are done */
1672 return 0;
1673 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001674 LOGP(DLLAPD, LOGL_INFO,
1675 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001676 /* Send RNR with F=0 */
1677 rc = lapd_send_rnr(lctx, 0, 0);
1678 }
1679 }
1680
1681 /* Send message, if possible due to acknowledged data */
1682 lapd_send_i(lctx, __LINE__);
1683
1684 return rc;
1685}
1686
1687/* Receive a LAPD message from L1 */
1688int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1689{
1690 int rc;
1691
1692 switch (lctx->format) {
1693 case LAPD_FORM_U:
1694 rc = lapd_rx_u(msg, lctx);
1695 break;
1696 case LAPD_FORM_S:
1697 rc = lapd_rx_s(msg, lctx);
1698 break;
1699 case LAPD_FORM_I:
1700 rc = lapd_rx_i(msg, lctx);
1701 break;
1702 default:
Philipp Maier08177d32016-12-08 17:23:26 +01001703 LOGP(DLLAPD, LOGL_NOTICE,
1704 "unknown LAPD format (dl=%p)\n", lctx->dl);
rootaf48bed2011-09-26 11:23:06 +02001705 msgb_free(msg);
1706 rc = -EINVAL;
1707 }
1708 return rc;
1709}
1710
1711/* L3 -> L2 */
1712
1713/* send unit data */
1714static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1715{
1716 struct lapd_datalink *dl = lctx->dl;
1717 struct msgb *msg = dp->oph.msg;
1718 struct lapd_msg_ctx nctx;
1719
1720 memcpy(&nctx, lctx, sizeof(nctx));
1721 /* keep nctx.ldp */
1722 /* keep nctx.sapi */
1723 /* keep nctx.tei */
1724 nctx.cr = dl->cr.loc2rem.cmd;
1725 nctx.format = LAPD_FORM_U;
1726 nctx.s_u = LAPD_U_UI;
1727 /* keep nctx.p_f */
1728 nctx.length = msg->len;
1729 nctx.more = 0;
1730
1731 return dl->send_ph_data_req(&nctx, msg);
1732}
1733
1734/* request link establishment */
1735static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1736{
1737 struct lapd_datalink *dl = lctx->dl;
1738 struct msgb *msg = dp->oph.msg;
1739 struct lapd_msg_ctx nctx;
1740
1741 if (msg->len)
1742 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
Philipp Maier08177d32016-12-08 17:23:26 +01001743 "(SABM) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001744 else
Philipp Maier08177d32016-12-08 17:23:26 +01001745 LOGP(DLLAPD, LOGL_INFO,
1746 "perform normal establishm. (SABM), (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001747
1748 /* Flush send-queue */
1749 /* Clear send-buffer */
1750 lapd_dl_flush_send(dl);
1751 /* be sure that history is empty */
1752 lapd_dl_flush_hist(dl);
1753
1754 /* save message context for further use */
1755 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1756
1757 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001758 msgb_free(dl->rcv_buffer);
1759 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001760
1761 /* assemble message */
1762 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1763 /* keep nctx.ldp */
1764 /* keep nctx.sapi */
1765 /* keep nctx.tei */
1766 nctx.cr = dl->cr.loc2rem.cmd;
1767 nctx.format = LAPD_FORM_U;
1768 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1769 nctx.p_f = 1;
1770 nctx.length = msg->len;
1771 nctx.more = 0;
1772
1773 /* Transmit-buffer carries exactly one segment */
1774 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1775 msgb_put(dl->tx_hist[0].msg, msg->len);
1776 if (msg->len)
1777 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1778 dl->tx_hist[0].more = 0;
1779 /* set Vs to 0, because it is used as index when resending SABM */
1780 dl->v_send = 0;
1781
1782 /* Set states */
1783 dl->own_busy = dl->peer_busy = 0;
1784 dl->retrans_ctr = 0;
1785 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1786
1787 /* Tramsmit and start T200 */
1788 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001789 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001790
1791 return 0;
1792}
1793
1794/* send data */
1795static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1796{
1797 struct lapd_datalink *dl = lctx->dl;
1798 struct msgb *msg = dp->oph.msg;
1799
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001800 if (msgb_l3len(msg) == 0) {
1801 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001802 "writing an empty message is not possible. (dl=%p)\n", dl);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001803 msgb_free(msg);
1804 return -1;
1805 }
1806
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001807 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +01001808 "writing message to send-queue: l3len: %d (dl=%p)\n",
1809 msgb_l3len(msg), dl);
rootaf48bed2011-09-26 11:23:06 +02001810
1811 /* Write data into the send queue */
1812 msgb_enqueue(&dl->send_queue, msg);
1813
1814 /* Send message, if possible */
1815 lapd_send_i(&dl->lctx, __LINE__);
1816
1817 return 0;
1818}
1819
1820/* Send next I frame from queued/buffered data */
1821static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1822{
1823 struct lapd_datalink *dl = lctx->dl;
1824 uint8_t k = dl->k;
1825 uint8_t h;
1826 struct msgb *msg;
1827 int length, left;
1828 int rc = - 1; /* we sent nothing */
1829 struct lapd_msg_ctx nctx;
1830
1831
Philipp Maier08177d32016-12-08 17:23:26 +01001832 LOGP(DLLAPD, LOGL_INFO,
1833 "%s() called from line %d (dl=%p)\n", __func__, line, dl);
rootaf48bed2011-09-26 11:23:06 +02001834
1835 next_frame:
1836
1837 if (dl->peer_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001838 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001839 return rc;
1840 }
1841
1842 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Philipp Maier08177d32016-12-08 17:23:26 +01001843 LOGP(DLLAPD, LOGL_INFO,
1844 "timer recovery, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001845 return rc;
1846 }
1847
1848 /* If the send state variable V(S) is equal to V(A) plus k
1849 * (where k is the maximum number of outstanding I frames - see
1850 * subclause 5.8.4), the data link layer entity shall not transmit any
1851 * new I frames, but shall retransmit an I frame as a result
1852 * of the error recovery procedures as described in subclauses 5.5.4 and
1853 * 5.5.7. */
1854 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1855 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
Philipp Maier08177d32016-12-08 17:23:26 +01001856 "more (k=%u V(S)=%u V(A)=%u) (dl=%p)\n", k, dl->v_send,
1857 dl->v_ack, dl);
rootaf48bed2011-09-26 11:23:06 +02001858 return rc;
1859 }
1860
1861 h = do_mod(dl->v_send, dl->range_hist);
1862
1863 /* if we have no tx_hist yet, we create it */
1864 if (!dl->tx_hist[h].msg) {
1865 /* Get next message into send-buffer, if any */
1866 if (!dl->send_buffer) {
1867 next_message:
1868 dl->send_out = 0;
1869 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1870 /* No more data to be sent */
1871 if (!dl->send_buffer)
1872 return rc;
1873 LOGP(DLLAPD, LOGL_INFO, "get message from "
Philipp Maier08177d32016-12-08 17:23:26 +01001874 "send-queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001875 }
1876
1877 /* How much is left in the send-buffer? */
1878 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1879 /* Segment, if data exceeds N201 */
1880 length = left;
1881 if (length > lctx->n201)
1882 length = lctx->n201;
1883 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
Philipp Maier08177d32016-12-08 17:23:26 +01001884 "length %d first byte %02x (dl=%p)\n",
rootaf48bed2011-09-26 11:23:06 +02001885 msgb_l3len(dl->send_buffer), dl->send_out, left,
Philipp Maier08177d32016-12-08 17:23:26 +01001886 lctx->n201, length, dl->send_buffer->l3h[0], dl);
rootaf48bed2011-09-26 11:23:06 +02001887 /* If message in send-buffer is completely sent */
1888 if (left == 0) {
1889 msgb_free(dl->send_buffer);
1890 dl->send_buffer = NULL;
1891 goto next_message;
1892 }
1893
Philipp Maier08177d32016-12-08 17:23:26 +01001894 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d (dl=%p)\n",
1895 (left > length) ? "segment " : "", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001896
1897 /* Create I frame (segment) and transmit-buffer content */
1898 msg = lapd_msgb_alloc(length, "LAPD I");
1899 msg->l3h = msgb_put(msg, length);
1900 /* assemble message */
1901 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1902 /* keep nctx.ldp */
1903 /* keep nctx.sapi */
1904 /* keep nctx.tei */
1905 nctx.cr = dl->cr.loc2rem.cmd;
1906 nctx.format = LAPD_FORM_I;
1907 nctx.p_f = 0;
1908 nctx.n_send = dl->v_send;
1909 nctx.n_recv = dl->v_recv;
1910 nctx.length = length;
1911 if (left > length)
1912 nctx.more = 1;
1913 else
1914 nctx.more = 0;
1915 if (length)
1916 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1917 length);
1918 /* store in tx_hist */
1919 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1920 msgb_put(dl->tx_hist[h].msg, msg->len);
1921 if (length)
1922 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1923 dl->tx_hist[h].more = nctx.more;
1924 /* Add length to track how much is already in the tx buffer */
1925 dl->send_out += length;
1926 } else {
1927 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
Philipp Maier08177d32016-12-08 17:23:26 +01001928 "V(S)=%d (dl=%p)\n", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001929
1930 /* Create I frame (segment) from tx_hist */
1931 length = dl->tx_hist[h].msg->len;
1932 msg = lapd_msgb_alloc(length, "LAPD I resend");
1933 msg->l3h = msgb_put(msg, length);
1934 /* assemble message */
1935 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1936 /* keep nctx.ldp */
1937 /* keep nctx.sapi */
1938 /* keep nctx.tei */
1939 nctx.cr = dl->cr.loc2rem.cmd;
1940 nctx.format = LAPD_FORM_I;
1941 nctx.p_f = 0;
1942 nctx.n_send = dl->v_send;
1943 nctx.n_recv = dl->v_recv;
1944 nctx.length = length;
1945 nctx.more = dl->tx_hist[h].more;
1946 if (length)
1947 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1948 }
1949
1950 /* The value of the send state variable V(S) shall be incremented by 1
1951 * at the end of the transmission of the I frame */
1952 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1953
1954 /* If timer T200 is not running at the time right before transmitting a
1955 * frame, when the PH-READY-TO-SEND primitive is received from the
1956 * physical layer., it shall be set. */
1957 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001958 /* stop Timer T203, if running */
1959 lapd_stop_t203(dl);
1960 /* start Timer T200 */
1961 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001962 }
1963
1964 dl->send_ph_data_req(&nctx, msg);
1965
1966 rc = 0; /* we sent something */
1967 goto next_frame;
1968}
1969
1970/* request link suspension */
1971static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1972{
1973 struct lapd_datalink *dl = lctx->dl;
1974 struct msgb *msg = dp->oph.msg;
1975
Philipp Maier08177d32016-12-08 17:23:26 +01001976 LOGP(DLLAPD, LOGL_INFO, "perform suspension (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001977
1978 /* put back the send-buffer to the send-queue (first position) */
1979 if (dl->send_buffer) {
1980 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
Philipp Maier08177d32016-12-08 17:23:26 +01001981 "queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001982 llist_add(&dl->send_buffer->list, &dl->send_queue);
1983 dl->send_buffer = NULL;
1984 } else
Philipp Maier08177d32016-12-08 17:23:26 +01001985 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001986
1987 /* Clear transmit buffer, but keep send buffer */
1988 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001989 /* Stop timers (there is no state change, so we must stop all timers */
1990 lapd_stop_t200(dl);
1991 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001992
1993 msgb_free(msg);
1994
1995 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1996}
1997
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001998/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001999static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2000{
2001 struct lapd_datalink *dl = lctx->dl;
2002 struct msgb *msg = dp->oph.msg;
2003 struct lapd_msg_ctx nctx;
2004
Philipp Maier08177d32016-12-08 17:23:26 +01002005 LOGP(DLLAPD, LOGL_INFO,
2006 "perform re-establishment (SABM) length=%d (dl=%p)\n",
2007 msg->len, dl);
rootaf48bed2011-09-26 11:23:06 +02002008
2009 /* be sure that history is empty */
2010 lapd_dl_flush_hist(dl);
2011
2012 /* save message context for further use */
2013 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2014
2015 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002016 msgb_free(dl->send_buffer);
2017 dl->send_buffer = NULL;
2018
rootaf48bed2011-09-26 11:23:06 +02002019 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002020 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002021 /* Write data into the send buffer, to be sent first */
2022 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002023 } else {
2024 msgb_free(msg);
2025 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002026 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002027 }
rootaf48bed2011-09-26 11:23:06 +02002028
2029 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002030 msgb_free(dl->rcv_buffer);
2031 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002032
2033 /* Create new msgb (old one is now free) */
2034 msg = lapd_msgb_alloc(0, "LAPD SABM");
2035 msg->l3h = msg->data;
2036 /* assemble message */
2037 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2038 /* keep nctx.ldp */
2039 /* keep nctx.sapi */
2040 /* keep nctx.tei */
2041 nctx.cr = dl->cr.loc2rem.cmd;
2042 nctx.format = LAPD_FORM_U;
2043 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2044 nctx.p_f = 1;
2045 nctx.length = 0;
2046 nctx.more = 0;
2047
2048 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2049 msgb_put(dl->tx_hist[0].msg, msg->len);
2050 if (msg->len)
2051 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2052 dl->tx_hist[0].more = 0;
2053 /* set Vs to 0, because it is used as index when resending SABM */
2054 dl->v_send = 0;
2055
2056 /* Set states */
2057 dl->own_busy = dl->peer_busy = 0;
2058 dl->retrans_ctr = 0;
2059 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2060
2061 /* Tramsmit and start T200 */
2062 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002063 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002064
2065 return 0;
2066}
2067
2068/* requesst release of link */
2069static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2070{
2071 struct lapd_datalink *dl = lctx->dl;
2072 struct msgb *msg = dp->oph.msg;
2073 struct lapd_msg_ctx nctx;
2074
2075 /* local release */
2076 if (dp->u.rel_req.mode) {
Philipp Maier08177d32016-12-08 17:23:26 +01002077 LOGP(DLLAPD, LOGL_INFO, "perform local release (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002078 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002079 /* stop Timer T200 */
2080 lapd_stop_t200(dl);
2081 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002082 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2083 /* flush buffers */
2084 lapd_dl_flush_tx(dl);
2085 lapd_dl_flush_send(dl);
2086 /* send notification to L3 */
2087 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2088 }
2089
2090 /* in case we are already disconnecting */
2091 if (dl->state == LAPD_STATE_DISC_SENT)
2092 return -EBUSY;
2093
2094 /* flush tx_hist */
2095 lapd_dl_flush_hist(dl);
2096
Philipp Maier08177d32016-12-08 17:23:26 +01002097 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002098
2099 /* Push LAPD header on msgb */
2100 /* assemble message */
2101 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2102 /* keep nctx.ldp */
2103 /* keep nctx.sapi */
2104 /* keep nctx.tei */
2105 nctx.cr = dl->cr.loc2rem.cmd;
2106 nctx.format = LAPD_FORM_U;
2107 nctx.s_u = LAPD_U_DISC;
2108 nctx.p_f = 1;
2109 nctx.length = 0;
2110 nctx.more = 0;
2111
2112 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2113 msgb_put(dl->tx_hist[0].msg, msg->len);
2114 if (msg->len)
2115 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2116 dl->tx_hist[0].more = 0;
2117 /* set Vs to 0, because it is used as index when resending DISC */
2118 dl->v_send = 0;
2119
2120 /* Set states */
2121 dl->own_busy = dl->peer_busy = 0;
2122 dl->retrans_ctr = 0;
2123 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2124
2125 /* Tramsmit and start T200 */
2126 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002127 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002128
2129 return 0;
2130}
2131
2132/* request release of link in idle state */
2133static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2134 struct lapd_msg_ctx *lctx)
2135{
2136 struct lapd_datalink *dl = lctx->dl;
2137 struct msgb *msg = dp->oph.msg;
2138
2139 msgb_free(msg);
2140
2141 /* send notification to L3 */
2142 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2143}
2144
2145/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002146static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002147 uint32_t states;
2148 int prim, op;
2149 const char *name;
2150 int (*rout) (struct osmo_dlsap_prim *dp,
2151 struct lapd_msg_ctx *lctx);
2152} l2downstatelist[] = {
2153 /* create and send UI command */
2154 {ALL_STATES,
2155 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2156 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2157
2158 /* create and send SABM command */
2159 {SBIT(LAPD_STATE_IDLE),
2160 PRIM_DL_EST, PRIM_OP_REQUEST,
2161 "DL-ESTABLISH-REQUEST", lapd_est_req},
2162
2163 /* create and send I command */
2164 {SBIT(LAPD_STATE_MF_EST) |
2165 SBIT(LAPD_STATE_TIMER_RECOV),
2166 PRIM_DL_DATA, PRIM_OP_REQUEST,
2167 "DL-DATA-REQUEST", lapd_data_req},
2168
2169 /* suspend datalink */
2170 {SBIT(LAPD_STATE_MF_EST) |
2171 SBIT(LAPD_STATE_TIMER_RECOV),
2172 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2173 "DL-SUSPEND-REQUEST", lapd_susp_req},
2174
2175 /* create and send SABM command (resume) */
2176 {SBIT(LAPD_STATE_MF_EST) |
2177 SBIT(LAPD_STATE_TIMER_RECOV),
2178 PRIM_DL_RES, PRIM_OP_REQUEST,
2179 "DL-RESUME-REQUEST", lapd_res_req},
2180
2181 /* create and send SABM command (reconnect) */
2182 {SBIT(LAPD_STATE_IDLE) |
2183 SBIT(LAPD_STATE_MF_EST) |
2184 SBIT(LAPD_STATE_TIMER_RECOV),
2185 PRIM_DL_RECON, PRIM_OP_REQUEST,
2186 "DL-RECONNECT-REQUEST", lapd_res_req},
2187
2188 /* create and send DISC command */
2189 {SBIT(LAPD_STATE_SABM_SENT) |
2190 SBIT(LAPD_STATE_MF_EST) |
2191 SBIT(LAPD_STATE_TIMER_RECOV) |
2192 SBIT(LAPD_STATE_DISC_SENT),
2193 PRIM_DL_REL, PRIM_OP_REQUEST,
2194 "DL-RELEASE-REQUEST", lapd_rel_req},
2195
2196 /* release in idle state */
2197 {SBIT(LAPD_STATE_IDLE),
2198 PRIM_DL_REL, PRIM_OP_REQUEST,
2199 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2200};
2201
2202#define L2DOWNSLLEN \
2203 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2204
2205int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2206{
2207 struct lapd_datalink *dl = lctx->dl;
2208 int i, supported = 0;
2209 struct msgb *msg = dp->oph.msg;
2210 int rc;
2211
2212 /* find function for current state and message */
2213 for (i = 0; i < L2DOWNSLLEN; i++) {
2214 if (dp->oph.primitive == l2downstatelist[i].prim
2215 && dp->oph.operation == l2downstatelist[i].op) {
2216 supported = 1;
2217 if ((SBIT(dl->state) & l2downstatelist[i].states))
2218 break;
2219 }
2220 }
2221 if (!supported) {
Philipp Maier08177d32016-12-08 17:23:26 +01002222 LOGP(DLLAPD, LOGL_NOTICE,
2223 "Message %u/%u unsupported. (dl=%p)\n", dp->oph.primitive,
2224 dp->oph.operation, dl);
rootaf48bed2011-09-26 11:23:06 +02002225 msgb_free(msg);
2226 return 0;
2227 }
2228 if (i == L2DOWNSLLEN) {
2229 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
Philipp Maier08177d32016-12-08 17:23:26 +01002230 "state %s. (dl=%p)\n", dp->oph.primitive,
Harald Weltec733d142017-03-15 10:20:51 +01002231 dp->oph.operation, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002232 msgb_free(msg);
2233 return 0;
2234 }
2235
Philipp Maier08177d32016-12-08 17:23:26 +01002236 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s (dl=%p)\n",
Harald Weltec733d142017-03-15 10:20:51 +01002237 l2downstatelist[i].name, lapd_state_name(dl->state), dl);
rootaf48bed2011-09-26 11:23:06 +02002238
2239 rc = l2downstatelist[i].rout(dp, lctx);
2240
2241 return rc;
2242}
2243
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002244/*! @} */