1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
// Copyright (c) 2014, 2015 Robert Clipsham <robert@octarineparrot.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! An ICMP packet abstraction.
use crate::PrimitiveValues;
use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::types::*;
/// Represents the "ICMP type" header field.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IcmpType(pub u8);
impl IcmpType {
/// Create a new `IcmpType` instance.
pub fn new(val: u8) -> IcmpType {
IcmpType(val)
}
}
impl PrimitiveValues for IcmpType {
type T = (u8,);
fn to_primitive_values(&self) -> (u8,) {
(self.0,)
}
}
/// Represents the "ICMP code" header field.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IcmpCode(pub u8);
impl IcmpCode {
/// Create a new `IcmpCode` instance.
pub fn new(val: u8) -> IcmpCode {
IcmpCode(val)
}
}
impl PrimitiveValues for IcmpCode {
type T = (u8,);
fn to_primitive_values(&self) -> (u8,) {
(self.0,)
}
}
/// Represents a generic ICMP packet.
#[packet]
pub struct Icmp {
#[construct_with(u8)]
pub icmp_type: IcmpType,
#[construct_with(u8)]
pub icmp_code: IcmpCode,
pub checksum: u16be,
// theoretically, the header is 64 bytes long, but since the "Rest Of Header" part depends on
// the ICMP type and ICMP code, we consider it's part of the payload.
// rest_of_header: u32be,
#[payload]
pub payload: Vec<u8>,
}
/// Calculates a checksum of an ICMP packet.
pub fn checksum(packet: &IcmpPacket) -> u16be {
use crate::Packet;
use crate::util;
util::checksum(packet.packet(), 1)
}
#[cfg(test)]
mod checksum_tests {
use alloc::vec;
use super::*;
#[test]
fn checksum_zeros() {
let mut data = vec![0u8; 8];
let expected = 65535;
let mut pkg = MutableIcmpPacket::new(&mut data[..]).unwrap();
assert_eq!(checksum(&pkg.to_immutable()), expected);
pkg.set_checksum(123);
assert_eq!(checksum(&pkg.to_immutable()), expected);
}
#[test]
fn checksum_nonzero() {
let mut data = vec![255u8; 8];
let expected = 0;
let mut pkg = MutableIcmpPacket::new(&mut data[..]).unwrap();
assert_eq!(checksum(&pkg.to_immutable()), expected);
pkg.set_checksum(0);
assert_eq!(checksum(&pkg.to_immutable()), expected);
}
#[test]
fn checksum_odd_bytes() {
let mut data = vec![191u8; 7];
let expected = 49535;
let pkg = IcmpPacket::new(&mut data[..]).unwrap();
assert_eq!(checksum(&pkg), expected);
}
}
/// The enumeration of the recognized ICMP types.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod IcmpTypes {
use crate::icmp::IcmpType;
/// ICMP type for "echo reply" packet.
pub const EchoReply: IcmpType = IcmpType(0);
/// ICMP type for "destination unreachable" packet.
pub const DestinationUnreachable: IcmpType = IcmpType(3);
/// ICMP type for "source quench" packet.
pub const SourceQuench: IcmpType = IcmpType(4);
/// ICMP type for "redirect message" packet.
pub const RedirectMessage: IcmpType = IcmpType(5);
/// ICMP type for "echo request" packet.
pub const EchoRequest: IcmpType = IcmpType(8);
/// ICMP type for "router advertisement" packet.
pub const RouterAdvertisement: IcmpType = IcmpType(9);
/// ICMP type for "router solicitation" packet.
pub const RouterSolicitation: IcmpType = IcmpType(10);
/// ICMP type for "time exceeded" packet.
pub const TimeExceeded: IcmpType = IcmpType(11);
/// ICMP type for "parameter problem" packet.
pub const ParameterProblem: IcmpType = IcmpType(12);
/// ICMP type for "timestamp" packet.
pub const Timestamp: IcmpType = IcmpType(13);
/// ICMP type for "timestamp reply" packet.
pub const TimestampReply: IcmpType = IcmpType(14);
/// ICMP type for "information request" packet.
pub const InformationRequest: IcmpType = IcmpType(15);
/// ICMP type for "information reply" packet.
pub const InformationReply: IcmpType = IcmpType(16);
/// ICMP type for "address mask request" packet.
pub const AddressMaskRequest: IcmpType = IcmpType(17);
/// ICMP type for "address mask reply" packet.
pub const AddressMaskReply: IcmpType = IcmpType(18);
/// ICMP type for "traceroute" packet.
pub const Traceroute: IcmpType = IcmpType(30);
}
pub mod echo_reply {
//! abstraction for ICMP "echo reply" packets.
//!
//! ```text
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Type | Code | Checksum |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Identifier | Sequence Number |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Data ...
//! +-+-+-+-+-
//! ```
use crate::PrimitiveValues;
use crate::icmp::{IcmpCode, IcmpType};
use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::types::*;
/// Represent the "identifier" field of the ICMP echo replay header.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Identifier(pub u16);
impl Identifier {
/// Create a new `Identifier` instance.
pub fn new(val: u16) -> Identifier {
Identifier(val)
}
}
impl PrimitiveValues for Identifier {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
/// Represent the "sequence number" field of the ICMP echo replay header.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SequenceNumber(pub u16);
impl SequenceNumber {
/// Create a new `SequenceNumber` instance.
pub fn new(val: u16) -> SequenceNumber {
SequenceNumber(val)
}
}
impl PrimitiveValues for SequenceNumber {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
/// Enumeration of available ICMP codes for ICMP echo replay packets. There is actually only
/// one, since the only valid ICMP code is 0.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod IcmpCodes {
use crate::icmp::IcmpCode;
/// 0 is the only available ICMP code for "echo reply" ICMP packets.
pub const NoCode: IcmpCode = IcmpCode(0);
}
/// Represents an ICMP echo reply packet.
#[packet]
pub struct EchoReply {
#[construct_with(u8)]
pub icmp_type: IcmpType,
#[construct_with(u8)]
pub icmp_code: IcmpCode,
pub checksum: u16be,
pub identifier: u16be,
pub sequence_number: u16be,
#[payload]
pub payload: Vec<u8>,
}
}
pub mod echo_request {
//! abstraction for "echo request" ICMP packets.
//!
//! ```text
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Type | Code | Checksum |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Identifier | Sequence Number |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Data ...
//! +-+-+-+-+-
//! ```
use crate::PrimitiveValues;
use crate::icmp::{IcmpCode, IcmpType};
use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::types::*;
/// Represents the identifier field.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Identifier(pub u16);
impl Identifier {
/// Create a new `Identifier` instance.
pub fn new(val: u16) -> Identifier {
Identifier(val)
}
}
impl PrimitiveValues for Identifier {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
/// Represents the sequence number field.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SequenceNumber(pub u16);
impl SequenceNumber {
/// Create a new `SequenceNumber` instance.
pub fn new(val: u16) -> SequenceNumber {
SequenceNumber(val)
}
}
impl PrimitiveValues for SequenceNumber {
type T = (u16,);
fn to_primitive_values(&self) -> (u16,) {
(self.0,)
}
}
/// Enumeration of available ICMP codes for "echo reply" ICMP packets. There is actually only
/// one, since the only valid ICMP code is 0.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod IcmpCodes {
use crate::icmp::IcmpCode;
/// 0 is the only available ICMP code for "echo reply" ICMP packets.
pub const NoCode: IcmpCode = IcmpCode(0);
}
/// Represents an "echo request" ICMP packet.
#[packet]
pub struct EchoRequest {
#[construct_with(u8)]
pub icmp_type: IcmpType,
#[construct_with(u8)]
pub icmp_code: IcmpCode,
pub checksum: u16be,
pub identifier: u16be,
pub sequence_number: u16be,
#[payload]
pub payload: Vec<u8>,
}
}
pub mod destination_unreachable {
//! abstraction for "destination unreachable" ICMP packets.
//!
//! ```text
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Type | Code | Checksum |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | unused |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Internet Header + 64 bits of Original Data Datagram |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! ```
use crate::icmp::{IcmpCode, IcmpType};
use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::types::*;
/// Enumeration of the recognized ICMP codes for "destination unreachable" ICMP packets.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod IcmpCodes {
use crate::icmp::IcmpCode;
/// ICMP code for "destination network unreachable" packet.
pub const DestinationNetworkUnreachable: IcmpCode = IcmpCode(0);
/// ICMP code for "destination host unreachable" packet.
pub const DestinationHostUnreachable: IcmpCode = IcmpCode(1);
/// ICMP code for "destination protocol unreachable" packet.
pub const DestinationProtocolUnreachable: IcmpCode = IcmpCode(2);
/// ICMP code for "destination port unreachable" packet.
pub const DestinationPortUnreachable: IcmpCode = IcmpCode(3);
/// ICMP code for "fragmentation required and DFF flag set" packet.
pub const FragmentationRequiredAndDFFlagSet: IcmpCode = IcmpCode(4);
/// ICMP code for "source route failed" packet.
pub const SourceRouteFailed: IcmpCode = IcmpCode(5);
/// ICMP code for "destination network unknown" packet.
pub const DestinationNetworkUnknown: IcmpCode = IcmpCode(6);
/// ICMP code for "destination host unknown" packet.
pub const DestinationHostUnknown: IcmpCode = IcmpCode(7);
/// ICMP code for "source host isolated" packet.
pub const SourceHostIsolated: IcmpCode = IcmpCode(8);
/// ICMP code for "network administrative prohibited" packet.
pub const NetworkAdministrativelyProhibited: IcmpCode = IcmpCode(9);
/// ICMP code for "host administrative prohibited" packet.
pub const HostAdministrativelyProhibited: IcmpCode = IcmpCode(10);
/// ICMP code for "network unreachable for this Type Of Service" packet.
pub const NetworkUnreachableForTOS: IcmpCode = IcmpCode(11);
/// ICMP code for "host unreachable for this Type Of Service" packet.
pub const HostUnreachableForTOS: IcmpCode = IcmpCode(12);
/// ICMP code for "communication administratively prohibited" packet.
pub const CommunicationAdministrativelyProhibited: IcmpCode = IcmpCode(13);
/// ICMP code for "host precedence violation" packet.
pub const HostPrecedenceViolation: IcmpCode = IcmpCode(14);
/// ICMP code for "precedence cut off in effect" packet.
pub const PrecedenceCutoffInEffect: IcmpCode = IcmpCode(15);
}
/// Represents an "echo request" ICMP packet.
#[packet]
pub struct DestinationUnreachable {
#[construct_with(u8)]
pub icmp_type: IcmpType,
#[construct_with(u8)]
pub icmp_code: IcmpCode,
pub checksum: u16be,
pub unused: u32be,
#[payload]
pub payload: Vec<u8>,
}
}
pub mod time_exceeded {
//! abstraction for "time exceeded" ICMP packets.
//!
//! ```text
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Type | Code | Checksum |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | unused |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! | Internet Header + 64 bits of Original Data Datagram |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! ```
use crate::icmp::{IcmpCode, IcmpType};
use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::types::*;
/// Enumeration of the recognized ICMP codes for "time exceeded" ICMP packets.
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub mod IcmpCodes {
use crate::icmp::IcmpCode;
/// ICMP code for "time to live exceeded in transit" packet.
pub const TimeToLiveExceededInTransit: IcmpCode = IcmpCode(0);
/// ICMP code for "fragment reassembly time exceeded" packet.
pub const FragmentReasemblyTimeExceeded: IcmpCode = IcmpCode(1);
}
/// Represents an "echo request" ICMP packet.
#[packet]
pub struct TimeExceeded {
#[construct_with(u8)]
pub icmp_type: IcmpType,
#[construct_with(u8)]
pub icmp_code: IcmpCode,
pub checksum: u16be,
pub unused: u32be,
#[payload]
pub payload: Vec<u8>,
}
}