SlideShare a Scribd company logo
cs423 - cotter 1
What are Raw Sockets?
1.A way to pass information to network
protocols other than TCP or UDP (e.g.
ICMP and IGMP)
2.A way to implement new IPv4 protocols
3.A way to build our own packets (be careful
here)
cs423 - cotter 2
Why Would We Use Them?
• Allows us to access packets sent over protocols
other than TCP / UDP
• Allows us to process IPv4 protocols in user
space
– Control, speed, troubleshooting
• Allow us to implement new IPv4 protocols
• Allows us to control the IP header
– Control option fields (beyond setsockopt() )
– Test / control packet fragmentation
cs423 - cotter 3
Limitations?
• Reliability Loss
• No Ports
• Nonstandard communication
• No Automatic ICMP
• Raw TCP / UDP unlikely
• Requires root / admin
cs423 - cotter 4
OS Involvement in Sockets
User Space Kernel Space
Socket App TCP/IP StackLinux
Socket ( AF_INET,
SOCK_STREAM,
IPPROTO_TCP)
Socket ( AF_INET,
SOCK_RAW,
IPPROTO_ICMP)
Socket ( AF_PACKET,
SOCK_RAW,
htons(ETH_P_IP))
Identify
Socket Type
Identify
Socket Type
Identify
Socket Type
TCP
IP
Ethernet
cs423 - cotter 5
Normal Socket Operation (TCP)
• Create a socket
– s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)
• Bind to a port (optional)
– Identify local IP and port desired and create data structure
– bind (s, (struct sockaddr *) &sin, sizeof(sin))
• Establish a connection to server
– Identify server IP and port
– connect (s, (struct sockaddr *) &sin, sizeof(sin))
• Send / Receive data
– Place data to be send into buffer
– recv (s, buf, strlen(buf), 0);
cs423 - cotter 6
Normal Socket Operation (TCP)
User Space Kernel Space
Socket App ProtocolLinux
socket ( ) Create socket
TCP, IP, Internet
connect( )
Bind to local port:
Connect to remote port
send( ) TCP, IP, InternetPass data thru local
stack to remote port
OK
OK
OK
TCP
cs423 - cotter 7
Raw Sockets Operation (ICMP)
• Create a socket
– s = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP)
• Since there is no port, there is no bind *
• There is no TCP, so no connection *
• Send / Receive data
– Place data to be sent into buffer
– sendto (s, buf, strlen(buf), 0, addr, &len);
* More later
cs423 - cotter 8
Raw Sockets Operation (ICMP)
User Space Kernel Space
Socket App ProtocolLinux
socket ( ) Create socket
sendto( ) IP, InternetPass data thru local
stack to remote host
OK
OK
ICMP
cs423 - cotter 9
Create a Raw Socket
• s = socket (AF_INET, SOCK_RAW, protocol)
– IPPROTO_ICMP, IPPROTO_IP, etc.
• Can create our own IP header if we wish
– const int on = 1;
– setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on));
• Can “bind”
– Since we have no port, the only effect is to associate a local IP
address with the raw socket. (useful if there are multiple local
IP addrs and we want to use only 1).
• Can “connect”
– Again, since we have no TCP, we have no connection. The
only effect is to associate a remote IP address with this socket.
cs423 - cotter 10
Raw Socket Output
• Normal output performed using sendto or sendmsg.
– Write or send can be used if the socket has been connected
• If IP_HDRINCL not set, starting addr of the data (buf)
specifies the first byte following the IP header that the kernel
will build.
– Size only includes the data above the IP header.
• If IP_HDRINCL is set, the starting addr of the data identifies
the first byte of the IP header.
– Size includes the IP header
– Set IP id field to 0 (tells kernel to set this field)
– Kernel will calculate IP checksum
• Kernel can fragment raw packets exceeding outgoing MTU
cs423 - cotter 11
Raw Socket Input
• Received TCP / UDP NEVER passed to a raw socket.
• Most ICMP packets are passed to a raw socket
– (Some exceptions for Berkeley-derived implementations)
• All IGMP packets are passed to a raw socket
• All IP datagrams with a protocol field that the kernel does
not understand (process) are passed to a raw socket.
• If packet has been fragmented, packet is reassembled
before being passed to raw socket
cs423 - cotter 12
Conditions that include / exclude
passing to specific raw sockets
• If a nonzero protocol is specified when raw
socket is created, datagram protocol must
match
• If raw socket is bound to a specific local
IP, then destination IP must match
• If raw socket is “connected” to a foreign IP
address, then the source IP address must
match
cs423 - cotter 13
Ping – Overview
• This example modified from code by Walton (Ch 18)
• Very simple program that uses ICMP to send a ping to
another machine over the Internet.
• Provides the option to send a defined number of packets
(or will send a default 25).
• We will build an ICMP packet (with a proper header,
including checksum) that will be updated each time we
send a new packet.
• We will display the raw packet that is received back from
our destination host and will interpret some of the data.
– (Output format is different from standard ping)
cs423 - cotter 14
ICMP Packet header
struct icmphdr {
u_int8_t type // ICMP message type (0)
u_int8_t code // ICMP type sub-code (0)
u_int16_t checksum E306, etc.
u_int16_t id // echo datagram id (use pid)
u_int16_t sequence // echo seq # 1, 2, 3, etc.
};
Packet body:
0 1 2 3 4 5 6 7 8 9 : ; < = > ? … B
cs423 - cotter 15
myNuPing.c (overview)
• Global Declarations
– Struct packet { }, some variables
• unsigned short checksum (void *b, int len)
– Calculate checksum for ICMP packet (header and data)
• void display (void *buf, int bytes)
– Format a received packet for display.
• void listener (void)
– Separate process to capture responses to pings
• void ping (struct sockaddr_in *addr)
– Create socket and send out pings 1/sec to specified IP addr
• int main (int count, shar *strings[ ])
– Test for valid instantiation, create addr structure
– Fork a separate process (listener) and use existing process for ping
cs423 - cotter 16
#defines and checksum calc
#define PACKETSIZE 64
struct packet {
struct icmphdr hdr;
char msg[PACKETSIZE-sizeof(struct icmphdr)];
};
int pid=-1;
int loops = 25;
struct protoent *proto=NULL;
unsigned short checksum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;
for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
cs423 - cotter 17
display - present echo info
void display(void *buf, int bytes) {
int i;
struct iphdr *ip = buf;
struct icmphdr *icmp = buf+ip->ihl*4;
printf("----------------n");
for ( i = 0; i < bytes; i++ ) {
if ( !(i & 15) ) printf("n%04X: ", i);
printf("%02X ", ((unsigned char*)buf)[i]);
}
printf("n");
printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s
",
ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
ip->ttl, inet_ntoa(ip->saddr));
printf("dst=%sn", inet_ntoa(ip->daddr));
if ( icmp->un.echo.id == pid ) {
printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]n",
icmp->type, icmp->code, ntohs(icmp->checksum),
icmp->un.echo.id, icmp->un.echo.sequence);
}
}
cs423 - cotter 18
Listener - separate process to
listen for and collect messages-
void listener(void) {
int sd, i;
struct sockaddr_in addr;
unsigned char buf[1024];
sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 ) {
perror("socket");
exit(0);
}
for (i = 0; i < loops; i++) {
int bytes, len=sizeof(addr);
bzero(buf, sizeof(buf));
bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *) &addr,
&len);
if ( bytes > 0 )
display(buf, bytes);
else
perror("recvfrom");
}
exit(0);
}
cs423 - cotter 19
ping - Create message and send it
void ping(struct sockaddr_in *addr)
{
const int val=255;
int i, j, sd, cnt=1;
struct packet pckt;
struct sockaddr_in r_addr;
sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 )
{
perror("socket");
return;
}
if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
perror("Set TTL option");
if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
perror("Request nonblocking I/O");
cs423 - cotter 20
ping (cont)
for (j = 0; j < loops; j++) { // send pings 1 per second
int len=sizeof(r_addr);
printf("Msg #%dn", cnt);
if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *)&r_addr, &len) > 0 )
printf("***Got message!***n");
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg[i] = i+'0';
pckt.msg[i] = 0;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *) addr, sizeof(*addr)) <= 0)
perror("sendto");
sleep(1);
}
}
cs423 - cotter 21
myNuPing.c – main()int main(int count, char *argv[]) {
struct hostent *hname;
struct sockaddr_in addr;
loops = 0;
if ( count != 3 ) {
printf("usage: %s <addr> <loops> n", argv[0]);
exit(0);
}
if (count == 3) // WE HAVE SPECIFIED A MESSAGE COUNT
loops = atoi(argv[2]);
if ( count > 1 ) {
pid = getpid();
proto = getprotobyname("ICMP");
hname = gethostbyname(argv[1]);
bzero(&addr, sizeof(addr));
addr.sin_family = hname->h_addrtype;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(long*)hname->h_addr;
if ( fork() == 0 )
listener();
else
ping(&addr);
wait(0);
}
else
printf("usage: myping <hostname>n");
return 0;
}
cs423 - cotter 22
“Ping” Output
[root]# ./myNuPing 134.193.12.34 2
Msg #1
----------------
0000: 45 00 00 54 CC 38 40 00 80 01 1F BE 86 12 34 56
0010: 86 12 34 57 00 00 E4 06 DF 07 01 00 30 31 32 33
0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43
0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53
0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
0050: 64 65 66 00
IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34
ICMP: type[0/0] checksum[58374] id[2015] seq[1]
Msg #2
***Got message!***
----------------
0000: 45 00 00 54 CC 39 40 00 80 01 1F BD 86 12 34 56
0010: 86 12 34 57 00 00 E3 06 DF 07 02 00 30 31 32 33
0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43
0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53
0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
0050: 64 65 66 00
IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34
ICMP: type[0/0] checksum[58118] id[2015] seq[2]
[root]#
cs423 - cotter 23
Summary
• Raw Sockets allow access to Protocols other
than the standard TCP and UDP
• Performance and capabilities may be OS
dependent.
– Some OSs block the ability to send packets that
originate from raw sockets (although reception may
be permitted).
• Raw sockets remove the burden of the complex
TCP/IP protocol stack, but they also remove the
safeguards and support that those protocols
provide

