blob: 31cfb51b28e02db4477462cdd9eb494dc3411d4c [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_vc_fsm.c
2 * NS virtual circuit FSM implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
6/* (C) 2020 sysmocom - s.f.m.c. GmbH
7 * Author: Alexander Couzens <lynxis@fe80.eu>
8 *
9 * All Rights Reserved
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
29 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
30 * associated weights. In theory, the BSS then uses this to establish a full mesh
31 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports */
32
33#include <errno.h>
34
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <osmocom/core/fsm.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/rate_ctr.h>
41#include <osmocom/core/socket.h>
42#include <osmocom/core/stat_item.h>
43#include <osmocom/gsm/prim.h>
44#include <osmocom/gsm/tlv.h>
45#include <osmocom/gprs/gprs_msgb.h>
46#include <osmocom/gprs/protocol/gsm_08_16.h>
47
48#include "gprs_ns2_internal.h"
49
50#define S(x) (1 << (x))
51
52#define DNS 10
53
54struct gprs_ns2_vc_priv {
55 struct gprs_ns2_vc *nsvc;
56 /* how often the timer was triggered */
57 int N;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010058 /* The initiator is responsible to UNBLOCK the VC. The BSS is usually the initiator.
59 * It can change during runtime. The side which blocks an unblocked side.*/
Alexander Couzensea01bf22021-01-18 14:01:01 +010060 bool initiator;
Daniel Willmannf5b2e282020-11-18 18:51:25 +010061 bool initiate_block;
62 bool initiate_reset;
Alexander Couzens47afc422021-01-17 20:12:46 +010063 /* if blocked by O&M/vty */
64 bool om_blocked;
65 /* if unitdata is forwarded to the user */
66 bool accept_unitdata;
Alexander Couzens6a161492020-07-12 13:45:50 +020067
68 /* the alive counter is present in all states */
69 struct {
70 struct osmo_timer_list timer;
71 enum ns2_timeout mode;
72 int N;
Alexander Couzensab0e8642021-02-12 02:50:44 +010073 struct timespec timer_started;
Alexander Couzens6a161492020-07-12 13:45:50 +020074 } alive;
75};
76
77
78/* The FSM covers both the VC with RESET/BLOCK and without RESET/BLOCK procedure..
79 *
80 * With RESET/BLOCK, the state should follow:
81 * - UNCONFIGURED -> RESET -> BLOCK -> UNBLOCKED
82 *
83 * Without RESET/BLOCK, the state should follow:
Alexander Couzensd3e31102021-02-03 11:15:07 +010084 * - UNCONFIGURED -> RECOVERY -> UNBLOCKED
Alexander Couzens6a161492020-07-12 13:45:50 +020085 *
86 * The UNBLOCKED and TEST states are used to send ALIVE PDU using the timeout Tns-test and Tns-alive.
87 * UNBLOCKED -> TEST: on expire of Tns-Test, send Alive PDU.
88 * TEST -> UNBLOCKED: on receive of Alive_Ack PDU, go into UNBLOCKED.
89 *
Alexander Couzensd3e31102021-02-03 11:15:07 +010090 * The RECOVERY state is used as intermediate, because a VC is only valid if it received an Alive ACK when
Alexander Couzens6a161492020-07-12 13:45:50 +020091 * not using RESET/BLOCK procedure.
92 */
93
94enum gprs_ns2_vc_state {
95 GPRS_NS2_ST_UNCONFIGURED,
96 GPRS_NS2_ST_RESET,
97 GPRS_NS2_ST_BLOCKED,
98 GPRS_NS2_ST_UNBLOCKED, /* allows sending NS_UNITDATA */
99
Alexander Couzensd3e31102021-02-03 11:15:07 +0100100 GPRS_NS2_ST_RECOVERING, /* only used when not using RESET/BLOCK procedure */
Alexander Couzens6a161492020-07-12 13:45:50 +0200101};
102
103enum gprs_ns2_vc_event {
Alexander Couzensf5775432021-01-18 13:49:00 +0100104 GPRS_NS2_EV_REQ_START,
Alexander Couzens6a161492020-07-12 13:45:50 +0200105
106 /* received messages */
Alexander Couzensf5775432021-01-18 13:49:00 +0100107 GPRS_NS2_EV_RX_RESET,
108 GPRS_NS2_EV_RX_RESET_ACK,
109 GPRS_NS2_EV_RX_UNBLOCK,
110 GPRS_NS2_EV_RX_UNBLOCK_ACK,
111 GPRS_NS2_EV_RX_BLOCK,
112 GPRS_NS2_EV_RX_BLOCK_ACK,
113 GPRS_NS2_EV_RX_ALIVE,
114 GPRS_NS2_EV_RX_ALIVE_ACK,
115 GPRS_NS2_EV_RX_STATUS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200116
Alexander Couzensf5775432021-01-18 13:49:00 +0100117 GPRS_NS2_EV_RX_UNITDATA,
Daniel Willmanned0c9822020-11-18 14:08:07 +0100118
Alexander Couzensf5775432021-01-18 13:49:00 +0100119 GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, /* called via vty for tests */
120 GPRS_NS2_EV_REQ_OM_BLOCK, /* vty cmd: block */
121 GPRS_NS2_EV_REQ_OM_UNBLOCK, /* vty cmd: unblock*/
Alexander Couzens6a161492020-07-12 13:45:50 +0200122};
123
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100124static const struct value_string ns2_vc_event_names[] = {
Harald Welteb339cbb2021-02-05 14:49:21 +0100125 { GPRS_NS2_EV_REQ_START, "REQ-START" },
126 { GPRS_NS2_EV_RX_RESET, "RX-RESET" },
127 { GPRS_NS2_EV_RX_RESET_ACK, "RX-RESET_ACK" },
128 { GPRS_NS2_EV_RX_UNBLOCK, "RX-UNBLOCK" },
129 { GPRS_NS2_EV_RX_UNBLOCK_ACK, "RX-UNBLOCK_ACK" },
130 { GPRS_NS2_EV_RX_BLOCK, "RX-BLOCK" },
131 { GPRS_NS2_EV_RX_BLOCK_ACK, "RX-BLOCK_ACK" },
132 { GPRS_NS2_EV_RX_ALIVE, "RX-ALIVE" },
133 { GPRS_NS2_EV_RX_ALIVE_ACK, "RX-ALIVE_ACK" },
134 { GPRS_NS2_EV_RX_STATUS, "RX-STATUS" },
135 { GPRS_NS2_EV_RX_UNITDATA, "RX-UNITDATA" },
136 { GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, "REQ-FORCE_UNCONFIGURED" },
Alexander Couzens47afc422021-01-17 20:12:46 +0100137 { GPRS_NS2_EV_REQ_OM_BLOCK, "REQ-O&M-BLOCK"},
138 { GPRS_NS2_EV_REQ_OM_UNBLOCK, "REQ-O&M-UNBLOCK"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200139 { 0, NULL }
140};
141
142static inline struct gprs_ns2_inst *ns_inst_from_fi(struct osmo_fsm_inst *fi)
143{
144 struct gprs_ns2_vc_priv *priv = fi->priv;
145 return priv->nsvc->nse->nsi;
146}
147
148static void start_test_procedure(struct gprs_ns2_vc_priv *priv)
149{
150 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
151
152 if (osmo_timer_pending(&priv->alive.timer))
153 return;
154
155 priv->alive.mode = NS_TOUT_TNS_ALIVE;
156 priv->alive.N = 0;
157
Alexander Couzensab0e8642021-02-12 02:50:44 +0100158 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200159 ns2_tx_alive(priv->nsvc);
160 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
161}
162
163static void stop_test_procedure(struct gprs_ns2_vc_priv *priv)
164{
165 osmo_timer_del(&priv->alive.timer);
166}
167
168static int alive_timer_elapsed_ms(struct gprs_ns2_vc_priv *priv)
169{
Alexander Couzensab0e8642021-02-12 02:50:44 +0100170 struct timespec now, elapsed;
Alexander Couzens6a161492020-07-12 13:45:50 +0200171
Alexander Couzensab0e8642021-02-12 02:50:44 +0100172 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0)
173 return 0;
174
175 timespecsub(&now, &priv->alive.timer_started, &elapsed);
176 LOGNSVC(priv->nsvc, LOGL_ERROR, "elapsed: %ld, now: %ld, saved: %ld.\n",
177 elapsed.tv_sec, now.tv_sec, priv->alive.timer_started.tv_sec);
178 return elapsed.tv_sec * 1000 + (elapsed.tv_nsec / 1000000);
Alexander Couzens6a161492020-07-12 13:45:50 +0200179}
180
181static void recv_test_procedure(struct osmo_fsm_inst *fi)
182{
183 struct gprs_ns2_vc_priv *priv = fi->priv;
184 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
185 struct gprs_ns2_vc *nsvc = priv->nsvc;
186
187 /* ignoring ACKs without sending an ALIVE */
188 if (priv->alive.mode != NS_TOUT_TNS_ALIVE)
189 return;
190
191 priv->alive.mode = NS_TOUT_TNS_TEST;
192 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
193 osmo_stat_item_set(nsvc->statg->items[NS_STAT_ALIVE_DELAY],
194 alive_timer_elapsed_ms(priv));
195}
196
197
198static void alive_timeout_handler(void *data)
199{
200 struct osmo_fsm_inst *fi = data;
201 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
202 struct gprs_ns2_vc_priv *priv = fi->priv;
203
204 switch (priv->alive.mode) {
205 case NS_TOUT_TNS_TEST:
206 priv->alive.mode = NS_TOUT_TNS_ALIVE;
Alexander Couzens2f8f7b62021-02-03 11:04:22 +0100207 priv->alive.N = 0;
Alexander Couzensfa4121d2021-02-12 02:54:46 +0100208 osmo_clock_gettime(CLOCK_MONOTONIC, &priv->alive.timer_started);
Alexander Couzens6a161492020-07-12 13:45:50 +0200209 ns2_tx_alive(priv->nsvc);
210 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
211 break;
212 case NS_TOUT_TNS_ALIVE:
Harald Welte4c54f592021-01-30 22:42:15 +0100213 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200214 priv->alive.N++;
215
216 if (priv->alive.N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
217 /* retransmission */
218 ns2_tx_alive(priv->nsvc);
219 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
220 } else {
221 /* lost connection */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100222 if (priv->nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200223 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
224 } else {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100225 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, nsi->timeout[NS_TOUT_TNS_ALIVE], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200226 }
227 }
228 break;
229 default:
230 break;
231 }
232}
233
Harald Welte6a9ec422021-01-31 18:56:29 +0100234
235static void ns2_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
236{
237 stop_test_procedure(fi->priv);
238}
239
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100240static void ns2_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200241{
242 struct gprs_ns2_vc_priv *priv = fi->priv;
243 struct gprs_ns2_inst *nsi = priv->nsvc->nse->nsi;
244
Alexander Couzensea01bf22021-01-18 14:01:01 +0100245 priv->initiate_reset = priv->initiate_block = priv->initiator;
246 priv->om_blocked = false;
247
Alexander Couzens6a161492020-07-12 13:45:50 +0200248 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100249 case GPRS_NS2_EV_REQ_START:
Alexander Couzens6a161492020-07-12 13:45:50 +0200250 switch (priv->nsvc->mode) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100251 case GPRS_NS2_VC_MODE_ALIVE:
Alexander Couzensd3e31102021-02-03 11:15:07 +0100252 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 +0200253 break;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100254 case GPRS_NS2_VC_MODE_BLOCKRESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
256 break;
257 }
258
259 break;
260 default:
261 OSMO_ASSERT(0);
262 }
263}
264
265
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100266static void ns2_st_reset_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200267{
268 struct gprs_ns2_vc_priv *priv = fi->priv;
269
270 if (old_state != GPRS_NS2_ST_RESET)
271 priv->N = 0;
272
Alexander Couzens47afc422021-01-17 20:12:46 +0100273 priv->accept_unitdata = false;
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100274 if (priv->initiate_reset)
Alexander Couzens6a161492020-07-12 13:45:50 +0200275 ns2_tx_reset(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
276
277 stop_test_procedure(priv);
278 ns2_nse_notify_unblocked(priv->nsvc, false);
279}
280
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100281static void ns2_st_reset(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200282{
283 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
284 struct gprs_ns2_vc_priv *priv = fi->priv;
285
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100286 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 switch (event) {
Alexander Couzens273f0632021-01-20 13:11:26 +0100288 case GPRS_NS2_EV_RX_RESET:
289 ns2_tx_reset_ack(priv->nsvc);
290 /* fall-through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100291 case GPRS_NS2_EV_RX_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200292 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
293 nsi->timeout[NS_TOUT_TNS_BLOCK], NS_TOUT_TNS_BLOCK);
294 break;
295 }
296 } else {
297 /* we are on the receiving end */
298 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100299 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200300 ns2_tx_reset_ack(priv->nsvc);
301 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
302 0, 0);
303 break;
304 }
305 }
306}
307
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100308static void ns2_st_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200309{
310 struct gprs_ns2_vc_priv *priv = fi->priv;
311
Harald Welte4c54f592021-01-30 22:42:15 +0100312 if (old_state != GPRS_NS2_ST_BLOCKED) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200313 priv->N = 0;
Harald Welte4c54f592021-01-30 22:42:15 +0100314 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
315 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200316
Alexander Couzens47afc422021-01-17 20:12:46 +0100317 if (priv->om_blocked) {
318 /* we are already blocked after a RESET */
319 if (old_state == GPRS_NS2_ST_RESET) {
320 osmo_timer_del(&fi->timer);
321 } else {
322 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
323 }
324 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200325 ns2_tx_unblock(priv->nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100326 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200327
328 start_test_procedure(priv);
329}
330
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100331static void ns2_st_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200332{
333 struct gprs_ns2_vc_priv *priv = fi->priv;
334
Alexander Couzens47afc422021-01-17 20:12:46 +0100335 if (priv->om_blocked) {
336 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100337 case GPRS_NS2_EV_RX_BLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100338 priv->accept_unitdata = false;
339 osmo_timer_del(&fi->timer);
340 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100341 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100342 priv->accept_unitdata = false;
343 ns2_tx_block_ack(priv->nsvc);
344 osmo_timer_del(&fi->timer);
345 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100346 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100347 priv->accept_unitdata = false;
348 ns2_tx_block(priv->nsvc, NS_CAUSE_OM_INTERVENTION);
349 osmo_timer_add(&fi->timer);
350 break;
351 }
352 } else if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200353 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100354 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 /* TODO: BLOCK is a UNBLOCK_NACK */
356 ns2_tx_block_ack(priv->nsvc);
357 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100358 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200359 ns2_tx_unblock_ack(priv->nsvc);
360 /* fall through */
Alexander Couzensf5775432021-01-18 13:49:00 +0100361 case GPRS_NS2_EV_RX_UNBLOCK_ACK:
Alexander Couzens47afc422021-01-17 20:12:46 +0100362 priv->accept_unitdata = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200363 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
364 0, NS_TOUT_TNS_TEST);
365 break;
366 }
367 } else {
368 /* we are on the receiving end. The initiator who sent RESET is responsible to UNBLOCK! */
369 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100370 case GPRS_NS2_EV_RX_BLOCK:
Alexander Couzens856b94c2021-01-19 19:42:17 +0100371 ns2_tx_block_ack(priv->nsvc);
372 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100373 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200374 ns2_tx_unblock_ack(priv->nsvc);
375 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED,
376 0, 0);
377 break;
378 }
379 }
380}
381
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100382static void ns2_st_unblocked_on_enter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200383{
384 struct gprs_ns2_vc_priv *priv = fi->priv;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100385 struct gprs_ns2_vc *nsvc = priv->nsvc;
386 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200387
Harald Welteb40bf8b2021-01-30 22:43:01 +0100388 if (old_state != GPRS_NS2_ST_UNBLOCKED)
389 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_UNBLOCKED]);
390
Alexander Couzens47afc422021-01-17 20:12:46 +0100391 priv->accept_unitdata = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100392 ns2_nse_notify_unblocked(nsvc, true);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100393 ns2_prim_status_ind(nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_RECOVERY);
Alexander Couzens6a161492020-07-12 13:45:50 +0200394}
395
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100396static void ns2_st_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200397{
398 struct gprs_ns2_vc_priv *priv = fi->priv;
399
400 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100401 case GPRS_NS2_EV_RX_UNBLOCK:
Alexander Couzensc4b74622021-01-10 20:44:26 +0100402 ns2_tx_unblock_ack(priv->nsvc);
403 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100404 case GPRS_NS2_EV_RX_BLOCK:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100405 priv->initiate_block = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200406 ns2_tx_block_ack(priv->nsvc);
407 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED,
408 0, 2);
409 break;
410 }
411}
412
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100413static void ns2_st_alive(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200414{
415 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100416 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200417 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNBLOCKED, 0, 0);
418 break;
419 }
420}
421
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100422static void ns2_st_alive_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200423{
424 struct gprs_ns2_vc_priv *priv = fi->priv;
425 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
426
427 priv->alive.mode = NS_TOUT_TNS_TEST;
428 osmo_timer_schedule(&priv->alive.timer, nsi->timeout[NS_TOUT_TNS_TEST], 0);
429
Alexander Couzensd3e31102021-02-03 11:15:07 +0100430 if (old_state != GPRS_NS2_ST_RECOVERING)
Alexander Couzens6a161492020-07-12 13:45:50 +0200431 priv->N = 0;
432
433 ns2_tx_alive(priv->nsvc);
434 ns2_nse_notify_unblocked(priv->nsvc, false);
435}
436
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100437static void ns2_st_alive_onleave(struct osmo_fsm_inst *fi, uint32_t next_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200438{
439 start_test_procedure(fi->priv);
440}
441
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100442static const struct osmo_fsm_state ns2_vc_states[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200443 [GPRS_NS2_ST_UNCONFIGURED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100444 .in_event_mask = S(GPRS_NS2_EV_REQ_START),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100445 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING),
Alexander Couzens6a161492020-07-12 13:45:50 +0200446 .name = "UNCONFIGURED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100447 .action = ns2_st_unconfigured,
Harald Welte6a9ec422021-01-31 18:56:29 +0100448 .onenter = ns2_st_unconfigured_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200449 },
450 [GPRS_NS2_ST_RESET] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100451 .in_event_mask = S(GPRS_NS2_EV_RX_RESET_ACK) | S(GPRS_NS2_EV_RX_RESET),
Alexander Couzens6a161492020-07-12 13:45:50 +0200452 .out_state_mask = S(GPRS_NS2_ST_RESET) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100453 S(GPRS_NS2_ST_BLOCKED) |
454 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200455 .name = "RESET",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100456 .action = ns2_st_reset,
457 .onenter = ns2_st_reset_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200458 },
459 [GPRS_NS2_ST_BLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100460 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_BLOCK_ACK) |
461 S(GPRS_NS2_EV_RX_UNBLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +0200462 .out_state_mask = S(GPRS_NS2_ST_RESET) |
463 S(GPRS_NS2_ST_UNBLOCKED) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100464 S(GPRS_NS2_ST_BLOCKED) |
465 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200466 .name = "BLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100467 .action = ns2_st_blocked,
468 .onenter = ns2_st_blocked_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200469 },
470 [GPRS_NS2_ST_UNBLOCKED] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100471 .in_event_mask = S(GPRS_NS2_EV_RX_BLOCK) | S(GPRS_NS2_EV_RX_UNBLOCK_ACK) |
472 S(GPRS_NS2_EV_RX_UNBLOCK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100473 .out_state_mask = S(GPRS_NS2_ST_RESET) | S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100474 S(GPRS_NS2_ST_BLOCKED) |
475 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 .name = "UNBLOCKED",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100477 .action = ns2_st_unblocked,
478 .onenter = ns2_st_unblocked_on_enter,
Alexander Couzens6a161492020-07-12 13:45:50 +0200479 },
480
Alexander Couzensd3e31102021-02-03 11:15:07 +0100481 /* ST_RECOVERING is only used on VC without RESET/BLOCK */
482 [GPRS_NS2_ST_RECOVERING] = {
Alexander Couzensf5775432021-01-18 13:49:00 +0100483 .in_event_mask = S(GPRS_NS2_EV_RX_ALIVE_ACK),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100484 .out_state_mask = S(GPRS_NS2_ST_RECOVERING) |
Daniel Willmanned0c9822020-11-18 14:08:07 +0100485 S(GPRS_NS2_ST_UNBLOCKED) |
486 S(GPRS_NS2_ST_UNCONFIGURED),
Alexander Couzensd3e31102021-02-03 11:15:07 +0100487 .name = "RECOVERING",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100488 .action = ns2_st_alive,
489 .onenter = ns2_st_alive_onenter,
490 .onleave = ns2_st_alive_onleave,
Alexander Couzens6a161492020-07-12 13:45:50 +0200491 },
492};
493
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100494static int ns2_vc_fsm_timer_cb(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200495{
496 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
497 struct gprs_ns2_vc_priv *priv = fi->priv;
498
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100499 switch (fi->state) {
500 case GPRS_NS2_ST_RESET:
501 if (priv->initiate_reset) {
Harald Welte4c54f592021-01-30 22:42:15 +0100502 rate_ctr_inc(&priv->nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200503 priv->N++;
504 if (priv->N <= nsi->timeout[NS_TOUT_TNS_RESET_RETRIES]) {
505 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
506 } else {
507 priv->N = 0;
508 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
509 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100510 }
511 break;
512 case GPRS_NS2_ST_BLOCKED:
513 if (priv->initiate_block) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200514 priv->N++;
Alexander Couzens47afc422021-01-17 20:12:46 +0100515 if (priv->om_blocked) {
516 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
517 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
518 } else {
519 /* 7.2 stop accepting data when BLOCK PDU not responded */
520 priv->accept_unitdata = false;
521 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200522 } else {
Alexander Couzens47afc422021-01-17 20:12:46 +0100523 if (priv->N <= nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES]) {
524 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
525 } else {
526 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], 0);
527 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200528 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100529 }
530 break;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100531 case GPRS_NS2_ST_RECOVERING:
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100532 if (priv->initiate_reset) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200533 priv->N++;
534 if (priv->N <= nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Alexander Couzensd3e31102021-02-03 11:15:07 +0100535 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200536 } else {
537 priv->N = 0;
Alexander Couzensd3e31102021-02-03 11:15:07 +0100538 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RECOVERING, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200539 }
540 break;
541 }
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100542 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200543 }
544 return 0;
545}
546
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100547static void ns2_recv_unitdata(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200548 struct msgb *msg)
549{
550 struct gprs_ns2_vc_priv *priv = fi->priv;
551 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
552 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
553 struct osmo_gprs_ns2_prim nsp = {};
554 uint16_t bvci;
555
Alexander Couzenscce88282020-10-26 00:25:50 +0100556 if (msgb_l2len(msg) < sizeof(*nsh) + 3) {
557 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200558 return;
Alexander Couzenscce88282020-10-26 00:25:50 +0100559 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200560
561 /* TODO: 7.1: For an IP sub-network, an NS-UNITDATA PDU
562 * for a PTP BVC may indicate a request to change the IP endpoint
563 * and/or a response to a change in the IP endpoint. */
564
565 /* TODO: nsh->data[0] -> C/R only valid in IP SNS */
566 bvci = nsh->data[1] << 8 | nsh->data[2];
567
Alexander Couzens89acdef2020-09-23 18:22:31 +0200568 msg->l3h = &nsh->data[3];
569 nsp.bvci = bvci;
570 nsp.nsei = priv->nsvc->nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200571
Alexander Couzensc1cd3332020-09-23 23:24:02 +0200572 /* 10.3.9 NS SDU Control Bits */
573 if (nsh->data[0] & 0x1)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100574 nsp.u.unitdata.change = GPRS_NS2_ENDPOINT_REQUEST_CHANGE;
Alexander Couzens6a161492020-07-12 13:45:50 +0200575
Alexander Couzens138b96f2021-01-25 16:23:29 +0100576 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens6a161492020-07-12 13:45:50 +0200577 PRIM_OP_INDICATION, msg);
578 nsi->cb(&nsp.oph, nsi->cb_data);
579}
580
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100581static void ns2_vc_fsm_allstate_action(struct osmo_fsm_inst *fi,
Alexander Couzens6a161492020-07-12 13:45:50 +0200582 uint32_t event,
583 void *data)
584{
585 struct gprs_ns2_vc_priv *priv = fi->priv;
586 struct gprs_ns2_inst *nsi = ns_inst_from_fi(fi);
Alexander Couzenscce88282020-10-26 00:25:50 +0100587 struct msgb *msg = data;
Alexander Couzens6a161492020-07-12 13:45:50 +0200588
589 switch (event) {
Alexander Couzensf5775432021-01-18 13:49:00 +0100590 case GPRS_NS2_EV_RX_RESET:
Alexander Couzens138b96f2021-01-25 16:23:29 +0100591 if (priv->nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzens6a161492020-07-12 13:45:50 +0200592 break;
593
594 /* move the FSM into reset */
595 if (fi->state != GPRS_NS2_ST_RESET) {
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100596 priv->initiate_reset = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200597 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_RESET, nsi->timeout[NS_TOUT_TNS_RESET], NS_TOUT_TNS_RESET);
598 }
599 /* pass the event down into FSM action */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100600 ns2_st_reset(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200601 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100602 case GPRS_NS2_EV_RX_ALIVE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 switch (fi->state) {
604 case GPRS_NS2_ST_UNCONFIGURED:
605 case GPRS_NS2_ST_RESET:
606 /* ignore ALIVE */
607 break;
608 default:
609 ns2_tx_alive_ack(priv->nsvc);
610 }
611 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100612 case GPRS_NS2_EV_RX_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200613 /* for VCs without RESET/BLOCK/UNBLOCK, the connections comes after ALIVE_ACK unblocked */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100614 if (fi->state == GPRS_NS2_ST_RECOVERING)
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100615 ns2_st_alive(fi, event, data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200616 else
617 recv_test_procedure(fi);
618 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100619 case GPRS_NS2_EV_RX_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100620 /* UNITDATA has to handle the release of msg.
621 * If send upwards (gprs_ns2_recv_unitdata) it must NOT free
622 * the msg, the upper layer has to do it.
623 * Otherwise the msg must be freed.
624 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200625 switch (fi->state) {
626 case GPRS_NS2_ST_BLOCKED:
627 /* 7.2.1: the BLOCKED_ACK might be lost */
Alexander Couzens47afc422021-01-17 20:12:46 +0100628 if (priv->accept_unitdata) {
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100629 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100630 return;
631 }
632
633 ns2_tx_status(priv->nsvc,
634 NS_CAUSE_NSVC_BLOCKED,
635 0, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200636 break;
637 /* ALIVE can receive UNITDATA if the ALIVE_ACK is lost */
Alexander Couzensd3e31102021-02-03 11:15:07 +0100638 case GPRS_NS2_ST_RECOVERING:
Alexander Couzens6a161492020-07-12 13:45:50 +0200639 case GPRS_NS2_ST_UNBLOCKED:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100640 ns2_recv_unitdata(fi, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100641 return;
Alexander Couzens6a161492020-07-12 13:45:50 +0200642 }
Alexander Couzenscce88282020-10-26 00:25:50 +0100643
644 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200645 break;
Alexander Couzensf5775432021-01-18 13:49:00 +0100646 case GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED:
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100647 if (fi->state != GPRS_NS2_ST_UNCONFIGURED) {
648 /* Force the NSVC back to its initial state */
649 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_UNCONFIGURED, 0, 0);
Alexander Couzens6f3b7382020-12-21 15:57:04 +0100650 return;
651 }
Daniel Willmanned0c9822020-11-18 14:08:07 +0100652 break;
Alexander Couzens47afc422021-01-17 20:12:46 +0100653 case GPRS_NS2_EV_REQ_OM_BLOCK:
654 /* vty cmd: block */
655 priv->initiate_block = true;
656 priv->om_blocked = true;
657 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
658 break;
659 case GPRS_NS2_EV_REQ_OM_UNBLOCK:
660 /* vty cmd: unblock*/
661 if (!priv->om_blocked)
662 return;
663 priv->om_blocked = false;
664 if (fi->state == GPRS_NS2_ST_BLOCKED)
665 osmo_fsm_inst_state_chg(fi, GPRS_NS2_ST_BLOCKED, nsi->timeout[NS_TOUT_TNS_BLOCK], 0);
666 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200667 }
668}
669
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100670static void ns2_vc_fsm_clean(struct osmo_fsm_inst *fi,
Alexander Couzens0346b642020-10-27 13:05:56 +0100671 enum osmo_fsm_term_cause cause)
672{
673 struct gprs_ns2_vc_priv *priv = fi->priv;
674
675 osmo_timer_del(&priv->alive.timer);
676}
677
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100678static struct osmo_fsm ns2_vc_fsm = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200679 .name = "GPRS-NS2-VC",
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100680 .states = ns2_vc_states,
681 .num_states = ARRAY_SIZE(ns2_vc_states),
Alexander Couzensf5775432021-01-18 13:49:00 +0100682 .allstate_event_mask = S(GPRS_NS2_EV_RX_UNITDATA) |
683 S(GPRS_NS2_EV_RX_RESET) |
684 S(GPRS_NS2_EV_RX_ALIVE) |
685 S(GPRS_NS2_EV_RX_ALIVE_ACK) |
686 S(GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED) |
Alexander Couzens47afc422021-01-17 20:12:46 +0100687 S(GPRS_NS2_EV_REQ_OM_BLOCK) |
688 S(GPRS_NS2_EV_REQ_OM_UNBLOCK),
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100689 .allstate_action = ns2_vc_fsm_allstate_action,
690 .cleanup = ns2_vc_fsm_clean,
691 .timer_cb = ns2_vc_fsm_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 /* .log_subsys = DNS, "is not constant" */
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100693 .event_names = ns2_vc_event_names,
Alexander Couzens6a161492020-07-12 13:45:50 +0200694 .pre_term = NULL,
695 .log_subsys = DLNS,
696};
697
698/*!
699 * \brief gprs_ns2_vc_fsm_alloc
700 * \param ctx
701 * \param vc
702 * \param id a char representation of the virtual curcuit
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100703 * \param initiator initiator is the site which starts the connection. Usually the BSS.
Alexander Couzens6a161492020-07-12 13:45:50 +0200704 * \return NULL on error, otherwise the fsm
705 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100706struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Daniel Willmannf5b2e282020-11-18 18:51:25 +0100707 const char *id, bool initiator)
Alexander Couzens6a161492020-07-12 13:45:50 +0200708{
709 struct osmo_fsm_inst *fi;
710 struct gprs_ns2_vc_priv *priv;
711
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100712 fi = osmo_fsm_inst_alloc(&ns2_vc_fsm, nsvc, NULL, LOGL_DEBUG, id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200713 if (!fi)
714 return fi;
715
716 nsvc->fi = fi;
717 priv = fi->priv = talloc_zero(fi, struct gprs_ns2_vc_priv);
718 priv->nsvc = nsvc;
Alexander Couzensea01bf22021-01-18 14:01:01 +0100719 priv->initiator = initiator;
Alexander Couzens6a161492020-07-12 13:45:50 +0200720
721 osmo_timer_setup(&priv->alive.timer, alive_timeout_handler, fi);
722
723 return fi;
724}
725
Harald Welte5bef2cc2020-09-18 22:33:24 +0200726/*! Start a NS-VC FSM.
727 * \param nsvc the virtual circuit
728 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100729int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200730{
731 /* allows to call this function even for started nsvc by gprs_ns2_start_alive_all_nsvcs */
732 if (nsvc->fi->state == GPRS_NS2_ST_UNCONFIGURED)
Alexander Couzensf5775432021-01-18 13:49:00 +0100733 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_START, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200734 return 0;
735}
736
Daniel Willmanned0c9822020-11-18 14:08:07 +0100737/*! Reset a NS-VC FSM.
738 * \param nsvc the virtual circuit
739 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100740int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc)
Daniel Willmanned0c9822020-11-18 14:08:07 +0100741{
Alexander Couzensf5775432021-01-18 13:49:00 +0100742 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_FORCE_UNCONFIGURED, NULL);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100743}
744
Alexander Couzens47afc422021-01-17 20:12:46 +0100745/*! Block a NS-VC.
746 * \param nsvc the virtual circuit
747 * \return 0 on success; negative on error */
748int ns2_vc_block(struct gprs_ns2_vc *nsvc)
749{
750 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_BLOCK, NULL);
751}
752
753/*! Unblock a NS-VC.
754 * \param nsvc the virtual circuit
755 * \return 0 on success; negative on error */
756int ns2_vc_unblock(struct gprs_ns2_vc *nsvc)
757{
758 return osmo_fsm_inst_dispatch(nsvc->fi, GPRS_NS2_EV_REQ_OM_UNBLOCK, NULL);
759}
760
Harald Welte5bef2cc2020-09-18 22:33:24 +0200761/*! entry point for messages from the driver/VL
762 * \param nsvc virtual circuit on which the message was received
763 * \param msg message that was received
764 * \param tp parsed TLVs of the received message
765 * \return 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100766int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200767{
768 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
769 struct osmo_fsm_inst *fi = nsvc->fi;
Alexander Couzenscce88282020-10-26 00:25:50 +0100770 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200771 uint8_t cause;
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100772 uint16_t nsei, nsvci;
Alexander Couzens6a161492020-07-12 13:45:50 +0200773
774 /* TODO: 7.2: on UNBLOCK/BLOCK: check if NS-VCI is correct,
775 * if not answer STATUS with "NS-VC unknown" */
Alexander Couzens6a161492020-07-12 13:45:50 +0200776 /* TODO: handle BLOCK/UNBLOCK/ALIVE with different VCI */
777
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100778 if (ns2_validate(nsvc, nsh->pdu_type, msg, tp, &cause)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200779 if (nsh->pdu_type != NS_PDUT_STATUS) {
Alexander Couzenscce88282020-10-26 00:25:50 +0100780 rc = ns2_tx_status(nsvc, cause, 0, msg);
781 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200782 }
783 }
784
Alexander Couzens43771df2021-01-20 13:03:03 +0100785 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
786 nsei = tlvp_val16be(tp, NS_IE_NSEI);
787 if (nsei != nsvc->nse->nsei) {
788 /* 48.016 § 7.3.1 send, RESET_ACK to wrong NSVCI + ignore */
789 if (nsh->pdu_type == NS_PDUT_RESET)
790 ns2_tx_reset_ack(nsvc);
791
792 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSEI=%05u. Ignoring PDU.\n",
793 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsei);
794 goto out;
795 }
796 }
797
Alexander Couzens3f576ab2021-01-20 14:50:31 +0100798 if (nsvc->nsvci_is_valid && TLVP_PRESENT(tp, NS_IE_VCI)) {
799 nsvci = tlvp_val16be(tp, NS_IE_VCI);
800 if (nsvci != nsvc->nsvci) {
801 /* 48.016 § 7.3.1 send RESET_ACK to wrong NSVCI + ignore */
802 if (nsh->pdu_type == NS_PDUT_RESET)
803 ns2_tx_reset_ack(nsvc);
804
805 LOGNSVC(nsvc, LOGL_ERROR, "Rx %s with wrong NSVCI=%05u. Ignoring PDU.\n",
806 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), nsvci);
807 goto out;
808 }
809 }
810
Alexander Couzens6a161492020-07-12 13:45:50 +0200811 switch (nsh->pdu_type) {
812 case NS_PDUT_RESET:
Alexander Couzensf5775432021-01-18 13:49:00 +0100813 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200814 break;
815 case NS_PDUT_RESET_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100816 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_RESET_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200817 break;
818 case NS_PDUT_BLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100819 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200820 break;
821 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100822 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_BLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200823 break;
824 case NS_PDUT_UNBLOCK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100825 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200826 break;
827 case NS_PDUT_UNBLOCK_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100828 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNBLOCK_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200829 break;
830 case NS_PDUT_ALIVE:
Alexander Couzensf5775432021-01-18 13:49:00 +0100831 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200832 break;
833 case NS_PDUT_ALIVE_ACK:
Alexander Couzensf5775432021-01-18 13:49:00 +0100834 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_ALIVE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200835 break;
836 case NS_PDUT_UNITDATA:
Alexander Couzenscce88282020-10-26 00:25:50 +0100837 /* UNITDATA have to free msg because it might send the msg layer upwards */
Alexander Couzensf5775432021-01-18 13:49:00 +0100838 osmo_fsm_inst_dispatch(fi, GPRS_NS2_EV_RX_UNITDATA, msg);
Alexander Couzenscce88282020-10-26 00:25:50 +0100839 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200840 default:
Harald Weltef2949742021-01-20 14:54:14 +0100841 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown NS PDU type %s\n", nsvc->nse->nsei,
842 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +0200843 return -EINVAL;
844 }
845
Alexander Couzenscce88282020-10-26 00:25:50 +0100846out:
847 msgb_free(msg);
848
849 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200850}
851
Harald Welte5bef2cc2020-09-18 22:33:24 +0200852/*! is the given NS-VC unblocked? */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100853int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200854{
855 return (nsvc->fi->state == GPRS_NS2_ST_UNBLOCKED);
856}
857
858/* initialize osmo_ctx on main tread */
859static __attribute__((constructor)) void on_dso_load_ctx(void)
860{
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100861 OSMO_ASSERT(osmo_fsm_register(&ns2_vc_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200862}