1use crate::{
2 datalink::{
3 arp::ArpReader,
4 ethernet::{EthernetReader, ETHERNET_MIN_FRAME_LENGTH},
5 },
6 misc::{EtherType, Icmpv4Type, Icmpv6Type, IpInIp, IpProtocol},
7 network::{
8 checksum::{pseudo_header, verify_internet_checksum},
9 icmpv4::{Icmpv4Reader, ICMPV4_MAX_VALID_CODE},
10 icmpv6::Icmpv6Reader,
11 ipv4::{IPv4Reader, IPV4_MIN_HEADER_LENGTH},
12 ipv6::IPv6Reader,
13 },
14 transport::{
15 tcp::{TcpReader, TCP_MIN_HEADER_LENGTH},
16 udp::UdpReader,
17 },
18};
19
20#[derive(Debug)]
22pub struct PacketParser<'a> {
23 pub ethernet: Option<EthernetReader<'a>>,
24 pub arp: Option<ArpReader<'a>>,
25 pub ipv4: Option<IPv4Reader<'a>>,
26 pub ipv6: Option<IPv6Reader<'a>>,
27 pub ip_in_ip: Option<IpInIp<'a>>,
28 pub tcp: Option<TcpReader<'a>>,
29 pub udp: Option<UdpReader<'a>>,
30 pub icmpv4: Option<Icmpv4Reader<'a>>,
31 pub icmpv6: Option<Icmpv6Reader<'a>>,
32}
33
34impl<'a> PacketParser<'a> {
35 #[inline]
37 fn new() -> Self {
38 Self {
39 ethernet: None,
40 arp: None,
41 ipv4: None,
42 ipv6: None,
43 ip_in_ip: None,
44 tcp: None,
45 udp: None,
46 icmpv4: None,
47 icmpv6: None,
48 }
49 }
50
51 #[inline]
53 pub fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
54 let mut parser = Self::new();
55
56 let ethernet = EthernetReader::parse(bytes)?;
57 let payload = &bytes[ethernet.header_len()..];
58
59 match EtherType::from(ethernet.ethertype()) {
60 EtherType::Arp => parser.arp = Some(ArpReader::parse(payload)?),
61 EtherType::Ipv4 => parser.parse_ipv4(payload, true)?,
62 EtherType::Ipv6 => parser.parse_ipv6(payload, true)?,
63 _ => (), }
65
66 parser.ethernet = Some(ethernet);
67
68 Ok(parser)
69 }
70
71 #[inline]
73 fn parse_ipv4(&mut self, payload: &'a [u8], from_ether: bool) -> Result<(), &'static str> {
74 let ipv4 = IPv4Reader::parse(payload)?;
75
76 let payload = ipv4.payload()?;
77 let protocol = IpProtocol::from(ipv4.protocol());
78
79 self.parse_protocol(protocol, payload, &ipv4)?;
80
81 if from_ether {
82 self.ipv4 = Some(ipv4);
83 } else {
84 self.ip_in_ip = Some(IpInIp::Ipv4(ipv4));
85 }
86
87 Ok(())
88 }
89
90 #[inline]
92 fn parse_ipv6(&mut self, payload: &'a [u8], from_ether: bool) -> Result<(), &'static str> {
93 let ipv6 = IPv6Reader::parse(payload)?;
94
95 let payload = ipv6.upper_layer_payload();
96 let next_header = IpProtocol::from(ipv6.final_next_header());
97
98 self.parse_protocol(next_header, payload, &ipv6)?;
99
100 if from_ether {
101 self.ipv6 = Some(ipv6);
102 } else {
103 self.ip_in_ip = Some(IpInIp::Ipv6(ipv6));
104 }
105
106 Ok(())
107 }
108
109 #[inline]
111 fn parse_protocol<T: VerifyReader<'a>>(
112 &mut self,
113 protocol: IpProtocol,
114 payload: &'a [u8],
115 reader: &T,
116 ) -> Result<(), &'static str> {
117 match protocol {
118 IpProtocol::Tcp => {
119 self.tcp = Some(TcpReader::parse(payload)?);
120 reader.verify_checksum()?;
121 }
122 IpProtocol::Udp => {
123 self.udp = Some(UdpReader::parse(payload)?);
124 reader.verify_checksum()?;
125 }
126 IpProtocol::Icmpv4 => {
127 self.icmpv4 = Some(Icmpv4Reader::parse(payload)?);
128 reader.verify_checksum()?;
129 }
130 IpProtocol::Icmpv6 => {
131 self.icmpv6 = Some(Icmpv6Reader::parse(payload)?);
132 reader.verify_checksum()?;
133 }
134 IpProtocol::Ipv4 => self.parse_ipv4(payload, false)?,
135 IpProtocol::Ipv6 => self.parse_ipv6(payload, false)?,
136 _ => (), }
138
139 Ok(())
140 }
141}
142
143pub trait ParseReader<'a>: Sized {
145 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str>;
151}
152
153impl<'a> ParseReader<'a> for EthernetReader<'a> {
154 #[inline]
158 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
159 if bytes.len() < ETHERNET_MIN_FRAME_LENGTH {
160 return Err("Slice needs to be least 64 bytes long to be a valid Ethernet frame.");
161 }
162
163 EthernetReader::new(bytes)
164 }
165}
166
167impl<'a> ParseReader<'a> for ArpReader<'a> {
168 #[inline]
172 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
173 let reader = ArpReader::new(bytes)?;
174
175 if reader.oper() > 2 {
176 return Err("ARP operation field is invalid, expected request (1) or reply (2).");
177 }
178
179 Ok(reader)
180 }
181}
182
183impl<'a> ParseReader<'a> for IPv4Reader<'a> {
184 #[inline]
188 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
189 let reader = IPv4Reader::new(bytes)?;
190
191 if reader.version() != 4 {
192 return Err("IPv4 version field is invalid. Expected version 4.");
193 }
194
195 if reader.header_len() < IPV4_MIN_HEADER_LENGTH {
196 return Err("IPv4 IHL field is invalid. Indicated header length is too short.");
197 }
198
199 if bytes.len() < reader.header_len() {
200 return Err("IPv4 header length is invalid. Indicated header length is too long.");
201 }
202
203 if bytes.len() != reader.total_length() as usize {
204 return Err("IPv4 total length field is invalid. Does not match actual length.");
205 }
206
207 if !reader.valid_checksum()? {
208 return Err("IPv4 checksum is invalid.");
209 }
210
211 Ok(reader)
212 }
213}
214
215impl<'a> ParseReader<'a> for IPv6Reader<'a> {
216 #[inline]
222 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
223 let reader = IPv6Reader::new(bytes)?;
224
225 if reader.version() != 6 {
226 return Err("IPv6 version field is invalid. Expected version 6.");
227 }
228
229 Ok(reader)
230 }
231}
232
233impl<'a> ParseReader<'a> for TcpReader<'a> {
234 #[inline]
238 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
239 let reader = TcpReader::new(bytes)?;
240
241 if reader.header_len() < TCP_MIN_HEADER_LENGTH {
242 return Err("TCP data offset field is invalid. Indicated header length is too short.");
243 }
244
245 if reader.flags() == 0 {
246 return Err("TCP flags field is invalid.");
247 }
248
249 Ok(reader)
250 }
251}
252
253impl<'a> ParseReader<'a> for UdpReader<'a> {
254 #[inline]
258 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
259 let reader = UdpReader::new(bytes)?;
260
261 if reader.length() as usize != reader.header_len() + reader.payload().len() {
262 return Err("UDP length field is invalid. Does not match actual length.");
263 }
264
265 Ok(reader)
266 }
267}
268
269impl<'a> ParseReader<'a> for Icmpv4Reader<'a> {
270 #[inline]
274 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
275 let reader = Icmpv4Reader::new(bytes)?;
276
277 if Icmpv4Type::from(reader.icmp_type()) == Icmpv4Type::Unknown {
278 return Err("ICMPv4 type field is invalid.");
279 }
280
281 if reader.icmp_code() > ICMPV4_MAX_VALID_CODE {
282 return Err("ICMPv4 code field is invalid.");
283 }
284
285 Ok(reader)
286 }
287}
288
289impl<'a> ParseReader<'a> for Icmpv6Reader<'a> {
290 #[inline]
294 fn parse(bytes: &'a [u8]) -> Result<Self, &'static str> {
295 let reader = Icmpv6Reader::new(bytes)?;
296
297 if Icmpv6Type::from(reader.icmp_type()) == Icmpv6Type::Unknown {
298 return Err("ICMPv6 type field is invalid.");
299 }
300
301 Ok(reader)
302 }
303}
304
305pub trait VerifyReader<'a> {
307 fn verify_checksum(&self) -> Result<(), &'static str>;
309}
310
311impl<'a> VerifyReader<'a> for IPv4Reader<'a> {
312 #[inline]
316 fn verify_checksum(&self) -> Result<(), &'static str> {
317 let src: [u8; 4] = self.src_ip().try_into().unwrap(); let dest: [u8; 4] = self.dest_ip().try_into().unwrap(); let protocol = self.protocol();
320
321 let sum = if IpProtocol::from(protocol) == IpProtocol::Icmpv4 {
323 0
324 } else {
325 pseudo_header(&src, &dest, protocol, self.payload()?.len())
326 };
327
328 if !verify_internet_checksum(self.payload()?, sum) {
329 return Err("IPv4 encapsulated checksum is invalid.");
330 }
331
332 Ok(())
333 }
334}
335
336impl<'a> VerifyReader<'a> for IPv6Reader<'a> {
337 #[inline]
341 fn verify_checksum(&self) -> Result<(), &'static str> {
342 if IpProtocol::from(self.final_next_header()) == IpProtocol::NoNextHeader {
343 return Ok(());
344 }
345
346 let src: [u8; 16] = self.src_addr().try_into().unwrap(); let dest: [u8; 16] = self.dest_addr().try_into().unwrap(); let sum = pseudo_header(
350 &src,
351 &dest,
352 self.final_next_header(),
353 self.upper_layer_payload().len(),
354 );
355
356 if !verify_internet_checksum(self.upper_layer_payload(), sum) {
357 return Err("IPv6 encapsulated checksum is invalid.");
358 }
359
360 Ok(())
361 }
362}
363
364#[cfg(test)]
365mod tests {
366 use super::*;
367
368 #[test]
369 fn parse_frame_too_short() {
370 let packet = [
372 4, 180, 254, 154, 129, 199, 52, 151, 246, 148, 2, 15, 8, 0, 53, 143, 48, 57, 212, 49,
373 112, 57, 123, 6, 87, 113, 192, 168, 1, 1, 192, 168, 1, 2, 0, 99, 0, 11, 0, 0, 0, 123,
374 0, 0, 1, 65, 59, 99, 16, 225, 41, 81, 4, 210,
375 ];
376
377 let parser = PacketParser::parse(&packet);
379
380 assert!(parser.is_err());
382 }
383
384 #[test]
385 fn vlan_tagged_frame() {
386 let packet = [
388 0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x5E, 0x4D, 0x3C, 0x2B, 0x1A, 0x00, 0x81, 0x00, 0x00, 0x64, 0x08, 0x00, 0x45, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x6b, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00, 0x02, 0x04, 0xD2, 0x16, 0x2E, 0x00, 0x1a, 0x63, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
410
411 let parser = PacketParser::parse(&packet);
413
414 assert!(parser.is_ok());
416
417 let unwrapped = parser.unwrap();
419
420 assert!(unwrapped.ethernet.is_some());
422 assert!(unwrapped.ipv4.is_some());
423 assert!(unwrapped.udp.is_some());
424
425 assert!(unwrapped.icmpv4.is_none());
427 assert!(unwrapped.arp.is_none());
428 assert!(unwrapped.tcp.is_none());
429
430 let ethernet = unwrapped.ethernet.unwrap();
432
433 assert!(ethernet.vlan_tag().is_some());
435 assert!(ethernet.double_vlan_tag().is_none());
436
437 assert_eq!(ethernet.vlan_tag().unwrap(), (0x8100, 100));
439
440 assert_eq!(ethernet.ethertype(), 0x0800);
442 }
443
444 #[test]
445 fn double_vlan_tagged_frame() {
446 let packet = [
448 0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x5E, 0x4D, 0x3C, 0x2B, 0x1A, 0x00, 0x88, 0xA8, 0x00, 0xC8, 0x81, 0x00, 0x00, 0x64, 0x08, 0x00, 0x45, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x6f, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00, 0x02, 0x04, 0xD2, 0x16, 0x2E, 0x00, 0x16, 0x63, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
471
472 let parser = PacketParser::parse(&packet);
474
475 assert!(parser.is_ok());
477
478 let unwrapped = parser.unwrap();
480
481 assert!(unwrapped.ethernet.is_some());
483 assert!(unwrapped.ipv4.is_some());
484 assert!(unwrapped.udp.is_some());
485
486 assert!(unwrapped.icmpv4.is_none());
488 assert!(unwrapped.arp.is_none());
489 assert!(unwrapped.tcp.is_none());
490
491 let ethernet = unwrapped.ethernet.unwrap();
493
494 assert!(ethernet.vlan_tag().is_none());
496 assert!(ethernet.double_vlan_tag().is_some());
497
498 assert_eq!(
500 ethernet.double_vlan_tag().unwrap(),
501 ((0x88A8, 200), (0x8100, 100))
502 );
503
504 assert_eq!(ethernet.ethertype(), 0x0800);
506 }
507
508 #[test]
509 fn icmpv4_echo_response() {
510 let packet = [
512 0x08, 0x00, 0x20, 0x86, 0x35, 0x4b, 0x00, 0xe0, 0xf7, 0x26, 0x3f, 0xe9, 0x08, 0x00,
513 0x45, 0x00, 0x00, 0x54, 0xaa, 0xfb, 0x40, 0x00, 0xfc, 0x01, 0xfa, 0x30, 0x8b, 0x85,
514 0xe9, 0x02, 0x8b, 0x85, 0xd9, 0x6e, 0x00, 0x00, 0x45, 0xda, 0x1e, 0x60, 0x00, 0x00,
515 0x33, 0x5e, 0x3a, 0xb8, 0x00, 0x00, 0x42, 0xac, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
516 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
517 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
518 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
519 ];
520
521 let parser = PacketParser::parse(&packet);
523
524 assert!(parser.is_ok());
526
527 let unwrapped = parser.unwrap();
529
530 assert!(unwrapped.ethernet.is_some());
532 assert!(unwrapped.ipv4.is_some());
533 assert!(unwrapped.icmpv4.is_some());
534 assert!(unwrapped.arp.is_none());
535 assert!(unwrapped.tcp.is_none());
536 assert!(unwrapped.udp.is_none());
537
538 let ethernet = unwrapped.ethernet.unwrap();
540 let ipv4 = unwrapped.ipv4.unwrap();
541 let icmpv4 = unwrapped.icmpv4.unwrap();
542
543 assert_eq!(ethernet.ethertype(), 0x0800);
545 assert_eq!(ipv4.protocol(), 1);
546 assert_eq!(ipv4.checksum(), 0xfa30);
547 assert_eq!(icmpv4.icmp_type(), 0);
548 assert_eq!(icmpv4.icmp_code(), 0);
549 assert_eq!(icmpv4.checksum(), 0x45da);
550 }
551
552 #[test]
553 fn ipv6_icmpv6() {
554 let packet = [
556 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd,
557 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
558 0x00, 0x00, 0x02, 0x00, 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0xfe, 0x80, 0x00, 0x00,
559 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x87, 0x00,
560 0x68, 0xbd, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
561 0x02, 0x60, 0x97, 0xff, 0xfe, 0x07, 0x69, 0xea, 0x01, 0x01, 0x00, 0x00, 0x86, 0x05,
562 0x80, 0xda,
563 ];
564
565 let parser = PacketParser::parse(&packet);
567
568 assert!(parser.is_ok());
570
571 let unwrapped = parser.unwrap();
573
574 assert!(unwrapped.ethernet.is_some());
576 assert!(unwrapped.ipv6.is_some());
577 assert!(unwrapped.icmpv6.is_some());
578
579 assert!(unwrapped.icmpv4.is_none());
581 assert!(unwrapped.arp.is_none());
582 assert!(unwrapped.tcp.is_none());
583 assert!(unwrapped.udp.is_none());
584
585 let ethernet = unwrapped.ethernet.unwrap();
587 let ipv6 = unwrapped.ipv6.unwrap();
588 let icmpv6 = unwrapped.icmpv6.unwrap();
589
590 assert_eq!(ethernet.ethertype(), 34525);
592 assert_eq!(ipv6.next_header(), 58);
593 assert_eq!(icmpv6.icmp_type(), 135);
594 assert_eq!(icmpv6.icmp_code(), 0);
595 }
596
597 #[test]
598 fn ipv6_udp_payload() {
599 let packet = [
601 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00, 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd,
602 0x60, 0x00, 0x00, 0x00, 0x00, 0x14, 0x11, 0x03, 0x3f, 0xfe, 0x05, 0x07, 0x00, 0x00,
603 0x00, 0x01, 0x02, 0x00, 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe, 0x05, 0x01,
604 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0, 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0xa0, 0x75,
605 0x82, 0xa1, 0x00, 0x14, 0x81, 0x13, 0x07, 0x03, 0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36,
606 0xef, 0x5d, 0x0a, 0x00,
607 ];
608
609 let parser = PacketParser::parse(&packet);
611
612 assert!(parser.is_ok());
614
615 let unwrapped = parser.unwrap();
617
618 assert!(unwrapped.ethernet.is_some());
620 assert!(unwrapped.ipv6.is_some());
621 assert!(unwrapped.udp.is_some());
622
623 assert!(unwrapped.icmpv4.is_none());
625 assert!(unwrapped.icmpv6.is_none());
626 assert!(unwrapped.arp.is_none());
627 assert!(unwrapped.tcp.is_none());
628
629 let udp = unwrapped.udp.unwrap();
631
632 assert_eq!(
634 udp.payload(),
635 [0x07, 0x03, 0x00, 0x00, 0xf9, 0xc8, 0xe7, 0x36, 0xef, 0x5d, 0x0a, 0x00]
636 );
637 }
638
639 #[test]
640 fn ipv6_routing_extension_header() {
641 let packet = [
643 0x86, 0x93, 0x23, 0xd3, 0x37, 0x8e, 0x22, 0x1a, 0x95, 0xd6, 0x7a, 0x23, 0x86, 0xdd,
644 0x60, 0x0f, 0xbb, 0x74, 0x00, 0x88, 0x2b, 0x3f, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
645 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
646 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x06,
647 0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
648 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
649 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
650 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x90,
651 0xa9, 0xa0, 0xba, 0x31, 0x1e, 0x8d, 0x02, 0x1b, 0x63, 0x8d, 0xa0, 0x12, 0x70, 0xf8,
652 0x8a, 0xf5, 0x00, 0x00, 0x02, 0x04, 0x07, 0x94, 0x04, 0x02, 0x08, 0x0a, 0x80, 0x1d,
653 0xa5, 0x22, 0x80, 0x1d, 0xa5, 0x22, 0x01, 0x03, 0x03, 0x07,
654 ];
655
656 let parser = PacketParser::parse(&packet);
658
659 assert!(parser.is_ok());
661
662 let unwrapped = parser.unwrap();
664
665 assert!(unwrapped.ethernet.is_some());
667 assert!(unwrapped.ipv6.is_some());
668 assert!(unwrapped.tcp.is_some());
669
670 assert!(unwrapped.icmpv4.is_none());
672 assert!(unwrapped.icmpv6.is_none());
673 assert!(unwrapped.arp.is_none());
674 assert!(unwrapped.udp.is_none());
675
676 let ipv6 = unwrapped.ipv6.unwrap();
678
679 assert!(ipv6.extension_headers.is_some());
681
682 let extension_headers = ipv6.extension_headers.unwrap();
684
685 assert!(extension_headers.routing.is_some());
687 }
688
689 #[test]
690 fn ipv6_hop_by_hop_options() {
691 let packet = [
693 0x44, 0x2a, 0x60, 0xf6, 0x27, 0x8a, 0x00, 0x0c, 0x29, 0x30, 0x76, 0xb5, 0x86, 0xdd,
694 0x60, 0x00, 0x00, 0x04, 0x00, 0x1c, 0x00, 0x40, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
695 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00,
696 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x00,
697 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x0c, 0x00, 0x87, 0x00, 0x00, 0x52, 0x0c,
698 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x40, 0x38, 0xcb, 0x04, 0x00, 0x00,
699 ];
700
701 let parser = PacketParser::parse(&packet);
703
704 assert!(parser.is_ok());
706
707 let unwrapped = parser.unwrap();
709
710 assert!(unwrapped.ethernet.is_some());
712 assert!(unwrapped.ipv6.is_some());
713 assert!(unwrapped.tcp.is_some());
714
715 assert!(unwrapped.icmpv4.is_none());
717 assert!(unwrapped.icmpv6.is_none());
718 assert!(unwrapped.arp.is_none());
719 assert!(unwrapped.udp.is_none());
720
721 let ipv6 = unwrapped.ipv6.unwrap();
723
724 assert!(ipv6.extension_headers.is_some());
726
727 let extension_headers = ipv6.extension_headers.unwrap();
729
730 assert!(extension_headers.hop_by_hop.is_some());
732 }
733
734 #[test]
735 fn ipv6_destination_options() {
736 let packet = [
738 0x44, 0x2a, 0x60, 0xf6, 0x27, 0x8a, 0x00, 0x0c, 0x29, 0x30, 0x76, 0xb5, 0x86, 0xdd,
739 0x60, 0x00, 0x00, 0x05, 0x00, 0x1c, 0x3c, 0x40, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
740 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00,
741 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x00,
742 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x0d, 0x00, 0x87, 0x00, 0x00, 0x52, 0x0d,
743 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x40, 0x38, 0xcb, 0x02, 0x00, 0x00,
744 ];
745
746 let parser = PacketParser::parse(&packet);
748
749 assert!(parser.is_ok());
751
752 let unwrapped = parser.unwrap();
754
755 assert!(unwrapped.ethernet.is_some());
757 assert!(unwrapped.ipv6.is_some());
758 assert!(unwrapped.tcp.is_some());
759
760 assert!(unwrapped.icmpv4.is_none());
762 assert!(unwrapped.icmpv6.is_none());
763 assert!(unwrapped.arp.is_none());
764 assert!(unwrapped.udp.is_none());
765
766 let ipv6 = unwrapped.ipv6.unwrap();
768
769 assert!(ipv6.extension_headers.is_some());
771
772 let extension_headers = ipv6.extension_headers.unwrap();
774
775 assert!(extension_headers.destination_1st.is_some());
777 }
778
779 #[test]
780 fn fragment_and_authentication_header() {
781 let pkt1 = [
783 0x70, 0x77, 0x81, 0xdd, 0xc3, 0x7c, 0x00, 0x26, 0x9e, 0x71, 0x9d, 0x33, 0x86, 0xdd,
784 0x60, 0x00, 0x00, 0x00, 0x00, 0x98, 0x2c, 0x9c, 0x26, 0x05, 0x60, 0x00, 0x23, 0xc0,
785 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x20, 0x01, 0x41, 0xd0,
786 0x00, 0x08, 0xcc, 0xd8, 0x01, 0x37, 0x00, 0x74, 0x01, 0x87, 0x01, 0x01, 0x3a, 0x00,
787 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8, 0x80, 0x00, 0x0b, 0xaf, 0x15, 0x5e, 0x3a, 0xfb,
788 0x4c, 0x9a, 0x02, 0x5c, 0x81, 0x31, 0xcf, 0xf7, 0x5c, 0xed, 0x0c, 0x57, 0x8a, 0xd0,
789 0x7d, 0x40, 0x11, 0x01, 0x08, 0x3a, 0x61, 0x57, 0xea, 0x73, 0xc4, 0xa1, 0x8a, 0x2e,
790 0x23, 0x3b, 0xdc, 0x78, 0x80, 0xa1, 0x25, 0x54, 0xe3, 0x12, 0x90, 0x48, 0xa6, 0x02,
791 0xff, 0x3b, 0x2f, 0xd4, 0x0a, 0x7a, 0x86, 0xf2, 0x81, 0x2d, 0x36, 0x16, 0x9e, 0x20,
792 0x34, 0xf5, 0x0d, 0x63, 0xef, 0xc7, 0xd5, 0x2a, 0x8f, 0x43, 0x6c, 0x64, 0xb7, 0x69,
793 0x0f, 0x6f, 0x86, 0xca, 0x55, 0x57, 0xd7, 0x71, 0x68, 0x1e, 0xac, 0x9c, 0x68, 0x6f,
794 0x93, 0x89, 0xe9, 0x1d, 0x34, 0xbe, 0xdc, 0x50, 0x19, 0x6d, 0x00, 0x53, 0x2d, 0x6a,
795 0xcb, 0x4a, 0x57, 0x61, 0xcf, 0x63, 0x99, 0x19, 0x42, 0x45, 0x46, 0xc7, 0x30, 0x57,
796 0x95, 0x1d, 0x67, 0x55, 0x0a, 0xa7, 0xb0, 0x00, 0x4a, 0xc1, 0x42, 0x43, 0x0e, 0xe4,
797 0x2e, 0x51, 0x05, 0xe2, 0x8c, 0x42, 0xa7, 0xae, 0x4f, 0x1a,
798 ];
799
800 let prs1 = PacketParser::parse(&pkt1);
802
803 assert!(prs1.is_ok());
805
806 let pkt2 = [
808 0x00, 0x1e, 0x7a, 0x79, 0x3f, 0x10, 0x00, 0x15, 0x62, 0x6a, 0xfe, 0xf1, 0x86, 0xdd,
809 0x6e, 0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
810 0x00, 0x00, 0x02, 0x15, 0x62, 0xff, 0xfe, 0x6a, 0xfe, 0xf1, 0xfe, 0x80, 0x00, 0x00,
811 0x00, 0x00, 0x00, 0x00, 0x02, 0x1e, 0x7a, 0xff, 0xfe, 0x79, 0x3f, 0x10, 0x59, 0x04,
812 0x00, 0x00, 0x00, 0x00, 0x09, 0x07, 0x00, 0x00, 0x00, 0xff, 0xd1, 0x54, 0xcc, 0xe6,
813 0x68, 0xcb, 0xcf, 0xfa, 0xfe, 0xbb, 0x93, 0xb8, 0x03, 0x01, 0x00, 0x2c, 0xc0, 0xa8,
814 0xff, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x22, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
815 0x01, 0x00, 0x00, 0x13, 0x00, 0x0a, 0x00, 0x28, 0xc0, 0xa8, 0xff, 0x0f, 0xc0, 0xa8,
816 0xff, 0x0e, 0xc0, 0xa8, 0xff, 0x0b, 0xc0, 0xa8, 0xff, 0x0f, 0x7f, 0xf4, 0xe0, 0xb7,
817 ];
818
819 let prs2 = PacketParser::parse(&pkt2);
821
822 assert!(prs2.is_ok());
824 }
825
826 #[test]
827 fn extension_headers_chained() {
828 let packet = [
831 0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5F, 0x86, 0xDD, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x40, 0x20, 0x01, 0x0D, 0xB8, 0x85, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x34, 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0xB3, 0xFF, 0xFE, 0x1E, 0x83, 0x29, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
851
852 let parser = PacketParser::parse(&packet);
854
855 assert!(parser.is_ok());
857
858 let unwrapped = parser.unwrap();
860
861 assert!(unwrapped.ethernet.is_some());
863 assert!(unwrapped.ipv6.is_some());
864
865 assert!(unwrapped.icmpv4.is_none());
867 assert!(unwrapped.icmpv6.is_none());
868 assert!(unwrapped.arp.is_none());
869 assert!(unwrapped.udp.is_none());
870 assert!(unwrapped.tcp.is_none());
871
872 let ipv6 = unwrapped.ipv6.unwrap();
874
875 assert!(ipv6.extension_headers.is_some());
877
878 let extension_headers = ipv6.extension_headers.unwrap();
880
881 assert!(extension_headers.hop_by_hop.is_some());
883
884 assert!(extension_headers.destination_1st.is_some());
886 }
887
888 #[test]
889 pub fn ipv6_in_ipv6_with_extension_header() {
890 let packet = [
892 0x86, 0x93, 0x23, 0xd3, 0x37, 0x8e, 0x22, 0x1a, 0x95, 0xd6, 0x7a, 0x23, 0x86, 0xdd,
893 0x60, 0x0f, 0xbb, 0x74, 0x00, 0x88, 0x2b, 0x3f, 0xfc, 0x00, 0x00, 0x42, 0x00, 0x00,
894 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfc, 0x00, 0x00, 0x02,
895 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x29, 0x06,
896 0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
897 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
898 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02,
899 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x60, 0x0f,
900 0xbb, 0x74, 0x00, 0x28, 0x06, 0x40, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
901 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x02, 0x00, 0x00,
902 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x90, 0xa9, 0xa0,
903 0xba, 0x31, 0x1e, 0x8d, 0x02, 0x1b, 0x63, 0x8d, 0xa0, 0x12, 0x70, 0xf8, 0x8a, 0xf5,
904 0x00, 0x00, 0x02, 0x04, 0x07, 0x94, 0x04, 0x02, 0x08, 0x0a, 0x80, 0x1d, 0xa5, 0x22,
905 0x80, 0x1d, 0xa5, 0x22, 0x01, 0x03, 0x03, 0x07,
906 ];
907
908 let parser = PacketParser::parse(&packet);
910
911 assert!(parser.is_ok());
913 }
914
915 #[test]
916 pub fn ipv6_in_ipv4() {
917 let packet = [
919 0xc2, 0x01, 0x42, 0x02, 0x00, 0x00, 0xc2, 0x00, 0x42, 0x02, 0x00, 0x00, 0x08, 0x00,
920 0x45, 0x00, 0x00, 0x78, 0x00, 0x09, 0x00, 0x00, 0xff, 0x29, 0xa7, 0x51, 0x0a, 0x00,
921 0x00, 0x01, 0x0a, 0x00, 0x00, 0x02, 0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3a, 0x40,
922 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
923 0x00, 0x01, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
924 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x7e, 0x8f, 0x18, 0xdc, 0x00, 0x00, 0x00, 0x01,
925 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
926 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
927 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
928 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
929 ];
930
931 let parser = PacketParser::parse(&packet);
933
934 assert!(parser.is_ok());
936 }
937
938 #[test]
939 fn ipv4_in_ipv4() {
940 let packet = [
942 0xc2, 0x01, 0x57, 0x75, 0x00, 0x00, 0xc2, 0x00, 0x57, 0x75, 0x00, 0x00, 0x08, 0x00,
943 0x45, 0x00, 0x00, 0x78, 0x00, 0x14, 0x00, 0x00, 0xff, 0x04, 0xa7, 0x6b, 0x0a, 0x00,
944 0x00, 0x01, 0x0a, 0x00, 0x00, 0x02, 0x45, 0x00, 0x00, 0x64, 0x00, 0x14, 0x00, 0x00,
945 0xff, 0x01, 0xb5, 0x7f, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x08, 0x00,
946 0x43, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x3b, 0x38,
947 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
948 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
949 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
950 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
951 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
952 ];
953
954 let parser = PacketParser::parse(&packet);
956
957 assert!(parser.is_ok());
959 }
960}