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 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 52 | struct gprs_ns2_vc_priv { |
| 53 | struct gprs_ns2_vc *nsvc; |
| 54 | /* how often the timer was triggered */ |
| 55 | int N; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 56 | /* The initiator is responsible to UNBLOCK the VC. The BSS is usually the initiator. |
| 57 | * It can change during runtime. The side which blocks an unblocked side.*/ |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame] | 58 | bool initiator; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 59 | bool initiate_block; |
| 60 | bool initiate_reset; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 61 | /* if blocked by O&M/vty */ |
| 62 | bool om_blocked; |
| 63 | /* if unitdata is forwarded to the user */ |
| 64 | bool accept_unitdata; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 65 | |
| 66 | /* the alive counter is present in all states */ |
| 67 | struct { |
| 68 | struct osmo_timer_list timer; |
| 69 | enum ns2_timeout mode; |
| 70 | int N; |
Alexander Couzens | ab0e864 | 2021-02-12 02:50:44 +0100 | [diff] [blame] | 71 | struct timespec timer_started; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 72 | } alive; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | /* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure.. |
| 77 | * |
| 78 | * With RESET/BLOCK, the state should follow: |
| 79 | * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED |
| 80 | * |
| 81 | * Without RESET/BLOCK, the state should follow: |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 82 | * - UNCONFIGURED -> RECOVERY -> UNBLOCKED |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 83 | * |
| 84 | * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive. |
| 85 | * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU. |
| 86 | * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED. |
| 87 | * |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 88 | * The RECOVERY state is used as intermediate, because a VC is only valid if it received an Alive ACK when |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 89 | * not using RESET/BLOCK procedure. |
| 90 | */ |
| 91 | |
| 92 | enum gprs_ns2_vc_state { |
| 93 | GPRS_NS2_ST_UNCONFIGURED, |
| 94 | GPRS_NS2_ST_RESET, |
| 95 | GPRS_NS2_ST_BLOCKED, |
| 96 | GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */ |
| 97 | |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 98 | GPRS_NS2_ST_RECOVERING, /* only used when not using RESET/BLOCK procedure */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | enum gprs_ns2_vc_event { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 102 | GPRS_NS2_EV_REQ_START, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 103 | |
| 104 | /* received messages */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 105 | GPRS_NS2_EV_RX_RESET, |
| 106 | GPRS_NS2_EV_RX_RESET_ACK, |
| 107 | GPRS_NS2_EV_RX_UNBLOCK, |
| 108 | GPRS_NS2_EV_RX_UNBLOCK_ACK, |
| 109 | GPRS_NS2_EV_RX_BLOCK, |
| 110 | GPRS_NS2_EV_RX_BLOCK_ACK, |
| 111 | GPRS_NS2_EV_RX_ALIVE, |
| 112 | GPRS_NS2_EV_RX_ALIVE_ACK, |
| 113 | GPRS_NS2_EV_RX_STATUS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 114 | |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 115 | GPRS_NS2_EV_RX_UNITDATA, |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 116 | |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 117 | GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */ |
Alexander Couzens | 27e5873 | 2021-03-21 17:12:08 +0100 | [diff] [blame] | 118 | GPRS_NS2_EV_REQ_OM_RESET, /* vty cmd: reset */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 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 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 123 | static const struct value_string ns2_vc_event_names[] = { |
Harald Welte | b339cbb | 2021-02-05 14:49:21 +0100 | [diff] [blame] | 124 | { GPRS_NS2_EV_REQ_START, "REQ-START" }, |
| 125 | { GPRS_NS2_EV_RX_RESET, "RX-RESET" }, |
| 126 | { GPRS_NS2_EV_RX_RESET_ACK, "RX-RESET_ACK" }, |
| 127 | { GPRS_NS2_EV_RX_UNBLOCK, "RX-UNBLOCK" }, |
| 128 | { GPRS_NS2_EV_RX_UNBLOCK_ACK, "RX-UNBLOCK_ACK" }, |
| 129 | { GPRS_NS2_EV_RX_BLOCK, "RX-BLOCK" }, |
| 130 | { GPRS_NS2_EV_RX_BLOCK_ACK, "RX-BLOCK_ACK" }, |
| 131 | { GPRS_NS2_EV_RX_ALIVE, "RX-ALIVE" }, |
| 132 | { GPRS_NS2_EV_RX_ALIVE_ACK, "RX-ALIVE_ACK" }, |
| 133 | { GPRS_NS2_EV_RX_STATUS, "RX-STATUS" }, |
| 134 | { GPRS_NS2_EV_RX_UNITDATA, "RX-UNITDATA" }, |
| 135 | { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "REQ-FORCE_UNCONFIGURED" }, |
Alexander Couzens | 27e5873 | 2021-03-21 17:12:08 +0100 | [diff] [blame] | 136 | { GPRS_NS2_EV_REQ_OM_RESET, "REQ-O&M-RESET"}, |
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 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 148 | /* Start the NS-TEST procedure, either with transmitting a tx_alive, |
| 149 | * (start_tx_alive==true) or with starting tns-test */ |
| 150 | static void start_test_procedure(struct osmo_fsm_inst *fi, bool start_tx_alive) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 151 | { |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 152 | struct gprs_ns2_vc_priv *priv = fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 153 | struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi; |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 154 | unsigned int tout_idx; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 155 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 156 | if (osmo_timer_pending(&priv->alive.timer)) { |
| 157 | if (start_tx_alive) { |
| 158 | if (priv->alive.mode == NS_TOUT_TNS_ALIVE) |
| 159 | return; |
| 160 | } else { |
| 161 | if (priv->alive.mode == NS_TOUT_TNS_TEST) |
| 162 | return; |
| 163 | } |
| 164 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 165 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 166 | priv->alive.N = 0; |
| 167 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 168 | if (start_tx_alive) { |
| 169 | priv->alive.mode = NS_TOUT_TNS_ALIVE; |
| 170 | osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started); |
| 171 | ns2_tx_alive(priv->nsvc); |
| 172 | tout_idx = NS_TOUT_TNS_ALIVE; |
| 173 | } else { |
| 174 | priv->alive.mode = NS_TOUT_TNS_TEST; |
| 175 | tout_idx = NS_TOUT_TNS_TEST; |
| 176 | } |
| 177 | LOGPFSML(fi, LOGL_DEBUG, "Starting Tns-%s of %u seconds\n", |
| 178 | tout_idx == NS_TOUT_TNS_ALIVE ? "alive" : "test", nsi->timeout[tout_idx]); |
| 179 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[tout_idx], 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void stop_test_procedure(struct gprs_ns2_vc_priv *priv) |
| 183 | { |
| 184 | osmo_timer_del(&priv->alive.timer); |
| 185 | } |
| 186 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 187 | /* how many milliseconds have expired since the last alive timer start? */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 188 | static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv) |
| 189 | { |
Alexander Couzens | ab0e864 | 2021-02-12 02:50:44 +0100 | [diff] [blame] | 190 | struct timespec now, elapsed; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 191 | |
Alexander Couzens | ab0e864 | 2021-02-12 02:50:44 +0100 | [diff] [blame] | 192 | if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) |
| 193 | return 0; |
| 194 | |
| 195 | timespecsub(&now, &priv->alive.timer_started, &elapsed); |
Alexander Couzens | ab0e864 | 2021-02-12 02:50:44 +0100 | [diff] [blame] | 196 | return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 199 | /* we just received a NS-ALIVE-ACK; re-schedule after Tns-test */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 200 | static void recv_test_procedure(struct osmo_fsm_inst *fi) |
| 201 | { |
| 202 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 203 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 204 | struct gprs_ns2_vc *nsvc = priv->nsvc; |
| 205 | |
| 206 | /* ignoring ACKs without sending an ALIVE */ |
| 207 | if (priv->alive.mode != NS_TOUT_TNS_ALIVE) |
| 208 | return; |
| 209 | |
| 210 | priv->alive.mode = NS_TOUT_TNS_TEST; |
| 211 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0); |
| 212 | osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY], |
| 213 | alive_timer_elapsed_ms(priv)); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static void alive_timeout_handler(void *data) |
| 218 | { |
| 219 | struct osmo_fsm_inst *fi = data; |
| 220 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 221 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 222 | |
| 223 | switch (priv->alive.mode) { |
| 224 | case NS_TOUT_TNS_TEST: |
| 225 | priv->alive.mode = NS_TOUT_TNS_ALIVE; |
Alexander Couzens | 2f8f7b6 | 2021-02-03 11:04:22 +0100 | [diff] [blame] | 226 | priv->alive.N = 0; |
Alexander Couzens | fa4121d | 2021-02-12 02:54:46 +0100 | [diff] [blame] | 227 | osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 228 | ns2_tx_alive(priv->nsvc); |
| 229 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 230 | break; |
| 231 | case NS_TOUT_TNS_ALIVE: |
Harald Welte | 4c54f59 | 2021-01-30 22:42:15 +0100 | [diff] [blame] | 232 | rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 233 | priv->alive.N++; |
| 234 | |
| 235 | if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
| 236 | /* retransmission */ |
| 237 | ns2_tx_alive(priv->nsvc); |
| 238 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
| 239 | } else { |
| 240 | /* lost connection */ |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 241 | if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 242 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 243 | } else { |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 244 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | break; |
| 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
Harald Welte | 6a9ec42 | 2021-01-31 18:56:29 +0100 | [diff] [blame] | 253 | |
| 254 | static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 255 | { |
| 256 | stop_test_procedure(fi->priv); |
| 257 | } |
| 258 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 259 | static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 260 | { |
| 261 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 262 | struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi; |
| 263 | |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame] | 264 | priv->initiate_reset = priv->initiate_block = priv->initiator; |
| 265 | priv->om_blocked = false; |
| 266 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 267 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 268 | case GPRS_NS2_EV_REQ_START: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 269 | switch (priv->nsvc->mode) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 270 | case GPRS_NS2_VC_MODE_ALIVE: |
Harald Welte | c51ddf2 | 2021-03-05 09:48:18 +0100 | [diff] [blame] | 271 | if (priv->nsvc->nse->dialect == GPRS_NS2_DIALECT_SNS) { |
| 272 | /* In IP-SNS, the NS-VC are assumed initially alive, until the alive |
| 273 | * procedure should fail at some future point */ |
| 274 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0); |
| 275 | } else { |
| 276 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE); |
| 277 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 278 | break; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 279 | case GPRS_NS2_VC_MODE_BLOCKRESET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 280 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET); |
| 281 | break; |
| 282 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 283 | break; |
| 284 | default: |
| 285 | OSMO_ASSERT(0); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 290 | static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 291 | { |
| 292 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 293 | |
| 294 | if (old_state != GPRS_NS2_ST_RESET) |
| 295 | priv->N = 0; |
| 296 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 297 | priv->accept_unitdata = false; |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 298 | if (priv->initiate_reset) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 299 | ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 300 | |
| 301 | stop_test_procedure(priv); |
| 302 | ns2_nse_notify_unblocked(priv->nsvc, false); |
| 303 | } |
| 304 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 305 | static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 306 | { |
| 307 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 308 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 309 | |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 310 | if (priv->initiate_reset) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 311 | switch (event) { |
Alexander Couzens | 273f063 | 2021-01-20 13:11:26 +0100 | [diff] [blame] | 312 | case GPRS_NS2_EV_RX_RESET: |
| 313 | ns2_tx_reset_ack(priv->nsvc); |
| 314 | /* fall-through */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 315 | case GPRS_NS2_EV_RX_RESET_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 316 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 317 | nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK); |
| 318 | break; |
| 319 | } |
| 320 | } else { |
| 321 | /* we are on the receiving end */ |
| 322 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 323 | case GPRS_NS2_EV_RX_RESET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 324 | ns2_tx_reset_ack(priv->nsvc); |
| 325 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 326 | 0, 0); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 332 | static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 333 | { |
| 334 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 335 | |
Harald Welte | 4c54f59 | 2021-01-30 22:42:15 +0100 | [diff] [blame] | 336 | if (old_state != GPRS_NS2_ST_BLOCKED) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 337 | priv->N = 0; |
Harald Welte | 4c54f59 | 2021-01-30 22:42:15 +0100 | [diff] [blame] | 338 | rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
| 339 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 340 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 341 | if (priv->om_blocked) { |
| 342 | /* we are already blocked after a RESET */ |
| 343 | if (old_state == GPRS_NS2_ST_RESET) { |
| 344 | osmo_timer_del(&fi->timer); |
| 345 | } else { |
| 346 | ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 347 | } |
| 348 | } else if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 349 | ns2_tx_unblock(priv->nsvc); |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 350 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 351 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 352 | start_test_procedure(fi, true); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 355 | static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 356 | { |
| 357 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 358 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 359 | if (priv->om_blocked) { |
| 360 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 361 | case GPRS_NS2_EV_RX_BLOCK_ACK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 362 | priv->accept_unitdata = false; |
| 363 | osmo_timer_del(&fi->timer); |
| 364 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 365 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 366 | priv->accept_unitdata = false; |
| 367 | ns2_tx_block_ack(priv->nsvc); |
| 368 | osmo_timer_del(&fi->timer); |
| 369 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 370 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 371 | priv->accept_unitdata = false; |
| 372 | ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION); |
| 373 | osmo_timer_add(&fi->timer); |
| 374 | break; |
| 375 | } |
| 376 | } else if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 377 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 378 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 379 | /* TODO: BLOCK is a UNBLOCK_NACK */ |
| 380 | ns2_tx_block_ack(priv->nsvc); |
| 381 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 382 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 383 | ns2_tx_unblock_ack(priv->nsvc); |
| 384 | /* fall through */ |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 385 | case GPRS_NS2_EV_RX_UNBLOCK_ACK: |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 386 | priv->accept_unitdata = true; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 387 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, |
| 388 | 0, NS_TOUT_TNS_TEST); |
| 389 | break; |
| 390 | } |
| 391 | } else { |
| 392 | /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */ |
| 393 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 394 | case GPRS_NS2_EV_RX_BLOCK: |
Alexander Couzens | 856b94c | 2021-01-19 19:42:17 +0100 | [diff] [blame] | 395 | ns2_tx_block_ack(priv->nsvc); |
| 396 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 397 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 398 | ns2_tx_unblock_ack(priv->nsvc); |
| 399 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, |
| 400 | 0, 0); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 406 | static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 407 | { |
| 408 | struct gprs_ns2_vc_priv *priv = fi->priv; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 409 | struct gprs_ns2_vc *nsvc = priv->nsvc; |
| 410 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 411 | |
Harald Welte | b40bf8b | 2021-01-30 22:43:01 +0100 | [diff] [blame] | 412 | if (old_state != GPRS_NS2_ST_UNBLOCKED) |
| 413 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]); |
| 414 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 415 | priv->accept_unitdata = true; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 416 | ns2_nse_notify_unblocked(nsvc, true); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 417 | ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY); |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 418 | |
| 419 | /* the closest interpretation of the spec would start Tns-test here first, |
| 420 | * and only send a NS-ALIVE after Tns-test has expired (i.e. setting the |
| 421 | * second argument to 'false'. However, being quick in detecting unavailability |
| 422 | * of a NS-VC seems like a good idea */ |
| 423 | start_test_procedure(fi, true); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 426 | static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 427 | { |
| 428 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 429 | |
| 430 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 431 | case GPRS_NS2_EV_RX_UNBLOCK: |
Alexander Couzens | c4b7462 | 2021-01-10 20:44:26 +0100 | [diff] [blame] | 432 | ns2_tx_unblock_ack(priv->nsvc); |
| 433 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 434 | case GPRS_NS2_EV_RX_BLOCK: |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 435 | priv->initiate_block = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 436 | ns2_tx_block_ack(priv->nsvc); |
| 437 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, |
| 438 | 0, 2); |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 443 | static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 444 | { |
| 445 | switch (event) { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 446 | case GPRS_NS2_EV_RX_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 447 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0); |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 452 | static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 453 | { |
| 454 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 455 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 456 | |
| 457 | priv->alive.mode = NS_TOUT_TNS_TEST; |
| 458 | osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0); |
| 459 | |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 460 | if (old_state != GPRS_NS2_ST_RECOVERING) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 461 | priv->N = 0; |
| 462 | |
Harald Welte | 5e40831 | 2021-03-23 18:16:30 +0100 | [diff] [blame] | 463 | start_test_procedure(fi, true); |
Alexander Couzens | 5b72247 | 2021-04-01 15:36:54 +0200 | [diff] [blame] | 464 | ns2_nse_notify_unblocked(priv->nsvc, false); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 467 | static const struct osmo_fsm_state ns2_vc_states[] = { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 468 | [GPRS_NS2_ST_UNCONFIGURED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 469 | .in_event_mask = S(GPRS_NS2_EV_REQ_START), |
Harald Welte | c51ddf2 | 2021-03-05 09:48:18 +0100 | [diff] [blame] | 470 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
| 471 | S(GPRS_NS2_ST_RECOVERING) | |
| 472 | S(GPRS_NS2_ST_UNBLOCKED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 473 | .name = "UNCONFIGURED", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 474 | .action = ns2_st_unconfigured, |
Harald Welte | 6a9ec42 | 2021-01-31 18:56:29 +0100 | [diff] [blame] | 475 | .onenter = ns2_st_unconfigured_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 476 | }, |
| 477 | [GPRS_NS2_ST_RESET] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 478 | .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] | 479 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 480 | S(GPRS_NS2_ST_BLOCKED) | |
| 481 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 482 | .name = "RESET", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 483 | .action = ns2_st_reset, |
| 484 | .onenter = ns2_st_reset_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 485 | }, |
| 486 | [GPRS_NS2_ST_BLOCKED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 487 | .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) | |
| 488 | 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] | 489 | .out_state_mask = S(GPRS_NS2_ST_RESET) | |
| 490 | S(GPRS_NS2_ST_UNBLOCKED) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 491 | S(GPRS_NS2_ST_BLOCKED) | |
| 492 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 493 | .name = "BLOCKED", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 494 | .action = ns2_st_blocked, |
| 495 | .onenter = ns2_st_blocked_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 496 | }, |
| 497 | [GPRS_NS2_ST_UNBLOCKED] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 498 | .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) | |
| 499 | S(GPRS_NS2_EV_RX_UNBLOCK), |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 500 | .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 501 | S(GPRS_NS2_ST_BLOCKED) | |
| 502 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 503 | .name = "UNBLOCKED", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 504 | .action = ns2_st_unblocked, |
| 505 | .onenter = ns2_st_unblocked_on_enter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 506 | }, |
| 507 | |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 508 | /* ST_RECOVERING is only used on VC without RESET/BLOCK */ |
| 509 | [GPRS_NS2_ST_RECOVERING] = { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 510 | .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK), |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 511 | .out_state_mask = S(GPRS_NS2_ST_RECOVERING) | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 512 | S(GPRS_NS2_ST_UNBLOCKED) | |
| 513 | S(GPRS_NS2_ST_UNCONFIGURED), |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 514 | .name = "RECOVERING", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 515 | .action = ns2_st_alive, |
| 516 | .onenter = ns2_st_alive_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 517 | }, |
| 518 | }; |
| 519 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 520 | static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 521 | { |
| 522 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 523 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 524 | |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 525 | switch (fi->state) { |
| 526 | case GPRS_NS2_ST_RESET: |
| 527 | if (priv->initiate_reset) { |
Harald Welte | 4c54f59 | 2021-01-30 22:42:15 +0100 | [diff] [blame] | 528 | rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 529 | priv->N++; |
| 530 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) { |
| 531 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 532 | } else { |
| 533 | priv->N = 0; |
| 534 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 535 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 536 | } |
| 537 | break; |
| 538 | case GPRS_NS2_ST_BLOCKED: |
| 539 | if (priv->initiate_block) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 540 | priv->N++; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 541 | if (priv->om_blocked) { |
| 542 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) { |
| 543 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 544 | } else { |
| 545 | /* 7.2 stop accepting data when BLOCK PDU not responded */ |
| 546 | priv->accept_unitdata = false; |
| 547 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 548 | } else { |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 549 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) { |
| 550 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 551 | } else { |
| 552 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0); |
| 553 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 554 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 555 | } |
| 556 | break; |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 557 | case GPRS_NS2_ST_RECOVERING: |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 558 | if (priv->initiate_reset) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 559 | priv->N++; |
| 560 | if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 561 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 562 | } else { |
| 563 | priv->N = 0; |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 564 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 565 | } |
| 566 | break; |
| 567 | } |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 568 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 573 | static void ns2_recv_unitdata(struct osmo_fsm_inst *fi, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 574 | struct msgb *msg) |
| 575 | { |
| 576 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 577 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
| 578 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 579 | struct osmo_gprs_ns2_prim nsp = {}; |
| 580 | uint16_t bvci; |
| 581 | |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 582 | if (msgb_l2len(msg) < sizeof(*nsh) + 3) { |
| 583 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 584 | return; |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 585 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 586 | |
| 587 | /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU |
| 588 | * for a PTP BVC may indicate a request to change the IP endpoint |
| 589 | * and/or a response to a change in the IP endpoint. */ |
| 590 | |
| 591 | /* TODO: nsh->data[0] -> C/R only valid in IP SNS */ |
| 592 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
| 593 | |
Alexander Couzens | 89acdef | 2020-09-23 18:22:31 +0200 | [diff] [blame] | 594 | msg->l3h = &nsh->data[3]; |
| 595 | nsp.bvci = bvci; |
| 596 | nsp.nsei = priv->nsvc->nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 597 | |
Alexander Couzens | c1cd333 | 2020-09-23 23:24:02 +0200 | [diff] [blame] | 598 | /* 10.3.9 NS SDU Control Bits */ |
| 599 | if (nsh->data[0] & 0x1) |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 600 | nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 601 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 602 | osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 603 | PRIM_OP_INDICATION, msg); |
| 604 | nsi->cb(&nsp.oph, nsi->cb_data); |
| 605 | } |
| 606 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 607 | static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 608 | uint32_t event, |
| 609 | void *data) |
| 610 | { |
| 611 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 612 | struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 613 | struct msgb *msg = data; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 614 | |
| 615 | switch (event) { |
Alexander Couzens | 27e5873 | 2021-03-21 17:12:08 +0100 | [diff] [blame] | 616 | case GPRS_NS2_EV_REQ_OM_RESET: |
| 617 | if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET) |
| 618 | break; |
| 619 | /* move the FSM into reset */ |
| 620 | if (fi->state != GPRS_NS2_ST_RESET) { |
| 621 | priv->initiate_reset = true; |
| 622 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET); |
| 623 | } |
| 624 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 625 | case GPRS_NS2_EV_RX_RESET: |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 626 | if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | /* move the FSM into reset */ |
| 630 | if (fi->state != GPRS_NS2_ST_RESET) { |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 631 | priv->initiate_reset = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 632 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET); |
| 633 | } |
| 634 | /* pass the event down into FSM action */ |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 635 | ns2_st_reset(fi, event, data); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 636 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 637 | case GPRS_NS2_EV_RX_ALIVE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 638 | switch (fi->state) { |
| 639 | case GPRS_NS2_ST_UNCONFIGURED: |
| 640 | case GPRS_NS2_ST_RESET: |
| 641 | /* ignore ALIVE */ |
| 642 | break; |
| 643 | default: |
| 644 | ns2_tx_alive_ack(priv->nsvc); |
| 645 | } |
| 646 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 647 | case GPRS_NS2_EV_RX_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 648 | /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */ |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 649 | if (fi->state == GPRS_NS2_ST_RECOVERING) |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 650 | ns2_st_alive(fi, event, data); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 651 | else |
| 652 | recv_test_procedure(fi); |
| 653 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 654 | case GPRS_NS2_EV_RX_UNITDATA: |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 655 | /* UNITDATA has to handle the release of msg. |
| 656 | * If send upwards (gprs_ns2_recv_unitdata) it must NOT free |
| 657 | * the msg, the upper layer has to do it. |
| 658 | * Otherwise the msg must be freed. |
| 659 | */ |
Alexander Couzens | 6cf65d9 | 2021-01-18 17:55:35 +0100 | [diff] [blame] | 660 | |
| 661 | LOG_NS_DATA(priv->nsvc, "Rx", NS_PDUT_UNITDATA, LOGL_INFO, "\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 662 | switch (fi->state) { |
| 663 | case GPRS_NS2_ST_BLOCKED: |
| 664 | /* 7.2.1: the BLOCKED_ACK might be lost */ |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 665 | if (priv->accept_unitdata) { |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 666 | ns2_recv_unitdata(fi, msg); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 667 | return; |
| 668 | } |
| 669 | |
| 670 | ns2_tx_status(priv->nsvc, |
| 671 | NS_CAUSE_NSVC_BLOCKED, |
| 672 | 0, msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 673 | break; |
| 674 | /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */ |
Alexander Couzens | d3e3110 | 2021-02-03 11:15:07 +0100 | [diff] [blame] | 675 | case GPRS_NS2_ST_RECOVERING: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 676 | case GPRS_NS2_ST_UNBLOCKED: |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 677 | ns2_recv_unitdata(fi, msg); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 678 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 679 | } |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 680 | |
| 681 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 682 | break; |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 683 | case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED: |
Alexander Couzens | 6f3b738 | 2020-12-21 15:57:04 +0100 | [diff] [blame] | 684 | if (fi->state != GPRS_NS2_ST_UNCONFIGURED) { |
| 685 | /* Force the NSVC back to its initial state */ |
| 686 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0); |
Alexander Couzens | 6f3b738 | 2020-12-21 15:57:04 +0100 | [diff] [blame] | 687 | return; |
| 688 | } |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 689 | break; |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 690 | case GPRS_NS2_EV_REQ_OM_BLOCK: |
| 691 | /* vty cmd: block */ |
| 692 | priv->initiate_block = true; |
| 693 | priv->om_blocked = true; |
| 694 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 695 | break; |
| 696 | case GPRS_NS2_EV_REQ_OM_UNBLOCK: |
| 697 | /* vty cmd: unblock*/ |
| 698 | if (!priv->om_blocked) |
| 699 | return; |
| 700 | priv->om_blocked = false; |
| 701 | if (fi->state == GPRS_NS2_ST_BLOCKED) |
| 702 | osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0); |
| 703 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 707 | static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi, |
Alexander Couzens | 0346b64 | 2020-10-27 13:05:56 +0100 | [diff] [blame] | 708 | enum osmo_fsm_term_cause cause) |
| 709 | { |
| 710 | struct gprs_ns2_vc_priv *priv = fi->priv; |
| 711 | |
| 712 | osmo_timer_del(&priv->alive.timer); |
| 713 | } |
| 714 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 715 | static struct osmo_fsm ns2_vc_fsm = { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 716 | .name = "GPRS-NS2-VC", |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 717 | .states = ns2_vc_states, |
| 718 | .num_states = ARRAY_SIZE(ns2_vc_states), |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 719 | .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) | |
| 720 | S(GPRS_NS2_EV_RX_RESET) | |
| 721 | S(GPRS_NS2_EV_RX_ALIVE) | |
| 722 | S(GPRS_NS2_EV_RX_ALIVE_ACK) | |
| 723 | S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) | |
Alexander Couzens | 27e5873 | 2021-03-21 17:12:08 +0100 | [diff] [blame] | 724 | S(GPRS_NS2_EV_REQ_OM_RESET) | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 725 | S(GPRS_NS2_EV_REQ_OM_BLOCK) | |
| 726 | S(GPRS_NS2_EV_REQ_OM_UNBLOCK), |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 727 | .allstate_action = ns2_vc_fsm_allstate_action, |
| 728 | .cleanup = ns2_vc_fsm_clean, |
| 729 | .timer_cb = ns2_vc_fsm_timer_cb, |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 730 | .event_names = ns2_vc_event_names, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 731 | .pre_term = NULL, |
| 732 | .log_subsys = DLNS, |
| 733 | }; |
| 734 | |
| 735 | /*! |
| 736 | * \brief gprs_ns2_vc_fsm_alloc |
| 737 | * \param ctx |
| 738 | * \param vc |
| 739 | * \param id a char representation of the virtual curcuit |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 740 | * \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] | 741 | * \return NULL on error, otherwise the fsm |
| 742 | */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 743 | struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc, |
Daniel Willmann | f5b2e28 | 2020-11-18 18:51:25 +0100 | [diff] [blame] | 744 | const char *id, bool initiator) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 745 | { |
| 746 | struct osmo_fsm_inst *fi; |
| 747 | struct gprs_ns2_vc_priv *priv; |
| 748 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 749 | fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 750 | if (!fi) |
| 751 | return fi; |
| 752 | |
| 753 | nsvc->fi = fi; |
| 754 | priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv); |
| 755 | priv->nsvc = nsvc; |
Alexander Couzens | ea01bf2 | 2021-01-18 14:01:01 +0100 | [diff] [blame] | 756 | priv->initiator = initiator; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 757 | |
| 758 | osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi); |
| 759 | |
| 760 | return fi; |
| 761 | } |
| 762 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 763 | /*! Start a NS-VC FSM. |
| 764 | * \param nsvc the virtual circuit |
| 765 | * \return 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 766 | int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 767 | { |
| 768 | /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */ |
| 769 | if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED) |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 770 | 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] | 771 | return 0; |
| 772 | } |
| 773 | |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 774 | /*! Reset a NS-VC FSM. |
| 775 | * \param nsvc the virtual circuit |
| 776 | * \return 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 777 | int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc) |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 778 | { |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 779 | 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] | 780 | } |
| 781 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 782 | /*! Block a NS-VC. |
| 783 | * \param nsvc the virtual circuit |
| 784 | * \return 0 on success; negative on error */ |
| 785 | int ns2_vc_block(struct gprs_ns2_vc *nsvc) |
| 786 | { |
Alexander Couzens | e7dfeac | 2021-03-21 17:28:36 +0100 | [diff] [blame] | 787 | struct gprs_ns2_vc_priv *priv = nsvc->fi->priv; |
Alexander Couzens | 79a3a84 | 2021-04-13 12:26:57 +0200 | [diff] [blame] | 788 | if (priv->om_blocked) |
Alexander Couzens | e7dfeac | 2021-03-21 17:28:36 +0100 | [diff] [blame] | 789 | return -EALREADY; |
| 790 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 791 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL); |
| 792 | } |
| 793 | |
| 794 | /*! Unblock a NS-VC. |
| 795 | * \param nsvc the virtual circuit |
| 796 | * \return 0 on success; negative on error */ |
| 797 | int ns2_vc_unblock(struct gprs_ns2_vc *nsvc) |
| 798 | { |
Alexander Couzens | e7dfeac | 2021-03-21 17:28:36 +0100 | [diff] [blame] | 799 | struct gprs_ns2_vc_priv *priv = nsvc->fi->priv; |
Alexander Couzens | 79a3a84 | 2021-04-13 12:26:57 +0200 | [diff] [blame] | 800 | if (!priv->om_blocked) |
Alexander Couzens | e7dfeac | 2021-03-21 17:28:36 +0100 | [diff] [blame] | 801 | return -EALREADY; |
| 802 | |
Alexander Couzens | 47afc42 | 2021-01-17 20:12:46 +0100 | [diff] [blame] | 803 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL); |
| 804 | } |
| 805 | |
Alexander Couzens | 27e5873 | 2021-03-21 17:12:08 +0100 | [diff] [blame] | 806 | /*! Reset a NS-VC. |
| 807 | * \param nsvc the virtual circuit |
| 808 | * \return 0 on success; negative on error */ |
| 809 | int ns2_vc_reset(struct gprs_ns2_vc *nsvc) |
| 810 | { |
| 811 | return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_RESET, NULL); |
| 812 | } |
| 813 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 814 | /*! entry point for messages from the driver/VL |
| 815 | * \param nsvc virtual circuit on which the message was received |
| 816 | * \param msg message that was received |
| 817 | * \param tp parsed TLVs of the received message |
| 818 | * \return 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 819 | int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 820 | { |
| 821 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 822 | struct osmo_fsm_inst *fi = nsvc->fi; |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 823 | int rc = 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 824 | uint8_t cause; |
Alexander Couzens | 3f576ab | 2021-01-20 14:50:31 +0100 | [diff] [blame] | 825 | uint16_t nsei, nsvci; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 826 | |
| 827 | /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct, |
| 828 | * if not answer STATUS with "NS-VC unknown" */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 829 | /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */ |
| 830 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 831 | if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 832 | if (nsh->pdu_type != NS_PDUT_STATUS) { |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 833 | rc = ns2_tx_status(nsvc, cause, 0, msg); |
| 834 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Alexander Couzens | 43771df | 2021-01-20 13:03:03 +0100 | [diff] [blame] | 838 | if (TLVP_PRESENT(tp, NS_IE_NSEI)) { |
| 839 | nsei = tlvp_val16be(tp, NS_IE_NSEI); |
| 840 | if (nsei != nsvc->nse->nsei) { |
| 841 | /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */ |
| 842 | if (nsh->pdu_type == NS_PDUT_RESET) |
| 843 | ns2_tx_reset_ack(nsvc); |
| 844 | |
Alexander Couzens | 6cf65d9 | 2021-01-18 17:55:35 +0100 | [diff] [blame] | 845 | LOG_NS_SIGNAL(nsvc, "Rx", nsh->pdu_type, LOGL_ERROR, " with wrong NSEI=%05u. Ignoring PDU.\n", nsei); |
Alexander Couzens | 43771df | 2021-01-20 13:03:03 +0100 | [diff] [blame] | 846 | goto out; |
| 847 | } |
| 848 | } |
| 849 | |
Alexander Couzens | 3f576ab | 2021-01-20 14:50:31 +0100 | [diff] [blame] | 850 | if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) { |
| 851 | nsvci = tlvp_val16be(tp, NS_IE_VCI); |
| 852 | if (nsvci != nsvc->nsvci) { |
| 853 | /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */ |
| 854 | if (nsh->pdu_type == NS_PDUT_RESET) |
| 855 | ns2_tx_reset_ack(nsvc); |
| 856 | |
Alexander Couzens | 6cf65d9 | 2021-01-18 17:55:35 +0100 | [diff] [blame] | 857 | LOG_NS_SIGNAL(nsvc, "Rx", nsh->pdu_type, LOGL_ERROR, " with wrong NSVCI=%05u. Ignoring PDU.\n", nsvci); |
Alexander Couzens | 3f576ab | 2021-01-20 14:50:31 +0100 | [diff] [blame] | 858 | goto out; |
| 859 | } |
| 860 | } |
| 861 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 862 | switch (nsh->pdu_type) { |
| 863 | case NS_PDUT_RESET: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 864 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 865 | break; |
| 866 | case NS_PDUT_RESET_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 867 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 868 | break; |
| 869 | case NS_PDUT_BLOCK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 870 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 871 | break; |
| 872 | case NS_PDUT_BLOCK_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 873 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 874 | break; |
| 875 | case NS_PDUT_UNBLOCK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 876 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 877 | break; |
| 878 | case NS_PDUT_UNBLOCK_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 879 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 880 | break; |
| 881 | case NS_PDUT_ALIVE: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 882 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 883 | break; |
| 884 | case NS_PDUT_ALIVE_ACK: |
Alexander Couzens | f577543 | 2021-01-18 13:49:00 +0100 | [diff] [blame] | 885 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 886 | break; |
| 887 | case NS_PDUT_UNITDATA: |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 888 | /* 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] | 889 | osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg); |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 890 | return 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 891 | default: |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 892 | LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei, |
| 893 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 894 | rc = -EINVAL; |
| 895 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 898 | out: |
| 899 | msgb_free(msg); |
| 900 | |
| 901 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 902 | } |
| 903 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 904 | /*! is the given NS-VC unblocked? */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 905 | int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 906 | { |
| 907 | return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED); |
| 908 | } |
| 909 | |
| 910 | /* initialize osmo_ctx on main tread */ |
| 911 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 912 | { |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 913 | OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 914 | } |