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