Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 1 | module IPA_Emulation { |
| 2 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 3 | /* This module implements the IPA multiplex protocol on top of TCP, using the IPL4asp |
| 4 | * test-port as provider. It implements both client and server roles, as well was the CCM |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 5 | * handshake for establishing the identity of the client to the server. |
| 6 | * |
| 7 | * It already knows certain well-known sub-protocols such as A-bis RSL, MGCP and SCCP and |
| 8 | * GSUP. IT hence transcodes messages so the user can work with abstract data types rather |
| 9 | * than binary messages. It handles multiple packets inside one TCP segment. |
| 10 | * |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 11 | * (C) 2017-2018 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 12 | * All rights reserved. |
| 13 | * |
| 14 | * Released under the terms of GNU General Public License, Version 2 or |
| 15 | * (at your option) any later version. |
| 16 | */ |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 17 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 18 | import from IPA_Types all; |
| 19 | import from IPA_CodecPort all; |
| 20 | import from IPA_CodecPort_CtrlFunct all; |
| 21 | import from IPL4asp_Types all; |
Harald Welte | 1218883 | 2017-11-29 11:47:13 +0100 | [diff] [blame] | 22 | import from IPL4asp_PortType all; |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 23 | import from Socket_API_Definitions all; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 24 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 25 | #ifdef IPA_EMULATION_SCCP |
| 26 | import from MTP3asp_Types all; |
| 27 | import from MTP3asp_PortType all; |
| 28 | #endif |
| 29 | |
| 30 | #ifdef IPA_EMULATION_RSL |
| 31 | import from RSL_Types all; |
| 32 | #endif |
| 33 | |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 34 | #ifdef IPA_EMULATION_OML |
| 35 | import from AbisOML_Types all; |
| 36 | #endif |
| 37 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 38 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 39 | import from MGCP_Types all; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 43 | import from GSUP_Types all; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 44 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 45 | |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 46 | #ifdef IPA_EMULATION_RSPRO |
| 47 | import from RSPRO all; |
| 48 | import from RSPRO_Types all; |
| 49 | #endif |
| 50 | |
| 51 | |
Harald Welte | a76c4bb | 2017-12-09 02:06:07 +0100 | [diff] [blame] | 52 | import from Osmocom_CTRL_Types all; |
| 53 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 54 | modulepar { |
Harald Welte | 92632e1 | 2017-11-25 02:31:20 +0100 | [diff] [blame] | 55 | /* Use Osmocom extended IPA mux header */ |
| 56 | boolean mp_ipa_mgcp_uses_osmo_ext := true; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 57 | } |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 58 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 59 | type enumerated IpaMode { |
| 60 | IPA_MODE_CLIENT, |
| 61 | IPA_MODE_SERVER |
| 62 | } |
| 63 | |
Neels Hofmeyr | 3bf31d2 | 2018-08-24 14:44:32 +0200 | [diff] [blame] | 64 | type enumerated IpaInitBehavior { |
| 65 | IPA_INIT_SEND_IPA_ID_GET, |
| 66 | IPA_INIT_SEND_IPA_ID_ACK |
| 67 | } |
| 68 | |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 69 | type record ASP_IPA_Unitdata { |
| 70 | IpaStreamId streamId, |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 71 | IpaExtStreamId streamIdExt optional, |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 72 | octetstring payload |
| 73 | } |
| 74 | |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 75 | type enumerated ASP_IPA_EventUpDown { |
| 76 | ASP_IPA_EVENT_DOWN, |
| 77 | ASP_IPA_EVENT_UP, |
| 78 | ASP_IPA_EVENT_ID_ACK |
| 79 | } |
| 80 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 81 | /* an event indicating us whether or not a connection is physically up or down, |
| 82 | * and whether we have received an ID_ACK */ |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 83 | type union ASP_IPA_Event { |
| 84 | ASP_IPA_EventUpDown up_down |
| 85 | } |
| 86 | |
| 87 | template ASP_IPA_Event t_ASP_IPA_EVT_UD(ASP_IPA_EventUpDown ud) := { |
| 88 | up_down := ud |
| 89 | } |
| 90 | |
Harald Welte | 1dd8f37 | 2017-11-25 02:25:27 +0100 | [diff] [blame] | 91 | template ASP_IPA_Unitdata t_ASP_IPA_UD(IpaStreamId sid, octetstring pl, |
| 92 | template IpaExtStreamId esid := omit) := { |
| 93 | streamId := sid, |
| 94 | streamIdExt := esid, |
| 95 | payload := pl |
| 96 | } |
| 97 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 98 | #ifdef IPA_EMULATION_RSL |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 99 | /* like ASP_IPA_Unitdata, but with RSL_Message abstract type instead of octetstring */ |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 100 | type record ASP_RSL_Unitdata { |
| 101 | IpaStreamId streamId, |
| 102 | RSL_Message rsl |
| 103 | }; |
| 104 | |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 105 | template ASP_RSL_Unitdata ts_ASP_RSL_UD(IpaStreamId sid, template RSL_Message rsl) := { |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 106 | streamId := sid, |
| 107 | rsl := valueof(rsl) |
| 108 | } |
| 109 | |
Harald Welte | 7ae019e | 2017-12-09 00:54:15 +0100 | [diff] [blame] | 110 | template ASP_RSL_Unitdata tr_ASP_RSL_UD(IpaStreamId sid, template RSL_Message rsl) := { |
| 111 | streamId := sid, |
| 112 | rsl := rsl |
| 113 | } |
| 114 | |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 115 | template IpaStreamId t_IpaSidRSL := ( IPAC_PROTO_RSL_TRX0, IPAC_PROTO_RSL_TRX1, |
| 116 | IPAC_PROTO_RSL_TRX2, IPAC_PROTO_RSL_TRX3 ); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 117 | #endif |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 118 | |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 119 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 120 | /* Client port for general IPA messages, not further decoded */ |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 121 | type port IPA_SP_PT message { |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 122 | inout ASP_IPA_Unitdata, ASP_IPA_Event; |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 123 | } with { extension "internal" } |
| 124 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 125 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 126 | /* Client port for MGCP inside IPA */ |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 127 | type port IPA_MGCP_PT message { |
| 128 | inout MgcpCommand, MgcpResponse; |
| 129 | } with { extension "internal" } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 130 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 131 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 132 | #ifdef IPA_EMULATION_RSL |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 133 | /* Client port for A-bis RSL inside IPA */ |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 134 | type port IPA_RSL_PT message { |
| 135 | inout ASP_RSL_Unitdata, ASP_IPA_Event; |
| 136 | } with { extension "internal" } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 137 | #endif |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 138 | |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 139 | #ifdef IPA_EMULATION_OML |
| 140 | /* Client port for A-bis OML inside IPA */ |
| 141 | type port IPA_OML_PT message { |
| 142 | inout OML_PDU, octetstring, ASP_IPA_Event; |
| 143 | } with { extension "internal" } |
| 144 | #endif |
| 145 | |
Harald Welte | a76c4bb | 2017-12-09 02:06:07 +0100 | [diff] [blame] | 146 | /* Client port for CTRL inside IPA */ |
| 147 | type port IPA_CTRL_PT message { |
| 148 | inout CtrlMessage, ASP_IPA_Event; |
| 149 | } with { extension "internal" } |
| 150 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 151 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 152 | /* Client port for CTRL inside IPA */ |
| 153 | type port IPA_GSUP_PT message { |
| 154 | inout GSUP_PDU, ASP_IPA_Event; |
| 155 | } with { extension "internal" } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 156 | #endif |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 157 | |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 158 | #ifdef IPA_EMULATION_RSPRO |
| 159 | type port IPA_RSPRO_PT message { |
| 160 | inout RsproPDU, ASP_IPA_Event; |
| 161 | } with { extension "internal" } |
| 162 | #endif |
| 163 | |
| 164 | |
| 165 | |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 166 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 167 | type component IPA_Emulation_CT { |
| 168 | /* down-facing port to IPA codec port */ |
| 169 | port IPA_CODEC_PT IPA_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 170 | #ifdef IPA_EMULATION_SCCP |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 171 | /* up-facing port to SCCP */ |
| 172 | port MTP3asp_SP_PT MTP3_SP_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 173 | #endif |
| 174 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 175 | /* up-facing port for MGCP */ |
| 176 | port IPA_MGCP_PT IPA_MGCP_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 177 | #endif |
| 178 | #ifdef IPA_EMULATION_RSL |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 179 | /* up-facing port for RSL */ |
| 180 | port IPA_RSL_PT IPA_RSL_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 181 | #endif |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 182 | #ifdef IPA_EMULATION_OML |
| 183 | /* up-facing port for OML */ |
| 184 | port IPA_OML_PT IPA_OML_PORT; |
| 185 | #endif |
Harald Welte | a76c4bb | 2017-12-09 02:06:07 +0100 | [diff] [blame] | 186 | /* up-facing port for CTRL */ |
| 187 | port IPA_CTRL_PT IPA_CTRL_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 188 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 189 | /* up-facing port for GSUP */ |
| 190 | port IPA_GSUP_PT IPA_GSUP_PORT; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 191 | #endif |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 192 | #ifdef IPA_EMULATION_RSPRO |
| 193 | /* up-facing port for RSPRO */ |
| 194 | port IPA_RSPRO_PT IPA_RSPRO_PORT; |
| 195 | #endif |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 196 | |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 197 | /* up-facing port for other streams */ |
| 198 | port IPA_SP_PT IPA_SP_PORT; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 199 | |
| 200 | var boolean g_initialized := false; |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 201 | var IPL4asp_Types.ConnectionId g_ipa_conn_id := -1; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 202 | /* Are we a BSC/MGW (truel) or MSC (false) */ |
| 203 | var boolean g_is_bsc_mgw; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 204 | |
| 205 | var IpaMode g_mode; |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 206 | var boolean g_ccm_enabled; |
Neels Hofmeyr | 3bf31d2 | 2018-08-24 14:44:32 +0200 | [diff] [blame] | 207 | var IpaInitBehavior g_init_behavior; |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 208 | var IPA_CCM_Parameters g_ccm_pars := c_IPA_default_ccm_pars; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 211 | type record IPA_CCM_Parameters { |
| 212 | charstring ser_nr optional, |
| 213 | charstring name optional, |
| 214 | charstring location1 optional, |
| 215 | charstring location2 optional, |
| 216 | charstring equip_version optional, |
| 217 | charstring sw_version optional, |
| 218 | charstring ip_addr optional, |
| 219 | charstring mac_addr optional, |
| 220 | charstring unit_id optional, |
| 221 | charstring osmo_rand optional |
| 222 | } |
| 223 | |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 224 | const IPA_CCM_Parameters c_IPA_default_ccm_pars := { |
| 225 | ser_nr := "", |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 226 | name := "mahlzeit", |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 227 | location1 := "", |
| 228 | location2 := "", |
| 229 | equip_version := "", |
| 230 | sw_version := "", |
| 231 | ip_addr := "", |
| 232 | mac_addr := "", |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 233 | unit_id := "0/1/2", |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 234 | osmo_rand := "" |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 235 | }; |
| 236 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 237 | /* Function to use to connect as client to a remote IPA Server */ |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 238 | function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port, |
| 239 | charstring local_host, IPL4asp_Types.PortNumber local_port, |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 240 | IPA_CCM_Parameters ccm_pars := c_IPA_default_ccm_pars) runs on IPA_Emulation_CT { |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 241 | var IPL4asp_Types.Result res; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 242 | res := IPA_CodecPort_CtrlFunct.f_IPL4_connect(IPA_PORT, remote_host, remote_port, |
| 243 | local_host, local_port, 0, { tcp:={} }); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 244 | if (not ispresent(res.connId)) { |
Stefan Sperling | 6a90be4 | 2018-08-31 15:05:39 +0200 | [diff] [blame] | 245 | setverdict(fail, "Could not connect IPA socket from ", local_host, " port ", local_port, |
| 246 | " to ", remote_host, " port ", remote_port, "; check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 247 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 248 | } |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 249 | g_ipa_conn_id := res.connId; |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 250 | g_ccm_pars := ccm_pars; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 251 | g_is_bsc_mgw := true; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 254 | /* Function to use to bind to a local port as IPA server, accepting remote clients */ |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 255 | function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port, |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 256 | IPA_CCM_Parameters ccm_pars := c_IPA_default_ccm_pars) runs on IPA_Emulation_CT { |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 257 | var IPL4asp_Types.Result res; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 258 | res := IPA_CodecPort_CtrlFunct.f_IPL4_listen(IPA_PORT, |
| 259 | local_host, local_port, { tcp:={} }); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 260 | if (not ispresent(res.connId)) { |
Max | b7aae8b | 2019-03-11 15:22:02 +0100 | [diff] [blame^] | 261 | setverdict(fail, "Could not listen IPA socket ", local_host, ":", local_port, ", check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 262 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 263 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 264 | g_ipa_conn_id := res.connId; |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 265 | g_ccm_pars := ccm_pars; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 266 | g_is_bsc_mgw := false; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 269 | #ifdef IPA_EMULATION_SCCP |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 270 | template ASP_MTP3_TRANSFERind ts_MTP3_XFER_ind(integer opc, octetstring data) := { |
| 271 | sio := { '10'B, '00'B, '0011'B }, |
| 272 | opc := opc, |
| 273 | dpc := 0, |
| 274 | sls := 0, |
| 275 | data := data |
| 276 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 277 | #endif |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 278 | |
| 279 | |
| 280 | private template IpaCcmRespPart t_IdRespPart(IpaCcmIdTag tag, charstring payload) := { |
| 281 | len := 0, /* overwritten by codec */ |
| 282 | tag := tag, |
Harald Welte | 95686e0 | 2018-08-02 15:05:43 +0200 | [diff] [blame] | 283 | data := char2oct(payload) & '00'O |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 286 | private function f_send_IPA_EVT(template ASP_IPA_Event evt) runs on IPA_Emulation_CT { |
Harald Welte | 2e32e43 | 2018-05-24 20:00:00 +0200 | [diff] [blame] | 287 | if (IPA_SP_PORT.checkstate("Connected")) { |
| 288 | IPA_SP_PORT.send(evt); |
| 289 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 290 | #ifdef IPA_EMULATION_RSL |
Harald Welte | 5819b55 | 2017-12-09 02:55:46 +0100 | [diff] [blame] | 291 | if (IPA_RSL_PORT.checkstate("Connected")) { |
| 292 | IPA_RSL_PORT.send(evt); |
| 293 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 294 | #endif |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 295 | #ifdef IPA_EMULATION_OML |
| 296 | if (IPA_OML_PORT.checkstate("Connected")) { |
| 297 | IPA_OML_PORT.send(evt); |
| 298 | } |
| 299 | #endif |
Harald Welte | 5819b55 | 2017-12-09 02:55:46 +0100 | [diff] [blame] | 300 | if (IPA_CTRL_PORT.checkstate("Connected")) { |
| 301 | IPA_CTRL_PORT.send(evt); |
| 302 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 303 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 304 | if (IPA_GSUP_PORT.checkstate("Connected")) { |
| 305 | IPA_GSUP_PORT.send(evt); |
| 306 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 307 | #endif |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 308 | #ifdef IPA_EMULATION_RSPRO |
| 309 | if (IPA_RSPRO_PORT.checkstate("Connected")) { |
| 310 | IPA_RSPRO_PORT.send(evt); |
| 311 | } |
| 312 | #endif |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 313 | /* FIXME: to other ports */ |
| 314 | } |
| 315 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 316 | /* build IPA CCM ID RESP response from IPA CCM GET */ |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 317 | private function f_ccm_make_id_resp(PDU_IPA_CCM get) runs on IPA_Emulation_CT return PDU_IPA_CCM { |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 318 | var integer i; |
| 319 | var PDU_IPA_CCM resp := { |
| 320 | msg_type := IPAC_MSGT_ID_RESP, |
| 321 | u := { |
| 322 | resp := {} |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | for (i := 0; i < sizeof(get.u.get); i := i + 1) { |
| 327 | var IpaCcmIdTag tag := get.u.get[i].tag; |
| 328 | var charstring foo; |
| 329 | select (tag) { |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 330 | case (IPAC_IDTAG_SERNR) { |
| 331 | foo := g_ccm_pars.ser_nr; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 332 | } |
| 333 | case (IPAC_IDTAG_UNITNAME) { |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 334 | foo := g_ccm_pars.name; |
| 335 | } |
| 336 | case (IPAC_IDTAG_LOCATION1) { |
| 337 | foo := g_ccm_pars.location1; |
| 338 | } |
| 339 | case (IPAC_IDTAG_LOCATION2) { |
| 340 | foo := g_ccm_pars.location2; |
| 341 | } |
| 342 | case (IPAC_IDTAG_EQUIPVERS) { |
| 343 | foo := g_ccm_pars.equip_version; |
| 344 | } |
| 345 | case (IPAC_IDTAG_SWVERSION) { |
| 346 | foo := g_ccm_pars.sw_version; |
| 347 | } |
| 348 | case (IPAC_IDTAG_IPADDR) { |
| 349 | foo := g_ccm_pars.ip_addr; |
| 350 | } |
| 351 | case (IPAC_IDTAG_MACADDR) { |
| 352 | foo := g_ccm_pars.mac_addr; |
| 353 | } |
| 354 | case (IPAC_IDTAG_UNIT) { |
| 355 | foo := g_ccm_pars.unit_id; |
| 356 | } |
| 357 | case (IPAC_IDTAG_OSMO_RAND) { |
| 358 | foo := g_ccm_pars.osmo_rand; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 359 | } |
| 360 | case else { |
Harald Welte | e21096d | 2017-12-04 20:45:12 +0100 | [diff] [blame] | 361 | foo := "unknown"; |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | resp.u.resp[sizeof(resp.u.resp)] := valueof(t_IdRespPart(tag, foo)); |
| 365 | } |
| 366 | |
| 367 | return resp; |
| 368 | } |
| 369 | |
| 370 | /* transmit IPA CCM message */ |
| 371 | private function f_ccm_tx(PDU_IPA_CCM ccm) runs on IPA_Emulation_CT { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 372 | var IPA_Send ipa_tx := valueof(t_IPA_Send(g_ipa_conn_id, IPAC_PROTO_CCM, enc_PDU_IPA_CCM(ccm))); |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 373 | log("CCM Tx:", ccm); |
| 374 | IPA_PORT.send(ipa_tx); |
| 375 | } |
| 376 | |
| 377 | template PDU_IPA_CCM ts_IPA_PONG := { |
| 378 | msg_type := IPAC_MSGT_PONG, |
| 379 | u := omit |
| 380 | } |
| 381 | |
| 382 | template PDU_IPA_CCM ts_IPA_ACK := { |
| 383 | msg_type := IPAC_MSGT_ID_ACK, |
| 384 | u := omit |
| 385 | } |
| 386 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 387 | template PDU_IPA_CCM ts_IPA_ID_GET := { |
| 388 | msg_type := IPAC_MSGT_ID_GET, |
| 389 | u := { |
| 390 | get := { |
| 391 | { 1, IPAC_IDTAG_UNITNAME } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 396 | /* receive IPA CCM message */ |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 397 | private function f_ccm_rx_client(PDU_IPA_CCM ccm) runs on IPA_Emulation_CT { |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 398 | select (ccm.msg_type) { |
| 399 | case (IPAC_MSGT_PING) { |
| 400 | f_ccm_tx(valueof(ts_IPA_PONG)); |
| 401 | } |
| 402 | case (IPAC_MSGT_ID_ACK) { |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 403 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_ID_ACK)); |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 404 | } |
| 405 | case (IPAC_MSGT_ID_GET) { |
| 406 | f_ccm_tx(f_ccm_make_id_resp(ccm)); |
| 407 | } |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 408 | case else { |
| 409 | log("Unknown/unsupported IPA CCM message type", ccm); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | private function f_ccm_rx_server(PDU_IPA_CCM ccm) runs on IPA_Emulation_CT { |
| 415 | select (ccm.msg_type) { |
| 416 | case (IPAC_MSGT_PING) { |
| 417 | f_ccm_tx(valueof(ts_IPA_PONG)); |
| 418 | } |
| 419 | case (IPAC_MSGT_ID_ACK) { |
| 420 | /* the IPA server should at some point receive an ID_ACK from the client, |
| 421 | * in case of RSL/OML from nanoBTS, this is actually the first message after |
| 422 | * the TCP connection is established. Other implementations may differ. |
| 423 | * We currently ignore it completely - but actually we should make sure that |
| 424 | * one ID_ACK is received by the server at some point */ |
Harald Welte | 2e32e43 | 2018-05-24 20:00:00 +0200 | [diff] [blame] | 425 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_ID_ACK)); |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 426 | } |
Harald Welte | 3bc387f | 2018-02-21 12:18:46 +0100 | [diff] [blame] | 427 | case (IPAC_MSGT_ID_RESP) { |
| 428 | log("IPA ID RESP: ", ccm.u.resp); |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 429 | /* acknowledge any identity that the client may have sent */ |
| 430 | f_ccm_tx(valueof(ts_IPA_ACK)); |
Harald Welte | 3bc387f | 2018-02-21 12:18:46 +0100 | [diff] [blame] | 431 | } |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 432 | case else { |
| 433 | log("Unknown/unsupported IPA CCM message type", ccm); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 438 | private function f_to_asp(IPA_RecvFrom ipa_rx) return ASP_IPA_Unitdata { |
| 439 | var ASP_IPA_Unitdata ret := { |
| 440 | streamId := ipa_rx.streamId, |
Harald Welte | 2a8f847 | 2017-11-23 21:11:34 +0100 | [diff] [blame] | 441 | streamIdExt := ipa_rx.streamIdExt, |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 442 | payload := ipa_rx.msg |
| 443 | } |
| 444 | return ret; |
| 445 | } |
| 446 | |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 447 | private function f_from_asp(IPL4asp_Types.ConnectionId connId, ASP_IPA_Unitdata ipa_tx) return IPA_Send { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 448 | var IPA_Send ret := valueof(t_IPA_Send(connId, ipa_tx.streamId, ipa_tx.payload, |
| 449 | ipa_tx.streamIdExt)); |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 450 | return ret; |
| 451 | } |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 452 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 453 | #ifdef IPA_EMULATION_RSL |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 454 | private function f_from_rsl(IPL4asp_Types.ConnectionId connId, ASP_RSL_Unitdata rsl_tx) return IPA_Send { |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 455 | var octetstring payload := enc_RSL_Message(rsl_tx.rsl); |
| 456 | var IPA_Send ret := valueof(t_IPA_Send(connId, rsl_tx.streamId, payload)); |
| 457 | return ret; |
| 458 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 459 | #endif |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 460 | |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 461 | #ifdef IPA_EMULATION_OML |
| 462 | private function f_from_oml(IPL4asp_Types.ConnectionId connId, OML_PDU oml_tx) return IPA_Send { |
| 463 | var octetstring payload := enc_OML_PDU(oml_tx); |
| 464 | var IPA_Send ret := valueof(t_IPA_Send(connId, IPAC_PROTO_OML, payload)); |
| 465 | return ret; |
| 466 | } |
| 467 | #endif |
| 468 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 469 | /* main function to use for a client-side IPA implementation */ |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 470 | function main_client(charstring remote_host, IPL4asp_Types.PortNumber remote_port, |
| 471 | charstring local_host, IPL4asp_Types.PortNumber local_port, |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 472 | IPA_CCM_Parameters ccm_pars := c_IPA_default_ccm_pars, |
| 473 | boolean ccm_enabled := true) runs on IPA_Emulation_CT { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 474 | g_mode := IPA_MODE_CLIENT; |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 475 | g_ccm_enabled := ccm_enabled; |
Harald Welte | e09921f | 2017-12-07 17:49:00 +0100 | [diff] [blame] | 476 | f_connect(remote_host, remote_port, local_host, local_port, ccm_pars); |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 477 | if (g_ccm_enabled) { |
| 478 | /* we're a client: Send ID_ACK immediately after connect */ |
| 479 | f_ccm_tx(valueof(ts_IPA_ACK)); |
| 480 | } |
Harald Welte | 03c0e56 | 2017-12-09 02:55:12 +0100 | [diff] [blame] | 481 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_UP)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 482 | ScanEvents(); |
| 483 | } |
| 484 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 485 | /* main function to use for a server-side IPA implementation */ |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 486 | function main_server(charstring local_host, IPL4asp_Types.PortNumber local_port, |
Neels Hofmeyr | 3bf31d2 | 2018-08-24 14:44:32 +0200 | [diff] [blame] | 487 | boolean ccm_enabled := true, |
| 488 | IpaInitBehavior init_behavior := IPA_INIT_SEND_IPA_ID_GET) |
| 489 | runs on IPA_Emulation_CT { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 490 | g_mode := IPA_MODE_SERVER; |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 491 | g_ccm_enabled := ccm_enabled; |
Neels Hofmeyr | 3bf31d2 | 2018-08-24 14:44:32 +0200 | [diff] [blame] | 492 | g_init_behavior := init_behavior; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 493 | f_bind(local_host, local_port); |
| 494 | ScanEvents(); |
| 495 | } |
| 496 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 497 | private function f_ctrl_to_user(octetstring msg) runs on IPA_Emulation_CT { |
| 498 | var charstring msg_ch := oct2char(msg); |
| 499 | IPA_CTRL_PORT.send(dec_CtrlMessage(msg_ch)); |
| 500 | } |
| 501 | |
| 502 | #ifdef IPA_EMULATION_GSUP |
| 503 | private function f_gsup_to_user(octetstring msg) runs on IPA_Emulation_CT { |
| 504 | var GSUP_PDU gsup := dec_GSUP_PDU(msg); |
| 505 | f_gsup_postprocess_decoded(gsup); |
| 506 | IPA_GSUP_PORT.send(gsup); |
| 507 | } |
| 508 | #endif |
| 509 | |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 510 | #ifdef IPA_EMULATION_RSPRO |
| 511 | private function f_rspro_to_user(octetstring msg) runs on IPA_Emulation_CT { |
| 512 | var RsproPDU rspro := dec_RsproPDU(msg); |
| 513 | IPA_RSPRO_PORT.send(rspro); |
| 514 | } |
| 515 | #endif |
| 516 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 517 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 518 | private function f_mgcp_to_user(octetstring msg) runs on IPA_Emulation_CT { |
| 519 | var charstring msg_ch := oct2char(msg); |
| 520 | if (g_is_bsc_mgw) { |
| 521 | log("============"); |
| 522 | log(msg_ch); |
| 523 | IPA_MGCP_PORT.send(dec_MgcpCommand(msg_ch)); |
| 524 | } else { |
| 525 | IPA_MGCP_PORT.send(dec_MgcpResponse(msg_ch)); |
| 526 | } |
| 527 | } |
| 528 | |
Harald Welte | 92632e1 | 2017-11-25 02:31:20 +0100 | [diff] [blame] | 529 | private function f_mgcp_to_ud(octetstring payload) runs on IPA_Emulation_CT return ASP_IPA_Unitdata { |
| 530 | if (mp_ipa_mgcp_uses_osmo_ext) { |
| 531 | return valueof(t_ASP_IPA_UD(IPAC_PROTO_MGCP_OLD, payload)); |
| 532 | } else { |
| 533 | return valueof(t_ASP_IPA_UD(IPAC_PROTO_OSMO, payload, IPAC_PROTO_EXT_MGCP)); |
| 534 | } |
| 535 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 536 | #endif |
Harald Welte | 92632e1 | 2017-11-25 02:31:20 +0100 | [diff] [blame] | 537 | |
Harald Welte | bdb6370 | 2017-12-09 01:15:44 +0100 | [diff] [blame] | 538 | /* main loop function for both client and server. 'thread' of the component */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 539 | private function ScanEvents() runs on IPA_Emulation_CT { |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 540 | var IPA_RecvFrom ipa_rx; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 541 | var ASP_Event asp_evt; |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 542 | var Socket_API_Definitions.PortEvent port_evt; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 543 | var octetstring payload; |
| 544 | var CtrlMessage ctrl_msg; |
| 545 | var ASP_IPA_Unitdata ipa_ud; |
| 546 | #ifdef IPA_EMULATION_SCCP |
| 547 | var ASP_MTP3_TRANSFERreq mtp_req; |
| 548 | #endif |
| 549 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 550 | var MgcpCommand mgcp_cmd; |
| 551 | var MgcpResponse mgcp_rsp; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 552 | #endif |
| 553 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 554 | var GSUP_PDU gsup_msg; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 555 | #endif |
| 556 | #ifdef IPA_EMULATION_RSL |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 557 | var ASP_RSL_Unitdata rsl; |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 558 | #endif |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 559 | #ifdef IPA_EMULATION_OML |
| 560 | var OML_PDU oml; |
| 561 | #endif |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 562 | #ifdef IPA_EMULATION_RSPRO |
| 563 | var RsproPDU rspro; |
| 564 | #endif |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 565 | |
Harald Welte | 3e6ad89 | 2017-12-12 14:39:46 +0100 | [diff] [blame] | 566 | /* Set function for dissecting the binary */ |
| 567 | var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen); |
| 568 | IPA_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(IPA_PORT, g_ipa_conn_id, vl_f, {0, 2, 3, 1, 0}); |
| 569 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 570 | while (true) { |
| 571 | alt { |
| 572 | /* Received IPA -> up into SCCP stack */ |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 573 | [g_ccm_enabled] IPA_PORT.receive(IPA_RecvFrom:{?,IPAC_PROTO_CCM,omit,?}) -> value ipa_rx { |
| 574 | var PDU_IPA_CCM ccm := dec_PDU_IPA_CCM(ipa_rx.msg); |
| 575 | log("CCM Rx:", ccm); |
| 576 | select (g_mode) { |
| 577 | case (IPA_MODE_CLIENT) { |
| 578 | f_ccm_rx_client(ccm); |
| 579 | } |
| 580 | case (IPA_MODE_SERVER) { |
| 581 | f_ccm_rx_server(ccm); |
| 582 | } |
| 583 | case else { |
| 584 | setverdict(fail, "Unknown mode"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 585 | mtc.stop; |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 586 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 587 | } |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 588 | } |
| 589 | [] IPA_PORT.receive(IPA_RecvFrom:?) -> value ipa_rx { |
| 590 | select (ipa_rx.streamId) { |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 591 | #ifdef IPA_EMULATION_SCCP |
| 592 | case (IPAC_PROTO_SCCP) { |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 593 | var ASP_MTP3_TRANSFERind mtp; |
| 594 | mtp := valueof(ts_MTP3_XFER_ind(0, ipa_rx.msg)); |
| 595 | MTP3_SP_PORT.send(mtp); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 596 | } |
| 597 | #endif |
| 598 | #ifdef IPA_EMULATION_MGCP |
| 599 | case (IPAC_PROTO_MGCP_OLD) { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 600 | f_mgcp_to_user(ipa_rx.msg); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 601 | } |
| 602 | #endif |
| 603 | #ifdef IPA_EMULATION_RSL |
| 604 | case (t_IpaSidRSL) { |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 605 | rsl := { |
| 606 | streamId := ipa_rx.streamId, |
| 607 | rsl := dec_RSL_Message(ipa_rx.msg) |
| 608 | }; |
| 609 | IPA_RSL_PORT.send(rsl); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 610 | } |
| 611 | #endif |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 612 | #ifdef IPA_EMULATION_OML |
| 613 | case (IPAC_PROTO_OML) { |
| 614 | oml := dec_OML_PDU(ipa_rx.msg) |
| 615 | IPA_OML_PORT.send(oml); |
| 616 | } |
| 617 | #endif |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 618 | case (IPAC_PROTO_OSMO) { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 619 | select (ipa_rx.streamIdExt) { |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 620 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 621 | case (IPAC_PROTO_EXT_MGCP) { |
| 622 | f_mgcp_to_user(ipa_rx.msg); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 623 | } |
| 624 | #endif |
| 625 | case (IPAC_PROTO_EXT_CTRL) { |
Harald Welte | a76c4bb | 2017-12-09 02:06:07 +0100 | [diff] [blame] | 626 | f_ctrl_to_user(ipa_rx.msg); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 627 | } |
| 628 | #ifdef IPA_EMULATION_GSUP |
| 629 | case (IPAC_PROTO_EXT_GSUP) { |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 630 | f_gsup_to_user(ipa_rx.msg); |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 631 | } |
| 632 | #endif |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 633 | #ifdef IPA_EMULATION_RSPRO |
| 634 | case (IPAC_PROTO_EXT_RSPRO) { |
| 635 | f_rspro_to_user(ipa_rx.msg); |
| 636 | } |
| 637 | #endif |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 638 | case else { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 639 | IPA_SP_PORT.send(f_to_asp(ipa_rx)); |
| 640 | } |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 641 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 642 | } |
| 643 | case else { |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 644 | IPA_SP_PORT.send(f_to_asp(ipa_rx)); |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 649 | /* server only */ |
| 650 | [] IPA_PORT.receive(ASP_Event:{connOpened:=?}) -> value asp_evt { |
| 651 | log("IPA: Connected"); |
| 652 | g_ipa_conn_id := asp_evt.connOpened.connId; |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 653 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_UP)); |
Harald Welte | 2d86aff | 2018-04-17 11:23:04 +0200 | [diff] [blame] | 654 | if (g_mode == IPA_MODE_SERVER and g_ccm_enabled) { |
Neels Hofmeyr | 3bf31d2 | 2018-08-24 14:44:32 +0200 | [diff] [blame] | 655 | select (g_init_behavior) { |
| 656 | case (IPA_INIT_SEND_IPA_ID_GET) { |
| 657 | f_ccm_tx(valueof(ts_IPA_ID_GET)); |
| 658 | } |
| 659 | case (IPA_INIT_SEND_IPA_ID_ACK) { |
| 660 | f_ccm_tx(valueof(ts_IPA_ACK)); |
| 661 | } |
| 662 | } |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | [] IPA_PORT.receive(ASP_Event:{connClosed:=?}) -> value asp_evt { |
| 667 | log("IPA: Closed"); |
| 668 | g_ipa_conn_id := -1; |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 669 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_DOWN)); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 670 | self.stop; |
| 671 | } |
| 672 | |
Stefan Sperling | 830dc9d | 2018-02-12 21:08:28 +0100 | [diff] [blame] | 673 | [] IPA_PORT.receive(Socket_API_Definitions.PortEvent:{result:={errorCode:=ERROR_SOCKET, connId:=?, os_error_code:=?, os_error_text:=?}}) -> value port_evt { |
| 674 | log("PortEvent: ERROR_SOCKET: ", port_evt); |
| 675 | g_ipa_conn_id := -1; |
| 676 | f_send_IPA_EVT(t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_DOWN)); |
| 677 | self.stop; |
| 678 | } |
| 679 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 680 | #ifdef IPA_EMULATION_SCCP |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 681 | /* Received SCCP -> down into IPA */ |
| 682 | [] MTP3_SP_PORT.receive(ASP_MTP3_TRANSFERreq: ?) -> value mtp_req { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 683 | var IPA_Send ipa_tx := valueof(t_IPA_Send(g_ipa_conn_id, IPAC_PROTO_SCCP, |
| 684 | mtp_req.data)); |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 685 | IPA_PORT.send(ipa_tx); |
| 686 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 687 | #endif |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 688 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 689 | #ifdef IPA_EMULATION_MGCP |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 690 | /* Received MGCP -> down into IPA */ |
| 691 | [] IPA_MGCP_PORT.receive(MgcpCommand:?) -> value mgcp_cmd { |
Harald Welte | 92632e1 | 2017-11-25 02:31:20 +0100 | [diff] [blame] | 692 | payload := char2oct(enc_MgcpCommand(mgcp_cmd)); |
| 693 | ipa_ud := f_mgcp_to_ud(payload); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 694 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 695 | } |
| 696 | [] IPA_MGCP_PORT.receive(MgcpResponse:?) -> value mgcp_rsp { |
Harald Welte | 1dd8f37 | 2017-11-25 02:25:27 +0100 | [diff] [blame] | 697 | payload := char2oct(enc_MgcpResponse(mgcp_rsp)); |
Harald Welte | 92632e1 | 2017-11-25 02:31:20 +0100 | [diff] [blame] | 698 | ipa_ud := f_mgcp_to_ud(payload); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 699 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 700 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 701 | #endif |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 702 | |
Harald Welte | a76c4bb | 2017-12-09 02:06:07 +0100 | [diff] [blame] | 703 | [] IPA_CTRL_PORT.receive(CtrlMessage:?) -> value ctrl_msg { |
| 704 | payload := char2oct(enc_CtrlMessage(ctrl_msg)); |
| 705 | ipa_ud := valueof(t_ASP_IPA_UD(IPAC_PROTO_OSMO, payload, IPAC_PROTO_EXT_CTRL)); |
| 706 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 707 | } |
| 708 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 709 | #ifdef IPA_EMULATION_GSUP |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 710 | [] IPA_GSUP_PORT.receive(GSUP_PDU:?) -> value gsup_msg { |
Harald Welte | 2f562b1 | 2018-01-24 20:52:38 +0100 | [diff] [blame] | 711 | f_gsup_preprocess_encoded(gsup_msg); |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 712 | payload := enc_GSUP_PDU(gsup_msg); |
| 713 | ipa_ud := valueof(t_ASP_IPA_UD(IPAC_PROTO_OSMO, payload, IPAC_PROTO_EXT_GSUP)); |
| 714 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 715 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 716 | #endif |
Harald Welte | df32723 | 2017-12-28 22:51:51 +0100 | [diff] [blame] | 717 | |
Harald Welte | 7460a72 | 2018-10-10 12:28:27 +0200 | [diff] [blame] | 718 | #ifdef IPA_EMULATION_RSPRO |
| 719 | [] IPA_RSPRO_PORT.receive(RsproPDU:?) -> value rspro { |
| 720 | payload := enc_RsproPDU(rspro); |
| 721 | ipa_ud := valueof(t_ASP_IPA_UD(IPAC_PROTO_OSMO, payload, IPAC_PROTO_EXT_RSPRO)); |
| 722 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 723 | } |
| 724 | #endif |
| 725 | |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 726 | #ifdef IPA_EMULATION_RSL |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 727 | /* Received RSL -> down into IPA */ |
| 728 | [] IPA_RSL_PORT.receive(ASP_RSL_Unitdata:?) -> value rsl { |
| 729 | IPA_PORT.send(f_from_rsl(g_ipa_conn_id, rsl)); |
| 730 | } |
Harald Welte | df27725 | 2018-02-20 15:49:30 +0100 | [diff] [blame] | 731 | #endif |
Harald Welte | c682666 | 2019-02-06 22:26:46 +0100 | [diff] [blame] | 732 | #ifdef IPA_EMULATION_OML |
| 733 | /* Received OML -> down into IPA */ |
| 734 | [] IPA_OML_PORT.receive(OML_PDU:?) -> value oml { |
| 735 | IPA_PORT.send(f_from_oml(g_ipa_conn_id, oml)); |
| 736 | } |
| 737 | [] IPA_OML_PORT.receive(octetstring:?) -> value payload { |
| 738 | IPA_PORT.send(t_IPA_Send(g_ipa_conn_id, IPAC_PROTO_OML, payload)); |
| 739 | } |
| 740 | #endif |
Harald Welte | 0d846a7 | 2017-12-07 17:58:28 +0100 | [diff] [blame] | 741 | /* Received MISC (OML/CTRL) -> down into IPA */ |
Harald Welte | c76f29f | 2017-11-22 12:46:46 +0100 | [diff] [blame] | 742 | [] IPA_SP_PORT.receive(ASP_IPA_Unitdata: ?) -> value ipa_ud { |
| 743 | IPA_PORT.send(f_from_asp(g_ipa_conn_id, ipa_ud)); |
| 744 | } |
| 745 | |
| 746 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
Harald Welte | 2e32e43 | 2018-05-24 20:00:00 +0200 | [diff] [blame] | 751 | /*********************************************************************** |
| 752 | * IPA Event waiter component. Wait for ASP_IPA_EVENT_ID_ACK |
| 753 | ***********************************************************************/ |
| 754 | |
| 755 | type component IPA_EventWaiter_CT { |
| 756 | port IPA_SP_PT IPA_SP_PORT; |
| 757 | } |
| 758 | |
| 759 | function waiter_main(template ASP_IPA_Event wait_for := t_ASP_IPA_EVT_UD(ASP_IPA_EVENT_ID_ACK)) |
| 760 | runs on IPA_EventWaiter_CT { |
| 761 | |
| 762 | alt { |
| 763 | [] IPA_SP_PORT.receive(wait_for) { |
| 764 | setverdict(pass); |
| 765 | } |
| 766 | [] IPA_SP_PORT.receive { repeat; } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | |
Harald Welte | d86cdc6 | 2017-11-22 00:45:07 +0100 | [diff] [blame] | 771 | } |