blob: 88ce8aeb281f5b9d0d309e690d9061acfa155f9a [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.*/
Alexander Couzensea01bf22021-01-18 14:01:01 +010060 bool initiator;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010061 bool initiate_block;
62 bool initiate_reset;
Alexander Couzens47afc422021-01-17 20:12:46 +010063 /* if blocked by O&M/vty */
64 bool om_blocked;
65 /* if unitdata is forwarded to the user */
66 bool accept_unitdata;
Alexander Couzens6a161492020-07-12 13:45:50 +020067
68 /* the alive counter is present in all states */
69 struct {
70 struct osmo_timer_list timer;
71 enum ns2_timeout mode;
72 int N;
Alexander Couzensab0e8642021-02-12 02:50:44 +010073 struct timespec timer_started;
Alexander Couzens6a161492020-07-12 13:45:50 +020074 } alive;
75};
76
77
78/* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure..
79 *
80 * With RESET/BLOCK, the state should follow:
81 * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED
82 *
83 * Without RESET/BLOCK, the state should follow:
Alexander Couzensd3e31102021-02-03 11:15:07 +010084 * - UNCONFIGURED -> RECOVERY -> UNBLOCKED
Alexander Couzens6a161492020-07-12 13:45:50 +020085 *
86 * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive.
87 * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU.
88 * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED.
89 *
Alexander Couzensd3e31102021-02-03 11:15:07 +010090 * The RECOVERY state is used as intermediate, because a VC is only valid if it received an Alive ACK when
Alexander Couzens6a161492020-07-12 13:45:50 +020091 * not using RESET/BLOCK procedure.
92 */
93
94enum gprs_ns2_vc_state {
95 GPRS_NS2_ST_UNCONFIGURED,
96 GPRS_NS2_ST_RESET,
97 GPRS_NS2_ST_BLOCKED,
98 GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */
99
Alexander Couzensd3e31102021-02-03 11:15:07 +0100100 GPRS_NS2_ST_RECOVERING, /* only used when not using RESET/BLOCK procedure */
Alexander Couzens6a161492020-07-12 13:45:50 +0200101};
102
103enum gprs_ns2_vc_event {
Alexander Couzensf5775432021-01-18 13:49:00 +0100104 GPRS_NS2_EV_REQ_START,
Alexander Couzens6a161492020-07-12 13:45:50 +0200105
106 /* received messages */
Alexander Couzensf5775432021-01-18 13:49:00 +0100107 GPRS_NS2_EV_RX_RESET,
108 GPRS_NS2_EV_RX_RESET_ACK,
109 GPRS_NS2_EV_RX_UNBLOCK,
110 GPRS_NS2_EV_RX_UNBLOCK_ACK,
111 GPRS_NS2_EV_RX_BLOCK,
112 GPRS_NS2_EV_RX_BLOCK_ACK,
113 GPRS_NS2_EV_RX_ALIVE,
114 GPRS_NS2_EV_RX_ALIVE_ACK,
115 GPRS_NS2_EV_RX_STATUS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200116
Alexander Couzensf5775432021-01-18 13:49:00 +0100117 GPRS_NS2_EV_RX_UNITDATA,
Daniel Willmanned0c9822020-11-18 14:08:07 +0100118
Alexander Couzensf5775432021-01-18 13:49:00 +0100119 GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */
120 GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */
121 GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/
Alexander Couzens6a161492020-07-12 13:45:50 +0200122};
123
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100124static const struct value_string ns2_vc_event_names[] = {
Harald Welteb339cbb2021-02-05 14:49:21 +0100125 { GPRS_NS2_EV_REQ_START, "REQ-START" },
126 { GPRS_NS2_EV_RX_RESET, "RX-RESET" },
127 { GPRS_NS2_EV_RX_RESET_ACK, "RX-RESET_ACK" },
128 { GPRS_NS2_EV_RX_UNBLOCK, "RX-UNBLOCK" },
129 { GPRS_NS2_EV_RX_UNBLOCK_ACK, "RX-UNBLOCK_ACK" },
130 { GPRS_NS2_EV_RX_BLOCK, "RX-BLOCK" },
131 { GPRS_NS2_EV_RX_BLOCK_ACK, "RX-BLOCK_ACK" },
132 { GPRS_NS2_EV_RX_ALIVE, "RX-ALIVE" },
133 { GPRS_NS2_EV_RX_ALIVE_ACK, "RX-ALIVE_ACK" },
134 { GPRS_NS2_EV_RX_STATUS, "RX-STATUS" },
135 { GPRS_NS2_EV_RX_UNITDATA, "RX-UNITDATA" },
136 { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "REQ-FORCE_UNCONFIGURED" },
Alexander Couzens47afc422021-01-17 20:12:46 +0100137 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
138 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200139 { 0, NULL }
140};
141
142static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
143{
144 struct gprs_ns2_vc_priv *priv = fi->priv;
145 return priv->nsvc->nse->nsi;
146}
147
148static void start_test_procedure(struct gprs_ns2_vc_priv *priv)
149{
150 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
151
152 if (osmo_timer_pending(&priv->alive.timer))
153 return;
154
155 priv->alive.mode = NS_TOUT_TNS_ALIVE;
156 priv->alive.N = 0;
157
Alexander Couzensab0e8642021-02-12 02:50:44 +0100158 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200159 ns2_tx_alive(priv->nsvc);
160 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
161}
162
163static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
164{
165 osmo_timer_del(&priv->alive.timer);
166}
167
168static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
169{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100170 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200171
Alexander Couzensab0e8642021-02-12 02:50:44 +0100172 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
173 return 0;
174
175 timespecsub(&now, &priv->alive.timer_started, &elapsed);
176 LOGNSVC(priv->nsvc, LOGL_ERROR, "elapsed: %ld, now: %ld, saved: %ld.\n",
177 elapsed.tv_sec, now.tv_sec, priv->alive.timer_started.tv_sec);
178 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200179}
180
181static void recv_test_procedure(struct osmo_fsm_inst *fi)
182{
183 struct gprs_ns2_vc_priv *priv = fi->priv;
184 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
185 struct gprs_ns2_vc *nsvc = priv->nsvc;
186
187 /* ignoring ACKs without sending an ALIVE */
188 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
189 return;
190
191 priv->alive.mode = NS_TOUT_TNS_TEST;
192 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
193 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
194 alive_timer_elapsed_ms(priv));
195}
196
197
198static void alive_timeout_handler(void *data)
199{
200 struct osmo_fsm_inst *fi = data;
201 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
202 struct gprs_ns2_vc_priv *priv = fi->priv;
203
204 switch (priv->alive.mode) {
205 case NS_TOUT_TNS_TEST:
206 priv->alive.mode = NS_TOUT_TNS_ALIVE;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100207 priv->alive.N = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200208 ns2_tx_alive(priv->nsvc);
209 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
210 break;
211 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100212 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200213 priv->alive.N++;
214
215 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
216 /* retransmission */
217 ns2_tx_alive(priv->nsvc);
218 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
219 } else {
220 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100221 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200222 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
223 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100224 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200225 }
226 }
227 break;
228 default:
229 break;
230 }
231}
232
Harald Welte6a9ec422021-01-31 18:56:29 +0100233
234static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
235{
236 stop_test_procedure(fi->priv);
237}
238
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100239static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200240{
241 struct gprs_ns2_vc_priv *priv = fi->priv;
242 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
243
Alexander Couzensea01bf22021-01-18 14:01:01 +0100244 priv->initiate_reset = priv->initiate_block = priv->initiator;
245 priv->om_blocked = false;
246
Alexander Couzens6a161492020-07-12 13:45:50 +0200247 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100248 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200249 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100250 case GPRS_NS2_VC_MODE_ALIVE:
Alexander Couzensd3e31102021-02-03 11:15:07 +0100251 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200252 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100253 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200254 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
255 break;
256 }
257
258 break;
259 default:
260 OSMO_ASSERT(0);
261 }
262}
263
264
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100265static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200266{
267 struct gprs_ns2_vc_priv *priv = fi->priv;
268
269 if (old_state != GPRS_NS2_ST_RESET)
270 priv->N = 0;
271
Alexander Couzens47afc422021-01-17 20:12:46 +0100272 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100273 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200274 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
275
276 stop_test_procedure(priv);
277 ns2_nse_notify_unblocked(priv->nsvc, false);
278}
279
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100280static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200281{
282 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
283 struct gprs_ns2_vc_priv *priv = fi->priv;
284
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100285 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100287 case GPRS_NS2_EV_RX_RESET:
288 ns2_tx_reset_ack(priv->nsvc);
289 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100290 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200291 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
292 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
293 break;
294 }
295 } else {
296 /* we are on the receiving end */
297 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100298 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200299 ns2_tx_reset_ack(priv->nsvc);
300 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
301 0, 0);
302 break;
303 }
304 }
305}
306
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100307static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200308{
309 struct gprs_ns2_vc_priv *priv = fi->priv;
310
Harald Welte4c54f592021-01-30 22:42:15 +0100311 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200312 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100313 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
314 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200315
Alexander Couzens47afc422021-01-17 20:12:46 +0100316 if (priv->om_blocked) {
317 /* we are already blocked after a RESET */
318 if (old_state == GPRS_NS2_ST_RESET) {
319 osmo_timer_del(&fi->timer);
320 } else {
321 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
322 }
323 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200324 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100325 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200326
327 start_test_procedure(priv);
328}
329
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100330static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200331{
332 struct gprs_ns2_vc_priv *priv = fi->priv;
333
Alexander Couzens47afc422021-01-17 20:12:46 +0100334 if (priv->om_blocked) {
335 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100336 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100337 priv->accept_unitdata = false;
338 osmo_timer_del(&fi->timer);
339 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100340 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100341 priv->accept_unitdata = false;
342 ns2_tx_block_ack(priv->nsvc);
343 osmo_timer_del(&fi->timer);
344 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100345 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100346 priv->accept_unitdata = false;
347 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
348 osmo_timer_add(&fi->timer);
349 break;
350 }
351 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200352 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100353 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200354 /* TODO: BLOCK is a UNBLOCK_NACK */
355 ns2_tx_block_ack(priv->nsvc);
356 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100357 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 ns2_tx_unblock_ack(priv->nsvc);
359 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100360 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100361 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200362 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
363 0, NS_TOUT_TNS_TEST);
364 break;
365 }
366 } else {
367 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
368 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100369 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100370 ns2_tx_block_ack(priv->nsvc);
371 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100372 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200373 ns2_tx_unblock_ack(priv->nsvc);
374 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
375 0, 0);
376 break;
377 }
378 }
379}
380
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100381static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200382{
383 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100384 struct gprs_ns2_vc *nsvc = priv->nsvc;
385 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200386
Harald Welteb40bf8b2021-01-30 22:43:01 +0100387 if (old_state != GPRS_NS2_ST_UNBLOCKED)
388 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
389
Alexander Couzens47afc422021-01-17 20:12:46 +0100390 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100391 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100392 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200393}
394
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100395static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200396{
397 struct gprs_ns2_vc_priv *priv = fi->priv;
398
399 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100400 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100401 ns2_tx_unblock_ack(priv->nsvc);
402 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100403 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100404 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200405 ns2_tx_block_ack(priv->nsvc);
406 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
407 0, 2);
408 break;
409 }
410}
411
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100412static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200413{
414 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100415 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200416 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
417 break;
418 }
419}
420
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100421static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200422{
423 struct gprs_ns2_vc_priv *priv = fi->priv;
424 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
425
426 priv->alive.mode = NS_TOUT_TNS_TEST;
427 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
428
Alexander Couzensd3e31102021-02-03 11:15:07 +0100429 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200430 priv->N = 0;
431
432 ns2_tx_alive(priv->nsvc);
433 ns2_nse_notify_unblocked(priv->nsvc, false);
434}
435
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100436static void ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200437{
438 start_test_procedure(fi->priv);
439}
440
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100441static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200442 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100443 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100444 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING),
Alexander Couzens6a161492020-07-12 13:45:50 +0200445 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100446 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100447 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200448 },
449 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100450 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200451 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100452 S(GPRS_NS2_ST_BLOCKED) |
453 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200454 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100455 .action = ns2_st_reset,
456 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 },
458 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100459 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
460 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200461 .out_state_mask = S(GPRS_NS2_ST_RESET) |
462 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100463 S(GPRS_NS2_ST_BLOCKED) |
464 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200465 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100466 .action = ns2_st_blocked,
467 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200468 },
469 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100470 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
471 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100472 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100473 S(GPRS_NS2_ST_BLOCKED) |
474 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200475 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100476 .action = ns2_st_unblocked,
477 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200478 },
479
Alexander Couzensd3e31102021-02-03 11:15:07 +0100480 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
481 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100482 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100483 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100484 S(GPRS_NS2_ST_UNBLOCKED) |
485 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100486 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100487 .action = ns2_st_alive,
488 .onenter = ns2_st_alive_onenter,
489 .onleave = ns2_st_alive_onleave,
Alexander Couzens6a161492020-07-12 13:45:50 +0200490 },
491};
492
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100493static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200494{
495 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
496 struct gprs_ns2_vc_priv *priv = fi->priv;
497
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100498 switch (fi->state) {
499 case GPRS_NS2_ST_RESET:
500 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100501 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200502 priv->N++;
503 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
504 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
505 } else {
506 priv->N = 0;
507 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
508 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100509 }
510 break;
511 case GPRS_NS2_ST_BLOCKED:
512 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200513 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100514 if (priv->om_blocked) {
515 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
516 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
517 } else {
518 /* 7.2 stop accepting data when BLOCK PDU not responded */
519 priv->accept_unitdata = false;
520 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200521 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100522 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
523 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
524 } else {
525 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
526 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100528 }
529 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100530 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100531 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200532 priv->N++;
533 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100534 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200535 } else {
536 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100537 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200538 }
539 break;
540 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100541 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200542 }
543 return 0;
544}
545
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100546static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200547 struct msgb *msg)
548{
549 struct gprs_ns2_vc_priv *priv = fi->priv;
550 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
551 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
552 struct osmo_gprs_ns2_prim nsp = {};
553 uint16_t bvci;
554
Alexander Couzenscce88282020-10-26 00:25:50 +0100555 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
556 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200557 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100558 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200559
560 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
561 * for a PTP BVC may indicate a request to change the IP endpoint
562 * and/or a response to a change in the IP endpoint. */
563
564 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
565 bvci = nsh->data[1] << 8 | nsh->data[2];
566
Alexander Couzens89acdef2020-09-23 18:22:31 +0200567 msg->l3h = &nsh->data[3];
568 nsp.bvci = bvci;
569 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200570
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200571 /* 10.3.9 NS SDU Control Bits */
572 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100573 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200574
Alexander Couzens138b96f2021-01-25 16:23:29 +0100575 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200576 PRIM_OP_INDICATION, msg);
577 nsi->cb(&nsp.oph, nsi->cb_data);
578}
579
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100580static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200581 uint32_t event,
582 void *data)
583{
584 struct gprs_ns2_vc_priv *priv = fi->priv;
585 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100586 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200587
588 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100589 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100590 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200591 break;
592
593 /* move the FSM into reset */
594 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100595 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200596 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
597 }
598 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100599 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200600 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100601 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200602 switch (fi->state) {
603 case GPRS_NS2_ST_UNCONFIGURED:
604 case GPRS_NS2_ST_RESET:
605 /* ignore ALIVE */
606 break;
607 default:
608 ns2_tx_alive_ack(priv->nsvc);
609 }
610 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100611 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200612 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100613 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100614 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200615 else
616 recv_test_procedure(fi);
617 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100618 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100619 /* UNITDATA has to handle the release of msg.
620 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
621 * the msg, the upper layer has to do it.
622 * Otherwise the msg must be freed.
623 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200624 switch (fi->state) {
625 case GPRS_NS2_ST_BLOCKED:
626 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100627 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100628 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100629 return;
630 }
631
632 ns2_tx_status(priv->nsvc,
633 NS_CAUSE_NSVC_BLOCKED,
634 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200635 break;
636 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100637 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200638 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100639 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100640 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200641 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100642
643 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200644 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100645 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100646 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
647 /* Force the NSVC back to its initial state */
648 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100649 return;
650 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100651 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100652 case GPRS_NS2_EV_REQ_OM_BLOCK:
653 /* vty cmd: block */
654 priv->initiate_block = true;
655 priv->om_blocked = true;
656 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
657 break;
658 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
659 /* vty cmd: unblock*/
660 if (!priv->om_blocked)
661 return;
662 priv->om_blocked = false;
663 if (fi->state == GPRS_NS2_ST_BLOCKED)
664 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
665 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200666 }
667}
668
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100669static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100670 enum osmo_fsm_term_cause cause)
671{
672 struct gprs_ns2_vc_priv *priv = fi->priv;
673
674 osmo_timer_del(&priv->alive.timer);
675}
676
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100677static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200678 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100679 .states = ns2_vc_states,
680 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100681 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
682 S(GPRS_NS2_EV_RX_RESET) |
683 S(GPRS_NS2_EV_RX_ALIVE) |
684 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
685 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100686 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
687 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100688 .allstate_action = ns2_vc_fsm_allstate_action,
689 .cleanup = ns2_vc_fsm_clean,
690 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +0200691 /* .log_subsys = DNS, "is not constant" */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100692 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200693 .pre_term = NULL,
694 .log_subsys = DLNS,
695};
696
697/*!
698 * \brief gprs_ns2_vc_fsm_alloc
699 * \param ctx
700 * \param vc
701 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100702 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200703 * \return NULL on error, otherwise the fsm
704 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100705struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100706 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200707{
708 struct osmo_fsm_inst *fi;
709 struct gprs_ns2_vc_priv *priv;
710
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100711 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200712 if (!fi)
713 return fi;
714
715 nsvc->fi = fi;
716 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
717 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100718 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200719
720 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
721
722 return fi;
723}
724
Harald Welte5bef2cc2020-09-18 22:33:24 +0200725/*! Start a NS-VC FSM.
726 * \param nsvc the virtual circuit
727 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100728int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200729{
730 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
731 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100732 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200733 return 0;
734}
735
Daniel Willmanned0c9822020-11-18 14:08:07 +0100736/*! Reset a NS-VC FSM.
737 * \param nsvc the virtual circuit
738 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100739int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100740{
Alexander Couzensf5775432021-01-18 13:49:00 +0100741 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100742}
743
Alexander Couzens47afc422021-01-17 20:12:46 +0100744/*! Block a NS-VC.
745 * \param nsvc the virtual circuit
746 * \return 0 on success; negative on error */
747int ns2_vc_block(struct gprs_ns2_vc *nsvc)
748{
749 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
750}
751
752/*! Unblock a NS-VC.
753 * \param nsvc the virtual circuit
754 * \return 0 on success; negative on error */
755int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
756{
757 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
758}
759
Harald Welte5bef2cc2020-09-18 22:33:24 +0200760/*! entry point for messages from the driver/VL
761 * \param nsvc virtual circuit on which the message was received
762 * \param msg message that was received
763 * \param tp parsed TLVs of the received message
764 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100765int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200766{
767 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
768 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100769 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200770 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100771 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200772
773 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
774 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200775 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
776
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100777 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200778 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100779 rc = ns2_tx_status(nsvc, cause, 0, msg);
780 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200781 }
782 }
783
Alexander Couzens43771df2021-01-20 13:03:03 +0100784 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
785 nsei = tlvp_val16be(tp, NS_IE_NSEI);
786 if (nsei != nsvc->nse->nsei) {
787 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
788 if (nsh->pdu_type == NS_PDUT_RESET)
789 ns2_tx_reset_ack(nsvc);
790
791 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
792 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
793 goto out;
794 }
795 }
796
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100797 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
798 nsvci = tlvp_val16be(tp, NS_IE_VCI);
799 if (nsvci != nsvc->nsvci) {
800 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
801 if (nsh->pdu_type == NS_PDUT_RESET)
802 ns2_tx_reset_ack(nsvc);
803
804 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
805 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
806 goto out;
807 }
808 }
809
Alexander Couzens6a161492020-07-12 13:45:50 +0200810 switch (nsh->pdu_type) {
811 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100812 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200813 break;
814 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100815 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200816 break;
817 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100818 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200819 break;
820 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100821 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200822 break;
823 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100824 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200825 break;
826 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100827 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200828 break;
829 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100830 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200831 break;
832 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100833 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200834 break;
835 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100836 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100837 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100838 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200839 default:
Harald Weltef2949742021-01-20 14:54:14 +0100840 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
841 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200842 return -EINVAL;
843 }
844
Alexander Couzenscce88282020-10-26 00:25:50 +0100845out:
846 msgb_free(msg);
847
848 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200849}
850
Harald Welte5bef2cc2020-09-18 22:33:24 +0200851/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100852int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200853{
854 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
855}
856
857/* initialize osmo_ctx on main tread */
858static __attribute__((constructor)) void on_dso_load_ctx(void)
859{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100860 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200861}