blob: 06f9ebe5ed15fb2cc950e531af60f9138f74919f [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
Philipp Maier11a58942018-06-25 16:40:48 +020055/* Get the matching payload type for a specified BSSAP codec type
56 * (see also: BSSAP_Types.ttcn */
57private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
58 if (codecType == GSM_FR) {
59 return PT_GSM;
60 } else if (codecType == GSM_HR) {
61 return PT_GSMHR;
62 } else if (codecType == GSM_EFR) {
63 return PT_GSMEFR;
64 } else if (codecType == FR_AMR or codecType == HR_AMR) {
65 return PT_AMR;
66 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
67 return PT_AMRWB;
68 }
69
70 return PT_PCMU;
71}
72
Harald Welte211219e2018-01-29 22:03:36 +010073/* Tuple containing host/ip and port */
74type record HostPort {
75 HostName host,
76 PortNumber port_nr
77};
78
79/* State encapsulating one MGCP Connection */
80type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020081 integer crcx_seen, /* Counts how many CRCX operations happend */
82 integer mdcx_seen, /* Counts how many MDCX operations happend C */
83 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
84 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010085 MgcpConnectionId conn_id,
86 charstring mime_type, /* e.g. AMR */
87 integer sample_rate, /* 8000 */
88 integer ptime, /* 20 */
89 uint7_t rtp_pt, /* RTP Payload Type */
90 HostPort mgw, /* MGW side */
91 HostPort peer /* CA side */
92};
93
94/* BTS media state */
95type record BtsMediaState {
96 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +020097 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +010098 uint16_t conn_id,
99 uint7_t rtp_pt,
100 HostPort bts,
101 HostPort peer
102};
103
104type record MediaState {
105 MgcpEndpoint mgcp_ep,
106 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +0100107 BtsMediaState bts,
108 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +0100109};
110
Philipp Maier11a58942018-06-25 16:40:48 +0200111function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +0100112 /* BTS Side */
113 g_media.bts := {
114 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200115 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +0100116 conn_id := nr,
117 rtp_pt := 0,
118 bts := {
119 host := bts,
120 port_nr := 9000 + nr*2
121 },
122 peer := -
123 }
124
Harald Welte261af4b2018-02-12 21:20:39 +0100125 g_media.bts1 := {
126 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200127 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100128 conn_id := nr,
129 rtp_pt := 0,
130 bts := {
131 host := bts, /* FIXME */
132 port_nr := 9000 + nr*2
133 },
134 peer := -
135 }
136
Harald Welte363cb0a2018-01-30 19:35:53 +0100137 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100138
139 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200140 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100141 g_media.mgcp_conn[i].sample_rate := 8000;
142 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200143 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200144 g_media.mgcp_conn[i].crcx_seen := 0;
145 g_media.mgcp_conn[i].mdcx_seen := 0;
146 g_media.mgcp_conn[i].crcx_seen_exp := 0;
147 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100148 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
149 }
150
151 g_media.mgcp_conn[0].mgw := {
152 host := mgw,
153 port_nr := 10000 + nr*2
154 }
155 g_media.mgcp_conn[1].mgw := {
156 host := mgw,
157 port_nr := 11000 + nr*2
158 }
159}
160
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200161/* Helper function to get the next free MGCP connection identifier. We can
162 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100163private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
164 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200165 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100166 return i;
167 }
168 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100169 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Only 2 Connections per EP!");
170 /* Should never be reached */
171 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100172}
173
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200174/* Helper function to pick a specific connection by its cid. Since we reach out
175 * for a connection that is in-use we also check if there was already exactly
176 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100177private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
178 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200179 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 +0100180 return i;
181 }
182 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100183 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No Connection for ID ", cid));
184 /* Should not be reached */
185 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100186}
187
Philipp Maierb2422352018-07-11 10:36:57 +0200188/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
189 * RSL level media handling */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200190altstep 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 +0100191 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100192 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100193 var boolean b_unused;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200194 [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 +0100195 /* Extract parameters from request + use in response */
196 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
197 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
198 }
199 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
200 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
201 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200202 rsl_pt.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200203 f_inet_addr(g_media.bts.bts.host),
Harald Welte211219e2018-01-29 22:03:36 +0100204 g_media.bts.bts.port_nr,
205 g_media.bts.rtp_pt));
206 g_media.bts.ipa_crcx_seen := true;
207 repeat;
208 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200209 [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 +0100210 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100211 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100212 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100213 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100214 }
215 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100216 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200217 g_media.bts.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100218 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100219 g_media.bts.peer.port_nr := ie.ipa_remote_port;
220 /* optional */
221 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
222 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
223 }
224 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
225 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
226 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200227 rsl_pt.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200228 f_inet_addr(g_media.bts.peer.host),
Harald Welte211219e2018-01-29 22:03:36 +0100229 g_media.bts.peer.port_nr,
230 g_media.bts.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200231 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100232 repeat;
233 }
Harald Welte261af4b2018-02-12 21:20:39 +0100234
235 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200236 [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 +0100237 /* Extract parameters from request + use in response */
238 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
239 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
240 }
241 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
242 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
243 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200244 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 +0200245 f_inet_addr(g_media.bts1.bts.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100246 g_media.bts1.bts.port_nr,
247 g_media.bts1.rtp_pt));
248 g_media.bts1.ipa_crcx_seen := true;
249 repeat;
250 }
251 /* on second (new) BTS during hand-over */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200252 [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 +0100253 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100254 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100255 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100256 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100257 }
258 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100259 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Vadim Yanitskiyfc631642021-07-03 02:42:45 +0200260 g_media.bts1.peer.host := f_inet_ntoa(ie.ipa_remote_ip);
Harald Welte930d0a72018-03-22 22:08:40 +0100261 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100262 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
263 /* optional */
264 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
265 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
266 }
267 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
268 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
269 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200270 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 +0200271 f_inet_addr(g_media.bts1.peer.host),
Harald Welte261af4b2018-02-12 21:20:39 +0100272 g_media.bts1.peer.port_nr,
273 g_media.bts1.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200274 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100275 repeat;
276 }
277
Philipp Maierb2422352018-07-11 10:36:57 +0200278
279}
280
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200281function f_rx_crcx(MgcpCommand mgcp_cmd)
282 runs on MSC_ConnHdlr return template MgcpResponse {
283 var MgcpOsmuxCID osmux_cid;
284 var SDP_Message sdp;
285 var integer cid := f_get_free_mgcp_conn();
Philipp Maiera2083892020-09-21 14:18:36 +0200286 var charstring local_rtp_addr;
287
288 if (g_pars.media_mgw_offer_ipv6 == true) {
289 local_rtp_addr := host_mgw_rtp_v6; /* Use IPv6 by default if no remote addr is provided by client */
290 } else {
291 local_rtp_addr := host_mgw_rtp_v4; /* Use IPv4 by default if no remote addr is provided by client */
292 }
293
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200294 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
295 if (cid != 0) {
296 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
297 }
298 /* we keep the endpoint name allocated during MediaState_init */
299 } else {
300 /* Call Agent allocated endpoint, trust/use it always */
301 g_media.mgcp_ep := mgcp_cmd.line.ep;
302 }
303 if (isvalue(mgcp_cmd.sdp)) {
304 sdp := mgcp_cmd.sdp;
305 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
306 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 +0200307 if (sdp.connection.addr_type == "IP6") {
308 local_rtp_addr := host_mgw_rtp_v6;
309 } else {
310 local_rtp_addr := host_mgw_rtp_v4;
311 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200312 }
313 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200314 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200315 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
316 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
317 mgcp_conn.mime_type & "/" &
318 int2str(mgcp_conn.sample_rate))),
319 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
320 var template MgcpResponse mgcp_resp;
321 if (g_pars.use_osmux and f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
322 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
323 mgcp_resp := ts_CRCX_ACK_osmux(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, osmux_cid, sdp);
324 } else {
325 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
326 }
327 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
328 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
329 return mgcp_resp;
330}
331
332function f_rx_mdcx(MgcpCommand mgcp_cmd)
333 runs on MSC_ConnHdlr return template MgcpResponse {
334 var SDP_Message sdp;
335 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200336 var charstring local_rtp_addr;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200337 if (isvalue(mgcp_cmd.sdp)) {
338 sdp := mgcp_cmd.sdp;
339 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
340 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 +0200341 if (sdp.connection.addr_type == "IP6") {
342 local_rtp_addr := host_mgw_rtp_v6;
343 } else {
344 local_rtp_addr := host_mgw_rtp_v4;
345 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200346 } else {
347 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
348 }
349 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200350 sdp := valueof(ts_SDP(mgcp_conn.peer.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200351 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
352 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
353 mgcp_conn.mime_type & "/" &
354 int2str(mgcp_conn.sample_rate))),
355 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
356 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
357 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
358}
359
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200360function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
361runs on MSC_ConnHdlr return template MGCP_RecvFrom {
362 var template MGCP_RecvFrom mrf := {
363 connId := ?,
364 remName := ?,
365 remPort := ?,
366 locName := ?,
367 locPort := ?,
368 msg := msg
369 }
370 return mrf;
371}
372
Philipp Maierb2422352018-07-11 10:36:57 +0200373/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
374 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200375
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200376altstep as_Media_mgw(boolean norepeat := false, boolean fail_on_dlcx := true) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200377 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200378 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200379 var MGCP_RecvFrom mrf;
380 var template MgcpMessage msg_crcx := {
381 command := tr_CRCX
382 }
383 var template MgcpMessage msg_mdcx := {
384 command := tr_MDCX
385 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200386 var template MgcpMessage msg_dlcx := {
387 command := tr_DLCX
388 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200389 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200390
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200391 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200392 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100393 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200394 if(norepeat == false) {
395 repeat;
396 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200397 }
398
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200399 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
400 mgcp_resp := f_rx_crcx(mrf.msg.command);
401 msg_resp := {
402 response := mgcp_resp
403 }
404 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
405 if(norepeat == false) {
406 repeat;
407 }
408 }
409
410 [g_pars.aoip] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200411 mgcp_resp := f_rx_mdcx(mgcp_cmd);
412 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200413 if(norepeat == false) {
414 repeat;
415 }
Harald Welte211219e2018-01-29 22:03:36 +0100416 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200417
418 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
419 mgcp_resp := f_rx_mdcx(mrf.msg.command);
420 msg_resp := {
421 response := mgcp_resp
422 }
423 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
424 if(norepeat == false) {
425 repeat;
426 }
427 }
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200428
429 [fail_on_dlcx and g_pars.aoip] MGCP.receive(tr_DLCX) {
430 setverdict(fail, "Unexpected DLCX received");
431 }
432
433 [fail_on_dlcx and not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_dlcx)) {
434 setverdict(fail, "Unexpected DLCX received");
435 }
Harald Welte211219e2018-01-29 22:03:36 +0100436}
437
Philipp Maierb2422352018-07-11 10:36:57 +0200438/* Altsteps for handling of media related commands. Can be activated by a given
439 * test case if it expects to see media related handling (i.e. voice calls) */
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200440altstep as_Media(boolean fail_on_dlcx := true) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200441 [] as_Media_ipacc();
Neels Hofmeyr9b320c12022-04-07 00:19:01 +0200442 [] as_Media_mgw(fail_on_dlcx := fail_on_dlcx);
Philipp Maierb2422352018-07-11 10:36:57 +0200443}
Harald Welte211219e2018-01-29 22:03:36 +0100444
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200445type port Coord_PT message
446{
447 inout charstring;
448} with { extension "internal" };
449
Harald Welte28d943e2017-11-25 15:00:50 +0100450/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200451 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
452 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
Harald Welte47cd0e32020-08-21 12:39:11 +0200453type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr, BSSAP_LE_ConnHdlr, StatsD_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100454 /* SCCP Connecction Identifier for the underlying SCCP connection */
455 var integer g_sccp_conn_id;
456
Harald Welte6811d102019-04-14 22:23:14 +0200457 /* procedure port back to our parent (RAN_Emulation_CT) for control */
458 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100459 port TELNETasp_PT BSCVTY;
Neels Hofmeyrd36a82f2021-07-20 22:10:49 +0200460 port Coord_PT COORD;
Vadim Yanitskiy5f14d342022-02-07 14:18:38 +0600461 port Coord_PT COORD2;
Harald Welte28d943e2017-11-25 15:00:50 +0100462
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200463 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
464 port IPA_MGCP_PT MGCP_MSC_CLIENT;
465 var integer g_trans_id := 0;
466
Harald Welte211219e2018-01-29 22:03:36 +0100467 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100468 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100469
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200470 var charstring host_bts := "127.0.0.2";
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200471 var charstring host_mgw_mgcp := "127.0.0.3";
472 var charstring host_mgw_rtp_v4 := "127.0.0.5";
473 var charstring host_mgw_rtp_v6 := "::1";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200474 var charstring host_msc := "127.0.0.4";
475
Harald Weltee97eab42018-03-21 18:46:06 +0100476 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100477}
478
Harald Welteeddf0e92020-06-21 19:42:15 +0200479function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100480 if (not g_vty_initialized) {
481 map(self:BSCVTY, system:BSCVTY);
482 f_vty_set_prompts(BSCVTY);
483 f_vty_transceive(BSCVTY, "enable");
484 g_vty_initialized := true;
485 }
Harald Welte28d943e2017-11-25 15:00:50 +0100486}
487
Harald Welteeddf0e92020-06-21 19:42:15 +0200488/* initialize all parameters */
489function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
490 f_MediaState_init(g_media, i, bts, mgw, codecType);
491 f_MscConnHdlr_init_vty();
492}
493
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200494private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
495 var MgcpTransId tid := int2str(g_trans_id);
496 g_trans_id := g_trans_id + 1;
497 return tid;
498}
499
Harald Welte6811d102019-04-14 22:23:14 +0200500/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100501 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
502private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200503runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100504 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200505 var boolean append_osmux_support := g_ran_ops.use_osmux and
506 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100507
Harald Weltec1a2fff2017-12-17 11:06:19 +0100508 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200509 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
510 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100511 }
512
513 return resp;
514}
515
Harald Welte47cd0e32020-08-21 12:39:11 +0200516/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
517 * BSSAP_LE message arrives. Can return a PDU_BSSAP_LE that should be sent in return */
518private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
519runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
520 var template PDU_BSSAP_LE resp := omit;
521
522 /* answer all RESET with a RESET ACK */
523 if (match(bssap, tr_BSSMAP_LE_Reset)) {
524 resp := ts_BSSMAP_LE_ResetAck;
525 }
526
527 return resp;
528}
529
Harald Welte6811d102019-04-14 22:23:14 +0200530const RanOps MSC_RanOps := {
531 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100532 unitdata_cb := refers(UnitdataCallback),
533 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100534 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200535 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200536 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200537 use_osmux := false,
Eric Wild66448c12022-04-02 14:57:53 +0200538 bssap_reset_retries := 1,
Harald Welte930d0a72018-03-22 22:08:40 +0100539 sccp_addr_local := omit,
540 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100541}
542
Harald Welte47cd0e32020-08-21 12:39:11 +0200543const BssapLeOps SMLC_BssapLeOps := {
544 create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
545 unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
546 decode_dtap := false,
547 role_ms := false,
548 sccp_addr_local := omit,
549 sccp_addr_peer := omit
550}
551
Daniel Willmann191e0d92018-01-17 12:44:35 +0100552const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100553 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
554 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100555}
556
Harald Weltec1a2fff2017-12-17 11:06:19 +0100557/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200558function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200559 RAN.call(RAN_register:{l3_enc, self}) {
560 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100561 }
562}
563
Harald Welte651fcdc2018-05-10 20:23:16 +0200564type record TestHdlrEncrParams {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100565 /* A mask of multiple encryption algorithms, for Encryption Information IE. */
566 OCT1 enc_alg_permitted,
567 /* Expect this encryption algorithm in Channel Activation from the BSC.
568 * To expect no encryption, set to '01'O == A5/0. */
569 OCT1 enc_alg_expect,
570 /* In BSSMAP, the Chosen Encryption Algorithm IE that the test sends to the BSC.
571 * When set to omit, the MSC will not send a Chosen Encryption Algorithm IE. */
572 OCT1 enc_alg_chosen optional,
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200573 octetstring enc_key,
574 octetstring enc_kc128 optional
Harald Welte651fcdc2018-05-10 20:23:16 +0200575};
576
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100577template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg_permitted,
578 octetstring key, template (omit) octetstring kc128 := omit) := {
579 enc_alg_permitted := alg_permitted,
580 enc_alg_expect := alg_permitted, /* <- for compat with tests before enc_alg_expect was added */
581 enc_alg_chosen := alg_permitted, /* <- for compat with tests before enc_alg_chosen was added */
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200582 enc_key := key,
583 enc_kc128 := kc128
Harald Welte651fcdc2018-05-10 20:23:16 +0200584}
585
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100586template (value) TestHdlrEncrParams t_EncrParams2(OCT1 alg_permitted,
587 OCT1 alg_expect,
588 template(omit) OCT1 alg_chosen,
589 octetstring key,
590 template (omit) octetstring kc128 := omit) := {
591 enc_alg_permitted := alg_permitted,
592 enc_alg_expect := alg_expect,
593 enc_alg_chosen := alg_chosen,
594 enc_key := key,
595 enc_kc128 := kc128
596}
597
598/* Convenience for common t_EncrParams2 usage.
599 *
600 * To test a scenario where only A5/3 is permitted:
601 * f_encr_params('08'O);
602 * To test a scenario where A5/3 is chosen from A5/0,A5/1,A5/3 (1 | 2 | 8 = 11 = 0xb):
603 * f_encr_params('0b'O, '08'O);
604 * To test the same, but the Chosen Encryption Algorithm IE should reflect A5/1:
605 * f_encr_params('0b'O, '08'O, '02'O);
606 * To test the same, but the MSC sends no Chosen Encryption Algorithm IE:
607 * f_encr_params('0b'O, '08'O, omit);
608 *
609 * Set alg_chosen and alg_expect magically when == '00'O:
610 * - enc_alg_expect is set to alg_chosen if present, else to alg_permitted.
611 * - enc_alg_chosen is set to alg_permitted.
612 * When only alg_permitted is given, alg_permitted must reflect only one algorithm, or f_cipher_mode_bssmap_to_rsl()
613 * will fail.
614 * When alg_chosen is passed as omit, the messages from the MSC will not contain a Chosen Encryption Algorithm IE.
615 */
616function f_encr_params(OCT1 alg_permitted, OCT1 alg_expect := '00'O, template (omit) OCT1 alg_chosen := '00'O,
617 boolean kc128 := false)
618 return TestHdlrEncrParams
619{
620 if (not istemplatekind(alg_chosen, "omit")) {
621 if (valueof(alg_chosen) == '00'O) {
622 if (alg_expect != '00'O) {
623 alg_chosen := alg_expect;
624 } else {
625 /* In this case, alg_permitted should have only one permitted algo */
626 alg_chosen := alg_permitted;
627 }
628 }
629 if (alg_expect == '00'O) {
630 alg_expect := valueof(alg_chosen);
631 }
632 }
633 if (alg_expect == '00'O) {
634 /* In this case, alg_permitted should have only one permitted algo */
635 alg_expect := valueof(alg_permitted);
636 }
Vadim Yanitskiy35c9a332022-03-16 20:26:35 +0300637 var template (omit) octetstring kc := omit;
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100638 if (kc128) {
639 kc := f_rnd_octstring(16);
640 }
641 return valueof(t_EncrParams2(alg_permitted, alg_expect, alg_chosen, f_rnd_octstring(8), kc));
642}
643
Harald Weltecc0b0142018-05-29 15:19:33 +0200644type record TestHdlrParamsLcls {
645 GlobalCallReferenceValue gcr optional,
646 /* LCLS Configuration */
647 BIT4 cfg optional,
648 /* LCLS Connection Status Control */
649 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100650 BIT4 exp_sts optional,
651 /* Whether to adjust *cx_seen_exp for LCLS tests */
652 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200653}
654
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200655type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200656 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200657 integer rsl_idx,
658 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200659}
660
Harald Weltec1a2fff2017-12-17 11:06:19 +0100661type record TestHdlrParams {
662 OCT1 ra,
663 GsmFrameNumber fn,
664 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100665 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200666 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200667 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200668 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100669 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200670 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100671 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200672 SCCP_PAR_Address sccp_addr_msc optional,
673 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100674 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100675 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200676 boolean aoip,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200677 boolean use_osmux,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200678 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200679 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200680 boolean media_mgw_offer_ipv6,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200681 OCT3 last_used_eutran_plmn optional,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200682 boolean exp_fast_return, /* RR Release expected to contain CellSelectInd ? */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200683 boolean expect_channel_mode_modify,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100684 uint3_t expect_tsc optional,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100685 BSSMAP_IE_CellIdentifier cell_id_source,
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100686 boolean expect_ho_fail,
687 boolean inter_bsc_ho_in__ho_req_in_initial_sccp_cr
Harald Weltec1a2fff2017-12-17 11:06:19 +0100688};
689
Philipp Maier48604732018-10-09 15:00:37 +0200690/* Note: Do not use valueof() to get a value of this template, use
691 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100692 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100693template (value) TestHdlrParams t_def_TestHdlrPars := {
694 ra := '23'O,
695 fn := 23,
696 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100697 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200698 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200699 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200700 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100701 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200702 encr := omit,
703 lcls := {
704 gcr := omit,
705 cfg := omit,
706 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100707 exp_sts := omit,
708 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100709 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200710 sccp_addr_msc := omit,
711 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100712 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100713 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200714 aoip := true,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200715 use_osmux := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200716 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200717 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200718 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200719 rsl_idx := 0,
720 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200721 },
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200722 media_mgw_offer_ipv6 := true,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200723 last_used_eutran_plmn := omit,
Pau Espin Pedrol9ae36d52021-06-15 17:29:43 +0200724 exp_fast_return := false,
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200725 expect_channel_mode_modify := false,
Pau Espin Pedrola8ef3be2022-02-16 16:21:17 +0100726 expect_tsc := omit,
Neels Hofmeyr907b23b2022-02-17 21:58:47 +0100727 cell_id_source := valueof(ts_CellID_LAC_CI(1, 1)),
Neels Hofmeyra23f3b12022-03-02 19:57:12 +0100728 expect_ho_fail := false,
729 inter_bsc_ho_in__ho_req_in_initial_sccp_cr := true
Harald Weltec1a2fff2017-12-17 11:06:19 +0100730}
731
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300732function f_create_chan_and_exp(template (present) PDU_BSSAP exp_l3_compl := ?)
733runs on MSC_ConnHdlr {
Harald Weltea0630032018-03-20 21:09:55 +0100734 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100735 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100736 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200737 var template uint3_t tsc := ?;
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300738 timer T;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200739
740 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
741 tsc := g_pars.expect_tsc;
742 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100743
Vadim Yanitskiya7fc5a62021-12-04 20:10:08 +0300744 if (istemplatekind(exp_l3_compl, "?")) {
745 if (g_pars.aoip == false) {
746 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := omit);
747 } else {
748 exp_l3_compl := tr_BSSMAP_ComplL3(l3_enc, codec_list := ?);
749 }
750 }
751
Neels Hofmeyrd601f9c2021-06-04 22:46:06 +0200752 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100753 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +0200754 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 +0300755 /* wait for a COMPL_L3 from the BSC to ensure that the SCCP connection is up */
756 T.start(2.0);
757 alt {
758 [] BSSAP.receive(exp_l3_compl);
759 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
760 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
761 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
762 }
763 [] T.timeout {
764 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
765 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
766 }
767 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100768}
769
Harald Welte898113b2018-01-31 18:32:21 +0100770function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200771 template (omit) RslChannelNr chan_nr := omit, RSL_DCHAN_PT rsl_pt := RSL) runs on MSC_ConnHdlr {
Harald Welte898113b2018-01-31 18:32:21 +0100772 if (not isvalue(link_id)) {
773 link_id := ts_RslLinkID_DCCH(0);
774 }
775 if (not isvalue(chan_nr)) {
776 chan_nr := g_chan_nr;
777 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200778 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 +0100779}
780
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200781function 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 +0100782 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100783 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100784 if (orig.msg_type == RSL_MT_ENCR_CMD) {
785 link_id := orig.ies[2].body.link_id;
786 } else {
787 link_id := orig.ies[1].body.link_id;
788 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200789 f_rsl_send_l3(l3, link_id, chan_nr, rsl_pt := rsl_pt);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100790}
791
Oliver Smith598e1ed2021-07-09 10:28:40 +0200792/* Convert the cipher representation on BSSMAP to the representation used on RSL */
793function f_cipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100794{
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300795 select (alg_bssmap) {
796 case ('01'O) { return RSL_ALG_ID_A5_0; }
797 case ('02'O) { return RSL_ALG_ID_A5_1; }
798 case ('04'O) { return RSL_ALG_ID_A5_2; }
799 case ('08'O) { return RSL_ALG_ID_A5_3; }
800 case ('10'O) { return RSL_ALG_ID_A5_4; }
801 case ('20'O) { return RSL_ALG_ID_A5_5; }
802 case ('40'O) { return RSL_ALG_ID_A5_6; }
803 case ('80'O) { return RSL_ALG_ID_A5_7; }
804 case else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100805 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
806 oct2str(alg_bssmap));
Harald Weltee613f962018-04-18 22:38:16 +0200807 return RSL_ALG_ID_A5_0;
Vadim Yanitskiy4483a222022-03-14 22:07:43 +0300808 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100809 }
810}
811
Vadim Yanitskiyaf2972e2022-03-14 22:27:57 +0300812/* Convert the cipher representation on BSSMAP to the one used on RR (3GPP TS 44.018) */
813function f_cipher_mode_bssmap_to_rr(OCT1 alg_bssmap) return BIT3
814{
815 select (alg_bssmap) {
816 case ('01'O) /* A5/0 */ { return '000'B; } /* SC=0 */
817 case ('02'O) /* A5/1 */ { return '000'B; } /* SC=1 */
818 case ('04'O) /* A5/2 */ { return '001'B; } /* SC=1 */
819 case ('08'O) /* A5/3 */ { return '010'B; } /* SC=1 */
820 case ('10'O) /* A5/4 */ { return '011'B; } /* SC=1 */
821 case ('20'O) /* A5/5 */ { return '100'B; } /* SC=1 */
822 case ('40'O) /* A5/6 */ { return '101'B; } /* SC=1 */
823 case ('80'O) /* A5/7 */ { return '110'B; } /* SC=1 */
824 case else {
825 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm: " &
826 oct2str(alg_bssmap));
827 return '000'B;
828 }
829 }
830}
831
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200832function f_verify_encr_info(RSL_Message rsl) runs on MSC_ConnHdlr {
833 var RSL_IE_Body encr_info;
834 var RSL_AlgId alg_rsl;
835 var template octetstring expect_kc;
836
837 /* If no encryption is enabled, then make sure there is no RSL_IE_ENCR_INFO */
838 if (not ispresent(g_pars.encr)) {
839 if (f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
840 setverdict(fail, "Found Encryption IE, but expected no encryption in ", rsl.msg_type);
841 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
842 return;
843 }
844 setverdict(pass);
845 return;
846 }
847
848 /* RSL uses a different representation of the encryption algorithm,
849 * so we need to convert first */
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100850 alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200851
852 if (alg_rsl == RSL_ALG_ID_A5_4 and ispresent(g_pars.encr.enc_kc128)) {
853 expect_kc := g_pars.encr.enc_kc128;
854 } else if (alg_rsl == RSL_ALG_ID_A5_0) {
855 /* When A5/0 is chosen, no encryption is active, so technically, no key is needed. However, 3GPP TS
856 * 48.058 9.3.7 Encryption Information stays quite silent about presence or absence of a key for A5/0.
857 * The only thing specified is how to indicate the length of the key; the possibility that the key may
858 * be zero length is not explicitly mentioned. So it seems that we should always send the key along,
859 * even for A5/0. Still, let's also allow a zero length key for A5/0. */
860 expect_kc := (g_pars.encr.enc_key, ''O);
861 } else {
862 expect_kc := g_pars.encr.enc_key;
863 }
864 log("for encryption algo ", alg_rsl, " expect kc = ", expect_kc);
865
866 if (not f_rsl_find_ie(rsl, RSL_IE_ENCR_INFO, encr_info)) {
867 if (alg_rsl == RSL_ALG_ID_A5_0) {
868 /* For A5/0, encryption is not active. It is fine to omit the Encryption Information in this
869 * case. Note that the first channel may see an RSL Encryption Command with A5/0 indicated, and
870 * a subsequent handover may activate a new channel without any Encryption Information. */
871 setverdict(pass);
872 return;
873 }
874 setverdict(fail, "Missing Encryption Information IE in ", rsl.msg_type);
875 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
876 return;
877 }
878
879 if (not match(encr_info, tr_EncrInfo(alg_rsl, expect_kc))) {
880 setverdict(fail, "Unexpected Kc in Encryption Information IE in ", rsl.msg_type);
881 Misc_Helpers.f_shutdown(__BFILE__, __LINE__);
882 return;
883 }
884 setverdict(pass);
885}
886
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200887function f_cipher_mode(TestHdlrEncrParams enc, boolean exp_fail := false,
888 RSL_DCHAN_PT rsl_pt := RSL, RSLEM_PROC_PT rsl_proc_pt := RSL_PROC)
Harald Welte38b2a102017-12-23 02:42:58 +0100889runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100890 var PDU_BSSAP bssap;
891 var RSL_Message rsl;
892
Neels Hofmeyr6c388f22021-06-11 02:36:56 +0200893 if (isvalue(enc.enc_kc128)) {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100894 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(enc.enc_alg_permitted, enc.enc_key, valueof(enc.enc_kc128)));
Harald Welte73cd2712017-12-17 00:44:52 +0100895 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100896 BSSAP.send(ts_BSSMAP_CipherModeCmd(enc.enc_alg_permitted, enc.enc_key));
Harald Welte73cd2712017-12-17 00:44:52 +0100897 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100898
Harald Welte73cd2712017-12-17 00:44:52 +0100899 alt {
900 /* RSL/UE Side */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200901 [] rsl_pt.receive(tr_RSL_ENCR_CMD(g_chan_nr)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100902 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
903 log("Rx L3 from net: ", l3);
Neels Hofmeyr72749342021-06-11 02:41:37 +0200904
Neels Hofmeyr58ffceb2021-06-21 02:17:30 +0200905 f_verify_encr_info(rsl);
906
Harald Welte73cd2712017-12-17 00:44:52 +0100907 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +0200908 f_rsl_reply(ts_RRM_CiphModeCompl, rsl, rsl_pt := rsl_pt);
Harald Welte73cd2712017-12-17 00:44:52 +0100909 }
910 repeat;
911 }
912 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100913 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100914 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +0100915 } else {
916 setverdict(pass);
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +0100917 var RSL_AlgId alg_rsl := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr5e31cfc2021-06-11 02:42:11 +0200918 if (oct2int(bssap.pdu.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algorithmIdentifier) != enum2int(alg_rsl)) {
919 setverdict(fail, "Unexpected Encryption Algorithm ID in BSSMAP Cipher Mode Complete");
920 }
Harald Welte38b2a102017-12-23 02:42:58 +0100921 }
Harald Welte73cd2712017-12-17 00:44:52 +0100922 }
923 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100924 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100925 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +0100926 } else {
927 setverdict(pass);
928 }
Harald Welte73cd2712017-12-17 00:44:52 +0100929 }
930 }
931}
932
Harald Welte211219e2018-01-29 22:03:36 +0100933/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
934function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
935 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100936 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100937
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300938 select (inp) {
939 case ('00001'B) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100940 chan_nr := valueof(t_RslChanNr_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300941 }
942 case ('11101'B) { /* VAMOS TCH/F */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +0200943 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Bm(tn));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300944 }
945 case ('0001?'B) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100946 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300947 }
948 case ('1111?'B) { /* VAMOS TCH/H */
Neels Hofmeyr9c3b82f2021-05-27 00:38:59 +0200949 chan_nr := valueof(t_RslChanNr_Osmo_VAMOS_Lm(tn, bit2int(substr(inp, 4, 1))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300950 }
951 case ('001??'B) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100952 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300953 }
954 case ('01???'B) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100955 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300956 }
957 case else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100958 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Vadim Yanitskiy2bcea3d2021-11-18 19:13:48 +0300959 }
Harald Welte211219e2018-01-29 22:03:36 +0100960 }
961
962 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100963 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +0100964 } else {
965 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
966 arfcn := oct2int(concat);
967 }
968}
969
970type record AssignmentState {
971 /* global */
972 boolean voice_call,
973 boolean is_assignment,
974 /* Assignment related bits */
975 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100976 boolean old_lchan_deact_sacch_seen,
977 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +0100978 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100979 RslChannelNr old_chan_nr,
980 /* Modify related bits */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200981 PDU_ML3_NW_MS rr_channel_mode_modify_msg optional,
Harald Welte211219e2018-01-29 22:03:36 +0100982 boolean rr_modify_seen,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200983 RSL_Message rsl_mode_modify_msg optional,
Harald Welte21583082018-01-29 22:28:26 +0100984 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100985}
986
987template (value) AssignmentState ts_AssignmentStateInit := {
988 voice_call := false,
989 is_assignment := false,
990 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100991 old_lchan_deact_sacch_seen := false,
992 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100993 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100994 old_chan_nr := -,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200995 rr_channel_mode_modify_msg := omit,
Harald Welte211219e2018-01-29 22:03:36 +0100996 rr_modify_seen := false,
Neels Hofmeyr559d5d02021-04-16 16:50:49 +0200997 rsl_mode_modify_msg := omit,
Harald Welte21583082018-01-29 22:28:26 +0100998 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100999}
1000
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001001private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
1002 encr_info := {
1003 len := ?,
1004 alg_id := alg,
1005 key := key
1006 }
1007}
1008
1009/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
1010private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
1011 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001012 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001013 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001014
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001015 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg_permitted != '01'O) {
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001016 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001017 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001018 } else {
Neels Hofmeyrd23e8a02022-02-17 01:55:59 +01001019 var RSL_AlgId alg := f_cipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg_expect);
Neels Hofmeyr0588cad2021-06-11 01:38:18 +02001020 var octetstring expect_key;
1021 if (alg == RSL_ALG_ID_A5_4) {
1022 expect_key := g_pars.encr.enc_kc128;
1023 } else {
1024 expect_key := g_pars.encr.enc_key;
1025 }
1026 if (not match(encr_info, tr_EncrInfo(alg, expect_key))) {
1027 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
1028 "Unexpected Kc in Encryption IE in RSL ENCR CMD");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001029 }
1030 }
1031 } else {
1032 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
1033 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001034 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001035 }
1036 }
1037 }
1038 /* 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 +01001039 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +01001040
Pau Espin Pedrol8f773632019-11-05 11:46:53 +01001041 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 +01001042 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
1043 }
1044
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +01001045 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
1046 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
1047 } else {
1048 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 +01001049 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 +01001050 }
1051 }
1052
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001053}
1054
Neels Hofmeyr6a955cc2021-10-02 14:53:48 +02001055function rr_chan_desc_tsc(ChannelDescription2_V cd2)
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001056 return uint3_t
1057{
1058 var uint3_t tsc := oct2int(cd2.octet3);
1059 tsc := tsc / 32; /* shl 5 */
1060 return tsc;
1061}
1062
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001063altstep 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 +01001064 var RSL_Message rsl;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001065 [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 +01001066 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1067 log("Rx L3 from net: ", l3);
1068 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
1069 var RslChannelNr new_chan_nr;
1070 var GsmArfcn arfcn;
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001071
1072 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1073 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime);
1074 if (not match(got_tsc, g_pars.expect_tsc)) {
1075 setverdict(fail, "RR Assignment: unexpected TSC in Channel Description: expected ",
1076 g_pars.expect_tsc, " got ", got_tsc);
1077 mtc.stop;
1078 }
1079 }
1080
Harald Welte211219e2018-01-29 22:03:36 +01001081 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
1082 new_chan_nr, arfcn);
1083 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +01001084
Harald Welte211219e2018-01-29 22:03:36 +01001085 /* register our component for this channel number at the RSL Emulation */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001086 f_rslem_register(0, new_chan_nr, PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001087 /* dispatch queued messages for this channel (if any) */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001088 f_rslem_dchan_queue_dispatch(PT := rsl_proc_pt);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001089
Harald Welte211219e2018-01-29 22:03:36 +01001090 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
1091 /* send assignment complete over the new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001092 rsl_pt.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +01001093 enc_PDU_ML3_MS_NW(l3_tx)));
1094 /* by default, send via the new channel from now */
1095 st.old_chan_nr := g_chan_nr;
1096 g_chan_nr := new_chan_nr;
1097 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001098 /* obtain channel activation from RSL_Emulation for new channel */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001099 var RSL_Message chan_act := f_rslem_get_last_act(rsl_proc_pt, 0, g_chan_nr);
Harald Welte8a9bf6f2018-05-10 22:00:49 +02001100 /* check it (e.g. for correct ciphering parameters) */
1101 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +01001102 repeat;
1103 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001104 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +01001105 }
Harald Welte211219e2018-01-29 22:03:36 +01001106 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001107 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001108 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001109 repeat;
1110 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001111 [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 +01001112 st.old_lchan_rll_rel_req_seen := true;
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001113 rsl_pt.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
Harald Welte211219e2018-01-29 22:03:36 +01001114 repeat;
1115 }
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001116 [st.rr_ass_cmpl_seen] rsl_pt.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1117 rsl_pt.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +01001118 /* unregister for old channel number in RSL emulation */
1119 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Neels Hofmeyr3222e0e2021-07-20 22:25:38 +02001120 f_rslem_unregister(0, st.old_chan_nr, PT := rsl_proc_pt);
Harald Welte21583082018-01-29 22:28:26 +01001121 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +01001122 repeat;
1123 }
1124}
1125
1126altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
1127 /* no assignment, just mode modify */
1128 var RSL_Message rsl;
1129
1130 [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 +01001131 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1132 log("Rx L3 from net: ", l3);
1133 if (ischosen(l3.msgs.rrm.channelModeModify)) {
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001134 st.rr_channel_mode_modify_msg := l3;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001135 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
1136 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +01001137 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001138 }
1139 repeat;
1140 }
Harald Welte211219e2018-01-29 22:03:36 +01001141 [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 +02001142 st.rsl_mode_modify_msg := rsl;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001143 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +01001144 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +01001145 repeat;
1146 }
Harald Welte211219e2018-01-29 22:03:36 +01001147}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001148
Harald Welte211219e2018-01-29 22:03:36 +01001149/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
1150function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
1151return boolean {
1152 select (bssmap.speechOrDataIndicator) {
1153 case ('0011'B) { /* Signalling */
1154 /* all channels support signalling */
1155 return true;
1156 }
1157 case else { /* Speech, Speech+CTM or CSD */
1158 select (bssmap.channelRateAndType) {
1159 case ('08'O) { /* TCH/F */
1160 select (rsl_chan_nr) {
1161 case (t_RslChanNr_Bm(?)) { return true; }
1162 }
1163 }
1164 case ('09'O) { /* TCH/H */
1165 select (rsl_chan_nr) {
1166 case (t_RslChanNr_Lm(?, ?)) { return true; }
1167 }
1168 }
1169 case else { /* full or half-rate */
1170 select (rsl_chan_nr) {
1171 case (t_RslChanNr_Bm(?)) { return true; }
1172 case (t_RslChanNr_Lm(?, ?)) { return true; }
1173 }
1174 }
1175 }
Harald Weltec1a2fff2017-12-17 11:06:19 +01001176 }
Harald Welte211219e2018-01-29 22:03:36 +01001177 }
1178 return false;
1179}
Daniel Willmann191e0d92018-01-17 12:44:35 +01001180
Philipp Maier8ef527e2018-05-18 11:12:50 +02001181/* Determine if the channel mode specified within rsl_chan_nr requires a
1182 * MODE MODIFY in to match the channel mode specified by given BSSMAP
1183 * ChannelType */
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001184function f_channel_needs_modify(TELNETasp_PT vty, BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
Philipp Maier8ef527e2018-05-18 11:12:50 +02001185return boolean {
1186
Philipp Maier8ef527e2018-05-18 11:12:50 +02001187 var boolean current_signalling := false;
1188 var boolean desired_signalling := false;
1189
1190 select (rsl_chan_nr) {
1191 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
1192 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001193 case (t_RslChanNr_Bm(?)) {
1194 /* TCH/F, always subslot 0 */
1195 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn) & " 0");
1196 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1197 current_signalling := true;
1198 }
1199 }
1200 case (t_RslChanNr_Lm(?, ?)) {
1201 /* TCH/H */
1202 var charstring res := f_vty_transceive_ret(vty, "show lchan 0 0 " & int2str(rsl_chan_nr.tn)
1203 & " " & int2str(rsl_chan_nr.u.lm.sub_chan));
1204 if (f_strstr(res, "Channel Mode / Codec: SIGNALLING", 0) >= 0) {
1205 current_signalling := true;
1206 }
1207 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001208 }
1209
1210 if (bssmap.speechOrDataIndicator == '0011'B) {
1211 desired_signalling := true;
1212 }
1213
1214 if (current_signalling == desired_signalling) {
1215 /* The desired channel mode is equal to the one we currently
1216 * have, there is no mode modification needed or expected */
1217 return false;
1218 } else {
1219 /* The desired channel mode and the current channel mode do
1220 * not match. A mode modification is required */
1221 return true;
1222 }
1223}
1224
Harald Weltecc0b0142018-05-29 15:19:33 +02001225/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
1226function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
1227 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
1228 if (istemplatekind(ass_tpl, "omit")) {
1229 return;
1230 }
1231 if (ispresent(g_pars.lcls.gcr)) {
1232 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
1233 }
1234 if (ispresent(g_pars.lcls.cfg)) {
1235 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1236 }
1237 if (ispresent(g_pars.lcls.csc)) {
1238 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1239 }
Harald Welte343b7742018-06-05 23:41:37 +02001240 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1241 if (ispresent(g_pars.lcls.exp_sts)) {
1242 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1243 } else {
1244 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1245 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001246 }
1247}
1248
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001249/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1250function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1251 template MgcpResponse resp_any := ?
1252 var MgcpResponse resp_val;
1253 resp.line.trans_id := cmd.line.trans_id;
1254 timer T := 5.0;
1255
1256 BSSAP.send(cmd);
1257 T.start;
1258 alt {
1259 [] as_Media_mgw();
1260 [] BSSAP.receive(resp) -> value resp_val { }
1261 [] BSSAP.receive(resp_any) {
1262 setverdict(fail, "Response didn't match template");
1263 mtc.stop;
1264 }
1265 [] BSSAP.receive { repeat; }
1266 [] T.timeout {
1267 setverdict(fail, "Timeout waiting for response to ", cmd);
1268 mtc.stop;
1269 }
1270 }
1271 T.stop;
1272}
1273
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001274/* Helper function to check if the activity on the MGCP matches what we
1275 * expected */
1276function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1277 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001278 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1279 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1280 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1281
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001282 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001283 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 +02001284 }
1285 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001286 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 +02001287 }
1288 }
1289}
1290
Harald Welte211219e2018-01-29 22:03:36 +01001291/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001292function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1293runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001294
1295 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001296 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001297
Philipp Maier4ce978c2020-08-11 22:54:07 +02001298 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001299 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1300 } else {
1301 /* Make sure a meaningful default is assigned in case the
1302 * codecList is not populated */
1303 codecType := FR_AMR;
1304 }
1305
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001306 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001307
Harald Weltecc0b0142018-05-29 15:19:33 +02001308 /* patch in the LCLS related items, as needed */
1309 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1310
Harald Welte7a14fd52018-05-10 22:26:55 +02001311 f_create_chan_and_exp();
Harald Welte7a14fd52018-05-10 22:26:55 +02001312
1313 /* start ciphering, if requested */
1314 if (ispresent(g_pars.encr)) {
Neels Hofmeyr6c388f22021-06-11 02:36:56 +02001315 f_cipher_mode(g_pars.encr);
Harald Welte7a14fd52018-05-10 22:26:55 +02001316 }
1317
1318 /* bail out early if no assignment requested */
1319 if (istemplatekind(ass_tpl, "omit")) {
1320 return;
1321 }
1322
1323 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001324 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001325 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001326 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001327 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001328
Harald Welte211219e2018-01-29 22:03:36 +01001329 var ExpectCriteria mgcpcrit := {
1330 connid := omit,
1331 endpoint := omit,
1332 transid := omit
1333 };
1334 var AssignmentState st := valueof(ts_AssignmentStateInit);
1335 /* if the channel type is SIGNAL, we're not handling a voice call */
1336 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
1337 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001338 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001339 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001340
Harald Welte211219e2018-01-29 22:03:36 +01001341 /* determine if the current channel can support the given service or not */
1342 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1343 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001344
1345 /* We decided to assign a new channel, so we do not expect
1346 * any mode modify messages on RSL */
1347 exp_modify := false;
1348 } else {
1349 /* We will continue working with the currently assigned
1350 * channel, we must now check if the mode of the current
1351 * channel is compatible. If not we expect the BSC to modify
1352 * the mode */
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001353 st.is_assignment := false;
Neels Hofmeyrc2425cc2021-04-19 13:19:55 +00001354 exp_modify := f_channel_needs_modify(BSCVTY, ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001355 }
1356
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001357 /* Some test situations will involve MGCP transactions on a media
1358 * gateway. Depending on the situation, we set up how many of each MGCP
1359 * message type are expected to be exchanged. */
1360 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1361 /* For tests that expect the assignment to fail, assume that
1362 * there will be no MGW communication as well. */
1363 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1364 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1365 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1366 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
1367 } else if (st.voice_call) {
1368 /* For voice calls we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001369 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1370 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1371 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1372 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001373 }
1374
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001375 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1376 * a channel and send us RR Assignment Command together with ip.access CRCX.
1377 * There is a risk that the RSL Emulation component would dequeue and process
1378 * ip.access CRCX faster than we process the Assignment Command and register
1379 * the corresponding handler for the indicated RSL channel number. This would
1380 * result in a failure, because at that moment there will be no handler for
1381 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1382 f_rslem_dchan_queue_enable();
1383
Harald Welte211219e2018-01-29 22:03:36 +01001384 f_create_mgcp_expect(mgcpcrit);
1385 BSSAP.send(ass_cmd);
1386
1387 T.start;
1388 alt {
1389 /* assignment related bits */
1390 [st.is_assignment] as_assignment(st);
1391
1392 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001393 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001394
1395 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
1396 [st.voice_call] as_Media();
1397
1398 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001399 [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 +01001400 setverdict(pass);
1401 }
Philipp Maier86f39202018-02-07 14:40:09 +01001402 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1403 setverdict(pass);
1404 }
Harald Welte21583082018-01-29 22:28:26 +01001405 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001406 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001407 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001408 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001409 }
1410 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001411 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001412 }
1413 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001414 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001415 }
1416 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001417 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001418 }
1419 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001420 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001421 }
1422 }
Harald Welte211219e2018-01-29 22:03:36 +01001423 log("g_media ", g_media);
1424 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001425 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001426 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001427
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001428 if (exp_modify) {
1429 /* Verify that the RR Channel Mode Modify and RSL MODE MODIFY message asked for the expected channel
1430 * mode. */
1431 /* TODO: more precisely expect the different types of speech? */
1432 var OCT1 rr_channel_mode := st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelMode.mode;
1433
1434 if (st.voice_call and rr_channel_mode == '00'O) {
1435 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1436 " to a speech mode, but got channelMode == ", rr_channel_mode);
1437 mtc.stop;
1438 } else if (not st.voice_call and rr_channel_mode != '00'O) {
1439 setverdict(fail, "f_establish_fully(): Expected RR Channel Mode Modify",
1440 " to signalling mode, but got channelMode == ", rr_channel_mode);
1441 mtc.stop;
1442 }
1443
1444 var RSL_IE_Body chan_mode_ie;
1445 if (not f_rsl_find_ie(st.rsl_mode_modify_msg, RSL_IE_CHAN_MODE, chan_mode_ie)) {
1446 setverdict(fail, "RSL MODE MODIFY message lacks a Channel Mode IE");
1447 mtc.stop;
1448 }
1449 var RSL_SpeechDataInd rsl_spd_ind := chan_mode_ie.chan_mode.spd_ind;
1450 if (st.voice_call and rsl_spd_ind != RSL_SPDI_SPEECH) {
1451 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1452 " to a speech mode, but got spd_ind == ", rsl_spd_ind);
1453 mtc.stop;
1454 } else if (not st.voice_call and rsl_spd_ind != RSL_SPDI_SIGN) {
1455 setverdict(fail, "f_establish_fully(): Expected RSL MODE MODIFY",
1456 " to signalling mode, but got spd_ind == ", rsl_spd_ind);
1457 mtc.stop;
1458 }
Neels Hofmeyrb5b7a6e2021-06-04 19:03:45 +02001459
1460 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1461 var uint3_t got_tsc := rr_chan_desc_tsc(st.rr_channel_mode_modify_msg.msgs.rrm.channelModeModify.channelDescription);
1462 if (not match(got_tsc, g_pars.expect_tsc)) {
1463 setverdict(fail, "RR Channel Mode Modify: unexpected TSC in Channel Description: expected ",
1464 g_pars.expect_tsc, " got ", got_tsc);
1465 mtc.stop;
1466 }
1467 }
1468
1469 } else {
1470 /* not exp_modify, so this did a Channel Activate */
1471
1472 /* Check the TSC */
1473 if (not istemplatekind(g_pars.expect_tsc, "omit")) {
1474 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
1475 var RSL_IE_Body ie;
1476 if (f_rsl_find_ie(chan_act, RSL_IE_CHAN_IDENT, ie)) {
1477 var uint3_t got_tsc := ie.chan_ident.ch_desc.v.tsc;
1478 if (not match(got_tsc, g_pars.expect_tsc)) {
1479 setverdict(fail, "RSL CHANnel ACTIVation: unexpected TSC in Channel Description: expected ",
1480 g_pars.expect_tsc, " got ", got_tsc);
1481 mtc.stop;
1482 }
1483 }
1484 }
Neels Hofmeyr559d5d02021-04-16 16:50:49 +02001485 }
1486
Philipp Maiera0976e92018-07-16 15:19:42 +02001487 /* When the BSC detects that LCLS is possible it will cross the
1488 * connetions that point to the PBX side of the MGW. In our case this
1489 * is mgcp_conn[1]. The BSC performs this operation already before the
1490 * assignment complete is generated. This means we expect another MDCX
1491 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001492 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 +02001493 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1494 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1495
1496 }
1497 }
1498
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001499 if (not exp_fail and st.voice_call and not g_pars.aoip) {
1500 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1501 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1502 connection. BSC will forward it to its MGW. */
1503 var template MgcpCommand cmd;
1504 var template MgcpResponse resp;
1505 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1506 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1507 var MgcpCallId call_id := '51234'H;
1508 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
1509 if (g_pars.use_osmux) {
1510 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1511 resp := tr_CRCX_ACK_osmux;
1512 } else {
1513 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1514 resp := tr_CRCX_ACK;
1515 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001516 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001517 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1518 { valueof(ts_SDP_ptime(20)) });
1519 mgcp_transceive_mgw(cmd, resp);
1520 }
1521
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001522 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001523
1524 if (st.is_assignment and st.assignment_done) {
1525 if (not st.old_lchan_deact_sacch_seen) {
1526 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1527 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1528 }
1529 if (st.old_lchan_rll_rel_req_seen) {
1530 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1531 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1532 }
1533 }
Neels Hofmeyr9d686302021-04-25 19:02:29 +00001534
1535 var charstring lchan_info := f_vty_transceive_ret(BSCVTY, "show lchan 0 0 " & int2str(g_chan_nr.tn) & " 0");
1536 if (f_strstr(lchan_info, "State: ESTABLISHED") < 0) {
1537 log("after f_establish_fully(), 'show lchan' replied: ", lchan_info);
1538 setverdict(fail, "lchan is not in state ESTABLISHED");
1539 mtc.stop;
1540 }
Harald Welte930d0a72018-03-22 22:08:40 +01001541}
1542
Harald Welte261af4b2018-02-12 21:20:39 +01001543type record HandoverState {
1544 /* Assignment related bits */
1545 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001546 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001547 boolean handover_done,
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001548 RslChannelNr old_chan_nr,
1549 uint3_t expect_target_tsc optional
Harald Welte261af4b2018-02-12 21:20:39 +01001550};
1551
1552altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1553 var RSL_Message rsl;
1554 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1555 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1556 log("Rx L3 from net: ", l3);
1557 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1558 var RslChannelNr new_chan_nr;
1559 var GsmArfcn arfcn;
1560 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1561 new_chan_nr, arfcn);
1562 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1563
Neels Hofmeyrc741fcb2021-10-02 14:52:28 +02001564 /* Verify correct TSC in handoverCommand */
1565 if (ispresent(st.expect_target_tsc)) {
1566 var uint3_t got_tsc := rr_chan_desc_tsc(l3.msgs.rrm.handoverCommand.channelDescription2);
1567 if (not match(got_tsc, st.expect_target_tsc)) {
1568 setverdict(fail, "RR Handover Command: unexpected TSC in Channel Description: expected ",
1569 st.expect_target_tsc, " got ", got_tsc);
1570 mtc.stop;
1571 } else {
1572 log("handoverCommand: verified TSC = ", got_tsc, " (matches ",
1573 st.expect_target_tsc, ")");
1574 }
1575 }
1576
Harald Welte261af4b2018-02-12 21:20:39 +01001577 /* register our component for this channel number at the RSL Emulation */
1578 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1579
1580 /* resume processing of RSL DChan messages, which was temporarily suspended
1581 * before performing a hand-over */
1582 f_rslem_resume(RSL1_PROC);
1583
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001584 /* send handover detect */
1585 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1586 f_sleep(0.3);
1587
Harald Welte261af4b2018-02-12 21:20:39 +01001588 /* send handover complete over the new channel */
1589 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001590 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001591 enc_PDU_ML3_MS_NW(l3_tx)));
1592 /* by default, send via the new channel from now */
1593 st.old_chan_nr := g_chan_nr;
1594 g_chan_nr := new_chan_nr;
1595 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001596
1597 /* Memorize how many MDCX we have seen before. We need this number to detect
1598 * that we have received the handover related MDCX */
1599 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001600 repeat;
1601 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001602 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001603 }
1604 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001605 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001606 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001607 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1608 repeat;
1609 }
Harald Welte261af4b2018-02-12 21:20:39 +01001610 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1611 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1612 repeat;
1613 }
1614 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1615 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1616 /* unregister for old channel number in RSL emulation */
1617 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1618 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001619
1620 /* The channel release must not necessarly be synchronized to the RSL handover
1621 * procedure since those events may happen independently nearly at the same
1622 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1623 * IPACC or MGCP level may be still in progress. In order to make sure that
1624 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001625 * as well as an MDCX on IPACC level.
1626 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1627 if (g_media.bts.ipa_crcx_seen
1628 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1629 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001630 repeat;
1631 } else {
1632 st.handover_done := true;
1633 }
Harald Welte261af4b2018-02-12 21:20:39 +01001634 }
1635}
1636
1637
Harald Weltec1a2fff2017-12-17 11:06:19 +01001638
Harald Welte28d943e2017-11-25 15:00:50 +01001639}