blob: e1a515cff606bb55ca0b7e32d68968768fd8a7fc [file] [log] [blame]
Harald Welte0e5aad22018-01-21 14:00:41 +01001module 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.
27 */
28
29
30import from Osmocom_Types all;
31import from GSUP_Types all;
32import from IPA_Emulation all;
33
34/* General "base class" component definition, of which specific implementations
35 * derive themselves by means of the "extends" feature */
36type component GSUP_ConnHdlr {
37 /* ports towards GSUP Emulator core / call dispatchar */
38 port GSUP_Conn_PT GSUP;
39 port GSUPEM_PROC_PT GSUP_PROC;
40}
41
42/* port between individual per-connection components and this dispatcher */
43type port GSUP_Conn_PT message {
44 inout GSUP_PDU;
45} with { extension "internal" };
46
47
48/* represents a single GSUP call */
49type record ConnectionData {
50 /* reference to the instance of the per-connection component */
51 GSUP_ConnHdlr comp_ref,
52 charstring imsi
53}
54
55type component GSUP_Emulation_CT {
56 /* UNIX DOMAIN socket on the bottom side, using primitives */
57 port IPA_GSUP_PT GSUP;
58 /* GSUP port to the per-connection clients */
59 port GSUP_Conn_PT GSUP_CLIENT;
60
61 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
62 var ConnectionData GsupImsiTable[16];
63
64 /* pending expected incoming connections */
65 var ExpectData GsupExpectTable[8];
66 /* procedure based port to register for incoming connections */
67 port GSUPEM_PROC_PT GSUP_PROC;
68};
69
70private function f_imsi_known(charstring imsi)
71runs on GSUP_Emulation_CT return boolean {
72 var integer i;
73 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
74 if (GsupImsiTable[i].imsi == imsi) {
75 return true;
76 }
77 }
78 return false;
79}
80
81private function f_comp_known(GSUP_ConnHdlr client)
82runs on GSUP_Emulation_CT return boolean {
83 var integer i;
84 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
85 if (GsupImsiTable[i].comp_ref == client) {
86 return true;
87 }
88 }
89 return false;
90}
91
92/* resolve component reference by connection ID */
93private function f_comp_by_imsi(charstring imsi)
94runs on GSUP_Emulation_CT return GSUP_ConnHdlr {
95 var integer i;
96 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
97 if (GsupImsiTable[i].imsi == imsi) {
98 return GsupImsiTable[i].comp_ref;
99 }
100 }
101 log("GSUP IMSI table not found by IMSI ", imsi);
102 setverdict(fail);
103 self.stop;
104}
105
106/* resolve connection ID by component reference */
107private function f_imsi_by_comp(GSUP_ConnHdlr client)
108runs on GSUP_Emulation_CT return charstring {
109 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
110 if (GsupImsiTable[i].comp_ref == client) {
111 return GsupImsiTable[i].imsi;
112 }
113 }
114 log("GSUP IMSI table not found by component ", client);
115 setverdict(fail);
116 self.stop;
117}
118
119private function f_imsi_table_init()
120runs 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
127private function f_imsi_table_add(GSUP_ConnHdlr comp_ref, charstring imsi)
128runs on GSUP_Emulation_CT {
129 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
130 if (GsupImsiTable[i].imsi == "") {
131 GsupImsiTable[i].comp_ref := comp_ref;
132 GsupImsiTable[i].imsi := imsi;
133 log("Added IMSI table entry ", i, comp_ref, imsi);
134 return;
135 }
136 }
137 log("GSUP IMSI table full!");
138 setverdict(fail);
139 self.stop;
140}
141
142private function f_imsi_table_del(charstring imsi)
143runs on GSUP_Emulation_CT {
144 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
145 if (GsupImsiTable[i].imsi == imsi) {
146 log("Deleted GSUP IMSI table entry ", i,
147 GsupImsiTable[i].comp_ref, imsi);
148 GsupImsiTable[i].imsi := "";
149 GsupImsiTable[i].comp_ref := null;
150 return
151 }
152 }
153 log("GSUP IMSI table attempt to delete non-existant ", imsi);
154 setverdict(fail);
155 self.stop;
156}
157
158
159/* call-back type, to be provided by specific implementation; called when new SCCP connection
160 * arrives */
161type function GsupCreateCallback(GSUP_PDU gsup, charstring id)
162runs on GSUP_Emulation_CT return GSUP_ConnHdlr;
163
164type record GsupOps {
165 GsupCreateCallback create_cb
166}
167
168function main(GsupOps ops, charstring id) runs on GSUP_Emulation_CT {
169
170 f_imsi_table_init();
171
172 while (true) {
173 var GSUP_ConnHdlr vc_conn;
174 var GSUP_ConnHdlr vc_hdlr;
175 var GSUP_PDU gsup;
176 var charstring imsi;
177
178 alt {
179
180 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_ID_ACK}) { repeat; }
181 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_UP}) { repeat; }
182 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_DOWN}) {
183 setverdict(fail, "GSUP Connection Lost");
184 self.stop;
185 }
186
187 /* GSUP -> Client: call related messages */
188 [] GSUP.receive(GSUP_PDU:?) -> value gsup {
189 imsi := hex2str(gsup.ies[0].val.imsi);
190
191 if (f_imsi_known(imsi)) {
192 vc_conn := f_comp_by_imsi(imsi);
193 GSUP_CLIENT.send(gsup) to vc_conn;
194 } else {
195 /* TODO: Only accept this for SETUP.req? */
196 vc_conn := ops.create_cb.apply(gsup, id)
197 /* store mapping between client components and SCCP connectionId */
198 f_imsi_table_add(vc_conn, imsi);
199 /* handle user payload */
200 GSUP_CLIENT.send(gsup) to vc_conn;
201 }
202 }
203
204 [] GSUP.receive { repeat; }
205
206 /* Client -> GSUP Socket: Normal message */
207 [] GSUP_CLIENT.receive(GSUP_PDU:?) -> value gsup sender vc_conn {
208 /* forward to GSUP socket */
209 GSUP.send(gsup);
210 }
211
212
213 /* Client -> us: procedure call to register expect */
214 [] GSUP_PROC.getcall(GSUPEM_register:{?,?}) -> param(imsi, vc_hdlr) {
215 f_create_expect(imsi, vc_hdlr);
216 GSUP_PROC.reply(GSUPEM_register:{imsi, vc_hdlr});
217 }
218
219 }
220 }
221}
222
223/***********************************************************************
224 * "Expect" Handling (mapping for expected incoming GSUP calls from IUT)
225 ***********************************************************************/
226
227/* data about an expected future incoming connection */
228type record ExpectData {
229 /* destination number based on which we can match it */
230 charstring imsi optional,
231 /* component reference for this connection */
232 GSUP_ConnHdlr vc_conn
233}
234
235/* procedure based port to register for incoming calls */
236signature GSUPEM_register(in charstring imsi, in GSUP_ConnHdlr hdlr);
237
238type port GSUPEM_PROC_PT procedure {
239 inout GSUPEM_register;
240} with { extension "internal" };
241
242/* CreateCallback that can be used as create_cb and will use the expectation table */
243function ExpectedCreateCallback(GSUP_PDU gsup, charstring id)
244runs on GSUP_Emulation_CT return GSUP_ConnHdlr {
245 var GSUP_ConnHdlr ret := null;
246 var charstring imsi;
247 var integer i;
248
249 imsi := hex2str(gsup.ies[0].val.imsi);
250
251 for (i := 0; i < sizeof(GsupExpectTable); i:= i+1) {
252 if (not ispresent(GsupExpectTable[i].imsi)) {
253 continue;
254 }
255 if (imsi == GsupExpectTable[i].imsi) {
256 ret := GsupExpectTable[i].vc_conn;
257 /* release this entry to be used again */
258 GsupExpectTable[i].imsi := omit;
259 GsupExpectTable[i].vc_conn := null;
260 log("Found GsupExpect[", i, "] for ", imsi, " handled at ", ret);
261 /* return the component reference */
262 return ret;
263 }
264 }
265 setverdict(fail, "Couldn't find GsupExpect for incoming imsi ", imsi);
266 return ret;
267}
268
269/* server/emulation side function to create expect */
270private function f_create_expect(charstring imsi, GSUP_ConnHdlr hdlr)
271runs on GSUP_Emulation_CT {
272 var integer i;
273 for (i := 0; i < sizeof(GsupExpectTable); i := i+1) {
274 if (not ispresent(GsupExpectTable[i].imsi)) {
275 GsupExpectTable[i].imsi := imsi;
276 GsupExpectTable[i].vc_conn := hdlr;
277 log("Created GsupExpect[", i, "] for ", imsi, " to be handled at ", hdlr);
278 return;
279 }
280 }
281 setverdict(fail, "No space left in GsupExpectTable");
282}
283
284/* client/conn_hdlr side function to use procedure port to create expect in emulation */
285function f_create_gsup_expect(charstring imsi) runs on GSUP_ConnHdlr {
286 GSUP_PROC.call(GSUPEM_register:{imsi, self}) {
287 [] GSUP_PROC.getreply(GSUPEM_register:{?,?}) {};
288 }
289}
290
291}