blob: e35a87a8507496119e5b82dcba37698b24d10555 [file] [log] [blame]
Harald Welted27ab242019-07-26 13:45:18 +02001module 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 Yanitskiyb46f01e2021-12-06 03:23:13 +030021 * 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 Welted27ab242019-07-26 13:45:18 +020025 * (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
34import from DIAMETER_CodecPort all;
35import from DIAMETER_CodecPort_CtrlFunct all;
36import from DIAMETER_Types all;
37import from DIAMETER_Templates all;
38import from Osmocom_Types all;
39import from IPL4asp_Types all;
Harald Welte61f73d52020-04-26 21:41:12 +020040import from Native_Functions all;
Harald Welted27ab242019-07-26 13:45:18 +020041
42type hexstring IMSI;
43
Harald Welted01b5d02020-04-26 22:05:53 +020044/* notify the recipient that a Capability Exchange happened */
45type record DiameterCapabilityExchgInd {
46 PDU_DIAMETER rx,
47 PDU_DIAMETER tx
48};
49
Harald Welted27ab242019-07-26 13:45:18 +020050type 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 */
57type port DIAMETER_Conn_PT message {
Harald Welte0e038082019-08-18 19:38:54 +020058 inout PDU_DIAMETER;
Harald Welted27ab242019-07-26 13:45:18 +020059} with { extension "internal" };
60
61/* global test port e.g. for non-imsi/conn specific messages */
62type port DIAMETER_PT message {
Harald Welted01b5d02020-04-26 22:05:53 +020063 inout PDU_DIAMETER, DiameterCapabilityExchgInd;
Harald Welted27ab242019-07-26 13:45:18 +020064} with { extension "internal" };
65
66
67/* represents a single DIAMETER Association */
68type record AssociationData {
69 DIAMETER_ConnHdlr comp_ref,
70 hexstring imsi optional
71};
72
73type 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 */
81 var AssociationData SgsapAssociationTable[16];
82 /* pending expected CRCX */
83 var ExpectData DiameterExpectTable[8];
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
93type function DIAMETERCreateCallback(PDU_DIAMETER msg, hexstring imsi, charstring id)
94runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr;
95
96type function DIAMETERUnitdataCallback(PDU_DIAMETER msg)
97runs on DIAMETER_Emulation_CT return template PDU_DIAMETER;
98
99type record DIAMETEROps {
100 DIAMETERCreateCallback create_cb,
Vadim Yanitskiyb46f01e2021-12-06 03:23:13 +0300101 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 Welted27ab242019-07-26 13:45:18 +0200105}
106
107type record DIAMETER_conn_parameters {
108 HostName remote_ip,
109 PortNumber remote_sctp_port,
110 HostName local_ip,
Harald Welte61f73d52020-04-26 21:41:12 +0200111 PortNumber local_sctp_port,
112 charstring origin_host,
113 charstring origin_realm,
114 uint32_t vendor_app_id
Harald Welted27ab242019-07-26 13:45:18 +0200115}
116
117function tr_DIAMETER_RecvFrom_R(template PDU_DIAMETER msg)
118runs on DIAMETER_Emulation_CT return template DIAMETER_RecvFrom {
119 var template DIAMETER_RecvFrom mrf := {
120 connId := g_diameter_conn_id,
121 remName := ?,
122 remPort := ?,
123 locName := ?,
124 locPort := ?,
125 msg := msg
126 }
127 return mrf;
128}
129
130private function f_imsi_known(hexstring imsi)
131runs on DIAMETER_Emulation_CT return boolean {
132 var integer i;
133 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
134 if (SgsapAssociationTable[i].imsi == imsi) {
135 return true;
136 }
137 }
138 return false;
139}
140
141private function f_comp_known(DIAMETER_ConnHdlr client)
142runs on DIAMETER_Emulation_CT return boolean {
143 var integer i;
144 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
145 if (SgsapAssociationTable[i].comp_ref == client) {
146 return true;
147 }
148 }
149 return false;
150}
151
152private function f_comp_by_imsi(hexstring imsi)
153runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr {
154 var integer i;
155 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
156 if (SgsapAssociationTable[i].imsi == imsi) {
157 return SgsapAssociationTable[i].comp_ref;
158 }
159 }
160 setverdict(fail, "DIAMETER Association Table not found by IMSI", imsi);
161 mtc.stop;
162}
163
164private function f_imsi_by_comp(DIAMETER_ConnHdlr client)
165runs on DIAMETER_Emulation_CT return hexstring {
166 var integer i;
167 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
168 if (SgsapAssociationTable[i].comp_ref == client) {
169 return SgsapAssociationTable[i].imsi;
170 }
171 }
172 setverdict(fail, "DIAMETER Association Table not found by component ", client);
173 mtc.stop;
174}
175
176private function f_imsi_table_add(DIAMETER_ConnHdlr comp_ref, hexstring imsi)
177runs on DIAMETER_Emulation_CT {
178 var integer i;
179 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
180 if (not isvalue(SgsapAssociationTable[i].imsi)) {
181 SgsapAssociationTable[i].imsi := imsi;
182 SgsapAssociationTable[i].comp_ref := comp_ref;
183 return;
184 }
185 }
186 testcase.stop("DIAMETER Association Table full!");
187}
188
189private function f_imsi_table_del(DIAMETER_ConnHdlr comp_ref, hexstring imsi)
190runs on DIAMETER_Emulation_CT {
191 var integer i;
192 for (i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
193 if (SgsapAssociationTable[i].comp_ref == comp_ref and
194 SgsapAssociationTable[i].imsi == imsi) {
195 SgsapAssociationTable[i].imsi := omit;
196 SgsapAssociationTable[i].comp_ref := null;
197 return;
198 }
199 }
200 setverdict(fail, "DIAMETER Association Table: Couldn't find to-be-deleted entry!");
201 mtc.stop;
202}
203
204
205private function f_imsi_table_init()
206runs on DIAMETER_Emulation_CT {
207 for (var integer i := 0; i < sizeof(SgsapAssociationTable); i := i+1) {
208 SgsapAssociationTable[i].comp_ref := null;
209 SgsapAssociationTable[i].imsi := omit;
210 }
211}
212
213function f_DIAMETER_get_avp(PDU_DIAMETER pdu, template (present) AVP_Code avp_code)
214return template (omit) AVP
215{
216 var integer i;
217
218 for (i := 0; i < lengthof(pdu.avps); i := i+1) {
219 if (not ispresent(pdu.avps[i].avp)) {
220 continue;
221 }
222 var AVP_Header hdr := pdu.avps[i].avp.avp_header;
223 if (match(hdr.avp_code, avp_code)) {
224 return pdu.avps[i].avp;
225 }
226 }
227 return omit;
228}
229
230function f_DIAMETER_get_imsi(PDU_DIAMETER pdu) return template (omit) IMSI
231{
232 var template (omit) AVP imsi_avp;
233
234 imsi_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_BASE_NONE_User_Name);
235 if (istemplatekind(imsi_avp, "omit")) {
Harald Weltef9fb63e2020-04-26 18:07:19 +0200236 var template (omit) AVP sid_avp;
237 sid_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_DCC_NONE_Subscription_Id);
238 if (istemplatekind(sid_avp, "omit")) {
239 return omit;
240 }
241 var AVP_Grouped grp := valueof(sid_avp.avp_data.avp_DCC_NONE_Subscription_Id);
242 if (not match(grp[0], tr_SubcrIdType(END_USER_IMSI))) {
243 return omit;
244 }
245 return str2hex(oct2char(grp[1].avp.avp_data.avp_DCC_NONE_Subscription_Id_Data));
Harald Welted27ab242019-07-26 13:45:18 +0200246 } else {
247 var octetstring imsi_oct := valueof(imsi_avp.avp_data.avp_BASE_NONE_User_Name);
248 return str2hex(oct2char(imsi_oct));
249 }
250}
251
252private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := {
253 sinfo_stream := omit,
254 sinfo_ppid := ppid,
255 remSocks := omit,
256 assocId := omit
257};
258
259private template PortEvent tr_SctpAssocChange := {
260 sctpEvent := {
261 sctpAssocChange := ?
262 }
263}
264private template PortEvent tr_SctpPeerAddrChange := {
265 sctpEvent := {
266 sctpPeerAddrChange := ?
267 }
268}
269
270private function f_diameter_xceive(template (value) PDU_DIAMETER tx,
271 template PDU_DIAMETER rx_t := ?)
272runs on DIAMETER_Emulation_CT return PDU_DIAMETER {
273 timer T := 10.0;
274 var DIAMETER_RecvFrom mrf;
275
276 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, tx));
277 alt {
278 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(rx_t)) -> value mrf { }
279 [] DIAMETER.receive(tr_SctpAssocChange) { repeat; }
280 [] DIAMETER.receive(tr_SctpPeerAddrChange) { repeat; }
281 [] T.timeout {
282 setverdict(fail, "Timeout waiting for ", rx_t);
283 mtc.stop;
284 }
285 }
286 return mrf.msg;
287}
288
289function main(DIAMETEROps ops, DIAMETER_conn_parameters p, charstring id) runs on DIAMETER_Emulation_CT {
290 var Result res;
291 g_diameter_id := id;
292 f_imsi_table_init();
293 f_expect_table_init();
294
295 map(self:DIAMETER, system:DIAMETER_CODEC_PT);
296 if (p.remote_sctp_port == -1) {
297 res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_listen(DIAMETER, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
298 } else {
299 res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_connect(DIAMETER, p.remote_ip, p.remote_sctp_port,
300 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
301 }
302 if (not ispresent(res.connId)) {
303 setverdict(fail, "Could not connect DIAMETER socket, check your configuration");
304 mtc.stop;
305 }
306 g_diameter_conn_id := res.connId;
307
308 while (true) {
309 var DIAMETER_ConnHdlr vc_conn;
Harald Welted27ab242019-07-26 13:45:18 +0200310 var template IMSI imsi_t;
311 var hexstring imsi;
312 var DIAMETER_RecvFrom mrf;
313 var PDU_DIAMETER msg;
314 var charstring vlr_name, mme_name;
315 var PortEvent port_evt;
316
317 alt {
318 [] DIAMETER.receive(PortEvent:{connOpened := ?}) -> value port_evt {
319 g_diameter_conn_id := port_evt.connOpened.connId;
320 }
321 [] DIAMETER.receive(PortEvent:?) { }
322 /* DIAMETER from client */
323 [] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg sender vc_conn {
324 /* Pass message through */
325 /* TODO: check which ConnectionID client has allocated + store in table? */
326 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg));
327 }
328
329 /* handle CER/CEA handshake */
330 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIAMETER_R(cmd_code := Capabilities_Exchange))) -> value mrf {
331 var template (value) PDU_DIAMETER resp;
Harald Welte61f73d52020-04-26 21:41:12 +0200332 resp := ts_DIA_CEA(mrf.msg.hop_by_hop_id, mrf.msg.end_to_end_id, p.origin_host,
333 p.origin_realm, f_inet_addr(p.local_ip), p.vendor_app_id);
Harald Welted27ab242019-07-26 13:45:18 +0200334 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, resp));
Harald Welted01b5d02020-04-26 22:05:53 +0200335 /* notify our user that the CER->CEA exchange has happened */
336 DIAMETER_UNIT.send(DiameterCapabilityExchgInd:{rx:=mrf.msg, tx:=valueof(resp)});
Harald Welted27ab242019-07-26 13:45:18 +0200337 }
Vadim Yanitskiy3f7a6dc2021-12-11 04:13:59 +0300338 /* handle DWR/DWA ping-pong */
339 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIA_DWR)) -> value mrf {
340 var template (value) PDU_DIAMETER resp;
341 resp := ts_DIA_DWA('00000001'O, p.origin_host, p.origin_realm,
342 hbh_id := mrf.msg.hop_by_hop_id,
343 ete_id := mrf.msg.end_to_end_id);
344 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp)));
345 }
Harald Welted27ab242019-07-26 13:45:18 +0200346
Vadim Yanitskiyb46f01e2021-12-06 03:23:13 +0300347 /* DIAMETER from the test suite */
348 [ops.raw] DIAMETER_UNIT.receive(PDU_DIAMETER:?) -> value msg {
349 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg));
350 }
351 /* DIAMETER from remote peer (raw mode) */
352 [ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf {
353 DIAMETER_UNIT.send(mrf.msg);
354 }
355 /* DIAMETER from remote peer (IMSI based routing) */
356 [not ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf {
Harald Welted27ab242019-07-26 13:45:18 +0200357 imsi_t := f_DIAMETER_get_imsi(mrf.msg);
358 if (isvalue(imsi_t)) {
359 imsi := valueof(imsi_t);
360 if (f_imsi_known(imsi)) {
361 vc_conn := f_comp_by_imsi(imsi);
362 DIAMETER_CLIENT.send(mrf.msg) to vc_conn;
363 } else {
364 vc_conn := ops.create_cb.apply(mrf.msg, imsi, id);
365 f_imsi_table_add(vc_conn, imsi);
366 DIAMETER_CLIENT.send(mrf.msg) to vc_conn;
367 }
368 } else {
369 /* message contained no IMSI; is not IMSI-oriented */
370 var template PDU_DIAMETER resp := ops.unitdata_cb.apply(mrf.msg);
371 if (isvalue(resp)) {
372 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp)));
373 }
374 }
375 }
376 [] DIAMETER.receive(tr_SctpAssocChange) { }
377 [] DIAMETER.receive(tr_SctpPeerAddrChange) { }
378 [] DIAMETER_PROC.getcall(DIAMETEREM_register:{?,?}) -> param(imsi, vc_conn) {
379 f_create_expect(imsi, vc_conn);
380 DIAMETER_PROC.reply(DIAMETEREM_register:{imsi, vc_conn}) to vc_conn;
381 }
382
383 }
384
385 }
386}
387
388/* "Expect" Handling */
389
390type record ExpectData {
391 hexstring imsi optional,
392 DIAMETER_ConnHdlr vc_conn
393}
394
395signature DIAMETEREM_register(in hexstring imsi, in DIAMETER_ConnHdlr hdlr);
396
397type port DIAMETEREM_PROC_PT procedure {
398 inout DIAMETEREM_register;
399} with { extension "internal" };
400
401/* Function that can be used as create_cb and will usse the expect table */
402function ExpectedCreateCallback(PDU_DIAMETER msg, hexstring imsi, charstring id)
403runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr {
404 var DIAMETER_ConnHdlr ret := null;
405 var integer i;
406
407 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
408 if (not ispresent(DiameterExpectTable[i].imsi)) {
409 continue;
410 }
411 if (imsi == DiameterExpectTable[i].imsi) {
412 ret := DiameterExpectTable[i].vc_conn;
413 /* Release this entry */
414 DiameterExpectTable[i].imsi := omit;
415 DiameterExpectTable[i].vc_conn := null;
416 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
417 return ret;
418 }
419 }
420 setverdict(fail, "Couldn't find Expect for ", msg);
421 mtc.stop;
422}
423
424private function f_create_expect(hexstring imsi, DIAMETER_ConnHdlr hdlr)
425runs on DIAMETER_Emulation_CT {
426 var integer i;
427
428 /* Check an entry like this is not already presnt */
429 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
430 if (imsi == DiameterExpectTable[i].imsi) {
431 setverdict(fail, "IMSI already present", imsi);
432 mtc.stop;
433 }
434 }
435 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
436 if (not ispresent(DiameterExpectTable[i].imsi)) {
437 DiameterExpectTable[i].imsi := imsi;
438 DiameterExpectTable[i].vc_conn := hdlr;
439 log("Created Expect[", i, "] for ", imsi, " to be handled at ", hdlr);
440 return;
441 }
442 }
443 testcase.stop("No space left in DiameterExpectTable")
444}
445
446/* client/conn_hdlr side function to use procedure port to create expect in emulation */
447function f_diameter_expect(hexstring imsi) runs on DIAMETER_ConnHdlr {
448 DIAMETER_PROC.call(DIAMETEREM_register:{imsi, self}) {
449 [] DIAMETER_PROC.getreply(DIAMETEREM_register:{?,?}) {};
450 }
451}
452
453private function f_expect_table_init()
454runs on DIAMETER_Emulation_CT {
455 var integer i;
456 for (i := 0; i < sizeof(DiameterExpectTable); i := i + 1) {
457 DiameterExpectTable[i].imsi := omit;
458 }
459}
460
461function DummyUnitdataCallback(PDU_DIAMETER msg)
462runs on DIAMETER_Emulation_CT return template PDU_DIAMETER {
463 log("Ignoring DIAMETER ", msg);
464 return omit;
465}
466
467
Harald Welted01b5d02020-04-26 22:05:53 +0200468function f_diameter_wait_capability(DIAMETER_PT pt)
469{
470 /* Wait for the Capability Exchange with the DUT */
471 timer T := 10.0;
472 T.start;
473 alt {
474 [] pt.receive(DiameterCapabilityExchgInd:?) {}
475 [] pt.receive {
476 setverdict(fail, "Unexpected receive waiting for DiameterCapabilityExchgInd");
477 mtc.stop;
478 }
479 [] T.timeout {
480 setverdict(fail, "Timeout waiting for DiameterCapabilityExchgInd");
481 mtc.stop;
482 }
483 }
484}
485
486
Harald Welted27ab242019-07-26 13:45:18 +0200487}