blob: 0c62003f3273f3d922a9656cae4fa67d41825ddc [file] [log] [blame]
Harald Welte28d943e2017-11-25 15:00:50 +01001module MSC_ConnectionHandler {
2
Harald Welte34b5a952019-05-27 11:54:11 +02003/* MSC Connection HAndler of BSC Tests in TTCN-3
4 * (C) 2017-2019 Harald Welte <laforge@gnumonks.org>
5 * contributions by sysmocom - s.f.m.c. GmbH
6 * All rights reserved.
7 *
8 * Released under the terms of GNU General Public License, Version 2 or
9 * (at your option) any later version.
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 */
13
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +010014import from Misc_Helpers all;
Harald Welte28d943e2017-11-25 15:00:50 +010015import from General_Types all;
16import from Osmocom_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +010017import from GSM_Types all;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +020018import from IPA_Emulation all;
Harald Welte28d943e2017-11-25 15:00:50 +010019import from SCCPasp_Types all;
20import from BSSAP_Types all;
Harald Welte6811d102019-04-14 22:23:14 +020021import from RAN_Emulation all;
Harald Welte47cd0e32020-08-21 12:39:11 +020022import from BSSAP_LE_Emulation all;
23import from BSSAP_LE_Types all;
24import from BSSMAP_LE_Templates all;
Harald Welte28d943e2017-11-25 15:00:50 +010025import from BSSMAP_Templates all;
26
Harald Welte211219e2018-01-29 22:03:36 +010027import from IPL4asp_Types all;
28import from Native_Functions all;
29
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +020030import from MGCP_CodecPort all;
Harald Welte28d943e2017-11-25 15:00:50 +010031import from MGCP_Types all;
32import from MGCP_Templates all;
Daniel Willmann191e0d92018-01-17 12:44:35 +010033import from MGCP_Emulation all;
Harald Welte28d943e2017-11-25 15:00:50 +010034import from SDP_Types all;
35
Daniel Willmannebdecc02020-08-12 15:30:17 +020036import from StatsD_Checker all;
37
Harald Weltec1a2fff2017-12-17 11:06:19 +010038import from RSL_Emulation all;
39import from RSL_Types all;
40
41import from MobileL3_Types all;
42import from MobileL3_CommonIE_Types all;
Harald Welte211219e2018-01-29 22:03:36 +010043import from MobileL3_RRM_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +010044import from L3_Templates all;
45
Harald Weltec20b1c42018-02-12 20:50:08 +010046import from TELNETasp_PortType all;
47import from Osmocom_VTY_Functions all;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +000048import from TCCConversion_Functions all;
Harald Weltec20b1c42018-02-12 20:50:08 +010049
Harald Weltec1a2fff2017-12-17 11:06:19 +010050
Harald Welte211219e2018-01-29 22:03:36 +010051/***********************************************************************
52 * Media related handling
53 ***********************************************************************/
54
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +020055/* TODO: import OSMUX_Types.ttcn */
56type INT1 OsmuxCID (0 .. 255);
57
Philipp Maier11a58942018-06-25 16:40:48 +020058/* Get the matching payload type for a specified BSSAP codec type
59 * (see also: BSSAP_Types.ttcn */
60private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
61 if (codecType == GSM_FR) {
62 return PT_GSM;
63 } else if (codecType == GSM_HR) {
64 return PT_GSMHR;
65 } else if (codecType == GSM_EFR) {
66 return PT_GSMEFR;
67 } else if (codecType == FR_AMR or codecType == HR_AMR) {
68 return PT_AMR;
69 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
70 return PT_AMRWB;
71 }
72
73 return PT_PCMU;
74}
75
Harald Welte211219e2018-01-29 22:03:36 +010076/* Tuple containing host/ip and port */
77type record HostPort {
78 HostName host,
79 PortNumber port_nr
80};
81
82/* State encapsulating one MGCP Connection */
83type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020084 integer crcx_seen, /* Counts how many CRCX operations happend */
85 integer mdcx_seen, /* Counts how many MDCX operations happend C */
86 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
87 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010088 MgcpConnectionId conn_id,
89 charstring mime_type, /* e.g. AMR */
90 integer sample_rate, /* 8000 */
91 integer ptime, /* 20 */
92 uint7_t rtp_pt, /* RTP Payload Type */
93 HostPort mgw, /* MGW side */
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +020094 HostPort peer, /* CA side */
95 OsmuxCID local_osmux_cid optional,
96 OsmuxCID remote_osmux_cid optional
Harald Welte211219e2018-01-29 22:03:36 +010097};
98
99/* BTS media state */
100type record BtsMediaState {
101 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200102 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +0100103 uint16_t conn_id,
104 uint7_t rtp_pt,
105 HostPort bts,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200106 HostPort peer,
107 OsmuxCID local_osmux_cid optional,
108 OsmuxCID remote_osmux_cid optional
Harald Welte211219e2018-01-29 22:03:36 +0100109};
110
111type record MediaState {
112 MgcpEndpoint mgcp_ep,
113 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +0100114 BtsMediaState bts,
115 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +0100116};
117
Philipp Maier11a58942018-06-25 16:40:48 +0200118function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +0100119 /* BTS Side */
120 g_media.bts := {
121 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200122 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +0100123 conn_id := nr,
124 rtp_pt := 0,
125 bts := {
126 host := bts,
127 port_nr := 9000 + nr*2
128 },
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200129 peer := -,
130 local_osmux_cid := nr,
131 remote_osmux_cid := omit
Harald Welte211219e2018-01-29 22:03:36 +0100132 }
133
Harald Welte261af4b2018-02-12 21:20:39 +0100134 g_media.bts1 := {
135 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200136 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100137 conn_id := nr,
138 rtp_pt := 0,
139 bts := {
140 host := bts, /* FIXME */
141 port_nr := 9000 + nr*2
142 },
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200143 peer := -,
144 local_osmux_cid := nr,
145 remote_osmux_cid := omit
Harald Welte261af4b2018-02-12 21:20:39 +0100146 }
147
Harald Welte363cb0a2018-01-30 19:35:53 +0100148 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100149
150 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200151 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100152 g_media.mgcp_conn[i].sample_rate := 8000;
153 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200154 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200155 g_media.mgcp_conn[i].crcx_seen := 0;
156 g_media.mgcp_conn[i].mdcx_seen := 0;
157 g_media.mgcp_conn[i].crcx_seen_exp := 0;
158 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100159 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200160 g_media.mgcp_conn[i].local_osmux_cid := i;
161 g_media.mgcp_conn[i].remote_osmux_cid := omit;
Harald Welte211219e2018-01-29 22:03:36 +0100162 }
163
164 g_media.mgcp_conn[0].mgw := {
165 host := mgw,
166 port_nr := 10000 + nr*2
167 }
168 g_media.mgcp_conn[1].mgw := {
169 host := mgw,
170 port_nr := 11000 + nr*2
171 }
172}
173
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200174/* Helper function to get the next free MGCP connection identifier. We can
175 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100176private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
177 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200178 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100179 return i;
180 }
181 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100182 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Only 2 Connections per EP!");
183 /* Should never be reached */
184 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100185}
186
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200187/* Helper function to pick a specific connection by its cid. Since we reach out
188 * for a connection that is in-use we also check if there was already exactly
189 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100190private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
191 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200192 if (g_media.mgcp_conn[i].conn_id == cid and g_media.mgcp_conn[i].crcx_seen == 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100193 return i;
194 }
195 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100196 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No Connection for ID ", cid));
197 /* Should not be reached */
198 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100199}
200
Oliver Smithbc392a82023-04-17 14:06:42 +0200201/* Verify that CSD CRCX/MDCX has the RSL_IE_IPAC_RTP_CSD_FMT IE, and that
202 * inside it the D value is set to RSL_IPA_RTP_CSD_TRAU_BTS. */
203private function f_ipacc_crcx_mdcx_check_rtp_pt_csd(RSL_Message rsl) runs on MSC_ConnHdlr {
204 var SDP_FIELD_PayloadType pt_csd := PT_CSD;
205 var RSL_IE_Body ie;
206
207 if (g_media.bts.rtp_pt != enum2int(pt_csd)) {
208 return;
209 }
210
211 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_CSD_FMT, ie)) {
212 if (ie.ipa_rtp_csd_fmt.d != RSL_IPA_RTP_CSD_TRAU_BTS) {
213 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
214 "Rx unexpected IPAC CRCX for CSD with RTP_CSD_FMT IE");
215 }
216 return;
217 }
218
219 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
220 "Rx unexpected IPAC CRCX for CSD without RTP_CSD_FMT IE");
221}
222
Philipp Maierb2422352018-07-11 10:36:57 +0200223/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
224 * RSL level media handling */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200225altstep as_Media_ipacc(RSL_DCHAN_PT rsl_pt := RSL, RSL_DCHAN_PT rsl_pt_ho_target := RSL1) runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100226 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100227 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100228 var boolean b_unused;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200229 [not g_media.bts.ipa_crcx_seen] rsl_pt.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
Harald Welte211219e2018-01-29 22:03:36 +0100230 /* Extract parameters from request + use in response */
231 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
232 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
233 }
234 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
235 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
236 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200237 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
238 if (not g_pars.use_osmux_bts) {
239 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX with Osmux CID IE");
240 }
241 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
242 } else {
243 if (g_pars.use_osmux_bts) {
244 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX without Osmux CID IE");
245 }
246 g_media.bts.local_osmux_cid := omit;
247 g_media.bts.remote_osmux_cid := omit;
248 }
Oliver Smithbc392a82023-04-17 14:06:42 +0200249 f_ipacc_crcx_mdcx_check_rtp_pt_csd(rsl);
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200250 rsl_pt.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200251 f_inet_addr(g_media.bts.bts.host),
Harald Welte211219e2018-01-29 22:03:36 +0100252 g_media.bts.bts.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200253 g_media.bts.rtp_pt,
254 g_media.bts.local_osmux_cid));
Harald Welte211219e2018-01-29 22:03:36 +0100255 g_media.bts.ipa_crcx_seen := true;
256 repeat;
257 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200258 [g_media.bts.ipa_crcx_seen] rsl_pt.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
Harald Welte211219e2018-01-29 22:03:36 +0100259 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100260 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100261 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100262 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100263 }
264 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100265 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200266 g_media.bts.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100267 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100268 g_media.bts.peer.port_nr := ie.ipa_remote_port;
269 /* optional */
270 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
271 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
272 }
273 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
274 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
275 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200276 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
277 if (not g_pars.use_osmux_bts) {
278 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX with Osmux CID IE");
279 }
280 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
281 } else {
282 if (g_pars.use_osmux_bts) {
283 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX without Osmux CID IE");
284 }
285 g_media.bts.local_osmux_cid := omit;
286 g_media.bts.remote_osmux_cid := omit;
287 }
Oliver Smithbc392a82023-04-17 14:06:42 +0200288 f_ipacc_crcx_mdcx_check_rtp_pt_csd(rsl);
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200289 rsl_pt.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200290 f_inet_addr(g_media.bts.peer.host),
Harald Welte211219e2018-01-29 22:03:36 +0100291 g_media.bts.peer.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200292 g_media.bts.rtp_pt,
293 g_media.bts.local_osmux_cid));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200294 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100295 repeat;
296 }
Harald Welte261af4b2018-02-12 21:20:39 +0100297
298 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200299 [not g_media.bts1.ipa_crcx_seen] rsl_pt_ho_target.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
Harald Welte261af4b2018-02-12 21:20:39 +0100300 /* Extract parameters from request + use in response */
301 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
302 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
303 }
304 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
305 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
306 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200307 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
308 if (not g_pars.use_osmux_bts) {
309 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX with Osmux CID IE");
310 }
311 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
312 } else {
313 if (g_pars.use_osmux_bts) {
314 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX without Osmux CID IE");
315 }
316 g_media.bts.local_osmux_cid := omit;
317 g_media.bts.remote_osmux_cid := omit;
318 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200319 rsl_pt_ho_target.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200320 f_inet_addr(g_media.bts1.bts.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100321 g_media.bts1.bts.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200322 g_media.bts1.rtp_pt,
323 g_media.bts.local_osmux_cid));
Harald Welte261af4b2018-02-12 21:20:39 +0100324 g_media.bts1.ipa_crcx_seen := true;
325 repeat;
326 }
327 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200328 [g_media.bts1.ipa_crcx_seen] rsl_pt_ho_target.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
Harald Welte261af4b2018-02-12 21:20:39 +0100329 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100330 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100331 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100332 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100333 }
334 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100335 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200336 g_media.bts1.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100337 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100338 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
339 /* optional */
340 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
341 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
342 }
343 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
344 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
345 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200346 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
347 if (not g_pars.use_osmux_bts) {
348 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX with Osmux CID IE");
349 }
350 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
351 } else {
352 if (g_pars.use_osmux_bts) {
353 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX without Osmux CID IE");
354 }
355 g_media.bts.local_osmux_cid := omit;
356 g_media.bts.remote_osmux_cid := omit;
357 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200358 rsl_pt_ho_target.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200359 f_inet_addr(g_media.bts1.peer.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100360 g_media.bts1.peer.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200361 g_media.bts1.rtp_pt,
362 g_media.bts.local_osmux_cid));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200363 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100364 repeat;
365 }
366
Philipp Maierb2422352018-07-11 10:36:57 +0200367
368}
369
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200370function f_rx_crcx(MgcpCommand mgcp_cmd)
371 runs on MSC_ConnHdlr return template MgcpResponse {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200372 var SDP_Message sdp;
373 var integer cid := f_get_free_mgcp_conn();
Philipp Maiera2083892020-09-21 14:18:36 +0200374 var charstring local_rtp_addr;
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200375 var MgcpOsmuxCID osmux_cid;
Philipp Maiera2083892020-09-21 14:18:36 +0200376
377 if (g_pars.media_mgw_offer_ipv6 == true) {
378 local_rtp_addr := host_mgw_rtp_v6; /* Use IPv6 by default if no remote addr is provided by client */
379 } else {
380 local_rtp_addr := host_mgw_rtp_v4; /* Use IPv4 by default if no remote addr is provided by client */
381 }
382
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200383 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
384 if (cid != 0) {
385 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
386 }
387 /* we keep the endpoint name allocated during MediaState_init */
388 } else {
389 /* Call Agent allocated endpoint, trust/use it always */
390 g_media.mgcp_ep := mgcp_cmd.line.ep;
391 }
392 if (isvalue(mgcp_cmd.sdp)) {
393 sdp := mgcp_cmd.sdp;
394 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
395 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200396 if (sdp.connection.addr_type == "IP6") {
397 local_rtp_addr := host_mgw_rtp_v6;
398 } else {
399 local_rtp_addr := host_mgw_rtp_v4;
400 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200401 }
402 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200403 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200404 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
405 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
406 mgcp_conn.mime_type & "/" &
407 int2str(mgcp_conn.sample_rate))),
408 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
409 var template MgcpResponse mgcp_resp;
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200410 if ((g_pars.use_osmux_cn or g_pars.use_osmux_bts) and
411 f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200412 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200413 if (osmux_cid != -1) {
414 mgcp_conn.remote_osmux_cid := osmux_cid;
415 }
416 mgcp_resp := ts_CRCX_ACK_osmux(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, mgcp_conn.local_osmux_cid, sdp);
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200417 } else {
418 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
419 }
420 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
421 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
422 return mgcp_resp;
423}
424
425function f_rx_mdcx(MgcpCommand mgcp_cmd)
426 runs on MSC_ConnHdlr return template MgcpResponse {
427 var SDP_Message sdp;
428 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200429 var charstring local_rtp_addr;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200430 if (isvalue(mgcp_cmd.sdp)) {
431 sdp := mgcp_cmd.sdp;
432 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
433 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200434 if (sdp.connection.addr_type == "IP6") {
435 local_rtp_addr := host_mgw_rtp_v6;
436 } else {
437 local_rtp_addr := host_mgw_rtp_v4;
438 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200439 } else {
440 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
441 }
442 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200443 sdp := valueof(ts_SDP(mgcp_conn.peer.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200444 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
445 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
446 mgcp_conn.mime_type & "/" &
447 int2str(mgcp_conn.sample_rate))),
448 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
449 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
450 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
451}
452
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200453function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
454runs on MSC_ConnHdlr return template MGCP_RecvFrom {
455 var template MGCP_RecvFrom mrf := {
456 connId := ?,
457 remName := ?,
458 remPort := ?,
459 locName := ?,
460 locPort := ?,
461 msg := msg
462 }
463 return mrf;
464}
465
Philipp Maierb2422352018-07-11 10:36:57 +0200466/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
467 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200468
Oliver Smith735b47c2023-02-15 16:03:54 +0100469altstep as_Media_mgw(boolean norepeat := false) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200470 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200471 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200472 var MGCP_RecvFrom mrf;
473 var template MgcpMessage msg_crcx := {
474 command := tr_CRCX
475 }
476 var template MgcpMessage msg_mdcx := {
477 command := tr_MDCX
478 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200479 var template MgcpMessage msg_dlcx := {
480 command := tr_DLCX
481 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200482 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200483
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200484 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200485 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100486 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200487 if(norepeat == false) {
488 repeat;
489 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200490 }
491
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200492 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
493 mgcp_resp := f_rx_crcx(mrf.msg.command);
494 msg_resp := {
495 response := mgcp_resp
496 }
497 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
498 if(norepeat == false) {
499 repeat;
500 }
501 }
502
Oliver Smith7a8594a2023-02-13 14:30:49 +0100503 [g_pars.aoip and not g_pars.ignore_mgw_mdcx] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200504 mgcp_resp := f_rx_mdcx(mgcp_cmd);
505 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200506 if(norepeat == false) {
507 repeat;
508 }
Harald Welte211219e2018-01-29 22:03:36 +0100509 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200510
Oliver Smith7a8594a2023-02-13 14:30:49 +0100511 [not g_pars.aoip and not g_pars.ignore_mgw_mdcx]
512 MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200513 mgcp_resp := f_rx_mdcx(mrf.msg.command);
514 msg_resp := {
515 response := mgcp_resp
516 }
517 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
518 if(norepeat == false) {
519 repeat;
520 }
521 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200522
Oliver Smith735b47c2023-02-15 16:03:54 +0100523 [g_pars.fail_on_dlcx and g_pars.aoip] MGCP.receive(tr_DLCX) {
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200524 setverdict(fail, "Unexpected DLCX received");
525 }
526
Oliver Smith735b47c2023-02-15 16:03:54 +0100527 [g_pars.fail_on_dlcx and not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_dlcx)) {
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200528 setverdict(fail, "Unexpected DLCX received");
529 }
Harald Welte211219e2018-01-29 22:03:36 +0100530}
531
Philipp Maierb2422352018-07-11 10:36:57 +0200532/* Altsteps for handling of media related commands. Can be activated by a given
533 * test case if it expects to see media related handling (i.e. voice calls) */
Oliver Smith735b47c2023-02-15 16:03:54 +0100534altstep as_Media() runs on MSC_ConnHdlr {
Oliver Smith4eed06f2023-02-15 15:43:19 +0100535 [not g_pars.ignore_ipa_media] as_Media_ipacc();
Oliver Smith735b47c2023-02-15 16:03:54 +0100536 [] as_Media_mgw();
Philipp Maierb2422352018-07-11 10:36:57 +0200537}
Harald Welte211219e2018-01-29 22:03:36 +0100538
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200539type port Coord_PT message
540{
541 inout charstring;
542} with { extension "internal" };
543
Harald Welte28d943e2017-11-25 15:00:50 +0100544/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200545 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
546 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
Harald Welte47cd0e32020-08-21 12:39:11 +0200547type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr, BSSAP_LE_ConnHdlr, StatsD_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100548 /* SCCP Connecction Identifier for the underlying SCCP connection */
549 var integer g_sccp_conn_id;
550
Harald Welte6811d102019-04-14 22:23:14 +0200551 /* procedure port back to our parent (RAN_Emulation_CT) for control */
552 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100553 port TELNETasp_PT BSCVTY;
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200554 port Coord_PT COORD;
Vadim Yanitskiy5f14d342022-02-07 14:18:38 +0600555 port Coord_PT COORD2;
Harald Welte28d943e2017-11-25 15:00:50 +0100556
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200557 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
558 port IPA_MGCP_PT MGCP_MSC_CLIENT;
559 var integer g_trans_id := 0;
560
Harald Welte211219e2018-01-29 22:03:36 +0100561 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100562 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100563
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200564 var charstring host_bts := "127.0.0.2";
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200565 var charstring host_mgw_mgcp := "127.0.0.3";
566 var charstring host_mgw_rtp_v4 := "127.0.0.5";
567 var charstring host_mgw_rtp_v6 := "::1";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200568 var charstring host_msc := "127.0.0.4";
569
Harald Weltee97eab42018-03-21 18:46:06 +0100570 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100571}
572
Harald Welteeddf0e92020-06-21 19:42:15 +0200573function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100574 if (not g_vty_initialized) {
575 map(self:BSCVTY, system:BSCVTY);
576 f_vty_set_prompts(BSCVTY);
577 f_vty_transceive(BSCVTY, "enable");
578 g_vty_initialized := true;
579 }
Harald Welte28d943e2017-11-25 15:00:50 +0100580}
581
Harald Welteeddf0e92020-06-21 19:42:15 +0200582/* initialize all parameters */
583function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
584 f_MediaState_init(g_media, i, bts, mgw, codecType);
585 f_MscConnHdlr_init_vty();
586}
587
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200588private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
589 var MgcpTransId tid := int2str(g_trans_id);
590 g_trans_id := g_trans_id + 1;
591 return tid;
592}
593
Harald Welte6811d102019-04-14 22:23:14 +0200594/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100595 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
596private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200597runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100598 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200599 var boolean append_osmux_support := g_ran_ops.use_osmux and
600 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100601
Harald Weltec1a2fff2017-12-17 11:06:19 +0100602 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200603 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
604 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100605 }
606
607 return resp;
608}
609
Harald Welte47cd0e32020-08-21 12:39:11 +0200610/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
611 * BSSAP_LE message arrives. Can return a PDU_BSSAP_LE that should be sent in return */
612private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
613runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
614 var template PDU_BSSAP_LE resp := omit;
615
616 /* answer all RESET with a RESET ACK */
617 if (match(bssap, tr_BSSMAP_LE_Reset)) {
618 resp := ts_BSSMAP_LE_ResetAck;
619 }
620
621 return resp;
622}
623
Harald Welte6811d102019-04-14 22:23:14 +0200624const RanOps MSC_RanOps := {
625 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100626 unitdata_cb := refers(UnitdataCallback),
627 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100628 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200629 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200630 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200631 use_osmux := false,
Eric Wild66448c12022-04-02 14:57:53 +0200632 bssap_reset_retries := 1,
Harald Welte930d0a72018-03-22 22:08:40 +0100633 sccp_addr_local := omit,
634 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100635}
636
Harald Welte47cd0e32020-08-21 12:39:11 +0200637const BssapLeOps SMLC_BssapLeOps := {
638 create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
639 unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
640 decode_dtap := false,
641 role_ms := false,
642 sccp_addr_local := omit,
643 sccp_addr_peer := omit
644}
645
Daniel Willmann191e0d92018-01-17 12:44:35 +0100646const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100647 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
648 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100649}
650
Harald Weltec1a2fff2017-12-17 11:06:19 +0100651/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200652function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200653 RAN.call(RAN_register:{l3_enc, self}) {
654 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100655 }
656}
657
Harald Welte651fcdc2018-05-10 20:23:16 +0200658type record TestHdlrEncrParams {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100659 /* A mask of multiple encryption algorithms, for Encryption Information IE. */
660 OCT1 enc_alg_permitted,
661 /* Expect this encryption algorithm in Channel Activation from the BSC.
662 * To expect no encryption, set to '01'O == A5/0. */
663 OCT1 enc_alg_expect,
664 /* In BSSMAP, the Chosen Encryption Algorithm IE that the test sends to the BSC.
665 * When set to omit, the MSC will not send a Chosen Encryption Algorithm IE. */
666 OCT1 enc_alg_chosen optional,
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200667 octetstring enc_key,
668 octetstring enc_kc128 optional
Harald Welte651fcdc2018-05-10 20:23:16 +0200669};
670
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100671template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg_permitted,
672 octetstring key, template (omit) octetstring kc128 := omit) := {
673 enc_alg_permitted := alg_permitted,
674 enc_alg_expect := alg_permitted, /* <- for compat with tests before enc_alg_expect was added */
675 enc_alg_chosen := alg_permitted, /* <- for compat with tests before enc_alg_chosen was added */
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200676 enc_key := key,
677 enc_kc128 := kc128
Harald Welte651fcdc2018-05-10 20:23:16 +0200678}
679
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100680template (value) TestHdlrEncrParams t_EncrParams2(OCT1 alg_permitted,
681 OCT1 alg_expect,
682 template(omit) OCT1 alg_chosen,
683 octetstring key,
684 template (omit) octetstring kc128 := omit) := {
685 enc_alg_permitted := alg_permitted,
686 enc_alg_expect := alg_expect,
687 enc_alg_chosen := alg_chosen,
688 enc_key := key,
689 enc_kc128 := kc128
690}
691
692/* Convenience for common t_EncrParams2 usage.
693 *
694 * To test a scenario where only A5/3 is permitted:
695 * f_encr_params('08'O);
696 * To test a scenario where A5/3 is chosen from A5/0,A5/1,A5/3 (1 | 2 | 8 = 11 = 0xb):
697 * f_encr_params('0b'O, '08'O);
698 * To test the same, but the Chosen Encryption Algorithm IE should reflect A5/1:
699 * f_encr_params('0b'O, '08'O, '02'O);
700 * To test the same, but the MSC sends no Chosen Encryption Algorithm IE:
701 * f_encr_params('0b'O, '08'O, omit);
702 *
703 * Set alg_chosen and alg_expect magically when == '00'O:
704 * - enc_alg_expect is set to alg_chosen if present, else to alg_permitted.
705 * - enc_alg_chosen is set to alg_permitted.
706 * When only alg_permitted is given, alg_permitted must reflect only one algorithm, or f_cipher_mode_bssmap_to_rsl()
707 * will fail.
708 * When alg_chosen is passed as omit, the messages from the MSC will not contain a Chosen Encryption Algorithm IE.
709 */
710function f_encr_params(OCT1 alg_permitted, OCT1 alg_expect := '00'O, template (omit) OCT1 alg_chosen := '00'O,
711 boolean kc128 := false)
712 return TestHdlrEncrParams
713{
714 if (not istemplatekind(alg_chosen, "omit")) {
715 if (valueof(alg_chosen) == '00'O) {
716 if (alg_expect != '00'O) {
717 alg_chosen := alg_expect;
718 } else {
719 /* In this case, alg_permitted should have only one permitted algo */
720 alg_chosen := alg_permitted;
721 }
722 }
723 if (alg_expect == '00'O) {
724 alg_expect := valueof(alg_chosen);
725 }
726 }
727 if (alg_expect == '00'O) {
728 /* In this case, alg_permitted should have only one permitted algo */
729 alg_expect := valueof(alg_permitted);
730 }
Vadim Yanitskiy35c9a332022-03-16 20:26:35 +0300731 var template (omit) octetstring kc := omit;
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100732 if (kc128) {
733 kc := f_rnd_octstring(16);
734 }
735 return valueof(t_EncrParams2(alg_permitted, alg_expect, alg_chosen, f_rnd_octstring(8), kc));
736}
737
Harald Weltecc0b0142018-05-29 15:19:33 +0200738type record TestHdlrParamsLcls {
739 GlobalCallReferenceValue gcr optional,
740 /* LCLS Configuration */
741 BIT4 cfg optional,
742 /* LCLS Connection Status Control */
743 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100744 BIT4 exp_sts optional,
745 /* Whether to adjust *cx_seen_exp for LCLS tests */
746 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200747}
748
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200749type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200750 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200751 integer rsl_idx,
752 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200753}
754
Harald Welte801ed1a2023-04-30 15:07:00 +1000755type record ASCITest {
756 boolean vgcs_setup_ok,
757 boolean vgcs_assign_ok,
758 boolean vgcs_assign_fail,
759 boolean vgcs_talker_req,
Andreas Eversbergcfdbb3e2023-07-20 12:22:58 +0200760 boolean vgcs_talker_fail,
Harald Welte801ed1a2023-04-30 15:07:00 +1000761 boolean vgcs_talker_est,
Andreas Eversberg35874922023-07-07 13:44:04 +0200762 boolean vgcs_talker_rel,
763 boolean vgcs_uplink_seized,
764 boolean vgcs_uplink_release
Harald Welte801ed1a2023-04-30 15:07:00 +1000765};
766
Harald Weltec1a2fff2017-12-17 11:06:19 +0100767type record TestHdlrParams {
768 OCT1 ra,
769 GsmFrameNumber fn,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100770 hexstring imsi optional,
771 hexstring imei optional,
Harald Welte60aa5762018-03-21 19:33:13 +0100772 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200773 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200774 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200775 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100776 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200777 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100778 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200779 SCCP_PAR_Address sccp_addr_msc optional,
780 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100781 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100782 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200783 boolean aoip,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200784 boolean use_osmux_cn,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200785 boolean use_osmux_bts,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200786 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200787 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200788 integer mgwpool_idx, /* MGCP_Emulation_CT (vc_MGCP) to use for this MSC ConnHdlr */
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200789 boolean media_mgw_offer_ipv6,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200790 OCT3 last_used_eutran_plmn optional,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200791 boolean exp_fast_return, /* RR Release expected to contain CellSelectInd ? */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200792 boolean expect_channel_mode_modify,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100793 uint3_t expect_tsc optional,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100794 BSSMAP_IE_CellIdentifier cell_id_source,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100795 boolean expect_ho_fail,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100796 boolean expect_ho_fail_lchan_est,
797 boolean inter_bsc_ho_in__ho_req_in_initial_sccp_cr,
Oliver Smith735b47c2023-02-15 16:03:54 +0100798 boolean ignore_mgw_mdcx,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100799 boolean fail_on_dlcx,
Harald Welte801ed1a2023-04-30 15:07:00 +1000800 boolean ignore_ipa_media,
801 ASCITest asci_test
Harald Weltec1a2fff2017-12-17 11:06:19 +0100802};
803
Philipp Maier48604732018-10-09 15:00:37 +0200804/* Note: Do not use valueof() to get a value of this template, use
805 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100806 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100807template (value) TestHdlrParams t_def_TestHdlrPars := {
808 ra := '23'O,
809 fn := 23,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100810 imsi := omit, /* set to random in f_gen_test_hdlr_pars() */
811 imei := omit, /* set to random in f_gen_test_hdlr_pars() */
Harald Welte60aa5762018-03-21 19:33:13 +0100812 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200813 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200814 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200815 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100816 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200817 encr := omit,
818 lcls := {
819 gcr := omit,
820 cfg := omit,
821 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100822 exp_sts := omit,
823 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100824 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200825 sccp_addr_msc := omit,
826 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100827 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100828 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200829 aoip := true,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200830 use_osmux_cn := false,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200831 use_osmux_bts := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200832 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200833 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200834 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200835 rsl_idx := 0,
836 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200837 },
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200838 mgwpool_idx := 0,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200839 media_mgw_offer_ipv6 := true,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200840 last_used_eutran_plmn := omit,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200841 exp_fast_return := false,
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200842 expect_channel_mode_modify := false,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100843 expect_tsc := omit,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100844 cell_id_source := valueof(ts_CellID_LAC_CI(1, 1)),
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100845 expect_ho_fail := false,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100846 expect_ho_fail_lchan_est := false,
847 inter_bsc_ho_in__ho_req_in_initial_sccp_cr := true,
Oliver Smith735b47c2023-02-15 16:03:54 +0100848 ignore_mgw_mdcx := false,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100849 fail_on_dlcx := true,
Harald Welte801ed1a2023-04-30 15:07:00 +1000850 ignore_ipa_media := false,
851 asci_test := {
852 vgcs_setup_ok := false,
853 vgcs_assign_ok := false,
854 vgcs_assign_fail := false,
855 vgcs_talker_req := false,
856 vgcs_talker_est := false,
Andreas Eversberg35874922023-07-07 13:44:04 +0200857 vgcs_talker_rel := false,
858 vgcs_uplink_seized := false,
859 vgcs_uplink_release := false
Harald Welte801ed1a2023-04-30 15:07:00 +1000860 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100861}
862
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300863function f_create_chan_and_exp(template (present) PDU_BSSAP exp_l3_compl := ?)
864runs on MSC_ConnHdlr {
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100865 var MobileIdentityLV mi;
866 var PDU_ML3_MS_NW l3_info;
867 var octetstring l3_enc;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200868 var template uint3_t tsc := ?;
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300869 timer T;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200870
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100871 if (ispresent(g_pars.imsi)) {
872 mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
873 } else if (ispresent(g_pars.imei)) {
874 mi := valueof(ts_MI_IMEI_LV(g_pars.imei));
875 } else {
876 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
877 "Either imsi or imei must be set!");
878 }
879 l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
880 l3_enc := enc_PDU_ML3_MS_NW(l3_info);
881
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200882 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
883 tsc := g_pars.expect_tsc;
884 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100885
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300886 if (istemplatekind(exp_l3_compl, "?")) {
887 if (g_pars.aoip == false) {
888 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := omit);
889 } else {
890 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := ?);
891 }
892 }
893
Neels Hofmeyrd601f9c2021-06-04 22:46:06 +0200894 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100895 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200896 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn, tsc);
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300897 /* wait for a COMPL_L3 from the BSC to ensure that the SCCP connection is up */
898 T.start(2.0);
899 alt {
900 [] BSSAP.receive(exp_l3_compl);
901 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
902 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
903 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
904 }
905 [] T.timeout {
906 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
907 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
908 }
909 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100910}
911
Harald Welte898113b2018-01-31 18:32:21 +0100912function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200913 template (omit) RslChannelNr chan_nr := omit, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Welte898113b2018-01-31 18:32:21 +0100914 if (not isvalue(link_id)) {
915 link_id := ts_RslLinkID_DCCH(0);
916 }
917 if (not isvalue(chan_nr)) {
918 chan_nr := g_chan_nr;
919 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200920 rsl_pt.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
Harald Welte898113b2018-01-31 18:32:21 +0100921}
922
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200923function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100924 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100925 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100926 if (orig.msg_type == RSL_MT_ENCR_CMD) {
927 link_id := orig.ies[2].body.link_id;
928 } else {
929 link_id := orig.ies[1].body.link_id;
930 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200931 f_rsl_send_l3(l3, link_id, chan_nr, rsl_pt := rsl_pt);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100932}
933
Oliver Smith598e1ed2021-07-09 10:28:40 +0200934/* Convert the cipher representation on BSSMAP to the representation used on RSL */
935function f_cipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100936{
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300937 select (alg_bssmap) {
938 case ('01'O) { return RSL_ALG_ID_A5_0; }
939 case ('02'O) { return RSL_ALG_ID_A5_1; }
940 case ('04'O) { return RSL_ALG_ID_A5_2; }
941 case ('08'O) { return RSL_ALG_ID_A5_3; }
942 case ('10'O) { return RSL_ALG_ID_A5_4; }
943 case ('20'O) { return RSL_ALG_ID_A5_5; }
944 case ('40'O) { return RSL_ALG_ID_A5_6; }
945 case ('80'O) { return RSL_ALG_ID_A5_7; }
946 case else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100947 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
948 oct2str(alg_bssmap));
Harald Weltee613f962018-04-18 22:38:16 +0200949 return RSL_ALG_ID_A5_0;
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300950 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100951 }
952}
953
Vadim Yanitskiyaf2972e2022-03-14 22:27:57 +0300954/* Convert the cipher representation on BSSMAP to the one used on RR (3GPP TS 44.018) */
955function f_cipher_mode_bssmap_to_rr(OCT1 alg_bssmap) return BIT3
956{
957 select (alg_bssmap) {
958 case ('01'O) /* A5/0 */ { return '000'B; } /* SC=0 */
959 case ('02'O) /* A5/1 */ { return '000'B; } /* SC=1 */
960 case ('04'O) /* A5/2 */ { return '001'B; } /* SC=1 */
961 case ('08'O) /* A5/3 */ { return '010'B; } /* SC=1 */
962 case ('10'O) /* A5/4 */ { return '011'B; } /* SC=1 */
963 case ('20'O) /* A5/5 */ { return '100'B; } /* SC=1 */
964 case ('40'O) /* A5/6 */ { return '101'B; } /* SC=1 */
965 case ('80'O) /* A5/7 */ { return '110'B; } /* SC=1 */
966 case else {
967 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
968 oct2str(alg_bssmap));
969 return '000'B;
970 }
971 }
972}
973
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200974function f_verify_encr_info(RSL_Message rsl) runs on MSC_ConnHdlr {
975 var RSL_IE_Body encr_info;
976 var RSL_AlgId alg_rsl;
977 var template octetstring expect_kc;
978
979 /* If no encryption is enabled, then make sure there is no RSL_IE_ENCR_INFO */
980 if (not ispresent(g_pars.encr)) {
981 if (f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
982 setverdict(fail, "Found Encryption IE, but expected no encryption in ", rsl.msg_type);
983 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
984 return;
985 }
986 setverdict(pass);
987 return;
988 }
989
990 /* RSL uses a different representation of the encryption algorithm,
991 * so we need to convert first */
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100992 alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200993
994 if (alg_rsl == RSL_ALG_ID_A5_4 and ispresent(g_pars.encr.enc_kc128)) {
995 expect_kc := g_pars.encr.enc_kc128;
996 } else if (alg_rsl == RSL_ALG_ID_A5_0) {
997 /* When A5/0 is chosen, no encryption is active, so technically, no key is needed. However, 3GPP TS
998 * 48.058 9.3.7 Encryption Information stays quite silent about presence or absence of a key for A5/0.
999 * The only thing specified is how to indicate the length of the key; the possibility that the key may
1000 * be zero length is not explicitly mentioned. So it seems that we should always send the key along,
1001 * even for A5/0. Still, let's also allow a zero length key for A5/0. */
1002 expect_kc := (g_pars.encr.enc_key, ''O);
1003 } else {
1004 expect_kc := g_pars.encr.enc_key;
1005 }
1006 log("for encryption algo ", alg_rsl, " expect kc = ", expect_kc);
1007
1008 if (not f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
1009 if (alg_rsl == RSL_ALG_ID_A5_0) {
1010 /* For A5/0, encryption is not active. It is fine to omit the Encryption Information in this
1011 * case. Note that the first channel may see an RSL Encryption Command with A5/0 indicated, and
1012 * a subsequent handover may activate a new channel without any Encryption Information. */
1013 setverdict(pass);
1014 return;
1015 }
1016 setverdict(fail, "Missing Encryption Information IE in ", rsl.msg_type);
1017 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1018 return;
1019 }
1020
1021 if (not match(encr_info, tr_EncrInfo(alg_rsl, expect_kc))) {
1022 setverdict(fail, "Unexpected Kc in Encryption Information IE in ", rsl.msg_type);
1023 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1024 return;
1025 }
1026 setverdict(pass);
1027}
1028
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001029function f_cipher_mode(TestHdlrEncrParams enc, boolean exp_fail := false,
1030 RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC)
Harald Welte38b2a102017-12-23 02:42:58 +01001031runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +01001032 var PDU_BSSAP bssap;
1033 var RSL_Message rsl;
1034
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001035 if (isvalue(enc.enc_kc128)) {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001036 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(enc.enc_alg_permitted, enc.enc_key, valueof(enc.enc_kc128)));
Harald Welte73cd2712017-12-17 00:44:52 +01001037 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001038 BSSAP.send(ts_BSSMAP_CipherModeCmd(enc.enc_alg_permitted, enc.enc_key));
Harald Welte73cd2712017-12-17 00:44:52 +01001039 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +01001040
Harald Welte73cd2712017-12-17 00:44:52 +01001041 alt {
1042 /* RSL/UE Side */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001043 [] rsl_pt.receive(tr_RSL_ENCR_CMD(g_chan_nr)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +01001044 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
1045 log("Rx L3 from net: ", l3);
Neels Hofmeyr72749342021-06-11 02:41:37 +02001046
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +02001047 f_verify_encr_info(rsl);
1048
Harald Welte73cd2712017-12-17 00:44:52 +01001049 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001050 f_rsl_reply(ts_RRM_CiphModeCompl, rsl, rsl_pt := rsl_pt);
Harald Welte73cd2712017-12-17 00:44:52 +01001051 }
1052 repeat;
1053 }
1054 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001055 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001056 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +01001057 } else {
1058 setverdict(pass);
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001059 var RSL_AlgId alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr5e31cfc2021-06-11 02:42:11 +02001060 if (oct2int(bssap.pdu.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algorithmIdentifier) != enum2int(alg_rsl)) {
1061 setverdict(fail, "Unexpected Encryption Algorithm ID in BSSMAP Cipher Mode Complete");
1062 }
Harald Welte38b2a102017-12-23 02:42:58 +01001063 }
Harald Welte73cd2712017-12-17 00:44:52 +01001064 }
1065 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001066 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001067 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +01001068 } else {
1069 setverdict(pass);
1070 }
Harald Welte73cd2712017-12-17 00:44:52 +01001071 }
1072 }
1073}
1074
Harald Welte211219e2018-01-29 22:03:36 +01001075/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
1076function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
1077 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +01001078 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +01001079
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001080 select (inp) {
1081 case ('00001'B) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +01001082 chan_nr := valueof(t_RslChanNr_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001083 }
1084 case ('11101'B) { /* VAMOS TCH/F */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001085 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001086 }
1087 case ('0001?'B) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +01001088 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001089 }
1090 case ('1111?'B) { /* VAMOS TCH/H */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001091 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001092 }
1093 case ('001??'B) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001094 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001095 }
1096 case ('01???'B) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001097 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001098 }
1099 case else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001100 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001101 }
Harald Welte211219e2018-01-29 22:03:36 +01001102 }
1103
1104 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001105 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +01001106 } else {
1107 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
1108 arfcn := oct2int(concat);
1109 }
1110}
1111
1112type record AssignmentState {
1113 /* global */
Oliver Smithce2dd692023-04-17 10:25:50 +02001114 boolean rtp_stream,
Harald Welte211219e2018-01-29 22:03:36 +01001115 boolean is_assignment,
1116 /* Assignment related bits */
1117 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001118 boolean old_lchan_deact_sacch_seen,
1119 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +01001120 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +01001121 RslChannelNr old_chan_nr,
1122 /* Modify related bits */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001123 PDU_ML3_NW_MS rr_channel_mode_modify_msg optional,
Harald Welte211219e2018-01-29 22:03:36 +01001124 boolean rr_modify_seen,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001125 RSL_Message rsl_mode_modify_msg optional,
Harald Welte21583082018-01-29 22:28:26 +01001126 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +01001127}
1128
1129template (value) AssignmentState ts_AssignmentStateInit := {
Oliver Smithce2dd692023-04-17 10:25:50 +02001130 rtp_stream := false,
Harald Welte211219e2018-01-29 22:03:36 +01001131 is_assignment := false,
1132 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001133 old_lchan_deact_sacch_seen := false,
1134 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +01001135 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +01001136 old_chan_nr := -,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001137 rr_channel_mode_modify_msg := omit,
Harald Welte211219e2018-01-29 22:03:36 +01001138 rr_modify_seen := false,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001139 rsl_mode_modify_msg := omit,
Harald Welte21583082018-01-29 22:28:26 +01001140 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +01001141}
1142
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001143private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
1144 encr_info := {
1145 len := ?,
1146 alg_id := alg,
1147 key := key
1148 }
1149}
1150
1151/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
1152private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
1153 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001154 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001155 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001156
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001157 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg_permitted != '01'O) {
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001158 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001159 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001160 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001161 var RSL_AlgId alg := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr0588cad2021-06-11 01:38:18 +02001162 var octetstring expect_key;
1163 if (alg == RSL_ALG_ID_A5_4) {
1164 expect_key := g_pars.encr.enc_kc128;
1165 } else {
1166 expect_key := g_pars.encr.enc_key;
1167 }
1168 if (not match(encr_info, tr_EncrInfo(alg, expect_key))) {
1169 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
1170 "Unexpected Kc in Encryption IE in RSL ENCR CMD");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001171 }
1172 }
1173 } else {
1174 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
1175 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001176 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001177 }
1178 }
1179 }
1180 /* FIXME: validate RSL_IE_ACT_TYPE, RSL_IE_CHAN_MODE, RSL_IE_CHAN_IDENT, RSL_IE_BS_POWER,
Pau Espin Pedrol7a9ccf82019-11-05 11:33:33 +01001181 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001182
Pau Espin Pedrol8f773632019-11-05 11:46:53 +01001183 if (g_pars.exp_ms_power_params and not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER_PARAM, ms_power_param)) {
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001184 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
1185 }
1186
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001187 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
1188 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
1189 } else {
1190 if (not match(ms_power.ms_power, tr_RSL_IE_MS_Power(g_pars.exp_ms_power_level, false))) {
Pau Espin Pedrol7a9ccf82019-11-05 11:33:33 +01001191 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Wrong MS Power IE in CHAN ACT, ", ms_power.ms_power.power_level, " vs exp ", g_pars.exp_ms_power_level));
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001192 }
1193 }
1194
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001195}
1196
Neels Hofmeyr6a955cc2021-10-02 14:53:48 +02001197function rr_chan_desc_tsc(ChannelDescription2_V cd2)
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001198 return uint3_t
1199{
1200 var uint3_t tsc := oct2int(cd2.octet3);
1201 tsc := tsc / 32; /* shl 5 */
1202 return tsc;
1203}
1204
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001205altstep as_assignment(inout AssignmentState st, RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +01001206 var RSL_Message rsl;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001207 [not st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
Harald Welte211219e2018-01-29 22:03:36 +01001208 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1209 log("Rx L3 from net: ", l3);
1210 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
1211 var RslChannelNr new_chan_nr;
1212 var GsmArfcn arfcn;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001213
1214 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1215 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime);
1216 if (not match(got_tsc, g_pars.expect_tsc)) {
1217 setverdict(fail, "RR Assignment: unexpected TSC in Channel Description: expected ",
1218 g_pars.expect_tsc, " got ", got_tsc);
1219 mtc.stop;
1220 }
1221 }
1222
Harald Welte211219e2018-01-29 22:03:36 +01001223 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
1224 new_chan_nr, arfcn);
1225 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +01001226
Harald Welte211219e2018-01-29 22:03:36 +01001227 /* register our component for this channel number at the RSL Emulation */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001228 f_rslem_register(0, new_chan_nr, PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001229 /* dispatch queued messages for this channel (if any) */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001230 f_rslem_dchan_queue_dispatch(PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001231
Harald Welte211219e2018-01-29 22:03:36 +01001232 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
1233 /* send assignment complete over the new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001234 rsl_pt.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +01001235 enc_PDU_ML3_MS_NW(l3_tx)));
1236 /* by default, send via the new channel from now */
1237 st.old_chan_nr := g_chan_nr;
1238 g_chan_nr := new_chan_nr;
1239 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001240 /* obtain channel activation from RSL_Emulation for new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001241 var RSL_Message chan_act := f_rslem_get_last_act(rsl_proc_pt, 0, g_chan_nr);
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001242 /* check it (e.g. for correct ciphering parameters) */
1243 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +01001244 repeat;
1245 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001246 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +01001247 }
Harald Welte211219e2018-01-29 22:03:36 +01001248 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001249 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001250 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001251 repeat;
1252 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001253 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001254 st.old_lchan_rll_rel_req_seen := true;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001255 rsl_pt.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
Harald Welte211219e2018-01-29 22:03:36 +01001256 repeat;
1257 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001258 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1259 rsl_pt.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +01001260 /* unregister for old channel number in RSL emulation */
1261 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001262 f_rslem_unregister(0, st.old_chan_nr, PT := rsl_proc_pt);
Harald Welte21583082018-01-29 22:28:26 +01001263 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +01001264 repeat;
1265 }
1266}
1267
1268altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
1269 /* no assignment, just mode modify */
1270 var RSL_Message rsl;
1271
Oliver Smithce2dd692023-04-17 10:25:50 +02001272 [st.rtp_stream and not st.rr_modify_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
Harald Weltec1a2fff2017-12-17 11:06:19 +01001273 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1274 log("Rx L3 from net: ", l3);
1275 if (ischosen(l3.msgs.rrm.channelModeModify)) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001276 st.rr_channel_mode_modify_msg := l3;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001277 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
1278 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +01001279 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001280 }
1281 repeat;
1282 }
Oliver Smithce2dd692023-04-17 10:25:50 +02001283 [st.rtp_stream and st.rr_modify_seen] RSL.receive(tr_RSL_MsgTypeD(RSL_MT_MODE_MODIFY_REQ)) -> value rsl {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001284 st.rsl_mode_modify_msg := rsl;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001285 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +01001286 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001287 repeat;
1288 }
Harald Welte211219e2018-01-29 22:03:36 +01001289}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001290
Harald Welte211219e2018-01-29 22:03:36 +01001291/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
1292function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
1293return boolean {
1294 select (bssmap.speechOrDataIndicator) {
1295 case ('0011'B) { /* Signalling */
1296 /* all channels support signalling */
1297 return true;
1298 }
1299 case else { /* Speech, Speech+CTM or CSD */
1300 select (bssmap.channelRateAndType) {
1301 case ('08'O) { /* TCH/F */
1302 select (rsl_chan_nr) {
1303 case (t_RslChanNr_Bm(?)) { return true; }
1304 }
1305 }
1306 case ('09'O) { /* TCH/H */
1307 select (rsl_chan_nr) {
1308 case (t_RslChanNr_Lm(?, ?)) { return true; }
1309 }
1310 }
1311 case else { /* full or half-rate */
1312 select (rsl_chan_nr) {
1313 case (t_RslChanNr_Bm(?)) { return true; }
1314 case (t_RslChanNr_Lm(?, ?)) { return true; }
1315 }
1316 }
1317 }
Harald Weltec1a2fff2017-12-17 11:06:19 +01001318 }
Harald Welte211219e2018-01-29 22:03:36 +01001319 }
1320 return false;
1321}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001322
Philipp Maier8ef527e2018-05-18 11:12:50 +02001323/* Determine if the channel mode specified within rsl_chan_nr requires a
1324 * MODE MODIFY in to match the channel mode specified by given BSSMAP
1325 * ChannelType */
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001326function f_channel_needs_modify(TELNETasp_PT vty, BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
Philipp Maier8ef527e2018-05-18 11:12:50 +02001327return boolean {
1328
Philipp Maier8ef527e2018-05-18 11:12:50 +02001329 var boolean current_signalling := false;
1330 var boolean desired_signalling := false;
1331
1332 select (rsl_chan_nr) {
1333 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
1334 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001335 case (t_RslChanNr_Bm(?)) {
1336 /* TCH/F, always subslot 0 */
1337 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn) & " 0");
1338 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1339 current_signalling := true;
1340 }
1341 }
1342 case (t_RslChanNr_Lm(?, ?)) {
1343 /* TCH/H */
1344 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn)
1345 & " " & int2str(rsl_chan_nr.u.lm.sub_chan));
1346 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1347 current_signalling := true;
1348 }
1349 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001350 }
1351
1352 if (bssmap.speechOrDataIndicator == '0011'B) {
1353 desired_signalling := true;
1354 }
1355
1356 if (current_signalling == desired_signalling) {
1357 /* The desired channel mode is equal to the one we currently
1358 * have, there is no mode modification needed or expected */
1359 return false;
1360 } else {
1361 /* The desired channel mode and the current channel mode do
1362 * not match. A mode modification is required */
1363 return true;
1364 }
1365}
1366
Harald Weltecc0b0142018-05-29 15:19:33 +02001367/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
1368function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
1369 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
1370 if (istemplatekind(ass_tpl, "omit")) {
1371 return;
1372 }
1373 if (ispresent(g_pars.lcls.gcr)) {
1374 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
1375 }
1376 if (ispresent(g_pars.lcls.cfg)) {
1377 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1378 }
1379 if (ispresent(g_pars.lcls.csc)) {
1380 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1381 }
Harald Welte343b7742018-06-05 23:41:37 +02001382 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1383 if (ispresent(g_pars.lcls.exp_sts)) {
1384 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1385 } else {
1386 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1387 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001388 }
1389}
1390
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001391/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1392function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1393 template MgcpResponse resp_any := ?
1394 var MgcpResponse resp_val;
1395 resp.line.trans_id := cmd.line.trans_id;
1396 timer T := 5.0;
1397
1398 BSSAP.send(cmd);
1399 T.start;
1400 alt {
1401 [] as_Media_mgw();
1402 [] BSSAP.receive(resp) -> value resp_val { }
1403 [] BSSAP.receive(resp_any) {
1404 setverdict(fail, "Response didn't match template");
1405 mtc.stop;
1406 }
1407 [] BSSAP.receive { repeat; }
1408 [] T.timeout {
1409 setverdict(fail, "Timeout waiting for response to ", cmd);
1410 mtc.stop;
1411 }
1412 }
1413 T.stop;
1414}
1415
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001416/* Helper function to check if the activity on the MGCP matches what we
1417 * expected */
1418function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1419 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001420 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1421 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1422 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1423
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001424 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001425 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "unexpected number of MGW-CRCX transactions on g_media.mgcp_conn[" & int2str(i) & "]");
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001426 }
1427 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001428 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "unexpected number of MGW-MDCX transactions on g_media.mgcp_conn[" & int2str(i) & "]");
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001429 }
1430 }
1431}
1432
Harald Welte211219e2018-01-29 22:03:36 +01001433/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001434function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1435runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001436
1437 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001438 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001439
Philipp Maier4ce978c2020-08-11 22:54:07 +02001440 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001441 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1442 } else {
1443 /* Make sure a meaningful default is assigned in case the
1444 * codecList is not populated */
1445 codecType := FR_AMR;
1446 }
1447
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001448 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001449
Harald Weltecc0b0142018-05-29 15:19:33 +02001450 /* patch in the LCLS related items, as needed */
1451 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1452
Harald Welte7a14fd52018-05-10 22:26:55 +02001453 f_create_chan_and_exp();
Harald Welte7a14fd52018-05-10 22:26:55 +02001454
1455 /* start ciphering, if requested */
1456 if (ispresent(g_pars.encr)) {
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001457 f_cipher_mode(g_pars.encr);
Harald Welte7a14fd52018-05-10 22:26:55 +02001458 }
1459
1460 /* bail out early if no assignment requested */
1461 if (istemplatekind(ass_tpl, "omit")) {
1462 return;
1463 }
1464
1465 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001466 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001467 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001468 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001469 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001470
Harald Welte211219e2018-01-29 22:03:36 +01001471 var ExpectCriteria mgcpcrit := {
1472 connid := omit,
1473 endpoint := omit,
1474 transid := omit
1475 };
1476 var AssignmentState st := valueof(ts_AssignmentStateInit);
Oliver Smithce2dd692023-04-17 10:25:50 +02001477 /* if the channel type is SIGNAL, we're not handling an rtp stream */
Harald Welte211219e2018-01-29 22:03:36 +01001478 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
Oliver Smithce2dd692023-04-17 10:25:50 +02001479 st.rtp_stream := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001480 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001481 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001482
Harald Welte211219e2018-01-29 22:03:36 +01001483 /* determine if the current channel can support the given service or not */
1484 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1485 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001486
1487 /* We decided to assign a new channel, so we do not expect
1488 * any mode modify messages on RSL */
1489 exp_modify := false;
1490 } else {
1491 /* We will continue working with the currently assigned
1492 * channel, we must now check if the mode of the current
1493 * channel is compatible. If not we expect the BSC to modify
1494 * the mode */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001495 st.is_assignment := false;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001496 exp_modify := f_channel_needs_modify(BSCVTY, ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001497 }
1498
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001499 /* Some test situations will involve MGCP transactions on a media
1500 * gateway. Depending on the situation, we set up how many of each MGCP
1501 * message type are expected to be exchanged. */
1502 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1503 /* For tests that expect the assignment to fail, assume that
1504 * there will be no MGW communication as well. */
1505 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1506 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1507 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1508 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Oliver Smithce2dd692023-04-17 10:25:50 +02001509 } else if (st.rtp_stream) {
1510 /* For RTP streams we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001511 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1512 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1513 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1514 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001515 }
1516
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001517 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1518 * a channel and send us RR Assignment Command together with ip.access CRCX.
1519 * There is a risk that the RSL Emulation component would dequeue and process
1520 * ip.access CRCX faster than we process the Assignment Command and register
1521 * the corresponding handler for the indicated RSL channel number. This would
1522 * result in a failure, because at that moment there will be no handler for
1523 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1524 f_rslem_dchan_queue_enable();
1525
Harald Welte211219e2018-01-29 22:03:36 +01001526 f_create_mgcp_expect(mgcpcrit);
1527 BSSAP.send(ass_cmd);
1528
1529 T.start;
1530 alt {
1531 /* assignment related bits */
1532 [st.is_assignment] as_assignment(st);
1533
1534 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001535 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001536
Oliver Smithce2dd692023-04-17 10:25:50 +02001537 /* RTP stream related bits (IPA CRCX/MDCX + MGCP) */
1538 [st.rtp_stream] as_Media();
Harald Welte211219e2018-01-29 22:03:36 +01001539
1540 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001541 [st.is_assignment and st.assignment_done or (not st.is_assignment and (st.modify_done or not exp_modify))] BSSAP.receive(exp_ass_cpl) -> value bssap {
Harald Welte211219e2018-01-29 22:03:36 +01001542 setverdict(pass);
1543 }
Philipp Maier86f39202018-02-07 14:40:09 +01001544 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1545 setverdict(pass);
1546 }
Harald Welte21583082018-01-29 22:28:26 +01001547 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001548 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001549 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001550 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001551 }
1552 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001553 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001554 }
1555 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001556 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001557 }
1558 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001559 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001560 }
1561 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001562 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001563 }
1564 }
Harald Welte211219e2018-01-29 22:03:36 +01001565 log("g_media ", g_media);
1566 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001567 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001568 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001569
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001570 if (exp_modify) {
1571 /* Verify that the RR Channel Mode Modify and RSL MODE MODIFY message asked for the expected channel
1572 * mode. */
1573 /* TODO: more precisely expect the different types of speech? */
1574 var OCT1 rr_channel_mode := st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelMode.mode;
1575
Oliver Smithce2dd692023-04-17 10:25:50 +02001576 if (st.rtp_stream and rr_channel_mode == '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001577 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1578 " to a speech mode, but got channelMode == ", rr_channel_mode);
1579 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001580 } else if (not st.rtp_stream and rr_channel_mode != '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001581 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1582 " to signalling mode, but got channelMode == ", rr_channel_mode);
1583 mtc.stop;
1584 }
1585
1586 var RSL_IE_Body chan_mode_ie;
1587 if (not f_rsl_find_ie(st.rsl_mode_modify_msg, RSL_IE_CHAN_MODE, chan_mode_ie)) {
1588 setverdict(fail, "RSL MODE MODIFY message lacks a Channel Mode IE");
1589 mtc.stop;
1590 }
1591 var RSL_SpeechDataInd rsl_spd_ind := chan_mode_ie.chan_mode.spd_ind;
Oliver Smithce2dd692023-04-17 10:25:50 +02001592 if (st.rtp_stream and rsl_spd_ind != RSL_SPDI_SPEECH) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001593 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1594 " to a speech mode, but got spd_ind == ", rsl_spd_ind);
1595 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001596 } else if (not st.rtp_stream and rsl_spd_ind != RSL_SPDI_SIGN) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001597 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1598 " to signalling mode, but got spd_ind == ", rsl_spd_ind);
1599 mtc.stop;
1600 }
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001601
1602 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1603 var uint3_t got_tsc := rr_chan_desc_tsc(st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelDescription);
1604 if (not match(got_tsc, g_pars.expect_tsc)) {
1605 setverdict(fail, "RR Channel Mode Modify: unexpected TSC in Channel Description: expected ",
1606 g_pars.expect_tsc, " got ", got_tsc);
1607 mtc.stop;
1608 }
1609 }
1610
1611 } else {
1612 /* not exp_modify, so this did a Channel Activate */
1613
1614 /* Check the TSC */
1615 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1616 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
1617 var RSL_IE_Body ie;
1618 if (f_rsl_find_ie(chan_act, RSL_IE_CHAN_IDENT, ie)) {
1619 var uint3_t got_tsc := ie.chan_ident.ch_desc.v.tsc;
1620 if (not match(got_tsc, g_pars.expect_tsc)) {
1621 setverdict(fail, "RSL CHANnel ACTIVation: unexpected TSC in Channel Description: expected ",
1622 g_pars.expect_tsc, " got ", got_tsc);
1623 mtc.stop;
1624 }
1625 }
1626 }
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001627 }
1628
Philipp Maiera0976e92018-07-16 15:19:42 +02001629 /* When the BSC detects that LCLS is possible it will cross the
1630 * connetions that point to the PBX side of the MGW. In our case this
1631 * is mgcp_conn[1]. The BSC performs this operation already before the
1632 * assignment complete is generated. This means we expect another MDCX
1633 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001634 if (g_pars.lcls.adjust_cx_exp and ispresent(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue)) {
Philipp Maiera0976e92018-07-16 15:19:42 +02001635 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1636 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1637
1638 }
1639 }
1640
Oliver Smithce2dd692023-04-17 10:25:50 +02001641 if (not exp_fail and st.rtp_stream and not g_pars.aoip) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001642 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1643 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1644 connection. BSC will forward it to its MGW. */
1645 var template MgcpCommand cmd;
1646 var template MgcpResponse resp;
1647 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1648 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1649 var MgcpCallId call_id := '51234'H;
1650 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +02001651 if (g_pars.use_osmux_cn) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001652 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1653 resp := tr_CRCX_ACK_osmux;
1654 } else {
1655 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1656 resp := tr_CRCX_ACK;
1657 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001658 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001659 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1660 { valueof(ts_SDP_ptime(20)) });
1661 mgcp_transceive_mgw(cmd, resp);
1662 }
1663
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001664 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001665
1666 if (st.is_assignment and st.assignment_done) {
1667 if (not st.old_lchan_deact_sacch_seen) {
1668 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1669 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1670 }
1671 if (st.old_lchan_rll_rel_req_seen) {
1672 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1673 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1674 }
1675 }
Neels Hofmeyr9d686302021-04-25 19:02:29 +00001676
1677 var charstring lchan_info := f_vty_transceive_ret(BSCVTY, "show lchan 0 0 " & int2str(g_chan_nr.tn) & " 0");
1678 if (f_strstr(lchan_info, "State: ESTABLISHED") < 0) {
1679 log("after f_establish_fully(), 'show lchan' replied: ", lchan_info);
1680 setverdict(fail, "lchan is not in state ESTABLISHED");
1681 mtc.stop;
1682 }
Harald Welte930d0a72018-03-22 22:08:40 +01001683}
1684
Harald Welte261af4b2018-02-12 21:20:39 +01001685type record HandoverState {
1686 /* Assignment related bits */
1687 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001688 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001689 boolean handover_done,
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001690 RslChannelNr old_chan_nr,
1691 uint3_t expect_target_tsc optional
Harald Welte261af4b2018-02-12 21:20:39 +01001692};
1693
1694altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1695 var RSL_Message rsl;
1696 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1697 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1698 log("Rx L3 from net: ", l3);
1699 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1700 var RslChannelNr new_chan_nr;
1701 var GsmArfcn arfcn;
1702 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1703 new_chan_nr, arfcn);
1704 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1705
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001706 /* Verify correct TSC in handoverCommand */
1707 if (ispresent(st.expect_target_tsc)) {
1708 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.handoverCommand.channelDescription2);
1709 if (not match(got_tsc, st.expect_target_tsc)) {
1710 setverdict(fail, "RR Handover Command: unexpected TSC in Channel Description: expected ",
1711 st.expect_target_tsc, " got ", got_tsc);
1712 mtc.stop;
1713 } else {
1714 log("handoverCommand: verified TSC = ", got_tsc, " (matches ",
1715 st.expect_target_tsc, ")");
1716 }
1717 }
1718
Harald Welte261af4b2018-02-12 21:20:39 +01001719 /* register our component for this channel number at the RSL Emulation */
1720 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1721
1722 /* resume processing of RSL DChan messages, which was temporarily suspended
1723 * before performing a hand-over */
1724 f_rslem_resume(RSL1_PROC);
1725
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001726 /* send handover detect */
1727 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1728 f_sleep(0.3);
1729
Harald Welte261af4b2018-02-12 21:20:39 +01001730 /* send handover complete over the new channel */
1731 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001732 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001733 enc_PDU_ML3_MS_NW(l3_tx)));
1734 /* by default, send via the new channel from now */
1735 st.old_chan_nr := g_chan_nr;
1736 g_chan_nr := new_chan_nr;
1737 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001738
1739 /* Memorize how many MDCX we have seen before. We need this number to detect
1740 * that we have received the handover related MDCX */
1741 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001742 repeat;
1743 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001744 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001745 }
1746 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001747 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001748 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001749 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1750 repeat;
1751 }
Harald Welte261af4b2018-02-12 21:20:39 +01001752 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1753 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1754 repeat;
1755 }
1756 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1757 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1758 /* unregister for old channel number in RSL emulation */
1759 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1760 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001761
1762 /* The channel release must not necessarly be synchronized to the RSL handover
1763 * procedure since those events may happen independently nearly at the same
1764 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1765 * IPACC or MGCP level may be still in progress. In order to make sure that
1766 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001767 * as well as an MDCX on IPACC level.
1768 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1769 if (g_media.bts.ipa_crcx_seen
1770 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1771 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001772 repeat;
1773 } else {
1774 st.handover_done := true;
1775 }
Harald Welte261af4b2018-02-12 21:20:39 +01001776 }
1777}
1778
1779
Harald Weltec1a2fff2017-12-17 11:06:19 +01001780
Harald Welte28d943e2017-11-25 15:00:50 +01001781}