blob: 0f1b9fead83e07f0f56aeb0e8c891d8eec5c15be [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 */
Pau Espin Pedrole4361702022-04-11 11:59:40 +020081 var AssociationData DiameterAssocTable[16];
Harald Welted27ab242019-07-26 13:45:18 +020082 /* 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,
Pau Espin Pedrol33b47492022-03-08 17:43:01 +0100114 uint32_t auth_app_id optional,
115 uint32_t vendor_app_id optional
Harald Welted27ab242019-07-26 13:45:18 +0200116}
117
118function tr_DIAMETER_RecvFrom_R(template PDU_DIAMETER msg)
119runs 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
131private function f_imsi_known(hexstring imsi)
132runs on DIAMETER_Emulation_CT return boolean {
133 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200134 for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) {
135 if (DiameterAssocTable[i].imsi == imsi) {
Harald Welted27ab242019-07-26 13:45:18 +0200136 return true;
137 }
138 }
139 return false;
140}
141
142private function f_comp_known(DIAMETER_ConnHdlr client)
143runs on DIAMETER_Emulation_CT return boolean {
144 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200145 for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) {
146 if (DiameterAssocTable[i].comp_ref == client) {
Harald Welted27ab242019-07-26 13:45:18 +0200147 return true;
148 }
149 }
150 return false;
151}
152
153private function f_comp_by_imsi(hexstring imsi)
154runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr {
155 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200156 for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) {
157 if (DiameterAssocTable[i].imsi == imsi) {
158 return DiameterAssocTable[i].comp_ref;
Harald Welted27ab242019-07-26 13:45:18 +0200159 }
160 }
161 setverdict(fail, "DIAMETER Association Table not found by IMSI", imsi);
162 mtc.stop;
163}
164
165private function f_imsi_by_comp(DIAMETER_ConnHdlr client)
166runs on DIAMETER_Emulation_CT return hexstring {
167 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200168 for (i := 0; i < sizeof(DiameterAssocTable); i := i+1) {
169 if (DiameterAssocTable[i].comp_ref == client) {
170 return DiameterAssocTable[i].imsi;
Harald Welted27ab242019-07-26 13:45:18 +0200171 }
172 }
173 setverdict(fail, "DIAMETER Association Table not found by component ", client);
174 mtc.stop;
175}
176
177private function f_imsi_table_add(DIAMETER_ConnHdlr comp_ref, hexstring imsi)
178runs on DIAMETER_Emulation_CT {
179 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200180 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 Welted27ab242019-07-26 13:45:18 +0200184 return;
185 }
186 }
187 testcase.stop("DIAMETER Association Table full!");
188}
189
190private function f_imsi_table_del(DIAMETER_ConnHdlr comp_ref, hexstring imsi)
191runs on DIAMETER_Emulation_CT {
192 var integer i;
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200193 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 Welted27ab242019-07-26 13:45:18 +0200198 return;
199 }
200 }
201 setverdict(fail, "DIAMETER Association Table: Couldn't find to-be-deleted entry!");
202 mtc.stop;
203}
204
205
206private function f_imsi_table_init()
207runs on DIAMETER_Emulation_CT {
Pau Espin Pedrole4361702022-04-11 11:59:40 +0200208 for (var integer i := 0; i < sizeof(DiameterAssocTable); i := i+1) {
209 DiameterAssocTable[i].comp_ref := null;
210 DiameterAssocTable[i].imsi := omit;
Harald Welted27ab242019-07-26 13:45:18 +0200211 }
212}
213
214function f_DIAMETER_get_avp(PDU_DIAMETER pdu, template (present) AVP_Code avp_code)
215return template (omit) AVP
216{
217 var integer i;
218
219 for (i := 0; i < lengthof(pdu.avps); i := i+1) {
220 if (not ispresent(pdu.avps[i].avp)) {
221 continue;
222 }
223 var AVP_Header hdr := pdu.avps[i].avp.avp_header;
224 if (match(hdr.avp_code, avp_code)) {
225 return pdu.avps[i].avp;
226 }
227 }
228 return omit;
229}
230
231function f_DIAMETER_get_imsi(PDU_DIAMETER pdu) return template (omit) IMSI
232{
233 var template (omit) AVP imsi_avp;
234
235 imsi_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_BASE_NONE_User_Name);
236 if (istemplatekind(imsi_avp, "omit")) {
Harald Weltef9fb63e2020-04-26 18:07:19 +0200237 var template (omit) AVP sid_avp;
238 sid_avp := f_DIAMETER_get_avp(pdu, c_AVP_Code_DCC_NONE_Subscription_Id);
239 if (istemplatekind(sid_avp, "omit")) {
240 return omit;
241 }
242 var AVP_Grouped grp := valueof(sid_avp.avp_data.avp_DCC_NONE_Subscription_Id);
243 if (not match(grp[0], tr_SubcrIdType(END_USER_IMSI))) {
244 return omit;
245 }
246 return str2hex(oct2char(grp[1].avp.avp_data.avp_DCC_NONE_Subscription_Id_Data));
Harald Welted27ab242019-07-26 13:45:18 +0200247 } else {
248 var octetstring imsi_oct := valueof(imsi_avp.avp_data.avp_BASE_NONE_User_Name);
249 return str2hex(oct2char(imsi_oct));
250 }
251}
252
253private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := {
254 sinfo_stream := omit,
255 sinfo_ppid := ppid,
256 remSocks := omit,
257 assocId := omit
258};
259
260private template PortEvent tr_SctpAssocChange := {
261 sctpEvent := {
262 sctpAssocChange := ?
263 }
264}
265private template PortEvent tr_SctpPeerAddrChange := {
266 sctpEvent := {
267 sctpPeerAddrChange := ?
268 }
269}
270
271private function f_diameter_xceive(template (value) PDU_DIAMETER tx,
272 template PDU_DIAMETER rx_t := ?)
273runs on DIAMETER_Emulation_CT return PDU_DIAMETER {
274 timer T := 10.0;
275 var DIAMETER_RecvFrom mrf;
276
277 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, tx));
278 alt {
279 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(rx_t)) -> value mrf { }
280 [] DIAMETER.receive(tr_SctpAssocChange) { repeat; }
281 [] DIAMETER.receive(tr_SctpPeerAddrChange) { repeat; }
282 [] T.timeout {
283 setverdict(fail, "Timeout waiting for ", rx_t);
284 mtc.stop;
285 }
286 }
287 return mrf.msg;
288}
289
290function main(DIAMETEROps ops, DIAMETER_conn_parameters p, charstring id) runs on DIAMETER_Emulation_CT {
291 var Result res;
292 g_diameter_id := id;
293 f_imsi_table_init();
294 f_expect_table_init();
295
296 map(self:DIAMETER, system:DIAMETER_CODEC_PT);
297 if (p.remote_sctp_port == -1) {
298 res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_listen(DIAMETER, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
299 } else {
300 res := DIAMETER_CodecPort_CtrlFunct.f_IPL4_connect(DIAMETER, p.remote_ip, p.remote_sctp_port,
301 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
302 }
303 if (not ispresent(res.connId)) {
304 setverdict(fail, "Could not connect DIAMETER socket, check your configuration");
305 mtc.stop;
306 }
307 g_diameter_conn_id := res.connId;
308
309 while (true) {
310 var DIAMETER_ConnHdlr vc_conn;
Harald Welted27ab242019-07-26 13:45:18 +0200311 var template IMSI imsi_t;
312 var hexstring imsi;
313 var DIAMETER_RecvFrom mrf;
314 var PDU_DIAMETER msg;
315 var charstring vlr_name, mme_name;
316 var PortEvent port_evt;
317
318 alt {
319 [] DIAMETER.receive(PortEvent:{connOpened := ?}) -> value port_evt {
320 g_diameter_conn_id := port_evt.connOpened.connId;
321 }
322 [] DIAMETER.receive(PortEvent:?) { }
323 /* DIAMETER from client */
324 [] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg sender vc_conn {
325 /* Pass message through */
326 /* TODO: check which ConnectionID client has allocated + store in table? */
327 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg));
328 }
329
330 /* handle CER/CEA handshake */
331 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIAMETER_R(cmd_code := Capabilities_Exchange))) -> value mrf {
332 var template (value) PDU_DIAMETER resp;
Pau Espin Pedrol33b47492022-03-08 17:43:01 +0100333 resp := f_ts_DIA_CEA(mrf.msg.hop_by_hop_id, mrf.msg.end_to_end_id, p.origin_host,
334 p.origin_realm, f_inet_addr(p.local_ip), p.auth_app_id, p.vendor_app_id);
Harald Welted27ab242019-07-26 13:45:18 +0200335 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, resp));
Harald Welted01b5d02020-04-26 22:05:53 +0200336 /* notify our user that the CER->CEA exchange has happened */
337 DIAMETER_UNIT.send(DiameterCapabilityExchgInd:{rx:=mrf.msg, tx:=valueof(resp)});
Harald Welted27ab242019-07-26 13:45:18 +0200338 }
Vadim Yanitskiy3f7a6dc2021-12-11 04:13:59 +0300339 /* handle DWR/DWA ping-pong */
340 [] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(tr_DIA_DWR)) -> value mrf {
341 var template (value) PDU_DIAMETER resp;
342 resp := ts_DIA_DWA('00000001'O, p.origin_host, p.origin_realm,
343 hbh_id := mrf.msg.hop_by_hop_id,
344 ete_id := mrf.msg.end_to_end_id);
345 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp)));
346 }
Harald Welted27ab242019-07-26 13:45:18 +0200347
Vadim Yanitskiyb46f01e2021-12-06 03:23:13 +0300348 /* DIAMETER from the test suite */
349 [ops.raw] DIAMETER_UNIT.receive(PDU_DIAMETER:?) -> value msg {
350 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, msg));
351 }
352 /* DIAMETER from remote peer (raw mode) */
353 [ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf {
354 DIAMETER_UNIT.send(mrf.msg);
355 }
356 /* DIAMETER from remote peer (IMSI based routing) */
357 [not ops.raw] DIAMETER.receive(tr_DIAMETER_RecvFrom_R(?)) -> value mrf {
Harald Welted27ab242019-07-26 13:45:18 +0200358 imsi_t := f_DIAMETER_get_imsi(mrf.msg);
359 if (isvalue(imsi_t)) {
360 imsi := valueof(imsi_t);
361 if (f_imsi_known(imsi)) {
362 vc_conn := f_comp_by_imsi(imsi);
363 DIAMETER_CLIENT.send(mrf.msg) to vc_conn;
364 } else {
365 vc_conn := ops.create_cb.apply(mrf.msg, imsi, id);
366 f_imsi_table_add(vc_conn, imsi);
367 DIAMETER_CLIENT.send(mrf.msg) to vc_conn;
368 }
369 } else {
370 /* message contained no IMSI; is not IMSI-oriented */
371 var template PDU_DIAMETER resp := ops.unitdata_cb.apply(mrf.msg);
372 if (isvalue(resp)) {
373 DIAMETER.send(t_DIAMETER_Send(g_diameter_conn_id, valueof(resp)));
374 }
375 }
376 }
377 [] DIAMETER.receive(tr_SctpAssocChange) { }
378 [] DIAMETER.receive(tr_SctpPeerAddrChange) { }
379 [] DIAMETER_PROC.getcall(DIAMETEREM_register:{?,?}) -> param(imsi, vc_conn) {
380 f_create_expect(imsi, vc_conn);
381 DIAMETER_PROC.reply(DIAMETEREM_register:{imsi, vc_conn}) to vc_conn;
382 }
383
384 }
385
386 }
387}
388
389/* "Expect" Handling */
390
391type record ExpectData {
392 hexstring imsi optional,
393 DIAMETER_ConnHdlr vc_conn
394}
395
396signature DIAMETEREM_register(in hexstring imsi, in DIAMETER_ConnHdlr hdlr);
397
398type port DIAMETEREM_PROC_PT procedure {
399 inout DIAMETEREM_register;
400} with { extension "internal" };
401
Pau Espin Pedrol69730a22022-04-11 12:00:53 +0200402/* Function that can be used as create_cb and will use the expect table */
Harald Welted27ab242019-07-26 13:45:18 +0200403function ExpectedCreateCallback(PDU_DIAMETER msg, hexstring imsi, charstring id)
404runs on DIAMETER_Emulation_CT return DIAMETER_ConnHdlr {
405 var DIAMETER_ConnHdlr ret := null;
406 var integer i;
407
408 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
409 if (not ispresent(DiameterExpectTable[i].imsi)) {
410 continue;
411 }
412 if (imsi == DiameterExpectTable[i].imsi) {
413 ret := DiameterExpectTable[i].vc_conn;
414 /* Release this entry */
415 DiameterExpectTable[i].imsi := omit;
416 DiameterExpectTable[i].vc_conn := null;
417 log("Found Expect[", i, "] for ", msg, " handled at ", ret);
418 return ret;
419 }
420 }
421 setverdict(fail, "Couldn't find Expect for ", msg);
422 mtc.stop;
423}
424
425private function f_create_expect(hexstring imsi, DIAMETER_ConnHdlr hdlr)
426runs on DIAMETER_Emulation_CT {
427 var integer i;
428
429 /* Check an entry like this is not already presnt */
430 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
431 if (imsi == DiameterExpectTable[i].imsi) {
432 setverdict(fail, "IMSI already present", imsi);
433 mtc.stop;
434 }
435 }
436 for (i := 0; i < sizeof(DiameterExpectTable); i := i+1) {
437 if (not ispresent(DiameterExpectTable[i].imsi)) {
438 DiameterExpectTable[i].imsi := imsi;
439 DiameterExpectTable[i].vc_conn := hdlr;
440 log("Created Expect[", i, "] for ", imsi, " to be handled at ", hdlr);
441 return;
442 }
443 }
444 testcase.stop("No space left in DiameterExpectTable")
445}
446
447/* client/conn_hdlr side function to use procedure port to create expect in emulation */
448function f_diameter_expect(hexstring imsi) runs on DIAMETER_ConnHdlr {
449 DIAMETER_PROC.call(DIAMETEREM_register:{imsi, self}) {
450 [] DIAMETER_PROC.getreply(DIAMETEREM_register:{?,?}) {};
451 }
452}
453
454private function f_expect_table_init()
455runs on DIAMETER_Emulation_CT {
456 var integer i;
457 for (i := 0; i < sizeof(DiameterExpectTable); i := i + 1) {
458 DiameterExpectTable[i].imsi := omit;
459 }
460}
461
462function DummyUnitdataCallback(PDU_DIAMETER msg)
463runs on DIAMETER_Emulation_CT return template PDU_DIAMETER {
464 log("Ignoring DIAMETER ", msg);
465 return omit;
466}
467
468
Harald Welted01b5d02020-04-26 22:05:53 +0200469function f_diameter_wait_capability(DIAMETER_PT pt)
470{
471 /* Wait for the Capability Exchange with the DUT */
472 timer T := 10.0;
473 T.start;
474 alt {
475 [] pt.receive(DiameterCapabilityExchgInd:?) {}
476 [] pt.receive {
477 setverdict(fail, "Unexpected receive waiting for DiameterCapabilityExchgInd");
478 mtc.stop;
479 }
480 [] T.timeout {
481 setverdict(fail, "Timeout waiting for DiameterCapabilityExchgInd");
482 mtc.stop;
483 }
484 }
485}
486
487
Harald Welted27ab242019-07-26 13:45:18 +0200488}