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