blob: 33a0328b08f968564187e93f7ac0e290292a3a28 [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;
58 /* The initiater is responsible to UNBLOCK the VC. The BSS is usually the initiater.
59 * It can change while runtime. The side which blocks an unblocked side.*/
60 bool initiater;
61
62 /* the alive counter is present in all states */
63 struct {
64 struct osmo_timer_list timer;
65 enum ns2_timeout mode;
66 int N;
67 struct timeval timer_started;
68 } alive;
69};
70
71
72/* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure..
73 *
74 * With RESET/BLOCK, the state should follow:
75 * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED
76 *
77 * Without RESET/BLOCK, the state should follow:
78 * - UNCONFIGURED -> ALIVE -> UNBLOCKED
79 *
80 * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive.
81 * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU.
82 * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED.
83 *
84 * The ALIVE state is used as intermediate, because a VC is only valid if it received an Alive ACK when
85 * not using RESET/BLOCK procedure.
86 */
87
88enum gprs_ns2_vc_state {
89 GPRS_NS2_ST_UNCONFIGURED,
90 GPRS_NS2_ST_RESET,
91 GPRS_NS2_ST_BLOCKED,
92 GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */
93
94 GPRS_NS2_ST_ALIVE, /* only used when not using RESET/BLOCK procedure */
95};
96
97enum gprs_ns2_vc_event {
98 GPRS_NS2_EV_START,
99
100 /* received messages */
101 GPRS_NS2_EV_RESET,
102 GPRS_NS2_EV_RESET_ACK,
103 GPRS_NS2_EV_UNBLOCK,
104 GPRS_NS2_EV_UNBLOCK_ACK,
105 GPRS_NS2_EV_BLOCK,
106 GPRS_NS2_EV_BLOCK_ACK,
107 GPRS_NS2_EV_ALIVE,
108 GPRS_NS2_EV_ALIVE_ACK,
109 GPRS_NS2_EV_STATUS,
110
111 GPRS_NS2_EV_UNITDATA,
112};
113
114static const struct value_string gprs_ns2_vc_event_names[] = {
115 { GPRS_NS2_EV_START, "START" },
116 { GPRS_NS2_EV_RESET, "RESET" },
117 { GPRS_NS2_EV_RESET_ACK, "RESET_ACK" },
118 { GPRS_NS2_EV_UNBLOCK, "UNBLOCK" },
119 { GPRS_NS2_EV_UNBLOCK_ACK, "UNBLOCK_ACK" },
120 { GPRS_NS2_EV_BLOCK, "BLOCK" },
121 { GPRS_NS2_EV_BLOCK_ACK, "BLOCK_ACK" },
122 { GPRS_NS2_EV_ALIVE, "ALIVE" },
123 { GPRS_NS2_EV_ALIVE_ACK, "ALIVE_ACK" },
124 { GPRS_NS2_EV_STATUS, "STATUS" },
125 { GPRS_NS2_EV_UNITDATA, "UNITDATA" },
126 { 0, NULL }
127};
128
129static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
130{
131 struct gprs_ns2_vc_priv *priv = fi->priv;
132 return priv->nsvc->nse->nsi;
133}
134
135static void start_test_procedure(struct gprs_ns2_vc_priv *priv)
136{
137 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
138
139 if (osmo_timer_pending(&priv->alive.timer))
140 return;
141
142 priv->alive.mode = NS_TOUT_TNS_ALIVE;
143 priv->alive.N = 0;
144
145 osmo_gettimeofday(&priv->alive.timer_started, NULL);
146 ns2_tx_alive(priv->nsvc);
147 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
148}
149
150static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
151{
152 osmo_timer_del(&priv->alive.timer);
153}
154
155static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
156{
157 struct timeval now, elapsed;
158 osmo_gettimeofday(&now, NULL);
159 timersub(&now, &priv->alive.timer_started, &elapsed);
160
161 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
162}
163
164static void recv_test_procedure(struct osmo_fsm_inst *fi)
165{
166 struct gprs_ns2_vc_priv *priv = fi->priv;
167 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
168 struct gprs_ns2_vc *nsvc = priv->nsvc;
169
170 /* ignoring ACKs without sending an ALIVE */
171 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
172 return;
173
174 priv->alive.mode = NS_TOUT_TNS_TEST;
175 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
176 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
177 alive_timer_elapsed_ms(priv));
178}
179
180
181static void alive_timeout_handler(void *data)
182{
183 struct osmo_fsm_inst *fi = data;
184 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
185 struct gprs_ns2_vc_priv *priv = fi->priv;
186
187 switch (priv->alive.mode) {
188 case NS_TOUT_TNS_TEST:
189 priv->alive.mode = NS_TOUT_TNS_ALIVE;
190 ns2_tx_alive(priv->nsvc);
191 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
192 break;
193 case NS_TOUT_TNS_ALIVE:
194 priv->alive.N++;
195
196 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
197 /* retransmission */
198 ns2_tx_alive(priv->nsvc);
199 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
200 } else {
201 /* lost connection */
202 if (priv->nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
203 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
204 } else {
205 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
206 }
207 }
208 break;
209 default:
210 break;
211 }
212}
213
214static void gprs_ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
215{
216 struct gprs_ns2_vc_priv *priv = fi->priv;
217 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
218
219 switch (event) {
220 case GPRS_NS2_EV_START:
221 switch (priv->nsvc->mode) {
222 case NS2_VC_MODE_ALIVE:
223 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
224 break;
225 case NS2_VC_MODE_BLOCKRESET:
226 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
227 break;
228 }
229
230 break;
231 default:
232 OSMO_ASSERT(0);
233 }
234}
235
236
237static void gprs_ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
238{
239 struct gprs_ns2_vc_priv *priv = fi->priv;
240
241 if (old_state != GPRS_NS2_ST_RESET)
242 priv->N = 0;
243
244 if (priv->initiater)
245 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
246
247 stop_test_procedure(priv);
248 ns2_nse_notify_unblocked(priv->nsvc, false);
249}
250
251static void gprs_ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
252{
253 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
254 struct gprs_ns2_vc_priv *priv = fi->priv;
255
256 if (priv->initiater) {
257 switch (event) {
258 case GPRS_NS2_EV_RESET_ACK:
259 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
260 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
261 break;
262 }
263 } else {
264 /* we are on the receiving end */
265 switch (event) {
266 case GPRS_NS2_EV_RESET:
267 ns2_tx_reset_ack(priv->nsvc);
268 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
269 0, 0);
270 break;
271 }
272 }
273}
274
275static void gprs_ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
276{
277 struct gprs_ns2_vc_priv *priv = fi->priv;
278
279 if (old_state != GPRS_NS2_ST_BLOCKED)
280 priv->N = 0;
281
282 if (priv->initiater)
283 ns2_tx_unblock(priv->nsvc);
284
285 start_test_procedure(priv);
286}
287
288static void gprs_ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
289{
290 struct gprs_ns2_vc_priv *priv = fi->priv;
291
292 if (priv->initiater) {
293 switch (event) {
294 case GPRS_NS2_EV_BLOCK:
295 /* TODO: BLOCK is a UNBLOCK_NACK */
296 ns2_tx_block_ack(priv->nsvc);
297 break;
298 case GPRS_NS2_EV_UNBLOCK:
299 ns2_tx_unblock_ack(priv->nsvc);
300 /* fall through */
301 case GPRS_NS2_EV_UNBLOCK_ACK:
302 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
303 0, NS_TOUT_TNS_TEST);
304 break;
305 }
306 } else {
307 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
308 switch (event) {
309 case GPRS_NS2_EV_UNBLOCK:
310 ns2_tx_unblock_ack(priv->nsvc);
311 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
312 0, 0);
313 break;
314 }
315 }
316}
317
318static void gprs_ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
319{
320 struct gprs_ns2_vc_priv *priv = fi->priv;
321
322 ns2_nse_notify_unblocked(priv->nsvc, true);
323}
324
325static void gprs_ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
326{
327 struct gprs_ns2_vc_priv *priv = fi->priv;
328
329 switch (event) {
330 case GPRS_NS2_EV_BLOCK:
331 priv->initiater = false;
332 ns2_tx_block_ack(priv->nsvc);
333 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
334 0, 2);
335 break;
336 }
337}
338
339static void gprs_ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
340{
341 switch (event) {
342 case GPRS_NS2_EV_ALIVE_ACK:
343 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
344 break;
345 }
346}
347
348static void gprs_ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
349{
350 struct gprs_ns2_vc_priv *priv = fi->priv;
351 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
352
353 priv->alive.mode = NS_TOUT_TNS_TEST;
354 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
355
356 if (old_state != GPRS_NS2_ST_ALIVE)
357 priv->N = 0;
358
359 ns2_tx_alive(priv->nsvc);
360 ns2_nse_notify_unblocked(priv->nsvc, false);
361}
362
363static void gprs_ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
364{
365 start_test_procedure(fi->priv);
366}
367
368static const struct osmo_fsm_state gprs_ns2_vc_states[] = {
369 [GPRS_NS2_ST_UNCONFIGURED] = {
370 .in_event_mask = S(GPRS_NS2_EV_START),
371 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE),
372 .name = "UNCONFIGURED",
373 .action = gprs_ns2_st_unconfigured,
374 },
375 [GPRS_NS2_ST_RESET] = {
376 .in_event_mask = S(GPRS_NS2_EV_RESET_ACK) | S(GPRS_NS2_EV_RESET),
377 .out_state_mask = S(GPRS_NS2_ST_RESET) |
378 S(GPRS_NS2_ST_BLOCKED),
379 .name = "RESET",
380 .action = gprs_ns2_st_reset,
381 .onenter = gprs_ns2_st_reset_onenter,
382 },
383 [GPRS_NS2_ST_BLOCKED] = {
384 .in_event_mask = S(GPRS_NS2_EV_BLOCK) | S(GPRS_NS2_EV_BLOCK_ACK) |
385 S(GPRS_NS2_EV_UNBLOCK) | S(GPRS_NS2_EV_UNBLOCK_ACK),
386 .out_state_mask = S(GPRS_NS2_ST_RESET) |
387 S(GPRS_NS2_ST_UNBLOCKED) |
388 S(GPRS_NS2_ST_BLOCKED),
389 .name = "BLOCKED",
390 .action = gprs_ns2_st_blocked,
391 .onenter = gprs_ns2_st_blocked_onenter,
392 },
393 [GPRS_NS2_ST_UNBLOCKED] = {
394 .in_event_mask = S(GPRS_NS2_EV_BLOCK),
395 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE) |
396 S(GPRS_NS2_ST_BLOCKED),
397 .name = "UNBLOCKED",
398 .action = gprs_ns2_st_unblocked,
399 .onenter = gprs_ns2_st_unblocked_on_enter,
400 },
401
402 /* ST_ALIVE is only used on VC without RESET/BLOCK */
403 [GPRS_NS2_ST_ALIVE] = {
404 .in_event_mask = S(GPRS_NS2_EV_ALIVE_ACK),
405 .out_state_mask = S(GPRS_NS2_ST_RESET) |
406 S(GPRS_NS2_ST_UNBLOCKED),
407 .name = "ALIVE",
408 .action = gprs_ns2_st_alive,
409 .onenter = gprs_ns2_st_alive_onenter,
410 .onleave = gprs_ns2_st_alive_onleave,
411 },
412};
413
414static int gprs_ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
415{
416 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
417 struct gprs_ns2_vc_priv *priv = fi->priv;
418
419 if (priv->initiater) {
420 /* PCU timeouts */
421 switch (fi->state) {
422 case GPRS_NS2_ST_RESET:
423 priv->N++;
424 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
425 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
426 } else {
427 priv->N = 0;
428 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
429 }
430 break;
431 case GPRS_NS2_ST_BLOCKED:
432 priv->N++;
433 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
434 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
435 } else {
436 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
437 }
438 break;
439 case GPRS_NS2_ST_ALIVE:
440 priv->N++;
441 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
442 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
443 } else {
444 priv->N = 0;
445 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
446 }
447 break;
448 }
449 }
450 return 0;
451}
452
453static void gprs_ns2_recv_unitdata(struct osmo_fsm_inst *fi,
454 struct msgb *msg)
455{
456 struct gprs_ns2_vc_priv *priv = fi->priv;
457 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
458 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
459 struct osmo_gprs_ns2_prim nsp = {};
460 uint16_t bvci;
461
462 if (msgb_l2len(msg) < sizeof(*nsh) + 3)
463 return;
464
465 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
466 * for a PTP BVC may indicate a request to change the IP endpoint
467 * and/or a response to a change in the IP endpoint. */
468
469 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
470 bvci = nsh->data[1] << 8 | nsh->data[2];
471
472 msgb_bssgph(msg) = &nsh->data[3];
473 msgb_bvci(msg) = nsp.bvci = bvci;
474 msgb_nsei(msg) = nsp.nsei = priv->nsvc->nse->nsei;
475
476 if (nsh->data[0])
477 nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE;
478
479 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
480 PRIM_OP_INDICATION, msg);
481 nsi->cb(&nsp.oph, nsi->cb_data);
482}
483
484static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
485 uint32_t event,
486 void *data)
487{
488 struct gprs_ns2_vc_priv *priv = fi->priv;
489 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
490
491 switch (event) {
492 case GPRS_NS2_EV_RESET:
493 if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET)
494 break;
495
496 /* move the FSM into reset */
497 if (fi->state != GPRS_NS2_ST_RESET) {
498 priv->initiater = false;
499 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
500 }
501 /* pass the event down into FSM action */
502 gprs_ns2_st_reset(fi, event, data);
503 break;
504 case GPRS_NS2_EV_ALIVE:
505 switch (fi->state) {
506 case GPRS_NS2_ST_UNCONFIGURED:
507 case GPRS_NS2_ST_RESET:
508 /* ignore ALIVE */
509 break;
510 default:
511 ns2_tx_alive_ack(priv->nsvc);
512 }
513 break;
514 case GPRS_NS2_EV_ALIVE_ACK:
515 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
516 if (fi->state == GPRS_NS2_ST_ALIVE)
517 gprs_ns2_st_alive(fi, event, data);
518 else
519 recv_test_procedure(fi);
520 break;
521 case GPRS_NS2_EV_UNITDATA:
522 switch (fi->state) {
523 case GPRS_NS2_ST_BLOCKED:
524 /* 7.2.1: the BLOCKED_ACK might be lost */
525 if (priv->initiater)
526 gprs_ns2_recv_unitdata(fi, data);
527 else
528 ns2_tx_status(priv->nsvc,
529 NS_CAUSE_NSVC_BLOCKED,
530 0, data);
531 break;
532 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
533 case GPRS_NS2_ST_ALIVE:
534 case GPRS_NS2_ST_UNBLOCKED:
535 gprs_ns2_recv_unitdata(fi, data);
536 break;
537 }
538 break;
539 }
540}
541
542static struct osmo_fsm gprs_ns2_vc_fsm = {
543 .name = "GPRS-NS2-VC",
544 .states = gprs_ns2_vc_states,
545 .num_states = ARRAY_SIZE(gprs_ns2_vc_states),
546 .allstate_event_mask = S(GPRS_NS2_EV_UNITDATA) |
547 S(GPRS_NS2_EV_RESET) |
548 S(GPRS_NS2_EV_ALIVE) |
549 S(GPRS_NS2_EV_ALIVE_ACK),
550 .allstate_action = gprs_ns2_vc_fsm_allstate_action,
551 .cleanup = NULL,
552 .timer_cb = gprs_ns2_vc_fsm_timer_cb,
553 /* .log_subsys = DNS, "is not constant" */
554 .event_names = gprs_ns2_vc_event_names,
555 .pre_term = NULL,
556 .log_subsys = DLNS,
557};
558
559/*!
560 * \brief gprs_ns2_vc_fsm_alloc
561 * \param ctx
562 * \param vc
563 * \param id a char representation of the virtual curcuit
564 * \param initiater initiater is the site which starts the connection. Usually the BSS.
565 * \return NULL on error, otherwise the fsm
566 */
567struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
568 const char *id, bool initiater)
569{
570 struct osmo_fsm_inst *fi;
571 struct gprs_ns2_vc_priv *priv;
572
573 fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
574 if (!fi)
575 return fi;
576
577 nsvc->fi = fi;
578 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
579 priv->nsvc = nsvc;
580 priv->initiater = initiater;
581
582 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
583
584 return fi;
585}
586
Harald Welte5bef2cc2020-09-18 22:33:24 +0200587/*! Start a NS-VC FSM.
588 * \param nsvc the virtual circuit
589 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200590int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
591{
592 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
593 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
594 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_START, NULL);
595 return 0;
596}
597
Harald Welte5bef2cc2020-09-18 22:33:24 +0200598/*! entry point for messages from the driver/VL
599 * \param nsvc virtual circuit on which the message was received
600 * \param msg message that was received
601 * \param tp parsed TLVs of the received message
602 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200603int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
604{
605 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
606 struct osmo_fsm_inst *fi = nsvc->fi;
607 uint8_t cause;
608
609 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
610 * if not answer STATUS with "NS-VC unknown" */
611 /* TODO: handle RESET with different VCI */
612 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
613
614 if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
615 if (nsh->pdu_type != NS_PDUT_STATUS) {
616 return ns2_tx_status(nsvc, cause, 0, msg);
617 }
618 }
619
620 switch (nsh->pdu_type) {
621 case NS_PDUT_RESET:
622 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET, tp);
623 break;
624 case NS_PDUT_RESET_ACK:
625 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET_ACK, tp);
626 break;
627 case NS_PDUT_BLOCK:
628 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK, tp);
629 break;
630 case NS_PDUT_BLOCK_ACK:
631 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK_ACK, tp);
632 break;
633 case NS_PDUT_UNBLOCK:
634 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK, tp);
635 break;
636 case NS_PDUT_UNBLOCK_ACK:
637 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK_ACK, tp);
638 break;
639 case NS_PDUT_ALIVE:
640 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE, tp);
641 break;
642 case NS_PDUT_ALIVE_ACK:
643 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE_ACK, tp);
644 break;
645 case NS_PDUT_UNITDATA:
646 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNITDATA, msg);
647 break;
648 default:
649 LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
650 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
651 return -EINVAL;
652 }
653
654 return 0;
655}
656
Harald Welte5bef2cc2020-09-18 22:33:24 +0200657/*! is the given NS-VC unblocked? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200658int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
659{
660 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
661}
662
663/* initialize osmo_ctx on main tread */
664static __attribute__((constructor)) void on_dso_load_ctx(void)
665{
666 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0);
667}