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