blob: cc634a5315b8ccb189bfe2e26b2b5d2ebf6bb223 [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;
119
120 /* BVCI of this BVC */
121 uint16_t bvci;
122
123 /* are we the SGSN (true) or the BSS (false) */
124 bool role_sgsn;
125
126 /* BSS side: are we locally marked blocked? */
127 bool locally_blocked;
128 uint8_t block_cause;
129
130 /* cause value of the last outbound BVC-RESET (for re-transmissions) */
131 uint8_t last_reset_cause;
132
133 struct {
134 /* Bit 0..7: Features; Bit 8..15: Extended Features */
135 uint32_t advertised;
136 uint32_t received;
137 uint32_t negotiated;
Harald Welte1fcfce82020-12-08 21:15:45 +0100138 /* only used if BSSGP_XFEAT_GBIT is negotiated */
139 enum bssgp_fc_granularity fc_granularity;
Harald Welte17a892f2020-12-07 21:39:03 +0100140 } features;
141
142 /* Cell Identification used by BSS when
143 * transmitting BVC-RESET / BVC-RESET-ACK, or those received
144 * from BSS in SGSN role */
145 struct gprs_ra_id ra_id;
146 uint16_t cell_id;
147
148 /* call-backs provided by the user */
149 const struct bssgp_bvc_fsm_ops *ops;
150 /* private data pointer passed to each call-back invocation */
151 void *ops_priv;
152};
153
154static int fi_tx_ptp(struct osmo_fsm_inst *fi, struct msgb *msg)
155{
156 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
157 struct bvc_fsm_priv *bfp = fi->priv;
158
159 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
160
161 LOGPFSM(fi, "Tx BSSGP %s\n", osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type));
162
163 return bssgp2_nsi_tx_ptp(bfp->nsi, bfp->nsei, bfp->bvci, msg, 0);
164}
165
166static int fi_tx_sig(struct osmo_fsm_inst *fi, struct msgb *msg)
167{
168 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
169 struct bvc_fsm_priv *bfp = fi->priv;
170
171 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
172
173 LOGPFSM(fi, "Tx BSSGP %s\n", osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type));
174
175 return bssgp2_nsi_tx_sig(bfp->nsi, bfp->nsei, msg, 0);
176}
177
178/* helper function to transmit BVC-RESET with right combination of conditional/optional IEs */
179static void _tx_bvc_reset(struct osmo_fsm_inst *fi, uint8_t cause)
180{
181 struct bvc_fsm_priv *bfp = fi->priv;
182 const uint8_t *features = NULL;
183 const uint8_t *features_ext = NULL;
184 uint8_t _features[2] = {
185 (bfp->features.advertised >> 0) & 0xff,
186 (bfp->features.advertised >> 8) & 0xff,
187 };
188 struct msgb *tx;
189
190 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
191
192 /* transmit BVC-RESET to peer; RA-ID only present for PTP from BSS */
193 if (bfp->bvci == 0) {
194 features = &_features[0];
195 features_ext = &_features[1];
196 }
197 tx = bssgp2_enc_bvc_reset(bfp->bvci, cause,
198 bfp->bvci && !bfp->role_sgsn ? &bfp->ra_id : NULL,
199 bfp->cell_id, features, features_ext);
200 fi_tx_sig(fi, tx);
201}
202
203/* helper function to transmit BVC-RESET-ACK with right combination of conditional/optional IEs */
204static void _tx_bvc_reset_ack(struct osmo_fsm_inst *fi)
205{
206 struct bvc_fsm_priv *bfp = fi->priv;
207 const uint8_t *features = NULL;
208 const uint8_t *features_ext = NULL;
209 uint8_t _features[2] = {
210 (bfp->features.advertised >> 0) & 0xff,
211 (bfp->features.advertised >> 8) & 0xff,
212 };
213 struct msgb *tx;
214
215 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
216
217 /* transmit BVC-RESET-ACK to peer; RA-ID only present for PTP from BSS -> SGSN */
218 if (bfp->bvci == 0) {
219 features = &_features[0];
220 features_ext = &_features[1];
221 }
222 tx = bssgp2_enc_bvc_reset_ack(bfp->bvci, bfp->bvci && !bfp->role_sgsn ? &bfp->ra_id : NULL,
223 bfp->cell_id, features, features_ext);
224 fi_tx_sig(fi, tx);
225}
226
227/* helper function to transmit BVC-STATUS with right combination of conditional/optional IEs */
228static void _tx_status(struct osmo_fsm_inst *fi, enum gprs_bssgp_cause cause, const struct msgb *rx)
229{
230 struct bvc_fsm_priv *bfp = fi->priv;
231 struct msgb *tx;
232 uint16_t *bvci = NULL;
233
234 /* GSM 08.18, 10.4.14.1: The BVCI must be included if (and only if) the
235 * cause is either "BVCI blocked" or "BVCI unknown" */
236 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED)
237 bvci = &bfp->bvci;
238
239 tx = bssgp2_enc_status(cause, bvci, rx);
240
241 if (msgb_bvci(rx) == 0)
242 fi_tx_sig(fi, tx);
243 else
244 fi_tx_ptp(fi, tx);
245}
246
247/* Update the features by bit-wise AND of advertised + received features */
248static void update_negotiated_features(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
249{
250 struct bvc_fsm_priv *bfp = fi->priv;
251
252 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
253
254 bfp->features.received = 0;
255
256 if (TLVP_PRES_LEN(tp, BSSGP_IE_FEATURE_BITMAP, 1))
257 bfp->features.received |= *TLVP_VAL(tp, BSSGP_IE_FEATURE_BITMAP);
258
259 if (TLVP_PRES_LEN(tp, BSSGP_IE_EXT_FEATURE_BITMAP, 1))
260 bfp->features.received |= (*TLVP_VAL(tp, BSSGP_IE_EXT_FEATURE_BITMAP) << 8);
261
262 bfp->features.negotiated = bfp->features.advertised & bfp->features.received;
263
264 LOGPFSML(fi, LOGL_NOTICE, "Updating features: Advertised 0x%04x, Received 0x%04x, Negotiated 0x%04x\n",
265 bfp->features.advertised, bfp->features.received, bfp->features.negotiated);
266}
267
268/* "tail" of each onenter() handler: Calling the state change notification call-back */
269static void _onenter_tail(struct osmo_fsm_inst *fi, uint32_t prev_state)
270{
271 struct bvc_fsm_priv *bfp = fi->priv;
272
273 if (prev_state == fi->state)
274 return;
275
276 if (bfp->ops && bfp->ops->state_chg_notification)
277 bfp->ops->state_chg_notification(bfp->nsei, bfp->bvci, prev_state, fi->state, bfp->ops_priv);
278}
279
280static void bssgp_bvc_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
281{
282 /* we don't really expect anything in this state; all handled via allstate */
283 OSMO_ASSERT(0);
284}
285
286static void bssgp_bvc_fsm_blocked_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
287{
288 struct bvc_fsm_priv *bfp = fi->priv;
289 /* signaling BVC can never be blocked */
290 OSMO_ASSERT(bfp->bvci != 0);
291 _onenter_tail(fi, prev_state);
292}
293
294static void bssgp_bvc_fsm_blocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
295{
296 struct bvc_fsm_priv *bfp = fi->priv;
297 struct msgb *rx = NULL, *tx;
298 const struct tlv_parsed *tp = NULL;
299 uint8_t cause;
300
301 switch (event) {
302 case BSSGP_BVCFSM_E_RX_BLOCK_ACK:
303 rx = data;
304 tp = (const struct tlv_parsed *) msgb_bcid(rx);
305 /* If a BVC-BLOCK-ACK PDU is received by a BSS for the signalling BVC, the PDU is ignored. */
306 if (bfp->bvci == 0) {
307 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK-ACK on BVCI=0 is illegal\n");
308 if (!bfp->role_sgsn)
309 break;
310 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
311 break;
312 }
313 /* stop T1 timer */
314 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, 0, 0);
315 break;
316 case BSSGP_BVCFSM_E_RX_BLOCK:
317 rx = data;
318 tp = (const struct tlv_parsed *) msgb_bcid(rx);
319 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
320 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-BLOCK (cause=%s)\n", bssgp_cause_str(cause));
321 /* If a BVC-BLOCK PDU is received by an SGSN for a blocked BVC, a BVC-BLOCK-ACK
322 * PDU shall be returned. */
323 if (bfp->role_sgsn) {
324 /* If a BVC-BLOCK PDU is received by an SGSN for
325 * the signalling BVC, the PDU is ignored */
326 if (bfp->bvci == 0)
327 break;
328 tx = bssgp2_enc_bvc_block_ack(bfp->bvci);
329 fi_tx_sig(fi, tx);
330 }
331 break;
332 case BSSGP_BVCFSM_E_RX_UNBLOCK:
333 rx = data;
334 tp = (const struct tlv_parsed *) msgb_bcid(rx);
335 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-UNBLOCK\n");
336 if (bfp->bvci == 0) {
337 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK on BVCI=0 is illegal\n");
338 /* If BVC-UNBLOCK PDU is received by an SGSN for the signalling BVC, the PDU is ignored.*/
339 if (bfp->role_sgsn)
340 break;
341 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
342 break;
343 }
344 if (!bfp->role_sgsn) {
345 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK on BSS is illegal\n");
346 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
347 break;
348 }
349 tx = bssgp2_enc_bvc_unblock_ack(bfp->bvci);
350 fi_tx_sig(fi, tx);
351 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T1_SECS, T1);
352 break;
353 case BSSGP_BVCFSM_E_REQ_UNBLOCK:
354 if (bfp->role_sgsn) {
355 LOGPFSML(fi, LOGL_ERROR, "SGSN side cannot initiate BVC unblock\n");
356 break;
357 }
358 if (bfp->bvci == 0) {
359 LOGPFSML(fi, LOGL_ERROR, "BVCI 0 cannot be unblocked\n");
360 break;
361 }
362 bfp->locally_blocked = false;
363 tx = bssgp2_enc_bvc_unblock(bfp->bvci);
364 fi_tx_sig(fi, tx);
365 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
366 break;
367 }
368}
369
370/* Waiting for RESET-ACK: Receive PDUs but don't transmit */
371static void bssgp_bvc_fsm_wait_reset_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
372{
373 struct bvc_fsm_priv *bfp = fi->priv;
374 const struct tlv_parsed *tp = NULL;
375 struct msgb *rx = NULL, *tx;
376
377 switch (event) {
378 case BSSGP_BVCFSM_E_RX_RESET_ACK:
379 rx = data;
380 tp = (const struct tlv_parsed *) msgb_bcid(rx);
381 if (bfp->bvci == 0)
382 update_negotiated_features(fi, tp);
383 if (bfp->role_sgsn && bfp->bvci != 0)
384 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
385 if (!bfp->role_sgsn && bfp->bvci != 0 && bfp->locally_blocked) {
386 /* initiate the blocking procedure */
387 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
388 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
389 fi_tx_sig(fi, tx);
390 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
391 } else
392 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
393 break;
394 }
395}
396
397static void bssgp_bvc_fsm_unblocked(struct osmo_fsm_inst *fi, uint32_t event, void *data)
398{
Harald Welte1fcfce82020-12-08 21:15:45 +0100399 struct bssgp2_flow_ctrl rx_fc, *tx_fc;
Harald Welte17a892f2020-12-07 21:39:03 +0100400 struct bvc_fsm_priv *bfp = fi->priv;
401 const struct tlv_parsed *tp = NULL;
402 struct msgb *rx = NULL, *tx;
Harald Welte1fcfce82020-12-08 21:15:45 +0100403 int rc;
Harald Welte17a892f2020-12-07 21:39:03 +0100404
405 switch (event) {
406 case BSSGP_BVCFSM_E_RX_UNBLOCK_ACK:
407 rx = data;
408 tp = (const struct tlv_parsed *) msgb_bcid(rx);
409 /* If BVC-UNBLOCK-ACK PDU is received by an BSS for the signalling BVC, the PDU is ignored. */
410 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-UNBLOCK-ACK on BVCI=0 is illegal\n");
411 if (bfp->bvci == 0) {
412 if (!bfp->role_sgsn)
413 break;
414 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
415 break;
416 }
417 /* stop T1 timer */
418 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
419 break;
420 case BSSGP_BVCFSM_E_RX_UNBLOCK:
421 rx = data;
422 tp = (const struct tlv_parsed *) msgb_bcid(rx);
423 /* If a BVC-UNBLOCK PDU is received by an SGSN for a blocked BVC, a BVC-UNBLOCK-ACK
424 * PDU shall be returned. */
425 if (bfp->role_sgsn) {
426 /* If a BVC-UNBLOCK PDU is received by an SGSN for
427 * the signalling BVC, the PDU is ignored */
428 if (bfp->bvci == 0)
429 break;
430 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, bfp->nsei, bfp->bvci, 0);
431 }
432 break;
433 case BSSGP_BVCFSM_E_RX_BLOCK:
434 rx = data;
435 tp = (const struct tlv_parsed *) msgb_bcid(rx);
436 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-BLOCK (cause=%s)\n",
437 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
438 /* If a BVC-BLOCK PDU is received by an SGSN for the signalling BVC, the PDU is ignored */
439 if (bfp->bvci == 0) {
440 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BVCI=0 is illegal\n");
441 if (bfp->role_sgsn)
442 break;
443 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
444 break;
445 }
446 if (!bfp->role_sgsn) {
447 LOGPFSML(fi, LOGL_ERROR, "Rx BVC-BLOCK on BSS is illegal\n");
448 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
449 break;
450 }
451 /* transmit BVC-BLOCK-ACK, transition to BLOCKED state */
452 tx = bssgp2_enc_bvc_block_ack(bfp->bvci);
453 fi_tx_sig(fi, tx);
454 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, 0, 0);
455 break;
456 case BSSGP_BVCFSM_E_REQ_BLOCK:
457 if (bfp->role_sgsn) {
458 LOGPFSML(fi, LOGL_ERROR, "SGSN may not initiate BVC-BLOCK\n");
459 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
460 break;
461 }
462 bfp->locally_blocked = true;
463 bfp->block_cause = *(uint8_t *)data;
464 /* transmit BVC-BLOCK, transition to BLOCKED state and start re-transmit timer */
465 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
466 fi_tx_sig(fi, tx);
467 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
468 break;
Harald Welte1fcfce82020-12-08 21:15:45 +0100469 case BSSGP_BVCFSM_E_RX_FC_BVC:
470 rx = data;
471 tp = (const struct tlv_parsed *) msgb_bcid(rx);
472 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
473 OSMO_ASSERT(bfp->role_sgsn);
474 rc = bssgp2_dec_fc_bvc(&rx_fc, tp);
475 if (rc < 0) {
476 _tx_status(fi, BSSGP_CAUSE_SEM_INCORR_PDU, rx);
477 break;
478 }
479 if (bfp->ops->rx_fc_bvc)
480 bfp->ops->rx_fc_bvc(bfp->nsei, bfp->bvci, &rx_fc, bfp->ops_priv);
481 tx = bssgp2_enc_fc_bvc_ack(rx_fc.tag);
482 fi_tx_ptp(fi, tx);
483 break;
484 case BSSGP_BVCFSM_E_RX_FC_BVC_ACK:
485 rx = data;
486 tp = (const struct tlv_parsed *) msgb_bcid(rx);
487 /* we assume osmo_tlv_prot_* has been used before calling here to ensure this */
488 OSMO_ASSERT(!bfp->role_sgsn);
489 break;
490 case BSSGP_BVCFSM_E_REQ_FC_BVC:
491 tx_fc = data;
492 tx = bssgp2_enc_fc_bvc(tx_fc, bfp->features.negotiated & (BSSGP_XFEAT_GBIT << 8) ?
493 &bfp->features.fc_granularity : NULL);
494 fi_tx_ptp(fi, tx);
495 break;
Harald Welte17a892f2020-12-07 21:39:03 +0100496 }
497}
498
499static void bssgp_bvc_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
500{
501 struct bvc_fsm_priv *bfp = fi->priv;
502 uint8_t cause;
503 const struct tlv_parsed *tp = NULL;
504 struct msgb *rx = NULL;
505
506 switch (event) {
507 case BSSGP_BVCFSM_E_REQ_RESET:
508 bfp->locally_blocked = false;
509 cause = bfp->last_reset_cause = *(uint8_t *) data;
510 _tx_bvc_reset(fi, cause);
511 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
512#if 0 /* not sure if we really should notify the application if itself has requested the reset? */
513 if (bfp->ops && bfp->ops->reset_notification)
514 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, NULL, 0, cause, bfp->ops_priv);
515#endif
516 break;
517 case BSSGP_BVCFSM_E_RX_RESET:
518 rx = data;
519 tp = (const struct tlv_parsed *) msgb_bcid(rx);
520 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
521 if (bfp->role_sgsn && bfp->bvci != 0)
522 bfp->cell_id = bssgp_parse_cell_id(&bfp->ra_id, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
523 LOGPFSML(fi, LOGL_NOTICE, "Rx BVC-RESET (cause=%s)\n", bssgp_cause_str(cause));
524 if (bfp->bvci == 0)
525 update_negotiated_features(fi, tp);
526 _tx_bvc_reset_ack(fi);
527 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, 0, 0);
528 if (bfp->ops && bfp->ops->reset_notification) {
529 bfp->ops->reset_notification(bfp->nsei, bfp->bvci, &bfp->ra_id, bfp->cell_id,
530 cause, bfp->ops_priv);
531 }
532 break;
533 }
534}
535
536static int bssgp_bvc_fsm_timer_cb(struct osmo_fsm_inst *fi)
537{
538 struct bvc_fsm_priv *bfp = fi->priv;
539 struct msgb *tx;
540
541 switch (fi->T) {
542 case T1:
543 switch (fi->state) {
544 case BSSGP_BVCFSM_S_BLOCKED:
545 /* re-transmit BVC-BLOCK */
546 tx = bssgp2_enc_bvc_block(bfp->bvci, bfp->block_cause);
547 fi_tx_sig(fi, tx);
548 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_BLOCKED, T1_SECS, T1);
549 break;
550 case BSSGP_BVCFSM_S_UNBLOCKED:
551 /* re-transmit BVC-UNBLOCK */
552 tx = bssgp2_enc_bvc_unblock(bfp->bvci);
553 fi_tx_sig(fi, tx);
554 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T1_SECS, T1);
555 break;
556 }
557 break;
558 case T2:
559 switch (fi->state) {
560 case BSSGP_BVCFSM_S_WAIT_RESET_ACK:
561 /* re-transmit BVC-RESET */
562 _tx_bvc_reset(fi, bfp->last_reset_cause);
563 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_WAIT_RESET_ACK, T2_SECS, T2);
564 break;
565 case BSSGP_BVCFSM_S_UNBLOCKED:
566 /* re-transmit BVC-RESET-ACK */
567 _tx_bvc_reset_ack(fi);
568 osmo_fsm_inst_state_chg(fi, BSSGP_BVCFSM_S_UNBLOCKED, T2_SECS, T2);
569 break;
570 }
571 break;
572 default:
573 OSMO_ASSERT(0);
574 break;
575 }
576 return 0;
577}
578
579
580
581static const struct osmo_fsm_state bssgp_bvc_fsm_states[] = {
582 [BSSGP_BVCFSM_S_NULL] = {
583 /* initial state from which we must do a RESET */
584 .name = "NULL",
585 .in_event_mask = 0,
586 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
587 S(BSSGP_BVCFSM_S_UNBLOCKED),
588 .action = bssgp_bvc_fsm_null,
589 },
590 [BSSGP_BVCFSM_S_BLOCKED] = {
591 .name = "BLOCKED",
592 .in_event_mask = S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
593 S(BSSGP_BVCFSM_E_RX_BLOCK) |
594 S(BSSGP_BVCFSM_E_RX_BLOCK_ACK) |
595 S(BSSGP_BVCFSM_E_REQ_UNBLOCK),
596 .out_state_mask = S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
597 S(BSSGP_BVCFSM_S_UNBLOCKED) |
598 S(BSSGP_BVCFSM_S_BLOCKED),
599 .action = bssgp_bvc_fsm_blocked,
600 .onenter = bssgp_bvc_fsm_blocked_onenter,
601 },
602 [BSSGP_BVCFSM_S_WAIT_RESET_ACK]= {
603 .name = "WAIT_RESET_ACK",
604 .in_event_mask = S(BSSGP_BVCFSM_E_RX_RESET_ACK),
605 .out_state_mask = S(BSSGP_BVCFSM_S_UNBLOCKED) |
606 S(BSSGP_BVCFSM_S_BLOCKED) |
607 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK),
608 .action = bssgp_bvc_fsm_wait_reset_ack,
609 .onenter = _onenter_tail,
610 },
611
612 [BSSGP_BVCFSM_S_UNBLOCKED] = {
613 .name = "UNBLOCKED",
614 .in_event_mask = S(BSSGP_BVCFSM_E_RX_BLOCK) |
615 S(BSSGP_BVCFSM_E_RX_UNBLOCK) |
616 S(BSSGP_BVCFSM_E_RX_UNBLOCK_ACK) |
Harald Welte1fcfce82020-12-08 21:15:45 +0100617 S(BSSGP_BVCFSM_E_REQ_BLOCK) |
618 S(BSSGP_BVCFSM_E_RX_FC_BVC) |
619 S(BSSGP_BVCFSM_E_RX_FC_BVC_ACK) |
620 S(BSSGP_BVCFSM_E_REQ_FC_BVC),
Harald Welte17a892f2020-12-07 21:39:03 +0100621 .out_state_mask = S(BSSGP_BVCFSM_S_BLOCKED) |
622 S(BSSGP_BVCFSM_S_WAIT_RESET_ACK) |
623 S(BSSGP_BVCFSM_S_UNBLOCKED),
624 .action = bssgp_bvc_fsm_unblocked,
625 .onenter = _onenter_tail,
626 },
627};
628
629static struct osmo_fsm bssgp_bvc_fsm = {
630 .name = "BSSGP-BVC",
631 .states = bssgp_bvc_fsm_states,
632 .num_states = ARRAY_SIZE(bssgp_bvc_fsm_states),
633 .allstate_event_mask = S(BSSGP_BVCFSM_E_REQ_RESET) |
634 S(BSSGP_BVCFSM_E_RX_RESET),
635 .allstate_action = bssgp_bvc_fsm_allstate,
636 .timer_cb = bssgp_bvc_fsm_timer_cb,
637 .log_subsys = DLBSSGP,
638 .event_names = ptp_bvc_event_names,
639};
640
641static struct osmo_fsm_inst *
642_bvc_fsm_alloc(void *ctx, struct gprs_ns2_inst *nsi, bool role_sgsn, uint16_t nsei, uint16_t bvci)
643{
644 struct osmo_fsm_inst *fi;
645 struct bvc_fsm_priv *bfp;
646 char idbuf[64];
647
648 /* TODO: encode our role in the id string? */
649 snprintf(idbuf, sizeof(idbuf), "NSE%05u-BVC%05u", nsei, bvci);
650
651 fi = osmo_fsm_inst_alloc(&bssgp_bvc_fsm, ctx, NULL, LOGL_INFO, idbuf);
652 if (!fi)
653 return NULL;
654
655 bfp = talloc_zero(fi, struct bvc_fsm_priv);
656 if (!bfp) {
657 osmo_fsm_inst_free(fi);
658 return NULL;
659 }
660 fi->priv = bfp;
661
662 bfp->nsi = nsi;
663 bfp->role_sgsn = role_sgsn;
664 bfp->nsei = nsei;
665 bfp->bvci = bvci;
666
667 return fi;
668}
669
670/*! Allocate a SIGNALING-BVC FSM for the BSS role (facing a remote SGSN).
671 * \param[in] ctx talloc context from which to allocate
672 * \param[in] nsi NS Instance on which this BVC operates
673 * \param[in] nsei NS Entity Identifier on which this BVC operates
674 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
675 * \returns newly-allocated FSM Instance; NULL in case of error */
676struct osmo_fsm_inst *
677bssgp_bvc_fsm_alloc_sig_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
678{
679 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, 0);
680 struct bvc_fsm_priv *bfp;
681
682 if (!fi)
683 return NULL;
684
685 bfp = fi->priv;
686 bfp->features.advertised = features;
687
688 return fi;
689}
690
691/*! Allocate a PTP-BVC FSM for the BSS role (facing a remote SGSN).
692 * \param[in] ctx talloc context from which to allocate
693 * \param[in] nsi NS Instance on which this BVC operates
694 * \param[in] nsei NS Entity Identifier on which this BVC operates
695 * \param[in] bvci BVCI of this FSM
696 * \param[in] ra_id Routing Area Identity of the cell (reported to SGSN)
697 * \param[in] cell_id Cell Identifier of the cell (reported to SGSN)
698 * \returns newly-allocated FSM Instance; NULL in case of error */
699struct osmo_fsm_inst *
700bssgp_bvc_fsm_alloc_ptp_bss(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei,
701 uint16_t bvci, const struct gprs_ra_id *ra_id, uint16_t cell_id)
702{
703 struct osmo_fsm_inst *fi;
704 struct bvc_fsm_priv *bfp;
705
706 OSMO_ASSERT(bvci >= 2);
707 OSMO_ASSERT(ra_id);
708
709 fi = _bvc_fsm_alloc(ctx, nsi, false, nsei, bvci);
710 if (!fi)
711 return NULL;
712
713 bfp = fi->priv;
714 bfp->ra_id = *ra_id;
715 bfp->cell_id = cell_id;
716
717 return fi;
718}
719
720/*! Allocate a SIGNALING-BVC FSM for the SGSN role (facing a remote BSS).
721 * \param[in] ctx talloc context from which to allocate
722 * \param[in] nsi NS Instance on which this BVC operates
723 * \param[in] nsei NS Entity Identifier on which this BVC operates
724 * \param[in] features Feature [byte 0] and Extended Feature [byte 1] bitmap
725 * \returns newly-allocated FSM Instance; NULL in case of error */
726struct osmo_fsm_inst *
727bssgp_bvc_fsm_alloc_sig_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint32_t features)
728{
729 struct osmo_fsm_inst *fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, 0);
730 struct bvc_fsm_priv *bfp;
731
732 if (!fi)
733 return NULL;
734
735 bfp = fi->priv;
736 bfp->features.advertised = features;
737
738 return fi;
739}
740
741/*! Allocate a PTP-BVC FSM for the SGSN role (facing a remote BSS).
742 * \param[in] ctx talloc context from which to allocate
743 * \param[in] nsi NS Instance on which this BVC operates
744 * \param[in] nsei NS Entity Identifier on which this BVC operates
745 * \param[in] bvci BVCI of this FSM
746 * \returns newly-allocated FSM Instance; NULL in case of error */
747struct osmo_fsm_inst *
748bssgp_bvc_fsm_alloc_ptp_sgsn(void *ctx, struct gprs_ns2_inst *nsi, uint16_t nsei, uint16_t bvci)
749{
750 struct osmo_fsm_inst *fi;
751
752 OSMO_ASSERT(bvci >= 2);
753
754 fi = _bvc_fsm_alloc(ctx, nsi, true, nsei, bvci);
755 if (!fi)
756 return NULL;
757
758 return fi;
759}
760
761/*! Set the 'operations' callbacks + private data.
762 * \param[in] fi FSM instance for which the data shall be set
763 * \param[in] ops BSSGP BVC FSM operations (call-back functions) to register
764 * \param[in] ops_priv opaque/private data pointer passed through to call-backs */
765void bssgp_bvc_fsm_set_ops(struct osmo_fsm_inst *fi, const struct bssgp_bvc_fsm_ops *ops, void *ops_priv)
766{
767 struct bvc_fsm_priv *bfp = fi->priv;
768
769 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
770
771 bfp->ops = ops;
772 bfp->ops_priv = ops_priv;
773}
774
775/*! Return if the given BVC FSM is in UNBLOCKED state. */
776bool bssgp_bvc_fsm_is_unblocked(struct osmo_fsm_inst *fi)
777{
778 return fi->state == BSSGP_BVCFSM_S_UNBLOCKED;
779}
780
781/*! Determine the cause value why given BVC FSM is blocked. */
782uint8_t bssgp_bvc_fsm_get_block_cause(struct osmo_fsm_inst *fi)
783{
784 struct bvc_fsm_priv *bfp = fi->priv;
785
786 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
787 return bfp->block_cause;
788}
789
790/*! Return the advertised features / extended features. */
791uint32_t bssgp_bvc_get_features_advertised(struct osmo_fsm_inst *fi)
792{
793 struct bvc_fsm_priv *bfp = fi->priv;
794
795 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
796 return bfp->features.advertised;
797}
798
799/*! Return the received features / extended features. */
800uint32_t bssgp_bvc_get_features_received(struct osmo_fsm_inst *fi)
801{
802 struct bvc_fsm_priv *bfp = fi->priv;
803
804 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
805 return bfp->features.received;
806}
807
808/*! Return the negotiated features / extended features. */
809uint32_t bssgp_bvc_get_features_negotiated(struct osmo_fsm_inst *fi)
810{
811 struct bvc_fsm_priv *bfp = fi->priv;
812
813 OSMO_ASSERT(fi->fsm == &bssgp_bvc_fsm);
814 return bfp->features.negotiated;
815}
816
817static __attribute__((constructor)) void on_dso_load_bvc_fsm(void)
818{
819 osmo_fsm_register(&bssgp_bvc_fsm);
820}