blob: cdd9b568a295d4db8171c73c3d63602685515d28 [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
Max87218ed2017-01-09 14:24:03 +0100380 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %s\n",
381 rsl_rlm_cause_name(cause));
rootaf48bed2011-09-26 11:23:06 +0200382 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 "
Max87218ed2017-01-09 14:24:03 +0100836 "allowed in state %s\n",
837 lapd_state_names[dl->state]);
Andreas Eversbergccc46332013-06-12 09:25:27 +0200838 mdl_error(MDL_CAUSE_SABM_MF, lctx);
839 msgb_free(msg);
840 return 0;
841 }
rootaf48bed2011-09-26 11:23:06 +0200842 /* Ignore SABM if content differs from first SABM. */
Andreas Eversbergccc46332013-06-12 09:25:27 +0200843 if (dl->mode == LAPD_MODE_NETWORK && length) {
rootaf48bed2011-09-26 11:23:06 +0200844#ifdef TEST_CONTENT_RESOLUTION_NETWORK
845 dl->cont_res->data[0] ^= 0x01;
846#endif
Andreas Eversberg6e182082013-02-06 14:13:21 +0100847 if (memcmp(dl->cont_res->data, msg->data,
848 length)) {
rootaf48bed2011-09-26 11:23:06 +0200849 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
850 "with diffrent content - "
851 "ignoring!\n");
852 msgb_free(msg);
853 return 0;
854 }
855 }
856 /* send UA again */
857 lapd_send_ua(lctx, length, msg->l3h);
858 msgb_free(msg);
859 return 0;
860 case LAPD_STATE_DISC_SENT:
861 /* 5.4.6.2 send DM with F=P */
862 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200863 /* stop Timer T200 */
864 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200865 msgb_free(msg);
866 return send_dl_simple(prim, op, lctx);
867 default:
868 /* collision: Send UA, but still wait for rx UA, then
869 * change to MF_EST state.
870 */
871 /* check for contention resoultion */
872 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
873 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
Max87218ed2017-01-09 14:24:03 +0100874 "during contention resolution (state %s)\n",
875 lapd_state_names[dl->state]);
rootaf48bed2011-09-26 11:23:06 +0200876 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
877 }
878 lapd_send_ua(lctx, length, msg->l3h);
879 msgb_free(msg);
880 return 0;
881 }
882 /* save message context for further use */
883 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
884#ifndef TEST_CONTENT_RESOLUTION_NETWORK
885 /* send UA response */
886 lapd_send_ua(lctx, length, msg->l3h);
887#endif
888 /* set Vs, Vr and Va to 0 */
889 dl->v_send = dl->v_recv = dl->v_ack = 0;
890 /* clear tx_hist */
891 lapd_dl_flush_hist(dl);
892 /* enter multiple-frame-established state */
893 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
894 /* store content resolution data on network side
895 * Note: cont_res will be removed when changing state again,
896 * so it must be allocated AFTER lapd_dl_newstate(). */
897 if (dl->mode == LAPD_MODE_NETWORK && length) {
898 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
899 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
900 length);
901 LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n");
902 }
903 /* send notification to L3 */
904 if (length == 0) {
905 /* 5.4.1.2 Normal establishment procedures */
906 rc = send_dl_simple(prim, op, lctx);
907 msgb_free(msg);
908 } else {
909 /* 5.4.1.4 Contention resolution establishment */
Harald Welte087116a2013-06-18 21:41:34 +0200910 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +0200911 rc = send_dl_l3(prim, op, lctx, msg);
912 }
913 break;
914 case LAPD_U_DM:
915 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
916 lapd_state_names[dl->state]);
917 /* G.2.2 Wrong value of the C/R bit */
918 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100919 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200920 msgb_free(msg);
921 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
922 return -EINVAL;
923 }
924 if (!lctx->p_f) {
925 /* 5.4.1.2 DM responses with the F bit set to "0"
926 * shall be ignored.
927 */
928 msgb_free(msg);
929 return 0;
930 }
931 switch (dl->state) {
932 case LAPD_STATE_SABM_SENT:
933 break;
934 case LAPD_STATE_MF_EST:
935 if (lctx->p_f) {
936 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
937 "response\n");
938 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
939 } else {
940 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
941 "response, multiple frame established "
942 "state\n");
943 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
944 /* reestablish */
945 if (!dl->reestablish) {
946 msgb_free(msg);
947 return 0;
948 }
949 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
950 "reestablishment.\n");
951 lapd_reestablish(dl);
952 }
953 msgb_free(msg);
954 return 0;
955 case LAPD_STATE_TIMER_RECOV:
956 /* FP = 0 (DM is normal in case PF = 1) */
957 if (!lctx->p_f) {
958 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
959 "response, multiple frame established "
960 "state\n");
961 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
962 msgb_free(msg);
963 /* reestablish */
964 if (!dl->reestablish)
965 return 0;
966 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
967 "reestablishment.\n");
968 return lapd_reestablish(dl);
969 }
970 break;
971 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200972 /* stop Timer T200 */
973 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200974 /* go to idle state */
975 lapd_dl_flush_tx(dl);
976 lapd_dl_flush_send(dl);
977 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
978 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
979 msgb_free(msg);
980 return 0;
981 case LAPD_STATE_IDLE:
982 /* 5.4.5 all other frame types shall be discarded */
983 default:
984 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
985 "(discarding)\n");
986 msgb_free(msg);
987 return 0;
988 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200989 /* stop timer T200 */
990 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200991 /* go to idle state */
992 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
993 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
994 msgb_free(msg);
995 break;
996 case LAPD_U_UI:
997 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
998 /* G.2.2 Wrong value of the C/R bit */
999 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001000 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +02001001 "error\n");
1002 msgb_free(msg);
1003 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1004 return -EINVAL;
1005 }
1006
1007 /* G.4.5 If UI is received with L>N201 or with M bit
1008 * set, AN MDL-ERROR-INDICATION is sent to MM.
1009 */
1010 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001011 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +02001012 "(%d > N201(%d) or M=%d)\n", length,
1013 lctx->n201, lctx->more);
1014 msgb_free(msg);
1015 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1016 return -EIO;
1017 }
1018
1019 /* do some length checks */
1020 if (length == 0) {
1021 /* 5.3.3 UI frames received with the length indicator
1022 * set to "0" shall be ignored
1023 */
1024 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1025 msgb_free(msg);
1026 return 0;
1027 }
Harald Welte087116a2013-06-18 21:41:34 +02001028 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001029 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1030 msg);
1031 break;
1032 case LAPD_U_DISC:
1033 prim = PRIM_DL_REL;
1034 op = PRIM_OP_INDICATION;
1035
1036 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1037 lapd_state_names[dl->state]);
1038 /* flush tx and send buffers */
1039 lapd_dl_flush_tx(dl);
1040 lapd_dl_flush_send(dl);
1041 /* 5.7.1 */
1042 dl->seq_err_cond = 0;
1043 /* G.2.2 Wrong value of the C/R bit */
1044 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001045 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001046 msgb_free(msg);
1047 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1048 return -EINVAL;
1049 }
1050 if (length > 0 || lctx->more) {
1051 /* G.4.4 If a DISC or DM frame is received with L>0 or
1052 * with the M bit set to "1", an MDL-ERROR-INDICATION
1053 * primitive with cause "U frame with incorrect
1054 * parameters" is sent to the mobile management entity.
1055 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001056 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001057 "U frame iwth incorrect parameters ");
1058 msgb_free(msg);
1059 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1060 return -EIO;
1061 }
1062 switch (dl->state) {
1063 case LAPD_STATE_IDLE:
1064 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1065 /* send DM with F=P */
1066 msgb_free(msg);
1067 return lapd_send_dm(lctx);
1068 case LAPD_STATE_SABM_SENT:
1069 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1070 /* 5.4.6.2 send DM with F=P */
1071 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001072 /* stop Timer T200 */
1073 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001074 /* go to idle state */
1075 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1076 msgb_free(msg);
1077 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1078 lctx);
1079 case LAPD_STATE_MF_EST:
1080 case LAPD_STATE_TIMER_RECOV:
1081 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1082 break;
1083 case LAPD_STATE_DISC_SENT:
1084 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1085 prim = PRIM_DL_REL;
1086 op = PRIM_OP_CONFIRM;
1087 break;
1088 default:
1089 lapd_send_ua(lctx, length, msg->l3h);
1090 msgb_free(msg);
1091 return 0;
1092 }
1093 /* send UA response */
1094 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001095 /* stop Timer T200 */
1096 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001097 /* enter idle state, keep tx-buffer with UA response */
1098 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1099 /* send notification to L3 */
1100 rc = send_dl_simple(prim, op, lctx);
1101 msgb_free(msg);
1102 break;
1103 case LAPD_U_UA:
1104 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1105 lapd_state_names[dl->state]);
1106 /* G.2.2 Wrong value of the C/R bit */
1107 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001108 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001109 "error\n");
1110 msgb_free(msg);
1111 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1112 return -EINVAL;
1113 }
1114
1115 /* G.4.5 If UA is received with L>N201 or with M bit
1116 * set, AN MDL-ERROR-INDICATION is sent to MM.
1117 */
1118 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001119 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001120 msgb_free(msg);
1121 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1122 return -EIO;
1123 }
1124
1125 if (!lctx->p_f) {
1126 /* 5.4.1.2 A UA response with the F bit set to "0"
1127 * shall be ignored.
1128 */
1129 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1130 msgb_free(msg);
1131 return 0;
1132 }
1133 switch (dl->state) {
1134 case LAPD_STATE_SABM_SENT:
1135 break;
1136 case LAPD_STATE_MF_EST:
1137 case LAPD_STATE_TIMER_RECOV:
1138 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1139 "(discarding)\n");
1140 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1141 msgb_free(msg);
1142 return 0;
1143 case LAPD_STATE_DISC_SENT:
1144 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001145 /* stop Timer T200 */
1146 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001147 /* go to idle state */
1148 lapd_dl_flush_tx(dl);
1149 lapd_dl_flush_send(dl);
1150 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1151 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1152 msgb_free(msg);
1153 return 0;
1154 case LAPD_STATE_IDLE:
1155 /* 5.4.5 all other frame types shall be discarded */
1156 default:
1157 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1158 "(discarding)\n");
1159 msgb_free(msg);
1160 return 0;
1161 }
1162 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001163 /* stop Timer T200 */
1164 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001165 /* compare UA with SABME if contention resolution is applied */
1166 if (dl->tx_hist[0].msg->len) {
1167 if (length != (dl->tx_hist[0].msg->len)
1168 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1169 length)) {
1170 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1171 "mismatches ****\n");
1172 rc = send_dl_simple(PRIM_DL_REL,
1173 PRIM_OP_INDICATION, lctx);
1174 msgb_free(msg);
1175 /* go to idle state */
1176 lapd_dl_flush_tx(dl);
1177 lapd_dl_flush_send(dl);
1178 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1179 return 0;
1180 }
1181 }
1182 /* set Vs, Vr and Va to 0 */
1183 dl->v_send = dl->v_recv = dl->v_ack = 0;
1184 /* clear tx_hist */
1185 lapd_dl_flush_hist(dl);
1186 /* enter multiple-frame-established state */
1187 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1188 /* send outstanding frames, if any (resume / reconnect) */
1189 lapd_send_i(lctx, __LINE__);
1190 /* send notification to L3 */
1191 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1192 msgb_free(msg);
1193 break;
1194 case LAPD_U_FRMR:
1195 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1196 /* send MDL ERROR INIDCATION to L3 */
1197 mdl_error(MDL_CAUSE_FRMR, lctx);
1198 msgb_free(msg);
1199 /* reestablish */
1200 if (!dl->reestablish)
1201 break;
1202 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1203 rc = lapd_reestablish(dl);
1204 break;
1205 default:
1206 /* G.3.1 */
1207 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1208 msgb_free(msg);
1209 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1210 return -EINVAL;
1211 }
1212 return rc;
1213}
1214
1215/* Receive a LAPD S (Supervisory) message from L1 */
1216static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1217{
1218 struct lapd_datalink *dl = lctx->dl;
1219 int length = lctx->length;
1220
1221 if (length > 0 || lctx->more) {
1222 /* G.4.3 If a supervisory frame is received with L>0 or
1223 * with the M bit set to "1", an MDL-ERROR-INDICATION
1224 * primitive with cause "S frame with incorrect
1225 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001226 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001227 "S frame with incorrect parameters\n");
1228 msgb_free(msg);
1229 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1230 return -EIO;
1231 }
1232
1233 if (lctx->cr == dl->cr.rem2loc.resp
1234 && lctx->p_f
1235 && dl->state != LAPD_STATE_TIMER_RECOV) {
1236 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1237 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1238 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1239 }
1240
1241 switch (dl->state) {
1242 case LAPD_STATE_IDLE:
1243 /* if P=1, respond DM with F=1 (5.2.2) */
1244 /* 5.4.5 all other frame types shall be discarded */
1245 if (lctx->p_f)
1246 lapd_send_dm(lctx); /* F=P */
1247 /* fall though */
1248 case LAPD_STATE_SABM_SENT:
1249 case LAPD_STATE_DISC_SENT:
1250 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1251 msgb_free(msg);
1252 return 0;
1253 }
1254 switch (lctx->s_u) {
1255 case LAPD_S_RR:
1256 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1257 lapd_state_names[dl->state]);
1258 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1259 lapd_acknowledge(lctx);
1260
1261 /* 5.5.3.2 */
1262 if (lctx->cr == dl->cr.rem2loc.cmd
1263 && lctx->p_f) {
1264 if (!dl->own_busy && !dl->seq_err_cond) {
1265 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1266 "with polling bit set and we are not "
1267 "busy, so we reply with RR frame "
1268 "response\n");
1269 lapd_send_rr(lctx, 1, 0);
1270 /* NOTE: In case of sequence error condition,
1271 * the REJ frame has been transmitted when
1272 * entering the condition, so it has not be
1273 * done here
1274 */
1275 } else if (dl->own_busy) {
1276 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1277 "with polling bit set and we are busy, "
1278 "so we reply with RR frame response\n");
1279 lapd_send_rnr(lctx, 1, 0);
1280 }
1281 } else if (lctx->cr == dl->cr.rem2loc.resp
1282 && lctx->p_f
1283 && dl->state == LAPD_STATE_TIMER_RECOV) {
1284 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1285 "and we are in timer recovery state, so "
1286 "we leave that state\n");
1287 /* V(S) to the N(R) in the RR frame */
1288 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001289 /* stop Timer T200 */
1290 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001291 /* 5.5.7 Clear timer recovery condition */
1292 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1293 }
1294 /* Send message, if possible due to acknowledged data */
1295 lapd_send_i(lctx, __LINE__);
1296
1297 break;
1298 case LAPD_S_RNR:
1299 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1300 lapd_state_names[dl->state]);
1301 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1302 lapd_acknowledge(lctx);
1303
1304 /* 5.5.5 */
1305 /* Set peer receiver busy condition */
1306 dl->peer_busy = 1;
1307
1308 if (lctx->p_f) {
1309 if (lctx->cr == dl->cr.rem2loc.cmd) {
1310 if (!dl->own_busy) {
1311 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1312 "command and we are not busy, "
1313 "so we reply with RR final "
1314 "response\n");
1315 /* Send RR with F=1 */
1316 lapd_send_rr(lctx, 1, 0);
1317 } else {
1318 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1319 "command and we are busy, so "
1320 "we reply with RNR final "
1321 "response\n");
1322 /* Send RNR with F=1 */
1323 lapd_send_rnr(lctx, 1, 0);
1324 }
1325 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1326 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1327 "and we in timer recovery state, so "
1328 "we leave that state\n");
1329 /* 5.5.7 Clear timer recovery condition */
1330 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1331 /* V(S) to the N(R) in the RNR frame */
1332 dl->v_send = lctx->n_recv;
1333 }
1334 } else
1335 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1336 "received\n");
1337
1338 /* Send message, if possible due to acknowledged data */
1339 lapd_send_i(lctx, __LINE__);
1340
1341 break;
1342 case LAPD_S_REJ:
1343 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1344 lapd_state_names[dl->state]);
1345 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1346 lapd_acknowledge(lctx);
1347
1348 /* 5.5.4.1 */
1349 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1350 /* Clear an existing peer receiver busy condition */
1351 dl->peer_busy = 0;
1352 /* V(S) and V(A) to the N(R) in the REJ frame */
1353 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001354 /* stop Timer T200 */
1355 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001356 /* 5.5.3.2 */
1357 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1358 if (!dl->own_busy && !dl->seq_err_cond) {
1359 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1360 "command not in timer recovery "
1361 "state and not in own busy "
1362 "condition received, so we "
1363 "respond with RR final "
1364 "response\n");
1365 lapd_send_rr(lctx, 1, 0);
1366 /* NOTE: In case of sequence error
1367 * condition, the REJ frame has been
1368 * transmitted when entering the
1369 * condition, so it has not be done
1370 * here
1371 */
1372 } else if (dl->own_busy) {
1373 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1374 "command not in timer recovery "
1375 "state and in own busy "
1376 "condition received, so we "
1377 "respond with RNR final "
1378 "response\n");
1379 lapd_send_rnr(lctx, 1, 0);
1380 }
1381 } else
1382 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1383 "polling command not in timer recovery "
1384 "state received\n");
1385 /* send MDL ERROR INIDCATION to L3 */
1386 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1387 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1388 }
1389
1390 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1391 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1392 "recovery state received\n");
1393 /* Clear an existing peer receiver busy condition */
1394 dl->peer_busy = 0;
1395 /* V(S) and V(A) to the N(R) in the REJ frame */
1396 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001397 /* stop Timer T200 */
1398 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001399 /* 5.5.7 Clear timer recovery condition */
1400 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1401 } else {
1402 /* Clear an existing peer receiver busy condition */
1403 dl->peer_busy = 0;
1404 /* V(S) and V(A) to the N(R) in the REJ frame */
1405 dl->v_send = dl->v_ack = lctx->n_recv;
1406 /* 5.5.3.2 */
1407 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1408 if (!dl->own_busy && !dl->seq_err_cond) {
1409 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1410 "command in timer recovery "
1411 "state and not in own busy "
1412 "condition received, so we "
1413 "respond with RR final "
1414 "response\n");
1415 lapd_send_rr(lctx, 1, 0);
1416 /* NOTE: In case of sequence error
1417 * condition, the REJ frame has been
1418 * transmitted when entering the
1419 * condition, so it has not be done
1420 * here
1421 */
1422 } else if (dl->own_busy) {
1423 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1424 "command in timer recovery "
1425 "state and in own busy "
1426 "condition received, so we "
1427 "respond with RNR final "
1428 "response\n");
1429 lapd_send_rnr(lctx, 1, 0);
1430 }
1431 } else
1432 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1433 "polling command in timer recovery "
1434 "state received\n");
1435 }
1436
1437 /* FIXME: 5.5.4.2 2) */
1438
1439 /* Send message, if possible due to acknowledged data */
1440 lapd_send_i(lctx, __LINE__);
1441
1442 break;
1443 default:
1444 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001445 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001446 msgb_free(msg);
1447 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1448 return -EINVAL;
1449 }
1450 msgb_free(msg);
1451 return 0;
1452}
1453
1454/* Receive a LAPD I (Information) message from L1 */
1455static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1456{
1457 struct lapd_datalink *dl = lctx->dl;
1458 //uint8_t nr = lctx->n_recv;
1459 uint8_t ns = lctx->n_send;
1460 int length = lctx->length;
1461 int rc;
1462
Holger Hans Peter Freyther1512ea62014-03-16 23:59:58 +01001463 LOGP(DLLAPD, LOGL_INFO, "I received in state %s on SAPI(%u)\n",
1464 lapd_state_names[dl->state], lctx->sapi);
rootaf48bed2011-09-26 11:23:06 +02001465
1466 /* G.2.2 Wrong value of the C/R bit */
1467 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001468 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001469 msgb_free(msg);
1470 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1471 return -EINVAL;
1472 }
1473
1474 if (length == 0 || length > lctx->n201) {
1475 /* G.4.2 If the length indicator of an I frame is set
1476 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1477 * primitive with cause "I frame with incorrect length"
1478 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001479 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001480 msgb_free(msg);
1481 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1482 return -EIO;
1483 }
1484
1485 /* G.4.2 If the numerical value of L is L<N201 and the M
1486 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1487 * cause "I frame with incorrect use of M bit" is sent to the
1488 * mobile management entity. */
1489 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001490 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001491 msgb_free(msg);
1492 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1493 return -EIO;
1494 }
1495
1496 switch (dl->state) {
1497 case LAPD_STATE_IDLE:
1498 /* if P=1, respond DM with F=1 (5.2.2) */
1499 /* 5.4.5 all other frame types shall be discarded */
1500 if (lctx->p_f)
1501 lapd_send_dm(lctx); /* F=P */
1502 /* fall though */
1503 case LAPD_STATE_SABM_SENT:
1504 case LAPD_STATE_DISC_SENT:
1505 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1506 msgb_free(msg);
1507 return 0;
1508 }
1509
1510 /* 5.7.1: N(s) sequence error */
1511 if (ns != dl->v_recv) {
1512 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1513 "V(R)=%u\n", ns, dl->v_recv);
1514 /* discard data */
1515 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001516 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001517 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001518 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001519 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001520 lapd_send_rej(lctx, lctx->p_f);
1521 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001522 /* If there are two subsequent sequence errors received,
1523 * ignore it. (Ignore every second subsequent error.)
1524 * This happens if our reply with the REJ is too slow,
1525 * so the remote gets a T200 timeout and sends another
1526 * frame with a sequence error.
1527 * Test showed that replying with two subsequent REJ
1528 * messages could the remote L2 process to abort.
1529 * Replying too slow shouldn't happen, but may happen
1530 * over serial link between BB and LAPD.
1531 */
1532 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001533 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001534 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1535 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1536 lapd_acknowledge(lctx); /* V(A) is also set here */
1537
1538 /* Send message, if possible due to acknowledged data */
1539 lapd_send_i(lctx, __LINE__);
1540
1541 return 0;
rootaf48bed2011-09-26 11:23:06 +02001542 }
1543 dl->seq_err_cond = 0;
1544
1545 /* Increment receiver state */
1546 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1547 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1548
1549 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1550 lapd_acknowledge(lctx); /* V(A) is also set here */
1551
1552 /* Only if we are not in own receiver busy condition */
1553 if (!dl->own_busy) {
1554 /* if the frame carries a complete segment */
1555 if (!lctx->more && !dl->rcv_buffer) {
1556 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1557 /* send a DATA INDICATION to L3 */
Harald Welte087116a2013-06-18 21:41:34 +02001558 msgb_trim(msg, length);
rootaf48bed2011-09-26 11:23:06 +02001559 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1560 msg);
1561 } else {
1562 /* create rcv_buffer */
1563 if (!dl->rcv_buffer) {
1564 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1565 "I frames (first message)\n");
1566 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1567 "LAPD RX");
1568 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1569 }
1570 /* concat. rcv_buffer */
1571 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1572 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1573 "overflow!\n");
1574 } else {
1575 memcpy(msgb_put(dl->rcv_buffer, length),
1576 msg->l3h, length);
1577 }
1578 /* if the last segment was received */
1579 if (!lctx->more) {
1580 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1581 "I frames (last message)\n");
1582 rc = send_dl_l3(PRIM_DL_DATA,
1583 PRIM_OP_INDICATION, lctx,
1584 dl->rcv_buffer);
1585 dl->rcv_buffer = NULL;
1586 } else
1587 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1588 "I frames (next message)\n");
1589 msgb_free(msg);
1590
1591 }
1592 } else
1593 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1594 "busy condition\n");
1595
1596 /* Check for P bit */
1597 if (lctx->p_f) {
1598 /* 5.5.2.1 */
1599 /* check if we are not in own receiver busy */
1600 if (!dl->own_busy) {
1601 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1602 /* Send RR with F=1 */
1603 rc = lapd_send_rr(lctx, 1, 0);
1604 } else {
1605 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1606 /* Send RNR with F=1 */
1607 rc = lapd_send_rnr(lctx, 1, 0);
1608 }
1609 } else {
1610 /* 5.5.2.2 */
1611 /* check if we are not in own receiver busy */
1612 if (!dl->own_busy) {
1613 /* NOTE: V(R) is already set above */
1614 rc = lapd_send_i(lctx, __LINE__);
Daniel Willmann3dc4e162014-03-20 19:24:48 +01001615
1616 /* if update_pending_iframe returns 0 it updated
1617 * the lapd header of an iframe in the tx queue */
1618 if (rc && dl->update_pending_frames)
1619 rc = dl->update_pending_frames(lctx);
1620
rootaf48bed2011-09-26 11:23:06 +02001621 if (rc) {
1622 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1623 "have no pending data, send RR\n");
1624 /* Send RR with F=0 */
1625 return lapd_send_rr(lctx, 0, 0);
1626 }
1627 /* all I or one RR is sent, we are done */
1628 return 0;
1629 } else {
1630 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1631 /* Send RNR with F=0 */
1632 rc = lapd_send_rnr(lctx, 0, 0);
1633 }
1634 }
1635
1636 /* Send message, if possible due to acknowledged data */
1637 lapd_send_i(lctx, __LINE__);
1638
1639 return rc;
1640}
1641
1642/* Receive a LAPD message from L1 */
1643int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1644{
1645 int rc;
1646
1647 switch (lctx->format) {
1648 case LAPD_FORM_U:
1649 rc = lapd_rx_u(msg, lctx);
1650 break;
1651 case LAPD_FORM_S:
1652 rc = lapd_rx_s(msg, lctx);
1653 break;
1654 case LAPD_FORM_I:
1655 rc = lapd_rx_i(msg, lctx);
1656 break;
1657 default:
1658 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1659 msgb_free(msg);
1660 rc = -EINVAL;
1661 }
1662 return rc;
1663}
1664
1665/* L3 -> L2 */
1666
1667/* send unit data */
1668static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1669{
1670 struct lapd_datalink *dl = lctx->dl;
1671 struct msgb *msg = dp->oph.msg;
1672 struct lapd_msg_ctx nctx;
1673
1674 memcpy(&nctx, lctx, sizeof(nctx));
1675 /* keep nctx.ldp */
1676 /* keep nctx.sapi */
1677 /* keep nctx.tei */
1678 nctx.cr = dl->cr.loc2rem.cmd;
1679 nctx.format = LAPD_FORM_U;
1680 nctx.s_u = LAPD_U_UI;
1681 /* keep nctx.p_f */
1682 nctx.length = msg->len;
1683 nctx.more = 0;
1684
1685 return dl->send_ph_data_req(&nctx, msg);
1686}
1687
1688/* request link establishment */
1689static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1690{
1691 struct lapd_datalink *dl = lctx->dl;
1692 struct msgb *msg = dp->oph.msg;
1693 struct lapd_msg_ctx nctx;
1694
1695 if (msg->len)
1696 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1697 "(SABM)\n");
1698 else
1699 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1700
1701 /* Flush send-queue */
1702 /* Clear send-buffer */
1703 lapd_dl_flush_send(dl);
1704 /* be sure that history is empty */
1705 lapd_dl_flush_hist(dl);
1706
1707 /* save message context for further use */
1708 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1709
1710 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001711 msgb_free(dl->rcv_buffer);
1712 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001713
1714 /* assemble message */
1715 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1716 /* keep nctx.ldp */
1717 /* keep nctx.sapi */
1718 /* keep nctx.tei */
1719 nctx.cr = dl->cr.loc2rem.cmd;
1720 nctx.format = LAPD_FORM_U;
1721 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1722 nctx.p_f = 1;
1723 nctx.length = msg->len;
1724 nctx.more = 0;
1725
1726 /* Transmit-buffer carries exactly one segment */
1727 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1728 msgb_put(dl->tx_hist[0].msg, msg->len);
1729 if (msg->len)
1730 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1731 dl->tx_hist[0].more = 0;
1732 /* set Vs to 0, because it is used as index when resending SABM */
1733 dl->v_send = 0;
1734
1735 /* Set states */
1736 dl->own_busy = dl->peer_busy = 0;
1737 dl->retrans_ctr = 0;
1738 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1739
1740 /* Tramsmit and start T200 */
1741 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001742 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001743
1744 return 0;
1745}
1746
1747/* send data */
1748static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1749{
1750 struct lapd_datalink *dl = lctx->dl;
1751 struct msgb *msg = dp->oph.msg;
1752
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001753 if (msgb_l3len(msg) == 0) {
1754 LOGP(DLLAPD, LOGL_ERROR,
1755 "writing an empty message is not possible.\n");
1756 msgb_free(msg);
1757 return -1;
1758 }
1759
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001760 LOGP(DLLAPD, LOGL_INFO,
1761 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001762
1763 /* Write data into the send queue */
1764 msgb_enqueue(&dl->send_queue, msg);
1765
1766 /* Send message, if possible */
1767 lapd_send_i(&dl->lctx, __LINE__);
1768
1769 return 0;
1770}
1771
1772/* Send next I frame from queued/buffered data */
1773static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1774{
1775 struct lapd_datalink *dl = lctx->dl;
1776 uint8_t k = dl->k;
1777 uint8_t h;
1778 struct msgb *msg;
1779 int length, left;
1780 int rc = - 1; /* we sent nothing */
1781 struct lapd_msg_ctx nctx;
1782
1783
1784 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1785
1786 next_frame:
1787
1788 if (dl->peer_busy) {
1789 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1790 return rc;
1791 }
1792
1793 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1794 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1795 return rc;
1796 }
1797
1798 /* If the send state variable V(S) is equal to V(A) plus k
1799 * (where k is the maximum number of outstanding I frames - see
1800 * subclause 5.8.4), the data link layer entity shall not transmit any
1801 * new I frames, but shall retransmit an I frame as a result
1802 * of the error recovery procedures as described in subclauses 5.5.4 and
1803 * 5.5.7. */
1804 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1805 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1806 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1807 dl->v_ack);
1808 return rc;
1809 }
1810
1811 h = do_mod(dl->v_send, dl->range_hist);
1812
1813 /* if we have no tx_hist yet, we create it */
1814 if (!dl->tx_hist[h].msg) {
1815 /* Get next message into send-buffer, if any */
1816 if (!dl->send_buffer) {
1817 next_message:
1818 dl->send_out = 0;
1819 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1820 /* No more data to be sent */
1821 if (!dl->send_buffer)
1822 return rc;
1823 LOGP(DLLAPD, LOGL_INFO, "get message from "
1824 "send-queue\n");
1825 }
1826
1827 /* How much is left in the send-buffer? */
1828 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1829 /* Segment, if data exceeds N201 */
1830 length = left;
1831 if (length > lctx->n201)
1832 length = lctx->n201;
1833 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1834 "length %d first byte %02x\n",
1835 msgb_l3len(dl->send_buffer), dl->send_out, left,
1836 lctx->n201, length, dl->send_buffer->l3h[0]);
1837 /* If message in send-buffer is completely sent */
1838 if (left == 0) {
1839 msgb_free(dl->send_buffer);
1840 dl->send_buffer = NULL;
1841 goto next_message;
1842 }
1843
1844 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1845 (left > length) ? "segment " : "", dl->v_send);
1846
1847 /* Create I frame (segment) and transmit-buffer content */
1848 msg = lapd_msgb_alloc(length, "LAPD I");
1849 msg->l3h = msgb_put(msg, length);
1850 /* assemble message */
1851 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1852 /* keep nctx.ldp */
1853 /* keep nctx.sapi */
1854 /* keep nctx.tei */
1855 nctx.cr = dl->cr.loc2rem.cmd;
1856 nctx.format = LAPD_FORM_I;
1857 nctx.p_f = 0;
1858 nctx.n_send = dl->v_send;
1859 nctx.n_recv = dl->v_recv;
1860 nctx.length = length;
1861 if (left > length)
1862 nctx.more = 1;
1863 else
1864 nctx.more = 0;
1865 if (length)
1866 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1867 length);
1868 /* store in tx_hist */
1869 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1870 msgb_put(dl->tx_hist[h].msg, msg->len);
1871 if (length)
1872 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1873 dl->tx_hist[h].more = nctx.more;
1874 /* Add length to track how much is already in the tx buffer */
1875 dl->send_out += length;
1876 } else {
1877 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1878 "V(S)=%d\n", dl->v_send);
1879
1880 /* Create I frame (segment) from tx_hist */
1881 length = dl->tx_hist[h].msg->len;
1882 msg = lapd_msgb_alloc(length, "LAPD I resend");
1883 msg->l3h = msgb_put(msg, length);
1884 /* assemble message */
1885 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1886 /* keep nctx.ldp */
1887 /* keep nctx.sapi */
1888 /* keep nctx.tei */
1889 nctx.cr = dl->cr.loc2rem.cmd;
1890 nctx.format = LAPD_FORM_I;
1891 nctx.p_f = 0;
1892 nctx.n_send = dl->v_send;
1893 nctx.n_recv = dl->v_recv;
1894 nctx.length = length;
1895 nctx.more = dl->tx_hist[h].more;
1896 if (length)
1897 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1898 }
1899
1900 /* The value of the send state variable V(S) shall be incremented by 1
1901 * at the end of the transmission of the I frame */
1902 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1903
1904 /* If timer T200 is not running at the time right before transmitting a
1905 * frame, when the PH-READY-TO-SEND primitive is received from the
1906 * physical layer., it shall be set. */
1907 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001908 /* stop Timer T203, if running */
1909 lapd_stop_t203(dl);
1910 /* start Timer T200 */
1911 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001912 }
1913
1914 dl->send_ph_data_req(&nctx, msg);
1915
1916 rc = 0; /* we sent something */
1917 goto next_frame;
1918}
1919
1920/* request link suspension */
1921static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1922{
1923 struct lapd_datalink *dl = lctx->dl;
1924 struct msgb *msg = dp->oph.msg;
1925
1926 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1927
1928 /* put back the send-buffer to the send-queue (first position) */
1929 if (dl->send_buffer) {
1930 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1931 "queue\n");
1932 llist_add(&dl->send_buffer->list, &dl->send_queue);
1933 dl->send_buffer = NULL;
1934 } else
1935 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1936
1937 /* Clear transmit buffer, but keep send buffer */
1938 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001939 /* Stop timers (there is no state change, so we must stop all timers */
1940 lapd_stop_t200(dl);
1941 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001942
1943 msgb_free(msg);
1944
1945 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1946}
1947
Neels Hofmeyr9e57a5a2015-12-21 11:20:14 +01001948/* request, resume or reconnect of link */
rootaf48bed2011-09-26 11:23:06 +02001949static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1950{
1951 struct lapd_datalink *dl = lctx->dl;
1952 struct msgb *msg = dp->oph.msg;
1953 struct lapd_msg_ctx nctx;
1954
1955 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1956 msg->len);
1957
1958 /* be sure that history is empty */
1959 lapd_dl_flush_hist(dl);
1960
1961 /* save message context for further use */
1962 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1963
1964 /* Replace message in the send-buffer (reconnect) */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001965 msgb_free(dl->send_buffer);
1966 dl->send_buffer = NULL;
1967
rootaf48bed2011-09-26 11:23:06 +02001968 dl->send_out = 0;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001969 if (msg->len) {
rootaf48bed2011-09-26 11:23:06 +02001970 /* Write data into the send buffer, to be sent first */
1971 dl->send_buffer = msg;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001972 } else {
1973 msgb_free(msg);
1974 msg = NULL;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001975 dl->send_buffer = NULL;
Andreas Eversbergcad54b82013-07-09 20:25:24 +02001976 }
rootaf48bed2011-09-26 11:23:06 +02001977
1978 /* Discard partly received L3 message */
Holger Hans Peter Freyther9b037a62013-07-11 08:17:02 +02001979 msgb_free(dl->rcv_buffer);
1980 dl->rcv_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001981
1982 /* Create new msgb (old one is now free) */
1983 msg = lapd_msgb_alloc(0, "LAPD SABM");
1984 msg->l3h = msg->data;
1985 /* assemble message */
1986 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1987 /* keep nctx.ldp */
1988 /* keep nctx.sapi */
1989 /* keep nctx.tei */
1990 nctx.cr = dl->cr.loc2rem.cmd;
1991 nctx.format = LAPD_FORM_U;
1992 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1993 nctx.p_f = 1;
1994 nctx.length = 0;
1995 nctx.more = 0;
1996
1997 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1998 msgb_put(dl->tx_hist[0].msg, msg->len);
1999 if (msg->len)
2000 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2001 dl->tx_hist[0].more = 0;
2002 /* set Vs to 0, because it is used as index when resending SABM */
2003 dl->v_send = 0;
2004
2005 /* Set states */
2006 dl->own_busy = dl->peer_busy = 0;
2007 dl->retrans_ctr = 0;
2008 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
2009
2010 /* Tramsmit and start T200 */
2011 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002012 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002013
2014 return 0;
2015}
2016
2017/* requesst release of link */
2018static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2019{
2020 struct lapd_datalink *dl = lctx->dl;
2021 struct msgb *msg = dp->oph.msg;
2022 struct lapd_msg_ctx nctx;
2023
2024 /* local release */
2025 if (dp->u.rel_req.mode) {
2026 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2027 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002028 /* stop Timer T200 */
2029 lapd_stop_t200(dl);
2030 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002031 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2032 /* flush buffers */
2033 lapd_dl_flush_tx(dl);
2034 lapd_dl_flush_send(dl);
2035 /* send notification to L3 */
2036 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2037 }
2038
2039 /* in case we are already disconnecting */
2040 if (dl->state == LAPD_STATE_DISC_SENT)
2041 return -EBUSY;
2042
2043 /* flush tx_hist */
2044 lapd_dl_flush_hist(dl);
2045
2046 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2047
2048 /* Push LAPD header on msgb */
2049 /* assemble message */
2050 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2051 /* keep nctx.ldp */
2052 /* keep nctx.sapi */
2053 /* keep nctx.tei */
2054 nctx.cr = dl->cr.loc2rem.cmd;
2055 nctx.format = LAPD_FORM_U;
2056 nctx.s_u = LAPD_U_DISC;
2057 nctx.p_f = 1;
2058 nctx.length = 0;
2059 nctx.more = 0;
2060
2061 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2062 msgb_put(dl->tx_hist[0].msg, msg->len);
2063 if (msg->len)
2064 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2065 dl->tx_hist[0].more = 0;
2066 /* set Vs to 0, because it is used as index when resending DISC */
2067 dl->v_send = 0;
2068
2069 /* Set states */
2070 dl->own_busy = dl->peer_busy = 0;
2071 dl->retrans_ctr = 0;
2072 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2073
2074 /* Tramsmit and start T200 */
2075 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002076 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002077
2078 return 0;
2079}
2080
2081/* request release of link in idle state */
2082static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2083 struct lapd_msg_ctx *lctx)
2084{
2085 struct lapd_datalink *dl = lctx->dl;
2086 struct msgb *msg = dp->oph.msg;
2087
2088 msgb_free(msg);
2089
2090 /* send notification to L3 */
2091 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2092}
2093
2094/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002095static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002096 uint32_t states;
2097 int prim, op;
2098 const char *name;
2099 int (*rout) (struct osmo_dlsap_prim *dp,
2100 struct lapd_msg_ctx *lctx);
2101} l2downstatelist[] = {
2102 /* create and send UI command */
2103 {ALL_STATES,
2104 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2105 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2106
2107 /* create and send SABM command */
2108 {SBIT(LAPD_STATE_IDLE),
2109 PRIM_DL_EST, PRIM_OP_REQUEST,
2110 "DL-ESTABLISH-REQUEST", lapd_est_req},
2111
2112 /* create and send I command */
2113 {SBIT(LAPD_STATE_MF_EST) |
2114 SBIT(LAPD_STATE_TIMER_RECOV),
2115 PRIM_DL_DATA, PRIM_OP_REQUEST,
2116 "DL-DATA-REQUEST", lapd_data_req},
2117
2118 /* suspend datalink */
2119 {SBIT(LAPD_STATE_MF_EST) |
2120 SBIT(LAPD_STATE_TIMER_RECOV),
2121 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2122 "DL-SUSPEND-REQUEST", lapd_susp_req},
2123
2124 /* create and send SABM command (resume) */
2125 {SBIT(LAPD_STATE_MF_EST) |
2126 SBIT(LAPD_STATE_TIMER_RECOV),
2127 PRIM_DL_RES, PRIM_OP_REQUEST,
2128 "DL-RESUME-REQUEST", lapd_res_req},
2129
2130 /* create and send SABM command (reconnect) */
2131 {SBIT(LAPD_STATE_IDLE) |
2132 SBIT(LAPD_STATE_MF_EST) |
2133 SBIT(LAPD_STATE_TIMER_RECOV),
2134 PRIM_DL_RECON, PRIM_OP_REQUEST,
2135 "DL-RECONNECT-REQUEST", lapd_res_req},
2136
2137 /* create and send DISC command */
2138 {SBIT(LAPD_STATE_SABM_SENT) |
2139 SBIT(LAPD_STATE_MF_EST) |
2140 SBIT(LAPD_STATE_TIMER_RECOV) |
2141 SBIT(LAPD_STATE_DISC_SENT),
2142 PRIM_DL_REL, PRIM_OP_REQUEST,
2143 "DL-RELEASE-REQUEST", lapd_rel_req},
2144
2145 /* release in idle state */
2146 {SBIT(LAPD_STATE_IDLE),
2147 PRIM_DL_REL, PRIM_OP_REQUEST,
2148 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2149};
2150
2151#define L2DOWNSLLEN \
2152 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2153
2154int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2155{
2156 struct lapd_datalink *dl = lctx->dl;
2157 int i, supported = 0;
2158 struct msgb *msg = dp->oph.msg;
2159 int rc;
2160
2161 /* find function for current state and message */
2162 for (i = 0; i < L2DOWNSLLEN; i++) {
2163 if (dp->oph.primitive == l2downstatelist[i].prim
2164 && dp->oph.operation == l2downstatelist[i].op) {
2165 supported = 1;
2166 if ((SBIT(dl->state) & l2downstatelist[i].states))
2167 break;
2168 }
2169 }
2170 if (!supported) {
2171 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2172 dp->oph.primitive, dp->oph.operation);
2173 msgb_free(msg);
2174 return 0;
2175 }
2176 if (i == L2DOWNSLLEN) {
2177 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2178 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2179 lapd_state_names[dl->state]);
2180 msgb_free(msg);
2181 return 0;
2182 }
2183
2184 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2185 l2downstatelist[i].name, lapd_state_names[dl->state]);
2186
2187 rc = l2downstatelist[i].rout(dp, lctx);
2188
2189 return rc;
2190}
2191
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +01002192/*! @} */