blob: 7b8f087db1acc92bf75135c45985adaaac35de40 [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
Harald Weltec1a2fff2017-12-17 11:06:19 +010033import from RSL_Emulation all;
34import from RSL_Types all;
35
36import from MobileL3_Types all;
37import from MobileL3_CommonIE_Types all;
Harald Welte211219e2018-01-29 22:03:36 +010038import from MobileL3_RRM_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +010039import from L3_Templates all;
40
Harald Weltec20b1c42018-02-12 20:50:08 +010041import from TELNETasp_PortType all;
42import from Osmocom_VTY_Functions all;
43
Harald Weltec1a2fff2017-12-17 11:06:19 +010044
Harald Welte211219e2018-01-29 22:03:36 +010045/***********************************************************************
46 * Media related handling
47 ***********************************************************************/
48
Philipp Maier11a58942018-06-25 16:40:48 +020049/* Get the matching payload type for a specified BSSAP codec type
50 * (see also: BSSAP_Types.ttcn */
51private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
52 if (codecType == GSM_FR) {
53 return PT_GSM;
54 } else if (codecType == GSM_HR) {
55 return PT_GSMHR;
56 } else if (codecType == GSM_EFR) {
57 return PT_GSMEFR;
58 } else if (codecType == FR_AMR or codecType == HR_AMR) {
59 return PT_AMR;
60 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
61 return PT_AMRWB;
62 }
63
64 return PT_PCMU;
65}
66
Harald Welte211219e2018-01-29 22:03:36 +010067/* Tuple containing host/ip and port */
68type record HostPort {
69 HostName host,
70 PortNumber port_nr
71};
72
73/* State encapsulating one MGCP Connection */
74type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020075 integer crcx_seen, /* Counts how many CRCX operations happend */
76 integer mdcx_seen, /* Counts how many MDCX operations happend C */
77 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
78 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010079 MgcpConnectionId conn_id,
80 charstring mime_type, /* e.g. AMR */
81 integer sample_rate, /* 8000 */
82 integer ptime, /* 20 */
83 uint7_t rtp_pt, /* RTP Payload Type */
84 HostPort mgw, /* MGW side */
85 HostPort peer /* CA side */
86};
87
88/* BTS media state */
89type record BtsMediaState {
90 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +020091 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +010092 uint16_t conn_id,
93 uint7_t rtp_pt,
94 HostPort bts,
95 HostPort peer
96};
97
98type record MediaState {
99 MgcpEndpoint mgcp_ep,
100 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +0100101 BtsMediaState bts,
102 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +0100103};
104
Philipp Maier11a58942018-06-25 16:40:48 +0200105function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +0100106 /* BTS Side */
107 g_media.bts := {
108 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200109 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +0100110 conn_id := nr,
111 rtp_pt := 0,
112 bts := {
113 host := bts,
114 port_nr := 9000 + nr*2
115 },
116 peer := -
117 }
118
Harald Welte261af4b2018-02-12 21:20:39 +0100119 g_media.bts1 := {
120 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200121 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100122 conn_id := nr,
123 rtp_pt := 0,
124 bts := {
125 host := bts, /* FIXME */
126 port_nr := 9000 + nr*2
127 },
128 peer := -
129 }
130
Harald Welte363cb0a2018-01-30 19:35:53 +0100131 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100132
133 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200134 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100135 g_media.mgcp_conn[i].sample_rate := 8000;
136 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200137 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200138 g_media.mgcp_conn[i].crcx_seen := 0;
139 g_media.mgcp_conn[i].mdcx_seen := 0;
140 g_media.mgcp_conn[i].crcx_seen_exp := 0;
141 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100142 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
143 }
144
145 g_media.mgcp_conn[0].mgw := {
146 host := mgw,
147 port_nr := 10000 + nr*2
148 }
149 g_media.mgcp_conn[1].mgw := {
150 host := mgw,
151 port_nr := 11000 + nr*2
152 }
153}
154
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200155/* Helper function to get the next free MGCP connection identifier. We can
156 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100157private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
158 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200159 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100160 return i;
161 }
162 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100163 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Only 2 Connections per EP!");
164 /* Should never be reached */
165 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100166}
167
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200168/* Helper function to pick a specific connection by its cid. Since we reach out
169 * for a connection that is in-use we also check if there was already exactly
170 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100171private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
172 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200173 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 +0100174 return i;
175 }
176 }
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100177 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("No Connection for ID ", cid));
178 /* Should not be reached */
179 return -1;
Harald Welte211219e2018-01-29 22:03:36 +0100180}
181
Philipp Maierb2422352018-07-11 10:36:57 +0200182/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
183 * RSL level media handling */
184altstep as_Media_ipacc() runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100185 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100186 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100187 var boolean b_unused;
Harald Welte211219e2018-01-29 22:03:36 +0100188 [not g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
189 /* Extract parameters from request + use in response */
190 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
191 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
192 }
193 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
194 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
195 }
196 RSL.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
197 oct2int(f_inet_addr(g_media.bts.bts.host)),
198 g_media.bts.bts.port_nr,
199 g_media.bts.rtp_pt));
200 g_media.bts.ipa_crcx_seen := true;
201 repeat;
202 }
203 [g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
204 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100205 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100206 if (g_media.bts.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100207 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte211219e2018-01-29 22:03:36 +0100208 }
209 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100210 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100211 g_media.bts.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100212 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100213 g_media.bts.peer.port_nr := ie.ipa_remote_port;
214 /* optional */
215 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
216 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
217 }
218 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
219 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
220 }
221 RSL.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
222 oct2int(f_inet_addr(g_media.bts.peer.host)),
223 g_media.bts.peer.port_nr,
224 g_media.bts.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200225 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100226 repeat;
227 }
Harald Welte261af4b2018-02-12 21:20:39 +0100228
229 /* on second (new) BTS during hand-over */
230 [not g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
231 /* Extract parameters from request + use in response */
232 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
233 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
234 }
235 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
236 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
237 }
238 RSL1.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
239 oct2int(f_inet_addr(g_media.bts1.bts.host)),
240 g_media.bts1.bts.port_nr,
241 g_media.bts1.rtp_pt));
242 g_media.bts1.ipa_crcx_seen := true;
243 repeat;
244 }
245 /* on second (new) BTS during hand-over */
246 [g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
247 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100248 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100249 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100250 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("IPA MDCX for unknown ConnId", rsl));
Harald Welte261af4b2018-02-12 21:20:39 +0100251 }
252 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100253 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100254 g_media.bts1.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100255 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100256 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
257 /* optional */
258 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
259 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
260 }
261 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
262 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
263 }
264 RSL1.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
265 oct2int(f_inet_addr(g_media.bts1.peer.host)),
266 g_media.bts1.peer.port_nr,
267 g_media.bts1.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200268 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100269 repeat;
270 }
271
Philipp Maierb2422352018-07-11 10:36:57 +0200272
273}
274
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200275function f_rx_crcx(MgcpCommand mgcp_cmd)
276 runs on MSC_ConnHdlr return template MgcpResponse {
277 var MgcpOsmuxCID osmux_cid;
278 var SDP_Message sdp;
279 var integer cid := f_get_free_mgcp_conn();
280 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
281 if (cid != 0) {
282 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MGCP wildcard EP only works in first CRCX");
283 }
284 /* we keep the endpoint name allocated during MediaState_init */
285 } else {
286 /* Call Agent allocated endpoint, trust/use it always */
287 g_media.mgcp_ep := mgcp_cmd.line.ep;
288 }
289 if (isvalue(mgcp_cmd.sdp)) {
290 sdp := mgcp_cmd.sdp;
291 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
292 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
293 }
294 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
295 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, mgcp_conn.mgw.host, "foo", "21",
296 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
297 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
298 mgcp_conn.mime_type & "/" &
299 int2str(mgcp_conn.sample_rate))),
300 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
301 var template MgcpResponse mgcp_resp;
302 if (g_pars.use_osmux and f_MgcpCmd_contains_par(mgcp_cmd, "X-OSMUX")) {
303 osmux_cid := f_MgcpCmd_extract_osmux_cid(mgcp_cmd);
304 mgcp_resp := ts_CRCX_ACK_osmux(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, osmux_cid, sdp);
305 } else {
306 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
307 }
308 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
309 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
310 return mgcp_resp;
311}
312
313function f_rx_mdcx(MgcpCommand mgcp_cmd)
314 runs on MSC_ConnHdlr return template MgcpResponse {
315 var SDP_Message sdp;
316 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
317 if (isvalue(mgcp_cmd.sdp)) {
318 sdp := mgcp_cmd.sdp;
319 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
320 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
321 } else {
322 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "MDCX has no [recognizable] SDP");
323 }
324 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
325 sdp := valueof(ts_SDP(mgcp_conn.peer.host, mgcp_conn.peer.host, "foo", "21",
326 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
327 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
328 mgcp_conn.mime_type & "/" &
329 int2str(mgcp_conn.sample_rate))),
330 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
331 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
332 return ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
333}
334
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200335function tr_MGCP_RecvFrom_any(template MgcpMessage msg)
336runs on MSC_ConnHdlr return template MGCP_RecvFrom {
337 var template MGCP_RecvFrom mrf := {
338 connId := ?,
339 remName := ?,
340 remPort := ?,
341 locName := ?,
342 locPort := ?,
343 msg := msg
344 }
345 return mrf;
346}
347
Philipp Maierb2422352018-07-11 10:36:57 +0200348/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
349 * MGW level media handling */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200350
Philipp Maierc1e95c82018-07-18 16:03:00 +0200351altstep as_Media_mgw(boolean norepeat := false) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200352 var MgcpCommand mgcp_cmd;
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200353 var template MgcpResponse mgcp_resp;
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200354 var MGCP_RecvFrom mrf;
355 var template MgcpMessage msg_crcx := {
356 command := tr_CRCX
357 }
358 var template MgcpMessage msg_mdcx := {
359 command := tr_MDCX
360 }
361 var template MgcpMessage msg_resp;
Philipp Maierb2422352018-07-11 10:36:57 +0200362
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200363 [g_pars.aoip] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200364 mgcp_resp := f_rx_crcx(mgcp_cmd);
Harald Welte363cb0a2018-01-30 19:35:53 +0100365 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200366 if(norepeat == false) {
367 repeat;
368 }
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200369 }
370
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200371 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_crcx)) -> value mrf {
372 mgcp_resp := f_rx_crcx(mrf.msg.command);
373 msg_resp := {
374 response := mgcp_resp
375 }
376 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
377 if(norepeat == false) {
378 repeat;
379 }
380 }
381
382 [g_pars.aoip] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
Pau Espin Pedrol513b8312019-06-18 17:37:36 +0200383 mgcp_resp := f_rx_mdcx(mgcp_cmd);
384 MGCP.send(mgcp_resp);
Philipp Maierc1e95c82018-07-18 16:03:00 +0200385 if(norepeat == false) {
386 repeat;
387 }
Harald Welte211219e2018-01-29 22:03:36 +0100388 }
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200389
390 [not g_pars.aoip] MGCP_MULTI.receive(tr_MGCP_RecvFrom_any(msg_mdcx)) -> value mrf {
391 mgcp_resp := f_rx_mdcx(mrf.msg.command);
392 msg_resp := {
393 response := mgcp_resp
394 }
395 MGCP_MULTI.send(t_MGCP_SendToMrf(mrf, msg_resp));
396 if(norepeat == false) {
397 repeat;
398 }
399 }
Harald Welte211219e2018-01-29 22:03:36 +0100400}
401
Philipp Maierb2422352018-07-11 10:36:57 +0200402/* Altsteps for handling of media related commands. Can be activated by a given
403 * test case if it expects to see media related handling (i.e. voice calls) */
404altstep as_Media() runs on MSC_ConnHdlr {
405 [] as_Media_ipacc();
406 [] as_Media_mgw();
407}
Harald Welte211219e2018-01-29 22:03:36 +0100408
Harald Welte28d943e2017-11-25 15:00:50 +0100409/* this component represents a single subscriber connection at the MSC.
Harald Welte6811d102019-04-14 22:23:14 +0200410 * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components.
411 * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */
412type component MSC_ConnHdlr extends RAN_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100413 /* SCCP Connecction Identifier for the underlying SCCP connection */
414 var integer g_sccp_conn_id;
415
Harald Welte6811d102019-04-14 22:23:14 +0200416 /* procedure port back to our parent (RAN_Emulation_CT) for control */
417 port RAN_PROC_PT RAN;
Harald Weltec20b1c42018-02-12 20:50:08 +0100418 port TELNETasp_PT BSCVTY;
Harald Welte28d943e2017-11-25 15:00:50 +0100419
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200420 /* Proxy MGCP-over-IPA and MGCP-over-UDP */
421 port IPA_MGCP_PT MGCP_MSC_CLIENT;
422 var integer g_trans_id := 0;
423
Harald Welte211219e2018-01-29 22:03:36 +0100424 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100425 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100426
Pau Espin Pedrol392963f2019-06-18 17:17:19 +0200427 var charstring host_bts := "127.0.0.2";
428 var charstring host_mgw := "127.0.0.3";
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200429 var charstring host_msc := "127.0.0.4";
430
Harald Weltee97eab42018-03-21 18:46:06 +0100431 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100432}
433
Harald Welteeddf0e92020-06-21 19:42:15 +0200434function f_MscConnHdlr_init_vty() runs on MSC_ConnHdlr {
Harald Weltee97eab42018-03-21 18:46:06 +0100435 if (not g_vty_initialized) {
436 map(self:BSCVTY, system:BSCVTY);
437 f_vty_set_prompts(BSCVTY);
438 f_vty_transceive(BSCVTY, "enable");
439 g_vty_initialized := true;
440 }
Harald Welte28d943e2017-11-25 15:00:50 +0100441}
442
Harald Welteeddf0e92020-06-21 19:42:15 +0200443/* initialize all parameters */
444function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
445 f_MediaState_init(g_media, i, bts, mgw, codecType);
446 f_MscConnHdlr_init_vty();
447}
448
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200449private function get_next_trans_id() runs on MSC_ConnHdlr return MgcpTransId {
450 var MgcpTransId tid := int2str(g_trans_id);
451 g_trans_id := g_trans_id + 1;
452 return tid;
453}
454
Harald Welte6811d102019-04-14 22:23:14 +0200455/* Callback function from general RAN_Emulation whenever a connectionless
Harald Welte28d943e2017-11-25 15:00:50 +0100456 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
457private function UnitdataCallback(PDU_BSSAP bssap)
Harald Welte6811d102019-04-14 22:23:14 +0200458runs on RAN_Emulation_CT return template PDU_BSSAP {
Harald Welte28d943e2017-11-25 15:00:50 +0100459 var template PDU_BSSAP resp := omit;
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200460 var boolean append_osmux_support := g_ran_ops.use_osmux and
461 (g_ran_ops.transport == BSSAP_TRANSPORT_AoIP);
Harald Welte28d943e2017-11-25 15:00:50 +0100462
Harald Weltec1a2fff2017-12-17 11:06:19 +0100463 /* answer all RESET with a RESET ACK */
Pau Espin Pedrol95549182019-06-06 16:17:30 +0200464 if (match(bssap, tr_BSSMAP_Reset(append_osmux_support))) {
465 resp := ts_BSSMAP_ResetAck(append_osmux_support);
Harald Welte28d943e2017-11-25 15:00:50 +0100466 }
467
468 return resp;
469}
470
Harald Welte6811d102019-04-14 22:23:14 +0200471const RanOps MSC_RanOps := {
472 create_cb := refers(RAN_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100473 unitdata_cb := refers(UnitdataCallback),
474 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100475 role_ms := false,
Harald Welte2fce7882019-04-15 11:48:05 +0200476 protocol := RAN_PROTOCOL_BSSAP,
Pau Espin Pedrolc6b78ff2019-06-06 15:58:17 +0200477 transport := BSSAP_TRANSPORT_AoIP,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200478 use_osmux := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100479 sccp_addr_local := omit,
480 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100481}
482
Daniel Willmann191e0d92018-01-17 12:44:35 +0100483const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100484 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
485 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100486}
487
Harald Weltec1a2fff2017-12-17 11:06:19 +0100488/* register an expect with the BSSMAP core */
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200489function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Welte6811d102019-04-14 22:23:14 +0200490 RAN.call(RAN_register:{l3_enc, self}) {
491 [] RAN.getreply(RAN_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100492 }
493}
494
Harald Welte651fcdc2018-05-10 20:23:16 +0200495type record TestHdlrEncrParams {
496 OCT1 enc_alg,
497 octetstring enc_key
498};
499
500template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg, octetstring key) := {
501 enc_alg := alg,
502 enc_key := key
503}
504
Harald Weltecc0b0142018-05-29 15:19:33 +0200505type record TestHdlrParamsLcls {
506 GlobalCallReferenceValue gcr optional,
507 /* LCLS Configuration */
508 BIT4 cfg optional,
509 /* LCLS Connection Status Control */
510 BIT4 csc optional,
Max2eeb5112018-11-06 19:21:41 +0100511 BIT4 exp_sts optional,
512 /* Whether to adjust *cx_seen_exp for LCLS tests */
513 boolean adjust_cx_exp
Harald Weltecc0b0142018-05-29 15:19:33 +0200514}
515
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200516type record TestHdlrParamsMSCPool {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200517 integer bssap_idx,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200518 integer rsl_idx,
519 PDU_ML3_MS_NW l3_info optional
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200520}
521
Harald Weltec1a2fff2017-12-17 11:06:19 +0100522type record TestHdlrParams {
523 OCT1 ra,
524 GsmFrameNumber fn,
525 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100526 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200527 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200528 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200529 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Philipp Maierd0e64b02019-03-13 14:15:23 +0100530 bitstring expect_mr_s0_s7 optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200531 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100532 TestHdlrParamsLcls lcls,
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200533 SCCP_PAR_Address sccp_addr_msc optional,
534 SCCP_PAR_Address sccp_addr_bsc optional,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100535 uint5_t exp_ms_power_level,
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100536 boolean exp_ms_power_params,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200537 boolean aoip,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200538 boolean use_osmux,
539 TestHdlrParamsMSCPool mscpool
Harald Weltec1a2fff2017-12-17 11:06:19 +0100540};
541
Philipp Maier48604732018-10-09 15:00:37 +0200542/* Note: Do not use valueof() to get a value of this template, use
543 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
Pau Espin Pedrol8df10112020-01-09 18:21:53 +0100544 * matched to the current test situation (aoip vs. sccplite) */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100545template (value) TestHdlrParams t_def_TestHdlrPars := {
546 ra := '23'O,
547 fn := 23,
548 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100549 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200550 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200551 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200552 expect_mr_conf_ie := omit,
Philipp Maierd0e64b02019-03-13 14:15:23 +0100553 expect_mr_s0_s7 := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200554 encr := omit,
555 lcls := {
556 gcr := omit,
557 cfg := omit,
558 csc := omit,
Max2eeb5112018-11-06 19:21:41 +0100559 exp_sts := omit,
560 adjust_cx_exp := true
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100561 },
Neels Hofmeyr90f80962020-06-12 16:16:55 +0200562 sccp_addr_msc := omit,
563 sccp_addr_bsc := omit,
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100564 exp_ms_power_level := 7, /* calculated from osmo-bsc.cfg "ms max power" */
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100565 exp_ms_power_params := false,
Pau Espin Pedrolc6a53db2019-05-20 19:31:47 +0200566 aoip := true,
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200567 use_osmux := false,
568 mscpool := {
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200569 bssap_idx := 0,
Neels Hofmeyr4f118412020-06-04 15:25:10 +0200570 rsl_idx := 0,
571 l3_info := omit
Neels Hofmeyrf246a922020-05-13 02:27:10 +0200572 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100573}
574
Harald Weltea0630032018-03-20 21:09:55 +0100575function f_create_chan_and_exp() runs on MSC_ConnHdlr {
576 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100577 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100578 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
579
580 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Harald Weltea0630032018-03-20 21:09:55 +0100581 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn);
Daniel Willmann191e0d92018-01-17 12:44:35 +0100582 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100583}
584
Harald Welte898113b2018-01-31 18:32:21 +0100585function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
586 template (omit) RslChannelNr chan_nr := omit) runs on MSC_ConnHdlr {
587 if (not isvalue(link_id)) {
588 link_id := ts_RslLinkID_DCCH(0);
589 }
590 if (not isvalue(chan_nr)) {
591 chan_nr := g_chan_nr;
592 }
593 RSL.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
594}
595
Harald Weltec1a2fff2017-12-17 11:06:19 +0100596function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig) runs on MSC_ConnHdlr {
597 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100598 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100599 if (orig.msg_type == RSL_MT_ENCR_CMD) {
600 link_id := orig.ies[2].body.link_id;
601 } else {
602 link_id := orig.ies[1].body.link_id;
603 }
Harald Welte898113b2018-01-31 18:32:21 +0100604 f_rsl_send_l3(l3, link_id, chan_nr);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100605}
606
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100607/* Convert the chipher representation on BSSMAP to the representation used on RSL */
Harald Weltee613f962018-04-18 22:38:16 +0200608function f_chipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100609{
610 /* A5 0 */
611 if (alg_bssmap == '01'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200612 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100613 }
614 /* A5 1 */
615 else if (alg_bssmap == '02'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200616 return RSL_ALG_ID_A5_1;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100617 }
618 /* A5 2 */
619 else if (alg_bssmap == '04'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200620 return RSL_ALG_ID_A5_2;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100621 }
622 /* A5 3 */
623 else if (alg_bssmap == '08'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200624 return RSL_ALG_ID_A5_3;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100625 }
626 /* A5 4 */
627 else if (alg_bssmap == '10'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200628 return RSL_ALG_ID_A5_4;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100629 }
630 /* A5 5 */
631 else if (alg_bssmap == '20'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200632 return RSL_ALG_ID_A5_5;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100633 }
634 /* A5 6 */
635 else if (alg_bssmap == '40'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200636 return RSL_ALG_ID_A5_6;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100637 }
638 /* A5 7 */
639 else if (alg_bssmap == '80'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200640 return RSL_ALG_ID_A5_7;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100641 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100642 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption Algorithm");
Harald Weltee613f962018-04-18 22:38:16 +0200643 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100644 }
645}
646
Harald Welte38b2a102017-12-23 02:42:58 +0100647function f_cipher_mode(OCT1 alg, OCT8 key, template OCT16 kc128 := omit, boolean exp_fail := false)
648runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100649 var PDU_BSSAP bssap;
650 var RSL_Message rsl;
Harald Weltee613f962018-04-18 22:38:16 +0200651 var RSL_AlgId alg_rsl;
Harald Welte73cd2712017-12-17 00:44:52 +0100652
653 if (isvalue(kc128)) {
654 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(alg, key, valueof(kc128)));
655 } else {
656 BSSAP.send(ts_BSSMAP_CipherModeCmd(alg, key));
657 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100658
659 /* RSL uses a different representation of the encryption algorithm,
660 * so we need to convert first */
661 alg_rsl := f_chipher_mode_bssmap_to_rsl(alg);
662
Harald Welte73cd2712017-12-17 00:44:52 +0100663 alt {
664 /* RSL/UE Side */
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100665 [] RSL.receive(tr_RSL_ENCR_CMD(g_chan_nr, ?, alg_rsl, key)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100666 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
667 log("Rx L3 from net: ", l3);
668 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
669 f_rsl_reply(ts_RRM_CiphModeCompl, rsl);
670 }
671 repeat;
672 }
673 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
674 // bssap.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algoritmhIdentifier
Harald Welte38b2a102017-12-23 02:42:58 +0100675 if (exp_fail == true) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100676 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Cipher Mode Complete");
Harald Welte38b2a102017-12-23 02:42:58 +0100677 } else {
678 setverdict(pass);
679 }
Harald Welte73cd2712017-12-17 00:44:52 +0100680 }
681 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100682 if (exp_fail == false) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100683 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Ciphering Mode Reject");
Harald Welte38b2a102017-12-23 02:42:58 +0100684 } else {
685 setverdict(pass);
686 }
Harald Welte73cd2712017-12-17 00:44:52 +0100687 }
688 }
689}
690
Harald Welte211219e2018-01-29 22:03:36 +0100691/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
692function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
693 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100694 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100695
696 if (match(inp, '00001'B)) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100697 chan_nr := valueof(t_RslChanNr_Bm(tn));
Harald Welte211219e2018-01-29 22:03:36 +0100698 }
699 else if (match(inp, '0001?'B)) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100700 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Harald Welte211219e2018-01-29 22:03:36 +0100701 }
702 else if (match(inp, '001??'B)) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100703 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Harald Welte211219e2018-01-29 22:03:36 +0100704 }
705 else if (match(inp, '01???'B)) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100706 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Harald Welte211219e2018-01-29 22:03:36 +0100707 }
708 else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100709 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unknown ChDesc!");
Harald Welte211219e2018-01-29 22:03:36 +0100710 }
711
712 if (ch_desc.octet3 and4b '10'O == '10'O) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100713 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "No support for Hopping");
Harald Welte211219e2018-01-29 22:03:36 +0100714 } else {
715 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
716 arfcn := oct2int(concat);
717 }
718}
719
720type record AssignmentState {
721 /* global */
722 boolean voice_call,
723 boolean is_assignment,
724 /* Assignment related bits */
725 boolean rr_ass_cmpl_seen,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100726 boolean old_lchan_deact_sacch_seen,
727 boolean old_lchan_rll_rel_req_seen,
Harald Welte21583082018-01-29 22:28:26 +0100728 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100729 RslChannelNr old_chan_nr,
730 /* Modify related bits */
731 boolean rr_modify_seen,
Harald Welte21583082018-01-29 22:28:26 +0100732 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100733}
734
735template (value) AssignmentState ts_AssignmentStateInit := {
736 voice_call := false,
737 is_assignment := false,
738 rr_ass_cmpl_seen := false,
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100739 old_lchan_deact_sacch_seen := false,
740 old_lchan_rll_rel_req_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100741 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100742 old_chan_nr := -,
743 rr_modify_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100744 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100745}
746
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200747private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
748 encr_info := {
749 len := ?,
750 alg_id := alg,
751 key := key
752 }
753}
754
755/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
756private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
757 var RSL_IE_Body encr_info;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100758 var RSL_IE_Body ms_power_param;
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100759 var RSL_IE_Body ms_power;
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100760
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200761 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg != '01'O) {
762 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100763 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Missing Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200764 } else {
765 var RSL_AlgId alg := f_chipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg);
766 if (not match(encr_info, tr_EncrInfo(alg, g_pars.encr.enc_key))) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100767 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Wrong Encryption IE in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200768 }
769 }
770 } else {
771 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
772 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100773 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unexpected Encryption in CHAN ACT");
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200774 }
775 }
776 }
777 /* 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 +0100778 * RSL_IE_TIMING_ADVANCE */
Pau Espin Pedrol3d008ae2019-10-28 13:35:46 +0100779
Pau Espin Pedrol8f773632019-11-05 11:46:53 +0100780 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 +0100781 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power Parameters not found in CHAN ACT");
782 }
783
Pau Espin Pedrolf7630a62019-10-28 15:07:08 +0100784 if (not f_rsl_find_ie(chan_act, RSL_IE_MS_POWER, ms_power)) {
785 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "IE MS Power not found in CHAN ACT");
786 } else {
787 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 +0100788 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 +0100789 }
790 }
791
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200792}
793
Harald Welte211219e2018-01-29 22:03:36 +0100794altstep as_assignment(inout AssignmentState st) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100795 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100796 [not st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
797 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
798 log("Rx L3 from net: ", l3);
799 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
800 var RslChannelNr new_chan_nr;
801 var GsmArfcn arfcn;
802 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
803 new_chan_nr, arfcn);
804 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100805
Harald Welte211219e2018-01-29 22:03:36 +0100806 /* register our component for this channel number at the RSL Emulation */
807 f_rslem_register(0, new_chan_nr);
Vadim Yanitskiy539e6302020-06-22 03:11:54 +0700808 /* dispatch queued messages for this channel (if any) */
809 f_rslem_dchan_queue_dispatch();
810
Harald Welte211219e2018-01-29 22:03:36 +0100811 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
812 /* send assignment complete over the new channel */
Neels Hofmeyrb0e73652018-07-14 21:56:51 +0200813 RSL.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +0100814 enc_PDU_ML3_MS_NW(l3_tx)));
815 /* by default, send via the new channel from now */
816 st.old_chan_nr := g_chan_nr;
817 g_chan_nr := new_chan_nr;
818 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200819 /* obtain channel activation from RSL_Emulation for new channel */
820 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
821 /* check it (e.g. for correct ciphering parameters) */
822 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +0100823 repeat;
824 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +0100825 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100826 }
Harald Welte211219e2018-01-29 22:03:36 +0100827 }
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100828 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
Neels Hofmeyra5302c82018-11-04 23:09:58 +0100829 st.old_lchan_deact_sacch_seen := true;
Neels Hofmeyr861a4c12018-11-07 01:23:17 +0100830 repeat;
831 }
Harald Welte211219e2018-01-29 22:03:36 +0100832 [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 +0100833 st.old_lchan_rll_rel_req_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100834 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
835 repeat;
836 }
837 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
838 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +0100839 /* unregister for old channel number in RSL emulation */
840 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
841 f_rslem_unregister(0, st.old_chan_nr);
Harald Welte21583082018-01-29 22:28:26 +0100842 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +0100843 repeat;
844 }
845}
846
847altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
848 /* no assignment, just mode modify */
849 var RSL_Message rsl;
850
851 [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 +0100852 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.channelModeModify)) {
855 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
856 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +0100857 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100858 }
859 repeat;
860 }
Harald Welte211219e2018-01-29 22:03:36 +0100861 [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 +0100862 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +0100863 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100864 repeat;
865 }
Harald Welte211219e2018-01-29 22:03:36 +0100866}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100867
Harald Welte211219e2018-01-29 22:03:36 +0100868/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
869function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
870return boolean {
871 select (bssmap.speechOrDataIndicator) {
872 case ('0011'B) { /* Signalling */
873 /* all channels support signalling */
874 return true;
875 }
876 case else { /* Speech, Speech+CTM or CSD */
877 select (bssmap.channelRateAndType) {
878 case ('08'O) { /* TCH/F */
879 select (rsl_chan_nr) {
880 case (t_RslChanNr_Bm(?)) { return true; }
881 }
882 }
883 case ('09'O) { /* TCH/H */
884 select (rsl_chan_nr) {
885 case (t_RslChanNr_Lm(?, ?)) { return true; }
886 }
887 }
888 case else { /* full or half-rate */
889 select (rsl_chan_nr) {
890 case (t_RslChanNr_Bm(?)) { return true; }
891 case (t_RslChanNr_Lm(?, ?)) { return true; }
892 }
893 }
894 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100895 }
Harald Welte211219e2018-01-29 22:03:36 +0100896 }
897 return false;
898}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100899
Philipp Maier8ef527e2018-05-18 11:12:50 +0200900/* Determine if the channel mode specified within rsl_chan_nr requires a
901 * MODE MODIFY in to match the channel mode specified by given BSSMAP
902 * ChannelType */
903function f_channel_needs_modify(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
904return boolean {
905
906 /* FIXME: This tests the rsl_chan_nr to determine if we are on a
907 * signalling channel or not. Unfortunately this may lead to false
908 * results if we are on a TCH. The problem is that a TCH may be also
909 * used in signalling mode, but this function assumes that only SDCCH4
910 * and SDCCH8 are used as signalling channels at all. */
911
912 var boolean current_signalling := false;
913 var boolean desired_signalling := false;
914
915 select (rsl_chan_nr) {
916 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
917 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
918 }
919
920 if (bssmap.speechOrDataIndicator == '0011'B) {
921 desired_signalling := true;
922 }
923
924 if (current_signalling == desired_signalling) {
925 /* The desired channel mode is equal to the one we currently
926 * have, there is no mode modification needed or expected */
927 return false;
928 } else {
929 /* The desired channel mode and the current channel mode do
930 * not match. A mode modification is required */
931 return true;
932 }
933}
934
Harald Weltecc0b0142018-05-29 15:19:33 +0200935/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
936function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
937 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
938 if (istemplatekind(ass_tpl, "omit")) {
939 return;
940 }
941 if (ispresent(g_pars.lcls.gcr)) {
942 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
943 }
944 if (ispresent(g_pars.lcls.cfg)) {
945 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
946 }
947 if (ispresent(g_pars.lcls.csc)) {
948 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
949 }
Harald Welte343b7742018-06-05 23:41:37 +0200950 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
951 if (ispresent(g_pars.lcls.exp_sts)) {
952 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
953 } else {
954 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
955 }
Harald Weltecc0b0142018-05-29 15:19:33 +0200956 }
957}
958
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +0200959/* Send a MGCP request through MGCP-over-IPA from MSC side and receive a (matching!) response */
960function mgcp_transceive_mgw(template MgcpCommand cmd, template MgcpResponse resp := ?) runs on MSC_ConnHdlr {
961 template MgcpResponse resp_any := ?
962 var MgcpResponse resp_val;
963 resp.line.trans_id := cmd.line.trans_id;
964 timer T := 5.0;
965
966 BSSAP.send(cmd);
967 T.start;
968 alt {
969 [] as_Media_mgw();
970 [] BSSAP.receive(resp) -> value resp_val { }
971 [] BSSAP.receive(resp_any) {
972 setverdict(fail, "Response didn't match template");
973 mtc.stop;
974 }
975 [] BSSAP.receive { repeat; }
976 [] T.timeout {
977 setverdict(fail, "Timeout waiting for response to ", cmd);
978 mtc.stop;
979 }
980 }
981 T.stop;
982}
983
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200984/* Helper function to check if the activity on the MGCP matches what we
985 * expected */
986function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
987 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +0200988 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
989 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
990 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
991
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200992 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +0100993 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 +0200994 }
995 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
Max4b0f4962018-11-06 19:20:24 +0100996 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 +0200997 }
998 }
999}
1000
Harald Welte211219e2018-01-29 22:03:36 +01001001/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +02001002function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
1003runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +02001004
1005 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001006 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001007
Philipp Maier4ce978c2020-08-11 22:54:07 +02001008 if (not istemplatekind(ass_tpl, "omit") and isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
Philipp Maier11a58942018-06-25 16:40:48 +02001009 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
1010 } else {
1011 /* Make sure a meaningful default is assigned in case the
1012 * codecList is not populated */
1013 codecType := FR_AMR;
1014 }
1015
Pau Espin Pedrol392963f2019-06-18 17:17:19 +02001016 f_MscConnHdlr_init(g_pars.media_nr, host_bts, host_mgw, codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +02001017
Harald Weltecc0b0142018-05-29 15:19:33 +02001018 /* patch in the LCLS related items, as needed */
1019 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
1020
Harald Welte7a14fd52018-05-10 22:26:55 +02001021 f_create_chan_and_exp();
1022 /* we should now have a COMPL_L3 at the MSC */
Philipp Maier48aeeec2018-09-14 18:20:04 +02001023
1024 var template PDU_BSSAP exp_l3_compl;
1025 exp_l3_compl := tr_BSSMAP_ComplL3()
Philipp Maier48604732018-10-09 15:00:37 +02001026 if (g_pars.aoip == false) {
Philipp Maier48aeeec2018-09-14 18:20:04 +02001027 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := omit;
1028 } else {
1029 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := ?;
1030 }
1031 T.start;
1032 alt {
1033 [] BSSAP.receive(exp_l3_compl);
1034 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001035 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001036 }
1037 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001038 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
Philipp Maier48aeeec2018-09-14 18:20:04 +02001039 }
1040 }
Harald Welte7a14fd52018-05-10 22:26:55 +02001041
1042 /* start ciphering, if requested */
1043 if (ispresent(g_pars.encr)) {
1044 f_cipher_mode(g_pars.encr.enc_alg, g_pars.encr.enc_key);
1045 }
1046
1047 /* bail out early if no assignment requested */
1048 if (istemplatekind(ass_tpl, "omit")) {
1049 return;
1050 }
1051
1052 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +01001053 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +01001054 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +01001055 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +02001056 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +02001057
Harald Welte211219e2018-01-29 22:03:36 +01001058 var ExpectCriteria mgcpcrit := {
1059 connid := omit,
1060 endpoint := omit,
1061 transid := omit
1062 };
1063 var AssignmentState st := valueof(ts_AssignmentStateInit);
1064 /* if the channel type is SIGNAL, we're not handling a voice call */
1065 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
1066 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001067 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +01001068 }
Philipp Maier8ef527e2018-05-18 11:12:50 +02001069
Harald Welte211219e2018-01-29 22:03:36 +01001070 /* determine if the current channel can support the given service or not */
1071 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
1072 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +02001073
1074 /* We decided to assign a new channel, so we do not expect
1075 * any mode modify messages on RSL */
1076 exp_modify := false;
1077 } else {
1078 /* We will continue working with the currently assigned
1079 * channel, we must now check if the mode of the current
1080 * channel is compatible. If not we expect the BSC to modify
1081 * the mode */
1082 exp_modify := f_channel_needs_modify(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +01001083 }
1084
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001085 /* Some test situations will involve MGCP transactions on a media
1086 * gateway. Depending on the situation, we set up how many of each MGCP
1087 * message type are expected to be exchanged. */
1088 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
1089 /* For tests that expect the assignment to fail, assume that
1090 * there will be no MGW communication as well. */
1091 g_media.mgcp_conn[0].crcx_seen_exp := 0;
1092 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
1093 g_media.mgcp_conn[1].crcx_seen_exp := 0;
1094 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
1095 } else if (st.voice_call) {
1096 /* For voice calls we expect the following MGCP activity */
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001097 g_media.mgcp_conn[0].crcx_seen_exp := 1;
1098 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
1099 g_media.mgcp_conn[1].crcx_seen_exp := 1;
1100 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001101 }
1102
Vadim Yanitskiy539e6302020-06-22 03:11:54 +07001103 /* On receipt of the BSSAP Assignment Command, the IUT (osmo-bsc) will allocate
1104 * a channel and send us RR Assignment Command together with ip.access CRCX.
1105 * There is a risk that the RSL Emulation component would dequeue and process
1106 * ip.access CRCX faster than we process the Assignment Command and register
1107 * the corresponding handler for the indicated RSL channel number. This would
1108 * result in a failure, because at that moment there will be no handler for
1109 * ip.access CRCX. Let's guard against this and enable additional queueing. */
1110 f_rslem_dchan_queue_enable();
1111
Harald Welte211219e2018-01-29 22:03:36 +01001112 f_create_mgcp_expect(mgcpcrit);
1113 BSSAP.send(ass_cmd);
1114
1115 T.start;
1116 alt {
1117 /* assignment related bits */
1118 [st.is_assignment] as_assignment(st);
1119
1120 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001121 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +01001122
1123 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
1124 [st.voice_call] as_Media();
1125
1126 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +02001127 [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 +01001128 setverdict(pass);
1129 }
Philipp Maier86f39202018-02-07 14:40:09 +01001130 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
1131 setverdict(pass);
1132 }
Harald Welte21583082018-01-29 22:28:26 +01001133 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +02001134 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +01001135 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001136 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001137 }
1138 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001139 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001140 }
1141 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001142 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received unexpected ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001143 }
1144 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001145 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Received non-matching ASSIGNMENT FAIL");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001146 }
1147 [] T.timeout {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001148 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +01001149 }
1150 }
Harald Welte211219e2018-01-29 22:03:36 +01001151 log("g_media ", g_media);
1152 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +02001153 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +01001154 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001155
Philipp Maiera0976e92018-07-16 15:19:42 +02001156 /* When the BSC detects that LCLS is possible it will cross the
1157 * connetions that point to the PBX side of the MGW. In our case this
1158 * is mgcp_conn[1]. The BSC performs this operation already before the
1159 * assignment complete is generated. This means we expect another MDCX
1160 * at mgcp_conn[1] when LCLS is expected. */
Max2eeb5112018-11-06 19:21:41 +01001161 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 +02001162 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1163 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1164
1165 }
1166 }
1167
Pau Espin Pedrolfd02ad42019-06-18 17:45:20 +02001168 if (not exp_fail and st.voice_call and not g_pars.aoip) {
1169 /* With SCCPLite, connect to BSC-located MGW using a CRCX + SDP.
1170 It is sent in MGCP over IPA in the BSC<->MSC (BSC-NAT)
1171 connection. BSC will forward it to its MGW. */
1172 var template MgcpCommand cmd;
1173 var template MgcpResponse resp;
1174 var integer cic := f_bssmap_ie_cic_2_int(ass_cmd.pdu.bssmap.assignmentRequest.circuitIdentityCode);
1175 var MgcpEndpoint ep := int2str(cic) & "@mgw"; /* 1: matches value configured in BSC_Tests.ttcn pass in AssignReq */
1176 var MgcpCallId call_id := '51234'H;
1177 var SDP_attribute_list attributes := { valueof(ts_SDP_ptime(20)) };
1178 if (g_pars.use_osmux) {
1179 cmd := ts_CRCX_osmux(get_next_trans_id(), ep, "sendrecv", call_id, cic);
1180 resp := tr_CRCX_ACK_osmux;
1181 } else {
1182 cmd := ts_CRCX(get_next_trans_id(), ep, "sendrecv", call_id);
1183 resp := tr_CRCX_ACK;
1184 }
1185 cmd.sdp := ts_SDP(host_msc, host_mgw, "23", "42",
1186 14000, { int2str(g_media.mgcp_conn[1].rtp_pt) },
1187 { valueof(ts_SDP_ptime(20)) });
1188 mgcp_transceive_mgw(cmd, resp);
1189 }
1190
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001191 f_check_mgcp_expectations();
Neels Hofmeyra5302c82018-11-04 23:09:58 +01001192
1193 if (st.is_assignment and st.assignment_done) {
1194 if (not st.old_lchan_deact_sacch_seen) {
1195 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1196 " released properly: expected a Deact SACCH on the old lchan, but saw none.");
1197 }
1198 if (st.old_lchan_rll_rel_req_seen) {
1199 setverdict(fail, "f_establish_fully(): Assignment completed, but the old lchan was not",
1200 " released properly: saw an RLL Release on the old lchan, but expecting none.");
1201 }
1202 }
Harald Welte930d0a72018-03-22 22:08:40 +01001203}
1204
Harald Welte261af4b2018-02-12 21:20:39 +01001205type record HandoverState {
1206 /* Assignment related bits */
1207 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001208 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001209 boolean handover_done,
1210 RslChannelNr old_chan_nr
1211};
1212
1213altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1214 var RSL_Message rsl;
1215 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1216 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1217 log("Rx L3 from net: ", l3);
1218 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1219 var RslChannelNr new_chan_nr;
1220 var GsmArfcn arfcn;
1221 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1222 new_chan_nr, arfcn);
1223 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1224
1225 /* register our component for this channel number at the RSL Emulation */
1226 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1227
1228 /* resume processing of RSL DChan messages, which was temporarily suspended
1229 * before performing a hand-over */
1230 f_rslem_resume(RSL1_PROC);
1231
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001232 /* send handover detect */
1233 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1234 f_sleep(0.3);
1235
Harald Welte261af4b2018-02-12 21:20:39 +01001236 /* send handover complete over the new channel */
1237 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001238 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001239 enc_PDU_ML3_MS_NW(l3_tx)));
1240 /* by default, send via the new channel from now */
1241 st.old_chan_nr := g_chan_nr;
1242 g_chan_nr := new_chan_nr;
1243 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001244
1245 /* Memorize how many MDCX we have seen before. We need this number to detect
1246 * that we have received the handover related MDCX */
1247 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001248 repeat;
1249 } else {
Daniel Willmann0c9e3fa2018-10-29 15:55:12 +01001250 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, log2str("Unexpected L3 received", l3));
Harald Welte261af4b2018-02-12 21:20:39 +01001251 }
1252 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001253 [st.rr_ho_cmpl_seen] as_Media_ipacc();
Pau Espin Pedrol76ba5412019-06-10 11:00:33 +02001254 [st.rr_ho_cmpl_seen] as_Media_mgw(true);
Neels Hofmeyr861a4c12018-11-07 01:23:17 +01001255 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DEACT_SACCH(st.old_chan_nr)) {
1256 repeat;
1257 }
Harald Welte261af4b2018-02-12 21:20:39 +01001258 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1259 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1260 repeat;
1261 }
1262 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1263 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1264 /* unregister for old channel number in RSL emulation */
1265 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1266 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001267
1268 /* The channel release must not necessarly be synchronized to the RSL handover
1269 * procedure since those events may happen independently nearly at the same
1270 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1271 * IPACC or MGCP level may be still in progress. In order to make sure that
1272 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
1273 * as well as an MDCX on IPACC level. */
1274 if (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1275 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false) {
1276 repeat;
1277 } else {
1278 st.handover_done := true;
1279 }
Harald Welte261af4b2018-02-12 21:20:39 +01001280 }
1281}
1282
1283
Harald Weltec1a2fff2017-12-17 11:06:19 +01001284
Harald Welte28d943e2017-11-25 15:00:50 +01001285}