Initial version deployed for: http://www.hotelsevendays.cz/
[harpy.git] / harpy
1 #! /usr/bin/perl
2 #
3 # $Id$
4 #
5 # /etc/inittab:
6 # ha:2345:respawn:/usr/local/sbin/harpy >>/var/log/harpy.log eth1 eth2
7 # or
8 # ha:2345:respawn:/usr/local/sbin/harpy >>/var/log/harpy.log eth_intra
9
10 use bytes;
11 sub __KERNEL__ { 1; }   # for "linux/socket.ph"
12 use strict;
13 use warnings;
14 use Socket;
15 require "linux/if_ether.ph";
16 require "linux/socket.ph";
17 require "linux/sockios.ph";
18 require "linux/if_arp.ph";
19 use Data::Dumper;
20 sub SOCKADDR_SIZEOF { 16; }
21
22
23 my $V=1;        # 2
24
25
26 $|=1;
27
28 sub hw_ntoa($)
29 {
30 my($n)=@_;
31
32         my $a=unpack("H*",$n);
33         $a=~s/..(?=.)/$&:/g;
34         return $a;
35 }
36
37 sub hw_aton($)
38 {
39 my($a)=@_;
40
41         $a=~tr/://d;
42         return pack "H*",$a;
43 }
44
45 sub sock($)
46 {
47 my($ifname)=@_;
48
49         my $sock;
50         socket $sock,AF_PACKET(),SOCK_RAW(),ETH_P_ARP() or die "No ARP socket: $!";
51
52 local *ioctl_ifhwaddr=sub($)
53 {
54 my($ifname)=@_;
55
56         my $buf=$ifname.("\x00"x0x1000);
57         ioctl $sock,SIOCGIFHWADDR(),$buf or die "ioctl($ifname,SIOCGIFHWADDR): $!";
58         my($trash,$sockaddr)=unpack("a16a16",$buf);
59         my($sa_len,$sa_data)=unpack("a2a6",$sockaddr);
60         return $sa_data;
61 };
62
63 local *ioctl_ifindex=sub($)
64 {
65 my($ifname)=@_;
66
67         my $buf=$ifname.("\x00"x0x1000);
68         ioctl $sock,SIOCGIFINDEX(),$buf or die "ioctl($ifname,SIOCGIFINDEX): $!";
69         my($trash,$ifindex)=unpack("a16i",$buf);
70         return $ifindex;
71 };
72
73         my $hw=ioctl_ifhwaddr($ifname);
74         my $sockaddr_ll=pack "SniSCCa8",                # struct sockaddr_ll:
75                         AF_PACKET(),                    # unsigned short int sll_family;
76                         ETH_P_ARP(),                    # unsigned short int sll_protocol;
77                         ioctl_ifindex($ifname),         # int sll_ifindex;
78                         ARPHRD_ETHER(),                 # unsigned short int sll_hatype;
79                         PACKET_BROADCAST(),             # unsigned char sll_pkttype;
80                         ETH_ALEN(),                     # unsigned char sll_halen;
81                         $hw;                            # unsigned char sll_addr[8];
82
83         bind $sock,$sockaddr_ll or die "bind($ifname): $!";
84
85         return $sock,hw_ntoa($hw);
86 }
87
88 #struct ether_arp_frame {
89 #       struct ether_header {
90 #               u_int8_t  ether_dhost[ETH_ALEN];        /* destination eth addr */
91 #               u_int8_t  ether_shost[ETH_ALEN];        /* source ether addr    */
92 #               u_int16_t ether_type;                   /* packet type ID field */
93 #               } ether_hdr;
94 #       struct ether_arp {
95 #               struct arphdr {         /* fixed-size header */
96 #                       unsigned short int ar_hrd;      /* Format of hardware address.  */
97 #                       unsigned short int ar_pro;      /* Format of protocol address.  */
98 #                       unsigned char ar_hln;           /* Length of hardware address.  */
99 #                       unsigned char ar_pln;           /* Length of protocol address.  */
100 #                       unsigned short int ar_op;       /* ARP opcode (command).  */
101 #                       } ea_hdr;
102 #               u_int8_t arp_sha[ETH_ALEN];     /* sender hardware address */
103 #               u_int8_t arp_spa[4];            /* sender protocol address */
104 #               u_int8_t arp_tha[ETH_ALEN];     /* target hardware address */
105 #               u_int8_t arp_tpa[4];            /* target protocol address */
106 #               } arp;
107 #       };
108
109 sub arp_pack($$$$)
110 {
111 my($dst_hw,$src_hw,$dst,$src)=@_;
112
113         my $msg=pack "a6a6nnnCCna6a4a6a4",
114                 hw_aton($dst_hw),       # $ether_dhost
115                 hw_aton($src_hw),       # $ether_shost
116                 2054,                   # $ether_type
117                 1,                      # $ar_hrd
118                 2048,                   # $ar_pro
119                 6,                      # $ar_hln
120                 4,                      # $ar_pln
121                 ARPOP_REPLY(),          # $ar_op
122                 hw_aton($src_hw),       # $arp_sha
123                 inet_aton($src),        # $arp_spa
124                 hw_aton($dst_hw),       # $arp_tha
125                 inet_aton($dst);        # $arp_tpa
126         return $msg;
127 }
128
129 sub arp_unpack($)
130 {
131 my($msg)=@_;
132
133         return if 42>length $msg;
134         my(
135                 $ether_dhost,
136                 $ether_shost,
137                 $ether_type,
138                 $ar_hrd,
139                 $ar_pro,
140                 $ar_hln,
141                 $ar_pln,
142                 $ar_op,
143                 $arp_sha,
144                 $arp_spa,
145                 $arp_tha,
146                 $arp_tpa,
147                 )=unpack "a6a6nnnCCna6a4a6a4",$msg;
148         $V>=3 and print Data::Dumper->Dump([
149                         $ether_dhost,
150                         $ether_shost,
151                         $ether_type,
152                         $ar_hrd,
153                         $ar_pro,
154                         $ar_hln,
155                         $ar_pln,
156                         $ar_op,
157                         $arp_sha,
158                         $arp_spa,
159                         $arp_tha,
160                         $arp_tpa,
161                 ],[
162                         "ether_dhost",
163                         "ether_shost",
164                         "ether_type",
165                         "ar_hrd",
166                         "ar_pro",
167                         "ar_hln",
168                         "ar_pln",
169                         "ar_op",
170                         "arp_sha",
171                         "arp_spa",
172                         "arp_tha",
173                         "arp_tpa",
174                 ]);
175         return if $ar_op!=ARPOP_REQUEST();
176         return if $ar_hln!=6;
177         return if $ar_pln!=4;
178         my $tell=inet_ntoa $arp_spa;
179         my $who_has=inet_ntoa $arp_tpa;
180         my $tell_hw=hw_ntoa $ether_shost;
181         return $tell,$who_has,$tell_hw;
182 }
183
184
185 my %socks;
186 for my $ifname (@ARGV) {
187         my($sock,$hw)=sock($ifname);
188         $socks{$ifname}={
189                         "sock"=>$sock,
190                         "hw"=>$hw,
191                         };
192         }
193
194 $V and print localtime()." START\n";
195 for (;;) {
196         my $rfds="";
197         vec($rfds,fileno($_->{"sock"}),1)=1 for values(%socks);
198         my $got=select $rfds,undef(),undef(),undef();
199         die "Invalid select(2): ".Dumper($got) if !defined $got || $got<0;
200
201         while (my($ifname,$hash)=each(%socks)) {
202                 next if !vec($rfds,fileno($hash->{"sock"}),1);
203                 $V>=2 and print localtime()." got packet: $ifname\n";
204                 my $msg;
205                 defined(my $from_addr=recv $hash->{"sock"},$msg,0x1000,0) or die "recv($ifname): $!";
206                 next if !(my($tell,$who_has,$tell_hw)=arp_unpack($msg));
207                 $V and print localtime()." got: tell=$tell,who_has=$who_has,tell_hw=$tell_hw\n";
208                 next if $tell eq $who_has;      # do not reply to self-detection queries
209                 my $msg_reply=arp_pack($tell_hw,$hash->{"hw"},$tell,$who_has);
210                 send $hash->{"sock"},$msg_reply,0,$from_addr or die "send($ifname): $!";
211                 }
212         }