blob: 3d6538ab847705410fcb220153aa464f928ba499 [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 Welte28d943e2017-11-25 15:00:50 +010022import from BSSMAP_Templates all;
23
Harald Welte211219e2018-01-29 22:03:36 +010024import from IPL4asp_Types all;
25import from Native_Functions all;
26
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +020027import from MGCP_CodecPort all;
Harald Welte28d943e2017-11-25 15:00:50 +010028import from MGCP_Types all;
29import from MGCP_Templates all;
Daniel Willmann191e0d92018-01-17 12:44:35 +010030import from MGCP_Emulation all;
Harald Welte28d943e2017-11-25 15:00:50 +010031import from SDP_Types all;
32
Daniel Willmannebdecc02020-08-12 15:30:17 +020033import from StatsD_Checker all;
34
Harald Weltec1a2fff2017-12-17 11:06:19 +010035import from RSL_Emulation all;
36import from RSL_Types all;
37
38import from MobileL3_Types all;
39import from MobileL3_CommonIE_Types all;
Harald Welte211219e2018-01-29 22:03:36 +010040import from MobileL3_RRM_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +010041import from L3_Templates all;
42
Harald Weltec20b1c42018-02-12 20:50:08 +010043import from TELNETasp_PortType all;
44import from Osmocom_VTY_Functions all;
45
Harald Weltec1a2fff2017-12-17 11:06:19 +010046
Harald Welte211219e2018-01-29 22:03:36 +010047/***********************************************************************
48 * Media related handling
49 ***********************************************************************/
50
Philipp Maier11a58942018-06-25 16:40:48 +020051/* Get the matching payload type for a specified BSSAP codec type
52 * (see also: BSSAP_Types.ttcn */
53private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
54 if (codecType == GSM_FR) {
55 return PT_GSM;
56 } else if (codecType == GSM_HR) {
57 return PT_GSMHR;
58 } else if (codecType == GSM_EFR) {
59 return PT_GSMEFR;
60 } else if (codecType == FR_AMR or codecType == HR_AMR) {
61 return PT_AMR;
62 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
63 return PT_AMRWB;
64 }
65
66 return PT_PCMU;
67}
68
Harald Welte211219e2018-01-29 22:03:36 +010069/* Tuple containing host/ip and port */
70type record HostPort {
71 HostName host,
72 PortNumber port_nr
73};
74
75/* State encapsulating one MGCP Connection */
76type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020077 integer crcx_seen, /* Counts how many CRCX operations happend */
78 integer mdcx_seen, /* Counts how many MDCX operations happend C */
79 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
80 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010081 MgcpConnectionId conn_id,
82 charstring mime_type, /* e.g. AMR */
83 integer sample_rate, /* 8000 */
84 integer ptime, /* 20 */
85 uint7_t rtp_pt, /* RTP Payload Type */
86 HostPort mgw, /* MGW side */
87 HostPort peer /* CA side */
88};
89
90/* BTS media state */
91type record BtsMediaState {
92 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +020093 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +010094 uint16_t conn_id,
95 uint7_t rtp_pt,
96 HostPort bts,
97 HostPort peer
98};
99
100type record MediaState {
101 MgcpEndpoint mgcp_ep,
102 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +0100103 BtsMediaState bts,
104 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +0100105};
106
Philipp Maier11a58942018-06-25 16:40:48 +0200107function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +0100108 /* BTS Side */
109 g_media.bts := {
110 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200111 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +0100112 conn_id := nr,
113 rtp_pt := 0,
114 bts := {
115 host := bts,
116 port_nr := 9000 + nr*2
117 },
118 peer := -
119 }
120
Harald Welte261af4b2018-02-12 21:20:39 +0100121 g_media.bts1 := {
122 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200123 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100124 conn_id := nr,
125 rtp_pt := 0,
126 bts := {
127 host := bts, /* FIXME */
128 port_nr := 9000 + nr*2
129 },
130 peer := -
131 }
132
Harald Welte363cb0a2018-01-30 19:35:53 +0100133 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100134
135 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200136 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100137 g_media.mgcp_conn[i].sample_rate := 8000;
138 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200139 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200140 g_media.mgcp_conn[i].crcx_seen := 0;
141 g_media.mgcp_conn[i].mdcx_seen := 0;
142 g_media.mgcp_conn[i].crcx_seen_exp := 0;
143 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100144 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
145 }
146
147 g_media.mgcp_conn[0].mgw := {
148 host := mgw,
149 port_nr := 10000 + nr*2
150 }
151 g_media.mgcp_conn[1].mgw := {
152 host := mgw,
153 port_nr := 11000 + nr*2
154 }
155}
156
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200157/* Helper function to get the next free MGCP connection identifier. We can
158 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100159private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
160 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200161 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100162 return i;
163 }
164 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100165 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Only 2 Connections per EP!");
166 /* Should never be reached */
167 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100168}
169
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200170/* Helper function to pick a specific connection by its cid. Since we reach out
171 * for a connection that is in-use we also check if there was already exactly
172 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100173private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
174 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200175 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 +0100176 return i;
177 }
178 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100179 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No Connection for ID ", cid));
180 /* Should not be reached */
181 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100182}
183
Philipp Maierb2422352018-07-11 10:36:57 +0200184/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
185 * RSL level media handling */
186altstep as_Media_ipacc() runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100187 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100188 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100189 var boolean b_unused;
Harald Welte211219e2018-01-29 22:03:36 +0100190 [not g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
191 /* Extract parameters from request + use in response */
192 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
193 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
194 }
195 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
196 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
197 }
198 RSL.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
199 oct2int(f_inet_addr(g_media.bts.bts.host)),
200 g_media.bts.bts.port_nr,
201 g_media.bts.rtp_pt));
202 g_media.bts.ipa_crcx_seen := true;
203 repeat;
204 }
205 [g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
206 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100207 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100208 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100209 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100210 }
211 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100212 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100213 g_media.bts.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100214 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100215 g_media.bts.peer.port_nr := ie.ipa_remote_port;
216 /* optional */
217 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
218 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
219 }
220 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
221 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
222 }
223 RSL.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
224 oct2int(f_inet_addr(g_media.bts.peer.host)),
225 g_media.bts.peer.port_nr,
226 g_media.bts.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200227 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100228 repeat;
229 }
Harald Welte261af4b2018-02-12 21:20:39 +0100230
231 /* on second (new) BTS during hand-over */
232 [not g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
233 /* Extract parameters from request + use in response */
234 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
235 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
236 }
237 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
238 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
239 }
240 RSL1.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
241 oct2int(f_inet_addr(g_media.bts1.bts.host)),
242 g_media.bts1.bts.port_nr,
243 g_media.bts1.rtp_pt));
244 g_media.bts1.ipa_crcx_seen := true;
245 repeat;
246 }
247 /* on second (new) BTS during hand-over */
248 [g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
249 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100250 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100251 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100252 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100253 }
254 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100255 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100256 g_media.bts1.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100257 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100258 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
259 /* optional */
260 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
261 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
262 }
263 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
264 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
265 }
266 RSL1.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
267 oct2int(f_inet_addr(g_media.bts1.peer.host)),
268 g_media.bts1.peer.port_nr,
269 g_media.bts1.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200270 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100271 repeat;
272 }
273
Philipp Maierb2422352018-07-11 10:36:57 +0200274
275}
276
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200277function f_rx_crcx(MgcpCommand mgcp_cmd)
278 runs on MSC_ConnHdlr return template MgcpResponse {
279 var MgcpOsmuxCID osmux_cid;
280 var SDP_Message sdp;
281 var integer cid := f_get_free_mgcp_conn();
Philipp Maiera2083892020-09-21 14:18:36 +0200282 var charstring local_rtp_addr;
283
284 if (g_pars.media_mgw_offer_ipv6 == true) {
285 local_rtp_addr := host_mgw_rtp_v6; /* Use IPv6 by default if no remote addr is provided by client */
286 } else {
287 local_rtp_addr := host_mgw_rtp_v4; /* Use IPv4 by default if no remote addr is provided by client */
288 }
289
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200290 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
291 if (cid != 0) {
292 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
293 }
294 /* we keep the endpoint name allocated during MediaState_init */
295 } else {
296 /* Call Agent allocated endpoint, trust/use it always */
297 g_media.mgcp_ep := mgcp_cmd.line.ep;
298 }
299 if (isvalue(mgcp_cmd.sdp)) {
300 sdp := mgcp_cmd.sdp;
301 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
302 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 +0200303 if (sdp.connection.addr_type == "IP6") {
304 local_rtp_addr := host_mgw_rtp_v6;
305 } else {
306 local_rtp_addr := host_mgw_rtp_v4;
307 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200308 }
309 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200310 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200311 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
312 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
313 mgcp_conn.mime_type & "/" &
314 int2str(mgcp_conn.sample_rate))),
315 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
316 var template MgcpResponse mgcp_resp;
317 if (g_pars.use_osmux and f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
318 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
319 mgcp_resp := ts_CRCX_ACK_osmux(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, osmux_cid, sdp);
320 } else {
321 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
322 }
323 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
324 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
325 return mgcp_resp;
326}
327
328function f_rx_mdcx(MgcpCommand mgcp_cmd)
329 runs on MSC_ConnHdlr return template MgcpResponse {
330 var SDP_Message sdp;
331 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200332 var charstring local_rtp_addr;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200333 if (isvalue(mgcp_cmd.sdp)) {
334 sdp := mgcp_cmd.sdp;
335 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
336 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 +0200337 if (sdp.connection.addr_type == "IP6") {
338 local_rtp_addr := host_mgw_rtp_v6;
339 } else {
340 local_rtp_addr := host_mgw_rtp_v4;
341 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200342 } else {
343 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
344 }
345 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200346 sdp := valueof(ts_SDP(mgcp_conn.peer.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200347 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
348 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
349 mgcp_conn.mime_type & "/" &
350 int2str(mgcp_conn.sample_rate))),
351 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
352 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
353 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
354}
355
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200356function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
357runs on MSC_ConnHdlr return template MGCP_RecvFrom {
358 var template MGCP_RecvFrom mrf := {
359 connId := ?,
360 remName := ?,
361 remPort := ?,
362 locName := ?,
363 locPort := ?,
364 msg := msg
365 }
366 return mrf;
367}
368
Philipp Maierb2422352018-07-11 10:36:57 +0200369/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
370 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200371
Philipp Maierc1e95c82018-07-18 16:03:00 +0200372altstep as_Media_mgw(boolean norepeat := false) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200373 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200374 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200375 var MGCP_RecvFrom mrf;
376 var template MgcpMessage msg_crcx := {
377 command := tr_CRCX
378 }
379 var template MgcpMessage msg_mdcx := {
380 command := tr_MDCX
381 }
382 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200383
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200384 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200385 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100386 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200387 if(norepeat == false) {
388 repeat;
389 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200390 }
391
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200392 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
393 mgcp_resp := f_rx_crcx(mrf.msg.command);
394 msg_resp := {
395 response := mgcp_resp
396 }
397 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
398 if(norepeat == false) {
399 repeat;
400 }
401 }
402
403 [g_pars.aoip] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200404 mgcp_resp := f_rx_mdcx(mgcp_cmd);
405 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200406 if(norepeat == false) {
407 repeat;
408 }
Harald Welte211219e2018-01-29 22:03:36 +0100409 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200410
411 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
412 mgcp_resp := f_rx_mdcx(mrf.msg.command);
413 msg_resp := {
414 response := mgcp_resp
415 }
416 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
417 if(norepeat == false) {
418 repeat;
419 }
420 }
Harald Welte211219e2018-01-29 22:03:36 +0100421}
422
Philipp Maierb2422352018-07-11 10:36:57 +0200423/* Altsteps for handling of media related commands. Can be activated by a given
424 * test case if it expects to see media related handling (i.e. voice calls) */
425altstep as_Media() runs on MSC_ConnHdlr {
426 [] as_Media_ipacc();
427 [] as_Media_mgw();
428}
Harald Welte211219e2018-01-29 22:03:36 +0100429
Harald Welte28d943e2017-11-25 15:00:50 +0100430/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200431 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
432 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
Harald Welte8cb58252020-08-31 16:50:17 +0200433type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr, StatsD_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100434 /* SCCP Connecction Identifier for the underlying SCCP connection */
435 var integer g_sccp_conn_id;
436
Harald Welte6811d102019-04-14 22:23:14 +0200437 /* procedure port back to our parent (RAN_Emulation_CT) for control */
438 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100439 port TELNETasp_PT BSCVTY;
Harald Welte28d943e2017-11-25 15:00:50 +0100440
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200441 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
442 port IPA_MGCP_PT MGCP_MSC_CLIENT;
443 var integer g_trans_id := 0;
444
Harald Welte211219e2018-01-29 22:03:36 +0100445 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100446 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100447
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200448 var charstring host_bts := "127.0.0.2";
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200449 var charstring host_mgw_mgcp := "127.0.0.3";
450 var charstring host_mgw_rtp_v4 := "127.0.0.5";
451 var charstring host_mgw_rtp_v6 := "::1";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200452 var charstring host_msc := "127.0.0.4";
453
Harald Weltee97eab42018-03-21 18:46:06 +0100454 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100455}
456
Harald Welteeddf0e92020-06-21 19:42:15 +0200457function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100458 if (not g_vty_initialized) {
459 map(self:BSCVTY, system:BSCVTY);
460 f_vty_set_prompts(BSCVTY);
461 f_vty_transceive(BSCVTY, "enable");
462 g_vty_initialized := true;
463 }
Harald Welte28d943e2017-11-25 15:00:50 +0100464}
465
Harald Welteeddf0e92020-06-21 19:42:15 +0200466/* initialize all parameters */
467function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
468 f_MediaState_init(g_media, i, bts, mgw, codecType);
469 f_MscConnHdlr_init_vty();
470}
471
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200472private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
473 var MgcpTransId tid := int2str(g_trans_id);
474 g_trans_id := g_trans_id + 1;
475 return tid;
476}
477
Harald Welte6811d102019-04-14 22:23:14 +0200478/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100479 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
480private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200481runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100482 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200483 var boolean append_osmux_support := g_ran_ops.use_osmux and
484 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100485
Harald Weltec1a2fff2017-12-17 11:06:19 +0100486 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200487 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
488 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100489 }
490
491 return resp;
492}
493
Harald Welte6811d102019-04-14 22:23:14 +0200494const RanOps MSC_RanOps := {
495 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100496 unitdata_cb := refers(UnitdataCallback),
497 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100498 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200499 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200500 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200501 use_osmux := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100502 sccp_addr_local := omit,
503 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100504}
505
Daniel Willmann191e0d92018-01-17 12:44:35 +0100506const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100507 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
508 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100509}
510
Harald Weltec1a2fff2017-12-17 11:06:19 +0100511/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200512function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200513 RAN.call(RAN_register:{l3_enc, self}) {
514 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100515 }
516}
517
Harald Welte651fcdc2018-05-10 20:23:16 +0200518type record TestHdlrEncrParams {
519 OCT1 enc_alg,
520 octetstring enc_key
521};
522
523template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg, octetstring key) := {
524 enc_alg := alg,
525 enc_key := key
526}
527
Harald Weltecc0b0142018-05-29 15:19:33 +0200528type record TestHdlrParamsLcls {
529 GlobalCallReferenceValue gcr optional,
530 /* LCLS Configuration */
531 BIT4 cfg optional,
532 /* LCLS Connection Status Control */
533 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100534 BIT4 exp_sts optional,
535 /* Whether to adjust *cx_seen_exp for LCLS tests */
536 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200537}
538
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200539type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200540 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200541 integer rsl_idx,
542 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200543}
544
Harald Weltec1a2fff2017-12-17 11:06:19 +0100545type record TestHdlrParams {
546 OCT1 ra,
547 GsmFrameNumber fn,
548 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100549 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200550 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200551 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200552 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100553 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200554 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100555 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200556 SCCP_PAR_Address sccp_addr_msc optional,
557 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100558 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100559 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200560 boolean aoip,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200561 boolean use_osmux,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200562 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200563 TestHdlrParamsMSCPool mscpool,
564 boolean media_mgw_offer_ipv6
Harald Weltec1a2fff2017-12-17 11:06:19 +0100565};
566
Philipp Maier48604732018-10-09 15:00:37 +0200567/* Note: Do not use valueof() to get a value of this template, use
568 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100569 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100570template (value) TestHdlrParams t_def_TestHdlrPars := {
571 ra := '23'O,
572 fn := 23,
573 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100574 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200575 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200576 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200577 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100578 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200579 encr := omit,
580 lcls := {
581 gcr := omit,
582 cfg := omit,
583 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100584 exp_sts := omit,
585 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100586 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200587 sccp_addr_msc := omit,
588 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100589 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100590 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200591 aoip := true,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200592 use_osmux := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200593 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200594 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200595 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200596 rsl_idx := 0,
597 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200598 },
599 media_mgw_offer_ipv6 := true
Harald Weltec1a2fff2017-12-17 11:06:19 +0100600}
601
Harald Weltea0630032018-03-20 21:09:55 +0100602function f_create_chan_and_exp() runs on MSC_ConnHdlr {
603 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100604 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100605 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
606
607 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Harald Weltea0630032018-03-20 21:09:55 +0100608 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn);
Daniel Willmann191e0d92018-01-17 12:44:35 +0100609 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100610}
611
Harald Welte898113b2018-01-31 18:32:21 +0100612function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
613 template (omit) RslChannelNr chan_nr := omit) runs on MSC_ConnHdlr {
614 if (not isvalue(link_id)) {
615 link_id := ts_RslLinkID_DCCH(0);
616 }
617 if (not isvalue(chan_nr)) {
618 chan_nr := g_chan_nr;
619 }
620 RSL.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
621}
622
Harald Weltec1a2fff2017-12-17 11:06:19 +0100623function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig) runs on MSC_ConnHdlr {
624 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100625 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100626 if (orig.msg_type == RSL_MT_ENCR_CMD) {
627 link_id := orig.ies[2].body.link_id;
628 } else {
629 link_id := orig.ies[1].body.link_id;
630 }
Harald Welte898113b2018-01-31 18:32:21 +0100631 f_rsl_send_l3(l3, link_id, chan_nr);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100632}
633
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100634/* Convert the chipher representation on BSSMAP to the representation used on RSL */
Harald Weltee613f962018-04-18 22:38:16 +0200635function f_chipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100636{
637 /* A5 0 */
638 if (alg_bssmap == '01'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200639 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100640 }
641 /* A5 1 */
642 else if (alg_bssmap == '02'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200643 return RSL_ALG_ID_A5_1;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100644 }
645 /* A5 2 */
646 else if (alg_bssmap == '04'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200647 return RSL_ALG_ID_A5_2;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100648 }
649 /* A5 3 */
650 else if (alg_bssmap == '08'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200651 return RSL_ALG_ID_A5_3;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100652 }
653 /* A5 4 */
654 else if (alg_bssmap == '10'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200655 return RSL_ALG_ID_A5_4;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100656 }
657 /* A5 5 */
658 else if (alg_bssmap == '20'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200659 return RSL_ALG_ID_A5_5;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100660 }
661 /* A5 6 */
662 else if (alg_bssmap == '40'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200663 return RSL_ALG_ID_A5_6;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100664 }
665 /* A5 7 */
666 else if (alg_bssmap == '80'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200667 return RSL_ALG_ID_A5_7;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100668 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100669 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm");
Harald Weltee613f962018-04-18 22:38:16 +0200670 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100671 }
672}
673
Harald Welte38b2a102017-12-23 02:42:58 +0100674function f_cipher_mode(OCT1 alg, OCT8 key, template OCT16 kc128 := omit, boolean exp_fail := false)
675runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100676 var PDU_BSSAP bssap;
677 var RSL_Message rsl;
Harald Weltee613f962018-04-18 22:38:16 +0200678 var RSL_AlgId alg_rsl;
Harald Welte73cd2712017-12-17 00:44:52 +0100679
680 if (isvalue(kc128)) {
681 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(alg, key, valueof(kc128)));
682 } else {
683 BSSAP.send(ts_BSSMAP_CipherModeCmd(alg, key));
684 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100685
686 /* RSL uses a different representation of the encryption algorithm,
687 * so we need to convert first */
688 alg_rsl := f_chipher_mode_bssmap_to_rsl(alg);
689
Harald Welte73cd2712017-12-17 00:44:52 +0100690 alt {
691 /* RSL/UE Side */
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100692 [] RSL.receive(tr_RSL_ENCR_CMD(g_chan_nr, ?, alg_rsl, key)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100693 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
694 log("Rx L3 from net: ", l3);
695 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
696 f_rsl_reply(ts_RRM_CiphModeCompl, rsl);
697 }
698 repeat;
699 }
700 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
701 // bssap.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algoritmhIdentifier
Harald Welte38b2a102017-12-23 02:42:58 +0100702 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100703 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +0100704 } else {
705 setverdict(pass);
706 }
Harald Welte73cd2712017-12-17 00:44:52 +0100707 }
708 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100709 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100710 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +0100711 } else {
712 setverdict(pass);
713 }
Harald Welte73cd2712017-12-17 00:44:52 +0100714 }
715 }
716}
717
Harald Welte211219e2018-01-29 22:03:36 +0100718/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
719function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
720 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100721 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100722
723 if (match(inp, '00001'B)) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100724 chan_nr := valueof(t_RslChanNr_Bm(tn));
Harald Welte211219e2018-01-29 22:03:36 +0100725 }
726 else if (match(inp, '0001?'B)) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100727 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Harald Welte211219e2018-01-29 22:03:36 +0100728 }
729 else if (match(inp, '001??'B)) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100730 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Harald Welte211219e2018-01-29 22:03:36 +0100731 }
732 else if (match(inp, '01???'B)) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100733 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Harald Welte211219e2018-01-29 22:03:36 +0100734 }
735 else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100736 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Harald Welte211219e2018-01-29 22:03:36 +0100737 }
738
739 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100740 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +0100741 } else {
742 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
743 arfcn := oct2int(concat);
744 }
745}
746
747type record AssignmentState {
748 /* global */
749 boolean voice_call,
750 boolean is_assignment,
751 /* Assignment related bits */
752 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100753 boolean old_lchan_deact_sacch_seen,
754 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +0100755 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100756 RslChannelNr old_chan_nr,
757 /* Modify related bits */
758 boolean rr_modify_seen,
Harald Welte21583082018-01-29 22:28:26 +0100759 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100760}
761
762template (value) AssignmentState ts_AssignmentStateInit := {
763 voice_call := false,
764 is_assignment := false,
765 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100766 old_lchan_deact_sacch_seen := false,
767 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100768 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100769 old_chan_nr := -,
770 rr_modify_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100771 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100772}
773
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200774private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
775 encr_info := {
776 len := ?,
777 alg_id := alg,
778 key := key
779 }
780}
781
782/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
783private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
784 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100785 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100786 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100787
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200788 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg != '01'O) {
789 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100790 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200791 } else {
792 var RSL_AlgId alg := f_chipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg);
793 if (not match(encr_info, tr_EncrInfo(alg, g_pars.encr.enc_key))) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100794 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Wrong Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200795 }
796 }
797 } else {
798 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
799 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100800 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200801 }
802 }
803 }
804 /* 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 +0100805 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100806
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100807 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 +0100808 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
809 }
810
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100811 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
812 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
813 } else {
814 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 +0100815 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 +0100816 }
817 }
818
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200819}
820
Harald Welte211219e2018-01-29 22:03:36 +0100821altstep as_assignment(inout AssignmentState st) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100822 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100823 [not st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
824 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
825 log("Rx L3 from net: ", l3);
826 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
827 var RslChannelNr new_chan_nr;
828 var GsmArfcn arfcn;
829 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
830 new_chan_nr, arfcn);
831 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100832
Harald Welte211219e2018-01-29 22:03:36 +0100833 /* register our component for this channel number at the RSL Emulation */
834 f_rslem_register(0, new_chan_nr);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +0700835 /* dispatch queued messages for this channel (if any) */
836 f_rslem_dchan_queue_dispatch();
837
Harald Welte211219e2018-01-29 22:03:36 +0100838 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
839 /* send assignment complete over the new channel */
Neels Hofmeyrb0e73652018-07-14 21:56:51 +0200840 RSL.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +0100841 enc_PDU_ML3_MS_NW(l3_tx)));
842 /* by default, send via the new channel from now */
843 st.old_chan_nr := g_chan_nr;
844 g_chan_nr := new_chan_nr;
845 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200846 /* obtain channel activation from RSL_Emulation for new channel */
847 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
848 /* check it (e.g. for correct ciphering parameters) */
849 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +0100850 repeat;
851 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100852 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100853 }
Harald Welte211219e2018-01-29 22:03:36 +0100854 }
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100855 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100856 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100857 repeat;
858 }
Harald Welte211219e2018-01-29 22:03:36 +0100859 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100860 st.old_lchan_rll_rel_req_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100861 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
862 repeat;
863 }
864 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
865 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +0100866 /* unregister for old channel number in RSL emulation */
867 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
868 f_rslem_unregister(0, st.old_chan_nr);
Harald Welte21583082018-01-29 22:28:26 +0100869 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +0100870 repeat;
871 }
872}
873
874altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
875 /* no assignment, just mode modify */
876 var RSL_Message rsl;
877
878 [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 +0100879 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
880 log("Rx L3 from net: ", l3);
881 if (ischosen(l3.msgs.rrm.channelModeModify)) {
882 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
883 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +0100884 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100885 }
886 repeat;
887 }
Harald Welte211219e2018-01-29 22:03:36 +0100888 [st.voice_call and st.rr_modify_seen] RSL.receive(tr_RSL_MsgTypeD(RSL_MT_MODE_MODIFY_REQ)) -> value rsl {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100889 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +0100890 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100891 repeat;
892 }
Harald Welte211219e2018-01-29 22:03:36 +0100893}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100894
Harald Welte211219e2018-01-29 22:03:36 +0100895/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
896function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
897return boolean {
898 select (bssmap.speechOrDataIndicator) {
899 case ('0011'B) { /* Signalling */
900 /* all channels support signalling */
901 return true;
902 }
903 case else { /* Speech, Speech+CTM or CSD */
904 select (bssmap.channelRateAndType) {
905 case ('08'O) { /* TCH/F */
906 select (rsl_chan_nr) {
907 case (t_RslChanNr_Bm(?)) { return true; }
908 }
909 }
910 case ('09'O) { /* TCH/H */
911 select (rsl_chan_nr) {
912 case (t_RslChanNr_Lm(?, ?)) { return true; }
913 }
914 }
915 case else { /* full or half-rate */
916 select (rsl_chan_nr) {
917 case (t_RslChanNr_Bm(?)) { return true; }
918 case (t_RslChanNr_Lm(?, ?)) { return true; }
919 }
920 }
921 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100922 }
Harald Welte211219e2018-01-29 22:03:36 +0100923 }
924 return false;
925}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100926
Philipp Maier8ef527e2018-05-18 11:12:50 +0200927/* Determine if the channel mode specified within rsl_chan_nr requires a
928 * MODE MODIFY in to match the channel mode specified by given BSSMAP
929 * ChannelType */
930function f_channel_needs_modify(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
931return boolean {
932
933 /* FIXME: This tests the rsl_chan_nr to determine if we are on a
934 * signalling channel or not. Unfortunately this may lead to false
935 * results if we are on a TCH. The problem is that a TCH may be also
936 * used in signalling mode, but this function assumes that only SDCCH4
937 * and SDCCH8 are used as signalling channels at all. */
938
939 var boolean current_signalling := false;
940 var boolean desired_signalling := false;
941
942 select (rsl_chan_nr) {
943 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
944 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
945 }
946
947 if (bssmap.speechOrDataIndicator == '0011'B) {
948 desired_signalling := true;
949 }
950
951 if (current_signalling == desired_signalling) {
952 /* The desired channel mode is equal to the one we currently
953 * have, there is no mode modification needed or expected */
954 return false;
955 } else {
956 /* The desired channel mode and the current channel mode do
957 * not match. A mode modification is required */
958 return true;
959 }
960}
961
Harald Weltecc0b0142018-05-29 15:19:33 +0200962/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
963function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
964 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
965 if (istemplatekind(ass_tpl, "omit")) {
966 return;
967 }
968 if (ispresent(g_pars.lcls.gcr)) {
969 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
970 }
971 if (ispresent(g_pars.lcls.cfg)) {
972 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
973 }
974 if (ispresent(g_pars.lcls.csc)) {
975 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
976 }
Harald Welte343b7742018-06-05 23:41:37 +0200977 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
978 if (ispresent(g_pars.lcls.exp_sts)) {
979 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
980 } else {
981 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
982 }
Harald Weltecc0b0142018-05-29 15:19:33 +0200983 }
984}
985
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200986/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
987function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
988 template MgcpResponse resp_any := ?
989 var MgcpResponse resp_val;
990 resp.line.trans_id := cmd.line.trans_id;
991 timer T := 5.0;
992
993 BSSAP.send(cmd);
994 T.start;
995 alt {
996 [] as_Media_mgw();
997 [] BSSAP.receive(resp) -> value resp_val { }
998 [] BSSAP.receive(resp_any) {
999 setverdict(fail, "Response didn't match template");
1000 mtc.stop;
1001 }
1002 [] BSSAP.receive { repeat; }
1003 [] T.timeout {
1004 setverdict(fail, "Timeout waiting for response to ", cmd);
1005 mtc.stop;
1006 }
1007 }
1008 T.stop;
1009}
1010
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001011/* Helper function to check if the activity on the MGCP matches what we
1012 * expected */
1013function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1014 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001015 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1016 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1017 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1018
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001019 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001020 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 +02001021 }
1022 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001023 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 +02001024 }
1025 }
1026}
1027
Harald Welte211219e2018-01-29 22:03:36 +01001028/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001029function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1030runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001031
1032 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001033 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001034
Philipp Maier4ce978c2020-08-11 22:54:07 +02001035 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001036 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1037 } else {
1038 /* Make sure a meaningful default is assigned in case the
1039 * codecList is not populated */
1040 codecType := FR_AMR;
1041 }
1042
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001043 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001044
Harald Weltecc0b0142018-05-29 15:19:33 +02001045 /* patch in the LCLS related items, as needed */
1046 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1047
Harald Welte7a14fd52018-05-10 22:26:55 +02001048 f_create_chan_and_exp();
1049 /* we should now have a COMPL_L3 at the MSC */
Philipp Maier48aeeec2018-09-14 18:20:04 +02001050
1051 var template PDU_BSSAP exp_l3_compl;
1052 exp_l3_compl := tr_BSSMAP_ComplL3()
Philipp Maier48604732018-10-09 15:00:37 +02001053 if (g_pars.aoip == false) {
Philipp Maier48aeeec2018-09-14 18:20:04 +02001054 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := omit;
1055 } else {
1056 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := ?;
1057 }
1058 T.start;
1059 alt {
1060 [] BSSAP.receive(exp_l3_compl);
1061 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001062 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001063 }
1064 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001065 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001066 }
1067 }
Harald Welte7a14fd52018-05-10 22:26:55 +02001068
1069 /* start ciphering, if requested */
1070 if (ispresent(g_pars.encr)) {
1071 f_cipher_mode(g_pars.encr.enc_alg, g_pars.encr.enc_key);
1072 }
1073
1074 /* bail out early if no assignment requested */
1075 if (istemplatekind(ass_tpl, "omit")) {
1076 return;
1077 }
1078
1079 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001080 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001081 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001082 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001083 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001084
Harald Welte211219e2018-01-29 22:03:36 +01001085 var ExpectCriteria mgcpcrit := {
1086 connid := omit,
1087 endpoint := omit,
1088 transid := omit
1089 };
1090 var AssignmentState st := valueof(ts_AssignmentStateInit);
1091 /* if the channel type is SIGNAL, we're not handling a voice call */
1092 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
1093 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001094 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001095 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001096
Harald Welte211219e2018-01-29 22:03:36 +01001097 /* determine if the current channel can support the given service or not */
1098 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1099 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001100
1101 /* We decided to assign a new channel, so we do not expect
1102 * any mode modify messages on RSL */
1103 exp_modify := false;
1104 } else {
1105 /* We will continue working with the currently assigned
1106 * channel, we must now check if the mode of the current
1107 * channel is compatible. If not we expect the BSC to modify
1108 * the mode */
1109 exp_modify := f_channel_needs_modify(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001110 }
1111
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001112 /* Some test situations will involve MGCP transactions on a media
1113 * gateway. Depending on the situation, we set up how many of each MGCP
1114 * message type are expected to be exchanged. */
1115 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1116 /* For tests that expect the assignment to fail, assume that
1117 * there will be no MGW communication as well. */
1118 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1119 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1120 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1121 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
1122 } else if (st.voice_call) {
1123 /* For voice calls we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001124 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1125 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1126 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1127 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001128 }
1129
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001130 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1131 * a channel and send us RR Assignment Command together with ip.access CRCX.
1132 * There is a risk that the RSL Emulation component would dequeue and process
1133 * ip.access CRCX faster than we process the Assignment Command and register
1134 * the corresponding handler for the indicated RSL channel number. This would
1135 * result in a failure, because at that moment there will be no handler for
1136 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1137 f_rslem_dchan_queue_enable();
1138
Harald Welte211219e2018-01-29 22:03:36 +01001139 f_create_mgcp_expect(mgcpcrit);
1140 BSSAP.send(ass_cmd);
1141
1142 T.start;
1143 alt {
1144 /* assignment related bits */
1145 [st.is_assignment] as_assignment(st);
1146
1147 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001148 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001149
1150 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
1151 [st.voice_call] as_Media();
1152
1153 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001154 [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 +01001155 setverdict(pass);
1156 }
Philipp Maier86f39202018-02-07 14:40:09 +01001157 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1158 setverdict(pass);
1159 }
Harald Welte21583082018-01-29 22:28:26 +01001160 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001161 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001162 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001163 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001164 }
1165 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001166 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001167 }
1168 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001169 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001170 }
1171 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001172 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001173 }
1174 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001175 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001176 }
1177 }
Harald Welte211219e2018-01-29 22:03:36 +01001178 log("g_media ", g_media);
1179 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001180 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001181 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001182
Philipp Maiera0976e92018-07-16 15:19:42 +02001183 /* When the BSC detects that LCLS is possible it will cross the
1184 * connetions that point to the PBX side of the MGW. In our case this
1185 * is mgcp_conn[1]. The BSC performs this operation already before the
1186 * assignment complete is generated. This means we expect another MDCX
1187 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001188 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 +02001189 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1190 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1191
1192 }
1193 }
1194
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001195 if (not exp_fail and st.voice_call and not g_pars.aoip) {
1196 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1197 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1198 connection. BSC will forward it to its MGW. */
1199 var template MgcpCommand cmd;
1200 var template MgcpResponse resp;
1201 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1202 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1203 var MgcpCallId call_id := '51234'H;
1204 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
1205 if (g_pars.use_osmux) {
1206 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1207 resp := tr_CRCX_ACK_osmux;
1208 } else {
1209 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1210 resp := tr_CRCX_ACK;
1211 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001212 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001213 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1214 { valueof(ts_SDP_ptime(20)) });
1215 mgcp_transceive_mgw(cmd, resp);
1216 }
1217
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001218 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001219
1220 if (st.is_assignment and st.assignment_done) {
1221 if (not st.old_lchan_deact_sacch_seen) {
1222 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1223 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1224 }
1225 if (st.old_lchan_rll_rel_req_seen) {
1226 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1227 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1228 }
1229 }
Harald Welte930d0a72018-03-22 22:08:40 +01001230}
1231
Harald Welte261af4b2018-02-12 21:20:39 +01001232type record HandoverState {
1233 /* Assignment related bits */
1234 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001235 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001236 boolean handover_done,
1237 RslChannelNr old_chan_nr
1238};
1239
1240altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1241 var RSL_Message rsl;
1242 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1243 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1244 log("Rx L3 from net: ", l3);
1245 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1246 var RslChannelNr new_chan_nr;
1247 var GsmArfcn arfcn;
1248 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1249 new_chan_nr, arfcn);
1250 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1251
1252 /* register our component for this channel number at the RSL Emulation */
1253 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1254
1255 /* resume processing of RSL DChan messages, which was temporarily suspended
1256 * before performing a hand-over */
1257 f_rslem_resume(RSL1_PROC);
1258
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001259 /* send handover detect */
1260 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1261 f_sleep(0.3);
1262
Harald Welte261af4b2018-02-12 21:20:39 +01001263 /* send handover complete over the new channel */
1264 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001265 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001266 enc_PDU_ML3_MS_NW(l3_tx)));
1267 /* by default, send via the new channel from now */
1268 st.old_chan_nr := g_chan_nr;
1269 g_chan_nr := new_chan_nr;
1270 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001271
1272 /* Memorize how many MDCX we have seen before. We need this number to detect
1273 * that we have received the handover related MDCX */
1274 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001275 repeat;
1276 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001277 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001278 }
1279 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001280 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001281 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001282 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1283 repeat;
1284 }
Harald Welte261af4b2018-02-12 21:20:39 +01001285 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1286 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1287 repeat;
1288 }
1289 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1290 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1291 /* unregister for old channel number in RSL emulation */
1292 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1293 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001294
1295 /* The channel release must not necessarly be synchronized to the RSL handover
1296 * procedure since those events may happen independently nearly at the same
1297 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1298 * IPACC or MGCP level may be still in progress. In order to make sure that
1299 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
1300 * as well as an MDCX on IPACC level. */
1301 if (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1302 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false) {
1303 repeat;
1304 } else {
1305 st.handover_done := true;
1306 }
Harald Welte261af4b2018-02-12 21:20:39 +01001307 }
1308}
1309
1310
Harald Weltec1a2fff2017-12-17 11:06:19 +01001311
Harald Welte28d943e2017-11-25 15:00:50 +01001312}