blob: ce9079d578c6e760cbcd384c039d79f3179be2a6 [file] [log] [blame]
Harald Welte17a892f2020-12-07 21:39:03 +01001/* BSSGP per-BVC Finite State Machine */
2
3/* (C) 2020 Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <stdio.h>
26
27#include <osmocom/core/fsm.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/core/tdef.h>
31
32#include <osmocom/gsm/gsm48.h>
33#include <osmocom/gsm/tlv.h>
34
35#include <osmocom/gprs/gprs_msgb.h>
36#include <osmocom/gprs/gprs_bssgp.h>
37#include <osmocom/gprs/gprs_bssgp2.h>
38#include <osmocom/gprs/bssgp_bvc_fsm.h>
39
40#include "common_vty.h"
41
42#define S(x) (1 << (x))
43
44/* TODO: Those are not made cofnigurable via a VTY yet */
45struct osmo_tdef bssgp_bvc_fsm_tdefs[] = {
46 {
47 .T = 1,
48 .default_val = 5,
49 .min_val = 1,
50 .max_val = 30,
51 .unit = OSMO_TDEF_S,
52 .desc = "Guards the BSSGP BVC (un)blocking procedure",
53 }, {
54 .T = 2,
55 .default_val = 10,
56 .min_val = 1,
57 .max_val = 120,
58 .unit = OSMO_TDEF_S,
59 .desc = "Guards the BSSGP BVC reset procedure",
60 }, {
61 .T = 3,
62 .default_val = 500,
63 .min_val = 100,
64 .max_val = 10000,
65 .unit = OSMO_TDEF_MS,
66 .desc = "Guards the BSSGP SUSPEND procedure",
67 }, {
68 .T = 4,
69 .default_val = 500,
70 .min_val = 100,
71 .max_val = 10000,
72 .unit = OSMO_TDEF_MS,
73 .desc = "Guards the BSSGP RESUME procedure",
74 }, {
75 .T = 5,
76 .default_val = 15,
77 .min_val = 1,
78 .max_val = 30,
79 .unit = OSMO_TDEF_S,
80 .desc = "Guards the BSSGP Radio Access Capability Update procedure",
81 },
82 {}
83};
84
85#define T1 1
86#define T2 2
87
88/* We cannot use osmo_tdef_fsm_* as it makes hard-coded assumptions that
89 * each new/target state will always use the same timer and timeout - or
90 * a timeout at all */
91#define T1_SECS osmo_tdef_get(bssgp_bvc_fsm_tdefs, 1, OSMO_TDEF_S, 5)
92#define T2_SECS osmo_tdef_get(bssgp_bvc_fsm_tdefs, 2, OSMO_TDEF_S, 10)
93
94/* forward declaration */
95static struct osmo_fsm bssgp_bvc_fsm;
96
97static const struct value_string ptp_bvc_event_names[] = {
98 { BSSGP_BVCFSM_E_RX_BLOCK, "RX-BVC-BLOCK" },
99 { BSSGP_BVCFSM_E_RX_BLOCK_ACK, "RX-BVC-BLOCK-ACK" },
100 { BSSGP_BVCFSM_E_RX_UNBLOCK, "RX-BVC-UNBLOCK" },
101 { BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, "RX-BVC-UNBLOCK-ACK" },
102 { BSSGP_BVCFSM_E_RX_RESET, "RX-BVC-RESET" },
103 { BSSGP_BVCFSM_E_RX_RESET_ACK, "RX-BVC-RESET-ACK" },
Harald Welte1fcfce82020-12-08 21:15:45 +0100104 { BSSGP_BVCFSM_E_RX_FC_BVC, "RX-FLOW-CONTROL-BVC" },
105 { BSSGP_BVCFSM_E_RX_FC_BVC_ACK, "RX-FLOW-CONTROL-BVC-ACK" },
Harald Welte17a892f2020-12-07 21:39:03 +0100106 { BSSGP_BVCFSM_E_REQ_BLOCK, "REQ-BLOCK" },
107 { BSSGP_BVCFSM_E_REQ_UNBLOCK, "REQ-UNBLOCK" },
108 { BSSGP_BVCFSM_E_REQ_RESET, "REQ-RESET" },
Harald Welte1fcfce82020-12-08 21:15:45 +0100109 { BSSGP_BVCFSM_E_REQ_FC_BVC, "REQ-FLOW-CONTROL-BVC" },
Harald Welte17a892f2020-12-07 21:39:03 +0100110 { 0, NULL }
111};
112
113struct bvc_fsm_priv {
114 /* NS-instance; defining the scope for NSEI below */
115 struct gprs_ns2_inst *nsi;
116
117 /* NSEI of the underlying NS Entity */
118 uint16_t nsei;
Daniel Willmann1ff86f72021-01-25 17:02:25 +0100119 /* Maximum size of the BSSGP PDU */
120 uint16_t max_pdu_len;
Harald Welte17a892f2020-12-07 21:39:03 +0100121
122 /* BVCI of this BVC */
123 uint16_t bvci;
124
125 /* are we the SGSN (true) or the BSS (false) */
126 bool role_sgsn;
127
128 /* BSS side: are we locally marked blocked? */
129 bool locally_blocked;
130 uint8_t block_cause;
131
132 /* cause value of the last outbound BVC-RESET (for re-transmissions) */
133 uint8_t last_reset_cause;
134
135 struct {
136 /* Bit 0..7: Features; Bit 8..15: Extended Features */
137 uint32_t advertised;
138 uint32_t received;
139 uint32_t negotiated;
Harald Welte1fcfce82020-12-08 21:15:45 +0100140 /* only used if BSSGP_XFEAT_GBIT is negotiated */
141 enum bssgp_fc_granularity fc_granularity;
Harald Welte17a892f2020-12-07 21:39:03 +0100142 } features;
143
144 /* Cell Identification used by BSS when
145 * transmitting BVC-RESET / BVC-RESET-ACK, or those received
146 * from BSS in SGSN role */
147 struct gprs_ra_id ra_id;
148 uint16_t cell_id;
149
150 /* call-backs provided by the user */
151 const struct bssgp_bvc_fsm_ops *ops;
152 /* private data pointer passed to each call-back invocation */
153 void *ops_priv;
154};
155
156static int fi_tx_ptp(struct osmo_fsm_inst *fi, struct msgb *msg)
157{
158 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
159 struct bvc_fsm_priv *bfp = fi->priv;
160
161 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
162
163 LOGPFSM(fi, "Tx BSSGP %s\n", osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type));
164
165 return bssgp2_nsi_tx_ptp(bfp->nsi, bfp->nsei, bfp->bvci, msg, 0);
166}
167
168static int fi_tx_sig(struct osmo_fsm_inst *fi, struct msgb *msg)
169{
170 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
171 struct bvc_fsm_priv *bfp = fi->priv;
172
173 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
174
175 LOGPFSM(fi, "Tx BSSGP %s\n", osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type));
176
177 return bssgp2_nsi_tx_sig(bfp->nsi, bfp->nsei, msg, 0);
178}
179
180/* helper function to transmit BVC-RESET with right combination of conditional/optional IEs */
181static void _tx_bvc_reset(struct osmo_fsm_inst *fi, uint8_t cause)
182{
183 struct bvc_fsm_priv *bfp = fi->priv;
184 const uint8_t *features = NULL;
185 const uint8_t *features_ext = NULL;
186 uint8_t _features[2] = {
187 (bfp->features.advertised >> 0) & 0xff,
188 (bfp->features.advertised >> 8) & 0xff,
189 };
190 struct msgb *tx;
191
192 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
193
194 /* transmit BVC-RESET to peer; RA-ID only present for PTP from BSS */
195 if (bfp->bvci == 0) {
196 features = &_features[0];
197 features_ext = &_features[1];
198 }
199 tx = bssgp2_enc_bvc_reset(bfp->bvci, cause,
200 bfp->bvci && !bfp->role_sgsn ? &bfp->ra_id : NULL,
201 bfp->cell_id, features, features_ext);
202 fi_tx_sig(fi, tx);
203}
204
205/* helper function to transmit BVC-RESET-ACK with right combination of conditional/optional IEs */
206static void _tx_bvc_reset_ack(struct osmo_fsm_inst *fi)
207{
208 struct bvc_fsm_priv *bfp = fi->priv;
209 const uint8_t *features = NULL;
210 const uint8_t *features_ext = NULL;
211 uint8_t _features[2] = {
212 (bfp->features.advertised >> 0) & 0xff,
213 (bfp->features.advertised >> 8) & 0xff,
214 };
215 struct msgb *tx;
216
217 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
218
219 /* transmit BVC-RESET-ACK to peer; RA-ID only present for PTP from BSS -> SGSN */
220 if (bfp->bvci == 0) {
221 features = &_features[0];
222 features_ext = &_features[1];
223 }
224 tx = bssgp2_enc_bvc_reset_ack(bfp->bvci, bfp->bvci && !bfp->role_sgsn ? &bfp->ra_id : NULL,
225 bfp->cell_id, features, features_ext);
226 fi_tx_sig(fi, tx);
227}
228
229/* helper function to transmit BVC-STATUS with right combination of conditional/optional IEs */
230static void _tx_status(struct osmo_fsm_inst *fi, enum gprs_bssgp_cause cause, const struct msgb *rx)
231{
232 struct bvc_fsm_priv *bfp = fi->priv;
233 struct msgb *tx;
234 uint16_t *bvci = NULL;
235
236 /* GSM 08.18, 10.4.14.1: The BVCI must be included if (and only if) the
237 * cause is either "BVCI blocked" or "BVCI unknown" */
238 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED)
239 bvci = &bfp->bvci;
240
Daniel Willmannfa632b82021-02-12 01:57:52 +0100241 tx = bssgp2_enc_status(cause, bvci, rx, bfp->max_pdu_len);
Harald Welte17a892f2020-12-07 21:39:03 +0100242
243 if (msgb_bvci(rx) == 0)
244 fi_tx_sig(fi, tx);
245 else
246 fi_tx_ptp(fi, tx);
247}
248
249/* Update the features by bit-wise AND of advertised + received features */
250static void update_negotiated_features(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
251{
252 struct bvc_fsm_priv *bfp = fi->priv;
253
254 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
255
256 bfp->features.received = 0;
257
258 if (TLVP_PRES_LEN(tp, BSSGP_IE_FEATURE_BITMAP, 1))
259 bfp->features.received |= *TLVP_VAL(tp, BSSGP_IE_FEATURE_BITMAP);
260
261 if (TLVP_PRES_LEN(tp, BSSGP_IE_EXT_FEATURE_BITMAP, 1))
262 bfp->features.received |= (*TLVP_VAL(tp, BSSGP_IE_EXT_FEATURE_BITMAP) << 8);
263
264 bfp->features.negotiated = bfp->features.advertised & bfp->features.received;
265
266 LOGPFSML(fi, LOGL_NOTICE, "Updating features: Advertised 0x%04x, Received 0x%04x, Negotiated 0x%04x\n",
267 bfp->features.advertised, bfp->features.received, bfp->features.negotiated);
268}
269
270/* "tail" of each onenter() handler: Calling the state change notification call-back */
271static void _onenter_tail(struct osmo_fsm_inst *fi, uint32_t prev_state)
272{
273 struct bvc_fsm_priv *bfp = fi->priv;
274
275 if (prev_state == fi->state)
276 return;
277
278 if (bfp->ops && bfp->ops->state_chg_notification)
279 bfp->ops->state_chg_notification(bfp->nsei, bfp->bvci, prev_state, fi->state, bfp->ops_priv);
280}
281
282static void bssgp_bvc_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
283{
284 /* we don't really expect anything in this state; all handled via allstate */
285 OSMO_ASSERT(0);
286}
287
288static void bssgp_bvc_fsm_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
289{
290 struct bvc_fsm_priv *bfp = fi->priv;
291 /* signaling BVC can never be blocked */
292 OSMO_ASSERT(bfp->bvci != 0);
293 _onenter_tail(fi, prev_state);
294}
295
296static void bssgp_bvc_fsm_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
297{
298 struct bvc_fsm_priv *bfp = fi->priv;
299 struct msgb *rx = NULL, *tx;
300 const struct tlv_parsed *tp = NULL;
301 uint8_t cause;
302
303 switch (event) {
304 case BSSGP_BVCFSM_E_RX_BLOCK_ACK:
305 rx = data;
306 tp = (const struct tlv_parsed *) msgb_bcid(rx);
307 /* If a BVC-BLOCK-ACK PDU is received by a BSS for the signalling BVC, the PDU is ignored. */
308 if (bfp->bvci == 0) {
309 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK-ACK on BVCI=0 is illegal\n");
310 if (!bfp->role_sgsn)
311 break;
312 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
313 break;
314 }
315 /* stop T1 timer */
316 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, 0, 0);
317 break;
318 case BSSGP_BVCFSM_E_RX_BLOCK:
319 rx = data;
320 tp = (const struct tlv_parsed *) msgb_bcid(rx);
321 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
322 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-BLOCK (cause=%s)\n", bssgp_cause_str(cause));
323 /* If a BVC-BLOCK PDU is received by an SGSN for a blocked BVC, a BVC-BLOCK-ACK
324 * PDU shall be returned. */
325 if (bfp->role_sgsn) {
326 /* If a BVC-BLOCK PDU is received by an SGSN for
327 * the signalling BVC, the PDU is ignored */
328 if (bfp->bvci == 0)
329 break;
330 tx = bssgp2_enc_bvc_block_ack(bfp->bvci);
331 fi_tx_sig(fi, tx);
332 }
333 break;
334 case BSSGP_BVCFSM_E_RX_UNBLOCK:
335 rx = data;
336 tp = (const struct tlv_parsed *) msgb_bcid(rx);
337 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-UNBLOCK\n");
338 if (bfp->bvci == 0) {
339 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK on BVCI=0 is illegal\n");
340 /* If BVC-UNBLOCK PDU is received by an SGSN for the signalling BVC, the PDU is ignored.*/
341 if (bfp->role_sgsn)
342 break;
343 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
344 break;
345 }
346 if (!bfp->role_sgsn) {
347 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK on BSS is illegal\n");
348 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
349 break;
350 }
351 tx = bssgp2_enc_bvc_unblock_ack(bfp->bvci);
352 fi_tx_sig(fi, tx);
353 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T1_SECS, T1);
354 break;
355 case BSSGP_BVCFSM_E_REQ_UNBLOCK:
356 if (bfp->role_sgsn) {
357 LOGPFSML(fi, LOGL_ERROR, "SGSN side cannot initiate BVC unblock\n");
358 break;
359 }
360 if (bfp->bvci == 0) {
361 LOGPFSML(fi, LOGL_ERROR, "BVCI 0 cannot be unblocked\n");
362 break;
363 }
364 bfp->locally_blocked = false;
365 tx = bssgp2_enc_bvc_unblock(bfp->bvci);
366 fi_tx_sig(fi, tx);
367 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
368 break;
369 }
370}
371
372/* Waiting for RESET-ACK: Receive PDUs but don't transmit */
373static void bssgp_bvc_fsm_wait_reset_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
374{
375 struct bvc_fsm_priv *bfp = fi->priv;
376 const struct tlv_parsed *tp = NULL;
377 struct msgb *rx = NULL, *tx;
378
379 switch (event) {
Harald Welte09422682021-02-13 11:01:47 +0100380 case BSSGP_BVCFSM_E_RX_RESET:
381 /* 48.018 Section 8.4.3: If the BSS (or SGSN) has sent a BVC-RESET PDU for a BVCI to
382 * the SGSN (or BSS) and is awaiting a BVC-RESET-ACK PDU in response, but instead
383 * receives a BVC-RESET PDU indicating the same BVCI, then this shall be interpreted
384 * as a BVC-RESET ACK PDU and the T2 timer shall be stopped. */
385 /* fall-through */
Harald Welte17a892f2020-12-07 21:39:03 +0100386 case BSSGP_BVCFSM_E_RX_RESET_ACK:
387 rx = data;
388 tp = (const struct tlv_parsed *) msgb_bcid(rx);
389 if (bfp->bvci == 0)
390 update_negotiated_features(fi, tp);
391 if (bfp->role_sgsn && bfp->bvci != 0)
392 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
393 if (!bfp->role_sgsn && bfp->bvci != 0 && bfp->locally_blocked) {
394 /* initiate the blocking procedure */
395 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
396 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
397 fi_tx_sig(fi, tx);
398 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
399 } else
400 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
401 break;
402 }
403}
404
405static void bssgp_bvc_fsm_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
406{
Harald Welte1fcfce82020-12-08 21:15:45 +0100407 struct bssgp2_flow_ctrl rx_fc, *tx_fc;
Harald Welte17a892f2020-12-07 21:39:03 +0100408 struct bvc_fsm_priv *bfp = fi->priv;
409 const struct tlv_parsed *tp = NULL;
410 struct msgb *rx = NULL, *tx;
Harald Welte1fcfce82020-12-08 21:15:45 +0100411 int rc;
Harald Welte17a892f2020-12-07 21:39:03 +0100412
413 switch (event) {
414 case BSSGP_BVCFSM_E_RX_UNBLOCK_ACK:
415 rx = data;
416 tp = (const struct tlv_parsed *) msgb_bcid(rx);
417 /* If BVC-UNBLOCK-ACK PDU is received by an BSS for the signalling BVC, the PDU is ignored. */
418 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK-ACK on BVCI=0 is illegal\n");
419 if (bfp->bvci == 0) {
420 if (!bfp->role_sgsn)
421 break;
422 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
423 break;
424 }
425 /* stop T1 timer */
426 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
427 break;
428 case BSSGP_BVCFSM_E_RX_UNBLOCK:
429 rx = data;
430 tp = (const struct tlv_parsed *) msgb_bcid(rx);
431 /* If a BVC-UNBLOCK PDU is received by an SGSN for a blocked BVC, a BVC-UNBLOCK-ACK
432 * PDU shall be returned. */
433 if (bfp->role_sgsn) {
434 /* If a BVC-UNBLOCK PDU is received by an SGSN for
435 * the signalling BVC, the PDU is ignored */
436 if (bfp->bvci == 0)
437 break;
438 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, bfp->nsei, bfp->bvci, 0);
439 }
440 break;
441 case BSSGP_BVCFSM_E_RX_BLOCK:
442 rx = data;
443 tp = (const struct tlv_parsed *) msgb_bcid(rx);
444 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-BLOCK (cause=%s)\n",
445 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
446 /* If a BVC-BLOCK PDU is received by an SGSN for the signalling BVC, the PDU is ignored */
447 if (bfp->bvci == 0) {
448 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BVCI=0 is illegal\n");
449 if (bfp->role_sgsn)
450 break;
451 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
452 break;
453 }
454 if (!bfp->role_sgsn) {
455 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BSS is illegal\n");
456 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
457 break;
458 }
459 /* transmit BVC-BLOCK-ACK, transition to BLOCKED state */
460 tx = bssgp2_enc_bvc_block_ack(bfp->bvci);
461 fi_tx_sig(fi, tx);
462 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, 0, 0);
463 break;
464 case BSSGP_BVCFSM_E_REQ_BLOCK:
465 if (bfp->role_sgsn) {
466 LOGPFSML(fi, LOGL_ERROR, "SGSN may not initiate BVC-BLOCK\n");
Daniel Willmann09bea012021-01-07 14:46:28 +0100467 break;
468 }
469 if (bfp->bvci == 0) {
470 LOGPFSML(fi, LOGL_ERROR, "BVCI 0 cannot be blocked\n");
Harald Welte17a892f2020-12-07 21:39:03 +0100471 break;
472 }
473 bfp->locally_blocked = true;
474 bfp->block_cause = *(uint8_t *)data;
475 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
476 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
477 fi_tx_sig(fi, tx);
478 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
479 break;
Harald Welte1fcfce82020-12-08 21:15:45 +0100480 case BSSGP_BVCFSM_E_RX_FC_BVC:
481 rx = data;
482 tp = (const struct tlv_parsed *) msgb_bcid(rx);
483 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
484 OSMO_ASSERT(bfp->role_sgsn);
485 rc = bssgp2_dec_fc_bvc(&rx_fc, tp);
486 if (rc < 0) {
487 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
488 break;
489 }
490 if (bfp->ops->rx_fc_bvc)
491 bfp->ops->rx_fc_bvc(bfp->nsei, bfp->bvci, &rx_fc, bfp->ops_priv);
492 tx = bssgp2_enc_fc_bvc_ack(rx_fc.tag);
493 fi_tx_ptp(fi, tx);
494 break;
495 case BSSGP_BVCFSM_E_RX_FC_BVC_ACK:
496 rx = data;
497 tp = (const struct tlv_parsed *) msgb_bcid(rx);
498 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
499 OSMO_ASSERT(!bfp->role_sgsn);
500 break;
501 case BSSGP_BVCFSM_E_REQ_FC_BVC:
502 tx_fc = data;
503 tx = bssgp2_enc_fc_bvc(tx_fc, bfp->features.negotiated & (BSSGP_XFEAT_GBIT << 8) ?
504 &bfp->features.fc_granularity : NULL);
505 fi_tx_ptp(fi, tx);
506 break;
Harald Welte17a892f2020-12-07 21:39:03 +0100507 }
508}
509
510static void bssgp_bvc_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
511{
512 struct bvc_fsm_priv *bfp = fi->priv;
513 uint8_t cause;
514 const struct tlv_parsed *tp = NULL;
515 struct msgb *rx = NULL;
516
517 switch (event) {
518 case BSSGP_BVCFSM_E_REQ_RESET:
519 bfp->locally_blocked = false;
520 cause = bfp->last_reset_cause = *(uint8_t *) data;
521 _tx_bvc_reset(fi, cause);
522 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
523#if 0 /* not sure if we really should notify the application if itself has requested the reset? */
524 if (bfp->ops && bfp->ops->reset_notification)
525 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, NULL, 0, cause, bfp->ops_priv);
526#endif
527 break;
528 case BSSGP_BVCFSM_E_RX_RESET:
529 rx = data;
530 tp = (const struct tlv_parsed *) msgb_bcid(rx);
531 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
532 if (bfp->role_sgsn && bfp->bvci != 0)
533 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
534 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-RESET (cause=%s)\n", bssgp_cause_str(cause));
535 if (bfp->bvci == 0)
536 update_negotiated_features(fi, tp);
537 _tx_bvc_reset_ack(fi);
538 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
539 if (bfp->ops && bfp->ops->reset_notification) {
540 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, &bfp->ra_id, bfp->cell_id,
541 cause, bfp->ops_priv);
542 }
543 break;
544 }
545}
546
547static int bssgp_bvc_fsm_timer_cb(struct osmo_fsm_inst *fi)
548{
549 struct bvc_fsm_priv *bfp = fi->priv;
550 struct msgb *tx;
551
552 switch (fi->T) {
553 case T1:
554 switch (fi->state) {
555 case BSSGP_BVCFSM_S_BLOCKED:
556 /* re-transmit BVC-BLOCK */
557 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
558 fi_tx_sig(fi, tx);
559 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
560 break;
561 case BSSGP_BVCFSM_S_UNBLOCKED:
562 /* re-transmit BVC-UNBLOCK */
563 tx = bssgp2_enc_bvc_unblock(bfp->bvci);
564 fi_tx_sig(fi, tx);
565 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T1_SECS, T1);
566 break;
567 }
568 break;
569 case T2:
570 switch (fi->state) {
571 case BSSGP_BVCFSM_S_WAIT_RESET_ACK:
572 /* re-transmit BVC-RESET */
573 _tx_bvc_reset(fi, bfp->last_reset_cause);
574 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
575 break;
576 case BSSGP_BVCFSM_S_UNBLOCKED:
577 /* re-transmit BVC-RESET-ACK */
578 _tx_bvc_reset_ack(fi);
579 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T2_SECS, T2);
580 break;
581 }
582 break;
583 default:
584 OSMO_ASSERT(0);
585 break;
586 }
587 return 0;
588}
589
590
591
592static const struct osmo_fsm_state bssgp_bvc_fsm_states[] = {
593 [BSSGP_BVCFSM_S_NULL] = {
594 /* initial state from which we must do a RESET */
595 .name = "NULL",
596 .in_event_mask = 0,
597 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
598 S(BSSGP_BVCFSM_S_UNBLOCKED),
599 .action = bssgp_bvc_fsm_null,
600 },
601 [BSSGP_BVCFSM_S_BLOCKED] = {
602 .name = "BLOCKED",
603 .in_event_mask = S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
604 S(BSSGP_BVCFSM_E_RX_BLOCK) |
605 S(BSSGP_BVCFSM_E_RX_BLOCK_ACK) |
606 S(BSSGP_BVCFSM_E_REQ_UNBLOCK),
607 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
608 S(BSSGP_BVCFSM_S_UNBLOCKED) |
609 S(BSSGP_BVCFSM_S_BLOCKED),
610 .action = bssgp_bvc_fsm_blocked,
611 .onenter = bssgp_bvc_fsm_blocked_onenter,
612 },
613 [BSSGP_BVCFSM_S_WAIT_RESET_ACK]= {
614 .name = "WAIT_RESET_ACK",
Harald Welte09422682021-02-13 11:01:47 +0100615 .in_event_mask = S(BSSGP_BVCFSM_E_RX_RESET_ACK) |
616 S(BSSGP_BVCFSM_E_RX_RESET),
Harald Welte17a892f2020-12-07 21:39:03 +0100617 .out_state_mask = S(BSSGP_BVCFSM_S_UNBLOCKED) |
618 S(BSSGP_BVCFSM_S_BLOCKED) |
619 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK),
620 .action = bssgp_bvc_fsm_wait_reset_ack,
621 .onenter = _onenter_tail,
622 },
623
624 [BSSGP_BVCFSM_S_UNBLOCKED] = {
625 .name = "UNBLOCKED",
626 .in_event_mask = S(BSSGP_BVCFSM_E_RX_BLOCK) |
627 S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
628 S(BSSGP_BVCFSM_E_RX_UNBLOCK_ACK) |
Harald Welte1fcfce82020-12-08 21:15:45 +0100629 S(BSSGP_BVCFSM_E_REQ_BLOCK) |
630 S(BSSGP_BVCFSM_E_RX_FC_BVC) |
631 S(BSSGP_BVCFSM_E_RX_FC_BVC_ACK) |
632 S(BSSGP_BVCFSM_E_REQ_FC_BVC),
Harald Welte17a892f2020-12-07 21:39:03 +0100633 .out_state_mask = S(BSSGP_BVCFSM_S_BLOCKED) |
634 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
635 S(BSSGP_BVCFSM_S_UNBLOCKED),
636 .action = bssgp_bvc_fsm_unblocked,
637 .onenter = _onenter_tail,
638 },
639};
640
641static struct osmo_fsm bssgp_bvc_fsm = {
642 .name = "BSSGP-BVC",
643 .states = bssgp_bvc_fsm_states,
644 .num_states = ARRAY_SIZE(bssgp_bvc_fsm_states),
645 .allstate_event_mask = S(BSSGP_BVCFSM_E_REQ_RESET) |
646 S(BSSGP_BVCFSM_E_RX_RESET),
647 .allstate_action = bssgp_bvc_fsm_allstate,
648 .timer_cb = bssgp_bvc_fsm_timer_cb,
649 .log_subsys = DLBSSGP,
650 .event_names = ptp_bvc_event_names,
651};
652
653static struct osmo_fsm_inst *
654_bvc_fsm_alloc(void *ctx, struct gprs_ns2_inst *nsi, bool role_sgsn, uint16_t nsei, uint16_t bvci)
655{
656 struct osmo_fsm_inst *fi;
657 struct bvc_fsm_priv *bfp;
658 char idbuf[64];
659
660 /* TODO: encode our role in the id string? */
661 snprintf(idbuf, sizeof(idbuf), "NSE%05u-BVC%05u", nsei, bvci);
662
663 fi = osmo_fsm_inst_alloc(&bssgp_bvc_fsm, ctx, NULL, LOGL_INFO, idbuf);
664 if (!fi)
665 return NULL;
666
667 bfp = talloc_zero(fi, struct bvc_fsm_priv);
668 if (!bfp) {
669 osmo_fsm_inst_free(fi);
670 return NULL;
671 }
672 fi->priv = bfp;
673
674 bfp->nsi = nsi;
675 bfp->role_sgsn = role_sgsn;
676 bfp->nsei = nsei;
677 bfp->bvci = bvci;
Daniel Willmann1ff86f72021-01-25 17:02:25 +0100678 bfp->max_pdu_len = UINT16_MAX;
Harald Welte17a892f2020-12-07 21:39:03 +0100679
680 return fi;
681}
682
683/*! Allocate a SIGNALING-BVC FSM for the BSS role (facing a remote SGSN).
684 * \param[in] ctx talloc context from which to allocate
685 * \param[in] nsi NS Instance on which this BVC operates
686 * \param[in] nsei NS Entity Identifier on which this BVC operates
687 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
688 * \returns newly-allocated FSM Instance; NULL in case of error */
689struct osmo_fsm_inst *
690bssgp_bvc_fsm_alloc_sig_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
691{
692 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, 0);
693 struct bvc_fsm_priv *bfp;
694
695 if (!fi)
696 return NULL;
697
698 bfp = fi->priv;
699 bfp->features.advertised = features;
700
701 return fi;
702}
703
704/*! Allocate a PTP-BVC FSM for the BSS role (facing a remote SGSN).
705 * \param[in] ctx talloc context from which to allocate
706 * \param[in] nsi NS Instance on which this BVC operates
707 * \param[in] nsei NS Entity Identifier on which this BVC operates
708 * \param[in] bvci BVCI of this FSM
709 * \param[in] ra_id Routing Area Identity of the cell (reported to SGSN)
710 * \param[in] cell_id Cell Identifier of the cell (reported to SGSN)
711 * \returns newly-allocated FSM Instance; NULL in case of error */
712struct osmo_fsm_inst *
713bssgp_bvc_fsm_alloc_ptp_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei,
714 uint16_t bvci, const struct gprs_ra_id *ra_id, uint16_t cell_id)
715{
716 struct osmo_fsm_inst *fi;
717 struct bvc_fsm_priv *bfp;
718
719 OSMO_ASSERT(bvci >= 2);
720 OSMO_ASSERT(ra_id);
721
722 fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, bvci);
723 if (!fi)
724 return NULL;
725
726 bfp = fi->priv;
727 bfp->ra_id = *ra_id;
728 bfp->cell_id = cell_id;
729
730 return fi;
731}
732
733/*! Allocate a SIGNALING-BVC FSM for the SGSN role (facing a remote BSS).
734 * \param[in] ctx talloc context from which to allocate
735 * \param[in] nsi NS Instance on which this BVC operates
736 * \param[in] nsei NS Entity Identifier on which this BVC operates
737 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
738 * \returns newly-allocated FSM Instance; NULL in case of error */
739struct osmo_fsm_inst *
740bssgp_bvc_fsm_alloc_sig_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
741{
742 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, 0);
743 struct bvc_fsm_priv *bfp;
744
745 if (!fi)
746 return NULL;
747
748 bfp = fi->priv;
749 bfp->features.advertised = features;
750
751 return fi;
752}
753
754/*! Allocate a PTP-BVC FSM for the SGSN role (facing a remote BSS).
755 * \param[in] ctx talloc context from which to allocate
756 * \param[in] nsi NS Instance on which this BVC operates
757 * \param[in] nsei NS Entity Identifier on which this BVC operates
758 * \param[in] bvci BVCI of this FSM
759 * \returns newly-allocated FSM Instance; NULL in case of error */
760struct osmo_fsm_inst *
761bssgp_bvc_fsm_alloc_ptp_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint16_t bvci)
762{
763 struct osmo_fsm_inst *fi;
764
765 OSMO_ASSERT(bvci >= 2);
766
767 fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, bvci);
768 if (!fi)
769 return NULL;
770
771 return fi;
772}
773
774/*! Set the 'operations' callbacks + private data.
775 * \param[in] fi FSM instance for which the data shall be set
776 * \param[in] ops BSSGP BVC FSM operations (call-back functions) to register
777 * \param[in] ops_priv opaque/private data pointer passed through to call-backs */
778void bssgp_bvc_fsm_set_ops(struct osmo_fsm_inst *fi, const struct bssgp_bvc_fsm_ops *ops, void *ops_priv)
779{
780 struct bvc_fsm_priv *bfp = fi->priv;
781
782 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
783
784 bfp->ops = ops;
785 bfp->ops_priv = ops_priv;
786}
787
788/*! Return if the given BVC FSM is in UNBLOCKED state. */
789bool bssgp_bvc_fsm_is_unblocked(struct osmo_fsm_inst *fi)
790{
791 return fi->state == BSSGP_BVCFSM_S_UNBLOCKED;
792}
793
794/*! Determine the cause value why given BVC FSM is blocked. */
795uint8_t bssgp_bvc_fsm_get_block_cause(struct osmo_fsm_inst *fi)
796{
797 struct bvc_fsm_priv *bfp = fi->priv;
798
799 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
800 return bfp->block_cause;
801}
802
803/*! Return the advertised features / extended features. */
Daniel Willmann4e5cdad2021-02-12 22:27:56 +0100804uint32_t bssgp_bvc_fsm_get_features_advertised(struct osmo_fsm_inst *fi)
Harald Welte17a892f2020-12-07 21:39:03 +0100805{
806 struct bvc_fsm_priv *bfp = fi->priv;
807
808 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
809 return bfp->features.advertised;
810}
811
812/*! Return the received features / extended features. */
Daniel Willmann4e5cdad2021-02-12 22:27:56 +0100813uint32_t bssgp_bvc_fsm_get_features_received(struct osmo_fsm_inst *fi)
Harald Welte17a892f2020-12-07 21:39:03 +0100814{
815 struct bvc_fsm_priv *bfp = fi->priv;
816
817 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
818 return bfp->features.received;
819}
820
821/*! Return the negotiated features / extended features. */
Daniel Willmann4e5cdad2021-02-12 22:27:56 +0100822uint32_t bssgp_bvc_fsm_get_features_negotiated(struct osmo_fsm_inst *fi)
Harald Welte17a892f2020-12-07 21:39:03 +0100823{
824 struct bvc_fsm_priv *bfp = fi->priv;
825
826 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
827 return bfp->features.negotiated;
828}
829
Daniel Willmann1ff86f72021-01-25 17:02:25 +0100830/*! Set the maximum size of a BSSGP PDU.
831 *! On the NS layer this corresponds to the size of an NS SDU in NS-UNITDATA (3GPP TS 48.016 Ch. 9.2.10) */
832void bssgp_bvc_fsm_set_max_pdu_len(struct osmo_fsm_inst *fi, uint16_t max_pdu_len) {
833 struct bvc_fsm_priv *bfp = fi->priv;
834
835 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
836 bfp->max_pdu_len = max_pdu_len;
837}
838
839/*! Return the maximum size of a BSSGP PDU
840 *! On the NS layer this corresponds to the size of an NS SDU in NS-UNITDATA (3GPP TS 48.016 Ch. 9.2.10) */
841uint16_t bssgp_bvc_fsm_get_max_pdu_len(const struct osmo_fsm_inst *fi)
842{
843 const struct bvc_fsm_priv *bfp = fi->priv;
844
845 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
846 return bfp->max_pdu_len;
847}
848
849
Harald Welte17a892f2020-12-07 21:39:03 +0100850static __attribute__((constructor)) void on_dso_load_bvc_fsm(void)
851{
Vadim Yanitskiye3d32d52021-02-05 14:37:41 +0100852 OSMO_ASSERT(osmo_fsm_register(&bssgp_bvc_fsm) == 0);
Harald Welte17a892f2020-12-07 21:39:03 +0100853}