blob: 245528f893fdf0e7398a67f74731b94ed9f17aa2 [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;
48
Harald Weltec1a2fff2017-12-17 11:06:19 +010049
Harald Welte211219e2018-01-29 22:03:36 +010050/***********************************************************************
51 * Media related handling
52 ***********************************************************************/
53
Philipp Maier11a58942018-06-25 16:40:48 +020054/* Get the matching payload type for a specified BSSAP codec type
55 * (see also: BSSAP_Types.ttcn */
56private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
57 if (codecType == GSM_FR) {
58 return PT_GSM;
59 } else if (codecType == GSM_HR) {
60 return PT_GSMHR;
61 } else if (codecType == GSM_EFR) {
62 return PT_GSMEFR;
63 } else if (codecType == FR_AMR or codecType == HR_AMR) {
64 return PT_AMR;
65 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
66 return PT_AMRWB;
67 }
68
69 return PT_PCMU;
70}
71
Harald Welte211219e2018-01-29 22:03:36 +010072/* Tuple containing host/ip and port */
73type record HostPort {
74 HostName host,
75 PortNumber port_nr
76};
77
78/* State encapsulating one MGCP Connection */
79type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020080 integer crcx_seen, /* Counts how many CRCX operations happend */
81 integer mdcx_seen, /* Counts how many MDCX operations happend C */
82 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
83 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010084 MgcpConnectionId conn_id,
85 charstring mime_type, /* e.g. AMR */
86 integer sample_rate, /* 8000 */
87 integer ptime, /* 20 */
88 uint7_t rtp_pt, /* RTP Payload Type */
89 HostPort mgw, /* MGW side */
90 HostPort peer /* CA side */
91};
92
93/* BTS media state */
94type record BtsMediaState {
95 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +020096 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +010097 uint16_t conn_id,
98 uint7_t rtp_pt,
99 HostPort bts,
100 HostPort peer
101};
102
103type record MediaState {
104 MgcpEndpoint mgcp_ep,
105 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +0100106 BtsMediaState bts,
107 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +0100108};
109
Philipp Maier11a58942018-06-25 16:40:48 +0200110function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +0100111 /* BTS Side */
112 g_media.bts := {
113 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200114 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +0100115 conn_id := nr,
116 rtp_pt := 0,
117 bts := {
118 host := bts,
119 port_nr := 9000 + nr*2
120 },
121 peer := -
122 }
123
Harald Welte261af4b2018-02-12 21:20:39 +0100124 g_media.bts1 := {
125 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200126 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100127 conn_id := nr,
128 rtp_pt := 0,
129 bts := {
130 host := bts, /* FIXME */
131 port_nr := 9000 + nr*2
132 },
133 peer := -
134 }
135
Harald Welte363cb0a2018-01-30 19:35:53 +0100136 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100137
138 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200139 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100140 g_media.mgcp_conn[i].sample_rate := 8000;
141 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200142 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200143 g_media.mgcp_conn[i].crcx_seen := 0;
144 g_media.mgcp_conn[i].mdcx_seen := 0;
145 g_media.mgcp_conn[i].crcx_seen_exp := 0;
146 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100147 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
148 }
149
150 g_media.mgcp_conn[0].mgw := {
151 host := mgw,
152 port_nr := 10000 + nr*2
153 }
154 g_media.mgcp_conn[1].mgw := {
155 host := mgw,
156 port_nr := 11000 + nr*2
157 }
158}
159
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200160/* Helper function to get the next free MGCP connection identifier. We can
161 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100162private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
163 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200164 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100165 return i;
166 }
167 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100168 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Only 2 Connections per EP!");
169 /* Should never be reached */
170 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100171}
172
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200173/* Helper function to pick a specific connection by its cid. Since we reach out
174 * for a connection that is in-use we also check if there was already exactly
175 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100176private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
177 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200178 if (g_media.mgcp_conn[i].conn_id == cid and g_media.mgcp_conn[i].crcx_seen == 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100179 return i;
180 }
181 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100182 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No Connection for ID ", cid));
183 /* Should not be reached */
184 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100185}
186
Philipp Maierb2422352018-07-11 10:36:57 +0200187/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
188 * RSL level media handling */
189altstep as_Media_ipacc() runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100190 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100191 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100192 var boolean b_unused;
Harald Welte211219e2018-01-29 22:03:36 +0100193 [not g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
194 /* Extract parameters from request + use in response */
195 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
196 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
197 }
198 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
199 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
200 }
201 RSL.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
202 oct2int(f_inet_addr(g_media.bts.bts.host)),
203 g_media.bts.bts.port_nr,
204 g_media.bts.rtp_pt));
205 g_media.bts.ipa_crcx_seen := true;
206 repeat;
207 }
208 [g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
209 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100210 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100211 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100212 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100213 }
214 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100215 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100216 g_media.bts.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100217 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100218 g_media.bts.peer.port_nr := ie.ipa_remote_port;
219 /* optional */
220 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
221 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
222 }
223 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
224 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
225 }
226 RSL.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
227 oct2int(f_inet_addr(g_media.bts.peer.host)),
228 g_media.bts.peer.port_nr,
229 g_media.bts.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200230 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100231 repeat;
232 }
Harald Welte261af4b2018-02-12 21:20:39 +0100233
234 /* on second (new) BTS during hand-over */
235 [not g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
236 /* Extract parameters from request + use in response */
237 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
238 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
239 }
240 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
241 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
242 }
243 RSL1.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
244 oct2int(f_inet_addr(g_media.bts1.bts.host)),
245 g_media.bts1.bts.port_nr,
246 g_media.bts1.rtp_pt));
247 g_media.bts1.ipa_crcx_seen := true;
248 repeat;
249 }
250 /* on second (new) BTS during hand-over */
251 [g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
252 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100253 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100254 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100255 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100256 }
257 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100258 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100259 g_media.bts1.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100260 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100261 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
262 /* optional */
263 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
264 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
265 }
266 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
267 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
268 }
269 RSL1.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
270 oct2int(f_inet_addr(g_media.bts1.peer.host)),
271 g_media.bts1.peer.port_nr,
272 g_media.bts1.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200273 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100274 repeat;
275 }
276
Philipp Maierb2422352018-07-11 10:36:57 +0200277
278}
279
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200280function f_rx_crcx(MgcpCommand mgcp_cmd)
281 runs on MSC_ConnHdlr return template MgcpResponse {
282 var MgcpOsmuxCID osmux_cid;
283 var SDP_Message sdp;
284 var integer cid := f_get_free_mgcp_conn();
Philipp Maiera2083892020-09-21 14:18:36 +0200285 var charstring local_rtp_addr;
286
287 if (g_pars.media_mgw_offer_ipv6 == true) {
288 local_rtp_addr := host_mgw_rtp_v6; /* Use IPv6 by default if no remote addr is provided by client */
289 } else {
290 local_rtp_addr := host_mgw_rtp_v4; /* Use IPv4 by default if no remote addr is provided by client */
291 }
292
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200293 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
294 if (cid != 0) {
295 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
296 }
297 /* we keep the endpoint name allocated during MediaState_init */
298 } else {
299 /* Call Agent allocated endpoint, trust/use it always */
300 g_media.mgcp_ep := mgcp_cmd.line.ep;
301 }
302 if (isvalue(mgcp_cmd.sdp)) {
303 sdp := mgcp_cmd.sdp;
304 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
305 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 +0200306 if (sdp.connection.addr_type == "IP6") {
307 local_rtp_addr := host_mgw_rtp_v6;
308 } else {
309 local_rtp_addr := host_mgw_rtp_v4;
310 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200311 }
312 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200313 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200314 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
315 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
316 mgcp_conn.mime_type & "/" &
317 int2str(mgcp_conn.sample_rate))),
318 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
319 var template MgcpResponse mgcp_resp;
320 if (g_pars.use_osmux and f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
321 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
322 mgcp_resp := ts_CRCX_ACK_osmux(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, osmux_cid, sdp);
323 } else {
324 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
325 }
326 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
327 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
328 return mgcp_resp;
329}
330
331function f_rx_mdcx(MgcpCommand mgcp_cmd)
332 runs on MSC_ConnHdlr return template MgcpResponse {
333 var SDP_Message sdp;
334 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200335 var charstring local_rtp_addr;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200336 if (isvalue(mgcp_cmd.sdp)) {
337 sdp := mgcp_cmd.sdp;
338 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
339 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 +0200340 if (sdp.connection.addr_type == "IP6") {
341 local_rtp_addr := host_mgw_rtp_v6;
342 } else {
343 local_rtp_addr := host_mgw_rtp_v4;
344 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200345 } else {
346 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
347 }
348 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200349 sdp := valueof(ts_SDP(mgcp_conn.peer.host, local_rtp_addr, "foo", "21",
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200350 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
351 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
352 mgcp_conn.mime_type & "/" &
353 int2str(mgcp_conn.sample_rate))),
354 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
355 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
356 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
357}
358
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200359function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
360runs on MSC_ConnHdlr return template MGCP_RecvFrom {
361 var template MGCP_RecvFrom mrf := {
362 connId := ?,
363 remName := ?,
364 remPort := ?,
365 locName := ?,
366 locPort := ?,
367 msg := msg
368 }
369 return mrf;
370}
371
Philipp Maierb2422352018-07-11 10:36:57 +0200372/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
373 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200374
Philipp Maierc1e95c82018-07-18 16:03:00 +0200375altstep as_Media_mgw(boolean norepeat := false) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200376 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200377 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200378 var MGCP_RecvFrom mrf;
379 var template MgcpMessage msg_crcx := {
380 command := tr_CRCX
381 }
382 var template MgcpMessage msg_mdcx := {
383 command := tr_MDCX
384 }
385 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200386
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200387 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200388 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100389 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200390 if(norepeat == false) {
391 repeat;
392 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200393 }
394
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200395 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
396 mgcp_resp := f_rx_crcx(mrf.msg.command);
397 msg_resp := {
398 response := mgcp_resp
399 }
400 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
401 if(norepeat == false) {
402 repeat;
403 }
404 }
405
406 [g_pars.aoip] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200407 mgcp_resp := f_rx_mdcx(mgcp_cmd);
408 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200409 if(norepeat == false) {
410 repeat;
411 }
Harald Welte211219e2018-01-29 22:03:36 +0100412 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200413
414 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
415 mgcp_resp := f_rx_mdcx(mrf.msg.command);
416 msg_resp := {
417 response := mgcp_resp
418 }
419 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
420 if(norepeat == false) {
421 repeat;
422 }
423 }
Harald Welte211219e2018-01-29 22:03:36 +0100424}
425
Philipp Maierb2422352018-07-11 10:36:57 +0200426/* Altsteps for handling of media related commands. Can be activated by a given
427 * test case if it expects to see media related handling (i.e. voice calls) */
428altstep as_Media() runs on MSC_ConnHdlr {
429 [] as_Media_ipacc();
430 [] as_Media_mgw();
431}
Harald Welte211219e2018-01-29 22:03:36 +0100432
Harald Welte28d943e2017-11-25 15:00:50 +0100433/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200434 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
435 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
Harald Welte47cd0e32020-08-21 12:39:11 +0200436type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr, BSSAP_LE_ConnHdlr, StatsD_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100437 /* SCCP Connecction Identifier for the underlying SCCP connection */
438 var integer g_sccp_conn_id;
439
Harald Welte6811d102019-04-14 22:23:14 +0200440 /* procedure port back to our parent (RAN_Emulation_CT) for control */
441 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100442 port TELNETasp_PT BSCVTY;
Harald Welte28d943e2017-11-25 15:00:50 +0100443
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200444 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
445 port IPA_MGCP_PT MGCP_MSC_CLIENT;
446 var integer g_trans_id := 0;
447
Harald Welte211219e2018-01-29 22:03:36 +0100448 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100449 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100450
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200451 var charstring host_bts := "127.0.0.2";
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200452 var charstring host_mgw_mgcp := "127.0.0.3";
453 var charstring host_mgw_rtp_v4 := "127.0.0.5";
454 var charstring host_mgw_rtp_v6 := "::1";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200455 var charstring host_msc := "127.0.0.4";
456
Harald Weltee97eab42018-03-21 18:46:06 +0100457 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100458}
459
Harald Welteeddf0e92020-06-21 19:42:15 +0200460function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100461 if (not g_vty_initialized) {
462 map(self:BSCVTY, system:BSCVTY);
463 f_vty_set_prompts(BSCVTY);
464 f_vty_transceive(BSCVTY, "enable");
465 g_vty_initialized := true;
466 }
Harald Welte28d943e2017-11-25 15:00:50 +0100467}
468
Harald Welteeddf0e92020-06-21 19:42:15 +0200469/* initialize all parameters */
470function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
471 f_MediaState_init(g_media, i, bts, mgw, codecType);
472 f_MscConnHdlr_init_vty();
473}
474
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200475private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
476 var MgcpTransId tid := int2str(g_trans_id);
477 g_trans_id := g_trans_id + 1;
478 return tid;
479}
480
Harald Welte6811d102019-04-14 22:23:14 +0200481/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100482 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
483private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200484runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100485 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200486 var boolean append_osmux_support := g_ran_ops.use_osmux and
487 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100488
Harald Weltec1a2fff2017-12-17 11:06:19 +0100489 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200490 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
491 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100492 }
493
494 return resp;
495}
496
Harald Welte47cd0e32020-08-21 12:39:11 +0200497/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
498 * BSSAP_LE message arrives. Can return a PDU_BSSAP_LE that should be sent in return */
499private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
500runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
501 var template PDU_BSSAP_LE resp := omit;
502
503 /* answer all RESET with a RESET ACK */
504 if (match(bssap, tr_BSSMAP_LE_Reset)) {
505 resp := ts_BSSMAP_LE_ResetAck;
506 }
507
508 return resp;
509}
510
Harald Welte6811d102019-04-14 22:23:14 +0200511const RanOps MSC_RanOps := {
512 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100513 unitdata_cb := refers(UnitdataCallback),
514 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100515 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200516 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200517 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200518 use_osmux := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100519 sccp_addr_local := omit,
520 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100521}
522
Harald Welte47cd0e32020-08-21 12:39:11 +0200523const BssapLeOps SMLC_BssapLeOps := {
524 create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
525 unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
526 decode_dtap := false,
527 role_ms := false,
528 sccp_addr_local := omit,
529 sccp_addr_peer := omit
530}
531
Daniel Willmann191e0d92018-01-17 12:44:35 +0100532const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100533 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
534 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100535}
536
Harald Weltec1a2fff2017-12-17 11:06:19 +0100537/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200538function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200539 RAN.call(RAN_register:{l3_enc, self}) {
540 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100541 }
542}
543
Harald Welte651fcdc2018-05-10 20:23:16 +0200544type record TestHdlrEncrParams {
545 OCT1 enc_alg,
546 octetstring enc_key
547};
548
549template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg, octetstring key) := {
550 enc_alg := alg,
551 enc_key := key
552}
553
Harald Weltecc0b0142018-05-29 15:19:33 +0200554type record TestHdlrParamsLcls {
555 GlobalCallReferenceValue gcr optional,
556 /* LCLS Configuration */
557 BIT4 cfg optional,
558 /* LCLS Connection Status Control */
559 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100560 BIT4 exp_sts optional,
561 /* Whether to adjust *cx_seen_exp for LCLS tests */
562 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200563}
564
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200565type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200566 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200567 integer rsl_idx,
568 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200569}
570
Harald Weltec1a2fff2017-12-17 11:06:19 +0100571type record TestHdlrParams {
572 OCT1 ra,
573 GsmFrameNumber fn,
574 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100575 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200576 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200577 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200578 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100579 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200580 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100581 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200582 SCCP_PAR_Address sccp_addr_msc optional,
583 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100584 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100585 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200586 boolean aoip,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200587 boolean use_osmux,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200588 charstring host_aoip_tla,
Philipp Maiera2083892020-09-21 14:18:36 +0200589 TestHdlrParamsMSCPool mscpool,
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200590 boolean media_mgw_offer_ipv6,
591 OCT3 last_used_eutran_plmn optional
Harald Weltec1a2fff2017-12-17 11:06:19 +0100592};
593
Philipp Maier48604732018-10-09 15:00:37 +0200594/* Note: Do not use valueof() to get a value of this template, use
595 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100596 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100597template (value) TestHdlrParams t_def_TestHdlrPars := {
598 ra := '23'O,
599 fn := 23,
600 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100601 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200602 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200603 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200604 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100605 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200606 encr := omit,
607 lcls := {
608 gcr := omit,
609 cfg := omit,
610 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100611 exp_sts := omit,
612 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100613 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200614 sccp_addr_msc := omit,
615 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100616 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100617 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200618 aoip := true,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200619 use_osmux := false,
Pau Espin Pedrol07866632020-09-03 19:10:55 +0200620 host_aoip_tla := "1.2.3.4",
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200621 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200622 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200623 rsl_idx := 0,
624 l3_info := omit
Philipp Maiera2083892020-09-21 14:18:36 +0200625 },
Pau Espin Pedrolc08d5522021-04-16 15:40:38 +0200626 media_mgw_offer_ipv6 := true,
627 last_used_eutran_plmn := omit
Harald Weltec1a2fff2017-12-17 11:06:19 +0100628}
629
Harald Weltea0630032018-03-20 21:09:55 +0100630function f_create_chan_and_exp() runs on MSC_ConnHdlr {
631 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100632 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100633 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
634
635 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Harald Weltea0630032018-03-20 21:09:55 +0100636 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn);
Daniel Willmann191e0d92018-01-17 12:44:35 +0100637 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100638}
639
Harald Welte898113b2018-01-31 18:32:21 +0100640function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
641 template (omit) RslChannelNr chan_nr := omit) runs on MSC_ConnHdlr {
642 if (not isvalue(link_id)) {
643 link_id := ts_RslLinkID_DCCH(0);
644 }
645 if (not isvalue(chan_nr)) {
646 chan_nr := g_chan_nr;
647 }
648 RSL.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
649}
650
Harald Weltec1a2fff2017-12-17 11:06:19 +0100651function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig) runs on MSC_ConnHdlr {
652 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100653 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100654 if (orig.msg_type == RSL_MT_ENCR_CMD) {
655 link_id := orig.ies[2].body.link_id;
656 } else {
657 link_id := orig.ies[1].body.link_id;
658 }
Harald Welte898113b2018-01-31 18:32:21 +0100659 f_rsl_send_l3(l3, link_id, chan_nr);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100660}
661
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100662/* Convert the chipher representation on BSSMAP to the representation used on RSL */
Harald Weltee613f962018-04-18 22:38:16 +0200663function f_chipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100664{
665 /* A5 0 */
666 if (alg_bssmap == '01'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200667 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100668 }
669 /* A5 1 */
670 else if (alg_bssmap == '02'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200671 return RSL_ALG_ID_A5_1;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100672 }
673 /* A5 2 */
674 else if (alg_bssmap == '04'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200675 return RSL_ALG_ID_A5_2;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100676 }
677 /* A5 3 */
678 else if (alg_bssmap == '08'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200679 return RSL_ALG_ID_A5_3;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100680 }
681 /* A5 4 */
682 else if (alg_bssmap == '10'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200683 return RSL_ALG_ID_A5_4;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100684 }
685 /* A5 5 */
686 else if (alg_bssmap == '20'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200687 return RSL_ALG_ID_A5_5;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100688 }
689 /* A5 6 */
690 else if (alg_bssmap == '40'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200691 return RSL_ALG_ID_A5_6;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100692 }
693 /* A5 7 */
694 else if (alg_bssmap == '80'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200695 return RSL_ALG_ID_A5_7;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100696 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100697 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm");
Harald Weltee613f962018-04-18 22:38:16 +0200698 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100699 }
700}
701
Harald Welte38b2a102017-12-23 02:42:58 +0100702function f_cipher_mode(OCT1 alg, OCT8 key, template OCT16 kc128 := omit, boolean exp_fail := false)
703runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100704 var PDU_BSSAP bssap;
705 var RSL_Message rsl;
Harald Weltee613f962018-04-18 22:38:16 +0200706 var RSL_AlgId alg_rsl;
Harald Welte73cd2712017-12-17 00:44:52 +0100707
708 if (isvalue(kc128)) {
709 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(alg, key, valueof(kc128)));
710 } else {
711 BSSAP.send(ts_BSSMAP_CipherModeCmd(alg, key));
712 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100713
714 /* RSL uses a different representation of the encryption algorithm,
715 * so we need to convert first */
716 alg_rsl := f_chipher_mode_bssmap_to_rsl(alg);
717
Harald Welte73cd2712017-12-17 00:44:52 +0100718 alt {
719 /* RSL/UE Side */
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100720 [] RSL.receive(tr_RSL_ENCR_CMD(g_chan_nr, ?, alg_rsl, key)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100721 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
722 log("Rx L3 from net: ", l3);
723 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
724 f_rsl_reply(ts_RRM_CiphModeCompl, rsl);
725 }
726 repeat;
727 }
728 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
729 // bssap.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algoritmhIdentifier
Harald Welte38b2a102017-12-23 02:42:58 +0100730 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100731 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +0100732 } else {
733 setverdict(pass);
734 }
Harald Welte73cd2712017-12-17 00:44:52 +0100735 }
736 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100737 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100738 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +0100739 } else {
740 setverdict(pass);
741 }
Harald Welte73cd2712017-12-17 00:44:52 +0100742 }
743 }
744}
745
Harald Welte211219e2018-01-29 22:03:36 +0100746/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
747function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
748 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100749 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100750
751 if (match(inp, '00001'B)) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100752 chan_nr := valueof(t_RslChanNr_Bm(tn));
Harald Welte211219e2018-01-29 22:03:36 +0100753 }
754 else if (match(inp, '0001?'B)) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100755 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Harald Welte211219e2018-01-29 22:03:36 +0100756 }
757 else if (match(inp, '001??'B)) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100758 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Harald Welte211219e2018-01-29 22:03:36 +0100759 }
760 else if (match(inp, '01???'B)) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100761 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Harald Welte211219e2018-01-29 22:03:36 +0100762 }
763 else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100764 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Harald Welte211219e2018-01-29 22:03:36 +0100765 }
766
767 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100768 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +0100769 } else {
770 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
771 arfcn := oct2int(concat);
772 }
773}
774
775type record AssignmentState {
776 /* global */
777 boolean voice_call,
778 boolean is_assignment,
779 /* Assignment related bits */
780 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100781 boolean old_lchan_deact_sacch_seen,
782 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +0100783 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100784 RslChannelNr old_chan_nr,
785 /* Modify related bits */
786 boolean rr_modify_seen,
Harald Welte21583082018-01-29 22:28:26 +0100787 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100788}
789
790template (value) AssignmentState ts_AssignmentStateInit := {
791 voice_call := false,
792 is_assignment := false,
793 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100794 old_lchan_deact_sacch_seen := false,
795 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100796 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100797 old_chan_nr := -,
798 rr_modify_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100799 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100800}
801
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200802private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
803 encr_info := {
804 len := ?,
805 alg_id := alg,
806 key := key
807 }
808}
809
810/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
811private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
812 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100813 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100814 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100815
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200816 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg != '01'O) {
817 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100818 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200819 } else {
820 var RSL_AlgId alg := f_chipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg);
821 if (not match(encr_info, tr_EncrInfo(alg, g_pars.encr.enc_key))) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100822 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Wrong Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200823 }
824 }
825 } else {
826 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
827 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100828 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200829 }
830 }
831 }
832 /* 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 +0100833 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100834
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100835 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 +0100836 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
837 }
838
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100839 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
840 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
841 } else {
842 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 +0100843 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 +0100844 }
845 }
846
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200847}
848
Harald Welte211219e2018-01-29 22:03:36 +0100849altstep as_assignment(inout AssignmentState st) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100850 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100851 [not st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
852 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
853 log("Rx L3 from net: ", l3);
854 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
855 var RslChannelNr new_chan_nr;
856 var GsmArfcn arfcn;
857 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
858 new_chan_nr, arfcn);
859 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100860
Harald Welte211219e2018-01-29 22:03:36 +0100861 /* register our component for this channel number at the RSL Emulation */
862 f_rslem_register(0, new_chan_nr);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +0700863 /* dispatch queued messages for this channel (if any) */
864 f_rslem_dchan_queue_dispatch();
865
Harald Welte211219e2018-01-29 22:03:36 +0100866 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
867 /* send assignment complete over the new channel */
Neels Hofmeyrb0e73652018-07-14 21:56:51 +0200868 RSL.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +0100869 enc_PDU_ML3_MS_NW(l3_tx)));
870 /* by default, send via the new channel from now */
871 st.old_chan_nr := g_chan_nr;
872 g_chan_nr := new_chan_nr;
873 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200874 /* obtain channel activation from RSL_Emulation for new channel */
875 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
876 /* check it (e.g. for correct ciphering parameters) */
877 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +0100878 repeat;
879 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100880 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100881 }
Harald Welte211219e2018-01-29 22:03:36 +0100882 }
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100883 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100884 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100885 repeat;
886 }
Harald Welte211219e2018-01-29 22:03:36 +0100887 [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 +0100888 st.old_lchan_rll_rel_req_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100889 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
890 repeat;
891 }
892 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
893 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +0100894 /* unregister for old channel number in RSL emulation */
895 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
896 f_rslem_unregister(0, st.old_chan_nr);
Harald Welte21583082018-01-29 22:28:26 +0100897 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +0100898 repeat;
899 }
900}
901
902altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
903 /* no assignment, just mode modify */
904 var RSL_Message rsl;
905
906 [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 +0100907 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
908 log("Rx L3 from net: ", l3);
909 if (ischosen(l3.msgs.rrm.channelModeModify)) {
910 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
911 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +0100912 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100913 }
914 repeat;
915 }
Harald Welte211219e2018-01-29 22:03:36 +0100916 [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 +0100917 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +0100918 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100919 repeat;
920 }
Harald Welte211219e2018-01-29 22:03:36 +0100921}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100922
Harald Welte211219e2018-01-29 22:03:36 +0100923/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
924function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
925return boolean {
926 select (bssmap.speechOrDataIndicator) {
927 case ('0011'B) { /* Signalling */
928 /* all channels support signalling */
929 return true;
930 }
931 case else { /* Speech, Speech+CTM or CSD */
932 select (bssmap.channelRateAndType) {
933 case ('08'O) { /* TCH/F */
934 select (rsl_chan_nr) {
935 case (t_RslChanNr_Bm(?)) { return true; }
936 }
937 }
938 case ('09'O) { /* TCH/H */
939 select (rsl_chan_nr) {
940 case (t_RslChanNr_Lm(?, ?)) { return true; }
941 }
942 }
943 case else { /* full or half-rate */
944 select (rsl_chan_nr) {
945 case (t_RslChanNr_Bm(?)) { return true; }
946 case (t_RslChanNr_Lm(?, ?)) { return true; }
947 }
948 }
949 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100950 }
Harald Welte211219e2018-01-29 22:03:36 +0100951 }
952 return false;
953}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100954
Philipp Maier8ef527e2018-05-18 11:12:50 +0200955/* Determine if the channel mode specified within rsl_chan_nr requires a
956 * MODE MODIFY in to match the channel mode specified by given BSSMAP
957 * ChannelType */
958function f_channel_needs_modify(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
959return boolean {
960
961 /* FIXME: This tests the rsl_chan_nr to determine if we are on a
962 * signalling channel or not. Unfortunately this may lead to false
963 * results if we are on a TCH. The problem is that a TCH may be also
964 * used in signalling mode, but this function assumes that only SDCCH4
965 * and SDCCH8 are used as signalling channels at all. */
966
967 var boolean current_signalling := false;
968 var boolean desired_signalling := false;
969
970 select (rsl_chan_nr) {
971 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
972 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
973 }
974
975 if (bssmap.speechOrDataIndicator == '0011'B) {
976 desired_signalling := true;
977 }
978
979 if (current_signalling == desired_signalling) {
980 /* The desired channel mode is equal to the one we currently
981 * have, there is no mode modification needed or expected */
982 return false;
983 } else {
984 /* The desired channel mode and the current channel mode do
985 * not match. A mode modification is required */
986 return true;
987 }
988}
989
Harald Weltecc0b0142018-05-29 15:19:33 +0200990/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
991function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
992 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
993 if (istemplatekind(ass_tpl, "omit")) {
994 return;
995 }
996 if (ispresent(g_pars.lcls.gcr)) {
997 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
998 }
999 if (ispresent(g_pars.lcls.cfg)) {
1000 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
1001 }
1002 if (ispresent(g_pars.lcls.csc)) {
1003 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
1004 }
Harald Welte343b7742018-06-05 23:41:37 +02001005 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
1006 if (ispresent(g_pars.lcls.exp_sts)) {
1007 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
1008 } else {
1009 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
1010 }
Harald Weltecc0b0142018-05-29 15:19:33 +02001011 }
1012}
1013
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001014/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
1015function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
1016 template MgcpResponse resp_any := ?
1017 var MgcpResponse resp_val;
1018 resp.line.trans_id := cmd.line.trans_id;
1019 timer T := 5.0;
1020
1021 BSSAP.send(cmd);
1022 T.start;
1023 alt {
1024 [] as_Media_mgw();
1025 [] BSSAP.receive(resp) -> value resp_val { }
1026 [] BSSAP.receive(resp_any) {
1027 setverdict(fail, "Response didn't match template");
1028 mtc.stop;
1029 }
1030 [] BSSAP.receive { repeat; }
1031 [] T.timeout {
1032 setverdict(fail, "Timeout waiting for response to ", cmd);
1033 mtc.stop;
1034 }
1035 }
1036 T.stop;
1037}
1038
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001039/* Helper function to check if the activity on the MGCP matches what we
1040 * expected */
1041function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
1042 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +02001043 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
1044 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
1045 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
1046
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001047 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001048 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 +02001049 }
1050 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +01001051 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 +02001052 }
1053 }
1054}
1055
Harald Welte211219e2018-01-29 22:03:36 +01001056/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001057function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1058runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001059
1060 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001061 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001062
Philipp Maier4ce978c2020-08-11 22:54:07 +02001063 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001064 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1065 } else {
1066 /* Make sure a meaningful default is assigned in case the
1067 * codecList is not populated */
1068 codecType := FR_AMR;
1069 }
1070
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001071 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw_mgcp, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001072
Harald Weltecc0b0142018-05-29 15:19:33 +02001073 /* patch in the LCLS related items, as needed */
1074 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1075
Harald Welte7a14fd52018-05-10 22:26:55 +02001076 f_create_chan_and_exp();
1077 /* we should now have a COMPL_L3 at the MSC */
Philipp Maier48aeeec2018-09-14 18:20:04 +02001078
1079 var template PDU_BSSAP exp_l3_compl;
1080 exp_l3_compl := tr_BSSMAP_ComplL3()
Philipp Maier48604732018-10-09 15:00:37 +02001081 if (g_pars.aoip == false) {
Philipp Maier48aeeec2018-09-14 18:20:04 +02001082 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := omit;
1083 } else {
1084 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := ?;
1085 }
1086 T.start;
1087 alt {
1088 [] BSSAP.receive(exp_l3_compl);
1089 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001090 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001091 }
1092 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001093 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001094 }
1095 }
Harald Welte7a14fd52018-05-10 22:26:55 +02001096
1097 /* start ciphering, if requested */
1098 if (ispresent(g_pars.encr)) {
1099 f_cipher_mode(g_pars.encr.enc_alg, g_pars.encr.enc_key);
1100 }
1101
1102 /* bail out early if no assignment requested */
1103 if (istemplatekind(ass_tpl, "omit")) {
1104 return;
1105 }
1106
1107 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001108 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001109 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001110 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001111 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001112
Harald Welte211219e2018-01-29 22:03:36 +01001113 var ExpectCriteria mgcpcrit := {
1114 connid := omit,
1115 endpoint := omit,
1116 transid := omit
1117 };
1118 var AssignmentState st := valueof(ts_AssignmentStateInit);
1119 /* if the channel type is SIGNAL, we're not handling a voice call */
1120 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
1121 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001122 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001123 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001124
Harald Welte211219e2018-01-29 22:03:36 +01001125 /* determine if the current channel can support the given service or not */
1126 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1127 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001128
1129 /* We decided to assign a new channel, so we do not expect
1130 * any mode modify messages on RSL */
1131 exp_modify := false;
1132 } else {
1133 /* We will continue working with the currently assigned
1134 * channel, we must now check if the mode of the current
1135 * channel is compatible. If not we expect the BSC to modify
1136 * the mode */
1137 exp_modify := f_channel_needs_modify(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001138 }
1139
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001140 /* Some test situations will involve MGCP transactions on a media
1141 * gateway. Depending on the situation, we set up how many of each MGCP
1142 * message type are expected to be exchanged. */
1143 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1144 /* For tests that expect the assignment to fail, assume that
1145 * there will be no MGW communication as well. */
1146 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1147 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1148 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1149 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
1150 } else if (st.voice_call) {
1151 /* For voice calls we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001152 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1153 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1154 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1155 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001156 }
1157
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001158 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1159 * a channel and send us RR Assignment Command together with ip.access CRCX.
1160 * There is a risk that the RSL Emulation component would dequeue and process
1161 * ip.access CRCX faster than we process the Assignment Command and register
1162 * the corresponding handler for the indicated RSL channel number. This would
1163 * result in a failure, because at that moment there will be no handler for
1164 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1165 f_rslem_dchan_queue_enable();
1166
Harald Welte211219e2018-01-29 22:03:36 +01001167 f_create_mgcp_expect(mgcpcrit);
1168 BSSAP.send(ass_cmd);
1169
1170 T.start;
1171 alt {
1172 /* assignment related bits */
1173 [st.is_assignment] as_assignment(st);
1174
1175 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001176 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001177
1178 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
1179 [st.voice_call] as_Media();
1180
1181 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001182 [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 +01001183 setverdict(pass);
1184 }
Philipp Maier86f39202018-02-07 14:40:09 +01001185 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1186 setverdict(pass);
1187 }
Harald Welte21583082018-01-29 22:28:26 +01001188 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001189 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001190 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001191 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001192 }
1193 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001194 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001195 }
1196 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001197 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001198 }
1199 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001200 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001201 }
1202 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001203 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001204 }
1205 }
Harald Welte211219e2018-01-29 22:03:36 +01001206 log("g_media ", g_media);
1207 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001208 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001209 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001210
Philipp Maiera0976e92018-07-16 15:19:42 +02001211 /* When the BSC detects that LCLS is possible it will cross the
1212 * connetions that point to the PBX side of the MGW. In our case this
1213 * is mgcp_conn[1]. The BSC performs this operation already before the
1214 * assignment complete is generated. This means we expect another MDCX
1215 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001216 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 +02001217 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1218 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1219
1220 }
1221 }
1222
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001223 if (not exp_fail and st.voice_call and not g_pars.aoip) {
1224 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1225 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1226 connection. BSC will forward it to its MGW. */
1227 var template MgcpCommand cmd;
1228 var template MgcpResponse resp;
1229 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1230 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1231 var MgcpCallId call_id := '51234'H;
1232 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
1233 if (g_pars.use_osmux) {
1234 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1235 resp := tr_CRCX_ACK_osmux;
1236 } else {
1237 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1238 resp := tr_CRCX_ACK;
1239 }
Pau Espin Pedrol07866632020-09-03 19:10:55 +02001240 cmd.sdp := ts_SDP(host_msc, host_mgw_rtp_v4, "23", "42",
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001241 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1242 { valueof(ts_SDP_ptime(20)) });
1243 mgcp_transceive_mgw(cmd, resp);
1244 }
1245
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001246 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001247
1248 if (st.is_assignment and st.assignment_done) {
1249 if (not st.old_lchan_deact_sacch_seen) {
1250 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1251 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1252 }
1253 if (st.old_lchan_rll_rel_req_seen) {
1254 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1255 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1256 }
1257 }
Harald Welte930d0a72018-03-22 22:08:40 +01001258}
1259
Harald Welte261af4b2018-02-12 21:20:39 +01001260type record HandoverState {
1261 /* Assignment related bits */
1262 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001263 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001264 boolean handover_done,
1265 RslChannelNr old_chan_nr
1266};
1267
1268altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1269 var RSL_Message rsl;
1270 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1271 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1272 log("Rx L3 from net: ", l3);
1273 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1274 var RslChannelNr new_chan_nr;
1275 var GsmArfcn arfcn;
1276 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1277 new_chan_nr, arfcn);
1278 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1279
1280 /* register our component for this channel number at the RSL Emulation */
1281 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1282
1283 /* resume processing of RSL DChan messages, which was temporarily suspended
1284 * before performing a hand-over */
1285 f_rslem_resume(RSL1_PROC);
1286
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001287 /* send handover detect */
1288 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1289 f_sleep(0.3);
1290
Harald Welte261af4b2018-02-12 21:20:39 +01001291 /* send handover complete over the new channel */
1292 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001293 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001294 enc_PDU_ML3_MS_NW(l3_tx)));
1295 /* by default, send via the new channel from now */
1296 st.old_chan_nr := g_chan_nr;
1297 g_chan_nr := new_chan_nr;
1298 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001299
1300 /* Memorize how many MDCX we have seen before. We need this number to detect
1301 * that we have received the handover related MDCX */
1302 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001303 repeat;
1304 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001305 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001306 }
1307 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001308 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Philipp Maier09f1c6d2020-09-14 21:28:52 +02001309 [st.rr_ho_cmpl_seen] as_Media_mgw();
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001310 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1311 repeat;
1312 }
Harald Welte261af4b2018-02-12 21:20:39 +01001313 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1314 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1315 repeat;
1316 }
1317 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1318 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1319 /* unregister for old channel number in RSL emulation */
1320 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1321 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001322
1323 /* The channel release must not necessarly be synchronized to the RSL handover
1324 * procedure since those events may happen independently nearly at the same
1325 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1326 * IPACC or MGCP level may be still in progress. In order to make sure that
1327 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
Neels Hofmeyrffd2ef12020-10-12 18:45:58 +00001328 * as well as an MDCX on IPACC level.
1329 * If ipa_crcx_seen is false, this is not a voice channel and we need not check MGCP at all.. */
1330 if (g_media.bts.ipa_crcx_seen
1331 and (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1332 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false)) {
Philipp Maierc1e95c82018-07-18 16:03:00 +02001333 repeat;
1334 } else {
1335 st.handover_done := true;
1336 }
Harald Welte261af4b2018-02-12 21:20:39 +01001337 }
1338}
1339
1340
Harald Weltec1a2fff2017-12-17 11:06:19 +01001341
Harald Welte28d943e2017-11-25 15:00:50 +01001342}