blob: 70258ccdde363f5c7db90dcbb57015ef7055ca67 [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);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200212 osmo_stat_item_set(osmo_stat_item_group_get_item(nsvc->statg, NS_STAT_ALIVE_DELAY),
Alexander Couzens6a161492020-07-12 13:45:50 +0200213 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:
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200232 rate_ctr_inc(rate_ctr_group_get_ctr(priv->nsvc->ctrg, 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;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200338 rate_ctr_inc(rate_ctr_group_get_ctr(priv->nsvc->ctrg, NS_CTR_BLOCKED));
Harald Welte4c54f592021-01-30 22:42:15 +0100339 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200340
Alexander Couzense09deb62021-06-15 21:35:45 +0200341 ns2_nse_notify_unblocked(priv->nsvc, false);
Alexander Couzens47afc422021-01-17 20:12:46 +0100342 if (priv->om_blocked) {
343 /* we are already blocked after a RESET */
344 if (old_state == GPRS_NS2_ST_RESET) {
345 osmo_timer_del(&fi->timer);
346 } else {
347 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
348 }
349 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200350 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100351 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200352
Harald Welte5e408312021-03-23 18:16:30 +0100353 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200354}
355
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100356static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200357{
358 struct gprs_ns2_vc_priv *priv = fi->priv;
359
Alexander Couzens47afc422021-01-17 20:12:46 +0100360 if (priv->om_blocked) {
361 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100362 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100363 priv->accept_unitdata = false;
364 osmo_timer_del(&fi->timer);
365 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100366 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100367 priv->accept_unitdata = false;
368 ns2_tx_block_ack(priv->nsvc);
369 osmo_timer_del(&fi->timer);
370 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100371 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100372 priv->accept_unitdata = false;
373 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
374 osmo_timer_add(&fi->timer);
375 break;
376 }
377 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200378 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100379 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200380 /* TODO: BLOCK is a UNBLOCK_NACK */
381 ns2_tx_block_ack(priv->nsvc);
382 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100383 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200384 ns2_tx_unblock_ack(priv->nsvc);
385 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100386 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100387 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200388 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
389 0, NS_TOUT_TNS_TEST);
390 break;
391 }
392 } else {
393 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
394 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100395 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100396 ns2_tx_block_ack(priv->nsvc);
397 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100398 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200399 ns2_tx_unblock_ack(priv->nsvc);
400 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
401 0, 0);
402 break;
403 }
404 }
405}
406
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100407static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200408{
409 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100410 struct gprs_ns2_vc *nsvc = priv->nsvc;
411 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200412
Harald Welteb40bf8b2021-01-30 22:43:01 +0100413 if (old_state != GPRS_NS2_ST_UNBLOCKED)
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200414 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_UNBLOCKED));
Harald Welteb40bf8b2021-01-30 22:43:01 +0100415
Alexander Couzens47afc422021-01-17 20:12:46 +0100416 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100417 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100418 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Harald Welte5e408312021-03-23 18:16:30 +0100419
420 /* the closest interpretation of the spec would start Tns-test here first,
421 * and only send a NS-ALIVE after Tns-test has expired (i.e. setting the
422 * second argument to 'false'. However, being quick in detecting unavailability
423 * of a NS-VC seems like a good idea */
424 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200425}
426
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100427static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200428{
429 struct gprs_ns2_vc_priv *priv = fi->priv;
430
431 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100432 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100433 ns2_tx_unblock_ack(priv->nsvc);
434 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100435 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100436 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200437 ns2_tx_block_ack(priv->nsvc);
438 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
439 0, 2);
440 break;
441 }
442}
443
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100444static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200445{
446 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100447 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200448 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
449 break;
450 }
451}
452
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100453static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200454{
455 struct gprs_ns2_vc_priv *priv = fi->priv;
456 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
457
458 priv->alive.mode = NS_TOUT_TNS_TEST;
459 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
460
Alexander Couzensd3e31102021-02-03 11:15:07 +0100461 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200462 priv->N = 0;
463
Harald Welte5e408312021-03-23 18:16:30 +0100464 start_test_procedure(fi, true);
Alexander Couzens5b722472021-04-01 15:36:54 +0200465 ns2_nse_notify_unblocked(priv->nsvc, false);
Alexander Couzens6a161492020-07-12 13:45:50 +0200466}
467
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100468static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200469 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100470 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Harald Weltec51ddf22021-03-05 09:48:18 +0100471 .out_state_mask = S(GPRS_NS2_ST_RESET) |
472 S(GPRS_NS2_ST_RECOVERING) |
473 S(GPRS_NS2_ST_UNBLOCKED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200474 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100475 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100476 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200477 },
478 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100479 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200480 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100481 S(GPRS_NS2_ST_BLOCKED) |
482 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200483 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100484 .action = ns2_st_reset,
485 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200486 },
487 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100488 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
489 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200490 .out_state_mask = S(GPRS_NS2_ST_RESET) |
491 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100492 S(GPRS_NS2_ST_BLOCKED) |
493 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200494 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100495 .action = ns2_st_blocked,
496 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200497 },
498 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100499 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
500 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100501 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100502 S(GPRS_NS2_ST_BLOCKED) |
503 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200504 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100505 .action = ns2_st_unblocked,
506 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200507 },
508
Alexander Couzensd3e31102021-02-03 11:15:07 +0100509 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
510 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100511 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100512 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100513 S(GPRS_NS2_ST_UNBLOCKED) |
514 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100515 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100516 .action = ns2_st_alive,
517 .onenter = ns2_st_alive_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200518 },
519};
520
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100521static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200522{
523 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
524 struct gprs_ns2_vc_priv *priv = fi->priv;
525
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100526 switch (fi->state) {
527 case GPRS_NS2_ST_RESET:
528 if (priv->initiate_reset) {
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200529 rate_ctr_inc(rate_ctr_group_get_ctr(priv->nsvc->ctrg, NS_CTR_LOST_RESET));
Alexander Couzens6a161492020-07-12 13:45:50 +0200530 priv->N++;
531 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
532 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
533 } else {
534 priv->N = 0;
535 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
536 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100537 }
538 break;
539 case GPRS_NS2_ST_BLOCKED:
540 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200541 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100542 if (priv->om_blocked) {
543 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
544 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
545 } else {
546 /* 7.2 stop accepting data when BLOCK PDU not responded */
547 priv->accept_unitdata = false;
548 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200549 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100550 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 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
554 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200555 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100556 }
557 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100558 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100559 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200560 priv->N++;
561 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100562 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200563 } else {
564 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100565 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200566 }
567 break;
568 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100569 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200570 }
571 return 0;
572}
573
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100574static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200575 struct msgb *msg)
576{
577 struct gprs_ns2_vc_priv *priv = fi->priv;
578 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
579 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
580 struct osmo_gprs_ns2_prim nsp = {};
581 uint16_t bvci;
582
Alexander Couzenscce88282020-10-26 00:25:50 +0100583 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
584 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200585 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100586 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200587
588 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
589 * for a PTP BVC may indicate a request to change the IP endpoint
590 * and/or a response to a change in the IP endpoint. */
591
592 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
593 bvci = nsh->data[1] << 8 | nsh->data[2];
594
Alexander Couzens89acdef2020-09-23 18:22:31 +0200595 msg->l3h = &nsh->data[3];
596 nsp.bvci = bvci;
597 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200598
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200599 /* 10.3.9 NS SDU Control Bits */
600 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100601 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200602
Alexander Couzens138b96f2021-01-25 16:23:29 +0100603 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200604 PRIM_OP_INDICATION, msg);
605 nsi->cb(&nsp.oph, nsi->cb_data);
606}
607
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100608static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200609 uint32_t event,
610 void *data)
611{
612 struct gprs_ns2_vc_priv *priv = fi->priv;
613 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100614 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200615
616 switch (event) {
Alexander Couzens27e58732021-03-21 17:12:08 +0100617 case GPRS_NS2_EV_REQ_OM_RESET:
618 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
619 break;
620 /* move the FSM into reset */
621 if (fi->state != GPRS_NS2_ST_RESET) {
622 priv->initiate_reset = true;
623 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
624 }
625 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100626 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100627 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200628 break;
629
630 /* move the FSM into reset */
631 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100632 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200633 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
634 }
635 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100636 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200637 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100638 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200639 switch (fi->state) {
640 case GPRS_NS2_ST_UNCONFIGURED:
641 case GPRS_NS2_ST_RESET:
642 /* ignore ALIVE */
643 break;
644 default:
645 ns2_tx_alive_ack(priv->nsvc);
646 }
647 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100648 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200649 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100650 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100651 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200652 else
653 recv_test_procedure(fi);
654 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100655 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100656 /* UNITDATA has to handle the release of msg.
657 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
658 * the msg, the upper layer has to do it.
659 * Otherwise the msg must be freed.
660 */
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100661
662 LOG_NS_DATA(priv->nsvc, "Rx", NS_PDUT_UNITDATA, LOGL_INFO, "\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200663 switch (fi->state) {
664 case GPRS_NS2_ST_BLOCKED:
665 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100666 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100667 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100668 return;
669 }
670
671 ns2_tx_status(priv->nsvc,
672 NS_CAUSE_NSVC_BLOCKED,
673 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200674 break;
675 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100676 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200677 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100678 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100679 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200680 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100681
682 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200683 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100684 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100685 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
686 /* Force the NSVC back to its initial state */
687 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100688 return;
689 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100690 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100691 case GPRS_NS2_EV_REQ_OM_BLOCK:
692 /* vty cmd: block */
693 priv->initiate_block = true;
694 priv->om_blocked = true;
695 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
696 break;
697 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
698 /* vty cmd: unblock*/
699 if (!priv->om_blocked)
700 return;
701 priv->om_blocked = false;
702 if (fi->state == GPRS_NS2_ST_BLOCKED)
703 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
704 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200705 }
706}
707
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100708static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100709 enum osmo_fsm_term_cause cause)
710{
711 struct gprs_ns2_vc_priv *priv = fi->priv;
712
713 osmo_timer_del(&priv->alive.timer);
714}
715
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100716static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200717 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100718 .states = ns2_vc_states,
719 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100720 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
721 S(GPRS_NS2_EV_RX_RESET) |
722 S(GPRS_NS2_EV_RX_ALIVE) |
723 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
724 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens27e58732021-03-21 17:12:08 +0100725 S(GPRS_NS2_EV_REQ_OM_RESET) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100726 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
727 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100728 .allstate_action = ns2_vc_fsm_allstate_action,
729 .cleanup = ns2_vc_fsm_clean,
730 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100731 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200732 .pre_term = NULL,
733 .log_subsys = DLNS,
734};
735
736/*!
737 * \brief gprs_ns2_vc_fsm_alloc
738 * \param ctx
739 * \param vc
740 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100741 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200742 * \return NULL on error, otherwise the fsm
743 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100744struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100745 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200746{
747 struct osmo_fsm_inst *fi;
748 struct gprs_ns2_vc_priv *priv;
749
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100750 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200751 if (!fi)
752 return fi;
753
754 nsvc->fi = fi;
755 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
756 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100757 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200758
759 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
760
761 return fi;
762}
763
Harald Welte5bef2cc2020-09-18 22:33:24 +0200764/*! Start a NS-VC FSM.
765 * \param nsvc the virtual circuit
766 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100767int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200768{
769 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
770 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100771 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200772 return 0;
773}
774
Daniel Willmanned0c9822020-11-18 14:08:07 +0100775/*! Reset a NS-VC FSM.
776 * \param nsvc the virtual circuit
777 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100778int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100779{
Alexander Couzensf5775432021-01-18 13:49:00 +0100780 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100781}
782
Alexander Couzens47afc422021-01-17 20:12:46 +0100783/*! Block a NS-VC.
784 * \param nsvc the virtual circuit
785 * \return 0 on success; negative on error */
786int ns2_vc_block(struct gprs_ns2_vc *nsvc)
787{
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100788 struct gprs_ns2_vc_priv *priv = nsvc->fi->priv;
Alexander Couzens79a3a842021-04-13 12:26:57 +0200789 if (priv->om_blocked)
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100790 return -EALREADY;
791
Alexander Couzens47afc422021-01-17 20:12:46 +0100792 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
793}
794
795/*! Unblock a NS-VC.
796 * \param nsvc the virtual circuit
797 * \return 0 on success; negative on error */
798int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
799{
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100800 struct gprs_ns2_vc_priv *priv = nsvc->fi->priv;
Alexander Couzens79a3a842021-04-13 12:26:57 +0200801 if (!priv->om_blocked)
Alexander Couzense7dfeac2021-03-21 17:28:36 +0100802 return -EALREADY;
803
Alexander Couzens47afc422021-01-17 20:12:46 +0100804 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
805}
806
Alexander Couzens27e58732021-03-21 17:12:08 +0100807/*! Reset a NS-VC.
808 * \param nsvc the virtual circuit
809 * \return 0 on success; negative on error */
810int ns2_vc_reset(struct gprs_ns2_vc *nsvc)
811{
812 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_RESET, NULL);
813}
814
Harald Welte5bef2cc2020-09-18 22:33:24 +0200815/*! entry point for messages from the driver/VL
816 * \param nsvc virtual circuit on which the message was received
817 * \param msg message that was received
818 * \param tp parsed TLVs of the received message
819 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100820int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200821{
822 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
823 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100824 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200825 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100826 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200827
828 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
829 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200830 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
831
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100832 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200833 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100834 rc = ns2_tx_status(nsvc, cause, 0, msg);
835 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200836 }
837 }
838
Alexander Couzens43771df2021-01-20 13:03:03 +0100839 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
840 nsei = tlvp_val16be(tp, NS_IE_NSEI);
841 if (nsei != nsvc->nse->nsei) {
842 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
843 if (nsh->pdu_type == NS_PDUT_RESET)
844 ns2_tx_reset_ack(nsvc);
845
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100846 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 +0100847 goto out;
848 }
849 }
850
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100851 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
852 nsvci = tlvp_val16be(tp, NS_IE_VCI);
853 if (nsvci != nsvc->nsvci) {
854 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
855 if (nsh->pdu_type == NS_PDUT_RESET)
856 ns2_tx_reset_ack(nsvc);
857
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100858 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 +0100859 goto out;
860 }
861 }
862
Alexander Couzens6a161492020-07-12 13:45:50 +0200863 switch (nsh->pdu_type) {
864 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100865 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200866 break;
867 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100868 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200869 break;
870 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100871 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200872 break;
873 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100874 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200875 break;
876 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100877 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200878 break;
879 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100880 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200881 break;
882 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100883 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200884 break;
885 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100886 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200887 break;
888 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100889 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100890 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100891 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200892 default:
Harald Weltef2949742021-01-20 14:54:14 +0100893 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
894 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +0100895 rc = -EINVAL;
896 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 }
898
Alexander Couzenscce88282020-10-26 00:25:50 +0100899out:
900 msgb_free(msg);
901
902 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200903}
904
Harald Welte5bef2cc2020-09-18 22:33:24 +0200905/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100906int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200907{
908 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
909}
910
911/* initialize osmo_ctx on main tread */
912static __attribute__((constructor)) void on_dso_load_ctx(void)
913{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100914 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200915}