blob: fa8cec2e82f225f050485cedd71771ed0377a53e [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \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 Couzens6a161492020-07-12 13:45:50 +020052struct gprs_ns2_vc_priv {
53 struct gprs_ns2_vc *nsvc;
54 /* how often the timer was triggered */
55 int N;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010056 /* 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 Couzensea01bf22021-01-18 14:01:01 +010058 bool initiator;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010059 bool initiate_block;
60 bool initiate_reset;
Alexander Couzens47afc422021-01-17 20:12:46 +010061 /* if blocked by O&M/vty */
62 bool om_blocked;
63 /* if unitdata is forwarded to the user */
64 bool accept_unitdata;
Alexander Couzens6a161492020-07-12 13:45:50 +020065
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 Couzensab0e8642021-02-12 02:50:44 +010071 struct timespec timer_started;
Alexander Couzens6a161492020-07-12 13:45:50 +020072 } 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 Couzensd3e31102021-02-03 11:15:07 +010082 * - UNCONFIGURED -> RECOVERY -> UNBLOCKED
Alexander Couzens6a161492020-07-12 13:45:50 +020083 *
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 Couzensd3e31102021-02-03 11:15:07 +010088 * The RECOVERY state is used as intermediate, because a VC is only valid if it received an Alive ACK when
Alexander Couzens6a161492020-07-12 13:45:50 +020089 * not using RESET/BLOCK procedure.
90 */
91
92enum 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 Couzensd3e31102021-02-03 11:15:07 +010098 GPRS_NS2_ST_RECOVERING, /* only used when not using RESET/BLOCK procedure */
Alexander Couzens6a161492020-07-12 13:45:50 +020099};
100
101enum gprs_ns2_vc_event {
Alexander Couzensf5775432021-01-18 13:49:00 +0100102 GPRS_NS2_EV_REQ_START,
Alexander Couzens6a161492020-07-12 13:45:50 +0200103
104 /* received messages */
Alexander Couzensf5775432021-01-18 13:49:00 +0100105 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 Couzens6a161492020-07-12 13:45:50 +0200114
Alexander Couzensf5775432021-01-18 13:49:00 +0100115 GPRS_NS2_EV_RX_UNITDATA,
Daniel Willmanned0c9822020-11-18 14:08:07 +0100116
Alexander Couzensf5775432021-01-18 13:49:00 +0100117 GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */
Alexander Couzens27e58732021-03-21 17:12:08 +0100118 GPRS_NS2_EV_REQ_OM_RESET, /* vty cmd: reset */
Alexander Couzensf5775432021-01-18 13:49:00 +0100119 GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */
120 GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/
Alexander Couzens6a161492020-07-12 13:45:50 +0200121};
122
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100123static const struct value_string ns2_vc_event_names[] = {
Harald Welteb339cbb2021-02-05 14:49:21 +0100124 { 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 Couzens27e58732021-03-21 17:12:08 +0100136 { GPRS_NS2_EV_REQ_OM_RESET, "REQ-O&M-RESET"},
Alexander Couzens47afc422021-01-17 20:12:46 +0100137 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
138 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200139 { 0, NULL }
140};
141
142static 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 Welte5e408312021-03-23 18:16:30 +0100148/* Start the NS-TEST procedure, either with transmitting a tx_alive,
149 * (start_tx_alive==true) or with starting tns-test */
150static void start_test_procedure(struct osmo_fsm_inst *fi, bool start_tx_alive)
Alexander Couzens6a161492020-07-12 13:45:50 +0200151{
Harald Welte5e408312021-03-23 18:16:30 +0100152 struct gprs_ns2_vc_priv *priv = fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200153 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
Harald Welte5e408312021-03-23 18:16:30 +0100154 unsigned int tout_idx;
Alexander Couzens6a161492020-07-12 13:45:50 +0200155
Harald Welte5e408312021-03-23 18:16:30 +0100156 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 Couzens6a161492020-07-12 13:45:50 +0200165
Alexander Couzens6a161492020-07-12 13:45:50 +0200166 priv->alive.N = 0;
167
Harald Welte5e408312021-03-23 18:16:30 +0100168 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 Couzens6a161492020-07-12 13:45:50 +0200180}
181
182static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
183{
Alexander Couzens8138c532021-06-15 22:11:40 +0200184 osmo_stat_item_set(osmo_stat_item_group_get_item(priv->nsvc->statg, NS_STAT_ALIVE_DELAY), 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200185 osmo_timer_del(&priv->alive.timer);
186}
187
Harald Welte5e408312021-03-23 18:16:30 +0100188/* how many milliseconds have expired since the last alive timer start? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200189static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
190{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100191 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200192
Alexander Couzensab0e8642021-02-12 02:50:44 +0100193 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
194 return 0;
195
196 timespecsub(&now, &priv->alive.timer_started, &elapsed);
Alexander Couzensab0e8642021-02-12 02:50:44 +0100197 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200198}
199
Harald Welte5e408312021-03-23 18:16:30 +0100200/* we just received a NS-ALIVE-ACK; re-schedule after Tns-test */
Alexander Couzens6a161492020-07-12 13:45:50 +0200201static void recv_test_procedure(struct osmo_fsm_inst *fi)
202{
203 struct gprs_ns2_vc_priv *priv = fi->priv;
204 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
205 struct gprs_ns2_vc *nsvc = priv->nsvc;
206
207 /* ignoring ACKs without sending an ALIVE */
208 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
209 return;
210
211 priv->alive.mode = NS_TOUT_TNS_TEST;
212 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200213 osmo_stat_item_set(osmo_stat_item_group_get_item(nsvc->statg, NS_STAT_ALIVE_DELAY),
Alexander Couzens6a161492020-07-12 13:45:50 +0200214 alive_timer_elapsed_ms(priv));
215}
216
217
218static void alive_timeout_handler(void *data)
219{
220 struct osmo_fsm_inst *fi = data;
221 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
222 struct gprs_ns2_vc_priv *priv = fi->priv;
223
224 switch (priv->alive.mode) {
225 case NS_TOUT_TNS_TEST:
226 priv->alive.mode = NS_TOUT_TNS_ALIVE;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100227 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100228 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200229 ns2_tx_alive(priv->nsvc);
230 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
231 break;
232 case NS_TOUT_TNS_ALIVE:
Daniel Willmannefa64f92021-07-09 20:35:00 +0200233 RATE_CTR_INC_NS(priv->nsvc, NS_CTR_LOST_ALIVE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200234 priv->alive.N++;
235
236 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
237 /* retransmission */
238 ns2_tx_alive(priv->nsvc);
239 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
240 } else {
241 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100242 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200243 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
244 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100245 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 }
247 }
248 break;
249 default:
250 break;
251 }
252}
253
Harald Welte6a9ec422021-01-31 18:56:29 +0100254
255static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
256{
Alexander Couzensa140d442021-07-12 16:42:32 +0200257 struct gprs_ns2_vc_priv *priv = fi->priv;
258
Harald Welte6a9ec422021-01-31 18:56:29 +0100259 stop_test_procedure(fi->priv);
Alexander Couzensa140d442021-07-12 16:42:32 +0200260 ns2_nse_notify_unblocked(priv->nsvc, false);
Harald Welte6a9ec422021-01-31 18:56:29 +0100261}
262
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100263static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200264{
265 struct gprs_ns2_vc_priv *priv = fi->priv;
266 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
267
Alexander Couzensea01bf22021-01-18 14:01:01 +0100268 priv->initiate_reset = priv->initiate_block = priv->initiator;
269 priv->om_blocked = false;
270
Alexander Couzens6a161492020-07-12 13:45:50 +0200271 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100272 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200273 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100274 case GPRS_NS2_VC_MODE_ALIVE:
Harald Weltec51ddf22021-03-05 09:48:18 +0100275 if (priv->nsvc->nse->dialect == GPRS_NS2_DIALECT_SNS) {
276 /* In IP-SNS, the NS-VC are assumed initially alive, until the alive
277 * procedure should fail at some future point */
278 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
279 } else {
280 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
281 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200282 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100283 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200284 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
285 break;
286 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 break;
288 default:
289 OSMO_ASSERT(0);
290 }
291}
292
293
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100294static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200295{
296 struct gprs_ns2_vc_priv *priv = fi->priv;
297
298 if (old_state != GPRS_NS2_ST_RESET)
299 priv->N = 0;
300
Alexander Couzens47afc422021-01-17 20:12:46 +0100301 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100302 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200303 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
304
305 stop_test_procedure(priv);
306 ns2_nse_notify_unblocked(priv->nsvc, false);
307}
308
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100309static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200310{
311 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
312 struct gprs_ns2_vc_priv *priv = fi->priv;
313
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100314 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200315 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100316 case GPRS_NS2_EV_RX_RESET:
317 ns2_tx_reset_ack(priv->nsvc);
318 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100319 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200320 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
321 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
322 break;
323 }
324 } else {
325 /* we are on the receiving end */
326 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100327 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200328 ns2_tx_reset_ack(priv->nsvc);
329 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
330 0, 0);
331 break;
332 }
333 }
334}
335
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100336static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200337{
338 struct gprs_ns2_vc_priv *priv = fi->priv;
339
Harald Welte4c54f592021-01-30 22:42:15 +0100340 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200341 priv->N = 0;
Daniel Willmannefa64f92021-07-09 20:35:00 +0200342 RATE_CTR_INC_NS(priv->nsvc, NS_CTR_BLOCKED);
Harald Welte4c54f592021-01-30 22:42:15 +0100343 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200344
Alexander Couzense09deb62021-06-15 21:35:45 +0200345 ns2_nse_notify_unblocked(priv->nsvc, false);
Alexander Couzens47afc422021-01-17 20:12:46 +0100346 if (priv->om_blocked) {
347 /* we are already blocked after a RESET */
348 if (old_state == GPRS_NS2_ST_RESET) {
349 osmo_timer_del(&fi->timer);
350 } else {
351 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
352 }
353 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200354 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100355 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200356
Harald Welte5e408312021-03-23 18:16:30 +0100357 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200358}
359
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100360static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200361{
362 struct gprs_ns2_vc_priv *priv = fi->priv;
363
Alexander Couzens47afc422021-01-17 20:12:46 +0100364 if (priv->om_blocked) {
365 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100366 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100367 priv->accept_unitdata = false;
368 osmo_timer_del(&fi->timer);
369 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100370 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100371 priv->accept_unitdata = false;
372 ns2_tx_block_ack(priv->nsvc);
373 osmo_timer_del(&fi->timer);
374 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100375 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100376 priv->accept_unitdata = false;
377 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
378 osmo_timer_add(&fi->timer);
379 break;
380 }
381 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200382 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100383 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200384 /* TODO: BLOCK is a UNBLOCK_NACK */
385 ns2_tx_block_ack(priv->nsvc);
386 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100387 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200388 ns2_tx_unblock_ack(priv->nsvc);
389 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100390 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100391 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200392 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
393 0, NS_TOUT_TNS_TEST);
394 break;
395 }
396 } else {
397 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
398 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100399 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100400 ns2_tx_block_ack(priv->nsvc);
401 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100402 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200403 ns2_tx_unblock_ack(priv->nsvc);
404 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
405 0, 0);
406 break;
407 }
408 }
409}
410
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100411static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200412{
413 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100414 struct gprs_ns2_vc *nsvc = priv->nsvc;
415 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200416
Alexander Couzensca5ce0d2021-09-05 23:15:56 +0200417 if (old_state != GPRS_NS2_ST_UNBLOCKED) {
Daniel Willmannefa64f92021-07-09 20:35:00 +0200418 RATE_CTR_INC_NS(nsvc, NS_CTR_UNBLOCKED);
Alexander Couzensca5ce0d2021-09-05 23:15:56 +0200419 osmo_clock_gettime(CLOCK_MONOTONIC, &nsvc->ts_alive_change);
420 }
Harald Welteb40bf8b2021-01-30 22:43:01 +0100421
Alexander Couzens47afc422021-01-17 20:12:46 +0100422 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100423 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100424 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Harald Welte5e408312021-03-23 18:16:30 +0100425
426 /* the closest interpretation of the spec would start Tns-test here first,
427 * and only send a NS-ALIVE after Tns-test has expired (i.e. setting the
428 * second argument to 'false'. However, being quick in detecting unavailability
429 * of a NS-VC seems like a good idea */
430 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200431}
432
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100433static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200434{
435 struct gprs_ns2_vc_priv *priv = fi->priv;
436
437 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100438 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100439 ns2_tx_unblock_ack(priv->nsvc);
440 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100441 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100442 priv->initiate_block = false;
Alexander Couzensf27fbf62021-09-06 00:19:15 +0200443 priv->accept_unitdata = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200444 ns2_tx_block_ack(priv->nsvc);
445 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
446 0, 2);
447 break;
448 }
449}
450
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100451static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200452{
453 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100454 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200455 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
456 break;
457 }
458}
459
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100460static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200461{
462 struct gprs_ns2_vc_priv *priv = fi->priv;
463 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
464
465 priv->alive.mode = NS_TOUT_TNS_TEST;
466 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
467
Alexander Couzensd3e31102021-02-03 11:15:07 +0100468 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200469 priv->N = 0;
470
Harald Welte5e408312021-03-23 18:16:30 +0100471 start_test_procedure(fi, true);
Alexander Couzens5b722472021-04-01 15:36:54 +0200472 ns2_nse_notify_unblocked(priv->nsvc, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200473}
474
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100475static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100477 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Harald Weltec51ddf22021-03-05 09:48:18 +0100478 .out_state_mask = S(GPRS_NS2_ST_RESET) |
479 S(GPRS_NS2_ST_RECOVERING) |
480 S(GPRS_NS2_ST_UNBLOCKED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200481 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100482 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100483 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200484 },
485 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100486 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200487 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100488 S(GPRS_NS2_ST_BLOCKED) |
489 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200490 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100491 .action = ns2_st_reset,
492 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200493 },
494 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100495 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
496 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200497 .out_state_mask = S(GPRS_NS2_ST_RESET) |
498 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100499 S(GPRS_NS2_ST_BLOCKED) |
500 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200501 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100502 .action = ns2_st_blocked,
503 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200504 },
505 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100506 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
507 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100508 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100509 S(GPRS_NS2_ST_BLOCKED) |
510 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200511 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100512 .action = ns2_st_unblocked,
513 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200514 },
515
Alexander Couzensd3e31102021-02-03 11:15:07 +0100516 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
517 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100518 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100519 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100520 S(GPRS_NS2_ST_UNBLOCKED) |
521 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100522 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100523 .action = ns2_st_alive,
524 .onenter = ns2_st_alive_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200525 },
526};
527
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100528static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200529{
530 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
531 struct gprs_ns2_vc_priv *priv = fi->priv;
532
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100533 switch (fi->state) {
534 case GPRS_NS2_ST_RESET:
535 if (priv->initiate_reset) {
Daniel Willmannefa64f92021-07-09 20:35:00 +0200536 RATE_CTR_INC_NS(priv->nsvc, NS_CTR_LOST_RESET);
Alexander Couzens6a161492020-07-12 13:45:50 +0200537 priv->N++;
538 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
539 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
540 } else {
541 priv->N = 0;
542 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
543 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100544 }
545 break;
546 case GPRS_NS2_ST_BLOCKED:
547 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200548 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100549 if (priv->om_blocked) {
550 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
551 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
552 } else {
553 /* 7.2 stop accepting data when BLOCK PDU not responded */
554 priv->accept_unitdata = false;
555 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200556 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100557 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
558 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
559 } else {
560 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
561 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200562 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100563 }
564 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100565 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100566 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200567 priv->N++;
568 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100569 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200570 } else {
571 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100572 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200573 }
574 break;
575 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100576 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200577 }
578 return 0;
579}
580
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100581static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200582 struct msgb *msg)
583{
584 struct gprs_ns2_vc_priv *priv = fi->priv;
585 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
586 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
587 struct osmo_gprs_ns2_prim nsp = {};
588 uint16_t bvci;
589
Alexander Couzenscce88282020-10-26 00:25:50 +0100590 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
591 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200592 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100593 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200594
595 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
596 * for a PTP BVC may indicate a request to change the IP endpoint
597 * and/or a response to a change in the IP endpoint. */
598
599 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
600 bvci = nsh->data[1] << 8 | nsh->data[2];
601
Alexander Couzens89acdef2020-09-23 18:22:31 +0200602 msg->l3h = &nsh->data[3];
603 nsp.bvci = bvci;
604 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200605
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200606 /* 10.3.9 NS SDU Control Bits */
607 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100608 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200609
Alexander Couzens138b96f2021-01-25 16:23:29 +0100610 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200611 PRIM_OP_INDICATION, msg);
612 nsi->cb(&nsp.oph, nsi->cb_data);
613}
614
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100615static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200616 uint32_t event,
617 void *data)
618{
619 struct gprs_ns2_vc_priv *priv = fi->priv;
620 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzensa7a757f2021-09-07 01:22:53 +0200621 struct tlv_parsed *tp;
Alexander Couzenscce88282020-10-26 00:25:50 +0100622 struct msgb *msg = data;
Alexander Couzensa7a757f2021-09-07 01:22:53 +0200623 uint8_t cause;
Alexander Couzens6a161492020-07-12 13:45:50 +0200624
625 switch (event) {
Alexander Couzens27e58732021-03-21 17:12:08 +0100626 case GPRS_NS2_EV_REQ_OM_RESET:
627 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
628 break;
629 /* move the FSM into reset */
630 if (fi->state != GPRS_NS2_ST_RESET) {
631 priv->initiate_reset = true;
632 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
633 }
634 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100635 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100636 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200637 break;
638
639 /* move the FSM into reset */
640 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100641 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200642 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
643 }
644 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100645 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200646 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100647 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200648 switch (fi->state) {
649 case GPRS_NS2_ST_UNCONFIGURED:
650 case GPRS_NS2_ST_RESET:
651 /* ignore ALIVE */
652 break;
653 default:
654 ns2_tx_alive_ack(priv->nsvc);
655 }
656 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100657 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200658 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100659 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100660 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200661 else
662 recv_test_procedure(fi);
663 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100664 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100665 /* UNITDATA has to handle the release of msg.
666 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
667 * the msg, the upper layer has to do it.
668 * Otherwise the msg must be freed.
669 */
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100670
671 LOG_NS_DATA(priv->nsvc, "Rx", NS_PDUT_UNITDATA, LOGL_INFO, "\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200672 switch (fi->state) {
673 case GPRS_NS2_ST_BLOCKED:
674 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100675 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100676 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100677 return;
678 }
679
680 ns2_tx_status(priv->nsvc,
681 NS_CAUSE_NSVC_BLOCKED,
682 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200683 break;
684 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100685 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200686 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100687 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100688 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200689 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100690
691 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100693 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100694 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
695 /* Force the NSVC back to its initial state */
696 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100697 return;
698 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100699 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100700 case GPRS_NS2_EV_REQ_OM_BLOCK:
701 /* vty cmd: block */
702 priv->initiate_block = true;
703 priv->om_blocked = true;
704 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
705 break;
706 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
707 /* vty cmd: unblock*/
708 if (!priv->om_blocked)
709 return;
710 priv->om_blocked = false;
711 if (fi->state == GPRS_NS2_ST_BLOCKED)
712 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
713 break;
Alexander Couzensa7a757f2021-09-07 01:22:53 +0200714 case GPRS_NS2_EV_RX_STATUS:
715 tp = data;
716 cause = tlvp_val8(tp, NS_IE_CAUSE, 0);
717 switch (cause) {
718 case NS_CAUSE_NSVC_BLOCKED:
719 if (fi->state != GPRS_NS2_ST_BLOCKED) {
720 LOG_NS_SIGNAL(priv->nsvc, "Rx", NS_PDUT_STATUS, LOGL_ERROR, ": remote side reported blocked state.\n");
721 priv->initiate_block = false;
722 priv->accept_unitdata = false;
723 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
724 }
725 break;
726 case NS_CAUSE_NSVC_UNKNOWN:
727 if (fi->state != GPRS_NS2_ST_RESET && fi->state != GPRS_NS2_ST_UNCONFIGURED) {
728 LOG_NS_SIGNAL(priv->nsvc, "Rx", NS_PDUT_STATUS, LOGL_ERROR, ": remote side reported unknown nsvc.\n");
729 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
730 }
731 break;
732 }
733
734 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200735 }
736}
737
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100738static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100739 enum osmo_fsm_term_cause cause)
740{
741 struct gprs_ns2_vc_priv *priv = fi->priv;
742
743 osmo_timer_del(&priv->alive.timer);
744}
745
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100746static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200747 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100748 .states = ns2_vc_states,
749 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100750 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
751 S(GPRS_NS2_EV_RX_RESET) |
752 S(GPRS_NS2_EV_RX_ALIVE) |
753 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
Alexander Couzensa7a757f2021-09-07 01:22:53 +0200754 S(GPRS_NS2_EV_RX_STATUS) |
Alexander Couzensf5775432021-01-18 13:49:00 +0100755 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens27e58732021-03-21 17:12:08 +0100756 S(GPRS_NS2_EV_REQ_OM_RESET) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100757 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
758 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100759 .allstate_action = ns2_vc_fsm_allstate_action,
760 .cleanup = ns2_vc_fsm_clean,
761 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100762 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200763 .pre_term = NULL,
764 .log_subsys = DLNS,
765};
766
767/*!
768 * \brief gprs_ns2_vc_fsm_alloc
769 * \param ctx
770 * \param vc
771 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100772 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200773 * \return NULL on error, otherwise the fsm
774 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100775struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100776 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200777{
778 struct osmo_fsm_inst *fi;
779 struct gprs_ns2_vc_priv *priv;
780
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100781 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200782 if (!fi)
783 return fi;
784
785 nsvc->fi = fi;
786 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
787 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100788 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200789
790 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
791
792 return fi;
793}
794
Harald Welte5bef2cc2020-09-18 22:33:24 +0200795/*! Start a NS-VC FSM.
796 * \param nsvc the virtual circuit
797 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100798int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200799{
800 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
801 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100802 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200803 return 0;
804}
805
Daniel Willmanned0c9822020-11-18 14:08:07 +0100806/*! Reset a NS-VC FSM.
807 * \param nsvc the virtual circuit
808 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100809int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100810{
Alexander Couzensf5775432021-01-18 13:49:00 +0100811 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100812}
813
Alexander Couzens47afc422021-01-17 20:12:46 +0100814/*! Block a NS-VC.
815 * \param nsvc the virtual circuit
816 * \return 0 on success; negative on error */
817int ns2_vc_block(struct gprs_ns2_vc *nsvc)
818{
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100819 struct gprs_ns2_vc_priv *priv = nsvc->fi->priv;
Alexander Couzens79a3a842021-04-13 12:26:57 +0200820 if (priv->om_blocked)
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100821 return -EALREADY;
822
Alexander Couzens47afc422021-01-17 20:12:46 +0100823 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
824}
825
826/*! Unblock a NS-VC.
827 * \param nsvc the virtual circuit
828 * \return 0 on success; negative on error */
829int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
830{
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100831 struct gprs_ns2_vc_priv *priv = nsvc->fi->priv;
Alexander Couzens79a3a842021-04-13 12:26:57 +0200832 if (!priv->om_blocked)
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100833 return -EALREADY;
834
Alexander Couzens47afc422021-01-17 20:12:46 +0100835 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
836}
837
Alexander Couzens27e58732021-03-21 17:12:08 +0100838/*! Reset a NS-VC.
839 * \param nsvc the virtual circuit
840 * \return 0 on success; negative on error */
841int ns2_vc_reset(struct gprs_ns2_vc *nsvc)
842{
843 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_RESET, NULL);
844}
845
Harald Welte5bef2cc2020-09-18 22:33:24 +0200846/*! entry point for messages from the driver/VL
847 * \param nsvc virtual circuit on which the message was received
848 * \param msg message that was received
849 * \param tp parsed TLVs of the received message
850 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100851int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200852{
853 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
854 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100855 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200856 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100857 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200858
859 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
860 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200861 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
862
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100863 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200864 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100865 rc = ns2_tx_status(nsvc, cause, 0, msg);
866 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200867 }
868 }
869
Alexander Couzens43771df2021-01-20 13:03:03 +0100870 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
871 nsei = tlvp_val16be(tp, NS_IE_NSEI);
872 if (nsei != nsvc->nse->nsei) {
873 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
874 if (nsh->pdu_type == NS_PDUT_RESET)
875 ns2_tx_reset_ack(nsvc);
876
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100877 LOG_NS_SIGNAL(nsvc, "Rx", nsh->pdu_type, LOGL_ERROR, " with wrong NSEI=%05u. Ignoring PDU.\n", nsei);
Alexander Couzens43771df2021-01-20 13:03:03 +0100878 goto out;
879 }
880 }
881
Alexander Couzense7873332021-09-06 18:17:41 +0200882 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100883 nsvci = tlvp_val16be(tp, NS_IE_VCI);
884 if (nsvci != nsvc->nsvci) {
885 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
886 if (nsh->pdu_type == NS_PDUT_RESET)
887 ns2_tx_reset_ack(nsvc);
888
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100889 LOG_NS_SIGNAL(nsvc, "Rx", nsh->pdu_type, LOGL_ERROR, " with wrong NSVCI=%05u. Ignoring PDU.\n", nsvci);
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100890 goto out;
891 }
892 }
893
Alexander Couzens6a161492020-07-12 13:45:50 +0200894 switch (nsh->pdu_type) {
895 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100896 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 break;
898 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100899 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200900 break;
901 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100902 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200903 break;
904 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100905 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200906 break;
907 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100908 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200909 break;
910 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100911 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200912 break;
913 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100914 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200915 break;
916 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100917 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200918 break;
919 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100920 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100921 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100922 return 0;
Alexander Couzensa7a757f2021-09-07 01:22:53 +0200923 case NS_PDUT_STATUS:
924 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_STATUS, tp);
925 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200926 default:
Harald Weltef2949742021-01-20 14:54:14 +0100927 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
928 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +0100929 rc = -EINVAL;
930 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200931 }
932
Alexander Couzenscce88282020-10-26 00:25:50 +0100933out:
934 msgb_free(msg);
935
936 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200937}
938
Harald Welte5bef2cc2020-09-18 22:33:24 +0200939/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100940int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200941{
942 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
943}
944
945/* initialize osmo_ctx on main tread */
946static __attribute__((constructor)) void on_dso_load_ctx(void)
947{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100948 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200949}