raw sockets and icmp raw sockets and icmp
play

Raw Sockets and ICMP Raw Sockets and ICMP Code Examples Ping - PDF document

Topics Topics Raw sockets Internet Control Message Protocol (ICMP) Raw Sockets and ICMP Raw Sockets and ICMP Code Examples Ping Traceroute Srinidhi Varadarajan 11/4/2002 2 Raw Sockets Raw Sockets Creating a Raw Socket


  1. Topics Topics � Raw sockets � Internet Control Message Protocol (ICMP) Raw Sockets and ICMP Raw Sockets and ICMP � Code Examples – Ping – Traceroute Srinidhi Varadarajan 11/4/2002 2 Raw Sockets Raw Sockets Creating a Raw Socket Creating a Raw Socket � Usually, sockets are used to build � Standard socket() call used to create a raw applications on top of a transport protocol socket – Stream sockets (TCP) – Family is AF_INET, as for TCP or UDP – Datagram sockets (UDP) – Socket type is SOCK_RAW instead of SOCK_STREAM or SOCK_DGRAM � Some applications need to access a lower – Socket protocol needs to be specified, e.g. layer protocol IPPROTO_ICMP (often left at 0 for UDP or TCP – Control protocols built on IP rather than UDP sockets) or TCP, such as ICMP and IGMP – Experimental transport protocols � A “raw” socket allows direct access to IP socket(AF_INET, SOCK_RAW, IPPROTO_ICMP) – Used to build applications on top of the network layer 11/4/2002 3 11/4/2002 4 Socket Types Socket Types Protocols Protocols � Protocol values – Used to define the Protocol field in the IP header Stream socket SOCK_STREAM 1 Datagram socket SOCK_DGRAM 2 IP (dummy) IPPROTO_IP 0 IP (dummy) IPPROTO_IP 0 Raw protocol interface SOCK_RAW 3 ICMP IPPROTO_ICMP 1 ICMP IPPROTO_ICMP 1 Reliably delivered message SOCK_RDM 4 IGMP IPPROTO_IGMP 2 IGMP IPPROTO_IGMP 2 Sequenced packet stream SOCK_SEQPACKET 5 Gateway IPPROTO_GGP 3 Gateway IPPROTO_GGP 3 TCP IPPROTO_TCP 6 TCP IPPROTO_TCP 6 PUP IPPROTO_PUP 12 PUP IPPROTO_PUP 12 UDP IPPROTO_UDP 17 UDP IPPROTO_UDP 17 XND IDP IPPROTO_IDP 22 XND IDP IPPROTO_IDP 22 Net Disk IPPROTO_ND 77 Net Disk IPPROTO_ND 77 Raw IP IPPROTO_RAW 255 Raw IP IPPROTO_RAW 255 11/4/2002 5 11/4/2002 6 Application Layer 1

  2. Internet Control Message Protocol Internet Control Message Protocol ICMP in the TCP/IP Suite ICMP in the TCP/IP Suite � ICMP defined in RFC 792 App App App Application � ICMP messages API – Query network node(s) for information – Report error conditions Transport TCP UDP � ICMP messages are carried as IP datagrams ICMP IP IGMP Network – ICMP “uses” or is “above” IP � ICMP messages usually processed by IP, UDP, or TCP Hardware – IP, TCP, and UDP “use” or are above ICMP ARP RARP Data Link Interface 11/4/2002 7 11/4/2002 8 ICMP Message Format (1) ICMP Message Format (1) ICMP Message Format (2) ICMP Message Format (2) � ICMP messages are encapsulated in IP 0 4 8 16 24 31 datagrams TYPE CODE CHECKSUM – IP-level routing use to move ICMP messages through a network Contents – IP provides multiplexing/demultiplexing based on protocol number (IPPROTO_ICMP = 1) � TYPE: Type of ICMP message IP datagram � CODE: Used by some types to indicate a specific condition IP Header ICMP Message � CHECKSUM: Checksum over full message � Contents depend on TYPE and CODE 20 bytes variable length 11/4/2002 9 11/4/2002 10 Example ICMP Message Types Example ICMP Message Types Error Example: Port Unreachable Error Example: Port Unreachable � Queries � Port unreachable error occurs when a receiving host receives a packet with an – TYPE = 8: Echo request unknown (inactive) port number – TYPE = 0: Echo reply � IP datagram is valid -- reaches addressed – TYPE = 13: Time stamp request host – TYPE = 14: Time stamp reply � Errors � UDP datagram contains a port that is not in use (e.g. 8000 and no application has a – TYPE = 3: Destination unreachable socket bound to an address with that port) • CODE = 0: Network unreachable • CODE = 1: Host unreachable � UDP replies with an ICMP “Destination • CODE = 2: Protocol unreachable Unreachable/Port Unreachable” message • CODE = 3: Port unreachable – TYPE = 3, CODE =3 – TYPE = 11: Time exceeded • CODE = 0: Time-to-live equals 0 in transit 11/4/2002 11 11/4/2002 12 Application Layer 2

  3. ICMP Error Messages ICMP Error Messages Ping Example Ping Example � ICMP error messages include header and � “Ping” utility first 8 bytes of offending IP datagram – Tests whether or not a host is reachable – All of IP header – Provides a round-trip time • Destination address, protocol number, etc. – Written by Mike Muuss in 1983 to diagnose – For UDP, all of UDP header including source network problems and destination port numbers � Operation � ICMP message for port unreachable – ICMP echo request (TYPE = 8) sent to host – Host replies with ICMP echo reply (TYPE = 0) ICMP message � Client-server roles Offending Offending IP ICMP – Host sending echo request is the client IP UDP Header Header – Host sending echo reply is the server Header Header – Server usually implemented in TCP/IP code 20 8 20 8 11/4/2002 13 11/4/2002 14 Ping Algorithm Ping Algorithm Echo Request/Reply Format (1) Echo Request/Reply Format (1) 0 4 8 16 24 31 1) Initialize echo request TYPE (0, 8) CODE (0) CHECKSUM 2) Send echo request IDENTIFIER SEQUENCE NUMBER 3) Wait for echo reply (or time out) 8: Request Optional Data 0: Reply 4) Receive reply (time value) 5) Report results � IDENTIFIER: Means to identify sending 6) Go back to 1 until complete instance of “ping” – Process id in UNIX � SEQUENCE NUMBER: Means to identify lost or misordered replies 11/4/2002 15 11/4/2002 16 Echo Request/Reply Format (2) Echo Request/Reply Format (2) Echo Request Echo Request � Echo request will include � Common ICMP echo reply/request header definition from icmp.h code example – Common request/reply header – Time stamp (32 bits) typedef struct tagICMPHDR – Filler data (REQ_DATASIZE bytes) { u_char Type; // Type typedef struct tagECHOREQUEST u_char Code; // Code { u_short Checksum; // Checksum ICMPHDR icmpHdr; // Header u_short ID; // Identification int dwTime; // Time u_short Seq; // Sequence char cData[REQ_DATASIZE]; // Fill data } ICMPHDR, *PICMPHDR; } ECHOREQUEST, *PECHOREQUEST; static ECHOREQUEST echo_req; 11/4/2002 17 11/4/2002 18 Application Layer 3

  4. Initializing the Echo Request Initializing the Echo Request Waiting for Echo Reply Waiting for Echo Reply echo_req.icmpHdr.Type = ICMP_ECHOREQ; � Time-out is important since ping will often be echo_req.icmpHdr.Code = 0; used when a host is unreachable echo_req.icmpHdr.Checksum = 0; � select() used with a time-out value to wait for echo_req.icmpHdr.ID = id++; echo reply echo_req.icmpHdr.Seq = seq++; // Fill in some data to send readfds.fd_count = 1; // set size memset(echo_req.cData, ' ', REQ_DATASIZE); readfds.fd_array[0] = raw; // socket set timeout.tv_sec = 10; // timeout (s) // Save tick count when sent (milliseconds) timeout.tv_usec = 0; // timeout (us) echo_req.dwTime = gettime …; // Put data in packet and compute checksum if((rc = select(1, &readfds, NULL, NULL, echo_req.icmpHdr.Checksum = in_cksum(…); &timeout)) == SOCKET_ERROR) errexit("select() failed %d\n", perror()); 11/4/2002 19 11/4/2002 20 Echo Reply Echo Reply IP Header (1) IP Header (1) � Raw socket returns IP header 0 4 8 16 24 31 � Received datagramcontains Vers HLen Service Type Total Length – IP header Identification Flags Fragment Offset – ICMP echo request/reply header Time To Live Protocol Header Checksum – Echo request message Source IP Address – Potentially, additional fill data typedef struct tagECHOREPLY Destination IP Address { IPHDR ipHdr; ECHOREQUEST echoRequest; char cFiller[256]; } ECHOREPLY, *PECHOREPLY; 11/4/2002 21 11/4/2002 22 IP Header (2) IP Header (2) Extracting Results from Reply Extracting Results from Reply typedef struct tagIPHDR � Ping client can extract IP, ICMP, and { echo information from the received u_char VIHL; // Ver, Hdr length datagram u_char TOS; // Type of service … short TotLen; // Total length ECHOREPLY echo_reply; short ID; // Identification … short FlagOff; // Flags, Frag off type = echo_reply.echoRequest.icmpHdr.Type; u_char TTL; // Time-to-live ttl = echo_reply.ipHdr.TTL; u_char Protocol; // Protocol … u_short Checksum; // Checksum struct in_addr iaSrc; // Source IP addr struct in_addr iaDst; // Dest IP addr } IPHDR, *PIPHDR; 11/4/2002 23 11/4/2002 24 Application Layer 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend