blob: 63d017fb7fdcd21129a8f90d8d4492f64ef02580 [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,
77 uint16_t conn_id,
78 uint7_t rtp_pt,
79 HostPort bts,
80 HostPort peer
81};
82
83type record MediaState {
84 MgcpEndpoint mgcp_ep,
85 MgcpConnState mgcp_conn[2],
Harald Welte261af4b2018-02-12 21:20:39 +010086 BtsMediaState bts,
87 BtsMediaState bts1 /* only during hand-over */
Harald Welte211219e2018-01-29 22:03:36 +010088};
89
Philipp Maier11a58942018-06-25 16:40:48 +020090function f_MediaState_init(inout MediaState g_media, integer nr, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) {
Harald Welte211219e2018-01-29 22:03:36 +010091 /* BTS Side */
92 g_media.bts := {
93 ipa_crcx_seen := false,
94 conn_id := nr,
95 rtp_pt := 0,
96 bts := {
97 host := bts,
98 port_nr := 9000 + nr*2
99 },
100 peer := -
101 }
102
Harald Welte261af4b2018-02-12 21:20:39 +0100103 g_media.bts1 := {
104 ipa_crcx_seen := false,
105 conn_id := nr,
106 rtp_pt := 0,
107 bts := {
108 host := bts, /* FIXME */
109 port_nr := 9000 + nr*2
110 },
111 peer := -
112 }
113
Harald Welte363cb0a2018-01-30 19:35:53 +0100114 g_media.mgcp_ep := "rtpbridge/" & int2str(nr) & "@mgw";
Harald Welte211219e2018-01-29 22:03:36 +0100115
116 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier11a58942018-06-25 16:40:48 +0200117 g_media.mgcp_conn[i].mime_type := f_encoding_name_from_pt(f_get_mgcp_pt(codecType));
Harald Welte211219e2018-01-29 22:03:36 +0100118 g_media.mgcp_conn[i].sample_rate := 8000;
119 g_media.mgcp_conn[i].ptime := 20;
Philipp Maier11a58942018-06-25 16:40:48 +0200120 g_media.mgcp_conn[i].rtp_pt := enum2int(f_get_mgcp_pt(codecType));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200121 g_media.mgcp_conn[i].crcx_seen := 0;
122 g_media.mgcp_conn[i].mdcx_seen := 0;
123 g_media.mgcp_conn[i].crcx_seen_exp := 0;
124 g_media.mgcp_conn[i].mdcx_seen_exp := 0;
Harald Welte211219e2018-01-29 22:03:36 +0100125 g_media.mgcp_conn[i].conn_id := f_mgcp_alloc_conn_id();
126 }
127
128 g_media.mgcp_conn[0].mgw := {
129 host := mgw,
130 port_nr := 10000 + nr*2
131 }
132 g_media.mgcp_conn[1].mgw := {
133 host := mgw,
134 port_nr := 11000 + nr*2
135 }
136}
137
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200138/* Helper function to get the next free MGCP connection identifier. We can
139 * recognize free connection identifiers by the fact that no CRCX happend yet */
Harald Welte211219e2018-01-29 22:03:36 +0100140private function f_get_free_mgcp_conn() runs on MSC_ConnHdlr return integer {
141 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200142 if (not g_media.mgcp_conn[i].crcx_seen >= 1) {
Harald Welte211219e2018-01-29 22:03:36 +0100143 return i;
144 }
145 }
146 setverdict(fail, "Only 2 Connections per EP!");
147 self.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100148}
149
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200150/* Helper function to pick a specific connection by its cid. Since we reach out
151 * for a connection that is in-use we also check if there was already exactly
152 * one CRCX happening on that connection. */
Harald Welte211219e2018-01-29 22:03:36 +0100153private function f_get_mgcp_conn(MgcpConnectionId cid) runs on MSC_ConnHdlr return integer {
154 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200155 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 +0100156 return i;
157 }
158 }
159 setverdict(fail, "No Connection for ID ", cid);
160 self.stop;
Harald Welte211219e2018-01-29 22:03:36 +0100161}
162
Philipp Maierb2422352018-07-11 10:36:57 +0200163/* altstep for handling of IPACC media related commands. Activated by as_Media() to test
164 * RSL level media handling */
165altstep as_Media_ipacc() runs on MSC_ConnHdlr {
Harald Welte211219e2018-01-29 22:03:36 +0100166 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100167 var RSL_IE_Body ie;
Harald Welte930d0a72018-03-22 22:08:40 +0100168 var boolean b_unused;
Harald Welte211219e2018-01-29 22:03:36 +0100169 [not g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
170 /* Extract parameters from request + use in response */
171 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
172 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
173 }
174 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
175 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
176 }
177 RSL.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts.conn_id,
178 oct2int(f_inet_addr(g_media.bts.bts.host)),
179 g_media.bts.bts.port_nr,
180 g_media.bts.rtp_pt));
181 g_media.bts.ipa_crcx_seen := true;
182 repeat;
183 }
184 [g_media.bts.ipa_crcx_seen] RSL.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
185 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100186 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100187 if (g_media.bts.conn_id != ie.ipa_conn_id) {
188 setverdict(fail, "IPA MDCX for unknown ConnId", rsl);
189 self.stop;
190 }
191 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100192 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100193 g_media.bts.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100194 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte211219e2018-01-29 22:03:36 +0100195 g_media.bts.peer.port_nr := ie.ipa_remote_port;
196 /* optional */
197 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
198 g_media.bts.rtp_pt := ie.ipa_rtp_pt;
199 }
200 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
201 g_media.bts.rtp_pt := ie.ipa_rtp_pt2;
202 }
203 RSL.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts.conn_id,
204 oct2int(f_inet_addr(g_media.bts.peer.host)),
205 g_media.bts.peer.port_nr,
206 g_media.bts.rtp_pt));
Harald Welte261af4b2018-02-12 21:20:39 +0100207 //g_media.bts.ipa_mdcx_seen := true;
Harald Welte211219e2018-01-29 22:03:36 +0100208 repeat;
209 }
Harald Welte261af4b2018-02-12 21:20:39 +0100210
211 /* on second (new) BTS during hand-over */
212 [not g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_CRCX(g_chan_nr)) -> value rsl {
213 /* Extract parameters from request + use in response */
214 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
215 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
216 }
217 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
218 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
219 }
220 RSL1.send(ts_RSL_IPA_CRCX_ACK(g_chan_nr, g_media.bts1.conn_id,
221 oct2int(f_inet_addr(g_media.bts1.bts.host)),
222 g_media.bts1.bts.port_nr,
223 g_media.bts1.rtp_pt));
224 g_media.bts1.ipa_crcx_seen := true;
225 repeat;
226 }
227 /* on second (new) BTS during hand-over */
228 [g_media.bts1.ipa_crcx_seen] RSL1.receive(tr_RSL_IPA_MDCX(g_chan_nr, ?)) -> value rsl{
229 /* Extract conn_id, ip, port, rtp_pt2 from request + use in response */
Harald Welte930d0a72018-03-22 22:08:40 +0100230 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_CONN_ID, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100231 if (g_media.bts1.conn_id != ie.ipa_conn_id) {
232 setverdict(fail, "IPA MDCX for unknown ConnId", rsl);
233 self.stop;
234 }
235 /* mandatory */
Harald Welte930d0a72018-03-22 22:08:40 +0100236 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_IP, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100237 g_media.bts1.peer.host := f_inet_ntoa(int2oct(ie.ipa_remote_ip, 4));
Harald Welte930d0a72018-03-22 22:08:40 +0100238 b_unused := f_rsl_find_ie(rsl, RSL_IE_IPAC_REMOTE_PORT, ie);
Harald Welte261af4b2018-02-12 21:20:39 +0100239 g_media.bts1.peer.port_nr := ie.ipa_remote_port;
240 /* optional */
241 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD, ie)) {
242 g_media.bts1.rtp_pt := ie.ipa_rtp_pt;
243 }
244 if (f_rsl_find_ie(rsl, RSL_IE_IPAC_RTP_PAYLOAD2, ie)) {
245 g_media.bts1.rtp_pt := ie.ipa_rtp_pt2;
246 }
247 RSL1.send(ts_RSL_IPA_MDCX_ACK(g_chan_nr, g_media.bts1.conn_id,
248 oct2int(f_inet_addr(g_media.bts1.peer.host)),
249 g_media.bts1.peer.port_nr,
250 g_media.bts1.rtp_pt));
251 //g_media.bts1.ipa_mdcx_seen := true;
252 repeat;
253 }
254
Philipp Maierb2422352018-07-11 10:36:57 +0200255
256}
257
258/* altstep for handling of MGCP media related commands. Activated by as_Media() to test
259 * MGW level media handling */
260altstep as_Media_mgw() runs on MSC_ConnHdlr {
261 var MgcpCommand mgcp_cmd;
262
Harald Welte211219e2018-01-29 22:03:36 +0100263 [] MGCP.receive(tr_CRCX) -> value mgcp_cmd {
264 var SDP_Message sdp;
265 var integer cid := f_get_free_mgcp_conn();
Harald Welte363cb0a2018-01-30 19:35:53 +0100266 if (match(mgcp_cmd.line.ep, t_MGCP_EP_wildcard)) {
267 if (cid != 0) {
268 setverdict(fail, "MGCP wildcard EP only works in first CRCX");
269 self.stop;
270 }
271 /* we keep the endpoint name allocated during MediaState_init */
272 } else {
273 /* Call Agent allocated endpoint, trust/use it always */
274 g_media.mgcp_ep := mgcp_cmd.line.ep;
275 }
Harald Welte211219e2018-01-29 22:03:36 +0100276 if (isvalue(mgcp_cmd.sdp)) {
277 sdp := mgcp_cmd.sdp;
278 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
279 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
280 }
281 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
282 sdp := valueof(ts_SDP(mgcp_conn.mgw.host, mgcp_conn.mgw.host, "foo", "21",
283 mgcp_conn.mgw.port_nr, { int2str(mgcp_conn.rtp_pt) },
284 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
285 mgcp_conn.mime_type & "/" &
286 int2str(mgcp_conn.sample_rate))),
287 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
Harald Welte363cb0a2018-01-30 19:35:53 +0100288 var template MgcpResponse mgcp_resp;
289 mgcp_resp := ts_CRCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp);
290 f_mgcp_par_append(mgcp_resp.params, ts_MgcpParSpecEP(g_media.mgcp_ep));
291 MGCP.send(mgcp_resp);
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200292 g_media.mgcp_conn[cid].crcx_seen := g_media.mgcp_conn[cid].crcx_seen + 1;
Harald Welte211219e2018-01-29 22:03:36 +0100293 repeat;
294 }
295 [] MGCP.receive(tr_MDCX) -> value mgcp_cmd {
296 var SDP_Message sdp;
297 var integer cid := f_get_mgcp_conn(f_MgcpCmd_extract_conn_id(mgcp_cmd));
298 if (isvalue(mgcp_cmd.sdp)) {
299 sdp := mgcp_cmd.sdp;
300 g_media.mgcp_conn[cid].peer.host := sdp.connection.conn_addr.addr;
301 g_media.mgcp_conn[cid].peer.port_nr := sdp.media_list[0].media_field.ports.port_number;
302 } else {
303 setverdict(fail, "MDCX has no [recognizable] SDP");
Harald Welte211219e2018-01-29 22:03:36 +0100304 }
305 var MgcpConnState mgcp_conn := g_media.mgcp_conn[cid];
306 sdp := valueof(ts_SDP(mgcp_conn.peer.host, mgcp_conn.peer.host, "foo", "21",
307 mgcp_conn.peer.port_nr, { int2str(mgcp_conn.rtp_pt) },
308 {valueof(ts_SDP_rtpmap(mgcp_conn.rtp_pt,
309 mgcp_conn.mime_type & "/" &
310 int2str(mgcp_conn.sample_rate))),
311 valueof(ts_SDP_ptime(mgcp_conn.ptime)) } ));
312 MGCP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, mgcp_conn.conn_id, sdp));
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200313 g_media.mgcp_conn[cid].mdcx_seen := g_media.mgcp_conn[cid].mdcx_seen + 1;
Harald Welte211219e2018-01-29 22:03:36 +0100314 repeat;
315 }
316}
317
Philipp Maierb2422352018-07-11 10:36:57 +0200318/* Altsteps for handling of media related commands. Can be activated by a given
319 * test case if it expects to see media related handling (i.e. voice calls) */
320altstep as_Media() runs on MSC_ConnHdlr {
321 [] as_Media_ipacc();
322 [] as_Media_mgw();
323}
Harald Welte211219e2018-01-29 22:03:36 +0100324
Harald Welte28d943e2017-11-25 15:00:50 +0100325/* this component represents a single subscriber connection at the MSC.
326 * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components.
327 * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */
Daniel Willmann191e0d92018-01-17 12:44:35 +0100328type component MSC_ConnHdlr extends BSSAP_ConnHdlr, RSL_DchanHdlr, MGCP_ConnHdlr {
Harald Welte28d943e2017-11-25 15:00:50 +0100329 /* SCCP Connecction Identifier for the underlying SCCP connection */
330 var integer g_sccp_conn_id;
331
Harald Weltec1a2fff2017-12-17 11:06:19 +0100332 /* procedure port back to our parent (BSSMAP_Emulation_CT) for control */
333 port BSSMAPEM_PROC_PT BSSMAPEM;
Harald Weltec20b1c42018-02-12 20:50:08 +0100334 port TELNETasp_PT BSCVTY;
Harald Welte28d943e2017-11-25 15:00:50 +0100335
Harald Welte211219e2018-01-29 22:03:36 +0100336 var MediaState g_media;
Harald Weltea0630032018-03-20 21:09:55 +0100337 var TestHdlrParams g_pars;
Harald Weltee97eab42018-03-21 18:46:06 +0100338
339 var boolean g_vty_initialized := false;
Harald Welte211219e2018-01-29 22:03:36 +0100340}
341
342/* initialize all parameters */
Philipp Maier11a58942018-06-25 16:40:48 +0200343function f_MscConnHdlr_init(integer i, HostName bts, HostName mgw, BSSMAP_FIELD_CodecType codecType) runs on MSC_ConnHdlr {
344 f_MediaState_init(g_media, i, bts, mgw, codecType);
Harald Weltee97eab42018-03-21 18:46:06 +0100345 if (not g_vty_initialized) {
346 map(self:BSCVTY, system:BSCVTY);
347 f_vty_set_prompts(BSCVTY);
348 f_vty_transceive(BSCVTY, "enable");
349 g_vty_initialized := true;
350 }
Harald Welte28d943e2017-11-25 15:00:50 +0100351}
352
353/* Callback function from general BSSMAP_Emulation whenever a connectionless
354 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
355private function UnitdataCallback(PDU_BSSAP bssap)
356runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
357 var template PDU_BSSAP resp := omit;
358
Harald Weltec1a2fff2017-12-17 11:06:19 +0100359 /* answer all RESET with a RESET ACK */
Harald Welte28d943e2017-11-25 15:00:50 +0100360 if (match(bssap, tr_BSSMAP_Reset)) {
361 resp := ts_BSSMAP_ResetAck;
362 }
363
364 return resp;
365}
366
367const BssmapOps MSC_BssmapOps := {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100368 create_cb := refers(BSSMAP_Emulation.ExpectedCreateCallback),
Harald Welte0b476062018-01-21 19:07:32 +0100369 unitdata_cb := refers(UnitdataCallback),
370 decode_dtap := false,
Harald Welte930d0a72018-03-22 22:08:40 +0100371 role_ms := false,
372 sccp_addr_local := omit,
373 sccp_addr_peer := omit
Harald Welte28d943e2017-11-25 15:00:50 +0100374}
375
Daniel Willmann191e0d92018-01-17 12:44:35 +0100376const MGCPOps MSC_MGCPOps := {
Harald Welte930d0a72018-03-22 22:08:40 +0100377 create_cb := refers(MGCP_Emulation.ExpectedCreateCallback),
378 unitdata_cb := refers(MGCP_Emulation.DummyUnitdataCallback)
Daniel Willmann191e0d92018-01-17 12:44:35 +0100379}
380
Harald Weltec1a2fff2017-12-17 11:06:19 +0100381/* register an expect with the BSSMAP core */
Daniel Willmann191e0d92018-01-17 12:44:35 +0100382private function f_create_bssmap_exp(octetstring l3_enc) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100383 BSSMAPEM.call(BSSMAPEM_register:{l3_enc, self}) {
384 [] BSSMAPEM.getreply(BSSMAPEM_register:{?, ?}) {};
Harald Welte28d943e2017-11-25 15:00:50 +0100385 }
386}
387
Harald Welte651fcdc2018-05-10 20:23:16 +0200388type record TestHdlrEncrParams {
389 OCT1 enc_alg,
390 octetstring enc_key
391};
392
393template (value) TestHdlrEncrParams t_EncrParams(OCT1 alg, octetstring key) := {
394 enc_alg := alg,
395 enc_key := key
396}
397
Harald Weltecc0b0142018-05-29 15:19:33 +0200398type record TestHdlrParamsLcls {
399 GlobalCallReferenceValue gcr optional,
400 /* LCLS Configuration */
401 BIT4 cfg optional,
402 /* LCLS Connection Status Control */
403 BIT4 csc optional,
404 BIT4 exp_sts optional
405}
406
Harald Weltec1a2fff2017-12-17 11:06:19 +0100407type record TestHdlrParams {
408 OCT1 ra,
409 GsmFrameNumber fn,
410 hexstring imsi,
Harald Welte60aa5762018-03-21 19:33:13 +0100411 RslLinkId link_id,
Harald Weltecbe911c2018-06-01 18:23:07 +0200412 integer media_nr, /* determins MGCP EP, port numbers */
Harald Welte651fcdc2018-05-10 20:23:16 +0200413 BSSMAP_IE_SpeechCodecList ass_codec_list optional,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200414 RSL_IE_Body expect_mr_conf_ie optional, /* typically present for AMR codecs */
Harald Weltecc0b0142018-05-29 15:19:33 +0200415 TestHdlrEncrParams encr optional,
416 TestHdlrParamsLcls lcls
Harald Weltec1a2fff2017-12-17 11:06:19 +0100417};
418
419template (value) TestHdlrParams t_def_TestHdlrPars := {
420 ra := '23'O,
421 fn := 23,
422 imsi := '001019876543210'H,
Harald Welte60aa5762018-03-21 19:33:13 +0100423 link_id := valueof(ts_RslLinkID_DCCH(0)),
Harald Weltecbe911c2018-06-01 18:23:07 +0200424 media_nr := 1,
Harald Welte651fcdc2018-05-10 20:23:16 +0200425 ass_codec_list := omit,
Neels Hofmeyrbcf62bc2018-07-04 00:24:33 +0200426 expect_mr_conf_ie := omit,
Harald Weltecc0b0142018-05-29 15:19:33 +0200427 encr := omit,
428 lcls := {
429 gcr := omit,
430 cfg := omit,
431 csc := omit,
432 exp_sts := omit
433 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100434}
435
Harald Weltea0630032018-03-20 21:09:55 +0100436function f_create_chan_and_exp() runs on MSC_ConnHdlr {
437 var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(g_pars.imsi));
Harald Welte6ed6bf92018-01-24 21:09:15 +0100438 var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi));
Harald Weltec1a2fff2017-12-17 11:06:19 +0100439 var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3_info);
440
441 /* call helper function for CHAN_RQD -> IMM ASS ->EST_IND */
Harald Weltea0630032018-03-20 21:09:55 +0100442 RSL_Emulation.f_chan_est(g_pars.ra, l3_enc, g_pars.link_id, g_pars.fn);
Daniel Willmann191e0d92018-01-17 12:44:35 +0100443 f_create_bssmap_exp(l3_enc);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100444}
445
Harald Welte898113b2018-01-31 18:32:21 +0100446function f_rsl_send_l3(template PDU_ML3_MS_NW l3, template (omit) RslLinkId link_id := omit,
447 template (omit) RslChannelNr chan_nr := omit) runs on MSC_ConnHdlr {
448 if (not isvalue(link_id)) {
449 link_id := ts_RslLinkID_DCCH(0);
450 }
451 if (not isvalue(chan_nr)) {
452 chan_nr := g_chan_nr;
453 }
454 RSL.send(ts_RSL_DATA_IND(valueof(chan_nr), valueof(link_id), enc_PDU_ML3_MS_NW(valueof(l3))));
455}
456
Harald Weltec1a2fff2017-12-17 11:06:19 +0100457function f_rsl_reply(template PDU_ML3_MS_NW l3, RSL_Message orig) runs on MSC_ConnHdlr {
458 var RslChannelNr chan_nr := orig.ies[0].body.chan_nr;
Harald Welte1a40de62017-12-23 02:28:34 +0100459 var RslLinkId link_id;
Harald Welte8d5eead2017-12-17 18:56:45 +0100460 if (orig.msg_type == RSL_MT_ENCR_CMD) {
461 link_id := orig.ies[2].body.link_id;
462 } else {
463 link_id := orig.ies[1].body.link_id;
464 }
Harald Welte898113b2018-01-31 18:32:21 +0100465 f_rsl_send_l3(l3, link_id, chan_nr);
Harald Weltec1a2fff2017-12-17 11:06:19 +0100466}
467
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100468/* Convert the chipher representation on BSSMAP to the representation used on RSL */
Harald Weltee613f962018-04-18 22:38:16 +0200469function f_chipher_mode_bssmap_to_rsl(OCT1 alg_bssmap) return RSL_AlgId
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100470{
471 /* A5 0 */
472 if (alg_bssmap == '01'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200473 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100474 }
475 /* A5 1 */
476 else if (alg_bssmap == '02'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200477 return RSL_ALG_ID_A5_1;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100478 }
479 /* A5 2 */
480 else if (alg_bssmap == '04'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200481 return RSL_ALG_ID_A5_2;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100482 }
483 /* A5 3 */
484 else if (alg_bssmap == '08'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200485 return RSL_ALG_ID_A5_3;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100486 }
487 /* A5 4 */
488 else if (alg_bssmap == '10'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200489 return RSL_ALG_ID_A5_4;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100490 }
491 /* A5 5 */
492 else if (alg_bssmap == '20'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200493 return RSL_ALG_ID_A5_5;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100494 }
495 /* A5 6 */
496 else if (alg_bssmap == '40'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200497 return RSL_ALG_ID_A5_6;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100498 }
499 /* A5 7 */
500 else if (alg_bssmap == '80'O) {
Harald Weltee613f962018-04-18 22:38:16 +0200501 return RSL_ALG_ID_A5_7;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100502 } else {
503 setverdict(fail, "Unexpected Encryption Algorithm");
Harald Weltee613f962018-04-18 22:38:16 +0200504 return RSL_ALG_ID_A5_0;
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100505 }
506}
507
Harald Welte38b2a102017-12-23 02:42:58 +0100508function f_cipher_mode(OCT1 alg, OCT8 key, template OCT16 kc128 := omit, boolean exp_fail := false)
509runs on MSC_ConnHdlr {
Harald Welte73cd2712017-12-17 00:44:52 +0100510 var PDU_BSSAP bssap;
511 var RSL_Message rsl;
Harald Weltee613f962018-04-18 22:38:16 +0200512 var RSL_AlgId alg_rsl;
Harald Welte73cd2712017-12-17 00:44:52 +0100513
514 if (isvalue(kc128)) {
515 BSSAP.send(ts_BSSMAP_CipherModeCmdKc128(alg, key, valueof(kc128)));
516 } else {
517 BSSAP.send(ts_BSSMAP_CipherModeCmd(alg, key));
518 }
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100519
520 /* RSL uses a different representation of the encryption algorithm,
521 * so we need to convert first */
522 alg_rsl := f_chipher_mode_bssmap_to_rsl(alg);
523
Harald Welte73cd2712017-12-17 00:44:52 +0100524 alt {
525 /* RSL/UE Side */
Philipp Maierb20c3dc2018-02-07 18:36:25 +0100526 [] RSL.receive(tr_RSL_ENCR_CMD(g_chan_nr, ?, alg_rsl, key)) -> value rsl {
Harald Welte73cd2712017-12-17 00:44:52 +0100527 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[3].body.l3_info.payload);
528 log("Rx L3 from net: ", l3);
529 if (ischosen(l3.msgs.rrm.cipheringModeCommand)) {
530 f_rsl_reply(ts_RRM_CiphModeCompl, rsl);
531 }
532 repeat;
533 }
534 [] BSSAP.receive(tr_BSSMAP_CipherModeCompl) -> value bssap {
535 // bssap.bssmap.cipherModeComplete.chosenEncryptionAlgorithm.algoritmhIdentifier
Harald Welte38b2a102017-12-23 02:42:58 +0100536 if (exp_fail == true) {
537 setverdict(fail, "Unexpected Cipher Mode Complete");
538 } else {
539 setverdict(pass);
540 }
Harald Welte73cd2712017-12-17 00:44:52 +0100541 }
542 [] BSSAP.receive(tr_BSSMAP_CipherModeRej) -> value bssap {
Harald Welte38b2a102017-12-23 02:42:58 +0100543 if (exp_fail == false) {
544 setverdict(fail, "Ciphering Mode Reject");
545 } else {
546 setverdict(pass);
547 }
Harald Welte73cd2712017-12-17 00:44:52 +0100548 }
549 }
550}
551
Harald Welte211219e2018-01-29 22:03:36 +0100552/* Convert from Ericsson ChanDesc2 format to Osmocom RslChannelNr format */
553function f_ChDesc2RslChanNr(ChannelDescription2_V ch_desc, out RslChannelNr chan_nr, out GsmArfcn arfcn) {
554 var BIT5 inp := ch_desc.channelTypeandTDMAOffset;
Harald Welte6fa1f732018-03-21 22:46:16 +0100555 var uint3_t tn := bit2int(ch_desc.timeslotNumber);
Harald Welte211219e2018-01-29 22:03:36 +0100556
557 if (match(inp, '00001'B)) { /* TCH/F */
Harald Welte6fa1f732018-03-21 22:46:16 +0100558 chan_nr := valueof(t_RslChanNr_Bm(tn));
Harald Welte211219e2018-01-29 22:03:36 +0100559 }
560 else if (match(inp, '0001?'B)) { /* TCH/H */
Harald Welte6fa1f732018-03-21 22:46:16 +0100561 chan_nr := valueof(t_RslChanNr_Lm(tn, bit2int(substr(inp, 4, 1))));
Harald Welte211219e2018-01-29 22:03:36 +0100562 }
563 else if (match(inp, '001??'B)) { /* SDCCH/4 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100564 chan_nr := valueof(t_RslChanNr_SDCCH4(tn, bit2int(substr(inp, 3, 2))));
Harald Welte211219e2018-01-29 22:03:36 +0100565 }
566 else if (match(inp, '01???'B)) { /* SDCCH/8 */
Harald Welte6fa1f732018-03-21 22:46:16 +0100567 chan_nr := valueof(t_RslChanNr_SDCCH8(tn, bit2int(substr(inp, 2, 3))));
Harald Welte211219e2018-01-29 22:03:36 +0100568 }
569 else {
570 setverdict(fail, "Unknown ChDesc!");
571 self.stop;
572 }
573
574 if (ch_desc.octet3 and4b '10'O == '10'O) {
575 setverdict(fail, "No support for Hopping");
576 self.stop;
577 } else {
578 var OCT2 concat := ch_desc.octet3 & ch_desc.octet4;
579 arfcn := oct2int(concat);
580 }
581}
582
583type record AssignmentState {
584 /* global */
585 boolean voice_call,
586 boolean is_assignment,
587 /* Assignment related bits */
588 boolean rr_ass_cmpl_seen,
Harald Welte21583082018-01-29 22:28:26 +0100589 boolean assignment_done,
Harald Welte211219e2018-01-29 22:03:36 +0100590 RslChannelNr old_chan_nr,
591 /* Modify related bits */
592 boolean rr_modify_seen,
Harald Welte21583082018-01-29 22:28:26 +0100593 boolean modify_done
Harald Welte211219e2018-01-29 22:03:36 +0100594}
595
596template (value) AssignmentState ts_AssignmentStateInit := {
597 voice_call := false,
598 is_assignment := false,
599 rr_ass_cmpl_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100600 assignment_done := false,
Harald Welte211219e2018-01-29 22:03:36 +0100601 old_chan_nr := -,
602 rr_modify_seen := false,
Harald Welte21583082018-01-29 22:28:26 +0100603 modify_done := false
Harald Welte211219e2018-01-29 22:03:36 +0100604}
605
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200606private template RSL_IE_Body tr_EncrInfo(template RSL_AlgId alg, template octetstring key) := {
607 encr_info := {
608 len := ?,
609 alg_id := alg,
610 key := key
611 }
612}
613
614/* ensure the RSL CHAN ACT (during assignment) contains values we expect depending on test case */
615private function f_check_chan_act(AssignmentState st, RSL_Message chan_act) runs on MSC_ConnHdlr {
616 var RSL_IE_Body encr_info;
617 if (ispresent(g_pars.encr) and g_pars.encr.enc_alg != '01'O) {
618 if (not f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
619 setverdict(fail, "Missing Encryption IE in CHAN ACT");
620 } else {
621 var RSL_AlgId alg := f_chipher_mode_bssmap_to_rsl(g_pars.encr.enc_alg);
622 if (not match(encr_info, tr_EncrInfo(alg, g_pars.encr.enc_key))) {
623 setverdict(fail, "Wrong Encryption IE in CHAN ACT");
624 }
625 }
626 } else {
627 if (f_rsl_find_ie(chan_act, RSL_IE_ENCR_INFO, encr_info)) {
628 if (encr_info.encr_info.alg_id != RSL_ALG_ID_A5_0) {
629 setverdict(fail, "Unexpected Encryption in CHAN ACT");
630 }
631 }
632 }
633 /* FIXME: validate RSL_IE_ACT_TYPE, RSL_IE_CHAN_MODE, RSL_IE_CHAN_IDENT, RSL_IE_BS_POWER,
634 * RSL_IE_MS_POWER, RSL_IE_TIMING_ADVANCE */
635}
636
Harald Welte211219e2018-01-29 22:03:36 +0100637altstep as_assignment(inout AssignmentState st) runs on MSC_ConnHdlr {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100638 var RSL_Message rsl;
Harald Welte211219e2018-01-29 22:03:36 +0100639 [not st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
640 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
641 log("Rx L3 from net: ", l3);
642 if (ischosen(l3.msgs.rrm.assignmentCommand)) {
643 var RslChannelNr new_chan_nr;
644 var GsmArfcn arfcn;
645 f_ChDesc2RslChanNr(l3.msgs.rrm.assignmentCommand.descrOf1stChAfterTime,
646 new_chan_nr, arfcn);
647 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
Harald Weltec1a2fff2017-12-17 11:06:19 +0100648
Harald Welte211219e2018-01-29 22:03:36 +0100649 /* register our component for this channel number at the RSL Emulation */
650 f_rslem_register(0, new_chan_nr);
651 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_AssignmentComplete('00'O));
652 /* send assignment complete over the new channel */
653 RSL.send(ts_RSL_DATA_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
654 enc_PDU_ML3_MS_NW(l3_tx)));
655 /* by default, send via the new channel from now */
656 st.old_chan_nr := g_chan_nr;
657 g_chan_nr := new_chan_nr;
658 st.rr_ass_cmpl_seen := true;
Harald Welte8a9bf6f2018-05-10 22:00:49 +0200659 /* obtain channel activation from RSL_Emulation for new channel */
660 var RSL_Message chan_act := f_rslem_get_last_act(RSL_PROC, 0, g_chan_nr);
661 /* check it (e.g. for correct ciphering parameters) */
662 f_check_chan_act(st, chan_act);
Harald Welte211219e2018-01-29 22:03:36 +0100663 repeat;
664 } else {
665 setverdict(fail, "Unexpected L3 received", l3);
666 self.stop;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100667 }
Harald Welte211219e2018-01-29 22:03:36 +0100668 }
669 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
670 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
671 repeat;
672 }
673 [st.rr_ass_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
674 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
Harald Welte1909f462018-01-29 22:29:29 +0100675 /* unregister for old channel number in RSL emulation */
676 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
677 f_rslem_unregister(0, st.old_chan_nr);
Harald Welte21583082018-01-29 22:28:26 +0100678 st.assignment_done := true;
Harald Welte211219e2018-01-29 22:03:36 +0100679 repeat;
680 }
681}
682
683altstep as_modify(inout AssignmentState st) runs on MSC_ConnHdlr {
684 /* no assignment, just mode modify */
685 var RSL_Message rsl;
686
687 [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 +0100688 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
689 log("Rx L3 from net: ", l3);
690 if (ischosen(l3.msgs.rrm.channelModeModify)) {
691 f_rsl_reply(ts_RRM_ModeModifyAck(l3.msgs.rrm.channelModeModify.channelDescription,
692 l3.msgs.rrm.channelModeModify.channelMode), rsl);
Harald Welte211219e2018-01-29 22:03:36 +0100693 st.rr_modify_seen := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100694 }
695 repeat;
696 }
Harald Welte211219e2018-01-29 22:03:36 +0100697 [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 +0100698 RSL.send(ts_RSL_MODE_MODIFY_ACK(g_chan_nr));
Harald Welte21583082018-01-29 22:28:26 +0100699 st.modify_done := true;
Harald Weltec1a2fff2017-12-17 11:06:19 +0100700 repeat;
701 }
Harald Welte211219e2018-01-29 22:03:36 +0100702}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100703
Harald Welte211219e2018-01-29 22:03:36 +0100704/* Determine if given rsl_chan_nr is compatible with given BSSMAP ChannelType */
705function f_channel_compatible(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
706return boolean {
707 select (bssmap.speechOrDataIndicator) {
708 case ('0011'B) { /* Signalling */
709 /* all channels support signalling */
710 return true;
711 }
712 case else { /* Speech, Speech+CTM or CSD */
713 select (bssmap.channelRateAndType) {
714 case ('08'O) { /* TCH/F */
715 select (rsl_chan_nr) {
716 case (t_RslChanNr_Bm(?)) { return true; }
717 }
718 }
719 case ('09'O) { /* TCH/H */
720 select (rsl_chan_nr) {
721 case (t_RslChanNr_Lm(?, ?)) { return true; }
722 }
723 }
724 case else { /* full or half-rate */
725 select (rsl_chan_nr) {
726 case (t_RslChanNr_Bm(?)) { return true; }
727 case (t_RslChanNr_Lm(?, ?)) { return true; }
728 }
729 }
730 }
Harald Weltec1a2fff2017-12-17 11:06:19 +0100731 }
Harald Welte211219e2018-01-29 22:03:36 +0100732 }
733 return false;
734}
Daniel Willmann191e0d92018-01-17 12:44:35 +0100735
Philipp Maier8ef527e2018-05-18 11:12:50 +0200736/* Determine if the channel mode specified within rsl_chan_nr requires a
737 * MODE MODIFY in to match the channel mode specified by given BSSMAP
738 * ChannelType */
739function f_channel_needs_modify(BSSMAP_IE_ChannelType bssmap, RslChannelNr rsl_chan_nr)
740return boolean {
741
742 /* FIXME: This tests the rsl_chan_nr to determine if we are on a
743 * signalling channel or not. Unfortunately this may lead to false
744 * results if we are on a TCH. The problem is that a TCH may be also
745 * used in signalling mode, but this function assumes that only SDCCH4
746 * and SDCCH8 are used as signalling channels at all. */
747
748 var boolean current_signalling := false;
749 var boolean desired_signalling := false;
750
751 select (rsl_chan_nr) {
752 case (t_RslChanNr_SDCCH4(?, ?)) { current_signalling := true; }
753 case (t_RslChanNr_SDCCH8(?, ?)) { current_signalling := true; }
754 }
755
756 if (bssmap.speechOrDataIndicator == '0011'B) {
757 desired_signalling := true;
758 }
759
760 if (current_signalling == desired_signalling) {
761 /* The desired channel mode is equal to the one we currently
762 * have, there is no mode modification needed or expected */
763 return false;
764 } else {
765 /* The desired channel mode and the current channel mode do
766 * not match. A mode modification is required */
767 return true;
768 }
769}
770
Harald Weltecc0b0142018-05-29 15:19:33 +0200771/* patch an BSSMAP ASS REQ with LCLS related IEs, depending on g_params */
772function f_ass_patch_lcls(inout template (omit) PDU_BSSAP ass_tpl,
773 inout template PDU_BSSAP ass_cpl) runs on MSC_ConnHdlr {
774 if (istemplatekind(ass_tpl, "omit")) {
775 return;
776 }
777 if (ispresent(g_pars.lcls.gcr)) {
778 ass_tpl.pdu.bssmap.assignmentRequest.globalCallReference := ts_BSSMAP_IE_GCR(g_pars.lcls.gcr);
779 }
780 if (ispresent(g_pars.lcls.cfg)) {
781 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_Configuration := ts_BSSMAP_IE_LclsCfg(g_pars.lcls.cfg);
782 }
783 if (ispresent(g_pars.lcls.csc)) {
784 ass_tpl.pdu.bssmap.assignmentRequest.lCLS_ConnectionStatusControl := ts_BSSMAP_IE_LclsCsc(g_pars.lcls.csc);
785 }
Harald Welte343b7742018-06-05 23:41:37 +0200786 if (ischosen(ass_cpl.pdu.bssmap.assignmentComplete)) {
787 if (ispresent(g_pars.lcls.exp_sts)) {
788 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := tr_BSSMAP_IE_LclsSts(g_pars.lcls.exp_sts);
789 } else {
790 ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status := omit;
791 }
Harald Weltecc0b0142018-05-29 15:19:33 +0200792 }
793}
794
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200795/* Helper function to check if the activity on the MGCP matches what we
796 * expected */
797function f_check_mgcp_expectations() runs on MSC_ConnHdlr {
798 for (var integer i:= 0; i < sizeof(g_media.mgcp_conn); i := i+1) {
Philipp Maier0a5d7e72018-07-16 15:13:11 +0200799 log(testcasename(), ": Check MGCP test expectations for g_media.mgcp_conn[", i , "]:",
800 " crcx_seen=", g_media.mgcp_conn[i].crcx_seen, ", crcx_seen_exp=", g_media.mgcp_conn[i].crcx_seen_exp,
801 ", mdcx_seen=", g_media.mgcp_conn[i].mdcx_seen, ", mdcx_seen_exp=", g_media.mgcp_conn[i].mdcx_seen_exp);
802
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200803 if(g_media.mgcp_conn[i].crcx_seen != g_media.mgcp_conn[i].crcx_seen_exp) {
804 setverdict(fail, "unexpected number of MGW-CRCX transactions");
805 }
806 if(g_media.mgcp_conn[i].mdcx_seen != g_media.mgcp_conn[i].mdcx_seen_exp) {
807 setverdict(fail, "unexpected number of MGW-MDCX transactions");
808 }
809 }
810}
811
Harald Welte211219e2018-01-29 22:03:36 +0100812/* establish a channel fully, expecting an assignment matching 'exp' */
Harald Welte7a14fd52018-05-10 22:26:55 +0200813function f_establish_fully(template (omit) PDU_BSSAP ass_tpl, template PDU_BSSAP exp_ass_cpl)
814runs on MSC_ConnHdlr {
Philipp Maier11a58942018-06-25 16:40:48 +0200815
816 var BSSMAP_FIELD_CodecType codecType;
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200817 var boolean sccplite := false;
818
819 /* Check if we run on SCCPLITE instead of SCCP by looking if a CIC is
820 * present or not. */
821 if (isvalue(ass_tpl.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
822 sccplite := true;
823 }
Philipp Maier11a58942018-06-25 16:40:48 +0200824
825 if (isvalue(ass_tpl.pdu.bssmap.assignmentRequest.codecList)) {
826 codecType := valueof(ass_tpl.pdu.bssmap.assignmentRequest.codecList.codecElements[0].codecType);
827 } else {
828 /* Make sure a meaningful default is assigned in case the
829 * codecList is not populated */
830 codecType := FR_AMR;
831 }
832
833 f_MscConnHdlr_init(g_pars.media_nr, "127.0.0.2", "127.0.0.3", codecType);
Harald Welte7a14fd52018-05-10 22:26:55 +0200834
Harald Weltecc0b0142018-05-29 15:19:33 +0200835 /* patch in the LCLS related items, as needed */
836 f_ass_patch_lcls(ass_tpl, exp_ass_cpl);
837
Harald Welte7a14fd52018-05-10 22:26:55 +0200838 f_create_chan_and_exp();
839 /* we should now have a COMPL_L3 at the MSC */
840 BSSAP.receive(tr_BSSMAP_ComplL3);
841
842 /* start ciphering, if requested */
843 if (ispresent(g_pars.encr)) {
844 f_cipher_mode(g_pars.encr.enc_alg, g_pars.encr.enc_key);
845 }
846
847 /* bail out early if no assignment requested */
848 if (istemplatekind(ass_tpl, "omit")) {
849 return;
850 }
851
852 var PDU_BSSAP ass_cmd := valueof(ass_tpl);
Harald Welte211219e2018-01-29 22:03:36 +0100853 var PDU_BSSAP bssap;
854 timer T := 10.0;
855 var boolean exp_compl := ischosen(exp_ass_cpl.pdu.bssmap.assignmentComplete);
Philipp Maier86f39202018-02-07 14:40:09 +0100856 var boolean exp_fail := ischosen(exp_ass_cpl.pdu.bssmap.assignmentFailure);
Philipp Maier8ef527e2018-05-18 11:12:50 +0200857 var boolean exp_modify;
Harald Welte211219e2018-01-29 22:03:36 +0100858 var ExpectCriteria mgcpcrit := {
859 connid := omit,
860 endpoint := omit,
861 transid := omit
862 };
863 var AssignmentState st := valueof(ts_AssignmentStateInit);
864 /* if the channel type is SIGNAL, we're not handling a voice call */
865 if (ass_cmd.pdu.bssmap.assignmentRequest.channelType.speechOrDataIndicator != '0011'B) {
866 st.voice_call := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +0200867 exp_modify := true;
Harald Welte211219e2018-01-29 22:03:36 +0100868 }
Philipp Maier8ef527e2018-05-18 11:12:50 +0200869
Harald Welte211219e2018-01-29 22:03:36 +0100870 /* determine if the current channel can support the given service or not */
871 if (not f_channel_compatible(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr)) {
872 st.is_assignment := true;
Philipp Maier8ef527e2018-05-18 11:12:50 +0200873
874 /* We decided to assign a new channel, so we do not expect
875 * any mode modify messages on RSL */
876 exp_modify := false;
877 } else {
878 /* We will continue working with the currently assigned
879 * channel, we must now check if the mode of the current
880 * channel is compatible. If not we expect the BSC to modify
881 * the mode */
882 exp_modify := f_channel_needs_modify(ass_cmd.pdu.bssmap.assignmentRequest.channelType, g_chan_nr);
Harald Welte211219e2018-01-29 22:03:36 +0100883 }
884
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200885 /* Some test situations will involve MGCP transactions on a media
886 * gateway. Depending on the situation, we set up how many of each MGCP
887 * message type are expected to be exchanged. */
888 if (isbound(exp_ass_cpl.pdu.bssmap.assignmentFailure)) {
889 /* For tests that expect the assignment to fail, assume that
890 * there will be no MGW communication as well. */
891 g_media.mgcp_conn[0].crcx_seen_exp := 0;
892 g_media.mgcp_conn[0].mdcx_seen_exp := 0;
893 g_media.mgcp_conn[1].crcx_seen_exp := 0;
894 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
895 } else if (st.voice_call) {
896 /* For voice calls we expect the following MGCP activity */
897 if (sccplite) {
898 g_media.mgcp_conn[0].crcx_seen_exp := 1;
899 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
900 g_media.mgcp_conn[1].crcx_seen_exp := 0;
901 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
902 } else {
903 g_media.mgcp_conn[0].crcx_seen_exp := 1;
904 g_media.mgcp_conn[0].mdcx_seen_exp := 1;
905 g_media.mgcp_conn[1].crcx_seen_exp := 1;
906 g_media.mgcp_conn[1].mdcx_seen_exp := 0;
907 }
908 }
909
Harald Welte211219e2018-01-29 22:03:36 +0100910 f_create_mgcp_expect(mgcpcrit);
911 BSSAP.send(ass_cmd);
912
913 T.start;
914 alt {
915 /* assignment related bits */
916 [st.is_assignment] as_assignment(st);
917
918 /* modify related bits */
Philipp Maier8ef527e2018-05-18 11:12:50 +0200919 [not st.is_assignment and exp_modify] as_modify(st);
Harald Welte211219e2018-01-29 22:03:36 +0100920
921 /* voice call related bits (IPA CRCX/MDCX + MGCP) */
922 [st.voice_call] as_Media();
923
924 /* if we receive exactly what we expected, always return + pass */
Philipp Maier8ef527e2018-05-18 11:12:50 +0200925 [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 +0100926 setverdict(pass);
927 }
Philipp Maier86f39202018-02-07 14:40:09 +0100928 [exp_fail] BSSAP.receive(exp_ass_cpl) -> value bssap {
929 setverdict(pass);
930 }
Harald Welte21583082018-01-29 22:28:26 +0100931 [(st.is_assignment and st.assignment_done or
Philipp Maier8ef527e2018-05-18 11:12:50 +0200932 (not st.is_assignment and (st.modify_done or not exp_modify))) and
Harald Welte21583082018-01-29 22:28:26 +0100933 exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
Harald Weltec1a2fff2017-12-17 11:06:19 +0100934 setverdict(fail, "Received non-matching ASSIGNMENT COMPLETE");
935 }
936 [exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
937 setverdict(fail, "Received unexpected ASSIGNMENT FAIL");
938 }
939 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentComplete) {
940 setverdict(fail, "Received unexpected ASSIGNMENT COMPLETE");
941 }
942 [not exp_compl] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
943 setverdict(fail, "Received non-matching ASSIGNMENT FAIL");
944 }
945 [] T.timeout {
Harald Welte458fd372018-03-21 11:26:23 +0100946 setverdict(fail, "Timeout waiting for ASSIGNMENT COMPLETE");
Harald Weltec1a2fff2017-12-17 11:06:19 +0100947 }
948 }
Harald Welte211219e2018-01-29 22:03:36 +0100949 log("g_media ", g_media);
950 if (not isbound(bssap)) {
951 self.stop;
952 }
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200953
Philipp Maiera0976e92018-07-16 15:19:42 +0200954 /* When the BSC detects that LCLS is possible it will cross the
955 * connetions that point to the PBX side of the MGW. In our case this
956 * is mgcp_conn[1]. The BSC performs this operation already before the
957 * assignment complete is generated. This means we expect another MDCX
958 * at mgcp_conn[1] when LCLS is expected. */
959 if (ispresent(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue)) {
960 if (valueof(exp_ass_cpl.pdu.bssmap.assignmentComplete.lCLS_BSS_Status.lCLS_BSS_StatusValue) == LCLS_STS_locally_switched) {
961 g_media.mgcp_conn[1].mdcx_seen_exp := g_media.mgcp_conn[1].mdcx_seen_exp + 1;
962
963 }
964 }
965
Philipp Maier3e2af5d2018-07-11 17:01:05 +0200966 f_check_mgcp_expectations();
Harald Welte930d0a72018-03-22 22:08:40 +0100967}
968
Harald Welte261af4b2018-02-12 21:20:39 +0100969type record HandoverState {
970 /* Assignment related bits */
971 boolean rr_ho_cmpl_seen,
972 boolean handover_done,
973 RslChannelNr old_chan_nr
974};
975
976altstep as_handover(inout HandoverState st) runs on MSC_ConnHdlr {
977 var RSL_Message rsl;
978 [not st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_DATA_REQ(g_chan_nr)) -> value rsl {
979 var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(rsl.ies[2].body.l3_info.payload);
980 log("Rx L3 from net: ", l3);
981 if (ischosen(l3.msgs.rrm.handoverCommand)) {
982 var RslChannelNr new_chan_nr;
983 var GsmArfcn arfcn;
984 f_ChDesc2RslChanNr(l3.msgs.rrm.handoverCommand.channelDescription2,
985 new_chan_nr, arfcn);
986 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
987
988 /* register our component for this channel number at the RSL Emulation */
989 f_rslem_register(0, new_chan_nr, RSL1_PROC);
990
991 /* resume processing of RSL DChan messages, which was temporarily suspended
992 * before performing a hand-over */
993 f_rslem_resume(RSL1_PROC);
994
Neels Hofmeyr378a49c2018-06-15 22:18:43 +0200995 /* send handover detect */
996 RSL1.send(ts_RSL_HANDO_DET(new_chan_nr));
997 f_sleep(0.3);
998
Harald Welte261af4b2018-02-12 21:20:39 +0100999 /* send handover complete over the new channel */
1000 var PDU_ML3_MS_NW l3_tx := valueof(ts_RRM_HandoverComplete('00'O));
1001 RSL1.send(ts_RSL_DATA_IND(new_chan_nr, valueof(ts_RslLinkID_DCCH(0)),
1002 enc_PDU_ML3_MS_NW(l3_tx)));
1003 /* by default, send via the new channel from now */
1004 st.old_chan_nr := g_chan_nr;
1005 g_chan_nr := new_chan_nr;
1006 st.rr_ho_cmpl_seen := true;
1007 repeat;
1008 } else {
1009 setverdict(fail, "Unexpected L3 received", l3);
1010 self.stop;
1011 }
1012 }
1013 [st.rr_ho_cmpl_seen] as_Media();
1014 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_REL_REQ(st.old_chan_nr, tr_RslLinkID_DCCH(0))) {
1015 RSL.send(ts_RSL_REL_CONF(st.old_chan_nr, valueof(ts_RslLinkID_DCCH(0))));
1016 repeat;
1017 }
1018 [st.rr_ho_cmpl_seen] RSL.receive(tr_RSL_RF_CHAN_REL(st.old_chan_nr)) {
1019 RSL.send(ts_RSL_RF_CHAN_REL_ACK(st.old_chan_nr));
1020 /* unregister for old channel number in RSL emulation */
1021 /* FIXME: Determine TRX NR by ARFCN, instead of hard-coded TRX0! */
1022 f_rslem_unregister(0, st.old_chan_nr);
1023 st.handover_done := true;
Harald Welte261af4b2018-02-12 21:20:39 +01001024 }
1025}
1026
1027
Harald Weltec1a2fff2017-12-17 11:06:19 +01001028
Harald Welte28d943e2017-11-25 15:00:50 +01001029}