blob: ed9c13bbe5fbd1f867a51ff9d1f29ed5752437f7 [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
Philipp Maierb2422352018-07-11 10:36:57 +0200201/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
202 * RSL level media handling */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200203altstep 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 +0100204 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100205 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100206 var boolean b_unused;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200207 [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 +0100208 /* Extract parameters from request + use in response */
209 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
210 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
211 }
212 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
213 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
214 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200215 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
216 if (not g_pars.use_osmux_bts) {
217 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX with Osmux CID IE");
218 }
219 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
220 } else {
221 if (g_pars.use_osmux_bts) {
222 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX without Osmux CID IE");
223 }
224 g_media.bts.local_osmux_cid := omit;
225 g_media.bts.remote_osmux_cid := omit;
226 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200227 rsl_pt.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200228 f_inet_addr(g_media.bts.bts.host),
Harald Welte211219e2018-01-29 22:03:36 +0100229 g_media.bts.bts.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200230 g_media.bts.rtp_pt,
231 g_media.bts.local_osmux_cid));
Harald Welte211219e2018-01-29 22:03:36 +0100232 g_media.bts.ipa_crcx_seen := true;
233 repeat;
234 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200235 [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 +0100236 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100237 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100238 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100239 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100240 }
241 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100242 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200243 g_media.bts.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100244 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100245 g_media.bts.peer.port_nr := ie.ipa_remote_port;
246 /* optional */
247 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
248 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
249 }
250 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
251 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
252 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200253 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
254 if (not g_pars.use_osmux_bts) {
255 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX with Osmux CID IE");
256 }
257 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
258 } else {
259 if (g_pars.use_osmux_bts) {
260 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX without Osmux CID IE");
261 }
262 g_media.bts.local_osmux_cid := omit;
263 g_media.bts.remote_osmux_cid := omit;
264 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200265 rsl_pt.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200266 f_inet_addr(g_media.bts.peer.host),
Harald Welte211219e2018-01-29 22:03:36 +0100267 g_media.bts.peer.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200268 g_media.bts.rtp_pt,
269 g_media.bts.local_osmux_cid));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200270 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100271 repeat;
272 }
Harald Welte261af4b2018-02-12 21:20:39 +0100273
274 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200275 [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 +0100276 /* Extract parameters from request + use in response */
277 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
278 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
279 }
280 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
281 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
282 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200283 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
284 if (not g_pars.use_osmux_bts) {
285 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX with Osmux CID IE");
286 }
287 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
288 } else {
289 if (g_pars.use_osmux_bts) {
290 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC CRCX without Osmux CID IE");
291 }
292 g_media.bts.local_osmux_cid := omit;
293 g_media.bts.remote_osmux_cid := omit;
294 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200295 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 +0200296 f_inet_addr(g_media.bts1.bts.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100297 g_media.bts1.bts.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200298 g_media.bts1.rtp_pt,
299 g_media.bts.local_osmux_cid));
Harald Welte261af4b2018-02-12 21:20:39 +0100300 g_media.bts1.ipa_crcx_seen := true;
301 repeat;
302 }
303 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200304 [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 +0100305 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100306 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100307 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100308 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100309 }
310 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100311 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200312 g_media.bts1.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100313 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100314 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
315 /* optional */
316 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
317 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
318 }
319 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
320 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
321 }
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200322 if (f_rsl_find_ie(rsl, RSL_IE_OSMO_OSMUX_CID, ie)) {
323 if (not g_pars.use_osmux_bts) {
324 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX with Osmux CID IE");
325 }
326 g_media.bts.remote_osmux_cid := ie.osmux_cid.cid;
327 } else {
328 if (g_pars.use_osmux_bts) {
329 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Rx unexpected IPAC MDCX without Osmux CID IE");
330 }
331 g_media.bts.local_osmux_cid := omit;
332 g_media.bts.remote_osmux_cid := omit;
333 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200334 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 +0200335 f_inet_addr(g_media.bts1.peer.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100336 g_media.bts1.peer.port_nr,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200337 g_media.bts1.rtp_pt,
338 g_media.bts.local_osmux_cid));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200339 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100340 repeat;
341 }
342
Philipp Maierb2422352018-07-11 10:36:57 +0200343
344}
345
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200346function f_rx_crcx(MgcpCommand mgcp_cmd)
347 runs on MSC_ConnHdlr return template MgcpResponse {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200348 var SDP_Message sdp;
349 var integer cid := f_get_free_mgcp_conn();
Philipp Maiera2083892020-09-21 14:18:36 +0200350 var charstring local_rtp_addr;
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200351 var MgcpOsmuxCID osmux_cid;
Philipp Maiera2083892020-09-21 14:18:36 +0200352
353 if (g_pars.media_mgw_offer_ipv6 == true) {
354 local_rtp_addr := host_mgw_rtp_v6; /* Use IPv6 by default if no remote addr is provided by client */
355 } else {
356 local_rtp_addr := host_mgw_rtp_v4; /* Use IPv4 by default if no remote addr is provided by client */
357 }
358
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200359 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
360 if (cid != 0) {
361 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
362 }
363 /* we keep the endpoint name allocated during MediaState_init */
364 } else {
365 /* Call Agent allocated endpoint, trust/use it always */
366 g_media.mgcp_ep := mgcp_cmd.line.ep;
367 }
368 if (isvalue(mgcp_cmd.sdp)) {
369 sdp := mgcp_cmd.sdp;
370 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
371 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 +0200372 if (sdp.connection.addr_type == "IP6") {
373 local_rtp_addr := host_mgw_rtp_v6;
374 } else {
375 local_rtp_addr := host_mgw_rtp_v4;
376 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200377 }
378 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200379 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200380 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
381 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
382 mgcp_conn.mime_type & "/" &
383 int2str(mgcp_conn.sample_rate))),
384 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
385 var template MgcpResponse mgcp_resp;
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200386 if ((g_pars.use_osmux_cn or g_pars.use_osmux_bts) and
387 f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200388 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200389 if (osmux_cid != -1) {
390 mgcp_conn.remote_osmux_cid := osmux_cid;
391 }
392 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 +0200393 } else {
394 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
395 }
396 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
397 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
398 return mgcp_resp;
399}
400
401function f_rx_mdcx(MgcpCommand mgcp_cmd)
402 runs on MSC_ConnHdlr return template MgcpResponse {
403 var SDP_Message sdp;
404 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200405 var charstring local_rtp_addr;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200406 if (isvalue(mgcp_cmd.sdp)) {
407 sdp := mgcp_cmd.sdp;
408 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
409 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 +0200410 if (sdp.connection.addr_type == "IP6") {
411 local_rtp_addr := host_mgw_rtp_v6;
412 } else {
413 local_rtp_addr := host_mgw_rtp_v4;
414 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200415 } else {
416 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
417 }
418 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200419 sdp := valueof(ts_SDP(mgcp_conn.peer.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200420 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
421 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
422 mgcp_conn.mime_type & "/" &
423 int2str(mgcp_conn.sample_rate))),
424 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
425 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
426 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
427}
428
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200429function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
430runs on MSC_ConnHdlr return template MGCP_RecvFrom {
431 var template MGCP_RecvFrom mrf := {
432 connId := ?,
433 remName := ?,
434 remPort := ?,
435 locName := ?,
436 locPort := ?,
437 msg := msg
438 }
439 return mrf;
440}
441
Philipp Maierb2422352018-07-11 10:36:57 +0200442/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
443 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200444
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200445altstep as_Media_mgw(boolean norepeat := false, boolean fail_on_dlcx := true) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200446 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200447 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200448 var MGCP_RecvFrom mrf;
449 var template MgcpMessage msg_crcx := {
450 command := tr_CRCX
451 }
452 var template MgcpMessage msg_mdcx := {
453 command := tr_MDCX
454 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200455 var template MgcpMessage msg_dlcx := {
456 command := tr_DLCX
457 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200458 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200459
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200460 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200461 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100462 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200463 if(norepeat == false) {
464 repeat;
465 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200466 }
467
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200468 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
469 mgcp_resp := f_rx_crcx(mrf.msg.command);
470 msg_resp := {
471 response := mgcp_resp
472 }
473 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
474 if(norepeat == false) {
475 repeat;
476 }
477 }
478
Oliver Smith7a8594a2023-02-13 14:30:49 +0100479 [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 +0200480 mgcp_resp := f_rx_mdcx(mgcp_cmd);
481 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200482 if(norepeat == false) {
483 repeat;
484 }
Harald Welte211219e2018-01-29 22:03:36 +0100485 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200486
Oliver Smith7a8594a2023-02-13 14:30:49 +0100487 [not g_pars.aoip and not g_pars.ignore_mgw_mdcx]
488 MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200489 mgcp_resp := f_rx_mdcx(mrf.msg.command);
490 msg_resp := {
491 response := mgcp_resp
492 }
493 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
494 if(norepeat == false) {
495 repeat;
496 }
497 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200498
499 [fail_on_dlcx and g_pars.aoip] MGCP.receive(tr_DLCX) {
500 setverdict(fail, "Unexpected DLCX received");
501 }
502
503 [fail_on_dlcx and not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_dlcx)) {
504 setverdict(fail, "Unexpected DLCX received");
505 }
Harald Welte211219e2018-01-29 22:03:36 +0100506}
507
Philipp Maierb2422352018-07-11 10:36:57 +0200508/* Altsteps for handling of media related commands. Can be activated by a given
509 * test case if it expects to see media related handling (i.e. voice calls) */
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200510altstep as_Media(boolean fail_on_dlcx := true) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200511 [] as_Media_ipacc();
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200512 [] as_Media_mgw(fail_on_dlcx := fail_on_dlcx);
Philipp Maierb2422352018-07-11 10:36:57 +0200513}
Harald Welte211219e2018-01-29 22:03:36 +0100514
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200515type port Coord_PT message
516{
517 inout charstring;
518} with { extension "internal" };
519
Harald Welte28d943e2017-11-25 15:00:50 +0100520/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200521 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
522 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
Harald Welte47cd0e32020-08-21 12:39:11 +0200523type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr, BSSAP_LE_ConnHdlr, StatsD_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100524 /* SCCP Connecction Identifier for the underlying SCCP connection */
525 var integer g_sccp_conn_id;
526
Harald Welte6811d102019-04-14 22:23:14 +0200527 /* procedure port back to our parent (RAN_Emulation_CT) for control */
528 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100529 port TELNETasp_PT BSCVTY;
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200530 port Coord_PT COORD;
Vadim Yanitskiy5f14d342022-02-07 14:18:38 +0600531 port Coord_PT COORD2;
Harald Welte28d943e2017-11-25 15:00:50 +0100532
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200533 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
534 port IPA_MGCP_PT MGCP_MSC_CLIENT;
535 var integer g_trans_id := 0;
536
Harald Welte211219e2018-01-29 22:03:36 +0100537 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100538 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100539
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200540 var charstring host_bts := "127.0.0.2";
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200541 var charstring host_mgw_mgcp := "127.0.0.3";
542 var charstring host_mgw_rtp_v4 := "127.0.0.5";
543 var charstring host_mgw_rtp_v6 := "::1";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200544 var charstring host_msc := "127.0.0.4";
545
Harald Weltee97eab42018-03-21 18:46:06 +0100546 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100547}
548
Harald Welteeddf0e92020-06-21 19:42:15 +0200549function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100550 if (not g_vty_initialized) {
551 map(self:BSCVTY, system:BSCVTY);
552 f_vty_set_prompts(BSCVTY);
553 f_vty_transceive(BSCVTY, "enable");
554 g_vty_initialized := true;
555 }
Harald Welte28d943e2017-11-25 15:00:50 +0100556}
557
Harald Welteeddf0e92020-06-21 19:42:15 +0200558/* initialize all parameters */
559function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
560 f_MediaState_init(g_media, i, bts, mgw, codecType);
561 f_MscConnHdlr_init_vty();
562}
563
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200564private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
565 var MgcpTransId tid := int2str(g_trans_id);
566 g_trans_id := g_trans_id + 1;
567 return tid;
568}
569
Harald Welte6811d102019-04-14 22:23:14 +0200570/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100571 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
572private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200573runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100574 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200575 var boolean append_osmux_support := g_ran_ops.use_osmux and
576 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100577
Harald Weltec1a2fff2017-12-17 11:06:19 +0100578 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200579 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
580 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100581 }
582
583 return resp;
584}
585
Harald Welte47cd0e32020-08-21 12:39:11 +0200586/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
587 * BSSAP_LE message arrives. Can return a PDU_BSSAP_LE that should be sent in return */
588private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
589runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
590 var template PDU_BSSAP_LE resp := omit;
591
592 /* answer all RESET with a RESET ACK */
593 if (match(bssap, tr_BSSMAP_LE_Reset)) {
594 resp := ts_BSSMAP_LE_ResetAck;
595 }
596
597 return resp;
598}
599
Harald Welte6811d102019-04-14 22:23:14 +0200600const RanOps MSC_RanOps := {
601 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100602 unitdata_cb := refers(UnitdataCallback),
603 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100604 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200605 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200606 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200607 use_osmux := false,
Eric Wild66448c12022-04-02 14:57:53 +0200608 bssap_reset_retries := 1,
Harald Welte930d0a72018-03-22 22:08:40 +0100609 sccp_addr_local := omit,
610 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100611}
612
Harald Welte47cd0e32020-08-21 12:39:11 +0200613const BssapLeOps SMLC_BssapLeOps := {
614 create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
615 unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
616 decode_dtap := false,
617 role_ms := false,
618 sccp_addr_local := omit,
619 sccp_addr_peer := omit
620}
621
Daniel Willmann191e0d92018-01-17 12:44:35 +0100622const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100623 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
624 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100625}
626
Harald Weltec1a2fff2017-12-17 11:06:19 +0100627/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200628function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200629 RAN.call(RAN_register:{l3_enc, self}) {
630 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100631 }
632}
633
Harald Welte651fcdc2018-05-10 20:23:16 +0200634type record TestHdlrEncrParams {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100635 /* A mask of multiple encryption algorithms, for Encryption Information IE. */
636 OCT1 enc_alg_permitted,
637 /* Expect this encryption algorithm in Channel Activation from the BSC.
638 * To expect no encryption, set to '01'O == A5/0. */
639 OCT1 enc_alg_expect,
640 /* In BSSMAP, the Chosen Encryption Algorithm IE that the test sends to the BSC.
641 * When set to omit, the MSC will not send a Chosen Encryption Algorithm IE. */
642 OCT1 enc_alg_chosen optional,
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200643 octetstring enc_key,
644 octetstring enc_kc128 optional
Harald Welte651fcdc2018-05-10 20:23:16 +0200645};
646
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100647template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg_permitted,
648 octetstring key, template (omit) octetstring kc128 := omit) := {
649 enc_alg_permitted := alg_permitted,
650 enc_alg_expect := alg_permitted, /* <- for compat with tests before enc_alg_expect was added */
651 enc_alg_chosen := alg_permitted, /* <- for compat with tests before enc_alg_chosen was added */
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200652 enc_key := key,
653 enc_kc128 := kc128
Harald Welte651fcdc2018-05-10 20:23:16 +0200654}
655
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100656template (value) TestHdlrEncrParams t_EncrParams2(OCT1 alg_permitted,
657 OCT1 alg_expect,
658 template(omit) OCT1 alg_chosen,
659 octetstring key,
660 template (omit) octetstring kc128 := omit) := {
661 enc_alg_permitted := alg_permitted,
662 enc_alg_expect := alg_expect,
663 enc_alg_chosen := alg_chosen,
664 enc_key := key,
665 enc_kc128 := kc128
666}
667
668/* Convenience for common t_EncrParams2 usage.
669 *
670 * To test a scenario where only A5/3 is permitted:
671 * f_encr_params('08'O);
672 * To test a scenario where A5/3 is chosen from A5/0,A5/1,A5/3 (1 | 2 | 8 = 11 = 0xb):
673 * f_encr_params('0b'O, '08'O);
674 * To test the same, but the Chosen Encryption Algorithm IE should reflect A5/1:
675 * f_encr_params('0b'O, '08'O, '02'O);
676 * To test the same, but the MSC sends no Chosen Encryption Algorithm IE:
677 * f_encr_params('0b'O, '08'O, omit);
678 *
679 * Set alg_chosen and alg_expect magically when == '00'O:
680 * - enc_alg_expect is set to alg_chosen if present, else to alg_permitted.
681 * - enc_alg_chosen is set to alg_permitted.
682 * When only alg_permitted is given, alg_permitted must reflect only one algorithm, or f_cipher_mode_bssmap_to_rsl()
683 * will fail.
684 * When alg_chosen is passed as omit, the messages from the MSC will not contain a Chosen Encryption Algorithm IE.
685 */
686function f_encr_params(OCT1 alg_permitted, OCT1 alg_expect := '00'O, template (omit) OCT1 alg_chosen := '00'O,
687 boolean kc128 := false)
688 return TestHdlrEncrParams
689{
690 if (not istemplatekind(alg_chosen, "omit")) {
691 if (valueof(alg_chosen) == '00'O) {
692 if (alg_expect != '00'O) {
693 alg_chosen := alg_expect;
694 } else {
695 /* In this case, alg_permitted should have only one permitted algo */
696 alg_chosen := alg_permitted;
697 }
698 }
699 if (alg_expect == '00'O) {
700 alg_expect := valueof(alg_chosen);
701 }
702 }
703 if (alg_expect == '00'O) {
704 /* In this case, alg_permitted should have only one permitted algo */
705 alg_expect := valueof(alg_permitted);
706 }
Vadim Yanitskiy35c9a332022-03-16 20:26:35 +0300707 var template (omit) octetstring kc := omit;
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100708 if (kc128) {
709 kc := f_rnd_octstring(16);
710 }
711 return valueof(t_EncrParams2(alg_permitted, alg_expect, alg_chosen, f_rnd_octstring(8), kc));
712}
713
Harald Weltecc0b0142018-05-29 15:19:33 +0200714type record TestHdlrParamsLcls {
715 GlobalCallReferenceValue gcr optional,
716 /* LCLS Configuration */
717 BIT4 cfg optional,
718 /* LCLS Connection Status Control */
719 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100720 BIT4 exp_sts optional,
721 /* Whether to adjust *cx_seen_exp for LCLS tests */
722 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200723}
724
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200725type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200726 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200727 integer rsl_idx,
728 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200729}
730
Harald Weltec1a2fff2017-12-17 11:06:19 +0100731type record TestHdlrParams {
732 OCT1 ra,
733 GsmFrameNumber fn,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100734 hexstring imsi optional,
735 hexstring imei optional,
Harald Welte60aa5762018-03-21 19:33:13 +0100736 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200737 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200738 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200739 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100740 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200741 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100742 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200743 SCCP_PAR_Address sccp_addr_msc optional,
744 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100745 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100746 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200747 boolean aoip,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200748 boolean use_osmux_cn,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200749 boolean use_osmux_bts,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200750 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200751 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200752 integer mgwpool_idx, /* MGCP_Emulation_CT (vc_MGCP) to use for this MSC ConnHdlr */
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200753 boolean media_mgw_offer_ipv6,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200754 OCT3 last_used_eutran_plmn optional,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200755 boolean exp_fast_return, /* RR Release expected to contain CellSelectInd ? */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200756 boolean expect_channel_mode_modify,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100757 uint3_t expect_tsc optional,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100758 BSSMAP_IE_CellIdentifier cell_id_source,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100759 boolean expect_ho_fail,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100760 boolean expect_ho_fail_lchan_est,
761 boolean inter_bsc_ho_in__ho_req_in_initial_sccp_cr,
762 boolean ignore_mgw_mdcx
Harald Weltec1a2fff2017-12-17 11:06:19 +0100763};
764
Philipp Maier48604732018-10-09 15:00:37 +0200765/* Note: Do not use valueof() to get a value of this template, use
766 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100767 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100768template (value) TestHdlrParams t_def_TestHdlrPars := {
769 ra := '23'O,
770 fn := 23,
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100771 imsi := omit, /* set to random in f_gen_test_hdlr_pars() */
772 imei := omit, /* set to random in f_gen_test_hdlr_pars() */
Harald Welte60aa5762018-03-21 19:33:13 +0100773 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200774 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200775 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200776 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100777 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200778 encr := omit,
779 lcls := {
780 gcr := omit,
781 cfg := omit,
782 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100783 exp_sts := omit,
784 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100785 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200786 sccp_addr_msc := omit,
787 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100788 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100789 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200790 aoip := true,
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +0200791 use_osmux_cn := false,
Pau Espin Pedrol29c6dfb2022-08-08 18:37:56 +0200792 use_osmux_bts := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200793 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200794 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200795 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200796 rsl_idx := 0,
797 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200798 },
Pau Espin Pedrol3c630532022-10-20 19:00:11 +0200799 mgwpool_idx := 0,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200800 media_mgw_offer_ipv6 := true,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200801 last_used_eutran_plmn := omit,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200802 exp_fast_return := false,
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200803 expect_channel_mode_modify := false,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100804 expect_tsc := omit,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100805 cell_id_source := valueof(ts_CellID_LAC_CI(1, 1)),
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100806 expect_ho_fail := false,
Oliver Smith7a8594a2023-02-13 14:30:49 +0100807 expect_ho_fail_lchan_est := false,
808 inter_bsc_ho_in__ho_req_in_initial_sccp_cr := true,
809 ignore_mgw_mdcx := false
Harald Weltec1a2fff2017-12-17 11:06:19 +0100810}
811
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300812function f_create_chan_and_exp(template (present) PDU_BSSAP exp_l3_compl := ?)
813runs on MSC_ConnHdlr {
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100814 var MobileIdentityLV mi;
815 var PDU_ML3_MS_NW l3_info;
816 var octetstring l3_enc;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200817 var template uint3_t tsc := ?;
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300818 timer T;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200819
Pau Espin Pedrolb27653c2023-01-03 14:07:21 +0100820 if (ispresent(g_pars.imsi)) {
821 mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
822 } else if (ispresent(g_pars.imei)) {
823 mi := valueof(ts_MI_IMEI_LV(g_pars.imei));
824 } else {
825 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
826 "Either imsi or imei must be set!");
827 }
828 l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
829 l3_enc := enc_PDU_ML3_MS_NW(l3_info);
830
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200831 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
832 tsc := g_pars.expect_tsc;
833 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100834
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300835 if (istemplatekind(exp_l3_compl, "?")) {
836 if (g_pars.aoip == false) {
837 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := omit);
838 } else {
839 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := ?);
840 }
841 }
842
Neels Hofmeyrd601f9c2021-06-04 22:46:06 +0200843 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100844 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200845 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 +0300846 /* wait for a COMPL_L3 from the BSC to ensure that the SCCP connection is up */
847 T.start(2.0);
848 alt {
849 [] BSSAP.receive(exp_l3_compl);
850 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
851 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
852 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
853 }
854 [] T.timeout {
855 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
856 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
857 }
858 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100859}
860
Harald Welte898113b2018-01-31 18:32:21 +0100861function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200862 template (omit) RslChannelNr chan_nr := omit, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Welte898113b2018-01-31 18:32:21 +0100863 if (not isvalue(link_id)) {
864 link_id := ts_RslLinkID_DCCH(0);
865 }
866 if (not isvalue(chan_nr)) {
867 chan_nr := g_chan_nr;
868 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200869 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 +0100870}
871
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200872function 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 +0100873 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100874 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100875 if (orig.msg_type == RSL_MT_ENCR_CMD) {
876 link_id := orig.ies[2].body.link_id;
877 } else {
878 link_id := orig.ies[1].body.link_id;
879 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200880 f_rsl_send_l3(l3, link_id, chan_nr, rsl_pt := rsl_pt);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100881}
882
Oliver Smith598e1ed2021-07-09 10:28:40 +0200883/* Convert the cipher representation on BSSMAP to the representation used on RSL */
884function f_cipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100885{
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300886 select (alg_bssmap) {
887 case ('01'O) { return RSL_ALG_ID_A5_0; }
888 case ('02'O) { return RSL_ALG_ID_A5_1; }
889 case ('04'O) { return RSL_ALG_ID_A5_2; }
890 case ('08'O) { return RSL_ALG_ID_A5_3; }
891 case ('10'O) { return RSL_ALG_ID_A5_4; }
892 case ('20'O) { return RSL_ALG_ID_A5_5; }
893 case ('40'O) { return RSL_ALG_ID_A5_6; }
894 case ('80'O) { return RSL_ALG_ID_A5_7; }
895 case else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100896 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
897 oct2str(alg_bssmap));
Harald Weltee613f962018-04-18 22:38:16 +0200898 return RSL_ALG_ID_A5_0;
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300899 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100900 }
901}
902
Vadim Yanitskiyaf2972e2022-03-14 22:27:57 +0300903/* Convert the cipher representation on BSSMAP to the one used on RR (3GPP TS 44.018) */
904function f_cipher_mode_bssmap_to_rr(OCT1 alg_bssmap) return BIT3
905{
906 select (alg_bssmap) {
907 case ('01'O) /* A5/0 */ { return '000'B; } /* SC=0 */
908 case ('02'O) /* A5/1 */ { return '000'B; } /* SC=1 */
909 case ('04'O) /* A5/2 */ { return '001'B; } /* SC=1 */
910 case ('08'O) /* A5/3 */ { return '010'B; } /* SC=1 */
911 case ('10'O) /* A5/4 */ { return '011'B; } /* SC=1 */
912 case ('20'O) /* A5/5 */ { return '100'B; } /* SC=1 */
913 case ('40'O) /* A5/6 */ { return '101'B; } /* SC=1 */
914 case ('80'O) /* A5/7 */ { return '110'B; } /* SC=1 */
915 case else {
916 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
917 oct2str(alg_bssmap));
918 return '000'B;
919 }
920 }
921}
922
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200923function f_verify_encr_info(RSL_Message rsl) runs on MSC_ConnHdlr {
924 var RSL_IE_Body encr_info;
925 var RSL_AlgId alg_rsl;
926 var template octetstring expect_kc;
927
928 /* If no encryption is enabled, then make sure there is no RSL_IE_ENCR_INFO */
929 if (not ispresent(g_pars.encr)) {
930 if (f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
931 setverdict(fail, "Found Encryption IE, but expected no encryption in ", rsl.msg_type);
932 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
933 return;
934 }
935 setverdict(pass);
936 return;
937 }
938
939 /* RSL uses a different representation of the encryption algorithm,
940 * so we need to convert first */
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100941 alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200942
943 if (alg_rsl == RSL_ALG_ID_A5_4 and ispresent(g_pars.encr.enc_kc128)) {
944 expect_kc := g_pars.encr.enc_kc128;
945 } else if (alg_rsl == RSL_ALG_ID_A5_0) {
946 /* When A5/0 is chosen, no encryption is active, so technically, no key is needed. However, 3GPP TS
947 * 48.058 9.3.7 Encryption Information stays quite silent about presence or absence of a key for A5/0.
948 * The only thing specified is how to indicate the length of the key; the possibility that the key may
949 * be zero length is not explicitly mentioned. So it seems that we should always send the key along,
950 * even for A5/0. Still, let's also allow a zero length key for A5/0. */
951 expect_kc := (g_pars.encr.enc_key, ''O);
952 } else {
953 expect_kc := g_pars.encr.enc_key;
954 }
955 log("for encryption algo ", alg_rsl, " expect kc = ", expect_kc);
956
957 if (not f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
958 if (alg_rsl == RSL_ALG_ID_A5_0) {
959 /* For A5/0, encryption is not active. It is fine to omit the Encryption Information in this
960 * case. Note that the first channel may see an RSL Encryption Command with A5/0 indicated, and
961 * a subsequent handover may activate a new channel without any Encryption Information. */
962 setverdict(pass);
963 return;
964 }
965 setverdict(fail, "Missing Encryption Information IE in ", rsl.msg_type);
966 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
967 return;
968 }
969
970 if (not match(encr_info, tr_EncrInfo(alg_rsl, expect_kc))) {
971 setverdict(fail, "Unexpected Kc in Encryption Information IE in ", rsl.msg_type);
972 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
973 return;
974 }
975 setverdict(pass);
976}
977
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200978function f_cipher_mode(TestHdlrEncrParams enc, boolean exp_fail := false,
979 RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC)
Harald Welte38b2a102017-12-23 02:42:58 +0100980runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100981 var PDU_BSSAP bssap;
982 var RSL_Message rsl;
983
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200984 if (isvalue(enc.enc_kc128)) {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100985 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(enc.enc_alg_permitted, enc.enc_key, valueof(enc.enc_kc128)));
Harald Welte73cd2712017-12-17 00:44:52 +0100986 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100987 BSSAP.send(ts_BSSMAP_CipherModeCmd(enc.enc_alg_permitted, enc.enc_key));
Harald Welte73cd2712017-12-17 00:44:52 +0100988 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100989
Harald Welte73cd2712017-12-17 00:44:52 +0100990 alt {
991 /* RSL/UE Side */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200992 [] rsl_pt.receive(tr_RSL_ENCR_CMD(g_chan_nr)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100993 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
994 log("Rx L3 from net: ", l3);
Neels Hofmeyr72749342021-06-11 02:41:37 +0200995
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200996 f_verify_encr_info(rsl);
997
Harald Welte73cd2712017-12-17 00:44:52 +0100998 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200999 f_rsl_reply(ts_RRM_CiphModeCompl, rsl, rsl_pt := rsl_pt);
Harald Welte73cd2712017-12-17 00:44:52 +01001000 }
1001 repeat;
1002 }
1003 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001004 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001005 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +01001006 } else {
1007 setverdict(pass);
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001008 var RSL_AlgId alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr5e31cfc2021-06-11 02:42:11 +02001009 if (oct2int(bssap.pdu.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algorithmIdentifier) != enum2int(alg_rsl)) {
1010 setverdict(fail, "Unexpected Encryption Algorithm ID in BSSMAP Cipher Mode Complete");
1011 }
Harald Welte38b2a102017-12-23 02:42:58 +01001012 }
Harald Welte73cd2712017-12-17 00:44:52 +01001013 }
1014 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +01001015 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001016 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +01001017 } else {
1018 setverdict(pass);
1019 }
Harald Welte73cd2712017-12-17 00:44:52 +01001020 }
1021 }
1022}
1023
Harald Welte211219e2018-01-29 22:03:36 +01001024/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
1025function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
1026 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +01001027 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +01001028
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001029 select (inp) {
1030 case ('00001'B) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +01001031 chan_nr := valueof(t_RslChanNr_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001032 }
1033 case ('11101'B) { /* VAMOS TCH/F */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001034 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001035 }
1036 case ('0001?'B) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +01001037 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001038 }
1039 case ('1111?'B) { /* VAMOS TCH/H */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +02001040 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001041 }
1042 case ('001??'B) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001043 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001044 }
1045 case ('01???'B) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +01001046 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001047 }
1048 case else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001049 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +03001050 }
Harald Welte211219e2018-01-29 22:03:36 +01001051 }
1052
1053 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001054 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +01001055 } else {
1056 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
1057 arfcn := oct2int(concat);
1058 }
1059}
1060
1061type record AssignmentState {
1062 /* global */
1063 boolean voice_call,
1064 boolean is_assignment,
1065 /* Assignment related bits */
1066 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001067 boolean old_lchan_deact_sacch_seen,
1068 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +01001069 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +01001070 RslChannelNr old_chan_nr,
1071 /* Modify related bits */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001072 PDU_ML3_NW_MS rr_channel_mode_modify_msg optional,
Harald Welte211219e2018-01-29 22:03:36 +01001073 boolean rr_modify_seen,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001074 RSL_Message rsl_mode_modify_msg optional,
Harald Welte21583082018-01-29 22:28:26 +01001075 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +01001076}
1077
1078template (value) AssignmentState ts_AssignmentStateInit := {
1079 voice_call := false,
1080 is_assignment := false,
1081 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001082 old_lchan_deact_sacch_seen := false,
1083 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +01001084 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +01001085 old_chan_nr := -,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001086 rr_channel_mode_modify_msg := omit,
Harald Welte211219e2018-01-29 22:03:36 +01001087 rr_modify_seen := false,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001088 rsl_mode_modify_msg := omit,
Harald Welte21583082018-01-29 22:28:26 +01001089 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +01001090}
1091
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001092private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
1093 encr_info := {
1094 len := ?,
1095 alg_id := alg,
1096 key := key
1097 }
1098}
1099
1100/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
1101private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
1102 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001103 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001104 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001105
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001106 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg_permitted != '01'O) {
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001107 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001108 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001109 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001110 var RSL_AlgId alg := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr0588cad2021-06-11 01:38:18 +02001111 var octetstring expect_key;
1112 if (alg == RSL_ALG_ID_A5_4) {
1113 expect_key := g_pars.encr.enc_kc128;
1114 } else {
1115 expect_key := g_pars.encr.enc_key;
1116 }
1117 if (not match(encr_info, tr_EncrInfo(alg, expect_key))) {
1118 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
1119 "Unexpected Kc in Encryption IE in RSL ENCR CMD");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001120 }
1121 }
1122 } else {
1123 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
1124 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001125 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001126 }
1127 }
1128 }
1129 /* 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 +01001130 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001131
Pau Espin Pedrol8f773632019-11-05 11:46:53 +01001132 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 +01001133 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
1134 }
1135
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001136 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
1137 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
1138 } else {
1139 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 +01001140 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 +01001141 }
1142 }
1143
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001144}
1145
Neels Hofmeyr6a955cc2021-10-02 14:53:48 +02001146function rr_chan_desc_tsc(ChannelDescription2_V cd2)
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001147 return uint3_t
1148{
1149 var uint3_t tsc := oct2int(cd2.octet3);
1150 tsc := tsc / 32; /* shl 5 */
1151 return tsc;
1152}
1153
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001154altstep 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 +01001155 var RSL_Message rsl;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001156 [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 +01001157 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1158 log("Rx L3 from net: ", l3);
1159 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
1160 var RslChannelNr new_chan_nr;
1161 var GsmArfcn arfcn;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001162
1163 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1164 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime);
1165 if (not match(got_tsc, g_pars.expect_tsc)) {
1166 setverdict(fail, "RR Assignment: unexpected TSC in Channel Description: expected ",
1167 g_pars.expect_tsc, " got ", got_tsc);
1168 mtc.stop;
1169 }
1170 }
1171
Harald Welte211219e2018-01-29 22:03:36 +01001172 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
1173 new_chan_nr, arfcn);
1174 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +01001175
Harald Welte211219e2018-01-29 22:03:36 +01001176 /* register our component for this channel number at the RSL Emulation */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001177 f_rslem_register(0, new_chan_nr, PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001178 /* dispatch queued messages for this channel (if any) */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001179 f_rslem_dchan_queue_dispatch(PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001180
Harald Welte211219e2018-01-29 22:03:36 +01001181 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
1182 /* send assignment complete over the new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001183 rsl_pt.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +01001184 enc_PDU_ML3_MS_NW(l3_tx)));
1185 /* by default, send via the new channel from now */
1186 st.old_chan_nr := g_chan_nr;
1187 g_chan_nr := new_chan_nr;
1188 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001189 /* obtain channel activation from RSL_Emulation for new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001190 var RSL_Message chan_act := f_rslem_get_last_act(rsl_proc_pt, 0, g_chan_nr);
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001191 /* check it (e.g. for correct ciphering parameters) */
1192 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +01001193 repeat;
1194 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001195 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +01001196 }
Harald Welte211219e2018-01-29 22:03:36 +01001197 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001198 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001199 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001200 repeat;
1201 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001202 [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 +01001203 st.old_lchan_rll_rel_req_seen := true;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001204 rsl_pt.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
Harald Welte211219e2018-01-29 22:03:36 +01001205 repeat;
1206 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001207 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1208 rsl_pt.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +01001209 /* unregister for old channel number in RSL emulation */
1210 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001211 f_rslem_unregister(0, st.old_chan_nr, PT := rsl_proc_pt);
Harald Welte21583082018-01-29 22:28:26 +01001212 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +01001213 repeat;
1214 }
1215}
1216
1217altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
1218 /* no assignment, just mode modify */
1219 var RSL_Message rsl;
1220
1221 [st.voice_call and not st.rr_modify_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
Harald Weltec1a2fff2017-12-17 11:06:19 +01001222 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1223 log("Rx L3 from net: ", l3);
1224 if (ischosen(l3.msgs.rrm.channelModeModify)) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001225 st.rr_channel_mode_modify_msg := l3;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001226 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
1227 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +01001228 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001229 }
1230 repeat;
1231 }
Harald Welte211219e2018-01-29 22:03:36 +01001232 [st.voice_call and st.rr_modify_seen] RSL.receive(tr_RSL_MsgTypeD(RSL_MT_MODE_MODIFY_REQ)) -> value rsl {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001233 st.rsl_mode_modify_msg := rsl;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001234 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +01001235 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001236 repeat;
1237 }
Harald Welte211219e2018-01-29 22:03:36 +01001238}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001239
Harald Welte211219e2018-01-29 22:03:36 +01001240/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
1241function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
1242return boolean {
1243 select (bssmap.speechOrDataIndicator) {
1244 case ('0011'B) { /* Signalling */
1245 /* all channels support signalling */
1246 return true;
1247 }
1248 case else { /* Speech, Speech+CTM or CSD */
1249 select (bssmap.channelRateAndType) {
1250 case ('08'O) { /* TCH/F */
1251 select (rsl_chan_nr) {
1252 case (t_RslChanNr_Bm(?)) { return true; }
1253 }
1254 }
1255 case ('09'O) { /* TCH/H */
1256 select (rsl_chan_nr) {
1257 case (t_RslChanNr_Lm(?, ?)) { return true; }
1258 }
1259 }
1260 case else { /* full or half-rate */
1261 select (rsl_chan_nr) {
1262 case (t_RslChanNr_Bm(?)) { return true; }
1263 case (t_RslChanNr_Lm(?, ?)) { return true; }
1264 }
1265 }
1266 }
Harald Weltec1a2fff2017-12-17 11:06:19 +01001267 }
Harald Welte211219e2018-01-29 22:03:36 +01001268 }
1269 return false;
1270}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001271
Philipp Maier8ef527e2018-05-18 11:12:50 +02001272/* Determine if the channel mode specified within rsl_chan_nr requires a
1273 * MODE MODIFY in to match the channel mode specified by given BSSMAP
1274 * ChannelType */
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001275function f_channel_needs_modify(TELNETasp_PT vty, BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
Philipp Maier8ef527e2018-05-18 11:12:50 +02001276return boolean {
1277
Philipp Maier8ef527e2018-05-18 11:12:50 +02001278 var boolean current_signalling := false;
1279 var boolean desired_signalling := false;
1280
1281 select (rsl_chan_nr) {
1282 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
1283 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001284 case (t_RslChanNr_Bm(?)) {
1285 /* TCH/F, always subslot 0 */
1286 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn) & " 0");
1287 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1288 current_signalling := true;
1289 }
1290 }
1291 case (t_RslChanNr_Lm(?, ?)) {
1292 /* TCH/H */
1293 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn)
1294 & " " & int2str(rsl_chan_nr.u.lm.sub_chan));
1295 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1296 current_signalling := true;
1297 }
1298 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001299 }
1300
1301 if (bssmap.speechOrDataIndicator == '0011'B) {
1302 desired_signalling := true;
1303 }
1304
1305 if (current_signalling == desired_signalling) {
1306 /* The desired channel mode is equal to the one we currently
1307 * have, there is no mode modification needed or expected */
1308 return false;
1309 } else {
1310 /* The desired channel mode and the current channel mode do
1311 * not match. A mode modification is required */
1312 return true;
1313 }
1314}
1315
Harald Weltecc0b0142018-05-29 15:19:33 +02001316/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
1317function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
1318 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
1319 if (istemplatekind(ass_tpl, "omit")) {
1320 return;
1321 }
1322 if (ispresent(g_pars.lcls.gcr)) {
1323 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
1324 }
1325 if (ispresent(g_pars.lcls.cfg)) {
1326 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1327 }
1328 if (ispresent(g_pars.lcls.csc)) {
1329 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1330 }
Harald Welte343b7742018-06-05 23:41:37 +02001331 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1332 if (ispresent(g_pars.lcls.exp_sts)) {
1333 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1334 } else {
1335 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1336 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001337 }
1338}
1339
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001340/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1341function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1342 template MgcpResponse resp_any := ?
1343 var MgcpResponse resp_val;
1344 resp.line.trans_id := cmd.line.trans_id;
1345 timer T := 5.0;
1346
1347 BSSAP.send(cmd);
1348 T.start;
1349 alt {
1350 [] as_Media_mgw();
1351 [] BSSAP.receive(resp) -> value resp_val { }
1352 [] BSSAP.receive(resp_any) {
1353 setverdict(fail, "Response didn't match template");
1354 mtc.stop;
1355 }
1356 [] BSSAP.receive { repeat; }
1357 [] T.timeout {
1358 setverdict(fail, "Timeout waiting for response to ", cmd);
1359 mtc.stop;
1360 }
1361 }
1362 T.stop;
1363}
1364
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001365/* Helper function to check if the activity on the MGCP matches what we
1366 * expected */
1367function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1368 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001369 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1370 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1371 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1372
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001373 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001374 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 +02001375 }
1376 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001377 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 +02001378 }
1379 }
1380}
1381
Harald Welte211219e2018-01-29 22:03:36 +01001382/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001383function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1384runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001385
1386 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001387 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001388
Philipp Maier4ce978c2020-08-11 22:54:07 +02001389 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001390 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1391 } else {
1392 /* Make sure a meaningful default is assigned in case the
1393 * codecList is not populated */
1394 codecType := FR_AMR;
1395 }
1396
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001397 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001398
Harald Weltecc0b0142018-05-29 15:19:33 +02001399 /* patch in the LCLS related items, as needed */
1400 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1401
Harald Welte7a14fd52018-05-10 22:26:55 +02001402 f_create_chan_and_exp();
Harald Welte7a14fd52018-05-10 22:26:55 +02001403
1404 /* start ciphering, if requested */
1405 if (ispresent(g_pars.encr)) {
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001406 f_cipher_mode(g_pars.encr);
Harald Welte7a14fd52018-05-10 22:26:55 +02001407 }
1408
1409 /* bail out early if no assignment requested */
1410 if (istemplatekind(ass_tpl, "omit")) {
1411 return;
1412 }
1413
1414 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001415 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001416 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001417 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001418 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001419
Harald Welte211219e2018-01-29 22:03:36 +01001420 var ExpectCriteria mgcpcrit := {
1421 connid := omit,
1422 endpoint := omit,
1423 transid := omit
1424 };
1425 var AssignmentState st := valueof(ts_AssignmentStateInit);
1426 /* if the channel type is SIGNAL, we're not handling a voice call */
1427 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
1428 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001429 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001430 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001431
Harald Welte211219e2018-01-29 22:03:36 +01001432 /* determine if the current channel can support the given service or not */
1433 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1434 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001435
1436 /* We decided to assign a new channel, so we do not expect
1437 * any mode modify messages on RSL */
1438 exp_modify := false;
1439 } else {
1440 /* We will continue working with the currently assigned
1441 * channel, we must now check if the mode of the current
1442 * channel is compatible. If not we expect the BSC to modify
1443 * the mode */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001444 st.is_assignment := false;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001445 exp_modify := f_channel_needs_modify(BSCVTY, ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001446 }
1447
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001448 /* Some test situations will involve MGCP transactions on a media
1449 * gateway. Depending on the situation, we set up how many of each MGCP
1450 * message type are expected to be exchanged. */
1451 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1452 /* For tests that expect the assignment to fail, assume that
1453 * there will be no MGW communication as well. */
1454 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1455 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1456 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1457 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
1458 } else if (st.voice_call) {
1459 /* For voice calls we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001460 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1461 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1462 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1463 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001464 }
1465
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001466 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1467 * a channel and send us RR Assignment Command together with ip.access CRCX.
1468 * There is a risk that the RSL Emulation component would dequeue and process
1469 * ip.access CRCX faster than we process the Assignment Command and register
1470 * the corresponding handler for the indicated RSL channel number. This would
1471 * result in a failure, because at that moment there will be no handler for
1472 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1473 f_rslem_dchan_queue_enable();
1474
Harald Welte211219e2018-01-29 22:03:36 +01001475 f_create_mgcp_expect(mgcpcrit);
1476 BSSAP.send(ass_cmd);
1477
1478 T.start;
1479 alt {
1480 /* assignment related bits */
1481 [st.is_assignment] as_assignment(st);
1482
1483 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001484 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001485
1486 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
1487 [st.voice_call] as_Media();
1488
1489 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001490 [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 +01001491 setverdict(pass);
1492 }
Philipp Maier86f39202018-02-07 14:40:09 +01001493 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1494 setverdict(pass);
1495 }
Harald Welte21583082018-01-29 22:28:26 +01001496 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001497 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001498 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001499 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001500 }
1501 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001502 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001503 }
1504 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001505 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001506 }
1507 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001508 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001509 }
1510 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001511 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001512 }
1513 }
Harald Welte211219e2018-01-29 22:03:36 +01001514 log("g_media ", g_media);
1515 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001516 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001517 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001518
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001519 if (exp_modify) {
1520 /* Verify that the RR Channel Mode Modify and RSL MODE MODIFY message asked for the expected channel
1521 * mode. */
1522 /* TODO: more precisely expect the different types of speech? */
1523 var OCT1 rr_channel_mode := st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelMode.mode;
1524
1525 if (st.voice_call and rr_channel_mode == '00'O) {
1526 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1527 " to a speech mode, but got channelMode == ", rr_channel_mode);
1528 mtc.stop;
1529 } else if (not st.voice_call and rr_channel_mode != '00'O) {
1530 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1531 " to signalling mode, but got channelMode == ", rr_channel_mode);
1532 mtc.stop;
1533 }
1534
1535 var RSL_IE_Body chan_mode_ie;
1536 if (not f_rsl_find_ie(st.rsl_mode_modify_msg, RSL_IE_CHAN_MODE, chan_mode_ie)) {
1537 setverdict(fail, "RSL MODE MODIFY message lacks a Channel Mode IE");
1538 mtc.stop;
1539 }
1540 var RSL_SpeechDataInd rsl_spd_ind := chan_mode_ie.chan_mode.spd_ind;
1541 if (st.voice_call and rsl_spd_ind != RSL_SPDI_SPEECH) {
1542 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1543 " to a speech mode, but got spd_ind == ", rsl_spd_ind);
1544 mtc.stop;
1545 } else if (not st.voice_call and rsl_spd_ind != RSL_SPDI_SIGN) {
1546 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1547 " to signalling mode, but got spd_ind == ", rsl_spd_ind);
1548 mtc.stop;
1549 }
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001550
1551 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1552 var uint3_t got_tsc := rr_chan_desc_tsc(st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelDescription);
1553 if (not match(got_tsc, g_pars.expect_tsc)) {
1554 setverdict(fail, "RR Channel Mode Modify: unexpected TSC in Channel Description: expected ",
1555 g_pars.expect_tsc, " got ", got_tsc);
1556 mtc.stop;
1557 }
1558 }
1559
1560 } else {
1561 /* not exp_modify, so this did a Channel Activate */
1562
1563 /* Check the TSC */
1564 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1565 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
1566 var RSL_IE_Body ie;
1567 if (f_rsl_find_ie(chan_act, RSL_IE_CHAN_IDENT, ie)) {
1568 var uint3_t got_tsc := ie.chan_ident.ch_desc.v.tsc;
1569 if (not match(got_tsc, g_pars.expect_tsc)) {
1570 setverdict(fail, "RSL CHANnel ACTIVation: unexpected TSC in Channel Description: expected ",
1571 g_pars.expect_tsc, " got ", got_tsc);
1572 mtc.stop;
1573 }
1574 }
1575 }
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001576 }
1577
Philipp Maiera0976e92018-07-16 15:19:42 +02001578 /* When the BSC detects that LCLS is possible it will cross the
1579 * connetions that point to the PBX side of the MGW. In our case this
1580 * is mgcp_conn[1]. The BSC performs this operation already before the
1581 * assignment complete is generated. This means we expect another MDCX
1582 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001583 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 +02001584 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1585 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1586
1587 }
1588 }
1589
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001590 if (not exp_fail and st.voice_call and not g_pars.aoip) {
1591 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1592 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1593 connection. BSC will forward it to its MGW. */
1594 var template MgcpCommand cmd;
1595 var template MgcpResponse resp;
1596 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1597 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1598 var MgcpCallId call_id := '51234'H;
1599 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
Pau Espin Pedrolf967afc2022-08-08 18:17:20 +02001600 if (g_pars.use_osmux_cn) {
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001601 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1602 resp := tr_CRCX_ACK_osmux;
1603 } else {
1604 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1605 resp := tr_CRCX_ACK;
1606 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001607 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001608 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1609 { valueof(ts_SDP_ptime(20)) });
1610 mgcp_transceive_mgw(cmd, resp);
1611 }
1612
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001613 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001614
1615 if (st.is_assignment and st.assignment_done) {
1616 if (not st.old_lchan_deact_sacch_seen) {
1617 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1618 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1619 }
1620 if (st.old_lchan_rll_rel_req_seen) {
1621 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1622 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1623 }
1624 }
Neels Hofmeyr9d686302021-04-25 19:02:29 +00001625
1626 var charstring lchan_info := f_vty_transceive_ret(BSCVTY, "show lchan 0 0 " & int2str(g_chan_nr.tn) & " 0");
1627 if (f_strstr(lchan_info, "State: ESTABLISHED") < 0) {
1628 log("after f_establish_fully(), 'show lchan' replied: ", lchan_info);
1629 setverdict(fail, "lchan is not in state ESTABLISHED");
1630 mtc.stop;
1631 }
Harald Welte930d0a72018-03-22 22:08:40 +01001632}
1633
Harald Welte261af4b2018-02-12 21:20:39 +01001634type record HandoverState {
1635 /* Assignment related bits */
1636 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001637 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001638 boolean handover_done,
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001639 RslChannelNr old_chan_nr,
1640 uint3_t expect_target_tsc optional
Harald Welte261af4b2018-02-12 21:20:39 +01001641};
1642
1643altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1644 var RSL_Message rsl;
1645 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1646 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1647 log("Rx L3 from net: ", l3);
1648 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1649 var RslChannelNr new_chan_nr;
1650 var GsmArfcn arfcn;
1651 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1652 new_chan_nr, arfcn);
1653 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1654
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001655 /* Verify correct TSC in handoverCommand */
1656 if (ispresent(st.expect_target_tsc)) {
1657 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.handoverCommand.channelDescription2);
1658 if (not match(got_tsc, st.expect_target_tsc)) {
1659 setverdict(fail, "RR Handover Command: unexpected TSC in Channel Description: expected ",
1660 st.expect_target_tsc, " got ", got_tsc);
1661 mtc.stop;
1662 } else {
1663 log("handoverCommand: verified TSC = ", got_tsc, " (matches ",
1664 st.expect_target_tsc, ")");
1665 }
1666 }
1667
Harald Welte261af4b2018-02-12 21:20:39 +01001668 /* register our component for this channel number at the RSL Emulation */
1669 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1670
1671 /* resume processing of RSL DChan messages, which was temporarily suspended
1672 * before performing a hand-over */
1673 f_rslem_resume(RSL1_PROC);
1674
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001675 /* send handover detect */
1676 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1677 f_sleep(0.3);
1678
Harald Welte261af4b2018-02-12 21:20:39 +01001679 /* send handover complete over the new channel */
1680 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001681 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001682 enc_PDU_ML3_MS_NW(l3_tx)));
1683 /* by default, send via the new channel from now */
1684 st.old_chan_nr := g_chan_nr;
1685 g_chan_nr := new_chan_nr;
1686 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001687
1688 /* Memorize how many MDCX we have seen before. We need this number to detect
1689 * that we have received the handover related MDCX */
1690 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001691 repeat;
1692 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001693 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001694 }
1695 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001696 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001697 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001698 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1699 repeat;
1700 }
Harald Welte261af4b2018-02-12 21:20:39 +01001701 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1702 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1703 repeat;
1704 }
1705 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1706 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1707 /* unregister for old channel number in RSL emulation */
1708 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1709 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001710
1711 /* The channel release must not necessarly be synchronized to the RSL handover
1712 * procedure since those events may happen independently nearly at the same
1713 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1714 * IPACC or MGCP level may be still in progress. In order to make sure that
1715 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001716 * as well as an MDCX on IPACC level.
1717 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1718 if (g_media.bts.ipa_crcx_seen
1719 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1720 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001721 repeat;
1722 } else {
1723 st.handover_done := true;
1724 }
Harald Welte261af4b2018-02-12 21:20:39 +01001725 }
1726}
1727
1728
Harald Weltec1a2fff2017-12-17 11:06:19 +01001729
Harald Welte28d943e2017-11-25 15:00:50 +01001730}