blob: d2ee146206f69a6df749975af2e13a3df6d3f74d [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
241 tx = bssgp2_enc_status(cause, bvci, rx);
242
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) {
380 case BSSGP_BVCFSM_E_RX_RESET_ACK:
381 rx = data;
382 tp = (const struct tlv_parsed *) msgb_bcid(rx);
383 if (bfp->bvci == 0)
384 update_negotiated_features(fi, tp);
385 if (bfp->role_sgsn && bfp->bvci != 0)
386 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
387 if (!bfp->role_sgsn && bfp->bvci != 0 && bfp->locally_blocked) {
388 /* initiate the blocking procedure */
389 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
390 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
391 fi_tx_sig(fi, tx);
392 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
393 } else
394 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
395 break;
396 }
397}
398
399static void bssgp_bvc_fsm_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
400{
Harald Welte1fcfce82020-12-08 21:15:45 +0100401 struct bssgp2_flow_ctrl rx_fc, *tx_fc;
Harald Welte17a892f2020-12-07 21:39:03 +0100402 struct bvc_fsm_priv *bfp = fi->priv;
403 const struct tlv_parsed *tp = NULL;
404 struct msgb *rx = NULL, *tx;
Harald Welte1fcfce82020-12-08 21:15:45 +0100405 int rc;
Harald Welte17a892f2020-12-07 21:39:03 +0100406
407 switch (event) {
408 case BSSGP_BVCFSM_E_RX_UNBLOCK_ACK:
409 rx = data;
410 tp = (const struct tlv_parsed *) msgb_bcid(rx);
411 /* If BVC-UNBLOCK-ACK PDU is received by an BSS for the signalling BVC, the PDU is ignored. */
412 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK-ACK on BVCI=0 is illegal\n");
413 if (bfp->bvci == 0) {
414 if (!bfp->role_sgsn)
415 break;
416 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
417 break;
418 }
419 /* stop T1 timer */
420 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
421 break;
422 case BSSGP_BVCFSM_E_RX_UNBLOCK:
423 rx = data;
424 tp = (const struct tlv_parsed *) msgb_bcid(rx);
425 /* If a BVC-UNBLOCK PDU is received by an SGSN for a blocked BVC, a BVC-UNBLOCK-ACK
426 * PDU shall be returned. */
427 if (bfp->role_sgsn) {
428 /* If a BVC-UNBLOCK PDU is received by an SGSN for
429 * the signalling BVC, the PDU is ignored */
430 if (bfp->bvci == 0)
431 break;
432 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, bfp->nsei, bfp->bvci, 0);
433 }
434 break;
435 case BSSGP_BVCFSM_E_RX_BLOCK:
436 rx = data;
437 tp = (const struct tlv_parsed *) msgb_bcid(rx);
438 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-BLOCK (cause=%s)\n",
439 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
440 /* If a BVC-BLOCK PDU is received by an SGSN for the signalling BVC, the PDU is ignored */
441 if (bfp->bvci == 0) {
442 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BVCI=0 is illegal\n");
443 if (bfp->role_sgsn)
444 break;
445 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
446 break;
447 }
448 if (!bfp->role_sgsn) {
449 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BSS is illegal\n");
450 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
451 break;
452 }
453 /* transmit BVC-BLOCK-ACK, transition to BLOCKED state */
454 tx = bssgp2_enc_bvc_block_ack(bfp->bvci);
455 fi_tx_sig(fi, tx);
456 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, 0, 0);
457 break;
458 case BSSGP_BVCFSM_E_REQ_BLOCK:
459 if (bfp->role_sgsn) {
460 LOGPFSML(fi, LOGL_ERROR, "SGSN may not initiate BVC-BLOCK\n");
Daniel Willmann09bea012021-01-07 14:46:28 +0100461 break;
462 }
463 if (bfp->bvci == 0) {
464 LOGPFSML(fi, LOGL_ERROR, "BVCI 0 cannot be blocked\n");
Harald Welte17a892f2020-12-07 21:39:03 +0100465 break;
466 }
467 bfp->locally_blocked = true;
468 bfp->block_cause = *(uint8_t *)data;
469 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
470 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
471 fi_tx_sig(fi, tx);
472 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
473 break;
Harald Welte1fcfce82020-12-08 21:15:45 +0100474 case BSSGP_BVCFSM_E_RX_FC_BVC:
475 rx = data;
476 tp = (const struct tlv_parsed *) msgb_bcid(rx);
477 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
478 OSMO_ASSERT(bfp->role_sgsn);
479 rc = bssgp2_dec_fc_bvc(&rx_fc, tp);
480 if (rc < 0) {
481 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
482 break;
483 }
484 if (bfp->ops->rx_fc_bvc)
485 bfp->ops->rx_fc_bvc(bfp->nsei, bfp->bvci, &rx_fc, bfp->ops_priv);
486 tx = bssgp2_enc_fc_bvc_ack(rx_fc.tag);
487 fi_tx_ptp(fi, tx);
488 break;
489 case BSSGP_BVCFSM_E_RX_FC_BVC_ACK:
490 rx = data;
491 tp = (const struct tlv_parsed *) msgb_bcid(rx);
492 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
493 OSMO_ASSERT(!bfp->role_sgsn);
494 break;
495 case BSSGP_BVCFSM_E_REQ_FC_BVC:
496 tx_fc = data;
497 tx = bssgp2_enc_fc_bvc(tx_fc, bfp->features.negotiated & (BSSGP_XFEAT_GBIT << 8) ?
498 &bfp->features.fc_granularity : NULL);
499 fi_tx_ptp(fi, tx);
500 break;
Harald Welte17a892f2020-12-07 21:39:03 +0100501 }
502}
503
504static void bssgp_bvc_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
505{
506 struct bvc_fsm_priv *bfp = fi->priv;
507 uint8_t cause;
508 const struct tlv_parsed *tp = NULL;
509 struct msgb *rx = NULL;
510
511 switch (event) {
512 case BSSGP_BVCFSM_E_REQ_RESET:
513 bfp->locally_blocked = false;
514 cause = bfp->last_reset_cause = *(uint8_t *) data;
515 _tx_bvc_reset(fi, cause);
516 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
517#if 0 /* not sure if we really should notify the application if itself has requested the reset? */
518 if (bfp->ops && bfp->ops->reset_notification)
519 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, NULL, 0, cause, bfp->ops_priv);
520#endif
521 break;
522 case BSSGP_BVCFSM_E_RX_RESET:
523 rx = data;
524 tp = (const struct tlv_parsed *) msgb_bcid(rx);
525 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
526 if (bfp->role_sgsn && bfp->bvci != 0)
527 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
528 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-RESET (cause=%s)\n", bssgp_cause_str(cause));
529 if (bfp->bvci == 0)
530 update_negotiated_features(fi, tp);
531 _tx_bvc_reset_ack(fi);
532 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
533 if (bfp->ops && bfp->ops->reset_notification) {
534 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, &bfp->ra_id, bfp->cell_id,
535 cause, bfp->ops_priv);
536 }
537 break;
538 }
539}
540
541static int bssgp_bvc_fsm_timer_cb(struct osmo_fsm_inst *fi)
542{
543 struct bvc_fsm_priv *bfp = fi->priv;
544 struct msgb *tx;
545
546 switch (fi->T) {
547 case T1:
548 switch (fi->state) {
549 case BSSGP_BVCFSM_S_BLOCKED:
550 /* re-transmit BVC-BLOCK */
551 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
552 fi_tx_sig(fi, tx);
553 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
554 break;
555 case BSSGP_BVCFSM_S_UNBLOCKED:
556 /* re-transmit BVC-UNBLOCK */
557 tx = bssgp2_enc_bvc_unblock(bfp->bvci);
558 fi_tx_sig(fi, tx);
559 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T1_SECS, T1);
560 break;
561 }
562 break;
563 case T2:
564 switch (fi->state) {
565 case BSSGP_BVCFSM_S_WAIT_RESET_ACK:
566 /* re-transmit BVC-RESET */
567 _tx_bvc_reset(fi, bfp->last_reset_cause);
568 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
569 break;
570 case BSSGP_BVCFSM_S_UNBLOCKED:
571 /* re-transmit BVC-RESET-ACK */
572 _tx_bvc_reset_ack(fi);
573 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T2_SECS, T2);
574 break;
575 }
576 break;
577 default:
578 OSMO_ASSERT(0);
579 break;
580 }
581 return 0;
582}
583
584
585
586static const struct osmo_fsm_state bssgp_bvc_fsm_states[] = {
587 [BSSGP_BVCFSM_S_NULL] = {
588 /* initial state from which we must do a RESET */
589 .name = "NULL",
590 .in_event_mask = 0,
591 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
592 S(BSSGP_BVCFSM_S_UNBLOCKED),
593 .action = bssgp_bvc_fsm_null,
594 },
595 [BSSGP_BVCFSM_S_BLOCKED] = {
596 .name = "BLOCKED",
597 .in_event_mask = S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
598 S(BSSGP_BVCFSM_E_RX_BLOCK) |
599 S(BSSGP_BVCFSM_E_RX_BLOCK_ACK) |
600 S(BSSGP_BVCFSM_E_REQ_UNBLOCK),
601 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
602 S(BSSGP_BVCFSM_S_UNBLOCKED) |
603 S(BSSGP_BVCFSM_S_BLOCKED),
604 .action = bssgp_bvc_fsm_blocked,
605 .onenter = bssgp_bvc_fsm_blocked_onenter,
606 },
607 [BSSGP_BVCFSM_S_WAIT_RESET_ACK]= {
608 .name = "WAIT_RESET_ACK",
609 .in_event_mask = S(BSSGP_BVCFSM_E_RX_RESET_ACK),
610 .out_state_mask = S(BSSGP_BVCFSM_S_UNBLOCKED) |
611 S(BSSGP_BVCFSM_S_BLOCKED) |
612 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK),
613 .action = bssgp_bvc_fsm_wait_reset_ack,
614 .onenter = _onenter_tail,
615 },
616
617 [BSSGP_BVCFSM_S_UNBLOCKED] = {
618 .name = "UNBLOCKED",
619 .in_event_mask = S(BSSGP_BVCFSM_E_RX_BLOCK) |
620 S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
621 S(BSSGP_BVCFSM_E_RX_UNBLOCK_ACK) |
Harald Welte1fcfce82020-12-08 21:15:45 +0100622 S(BSSGP_BVCFSM_E_REQ_BLOCK) |
623 S(BSSGP_BVCFSM_E_RX_FC_BVC) |
624 S(BSSGP_BVCFSM_E_RX_FC_BVC_ACK) |
625 S(BSSGP_BVCFSM_E_REQ_FC_BVC),
Harald Welte17a892f2020-12-07 21:39:03 +0100626 .out_state_mask = S(BSSGP_BVCFSM_S_BLOCKED) |
627 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
628 S(BSSGP_BVCFSM_S_UNBLOCKED),
629 .action = bssgp_bvc_fsm_unblocked,
630 .onenter = _onenter_tail,
631 },
632};
633
634static struct osmo_fsm bssgp_bvc_fsm = {
635 .name = "BSSGP-BVC",
636 .states = bssgp_bvc_fsm_states,
637 .num_states = ARRAY_SIZE(bssgp_bvc_fsm_states),
638 .allstate_event_mask = S(BSSGP_BVCFSM_E_REQ_RESET) |
639 S(BSSGP_BVCFSM_E_RX_RESET),
640 .allstate_action = bssgp_bvc_fsm_allstate,
641 .timer_cb = bssgp_bvc_fsm_timer_cb,
642 .log_subsys = DLBSSGP,
643 .event_names = ptp_bvc_event_names,
644};
645
646static struct osmo_fsm_inst *
647_bvc_fsm_alloc(void *ctx, struct gprs_ns2_inst *nsi, bool role_sgsn, uint16_t nsei, uint16_t bvci)
648{
649 struct osmo_fsm_inst *fi;
650 struct bvc_fsm_priv *bfp;
651 char idbuf[64];
652
653 /* TODO: encode our role in the id string? */
654 snprintf(idbuf, sizeof(idbuf), "NSE%05u-BVC%05u", nsei, bvci);
655
656 fi = osmo_fsm_inst_alloc(&bssgp_bvc_fsm, ctx, NULL, LOGL_INFO, idbuf);
657 if (!fi)
658 return NULL;
659
660 bfp = talloc_zero(fi, struct bvc_fsm_priv);
661 if (!bfp) {
662 osmo_fsm_inst_free(fi);
663 return NULL;
664 }
665 fi->priv = bfp;
666
667 bfp->nsi = nsi;
668 bfp->role_sgsn = role_sgsn;
669 bfp->nsei = nsei;
670 bfp->bvci = bvci;
Daniel Willmann1ff86f72021-01-25 17:02:25 +0100671 bfp->max_pdu_len = UINT16_MAX;
Harald Welte17a892f2020-12-07 21:39:03 +0100672
673 return fi;
674}
675
676/*! Allocate a SIGNALING-BVC FSM for the BSS role (facing a remote SGSN).
677 * \param[in] ctx talloc context from which to allocate
678 * \param[in] nsi NS Instance on which this BVC operates
679 * \param[in] nsei NS Entity Identifier on which this BVC operates
680 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
681 * \returns newly-allocated FSM Instance; NULL in case of error */
682struct osmo_fsm_inst *
683bssgp_bvc_fsm_alloc_sig_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
684{
685 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, 0);
686 struct bvc_fsm_priv *bfp;
687
688 if (!fi)
689 return NULL;
690
691 bfp = fi->priv;
692 bfp->features.advertised = features;
693
694 return fi;
695}
696
697/*! Allocate a PTP-BVC FSM for the BSS role (facing a remote SGSN).
698 * \param[in] ctx talloc context from which to allocate
699 * \param[in] nsi NS Instance on which this BVC operates
700 * \param[in] nsei NS Entity Identifier on which this BVC operates
701 * \param[in] bvci BVCI of this FSM
702 * \param[in] ra_id Routing Area Identity of the cell (reported to SGSN)
703 * \param[in] cell_id Cell Identifier of the cell (reported to SGSN)
704 * \returns newly-allocated FSM Instance; NULL in case of error */
705struct osmo_fsm_inst *
706bssgp_bvc_fsm_alloc_ptp_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei,
707 uint16_t bvci, const struct gprs_ra_id *ra_id, uint16_t cell_id)
708{
709 struct osmo_fsm_inst *fi;
710 struct bvc_fsm_priv *bfp;
711
712 OSMO_ASSERT(bvci >= 2);
713 OSMO_ASSERT(ra_id);
714
715 fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, bvci);
716 if (!fi)
717 return NULL;
718
719 bfp = fi->priv;
720 bfp->ra_id = *ra_id;
721 bfp->cell_id = cell_id;
722
723 return fi;
724}
725
726/*! Allocate a SIGNALING-BVC FSM for the SGSN role (facing a remote BSS).
727 * \param[in] ctx talloc context from which to allocate
728 * \param[in] nsi NS Instance on which this BVC operates
729 * \param[in] nsei NS Entity Identifier on which this BVC operates
730 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
731 * \returns newly-allocated FSM Instance; NULL in case of error */
732struct osmo_fsm_inst *
733bssgp_bvc_fsm_alloc_sig_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
734{
735 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, 0);
736 struct bvc_fsm_priv *bfp;
737
738 if (!fi)
739 return NULL;
740
741 bfp = fi->priv;
742 bfp->features.advertised = features;
743
744 return fi;
745}
746
747/*! Allocate a PTP-BVC FSM for the SGSN role (facing a remote BSS).
748 * \param[in] ctx talloc context from which to allocate
749 * \param[in] nsi NS Instance on which this BVC operates
750 * \param[in] nsei NS Entity Identifier on which this BVC operates
751 * \param[in] bvci BVCI of this FSM
752 * \returns newly-allocated FSM Instance; NULL in case of error */
753struct osmo_fsm_inst *
754bssgp_bvc_fsm_alloc_ptp_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint16_t bvci)
755{
756 struct osmo_fsm_inst *fi;
757
758 OSMO_ASSERT(bvci >= 2);
759
760 fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, bvci);
761 if (!fi)
762 return NULL;
763
764 return fi;
765}
766
767/*! Set the 'operations' callbacks + private data.
768 * \param[in] fi FSM instance for which the data shall be set
769 * \param[in] ops BSSGP BVC FSM operations (call-back functions) to register
770 * \param[in] ops_priv opaque/private data pointer passed through to call-backs */
771void bssgp_bvc_fsm_set_ops(struct osmo_fsm_inst *fi, const struct bssgp_bvc_fsm_ops *ops, void *ops_priv)
772{
773 struct bvc_fsm_priv *bfp = fi->priv;
774
775 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
776
777 bfp->ops = ops;
778 bfp->ops_priv = ops_priv;
779}
780
781/*! Return if the given BVC FSM is in UNBLOCKED state. */
782bool bssgp_bvc_fsm_is_unblocked(struct osmo_fsm_inst *fi)
783{
784 return fi->state == BSSGP_BVCFSM_S_UNBLOCKED;
785}
786
787/*! Determine the cause value why given BVC FSM is blocked. */
788uint8_t bssgp_bvc_fsm_get_block_cause(struct osmo_fsm_inst *fi)
789{
790 struct bvc_fsm_priv *bfp = fi->priv;
791
792 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
793 return bfp->block_cause;
794}
795
796/*! Return the advertised features / extended features. */
797uint32_t bssgp_bvc_get_features_advertised(struct osmo_fsm_inst *fi)
798{
799 struct bvc_fsm_priv *bfp = fi->priv;
800
801 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
802 return bfp->features.advertised;
803}
804
805/*! Return the received features / extended features. */
806uint32_t bssgp_bvc_get_features_received(struct osmo_fsm_inst *fi)
807{
808 struct bvc_fsm_priv *bfp = fi->priv;
809
810 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
811 return bfp->features.received;
812}
813
814/*! Return the negotiated features / extended features. */
815uint32_t bssgp_bvc_get_features_negotiated(struct osmo_fsm_inst *fi)
816{
817 struct bvc_fsm_priv *bfp = fi->priv;
818
819 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
820 return bfp->features.negotiated;
821}
822
Daniel Willmann1ff86f72021-01-25 17:02:25 +0100823/*! Set the maximum size of a BSSGP PDU.
824 *! On the NS layer this corresponds to the size of an NS SDU in NS-UNITDATA (3GPP TS 48.016 Ch. 9.2.10) */
825void bssgp_bvc_fsm_set_max_pdu_len(struct osmo_fsm_inst *fi, uint16_t max_pdu_len) {
826 struct bvc_fsm_priv *bfp = fi->priv;
827
828 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
829 bfp->max_pdu_len = max_pdu_len;
830}
831
832/*! Return the maximum size of a BSSGP PDU
833 *! On the NS layer this corresponds to the size of an NS SDU in NS-UNITDATA (3GPP TS 48.016 Ch. 9.2.10) */
834uint16_t bssgp_bvc_fsm_get_max_pdu_len(const struct osmo_fsm_inst *fi)
835{
836 const struct bvc_fsm_priv *bfp = fi->priv;
837
838 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
839 return bfp->max_pdu_len;
840}
841
842
Harald Welte17a892f2020-12-07 21:39:03 +0100843static __attribute__((constructor)) void on_dso_load_bvc_fsm(void)
844{
Vadim Yanitskiye3d32d52021-02-05 14:37:41 +0100845 OSMO_ASSERT(osmo_fsm_register(&bssgp_bvc_fsm) == 0);
Harald Welte17a892f2020-12-07 21:39:03 +0100846}