blob: 75a95ba83f197eadc31975935bb011449ae4ab57 [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
131function main(Iuh_conn_parameters p, charstring id) runs on Iuh_Emulation_CT {
132 var Result res;
133 g_pars := p;
134 g_Iuh_id := id;
135
136 map(self:Iuh, system:Iuh_CODEC_PT);
137 if (emu_is_server()) {
138 res := Iuh_CodecPort_CtrlFunct.f_IPL4_listen(Iuh, p.local_ip, p.local_sctp_port, { sctp := valueof(ts_SCTP) });
139 } else {
140 res := Iuh_CodecPort_CtrlFunct.f_IPL4_connect(Iuh, p.remote_ip, p.remote_sctp_port,
141 p.local_ip, p.local_sctp_port, -1, { sctp := valueof(ts_SCTP) });
142 }
143 if (not ispresent(res.connId)) {
144 f_shutdown(__FILE__, __LINE__, fail, "Could not connect Iuh socket, check your configuration");
145 }
146 g_self_conn_id := res.connId;
147
148 /* notify user about SCTP establishment */
149 if (p.remote_sctp_port != -1) {
150 HNBAP.send(IUHEM_Event:{up_down:=IUHEM_EVENT_UP});
151 }
152
153 while (true) {
154 var Iuh_RecvFrom mrf;
155 var HNBAP_PDU hnbap_msg;
156 var RUA_PDU rua_msg;
157 var ASP_Event asp_evt;
158
159 alt {
160 /* HNBAP from client: pass on transparently */
161 [] HNBAP.receive(HNBAP_PDU:?) -> value hnbap_msg {
162 /* Pass message through */
163 Iuh.send(t_Iuh_Send_HNBAP(f_iuh_conn_id(), hnbap_msg));
164 }
165 /* RUA from client: pass on transparently */
166 [] RUA.receive(RUA_PDU:?) -> value rua_msg {
167 /* Pass message through */
168 Iuh.send(t_Iuh_Send_RUA(f_iuh_conn_id(), rua_msg));
169 }
170
171 /* Iuh received from peer (HNBGW or HnodeB) */
172 [] Iuh.receive(tr_Iuh_RecvFrom_R(?)) -> value mrf {
173 if (not match(mrf.connId, f_iuh_conn_id())) {
174 f_shutdown(__FILE__, __LINE__, fail, log2str("Received message from unexpected conn_id!", mrf));
175 }
176
177 if (match(mrf, t_Iuh_RecvFrom_HNBAP(?))) {
178 HNBAP.send(mrf.msg.hnbap);
179 } else if (match(mrf, t_Iuh_RecvFrom_RUA(?))) {
180 RUA.send(mrf.msg.rua);
181 } else {
182 /* TODO: special handling, as it contains multiple HNB connection ids */
183 f_shutdown(__FILE__, __LINE__, fail, log2str("UNEXPECTED MESSAGE RECEIVED!", mrf));
184 }
185 }
186 [] Iuh.receive(tr_SctpAssocChange) { }
187 [] Iuh.receive(tr_SctpPeerAddrChange) { }
188
189 /* server only */
190 [] Iuh.receive(ASP_Event:{connOpened:=?}) -> value asp_evt {
191 if (not emu_is_server()) {
192 f_shutdown(__FILE__, __LINE__, fail, log2str("Unexpected event receiver in client mode", asp_evt));
193 }
194 g_last_conn_id := asp_evt.connOpened.connId;
195 log("Established a new Iuh connection (conn_id=", g_last_conn_id, ")");
196
197 HNBAP.send(IUHEM_Event:{up_down:=IUHEM_EVENT_UP}); /* TODO: send g_last_conn_id */
198 }
199
200 [] Iuh.receive(ASP_Event:{connClosed:=?}) -> value asp_evt {
201 log("Iuh: Closed");
202 g_self_conn_id := -1;
203 HNBAP.send(IUHEM_Event:{up_down:=IUHEM_EVENT_DOWN}); /* TODO: send asp_evt.connClosed.connId */
204 if (not emu_is_server()) {
205 self.stop;
206 }
207 }
208 }
209 }
210}
211
212}