blob: 269fb795bab662b5fb7ef977350ef29fba451e2c [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,
Andreas Eversberga29b2f32023-07-20 17:28:41 +0200763 boolean vgcs_uplink_reject,
Andreas Eversberg35874922023-07-07 13:44:04 +0200764 boolean vgcs_uplink_seized,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200765 boolean vgcs_uplink_release,
766 boolean delay_bts,
767 boolean delay_msc
Harald Welte801ed1a2023-04-30 15:07:00 +1000768};
769
Harald Weltec1a2fff2017-12-17 11:06:19 +0100770type record TestHdlrParams {
771 OCT1 ra,
772 GsmFrameNumber fn,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100773 hexstring imsi optional,
774 hexstring imei optional,
Harald Welte60aa5762018-03-21 19:33:13 +0100775 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200776 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200777 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200778 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100779 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200780 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100781 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200782 SCCP_PAR_Address sccp_addr_msc optional,
783 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100784 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100785 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200786 boolean aoip,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200787 boolean use_osmux_cn,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200788 boolean use_osmux_bts,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200789 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200790 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200791 integer mgwpool_idx, /* MGCP_Emulation_CT (vc_MGCP) to use for this MSC ConnHdlr */
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200792 boolean media_mgw_offer_ipv6,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200793 OCT3 last_used_eutran_plmn optional,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200794 boolean exp_fast_return, /* RR Release expected to contain CellSelectInd ? */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200795 boolean expect_channel_mode_modify,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100796 uint3_t expect_tsc optional,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100797 BSSMAP_IE_CellIdentifier cell_id_source,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100798 boolean expect_ho_fail,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100799 boolean expect_ho_fail_lchan_est,
800 boolean inter_bsc_ho_in__ho_req_in_initial_sccp_cr,
Oliver Smith735b47c2023-02-15 16:03:54 +0100801 boolean ignore_mgw_mdcx,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100802 boolean fail_on_dlcx,
Harald Welte801ed1a2023-04-30 15:07:00 +1000803 boolean ignore_ipa_media,
804 ASCITest asci_test
Harald Weltec1a2fff2017-12-17 11:06:19 +0100805};
806
Philipp Maier48604732018-10-09 15:00:37 +0200807/* Note: Do not use valueof() to get a value of this template, use
808 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100809 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100810template (value) TestHdlrParams t_def_TestHdlrPars := {
811 ra := '23'O,
812 fn := 23,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100813 imsi := omit, /* set to random in f_gen_test_hdlr_pars() */
814 imei := omit, /* set to random in f_gen_test_hdlr_pars() */
Harald Welte60aa5762018-03-21 19:33:13 +0100815 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200816 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200817 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200818 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100819 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200820 encr := omit,
821 lcls := {
822 gcr := omit,
823 cfg := omit,
824 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100825 exp_sts := omit,
826 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100827 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200828 sccp_addr_msc := omit,
829 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100830 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100831 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200832 aoip := true,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200833 use_osmux_cn := false,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200834 use_osmux_bts := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200835 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200836 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200837 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200838 rsl_idx := 0,
839 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200840 },
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200841 mgwpool_idx := 0,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200842 media_mgw_offer_ipv6 := true,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200843 last_used_eutran_plmn := omit,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200844 exp_fast_return := false,
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200845 expect_channel_mode_modify := false,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100846 expect_tsc := omit,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100847 cell_id_source := valueof(ts_CellID_LAC_CI(1, 1)),
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100848 expect_ho_fail := false,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100849 expect_ho_fail_lchan_est := false,
850 inter_bsc_ho_in__ho_req_in_initial_sccp_cr := true,
Oliver Smith735b47c2023-02-15 16:03:54 +0100851 ignore_mgw_mdcx := false,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100852 fail_on_dlcx := true,
Harald Welte801ed1a2023-04-30 15:07:00 +1000853 ignore_ipa_media := false,
854 asci_test := {
855 vgcs_setup_ok := false,
856 vgcs_assign_ok := false,
857 vgcs_assign_fail := false,
858 vgcs_talker_req := false,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200859 vgcs_talker_fail := false,
Harald Welte801ed1a2023-04-30 15:07:00 +1000860 vgcs_talker_est := false,
Andreas Eversberg35874922023-07-07 13:44:04 +0200861 vgcs_talker_rel := false,
Andreas Eversberga29b2f32023-07-20 17:28:41 +0200862 vgcs_uplink_reject := false,
Andreas Eversberg35874922023-07-07 13:44:04 +0200863 vgcs_uplink_seized := false,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200864 vgcs_uplink_release := false,
865 delay_bts := false,
866 delay_msc := false
Harald Welte801ed1a2023-04-30 15:07:00 +1000867 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100868}
869
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300870function f_create_chan_and_exp(template (present) PDU_BSSAP exp_l3_compl := ?)
871runs on MSC_ConnHdlr {
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100872 var MobileIdentityLV mi;
873 var PDU_ML3_MS_NW l3_info;
874 var octetstring l3_enc;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200875 var template uint3_t tsc := ?;
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300876 timer T;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200877
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100878 if (ispresent(g_pars.imsi)) {
879 mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
880 } else if (ispresent(g_pars.imei)) {
881 mi := valueof(ts_MI_IMEI_LV(g_pars.imei));
882 } else {
883 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
884 "Either imsi or imei must be set!");
885 }
886 l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
887 l3_enc := enc_PDU_ML3_MS_NW(l3_info);
888
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200889 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
890 tsc := g_pars.expect_tsc;
891 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100892
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300893 if (istemplatekind(exp_l3_compl, "?")) {
894 if (g_pars.aoip == false) {
895 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := omit);
896 } else {
897 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := ?);
898 }
899 }
900
Neels Hofmeyrd601f9c2021-06-04 22:46:06 +0200901 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100902 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200903 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 +0300904 /* wait for a COMPL_L3 from the BSC to ensure that the SCCP connection is up */
905 T.start(2.0);
906 alt {
907 [] BSSAP.receive(exp_l3_compl);
908 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
909 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
910 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
911 }
912 [] T.timeout {
913 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
914 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
915 }
916 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100917}
918
Harald Welte898113b2018-01-31 18:32:21 +0100919function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200920 template (omit) RslChannelNr chan_nr := omit, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Welte898113b2018-01-31 18:32:21 +0100921 if (not isvalue(link_id)) {
922 link_id := ts_RslLinkID_DCCH(0);
923 }
924 if (not isvalue(chan_nr)) {
925 chan_nr := g_chan_nr;
926 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200927 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 +0100928}
929
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200930function 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 +0100931 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100932 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100933 if (orig.msg_type == RSL_MT_ENCR_CMD) {
934 link_id := orig.ies[2].body.link_id;
935 } else {
936 link_id := orig.ies[1].body.link_id;
937 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200938 f_rsl_send_l3(l3, link_id, chan_nr, rsl_pt := rsl_pt);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100939}
940
Oliver Smith598e1ed2021-07-09 10:28:40 +0200941/* Convert the cipher representation on BSSMAP to the representation used on RSL */
942function f_cipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100943{
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300944 select (alg_bssmap) {
945 case ('01'O) { return RSL_ALG_ID_A5_0; }
946 case ('02'O) { return RSL_ALG_ID_A5_1; }
947 case ('04'O) { return RSL_ALG_ID_A5_2; }
948 case ('08'O) { return RSL_ALG_ID_A5_3; }
949 case ('10'O) { return RSL_ALG_ID_A5_4; }
950 case ('20'O) { return RSL_ALG_ID_A5_5; }
951 case ('40'O) { return RSL_ALG_ID_A5_6; }
952 case ('80'O) { return RSL_ALG_ID_A5_7; }
953 case else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100954 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
955 oct2str(alg_bssmap));
Harald Weltee613f962018-04-18 22:38:16 +0200956 return RSL_ALG_ID_A5_0;
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300957 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100958 }
959}
960
Vadim Yanitskiyaf2972e2022-03-14 22:27:57 +0300961/* Convert the cipher representation on BSSMAP to the one used on RR (3GPP TS 44.018) */
962function f_cipher_mode_bssmap_to_rr(OCT1 alg_bssmap) return BIT3
963{
964 select (alg_bssmap) {
965 case ('01'O) /* A5/0 */ { return '000'B; } /* SC=0 */
966 case ('02'O) /* A5/1 */ { return '000'B; } /* SC=1 */
967 case ('04'O) /* A5/2 */ { return '001'B; } /* SC=1 */
968 case ('08'O) /* A5/3 */ { return '010'B; } /* SC=1 */
969 case ('10'O) /* A5/4 */ { return '011'B; } /* SC=1 */
970 case ('20'O) /* A5/5 */ { return '100'B; } /* SC=1 */
971 case ('40'O) /* A5/6 */ { return '101'B; } /* SC=1 */
972 case ('80'O) /* A5/7 */ { return '110'B; } /* SC=1 */
973 case else {
974 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
975 oct2str(alg_bssmap));
976 return '000'B;
977 }
978 }
979}
980
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200981function f_verify_encr_info(RSL_Message rsl) runs on MSC_ConnHdlr {
982 var RSL_IE_Body encr_info;
983 var RSL_AlgId alg_rsl;
984 var template octetstring expect_kc;
985
986 /* If no encryption is enabled, then make sure there is no RSL_IE_ENCR_INFO */
987 if (not ispresent(g_pars.encr)) {
988 if (f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
989 setverdict(fail, "Found Encryption IE, but expected no encryption in ", rsl.msg_type);
990 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
991 return;
992 }
993 setverdict(pass);
994 return;
995 }
996
997 /* RSL uses a different representation of the encryption algorithm,
998 * so we need to convert first */
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100999 alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +02001000
1001 if (alg_rsl == RSL_ALG_ID_A5_4 and ispresent(g_pars.encr.enc_kc128)) {
1002 expect_kc := g_pars.encr.enc_kc128;
1003 } else if (alg_rsl == RSL_ALG_ID_A5_0) {
1004 /* When A5/0 is chosen, no encryption is active, so technically, no key is needed. However, 3GPP TS
1005 * 48.058 9.3.7 Encryption Information stays quite silent about presence or absence of a key for A5/0.
1006 * The only thing specified is how to indicate the length of the key; the possibility that the key may
1007 * be zero length is not explicitly mentioned. So it seems that we should always send the key along,
1008 * even for A5/0. Still, let's also allow a zero length key for A5/0. */
1009 expect_kc := (g_pars.encr.enc_key, ''O);
1010 } else {
1011 expect_kc := g_pars.encr.enc_key;
1012 }
1013 log("for encryption algo ", alg_rsl, " expect kc = ", expect_kc);
1014
1015 if (not f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
1016 if (alg_rsl == RSL_ALG_ID_A5_0) {
1017 /* For A5/0, encryption is not active. It is fine to omit the Encryption Information in this
1018 * case. Note that the first channel may see an RSL Encryption Command with A5/0 indicated, and
1019 * a subsequent handover may activate a new channel without any Encryption Information. */
1020 setverdict(pass);
1021 return;
1022 }
1023 setverdict(fail, "Missing Encryption Information IE in ", rsl.msg_type);
1024 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1025 return;
1026 }
1027
1028 if (not match(encr_info, tr_EncrInfo(alg_rsl, expect_kc))) {
1029 setverdict(fail, "Unexpected Kc in Encryption Information IE in ", rsl.msg_type);
1030 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1031 return;
1032 }
1033 setverdict(pass);
1034}
1035
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001036function f_cipher_mode(TestHdlrEncrParams enc, boolean exp_fail := false,
1037 RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC)
Harald Welte38b2a102017-12-23 02:42:58 +01001038runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +01001039 var PDU_BSSAP bssap;
1040 var RSL_Message rsl;
1041
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001042 if (isvalue(enc.enc_kc128)) {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001043 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(enc.enc_alg_permitted, enc.enc_key, valueof(enc.enc_kc128)));
Harald Welte73cd2712017-12-17 00:44:52 +01001044 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001045 BSSAP.send(ts_BSSMAP_CipherModeCmd(enc.enc_alg_permitted, enc.enc_key));
Harald Welte73cd2712017-12-17 00:44:52 +01001046 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +01001047
Harald Welte73cd2712017-12-17 00:44:52 +01001048 alt {
1049 /* RSL/UE Side */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001050 [] rsl_pt.receive(tr_RSL_ENCR_CMD(g_chan_nr)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +01001051 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
1052 log("Rx L3 from net: ", l3);
Neels Hofmeyr72749342021-06-11 02:41:37 +02001053
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +02001054 f_verify_encr_info(rsl);
1055
Harald Welte73cd2712017-12-17 00:44:52 +01001056 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001057 f_rsl_reply(ts_RRM_CiphModeCompl, rsl, rsl_pt := rsl_pt);
Harald Welte73cd2712017-12-17 00:44:52 +01001058 }
1059 repeat;
1060 }
1061 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001062 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001063 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +01001064 } else {
1065 setverdict(pass);
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001066 var RSL_AlgId alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr5e31cfc2021-06-11 02:42:11 +02001067 if (oct2int(bssap.pdu.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algorithmIdentifier) != enum2int(alg_rsl)) {
1068 setverdict(fail, "Unexpected Encryption Algorithm ID in BSSMAP Cipher Mode Complete");
1069 }
Harald Welte38b2a102017-12-23 02:42:58 +01001070 }
Harald Welte73cd2712017-12-17 00:44:52 +01001071 }
1072 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001073 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001074 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +01001075 } else {
1076 setverdict(pass);
1077 }
Harald Welte73cd2712017-12-17 00:44:52 +01001078 }
1079 }
1080}
1081
Harald Welte211219e2018-01-29 22:03:36 +01001082/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
1083function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
1084 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +01001085 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +01001086
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001087 select (inp) {
1088 case ('00001'B) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +01001089 chan_nr := valueof(t_RslChanNr_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001090 }
1091 case ('11101'B) { /* VAMOS TCH/F */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001092 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001093 }
1094 case ('0001?'B) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +01001095 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001096 }
1097 case ('1111?'B) { /* VAMOS TCH/H */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001098 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001099 }
1100 case ('001??'B) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001101 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001102 }
1103 case ('01???'B) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001104 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001105 }
1106 case else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001107 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001108 }
Harald Welte211219e2018-01-29 22:03:36 +01001109 }
1110
1111 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001112 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +01001113 } else {
1114 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
1115 arfcn := oct2int(concat);
1116 }
1117}
1118
1119type record AssignmentState {
1120 /* global */
Oliver Smithce2dd692023-04-17 10:25:50 +02001121 boolean rtp_stream,
Harald Welte211219e2018-01-29 22:03:36 +01001122 boolean is_assignment,
1123 /* Assignment related bits */
1124 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001125 boolean old_lchan_deact_sacch_seen,
1126 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +01001127 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +01001128 RslChannelNr old_chan_nr,
1129 /* Modify related bits */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001130 PDU_ML3_NW_MS rr_channel_mode_modify_msg optional,
Harald Welte211219e2018-01-29 22:03:36 +01001131 boolean rr_modify_seen,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001132 RSL_Message rsl_mode_modify_msg optional,
Harald Welte21583082018-01-29 22:28:26 +01001133 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +01001134}
1135
1136template (value) AssignmentState ts_AssignmentStateInit := {
Oliver Smithce2dd692023-04-17 10:25:50 +02001137 rtp_stream := false,
Harald Welte211219e2018-01-29 22:03:36 +01001138 is_assignment := false,
1139 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001140 old_lchan_deact_sacch_seen := false,
1141 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +01001142 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +01001143 old_chan_nr := -,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001144 rr_channel_mode_modify_msg := omit,
Harald Welte211219e2018-01-29 22:03:36 +01001145 rr_modify_seen := false,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001146 rsl_mode_modify_msg := omit,
Harald Welte21583082018-01-29 22:28:26 +01001147 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +01001148}
1149
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001150private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
1151 encr_info := {
1152 len := ?,
1153 alg_id := alg,
1154 key := key
1155 }
1156}
1157
1158/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
1159private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
1160 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001161 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001162 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001163
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001164 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg_permitted != '01'O) {
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001165 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001166 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001167 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001168 var RSL_AlgId alg := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr0588cad2021-06-11 01:38:18 +02001169 var octetstring expect_key;
1170 if (alg == RSL_ALG_ID_A5_4) {
1171 expect_key := g_pars.encr.enc_kc128;
1172 } else {
1173 expect_key := g_pars.encr.enc_key;
1174 }
1175 if (not match(encr_info, tr_EncrInfo(alg, expect_key))) {
1176 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
1177 "Unexpected Kc in Encryption IE in RSL ENCR CMD");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001178 }
1179 }
1180 } else {
1181 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
1182 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001183 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001184 }
1185 }
1186 }
1187 /* 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 +01001188 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001189
Pau Espin Pedrol8f773632019-11-05 11:46:53 +01001190 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 +01001191 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
1192 }
1193
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001194 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
1195 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
1196 } else {
1197 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 +01001198 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 +01001199 }
1200 }
1201
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001202}
1203
Neels Hofmeyr6a955cc2021-10-02 14:53:48 +02001204function rr_chan_desc_tsc(ChannelDescription2_V cd2)
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001205 return uint3_t
1206{
1207 var uint3_t tsc := oct2int(cd2.octet3);
1208 tsc := tsc / 32; /* shl 5 */
1209 return tsc;
1210}
1211
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001212altstep 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 +01001213 var RSL_Message rsl;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001214 [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 +01001215 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1216 log("Rx L3 from net: ", l3);
1217 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
1218 var RslChannelNr new_chan_nr;
1219 var GsmArfcn arfcn;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001220
1221 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1222 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime);
1223 if (not match(got_tsc, g_pars.expect_tsc)) {
1224 setverdict(fail, "RR Assignment: unexpected TSC in Channel Description: expected ",
1225 g_pars.expect_tsc, " got ", got_tsc);
1226 mtc.stop;
1227 }
1228 }
1229
Harald Welte211219e2018-01-29 22:03:36 +01001230 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
1231 new_chan_nr, arfcn);
1232 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +01001233
Harald Welte211219e2018-01-29 22:03:36 +01001234 /* register our component for this channel number at the RSL Emulation */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001235 f_rslem_register(0, new_chan_nr, PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001236 /* dispatch queued messages for this channel (if any) */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001237 f_rslem_dchan_queue_dispatch(PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001238
Harald Welte211219e2018-01-29 22:03:36 +01001239 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
1240 /* send assignment complete over the new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001241 rsl_pt.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +01001242 enc_PDU_ML3_MS_NW(l3_tx)));
1243 /* by default, send via the new channel from now */
1244 st.old_chan_nr := g_chan_nr;
1245 g_chan_nr := new_chan_nr;
1246 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001247 /* obtain channel activation from RSL_Emulation for new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001248 var RSL_Message chan_act := f_rslem_get_last_act(rsl_proc_pt, 0, g_chan_nr);
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001249 /* check it (e.g. for correct ciphering parameters) */
1250 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +01001251 repeat;
1252 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001253 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +01001254 }
Harald Welte211219e2018-01-29 22:03:36 +01001255 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001256 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001257 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001258 repeat;
1259 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001260 [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 +01001261 st.old_lchan_rll_rel_req_seen := true;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001262 rsl_pt.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
Harald Welte211219e2018-01-29 22:03:36 +01001263 repeat;
1264 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001265 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1266 rsl_pt.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +01001267 /* unregister for old channel number in RSL emulation */
1268 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001269 f_rslem_unregister(0, st.old_chan_nr, PT := rsl_proc_pt);
Harald Welte21583082018-01-29 22:28:26 +01001270 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +01001271 repeat;
1272 }
1273}
1274
1275altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
1276 /* no assignment, just mode modify */
1277 var RSL_Message rsl;
1278
Oliver Smithce2dd692023-04-17 10:25:50 +02001279 [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 +01001280 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1281 log("Rx L3 from net: ", l3);
1282 if (ischosen(l3.msgs.rrm.channelModeModify)) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001283 st.rr_channel_mode_modify_msg := l3;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001284 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
1285 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +01001286 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001287 }
1288 repeat;
1289 }
Oliver Smithce2dd692023-04-17 10:25:50 +02001290 [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 +02001291 st.rsl_mode_modify_msg := rsl;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001292 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +01001293 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001294 repeat;
1295 }
Harald Welte211219e2018-01-29 22:03:36 +01001296}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001297
Harald Welte211219e2018-01-29 22:03:36 +01001298/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
1299function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
1300return boolean {
1301 select (bssmap.speechOrDataIndicator) {
1302 case ('0011'B) { /* Signalling */
1303 /* all channels support signalling */
1304 return true;
1305 }
1306 case else { /* Speech, Speech+CTM or CSD */
1307 select (bssmap.channelRateAndType) {
1308 case ('08'O) { /* TCH/F */
1309 select (rsl_chan_nr) {
1310 case (t_RslChanNr_Bm(?)) { return true; }
1311 }
1312 }
1313 case ('09'O) { /* TCH/H */
1314 select (rsl_chan_nr) {
1315 case (t_RslChanNr_Lm(?, ?)) { return true; }
1316 }
1317 }
1318 case else { /* full or half-rate */
1319 select (rsl_chan_nr) {
1320 case (t_RslChanNr_Bm(?)) { return true; }
1321 case (t_RslChanNr_Lm(?, ?)) { return true; }
1322 }
1323 }
1324 }
Harald Weltec1a2fff2017-12-17 11:06:19 +01001325 }
Harald Welte211219e2018-01-29 22:03:36 +01001326 }
1327 return false;
1328}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001329
Philipp Maier8ef527e2018-05-18 11:12:50 +02001330/* Determine if the channel mode specified within rsl_chan_nr requires a
1331 * MODE MODIFY in to match the channel mode specified by given BSSMAP
1332 * ChannelType */
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001333function f_channel_needs_modify(TELNETasp_PT vty, BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
Philipp Maier8ef527e2018-05-18 11:12:50 +02001334return boolean {
1335
Philipp Maier8ef527e2018-05-18 11:12:50 +02001336 var boolean current_signalling := false;
1337 var boolean desired_signalling := false;
1338
1339 select (rsl_chan_nr) {
1340 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
1341 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001342 case (t_RslChanNr_Bm(?)) {
1343 /* TCH/F, always subslot 0 */
1344 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn) & " 0");
1345 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1346 current_signalling := true;
1347 }
1348 }
1349 case (t_RslChanNr_Lm(?, ?)) {
1350 /* TCH/H */
1351 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn)
1352 & " " & int2str(rsl_chan_nr.u.lm.sub_chan));
1353 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1354 current_signalling := true;
1355 }
1356 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001357 }
1358
1359 if (bssmap.speechOrDataIndicator == '0011'B) {
1360 desired_signalling := true;
1361 }
1362
1363 if (current_signalling == desired_signalling) {
1364 /* The desired channel mode is equal to the one we currently
1365 * have, there is no mode modification needed or expected */
1366 return false;
1367 } else {
1368 /* The desired channel mode and the current channel mode do
1369 * not match. A mode modification is required */
1370 return true;
1371 }
1372}
1373
Harald Weltecc0b0142018-05-29 15:19:33 +02001374/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
1375function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
1376 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
1377 if (istemplatekind(ass_tpl, "omit")) {
1378 return;
1379 }
1380 if (ispresent(g_pars.lcls.gcr)) {
1381 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
1382 }
1383 if (ispresent(g_pars.lcls.cfg)) {
1384 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1385 }
1386 if (ispresent(g_pars.lcls.csc)) {
1387 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1388 }
Harald Welte343b7742018-06-05 23:41:37 +02001389 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1390 if (ispresent(g_pars.lcls.exp_sts)) {
1391 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1392 } else {
1393 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1394 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001395 }
1396}
1397
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001398/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1399function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1400 template MgcpResponse resp_any := ?
1401 var MgcpResponse resp_val;
1402 resp.line.trans_id := cmd.line.trans_id;
1403 timer T := 5.0;
1404
1405 BSSAP.send(cmd);
1406 T.start;
1407 alt {
1408 [] as_Media_mgw();
1409 [] BSSAP.receive(resp) -> value resp_val { }
1410 [] BSSAP.receive(resp_any) {
1411 setverdict(fail, "Response didn't match template");
1412 mtc.stop;
1413 }
1414 [] BSSAP.receive { repeat; }
1415 [] T.timeout {
1416 setverdict(fail, "Timeout waiting for response to ", cmd);
1417 mtc.stop;
1418 }
1419 }
1420 T.stop;
1421}
1422
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001423/* Helper function to check if the activity on the MGCP matches what we
1424 * expected */
1425function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1426 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001427 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1428 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1429 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1430
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001431 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001432 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 +02001433 }
1434 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001435 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 +02001436 }
1437 }
1438}
1439
Harald Welte211219e2018-01-29 22:03:36 +01001440/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001441function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1442runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001443
1444 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001445 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001446
Philipp Maier4ce978c2020-08-11 22:54:07 +02001447 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001448 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1449 } else {
1450 /* Make sure a meaningful default is assigned in case the
1451 * codecList is not populated */
1452 codecType := FR_AMR;
1453 }
1454
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001455 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001456
Harald Weltecc0b0142018-05-29 15:19:33 +02001457 /* patch in the LCLS related items, as needed */
1458 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1459
Harald Welte7a14fd52018-05-10 22:26:55 +02001460 f_create_chan_and_exp();
Harald Welte7a14fd52018-05-10 22:26:55 +02001461
1462 /* start ciphering, if requested */
1463 if (ispresent(g_pars.encr)) {
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001464 f_cipher_mode(g_pars.encr);
Harald Welte7a14fd52018-05-10 22:26:55 +02001465 }
1466
1467 /* bail out early if no assignment requested */
1468 if (istemplatekind(ass_tpl, "omit")) {
1469 return;
1470 }
1471
1472 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001473 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001474 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001475 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001476 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001477
Harald Welte211219e2018-01-29 22:03:36 +01001478 var ExpectCriteria mgcpcrit := {
1479 connid := omit,
1480 endpoint := omit,
1481 transid := omit
1482 };
1483 var AssignmentState st := valueof(ts_AssignmentStateInit);
Oliver Smithce2dd692023-04-17 10:25:50 +02001484 /* if the channel type is SIGNAL, we're not handling an rtp stream */
Harald Welte211219e2018-01-29 22:03:36 +01001485 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
Oliver Smithce2dd692023-04-17 10:25:50 +02001486 st.rtp_stream := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001487 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001488 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001489
Harald Welte211219e2018-01-29 22:03:36 +01001490 /* determine if the current channel can support the given service or not */
1491 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1492 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001493
1494 /* We decided to assign a new channel, so we do not expect
1495 * any mode modify messages on RSL */
1496 exp_modify := false;
1497 } else {
1498 /* We will continue working with the currently assigned
1499 * channel, we must now check if the mode of the current
1500 * channel is compatible. If not we expect the BSC to modify
1501 * the mode */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001502 st.is_assignment := false;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001503 exp_modify := f_channel_needs_modify(BSCVTY, ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001504 }
1505
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001506 /* Some test situations will involve MGCP transactions on a media
1507 * gateway. Depending on the situation, we set up how many of each MGCP
1508 * message type are expected to be exchanged. */
1509 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1510 /* For tests that expect the assignment to fail, assume that
1511 * there will be no MGW communication as well. */
1512 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1513 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1514 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1515 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Oliver Smithce2dd692023-04-17 10:25:50 +02001516 } else if (st.rtp_stream) {
1517 /* For RTP streams we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001518 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1519 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1520 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1521 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001522 }
1523
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001524 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1525 * a channel and send us RR Assignment Command together with ip.access CRCX.
1526 * There is a risk that the RSL Emulation component would dequeue and process
1527 * ip.access CRCX faster than we process the Assignment Command and register
1528 * the corresponding handler for the indicated RSL channel number. This would
1529 * result in a failure, because at that moment there will be no handler for
1530 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1531 f_rslem_dchan_queue_enable();
1532
Harald Welte211219e2018-01-29 22:03:36 +01001533 f_create_mgcp_expect(mgcpcrit);
1534 BSSAP.send(ass_cmd);
1535
1536 T.start;
1537 alt {
1538 /* assignment related bits */
1539 [st.is_assignment] as_assignment(st);
1540
1541 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001542 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001543
Oliver Smithce2dd692023-04-17 10:25:50 +02001544 /* RTP stream related bits (IPA CRCX/MDCX + MGCP) */
1545 [st.rtp_stream] as_Media();
Harald Welte211219e2018-01-29 22:03:36 +01001546
1547 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001548 [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 +01001549 setverdict(pass);
1550 }
Philipp Maier86f39202018-02-07 14:40:09 +01001551 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1552 setverdict(pass);
1553 }
Harald Welte21583082018-01-29 22:28:26 +01001554 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001555 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001556 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001557 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001558 }
1559 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001560 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001561 }
1562 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001563 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001564 }
1565 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001566 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001567 }
1568 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001569 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001570 }
1571 }
Harald Welte211219e2018-01-29 22:03:36 +01001572 log("g_media ", g_media);
1573 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001574 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001575 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001576
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001577 if (exp_modify) {
1578 /* Verify that the RR Channel Mode Modify and RSL MODE MODIFY message asked for the expected channel
1579 * mode. */
1580 /* TODO: more precisely expect the different types of speech? */
1581 var OCT1 rr_channel_mode := st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelMode.mode;
1582
Oliver Smithce2dd692023-04-17 10:25:50 +02001583 if (st.rtp_stream and rr_channel_mode == '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001584 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1585 " to a speech mode, but got channelMode == ", rr_channel_mode);
1586 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001587 } else if (not st.rtp_stream and rr_channel_mode != '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001588 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1589 " to signalling mode, but got channelMode == ", rr_channel_mode);
1590 mtc.stop;
1591 }
1592
1593 var RSL_IE_Body chan_mode_ie;
1594 if (not f_rsl_find_ie(st.rsl_mode_modify_msg, RSL_IE_CHAN_MODE, chan_mode_ie)) {
1595 setverdict(fail, "RSL MODE MODIFY message lacks a Channel Mode IE");
1596 mtc.stop;
1597 }
1598 var RSL_SpeechDataInd rsl_spd_ind := chan_mode_ie.chan_mode.spd_ind;
Oliver Smithce2dd692023-04-17 10:25:50 +02001599 if (st.rtp_stream and rsl_spd_ind != RSL_SPDI_SPEECH) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001600 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1601 " to a speech mode, but got spd_ind == ", rsl_spd_ind);
1602 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001603 } else if (not st.rtp_stream and rsl_spd_ind != RSL_SPDI_SIGN) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001604 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1605 " to signalling mode, but got spd_ind == ", rsl_spd_ind);
1606 mtc.stop;
1607 }
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001608
1609 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1610 var uint3_t got_tsc := rr_chan_desc_tsc(st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelDescription);
1611 if (not match(got_tsc, g_pars.expect_tsc)) {
1612 setverdict(fail, "RR Channel Mode Modify: unexpected TSC in Channel Description: expected ",
1613 g_pars.expect_tsc, " got ", got_tsc);
1614 mtc.stop;
1615 }
1616 }
1617
1618 } else {
1619 /* not exp_modify, so this did a Channel Activate */
1620
1621 /* Check the TSC */
1622 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1623 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
1624 var RSL_IE_Body ie;
1625 if (f_rsl_find_ie(chan_act, RSL_IE_CHAN_IDENT, ie)) {
1626 var uint3_t got_tsc := ie.chan_ident.ch_desc.v.tsc;
1627 if (not match(got_tsc, g_pars.expect_tsc)) {
1628 setverdict(fail, "RSL CHANnel ACTIVation: unexpected TSC in Channel Description: expected ",
1629 g_pars.expect_tsc, " got ", got_tsc);
1630 mtc.stop;
1631 }
1632 }
1633 }
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001634 }
1635
Philipp Maiera0976e92018-07-16 15:19:42 +02001636 /* When the BSC detects that LCLS is possible it will cross the
1637 * connetions that point to the PBX side of the MGW. In our case this
1638 * is mgcp_conn[1]. The BSC performs this operation already before the
1639 * assignment complete is generated. This means we expect another MDCX
1640 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001641 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 +02001642 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1643 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1644
1645 }
1646 }
1647
Oliver Smithce2dd692023-04-17 10:25:50 +02001648 if (not exp_fail and st.rtp_stream and not g_pars.aoip) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001649 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1650 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1651 connection. BSC will forward it to its MGW. */
1652 var template MgcpCommand cmd;
1653 var template MgcpResponse resp;
1654 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1655 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1656 var MgcpCallId call_id := '51234'H;
1657 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +02001658 if (g_pars.use_osmux_cn) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001659 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1660 resp := tr_CRCX_ACK_osmux;
1661 } else {
1662 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1663 resp := tr_CRCX_ACK;
1664 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001665 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001666 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1667 { valueof(ts_SDP_ptime(20)) });
1668 mgcp_transceive_mgw(cmd, resp);
1669 }
1670
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001671 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001672
1673 if (st.is_assignment and st.assignment_done) {
1674 if (not st.old_lchan_deact_sacch_seen) {
1675 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1676 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1677 }
1678 if (st.old_lchan_rll_rel_req_seen) {
1679 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1680 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1681 }
1682 }
Neels Hofmeyr9d686302021-04-25 19:02:29 +00001683
1684 var charstring lchan_info := f_vty_transceive_ret(BSCVTY, "show lchan 0 0 " & int2str(g_chan_nr.tn) & " 0");
1685 if (f_strstr(lchan_info, "State: ESTABLISHED") < 0) {
1686 log("after f_establish_fully(), 'show lchan' replied: ", lchan_info);
1687 setverdict(fail, "lchan is not in state ESTABLISHED");
1688 mtc.stop;
1689 }
Harald Welte930d0a72018-03-22 22:08:40 +01001690}
1691
Harald Welte261af4b2018-02-12 21:20:39 +01001692type record HandoverState {
1693 /* Assignment related bits */
1694 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001695 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001696 boolean handover_done,
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001697 RslChannelNr old_chan_nr,
1698 uint3_t expect_target_tsc optional
Harald Welte261af4b2018-02-12 21:20:39 +01001699};
1700
1701altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1702 var RSL_Message rsl;
1703 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1704 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1705 log("Rx L3 from net: ", l3);
1706 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1707 var RslChannelNr new_chan_nr;
1708 var GsmArfcn arfcn;
1709 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1710 new_chan_nr, arfcn);
1711 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1712
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001713 /* Verify correct TSC in handoverCommand */
1714 if (ispresent(st.expect_target_tsc)) {
1715 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.handoverCommand.channelDescription2);
1716 if (not match(got_tsc, st.expect_target_tsc)) {
1717 setverdict(fail, "RR Handover Command: unexpected TSC in Channel Description: expected ",
1718 st.expect_target_tsc, " got ", got_tsc);
1719 mtc.stop;
1720 } else {
1721 log("handoverCommand: verified TSC = ", got_tsc, " (matches ",
1722 st.expect_target_tsc, ")");
1723 }
1724 }
1725
Harald Welte261af4b2018-02-12 21:20:39 +01001726 /* register our component for this channel number at the RSL Emulation */
1727 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1728
1729 /* resume processing of RSL DChan messages, which was temporarily suspended
1730 * before performing a hand-over */
1731 f_rslem_resume(RSL1_PROC);
1732
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001733 /* send handover detect */
1734 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1735 f_sleep(0.3);
1736
Harald Welte261af4b2018-02-12 21:20:39 +01001737 /* send handover complete over the new channel */
1738 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001739 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001740 enc_PDU_ML3_MS_NW(l3_tx)));
1741 /* by default, send via the new channel from now */
1742 st.old_chan_nr := g_chan_nr;
1743 g_chan_nr := new_chan_nr;
1744 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001745
1746 /* Memorize how many MDCX we have seen before. We need this number to detect
1747 * that we have received the handover related MDCX */
1748 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001749 repeat;
1750 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001751 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001752 }
1753 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001754 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001755 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001756 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1757 repeat;
1758 }
Harald Welte261af4b2018-02-12 21:20:39 +01001759 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1760 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1761 repeat;
1762 }
1763 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1764 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1765 /* unregister for old channel number in RSL emulation */
1766 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1767 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001768
1769 /* The channel release must not necessarly be synchronized to the RSL handover
1770 * procedure since those events may happen independently nearly at the same
1771 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1772 * IPACC or MGCP level may be still in progress. In order to make sure that
1773 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001774 * as well as an MDCX on IPACC level.
1775 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1776 if (g_media.bts.ipa_crcx_seen
1777 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1778 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001779 repeat;
1780 } else {
1781 st.handover_done := true;
1782 }
Harald Welte261af4b2018-02-12 21:20:39 +01001783 }
1784}
1785
1786
Harald Weltec1a2fff2017-12-17 11:06:19 +01001787
Harald Welte28d943e2017-11-25 15:00:50 +01001788}