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