blob: 40c89849047a0db1d3e5cce0b9494b0ea1cb646a [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);
Alexander Couzensab0e8642021-02-12 02:50:44 +0100176 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200177}
178
179static void recv_test_procedure(struct osmo_fsm_inst *fi)
180{
181 struct gprs_ns2_vc_priv *priv = fi->priv;
182 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
183 struct gprs_ns2_vc *nsvc = priv->nsvc;
184
185 /* ignoring ACKs without sending an ALIVE */
186 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
187 return;
188
189 priv->alive.mode = NS_TOUT_TNS_TEST;
190 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
191 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
192 alive_timer_elapsed_ms(priv));
193}
194
195
196static void alive_timeout_handler(void *data)
197{
198 struct osmo_fsm_inst *fi = data;
199 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
200 struct gprs_ns2_vc_priv *priv = fi->priv;
201
202 switch (priv->alive.mode) {
203 case NS_TOUT_TNS_TEST:
204 priv->alive.mode = NS_TOUT_TNS_ALIVE;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100205 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100206 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200207 ns2_tx_alive(priv->nsvc);
208 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
209 break;
210 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100211 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200212 priv->alive.N++;
213
214 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
215 /* retransmission */
216 ns2_tx_alive(priv->nsvc);
217 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
218 } else {
219 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100220 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200221 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
222 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100223 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200224 }
225 }
226 break;
227 default:
228 break;
229 }
230}
231
Harald Welte6a9ec422021-01-31 18:56:29 +0100232
233static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
234{
235 stop_test_procedure(fi->priv);
236}
237
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100238static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200239{
240 struct gprs_ns2_vc_priv *priv = fi->priv;
241 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
242
Alexander Couzensea01bf22021-01-18 14:01:01 +0100243 priv->initiate_reset = priv->initiate_block = priv->initiator;
244 priv->om_blocked = false;
245
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100247 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200248 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100249 case GPRS_NS2_VC_MODE_ALIVE:
Alexander Couzensd3e31102021-02-03 11:15:07 +0100250 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 +0200251 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100252 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200253 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
254 break;
255 }
256
257 break;
258 default:
259 OSMO_ASSERT(0);
260 }
261}
262
263
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100264static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200265{
266 struct gprs_ns2_vc_priv *priv = fi->priv;
267
268 if (old_state != GPRS_NS2_ST_RESET)
269 priv->N = 0;
270
Alexander Couzens47afc422021-01-17 20:12:46 +0100271 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100272 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200273 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
274
275 stop_test_procedure(priv);
276 ns2_nse_notify_unblocked(priv->nsvc, false);
277}
278
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100279static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200280{
281 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
282 struct gprs_ns2_vc_priv *priv = fi->priv;
283
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100284 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100286 case GPRS_NS2_EV_RX_RESET:
287 ns2_tx_reset_ack(priv->nsvc);
288 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100289 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200290 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
291 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
292 break;
293 }
294 } else {
295 /* we are on the receiving end */
296 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100297 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200298 ns2_tx_reset_ack(priv->nsvc);
299 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
300 0, 0);
301 break;
302 }
303 }
304}
305
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100306static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200307{
308 struct gprs_ns2_vc_priv *priv = fi->priv;
309
Harald Welte4c54f592021-01-30 22:42:15 +0100310 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200311 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100312 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
313 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200314
Alexander Couzens47afc422021-01-17 20:12:46 +0100315 if (priv->om_blocked) {
316 /* we are already blocked after a RESET */
317 if (old_state == GPRS_NS2_ST_RESET) {
318 osmo_timer_del(&fi->timer);
319 } else {
320 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
321 }
322 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200323 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100324 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200325
326 start_test_procedure(priv);
327}
328
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100329static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200330{
331 struct gprs_ns2_vc_priv *priv = fi->priv;
332
Alexander Couzens47afc422021-01-17 20:12:46 +0100333 if (priv->om_blocked) {
334 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100335 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100336 priv->accept_unitdata = false;
337 osmo_timer_del(&fi->timer);
338 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100339 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100340 priv->accept_unitdata = false;
341 ns2_tx_block_ack(priv->nsvc);
342 osmo_timer_del(&fi->timer);
343 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100344 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100345 priv->accept_unitdata = false;
346 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
347 osmo_timer_add(&fi->timer);
348 break;
349 }
350 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200351 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100352 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200353 /* TODO: BLOCK is a UNBLOCK_NACK */
354 ns2_tx_block_ack(priv->nsvc);
355 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100356 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200357 ns2_tx_unblock_ack(priv->nsvc);
358 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100359 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100360 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200361 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
362 0, NS_TOUT_TNS_TEST);
363 break;
364 }
365 } else {
366 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
367 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100368 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100369 ns2_tx_block_ack(priv->nsvc);
370 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100371 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200372 ns2_tx_unblock_ack(priv->nsvc);
373 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
374 0, 0);
375 break;
376 }
377 }
378}
379
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100380static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200381{
382 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100383 struct gprs_ns2_vc *nsvc = priv->nsvc;
384 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200385
Harald Welteb40bf8b2021-01-30 22:43:01 +0100386 if (old_state != GPRS_NS2_ST_UNBLOCKED)
387 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
388
Alexander Couzens47afc422021-01-17 20:12:46 +0100389 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100390 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100391 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200392}
393
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100394static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200395{
396 struct gprs_ns2_vc_priv *priv = fi->priv;
397
398 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100399 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100400 ns2_tx_unblock_ack(priv->nsvc);
401 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100402 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100403 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200404 ns2_tx_block_ack(priv->nsvc);
405 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
406 0, 2);
407 break;
408 }
409}
410
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100411static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200412{
413 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100414 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200415 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
416 break;
417 }
418}
419
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100420static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200421{
422 struct gprs_ns2_vc_priv *priv = fi->priv;
423 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
424
425 priv->alive.mode = NS_TOUT_TNS_TEST;
426 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
427
Alexander Couzensd3e31102021-02-03 11:15:07 +0100428 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200429 priv->N = 0;
430
431 ns2_tx_alive(priv->nsvc);
432 ns2_nse_notify_unblocked(priv->nsvc, false);
433}
434
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100435static void ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200436{
437 start_test_procedure(fi->priv);
438}
439
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100440static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200441 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100442 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100443 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING),
Alexander Couzens6a161492020-07-12 13:45:50 +0200444 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100445 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100446 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200447 },
448 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100449 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200450 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100451 S(GPRS_NS2_ST_BLOCKED) |
452 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200453 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100454 .action = ns2_st_reset,
455 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200456 },
457 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100458 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
459 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200460 .out_state_mask = S(GPRS_NS2_ST_RESET) |
461 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100462 S(GPRS_NS2_ST_BLOCKED) |
463 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200464 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100465 .action = ns2_st_blocked,
466 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 },
468 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100469 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
470 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100471 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100472 S(GPRS_NS2_ST_BLOCKED) |
473 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200474 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100475 .action = ns2_st_unblocked,
476 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200477 },
478
Alexander Couzensd3e31102021-02-03 11:15:07 +0100479 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
480 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100481 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100482 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100483 S(GPRS_NS2_ST_UNBLOCKED) |
484 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100485 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100486 .action = ns2_st_alive,
487 .onenter = ns2_st_alive_onenter,
488 .onleave = ns2_st_alive_onleave,
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 Couzens6a161492020-07-12 13:45:50 +0200690 /* .log_subsys = DNS, "is not constant" */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100691 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 .pre_term = NULL,
693 .log_subsys = DLNS,
694};
695
696/*!
697 * \brief gprs_ns2_vc_fsm_alloc
698 * \param ctx
699 * \param vc
700 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100701 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200702 * \return NULL on error, otherwise the fsm
703 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100704struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100705 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200706{
707 struct osmo_fsm_inst *fi;
708 struct gprs_ns2_vc_priv *priv;
709
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100710 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200711 if (!fi)
712 return fi;
713
714 nsvc->fi = fi;
715 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
716 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100717 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200718
719 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
720
721 return fi;
722}
723
Harald Welte5bef2cc2020-09-18 22:33:24 +0200724/*! Start a NS-VC FSM.
725 * \param nsvc the virtual circuit
726 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100727int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200728{
729 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
730 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100731 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200732 return 0;
733}
734
Daniel Willmanned0c9822020-11-18 14:08:07 +0100735/*! Reset a NS-VC FSM.
736 * \param nsvc the virtual circuit
737 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100738int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100739{
Alexander Couzensf5775432021-01-18 13:49:00 +0100740 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100741}
742
Alexander Couzens47afc422021-01-17 20:12:46 +0100743/*! Block a NS-VC.
744 * \param nsvc the virtual circuit
745 * \return 0 on success; negative on error */
746int ns2_vc_block(struct gprs_ns2_vc *nsvc)
747{
748 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
749}
750
751/*! Unblock a NS-VC.
752 * \param nsvc the virtual circuit
753 * \return 0 on success; negative on error */
754int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
755{
756 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
757}
758
Harald Welte5bef2cc2020-09-18 22:33:24 +0200759/*! entry point for messages from the driver/VL
760 * \param nsvc virtual circuit on which the message was received
761 * \param msg message that was received
762 * \param tp parsed TLVs of the received message
763 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100764int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200765{
766 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
767 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100768 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200769 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100770 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200771
772 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
773 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200774 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
775
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100776 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200777 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100778 rc = ns2_tx_status(nsvc, cause, 0, msg);
779 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200780 }
781 }
782
Alexander Couzens43771df2021-01-20 13:03:03 +0100783 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
784 nsei = tlvp_val16be(tp, NS_IE_NSEI);
785 if (nsei != nsvc->nse->nsei) {
786 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
787 if (nsh->pdu_type == NS_PDUT_RESET)
788 ns2_tx_reset_ack(nsvc);
789
790 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
791 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
792 goto out;
793 }
794 }
795
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100796 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
797 nsvci = tlvp_val16be(tp, NS_IE_VCI);
798 if (nsvci != nsvc->nsvci) {
799 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
800 if (nsh->pdu_type == NS_PDUT_RESET)
801 ns2_tx_reset_ack(nsvc);
802
803 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
804 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
805 goto out;
806 }
807 }
808
Alexander Couzens6a161492020-07-12 13:45:50 +0200809 switch (nsh->pdu_type) {
810 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100811 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200812 break;
813 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100814 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200815 break;
816 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100817 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200818 break;
819 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100820 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200821 break;
822 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100823 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200824 break;
825 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100826 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200827 break;
828 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100829 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200830 break;
831 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100832 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200833 break;
834 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100835 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100836 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100837 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200838 default:
Harald Weltef2949742021-01-20 14:54:14 +0100839 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
840 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200841 return -EINVAL;
842 }
843
Alexander Couzenscce88282020-10-26 00:25:50 +0100844out:
845 msgb_free(msg);
846
847 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200848}
849
Harald Welte5bef2cc2020-09-18 22:33:24 +0200850/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100851int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200852{
853 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
854}
855
856/* initialize osmo_ctx on main tread */
857static __attribute__((constructor)) void on_dso_load_ctx(void)
858{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100859 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200860}