blob: 0d30f1296852867b8001ec6a041c9e92083c5bde [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;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100321 struct gprs_ns2_vc *nsvc = priv->nsvc;
322 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200323
Daniel Willmann15c09a82020-11-03 23:05:43 +0100324 ns2_nse_notify_unblocked(nsvc, true);
325 ns2_prim_status_ind(nse, nsvc, 0, NS_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200326}
327
328static void gprs_ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
329{
330 struct gprs_ns2_vc_priv *priv = fi->priv;
331
332 switch (event) {
333 case GPRS_NS2_EV_BLOCK:
334 priv->initiater = false;
335 ns2_tx_block_ack(priv->nsvc);
336 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
337 0, 2);
338 break;
339 }
340}
341
342static void gprs_ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
343{
344 switch (event) {
345 case GPRS_NS2_EV_ALIVE_ACK:
346 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
347 break;
348 }
349}
350
351static void gprs_ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
352{
353 struct gprs_ns2_vc_priv *priv = fi->priv;
354 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
355
356 priv->alive.mode = NS_TOUT_TNS_TEST;
357 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
358
359 if (old_state != GPRS_NS2_ST_ALIVE)
360 priv->N = 0;
361
362 ns2_tx_alive(priv->nsvc);
363 ns2_nse_notify_unblocked(priv->nsvc, false);
364}
365
366static void gprs_ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
367{
368 start_test_procedure(fi->priv);
369}
370
371static const struct osmo_fsm_state gprs_ns2_vc_states[] = {
372 [GPRS_NS2_ST_UNCONFIGURED] = {
373 .in_event_mask = S(GPRS_NS2_EV_START),
374 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE),
375 .name = "UNCONFIGURED",
376 .action = gprs_ns2_st_unconfigured,
377 },
378 [GPRS_NS2_ST_RESET] = {
379 .in_event_mask = S(GPRS_NS2_EV_RESET_ACK) | S(GPRS_NS2_EV_RESET),
380 .out_state_mask = S(GPRS_NS2_ST_RESET) |
381 S(GPRS_NS2_ST_BLOCKED),
382 .name = "RESET",
383 .action = gprs_ns2_st_reset,
384 .onenter = gprs_ns2_st_reset_onenter,
385 },
386 [GPRS_NS2_ST_BLOCKED] = {
387 .in_event_mask = S(GPRS_NS2_EV_BLOCK) | S(GPRS_NS2_EV_BLOCK_ACK) |
388 S(GPRS_NS2_EV_UNBLOCK) | S(GPRS_NS2_EV_UNBLOCK_ACK),
389 .out_state_mask = S(GPRS_NS2_ST_RESET) |
390 S(GPRS_NS2_ST_UNBLOCKED) |
391 S(GPRS_NS2_ST_BLOCKED),
392 .name = "BLOCKED",
393 .action = gprs_ns2_st_blocked,
394 .onenter = gprs_ns2_st_blocked_onenter,
395 },
396 [GPRS_NS2_ST_UNBLOCKED] = {
397 .in_event_mask = S(GPRS_NS2_EV_BLOCK),
398 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE) |
399 S(GPRS_NS2_ST_BLOCKED),
400 .name = "UNBLOCKED",
401 .action = gprs_ns2_st_unblocked,
402 .onenter = gprs_ns2_st_unblocked_on_enter,
403 },
404
405 /* ST_ALIVE is only used on VC without RESET/BLOCK */
406 [GPRS_NS2_ST_ALIVE] = {
407 .in_event_mask = S(GPRS_NS2_EV_ALIVE_ACK),
408 .out_state_mask = S(GPRS_NS2_ST_RESET) |
409 S(GPRS_NS2_ST_UNBLOCKED),
410 .name = "ALIVE",
411 .action = gprs_ns2_st_alive,
412 .onenter = gprs_ns2_st_alive_onenter,
413 .onleave = gprs_ns2_st_alive_onleave,
414 },
415};
416
417static int gprs_ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
418{
419 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
420 struct gprs_ns2_vc_priv *priv = fi->priv;
421
422 if (priv->initiater) {
423 /* PCU timeouts */
424 switch (fi->state) {
425 case GPRS_NS2_ST_RESET:
426 priv->N++;
427 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
428 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
429 } else {
430 priv->N = 0;
431 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
432 }
433 break;
434 case GPRS_NS2_ST_BLOCKED:
435 priv->N++;
436 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
437 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
438 } else {
439 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
440 }
441 break;
442 case GPRS_NS2_ST_ALIVE:
443 priv->N++;
444 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
445 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
446 } else {
447 priv->N = 0;
448 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
449 }
450 break;
451 }
452 }
453 return 0;
454}
455
456static void gprs_ns2_recv_unitdata(struct osmo_fsm_inst *fi,
457 struct msgb *msg)
458{
459 struct gprs_ns2_vc_priv *priv = fi->priv;
460 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
461 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
462 struct osmo_gprs_ns2_prim nsp = {};
463 uint16_t bvci;
464
Alexander Couzenscce88282020-10-26 00:25:50 +0100465 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
466 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100468 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200469
470 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
471 * for a PTP BVC may indicate a request to change the IP endpoint
472 * and/or a response to a change in the IP endpoint. */
473
474 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
475 bvci = nsh->data[1] << 8 | nsh->data[2];
476
Alexander Couzens89acdef2020-09-23 18:22:31 +0200477 msg->l3h = &nsh->data[3];
478 nsp.bvci = bvci;
479 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200480
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200481 /* 10.3.9 NS SDU Control Bits */
482 if (nsh->data[0] & 0x1)
Alexander Couzens6a161492020-07-12 13:45:50 +0200483 nsp.u.unitdata.change = NS_ENDPOINT_REQUEST_CHANGE;
484
485 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
486 PRIM_OP_INDICATION, msg);
487 nsi->cb(&nsp.oph, nsi->cb_data);
488}
489
490static void gprs_ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
491 uint32_t event,
492 void *data)
493{
494 struct gprs_ns2_vc_priv *priv = fi->priv;
495 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100496 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200497
498 switch (event) {
499 case GPRS_NS2_EV_RESET:
500 if (priv->nsvc->mode != NS2_VC_MODE_BLOCKRESET)
501 break;
502
503 /* move the FSM into reset */
504 if (fi->state != GPRS_NS2_ST_RESET) {
505 priv->initiater = false;
506 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
507 }
508 /* pass the event down into FSM action */
509 gprs_ns2_st_reset(fi, event, data);
510 break;
511 case GPRS_NS2_EV_ALIVE:
512 switch (fi->state) {
513 case GPRS_NS2_ST_UNCONFIGURED:
514 case GPRS_NS2_ST_RESET:
515 /* ignore ALIVE */
516 break;
517 default:
518 ns2_tx_alive_ack(priv->nsvc);
519 }
520 break;
521 case GPRS_NS2_EV_ALIVE_ACK:
522 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
523 if (fi->state == GPRS_NS2_ST_ALIVE)
524 gprs_ns2_st_alive(fi, event, data);
525 else
526 recv_test_procedure(fi);
527 break;
528 case GPRS_NS2_EV_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100529 /* UNITDATA has to handle the release of msg.
530 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
531 * the msg, the upper layer has to do it.
532 * Otherwise the msg must be freed.
533 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200534 switch (fi->state) {
535 case GPRS_NS2_ST_BLOCKED:
536 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzenscce88282020-10-26 00:25:50 +0100537 if (priv->initiater) {
538 gprs_ns2_recv_unitdata(fi, msg);
539 return;
540 }
541
542 ns2_tx_status(priv->nsvc,
543 NS_CAUSE_NSVC_BLOCKED,
544 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200545 break;
546 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
547 case GPRS_NS2_ST_ALIVE:
548 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzenscce88282020-10-26 00:25:50 +0100549 gprs_ns2_recv_unitdata(fi, msg);
550 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200551 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100552
553 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200554 break;
555 }
556}
557
Alexander Couzens0346b642020-10-27 13:05:56 +0100558static void gprs_ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
559 enum osmo_fsm_term_cause cause)
560{
561 struct gprs_ns2_vc_priv *priv = fi->priv;
562
563 osmo_timer_del(&priv->alive.timer);
564}
565
Alexander Couzens6a161492020-07-12 13:45:50 +0200566static struct osmo_fsm gprs_ns2_vc_fsm = {
567 .name = "GPRS-NS2-VC",
568 .states = gprs_ns2_vc_states,
569 .num_states = ARRAY_SIZE(gprs_ns2_vc_states),
570 .allstate_event_mask = S(GPRS_NS2_EV_UNITDATA) |
571 S(GPRS_NS2_EV_RESET) |
572 S(GPRS_NS2_EV_ALIVE) |
573 S(GPRS_NS2_EV_ALIVE_ACK),
574 .allstate_action = gprs_ns2_vc_fsm_allstate_action,
Alexander Couzens0346b642020-10-27 13:05:56 +0100575 .cleanup = gprs_ns2_vc_fsm_clean,
Alexander Couzens6a161492020-07-12 13:45:50 +0200576 .timer_cb = gprs_ns2_vc_fsm_timer_cb,
577 /* .log_subsys = DNS, "is not constant" */
578 .event_names = gprs_ns2_vc_event_names,
579 .pre_term = NULL,
580 .log_subsys = DLNS,
581};
582
583/*!
584 * \brief gprs_ns2_vc_fsm_alloc
585 * \param ctx
586 * \param vc
587 * \param id a char representation of the virtual curcuit
588 * \param initiater initiater is the site which starts the connection. Usually the BSS.
589 * \return NULL on error, otherwise the fsm
590 */
591struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
592 const char *id, bool initiater)
593{
594 struct osmo_fsm_inst *fi;
595 struct gprs_ns2_vc_priv *priv;
596
597 fi = osmo_fsm_inst_alloc(&gprs_ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
598 if (!fi)
599 return fi;
600
601 nsvc->fi = fi;
602 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
603 priv->nsvc = nsvc;
604 priv->initiater = initiater;
605
606 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
607
608 return fi;
609}
610
Harald Welte5bef2cc2020-09-18 22:33:24 +0200611/*! Start a NS-VC FSM.
612 * \param nsvc the virtual circuit
613 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200614int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
615{
616 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
617 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
618 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_START, NULL);
619 return 0;
620}
621
Harald Welte5bef2cc2020-09-18 22:33:24 +0200622/*! entry point for messages from the driver/VL
623 * \param nsvc virtual circuit on which the message was received
624 * \param msg message that was received
625 * \param tp parsed TLVs of the received message
626 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200627int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
628{
629 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
630 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100631 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200632 uint8_t cause;
633
634 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
635 * if not answer STATUS with "NS-VC unknown" */
636 /* TODO: handle RESET with different VCI */
637 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
638
639 if (gprs_ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
640 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100641 rc = ns2_tx_status(nsvc, cause, 0, msg);
642 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200643 }
644 }
645
646 switch (nsh->pdu_type) {
647 case NS_PDUT_RESET:
648 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET, tp);
649 break;
650 case NS_PDUT_RESET_ACK:
651 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RESET_ACK, tp);
652 break;
653 case NS_PDUT_BLOCK:
654 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK, tp);
655 break;
656 case NS_PDUT_BLOCK_ACK:
657 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_BLOCK_ACK, tp);
658 break;
659 case NS_PDUT_UNBLOCK:
660 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK, tp);
661 break;
662 case NS_PDUT_UNBLOCK_ACK:
663 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNBLOCK_ACK, tp);
664 break;
665 case NS_PDUT_ALIVE:
666 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE, tp);
667 break;
668 case NS_PDUT_ALIVE_ACK:
669 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_ALIVE_ACK, tp);
670 break;
671 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100672 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzens6a161492020-07-12 13:45:50 +0200673 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100674 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200675 default:
676 LOGP(DLNS, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
677 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
678 return -EINVAL;
679 }
680
Alexander Couzenscce88282020-10-26 00:25:50 +0100681out:
682 msgb_free(msg);
683
684 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200685}
686
Harald Welte5bef2cc2020-09-18 22:33:24 +0200687/*! is the given NS-VC unblocked? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200688int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
689{
690 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
691}
692
693/* initialize osmo_ctx on main tread */
694static __attribute__((constructor)) void on_dso_load_ctx(void)
695{
696 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_vc_fsm) == 0);
697}