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