blob: 079cc74bb59a60ccdb475c18eca6ec4b6f9a28af [file] [log] [blame]
Harald Welte28d943e2017-11-25 15:00:50 +01001module MSC_ConnectionHandler {
2
3import from General_Types all;
4import from Osmocom_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +01005import from GSM_Types all;
Harald Welte28d943e2017-11-25 15:00:50 +01006import from SCCPasp_Types all;
7import from BSSAP_Types all;
8import from BSSMAP_Emulation all;
9import from BSSMAP_Templates all;
10
Harald Welte211219e2018-01-29 22:03:36 +010011import from IPL4asp_Types all;
12import from Native_Functions all;
13
Harald Welte28d943e2017-11-25 15:00:50 +010014import from MGCP_Types all;
15import from MGCP_Templates all;
Daniel Willmann191e0d92018-01-17 12:44:35 +010016import from MGCP_Emulation all;
Harald Welte28d943e2017-11-25 15:00:50 +010017import from SDP_Types all;
18
Harald Weltec1a2fff2017-12-17 11:06:19 +010019import from RSL_Emulation all;
20import from RSL_Types all;
21
22import from MobileL3_Types all;
23import from MobileL3_CommonIE_Types all;
Harald Welte211219e2018-01-29 22:03:36 +010024import from MobileL3_RRM_Types all;
Harald Weltec1a2fff2017-12-17 11:06:19 +010025import from L3_Templates all;
26
Harald Weltec20b1c42018-02-12 20:50:08 +010027import from TELNETasp_PortType all;
28import from Osmocom_VTY_Functions all;
29
Harald Weltec1a2fff2017-12-17 11:06:19 +010030
Harald Welte211219e2018-01-29 22:03:36 +010031/***********************************************************************
32 * Media related handling
33 ***********************************************************************/
34
Philipp Maier11a58942018-06-25 16:40:48 +020035/* Get the matching payload type for a specified BSSAP codec type
36 * (see also: BSSAP_Types.ttcn */
37private function f_get_mgcp_pt(BSSMAP_FIELD_CodecType codecType) return SDP_FIELD_PayloadType {
38 if (codecType == GSM_FR) {
39 return PT_GSM;
40 } else if (codecType == GSM_HR) {
41 return PT_GSMHR;
42 } else if (codecType == GSM_EFR) {
43 return PT_GSMEFR;
44 } else if (codecType == FR_AMR or codecType == HR_AMR) {
45 return PT_AMR;
46 } else if (codecType == FR_AMR_WB or codecType == OHR_AMR or codecType == OFR_AMR_WB or codecType == OHR_AMR_WB) {
47 return PT_AMRWB;
48 }
49
50 return PT_PCMU;
51}
52
Harald Welte211219e2018-01-29 22:03:36 +010053/* Tuple containing host/ip and port */
54type record HostPort {
55 HostName host,
56 PortNumber port_nr
57};
58
59/* State encapsulating one MGCP Connection */
60type record MgcpConnState {
Philipp Maier3e2af5d2018-07-11 17:01:05 +020061 integer crcx_seen, /* Counts how many CRCX operations happend */
62 integer mdcx_seen, /* Counts how many MDCX operations happend C */
63 integer crcx_seen_exp, /* Sets the expected number of CRCX operations */
64 integer mdcx_seen_exp, /* Sets the expected number of MDCX operations */
Harald Welte211219e2018-01-29 22:03:36 +010065 MgcpConnectionId conn_id,
66 charstring mime_type, /* e.g. AMR */
67 integer sample_rate, /* 8000 */
68 integer ptime, /* 20 */
69 uint7_t rtp_pt, /* RTP Payload Type */
70 HostPort mgw, /* MGW side */
71 HostPort peer /* CA side */
72};
73
74/* BTS media state */
75type record BtsMediaState {
76 boolean ipa_crcx_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +020077 boolean ipa_mdcx_seen,
Harald Welte211219e2018-01-29 22:03:36 +010078 uint16_t conn_id,
79 uint7_t rtp_pt,
80 HostPort bts,
81 HostPort peer
82};
83
84type record MediaState {
85 MgcpEndpoint mgcp_ep,
86 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +010087 BtsMediaState bts,
88 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +010089};
90
Philipp Maier11a58942018-06-25 16:40:48 +020091function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +010092 /* BTS Side */
93 g_media.bts := {
94 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +020095 ipa_mdcx_seen := false,
Harald Welte211219e2018-01-29 22:03:36 +010096 conn_id := nr,
97 rtp_pt := 0,
98 bts := {
99 host := bts,
100 port_nr := 9000 + nr*2
101 },
102 peer := -
103 }
104
Harald Welte261af4b2018-02-12 21:20:39 +0100105 g_media.bts1 := {
106 ipa_crcx_seen := false,
Philipp Maierc1e95c82018-07-18 16:03:00 +0200107 ipa_mdcx_seen := false,
Harald Welte261af4b2018-02-12 21:20:39 +0100108 conn_id := nr,
109 rtp_pt := 0,
110 bts := {
111 host := bts, /* FIXME */
112 port_nr := 9000 + nr*2
113 },
114 peer := -
115 }
116
Harald Welte363cb0a2018-01-30 19:35:53 +0100117 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100118
119 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200120 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100121 g_media.mgcp_conn[i].sample_rate := 8000;
122 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200123 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200124 g_media.mgcp_conn[i].crcx_seen := 0;
125 g_media.mgcp_conn[i].mdcx_seen := 0;
126 g_media.mgcp_conn[i].crcx_seen_exp := 0;
127 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100128 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
129 }
130
131 g_media.mgcp_conn[0].mgw := {
132 host := mgw,
133 port_nr := 10000 + nr*2
134 }
135 g_media.mgcp_conn[1].mgw := {
136 host := mgw,
137 port_nr := 11000 + nr*2
138 }
139}
140
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200141/* Helper function to get the next free MGCP connection identifier. We can
142 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100143private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
144 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200145 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100146 return i;
147 }
148 }
149 setverdict(fail, "Only 2 Connections per EP!");
Daniel Willmannafce8662018-07-06 23:11:32 +0200150 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100151}
152
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200153/* Helper function to pick a specific connection by its cid. Since we reach out
154 * for a connection that is in-use we also check if there was already exactly
155 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100156private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
157 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200158 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 +0100159 return i;
160 }
161 }
162 setverdict(fail, "No Connection for ID ", cid);
Daniel Willmannafce8662018-07-06 23:11:32 +0200163 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100164}
165
Philipp Maierb2422352018-07-11 10:36:57 +0200166/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
167 * RSL level media handling */
168altstep as_Media_ipacc() runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100169 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100170 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100171 var boolean b_unused;
Harald Welte211219e2018-01-29 22:03:36 +0100172 [not g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
173 /* Extract parameters from request + use in response */
174 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
175 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
176 }
177 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
178 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
179 }
180 RSL.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
181 oct2int(f_inet_addr(g_media.bts.bts.host)),
182 g_media.bts.bts.port_nr,
183 g_media.bts.rtp_pt));
184 g_media.bts.ipa_crcx_seen := true;
185 repeat;
186 }
187 [g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
188 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100189 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100190 if (g_media.bts.conn_id != ie.ipa_conn_id) {
191 setverdict(fail, "IPA MDCX for unknown ConnId", rsl);
Daniel Willmannafce8662018-07-06 23:11:32 +0200192 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100193 }
194 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100195 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100196 g_media.bts.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100197 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100198 g_media.bts.peer.port_nr := ie.ipa_remote_port;
199 /* optional */
200 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
201 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
202 }
203 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
204 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
205 }
206 RSL.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
207 oct2int(f_inet_addr(g_media.bts.peer.host)),
208 g_media.bts.peer.port_nr,
209 g_media.bts.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200210 g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100211 repeat;
212 }
Harald Welte261af4b2018-02-12 21:20:39 +0100213
214 /* on second (new) BTS during hand-over */
215 [not g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
216 /* Extract parameters from request + use in response */
217 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
218 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
219 }
220 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
221 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
222 }
223 RSL1.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
224 oct2int(f_inet_addr(g_media.bts1.bts.host)),
225 g_media.bts1.bts.port_nr,
226 g_media.bts1.rtp_pt));
227 g_media.bts1.ipa_crcx_seen := true;
228 repeat;
229 }
230 /* on second (new) BTS during hand-over */
231 [g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
232 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100233 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100234 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
235 setverdict(fail, "IPA MDCX for unknown ConnId", rsl);
Daniel Willmannafce8662018-07-06 23:11:32 +0200236 mtc.stop;
Harald Welte261af4b2018-02-12 21:20:39 +0100237 }
238 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100239 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100240 g_media.bts1.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100241 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100242 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
243 /* optional */
244 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
245 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
246 }
247 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
248 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
249 }
250 RSL1.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
251 oct2int(f_inet_addr(g_media.bts1.peer.host)),
252 g_media.bts1.peer.port_nr,
253 g_media.bts1.rtp_pt));
Philipp Maierc1e95c82018-07-18 16:03:00 +0200254 g_media.bts1.ipa_mdcx_seen := true;
Harald Welte261af4b2018-02-12 21:20:39 +0100255 repeat;
256 }
257
Philipp Maierb2422352018-07-11 10:36:57 +0200258
259}
260
261/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
262 * MGW level media handling */
Philipp Maierc1e95c82018-07-18 16:03:00 +0200263altstep as_Media_mgw(boolean norepeat := false) runs on MSC_ConnHdlr {
Philipp Maierb2422352018-07-11 10:36:57 +0200264 var MgcpCommand mgcp_cmd;
265
Harald Welte211219e2018-01-29 22:03:36 +0100266 [] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
267 var SDP_Message sdp;
268 var integer cid := f_get_free_mgcp_conn();
Harald Welte363cb0a2018-01-30 19:35:53 +0100269 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
270 if (cid != 0) {
271 setverdict(fail, "MGCP wildcard EP only works in first CRCX");
Daniel Willmannafce8662018-07-06 23:11:32 +0200272 mtc.stop;
Harald Welte363cb0a2018-01-30 19:35:53 +0100273 }
274 /* we keep the endpoint name allocated during MediaState_init */
275 } else {
276 /* Call Agent allocated endpoint, trust/use it always */
277 g_media.mgcp_ep := mgcp_cmd.line.ep;
278 }
Harald Welte211219e2018-01-29 22:03:36 +0100279 if (isvalue(mgcp_cmd.sdp)) {
280 sdp := mgcp_cmd.sdp;
281 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
282 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
283 }
284 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
285 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, mgcp_conn.mgw.host, "foo", "21",
286 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
287 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
288 mgcp_conn.mime_type & "/" &
289 int2str(mgcp_conn.sample_rate))),
290 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
Harald Welte363cb0a2018-01-30 19:35:53 +0100291 var template MgcpResponse mgcp_resp;
292 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
293 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
294 MGCP.send(mgcp_resp);
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200295 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
Philipp Maierc1e95c82018-07-18 16:03:00 +0200296 if(norepeat == false) {
297 repeat;
298 }
Harald Welte211219e2018-01-29 22:03:36 +0100299 }
300 [] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
301 var SDP_Message sdp;
302 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
303 if (isvalue(mgcp_cmd.sdp)) {
304 sdp := mgcp_cmd.sdp;
305 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
306 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
307 } else {
308 setverdict(fail, "MDCX has no [recognizable] SDP");
Harald Welte211219e2018-01-29 22:03:36 +0100309 }
310 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
311 sdp := valueof(ts_SDP(mgcp_conn.peer.host, mgcp_conn.peer.host, "foo", "21",
312 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
313 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
314 mgcp_conn.mime_type & "/" &
315 int2str(mgcp_conn.sample_rate))),
316 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
317 MGCP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200318 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
Philipp Maierc1e95c82018-07-18 16:03:00 +0200319 if(norepeat == false) {
320 repeat;
321 }
Harald Welte211219e2018-01-29 22:03:36 +0100322 }
323}
324
Philipp Maierb2422352018-07-11 10:36:57 +0200325/* Altsteps for handling of media related commands. Can be activated by a given
326 * test case if it expects to see media related handling (i.e. voice calls) */
327altstep as_Media() runs on MSC_ConnHdlr {
328 [] as_Media_ipacc();
329 [] as_Media_mgw();
330}
Harald Welte211219e2018-01-29 22:03:36 +0100331
Harald Welte28d943e2017-11-25 15:00:50 +0100332/* this component represents a single subscriber connection at the MSC.
333 * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components.
334 * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */
Daniel Willmann191e0d92018-01-17 12:44:35 +0100335type component MSC_ConnHdlr extends BSSAP_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100336 /* SCCP Connecction Identifier for the underlying SCCP connection */
337 var integer g_sccp_conn_id;
338
Harald Weltec1a2fff2017-12-17 11:06:19 +0100339 /* procedure port back to our parent (BSSMAP_Emulation_CT) for control */
340 port BSSMAPEM_PROC_PT BSSMAPEM;
Harald Weltec20b1c42018-02-12 20:50:08 +0100341 port TELNETasp_PT BSCVTY;
Harald Welte28d943e2017-11-25 15:00:50 +0100342
Harald Welte211219e2018-01-29 22:03:36 +0100343 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100344 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100345
346 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100347}
348
349/* initialize all parameters */
Philipp Maier11a58942018-06-25 16:40:48 +0200350function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
351 f_MediaState_init(g_media, i, bts, mgw, codecType);
Harald Weltee97eab42018-03-21 18:46:06 +0100352 if (not g_vty_initialized) {
353 map(self:BSCVTY, system:BSCVTY);
354 f_vty_set_prompts(BSCVTY);
355 f_vty_transceive(BSCVTY, "enable");
356 g_vty_initialized := true;
357 }
Harald Welte28d943e2017-11-25 15:00:50 +0100358}
359
360/* Callback function from general BSSMAP_Emulation whenever a connectionless
361 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
362private function UnitdataCallback(PDU_BSSAP bssap)
363runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
364 var template PDU_BSSAP resp := omit;
365
Harald Weltec1a2fff2017-12-17 11:06:19 +0100366 /* answer all RESET with a RESET ACK */
Harald Welte28d943e2017-11-25 15:00:50 +0100367 if (match(bssap, tr_BSSMAP_Reset)) {
368 resp := ts_BSSMAP_ResetAck;
369 }
370
371 return resp;
372}
373
374const BssmapOps MSC_BssmapOps := {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100375 create_cb := refers(BSSMAP_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100376 unitdata_cb := refers(UnitdataCallback),
377 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100378 role_ms := false,
379 sccp_addr_local := omit,
380 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100381}
382
Daniel Willmann191e0d92018-01-17 12:44:35 +0100383const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100384 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
385 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100386}
387
Harald Weltec1a2fff2017-12-17 11:06:19 +0100388/* register an expect with the BSSMAP core */
Daniel Willmann191e0d92018-01-17 12:44:35 +0100389private function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100390 BSSMAPEM.call(BSSMAPEM_register:{l3_enc, self}) {
391 [] BSSMAPEM.getreply(BSSMAPEM_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100392 }
393}
394
Harald Welte651fcdc2018-05-10 20:23:16 +0200395type record TestHdlrEncrParams {
396 OCT1 enc_alg,
397 octetstring enc_key
398};
399
400template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg, octetstring key) := {
401 enc_alg := alg,
402 enc_key := key
403}
404
Harald Weltecc0b0142018-05-29 15:19:33 +0200405type record TestHdlrParamsLcls {
406 GlobalCallReferenceValue gcr optional,
407 /* LCLS Configuration */
408 BIT4 cfg optional,
409 /* LCLS Connection Status Control */
410 BIT4 csc optional,
411 BIT4 exp_sts optional
412}
413
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100414type record TestHdlrParamsHandover {
415 SCCP_PAR_Address sccp_addr_msc,
416 SCCP_PAR_Address sccp_addr_bsc
417}
418
Harald Weltec1a2fff2017-12-17 11:06:19 +0100419type record TestHdlrParams {
420 OCT1 ra,
421 GsmFrameNumber fn,
422 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100423 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200424 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200425 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200426 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200427 TestHdlrEncrParams encr optional,
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100428 TestHdlrParamsLcls lcls,
429 TestHdlrParamsHandover handover optional
Harald Weltec1a2fff2017-12-17 11:06:19 +0100430};
431
432template (value) TestHdlrParams t_def_TestHdlrPars := {
433 ra := '23'O,
434 fn := 23,
435 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100436 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200437 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200438 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200439 expect_mr_conf_ie := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200440 encr := omit,
441 lcls := {
442 gcr := omit,
443 cfg := omit,
444 csc := omit,
445 exp_sts := omit
Neels Hofmeyrbd0ef932018-03-19 14:58:46 +0100446 },
447 handover := omit
Harald Weltec1a2fff2017-12-17 11:06:19 +0100448}
449
Harald Weltea0630032018-03-20 21:09:55 +0100450function f_create_chan_and_exp() runs on MSC_ConnHdlr {
451 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100452 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100453 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
454
455 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Harald Weltea0630032018-03-20 21:09:55 +0100456 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn);
Daniel Willmann191e0d92018-01-17 12:44:35 +0100457 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100458}
459
Harald Welte898113b2018-01-31 18:32:21 +0100460function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
461 template (omit) RslChannelNr chan_nr := omit) runs on MSC_ConnHdlr {
462 if (not isvalue(link_id)) {
463 link_id := ts_RslLinkID_DCCH(0);
464 }
465 if (not isvalue(chan_nr)) {
466 chan_nr := g_chan_nr;
467 }
468 RSL.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
469}
470
Harald Weltec1a2fff2017-12-17 11:06:19 +0100471function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig) runs on MSC_ConnHdlr {
472 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100473 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100474 if (orig.msg_type == RSL_MT_ENCR_CMD) {
475 link_id := orig.ies[2].body.link_id;
476 } else {
477 link_id := orig.ies[1].body.link_id;
478 }
Harald Welte898113b2018-01-31 18:32:21 +0100479 f_rsl_send_l3(l3, link_id, chan_nr);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100480}
481
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100482/* Convert the chipher representation on BSSMAP to the representation used on RSL */
Harald Weltee613f962018-04-18 22:38:16 +0200483function f_chipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100484{
485 /* A5 0 */
486 if (alg_bssmap == '01'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200487 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100488 }
489 /* A5 1 */
490 else if (alg_bssmap == '02'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200491 return RSL_ALG_ID_A5_1;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100492 }
493 /* A5 2 */
494 else if (alg_bssmap == '04'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200495 return RSL_ALG_ID_A5_2;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100496 }
497 /* A5 3 */
498 else if (alg_bssmap == '08'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200499 return RSL_ALG_ID_A5_3;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100500 }
501 /* A5 4 */
502 else if (alg_bssmap == '10'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200503 return RSL_ALG_ID_A5_4;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100504 }
505 /* A5 5 */
506 else if (alg_bssmap == '20'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200507 return RSL_ALG_ID_A5_5;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100508 }
509 /* A5 6 */
510 else if (alg_bssmap == '40'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200511 return RSL_ALG_ID_A5_6;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100512 }
513 /* A5 7 */
514 else if (alg_bssmap == '80'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200515 return RSL_ALG_ID_A5_7;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100516 } else {
517 setverdict(fail, "Unexpected Encryption Algorithm");
Harald Weltee613f962018-04-18 22:38:16 +0200518 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100519 }
520}
521
Harald Welte38b2a102017-12-23 02:42:58 +0100522function f_cipher_mode(OCT1 alg, OCT8 key, template OCT16 kc128 := omit, boolean exp_fail := false)
523runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100524 var PDU_BSSAP bssap;
525 var RSL_Message rsl;
Harald Weltee613f962018-04-18 22:38:16 +0200526 var RSL_AlgId alg_rsl;
Harald Welte73cd2712017-12-17 00:44:52 +0100527
528 if (isvalue(kc128)) {
529 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(alg, key, valueof(kc128)));
530 } else {
531 BSSAP.send(ts_BSSMAP_CipherModeCmd(alg, key));
532 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100533
534 /* RSL uses a different representation of the encryption algorithm,
535 * so we need to convert first */
536 alg_rsl := f_chipher_mode_bssmap_to_rsl(alg);
537
Harald Welte73cd2712017-12-17 00:44:52 +0100538 alt {
539 /* RSL/UE Side */
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100540 [] RSL.receive(tr_RSL_ENCR_CMD(g_chan_nr, ?, alg_rsl, key)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100541 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
542 log("Rx L3 from net: ", l3);
543 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
544 f_rsl_reply(ts_RRM_CiphModeCompl, rsl);
545 }
546 repeat;
547 }
548 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
549 // bssap.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algoritmhIdentifier
Harald Welte38b2a102017-12-23 02:42:58 +0100550 if (exp_fail == true) {
551 setverdict(fail, "Unexpected Cipher Mode Complete");
552 } else {
553 setverdict(pass);
554 }
Harald Welte73cd2712017-12-17 00:44:52 +0100555 }
556 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100557 if (exp_fail == false) {
558 setverdict(fail, "Ciphering Mode Reject");
559 } else {
560 setverdict(pass);
561 }
Harald Welte73cd2712017-12-17 00:44:52 +0100562 }
563 }
564}
565
Harald Welte211219e2018-01-29 22:03:36 +0100566/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
567function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
568 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100569 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100570
571 if (match(inp, '00001'B)) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100572 chan_nr := valueof(t_RslChanNr_Bm(tn));
Harald Welte211219e2018-01-29 22:03:36 +0100573 }
574 else if (match(inp, '0001?'B)) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100575 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Harald Welte211219e2018-01-29 22:03:36 +0100576 }
577 else if (match(inp, '001??'B)) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100578 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Harald Welte211219e2018-01-29 22:03:36 +0100579 }
580 else if (match(inp, '01???'B)) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100581 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Harald Welte211219e2018-01-29 22:03:36 +0100582 }
583 else {
584 setverdict(fail, "Unknown ChDesc!");
Daniel Willmannafce8662018-07-06 23:11:32 +0200585 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100586 }
587
588 if (ch_desc.octet3 and4b '10'O == '10'O) {
589 setverdict(fail, "No support for Hopping");
Daniel Willmannafce8662018-07-06 23:11:32 +0200590 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100591 } else {
592 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
593 arfcn := oct2int(concat);
594 }
595}
596
597type record AssignmentState {
598 /* global */
599 boolean voice_call,
600 boolean is_assignment,
601 /* Assignment related bits */
602 boolean rr_ass_cmpl_seen,
Harald Welte21583082018-01-29 22:28:26 +0100603 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100604 RslChannelNr old_chan_nr,
605 /* Modify related bits */
606 boolean rr_modify_seen,
Harald Welte21583082018-01-29 22:28:26 +0100607 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100608}
609
610template (value) AssignmentState ts_AssignmentStateInit := {
611 voice_call := false,
612 is_assignment := false,
613 rr_ass_cmpl_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100614 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100615 old_chan_nr := -,
616 rr_modify_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100617 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100618}
619
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200620private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
621 encr_info := {
622 len := ?,
623 alg_id := alg,
624 key := key
625 }
626}
627
628/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
629private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
630 var RSL_IE_Body encr_info;
631 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg != '01'O) {
632 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
633 setverdict(fail, "Missing Encryption IE in CHAN ACT");
634 } else {
635 var RSL_AlgId alg := f_chipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg);
636 if (not match(encr_info, tr_EncrInfo(alg, g_pars.encr.enc_key))) {
637 setverdict(fail, "Wrong Encryption IE in CHAN ACT");
638 }
639 }
640 } else {
641 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
642 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
643 setverdict(fail, "Unexpected Encryption in CHAN ACT");
644 }
645 }
646 }
647 /* FIXME: validate RSL_IE_ACT_TYPE, RSL_IE_CHAN_MODE, RSL_IE_CHAN_IDENT, RSL_IE_BS_POWER,
648 * RSL_IE_MS_POWER, RSL_IE_TIMING_ADVANCE */
649}
650
Harald Welte211219e2018-01-29 22:03:36 +0100651altstep as_assignment(inout AssignmentState st) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100652 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100653 [not st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
654 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
655 log("Rx L3 from net: ", l3);
656 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
657 var RslChannelNr new_chan_nr;
658 var GsmArfcn arfcn;
659 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
660 new_chan_nr, arfcn);
661 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100662
Harald Welte211219e2018-01-29 22:03:36 +0100663 /* register our component for this channel number at the RSL Emulation */
664 f_rslem_register(0, new_chan_nr);
665 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
666 /* send assignment complete over the new channel */
Neels Hofmeyrb0e73652018-07-14 21:56:51 +0200667 RSL.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte211219e2018-01-29 22:03:36 +0100668 enc_PDU_ML3_MS_NW(l3_tx)));
669 /* by default, send via the new channel from now */
670 st.old_chan_nr := g_chan_nr;
671 g_chan_nr := new_chan_nr;
672 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200673 /* obtain channel activation from RSL_Emulation for new channel */
674 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
675 /* check it (e.g. for correct ciphering parameters) */
676 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +0100677 repeat;
678 } else {
679 setverdict(fail, "Unexpected L3 received", l3);
Daniel Willmannafce8662018-07-06 23:11:32 +0200680 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100681 }
Harald Welte211219e2018-01-29 22:03:36 +0100682 }
683 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
684 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
685 repeat;
686 }
687 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
688 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +0100689 /* unregister for old channel number in RSL emulation */
690 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
691 f_rslem_unregister(0, st.old_chan_nr);
Harald Welte21583082018-01-29 22:28:26 +0100692 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +0100693 repeat;
694 }
695}
696
697altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
698 /* no assignment, just mode modify */
699 var RSL_Message rsl;
700
701 [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 +0100702 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
703 log("Rx L3 from net: ", l3);
704 if (ischosen(l3.msgs.rrm.channelModeModify)) {
705 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
706 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +0100707 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100708 }
709 repeat;
710 }
Harald Welte211219e2018-01-29 22:03:36 +0100711 [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 +0100712 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +0100713 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100714 repeat;
715 }
Harald Welte211219e2018-01-29 22:03:36 +0100716}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100717
Harald Welte211219e2018-01-29 22:03:36 +0100718/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
719function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
720return boolean {
721 select (bssmap.speechOrDataIndicator) {
722 case ('0011'B) { /* Signalling */
723 /* all channels support signalling */
724 return true;
725 }
726 case else { /* Speech, Speech+CTM or CSD */
727 select (bssmap.channelRateAndType) {
728 case ('08'O) { /* TCH/F */
729 select (rsl_chan_nr) {
730 case (t_RslChanNr_Bm(?)) { return true; }
731 }
732 }
733 case ('09'O) { /* TCH/H */
734 select (rsl_chan_nr) {
735 case (t_RslChanNr_Lm(?, ?)) { return true; }
736 }
737 }
738 case else { /* full or half-rate */
739 select (rsl_chan_nr) {
740 case (t_RslChanNr_Bm(?)) { return true; }
741 case (t_RslChanNr_Lm(?, ?)) { return true; }
742 }
743 }
744 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100745 }
Harald Welte211219e2018-01-29 22:03:36 +0100746 }
747 return false;
748}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100749
Philipp Maier8ef527e2018-05-18 11:12:50 +0200750/* Determine if the channel mode specified within rsl_chan_nr requires a
751 * MODE MODIFY in to match the channel mode specified by given BSSMAP
752 * ChannelType */
753function f_channel_needs_modify(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
754return boolean {
755
756 /* FIXME: This tests the rsl_chan_nr to determine if we are on a
757 * signalling channel or not. Unfortunately this may lead to false
758 * results if we are on a TCH. The problem is that a TCH may be also
759 * used in signalling mode, but this function assumes that only SDCCH4
760 * and SDCCH8 are used as signalling channels at all. */
761
762 var boolean current_signalling := false;
763 var boolean desired_signalling := false;
764
765 select (rsl_chan_nr) {
766 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
767 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
768 }
769
770 if (bssmap.speechOrDataIndicator == '0011'B) {
771 desired_signalling := true;
772 }
773
774 if (current_signalling == desired_signalling) {
775 /* The desired channel mode is equal to the one we currently
776 * have, there is no mode modification needed or expected */
777 return false;
778 } else {
779 /* The desired channel mode and the current channel mode do
780 * not match. A mode modification is required */
781 return true;
782 }
783}
784
Harald Weltecc0b0142018-05-29 15:19:33 +0200785/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
786function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
787 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
788 if (istemplatekind(ass_tpl, "omit")) {
789 return;
790 }
791 if (ispresent(g_pars.lcls.gcr)) {
792 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
793 }
794 if (ispresent(g_pars.lcls.cfg)) {
795 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
796 }
797 if (ispresent(g_pars.lcls.csc)) {
798 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
799 }
Harald Welte343b7742018-06-05 23:41:37 +0200800 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
801 if (ispresent(g_pars.lcls.exp_sts)) {
802 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
803 } else {
804 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
805 }
Harald Weltecc0b0142018-05-29 15:19:33 +0200806 }
807}
808
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200809/* Helper function to check if the activity on the MGCP matches what we
810 * expected */
811function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
812 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +0200813 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
814 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
815 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
816
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200817 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
818 setverdict(fail, "unexpected number of MGW-CRCX transactions");
819 }
820 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
821 setverdict(fail, "unexpected number of MGW-MDCX transactions");
822 }
823 }
824}
825
Harald Welte211219e2018-01-29 22:03:36 +0100826/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +0200827function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
828runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +0200829
830 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200831 var boolean sccplite := false;
Philipp Maier48aeeec2018-09-14 18:20:04 +0200832 timer T := 10.0;
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200833
834 /* Check if we run on SCCPLITE instead of SCCP by looking if a CIC is
835 * present or not. */
836 if (isvalue(ass_tpl.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
837 sccplite := true;
838 }
Philipp Maier11a58942018-06-25 16:40:48 +0200839
840 if (isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
841 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
842 } else {
843 /* Make sure a meaningful default is assigned in case the
844 * codecList is not populated */
845 codecType := FR_AMR;
846 }
847
848 f_MscConnHdlr_init(g_pars.media_nr, "127.0.0.2", "127.0.0.3", codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +0200849
Harald Weltecc0b0142018-05-29 15:19:33 +0200850 /* patch in the LCLS related items, as needed */
851 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
852
Harald Welte7a14fd52018-05-10 22:26:55 +0200853 f_create_chan_and_exp();
854 /* we should now have a COMPL_L3 at the MSC */
Philipp Maier48aeeec2018-09-14 18:20:04 +0200855
856 var template PDU_BSSAP exp_l3_compl;
857 exp_l3_compl := tr_BSSMAP_ComplL3()
858 if (sccplite) {
859 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := omit;
860 } else {
861 exp_l3_compl.pdu.bssmap.completeLayer3Information.codecList := ?;
862 }
863 T.start;
864 alt {
865 [] BSSAP.receive(exp_l3_compl);
866 [] BSSAP.receive(tr_BSSMAP_ComplL3) {
867 setverdict(fail, "Received non-matching COMPLETE LAYER 3 INFORMATION");
868 all component.stop;
869 mtc.stop;
870 }
871 [] T.timeout {
872 setverdict(fail, "Timeout waiting for COMPLETE LAYER 3 INFORMATION");
873 all component.stop;
874 mtc.stop;
875 }
876 }
Harald Welte7a14fd52018-05-10 22:26:55 +0200877
878 /* start ciphering, if requested */
879 if (ispresent(g_pars.encr)) {
880 f_cipher_mode(g_pars.encr.enc_alg, g_pars.encr.enc_key);
881 }
882
883 /* bail out early if no assignment requested */
884 if (istemplatekind(ass_tpl, "omit")) {
885 return;
886 }
887
888 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +0100889 var PDU_BSSAP bssap;
Harald Welte211219e2018-01-29 22:03:36 +0100890 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +0100891 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +0200892 var boolean exp_modify;
Philipp Maier48aeeec2018-09-14 18:20:04 +0200893
Harald Welte211219e2018-01-29 22:03:36 +0100894 var ExpectCriteria mgcpcrit := {
895 connid := omit,
896 endpoint := omit,
897 transid := omit
898 };
899 var AssignmentState st := valueof(ts_AssignmentStateInit);
900 /* if the channel type is SIGNAL, we're not handling a voice call */
901 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
902 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +0200903 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +0100904 }
Philipp Maier8ef527e2018-05-18 11:12:50 +0200905
Harald Welte211219e2018-01-29 22:03:36 +0100906 /* determine if the current channel can support the given service or not */
907 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
908 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +0200909
910 /* We decided to assign a new channel, so we do not expect
911 * any mode modify messages on RSL */
912 exp_modify := false;
913 } else {
914 /* We will continue working with the currently assigned
915 * channel, we must now check if the mode of the current
916 * channel is compatible. If not we expect the BSC to modify
917 * the mode */
918 exp_modify := f_channel_needs_modify(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +0100919 }
920
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200921 /* Some test situations will involve MGCP transactions on a media
922 * gateway. Depending on the situation, we set up how many of each MGCP
923 * message type are expected to be exchanged. */
924 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
925 /* For tests that expect the assignment to fail, assume that
926 * there will be no MGW communication as well. */
927 g_media.mgcp_conn[0].crcx_seen_exp := 0;
928 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
929 g_media.mgcp_conn[1].crcx_seen_exp := 0;
930 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
931 } else if (st.voice_call) {
932 /* For voice calls we expect the following MGCP activity */
933 if (sccplite) {
934 g_media.mgcp_conn[0].crcx_seen_exp := 1;
935 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
936 g_media.mgcp_conn[1].crcx_seen_exp := 0;
937 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
938 } else {
939 g_media.mgcp_conn[0].crcx_seen_exp := 1;
940 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
941 g_media.mgcp_conn[1].crcx_seen_exp := 1;
942 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
943 }
944 }
945
Harald Welte211219e2018-01-29 22:03:36 +0100946 f_create_mgcp_expect(mgcpcrit);
947 BSSAP.send(ass_cmd);
948
949 T.start;
950 alt {
951 /* assignment related bits */
952 [st.is_assignment] as_assignment(st);
953
954 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +0200955 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +0100956
957 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
958 [st.voice_call] as_Media();
959
960 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +0200961 [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 +0100962 setverdict(pass);
963 }
Philipp Maier86f39202018-02-07 14:40:09 +0100964 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
965 setverdict(pass);
966 }
Harald Welte21583082018-01-29 22:28:26 +0100967 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +0200968 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +0100969 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100970 setverdict(fail, "Received non-matching ASSIGNMENT COMPLETE");
Philipp Maier8aa5cb32018-09-12 17:09:37 +0200971 all component.stop;
972 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100973 }
974 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
975 setverdict(fail, "Received unexpected ASSIGNMENT FAIL");
Philipp Maier8aa5cb32018-09-12 17:09:37 +0200976 all component.stop;
977 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100978 }
979 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
980 setverdict(fail, "Received unexpected ASSIGNMENT COMPLETE");
Philipp Maier8aa5cb32018-09-12 17:09:37 +0200981 all component.stop;
982 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100983 }
984 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
985 setverdict(fail, "Received non-matching ASSIGNMENT FAIL");
Philipp Maier8aa5cb32018-09-12 17:09:37 +0200986 all component.stop;
987 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100988 }
989 [] T.timeout {
Harald Welte458fd372018-03-21 11:26:23 +0100990 setverdict(fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Philipp Maier8aa5cb32018-09-12 17:09:37 +0200991 all component.stop;
992 mtc.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100993 }
994 }
Harald Welte211219e2018-01-29 22:03:36 +0100995 log("g_media ", g_media);
996 if (not isbound(bssap)) {
Daniel Willmannafce8662018-07-06 23:11:32 +0200997 mtc.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100998 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200999
Philipp Maiera0976e92018-07-16 15:19:42 +02001000 /* When the BSC detects that LCLS is possible it will cross the
1001 * connetions that point to the PBX side of the MGW. In our case this
1002 * is mgcp_conn[1]. The BSC performs this operation already before the
1003 * assignment complete is generated. This means we expect another MDCX
1004 * at mgcp_conn[1] when LCLS is expected. */
1005 if (ispresent(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue)) {
1006 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
1007 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
1008
1009 }
1010 }
1011
Philipp Maier3e2af5d2018-07-11 17:01:05 +02001012 f_check_mgcp_expectations();
Harald Welte930d0a72018-03-22 22:08:40 +01001013}
1014
Harald Welte261af4b2018-02-12 21:20:39 +01001015type record HandoverState {
1016 /* Assignment related bits */
1017 boolean rr_ho_cmpl_seen,
Philipp Maierc1e95c82018-07-18 16:03:00 +02001018 integer mdcx_seen_before_ho,
Harald Welte261af4b2018-02-12 21:20:39 +01001019 boolean handover_done,
1020 RslChannelNr old_chan_nr
1021};
1022
1023altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
1024 var RSL_Message rsl;
1025 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
1026 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
1027 log("Rx L3 from net: ", l3);
1028 if (ischosen(l3.msgs.rrm.handoverCommand)) {
1029 var RslChannelNr new_chan_nr;
1030 var GsmArfcn arfcn;
1031 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
1032 new_chan_nr, arfcn);
1033 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1034
1035 /* register our component for this channel number at the RSL Emulation */
1036 f_rslem_register(0, new_chan_nr, RSL1_PROC);
1037
1038 /* resume processing of RSL DChan messages, which was temporarily suspended
1039 * before performing a hand-over */
1040 f_rslem_resume(RSL1_PROC);
1041
Neels Hofmeyr378a49c2018-06-15 22:18:43 +02001042 /* send handover detect */
1043 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
1044 f_sleep(0.3);
1045
Harald Welte261af4b2018-02-12 21:20:39 +01001046 /* send handover complete over the new channel */
1047 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
Neels Hofmeyrd07ee132018-07-14 22:28:29 +02001048 RSL1.send(ts_RSL_EST_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
Harald Welte261af4b2018-02-12 21:20:39 +01001049 enc_PDU_ML3_MS_NW(l3_tx)));
1050 /* by default, send via the new channel from now */
1051 st.old_chan_nr := g_chan_nr;
1052 g_chan_nr := new_chan_nr;
1053 st.rr_ho_cmpl_seen := true;
Philipp Maierc1e95c82018-07-18 16:03:00 +02001054
1055 /* Memorize how many MDCX we have seen before. We need this number to detect
1056 * that we have received the handover related MDCX */
1057 st.mdcx_seen_before_ho := g_media.mgcp_conn[0].mdcx_seen;
Harald Welte261af4b2018-02-12 21:20:39 +01001058 repeat;
1059 } else {
1060 setverdict(fail, "Unexpected L3 received", l3);
Daniel Willmannafce8662018-07-06 23:11:32 +02001061 mtc.stop;
Harald Welte261af4b2018-02-12 21:20:39 +01001062 }
1063 }
Philipp Maierc1e95c82018-07-18 16:03:00 +02001064 [st.rr_ho_cmpl_seen] as_Media_ipacc();
1065 [st.rr_ho_cmpl_seen] as_Media_mgw(true);
Harald Welte261af4b2018-02-12 21:20:39 +01001066 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1067 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1068 repeat;
1069 }
1070 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1071 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1072 /* unregister for old channel number in RSL emulation */
1073 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1074 f_rslem_unregister(0, st.old_chan_nr);
Philipp Maierc1e95c82018-07-18 16:03:00 +02001075
1076 /* The channel release must not necessarly be synchronized to the RSL handover
1077 * procedure since those events may happen independently nearly at the same
1078 * time. When we receive the RSL_RF_CHAN_REL command the media negotiation on
1079 * IPACC or MGCP level may be still in progress. In order to make sure that
1080 * we do only stop when we have seen an MDCX on MGCP level and another a CRCX
1081 * as well as an MDCX on IPACC level. */
1082 if (g_media.mgcp_conn[0].mdcx_seen <= st.mdcx_seen_before_ho or
1083 g_media.bts1.ipa_mdcx_seen == false or g_media.bts1.ipa_crcx_seen == false) {
1084 repeat;
1085 } else {
1086 st.handover_done := true;
1087 }
Harald Welte261af4b2018-02-12 21:20:39 +01001088 }
1089}
1090
1091
Harald Weltec1a2fff2017-12-17 11:06:19 +01001092
Harald Welte28d943e2017-11-25 15:00:50 +01001093}