blob: f4d88f48717017d9fb8c985437efb3d3d18c8104 [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 */
118 GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */
119 GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/
Alexander Couzens6a161492020-07-12 13:45:50 +0200120};
121
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100122static const struct value_string ns2_vc_event_names[] = {
Harald Welteb339cbb2021-02-05 14:49:21 +0100123 { GPRS_NS2_EV_REQ_START, "REQ-START" },
124 { GPRS_NS2_EV_RX_RESET, "RX-RESET" },
125 { GPRS_NS2_EV_RX_RESET_ACK, "RX-RESET_ACK" },
126 { GPRS_NS2_EV_RX_UNBLOCK, "RX-UNBLOCK" },
127 { GPRS_NS2_EV_RX_UNBLOCK_ACK, "RX-UNBLOCK_ACK" },
128 { GPRS_NS2_EV_RX_BLOCK, "RX-BLOCK" },
129 { GPRS_NS2_EV_RX_BLOCK_ACK, "RX-BLOCK_ACK" },
130 { GPRS_NS2_EV_RX_ALIVE, "RX-ALIVE" },
131 { GPRS_NS2_EV_RX_ALIVE_ACK, "RX-ALIVE_ACK" },
132 { GPRS_NS2_EV_RX_STATUS, "RX-STATUS" },
133 { GPRS_NS2_EV_RX_UNITDATA, "RX-UNITDATA" },
134 { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "REQ-FORCE_UNCONFIGURED" },
Alexander Couzens47afc422021-01-17 20:12:46 +0100135 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
136 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200137 { 0, NULL }
138};
139
140static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
141{
142 struct gprs_ns2_vc_priv *priv = fi->priv;
143 return priv->nsvc->nse->nsi;
144}
145
Harald Welte5e408312021-03-23 18:16:30 +0100146/* Start the NS-TEST procedure, either with transmitting a tx_alive,
147 * (start_tx_alive==true) or with starting tns-test */
148static void start_test_procedure(struct osmo_fsm_inst *fi, bool start_tx_alive)
Alexander Couzens6a161492020-07-12 13:45:50 +0200149{
Harald Welte5e408312021-03-23 18:16:30 +0100150 struct gprs_ns2_vc_priv *priv = fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200151 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
Harald Welte5e408312021-03-23 18:16:30 +0100152 unsigned int tout_idx;
Alexander Couzens6a161492020-07-12 13:45:50 +0200153
Harald Welte5e408312021-03-23 18:16:30 +0100154 if (osmo_timer_pending(&priv->alive.timer)) {
155 if (start_tx_alive) {
156 if (priv->alive.mode == NS_TOUT_TNS_ALIVE)
157 return;
158 } else {
159 if (priv->alive.mode == NS_TOUT_TNS_TEST)
160 return;
161 }
162 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200163
Alexander Couzens6a161492020-07-12 13:45:50 +0200164 priv->alive.N = 0;
165
Harald Welte5e408312021-03-23 18:16:30 +0100166 if (start_tx_alive) {
167 priv->alive.mode = NS_TOUT_TNS_ALIVE;
168 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
169 ns2_tx_alive(priv->nsvc);
170 tout_idx = NS_TOUT_TNS_ALIVE;
171 } else {
172 priv->alive.mode = NS_TOUT_TNS_TEST;
173 tout_idx = NS_TOUT_TNS_TEST;
174 }
175 LOGPFSML(fi, LOGL_DEBUG, "Starting Tns-%s of %u seconds\n",
176 tout_idx == NS_TOUT_TNS_ALIVE ? "alive" : "test", nsi->timeout[tout_idx]);
177 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[tout_idx], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200178}
179
180static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
181{
182 osmo_timer_del(&priv->alive.timer);
183}
184
Harald Welte5e408312021-03-23 18:16:30 +0100185/* how many milliseconds have expired since the last alive timer start? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200186static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
187{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100188 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200189
Alexander Couzensab0e8642021-02-12 02:50:44 +0100190 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
191 return 0;
192
193 timespecsub(&now, &priv->alive.timer_started, &elapsed);
Alexander Couzensab0e8642021-02-12 02:50:44 +0100194 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200195}
196
Harald Welte5e408312021-03-23 18:16:30 +0100197/* we just received a NS-ALIVE-ACK; re-schedule after Tns-test */
Alexander Couzens6a161492020-07-12 13:45:50 +0200198static void recv_test_procedure(struct osmo_fsm_inst *fi)
199{
200 struct gprs_ns2_vc_priv *priv = fi->priv;
201 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
202 struct gprs_ns2_vc *nsvc = priv->nsvc;
203
204 /* ignoring ACKs without sending an ALIVE */
205 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
206 return;
207
208 priv->alive.mode = NS_TOUT_TNS_TEST;
209 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
210 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
211 alive_timer_elapsed_ms(priv));
212}
213
214
215static void alive_timeout_handler(void *data)
216{
217 struct osmo_fsm_inst *fi = data;
218 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
219 struct gprs_ns2_vc_priv *priv = fi->priv;
220
221 switch (priv->alive.mode) {
222 case NS_TOUT_TNS_TEST:
223 priv->alive.mode = NS_TOUT_TNS_ALIVE;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100224 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100225 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200226 ns2_tx_alive(priv->nsvc);
227 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
228 break;
229 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100230 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200231 priv->alive.N++;
232
233 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
234 /* retransmission */
235 ns2_tx_alive(priv->nsvc);
236 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
237 } else {
238 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100239 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200240 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
241 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100242 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200243 }
244 }
245 break;
246 default:
247 break;
248 }
249}
250
Harald Welte6a9ec422021-01-31 18:56:29 +0100251
252static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
253{
254 stop_test_procedure(fi->priv);
255}
256
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100257static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200258{
259 struct gprs_ns2_vc_priv *priv = fi->priv;
260 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
261
Alexander Couzensea01bf22021-01-18 14:01:01 +0100262 priv->initiate_reset = priv->initiate_block = priv->initiator;
263 priv->om_blocked = false;
264
Alexander Couzens6a161492020-07-12 13:45:50 +0200265 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100266 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100268 case GPRS_NS2_VC_MODE_ALIVE:
Harald Weltec51ddf22021-03-05 09:48:18 +0100269 if (priv->nsvc->nse->dialect == GPRS_NS2_DIALECT_SNS) {
270 /* In IP-SNS, the NS-VC are assumed initially alive, until the alive
271 * procedure should fail at some future point */
272 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
273 } else {
274 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
275 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200276 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100277 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200278 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
279 break;
280 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 break;
282 default:
283 OSMO_ASSERT(0);
284 }
285}
286
287
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100288static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200289{
290 struct gprs_ns2_vc_priv *priv = fi->priv;
291
292 if (old_state != GPRS_NS2_ST_RESET)
293 priv->N = 0;
294
Alexander Couzens47afc422021-01-17 20:12:46 +0100295 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100296 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200297 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
298
299 stop_test_procedure(priv);
300 ns2_nse_notify_unblocked(priv->nsvc, false);
301}
302
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100303static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200304{
305 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
306 struct gprs_ns2_vc_priv *priv = fi->priv;
307
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100308 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200309 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100310 case GPRS_NS2_EV_RX_RESET:
311 ns2_tx_reset_ack(priv->nsvc);
312 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100313 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200314 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
315 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
316 break;
317 }
318 } else {
319 /* we are on the receiving end */
320 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100321 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200322 ns2_tx_reset_ack(priv->nsvc);
323 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
324 0, 0);
325 break;
326 }
327 }
328}
329
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100330static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200331{
332 struct gprs_ns2_vc_priv *priv = fi->priv;
333
Harald Welte4c54f592021-01-30 22:42:15 +0100334 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200335 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100336 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
337 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200338
Alexander Couzens47afc422021-01-17 20:12:46 +0100339 if (priv->om_blocked) {
340 /* we are already blocked after a RESET */
341 if (old_state == GPRS_NS2_ST_RESET) {
342 osmo_timer_del(&fi->timer);
343 } else {
344 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
345 }
346 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200347 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100348 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200349
Harald Welte5e408312021-03-23 18:16:30 +0100350 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200351}
352
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100353static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200354{
355 struct gprs_ns2_vc_priv *priv = fi->priv;
356
Alexander Couzens47afc422021-01-17 20:12:46 +0100357 if (priv->om_blocked) {
358 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100359 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100360 priv->accept_unitdata = false;
361 osmo_timer_del(&fi->timer);
362 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100363 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100364 priv->accept_unitdata = false;
365 ns2_tx_block_ack(priv->nsvc);
366 osmo_timer_del(&fi->timer);
367 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100368 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100369 priv->accept_unitdata = false;
370 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
371 osmo_timer_add(&fi->timer);
372 break;
373 }
374 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200375 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100376 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200377 /* TODO: BLOCK is a UNBLOCK_NACK */
378 ns2_tx_block_ack(priv->nsvc);
379 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100380 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200381 ns2_tx_unblock_ack(priv->nsvc);
382 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100383 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100384 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200385 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
386 0, NS_TOUT_TNS_TEST);
387 break;
388 }
389 } else {
390 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
391 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100392 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100393 ns2_tx_block_ack(priv->nsvc);
394 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100395 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200396 ns2_tx_unblock_ack(priv->nsvc);
397 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
398 0, 0);
399 break;
400 }
401 }
402}
403
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100404static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200405{
406 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100407 struct gprs_ns2_vc *nsvc = priv->nsvc;
408 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200409
Harald Welteb40bf8b2021-01-30 22:43:01 +0100410 if (old_state != GPRS_NS2_ST_UNBLOCKED)
411 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
412
Alexander Couzens47afc422021-01-17 20:12:46 +0100413 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100414 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100415 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Harald Welte5e408312021-03-23 18:16:30 +0100416
417 /* the closest interpretation of the spec would start Tns-test here first,
418 * and only send a NS-ALIVE after Tns-test has expired (i.e. setting the
419 * second argument to 'false'. However, being quick in detecting unavailability
420 * of a NS-VC seems like a good idea */
421 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200422}
423
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100424static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200425{
426 struct gprs_ns2_vc_priv *priv = fi->priv;
427
428 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100429 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100430 ns2_tx_unblock_ack(priv->nsvc);
431 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100432 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100433 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200434 ns2_tx_block_ack(priv->nsvc);
435 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
436 0, 2);
437 break;
438 }
439}
440
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100441static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200442{
443 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100444 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200445 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
446 break;
447 }
448}
449
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100450static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200451{
452 struct gprs_ns2_vc_priv *priv = fi->priv;
453 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
454
455 priv->alive.mode = NS_TOUT_TNS_TEST;
456 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
457
Alexander Couzensd3e31102021-02-03 11:15:07 +0100458 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200459 priv->N = 0;
460
Alexander Couzens6a161492020-07-12 13:45:50 +0200461 ns2_nse_notify_unblocked(priv->nsvc, false);
Harald Welte5e408312021-03-23 18:16:30 +0100462 start_test_procedure(fi, true);
Alexander Couzens6a161492020-07-12 13:45:50 +0200463}
464
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100465static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200466 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100467 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Harald Weltec51ddf22021-03-05 09:48:18 +0100468 .out_state_mask = S(GPRS_NS2_ST_RESET) |
469 S(GPRS_NS2_ST_RECOVERING) |
470 S(GPRS_NS2_ST_UNBLOCKED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100472 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100473 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200474 },
475 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100476 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200477 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100478 S(GPRS_NS2_ST_BLOCKED) |
479 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200480 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100481 .action = ns2_st_reset,
482 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200483 },
484 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100485 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
486 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200487 .out_state_mask = S(GPRS_NS2_ST_RESET) |
488 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100489 S(GPRS_NS2_ST_BLOCKED) |
490 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200491 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100492 .action = ns2_st_blocked,
493 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200494 },
495 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100496 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
497 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100498 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
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 = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100502 .action = ns2_st_unblocked,
503 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200504 },
505
Alexander Couzensd3e31102021-02-03 11:15:07 +0100506 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
507 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100508 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100509 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100510 S(GPRS_NS2_ST_UNBLOCKED) |
511 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100512 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100513 .action = ns2_st_alive,
514 .onenter = ns2_st_alive_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200515 },
516};
517
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100518static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200519{
520 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
521 struct gprs_ns2_vc_priv *priv = fi->priv;
522
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100523 switch (fi->state) {
524 case GPRS_NS2_ST_RESET:
525 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100526 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 priv->N++;
528 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
529 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
530 } else {
531 priv->N = 0;
532 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
533 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100534 }
535 break;
536 case GPRS_NS2_ST_BLOCKED:
537 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200538 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100539 if (priv->om_blocked) {
540 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
541 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
542 } else {
543 /* 7.2 stop accepting data when BLOCK PDU not responded */
544 priv->accept_unitdata = false;
545 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200546 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100547 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
548 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
549 } else {
550 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
551 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200552 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100553 }
554 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100555 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100556 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200557 priv->N++;
558 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100559 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200560 } else {
561 priv->N = 0;
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 }
564 break;
565 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100566 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200567 }
568 return 0;
569}
570
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100571static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200572 struct msgb *msg)
573{
574 struct gprs_ns2_vc_priv *priv = fi->priv;
575 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
576 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
577 struct osmo_gprs_ns2_prim nsp = {};
578 uint16_t bvci;
579
Alexander Couzenscce88282020-10-26 00:25:50 +0100580 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
581 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200582 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100583 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200584
585 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
586 * for a PTP BVC may indicate a request to change the IP endpoint
587 * and/or a response to a change in the IP endpoint. */
588
589 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
590 bvci = nsh->data[1] << 8 | nsh->data[2];
591
Alexander Couzens89acdef2020-09-23 18:22:31 +0200592 msg->l3h = &nsh->data[3];
593 nsp.bvci = bvci;
594 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200595
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200596 /* 10.3.9 NS SDU Control Bits */
597 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100598 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200599
Alexander Couzens138b96f2021-01-25 16:23:29 +0100600 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200601 PRIM_OP_INDICATION, msg);
602 nsi->cb(&nsp.oph, nsi->cb_data);
603}
604
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100605static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200606 uint32_t event,
607 void *data)
608{
609 struct gprs_ns2_vc_priv *priv = fi->priv;
610 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100611 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200612
613 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100614 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100615 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200616 break;
617
618 /* move the FSM into reset */
619 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100620 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200621 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
622 }
623 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100624 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200625 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100626 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200627 switch (fi->state) {
628 case GPRS_NS2_ST_UNCONFIGURED:
629 case GPRS_NS2_ST_RESET:
630 /* ignore ALIVE */
631 break;
632 default:
633 ns2_tx_alive_ack(priv->nsvc);
634 }
635 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100636 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200637 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100638 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100639 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200640 else
641 recv_test_procedure(fi);
642 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100643 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100644 /* UNITDATA has to handle the release of msg.
645 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
646 * the msg, the upper layer has to do it.
647 * Otherwise the msg must be freed.
648 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200649 switch (fi->state) {
650 case GPRS_NS2_ST_BLOCKED:
651 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100652 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100653 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100654 return;
655 }
656
657 ns2_tx_status(priv->nsvc,
658 NS_CAUSE_NSVC_BLOCKED,
659 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200660 break;
661 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100662 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200663 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100664 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100665 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200666 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100667
668 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200669 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100670 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100671 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
672 /* Force the NSVC back to its initial state */
673 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100674 return;
675 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100676 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100677 case GPRS_NS2_EV_REQ_OM_BLOCK:
678 /* vty cmd: block */
679 priv->initiate_block = true;
680 priv->om_blocked = true;
681 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
682 break;
683 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
684 /* vty cmd: unblock*/
685 if (!priv->om_blocked)
686 return;
687 priv->om_blocked = false;
688 if (fi->state == GPRS_NS2_ST_BLOCKED)
689 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
690 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200691 }
692}
693
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100694static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100695 enum osmo_fsm_term_cause cause)
696{
697 struct gprs_ns2_vc_priv *priv = fi->priv;
698
699 osmo_timer_del(&priv->alive.timer);
700}
701
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100702static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200703 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100704 .states = ns2_vc_states,
705 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100706 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
707 S(GPRS_NS2_EV_RX_RESET) |
708 S(GPRS_NS2_EV_RX_ALIVE) |
709 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
710 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100711 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
712 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100713 .allstate_action = ns2_vc_fsm_allstate_action,
714 .cleanup = ns2_vc_fsm_clean,
715 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100716 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200717 .pre_term = NULL,
718 .log_subsys = DLNS,
719};
720
721/*!
722 * \brief gprs_ns2_vc_fsm_alloc
723 * \param ctx
724 * \param vc
725 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100726 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200727 * \return NULL on error, otherwise the fsm
728 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100729struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100730 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200731{
732 struct osmo_fsm_inst *fi;
733 struct gprs_ns2_vc_priv *priv;
734
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100735 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200736 if (!fi)
737 return fi;
738
739 nsvc->fi = fi;
740 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
741 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100742 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200743
744 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
745
746 return fi;
747}
748
Harald Welte5bef2cc2020-09-18 22:33:24 +0200749/*! Start a NS-VC FSM.
750 * \param nsvc the virtual circuit
751 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100752int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200753{
754 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
755 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100756 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200757 return 0;
758}
759
Daniel Willmanned0c9822020-11-18 14:08:07 +0100760/*! Reset a NS-VC FSM.
761 * \param nsvc the virtual circuit
762 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100763int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100764{
Alexander Couzensf5775432021-01-18 13:49:00 +0100765 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100766}
767
Alexander Couzens47afc422021-01-17 20:12:46 +0100768/*! Block a NS-VC.
769 * \param nsvc the virtual circuit
770 * \return 0 on success; negative on error */
771int ns2_vc_block(struct gprs_ns2_vc *nsvc)
772{
773 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
774}
775
776/*! Unblock a NS-VC.
777 * \param nsvc the virtual circuit
778 * \return 0 on success; negative on error */
779int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
780{
781 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
782}
783
Harald Welte5bef2cc2020-09-18 22:33:24 +0200784/*! entry point for messages from the driver/VL
785 * \param nsvc virtual circuit on which the message was received
786 * \param msg message that was received
787 * \param tp parsed TLVs of the received message
788 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100789int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200790{
791 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
792 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100793 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200794 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100795 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200796
797 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
798 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200799 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
800
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100801 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200802 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100803 rc = ns2_tx_status(nsvc, cause, 0, msg);
804 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200805 }
806 }
807
Alexander Couzens43771df2021-01-20 13:03:03 +0100808 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
809 nsei = tlvp_val16be(tp, NS_IE_NSEI);
810 if (nsei != nsvc->nse->nsei) {
811 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
812 if (nsh->pdu_type == NS_PDUT_RESET)
813 ns2_tx_reset_ack(nsvc);
814
815 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
816 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
817 goto out;
818 }
819 }
820
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100821 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
822 nsvci = tlvp_val16be(tp, NS_IE_VCI);
823 if (nsvci != nsvc->nsvci) {
824 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
825 if (nsh->pdu_type == NS_PDUT_RESET)
826 ns2_tx_reset_ack(nsvc);
827
828 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
829 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
830 goto out;
831 }
832 }
833
Alexander Couzens6a161492020-07-12 13:45:50 +0200834 switch (nsh->pdu_type) {
835 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100836 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200837 break;
838 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100839 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200840 break;
841 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100842 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200843 break;
844 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100845 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200846 break;
847 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100848 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200849 break;
850 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100851 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200852 break;
853 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100854 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200855 break;
856 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100857 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200858 break;
859 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100860 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100861 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100862 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200863 default:
Harald Weltef2949742021-01-20 14:54:14 +0100864 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
865 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200866 return -EINVAL;
867 }
868
Alexander Couzenscce88282020-10-26 00:25:50 +0100869out:
870 msgb_free(msg);
871
872 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200873}
874
Harald Welte5bef2cc2020-09-18 22:33:24 +0200875/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100876int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200877{
878 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
879}
880
881/* initialize osmo_ctx on main tread */
882static __attribute__((constructor)) void on_dso_load_ctx(void)
883{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100884 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200885}