Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 1 | module PGW_Tests { |
| 2 | |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 3 | import from TCCEncoding_Functions all; |
| 4 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 5 | import from General_Types all; |
| 6 | import from Osmocom_Types all; |
| 7 | import from Native_Functions all; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 8 | import from Misc_Helpers all; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 9 | |
| 10 | import from GTPv2_Types all; |
| 11 | import from GTPv2_Templates all; |
| 12 | import from GTPv2_Emulation all; |
| 13 | |
| 14 | import from UECUPS_Types all; |
| 15 | |
| 16 | import from DNS_Helpers all; |
| 17 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 18 | |
| 19 | import from DIAMETER_Types all; |
| 20 | import from DIAMETER_Templates all; |
Pau Espin Pedrol | 117a94f | 2023-12-21 16:10:12 +0100 | [diff] [blame] | 21 | import from DIAMETER_ts29_272_Templates all; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 22 | import from DIAMETER_Emulation all; |
| 23 | |
| 24 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 25 | modulepar { |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 26 | charstring mp_pgw_hostname := "127.0.0.4"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 27 | charstring mp_local_hostname_c := "127.0.0.1"; |
| 28 | charstring mp_local_hostname_u := "127.0.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 29 | |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 30 | charstring mp_run_prog_log_path := "/tmp"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 31 | charstring mp_run_prog_as_user := "laforge"; |
| 32 | charstring mp_ping_hostname := "10.45.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 33 | |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 34 | charstring mp_pcrf_local_ip := "127.0.0.9"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 35 | integer mp_pcrf_local_port := 3868; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 36 | |
| 37 | charstring mp_ocs_local_ip := "127.0.0.9"; |
| 38 | integer mp_ocs_local_port := 3869; |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 39 | |
| 40 | charstring mp_diam_realm := "localdomain"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* main component, we typically have one per testcase */ |
| 44 | type component PGW_Test_CT { |
| 45 | var GTPv2_Emulation_CT vc_GTP2; |
| 46 | port GTP2EM_PT TEID0; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 47 | |
| 48 | /* emulated PCRF */ |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 49 | var DIAMETER_Emulation_CT vc_Gx; |
| 50 | port DIAMETER_PT Gx_UNIT; |
| 51 | port DIAMETEREM_PROC_PT Gx_PROC; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 52 | /* emulated OCS */ |
| 53 | var DIAMETER_Emulation_CT vc_Gy; |
| 54 | port DIAMETER_PT Gy_UNIT; |
| 55 | port DIAMETEREM_PROC_PT Gy_PROC; |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 56 | /* global test case guard timer (actual timeout value is set in f_init()) */ |
| 57 | timer T_guard; |
| 58 | } |
| 59 | |
| 60 | /* global altstep for global guard timer; */ |
| 61 | altstep as_Tguard() runs on PGW_Test_CT { |
| 62 | [] T_guard.timeout { |
| 63 | setverdict(fail, "Timeout of T_guard"); |
| 64 | mtc.stop; |
| 65 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 68 | type component DIAMETER_ConnHdlr_CT extends DIAMETER_ConnHdlr { |
| 69 | port DIAMETER_Conn_PT DIAMETER_CLIENT; |
| 70 | } |
| 71 | |
| 72 | function f_diam_connhldr_ct_main(hexstring imsi) runs on DIAMETER_ConnHdlr_CT { |
| 73 | var PDU_DIAMETER msg; |
| 74 | |
| 75 | if (DIAMETER_PROC.checkstate("Connected")) { |
Pau Espin Pedrol | db017f4 | 2023-08-25 19:22:25 +0200 | [diff] [blame] | 76 | f_diameter_expect_imsi(imsi); |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | while (true) { |
| 80 | alt { |
| 81 | [] DIAMETER_CLIENT.receive(PDU_DIAMETER:?) -> value msg { |
| 82 | DIAMETER.send(msg); |
| 83 | } |
| 84 | [] DIAMETER.receive(PDU_DIAMETER:?) -> value msg { |
| 85 | DIAMETER_CLIENT.send(msg); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 92 | /* per-session component; we typically have 1..N per testcase */ |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 93 | type component PGW_Session_CT extends GTP2_ConnHdlr { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 94 | var SessionPars g_pars; |
| 95 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 96 | port DIAMETER_Conn_PT Gx; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 97 | port DIAMETER_Conn_PT Gy; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 98 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 99 | /* TEI (Data) local side */ |
| 100 | var OCT4 g_teid; |
| 101 | /* TEI (Control) local side */ |
| 102 | var OCT4 g_teic; |
| 103 | /* TEI (Data) remote side */ |
| 104 | var OCT4 g_teid_remote; |
| 105 | /* TEI (Control) remote side */ |
| 106 | var OCT4 g_teic_remote; |
| 107 | /* GTP-U IPv4 address remote sie */ |
| 108 | var OCT4 g_gtpu4_remote; |
| 109 | var OCT16 g_gtpu6_remote; |
| 110 | |
| 111 | /* Address allocation */ |
| 112 | var OCT4 g_ip4_addr; |
| 113 | var OCT16 g_ip6_addr; |
| 114 | var integer g_ip6_plen; |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 115 | |
| 116 | /* Store last received Gy message */ |
| 117 | var PDU_DIAMETER g_rx_gy; |
Pau Espin Pedrol | 967d871 | 2023-02-28 16:09:32 +0100 | [diff] [blame] | 118 | |
| 119 | /* number of programs started, used as identifier. */ |
| 120 | var integer g_start_prog_count := 0; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* configuration data for a given Session */ |
| 124 | type record SessionPars { |
| 125 | hexstring imsi, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 126 | hexstring msisdn optional, |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 127 | // serving network |
| 128 | integer rat_type, |
| 129 | // flags? |
| 130 | charstring apn, |
| 131 | /* Apn subscribed or non-subscribed */ |
| 132 | boolean selection_mode, |
| 133 | BIT3 pdn_type, |
| 134 | /* PAA */ |
| 135 | /* Max APN Restriction */ |
| 136 | /* APN-AMBR */ |
| 137 | octetstring pco optional, |
| 138 | octetstring epco optional, |
| 139 | /* Bearer Contexts to be created */ |
| 140 | |
| 141 | charstring tun_dev_name, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 142 | charstring tun_netns_name optional, |
| 143 | |
| 144 | /* In seconds. 0 => disabled, !0 => grant over CC-Time period */ |
| 145 | integer gy_validity_time |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template (value) SessionPars |
| 149 | t_SessionPars(hexstring imsi, charstring tundev, integer rat_type := 6, charstring apn := "internet", |
| 150 | boolean selection_mode := false, BIT3 pdn_type := '001'B) := { |
| 151 | imsi := imsi, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 152 | msisdn := '1234'H, |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 153 | rat_type := rat_type, |
| 154 | apn := apn, |
| 155 | selection_mode := selection_mode, |
| 156 | pdn_type := pdn_type, |
| 157 | pco := omit, |
| 158 | epco := omit, |
| 159 | tun_dev_name := tundev, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 160 | tun_netns_name := tundev, |
| 161 | gy_validity_time := 0 |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | type record BearerConfig { |
| 165 | integer eps_bearer_id |
| 166 | } |
| 167 | |
| 168 | type function void_fn() runs on PGW_Session_CT; |
| 169 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 170 | friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg) |
| 171 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER { |
| 172 | DIAMETER_UNIT.send(msg); |
| 173 | return omit; |
| 174 | } |
| 175 | |
| 176 | friend function f_init_diameter(charstring id) runs on PGW_Test_CT { |
| 177 | var DIAMETEROps ops := { |
| 178 | create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback), |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 179 | unitdata_cb := refers(DiameterForwardUnitdataCallback), |
| 180 | raw := false /* handler mode (IMSI based routing) */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 181 | }; |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 182 | var DIAMETER_conn_parameters pars; |
| 183 | |
| 184 | /* Gx setup: */ |
| 185 | pars := { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 186 | remote_ip := mp_pgw_hostname, |
| 187 | remote_sctp_port := -1, |
| 188 | local_ip := mp_pcrf_local_ip, |
| 189 | local_sctp_port := mp_pcrf_local_port, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 190 | origin_host := "pcrf." & mp_diam_realm, |
| 191 | origin_realm := mp_diam_realm, |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 192 | auth_app_id := omit, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 193 | vendor_app_id := c_DIAMETER_3GPP_Gx_AID |
| 194 | }; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 195 | vc_Gx := DIAMETER_Emulation_CT.create(id); |
| 196 | map(vc_Gx:DIAMETER, system:DIAMETER_CODEC_PT); |
| 197 | connect(vc_Gx:DIAMETER_UNIT, self:Gx_UNIT); |
| 198 | connect(vc_Gx:DIAMETER_PROC, self:Gx_PROC); |
| 199 | vc_Gx.start(DIAMETER_Emulation.main(ops, pars, id)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 200 | |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 201 | /* Gy setup: */ |
| 202 | pars := { |
| 203 | remote_ip := mp_pgw_hostname, |
| 204 | remote_sctp_port := -1, |
| 205 | local_ip := mp_ocs_local_ip, |
| 206 | local_sctp_port := mp_ocs_local_port, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 207 | origin_host := "ocs." & mp_diam_realm, |
| 208 | origin_realm := mp_diam_realm, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 209 | auth_app_id := c_DIAMETER_CREDIT_CONTROL_AID, |
| 210 | vendor_app_id := omit |
| 211 | }; |
| 212 | vc_Gy := DIAMETER_Emulation_CT.create(id); |
| 213 | map(vc_Gy:DIAMETER, system:DIAMETER_CODEC_PT); |
| 214 | connect(vc_Gy:DIAMETER_UNIT, self:Gy_UNIT); |
| 215 | connect(vc_Gy:DIAMETER_PROC, self:Gy_PROC); |
| 216 | vc_Gy.start(DIAMETER_Emulation.main(ops, pars, id)); |
| 217 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 218 | f_diameter_wait_capability(Gx_UNIT); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 219 | f_diameter_wait_capability(Gy_UNIT); |
Pau Espin Pedrol | bb5f45f | 2022-04-11 12:36:55 +0200 | [diff] [blame] | 220 | /* Give some time for our emulation to get out of SUSPECT list of SUT (3 watchdong ping-pongs): |
| 221 | * RFC6733 sec 5.1 |
| 222 | * RFC3539 sec 3.4.1 [5] |
| 223 | * https://github.com/freeDiameter/freeDiameter/blob/master/libfdcore/p_psm.c#L49 |
| 224 | */ |
| 225 | f_sleep(1.0); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 228 | private function f_init(float guard_timeout := 60.0) runs on PGW_Test_CT { |
| 229 | T_guard.start(guard_timeout); |
| 230 | activate(as_Tguard()); |
| 231 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 232 | var Gtp2EmulationCfg cfg := { |
| 233 | gtpc_bind_ip := mp_local_hostname_c, |
| 234 | gtpc_bind_port := GTP2C_PORT, |
| 235 | gtpc_remote_ip := mp_pgw_hostname, |
| 236 | gtpc_remote_port := GTP2C_PORT, |
| 237 | sgw_role := true, |
| 238 | use_gtpu_daemon := true |
| 239 | }; |
| 240 | |
| 241 | vc_GTP2 := GTPv2_Emulation_CT.create("GTP2_EM"); |
| 242 | map(vc_GTP2:GTP2C, system:GTP2C); |
| 243 | connect(vc_GTP2:TEID0, self:TEID0); |
| 244 | vc_GTP2.start(GTPv2_Emulation.main(cfg)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 245 | |
| 246 | if (mp_pcrf_local_ip != "") { |
| 247 | f_init_diameter(testcasename()); |
| 248 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 251 | 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] | 252 | runs on PGW_Test_CT return PGW_Session_CT { |
| 253 | var charstring id := testcasename(); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 254 | var DIAMETER_ConnHdlr_CT vc_conn_gx, vc_conn_gy; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 255 | var PGW_Session_CT vc_conn; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 256 | var SessionPars pars; |
| 257 | |
| 258 | if (isvalue(pars_tmpl)) { |
| 259 | pars := valueof(pars_tmpl); |
| 260 | } else { |
| 261 | /*TODO: set default values */ |
| 262 | } |
| 263 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 264 | vc_conn := PGW_Session_CT.create(id); |
| 265 | connect(vc_conn:GTP2, vc_GTP2:CLIENT); |
| 266 | connect(vc_conn:GTP2_PROC, vc_GTP2:CLIENT_PROC); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 267 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 268 | if (isbound(vc_Gx)) { |
| 269 | vc_conn_gx := DIAMETER_ConnHdlr_CT.create(id); |
| 270 | connect(vc_conn_gx:DIAMETER, vc_Gx:DIAMETER_CLIENT); |
| 271 | connect(vc_conn_gx:DIAMETER_PROC, vc_Gx:DIAMETER_PROC); |
| 272 | connect(vc_conn:Gx, vc_conn_gx:DIAMETER_CLIENT); |
| 273 | vc_conn_gx.start(f_diam_connhldr_ct_main(pars.imsi)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 276 | if (isbound(vc_Gy)) { |
| 277 | vc_conn_gy := DIAMETER_ConnHdlr_CT.create(id); |
| 278 | connect(vc_conn_gy:DIAMETER, vc_Gy:DIAMETER_CLIENT); |
| 279 | connect(vc_conn_gy:DIAMETER_PROC, vc_Gy:DIAMETER_PROC); |
| 280 | connect(vc_conn:Gy, vc_conn_gy:DIAMETER_CLIENT); |
| 281 | vc_conn_gy.start(f_diam_connhldr_ct_main(pars.imsi)); |
| 282 | } |
| 283 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 284 | vc_conn.start(f_handler_init(fn, pars)); |
| 285 | return vc_conn; |
| 286 | } |
| 287 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 288 | private function f_handler_init(void_fn fn, SessionPars pars) |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 289 | runs on PGW_Session_CT { |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 290 | g_pars := valueof(pars); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 291 | fn.apply(); |
| 292 | } |
| 293 | |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 294 | private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring { |
| 295 | var integer suffix_len := tot_len - lengthof(prefix); |
| 296 | var charstring suffix_ch := int2str(suffix); |
| 297 | var integer pad_len := suffix_len - lengthof(suffix_ch); |
| 298 | |
| 299 | return prefix & int2hex(0, pad_len) & str2hex(suffix_ch); |
| 300 | } |
| 301 | |
| 302 | function f_gen_imei(integer suffix) return hexstring { |
| 303 | return f_concat_pad(14, '49999'H, suffix); |
| 304 | } |
| 305 | |
| 306 | function f_gen_imsi(integer suffix) return hexstring { |
| 307 | return f_concat_pad(15, '26242'H, suffix); |
| 308 | } |
| 309 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 310 | 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] | 311 | var PDU_DIAMETER rx_dia; |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 312 | [] 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] | 313 | var template (omit) AVP avp; |
| 314 | var octetstring sess_id; |
| 315 | var AVP_Unsigned32 req_num; |
| 316 | |
| 317 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id); |
| 318 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 319 | |
| 320 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number); |
| 321 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 322 | |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 323 | 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] | 324 | req_type, req_num)); |
| 325 | } |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 326 | [] Gx.receive(PDU_DIAMETER:?) -> value rx_dia { |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 327 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 328 | log2str("Received unexpected DIAMETER ", rx_dia)); |
| 329 | } |
| 330 | } |
| 331 | |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 332 | function f_tr_DIA_Gy_CCR(DCC_NONE_CC_Request_Type req_type) |
| 333 | runs on PGW_Session_CT return template (present) PDU_DIAMETER |
| 334 | { |
| 335 | var template (present) PDU_DIAMETER tpl; |
| 336 | var charstring smf_origin_host := "smf." & mp_diam_realm; |
| 337 | var template (present) octetstring imsi := ?; |
| 338 | var template (present) octetstring msisdn := ?; |
| 339 | var template (present) octetstring rat_type := ?; |
| 340 | var template (present) OCT4 charging_char := ?; |
| 341 | var template (present) OCT1 nsapi := ?; |
| 342 | imsi := char2oct(f_dec_TBCD(imsi_hex2oct(g_pars.imsi))); |
| 343 | //msisdn := char2oct(f_dec_TBCD(substr(ctx_val.msisdn, 1, lengthof(ctx_val.msisdn) -1))); |
| 344 | rat_type := int2oct(g_pars.rat_type, 1); |
| 345 | charging_char := char2oct(oct2str('0000'O)); // f_create_session() uses hardcoded chg_car := '0000'O |
| 346 | nsapi := '01'O; // f_create_session() uses hardcoded bearer_id := 1 |
| 347 | select (req_type) { |
| 348 | case (INITIAL_REQUEST) { |
| 349 | tpl := tr_DIAMETER(flags:='11000000'B, cmd_code:=Credit_Control, |
| 350 | avps := superset( |
| 351 | tr_AVP_SessionId, |
| 352 | tr_AVP_OriginHost(smf_origin_host), |
| 353 | tr_AVP_OriginRealm(mp_diam_realm), |
| 354 | tr_AVP_DestinationRealm(mp_diam_realm), |
| 355 | tr_AVP_AuthAppId(int2oct(c_DIAMETER_CREDIT_CONTROL_AID, 4)), |
| 356 | tr_AVP_ServiceContextId, |
| 357 | tr_AVP_CcReqType(req_type), |
| 358 | tr_AVP_CcReqNum(?), |
| 359 | tr_AVP_EventTimestamp(?), |
| 360 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_IMSI), tr_AVP_SubcrIdData(imsi)}), |
| 361 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_E164), tr_AVP_SubcrIdData(msisdn)}), |
| 362 | tr_AVP_RequestedAction(DIRECT_DEBITING), |
| 363 | tr_AVP_3GPP_AoCRequestType, |
| 364 | tr_AVP_MultipleServicesIndicator, |
| 365 | tr_AVP_Multiple_Services_Credit_Control(content := superset( |
| 366 | tr_AVP_Requested_Service_Unit, |
Pau Espin Pedrol | 6477d73 | 2022-06-03 12:04:40 +0200 | [diff] [blame] | 367 | tr_AVP_PCC_3GPP_QoS_Information, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 368 | tr_AVP_GI_3GPP_RatType(rat_type) |
| 369 | )), |
| 370 | tr_AVP_3GPP_ServiceInformation(content := superset( |
| 371 | tr_AVP_3GPP_PSInformation(content := superset( |
| 372 | tr_AVP_3GPP_ChargingId, |
| 373 | tr_AVP_3GPP_PDPType((IPv4,IPv6,IPv4v6)), |
| 374 | tr_AVP_3GPP_PDPAddress(tr_AVP_Address((IP,IP6), ?)), |
| 375 | tr_AVP_3GPP_SGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pcrf_local_ip))), |
| 376 | tr_AVP_3GPP_GGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pgw_hostname))), |
| 377 | tr_AVP_3GPP_CalledStationId, |
| 378 | tr_AVP_3GPP_SelectionMode, |
| 379 | tr_AVP_3GPP_ChargingCharacteristics(charging_char), |
| 380 | tr_AVP_3GPP_SGSNMCCMNC, |
| 381 | tr_AVP_3GPP_NSAPI(nsapi), |
| 382 | /*We don't yet send MS_Tz in CreateSessionReq: |
| 383 | tr_AVP_3GPP_MS_TimeZone,*/ |
| 384 | tr_AVP_3GPP_ULI/*, |
| 385 | We don't yet send IMEI in CreateSessionReq: |
| 386 | tr_AVP_UserEquipmentInfo({ |
| 387 | tr_AVP_UserEquipmentInfoType(IMEISV), |
| 388 | tr_AVP_UserEquipmentInfoValue(imeisv) |
| 389 | })*/ |
| 390 | )) |
| 391 | )) |
| 392 | )); |
| 393 | } |
| 394 | case (UPDATE_REQUEST) { |
| 395 | tpl := tr_DIAMETER(flags:='11000000'B, cmd_code:=Credit_Control, |
| 396 | avps := superset( |
| 397 | tr_AVP_SessionId, |
| 398 | tr_AVP_OriginHost(smf_origin_host), |
| 399 | tr_AVP_OriginRealm(mp_diam_realm), |
| 400 | tr_AVP_DestinationRealm(mp_diam_realm), |
| 401 | tr_AVP_AuthAppId(int2oct(c_DIAMETER_CREDIT_CONTROL_AID, 4)), |
| 402 | tr_AVP_ServiceContextId, |
| 403 | tr_AVP_CcReqType(req_type), |
| 404 | tr_AVP_CcReqNum(?), |
| 405 | tr_AVP_DestinationHost(?), |
| 406 | tr_AVP_EventTimestamp(?), |
| 407 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_IMSI), tr_AVP_SubcrIdData(imsi)}), |
| 408 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_E164), tr_AVP_SubcrIdData(msisdn)}), |
| 409 | tr_AVP_RequestedAction(DIRECT_DEBITING), |
| 410 | tr_AVP_3GPP_AoCRequestType, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 411 | tr_AVP_Multiple_Services_Credit_Control(content := superset( |
| 412 | tr_AVP_Requested_Service_Unit, |
| 413 | tr_AVP_Used_Service_Unit, |
Pau Espin Pedrol | cba0f6d | 2022-05-24 13:49:46 +0200 | [diff] [blame] | 414 | /* tr_AVP_3GPP_Reporting_Reason, can be sometimes inside UsedServiceUnit */ |
Pau Espin Pedrol | 6477d73 | 2022-06-03 12:04:40 +0200 | [diff] [blame] | 415 | tr_AVP_PCC_3GPP_QoS_Information, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 416 | tr_AVP_GI_3GPP_RatType(rat_type) |
| 417 | )), |
| 418 | tr_AVP_3GPP_ServiceInformation(content := superset( |
| 419 | tr_AVP_3GPP_PSInformation(content := superset( |
| 420 | tr_AVP_3GPP_ChargingId, |
| 421 | /* tr_AVP_3GPP_PDPType, Only in INIT */ |
| 422 | tr_AVP_3GPP_PDPAddress(tr_AVP_Address((IP,IP6), ?)), |
| 423 | tr_AVP_3GPP_SGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pcrf_local_ip))), |
| 424 | tr_AVP_3GPP_GGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pgw_hostname))), |
| 425 | tr_AVP_3GPP_CalledStationId, |
| 426 | tr_AVP_3GPP_SelectionMode, |
| 427 | tr_AVP_3GPP_ChargingCharacteristics(charging_char), |
| 428 | tr_AVP_3GPP_SGSNMCCMNC, |
| 429 | tr_AVP_3GPP_NSAPI(nsapi), |
| 430 | /*We don't yet send MS_Tz in CreateSessionReq: |
| 431 | tr_AVP_3GPP_MS_TimeZone,*/ |
| 432 | tr_AVP_3GPP_ULI/*, |
| 433 | We don't yet send IMEI in CreateSessionReq: |
| 434 | tr_AVP_UserEquipmentInfo({ |
| 435 | tr_AVP_UserEquipmentInfoType(IMEISV), |
| 436 | tr_AVP_UserEquipmentInfoValue(imeisv) |
| 437 | })*/ |
| 438 | )) |
| 439 | )) |
| 440 | )); |
| 441 | } |
| 442 | case (TERMINATION_REQUEST) { |
| 443 | tpl := tr_DIAMETER(flags:='11000000'B, cmd_code:=Credit_Control, |
| 444 | avps := superset( |
| 445 | tr_AVP_SessionId, |
| 446 | tr_AVP_OriginHost(smf_origin_host), |
| 447 | tr_AVP_OriginRealm(mp_diam_realm), |
| 448 | tr_AVP_DestinationRealm(mp_diam_realm), |
| 449 | tr_AVP_AuthAppId(int2oct(c_DIAMETER_CREDIT_CONTROL_AID, 4)), |
| 450 | tr_AVP_ServiceContextId, |
| 451 | tr_AVP_CcReqType(req_type), |
| 452 | tr_AVP_CcReqNum(?), |
| 453 | tr_AVP_DestinationHost(?), |
| 454 | tr_AVP_EventTimestamp(?), |
| 455 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_IMSI), tr_AVP_SubcrIdData(imsi)}), |
| 456 | tr_AVP_SubcrId({tr_AVP_SubcrIdType(END_USER_E164), tr_AVP_SubcrIdData(msisdn)}), |
| 457 | tr_AVP_TerminationCause(?), |
| 458 | tr_AVP_RequestedAction(DIRECT_DEBITING), |
| 459 | tr_AVP_3GPP_AoCRequestType, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 460 | tr_AVP_Multiple_Services_Credit_Control(content := superset( |
| 461 | /* tr_AVP_Requested_Service_Unit, Only in INIT and UPDATE */ |
| 462 | tr_AVP_Used_Service_Unit, |
Pau Espin Pedrol | cba0f6d | 2022-05-24 13:49:46 +0200 | [diff] [blame] | 463 | tr_AVP_3GPP_Reporting_Reason(FINAL), |
Pau Espin Pedrol | 6477d73 | 2022-06-03 12:04:40 +0200 | [diff] [blame] | 464 | tr_AVP_PCC_3GPP_QoS_Information, |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 465 | tr_AVP_GI_3GPP_RatType(rat_type) |
| 466 | )), |
| 467 | tr_AVP_3GPP_ServiceInformation(content := superset( |
| 468 | tr_AVP_3GPP_PSInformation(content := superset( |
| 469 | tr_AVP_3GPP_ChargingId, |
| 470 | /* tr_AVP_3GPP_PDPType, Only in INIT */ |
| 471 | tr_AVP_3GPP_PDPAddress(tr_AVP_Address((IP,IP6), ?)), |
| 472 | tr_AVP_3GPP_SGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pcrf_local_ip))), |
| 473 | tr_AVP_3GPP_GGSNAddress(tr_AVP_Address(IP, f_inet_addr(mp_pgw_hostname))), |
| 474 | tr_AVP_3GPP_CalledStationId, |
| 475 | tr_AVP_3GPP_SelectionMode, |
| 476 | tr_AVP_3GPP_ChargingCharacteristics(charging_char), |
| 477 | tr_AVP_3GPP_SGSNMCCMNC, |
| 478 | tr_AVP_3GPP_NSAPI(nsapi), |
| 479 | /*We don't yet send MS_Tz in CreateSessionReq: |
| 480 | tr_AVP_3GPP_MS_TimeZone,*/ |
| 481 | tr_AVP_3GPP_ULI/*, |
| 482 | We don't yet send IMEI in CreateSessionReq: |
| 483 | tr_AVP_UserEquipmentInfo({ |
| 484 | tr_AVP_UserEquipmentInfoType(IMEISV), |
| 485 | tr_AVP_UserEquipmentInfoValue(imeisv) |
| 486 | })*/ |
| 487 | )) |
| 488 | )) |
| 489 | )); |
| 490 | } |
| 491 | } |
| 492 | return tpl; |
| 493 | } |
| 494 | |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 495 | private altstep as_DIA_Gy_CCR(DCC_NONE_CC_Request_Type req_type) |
| 496 | runs on PGW_Session_CT { |
| 497 | [] Gy.receive(f_tr_DIA_Gy_CCR(req_type := req_type)) -> value g_rx_gy { |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 498 | var template (value) PDU_DIAMETER tx_dia; |
| 499 | var template (omit) AVP avp; |
| 500 | var octetstring sess_id; |
| 501 | var AVP_Unsigned32 req_num; |
| 502 | |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 503 | avp := f_DIAMETER_get_avp(g_rx_gy, c_AVP_Code_BASE_NONE_Session_Id); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 504 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 505 | |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 506 | avp := f_DIAMETER_get_avp(g_rx_gy, c_AVP_Code_DCC_NONE_CC_Request_Number); |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 507 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 508 | if (g_pars.gy_validity_time > 0) { |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 509 | tx_dia := ts_DIA_Gy_CCA_ValidityTime(g_rx_gy.hop_by_hop_id, g_rx_gy.end_to_end_id, sess_id, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 510 | req_type, req_num, g_pars.gy_validity_time); |
| 511 | } else { |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 512 | tx_dia := ts_DIA_Gy_CCA(g_rx_gy.hop_by_hop_id, g_rx_gy.end_to_end_id, sess_id, |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 513 | req_type, req_num); |
| 514 | } |
| 515 | Gy.send(tx_dia); |
| 516 | } |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 517 | [] Gy.receive(PDU_DIAMETER:?) -> value g_rx_gy { |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 518 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 519 | log2str("Received unexpected DIAMETER Gy", g_rx_gy)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 523 | |
| 524 | /* find TEID of given interface type (and optionally instance) */ |
| 525 | private function f_find_teid(FullyQualifiedTEID_List list, |
| 526 | template (present) integer if_type, |
| 527 | template (present) BIT4 instance := ?) |
| 528 | return template (omit) FullyQualifiedTEID |
| 529 | { |
| 530 | var integer i; |
| 531 | for (i := 0; i < lengthof(list); i := i+1) { |
| 532 | if (match(list[i].interfaceType, if_type) and |
| 533 | match(list[i].instance, instance)) { |
| 534 | return list[i]; |
| 535 | } |
| 536 | } |
| 537 | return omit; |
| 538 | } |
| 539 | |
| 540 | /* process one to-be-created bearer context */ |
| 541 | private function process_bctx_create(BearerContextGrouped bctx) runs on PGW_Session_CT |
| 542 | { |
| 543 | /* FIXME: EPS Bearer ID */ |
| 544 | /* FIXME: Cause */ |
| 545 | |
| 546 | /* find F-TEID of the P-GW U side */ |
| 547 | var FullyQualifiedTEID rx_fteid; |
| 548 | rx_fteid := valueof(f_find_teid(bctx.bearerContextIEs.fullyQualifiedTEID, 5, '0010'B)); |
| 549 | g_teid_remote := rx_fteid.tEID_GRE_Key; |
| 550 | if (rx_fteid.v4_Flag == '1'B) { |
| 551 | g_gtpu4_remote := rx_fteid.iPv4_Address; |
| 552 | } |
| 553 | if (rx_fteid.v6_Flag == '1'B) { |
| 554 | g_gtpu6_remote := rx_fteid.iPv6_Address; |
| 555 | } |
| 556 | |
| 557 | var UECUPS_CreateTun uecups_create := { |
| 558 | tx_teid := oct2int(g_teid_remote), |
| 559 | rx_teid := oct2int(g_teid), |
| 560 | user_addr_type := IPV4, |
| 561 | user_addr := '00000000'O, |
| 562 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 563 | remote_gtp_ep := valueof(ts_UECUPS_SockAddr(g_gtpu4_remote)), |
| 564 | tun_dev_name := g_pars.tun_dev_name, |
| 565 | tun_netns_name := g_pars.tun_netns_name |
| 566 | }; |
| 567 | |
| 568 | /* create tunnel in daemon */ |
| 569 | if (isbound(g_ip4_addr)) { |
| 570 | uecups_create.user_addr := g_ip4_addr; |
| 571 | f_gtp2_create_tunnel(uecups_create); |
| 572 | } |
| 573 | if (isbound(g_ip6_addr)) { |
| 574 | uecups_create.user_addr_type := IPV6; |
| 575 | uecups_create.user_addr := g_ip6_addr; |
| 576 | f_gtp2_create_tunnel(uecups_create); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* create a session on the PGW */ |
| 581 | private function f_create_session() runs on PGW_Session_CT { |
| 582 | var PDU_GTPCv2 rx; |
| 583 | |
| 584 | /* allocate + register TEID-C on local side */ |
| 585 | g_teic := f_gtp2_allocate_teid(); |
| 586 | g_teid := g_teic; |
| 587 | |
| 588 | var template (value) FullyQualifiedTEID fteid_c_ie, fteid_u_ie; |
| 589 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 590 | f_inet_addr(mp_local_hostname_c), omit); |
| 591 | fteid_u_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPU, g_teid, 2, |
| 592 | f_inet_addr(mp_local_hostname_u), omit); |
| 593 | var template (value) PDU_GTPCv2 g2c := |
Pau Espin Pedrol | e343a88 | 2022-05-18 18:51:11 +0200 | [diff] [blame] | 594 | ts_GTP2C_CreateSessionReq(imsi := g_pars.imsi, msisdn := g_pars.msisdn, rat_type := 6, |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 595 | sender_fteid := fteid_c_ie, |
| 596 | apn := f_enc_dns_hostname(g_pars.apn), |
| 597 | pdn_type := g_pars.pdn_type, teid_list := { fteid_u_ie }, |
| 598 | chg_car := '0000'O, bearer_id := 1); |
| 599 | /* open5gs up to 1.2.3 won't accept it without ULI, despite not mandatory */ |
| 600 | var template (value) TAI tai := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0001'O }; |
| 601 | var template (value) ECGI ecgi := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0'H, 23 }; |
| 602 | g2c.gtpcv2_pdu.createSessionRequest.userLocationInfo := ts_GTP2C_UserLocInfo(tai := tai, ecgi := ecgi); |
Vadim Yanitskiy | bada3c9 | 2022-01-20 18:59:07 +0600 | [diff] [blame] | 603 | g2c.gtpcv2_pdu.createSessionRequest.servingNetwork := ts_GTP2C_ServingNetwork('001'H, '01F'H); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 604 | |
| 605 | GTP2.send(g2c); |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 606 | if (Gx.checkstate("Connected")) { |
| 607 | as_DIA_Gx_CCR(INITIAL_REQUEST); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 608 | } |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 609 | if (Gy.checkstate("Connected")) { |
| 610 | as_DIA_Gy_CCR(INITIAL_REQUEST); |
| 611 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 612 | alt { |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 613 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=Request_accepted)) -> value rx { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 614 | /* extract TEIDs */ |
| 615 | var CreateSessionResponse resp := rx.gtpcv2_pdu.createSessionResponse; |
| 616 | g_teic_remote := resp.fullyQualifiedTEID[0].tEID_GRE_Key; |
| 617 | |
| 618 | /* extract allocated address[es] */ |
| 619 | var PDN_Address_and_Prefix paa := resp.pDN_AddressAllocation.pDN_Address_and_Prefix; |
| 620 | if (ischosen(paa.iPv4_Address)) { |
| 621 | g_ip4_addr := paa.iPv4_Address; |
| 622 | } else if (ischosen(paa.iPv6_Address)) { |
| 623 | g_ip6_addr := paa.iPv6_Address.iPv6_Address; |
| 624 | g_ip6_plen := paa.iPv6_Address.prefixLength; |
| 625 | } else if (ischosen(paa.iPv4_IPv6)) { |
| 626 | g_ip4_addr := paa.iPv4_IPv6.iPv4_Address; |
| 627 | g_ip6_addr := paa.iPv4_IPv6.iPv6_Address; |
| 628 | g_ip6_plen := paa.iPv4_IPv6.prefixLength; |
| 629 | } |
| 630 | var integer i; |
| 631 | for (i := 0; i < lengthof(resp.bearerContextGrouped); i := i+1) { |
| 632 | var BearerContextGrouped bctx := resp.bearerContextGrouped[i]; |
| 633 | select (bctx.instance) { |
| 634 | case ('0000'B) { // created |
| 635 | process_bctx_create(bctx); |
| 636 | } |
| 637 | case ('0001'B) { // removed |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 638 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 639 | "We don't expect removed bearer contexts yet"); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=?)) -> value rx { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 645 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 646 | log2str("Unexpected CreateSessionResp(cause=", |
| 647 | rx.gtpcv2_pdu.createSessionResponse.cause.causeValue, ")")); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 648 | } |
| 649 | [] GTP2.receive { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 650 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 651 | "Unexpected GTPv2 while waiting for CreateSessionResp"); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | } |
| 656 | |
| 657 | /* delete the session from the PGW */ |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 658 | private function f_delete_session(template (omit) GTP2C_Cause tx_cause := omit, |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 659 | template (present) OCT4 exp_teid, |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 660 | template (present) GTP2C_Cause exp_cause, |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame] | 661 | boolean expect_diameter := true) runs on PGW_Session_CT { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 662 | var template (value) FullyQualifiedTEID fteid_c_ie |
| 663 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 664 | f_inet_addr(mp_local_hostname_c), omit); |
| 665 | var template PDU_GTPCv2 g2c := |
| 666 | ts_GTP2C_DeleteSessionReq(d_teid := g_teic_remote, cause := tx_cause, |
| 667 | sender_fteid := fteid_c_ie, |
| 668 | teid_list := {}, bearer_id := 1); |
| 669 | |
| 670 | GTP2.send(g2c); |
Pau Espin Pedrol | 8b3123f | 2022-04-20 17:17:05 +0200 | [diff] [blame] | 671 | if (Gx.checkstate("Connected") and expect_diameter) { |
| 672 | as_DIA_Gx_CCR(TERMINATION_REQUEST); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 673 | } |
Pau Espin Pedrol | 251d064 | 2022-04-20 18:16:45 +0200 | [diff] [blame] | 674 | if (Gy.checkstate("Connected") and expect_diameter) { |
| 675 | as_DIA_Gy_CCR(TERMINATION_REQUEST); |
| 676 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 677 | alt { |
| 678 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(d_teid := exp_teid, cause := exp_cause)) { |
| 679 | setverdict(pass); |
| 680 | } |
| 681 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(?, ?)) { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 682 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 683 | "Unexpected DeleteSessionResp"); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 684 | } |
| 685 | [] GTP2.receive { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 686 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 687 | "Unexpected GTPv2 while waiting for DeleteSessionResp"); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | /* destroy tunnel in daemon */ |
| 692 | if (isbound(g_teid)) { |
| 693 | var UECUPS_DestroyTun uecups_destroy := { |
| 694 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 695 | rx_teid := oct2int(g_teid) |
| 696 | }; |
| 697 | /* FIXME: what about IPv4/IPv6 differentiation? */ |
| 698 | f_gtp2_destroy_tunnel(uecups_destroy); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* start a program on the user plane side; return its PID */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 703 | private function f_start_prog(charstring command, boolean redirect_output := true) |
| 704 | runs on PGW_Session_CT return integer |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 705 | { |
| 706 | var UECUPS_StartProgram sprog := { |
| 707 | command := command, |
| 708 | environment := {}, |
| 709 | run_as_user := mp_run_prog_as_user, |
| 710 | tun_netns_name := g_pars.tun_netns_name |
| 711 | }; |
Pau Espin Pedrol | 967d871 | 2023-02-28 16:09:32 +0100 | [diff] [blame] | 712 | g_start_prog_count := g_start_prog_count + 1; |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 713 | |
| 714 | /* Redirect stdout/stderr to the user-specified location */ |
| 715 | if (redirect_output) { |
Pau Espin Pedrol | 967d871 | 2023-02-28 16:09:32 +0100 | [diff] [blame] | 716 | var charstring id := testcasename() & "-" & hex2str(g_pars.imsi) & "-" & int2str(g_start_prog_count); |
| 717 | var charstring prefix := mp_run_prog_log_path & "/" & id; |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 718 | sprog.command := sprog.command & " 1>>" & prefix & ".prog.stdout"; |
| 719 | sprog.command := sprog.command & " 2>>" & prefix & ".prog.stderr"; |
| 720 | } |
| 721 | |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 722 | log("Starting a program: ", command); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 723 | var UECUPS_StartProgramRes res := f_gtp2_start_program(sprog); |
| 724 | if (res.result != OK) { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 725 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 726 | log2str("Unable to start program '", command, "'")); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 727 | } |
Pau Espin Pedrol | 967d871 | 2023-02-28 16:09:32 +0100 | [diff] [blame] | 728 | log("Started program '", command, "' with PID ", res.pid); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 729 | return res.pid; |
| 730 | } |
| 731 | |
| 732 | /* wait for termination of a given PID with specified exit_code */ |
| 733 | private function f_wait_term(integer pid, template (present) integer exit_code := 0, |
| 734 | float tout := 10.0) runs on PGW_Session_CT |
| 735 | { |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 736 | var UECUPS_ProgramTermInd pti; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 737 | timer T := tout; |
| 738 | |
| 739 | T.start; |
| 740 | alt { |
| 741 | [] GTP2.receive(UECUPS_ProgramTermInd:{pid := pid, exit_code := exit_code}) { |
| 742 | setverdict(pass); |
| 743 | } |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 744 | [] GTP2.receive(UECUPS_ProgramTermInd:?) -> value pti { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 745 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
| 746 | log2str("Received unexpected ProgramTermInd := ", pti)); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 747 | } |
| 748 | [] T.timeout { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 749 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, |
Pau Espin Pedrol | 967d871 | 2023-02-28 16:09:32 +0100 | [diff] [blame] | 750 | log2str("timeout (", tout, " seconds) waiting for user-plane program PID ", pid, " termination")); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* execute a program and wait for result */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 756 | private function f_start_prog_wait(charstring command, |
| 757 | template integer exit_code := 0, |
| 758 | float tout := 10.0, |
| 759 | boolean redirect_output := true) |
| 760 | runs on PGW_Session_CT |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 761 | { |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 762 | var integer pid := f_start_prog(command, redirect_output); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 763 | f_wait_term(pid, exit_code, tout); |
| 764 | } |
| 765 | |
| 766 | /* execute ping command and wait for result */ |
| 767 | private function f_ping4(charstring host, integer interval := 1, integer count := 10) runs on PGW_Session_CT |
| 768 | { |
| 769 | var charstring ping :="ping -c " & int2str(count) & " -i " & int2str(interval); |
Pau Espin Pedrol | 55980c4 | 2023-02-28 12:40:52 +0100 | [diff] [blame] | 770 | |
| 771 | if (not isbound(g_ip4_addr)) { |
| 772 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "f_ping4(): g_ip4_addr is unset!"); |
| 773 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 774 | ping := ping & " -I " & f_inet_ntoa(g_ip4_addr); |
| 775 | ping := ping & " " & host; |
Harald Welte | 8cfdc7c | 2020-04-21 22:48:34 +0200 | [diff] [blame] | 776 | f_start_prog_wait(ping, tout := int2float(5 + interval*count)); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | /* send echo request; expect response */ |
| 783 | testcase TC_tx_echo() runs on PGW_Test_CT { |
| 784 | timer T := 5.0; |
| 785 | |
| 786 | f_init(); |
| 787 | |
| 788 | TEID0.send(ts_GTP2C_EchoReq(0)); |
| 789 | T.start; |
| 790 | alt { |
| 791 | [] TEID0.receive(tr_GTP2C_EchoResp) { |
| 792 | setverdict(pass); |
| 793 | } |
| 794 | [] T.timeout { |
Pau Espin Pedrol | f97f6e2 | 2023-02-28 14:13:42 +0100 | [diff] [blame] | 795 | Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "timeout waiting for Echo Response"); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* create a session, expect it to succeed */ |
| 801 | private function f_TC_createSession() runs on PGW_Session_CT { |
| 802 | f_create_session(); |
| 803 | setverdict(pass); |
| 804 | } |
| 805 | testcase TC_createSession() runs on PGW_Test_CT { |
| 806 | var PGW_Session_CT vc_conn; |
| 807 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun22")); |
| 808 | f_init(); |
| 809 | vc_conn := f_start_handler(refers(f_TC_createSession), pars); |
| 810 | vc_conn.done; |
| 811 | } |
| 812 | |
| 813 | /* create a session, then execute a ping command on the user plane */ |
| 814 | private function f_TC_createSession_ping4() runs on PGW_Session_CT { |
| 815 | f_create_session(); |
| 816 | f_ping4(mp_ping_hostname); |
| 817 | setverdict(pass); |
| 818 | } |
| 819 | testcase TC_createSession_ping4() runs on PGW_Test_CT { |
| 820 | var PGW_Session_CT vc_conn; |
| 821 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 822 | f_init(); |
| 823 | vc_conn := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 824 | vc_conn.done; |
| 825 | } |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 826 | testcase TC_createSession_ping4_256() runs on PGW_Test_CT { |
| 827 | var PGW_Session_CT vc_conn[256]; |
| 828 | var integer i; |
| 829 | |
| 830 | f_init(); |
| 831 | |
| 832 | for (i := 0; i < sizeof(vc_conn); i:=i+1) { |
| 833 | var charstring tundev := "ping" & int2str(i); |
| 834 | var SessionPars pars := valueof(t_SessionPars(f_gen_imsi(i), tundev)); |
| 835 | vc_conn[i] := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 836 | } |
| 837 | |
| 838 | for (i := 0; i < lengthof(vc_conn); i:=i+1) { |
| 839 | vc_conn[i].done; |
| 840 | } |
| 841 | } |
| 842 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 843 | |
| 844 | /* create a session, then delete it again */ |
| 845 | private function f_TC_createSession_deleteSession() runs on PGW_Session_CT { |
| 846 | f_create_session(); |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 847 | f_delete_session(omit, g_teic, Request_accepted); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 848 | setverdict(pass); |
| 849 | } |
| 850 | testcase TC_createSession_deleteSession() runs on PGW_Test_CT { |
| 851 | var PGW_Session_CT vc_conn; |
| 852 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 853 | f_init(); |
| 854 | vc_conn := f_start_handler(refers(f_TC_createSession_deleteSession), pars); |
| 855 | vc_conn.done; |
| 856 | } |
| 857 | |
| 858 | /* send a DeleteSessionReq for an unknown/invalid TEID */ |
| 859 | private function f_TC_deleteSession_unknown() runs on PGW_Session_CT { |
| 860 | g_teic := f_gtp2_allocate_teid(); |
| 861 | g_teic_remote := f_rnd_octstring(4); |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 862 | f_delete_session(omit, '00000000'O, Context_Not_Found, false); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 863 | setverdict(pass); |
| 864 | } |
| 865 | testcase TC_deleteSession_unknown() runs on PGW_Test_CT { |
| 866 | var PGW_Session_CT vc_conn; |
| 867 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 868 | f_init(); |
| 869 | vc_conn := f_start_handler(refers(f_TC_deleteSession_unknown), pars); |
| 870 | vc_conn.done; |
| 871 | } |
| 872 | |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 873 | /* Test charging over Gy interface. */ |
| 874 | private function f_TC_gy_charging_cc_time() runs on PGW_Session_CT { |
| 875 | var default d; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 876 | |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 877 | f_create_session(); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 878 | |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 879 | /* We should receive an update even if no traffic is sent: */ |
| 880 | as_DIA_Gy_CCR(UPDATE_REQUEST); |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 881 | f_validate_gy_cc_report(g_rx_gy, VALIDITY_TIME, (3..4), 0, 0); |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 882 | |
| 883 | d := activate(as_DIA_Gy_CCR(UPDATE_REQUEST)); |
| 884 | f_ping4(mp_ping_hostname); |
| 885 | /* Let the CCA reach the GGSN */ |
| 886 | f_sleep(0.5); |
| 887 | deactivate(d); |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 888 | f_validate_gy_cc_report(g_rx_gy, VALIDITY_TIME, (3..4), (28..1000), (28..1000)); |
| 889 | |
| 890 | as_DIA_Gy_CCR(UPDATE_REQUEST); |
| 891 | f_validate_gy_cc_report(g_rx_gy, VALIDITY_TIME, (3..4), ?, ?); |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 892 | |
Pau Espin Pedrol | 1344e47 | 2023-10-23 19:54:29 +0200 | [diff] [blame] | 893 | f_delete_session(omit, g_teic, Request_accepted); |
Pau Espin Pedrol | 2fde1ef | 2022-05-20 18:53:09 +0200 | [diff] [blame] | 894 | f_validate_gy_cc_report(g_rx_gy, FINAL, (0..1), 0, 0); |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 895 | setverdict(pass); |
| 896 | } |
| 897 | testcase TC_gy_charging_cc_time() runs on PGW_Test_CT { |
| 898 | var PGW_Session_CT vc_conn; |
| 899 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 900 | pars.gy_validity_time := 3; /* Grant access for 3 seconds, needs to be re-validated afterwards */ |
| 901 | f_init(); |
| 902 | vc_conn := f_start_handler(refers(f_TC_gy_charging_cc_time), pars); |
| 903 | vc_conn.done; |
| 904 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 905 | |
| 906 | control { |
| 907 | execute( TC_tx_echo() ); |
| 908 | execute( TC_createSession() ); |
| 909 | execute( TC_createSession_ping4() ); |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 910 | execute( TC_createSession_ping4_256() ); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 911 | execute( TC_createSession_deleteSession() ); |
| 912 | execute( TC_deleteSession_unknown() ); |
Pau Espin Pedrol | 431f346 | 2022-04-20 18:45:35 +0200 | [diff] [blame] | 913 | execute( TC_gy_charging_cc_time() ); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | |
| 917 | } |