/[pxelator]/lib/PXElator/dhcpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 72 by dpavlin, Thu Jul 30 23:57:19 2009 UTC revision 260 by dpavlin, Wed Aug 19 10:56:04 2009 UTC
# Line 18  use autodie; Line 18  use autodie;
18  use IO::Socket::INET;  use IO::Socket::INET;
19  use File::Slurp;  use File::Slurp;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
 use Net::Ping;  
 use Module::Refresh;  
21    
22  use lib '..';  use lib '..';
23  use Net::DHCP::Packet;  use Net::DHCP::Packet;
24  use Net::DHCP::Constants 0.67;  use Net::DHCP::Constants 0.67;
25    
26    use CouchDB;
27    use format;
28    
29  use server;  use server;
30  my $debug = server::debug;  my $debug = server::debug;
 use pxe;  
31    
32  if ( ! $server::ip ) {  if ( ! $server::ip ) {
33          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 35  if ( ! $server::ip ) { Line 35  if ( ! $server::ip ) {
35          $server::ip = $server_ip;          $server::ip = $server_ip;
36  }  }
37    
38  warn "server ip $server::ip file: $pxe::file range: $server::ip_from - $server::ip_to\n";  warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
39    
40  my $addr = $server::ip_from;  use client;
41    
42  sub client_ip {  sub client_mac_ip {
43          my ( $mac ) = @_;          my ( $mac, $request_ip ) = @_;
44    
45          my $conf = "$server::base_dir/conf/$server::ip";          my $conf = $server::conf;
46          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
47    
48          if ( -e "$conf/mac/$mac" ) {          my $ip;
                 my $ip = read_file "$conf/mac/$mac";  
                 print "$mac old $ip\n";  
                 return $ip;  
         }  
   
         mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );  
   
         my $p = Net::Ping->new;  
49    
50          my $prefix = $server::ip;          if ( $ip = client::ip_from_mac( $mac ) ) {
51          $prefix =~ s{\.\d+$}{.};                  print "RENEW $mac $ip\n";
52          my $ip = $prefix . $addr;                  return $ip;
53          while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {          } elsif ( in_our_range( $request_ip ) ) {
54                  $ip = $prefix . $addr++;                  $ip = client::next_ip( $mac );
55                  die "all addresses allocated!" if $addr == $server::ip_to;                  print "NEW $mac $ip\n";
56          }          } else {
57                    $ip = $request_ip;
58          write_file "$conf/mac/$mac", $ip;                  client::save_ip_mac( $ip, $mac );
59          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {                  warn "W: $ip our of server range $server::ip $server::netmask\n";
                 unlink     "$conf/ip/$ip";  
                 symlink    "$conf/mac/$mac", "$conf/ip/$ip";  
                 warn "$mac IP changed to $ip";  
60          }          }
61    
         print "$mac NEW $ip\n";  
   
62          return $ip;          return $ip;
63  }  }
64    
65    use log;
66    use config;
67    use pxelinux;
68    use client;
69    
70    our $file;
71  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
72    
73    sub ip2bin { pack('C*', split(/\./, $_[0])) };
74    sub in_our_range {
75            my $ip = shift;
76            return 1 if $ip eq '0.0.0.0';
77            return 1 if (
78                    ( ip2bin($ip)         & ip2bin($server::netmask) )
79                    eq
80                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
81            );
82    }
83    
84  sub process_packet {  sub process_packet {
85          my $sock = shift;          my $sock = shift;
86    
87            server->refresh;
88    
89          my $buf;          my $buf;
90          $sock->recv($buf, 1024);          $sock->recv($buf, 1024);
91          my $size = 'empty';          my $size = 'empty';
92          $size = length($buf) . ' bytes' if defined $buf;          $size = length($buf) . ' bytes' if defined $buf;
93    
94          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n";          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
95          return unless $buf;          return unless $buf;
96    
97          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
98    
99          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
100    
101            $dhcp->comment( $transaction++ );
102    
103          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
104          my $ip = client_ip($mac);          my $ip = client_mac_ip($mac, $dhcp->ciaddr);
105    
106            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
107            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
108    
109            my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname,
110                    options => {
111                            map {
112                                    ( $_ => $dhcp->getOptionValue( $_ ) )
113                            } @{ $dhcp->{options_order} }
114                    },
115            };
116    
117  =for later  =for later
118    
# Line 108  sub process_packet { Line 126  sub process_packet {
126    
127  =cut  =cut
128    
129            config::for_ip( $ip );
130    
131          my $packet = {          my $packet = {
132                  Op              => BOOTREPLY(),                  Op              => BOOTREPLY(),
133                  Hops    => $dhcp->hops(),                  Hops    => $dhcp->hops(),
# Line 118  sub process_packet { Line 138  sub process_packet {
138                  Siaddr  => $server::ip,                  Siaddr  => $server::ip,
139                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
140                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
141                  File    => $pxe::file,                  File    => $file,
142                    DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request
143          };          };
144    
145          pxe::config_for_ip( $ip );          my $options = {
146                    DHO_SUBNET_MASK()       => $server::netmask,
147                    DHO_ROUTERS()           => $server::ip,
148                    DHO_DOMAIN_NAME()       => $server::domain_name,
149                    DHO_NAME_SERVERS()      => $server::ip,
150                    DHO_DOMAIN_NAME_SERVERS() => $server::ip,
151                    DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
152                    DHO_BROADCAST_ADDRESS() => $server::bcast,
153    #               DHO_NTP_SERVERS() => '',
154            };
155    
156            my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
157            warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
158    
159            my @missing;
160            foreach ( @requested ) {
161                    if ( defined $options->{$_} ) {
162                            $packet->{$_} = $options->{$_};
163                    } else {
164                            push @missing, $_;
165                    }
166            }
167    
168            warn "W: options requested but missing: ",dump( @missing ),$/;
169            $audit->{requested} = [ @requested ];
170            $audit->{missing}   = [ @missing   ];
171    
172            foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
173                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
174                    warn "DH0: $@" if $@;
175                    my $v = eval "\$pxelinux::$opt";
176                    warn "v: $@" if $@;
177                    next unless defined $v;
178                    warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
179                    $packet->{ $DH0 } = $v;
180            }
181    
182          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
183    
184          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
185                  log::mac $mac, "DHCP DISCOVER";                  $audit->{type} = 'discover';
186                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
187                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
188          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
189                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
190                  log::mac $mac, "DHCP REQUEST $requested_ip";                  $audit->{type} = 'request';
191                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
192                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
193                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
 #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME  
                         $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';  
                         $packet->{DHO_ROUTERS()}                = $server::ip;  
 #                       $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';  
 #                       $packet->{DHO_NAME_SERVERS()}           = $server::ip;  
194  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
195                  } else {                  } else {
196                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
197                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
198                  }                  }
199          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
200                  log::mac $mac, "DHCP INFORM ignored";                  $audit->{type} = 'inform';
201          } else {          } else {
202                  log::mac $mac, "$messagetype igored (bootp?)";                  $audit->{type} = sprintf('ignored %x', $messagetype);
203          }          }
204    
205          warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n";          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
206            $audit->{response} = $packet;
207    
208          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
209          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
210    
211          my $reply = IO::Socket::INET->new(          if ( in_our_range( $ip ) ) {
212                  LocalAddr => $server::ip,                  my $buff = $packet->serialize();
213                  LocalPort => 67,  
214                  Proto => "udp",                  my $reply = IO::Socket::INET->new(
215                  Broadcast => 1,                          LocalAddr => $server::ip,
216                  PeerAddr => '255.255.255.255',                          LocalPort => 67,
217                  PeerPort => 68,                          Proto => "udp",
218                  Reuse => 1,                          Broadcast => 1,
219          ) or die "socket: $@";                          PeerAddr => '255.255.255.255',
220                            PeerPort => 68,
221                            Reuse => 1,
222                    ) or die "socket: $@";
223    
224          my $buff = $packet->serialize();                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
225          $reply->send( $buff, 0 ) or die "Error sending: $!\n";          } else {
226                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
227            }
228    
229            CouchDB::audit( $audit->{type}, $audit );
230    
231  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
232    
# Line 189  sub start { Line 248  sub start {
248    
249          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
250    
251            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
252    
253          while (1) {          while (1) {
                 Module::Refresh->refresh;  
254                  process_packet $sock;                  process_packet $sock;
255          }          }
256  }  }

Legend:
Removed from v.72  
changed lines
  Added in v.260

  ViewVC Help
Powered by ViewVC 1.1.26