Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 1 | module DIAMETER_Emulation { |
| 2 | |
| 3 | /* DIAMETER Emulation, runs on top of DIAMETER_CodecPort. It multiplexes/demultiplexes |
| 4 | * the individual IMSIs/subscribers, so there can be separate TTCN-3 components handling |
| 5 | * each of them. |
| 6 | * |
| 7 | * The DIAMETER_Emulation.main() function processes DIAMETER primitives from the DIAMETER |
| 8 | * socket via the DIAMETER_CodecPort, and dispatches them to the per-IMSI components. |
| 9 | * |
| 10 | * For each new IMSI, the DiameterOps.create_cb() is called. It can create |
| 11 | * or resolve a TTCN-3 component, and returns a component reference to which that IMSI |
| 12 | * is routed/dispatched. |
| 13 | * |
| 14 | * If a pre-existing component wants to register to handle a future inbound IMSI, it can |
| 15 | * do so by registering an "expect" with the expected IMSI. |
| 16 | * |
| 17 | * Inbound DIAMETER messages without IMSI (such as RESET-IND/ACK) are dispatched to |
| 18 | * the DiameterOps.unitdata_cb() callback, which is registered with an argument to the |
| 19 | * main() function below. |
| 20 | * |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 21 | * Alternatively, all inbound DIAMETER PDUs can be routed to a single component |
| 22 | * regardless of the IMSI. This is called 'raw' mode and can be achieved by |
| 23 | * setting the 'raw' field in DIAMETEROps to true. |
| 24 | * |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 25 | * (C) 2019 by Harald Welte <laforge@gnumonks.org> |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * Released under the terms of GNU General Public License, Version 2 or |
| 29 | * (at your option) any later version. |
| 30 | * |
| 31 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 32 | */ |
| 33 | |
| 34 | import from DIAMETER_CodecPort all; |
| 35 | import from DIAMETER_CodecPort_CtrlFunct all; |
| 36 | import from DIAMETER_Types all; |
| 37 | import from DIAMETER_Templates all; |
| 38 | import from Osmocom_Types all; |
| 39 | import from IPL4asp_Types all; |
Harald Welte | 61f73d5 | 2020-04-26 21:41:12 +0200 | [diff] [blame] | 40 | import from Native_Functions all; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 41 | |
| 42 | type hexstring IMSI; |
| 43 | |
Harald Welte | d01b5d0 | 2020-04-26 22:05:53 +0200 | [diff] [blame] | 44 | /* notify the recipient that a Capability Exchange happened */ |
| 45 | type record DiameterCapabilityExchgInd { |
| 46 | PDU_DIAMETER rx, |
| 47 | PDU_DIAMETER tx |
| 48 | }; |
| 49 | |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 50 | type component DIAMETER_ConnHdlr { |
| 51 | port DIAMETER_Conn_PT DIAMETER; |
| 52 | /* procedure based port to register for incoming connections */ |
| 53 | port DIAMETEREM_PROC_PT DIAMETER_PROC; |
| 54 | } |
| 55 | |
| 56 | /* port between individual per-connection components and this dispatcher */ |
| 57 | type port DIAMETER_Conn_PT message { |
Harald Welte | 0e03808 | 2019-08-18 19:38:54 +0200 | [diff] [blame] | 58 | inout PDU_DIAMETER; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 59 | } with { extension "internal" }; |
| 60 | |
| 61 | /* global test port e.g. for non-imsi/conn specific messages */ |
| 62 | type port DIAMETER_PT message { |
Harald Welte | d01b5d0 | 2020-04-26 22:05:53 +0200 | [diff] [blame] | 63 | inout PDU_DIAMETER, DiameterCapabilityExchgInd; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 64 | } with { extension "internal" }; |
| 65 | |
| 66 | |
| 67 | /* represents a single DIAMETER Association */ |
| 68 | type record AssociationData { |
| 69 | DIAMETER_ConnHdlr comp_ref, |
| 70 | hexstring imsi optional |
| 71 | }; |
| 72 | |
| 73 | type component DIAMETER_Emulation_CT { |
| 74 | /* Port facing to the UDP SUT */ |
| 75 | port DIAMETER_CODEC_PT DIAMETER; |
| 76 | /* All DIAMETER_ConnHdlr DIAMETER ports connect here |
| 77 | * DIAMETER_Emulation_CT.main needs to figure out what messages |
| 78 | * to send where with CLIENT.send() to vc_conn */ |
| 79 | port DIAMETER_Conn_PT DIAMETER_CLIENT; |
| 80 | /* currently tracked connections */ |
Pau Espin Pedrol | b335f01 | 2022-04-11 12:07:41 +0200 | [diff] [blame] | 81 | var AssociationData DiameterAssocTable[256]; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 82 | /* pending expected CRCX */ |
Pau Espin Pedrol | b335f01 | 2022-04-11 12:07:41 +0200 | [diff] [blame] | 83 | var ExpectData DiameterExpectTable[256]; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 84 | /* procedure based port to register for incoming connections */ |
| 85 | port DIAMETEREM_PROC_PT DIAMETER_PROC; |
| 86 | /* test port for unit data messages */ |
| 87 | port DIAMETER_PT DIAMETER_UNIT; |
| 88 | |
| 89 | var charstring g_diameter_id; |
| 90 | var integer g_diameter_conn_id := -1; |
| 91 | } |
| 92 | |
| 93 | type function DIAMETERCreateCallback(PDU_DIAMETER msg, hexstring imsi, charstring id) |
| 94 | runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr; |
| 95 | |
| 96 | type function DIAMETERUnitdataCallback(PDU_DIAMETER msg) |
| 97 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER; |
| 98 | |
| 99 | type record DIAMETEROps { |
| 100 | DIAMETERCreateCallback create_cb, |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 101 | DIAMETERUnitdataCallback unitdata_cb, |
| 102 | /* If true, this parameter disables IMSI based routing, so that all incoming |
| 103 | * PDUs get routed to a single component connected via the DIAMETER_UNIT port. */ |
| 104 | boolean raw |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | type record DIAMETER_conn_parameters { |
| 108 | HostName remote_ip, |
| 109 | PortNumber remote_sctp_port, |
| 110 | HostName local_ip, |
Harald Welte | 61f73d5 | 2020-04-26 21:41:12 +0200 | [diff] [blame] | 111 | PortNumber local_sctp_port, |
| 112 | charstring origin_host, |
| 113 | charstring origin_realm, |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 114 | uint32_t auth_app_id optional, |
| 115 | uint32_t vendor_app_id optional |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | function tr_DIAMETER_RecvFrom_R(template PDU_DIAMETER msg) |
| 119 | runs on DIAMETER_Emulation_CT return template DIAMETER_RecvFrom { |
| 120 | var template DIAMETER_RecvFrom mrf := { |
| 121 | connId := g_diameter_conn_id, |
| 122 | remName := ?, |
| 123 | remPort := ?, |
| 124 | locName := ?, |
| 125 | locPort := ?, |
| 126 | msg := msg |
| 127 | } |
| 128 | return mrf; |
| 129 | } |
| 130 | |
| 131 | private function f_imsi_known(hexstring imsi) |
| 132 | runs on DIAMETER_Emulation_CT return boolean { |
| 133 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 134 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 135 | if (DiameterAssocTable[i].imsi == imsi) { |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | private function f_comp_known(DIAMETER_ConnHdlr client) |
| 143 | runs on DIAMETER_Emulation_CT return boolean { |
| 144 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 145 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 146 | if (DiameterAssocTable[i].comp_ref == client) { |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | private function f_comp_by_imsi(hexstring imsi) |
| 154 | runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr { |
| 155 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 156 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 157 | if (DiameterAssocTable[i].imsi == imsi) { |
| 158 | return DiameterAssocTable[i].comp_ref; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | setverdict(fail, "DIAMETER Association Table not found by IMSI", imsi); |
| 162 | mtc.stop; |
| 163 | } |
| 164 | |
| 165 | private function f_imsi_by_comp(DIAMETER_ConnHdlr client) |
| 166 | runs on DIAMETER_Emulation_CT return hexstring { |
| 167 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 168 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 169 | if (DiameterAssocTable[i].comp_ref == client) { |
| 170 | return DiameterAssocTable[i].imsi; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | setverdict(fail, "DIAMETER Association Table not found by component ", client); |
| 174 | mtc.stop; |
| 175 | } |
| 176 | |
| 177 | private function f_imsi_table_add(DIAMETER_ConnHdlr comp_ref, hexstring imsi) |
| 178 | runs on DIAMETER_Emulation_CT { |
| 179 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 180 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 181 | if (not isvalue(DiameterAssocTable[i].imsi)) { |
| 182 | DiameterAssocTable[i].imsi := imsi; |
| 183 | DiameterAssocTable[i].comp_ref := comp_ref; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | testcase.stop("DIAMETER Association Table full!"); |
| 188 | } |
| 189 | |
| 190 | private function f_imsi_table_del(DIAMETER_ConnHdlr comp_ref, hexstring imsi) |
| 191 | runs on DIAMETER_Emulation_CT { |
| 192 | var integer i; |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 193 | for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 194 | if (DiameterAssocTable[i].comp_ref == comp_ref and |
| 195 | DiameterAssocTable[i].imsi == imsi) { |
| 196 | DiameterAssocTable[i].imsi := omit; |
| 197 | DiameterAssocTable[i].comp_ref := null; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | } |
| 201 | setverdict(fail, "DIAMETER Association Table: Couldn't find to-be-deleted entry!"); |
| 202 | mtc.stop; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | private function f_imsi_table_init() |
| 207 | runs on DIAMETER_Emulation_CT { |
Pau Espin Pedrol | e436170 | 2022-04-11 11:59:40 +0200 | [diff] [blame] | 208 | for (var integer i := 0; i < sizeof(DiameterAssocTable); i := i+1) { |
| 209 | DiameterAssocTable[i].comp_ref := null; |
| 210 | DiameterAssocTable[i].imsi := omit; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 214 | function f_DIAMETER_get_imsi(PDU_DIAMETER pdu) return template (omit) IMSI |
| 215 | { |
| 216 | var template (omit) AVP imsi_avp; |
| 217 | |
| 218 | imsi_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_BASE_NONE_User_Name); |
| 219 | if (istemplatekind(imsi_avp, "omit")) { |
Harald Welte | f9fb63e | 2020-04-26 18:07:19 +0200 | [diff] [blame] | 220 | var template (omit) AVP sid_avp; |
| 221 | sid_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_DCC_NONE_Subscription_Id); |
| 222 | if (istemplatekind(sid_avp, "omit")) { |
| 223 | return omit; |
| 224 | } |
| 225 | var AVP_Grouped grp := valueof(sid_avp.avp_data.avp_DCC_NONE_Subscription_Id); |
Pau Espin Pedrol | b8cd34a | 2022-05-18 16:34:12 +0200 | [diff] [blame] | 226 | if (not match(grp[0], tr_AVP_SubcrIdType(END_USER_IMSI))) { |
Harald Welte | f9fb63e | 2020-04-26 18:07:19 +0200 | [diff] [blame] | 227 | return omit; |
| 228 | } |
| 229 | return str2hex(oct2char(grp[1].avp.avp_data.avp_DCC_NONE_Subscription_Id_Data)); |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 230 | } else { |
| 231 | var octetstring imsi_oct := valueof(imsi_avp.avp_data.avp_BASE_NONE_User_Name); |
| 232 | return str2hex(oct2char(imsi_oct)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := { |
| 237 | sinfo_stream := omit, |
| 238 | sinfo_ppid := ppid, |
| 239 | remSocks := omit, |
| 240 | assocId := omit |
| 241 | }; |
| 242 | |
| 243 | private template PortEvent tr_SctpAssocChange := { |
| 244 | sctpEvent := { |
| 245 | sctpAssocChange := ? |
| 246 | } |
| 247 | } |
| 248 | private template PortEvent tr_SctpPeerAddrChange := { |
| 249 | sctpEvent := { |
| 250 | sctpPeerAddrChange := ? |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private function f_diameter_xceive(template (value) PDU_DIAMETER tx, |
| 255 | template PDU_DIAMETER rx_t := ?) |
| 256 | runs on DIAMETER_Emulation_CT return PDU_DIAMETER { |
| 257 | timer T := 10.0; |
| 258 | var DIAMETER_RecvFrom mrf; |
| 259 | |
| 260 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, tx)); |
Vadim Yanitskiy | 672a41e | 2023-02-14 04:52:56 +0700 | [diff] [blame] | 261 | T.start; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 262 | alt { |
| 263 | [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(rx_t)) -> value mrf { } |
Vadim Yanitskiy | 3148a0e | 2023-02-11 09:27:42 +0700 | [diff] [blame] | 264 | [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf { |
| 265 | setverdict(fail, "Rx unexpected DIAMETER PDU: ", mrf); |
| 266 | mtc.stop; |
| 267 | } |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 268 | [] DIAMETER.receive(tr_SctpAssocChange) { repeat; } |
| 269 | [] DIAMETER.receive(tr_SctpPeerAddrChange) { repeat; } |
| 270 | [] T.timeout { |
| 271 | setverdict(fail, "Timeout waiting for ", rx_t); |
| 272 | mtc.stop; |
| 273 | } |
| 274 | } |
| 275 | return mrf.msg; |
| 276 | } |
| 277 | |
| 278 | function main(DIAMETEROps ops, DIAMETER_conn_parameters p, charstring id) runs on DIAMETER_Emulation_CT { |
Vadim Yanitskiy | b94e459 | 2023-02-11 08:09:09 +0700 | [diff] [blame] | 279 | var boolean server_mode := p.remote_sctp_port == -1; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 280 | var Result res; |
| 281 | g_diameter_id := id; |
| 282 | f_imsi_table_init(); |
| 283 | f_expect_table_init(); |
| 284 | |
| 285 | map(self:DIAMETER, system:DIAMETER_CODEC_PT); |
Vadim Yanitskiy | b94e459 | 2023-02-11 08:09:09 +0700 | [diff] [blame] | 286 | if (server_mode) { |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 287 | res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_listen(DIAMETER, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) }); |
| 288 | } else { |
| 289 | res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_connect(DIAMETER, p.remote_ip, p.remote_sctp_port, |
| 290 | p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) }); |
| 291 | } |
| 292 | if (not ispresent(res.connId)) { |
| 293 | setverdict(fail, "Could not connect DIAMETER socket, check your configuration"); |
| 294 | mtc.stop; |
| 295 | } |
| 296 | g_diameter_conn_id := res.connId; |
| 297 | |
Vadim Yanitskiy | b94e459 | 2023-02-11 08:09:09 +0700 | [diff] [blame] | 298 | /* If in client mode, send CER immediately */ |
| 299 | if (not server_mode) { |
| 300 | var template (value) PDU_DIAMETER req; |
| 301 | var PDU_DIAMETER rsp; |
| 302 | |
| 303 | req := ts_DIA_CER(f_inet_addr(p.local_ip), p.vendor_app_id, |
| 304 | orig_host := p.origin_host, orig_realm := p.origin_realm); |
| 305 | rsp := f_diameter_xceive(req, tr_DIAMETER_A(Capabilities_Exchange, req.application_id)); |
| 306 | /* notify our user that the CER->CEA exchange has happened */ |
| 307 | DIAMETER_UNIT.send(DiameterCapabilityExchgInd:{rx := rsp, tx := valueof(req)}); |
| 308 | } |
| 309 | |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 310 | while (true) { |
| 311 | var DIAMETER_ConnHdlr vc_conn; |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 312 | var template IMSI imsi_t; |
| 313 | var hexstring imsi; |
| 314 | var DIAMETER_RecvFrom mrf; |
| 315 | var PDU_DIAMETER msg; |
| 316 | var charstring vlr_name, mme_name; |
| 317 | var PortEvent port_evt; |
| 318 | |
| 319 | alt { |
| 320 | [] DIAMETER.receive(PortEvent:{connOpened := ?}) -> value port_evt { |
| 321 | g_diameter_conn_id := port_evt.connOpened.connId; |
| 322 | } |
| 323 | [] DIAMETER.receive(PortEvent:?) { } |
| 324 | /* DIAMETER from client */ |
| 325 | [] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg sender vc_conn { |
| 326 | /* Pass message through */ |
| 327 | /* TODO: check which ConnectionID client has allocated + store in table? */ |
| 328 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg)); |
| 329 | } |
| 330 | |
| 331 | /* handle CER/CEA handshake */ |
| 332 | [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIAMETER_R(cmd_code := Capabilities_Exchange))) -> value mrf { |
| 333 | var template (value) PDU_DIAMETER resp; |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 334 | resp := f_ts_DIA_CEA(mrf.msg.hop_by_hop_id, mrf.msg.end_to_end_id, p.origin_host, |
| 335 | p.origin_realm, f_inet_addr(p.local_ip), p.auth_app_id, p.vendor_app_id); |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 336 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, resp)); |
Harald Welte | d01b5d0 | 2020-04-26 22:05:53 +0200 | [diff] [blame] | 337 | /* notify our user that the CER->CEA exchange has happened */ |
| 338 | DIAMETER_UNIT.send(DiameterCapabilityExchgInd:{rx:=mrf.msg, tx:=valueof(resp)}); |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 339 | } |
Vadim Yanitskiy | 3f7a6dc | 2021-12-11 04:13:59 +0300 | [diff] [blame] | 340 | /* handle DWR/DWA ping-pong */ |
| 341 | [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIA_DWR)) -> value mrf { |
| 342 | var template (value) PDU_DIAMETER resp; |
| 343 | resp := ts_DIA_DWA('00000001'O, p.origin_host, p.origin_realm, |
| 344 | hbh_id := mrf.msg.hop_by_hop_id, |
| 345 | ete_id := mrf.msg.end_to_end_id); |
| 346 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp))); |
| 347 | } |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 348 | |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 349 | /* DIAMETER from the test suite */ |
| 350 | [ops.raw] DIAMETER_UNIT.receive(PDU_DIAMETER:?) -> value msg { |
| 351 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg)); |
| 352 | } |
| 353 | /* DIAMETER from remote peer (raw mode) */ |
| 354 | [ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf { |
| 355 | DIAMETER_UNIT.send(mrf.msg); |
| 356 | } |
| 357 | /* DIAMETER from remote peer (IMSI based routing) */ |
| 358 | [not ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf { |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 359 | imsi_t := f_DIAMETER_get_imsi(mrf.msg); |
| 360 | if (isvalue(imsi_t)) { |
| 361 | imsi := valueof(imsi_t); |
| 362 | if (f_imsi_known(imsi)) { |
| 363 | vc_conn := f_comp_by_imsi(imsi); |
| 364 | DIAMETER_CLIENT.send(mrf.msg) to vc_conn; |
| 365 | } else { |
| 366 | vc_conn := ops.create_cb.apply(mrf.msg, imsi, id); |
| 367 | f_imsi_table_add(vc_conn, imsi); |
| 368 | DIAMETER_CLIENT.send(mrf.msg) to vc_conn; |
| 369 | } |
| 370 | } else { |
| 371 | /* message contained no IMSI; is not IMSI-oriented */ |
| 372 | var template PDU_DIAMETER resp := ops.unitdata_cb.apply(mrf.msg); |
| 373 | if (isvalue(resp)) { |
| 374 | DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp))); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | [] DIAMETER.receive(tr_SctpAssocChange) { } |
| 379 | [] DIAMETER.receive(tr_SctpPeerAddrChange) { } |
| 380 | [] DIAMETER_PROC.getcall(DIAMETEREM_register:{?,?}) -> param(imsi, vc_conn) { |
| 381 | f_create_expect(imsi, vc_conn); |
| 382 | DIAMETER_PROC.reply(DIAMETEREM_register:{imsi, vc_conn}) to vc_conn; |
| 383 | } |
| 384 | |
| 385 | } |
| 386 | |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* "Expect" Handling */ |
| 391 | |
| 392 | type record ExpectData { |
| 393 | hexstring imsi optional, |
| 394 | DIAMETER_ConnHdlr vc_conn |
| 395 | } |
| 396 | |
| 397 | signature DIAMETEREM_register(in hexstring imsi, in DIAMETER_ConnHdlr hdlr); |
| 398 | |
| 399 | type port DIAMETEREM_PROC_PT procedure { |
| 400 | inout DIAMETEREM_register; |
| 401 | } with { extension "internal" }; |
| 402 | |
Pau Espin Pedrol | 69730a2 | 2022-04-11 12:00:53 +0200 | [diff] [blame] | 403 | /* Function that can be used as create_cb and will use the expect table */ |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 404 | function ExpectedCreateCallback(PDU_DIAMETER msg, hexstring imsi, charstring id) |
| 405 | runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr { |
| 406 | var DIAMETER_ConnHdlr ret := null; |
| 407 | var integer i; |
| 408 | |
| 409 | for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) { |
| 410 | if (not ispresent(DiameterExpectTable[i].imsi)) { |
| 411 | continue; |
| 412 | } |
| 413 | if (imsi == DiameterExpectTable[i].imsi) { |
| 414 | ret := DiameterExpectTable[i].vc_conn; |
| 415 | /* Release this entry */ |
| 416 | DiameterExpectTable[i].imsi := omit; |
| 417 | DiameterExpectTable[i].vc_conn := null; |
| 418 | log("Found Expect[", i, "] for ", msg, " handled at ", ret); |
| 419 | return ret; |
| 420 | } |
| 421 | } |
| 422 | setverdict(fail, "Couldn't find Expect for ", msg); |
| 423 | mtc.stop; |
| 424 | } |
| 425 | |
| 426 | private function f_create_expect(hexstring imsi, DIAMETER_ConnHdlr hdlr) |
| 427 | runs on DIAMETER_Emulation_CT { |
| 428 | var integer i; |
| 429 | |
| 430 | /* Check an entry like this is not already presnt */ |
| 431 | for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) { |
| 432 | if (imsi == DiameterExpectTable[i].imsi) { |
| 433 | setverdict(fail, "IMSI already present", imsi); |
| 434 | mtc.stop; |
| 435 | } |
| 436 | } |
| 437 | for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) { |
| 438 | if (not ispresent(DiameterExpectTable[i].imsi)) { |
| 439 | DiameterExpectTable[i].imsi := imsi; |
| 440 | DiameterExpectTable[i].vc_conn := hdlr; |
| 441 | log("Created Expect[", i, "] for ", imsi, " to be handled at ", hdlr); |
| 442 | return; |
| 443 | } |
| 444 | } |
| 445 | testcase.stop("No space left in DiameterExpectTable") |
| 446 | } |
| 447 | |
| 448 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 449 | function f_diameter_expect(hexstring imsi) runs on DIAMETER_ConnHdlr { |
| 450 | DIAMETER_PROC.call(DIAMETEREM_register:{imsi, self}) { |
| 451 | [] DIAMETER_PROC.getreply(DIAMETEREM_register:{?,?}) {}; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | private function f_expect_table_init() |
| 456 | runs on DIAMETER_Emulation_CT { |
| 457 | var integer i; |
| 458 | for (i := 0; i < sizeof(DiameterExpectTable); i := i + 1) { |
| 459 | DiameterExpectTable[i].imsi := omit; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | function DummyUnitdataCallback(PDU_DIAMETER msg) |
| 464 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER { |
| 465 | log("Ignoring DIAMETER ", msg); |
| 466 | return omit; |
| 467 | } |
| 468 | |
| 469 | |
Harald Welte | d01b5d0 | 2020-04-26 22:05:53 +0200 | [diff] [blame] | 470 | function f_diameter_wait_capability(DIAMETER_PT pt) |
| 471 | { |
| 472 | /* Wait for the Capability Exchange with the DUT */ |
| 473 | timer T := 10.0; |
| 474 | T.start; |
| 475 | alt { |
| 476 | [] pt.receive(DiameterCapabilityExchgInd:?) {} |
| 477 | [] pt.receive { |
| 478 | setverdict(fail, "Unexpected receive waiting for DiameterCapabilityExchgInd"); |
| 479 | mtc.stop; |
| 480 | } |
| 481 | [] T.timeout { |
| 482 | setverdict(fail, "Timeout waiting for DiameterCapabilityExchgInd"); |
| 483 | mtc.stop; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
Harald Welte | d27ab24 | 2019-07-26 13:45:18 +0200 | [diff] [blame] | 489 | } |