blob: 451437f21d579af2964eac33693e570debec438a [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
Alexander Couzens89acdef2020-09-23 18:22:31 +0200472 msg->l3h = &nsh->data[3];
473 nsp.bvci = bvci;
474 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200475
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200476 /* 10.3.9 NS SDU Control Bits */
477 if (nsh->data[0] & 0x1)
Alexander Couzens6a161492020-07-12 13:45:50 +0200478 nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE;
479
480 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
481 PRIM_OP_INDICATION, msg);
482 nsi->cb(&nsp.oph, nsi->cb_data);
483}
484
485static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
486 uint32_t event,
487 void *data)
488{
489 struct gprs_ns2_vc_priv *priv = fi->priv;
490 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
491
492 switch (event) {
493 case GPRS_NS2_EV_RESET:
494 if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET)
495 break;
496
497 /* move the FSM into reset */
498 if (fi->state != GPRS_NS2_ST_RESET) {
499 priv->initiater = false;
500 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
501 }
502 /* pass the event down into FSM action */
503 gprs_ns2_st_reset(fi, event, data);
504 break;
505 case GPRS_NS2_EV_ALIVE:
506 switch (fi->state) {
507 case GPRS_NS2_ST_UNCONFIGURED:
508 case GPRS_NS2_ST_RESET:
509 /* ignore ALIVE */
510 break;
511 default:
512 ns2_tx_alive_ack(priv->nsvc);
513 }
514 break;
515 case GPRS_NS2_EV_ALIVE_ACK:
516 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
517 if (fi->state == GPRS_NS2_ST_ALIVE)
518 gprs_ns2_st_alive(fi, event, data);
519 else
520 recv_test_procedure(fi);
521 break;
522 case GPRS_NS2_EV_UNITDATA:
523 switch (fi->state) {
524 case GPRS_NS2_ST_BLOCKED:
525 /* 7.2.1: the BLOCKED_ACK might be lost */
526 if (priv->initiater)
527 gprs_ns2_recv_unitdata(fi, data);
528 else
529 ns2_tx_status(priv->nsvc,
530 NS_CAUSE_NSVC_BLOCKED,
531 0, data);
532 break;
533 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
534 case GPRS_NS2_ST_ALIVE:
535 case GPRS_NS2_ST_UNBLOCKED:
536 gprs_ns2_recv_unitdata(fi, data);
537 break;
538 }
539 break;
540 }
541}
542
Alexander Couzens0346b642020-10-27 13:05:56 +0100543static void gprs_ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
544 enum osmo_fsm_term_cause cause)
545{
546 struct gprs_ns2_vc_priv *priv = fi->priv;
547
548 osmo_timer_del(&priv->alive.timer);
549}
550
Alexander Couzens6a161492020-07-12 13:45:50 +0200551static struct osmo_fsm gprs_ns2_vc_fsm = {
552 .name = "GPRS-NS2-VC",
553 .states = gprs_ns2_vc_states,
554 .num_states = ARRAY_SIZE(gprs_ns2_vc_states),
555 .allstate_event_mask = S(GPRS_NS2_EV_UNITDATA) |
556 S(GPRS_NS2_EV_RESET) |
557 S(GPRS_NS2_EV_ALIVE) |
558 S(GPRS_NS2_EV_ALIVE_ACK),
559 .allstate_action = gprs_ns2_vc_fsm_allstate_action,
Alexander Couzens0346b642020-10-27 13:05:56 +0100560 .cleanup = gprs_ns2_vc_fsm_clean,
Alexander Couzens6a161492020-07-12 13:45:50 +0200561 .timer_cb = gprs_ns2_vc_fsm_timer_cb,
562 /* .log_subsys = DNS, "is not constant" */
563 .event_names = gprs_ns2_vc_event_names,
564 .pre_term = NULL,
565 .log_subsys = DLNS,
566};
567
568/*!
569 * \brief gprs_ns2_vc_fsm_alloc
570 * \param ctx
571 * \param vc
572 * \param id a char representation of the virtual curcuit
573 * \param initiater initiater is the site which starts the connection. Usually the BSS.
574 * \return NULL on error, otherwise the fsm
575 */
576struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
577 const char *id, bool initiater)
578{
579 struct osmo_fsm_inst *fi;
580 struct gprs_ns2_vc_priv *priv;
581
582 fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
583 if (!fi)
584 return fi;
585
586 nsvc->fi = fi;
587 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
588 priv->nsvc = nsvc;
589 priv->initiater = initiater;
590
591 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
592
593 return fi;
594}
595
Harald Welte5bef2cc2020-09-18 22:33:24 +0200596/*! Start a NS-VC FSM.
597 * \param nsvc the virtual circuit
598 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200599int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
600{
601 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
602 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
603 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_START, NULL);
604 return 0;
605}
606
Harald Welte5bef2cc2020-09-18 22:33:24 +0200607/*! entry point for messages from the driver/VL
608 * \param nsvc virtual circuit on which the message was received
609 * \param msg message that was received
610 * \param tp parsed TLVs of the received message
611 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200612int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
613{
614 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
615 struct osmo_fsm_inst *fi = nsvc->fi;
616 uint8_t cause;
617
618 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
619 * if not answer STATUS with "NS-VC unknown" */
620 /* TODO: handle RESET with different VCI */
621 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
622
623 if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
624 if (nsh->pdu_type != NS_PDUT_STATUS) {
625 return ns2_tx_status(nsvc, cause, 0, msg);
626 }
627 }
628
629 switch (nsh->pdu_type) {
630 case NS_PDUT_RESET:
631 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET, tp);
632 break;
633 case NS_PDUT_RESET_ACK:
634 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET_ACK, tp);
635 break;
636 case NS_PDUT_BLOCK:
637 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK, tp);
638 break;
639 case NS_PDUT_BLOCK_ACK:
640 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK_ACK, tp);
641 break;
642 case NS_PDUT_UNBLOCK:
643 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK, tp);
644 break;
645 case NS_PDUT_UNBLOCK_ACK:
646 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK_ACK, tp);
647 break;
648 case NS_PDUT_ALIVE:
649 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE, tp);
650 break;
651 case NS_PDUT_ALIVE_ACK:
652 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE_ACK, tp);
653 break;
654 case NS_PDUT_UNITDATA:
655 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNITDATA, msg);
656 break;
657 default:
658 LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
659 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
660 return -EINVAL;
661 }
662
663 return 0;
664}
665
Harald Welte5bef2cc2020-09-18 22:33:24 +0200666/*! is the given NS-VC unblocked? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200667int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
668{
669 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
670}
671
672/* initialize osmo_ctx on main tread */
673static __attribute__((constructor)) void on_dso_load_ctx(void)
674{
675 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0);
676}