blob: bf5c388afa8b4d909dfb415da20195f1236abf27 [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;
200 LOGP(DLLAPD, LOGL_INFO, "start T200\n");
201 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;
208 LOGP(DLLAPD, LOGL_INFO, "start T203\n");
209 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;
216 LOGP(DLLAPD, LOGL_INFO, "stop T200\n");
217 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;
224 LOGP(DLLAPD, LOGL_INFO, "stop T203\n");
225 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{
230 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s\n",
231 lapd_state_names[dl->state], lapd_state_names[state]);
232
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, "
292 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
293
294 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
295
296 if (!tall_lapd_ctx)
297 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100298 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
299 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200300}
301
302/* reset to IDLE state */
303void lapd_dl_reset(struct lapd_datalink *dl)
304{
305 if (dl->state == LAPD_STATE_IDLE)
306 return;
307 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
308 /* enter idle state (and remove eventual cont_res) */
309 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
310 /* flush buffer */
311 lapd_dl_flush_tx(dl);
312 lapd_dl_flush_send(dl);
313 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200314 msgb_free(dl->rcv_buffer);
315 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200316 /* stop Timers */
317 lapd_stop_t200(dl);
318 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200319}
320
321/* reset and de-allocate history buffer */
322void lapd_dl_exit(struct lapd_datalink *dl)
323{
324 /* free all ressources except history buffer */
325 lapd_dl_reset(dl);
326 /* free history buffer list */
327 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200328 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200329}
330
331/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
332int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
333{
334 switch (mode) {
335 case LAPD_MODE_USER:
336 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
337 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
338 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
339 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
340 break;
341 case LAPD_MODE_NETWORK:
342 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
343 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
344 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
345 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
346 break;
347 default:
348 return -EINVAL;
349 }
350 dl->mode = mode;
351
352 return 0;
353}
354
355/* send DL message with optional msgb */
356static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
357 struct msgb *msg)
358{
359 struct lapd_datalink *dl = lctx->dl;
360 struct osmo_dlsap_prim dp;
361
362 osmo_prim_init(&dp.oph, 0, prim, op, msg);
363 return dl->send_dlsap(&dp, lctx);
364}
365
366/* send simple DL message */
367static inline int send_dl_simple(uint8_t prim, uint8_t op,
368 struct lapd_msg_ctx *lctx)
369{
370 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
371
372 return send_dl_l3(prim, op, lctx, msg);
373}
374
375/* send MDL-ERROR INDICATION */
376static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
377{
378 struct lapd_datalink *dl = lctx->dl;
379 struct osmo_dlsap_prim dp;
380
Max87218ed2017-01-09 14:24:03 +0100381 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %s\n",
382 rsl_rlm_cause_name(cause));
rootaf48bed2011-09-26 11:23:06 +0200383 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
384 dp.u.error_ind.cause = cause;
385 return dl->send_dlsap(&dp, lctx);
386}
387
388/* send UA response */
389static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
390{
391 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
392 struct lapd_msg_ctx nctx;
393 struct lapd_datalink *dl = lctx->dl;
394
395 memcpy(&nctx, lctx, sizeof(nctx));
396 msg->l3h = msgb_put(msg, len);
397 if (len)
398 memcpy(msg->l3h, data, len);
399 /* keep nctx.ldp */
400 /* keep nctx.sapi */
401 /* keep nctx.tei */
402 nctx.cr = dl->cr.loc2rem.resp;
403 nctx.format = LAPD_FORM_U;
404 nctx.s_u = LAPD_U_UA;
405 /* keep nctx.p_f */
406 nctx.length = len;
407 nctx.more = 0;
408
409 return dl->send_ph_data_req(&nctx, msg);
410}
411
412/* send DM response */
413static int lapd_send_dm(struct lapd_msg_ctx *lctx)
414{
415 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
416 struct lapd_msg_ctx nctx;
417 struct lapd_datalink *dl = lctx->dl;
418
419 memcpy(&nctx, lctx, sizeof(nctx));
420 /* keep nctx.ldp */
421 /* keep nctx.sapi */
422 /* keep nctx.tei */
423 nctx.cr = dl->cr.loc2rem.resp;
424 nctx.format = LAPD_FORM_U;
425 nctx.s_u = LAPD_U_DM;
426 /* keep nctx.p_f */
427 nctx.length = 0;
428 nctx.more = 0;
429
430 return dl->send_ph_data_req(&nctx, msg);
431}
432
433/* send RR response / command */
434static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
435{
436 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
437 struct lapd_msg_ctx nctx;
438 struct lapd_datalink *dl = lctx->dl;
439
440 memcpy(&nctx, lctx, sizeof(nctx));
441 /* keep nctx.ldp */
442 /* keep nctx.sapi */
443 /* keep nctx.tei */
444 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
445 nctx.format = LAPD_FORM_S;
446 nctx.s_u = LAPD_S_RR;
447 nctx.p_f = f_bit;
448 nctx.n_recv = dl->v_recv;
449 nctx.length = 0;
450 nctx.more = 0;
451
452 return dl->send_ph_data_req(&nctx, msg);
453}
454
455/* send RNR response / command */
456static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
457{
458 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
459 struct lapd_msg_ctx nctx;
460 struct lapd_datalink *dl = lctx->dl;
461
462 memcpy(&nctx, lctx, sizeof(nctx));
463 /* keep nctx.ldp */
464 /* keep nctx.sapi */
465 /* keep nctx.tei */
466 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
467 nctx.format = LAPD_FORM_S;
468 nctx.s_u = LAPD_S_RNR;
469 nctx.p_f = f_bit;
470 nctx.n_recv = dl->v_recv;
471 nctx.length = 0;
472 nctx.more = 0;
473
474 return dl->send_ph_data_req(&nctx, msg);
475}
476
477/* send REJ response */
478static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
479{
480 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
481 struct lapd_msg_ctx nctx;
482 struct lapd_datalink *dl = lctx->dl;
483
484 memcpy(&nctx, lctx, sizeof(nctx));
485 /* keep nctx.ldp */
486 /* keep nctx.sapi */
487 /* keep nctx.tei */
488 nctx.cr = dl->cr.loc2rem.resp;
489 nctx.format = LAPD_FORM_S;
490 nctx.s_u = LAPD_S_REJ;
491 nctx.p_f = f_bit;
492 nctx.n_recv = dl->v_recv;
493 nctx.length = 0;
494 nctx.more = 0;
495
496 return dl->send_ph_data_req(&nctx, msg);
497}
498
499/* resend SABM or DISC message */
500static int lapd_send_resend(struct lapd_datalink *dl)
501{
502 struct msgb *msg;
503 uint8_t h = do_mod(dl->v_send, dl->range_hist);
504 int length = dl->tx_hist[h].msg->len;
505 struct lapd_msg_ctx nctx;
506
507 /* assemble message */
508 memcpy(&nctx, &dl->lctx, sizeof(nctx));
509 /* keep nctx.ldp */
510 /* keep nctx.sapi */
511 /* keep nctx.tei */
512 nctx.cr = dl->cr.loc2rem.cmd;
513 nctx.format = LAPD_FORM_U;
514 if (dl->state == LAPD_STATE_SABM_SENT)
515 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
516 else
517 nctx.s_u = LAPD_U_DISC;
518 nctx.p_f = 1;
519 nctx.length = length;
520 nctx.more = 0;
521
522 /* Resend SABM/DISC from tx_hist */
523 msg = lapd_msgb_alloc(length, "LAPD resend");
524 msg->l3h = msgb_put(msg, length);
525 if (length)
526 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
527
528 return dl->send_ph_data_req(&nctx, msg);
529}
530
531/* reestablish link */
532static int lapd_reestablish(struct lapd_datalink *dl)
533{
534 struct osmo_dlsap_prim dp;
535 struct msgb *msg;
536
537 msg = lapd_msgb_alloc(0, "DUMMY");
538 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
539
540 return lapd_est_req(&dp, &dl->lctx);
541}
542
543/* Timer callback on T200 expiry */
544static void lapd_t200_cb(void *data)
545{
546 struct lapd_datalink *dl = data;
547
Andreas Eversberg742fc792011-09-27 09:40:25 +0200548 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200549 (int) dl->state);
550
551 switch (dl->state) {
552 case LAPD_STATE_SABM_SENT:
553 /* 5.4.1.3 */
554 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
555 /* send RELEASE INDICATION to L3 */
556 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
557 &dl->lctx);
558 /* send MDL ERROR INIDCATION to L3 */
559 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
560 /* flush tx and send buffers */
561 lapd_dl_flush_tx(dl);
562 lapd_dl_flush_send(dl);
563 /* go back to idle state */
564 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
565 /* NOTE: we must not change any other states or buffers
566 * and queues, since we may reconnect after handover
567 * failure. the buffered messages is replaced there */
568 break;
569 }
570 /* retransmit SABM command */
571 lapd_send_resend(dl);
572 /* increment re-transmission counter */
573 dl->retrans_ctr++;
574 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200575 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200576 break;
577 case LAPD_STATE_DISC_SENT:
578 /* 5.4.4.3 */
579 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
580 /* send RELEASE INDICATION to L3 */
581 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
582 /* send MDL ERROR INIDCATION to L3 */
583 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
584 /* flush tx and send buffers */
585 lapd_dl_flush_tx(dl);
586 lapd_dl_flush_send(dl);
587 /* go back to idle state */
588 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
589 /* NOTE: we must not change any other states or buffers
590 * and queues, since we may reconnect after handover
591 * failure. the buffered messages is replaced there */
592 break;
593 }
594 /* retransmit DISC command */
595 lapd_send_resend(dl);
596 /* increment re-transmission counter */
597 dl->retrans_ctr++;
598 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200599 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200600 break;
601 case LAPD_STATE_MF_EST:
602 /* 5.5.7 */
603 dl->retrans_ctr = 0;
604 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
605 /* fall through */
606 case LAPD_STATE_TIMER_RECOV:
607 dl->retrans_ctr++;
608 if (dl->retrans_ctr < dl->n200) {
609 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
610 uint8_t h = do_mod(vs, dl->range_hist);
611 /* retransmit I frame (V_s-1) with P=1, if any */
612 if (dl->tx_hist[h].msg) {
613 struct msgb *msg;
614 int length = dl->tx_hist[h].msg->len;
615 struct lapd_msg_ctx nctx;
616
617 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
618 " V(S)=%d\n", vs);
619 /* Create I frame (segment) from tx_hist */
620 memcpy(&nctx, &dl->lctx, sizeof(nctx));
621 /* keep nctx.ldp */
622 /* keep nctx.sapi */
623 /* keep nctx.tei */
624 nctx.cr = dl->cr.loc2rem.cmd;
625 nctx.format = LAPD_FORM_I;
626 nctx.p_f = 1;
627 nctx.n_send = vs;
628 nctx.n_recv = dl->v_recv;
629 nctx.length = length;
630 nctx.more = dl->tx_hist[h].more;
631 msg = lapd_msgb_alloc(length, "LAPD I resend");
632 msg->l3h = msgb_put(msg, length);
633 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
634 length);
635 dl->send_ph_data_req(&nctx, msg);
636 } else {
637 /* OR send appropriate supervision frame with P=1 */
638 if (!dl->own_busy && !dl->seq_err_cond) {
639 lapd_send_rr(&dl->lctx, 1, 1);
640 /* NOTE: In case of sequence error
641 * condition, the REJ frame has been
642 * transmitted when entering the
643 * condition, so it has not be done
644 * here
645 */
646 } else if (dl->own_busy) {
647 lapd_send_rnr(&dl->lctx, 1, 1);
648 } else {
649 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
650 "pls. fix\n");
651 }
652 }
653 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200654 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200655 } else {
656 /* send MDL ERROR INIDCATION to L3 */
657 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
658 /* reestablish */
659 if (!dl->reestablish)
660 break;
661 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
662 "reestablishment.\n");
663 lapd_reestablish(dl);
664 }
665 break;
666 default:
667 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
668 "dl->state %d\n", (int) dl->state);
669 }
670}
671
672/* Timer callback on T203 expiry */
673static void lapd_t203_cb(void *data)
674{
675 struct lapd_datalink *dl = data;
676
Andreas Eversberg742fc792011-09-27 09:40:25 +0200677 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200678 (int) dl->state);
679
680 if (dl->state != LAPD_STATE_MF_EST) {
681 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
682 "please fix!\n");
683 return;
684 }
685
686 /* set retransmission counter to 0 */
687 dl->retrans_ctr = 0;
688 /* enter timer recovery state */
689 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
690 /* transmit a supervisory command with P bit set to 1 as follows: */
691 if (!dl->own_busy) {
692 LOGP(DLLAPD, LOGL_INFO, "transmit an RR poll command\n");
693 /* Send RR with P=1 */
694 lapd_send_rr(&dl->lctx, 1, 1);
695 } else {
696 LOGP(DLLAPD, LOGL_INFO, "transmit an RNR poll command\n");
697 /* Send RNR with P=1 */
698 lapd_send_rnr(&dl->lctx, 1, 1);
699 }
700 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200701 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200702}
703
704/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
705static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
706{
707 struct lapd_datalink *dl = lctx->dl;
708 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100709 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200710 int i, h;
711
712 /* supervisory frame ? */
713 if (lctx->format == LAPD_FORM_S)
714 s = 1;
715 /* REJ frame ? */
716 if (s && lctx->s_u == LAPD_S_REJ)
717 rej = 1;
718
719 /* Flush all transmit buffers of acknowledged frames */
720 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
721 h = do_mod(i, dl->range_hist);
722 if (dl->tx_hist[h].msg) {
723 msgb_free(dl->tx_hist[h].msg);
724 dl->tx_hist[h].msg = NULL;
725 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
726 }
727 }
728
729 if (dl->state != LAPD_STATE_TIMER_RECOV) {
730 /* When not in the timer recovery condition, the data
731 * link layer entity shall reset the timer T200 on
732 * receipt of a valid I frame with N(R) higher than V(A),
733 * or an REJ with an N(R) equal to V(A). */
734 if ((!rej && nr != dl->v_ack)
735 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200736 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200737 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200738 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
739 }
740 /* 5.7.4: N(R) sequence error
741 * N(R) is called valid, if and only if
742 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
743 */
744 if (sub_mod(nr, dl->v_ack, dl->v_range)
745 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
746 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error\n");
747 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
748 }
749 }
750
751 /* V(A) shall be set to the value of N(R) */
752 dl->v_ack = nr;
753
Andreas Eversberg742fc792011-09-27 09:40:25 +0200754 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200755 * and if there are outstanding I frames, restart T200 */
756 if (t200_reset && !rej) {
757 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
758 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
759 "frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200760 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200761 }
762 }
763
764 /* This also does a restart, when I or S frame is received */
765
766 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200767 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200768 /* Start T203, if T200 is not running in MF EST state, if enabled */
769 if (!osmo_timer_pending(&dl->t200)
770 && (dl->t203_sec || dl->t203_usec)
771 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200772 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200773 }
774}
775
776/* L1 -> L2 */
777
778/* Receive a LAPD U (Unnumbered) message from L1 */
779static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
780{
781 struct lapd_datalink *dl = lctx->dl;
782 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100783 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200784 uint8_t prim, op;
785
786 switch (lctx->s_u) {
787 case LAPD_U_SABM:
788 case LAPD_U_SABME:
789 prim = PRIM_DL_EST;
790 op = PRIM_OP_INDICATION;
791
792 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s\n",
793 lapd_state_names[dl->state]);
794 /* 5.7.1 */
795 dl->seq_err_cond = 0;
796 /* G.2.2 Wrong value of the C/R bit */
797 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100798 LOGP(DLLAPD, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200799 msgb_free(msg);
800 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
801 return -EINVAL;
802 }
803
804 /* G.4.5 If SABM is received with L>N201 or with M bit
805 * set, AN MDL-ERROR-INDICATION is sent to MM.
806 */
807 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100808 LOGP(DLLAPD, LOGL_ERROR, "SABM too large error\n");
rootaf48bed2011-09-26 11:23:06 +0200809 msgb_free(msg);
810 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
811 return -EIO;
812 }
813
814 switch (dl->state) {
815 case LAPD_STATE_IDLE:
816 break;
817 case LAPD_STATE_MF_EST:
818 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
819 "frame established state\n");
820 /* If link is lost on the remote side, we start over
821 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100822 /* Additionally, continue in case of content resoltion
823 * (GSM network). This happens, if the mobile has not
824 * yet received UA or another mobile (collision) tries
825 * to establish connection. The mobile must receive
826 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200827 /* 5.4.2.1 */
828 if (!length) {
829 /* If no content resolution, this is a
830 * re-establishment. */
831 LOGP(DLLAPD, LOGL_INFO,
832 "Remote reestablish\n");
rootaf48bed2011-09-26 11:23:06 +0200833 break;
834 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200835 if (!dl->cont_res) {
836 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
Max87218ed2017-01-09 14:24:03 +0100837 "allowed in state %s\n",
838 lapd_state_names[dl->state]);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200839 mdl_error(MDL_CAUSE_SABM_MF, lctx);
840 msgb_free(msg);
841 return 0;
842 }
rootaf48bed2011-09-26 11:23:06 +0200843 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200844 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200845#ifdef TEST_CONTENT_RESOLUTION_NETWORK
846 dl->cont_res->data[0] ^= 0x01;
847#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100848 if (memcmp(dl->cont_res->data, msg->data,
849 length)) {
rootaf48bed2011-09-26 11:23:06 +0200850 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
851 "with diffrent content - "
852 "ignoring!\n");
853 msgb_free(msg);
854 return 0;
855 }
856 }
857 /* send UA again */
858 lapd_send_ua(lctx, length, msg->l3h);
859 msgb_free(msg);
860 return 0;
861 case LAPD_STATE_DISC_SENT:
862 /* 5.4.6.2 send DM with F=P */
863 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200864 /* stop Timer T200 */
865 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200866 msgb_free(msg);
867 return send_dl_simple(prim, op, lctx);
868 default:
869 /* collision: Send UA, but still wait for rx UA, then
870 * change to MF_EST state.
871 */
872 /* check for contention resoultion */
873 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
874 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Max87218ed2017-01-09 14:24:03 +0100875 "during contention resolution (state %s)\n",
876 lapd_state_names[dl->state]);
rootaf48bed2011-09-26 11:23:06 +0200877 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
878 }
879 lapd_send_ua(lctx, length, msg->l3h);
880 msgb_free(msg);
881 return 0;
882 }
883 /* save message context for further use */
884 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
885#ifndef TEST_CONTENT_RESOLUTION_NETWORK
886 /* send UA response */
887 lapd_send_ua(lctx, length, msg->l3h);
888#endif
889 /* set Vs, Vr and Va to 0 */
890 dl->v_send = dl->v_recv = dl->v_ack = 0;
891 /* clear tx_hist */
892 lapd_dl_flush_hist(dl);
893 /* enter multiple-frame-established state */
894 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
895 /* store content resolution data on network side
896 * Note: cont_res will be removed when changing state again,
897 * so it must be allocated AFTER lapd_dl_newstate(). */
898 if (dl->mode == LAPD_MODE_NETWORK && length) {
899 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
900 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
901 length);
902 LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n");
903 }
904 /* send notification to L3 */
905 if (length == 0) {
906 /* 5.4.1.2 Normal establishment procedures */
907 rc = send_dl_simple(prim, op, lctx);
908 msgb_free(msg);
909 } else {
910 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200911 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200912 rc = send_dl_l3(prim, op, lctx, msg);
913 }
914 break;
915 case LAPD_U_DM:
916 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
917 lapd_state_names[dl->state]);
918 /* G.2.2 Wrong value of the C/R bit */
919 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100920 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200921 msgb_free(msg);
922 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
923 return -EINVAL;
924 }
925 if (!lctx->p_f) {
926 /* 5.4.1.2 DM responses with the F bit set to "0"
927 * shall be ignored.
928 */
929 msgb_free(msg);
930 return 0;
931 }
932 switch (dl->state) {
933 case LAPD_STATE_SABM_SENT:
934 break;
935 case LAPD_STATE_MF_EST:
936 if (lctx->p_f) {
937 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
938 "response\n");
939 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
940 } else {
941 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
942 "response, multiple frame established "
943 "state\n");
944 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
945 /* reestablish */
946 if (!dl->reestablish) {
947 msgb_free(msg);
948 return 0;
949 }
950 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
951 "reestablishment.\n");
952 lapd_reestablish(dl);
953 }
954 msgb_free(msg);
955 return 0;
956 case LAPD_STATE_TIMER_RECOV:
957 /* FP = 0 (DM is normal in case PF = 1) */
958 if (!lctx->p_f) {
959 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
960 "response, multiple frame established "
961 "state\n");
962 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
963 msgb_free(msg);
964 /* reestablish */
965 if (!dl->reestablish)
966 return 0;
967 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
968 "reestablishment.\n");
969 return lapd_reestablish(dl);
970 }
971 break;
972 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200973 /* stop Timer T200 */
974 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200975 /* go to idle state */
976 lapd_dl_flush_tx(dl);
977 lapd_dl_flush_send(dl);
978 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
979 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
980 msgb_free(msg);
981 return 0;
982 case LAPD_STATE_IDLE:
983 /* 5.4.5 all other frame types shall be discarded */
984 default:
985 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
986 "(discarding)\n");
987 msgb_free(msg);
988 return 0;
989 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200990 /* stop timer T200 */
991 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200992 /* go to idle state */
993 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
994 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
995 msgb_free(msg);
996 break;
997 case LAPD_U_UI:
998 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
999 /* G.2.2 Wrong value of the C/R bit */
1000 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001001 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +02001002 "error\n");
1003 msgb_free(msg);
1004 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1005 return -EINVAL;
1006 }
1007
1008 /* G.4.5 If UI is received with L>N201 or with M bit
1009 * set, AN MDL-ERROR-INDICATION is sent to MM.
1010 */
1011 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001012 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +02001013 "(%d > N201(%d) or M=%d)\n", length,
1014 lctx->n201, lctx->more);
1015 msgb_free(msg);
1016 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1017 return -EIO;
1018 }
1019
1020 /* do some length checks */
1021 if (length == 0) {
1022 /* 5.3.3 UI frames received with the length indicator
1023 * set to "0" shall be ignored
1024 */
1025 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1026 msgb_free(msg);
1027 return 0;
1028 }
Harald Welte087116a2013-06-18 21:41:34 +02001029 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001030 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1031 msg);
1032 break;
1033 case LAPD_U_DISC:
1034 prim = PRIM_DL_REL;
1035 op = PRIM_OP_INDICATION;
1036
1037 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1038 lapd_state_names[dl->state]);
1039 /* flush tx and send buffers */
1040 lapd_dl_flush_tx(dl);
1041 lapd_dl_flush_send(dl);
1042 /* 5.7.1 */
1043 dl->seq_err_cond = 0;
1044 /* G.2.2 Wrong value of the C/R bit */
1045 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001046 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001047 msgb_free(msg);
1048 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1049 return -EINVAL;
1050 }
1051 if (length > 0 || lctx->more) {
1052 /* G.4.4 If a DISC or DM frame is received with L>0 or
1053 * with the M bit set to "1", an MDL-ERROR-INDICATION
1054 * primitive with cause "U frame with incorrect
1055 * parameters" is sent to the mobile management entity.
1056 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001057 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001058 "U frame iwth incorrect parameters ");
1059 msgb_free(msg);
1060 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1061 return -EIO;
1062 }
1063 switch (dl->state) {
1064 case LAPD_STATE_IDLE:
1065 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1066 /* send DM with F=P */
1067 msgb_free(msg);
1068 return lapd_send_dm(lctx);
1069 case LAPD_STATE_SABM_SENT:
1070 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1071 /* 5.4.6.2 send DM with F=P */
1072 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001073 /* stop Timer T200 */
1074 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001075 /* go to idle state */
1076 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1077 msgb_free(msg);
1078 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1079 lctx);
1080 case LAPD_STATE_MF_EST:
1081 case LAPD_STATE_TIMER_RECOV:
1082 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1083 break;
1084 case LAPD_STATE_DISC_SENT:
1085 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1086 prim = PRIM_DL_REL;
1087 op = PRIM_OP_CONFIRM;
1088 break;
1089 default:
1090 lapd_send_ua(lctx, length, msg->l3h);
1091 msgb_free(msg);
1092 return 0;
1093 }
1094 /* send UA response */
1095 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001096 /* stop Timer T200 */
1097 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001098 /* enter idle state, keep tx-buffer with UA response */
1099 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1100 /* send notification to L3 */
1101 rc = send_dl_simple(prim, op, lctx);
1102 msgb_free(msg);
1103 break;
1104 case LAPD_U_UA:
1105 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1106 lapd_state_names[dl->state]);
1107 /* G.2.2 Wrong value of the C/R bit */
1108 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001109 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001110 "error\n");
1111 msgb_free(msg);
1112 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1113 return -EINVAL;
1114 }
1115
1116 /* G.4.5 If UA is received with L>N201 or with M bit
1117 * set, AN MDL-ERROR-INDICATION is sent to MM.
1118 */
1119 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001120 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001121 msgb_free(msg);
1122 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1123 return -EIO;
1124 }
1125
1126 if (!lctx->p_f) {
1127 /* 5.4.1.2 A UA response with the F bit set to "0"
1128 * shall be ignored.
1129 */
1130 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1131 msgb_free(msg);
1132 return 0;
1133 }
1134 switch (dl->state) {
1135 case LAPD_STATE_SABM_SENT:
1136 break;
1137 case LAPD_STATE_MF_EST:
1138 case LAPD_STATE_TIMER_RECOV:
1139 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1140 "(discarding)\n");
1141 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1142 msgb_free(msg);
1143 return 0;
1144 case LAPD_STATE_DISC_SENT:
1145 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001146 /* stop Timer T200 */
1147 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001148 /* go to idle state */
1149 lapd_dl_flush_tx(dl);
1150 lapd_dl_flush_send(dl);
1151 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1152 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1153 msgb_free(msg);
1154 return 0;
1155 case LAPD_STATE_IDLE:
1156 /* 5.4.5 all other frame types shall be discarded */
1157 default:
1158 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1159 "(discarding)\n");
1160 msgb_free(msg);
1161 return 0;
1162 }
1163 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001164 /* stop Timer T200 */
1165 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001166 /* compare UA with SABME if contention resolution is applied */
1167 if (dl->tx_hist[0].msg->len) {
1168 if (length != (dl->tx_hist[0].msg->len)
1169 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1170 length)) {
1171 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1172 "mismatches ****\n");
1173 rc = send_dl_simple(PRIM_DL_REL,
1174 PRIM_OP_INDICATION, lctx);
1175 msgb_free(msg);
1176 /* go to idle state */
1177 lapd_dl_flush_tx(dl);
1178 lapd_dl_flush_send(dl);
1179 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1180 return 0;
1181 }
1182 }
1183 /* set Vs, Vr and Va to 0 */
1184 dl->v_send = dl->v_recv = dl->v_ack = 0;
1185 /* clear tx_hist */
1186 lapd_dl_flush_hist(dl);
1187 /* enter multiple-frame-established state */
1188 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1189 /* send outstanding frames, if any (resume / reconnect) */
1190 lapd_send_i(lctx, __LINE__);
1191 /* send notification to L3 */
1192 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1193 msgb_free(msg);
1194 break;
1195 case LAPD_U_FRMR:
1196 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1197 /* send MDL ERROR INIDCATION to L3 */
1198 mdl_error(MDL_CAUSE_FRMR, lctx);
1199 msgb_free(msg);
1200 /* reestablish */
1201 if (!dl->reestablish)
1202 break;
1203 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1204 rc = lapd_reestablish(dl);
1205 break;
1206 default:
1207 /* G.3.1 */
1208 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1209 msgb_free(msg);
1210 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1211 return -EINVAL;
1212 }
1213 return rc;
1214}
1215
1216/* Receive a LAPD S (Supervisory) message from L1 */
1217static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1218{
1219 struct lapd_datalink *dl = lctx->dl;
1220 int length = lctx->length;
1221
1222 if (length > 0 || lctx->more) {
1223 /* G.4.3 If a supervisory frame is received with L>0 or
1224 * with the M bit set to "1", an MDL-ERROR-INDICATION
1225 * primitive with cause "S frame with incorrect
1226 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001227 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001228 "S frame with incorrect parameters\n");
1229 msgb_free(msg);
1230 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1231 return -EIO;
1232 }
1233
1234 if (lctx->cr == dl->cr.rem2loc.resp
1235 && lctx->p_f
1236 && dl->state != LAPD_STATE_TIMER_RECOV) {
1237 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1238 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1239 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1240 }
1241
1242 switch (dl->state) {
1243 case LAPD_STATE_IDLE:
1244 /* if P=1, respond DM with F=1 (5.2.2) */
1245 /* 5.4.5 all other frame types shall be discarded */
1246 if (lctx->p_f)
1247 lapd_send_dm(lctx); /* F=P */
1248 /* fall though */
1249 case LAPD_STATE_SABM_SENT:
1250 case LAPD_STATE_DISC_SENT:
1251 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1252 msgb_free(msg);
1253 return 0;
1254 }
1255 switch (lctx->s_u) {
1256 case LAPD_S_RR:
1257 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1258 lapd_state_names[dl->state]);
1259 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1260 lapd_acknowledge(lctx);
1261
1262 /* 5.5.3.2 */
1263 if (lctx->cr == dl->cr.rem2loc.cmd
1264 && lctx->p_f) {
1265 if (!dl->own_busy && !dl->seq_err_cond) {
1266 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1267 "with polling bit set and we are not "
1268 "busy, so we reply with RR frame "
1269 "response\n");
1270 lapd_send_rr(lctx, 1, 0);
1271 /* NOTE: In case of sequence error condition,
1272 * the REJ frame has been transmitted when
1273 * entering the condition, so it has not be
1274 * done here
1275 */
1276 } else if (dl->own_busy) {
1277 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1278 "with polling bit set and we are busy, "
1279 "so we reply with RR frame response\n");
1280 lapd_send_rnr(lctx, 1, 0);
1281 }
1282 } else if (lctx->cr == dl->cr.rem2loc.resp
1283 && lctx->p_f
1284 && dl->state == LAPD_STATE_TIMER_RECOV) {
1285 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1286 "and we are in timer recovery state, so "
1287 "we leave that state\n");
1288 /* V(S) to the N(R) in the RR frame */
1289 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001290 /* stop Timer T200 */
1291 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001292 /* 5.5.7 Clear timer recovery condition */
1293 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1294 }
1295 /* Send message, if possible due to acknowledged data */
1296 lapd_send_i(lctx, __LINE__);
1297
1298 break;
1299 case LAPD_S_RNR:
1300 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1301 lapd_state_names[dl->state]);
1302 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1303 lapd_acknowledge(lctx);
1304
1305 /* 5.5.5 */
1306 /* Set peer receiver busy condition */
1307 dl->peer_busy = 1;
1308
1309 if (lctx->p_f) {
1310 if (lctx->cr == dl->cr.rem2loc.cmd) {
1311 if (!dl->own_busy) {
1312 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1313 "command and we are not busy, "
1314 "so we reply with RR final "
1315 "response\n");
1316 /* Send RR with F=1 */
1317 lapd_send_rr(lctx, 1, 0);
1318 } else {
1319 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1320 "command and we are busy, so "
1321 "we reply with RNR final "
1322 "response\n");
1323 /* Send RNR with F=1 */
1324 lapd_send_rnr(lctx, 1, 0);
1325 }
1326 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1327 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1328 "and we in timer recovery state, so "
1329 "we leave that state\n");
1330 /* 5.5.7 Clear timer recovery condition */
1331 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1332 /* V(S) to the N(R) in the RNR frame */
1333 dl->v_send = lctx->n_recv;
1334 }
1335 } else
1336 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1337 "received\n");
1338
1339 /* Send message, if possible due to acknowledged data */
1340 lapd_send_i(lctx, __LINE__);
1341
1342 break;
1343 case LAPD_S_REJ:
1344 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1345 lapd_state_names[dl->state]);
1346 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1347 lapd_acknowledge(lctx);
1348
1349 /* 5.5.4.1 */
1350 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1351 /* Clear an existing peer receiver busy condition */
1352 dl->peer_busy = 0;
1353 /* V(S) and V(A) to the N(R) in the REJ frame */
1354 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001355 /* stop Timer T200 */
1356 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001357 /* 5.5.3.2 */
1358 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1359 if (!dl->own_busy && !dl->seq_err_cond) {
1360 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1361 "command not in timer recovery "
1362 "state and not in own busy "
1363 "condition received, so we "
1364 "respond with RR final "
1365 "response\n");
1366 lapd_send_rr(lctx, 1, 0);
1367 /* NOTE: In case of sequence error
1368 * condition, the REJ frame has been
1369 * transmitted when entering the
1370 * condition, so it has not be done
1371 * here
1372 */
1373 } else if (dl->own_busy) {
1374 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1375 "command not in timer recovery "
1376 "state and in own busy "
1377 "condition received, so we "
1378 "respond with RNR final "
1379 "response\n");
1380 lapd_send_rnr(lctx, 1, 0);
1381 }
1382 } else
1383 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1384 "polling command not in timer recovery "
1385 "state received\n");
1386 /* send MDL ERROR INIDCATION to L3 */
1387 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1388 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1389 }
1390
1391 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1392 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1393 "recovery state received\n");
1394 /* Clear an existing peer receiver busy condition */
1395 dl->peer_busy = 0;
1396 /* V(S) and V(A) to the N(R) in the REJ frame */
1397 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001398 /* stop Timer T200 */
1399 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001400 /* 5.5.7 Clear timer recovery condition */
1401 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1402 } else {
1403 /* Clear an existing peer receiver busy condition */
1404 dl->peer_busy = 0;
1405 /* V(S) and V(A) to the N(R) in the REJ frame */
1406 dl->v_send = dl->v_ack = lctx->n_recv;
1407 /* 5.5.3.2 */
1408 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1409 if (!dl->own_busy && !dl->seq_err_cond) {
1410 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1411 "command in timer recovery "
1412 "state and not in own busy "
1413 "condition received, so we "
1414 "respond with RR final "
1415 "response\n");
1416 lapd_send_rr(lctx, 1, 0);
1417 /* NOTE: In case of sequence error
1418 * condition, the REJ frame has been
1419 * transmitted when entering the
1420 * condition, so it has not be done
1421 * here
1422 */
1423 } else if (dl->own_busy) {
1424 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1425 "command in timer recovery "
1426 "state and in own busy "
1427 "condition received, so we "
1428 "respond with RNR final "
1429 "response\n");
1430 lapd_send_rnr(lctx, 1, 0);
1431 }
1432 } else
1433 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1434 "polling command in timer recovery "
1435 "state received\n");
1436 }
1437
1438 /* FIXME: 5.5.4.2 2) */
1439
1440 /* Send message, if possible due to acknowledged data */
1441 lapd_send_i(lctx, __LINE__);
1442
1443 break;
1444 default:
1445 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001446 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001447 msgb_free(msg);
1448 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1449 return -EINVAL;
1450 }
1451 msgb_free(msg);
1452 return 0;
1453}
1454
1455/* Receive a LAPD I (Information) message from L1 */
1456static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1457{
1458 struct lapd_datalink *dl = lctx->dl;
1459 //uint8_t nr = lctx->n_recv;
1460 uint8_t ns = lctx->n_send;
1461 int length = lctx->length;
1462 int rc;
1463
Holger Hans Peter Freyther1512ea62014-03-16 23:59:58 +01001464 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1465 lapd_state_names[dl->state], lctx->sapi);
rootaf48bed2011-09-26 11:23:06 +02001466
1467 /* G.2.2 Wrong value of the C/R bit */
1468 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001469 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001470 msgb_free(msg);
1471 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1472 return -EINVAL;
1473 }
1474
1475 if (length == 0 || length > lctx->n201) {
1476 /* G.4.2 If the length indicator of an I frame is set
1477 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1478 * primitive with cause "I frame with incorrect length"
1479 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001480 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001481 msgb_free(msg);
1482 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1483 return -EIO;
1484 }
1485
1486 /* G.4.2 If the numerical value of L is L<N201 and the M
1487 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1488 * cause "I frame with incorrect use of M bit" is sent to the
1489 * mobile management entity. */
1490 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001491 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001492 msgb_free(msg);
1493 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1494 return -EIO;
1495 }
1496
1497 switch (dl->state) {
1498 case LAPD_STATE_IDLE:
1499 /* if P=1, respond DM with F=1 (5.2.2) */
1500 /* 5.4.5 all other frame types shall be discarded */
1501 if (lctx->p_f)
1502 lapd_send_dm(lctx); /* F=P */
1503 /* fall though */
1504 case LAPD_STATE_SABM_SENT:
1505 case LAPD_STATE_DISC_SENT:
1506 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1507 msgb_free(msg);
1508 return 0;
1509 }
1510
1511 /* 5.7.1: N(s) sequence error */
1512 if (ns != dl->v_recv) {
1513 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1514 "V(R)=%u\n", ns, dl->v_recv);
1515 /* discard data */
1516 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001517 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001518 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001519 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001520 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001521 lapd_send_rej(lctx, lctx->p_f);
1522 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001523 /* If there are two subsequent sequence errors received,
1524 * ignore it. (Ignore every second subsequent error.)
1525 * This happens if our reply with the REJ is too slow,
1526 * so the remote gets a T200 timeout and sends another
1527 * frame with a sequence error.
1528 * Test showed that replying with two subsequent REJ
1529 * messages could the remote L2 process to abort.
1530 * Replying too slow shouldn't happen, but may happen
1531 * over serial link between BB and LAPD.
1532 */
1533 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001534 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001535 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1536 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1537 lapd_acknowledge(lctx); /* V(A) is also set here */
1538
1539 /* Send message, if possible due to acknowledged data */
1540 lapd_send_i(lctx, __LINE__);
1541
1542 return 0;
rootaf48bed2011-09-26 11:23:06 +02001543 }
1544 dl->seq_err_cond = 0;
1545
1546 /* Increment receiver state */
1547 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1548 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1549
1550 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1551 lapd_acknowledge(lctx); /* V(A) is also set here */
1552
1553 /* Only if we are not in own receiver busy condition */
1554 if (!dl->own_busy) {
1555 /* if the frame carries a complete segment */
1556 if (!lctx->more && !dl->rcv_buffer) {
1557 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1558 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001559 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001560 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1561 msg);
1562 } else {
1563 /* create rcv_buffer */
1564 if (!dl->rcv_buffer) {
1565 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1566 "I frames (first message)\n");
1567 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1568 "LAPD RX");
1569 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1570 }
1571 /* concat. rcv_buffer */
1572 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1573 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1574 "overflow!\n");
1575 } else {
1576 memcpy(msgb_put(dl->rcv_buffer, length),
1577 msg->l3h, length);
1578 }
1579 /* if the last segment was received */
1580 if (!lctx->more) {
1581 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1582 "I frames (last message)\n");
1583 rc = send_dl_l3(PRIM_DL_DATA,
1584 PRIM_OP_INDICATION, lctx,
1585 dl->rcv_buffer);
1586 dl->rcv_buffer = NULL;
1587 } else
1588 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1589 "I frames (next message)\n");
1590 msgb_free(msg);
1591
1592 }
1593 } else
1594 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1595 "busy condition\n");
1596
1597 /* Check for P bit */
1598 if (lctx->p_f) {
1599 /* 5.5.2.1 */
1600 /* check if we are not in own receiver busy */
1601 if (!dl->own_busy) {
1602 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1603 /* Send RR with F=1 */
1604 rc = lapd_send_rr(lctx, 1, 0);
1605 } else {
1606 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1607 /* Send RNR with F=1 */
1608 rc = lapd_send_rnr(lctx, 1, 0);
1609 }
1610 } else {
1611 /* 5.5.2.2 */
1612 /* check if we are not in own receiver busy */
1613 if (!dl->own_busy) {
1614 /* NOTE: V(R) is already set above */
1615 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001616
1617 /* if update_pending_iframe returns 0 it updated
1618 * the lapd header of an iframe in the tx queue */
1619 if (rc && dl->update_pending_frames)
1620 rc = dl->update_pending_frames(lctx);
1621
rootaf48bed2011-09-26 11:23:06 +02001622 if (rc) {
1623 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1624 "have no pending data, send RR\n");
1625 /* Send RR with F=0 */
1626 return lapd_send_rr(lctx, 0, 0);
1627 }
1628 /* all I or one RR is sent, we are done */
1629 return 0;
1630 } else {
1631 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1632 /* Send RNR with F=0 */
1633 rc = lapd_send_rnr(lctx, 0, 0);
1634 }
1635 }
1636
1637 /* Send message, if possible due to acknowledged data */
1638 lapd_send_i(lctx, __LINE__);
1639
1640 return rc;
1641}
1642
1643/* Receive a LAPD message from L1 */
1644int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1645{
1646 int rc;
1647
1648 switch (lctx->format) {
1649 case LAPD_FORM_U:
1650 rc = lapd_rx_u(msg, lctx);
1651 break;
1652 case LAPD_FORM_S:
1653 rc = lapd_rx_s(msg, lctx);
1654 break;
1655 case LAPD_FORM_I:
1656 rc = lapd_rx_i(msg, lctx);
1657 break;
1658 default:
1659 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1660 msgb_free(msg);
1661 rc = -EINVAL;
1662 }
1663 return rc;
1664}
1665
1666/* L3 -> L2 */
1667
1668/* send unit data */
1669static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1670{
1671 struct lapd_datalink *dl = lctx->dl;
1672 struct msgb *msg = dp->oph.msg;
1673 struct lapd_msg_ctx nctx;
1674
1675 memcpy(&nctx, lctx, sizeof(nctx));
1676 /* keep nctx.ldp */
1677 /* keep nctx.sapi */
1678 /* keep nctx.tei */
1679 nctx.cr = dl->cr.loc2rem.cmd;
1680 nctx.format = LAPD_FORM_U;
1681 nctx.s_u = LAPD_U_UI;
1682 /* keep nctx.p_f */
1683 nctx.length = msg->len;
1684 nctx.more = 0;
1685
1686 return dl->send_ph_data_req(&nctx, msg);
1687}
1688
1689/* request link establishment */
1690static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1691{
1692 struct lapd_datalink *dl = lctx->dl;
1693 struct msgb *msg = dp->oph.msg;
1694 struct lapd_msg_ctx nctx;
1695
1696 if (msg->len)
1697 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1698 "(SABM)\n");
1699 else
1700 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1701
1702 /* Flush send-queue */
1703 /* Clear send-buffer */
1704 lapd_dl_flush_send(dl);
1705 /* be sure that history is empty */
1706 lapd_dl_flush_hist(dl);
1707
1708 /* save message context for further use */
1709 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1710
1711 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001712 msgb_free(dl->rcv_buffer);
1713 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001714
1715 /* assemble message */
1716 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1717 /* keep nctx.ldp */
1718 /* keep nctx.sapi */
1719 /* keep nctx.tei */
1720 nctx.cr = dl->cr.loc2rem.cmd;
1721 nctx.format = LAPD_FORM_U;
1722 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1723 nctx.p_f = 1;
1724 nctx.length = msg->len;
1725 nctx.more = 0;
1726
1727 /* Transmit-buffer carries exactly one segment */
1728 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1729 msgb_put(dl->tx_hist[0].msg, msg->len);
1730 if (msg->len)
1731 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1732 dl->tx_hist[0].more = 0;
1733 /* set Vs to 0, because it is used as index when resending SABM */
1734 dl->v_send = 0;
1735
1736 /* Set states */
1737 dl->own_busy = dl->peer_busy = 0;
1738 dl->retrans_ctr = 0;
1739 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1740
1741 /* Tramsmit and start T200 */
1742 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001743 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001744
1745 return 0;
1746}
1747
1748/* send data */
1749static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1750{
1751 struct lapd_datalink *dl = lctx->dl;
1752 struct msgb *msg = dp->oph.msg;
1753
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001754 if (msgb_l3len(msg) == 0) {
1755 LOGP(DLLAPD, LOGL_ERROR,
1756 "writing an empty message is not possible.\n");
1757 msgb_free(msg);
1758 return -1;
1759 }
1760
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001761 LOGP(DLLAPD, LOGL_INFO,
1762 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001763
1764 /* Write data into the send queue */
1765 msgb_enqueue(&dl->send_queue, msg);
1766
1767 /* Send message, if possible */
1768 lapd_send_i(&dl->lctx, __LINE__);
1769
1770 return 0;
1771}
1772
1773/* Send next I frame from queued/buffered data */
1774static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1775{
1776 struct lapd_datalink *dl = lctx->dl;
1777 uint8_t k = dl->k;
1778 uint8_t h;
1779 struct msgb *msg;
1780 int length, left;
1781 int rc = - 1; /* we sent nothing */
1782 struct lapd_msg_ctx nctx;
1783
1784
1785 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1786
1787 next_frame:
1788
1789 if (dl->peer_busy) {
1790 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1791 return rc;
1792 }
1793
1794 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1795 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1796 return rc;
1797 }
1798
1799 /* If the send state variable V(S) is equal to V(A) plus k
1800 * (where k is the maximum number of outstanding I frames - see
1801 * subclause 5.8.4), the data link layer entity shall not transmit any
1802 * new I frames, but shall retransmit an I frame as a result
1803 * of the error recovery procedures as described in subclauses 5.5.4 and
1804 * 5.5.7. */
1805 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1806 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1807 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1808 dl->v_ack);
1809 return rc;
1810 }
1811
1812 h = do_mod(dl->v_send, dl->range_hist);
1813
1814 /* if we have no tx_hist yet, we create it */
1815 if (!dl->tx_hist[h].msg) {
1816 /* Get next message into send-buffer, if any */
1817 if (!dl->send_buffer) {
1818 next_message:
1819 dl->send_out = 0;
1820 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1821 /* No more data to be sent */
1822 if (!dl->send_buffer)
1823 return rc;
1824 LOGP(DLLAPD, LOGL_INFO, "get message from "
1825 "send-queue\n");
1826 }
1827
1828 /* How much is left in the send-buffer? */
1829 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1830 /* Segment, if data exceeds N201 */
1831 length = left;
1832 if (length > lctx->n201)
1833 length = lctx->n201;
1834 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1835 "length %d first byte %02x\n",
1836 msgb_l3len(dl->send_buffer), dl->send_out, left,
1837 lctx->n201, length, dl->send_buffer->l3h[0]);
1838 /* If message in send-buffer is completely sent */
1839 if (left == 0) {
1840 msgb_free(dl->send_buffer);
1841 dl->send_buffer = NULL;
1842 goto next_message;
1843 }
1844
1845 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1846 (left > length) ? "segment " : "", dl->v_send);
1847
1848 /* Create I frame (segment) and transmit-buffer content */
1849 msg = lapd_msgb_alloc(length, "LAPD I");
1850 msg->l3h = msgb_put(msg, length);
1851 /* assemble message */
1852 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1853 /* keep nctx.ldp */
1854 /* keep nctx.sapi */
1855 /* keep nctx.tei */
1856 nctx.cr = dl->cr.loc2rem.cmd;
1857 nctx.format = LAPD_FORM_I;
1858 nctx.p_f = 0;
1859 nctx.n_send = dl->v_send;
1860 nctx.n_recv = dl->v_recv;
1861 nctx.length = length;
1862 if (left > length)
1863 nctx.more = 1;
1864 else
1865 nctx.more = 0;
1866 if (length)
1867 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1868 length);
1869 /* store in tx_hist */
1870 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1871 msgb_put(dl->tx_hist[h].msg, msg->len);
1872 if (length)
1873 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1874 dl->tx_hist[h].more = nctx.more;
1875 /* Add length to track how much is already in the tx buffer */
1876 dl->send_out += length;
1877 } else {
1878 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1879 "V(S)=%d\n", dl->v_send);
1880
1881 /* Create I frame (segment) from tx_hist */
1882 length = dl->tx_hist[h].msg->len;
1883 msg = lapd_msgb_alloc(length, "LAPD I resend");
1884 msg->l3h = msgb_put(msg, length);
1885 /* assemble message */
1886 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1887 /* keep nctx.ldp */
1888 /* keep nctx.sapi */
1889 /* keep nctx.tei */
1890 nctx.cr = dl->cr.loc2rem.cmd;
1891 nctx.format = LAPD_FORM_I;
1892 nctx.p_f = 0;
1893 nctx.n_send = dl->v_send;
1894 nctx.n_recv = dl->v_recv;
1895 nctx.length = length;
1896 nctx.more = dl->tx_hist[h].more;
1897 if (length)
1898 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1899 }
1900
1901 /* The value of the send state variable V(S) shall be incremented by 1
1902 * at the end of the transmission of the I frame */
1903 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1904
1905 /* If timer T200 is not running at the time right before transmitting a
1906 * frame, when the PH-READY-TO-SEND primitive is received from the
1907 * physical layer., it shall be set. */
1908 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001909 /* stop Timer T203, if running */
1910 lapd_stop_t203(dl);
1911 /* start Timer T200 */
1912 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001913 }
1914
1915 dl->send_ph_data_req(&nctx, msg);
1916
1917 rc = 0; /* we sent something */
1918 goto next_frame;
1919}
1920
1921/* request link suspension */
1922static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1923{
1924 struct lapd_datalink *dl = lctx->dl;
1925 struct msgb *msg = dp->oph.msg;
1926
1927 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1928
1929 /* put back the send-buffer to the send-queue (first position) */
1930 if (dl->send_buffer) {
1931 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1932 "queue\n");
1933 llist_add(&dl->send_buffer->list, &dl->send_queue);
1934 dl->send_buffer = NULL;
1935 } else
1936 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1937
1938 /* Clear transmit buffer, but keep send buffer */
1939 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001940 /* Stop timers (there is no state change, so we must stop all timers */
1941 lapd_stop_t200(dl);
1942 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001943
1944 msgb_free(msg);
1945
1946 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1947}
1948
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001949/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001950static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1951{
1952 struct lapd_datalink *dl = lctx->dl;
1953 struct msgb *msg = dp->oph.msg;
1954 struct lapd_msg_ctx nctx;
1955
1956 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1957 msg->len);
1958
1959 /* be sure that history is empty */
1960 lapd_dl_flush_hist(dl);
1961
1962 /* save message context for further use */
1963 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1964
1965 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001966 msgb_free(dl->send_buffer);
1967 dl->send_buffer = NULL;
1968
rootaf48bed2011-09-26 11:23:06 +02001969 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001970 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02001971 /* Write data into the send buffer, to be sent first */
1972 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001973 } else {
1974 msgb_free(msg);
1975 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001976 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001977 }
rootaf48bed2011-09-26 11:23:06 +02001978
1979 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001980 msgb_free(dl->rcv_buffer);
1981 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001982
1983 /* Create new msgb (old one is now free) */
1984 msg = lapd_msgb_alloc(0, "LAPD SABM");
1985 msg->l3h = msg->data;
1986 /* assemble message */
1987 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1988 /* keep nctx.ldp */
1989 /* keep nctx.sapi */
1990 /* keep nctx.tei */
1991 nctx.cr = dl->cr.loc2rem.cmd;
1992 nctx.format = LAPD_FORM_U;
1993 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1994 nctx.p_f = 1;
1995 nctx.length = 0;
1996 nctx.more = 0;
1997
1998 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1999 msgb_put(dl->tx_hist[0].msg, msg->len);
2000 if (msg->len)
2001 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2002 dl->tx_hist[0].more = 0;
2003 /* set Vs to 0, because it is used as index when resending SABM */
2004 dl->v_send = 0;
2005
2006 /* Set states */
2007 dl->own_busy = dl->peer_busy = 0;
2008 dl->retrans_ctr = 0;
2009 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2010
2011 /* Tramsmit and start T200 */
2012 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002013 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002014
2015 return 0;
2016}
2017
2018/* requesst release of link */
2019static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2020{
2021 struct lapd_datalink *dl = lctx->dl;
2022 struct msgb *msg = dp->oph.msg;
2023 struct lapd_msg_ctx nctx;
2024
2025 /* local release */
2026 if (dp->u.rel_req.mode) {
2027 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2028 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002029 /* stop Timer T200 */
2030 lapd_stop_t200(dl);
2031 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002032 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2033 /* flush buffers */
2034 lapd_dl_flush_tx(dl);
2035 lapd_dl_flush_send(dl);
2036 /* send notification to L3 */
2037 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2038 }
2039
2040 /* in case we are already disconnecting */
2041 if (dl->state == LAPD_STATE_DISC_SENT)
2042 return -EBUSY;
2043
2044 /* flush tx_hist */
2045 lapd_dl_flush_hist(dl);
2046
2047 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2048
2049 /* Push LAPD header on msgb */
2050 /* assemble message */
2051 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2052 /* keep nctx.ldp */
2053 /* keep nctx.sapi */
2054 /* keep nctx.tei */
2055 nctx.cr = dl->cr.loc2rem.cmd;
2056 nctx.format = LAPD_FORM_U;
2057 nctx.s_u = LAPD_U_DISC;
2058 nctx.p_f = 1;
2059 nctx.length = 0;
2060 nctx.more = 0;
2061
2062 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2063 msgb_put(dl->tx_hist[0].msg, msg->len);
2064 if (msg->len)
2065 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2066 dl->tx_hist[0].more = 0;
2067 /* set Vs to 0, because it is used as index when resending DISC */
2068 dl->v_send = 0;
2069
2070 /* Set states */
2071 dl->own_busy = dl->peer_busy = 0;
2072 dl->retrans_ctr = 0;
2073 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2074
2075 /* Tramsmit and start T200 */
2076 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002077 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002078
2079 return 0;
2080}
2081
2082/* request release of link in idle state */
2083static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2084 struct lapd_msg_ctx *lctx)
2085{
2086 struct lapd_datalink *dl = lctx->dl;
2087 struct msgb *msg = dp->oph.msg;
2088
2089 msgb_free(msg);
2090
2091 /* send notification to L3 */
2092 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2093}
2094
2095/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002096static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002097 uint32_t states;
2098 int prim, op;
2099 const char *name;
2100 int (*rout) (struct osmo_dlsap_prim *dp,
2101 struct lapd_msg_ctx *lctx);
2102} l2downstatelist[] = {
2103 /* create and send UI command */
2104 {ALL_STATES,
2105 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2106 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2107
2108 /* create and send SABM command */
2109 {SBIT(LAPD_STATE_IDLE),
2110 PRIM_DL_EST, PRIM_OP_REQUEST,
2111 "DL-ESTABLISH-REQUEST", lapd_est_req},
2112
2113 /* create and send I command */
2114 {SBIT(LAPD_STATE_MF_EST) |
2115 SBIT(LAPD_STATE_TIMER_RECOV),
2116 PRIM_DL_DATA, PRIM_OP_REQUEST,
2117 "DL-DATA-REQUEST", lapd_data_req},
2118
2119 /* suspend datalink */
2120 {SBIT(LAPD_STATE_MF_EST) |
2121 SBIT(LAPD_STATE_TIMER_RECOV),
2122 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2123 "DL-SUSPEND-REQUEST", lapd_susp_req},
2124
2125 /* create and send SABM command (resume) */
2126 {SBIT(LAPD_STATE_MF_EST) |
2127 SBIT(LAPD_STATE_TIMER_RECOV),
2128 PRIM_DL_RES, PRIM_OP_REQUEST,
2129 "DL-RESUME-REQUEST", lapd_res_req},
2130
2131 /* create and send SABM command (reconnect) */
2132 {SBIT(LAPD_STATE_IDLE) |
2133 SBIT(LAPD_STATE_MF_EST) |
2134 SBIT(LAPD_STATE_TIMER_RECOV),
2135 PRIM_DL_RECON, PRIM_OP_REQUEST,
2136 "DL-RECONNECT-REQUEST", lapd_res_req},
2137
2138 /* create and send DISC command */
2139 {SBIT(LAPD_STATE_SABM_SENT) |
2140 SBIT(LAPD_STATE_MF_EST) |
2141 SBIT(LAPD_STATE_TIMER_RECOV) |
2142 SBIT(LAPD_STATE_DISC_SENT),
2143 PRIM_DL_REL, PRIM_OP_REQUEST,
2144 "DL-RELEASE-REQUEST", lapd_rel_req},
2145
2146 /* release in idle state */
2147 {SBIT(LAPD_STATE_IDLE),
2148 PRIM_DL_REL, PRIM_OP_REQUEST,
2149 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2150};
2151
2152#define L2DOWNSLLEN \
2153 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2154
2155int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2156{
2157 struct lapd_datalink *dl = lctx->dl;
2158 int i, supported = 0;
2159 struct msgb *msg = dp->oph.msg;
2160 int rc;
2161
2162 /* find function for current state and message */
2163 for (i = 0; i < L2DOWNSLLEN; i++) {
2164 if (dp->oph.primitive == l2downstatelist[i].prim
2165 && dp->oph.operation == l2downstatelist[i].op) {
2166 supported = 1;
2167 if ((SBIT(dl->state) & l2downstatelist[i].states))
2168 break;
2169 }
2170 }
2171 if (!supported) {
2172 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2173 dp->oph.primitive, dp->oph.operation);
2174 msgb_free(msg);
2175 return 0;
2176 }
2177 if (i == L2DOWNSLLEN) {
2178 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2179 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2180 lapd_state_names[dl->state]);
2181 msgb_free(msg);
2182 return 0;
2183 }
2184
2185 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2186 l2downstatelist[i].name, lapd_state_names[dl->state]);
2187
2188 rc = l2downstatelist[i].rout(dp, lctx);
2189
2190 return rc;
2191}
2192
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002193/*! @} */