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