Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 1 | module PGW_Tests { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from Native_Functions all; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 6 | import from Misc_Helpers all; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 7 | |
| 8 | import from GTPv2_Types all; |
| 9 | import from GTPv2_Templates all; |
| 10 | import from GTPv2_Emulation all; |
| 11 | |
| 12 | import from UECUPS_Types all; |
| 13 | |
| 14 | import from DNS_Helpers all; |
| 15 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 16 | |
| 17 | import from DIAMETER_Types all; |
| 18 | import from DIAMETER_Templates all; |
| 19 | import from DIAMETER_Emulation all; |
| 20 | |
| 21 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 22 | modulepar { |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 23 | charstring mp_pgw_hostname := "127.0.0.4"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 24 | charstring mp_local_hostname_c := "127.0.0.1"; |
| 25 | charstring mp_local_hostname_u := "127.0.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 26 | |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 27 | charstring mp_run_prog_log_path := "/tmp"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 28 | charstring mp_run_prog_as_user := "laforge"; |
| 29 | charstring mp_ping_hostname := "10.45.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 30 | |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 31 | charstring mp_pcrf_local_ip := "127.0.0.9"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 32 | integer mp_pcrf_local_port := 3868; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 33 | |
| 34 | charstring mp_ocs_local_ip := "127.0.0.9"; |
| 35 | integer mp_ocs_local_port := 3869; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* main component, we typically have one per testcase */ |
| 39 | type component PGW_Test_CT { |
| 40 | var GTPv2_Emulation_CT vc_GTP2; |
| 41 | port GTP2EM_PT TEID0; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 42 | |
| 43 | /* emulated PCRF */ |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 44 | var DIAMETER_Emulation_CT vc_Gx; |
| 45 | port DIAMETER_PT Gx_UNIT; |
| 46 | port DIAMETEREM_PROC_PT Gx_PROC; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 47 | /* emulated OCS */ |
| 48 | var DIAMETER_Emulation_CT vc_Gy; |
| 49 | port DIAMETER_PT Gy_UNIT; |
| 50 | port DIAMETEREM_PROC_PT Gy_PROC; |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 51 | /* global test case guard timer (actual timeout value is set in f_init()) */ |
| 52 | timer T_guard; |
| 53 | } |
| 54 | |
| 55 | /* global altstep for global guard timer; */ |
| 56 | altstep as_Tguard() runs on PGW_Test_CT { |
| 57 | [] T_guard.timeout { |
| 58 | setverdict(fail, "Timeout of T_guard"); |
| 59 | mtc.stop; |
| 60 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 63 | type component DIAMETER_ConnHdlr_CT extends DIAMETER_ConnHdlr { |
| 64 | port DIAMETER_Conn_PT DIAMETER_CLIENT; |
| 65 | } |
| 66 | |
| 67 | function f_diam_connhldr_ct_main(hexstring imsi) runs on DIAMETER_ConnHdlr_CT { |
| 68 | var PDU_DIAMETER msg; |
| 69 | |
| 70 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 71 | f_diameter_expect(imsi); |
| 72 | } |
| 73 | |
| 74 | while (true) { |
| 75 | alt { |
| 76 | [] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg { |
| 77 | DIAMETER.send(msg); |
| 78 | } |
| 79 | [] DIAMETER.receive(PDU_DIAMETER:?) -> value msg { |
| 80 | DIAMETER_CLIENT.send(msg); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 87 | /* per-session component; we typically have 1..N per testcase */ |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 88 | type component PGW_Session_CT extends GTP2_ConnHdlr { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 89 | var SessionPars g_pars; |
| 90 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 91 | port DIAMETER_Conn_PT Gx; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 92 | port DIAMETER_Conn_PT Gy; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 93 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 94 | /* TEI (Data) local side */ |
| 95 | var OCT4 g_teid; |
| 96 | /* TEI (Control) local side */ |
| 97 | var OCT4 g_teic; |
| 98 | /* TEI (Data) remote side */ |
| 99 | var OCT4 g_teid_remote; |
| 100 | /* TEI (Control) remote side */ |
| 101 | var OCT4 g_teic_remote; |
| 102 | /* GTP-U IPv4 address remote sie */ |
| 103 | var OCT4 g_gtpu4_remote; |
| 104 | var OCT16 g_gtpu6_remote; |
| 105 | |
| 106 | /* Address allocation */ |
| 107 | var OCT4 g_ip4_addr; |
| 108 | var OCT16 g_ip6_addr; |
| 109 | var integer g_ip6_plen; |
| 110 | } |
| 111 | |
| 112 | /* configuration data for a given Session */ |
| 113 | type record SessionPars { |
| 114 | hexstring imsi, |
| 115 | octetstring msisdn optional, |
| 116 | // serving network |
| 117 | integer rat_type, |
| 118 | // flags? |
| 119 | charstring apn, |
| 120 | /* Apn subscribed or non-subscribed */ |
| 121 | boolean selection_mode, |
| 122 | BIT3 pdn_type, |
| 123 | /* PAA */ |
| 124 | /* Max APN Restriction */ |
| 125 | /* APN-AMBR */ |
| 126 | octetstring pco optional, |
| 127 | octetstring epco optional, |
| 128 | /* Bearer Contexts to be created */ |
| 129 | |
| 130 | charstring tun_dev_name, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 131 | charstring tun_netns_name optional, |
| 132 | |
| 133 | /* In seconds. 0 => disabled, !0 => grant over CC-Time period */ |
| 134 | integer gy_validity_time |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | template (value) SessionPars |
| 138 | t_SessionPars(hexstring imsi, charstring tundev, integer rat_type := 6, charstring apn := "internet", |
| 139 | boolean selection_mode := false, BIT3 pdn_type := '001'B) := { |
| 140 | imsi := imsi, |
| 141 | msisdn := omit, |
| 142 | rat_type := rat_type, |
| 143 | apn := apn, |
| 144 | selection_mode := selection_mode, |
| 145 | pdn_type := pdn_type, |
| 146 | pco := omit, |
| 147 | epco := omit, |
| 148 | tun_dev_name := tundev, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 149 | tun_netns_name := tundev, |
| 150 | gy_validity_time := 0 |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | type record BearerConfig { |
| 154 | integer eps_bearer_id |
| 155 | } |
| 156 | |
| 157 | type function void_fn() runs on PGW_Session_CT; |
| 158 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 159 | friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg) |
| 160 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER { |
| 161 | DIAMETER_UNIT.send(msg); |
| 162 | return omit; |
| 163 | } |
| 164 | |
| 165 | friend function f_init_diameter(charstring id) runs on PGW_Test_CT { |
| 166 | var DIAMETEROps ops := { |
| 167 | create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback), |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 168 | unitdata_cb := refers(DiameterForwardUnitdataCallback), |
| 169 | raw := false /* handler mode (IMSI based routing) */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 170 | }; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 171 | var DIAMETER_conn_parameters pars; |
| 172 | |
| 173 | /* Gx setup: */ |
| 174 | pars := { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 175 | remote_ip := mp_pgw_hostname, |
| 176 | remote_sctp_port := -1, |
| 177 | local_ip := mp_pcrf_local_ip, |
| 178 | local_sctp_port := mp_pcrf_local_port, |
| 179 | origin_host := "pcrf.localdomain", |
| 180 | origin_realm := "localdomain", |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 181 | auth_app_id := omit, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 182 | vendor_app_id := c_DIAMETER_3GPP_Gx_AID |
| 183 | }; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 184 | vc_Gx := DIAMETER_Emulation_CT.create(id); |
| 185 | map(vc_Gx:DIAMETER, system:DIAMETER_CODEC_PT); |
| 186 | connect(vc_Gx:DIAMETER_UNIT, self:Gx_UNIT); |
| 187 | connect(vc_Gx:DIAMETER_PROC, self:Gx_PROC); |
| 188 | vc_Gx.start(DIAMETER_Emulation.main(ops, pars, id)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 189 | |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 190 | /* Gy setup: */ |
| 191 | pars := { |
| 192 | remote_ip := mp_pgw_hostname, |
| 193 | remote_sctp_port := -1, |
| 194 | local_ip := mp_ocs_local_ip, |
| 195 | local_sctp_port := mp_ocs_local_port, |
| 196 | origin_host := "ocs.localdomain", |
| 197 | origin_realm := "localdomain", |
| 198 | auth_app_id := c_DIAMETER_CREDIT_CONTROL_AID, |
| 199 | vendor_app_id := omit |
| 200 | }; |
| 201 | vc_Gy := DIAMETER_Emulation_CT.create(id); |
| 202 | map(vc_Gy:DIAMETER, system:DIAMETER_CODEC_PT); |
| 203 | connect(vc_Gy:DIAMETER_UNIT, self:Gy_UNIT); |
| 204 | connect(vc_Gy:DIAMETER_PROC, self:Gy_PROC); |
| 205 | vc_Gy.start(DIAMETER_Emulation.main(ops, pars, id)); |
| 206 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 207 | f_diameter_wait_capability(Gx_UNIT); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 208 | f_diameter_wait_capability(Gy_UNIT); |
Pau Espin Pedrol | bb5f45f | 2022-04-11 12:36:55 +0200 | [diff] [blame] | 209 | /* Give some time for our emulation to get out of SUSPECT list of SUT (3 watchdong ping-pongs): |
| 210 | * RFC6733 sec 5.1 |
| 211 | * RFC3539 sec 3.4.1 [5] |
| 212 | * https://github.com/freeDiameter/freeDiameter/blob/master/libfdcore/p_psm.c#L49 |
| 213 | */ |
| 214 | f_sleep(1.0); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 217 | private function f_init(float guard_timeout := 60.0) runs on PGW_Test_CT { |
| 218 | T_guard.start(guard_timeout); |
| 219 | activate(as_Tguard()); |
| 220 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 221 | var Gtp2EmulationCfg cfg := { |
| 222 | gtpc_bind_ip := mp_local_hostname_c, |
| 223 | gtpc_bind_port := GTP2C_PORT, |
| 224 | gtpc_remote_ip := mp_pgw_hostname, |
| 225 | gtpc_remote_port := GTP2C_PORT, |
| 226 | sgw_role := true, |
| 227 | use_gtpu_daemon := true |
| 228 | }; |
| 229 | |
| 230 | vc_GTP2 := GTPv2_Emulation_CT.create("GTP2_EM"); |
| 231 | map(vc_GTP2:GTP2C, system:GTP2C); |
| 232 | connect(vc_GTP2:TEID0, self:TEID0); |
| 233 | vc_GTP2.start(GTPv2_Emulation.main(cfg)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 234 | |
| 235 | if (mp_pcrf_local_ip != "") { |
| 236 | f_init_diameter(testcasename()); |
| 237 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 240 | function f_start_handler(void_fn fn, template (omit) SessionPars pars_tmpl := omit) |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 241 | runs on PGW_Test_CT return PGW_Session_CT { |
| 242 | var charstring id := testcasename(); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 243 | var DIAMETER_ConnHdlr_CT vc_conn_gx, vc_conn_gy; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 244 | var PGW_Session_CT vc_conn; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 245 | var SessionPars pars; |
| 246 | |
| 247 | if (isvalue(pars_tmpl)) { |
| 248 | pars := valueof(pars_tmpl); |
| 249 | } else { |
| 250 | /*TODO: set default values */ |
| 251 | } |
| 252 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 253 | vc_conn := PGW_Session_CT.create(id); |
| 254 | connect(vc_conn:GTP2, vc_GTP2:CLIENT); |
| 255 | connect(vc_conn:GTP2_PROC, vc_GTP2:CLIENT_PROC); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 256 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 257 | if (isbound(vc_Gx)) { |
| 258 | vc_conn_gx := DIAMETER_ConnHdlr_CT.create(id); |
| 259 | connect(vc_conn_gx:DIAMETER, vc_Gx:DIAMETER_CLIENT); |
| 260 | connect(vc_conn_gx:DIAMETER_PROC, vc_Gx:DIAMETER_PROC); |
| 261 | connect(vc_conn:Gx, vc_conn_gx:DIAMETER_CLIENT); |
| 262 | vc_conn_gx.start(f_diam_connhldr_ct_main(pars.imsi)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 265 | if (isbound(vc_Gy)) { |
| 266 | vc_conn_gy := DIAMETER_ConnHdlr_CT.create(id); |
| 267 | connect(vc_conn_gy:DIAMETER, vc_Gy:DIAMETER_CLIENT); |
| 268 | connect(vc_conn_gy:DIAMETER_PROC, vc_Gy:DIAMETER_PROC); |
| 269 | connect(vc_conn:Gy, vc_conn_gy:DIAMETER_CLIENT); |
| 270 | vc_conn_gy.start(f_diam_connhldr_ct_main(pars.imsi)); |
| 271 | } |
| 272 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 273 | vc_conn.start(f_handler_init(fn, pars)); |
| 274 | return vc_conn; |
| 275 | } |
| 276 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 277 | private function f_handler_init(void_fn fn, SessionPars pars) |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 278 | runs on PGW_Session_CT { |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 279 | g_pars := valueof(pars); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 280 | fn.apply(); |
| 281 | } |
| 282 | |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 283 | private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring { |
| 284 | var integer suffix_len := tot_len - lengthof(prefix); |
| 285 | var charstring suffix_ch := int2str(suffix); |
| 286 | var integer pad_len := suffix_len - lengthof(suffix_ch); |
| 287 | |
| 288 | return prefix & int2hex(0, pad_len) & str2hex(suffix_ch); |
| 289 | } |
| 290 | |
| 291 | function f_gen_imei(integer suffix) return hexstring { |
| 292 | return f_concat_pad(14, '49999'H, suffix); |
| 293 | } |
| 294 | |
| 295 | function f_gen_imsi(integer suffix) return hexstring { |
| 296 | return f_concat_pad(15, '26242'H, suffix); |
| 297 | } |
| 298 | |
| 299 | function f_gen_msisdn(integer suffix) return hexstring { |
| 300 | return f_concat_pad(12, '49123'H, suffix); |
| 301 | } |
| 302 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 303 | private altstep as_DIA_Gx_CCR(DCC_NONE_CC_Request_Type req_type) runs on PGW_Session_CT { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 304 | var PDU_DIAMETER rx_dia; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 305 | [] Gx.receive(tr_DIA_Gx_CCR(req_type := req_type)) -> value rx_dia { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 306 | var template (omit) AVP avp; |
| 307 | var octetstring sess_id; |
| 308 | var AVP_Unsigned32 req_num; |
| 309 | |
| 310 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id); |
| 311 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 312 | |
| 313 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number); |
| 314 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 315 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 316 | Gx.send(ts_DIA_Gx_CCA(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 317 | req_type, req_num)); |
| 318 | } |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 319 | [] Gx.receive(PDU_DIAMETER:?) -> value rx_dia { |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 320 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 321 | log2str("Received unexpected DIAMETER ", rx_dia)); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private altstep as_DIA_Gy_CCR(DCC_NONE_CC_Request_Type req_type) runs on PGW_Session_CT { |
| 326 | var PDU_DIAMETER rx_dia; |
| 327 | [] Gy.receive(tr_DIA_Gy_CCR(req_type := req_type)) -> value rx_dia { |
| 328 | var template (value) PDU_DIAMETER tx_dia; |
| 329 | var template (omit) AVP avp; |
| 330 | var octetstring sess_id; |
| 331 | var AVP_Unsigned32 req_num; |
| 332 | |
| 333 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id); |
| 334 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 335 | |
| 336 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number); |
| 337 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 338 | if (g_pars.gy_validity_time > 0) { |
| 339 | tx_dia := ts_DIA_Gy_CCA_ValidityTime(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id, |
| 340 | req_type, req_num, g_pars.gy_validity_time); |
| 341 | } else { |
| 342 | tx_dia := ts_DIA_Gy_CCA(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id, |
| 343 | req_type, req_num); |
| 344 | } |
| 345 | Gy.send(tx_dia); |
| 346 | } |
| 347 | [] Gy.receive(PDU_DIAMETER:?) -> value rx_dia { |
| 348 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 349 | log2str("Received unexpected DIAMETER Gy", rx_dia)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 353 | |
| 354 | /* find TEID of given interface type (and optionally instance) */ |
| 355 | private function f_find_teid(FullyQualifiedTEID_List list, |
| 356 | template (present) integer if_type, |
| 357 | template (present) BIT4 instance := ?) |
| 358 | return template (omit) FullyQualifiedTEID |
| 359 | { |
| 360 | var integer i; |
| 361 | for (i := 0; i < lengthof(list); i := i+1) { |
| 362 | if (match(list[i].interfaceType, if_type) and |
| 363 | match(list[i].instance, instance)) { |
| 364 | return list[i]; |
| 365 | } |
| 366 | } |
| 367 | return omit; |
| 368 | } |
| 369 | |
| 370 | /* process one to-be-created bearer context */ |
| 371 | private function process_bctx_create(BearerContextGrouped bctx) runs on PGW_Session_CT |
| 372 | { |
| 373 | /* FIXME: EPS Bearer ID */ |
| 374 | /* FIXME: Cause */ |
| 375 | |
| 376 | /* find F-TEID of the P-GW U side */ |
| 377 | var FullyQualifiedTEID rx_fteid; |
| 378 | rx_fteid := valueof(f_find_teid(bctx.bearerContextIEs.fullyQualifiedTEID, 5, '0010'B)); |
| 379 | g_teid_remote := rx_fteid.tEID_GRE_Key; |
| 380 | if (rx_fteid.v4_Flag == '1'B) { |
| 381 | g_gtpu4_remote := rx_fteid.iPv4_Address; |
| 382 | } |
| 383 | if (rx_fteid.v6_Flag == '1'B) { |
| 384 | g_gtpu6_remote := rx_fteid.iPv6_Address; |
| 385 | } |
| 386 | |
| 387 | var UECUPS_CreateTun uecups_create := { |
| 388 | tx_teid := oct2int(g_teid_remote), |
| 389 | rx_teid := oct2int(g_teid), |
| 390 | user_addr_type := IPV4, |
| 391 | user_addr := '00000000'O, |
| 392 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 393 | remote_gtp_ep := valueof(ts_UECUPS_SockAddr(g_gtpu4_remote)), |
| 394 | tun_dev_name := g_pars.tun_dev_name, |
| 395 | tun_netns_name := g_pars.tun_netns_name |
| 396 | }; |
| 397 | |
| 398 | /* create tunnel in daemon */ |
| 399 | if (isbound(g_ip4_addr)) { |
| 400 | uecups_create.user_addr := g_ip4_addr; |
| 401 | f_gtp2_create_tunnel(uecups_create); |
| 402 | } |
| 403 | if (isbound(g_ip6_addr)) { |
| 404 | uecups_create.user_addr_type := IPV6; |
| 405 | uecups_create.user_addr := g_ip6_addr; |
| 406 | f_gtp2_create_tunnel(uecups_create); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* create a session on the PGW */ |
| 411 | private function f_create_session() runs on PGW_Session_CT { |
| 412 | var PDU_GTPCv2 rx; |
| 413 | |
| 414 | /* allocate + register TEID-C on local side */ |
| 415 | g_teic := f_gtp2_allocate_teid(); |
| 416 | g_teid := g_teic; |
| 417 | |
| 418 | var template (value) FullyQualifiedTEID fteid_c_ie, fteid_u_ie; |
| 419 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 420 | f_inet_addr(mp_local_hostname_c), omit); |
| 421 | fteid_u_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPU, g_teid, 2, |
| 422 | f_inet_addr(mp_local_hostname_u), omit); |
| 423 | var template (value) PDU_GTPCv2 g2c := |
| 424 | ts_GTP2C_CreateSessionReq(imsi := g_pars.imsi, msisdn := omit, rat_type := 6, |
| 425 | sender_fteid := fteid_c_ie, |
| 426 | apn := f_enc_dns_hostname(g_pars.apn), |
| 427 | pdn_type := g_pars.pdn_type, teid_list := { fteid_u_ie }, |
| 428 | chg_car := '0000'O, bearer_id := 1); |
| 429 | /* open5gs up to 1.2.3 won't accept it without ULI, despite not mandatory */ |
| 430 | var template (value) TAI tai := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0001'O }; |
| 431 | var template (value) ECGI ecgi := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0'H, 23 }; |
| 432 | g2c.gtpcv2_pdu.createSessionRequest.userLocationInfo := ts_GTP2C_UserLocInfo(tai := tai, ecgi := ecgi); |
Vadim Yanitskiy | bada3c9 | 2022-01-20 18:59:07 +0600 | [diff] [blame] | 433 | g2c.gtpcv2_pdu.createSessionRequest.servingNetwork := ts_GTP2C_ServingNetwork('001'H, '01F'H); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 434 | |
| 435 | GTP2.send(g2c); |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 436 | if (Gx.checkstate("Connected")) { |
| 437 | as_DIA_Gx_CCR(INITIAL_REQUEST); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 438 | } |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 439 | if (Gy.checkstate("Connected")) { |
| 440 | as_DIA_Gy_CCR(INITIAL_REQUEST); |
| 441 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 442 | alt { |
| 443 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:='10'O)) -> value rx { |
| 444 | /* extract TEIDs */ |
| 445 | var CreateSessionResponse resp := rx.gtpcv2_pdu.createSessionResponse; |
| 446 | g_teic_remote := resp.fullyQualifiedTEID[0].tEID_GRE_Key; |
| 447 | |
| 448 | /* extract allocated address[es] */ |
| 449 | var PDN_Address_and_Prefix paa := resp.pDN_AddressAllocation.pDN_Address_and_Prefix; |
| 450 | if (ischosen(paa.iPv4_Address)) { |
| 451 | g_ip4_addr := paa.iPv4_Address; |
| 452 | } else if (ischosen(paa.iPv6_Address)) { |
| 453 | g_ip6_addr := paa.iPv6_Address.iPv6_Address; |
| 454 | g_ip6_plen := paa.iPv6_Address.prefixLength; |
| 455 | } else if (ischosen(paa.iPv4_IPv6)) { |
| 456 | g_ip4_addr := paa.iPv4_IPv6.iPv4_Address; |
| 457 | g_ip6_addr := paa.iPv4_IPv6.iPv6_Address; |
| 458 | g_ip6_plen := paa.iPv4_IPv6.prefixLength; |
| 459 | } |
| 460 | var integer i; |
| 461 | for (i := 0; i < lengthof(resp.bearerContextGrouped); i := i+1) { |
| 462 | var BearerContextGrouped bctx := resp.bearerContextGrouped[i]; |
| 463 | select (bctx.instance) { |
| 464 | case ('0000'B) { // created |
| 465 | process_bctx_create(bctx); |
| 466 | } |
| 467 | case ('0001'B) { // removed |
| 468 | setverdict(fail, "We don't expect removed bearer contexts yet"); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=?)) -> value rx { |
| 474 | setverdict(fail, "Unexpected CreateSessionResp(cause=", |
| 475 | rx.gtpcv2_pdu.createSessionResponse.cause.causeValue, ")"); |
| 476 | } |
| 477 | [] GTP2.receive { |
| 478 | setverdict(fail, "Unexpected GTPv2 while waiting for CreateSessionResp"); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | } |
| 483 | |
| 484 | /* delete the session from the PGW */ |
| 485 | private function f_delete_session(template (omit) OCT1 tx_cause := omit, |
| 486 | template (present) OCT4 exp_teid, |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame] | 487 | template (present) OCT1 exp_cause, |
| 488 | boolean expect_diameter := true) runs on PGW_Session_CT { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 489 | var template (value) FullyQualifiedTEID fteid_c_ie |
| 490 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 491 | f_inet_addr(mp_local_hostname_c), omit); |
| 492 | var template PDU_GTPCv2 g2c := |
| 493 | ts_GTP2C_DeleteSessionReq(d_teid := g_teic_remote, cause := tx_cause, |
| 494 | sender_fteid := fteid_c_ie, |
| 495 | teid_list := {}, bearer_id := 1); |
| 496 | |
| 497 | GTP2.send(g2c); |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 498 | if (Gx.checkstate("Connected") and expect_diameter) { |
| 499 | as_DIA_Gx_CCR(TERMINATION_REQUEST); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 500 | } |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 501 | if (Gy.checkstate("Connected") and expect_diameter) { |
| 502 | as_DIA_Gy_CCR(TERMINATION_REQUEST); |
| 503 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 504 | alt { |
| 505 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(d_teid := exp_teid, cause := exp_cause)) { |
| 506 | setverdict(pass); |
| 507 | } |
| 508 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(?, ?)) { |
| 509 | setverdict(fail, "Unexpected DeleteSessionResp"); |
| 510 | } |
| 511 | [] GTP2.receive { |
| 512 | setverdict(fail, "Unexpected GTPv2 while waiting for DeleteSessionResp"); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /* destroy tunnel in daemon */ |
| 517 | if (isbound(g_teid)) { |
| 518 | var UECUPS_DestroyTun uecups_destroy := { |
| 519 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 520 | rx_teid := oct2int(g_teid) |
| 521 | }; |
| 522 | /* FIXME: what about IPv4/IPv6 differentiation? */ |
| 523 | f_gtp2_destroy_tunnel(uecups_destroy); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /* start a program on the user plane side; return its PID */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 528 | private function f_start_prog(charstring command, boolean redirect_output := true) |
| 529 | runs on PGW_Session_CT return integer |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 530 | { |
| 531 | var UECUPS_StartProgram sprog := { |
| 532 | command := command, |
| 533 | environment := {}, |
| 534 | run_as_user := mp_run_prog_as_user, |
| 535 | tun_netns_name := g_pars.tun_netns_name |
| 536 | }; |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 537 | |
| 538 | /* Redirect stdout/stderr to the user-specified location */ |
| 539 | if (redirect_output) { |
| 540 | var charstring prefix := mp_run_prog_log_path & "/" & testcasename(); |
| 541 | sprog.command := sprog.command & " 1>>" & prefix & ".prog.stdout"; |
| 542 | sprog.command := sprog.command & " 2>>" & prefix & ".prog.stderr"; |
| 543 | } |
| 544 | |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 545 | log("Starting a program: ", command); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 546 | var UECUPS_StartProgramRes res := f_gtp2_start_program(sprog); |
| 547 | if (res.result != OK) { |
| 548 | setverdict(fail, "Unable to start program '", command, "'"); |
| 549 | } |
| 550 | return res.pid; |
| 551 | } |
| 552 | |
| 553 | /* wait for termination of a given PID with specified exit_code */ |
| 554 | private function f_wait_term(integer pid, template (present) integer exit_code := 0, |
| 555 | float tout := 10.0) runs on PGW_Session_CT |
| 556 | { |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 557 | var UECUPS_ProgramTermInd pti; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 558 | timer T := tout; |
| 559 | |
| 560 | T.start; |
| 561 | alt { |
| 562 | [] GTP2.receive(UECUPS_ProgramTermInd:{pid := pid, exit_code := exit_code}) { |
| 563 | setverdict(pass); |
| 564 | } |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 565 | [] GTP2.receive(UECUPS_ProgramTermInd:?) -> value pti { |
| 566 | setverdict(fail, "Received unexpected ProgramTermInd := ", pti); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 567 | } |
| 568 | [] T.timeout { |
| 569 | setverdict(fail, "timeout waiting for user-plane program termination"); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* execute a program and wait for result */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 575 | private function f_start_prog_wait(charstring command, |
| 576 | template integer exit_code := 0, |
| 577 | float tout := 10.0, |
| 578 | boolean redirect_output := true) |
| 579 | runs on PGW_Session_CT |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 580 | { |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 581 | var integer pid := f_start_prog(command, redirect_output); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 582 | f_wait_term(pid, exit_code, tout); |
| 583 | } |
| 584 | |
| 585 | /* execute ping command and wait for result */ |
| 586 | private function f_ping4(charstring host, integer interval := 1, integer count := 10) runs on PGW_Session_CT |
| 587 | { |
| 588 | var charstring ping :="ping -c " & int2str(count) & " -i " & int2str(interval); |
| 589 | ping := ping & " -I " & f_inet_ntoa(g_ip4_addr); |
| 590 | ping := ping & " " & host; |
Harald Welte | 8cfdc7c | 2020-04-21 22:48:34 +0200 | [diff] [blame] | 591 | f_start_prog_wait(ping, tout := int2float(5 + interval*count)); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | /* send echo request; expect response */ |
| 598 | testcase TC_tx_echo() runs on PGW_Test_CT { |
| 599 | timer T := 5.0; |
| 600 | |
| 601 | f_init(); |
| 602 | |
| 603 | TEID0.send(ts_GTP2C_EchoReq(0)); |
| 604 | T.start; |
| 605 | alt { |
| 606 | [] TEID0.receive(tr_GTP2C_EchoResp) { |
| 607 | setverdict(pass); |
| 608 | } |
| 609 | [] T.timeout { |
| 610 | setverdict(fail, "timeout waiting for Echo Response"); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* create a session, expect it to succeed */ |
| 616 | private function f_TC_createSession() runs on PGW_Session_CT { |
| 617 | f_create_session(); |
| 618 | setverdict(pass); |
| 619 | } |
| 620 | testcase TC_createSession() runs on PGW_Test_CT { |
| 621 | var PGW_Session_CT vc_conn; |
| 622 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun22")); |
| 623 | f_init(); |
| 624 | vc_conn := f_start_handler(refers(f_TC_createSession), pars); |
| 625 | vc_conn.done; |
| 626 | } |
| 627 | |
| 628 | /* create a session, then execute a ping command on the user plane */ |
| 629 | private function f_TC_createSession_ping4() runs on PGW_Session_CT { |
| 630 | f_create_session(); |
| 631 | f_ping4(mp_ping_hostname); |
| 632 | setverdict(pass); |
| 633 | } |
| 634 | testcase TC_createSession_ping4() runs on PGW_Test_CT { |
| 635 | var PGW_Session_CT vc_conn; |
| 636 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 637 | f_init(); |
| 638 | vc_conn := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 639 | vc_conn.done; |
| 640 | } |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 641 | testcase TC_createSession_ping4_256() runs on PGW_Test_CT { |
| 642 | var PGW_Session_CT vc_conn[256]; |
| 643 | var integer i; |
| 644 | |
| 645 | f_init(); |
| 646 | |
| 647 | for (i := 0; i < sizeof(vc_conn); i:=i+1) { |
| 648 | var charstring tundev := "ping" & int2str(i); |
| 649 | var SessionPars pars := valueof(t_SessionPars(f_gen_imsi(i), tundev)); |
| 650 | vc_conn[i] := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 651 | } |
| 652 | |
| 653 | for (i := 0; i < lengthof(vc_conn); i:=i+1) { |
| 654 | vc_conn[i].done; |
| 655 | } |
| 656 | } |
| 657 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 658 | |
| 659 | /* create a session, then delete it again */ |
| 660 | private function f_TC_createSession_deleteSession() runs on PGW_Session_CT { |
| 661 | f_create_session(); |
| 662 | f_delete_session(omit, g_teic, '10'O); |
| 663 | setverdict(pass); |
| 664 | } |
| 665 | testcase TC_createSession_deleteSession() runs on PGW_Test_CT { |
| 666 | var PGW_Session_CT vc_conn; |
| 667 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 668 | f_init(); |
| 669 | vc_conn := f_start_handler(refers(f_TC_createSession_deleteSession), pars); |
| 670 | vc_conn.done; |
| 671 | } |
| 672 | |
| 673 | /* send a DeleteSessionReq for an unknown/invalid TEID */ |
| 674 | private function f_TC_deleteSession_unknown() runs on PGW_Session_CT { |
| 675 | g_teic := f_gtp2_allocate_teid(); |
| 676 | g_teic_remote := f_rnd_octstring(4); |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame] | 677 | f_delete_session(omit, '00000000'O, '40'O /* Context Unknown */, false); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 678 | setverdict(pass); |
| 679 | } |
| 680 | testcase TC_deleteSession_unknown() runs on PGW_Test_CT { |
| 681 | var PGW_Session_CT vc_conn; |
| 682 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 683 | f_init(); |
| 684 | vc_conn := f_start_handler(refers(f_TC_deleteSession_unknown), pars); |
| 685 | vc_conn.done; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | control { |
| 692 | execute( TC_tx_echo() ); |
| 693 | execute( TC_createSession() ); |
| 694 | execute( TC_createSession_ping4() ); |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 695 | execute( TC_createSession_ping4_256() ); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 696 | execute( TC_createSession_deleteSession() ); |
| 697 | execute( TC_deleteSession_unknown() ); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | } |