blob: bc8e7c33526fb30ba7ed2027509fb1cae1ec9316 [file] [log] [blame]
Pau Espin Pedrolb54acca2021-11-22 20:35:44 +01001module Iuh_Emulation {
2
3/* Iuh Emulation, runs on top of Iuh_CodecPort. It multiplexes/demultiplexes
4 * HNBAP and RUA.
5 *
6 * The Iuh_Emulation.main() function processes Iuh primitives from the Iuh
7 * socket via the Iuh_CodecPort, and dispatches them to HNBAP/RUA ports.
8 *
9 * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
10 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
11 * All rights reserved.
12 *
13 * Released under the terms of GNU General Public License, Version 2 or
14 * (at your option) any later version.
15 *
16 * SPDX-License-Identifier: GPL-2.0-or-later
17 */
18
19import from Iuh_CodecPort all;
20import from Iuh_CodecPort_CtrlFunct all;
21import from HNBAP_Types all;
22import from HNBAP_Constants all;
23import from HNBAP_PDU_Contents all;
24import from HNBAP_PDU_Descriptions all;
25import from HNBAP_IEs all;
26import from HNBAP_Templates all;
27import from RUA_Types all;
28import from RUA_Constants all;
29import from RUA_PDU_Contents all;
30import from RUA_PDU_Descriptions all;
31import from RUA_IEs all;
32import from RUA_Templates all;
33import from Iuh_Types all;
34
35import from General_Types all;
36import from Misc_Helpers all;
37import from Osmocom_Types all;
38import from IPL4asp_Types all;
39import from DNS_Helpers all;
40
41type enumerated IUHEM_EventUpDown {
42 IUHEM_EVENT_DOWN,
43 IUHEM_EVENT_UP
44}
45
46/* an event indicating us whether or not a connection is physically up or down. */
47type union IUHEM_Event {
48 IUHEM_EventUpDown up_down
49}
50
51type port HNBAP_PT message {
52 inout HNBAP_PDU, IUHEM_Event;
53} with { extension "internal" };
54type port RUA_PT message {
55 inout RUA_PDU, IUHEM_Event;
56} with { extension "internal" };
57
58type component Iuh_Emulation_CT {
59 /* Port facing to the SCTP SUT */
60 port Iuh_CODEC_PT Iuh;
61 /* Port facing to user upper side stack: */
62 port HNBAP_PT HNBAP;
63 port RUA_PT RUA;
64
65 var Iuh_conn_parameters g_pars;
66 var charstring g_Iuh_id;
67 var integer g_self_conn_id := -1;
68 var IPL4asp_Types.ConnectionId g_last_conn_id := -1; /* server only */
69}
70
71type record Iuh_conn_parameters {
72 HostName remote_ip,
73 PortNumber remote_sctp_port,
74 HostName local_ip,
75 PortNumber local_sctp_port
76}
77
78function tr_Iuh_RecvFrom_R(template Iuh_PDU msg)
79runs on Iuh_Emulation_CT return template Iuh_RecvFrom {
80 var template Iuh_RecvFrom mrf := {
81 connId := ?,
82 remName := ?,
83 remPort := ?,
84 locName := ?,
85 locPort := ?,
86 msg := msg
87 }
88 return mrf;
89}
90
91private template (value) SctpTuple ts_SCTP(template (omit) integer ppid := omit) := {
92 sinfo_stream := omit,
93 sinfo_ppid := ppid,
94 remSocks := omit,
95 assocId := omit
96};
97
98private template PortEvent tr_SctpAssocChange := {
99 sctpEvent := {
100 sctpAssocChange := ?
101 }
102}
103private template PortEvent tr_SctpPeerAddrChange := {
104 sctpEvent := {
105 sctpPeerAddrChange := ?
106 }
107}
108
109private function emu_is_server() runs on Iuh_Emulation_CT return boolean {
110 return g_pars.remote_sctp_port == -1
111}
112
113/* Resolve TCP/IP connection identifier depending on server/client mode */
114private function f_iuh_conn_id() runs on Iuh_Emulation_CT
115return IPL4asp_Types.ConnectionId {
116 var IPL4asp_Types.ConnectionId conn_id;
117
118 if (not emu_is_server()) {
119 conn_id := g_self_conn_id;
120 } else {
121 conn_id := g_last_conn_id;
122 }
123
124 if (conn_id == -1) { /* Just to be sure */
125 f_shutdown(__FILE__, __LINE__, fail, "Connection is not established");
126 }
127
128 return conn_id;
129}
130
Pau Espin Pedrol066f46f2021-12-23 15:11:05 +0100131private function f_send_IUHEM_Event(template (value) IUHEM_Event evt) runs on Iuh_Emulation_CT {
132 if (HNBAP.checkstate("Connected")) {
133 HNBAP.send(evt);
134 }
135}
136
Pau Espin Pedrolb54acca2021-11-22 20:35:44 +0100137function main(Iuh_conn_parameters p, charstring id) runs on Iuh_Emulation_CT {
138 var Result res;
139 g_pars := p;
140 g_Iuh_id := id;
141
142 map(self:Iuh, system:Iuh_CODEC_PT);
143 if (emu_is_server()) {
144 res := Iuh_CodecPort_CtrlFunct.f_IPL4_listen(Iuh, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
145 } else {
146 res := Iuh_CodecPort_CtrlFunct.f_IPL4_connect(Iuh, p.remote_ip, p.remote_sctp_port,
147 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
148 }
149 if (not ispresent(res.connId)) {
150 f_shutdown(__FILE__, __LINE__, fail, "Could not connect Iuh socket, check your configuration");
151 }
152 g_self_conn_id := res.connId;
153
154 /* notify user about SCTP establishment */
155 if (p.remote_sctp_port != -1) {
Pau Espin Pedrol066f46f2021-12-23 15:11:05 +0100156 f_send_IUHEM_Event(IUHEM_Event:{up_down:=IUHEM_EVENT_UP});
Pau Espin Pedrolb54acca2021-11-22 20:35:44 +0100157 }
158
159 while (true) {
160 var Iuh_RecvFrom mrf;
161 var HNBAP_PDU hnbap_msg;
162 var RUA_PDU rua_msg;
163 var ASP_Event asp_evt;
164
165 alt {
166 /* HNBAP from client: pass on transparently */
167 [] HNBAP.receive(HNBAP_PDU:?) -> value hnbap_msg {
168 /* Pass message through */
169 Iuh.send(t_Iuh_Send_HNBAP(f_iuh_conn_id(), hnbap_msg));
170 }
171 /* RUA from client: pass on transparently */
172 [] RUA.receive(RUA_PDU:?) -> value rua_msg {
173 /* Pass message through */
174 Iuh.send(t_Iuh_Send_RUA(f_iuh_conn_id(), rua_msg));
175 }
176
177 /* Iuh received from peer (HNBGW or HnodeB) */
178 [] Iuh.receive(tr_Iuh_RecvFrom_R(?)) -> value mrf {
179 if (not match(mrf.connId, f_iuh_conn_id())) {
180 f_shutdown(__FILE__, __LINE__, fail, log2str("Received message from unexpected conn_id!", mrf));
181 }
182
183 if (match(mrf, t_Iuh_RecvFrom_HNBAP(?))) {
184 HNBAP.send(mrf.msg.hnbap);
185 } else if (match(mrf, t_Iuh_RecvFrom_RUA(?))) {
186 RUA.send(mrf.msg.rua);
187 } else {
188 /* TODO: special handling, as it contains multiple HNB connection ids */
189 f_shutdown(__FILE__, __LINE__, fail, log2str("UNEXPECTED MESSAGE RECEIVED!", mrf));
190 }
191 }
192 [] Iuh.receive(tr_SctpAssocChange) { }
193 [] Iuh.receive(tr_SctpPeerAddrChange) { }
194
195 /* server only */
196 [] Iuh.receive(ASP_Event:{connOpened:=?}) -> value asp_evt {
197 if (not emu_is_server()) {
198 f_shutdown(__FILE__, __LINE__, fail, log2str("Unexpected event receiver in client mode", asp_evt));
199 }
200 g_last_conn_id := asp_evt.connOpened.connId;
201 log("Established a new Iuh connection (conn_id=", g_last_conn_id, ")");
202
Pau Espin Pedrol066f46f2021-12-23 15:11:05 +0100203 f_send_IUHEM_Event(IUHEM_Event:{up_down:=IUHEM_EVENT_UP}); /* TODO: send g_last_conn_id */
Pau Espin Pedrolb54acca2021-11-22 20:35:44 +0100204 }
205
206 [] Iuh.receive(ASP_Event:{connClosed:=?}) -> value asp_evt {
207 log("Iuh: Closed");
208 g_self_conn_id := -1;
Pau Espin Pedrol066f46f2021-12-23 15:11:05 +0100209 f_send_IUHEM_Event(IUHEM_Event:{up_down:=IUHEM_EVENT_DOWN}); /* TODO: send asp_evt.connClosed.connId */
Pau Espin Pedrolb54acca2021-11-22 20:35:44 +0100210 if (not emu_is_server()) {
211 self.stop;
212 }
213 }
214 }
215 }
216}
217
218}