blob: 5af82aa26ca0252a609f8621b8aa94c64a29ac96 [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 */
183const char *lapd_state_names[] = {
184 "LAPD_STATE_NULL",
185 "LAPD_STATE_TEI_UNASS",
186 "LAPD_STATE_ASS_TEI_WAIT",
187 "LAPD_STATE_EST_TEI_WAIT",
188 "LAPD_STATE_IDLE",
189 "LAPD_STATE_SABM_SENT",
190 "LAPD_STATE_DISC_SENT",
191 "LAPD_STATE_MF_EST",
192 "LAPD_STATE_TIMER_RECOV",
193
194};
195
Andreas Eversberg742fc792011-09-27 09:40:25 +0200196static void lapd_start_t200(struct lapd_datalink *dl)
197{
198 if (osmo_timer_pending(&dl->t200))
199 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100200 LOGP(DLLAPD, LOGL_INFO, "start T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200201 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
202}
203
204static void lapd_start_t203(struct lapd_datalink *dl)
205{
206 if (osmo_timer_pending(&dl->t203))
207 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100208 LOGP(DLLAPD, LOGL_INFO, "start T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200209 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
210}
211
212static void lapd_stop_t200(struct lapd_datalink *dl)
213{
214 if (!osmo_timer_pending(&dl->t200))
215 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100216 LOGP(DLLAPD, LOGL_INFO, "stop T200 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200217 osmo_timer_del(&dl->t200);
218}
219
220static void lapd_stop_t203(struct lapd_datalink *dl)
221{
222 if (!osmo_timer_pending(&dl->t203))
223 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100224 LOGP(DLLAPD, LOGL_INFO, "stop T203 (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200225 osmo_timer_del(&dl->t203);
226}
227
rootaf48bed2011-09-26 11:23:06 +0200228static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
229{
Philipp Maier08177d32016-12-08 17:23:26 +0100230 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s (dl=%p)\n",
231 lapd_state_names[dl->state], lapd_state_names[state], dl);
rootaf48bed2011-09-26 11:23:06 +0200232
233 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
234 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200235 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200236 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200237 msgb_free(dl->cont_res);
238 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200239 }
240
241 /* start T203 on entering MF EST state, if enabled */
242 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200243 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
244 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200245
246 dl->state = state;
247}
248
rootaf48bed2011-09-26 11:23:06 +0200249static void *tall_lapd_ctx = NULL;
250
251/* init datalink instance and allocate history */
252void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
253 int maxf)
254{
255 int m;
256
257 memset(dl, 0, sizeof(*dl));
258 INIT_LLIST_HEAD(&dl->send_queue);
259 INIT_LLIST_HEAD(&dl->tx_queue);
260 dl->reestablish = 1;
261 dl->n200_est_rel = 3;
262 dl->n200 = 3;
263 dl->t200_sec = 1;
264 dl->t200_usec = 0;
265 dl->t200.data = dl;
266 dl->t200.cb = &lapd_t200_cb;
267 dl->t203_sec = 10;
268 dl->t203_usec = 0;
269 dl->t203.data = dl;
270 dl->t203.cb = &lapd_t203_cb;
271 dl->maxf = maxf;
272 if (k > v_range - 1)
273 k = v_range - 1;
274 dl->k = k;
275 dl->v_range = v_range;
276
277 /* Calculate modulo for history array:
278 * - The history range must be at least k+1.
279 * - The history range must be 2^x, where x is as low as possible.
280 */
281 k++;
282 for (m = 0x80; m; m >>= 1) {
283 if ((m & k)) {
284 if (k > m)
285 m <<= 1;
286 dl->range_hist = m;
287 break;
288 }
289 }
290
291 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
Philipp Maier08177d32016-12-08 17:23:26 +0100292 "history range = %d (dl=%p)\n", dl->v_range, dl->k,
293 dl->range_hist, dl);
rootaf48bed2011-09-26 11:23:06 +0200294
295 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
296
297 if (!tall_lapd_ctx)
298 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100299 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
300 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200301}
302
303/* reset to IDLE state */
304void lapd_dl_reset(struct lapd_datalink *dl)
305{
306 if (dl->state == LAPD_STATE_IDLE)
307 return;
Philipp Maier08177d32016-12-08 17:23:26 +0100308 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200309 /* enter idle state (and remove eventual cont_res) */
310 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
311 /* 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);
rootaf48bed2011-09-26 11:23:06 +0200320}
321
322/* reset and de-allocate history buffer */
323void lapd_dl_exit(struct lapd_datalink *dl)
324{
325 /* free all ressources except history buffer */
326 lapd_dl_reset(dl);
327 /* free history buffer list */
328 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200329 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200330}
331
332/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
333int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
334{
335 switch (mode) {
336 case LAPD_MODE_USER:
337 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
338 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
339 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
340 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
341 break;
342 case LAPD_MODE_NETWORK:
343 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
344 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
345 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
346 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
347 break;
348 default:
349 return -EINVAL;
350 }
351 dl->mode = mode;
352
353 return 0;
354}
355
356/* send DL message with optional msgb */
357static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
358 struct msgb *msg)
359{
360 struct lapd_datalink *dl = lctx->dl;
361 struct osmo_dlsap_prim dp;
362
363 osmo_prim_init(&dp.oph, 0, prim, op, msg);
364 return dl->send_dlsap(&dp, lctx);
365}
366
367/* send simple DL message */
368static inline int send_dl_simple(uint8_t prim, uint8_t op,
369 struct lapd_msg_ctx *lctx)
370{
371 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
372
373 return send_dl_l3(prim, op, lctx, msg);
374}
375
376/* send MDL-ERROR INDICATION */
377static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
378{
379 struct lapd_datalink *dl = lctx->dl;
380 struct osmo_dlsap_prim dp;
381
Philipp Maier08177d32016-12-08 17:23:26 +0100382 LOGP(DLLAPD, LOGL_NOTICE,
383 "sending MDL-ERROR-IND cause %d from state %s (dl=%p)\n",
384 cause, lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200385 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
386 dp.u.error_ind.cause = cause;
387 return dl->send_dlsap(&dp, lctx);
388}
389
390/* send UA response */
391static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
392{
393 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
394 struct lapd_msg_ctx nctx;
395 struct lapd_datalink *dl = lctx->dl;
396
397 memcpy(&nctx, lctx, sizeof(nctx));
398 msg->l3h = msgb_put(msg, len);
399 if (len)
400 memcpy(msg->l3h, data, len);
401 /* keep nctx.ldp */
402 /* keep nctx.sapi */
403 /* keep nctx.tei */
404 nctx.cr = dl->cr.loc2rem.resp;
405 nctx.format = LAPD_FORM_U;
406 nctx.s_u = LAPD_U_UA;
407 /* keep nctx.p_f */
408 nctx.length = len;
409 nctx.more = 0;
410
411 return dl->send_ph_data_req(&nctx, msg);
412}
413
414/* send DM response */
415static int lapd_send_dm(struct lapd_msg_ctx *lctx)
416{
417 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
418 struct lapd_msg_ctx nctx;
419 struct lapd_datalink *dl = lctx->dl;
420
421 memcpy(&nctx, lctx, sizeof(nctx));
422 /* keep nctx.ldp */
423 /* keep nctx.sapi */
424 /* keep nctx.tei */
425 nctx.cr = dl->cr.loc2rem.resp;
426 nctx.format = LAPD_FORM_U;
427 nctx.s_u = LAPD_U_DM;
428 /* keep nctx.p_f */
429 nctx.length = 0;
430 nctx.more = 0;
431
432 return dl->send_ph_data_req(&nctx, msg);
433}
434
435/* send RR response / command */
436static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
437{
438 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
439 struct lapd_msg_ctx nctx;
440 struct lapd_datalink *dl = lctx->dl;
441
442 memcpy(&nctx, lctx, sizeof(nctx));
443 /* keep nctx.ldp */
444 /* keep nctx.sapi */
445 /* keep nctx.tei */
446 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
447 nctx.format = LAPD_FORM_S;
448 nctx.s_u = LAPD_S_RR;
449 nctx.p_f = f_bit;
450 nctx.n_recv = dl->v_recv;
451 nctx.length = 0;
452 nctx.more = 0;
453
454 return dl->send_ph_data_req(&nctx, msg);
455}
456
457/* send RNR response / command */
458static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
459{
460 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
461 struct lapd_msg_ctx nctx;
462 struct lapd_datalink *dl = lctx->dl;
463
464 memcpy(&nctx, lctx, sizeof(nctx));
465 /* keep nctx.ldp */
466 /* keep nctx.sapi */
467 /* keep nctx.tei */
468 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
469 nctx.format = LAPD_FORM_S;
470 nctx.s_u = LAPD_S_RNR;
471 nctx.p_f = f_bit;
472 nctx.n_recv = dl->v_recv;
473 nctx.length = 0;
474 nctx.more = 0;
475
476 return dl->send_ph_data_req(&nctx, msg);
477}
478
479/* send REJ response */
480static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
481{
482 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
483 struct lapd_msg_ctx nctx;
484 struct lapd_datalink *dl = lctx->dl;
485
486 memcpy(&nctx, lctx, sizeof(nctx));
487 /* keep nctx.ldp */
488 /* keep nctx.sapi */
489 /* keep nctx.tei */
490 nctx.cr = dl->cr.loc2rem.resp;
491 nctx.format = LAPD_FORM_S;
492 nctx.s_u = LAPD_S_REJ;
493 nctx.p_f = f_bit;
494 nctx.n_recv = dl->v_recv;
495 nctx.length = 0;
496 nctx.more = 0;
497
498 return dl->send_ph_data_req(&nctx, msg);
499}
500
501/* resend SABM or DISC message */
502static int lapd_send_resend(struct lapd_datalink *dl)
503{
504 struct msgb *msg;
505 uint8_t h = do_mod(dl->v_send, dl->range_hist);
506 int length = dl->tx_hist[h].msg->len;
507 struct lapd_msg_ctx nctx;
508
509 /* assemble message */
510 memcpy(&nctx, &dl->lctx, sizeof(nctx));
511 /* keep nctx.ldp */
512 /* keep nctx.sapi */
513 /* keep nctx.tei */
514 nctx.cr = dl->cr.loc2rem.cmd;
515 nctx.format = LAPD_FORM_U;
516 if (dl->state == LAPD_STATE_SABM_SENT)
517 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
518 else
519 nctx.s_u = LAPD_U_DISC;
520 nctx.p_f = 1;
521 nctx.length = length;
522 nctx.more = 0;
523
524 /* Resend SABM/DISC from tx_hist */
525 msg = lapd_msgb_alloc(length, "LAPD resend");
526 msg->l3h = msgb_put(msg, length);
527 if (length)
528 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
529
530 return dl->send_ph_data_req(&nctx, msg);
531}
532
533/* reestablish link */
534static int lapd_reestablish(struct lapd_datalink *dl)
535{
536 struct osmo_dlsap_prim dp;
537 struct msgb *msg;
538
Philipp Maier08177d32016-12-08 17:23:26 +0100539 LOGP(DLLAPD, LOGL_DEBUG, "lapd reestablish (dl=%p)\n", dl);
540
rootaf48bed2011-09-26 11:23:06 +0200541 msg = lapd_msgb_alloc(0, "DUMMY");
542 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
543
544 return lapd_est_req(&dp, &dl->lctx);
545}
546
547/* Timer callback on T200 expiry */
548static void lapd_t200_cb(void *data)
549{
550 struct lapd_datalink *dl = data;
551
Philipp Maier08177d32016-12-08 17:23:26 +0100552 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 state=%s (dl=%p)\n",
553 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200554
555 switch (dl->state) {
556 case LAPD_STATE_SABM_SENT:
557 /* 5.4.1.3 */
558 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
rootaf48bed2011-09-26 11:23:06 +0200559 /* flush tx and send buffers */
560 lapd_dl_flush_tx(dl);
561 lapd_dl_flush_send(dl);
562 /* go back to idle state */
563 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
564 /* NOTE: we must not change any other states or buffers
565 * and queues, since we may reconnect after handover
566 * failure. the buffered messages is replaced there */
Philipp Maierd9f61292016-12-08 10:45:06 +0100567 /* send RELEASE INDICATION to L3 */
568 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
569 &dl->lctx);
570 /* send MDL ERROR INIDCATION to L3 */
571 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
rootaf48bed2011-09-26 11:23:06 +0200572 break;
573 }
574 /* retransmit SABM command */
575 lapd_send_resend(dl);
576 /* increment re-transmission counter */
577 dl->retrans_ctr++;
578 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200579 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200580 break;
581 case LAPD_STATE_DISC_SENT:
582 /* 5.4.4.3 */
583 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
584 /* send RELEASE INDICATION to L3 */
585 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
586 /* send MDL ERROR INIDCATION to L3 */
587 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
588 /* flush tx and send buffers */
589 lapd_dl_flush_tx(dl);
590 lapd_dl_flush_send(dl);
591 /* go back to idle state */
592 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
593 /* NOTE: we must not change any other states or buffers
594 * and queues, since we may reconnect after handover
595 * failure. the buffered messages is replaced there */
596 break;
597 }
598 /* retransmit DISC command */
599 lapd_send_resend(dl);
600 /* increment re-transmission counter */
601 dl->retrans_ctr++;
602 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200603 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200604 break;
605 case LAPD_STATE_MF_EST:
606 /* 5.5.7 */
607 dl->retrans_ctr = 0;
608 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
609 /* fall through */
610 case LAPD_STATE_TIMER_RECOV:
611 dl->retrans_ctr++;
612 if (dl->retrans_ctr < dl->n200) {
613 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
614 uint8_t h = do_mod(vs, dl->range_hist);
615 /* retransmit I frame (V_s-1) with P=1, if any */
616 if (dl->tx_hist[h].msg) {
617 struct msgb *msg;
618 int length = dl->tx_hist[h].msg->len;
619 struct lapd_msg_ctx nctx;
620
621 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
Philipp Maier08177d32016-12-08 17:23:26 +0100622 " V(S)=%d (dl=%p)\n", vs, dl);
rootaf48bed2011-09-26 11:23:06 +0200623 /* Create I frame (segment) from tx_hist */
624 memcpy(&nctx, &dl->lctx, sizeof(nctx));
625 /* keep nctx.ldp */
626 /* keep nctx.sapi */
627 /* keep nctx.tei */
628 nctx.cr = dl->cr.loc2rem.cmd;
629 nctx.format = LAPD_FORM_I;
630 nctx.p_f = 1;
631 nctx.n_send = vs;
632 nctx.n_recv = dl->v_recv;
633 nctx.length = length;
634 nctx.more = dl->tx_hist[h].more;
635 msg = lapd_msgb_alloc(length, "LAPD I resend");
636 msg->l3h = msgb_put(msg, length);
637 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
638 length);
639 dl->send_ph_data_req(&nctx, msg);
640 } else {
641 /* OR send appropriate supervision frame with P=1 */
642 if (!dl->own_busy && !dl->seq_err_cond) {
643 lapd_send_rr(&dl->lctx, 1, 1);
644 /* NOTE: In case of sequence error
645 * condition, the REJ frame has been
646 * transmitted when entering the
647 * condition, so it has not be done
648 * here
649 */
650 } else if (dl->own_busy) {
651 lapd_send_rnr(&dl->lctx, 1, 1);
652 } else {
653 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
Philipp Maier08177d32016-12-08 17:23:26 +0100654 "pls. fix (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200655 }
656 }
657 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200658 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200659 } else {
660 /* send MDL ERROR INIDCATION to L3 */
661 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
662 /* reestablish */
663 if (!dl->reestablish)
664 break;
665 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100666 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200667 lapd_reestablish(dl);
668 }
669 break;
670 default:
671 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
Philipp Maier08177d32016-12-08 17:23:26 +0100672 "dl->state %s (dl=%p)\n", lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200673 }
674}
675
676/* Timer callback on T203 expiry */
677static void lapd_t203_cb(void *data)
678{
679 struct lapd_datalink *dl = data;
680
Philipp Maier08177d32016-12-08 17:23:26 +0100681 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 state=%s (dl=%p)\n",
682 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200683
684 if (dl->state != LAPD_STATE_MF_EST) {
685 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
Philipp Maier08177d32016-12-08 17:23:26 +0100686 "please fix! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200687 return;
688 }
689
690 /* set retransmission counter to 0 */
691 dl->retrans_ctr = 0;
692 /* enter timer recovery state */
693 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
694 /* transmit a supervisory command with P bit set to 1 as follows: */
695 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +0100696 LOGP(DLLAPD, LOGL_INFO,
697 "transmit an RR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200698 /* Send RR with P=1 */
699 lapd_send_rr(&dl->lctx, 1, 1);
700 } else {
Philipp Maier08177d32016-12-08 17:23:26 +0100701 LOGP(DLLAPD, LOGL_INFO,
702 "transmit an RNR poll command (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200703 /* Send RNR with P=1 */
704 lapd_send_rnr(&dl->lctx, 1, 1);
705 }
706 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200707 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200708}
709
710/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
711static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
712{
713 struct lapd_datalink *dl = lctx->dl;
714 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100715 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200716 int i, h;
717
718 /* supervisory frame ? */
719 if (lctx->format == LAPD_FORM_S)
720 s = 1;
721 /* REJ frame ? */
722 if (s && lctx->s_u == LAPD_S_REJ)
723 rej = 1;
724
725 /* Flush all transmit buffers of acknowledged frames */
726 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
727 h = do_mod(i, dl->range_hist);
728 if (dl->tx_hist[h].msg) {
729 msgb_free(dl->tx_hist[h].msg);
730 dl->tx_hist[h].msg = NULL;
731 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
732 }
733 }
734
735 if (dl->state != LAPD_STATE_TIMER_RECOV) {
736 /* When not in the timer recovery condition, the data
737 * link layer entity shall reset the timer T200 on
738 * receipt of a valid I frame with N(R) higher than V(A),
739 * or an REJ with an N(R) equal to V(A). */
740 if ((!rej && nr != dl->v_ack)
741 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200742 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200743 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200744 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
745 }
746 /* 5.7.4: N(R) sequence error
747 * N(R) is called valid, if and only if
748 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
749 */
750 if (sub_mod(nr, dl->v_ack, dl->v_range)
751 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
Philipp Maier08177d32016-12-08 17:23:26 +0100752 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200753 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
754 }
755 }
756
757 /* V(A) shall be set to the value of N(R) */
758 dl->v_ack = nr;
759
Andreas Eversberg742fc792011-09-27 09:40:25 +0200760 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200761 * and if there are outstanding I frames, restart T200 */
762 if (t200_reset && !rej) {
763 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
764 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
Philipp Maier08177d32016-12-08 17:23:26 +0100765 "frame(s) (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200766 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200767 }
768 }
769
770 /* This also does a restart, when I or S frame is received */
771
772 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200773 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200774 /* Start T203, if T200 is not running in MF EST state, if enabled */
775 if (!osmo_timer_pending(&dl->t200)
776 && (dl->t203_sec || dl->t203_usec)
777 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200778 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200779 }
780}
781
782/* L1 -> L2 */
783
784/* Receive a LAPD U (Unnumbered) message from L1 */
785static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
786{
787 struct lapd_datalink *dl = lctx->dl;
788 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100789 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200790 uint8_t prim, op;
791
792 switch (lctx->s_u) {
793 case LAPD_U_SABM:
794 case LAPD_U_SABME:
795 prim = PRIM_DL_EST;
796 op = PRIM_OP_INDICATION;
797
Philipp Maier08177d32016-12-08 17:23:26 +0100798 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s (dl=%p)\n",
799 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200800 /* 5.7.1 */
801 dl->seq_err_cond = 0;
802 /* G.2.2 Wrong value of the C/R bit */
803 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +0100804 LOGP(DLLAPD, LOGL_ERROR,
805 "SABM response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200806 msgb_free(msg);
807 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
808 return -EINVAL;
809 }
810
811 /* G.4.5 If SABM is received with L>N201 or with M bit
812 * set, AN MDL-ERROR-INDICATION is sent to MM.
813 */
814 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +0100815 LOGP(DLLAPD, LOGL_ERROR,
816 "SABM too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200817 msgb_free(msg);
818 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
819 return -EIO;
820 }
821
822 switch (dl->state) {
823 case LAPD_STATE_IDLE:
824 break;
825 case LAPD_STATE_MF_EST:
826 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
Philipp Maier08177d32016-12-08 17:23:26 +0100827 "frame established state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200828 /* If link is lost on the remote side, we start over
829 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100830 /* Additionally, continue in case of content resoltion
831 * (GSM network). This happens, if the mobile has not
832 * yet received UA or another mobile (collision) tries
833 * to establish connection. The mobile must receive
834 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200835 /* 5.4.2.1 */
836 if (!length) {
837 /* If no content resolution, this is a
838 * re-establishment. */
839 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +0100840 "Remote reestablish (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200841 break;
842 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200843 if (!dl->cont_res) {
844 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Philipp Maier08177d32016-12-08 17:23:26 +0100845 "allowed in state %s (dl=%p)\n",
846 lapd_state_names[dl->state], dl);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200847 mdl_error(MDL_CAUSE_SABM_MF, lctx);
848 msgb_free(msg);
849 return 0;
850 }
rootaf48bed2011-09-26 11:23:06 +0200851 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200852 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200853#ifdef TEST_CONTENT_RESOLUTION_NETWORK
854 dl->cont_res->data[0] ^= 0x01;
855#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100856 if (memcmp(dl->cont_res->data, msg->data,
857 length)) {
rootaf48bed2011-09-26 11:23:06 +0200858 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
Philipp Maier08177d32016-12-08 17:23:26 +0100859 "with diffrent content - "
860 "ignoring! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200861 msgb_free(msg);
862 return 0;
863 }
864 }
865 /* send UA again */
866 lapd_send_ua(lctx, length, msg->l3h);
867 msgb_free(msg);
868 return 0;
869 case LAPD_STATE_DISC_SENT:
870 /* 5.4.6.2 send DM with F=P */
871 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200872 /* stop Timer T200 */
873 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200874 msgb_free(msg);
875 return send_dl_simple(prim, op, lctx);
876 default:
877 /* collision: Send UA, but still wait for rx UA, then
878 * change to MF_EST state.
879 */
880 /* check for contention resoultion */
881 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
882 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Philipp Maier08177d32016-12-08 17:23:26 +0100883 "during contention resolution (state=%s, dl=%p)\n",
884 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200885 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
886 }
887 lapd_send_ua(lctx, length, msg->l3h);
888 msgb_free(msg);
889 return 0;
890 }
891 /* save message context for further use */
892 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
893#ifndef TEST_CONTENT_RESOLUTION_NETWORK
894 /* send UA response */
895 lapd_send_ua(lctx, length, msg->l3h);
896#endif
897 /* set Vs, Vr and Va to 0 */
898 dl->v_send = dl->v_recv = dl->v_ack = 0;
899 /* clear tx_hist */
900 lapd_dl_flush_hist(dl);
901 /* enter multiple-frame-established state */
902 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
903 /* store content resolution data on network side
904 * Note: cont_res will be removed when changing state again,
905 * so it must be allocated AFTER lapd_dl_newstate(). */
906 if (dl->mode == LAPD_MODE_NETWORK && length) {
907 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
908 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
909 length);
Philipp Maier08177d32016-12-08 17:23:26 +0100910 LOGP(DLLAPD, LOGL_NOTICE,
911 "Store content res. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200912 }
913 /* send notification to L3 */
914 if (length == 0) {
915 /* 5.4.1.2 Normal establishment procedures */
916 rc = send_dl_simple(prim, op, lctx);
917 msgb_free(msg);
918 } else {
919 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200920 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200921 rc = send_dl_l3(prim, op, lctx, msg);
922 }
923 break;
924 case LAPD_U_DM:
Philipp Maier08177d32016-12-08 17:23:26 +0100925 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s (dl=%p)\n",
926 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +0200927 /* G.2.2 Wrong value of the C/R bit */
928 if (lctx->cr == dl->cr.rem2loc.cmd) {
Philipp Maier08177d32016-12-08 17:23:26 +0100929 LOGP(DLLAPD, LOGL_ERROR,
930 "DM command error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200931 msgb_free(msg);
932 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
933 return -EINVAL;
934 }
935 if (!lctx->p_f) {
936 /* 5.4.1.2 DM responses with the F bit set to "0"
937 * shall be ignored.
938 */
939 msgb_free(msg);
940 return 0;
941 }
942 switch (dl->state) {
943 case LAPD_STATE_SABM_SENT:
944 break;
945 case LAPD_STATE_MF_EST:
946 if (lctx->p_f) {
947 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
Philipp Maier08177d32016-12-08 17:23:26 +0100948 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200949 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
950 } else {
951 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
952 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100953 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200954 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
955 /* reestablish */
956 if (!dl->reestablish) {
957 msgb_free(msg);
958 return 0;
959 }
960 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100961 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200962 lapd_reestablish(dl);
963 }
964 msgb_free(msg);
965 return 0;
966 case LAPD_STATE_TIMER_RECOV:
967 /* FP = 0 (DM is normal in case PF = 1) */
968 if (!lctx->p_f) {
969 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
970 "response, multiple frame established "
Philipp Maier08177d32016-12-08 17:23:26 +0100971 "state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200972 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
973 msgb_free(msg);
974 /* reestablish */
975 if (!dl->reestablish)
976 return 0;
977 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
Philipp Maier08177d32016-12-08 17:23:26 +0100978 "reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200979 return lapd_reestablish(dl);
980 }
981 break;
982 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200983 /* stop Timer T200 */
984 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200985 /* go to idle state */
986 lapd_dl_flush_tx(dl);
987 lapd_dl_flush_send(dl);
988 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
989 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
990 msgb_free(msg);
991 return 0;
992 case LAPD_STATE_IDLE:
993 /* 5.4.5 all other frame types shall be discarded */
994 default:
995 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
Philipp Maier08177d32016-12-08 17:23:26 +0100996 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +0200997 msgb_free(msg);
998 return 0;
999 }
Andreas Eversberg742fc792011-09-27 09:40:25 +02001000 /* stop timer T200 */
1001 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001002 /* go to idle state */
1003 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1004 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
1005 msgb_free(msg);
1006 break;
1007 case LAPD_U_UI:
Philipp Maier08177d32016-12-08 17:23:26 +01001008 LOGP(DLLAPD, LOGL_INFO, "UI received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001009 /* G.2.2 Wrong value of the C/R bit */
1010 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001011 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
Philipp Maier08177d32016-12-08 17:23:26 +01001012 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001013 msgb_free(msg);
1014 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1015 return -EINVAL;
1016 }
1017
1018 /* G.4.5 If UI is received with L>N201 or with M bit
1019 * set, AN MDL-ERROR-INDICATION is sent to MM.
1020 */
1021 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001022 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
Philipp Maier08177d32016-12-08 17:23:26 +01001023 "(%d > N201(%d) or M=%d) (dl=%p)\n", length,
1024 lctx->n201, lctx->more, dl);
rootaf48bed2011-09-26 11:23:06 +02001025 msgb_free(msg);
1026 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1027 return -EIO;
1028 }
1029
1030 /* do some length checks */
1031 if (length == 0) {
1032 /* 5.3.3 UI frames received with the length indicator
1033 * set to "0" shall be ignored
1034 */
Philipp Maier08177d32016-12-08 17:23:26 +01001035 LOGP(DLLAPD, LOGL_INFO,
1036 "length=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001037 msgb_free(msg);
1038 return 0;
1039 }
Harald Welte087116a2013-06-18 21:41:34 +02001040 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001041 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1042 msg);
1043 break;
1044 case LAPD_U_DISC:
1045 prim = PRIM_DL_REL;
1046 op = PRIM_OP_INDICATION;
1047
Philipp Maier08177d32016-12-08 17:23:26 +01001048 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s (dl=%p)\n",
1049 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02001050 /* flush tx and send buffers */
1051 lapd_dl_flush_tx(dl);
1052 lapd_dl_flush_send(dl);
1053 /* 5.7.1 */
1054 dl->seq_err_cond = 0;
1055 /* G.2.2 Wrong value of the C/R bit */
1056 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001057 LOGP(DLLAPD, LOGL_ERROR,
1058 "DISC response error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001059 msgb_free(msg);
1060 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1061 return -EINVAL;
1062 }
1063 if (length > 0 || lctx->more) {
1064 /* G.4.4 If a DISC or DM frame is received with L>0 or
1065 * with the M bit set to "1", an MDL-ERROR-INDICATION
1066 * primitive with cause "U frame with incorrect
1067 * parameters" is sent to the mobile management entity.
1068 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001069 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001070 "U frame iwth incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001071 msgb_free(msg);
1072 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1073 return -EIO;
1074 }
1075 switch (dl->state) {
1076 case LAPD_STATE_IDLE:
Philipp Maier08177d32016-12-08 17:23:26 +01001077 LOGP(DLLAPD, LOGL_INFO,
1078 "DISC in idle state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001079 /* send DM with F=P */
1080 msgb_free(msg);
1081 return lapd_send_dm(lctx);
1082 case LAPD_STATE_SABM_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001083 LOGP(DLLAPD, LOGL_INFO,
1084 "DISC in SABM state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001085 /* 5.4.6.2 send DM with F=P */
1086 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001087 /* stop Timer T200 */
1088 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001089 /* go to idle state */
1090 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1091 msgb_free(msg);
1092 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1093 lctx);
1094 case LAPD_STATE_MF_EST:
1095 case LAPD_STATE_TIMER_RECOV:
Philipp Maier08177d32016-12-08 17:23:26 +01001096 LOGP(DLLAPD, LOGL_INFO,
1097 "DISC in est state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001098 break;
1099 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001100 LOGP(DLLAPD, LOGL_INFO,
1101 "DISC in disc state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001102 prim = PRIM_DL_REL;
1103 op = PRIM_OP_CONFIRM;
1104 break;
1105 default:
1106 lapd_send_ua(lctx, length, msg->l3h);
1107 msgb_free(msg);
1108 return 0;
1109 }
1110 /* send UA response */
1111 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001112 /* stop Timer T200 */
1113 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001114 /* enter idle state, keep tx-buffer with UA response */
1115 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1116 /* send notification to L3 */
1117 rc = send_dl_simple(prim, op, lctx);
1118 msgb_free(msg);
1119 break;
1120 case LAPD_U_UA:
Philipp Maier08177d32016-12-08 17:23:26 +01001121 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s (dl=%p)\n",
1122 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02001123 /* G.2.2 Wrong value of the C/R bit */
1124 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001125 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
Philipp Maier08177d32016-12-08 17:23:26 +01001126 "error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001127 msgb_free(msg);
1128 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1129 return -EINVAL;
1130 }
1131
1132 /* G.4.5 If UA is received with L>N201 or with M bit
1133 * set, AN MDL-ERROR-INDICATION is sent to MM.
1134 */
1135 if (lctx->more || length > lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001136 LOGP(DLLAPD, LOGL_ERROR,
1137 "UA too large error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001138 msgb_free(msg);
1139 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1140 return -EIO;
1141 }
1142
1143 if (!lctx->p_f) {
1144 /* 5.4.1.2 A UA response with the F bit set to "0"
1145 * shall be ignored.
1146 */
Philipp Maier08177d32016-12-08 17:23:26 +01001147 LOGP(DLLAPD, LOGL_INFO,
1148 "F=0 (discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001149 msgb_free(msg);
1150 return 0;
1151 }
1152 switch (dl->state) {
1153 case LAPD_STATE_SABM_SENT:
1154 break;
1155 case LAPD_STATE_MF_EST:
1156 case LAPD_STATE_TIMER_RECOV:
1157 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001158 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001159 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1160 msgb_free(msg);
1161 return 0;
1162 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001163 LOGP(DLLAPD, LOGL_INFO,
1164 "UA in disconnect state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001165 /* stop Timer T200 */
1166 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001167 /* go to idle state */
1168 lapd_dl_flush_tx(dl);
1169 lapd_dl_flush_send(dl);
1170 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1171 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1172 msgb_free(msg);
1173 return 0;
1174 case LAPD_STATE_IDLE:
1175 /* 5.4.5 all other frame types shall be discarded */
1176 default:
1177 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
Philipp Maier08177d32016-12-08 17:23:26 +01001178 "(discarding) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001179 msgb_free(msg);
1180 return 0;
1181 }
Philipp Maier08177d32016-12-08 17:23:26 +01001182 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state (dl=%p)\n", dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001183 /* stop Timer T200 */
1184 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001185 /* compare UA with SABME if contention resolution is applied */
1186 if (dl->tx_hist[0].msg->len) {
1187 if (length != (dl->tx_hist[0].msg->len)
1188 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1189 length)) {
1190 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
Philipp Maier08177d32016-12-08 17:23:26 +01001191 "mismatches **** (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001192 rc = send_dl_simple(PRIM_DL_REL,
1193 PRIM_OP_INDICATION, lctx);
1194 msgb_free(msg);
1195 /* go to idle state */
1196 lapd_dl_flush_tx(dl);
1197 lapd_dl_flush_send(dl);
1198 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1199 return 0;
1200 }
1201 }
1202 /* set Vs, Vr and Va to 0 */
1203 dl->v_send = dl->v_recv = dl->v_ack = 0;
1204 /* clear tx_hist */
1205 lapd_dl_flush_hist(dl);
1206 /* enter multiple-frame-established state */
1207 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1208 /* send outstanding frames, if any (resume / reconnect) */
1209 lapd_send_i(lctx, __LINE__);
1210 /* send notification to L3 */
1211 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1212 msgb_free(msg);
1213 break;
1214 case LAPD_U_FRMR:
Philipp Maier08177d32016-12-08 17:23:26 +01001215 LOGP(DLLAPD, LOGL_NOTICE,
1216 "Frame reject received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001217 /* send MDL ERROR INIDCATION to L3 */
1218 mdl_error(MDL_CAUSE_FRMR, lctx);
1219 msgb_free(msg);
1220 /* reestablish */
1221 if (!dl->reestablish)
1222 break;
Philipp Maier08177d32016-12-08 17:23:26 +01001223 LOGP(DLLAPD, LOGL_NOTICE,
1224 "Performing reestablishment. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001225 rc = lapd_reestablish(dl);
1226 break;
1227 default:
1228 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001229 LOGP(DLLAPD, LOGL_NOTICE,
1230 "Unnumbered frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001231 msgb_free(msg);
1232 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1233 return -EINVAL;
1234 }
1235 return rc;
1236}
1237
1238/* Receive a LAPD S (Supervisory) message from L1 */
1239static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1240{
1241 struct lapd_datalink *dl = lctx->dl;
1242 int length = lctx->length;
1243
1244 if (length > 0 || lctx->more) {
1245 /* G.4.3 If a supervisory frame is received with L>0 or
1246 * with the M bit set to "1", an MDL-ERROR-INDICATION
1247 * primitive with cause "S frame with incorrect
1248 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001249 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001250 "S frame with incorrect parameters (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001251 msgb_free(msg);
1252 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1253 return -EIO;
1254 }
1255
1256 if (lctx->cr == dl->cr.rem2loc.resp
1257 && lctx->p_f
1258 && dl->state != LAPD_STATE_TIMER_RECOV) {
1259 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001260 LOGP(DLLAPD, LOGL_NOTICE,
1261 "S frame response with F=1 error (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001262 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1263 }
1264
1265 switch (dl->state) {
1266 case LAPD_STATE_IDLE:
1267 /* if P=1, respond DM with F=1 (5.2.2) */
1268 /* 5.4.5 all other frame types shall be discarded */
1269 if (lctx->p_f)
1270 lapd_send_dm(lctx); /* F=P */
1271 /* fall though */
1272 case LAPD_STATE_SABM_SENT:
1273 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001274 LOGP(DLLAPD, LOGL_NOTICE,
1275 "S frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001276 msgb_free(msg);
1277 return 0;
1278 }
1279 switch (lctx->s_u) {
1280 case LAPD_S_RR:
Philipp Maier08177d32016-12-08 17:23:26 +01001281 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s (dl=%p)\n",
1282 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02001283 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1284 lapd_acknowledge(lctx);
1285
1286 /* 5.5.3.2 */
1287 if (lctx->cr == dl->cr.rem2loc.cmd
1288 && lctx->p_f) {
1289 if (!dl->own_busy && !dl->seq_err_cond) {
1290 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001291 "with polling bit set and we are not "
1292 "busy, so we reply with RR frame "
1293 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001294 lapd_send_rr(lctx, 1, 0);
1295 /* NOTE: In case of sequence error condition,
1296 * the REJ frame has been transmitted when
1297 * entering the condition, so it has not be
1298 * done here
1299 */
1300 } else if (dl->own_busy) {
1301 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
Philipp Maier08177d32016-12-08 17:23:26 +01001302 "with polling bit set and we are busy, "
1303 "so we reply with RR frame response (dl=%p)\n",
1304 dl);
rootaf48bed2011-09-26 11:23:06 +02001305 lapd_send_rnr(lctx, 1, 0);
1306 }
1307 } else if (lctx->cr == dl->cr.rem2loc.resp
1308 && lctx->p_f
1309 && dl->state == LAPD_STATE_TIMER_RECOV) {
1310 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1311 "and we are in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001312 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001313 /* V(S) to the N(R) in the RR frame */
1314 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001315 /* stop Timer T200 */
1316 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001317 /* 5.5.7 Clear timer recovery condition */
1318 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1319 }
1320 /* Send message, if possible due to acknowledged data */
1321 lapd_send_i(lctx, __LINE__);
1322
1323 break;
1324 case LAPD_S_RNR:
Philipp Maier08177d32016-12-08 17:23:26 +01001325 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s (dl=%p)\n",
1326 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02001327 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1328 lapd_acknowledge(lctx);
1329
1330 /* 5.5.5 */
1331 /* Set peer receiver busy condition */
1332 dl->peer_busy = 1;
1333
1334 if (lctx->p_f) {
1335 if (lctx->cr == dl->cr.rem2loc.cmd) {
1336 if (!dl->own_busy) {
1337 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1338 "command and we are not busy, "
1339 "so we reply with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001340 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001341 /* Send RR with F=1 */
1342 lapd_send_rr(lctx, 1, 0);
1343 } else {
1344 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1345 "command and we are busy, so "
1346 "we reply with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001347 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001348 /* Send RNR with F=1 */
1349 lapd_send_rnr(lctx, 1, 0);
1350 }
1351 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1352 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1353 "and we in timer recovery state, so "
Philipp Maier08177d32016-12-08 17:23:26 +01001354 "we leave that state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001355 /* 5.5.7 Clear timer recovery condition */
1356 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1357 /* V(S) to the N(R) in the RNR frame */
1358 dl->v_send = lctx->n_recv;
1359 }
1360 } else
1361 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
Philipp Maier08177d32016-12-08 17:23:26 +01001362 "received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001363
1364 /* Send message, if possible due to acknowledged data */
1365 lapd_send_i(lctx, __LINE__);
1366
1367 break;
1368 case LAPD_S_REJ:
Philipp Maier08177d32016-12-08 17:23:26 +01001369 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s (dl=%p)\n",
1370 lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02001371 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1372 lapd_acknowledge(lctx);
1373
1374 /* 5.5.4.1 */
1375 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1376 /* Clear an existing peer receiver busy condition */
1377 dl->peer_busy = 0;
1378 /* V(S) and V(A) to the N(R) in the REJ frame */
1379 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001380 /* stop Timer T200 */
1381 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001382 /* 5.5.3.2 */
1383 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1384 if (!dl->own_busy && !dl->seq_err_cond) {
1385 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1386 "command not in timer recovery "
1387 "state and not in own busy "
1388 "condition received, so we "
1389 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001390 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001391 lapd_send_rr(lctx, 1, 0);
1392 /* NOTE: In case of sequence error
1393 * condition, the REJ frame has been
1394 * transmitted when entering the
1395 * condition, so it has not be done
1396 * here
1397 */
1398 } else if (dl->own_busy) {
1399 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1400 "command not in timer recovery "
1401 "state and in own busy "
1402 "condition received, so we "
1403 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001404 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001405 lapd_send_rnr(lctx, 1, 0);
1406 }
1407 } else
1408 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1409 "polling command not in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001410 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001411 /* send MDL ERROR INIDCATION to L3 */
1412 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
Philipp Maier08177d32016-12-08 17:23:26 +01001413 LOGP(DLLAPD, LOGL_ERROR,
1414 "unsolicited supervisory response! (dl=%p)\n",
1415 dl);
rootaf48bed2011-09-26 11:23:06 +02001416 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1417 }
1418
1419 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1420 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
Philipp Maier08177d32016-12-08 17:23:26 +01001421 "recovery state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001422 /* Clear an existing peer receiver busy condition */
1423 dl->peer_busy = 0;
1424 /* V(S) and V(A) to the N(R) in the REJ frame */
1425 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001426 /* stop Timer T200 */
1427 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001428 /* 5.5.7 Clear timer recovery condition */
1429 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1430 } else {
1431 /* 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;
1435 /* 5.5.3.2 */
1436 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1437 if (!dl->own_busy && !dl->seq_err_cond) {
1438 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1439 "command in timer recovery "
1440 "state and not in own busy "
1441 "condition received, so we "
1442 "respond with RR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001443 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001444 lapd_send_rr(lctx, 1, 0);
1445 /* NOTE: In case of sequence error
1446 * condition, the REJ frame has been
1447 * transmitted when entering the
1448 * condition, so it has not be done
1449 * here
1450 */
1451 } else if (dl->own_busy) {
1452 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1453 "command in timer recovery "
1454 "state and in own busy "
1455 "condition received, so we "
1456 "respond with RNR final "
Philipp Maier08177d32016-12-08 17:23:26 +01001457 "response (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001458 lapd_send_rnr(lctx, 1, 0);
1459 }
1460 } else
1461 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1462 "polling command in timer recovery "
Philipp Maier08177d32016-12-08 17:23:26 +01001463 "state received (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001464 }
1465
1466 /* FIXME: 5.5.4.2 2) */
1467
1468 /* Send message, if possible due to acknowledged data */
1469 lapd_send_i(lctx, __LINE__);
1470
1471 break;
1472 default:
1473 /* G.3.1 */
Philipp Maier08177d32016-12-08 17:23:26 +01001474 LOGP(DLLAPD, LOGL_ERROR,
1475 "Supervisory frame not allowed. (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001476 msgb_free(msg);
1477 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1478 return -EINVAL;
1479 }
1480 msgb_free(msg);
1481 return 0;
1482}
1483
1484/* Receive a LAPD I (Information) message from L1 */
1485static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1486{
1487 struct lapd_datalink *dl = lctx->dl;
1488 //uint8_t nr = lctx->n_recv;
1489 uint8_t ns = lctx->n_send;
1490 int length = lctx->length;
1491 int rc;
1492
Philipp Maier08177d32016-12-08 17:23:26 +01001493 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u) (dl=%p)\n",
1494 lapd_state_names[dl->state], lctx->sapi, dl);
rootaf48bed2011-09-26 11:23:06 +02001495
1496 /* G.2.2 Wrong value of the C/R bit */
1497 if (lctx->cr == dl->cr.rem2loc.resp) {
Philipp Maier08177d32016-12-08 17:23:26 +01001498 LOGP(DLLAPD, LOGL_ERROR,
1499 "I frame response not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001500 msgb_free(msg);
1501 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1502 return -EINVAL;
1503 }
1504
1505 if (length == 0 || length > lctx->n201) {
1506 /* G.4.2 If the length indicator of an I frame is set
1507 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1508 * primitive with cause "I frame with incorrect length"
1509 * is sent to the mobile management entity. */
Philipp Maier08177d32016-12-08 17:23:26 +01001510 LOGP(DLLAPD, LOGL_ERROR,
1511 "I frame length not allowed (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001512 msgb_free(msg);
1513 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1514 return -EIO;
1515 }
1516
1517 /* G.4.2 If the numerical value of L is L<N201 and the M
1518 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1519 * cause "I frame with incorrect use of M bit" is sent to the
1520 * mobile management entity. */
1521 if (lctx->more && length < lctx->n201) {
Philipp Maier08177d32016-12-08 17:23:26 +01001522 LOGP(DLLAPD, LOGL_ERROR,
1523 "I frame with M bit too short (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001524 msgb_free(msg);
1525 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1526 return -EIO;
1527 }
1528
1529 switch (dl->state) {
1530 case LAPD_STATE_IDLE:
1531 /* if P=1, respond DM with F=1 (5.2.2) */
1532 /* 5.4.5 all other frame types shall be discarded */
1533 if (lctx->p_f)
1534 lapd_send_dm(lctx); /* F=P */
1535 /* fall though */
1536 case LAPD_STATE_SABM_SENT:
1537 case LAPD_STATE_DISC_SENT:
Philipp Maier08177d32016-12-08 17:23:26 +01001538 LOGP(DLLAPD, LOGL_NOTICE,
1539 "I frame ignored in this state (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001540 msgb_free(msg);
1541 return 0;
1542 }
1543
1544 /* 5.7.1: N(s) sequence error */
1545 if (ns != dl->v_recv) {
1546 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
Philipp Maier08177d32016-12-08 17:23:26 +01001547 "V(R)=%u (dl=%p)\n", ns, dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001548 /* discard data */
1549 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001550 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001551 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001552 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001553 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001554 lapd_send_rej(lctx, lctx->p_f);
1555 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001556 /* If there are two subsequent sequence errors received,
1557 * ignore it. (Ignore every second subsequent error.)
1558 * This happens if our reply with the REJ is too slow,
1559 * so the remote gets a T200 timeout and sends another
1560 * frame with a sequence error.
1561 * Test showed that replying with two subsequent REJ
1562 * messages could the remote L2 process to abort.
1563 * Replying too slow shouldn't happen, but may happen
1564 * over serial link between BB and LAPD.
1565 */
1566 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001567 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001568 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1569 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1570 lapd_acknowledge(lctx); /* V(A) is also set here */
1571
1572 /* Send message, if possible due to acknowledged data */
1573 lapd_send_i(lctx, __LINE__);
1574
1575 return 0;
rootaf48bed2011-09-26 11:23:06 +02001576 }
1577 dl->seq_err_cond = 0;
1578
1579 /* Increment receiver state */
1580 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
Philipp Maier08177d32016-12-08 17:23:26 +01001581 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u (dl=%p)\n",
1582 dl->v_recv, dl);
rootaf48bed2011-09-26 11:23:06 +02001583
1584 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1585 lapd_acknowledge(lctx); /* V(A) is also set here */
1586
1587 /* Only if we are not in own receiver busy condition */
1588 if (!dl->own_busy) {
1589 /* if the frame carries a complete segment */
1590 if (!lctx->more && !dl->rcv_buffer) {
Philipp Maier08177d32016-12-08 17:23:26 +01001591 LOGP(DLLAPD, LOGL_INFO,
1592 "message in single I frame (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001593 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001594 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001595 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1596 msg);
1597 } else {
1598 /* create rcv_buffer */
1599 if (!dl->rcv_buffer) {
1600 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001601 "I frames (first message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001602 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1603 "LAPD RX");
1604 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1605 }
1606 /* concat. rcv_buffer */
1607 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1608 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
Philipp Maier08177d32016-12-08 17:23:26 +01001609 "overflow! (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001610 } else {
1611 memcpy(msgb_put(dl->rcv_buffer, length),
1612 msg->l3h, length);
1613 }
1614 /* if the last segment was received */
1615 if (!lctx->more) {
1616 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001617 "I frames (last message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001618 rc = send_dl_l3(PRIM_DL_DATA,
1619 PRIM_OP_INDICATION, lctx,
1620 dl->rcv_buffer);
1621 dl->rcv_buffer = NULL;
1622 } else
1623 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
Philipp Maier08177d32016-12-08 17:23:26 +01001624 "I frames (next message) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001625 msgb_free(msg);
1626
1627 }
1628 } else
1629 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1630 "busy condition\n");
1631
1632 /* Check for P bit */
1633 if (lctx->p_f) {
1634 /* 5.5.2.1 */
1635 /* check if we are not in own receiver busy */
1636 if (!dl->own_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001637 LOGP(DLLAPD, LOGL_INFO,
1638 "we are not busy, send RR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001639 /* Send RR with F=1 */
1640 rc = lapd_send_rr(lctx, 1, 0);
1641 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001642 LOGP(DLLAPD, LOGL_INFO,
1643 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001644 /* Send RNR with F=1 */
1645 rc = lapd_send_rnr(lctx, 1, 0);
1646 }
1647 } else {
1648 /* 5.5.2.2 */
1649 /* check if we are not in own receiver busy */
1650 if (!dl->own_busy) {
1651 /* NOTE: V(R) is already set above */
1652 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001653
1654 /* if update_pending_iframe returns 0 it updated
1655 * the lapd header of an iframe in the tx queue */
1656 if (rc && dl->update_pending_frames)
1657 rc = dl->update_pending_frames(lctx);
1658
rootaf48bed2011-09-26 11:23:06 +02001659 if (rc) {
1660 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
Philipp Maier08177d32016-12-08 17:23:26 +01001661 "have no pending data, send RR (dl=%p)\n",
1662 dl);
rootaf48bed2011-09-26 11:23:06 +02001663 /* Send RR with F=0 */
1664 return lapd_send_rr(lctx, 0, 0);
1665 }
1666 /* all I or one RR is sent, we are done */
1667 return 0;
1668 } else {
Philipp Maier08177d32016-12-08 17:23:26 +01001669 LOGP(DLLAPD, LOGL_INFO,
1670 "we are busy, send RNR (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001671 /* Send RNR with F=0 */
1672 rc = lapd_send_rnr(lctx, 0, 0);
1673 }
1674 }
1675
1676 /* Send message, if possible due to acknowledged data */
1677 lapd_send_i(lctx, __LINE__);
1678
1679 return rc;
1680}
1681
1682/* Receive a LAPD message from L1 */
1683int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1684{
1685 int rc;
1686
1687 switch (lctx->format) {
1688 case LAPD_FORM_U:
1689 rc = lapd_rx_u(msg, lctx);
1690 break;
1691 case LAPD_FORM_S:
1692 rc = lapd_rx_s(msg, lctx);
1693 break;
1694 case LAPD_FORM_I:
1695 rc = lapd_rx_i(msg, lctx);
1696 break;
1697 default:
Philipp Maier08177d32016-12-08 17:23:26 +01001698 LOGP(DLLAPD, LOGL_NOTICE,
1699 "unknown LAPD format (dl=%p)\n", lctx->dl);
rootaf48bed2011-09-26 11:23:06 +02001700 msgb_free(msg);
1701 rc = -EINVAL;
1702 }
1703 return rc;
1704}
1705
1706/* L3 -> L2 */
1707
1708/* send unit data */
1709static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1710{
1711 struct lapd_datalink *dl = lctx->dl;
1712 struct msgb *msg = dp->oph.msg;
1713 struct lapd_msg_ctx nctx;
1714
1715 memcpy(&nctx, lctx, sizeof(nctx));
1716 /* keep nctx.ldp */
1717 /* keep nctx.sapi */
1718 /* keep nctx.tei */
1719 nctx.cr = dl->cr.loc2rem.cmd;
1720 nctx.format = LAPD_FORM_U;
1721 nctx.s_u = LAPD_U_UI;
1722 /* keep nctx.p_f */
1723 nctx.length = msg->len;
1724 nctx.more = 0;
1725
1726 return dl->send_ph_data_req(&nctx, msg);
1727}
1728
1729/* request link establishment */
1730static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1731{
1732 struct lapd_datalink *dl = lctx->dl;
1733 struct msgb *msg = dp->oph.msg;
1734 struct lapd_msg_ctx nctx;
1735
1736 if (msg->len)
1737 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
Philipp Maier08177d32016-12-08 17:23:26 +01001738 "(SABM) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001739 else
Philipp Maier08177d32016-12-08 17:23:26 +01001740 LOGP(DLLAPD, LOGL_INFO,
1741 "perform normal establishm. (SABM), (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001742
1743 /* Flush send-queue */
1744 /* Clear send-buffer */
1745 lapd_dl_flush_send(dl);
1746 /* be sure that history is empty */
1747 lapd_dl_flush_hist(dl);
1748
1749 /* save message context for further use */
1750 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1751
1752 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001753 msgb_free(dl->rcv_buffer);
1754 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001755
1756 /* assemble message */
1757 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1758 /* keep nctx.ldp */
1759 /* keep nctx.sapi */
1760 /* keep nctx.tei */
1761 nctx.cr = dl->cr.loc2rem.cmd;
1762 nctx.format = LAPD_FORM_U;
1763 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1764 nctx.p_f = 1;
1765 nctx.length = msg->len;
1766 nctx.more = 0;
1767
1768 /* Transmit-buffer carries exactly one segment */
1769 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1770 msgb_put(dl->tx_hist[0].msg, msg->len);
1771 if (msg->len)
1772 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1773 dl->tx_hist[0].more = 0;
1774 /* set Vs to 0, because it is used as index when resending SABM */
1775 dl->v_send = 0;
1776
1777 /* Set states */
1778 dl->own_busy = dl->peer_busy = 0;
1779 dl->retrans_ctr = 0;
1780 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1781
1782 /* Tramsmit and start T200 */
1783 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001784 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001785
1786 return 0;
1787}
1788
1789/* send data */
1790static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1791{
1792 struct lapd_datalink *dl = lctx->dl;
1793 struct msgb *msg = dp->oph.msg;
1794
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001795 if (msgb_l3len(msg) == 0) {
1796 LOGP(DLLAPD, LOGL_ERROR,
Philipp Maier08177d32016-12-08 17:23:26 +01001797 "writing an empty message is not possible. (dl=%p)\n", dl);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001798 msgb_free(msg);
1799 return -1;
1800 }
1801
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001802 LOGP(DLLAPD, LOGL_INFO,
Philipp Maier08177d32016-12-08 17:23:26 +01001803 "writing message to send-queue: l3len: %d (dl=%p)\n",
1804 msgb_l3len(msg), dl);
rootaf48bed2011-09-26 11:23:06 +02001805
1806 /* Write data into the send queue */
1807 msgb_enqueue(&dl->send_queue, msg);
1808
1809 /* Send message, if possible */
1810 lapd_send_i(&dl->lctx, __LINE__);
1811
1812 return 0;
1813}
1814
1815/* Send next I frame from queued/buffered data */
1816static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1817{
1818 struct lapd_datalink *dl = lctx->dl;
1819 uint8_t k = dl->k;
1820 uint8_t h;
1821 struct msgb *msg;
1822 int length, left;
1823 int rc = - 1; /* we sent nothing */
1824 struct lapd_msg_ctx nctx;
1825
1826
Philipp Maier08177d32016-12-08 17:23:26 +01001827 LOGP(DLLAPD, LOGL_INFO,
1828 "%s() called from line %d (dl=%p)\n", __func__, line, dl);
rootaf48bed2011-09-26 11:23:06 +02001829
1830 next_frame:
1831
1832 if (dl->peer_busy) {
Philipp Maier08177d32016-12-08 17:23:26 +01001833 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001834 return rc;
1835 }
1836
1837 if (dl->state == LAPD_STATE_TIMER_RECOV) {
Philipp Maier08177d32016-12-08 17:23:26 +01001838 LOGP(DLLAPD, LOGL_INFO,
1839 "timer recovery, not sending (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001840 return rc;
1841 }
1842
1843 /* If the send state variable V(S) is equal to V(A) plus k
1844 * (where k is the maximum number of outstanding I frames - see
1845 * subclause 5.8.4), the data link layer entity shall not transmit any
1846 * new I frames, but shall retransmit an I frame as a result
1847 * of the error recovery procedures as described in subclauses 5.5.4 and
1848 * 5.5.7. */
1849 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1850 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
Philipp Maier08177d32016-12-08 17:23:26 +01001851 "more (k=%u V(S)=%u V(A)=%u) (dl=%p)\n", k, dl->v_send,
1852 dl->v_ack, dl);
rootaf48bed2011-09-26 11:23:06 +02001853 return rc;
1854 }
1855
1856 h = do_mod(dl->v_send, dl->range_hist);
1857
1858 /* if we have no tx_hist yet, we create it */
1859 if (!dl->tx_hist[h].msg) {
1860 /* Get next message into send-buffer, if any */
1861 if (!dl->send_buffer) {
1862 next_message:
1863 dl->send_out = 0;
1864 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1865 /* No more data to be sent */
1866 if (!dl->send_buffer)
1867 return rc;
1868 LOGP(DLLAPD, LOGL_INFO, "get message from "
Philipp Maier08177d32016-12-08 17:23:26 +01001869 "send-queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001870 }
1871
1872 /* How much is left in the send-buffer? */
1873 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1874 /* Segment, if data exceeds N201 */
1875 length = left;
1876 if (length > lctx->n201)
1877 length = lctx->n201;
1878 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
Philipp Maier08177d32016-12-08 17:23:26 +01001879 "length %d first byte %02x (dl=%p)\n",
rootaf48bed2011-09-26 11:23:06 +02001880 msgb_l3len(dl->send_buffer), dl->send_out, left,
Philipp Maier08177d32016-12-08 17:23:26 +01001881 lctx->n201, length, dl->send_buffer->l3h[0], dl);
rootaf48bed2011-09-26 11:23:06 +02001882 /* If message in send-buffer is completely sent */
1883 if (left == 0) {
1884 msgb_free(dl->send_buffer);
1885 dl->send_buffer = NULL;
1886 goto next_message;
1887 }
1888
Philipp Maier08177d32016-12-08 17:23:26 +01001889 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d (dl=%p)\n",
1890 (left > length) ? "segment " : "", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001891
1892 /* Create I frame (segment) and transmit-buffer content */
1893 msg = lapd_msgb_alloc(length, "LAPD I");
1894 msg->l3h = msgb_put(msg, length);
1895 /* assemble message */
1896 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1897 /* keep nctx.ldp */
1898 /* keep nctx.sapi */
1899 /* keep nctx.tei */
1900 nctx.cr = dl->cr.loc2rem.cmd;
1901 nctx.format = LAPD_FORM_I;
1902 nctx.p_f = 0;
1903 nctx.n_send = dl->v_send;
1904 nctx.n_recv = dl->v_recv;
1905 nctx.length = length;
1906 if (left > length)
1907 nctx.more = 1;
1908 else
1909 nctx.more = 0;
1910 if (length)
1911 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1912 length);
1913 /* store in tx_hist */
1914 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1915 msgb_put(dl->tx_hist[h].msg, msg->len);
1916 if (length)
1917 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1918 dl->tx_hist[h].more = nctx.more;
1919 /* Add length to track how much is already in the tx buffer */
1920 dl->send_out += length;
1921 } else {
1922 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
Philipp Maier08177d32016-12-08 17:23:26 +01001923 "V(S)=%d (dl=%p)\n", dl->v_send, dl);
rootaf48bed2011-09-26 11:23:06 +02001924
1925 /* Create I frame (segment) from tx_hist */
1926 length = dl->tx_hist[h].msg->len;
1927 msg = lapd_msgb_alloc(length, "LAPD I resend");
1928 msg->l3h = msgb_put(msg, length);
1929 /* assemble message */
1930 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1931 /* keep nctx.ldp */
1932 /* keep nctx.sapi */
1933 /* keep nctx.tei */
1934 nctx.cr = dl->cr.loc2rem.cmd;
1935 nctx.format = LAPD_FORM_I;
1936 nctx.p_f = 0;
1937 nctx.n_send = dl->v_send;
1938 nctx.n_recv = dl->v_recv;
1939 nctx.length = length;
1940 nctx.more = dl->tx_hist[h].more;
1941 if (length)
1942 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1943 }
1944
1945 /* The value of the send state variable V(S) shall be incremented by 1
1946 * at the end of the transmission of the I frame */
1947 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1948
1949 /* If timer T200 is not running at the time right before transmitting a
1950 * frame, when the PH-READY-TO-SEND primitive is received from the
1951 * physical layer., it shall be set. */
1952 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001953 /* stop Timer T203, if running */
1954 lapd_stop_t203(dl);
1955 /* start Timer T200 */
1956 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001957 }
1958
1959 dl->send_ph_data_req(&nctx, msg);
1960
1961 rc = 0; /* we sent something */
1962 goto next_frame;
1963}
1964
1965/* request link suspension */
1966static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1967{
1968 struct lapd_datalink *dl = lctx->dl;
1969 struct msgb *msg = dp->oph.msg;
1970
Philipp Maier08177d32016-12-08 17:23:26 +01001971 LOGP(DLLAPD, LOGL_INFO, "perform suspension (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001972
1973 /* put back the send-buffer to the send-queue (first position) */
1974 if (dl->send_buffer) {
1975 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
Philipp Maier08177d32016-12-08 17:23:26 +01001976 "queue (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001977 llist_add(&dl->send_buffer->list, &dl->send_queue);
1978 dl->send_buffer = NULL;
1979 } else
Philipp Maier08177d32016-12-08 17:23:26 +01001980 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02001981
1982 /* Clear transmit buffer, but keep send buffer */
1983 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001984 /* Stop timers (there is no state change, so we must stop all timers */
1985 lapd_stop_t200(dl);
1986 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001987
1988 msgb_free(msg);
1989
1990 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1991}
1992
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001993/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001994static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1995{
1996 struct lapd_datalink *dl = lctx->dl;
1997 struct msgb *msg = dp->oph.msg;
1998 struct lapd_msg_ctx nctx;
1999
Philipp Maier08177d32016-12-08 17:23:26 +01002000 LOGP(DLLAPD, LOGL_INFO,
2001 "perform re-establishment (SABM) length=%d (dl=%p)\n",
2002 msg->len, dl);
rootaf48bed2011-09-26 11:23:06 +02002003
2004 /* be sure that history is empty */
2005 lapd_dl_flush_hist(dl);
2006
2007 /* save message context for further use */
2008 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
2009
2010 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002011 msgb_free(dl->send_buffer);
2012 dl->send_buffer = NULL;
2013
rootaf48bed2011-09-26 11:23:06 +02002014 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002015 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02002016 /* Write data into the send buffer, to be sent first */
2017 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002018 } else {
2019 msgb_free(msg);
2020 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01002021 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02002022 }
rootaf48bed2011-09-26 11:23:06 +02002023
2024 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02002025 msgb_free(dl->rcv_buffer);
2026 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02002027
2028 /* Create new msgb (old one is now free) */
2029 msg = lapd_msgb_alloc(0, "LAPD SABM");
2030 msg->l3h = msg->data;
2031 /* assemble message */
2032 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2033 /* keep nctx.ldp */
2034 /* keep nctx.sapi */
2035 /* keep nctx.tei */
2036 nctx.cr = dl->cr.loc2rem.cmd;
2037 nctx.format = LAPD_FORM_U;
2038 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
2039 nctx.p_f = 1;
2040 nctx.length = 0;
2041 nctx.more = 0;
2042
2043 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2044 msgb_put(dl->tx_hist[0].msg, msg->len);
2045 if (msg->len)
2046 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2047 dl->tx_hist[0].more = 0;
2048 /* set Vs to 0, because it is used as index when resending SABM */
2049 dl->v_send = 0;
2050
2051 /* Set states */
2052 dl->own_busy = dl->peer_busy = 0;
2053 dl->retrans_ctr = 0;
2054 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2055
2056 /* Tramsmit and start T200 */
2057 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002058 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002059
2060 return 0;
2061}
2062
2063/* requesst release of link */
2064static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2065{
2066 struct lapd_datalink *dl = lctx->dl;
2067 struct msgb *msg = dp->oph.msg;
2068 struct lapd_msg_ctx nctx;
2069
2070 /* local release */
2071 if (dp->u.rel_req.mode) {
Philipp Maier08177d32016-12-08 17:23:26 +01002072 LOGP(DLLAPD, LOGL_INFO, "perform local release (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002073 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002074 /* stop Timer T200 */
2075 lapd_stop_t200(dl);
2076 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002077 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2078 /* flush buffers */
2079 lapd_dl_flush_tx(dl);
2080 lapd_dl_flush_send(dl);
2081 /* send notification to L3 */
2082 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2083 }
2084
2085 /* in case we are already disconnecting */
2086 if (dl->state == LAPD_STATE_DISC_SENT)
2087 return -EBUSY;
2088
2089 /* flush tx_hist */
2090 lapd_dl_flush_hist(dl);
2091
Philipp Maier08177d32016-12-08 17:23:26 +01002092 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC) (dl=%p)\n", dl);
rootaf48bed2011-09-26 11:23:06 +02002093
2094 /* Push LAPD header on msgb */
2095 /* assemble message */
2096 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2097 /* keep nctx.ldp */
2098 /* keep nctx.sapi */
2099 /* keep nctx.tei */
2100 nctx.cr = dl->cr.loc2rem.cmd;
2101 nctx.format = LAPD_FORM_U;
2102 nctx.s_u = LAPD_U_DISC;
2103 nctx.p_f = 1;
2104 nctx.length = 0;
2105 nctx.more = 0;
2106
2107 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2108 msgb_put(dl->tx_hist[0].msg, msg->len);
2109 if (msg->len)
2110 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2111 dl->tx_hist[0].more = 0;
2112 /* set Vs to 0, because it is used as index when resending DISC */
2113 dl->v_send = 0;
2114
2115 /* Set states */
2116 dl->own_busy = dl->peer_busy = 0;
2117 dl->retrans_ctr = 0;
2118 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2119
2120 /* Tramsmit and start T200 */
2121 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002122 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002123
2124 return 0;
2125}
2126
2127/* request release of link in idle state */
2128static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2129 struct lapd_msg_ctx *lctx)
2130{
2131 struct lapd_datalink *dl = lctx->dl;
2132 struct msgb *msg = dp->oph.msg;
2133
2134 msgb_free(msg);
2135
2136 /* send notification to L3 */
2137 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2138}
2139
2140/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002141static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002142 uint32_t states;
2143 int prim, op;
2144 const char *name;
2145 int (*rout) (struct osmo_dlsap_prim *dp,
2146 struct lapd_msg_ctx *lctx);
2147} l2downstatelist[] = {
2148 /* create and send UI command */
2149 {ALL_STATES,
2150 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2151 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2152
2153 /* create and send SABM command */
2154 {SBIT(LAPD_STATE_IDLE),
2155 PRIM_DL_EST, PRIM_OP_REQUEST,
2156 "DL-ESTABLISH-REQUEST", lapd_est_req},
2157
2158 /* create and send I command */
2159 {SBIT(LAPD_STATE_MF_EST) |
2160 SBIT(LAPD_STATE_TIMER_RECOV),
2161 PRIM_DL_DATA, PRIM_OP_REQUEST,
2162 "DL-DATA-REQUEST", lapd_data_req},
2163
2164 /* suspend datalink */
2165 {SBIT(LAPD_STATE_MF_EST) |
2166 SBIT(LAPD_STATE_TIMER_RECOV),
2167 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2168 "DL-SUSPEND-REQUEST", lapd_susp_req},
2169
2170 /* create and send SABM command (resume) */
2171 {SBIT(LAPD_STATE_MF_EST) |
2172 SBIT(LAPD_STATE_TIMER_RECOV),
2173 PRIM_DL_RES, PRIM_OP_REQUEST,
2174 "DL-RESUME-REQUEST", lapd_res_req},
2175
2176 /* create and send SABM command (reconnect) */
2177 {SBIT(LAPD_STATE_IDLE) |
2178 SBIT(LAPD_STATE_MF_EST) |
2179 SBIT(LAPD_STATE_TIMER_RECOV),
2180 PRIM_DL_RECON, PRIM_OP_REQUEST,
2181 "DL-RECONNECT-REQUEST", lapd_res_req},
2182
2183 /* create and send DISC command */
2184 {SBIT(LAPD_STATE_SABM_SENT) |
2185 SBIT(LAPD_STATE_MF_EST) |
2186 SBIT(LAPD_STATE_TIMER_RECOV) |
2187 SBIT(LAPD_STATE_DISC_SENT),
2188 PRIM_DL_REL, PRIM_OP_REQUEST,
2189 "DL-RELEASE-REQUEST", lapd_rel_req},
2190
2191 /* release in idle state */
2192 {SBIT(LAPD_STATE_IDLE),
2193 PRIM_DL_REL, PRIM_OP_REQUEST,
2194 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2195};
2196
2197#define L2DOWNSLLEN \
2198 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2199
2200int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2201{
2202 struct lapd_datalink *dl = lctx->dl;
2203 int i, supported = 0;
2204 struct msgb *msg = dp->oph.msg;
2205 int rc;
2206
2207 /* find function for current state and message */
2208 for (i = 0; i < L2DOWNSLLEN; i++) {
2209 if (dp->oph.primitive == l2downstatelist[i].prim
2210 && dp->oph.operation == l2downstatelist[i].op) {
2211 supported = 1;
2212 if ((SBIT(dl->state) & l2downstatelist[i].states))
2213 break;
2214 }
2215 }
2216 if (!supported) {
Philipp Maier08177d32016-12-08 17:23:26 +01002217 LOGP(DLLAPD, LOGL_NOTICE,
2218 "Message %u/%u unsupported. (dl=%p)\n", dp->oph.primitive,
2219 dp->oph.operation, dl);
rootaf48bed2011-09-26 11:23:06 +02002220 msgb_free(msg);
2221 return 0;
2222 }
2223 if (i == L2DOWNSLLEN) {
2224 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
Philipp Maier08177d32016-12-08 17:23:26 +01002225 "state %s. (dl=%p)\n", dp->oph.primitive,
2226 dp->oph.operation, lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02002227 msgb_free(msg);
2228 return 0;
2229 }
2230
Philipp Maier08177d32016-12-08 17:23:26 +01002231 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s (dl=%p)\n",
2232 l2downstatelist[i].name, lapd_state_names[dl->state], dl);
rootaf48bed2011-09-26 11:23:06 +02002233
2234 rc = l2downstatelist[i].rout(dp, lctx);
2235
2236 return rc;
2237}
2238
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002239/*! @} */