blob: a8cb570c476021fe3f5a35100d09fe30ef520d5b [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{
184 osmo_timer_del(&priv->alive.timer);
185}
186
Harald Welte5e408312021-03-23 18:16:30 +0100187/* how many milliseconds have expired since the last alive timer start? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200188static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
189{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100190 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200191
Alexander Couzensab0e8642021-02-12 02:50:44 +0100192 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
193 return 0;
194
195 timespecsub(&now, &priv->alive.timer_started, &elapsed);
Alexander Couzensab0e8642021-02-12 02:50:44 +0100196 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200197}
198
Harald Welte5e408312021-03-23 18:16:30 +0100199/* we just received a NS-ALIVE-ACK; re-schedule after Tns-test */
Alexander Couzens6a161492020-07-12 13:45:50 +0200200static 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
217static 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 Couzens2f8f7b62021-02-03 11:04:22 +0100226 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100227 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200228 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 Welte4c54f592021-01-30 22:42:15 +0100232 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200233 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 Couzens138b96f2021-01-25 16:23:29 +0100241 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200242 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
243 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100244 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200245 }
246 }
247 break;
248 default:
249 break;
250 }
251}
252
Harald Welte6a9ec422021-01-31 18:56:29 +0100253
254static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
255{
256 stop_test_procedure(fi->priv);
257}
258
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100259static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200260{
261 struct gprs_ns2_vc_priv *priv = fi->priv;
262 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
263
Alexander Couzensea01bf22021-01-18 14:01:01 +0100264 priv->initiate_reset = priv->initiate_block = priv->initiator;
265 priv->om_blocked = false;
266
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100268 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100270 case GPRS_NS2_VC_MODE_ALIVE:
Harald Weltec51ddf22021-03-05 09:48:18 +0100271 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 Couzens6a161492020-07-12 13:45:50 +0200278 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100279 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200280 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
281 break;
282 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 break;
284 default:
285 OSMO_ASSERT(0);
286 }
287}
288
289
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100290static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200291{
292 struct gprs_ns2_vc_priv *priv = fi->priv;
293
294 if (old_state != GPRS_NS2_ST_RESET)
295 priv->N = 0;
296
Alexander Couzens47afc422021-01-17 20:12:46 +0100297 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100298 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200299 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 Couzensf7e2cac2021-01-25 16:18:21 +0100305static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200306{
307 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
308 struct gprs_ns2_vc_priv *priv = fi->priv;
309
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100310 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200311 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100312 case GPRS_NS2_EV_RX_RESET:
313 ns2_tx_reset_ack(priv->nsvc);
314 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100315 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200316 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 Couzensf5775432021-01-18 13:49:00 +0100323 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200324 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 Couzensf7e2cac2021-01-25 16:18:21 +0100332static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200333{
334 struct gprs_ns2_vc_priv *priv = fi->priv;
335
Harald Welte4c54f592021-01-30 22:42:15 +0100336 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200337 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100338 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
339 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200340
Alexander Couzens47afc422021-01-17 20:12:46 +0100341 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 Couzens6a161492020-07-12 13:45:50 +0200349 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100350 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200351
Harald Welte5e408312021-03-23 18:16:30 +0100352 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200353}
354
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100355static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200356{
357 struct gprs_ns2_vc_priv *priv = fi->priv;
358
Alexander Couzens47afc422021-01-17 20:12:46 +0100359 if (priv->om_blocked) {
360 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100361 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100362 priv->accept_unitdata = false;
363 osmo_timer_del(&fi->timer);
364 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100365 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100366 priv->accept_unitdata = false;
367 ns2_tx_block_ack(priv->nsvc);
368 osmo_timer_del(&fi->timer);
369 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100370 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100371 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 Couzens6a161492020-07-12 13:45:50 +0200377 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100378 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200379 /* TODO: BLOCK is a UNBLOCK_NACK */
380 ns2_tx_block_ack(priv->nsvc);
381 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100382 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200383 ns2_tx_unblock_ack(priv->nsvc);
384 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100385 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100386 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200387 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 Couzensf5775432021-01-18 13:49:00 +0100394 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100395 ns2_tx_block_ack(priv->nsvc);
396 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100397 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200398 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 Couzensf7e2cac2021-01-25 16:18:21 +0100406static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200407{
408 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100409 struct gprs_ns2_vc *nsvc = priv->nsvc;
410 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200411
Harald Welteb40bf8b2021-01-30 22:43:01 +0100412 if (old_state != GPRS_NS2_ST_UNBLOCKED)
413 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
414
Alexander Couzens47afc422021-01-17 20:12:46 +0100415 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100416 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100417 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Harald Welte5e408312021-03-23 18:16:30 +0100418
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 Couzens6a161492020-07-12 13:45:50 +0200424}
425
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100426static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200427{
428 struct gprs_ns2_vc_priv *priv = fi->priv;
429
430 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100431 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100432 ns2_tx_unblock_ack(priv->nsvc);
433 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100434 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100435 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200436 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 Couzensf7e2cac2021-01-25 16:18:21 +0100443static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200444{
445 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100446 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200447 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
448 break;
449 }
450}
451
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100452static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200453{
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 Couzensd3e31102021-02-03 11:15:07 +0100460 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200461 priv->N = 0;
462
Alexander Couzens6a161492020-07-12 13:45:50 +0200463 ns2_nse_notify_unblocked(priv->nsvc, false);
Harald Welte5e408312021-03-23 18:16:30 +0100464 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200465}
466
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100467static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200468 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100469 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Harald Weltec51ddf22021-03-05 09:48:18 +0100470 .out_state_mask = S(GPRS_NS2_ST_RESET) |
471 S(GPRS_NS2_ST_RECOVERING) |
472 S(GPRS_NS2_ST_UNBLOCKED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200473 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100474 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100475 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 },
477 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100478 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200479 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100480 S(GPRS_NS2_ST_BLOCKED) |
481 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200482 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100483 .action = ns2_st_reset,
484 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200485 },
486 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100487 .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 Couzens6a161492020-07-12 13:45:50 +0200489 .out_state_mask = S(GPRS_NS2_ST_RESET) |
490 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100491 S(GPRS_NS2_ST_BLOCKED) |
492 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200493 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100494 .action = ns2_st_blocked,
495 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200496 },
497 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100498 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
499 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100500 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100501 S(GPRS_NS2_ST_BLOCKED) |
502 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200503 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100504 .action = ns2_st_unblocked,
505 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200506 },
507
Alexander Couzensd3e31102021-02-03 11:15:07 +0100508 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
509 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100510 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100511 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100512 S(GPRS_NS2_ST_UNBLOCKED) |
513 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100514 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100515 .action = ns2_st_alive,
516 .onenter = ns2_st_alive_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200517 },
518};
519
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100520static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200521{
522 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
523 struct gprs_ns2_vc_priv *priv = fi->priv;
524
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100525 switch (fi->state) {
526 case GPRS_NS2_ST_RESET:
527 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100528 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200529 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 Willmannf5b2e282020-11-18 18:51:25 +0100536 }
537 break;
538 case GPRS_NS2_ST_BLOCKED:
539 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200540 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100541 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 Couzens6a161492020-07-12 13:45:50 +0200548 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100549 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 Couzens6a161492020-07-12 13:45:50 +0200554 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100555 }
556 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100557 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100558 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200559 priv->N++;
560 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100561 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200562 } else {
563 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100564 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200565 }
566 break;
567 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100568 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200569 }
570 return 0;
571}
572
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100573static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200574 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 Couzenscce88282020-10-26 00:25:50 +0100582 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
583 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200584 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100585 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200586
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 Couzens89acdef2020-09-23 18:22:31 +0200594 msg->l3h = &nsh->data[3];
595 nsp.bvci = bvci;
596 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200597
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200598 /* 10.3.9 NS SDU Control Bits */
599 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100600 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200601
Alexander Couzens138b96f2021-01-25 16:23:29 +0100602 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 PRIM_OP_INDICATION, msg);
604 nsi->cb(&nsp.oph, nsi->cb_data);
605}
606
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100607static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200608 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 Couzenscce88282020-10-26 00:25:50 +0100613 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200614
615 switch (event) {
Alexander Couzens27e58732021-03-21 17:12:08 +0100616 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 Couzensf5775432021-01-18 13:49:00 +0100625 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100626 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200627 break;
628
629 /* move the FSM into reset */
630 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100631 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200632 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 Couzensf7e2cac2021-01-25 16:18:21 +0100635 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200636 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100637 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200638 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 Couzensf5775432021-01-18 13:49:00 +0100647 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200648 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100649 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100650 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200651 else
652 recv_test_procedure(fi);
653 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100654 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100655 /* 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 Couzens6cf65d92021-01-18 17:55:35 +0100660
661 LOG_NS_DATA(priv->nsvc, "Rx", NS_PDUT_UNITDATA, LOGL_INFO, "\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200662 switch (fi->state) {
663 case GPRS_NS2_ST_BLOCKED:
664 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100665 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100666 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100667 return;
668 }
669
670 ns2_tx_status(priv->nsvc,
671 NS_CAUSE_NSVC_BLOCKED,
672 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200673 break;
674 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100675 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200676 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100677 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100678 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200679 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100680
681 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200682 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100683 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100684 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 Couzens6f3b7382020-12-21 15:57:04 +0100687 return;
688 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100689 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100690 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 Couzens6a161492020-07-12 13:45:50 +0200704 }
705}
706
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100707static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100708 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 Couzensf7e2cac2021-01-25 16:18:21 +0100715static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200716 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100717 .states = ns2_vc_states,
718 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100719 .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 Couzens27e58732021-03-21 17:12:08 +0100724 S(GPRS_NS2_EV_REQ_OM_RESET) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100725 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
726 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100727 .allstate_action = ns2_vc_fsm_allstate_action,
728 .cleanup = ns2_vc_fsm_clean,
729 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100730 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 .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 Willmannf5b2e282020-11-18 18:51:25 +0100740 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200741 * \return NULL on error, otherwise the fsm
742 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100743struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100744 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200745{
746 struct osmo_fsm_inst *fi;
747 struct gprs_ns2_vc_priv *priv;
748
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100749 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200750 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 Couzensea01bf22021-01-18 14:01:01 +0100756 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200757
758 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
759
760 return fi;
761}
762
Harald Welte5bef2cc2020-09-18 22:33:24 +0200763/*! Start a NS-VC FSM.
764 * \param nsvc the virtual circuit
765 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100766int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200767{
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 Couzensf5775432021-01-18 13:49:00 +0100770 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200771 return 0;
772}
773
Daniel Willmanned0c9822020-11-18 14:08:07 +0100774/*! Reset a NS-VC FSM.
775 * \param nsvc the virtual circuit
776 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100777int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100778{
Alexander Couzensf5775432021-01-18 13:49:00 +0100779 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100780}
781
Alexander Couzens47afc422021-01-17 20:12:46 +0100782/*! Block a NS-VC.
783 * \param nsvc the virtual circuit
784 * \return 0 on success; negative on error */
785int ns2_vc_block(struct gprs_ns2_vc *nsvc)
786{
787 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
788}
789
790/*! Unblock a NS-VC.
791 * \param nsvc the virtual circuit
792 * \return 0 on success; negative on error */
793int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
794{
795 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
796}
797
Alexander Couzens27e58732021-03-21 17:12:08 +0100798/*! Reset a NS-VC.
799 * \param nsvc the virtual circuit
800 * \return 0 on success; negative on error */
801int ns2_vc_reset(struct gprs_ns2_vc *nsvc)
802{
803 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_RESET, NULL);
804}
805
Harald Welte5bef2cc2020-09-18 22:33:24 +0200806/*! entry point for messages from the driver/VL
807 * \param nsvc virtual circuit on which the message was received
808 * \param msg message that was received
809 * \param tp parsed TLVs of the received message
810 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100811int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200812{
813 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
814 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100815 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200816 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100817 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200818
819 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
820 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200821 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
822
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100823 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200824 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100825 rc = ns2_tx_status(nsvc, cause, 0, msg);
826 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200827 }
828 }
829
Alexander Couzens43771df2021-01-20 13:03:03 +0100830 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
831 nsei = tlvp_val16be(tp, NS_IE_NSEI);
832 if (nsei != nsvc->nse->nsei) {
833 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
834 if (nsh->pdu_type == NS_PDUT_RESET)
835 ns2_tx_reset_ack(nsvc);
836
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100837 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 +0100838 goto out;
839 }
840 }
841
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100842 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
843 nsvci = tlvp_val16be(tp, NS_IE_VCI);
844 if (nsvci != nsvc->nsvci) {
845 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
846 if (nsh->pdu_type == NS_PDUT_RESET)
847 ns2_tx_reset_ack(nsvc);
848
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100849 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 +0100850 goto out;
851 }
852 }
853
Alexander Couzens6a161492020-07-12 13:45:50 +0200854 switch (nsh->pdu_type) {
855 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100856 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200857 break;
858 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100859 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200860 break;
861 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100862 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200863 break;
864 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100865 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200866 break;
867 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100868 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200869 break;
870 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100871 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200872 break;
873 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100874 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200875 break;
876 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100877 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200878 break;
879 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100880 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100881 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100882 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200883 default:
Harald Weltef2949742021-01-20 14:54:14 +0100884 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
885 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +0100886 rc = -EINVAL;
887 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200888 }
889
Alexander Couzenscce88282020-10-26 00:25:50 +0100890out:
891 msgb_free(msg);
892
893 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200894}
895
Harald Welte5bef2cc2020-09-18 22:33:24 +0200896/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100897int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200898{
899 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
900}
901
902/* initialize osmo_ctx on main tread */
903static __attribute__((constructor)) void on_dso_load_ctx(void)
904{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100905 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200906}