More Related Content

What's hot

Splay Tree
Splay TreeSplay Tree
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Mobile Transport layer
Mobile Transport layerMobile Transport layer
Mobile Transport layer
Pallepati Vasavi
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
Sampath Kumar S
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.
Malek Sumaiya
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
DataminingTools Inc
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Ratnakar Mikkili
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)Jagjit Wilku
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
Abhishek Singh
 
TCP timers.ppt
TCP timers.pptTCP timers.ppt
TCP timers.ppt
Jayaprasanna4
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
Mohammad Imam Hossain
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
Sohail Ahmed
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
Dattatray Gandhmal
 
Congestion control in tcp
Congestion control in tcpCongestion control in tcp
Congestion control in tcp
samarai_apoc
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,8neutron8
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
Amit Kumar Rathi
 

What's hot (20)

Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Mobile Transport layer
Mobile Transport layerMobile Transport layer
Mobile Transport layer
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
 
List in Python
List in PythonList in Python
List in Python
 
String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
 
TCP timers.ppt
TCP timers.pptTCP timers.ppt
TCP timers.ppt
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Congestion control in tcp
Congestion control in tcpCongestion control in tcp
Congestion control in tcp
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
 

Similar to Cs423 raw sockets_bw

sockets
socketssockets
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
support58
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
ssuserc4a497
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
Md. Golam Hossain
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)
Flor Ian
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
Avinash Varma Kalidindi
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
艾鍗科技
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
ScyllaDB
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecturehugo lu
 
