blob: 4fa53c190aef45388a02b0b20f8057566922de85 [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
Alexander Couzenscce88282020-10-26 00:25:50 +0100462 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
463 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200464 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100465 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200466
467 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
468 * for a PTP BVC may indicate a request to change the IP endpoint
469 * and/or a response to a change in the IP endpoint. */
470
471 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
472 bvci = nsh->data[1] << 8 | nsh->data[2];
473
Alexander Couzens89acdef2020-09-23 18:22:31 +0200474 msg->l3h = &nsh->data[3];
475 nsp.bvci = bvci;
476 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200477
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200478 /* 10.3.9 NS SDU Control Bits */
479 if (nsh->data[0] & 0x1)
Alexander Couzens6a161492020-07-12 13:45:50 +0200480 nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE;
481
482 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
483 PRIM_OP_INDICATION, msg);
484 nsi->cb(&nsp.oph, nsi->cb_data);
485}
486
487static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
488 uint32_t event,
489 void *data)
490{
491 struct gprs_ns2_vc_priv *priv = fi->priv;
492 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100493 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200494
495 switch (event) {
496 case GPRS_NS2_EV_RESET:
497 if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET)
498 break;
499
500 /* move the FSM into reset */
501 if (fi->state != GPRS_NS2_ST_RESET) {
502 priv->initiater = false;
503 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
504 }
505 /* pass the event down into FSM action */
506 gprs_ns2_st_reset(fi, event, data);
507 break;
508 case GPRS_NS2_EV_ALIVE:
509 switch (fi->state) {
510 case GPRS_NS2_ST_UNCONFIGURED:
511 case GPRS_NS2_ST_RESET:
512 /* ignore ALIVE */
513 break;
514 default:
515 ns2_tx_alive_ack(priv->nsvc);
516 }
517 break;
518 case GPRS_NS2_EV_ALIVE_ACK:
519 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
520 if (fi->state == GPRS_NS2_ST_ALIVE)
521 gprs_ns2_st_alive(fi, event, data);
522 else
523 recv_test_procedure(fi);
524 break;
525 case GPRS_NS2_EV_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100526 /* UNITDATA has to handle the release of msg.
527 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
528 * the msg, the upper layer has to do it.
529 * Otherwise the msg must be freed.
530 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200531 switch (fi->state) {
532 case GPRS_NS2_ST_BLOCKED:
533 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzenscce88282020-10-26 00:25:50 +0100534 if (priv->initiater) {
535 gprs_ns2_recv_unitdata(fi, msg);
536 return;
537 }
538
539 ns2_tx_status(priv->nsvc,
540 NS_CAUSE_NSVC_BLOCKED,
541 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200542 break;
543 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
544 case GPRS_NS2_ST_ALIVE:
545 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzenscce88282020-10-26 00:25:50 +0100546 gprs_ns2_recv_unitdata(fi, msg);
547 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200548 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100549
550 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200551 break;
552 }
553}
554
Alexander Couzens0346b642020-10-27 13:05:56 +0100555static void gprs_ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
556 enum osmo_fsm_term_cause cause)
557{
558 struct gprs_ns2_vc_priv *priv = fi->priv;
559
560 osmo_timer_del(&priv->alive.timer);
561}
562
Alexander Couzens6a161492020-07-12 13:45:50 +0200563static struct osmo_fsm gprs_ns2_vc_fsm = {
564 .name = "GPRS-NS2-VC",
565 .states = gprs_ns2_vc_states,
566 .num_states = ARRAY_SIZE(gprs_ns2_vc_states),
567 .allstate_event_mask = S(GPRS_NS2_EV_UNITDATA) |
568 S(GPRS_NS2_EV_RESET) |
569 S(GPRS_NS2_EV_ALIVE) |
570 S(GPRS_NS2_EV_ALIVE_ACK),
571 .allstate_action = gprs_ns2_vc_fsm_allstate_action,
Alexander Couzens0346b642020-10-27 13:05:56 +0100572 .cleanup = gprs_ns2_vc_fsm_clean,
Alexander Couzens6a161492020-07-12 13:45:50 +0200573 .timer_cb = gprs_ns2_vc_fsm_timer_cb,
574 /* .log_subsys = DNS, "is not constant" */
575 .event_names = gprs_ns2_vc_event_names,
576 .pre_term = NULL,
577 .log_subsys = DLNS,
578};
579
580/*!
581 * \brief gprs_ns2_vc_fsm_alloc
582 * \param ctx
583 * \param vc
584 * \param id a char representation of the virtual curcuit
585 * \param initiater initiater is the site which starts the connection. Usually the BSS.
586 * \return NULL on error, otherwise the fsm
587 */
588struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
589 const char *id, bool initiater)
590{
591 struct osmo_fsm_inst *fi;
592 struct gprs_ns2_vc_priv *priv;
593
594 fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
595 if (!fi)
596 return fi;
597
598 nsvc->fi = fi;
599 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
600 priv->nsvc = nsvc;
601 priv->initiater = initiater;
602
603 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
604
605 return fi;
606}
607
Harald Welte5bef2cc2020-09-18 22:33:24 +0200608/*! Start a NS-VC FSM.
609 * \param nsvc the virtual circuit
610 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200611int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
612{
613 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
614 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
615 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_START, NULL);
616 return 0;
617}
618
Harald Welte5bef2cc2020-09-18 22:33:24 +0200619/*! entry point for messages from the driver/VL
620 * \param nsvc virtual circuit on which the message was received
621 * \param msg message that was received
622 * \param tp parsed TLVs of the received message
623 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200624int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
625{
626 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
627 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100628 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200629 uint8_t cause;
630
631 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
632 * if not answer STATUS with "NS-VC unknown" */
633 /* TODO: handle RESET with different VCI */
634 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
635
636 if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
637 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100638 rc = ns2_tx_status(nsvc, cause, 0, msg);
639 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200640 }
641 }
642
643 switch (nsh->pdu_type) {
644 case NS_PDUT_RESET:
645 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET, tp);
646 break;
647 case NS_PDUT_RESET_ACK:
648 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET_ACK, tp);
649 break;
650 case NS_PDUT_BLOCK:
651 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK, tp);
652 break;
653 case NS_PDUT_BLOCK_ACK:
654 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK_ACK, tp);
655 break;
656 case NS_PDUT_UNBLOCK:
657 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK, tp);
658 break;
659 case NS_PDUT_UNBLOCK_ACK:
660 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK_ACK, tp);
661 break;
662 case NS_PDUT_ALIVE:
663 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE, tp);
664 break;
665 case NS_PDUT_ALIVE_ACK:
666 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE_ACK, tp);
667 break;
668 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100669 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzens6a161492020-07-12 13:45:50 +0200670 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100671 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200672 default:
673 LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
674 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
675 return -EINVAL;
676 }
677
Alexander Couzenscce88282020-10-26 00:25:50 +0100678out:
679 msgb_free(msg);
680
681 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200682}
683
Harald Welte5bef2cc2020-09-18 22:33:24 +0200684/*! is the given NS-VC unblocked? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200685int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
686{
687 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
688}
689
690/* initialize osmo_ctx on main tread */
691static __attribute__((constructor)) void on_dso_load_ctx(void)
692{
693 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0);
694}