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 */ |
| 67 | var ExpectData GsupExpectTable[8]; |
| 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 | |
| 186 | [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_ID_ACK}) { repeat; } |
| 187 | [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_UP}) { repeat; } |
| 188 | [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_DOWN}) { |
| 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 | |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /*********************************************************************** |
| 230 | * "Expect" Handling (mapping for expected incoming GSUP calls from IUT) |
| 231 | ***********************************************************************/ |
| 232 | |
| 233 | /* data about an expected future incoming connection */ |
| 234 | type record ExpectData { |
| 235 | /* destination number based on which we can match it */ |
| 236 | charstring imsi optional, |
| 237 | /* component reference for this connection */ |
| 238 | GSUP_ConnHdlr vc_conn |
| 239 | } |
| 240 | |
| 241 | /* procedure based port to register for incoming calls */ |
| 242 | signature GSUPEM_register(in charstring imsi, in GSUP_ConnHdlr hdlr); |
| 243 | |
| 244 | type port GSUPEM_PROC_PT procedure { |
| 245 | inout GSUPEM_register; |
| 246 | } with { extension "internal" }; |
| 247 | |
| 248 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 249 | function ExpectedCreateCallback(GSUP_PDU gsup, charstring id) |
| 250 | runs on GSUP_Emulation_CT return GSUP_ConnHdlr { |
| 251 | var GSUP_ConnHdlr ret := null; |
| 252 | var charstring imsi; |
| 253 | var integer i; |
| 254 | |
| 255 | imsi := hex2str(gsup.ies[0].val.imsi); |
| 256 | |
| 257 | for (i := 0; i < sizeof(GsupExpectTable); i:= i+1) { |
| 258 | if (not ispresent(GsupExpectTable[i].imsi)) { |
| 259 | continue; |
| 260 | } |
| 261 | if (imsi == GsupExpectTable[i].imsi) { |
| 262 | ret := GsupExpectTable[i].vc_conn; |
| 263 | /* release this entry to be used again */ |
| 264 | GsupExpectTable[i].imsi := omit; |
| 265 | GsupExpectTable[i].vc_conn := null; |
| 266 | log("Found GsupExpect[", i, "] for ", imsi, " handled at ", ret); |
| 267 | /* return the component reference */ |
| 268 | return ret; |
| 269 | } |
| 270 | } |
| 271 | setverdict(fail, "Couldn't find GsupExpect for incoming imsi ", imsi); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 272 | mtc.stop; |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | /* server/emulation side function to create expect */ |
| 277 | private function f_create_expect(charstring imsi, GSUP_ConnHdlr hdlr) |
| 278 | runs on GSUP_Emulation_CT { |
| 279 | var integer i; |
| 280 | for (i := 0; i < sizeof(GsupExpectTable); i := i+1) { |
| 281 | if (not ispresent(GsupExpectTable[i].imsi)) { |
| 282 | GsupExpectTable[i].imsi := imsi; |
| 283 | GsupExpectTable[i].vc_conn := hdlr; |
| 284 | log("Created GsupExpect[", i, "] for ", imsi, " to be handled at ", hdlr); |
| 285 | return; |
| 286 | } |
| 287 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 288 | testcase.stop("No space left in GsupExpectTable"); |
Harald Welte | 0e5aad2 | 2018-01-21 14:00:41 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 292 | function f_create_gsup_expect(charstring imsi) runs on GSUP_ConnHdlr { |
| 293 | GSUP_PROC.call(GSUPEM_register:{imsi, self}) { |
| 294 | [] GSUP_PROC.getreply(GSUPEM_register:{?,?}) {}; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | } |