blob: 264fa4953bd731a19484456792ba6ad145d7631e [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
Andreas Eversberg1d3e48f2023-08-21 15:54:51 +0200862/* Send TERMINATION REJECT to the calling/talking subscriber. */
863static void gcc_termination_reject(struct gsm_trans *trans, enum osmo_gsm44068_cause cause)
864{
865 uint8_t pdisc = (trans->type == TRANS_GCC) ? GSM48_PDISC_GROUP_CC : GSM48_PDISC_BCAST_CC;
866 int rc;
867
868 /* Send TERMINATION towards MS. */
869 rc = gsm44068_tx_termination(trans->msc_a, trans->gcc.uplink_bss,
870 pdisc | (trans->transaction_id << 4),
871 OSMO_GSM44068_MSGT_TERMINATION_REJECT,
872 cause, NULL, 0);
873 if (rc < 0)
874 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION REJECT towards MS.\n");
875}
876
Andreas Eversberge24636c2023-04-23 12:20:55 +0200877/* Start inactivity timer.
878 * This timer is used to terminate the call, if the radio connection to the caller gets lost. */
879static void start_inactivity_timer(struct gsm_trans *trans)
880{
881 if (trans->gcc.inactivity_to) {
882 LOG_GCC(trans, LOGL_DEBUG, "Set inactivity timer to %d seconds.\n", trans->gcc.inactivity_to);
883 osmo_timer_schedule(&trans->gcc.timer_inactivity, trans->gcc.inactivity_to, 0);
884 }
885}
886
887static void stop_inactivity_timer(struct gsm_trans *trans)
888{
889 if (osmo_timer_pending(&trans->gcc.timer_inactivity)) {
890 LOG_GCC(trans, LOGL_DEBUG, "Stop pending inactivity timer.\n");
891 osmo_timer_del(&trans->gcc.timer_inactivity);
892 }
893}
894
895static void inactivity_timer_cb(void *data)
896{
897 struct gsm_trans *trans = data;
898
899 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_TIMEOUT, NULL);
900}
901
902/* Set the parameters of the talker. (downlink mute/unmute, uplink unmute, COMM=T, originator) */
903static int set_parameter(struct gsm_trans *trans)
904{
905 uint8_t pdisc = (trans->type == TRANS_GCC) ? GSM48_PDISC_GROUP_CC : GSM48_PDISC_BCAST_CC;
906 int rc;
907
Andreas Eversberg1c980312023-07-27 13:18:27 +0200908 rc = gsm44068_tx_set_parameter(trans, pdisc | (trans->transaction_id << 4),
909 !trans->gcc.mute_talker, 1, 1, trans->gcc.uplink_originator);
Andreas Eversberge24636c2023-04-23 12:20:55 +0200910 if (rc < 0)
911 LOG_GCC(trans, LOGL_ERROR, "Failed to send SET PARAMETER towards MS.\n");
912 return rc;
913}
914
915/* Check in which cell the uplink is used and set "uplink_cell". */
916static int set_uplink_cell(struct vgcs_bss *bss, struct gsm0808_cell_id *cell_id_ie, uint16_t cell_id)
917{
918 struct vgcs_bss_cell *cell;
919
920 if (cell_id_ie) {
921 /* Get cell ID to determine talker channel. */
922 switch (cell_id_ie->id_discr) {
923 case CELL_IDENT_CI:
924 cell_id = cell_id_ie->id.ci;
925 break;
926 case CELL_IDENT_LAC_AND_CI:
927 cell_id = cell_id_ie->id.lac_and_ci.ci;
928 break;
929 default:
930 LOG_BSS(bss, LOGL_DEBUG, "Cannot idenitfy cell, please fix!\n");
931 return -EINVAL;
932 }
933 }
934
935 /* Search for cell ID. */
936 bss->trans->gcc.uplink_cell = NULL;
937 llist_for_each_entry(cell, &bss->cell_list, list_bss) {
938 if (cell->cell_id == cell_id) {
939 LOG_BSS(bss, LOGL_DEBUG, "Talker is talking on cell %d.\n", cell->cell_id);
940 bss->trans->gcc.uplink_cell = cell;
941 return 0;
942 }
943 }
944
945 LOG_BSS(bss, LOGL_DEBUG, "Cell ID %d is not in list of current BSS, please fix!\n", cell_id);
946 return -EINVAL;
947}
948
949/* Set the MGW conference mode.
950 * All cells are listening to the conference. If there is a talker, this cell is also transmitting to the conference. */
951static int set_mgw_conference(struct gsm_trans *trans)
952{
953 struct vgcs_bss *bss;
954 struct vgcs_bss_cell *cell;
955 struct rtp_stream *rtps;
956 int rc;
957
958 /* All cells without talker are listening */
959 llist_for_each_entry(bss, &trans->gcc.bss_list, list) {
960 llist_for_each_entry(cell, &bss->cell_list, list_bss) {
961 if (!(rtps = cell->rtps))
962 continue;
963 if (rtps->crcx_conn_mode != MGCP_CONN_SEND_ONLY) {
964 LOG_CELL(cell, LOGL_DEBUG, "Setting cell %d into listening mode.\n", cell->cell_id);
965 rtp_stream_set_mode(rtps, MGCP_CONN_SEND_ONLY);
966 rc = rtp_stream_commit(rtps);
967 if (rc < 0)
968 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream "
969 "for cell %d.\n", cell->cell_id);
970 }
971 }
972 }
973
974 if (trans->gcc.uplink_cell && trans->gcc.uplink_cell->rtps) {
975 cell = trans->gcc.uplink_cell;
976 rtps = cell->rtps;
977 LOG_CELL(cell, LOGL_DEBUG, "Setting cell %d into listening mode.\n", cell->cell_id);
978 rtp_stream_set_mode(rtps, MGCP_CONN_CONFECHO);
979 rc = rtp_stream_commit(rtps);
980 if (rc < 0)
981 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream "
982 "for cell %d.\n", cell->cell_id);
983 }
984
985 return 0;
986}
987
988static void _assign_complete(struct gsm_trans *trans, bool send_connect)
989{
990 uint16_t cell_id;
991
Andreas Eversberge2a40882023-07-19 09:56:53 +0200992 OSMO_ASSERT(trans->msc_a);
993
Andreas Eversberge24636c2023-04-23 12:20:55 +0200994 /* Change state. */
995 osmo_fsm_inst_state_chg(trans->gcc.fi, VGCS_GCC_ST_N2_CALL_ACTIVE, 0, 0);
996 /* Get cell ID. */
Andreas Eversberge2a40882023-07-19 09:56:53 +0200997 cell_id = trans->msc_a->via_cell.cell_identity;
Andreas Eversberge24636c2023-04-23 12:20:55 +0200998 /* Releasing dedicated channel. */
999 release_msc_a(trans);
1000 /* Send CONNECT to the calling subscriber. */
1001 if (send_connect)
1002 gcc_connect(trans);
1003 /* Set parameter. */
1004 set_parameter(trans);
1005 /* Start inactivity timer, if uplink is free. */
1006 if (!trans->gcc.uplink_busy)
1007 start_inactivity_timer(trans);
1008 /* Set cell of current talker. */
1009 set_uplink_cell(trans->gcc.uplink_bss, NULL, cell_id);
1010 /* Set MGW conference. */
1011 set_mgw_conference(trans);
1012}
1013
1014#define CONNECT_OPTION false
1015
1016static void vgcs_gcc_fsm_n0_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1017{
1018 struct gsm_trans *trans = fi->priv;
1019 int rc;
1020
1021 switch (event) {
1022 case VGCS_GCC_EV_NET_SETUP:
1023 /* Establish call towards all BSSs. */
1024 LOG_GCC(trans, LOGL_DEBUG, "Setup by network, trying to establish cells.\n");
1025 rc = gcc_establish_bss(trans);
1026 if (rc < 0) {
1027 LOG_GCC(trans, LOGL_NOTICE, "Failed to setup call to any cell.\n");
1028 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1029 break;
1030 }
1031 /* Keep state until established or released. */
1032 break;
1033 case VGCS_GCC_EV_NET_TERM:
1034 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1035 /* Destroy group call in all cells. */
1036 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1037 break;
1038 case VGCS_GCC_EV_USER_SETUP:
1039 LOG_GCC(trans, LOGL_DEBUG, "Setup by MS, trying to establish cells.\n");
1040 /* Change state. */
1041 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N1_CALL_INITIATED, 0, 0);
1042 /* Establish call towards all BSSs. */
1043 rc = gcc_establish_bss(trans);
1044 if (rc < 0) {
1045 LOG_GCC(trans, LOGL_NOTICE, "Failed to setup call to any cell.\n");
1046 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1047 break;
1048 }
1049 if (CONNECT_OPTION) {
1050 /* Send CONNECT to the calling subscriber. */
1051 gcc_connect(trans);
1052 /* Change state. */
1053 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N3_CALL_EST_PROC, 0, 0);
1054 }
1055 break;
1056 case VGCS_GCC_EV_BSS_ESTABLISHED:
1057 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, sending CONNECT to caller.\n");
1058 /* Change state. */
1059 osmo_fsm_inst_state_chg(fi, VGCS_GCC_ST_N2_CALL_ACTIVE, 0, 0);
1060 /* Start inactivity timer, if uplink is free. */
1061 if (!trans->gcc.uplink_busy)
1062 start_inactivity_timer(trans);
1063 break;
1064 case VGCS_GCC_EV_BSS_RELEASED:
1065 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1066 /* Send TERMINATE to the calling subscriber. */
1067 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1068 break;
1069 default:
1070 OSMO_ASSERT(false);
1071 }
1072}
1073
1074static void vgcs_gcc_fsm_n1_call_initiated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1075{
1076 struct gsm_trans *trans = fi->priv;
1077 int rc;
1078
1079 switch (event) {
1080 case VGCS_GCC_EV_NET_TERM:
1081 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1082 /* Destroy group call in all cells. */
1083 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1084 break;
1085 case VGCS_GCC_EV_USER_TERM:
1086 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1087 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1088 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1089 break;
1090 case VGCS_GCC_EV_BSS_ESTABLISHED:
1091 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, assign caller to VGCS.\n");
1092 /* Send assignment to the calling subscriber. */
1093 rc = gcc_assign(trans);
1094 if (rc < 0) {
1095 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1096 break;
1097 }
1098 break;
1099 case VGCS_GCC_EV_BSS_ASSIGN_CPL:
1100 LOG_GCC(trans, LOGL_DEBUG, "Assignment complete, sending CONNECT to caller, releasing channel.\n");
1101 /* Handle assignment complete */
1102 _assign_complete(trans, true);
1103 break;
1104 case VGCS_GCC_EV_BSS_ASSIGN_FAIL:
1105 LOG_GCC(trans, LOGL_DEBUG, "Assignment failed, releasing call.\n");
1106 /* Send TERMINATE to the calling subscriber. */
1107 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1108 break;
1109 case VGCS_GCC_EV_BSS_RELEASED:
1110 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1111 /* Send TERMINATE to the calling subscriber. */
1112 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1113 break;
1114 default:
1115 OSMO_ASSERT(false);
1116 }
1117}
1118
1119static void vgcs_gcc_fsm_n2_call_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1120{
1121 struct gsm_trans *trans = fi->priv;
1122
1123 switch (event) {
1124 case VGCS_GCC_EV_NET_TERM:
1125 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, 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 case VGCS_GCC_EV_USER_TERM:
Andreas Eversberg1d3e48f2023-08-21 15:54:51 +02001130 if (!trans->gcc.uplink_originator) {
1131 LOG_GCC(trans, LOGL_ERROR, "Termination by user, but it is not the originator.\n");
1132 gcc_termination_reject(trans, OSMO_GSM44068_CAUSE_USER_NOT_ORIGINATOR);
1133 break;
1134 }
Andreas Eversberge24636c2023-04-23 12:20:55 +02001135 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1136 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1137 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1138 break;
1139 case VGCS_GCC_EV_BSS_RELEASED:
1140 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1141 /* Send TERMINATE to the calling subscriber. */
1142 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1143 break;
1144 case VGCS_GCC_EV_TIMEOUT:
1145 LOG_GCC(trans, LOGL_DEBUG, "Termination by inactivity timer, destroying call.\n");
1146 /* Destroy group call in all cells. */
1147 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1148 break;
1149 default:
1150 OSMO_ASSERT(false);
1151 }
1152}
1153
1154static void vgcs_gcc_fsm_n3_call_est_proc(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1155{
1156 struct gsm_trans *trans = fi->priv;
1157 int rc;
1158
1159 switch (event) {
1160 case VGCS_GCC_EV_NET_TERM:
1161 LOG_GCC(trans, LOGL_DEBUG, "Termination by network, destroying call.\n");
1162 /* Destroy group call in all cells. */
1163 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1164 break;
1165 case VGCS_GCC_EV_USER_TERM:
1166 LOG_GCC(trans, LOGL_DEBUG, "Termination by user, destroying call.\n");
1167 /* Send TERMINATE to the calling subscriber and destroy group call in all cells. */
1168 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
1169 break;
1170 case VGCS_GCC_EV_BSS_ESTABLISHED:
1171 LOG_GCC(trans, LOGL_DEBUG, "All cells establised, for a group call, assign caller to VGCS.\n");
1172 /* Send assignment to the calling subscriber. */
1173 rc = gcc_assign(trans);
1174 if (rc < 0) {
1175 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1176 break;
1177 }
1178 break;
1179 case VGCS_GCC_EV_BSS_ASSIGN_CPL:
1180 LOG_GCC(trans, LOGL_DEBUG, "Assignment complete, sending CONNECT to caller, releasing channel.\n");
1181 /* Handle assignment complete */
1182 _assign_complete(trans, false);
1183 break;
1184 case VGCS_GCC_EV_BSS_ASSIGN_FAIL:
1185 LOG_GCC(trans, LOGL_DEBUG, "Assignment failed, releasing call.\n");
1186 /* Send TERMINATE to the calling subscriber. */
1187 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1188 break;
1189 case VGCS_GCC_EV_BSS_RELEASED:
1190 LOG_GCC(trans, LOGL_DEBUG, "All group call in all cells failed, destroying call.\n");
1191 /* Send TERMINATE to the calling subscriber. */
1192 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1193 break;
1194 default:
1195 OSMO_ASSERT(false);
1196 }
1197}
1198
1199static const struct osmo_fsm_state vgcs_gcc_fsm_states[] = {
1200 [VGCS_GCC_ST_N0_NULL] = {
1201 .name = "NULL (N0)",
1202 .in_event_mask = S(VGCS_GCC_EV_NET_SETUP) |
1203 S(VGCS_GCC_EV_NET_TERM) |
1204 S(VGCS_GCC_EV_USER_SETUP) |
1205 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1206 S(VGCS_GCC_EV_BSS_RELEASED),
1207 .out_state_mask = S(VGCS_GCC_ST_N1_CALL_INITIATED) |
1208 S(VGCS_GCC_ST_N2_CALL_ACTIVE),
1209 .action = vgcs_gcc_fsm_n0_null,
1210 },
1211 [VGCS_GCC_ST_N1_CALL_INITIATED] = {
1212 .name = "CALL INITATED (N1)",
1213 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1214 S(VGCS_GCC_EV_USER_TERM) |
1215 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1216 S(VGCS_GCC_EV_BSS_ASSIGN_CPL) |
1217 S(VGCS_GCC_EV_BSS_ASSIGN_FAIL) |
1218 S(VGCS_GCC_EV_BSS_RELEASED),
1219 .out_state_mask = S(VGCS_GCC_ST_N0_NULL) |
1220 S(VGCS_GCC_ST_N2_CALL_ACTIVE) |
1221 S(VGCS_GCC_ST_N3_CALL_EST_PROC),
1222 .action = vgcs_gcc_fsm_n1_call_initiated,
1223 },
1224 [VGCS_GCC_ST_N2_CALL_ACTIVE] = {
1225 .name = "CALL ACTIVE (N2)",
1226 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1227 S(VGCS_GCC_EV_USER_TERM) |
1228 S(VGCS_GCC_EV_BSS_RELEASED) |
1229 S(VGCS_GCC_EV_TIMEOUT),
1230 .out_state_mask = S(VGCS_GCC_ST_N0_NULL),
1231 .action = vgcs_gcc_fsm_n2_call_active,
1232 },
1233 [VGCS_GCC_ST_N3_CALL_EST_PROC] = {
1234 .name = "CALL EST PROCEEDING (N3)",
1235 .in_event_mask = S(VGCS_GCC_EV_NET_TERM) |
1236 S(VGCS_GCC_EV_USER_TERM) |
1237 S(VGCS_GCC_EV_BSS_ESTABLISHED) |
1238 S(VGCS_GCC_EV_BSS_ASSIGN_CPL) |
1239 S(VGCS_GCC_EV_BSS_ASSIGN_FAIL) |
1240 S(VGCS_GCC_EV_BSS_RELEASED),
1241 .out_state_mask = S(VGCS_GCC_ST_N2_CALL_ACTIVE) |
1242 S(VGCS_GCC_ST_N0_NULL),
1243 .action = vgcs_gcc_fsm_n3_call_est_proc,
1244 },
1245 // We don't need a state to wait for the group call to be terminated in all cells
1246};
1247
1248static struct osmo_fsm vgcs_bcc_fsm = {
1249 .name = "bcc",
1250 .states = vgcs_gcc_fsm_states,
1251 .num_states = ARRAY_SIZE(vgcs_gcc_fsm_states),
1252 .log_subsys = DBCC,
1253 .event_names = vgcs_gcc_fsm_event_names,
1254};
1255
1256static struct osmo_fsm vgcs_gcc_fsm = {
1257 .name = "gcc",
1258 .states = vgcs_gcc_fsm_states,
1259 .num_states = ARRAY_SIZE(vgcs_gcc_fsm_states),
1260 .log_subsys = DGCC,
1261 .event_names = vgcs_gcc_fsm_event_names,
1262};
1263
1264const char *vgcs_bcc_gcc_state_name(struct osmo_fsm_inst *fi)
1265{
1266 return vgcs_gcc_fsm_states[fi->state].name;
1267}
1268
1269static int update_uplink_state(struct vgcs_bss *bss, bool uplink_busy);
1270
1271/* Receive RR messages from calling subscriber, prior assignment to VGCS/VBS. */
1272int gsm44068_rcv_rr(struct msc_a *msc_a, struct msgb *msg)
1273{
1274 struct gsm_trans *trans = NULL;
1275 struct gsm48_hdr *gh;
1276 uint8_t msg_type;
1277
1278 gh = msgb_l3(msg);
1279 msg_type = gsm48_hdr_msg_type(gh);
1280
1281 /* Find transaction. */
1282 trans = trans_find_by_type(msc_a, TRANS_GCC);
1283 if (!trans)
1284 trans = trans_find_by_type(msc_a, TRANS_BCC);
1285
1286 if (!trans) {
1287 LOG_GCC(trans, LOGL_ERROR, "No VGCS/VBS transaction.\n");
1288 return -EINVAL;
1289 }
1290
1291 /* In case the phone releases uplink prior being assigned to a VGCS */
1292 if (msg_type == GSM48_MT_RR_UPLINK_RELEASE) {
1293 struct vgcs_bss *bss;
1294
1295 LOG_GCC(trans, LOGL_INFO, "Received UPLINK RELEASE on initial channel.\n");
1296 /* Clear the busy flag and unblock all cells. */
1297 trans->gcc.uplink_bss = NULL;
1298 trans->gcc.uplink_cell = NULL;
1299 trans->gcc.uplink_busy = false;
1300 llist_for_each_entry(bss, &trans->gcc.bss_list, list) {
1301 /* Update uplink state. */
1302 update_uplink_state(bss, trans->gcc.uplink_busy);
1303 }
1304 /* Start inactivity timer. */
1305 start_inactivity_timer(bss->trans);
1306 /* Next, the MS will switch to the VGCS as listener. Nothing else to do here. */
1307 }
1308
1309 return 0;
1310}
1311
1312/* Allocation of transaction for group call */
1313static struct gsm_trans *trans_alloc_vgcs(struct gsm_network *net,
1314 struct vlr_subscr *vsub,
1315 enum trans_type trans_type, uint8_t transaction_id,
1316 uint32_t callref,
1317 struct gcr *gcr,
1318 bool uplink_busy)
1319{
1320 struct gsm_trans *trans;
1321
1322 trans = trans_alloc(net, vsub, trans_type, transaction_id, callref);
1323 if (!trans) {
1324 LOG_GCC(trans, LOGL_ERROR, "No memory for trans.\n");
1325 return NULL;
1326 }
1327 /* The uplink is busy when the call is started until the calling subscriber releases. */
1328 trans->gcc.uplink_busy = uplink_busy;
1329 trans->gcc.uplink_originator = true;
1330 INIT_LLIST_HEAD(&trans->gcc.bss_list);
1331 trans->gcc.inactivity_to = gcr->timeout;
1332 trans->gcc.mute_talker = gcr->mute_talker;
1333 trans->gcc.timer_inactivity.data = trans;
1334 trans->gcc.timer_inactivity.cb = inactivity_timer_cb;
1335 trans->gcc.fi = osmo_fsm_inst_alloc((trans_type == TRANS_GCC) ? &vgcs_gcc_fsm : &vgcs_bcc_fsm,
1336 trans, trans, LOGL_DEBUG, NULL);
1337 if (!trans->gcc.fi) {
1338 LOG_GCC(trans, LOGL_ERROR, "No memory for state machine.\n");
1339 trans_free(trans);
1340 return NULL;
1341 }
1342
1343 return trans;
1344}
1345
1346/* Create transaction from incoming voice group/broadcast call. */
1347static struct gsm_trans *trans_create_bcc_gcc(struct msc_a *msc_a, enum trans_type trans_type, uint8_t transaction_id,
1348 uint8_t pdisc, uint8_t msg_type, uint32_t callref)
1349{
Andreas Eversberg7e65ed92023-07-19 09:38:25 +02001350 struct gsm_network *net;
1351 struct vlr_subscr *vsub;
Andreas Eversberge24636c2023-04-23 12:20:55 +02001352 struct gsm_trans *trans = NULL;
1353 struct gcr *gcr;
1354 int rc;
1355
1356 if (!msc_a) {
1357 LOG_GCC(trans, LOGL_ERROR, "Invalid conn: no msc_a\n");
1358 return NULL;
1359 }
Andreas Eversberg7e65ed92023-07-19 09:38:25 +02001360 net = msc_a_net(msc_a);
1361 vsub = msc_a_vsub(msc_a);
1362
Andreas Eversberge24636c2023-04-23 12:20:55 +02001363 if (!vsub) {
1364 LOG_GCC(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
1365 return NULL;
1366 }
1367
1368 /* An earlier CM Service Request for this CC message now has concluded */
1369 if (!osmo_use_count_by(&msc_a->use_count,
1370 (trans_type == TRANS_GCC) ? MSC_A_USE_CM_SERVICE_GCC : MSC_A_USE_CM_SERVICE_BCC))
1371 LOG_MSC_A(msc_a, LOGL_ERROR,
1372 "Creating new %s transaction without prior CM Service Request.\n",
1373 get_value_string(trans_type_names, trans_type));
1374 else
1375 msc_a_put(msc_a,
1376 (trans_type == TRANS_GCC) ? MSC_A_USE_CM_SERVICE_GCC : MSC_A_USE_CM_SERVICE_BCC);
1377
1378 /* A transaction must be created with a SETUP message. */
1379 if (msg_type != OSMO_GSM44068_MSGT_IMMEDIATE_SETUP
1380 && msg_type != OSMO_GSM44068_MSGT_SETUP
1381 && msg_type != OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2) {
1382 LOG_GCC(trans, LOGL_ERROR, "No transaction and message is not a SETUP.\n");
1383 return NULL;
1384 }
1385
1386 /* Check if callref already exists. */
1387 trans = trans_find_by_callref(net, trans_type, callref);
1388 if (trans) {
1389 LOG_GCC(trans, LOGL_INFO, "Call to existing %s with callref %s, rejecting!\n",
1390 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1391 rc = gsm44068_tx_termination(msc_a, NULL,
1392 pdisc | (transaction_id << 4),
1393 OSMO_GSM44068_MSGT_TERMINATION,
1394 OSMO_GSM44068_CAUSE_BUSY, NULL, 0);
1395 if (rc < 0)
1396 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1397 return 0;
1398 }
1399
1400 /* Check GCR for Group ID. */
1401 gcr = gcr_by_callref(net, trans_type, callref);
1402 if (!gcr) {
1403 LOG_GCC(trans, LOGL_INFO, "No Group configured for %s callref %s, rejecting!\n",
1404 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1405 // FIXME: Better cause value for a group that does not exist ?
1406 rc = gsm44068_tx_termination(msc_a, NULL,
1407 pdisc | (transaction_id << 4),
1408 OSMO_GSM44068_MSGT_TERMINATION,
1409 OSMO_GSM44068_CAUSE_REQUESTED_SERVICE_NOT_SUB, NULL, 0);
1410 if (rc < 0)
1411 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1412 return 0;
1413 }
1414
1415 /* Create transaction, uplink is busy. */
1416 trans = trans_alloc_vgcs(net, vsub, trans_type, transaction_id, callref, gcr, true);
1417 if (!trans) {
1418 rc = gsm44068_tx_termination(msc_a, NULL,
1419 pdisc | (transaction_id << 4),
1420 OSMO_GSM44068_MSGT_TERMINATION,
1421 OSMO_GSM44068_CAUSE_NETWORK_FAILURE, NULL, 0);
1422 if (rc < 0)
1423 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1424 return NULL;
1425 }
1426
1427 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
1428 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept %s transaction.\n",
1429 get_value_string(trans_type_names, trans_type));
1430 gcc_terminate_and_destroy(trans, OSMO_GSM44068_CAUSE_NETWORK_FAILURE);
1431 return NULL;
1432 }
1433
1434 /* Assign transaction */
1435 msc_a_get(msc_a, (trans_type == TRANS_GCC) ? MSC_A_USE_GCC : MSC_A_USE_BCC);
1436 trans->msc_a = msc_a;
1437 trans->dlci = 0; /* main DCCH */
1438
1439 return trans;
1440}
1441
1442/* Receive GCC/BCC messages from calling subscriber, depending on the PDISC used. */
1443int gsm44068_rcv_bcc_gcc(struct msc_a *msc_a, struct gsm_trans *trans, struct msgb *msg)
1444{
1445 struct gsm48_hdr *gh = msgb_l3(msg);
1446 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1447 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1448 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
1449 enum trans_type trans_type = (pdisc == GSM48_PDISC_GROUP_CC) ? TRANS_GCC : TRANS_BCC;
1450
1451 uint8_t key_seq;
1452 bool talker_prio_requested;
1453 bool with_talker_prio;
1454 uint8_t talker_prio;
1455 struct gsm48_classmark2 cm2;
1456 struct osmo_mobile_identity mi;
1457 uint32_t callref;
1458 bool with_prio;
1459 uint8_t prio;
1460 char user_user[64] = "";
1461 uint8_t cause;
1462 uint8_t diag[256];
1463 uint8_t diag_len;
1464 bool with_call_state;
1465 enum osmo_gsm44068_call_state call_state;
1466 bool with_state_attrs;
1467 uint8_t da, ua, comm, oi;
1468 int rc = 0;
1469
1470 /* Remove sequence number (bit 7) from message type. */
1471 msg_type &= 0xbf;
1472
1473 /* Parse messages. */
1474 switch (msg_type) {
1475 case OSMO_GSM44068_MSGT_SETUP:
1476 rc = gsm44068_rx_setup(msg, &talker_prio_requested, &talker_prio, &callref, &with_prio, &prio,
1477 user_user);
1478 break;
1479 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP:
1480 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2:
1481 rc = gsm44068_rx_immediate_setup(msg, &talker_prio, &key_seq, &cm2, &mi, &callref, &with_prio, &prio,
1482 user_user);
1483 break;
1484 case OSMO_GSM44068_MSGT_STATUS:
1485 rc = gsm44068_rx_status(msg, &cause, diag, &diag_len, &with_call_state, &call_state,
1486 &with_state_attrs, &da, &ua, &comm, &oi);
1487 break;
1488 case OSMO_GSM44068_MSGT_TERMINATION_REQUEST:
1489 rc = gsm44068_rx_termination_req(msg, &callref, &with_prio, &prio, &with_talker_prio, &talker_prio);
1490 break;
1491 default:
1492 LOG_GCC(trans, LOGL_ERROR, "Invalid message type: 0x%02x\n", msg_type);
1493 return -EINVAL;
1494 }
1495 if (rc < 0)
1496 return rc;
1497
1498 /* Find transaction, if called from msc_a. */
1499 if (!trans)
1500 trans = trans_find_by_id(msc_a, trans_type, transaction_id);
1501
1502 /* Create transaction for SETUP message. */
1503 if (!trans) {
1504 trans = trans_create_bcc_gcc(msc_a, trans_type, transaction_id, pdisc, msg_type, callref);
1505 if (!trans)
1506 return -EINVAL;
1507 } else {
1508 /* A phone may not call while a VGCS is already active */
1509 if (msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP
1510 || msg_type == OSMO_GSM44068_MSGT_SETUP
1511 || msg_type == OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2) {
1512 LOG_GCC(trans, LOGL_ERROR, "Received SETUP while call is already set up, rejecting.\n");
1513 rc = gsm44068_tx_termination(msc_a, NULL,
1514 pdisc | (transaction_id << 4),
1515 OSMO_GSM44068_MSGT_TERMINATION,
1516 OSMO_GSM44068_CAUSE_NETWORK_FAILURE, NULL, 0);
1517 if (rc < 0)
1518 LOG_GCC(trans, LOGL_ERROR, "Failed to send TERMINATION towards MS.\n");
1519 return -EINVAL;
1520 }
1521 }
1522
1523 /* Handle received GCC messages (trigger state machine). */
1524 switch (msg_type) {
1525 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP:
1526 case OSMO_GSM44068_MSGT_SETUP:
1527 case OSMO_GSM44068_MSGT_IMMEDIATE_SETUP_2:
1528 LOG_GCC(trans, LOGL_INFO, "Received SETUP.\n");
1529 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_USER_SETUP, NULL);
1530 break;
1531 case OSMO_GSM44068_MSGT_STATUS:
1532 LOG_GCC(trans, LOGL_NOTICE, "Received STATUS with cause %d (%s).\n", cause,
1533 get_value_string(osmo_gsm44068_cause_names, cause));
1534 if (diag_len)
1535 LOG_GCC(trans, LOGL_NOTICE, " -> diagnostics: %s\n", osmo_hexdump(diag, diag_len));
1536 if (with_call_state)
1537 LOG_GCC(trans, LOGL_NOTICE, " -> call state %s\n",
1538 get_value_string(osmo_gsm44068_call_state_names, call_state));
1539 break;
1540 case OSMO_GSM44068_MSGT_TERMINATION_REQUEST:
1541 LOG_GCC(trans, LOGL_INFO, "Received TERMINATRION REQUEST.\n");
1542 if (callref != trans->callref) {
1543 LOG_GCC(trans, LOGL_NOTICE, "Received callref 0x%x does not match!\n", callref);
1544 break;
1545 }
1546 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_USER_TERM, NULL);
1547 break;
1548 }
1549
1550 return 0;
1551}
1552
1553static void bss_clear(struct vgcs_bss *bss, uint8_t cause, bool notify_trans);
1554
1555/* Call Control Specific transaction release.
1556 * gets called by trans_free, DO NOT CALL YOURSELF! */
1557void gsm44068_bcc_gcc_trans_free(struct gsm_trans *trans)
1558{
1559 struct vgcs_bss *bss, *bss2;
1560
Andreas Eversberge24636c2023-04-23 12:20:55 +02001561 /* Free FSM. */
Andreas Eversbergb865c622023-07-19 10:06:07 +02001562 if (trans->gcc.fi) {
1563 osmo_fsm_inst_state_chg(trans->gcc.fi, VGCS_GCC_ST_N0_NULL, 0, 0);
Andreas Eversberge24636c2023-04-23 12:20:55 +02001564 osmo_fsm_inst_term(trans->gcc.fi, OSMO_FSM_TERM_REGULAR, NULL);
Andreas Eversbergb865c622023-07-19 10:06:07 +02001565 }
Andreas Eversberge24636c2023-04-23 12:20:55 +02001566
1567 /* Remove relations to cells.
1568 * We must loop safe, because bss_clear() will detach every call control instance from list. */
1569 llist_for_each_entry_safe(bss, bss2, &trans->gcc.bss_list, list)
1570 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_CLEAR, NULL);
1571
1572 /* Stop inactivity timer. */
1573 stop_inactivity_timer(trans);
1574}
1575
1576/* Create a new call from VTY command. */
1577const char *vgcs_vty_initiate(struct gsm_network *gsmnet, struct gcr *gcr)
1578{
1579 enum trans_type trans_type;
1580 uint32_t callref;
1581 struct gsm_trans *trans;
1582
1583 /* Get callref from stored suffix. Caller cannot choose a prefix. */
1584 trans_type = gcr->trans_type;
1585 callref = atoi(gcr->group_id);
1586
1587 /* Check if callref already exists. */
1588 trans = trans_find_by_callref(gsmnet, trans_type, callref);
1589 if (trans) {
1590 LOG_GCC(trans, LOGL_INFO, "Call to existing %s with callref %s, rejecting!\n",
1591 trans_type_name(trans_type), gsm44068_group_id_string(callref));
1592 return "Call already exists.";
1593 }
1594
1595 /* Create transaction, uplink is free. */
1596 trans = trans_alloc_vgcs(gsmnet, NULL, trans_type, 0, callref, gcr, false);
1597 if (!trans) {
1598 LOG_GCC(trans, LOGL_ERROR, "No memory for trans.\n");
1599 return "Failed to create call.";
1600 }
1601
1602 LOG_GCC(trans, LOGL_INFO, "VTY initiates call.\n");
1603 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_NET_SETUP, NULL);
1604
1605 return NULL;
1606}
1607
1608/* Destroy a call from VTY command. */
1609const char *vgcs_vty_terminate(struct gsm_network *gsmnet, struct gcr *gcr)
1610{
1611 enum trans_type trans_type;
1612 uint32_t callref;
1613 struct gsm_trans *trans;
1614
1615 /* Get callref from stored suffix. Caller cannot choose a prefix. */
1616 trans_type = gcr->trans_type;
1617 callref = atoi(gcr->group_id);
1618
1619 /* Check if callref exists. */
1620 trans = trans_find_by_callref(gsmnet, trans_type, callref);
1621 if (!trans)
1622 return "Call does not exist.";
1623
1624 LOG_GCC(trans, LOGL_INFO, "VTY terminates call.\n");
1625 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_NET_TERM, NULL);
1626
1627 return NULL;
1628}
1629
1630/*
1631 * BSS state machine - handles all BSS "call control" instances
1632 */
1633
1634static const struct value_string vgcs_bss_fsm_event_names[] = {
1635 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP),
1636 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP_ACK),
1637 OSMO_VALUE_STRING(VGCS_BSS_EV_SETUP_REFUSE),
1638 OSMO_VALUE_STRING(VGCS_BSS_EV_ACTIVE_OR_FAIL),
1639 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_REQUEST),
1640 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_REQUEST_CNF),
1641 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_APP_DATA),
1642 OSMO_VALUE_STRING(VGCS_BSS_EV_BSS_DTAP),
1643 OSMO_VALUE_STRING(VGCS_BSS_EV_UL_RELEASE),
1644 OSMO_VALUE_STRING(VGCS_BSS_EV_CLEAR),
1645 OSMO_VALUE_STRING(VGCS_BSS_EV_CLOSE),
1646 OSMO_VALUE_STRING(VGCS_BSS_EV_RELEASED),
1647 { }
1648};
1649
1650/* Blocks or unblocks uplinks of a BSS. */
1651static int update_uplink_state(struct vgcs_bss *bss, bool uplink_busy)
1652{
1653 struct ran_msg ran_msg;
1654 int rc;
1655
1656 if (uplink_busy) {
1657 /* Send UPLINK SEIZED COMMAND to BSS. */
1658 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK SEIZED COMMAND towards BSS.\n");
1659 ran_msg = (struct ran_msg){
1660 .msg_type = RAN_MSG_UPLINK_SEIZED_CMD,
1661 .uplink_seized_cmd = {
1662 .cause = GSM0808_CAUSE_CALL_CONTROL,
1663 },
1664 };
1665 } else {
1666 /* Send UPLINK RELEASE COMMAND to BSS. */
1667 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK RELEASE COMMAND towards BSS.\n");
1668 ran_msg = (struct ran_msg){
1669 .msg_type = RAN_MSG_UPLINK_RELEASE_CMD,
1670 .uplink_release_cmd = {
1671 .cause = GSM0808_CAUSE_CALL_CONTROL,
1672 },
1673 };
1674 }
1675
1676 rc = ran_encode_and_send(bss->fi, &ran_msg, bss->conn, false);
1677
1678 return rc;
1679}
1680
1681/* Clear the connection towards BSS.
1682 * The instance is removed soon, so it is detached from transaction and cells. */
1683static void bss_clear(struct vgcs_bss *bss, uint8_t cause, bool notify_trans)
1684{
1685 struct ran_msg ran_msg;
1686 struct gsm_trans *trans = bss->trans;
1687 struct vgcs_bss_cell *cell, *cell2;
1688
1689 /* Must detach us from transaction. */
1690 if (bss->trans) {
1691 /* Remove pointer to talking BSS and cell. */
1692 if (bss == bss->trans->gcc.uplink_bss) {
1693 bss->trans->gcc.uplink_bss = NULL;
1694 bss->trans->gcc.uplink_cell = NULL;
1695 }
1696 llist_del(&bss->list);
1697 bss->trans = NULL;
1698 }
1699
1700 /* Change state. */
1701 osmo_fsm_inst_state_chg(bss->fi, VGCS_BSS_ST_RELEASE, 0, 0);
1702
1703 /* Send Clear Command to BSS. */
1704 ran_msg = (struct ran_msg){
1705 .msg_type = RAN_MSG_CLEAR_COMMAND,
1706 .clear_command = {
1707 .gsm0808_cause = cause,
1708 },
1709 };
1710 if (bss->conn) {
1711 LOG_BSS(bss, LOGL_DEBUG, "Sending CLEAR COMMAND for call controling channel.\n");
1712 ran_encode_and_send(bss->fi, &ran_msg, bss->conn, false);
1713 }
1714
1715 /* Trigger clear of all cells. Be safe, because the process will remove cells from list. */
1716 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss)
1717 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_CLEAR, NULL);
1718
1719 /* Detach us from all BSS, if still linked */
1720 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss) {
1721 llist_del(&cell->list_bss);
1722 cell->bss = NULL;
1723 }
1724
1725 /* If all BS are gone, notify calling subscriber process. */
1726 if (notify_trans && trans && llist_empty(&trans->gcc.bss_list)) {
1727 LOG_BSS(bss, LOGL_DEBUG, "Notify calling user process, that all BSSs are cleared.\n");
1728 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_RELEASED, NULL);
1729 }
1730}
1731
1732/* When finally the BSS connection is released. (CLEAR COMPLETE response)
1733 * The instance is removed, so it is detached from transaction and cells, if not already. */
1734static void bss_destroy(struct vgcs_bss *bss)
1735{
1736 struct vgcs_bss_cell *cell, *cell2;
1737
1738 LOG_BSS(bss, LOGL_DEBUG, "Removing BSS call controling instance.\n");
1739
1740 /* Must detach us from transaction, if not already. */
1741 if (bss->trans) {
1742 /* Remove pointer to talking BSS and cell. */
1743 if (bss == bss->trans->gcc.uplink_bss) {
1744 bss->trans->gcc.uplink_bss = NULL;
1745 bss->trans->gcc.uplink_cell = NULL;
1746 }
1747 llist_del(&bss->list);
1748 bss->trans = NULL;
1749 }
1750
1751 /* Detach us from RAN connection. */
1752 if (bss->conn) {
1753 if (bss->conn->vgcs.bss == bss)
1754 bss->conn->vgcs.bss = NULL;
1755 if (bss->conn->vgcs.cell == bss)
1756 bss->conn->vgcs.cell = NULL;
1757 ran_conn_close(bss->conn);
1758 bss->conn = NULL;
1759 }
1760
1761 /* Detach us from all BSS, if still linked */
1762 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss) {
1763 llist_del(&cell->list_bss);
1764 cell->bss = NULL;
1765 }
1766
1767 /* Free FSM. (should be allocated) */
1768 osmo_fsm_inst_state_chg(bss->fi, VGCS_BSS_ST_NULL, 0, 0);
1769 osmo_fsm_inst_term(bss->fi, OSMO_FSM_TERM_REGULAR, NULL);
1770}
1771
1772/* Get identity of talker.
1773 * This is required to detect if the talker is the calling subscriber. */
1774static int talker_identity(struct vgcs_bss *bss, uint8_t *l3, int l3_len)
1775{
1776 struct osmo_mobile_identity mi;
1777 int rc;
1778
Andreas Eversberge24636c2023-04-23 12:20:55 +02001779 rc = osmo_mobile_identity_decode_from_l3_buf(&mi, l3, l3_len, false);
1780 if (rc < 0) {
1781 LOG_BSS(bss, LOGL_DEBUG, "Talker's Identity cannot be decoded.\n");
1782 return rc;
1783 }
1784
1785 switch (mi.type) {
1786 case GSM_MI_TYPE_IMSI:
1787 if (!bss->trans->vsub)
1788 break;
1789 LOG_BSS(bss, LOGL_DEBUG, "Talker's sends IMSI %s, originator has IMSI %s.\n",
1790 mi.imsi, bss->trans->vsub->imsi);
1791 if (!strcmp(mi.imsi, bss->trans->vsub->imsi))
1792 return 1;
1793 break;
1794 case GSM_MI_TYPE_TMSI:
1795 if (!bss->trans->vsub)
1796 break;
1797 LOG_BSS(bss, LOGL_DEBUG, "Talker's sends TMSI 0x%08x, originator has TMSI 0x%08x.\n",
1798 mi.tmsi, bss->trans->vsub->tmsi);
1799 if (mi.tmsi == bss->trans->vsub->tmsi)
1800 return 1;
1801 break;
1802 default:
1803 LOG_BSS(bss, LOGL_DEBUG, "Talker's Identity is not IMSI nor TMSI.\n");
1804 return -EINVAL;
1805 }
1806
1807 return 0;
1808}
1809
1810static void vgcs_bss_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1811{
1812 struct vgcs_bss *bss = fi->priv;
1813 struct ran_msg ran_msg;
1814
1815 switch (event) {
1816 case VGCS_BSS_EV_SETUP:
1817 /* Change state. */
1818 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_SETUP, 0, 0);
1819 /* Send VGCS/VBS SETUP to BSS. */
1820 LOG_BSS(bss, LOGL_DEBUG, "Sending VGCS/VBS SETUP towards BSS.\n");
1821 ran_msg = (struct ran_msg){
1822 .msg_type = RAN_MSG_VGCS_VBS_SETUP,
1823 .vgcs_vbs_setup = {
1824 .callref = {
1825 .sf = (bss->trans->type == TRANS_GCC),
1826 },
1827 .vgcs_feature_flags_present = true,
1828 },
1829 };
1830 osmo_store32be_ext(bss->callref >> 3, &ran_msg.vgcs_vbs_setup.callref.call_ref_hi, 3);
1831 ran_msg.vgcs_vbs_setup.callref.call_ref_lo = bss->callref & 0x7;
1832 /* First message, so we must set "initial" to "true". */
1833 ran_encode_and_send(fi, &ran_msg, bss->conn, true);
1834 break;
1835 case VGCS_BSS_EV_CLEAR:
1836 /* The calling user process requested clearing of VGCS/VBS call. */
1837 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1838 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1839 break;
1840 default:
1841 OSMO_ASSERT(false);
1842 }
1843}
1844
1845static void vgcs_bss_fsm_setup(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1846{
1847 struct vgcs_bss *bss = fi->priv;
1848 struct vgcs_bss_cell *cell, *cell2;
1849
1850 switch (event) {
1851 case VGCS_BSS_EV_SETUP_ACK:
1852 /* Receive VGCS/VBS SETUP ACK from BSS. */
1853 LOG_BSS(bss, LOGL_DEBUG, "Received VGCS/VBS SETUP ACK from BSS.\n");
1854 /* Send current uplink state to this BSS. */
1855 if (bss->trans)
1856 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
1857 /* Change state. */
1858 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_ASSIGNMENT, 0, 0);
1859 /* Trigger VGCS/VBS ASSIGNMENT */
1860 llist_for_each_entry_safe(cell, cell2, &bss->cell_list, list_bss)
1861 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN, NULL);
1862 /* If all failed, clear call. */
1863 if (llist_empty(&bss->cell_list)) {
1864 LOG_BSS(bss, LOGL_NOTICE, "All VGCS/VBS assignments failed.\n");
1865 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1866 break;
1867 }
1868 break;
1869 case VGCS_BSS_EV_SETUP_REFUSE:
1870 /* Received VGCS/VBS SETUP REFUSE from BSS. */
1871 LOG_BSS(bss, LOGL_NOTICE, "Received VGCS/VBS SETUP REFUSE from BSS.\n");
1872 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1873 break;
1874 case VGCS_BSS_EV_CLEAR:
1875 /* The calling user process requested clearing of VGCS/VBS call. */
1876 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1877 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1878 break;
1879 case VGCS_BSS_EV_CLOSE:
1880 /* The SCCP connection from the MSC has been closed. */
1881 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
1882 if (bss->conn) {
1883 bss->conn->vgcs.bss = NULL;
1884 bss->conn = NULL;
1885 }
1886 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1887 break;
1888 default:
1889 OSMO_ASSERT(false);
1890 }
1891}
1892
1893static void vgcs_bss_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1894{
1895 struct vgcs_bss *bss = fi->priv;
1896 struct vgcs_bss_cell *c;
1897 bool assigned;
1898
1899 switch (event) {
1900 case VGCS_BSS_EV_ACTIVE_OR_FAIL:
1901 /* If all gone, clear call. */
1902 if (llist_empty(&bss->cell_list)) {
1903 LOG_BSS(bss, LOGL_NOTICE, "All VGCS/VBS assignments failed.\n");
1904 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1905 break;
1906 }
1907 /* Is there a response for all cells?
1908 * This means that all the channels have a positive response
1909 * There is no channel with negative response, because a
1910 * negative response will remove the channel. */
1911 assigned = true;
1912 llist_for_each_entry(c, &bss->cell_list, list_bss) {
1913 if (!c->assigned)
1914 assigned = false;
1915 }
1916 if (!assigned)
1917 break;
1918 LOG_BSS(bss, LOGL_DEBUG, "All VGCS/VBS assignments have responded.\n");
1919 /* Change state. */
1920 osmo_fsm_inst_state_chg(fi, VGCS_BSS_ST_ACTIVE, 0, 0);
1921 /* Notify calling subscriber process. */
1922 LOG_BSS(bss, LOGL_DEBUG, "Notify calling user process, that all BSSs are connected.\n");
1923 if (bss->trans)
1924 osmo_fsm_inst_dispatch(bss->trans->gcc.fi, VGCS_GCC_EV_BSS_ESTABLISHED, NULL);
1925 break;
1926 case VGCS_BSS_EV_CLEAR:
1927 /* The calling user process requested clearing of VGCS/VBS call. */
1928 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
1929 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
1930 break;
1931 case VGCS_BSS_EV_CLOSE:
1932 /* The SCCP connection from the MSC has been closed. */
1933 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
1934 if (bss->conn) {
1935 bss->conn->vgcs.bss = NULL;
1936 bss->conn = NULL;
1937 }
1938 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
1939 break;
1940 default:
1941 OSMO_ASSERT(false);
1942 }
1943}
1944
1945static void vgcs_bss_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1946{
1947 struct vgcs_bss *bss = fi->priv, *other;
1948 struct ran_msg *rx_ran_msg = data;
1949 struct ran_msg tx_ran_msg;
1950 int rc;
1951
1952 switch (event) {
1953 case VGCS_BSS_EV_UL_REQUEST:
1954 LOG_BSS(bss, LOGL_DEBUG, "Listener changed to talker.\n");
1955 if (!bss->trans)
1956 break;
1957 /* Someone is talking. Check if there is no other uplink already busy.
1958 * This should not happen, since all other cells are blocked (SEIZED) as soon as the uplink was
1959 * requested. This may happen due to a race condition, where the uplink was requested before the
1960 * UPLINK SEIZED COMMAND has been received by BSS. */
1961 if (bss->trans->gcc.uplink_busy) {
1962 /* Send UPLINK REJECT COMMAND to BSS. */
1963 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK REJECT COMMAND towards BSS.\n");
1964 tx_ran_msg = (struct ran_msg){
1965 .msg_type = RAN_MSG_UPLINK_REJECT_CMD,
1966 .uplink_reject_cmd = {
1967 .cause = GSM0808_CAUSE_CALL_CONTROL,
1968 },
1969 };
1970 ran_encode_and_send(fi, &tx_ran_msg, bss->conn, false);
1971 break;
1972 }
1973 /* Send UPLINK REQUEST ACKNOWLEDGE to BSS. */
1974 LOG_BSS(bss, LOGL_DEBUG, "Sending (VGCS) UPLINK REQUEST ACKNOWLEDGE towards BSS.\n");
1975 tx_ran_msg = (struct ran_msg){
1976 .msg_type = RAN_MSG_UPLINK_REQUEST_ACK,
1977 };
1978 ran_encode_and_send(fi, &tx_ran_msg, bss->conn, false);
1979 /* Set the busy flag and block all other cells. */
1980 bss->trans->gcc.uplink_bss = bss;
1981 bss->trans->gcc.uplink_busy = true;
1982 bss->trans->gcc.uplink_originator = false;
1983 llist_for_each_entry(other, &bss->trans->gcc.bss_list, list) {
1984 if (other == bss)
1985 continue;
1986 /* Update uplink state. */
1987 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
1988 }
1989 /* Stop inactivity timer. */
1990 stop_inactivity_timer(bss->trans);
1991 break;
1992 case VGCS_BSS_EV_UL_REQUEST_CNF:
1993 LOG_BSS(bss, LOGL_DEBUG, "Talker established uplink.\n");
1994 if (!bss->trans)
1995 break;
1996 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
1997 LOG_BSS(bss, LOGL_ERROR, "Got UL REQUEST CNF, but we did not granted uplink.\n");
1998 break;
1999 }
2000 /* Determine if talker is the originator of the call. */
2001 rc = talker_identity(bss, rx_ran_msg->uplink_request_cnf.l3.l3,
2002 rx_ran_msg->uplink_request_cnf.l3.l3_len);
2003 if (rc > 0) {
2004 bss->trans->gcc.uplink_originator = true;
2005 LOG_BSS(bss, LOGL_DEBUG, "Talker is the originator of the call.\n");
2006 }
2007 /* Set parameter. */
2008 set_parameter(bss->trans);
2009 /* Set cell of current talker. */
2010 set_uplink_cell(bss, &rx_ran_msg->uplink_request_cnf.cell_identifier, 0);
2011 /* Set MGW conference. */
2012 set_mgw_conference(bss->trans);
2013 break;
2014 case VGCS_BSS_EV_UL_APP_DATA:
2015 LOG_BSS(bss, LOGL_DEBUG, "Talker sends application data on uplink.\n");
2016 if (!bss->trans)
2017 break;
2018 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
2019 LOG_BSS(bss, LOGL_ERROR, "Got UP APP DATA, but we did not granted uplink.\n");
2020 break;
2021 }
2022 // FIXME: Use L3 info and feed to app.
2023 break;
2024 case VGCS_BSS_EV_BSS_DTAP:
2025 LOG_BSS(bss, LOGL_DEBUG, "Talker sends DTAP message.\n");
2026 if (!bss->trans)
2027 break;
2028 if (!bss->trans->gcc.uplink_busy || bss->trans->gcc.uplink_bss != bss) {
2029 LOG_BSS(bss, LOGL_ERROR, "Got DTAP from BSS, but we did not granted uplink.\n");
2030 break;
2031 }
2032 gsm44068_rcv_bcc_gcc(NULL, bss->trans, rx_ran_msg->dtap);
2033 break;
2034 case VGCS_BSS_EV_UL_RELEASE:
2035 LOG_BSS(bss, LOGL_DEBUG, "Talker released uplink.\n");
2036 if (!bss->trans)
2037 break;
2038 if (bss->trans->type == TRANS_BCC) {
2039 LOG_BSS(bss, LOGL_DEBUG, "This is a broadcast call, terminating call.\n");
2040 gcc_terminate_and_destroy(bss->trans, OSMO_GSM44068_CAUSE_NORMAL_CALL_CLEARING);
2041 break;
2042 }
2043 if (!bss->trans->gcc.uplink_busy) {
2044 LOG_BSS(bss, LOGL_NOTICE, "Got uplink release, but no uplink busy.\n");
2045 break;
2046 }
2047 /* Talker release the uplink. Ignore, if not from the current talking cell. */
2048 if (bss->trans->gcc.uplink_bss != bss) {
2049 LOG_BSS(bss, LOGL_NOTICE, "Got uplink release, but uplink busy in other cell.\n");
2050 break;
2051 }
2052 /* Clear the busy flag and unblock all other cells. */
2053 bss->trans->gcc.uplink_bss = NULL;
2054 bss->trans->gcc.uplink_cell = NULL;
2055 bss->trans->gcc.uplink_busy = false;
2056 llist_for_each_entry(other, &bss->trans->gcc.bss_list, list) {
2057 if (other == bss)
2058 continue;
2059 /* Update uplink state. */
2060 if (bss->trans)
2061 update_uplink_state(bss, bss->trans->gcc.uplink_busy);
2062 }
2063 /* Set MGW conference. */
2064 set_mgw_conference(bss->trans);
2065 /* Start inactivity timer. */
2066 start_inactivity_timer(bss->trans);
2067 break;
2068 case VGCS_BSS_EV_CLEAR:
2069 /* The calling user process requested clearing of VGCS/VBS call. */
2070 LOG_BSS(bss, LOGL_DEBUG, "Received clearing from calling user process.\n");
2071 bss_clear(bss, GSM0808_CAUSE_CALL_CONTROL, false);
2072 break;
2073 case VGCS_BSS_EV_CLOSE:
2074 /* The SCCP connection from the MSC has been closed. */
2075 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2076 if (bss->conn) {
2077 bss->conn->vgcs.bss = NULL;
2078 bss->conn = NULL;
2079 }
2080 bss_clear(bss, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC, true);
2081 break;
2082 default:
2083 OSMO_ASSERT(false);
2084 }
2085}
2086
2087static void vgcs_bss_fsm_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2088{
2089 struct vgcs_bss *bss = fi->priv;
2090
2091 switch (event) {
2092 case VGCS_BSS_EV_CLOSE:
2093 /* The SCCP connection from the MSC has been closed while waitring fro CLEAR COMPLETE. */
2094 LOG_BSS(bss, LOGL_NOTICE, "Received SCCP closing collision.\n");
2095 bss_destroy(bss);
2096 break;
2097 case VGCS_BSS_EV_RELEASED:
2098 LOG_BSS(bss, LOGL_DEBUG, "Received CLEAR COMPLETE from BSS, we are done!\n");
2099 bss_destroy(bss);
2100 break;
2101 default:
2102 OSMO_ASSERT(false);
2103 }
2104}
2105
2106static const struct osmo_fsm_state vgcs_bss_fsm_states[] = {
2107 [VGCS_BSS_ST_NULL] = {
2108 .name = "NULL",
2109 .in_event_mask = S(VGCS_BSS_EV_SETUP) |
2110 S(VGCS_BSS_EV_CLEAR),
2111 .out_state_mask = S(VGCS_BSS_ST_SETUP),
2112 .action = vgcs_bss_fsm_null,
2113 },
2114 [VGCS_BSS_ST_SETUP] = {
2115 .name = "SETUP sent",
2116 .in_event_mask = S(VGCS_BSS_EV_SETUP_ACK) |
2117 S(VGCS_BSS_EV_SETUP_REFUSE) |
2118 S(VGCS_BSS_EV_CLEAR) |
2119 S(VGCS_BSS_EV_CLOSE),
2120 .out_state_mask = S(VGCS_BSS_ST_ASSIGNMENT) |
2121 S(VGCS_BSS_ST_RELEASE),
2122 .action = vgcs_bss_fsm_setup,
2123 },
2124 [VGCS_BSS_ST_ASSIGNMENT] = {
2125 .name = "ASSIGNMENT Sent",
2126 .in_event_mask = S(VGCS_BSS_EV_ACTIVE_OR_FAIL) |
2127 S(VGCS_BSS_EV_CLEAR) |
2128 S(VGCS_BSS_EV_CLOSE),
2129 .out_state_mask = S(VGCS_BSS_ST_ACTIVE) |
2130 S(VGCS_BSS_ST_RELEASE),
2131 .action = vgcs_bss_fsm_assignment,
2132 },
2133 [VGCS_BSS_ST_ACTIVE] = {
2134 .name = "VGCS/VBS Active",
2135 .in_event_mask = S(VGCS_BSS_EV_UL_REQUEST) |
2136 S(VGCS_BSS_EV_UL_REQUEST_CNF) |
2137 S(VGCS_BSS_EV_UL_APP_DATA) |
2138 S(VGCS_BSS_EV_BSS_DTAP) |
2139 S(VGCS_BSS_EV_UL_RELEASE) |
2140 S(VGCS_BSS_EV_CLEAR) |
2141 S(VGCS_BSS_EV_CLOSE),
2142 .out_state_mask = S(VGCS_BSS_ST_RELEASE),
2143 .action = vgcs_bss_fsm_active,
2144 },
2145 [VGCS_BSS_ST_RELEASE] = {
2146 .name = "Releasing VGCS/VBS control",
2147 .in_event_mask = S(VGCS_BSS_EV_CLEAR) |
2148 S(VGCS_BSS_EV_RELEASED),
2149 .out_state_mask = S(VGCS_BSS_ST_NULL),
2150 .action = vgcs_bss_fsm_release,
2151 },
2152};
2153
2154static struct osmo_fsm vgcs_bss_fsm = {
2155 .name = "vgcs_bss",
2156 .states = vgcs_bss_fsm_states,
2157 .num_states = ARRAY_SIZE(vgcs_bss_fsm_states),
2158 .log_subsys = DASCI,
2159 .event_names = vgcs_bss_fsm_event_names,
2160};
2161
2162/* The BSS accepts VGCS/VBS and sends us supported features. */
2163void vgcs_vbs_setup_ack(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2164{
2165 if (!bss->trans)
2166 return;
2167 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_SETUP_ACK, (void *)ran_msg);
2168}
2169
2170/* The BSS refuses VGCS/VBS. */
2171void vgcs_vbs_setup_refuse(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2172{
2173 if (!bss->trans)
2174 return;
2175 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_SETUP_REFUSE, (void *)ran_msg);
2176}
2177
2178/* The BSS needs more time for VGCS/VBS channel assignment. */
2179void vgcs_vbs_queuing_ind(struct vgcs_bss_cell *cell)
2180{
2181 if (!cell->bss)
2182 return;
2183}
2184
2185/* A mobile station requests the uplink on a VGCS channel. */
2186void vgcs_uplink_request(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2187{
2188 if (!bss->trans)
2189 return;
2190 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_REQUEST, (void *)ran_msg);
2191}
2192
2193/* The uplink on a VGCS channel has been established. */
2194void vgcs_uplink_request_cnf(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2195{
2196 if (!bss->trans)
2197 return;
2198 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_REQUEST_CNF, (void *)ran_msg);
2199}
2200
2201/* Application data received on the uplink of a VGCS channel. */
2202void vgcs_app_data(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2203{
2204 if (!bss->trans)
2205 return;
2206 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_APP_DATA, (void *)ran_msg);
2207}
2208
2209/* Application data received on the uplink of a VGCS channel. */
2210void vgcs_bss_dtap(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2211{
2212 if (!bss->trans)
2213 return;
2214 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_BSS_DTAP, (void *)ran_msg);
2215}
2216
2217/* A mobile station releases the uplink on a VGCS channel. */
2218void vgcs_uplink_release_ind(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2219{
2220 if (!bss->trans)
2221 return;
2222 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_UL_RELEASE, (void *)ran_msg);
2223}
2224
2225/* The BSS gives cell status about VGCS/VBS channel. */
2226void vgcs_vbs_assign_status(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2227{
2228 if (!cell->bss)
2229 return;
2230}
2231
2232void vgcs_vbs_caller_assign_cpl(struct gsm_trans *trans)
2233{
2234 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_ASSIGN_CPL, NULL);
2235}
2236
2237void vgcs_vbs_caller_assign_fail(struct gsm_trans *trans)
2238{
2239 osmo_fsm_inst_dispatch(trans->gcc.fi, VGCS_GCC_EV_BSS_ASSIGN_FAIL, NULL);
2240}
2241
2242/* BSS indicated that the channel has been released. */
2243void vgcs_vbs_clear_req(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2244{
2245 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_CLOSE, (void *)ran_msg);
2246}
2247
2248/* BSS indicated that the channel has been released. */
2249void vgcs_vbs_clear_cpl(struct vgcs_bss *bss, const struct ran_msg *ran_msg)
2250{
2251 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_RELEASED, (void *)ran_msg);
2252}
2253
2254/*
2255 * Cell resource state machine - handles all "resource control" instances
2256 */
2257
2258static const struct value_string vgcs_cell_fsm_event_names[] = {
2259 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_GONE),
2260 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE),
2261 OSMO_VALUE_STRING(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED),
2262 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN),
2263 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN_RES),
2264 OSMO_VALUE_STRING(VGCS_CELL_EV_ASSIGN_FAIL),
2265 OSMO_VALUE_STRING(VGCS_CELL_EV_CLEAR),
2266 OSMO_VALUE_STRING(VGCS_CELL_EV_CLOSE),
2267 OSMO_VALUE_STRING(VGCS_CELL_EV_RELEASED),
2268 { }
2269};
2270
2271static void cell_destroy(struct vgcs_bss_cell *cell);
2272
2273/* Clear the connection towards BSS.
2274 * Relations to the BSS and transaction is removed. */
2275static void cell_clear(struct vgcs_bss_cell *cell, uint8_t cause)
2276{
2277 struct ran_msg ran_msg;
2278
2279 /* Must detach us from BSS. */
2280 if (cell->bss) {
2281 /* Remove pointer to talking channel. */
2282 if (cell->bss->trans && cell->bss->trans->gcc.uplink_cell == cell)
2283 cell->bss->trans->gcc.uplink_cell = NULL;
2284 llist_del(&cell->list_bss);
2285 cell->bss = NULL;
2286 }
2287
2288 /* Change state. */
2289 if (cell->fi->state != VGCS_CELL_ST_RELEASE)
2290 osmo_fsm_inst_state_chg(cell->fi, VGCS_CELL_ST_RELEASE, 0, 0);
2291
2292 /* If there is no event to wait for, we can just destroy. */
2293 if (!cell->conn && !cell->rtps) {
2294 cell_destroy(cell);
2295 return;
2296 }
2297
2298 /* Send Clear Command to BSS. */
2299 if (cell->conn) {
2300 ran_msg = (struct ran_msg){
2301 .msg_type = RAN_MSG_CLEAR_COMMAND,
2302 .clear_command = {
2303 .gsm0808_cause = cause,
2304 },
2305 };
2306 LOG_CELL(cell, LOGL_DEBUG, "Sending CLEAR COMMAND for call controling channel.\n");
2307 ran_encode_and_send(cell->fi, &ran_msg, cell->conn, false);
2308 }
2309
2310 /* Clear RTP stream. This may trigger VGCS_CELL_EV_RTP_STREAM_GONE within this release function. */
2311 if (cell->rtps)
2312 rtp_stream_release(cell->rtps);
2313}
2314
2315/* When finally the BSS connection is released. (CLEAR COMPLETE response)
2316 * Relations to the BSS and transaction is removed, if not already. */
2317static void cell_destroy(struct vgcs_bss_cell *cell)
2318{
2319 struct vgcs_mgw_ep *mgw;
2320
2321 /* close RAN conn */
2322 if (cell->conn) {
2323 cell->conn->vgcs.cell = NULL;
2324 ran_conn_close(cell->conn);
2325 cell->conn = NULL;
2326 }
2327
2328 /* Detach from BSS now. Check, to prevent race condition. */
2329 if (cell->bss) {
2330 /* Remove pointer to talking channel. */
2331 if (cell->bss->trans && cell->bss->trans->gcc.uplink_cell == cell)
2332 cell->bss->trans->gcc.uplink_cell = NULL;
2333 llist_del(&cell->list_bss);
2334 cell->bss = NULL;
2335 }
2336
2337 /* Detach from MGW now. Check, to prevent race condition. */
2338 if (cell->mgw) {
2339 mgw = cell->mgw;
2340 llist_del(&cell->list_mgw);
2341 cell->mgw = NULL;
2342 /* Destroy MGW endpoint, if list is empty. */
2343 if (llist_empty(&mgw->cell_list))
2344 osmo_fsm_inst_dispatch(mgw->fi, VGCS_MGW_EP_EV_CLEAR, NULL);
2345 }
2346
2347 LOG_CELL(cell, LOGL_DEBUG, "Detroy connection to cell.\n");
2348
2349 /* Free FSM. (should be allocated) */
2350 osmo_fsm_inst_state_chg(cell->fi, VGCS_CELL_ST_NULL, 0, 0);
2351 osmo_fsm_inst_term(cell->fi, OSMO_FSM_TERM_REGULAR, NULL);
2352}
2353
2354static void vgcs_cell_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2355{
2356 struct vgcs_bss_cell *cell = fi->priv;
2357 const struct codec_mapping *cm;
2358 int rc;
2359
2360 switch (event) {
2361 case VGCS_CELL_EV_ASSIGN:
2362 LOG_CELL(cell, LOGL_DEBUG, "Received assignment from BSS controling process.\n");
2363 /* Allocate rtps stream. */
2364 cell->rtps = rtp_stream_alloc(cell->fi, VGCS_CELL_EV_RTP_STREAM_GONE,
2365 VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE,
2366 VGCS_CELL_EV_RTP_STREAM_ESTABLISHED, RTP_TO_RAN, cell->call_id,
2367 NULL);
2368 if (!cell->rtps) {
2369 LOG_CELL(cell, LOGL_DEBUG, "Failed to allocate RTP stream, cannot continue.\n");
2370 cell_destroy(cell);
2371 break;
2372 }
2373 /* Hard coded codec: GSM V1 */
2374 cm = codec_mapping_by_gsm0808_speech_codec_type(GSM0808_SCT_FR1);
Andreas Eversberg33a433a2023-07-19 10:01:20 +02002375 if (!cm) {
2376 LOG_CELL(cell, LOGL_DEBUG, "Selected codec not supported, cannot continue.\n");
2377 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2378 break;
2379 }
Andreas Eversberge24636c2023-04-23 12:20:55 +02002380 rtp_stream_set_one_codec(cell->rtps, &cm->sdp);
2381 /* Set initial mode. */
2382 rtp_stream_set_mode(cell->rtps, MGCP_CONN_RECV_ONLY);
2383 /* Commit RTP stream. */
Andreas Eversbergd6377602023-07-19 09:46:18 +02002384 if (!cell->bss || !cell->bss->trans) {
2385 LOG_CELL(cell, LOGL_DEBUG, "No BSS/transaction, cannot continue.\n");
Andreas Eversberge24636c2023-04-23 12:20:55 +02002386 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2387 break;
2388 }
Andreas Eversbergd6377602023-07-19 09:46:18 +02002389 if (!cell->mgw || !cell->mgw->mgw_ep) {
Andreas Eversberge24636c2023-04-23 12:20:55 +02002390 LOG_CELL(cell, LOGL_DEBUG, "No MGW endpoint, cannot continue.\n");
2391 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2392 break;
2393 }
2394 rc = rtp_stream_ensure_ci(cell->rtps, cell->mgw->mgw_ep);
2395 if (rc < 0) {
2396 LOG_CELL(cell, LOGL_DEBUG, "Failed to trigger RTP stream CI.\n");
2397 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2398 break;
2399 }
2400 /* Change state. */
2401 osmo_fsm_inst_state_chg(fi, VGCS_CELL_ST_ASSIGNMENT, 0, 0);
2402 break;
2403 case VGCS_CELL_EV_CLEAR:
2404 /* The calling user process requested clearing of VGCS/VBS call. */
2405 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2406 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2407 break;
2408 default:
2409 OSMO_ASSERT(false);
2410 }
2411}
2412
2413static void vgcs_cell_fsm_assignment(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2414{
2415 struct vgcs_bss_cell *cell = fi->priv;
2416 struct ran_msg *rx_ran_msg = data;
2417 struct ran_msg tx_ran_msg;
2418 struct osmo_sockaddr_str ss;
2419 const struct codec_mapping *cm;
2420 struct vgcs_bss *bss;
2421 int rc;
2422
2423 switch (event) {
2424 case VGCS_CELL_EV_RTP_STREAM_GONE:
2425 /* The RTP stream failed. */
2426 LOG_CELL(cell, LOGL_ERROR, "RTP stream of MGW failed.\n");
2427 cell->rtps = NULL;
2428 goto channel_fail;
2429 break;
2430 case VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE:
2431 /* The RTP stream sends its peer. */
2432 if (!osmo_sockaddr_str_is_nonzero(&cell->rtps->local)) {
2433 LOG_CELL(cell, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
2434 OSMO_SOCKADDR_STR_FMT_ARGS(&cell->rtps->local));
2435 goto channel_fail;
2436 }
2437 LOG_CELL(cell, LOGL_DEBUG,
2438 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
2439 rtp_direction_name(cell->rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&cell->rtps->local),
2440 cell->rtps->use_osmux ? "yes" : "no", cell->rtps->local_osmux_cid);
2441 /* Send VGCS/VBS ASSIGNMENT REQUEST to BSS */
2442 LOG_CELL(cell, LOGL_DEBUG, "Sending VGCS/VBS ASSIGNMENT REQUEST towards BSS.\n");
2443 tx_ran_msg = (struct ran_msg) {
2444 .msg_type = RAN_MSG_VGCS_VBS_ASSIGN_REQ,
2445 .vgcs_vbs_assign_req = {
2446 /* For now we support GSM/FR V1 only. This shall be supported by all MS. */
2447 .channel_type = {
2448 .ch_indctr = GSM0808_CHAN_SPEECH,
2449 .ch_rate_type = GSM0808_SPEECH_FULL_BM,
2450 .perm_spch_len = 1,
2451 .perm_spch[0] = GSM0808_PERM_FR1,
2452 },
2453 /* For now we want a channel without any delay. */
2454 .ass_req = GSM0808_ASRQ_IMMEDIATE,
2455 .callref = {
2456 .sf = (cell->trans_type == TRANS_GCC),
2457 },
2458 /* We need to identify the cell only. */
2459 .cell_identifier = {
2460 .id_discr = CELL_IDENT_CI,
2461 .id.ci = cell->cell_id,
2462 },
2463 .aoip_transport_layer_present = true,
2464 .call_id_present = true,
2465 .call_id = cell->call_id,
2466 .codec_list_present = true,
2467 .codec_list_msc_preferred = {
2468 .len = 1,
2469 .codec[0] = {
2470 .fi = 1,
2471 .type = GSM0808_SCT_FR1,
2472 .cfg = 0,
2473 },
2474 },
2475 },
2476 };
2477 osmo_store32be_ext(cell->callref >> 3, &tx_ran_msg.vgcs_vbs_assign_req.callref.call_ref_hi, 3);
2478 tx_ran_msg.vgcs_vbs_assign_req.callref.call_ref_lo = cell->callref & 0x7;
2479 osmo_sockaddr_str_to_sockaddr(&cell->rtps->local, &tx_ran_msg.vgcs_vbs_assign_req.aoip_transport_layer);
2480 /* First message, so we must set "initial" to "true". */
2481 ran_encode_and_send(fi, &tx_ran_msg, cell->conn, true);
2482 break;
2483 case VGCS_CELL_EV_RTP_STREAM_ESTABLISHED:
2484 /* The RTP stream established. */
2485 LOG_CELL(cell, LOGL_DEBUG, "RTP stream is established.\n");
2486 break;
2487 case VGCS_CELL_EV_ASSIGN_RES:
2488 /* Receive VGCS/VBS ASSIGNMENT RESULT from BSS. */
2489 LOG_CELL(cell, LOGL_DEBUG, "Received VGCS/VBS ASSIGNMENT RESULT from BSS.\n");
2490 cell->assigned = true;
2491 if (!rx_ran_msg->vgcs_vbs_assign_res.aoip_transport_layer_present
2492 && !rx_ran_msg->vgcs_vbs_assign_res.codec_present
2493 && !rx_ran_msg->vgcs_vbs_assign_res.call_id_present) {
2494 LOG_CELL(cell, LOGL_ERROR, "Mandatory IEs missing.\n");
2495 goto channel_fail;
2496 }
2497 /* Send remote peer to RTP stream. */
2498 if (osmo_sockaddr_str_from_sockaddr(&ss, &rx_ran_msg->vgcs_vbs_assign_res.aoip_transport_layer)) {
2499 LOG_CELL(cell, LOGL_ERROR, "Cannot RTP-CONNECT, invalid RTP IP:port in incoming MNCC "
2500 "message\n");
2501 goto channel_fail;
2502 }
2503 rtp_stream_set_remote_addr(cell->rtps, &ss);
2504 /* Send remote codec to RTP stream. */
2505 cm = codec_mapping_by_gsm0808_speech_codec_type(rx_ran_msg->vgcs_vbs_assign_res.codec_msc_chosen.type);
2506 if (!cm) {
2507 LOG_CELL(cell, LOGL_ERROR, "Chosen codec by BSC is not supported by MSC.\n");
2508 goto channel_fail;
2509 }
2510 rtp_stream_set_one_codec(cell->rtps, &cm->sdp);
2511 /* Set listening mode. */
2512 rtp_stream_set_mode(cell->rtps, MGCP_CONN_SEND_ONLY);
2513 /* Commit RTP stream. */
2514 rc = rtp_stream_commit(cell->rtps);
2515 if (rc < 0) {
2516 LOG_CELL(cell, LOGL_ERROR, "Failed to commit parameters to RTP stream.\n");
2517 goto channel_fail;
2518 }
2519 /* Change state. */
2520 osmo_fsm_inst_state_chg(fi, VGCS_CELL_ST_ACTIVE, 0, 0);
2521 /* Notify BSS FSM about channel activation. */
2522 if (cell->bss)
2523 osmo_fsm_inst_dispatch(cell->bss->fi, VGCS_BSS_EV_ACTIVE_OR_FAIL, NULL);
2524 break;
2525 case VGCS_CELL_EV_ASSIGN_FAIL:
2526 /* Received VGCS/VBS ASSIGNMENT FAILURE from BSS. */
2527 LOG_CELL(cell, LOGL_NOTICE, "Received VGCS/VBS ASSIGNMENT FAILURE from BSS.\n");
2528channel_fail:
2529 bss = cell->bss;
Andreas Eversberge24636c2023-04-23 12:20:55 +02002530 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2531 /* Notify BSS FSM about channel failure. */
2532 if (bss)
2533 osmo_fsm_inst_dispatch(bss->fi, VGCS_BSS_EV_ACTIVE_OR_FAIL, NULL);
2534 break;
2535 case VGCS_CELL_EV_CLEAR:
2536 /* The calling user process requested clearing of VGCS/VBS call. */
2537 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2538 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2539 break;
2540 case VGCS_CELL_EV_CLOSE:
2541 /* The SCCP connection from the MSC has been closed. */
2542 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2543 if (cell->conn) {
2544 cell->conn->vgcs.bss = NULL;
2545 cell->conn = NULL;
2546 }
2547 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2548 break;
2549 default:
2550 OSMO_ASSERT(false);
2551 }
2552}
2553
2554static void vgcs_cell_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2555{
2556 struct vgcs_bss_cell *cell = fi->priv;
2557
2558 switch (event) {
2559 case VGCS_CELL_EV_RTP_STREAM_GONE:
2560 /* The RTP stream failed. */
2561 LOG_CELL(cell, LOGL_ERROR, "RTP stream of MGW failed.\n");
2562 cell->rtps = NULL;
2563 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2564 break;
2565 case VGCS_CELL_EV_RTP_STREAM_ESTABLISHED:
2566 /* The RTP stream established. */
2567 LOG_CELL(cell, LOGL_DEBUG, "RTP stream is established.\n");
2568 break;
2569 case VGCS_CELL_EV_CLEAR:
2570 /* The calling user process requested clearing of VGCS/VBS call. */
2571 LOG_CELL(cell, LOGL_DEBUG, "Received clearing from BSS controling process.\n");
2572 cell_clear(cell, GSM0808_CAUSE_CALL_CONTROL);
2573 break;
2574 case VGCS_CELL_EV_CLOSE:
2575 /* The SCCP connection from the MSC has been closed. */
2576 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP connecting closing from MSC.\n");
2577 if (cell->conn) {
2578 cell->conn->vgcs.bss = NULL;
2579 cell->conn = NULL;
2580 }
2581 cell_clear(cell, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC);
2582 break;
2583 default:
2584 OSMO_ASSERT(false);
2585 }
2586}
2587
2588static void vgcs_cell_fsm_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2589{
2590 struct vgcs_bss_cell *cell = fi->priv;
2591
2592 switch (event) {
2593 case VGCS_CELL_EV_RTP_STREAM_GONE:
2594 /* The RTP stream gone. */
2595 LOG_CELL(cell, LOGL_ERROR, "RTP stream gone.\n");
2596 cell->rtps = NULL;
2597 /* Wait for RAN conn. */
2598 if (cell->conn)
2599 break;
2600 cell_destroy(cell);
2601 break;
2602 case VGCS_CELL_EV_CLEAR:
2603 case VGCS_CELL_EV_RELEASED:
2604 if (event == VGCS_CELL_EV_CLEAR) {
2605 /* The SCCP connection from the MSC has been closed while waiting for CLEAR COMPLETE. */
2606 LOG_CELL(cell, LOGL_NOTICE, "Received SCCP closing collision.\n");
2607 } else
2608 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR COMPLETE from BSS, we are done!\n");
2609 /* Wait for RTP stream. */
2610 if (cell->rtps) {
2611 /* close RAN conn */
2612 if (cell->conn) {
2613 cell->conn->vgcs.cell = NULL;
2614 ran_conn_close(cell->conn);
2615 cell->conn = NULL;
2616 }
2617 break;
2618 }
2619 cell_destroy(cell);
2620 break;
2621 default:
2622 OSMO_ASSERT(false);
2623 }
2624}
2625
2626static const struct osmo_fsm_state vgcs_cell_fsm_states[] = {
2627 [VGCS_CELL_ST_NULL] = {
2628 .name = "NULL",
2629 .in_event_mask = S(VGCS_CELL_EV_ASSIGN) |
2630 S(VGCS_CELL_EV_CLEAR),
2631 .out_state_mask = S(VGCS_CELL_ST_ASSIGNMENT),
2632 .action = vgcs_cell_fsm_null,
2633 },
2634 [VGCS_CELL_ST_ASSIGNMENT] = {
2635 .name = "ASSIGNMENT Sent",
2636 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2637 S(VGCS_CELL_EV_RTP_STREAM_ADDR_AVAILABLE) |
2638 S(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED) |
2639 S(VGCS_CELL_EV_ASSIGN_RES) |
2640 S(VGCS_CELL_EV_ASSIGN_FAIL) |
2641 S(VGCS_CELL_EV_CLEAR) |
2642 S(VGCS_CELL_EV_CLOSE),
2643 .out_state_mask = S(VGCS_CELL_ST_ACTIVE) |
2644 S(VGCS_CELL_ST_RELEASE),
2645 .action = vgcs_cell_fsm_assignment,
2646 },
2647 [VGCS_CELL_ST_ACTIVE] = {
2648 .name = "VGCS/VBS channel active",
2649 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2650 S(VGCS_CELL_EV_RTP_STREAM_ESTABLISHED) |
2651 S(VGCS_CELL_EV_CLEAR) |
2652 S(VGCS_CELL_EV_CLOSE),
2653 .out_state_mask = S(VGCS_CELL_ST_RELEASE),
2654 .action = vgcs_cell_fsm_active,
2655 },
2656 [VGCS_CELL_ST_RELEASE] = {
2657 .name = "Releasing VGCS/VBS channel",
2658 .in_event_mask = S(VGCS_CELL_EV_RTP_STREAM_GONE) |
2659 S(VGCS_CELL_EV_CLEAR) |
2660 S(VGCS_CELL_EV_RELEASED),
2661 .out_state_mask = S(VGCS_CELL_ST_NULL),
2662 .action = vgcs_cell_fsm_release,
2663 },
2664};
2665
2666static struct osmo_fsm vgcs_cell_fsm = {
2667 .name = "vgcs_cell",
2668 .states = vgcs_cell_fsm_states,
2669 .num_states = ARRAY_SIZE(vgcs_cell_fsm_states),
2670 .log_subsys = DASCI,
2671 .event_names = vgcs_cell_fsm_event_names,
2672};
2673
2674/* The BSS accepts VGCS/VBS channel assignment. */
2675void vgcs_vbs_assign_result(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2676{
2677 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN_RES, (void *)ran_msg);
2678}
2679
2680/* The BSS refuses VGCS/VBS channel assignment. */
2681void vgcs_vbs_assign_fail(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2682{
2683 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_ASSIGN_FAIL, (void *)ran_msg);
2684}
2685
2686/* BSS indicated that the channel has been released. */
2687void vgcs_vbs_clear_req_channel(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2688{
2689 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR REQUEST for resource controling channel from BSS.\n");
2690 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_CLOSE, (void *)ran_msg);
2691}
2692
2693/* BSS confirms the release of channel. */
2694void vgcs_vbs_clear_cpl_channel(struct vgcs_bss_cell *cell, const struct ran_msg *ran_msg)
2695{
2696 LOG_CELL(cell, LOGL_DEBUG, "Received CLEAR COMPLETE for resource controling channel from BSS.\n");
2697 osmo_fsm_inst_dispatch(cell->fi, VGCS_CELL_EV_RELEASED, (void *)ran_msg);
2698}
2699
2700/*
2701 * MGW endpoint FSM
2702 */
2703
2704static const struct value_string vgcs_mgw_ep_fsm_event_names[] = {
2705 OSMO_VALUE_STRING(VGCS_MGW_EP_EV_FREE),
2706 OSMO_VALUE_STRING(VGCS_MGW_EP_EV_CLEAR),
2707 { }
2708};
2709
2710static void vgcs_mgw_ep_fsm_active(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2711{
2712 struct vgcs_mgw_ep *mgw = fi->priv;
2713 struct vgcs_bss_cell *cell, *cell2;
2714 struct mgcp_client *mgcp_client;
2715
2716 switch (event) {
2717 case VGCS_MGW_EP_EV_FREE:
2718 LOGP(DASCI, LOGL_DEBUG, "MGW connection closed, removing all cell instances.\n");
2719 llist_for_each_entry_safe(cell, cell2, &mgw->cell_list, list_mgw) {
2720 if (cell->rtps)
2721 cell->rtps->ci = NULL;
2722 llist_del(&cell->list_mgw);
2723 cell->mgw = NULL;
2724 }
2725 /* Put MGCP client back into MGW pool. */
2726 mgcp_client = osmo_mgcpc_ep_client(mgw->mgw_ep);
2727 mgcp_client_pool_put(mgcp_client);
2728 /* Destroy this instance. */
2729 osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL);
2730 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
2731 break;
2732 case VGCS_MGW_EP_EV_CLEAR:
2733 if (!llist_empty(&mgw->cell_list))
2734 break;
2735 LOGP(DASCI, LOGL_DEBUG, "Cell list of MGW instance is now empty, dropping.\n");
2736 /* Destroy this instance. */
2737 osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL);
2738 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
2739 break;
2740 default:
2741 OSMO_ASSERT(false);
2742 }
2743}
2744
2745static const struct osmo_fsm_state vgcs_mgw_ep_fsm_states[] = {
2746 [VGCS_MGW_EP_ST_NULL] = {
2747 .name = "NULL",
2748 .out_state_mask = S(VGCS_MGW_EP_ST_ACTIVE),
2749 },
2750 [VGCS_MGW_EP_ST_ACTIVE] = {
2751 .name = "MGW endpoint allocated",
2752 .in_event_mask = S(VGCS_MGW_EP_EV_FREE) |
2753 S(VGCS_MGW_EP_EV_CLEAR),
2754 .out_state_mask = S(VGCS_MGW_EP_ST_NULL),
2755 .action = vgcs_mgw_ep_fsm_active,
2756 },
2757};
2758
2759static struct osmo_fsm vgcs_mgw_ep_fsm = {
2760 .name = "vgcs_mgw_ep",
2761 .states = vgcs_mgw_ep_fsm_states,
2762 .num_states = ARRAY_SIZE(vgcs_mgw_ep_fsm_states),
2763 .log_subsys = DASCI,
2764 .event_names = vgcs_mgw_ep_fsm_event_names,
2765};