blob: fd163c831c615797f1f99d0d806e77c298a1148e [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>
85
86/* TS 04.06 Table 4 / Section 3.8.1 */
87#define LAPD_U_SABM 0x7
88#define LAPD_U_SABME 0xf
89#define LAPD_U_DM 0x3
90#define LAPD_U_UI 0x0
91#define LAPD_U_DISC 0x8
92#define LAPD_U_UA 0xC
93#define LAPD_U_FRMR 0x11
94
95#define LAPD_S_RR 0x0
96#define LAPD_S_RNR 0x1
97#define LAPD_S_REJ 0x2
98
99#define CR_USER2NET_CMD 0
100#define CR_USER2NET_RESP 1
101#define CR_NET2USER_CMD 1
102#define CR_NET2USER_RESP 0
103
104#define LAPD_HEADROOM 56
105
106#define SBIT(a) (1 << a)
107#define ALL_STATES 0xffffffff
108
Andreas Eversberg742fc792011-09-27 09:40:25 +0200109static void lapd_t200_cb(void *data);
110static void lapd_t203_cb(void *data);
111static int lapd_send_i(struct lapd_msg_ctx *lctx, int line);
112static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx);
113
rootaf48bed2011-09-26 11:23:06 +0200114/* UTILITY FUNCTIONS */
115
116struct msgb *lapd_msgb_alloc(int length, const char *name)
117{
118 /* adding space for padding, FIXME: add as an option */
119 if (length < 21)
120 length = 21;
121 return msgb_alloc_headroom(length + LAPD_HEADROOM, LAPD_HEADROOM, name);
122}
123
124static inline uint8_t do_mod(uint8_t x, uint8_t m)
125{
126 return x & (m - 1);
127}
128
129static inline uint8_t inc_mod(uint8_t x, uint8_t m)
130{
131 return (x + 1) & (m - 1);
132}
133
134static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m)
135{
136 return (x + y) & (m - 1);
137}
138
139static inline uint8_t sub_mod(uint8_t x, uint8_t y, uint8_t m)
140{
141 return (x - y) & (m - 1); /* handle negative results correctly */
142}
143
144static void lapd_dl_flush_send(struct lapd_datalink *dl)
145{
146 struct msgb *msg;
147
148 /* Flush send-queue */
149 while ((msg = msgb_dequeue(&dl->send_queue)))
150 msgb_free(msg);
151
152 /* Clear send-buffer */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200153 msgb_free(dl->send_buffer);
154 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +0200155}
156
157static void lapd_dl_flush_hist(struct lapd_datalink *dl)
158{
159 unsigned int i;
160
Harald Weltef92e44c2016-08-01 00:24:19 +0200161 if (!dl->range_hist || !dl->tx_hist)
Harald Welte0ee90f82016-07-03 20:45:21 +0200162 return;
163
rootaf48bed2011-09-26 11:23:06 +0200164 for (i = 0; i < dl->range_hist; i++) {
165 if (dl->tx_hist[i].msg) {
166 msgb_free(dl->tx_hist[i].msg);
167 dl->tx_hist[i].msg = NULL;
168 }
169 }
170}
171
172static void lapd_dl_flush_tx(struct lapd_datalink *dl)
173{
174 struct msgb *msg;
175
176 while ((msg = msgb_dequeue(&dl->tx_queue)))
177 msgb_free(msg);
178 lapd_dl_flush_hist(dl);
179}
180
181/* Figure B.2/Q.921 */
182const char *lapd_state_names[] = {
183 "LAPD_STATE_NULL",
184 "LAPD_STATE_TEI_UNASS",
185 "LAPD_STATE_ASS_TEI_WAIT",
186 "LAPD_STATE_EST_TEI_WAIT",
187 "LAPD_STATE_IDLE",
188 "LAPD_STATE_SABM_SENT",
189 "LAPD_STATE_DISC_SENT",
190 "LAPD_STATE_MF_EST",
191 "LAPD_STATE_TIMER_RECOV",
192
193};
194
Andreas Eversberg742fc792011-09-27 09:40:25 +0200195static void lapd_start_t200(struct lapd_datalink *dl)
196{
197 if (osmo_timer_pending(&dl->t200))
198 return;
199 LOGP(DLLAPD, LOGL_INFO, "start T200\n");
200 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
201}
202
203static void lapd_start_t203(struct lapd_datalink *dl)
204{
205 if (osmo_timer_pending(&dl->t203))
206 return;
207 LOGP(DLLAPD, LOGL_INFO, "start T203\n");
208 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
209}
210
211static void lapd_stop_t200(struct lapd_datalink *dl)
212{
213 if (!osmo_timer_pending(&dl->t200))
214 return;
215 LOGP(DLLAPD, LOGL_INFO, "stop T200\n");
216 osmo_timer_del(&dl->t200);
217}
218
219static void lapd_stop_t203(struct lapd_datalink *dl)
220{
221 if (!osmo_timer_pending(&dl->t203))
222 return;
223 LOGP(DLLAPD, LOGL_INFO, "stop T203\n");
224 osmo_timer_del(&dl->t203);
225}
226
rootaf48bed2011-09-26 11:23:06 +0200227static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
228{
229 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s\n",
230 lapd_state_names[dl->state], lapd_state_names[state]);
231
232 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
233 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200234 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200235 /* remove content res. (network side) on leaving MF EST state */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200236 msgb_free(dl->cont_res);
237 dl->cont_res = NULL;
rootaf48bed2011-09-26 11:23:06 +0200238 }
239
240 /* start T203 on entering MF EST state, if enabled */
241 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200242 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
243 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200244
245 dl->state = state;
246}
247
rootaf48bed2011-09-26 11:23:06 +0200248static void *tall_lapd_ctx = NULL;
249
250/* init datalink instance and allocate history */
251void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
252 int maxf)
253{
254 int m;
255
256 memset(dl, 0, sizeof(*dl));
257 INIT_LLIST_HEAD(&dl->send_queue);
258 INIT_LLIST_HEAD(&dl->tx_queue);
259 dl->reestablish = 1;
260 dl->n200_est_rel = 3;
261 dl->n200 = 3;
262 dl->t200_sec = 1;
263 dl->t200_usec = 0;
264 dl->t200.data = dl;
265 dl->t200.cb = &lapd_t200_cb;
266 dl->t203_sec = 10;
267 dl->t203_usec = 0;
268 dl->t203.data = dl;
269 dl->t203.cb = &lapd_t203_cb;
270 dl->maxf = maxf;
271 if (k > v_range - 1)
272 k = v_range - 1;
273 dl->k = k;
274 dl->v_range = v_range;
275
276 /* Calculate modulo for history array:
277 * - The history range must be at least k+1.
278 * - The history range must be 2^x, where x is as low as possible.
279 */
280 k++;
281 for (m = 0x80; m; m >>= 1) {
282 if ((m & k)) {
283 if (k > m)
284 m <<= 1;
285 dl->range_hist = m;
286 break;
287 }
288 }
289
290 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
291 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
292
293 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
294
295 if (!tall_lapd_ctx)
296 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
Holger Hans Peter Freyther10f0bde2014-02-09 20:03:38 +0100297 dl->tx_hist = talloc_zero_array(tall_lapd_ctx,
298 struct lapd_history, dl->range_hist);
rootaf48bed2011-09-26 11:23:06 +0200299}
300
301/* reset to IDLE state */
302void lapd_dl_reset(struct lapd_datalink *dl)
303{
304 if (dl->state == LAPD_STATE_IDLE)
305 return;
306 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
307 /* enter idle state (and remove eventual cont_res) */
308 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
309 /* flush buffer */
310 lapd_dl_flush_tx(dl);
311 lapd_dl_flush_send(dl);
312 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +0200313 msgb_free(dl->rcv_buffer);
314 dl->rcv_buffer = NULL;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200315 /* stop Timers */
316 lapd_stop_t200(dl);
317 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200318}
319
320/* reset and de-allocate history buffer */
321void lapd_dl_exit(struct lapd_datalink *dl)
322{
323 /* free all ressources except history buffer */
324 lapd_dl_reset(dl);
325 /* free history buffer list */
326 talloc_free(dl->tx_hist);
Holger Hans Peter Freytherf5a079f2013-05-08 18:42:39 +0200327 dl->tx_hist = NULL;
rootaf48bed2011-09-26 11:23:06 +0200328}
329
330/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
331int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
332{
333 switch (mode) {
334 case LAPD_MODE_USER:
335 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
336 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
337 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
338 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
339 break;
340 case LAPD_MODE_NETWORK:
341 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
342 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
343 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
344 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
345 break;
346 default:
347 return -EINVAL;
348 }
349 dl->mode = mode;
350
351 return 0;
352}
353
354/* send DL message with optional msgb */
355static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
356 struct msgb *msg)
357{
358 struct lapd_datalink *dl = lctx->dl;
359 struct osmo_dlsap_prim dp;
360
361 osmo_prim_init(&dp.oph, 0, prim, op, msg);
362 return dl->send_dlsap(&dp, lctx);
363}
364
365/* send simple DL message */
366static inline int send_dl_simple(uint8_t prim, uint8_t op,
367 struct lapd_msg_ctx *lctx)
368{
369 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
370
371 return send_dl_l3(prim, op, lctx, msg);
372}
373
374/* send MDL-ERROR INDICATION */
375static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
376{
377 struct lapd_datalink *dl = lctx->dl;
378 struct osmo_dlsap_prim dp;
379
380 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %d\n",
381 cause);
382 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
383 dp.u.error_ind.cause = cause;
384 return dl->send_dlsap(&dp, lctx);
385}
386
387/* send UA response */
388static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
389{
390 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
391 struct lapd_msg_ctx nctx;
392 struct lapd_datalink *dl = lctx->dl;
393
394 memcpy(&nctx, lctx, sizeof(nctx));
395 msg->l3h = msgb_put(msg, len);
396 if (len)
397 memcpy(msg->l3h, data, len);
398 /* keep nctx.ldp */
399 /* keep nctx.sapi */
400 /* keep nctx.tei */
401 nctx.cr = dl->cr.loc2rem.resp;
402 nctx.format = LAPD_FORM_U;
403 nctx.s_u = LAPD_U_UA;
404 /* keep nctx.p_f */
405 nctx.length = len;
406 nctx.more = 0;
407
408 return dl->send_ph_data_req(&nctx, msg);
409}
410
411/* send DM response */
412static int lapd_send_dm(struct lapd_msg_ctx *lctx)
413{
414 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
415 struct lapd_msg_ctx nctx;
416 struct lapd_datalink *dl = lctx->dl;
417
418 memcpy(&nctx, lctx, sizeof(nctx));
419 /* keep nctx.ldp */
420 /* keep nctx.sapi */
421 /* keep nctx.tei */
422 nctx.cr = dl->cr.loc2rem.resp;
423 nctx.format = LAPD_FORM_U;
424 nctx.s_u = LAPD_U_DM;
425 /* keep nctx.p_f */
426 nctx.length = 0;
427 nctx.more = 0;
428
429 return dl->send_ph_data_req(&nctx, msg);
430}
431
432/* send RR response / command */
433static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
434{
435 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
436 struct lapd_msg_ctx nctx;
437 struct lapd_datalink *dl = lctx->dl;
438
439 memcpy(&nctx, lctx, sizeof(nctx));
440 /* keep nctx.ldp */
441 /* keep nctx.sapi */
442 /* keep nctx.tei */
443 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
444 nctx.format = LAPD_FORM_S;
445 nctx.s_u = LAPD_S_RR;
446 nctx.p_f = f_bit;
447 nctx.n_recv = dl->v_recv;
448 nctx.length = 0;
449 nctx.more = 0;
450
451 return dl->send_ph_data_req(&nctx, msg);
452}
453
454/* send RNR response / command */
455static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
456{
457 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
458 struct lapd_msg_ctx nctx;
459 struct lapd_datalink *dl = lctx->dl;
460
461 memcpy(&nctx, lctx, sizeof(nctx));
462 /* keep nctx.ldp */
463 /* keep nctx.sapi */
464 /* keep nctx.tei */
465 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
466 nctx.format = LAPD_FORM_S;
467 nctx.s_u = LAPD_S_RNR;
468 nctx.p_f = f_bit;
469 nctx.n_recv = dl->v_recv;
470 nctx.length = 0;
471 nctx.more = 0;
472
473 return dl->send_ph_data_req(&nctx, msg);
474}
475
476/* send REJ response */
477static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
478{
479 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
480 struct lapd_msg_ctx nctx;
481 struct lapd_datalink *dl = lctx->dl;
482
483 memcpy(&nctx, lctx, sizeof(nctx));
484 /* keep nctx.ldp */
485 /* keep nctx.sapi */
486 /* keep nctx.tei */
487 nctx.cr = dl->cr.loc2rem.resp;
488 nctx.format = LAPD_FORM_S;
489 nctx.s_u = LAPD_S_REJ;
490 nctx.p_f = f_bit;
491 nctx.n_recv = dl->v_recv;
492 nctx.length = 0;
493 nctx.more = 0;
494
495 return dl->send_ph_data_req(&nctx, msg);
496}
497
498/* resend SABM or DISC message */
499static int lapd_send_resend(struct lapd_datalink *dl)
500{
501 struct msgb *msg;
502 uint8_t h = do_mod(dl->v_send, dl->range_hist);
503 int length = dl->tx_hist[h].msg->len;
504 struct lapd_msg_ctx nctx;
505
506 /* assemble message */
507 memcpy(&nctx, &dl->lctx, sizeof(nctx));
508 /* keep nctx.ldp */
509 /* keep nctx.sapi */
510 /* keep nctx.tei */
511 nctx.cr = dl->cr.loc2rem.cmd;
512 nctx.format = LAPD_FORM_U;
513 if (dl->state == LAPD_STATE_SABM_SENT)
514 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
515 else
516 nctx.s_u = LAPD_U_DISC;
517 nctx.p_f = 1;
518 nctx.length = length;
519 nctx.more = 0;
520
521 /* Resend SABM/DISC from tx_hist */
522 msg = lapd_msgb_alloc(length, "LAPD resend");
523 msg->l3h = msgb_put(msg, length);
524 if (length)
525 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
526
527 return dl->send_ph_data_req(&nctx, msg);
528}
529
530/* reestablish link */
531static int lapd_reestablish(struct lapd_datalink *dl)
532{
533 struct osmo_dlsap_prim dp;
534 struct msgb *msg;
535
536 msg = lapd_msgb_alloc(0, "DUMMY");
537 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
538
539 return lapd_est_req(&dp, &dl->lctx);
540}
541
542/* Timer callback on T200 expiry */
543static void lapd_t200_cb(void *data)
544{
545 struct lapd_datalink *dl = data;
546
Andreas Eversberg742fc792011-09-27 09:40:25 +0200547 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200548 (int) dl->state);
549
550 switch (dl->state) {
551 case LAPD_STATE_SABM_SENT:
552 /* 5.4.1.3 */
553 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
554 /* send RELEASE INDICATION to L3 */
555 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
556 &dl->lctx);
557 /* send MDL ERROR INIDCATION to L3 */
558 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
559 /* 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 */
567 break;
568 }
569 /* retransmit SABM command */
570 lapd_send_resend(dl);
571 /* increment re-transmission counter */
572 dl->retrans_ctr++;
573 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200574 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200575 break;
576 case LAPD_STATE_DISC_SENT:
577 /* 5.4.4.3 */
578 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
579 /* send RELEASE INDICATION to L3 */
580 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
581 /* send MDL ERROR INIDCATION to L3 */
582 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
583 /* flush tx and send buffers */
584 lapd_dl_flush_tx(dl);
585 lapd_dl_flush_send(dl);
586 /* go back to idle state */
587 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
588 /* NOTE: we must not change any other states or buffers
589 * and queues, since we may reconnect after handover
590 * failure. the buffered messages is replaced there */
591 break;
592 }
593 /* retransmit DISC command */
594 lapd_send_resend(dl);
595 /* increment re-transmission counter */
596 dl->retrans_ctr++;
597 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200598 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200599 break;
600 case LAPD_STATE_MF_EST:
601 /* 5.5.7 */
602 dl->retrans_ctr = 0;
603 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
604 /* fall through */
605 case LAPD_STATE_TIMER_RECOV:
606 dl->retrans_ctr++;
607 if (dl->retrans_ctr < dl->n200) {
608 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
609 uint8_t h = do_mod(vs, dl->range_hist);
610 /* retransmit I frame (V_s-1) with P=1, if any */
611 if (dl->tx_hist[h].msg) {
612 struct msgb *msg;
613 int length = dl->tx_hist[h].msg->len;
614 struct lapd_msg_ctx nctx;
615
616 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
617 " V(S)=%d\n", vs);
618 /* Create I frame (segment) from tx_hist */
619 memcpy(&nctx, &dl->lctx, sizeof(nctx));
620 /* keep nctx.ldp */
621 /* keep nctx.sapi */
622 /* keep nctx.tei */
623 nctx.cr = dl->cr.loc2rem.cmd;
624 nctx.format = LAPD_FORM_I;
625 nctx.p_f = 1;
626 nctx.n_send = vs;
627 nctx.n_recv = dl->v_recv;
628 nctx.length = length;
629 nctx.more = dl->tx_hist[h].more;
630 msg = lapd_msgb_alloc(length, "LAPD I resend");
631 msg->l3h = msgb_put(msg, length);
632 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
633 length);
634 dl->send_ph_data_req(&nctx, msg);
635 } else {
636 /* OR send appropriate supervision frame with P=1 */
637 if (!dl->own_busy && !dl->seq_err_cond) {
638 lapd_send_rr(&dl->lctx, 1, 1);
639 /* NOTE: In case of sequence error
640 * condition, the REJ frame has been
641 * transmitted when entering the
642 * condition, so it has not be done
643 * here
644 */
645 } else if (dl->own_busy) {
646 lapd_send_rnr(&dl->lctx, 1, 1);
647 } else {
648 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
649 "pls. fix\n");
650 }
651 }
652 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200653 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200654 } else {
655 /* send MDL ERROR INIDCATION to L3 */
656 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
657 /* reestablish */
658 if (!dl->reestablish)
659 break;
660 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
661 "reestablishment.\n");
662 lapd_reestablish(dl);
663 }
664 break;
665 default:
666 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
667 "dl->state %d\n", (int) dl->state);
668 }
669}
670
671/* Timer callback on T203 expiry */
672static void lapd_t203_cb(void *data)
673{
674 struct lapd_datalink *dl = data;
675
Andreas Eversberg742fc792011-09-27 09:40:25 +0200676 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200677 (int) dl->state);
678
679 if (dl->state != LAPD_STATE_MF_EST) {
680 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
681 "please fix!\n");
682 return;
683 }
684
685 /* set retransmission counter to 0 */
686 dl->retrans_ctr = 0;
687 /* enter timer recovery state */
688 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
689 /* transmit a supervisory command with P bit set to 1 as follows: */
690 if (!dl->own_busy) {
691 LOGP(DLLAPD, LOGL_INFO, "transmit an RR poll command\n");
692 /* Send RR with P=1 */
693 lapd_send_rr(&dl->lctx, 1, 1);
694 } else {
695 LOGP(DLLAPD, LOGL_INFO, "transmit an RNR poll command\n");
696 /* Send RNR with P=1 */
697 lapd_send_rnr(&dl->lctx, 1, 1);
698 }
699 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200700 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200701}
702
703/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
704static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
705{
706 struct lapd_datalink *dl = lctx->dl;
707 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100708 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200709 int i, h;
710
711 /* supervisory frame ? */
712 if (lctx->format == LAPD_FORM_S)
713 s = 1;
714 /* REJ frame ? */
715 if (s && lctx->s_u == LAPD_S_REJ)
716 rej = 1;
717
718 /* Flush all transmit buffers of acknowledged frames */
719 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
720 h = do_mod(i, dl->range_hist);
721 if (dl->tx_hist[h].msg) {
722 msgb_free(dl->tx_hist[h].msg);
723 dl->tx_hist[h].msg = NULL;
724 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
725 }
726 }
727
728 if (dl->state != LAPD_STATE_TIMER_RECOV) {
729 /* When not in the timer recovery condition, the data
730 * link layer entity shall reset the timer T200 on
731 * receipt of a valid I frame with N(R) higher than V(A),
732 * or an REJ with an N(R) equal to V(A). */
733 if ((!rej && nr != dl->v_ack)
734 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200735 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200736 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200737 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
738 }
739 /* 5.7.4: N(R) sequence error
740 * N(R) is called valid, if and only if
741 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
742 */
743 if (sub_mod(nr, dl->v_ack, dl->v_range)
744 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
745 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error\n");
746 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
747 }
748 }
749
750 /* V(A) shall be set to the value of N(R) */
751 dl->v_ack = nr;
752
Andreas Eversberg742fc792011-09-27 09:40:25 +0200753 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200754 * and if there are outstanding I frames, restart T200 */
755 if (t200_reset && !rej) {
756 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
757 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
758 "frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200759 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200760 }
761 }
762
763 /* This also does a restart, when I or S frame is received */
764
765 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200766 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200767 /* Start T203, if T200 is not running in MF EST state, if enabled */
768 if (!osmo_timer_pending(&dl->t200)
769 && (dl->t203_sec || dl->t203_usec)
770 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200771 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200772 }
773}
774
775/* L1 -> L2 */
776
777/* Receive a LAPD U (Unnumbered) message from L1 */
778static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
779{
780 struct lapd_datalink *dl = lctx->dl;
781 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100782 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200783 uint8_t prim, op;
784
785 switch (lctx->s_u) {
786 case LAPD_U_SABM:
787 case LAPD_U_SABME:
788 prim = PRIM_DL_EST;
789 op = PRIM_OP_INDICATION;
790
791 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s\n",
792 lapd_state_names[dl->state]);
793 /* 5.7.1 */
794 dl->seq_err_cond = 0;
795 /* G.2.2 Wrong value of the C/R bit */
796 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100797 LOGP(DLLAPD, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200798 msgb_free(msg);
799 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
800 return -EINVAL;
801 }
802
803 /* G.4.5 If SABM is received with L>N201 or with M bit
804 * set, AN MDL-ERROR-INDICATION is sent to MM.
805 */
806 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100807 LOGP(DLLAPD, LOGL_ERROR, "SABM too large error\n");
rootaf48bed2011-09-26 11:23:06 +0200808 msgb_free(msg);
809 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
810 return -EIO;
811 }
812
813 switch (dl->state) {
814 case LAPD_STATE_IDLE:
815 break;
816 case LAPD_STATE_MF_EST:
817 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
818 "frame established state\n");
819 /* If link is lost on the remote side, we start over
820 * and send DL-ESTABLISH indication again. */
Andreas Eversberg6e182082013-02-06 14:13:21 +0100821 /* Additionally, continue in case of content resoltion
822 * (GSM network). This happens, if the mobile has not
823 * yet received UA or another mobile (collision) tries
824 * to establish connection. The mobile must receive
825 * UA again. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200826 /* 5.4.2.1 */
827 if (!length) {
828 /* If no content resolution, this is a
829 * re-establishment. */
830 LOGP(DLLAPD, LOGL_INFO,
831 "Remote reestablish\n");
rootaf48bed2011-09-26 11:23:06 +0200832 break;
833 }
Andreas Eversbergccc46332013-06-12 09:25:27 +0200834 if (!dl->cont_res) {
835 LOGP(DLLAPD, LOGL_INFO, "SABM command not "
836 "allowed in this state\n");
837 mdl_error(MDL_CAUSE_SABM_MF, lctx);
838 msgb_free(msg);
839 return 0;
840 }
rootaf48bed2011-09-26 11:23:06 +0200841 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200842 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200843#ifdef TEST_CONTENT_RESOLUTION_NETWORK
844 dl->cont_res->data[0] ^= 0x01;
845#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100846 if (memcmp(dl->cont_res->data, msg->data,
847 length)) {
rootaf48bed2011-09-26 11:23:06 +0200848 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
849 "with diffrent content - "
850 "ignoring!\n");
851 msgb_free(msg);
852 return 0;
853 }
854 }
855 /* send UA again */
856 lapd_send_ua(lctx, length, msg->l3h);
857 msgb_free(msg);
858 return 0;
859 case LAPD_STATE_DISC_SENT:
860 /* 5.4.6.2 send DM with F=P */
861 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200862 /* stop Timer T200 */
863 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200864 msgb_free(msg);
865 return send_dl_simple(prim, op, lctx);
866 default:
867 /* collision: Send UA, but still wait for rx UA, then
868 * change to MF_EST state.
869 */
870 /* check for contention resoultion */
871 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
872 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
873 "during contention resolution\n");
874 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
875 }
876 lapd_send_ua(lctx, length, msg->l3h);
877 msgb_free(msg);
878 return 0;
879 }
880 /* save message context for further use */
881 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
882#ifndef TEST_CONTENT_RESOLUTION_NETWORK
883 /* send UA response */
884 lapd_send_ua(lctx, length, msg->l3h);
885#endif
886 /* set Vs, Vr and Va to 0 */
887 dl->v_send = dl->v_recv = dl->v_ack = 0;
888 /* clear tx_hist */
889 lapd_dl_flush_hist(dl);
890 /* enter multiple-frame-established state */
891 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
892 /* store content resolution data on network side
893 * Note: cont_res will be removed when changing state again,
894 * so it must be allocated AFTER lapd_dl_newstate(). */
895 if (dl->mode == LAPD_MODE_NETWORK && length) {
896 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
897 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
898 length);
899 LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n");
900 }
901 /* send notification to L3 */
902 if (length == 0) {
903 /* 5.4.1.2 Normal establishment procedures */
904 rc = send_dl_simple(prim, op, lctx);
905 msgb_free(msg);
906 } else {
907 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200908 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200909 rc = send_dl_l3(prim, op, lctx, msg);
910 }
911 break;
912 case LAPD_U_DM:
913 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
914 lapd_state_names[dl->state]);
915 /* G.2.2 Wrong value of the C/R bit */
916 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100917 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200918 msgb_free(msg);
919 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
920 return -EINVAL;
921 }
922 if (!lctx->p_f) {
923 /* 5.4.1.2 DM responses with the F bit set to "0"
924 * shall be ignored.
925 */
926 msgb_free(msg);
927 return 0;
928 }
929 switch (dl->state) {
930 case LAPD_STATE_SABM_SENT:
931 break;
932 case LAPD_STATE_MF_EST:
933 if (lctx->p_f) {
934 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
935 "response\n");
936 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
937 } else {
938 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
939 "response, multiple frame established "
940 "state\n");
941 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
942 /* reestablish */
943 if (!dl->reestablish) {
944 msgb_free(msg);
945 return 0;
946 }
947 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
948 "reestablishment.\n");
949 lapd_reestablish(dl);
950 }
951 msgb_free(msg);
952 return 0;
953 case LAPD_STATE_TIMER_RECOV:
954 /* FP = 0 (DM is normal in case PF = 1) */
955 if (!lctx->p_f) {
956 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
957 "response, multiple frame established "
958 "state\n");
959 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
960 msgb_free(msg);
961 /* reestablish */
962 if (!dl->reestablish)
963 return 0;
964 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
965 "reestablishment.\n");
966 return lapd_reestablish(dl);
967 }
968 break;
969 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200970 /* stop Timer T200 */
971 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200972 /* go to idle state */
973 lapd_dl_flush_tx(dl);
974 lapd_dl_flush_send(dl);
975 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
976 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
977 msgb_free(msg);
978 return 0;
979 case LAPD_STATE_IDLE:
980 /* 5.4.5 all other frame types shall be discarded */
981 default:
982 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
983 "(discarding)\n");
984 msgb_free(msg);
985 return 0;
986 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200987 /* stop timer T200 */
988 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200989 /* go to idle state */
990 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
991 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
992 msgb_free(msg);
993 break;
994 case LAPD_U_UI:
995 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
996 /* G.2.2 Wrong value of the C/R bit */
997 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100998 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +0200999 "error\n");
1000 msgb_free(msg);
1001 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1002 return -EINVAL;
1003 }
1004
1005 /* G.4.5 If UI is received with L>N201 or with M bit
1006 * set, AN MDL-ERROR-INDICATION is sent to MM.
1007 */
1008 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001009 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +02001010 "(%d > N201(%d) or M=%d)\n", length,
1011 lctx->n201, lctx->more);
1012 msgb_free(msg);
1013 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1014 return -EIO;
1015 }
1016
1017 /* do some length checks */
1018 if (length == 0) {
1019 /* 5.3.3 UI frames received with the length indicator
1020 * set to "0" shall be ignored
1021 */
1022 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1023 msgb_free(msg);
1024 return 0;
1025 }
Harald Welte087116a2013-06-18 21:41:34 +02001026 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001027 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1028 msg);
1029 break;
1030 case LAPD_U_DISC:
1031 prim = PRIM_DL_REL;
1032 op = PRIM_OP_INDICATION;
1033
1034 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1035 lapd_state_names[dl->state]);
1036 /* flush tx and send buffers */
1037 lapd_dl_flush_tx(dl);
1038 lapd_dl_flush_send(dl);
1039 /* 5.7.1 */
1040 dl->seq_err_cond = 0;
1041 /* G.2.2 Wrong value of the C/R bit */
1042 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001043 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001044 msgb_free(msg);
1045 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1046 return -EINVAL;
1047 }
1048 if (length > 0 || lctx->more) {
1049 /* G.4.4 If a DISC or DM frame is received with L>0 or
1050 * with the M bit set to "1", an MDL-ERROR-INDICATION
1051 * primitive with cause "U frame with incorrect
1052 * parameters" is sent to the mobile management entity.
1053 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001054 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001055 "U frame iwth incorrect parameters ");
1056 msgb_free(msg);
1057 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1058 return -EIO;
1059 }
1060 switch (dl->state) {
1061 case LAPD_STATE_IDLE:
1062 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1063 /* send DM with F=P */
1064 msgb_free(msg);
1065 return lapd_send_dm(lctx);
1066 case LAPD_STATE_SABM_SENT:
1067 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1068 /* 5.4.6.2 send DM with F=P */
1069 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001070 /* stop Timer T200 */
1071 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001072 /* go to idle state */
1073 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1074 msgb_free(msg);
1075 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1076 lctx);
1077 case LAPD_STATE_MF_EST:
1078 case LAPD_STATE_TIMER_RECOV:
1079 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1080 break;
1081 case LAPD_STATE_DISC_SENT:
1082 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1083 prim = PRIM_DL_REL;
1084 op = PRIM_OP_CONFIRM;
1085 break;
1086 default:
1087 lapd_send_ua(lctx, length, msg->l3h);
1088 msgb_free(msg);
1089 return 0;
1090 }
1091 /* send UA response */
1092 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001093 /* stop Timer T200 */
1094 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001095 /* enter idle state, keep tx-buffer with UA response */
1096 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1097 /* send notification to L3 */
1098 rc = send_dl_simple(prim, op, lctx);
1099 msgb_free(msg);
1100 break;
1101 case LAPD_U_UA:
1102 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1103 lapd_state_names[dl->state]);
1104 /* G.2.2 Wrong value of the C/R bit */
1105 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001106 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001107 "error\n");
1108 msgb_free(msg);
1109 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1110 return -EINVAL;
1111 }
1112
1113 /* G.4.5 If UA is received with L>N201 or with M bit
1114 * set, AN MDL-ERROR-INDICATION is sent to MM.
1115 */
1116 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001117 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001118 msgb_free(msg);
1119 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1120 return -EIO;
1121 }
1122
1123 if (!lctx->p_f) {
1124 /* 5.4.1.2 A UA response with the F bit set to "0"
1125 * shall be ignored.
1126 */
1127 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1128 msgb_free(msg);
1129 return 0;
1130 }
1131 switch (dl->state) {
1132 case LAPD_STATE_SABM_SENT:
1133 break;
1134 case LAPD_STATE_MF_EST:
1135 case LAPD_STATE_TIMER_RECOV:
1136 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1137 "(discarding)\n");
1138 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1139 msgb_free(msg);
1140 return 0;
1141 case LAPD_STATE_DISC_SENT:
1142 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001143 /* stop Timer T200 */
1144 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001145 /* go to idle state */
1146 lapd_dl_flush_tx(dl);
1147 lapd_dl_flush_send(dl);
1148 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1149 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1150 msgb_free(msg);
1151 return 0;
1152 case LAPD_STATE_IDLE:
1153 /* 5.4.5 all other frame types shall be discarded */
1154 default:
1155 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1156 "(discarding)\n");
1157 msgb_free(msg);
1158 return 0;
1159 }
1160 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001161 /* stop Timer T200 */
1162 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001163 /* compare UA with SABME if contention resolution is applied */
1164 if (dl->tx_hist[0].msg->len) {
1165 if (length != (dl->tx_hist[0].msg->len)
1166 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1167 length)) {
1168 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1169 "mismatches ****\n");
1170 rc = send_dl_simple(PRIM_DL_REL,
1171 PRIM_OP_INDICATION, lctx);
1172 msgb_free(msg);
1173 /* go to idle state */
1174 lapd_dl_flush_tx(dl);
1175 lapd_dl_flush_send(dl);
1176 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1177 return 0;
1178 }
1179 }
1180 /* set Vs, Vr and Va to 0 */
1181 dl->v_send = dl->v_recv = dl->v_ack = 0;
1182 /* clear tx_hist */
1183 lapd_dl_flush_hist(dl);
1184 /* enter multiple-frame-established state */
1185 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1186 /* send outstanding frames, if any (resume / reconnect) */
1187 lapd_send_i(lctx, __LINE__);
1188 /* send notification to L3 */
1189 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1190 msgb_free(msg);
1191 break;
1192 case LAPD_U_FRMR:
1193 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1194 /* send MDL ERROR INIDCATION to L3 */
1195 mdl_error(MDL_CAUSE_FRMR, lctx);
1196 msgb_free(msg);
1197 /* reestablish */
1198 if (!dl->reestablish)
1199 break;
1200 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1201 rc = lapd_reestablish(dl);
1202 break;
1203 default:
1204 /* G.3.1 */
1205 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1206 msgb_free(msg);
1207 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1208 return -EINVAL;
1209 }
1210 return rc;
1211}
1212
1213/* Receive a LAPD S (Supervisory) message from L1 */
1214static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1215{
1216 struct lapd_datalink *dl = lctx->dl;
1217 int length = lctx->length;
1218
1219 if (length > 0 || lctx->more) {
1220 /* G.4.3 If a supervisory frame is received with L>0 or
1221 * with the M bit set to "1", an MDL-ERROR-INDICATION
1222 * primitive with cause "S frame with incorrect
1223 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001224 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001225 "S frame with incorrect parameters\n");
1226 msgb_free(msg);
1227 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1228 return -EIO;
1229 }
1230
1231 if (lctx->cr == dl->cr.rem2loc.resp
1232 && lctx->p_f
1233 && dl->state != LAPD_STATE_TIMER_RECOV) {
1234 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1235 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1236 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1237 }
1238
1239 switch (dl->state) {
1240 case LAPD_STATE_IDLE:
1241 /* if P=1, respond DM with F=1 (5.2.2) */
1242 /* 5.4.5 all other frame types shall be discarded */
1243 if (lctx->p_f)
1244 lapd_send_dm(lctx); /* F=P */
1245 /* fall though */
1246 case LAPD_STATE_SABM_SENT:
1247 case LAPD_STATE_DISC_SENT:
1248 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1249 msgb_free(msg);
1250 return 0;
1251 }
1252 switch (lctx->s_u) {
1253 case LAPD_S_RR:
1254 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1255 lapd_state_names[dl->state]);
1256 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1257 lapd_acknowledge(lctx);
1258
1259 /* 5.5.3.2 */
1260 if (lctx->cr == dl->cr.rem2loc.cmd
1261 && lctx->p_f) {
1262 if (!dl->own_busy && !dl->seq_err_cond) {
1263 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1264 "with polling bit set and we are not "
1265 "busy, so we reply with RR frame "
1266 "response\n");
1267 lapd_send_rr(lctx, 1, 0);
1268 /* NOTE: In case of sequence error condition,
1269 * the REJ frame has been transmitted when
1270 * entering the condition, so it has not be
1271 * done here
1272 */
1273 } else if (dl->own_busy) {
1274 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1275 "with polling bit set and we are busy, "
1276 "so we reply with RR frame response\n");
1277 lapd_send_rnr(lctx, 1, 0);
1278 }
1279 } else if (lctx->cr == dl->cr.rem2loc.resp
1280 && lctx->p_f
1281 && dl->state == LAPD_STATE_TIMER_RECOV) {
1282 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1283 "and we are in timer recovery state, so "
1284 "we leave that state\n");
1285 /* V(S) to the N(R) in the RR frame */
1286 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001287 /* stop Timer T200 */
1288 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001289 /* 5.5.7 Clear timer recovery condition */
1290 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1291 }
1292 /* Send message, if possible due to acknowledged data */
1293 lapd_send_i(lctx, __LINE__);
1294
1295 break;
1296 case LAPD_S_RNR:
1297 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1298 lapd_state_names[dl->state]);
1299 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1300 lapd_acknowledge(lctx);
1301
1302 /* 5.5.5 */
1303 /* Set peer receiver busy condition */
1304 dl->peer_busy = 1;
1305
1306 if (lctx->p_f) {
1307 if (lctx->cr == dl->cr.rem2loc.cmd) {
1308 if (!dl->own_busy) {
1309 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1310 "command and we are not busy, "
1311 "so we reply with RR final "
1312 "response\n");
1313 /* Send RR with F=1 */
1314 lapd_send_rr(lctx, 1, 0);
1315 } else {
1316 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1317 "command and we are busy, so "
1318 "we reply with RNR final "
1319 "response\n");
1320 /* Send RNR with F=1 */
1321 lapd_send_rnr(lctx, 1, 0);
1322 }
1323 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1324 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1325 "and we in timer recovery state, so "
1326 "we leave that state\n");
1327 /* 5.5.7 Clear timer recovery condition */
1328 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1329 /* V(S) to the N(R) in the RNR frame */
1330 dl->v_send = lctx->n_recv;
1331 }
1332 } else
1333 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1334 "received\n");
1335
1336 /* Send message, if possible due to acknowledged data */
1337 lapd_send_i(lctx, __LINE__);
1338
1339 break;
1340 case LAPD_S_REJ:
1341 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1342 lapd_state_names[dl->state]);
1343 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1344 lapd_acknowledge(lctx);
1345
1346 /* 5.5.4.1 */
1347 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1348 /* Clear an existing peer receiver busy condition */
1349 dl->peer_busy = 0;
1350 /* V(S) and V(A) to the N(R) in the REJ frame */
1351 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001352 /* stop Timer T200 */
1353 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001354 /* 5.5.3.2 */
1355 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1356 if (!dl->own_busy && !dl->seq_err_cond) {
1357 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1358 "command not in timer recovery "
1359 "state and not in own busy "
1360 "condition received, so we "
1361 "respond with RR final "
1362 "response\n");
1363 lapd_send_rr(lctx, 1, 0);
1364 /* NOTE: In case of sequence error
1365 * condition, the REJ frame has been
1366 * transmitted when entering the
1367 * condition, so it has not be done
1368 * here
1369 */
1370 } else if (dl->own_busy) {
1371 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1372 "command not in timer recovery "
1373 "state and in own busy "
1374 "condition received, so we "
1375 "respond with RNR final "
1376 "response\n");
1377 lapd_send_rnr(lctx, 1, 0);
1378 }
1379 } else
1380 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1381 "polling command not in timer recovery "
1382 "state received\n");
1383 /* send MDL ERROR INIDCATION to L3 */
1384 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1385 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1386 }
1387
1388 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1389 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1390 "recovery state received\n");
1391 /* Clear an existing peer receiver busy condition */
1392 dl->peer_busy = 0;
1393 /* V(S) and V(A) to the N(R) in the REJ frame */
1394 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001395 /* stop Timer T200 */
1396 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001397 /* 5.5.7 Clear timer recovery condition */
1398 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1399 } else {
1400 /* Clear an existing peer receiver busy condition */
1401 dl->peer_busy = 0;
1402 /* V(S) and V(A) to the N(R) in the REJ frame */
1403 dl->v_send = dl->v_ack = lctx->n_recv;
1404 /* 5.5.3.2 */
1405 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1406 if (!dl->own_busy && !dl->seq_err_cond) {
1407 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1408 "command in timer recovery "
1409 "state and not in own busy "
1410 "condition received, so we "
1411 "respond with RR final "
1412 "response\n");
1413 lapd_send_rr(lctx, 1, 0);
1414 /* NOTE: In case of sequence error
1415 * condition, the REJ frame has been
1416 * transmitted when entering the
1417 * condition, so it has not be done
1418 * here
1419 */
1420 } else if (dl->own_busy) {
1421 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1422 "command in timer recovery "
1423 "state and in own busy "
1424 "condition received, so we "
1425 "respond with RNR final "
1426 "response\n");
1427 lapd_send_rnr(lctx, 1, 0);
1428 }
1429 } else
1430 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1431 "polling command in timer recovery "
1432 "state received\n");
1433 }
1434
1435 /* FIXME: 5.5.4.2 2) */
1436
1437 /* Send message, if possible due to acknowledged data */
1438 lapd_send_i(lctx, __LINE__);
1439
1440 break;
1441 default:
1442 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001443 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001444 msgb_free(msg);
1445 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1446 return -EINVAL;
1447 }
1448 msgb_free(msg);
1449 return 0;
1450}
1451
1452/* Receive a LAPD I (Information) message from L1 */
1453static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1454{
1455 struct lapd_datalink *dl = lctx->dl;
1456 //uint8_t nr = lctx->n_recv;
1457 uint8_t ns = lctx->n_send;
1458 int length = lctx->length;
1459 int rc;
1460
Holger Hans Peter Freyther1512ea62014-03-16 23:59:58 +01001461 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1462 lapd_state_names[dl->state], lctx->sapi);
rootaf48bed2011-09-26 11:23:06 +02001463
1464 /* G.2.2 Wrong value of the C/R bit */
1465 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001466 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001467 msgb_free(msg);
1468 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1469 return -EINVAL;
1470 }
1471
1472 if (length == 0 || length > lctx->n201) {
1473 /* G.4.2 If the length indicator of an I frame is set
1474 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1475 * primitive with cause "I frame with incorrect length"
1476 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001477 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001478 msgb_free(msg);
1479 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1480 return -EIO;
1481 }
1482
1483 /* G.4.2 If the numerical value of L is L<N201 and the M
1484 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1485 * cause "I frame with incorrect use of M bit" is sent to the
1486 * mobile management entity. */
1487 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001488 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001489 msgb_free(msg);
1490 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1491 return -EIO;
1492 }
1493
1494 switch (dl->state) {
1495 case LAPD_STATE_IDLE:
1496 /* if P=1, respond DM with F=1 (5.2.2) */
1497 /* 5.4.5 all other frame types shall be discarded */
1498 if (lctx->p_f)
1499 lapd_send_dm(lctx); /* F=P */
1500 /* fall though */
1501 case LAPD_STATE_SABM_SENT:
1502 case LAPD_STATE_DISC_SENT:
1503 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1504 msgb_free(msg);
1505 return 0;
1506 }
1507
1508 /* 5.7.1: N(s) sequence error */
1509 if (ns != dl->v_recv) {
1510 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1511 "V(R)=%u\n", ns, dl->v_recv);
1512 /* discard data */
1513 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001514 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001515 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001516 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001517 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001518 lapd_send_rej(lctx, lctx->p_f);
1519 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001520 /* If there are two subsequent sequence errors received,
1521 * ignore it. (Ignore every second subsequent error.)
1522 * This happens if our reply with the REJ is too slow,
1523 * so the remote gets a T200 timeout and sends another
1524 * frame with a sequence error.
1525 * Test showed that replying with two subsequent REJ
1526 * messages could the remote L2 process to abort.
1527 * Replying too slow shouldn't happen, but may happen
1528 * over serial link between BB and LAPD.
1529 */
1530 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001531 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001532 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1533 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1534 lapd_acknowledge(lctx); /* V(A) is also set here */
1535
1536 /* Send message, if possible due to acknowledged data */
1537 lapd_send_i(lctx, __LINE__);
1538
1539 return 0;
rootaf48bed2011-09-26 11:23:06 +02001540 }
1541 dl->seq_err_cond = 0;
1542
1543 /* Increment receiver state */
1544 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1545 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1546
1547 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1548 lapd_acknowledge(lctx); /* V(A) is also set here */
1549
1550 /* Only if we are not in own receiver busy condition */
1551 if (!dl->own_busy) {
1552 /* if the frame carries a complete segment */
1553 if (!lctx->more && !dl->rcv_buffer) {
1554 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1555 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001556 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001557 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1558 msg);
1559 } else {
1560 /* create rcv_buffer */
1561 if (!dl->rcv_buffer) {
1562 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1563 "I frames (first message)\n");
1564 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1565 "LAPD RX");
1566 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1567 }
1568 /* concat. rcv_buffer */
1569 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1570 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1571 "overflow!\n");
1572 } else {
1573 memcpy(msgb_put(dl->rcv_buffer, length),
1574 msg->l3h, length);
1575 }
1576 /* if the last segment was received */
1577 if (!lctx->more) {
1578 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1579 "I frames (last message)\n");
1580 rc = send_dl_l3(PRIM_DL_DATA,
1581 PRIM_OP_INDICATION, lctx,
1582 dl->rcv_buffer);
1583 dl->rcv_buffer = NULL;
1584 } else
1585 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1586 "I frames (next message)\n");
1587 msgb_free(msg);
1588
1589 }
1590 } else
1591 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1592 "busy condition\n");
1593
1594 /* Check for P bit */
1595 if (lctx->p_f) {
1596 /* 5.5.2.1 */
1597 /* check if we are not in own receiver busy */
1598 if (!dl->own_busy) {
1599 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1600 /* Send RR with F=1 */
1601 rc = lapd_send_rr(lctx, 1, 0);
1602 } else {
1603 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1604 /* Send RNR with F=1 */
1605 rc = lapd_send_rnr(lctx, 1, 0);
1606 }
1607 } else {
1608 /* 5.5.2.2 */
1609 /* check if we are not in own receiver busy */
1610 if (!dl->own_busy) {
1611 /* NOTE: V(R) is already set above */
1612 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001613
1614 /* if update_pending_iframe returns 0 it updated
1615 * the lapd header of an iframe in the tx queue */
1616 if (rc && dl->update_pending_frames)
1617 rc = dl->update_pending_frames(lctx);
1618
rootaf48bed2011-09-26 11:23:06 +02001619 if (rc) {
1620 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1621 "have no pending data, send RR\n");
1622 /* Send RR with F=0 */
1623 return lapd_send_rr(lctx, 0, 0);
1624 }
1625 /* all I or one RR is sent, we are done */
1626 return 0;
1627 } else {
1628 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1629 /* Send RNR with F=0 */
1630 rc = lapd_send_rnr(lctx, 0, 0);
1631 }
1632 }
1633
1634 /* Send message, if possible due to acknowledged data */
1635 lapd_send_i(lctx, __LINE__);
1636
1637 return rc;
1638}
1639
1640/* Receive a LAPD message from L1 */
1641int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1642{
1643 int rc;
1644
1645 switch (lctx->format) {
1646 case LAPD_FORM_U:
1647 rc = lapd_rx_u(msg, lctx);
1648 break;
1649 case LAPD_FORM_S:
1650 rc = lapd_rx_s(msg, lctx);
1651 break;
1652 case LAPD_FORM_I:
1653 rc = lapd_rx_i(msg, lctx);
1654 break;
1655 default:
1656 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1657 msgb_free(msg);
1658 rc = -EINVAL;
1659 }
1660 return rc;
1661}
1662
1663/* L3 -> L2 */
1664
1665/* send unit data */
1666static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1667{
1668 struct lapd_datalink *dl = lctx->dl;
1669 struct msgb *msg = dp->oph.msg;
1670 struct lapd_msg_ctx nctx;
1671
1672 memcpy(&nctx, lctx, sizeof(nctx));
1673 /* keep nctx.ldp */
1674 /* keep nctx.sapi */
1675 /* keep nctx.tei */
1676 nctx.cr = dl->cr.loc2rem.cmd;
1677 nctx.format = LAPD_FORM_U;
1678 nctx.s_u = LAPD_U_UI;
1679 /* keep nctx.p_f */
1680 nctx.length = msg->len;
1681 nctx.more = 0;
1682
1683 return dl->send_ph_data_req(&nctx, msg);
1684}
1685
1686/* request link establishment */
1687static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1688{
1689 struct lapd_datalink *dl = lctx->dl;
1690 struct msgb *msg = dp->oph.msg;
1691 struct lapd_msg_ctx nctx;
1692
1693 if (msg->len)
1694 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1695 "(SABM)\n");
1696 else
1697 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1698
1699 /* Flush send-queue */
1700 /* Clear send-buffer */
1701 lapd_dl_flush_send(dl);
1702 /* be sure that history is empty */
1703 lapd_dl_flush_hist(dl);
1704
1705 /* save message context for further use */
1706 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1707
1708 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001709 msgb_free(dl->rcv_buffer);
1710 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001711
1712 /* assemble message */
1713 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1714 /* keep nctx.ldp */
1715 /* keep nctx.sapi */
1716 /* keep nctx.tei */
1717 nctx.cr = dl->cr.loc2rem.cmd;
1718 nctx.format = LAPD_FORM_U;
1719 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1720 nctx.p_f = 1;
1721 nctx.length = msg->len;
1722 nctx.more = 0;
1723
1724 /* Transmit-buffer carries exactly one segment */
1725 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1726 msgb_put(dl->tx_hist[0].msg, msg->len);
1727 if (msg->len)
1728 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1729 dl->tx_hist[0].more = 0;
1730 /* set Vs to 0, because it is used as index when resending SABM */
1731 dl->v_send = 0;
1732
1733 /* Set states */
1734 dl->own_busy = dl->peer_busy = 0;
1735 dl->retrans_ctr = 0;
1736 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1737
1738 /* Tramsmit and start T200 */
1739 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001740 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001741
1742 return 0;
1743}
1744
1745/* send data */
1746static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1747{
1748 struct lapd_datalink *dl = lctx->dl;
1749 struct msgb *msg = dp->oph.msg;
1750
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001751 if (msgb_l3len(msg) == 0) {
1752 LOGP(DLLAPD, LOGL_ERROR,
1753 "writing an empty message is not possible.\n");
1754 msgb_free(msg);
1755 return -1;
1756 }
1757
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001758 LOGP(DLLAPD, LOGL_INFO,
1759 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001760
1761 /* Write data into the send queue */
1762 msgb_enqueue(&dl->send_queue, msg);
1763
1764 /* Send message, if possible */
1765 lapd_send_i(&dl->lctx, __LINE__);
1766
1767 return 0;
1768}
1769
1770/* Send next I frame from queued/buffered data */
1771static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1772{
1773 struct lapd_datalink *dl = lctx->dl;
1774 uint8_t k = dl->k;
1775 uint8_t h;
1776 struct msgb *msg;
1777 int length, left;
1778 int rc = - 1; /* we sent nothing */
1779 struct lapd_msg_ctx nctx;
1780
1781
1782 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1783
1784 next_frame:
1785
1786 if (dl->peer_busy) {
1787 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1788 return rc;
1789 }
1790
1791 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1792 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1793 return rc;
1794 }
1795
1796 /* If the send state variable V(S) is equal to V(A) plus k
1797 * (where k is the maximum number of outstanding I frames - see
1798 * subclause 5.8.4), the data link layer entity shall not transmit any
1799 * new I frames, but shall retransmit an I frame as a result
1800 * of the error recovery procedures as described in subclauses 5.5.4 and
1801 * 5.5.7. */
1802 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1803 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1804 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1805 dl->v_ack);
1806 return rc;
1807 }
1808
1809 h = do_mod(dl->v_send, dl->range_hist);
1810
1811 /* if we have no tx_hist yet, we create it */
1812 if (!dl->tx_hist[h].msg) {
1813 /* Get next message into send-buffer, if any */
1814 if (!dl->send_buffer) {
1815 next_message:
1816 dl->send_out = 0;
1817 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1818 /* No more data to be sent */
1819 if (!dl->send_buffer)
1820 return rc;
1821 LOGP(DLLAPD, LOGL_INFO, "get message from "
1822 "send-queue\n");
1823 }
1824
1825 /* How much is left in the send-buffer? */
1826 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1827 /* Segment, if data exceeds N201 */
1828 length = left;
1829 if (length > lctx->n201)
1830 length = lctx->n201;
1831 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1832 "length %d first byte %02x\n",
1833 msgb_l3len(dl->send_buffer), dl->send_out, left,
1834 lctx->n201, length, dl->send_buffer->l3h[0]);
1835 /* If message in send-buffer is completely sent */
1836 if (left == 0) {
1837 msgb_free(dl->send_buffer);
1838 dl->send_buffer = NULL;
1839 goto next_message;
1840 }
1841
1842 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1843 (left > length) ? "segment " : "", dl->v_send);
1844
1845 /* Create I frame (segment) and transmit-buffer content */
1846 msg = lapd_msgb_alloc(length, "LAPD I");
1847 msg->l3h = msgb_put(msg, length);
1848 /* assemble message */
1849 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1850 /* keep nctx.ldp */
1851 /* keep nctx.sapi */
1852 /* keep nctx.tei */
1853 nctx.cr = dl->cr.loc2rem.cmd;
1854 nctx.format = LAPD_FORM_I;
1855 nctx.p_f = 0;
1856 nctx.n_send = dl->v_send;
1857 nctx.n_recv = dl->v_recv;
1858 nctx.length = length;
1859 if (left > length)
1860 nctx.more = 1;
1861 else
1862 nctx.more = 0;
1863 if (length)
1864 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1865 length);
1866 /* store in tx_hist */
1867 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1868 msgb_put(dl->tx_hist[h].msg, msg->len);
1869 if (length)
1870 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1871 dl->tx_hist[h].more = nctx.more;
1872 /* Add length to track how much is already in the tx buffer */
1873 dl->send_out += length;
1874 } else {
1875 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1876 "V(S)=%d\n", dl->v_send);
1877
1878 /* Create I frame (segment) from tx_hist */
1879 length = dl->tx_hist[h].msg->len;
1880 msg = lapd_msgb_alloc(length, "LAPD I resend");
1881 msg->l3h = msgb_put(msg, length);
1882 /* assemble message */
1883 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1884 /* keep nctx.ldp */
1885 /* keep nctx.sapi */
1886 /* keep nctx.tei */
1887 nctx.cr = dl->cr.loc2rem.cmd;
1888 nctx.format = LAPD_FORM_I;
1889 nctx.p_f = 0;
1890 nctx.n_send = dl->v_send;
1891 nctx.n_recv = dl->v_recv;
1892 nctx.length = length;
1893 nctx.more = dl->tx_hist[h].more;
1894 if (length)
1895 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1896 }
1897
1898 /* The value of the send state variable V(S) shall be incremented by 1
1899 * at the end of the transmission of the I frame */
1900 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1901
1902 /* If timer T200 is not running at the time right before transmitting a
1903 * frame, when the PH-READY-TO-SEND primitive is received from the
1904 * physical layer., it shall be set. */
1905 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001906 /* stop Timer T203, if running */
1907 lapd_stop_t203(dl);
1908 /* start Timer T200 */
1909 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001910 }
1911
1912 dl->send_ph_data_req(&nctx, msg);
1913
1914 rc = 0; /* we sent something */
1915 goto next_frame;
1916}
1917
1918/* request link suspension */
1919static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1920{
1921 struct lapd_datalink *dl = lctx->dl;
1922 struct msgb *msg = dp->oph.msg;
1923
1924 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1925
1926 /* put back the send-buffer to the send-queue (first position) */
1927 if (dl->send_buffer) {
1928 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1929 "queue\n");
1930 llist_add(&dl->send_buffer->list, &dl->send_queue);
1931 dl->send_buffer = NULL;
1932 } else
1933 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1934
1935 /* Clear transmit buffer, but keep send buffer */
1936 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001937 /* Stop timers (there is no state change, so we must stop all timers */
1938 lapd_stop_t200(dl);
1939 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001940
1941 msgb_free(msg);
1942
1943 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1944}
1945
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001946/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001947static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1948{
1949 struct lapd_datalink *dl = lctx->dl;
1950 struct msgb *msg = dp->oph.msg;
1951 struct lapd_msg_ctx nctx;
1952
1953 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1954 msg->len);
1955
1956 /* be sure that history is empty */
1957 lapd_dl_flush_hist(dl);
1958
1959 /* save message context for further use */
1960 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1961
1962 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001963 msgb_free(dl->send_buffer);
1964 dl->send_buffer = NULL;
1965
rootaf48bed2011-09-26 11:23:06 +02001966 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001967 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02001968 /* Write data into the send buffer, to be sent first */
1969 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001970 } else {
1971 msgb_free(msg);
1972 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001973 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001974 }
rootaf48bed2011-09-26 11:23:06 +02001975
1976 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001977 msgb_free(dl->rcv_buffer);
1978 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001979
1980 /* Create new msgb (old one is now free) */
1981 msg = lapd_msgb_alloc(0, "LAPD SABM");
1982 msg->l3h = msg->data;
1983 /* assemble message */
1984 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1985 /* keep nctx.ldp */
1986 /* keep nctx.sapi */
1987 /* keep nctx.tei */
1988 nctx.cr = dl->cr.loc2rem.cmd;
1989 nctx.format = LAPD_FORM_U;
1990 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1991 nctx.p_f = 1;
1992 nctx.length = 0;
1993 nctx.more = 0;
1994
1995 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1996 msgb_put(dl->tx_hist[0].msg, msg->len);
1997 if (msg->len)
1998 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1999 dl->tx_hist[0].more = 0;
2000 /* set Vs to 0, because it is used as index when resending SABM */
2001 dl->v_send = 0;
2002
2003 /* Set states */
2004 dl->own_busy = dl->peer_busy = 0;
2005 dl->retrans_ctr = 0;
2006 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2007
2008 /* Tramsmit and start T200 */
2009 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002010 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002011
2012 return 0;
2013}
2014
2015/* requesst release of link */
2016static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2017{
2018 struct lapd_datalink *dl = lctx->dl;
2019 struct msgb *msg = dp->oph.msg;
2020 struct lapd_msg_ctx nctx;
2021
2022 /* local release */
2023 if (dp->u.rel_req.mode) {
2024 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2025 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002026 /* stop Timer T200 */
2027 lapd_stop_t200(dl);
2028 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002029 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2030 /* flush buffers */
2031 lapd_dl_flush_tx(dl);
2032 lapd_dl_flush_send(dl);
2033 /* send notification to L3 */
2034 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2035 }
2036
2037 /* in case we are already disconnecting */
2038 if (dl->state == LAPD_STATE_DISC_SENT)
2039 return -EBUSY;
2040
2041 /* flush tx_hist */
2042 lapd_dl_flush_hist(dl);
2043
2044 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2045
2046 /* Push LAPD header on msgb */
2047 /* assemble message */
2048 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2049 /* keep nctx.ldp */
2050 /* keep nctx.sapi */
2051 /* keep nctx.tei */
2052 nctx.cr = dl->cr.loc2rem.cmd;
2053 nctx.format = LAPD_FORM_U;
2054 nctx.s_u = LAPD_U_DISC;
2055 nctx.p_f = 1;
2056 nctx.length = 0;
2057 nctx.more = 0;
2058
2059 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2060 msgb_put(dl->tx_hist[0].msg, msg->len);
2061 if (msg->len)
2062 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2063 dl->tx_hist[0].more = 0;
2064 /* set Vs to 0, because it is used as index when resending DISC */
2065 dl->v_send = 0;
2066
2067 /* Set states */
2068 dl->own_busy = dl->peer_busy = 0;
2069 dl->retrans_ctr = 0;
2070 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2071
2072 /* Tramsmit and start T200 */
2073 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002074 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002075
2076 return 0;
2077}
2078
2079/* request release of link in idle state */
2080static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2081 struct lapd_msg_ctx *lctx)
2082{
2083 struct lapd_datalink *dl = lctx->dl;
2084 struct msgb *msg = dp->oph.msg;
2085
2086 msgb_free(msg);
2087
2088 /* send notification to L3 */
2089 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2090}
2091
2092/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002093static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002094 uint32_t states;
2095 int prim, op;
2096 const char *name;
2097 int (*rout) (struct osmo_dlsap_prim *dp,
2098 struct lapd_msg_ctx *lctx);
2099} l2downstatelist[] = {
2100 /* create and send UI command */
2101 {ALL_STATES,
2102 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2103 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2104
2105 /* create and send SABM command */
2106 {SBIT(LAPD_STATE_IDLE),
2107 PRIM_DL_EST, PRIM_OP_REQUEST,
2108 "DL-ESTABLISH-REQUEST", lapd_est_req},
2109
2110 /* create and send I command */
2111 {SBIT(LAPD_STATE_MF_EST) |
2112 SBIT(LAPD_STATE_TIMER_RECOV),
2113 PRIM_DL_DATA, PRIM_OP_REQUEST,
2114 "DL-DATA-REQUEST", lapd_data_req},
2115
2116 /* suspend datalink */
2117 {SBIT(LAPD_STATE_MF_EST) |
2118 SBIT(LAPD_STATE_TIMER_RECOV),
2119 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2120 "DL-SUSPEND-REQUEST", lapd_susp_req},
2121
2122 /* create and send SABM command (resume) */
2123 {SBIT(LAPD_STATE_MF_EST) |
2124 SBIT(LAPD_STATE_TIMER_RECOV),
2125 PRIM_DL_RES, PRIM_OP_REQUEST,
2126 "DL-RESUME-REQUEST", lapd_res_req},
2127
2128 /* create and send SABM command (reconnect) */
2129 {SBIT(LAPD_STATE_IDLE) |
2130 SBIT(LAPD_STATE_MF_EST) |
2131 SBIT(LAPD_STATE_TIMER_RECOV),
2132 PRIM_DL_RECON, PRIM_OP_REQUEST,
2133 "DL-RECONNECT-REQUEST", lapd_res_req},
2134
2135 /* create and send DISC command */
2136 {SBIT(LAPD_STATE_SABM_SENT) |
2137 SBIT(LAPD_STATE_MF_EST) |
2138 SBIT(LAPD_STATE_TIMER_RECOV) |
2139 SBIT(LAPD_STATE_DISC_SENT),
2140 PRIM_DL_REL, PRIM_OP_REQUEST,
2141 "DL-RELEASE-REQUEST", lapd_rel_req},
2142
2143 /* release in idle state */
2144 {SBIT(LAPD_STATE_IDLE),
2145 PRIM_DL_REL, PRIM_OP_REQUEST,
2146 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2147};
2148
2149#define L2DOWNSLLEN \
2150 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2151
2152int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2153{
2154 struct lapd_datalink *dl = lctx->dl;
2155 int i, supported = 0;
2156 struct msgb *msg = dp->oph.msg;
2157 int rc;
2158
2159 /* find function for current state and message */
2160 for (i = 0; i < L2DOWNSLLEN; i++) {
2161 if (dp->oph.primitive == l2downstatelist[i].prim
2162 && dp->oph.operation == l2downstatelist[i].op) {
2163 supported = 1;
2164 if ((SBIT(dl->state) & l2downstatelist[i].states))
2165 break;
2166 }
2167 }
2168 if (!supported) {
2169 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2170 dp->oph.primitive, dp->oph.operation);
2171 msgb_free(msg);
2172 return 0;
2173 }
2174 if (i == L2DOWNSLLEN) {
2175 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2176 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2177 lapd_state_names[dl->state]);
2178 msgb_free(msg);
2179 return 0;
2180 }
2181
2182 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2183 l2downstatelist[i].name, lapd_state_names[dl->state]);
2184
2185 rc = l2downstatelist[i].rout(dp, lctx);
2186
2187 return rc;
2188}
2189
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002190/*! @} */