blob: 8604bbead2304e799a643cc6755d35c230a8b137 [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
52#define DNS 10
53
54struct gprs_ns2_vc_priv {
55 struct gprs_ns2_vc *nsvc;
56 /* how often the timer was triggered */
57 int N;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010058 /* The initiator is responsible to UNBLOCK the VC. The BSS is usually the initiator.
59 * It can change during runtime. The side which blocks an unblocked side.*/
60 bool initiate_block;
61 bool initiate_reset;
Alexander Couzens47afc422021-01-17 20:12:46 +010062 /* if blocked by O&M/vty */
63 bool om_blocked;
64 /* if unitdata is forwarded to the user */
65 bool accept_unitdata;
Alexander Couzens6a161492020-07-12 13:45:50 +020066
67 /* the alive counter is present in all states */
68 struct {
69 struct osmo_timer_list timer;
70 enum ns2_timeout mode;
71 int N;
72 struct timeval timer_started;
73 } alive;
74};
75
76
77/* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure..
78 *
79 * With RESET/BLOCK, the state should follow:
80 * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED
81 *
82 * Without RESET/BLOCK, the state should follow:
83 * - UNCONFIGURED -> ALIVE -> UNBLOCKED
84 *
85 * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive.
86 * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU.
87 * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED.
88 *
89 * The ALIVE state is used as intermediate, because a VC is only valid if it received an Alive ACK when
90 * not using RESET/BLOCK procedure.
91 */
92
93enum gprs_ns2_vc_state {
94 GPRS_NS2_ST_UNCONFIGURED,
95 GPRS_NS2_ST_RESET,
96 GPRS_NS2_ST_BLOCKED,
97 GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */
98
99 GPRS_NS2_ST_ALIVE, /* only used when not using RESET/BLOCK procedure */
100};
101
102enum gprs_ns2_vc_event {
103 GPRS_NS2_EV_START,
104
105 /* received messages */
106 GPRS_NS2_EV_RESET,
107 GPRS_NS2_EV_RESET_ACK,
108 GPRS_NS2_EV_UNBLOCK,
109 GPRS_NS2_EV_UNBLOCK_ACK,
110 GPRS_NS2_EV_BLOCK,
111 GPRS_NS2_EV_BLOCK_ACK,
112 GPRS_NS2_EV_ALIVE,
113 GPRS_NS2_EV_ALIVE_ACK,
114 GPRS_NS2_EV_STATUS,
115
116 GPRS_NS2_EV_UNITDATA,
Daniel Willmanned0c9822020-11-18 14:08:07 +0100117
Alexander Couzens47afc422021-01-17 20:12:46 +0100118 GPRS_NS2_EV_FORCE_UNCONFIGURED, /* called via vty for tests */
119 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
123static const struct value_string gprs_ns2_vc_event_names[] = {
Daniel Willmanned0c9822020-11-18 14:08:07 +0100124 { GPRS_NS2_EV_START, "START" },
125 { GPRS_NS2_EV_RESET, "RESET" },
126 { GPRS_NS2_EV_RESET_ACK, "RESET_ACK" },
127 { GPRS_NS2_EV_UNBLOCK, "UNBLOCK" },
128 { GPRS_NS2_EV_UNBLOCK_ACK, "UNBLOCK_ACK" },
129 { GPRS_NS2_EV_BLOCK, "BLOCK" },
130 { GPRS_NS2_EV_BLOCK_ACK, "BLOCK_ACK" },
131 { GPRS_NS2_EV_ALIVE, "ALIVE" },
132 { GPRS_NS2_EV_ALIVE_ACK, "ALIVE_ACK" },
133 { GPRS_NS2_EV_STATUS, "STATUS" },
134 { GPRS_NS2_EV_UNITDATA, "UNITDATA" },
Alexander Couzens5443ff82021-01-15 04:36:05 +0100135 { GPRS_NS2_EV_FORCE_UNCONFIGURED, "FORCE_UNCONFIGURED" },
Alexander Couzens47afc422021-01-17 20:12:46 +0100136 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
137 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200138 { 0, NULL }
139};
140
141static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
142{
143 struct gprs_ns2_vc_priv *priv = fi->priv;
144 return priv->nsvc->nse->nsi;
145}
146
147static void start_test_procedure(struct gprs_ns2_vc_priv *priv)
148{
149 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
150
151 if (osmo_timer_pending(&priv->alive.timer))
152 return;
153
154 priv->alive.mode = NS_TOUT_TNS_ALIVE;
155 priv->alive.N = 0;
156
157 osmo_gettimeofday(&priv->alive.timer_started, NULL);
158 ns2_tx_alive(priv->nsvc);
159 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
160}
161
162static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
163{
164 osmo_timer_del(&priv->alive.timer);
165}
166
167static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
168{
169 struct timeval now, elapsed;
170 osmo_gettimeofday(&now, NULL);
171 timersub(&now, &priv->alive.timer_started, &elapsed);
172
173 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
174}
175
176static void recv_test_procedure(struct osmo_fsm_inst *fi)
177{
178 struct gprs_ns2_vc_priv *priv = fi->priv;
179 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
180 struct gprs_ns2_vc *nsvc = priv->nsvc;
181
182 /* ignoring ACKs without sending an ALIVE */
183 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
184 return;
185
186 priv->alive.mode = NS_TOUT_TNS_TEST;
187 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
188 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
189 alive_timer_elapsed_ms(priv));
190}
191
192
193static void alive_timeout_handler(void *data)
194{
195 struct osmo_fsm_inst *fi = data;
196 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
197 struct gprs_ns2_vc_priv *priv = fi->priv;
198
199 switch (priv->alive.mode) {
200 case NS_TOUT_TNS_TEST:
201 priv->alive.mode = NS_TOUT_TNS_ALIVE;
202 ns2_tx_alive(priv->nsvc);
203 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
204 break;
205 case NS_TOUT_TNS_ALIVE:
206 priv->alive.N++;
207
208 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
209 /* retransmission */
210 ns2_tx_alive(priv->nsvc);
211 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
212 } else {
213 /* lost connection */
214 if (priv->nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
215 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
216 } else {
217 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
218 }
219 }
220 break;
221 default:
222 break;
223 }
224}
225
226static void gprs_ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
227{
228 struct gprs_ns2_vc_priv *priv = fi->priv;
229 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
230
231 switch (event) {
232 case GPRS_NS2_EV_START:
233 switch (priv->nsvc->mode) {
234 case NS2_VC_MODE_ALIVE:
235 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
236 break;
237 case NS2_VC_MODE_BLOCKRESET:
238 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
239 break;
240 }
241
242 break;
243 default:
244 OSMO_ASSERT(0);
245 }
246}
247
248
249static void gprs_ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
250{
251 struct gprs_ns2_vc_priv *priv = fi->priv;
252
253 if (old_state != GPRS_NS2_ST_RESET)
254 priv->N = 0;
255
Alexander Couzens47afc422021-01-17 20:12:46 +0100256 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100257 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200258 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
259
260 stop_test_procedure(priv);
261 ns2_nse_notify_unblocked(priv->nsvc, false);
262}
263
264static void gprs_ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
265{
266 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
267 struct gprs_ns2_vc_priv *priv = fi->priv;
268
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100269 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 switch (event) {
271 case GPRS_NS2_EV_RESET_ACK:
272 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
273 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
274 break;
275 }
276 } else {
277 /* we are on the receiving end */
278 switch (event) {
279 case GPRS_NS2_EV_RESET:
280 ns2_tx_reset_ack(priv->nsvc);
281 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
282 0, 0);
283 break;
284 }
285 }
286}
287
288static void gprs_ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
289{
290 struct gprs_ns2_vc_priv *priv = fi->priv;
291
292 if (old_state != GPRS_NS2_ST_BLOCKED)
293 priv->N = 0;
294
Alexander Couzens47afc422021-01-17 20:12:46 +0100295 if (priv->om_blocked) {
296 /* we are already blocked after a RESET */
297 if (old_state == GPRS_NS2_ST_RESET) {
298 osmo_timer_del(&fi->timer);
299 } else {
300 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
301 }
302 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200303 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100304 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200305
306 start_test_procedure(priv);
307}
308
309static void gprs_ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
310{
311 struct gprs_ns2_vc_priv *priv = fi->priv;
312
Alexander Couzens47afc422021-01-17 20:12:46 +0100313 if (priv->om_blocked) {
314 switch (event) {
315 case GPRS_NS2_EV_BLOCK_ACK:
316 priv->accept_unitdata = false;
317 osmo_timer_del(&fi->timer);
318 break;
319 case GPRS_NS2_EV_BLOCK:
320 priv->accept_unitdata = false;
321 ns2_tx_block_ack(priv->nsvc);
322 osmo_timer_del(&fi->timer);
323 break;
324 case GPRS_NS2_EV_UNBLOCK:
325 priv->accept_unitdata = false;
326 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
327 osmo_timer_add(&fi->timer);
328 break;
329 }
330 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200331 switch (event) {
332 case GPRS_NS2_EV_BLOCK:
333 /* TODO: BLOCK is a UNBLOCK_NACK */
334 ns2_tx_block_ack(priv->nsvc);
335 break;
336 case GPRS_NS2_EV_UNBLOCK:
337 ns2_tx_unblock_ack(priv->nsvc);
338 /* fall through */
339 case GPRS_NS2_EV_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100340 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200341 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
342 0, NS_TOUT_TNS_TEST);
343 break;
344 }
345 } else {
346 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
347 switch (event) {
348 case GPRS_NS2_EV_UNBLOCK:
349 ns2_tx_unblock_ack(priv->nsvc);
350 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
351 0, 0);
352 break;
353 }
354 }
355}
356
357static void gprs_ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
358{
359 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100360 struct gprs_ns2_vc *nsvc = priv->nsvc;
361 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200362
Alexander Couzens47afc422021-01-17 20:12:46 +0100363 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100364 ns2_nse_notify_unblocked(nsvc, true);
365 ns2_prim_status_ind(nse, nsvc, 0, NS_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200366}
367
368static void gprs_ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
369{
370 struct gprs_ns2_vc_priv *priv = fi->priv;
371
372 switch (event) {
Alexander Couzensc4b74622021-01-10 20:44:26 +0100373 case GPRS_NS2_EV_UNBLOCK:
374 ns2_tx_unblock_ack(priv->nsvc);
375 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200376 case GPRS_NS2_EV_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100377 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200378 ns2_tx_block_ack(priv->nsvc);
379 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
380 0, 2);
381 break;
382 }
383}
384
385static void gprs_ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
386{
387 switch (event) {
388 case GPRS_NS2_EV_ALIVE_ACK:
389 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
390 break;
391 }
392}
393
394static void gprs_ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
395{
396 struct gprs_ns2_vc_priv *priv = fi->priv;
397 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
398
399 priv->alive.mode = NS_TOUT_TNS_TEST;
400 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
401
402 if (old_state != GPRS_NS2_ST_ALIVE)
403 priv->N = 0;
404
405 ns2_tx_alive(priv->nsvc);
406 ns2_nse_notify_unblocked(priv->nsvc, false);
407}
408
409static void gprs_ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
410{
411 start_test_procedure(fi->priv);
412}
413
414static const struct osmo_fsm_state gprs_ns2_vc_states[] = {
415 [GPRS_NS2_ST_UNCONFIGURED] = {
416 .in_event_mask = S(GPRS_NS2_EV_START),
417 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE),
418 .name = "UNCONFIGURED",
419 .action = gprs_ns2_st_unconfigured,
420 },
421 [GPRS_NS2_ST_RESET] = {
422 .in_event_mask = S(GPRS_NS2_EV_RESET_ACK) | S(GPRS_NS2_EV_RESET),
423 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100424 S(GPRS_NS2_ST_BLOCKED) |
425 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200426 .name = "RESET",
427 .action = gprs_ns2_st_reset,
428 .onenter = gprs_ns2_st_reset_onenter,
429 },
430 [GPRS_NS2_ST_BLOCKED] = {
431 .in_event_mask = S(GPRS_NS2_EV_BLOCK) | S(GPRS_NS2_EV_BLOCK_ACK) |
432 S(GPRS_NS2_EV_UNBLOCK) | S(GPRS_NS2_EV_UNBLOCK_ACK),
433 .out_state_mask = S(GPRS_NS2_ST_RESET) |
434 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100435 S(GPRS_NS2_ST_BLOCKED) |
436 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200437 .name = "BLOCKED",
438 .action = gprs_ns2_st_blocked,
439 .onenter = gprs_ns2_st_blocked_onenter,
440 },
441 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensc4b74622021-01-10 20:44:26 +0100442 .in_event_mask = S(GPRS_NS2_EV_BLOCK) | S(GPRS_NS2_EV_UNBLOCK_ACK) |
443 S(GPRS_NS2_EV_UNBLOCK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200444 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100445 S(GPRS_NS2_ST_BLOCKED) |
446 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200447 .name = "UNBLOCKED",
448 .action = gprs_ns2_st_unblocked,
449 .onenter = gprs_ns2_st_unblocked_on_enter,
450 },
451
452 /* ST_ALIVE is only used on VC without RESET/BLOCK */
453 [GPRS_NS2_ST_ALIVE] = {
454 .in_event_mask = S(GPRS_NS2_EV_ALIVE_ACK),
455 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100456 S(GPRS_NS2_ST_UNBLOCKED) |
457 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200458 .name = "ALIVE",
459 .action = gprs_ns2_st_alive,
460 .onenter = gprs_ns2_st_alive_onenter,
461 .onleave = gprs_ns2_st_alive_onleave,
462 },
463};
464
465static int gprs_ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
466{
467 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
468 struct gprs_ns2_vc_priv *priv = fi->priv;
469
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100470 switch (fi->state) {
471 case GPRS_NS2_ST_RESET:
472 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200473 priv->N++;
474 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
475 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
476 } else {
477 priv->N = 0;
478 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
479 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100480 }
481 break;
482 case GPRS_NS2_ST_BLOCKED:
483 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200484 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100485 if (priv->om_blocked) {
486 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
487 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
488 } else {
489 /* 7.2 stop accepting data when BLOCK PDU not responded */
490 priv->accept_unitdata = false;
491 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200492 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100493 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
494 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
495 } else {
496 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
497 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200498 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100499 }
500 break;
501 case GPRS_NS2_ST_ALIVE:
502 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200503 priv->N++;
504 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
505 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
506 } else {
507 priv->N = 0;
508 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
509 }
510 break;
511 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100512 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200513 }
514 return 0;
515}
516
517static void gprs_ns2_recv_unitdata(struct osmo_fsm_inst *fi,
518 struct msgb *msg)
519{
520 struct gprs_ns2_vc_priv *priv = fi->priv;
521 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
522 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
523 struct osmo_gprs_ns2_prim nsp = {};
524 uint16_t bvci;
525
Alexander Couzenscce88282020-10-26 00:25:50 +0100526 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
527 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200528 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100529 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200530
531 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
532 * for a PTP BVC may indicate a request to change the IP endpoint
533 * and/or a response to a change in the IP endpoint. */
534
535 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
536 bvci = nsh->data[1] << 8 | nsh->data[2];
537
Alexander Couzens89acdef2020-09-23 18:22:31 +0200538 msg->l3h = &nsh->data[3];
539 nsp.bvci = bvci;
540 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200541
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200542 /* 10.3.9 NS SDU Control Bits */
543 if (nsh->data[0] & 0x1)
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE;
545
546 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
547 PRIM_OP_INDICATION, msg);
548 nsi->cb(&nsp.oph, nsi->cb_data);
549}
550
551static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
552 uint32_t event,
553 void *data)
554{
555 struct gprs_ns2_vc_priv *priv = fi->priv;
556 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100557 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200558
559 switch (event) {
560 case GPRS_NS2_EV_RESET:
561 if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET)
562 break;
563
564 /* move the FSM into reset */
565 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100566 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200567 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
568 }
569 /* pass the event down into FSM action */
570 gprs_ns2_st_reset(fi, event, data);
571 break;
572 case GPRS_NS2_EV_ALIVE:
573 switch (fi->state) {
574 case GPRS_NS2_ST_UNCONFIGURED:
575 case GPRS_NS2_ST_RESET:
576 /* ignore ALIVE */
577 break;
578 default:
579 ns2_tx_alive_ack(priv->nsvc);
580 }
581 break;
582 case GPRS_NS2_EV_ALIVE_ACK:
583 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
584 if (fi->state == GPRS_NS2_ST_ALIVE)
585 gprs_ns2_st_alive(fi, event, data);
586 else
587 recv_test_procedure(fi);
588 break;
589 case GPRS_NS2_EV_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100590 /* UNITDATA has to handle the release of msg.
591 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
592 * the msg, the upper layer has to do it.
593 * Otherwise the msg must be freed.
594 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200595 switch (fi->state) {
596 case GPRS_NS2_ST_BLOCKED:
597 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100598 if (priv->accept_unitdata) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100599 gprs_ns2_recv_unitdata(fi, msg);
600 return;
601 }
602
603 ns2_tx_status(priv->nsvc,
604 NS_CAUSE_NSVC_BLOCKED,
605 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200606 break;
607 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
608 case GPRS_NS2_ST_ALIVE:
609 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzenscce88282020-10-26 00:25:50 +0100610 gprs_ns2_recv_unitdata(fi, msg);
611 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200612 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100613
614 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200615 break;
Daniel Willmanned0c9822020-11-18 14:08:07 +0100616 case GPRS_NS2_EV_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100617 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
618 /* Force the NSVC back to its initial state */
619 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
620 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_START, NULL);
621 return;
622 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100623 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100624 case GPRS_NS2_EV_REQ_OM_BLOCK:
625 /* vty cmd: block */
626 priv->initiate_block = true;
627 priv->om_blocked = true;
628 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
629 break;
630 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
631 /* vty cmd: unblock*/
632 if (!priv->om_blocked)
633 return;
634 priv->om_blocked = false;
635 if (fi->state == GPRS_NS2_ST_BLOCKED)
636 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
637 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200638 }
639}
640
Alexander Couzens0346b642020-10-27 13:05:56 +0100641static void gprs_ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
642 enum osmo_fsm_term_cause cause)
643{
644 struct gprs_ns2_vc_priv *priv = fi->priv;
645
646 osmo_timer_del(&priv->alive.timer);
647}
648
Alexander Couzens6a161492020-07-12 13:45:50 +0200649static struct osmo_fsm gprs_ns2_vc_fsm = {
650 .name = "GPRS-NS2-VC",
651 .states = gprs_ns2_vc_states,
652 .num_states = ARRAY_SIZE(gprs_ns2_vc_states),
653 .allstate_event_mask = S(GPRS_NS2_EV_UNITDATA) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100654 S(GPRS_NS2_EV_RESET) |
Alexander Couzens6a161492020-07-12 13:45:50 +0200655 S(GPRS_NS2_EV_ALIVE) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100656 S(GPRS_NS2_EV_ALIVE_ACK) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100657 S(GPRS_NS2_EV_FORCE_UNCONFIGURED) |
658 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
659 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200660 .allstate_action = gprs_ns2_vc_fsm_allstate_action,
Alexander Couzens0346b642020-10-27 13:05:56 +0100661 .cleanup = gprs_ns2_vc_fsm_clean,
Alexander Couzens6a161492020-07-12 13:45:50 +0200662 .timer_cb = gprs_ns2_vc_fsm_timer_cb,
663 /* .log_subsys = DNS, "is not constant" */
664 .event_names = gprs_ns2_vc_event_names,
665 .pre_term = NULL,
666 .log_subsys = DLNS,
667};
668
669/*!
670 * \brief gprs_ns2_vc_fsm_alloc
671 * \param ctx
672 * \param vc
673 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100674 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200675 * \return NULL on error, otherwise the fsm
676 */
677struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100678 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200679{
680 struct osmo_fsm_inst *fi;
681 struct gprs_ns2_vc_priv *priv;
682
683 fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
684 if (!fi)
685 return fi;
686
687 nsvc->fi = fi;
688 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
689 priv->nsvc = nsvc;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100690 priv->initiate_reset = initiator;
691 priv->initiate_block = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200692
693 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
694
695 return fi;
696}
697
Harald Welte5bef2cc2020-09-18 22:33:24 +0200698/*! Start a NS-VC FSM.
699 * \param nsvc the virtual circuit
700 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200701int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
702{
703 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
704 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
705 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_START, NULL);
706 return 0;
707}
708
Daniel Willmanned0c9822020-11-18 14:08:07 +0100709/*! Reset a NS-VC FSM.
710 * \param nsvc the virtual circuit
711 * \return 0 on success; negative on error */
712int gprs_ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
713{
714 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_FORCE_UNCONFIGURED, NULL);
715}
716
Alexander Couzens47afc422021-01-17 20:12:46 +0100717/*! Block a NS-VC.
718 * \param nsvc the virtual circuit
719 * \return 0 on success; negative on error */
720int ns2_vc_block(struct gprs_ns2_vc *nsvc)
721{
722 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
723}
724
725/*! Unblock a NS-VC.
726 * \param nsvc the virtual circuit
727 * \return 0 on success; negative on error */
728int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
729{
730 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
731}
732
Harald Welte5bef2cc2020-09-18 22:33:24 +0200733/*! entry point for messages from the driver/VL
734 * \param nsvc virtual circuit on which the message was received
735 * \param msg message that was received
736 * \param tp parsed TLVs of the received message
737 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200738int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
739{
740 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
741 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100742 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200743 uint8_t cause;
744
745 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
746 * if not answer STATUS with "NS-VC unknown" */
747 /* TODO: handle RESET with different VCI */
748 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
749
750 if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
751 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100752 rc = ns2_tx_status(nsvc, cause, 0, msg);
753 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200754 }
755 }
756
757 switch (nsh->pdu_type) {
758 case NS_PDUT_RESET:
759 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET, tp);
760 break;
761 case NS_PDUT_RESET_ACK:
762 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET_ACK, tp);
763 break;
764 case NS_PDUT_BLOCK:
765 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK, tp);
766 break;
767 case NS_PDUT_BLOCK_ACK:
768 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK_ACK, tp);
769 break;
770 case NS_PDUT_UNBLOCK:
771 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK, tp);
772 break;
773 case NS_PDUT_UNBLOCK_ACK:
774 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK_ACK, tp);
775 break;
776 case NS_PDUT_ALIVE:
777 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE, tp);
778 break;
779 case NS_PDUT_ALIVE_ACK:
780 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE_ACK, tp);
781 break;
782 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100783 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzens6a161492020-07-12 13:45:50 +0200784 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100785 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200786 default:
787 LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
788 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
789 return -EINVAL;
790 }
791
Alexander Couzenscce88282020-10-26 00:25:50 +0100792out:
793 msgb_free(msg);
794
795 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200796}
797
Harald Welte5bef2cc2020-09-18 22:33:24 +0200798/*! is the given NS-VC unblocked? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200799int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
800{
801 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
802}
803
804/* initialize osmo_ctx on main tread */
805static __attribute__((constructor)) void on_dso_load_ctx(void)
806{
807 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0);
808}