blob: b499244f9139ea52f0a32d51a253edcf91ec72b4 [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 {
Pau Espin Pedrol577688b2023-09-28 14:51:42 +0200584 g_trans_id := i * 1000; /* Avoid different MscConnHdlr submitting same trans_id over MGCP-IPA */
Harald Welteeddf0e92020-06-21 19:42:15 +0200585 f_MediaState_init(g_media, i, bts, mgw, codecType);
586 f_MscConnHdlr_init_vty();
587}
588
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200589private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
590 var MgcpTransId tid := int2str(g_trans_id);
591 g_trans_id := g_trans_id + 1;
592 return tid;
593}
594
Harald Welte6811d102019-04-14 22:23:14 +0200595/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100596 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
597private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200598runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100599 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200600 var boolean append_osmux_support := g_ran_ops.use_osmux and
601 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100602
Harald Weltec1a2fff2017-12-17 11:06:19 +0100603 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200604 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
605 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100606 }
607
608 return resp;
609}
610
Harald Welte47cd0e32020-08-21 12:39:11 +0200611/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
612 * BSSAP_LE message arrives. Can return a PDU_BSSAP_LE that should be sent in return */
613private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
614runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
615 var template PDU_BSSAP_LE resp := omit;
616
617 /* answer all RESET with a RESET ACK */
618 if (match(bssap, tr_BSSMAP_LE_Reset)) {
619 resp := ts_BSSMAP_LE_ResetAck;
620 }
621
622 return resp;
623}
624
Harald Welte6811d102019-04-14 22:23:14 +0200625const RanOps MSC_RanOps := {
626 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100627 unitdata_cb := refers(UnitdataCallback),
628 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100629 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200630 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200631 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200632 use_osmux := false,
Eric Wild66448c12022-04-02 14:57:53 +0200633 bssap_reset_retries := 1,
Harald Welte930d0a72018-03-22 22:08:40 +0100634 sccp_addr_local := omit,
635 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100636}
637
Harald Welte47cd0e32020-08-21 12:39:11 +0200638const BssapLeOps SMLC_BssapLeOps := {
639 create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
640 unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
641 decode_dtap := false,
642 role_ms := false,
643 sccp_addr_local := omit,
644 sccp_addr_peer := omit
645}
646
Daniel Willmann191e0d92018-01-17 12:44:35 +0100647const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100648 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
649 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100650}
651
Harald Weltec1a2fff2017-12-17 11:06:19 +0100652/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200653function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200654 RAN.call(RAN_register:{l3_enc, self}) {
655 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100656 }
657}
658
Harald Welte651fcdc2018-05-10 20:23:16 +0200659type record TestHdlrEncrParams {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100660 /* A mask of multiple encryption algorithms, for Encryption Information IE. */
661 OCT1 enc_alg_permitted,
662 /* Expect this encryption algorithm in Channel Activation from the BSC.
663 * To expect no encryption, set to '01'O == A5/0. */
664 OCT1 enc_alg_expect,
665 /* In BSSMAP, the Chosen Encryption Algorithm IE that the test sends to the BSC.
666 * When set to omit, the MSC will not send a Chosen Encryption Algorithm IE. */
667 OCT1 enc_alg_chosen optional,
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200668 octetstring enc_key,
669 octetstring enc_kc128 optional
Harald Welte651fcdc2018-05-10 20:23:16 +0200670};
671
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100672template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg_permitted,
673 octetstring key, template (omit) octetstring kc128 := omit) := {
674 enc_alg_permitted := alg_permitted,
675 enc_alg_expect := alg_permitted, /* <- for compat with tests before enc_alg_expect was added */
676 enc_alg_chosen := alg_permitted, /* <- for compat with tests before enc_alg_chosen was added */
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200677 enc_key := key,
678 enc_kc128 := kc128
Harald Welte651fcdc2018-05-10 20:23:16 +0200679}
680
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100681template (value) TestHdlrEncrParams t_EncrParams2(OCT1 alg_permitted,
682 OCT1 alg_expect,
683 template(omit) OCT1 alg_chosen,
684 octetstring key,
685 template (omit) octetstring kc128 := omit) := {
686 enc_alg_permitted := alg_permitted,
687 enc_alg_expect := alg_expect,
688 enc_alg_chosen := alg_chosen,
689 enc_key := key,
690 enc_kc128 := kc128
691}
692
693/* Convenience for common t_EncrParams2 usage.
694 *
695 * To test a scenario where only A5/3 is permitted:
696 * f_encr_params('08'O);
697 * To test a scenario where A5/3 is chosen from A5/0,A5/1,A5/3 (1 | 2 | 8 = 11 = 0xb):
698 * f_encr_params('0b'O, '08'O);
699 * To test the same, but the Chosen Encryption Algorithm IE should reflect A5/1:
700 * f_encr_params('0b'O, '08'O, '02'O);
701 * To test the same, but the MSC sends no Chosen Encryption Algorithm IE:
702 * f_encr_params('0b'O, '08'O, omit);
703 *
704 * Set alg_chosen and alg_expect magically when == '00'O:
705 * - enc_alg_expect is set to alg_chosen if present, else to alg_permitted.
706 * - enc_alg_chosen is set to alg_permitted.
707 * When only alg_permitted is given, alg_permitted must reflect only one algorithm, or f_cipher_mode_bssmap_to_rsl()
708 * will fail.
709 * When alg_chosen is passed as omit, the messages from the MSC will not contain a Chosen Encryption Algorithm IE.
710 */
711function f_encr_params(OCT1 alg_permitted, OCT1 alg_expect := '00'O, template (omit) OCT1 alg_chosen := '00'O,
712 boolean kc128 := false)
713 return TestHdlrEncrParams
714{
715 if (not istemplatekind(alg_chosen, "omit")) {
716 if (valueof(alg_chosen) == '00'O) {
717 if (alg_expect != '00'O) {
718 alg_chosen := alg_expect;
719 } else {
720 /* In this case, alg_permitted should have only one permitted algo */
721 alg_chosen := alg_permitted;
722 }
723 }
724 if (alg_expect == '00'O) {
725 alg_expect := valueof(alg_chosen);
726 }
727 }
728 if (alg_expect == '00'O) {
729 /* In this case, alg_permitted should have only one permitted algo */
730 alg_expect := valueof(alg_permitted);
731 }
Vadim Yanitskiy35c9a332022-03-16 20:26:35 +0300732 var template (omit) octetstring kc := omit;
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100733 if (kc128) {
734 kc := f_rnd_octstring(16);
735 }
736 return valueof(t_EncrParams2(alg_permitted, alg_expect, alg_chosen, f_rnd_octstring(8), kc));
737}
738
Harald Weltecc0b0142018-05-29 15:19:33 +0200739type record TestHdlrParamsLcls {
740 GlobalCallReferenceValue gcr optional,
741 /* LCLS Configuration */
742 BIT4 cfg optional,
743 /* LCLS Connection Status Control */
744 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100745 BIT4 exp_sts optional,
746 /* Whether to adjust *cx_seen_exp for LCLS tests */
747 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200748}
749
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200750type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200751 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200752 integer rsl_idx,
753 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200754}
755
Harald Welte801ed1a2023-04-30 15:07:00 +1000756type record ASCITest {
757 boolean vgcs_setup_ok,
758 boolean vgcs_assign_ok,
759 boolean vgcs_assign_fail,
760 boolean vgcs_talker_req,
Andreas Eversbergcfdbb3e2023-07-20 12:22:58 +0200761 boolean vgcs_talker_fail,
Harald Welte801ed1a2023-04-30 15:07:00 +1000762 boolean vgcs_talker_est,
Andreas Eversberg35874922023-07-07 13:44:04 +0200763 boolean vgcs_talker_rel,
Andreas Eversberga29b2f32023-07-20 17:28:41 +0200764 boolean vgcs_uplink_reject,
Andreas Eversberg35874922023-07-07 13:44:04 +0200765 boolean vgcs_uplink_seized,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200766 boolean vgcs_uplink_release,
767 boolean delay_bts,
768 boolean delay_msc
Harald Welte801ed1a2023-04-30 15:07:00 +1000769};
770
Harald Weltec1a2fff2017-12-17 11:06:19 +0100771type record TestHdlrParams {
772 OCT1 ra,
773 GsmFrameNumber fn,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100774 hexstring imsi optional,
775 hexstring imei optional,
Harald Welte60aa5762018-03-21 19:33:13 +0100776 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200777 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200778 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200779 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100780 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200781 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100782 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200783 SCCP_PAR_Address sccp_addr_msc optional,
784 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100785 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100786 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200787 boolean aoip,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200788 boolean use_osmux_cn,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200789 boolean use_osmux_bts,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200790 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200791 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200792 integer mgwpool_idx, /* MGCP_Emulation_CT (vc_MGCP) to use for this MSC ConnHdlr */
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200793 boolean media_mgw_offer_ipv6,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200794 OCT3 last_used_eutran_plmn optional,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200795 boolean exp_fast_return, /* RR Release expected to contain CellSelectInd ? */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200796 boolean expect_channel_mode_modify,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100797 uint3_t expect_tsc optional,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100798 BSSMAP_IE_CellIdentifier cell_id_source,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100799 boolean expect_ho_fail,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100800 boolean expect_ho_fail_lchan_est,
801 boolean inter_bsc_ho_in__ho_req_in_initial_sccp_cr,
Oliver Smith735b47c2023-02-15 16:03:54 +0100802 boolean ignore_mgw_mdcx,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100803 boolean fail_on_dlcx,
Harald Welte801ed1a2023-04-30 15:07:00 +1000804 boolean ignore_ipa_media,
805 ASCITest asci_test
Harald Weltec1a2fff2017-12-17 11:06:19 +0100806};
807
Philipp Maier48604732018-10-09 15:00:37 +0200808/* Note: Do not use valueof() to get a value of this template, use
809 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100810 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100811template (value) TestHdlrParams t_def_TestHdlrPars := {
812 ra := '23'O,
813 fn := 23,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100814 imsi := omit, /* set to random in f_gen_test_hdlr_pars() */
815 imei := omit, /* set to random in f_gen_test_hdlr_pars() */
Harald Welte60aa5762018-03-21 19:33:13 +0100816 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200817 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200818 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200819 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100820 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200821 encr := omit,
822 lcls := {
823 gcr := omit,
824 cfg := omit,
825 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100826 exp_sts := omit,
827 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100828 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200829 sccp_addr_msc := omit,
830 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100831 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100832 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200833 aoip := true,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200834 use_osmux_cn := false,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200835 use_osmux_bts := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200836 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200837 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200838 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200839 rsl_idx := 0,
840 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200841 },
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200842 mgwpool_idx := 0,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200843 media_mgw_offer_ipv6 := true,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200844 last_used_eutran_plmn := omit,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200845 exp_fast_return := false,
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200846 expect_channel_mode_modify := false,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100847 expect_tsc := omit,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100848 cell_id_source := valueof(ts_CellID_LAC_CI(1, 1)),
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100849 expect_ho_fail := false,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100850 expect_ho_fail_lchan_est := false,
851 inter_bsc_ho_in__ho_req_in_initial_sccp_cr := true,
Oliver Smith735b47c2023-02-15 16:03:54 +0100852 ignore_mgw_mdcx := false,
Oliver Smith4eed06f2023-02-15 15:43:19 +0100853 fail_on_dlcx := true,
Harald Welte801ed1a2023-04-30 15:07:00 +1000854 ignore_ipa_media := false,
855 asci_test := {
856 vgcs_setup_ok := false,
857 vgcs_assign_ok := false,
858 vgcs_assign_fail := false,
859 vgcs_talker_req := false,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200860 vgcs_talker_fail := false,
Harald Welte801ed1a2023-04-30 15:07:00 +1000861 vgcs_talker_est := false,
Andreas Eversberg35874922023-07-07 13:44:04 +0200862 vgcs_talker_rel := false,
Andreas Eversberga29b2f32023-07-20 17:28:41 +0200863 vgcs_uplink_reject := false,
Andreas Eversberg35874922023-07-07 13:44:04 +0200864 vgcs_uplink_seized := false,
Andreas Eversberg28d6b812023-07-20 13:07:26 +0200865 vgcs_uplink_release := false,
866 delay_bts := false,
867 delay_msc := false
Harald Welte801ed1a2023-04-30 15:07:00 +1000868 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100869}
870
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300871function f_create_chan_and_exp(template (present) PDU_BSSAP exp_l3_compl := ?)
872runs on MSC_ConnHdlr {
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100873 var MobileIdentityLV mi;
874 var PDU_ML3_MS_NW l3_info;
875 var octetstring l3_enc;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200876 var template uint3_t tsc := ?;
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300877 timer T;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200878
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100879 if (ispresent(g_pars.imsi)) {
880 mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
881 } else if (ispresent(g_pars.imei)) {
882 mi := valueof(ts_MI_IMEI_LV(g_pars.imei));
883 } else {
884 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
885 "Either imsi or imei must be set!");
886 }
887 l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
888 l3_enc := enc_PDU_ML3_MS_NW(l3_info);
889
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200890 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
891 tsc := g_pars.expect_tsc;
892 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100893
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300894 if (istemplatekind(exp_l3_compl, "?")) {
895 if (g_pars.aoip == false) {
896 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := omit);
897 } else {
898 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := ?);
899 }
900 }
901
Neels Hofmeyrd601f9c2021-06-04 22:46:06 +0200902 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100903 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200904 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 +0300905 /* wait for a COMPL_L3 from the BSC to ensure that the SCCP connection is up */
906 T.start(2.0);
907 alt {
908 [] BSSAP.receive(exp_l3_compl);
909 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
910 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
911 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
912 }
913 [] T.timeout {
914 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
915 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
916 }
917 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100918}
919
Harald Welte898113b2018-01-31 18:32:21 +0100920function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200921 template (omit) RslChannelNr chan_nr := omit, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Welte898113b2018-01-31 18:32:21 +0100922 if (not isvalue(link_id)) {
923 link_id := ts_RslLinkID_DCCH(0);
924 }
925 if (not isvalue(chan_nr)) {
926 chan_nr := g_chan_nr;
927 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200928 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 +0100929}
930
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200931function 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 +0100932 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100933 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100934 if (orig.msg_type == RSL_MT_ENCR_CMD) {
935 link_id := orig.ies[2].body.link_id;
936 } else {
937 link_id := orig.ies[1].body.link_id;
938 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200939 f_rsl_send_l3(l3, link_id, chan_nr, rsl_pt := rsl_pt);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100940}
941
Oliver Smith598e1ed2021-07-09 10:28:40 +0200942/* Convert the cipher representation on BSSMAP to the representation used on RSL */
943function f_cipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100944{
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300945 select (alg_bssmap) {
946 case ('01'O) { return RSL_ALG_ID_A5_0; }
947 case ('02'O) { return RSL_ALG_ID_A5_1; }
948 case ('04'O) { return RSL_ALG_ID_A5_2; }
949 case ('08'O) { return RSL_ALG_ID_A5_3; }
950 case ('10'O) { return RSL_ALG_ID_A5_4; }
951 case ('20'O) { return RSL_ALG_ID_A5_5; }
952 case ('40'O) { return RSL_ALG_ID_A5_6; }
953 case ('80'O) { return RSL_ALG_ID_A5_7; }
954 case else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100955 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
956 oct2str(alg_bssmap));
Harald Weltee613f962018-04-18 22:38:16 +0200957 return RSL_ALG_ID_A5_0;
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300958 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100959 }
960}
961
Vadim Yanitskiyaf2972e2022-03-14 22:27:57 +0300962/* Convert the cipher representation on BSSMAP to the one used on RR (3GPP TS 44.018) */
963function f_cipher_mode_bssmap_to_rr(OCT1 alg_bssmap) return BIT3
964{
965 select (alg_bssmap) {
966 case ('01'O) /* A5/0 */ { return '000'B; } /* SC=0 */
967 case ('02'O) /* A5/1 */ { return '000'B; } /* SC=1 */
968 case ('04'O) /* A5/2 */ { return '001'B; } /* SC=1 */
969 case ('08'O) /* A5/3 */ { return '010'B; } /* SC=1 */
970 case ('10'O) /* A5/4 */ { return '011'B; } /* SC=1 */
971 case ('20'O) /* A5/5 */ { return '100'B; } /* SC=1 */
972 case ('40'O) /* A5/6 */ { return '101'B; } /* SC=1 */
973 case ('80'O) /* A5/7 */ { return '110'B; } /* SC=1 */
974 case else {
975 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
976 oct2str(alg_bssmap));
977 return '000'B;
978 }
979 }
980}
981
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200982function f_verify_encr_info(RSL_Message rsl) runs on MSC_ConnHdlr {
983 var RSL_IE_Body encr_info;
984 var RSL_AlgId alg_rsl;
985 var template octetstring expect_kc;
986
987 /* If no encryption is enabled, then make sure there is no RSL_IE_ENCR_INFO */
988 if (not ispresent(g_pars.encr)) {
989 if (f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
990 setverdict(fail, "Found Encryption IE, but expected no encryption in ", rsl.msg_type);
991 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
992 return;
993 }
994 setverdict(pass);
995 return;
996 }
997
998 /* RSL uses a different representation of the encryption algorithm,
999 * so we need to convert first */
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001000 alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +02001001
1002 if (alg_rsl == RSL_ALG_ID_A5_4 and ispresent(g_pars.encr.enc_kc128)) {
1003 expect_kc := g_pars.encr.enc_kc128;
1004 } else if (alg_rsl == RSL_ALG_ID_A5_0) {
1005 /* When A5/0 is chosen, no encryption is active, so technically, no key is needed. However, 3GPP TS
1006 * 48.058 9.3.7 Encryption Information stays quite silent about presence or absence of a key for A5/0.
1007 * The only thing specified is how to indicate the length of the key; the possibility that the key may
1008 * be zero length is not explicitly mentioned. So it seems that we should always send the key along,
1009 * even for A5/0. Still, let's also allow a zero length key for A5/0. */
1010 expect_kc := (g_pars.encr.enc_key, ''O);
1011 } else {
1012 expect_kc := g_pars.encr.enc_key;
1013 }
1014 log("for encryption algo ", alg_rsl, " expect kc = ", expect_kc);
1015
1016 if (not f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
1017 if (alg_rsl == RSL_ALG_ID_A5_0) {
1018 /* For A5/0, encryption is not active. It is fine to omit the Encryption Information in this
1019 * case. Note that the first channel may see an RSL Encryption Command with A5/0 indicated, and
1020 * a subsequent handover may activate a new channel without any Encryption Information. */
1021 setverdict(pass);
1022 return;
1023 }
1024 setverdict(fail, "Missing Encryption Information IE in ", rsl.msg_type);
1025 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1026 return;
1027 }
1028
1029 if (not match(encr_info, tr_EncrInfo(alg_rsl, expect_kc))) {
1030 setverdict(fail, "Unexpected Kc in Encryption Information IE in ", rsl.msg_type);
1031 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
1032 return;
1033 }
1034 setverdict(pass);
1035}
1036
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001037function f_cipher_mode(TestHdlrEncrParams enc, boolean exp_fail := false,
1038 RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC)
Harald Welte38b2a102017-12-23 02:42:58 +01001039runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +01001040 var PDU_BSSAP bssap;
1041 var RSL_Message rsl;
1042
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001043 if (isvalue(enc.enc_kc128)) {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001044 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(enc.enc_alg_permitted, enc.enc_key, valueof(enc.enc_kc128)));
Harald Welte73cd2712017-12-17 00:44:52 +01001045 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001046 BSSAP.send(ts_BSSMAP_CipherModeCmd(enc.enc_alg_permitted, enc.enc_key));
Harald Welte73cd2712017-12-17 00:44:52 +01001047 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +01001048
Harald Welte73cd2712017-12-17 00:44:52 +01001049 alt {
1050 /* RSL/UE Side */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001051 [] rsl_pt.receive(tr_RSL_ENCR_CMD(g_chan_nr)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +01001052 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
1053 log("Rx L3 from net: ", l3);
Neels Hofmeyr72749342021-06-11 02:41:37 +02001054
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +02001055 f_verify_encr_info(rsl);
1056
Harald Welte73cd2712017-12-17 00:44:52 +01001057 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001058 f_rsl_reply(ts_RRM_CiphModeCompl, rsl, rsl_pt := rsl_pt);
Harald Welte73cd2712017-12-17 00:44:52 +01001059 }
1060 repeat;
1061 }
1062 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001063 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001064 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +01001065 } else {
1066 setverdict(pass);
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001067 var RSL_AlgId alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr5e31cfc2021-06-11 02:42:11 +02001068 if (oct2int(bssap.pdu.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algorithmIdentifier) != enum2int(alg_rsl)) {
1069 setverdict(fail, "Unexpected Encryption Algorithm ID in BSSMAP Cipher Mode Complete");
1070 }
Harald Welte38b2a102017-12-23 02:42:58 +01001071 }
Harald Welte73cd2712017-12-17 00:44:52 +01001072 }
1073 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001074 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001075 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +01001076 } else {
1077 setverdict(pass);
1078 }
Harald Welte73cd2712017-12-17 00:44:52 +01001079 }
1080 }
1081}
1082
Harald Welte211219e2018-01-29 22:03:36 +01001083/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
1084function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
1085 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +01001086 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +01001087
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001088 select (inp) {
1089 case ('00001'B) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +01001090 chan_nr := valueof(t_RslChanNr_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001091 }
1092 case ('11101'B) { /* VAMOS TCH/F */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001093 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001094 }
1095 case ('0001?'B) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +01001096 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001097 }
1098 case ('1111?'B) { /* VAMOS TCH/H */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001099 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001100 }
1101 case ('001??'B) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001102 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001103 }
1104 case ('01???'B) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001105 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001106 }
1107 case else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001108 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001109 }
Harald Welte211219e2018-01-29 22:03:36 +01001110 }
1111
1112 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001113 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +01001114 } else {
1115 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
1116 arfcn := oct2int(concat);
1117 }
1118}
1119
1120type record AssignmentState {
1121 /* global */
Oliver Smithce2dd692023-04-17 10:25:50 +02001122 boolean rtp_stream,
Harald Welte211219e2018-01-29 22:03:36 +01001123 boolean is_assignment,
1124 /* Assignment related bits */
1125 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001126 boolean old_lchan_deact_sacch_seen,
1127 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +01001128 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +01001129 RslChannelNr old_chan_nr,
1130 /* Modify related bits */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001131 PDU_ML3_NW_MS rr_channel_mode_modify_msg optional,
Harald Welte211219e2018-01-29 22:03:36 +01001132 boolean rr_modify_seen,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001133 RSL_Message rsl_mode_modify_msg optional,
Harald Welte21583082018-01-29 22:28:26 +01001134 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +01001135}
1136
1137template (value) AssignmentState ts_AssignmentStateInit := {
Oliver Smithce2dd692023-04-17 10:25:50 +02001138 rtp_stream := false,
Harald Welte211219e2018-01-29 22:03:36 +01001139 is_assignment := false,
1140 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001141 old_lchan_deact_sacch_seen := false,
1142 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +01001143 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +01001144 old_chan_nr := -,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001145 rr_channel_mode_modify_msg := omit,
Harald Welte211219e2018-01-29 22:03:36 +01001146 rr_modify_seen := false,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001147 rsl_mode_modify_msg := omit,
Harald Welte21583082018-01-29 22:28:26 +01001148 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +01001149}
1150
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001151private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
1152 encr_info := {
1153 len := ?,
1154 alg_id := alg,
1155 key := key
1156 }
1157}
1158
1159/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
1160private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
1161 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001162 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001163 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001164
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001165 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg_permitted != '01'O) {
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001166 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001167 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001168 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001169 var RSL_AlgId alg := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr0588cad2021-06-11 01:38:18 +02001170 var octetstring expect_key;
1171 if (alg == RSL_ALG_ID_A5_4) {
1172 expect_key := g_pars.encr.enc_kc128;
1173 } else {
1174 expect_key := g_pars.encr.enc_key;
1175 }
1176 if (not match(encr_info, tr_EncrInfo(alg, expect_key))) {
1177 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
1178 "Unexpected Kc in Encryption IE in RSL ENCR CMD");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001179 }
1180 }
1181 } else {
1182 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
1183 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001184 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001185 }
1186 }
1187 }
1188 /* 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 +01001189 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001190
Pau Espin Pedrol8f773632019-11-05 11:46:53 +01001191 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 +01001192 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
1193 }
1194
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001195 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
1196 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
1197 } else {
1198 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 +01001199 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 +01001200 }
1201 }
1202
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001203}
1204
Neels Hofmeyr6a955cc2021-10-02 14:53:48 +02001205function rr_chan_desc_tsc(ChannelDescription2_V cd2)
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001206 return uint3_t
1207{
1208 var uint3_t tsc := oct2int(cd2.octet3);
1209 tsc := tsc / 32; /* shl 5 */
1210 return tsc;
1211}
1212
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001213altstep 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 +01001214 var RSL_Message rsl;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001215 [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 +01001216 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1217 log("Rx L3 from net: ", l3);
1218 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
1219 var RslChannelNr new_chan_nr;
1220 var GsmArfcn arfcn;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001221
1222 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1223 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime);
1224 if (not match(got_tsc, g_pars.expect_tsc)) {
1225 setverdict(fail, "RR Assignment: unexpected TSC in Channel Description: expected ",
1226 g_pars.expect_tsc, " got ", got_tsc);
1227 mtc.stop;
1228 }
1229 }
1230
Harald Welte211219e2018-01-29 22:03:36 +01001231 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
1232 new_chan_nr, arfcn);
1233 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +01001234
Harald Welte211219e2018-01-29 22:03:36 +01001235 /* register our component for this channel number at the RSL Emulation */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001236 f_rslem_register(0, new_chan_nr, PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001237 /* dispatch queued messages for this channel (if any) */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001238 f_rslem_dchan_queue_dispatch(PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001239
Harald Welte211219e2018-01-29 22:03:36 +01001240 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
1241 /* send assignment complete over the new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001242 rsl_pt.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +01001243 enc_PDU_ML3_MS_NW(l3_tx)));
1244 /* by default, send via the new channel from now */
1245 st.old_chan_nr := g_chan_nr;
1246 g_chan_nr := new_chan_nr;
1247 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001248 /* obtain channel activation from RSL_Emulation for new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001249 var RSL_Message chan_act := f_rslem_get_last_act(rsl_proc_pt, 0, g_chan_nr);
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001250 /* check it (e.g. for correct ciphering parameters) */
1251 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +01001252 repeat;
1253 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001254 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +01001255 }
Harald Welte211219e2018-01-29 22:03:36 +01001256 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001257 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001258 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001259 repeat;
1260 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001261 [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 +01001262 st.old_lchan_rll_rel_req_seen := true;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001263 rsl_pt.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
Harald Welte211219e2018-01-29 22:03:36 +01001264 repeat;
1265 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001266 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1267 rsl_pt.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +01001268 /* unregister for old channel number in RSL emulation */
1269 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001270 f_rslem_unregister(0, st.old_chan_nr, PT := rsl_proc_pt);
Harald Welte21583082018-01-29 22:28:26 +01001271 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +01001272 repeat;
1273 }
1274}
1275
1276altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
1277 /* no assignment, just mode modify */
1278 var RSL_Message rsl;
1279
Oliver Smithce2dd692023-04-17 10:25:50 +02001280 [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 +01001281 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1282 log("Rx L3 from net: ", l3);
1283 if (ischosen(l3.msgs.rrm.channelModeModify)) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001284 st.rr_channel_mode_modify_msg := l3;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001285 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
1286 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +01001287 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001288 }
1289 repeat;
1290 }
Oliver Smithce2dd692023-04-17 10:25:50 +02001291 [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 +02001292 st.rsl_mode_modify_msg := rsl;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001293 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +01001294 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001295 repeat;
1296 }
Harald Welte211219e2018-01-29 22:03:36 +01001297}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001298
Harald Welte211219e2018-01-29 22:03:36 +01001299/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
1300function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
1301return boolean {
1302 select (bssmap.speechOrDataIndicator) {
1303 case ('0011'B) { /* Signalling */
1304 /* all channels support signalling */
1305 return true;
1306 }
1307 case else { /* Speech, Speech+CTM or CSD */
1308 select (bssmap.channelRateAndType) {
1309 case ('08'O) { /* TCH/F */
1310 select (rsl_chan_nr) {
1311 case (t_RslChanNr_Bm(?)) { return true; }
1312 }
1313 }
1314 case ('09'O) { /* TCH/H */
1315 select (rsl_chan_nr) {
1316 case (t_RslChanNr_Lm(?, ?)) { return true; }
1317 }
1318 }
1319 case else { /* full or half-rate */
1320 select (rsl_chan_nr) {
1321 case (t_RslChanNr_Bm(?)) { return true; }
1322 case (t_RslChanNr_Lm(?, ?)) { return true; }
1323 }
1324 }
1325 }
Harald Weltec1a2fff2017-12-17 11:06:19 +01001326 }
Harald Welte211219e2018-01-29 22:03:36 +01001327 }
1328 return false;
1329}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001330
Philipp Maier8ef527e2018-05-18 11:12:50 +02001331/* Determine if the channel mode specified within rsl_chan_nr requires a
1332 * MODE MODIFY in to match the channel mode specified by given BSSMAP
1333 * ChannelType */
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001334function f_channel_needs_modify(TELNETasp_PT vty, BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
Philipp Maier8ef527e2018-05-18 11:12:50 +02001335return boolean {
1336
Philipp Maier8ef527e2018-05-18 11:12:50 +02001337 var boolean current_signalling := false;
1338 var boolean desired_signalling := false;
1339
1340 select (rsl_chan_nr) {
1341 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
1342 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001343 case (t_RslChanNr_Bm(?)) {
1344 /* TCH/F, always subslot 0 */
1345 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn) & " 0");
1346 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1347 current_signalling := true;
1348 }
1349 }
1350 case (t_RslChanNr_Lm(?, ?)) {
1351 /* TCH/H */
1352 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn)
1353 & " " & int2str(rsl_chan_nr.u.lm.sub_chan));
1354 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1355 current_signalling := true;
1356 }
1357 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001358 }
1359
1360 if (bssmap.speechOrDataIndicator == '0011'B) {
1361 desired_signalling := true;
1362 }
1363
1364 if (current_signalling == desired_signalling) {
1365 /* The desired channel mode is equal to the one we currently
1366 * have, there is no mode modification needed or expected */
1367 return false;
1368 } else {
1369 /* The desired channel mode and the current channel mode do
1370 * not match. A mode modification is required */
1371 return true;
1372 }
1373}
1374
Harald Weltecc0b0142018-05-29 15:19:33 +02001375/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
1376function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
1377 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
1378 if (istemplatekind(ass_tpl, "omit")) {
1379 return;
1380 }
1381 if (ispresent(g_pars.lcls.gcr)) {
1382 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
1383 }
1384 if (ispresent(g_pars.lcls.cfg)) {
1385 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1386 }
1387 if (ispresent(g_pars.lcls.csc)) {
1388 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1389 }
Harald Welte343b7742018-06-05 23:41:37 +02001390 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1391 if (ispresent(g_pars.lcls.exp_sts)) {
1392 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1393 } else {
1394 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1395 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001396 }
1397}
1398
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001399/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1400function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1401 template MgcpResponse resp_any := ?
1402 var MgcpResponse resp_val;
1403 resp.line.trans_id := cmd.line.trans_id;
1404 timer T := 5.0;
1405
1406 BSSAP.send(cmd);
1407 T.start;
1408 alt {
1409 [] as_Media_mgw();
1410 [] BSSAP.receive(resp) -> value resp_val { }
1411 [] BSSAP.receive(resp_any) {
1412 setverdict(fail, "Response didn't match template");
1413 mtc.stop;
1414 }
1415 [] BSSAP.receive { repeat; }
1416 [] T.timeout {
1417 setverdict(fail, "Timeout waiting for response to ", cmd);
1418 mtc.stop;
1419 }
1420 }
1421 T.stop;
1422}
1423
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001424/* Helper function to check if the activity on the MGCP matches what we
1425 * expected */
1426function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1427 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001428 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1429 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1430 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1431
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001432 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001433 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 +02001434 }
1435 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001436 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 +02001437 }
1438 }
1439}
1440
Harald Welte211219e2018-01-29 22:03:36 +01001441/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001442function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1443runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001444
1445 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001446 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001447
Philipp Maier4ce978c2020-08-11 22:54:07 +02001448 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001449 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1450 } else {
1451 /* Make sure a meaningful default is assigned in case the
1452 * codecList is not populated */
1453 codecType := FR_AMR;
1454 }
1455
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001456 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001457
Harald Weltecc0b0142018-05-29 15:19:33 +02001458 /* patch in the LCLS related items, as needed */
1459 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1460
Harald Welte7a14fd52018-05-10 22:26:55 +02001461 f_create_chan_and_exp();
Harald Welte7a14fd52018-05-10 22:26:55 +02001462
1463 /* start ciphering, if requested */
1464 if (ispresent(g_pars.encr)) {
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001465 f_cipher_mode(g_pars.encr);
Harald Welte7a14fd52018-05-10 22:26:55 +02001466 }
1467
1468 /* bail out early if no assignment requested */
1469 if (istemplatekind(ass_tpl, "omit")) {
1470 return;
1471 }
1472
1473 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001474 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001475 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001476 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001477 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001478
Harald Welte211219e2018-01-29 22:03:36 +01001479 var ExpectCriteria mgcpcrit := {
1480 connid := omit,
1481 endpoint := omit,
1482 transid := omit
1483 };
1484 var AssignmentState st := valueof(ts_AssignmentStateInit);
Oliver Smithce2dd692023-04-17 10:25:50 +02001485 /* if the channel type is SIGNAL, we're not handling an rtp stream */
Harald Welte211219e2018-01-29 22:03:36 +01001486 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
Oliver Smithce2dd692023-04-17 10:25:50 +02001487 st.rtp_stream := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001488 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001489 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001490
Harald Welte211219e2018-01-29 22:03:36 +01001491 /* determine if the current channel can support the given service or not */
1492 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1493 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001494
1495 /* We decided to assign a new channel, so we do not expect
1496 * any mode modify messages on RSL */
1497 exp_modify := false;
1498 } else {
1499 /* We will continue working with the currently assigned
1500 * channel, we must now check if the mode of the current
1501 * channel is compatible. If not we expect the BSC to modify
1502 * the mode */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001503 st.is_assignment := false;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001504 exp_modify := f_channel_needs_modify(BSCVTY, ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001505 }
1506
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001507 /* Some test situations will involve MGCP transactions on a media
1508 * gateway. Depending on the situation, we set up how many of each MGCP
1509 * message type are expected to be exchanged. */
1510 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1511 /* For tests that expect the assignment to fail, assume that
1512 * there will be no MGW communication as well. */
1513 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1514 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1515 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1516 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Oliver Smithce2dd692023-04-17 10:25:50 +02001517 } else if (st.rtp_stream) {
1518 /* For RTP streams we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001519 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1520 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1521 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1522 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001523 }
1524
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001525 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1526 * a channel and send us RR Assignment Command together with ip.access CRCX.
1527 * There is a risk that the RSL Emulation component would dequeue and process
1528 * ip.access CRCX faster than we process the Assignment Command and register
1529 * the corresponding handler for the indicated RSL channel number. This would
1530 * result in a failure, because at that moment there will be no handler for
1531 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1532 f_rslem_dchan_queue_enable();
1533
Harald Welte211219e2018-01-29 22:03:36 +01001534 f_create_mgcp_expect(mgcpcrit);
1535 BSSAP.send(ass_cmd);
1536
1537 T.start;
1538 alt {
1539 /* assignment related bits */
1540 [st.is_assignment] as_assignment(st);
1541
1542 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001543 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001544
Oliver Smithce2dd692023-04-17 10:25:50 +02001545 /* RTP stream related bits (IPA CRCX/MDCX + MGCP) */
1546 [st.rtp_stream] as_Media();
Harald Welte211219e2018-01-29 22:03:36 +01001547
1548 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001549 [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 +01001550 setverdict(pass);
1551 }
Philipp Maier86f39202018-02-07 14:40:09 +01001552 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1553 setverdict(pass);
1554 }
Harald Welte21583082018-01-29 22:28:26 +01001555 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001556 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001557 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001558 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001559 }
1560 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001561 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001562 }
1563 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001564 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001565 }
1566 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001567 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001568 }
1569 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001570 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001571 }
1572 }
Harald Welte211219e2018-01-29 22:03:36 +01001573 log("g_media ", g_media);
1574 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001575 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001576 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001577
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001578 if (exp_modify) {
1579 /* Verify that the RR Channel Mode Modify and RSL MODE MODIFY message asked for the expected channel
1580 * mode. */
1581 /* TODO: more precisely expect the different types of speech? */
1582 var OCT1 rr_channel_mode := st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelMode.mode;
1583
Oliver Smithce2dd692023-04-17 10:25:50 +02001584 if (st.rtp_stream and rr_channel_mode == '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001585 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1586 " to a speech mode, but got channelMode == ", rr_channel_mode);
1587 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001588 } else if (not st.rtp_stream and rr_channel_mode != '00'O) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001589 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1590 " to signalling mode, but got channelMode == ", rr_channel_mode);
1591 mtc.stop;
1592 }
1593
1594 var RSL_IE_Body chan_mode_ie;
1595 if (not f_rsl_find_ie(st.rsl_mode_modify_msg, RSL_IE_CHAN_MODE, chan_mode_ie)) {
1596 setverdict(fail, "RSL MODE MODIFY message lacks a Channel Mode IE");
1597 mtc.stop;
1598 }
1599 var RSL_SpeechDataInd rsl_spd_ind := chan_mode_ie.chan_mode.spd_ind;
Oliver Smithce2dd692023-04-17 10:25:50 +02001600 if (st.rtp_stream and rsl_spd_ind != RSL_SPDI_SPEECH) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001601 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1602 " to a speech mode, but got spd_ind == ", rsl_spd_ind);
1603 mtc.stop;
Oliver Smithce2dd692023-04-17 10:25:50 +02001604 } else if (not st.rtp_stream and rsl_spd_ind != RSL_SPDI_SIGN) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001605 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1606 " to signalling mode, but got spd_ind == ", rsl_spd_ind);
1607 mtc.stop;
1608 }
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001609
1610 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1611 var uint3_t got_tsc := rr_chan_desc_tsc(st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelDescription);
1612 if (not match(got_tsc, g_pars.expect_tsc)) {
1613 setverdict(fail, "RR Channel Mode Modify: unexpected TSC in Channel Description: expected ",
1614 g_pars.expect_tsc, " got ", got_tsc);
1615 mtc.stop;
1616 }
1617 }
1618
1619 } else {
1620 /* not exp_modify, so this did a Channel Activate */
1621
1622 /* Check the TSC */
1623 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1624 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
1625 var RSL_IE_Body ie;
1626 if (f_rsl_find_ie(chan_act, RSL_IE_CHAN_IDENT, ie)) {
1627 var uint3_t got_tsc := ie.chan_ident.ch_desc.v.tsc;
1628 if (not match(got_tsc, g_pars.expect_tsc)) {
1629 setverdict(fail, "RSL CHANnel ACTIVation: unexpected TSC in Channel Description: expected ",
1630 g_pars.expect_tsc, " got ", got_tsc);
1631 mtc.stop;
1632 }
1633 }
1634 }
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001635 }
1636
Philipp Maiera0976e92018-07-16 15:19:42 +02001637 /* When the BSC detects that LCLS is possible it will cross the
1638 * connetions that point to the PBX side of the MGW. In our case this
1639 * is mgcp_conn[1]. The BSC performs this operation already before the
1640 * assignment complete is generated. This means we expect another MDCX
1641 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001642 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 +02001643 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1644 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1645
1646 }
1647 }
1648
Oliver Smithce2dd692023-04-17 10:25:50 +02001649 if (not exp_fail and st.rtp_stream and not g_pars.aoip) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001650 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1651 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1652 connection. BSC will forward it to its MGW. */
1653 var template MgcpCommand cmd;
1654 var template MgcpResponse resp;
1655 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
Pau Espin Pedrolb522e1c2023-09-28 13:38:50 +02001656 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* matches value configured in BSC_Tests.ttcn pass in AssignReq */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001657 var MgcpCallId call_id := '51234'H;
1658 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +02001659 if (g_pars.use_osmux_cn) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001660 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1661 resp := tr_CRCX_ACK_osmux;
1662 } else {
1663 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1664 resp := tr_CRCX_ACK;
1665 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001666 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001667 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1668 { valueof(ts_SDP_ptime(20)) });
1669 mgcp_transceive_mgw(cmd, resp);
1670 }
1671
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001672 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001673
1674 if (st.is_assignment and st.assignment_done) {
1675 if (not st.old_lchan_deact_sacch_seen) {
1676 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1677 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1678 }
1679 if (st.old_lchan_rll_rel_req_seen) {
1680 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1681 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1682 }
1683 }
Neels Hofmeyr9d686302021-04-25 19:02:29 +00001684
1685 var charstring lchan_info := f_vty_transceive_ret(BSCVTY, "show lchan 0 0 " & int2str(g_chan_nr.tn) & " 0");
1686 if (f_strstr(lchan_info, "State: ESTABLISHED") < 0) {
1687 log("after f_establish_fully(), 'show lchan' replied: ", lchan_info);
1688 setverdict(fail, "lchan is not in state ESTABLISHED");
1689 mtc.stop;
1690 }
Harald Welte930d0a72018-03-22 22:08:40 +01001691}
1692
Harald Welte261af4b2018-02-12 21:20:39 +01001693type record HandoverState {
1694 /* Assignment related bits */
1695 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001696 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001697 boolean handover_done,
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001698 RslChannelNr old_chan_nr,
1699 uint3_t expect_target_tsc optional
Harald Welte261af4b2018-02-12 21:20:39 +01001700};
1701
1702altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1703 var RSL_Message rsl;
1704 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1705 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1706 log("Rx L3 from net: ", l3);
1707 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1708 var RslChannelNr new_chan_nr;
1709 var GsmArfcn arfcn;
1710 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1711 new_chan_nr, arfcn);
1712 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1713
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001714 /* Verify correct TSC in handoverCommand */
1715 if (ispresent(st.expect_target_tsc)) {
1716 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.handoverCommand.channelDescription2);
1717 if (not match(got_tsc, st.expect_target_tsc)) {
1718 setverdict(fail, "RR Handover Command: unexpected TSC in Channel Description: expected ",
1719 st.expect_target_tsc, " got ", got_tsc);
1720 mtc.stop;
1721 } else {
1722 log("handoverCommand: verified TSC = ", got_tsc, " (matches ",
1723 st.expect_target_tsc, ")");
1724 }
1725 }
1726
Harald Welte261af4b2018-02-12 21:20:39 +01001727 /* register our component for this channel number at the RSL Emulation */
1728 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1729
1730 /* resume processing of RSL DChan messages, which was temporarily suspended
1731 * before performing a hand-over */
1732 f_rslem_resume(RSL1_PROC);
1733
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001734 /* send handover detect */
1735 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1736 f_sleep(0.3);
1737
Harald Welte261af4b2018-02-12 21:20:39 +01001738 /* send handover complete over the new channel */
1739 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001740 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001741 enc_PDU_ML3_MS_NW(l3_tx)));
1742 /* by default, send via the new channel from now */
1743 st.old_chan_nr := g_chan_nr;
1744 g_chan_nr := new_chan_nr;
1745 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001746
1747 /* Memorize how many MDCX we have seen before. We need this number to detect
1748 * that we have received the handover related MDCX */
1749 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001750 repeat;
1751 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001752 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001753 }
1754 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001755 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001756 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001757 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1758 repeat;
1759 }
Harald Welte261af4b2018-02-12 21:20:39 +01001760 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1761 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1762 repeat;
1763 }
1764 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1765 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1766 /* unregister for old channel number in RSL emulation */
1767 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1768 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001769
1770 /* The channel release must not necessarly be synchronized to the RSL handover
1771 * procedure since those events may happen independently nearly at the same
1772 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1773 * IPACC or MGCP level may be still in progress. In order to make sure that
1774 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001775 * as well as an MDCX on IPACC level.
1776 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1777 if (g_media.bts.ipa_crcx_seen
1778 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1779 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001780 repeat;
1781 } else {
1782 st.handover_done := true;
1783 }
Harald Welte261af4b2018-02-12 21:20:39 +01001784 }
1785}
1786
1787
Harald Weltec1a2fff2017-12-17 11:06:19 +01001788
Harald Welte28d943e2017-11-25 15:00:50 +01001789}