TCP IP
TCP IPTCP IP
TCP IPhivasu
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
Kiran Divekar
 
Sockets intro
Sockets introSockets intro
Sockets intro
AviNash ChaVhan
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
Andriy Berestovskyy
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
Aashiq Ahamed N
 

Similar to Cs423 raw sockets_bw (20)

Sockets
SocketsSockets
Sockets
 
sockets
socketssockets
sockets
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Udp socket programming(Florian)
Udp socket programming(Florian)Udp socket programming(Florian)
Udp socket programming(Florian)
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Introduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.pptIntroduction to sockets tcp ip protocol.ppt
Introduction to sockets tcp ip protocol.ppt
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
123
123123
123
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
TCP IP
TCP IPTCP IP
TCP IP
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Packet filtering using jpcap
Packet filtering using jpcapPacket filtering using jpcap
Packet filtering using jpcap
 
Gas leakage detection system
Gas leakage detection systemGas leakage detection system
Gas leakage detection system
 

Recently uploaded

What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
Autohaus Service and Sales
 
Things to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your carThings to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your car
jennifermiller8137
 
Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?
jennifermiller8137
 
Core technology of Hyundai Motor Group's EV platform 'E-GMP'
Core technology of Hyundai Motor Group's EV platform 'E-GMP'Core technology of Hyundai Motor Group's EV platform 'E-GMP'
Core technology of Hyundai Motor Group's EV platform 'E-GMP'
Hyundai Motor Group
 
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
mymwpc
 
