Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 1 | module GSUP_Emulation { |
| 2 | |
| 3 | /* GSUP Emulation, runs on top of IPA_Emulation. It multiplexes/demultiplexes |
| 4 | * the individual calls, so there can be separate TTCN-3 components handling |
| 5 | * each of the calls |
| 6 | * |
| 7 | * The GSUP_Emulation.main() function processes GSUP primitives from the IPA/GSUP |
| 8 | * socket via the IPA_Emulation, and dispatches them to the per-connection components. |
| 9 | * |
| 10 | * Outbound GSUP connections are initiated by sending a FIXME primitive |
| 11 | * to the component running the GSUP_Emulation.main() function. |
| 12 | * |
| 13 | * For each new inbound connections, the GsupOps.create_cb() is called. It can create |
| 14 | * or resolve a TTCN-3 component, and returns a component reference to which that inbound |
| 15 | * connection is routed/dispatched. |
| 16 | * |
| 17 | * If a pre-existing component wants to register to handle a future inbound call, it can |
| 18 | * do so by registering an "expect" with the expected destination phone number. This is e.g. useful |
| 19 | * if you are simulating BSC + HUL, and first trigger a connection from BSC side in a |
| 20 | * component which then subsequently should also handle the GSUP emulation. |
| 21 | * |
| 22 | * (C) 2018 by Harald Welte <laforge@gnumonks.org> |
| 23 | * All rights reserved. |
| 24 | * |
| 25 | * Released under the terms of GNU General Public License, Version 2 or |
| 26 | * (at your option) any later version. |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 27 | * |
| 28 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | |
| 32 | import from Osmocom_Types all; |
| 33 | import from GSUP_Types all; |
| 34 | import from IPA_Emulation all; |
| 35 | |
| 36 | /* General "base class" component definition, of which specific implementations |
| 37 | * derive themselves by means of the "extends" feature */ |
| 38 | type component GSUP_ConnHdlr { |
| 39 | /* ports towards GSUP Emulator core / call dispatchar */ |
| 40 | port GSUP_Conn_PT GSUP; |
| 41 | port GSUPEM_PROC_PT GSUP_PROC; |
| 42 | } |
| 43 | |
| 44 | /* port between individual per-connection components and this dispatcher */ |
| 45 | type port GSUP_Conn_PT message { |
| 46 | inout GSUP_PDU; |
| 47 | } with { extension "internal" }; |
| 48 | |
| 49 | |
| 50 | /* represents a single GSUP call */ |
| 51 | type record ConnectionData { |
| 52 | /* reference to the instance of the per-connection component */ |
| 53 | GSUP_ConnHdlr comp_ref, |
| 54 | charstring imsi |
| 55 | } |
| 56 | |
| 57 | type component GSUP_Emulation_CT { |
| 58 | /* UNIX DOMAIN socket on the bottom side, using primitives */ |
| 59 | port IPA_GSUP_PT GSUP; |
| 60 | /* GSUP port to the per-connection clients */ |
| 61 | port GSUP_Conn_PT GSUP_CLIENT; |
| 62 | |
| 63 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 64 | var ConnectionData GsupImsiTable[16]; |
| 65 | |
| 66 | /* pending expected incoming connections */ |
Vadim Yanitskiy | f3906e6 | 2020-01-10 00:46:57 +0100 | [diff] [blame] | 67 | var ExpectData GsupExpectTable[16]; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 68 | /* procedure based port to register for incoming connections */ |
| 69 | port GSUPEM_PROC_PT GSUP_PROC; |
| 70 | }; |
| 71 | |
| 72 | private function f_imsi_known(charstring imsi) |
| 73 | runs on GSUP_Emulation_CT return boolean { |
| 74 | var integer i; |
| 75 | for (i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 76 | if (GsupImsiTable[i].imsi == imsi) { |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | private function f_comp_known(GSUP_ConnHdlr client) |
| 84 | runs on GSUP_Emulation_CT return boolean { |
| 85 | var integer i; |
| 86 | for (i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 87 | if (GsupImsiTable[i].comp_ref == client) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /* resolve component reference by connection ID */ |
| 95 | private function f_comp_by_imsi(charstring imsi) |
| 96 | runs on GSUP_Emulation_CT return GSUP_ConnHdlr { |
| 97 | var integer i; |
| 98 | for (i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 99 | if (GsupImsiTable[i].imsi == imsi) { |
| 100 | return GsupImsiTable[i].comp_ref; |
| 101 | } |
| 102 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 103 | setverdict(fail, "GSUP IMSI table not found by IMSI ", imsi); |
| 104 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* resolve connection ID by component reference */ |
| 108 | private function f_imsi_by_comp(GSUP_ConnHdlr client) |
| 109 | runs on GSUP_Emulation_CT return charstring { |
| 110 | for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 111 | if (GsupImsiTable[i].comp_ref == client) { |
| 112 | return GsupImsiTable[i].imsi; |
| 113 | } |
| 114 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 115 | setverdict(fail, "GSUP IMSI table not found by component ", client); |
| 116 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private function f_imsi_table_init() |
| 120 | runs on GSUP_Emulation_CT { |
| 121 | for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 122 | GsupImsiTable[i].comp_ref := null; |
| 123 | GsupImsiTable[i].imsi := ""; |
| 124 | } |
| 125 | } |
| 126 | |
Harald Welte | d95da56 | 2018-01-27 21:46:53 +0100 | [diff] [blame] | 127 | private function f_expect_table_init() |
| 128 | runs on GSUP_Emulation_CT { |
| 129 | for (var integer i := 0; i < sizeof(GsupExpectTable); i := i+1) { |
| 130 | GsupExpectTable[i].vc_conn := null; |
| 131 | GsupExpectTable[i].imsi := omit; |
| 132 | } |
| 133 | } |
| 134 | |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 135 | private function f_imsi_table_add(GSUP_ConnHdlr comp_ref, charstring imsi) |
| 136 | runs on GSUP_Emulation_CT { |
| 137 | for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 138 | if (GsupImsiTable[i].imsi == "") { |
| 139 | GsupImsiTable[i].comp_ref := comp_ref; |
| 140 | GsupImsiTable[i].imsi := imsi; |
| 141 | log("Added IMSI table entry ", i, comp_ref, imsi); |
| 142 | return; |
| 143 | } |
| 144 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 145 | testcase.stop("GSUP IMSI table full!"); |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private function f_imsi_table_del(charstring imsi) |
| 149 | runs on GSUP_Emulation_CT { |
| 150 | for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) { |
| 151 | if (GsupImsiTable[i].imsi == imsi) { |
| 152 | log("Deleted GSUP IMSI table entry ", i, |
| 153 | GsupImsiTable[i].comp_ref, imsi); |
| 154 | GsupImsiTable[i].imsi := ""; |
| 155 | GsupImsiTable[i].comp_ref := null; |
| 156 | return |
| 157 | } |
| 158 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 159 | setverdict(fail, "GSUP IMSI table attempt to delete non-existant ", imsi); |
| 160 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 165 | * arrives */ |
| 166 | type function GsupCreateCallback(GSUP_PDU gsup, charstring id) |
| 167 | runs on GSUP_Emulation_CT return GSUP_ConnHdlr; |
| 168 | |
| 169 | type record GsupOps { |
| 170 | GsupCreateCallback create_cb |
| 171 | } |
| 172 | |
| 173 | function main(GsupOps ops, charstring id) runs on GSUP_Emulation_CT { |
| 174 | |
| 175 | f_imsi_table_init(); |
Harald Welte | d95da56 | 2018-01-27 21:46:53 +0100 | [diff] [blame] | 176 | f_expect_table_init(); |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 177 | |
| 178 | while (true) { |
| 179 | var GSUP_ConnHdlr vc_conn; |
| 180 | var GSUP_ConnHdlr vc_hdlr; |
| 181 | var GSUP_PDU gsup; |
| 182 | var charstring imsi; |
| 183 | |
| 184 | alt { |
| 185 | |
Vadim Yanitskiy | a2afacc | 2020-05-18 21:16:19 +0700 | [diff] [blame] | 186 | [] GSUP.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_ID_ACK)) { repeat; } |
| 187 | [] GSUP.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_UP)) { repeat; } |
| 188 | [] GSUP.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN)) { |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 189 | setverdict(fail, "GSUP Connection Lost"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 190 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* GSUP -> Client: call related messages */ |
| 194 | [] GSUP.receive(GSUP_PDU:?) -> value gsup { |
| 195 | imsi := hex2str(gsup.ies[0].val.imsi); |
| 196 | |
| 197 | if (f_imsi_known(imsi)) { |
| 198 | vc_conn := f_comp_by_imsi(imsi); |
| 199 | GSUP_CLIENT.send(gsup) to vc_conn; |
| 200 | } else { |
| 201 | /* TODO: Only accept this for SETUP.req? */ |
| 202 | vc_conn := ops.create_cb.apply(gsup, id) |
| 203 | /* store mapping between client components and SCCP connectionId */ |
| 204 | f_imsi_table_add(vc_conn, imsi); |
| 205 | /* handle user payload */ |
| 206 | GSUP_CLIENT.send(gsup) to vc_conn; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | [] GSUP.receive { repeat; } |
| 211 | |
| 212 | /* Client -> GSUP Socket: Normal message */ |
| 213 | [] GSUP_CLIENT.receive(GSUP_PDU:?) -> value gsup sender vc_conn { |
| 214 | /* forward to GSUP socket */ |
| 215 | GSUP.send(gsup); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* Client -> us: procedure call to register expect */ |
| 220 | [] GSUP_PROC.getcall(GSUPEM_register:{?,?}) -> param(imsi, vc_hdlr) { |
| 221 | f_create_expect(imsi, vc_hdlr); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 222 | GSUP_PROC.reply(GSUPEM_register:{imsi, vc_hdlr}) to vc_hdlr; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Neels Hofmeyr | 2c4f7d8 | 2021-07-28 00:55:14 +0200 | [diff] [blame] | 225 | [] GSUP_PROC.getcall(GSUPEM_change_connhdlr:{?,?}) -> param(imsi, vc_hdlr) { |
| 226 | f_imsi_table_del(imsi); |
| 227 | f_imsi_table_add(vc_hdlr, imsi); |
| 228 | GSUP_PROC.reply(GSUPEM_change_connhdlr:{imsi, vc_hdlr}) to vc_hdlr; |
| 229 | } |
| 230 | |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /*********************************************************************** |
| 236 | * "Expect" Handling (mapping for expected incoming GSUP calls from IUT) |
| 237 | ***********************************************************************/ |
| 238 | |
| 239 | /* data about an expected future incoming connection */ |
| 240 | type record ExpectData { |
| 241 | /* destination number based on which we can match it */ |
| 242 | charstring imsi optional, |
| 243 | /* component reference for this connection */ |
| 244 | GSUP_ConnHdlr vc_conn |
| 245 | } |
| 246 | |
| 247 | /* procedure based port to register for incoming calls */ |
| 248 | signature GSUPEM_register(in charstring imsi, in GSUP_ConnHdlr hdlr); |
Neels Hofmeyr | 2c4f7d8 | 2021-07-28 00:55:14 +0200 | [diff] [blame] | 249 | signature GSUPEM_change_connhdlr(in charstring imsi, in GSUP_ConnHdlr hdlr); |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 250 | |
| 251 | type port GSUPEM_PROC_PT procedure { |
Neels Hofmeyr | 2c4f7d8 | 2021-07-28 00:55:14 +0200 | [diff] [blame] | 252 | inout GSUPEM_register, GSUPEM_change_connhdlr; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 253 | } with { extension "internal" }; |
| 254 | |
| 255 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 256 | function ExpectedCreateCallback(GSUP_PDU gsup, charstring id) |
| 257 | runs on GSUP_Emulation_CT return GSUP_ConnHdlr { |
| 258 | var GSUP_ConnHdlr ret := null; |
| 259 | var charstring imsi; |
| 260 | var integer i; |
| 261 | |
| 262 | imsi := hex2str(gsup.ies[0].val.imsi); |
| 263 | |
| 264 | for (i := 0; i < sizeof(GsupExpectTable); i:= i+1) { |
| 265 | if (not ispresent(GsupExpectTable[i].imsi)) { |
| 266 | continue; |
| 267 | } |
| 268 | if (imsi == GsupExpectTable[i].imsi) { |
| 269 | ret := GsupExpectTable[i].vc_conn; |
| 270 | /* release this entry to be used again */ |
| 271 | GsupExpectTable[i].imsi := omit; |
| 272 | GsupExpectTable[i].vc_conn := null; |
| 273 | log("Found GsupExpect[", i, "] for ", imsi, " handled at ", ret); |
| 274 | /* return the component reference */ |
| 275 | return ret; |
| 276 | } |
| 277 | } |
| 278 | setverdict(fail, "Couldn't find GsupExpect for incoming imsi ", imsi); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 279 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* server/emulation side function to create expect */ |
| 284 | private function f_create_expect(charstring imsi, GSUP_ConnHdlr hdlr) |
| 285 | runs on GSUP_Emulation_CT { |
| 286 | var integer i; |
| 287 | for (i := 0; i < sizeof(GsupExpectTable); i := i+1) { |
| 288 | if (not ispresent(GsupExpectTable[i].imsi)) { |
| 289 | GsupExpectTable[i].imsi := imsi; |
| 290 | GsupExpectTable[i].vc_conn := hdlr; |
| 291 | log("Created GsupExpect[", i, "] for ", imsi, " to be handled at ", hdlr); |
| 292 | return; |
| 293 | } |
| 294 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 295 | testcase.stop("No space left in GsupExpectTable"); |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 299 | function f_create_gsup_expect(charstring imsi) runs on GSUP_ConnHdlr { |
| 300 | GSUP_PROC.call(GSUPEM_register:{imsi, self}) { |
| 301 | [] GSUP_PROC.getreply(GSUPEM_register:{?,?}) {}; |
| 302 | } |
| 303 | } |
| 304 | |
Oliver Smith | 6e81f7e | 2020-01-29 14:25:41 +0100 | [diff] [blame] | 305 | /* Same as f_create_gsup_expect, but with explicit addressing. Needed when connecting multiple ports to GSUP_PROC. */ |
| 306 | function f_create_gsup_expect_explicit(charstring imsi, GSUP_Emulation_CT ct) runs on GSUP_ConnHdlr { |
| 307 | GSUP_PROC.call(GSUPEM_register:{imsi, self}) to ct { |
| 308 | [] GSUP_PROC.getreply(GSUPEM_register:{?,?}) {}; |
| 309 | } |
| 310 | } |
| 311 | |
Neels Hofmeyr | 2c4f7d8 | 2021-07-28 00:55:14 +0200 | [diff] [blame] | 312 | function f_gsup_change_connhdlr(charstring imsi) runs on GSUP_ConnHdlr { |
| 313 | GSUP_PROC.call(GSUPEM_change_connhdlr:{imsi, self}) { |
| 314 | [] GSUP_PROC.getreply(GSUPEM_change_connhdlr:{?,?}) {}; |
| 315 | } |
| 316 | } |
| 317 | |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 318 | } |