blob: b802564ee23c1df5e9adb4d92998e4beee4e7dfa [file] [log] [blame]
Harald Welte379d45a2017-08-03 09:55:15 +02001module GGSN_Tests {
2
Harald Welte94ade362017-08-04 00:36:55 +02003 import from General_Types all;
Harald Welte811651e2017-08-05 15:25:06 +02004 import from Osmocom_Types all;
Harald Welte94ade362017-08-04 00:36:55 +02005 import from IPL4asp_PortType all;
6 import from IPL4asp_Types all;
7 import from GTP_CodecPort all;
8 import from GTP_CodecPort_CtrlFunct all;
9 import from GTPC_Types all;
10 import from GTPU_Types all;
Harald Welte71a36022017-12-04 18:55:58 +010011 import from IPCP_Types all;
Harald Welte231b9412017-08-09 17:16:31 +020012 import from IP_Types all;
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +010013 import from ICMP_Types all;
Harald Welte231b9412017-08-09 17:16:31 +020014 import from ICMPv6_Types all;
Harald Welteddeecbb2017-08-18 22:53:30 +020015 import from Native_Functions all;
Harald Welte94ade362017-08-04 00:36:55 +020016
17 const integer GTP0_PORT := 3386;
18 const integer GTP1C_PORT := 2123;
19 const integer GTP1U_PORT := 2152;
Harald Welteddeecbb2017-08-18 22:53:30 +020020
21 modulepar {
22 charstring m_bind_ip_gtpc := "127.23.42.1";
23 charstring m_bind_ip_gtpu := "127.23.42.1";
24
25 charstring m_ggsn_ip_gtpc := "127.0.0.6";
26 charstring m_ggsn_ip_gtpu := "127.0.0.6";
Pau Espin Pedrolf69a4382018-01-29 13:09:00 +010027
28 charstring m_ggsn_ip4_dns1 := "192.168.100.1"
29 charstring m_ggsn_ip4_dns2 := "8.8.8.8"
Pau Espin Pedrol363ba482018-01-29 18:42:00 +010030 charstring m_ggsn_ip6_dns1 := "2001:4860:4860::8888"
31 charstring m_ggsn_ip6_dns2 := "2001:4860:4860::8844"
Harald Welteddeecbb2017-08-18 22:53:30 +020032 }
Harald Welte94ade362017-08-04 00:36:55 +020033
Harald Welte811651e2017-08-05 15:25:06 +020034 type set PdpContext {
35 hexstring imsi,
36 octetstring msisdn optional,
37 octetstring apn,
Harald Welteed7a1772017-08-09 20:26:20 +020038 ProtConfigOptions pco_req optional,
39 ProtConfigOptions pco_neg optional,
Harald Welte811651e2017-08-05 15:25:06 +020040 EndUserAddress eua,
Harald Welte231b9412017-08-09 17:16:31 +020041 OCT16 ip6_prefix optional,
Harald Welte811651e2017-08-05 15:25:06 +020042 BIT4 nsapi,
43 /* TEI (Data) local side */
44 OCT4 teid,
45 /* TEI (Control) local side */
46 OCT4 teic,
47 /* TEI (Data) remote side */
48 OCT4 teid_remote,
49 /* TEI (Control) remote side */
Harald Welte5438b9d2017-08-13 13:27:48 +020050 OCT4 teic_remote
Harald Welte811651e2017-08-05 15:25:06 +020051 }
52
Harald Welte94ade362017-08-04 00:36:55 +020053 type component GT_CT {
54 port GTPC_PT GTPC;
55 port GTPU_PT GTPU;
56
Harald Welte0be142b2017-08-13 13:28:10 +020057 var boolean g_initialized := false;
58
Harald Welte94ade362017-08-04 00:36:55 +020059 var OCT1 g_restart_ctr := '01'O;
60 /* FIXME: unify with g_bind_ip + parse from config file */
Harald Welteddeecbb2017-08-18 22:53:30 +020061 var OCT4 g_sgsn_ip_c;
62 var OCT4 g_sgsn_ip_u;
Harald Welte94ade362017-08-04 00:36:55 +020063 /* FIXME: parse remName from config file */
Harald Welteddeecbb2017-08-18 22:53:30 +020064 var GtpPeer g_peer_c := { connId := 0, remName := m_ggsn_ip_gtpc, remPort := GTP1C_PORT };
65 var GtpPeer g_peer_u := { connId := 0, remName := m_ggsn_ip_gtpu, remPort := GTP1U_PORT };
Harald Welte94ade362017-08-04 00:36:55 +020066 timer T_default := 3.0;
Harald Welte5438b9d2017-08-13 13:27:48 +020067
68 /* next to-be-sent GTP-C sequence number */
69 var uint16_t g_c_seq_nr;
70 /* next to-be-sent GTP-U sequence number */
71 var uint16_t g_d_seq_nr;
Harald Welte94ade362017-08-04 00:36:55 +020072 }
73
74 function f_init() runs on GT_CT {
Harald Welte0be142b2017-08-13 13:28:10 +020075 if (g_initialized == true) {
76 return;
77 }
78 g_initialized := true;
79
Harald Welteddeecbb2017-08-18 22:53:30 +020080 g_sgsn_ip_c := f_inet_addr(m_bind_ip_gtpc);
81 g_sgsn_ip_u := f_inet_addr(m_bind_ip_gtpu);
82
Harald Welte94ade362017-08-04 00:36:55 +020083 var Result res;
84 map(self:GTPC, system:GTPC);
Harald Welteddeecbb2017-08-18 22:53:30 +020085 res := GTP_CodecPort_CtrlFunct.f_IPL4_listen(GTPC, m_bind_ip_gtpc, GTP1C_PORT, {udp:={}});
Harald Welte94ade362017-08-04 00:36:55 +020086 log("GTP1C ConnectionID: ", res.connId);
Harald Welte811651e2017-08-05 15:25:06 +020087 g_peer_c.connId := res.connId;
Harald Welte94ade362017-08-04 00:36:55 +020088
89 map(self:GTPU, system:GTPU);
Harald Welteddeecbb2017-08-18 22:53:30 +020090 res := GTP_CodecPort_CtrlFunct.f_GTPU_listen(GTPU, m_bind_ip_gtpu, GTP1U_PORT, {udp:={}});
Harald Welte811651e2017-08-05 15:25:06 +020091 g_peer_u.connId:= res.connId;
Harald Welte5438b9d2017-08-13 13:27:48 +020092
93 g_restart_ctr := f_rnd_octstring(1);
Harald Welte11dbc7b2017-08-13 18:57:56 +020094 g_c_seq_nr := f_rnd_int(65535);
95 g_d_seq_nr := f_rnd_int(65535);
Harald Welte94ade362017-08-04 00:36:55 +020096 }
97
98 /* generalized GTP-C receive template */
Harald Weltedca80052017-08-13 20:01:38 +020099 template PDU_GTPC tr_GTP1C_PDU(template OCT1 msg_type, template OCT4 teid, template GTPC_PDUs pdu := ?) := {
Harald Welte94ade362017-08-04 00:36:55 +0200100 /* N-PDU Number flag (PN) shall be set to '0'. A GTP-C receiver shall not return an
101 * error if this flag is set to '1'. */
102 pn_bit := '0'B,
103 /* Sequence number flag (S) shall be set to '1'. */
104 s_bit := '1'B,
105 e_bit := ?,
106 spare := ?,
107 /* Protocol Type flag (PT) shall be set to '1'.*/
108 pt := '1'B,
109 /* Version shall be set to decimal 1 ('001'). */
110 version := '001'B,
111 messageType := msg_type,
112 lengthf := ?,
113 teid := teid,
114 opt_part := *,
Harald Weltedca80052017-08-13 20:01:38 +0200115 gtpc_pdu := pdu
Harald Welte94ade362017-08-04 00:36:55 +0200116 }
117
118 /* generalized GTP-C send template */
Harald Welte811651e2017-08-05 15:25:06 +0200119 template PDU_GTPC ts_GTP1C_PDU(OCT1 msg_type, OCT4 teid, GTPC_PDUs pdu, uint16_t seq_nr) := {
Harald Welte94ade362017-08-04 00:36:55 +0200120 /* N-PDU Number flag (PN) shall be set to '0'. A GTP-C receiver shall not return an
121 * error if this flag is set to '1'. */
122 pn_bit := '0'B,
123 /* Sequence number flag (S) shall be set to '1'. */
124 s_bit := '1'B,
125 e_bit := '0'B,
126 spare := '0'B,
127 /* Protocol Type flag (PT) shall be set to '1'.*/
128 pt := '1'B,
129 /* Version shall be set to decimal 1 ('001'). */
130 version := '001'B,
131 messageType := msg_type,
132 lengthf := 0, /* we assume encoder overwrites this */
133 teid := teid,
134 opt_part := {
Harald Welte811651e2017-08-05 15:25:06 +0200135 sequenceNumber := int2oct(seq_nr, 2),
Harald Welte94ade362017-08-04 00:36:55 +0200136 npduNumber := '00'O,
137 nextExtHeader := '00'O,
138 gTPC_extensionHeader_List := omit
139 },
140 gtpc_pdu := pdu
141 }
142
143 /* recovery IE */
144 template Recovery_gtpc ts_Recovery(OCT1 restart_counter) := {
Harald Weltedca80052017-08-13 20:01:38 +0200145 type_gtpc := '0E'O,
146 restartCounter := restart_counter
147 }
148
149 template Recovery_gtpc tr_Recovery(template OCT1 restart_counter) := {
150 type_gtpc := '0E'O,
Harald Welte94ade362017-08-04 00:36:55 +0200151 restartCounter := restart_counter
152 }
153
154 /* template matching reception of GTP-C echo-request */
Harald Weltedca80052017-08-13 20:01:38 +0200155 template Gtp1cUnitdata tr_GTPC_MsgType(template GtpPeer peer, template OCT1 msg_type, template OCT4 teid, template GTPC_PDUs pdus := ?) := {
Harald Welte94ade362017-08-04 00:36:55 +0200156 peer := peer,
Harald Weltedca80052017-08-13 20:01:38 +0200157 gtpc := tr_GTP1C_PDU(msg_type, teid, pdus)
Harald Welte94ade362017-08-04 00:36:55 +0200158 }
159
160 /* template matching reception of GTP-C echo-request */
161 template Gtp1cUnitdata tr_GTPC_PING(template GtpPeer peer) := tr_GTPC_MsgType(peer, echoRequest, '00000000'O);
162
Harald Weltedca80052017-08-13 20:01:38 +0200163 template GTPC_PDUs tr_EchoRespPDU(template OCT1 restart_counter) := {
164 echoResponse := {
165 recovery := tr_Recovery(restart_counter),
166 private_extension_gtpc := *
167 }
168 }
169
170 /* template matching reception of GTP-C echo-response */
171 template Gtp1cUnitdata tr_GTPC_PONG(template GtpPeer peer) := tr_GTPC_MsgType(peer, echoResponse, '00000000'O, tr_EchoRespPDU(?));
172
Harald Welte94ade362017-08-04 00:36:55 +0200173 template GTPC_PDUs ts_EchoRespPDU(OCT1 restart_counter) := {
174 echoResponse := {
Harald Weltedca80052017-08-13 20:01:38 +0200175 recovery := ts_Recovery(restart_counter),
Harald Welte94ade362017-08-04 00:36:55 +0200176 private_extension_gtpc := omit
177 }
178 }
179
180 /* master template for senidng a GTP-C echo response */
Harald Welte811651e2017-08-05 15:25:06 +0200181 template Gtp1cUnitdata ts_GTPC_PONG(GtpPeer peer, uint16_t seq, OCT1 rest_ctr) := {
Harald Welte94ade362017-08-04 00:36:55 +0200182 peer := peer,
Harald Welte811651e2017-08-05 15:25:06 +0200183 gtpc := ts_GTP1C_PDU(echoResponse, '00000000'O, valueof(ts_EchoRespPDU(rest_ctr)), seq)
Harald Welte94ade362017-08-04 00:36:55 +0200184 }
185
Harald Weltedca80052017-08-13 20:01:38 +0200186 template GTPC_PDUs ts_EchoReqPDU := {
187 echoRequest := {
188 private_extension_gtpc := omit
189 }
190 }
191
192 /* master template for sending a GTP-C echo request */
193 template Gtp1cUnitdata ts_GTPC_PING(GtpPeer peer, uint16_t seq) := {
194 peer := peer,
195 gtpc := ts_GTP1C_PDU(echoRequest, '00000000'O, valueof(ts_EchoReqPDU), seq)
196 }
197
Harald Welte94ade362017-08-04 00:36:55 +0200198 template EndUserAddress t_EuaIPv4(template OCT4 ip_addr) := {
199 type_gtpc := '80'O,
200 endUserAddress := {
201 endUserAddressIPv4 := {
202 lengthf := 2,
203 pdp_typeorg := '0001'B,
204 spare := '1111'B,
205 pdp_typenum := '21'O,
206 ipv4_address := ip_addr
207 }
208 }
209 }
210 template EndUserAddress t_EuaIPv4Dyn := t_EuaIPv4(omit);
Harald Welte99ef9a42017-08-14 21:42:03 +0200211 template EndUserAddress tr_EuaIPv4(template OCT4 ip_addr) modifies t_EuaIPv4 := {
212 endUserAddress := {
213 endUserAddressIPv4 := {
Harald Weltebb5a19e2017-09-21 22:50:41 +0800214 lengthf := 2+lengthof(ip_addr)
Harald Welte99ef9a42017-08-14 21:42:03 +0200215 }
216 }
217 }
218
Harald Welte94ade362017-08-04 00:36:55 +0200219 template EndUserAddress t_EuaIPv6(template OCT16 ip_addr) := {
220 type_gtpc := '80'O,
221 endUserAddress := {
222 endUserAddressIPv6 := {
223 lengthf := 2,
224 pdp_typeorg := '0001'B,
225 spare := '1111'B,
226 pdp_typenum := '57'O,
227 ipv6_address := ip_addr
228 }
229 }
230 }
231 template EndUserAddress t_EuaIPv6Dyn := t_EuaIPv6(omit);
Harald Welte99ef9a42017-08-14 21:42:03 +0200232 template EndUserAddress tr_EuaIPv6(template OCT16 ip_addr) modifies t_EuaIPv6 := {
233 endUserAddress := {
234 endUserAddressIPv6 := {
235 lengthf := 2+lengthof(ip_addr)
236 }
237 }
238 }
Harald Welte94ade362017-08-04 00:36:55 +0200239
240 template AccessPointName ts_APN(octetstring apn) := {
241 type_gtpc := '83'O,
242 lengthf := lengthof(apn),
243 apn_value := apn
244 }
245
246 template GSN_Address_GTPC ts_GsnAddr(octetstring ip_addr) := {
247 type_gtpc := '85'O,
248 lengthf := lengthof(ip_addr),
249 addressf := ip_addr
250 }
251
252 template MSISDN ts_Msisdn(octetstring msisdn) := {
253 type_gtpc := '86'O,
254 lengthf := lengthof(msisdn),
255 msisdn := msisdn
256 }
257
258 template QualityOfServiceProfile ts_QosDefault := {
259 type_gtpc := '87'O,
260 lengthf := 4,
261 allocRetensionPrio := '00'O,
262 qos_ProfileValue := {
263 reliabilityClass := '011'B,
264 delayClass := '001'B,
265 spare1 := '00'B,
266 precedenceClass := '010'B,
267 spare2 := '0'B,
268 peakThroughput := '1001'B,
269 meanThroughput := '11111'B,
270 spare3 := '000'B,
271 deliverErroneusSDU := omit,
272 deliveryOrder := omit,
273 trafficClass := omit,
274 maxSDUSize := omit,
275 maxBitrateUplink := omit,
276 maxBitrateDownlink := omit,
277 sduErrorRatio := omit,
278 residualBER := omit,
279 trafficHandlingPriority := omit,
280 transferDelay := omit,
281 guaranteedBitRateUplink := omit,
282 guaranteedBitRateDownlink := omit,
283 sourceStatisticsDescriptor := omit,
284 signallingIndication := omit,
285 spare4 := omit,
286 maxBitrateDownlinkExt := omit,
287 guaranteedBitRateDownlinkExt := omit,
288 maxBitrateUplinkExt := omit,
289 guaranteedBitRateUplinkExt := omit
290 }
291 }
292
293 template IMSI_gtpc ts_Imsi(hexstring digits) := {
294 type_gtpc := '02'O,
295 digits := digits,
296 padding := 'F'H
297 }
298
299 template GTPC_PDUs ts_CreatePdpPDU(hexstring imsi, OCT1 restart_ctr, OCT4 teid_data, OCT4 teid_ctrl,
300 BIT4 nsapi, EndUserAddress eua, octetstring apn,
301 octetstring sgsn_ip_sign, octetstring sgsn_ip_data,
Harald Welteed7a1772017-08-09 20:26:20 +0200302 octetstring msisdn, template ProtConfigOptions pco := omit) := {
Harald Welte94ade362017-08-04 00:36:55 +0200303 createPDPContextRequest := {
304 imsi := ts_Imsi(imsi),
305 rai := omit,
306 recovery := ts_Recovery(restart_ctr),
307 selectionMode := {
308 type_gtpc := '0F'O,
309 selectModeValue := '00'B,
310 spare := '111111'B
311 },
312 teidDataI := {
313 type_gtpc := '00'O,
314 teidDataI := teid_data
315 },
316 teidControlPlane := {
317 type_gtpc := '00'O,
318 teidControlPlane := teid_ctrl
319 },
320 nsapi := {
321 type_gtpc := '00'O,
322 nsapi := nsapi,
323 unused := '0000'B
324 },
325 linked_nsapi := omit,
326 charging_char := omit,
327 trace_ref := omit,
328 trace_type := omit,
329 endUserAddress := eua,
330 accessPointName := ts_APN(apn),
Harald Welteed7a1772017-08-09 20:26:20 +0200331 protConfigOptions := pco,
Harald Welte94ade362017-08-04 00:36:55 +0200332 sgsn_addr_signalling := ts_GsnAddr(sgsn_ip_sign),
333 sgsn_addr_traffic := ts_GsnAddr(sgsn_ip_data),
334 msisdn := ts_Msisdn(msisdn),
335 qualityOfServiceProfile := ts_QosDefault,
336 tft := omit,
337 triggerId := omit,
338 omcId := omit,
339 commonFlags := omit,
340 aPN_Restriction := omit,
341 ratType := omit,
342 userLocationInformation := omit,
343 mS_TimeZone := omit,
344 imeisv := omit,
345 camelChargingInformationContainer := omit,
346 additionalTraceInfo := omit,
347 correlationID := omit,
348 evolvedAllocationRetentionPriorityI := omit,
349 extendedCommonFlags := omit,
350 userCSGInformation := omit,
351 aPN_AMBR := omit,
352 signallingPriorityIndication := omit,
353 cN_OperatorSelectionEntity := omit,
354 private_extension_gtpc := omit
355 }
356 }
357
Harald Welte811651e2017-08-05 15:25:06 +0200358 template Gtp1cUnitdata ts_GTPC_CreatePDP(GtpPeer peer, uint16_t seq, hexstring imsi,
359 OCT1 restart_ctr, OCT4 teid_data,
Harald Welte94ade362017-08-04 00:36:55 +0200360 OCT4 teid_ctrl, BIT4 nsapi, EndUserAddress eua,
361 octetstring apn, octetstring sgsn_ip_sign,
Harald Welteed7a1772017-08-09 20:26:20 +0200362 octetstring sgsn_ip_data, octetstring msisdn,
363 template ProtConfigOptions pco := omit) := {
Harald Welte94ade362017-08-04 00:36:55 +0200364 peer := peer,
365 gtpc := ts_GTP1C_PDU(createPDPContextRequest, '00000000'O,
366 valueof(ts_CreatePdpPDU(imsi, restart_ctr, teid_data, teid_ctrl,
367 nsapi, eua, apn, sgsn_ip_sign,
Harald Welteed7a1772017-08-09 20:26:20 +0200368 sgsn_ip_data, msisdn, pco)), seq)
369 }
370
Harald Welte99ef9a42017-08-14 21:42:03 +0200371 /* PCO send base template */
Harald Welteed7a1772017-08-09 20:26:20 +0200372 template ProtConfigOptions ts_PCO := {
373 type_gtpc := '84'O,
374 lengthf := 0,
375 configProtocol := '000'B,
376 spare := '0000'B,
377 extension0 := '1'B,
378 protocols := {}
379 }
Harald Welte99ef9a42017-08-14 21:42:03 +0200380 /* PCO receive base template */
381 template ProtConfigOptions tr_PCO := {
382 type_gtpc := '84'O,
383 lengthf := ?,
384 configProtocol := '000'B,
385 spare := ?,
386 extension0 := '1'B,
387 protocols := {}
388 }
Harald Welteed7a1772017-08-09 20:26:20 +0200389
390 template ProtConfigOptions ts_PCO_IPv6_DNS modifies ts_PCO := {
391 protocols := {
392 { protocolID := '0003'O, lengthProtoID := 0, protoIDContents := ''O }
393 }
394 }
Harald Welte99ef9a42017-08-14 21:42:03 +0200395 template ProtConfigOptions tr_PCO_IPv6_DNS_resp(template OCT16 contents) modifies tr_PCO := {
396 protocols := {
Harald Welte3ab91d62017-08-25 14:46:39 +0200397 *, { protocolID := '0003'O, lengthProtoID := 16, protoIDContents := contents }, *
Harald Welte99ef9a42017-08-14 21:42:03 +0200398 }
399 }
Harald Welteed7a1772017-08-09 20:26:20 +0200400
401 template ProtConfigOptions ts_PCO_IPv4_DNS_IPCP modifies ts_PCO := {
402 protocols := {
Harald Welteab4ca942017-09-07 18:41:52 +0200403 /* dummy PAP entry to check if our parser in the GGSN can properly iterate over
404 * the list of protocols, see Change-Id Icc2e6716c33d78d3c3e000f529806228d8aa155e */
405 { protocolID := 'C023'O, lengthProtoID := 0, protoIDContents := ''O },
Harald Welteeb9184d2017-12-04 19:01:47 +0100406 { protocolID := '8021'O, lengthProtoID := 16, protoIDContents :=
407 enc_IpcpPacket(valueof(ts_IPCP_ReqDNS)) }
Harald Welteed7a1772017-08-09 20:26:20 +0200408 }
409 }
410
Harald Welte71a36022017-12-04 18:55:58 +0100411 template ProtocolElement tr_PCO_Proto(OCT2 prot_id) := {
412 protocolID := prot_id,
413 lengthProtoID := ?,
414 protoIDContents := ?
415 }
416 template ProtConfigOptions tr_PCO_Contains(OCT2 prot_id) modifies tr_PCO := {
417 protocols := { *, tr_PCO_Proto(prot_id), * }
418 }
419
Harald Welteed7a1772017-08-09 20:26:20 +0200420 template ProtConfigOptions ts_PCO_IPv4_DNS_CONT modifies ts_PCO := {
421 protocols := {
422 { protocolID := '000d'O, lengthProtoID := 0, protoIDContents := ''O }
423 }
Harald Welte94ade362017-08-04 00:36:55 +0200424 }
Harald Welte99ef9a42017-08-14 21:42:03 +0200425 template ProtConfigOptions tr_PCO_IPv4_DNS_CONT_resp(template OCT4 contents) modifies tr_PCO := {
426 protocols := {
Harald Welte3ab91d62017-08-25 14:46:39 +0200427 *, { protocolID := '000d'O, lengthProtoID := 4, protoIDContents := contents }, *
Harald Welte99ef9a42017-08-14 21:42:03 +0200428 }
429 }
430
Harald Welte71a36022017-12-04 18:55:58 +0100431 /* extract a given protocol payload from PCO */
Pau Espin Pedrol363ba482018-01-29 18:42:00 +0100432 function f_PCO_extract_proto(ProtConfigOptions pco, OCT2 protocol, integer nth_match := 1) return octetstring {
Harald Welte71a36022017-12-04 18:55:58 +0100433 var integer i;
Pau Espin Pedrol363ba482018-01-29 18:42:00 +0100434 var integer num_matches := 0;
Harald Welte71a36022017-12-04 18:55:58 +0100435 for (i := 0; i < lengthof(pco.protocols); i := i + 1) {
436 if (pco.protocols[i].protocolID == protocol) {
Pau Espin Pedrol363ba482018-01-29 18:42:00 +0100437 num_matches := num_matches + 1;
438 if (num_matches == nth_match) {
439 return pco.protocols[i].protoIDContents;
440 }
Harald Welte71a36022017-12-04 18:55:58 +0100441 }
442 }
443 setverdict(fail);
444 return ''O;
445 }
446
447 template IpcpPacket tr_IPCP(template LcpCode code, template uint8_t identifier,
448 template IpcpOptionList opts) := {
449 code := code,
450 identifier := identifier,
451 len := ?,
452 options := opts
453 }
454 template IpcpOption tr_IPCP_PrimaryDns(template OCT4 addr) := {
455 code := IPCP_OPT_PrimaryDNS,
456 len := 6,
457 data := addr
458 }
459 template IpcpOption tr_IPCP_SecondaryDns(template OCT4 addr) := {
460 code := IPCP_OPT_SecondaryDNS,
461 len := 6,
462 data := addr
463 }
Harald Welte71a36022017-12-04 18:55:58 +0100464 template IpcpPacket tr_IPCP_Ack_DNS(template uint8_t identifier := ?, template OCT4 dns1 := ?,
465 template OCT4 dns2 := ?) :=
466 tr_IPCP(LCP_Configure_Ack, identifier,
467 { *, tr_IPCP_PrimaryDns(dns1), *, tr_IPCP_SecondaryDns(dns2), * });
468
Harald Welteeb9184d2017-12-04 19:01:47 +0100469 template IpcpPacket ts_IPCP(LcpCode code, uint8_t identifier, template IpcpOptionList opts) := {
470 code := code,
471 identifier := identifier,
472 len := 0, /* overwritten */
473 options := opts
474 }
475 template IpcpPacket ts_IPCP_ReqDNS(uint8_t identifier := 0) :=
476 ts_IPCP(LCP_Configure_Request, identifier,
477 { tr_IPCP_PrimaryDns('00000000'O), tr_IPCP_SecondaryDns('00000000'O) });
Harald Welte94ade362017-08-04 00:36:55 +0200478
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200479 function f_teardown_ind_IE(in template BIT1 ind) return template TearDownInd {
Harald Welte811651e2017-08-05 15:25:06 +0200480/*
481 if (not isvalue(ind)) {
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200482 return omit;
483 }
Harald Welte811651e2017-08-05 15:25:06 +0200484*/
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200485 var TearDownInd ret := {
486 type_gtpc := '13'O,
487 tdInd := valueof(ind),
488 spare:= '0000000'B
489 }
490 return ret;
491 }
492
493 template GTPC_PDUs ts_DeletePdpPDU(BIT4 nsapi, template BIT1 teardown_ind) := {
494 deletePDPContextRequest := {
495 cause := omit,
496 tearDownIndicator := f_teardown_ind_IE(teardown_ind),
497 nsapi := {
498 type_gtpc := '14'O,
499 nsapi := nsapi,
500 unused := '0000'B
501 },
502 protConfigOptions := omit,
503 userLocationInformation := omit,
504 mS_TimeZone := omit,
505 extendedCommonFlags := omit,
506 uLI_Timestamp := omit,
507 private_extension_gtpc := omit
508 }
509 }
510
Harald Welte811651e2017-08-05 15:25:06 +0200511 template Gtp1cUnitdata ts_GTPC_DeletePDP(GtpPeer peer, uint16_t seq, OCT4 teid,
512 BIT4 nsapi, template BIT1 teardown_ind) := {
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200513 peer := peer,
514 gtpc := ts_GTP1C_PDU(deletePDPContextRequest, teid,
Harald Welte811651e2017-08-05 15:25:06 +0200515 valueof(ts_DeletePdpPDU(nsapi, teardown_ind)), seq)
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200516 }
517
518
Harald Welte3af89482017-08-04 16:20:23 +0200519 /* GTP-U */
520
Harald Welte231b9412017-08-09 17:16:31 +0200521 template PDU_GTPU tr_GTP1U_PDU(template OCT1 msg_type, template OCT4 teid, template GTPU_IEs ies := ?) := {
Harald Welte3af89482017-08-04 16:20:23 +0200522 pn_bit := ?,
523 s_bit := ?,
524 e_bit := ?,
525 spare := ?,
526 /* Protocol Type flag (PT) shall be set to '1' in GTP */
527 pt := '1'B,
528 /* Version shall be set to decimal 1 ('001'). */
529 version := '001'B,
530 messageType := msg_type,
531 lengthf := ?,
532 teid := teid,
533 opt_part := *,
Harald Welte231b9412017-08-09 17:16:31 +0200534 gtpu_IEs := ies
Harald Welte3af89482017-08-04 16:20:23 +0200535 }
536
537 /* generalized GTP-U send template */
Harald Welte811651e2017-08-05 15:25:06 +0200538 template PDU_GTPU ts_GTP1U_PDU(OCT1 msg_type, uint16_t seq, OCT4 teid, GTPU_IEs ies) := {
Harald Welte3af89482017-08-04 16:20:23 +0200539 /* N-PDU Number flag (PN): the GTP-U header contains a meaningful N-PDU Number field if the PN
540 * flag is set to 1. */
541 pn_bit := '0'B, /* we assume the encoder overwrites this if an optional part is given */
542 /* If the Sequence Number flag (S) is set to '1' the sequence number field is present and
543 * meaningful otherwise it is set to '0'. For GTP-U messages Echo Request, Echo Response,
544 * Error Indication and Supported Extension Headers Notification, the S flag shall be set to '1'. */
Harald Welte811651e2017-08-05 15:25:06 +0200545 s_bit := '1'B, /* we assume the encoder overwrites this if an optional part is given */
Harald Welte3af89482017-08-04 16:20:23 +0200546 /* Extension header presence */
547 e_bit := '0'B,
548 spare := '0'B,
549 /* Protocol Type flag (PT) shall be set to '1' in GTP */
550 pt := '1'B,
551 /* Version shall be set to decimal 1 ('001'). */
552 version := '001'B,
553 messageType := msg_type,
554 lengthf := 0, /* we assume encoder overwrites this */
555 teid := teid,
Harald Welte811651e2017-08-05 15:25:06 +0200556 opt_part := {
557 sequenceNumber := int2oct(seq, 2),
558 npduNumber := '00'O,
559 nextExtHeader := '00'O,
560 gTPU_extensionHeader_List := omit
561 },
Harald Welte3af89482017-08-04 16:20:23 +0200562 gtpu_IEs := ies
563 }
564
565 template Gtp1uUnitdata tr_GTPU_MsgType(template GtpPeer peer, template OCT1 msg_type, template OCT4 teid) := {
566 peer := peer,
567 gtpu := tr_GTP1U_PDU(msg_type, teid)
568 }
569
570
Harald Welte231b9412017-08-09 17:16:31 +0200571 /* template matching reception of GTP-U echo-request */
Harald Welte3af89482017-08-04 16:20:23 +0200572 template Gtp1uUnitdata tr_GTPU_PING(template GtpPeer peer) := tr_GTPU_MsgType(peer, echoRequest, '00000000'O);
573
Harald Welte231b9412017-08-09 17:16:31 +0200574 /* template matching reception of GTP-U GPDU */
575 template GTPU_IEs t_GPDU(template octetstring data) := {
576 g_PDU_IEs := {
577 data := data
578 }
579 }
580 template Gtp1uUnitdata tr_GTPU_GPDU(template GtpPeer peer, template OCT4 teid, template octetstring data := ?) := {
581 peer := peer,
582 gtpu := tr_GTP1U_PDU('FF'O, teid, t_GPDU(data))
583 }
584
Harald Welte3af89482017-08-04 16:20:23 +0200585 template GTPU_IEs ts_UEchoRespPDU(OCT1 restart_counter) := {
586 echoResponse_IEs := {
587 recovery_gtpu := {
588 type_gtpu := '00'O, /* we assume encoder fixes? */
589 restartCounter := restart_counter
590 },
591 private_extension_gtpu := omit
592 }
593 }
594
595 /* master template for sending a GTP-U echo response */
Harald Welte811651e2017-08-05 15:25:06 +0200596 template Gtp1uUnitdata ts_GTPU_PONG(GtpPeer peer, uint16_t seq, OCT1 rest_ctr) := {
Harald Welte3af89482017-08-04 16:20:23 +0200597 peer := peer,
Harald Welte811651e2017-08-05 15:25:06 +0200598 gtpu := ts_GTP1U_PDU(echoResponse, seq, '00000000'O, valueof(ts_UEchoRespPDU(rest_ctr)))
Harald Welte3af89482017-08-04 16:20:23 +0200599 }
600
Harald Welte811651e2017-08-05 15:25:06 +0200601 /* master template for sending a GTP-U user plane data */
602 template Gtp1uUnitdata ts_GTP1U_GPDU(GtpPeer peer, uint16_t seq, OCT4 teid, octetstring data) := {
603 peer := peer,
604 gtpu := ts_GTP1U_PDU('FF'O, seq, teid, { g_PDU_IEs := { data := data }})
605 }
Harald Welte3af89482017-08-04 16:20:23 +0200606
Harald Welte94ade362017-08-04 00:36:55 +0200607 /* Altstep implementing responses to any incoming echo requests */
608 altstep pingpong() runs on GT_CT {
609 var Gtp1cUnitdata ud;
Harald Welte3af89482017-08-04 16:20:23 +0200610 var Gtp1uUnitdata udu;
Harald Welte94ade362017-08-04 00:36:55 +0200611 [] GTPC.receive(tr_GTPC_PING(?)) -> value ud {
Harald Welte811651e2017-08-05 15:25:06 +0200612 var uint16_t seq := oct2int(ud.gtpc.opt_part.sequenceNumber);
613 GTPC.send(ts_GTPC_PONG(ud.peer, seq, '00'O));
Harald Welte94ade362017-08-04 00:36:55 +0200614 repeat;
615 };
Harald Welte3af89482017-08-04 16:20:23 +0200616 [] GTPU.receive(tr_GTPU_PING(?)) -> value udu {
Harald Welte811651e2017-08-05 15:25:06 +0200617 var uint16_t seq := oct2int(udu.gtpu.opt_part.sequenceNumber);
618 GTPU.send(ts_GTPU_PONG(udu.peer, seq, '00'O));
Harald Welte3af89482017-08-04 16:20:23 +0200619 };
Harald Welte94ade362017-08-04 00:36:55 +0200620 [] T_default.timeout { setverdict(fail); };
621 }
622
Harald Welte811651e2017-08-05 15:25:06 +0200623 /* 'internet' in DNS encoding */
Harald Welteed097432017-08-13 13:28:49 +0200624 const octetstring c_ApnInternet := '08696E7465726E6574'O;
625 const octetstring c_ApnInet6 := '05696E657436'O;
626 const octetstring c_ApnInet46 := '06696E65743436'O;
Harald Welte94ade362017-08-04 00:36:55 +0200627
Harald Welte811651e2017-08-05 15:25:06 +0200628 /* return random NSAPI */
629 function f_rnd_nsapi() return BIT4 {
630 return int2bit(f_rnd_int(16), 4);
631 }
632
633 /* return random TEI[DC] */
634 function f_rnd_tei() return OCT4 {
635 return int2oct(f_rnd_int(4294967296), 4);
636 }
637
638 /* define an (internal) representation of a PDP context */
639 template PdpContext t_DefinePDP(hexstring imsi, octetstring msisdn, octetstring apn,
640 EndUserAddress eua) := {
641 imsi := imsi,
642 msisdn := msisdn,
643 nsapi := f_rnd_nsapi(),
644 apn := apn,
Harald Welteed7a1772017-08-09 20:26:20 +0200645 pco_req := omit,
Harald Welte811651e2017-08-05 15:25:06 +0200646 eua := eua,
647 teid := f_rnd_tei(),
Harald Welte5438b9d2017-08-13 13:27:48 +0200648 teic := f_rnd_tei()
Harald Welte811651e2017-08-05 15:25:06 +0200649 }
650
651 /* send GTP-C for a given context and increment sequence number */
Harald Welte41575e92017-08-13 13:49:57 +0200652 function f_send_gtpc(in template Gtp1cUnitdata data) runs on GT_CT {
Harald Welte811651e2017-08-05 15:25:06 +0200653 GTPC.send(data);
Harald Welte5438b9d2017-08-13 13:27:48 +0200654 g_c_seq_nr := g_c_seq_nr + 1;
Harald Welte811651e2017-08-05 15:25:06 +0200655 }
656
657 /* send GTP-U for a given context and increment sequence number */
Harald Welte231b9412017-08-09 17:16:31 +0200658 function f_send_gtpu(inout PdpContext ctx, in octetstring data) runs on GT_CT {
Harald Welte5438b9d2017-08-13 13:27:48 +0200659 GTPU.send(ts_GTP1U_GPDU(g_peer_u, g_d_seq_nr, ctx.teid_remote, data));
660 g_d_seq_nr := g_d_seq_nr + 1;
Harald Welte811651e2017-08-05 15:25:06 +0200661 }
662
663 /* send a PDP context activation */
664 function f_pdp_ctx_act(inout PdpContext ctx) runs on GT_CT {
Harald Welte94ade362017-08-04 00:36:55 +0200665 var Gtp1cUnitdata ud;
Harald Welte94ade362017-08-04 00:36:55 +0200666 var default d;
667
668 log("sending CreatePDP");
Harald Welte41575e92017-08-13 13:49:57 +0200669 f_send_gtpc(ts_GTPC_CreatePDP(g_peer_c, g_c_seq_nr, ctx.imsi, g_restart_ctr,
Harald Welte811651e2017-08-05 15:25:06 +0200670 ctx.teid, ctx.teic, ctx.nsapi, ctx.eua, ctx.apn,
Harald Welteed7a1772017-08-09 20:26:20 +0200671 g_sgsn_ip_c, g_sgsn_ip_u, ctx.msisdn, ctx.pco_req));
Harald Welte94ade362017-08-04 00:36:55 +0200672 T_default.start;
Harald Welte94ade362017-08-04 00:36:55 +0200673 d := activate(pingpong());
674 alt {
Harald Welte811651e2017-08-05 15:25:06 +0200675 [] GTPC.receive(tr_GTPC_MsgType(g_peer_c, createPDPContextResponse, ctx.teic)) -> value ud {
676 var CreatePDPContextResponse cpr := ud.gtpc.gtpc_pdu.createPDPContextResponse;
677 if (cpr.cause.causevalue == '80'O) {
Harald Welte99ef9a42017-08-14 21:42:03 +0200678 /* Check if EUA type corresponds to requested type */
679 if (match(ctx.eua, t_EuaIPv4(?)) and
680 not match(cpr.endUserAddress, tr_EuaIPv4(?))){
681 setverdict(fail);
682 }
683 if (match(ctx.eua, t_EuaIPv6(?)) and
684 not match(cpr.endUserAddress, tr_EuaIPv6(?))) {
685 setverdict(fail);
686 }
687 /* Check if PCO response corresponds to request */
688 if (ispresent(ctx.pco_req)) {
689 if (match(ctx.pco_req, ts_PCO_IPv4_DNS_CONT) and
690 not match(cpr.protConfigOptions, tr_PCO_IPv4_DNS_CONT_resp(?))) {
691 log("IPv4 DNS Container requested, but missing");
692 setverdict(fail);
693 }
694 if (match(ctx.pco_req, ts_PCO_IPv6_DNS) and
695 not match(cpr.protConfigOptions, tr_PCO_IPv6_DNS_resp(?))) {
696 log("IPv6 DNS Container requested, but missing");
697 setverdict(fail);
698 }
699 }
Harald Welte811651e2017-08-05 15:25:06 +0200700 ctx.teid_remote := cpr.teidDataI.teidDataI;
701 ctx.teic_remote := cpr.teidControlPlane.teidControlPlane;
Harald Welte231b9412017-08-09 17:16:31 +0200702 ctx.eua := cpr.endUserAddress;
Harald Welteed7a1772017-08-09 20:26:20 +0200703 ctx.pco_neg := cpr.protConfigOptions;
Harald Welte94ade362017-08-04 00:36:55 +0200704 setverdict(pass);
705 } else {
706 setverdict(fail);
707 }
708 }
709 }
710 deactivate(d);
Harald Welte811651e2017-08-05 15:25:06 +0200711 T_default.stop;
Harald Welte94ade362017-08-04 00:36:55 +0200712 }
713
Harald Welte811651e2017-08-05 15:25:06 +0200714 function f_pdp_ctx_del(PdpContext ctx, template BIT1 teardown_ind) runs on GT_CT {
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200715 var Gtp1cUnitdata ud;
716 var default d;
717
Harald Welte41575e92017-08-13 13:49:57 +0200718 f_send_gtpc(ts_GTPC_DeletePDP(g_peer_c, g_c_seq_nr, ctx.teic_remote, ctx.nsapi, teardown_ind));
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200719 T_default.start;
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200720 d := activate(pingpong());
721 alt {
Harald Welte811651e2017-08-05 15:25:06 +0200722 [] GTPC.receive(tr_GTPC_MsgType(g_peer_c, deletePDPContextResponse, ctx.teic)) -> value ud {
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200723 if (ud.gtpc.gtpc_pdu.deletePDPContextResponse.cause.causevalue == '80'O) {
724 setverdict(pass);
725 } else {
726 setverdict(fail);
727 }
728 }
729 }
730 deactivate(d);
Harald Welte811651e2017-08-05 15:25:06 +0200731 T_default.stop;
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200732 }
Harald Welte811651e2017-08-05 15:25:06 +0200733 /* IPv6 router solicitation fe80::2 -> ff02::2 from 02:88:b5:1f:25:59 */
734 const octetstring c_router_solicit := '6000000000103afffe800000000000000000000000000002ff02000000000000000000000000000285009f2b0000000001010288b51f2559'O;
735 /* IPv6 neighbor solicitation fe80::2 -> ff02::1:ff00:2 from 02:88:b5:1f:25:59 */
736 const octetstring c_neigh_solicit:= '6000000000203afffe800000000000000000000000000002ff0200000000000000000001ff00000287009f9600000000fe80000000000000000000000000000201010288b51f2559'O;
Harald Weltef1e0d5a2017-08-05 08:51:22 +0200737
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +0100738 /* template for an ICMPv6 echo request */
739 template PDU_ICMP ts_ICMPv4_ERQ := {
740 echo := {
741 type_field := 8,
742 code := 0,
743 checksum := '0000'O,
744 identifier := '0345'O,
745 sequence_number := '0001'O,
746 data := ''O
747 }
748 }
749
750 /* template for an ICMPv4 echo reply */
751 template PDU_ICMP tr_ICMPv4_ERP(template octetstring data := *) := {
752 echo_reply := {
753 type_field := 0,
754 code := 0,
755 checksum := ?,
756 identifier := ?,
757 sequence_number := ?,
758 data := data
759 }
760 }
761
762 /* template for receiving/matching an ICMPv6 Destination Unreachable */
763 template PDU_ICMP tr_ICMPv4_DU := {
764 destination_unreachable := {
765 type_field := 1,
766 code := ?,
767 checksum := ?,
768 unused := ?,
769 original_ip_msg := ?
770 }
771 }
772
773 /* template to construct IPv4_packet from input arguments, ready for use in f_IPv4_enc() */
774 template IPv4_packet ts_IP4(OCT4 srcaddr, OCT4 dstaddr, LIN1 proto, LIN2_BO_LAST tlen, octetstring payload) := {
775 header := {
776 ver := 4,
777 hlen := 5,
778 tos := 0,
779 tlen := tlen,
780 id := 35902,
781 res := '0'B,
782 dfrag := '1'B,
783 mfrag := '0'B,
784 foffset := 0,
785 ttl := 64,
786 proto := proto,
787 cksum := 0,
788 srcaddr := srcaddr,
789 dstaddr := dstaddr
790 },
791 ext_headers := omit,
792 payload := payload
793 }
794
Harald Welte231b9412017-08-09 17:16:31 +0200795 /* template to generate a 'Prefix Information' ICMPv6 option */
796 template OptionField ts_ICMP6_OptPrefix(OCT16 prefix, INT1 prefix_len) := {
797 prefixInformation := {
798 typeField := 3,
799 lengthIndicator := 8,
800 prefixLength := prefix_len,
801 reserved1 := '000000'B,
802 a_Bit := '0'B,
803 l_Bit := '0'B,
804 validLifetime := oct2int('FFFFFFFF'O),
805 preferredLifetime := oct2int('FFFFFFFF'O),
806 reserved2 := '00000000'O,
807 prefix := prefix
808 }
809 }
810
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +0100811 /* template for an ICMPv6 echo request */
812 template PDU_ICMPv6 ts_ICMPv6_ERQ := {
813 echoRequest := {
814 typeField := 128,
815 code := 0,
816 checksum := '0000'O,
817 identifier := 0,
818 sequenceNr := 0,
819 data := ''O
820 }
821 }
822
Harald Welte231b9412017-08-09 17:16:31 +0200823 /* template for an ICMPv6 router solicitation */
824 template PDU_ICMPv6 ts_ICMPv6_RS := {
825 routerSolicitation := {
826 typeField := 133,
827 code := 0,
828 checksum := '0000'O,
829 reserved := '00000000'O,
830 /* TODO: do we need 'Source link-layer address' ? */
831 options := omit
832 }
833 }
834
835 /* template for an ICMPv6 router advertisement */
836 template PDU_ICMPv6 ts_ICMPv6_RA(OCT16 prefix, INT1 prefix_len) := {
837 routerAdvertisement := {
838 typeField := 134,
839 code := 0,
840 checksum := '0000'O,
841 curHopLimit := ?,
842 reserved := '000000'B,
843 o_Bit := '0'B,
844 m_Bit := '0'B,
845 routerLifetime := oct2int('FFFF'O),
846 reachableTime := oct2int('FFFFFFFF'O),
847 retransTimer := oct2int('FFFFFFFF'O),
848 options := {
849 ts_ICMP6_OptPrefix(prefix, prefix_len)
850 }
851 }
852 }
853
854 template PDU_ICMPv6 ts_ICMPv6_NS(OCT16 target_addr) := {
855 neighborSolicitation := {
856 typeField := 135,
857 code := 0,
858 checksum := '0000'O,
859 reserved := '00000000'O,
860 targetAddress := target_addr,
861 /* TODO: do we need 'Source link-layer address' ? */
862 options := omit
863 }
864 }
865
866 /* derive ICMPv6 link-local address from lower 64bit of link_id */
867 /* template for receiving/matching an ICMPv6 'Prefix Information' option */
868 template OptionField tr_ICMP6_OptPrefix(template OCT16 prefix, template INT1 prefix_len) := {
869 prefixInformation := {
870 typeField := 3,
871 lengthIndicator := 4,
872 prefixLength := prefix_len,
873 reserved1 := ?,
874 a_Bit := ?,
875 l_Bit := ?,
876 validLifetime := ?,
877 preferredLifetime := ?,
878 reserved2 := ?,
879 prefix := prefix
880 }
881 }
882
883 /* template for receiving/matching an ICMPv6 router advertisement */
884 template PDU_ICMPv6 tr_ICMPv6_RA(template OCT16 prefix, template INT1 prefix_len) := {
885 routerAdvertisement := {
886 typeField := 134,
887 code := 0,
888 checksum := ?,
889 curHopLimit := ?,
890 reserved := ?,
891 o_Bit := '0'B,
892 m_Bit := '0'B,
893 routerLifetime := ?,
894 reachableTime := ?,
895 retransTimer := ?,
896 options := {
897 tr_ICMP6_OptPrefix(prefix, prefix_len)
898 }
899 }
900 }
901
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +0100902 /* template for receiving/matching an ICMPv6 Destination Unreachable */
903 template PDU_ICMPv6 tr_ICMPv6_DU := {
904 destinationUnreachable := {
905 typeField := 1,
906 code := ?,
907 checksum := ?,
908 unused := ?,
909 originalIpMsg := ?
910 }
911 }
912
913 /* template for receiving/matching an ICMPv6 echo reply */
914 template PDU_ICMPv6 tr_ICMPv6_ERP(template octetstring data := *) := {
915 echoReply := {
916 typeField := 129,
917 code := 0,
918 checksum := ?,
919 identifier := ?,
920 sequenceNr := ?,
921 data := data
922 }
923 }
924
Harald Welte231b9412017-08-09 17:16:31 +0200925 /* template to construct IPv6_packet from input arguments, ready for use in f_IPv6_enc() */
926 template IPv6_packet ts_IP6(OCT16 srcaddr, OCT16 dstaddr, LIN1 nexthead, octetstring payload, LIN1 hlim := 255) := {
927 header := {
928 ver := 6,
929 trclass := 0,
930 flabel := 0,
931 plen := 0,
932 nexthead := nexthead,
933 hlim := hlim,
934 srcaddr := srcaddr,
935 dstaddr := dstaddr
936 },
937 ext_headers := omit,
938 payload := payload
939 }
940
941 function f_ipv6_link_local(in OCT16 link_id) return OCT16 {
942 return 'FE80000000000000'O & substr(link_id, 8, 8);
943 }
944
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +0100945 function f_ipv6_global(in OCT16 link_id) return OCT16 {
946 return substr(link_id, 0, 8) & '1234123412341234'O;
947 }
948
949 /* Create a new different IPv6 addr from input. Starts mangling at byte prefix. */
950 function f_ipv6_mangle(in OCT16 addr, in integer prefix := 0) return OCT16 {
951 var integer i;
952 var octetstring res := substr(addr, 0, prefix);
953 for (i := prefix; i < lengthof(addr); i := i + 1) {
954 var octetstring a := addr[i] xor4b '11'O;
955 res := res & a;
956 }
957 return res;
958 }
959
Harald Welte231b9412017-08-09 17:16:31 +0200960 /* Compute solicited-node multicast address as per RFC4291 2.7.1 */
961 function f_ipv6_sol_node_mcast(in OCT16 addr) return OCT16 {
962 return 'FF0200000000000000000001FF'O & substr(addr, 13, 3);
963 }
964
965 /* generate and encode ICMPv6 router solicitation */
966 function f_gen_icmpv6_router_solicitation(in OCT16 link_id) return octetstring {
967 const OCT16 c_ip6_all_router_mcast := 'FF020000000000000000000000000002'O;
968 var OCT16 saddr := f_ipv6_link_local(link_id);
969
970 var octetstring tmp;
971 tmp := f_enc_PDU_ICMPv6(valueof(ts_ICMPv6_RS), saddr, c_ip6_all_router_mcast);
972 var IPv6_packet ip6 := valueof(ts_IP6(saddr, c_ip6_all_router_mcast, 58, tmp));
973
974 return f_IPv6_enc(ip6);
975 }
976
977 /* create ICMPv6 router solicitation deriving link-id from PDP Context EUA */
978 function f_icmpv6_rs_for_pdp(in PdpContext ctx) return octetstring {
979 var OCT16 interface_id := ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address;
980 return f_gen_icmpv6_router_solicitation(interface_id);
981 }
982
983 /* generate and encode ICMPv6 neighbor solicitation */
984 function f_gen_icmpv6_neigh_solicit(in OCT16 saddr, in OCT16 daddr, in OCT16 tgt_addr) return octetstring {
985 var octetstring tmp;
986 tmp := f_enc_PDU_ICMPv6(valueof(ts_ICMPv6_NS(tgt_addr)), saddr, daddr);
987 var IPv6_packet ip6 := valueof(ts_IP6(saddr, daddr, 58, tmp));
988 return f_IPv6_enc(ip6);
989 }
990
991 /* generate and encode ICMPv6 neighbor solicitation for PDP Context */
992 function f_gen_icmpv6_neigh_solicit_for_pdp(in PdpContext ctx) return octetstring {
993 var OCT16 interface_id := ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address;
994 var OCT16 link_local := f_ipv6_link_local(interface_id);
995 var OCT16 daddr := f_ipv6_sol_node_mcast(link_local);
996
997 return f_gen_icmpv6_neigh_solicit(link_local, daddr, link_local);
998 }
999
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001000 /* Send an ICMPv4 echo msg through GTP given pdp ctx, and ip src and dst addr */
1001 function f_gen_icmpv4_echo(OCT4 saddr, OCT4 daddr) return octetstring {
1002 var octetstring tmp := f_enc_PDU_ICMP(valueof(ts_ICMPv4_ERQ));
1003 var IPv4_packet ip4 := valueof(ts_IP4(saddr, daddr, 1, 50, tmp));
1004 var octetstring data := f_IPv4_enc(ip4);
1005 var OCT2 cksum := f_IPv4_checksum(data);
1006 data[10] := cksum[0];
1007 data[11] := cksum[1];
1008 return data;
1009 }
1010
1011 /* Send an ICMPv6 echo msg through GTP given pdp ctx, and ip src and dst addr */
1012 function f_gen_icmpv6_echo(OCT16 saddr, OCT16 daddr) return octetstring {
1013 var octetstring tmp := f_enc_PDU_ICMPv6(valueof(ts_ICMPv6_ERQ), saddr, daddr);
1014 var IPv6_packet ip6 := valueof(ts_IP6(saddr, daddr, 58, tmp));
1015 var octetstring data := f_IPv6_enc(ip6);
1016 return data;
1017 }
1018
1019 /* Wait for ICMPv4 from GTP */
1020 function f_wait_icmp4(PdpContext ctx, template PDU_ICMP expected) runs on GT_CT {
Harald Welte231b9412017-08-09 17:16:31 +02001021 var Gtp1uUnitdata ud;
1022 T_default.start;
1023 alt {
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001024 [] GTPU.receive(tr_GTPU_GPDU(g_peer_u, ?)) -> value ud {
1025 var octetstring gpdu := ud.gtpu.gtpu_IEs.g_PDU_IEs.data;
1026 var IPv4_packet ip4 := f_IPv4_dec(gpdu);
1027 if (ip4.header.ver != 4) {
1028 repeat;
1029 }
1030 var PDU_ICMP icmp4 := f_dec_PDU_ICMP(ip4.payload);
1031 if (not match(icmp4, expected)) {
1032 repeat;
1033 }
1034 }
1035 [] GTPU.receive { setverdict(fail); }
1036 [] T_default.timeout { setverdict(fail); }
1037 }
1038 T_default.stop;
1039 }
1040
1041 /* Wait for ICMPv4 echo reply (or unreachable) from GTP */
1042 function f_wait_icmp4_echo_reply(PdpContext ctx) runs on GT_CT {
1043 f_wait_icmp4(ctx, (tr_ICMPv4_ERP, tr_ICMPv4_DU));
1044 }
1045
1046 /* Wait for ICMPv6 from GTP */
1047 function f_wait_icmp6(PdpContext ctx, template PDU_ICMPv6 expected) runs on GT_CT {
1048 var Gtp1uUnitdata ud;
1049 T_default.start;
1050 alt {
Harald Welte231b9412017-08-09 17:16:31 +02001051 [] GTPU.receive(tr_GTPU_GPDU(g_peer_u, ?)) -> value ud {
1052 var octetstring gpdu := ud.gtpu.gtpu_IEs.g_PDU_IEs.data;
1053 var IPv6_packet ip6 := f_IPv6_dec(gpdu);
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001054 if (ip6.header.ver != 6 or ip6.header.nexthead != 58) {
Harald Welte231b9412017-08-09 17:16:31 +02001055 repeat;
1056 }
1057 var PDU_ICMPv6 icmp6 := f_dec_PDU_ICMPv6(ip6.payload);
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001058 if (not match(icmp6, expected)) {
Harald Welte231b9412017-08-09 17:16:31 +02001059 repeat;
1060 }
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001061 /* We are waiting for RA, update ctx */
1062 if (match(icmp6, tr_ICMPv6_RA(?, 64))) {
1063 ctx.ip6_prefix := icmp6.routerAdvertisement.options[0].prefixInformation.prefix;
1064 log("RA with /64 prefix ", ctx.ip6_prefix);
1065 }
Harald Welte231b9412017-08-09 17:16:31 +02001066 }
1067 [] GTPU.receive(tr_GTPU_GPDU(?, ?)) { repeat; }
1068 [] GTPU.receive { setverdict(fail); }
1069 [] T_default.timeout { setverdict(fail); }
1070 }
1071 T_default.stop;
1072 }
1073
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001074 /* wait for GGSN to send us an ICMPv6 router advertisement */
1075 function f_wait_rtr_adv(PdpContext ctx) runs on GT_CT {
1076 f_wait_icmp6(ctx, tr_ICMPv6_RA(?, 64));
1077 }
1078
1079 /* Wait for ICMPv6 echo reply (or unreachable) from GTP */
1080 function f_wait_icmp6_echo_reply(PdpContext ctx) runs on GT_CT {
1081 f_wait_icmp6(ctx, (tr_ICMPv6_ERP,tr_ICMPv6_DU));
1082 }
1083
1084 /* Assert we don't receive a ICMPv4/6 echo reply (or unreachable) from GTP */
1085 function f_wait_gtpu_fail(PdpContext ctx) runs on GT_CT {
1086 T_default.start;
1087 alt {
1088 [] GTPU.receive { setverdict(fail); }
1089 [] T_default.timeout { }
1090 }
1091 T_default.stop;
1092 }
1093
Harald Welte0ef285b2017-08-13 20:06:01 +02001094 /* Test IPv6 context activation for dynamic IPv6 EUA without request of IPv6 DNS */
Harald Welteed7a1772017-08-09 20:26:20 +02001095 testcase TC_pdp6_act_deact() runs on GT_CT {
Harald Welte94ade362017-08-04 00:36:55 +02001096 f_init();
Harald Welte231b9412017-08-09 17:16:31 +02001097
Harald Welteed097432017-08-13 13:28:49 +02001098 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInet6, valueof(t_EuaIPv6Dyn)));
Harald Welte811651e2017-08-05 15:25:06 +02001099 f_pdp_ctx_act(ctx);
Harald Welteed7a1772017-08-09 20:26:20 +02001100 f_pdp_ctx_del(ctx, '1'B);
1101 }
1102
Harald Welte0ef285b2017-08-13 20:06:01 +02001103 /* Test IPv6 context activation for dynamic IPv6 EUA wirh request of IPv6 DNS in PCO */
Harald Welteed7a1772017-08-09 20:26:20 +02001104 testcase TC_pdp6_act_deact_pcodns() runs on GT_CT {
1105 f_init();
1106
Harald Welteed097432017-08-13 13:28:49 +02001107 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInet6, valueof(t_EuaIPv6Dyn)));
Harald Welteed7a1772017-08-09 20:26:20 +02001108 ctx.pco_req := valueof(ts_PCO_IPv6_DNS);
1109 f_pdp_ctx_act(ctx);
Pau Espin Pedrol363ba482018-01-29 18:42:00 +01001110
1111 /* verify PCO contains both primary and secondary DNS */
1112 var OCT4 ggsn_ip6_dns1 := f_inet6_addr(m_ggsn_ip6_dns1);
1113 if (not match(f_PCO_extract_proto(ctx.pco_neg, '0003'O, 1), ggsn_ip6_dns1)) {
1114 setverdict(fail, "Primary DNS IPv6 PCO option not found");
1115 }
1116
1117 var OCT4 ggsn_ip6_dns2 := f_inet6_addr(m_ggsn_ip6_dns2);
1118 if (not match(f_PCO_extract_proto(ctx.pco_neg, '0003'O, 2), ggsn_ip6_dns2)) {
1119 setverdict(fail, "Secondary DNS IPv6 PCO option not found");
1120 }
1121
Harald Welteed7a1772017-08-09 20:26:20 +02001122 f_pdp_ctx_del(ctx, '1'B);
1123 }
1124
Harald Welte0ef285b2017-08-13 20:06:01 +02001125 /* Test PDP context activation for dynamic IPv6 EUA with IPv6 DNS in PCO and router solicitation/advertisement */
Harald Welteed7a1772017-08-09 20:26:20 +02001126 testcase TC_pdp6_act_deact_icmp6() runs on GT_CT {
1127 f_init();
1128
Harald Welteed097432017-08-13 13:28:49 +02001129 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInet6, valueof(t_EuaIPv6Dyn)));
Harald Welteed7a1772017-08-09 20:26:20 +02001130 ctx.pco_req := valueof(ts_PCO_IPv6_DNS);
1131 f_pdp_ctx_act(ctx);
Harald Welte231b9412017-08-09 17:16:31 +02001132
1133 //f_send_gtpu(ctx, c_router_solicit);
1134 //f_send_gtpu(ctx, c_neigh_solicit);
1135
1136 f_send_gtpu(ctx, f_icmpv6_rs_for_pdp(ctx));
1137 f_wait_rtr_adv(ctx);
1138 f_send_gtpu(ctx, f_gen_icmpv6_neigh_solicit_for_pdp(ctx));
1139
Harald Welte811651e2017-08-05 15:25:06 +02001140 f_pdp_ctx_del(ctx, '1'B);
Harald Welte94ade362017-08-04 00:36:55 +02001141 }
1142
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001143 /* Test PDP context activation for dynamic IPv6 EUA with IPv6 DNS in PCO and router solicitation/advertisement.
1144 Test we can send ICMPv6 ping over GTPU to DNS server. */
1145 testcase TC_pdp6_act_deact_gtpu_access() runs on GT_CT {
1146 f_init();
1147 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInet6, valueof(t_EuaIPv6Dyn)));
1148 ctx.pco_req := valueof(ts_PCO_IPv6_DNS);
1149 f_pdp_ctx_act(ctx);
1150
1151 f_send_gtpu(ctx, f_icmpv6_rs_for_pdp(ctx));
1152 f_wait_rtr_adv(ctx);
1153 f_send_gtpu(ctx, f_gen_icmpv6_neigh_solicit_for_pdp(ctx));
1154
1155 var OCT16 dns1_addr := f_PCO_extract_proto(ctx.pco_neg, '0003'O);
1156
1157 /* Check if we can use valid link-local src addr. */
1158 var OCT16 saddr_ll := f_ipv6_link_local(ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address);
1159 f_send_gtpu(ctx, f_gen_icmpv6_echo(saddr_ll, dns1_addr));
1160 f_wait_icmp6_echo_reply(ctx);
1161
1162 /* Check that attempting RA with another ll src addr won't work, packet dropped: */
1163 var OCT16 saddr_ll_wrong := f_ipv6_mangle(saddr_ll, 8);
1164 f_send_gtpu(ctx, f_gen_icmpv6_router_solicitation(saddr_ll_wrong));
1165 f_wait_gtpu_fail(ctx);
1166
1167 /* Check if we can use valid global src addr, should work */
1168 var OCT16 saddr_glob := f_ipv6_global(ctx.eua.endUserAddress.endUserAddressIPv6.ipv6_address);
1169 f_send_gtpu(ctx, f_gen_icmpv6_echo(saddr_glob, dns1_addr));
1170 f_wait_icmp6_echo_reply(ctx);
1171
1172 /* Assert that packets with wrong global src addr are dropped by GGSN */
1173 var OCT16 saddr_wrong := f_ipv6_mangle(saddr_glob);
1174 f_send_gtpu(ctx, f_gen_icmpv6_echo(saddr_wrong, dns1_addr));
1175 f_wait_gtpu_fail(ctx);
1176
1177 /* Send an IPv4 ICMP ECHO REQUEST to APN6, should fail (packet dropped) */
1178 var OCT4 saddr_v4 := f_inet_addr("192.168.10.2");
1179 var OCT4 daddr_v4 := f_inet_addr("8.8.8.8");
1180 f_send_gtpu(ctx, f_gen_icmpv4_echo(saddr_v4, daddr_v4));
1181 f_wait_gtpu_fail(ctx);
1182
1183 f_pdp_ctx_del(ctx, '1'B);
1184 }
1185
Harald Welte0ef285b2017-08-13 20:06:01 +02001186 /* Test PDP context activation for dynamic IPv4 EUA without DNS request */
Harald Welteed7a1772017-08-09 20:26:20 +02001187 testcase TC_pdp4_act_deact() runs on GT_CT {
1188 f_init();
Harald Welteed097432017-08-13 13:28:49 +02001189 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInternet, valueof(t_EuaIPv4Dyn)));
Harald Welteed7a1772017-08-09 20:26:20 +02001190 f_pdp_ctx_act(ctx);
1191 f_pdp_ctx_del(ctx, '1'B);
1192 }
1193
Harald Welte0ef285b2017-08-13 20:06:01 +02001194 /* Test PDP context activation for dynamic IPv4 EUA with IPv4 DNS in IPCP */
Harald Welteed7a1772017-08-09 20:26:20 +02001195 testcase TC_pdp4_act_deact_ipcp() runs on GT_CT {
1196 f_init();
Pau Espin Pedrolf69a4382018-01-29 13:09:00 +01001197 var OCT4 ggsn_ip4_dns1 := f_inet_addr(m_ggsn_ip4_dns1);
1198 var OCT4 ggsn_ip4_dns2 := f_inet_addr(m_ggsn_ip4_dns2);
Harald Welteed097432017-08-13 13:28:49 +02001199 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInternet, valueof(t_EuaIPv4Dyn)));
Harald Weltedca80052017-08-13 20:01:38 +02001200 ctx.pco_req := valueof(ts_PCO_IPv4_DNS_IPCP);
Harald Welteed7a1772017-08-09 20:26:20 +02001201 f_pdp_ctx_act(ctx);
Harald Welte71a36022017-12-04 18:55:58 +01001202 /* verify IPCP is at all contained */
1203 if (not match(ctx.pco_neg, tr_PCO_Contains('8021'O))) {
1204 setverdict(fail, "IPCP not found in PCO");
1205 }
1206 /* verify IPCP contains both primary and secondary DNS */
1207 var IpcpPacket ipcp := dec_IpcpPacket(f_PCO_extract_proto(ctx.pco_neg, '8021'O));
Pau Espin Pedrolf69a4382018-01-29 13:09:00 +01001208 if (not match(ipcp, tr_IPCP_Ack_DNS(0, ggsn_ip4_dns1, ggsn_ip4_dns2))) {
1209 if (not match(ipcp, tr_IPCP_Ack_DNS(0))) {
1210 setverdict(fail, "Primary/Secondary DNS PCO IPCP option not found");
1211 } else {
1212 setverdict(fail, "Primary/Secondary DNS PCO IPCP option found but not matching expected values");
1213 }
Harald Welte71a36022017-12-04 18:55:58 +01001214 }
Harald Welteed7a1772017-08-09 20:26:20 +02001215 f_pdp_ctx_del(ctx, '1'B);
1216 }
1217
Harald Welte0ef285b2017-08-13 20:06:01 +02001218 /* Test PDP context activation for dynamic IPv4 EUA with IPv4 DNS in PCO */
Harald Welteed7a1772017-08-09 20:26:20 +02001219 testcase TC_pdp4_act_deact_pcodns() runs on GT_CT {
1220 f_init();
Harald Welteed097432017-08-13 13:28:49 +02001221 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInternet, valueof(t_EuaIPv4Dyn)));
Harald Weltedca80052017-08-13 20:01:38 +02001222 ctx.pco_req := valueof(ts_PCO_IPv4_DNS_CONT);
Harald Welteed7a1772017-08-09 20:26:20 +02001223 f_pdp_ctx_act(ctx);
Pau Espin Pedrol363ba482018-01-29 18:42:00 +01001224
1225 /* verify PCO contains both primary and secondary DNS */
1226 var OCT4 ggsn_ip4_dns1 := f_inet_addr(m_ggsn_ip4_dns1);
1227 if (not match(f_PCO_extract_proto(ctx.pco_neg, '000d'O, 1), ggsn_ip4_dns1)) {
1228 setverdict(fail, "Primary DNS IPv4 PCO option not found");
1229 }
1230
1231 var OCT4 ggsn_ip4_dns2 := f_inet_addr(m_ggsn_ip4_dns2);
1232 if (not match(f_PCO_extract_proto(ctx.pco_neg, '000d'O, 2), ggsn_ip4_dns2)) {
1233 setverdict(fail, "Secondary DNS IPv4 PCO option not found");
1234 }
1235
Harald Welteed7a1772017-08-09 20:26:20 +02001236 f_pdp_ctx_del(ctx, '1'B);
1237 }
1238
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001239 /* Test PDP context activation for dynamic IPv4 EUA.
1240 Test we can send ICMPv6 ping over GTPU to DNS server. */
1241 testcase TC_pdp4_act_deact_gtpu_access() runs on GT_CT {
1242 f_init();
1243 var PdpContext ctx := valueof(t_DefinePDP(f_rnd_imsi('26242'H), '1234'O, c_ApnInternet, valueof(t_EuaIPv4Dyn)));
1244 ctx.pco_req := valueof(ts_PCO_IPv4_DNS_CONT);
1245 f_pdp_ctx_act(ctx);
1246
1247 var OCT4 dns1_addr := f_PCO_extract_proto(ctx.pco_neg, '000d'O);
1248
1249 /* Check if we can use valid global src addr, should work */
1250 var OCT4 saddr := ctx.eua.endUserAddress.endUserAddressIPv4.ipv4_address;
1251 f_send_gtpu(ctx, f_gen_icmpv4_echo(saddr, dns1_addr));
1252 f_wait_icmp4_echo_reply(ctx);
1253
1254 /* Assert that packets with wrong global src addr are dropped by GGSN */
1255 var OCT4 saddr_wrong := substr(saddr, 0, 3) & (saddr[3] xor4b '11'O);
1256 f_send_gtpu(ctx, f_gen_icmpv4_echo(saddr_wrong, dns1_addr));
1257 f_wait_gtpu_fail(ctx);
1258
1259 /* Send an IPv6 RA to APN4, should fail (packet dropped) */
1260 var OCT16 saddr_v6 := f_inet6_addr("fde4:8dba:82e1:2000:1:2:3:4");
1261 f_send_gtpu(ctx, f_gen_icmpv6_router_solicitation(saddr_v6));
1262 f_wait_gtpu_fail(ctx);
1263 f_pdp_ctx_del(ctx, '1'B);
1264 }
1265
Harald Weltedca80052017-08-13 20:01:38 +02001266 testcase TC_echo_req_resp() runs on GT_CT {
1267 f_init();
1268 f_send_gtpc(ts_GTPC_PING(g_peer_c, g_c_seq_nr));
1269 T_default.start;
1270 alt {
1271 [] GTPC.receive(tr_GTPC_PONG(g_peer_c)) { setverdict(pass); };
1272 [] GTPC.receive { repeat; };
1273 [] T_default.timeout { setverdict(fail); }
1274 }
1275 T_default.stop;
1276 }
1277
Harald Welte94ade362017-08-04 00:36:55 +02001278 control {
Harald Welteed7a1772017-08-09 20:26:20 +02001279 execute(TC_pdp4_act_deact());
1280 execute(TC_pdp4_act_deact_ipcp());
1281 execute(TC_pdp4_act_deact_pcodns());
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001282 execute(TC_pdp4_act_deact_gtpu_access());
Harald Welteed7a1772017-08-09 20:26:20 +02001283
1284 execute(TC_pdp6_act_deact());
1285 execute(TC_pdp6_act_deact_pcodns());
1286 execute(TC_pdp6_act_deact_icmp6());
Pau Espin Pedrol3d9338f2018-01-29 20:42:54 +01001287 execute(TC_pdp6_act_deact_gtpu_access());
Harald Weltedca80052017-08-13 20:01:38 +02001288
1289 execute(TC_echo_req_resp());
Harald Welte94ade362017-08-04 00:36:55 +02001290 }
Harald Welte379d45a2017-08-03 09:55:15 +02001291}