Antique Plastic Traders Company Profile
Antique Plastic Traders Company ProfileAntique Plastic Traders Company Profile
Antique Plastic Traders Company Profile
Antique Plastic Traders
 
What do the symbols on vehicle dashboard mean?
What do the symbols on vehicle dashboard mean?What do the symbols on vehicle dashboard mean?
What do the symbols on vehicle dashboard mean?
Hyundai Motor Group
 
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to TellWondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Vic Auto Collision & Repair
 
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptxStatistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
coc7987515756
 
What Exactly Is The Common Rail Direct Injection System & How Does It Work
What Exactly Is The Common Rail Direct Injection System & How Does It WorkWhat Exactly Is The Common Rail Direct Injection System & How Does It Work
What Exactly Is The Common Rail Direct Injection System & How Does It Work
Motor Cars International
 
gtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
gtycccccccccccccccccccccccccccccccccccccccccccccccccccccccgtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
gtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
4thzenzstar
 
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out HereWhy Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Masters European & Gapanese Auto Repair
 
Why Is Your BMW X3 Hood Not Responding To Release Commands
Why Is Your BMW X3 Hood Not Responding To Release CommandsWhy Is Your BMW X3 Hood Not Responding To Release Commands
Why Is Your BMW X3 Hood Not Responding To Release Commands
Dart Auto
 
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs  Consulting SMEs.pptxEmpowering Limpopo Entrepreneurs  Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Precious Mvulane CA (SA),RA
 
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
eygkup
 
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
ahmedendrise81
 
Ec460b lc Excavator Volvo Service Repair.pdf
Ec460b lc Excavator Volvo Service Repair.pdfEc460b lc Excavator Volvo Service Repair.pdf
Ec460b lc Excavator Volvo Service Repair.pdf
Excavator
 
Tyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEATTyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEAT
kshamashah95
 
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
European Service Center
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
daothibichhang1
 

Recently uploaded (20)

What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
What Does the PARKTRONIC Inoperative, See Owner's Manual Message Mean for You...
 
Things to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your carThings to remember while upgrading the brakes of your car
Things to remember while upgrading the brakes of your car
 
Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?Digital Fleet Management - Why Your Business Need It?
Digital Fleet Management - Why Your Business Need It?
 
Core technology of Hyundai Motor Group's EV platform 'E-GMP'
Core technology of Hyundai Motor Group's EV platform 'E-GMP'Core technology of Hyundai Motor Group's EV platform 'E-GMP'
Core technology of Hyundai Motor Group's EV platform 'E-GMP'
 
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
一比一原版(AUT毕业证)奥克兰理工大学毕业证成绩单如何办理
 
Antique Plastic Traders Company Profile
Antique Plastic Traders Company ProfileAntique Plastic Traders Company Profile
Antique Plastic Traders Company Profile
 
What do the symbols on vehicle dashboard mean?
What do the symbols on vehicle dashboard mean?What do the symbols on vehicle dashboard mean?
What do the symbols on vehicle dashboard mean?
 
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to TellWondering if Your Mercedes EIS is at Fault Here’s How to Tell
Wondering if Your Mercedes EIS is at Fault Here’s How to Tell
 
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptxStatistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
Statistics5,c.xz,c.;c.;d.c;d;ssssss.pptx
 
What Exactly Is The Common Rail Direct Injection System & How Does It Work
What Exactly Is The Common Rail Direct Injection System & How Does It WorkWhat Exactly Is The Common Rail Direct Injection System & How Does It Work
What Exactly Is The Common Rail Direct Injection System & How Does It Work
 
gtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
gtycccccccccccccccccccccccccccccccccccccccccccccccccccccccgtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
gtyccccccccccccccccccccccccccccccccccccccccccccccccccccccc
 
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out HereWhy Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
Why Isn't Your BMW X5's Comfort Access Functioning Properly Find Out Here
 
Why Is Your BMW X3 Hood Not Responding To Release Commands
Why Is Your BMW X3 Hood Not Responding To Release CommandsWhy Is Your BMW X3 Hood Not Responding To Release Commands
Why Is Your BMW X3 Hood Not Responding To Release Commands
 
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs  Consulting SMEs.pptxEmpowering Limpopo Entrepreneurs  Consulting SMEs.pptx
Empowering Limpopo Entrepreneurs Consulting SMEs.pptx
 
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
一比一原版(AIS毕业证)奥克兰商学院毕业证成绩单如何办理
 
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
欧洲杯比赛投注官网-欧洲杯比赛投注官网网站-欧洲杯比赛投注官网|【​网址​🎉ac123.net🎉​】
 
Ec460b lc Excavator Volvo Service Repair.pdf
Ec460b lc Excavator Volvo Service Repair.pdfEc460b lc Excavator Volvo Service Repair.pdf
Ec460b lc Excavator Volvo Service Repair.pdf
 
Tyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEATTyre Industrymarket overview with examples of CEAT
Tyre Industrymarket overview with examples of CEAT
 
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
What Causes 'Trans Failsafe Prog' to Trigger in BMW X5
 
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.docBài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
Bài tập - Tiếng anh 11 Global Success UNIT 1 - Bản HS.doc
 

