blob: 0dc78ebb2755bed78e652c51c4ba1af5e60ea7bb [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
28/*! \file lapd.c */
29
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 */
153 if (dl->send_buffer) {
154 msgb_free(dl->send_buffer);
155 dl->send_buffer = NULL;
156 }
157}
158
159static void lapd_dl_flush_hist(struct lapd_datalink *dl)
160{
161 unsigned int i;
162
163 for (i = 0; i < dl->range_hist; i++) {
164 if (dl->tx_hist[i].msg) {
165 msgb_free(dl->tx_hist[i].msg);
166 dl->tx_hist[i].msg = NULL;
167 }
168 }
169}
170
171static void lapd_dl_flush_tx(struct lapd_datalink *dl)
172{
173 struct msgb *msg;
174
175 while ((msg = msgb_dequeue(&dl->tx_queue)))
176 msgb_free(msg);
177 lapd_dl_flush_hist(dl);
178}
179
180/* Figure B.2/Q.921 */
181const char *lapd_state_names[] = {
182 "LAPD_STATE_NULL",
183 "LAPD_STATE_TEI_UNASS",
184 "LAPD_STATE_ASS_TEI_WAIT",
185 "LAPD_STATE_EST_TEI_WAIT",
186 "LAPD_STATE_IDLE",
187 "LAPD_STATE_SABM_SENT",
188 "LAPD_STATE_DISC_SENT",
189 "LAPD_STATE_MF_EST",
190 "LAPD_STATE_TIMER_RECOV",
191
192};
193
Andreas Eversberg742fc792011-09-27 09:40:25 +0200194static void lapd_start_t200(struct lapd_datalink *dl)
195{
196 if (osmo_timer_pending(&dl->t200))
197 return;
198 LOGP(DLLAPD, LOGL_INFO, "start T200\n");
199 osmo_timer_schedule(&dl->t200, dl->t200_sec, dl->t200_usec);
200}
201
202static void lapd_start_t203(struct lapd_datalink *dl)
203{
204 if (osmo_timer_pending(&dl->t203))
205 return;
206 LOGP(DLLAPD, LOGL_INFO, "start T203\n");
207 osmo_timer_schedule(&dl->t203, dl->t203_sec, dl->t203_usec);
208}
209
210static void lapd_stop_t200(struct lapd_datalink *dl)
211{
212 if (!osmo_timer_pending(&dl->t200))
213 return;
214 LOGP(DLLAPD, LOGL_INFO, "stop T200\n");
215 osmo_timer_del(&dl->t200);
216}
217
218static void lapd_stop_t203(struct lapd_datalink *dl)
219{
220 if (!osmo_timer_pending(&dl->t203))
221 return;
222 LOGP(DLLAPD, LOGL_INFO, "stop T203\n");
223 osmo_timer_del(&dl->t203);
224}
225
rootaf48bed2011-09-26 11:23:06 +0200226static void lapd_dl_newstate(struct lapd_datalink *dl, uint32_t state)
227{
228 LOGP(DLLAPD, LOGL_INFO, "new state %s -> %s\n",
229 lapd_state_names[dl->state], lapd_state_names[state]);
230
231 if (state != LAPD_STATE_MF_EST && dl->state == LAPD_STATE_MF_EST) {
232 /* stop T203 on leaving MF EST state, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200233 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200234 /* remove content res. (network side) on leaving MF EST state */
235 if (dl->cont_res) {
236 msgb_free(dl->cont_res);
237 dl->cont_res = NULL;
238 }
239 }
240
241 /* start T203 on entering MF EST state, if enabled */
242 if ((dl->t203_sec || dl->t203_usec)
Andreas Eversberg742fc792011-09-27 09:40:25 +0200243 && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST)
244 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200245
246 dl->state = state;
247}
248
rootaf48bed2011-09-26 11:23:06 +0200249static void *tall_lapd_ctx = NULL;
250
251/* init datalink instance and allocate history */
252void lapd_dl_init(struct lapd_datalink *dl, uint8_t k, uint8_t v_range,
253 int maxf)
254{
255 int m;
256
257 memset(dl, 0, sizeof(*dl));
258 INIT_LLIST_HEAD(&dl->send_queue);
259 INIT_LLIST_HEAD(&dl->tx_queue);
260 dl->reestablish = 1;
261 dl->n200_est_rel = 3;
262 dl->n200 = 3;
263 dl->t200_sec = 1;
264 dl->t200_usec = 0;
265 dl->t200.data = dl;
266 dl->t200.cb = &lapd_t200_cb;
267 dl->t203_sec = 10;
268 dl->t203_usec = 0;
269 dl->t203.data = dl;
270 dl->t203.cb = &lapd_t203_cb;
271 dl->maxf = maxf;
272 if (k > v_range - 1)
273 k = v_range - 1;
274 dl->k = k;
275 dl->v_range = v_range;
276
277 /* Calculate modulo for history array:
278 * - The history range must be at least k+1.
279 * - The history range must be 2^x, where x is as low as possible.
280 */
281 k++;
282 for (m = 0x80; m; m >>= 1) {
283 if ((m & k)) {
284 if (k > m)
285 m <<= 1;
286 dl->range_hist = m;
287 break;
288 }
289 }
290
291 LOGP(DLLAPD, LOGL_INFO, "Init DL layer: sequence range = %d, k = %d, "
292 "history range = %d\n", dl->v_range, dl->k, dl->range_hist);
293
294 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
295
296 if (!tall_lapd_ctx)
297 tall_lapd_ctx = talloc_named_const(NULL, 1, "lapd context");
298 dl->tx_hist = (struct lapd_history *) talloc_zero_array(tall_lapd_ctx,
299 struct log_info, dl->range_hist);
300}
301
302/* reset to IDLE state */
303void lapd_dl_reset(struct lapd_datalink *dl)
304{
305 if (dl->state == LAPD_STATE_IDLE)
306 return;
307 LOGP(DLLAPD, LOGL_INFO, "Resetting LAPDm instance\n");
308 /* enter idle state (and remove eventual cont_res) */
309 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
310 /* flush buffer */
311 lapd_dl_flush_tx(dl);
312 lapd_dl_flush_send(dl);
313 /* Discard partly received L3 message */
314 if (dl->rcv_buffer) {
315 msgb_free(dl->rcv_buffer);
316 dl->rcv_buffer = NULL;
317 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200318 /* stop Timers */
319 lapd_stop_t200(dl);
320 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200321}
322
323/* reset and de-allocate history buffer */
324void lapd_dl_exit(struct lapd_datalink *dl)
325{
326 /* free all ressources except history buffer */
327 lapd_dl_reset(dl);
328 /* free history buffer list */
329 talloc_free(dl->tx_hist);
330}
331
332/*! \brief Set the \ref lapdm_mode of a LAPDm entity */
333int lapd_set_mode(struct lapd_datalink *dl, enum lapd_mode mode)
334{
335 switch (mode) {
336 case LAPD_MODE_USER:
337 dl->cr.loc2rem.cmd = CR_USER2NET_CMD;
338 dl->cr.loc2rem.resp = CR_USER2NET_RESP;
339 dl->cr.rem2loc.cmd = CR_NET2USER_CMD;
340 dl->cr.rem2loc.resp = CR_NET2USER_RESP;
341 break;
342 case LAPD_MODE_NETWORK:
343 dl->cr.loc2rem.cmd = CR_NET2USER_CMD;
344 dl->cr.loc2rem.resp = CR_NET2USER_RESP;
345 dl->cr.rem2loc.cmd = CR_USER2NET_CMD;
346 dl->cr.rem2loc.resp = CR_USER2NET_RESP;
347 break;
348 default:
349 return -EINVAL;
350 }
351 dl->mode = mode;
352
353 return 0;
354}
355
356/* send DL message with optional msgb */
357static int send_dl_l3(uint8_t prim, uint8_t op, struct lapd_msg_ctx *lctx,
358 struct msgb *msg)
359{
360 struct lapd_datalink *dl = lctx->dl;
361 struct osmo_dlsap_prim dp;
362
363 osmo_prim_init(&dp.oph, 0, prim, op, msg);
364 return dl->send_dlsap(&dp, lctx);
365}
366
367/* send simple DL message */
368static inline int send_dl_simple(uint8_t prim, uint8_t op,
369 struct lapd_msg_ctx *lctx)
370{
371 struct msgb *msg = lapd_msgb_alloc(0, "DUMMY");
372
373 return send_dl_l3(prim, op, lctx, msg);
374}
375
376/* send MDL-ERROR INDICATION */
377static int mdl_error(uint8_t cause, struct lapd_msg_ctx *lctx)
378{
379 struct lapd_datalink *dl = lctx->dl;
380 struct osmo_dlsap_prim dp;
381
382 LOGP(DLLAPD, LOGL_NOTICE, "sending MDL-ERROR-IND cause %d\n",
383 cause);
384 osmo_prim_init(&dp.oph, 0, PRIM_MDL_ERROR, PRIM_OP_INDICATION, NULL);
385 dp.u.error_ind.cause = cause;
386 return dl->send_dlsap(&dp, lctx);
387}
388
389/* send UA response */
390static int lapd_send_ua(struct lapd_msg_ctx *lctx, uint8_t len, uint8_t *data)
391{
392 struct msgb *msg = lapd_msgb_alloc(len, "LAPD UA");
393 struct lapd_msg_ctx nctx;
394 struct lapd_datalink *dl = lctx->dl;
395
396 memcpy(&nctx, lctx, sizeof(nctx));
397 msg->l3h = msgb_put(msg, len);
398 if (len)
399 memcpy(msg->l3h, data, len);
400 /* keep nctx.ldp */
401 /* keep nctx.sapi */
402 /* keep nctx.tei */
403 nctx.cr = dl->cr.loc2rem.resp;
404 nctx.format = LAPD_FORM_U;
405 nctx.s_u = LAPD_U_UA;
406 /* keep nctx.p_f */
407 nctx.length = len;
408 nctx.more = 0;
409
410 return dl->send_ph_data_req(&nctx, msg);
411}
412
413/* send DM response */
414static int lapd_send_dm(struct lapd_msg_ctx *lctx)
415{
416 struct msgb *msg = lapd_msgb_alloc(0, "LAPD DM");
417 struct lapd_msg_ctx nctx;
418 struct lapd_datalink *dl = lctx->dl;
419
420 memcpy(&nctx, lctx, sizeof(nctx));
421 /* keep nctx.ldp */
422 /* keep nctx.sapi */
423 /* keep nctx.tei */
424 nctx.cr = dl->cr.loc2rem.resp;
425 nctx.format = LAPD_FORM_U;
426 nctx.s_u = LAPD_U_DM;
427 /* keep nctx.p_f */
428 nctx.length = 0;
429 nctx.more = 0;
430
431 return dl->send_ph_data_req(&nctx, msg);
432}
433
434/* send RR response / command */
435static int lapd_send_rr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
436{
437 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RR");
438 struct lapd_msg_ctx nctx;
439 struct lapd_datalink *dl = lctx->dl;
440
441 memcpy(&nctx, lctx, sizeof(nctx));
442 /* keep nctx.ldp */
443 /* keep nctx.sapi */
444 /* keep nctx.tei */
445 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
446 nctx.format = LAPD_FORM_S;
447 nctx.s_u = LAPD_S_RR;
448 nctx.p_f = f_bit;
449 nctx.n_recv = dl->v_recv;
450 nctx.length = 0;
451 nctx.more = 0;
452
453 return dl->send_ph_data_req(&nctx, msg);
454}
455
456/* send RNR response / command */
457static int lapd_send_rnr(struct lapd_msg_ctx *lctx, uint8_t f_bit, uint8_t cmd)
458{
459 struct msgb *msg = lapd_msgb_alloc(0, "LAPD RNR");
460 struct lapd_msg_ctx nctx;
461 struct lapd_datalink *dl = lctx->dl;
462
463 memcpy(&nctx, lctx, sizeof(nctx));
464 /* keep nctx.ldp */
465 /* keep nctx.sapi */
466 /* keep nctx.tei */
467 nctx.cr = (cmd) ? dl->cr.loc2rem.cmd : dl->cr.loc2rem.resp;
468 nctx.format = LAPD_FORM_S;
469 nctx.s_u = LAPD_S_RNR;
470 nctx.p_f = f_bit;
471 nctx.n_recv = dl->v_recv;
472 nctx.length = 0;
473 nctx.more = 0;
474
475 return dl->send_ph_data_req(&nctx, msg);
476}
477
478/* send REJ response */
479static int lapd_send_rej(struct lapd_msg_ctx *lctx, uint8_t f_bit)
480{
481 struct msgb *msg = lapd_msgb_alloc(0, "LAPD REJ");
482 struct lapd_msg_ctx nctx;
483 struct lapd_datalink *dl = lctx->dl;
484
485 memcpy(&nctx, lctx, sizeof(nctx));
486 /* keep nctx.ldp */
487 /* keep nctx.sapi */
488 /* keep nctx.tei */
489 nctx.cr = dl->cr.loc2rem.resp;
490 nctx.format = LAPD_FORM_S;
491 nctx.s_u = LAPD_S_REJ;
492 nctx.p_f = f_bit;
493 nctx.n_recv = dl->v_recv;
494 nctx.length = 0;
495 nctx.more = 0;
496
497 return dl->send_ph_data_req(&nctx, msg);
498}
499
500/* resend SABM or DISC message */
501static int lapd_send_resend(struct lapd_datalink *dl)
502{
503 struct msgb *msg;
504 uint8_t h = do_mod(dl->v_send, dl->range_hist);
505 int length = dl->tx_hist[h].msg->len;
506 struct lapd_msg_ctx nctx;
507
508 /* assemble message */
509 memcpy(&nctx, &dl->lctx, sizeof(nctx));
510 /* keep nctx.ldp */
511 /* keep nctx.sapi */
512 /* keep nctx.tei */
513 nctx.cr = dl->cr.loc2rem.cmd;
514 nctx.format = LAPD_FORM_U;
515 if (dl->state == LAPD_STATE_SABM_SENT)
516 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
517 else
518 nctx.s_u = LAPD_U_DISC;
519 nctx.p_f = 1;
520 nctx.length = length;
521 nctx.more = 0;
522
523 /* Resend SABM/DISC from tx_hist */
524 msg = lapd_msgb_alloc(length, "LAPD resend");
525 msg->l3h = msgb_put(msg, length);
526 if (length)
527 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
528
529 return dl->send_ph_data_req(&nctx, msg);
530}
531
532/* reestablish link */
533static int lapd_reestablish(struct lapd_datalink *dl)
534{
535 struct osmo_dlsap_prim dp;
536 struct msgb *msg;
537
538 msg = lapd_msgb_alloc(0, "DUMMY");
539 osmo_prim_init(&dp.oph, 0, PRIM_DL_EST, PRIM_OP_REQUEST, msg);
540
541 return lapd_est_req(&dp, &dl->lctx);
542}
543
544/* Timer callback on T200 expiry */
545static void lapd_t200_cb(void *data)
546{
547 struct lapd_datalink *dl = data;
548
Andreas Eversberg742fc792011-09-27 09:40:25 +0200549 LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200550 (int) dl->state);
551
552 switch (dl->state) {
553 case LAPD_STATE_SABM_SENT:
554 /* 5.4.1.3 */
555 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
556 /* send RELEASE INDICATION to L3 */
557 send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
558 &dl->lctx);
559 /* send MDL ERROR INIDCATION to L3 */
560 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
561 /* flush tx and send buffers */
562 lapd_dl_flush_tx(dl);
563 lapd_dl_flush_send(dl);
564 /* go back to idle state */
565 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
566 /* NOTE: we must not change any other states or buffers
567 * and queues, since we may reconnect after handover
568 * failure. the buffered messages is replaced there */
569 break;
570 }
571 /* retransmit SABM command */
572 lapd_send_resend(dl);
573 /* increment re-transmission counter */
574 dl->retrans_ctr++;
575 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200576 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200577 break;
578 case LAPD_STATE_DISC_SENT:
579 /* 5.4.4.3 */
580 if (dl->retrans_ctr + 1 >= dl->n200_est_rel + 1) {
581 /* send RELEASE INDICATION to L3 */
582 send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
583 /* send MDL ERROR INIDCATION to L3 */
584 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
585 /* flush tx and send buffers */
586 lapd_dl_flush_tx(dl);
587 lapd_dl_flush_send(dl);
588 /* go back to idle state */
589 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
590 /* NOTE: we must not change any other states or buffers
591 * and queues, since we may reconnect after handover
592 * failure. the buffered messages is replaced there */
593 break;
594 }
595 /* retransmit DISC command */
596 lapd_send_resend(dl);
597 /* increment re-transmission counter */
598 dl->retrans_ctr++;
599 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200600 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200601 break;
602 case LAPD_STATE_MF_EST:
603 /* 5.5.7 */
604 dl->retrans_ctr = 0;
605 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
606 /* fall through */
607 case LAPD_STATE_TIMER_RECOV:
608 dl->retrans_ctr++;
609 if (dl->retrans_ctr < dl->n200) {
610 uint8_t vs = sub_mod(dl->v_send, 1, dl->v_range);
611 uint8_t h = do_mod(vs, dl->range_hist);
612 /* retransmit I frame (V_s-1) with P=1, if any */
613 if (dl->tx_hist[h].msg) {
614 struct msgb *msg;
615 int length = dl->tx_hist[h].msg->len;
616 struct lapd_msg_ctx nctx;
617
618 LOGP(DLLAPD, LOGL_INFO, "retransmit last frame"
619 " V(S)=%d\n", vs);
620 /* Create I frame (segment) from tx_hist */
621 memcpy(&nctx, &dl->lctx, sizeof(nctx));
622 /* keep nctx.ldp */
623 /* keep nctx.sapi */
624 /* keep nctx.tei */
625 nctx.cr = dl->cr.loc2rem.cmd;
626 nctx.format = LAPD_FORM_I;
627 nctx.p_f = 1;
628 nctx.n_send = vs;
629 nctx.n_recv = dl->v_recv;
630 nctx.length = length;
631 nctx.more = dl->tx_hist[h].more;
632 msg = lapd_msgb_alloc(length, "LAPD I resend");
633 msg->l3h = msgb_put(msg, length);
634 memcpy(msg->l3h, dl->tx_hist[h].msg->data,
635 length);
636 dl->send_ph_data_req(&nctx, msg);
637 } else {
638 /* OR send appropriate supervision frame with P=1 */
639 if (!dl->own_busy && !dl->seq_err_cond) {
640 lapd_send_rr(&dl->lctx, 1, 1);
641 /* NOTE: In case of sequence error
642 * condition, the REJ frame has been
643 * transmitted when entering the
644 * condition, so it has not be done
645 * here
646 */
647 } else if (dl->own_busy) {
648 lapd_send_rnr(&dl->lctx, 1, 1);
649 } else {
650 LOGP(DLLAPD, LOGL_INFO, "unhandled, "
651 "pls. fix\n");
652 }
653 }
654 /* restart T200 (PH-READY-TO-SEND) */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200655 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200656 } else {
657 /* send MDL ERROR INIDCATION to L3 */
658 mdl_error(MDL_CAUSE_T200_EXPIRED, &dl->lctx);
659 /* reestablish */
660 if (!dl->reestablish)
661 break;
662 LOGP(DLLAPD, LOGL_NOTICE, "N200 reached, performing "
663 "reestablishment.\n");
664 lapd_reestablish(dl);
665 }
666 break;
667 default:
668 LOGP(DLLAPD, LOGL_INFO, "T200 expired in unexpected "
669 "dl->state %d\n", (int) dl->state);
670 }
671}
672
673/* Timer callback on T203 expiry */
674static void lapd_t203_cb(void *data)
675{
676 struct lapd_datalink *dl = data;
677
Andreas Eversberg742fc792011-09-27 09:40:25 +0200678 LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl,
rootaf48bed2011-09-26 11:23:06 +0200679 (int) dl->state);
680
681 if (dl->state != LAPD_STATE_MF_EST) {
682 LOGP(DLLAPD, LOGL_ERROR, "T203 fired outside MF EST state, "
683 "please fix!\n");
684 return;
685 }
686
687 /* set retransmission counter to 0 */
688 dl->retrans_ctr = 0;
689 /* enter timer recovery state */
690 lapd_dl_newstate(dl, LAPD_STATE_TIMER_RECOV);
691 /* transmit a supervisory command with P bit set to 1 as follows: */
692 if (!dl->own_busy) {
693 LOGP(DLLAPD, LOGL_INFO, "transmit an RR poll command\n");
694 /* Send RR with P=1 */
695 lapd_send_rr(&dl->lctx, 1, 1);
696 } else {
697 LOGP(DLLAPD, LOGL_INFO, "transmit an RNR poll command\n");
698 /* Send RNR with P=1 */
699 lapd_send_rnr(&dl->lctx, 1, 1);
700 }
701 /* start T200 */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200702 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200703}
704
705/* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */
706static void lapd_acknowledge(struct lapd_msg_ctx *lctx)
707{
708 struct lapd_datalink *dl = lctx->dl;
709 uint8_t nr = lctx->n_recv;
Holger Hans Peter Freytherfb6a2e22012-03-16 10:35:38 +0100710 int s = 0, rej = 0, t200_reset = 0;
rootaf48bed2011-09-26 11:23:06 +0200711 int i, h;
712
713 /* supervisory frame ? */
714 if (lctx->format == LAPD_FORM_S)
715 s = 1;
716 /* REJ frame ? */
717 if (s && lctx->s_u == LAPD_S_REJ)
718 rej = 1;
719
720 /* Flush all transmit buffers of acknowledged frames */
721 for (i = dl->v_ack; i != nr; i = inc_mod(i, dl->v_range)) {
722 h = do_mod(i, dl->range_hist);
723 if (dl->tx_hist[h].msg) {
724 msgb_free(dl->tx_hist[h].msg);
725 dl->tx_hist[h].msg = NULL;
726 LOGP(DLLAPD, LOGL_INFO, "ack frame %d\n", i);
727 }
728 }
729
730 if (dl->state != LAPD_STATE_TIMER_RECOV) {
731 /* When not in the timer recovery condition, the data
732 * link layer entity shall reset the timer T200 on
733 * receipt of a valid I frame with N(R) higher than V(A),
734 * or an REJ with an N(R) equal to V(A). */
735 if ((!rej && nr != dl->v_ack)
736 || (rej && nr == dl->v_ack)) {
rootaf48bed2011-09-26 11:23:06 +0200737 t200_reset = 1;
Andreas Eversberg742fc792011-09-27 09:40:25 +0200738 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200739 /* 5.5.3.1 Note 1 + 2 imply timer recovery cond. */
740 }
741 /* 5.7.4: N(R) sequence error
742 * N(R) is called valid, if and only if
743 * (N(R)-V(A)) mod 8 <= (V(S)-V(A)) mod 8.
744 */
745 if (sub_mod(nr, dl->v_ack, dl->v_range)
746 > sub_mod(dl->v_send, dl->v_ack, dl->v_range)) {
747 LOGP(DLLAPD, LOGL_NOTICE, "N(R) sequence error\n");
748 mdl_error(MDL_CAUSE_SEQ_ERR, lctx);
749 }
750 }
751
752 /* V(A) shall be set to the value of N(R) */
753 dl->v_ack = nr;
754
Andreas Eversberg742fc792011-09-27 09:40:25 +0200755 /* If T200 has been stopped by the receipt of an I, RR or RNR frame,
rootaf48bed2011-09-26 11:23:06 +0200756 * and if there are outstanding I frames, restart T200 */
757 if (t200_reset && !rej) {
758 if (dl->tx_hist[sub_mod(dl->v_send, 1, dl->range_hist)].msg) {
759 LOGP(DLLAPD, LOGL_INFO, "start T200, due to unacked I "
760 "frame(s)\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +0200761 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200762 }
763 }
764
765 /* This also does a restart, when I or S frame is received */
766
767 /* Stop T203, if running */
Andreas Eversberg742fc792011-09-27 09:40:25 +0200768 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200769 /* Start T203, if T200 is not running in MF EST state, if enabled */
770 if (!osmo_timer_pending(&dl->t200)
771 && (dl->t203_sec || dl->t203_usec)
772 && (dl->state == LAPD_STATE_MF_EST)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +0200773 lapd_start_t203(dl);
rootaf48bed2011-09-26 11:23:06 +0200774 }
775}
776
777/* L1 -> L2 */
778
779/* Receive a LAPD U (Unnumbered) message from L1 */
780static int lapd_rx_u(struct msgb *msg, struct lapd_msg_ctx *lctx)
781{
782 struct lapd_datalink *dl = lctx->dl;
783 int length = lctx->length;
Sylvain Munaut9a5f3b82011-11-20 09:01:59 +0100784 int rc = 0;
rootaf48bed2011-09-26 11:23:06 +0200785 uint8_t prim, op;
786
787 switch (lctx->s_u) {
788 case LAPD_U_SABM:
789 case LAPD_U_SABME:
790 prim = PRIM_DL_EST;
791 op = PRIM_OP_INDICATION;
792
793 LOGP(DLLAPD, LOGL_INFO, "SABM(E) received in state %s\n",
794 lapd_state_names[dl->state]);
795 /* 5.7.1 */
796 dl->seq_err_cond = 0;
797 /* G.2.2 Wrong value of the C/R bit */
798 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100799 LOGP(DLLAPD, LOGL_ERROR, "SABM response error\n");
rootaf48bed2011-09-26 11:23:06 +0200800 msgb_free(msg);
801 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
802 return -EINVAL;
803 }
804
805 /* G.4.5 If SABM is received with L>N201 or with M bit
806 * set, AN MDL-ERROR-INDICATION is sent to MM.
807 */
808 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100809 LOGP(DLLAPD, LOGL_ERROR, "SABM too large error\n");
rootaf48bed2011-09-26 11:23:06 +0200810 msgb_free(msg);
811 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
812 return -EIO;
813 }
814
815 switch (dl->state) {
816 case LAPD_STATE_IDLE:
817 break;
818 case LAPD_STATE_MF_EST:
819 LOGP(DLLAPD, LOGL_INFO, "SABM command, multiple "
820 "frame established state\n");
821 /* If link is lost on the remote side, we start over
822 * and send DL-ESTABLISH indication again. */
823 if (dl->v_send != dl->v_recv) {
824 LOGP(DLLAPD, LOGL_INFO, "Remote reestablish\n");
825 mdl_error(MDL_CAUSE_SABM_MF, lctx);
826 break;
827 }
828 /* Ignore SABM if content differs from first SABM. */
829 if (dl->mode == LAPD_MODE_NETWORK && length
830 && dl->cont_res) {
831#ifdef TEST_CONTENT_RESOLUTION_NETWORK
832 dl->cont_res->data[0] ^= 0x01;
833#endif
834 if (memcmp(dl->cont_res, msg->data, length)) {
835 LOGP(DLLAPD, LOGL_INFO, "Another SABM "
836 "with diffrent content - "
837 "ignoring!\n");
838 msgb_free(msg);
839 return 0;
840 }
841 }
842 /* send UA again */
843 lapd_send_ua(lctx, length, msg->l3h);
844 msgb_free(msg);
845 return 0;
846 case LAPD_STATE_DISC_SENT:
847 /* 5.4.6.2 send DM with F=P */
848 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +0200849 /* stop Timer T200 */
850 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200851 msgb_free(msg);
852 return send_dl_simple(prim, op, lctx);
853 default:
854 /* collision: Send UA, but still wait for rx UA, then
855 * change to MF_EST state.
856 */
857 /* check for contention resoultion */
858 if (dl->tx_hist[0].msg && dl->tx_hist[0].msg->len) {
859 LOGP(DLLAPD, LOGL_NOTICE, "SABM not allowed "
860 "during contention resolution\n");
861 mdl_error(MDL_CAUSE_SABM_INFO_NOTALL, lctx);
862 }
863 lapd_send_ua(lctx, length, msg->l3h);
864 msgb_free(msg);
865 return 0;
866 }
867 /* save message context for further use */
868 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
869#ifndef TEST_CONTENT_RESOLUTION_NETWORK
870 /* send UA response */
871 lapd_send_ua(lctx, length, msg->l3h);
872#endif
873 /* set Vs, Vr and Va to 0 */
874 dl->v_send = dl->v_recv = dl->v_ack = 0;
875 /* clear tx_hist */
876 lapd_dl_flush_hist(dl);
877 /* enter multiple-frame-established state */
878 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
879 /* store content resolution data on network side
880 * Note: cont_res will be removed when changing state again,
881 * so it must be allocated AFTER lapd_dl_newstate(). */
882 if (dl->mode == LAPD_MODE_NETWORK && length) {
883 dl->cont_res = lapd_msgb_alloc(length, "CONT RES");
884 memcpy(msgb_put(dl->cont_res, length), msg->l3h,
885 length);
886 LOGP(DLLAPD, LOGL_NOTICE, "Store content res.\n");
887 }
888 /* send notification to L3 */
889 if (length == 0) {
890 /* 5.4.1.2 Normal establishment procedures */
891 rc = send_dl_simple(prim, op, lctx);
892 msgb_free(msg);
893 } else {
894 /* 5.4.1.4 Contention resolution establishment */
895 rc = send_dl_l3(prim, op, lctx, msg);
896 }
897 break;
898 case LAPD_U_DM:
899 LOGP(DLLAPD, LOGL_INFO, "DM received in state %s\n",
900 lapd_state_names[dl->state]);
901 /* G.2.2 Wrong value of the C/R bit */
902 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100903 LOGP(DLLAPD, LOGL_ERROR, "DM command error\n");
rootaf48bed2011-09-26 11:23:06 +0200904 msgb_free(msg);
905 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
906 return -EINVAL;
907 }
908 if (!lctx->p_f) {
909 /* 5.4.1.2 DM responses with the F bit set to "0"
910 * shall be ignored.
911 */
912 msgb_free(msg);
913 return 0;
914 }
915 switch (dl->state) {
916 case LAPD_STATE_SABM_SENT:
917 break;
918 case LAPD_STATE_MF_EST:
919 if (lctx->p_f) {
920 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
921 "response\n");
922 mdl_error(MDL_CAUSE_UNSOL_DM_RESP, lctx);
923 } else {
924 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
925 "response, multiple frame established "
926 "state\n");
927 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
928 /* reestablish */
929 if (!dl->reestablish) {
930 msgb_free(msg);
931 return 0;
932 }
933 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
934 "reestablishment.\n");
935 lapd_reestablish(dl);
936 }
937 msgb_free(msg);
938 return 0;
939 case LAPD_STATE_TIMER_RECOV:
940 /* FP = 0 (DM is normal in case PF = 1) */
941 if (!lctx->p_f) {
942 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM "
943 "response, multiple frame established "
944 "state\n");
945 mdl_error(MDL_CAUSE_UNSOL_DM_RESP_MF, lctx);
946 msgb_free(msg);
947 /* reestablish */
948 if (!dl->reestablish)
949 return 0;
950 LOGP(DLLAPD, LOGL_NOTICE, "Performing "
951 "reestablishment.\n");
952 return lapd_reestablish(dl);
953 }
954 break;
955 case LAPD_STATE_DISC_SENT:
Andreas Eversberg742fc792011-09-27 09:40:25 +0200956 /* stop Timer T200 */
957 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200958 /* go to idle state */
959 lapd_dl_flush_tx(dl);
960 lapd_dl_flush_send(dl);
961 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
962 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
963 msgb_free(msg);
964 return 0;
965 case LAPD_STATE_IDLE:
966 /* 5.4.5 all other frame types shall be discarded */
967 default:
968 LOGP(DLLAPD, LOGL_INFO, "unsolicited DM response! "
969 "(discarding)\n");
970 msgb_free(msg);
971 return 0;
972 }
Andreas Eversberg742fc792011-09-27 09:40:25 +0200973 /* stop timer T200 */
974 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +0200975 /* go to idle state */
976 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
977 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION, lctx);
978 msgb_free(msg);
979 break;
980 case LAPD_U_UI:
981 LOGP(DLLAPD, LOGL_INFO, "UI received\n");
982 /* G.2.2 Wrong value of the C/R bit */
983 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100984 LOGP(DLLAPD, LOGL_ERROR, "UI indicates response "
rootaf48bed2011-09-26 11:23:06 +0200985 "error\n");
986 msgb_free(msg);
987 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
988 return -EINVAL;
989 }
990
991 /* G.4.5 If UI is received with L>N201 or with M bit
992 * set, AN MDL-ERROR-INDICATION is sent to MM.
993 */
994 if (length > lctx->n201 || lctx->more) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +0100995 LOGP(DLLAPD, LOGL_ERROR, "UI too large error "
rootaf48bed2011-09-26 11:23:06 +0200996 "(%d > N201(%d) or M=%d)\n", length,
997 lctx->n201, lctx->more);
998 msgb_free(msg);
999 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1000 return -EIO;
1001 }
1002
1003 /* do some length checks */
1004 if (length == 0) {
1005 /* 5.3.3 UI frames received with the length indicator
1006 * set to "0" shall be ignored
1007 */
1008 LOGP(DLLAPD, LOGL_INFO, "length=0 (discarding)\n");
1009 msgb_free(msg);
1010 return 0;
1011 }
1012 rc = send_dl_l3(PRIM_DL_UNIT_DATA, PRIM_OP_INDICATION, lctx,
1013 msg);
1014 break;
1015 case LAPD_U_DISC:
1016 prim = PRIM_DL_REL;
1017 op = PRIM_OP_INDICATION;
1018
1019 LOGP(DLLAPD, LOGL_INFO, "DISC received in state %s\n",
1020 lapd_state_names[dl->state]);
1021 /* flush tx and send buffers */
1022 lapd_dl_flush_tx(dl);
1023 lapd_dl_flush_send(dl);
1024 /* 5.7.1 */
1025 dl->seq_err_cond = 0;
1026 /* G.2.2 Wrong value of the C/R bit */
1027 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001028 LOGP(DLLAPD, LOGL_ERROR, "DISC response error\n");
rootaf48bed2011-09-26 11:23:06 +02001029 msgb_free(msg);
1030 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1031 return -EINVAL;
1032 }
1033 if (length > 0 || lctx->more) {
1034 /* G.4.4 If a DISC or DM frame is received with L>0 or
1035 * with the M bit set to "1", an MDL-ERROR-INDICATION
1036 * primitive with cause "U frame with incorrect
1037 * parameters" is sent to the mobile management entity.
1038 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001039 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001040 "U frame iwth incorrect parameters ");
1041 msgb_free(msg);
1042 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1043 return -EIO;
1044 }
1045 switch (dl->state) {
1046 case LAPD_STATE_IDLE:
1047 LOGP(DLLAPD, LOGL_INFO, "DISC in idle state\n");
1048 /* send DM with F=P */
1049 msgb_free(msg);
1050 return lapd_send_dm(lctx);
1051 case LAPD_STATE_SABM_SENT:
1052 LOGP(DLLAPD, LOGL_INFO, "DISC in SABM state\n");
1053 /* 5.4.6.2 send DM with F=P */
1054 lapd_send_dm(lctx);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001055 /* stop Timer T200 */
1056 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001057 /* go to idle state */
1058 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1059 msgb_free(msg);
1060 return send_dl_simple(PRIM_DL_REL, PRIM_OP_INDICATION,
1061 lctx);
1062 case LAPD_STATE_MF_EST:
1063 case LAPD_STATE_TIMER_RECOV:
1064 LOGP(DLLAPD, LOGL_INFO, "DISC in est state\n");
1065 break;
1066 case LAPD_STATE_DISC_SENT:
1067 LOGP(DLLAPD, LOGL_INFO, "DISC in disc state\n");
1068 prim = PRIM_DL_REL;
1069 op = PRIM_OP_CONFIRM;
1070 break;
1071 default:
1072 lapd_send_ua(lctx, length, msg->l3h);
1073 msgb_free(msg);
1074 return 0;
1075 }
1076 /* send UA response */
1077 lapd_send_ua(lctx, length, msg->l3h);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001078 /* stop Timer T200 */
1079 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001080 /* enter idle state, keep tx-buffer with UA response */
1081 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1082 /* send notification to L3 */
1083 rc = send_dl_simple(prim, op, lctx);
1084 msgb_free(msg);
1085 break;
1086 case LAPD_U_UA:
1087 LOGP(DLLAPD, LOGL_INFO, "UA received in state %s\n",
1088 lapd_state_names[dl->state]);
1089 /* G.2.2 Wrong value of the C/R bit */
1090 if (lctx->cr == dl->cr.rem2loc.cmd) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001091 LOGP(DLLAPD, LOGL_ERROR, "UA indicates command "
rootaf48bed2011-09-26 11:23:06 +02001092 "error\n");
1093 msgb_free(msg);
1094 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1095 return -EINVAL;
1096 }
1097
1098 /* G.4.5 If UA is received with L>N201 or with M bit
1099 * set, AN MDL-ERROR-INDICATION is sent to MM.
1100 */
1101 if (lctx->more || length > lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001102 LOGP(DLLAPD, LOGL_ERROR, "UA too large error\n");
rootaf48bed2011-09-26 11:23:06 +02001103 msgb_free(msg);
1104 mdl_error(MDL_CAUSE_UFRM_INC_PARAM, lctx);
1105 return -EIO;
1106 }
1107
1108 if (!lctx->p_f) {
1109 /* 5.4.1.2 A UA response with the F bit set to "0"
1110 * shall be ignored.
1111 */
1112 LOGP(DLLAPD, LOGL_INFO, "F=0 (discarding)\n");
1113 msgb_free(msg);
1114 return 0;
1115 }
1116 switch (dl->state) {
1117 case LAPD_STATE_SABM_SENT:
1118 break;
1119 case LAPD_STATE_MF_EST:
1120 case LAPD_STATE_TIMER_RECOV:
1121 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1122 "(discarding)\n");
1123 mdl_error(MDL_CAUSE_UNSOL_UA_RESP, lctx);
1124 msgb_free(msg);
1125 return 0;
1126 case LAPD_STATE_DISC_SENT:
1127 LOGP(DLLAPD, LOGL_INFO, "UA in disconnect state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001128 /* stop Timer T200 */
1129 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001130 /* go to idle state */
1131 lapd_dl_flush_tx(dl);
1132 lapd_dl_flush_send(dl);
1133 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1134 rc = send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, lctx);
1135 msgb_free(msg);
1136 return 0;
1137 case LAPD_STATE_IDLE:
1138 /* 5.4.5 all other frame types shall be discarded */
1139 default:
1140 LOGP(DLLAPD, LOGL_INFO, "unsolicited UA response! "
1141 "(discarding)\n");
1142 msgb_free(msg);
1143 return 0;
1144 }
1145 LOGP(DLLAPD, LOGL_INFO, "UA in SABM state\n");
Andreas Eversberg742fc792011-09-27 09:40:25 +02001146 /* stop Timer T200 */
1147 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001148 /* compare UA with SABME if contention resolution is applied */
1149 if (dl->tx_hist[0].msg->len) {
1150 if (length != (dl->tx_hist[0].msg->len)
1151 || !!memcmp(dl->tx_hist[0].msg->data, msg->l3h,
1152 length)) {
1153 LOGP(DLLAPD, LOGL_INFO, "**** UA response "
1154 "mismatches ****\n");
1155 rc = send_dl_simple(PRIM_DL_REL,
1156 PRIM_OP_INDICATION, lctx);
1157 msgb_free(msg);
1158 /* go to idle state */
1159 lapd_dl_flush_tx(dl);
1160 lapd_dl_flush_send(dl);
1161 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
1162 return 0;
1163 }
1164 }
1165 /* set Vs, Vr and Va to 0 */
1166 dl->v_send = dl->v_recv = dl->v_ack = 0;
1167 /* clear tx_hist */
1168 lapd_dl_flush_hist(dl);
1169 /* enter multiple-frame-established state */
1170 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1171 /* send outstanding frames, if any (resume / reconnect) */
1172 lapd_send_i(lctx, __LINE__);
1173 /* send notification to L3 */
1174 rc = send_dl_simple(PRIM_DL_EST, PRIM_OP_CONFIRM, lctx);
1175 msgb_free(msg);
1176 break;
1177 case LAPD_U_FRMR:
1178 LOGP(DLLAPD, LOGL_NOTICE, "Frame reject received\n");
1179 /* send MDL ERROR INIDCATION to L3 */
1180 mdl_error(MDL_CAUSE_FRMR, lctx);
1181 msgb_free(msg);
1182 /* reestablish */
1183 if (!dl->reestablish)
1184 break;
1185 LOGP(DLLAPD, LOGL_NOTICE, "Performing reestablishment.\n");
1186 rc = lapd_reestablish(dl);
1187 break;
1188 default:
1189 /* G.3.1 */
1190 LOGP(DLLAPD, LOGL_NOTICE, "Unnumbered frame not allowed.\n");
1191 msgb_free(msg);
1192 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1193 return -EINVAL;
1194 }
1195 return rc;
1196}
1197
1198/* Receive a LAPD S (Supervisory) message from L1 */
1199static int lapd_rx_s(struct msgb *msg, struct lapd_msg_ctx *lctx)
1200{
1201 struct lapd_datalink *dl = lctx->dl;
1202 int length = lctx->length;
1203
1204 if (length > 0 || lctx->more) {
1205 /* G.4.3 If a supervisory frame is received with L>0 or
1206 * with the M bit set to "1", an MDL-ERROR-INDICATION
1207 * primitive with cause "S frame with incorrect
1208 * parameters" is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001209 LOGP(DLLAPD, LOGL_ERROR,
rootaf48bed2011-09-26 11:23:06 +02001210 "S frame with incorrect parameters\n");
1211 msgb_free(msg);
1212 mdl_error(MDL_CAUSE_SFRM_INC_PARAM, lctx);
1213 return -EIO;
1214 }
1215
1216 if (lctx->cr == dl->cr.rem2loc.resp
1217 && lctx->p_f
1218 && dl->state != LAPD_STATE_TIMER_RECOV) {
1219 /* 5.4.2.2: Inidcate error on supervisory reponse F=1 */
1220 LOGP(DLLAPD, LOGL_NOTICE, "S frame response with F=1 error\n");
1221 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1222 }
1223
1224 switch (dl->state) {
1225 case LAPD_STATE_IDLE:
1226 /* if P=1, respond DM with F=1 (5.2.2) */
1227 /* 5.4.5 all other frame types shall be discarded */
1228 if (lctx->p_f)
1229 lapd_send_dm(lctx); /* F=P */
1230 /* fall though */
1231 case LAPD_STATE_SABM_SENT:
1232 case LAPD_STATE_DISC_SENT:
1233 LOGP(DLLAPD, LOGL_NOTICE, "S frame ignored in this state\n");
1234 msgb_free(msg);
1235 return 0;
1236 }
1237 switch (lctx->s_u) {
1238 case LAPD_S_RR:
1239 LOGP(DLLAPD, LOGL_INFO, "RR received in state %s\n",
1240 lapd_state_names[dl->state]);
1241 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1242 lapd_acknowledge(lctx);
1243
1244 /* 5.5.3.2 */
1245 if (lctx->cr == dl->cr.rem2loc.cmd
1246 && lctx->p_f) {
1247 if (!dl->own_busy && !dl->seq_err_cond) {
1248 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1249 "with polling bit set and we are not "
1250 "busy, so we reply with RR frame "
1251 "response\n");
1252 lapd_send_rr(lctx, 1, 0);
1253 /* NOTE: In case of sequence error condition,
1254 * the REJ frame has been transmitted when
1255 * entering the condition, so it has not be
1256 * done here
1257 */
1258 } else if (dl->own_busy) {
1259 LOGP(DLLAPD, LOGL_INFO, "RR frame command "
1260 "with polling bit set and we are busy, "
1261 "so we reply with RR frame response\n");
1262 lapd_send_rnr(lctx, 1, 0);
1263 }
1264 } else if (lctx->cr == dl->cr.rem2loc.resp
1265 && lctx->p_f
1266 && dl->state == LAPD_STATE_TIMER_RECOV) {
1267 LOGP(DLLAPD, LOGL_INFO, "RR response with F==1, "
1268 "and we are in timer recovery state, so "
1269 "we leave that state\n");
1270 /* V(S) to the N(R) in the RR frame */
1271 dl->v_send = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001272 /* stop Timer T200 */
1273 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001274 /* 5.5.7 Clear timer recovery condition */
1275 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1276 }
1277 /* Send message, if possible due to acknowledged data */
1278 lapd_send_i(lctx, __LINE__);
1279
1280 break;
1281 case LAPD_S_RNR:
1282 LOGP(DLLAPD, LOGL_INFO, "RNR received in state %s\n",
1283 lapd_state_names[dl->state]);
1284 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1285 lapd_acknowledge(lctx);
1286
1287 /* 5.5.5 */
1288 /* Set peer receiver busy condition */
1289 dl->peer_busy = 1;
1290
1291 if (lctx->p_f) {
1292 if (lctx->cr == dl->cr.rem2loc.cmd) {
1293 if (!dl->own_busy) {
1294 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1295 "command and we are not busy, "
1296 "so we reply with RR final "
1297 "response\n");
1298 /* Send RR with F=1 */
1299 lapd_send_rr(lctx, 1, 0);
1300 } else {
1301 LOGP(DLLAPD, LOGL_INFO, "RNR poll "
1302 "command and we are busy, so "
1303 "we reply with RNR final "
1304 "response\n");
1305 /* Send RNR with F=1 */
1306 lapd_send_rnr(lctx, 1, 0);
1307 }
1308 } else if (dl->state == LAPD_STATE_TIMER_RECOV) {
1309 LOGP(DLLAPD, LOGL_INFO, "RNR poll response "
1310 "and we in timer recovery state, so "
1311 "we leave that state\n");
1312 /* 5.5.7 Clear timer recovery condition */
1313 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1314 /* V(S) to the N(R) in the RNR frame */
1315 dl->v_send = lctx->n_recv;
1316 }
1317 } else
1318 LOGP(DLLAPD, LOGL_INFO, "RNR not polling/final state "
1319 "received\n");
1320
1321 /* Send message, if possible due to acknowledged data */
1322 lapd_send_i(lctx, __LINE__);
1323
1324 break;
1325 case LAPD_S_REJ:
1326 LOGP(DLLAPD, LOGL_INFO, "REJ received in state %s\n",
1327 lapd_state_names[dl->state]);
1328 /* 5.5.3.1: Acknowlege all tx frames up the the N(R)-1 */
1329 lapd_acknowledge(lctx);
1330
1331 /* 5.5.4.1 */
1332 if (dl->state != LAPD_STATE_TIMER_RECOV) {
1333 /* Clear an existing peer receiver busy condition */
1334 dl->peer_busy = 0;
1335 /* V(S) and V(A) to the N(R) in the REJ frame */
1336 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001337 /* stop Timer T200 */
1338 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001339 /* 5.5.3.2 */
1340 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1341 if (!dl->own_busy && !dl->seq_err_cond) {
1342 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1343 "command not in timer recovery "
1344 "state and not in own busy "
1345 "condition received, so we "
1346 "respond with RR final "
1347 "response\n");
1348 lapd_send_rr(lctx, 1, 0);
1349 /* NOTE: In case of sequence error
1350 * condition, the REJ frame has been
1351 * transmitted when entering the
1352 * condition, so it has not be done
1353 * here
1354 */
1355 } else if (dl->own_busy) {
1356 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1357 "command not in timer recovery "
1358 "state and in own busy "
1359 "condition received, so we "
1360 "respond with RNR final "
1361 "response\n");
1362 lapd_send_rnr(lctx, 1, 0);
1363 }
1364 } else
1365 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1366 "polling command not in timer recovery "
1367 "state received\n");
1368 /* send MDL ERROR INIDCATION to L3 */
1369 if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1370 mdl_error(MDL_CAUSE_UNSOL_SPRV_RESP, lctx);
1371 }
1372
1373 } else if (lctx->cr == dl->cr.rem2loc.resp && lctx->p_f) {
1374 LOGP(DLLAPD, LOGL_INFO, "REJ poll response in timer "
1375 "recovery state received\n");
1376 /* Clear an existing peer receiver busy condition */
1377 dl->peer_busy = 0;
1378 /* V(S) and V(A) to the N(R) in the REJ frame */
1379 dl->v_send = dl->v_ack = lctx->n_recv;
Andreas Eversberg742fc792011-09-27 09:40:25 +02001380 /* stop Timer T200 */
1381 lapd_stop_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001382 /* 5.5.7 Clear timer recovery condition */
1383 lapd_dl_newstate(dl, LAPD_STATE_MF_EST);
1384 } else {
1385 /* Clear an existing peer receiver busy condition */
1386 dl->peer_busy = 0;
1387 /* V(S) and V(A) to the N(R) in the REJ frame */
1388 dl->v_send = dl->v_ack = lctx->n_recv;
1389 /* 5.5.3.2 */
1390 if (lctx->cr == dl->cr.rem2loc.cmd && lctx->p_f) {
1391 if (!dl->own_busy && !dl->seq_err_cond) {
1392 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1393 "command in timer recovery "
1394 "state and not in own busy "
1395 "condition received, so we "
1396 "respond with RR final "
1397 "response\n");
1398 lapd_send_rr(lctx, 1, 0);
1399 /* NOTE: In case of sequence error
1400 * condition, the REJ frame has been
1401 * transmitted when entering the
1402 * condition, so it has not be done
1403 * here
1404 */
1405 } else if (dl->own_busy) {
1406 LOGP(DLLAPD, LOGL_INFO, "REJ poll "
1407 "command in timer recovery "
1408 "state and in own busy "
1409 "condition received, so we "
1410 "respond with RNR final "
1411 "response\n");
1412 lapd_send_rnr(lctx, 1, 0);
1413 }
1414 } else
1415 LOGP(DLLAPD, LOGL_INFO, "REJ response or not "
1416 "polling command in timer recovery "
1417 "state received\n");
1418 }
1419
1420 /* FIXME: 5.5.4.2 2) */
1421
1422 /* Send message, if possible due to acknowledged data */
1423 lapd_send_i(lctx, __LINE__);
1424
1425 break;
1426 default:
1427 /* G.3.1 */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001428 LOGP(DLLAPD, LOGL_ERROR, "Supervisory frame not allowed.\n");
rootaf48bed2011-09-26 11:23:06 +02001429 msgb_free(msg);
1430 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1431 return -EINVAL;
1432 }
1433 msgb_free(msg);
1434 return 0;
1435}
1436
1437/* Receive a LAPD I (Information) message from L1 */
1438static int lapd_rx_i(struct msgb *msg, struct lapd_msg_ctx *lctx)
1439{
1440 struct lapd_datalink *dl = lctx->dl;
1441 //uint8_t nr = lctx->n_recv;
1442 uint8_t ns = lctx->n_send;
1443 int length = lctx->length;
1444 int rc;
1445
1446 LOGP(DLLAPD, LOGL_INFO, "I received in state %s\n",
1447 lapd_state_names[dl->state]);
1448
1449 /* G.2.2 Wrong value of the C/R bit */
1450 if (lctx->cr == dl->cr.rem2loc.resp) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001451 LOGP(DLLAPD, LOGL_ERROR, "I frame response not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001452 msgb_free(msg);
1453 mdl_error(MDL_CAUSE_FRM_UNIMPL, lctx);
1454 return -EINVAL;
1455 }
1456
1457 if (length == 0 || length > lctx->n201) {
1458 /* G.4.2 If the length indicator of an I frame is set
1459 * to a numerical value L>N201 or L=0, an MDL-ERROR-INDICATION
1460 * primitive with cause "I frame with incorrect length"
1461 * is sent to the mobile management entity. */
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001462 LOGP(DLLAPD, LOGL_ERROR, "I frame length not allowed\n");
rootaf48bed2011-09-26 11:23:06 +02001463 msgb_free(msg);
1464 mdl_error(MDL_CAUSE_IFRM_INC_LEN, lctx);
1465 return -EIO;
1466 }
1467
1468 /* G.4.2 If the numerical value of L is L<N201 and the M
1469 * bit is set to "1", then an MDL-ERROR-INDICATION primitive with
1470 * cause "I frame with incorrect use of M bit" is sent to the
1471 * mobile management entity. */
1472 if (lctx->more && length < lctx->n201) {
Holger Hans Peter Freyther8c012312012-11-26 16:52:23 +01001473 LOGP(DLLAPD, LOGL_ERROR, "I frame with M bit too short\n");
rootaf48bed2011-09-26 11:23:06 +02001474 msgb_free(msg);
1475 mdl_error(MDL_CAUSE_IFRM_INC_MBITS, lctx);
1476 return -EIO;
1477 }
1478
1479 switch (dl->state) {
1480 case LAPD_STATE_IDLE:
1481 /* if P=1, respond DM with F=1 (5.2.2) */
1482 /* 5.4.5 all other frame types shall be discarded */
1483 if (lctx->p_f)
1484 lapd_send_dm(lctx); /* F=P */
1485 /* fall though */
1486 case LAPD_STATE_SABM_SENT:
1487 case LAPD_STATE_DISC_SENT:
1488 LOGP(DLLAPD, LOGL_NOTICE, "I frame ignored in this state\n");
1489 msgb_free(msg);
1490 return 0;
1491 }
1492
1493 /* 5.7.1: N(s) sequence error */
1494 if (ns != dl->v_recv) {
1495 LOGP(DLLAPD, LOGL_NOTICE, "N(S) sequence error: N(S)=%u, "
1496 "V(R)=%u\n", ns, dl->v_recv);
1497 /* discard data */
1498 msgb_free(msg);
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001499 if (dl->seq_err_cond != 1) {
rootaf48bed2011-09-26 11:23:06 +02001500 /* FIXME: help me understand what exactly todo here
rootaf48bed2011-09-26 11:23:06 +02001501 */
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001502 dl->seq_err_cond = 1;
rootaf48bed2011-09-26 11:23:06 +02001503 lapd_send_rej(lctx, lctx->p_f);
1504 } else {
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001505 /* If there are two subsequent sequence errors received,
1506 * ignore it. (Ignore every second subsequent error.)
1507 * This happens if our reply with the REJ is too slow,
1508 * so the remote gets a T200 timeout and sends another
1509 * frame with a sequence error.
1510 * Test showed that replying with two subsequent REJ
1511 * messages could the remote L2 process to abort.
1512 * Replying too slow shouldn't happen, but may happen
1513 * over serial link between BB and LAPD.
1514 */
1515 dl->seq_err_cond = 2;
rootaf48bed2011-09-26 11:23:06 +02001516 }
Andreas.Eversberg301f01e2012-01-10 13:02:01 +01001517 /* Even if N(s) sequence error, acknowledge to N(R)-1 */
1518 /* 5.5.3.1: Acknowlege all transmitted frames up the N(R)-1 */
1519 lapd_acknowledge(lctx); /* V(A) is also set here */
1520
1521 /* Send message, if possible due to acknowledged data */
1522 lapd_send_i(lctx, __LINE__);
1523
1524 return 0;
rootaf48bed2011-09-26 11:23:06 +02001525 }
1526 dl->seq_err_cond = 0;
1527
1528 /* Increment receiver state */
1529 dl->v_recv = inc_mod(dl->v_recv, dl->v_range);
1530 LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv);
1531
1532 /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */
1533 lapd_acknowledge(lctx); /* V(A) is also set here */
1534
1535 /* Only if we are not in own receiver busy condition */
1536 if (!dl->own_busy) {
1537 /* if the frame carries a complete segment */
1538 if (!lctx->more && !dl->rcv_buffer) {
1539 LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n");
1540 /* send a DATA INDICATION to L3 */
1541 msg->len = length;
1542 msg->tail = msg->data + length;
1543 rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx,
1544 msg);
1545 } else {
1546 /* create rcv_buffer */
1547 if (!dl->rcv_buffer) {
1548 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1549 "I frames (first message)\n");
1550 dl->rcv_buffer = lapd_msgb_alloc(dl->maxf,
1551 "LAPD RX");
1552 dl->rcv_buffer->l3h = dl->rcv_buffer->data;
1553 }
1554 /* concat. rcv_buffer */
1555 if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) {
1556 LOGP(DLLAPD, LOGL_NOTICE, "Received frame "
1557 "overflow!\n");
1558 } else {
1559 memcpy(msgb_put(dl->rcv_buffer, length),
1560 msg->l3h, length);
1561 }
1562 /* if the last segment was received */
1563 if (!lctx->more) {
1564 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1565 "I frames (last message)\n");
1566 rc = send_dl_l3(PRIM_DL_DATA,
1567 PRIM_OP_INDICATION, lctx,
1568 dl->rcv_buffer);
1569 dl->rcv_buffer = NULL;
1570 } else
1571 LOGP(DLLAPD, LOGL_INFO, "message in multiple "
1572 "I frames (next message)\n");
1573 msgb_free(msg);
1574
1575 }
1576 } else
1577 LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver "
1578 "busy condition\n");
1579
1580 /* Check for P bit */
1581 if (lctx->p_f) {
1582 /* 5.5.2.1 */
1583 /* check if we are not in own receiver busy */
1584 if (!dl->own_busy) {
1585 LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n");
1586 /* Send RR with F=1 */
1587 rc = lapd_send_rr(lctx, 1, 0);
1588 } else {
1589 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1590 /* Send RNR with F=1 */
1591 rc = lapd_send_rnr(lctx, 1, 0);
1592 }
1593 } else {
1594 /* 5.5.2.2 */
1595 /* check if we are not in own receiver busy */
1596 if (!dl->own_busy) {
1597 /* NOTE: V(R) is already set above */
1598 rc = lapd_send_i(lctx, __LINE__);
1599 if (rc) {
1600 LOGP(DLLAPD, LOGL_INFO, "we are not busy and "
1601 "have no pending data, send RR\n");
1602 /* Send RR with F=0 */
1603 return lapd_send_rr(lctx, 0, 0);
1604 }
1605 /* all I or one RR is sent, we are done */
1606 return 0;
1607 } else {
1608 LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n");
1609 /* Send RNR with F=0 */
1610 rc = lapd_send_rnr(lctx, 0, 0);
1611 }
1612 }
1613
1614 /* Send message, if possible due to acknowledged data */
1615 lapd_send_i(lctx, __LINE__);
1616
1617 return rc;
1618}
1619
1620/* Receive a LAPD message from L1 */
1621int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx)
1622{
1623 int rc;
1624
1625 switch (lctx->format) {
1626 case LAPD_FORM_U:
1627 rc = lapd_rx_u(msg, lctx);
1628 break;
1629 case LAPD_FORM_S:
1630 rc = lapd_rx_s(msg, lctx);
1631 break;
1632 case LAPD_FORM_I:
1633 rc = lapd_rx_i(msg, lctx);
1634 break;
1635 default:
1636 LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n");
1637 msgb_free(msg);
1638 rc = -EINVAL;
1639 }
1640 return rc;
1641}
1642
1643/* L3 -> L2 */
1644
1645/* send unit data */
1646static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1647{
1648 struct lapd_datalink *dl = lctx->dl;
1649 struct msgb *msg = dp->oph.msg;
1650 struct lapd_msg_ctx nctx;
1651
1652 memcpy(&nctx, lctx, sizeof(nctx));
1653 /* keep nctx.ldp */
1654 /* keep nctx.sapi */
1655 /* keep nctx.tei */
1656 nctx.cr = dl->cr.loc2rem.cmd;
1657 nctx.format = LAPD_FORM_U;
1658 nctx.s_u = LAPD_U_UI;
1659 /* keep nctx.p_f */
1660 nctx.length = msg->len;
1661 nctx.more = 0;
1662
1663 return dl->send_ph_data_req(&nctx, msg);
1664}
1665
1666/* request link establishment */
1667static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1668{
1669 struct lapd_datalink *dl = lctx->dl;
1670 struct msgb *msg = dp->oph.msg;
1671 struct lapd_msg_ctx nctx;
1672
1673 if (msg->len)
1674 LOGP(DLLAPD, LOGL_INFO, "perform establishment with content "
1675 "(SABM)\n");
1676 else
1677 LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n");
1678
1679 /* Flush send-queue */
1680 /* Clear send-buffer */
1681 lapd_dl_flush_send(dl);
1682 /* be sure that history is empty */
1683 lapd_dl_flush_hist(dl);
1684
1685 /* save message context for further use */
1686 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1687
1688 /* Discard partly received L3 message */
1689 if (dl->rcv_buffer) {
1690 msgb_free(dl->rcv_buffer);
1691 dl->rcv_buffer = NULL;
1692 }
1693
1694 /* assemble message */
1695 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1696 /* keep nctx.ldp */
1697 /* keep nctx.sapi */
1698 /* keep nctx.tei */
1699 nctx.cr = dl->cr.loc2rem.cmd;
1700 nctx.format = LAPD_FORM_U;
1701 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1702 nctx.p_f = 1;
1703 nctx.length = msg->len;
1704 nctx.more = 0;
1705
1706 /* Transmit-buffer carries exactly one segment */
1707 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1708 msgb_put(dl->tx_hist[0].msg, msg->len);
1709 if (msg->len)
1710 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1711 dl->tx_hist[0].more = 0;
1712 /* set Vs to 0, because it is used as index when resending SABM */
1713 dl->v_send = 0;
1714
1715 /* Set states */
1716 dl->own_busy = dl->peer_busy = 0;
1717 dl->retrans_ctr = 0;
1718 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1719
1720 /* Tramsmit and start T200 */
1721 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001722 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001723
1724 return 0;
1725}
1726
1727/* send data */
1728static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1729{
1730 struct lapd_datalink *dl = lctx->dl;
1731 struct msgb *msg = dp->oph.msg;
1732
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +08001733 if (msgb_l3len(msg) == 0) {
1734 LOGP(DLLAPD, LOGL_ERROR,
1735 "writing an empty message is not possible.\n");
1736 msgb_free(msg);
1737 return -1;
1738 }
1739
Holger Hans Peter Freyther6ecafef2012-01-13 05:46:26 +08001740 LOGP(DLLAPD, LOGL_INFO,
1741 "writing message to send-queue: l3len: %d\n", msgb_l3len(msg));
rootaf48bed2011-09-26 11:23:06 +02001742
1743 /* Write data into the send queue */
1744 msgb_enqueue(&dl->send_queue, msg);
1745
1746 /* Send message, if possible */
1747 lapd_send_i(&dl->lctx, __LINE__);
1748
1749 return 0;
1750}
1751
1752/* Send next I frame from queued/buffered data */
1753static int lapd_send_i(struct lapd_msg_ctx *lctx, int line)
1754{
1755 struct lapd_datalink *dl = lctx->dl;
1756 uint8_t k = dl->k;
1757 uint8_t h;
1758 struct msgb *msg;
1759 int length, left;
1760 int rc = - 1; /* we sent nothing */
1761 struct lapd_msg_ctx nctx;
1762
1763
1764 LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line);
1765
1766 next_frame:
1767
1768 if (dl->peer_busy) {
1769 LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n");
1770 return rc;
1771 }
1772
1773 if (dl->state == LAPD_STATE_TIMER_RECOV) {
1774 LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n");
1775 return rc;
1776 }
1777
1778 /* If the send state variable V(S) is equal to V(A) plus k
1779 * (where k is the maximum number of outstanding I frames - see
1780 * subclause 5.8.4), the data link layer entity shall not transmit any
1781 * new I frames, but shall retransmit an I frame as a result
1782 * of the error recovery procedures as described in subclauses 5.5.4 and
1783 * 5.5.7. */
1784 if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) {
1785 LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending "
1786 "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send,
1787 dl->v_ack);
1788 return rc;
1789 }
1790
1791 h = do_mod(dl->v_send, dl->range_hist);
1792
1793 /* if we have no tx_hist yet, we create it */
1794 if (!dl->tx_hist[h].msg) {
1795 /* Get next message into send-buffer, if any */
1796 if (!dl->send_buffer) {
1797 next_message:
1798 dl->send_out = 0;
1799 dl->send_buffer = msgb_dequeue(&dl->send_queue);
1800 /* No more data to be sent */
1801 if (!dl->send_buffer)
1802 return rc;
1803 LOGP(DLLAPD, LOGL_INFO, "get message from "
1804 "send-queue\n");
1805 }
1806
1807 /* How much is left in the send-buffer? */
1808 left = msgb_l3len(dl->send_buffer) - dl->send_out;
1809 /* Segment, if data exceeds N201 */
1810 length = left;
1811 if (length > lctx->n201)
1812 length = lctx->n201;
1813 LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d "
1814 "length %d first byte %02x\n",
1815 msgb_l3len(dl->send_buffer), dl->send_out, left,
1816 lctx->n201, length, dl->send_buffer->l3h[0]);
1817 /* If message in send-buffer is completely sent */
1818 if (left == 0) {
1819 msgb_free(dl->send_buffer);
1820 dl->send_buffer = NULL;
1821 goto next_message;
1822 }
1823
1824 LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n",
1825 (left > length) ? "segment " : "", dl->v_send);
1826
1827 /* Create I frame (segment) and transmit-buffer content */
1828 msg = lapd_msgb_alloc(length, "LAPD I");
1829 msg->l3h = msgb_put(msg, length);
1830 /* assemble message */
1831 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1832 /* keep nctx.ldp */
1833 /* keep nctx.sapi */
1834 /* keep nctx.tei */
1835 nctx.cr = dl->cr.loc2rem.cmd;
1836 nctx.format = LAPD_FORM_I;
1837 nctx.p_f = 0;
1838 nctx.n_send = dl->v_send;
1839 nctx.n_recv = dl->v_recv;
1840 nctx.length = length;
1841 if (left > length)
1842 nctx.more = 1;
1843 else
1844 nctx.more = 0;
1845 if (length)
1846 memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out,
1847 length);
1848 /* store in tx_hist */
1849 dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST");
1850 msgb_put(dl->tx_hist[h].msg, msg->len);
1851 if (length)
1852 memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len);
1853 dl->tx_hist[h].more = nctx.more;
1854 /* Add length to track how much is already in the tx buffer */
1855 dl->send_out += length;
1856 } else {
1857 LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer "
1858 "V(S)=%d\n", dl->v_send);
1859
1860 /* Create I frame (segment) from tx_hist */
1861 length = dl->tx_hist[h].msg->len;
1862 msg = lapd_msgb_alloc(length, "LAPD I resend");
1863 msg->l3h = msgb_put(msg, length);
1864 /* assemble message */
1865 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1866 /* keep nctx.ldp */
1867 /* keep nctx.sapi */
1868 /* keep nctx.tei */
1869 nctx.cr = dl->cr.loc2rem.cmd;
1870 nctx.format = LAPD_FORM_I;
1871 nctx.p_f = 0;
1872 nctx.n_send = dl->v_send;
1873 nctx.n_recv = dl->v_recv;
1874 nctx.length = length;
1875 nctx.more = dl->tx_hist[h].more;
1876 if (length)
1877 memcpy(msg->l3h, dl->tx_hist[h].msg->data, length);
1878 }
1879
1880 /* The value of the send state variable V(S) shall be incremented by 1
1881 * at the end of the transmission of the I frame */
1882 dl->v_send = inc_mod(dl->v_send, dl->v_range);
1883
1884 /* If timer T200 is not running at the time right before transmitting a
1885 * frame, when the PH-READY-TO-SEND primitive is received from the
1886 * physical layer., it shall be set. */
1887 if (!osmo_timer_pending(&dl->t200)) {
Andreas Eversberg742fc792011-09-27 09:40:25 +02001888 /* stop Timer T203, if running */
1889 lapd_stop_t203(dl);
1890 /* start Timer T200 */
1891 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001892 }
1893
1894 dl->send_ph_data_req(&nctx, msg);
1895
1896 rc = 0; /* we sent something */
1897 goto next_frame;
1898}
1899
1900/* request link suspension */
1901static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1902{
1903 struct lapd_datalink *dl = lctx->dl;
1904 struct msgb *msg = dp->oph.msg;
1905
1906 LOGP(DLLAPD, LOGL_INFO, "perform suspension\n");
1907
1908 /* put back the send-buffer to the send-queue (first position) */
1909 if (dl->send_buffer) {
1910 LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to "
1911 "queue\n");
1912 llist_add(&dl->send_buffer->list, &dl->send_queue);
1913 dl->send_buffer = NULL;
1914 } else
1915 LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n");
1916
1917 /* Clear transmit buffer, but keep send buffer */
1918 lapd_dl_flush_tx(dl);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001919 /* Stop timers (there is no state change, so we must stop all timers */
1920 lapd_stop_t200(dl);
1921 lapd_stop_t203(dl);
rootaf48bed2011-09-26 11:23:06 +02001922
1923 msgb_free(msg);
1924
1925 return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx);
1926}
1927
1928/* requesst resume or reconnect of link */
1929static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1930{
1931 struct lapd_datalink *dl = lctx->dl;
1932 struct msgb *msg = dp->oph.msg;
1933 struct lapd_msg_ctx nctx;
1934
1935 LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n",
1936 msg->len);
1937
1938 /* be sure that history is empty */
1939 lapd_dl_flush_hist(dl);
1940
1941 /* save message context for further use */
1942 memcpy(&dl->lctx, lctx, sizeof(dl->lctx));
1943
1944 /* Replace message in the send-buffer (reconnect) */
1945 if (dl->send_buffer)
1946 msgb_free(dl->send_buffer);
1947 dl->send_out = 0;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001948 if (msg && msg->len)
rootaf48bed2011-09-26 11:23:06 +02001949 /* Write data into the send buffer, to be sent first */
1950 dl->send_buffer = msg;
Andreas Eversberg5ad4ac82011-11-01 09:40:21 +01001951 else
1952 dl->send_buffer = NULL;
rootaf48bed2011-09-26 11:23:06 +02001953
1954 /* Discard partly received L3 message */
1955 if (dl->rcv_buffer) {
1956 msgb_free(dl->rcv_buffer);
1957 dl->rcv_buffer = NULL;
1958 }
1959
1960 /* Create new msgb (old one is now free) */
1961 msg = lapd_msgb_alloc(0, "LAPD SABM");
1962 msg->l3h = msg->data;
1963 /* assemble message */
1964 memcpy(&nctx, &dl->lctx, sizeof(nctx));
1965 /* keep nctx.ldp */
1966 /* keep nctx.sapi */
1967 /* keep nctx.tei */
1968 nctx.cr = dl->cr.loc2rem.cmd;
1969 nctx.format = LAPD_FORM_U;
1970 nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM;
1971 nctx.p_f = 1;
1972 nctx.length = 0;
1973 nctx.more = 0;
1974
1975 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
1976 msgb_put(dl->tx_hist[0].msg, msg->len);
1977 if (msg->len)
1978 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
1979 dl->tx_hist[0].more = 0;
1980 /* set Vs to 0, because it is used as index when resending SABM */
1981 dl->v_send = 0;
1982
1983 /* Set states */
1984 dl->own_busy = dl->peer_busy = 0;
1985 dl->retrans_ctr = 0;
1986 lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT);
1987
1988 /* Tramsmit and start T200 */
1989 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02001990 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02001991
1992 return 0;
1993}
1994
1995/* requesst release of link */
1996static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
1997{
1998 struct lapd_datalink *dl = lctx->dl;
1999 struct msgb *msg = dp->oph.msg;
2000 struct lapd_msg_ctx nctx;
2001
2002 /* local release */
2003 if (dp->u.rel_req.mode) {
2004 LOGP(DLLAPD, LOGL_INFO, "perform local release\n");
2005 msgb_free(msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002006 /* stop Timer T200 */
2007 lapd_stop_t200(dl);
2008 /* enter idle state, T203 is stopped here, if running */
rootaf48bed2011-09-26 11:23:06 +02002009 lapd_dl_newstate(dl, LAPD_STATE_IDLE);
2010 /* flush buffers */
2011 lapd_dl_flush_tx(dl);
2012 lapd_dl_flush_send(dl);
2013 /* send notification to L3 */
2014 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2015 }
2016
2017 /* in case we are already disconnecting */
2018 if (dl->state == LAPD_STATE_DISC_SENT)
2019 return -EBUSY;
2020
2021 /* flush tx_hist */
2022 lapd_dl_flush_hist(dl);
2023
2024 LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n");
2025
2026 /* Push LAPD header on msgb */
2027 /* assemble message */
2028 memcpy(&nctx, &dl->lctx, sizeof(nctx));
2029 /* keep nctx.ldp */
2030 /* keep nctx.sapi */
2031 /* keep nctx.tei */
2032 nctx.cr = dl->cr.loc2rem.cmd;
2033 nctx.format = LAPD_FORM_U;
2034 nctx.s_u = LAPD_U_DISC;
2035 nctx.p_f = 1;
2036 nctx.length = 0;
2037 nctx.more = 0;
2038
2039 dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST");
2040 msgb_put(dl->tx_hist[0].msg, msg->len);
2041 if (msg->len)
2042 memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len);
2043 dl->tx_hist[0].more = 0;
2044 /* set Vs to 0, because it is used as index when resending DISC */
2045 dl->v_send = 0;
2046
2047 /* Set states */
2048 dl->own_busy = dl->peer_busy = 0;
2049 dl->retrans_ctr = 0;
2050 lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT);
2051
2052 /* Tramsmit and start T200 */
2053 dl->send_ph_data_req(&nctx, msg);
Andreas Eversberg742fc792011-09-27 09:40:25 +02002054 lapd_start_t200(dl);
rootaf48bed2011-09-26 11:23:06 +02002055
2056 return 0;
2057}
2058
2059/* request release of link in idle state */
2060static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp,
2061 struct lapd_msg_ctx *lctx)
2062{
2063 struct lapd_datalink *dl = lctx->dl;
2064 struct msgb *msg = dp->oph.msg;
2065
2066 msgb_free(msg);
2067
2068 /* send notification to L3 */
2069 return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx);
2070}
2071
2072/* statefull handling for DL SAP messages from L3 */
Holger Hans Peter Freyther579fb092012-11-22 10:54:23 +01002073static const struct l2downstate {
rootaf48bed2011-09-26 11:23:06 +02002074 uint32_t states;
2075 int prim, op;
2076 const char *name;
2077 int (*rout) (struct osmo_dlsap_prim *dp,
2078 struct lapd_msg_ctx *lctx);
2079} l2downstatelist[] = {
2080 /* create and send UI command */
2081 {ALL_STATES,
2082 PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST,
2083 "DL-UNIT-DATA-REQUEST", lapd_udata_req},
2084
2085 /* create and send SABM command */
2086 {SBIT(LAPD_STATE_IDLE),
2087 PRIM_DL_EST, PRIM_OP_REQUEST,
2088 "DL-ESTABLISH-REQUEST", lapd_est_req},
2089
2090 /* create and send I command */
2091 {SBIT(LAPD_STATE_MF_EST) |
2092 SBIT(LAPD_STATE_TIMER_RECOV),
2093 PRIM_DL_DATA, PRIM_OP_REQUEST,
2094 "DL-DATA-REQUEST", lapd_data_req},
2095
2096 /* suspend datalink */
2097 {SBIT(LAPD_STATE_MF_EST) |
2098 SBIT(LAPD_STATE_TIMER_RECOV),
2099 PRIM_DL_SUSP, PRIM_OP_REQUEST,
2100 "DL-SUSPEND-REQUEST", lapd_susp_req},
2101
2102 /* create and send SABM command (resume) */
2103 {SBIT(LAPD_STATE_MF_EST) |
2104 SBIT(LAPD_STATE_TIMER_RECOV),
2105 PRIM_DL_RES, PRIM_OP_REQUEST,
2106 "DL-RESUME-REQUEST", lapd_res_req},
2107
2108 /* create and send SABM command (reconnect) */
2109 {SBIT(LAPD_STATE_IDLE) |
2110 SBIT(LAPD_STATE_MF_EST) |
2111 SBIT(LAPD_STATE_TIMER_RECOV),
2112 PRIM_DL_RECON, PRIM_OP_REQUEST,
2113 "DL-RECONNECT-REQUEST", lapd_res_req},
2114
2115 /* create and send DISC command */
2116 {SBIT(LAPD_STATE_SABM_SENT) |
2117 SBIT(LAPD_STATE_MF_EST) |
2118 SBIT(LAPD_STATE_TIMER_RECOV) |
2119 SBIT(LAPD_STATE_DISC_SENT),
2120 PRIM_DL_REL, PRIM_OP_REQUEST,
2121 "DL-RELEASE-REQUEST", lapd_rel_req},
2122
2123 /* release in idle state */
2124 {SBIT(LAPD_STATE_IDLE),
2125 PRIM_DL_REL, PRIM_OP_REQUEST,
2126 "DL-RELEASE-REQUEST", lapd_rel_req_idle},
2127};
2128
2129#define L2DOWNSLLEN \
2130 (sizeof(l2downstatelist) / sizeof(struct l2downstate))
2131
2132int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx)
2133{
2134 struct lapd_datalink *dl = lctx->dl;
2135 int i, supported = 0;
2136 struct msgb *msg = dp->oph.msg;
2137 int rc;
2138
2139 /* find function for current state and message */
2140 for (i = 0; i < L2DOWNSLLEN; i++) {
2141 if (dp->oph.primitive == l2downstatelist[i].prim
2142 && dp->oph.operation == l2downstatelist[i].op) {
2143 supported = 1;
2144 if ((SBIT(dl->state) & l2downstatelist[i].states))
2145 break;
2146 }
2147 }
2148 if (!supported) {
2149 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n",
2150 dp->oph.primitive, dp->oph.operation);
2151 msgb_free(msg);
2152 return 0;
2153 }
2154 if (i == L2DOWNSLLEN) {
2155 LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this "
2156 "state %s.\n", dp->oph.primitive, dp->oph.operation,
2157 lapd_state_names[dl->state]);
2158 msgb_free(msg);
2159 return 0;
2160 }
2161
2162 LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n",
2163 l2downstatelist[i].name, lapd_state_names[dl->state]);
2164
2165 rc = l2downstatelist[i].rout(dp, lctx);
2166
2167 return rc;
2168}
2169