Pkt Sniffer Code (Read pkts only) using AF_PACKET Linux Sokets In the previous part we part we made a simple sniffer which created a raw socket and started receiving on it. But it had few drawbacks: 1. Could sniff only incoming data. 2. Could sniff only TC or !" or IC# or any one protocol packets at a time. $. rovided I frames% so &thernet headers were not available. In this article we are going to modify the same code to fi' the above $ dra wbacks. !o"e#er "e s$all not %e using li%pap. li%pap. T$is "ill %e done using pure Linux sokets& T$e differene is #ery s'all and is lines Instead of : sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP); 1 (e do : 1 2 3
sock_raw = socket( AF_PACKET , SOCK_RAW , htons(ETH_P_ALL)) ; //Opto!a" //setsockopt(sock_raw , SO#_SOCKET , SO_$IN%TO%E&ICE , 'et' , str"e!('et')* + );
and we are done.
)ow it will: 1. *niff both incoming and outgoing traffic. 2. *niff +,, &T-&)&T &T-&)&T /+#&*% which includes all kinds of I packets and even more if there are any. $. rovides the &thernet headers too% which contain the mac addresses. The setsockopt line is optional. Its important to provide the correct interface name to setsockopt % eth0 in this case and in most cases. *o may be you would like to present the user with a list of interfaces available and allow him to choose the one to be sniffed.
A*A+ ,-TE Can "e use t$is 'et$od "it$ t$e adapter in pro'isuous 'ode and apture e#eryt$ing on t$e "ire. Li%pap "ill do t$is %ut an t$is 'et$od do it.
Here s the !"## so"rce co$e% co$e% !c"-.e!et!et/!01 !c"-.eerr!o01 !c"-.e!et.201 !c"-.est.o01 //For sta!.ar. t!3s !c"-.est."201 //4a""oc !c"-.estr!301 //str"e!
!c"-.e!et!et/p_c4p01 !c"-.e!et!et/-.p01 !c"-.e!et!et/tcp01 !c"-.e!et!et/p01 !c"-.e!et!et/6_eter01 !c"-.e!et/eter!et01 !c"-.es8s/socket01 !c"-.earpa/!et01 !c"-.es8s/oct"01 !c"-.es8s/t4e01 !c"-.es8s/t8pes01 !c"-.e-!st.01
//Pro5.es .ec"arato!s //Pro5.es .ec"arato!s //Pro5.es .ec"arato!s //Pro5.es .ec"arato!s //For ET7_P_A## //For eter_ea.er
5o. ProcessPacket (-!s3!e. car& , 5o. ProcessPacket 5o. pr!t_p_ea.er 5o. pr!t_p_ea.er (-!s3!e. car& 5o. pr!t_tcp_packet 5o. pr!t_tcp_packet (-!s3!e. car 5o. pr!t_-.p_packet 5o. pr!t_-.p_packet (-!s3!e. car
!t); , !t); & , !t ); & , !t );
6or 6or 6or 6or
c4p ea.er -.p ea.er tcp ea.er p ea.er
5o. pr!t_c4p_packet (-!s3!e. car& , !t ); 5o. pr!t_c4p_packet 5o. Pr!t%ata 5o. Pr!t%ata (-!s3!e. car& , !t); FI#E &"o36"e ; str-ct socka..r_! str-ct socka..r_! so-rce ,.est; !t tcp !t tcp=,-.p=,c4p=,oters =,34p=,tota"=,,9; !t 4a!() !t 4a! ' !t sa..r_s:e !t sa..r_s:e , .ata_s:e ; str-ct socka..r str-ct socka..r sa..r ; -!s3!e. car &2-66er = (-!s3!e. car &) 4a""oc(<<); //Its $3>
"o36"e =6ope!('"o30t?t' ,'w'); !( ! ("o36"e == LL) LL) ' pr!t6 ('@!a2"e to create "o30t?t 6"e0' ); * pr!t6 ('Start!3000!' ); !t sock_raw = socket( AF_PACKET , SOCK_RAW , to!s(ET7_P_A## )) ; !t sock_raw //setsockopt(sock_raw , SO#_SOCKET , SO_$IN%TO%E&ICE , 'et' , str"e!('et')* + );
!( ! (sock_raw + ) ' //Pr!t te error wt proper 4essa3e perror ('Socket Error' ); ret"rn +; * wh#e( wh#e (+) ' sa..r_s:e = seo! sa..r ; //Rece5e a packet .ata_s:e = rec56ro4 (sock_raw , 2-66er , << , , -sa..r , (sock"e!_t &) -sa..r_s:e ); !( ! (.ata_s:e + ) ' pr!t6('Rec56ro4 error , 6a"e. to 3et packets!' ); ret"rn +; * //Now process te packet ProcessPacket (2-66er , .ata_s:e ); * c"ose(sock_raw ); pr!t6 ('F!se.' ); ret"rn ;
*
5o. ProcessPacket (-!s3!e. car& 2-66er, !t 5o. ProcessPacket !t s:e s:e) ' //Bet te IP 7ea.er part o6 ts packet , e?c"-.!3 te eter!et ea.er seo!( (str-ct str-ct p.r str-ct p.r &p = (str-ct str-ct p.r p.r &)(2-66er . seo! str-ct et.r et.r)); ..tota"; swtch (p/0 protoco" protoco" ) //Ceck te Protoco" a!. .o accor.!3"8000 ' case +% //ICP Protoco" ..c4p; pr!t_c4p_packet ( 2-66er , s:e); 1reak; 1reak ; case D% //IBP Protoco" ..34p; 1reak; 1reak ; case %
//TCP Protoco"
5o. pr!t_c4p_packet (-!s3!e. car& , !t ); 5o. pr!t_c4p_packet 5o. Pr!t%ata 5o. Pr!t%ata (-!s3!e. car& , !t); FI#E &"o36"e ; str-ct socka..r_! str-ct socka..r_! so-rce ,.est; !t tcp !t tcp=,-.p=,c4p=,oters =,34p=,tota"=,,9; !t 4a!() !t 4a! ' !t sa..r_s:e !t sa..r_s:e , .ata_s:e ; str-ct socka..r str-ct socka..r sa..r ; -!s3!e. car &2-66er = (-!s3!e. car &) 4a""oc(<<); //Its $3>
"o36"e =6ope!('"o30t?t' ,'w'); !( ! ("o36"e == LL) LL) ' pr!t6 ('@!a2"e to create "o30t?t 6"e0' ); * pr!t6 ('Start!3000!' ); !t sock_raw = socket( AF_PACKET , SOCK_RAW , to!s(ET7_P_A## )) ; !t sock_raw //setsockopt(sock_raw , SO#_SOCKET , SO_$IN%TO%E&ICE , 'et' , str"e!('et')* + );
!( ! (sock_raw + ) ' //Pr!t te error wt proper 4essa3e perror ('Socket Error' ); ret"rn +; * wh#e( wh#e (+) ' sa..r_s:e = seo! sa..r ; //Rece5e a packet .ata_s:e = rec56ro4 (sock_raw , 2-66er , << , , -sa..r , (sock"e!_t &) -sa..r_s:e ); !( ! (.ata_s:e + ) ' pr!t6('Rec56ro4 error , 6a"e. to 3et packets!' ); ret"rn +; * //Now process te packet ProcessPacket (2-66er , .ata_s:e ); * c"ose(sock_raw ); pr!t6 ('F!se.' ); ret"rn ;
*
5o. ProcessPacket (-!s3!e. car& 2-66er, !t 5o. ProcessPacket !t s:e s:e) ' //Bet te IP 7ea.er part o6 ts packet , e?c"-.!3 te eter!et ea.er seo!( (str-ct str-ct p.r str-ct p.r &p = (str-ct str-ct p.r p.r &)(2-66er . seo! str-ct et.r et.r)); ..tota"; swtch (p/0 protoco" protoco" ) //Ceck te Protoco" a!. .o accor.!3"8000 ' case +% //ICP Protoco" ..c4p; pr!t_c4p_packet ( 2-66er , s:e); 1reak; 1reak ; case D% //IBP Protoco" ..34p; 1reak; 1reak ; case %
//TCP Protoco"
..tcp; pr!t_tcp_packet (2-66er , s:e); 1reak; 1reak ; case +% //@%P Protoco" ..-.p; pr!t_-.p_packet (2-66er , s:e); 1reak; 1reak ;
$e!a"#t% //So4e Oter Protoco" "ke ARP etc0 $e!a"#t% ..oters ; 1reak; 1reak ; * pr!t6 ('TCP G. @%P G. ICP G. IBP G. , -.p , c4p , 34p , oters , tota"); *
Oters G.
Tota" G.r' , tcp
5o. pr!t_eter!et_ea.er (-!s3!e. car& $-66er, !t 5o. pr!t_eter!et_ea.er !t S:e S:e) ' str-ct et.r str-ct et.r &et = (str-ct str-ct et.r et.r &)$-66er ;
*
6pr!t6 ("o36"e , '!'); 6pr!t6 ("o36"e , 'Eter!et 7ea.er!' ); 6pr!t6 ("o36"e , ' H%est!ato! A..ress G0DJG0DJG0DJG0DJG0DJ G0DJG0DJG0DJG0DJG0DJG0DJ G0DJ !' , et/0 _.est _.est 23,et/0 _.est _.est2+3,et/0 _.est _.est2D3,et/0 _.est _.est23,et/0 _.est _.est 23, et/0 _.est _.est 2<3 ); 6pr!t6 ("o36"e , ' HSo-rce A..ress G0DJG0DJG0DJG0DJG0DJ G0DJG0DJG0DJG0DJG0DJG0DJ G0DJ !' , et/0 _so-rce _so-rce 23 , et/0 _so-rce _so-rce 2+3 , et/0 _so-rce _so-rce 2D3 , et/0 _so-rce _so-rce 23 , et/0 _so-rce _so-rce 23 , et/0 _so-rce _so-rce 2<3 ); 6pr!t6 ("o36"e , ' HProtoco" G- !' ,(-!s3!e. sort)et/0 _proto _proto );
5o. pr!t_p_ea.er (-!s3!e. car& $-66er , !t 5o. pr!t_p_ea.er !t S:e S:e) ' pr!t_eter!et_ea.er ($-66er , S:e); -!s3!e. sort sort p.r"e! p.r"e! ;
str-ct p.r &p = (str-ct str-ct p.r str-ct p.r p.r &)($-66er p.r"e! =p/0 " "&;
seo!( (so-rce )); 4e4set (-so-rce , , seo! so-rce 4s!_a..r 4s_a..r = p/0 sa..r sa..r;
4e4set (-.est, , seo! seo!( (.est)); .est4s!_a..r 4s_a..r = p/0 .a..r .a..r ;
. seo! seo!( (str-ct str-ct et.r et.r) );
6pr!t6 ("o36"e , '!'); 6pr!t6 ("o36"e , 'IP 7ea.er!' ); 6pr!t6 ("o36"e , ' HIP &erso! G.!' ,(-!s3!e. !t)p/0 5erso! 5erso! ); 6pr!t6 ("o36"e , ' HIP 7ea.er #e!3t G. %WOR%S or G. $8tes!' , (-!s3!e. !t)p/0 " ", ((-!s3!e. !t)(p/0 " "))&); 6pr!t6 ("o36"e , ' HT8pe O6 Ser5ce G.!' ,(-!s3!e. !t)p/0 tos tos); 6pr!t6 ("o36"e , ' HIP Tota" #e!3t G. $8tes(S:e o6 Packet)!' , !tos(p/0 tot_"e! tot_"e! )); 6pr!t6 ("o36"e , ' HI.e!t6cato! G.!' ,!tos(p/0 . .)); //6pr!t6("o36"e , ' HReser5e. LERO Fe". G.!', (-!s3!e. !t)p.r 1p_reser5e._:ero); //6pr!t6("o36"e , ' H%o!t Fra34e!t Fe". G.!', (-!s3!e. !t)p.r1p_.o!t_6ra34e!t) !t)p.r1p_.o!t_6ra34e!t); ;
*
//6pr!t6("o36"e , ' Hore Fra34e!t Fe". G.!', (-!s3!e. !t)p.r1p_4ore_6ra34e!t); 6pr!t6("o36"e , ' HTT# G.!' ,(-!s3!e. !t)p/0 tt"); 6pr!t6("o36"e , ' HProtoco" G.!' ,(-!s3!e. !t)p/0 protoco" ); 6pr!t6("o36"e , ' HCecks-4 G.!' ,!tos(p/0 ceck)); 6pr!t6("o36"e , ' HSo-rce IP Gs!' ,!et_!toa (so-rce4s!_a..r )); 6pr!t6("o36"e , ' H%est!ato! IP Gs!' ,!et_!toa (.est4s!_a..r ));
5o. pr!t_tcp_packet (-!s3!e. car& $-66er, !t S:e) ' -!s3!e. sort p.r"e!;
str-ct p.r &p = (str-ct p.r &)( $-66er p.r"e! = p/0 "&;
. seo!(str-ct et.r) );
str-ct tcp.r &tcp=(str-ct tcp.r&)($-66er . p.r"e! . seo!(str-ct et.r)); !t ea.er_s:e =
seo!(str-ct et.r) . p.r"e! . tcp/0 .o66&;
6pr!t6 ("o36"e , '!!MMMMMMMMMMMMMMMMMMMMMMTCP PacketMMMMMMMMMMMMMMMMMMMMMMMMM!' ); pr!t_p_ea.er ($-66er,S:e);
6pr!t6 ("o36"e , '!'); 6pr!t6("o36"e , 'TCP 7ea.er!' ); 6pr!t6("o36"e , ' HSo-rce Port G-!' ,!tos(tcp/0 so-rce)); 6pr!t6("o36"e , ' H%est!ato! Port G-!' ,!tos(tcp/0 .est)); 6pr!t6("o36"e , ' HSe-e!ce N-42er G-!' ,!to"(tcp/0 se)); 6pr!t6("o36"e , ' HAck!ow"e.3e N-42er G-!' ,!to"(tcp/0 ack_se )); 6pr!t6("o36"e , ' H7ea.er #e!3t G. %WOR%S or G. $TES!' , .o66,(-!s3!e. !t)tcp/0 .o66&); (-!s3!e. !t)tcp/0 //6pr!t6("o36"e , ' HCWR F"a3 G.!',(-!s3!e. !t)tcp1cwr); //6pr!t6("o36"e , ' HECN F"a3 G.!',(-!s3!e. !t)tcp1ece); 6pr!t6("o36"e , ' H@r3e!t F"a3 G.!' ,(-!s3!e. !t)tcp/0 -r3); 6pr!t6("o36"e , ' HAck!ow"e.3e4e!t F"a3 G.!' ,(-!s3!e. !t)tcp/0 ack); 6pr!t6("o36"e , ' HP-s F"a3 G.!' ,(-!s3!e. !t)tcp/0 ps); 6pr!t6("o36"e , ' HReset F"a3 G.!' ,(-!s3!e. !t)tcp/0 rst); 6pr!t6("o36"e , ' HS8!cro!se F"a3 G.!' ,(-!s3!e. !t)tcp/0 s8!); 6pr!t6("o36"e , ' HF!s F"a3 G.!' ,(-!s3!e. !t)tcp/0 6!); 6pr!t6("o36"e , ' HW!.ow G.!' ,!tos(tcp/0 w!.ow)); 6pr!t6("o36"e , ' HCecks-4 G.!' ,!tos(tcp/0 ceck)); 6pr!t6("o36"e , ' H@r3e!t Po!ter G.!' ,tcp/0 -r3_ptr ); 6pr!t6("o36"e , '!'); 6pr!t6("o36"e , ' %ATA %-4p ' ); 6pr!t6("o36"e , '!');
6pr!t6 ("o36"e , 'IP 7ea.er!' ); Pr!t%ata ($-66er,p.r"e! );
6pr!t6 ("o36"e , 'TCP 7ea.er!' ); Pr!t%ata ($-66er.p.r"e! ,tcp/0 .o66&);
6pr!t6 ("o36"e , '%ata Pa8"oa.!' ); Pr!t%ata ($-66er . ea.er_s:e , S:e / ea.er_s:e ); 6pr!t6 ("o36"e , '!' ); *
5o. pr!t_-.p_packet (-!s3!e. car &$-66er , !t S:e) '
-!s3!e. sort p.r"e!;
str-ct p.r &p = (str-ct p.r &)($-66er . p.r"e! = p/0 "&;
seo!(str-ct et.r));
str-ct -.p.r &-.p = (str-ct -.p.r&)($-66er . p.r"e! !t ea.er_s:e =
. seo!(str-ct et.r ));
seo!(str-ct et.r) . p.r"e! . seo! -.p;
6pr!t6 ("o36"e , '!!MMMMMMMMMMMMMMMMMMMMMM@%P PacketMMMMMMMMMMMMMMMMMMMMMMMMM!' ); pr!t_p_ea.er ($-66er,S:e);
6pr!t6 ("o36"e 6pr!t6("o36"e 6pr!t6("o36"e 6pr!t6("o36"e 6pr!t6("o36"e
6pr!t6 ("o36"e , '!'); 6pr!t6("o36"e , 'IP 7ea.er!' ); Pr!t%ata ($-66er , p.r"e!);
6pr!t6 ("o36"e , '@%P 7ea.er!' ); Pr!t%ata ($-66er.p.r"e! , seo! -.p);
, , , , ,
'!@%P 7ea.er!' ); ' HSo-rce Port ' H%est!ato! Port ' H@%P #e!3t ' H@%P Cecks-4
G.!' G.!' G.!' G.!'
, !tos(-.p/0 so-rce)); , !tos(-.p/0 .est)); , !tos(-.p/0 "e!)); ceck)); , !tos(-.p/0
6pr!t6 ("o36"e , '%ata Pa8"oa.!' );
//o5e te po!ter aea. a!. re.-ce te s:e o6 str!3 Pr!t%ata ($-66er . ea.er_s:e , S:e / ea.er_s:e ); 6pr!t6 ("o36"e , '!' );
*
5o. pr!t_c4p_packet (-!s3!e. car& $-66er , !t S:e) ' -!s3!e. sort p.r"e!;
str-ct p.r &p = (str-ct p.r &)($-66er p.r"e! = p/0 " & ;
. seo!(str-ct et.r));
str-ct c4p.r &c4p = (str-ct c4p.r &)($-66er . p.r"e! . seo!(str-ct et.r )); !t ea.er_s:e =
seo!(str-ct et.r) . p.r"e! . seo! c4p;
6pr!t6 ("o36"e , '!!MMMMMMMMMMMMMMMMMMMMICP PacketMMMMMMMMMMMMMMMMMMMMMM!' ); pr!t_p_ea.er ($-66er , S:e); 6pr!t6 ("o36"e , '!');
6pr!t6 ("o36"e , 'ICP 7ea.er!' ); 6pr!t6("o36"e , ' HT8pe G.' ,(-!s3!e. !t)(c4p/0 t8pe)); !((-!s3!e. !t)(c4p/0 t8pe) == ++) ' 6pr!t6("o36"e , ' (TT# E?pre.)!' ); * e#se !((-!s3!e. !t)(c4p/0 t8pe) == ICP_EC7OREP# ) ' 6pr!t6("o36"e , ' (ICP Eco Rep"8)!' ); *
6pr!t6 ("o36"e , 6pr!t6("o36"e , //6pr!t6("o36"e //6pr!t6("o36"e 6pr!t6("o36"e ,
6pr!t6("o36"e , 'IP 7ea.er!' ); Pr!t%ata ($-66er,p.r"e! );
6pr!t6 ("o36"e , '@%P 7ea.er!' ); Pr!t%ata ($-66er . p.r"e! , seo! c4p);
' HCo.e G.!' ,(-!s3!e. !t)(c4p/0 co.e)); ' HCecks-4 G.!' ,!tos(c4p/0 cecks-4 )); , ' HI% G.!',!tos(c4p1.)); , ' HSe-e!ce G.!',!tos(c4p1se-e!ce)); '!');
6pr!t6 ("o36"e , '%ata Pa8"oa.!' );
//o5e te po!ter aea. a!. re.-ce te s:e o6 str!3 Pr!t%ata ($-66er . ea.er_s:e , (S:e / ea.er_s:e ) ); 6pr!t6 ("o36"e , '!' );
*
5o. Pr!t%ata (-!s3!e. car& .ata , !t S:e) ' !t , 9; !or(= ; + S:e ; ..) ' !( 5= -- 6+==) //6 o!e "!e o6 e? pr!t!3 s co4p"ete000 ' 6pr!t6 ("o36"e , ' '); !or(9=/+ ; 9 + ; 9..) ' !(.ata2930=D -- .ata293+=+D) 6pr!t6("o36"e , 'Gc',(-!s3!e. car).ata293); //6 !-42er or a"pa2et e#se 6pr!t6 ("o36"e , '0'); //oterwse pr!t a .ot * 6pr!t6 ("o36"e , '!'); * !(6+==) 6pr!t6("o36"e , ' '); 6pr!t6("o36"e , ' GDJ',(-!s3!e. !t).ata23);
!( ==S:e/+) //pr!t te "ast spaces ' !or(9=;9 ++6+;9..) ' 6pr!t6("o36"e , ' '); //e?tra spaces *
6pr!t6 ("o36"e , '
!or(9=/6+ ; 9 += ; 9..) ' !(.ata2930=D -- .ata293+=+D) ' 6pr!t6("o36"e , 'Gc',(-!s3!e. car).ata293); * e#se ' 6pr!t6("o36"e , '0'); * * 6pr!t6 ("o36"e , '!' );
* * *
');
The log file will looks somewhat like this :
MMMMMMMMMMMMMMMMMMMMMMMTCP PacketMMMMMMMMMMMMMMMMMMMMMMMMM
Eter!et 7ea.er
H%est!ato! A..ress D<
HSo-rce A..ress
+CCFQEE
HProtoco"
IP 7ea.er
HIP &erso!
HIP 7ea.er #e!3t
< %WOR%S or D $8tes
HT8pe O6 Ser5ce
HIP Tota" #e!3t
++
HI.e!t6cato!
++DD
HTT#
$8tes(S:e o6 Packet)
HProtoco"
HCecks-4
HSo-rce IP
+QD0+0+0
H%est!ato! IP
0+D<0+0+D<
TCP 7ea.er
HSo-rce Port
<<
H%est!ato! Port
HSe-e!ce N-42er
<<
HAck!ow"e.3e N-42er DD
H7ea.er #e!3t
< %WOR%S or D $TES
H@r3e!t F"a3
HAck!ow"e.3e4e!t F"a3 +
HP-s F"a3
+
HReset F"a3
HS8!cro!se F"a3
HF!s F"a3
HW!.ow
DQD
HCecks-4
D+<
H@r3e!t Po!ter
%ATA %-4p
IP 7ea.er
D<
0G0=000008000E0
% D
00$
TCP 7ea.er
$ C A + A % %
0000000UVBV0w
+ A%
0600
%ata Pa8"oa.
+ + A QC <% + A+ D< A$ CE $ C E$
000000X00G000H0
+A A A %% E $ E C+ Q A %D D< D
00C000k!C0090G
Q
0030,00Y0000#
% A E % QE F $ CE QE $ F E+ $% QE < <
00000k00k0000PS
% F A$ ++ < % + D F C A ED D$ +
400000A0000007*
+ + FF <$ %F < % <$ A% Q
000Z0P0Z000#000
$ D E D
0rYD0
In the above log we can see the &thernet headers being printed. They show the source and destination mac address along with the packet protocol. means I protocol )ote: 1. If you want to sniff only I and + packets for e'ample then you can try this: sockraw 3 socket4 +/+C5&T % *6C5+( % htons4&T-I7&T-+88 9 The complete list of protocols is found in usrincludelinu'ifether.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/M M Tese are te .e6!e. Eter!et Protoco" I%[s0 M/ .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e
ET7_P_#OOP ? ET7_P_P@P ?D ET7_P_P@PAT ?D+ ET7_P_IP ? ET7_P_JD< ?< ET7_P_ARP ? ET7_P_$P\ ?FF ET7_P_IEEEP@P ?a ET7_P_IEEEP@PAT ?a+ ET7_P_%EC ? ET7_P_%NA_%# ?+ ET7_P_%NA_RC ?D ET7_P_%NA_RT ?
/M /M /M /M /M /M /M /M /M /M /M /M /M
Eter!et #oop2ack packet M/ Jero? P@P packet M/ Jero? P@P A..r Tra!s packet M/ I!ter!et Protoco" packet M/ CCITT J0D< M/ A..ress Reso"-to! packet M/ B$P\ AJ0D< Eter!et Packet ZNOT AN OFFICIA## REBISTERE% I%X M/ Jero? IEEED0 P@P packet M/ Jero? IEEED0 P@P A..r Tra!s packet M/ %EC Ass3!e. proto M/ %EC %NA %-4p/#oa. M/ %EC %NA Re4ote Co!so"e M/ %EC %NA Ro-t!3 M/
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
&n;oy<<
.e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e
.e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e
ET7_P_#AT ? /M %EC #AT M/ ET7_P_%IAB ?< /M %EC %a3!ostcs M/ ET7_P_C@ST ? /M %EC C-sto4er -se M/ ET7_P_SCA ? /M %EC S8ste4s Co44s Arc M/ ET7_P_TE$ ?<< /M Tra!s Eter $r.3!3 M/ ET7_P_RARP ?< /M Re5erse A..r Res packet M/ ET7_P_ATA#K ?Q$ /M App"eta"k %%P M/ ET7_P_AARP ?F /M App"eta"k AARP M/ ET7_P_D+\ ?+ /M D0+\ AN E?te!.e. 7ea.er M/ ET7_P_IPJ ?+ /M IPJ o5er %IJ M/ ET7_P_IP& ?%% /M IP5 o5er 2"-e2ook M/ ET7_P_PA@SE ? /M IEEE Pa-se 6ra4es0 See D0 +$ M/ ET7_P_S#OW ?Q /M S"ow Protoco"0 See D0a. $ M/ ET7_P_WCCP ?E /M We2cace coor.!ato! protoc .ra6tw"so!wrecwccp5D0t?t M/ ET7_P_PPP_%ISC ? /M PPPoE .sco5er8 4essa3es M/ ET7_P_PPP_SES ? /M PPPoE sesso! 4essa3es M/ ET7_P_P#S_@C ? /M P#S @!cast tra66c M/ ET7_P_P#S_C ? /M P#S -"tcast tra66c M/ ET7_P_ATPOA ?c /M -"tProtoco" O5er AT M/ ET7_P_#INK_CT# ?c /M 7PNA, w"a! "!k "oca" t-!!e" M/ ET7_P_ATFATE ? /M Fra4e2ase. AT Tra!sport M o5er Eter!et M/ ET7_P_PAE ?E /M Port Access E!tt8 (IEEE D0+J) M/ ET7_P_AOE ?AD /M ATA o5er Eter!et M/ ET7_P_TIPC ?CA /M TIPC M/ ET7_P_+< ?F /M IEEE +< T4es8!c M/ ET7_P_FCOE ?Q /M F2re Ca!!e" o5er Eter!et M/ ET7_P_FIP ?Q+ /M FCoE I!ta":ato! Protoco" M/ ET7_P_E%SA ?%A%A /M Etert8pe %SA Z NOT AN OFFICIA## REBISTERE% I% X M/
/M M No! %IJ t8pes0 Wo![t c"as 6or +< t8pes0 M/ .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e .e6!e
ET7_P_D_ ?+ ET7_P_AJD< ?D ET7_P_A## ? ET7_P_D_D ? ET7_P_SNAP ?< ET7_P_%%CP ? ET7_P_WAN_PPP ? ET7_P_PPP_P ? ET7_P_#OCA#TA#K ?Q ET7_P_CAN ?C ET7_P_PPPTA#K ?+ ET7_P_TR_D_D ?++ ET7_P_O$ITEJ ?+< ET7_P_CONTRO# ?+ ET7_P_IR%A ?+ ET7_P_ECONET ?+ ET7_P_7%#C ?+Q ET7_P_ARCNET ?+A ET7_P_%SA ?+$ ET7_P_TRAI#ER ?+C ET7_P_P7ONET ?F< ET7_P_IEEED+< ?F ET7_P_CAIF ?F
/M /M /M /M /M /M /M /M /M
%-448 t8pe 6or D0 6ra4es M/ %-448 protoco" . 6or AJ0D< M/ E5er8 packet (2e care6-">>>) M/ D0D 6ra4es M/ I!ter!a" o!"8 M/ %EC %%CP I!ter!a" o!"8 M/ %-448 t8pe 6or WAN PPP 6ra4esM/ %-448 t8pe 6or PPP P 6ra4es M/ #oca"ta"k pse-.o t8pe M/ /M Co!tro""er Area Network M/ /M %-448 t8pe 6or Ata"k o5er PPPM/ /M D0D 6ra4es M/ /M o2te? (ka:ca6e0!et) M/ /M Car. spec6c co!tro" 6ra4es M/ /M #!-?Ir%A M/ /M Acor! Eco!et M/ /M 7%#C 6ra4es M/ /M +A 6or ArcNet ) M/ /M %str2-te. Swtc Arc0 M/ /M Tra"er swtc ta33!3 M/ /M Noka Po!et 6ra4es M/ /M IEEED0+<0 6ra4e M/ /M STErcsso! CAIF protoco" M/
Send an Et$ fra'e using an AF_PACKET soket in C Content
•
1 Objective
•
2 Background
•
3 Scenario
•
4 Method
o
4.1 Overview
o
4.2 Select the required EtherT!e
o
4.3 "reate the #$%"'ET (ocket
o
4.4 )eter*ine the inde+ nu*ber o, the Ethernet inter,ace to be u(ed
o
4.- "on(truct the de(tination addre((
o
4. Send the Ethernet ,ra*e
o
4./ Send the ,ra*e 0u(ing (endto
o
4. Send the ,ra*e 0u(ing (end*(g
•
- #lternative(
o
-.1 (ing lib!ca!
o
-.2 (ing a raw (ocket
•
$urther reading
Tested on
Debian (Lenny) Ubuntu (Lucid, Trusty)
6b;ective To send an arbitrary t!ernet "ra#e usin$ an
AF_PACKET soc%et
Background t!ernet is a &in% &ayer 'rotoco& ost net*or%in$ 'ro$ra#s interact *it! t!e net*or% stac% at t!e trans'ort &ayer or abo+e, so !a+e no need to dea& *it! t!ernet "ra#es direct&y, but t!ere are so#e circu#stances *!ere interaction at a &o*er &e+e& #ay be necessary T!ese inc&ude •
•
i#'&e#entation o" t!ernet-based 'rotoco&s t!at are not bui&t in to t!e net*or% stac%, and 'roduction o" #a&"or#ed or ot!er*ise non-standard "ra#es "or testin$ 'ur'oses
*cenario .u''ose t!at you *is! to send an / reuest "or t!e address 192168083 T!e reuest is to be sent "ro# inter"ace et to t!e broadcast / adddress (/ is t!e /ddress eso&ution rotoco& t is used *!en a !ost needs to send a data$ra# to a $i+en address, but does not %no* *!ic! / address corres'onds to t!at address)
#ethod 6verview T!e #et!od described !ere !as "i+e ste's 1 .e&ect t!e reuired t!erTy'e 2 reate t!e AF_PACKET soc%et 3 Deter#ine t!e inde nu#ber o" t!e t!ernet inter"ace to be used 4 onstruct t!e destination address 5 .end t!e t!ernet "ra#e T!e "o&&o*in$ !eader "i&es are used !eader
/sed %y
err!o01 str!301
err!o
arpa/!et01
!_a..r_t , to!s
!et/eter!et01 !et/601 !et!et/6_eter0 1 !etpacket/packet0 1 s8s/oct"01 s8s/socket01
ET7ER_A%%R_#EN , ET7_P_M str-ct 6re
4e4cp8, strerror , str"e!
str-ct eter_arp str-ct socka..r_"" SIOCBIFIN%EJ , oct" str-ct socka..r , str-ct o5ec , str-ct
4s3.r, AF_PACKET , SOCK_%BRA , socket, se!.to, se!.4s3 AF_PACKET soc%ets
are s'eci"ic to Linu ro$ra#s t!at #a%e use o" t!e# need e&e+ated 'ri+i&e$es
in order to run .ettin$ SO_$ROA%CAST does not a''ear to be necessary *!en sendin$ broadcast "ra#es usin$ an AF_PACKET soc%et .o#e 'ro$ra#s do so any*ay, *!ic! is un&i%e&y to be !ar#"u&, and cou&d be considered a *ort!*!i&e !ed$e a$ainst any "uture c!an$e in be!a+iour
*elect the re=uired &therType T!e t!erTy'e o" an t!ernet "ra#e s'eci"ies t!e ty'e o" 'ay&oad t!at it contains T!ere are se+era& sources "ro# *!ic! t!erTy'es can be obtained •
T!e !eader "i&e "!-?/6_eter01 'ro+ides constants "or #ost co##on&y-used t!erTy'es a#'&es inc&ude ET7_P_IP "or t!e nternet rotoco& ( ? ), ET7_P_ARP "or t!e /ddress eso&ution rotoco& ( ?) and ET7_P_D+\ "or 8021 L/ ta$s ( ?+)
•
T!e #aintains t!e de"initi+e &ist o" re$istered t!erTy'es
•
/ se#i-o""icia& &ist is #aintained by //
T!e *i&dcard +a&ue ET7_P_A## a&&o*s any t!erTy'e to be recei+ed *it!out usin$ #u&ti'&e soc%ets T!is inc&udes t!erTy'es t!at are !and&ed by t!e %erne&, suc! as and / " you need an t!erTy'e "or e'eri#enta& or 'ri+ate use t!en t!e +a&ues been reser+ed "or t!at 'ur'ose
?2< and ?2 !a+e
Create the +/+C5&T socket T!e soc%et t!at *i&& be used to send t!e t!ernet "ra#e s!ou&d be created usin$ t!e T!is ta%es t!ree ar$u#ents
socket "unction
•
t!e do#ain (AF_PACKET "or a 'ac%et soc%et)
•
t!e soc%et ty'e (SOCK_%BRA i" you *ant t!e t!ernet !eader to be constructed "or you or SOCK_RAW i" you *ant to construct it yourse&") and
•
t!e 'rotoco& (eua& to t!e t!erty'e c!osen abo+e, con+erted to net*or% byte order), *!ic! is used "or "i&terin$ inbound 'ac%ets
n t!is instance t!e soc%et *i&& be used "or sendin$ (and 'resu#ab&y a&so recei+in$) / reuests, t!ere"ore t!e t!ird ar$u#ent s!ou&d be set to to!s(ET7_P_ARP) (or eui+a&ent&y, to!s(?) ) T!ere is no need to construct a custo# t!ernet !eader so t!e second ar$u#ent s!ou&d be set to SOCK_%BRA