blob: f3867fe4efb0189ff7bd3bdc48299d3b63789ced [file] [log] [blame]
Andreas Eversberge24636c2023-04-23 12:20:55 +02001/* Handle VGCS/VBCS calls. (Voice Group/Broadcast Call Service). */
2/*
3 * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: AGPL-3.0+
7 *
8 * Author: Andreas Eversberg
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/* The process consists of four state machines:
25 *
26 * The call control state machine "GCC" handles the voice group/broadcast call.
27 * There is one instance for every call. It is mainly controlled by the calling
28 * subscriber. The state machine is described in 3GPP TS 44.068 / 44.069.
29 * One SCCP connection to the calling subscriber is associated with the state
30 * machine. Once the calling subscriber leaves or is assigned to the VGCS/VBS
31 * channel, the association to the MSC-A role is removed and the SCCP connection
32 * is closed. The state machine with the transaction still exists until the end
33 * of the call.
34 *
35 * The BSS control state machine "vgcs_bss_fsm" handles the call in each BSC.
36 * There are as many instances as there are BSCs where the call is placed to.
37 * The instances are linked to the call control in a 1:n relation.
38 * One SCCP connection for every BSC is associated with the state machine.
39 * It sets up the call in the BSC and handles the uplink control and signaling
40 * with the talking phone.
41 *
42 * The resource controling state machine "vgcs_cell_fsm" handles the channel for
43 * each BTS that has a VGCS for the call. The instances are linked to the BSS
44 * control in a 1:n relation.
45 * One SCCP connection for every cell is associated with each list entry.
46 * It assigns the VGCS/VBS channel and the conference bridge in the MGW.
47 *
48 * The MGW endpoint state machine "vgcs_mgw_ep_fsm" handles the endpoint
49 * connection for each call. It controls the clearing of the MGW connections
50 * in case of endpoint failure. All instances of the resource controlling state
51 * machine are linked to this state machine in a 1:n relation.
52 *
53 * Setup of a call:
54 *
55 * When the calling subscriber dials a group/broadcast call, the GCR is checked
56 * for an existing Group ID. If it exists, the call is setup towards the a given
57 * list of MSCs for this Group ID. Also the channels are assigned for a given
58 * list of cells for this Group ID.
59 * The call can also be initiated via VTY.
60 *
61 * Then the calling subscriber is assigned to the VGCS channel of the same cell
62 * where the call was initialized. Afterwards the call is connected. The calling
63 * subscriber may then stay on the uplink or release it.
64 *
65 * Uplink control:
66 *
67 * Any BSC may indicate a talking subscriber. If there is no talking subscriber
68 * yet, the uplink is granted, otherwise it is rejected. If the uplink is in
69 * use on one BSC, all other BSCs will be blocked. If the uplink becomes free,
70 * all other BSCs will be unblocked.
71 *
72 * Termination of the call:
73 *
74 * The calling subscriber accesses the uplink. The it sends a termination
75 * request. This request is acknowledged by a termination command towards
76 * the calling subscriber. The call is cleared.
77 * The call can also be terminated via VTY and/or a timeout.
78 *
79 */
80
81#include <osmocom/core/utils.h>
82#include <osmocom/core/fsm.h>
83#include <osmocom/gsm/protocol/gsm_44_068.h>
84#include <osmocom/sigtran/sccp_helpers.h>
85#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
86
87#include <osmocom/msc/gsm_data.h>
88#include <osmocom/msc/sccp_ran.h>
89#include <osmocom/msc/ran_infra.h>
90#include <osmocom/msc/ran_peer.h>
91#include <osmocom/msc/ran_msg_a.h>
92#include <osmocom/msc/msub.h>
93#include <osmocom/msc/debug.h>
94#include <osmocom/msc/msc_a.h>
95#include <osmocom/msc/vlr.h>
96#include <osmocom/msc/rtp_stream.h>
97#include <osmocom/msc/codec_mapping.h>
98#include <osmocom/msc/msc_vgcs.h>
99#include <osmocom/msc/asci_gcr.h>
100
101#define S(x) (1 << (x))
102
103#define LOG_GCC(trans, level, fmt, args...) \
104 LOGP((trans) ? ((trans->type == TRANS_GCC) ? DGCC : DBCC) : DASCI, level, \
105 (trans) ? ((trans->type == TRANS_GCC) ? ("GCC callref %s: " fmt) : ("BCC callref %s: " fmt)) : "%s" fmt, \
106 (trans) ? gsm44068_group_id_string(trans->callref) : "", ##args)
107#define LOG_BSS(bss, level, fmt, args...) \
108 LOGP(DASCI, level, \
109 (bss->trans_type == TRANS_GCC) ? ("GCC callref %s, BSS #%s: " fmt) : ("BCC callref %s, BSS #%s: " fmt), \
110 gsm44068_group_id_string(bss->callref), osmo_ss7_pointcode_print(NULL, bss->pc), ##args)
111#define LOG_CELL(cell, level, fmt, args...) \
112 LOGP(DASCI, level, \
113 (cell->trans_type == TRANS_GCC) ? ("GCC callref %s, BSS #%s, CID %d: " fmt) \
114 : ("BCC callref %s, BSS #%s, CID %d: " fmt), \
115 gsm44068_group_id_string(cell->callref), osmo_ss7_pointcode_print(NULL, cell->pc), cell->cell_id, ##args)
116
117static struct osmo_fsm vgcs_bcc_fsm;
118static struct osmo_fsm vgcs_gcc_fsm;
119static struct osmo_fsm vgcs_bss_fsm;
120static struct osmo_fsm vgcs_cell_fsm;
121static struct osmo_fsm vgcs_mgw_ep_fsm;
122
123static __attribute__((constructor)) void vgcs_fsm_init(void)
124{
125 OSMO_ASSERT(osmo_fsm_register(&vgcs_bcc_fsm) == 0);
126 OSMO_ASSERT(osmo_fsm_register(&vgcs_gcc_fsm) == 0);
127 OSMO_ASSERT(osmo_fsm_register(&vgcs_bss_fsm) == 0);
128 OSMO_ASSERT(osmo_fsm_register(&vgcs_cell_fsm) == 0);
129 OSMO_ASSERT(osmo_fsm_register(&vgcs_mgw_ep_fsm) == 0);
130}
131
132const char *gsm44068_group_id_string(uint32_t callref)
133{
134 static char string[9];
135
136 snprintf(string, sizeof(string), "%08u", callref);
137 string[sizeof(string) - 1] = '\0';
138
139 return string;
140}
141
142/* Resolve ran peer from point-code */
143static struct ran_peer *ran_peer_for_pc(struct gsm_network *msc_network, int pc)
144{
145 struct sccp_ran_inst *sri;
146 struct osmo_sccp_addr addr = {};
147 struct ran_peer *rp;
148
149 sri = msc_network->a.sri;
150 if (!osmo_sccp_get_ss7(sri->sccp)) {
151 LOGP(DASCI, LOGL_ERROR, "No SS7???\n");
152 return NULL;
153 }
154 osmo_sccp_make_addr_pc_ssn(&addr, pc, sri->ran->ssn);
155 rp = ran_peer_find_by_addr(sri, &addr);
156
157 return rp;
158}
159
160/* Encode message and send towards BSC. */
161int ran_encode_and_send(struct osmo_fsm_inst *fi, struct ran_msg *ran_msg, struct ran_conn *conn, bool initial)
162{
163 struct msgb *l3_msg;
164 int rc;
165
166 l3_msg = ran_a_encode(fi, ran_msg);
167 if (!l3_msg) {
168 LOGP(DASCI, LOGL_ERROR, "ran_a_encode() failed.\n");
169 return -EINVAL;
170 }
171 rc = ran_conn_down_l2_co(conn, l3_msg, initial);
172 msgb_free(l3_msg);
173
174 return rc;
175}
176
177/* Transmit DTAP message to talker
178 * This is used for sending group/broadcast call control messages. */
179int tx_dtap_to_talker(struct vgcs_bss *bss, struct msgb *l3_msg)
180{
181 struct ran_msg ran_msg;
182 struct gsm48_hdr *gh = msgb_l3(l3_msg) ? : l3_msg->data;
183 uint8_t pdisc = gsm48_hdr_pdisc(gh);
184 int rc;
185
186
187 LOG_BSS(bss, LOGL_DEBUG, "Sending DTAP: %s %s\n",
188 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
189
190 ran_msg = (struct ran_msg){
191 .msg_type = RAN_MSG_DTAP,
192 .dtap = l3_msg,
193 };
194
195 rc = ran_encode_and_send(bss->fi, &ran_msg, bss->conn, false);
196
197 return rc;
198}
199
200/*
201 * GCC/BCC Message transcoding
202 */
203
204static void _add_cause_ie(struct msgb *msg, uint8_t cause, uint8_t *diag, uint8_t diag_len)
205{
206 uint8_t *ie = msgb_put(msg, 2 + diag_len);
207
208 ie[0] = 1 + diag_len;
209 ie[1] = cause;
210 if (diag && diag_len) {
211 ie[1] |= 0x80;
212 memcpy(ie + 2, diag, diag_len);
213 }
214}
215
216static void _add_callref_ie(struct msgb *msg, uint32_t callref, bool with_prio, uint8_t prio)
217{
218 uint32_t ie;
219
220 ie = callref << 5;
221 if (with_prio)
222 ie |= 0x10 | (prio << 1);
223 msgb_put_u32(msg, ie);
224}
225
226static int _msg_too_short(void)
227{
228 LOGP(DASCI, LOGL_ERROR, "MSG too short.\n");
229 return -EINVAL;
230}
231
232static int _ie_invalid(void)
233{
234 LOGP(DASCI, LOGL_ERROR, "IE invalid.\n");
235 return -EINVAL;
236}
237
238static int _rx_callref(uint8_t *ie, unsigned int remaining_len, uint32_t *callref, bool *with_prio, uint8_t *prio)
239{
240 uint8_t ie_len;
241
242 ie_len = sizeof(uint32_t);
243 if (remaining_len < ie_len)
244 return _msg_too_short();
245 *callref = osmo_load32be(ie) >> 5;
246 if (ie[3] & 0x10) {
247 *with_prio = true;
248 *prio = (ie[3] >> 1) & 0x7;
249 } else
250 *with_prio = false;
251
252 return ie_len;
253}
254
255/* 3GPP TS 44.068 Clause 8.1 */
256static int gsm44068_tx_connect(struct gsm_trans *trans, uint8_t pdisc, uint32_t callref, bool with_prio, uint8_t prio,
257 uint8_t oi, uint8_t talker_prio, bool with_sms, uint8_t sms_dc, uint8_t sms_gp)
258{
259 struct msgb *msg = gsm44068_msgb_alloc_name("GSM 44.068 TX CONNECT");
260 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
261 uint8_t ie;
262
263 gh->proto_discr = pdisc;
264 gh->msg_type = OSMO_GSM44068_MSGT_CONNECT;
265 _add_callref_ie(msg, callref, with_prio, prio);
266 ie = (talker_prio << 4) | oi;
267 msgb_put_u8(msg, ie);
268 if (with_sms) {
269 ie = OSMO_GSM44068_IEI_SMS_INDICATIONS | (sms_dc << 1) | sms_gp;
270 msgb_put_u8(msg, ie);
271 }
272
273 /* Send to calling subscriber, depending on the link he is. */
274 if (trans->msc_a)
275 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
276 if (trans->gcc.uplink_bss)
277 return tx_dtap_to_talker(trans->gcc.uplink_bss, msg);
278 msgb_free(msg);
279 return -EIO;
280}
281
282/* The Get Status procedure is not used by the current implementation.
283 * It is commented out, so it can be used in the future.
284 * The idea is to have a complete set of GCC/BCC message transcoding.
285 */
286#if 0
287/* 3GPP TS 44.068 Clause 8.2 */
288static int gsm44068_tx_get_status(struct gsm_trans *trans, uint8_t pdisc, struct osmo_mobile_identity *mi)
289{
290 struct msgb *msg = gsm44068_msgb_alloc_name("GSM 44.068 TX GET STATUS");
291 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
292
293 gh->proto_discr = pdisc;
294 gh->msg_type = OSMO_GSM44068_MSGT_GET_STATUS;
295 if (mi) {
296 uint8_t *l;
297 int rc;
298
299 l = msgb_tl_put(msg, OSMO_GSM44068_IEI_MOBILE_IDENTITY);
300 rc = osmo_mobile_identity_encode_msgb(msg, mi, false);
301 if (rc < 0) {
302 msgb_free(msg);
303 return -EINVAL;
304 }
305 *l = rc;
306 }
307
308 /* Send to calling subscriber, depending on the link he is. */
309 if (trans->msc_a)
310 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
311 if (trans->gcc.uplink_bss)
312 return tx_dtap_to_talker(trans->gcc.uplink_bss, msg);
313 msgb_free(msg);
314 return -EIO;
315}
316#endif
317
318/* 3GPP TS 44.068 Clause 8.3 and 8.3a */
319static int gsm44068_rx_immediate_setup(struct msgb *msg, uint8_t *talker_prio, uint8_t *key_seq,
320 struct gsm48_classmark2 *cm2, struct osmo_mobile_identity *mi,
321 uint32_t *callref, bool *with_prio, uint8_t *prio, char *user_user)
322{
323 struct gsm48_hdr *gh = msgb_l3(msg);
324 unsigned int remaining_len = msgb_l3len(msg) - sizeof(*gh);
325 uint8_t *ie = gh->data;
326 uint8_t ie_len;
327 uint64_t otdi;
328 int i;
329 int rc;
330
331 /* Talker priority / Cyphering key sequence */
332 if (remaining_len < 1)
333 return _msg_too_short();
334 *talker_prio = ie[0] & 0x07;
335 *key_seq = (ie[0] >> 4) & 0x07;
336 remaining_len -= 1;
337 ie += 1;
338
339 /* Mobile station classmark 2 */
340 if (remaining_len < 4)
341 return _msg_too_short();
342 ie_len = ie[0];
343 if (remaining_len < ie_len + 1)
344 return _msg_too_short();
345 if (ie_len != 3)
346 return _ie_invalid();
347 memcpy(cm2, ie + 1, ie_len);
348 remaining_len -= ie_len + 1;
349 ie += ie_len + 1;
350
351 /* Mobile indentity */
352 if (gh->msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP) {
353 /* IMMEDIATE SETUP uses IMSI/TMSI */
354 if (remaining_len < 2)
355 return _msg_too_short();
356 ie_len = ie[0];
357 if (remaining_len < ie_len + 1)
358 return _msg_too_short();
359 rc = osmo_mobile_identity_decode(mi, ie + 1, ie_len, false);
360 if (rc) {
361 LOGP(DMM, LOGL_ERROR, "Failure to decode Mobile Identity in GCC/BCC IMMEDDIATE SETUP"
362 " (rc=%d)\n", rc);
363 return -EINVAL;
364 }
365 remaining_len -= ie_len + 1;
366 ie += ie_len + 1;
367 } else {
368 /* IMMEDIATE SETUP 2 uses TMSI only */
369 if (remaining_len < 4)
370 return _msg_too_short();
371 mi->type = GSM_MI_TYPE_TMSI;
372 mi->tmsi = osmo_load32be(ie);
373 remaining_len -= 4;
374 ie += 4;
375 }
376
377 /* Call reference */
378 rc = _rx_callref(ie, remaining_len, callref, with_prio, prio);
379 if (rc < 0)
380 return rc;
381 remaining_len -= rc;
382 ie += rc;
383
384 /* OTID */
385 if (gh->msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2 && user_user) {
386 ie_len = 5;
387 if (remaining_len < ie_len)
388 return _msg_too_short();
389 otdi = osmo_load32be(ie + 1) | ((uint64_t)ie[0] << 32);
390
391 for (i = 0; i < 12; i++) {
392 user_user[i] = (otdi % 10) + '0';
393 otdi /= 10;
394 }
395 user_user[i] = '\0';
396 remaining_len -= ie_len;
397 ie += ie_len;
398 } else if (user_user)
399 user_user[0] = '\0';
400
401 return 0;
402}
403
404/* 3GPP TS 44.068 Clause 8.4 */
405static int gsm44068_tx_set_parameter(struct gsm_trans *trans, uint8_t pdisc, uint8_t da, uint8_t ua, uint8_t comm,
406 uint8_t oi)
407{
408 struct msgb *msg = gsm44068_msgb_alloc_name("GSM 44.068 TX SET PARAMETER");
409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
410 uint8_t ie;
411
412 gh->proto_discr = pdisc;
413 gh->msg_type = OSMO_GSM44068_MSGT_SET_PARAMETER;
414 ie = (da << 3) | (ua << 2) | (comm << 1) | oi;
415 msgb_put_u8(msg, ie);
416
417 /* Send to calling subscriber, depending on the link he is. */
418 if (trans->msc_a)
419 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
420 if (trans->gcc.uplink_bss)
421 return tx_dtap_to_talker(trans->gcc.uplink_bss, msg);
422 msgb_free(msg);
423 return -EIO;
424}
425
426/* 3GPP TS 44.068 Clause 8.5 */
427static int gsm44068_rx_setup(struct msgb *msg, bool *with_talker_prio, uint8_t *talker_prio,
428 uint32_t *callref, bool *with_prio, uint8_t *prio, char *user_user)
429{
430 struct gsm48_hdr *gh = msgb_l3(msg);
431 unsigned int remaining_len = msgb_l3len(msg) - sizeof(*gh);
432 uint8_t *ie = gh->data;
433 struct tlv_parsed tp;
434 struct tlv_p_entry *tlv;
435 int rc;
436
437 /* Call reference */
438 rc = _rx_callref(ie, remaining_len, callref, with_prio, prio);
439 if (rc < 0)
440 return rc;
441 remaining_len -= rc;
442 ie += rc;
443
444 rc = tlv_parse(&tp, &osmo_gsm44068_att_tlvdef, ie, remaining_len, 0, 0);
445 if (rc < 0)
446 return _ie_invalid();
447
448 /* User-user */
449 tlv = TLVP_GET(&tp, OSMO_GSM44068_IEI_USER_USER);
450 if (tlv && tlv->len && tlv->len <= 1 + 12 && user_user) {
451 memcpy(user_user, tlv->val, tlv->len - 1);
452 user_user[tlv->len - 1] = '\0';
453 }
454
455 /* Talker priority */
456 tlv = TLVP_GET(&tp, OSMO_GSM44068_IEI_TALKER_PRIORITY);
457 if (tlv && tlv->len) {
458 *with_talker_prio = true;
459 *talker_prio = tlv->val[0] & 0x07;
460 } else
461 *with_talker_prio = false;
462
463 return 0;
464}
465
466/* 3GPP TS 44.068 Clause 8.6 */
467static int gsm44068_rx_status(struct msgb *msg, uint8_t *cause, uint8_t *diag, uint8_t *diag_len,
468 bool *with_call_state, enum osmo_gsm44068_call_state *call_state,
469 bool *with_state_attrs, uint8_t *da, uint8_t *ua, uint8_t *comm, uint8_t *oi)
470{
471 struct gsm48_hdr *gh = msgb_l3(msg);
472 unsigned int remaining_len = msgb_l3len(msg) - sizeof(*gh);
473 uint8_t *ie = gh->data;
474 uint8_t ie_len;
475 struct tlv_parsed tp;
476 struct tlv_p_entry *tlv;
477 int rc;
478
479 /* Cause */
480 if (remaining_len < 2 || ie[0] < remaining_len - 2)
481 return _msg_too_short();
482 ie_len = ie[0];
483 if (remaining_len < ie_len + 1)
484 return _msg_too_short();
485 if (ie_len < 1)
486 return _ie_invalid();
487 *cause = ie[1] & 0x7f;
488 *diag_len = ie_len - 1;
489 if (*diag_len)
490 memcpy(diag, ie + 2, ie_len - 1);
491 remaining_len -= ie_len + 1;
492 ie += ie_len + 1;
493
494 rc = tlv_parse(&tp, &osmo_gsm44068_att_tlvdef, ie, remaining_len, 0, 0);
495 if (rc < 0)
496 return _ie_invalid();
497
498 /* Call state */
499 tlv = TLVP_GET(&tp, OSMO_GSM44068_IEI_CALL_STATE);
500 if (tlv) {
501 *with_call_state = true;
502 *call_state = tlv->val[0] & 0x7;
503 } else
504 *with_call_state = false;
505
506 /* State attributes */
507 tlv = TLVP_GET(&tp, OSMO_GSM44068_IEI_STATE_ATTRIBUTES);
508 if (tlv) {
509 *with_state_attrs = true;
510 *da = (tlv->val[0] >> 3) & 0x1;
511 *ua = (tlv->val[0] >> 2) & 0x1;
512 *comm = (tlv->val[0] >> 1) & 0x1;
513 *oi = tlv->val[0] & 0x1;
514 } else
515 *with_state_attrs = false;
516
517 return 0;
518}
519
520/* 3GPP TS 44.068 Clause 8.7 and 8.8 */
521static int gsm44068_tx_termination(struct msc_a *msc_a, struct vgcs_bss *bss, uint8_t pdisc, uint8_t msg_type,
522 uint8_t cause, uint8_t *diag, uint8_t diag_len)
523{
524 struct msgb *msg = gsm44068_msgb_alloc_name("GSM 44.068 TX TERMINATION");
525 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
526
527 gh->proto_discr = pdisc;
528 gh->msg_type = msg_type;
529 _add_cause_ie(msg, cause, diag, diag_len);
530
531 /* Send to calling subscriber, depending on the link he is. */
532 if (msc_a)
533 return msc_a_tx_dtap_to_i(msc_a, msg);
534 if (bss)
535 return tx_dtap_to_talker(bss, msg);
536 msgb_free(msg);
537 return -EIO;
538}
539
540/* 3GPP TS 44.068 Clause 8.9 */
541static int gsm44068_rx_termination_req(struct msgb *msg, uint32_t *callref, bool *with_prio, uint8_t *prio,
542 bool *with_talker_prio, uint8_t *talker_prio)
543{
544 struct gsm48_hdr *gh = msgb_l3(msg);
545 unsigned int remaining_len = msgb_l3len(msg) - sizeof(*gh);
546 uint8_t *ie = gh->data;
547 struct tlv_parsed tp;
548 struct tlv_p_entry *tlv;
549 int rc;
550
551 /* Call reference */
552 rc = _rx_callref(ie, remaining_len, callref, with_prio, prio);
553 if (rc < 0)
554 return rc;
555 remaining_len -= rc;
556 ie += rc;
557
558 rc = tlv_parse(&tp, &osmo_gsm44068_att_tlvdef, ie, remaining_len, 0, 0);
559 if (rc < 0)
560 return _ie_invalid();
561
562 /* Talker priority */
563 tlv = TLVP_GET(&tp, OSMO_GSM44068_IEI_TALKER_PRIORITY);
564 if (tlv && tlv->len) {
565 *with_talker_prio = true;
566 *talker_prio = tlv->val[0] & 0x07;
567 } else
568 *with_talker_prio = false;
569
570 return 0;
571}
572
573/*
574 * GCC/BCC state machine - handles calling subscriber process
575 */
576
577static const struct value_string vgcs_gcc_fsm_event_names[] = {
578 OSMO_VALUE_STRING(VGCS_GCC_EV_NET_SETUP),
579 OSMO_VALUE_STRING(VGCS_GCC_EV_NET_TERM),
580 OSMO_VALUE_STRING(VGCS_GCC_EV_USER_SETUP),
581 OSMO_VALUE_STRING(VGCS_GCC_EV_USER_TERM),
582 OSMO_VALUE_STRING(VGCS_GCC_EV_BSS_ESTABLISHED),
583 OSMO_VALUE_STRING(VGCS_GCC_EV_BSS_ASSIGN_CPL),
584 OSMO_VALUE_STRING(VGCS_GCC_EV_BSS_ASSIGN_FAIL),
585 OSMO_VALUE_STRING(VGCS_GCC_EV_BSS_RELEASED),
586 OSMO_VALUE_STRING(VGCS_GCC_EV_TIMEOUT),
587 { }
588};
589
590static int gcc_establish_bss(struct gsm_trans *trans)
591{
592 struct gsm_network *net = trans->net;
593 struct vgcs_mgw_ep *mgw = NULL;
594 struct mgcp_client *mgcp_client;
595 struct gcr *gcr;
596 struct gcr_bss *b;
597 struct gcr_cell *c;
598 struct vgcs_bss *bss;
599 struct vgcs_bss_cell *cell;
600 struct osmo_fsm_inst *fi;
601 struct ran_peer *rp;
602
603 /* Failure should not happen, because it has been checked before. */
604 gcr = gcr_by_callref(trans->net, trans->type, trans->callref);
605 if (!gcr)
606 return -EINVAL;
607
608 /* Allocate MGW endpoint. */
609 mgcp_client = mgcp_client_pool_get(trans->net->mgw.mgw_pool);
610 if (!mgcp_client) {
611 LOG_GCC(trans, LOGL_ERROR, "No MGW client, please check config.\n");
612 goto err_mgw;
613 }
614 fi = osmo_fsm_inst_alloc(&vgcs_mgw_ep_fsm, net, NULL, LOGL_DEBUG, NULL);
615 if (!fi) {
616 LOG_GCC(trans, LOGL_ERROR, "No memory for VGCS MSG state machine.\n");
617 goto err_mgw;
618 }
619 osmo_fsm_inst_update_id(fi, "vgcs-mgw-ep");
620 osmo_fsm_inst_state_chg(fi, VGCS_MGW_EP_ST_ACTIVE, 0, 0);
621 mgw = talloc_zero(fi, struct vgcs_mgw_ep);
622 if (!mgw) {
623 LOG_GCC(trans, LOGL_ERROR, "No memory for MGW ep structure.\n");
624 osmo_fsm_inst_free(fi);
625 goto err_mgw;
626 }
627 mgw->fi = fi;
628 fi->priv = mgw;
629 INIT_LLIST_HEAD(&mgw->cell_list);
630 mgw->mgw_ep = osmo_mgcpc_ep_alloc(mgw->fi, VGCS_MGW_EP_EV_FREE,
631 mgcp_client, trans->net->mgw.tdefs, mgw->fi->id,
632 "%s", mgcp_client_rtpbridge_wildcard(mgcp_client));
633 if (!mgw->mgw_ep) {
634 LOG_GCC(trans, LOGL_ERROR, "No memory for MGW endpoint state machine.\n");
635 goto err_mgw;
636 }
637
638 /* Create BSS list structures. */
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200639 LOG_GCC(trans, LOGL_DEBUG, "Creating BSS list structure with cell list structures.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +0200640 llist_for_each_entry(b, &gcr->bss_list, list) {
641 LOG_GCC(trans, LOGL_DEBUG, " -> BSS with PC %s.\n", osmo_ss7_pointcode_print(NULL, b->pc));
642 /* Resolve ran_peer. */
643 rp = ran_peer_for_pc(trans->net, b->pc);
644 if (!rp) {
645 LOG_GCC(trans, LOGL_ERROR, "Failed to resolve point code %s, skipping BSS!\n",
646 osmo_ss7_pointcode_print(NULL, b->pc));
647 continue;
648 }
649 /* Create state machine. */
650 fi = osmo_fsm_inst_alloc(&vgcs_bss_fsm, net, NULL, LOGL_DEBUG, NULL);
651 if (!fi) {
652 LOG_GCC(trans, LOGL_ERROR, "No memory for state machine.\n");
653 break;
654 }
655 /* Create call structure. */
656 bss = talloc_zero(fi, struct vgcs_bss);
657 if (!bss) {
658 LOG_GCC(trans, LOGL_ERROR, "No memory for BSS call structure.\n");
659 osmo_fsm_inst_free(fi);
660 break;
661 }
662 bss->fi = fi;
663 fi->priv = bss;
664 INIT_LLIST_HEAD(&bss->cell_list);
665 bss->trans = trans;
666 bss->trans_type = trans->type;
667 bss->callref = trans->callref;
668 bss->pc = b->pc;
669 /* Create ran connection. */
670 bss->conn = ran_conn_create_outgoing(rp);
671 if (!bss->conn) {
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200672 LOG_GCC(trans, LOGL_ERROR, "Failed to create RAN connection.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +0200673 osmo_fsm_inst_free(bss->fi);
674 continue;
675 }
676 bss->conn->vgcs.bss = bss;
677 /* Create cell list structures. */
678 llist_for_each_entry(c, &b->cell_list, list) {
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200679 LOG_GCC(trans, LOGL_DEBUG, " -> Cell ID %d.\n", c->cell_id);
Andreas Eversberge24636c2023-04-23 12:20:55 +0200680 /* Create state machine. */
681 fi = osmo_fsm_inst_alloc(&vgcs_cell_fsm, net, NULL, LOGL_DEBUG, NULL);
682 if (!fi) {
683 LOG_GCC(trans, LOGL_ERROR, "No memory for state machine.\n");
684 break;
685 }
686 /* Create cell structure. */
687 cell = talloc_zero(fi, struct vgcs_bss_cell);
688 if (!cell) {
689 LOG_GCC(trans, LOGL_ERROR, "No memory for BSS cell structure.\n");
690 osmo_fsm_inst_free(fi);
691 break;
692 }
693 cell->fi = fi;
694 fi->priv = cell;
695 osmo_fsm_inst_update_id_f(cell->fi, "vgcs-cell-%d", c->cell_id);
696 cell->trans_type = trans->type;
697 cell->callref = trans->callref;
698 cell->pc = b->pc;
699 cell->cell_id = c->cell_id;
700 cell->call_id = trans->call_id;
701 /* Create ran connection. */
702 cell->conn = ran_conn_create_outgoing(rp);
703 if (!cell->conn) {
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200704 LOG_GCC(trans, LOGL_ERROR, "Failed to create RAN connection.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +0200705 osmo_fsm_inst_free(cell->fi);
706 continue;
707 }
708 cell->conn->vgcs.cell = cell;
709 /* Attach to cell list of BSS and MGW endpoint */
710 llist_add_tail(&cell->list_bss, &bss->cell_list);
711 cell->bss = bss;
712 llist_add_tail(&cell->list_mgw, &mgw->cell_list);
713 cell->mgw = mgw;
714 }
715 /* No cell? */
716 if (llist_empty(&bss->cell_list)) {
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200717 LOG_GCC(trans, LOGL_DEBUG, " -> No Cell in this BSS.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +0200718 osmo_fsm_inst_free(bss->fi);
719 break;
720 }
721 /* Attach to transaction list */
722 llist_add_tail(&bss->list, &trans->gcc.bss_list);
723 /* Trigger VGCS/VBS SETUP */
724 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_SETUP, NULL);
725 }
726 /* No BSS? */
727 if (llist_empty(&trans->gcc.bss_list)) {
728 /* Also destroy MGW, because this list is empty too! */
Andreas Eversbergcc7b7b82023-07-27 13:17:50 +0200729 LOG_GCC(trans, LOGL_NOTICE, "No BSS found, please check your VTY configuration and add cells.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +0200730 goto err_mgw;
731 }
732 return 0;
733
734err_mgw:
735 if (mgw) {
736 if (mgw->mgw_ep) {
737 /* This will also free FSM instance and vgcs_mgw_ep structure. */
738 osmo_fsm_inst_dispatch(mgw->fi, VGCS_MGW_EP_EV_CLEAR, NULL);
739 return -EINVAL;
740 }
741 osmo_fsm_inst_free(mgw->fi);
742 }
743 return -EINVAL;
744}
745
746/* Send Assignment Request to the calling subscriber.
747 * This is used to assign the subscriber from early assigned channel to the VGCS/VBS channel. */
748static int gcc_assign(struct gsm_trans *trans)
749{
750 struct ran_msg tx_ran_msg;
751 struct gsm0808_channel_type channel_type;
752 struct vgcs_bss *bss = NULL, *b;
753
754 /* No assignment, because the calling subscriber is already assigned or there is no calling subscriber. */
755 if (!trans->msc_a)
756 return 0;
757
758 /* Check calling subscriber's MSC */
759 struct ran_conn *conn = msub_ran_conn(trans->msc_a->c.msub);
760 if (!conn) {
761 LOG_GCC(trans, LOGL_ERROR, "Calling subscriber has no ran_conn????\n");
762 return -EINVAL;
763 }
764 llist_for_each_entry(b, &trans->gcc.bss_list, list) {
765 if (osmo_sccp_addr_ri_cmp(&conn->ran_peer->peer_addr, &b->conn->ran_peer->peer_addr))
766 continue;
767 bss = b;
768 break;
769 }
770 if (!bss) {
771 LOG_GCC(trans, LOGL_ERROR, "Calling subscriber comes from BSC that has no VGCS call.\n");
772 return -EINVAL;
773 }
774
775 /* For now we support GSM/FR V1 only. This shall be supported by all MS. */
776 channel_type = (struct gsm0808_channel_type) {
777 .ch_indctr = GSM0808_CHAN_SPEECH,
778 .ch_rate_type = GSM0808_SPEECH_FULL_BM,
779 .perm_spch_len = 1,
780 .perm_spch[0] = GSM0808_PERM_FR1,
781 };
782
783 /* Send assignment to VGCS channel */
784 tx_ran_msg = (struct ran_msg) {
785 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
786 .assignment_command = {
787 .channel_type = &channel_type,
788 .callref_present = true,
789 .callref = {
790 .sf = (trans->type == TRANS_GCC),
791 },
792 },
793 };
794 osmo_store32be_ext(trans->callref >> 3, &tx_ran_msg.assignment_command.callref.call_ref_hi, 3);
795 tx_ran_msg.assignment_command.callref.call_ref_lo = trans->callref & 0x7;
796 if (msc_a_ran_down(trans->msc_a, MSC_ROLE_I, &tx_ran_msg)) {
797 LOG_GCC(trans, LOGL_ERROR, "Cannot send Assignment\n");
798 return -EIO;
799 }
800
801 /* Assign Talker to BSS of the calling subscriber. */
802 trans->gcc.uplink_bss = bss;
803
804 return 0;
805}
806
807/* Send CONNECT to the calling subscriber. */
808static void gcc_connect(struct gsm_trans *trans)
809{
810 uint8_t pdisc = (trans->type == TRANS_GCC) ? GSM48_PDISC_GROUP_CC : GSM48_PDISC_BCAST_CC;
811 int rc;
812
813 /* Send CONNECT towards MS. */
814 rc = gsm44068_tx_connect(trans,
815 pdisc | (trans->transaction_id << 4),
816 trans->callref, 0, 0, 1, 0, 0, 0, 0);
817 if (rc < 0)
818 LOG_GCC(trans, LOGL_ERROR, "Failed to send CONNECT towards MS. Continue anyway.\n");
819}
820
821/* Release dedicated (SDCCH) channel of calling subscriber after assigning to VGCS */
822static void release_msc_a(struct gsm_trans *trans)
823{
824 struct msc_a *msc_a = trans->msc_a;
825
826 if (!msc_a)
827 return;
828
829 trans->msc_a = NULL;
830 switch (trans->type) {
831 case TRANS_GCC:
832 msc_a_put(msc_a, MSC_A_USE_GCC);
833 break;
834 case TRANS_BCC:
835 msc_a_put(msc_a, MSC_A_USE_BCC);
836 break;
837 default:
838 break;
839 }
840}
841
842/* Send TERMINATE to the calling/talking subscriber, then destroy transaction. */
843static void gcc_terminate_and_destroy(struct gsm_trans *trans, enum osmo_gsm44068_cause cause)
844{
845 uint8_t pdisc = (trans->type == TRANS_GCC) ? GSM48_PDISC_GROUP_CC : GSM48_PDISC_BCAST_CC;
846 int rc;
847
848 /* Send TERMINATION towards MS. */
849 rc = gsm44068_tx_termination(trans->msc_a, trans->gcc.uplink_bss,
850 pdisc | (trans->transaction_id << 4),
851 OSMO_GSM44068_MSGT_TERMINATION,
852 cause, NULL, 0);
853 if (rc < 0)
854 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS. Continue anyway.\n");
855
856 /* Destroy transaction, note that also _gsm44068_gcc_trans_free() will be called by trans_free().
857 * There the complete state machine is destroyed. */
858 trans->callref = 0;
859 trans_free(trans);
860}
861
862/* Start inactivity timer.
863 * This timer is used to terminate the call, if the radio connection to the caller gets lost. */
864static void start_inactivity_timer(struct gsm_trans *trans)
865{
866 if (trans->gcc.inactivity_to) {
867 LOG_GCC(trans, LOGL_DEBUG, "Set inactivity timer to %d seconds.\n", trans->gcc.inactivity_to);
868 osmo_timer_schedule(&trans->gcc.timer_inactivity, trans->gcc.inactivity_to, 0);
869 }
870}
871
872static void stop_inactivity_timer(struct gsm_trans *trans)
873{
874 if (osmo_timer_pending(&trans->gcc.timer_inactivity)) {
875 LOG_GCC(trans, LOGL_DEBUG, "Stop pending inactivity timer.\n");
876 osmo_timer_del(&trans->gcc.timer_inactivity);
877 }
878}
879
880static void inactivity_timer_cb(void *data)
881{
882 struct gsm_trans *trans = data;
883
884 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_TIMEOUT, NULL);
885}
886
887/* Set the parameters of the talker. (downlink mute/unmute, uplink unmute, COMM=T, originator) */
888static int set_parameter(struct gsm_trans *trans)
889{
890 uint8_t pdisc = (trans->type == TRANS_GCC) ? GSM48_PDISC_GROUP_CC : GSM48_PDISC_BCAST_CC;
891 int rc;
892
Andreas Eversberg1c980312023-07-27 13:18:27 +0200893 rc = gsm44068_tx_set_parameter(trans, pdisc | (trans->transaction_id << 4),
894 !trans->gcc.mute_talker, 1, 1, trans->gcc.uplink_originator);
Andreas Eversberge24636c2023-04-23 12:20:55 +0200895 if (rc < 0)
896 LOG_GCC(trans, LOGL_ERROR, "Failed to send SET PARAMETER towards MS.\n");
897 return rc;
898}
899
900/* Check in which cell the uplink is used and set "uplink_cell". */
901static int set_uplink_cell(struct vgcs_bss *bss, struct gsm0808_cell_id *cell_id_ie, uint16_t cell_id)
902{
903 struct vgcs_bss_cell *cell;
904
905 if (cell_id_ie) {
906 /* Get cell ID to determine talker channel. */
907 switch (cell_id_ie->id_discr) {
908 case CELL_IDENT_CI:
909 cell_id = cell_id_ie->id.ci;
910 break;
911 case CELL_IDENT_LAC_AND_CI:
912 cell_id = cell_id_ie->id.lac_and_ci.ci;
913 break;
914 default:
915 LOG_BSS(bss, LOGL_DEBUG, "Cannot idenitfy cell, please fix!\n");
916 return -EINVAL;
917 }
918 }
919
920 /* Search for cell ID. */
921 bss->trans->gcc.uplink_cell = NULL;
922 llist_for_each_entry(cell, &bss->cell_list, list_bss) {
923 if (cell->cell_id == cell_id) {
924 LOG_BSS(bss, LOGL_DEBUG, "Talker is talking on cell %d.\n", cell->cell_id);
925 bss->trans->gcc.uplink_cell = cell;
926 return 0;
927 }
928 }
929
930 LOG_BSS(bss, LOGL_DEBUG, "Cell ID %d is not in list of current BSS, please fix!\n", cell_id);
931 return -EINVAL;
932}
933
934/* Set the MGW conference mode.
935 * All cells are listening to the conference. If there is a talker, this cell is also transmitting to the conference. */
936static int set_mgw_conference(struct gsm_trans *trans)
937{
938 struct vgcs_bss *bss;
939 struct vgcs_bss_cell *cell;
940 struct rtp_stream *rtps;
941 int rc;
942
943 /* All cells without talker are listening */
944 llist_for_each_entry(bss, &trans->gcc.bss_list, list) {
945 llist_for_each_entry(cell, &bss->cell_list, list_bss) {
946 if (!(rtps = cell->rtps))
947 continue;
948 if (rtps->crcx_conn_mode != MGCP_CONN_SEND_ONLY) {
949 LOG_CELL(cell, LOGL_DEBUG, "Setting cell %d into listening mode.\n", cell->cell_id);
950 rtp_stream_set_mode(rtps, MGCP_CONN_SEND_ONLY);
951 rc = rtp_stream_commit(rtps);
952 if (rc < 0)
953 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream "
954 "for cell %d.\n", cell->cell_id);
955 }
956 }
957 }
958
959 if (trans->gcc.uplink_cell && trans->gcc.uplink_cell->rtps) {
960 cell = trans->gcc.uplink_cell;
961 rtps = cell->rtps;
962 LOG_CELL(cell, LOGL_DEBUG, "Setting cell %d into listening mode.\n", cell->cell_id);
963 rtp_stream_set_mode(rtps, MGCP_CONN_CONFECHO);
964 rc = rtp_stream_commit(rtps);
965 if (rc < 0)
966 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream "
967 "for cell %d.\n", cell->cell_id);
968 }
969
970 return 0;
971}
972
973static void _assign_complete(struct gsm_trans *trans, bool send_connect)
974{
975 uint16_t cell_id;
976
Andreas Eversberge2a40882023-07-19 09:56:53 +0200977 OSMO_ASSERT(trans->msc_a);
978
Andreas Eversberge24636c2023-04-23 12:20:55 +0200979 /* Change state. */
980 osmo_fsm_inst_state_chg(trans->gcc.fi, VGCS_GCC_ST_N2_CALL_ACTIVE, 0, 0);
981 /* Get cell ID. */
Andreas Eversberge2a40882023-07-19 09:56:53 +0200982 cell_id = trans->msc_a->via_cell.cell_identity;
Andreas Eversberge24636c2023-04-23 12:20:55 +0200983 /* Releasing dedicated channel. */
984 release_msc_a(trans);
985 /* Send CONNECT to the calling subscriber. */
986 if (send_connect)
987 gcc_connect(trans);
988 /* Set parameter. */
989 set_parameter(trans);
990 /* Start inactivity timer, if uplink is free. */
991 if (!trans->gcc.uplink_busy)
992 start_inactivity_timer(trans);
993 /* Set cell of current talker. */
994 set_uplink_cell(trans->gcc.uplink_bss, NULL, cell_id);
995 /* Set MGW conference. */
996 set_mgw_conference(trans);
997}
998
999#define CONNECT_OPTION false
1000
1001static void vgcs_gcc_fsm_n0_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1002{
1003 struct gsm_trans *trans = fi->priv;
1004 int rc;
1005
1006 switch (event) {
1007 case VGCS_GCC_EV_NET_SETUP:
1008 /* Establish call towards all BSSs. */
1009 LOG_GCC(trans, LOGL_DEBUG, "Setup by network, trying to establish cells.\n");
1010 rc = gcc_establish_bss(trans);
1011 if (rc < 0) {
1012 LOG_GCC(trans, LOGL_NOTICE, "Failed to setup call to any cell.\n");
1013 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1014 break;
1015 }
1016 /* Keep state until established or released. */
1017 break;
1018 case VGCS_GCC_EV_NET_TERM:
1019 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1020 /* Destroy group call in all cells. */
1021 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1022 break;
1023 case VGCS_GCC_EV_USER_SETUP:
1024 LOG_GCC(trans, LOGL_DEBUG, "Setup by MS, trying to establish cells.\n");
1025 /* Change state. */
1026 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N1_CALL_INITIATED, 0, 0);
1027 /* Establish call towards all BSSs. */
1028 rc = gcc_establish_bss(trans);
1029 if (rc < 0) {
1030 LOG_GCC(trans, LOGL_NOTICE, "Failed to setup call to any cell.\n");
1031 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1032 break;
1033 }
1034 if (CONNECT_OPTION) {
1035 /* Send CONNECT to the calling subscriber. */
1036 gcc_connect(trans);
1037 /* Change state. */
1038 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N3_CALL_EST_PROC, 0, 0);
1039 }
1040 break;
1041 case VGCS_GCC_EV_BSS_ESTABLISHED:
1042 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, sending CONNECT to caller.\n");
1043 /* Change state. */
1044 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N2_CALL_ACTIVE, 0, 0);
1045 /* Start inactivity timer, if uplink is free. */
1046 if (!trans->gcc.uplink_busy)
1047 start_inactivity_timer(trans);
1048 break;
1049 case VGCS_GCC_EV_BSS_RELEASED:
1050 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1051 /* Send TERMINATE to the calling subscriber. */
1052 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1053 break;
1054 default:
1055 OSMO_ASSERT(false);
1056 }
1057}
1058
1059static void vgcs_gcc_fsm_n1_call_initiated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1060{
1061 struct gsm_trans *trans = fi->priv;
1062 int rc;
1063
1064 switch (event) {
1065 case VGCS_GCC_EV_NET_TERM:
1066 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1067 /* Destroy group call in all cells. */
1068 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1069 break;
1070 case VGCS_GCC_EV_USER_TERM:
1071 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1072 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1073 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1074 break;
1075 case VGCS_GCC_EV_BSS_ESTABLISHED:
1076 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, assign caller to VGCS.\n");
1077 /* Send assignment to the calling subscriber. */
1078 rc = gcc_assign(trans);
1079 if (rc < 0) {
1080 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1081 break;
1082 }
1083 break;
1084 case VGCS_GCC_EV_BSS_ASSIGN_CPL:
1085 LOG_GCC(trans, LOGL_DEBUG, "Assignment complete, sending CONNECT to caller, releasing channel.\n");
1086 /* Handle assignment complete */
1087 _assign_complete(trans, true);
1088 break;
1089 case VGCS_GCC_EV_BSS_ASSIGN_FAIL:
1090 LOG_GCC(trans, LOGL_DEBUG, "Assignment failed, releasing call.\n");
1091 /* Send TERMINATE to the calling subscriber. */
1092 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1093 break;
1094 case VGCS_GCC_EV_BSS_RELEASED:
1095 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1096 /* Send TERMINATE to the calling subscriber. */
1097 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1098 break;
1099 default:
1100 OSMO_ASSERT(false);
1101 }
1102}
1103
1104static void vgcs_gcc_fsm_n2_call_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1105{
1106 struct gsm_trans *trans = fi->priv;
1107
1108 switch (event) {
1109 case VGCS_GCC_EV_NET_TERM:
1110 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1111 /* Destroy group call in all cells. */
1112 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1113 break;
1114 case VGCS_GCC_EV_USER_TERM:
1115 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1116 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1117 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1118 break;
1119 case VGCS_GCC_EV_BSS_RELEASED:
1120 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1121 /* Send TERMINATE to the calling subscriber. */
1122 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1123 break;
1124 case VGCS_GCC_EV_TIMEOUT:
1125 LOG_GCC(trans, LOGL_DEBUG, "Termination by inactivity timer, destroying call.\n");
1126 /* Destroy group call in all cells. */
1127 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1128 break;
1129 default:
1130 OSMO_ASSERT(false);
1131 }
1132}
1133
1134static void vgcs_gcc_fsm_n3_call_est_proc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1135{
1136 struct gsm_trans *trans = fi->priv;
1137 int rc;
1138
1139 switch (event) {
1140 case VGCS_GCC_EV_NET_TERM:
1141 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1142 /* Destroy group call in all cells. */
1143 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1144 break;
1145 case VGCS_GCC_EV_USER_TERM:
1146 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1147 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1148 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1149 break;
1150 case VGCS_GCC_EV_BSS_ESTABLISHED:
1151 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, assign caller to VGCS.\n");
1152 /* Send assignment to the calling subscriber. */
1153 rc = gcc_assign(trans);
1154 if (rc < 0) {
1155 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1156 break;
1157 }
1158 break;
1159 case VGCS_GCC_EV_BSS_ASSIGN_CPL:
1160 LOG_GCC(trans, LOGL_DEBUG, "Assignment complete, sending CONNECT to caller, releasing channel.\n");
1161 /* Handle assignment complete */
1162 _assign_complete(trans, false);
1163 break;
1164 case VGCS_GCC_EV_BSS_ASSIGN_FAIL:
1165 LOG_GCC(trans, LOGL_DEBUG, "Assignment failed, releasing call.\n");
1166 /* Send TERMINATE to the calling subscriber. */
1167 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1168 break;
1169 case VGCS_GCC_EV_BSS_RELEASED:
1170 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1171 /* Send TERMINATE to the calling subscriber. */
1172 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1173 break;
1174 default:
1175 OSMO_ASSERT(false);
1176 }
1177}
1178
1179static const struct osmo_fsm_state vgcs_gcc_fsm_states[] = {
1180 [VGCS_GCC_ST_N0_NULL] = {
1181 .name = "NULL (N0)",
1182 .in_event_mask = S(VGCS_GCC_EV_NET_SETUP) |
1183 S(VGCS_GCC_EV_NET_TERM) |
1184 S(VGCS_GCC_EV_USER_SETUP) |
1185 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1186 S(VGCS_GCC_EV_BSS_RELEASED),
1187 .out_state_mask = S(VGCS_GCC_ST_N1_CALL_INITIATED) |
1188 S(VGCS_GCC_ST_N2_CALL_ACTIVE),
1189 .action = vgcs_gcc_fsm_n0_null,
1190 },
1191 [VGCS_GCC_ST_N1_CALL_INITIATED] = {
1192 .name = "CALL INITATED (N1)",
1193 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1194 S(VGCS_GCC_EV_USER_TERM) |
1195 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1196 S(VGCS_GCC_EV_BSS_ASSIGN_CPL) |
1197 S(VGCS_GCC_EV_BSS_ASSIGN_FAIL) |
1198 S(VGCS_GCC_EV_BSS_RELEASED),
1199 .out_state_mask = S(VGCS_GCC_ST_N0_NULL) |
1200 S(VGCS_GCC_ST_N2_CALL_ACTIVE) |
1201 S(VGCS_GCC_ST_N3_CALL_EST_PROC),
1202 .action = vgcs_gcc_fsm_n1_call_initiated,
1203 },
1204 [VGCS_GCC_ST_N2_CALL_ACTIVE] = {
1205 .name = "CALL ACTIVE (N2)",
1206 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1207 S(VGCS_GCC_EV_USER_TERM) |
1208 S(VGCS_GCC_EV_BSS_RELEASED) |
1209 S(VGCS_GCC_EV_TIMEOUT),
1210 .out_state_mask = S(VGCS_GCC_ST_N0_NULL),
1211 .action = vgcs_gcc_fsm_n2_call_active,
1212 },
1213 [VGCS_GCC_ST_N3_CALL_EST_PROC] = {
1214 .name = "CALL EST PROCEEDING (N3)",
1215 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1216 S(VGCS_GCC_EV_USER_TERM) |
1217 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1218 S(VGCS_GCC_EV_BSS_ASSIGN_CPL) |
1219 S(VGCS_GCC_EV_BSS_ASSIGN_FAIL) |
1220 S(VGCS_GCC_EV_BSS_RELEASED),
1221 .out_state_mask = S(VGCS_GCC_ST_N2_CALL_ACTIVE) |
1222 S(VGCS_GCC_ST_N0_NULL),
1223 .action = vgcs_gcc_fsm_n3_call_est_proc,
1224 },
1225 // We don't need a state to wait for the group call to be terminated in all cells
1226};
1227
1228static struct osmo_fsm vgcs_bcc_fsm = {
1229 .name = "bcc",
1230 .states = vgcs_gcc_fsm_states,
1231 .num_states = ARRAY_SIZE(vgcs_gcc_fsm_states),
1232 .log_subsys = DBCC,
1233 .event_names = vgcs_gcc_fsm_event_names,
1234};
1235
1236static struct osmo_fsm vgcs_gcc_fsm = {
1237 .name = "gcc",
1238 .states = vgcs_gcc_fsm_states,
1239 .num_states = ARRAY_SIZE(vgcs_gcc_fsm_states),
1240 .log_subsys = DGCC,
1241 .event_names = vgcs_gcc_fsm_event_names,
1242};
1243
1244const char *vgcs_bcc_gcc_state_name(struct osmo_fsm_inst *fi)
1245{
1246 return vgcs_gcc_fsm_states[fi->state].name;
1247}
1248
1249static int update_uplink_state(struct vgcs_bss *bss, bool uplink_busy);
1250
1251/* Receive RR messages from calling subscriber, prior assignment to VGCS/VBS. */
1252int gsm44068_rcv_rr(struct msc_a *msc_a, struct msgb *msg)
1253{
1254 struct gsm_trans *trans = NULL;
1255 struct gsm48_hdr *gh;
1256 uint8_t msg_type;
1257
1258 gh = msgb_l3(msg);
1259 msg_type = gsm48_hdr_msg_type(gh);
1260
1261 /* Find transaction. */
1262 trans = trans_find_by_type(msc_a, TRANS_GCC);
1263 if (!trans)
1264 trans = trans_find_by_type(msc_a, TRANS_BCC);
1265
1266 if (!trans) {
1267 LOG_GCC(trans, LOGL_ERROR, "No VGCS/VBS transaction.\n");
1268 return -EINVAL;
1269 }
1270
1271 /* In case the phone releases uplink prior being assigned to a VGCS */
1272 if (msg_type == GSM48_MT_RR_UPLINK_RELEASE) {
1273 struct vgcs_bss *bss;
1274
1275 LOG_GCC(trans, LOGL_INFO, "Received UPLINK RELEASE on initial channel.\n");
1276 /* Clear the busy flag and unblock all cells. */
1277 trans->gcc.uplink_bss = NULL;
1278 trans->gcc.uplink_cell = NULL;
1279 trans->gcc.uplink_busy = false;
1280 llist_for_each_entry(bss, &trans->gcc.bss_list, list) {
1281 /* Update uplink state. */
1282 update_uplink_state(bss, trans->gcc.uplink_busy);
1283 }
1284 /* Start inactivity timer. */
1285 start_inactivity_timer(bss->trans);
1286 /* Next, the MS will switch to the VGCS as listener. Nothing else to do here. */
1287 }
1288
1289 return 0;
1290}
1291
1292/* Allocation of transaction for group call */
1293static struct gsm_trans *trans_alloc_vgcs(struct gsm_network *net,
1294 struct vlr_subscr *vsub,
1295 enum trans_type trans_type, uint8_t transaction_id,
1296 uint32_t callref,
1297 struct gcr *gcr,
1298 bool uplink_busy)
1299{
1300 struct gsm_trans *trans;
1301
1302 trans = trans_alloc(net, vsub, trans_type, transaction_id, callref);
1303 if (!trans) {
1304 LOG_GCC(trans, LOGL_ERROR, "No memory for trans.\n");
1305 return NULL;
1306 }
1307 /* The uplink is busy when the call is started until the calling subscriber releases. */
1308 trans->gcc.uplink_busy = uplink_busy;
1309 trans->gcc.uplink_originator = true;
1310 INIT_LLIST_HEAD(&trans->gcc.bss_list);
1311 trans->gcc.inactivity_to = gcr->timeout;
1312 trans->gcc.mute_talker = gcr->mute_talker;
1313 trans->gcc.timer_inactivity.data = trans;
1314 trans->gcc.timer_inactivity.cb = inactivity_timer_cb;
1315 trans->gcc.fi = osmo_fsm_inst_alloc((trans_type == TRANS_GCC) ? &vgcs_gcc_fsm : &vgcs_bcc_fsm,
1316 trans, trans, LOGL_DEBUG, NULL);
1317 if (!trans->gcc.fi) {
1318 LOG_GCC(trans, LOGL_ERROR, "No memory for state machine.\n");
1319 trans_free(trans);
1320 return NULL;
1321 }
1322
1323 return trans;
1324}
1325
1326/* Create transaction from incoming voice group/broadcast call. */
1327static struct gsm_trans *trans_create_bcc_gcc(struct msc_a *msc_a, enum trans_type trans_type, uint8_t transaction_id,
1328 uint8_t pdisc, uint8_t msg_type, uint32_t callref)
1329{
Andreas Eversberg7e65ed92023-07-19 09:38:25 +02001330 struct gsm_network *net;
1331 struct vlr_subscr *vsub;
Andreas Eversberge24636c2023-04-23 12:20:55 +02001332 struct gsm_trans *trans = NULL;
1333 struct gcr *gcr;
1334 int rc;
1335
1336 if (!msc_a) {
1337 LOG_GCC(trans, LOGL_ERROR, "Invalid conn: no msc_a\n");
1338 return NULL;
1339 }
Andreas Eversberg7e65ed92023-07-19 09:38:25 +02001340 net = msc_a_net(msc_a);
1341 vsub = msc_a_vsub(msc_a);
1342
Andreas Eversberge24636c2023-04-23 12:20:55 +02001343 if (!vsub) {
1344 LOG_GCC(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
1345 return NULL;
1346 }
1347
1348 /* An earlier CM Service Request for this CC message now has concluded */
1349 if (!osmo_use_count_by(&msc_a->use_count,
1350 (trans_type == TRANS_GCC) ? MSC_A_USE_CM_SERVICE_GCC : MSC_A_USE_CM_SERVICE_BCC))
1351 LOG_MSC_A(msc_a, LOGL_ERROR,
1352 "Creating new %s transaction without prior CM Service Request.\n",
1353 get_value_string(trans_type_names, trans_type));
1354 else
1355 msc_a_put(msc_a,
1356 (trans_type == TRANS_GCC) ? MSC_A_USE_CM_SERVICE_GCC : MSC_A_USE_CM_SERVICE_BCC);
1357
1358 /* A transaction must be created with a SETUP message. */
1359 if (msg_type != OSMO_GSM44068_MSGT_IMMEDIATE_SETUP
1360 && msg_type != OSMO_GSM44068_MSGT_SETUP
1361 && msg_type != OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2) {
1362 LOG_GCC(trans, LOGL_ERROR, "No transaction and message is not a SETUP.\n");
1363 return NULL;
1364 }
1365
1366 /* Check if callref already exists. */
1367 trans = trans_find_by_callref(net, trans_type, callref);
1368 if (trans) {
1369 LOG_GCC(trans, LOGL_INFO, "Call to existing %s with callref %s, rejecting!\n",
1370 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1371 rc = gsm44068_tx_termination(msc_a, NULL,
1372 pdisc | (transaction_id << 4),
1373 OSMO_GSM44068_MSGT_TERMINATION,
1374 OSMO_GSM44068_CAUSE_BUSY, NULL, 0);
1375 if (rc < 0)
1376 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1377 return 0;
1378 }
1379
1380 /* Check GCR for Group ID. */
1381 gcr = gcr_by_callref(net, trans_type, callref);
1382 if (!gcr) {
1383 LOG_GCC(trans, LOGL_INFO, "No Group configured for %s callref %s, rejecting!\n",
1384 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1385 // FIXME: Better cause value for a group that does not exist ?
1386 rc = gsm44068_tx_termination(msc_a, NULL,
1387 pdisc | (transaction_id << 4),
1388 OSMO_GSM44068_MSGT_TERMINATION,
1389 OSMO_GSM44068_CAUSE_REQUESTED_SERVICE_NOT_SUB, NULL, 0);
1390 if (rc < 0)
1391 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1392 return 0;
1393 }
1394
1395 /* Create transaction, uplink is busy. */
1396 trans = trans_alloc_vgcs(net, vsub, trans_type, transaction_id, callref, gcr, true);
1397 if (!trans) {
1398 rc = gsm44068_tx_termination(msc_a, NULL,
1399 pdisc | (transaction_id << 4),
1400 OSMO_GSM44068_MSGT_TERMINATION,
1401 OSMO_GSM44068_CAUSE_NETWORK_FAILURE, NULL, 0);
1402 if (rc < 0)
1403 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1404 return NULL;
1405 }
1406
1407 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
1408 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept %s transaction.\n",
1409 get_value_string(trans_type_names, trans_type));
1410 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1411 return NULL;
1412 }
1413
1414 /* Assign transaction */
1415 msc_a_get(msc_a, (trans_type == TRANS_GCC) ? MSC_A_USE_GCC : MSC_A_USE_BCC);
1416 trans->msc_a = msc_a;
1417 trans->dlci = 0; /* main DCCH */
1418
1419 return trans;
1420}
1421
1422/* Receive GCC/BCC messages from calling subscriber, depending on the PDISC used. */
1423int gsm44068_rcv_bcc_gcc(struct msc_a *msc_a, struct gsm_trans *trans, struct msgb *msg)
1424{
1425 struct gsm48_hdr *gh = msgb_l3(msg);
1426 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1427 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1428 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
1429 enum trans_type trans_type = (pdisc == GSM48_PDISC_GROUP_CC) ? TRANS_GCC : TRANS_BCC;
1430
1431 uint8_t key_seq;
1432 bool talker_prio_requested;
1433 bool with_talker_prio;
1434 uint8_t talker_prio;
1435 struct gsm48_classmark2 cm2;
1436 struct osmo_mobile_identity mi;
1437 uint32_t callref;
1438 bool with_prio;
1439 uint8_t prio;
1440 char user_user[64] = "";
1441 uint8_t cause;
1442 uint8_t diag[256];
1443 uint8_t diag_len;
1444 bool with_call_state;
1445 enum osmo_gsm44068_call_state call_state;
1446 bool with_state_attrs;
1447 uint8_t da, ua, comm, oi;
1448 int rc = 0;
1449
1450 /* Remove sequence number (bit 7) from message type. */
1451 msg_type &= 0xbf;
1452
1453 /* Parse messages. */
1454 switch (msg_type) {
1455 case OSMO_GSM44068_MSGT_SETUP:
1456 rc = gsm44068_rx_setup(msg, &talker_prio_requested, &talker_prio, &callref, &with_prio, &prio,
1457 user_user);
1458 break;
1459 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP:
1460 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2:
1461 rc = gsm44068_rx_immediate_setup(msg, &talker_prio, &key_seq, &cm2, &mi, &callref, &with_prio, &prio,
1462 user_user);
1463 break;
1464 case OSMO_GSM44068_MSGT_STATUS:
1465 rc = gsm44068_rx_status(msg, &cause, diag, &diag_len, &with_call_state, &call_state,
1466 &with_state_attrs, &da, &ua, &comm, &oi);
1467 break;
1468 case OSMO_GSM44068_MSGT_TERMINATION_REQUEST:
1469 rc = gsm44068_rx_termination_req(msg, &callref, &with_prio, &prio, &with_talker_prio, &talker_prio);
1470 break;
1471 default:
1472 LOG_GCC(trans, LOGL_ERROR, "Invalid message type: 0x%02x\n", msg_type);
1473 return -EINVAL;
1474 }
1475 if (rc < 0)
1476 return rc;
1477
1478 /* Find transaction, if called from msc_a. */
1479 if (!trans)
1480 trans = trans_find_by_id(msc_a, trans_type, transaction_id);
1481
1482 /* Create transaction for SETUP message. */
1483 if (!trans) {
1484 trans = trans_create_bcc_gcc(msc_a, trans_type, transaction_id, pdisc, msg_type, callref);
1485 if (!trans)
1486 return -EINVAL;
1487 } else {
1488 /* A phone may not call while a VGCS is already active */
1489 if (msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP
1490 || msg_type == OSMO_GSM44068_MSGT_SETUP
1491 || msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2) {
1492 LOG_GCC(trans, LOGL_ERROR, "Received SETUP while call is already set up, rejecting.\n");
1493 rc = gsm44068_tx_termination(msc_a, NULL,
1494 pdisc | (transaction_id << 4),
1495 OSMO_GSM44068_MSGT_TERMINATION,
1496 OSMO_GSM44068_CAUSE_NETWORK_FAILURE, NULL, 0);
1497 if (rc < 0)
1498 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1499 return -EINVAL;
1500 }
1501 }
1502
1503 /* Handle received GCC messages (trigger state machine). */
1504 switch (msg_type) {
1505 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP:
1506 case OSMO_GSM44068_MSGT_SETUP:
1507 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2:
1508 LOG_GCC(trans, LOGL_INFO, "Received SETUP.\n");
1509 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_USER_SETUP, NULL);
1510 break;
1511 case OSMO_GSM44068_MSGT_STATUS:
1512 LOG_GCC(trans, LOGL_NOTICE, "Received STATUS with cause %d (%s).\n", cause,
1513 get_value_string(osmo_gsm44068_cause_names, cause));
1514 if (diag_len)
1515 LOG_GCC(trans, LOGL_NOTICE, " -> diagnostics: %s\n", osmo_hexdump(diag, diag_len));
1516 if (with_call_state)
1517 LOG_GCC(trans, LOGL_NOTICE, " -> call state %s\n",
1518 get_value_string(osmo_gsm44068_call_state_names, call_state));
1519 break;
1520 case OSMO_GSM44068_MSGT_TERMINATION_REQUEST:
1521 LOG_GCC(trans, LOGL_INFO, "Received TERMINATRION REQUEST.\n");
1522 if (callref != trans->callref) {
1523 LOG_GCC(trans, LOGL_NOTICE, "Received callref 0x%x does not match!\n", callref);
1524 break;
1525 }
1526 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_USER_TERM, NULL);
1527 break;
1528 }
1529
1530 return 0;
1531}
1532
1533static void bss_clear(struct vgcs_bss *bss, uint8_t cause, bool notify_trans);
1534
1535/* Call Control Specific transaction release.
1536 * gets called by trans_free, DO NOT CALL YOURSELF! */
1537void gsm44068_bcc_gcc_trans_free(struct gsm_trans *trans)
1538{
1539 struct vgcs_bss *bss, *bss2;
1540
Andreas Eversberge24636c2023-04-23 12:20:55 +02001541 /* Free FSM. */
Andreas Eversbergb865c622023-07-19 10:06:07 +02001542 if (trans->gcc.fi) {
1543 osmo_fsm_inst_state_chg(trans->gcc.fi, VGCS_GCC_ST_N0_NULL, 0, 0);
Andreas Eversberge24636c2023-04-23 12:20:55 +02001544 osmo_fsm_inst_term(trans->gcc.fi, OSMO_FSM_TERM_REGULAR, NULL);
Andreas Eversbergb865c622023-07-19 10:06:07 +02001545 }
Andreas Eversberge24636c2023-04-23 12:20:55 +02001546
1547 /* Remove relations to cells.
1548 * We must loop safe, because bss_clear() will detach every call control instance from list. */
1549 llist_for_each_entry_safe(bss, bss2, &trans->gcc.bss_list, list)
1550 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_CLEAR, NULL);
1551
1552 /* Stop inactivity timer. */
1553 stop_inactivity_timer(trans);
1554}
1555
1556/* Create a new call from VTY command. */
1557const char *vgcs_vty_initiate(struct gsm_network *gsmnet, struct gcr *gcr)
1558{
1559 enum trans_type trans_type;
1560 uint32_t callref;
1561 struct gsm_trans *trans;
1562
1563 /* Get callref from stored suffix. Caller cannot choose a prefix. */
1564 trans_type = gcr->trans_type;
1565 callref = atoi(gcr->group_id);
1566
1567 /* Check if callref already exists. */
1568 trans = trans_find_by_callref(gsmnet, trans_type, callref);
1569 if (trans) {
1570 LOG_GCC(trans, LOGL_INFO, "Call to existing %s with callref %s, rejecting!\n",
1571 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1572 return "Call already exists.";
1573 }
1574
1575 /* Create transaction, uplink is free. */
1576 trans = trans_alloc_vgcs(gsmnet, NULL, trans_type, 0, callref, gcr, false);
1577 if (!trans) {
1578 LOG_GCC(trans, LOGL_ERROR, "No memory for trans.\n");
1579 return "Failed to create call.";
1580 }
1581
1582 LOG_GCC(trans, LOGL_INFO, "VTY initiates call.\n");
1583 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_NET_SETUP, NULL);
1584
1585 return NULL;
1586}
1587
1588/* Destroy a call from VTY command. */
1589const char *vgcs_vty_terminate(struct gsm_network *gsmnet, struct gcr *gcr)
1590{
1591 enum trans_type trans_type;
1592 uint32_t callref;
1593 struct gsm_trans *trans;
1594
1595 /* Get callref from stored suffix. Caller cannot choose a prefix. */
1596 trans_type = gcr->trans_type;
1597 callref = atoi(gcr->group_id);
1598
1599 /* Check if callref exists. */
1600 trans = trans_find_by_callref(gsmnet, trans_type, callref);
1601 if (!trans)
1602 return "Call does not exist.";
1603
1604 LOG_GCC(trans, LOGL_INFO, "VTY terminates call.\n");
1605 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_NET_TERM, NULL);
1606
1607 return NULL;
1608}
1609
1610/*
1611 * BSS state machine - handles all BSS "call control" instances
1612 */
1613
1614static const struct value_string vgcs_bss_fsm_event_names[] = {
1615 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP),
1616 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP_ACK),
1617 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP_REFUSE),
1618 OSMO_VALUE_STRING(VGCS_BSS_EV_ACTIVE_OR_FAIL),
1619 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_REQUEST),
1620 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_REQUEST_CNF),
1621 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_APP_DATA),
1622 OSMO_VALUE_STRING(VGCS_BSS_EV_BSS_DTAP),
1623 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_RELEASE),
1624 OSMO_VALUE_STRING(VGCS_BSS_EV_CLEAR),
1625 OSMO_VALUE_STRING(VGCS_BSS_EV_CLOSE),
1626 OSMO_VALUE_STRING(VGCS_BSS_EV_RELEASED),
1627 { }
1628};
1629
1630/* Blocks or unblocks uplinks of a BSS. */
1631static int update_uplink_state(struct vgcs_bss *bss, bool uplink_busy)
1632{
1633 struct ran_msg ran_msg;
1634 int rc;
1635
1636 if (uplink_busy) {
1637 /* Send UPLINK SEIZED COMMAND to BSS. */
1638 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK SEIZED COMMAND towards BSS.\n");
1639 ran_msg = (struct ran_msg){
1640 .msg_type = RAN_MSG_UPLINK_SEIZED_CMD,
1641 .uplink_seized_cmd = {
1642 .cause = GSM0808_CAUSE_CALL_CONTROL,
1643 },
1644 };
1645 } else {
1646 /* Send UPLINK RELEASE COMMAND to BSS. */
1647 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK RELEASE COMMAND towards BSS.\n");
1648 ran_msg = (struct ran_msg){
1649 .msg_type = RAN_MSG_UPLINK_RELEASE_CMD,
1650 .uplink_release_cmd = {
1651 .cause = GSM0808_CAUSE_CALL_CONTROL,
1652 },
1653 };
1654 }
1655
1656 rc = ran_encode_and_send(bss->fi, &ran_msg, bss->conn, false);
1657
1658 return rc;
1659}
1660
1661/* Clear the connection towards BSS.
1662 * The instance is removed soon, so it is detached from transaction and cells. */
1663static void bss_clear(struct vgcs_bss *bss, uint8_t cause, bool notify_trans)
1664{
1665 struct ran_msg ran_msg;
1666 struct gsm_trans *trans = bss->trans;
1667 struct vgcs_bss_cell *cell, *cell2;
1668
1669 /* Must detach us from transaction. */
1670 if (bss->trans) {
1671 /* Remove pointer to talking BSS and cell. */
1672 if (bss == bss->trans->gcc.uplink_bss) {
1673 bss->trans->gcc.uplink_bss = NULL;
1674 bss->trans->gcc.uplink_cell = NULL;
1675 }
1676 llist_del(&bss->list);
1677 bss->trans = NULL;
1678 }
1679
1680 /* Change state. */
1681 osmo_fsm_inst_state_chg(bss->fi, VGCS_BSS_ST_RELEASE, 0, 0);
1682
1683 /* Send Clear Command to BSS. */
1684 ran_msg = (struct ran_msg){
1685 .msg_type = RAN_MSG_CLEAR_COMMAND,
1686 .clear_command = {
1687 .gsm0808_cause = cause,
1688 },
1689 };
1690 if (bss->conn) {
1691 LOG_BSS(bss, LOGL_DEBUG, "Sending CLEAR COMMAND for call controling channel.\n");
1692 ran_encode_and_send(bss->fi, &ran_msg, bss->conn, false);
1693 }
1694
1695 /* Trigger clear of all cells. Be safe, because the process will remove cells from list. */
1696 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss)
1697 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_CLEAR, NULL);
1698
1699 /* Detach us from all BSS, if still linked */
1700 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss) {
1701 llist_del(&cell->list_bss);
1702 cell->bss = NULL;
1703 }
1704
1705 /* If all BS are gone, notify calling subscriber process. */
1706 if (notify_trans && trans && llist_empty(&trans->gcc.bss_list)) {
1707 LOG_BSS(bss, LOGL_DEBUG, "Notify calling user process, that all BSSs are cleared.\n");
1708 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_RELEASED, NULL);
1709 }
1710}
1711
1712/* When finally the BSS connection is released. (CLEAR COMPLETE response)
1713 * The instance is removed, so it is detached from transaction and cells, if not already. */
1714static void bss_destroy(struct vgcs_bss *bss)
1715{
1716 struct vgcs_bss_cell *cell, *cell2;
1717
1718 LOG_BSS(bss, LOGL_DEBUG, "Removing BSS call controling instance.\n");
1719
1720 /* Must detach us from transaction, if not already. */
1721 if (bss->trans) {
1722 /* Remove pointer to talking BSS and cell. */
1723 if (bss == bss->trans->gcc.uplink_bss) {
1724 bss->trans->gcc.uplink_bss = NULL;
1725 bss->trans->gcc.uplink_cell = NULL;
1726 }
1727 llist_del(&bss->list);
1728 bss->trans = NULL;
1729 }
1730
1731 /* Detach us from RAN connection. */
1732 if (bss->conn) {
1733 if (bss->conn->vgcs.bss == bss)
1734 bss->conn->vgcs.bss = NULL;
1735 if (bss->conn->vgcs.cell == bss)
1736 bss->conn->vgcs.cell = NULL;
1737 ran_conn_close(bss->conn);
1738 bss->conn = NULL;
1739 }
1740
1741 /* Detach us from all BSS, if still linked */
1742 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss) {
1743 llist_del(&cell->list_bss);
1744 cell->bss = NULL;
1745 }
1746
1747 /* Free FSM. (should be allocated) */
1748 osmo_fsm_inst_state_chg(bss->fi, VGCS_BSS_ST_NULL, 0, 0);
1749 osmo_fsm_inst_term(bss->fi, OSMO_FSM_TERM_REGULAR, NULL);
1750}
1751
1752/* Get identity of talker.
1753 * This is required to detect if the talker is the calling subscriber. */
1754static int talker_identity(struct vgcs_bss *bss, uint8_t *l3, int l3_len)
1755{
1756 struct osmo_mobile_identity mi;
1757 int rc;
1758
1759 puts(osmo_hexdump(l3, l3_len));
1760 rc = osmo_mobile_identity_decode_from_l3_buf(&mi, l3, l3_len, false);
1761 if (rc < 0) {
1762 LOG_BSS(bss, LOGL_DEBUG, "Talker's Identity cannot be decoded.\n");
1763 return rc;
1764 }
1765
1766 switch (mi.type) {
1767 case GSM_MI_TYPE_IMSI:
1768 if (!bss->trans->vsub)
1769 break;
1770 LOG_BSS(bss, LOGL_DEBUG, "Talker's sends IMSI %s, originator has IMSI %s.\n",
1771 mi.imsi, bss->trans->vsub->imsi);
1772 if (!strcmp(mi.imsi, bss->trans->vsub->imsi))
1773 return 1;
1774 break;
1775 case GSM_MI_TYPE_TMSI:
1776 if (!bss->trans->vsub)
1777 break;
1778 LOG_BSS(bss, LOGL_DEBUG, "Talker's sends TMSI 0x%08x, originator has TMSI 0x%08x.\n",
1779 mi.tmsi, bss->trans->vsub->tmsi);
1780 if (mi.tmsi == bss->trans->vsub->tmsi)
1781 return 1;
1782 break;
1783 default:
1784 LOG_BSS(bss, LOGL_DEBUG, "Talker's Identity is not IMSI nor TMSI.\n");
1785 return -EINVAL;
1786 }
1787
1788 return 0;
1789}
1790
1791static void vgcs_bss_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1792{
1793 struct vgcs_bss *bss = fi->priv;
1794 struct ran_msg ran_msg;
1795
1796 switch (event) {
1797 case VGCS_BSS_EV_SETUP:
1798 /* Change state. */
1799 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_SETUP, 0, 0);
1800 /* Send VGCS/VBS SETUP to BSS. */
1801 LOG_BSS(bss, LOGL_DEBUG, "Sending VGCS/VBS SETUP towards BSS.\n");
1802 ran_msg = (struct ran_msg){
1803 .msg_type = RAN_MSG_VGCS_VBS_SETUP,
1804 .vgcs_vbs_setup = {
1805 .callref = {
1806 .sf = (bss->trans->type == TRANS_GCC),
1807 },
1808 .vgcs_feature_flags_present = true,
1809 },
1810 };
1811 osmo_store32be_ext(bss->callref >> 3, &ran_msg.vgcs_vbs_setup.callref.call_ref_hi, 3);
1812 ran_msg.vgcs_vbs_setup.callref.call_ref_lo = bss->callref & 0x7;
1813 /* First message, so we must set "initial" to "true". */
1814 ran_encode_and_send(fi, &ran_msg, bss->conn, true);
1815 break;
1816 case VGCS_BSS_EV_CLEAR:
1817 /* The calling user process requested clearing of VGCS/VBS call. */
1818 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1819 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1820 break;
1821 default:
1822 OSMO_ASSERT(false);
1823 }
1824}
1825
1826static void vgcs_bss_fsm_setup(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1827{
1828 struct vgcs_bss *bss = fi->priv;
1829 struct vgcs_bss_cell *cell, *cell2;
1830
1831 switch (event) {
1832 case VGCS_BSS_EV_SETUP_ACK:
1833 /* Receive VGCS/VBS SETUP ACK from BSS. */
1834 LOG_BSS(bss, LOGL_DEBUG, "Received VGCS/VBS SETUP ACK from BSS.\n");
1835 /* Send current uplink state to this BSS. */
1836 if (bss->trans)
1837 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
1838 /* Change state. */
1839 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_ASSIGNMENT, 0, 0);
1840 /* Trigger VGCS/VBS ASSIGNMENT */
1841 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss)
1842 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN, NULL);
1843 /* If all failed, clear call. */
1844 if (llist_empty(&bss->cell_list)) {
1845 LOG_BSS(bss, LOGL_NOTICE, "All VGCS/VBS assignments failed.\n");
1846 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1847 break;
1848 }
1849 break;
1850 case VGCS_BSS_EV_SETUP_REFUSE:
1851 /* Received VGCS/VBS SETUP REFUSE from BSS. */
1852 LOG_BSS(bss, LOGL_NOTICE, "Received VGCS/VBS SETUP REFUSE from BSS.\n");
1853 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1854 break;
1855 case VGCS_BSS_EV_CLEAR:
1856 /* The calling user process requested clearing of VGCS/VBS call. */
1857 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1858 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1859 break;
1860 case VGCS_BSS_EV_CLOSE:
1861 /* The SCCP connection from the MSC has been closed. */
1862 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
1863 if (bss->conn) {
1864 bss->conn->vgcs.bss = NULL;
1865 bss->conn = NULL;
1866 }
1867 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1868 break;
1869 default:
1870 OSMO_ASSERT(false);
1871 }
1872}
1873
1874static void vgcs_bss_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1875{
1876 struct vgcs_bss *bss = fi->priv;
1877 struct vgcs_bss_cell *c;
1878 bool assigned;
1879
1880 switch (event) {
1881 case VGCS_BSS_EV_ACTIVE_OR_FAIL:
1882 /* If all gone, clear call. */
1883 if (llist_empty(&bss->cell_list)) {
1884 LOG_BSS(bss, LOGL_NOTICE, "All VGCS/VBS assignments failed.\n");
1885 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1886 break;
1887 }
1888 /* Is there a response for all cells?
1889 * This means that all the channels have a positive response
1890 * There is no channel with negative response, because a
1891 * negative response will remove the channel. */
1892 assigned = true;
1893 llist_for_each_entry(c, &bss->cell_list, list_bss) {
1894 if (!c->assigned)
1895 assigned = false;
1896 }
1897 if (!assigned)
1898 break;
1899 LOG_BSS(bss, LOGL_DEBUG, "All VGCS/VBS assignments have responded.\n");
1900 /* Change state. */
1901 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_ACTIVE, 0, 0);
1902 /* Notify calling subscriber process. */
1903 LOG_BSS(bss, LOGL_DEBUG, "Notify calling user process, that all BSSs are connected.\n");
1904 if (bss->trans)
1905 osmo_fsm_inst_dispatch(bss->trans->gcc.fi, VGCS_GCC_EV_BSS_ESTABLISHED, NULL);
1906 break;
1907 case VGCS_BSS_EV_CLEAR:
1908 /* The calling user process requested clearing of VGCS/VBS call. */
1909 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1910 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1911 break;
1912 case VGCS_BSS_EV_CLOSE:
1913 /* The SCCP connection from the MSC has been closed. */
1914 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
1915 if (bss->conn) {
1916 bss->conn->vgcs.bss = NULL;
1917 bss->conn = NULL;
1918 }
1919 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1920 break;
1921 default:
1922 OSMO_ASSERT(false);
1923 }
1924}
1925
1926static void vgcs_bss_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1927{
1928 struct vgcs_bss *bss = fi->priv, *other;
1929 struct ran_msg *rx_ran_msg = data;
1930 struct ran_msg tx_ran_msg;
1931 int rc;
1932
1933 switch (event) {
1934 case VGCS_BSS_EV_UL_REQUEST:
1935 LOG_BSS(bss, LOGL_DEBUG, "Listener changed to talker.\n");
1936 if (!bss->trans)
1937 break;
1938 /* Someone is talking. Check if there is no other uplink already busy.
1939 * This should not happen, since all other cells are blocked (SEIZED) as soon as the uplink was
1940 * requested. This may happen due to a race condition, where the uplink was requested before the
1941 * UPLINK SEIZED COMMAND has been received by BSS. */
1942 if (bss->trans->gcc.uplink_busy) {
1943 /* Send UPLINK REJECT COMMAND to BSS. */
1944 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK REJECT COMMAND towards BSS.\n");
1945 tx_ran_msg = (struct ran_msg){
1946 .msg_type = RAN_MSG_UPLINK_REJECT_CMD,
1947 .uplink_reject_cmd = {
1948 .cause = GSM0808_CAUSE_CALL_CONTROL,
1949 },
1950 };
1951 ran_encode_and_send(fi, &tx_ran_msg, bss->conn, false);
1952 break;
1953 }
1954 /* Send UPLINK REQUEST ACKNOWLEDGE to BSS. */
1955 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK REQUEST ACKNOWLEDGE towards BSS.\n");
1956 tx_ran_msg = (struct ran_msg){
1957 .msg_type = RAN_MSG_UPLINK_REQUEST_ACK,
1958 };
1959 ran_encode_and_send(fi, &tx_ran_msg, bss->conn, false);
1960 /* Set the busy flag and block all other cells. */
1961 bss->trans->gcc.uplink_bss = bss;
1962 bss->trans->gcc.uplink_busy = true;
1963 bss->trans->gcc.uplink_originator = false;
1964 llist_for_each_entry(other, &bss->trans->gcc.bss_list, list) {
1965 if (other == bss)
1966 continue;
1967 /* Update uplink state. */
1968 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
1969 }
1970 /* Stop inactivity timer. */
1971 stop_inactivity_timer(bss->trans);
1972 break;
1973 case VGCS_BSS_EV_UL_REQUEST_CNF:
1974 LOG_BSS(bss, LOGL_DEBUG, "Talker established uplink.\n");
1975 if (!bss->trans)
1976 break;
1977 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
1978 LOG_BSS(bss, LOGL_ERROR, "Got UL REQUEST CNF, but we did not granted uplink.\n");
1979 break;
1980 }
1981 /* Determine if talker is the originator of the call. */
1982 rc = talker_identity(bss, rx_ran_msg->uplink_request_cnf.l3.l3,
1983 rx_ran_msg->uplink_request_cnf.l3.l3_len);
1984 if (rc > 0) {
1985 bss->trans->gcc.uplink_originator = true;
1986 LOG_BSS(bss, LOGL_DEBUG, "Talker is the originator of the call.\n");
1987 }
1988 /* Set parameter. */
1989 set_parameter(bss->trans);
1990 /* Set cell of current talker. */
1991 set_uplink_cell(bss, &rx_ran_msg->uplink_request_cnf.cell_identifier, 0);
1992 /* Set MGW conference. */
1993 set_mgw_conference(bss->trans);
1994 break;
1995 case VGCS_BSS_EV_UL_APP_DATA:
1996 LOG_BSS(bss, LOGL_DEBUG, "Talker sends application data on uplink.\n");
1997 if (!bss->trans)
1998 break;
1999 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
2000 LOG_BSS(bss, LOGL_ERROR, "Got UP APP DATA, but we did not granted uplink.\n");
2001 break;
2002 }
2003 // FIXME: Use L3 info and feed to app.
2004 break;
2005 case VGCS_BSS_EV_BSS_DTAP:
2006 LOG_BSS(bss, LOGL_DEBUG, "Talker sends DTAP message.\n");
2007 if (!bss->trans)
2008 break;
2009 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
2010 LOG_BSS(bss, LOGL_ERROR, "Got DTAP from BSS, but we did not granted uplink.\n");
2011 break;
2012 }
2013 gsm44068_rcv_bcc_gcc(NULL, bss->trans, rx_ran_msg->dtap);
2014 break;
2015 case VGCS_BSS_EV_UL_RELEASE:
2016 LOG_BSS(bss, LOGL_DEBUG, "Talker released uplink.\n");
2017 if (!bss->trans)
2018 break;
2019 if (bss->trans->type == TRANS_BCC) {
2020 LOG_BSS(bss, LOGL_DEBUG, "This is a broadcast call, terminating call.\n");
2021 gcc_terminate_and_destroy(bss->trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
2022 break;
2023 }
2024 if (!bss->trans->gcc.uplink_busy) {
2025 LOG_BSS(bss, LOGL_NOTICE, "Got uplink release, but no uplink busy.\n");
2026 break;
2027 }
2028 /* Talker release the uplink. Ignore, if not from the current talking cell. */
2029 if (bss->trans->gcc.uplink_bss != bss) {
2030 LOG_BSS(bss, LOGL_NOTICE, "Got uplink release, but uplink busy in other cell.\n");
2031 break;
2032 }
2033 /* Clear the busy flag and unblock all other cells. */
2034 bss->trans->gcc.uplink_bss = NULL;
2035 bss->trans->gcc.uplink_cell = NULL;
2036 bss->trans->gcc.uplink_busy = false;
2037 llist_for_each_entry(other, &bss->trans->gcc.bss_list, list) {
2038 if (other == bss)
2039 continue;
2040 /* Update uplink state. */
2041 if (bss->trans)
2042 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
2043 }
2044 /* Set MGW conference. */
2045 set_mgw_conference(bss->trans);
2046 /* Start inactivity timer. */
2047 start_inactivity_timer(bss->trans);
2048 break;
2049 case VGCS_BSS_EV_CLEAR:
2050 /* The calling user process requested clearing of VGCS/VBS call. */
2051 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
2052 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
2053 break;
2054 case VGCS_BSS_EV_CLOSE:
2055 /* The SCCP connection from the MSC has been closed. */
2056 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2057 if (bss->conn) {
2058 bss->conn->vgcs.bss = NULL;
2059 bss->conn = NULL;
2060 }
2061 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
2062 break;
2063 default:
2064 OSMO_ASSERT(false);
2065 }
2066}
2067
2068static void vgcs_bss_fsm_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2069{
2070 struct vgcs_bss *bss = fi->priv;
2071
2072 switch (event) {
2073 case VGCS_BSS_EV_CLOSE:
2074 /* The SCCP connection from the MSC has been closed while waitring fro CLEAR COMPLETE. */
2075 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP closing collision.\n");
2076 bss_destroy(bss);
2077 break;
2078 case VGCS_BSS_EV_RELEASED:
2079 LOG_BSS(bss, LOGL_DEBUG, "Received CLEAR COMPLETE from BSS, we are done!\n");
2080 bss_destroy(bss);
2081 break;
2082 default:
2083 OSMO_ASSERT(false);
2084 }
2085}
2086
2087static const struct osmo_fsm_state vgcs_bss_fsm_states[] = {
2088 [VGCS_BSS_ST_NULL] = {
2089 .name = "NULL",
2090 .in_event_mask = S(VGCS_BSS_EV_SETUP) |
2091 S(VGCS_BSS_EV_CLEAR),
2092 .out_state_mask = S(VGCS_BSS_ST_SETUP),
2093 .action = vgcs_bss_fsm_null,
2094 },
2095 [VGCS_BSS_ST_SETUP] = {
2096 .name = "SETUP sent",
2097 .in_event_mask = S(VGCS_BSS_EV_SETUP_ACK) |
2098 S(VGCS_BSS_EV_SETUP_REFUSE) |
2099 S(VGCS_BSS_EV_CLEAR) |
2100 S(VGCS_BSS_EV_CLOSE),
2101 .out_state_mask = S(VGCS_BSS_ST_ASSIGNMENT) |
2102 S(VGCS_BSS_ST_RELEASE),
2103 .action = vgcs_bss_fsm_setup,
2104 },
2105 [VGCS_BSS_ST_ASSIGNMENT] = {
2106 .name = "ASSIGNMENT Sent",
2107 .in_event_mask = S(VGCS_BSS_EV_ACTIVE_OR_FAIL) |
2108 S(VGCS_BSS_EV_CLEAR) |
2109 S(VGCS_BSS_EV_CLOSE),
2110 .out_state_mask = S(VGCS_BSS_ST_ACTIVE) |
2111 S(VGCS_BSS_ST_RELEASE),
2112 .action = vgcs_bss_fsm_assignment,
2113 },
2114 [VGCS_BSS_ST_ACTIVE] = {
2115 .name = "VGCS/VBS Active",
2116 .in_event_mask = S(VGCS_BSS_EV_UL_REQUEST) |
2117 S(VGCS_BSS_EV_UL_REQUEST_CNF) |
2118 S(VGCS_BSS_EV_UL_APP_DATA) |
2119 S(VGCS_BSS_EV_BSS_DTAP) |
2120 S(VGCS_BSS_EV_UL_RELEASE) |
2121 S(VGCS_BSS_EV_CLEAR) |
2122 S(VGCS_BSS_EV_CLOSE),
2123 .out_state_mask = S(VGCS_BSS_ST_RELEASE),
2124 .action = vgcs_bss_fsm_active,
2125 },
2126 [VGCS_BSS_ST_RELEASE] = {
2127 .name = "Releasing VGCS/VBS control",
2128 .in_event_mask = S(VGCS_BSS_EV_CLEAR) |
2129 S(VGCS_BSS_EV_RELEASED),
2130 .out_state_mask = S(VGCS_BSS_ST_NULL),
2131 .action = vgcs_bss_fsm_release,
2132 },
2133};
2134
2135static struct osmo_fsm vgcs_bss_fsm = {
2136 .name = "vgcs_bss",
2137 .states = vgcs_bss_fsm_states,
2138 .num_states = ARRAY_SIZE(vgcs_bss_fsm_states),
2139 .log_subsys = DASCI,
2140 .event_names = vgcs_bss_fsm_event_names,
2141};
2142
2143/* The BSS accepts VGCS/VBS and sends us supported features. */
2144void vgcs_vbs_setup_ack(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2145{
2146 if (!bss->trans)
2147 return;
2148 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_SETUP_ACK, (void *)ran_msg);
2149}
2150
2151/* The BSS refuses VGCS/VBS. */
2152void vgcs_vbs_setup_refuse(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2153{
2154 if (!bss->trans)
2155 return;
2156 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_SETUP_REFUSE, (void *)ran_msg);
2157}
2158
2159/* The BSS needs more time for VGCS/VBS channel assignment. */
2160void vgcs_vbs_queuing_ind(struct vgcs_bss_cell *cell)
2161{
2162 if (!cell->bss)
2163 return;
2164}
2165
2166/* A mobile station requests the uplink on a VGCS channel. */
2167void vgcs_uplink_request(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2168{
2169 if (!bss->trans)
2170 return;
2171 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_REQUEST, (void *)ran_msg);
2172}
2173
2174/* The uplink on a VGCS channel has been established. */
2175void vgcs_uplink_request_cnf(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2176{
2177 if (!bss->trans)
2178 return;
2179 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_REQUEST_CNF, (void *)ran_msg);
2180}
2181
2182/* Application data received on the uplink of a VGCS channel. */
2183void vgcs_app_data(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2184{
2185 if (!bss->trans)
2186 return;
2187 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_APP_DATA, (void *)ran_msg);
2188}
2189
2190/* Application data received on the uplink of a VGCS channel. */
2191void vgcs_bss_dtap(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2192{
2193 if (!bss->trans)
2194 return;
2195 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_BSS_DTAP, (void *)ran_msg);
2196}
2197
2198/* A mobile station releases the uplink on a VGCS channel. */
2199void vgcs_uplink_release_ind(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2200{
2201 if (!bss->trans)
2202 return;
2203 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_RELEASE, (void *)ran_msg);
2204}
2205
2206/* The BSS gives cell status about VGCS/VBS channel. */
2207void vgcs_vbs_assign_status(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2208{
2209 if (!cell->bss)
2210 return;
2211}
2212
2213void vgcs_vbs_caller_assign_cpl(struct gsm_trans *trans)
2214{
2215 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_ASSIGN_CPL, NULL);
2216}
2217
2218void vgcs_vbs_caller_assign_fail(struct gsm_trans *trans)
2219{
2220 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_ASSIGN_FAIL, NULL);
2221}
2222
2223/* BSS indicated that the channel has been released. */
2224void vgcs_vbs_clear_req(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2225{
2226 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_CLOSE, (void *)ran_msg);
2227}
2228
2229/* BSS indicated that the channel has been released. */
2230void vgcs_vbs_clear_cpl(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2231{
2232 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_RELEASED, (void *)ran_msg);
2233}
2234
2235/*
2236 * Cell resource state machine - handles all "resource control" instances
2237 */
2238
2239static const struct value_string vgcs_cell_fsm_event_names[] = {
2240 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_GONE),
2241 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE),
2242 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED),
2243 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN),
2244 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN_RES),
2245 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN_FAIL),
2246 OSMO_VALUE_STRING(VGCS_CELL_EV_CLEAR),
2247 OSMO_VALUE_STRING(VGCS_CELL_EV_CLOSE),
2248 OSMO_VALUE_STRING(VGCS_CELL_EV_RELEASED),
2249 { }
2250};
2251
2252static void cell_destroy(struct vgcs_bss_cell *cell);
2253
2254/* Clear the connection towards BSS.
2255 * Relations to the BSS and transaction is removed. */
2256static void cell_clear(struct vgcs_bss_cell *cell, uint8_t cause)
2257{
2258 struct ran_msg ran_msg;
2259
2260 /* Must detach us from BSS. */
2261 if (cell->bss) {
2262 /* Remove pointer to talking channel. */
2263 if (cell->bss->trans && cell->bss->trans->gcc.uplink_cell == cell)
2264 cell->bss->trans->gcc.uplink_cell = NULL;
2265 llist_del(&cell->list_bss);
2266 cell->bss = NULL;
2267 }
2268
2269 /* Change state. */
2270 if (cell->fi->state != VGCS_CELL_ST_RELEASE)
2271 osmo_fsm_inst_state_chg(cell->fi, VGCS_CELL_ST_RELEASE, 0, 0);
2272
2273 /* If there is no event to wait for, we can just destroy. */
2274 if (!cell->conn && !cell->rtps) {
2275 cell_destroy(cell);
2276 return;
2277 }
2278
2279 /* Send Clear Command to BSS. */
2280 if (cell->conn) {
2281 ran_msg = (struct ran_msg){
2282 .msg_type = RAN_MSG_CLEAR_COMMAND,
2283 .clear_command = {
2284 .gsm0808_cause = cause,
2285 },
2286 };
2287 LOG_CELL(cell, LOGL_DEBUG, "Sending CLEAR COMMAND for call controling channel.\n");
2288 ran_encode_and_send(cell->fi, &ran_msg, cell->conn, false);
2289 }
2290
2291 /* Clear RTP stream. This may trigger VGCS_CELL_EV_RTP_STREAM_GONE within this release function. */
2292 if (cell->rtps)
2293 rtp_stream_release(cell->rtps);
2294}
2295
2296/* When finally the BSS connection is released. (CLEAR COMPLETE response)
2297 * Relations to the BSS and transaction is removed, if not already. */
2298static void cell_destroy(struct vgcs_bss_cell *cell)
2299{
2300 struct vgcs_mgw_ep *mgw;
2301
2302 /* close RAN conn */
2303 if (cell->conn) {
2304 cell->conn->vgcs.cell = NULL;
2305 ran_conn_close(cell->conn);
2306 cell->conn = NULL;
2307 }
2308
2309 /* Detach from BSS now. Check, to prevent race condition. */
2310 if (cell->bss) {
2311 /* Remove pointer to talking channel. */
2312 if (cell->bss->trans && cell->bss->trans->gcc.uplink_cell == cell)
2313 cell->bss->trans->gcc.uplink_cell = NULL;
2314 llist_del(&cell->list_bss);
2315 cell->bss = NULL;
2316 }
2317
2318 /* Detach from MGW now. Check, to prevent race condition. */
2319 if (cell->mgw) {
2320 mgw = cell->mgw;
2321 llist_del(&cell->list_mgw);
2322 cell->mgw = NULL;
2323 /* Destroy MGW endpoint, if list is empty. */
2324 if (llist_empty(&mgw->cell_list))
2325 osmo_fsm_inst_dispatch(mgw->fi, VGCS_MGW_EP_EV_CLEAR, NULL);
2326 }
2327
2328 LOG_CELL(cell, LOGL_DEBUG, "Detroy connection to cell.\n");
2329
2330 /* Free FSM. (should be allocated) */
2331 osmo_fsm_inst_state_chg(cell->fi, VGCS_CELL_ST_NULL, 0, 0);
2332 osmo_fsm_inst_term(cell->fi, OSMO_FSM_TERM_REGULAR, NULL);
2333}
2334
2335static void vgcs_cell_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2336{
2337 struct vgcs_bss_cell *cell = fi->priv;
2338 const struct codec_mapping *cm;
2339 int rc;
2340
2341 switch (event) {
2342 case VGCS_CELL_EV_ASSIGN:
2343 LOG_CELL(cell, LOGL_DEBUG, "Received assignment from BSS controling process.\n");
2344 /* Allocate rtps stream. */
2345 cell->rtps = rtp_stream_alloc(cell->fi, VGCS_CELL_EV_RTP_STREAM_GONE,
2346 VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE,
2347 VGCS_CELL_EV_RTP_STREAM_ESTABLISHED, RTP_TO_RAN, cell->call_id,
2348 NULL);
2349 if (!cell->rtps) {
2350 LOG_CELL(cell, LOGL_DEBUG, "Failed to allocate RTP stream, cannot continue.\n");
2351 cell_destroy(cell);
2352 break;
2353 }
2354 /* Hard coded codec: GSM V1 */
2355 cm = codec_mapping_by_gsm0808_speech_codec_type(GSM0808_SCT_FR1);
Andreas Eversberg33a433a2023-07-19 10:01:20 +02002356 if (!cm) {
2357 LOG_CELL(cell, LOGL_DEBUG, "Selected codec not supported, cannot continue.\n");
2358 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2359 break;
2360 }
Andreas Eversberge24636c2023-04-23 12:20:55 +02002361 rtp_stream_set_one_codec(cell->rtps, &cm->sdp);
2362 /* Set initial mode. */
2363 rtp_stream_set_mode(cell->rtps, MGCP_CONN_RECV_ONLY);
2364 /* Commit RTP stream. */
Andreas Eversbergd6377602023-07-19 09:46:18 +02002365 if (!cell->bss || !cell->bss->trans) {
2366 LOG_CELL(cell, LOGL_DEBUG, "No BSS/transaction, cannot continue.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +02002367 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2368 break;
2369 }
Andreas Eversbergd6377602023-07-19 09:46:18 +02002370 if (!cell->mgw || !cell->mgw->mgw_ep) {
Andreas Eversberge24636c2023-04-23 12:20:55 +02002371 LOG_CELL(cell, LOGL_DEBUG, "No MGW endpoint, cannot continue.\n");
2372 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2373 break;
2374 }
2375 rc = rtp_stream_ensure_ci(cell->rtps, cell->mgw->mgw_ep);
2376 if (rc < 0) {
2377 LOG_CELL(cell, LOGL_DEBUG, "Failed to trigger RTP stream CI.\n");
2378 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2379 break;
2380 }
2381 /* Change state. */
2382 osmo_fsm_inst_state_chg(fi, VGCS_CELL_ST_ASSIGNMENT, 0, 0);
2383 break;
2384 case VGCS_CELL_EV_CLEAR:
2385 /* The calling user process requested clearing of VGCS/VBS call. */
2386 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2387 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2388 break;
2389 default:
2390 OSMO_ASSERT(false);
2391 }
2392}
2393
2394static void vgcs_cell_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2395{
2396 struct vgcs_bss_cell *cell = fi->priv;
2397 struct ran_msg *rx_ran_msg = data;
2398 struct ran_msg tx_ran_msg;
2399 struct osmo_sockaddr_str ss;
2400 const struct codec_mapping *cm;
2401 struct vgcs_bss *bss;
2402 int rc;
2403
2404 switch (event) {
2405 case VGCS_CELL_EV_RTP_STREAM_GONE:
2406 /* The RTP stream failed. */
2407 LOG_CELL(cell, LOGL_ERROR, "RTP stream of MGW failed.\n");
2408 cell->rtps = NULL;
2409 goto channel_fail;
2410 break;
2411 case VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE:
2412 /* The RTP stream sends its peer. */
2413 if (!osmo_sockaddr_str_is_nonzero(&cell->rtps->local)) {
2414 LOG_CELL(cell, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
2415 OSMO_SOCKADDR_STR_FMT_ARGS(&cell->rtps->local));
2416 goto channel_fail;
2417 }
2418 LOG_CELL(cell, LOGL_DEBUG,
2419 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
2420 rtp_direction_name(cell->rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&cell->rtps->local),
2421 cell->rtps->use_osmux ? "yes" : "no", cell->rtps->local_osmux_cid);
2422 /* Send VGCS/VBS ASSIGNMENT REQUEST to BSS */
2423 LOG_CELL(cell, LOGL_DEBUG, "Sending VGCS/VBS ASSIGNMENT REQUEST towards BSS.\n");
2424 tx_ran_msg = (struct ran_msg) {
2425 .msg_type = RAN_MSG_VGCS_VBS_ASSIGN_REQ,
2426 .vgcs_vbs_assign_req = {
2427 /* For now we support GSM/FR V1 only. This shall be supported by all MS. */
2428 .channel_type = {
2429 .ch_indctr = GSM0808_CHAN_SPEECH,
2430 .ch_rate_type = GSM0808_SPEECH_FULL_BM,
2431 .perm_spch_len = 1,
2432 .perm_spch[0] = GSM0808_PERM_FR1,
2433 },
2434 /* For now we want a channel without any delay. */
2435 .ass_req = GSM0808_ASRQ_IMMEDIATE,
2436 .callref = {
2437 .sf = (cell->trans_type == TRANS_GCC),
2438 },
2439 /* We need to identify the cell only. */
2440 .cell_identifier = {
2441 .id_discr = CELL_IDENT_CI,
2442 .id.ci = cell->cell_id,
2443 },
2444 .aoip_transport_layer_present = true,
2445 .call_id_present = true,
2446 .call_id = cell->call_id,
2447 .codec_list_present = true,
2448 .codec_list_msc_preferred = {
2449 .len = 1,
2450 .codec[0] = {
2451 .fi = 1,
2452 .type = GSM0808_SCT_FR1,
2453 .cfg = 0,
2454 },
2455 },
2456 },
2457 };
2458 osmo_store32be_ext(cell->callref >> 3, &tx_ran_msg.vgcs_vbs_assign_req.callref.call_ref_hi, 3);
2459 tx_ran_msg.vgcs_vbs_assign_req.callref.call_ref_lo = cell->callref & 0x7;
2460 osmo_sockaddr_str_to_sockaddr(&cell->rtps->local, &tx_ran_msg.vgcs_vbs_assign_req.aoip_transport_layer);
2461 /* First message, so we must set "initial" to "true". */
2462 ran_encode_and_send(fi, &tx_ran_msg, cell->conn, true);
2463 break;
2464 case VGCS_CELL_EV_RTP_STREAM_ESTABLISHED:
2465 /* The RTP stream established. */
2466 LOG_CELL(cell, LOGL_DEBUG, "RTP stream is established.\n");
2467 break;
2468 case VGCS_CELL_EV_ASSIGN_RES:
2469 /* Receive VGCS/VBS ASSIGNMENT RESULT from BSS. */
2470 LOG_CELL(cell, LOGL_DEBUG, "Received VGCS/VBS ASSIGNMENT RESULT from BSS.\n");
2471 cell->assigned = true;
2472 if (!rx_ran_msg->vgcs_vbs_assign_res.aoip_transport_layer_present
2473 && !rx_ran_msg->vgcs_vbs_assign_res.codec_present
2474 && !rx_ran_msg->vgcs_vbs_assign_res.call_id_present) {
2475 LOG_CELL(cell, LOGL_ERROR, "Mandatory IEs missing.\n");
2476 goto channel_fail;
2477 }
2478 /* Send remote peer to RTP stream. */
2479 if (osmo_sockaddr_str_from_sockaddr(&ss, &rx_ran_msg->vgcs_vbs_assign_res.aoip_transport_layer)) {
2480 LOG_CELL(cell, LOGL_ERROR, "Cannot RTP-CONNECT, invalid RTP IP:port in incoming MNCC "
2481 "message\n");
2482 goto channel_fail;
2483 }
2484 rtp_stream_set_remote_addr(cell->rtps, &ss);
2485 /* Send remote codec to RTP stream. */
2486 cm = codec_mapping_by_gsm0808_speech_codec_type(rx_ran_msg->vgcs_vbs_assign_res.codec_msc_chosen.type);
2487 if (!cm) {
2488 LOG_CELL(cell, LOGL_ERROR, "Chosen codec by BSC is not supported by MSC.\n");
2489 goto channel_fail;
2490 }
2491 rtp_stream_set_one_codec(cell->rtps, &cm->sdp);
2492 /* Set listening mode. */
2493 rtp_stream_set_mode(cell->rtps, MGCP_CONN_SEND_ONLY);
2494 /* Commit RTP stream. */
2495 rc = rtp_stream_commit(cell->rtps);
2496 if (rc < 0) {
2497 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream.\n");
2498 goto channel_fail;
2499 }
2500 /* Change state. */
2501 osmo_fsm_inst_state_chg(fi, VGCS_CELL_ST_ACTIVE, 0, 0);
2502 /* Notify BSS FSM about channel activation. */
2503 if (cell->bss)
2504 osmo_fsm_inst_dispatch(cell->bss->fi, VGCS_BSS_EV_ACTIVE_OR_FAIL, NULL);
2505 break;
2506 case VGCS_CELL_EV_ASSIGN_FAIL:
2507 /* Received VGCS/VBS ASSIGNMENT FAILURE from BSS. */
2508 LOG_CELL(cell, LOGL_NOTICE, "Received VGCS/VBS ASSIGNMENT FAILURE from BSS.\n");
2509channel_fail:
2510 bss = cell->bss;
2511 /* Remove cell. */
2512 tx_ran_msg = (struct ran_msg){
2513 .msg_type = RAN_MSG_CLEAR_COMMAND,
2514 .clear_command = {
2515 .gsm0808_cause = GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC,
2516 },
2517 };
Andreas Eversberg482f0bd2023-07-19 10:10:04 +02002518 ran_encode_and_send(fi, &tx_ran_msg, cell->conn, false);
Andreas Eversberge24636c2023-04-23 12:20:55 +02002519 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2520 /* Notify BSS FSM about channel failure. */
2521 if (bss)
2522 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_ACTIVE_OR_FAIL, NULL);
2523 break;
2524 case VGCS_CELL_EV_CLEAR:
2525 /* The calling user process requested clearing of VGCS/VBS call. */
2526 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2527 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2528 break;
2529 case VGCS_CELL_EV_CLOSE:
2530 /* The SCCP connection from the MSC has been closed. */
2531 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2532 if (cell->conn) {
2533 cell->conn->vgcs.bss = NULL;
2534 cell->conn = NULL;
2535 }
2536 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2537 break;
2538 default:
2539 OSMO_ASSERT(false);
2540 }
2541}
2542
2543static void vgcs_cell_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2544{
2545 struct vgcs_bss_cell *cell = fi->priv;
2546
2547 switch (event) {
2548 case VGCS_CELL_EV_RTP_STREAM_GONE:
2549 /* The RTP stream failed. */
2550 LOG_CELL(cell, LOGL_ERROR, "RTP stream of MGW failed.\n");
2551 cell->rtps = NULL;
2552 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2553 break;
2554 case VGCS_CELL_EV_RTP_STREAM_ESTABLISHED:
2555 /* The RTP stream established. */
2556 LOG_CELL(cell, LOGL_DEBUG, "RTP stream is established.\n");
2557 break;
2558 case VGCS_CELL_EV_CLEAR:
2559 /* The calling user process requested clearing of VGCS/VBS call. */
2560 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2561 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2562 break;
2563 case VGCS_CELL_EV_CLOSE:
2564 /* The SCCP connection from the MSC has been closed. */
2565 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2566 if (cell->conn) {
2567 cell->conn->vgcs.bss = NULL;
2568 cell->conn = NULL;
2569 }
2570 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2571 break;
2572 default:
2573 OSMO_ASSERT(false);
2574 }
2575}
2576
2577static void vgcs_cell_fsm_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2578{
2579 struct vgcs_bss_cell *cell = fi->priv;
2580
2581 switch (event) {
2582 case VGCS_CELL_EV_RTP_STREAM_GONE:
2583 /* The RTP stream gone. */
2584 LOG_CELL(cell, LOGL_ERROR, "RTP stream gone.\n");
2585 cell->rtps = NULL;
2586 /* Wait for RAN conn. */
2587 if (cell->conn)
2588 break;
2589 cell_destroy(cell);
2590 break;
2591 case VGCS_CELL_EV_CLEAR:
2592 case VGCS_CELL_EV_RELEASED:
2593 if (event == VGCS_CELL_EV_CLEAR) {
2594 /* The SCCP connection from the MSC has been closed while waiting for CLEAR COMPLETE. */
2595 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP closing collision.\n");
2596 } else
2597 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR COMPLETE from BSS, we are done!\n");
2598 /* Wait for RTP stream. */
2599 if (cell->rtps) {
2600 /* close RAN conn */
2601 if (cell->conn) {
2602 cell->conn->vgcs.cell = NULL;
2603 ran_conn_close(cell->conn);
2604 cell->conn = NULL;
2605 }
2606 break;
2607 }
2608 cell_destroy(cell);
2609 break;
2610 default:
2611 OSMO_ASSERT(false);
2612 }
2613}
2614
2615static const struct osmo_fsm_state vgcs_cell_fsm_states[] = {
2616 [VGCS_CELL_ST_NULL] = {
2617 .name = "NULL",
2618 .in_event_mask = S(VGCS_CELL_EV_ASSIGN) |
2619 S(VGCS_CELL_EV_CLEAR),
2620 .out_state_mask = S(VGCS_CELL_ST_ASSIGNMENT),
2621 .action = vgcs_cell_fsm_null,
2622 },
2623 [VGCS_CELL_ST_ASSIGNMENT] = {
2624 .name = "ASSIGNMENT Sent",
2625 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2626 S(VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE) |
2627 S(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED) |
2628 S(VGCS_CELL_EV_ASSIGN_RES) |
2629 S(VGCS_CELL_EV_ASSIGN_FAIL) |
2630 S(VGCS_CELL_EV_CLEAR) |
2631 S(VGCS_CELL_EV_CLOSE),
2632 .out_state_mask = S(VGCS_CELL_ST_ACTIVE) |
2633 S(VGCS_CELL_ST_RELEASE),
2634 .action = vgcs_cell_fsm_assignment,
2635 },
2636 [VGCS_CELL_ST_ACTIVE] = {
2637 .name = "VGCS/VBS channel active",
2638 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2639 S(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED) |
2640 S(VGCS_CELL_EV_CLEAR) |
2641 S(VGCS_CELL_EV_CLOSE),
2642 .out_state_mask = S(VGCS_CELL_ST_RELEASE),
2643 .action = vgcs_cell_fsm_active,
2644 },
2645 [VGCS_CELL_ST_RELEASE] = {
2646 .name = "Releasing VGCS/VBS channel",
2647 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2648 S(VGCS_CELL_EV_CLEAR) |
2649 S(VGCS_CELL_EV_RELEASED),
2650 .out_state_mask = S(VGCS_CELL_ST_NULL),
2651 .action = vgcs_cell_fsm_release,
2652 },
2653};
2654
2655static struct osmo_fsm vgcs_cell_fsm = {
2656 .name = "vgcs_cell",
2657 .states = vgcs_cell_fsm_states,
2658 .num_states = ARRAY_SIZE(vgcs_cell_fsm_states),
2659 .log_subsys = DASCI,
2660 .event_names = vgcs_cell_fsm_event_names,
2661};
2662
2663/* The BSS accepts VGCS/VBS channel assignment. */
2664void vgcs_vbs_assign_result(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2665{
2666 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN_RES, (void *)ran_msg);
2667}
2668
2669/* The BSS refuses VGCS/VBS channel assignment. */
2670void vgcs_vbs_assign_fail(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2671{
2672 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN_FAIL, (void *)ran_msg);
2673}
2674
2675/* BSS indicated that the channel has been released. */
2676void vgcs_vbs_clear_req_channel(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2677{
2678 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR REQUEST for resource controling channel from BSS.\n");
2679 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_CLOSE, (void *)ran_msg);
2680}
2681
2682/* BSS confirms the release of channel. */
2683void vgcs_vbs_clear_cpl_channel(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2684{
2685 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR COMPLETE for resource controling channel from BSS.\n");
2686 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_RELEASED, (void *)ran_msg);
2687}
2688
2689/*
2690 * MGW endpoint FSM
2691 */
2692
2693static const struct value_string vgcs_mgw_ep_fsm_event_names[] = {
2694 OSMO_VALUE_STRING(VGCS_MGW_EP_EV_FREE),
2695 OSMO_VALUE_STRING(VGCS_MGW_EP_EV_CLEAR),
2696 { }
2697};
2698
2699static void vgcs_mgw_ep_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2700{
2701 struct vgcs_mgw_ep *mgw = fi->priv;
2702 struct vgcs_bss_cell *cell, *cell2;
2703 struct mgcp_client *mgcp_client;
2704
2705 switch (event) {
2706 case VGCS_MGW_EP_EV_FREE:
2707 LOGP(DASCI, LOGL_DEBUG, "MGW connection closed, removing all cell instances.\n");
2708 llist_for_each_entry_safe(cell, cell2, &mgw->cell_list, list_mgw) {
2709 if (cell->rtps)
2710 cell->rtps->ci = NULL;
2711 llist_del(&cell->list_mgw);
2712 cell->mgw = NULL;
2713 }
2714 /* Put MGCP client back into MGW pool. */
2715 mgcp_client = osmo_mgcpc_ep_client(mgw->mgw_ep);
2716 mgcp_client_pool_put(mgcp_client);
2717 /* Destroy this instance. */
2718 osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL);
2719 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
2720 break;
2721 case VGCS_MGW_EP_EV_CLEAR:
2722 if (!llist_empty(&mgw->cell_list))
2723 break;
2724 LOGP(DASCI, LOGL_DEBUG, "Cell list of MGW instance is now empty, dropping.\n");
2725 /* Destroy this instance. */
2726 osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL);
2727 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
2728 break;
2729 default:
2730 OSMO_ASSERT(false);
2731 }
2732}
2733
2734static const struct osmo_fsm_state vgcs_mgw_ep_fsm_states[] = {
2735 [VGCS_MGW_EP_ST_NULL] = {
2736 .name = "NULL",
2737 .out_state_mask = S(VGCS_MGW_EP_ST_ACTIVE),
2738 },
2739 [VGCS_MGW_EP_ST_ACTIVE] = {
2740 .name = "MGW endpoint allocated",
2741 .in_event_mask = S(VGCS_MGW_EP_EV_FREE) |
2742 S(VGCS_MGW_EP_EV_CLEAR),
2743 .out_state_mask = S(VGCS_MGW_EP_ST_NULL),
2744 .action = vgcs_mgw_ep_fsm_active,
2745 },
2746};
2747
2748static struct osmo_fsm vgcs_mgw_ep_fsm = {
2749 .name = "vgcs_mgw_ep",
2750 .states = vgcs_mgw_ep_fsm_states,
2751 .num_states = ARRAY_SIZE(vgcs_mgw_ep_fsm_states),
2752 .log_subsys = DASCI,
2753 .event_names = vgcs_mgw_ep_fsm_event_names,
2754};