Cs423 raw sockets_bw

  • 1. cs423 - cotter 1 What are Raw Sockets? 1.A way to pass information to network protocols other than TCP or UDP (e.g. ICMP and IGMP) 2.A way to implement new IPv4 protocols 3.A way to build our own packets (be careful here)
  • 2. cs423 - cotter 2 Why Would We Use Them? • Allows us to access packets sent over protocols other than TCP / UDP • Allows us to process IPv4 protocols in user space – Control, speed, troubleshooting • Allow us to implement new IPv4 protocols • Allows us to control the IP header – Control option fields (beyond setsockopt() ) – Test / control packet fragmentation
  • 3. cs423 - cotter 3 Limitations? • Reliability Loss • No Ports • Nonstandard communication • No Automatic ICMP • Raw TCP / UDP unlikely • Requires root / admin
  • 4. cs423 - cotter 4 OS Involvement in Sockets User Space Kernel Space Socket App TCP/IP StackLinux Socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP) Socket ( AF_INET, SOCK_RAW, IPPROTO_ICMP) Socket ( AF_PACKET, SOCK_RAW, htons(ETH_P_IP)) Identify Socket Type Identify Socket Type Identify Socket Type TCP IP Ethernet
  • 5. cs423 - cotter 5 Normal Socket Operation (TCP) • Create a socket – s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP) • Bind to a port (optional) – Identify local IP and port desired and create data structure – bind (s, (struct sockaddr *) &sin, sizeof(sin)) • Establish a connection to server – Identify server IP and port – connect (s, (struct sockaddr *) &sin, sizeof(sin)) • Send / Receive data – Place data to be send into buffer – recv (s, buf, strlen(buf), 0);
  • 6. cs423 - cotter 6 Normal Socket Operation (TCP) User Space Kernel Space Socket App ProtocolLinux socket ( ) Create socket TCP, IP, Internet connect( ) Bind to local port: Connect to remote port send( ) TCP, IP, InternetPass data thru local stack to remote port OK OK OK TCP
  • 7. cs423 - cotter 7 Raw Sockets Operation (ICMP) • Create a socket – s = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP) • Since there is no port, there is no bind * • There is no TCP, so no connection * • Send / Receive data – Place data to be sent into buffer – sendto (s, buf, strlen(buf), 0, addr, &len); * More later
  • 8. cs423 - cotter 8 Raw Sockets Operation (ICMP) User Space Kernel Space Socket App ProtocolLinux socket ( ) Create socket sendto( ) IP, InternetPass data thru local stack to remote host OK OK ICMP
  • 9. cs423 - cotter 9 Create a Raw Socket • s = socket (AF_INET, SOCK_RAW, protocol) – IPPROTO_ICMP, IPPROTO_IP, etc. • Can create our own IP header if we wish – const int on = 1; – setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)); • Can “bind” – Since we have no port, the only effect is to associate a local IP address with the raw socket. (useful if there are multiple local IP addrs and we want to use only 1). • Can “connect” – Again, since we have no TCP, we have no connection. The only effect is to associate a remote IP address with this socket.
  • 10. cs423 - cotter 10 Raw Socket Output • Normal output performed using sendto or sendmsg. – Write or send can be used if the socket has been connected • If IP_HDRINCL not set, starting addr of the data (buf) specifies the first byte following the IP header that the kernel will build. – Size only includes the data above the IP header. • If IP_HDRINCL is set, the starting addr of the data identifies the first byte of the IP header. – Size includes the IP header – Set IP id field to 0 (tells kernel to set this field) – Kernel will calculate IP checksum • Kernel can fragment raw packets exceeding outgoing MTU
  • 11. cs423 - cotter 11 Raw Socket Input • Received TCP / UDP NEVER passed to a raw socket. • Most ICMP packets are passed to a raw socket – (Some exceptions for Berkeley-derived implementations) • All IGMP packets are passed to a raw socket • All IP datagrams with a protocol field that the kernel does not understand (process) are passed to a raw socket. • If packet has been fragmented, packet is reassembled before being passed to raw socket
  • 12. cs423 - cotter 12 Conditions that include / exclude passing to specific raw sockets • If a nonzero protocol is specified when raw socket is created, datagram protocol must match • If raw socket is bound to a specific local IP, then destination IP must match • If raw socket is “connected” to a foreign IP address, then the source IP address must match
  • 13. cs423 - cotter 13 Ping – Overview • This example modified from code by Walton (Ch 18) • Very simple program that uses ICMP to send a ping to another machine over the Internet. • Provides the option to send a defined number of packets (or will send a default 25). • We will build an ICMP packet (with a proper header, including checksum) that will be updated each time we send a new packet. • We will display the raw packet that is received back from our destination host and will interpret some of the data. – (Output format is different from standard ping)
  • 14. cs423 - cotter 14 ICMP Packet header struct icmphdr { u_int8_t type // ICMP message type (0) u_int8_t code // ICMP type sub-code (0) u_int16_t checksum E306, etc. u_int16_t id // echo datagram id (use pid) u_int16_t sequence // echo seq # 1, 2, 3, etc. }; Packet body: 0 1 2 3 4 5 6 7 8 9 : ; < = > ? … B
  • 15. cs423 - cotter 15 myNuPing.c (overview) • Global Declarations – Struct packet { }, some variables • unsigned short checksum (void *b, int len) – Calculate checksum for ICMP packet (header and data) • void display (void *buf, int bytes) – Format a received packet for display. • void listener (void) – Separate process to capture responses to pings • void ping (struct sockaddr_in *addr) – Create socket and send out pings 1/sec to specified IP addr • int main (int count, shar *strings[ ]) – Test for valid instantiation, create addr structure – Fork a separate process (listener) and use existing process for ping
  • 16. cs423 - cotter 16 #defines and checksum calc #define PACKETSIZE 64 struct packet { struct icmphdr hdr; char msg[PACKETSIZE-sizeof(struct icmphdr)]; }; int pid=-1; int loops = 25; struct protoent *proto=NULL; unsigned short checksum(void *b, int len) { unsigned short *buf = b; unsigned int sum=0; unsigned short result; for ( sum = 0; len > 1; len -= 2 ) sum += *buf++; if ( len == 1 ) sum += *(unsigned char*)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result; }
  • 17. cs423 - cotter 17 display - present echo info void display(void *buf, int bytes) { int i; struct iphdr *ip = buf; struct icmphdr *icmp = buf+ip->ihl*4; printf("----------------n"); for ( i = 0; i < bytes; i++ ) { if ( !(i & 15) ) printf("n%04X: ", i); printf("%02X ", ((unsigned char*)buf)[i]); } printf("n"); printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ", ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol, ip->ttl, inet_ntoa(ip->saddr)); printf("dst=%sn", inet_ntoa(ip->daddr)); if ( icmp->un.echo.id == pid ) { printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]n", icmp->type, icmp->code, ntohs(icmp->checksum), icmp->un.echo.id, icmp->un.echo.sequence); } }
  • 18. cs423 - cotter 18 Listener - separate process to listen for and collect messages- void listener(void) { int sd, i; struct sockaddr_in addr; unsigned char buf[1024]; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); exit(0); } for (i = 0; i < loops; i++) { int bytes, len=sizeof(addr); bzero(buf, sizeof(buf)); bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *) &addr, &len); if ( bytes > 0 ) display(buf, bytes); else perror("recvfrom"); } exit(0); }
  • 19. cs423 - cotter 19 ping - Create message and send it void ping(struct sockaddr_in *addr) { const int val=255; int i, j, sd, cnt=1; struct packet pckt; struct sockaddr_in r_addr; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); return; } if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) perror("Set TTL option"); if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) perror("Request nonblocking I/O");
  • 20. cs423 - cotter 20 ping (cont) for (j = 0; j < loops; j++) { // send pings 1 per second int len=sizeof(r_addr); printf("Msg #%dn", cnt); if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *)&r_addr, &len) > 0 ) printf("***Got message!***n"); bzero(&pckt, sizeof(pckt)); pckt.hdr.type = ICMP_ECHO; pckt.hdr.un.echo.id = pid; for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0'; pckt.msg[i] = 0; pckt.hdr.un.echo.sequence = cnt++; pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); if (sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr *) addr, sizeof(*addr)) <= 0) perror("sendto"); sleep(1); } }
  • 21. cs423 - cotter 21 myNuPing.c – main()int main(int count, char *argv[]) { struct hostent *hname; struct sockaddr_in addr; loops = 0; if ( count != 3 ) { printf("usage: %s <addr> <loops> n", argv[0]); exit(0); } if (count == 3) // WE HAVE SPECIFIED A MESSAGE COUNT loops = atoi(argv[2]); if ( count > 1 ) { pid = getpid(); proto = getprotobyname("ICMP"); hname = gethostbyname(argv[1]); bzero(&addr, sizeof(addr)); addr.sin_family = hname->h_addrtype; addr.sin_port = 0; addr.sin_addr.s_addr = *(long*)hname->h_addr; if ( fork() == 0 ) listener(); else ping(&addr); wait(0); } else printf("usage: myping <hostname>n"); return 0; }
  • 22. cs423 - cotter 22 “Ping” Output [root]# ./myNuPing 134.193.12.34 2 Msg #1 ---------------- 0000: 45 00 00 54 CC 38 40 00 80 01 1F BE 86 12 34 56 0010: 86 12 34 57 00 00 E4 06 DF 07 01 00 30 31 32 33 0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 0050: 64 65 66 00 IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34 ICMP: type[0/0] checksum[58374] id[2015] seq[1] Msg #2 ***Got message!*** ---------------- 0000: 45 00 00 54 CC 39 40 00 80 01 1F BD 86 12 34 56 0010: 86 12 34 57 00 00 E3 06 DF 07 02 00 30 31 32 33 0020: 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 0030: 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 0040: 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 0050: 64 65 66 00 IPv4: hdr-size=20 pkt-size=84 protocol=1 TTL=128 src=134.193.12.35 dst=134.193.12.34 ICMP: type[0/0] checksum[58118] id[2015] seq[2] [root]#
  • 23. cs423 - cotter 23 Summary • Raw Sockets allow access to Protocols other than the standard TCP and UDP • Performance and capabilities may be OS dependent. – Some OSs block the ability to send packets that originate from raw sockets (although reception may be permitted). • Raw sockets remove the burden of the complex TCP/IP protocol stack, but they also remove the safeguards and support that those protocols provide

Editor's Notes

  1. cs423 - cotter
  2. cs423 - cotter
  3. cs423 - cotter
  4. cs423 - cotter
  5. cs423 - cotter
  6. cs423 - cotter
  7. cs423 - cotter
  8. cs423 - cotter
  9. cs423 - cotter
  10. cs423 - cotter