blob: 65248eaf6d8610a86ff25408ced501d987810b7b [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
Alexander Couzens6a161492020-07-12 13:45:50 +020052struct gprs_ns2_vc_priv {
53 struct gprs_ns2_vc *nsvc;
54 /* how often the timer was triggered */
55 int N;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010056 /* The initiator is responsible to UNBLOCK the VC. The BSS is usually the initiator.
57 * It can change during runtime. The side which blocks an unblocked side.*/
Alexander Couzensea01bf22021-01-18 14:01:01 +010058 bool initiator;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010059 bool initiate_block;
60 bool initiate_reset;
Alexander Couzens47afc422021-01-17 20:12:46 +010061 /* if blocked by O&M/vty */
62 bool om_blocked;
63 /* if unitdata is forwarded to the user */
64 bool accept_unitdata;
Alexander Couzens6a161492020-07-12 13:45:50 +020065
66 /* the alive counter is present in all states */
67 struct {
68 struct osmo_timer_list timer;
69 enum ns2_timeout mode;
70 int N;
Alexander Couzensab0e8642021-02-12 02:50:44 +010071 struct timespec timer_started;
Alexander Couzens6a161492020-07-12 13:45:50 +020072 } alive;
73};
74
75
76/* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure..
77 *
78 * With RESET/BLOCK, the state should follow:
79 * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED
80 *
81 * Without RESET/BLOCK, the state should follow:
Alexander Couzensd3e31102021-02-03 11:15:07 +010082 * - UNCONFIGURED -> RECOVERY -> UNBLOCKED
Alexander Couzens6a161492020-07-12 13:45:50 +020083 *
84 * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive.
85 * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU.
86 * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED.
87 *
Alexander Couzensd3e31102021-02-03 11:15:07 +010088 * 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 +020089 * not using RESET/BLOCK procedure.
90 */
91
92enum gprs_ns2_vc_state {
93 GPRS_NS2_ST_UNCONFIGURED,
94 GPRS_NS2_ST_RESET,
95 GPRS_NS2_ST_BLOCKED,
96 GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */
97
Alexander Couzensd3e31102021-02-03 11:15:07 +010098 GPRS_NS2_ST_RECOVERING, /* only used when not using RESET/BLOCK procedure */
Alexander Couzens6a161492020-07-12 13:45:50 +020099};
100
101enum gprs_ns2_vc_event {
Alexander Couzensf5775432021-01-18 13:49:00 +0100102 GPRS_NS2_EV_REQ_START,
Alexander Couzens6a161492020-07-12 13:45:50 +0200103
104 /* received messages */
Alexander Couzensf5775432021-01-18 13:49:00 +0100105 GPRS_NS2_EV_RX_RESET,
106 GPRS_NS2_EV_RX_RESET_ACK,
107 GPRS_NS2_EV_RX_UNBLOCK,
108 GPRS_NS2_EV_RX_UNBLOCK_ACK,
109 GPRS_NS2_EV_RX_BLOCK,
110 GPRS_NS2_EV_RX_BLOCK_ACK,
111 GPRS_NS2_EV_RX_ALIVE,
112 GPRS_NS2_EV_RX_ALIVE_ACK,
113 GPRS_NS2_EV_RX_STATUS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200114
Alexander Couzensf5775432021-01-18 13:49:00 +0100115 GPRS_NS2_EV_RX_UNITDATA,
Daniel Willmanned0c9822020-11-18 14:08:07 +0100116
Alexander Couzensf5775432021-01-18 13:49:00 +0100117 GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */
118 GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */
119 GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/
Alexander Couzens6a161492020-07-12 13:45:50 +0200120};
121
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100122static const struct value_string ns2_vc_event_names[] = {
Harald Welteb339cbb2021-02-05 14:49:21 +0100123 { GPRS_NS2_EV_REQ_START, "REQ-START" },
124 { GPRS_NS2_EV_RX_RESET, "RX-RESET" },
125 { GPRS_NS2_EV_RX_RESET_ACK, "RX-RESET_ACK" },
126 { GPRS_NS2_EV_RX_UNBLOCK, "RX-UNBLOCK" },
127 { GPRS_NS2_EV_RX_UNBLOCK_ACK, "RX-UNBLOCK_ACK" },
128 { GPRS_NS2_EV_RX_BLOCK, "RX-BLOCK" },
129 { GPRS_NS2_EV_RX_BLOCK_ACK, "RX-BLOCK_ACK" },
130 { GPRS_NS2_EV_RX_ALIVE, "RX-ALIVE" },
131 { GPRS_NS2_EV_RX_ALIVE_ACK, "RX-ALIVE_ACK" },
132 { GPRS_NS2_EV_RX_STATUS, "RX-STATUS" },
133 { GPRS_NS2_EV_RX_UNITDATA, "RX-UNITDATA" },
134 { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "REQ-FORCE_UNCONFIGURED" },
Alexander Couzens47afc422021-01-17 20:12:46 +0100135 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
136 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200137 { 0, NULL }
138};
139
140static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
141{
142 struct gprs_ns2_vc_priv *priv = fi->priv;
143 return priv->nsvc->nse->nsi;
144}
145
146static void start_test_procedure(struct gprs_ns2_vc_priv *priv)
147{
148 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
149
150 if (osmo_timer_pending(&priv->alive.timer))
151 return;
152
153 priv->alive.mode = NS_TOUT_TNS_ALIVE;
154 priv->alive.N = 0;
155
Alexander Couzensab0e8642021-02-12 02:50:44 +0100156 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200157 ns2_tx_alive(priv->nsvc);
158 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
159}
160
161static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
162{
163 osmo_timer_del(&priv->alive.timer);
164}
165
166static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
167{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100168 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200169
Alexander Couzensab0e8642021-02-12 02:50:44 +0100170 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
171 return 0;
172
173 timespecsub(&now, &priv->alive.timer_started, &elapsed);
Alexander Couzensab0e8642021-02-12 02:50:44 +0100174 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200175}
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;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100203 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100204 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200205 ns2_tx_alive(priv->nsvc);
206 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
207 break;
208 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100209 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200210 priv->alive.N++;
211
212 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
213 /* retransmission */
214 ns2_tx_alive(priv->nsvc);
215 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
216 } else {
217 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100218 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200219 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
220 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100221 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200222 }
223 }
224 break;
225 default:
226 break;
227 }
228}
229
Harald Welte6a9ec422021-01-31 18:56:29 +0100230
231static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
232{
233 stop_test_procedure(fi->priv);
234}
235
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100236static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200237{
238 struct gprs_ns2_vc_priv *priv = fi->priv;
239 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
240
Alexander Couzensea01bf22021-01-18 14:01:01 +0100241 priv->initiate_reset = priv->initiate_block = priv->initiator;
242 priv->om_blocked = false;
243
Alexander Couzens6a161492020-07-12 13:45:50 +0200244 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100245 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100247 case GPRS_NS2_VC_MODE_ALIVE:
Harald Weltec51ddf22021-03-05 09:48:18 +0100248 if (priv->nsvc->nse->dialect == GPRS_NS2_DIALECT_SNS) {
249 /* In IP-SNS, the NS-VC are assumed initially alive, until the alive
250 * procedure should fail at some future point */
251 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
252 } else {
253 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], NS_TOUT_TNS_ALIVE);
254 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100256 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200257 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
258 break;
259 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200260 break;
261 default:
262 OSMO_ASSERT(0);
263 }
264}
265
266
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100267static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200268{
269 struct gprs_ns2_vc_priv *priv = fi->priv;
270
271 if (old_state != GPRS_NS2_ST_RESET)
272 priv->N = 0;
273
Alexander Couzens47afc422021-01-17 20:12:46 +0100274 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100275 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200276 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
277
278 stop_test_procedure(priv);
279 ns2_nse_notify_unblocked(priv->nsvc, false);
280}
281
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100282static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200283{
284 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
285 struct gprs_ns2_vc_priv *priv = fi->priv;
286
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100287 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200288 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100289 case GPRS_NS2_EV_RX_RESET:
290 ns2_tx_reset_ack(priv->nsvc);
291 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100292 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200293 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
294 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
295 break;
296 }
297 } else {
298 /* we are on the receiving end */
299 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100300 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200301 ns2_tx_reset_ack(priv->nsvc);
302 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
303 0, 0);
304 break;
305 }
306 }
307}
308
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100309static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200310{
311 struct gprs_ns2_vc_priv *priv = fi->priv;
312
Harald Welte4c54f592021-01-30 22:42:15 +0100313 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200314 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100315 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
316 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200317
Alexander Couzens47afc422021-01-17 20:12:46 +0100318 if (priv->om_blocked) {
319 /* we are already blocked after a RESET */
320 if (old_state == GPRS_NS2_ST_RESET) {
321 osmo_timer_del(&fi->timer);
322 } else {
323 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
324 }
325 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200326 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100327 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200328
329 start_test_procedure(priv);
330}
331
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100332static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200333{
334 struct gprs_ns2_vc_priv *priv = fi->priv;
335
Alexander Couzens47afc422021-01-17 20:12:46 +0100336 if (priv->om_blocked) {
337 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100338 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100339 priv->accept_unitdata = false;
340 osmo_timer_del(&fi->timer);
341 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100342 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100343 priv->accept_unitdata = false;
344 ns2_tx_block_ack(priv->nsvc);
345 osmo_timer_del(&fi->timer);
346 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100347 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100348 priv->accept_unitdata = false;
349 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
350 osmo_timer_add(&fi->timer);
351 break;
352 }
353 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200354 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100355 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200356 /* TODO: BLOCK is a UNBLOCK_NACK */
357 ns2_tx_block_ack(priv->nsvc);
358 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100359 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200360 ns2_tx_unblock_ack(priv->nsvc);
361 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100362 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100363 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200364 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
365 0, NS_TOUT_TNS_TEST);
366 break;
367 }
368 } else {
369 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
370 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100371 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100372 ns2_tx_block_ack(priv->nsvc);
373 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100374 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200375 ns2_tx_unblock_ack(priv->nsvc);
376 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
377 0, 0);
378 break;
379 }
380 }
381}
382
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100383static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200384{
385 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100386 struct gprs_ns2_vc *nsvc = priv->nsvc;
387 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200388
Harald Welteb40bf8b2021-01-30 22:43:01 +0100389 if (old_state != GPRS_NS2_ST_UNBLOCKED)
390 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
391
Alexander Couzens47afc422021-01-17 20:12:46 +0100392 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100393 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100394 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200395}
396
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100397static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200398{
399 struct gprs_ns2_vc_priv *priv = fi->priv;
400
401 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100402 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100403 ns2_tx_unblock_ack(priv->nsvc);
404 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100405 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100406 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200407 ns2_tx_block_ack(priv->nsvc);
408 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
409 0, 2);
410 break;
411 }
412}
413
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100414static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200415{
416 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100417 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200418 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
419 break;
420 }
421}
422
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100423static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200424{
425 struct gprs_ns2_vc_priv *priv = fi->priv;
426 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
427
428 priv->alive.mode = NS_TOUT_TNS_TEST;
429 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
430
Alexander Couzensd3e31102021-02-03 11:15:07 +0100431 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200432 priv->N = 0;
433
434 ns2_tx_alive(priv->nsvc);
435 ns2_nse_notify_unblocked(priv->nsvc, false);
Harald Weltec51ddf22021-03-05 09:48:18 +0100436 start_test_procedure(priv);
Alexander Couzens6a161492020-07-12 13:45:50 +0200437}
438
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100439static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200440 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100441 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Harald Weltec51ddf22021-03-05 09:48:18 +0100442 .out_state_mask = S(GPRS_NS2_ST_RESET) |
443 S(GPRS_NS2_ST_RECOVERING) |
444 S(GPRS_NS2_ST_UNBLOCKED),
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,
Alexander Couzens6a161492020-07-12 13:45:50 +0200489 },
490};
491
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100492static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200493{
494 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
495 struct gprs_ns2_vc_priv *priv = fi->priv;
496
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100497 switch (fi->state) {
498 case GPRS_NS2_ST_RESET:
499 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100500 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200501 priv->N++;
502 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
503 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
504 } else {
505 priv->N = 0;
506 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
507 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100508 }
509 break;
510 case GPRS_NS2_ST_BLOCKED:
511 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200512 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100513 if (priv->om_blocked) {
514 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
515 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
516 } else {
517 /* 7.2 stop accepting data when BLOCK PDU not responded */
518 priv->accept_unitdata = false;
519 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200520 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100521 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
522 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
523 } else {
524 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
525 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200526 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100527 }
528 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100529 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100530 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200531 priv->N++;
532 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100533 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200534 } else {
535 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100536 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200537 }
538 break;
539 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100540 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200541 }
542 return 0;
543}
544
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100545static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200546 struct msgb *msg)
547{
548 struct gprs_ns2_vc_priv *priv = fi->priv;
549 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
550 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
551 struct osmo_gprs_ns2_prim nsp = {};
552 uint16_t bvci;
553
Alexander Couzenscce88282020-10-26 00:25:50 +0100554 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
555 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200556 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100557 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200558
559 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
560 * for a PTP BVC may indicate a request to change the IP endpoint
561 * and/or a response to a change in the IP endpoint. */
562
563 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
564 bvci = nsh->data[1] << 8 | nsh->data[2];
565
Alexander Couzens89acdef2020-09-23 18:22:31 +0200566 msg->l3h = &nsh->data[3];
567 nsp.bvci = bvci;
568 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200569
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200570 /* 10.3.9 NS SDU Control Bits */
571 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100572 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200573
Alexander Couzens138b96f2021-01-25 16:23:29 +0100574 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200575 PRIM_OP_INDICATION, msg);
576 nsi->cb(&nsp.oph, nsi->cb_data);
577}
578
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100579static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200580 uint32_t event,
581 void *data)
582{
583 struct gprs_ns2_vc_priv *priv = fi->priv;
584 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100585 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200586
587 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100588 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100589 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200590 break;
591
592 /* move the FSM into reset */
593 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100594 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200595 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
596 }
597 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100598 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200599 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100600 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200601 switch (fi->state) {
602 case GPRS_NS2_ST_UNCONFIGURED:
603 case GPRS_NS2_ST_RESET:
604 /* ignore ALIVE */
605 break;
606 default:
607 ns2_tx_alive_ack(priv->nsvc);
608 }
609 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100610 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200611 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100612 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100613 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200614 else
615 recv_test_procedure(fi);
616 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100617 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100618 /* UNITDATA has to handle the release of msg.
619 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
620 * the msg, the upper layer has to do it.
621 * Otherwise the msg must be freed.
622 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200623 switch (fi->state) {
624 case GPRS_NS2_ST_BLOCKED:
625 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100626 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100627 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100628 return;
629 }
630
631 ns2_tx_status(priv->nsvc,
632 NS_CAUSE_NSVC_BLOCKED,
633 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200634 break;
635 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100636 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200637 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100638 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100639 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200640 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100641
642 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200643 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100644 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100645 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
646 /* Force the NSVC back to its initial state */
647 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100648 return;
649 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100650 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100651 case GPRS_NS2_EV_REQ_OM_BLOCK:
652 /* vty cmd: block */
653 priv->initiate_block = true;
654 priv->om_blocked = true;
655 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
656 break;
657 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
658 /* vty cmd: unblock*/
659 if (!priv->om_blocked)
660 return;
661 priv->om_blocked = false;
662 if (fi->state == GPRS_NS2_ST_BLOCKED)
663 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
664 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200665 }
666}
667
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100668static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100669 enum osmo_fsm_term_cause cause)
670{
671 struct gprs_ns2_vc_priv *priv = fi->priv;
672
673 osmo_timer_del(&priv->alive.timer);
674}
675
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100676static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200677 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100678 .states = ns2_vc_states,
679 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100680 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
681 S(GPRS_NS2_EV_RX_RESET) |
682 S(GPRS_NS2_EV_RX_ALIVE) |
683 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
684 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100685 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
686 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100687 .allstate_action = ns2_vc_fsm_allstate_action,
688 .cleanup = ns2_vc_fsm_clean,
689 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100690 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200691 .pre_term = NULL,
692 .log_subsys = DLNS,
693};
694
695/*!
696 * \brief gprs_ns2_vc_fsm_alloc
697 * \param ctx
698 * \param vc
699 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100700 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200701 * \return NULL on error, otherwise the fsm
702 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100703struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100704 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200705{
706 struct osmo_fsm_inst *fi;
707 struct gprs_ns2_vc_priv *priv;
708
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100709 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200710 if (!fi)
711 return fi;
712
713 nsvc->fi = fi;
714 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
715 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100716 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200717
718 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
719
720 return fi;
721}
722
Harald Welte5bef2cc2020-09-18 22:33:24 +0200723/*! Start a NS-VC FSM.
724 * \param nsvc the virtual circuit
725 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100726int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200727{
728 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
729 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100730 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 return 0;
732}
733
Daniel Willmanned0c9822020-11-18 14:08:07 +0100734/*! Reset a NS-VC FSM.
735 * \param nsvc the virtual circuit
736 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100737int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100738{
Alexander Couzensf5775432021-01-18 13:49:00 +0100739 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100740}
741
Alexander Couzens47afc422021-01-17 20:12:46 +0100742/*! Block a NS-VC.
743 * \param nsvc the virtual circuit
744 * \return 0 on success; negative on error */
745int ns2_vc_block(struct gprs_ns2_vc *nsvc)
746{
747 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
748}
749
750/*! Unblock a NS-VC.
751 * \param nsvc the virtual circuit
752 * \return 0 on success; negative on error */
753int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
754{
755 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
756}
757
Harald Welte5bef2cc2020-09-18 22:33:24 +0200758/*! entry point for messages from the driver/VL
759 * \param nsvc virtual circuit on which the message was received
760 * \param msg message that was received
761 * \param tp parsed TLVs of the received message
762 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100763int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200764{
765 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
766 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100767 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200768 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100769 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200770
771 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
772 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200773 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
774
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100775 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200776 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100777 rc = ns2_tx_status(nsvc, cause, 0, msg);
778 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200779 }
780 }
781
Alexander Couzens43771df2021-01-20 13:03:03 +0100782 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
783 nsei = tlvp_val16be(tp, NS_IE_NSEI);
784 if (nsei != nsvc->nse->nsei) {
785 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
786 if (nsh->pdu_type == NS_PDUT_RESET)
787 ns2_tx_reset_ack(nsvc);
788
789 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
790 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
791 goto out;
792 }
793 }
794
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100795 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
796 nsvci = tlvp_val16be(tp, NS_IE_VCI);
797 if (nsvci != nsvc->nsvci) {
798 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
799 if (nsh->pdu_type == NS_PDUT_RESET)
800 ns2_tx_reset_ack(nsvc);
801
802 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
803 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
804 goto out;
805 }
806 }
807
Alexander Couzens6a161492020-07-12 13:45:50 +0200808 switch (nsh->pdu_type) {
809 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100810 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200811 break;
812 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100813 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200814 break;
815 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100816 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200817 break;
818 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100819 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200820 break;
821 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100822 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200823 break;
824 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100825 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200826 break;
827 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100828 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200829 break;
830 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100831 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200832 break;
833 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100834 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100835 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100836 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200837 default:
Harald Weltef2949742021-01-20 14:54:14 +0100838 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
839 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200840 return -EINVAL;
841 }
842
Alexander Couzenscce88282020-10-26 00:25:50 +0100843out:
844 msgb_free(msg);
845
846 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200847}
848
Harald Welte5bef2cc2020-09-18 22:33:24 +0200849/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100850int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200851{
852 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
853}
854
855/* initialize osmo_ctx on main tread */
856static __attribute__((constructor)) void on_dso_load_ctx(void)
857{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100858 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200859}