Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_vc_fsm.c |
| 2 | * NS virtual circuit FSM implementation |
| 3 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 4 | * as well as its successor 3GPP TS 48.016 */ |
| 5 | |
| 6 | /* (C) 2020 sysmocom - s.f.m.c. GmbH |
| 7 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | /* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures |
| 29 | * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and |
| 30 | * associated weights. In theory, the BSS then uses this to establish a full mesh |
| 31 | * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports */ |
| 32 | |
| 33 | #include <errno.h> |
| 34 | |
| 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | |
| 38 | #include <osmocom/core/fsm.h> |
| 39 | #include <osmocom/core/msgb.h> |
| 40 | #include <osmocom/core/rate_ctr.h> |
| 41 | #include <osmocom/core/socket.h> |
| 42 | #include <osmocom/core/stat_item.h> |
| 43 | #include <osmocom/gsm/prim.h> |
| 44 | #include <osmocom/gsm/tlv.h> |
| 45 | #include <osmocom/gprs/gprs_msgb.h> |
| 46 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 47 | |
| 48 | #include "gprs_ns2_internal.h" |
| 49 | |
| 50 | #define S(x) (1 << (x)) |
| 51 | |
| 52 | #define DNS 10 |
| 53 | |
| 54 | struct gprs_ns2_vc_priv { |
| 55 | struct gprs_ns2_vc *nsvc; |
| 56 | /* how often the timer was triggered */ |
| 57 | int N; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 58 | /* The initiator is responsible to UNBLOCK the VC. The BSS is usually the initiator. |
| 59 | * It can change during runtime. The side which blocks an unblocked side.*/ |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame^] | 60 | bool initiator; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 61 | bool initiate_block; |
| 62 | bool initiate_reset; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 63 | /* if blocked by O&M/vty */ |
| 64 | bool om_blocked; |
| 65 | /* if unitdata is forwarded to the user */ |
| 66 | bool accept_unitdata; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 67 | |
| 68 | /* the alive counter is present in all states */ |
| 69 | struct { |
| 70 | struct osmo_timer_list timer; |
| 71 | enum ns2_timeout mode; |
| 72 | int N; |
| 73 | struct timeval timer_started; |
| 74 | } alive; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | /* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure.. |
| 79 | * |
| 80 | * With RESET/BLOCK, the state should follow: |
| 81 | * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED |
| 82 | * |
| 83 | * Without RESET/BLOCK, the state should follow: |
| 84 | * - UNCONFIGURED -> ALIVE -> UNBLOCKED |
| 85 | * |
| 86 | * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive. |
| 87 | * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU. |
| 88 | * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED. |
| 89 | * |
| 90 | * The ALIVE state is used as intermediate, because a VC is only valid if it received an Alive ACK when |
| 91 | * not using RESET/BLOCK procedure. |
| 92 | */ |
| 93 | |
| 94 | enum gprs_ns2_vc_state { |
| 95 | GPRS_NS2_ST_UNCONFIGURED, |
| 96 | GPRS_NS2_ST_RESET, |
| 97 | GPRS_NS2_ST_BLOCKED, |
| 98 | GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */ |
| 99 | |
| 100 | GPRS_NS2_ST_ALIVE, /* only used when not using RESET/BLOCK procedure */ |
| 101 | }; |
| 102 | |
| 103 | enum gprs_ns2_vc_event { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 104 | GPRS_NS2_EV_REQ_START, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 105 | |
| 106 | /* received messages */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 107 | GPRS_NS2_EV_RX_RESET, |
| 108 | GPRS_NS2_EV_RX_RESET_ACK, |
| 109 | GPRS_NS2_EV_RX_UNBLOCK, |
| 110 | GPRS_NS2_EV_RX_UNBLOCK_ACK, |
| 111 | GPRS_NS2_EV_RX_BLOCK, |
| 112 | GPRS_NS2_EV_RX_BLOCK_ACK, |
| 113 | GPRS_NS2_EV_RX_ALIVE, |
| 114 | GPRS_NS2_EV_RX_ALIVE_ACK, |
| 115 | GPRS_NS2_EV_RX_STATUS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 116 | |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 117 | GPRS_NS2_EV_RX_UNITDATA, |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 118 | |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 119 | GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */ |
| 120 | GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */ |
| 121 | GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static const struct value_string gprs_ns2_vc_event_names[] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 125 | { GPRS_NS2_EV_REQ_START, "START" }, |
| 126 | { GPRS_NS2_EV_RX_RESET, "RESET" }, |
| 127 | { GPRS_NS2_EV_RX_RESET_ACK, "RESET_ACK" }, |
| 128 | { GPRS_NS2_EV_RX_UNBLOCK, "UNBLOCK" }, |
| 129 | { GPRS_NS2_EV_RX_UNBLOCK_ACK, "UNBLOCK_ACK" }, |
| 130 | { GPRS_NS2_EV_RX_BLOCK, "BLOCK" }, |
| 131 | { GPRS_NS2_EV_RX_BLOCK_ACK, "BLOCK_ACK" }, |
| 132 | { GPRS_NS2_EV_RX_ALIVE, "ALIVE" }, |
| 133 | { GPRS_NS2_EV_RX_ALIVE_ACK, "ALIVE_ACK" }, |
| 134 | { GPRS_NS2_EV_RX_STATUS, "STATUS" }, |
| 135 | { GPRS_NS2_EV_RX_UNITDATA, "UNITDATA" }, |
| 136 | { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "FORCE_UNCONFIGURED" }, |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 137 | { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"}, |
| 138 | { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"}, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 139 | { 0, NULL } |
| 140 | }; |
| 141 | |
| 142 | static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi) |
| 143 | { |
| 144 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 145 | return priv->nsvc->nse->nsi; |
| 146 | } |
| 147 | |
| 148 | static void start_test_procedure(struct gprs_ns2_vc_priv *priv) |
| 149 | { |
| 150 | struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi; |
| 151 | |
| 152 | if (osmo_timer_pending(&priv->alive.timer)) |
| 153 | return; |
| 154 | |
| 155 | priv->alive.mode = NS_TOUT_TNS_ALIVE; |
| 156 | priv->alive.N = 0; |
| 157 | |
| 158 | osmo_gettimeofday(&priv->alive.timer_started, NULL); |
| 159 | ns2_tx_alive(priv->nsvc); |
| 160 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 161 | } |
| 162 | |
| 163 | static void stop_test_procedure(struct gprs_ns2_vc_priv *priv) |
| 164 | { |
| 165 | osmo_timer_del(&priv->alive.timer); |
| 166 | } |
| 167 | |
| 168 | static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv) |
| 169 | { |
| 170 | struct timeval now, elapsed; |
| 171 | osmo_gettimeofday(&now, NULL); |
| 172 | timersub(&now, &priv->alive.timer_started, &elapsed); |
| 173 | |
| 174 | return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000; |
| 175 | } |
| 176 | |
| 177 | static void recv_test_procedure(struct osmo_fsm_inst *fi) |
| 178 | { |
| 179 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 180 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 181 | struct gprs_ns2_vc *nsvc = priv->nsvc; |
| 182 | |
| 183 | /* ignoring ACKs without sending an ALIVE */ |
| 184 | if (priv->alive.mode != NS_TOUT_TNS_ALIVE) |
| 185 | return; |
| 186 | |
| 187 | priv->alive.mode = NS_TOUT_TNS_TEST; |
| 188 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0); |
| 189 | osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY], |
| 190 | alive_timer_elapsed_ms(priv)); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static void alive_timeout_handler(void *data) |
| 195 | { |
| 196 | struct osmo_fsm_inst *fi = data; |
| 197 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 198 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 199 | |
| 200 | switch (priv->alive.mode) { |
| 201 | case NS_TOUT_TNS_TEST: |
| 202 | priv->alive.mode = NS_TOUT_TNS_ALIVE; |
| 203 | ns2_tx_alive(priv->nsvc); |
| 204 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 205 | break; |
| 206 | case NS_TOUT_TNS_ALIVE: |
| 207 | priv->alive.N++; |
| 208 | |
| 209 | if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
| 210 | /* retransmission */ |
| 211 | ns2_tx_alive(priv->nsvc); |
| 212 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 213 | } else { |
| 214 | /* lost connection */ |
| 215 | if (priv->nsvc->mode == NS2_VC_MODE_BLOCKRESET) { |
| 216 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 217 | } else { |
| 218 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 219 | } |
| 220 | } |
| 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void gprs_ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 228 | { |
| 229 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 230 | struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi; |
| 231 | |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame^] | 232 | priv->initiate_reset = priv->initiate_block = priv->initiator; |
| 233 | priv->om_blocked = false; |
| 234 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 235 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 236 | case GPRS_NS2_EV_REQ_START: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 237 | switch (priv->nsvc->mode) { |
| 238 | case NS2_VC_MODE_ALIVE: |
| 239 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE); |
| 240 | break; |
| 241 | case NS2_VC_MODE_BLOCKRESET: |
| 242 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET); |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | break; |
| 247 | default: |
| 248 | OSMO_ASSERT(0); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static void gprs_ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 254 | { |
| 255 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 256 | |
| 257 | if (old_state != GPRS_NS2_ST_RESET) |
| 258 | priv->N = 0; |
| 259 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 260 | priv->accept_unitdata = false; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 261 | if (priv->initiate_reset) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 262 | ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 263 | |
| 264 | stop_test_procedure(priv); |
| 265 | ns2_nse_notify_unblocked(priv->nsvc, false); |
| 266 | } |
| 267 | |
| 268 | static void gprs_ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 269 | { |
| 270 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 271 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 272 | |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 273 | if (priv->initiate_reset) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 274 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 275 | case GPRS_NS2_EV_RX_RESET_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 276 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 277 | nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK); |
| 278 | break; |
| 279 | } |
| 280 | } else { |
| 281 | /* we are on the receiving end */ |
| 282 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 283 | case GPRS_NS2_EV_RX_RESET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 284 | ns2_tx_reset_ack(priv->nsvc); |
| 285 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 286 | 0, 0); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void gprs_ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 293 | { |
| 294 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 295 | |
| 296 | if (old_state != GPRS_NS2_ST_BLOCKED) |
| 297 | priv->N = 0; |
| 298 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 299 | if (priv->om_blocked) { |
| 300 | /* we are already blocked after a RESET */ |
| 301 | if (old_state == GPRS_NS2_ST_RESET) { |
| 302 | osmo_timer_del(&fi->timer); |
| 303 | } else { |
| 304 | ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 305 | } |
| 306 | } else if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 307 | ns2_tx_unblock(priv->nsvc); |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 308 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 309 | |
| 310 | start_test_procedure(priv); |
| 311 | } |
| 312 | |
| 313 | static void gprs_ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 314 | { |
| 315 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 316 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 317 | if (priv->om_blocked) { |
| 318 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 319 | case GPRS_NS2_EV_RX_BLOCK_ACK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 320 | priv->accept_unitdata = false; |
| 321 | osmo_timer_del(&fi->timer); |
| 322 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 323 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 324 | priv->accept_unitdata = false; |
| 325 | ns2_tx_block_ack(priv->nsvc); |
| 326 | osmo_timer_del(&fi->timer); |
| 327 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 328 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 329 | priv->accept_unitdata = false; |
| 330 | ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 331 | osmo_timer_add(&fi->timer); |
| 332 | break; |
| 333 | } |
| 334 | } else if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 335 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 336 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 337 | /* TODO: BLOCK is a UNBLOCK_NACK */ |
| 338 | ns2_tx_block_ack(priv->nsvc); |
| 339 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 340 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 341 | ns2_tx_unblock_ack(priv->nsvc); |
| 342 | /* fall through */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 343 | case GPRS_NS2_EV_RX_UNBLOCK_ACK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 344 | priv->accept_unitdata = true; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 345 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, |
| 346 | 0, NS_TOUT_TNS_TEST); |
| 347 | break; |
| 348 | } |
| 349 | } else { |
| 350 | /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */ |
| 351 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 352 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 856b94c | 2021-01-19 19:42:17 +0100 | [diff] [blame] | 353 | ns2_tx_block_ack(priv->nsvc); |
| 354 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 355 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 356 | ns2_tx_unblock_ack(priv->nsvc); |
| 357 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, |
| 358 | 0, 0); |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static void gprs_ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 365 | { |
| 366 | struct gprs_ns2_vc_priv *priv = fi->priv; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 367 | struct gprs_ns2_vc *nsvc = priv->nsvc; |
| 368 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 369 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 370 | priv->accept_unitdata = true; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 371 | ns2_nse_notify_unblocked(nsvc, true); |
| 372 | ns2_prim_status_ind(nse, nsvc, 0, NS_AFF_CAUSE_VC_RECOVERY); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void gprs_ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 376 | { |
| 377 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 378 | |
| 379 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 380 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | c4b7462 | 2021-01-10 20:44:26 +0100 | [diff] [blame] | 381 | ns2_tx_unblock_ack(priv->nsvc); |
| 382 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 383 | case GPRS_NS2_EV_RX_BLOCK: |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 384 | priv->initiate_block = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 385 | ns2_tx_block_ack(priv->nsvc); |
| 386 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 387 | 0, 2); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void gprs_ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 393 | { |
| 394 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 395 | case GPRS_NS2_EV_RX_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 396 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0); |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void gprs_ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 402 | { |
| 403 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 404 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 405 | |
| 406 | priv->alive.mode = NS_TOUT_TNS_TEST; |
| 407 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0); |
| 408 | |
| 409 | if (old_state != GPRS_NS2_ST_ALIVE) |
| 410 | priv->N = 0; |
| 411 | |
| 412 | ns2_tx_alive(priv->nsvc); |
| 413 | ns2_nse_notify_unblocked(priv->nsvc, false); |
| 414 | } |
| 415 | |
| 416 | static void gprs_ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state) |
| 417 | { |
| 418 | start_test_procedure(fi->priv); |
| 419 | } |
| 420 | |
| 421 | static const struct osmo_fsm_state gprs_ns2_vc_states[] = { |
| 422 | [GPRS_NS2_ST_UNCONFIGURED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 423 | .in_event_mask = S(GPRS_NS2_EV_REQ_START), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 424 | .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE), |
| 425 | .name = "UNCONFIGURED", |
| 426 | .action = gprs_ns2_st_unconfigured, |
| 427 | }, |
| 428 | [GPRS_NS2_ST_RESET] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 429 | .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 430 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 431 | S(GPRS_NS2_ST_BLOCKED) | |
| 432 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 433 | .name = "RESET", |
| 434 | .action = gprs_ns2_st_reset, |
| 435 | .onenter = gprs_ns2_st_reset_onenter, |
| 436 | }, |
| 437 | [GPRS_NS2_ST_BLOCKED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 438 | .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) | |
| 439 | S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 440 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
| 441 | S(GPRS_NS2_ST_UNBLOCKED) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 442 | S(GPRS_NS2_ST_BLOCKED) | |
| 443 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 444 | .name = "BLOCKED", |
| 445 | .action = gprs_ns2_st_blocked, |
| 446 | .onenter = gprs_ns2_st_blocked_onenter, |
| 447 | }, |
| 448 | [GPRS_NS2_ST_UNBLOCKED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 449 | .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) | |
| 450 | S(GPRS_NS2_EV_RX_UNBLOCK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 451 | .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 452 | S(GPRS_NS2_ST_BLOCKED) | |
| 453 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 454 | .name = "UNBLOCKED", |
| 455 | .action = gprs_ns2_st_unblocked, |
| 456 | .onenter = gprs_ns2_st_unblocked_on_enter, |
| 457 | }, |
| 458 | |
| 459 | /* ST_ALIVE is only used on VC without RESET/BLOCK */ |
| 460 | [GPRS_NS2_ST_ALIVE] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 461 | .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 462 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 463 | S(GPRS_NS2_ST_UNBLOCKED) | |
| 464 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 465 | .name = "ALIVE", |
| 466 | .action = gprs_ns2_st_alive, |
| 467 | .onenter = gprs_ns2_st_alive_onenter, |
| 468 | .onleave = gprs_ns2_st_alive_onleave, |
| 469 | }, |
| 470 | }; |
| 471 | |
| 472 | static int gprs_ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 473 | { |
| 474 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 475 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 476 | |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 477 | switch (fi->state) { |
| 478 | case GPRS_NS2_ST_RESET: |
| 479 | if (priv->initiate_reset) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 480 | priv->N++; |
| 481 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) { |
| 482 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 483 | } else { |
| 484 | priv->N = 0; |
| 485 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 486 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 487 | } |
| 488 | break; |
| 489 | case GPRS_NS2_ST_BLOCKED: |
| 490 | if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 491 | priv->N++; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 492 | if (priv->om_blocked) { |
| 493 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) { |
| 494 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 495 | } else { |
| 496 | /* 7.2 stop accepting data when BLOCK PDU not responded */ |
| 497 | priv->accept_unitdata = false; |
| 498 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 499 | } else { |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 500 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) { |
| 501 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 502 | } else { |
| 503 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 504 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 505 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 506 | } |
| 507 | break; |
| 508 | case GPRS_NS2_ST_ALIVE: |
| 509 | if (priv->initiate_reset) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 510 | priv->N++; |
| 511 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
| 512 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0); |
| 513 | } else { |
| 514 | priv->N = 0; |
| 515 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0); |
| 516 | } |
| 517 | break; |
| 518 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 519 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static void gprs_ns2_recv_unitdata(struct osmo_fsm_inst *fi, |
| 525 | struct msgb *msg) |
| 526 | { |
| 527 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 528 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 529 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 530 | struct osmo_gprs_ns2_prim nsp = {}; |
| 531 | uint16_t bvci; |
| 532 | |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 533 | if (msgb_l2len(msg) < sizeof(*nsh) + 3) { |
| 534 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 535 | return; |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 536 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 537 | |
| 538 | /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU |
| 539 | * for a PTP BVC may indicate a request to change the IP endpoint |
| 540 | * and/or a response to a change in the IP endpoint. */ |
| 541 | |
| 542 | /* TODO: nsh->data[0] -> C/R only valid in IP SNS */ |
| 543 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
| 544 | |
Alexander Couzens | 89acdef | 2020-09-23 18:22:31 +0200 | [diff] [blame] | 545 | msg->l3h = &nsh->data[3]; |
| 546 | nsp.bvci = bvci; |
| 547 | nsp.nsei = priv->nsvc->nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 548 | |
Alexander Couzens | c1cd333 | 2020-09-23 23:24:02 +0200 | [diff] [blame] | 549 | /* 10.3.9 NS SDU Control Bits */ |
| 550 | if (nsh->data[0] & 0x1) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 551 | nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE; |
| 552 | |
| 553 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 554 | PRIM_OP_INDICATION, msg); |
| 555 | nsi->cb(&nsp.oph, nsi->cb_data); |
| 556 | } |
| 557 | |
| 558 | static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi, |
| 559 | uint32_t event, |
| 560 | void *data) |
| 561 | { |
| 562 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 563 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 564 | struct msgb *msg = data; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 565 | |
| 566 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 567 | case GPRS_NS2_EV_RX_RESET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 568 | if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET) |
| 569 | break; |
| 570 | |
| 571 | /* move the FSM into reset */ |
| 572 | if (fi->state != GPRS_NS2_ST_RESET) { |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 573 | priv->initiate_reset = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 574 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET); |
| 575 | } |
| 576 | /* pass the event down into FSM action */ |
| 577 | gprs_ns2_st_reset(fi, event, data); |
| 578 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 579 | case GPRS_NS2_EV_RX_ALIVE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 580 | switch (fi->state) { |
| 581 | case GPRS_NS2_ST_UNCONFIGURED: |
| 582 | case GPRS_NS2_ST_RESET: |
| 583 | /* ignore ALIVE */ |
| 584 | break; |
| 585 | default: |
| 586 | ns2_tx_alive_ack(priv->nsvc); |
| 587 | } |
| 588 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 589 | case GPRS_NS2_EV_RX_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 590 | /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */ |
| 591 | if (fi->state == GPRS_NS2_ST_ALIVE) |
| 592 | gprs_ns2_st_alive(fi, event, data); |
| 593 | else |
| 594 | recv_test_procedure(fi); |
| 595 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 596 | case GPRS_NS2_EV_RX_UNITDATA: |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 597 | /* UNITDATA has to handle the release of msg. |
| 598 | * If send upwards (gprs_ns2_recv_unitdata) it must NOT free |
| 599 | * the msg, the upper layer has to do it. |
| 600 | * Otherwise the msg must be freed. |
| 601 | */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 602 | switch (fi->state) { |
| 603 | case GPRS_NS2_ST_BLOCKED: |
| 604 | /* 7.2.1: the BLOCKED_ACK might be lost */ |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 605 | if (priv->accept_unitdata) { |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 606 | gprs_ns2_recv_unitdata(fi, msg); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | ns2_tx_status(priv->nsvc, |
| 611 | NS_CAUSE_NSVC_BLOCKED, |
| 612 | 0, msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 613 | break; |
| 614 | /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */ |
| 615 | case GPRS_NS2_ST_ALIVE: |
| 616 | case GPRS_NS2_ST_UNBLOCKED: |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 617 | gprs_ns2_recv_unitdata(fi, msg); |
| 618 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 619 | } |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 620 | |
| 621 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 622 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 623 | case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED: |
Alexander Couzens | 6f3b738 | 2020-12-21 15:57:04 +0100 | [diff] [blame] | 624 | if (fi->state != GPRS_NS2_ST_UNCONFIGURED) { |
| 625 | /* Force the NSVC back to its initial state */ |
| 626 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0); |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 627 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_REQ_START, NULL); |
Alexander Couzens | 6f3b738 | 2020-12-21 15:57:04 +0100 | [diff] [blame] | 628 | return; |
| 629 | } |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 630 | break; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 631 | case GPRS_NS2_EV_REQ_OM_BLOCK: |
| 632 | /* vty cmd: block */ |
| 633 | priv->initiate_block = true; |
| 634 | priv->om_blocked = true; |
| 635 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 636 | break; |
| 637 | case GPRS_NS2_EV_REQ_OM_UNBLOCK: |
| 638 | /* vty cmd: unblock*/ |
| 639 | if (!priv->om_blocked) |
| 640 | return; |
| 641 | priv->om_blocked = false; |
| 642 | if (fi->state == GPRS_NS2_ST_BLOCKED) |
| 643 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 644 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Alexander Couzens | 0346b64 | 2020-10-27 13:05:56 +0100 | [diff] [blame] | 648 | static void gprs_ns2_vc_fsm_clean(struct osmo_fsm_inst *fi, |
| 649 | enum osmo_fsm_term_cause cause) |
| 650 | { |
| 651 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 652 | |
| 653 | osmo_timer_del(&priv->alive.timer); |
| 654 | } |
| 655 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 656 | static struct osmo_fsm gprs_ns2_vc_fsm = { |
| 657 | .name = "GPRS-NS2-VC", |
| 658 | .states = gprs_ns2_vc_states, |
| 659 | .num_states = ARRAY_SIZE(gprs_ns2_vc_states), |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 660 | .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) | |
| 661 | S(GPRS_NS2_EV_RX_RESET) | |
| 662 | S(GPRS_NS2_EV_RX_ALIVE) | |
| 663 | S(GPRS_NS2_EV_RX_ALIVE_ACK) | |
| 664 | S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 665 | S(GPRS_NS2_EV_REQ_OM_BLOCK) | |
| 666 | S(GPRS_NS2_EV_REQ_OM_UNBLOCK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 667 | .allstate_action = gprs_ns2_vc_fsm_allstate_action, |
Alexander Couzens | 0346b64 | 2020-10-27 13:05:56 +0100 | [diff] [blame] | 668 | .cleanup = gprs_ns2_vc_fsm_clean, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 669 | .timer_cb = gprs_ns2_vc_fsm_timer_cb, |
| 670 | /* .log_subsys = DNS, "is not constant" */ |
| 671 | .event_names = gprs_ns2_vc_event_names, |
| 672 | .pre_term = NULL, |
| 673 | .log_subsys = DLNS, |
| 674 | }; |
| 675 | |
| 676 | /*! |
| 677 | * \brief gprs_ns2_vc_fsm_alloc |
| 678 | * \param ctx |
| 679 | * \param vc |
| 680 | * \param id a char representation of the virtual curcuit |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 681 | * \param initiator initiator is the site which starts the connection. Usually the BSS. |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 682 | * \return NULL on error, otherwise the fsm |
| 683 | */ |
| 684 | struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc, |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 685 | const char *id, bool initiator) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 686 | { |
| 687 | struct osmo_fsm_inst *fi; |
| 688 | struct gprs_ns2_vc_priv *priv; |
| 689 | |
| 690 | fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id); |
| 691 | if (!fi) |
| 692 | return fi; |
| 693 | |
| 694 | nsvc->fi = fi; |
| 695 | priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv); |
| 696 | priv->nsvc = nsvc; |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame^] | 697 | priv->initiator = initiator; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 698 | |
| 699 | osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi); |
| 700 | |
| 701 | return fi; |
| 702 | } |
| 703 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 704 | /*! Start a NS-VC FSM. |
| 705 | * \param nsvc the virtual circuit |
| 706 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 707 | int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc) |
| 708 | { |
| 709 | /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */ |
| 710 | if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED) |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 711 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 715 | /*! Reset a NS-VC FSM. |
| 716 | * \param nsvc the virtual circuit |
| 717 | * \return 0 on success; negative on error */ |
| 718 | int gprs_ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc) |
| 719 | { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 720 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL); |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 723 | /*! Block a NS-VC. |
| 724 | * \param nsvc the virtual circuit |
| 725 | * \return 0 on success; negative on error */ |
| 726 | int ns2_vc_block(struct gprs_ns2_vc *nsvc) |
| 727 | { |
| 728 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL); |
| 729 | } |
| 730 | |
| 731 | /*! Unblock a NS-VC. |
| 732 | * \param nsvc the virtual circuit |
| 733 | * \return 0 on success; negative on error */ |
| 734 | int ns2_vc_unblock(struct gprs_ns2_vc *nsvc) |
| 735 | { |
| 736 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL); |
| 737 | } |
| 738 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 739 | /*! entry point for messages from the driver/VL |
| 740 | * \param nsvc virtual circuit on which the message was received |
| 741 | * \param msg message that was received |
| 742 | * \param tp parsed TLVs of the received message |
| 743 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 744 | int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp) |
| 745 | { |
| 746 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 747 | struct osmo_fsm_inst *fi = nsvc->fi; |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 748 | int rc = 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 749 | uint8_t cause; |
| 750 | |
| 751 | /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct, |
| 752 | * if not answer STATUS with "NS-VC unknown" */ |
| 753 | /* TODO: handle RESET with different VCI */ |
| 754 | /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */ |
| 755 | |
| 756 | if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) { |
| 757 | if (nsh->pdu_type != NS_PDUT_STATUS) { |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 758 | rc = ns2_tx_status(nsvc, cause, 0, msg); |
| 759 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | switch (nsh->pdu_type) { |
| 764 | case NS_PDUT_RESET: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 765 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 766 | break; |
| 767 | case NS_PDUT_RESET_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 768 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 769 | break; |
| 770 | case NS_PDUT_BLOCK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 771 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 772 | break; |
| 773 | case NS_PDUT_BLOCK_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 774 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 775 | break; |
| 776 | case NS_PDUT_UNBLOCK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 777 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 778 | break; |
| 779 | case NS_PDUT_UNBLOCK_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 780 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 781 | break; |
| 782 | case NS_PDUT_ALIVE: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 783 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 784 | break; |
| 785 | case NS_PDUT_ALIVE_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 786 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 787 | break; |
| 788 | case NS_PDUT_UNITDATA: |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 789 | /* UNITDATA have to free msg because it might send the msg layer upwards */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 790 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 791 | return 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 792 | default: |
| 793 | LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei, |
| 794 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 795 | return -EINVAL; |
| 796 | } |
| 797 | |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 798 | out: |
| 799 | msgb_free(msg); |
| 800 | |
| 801 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 804 | /*! is the given NS-VC unblocked? */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 805 | int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc) |
| 806 | { |
| 807 | return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED); |
| 808 | } |
| 809 | |
| 810 | /* initialize osmo_ctx on main tread */ |
| 811 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 812 | { |
| 813 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0); |
| 814 | } |