root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 109 | static void lapd_t200_cb(void *data); |
| 110 | static void lapd_t203_cb(void *data); |
| 111 | static int lapd_send_i(struct lapd_msg_ctx *lctx, int line); |
| 112 | static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx); |
| 113 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 114 | /* UTILITY FUNCTIONS */ |
| 115 | |
| 116 | struct 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 | |
| 124 | static inline uint8_t do_mod(uint8_t x, uint8_t m) |
| 125 | { |
| 126 | return x & (m - 1); |
| 127 | } |
| 128 | |
| 129 | static inline uint8_t inc_mod(uint8_t x, uint8_t m) |
| 130 | { |
| 131 | return (x + 1) & (m - 1); |
| 132 | } |
| 133 | |
| 134 | static inline uint8_t add_mod(uint8_t x, uint8_t y, uint8_t m) |
| 135 | { |
| 136 | return (x + y) & (m - 1); |
| 137 | } |
| 138 | |
| 139 | static 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 | |
| 144 | static 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 | |
| 159 | static 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 | |
| 171 | static 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 */ |
| 181 | const 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 194 | static 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 | |
| 202 | static 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 | |
| 210 | static 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 | |
| 218 | static 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 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 226 | static 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 233 | lapd_stop_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 234 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 243 | && state == LAPD_STATE_MF_EST && dl->state != LAPD_STATE_MF_EST) |
| 244 | lapd_start_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 245 | |
| 246 | dl->state = state; |
| 247 | } |
| 248 | |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 249 | static void *tall_lapd_ctx = NULL; |
| 250 | |
| 251 | /* init datalink instance and allocate history */ |
| 252 | void 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 */ |
| 303 | void 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 318 | /* stop Timers */ |
| 319 | lapd_stop_t200(dl); |
| 320 | lapd_stop_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* reset and de-allocate history buffer */ |
| 324 | void 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 */ |
| 333 | int 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 */ |
| 357 | static 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 */ |
| 368 | static 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 */ |
| 377 | static 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 */ |
| 390 | static 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 */ |
| 414 | static 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 */ |
| 435 | static 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 */ |
| 457 | static 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 */ |
| 479 | static 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 */ |
| 501 | static 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 */ |
| 533 | static 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 */ |
| 545 | static void lapd_t200_cb(void *data) |
| 546 | { |
| 547 | struct lapd_datalink *dl = data; |
| 548 | |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 549 | LOGP(DLLAPD, LOGL_INFO, "Timeout T200 (%p) state=%d\n", dl, |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 550 | (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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 576 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 577 | 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 600 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 601 | 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 655 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 656 | } 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 */ |
| 674 | static void lapd_t203_cb(void *data) |
| 675 | { |
| 676 | struct lapd_datalink *dl = data; |
| 677 | |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 678 | LOGP(DLLAPD, LOGL_INFO, "Timeout T203 (%p) state=%d\n", dl, |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 679 | (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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 702 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* 5.5.3.1: Common function to acknowlege frames up to the given N(R) value */ |
| 706 | static 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)) { |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 737 | t200_reset = 1; |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 738 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 739 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 755 | /* If T200 has been stopped by the receipt of an I, RR or RNR frame, |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 756 | * 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 762 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | /* This also does a restart, when I or S frame is received */ |
| 767 | |
| 768 | /* Stop T203, if running */ |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 769 | lapd_stop_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 770 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 774 | lapd_start_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | /* L1 -> L2 */ |
| 779 | |
| 780 | /* Receive a LAPD U (Unnumbered) message from L1 */ |
| 781 | static 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; |
| 785 | int rc; |
| 786 | 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 850 | /* stop Timer T200 */ |
| 851 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 852 | 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 957 | /* stop Timer T200 */ |
| 958 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 959 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 974 | /* stop timer T200 */ |
| 975 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 976 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1056 | /* stop Timer T200 */ |
| 1057 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1058 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1079 | /* stop Timer T200 */ |
| 1080 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1081 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1129 | /* stop Timer T200 */ |
| 1130 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1131 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1147 | /* stop Timer T200 */ |
| 1148 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1149 | /* 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 */ |
| 1200 | static 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1273 | /* stop Timer T200 */ |
| 1274 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1275 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1338 | /* stop Timer T200 */ |
| 1339 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1340 | /* 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 Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1381 | /* stop Timer T200 */ |
| 1382 | lapd_stop_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1383 | /* 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 */ |
| 1439 | static 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); |
| 1500 | if (!dl->seq_err_cond) { |
| 1501 | /* FIXME: help me understand what exactly todo here |
| 1502 | dl->seq_err_cond = 1; |
| 1503 | */ |
| 1504 | lapd_send_rej(lctx, lctx->p_f); |
| 1505 | } else { |
| 1506 | } |
| 1507 | return -EIO; |
| 1508 | } |
| 1509 | dl->seq_err_cond = 0; |
| 1510 | |
| 1511 | /* Increment receiver state */ |
| 1512 | dl->v_recv = inc_mod(dl->v_recv, dl->v_range); |
| 1513 | LOGP(DLLAPD, LOGL_INFO, "incrementing V(R) to %u\n", dl->v_recv); |
| 1514 | |
| 1515 | /* 5.5.3.1: Acknowlege all transmitted frames up the the N(R)-1 */ |
| 1516 | lapd_acknowledge(lctx); /* V(A) is also set here */ |
| 1517 | |
| 1518 | /* Only if we are not in own receiver busy condition */ |
| 1519 | if (!dl->own_busy) { |
| 1520 | /* if the frame carries a complete segment */ |
| 1521 | if (!lctx->more && !dl->rcv_buffer) { |
| 1522 | LOGP(DLLAPD, LOGL_INFO, "message in single I frame\n"); |
| 1523 | /* send a DATA INDICATION to L3 */ |
| 1524 | msg->len = length; |
| 1525 | msg->tail = msg->data + length; |
| 1526 | rc = send_dl_l3(PRIM_DL_DATA, PRIM_OP_INDICATION, lctx, |
| 1527 | msg); |
| 1528 | } else { |
| 1529 | /* create rcv_buffer */ |
| 1530 | if (!dl->rcv_buffer) { |
| 1531 | LOGP(DLLAPD, LOGL_INFO, "message in multiple " |
| 1532 | "I frames (first message)\n"); |
| 1533 | dl->rcv_buffer = lapd_msgb_alloc(dl->maxf, |
| 1534 | "LAPD RX"); |
| 1535 | dl->rcv_buffer->l3h = dl->rcv_buffer->data; |
| 1536 | } |
| 1537 | /* concat. rcv_buffer */ |
| 1538 | if (msgb_l3len(dl->rcv_buffer) + length > dl->maxf) { |
| 1539 | LOGP(DLLAPD, LOGL_NOTICE, "Received frame " |
| 1540 | "overflow!\n"); |
| 1541 | } else { |
| 1542 | memcpy(msgb_put(dl->rcv_buffer, length), |
| 1543 | msg->l3h, length); |
| 1544 | } |
| 1545 | /* if the last segment was received */ |
| 1546 | if (!lctx->more) { |
| 1547 | LOGP(DLLAPD, LOGL_INFO, "message in multiple " |
| 1548 | "I frames (last message)\n"); |
| 1549 | rc = send_dl_l3(PRIM_DL_DATA, |
| 1550 | PRIM_OP_INDICATION, lctx, |
| 1551 | dl->rcv_buffer); |
| 1552 | dl->rcv_buffer = NULL; |
| 1553 | } else |
| 1554 | LOGP(DLLAPD, LOGL_INFO, "message in multiple " |
| 1555 | "I frames (next message)\n"); |
| 1556 | msgb_free(msg); |
| 1557 | |
| 1558 | } |
| 1559 | } else |
| 1560 | LOGP(DLLAPD, LOGL_INFO, "I frame ignored during own receiver " |
| 1561 | "busy condition\n"); |
| 1562 | |
| 1563 | /* Check for P bit */ |
| 1564 | if (lctx->p_f) { |
| 1565 | /* 5.5.2.1 */ |
| 1566 | /* check if we are not in own receiver busy */ |
| 1567 | if (!dl->own_busy) { |
| 1568 | LOGP(DLLAPD, LOGL_INFO, "we are not busy, send RR\n"); |
| 1569 | /* Send RR with F=1 */ |
| 1570 | rc = lapd_send_rr(lctx, 1, 0); |
| 1571 | } else { |
| 1572 | LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n"); |
| 1573 | /* Send RNR with F=1 */ |
| 1574 | rc = lapd_send_rnr(lctx, 1, 0); |
| 1575 | } |
| 1576 | } else { |
| 1577 | /* 5.5.2.2 */ |
| 1578 | /* check if we are not in own receiver busy */ |
| 1579 | if (!dl->own_busy) { |
| 1580 | /* NOTE: V(R) is already set above */ |
| 1581 | rc = lapd_send_i(lctx, __LINE__); |
| 1582 | if (rc) { |
| 1583 | LOGP(DLLAPD, LOGL_INFO, "we are not busy and " |
| 1584 | "have no pending data, send RR\n"); |
| 1585 | /* Send RR with F=0 */ |
| 1586 | return lapd_send_rr(lctx, 0, 0); |
| 1587 | } |
| 1588 | /* all I or one RR is sent, we are done */ |
| 1589 | return 0; |
| 1590 | } else { |
| 1591 | LOGP(DLLAPD, LOGL_INFO, "we are busy, send RNR\n"); |
| 1592 | /* Send RNR with F=0 */ |
| 1593 | rc = lapd_send_rnr(lctx, 0, 0); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* Send message, if possible due to acknowledged data */ |
| 1598 | lapd_send_i(lctx, __LINE__); |
| 1599 | |
| 1600 | return rc; |
| 1601 | } |
| 1602 | |
| 1603 | /* Receive a LAPD message from L1 */ |
| 1604 | int lapd_ph_data_ind(struct msgb *msg, struct lapd_msg_ctx *lctx) |
| 1605 | { |
| 1606 | int rc; |
| 1607 | |
| 1608 | switch (lctx->format) { |
| 1609 | case LAPD_FORM_U: |
| 1610 | rc = lapd_rx_u(msg, lctx); |
| 1611 | break; |
| 1612 | case LAPD_FORM_S: |
| 1613 | rc = lapd_rx_s(msg, lctx); |
| 1614 | break; |
| 1615 | case LAPD_FORM_I: |
| 1616 | rc = lapd_rx_i(msg, lctx); |
| 1617 | break; |
| 1618 | default: |
| 1619 | LOGP(DLLAPD, LOGL_NOTICE, "unknown LAPD format\n"); |
| 1620 | msgb_free(msg); |
| 1621 | rc = -EINVAL; |
| 1622 | } |
| 1623 | return rc; |
| 1624 | } |
| 1625 | |
| 1626 | /* L3 -> L2 */ |
| 1627 | |
| 1628 | /* send unit data */ |
| 1629 | static int lapd_udata_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1630 | { |
| 1631 | struct lapd_datalink *dl = lctx->dl; |
| 1632 | struct msgb *msg = dp->oph.msg; |
| 1633 | struct lapd_msg_ctx nctx; |
| 1634 | |
| 1635 | memcpy(&nctx, lctx, sizeof(nctx)); |
| 1636 | /* keep nctx.ldp */ |
| 1637 | /* keep nctx.sapi */ |
| 1638 | /* keep nctx.tei */ |
| 1639 | nctx.cr = dl->cr.loc2rem.cmd; |
| 1640 | nctx.format = LAPD_FORM_U; |
| 1641 | nctx.s_u = LAPD_U_UI; |
| 1642 | /* keep nctx.p_f */ |
| 1643 | nctx.length = msg->len; |
| 1644 | nctx.more = 0; |
| 1645 | |
| 1646 | return dl->send_ph_data_req(&nctx, msg); |
| 1647 | } |
| 1648 | |
| 1649 | /* request link establishment */ |
| 1650 | static int lapd_est_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1651 | { |
| 1652 | struct lapd_datalink *dl = lctx->dl; |
| 1653 | struct msgb *msg = dp->oph.msg; |
| 1654 | struct lapd_msg_ctx nctx; |
| 1655 | |
| 1656 | if (msg->len) |
| 1657 | LOGP(DLLAPD, LOGL_INFO, "perform establishment with content " |
| 1658 | "(SABM)\n"); |
| 1659 | else |
| 1660 | LOGP(DLLAPD, LOGL_INFO, "perform normal establishm. (SABM)\n"); |
| 1661 | |
| 1662 | /* Flush send-queue */ |
| 1663 | /* Clear send-buffer */ |
| 1664 | lapd_dl_flush_send(dl); |
| 1665 | /* be sure that history is empty */ |
| 1666 | lapd_dl_flush_hist(dl); |
| 1667 | |
| 1668 | /* save message context for further use */ |
| 1669 | memcpy(&dl->lctx, lctx, sizeof(dl->lctx)); |
| 1670 | |
| 1671 | /* Discard partly received L3 message */ |
| 1672 | if (dl->rcv_buffer) { |
| 1673 | msgb_free(dl->rcv_buffer); |
| 1674 | dl->rcv_buffer = NULL; |
| 1675 | } |
| 1676 | |
| 1677 | /* assemble message */ |
| 1678 | memcpy(&nctx, &dl->lctx, sizeof(nctx)); |
| 1679 | /* keep nctx.ldp */ |
| 1680 | /* keep nctx.sapi */ |
| 1681 | /* keep nctx.tei */ |
| 1682 | nctx.cr = dl->cr.loc2rem.cmd; |
| 1683 | nctx.format = LAPD_FORM_U; |
| 1684 | nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM; |
| 1685 | nctx.p_f = 1; |
| 1686 | nctx.length = msg->len; |
| 1687 | nctx.more = 0; |
| 1688 | |
| 1689 | /* Transmit-buffer carries exactly one segment */ |
| 1690 | dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); |
| 1691 | msgb_put(dl->tx_hist[0].msg, msg->len); |
| 1692 | if (msg->len) |
| 1693 | memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); |
| 1694 | dl->tx_hist[0].more = 0; |
| 1695 | /* set Vs to 0, because it is used as index when resending SABM */ |
| 1696 | dl->v_send = 0; |
| 1697 | |
| 1698 | /* Set states */ |
| 1699 | dl->own_busy = dl->peer_busy = 0; |
| 1700 | dl->retrans_ctr = 0; |
| 1701 | lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT); |
| 1702 | |
| 1703 | /* Tramsmit and start T200 */ |
| 1704 | dl->send_ph_data_req(&nctx, msg); |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1705 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1706 | |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /* send data */ |
| 1711 | static int lapd_data_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1712 | { |
| 1713 | struct lapd_datalink *dl = lctx->dl; |
| 1714 | struct msgb *msg = dp->oph.msg; |
| 1715 | |
| 1716 | LOGP(DLLAPD, LOGL_INFO, "writing message to send-queue\n"); |
| 1717 | |
| 1718 | /* Write data into the send queue */ |
| 1719 | msgb_enqueue(&dl->send_queue, msg); |
| 1720 | |
| 1721 | /* Send message, if possible */ |
| 1722 | lapd_send_i(&dl->lctx, __LINE__); |
| 1723 | |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | /* Send next I frame from queued/buffered data */ |
| 1728 | static int lapd_send_i(struct lapd_msg_ctx *lctx, int line) |
| 1729 | { |
| 1730 | struct lapd_datalink *dl = lctx->dl; |
| 1731 | uint8_t k = dl->k; |
| 1732 | uint8_t h; |
| 1733 | struct msgb *msg; |
| 1734 | int length, left; |
| 1735 | int rc = - 1; /* we sent nothing */ |
| 1736 | struct lapd_msg_ctx nctx; |
| 1737 | |
| 1738 | |
| 1739 | LOGP(DLLAPD, LOGL_INFO, "%s() called from line %d\n", __func__, line); |
| 1740 | |
| 1741 | next_frame: |
| 1742 | |
| 1743 | if (dl->peer_busy) { |
| 1744 | LOGP(DLLAPD, LOGL_INFO, "peer busy, not sending\n"); |
| 1745 | return rc; |
| 1746 | } |
| 1747 | |
| 1748 | if (dl->state == LAPD_STATE_TIMER_RECOV) { |
| 1749 | LOGP(DLLAPD, LOGL_INFO, "timer recovery, not sending\n"); |
| 1750 | return rc; |
| 1751 | } |
| 1752 | |
| 1753 | /* If the send state variable V(S) is equal to V(A) plus k |
| 1754 | * (where k is the maximum number of outstanding I frames - see |
| 1755 | * subclause 5.8.4), the data link layer entity shall not transmit any |
| 1756 | * new I frames, but shall retransmit an I frame as a result |
| 1757 | * of the error recovery procedures as described in subclauses 5.5.4 and |
| 1758 | * 5.5.7. */ |
| 1759 | if (dl->v_send == add_mod(dl->v_ack, k, dl->v_range)) { |
| 1760 | LOGP(DLLAPD, LOGL_INFO, "k frames outstanding, not sending " |
| 1761 | "more (k=%u V(S)=%u V(A)=%u)\n", k, dl->v_send, |
| 1762 | dl->v_ack); |
| 1763 | return rc; |
| 1764 | } |
| 1765 | |
| 1766 | h = do_mod(dl->v_send, dl->range_hist); |
| 1767 | |
| 1768 | /* if we have no tx_hist yet, we create it */ |
| 1769 | if (!dl->tx_hist[h].msg) { |
| 1770 | /* Get next message into send-buffer, if any */ |
| 1771 | if (!dl->send_buffer) { |
| 1772 | next_message: |
| 1773 | dl->send_out = 0; |
| 1774 | dl->send_buffer = msgb_dequeue(&dl->send_queue); |
| 1775 | /* No more data to be sent */ |
| 1776 | if (!dl->send_buffer) |
| 1777 | return rc; |
| 1778 | LOGP(DLLAPD, LOGL_INFO, "get message from " |
| 1779 | "send-queue\n"); |
| 1780 | } |
| 1781 | |
| 1782 | /* How much is left in the send-buffer? */ |
| 1783 | left = msgb_l3len(dl->send_buffer) - dl->send_out; |
| 1784 | /* Segment, if data exceeds N201 */ |
| 1785 | length = left; |
| 1786 | if (length > lctx->n201) |
| 1787 | length = lctx->n201; |
| 1788 | LOGP(DLLAPD, LOGL_INFO, "msg-len %d sent %d left %d N201 %d " |
| 1789 | "length %d first byte %02x\n", |
| 1790 | msgb_l3len(dl->send_buffer), dl->send_out, left, |
| 1791 | lctx->n201, length, dl->send_buffer->l3h[0]); |
| 1792 | /* If message in send-buffer is completely sent */ |
| 1793 | if (left == 0) { |
| 1794 | msgb_free(dl->send_buffer); |
| 1795 | dl->send_buffer = NULL; |
| 1796 | goto next_message; |
| 1797 | } |
| 1798 | |
| 1799 | LOGP(DLLAPD, LOGL_INFO, "send I frame %sV(S)=%d\n", |
| 1800 | (left > length) ? "segment " : "", dl->v_send); |
| 1801 | |
| 1802 | /* Create I frame (segment) and transmit-buffer content */ |
| 1803 | msg = lapd_msgb_alloc(length, "LAPD I"); |
| 1804 | msg->l3h = msgb_put(msg, length); |
| 1805 | /* assemble message */ |
| 1806 | memcpy(&nctx, &dl->lctx, sizeof(nctx)); |
| 1807 | /* keep nctx.ldp */ |
| 1808 | /* keep nctx.sapi */ |
| 1809 | /* keep nctx.tei */ |
| 1810 | nctx.cr = dl->cr.loc2rem.cmd; |
| 1811 | nctx.format = LAPD_FORM_I; |
| 1812 | nctx.p_f = 0; |
| 1813 | nctx.n_send = dl->v_send; |
| 1814 | nctx.n_recv = dl->v_recv; |
| 1815 | nctx.length = length; |
| 1816 | if (left > length) |
| 1817 | nctx.more = 1; |
| 1818 | else |
| 1819 | nctx.more = 0; |
| 1820 | if (length) |
| 1821 | memcpy(msg->l3h, dl->send_buffer->l3h + dl->send_out, |
| 1822 | length); |
| 1823 | /* store in tx_hist */ |
| 1824 | dl->tx_hist[h].msg = lapd_msgb_alloc(msg->len, "HIST"); |
| 1825 | msgb_put(dl->tx_hist[h].msg, msg->len); |
| 1826 | if (length) |
| 1827 | memcpy(dl->tx_hist[h].msg->data, msg->l3h, msg->len); |
| 1828 | dl->tx_hist[h].more = nctx.more; |
| 1829 | /* Add length to track how much is already in the tx buffer */ |
| 1830 | dl->send_out += length; |
| 1831 | } else { |
| 1832 | LOGP(DLLAPD, LOGL_INFO, "resend I frame from tx buffer " |
| 1833 | "V(S)=%d\n", dl->v_send); |
| 1834 | |
| 1835 | /* Create I frame (segment) from tx_hist */ |
| 1836 | length = dl->tx_hist[h].msg->len; |
| 1837 | msg = lapd_msgb_alloc(length, "LAPD I resend"); |
| 1838 | msg->l3h = msgb_put(msg, length); |
| 1839 | /* assemble message */ |
| 1840 | memcpy(&nctx, &dl->lctx, sizeof(nctx)); |
| 1841 | /* keep nctx.ldp */ |
| 1842 | /* keep nctx.sapi */ |
| 1843 | /* keep nctx.tei */ |
| 1844 | nctx.cr = dl->cr.loc2rem.cmd; |
| 1845 | nctx.format = LAPD_FORM_I; |
| 1846 | nctx.p_f = 0; |
| 1847 | nctx.n_send = dl->v_send; |
| 1848 | nctx.n_recv = dl->v_recv; |
| 1849 | nctx.length = length; |
| 1850 | nctx.more = dl->tx_hist[h].more; |
| 1851 | if (length) |
| 1852 | memcpy(msg->l3h, dl->tx_hist[h].msg->data, length); |
| 1853 | } |
| 1854 | |
| 1855 | /* The value of the send state variable V(S) shall be incremented by 1 |
| 1856 | * at the end of the transmission of the I frame */ |
| 1857 | dl->v_send = inc_mod(dl->v_send, dl->v_range); |
| 1858 | |
| 1859 | /* If timer T200 is not running at the time right before transmitting a |
| 1860 | * frame, when the PH-READY-TO-SEND primitive is received from the |
| 1861 | * physical layer., it shall be set. */ |
| 1862 | if (!osmo_timer_pending(&dl->t200)) { |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1863 | /* stop Timer T203, if running */ |
| 1864 | lapd_stop_t203(dl); |
| 1865 | /* start Timer T200 */ |
| 1866 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | dl->send_ph_data_req(&nctx, msg); |
| 1870 | |
| 1871 | rc = 0; /* we sent something */ |
| 1872 | goto next_frame; |
| 1873 | } |
| 1874 | |
| 1875 | /* request link suspension */ |
| 1876 | static int lapd_susp_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1877 | { |
| 1878 | struct lapd_datalink *dl = lctx->dl; |
| 1879 | struct msgb *msg = dp->oph.msg; |
| 1880 | |
| 1881 | LOGP(DLLAPD, LOGL_INFO, "perform suspension\n"); |
| 1882 | |
| 1883 | /* put back the send-buffer to the send-queue (first position) */ |
| 1884 | if (dl->send_buffer) { |
| 1885 | LOGP(DLLAPD, LOGL_INFO, "put frame in sendbuffer back to " |
| 1886 | "queue\n"); |
| 1887 | llist_add(&dl->send_buffer->list, &dl->send_queue); |
| 1888 | dl->send_buffer = NULL; |
| 1889 | } else |
| 1890 | LOGP(DLLAPD, LOGL_INFO, "no frame in sendbuffer\n"); |
| 1891 | |
| 1892 | /* Clear transmit buffer, but keep send buffer */ |
| 1893 | lapd_dl_flush_tx(dl); |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1894 | /* Stop timers (there is no state change, so we must stop all timers */ |
| 1895 | lapd_stop_t200(dl); |
| 1896 | lapd_stop_t203(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1897 | |
| 1898 | msgb_free(msg); |
| 1899 | |
| 1900 | return send_dl_simple(PRIM_DL_SUSP, PRIM_OP_CONFIRM, &dl->lctx); |
| 1901 | } |
| 1902 | |
| 1903 | /* requesst resume or reconnect of link */ |
| 1904 | static int lapd_res_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1905 | { |
| 1906 | struct lapd_datalink *dl = lctx->dl; |
| 1907 | struct msgb *msg = dp->oph.msg; |
| 1908 | struct lapd_msg_ctx nctx; |
| 1909 | |
| 1910 | LOGP(DLLAPD, LOGL_INFO, "perform re-establishment (SABM) length=%d\n", |
| 1911 | msg->len); |
| 1912 | |
| 1913 | /* be sure that history is empty */ |
| 1914 | lapd_dl_flush_hist(dl); |
| 1915 | |
| 1916 | /* save message context for further use */ |
| 1917 | memcpy(&dl->lctx, lctx, sizeof(dl->lctx)); |
| 1918 | |
| 1919 | /* Replace message in the send-buffer (reconnect) */ |
| 1920 | if (dl->send_buffer) |
| 1921 | msgb_free(dl->send_buffer); |
| 1922 | dl->send_out = 0; |
Andreas Eversberg | 5ad4ac8 | 2011-11-01 09:40:21 +0100 | [diff] [blame] | 1923 | if (msg && msg->len) |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1924 | /* Write data into the send buffer, to be sent first */ |
| 1925 | dl->send_buffer = msg; |
Andreas Eversberg | 5ad4ac8 | 2011-11-01 09:40:21 +0100 | [diff] [blame] | 1926 | else |
| 1927 | dl->send_buffer = NULL; |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1928 | |
| 1929 | /* Discard partly received L3 message */ |
| 1930 | if (dl->rcv_buffer) { |
| 1931 | msgb_free(dl->rcv_buffer); |
| 1932 | dl->rcv_buffer = NULL; |
| 1933 | } |
| 1934 | |
| 1935 | /* Create new msgb (old one is now free) */ |
| 1936 | msg = lapd_msgb_alloc(0, "LAPD SABM"); |
| 1937 | msg->l3h = msg->data; |
| 1938 | /* assemble message */ |
| 1939 | memcpy(&nctx, &dl->lctx, sizeof(nctx)); |
| 1940 | /* keep nctx.ldp */ |
| 1941 | /* keep nctx.sapi */ |
| 1942 | /* keep nctx.tei */ |
| 1943 | nctx.cr = dl->cr.loc2rem.cmd; |
| 1944 | nctx.format = LAPD_FORM_U; |
| 1945 | nctx.s_u = (dl->use_sabme) ? LAPD_U_SABME : LAPD_U_SABM; |
| 1946 | nctx.p_f = 1; |
| 1947 | nctx.length = 0; |
| 1948 | nctx.more = 0; |
| 1949 | |
| 1950 | dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); |
| 1951 | msgb_put(dl->tx_hist[0].msg, msg->len); |
| 1952 | if (msg->len) |
| 1953 | memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); |
| 1954 | dl->tx_hist[0].more = 0; |
| 1955 | /* set Vs to 0, because it is used as index when resending SABM */ |
| 1956 | dl->v_send = 0; |
| 1957 | |
| 1958 | /* Set states */ |
| 1959 | dl->own_busy = dl->peer_busy = 0; |
| 1960 | dl->retrans_ctr = 0; |
| 1961 | lapd_dl_newstate(dl, LAPD_STATE_SABM_SENT); |
| 1962 | |
| 1963 | /* Tramsmit and start T200 */ |
| 1964 | dl->send_ph_data_req(&nctx, msg); |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1965 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1966 | |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | /* requesst release of link */ |
| 1971 | static int lapd_rel_req(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 1972 | { |
| 1973 | struct lapd_datalink *dl = lctx->dl; |
| 1974 | struct msgb *msg = dp->oph.msg; |
| 1975 | struct lapd_msg_ctx nctx; |
| 1976 | |
| 1977 | /* local release */ |
| 1978 | if (dp->u.rel_req.mode) { |
| 1979 | LOGP(DLLAPD, LOGL_INFO, "perform local release\n"); |
| 1980 | msgb_free(msg); |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 1981 | /* stop Timer T200 */ |
| 1982 | lapd_stop_t200(dl); |
| 1983 | /* enter idle state, T203 is stopped here, if running */ |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 1984 | lapd_dl_newstate(dl, LAPD_STATE_IDLE); |
| 1985 | /* flush buffers */ |
| 1986 | lapd_dl_flush_tx(dl); |
| 1987 | lapd_dl_flush_send(dl); |
| 1988 | /* send notification to L3 */ |
| 1989 | return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx); |
| 1990 | } |
| 1991 | |
| 1992 | /* in case we are already disconnecting */ |
| 1993 | if (dl->state == LAPD_STATE_DISC_SENT) |
| 1994 | return -EBUSY; |
| 1995 | |
| 1996 | /* flush tx_hist */ |
| 1997 | lapd_dl_flush_hist(dl); |
| 1998 | |
| 1999 | LOGP(DLLAPD, LOGL_INFO, "perform normal release (DISC)\n"); |
| 2000 | |
| 2001 | /* Push LAPD header on msgb */ |
| 2002 | /* assemble message */ |
| 2003 | memcpy(&nctx, &dl->lctx, sizeof(nctx)); |
| 2004 | /* keep nctx.ldp */ |
| 2005 | /* keep nctx.sapi */ |
| 2006 | /* keep nctx.tei */ |
| 2007 | nctx.cr = dl->cr.loc2rem.cmd; |
| 2008 | nctx.format = LAPD_FORM_U; |
| 2009 | nctx.s_u = LAPD_U_DISC; |
| 2010 | nctx.p_f = 1; |
| 2011 | nctx.length = 0; |
| 2012 | nctx.more = 0; |
| 2013 | |
| 2014 | dl->tx_hist[0].msg = lapd_msgb_alloc(msg->len, "HIST"); |
| 2015 | msgb_put(dl->tx_hist[0].msg, msg->len); |
| 2016 | if (msg->len) |
| 2017 | memcpy(dl->tx_hist[0].msg->data, msg->l3h, msg->len); |
| 2018 | dl->tx_hist[0].more = 0; |
| 2019 | /* set Vs to 0, because it is used as index when resending DISC */ |
| 2020 | dl->v_send = 0; |
| 2021 | |
| 2022 | /* Set states */ |
| 2023 | dl->own_busy = dl->peer_busy = 0; |
| 2024 | dl->retrans_ctr = 0; |
| 2025 | lapd_dl_newstate(dl, LAPD_STATE_DISC_SENT); |
| 2026 | |
| 2027 | /* Tramsmit and start T200 */ |
| 2028 | dl->send_ph_data_req(&nctx, msg); |
Andreas Eversberg | 742fc79 | 2011-09-27 09:40:25 +0200 | [diff] [blame] | 2029 | lapd_start_t200(dl); |
root | af48bed | 2011-09-26 11:23:06 +0200 | [diff] [blame] | 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | /* request release of link in idle state */ |
| 2035 | static int lapd_rel_req_idle(struct osmo_dlsap_prim *dp, |
| 2036 | struct lapd_msg_ctx *lctx) |
| 2037 | { |
| 2038 | struct lapd_datalink *dl = lctx->dl; |
| 2039 | struct msgb *msg = dp->oph.msg; |
| 2040 | |
| 2041 | msgb_free(msg); |
| 2042 | |
| 2043 | /* send notification to L3 */ |
| 2044 | return send_dl_simple(PRIM_DL_REL, PRIM_OP_CONFIRM, &dl->lctx); |
| 2045 | } |
| 2046 | |
| 2047 | /* statefull handling for DL SAP messages from L3 */ |
| 2048 | static struct l2downstate { |
| 2049 | uint32_t states; |
| 2050 | int prim, op; |
| 2051 | const char *name; |
| 2052 | int (*rout) (struct osmo_dlsap_prim *dp, |
| 2053 | struct lapd_msg_ctx *lctx); |
| 2054 | } l2downstatelist[] = { |
| 2055 | /* create and send UI command */ |
| 2056 | {ALL_STATES, |
| 2057 | PRIM_DL_UNIT_DATA, PRIM_OP_REQUEST, |
| 2058 | "DL-UNIT-DATA-REQUEST", lapd_udata_req}, |
| 2059 | |
| 2060 | /* create and send SABM command */ |
| 2061 | {SBIT(LAPD_STATE_IDLE), |
| 2062 | PRIM_DL_EST, PRIM_OP_REQUEST, |
| 2063 | "DL-ESTABLISH-REQUEST", lapd_est_req}, |
| 2064 | |
| 2065 | /* create and send I command */ |
| 2066 | {SBIT(LAPD_STATE_MF_EST) | |
| 2067 | SBIT(LAPD_STATE_TIMER_RECOV), |
| 2068 | PRIM_DL_DATA, PRIM_OP_REQUEST, |
| 2069 | "DL-DATA-REQUEST", lapd_data_req}, |
| 2070 | |
| 2071 | /* suspend datalink */ |
| 2072 | {SBIT(LAPD_STATE_MF_EST) | |
| 2073 | SBIT(LAPD_STATE_TIMER_RECOV), |
| 2074 | PRIM_DL_SUSP, PRIM_OP_REQUEST, |
| 2075 | "DL-SUSPEND-REQUEST", lapd_susp_req}, |
| 2076 | |
| 2077 | /* create and send SABM command (resume) */ |
| 2078 | {SBIT(LAPD_STATE_MF_EST) | |
| 2079 | SBIT(LAPD_STATE_TIMER_RECOV), |
| 2080 | PRIM_DL_RES, PRIM_OP_REQUEST, |
| 2081 | "DL-RESUME-REQUEST", lapd_res_req}, |
| 2082 | |
| 2083 | /* create and send SABM command (reconnect) */ |
| 2084 | {SBIT(LAPD_STATE_IDLE) | |
| 2085 | SBIT(LAPD_STATE_MF_EST) | |
| 2086 | SBIT(LAPD_STATE_TIMER_RECOV), |
| 2087 | PRIM_DL_RECON, PRIM_OP_REQUEST, |
| 2088 | "DL-RECONNECT-REQUEST", lapd_res_req}, |
| 2089 | |
| 2090 | /* create and send DISC command */ |
| 2091 | {SBIT(LAPD_STATE_SABM_SENT) | |
| 2092 | SBIT(LAPD_STATE_MF_EST) | |
| 2093 | SBIT(LAPD_STATE_TIMER_RECOV) | |
| 2094 | SBIT(LAPD_STATE_DISC_SENT), |
| 2095 | PRIM_DL_REL, PRIM_OP_REQUEST, |
| 2096 | "DL-RELEASE-REQUEST", lapd_rel_req}, |
| 2097 | |
| 2098 | /* release in idle state */ |
| 2099 | {SBIT(LAPD_STATE_IDLE), |
| 2100 | PRIM_DL_REL, PRIM_OP_REQUEST, |
| 2101 | "DL-RELEASE-REQUEST", lapd_rel_req_idle}, |
| 2102 | }; |
| 2103 | |
| 2104 | #define L2DOWNSLLEN \ |
| 2105 | (sizeof(l2downstatelist) / sizeof(struct l2downstate)) |
| 2106 | |
| 2107 | int lapd_recv_dlsap(struct osmo_dlsap_prim *dp, struct lapd_msg_ctx *lctx) |
| 2108 | { |
| 2109 | struct lapd_datalink *dl = lctx->dl; |
| 2110 | int i, supported = 0; |
| 2111 | struct msgb *msg = dp->oph.msg; |
| 2112 | int rc; |
| 2113 | |
| 2114 | /* find function for current state and message */ |
| 2115 | for (i = 0; i < L2DOWNSLLEN; i++) { |
| 2116 | if (dp->oph.primitive == l2downstatelist[i].prim |
| 2117 | && dp->oph.operation == l2downstatelist[i].op) { |
| 2118 | supported = 1; |
| 2119 | if ((SBIT(dl->state) & l2downstatelist[i].states)) |
| 2120 | break; |
| 2121 | } |
| 2122 | } |
| 2123 | if (!supported) { |
| 2124 | LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unsupported.\n", |
| 2125 | dp->oph.primitive, dp->oph.operation); |
| 2126 | msgb_free(msg); |
| 2127 | return 0; |
| 2128 | } |
| 2129 | if (i == L2DOWNSLLEN) { |
| 2130 | LOGP(DLLAPD, LOGL_NOTICE, "Message %u/%u unhandled at this " |
| 2131 | "state %s.\n", dp->oph.primitive, dp->oph.operation, |
| 2132 | lapd_state_names[dl->state]); |
| 2133 | msgb_free(msg); |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | LOGP(DLLAPD, LOGL_INFO, "Message %s received in state %s\n", |
| 2138 | l2downstatelist[i].name, lapd_state_names[dl->state]); |
| 2139 | |
| 2140 | rc = l2downstatelist[i].rout(dp, lctx); |
| 2141 | |
| 2142 | return rc; |
| 2143 | } |
| 2144 | |