blob: f4c4e4634ceb00b02e70210c59b19d0f672649ea [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;
73 struct timeval timer_started;
74 } 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:
84 * - UNCONFIGURED -> ALIVE -> UNBLOCKED
85 *
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 *
90 * The ALIVE state is used as intermediate, because a VC is only valid if it received an Alive ACK when
91 * 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
100 GPRS_NS2_ST_ALIVE, /* only used when not using RESET/BLOCK procedure */
101};
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 Welted0835762021-01-31 17:20:41 +0100125 { GPRS_NS2_EV_REQ_START, "START" },
Alexander Couzensf5775432021-01-18 13:49:00 +0100126 { GPRS_NS2_EV_RX_RESET, "RESET" },
127 { GPRS_NS2_EV_RX_RESET_ACK, "RESET_ACK" },
Harald Welted0835762021-01-31 17:20:41 +0100128 { GPRS_NS2_EV_RX_UNBLOCK, "UNBLOCK" },
Alexander Couzensf5775432021-01-18 13:49:00 +0100129 { GPRS_NS2_EV_RX_UNBLOCK_ACK, "UNBLOCK_ACK" },
130 { GPRS_NS2_EV_RX_BLOCK, "BLOCK" },
131 { GPRS_NS2_EV_RX_BLOCK_ACK, "BLOCK_ACK" },
132 { GPRS_NS2_EV_RX_ALIVE, "ALIVE" },
133 { GPRS_NS2_EV_RX_ALIVE_ACK, "ALIVE_ACK" },
Harald Welted0835762021-01-31 17:20:41 +0100134 { GPRS_NS2_EV_RX_STATUS, "STATUS" },
135 { GPRS_NS2_EV_RX_UNITDATA, "UNITDATA" },
Alexander Couzensf5775432021-01-18 13:49:00 +0100136 { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "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
158 osmo_gettimeofday(&priv->alive.timer_started, NULL);
159 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{
170 struct timeval now, elapsed;
171 osmo_gettimeofday(&now, NULL);
172 timersub(&now, &priv->alive.timer_started, &elapsed);
173
174 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
175}
176
177static void recv_test_procedure(struct osmo_fsm_inst *fi)
178{
179 struct gprs_ns2_vc_priv *priv = fi->priv;
180 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
181 struct gprs_ns2_vc *nsvc = priv->nsvc;
182
183 /* ignoring ACKs without sending an ALIVE */
184 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
185 return;
186
187 priv->alive.mode = NS_TOUT_TNS_TEST;
188 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
189 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
190 alive_timer_elapsed_ms(priv));
191}
192
193
194static void alive_timeout_handler(void *data)
195{
196 struct osmo_fsm_inst *fi = data;
197 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
198 struct gprs_ns2_vc_priv *priv = fi->priv;
199
200 switch (priv->alive.mode) {
201 case NS_TOUT_TNS_TEST:
202 priv->alive.mode = NS_TOUT_TNS_ALIVE;
203 ns2_tx_alive(priv->nsvc);
204 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
205 break;
206 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100207 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200208 priv->alive.N++;
209
210 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
211 /* retransmission */
212 ns2_tx_alive(priv->nsvc);
213 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
214 } else {
215 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100216 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200217 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
218 } else {
219 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
220 }
221 }
222 break;
223 default:
224 break;
225 }
226}
227
Harald Welte6a9ec422021-01-31 18:56:29 +0100228
229static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
230{
231 stop_test_procedure(fi->priv);
232}
233
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100234static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200235{
236 struct gprs_ns2_vc_priv *priv = fi->priv;
237 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
238
Alexander Couzensea01bf22021-01-18 14:01:01 +0100239 priv->initiate_reset = priv->initiate_block = priv->initiator;
240 priv->om_blocked = false;
241
Alexander Couzens6a161492020-07-12 13:45:50 +0200242 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100243 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200244 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100245 case GPRS_NS2_VC_MODE_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
247 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100248 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200249 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
250 break;
251 }
252
253 break;
254 default:
255 OSMO_ASSERT(0);
256 }
257}
258
259
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100260static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200261{
262 struct gprs_ns2_vc_priv *priv = fi->priv;
263
264 if (old_state != GPRS_NS2_ST_RESET)
265 priv->N = 0;
266
Alexander Couzens47afc422021-01-17 20:12:46 +0100267 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100268 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
270
271 stop_test_procedure(priv);
272 ns2_nse_notify_unblocked(priv->nsvc, false);
273}
274
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100275static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200276{
277 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
278 struct gprs_ns2_vc_priv *priv = fi->priv;
279
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100280 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100282 case GPRS_NS2_EV_RX_RESET:
283 ns2_tx_reset_ack(priv->nsvc);
284 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100285 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
287 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
288 break;
289 }
290 } else {
291 /* we are on the receiving end */
292 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100293 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200294 ns2_tx_reset_ack(priv->nsvc);
295 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
296 0, 0);
297 break;
298 }
299 }
300}
301
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100302static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200303{
304 struct gprs_ns2_vc_priv *priv = fi->priv;
305
Harald Welte4c54f592021-01-30 22:42:15 +0100306 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200307 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100308 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
309 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200310
Alexander Couzens47afc422021-01-17 20:12:46 +0100311 if (priv->om_blocked) {
312 /* we are already blocked after a RESET */
313 if (old_state == GPRS_NS2_ST_RESET) {
314 osmo_timer_del(&fi->timer);
315 } else {
316 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
317 }
318 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200319 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100320 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200321
322 start_test_procedure(priv);
323}
324
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100325static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200326{
327 struct gprs_ns2_vc_priv *priv = fi->priv;
328
Alexander Couzens47afc422021-01-17 20:12:46 +0100329 if (priv->om_blocked) {
330 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100331 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100332 priv->accept_unitdata = false;
333 osmo_timer_del(&fi->timer);
334 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100335 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100336 priv->accept_unitdata = false;
337 ns2_tx_block_ack(priv->nsvc);
338 osmo_timer_del(&fi->timer);
339 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100340 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100341 priv->accept_unitdata = false;
342 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
343 osmo_timer_add(&fi->timer);
344 break;
345 }
346 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200347 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100348 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 /* TODO: BLOCK is a UNBLOCK_NACK */
350 ns2_tx_block_ack(priv->nsvc);
351 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100352 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200353 ns2_tx_unblock_ack(priv->nsvc);
354 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100355 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100356 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200357 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
358 0, NS_TOUT_TNS_TEST);
359 break;
360 }
361 } else {
362 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
363 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100364 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100365 ns2_tx_block_ack(priv->nsvc);
366 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100367 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200368 ns2_tx_unblock_ack(priv->nsvc);
369 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
370 0, 0);
371 break;
372 }
373 }
374}
375
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100376static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200377{
378 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100379 struct gprs_ns2_vc *nsvc = priv->nsvc;
380 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200381
Harald Welteb40bf8b2021-01-30 22:43:01 +0100382 if (old_state != GPRS_NS2_ST_UNBLOCKED)
383 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
384
Alexander Couzens47afc422021-01-17 20:12:46 +0100385 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100386 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100387 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200388}
389
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100390static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200391{
392 struct gprs_ns2_vc_priv *priv = fi->priv;
393
394 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100395 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100396 ns2_tx_unblock_ack(priv->nsvc);
397 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100398 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100399 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200400 ns2_tx_block_ack(priv->nsvc);
401 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
402 0, 2);
403 break;
404 }
405}
406
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100407static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200408{
409 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100410 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200411 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
412 break;
413 }
414}
415
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100416static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200417{
418 struct gprs_ns2_vc_priv *priv = fi->priv;
419 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
420
421 priv->alive.mode = NS_TOUT_TNS_TEST;
422 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
423
424 if (old_state != GPRS_NS2_ST_ALIVE)
425 priv->N = 0;
426
427 ns2_tx_alive(priv->nsvc);
428 ns2_nse_notify_unblocked(priv->nsvc, false);
429}
430
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100431static void ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200432{
433 start_test_procedure(fi->priv);
434}
435
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100436static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200437 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100438 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Alexander Couzens6a161492020-07-12 13:45:50 +0200439 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE),
440 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100441 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100442 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200443 },
444 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100445 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200446 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100447 S(GPRS_NS2_ST_BLOCKED) |
448 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200449 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100450 .action = ns2_st_reset,
451 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200452 },
453 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100454 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
455 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200456 .out_state_mask = S(GPRS_NS2_ST_RESET) |
457 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100458 S(GPRS_NS2_ST_BLOCKED) |
459 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200460 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100461 .action = ns2_st_blocked,
462 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200463 },
464 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100465 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
466 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_ALIVE) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100468 S(GPRS_NS2_ST_BLOCKED) |
469 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200470 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100471 .action = ns2_st_unblocked,
472 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200473 },
474
475 /* ST_ALIVE is only used on VC without RESET/BLOCK */
476 [GPRS_NS2_ST_ALIVE] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100477 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzens191c2d72021-01-19 17:59:45 +0100478 .out_state_mask = S(GPRS_NS2_ST_ALIVE) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100479 S(GPRS_NS2_ST_UNBLOCKED) |
480 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200481 .name = "ALIVE",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100482 .action = ns2_st_alive,
483 .onenter = ns2_st_alive_onenter,
484 .onleave = ns2_st_alive_onleave,
Alexander Couzens6a161492020-07-12 13:45:50 +0200485 },
486};
487
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100488static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200489{
490 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
491 struct gprs_ns2_vc_priv *priv = fi->priv;
492
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100493 switch (fi->state) {
494 case GPRS_NS2_ST_RESET:
495 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100496 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200497 priv->N++;
498 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
499 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
500 } else {
501 priv->N = 0;
502 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
503 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100504 }
505 break;
506 case GPRS_NS2_ST_BLOCKED:
507 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200508 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100509 if (priv->om_blocked) {
510 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
511 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
512 } else {
513 /* 7.2 stop accepting data when BLOCK PDU not responded */
514 priv->accept_unitdata = false;
515 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200516 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100517 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
518 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
519 } else {
520 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
521 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200522 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100523 }
524 break;
525 case GPRS_NS2_ST_ALIVE:
526 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 priv->N++;
528 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
529 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
530 } else {
531 priv->N = 0;
532 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_ALIVE, 0, 0);
533 }
534 break;
535 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100536 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200537 }
538 return 0;
539}
540
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100541static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200542 struct msgb *msg)
543{
544 struct gprs_ns2_vc_priv *priv = fi->priv;
545 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
546 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
547 struct osmo_gprs_ns2_prim nsp = {};
548 uint16_t bvci;
549
Alexander Couzenscce88282020-10-26 00:25:50 +0100550 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
551 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200552 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100553 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200554
555 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
556 * for a PTP BVC may indicate a request to change the IP endpoint
557 * and/or a response to a change in the IP endpoint. */
558
559 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
560 bvci = nsh->data[1] << 8 | nsh->data[2];
561
Alexander Couzens89acdef2020-09-23 18:22:31 +0200562 msg->l3h = &nsh->data[3];
563 nsp.bvci = bvci;
564 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200565
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200566 /* 10.3.9 NS SDU Control Bits */
567 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100568 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200569
Alexander Couzens138b96f2021-01-25 16:23:29 +0100570 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200571 PRIM_OP_INDICATION, msg);
572 nsi->cb(&nsp.oph, nsi->cb_data);
573}
574
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100575static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200576 uint32_t event,
577 void *data)
578{
579 struct gprs_ns2_vc_priv *priv = fi->priv;
580 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100581 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200582
583 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100584 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100585 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200586 break;
587
588 /* move the FSM into reset */
589 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100590 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200591 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
592 }
593 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100594 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200595 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100596 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200597 switch (fi->state) {
598 case GPRS_NS2_ST_UNCONFIGURED:
599 case GPRS_NS2_ST_RESET:
600 /* ignore ALIVE */
601 break;
602 default:
603 ns2_tx_alive_ack(priv->nsvc);
604 }
605 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100606 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200607 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
608 if (fi->state == GPRS_NS2_ST_ALIVE)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100609 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200610 else
611 recv_test_procedure(fi);
612 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100613 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100614 /* UNITDATA has to handle the release of msg.
615 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
616 * the msg, the upper layer has to do it.
617 * Otherwise the msg must be freed.
618 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200619 switch (fi->state) {
620 case GPRS_NS2_ST_BLOCKED:
621 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100622 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100623 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100624 return;
625 }
626
627 ns2_tx_status(priv->nsvc,
628 NS_CAUSE_NSVC_BLOCKED,
629 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200630 break;
631 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
632 case GPRS_NS2_ST_ALIVE:
633 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100634 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100635 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200636 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100637
638 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200639 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100640 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100641 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
642 /* Force the NSVC back to its initial state */
643 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100644 return;
645 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100646 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100647 case GPRS_NS2_EV_REQ_OM_BLOCK:
648 /* vty cmd: block */
649 priv->initiate_block = true;
650 priv->om_blocked = true;
651 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
652 break;
653 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
654 /* vty cmd: unblock*/
655 if (!priv->om_blocked)
656 return;
657 priv->om_blocked = false;
658 if (fi->state == GPRS_NS2_ST_BLOCKED)
659 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
660 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200661 }
662}
663
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100664static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100665 enum osmo_fsm_term_cause cause)
666{
667 struct gprs_ns2_vc_priv *priv = fi->priv;
668
669 osmo_timer_del(&priv->alive.timer);
670}
671
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100672static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200673 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100674 .states = ns2_vc_states,
675 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100676 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
677 S(GPRS_NS2_EV_RX_RESET) |
678 S(GPRS_NS2_EV_RX_ALIVE) |
679 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
680 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100681 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
682 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100683 .allstate_action = ns2_vc_fsm_allstate_action,
684 .cleanup = ns2_vc_fsm_clean,
685 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +0200686 /* .log_subsys = DNS, "is not constant" */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100687 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200688 .pre_term = NULL,
689 .log_subsys = DLNS,
690};
691
692/*!
693 * \brief gprs_ns2_vc_fsm_alloc
694 * \param ctx
695 * \param vc
696 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100697 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200698 * \return NULL on error, otherwise the fsm
699 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100700struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100701 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200702{
703 struct osmo_fsm_inst *fi;
704 struct gprs_ns2_vc_priv *priv;
705
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100706 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200707 if (!fi)
708 return fi;
709
710 nsvc->fi = fi;
711 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
712 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100713 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200714
715 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
716
717 return fi;
718}
719
Harald Welte5bef2cc2020-09-18 22:33:24 +0200720/*! Start a NS-VC FSM.
721 * \param nsvc the virtual circuit
722 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100723int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200724{
725 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
726 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100727 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200728 return 0;
729}
730
Daniel Willmanned0c9822020-11-18 14:08:07 +0100731/*! Reset a NS-VC FSM.
732 * \param nsvc the virtual circuit
733 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100734int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100735{
Alexander Couzensf5775432021-01-18 13:49:00 +0100736 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100737}
738
Alexander Couzens47afc422021-01-17 20:12:46 +0100739/*! Block a NS-VC.
740 * \param nsvc the virtual circuit
741 * \return 0 on success; negative on error */
742int ns2_vc_block(struct gprs_ns2_vc *nsvc)
743{
744 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
745}
746
747/*! Unblock a NS-VC.
748 * \param nsvc the virtual circuit
749 * \return 0 on success; negative on error */
750int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
751{
752 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
753}
754
Harald Welte5bef2cc2020-09-18 22:33:24 +0200755/*! entry point for messages from the driver/VL
756 * \param nsvc virtual circuit on which the message was received
757 * \param msg message that was received
758 * \param tp parsed TLVs of the received message
759 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100760int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200761{
762 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
763 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100764 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200765 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100766 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200767
768 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
769 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200770 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
771
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100772 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200773 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100774 rc = ns2_tx_status(nsvc, cause, 0, msg);
775 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200776 }
777 }
778
Alexander Couzens43771df2021-01-20 13:03:03 +0100779 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
780 nsei = tlvp_val16be(tp, NS_IE_NSEI);
781 if (nsei != nsvc->nse->nsei) {
782 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
783 if (nsh->pdu_type == NS_PDUT_RESET)
784 ns2_tx_reset_ack(nsvc);
785
786 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
787 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
788 goto out;
789 }
790 }
791
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100792 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
793 nsvci = tlvp_val16be(tp, NS_IE_VCI);
794 if (nsvci != nsvc->nsvci) {
795 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
796 if (nsh->pdu_type == NS_PDUT_RESET)
797 ns2_tx_reset_ack(nsvc);
798
799 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
800 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
801 goto out;
802 }
803 }
804
Alexander Couzens6a161492020-07-12 13:45:50 +0200805 switch (nsh->pdu_type) {
806 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100807 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200808 break;
809 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100810 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200811 break;
812 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100813 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200814 break;
815 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100816 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200817 break;
818 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100819 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200820 break;
821 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100822 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200823 break;
824 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100825 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200826 break;
827 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100828 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200829 break;
830 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100831 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100832 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100833 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200834 default:
Harald Weltef2949742021-01-20 14:54:14 +0100835 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
836 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200837 return -EINVAL;
838 }
839
Alexander Couzenscce88282020-10-26 00:25:50 +0100840out:
841 msgb_free(msg);
842
843 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200844}
845
Harald Welte5bef2cc2020-09-18 22:33:24 +0200846/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100847int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200848{
849 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
850}
851
852/* initialize osmo_ctx on main tread */
853static __attribute__((constructor)) void on_dso_load_ctx(void)
854{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100855 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200856}