--- lib/PXElator/dhcpd.pm 2009/07/30 00:07:57 48 +++ lib/PXElator/dhcpd.pm 2010/01/02 17:20:40 457 @@ -18,17 +18,16 @@ use IO::Socket::INET; use File::Slurp; use Data::Dump qw/dump/; -use Net::Ping; -use Module::Refresh; use lib '..'; use Net::DHCP::Packet; use Net::DHCP::Constants 0.67; -use server; -use pxe; +use CouchDB; +use format; -my $debug = 1; +use server; +my $debug = server::debug; if ( ! $server::ip ) { my $server_ip = `/sbin/ifconfig`; @@ -36,46 +35,45 @@ $server::ip = $server_ip; } -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"; -my $addr = $server::ip_from; +use client; -sub client_ip { - my ( $mac ) = @_; +sub client_mac_ip { + my ( $mac, $request_ip ) = @_; - my $conf = "conf/$server::ip"; - mkdir $conf unless -e $conf; - - if ( -e "$conf/mac/$mac" ) { - my $ip = read_file "$conf/mac/$mac"; - print "$mac old $ip\n"; - return $ip; + if ( ! $mac ) { + warn "W: no mac in requiest\n"; + return; } - mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' ); - - my $p = Net::Ping->new; + my $conf = $server::conf; + mkdir $conf unless -e $conf; - my $prefix = $server::ip; - $prefix =~ s{\.\d+$}{.}; - my $ip = $prefix . $addr; - while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) { - $ip = $prefix . $addr++; - die "all addresses allocated!" if $addr == $server::ip_to; - } + my $ip; - write_file "$conf/mac/$mac", $ip; - if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") { - unlink "$conf/ip/$ip"; - symlink "$conf/mac/$mac", "$conf/ip/$ip"; - warn "$mac IP changed to $ip"; + if ( $ip = client::ip_from_mac( $mac ) ) { + print "RENEW $mac $ip\n"; + client::save_ip_mac( $ip, $mac ); + return $ip; + } elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) { + $ip = client::next_ip( $mac ); + print "NEW $mac $ip\n"; + } else { + $ip = $request_ip; + client::save_ip_mac( $ip, $mac ); + warn "W: $ip out of server range $server::ip/$server::netmask\n"; } - print "$mac NEW $ip\n"; - return $ip; } +use log; +use config; +use pxelinux; +use client; + +our $file; our $transaction = 0; # FIXME predictible transaction numbers sub process_packet { @@ -86,16 +84,28 @@ my $size = 'empty'; $size = length($buf) . ' bytes' if defined $buf; - print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n"; + print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug; return unless $buf; my $dhcp = Net::DHCP::Packet->new($buf); + + warn "recv: ", $dhcp->toString if $debug; + $dhcp->comment( $transaction++ ); - warn "recv: ", $dhcp->toString; + my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) ); + my $ip = client_mac_ip($mac, $dhcp->ciaddr); + + my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); + print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname; - my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); - my $ip = client_ip($mac); + my $audit = { mac => $mac, ip => $ip, hostname => $hostname, + options => { + map { + ( $_ => $dhcp->getOptionValue( $_ ) ) + } @{ $dhcp->{options_order} } + }, + }; =for later @@ -109,6 +119,10 @@ =cut + config::for_ip( $ip ); + + my $server = server::as_hash_for $ip; + my $packet = { Op => BOOTREPLY(), Hops => $dhcp->hops(), @@ -116,57 +130,107 @@ Flags => $dhcp->flags(), Ciaddr => $dhcp->ciaddr(), Yiaddr => $ip, - Siaddr => $server::ip, + Siaddr => $server->{ip}, Giaddr => $dhcp->giaddr(), Chaddr => $dhcp->chaddr(), - File => $pxe::file, + File => $file, + DHO_DHCP_SERVER_IDENTIFIER() => $server->{ip}, # busybox/udhcpc needs it but doesn't request + }; + + my $options = { + DHO_SUBNET_MASK() => $server->{netmask}, + DHO_ROUTERS() => $server->{ip}, + DHO_DOMAIN_NAME() => $server->{domain}, + DHO_NAME_SERVERS() => $server->{ip}, + DHO_DOMAIN_NAME_SERVERS() => $server->{ip}, + DHO_HOST_NAME() => client::conf( $ip, 'hostname' ), + DHO_BROADCAST_ADDRESS() => $server->{bcast}, +# DHO_NTP_SERVERS() => '', }; + my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST)); + warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug; + + my @missing; + foreach ( @requested ) { + if ( defined $options->{$_} ) { + $packet->{$_} = $options->{$_}; + } else { + push @missing, $_; + } + } + + warn "W: options requested but missing: ",dump( @missing ),$/; + $audit->{requested} = [ @requested ]; + $audit->{missing} = [ @missing ]; + + foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { + my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; + warn "DH0: $@" if $@; + my $v = eval "\$pxelinux::$opt"; + warn "v: $@" if $@; + next unless defined $v; + warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug; + $packet->{ $DH0 } = $v; + } + my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); + my @type; + if ($messagetype eq DHCPDISCOVER()) { - warn "DHCP DISCOVER"; $packet->{Comment} = $dhcp->comment(); $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); + @type = qw( discover offer ); } elsif ($messagetype eq DHCPREQUEST()) { - my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()); - warn "DHCP REQUEST $requested_ip"; + @type = qw( request ); + my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr(); if ( $ip eq $requested_ip ) { $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); $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; # $packet->{DHO_ROOT_PATH()} = '/exports/foobar'; + $type[1] = 'ack'; } else { $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK(); - $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip"; + $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip"; + $type[1] = 'nak'; } } elsif ($messagetype eq DHCPINFORM()) { - warn "DHCP INFORM ignored"; + @type = qw( inform ignored ); } else { - warn "$messagetype igored (bootp?)"; + @type = ( $messagetype, 'ignored' ); } - warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n"; + warn "# type ",dump @type; + $audit->{type} = [ @type ]; + + $audit->{response} = $packet; $packet = new Net::DHCP::Packet( %$packet ); - warn "## ",$packet->toString(),"\n" if $debug; + warn "send ",$packet->toString() if $debug; + + if ( ip::in_dhcp_range( $ip ) ) { + my $buff = $packet->serialize(); - my $reply = IO::Socket::INET->new( - LocalAddr => $server::ip, - LocalPort => 67, - Proto => "udp", - Broadcast => 1, - PeerAddr => '255.255.255.255', - PeerPort => 68, - Reuse => 1, - ) or die "socket: $@"; + my $reply = IO::Socket::INET->new( + LocalAddr => $server->{ip}, + LocalPort => 67, + Proto => "udp", + Broadcast => 1, +# PeerAddr => '255.255.255.255', + PeerAddr => $server->{bcast}, + PeerPort => 68, + Reuse => 1, + ) or die "socket: $@"; - my $buff = $packet->serialize(); - $reply->send( $buff, 0 ) or die "Error sending: $!\n"; + $reply->send( $buff, 0 ) or die "Error sending: $!\n"; + warn ">> $mac == $ip server: $server->{ip}", $file ? " file: $file\n" : "\n"; + } else { + $audit->{error} = "$ip not in server range $server::ip $server::netmask - no packet sent"; + warn $audit->{error}; + } + + CouchDB::audit( @type, $audit ); # system("arp -s $ip $mac"), @@ -188,8 +252,10 @@ print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; + CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } ); + while (1) { - Module::Refresh->refresh; + server->refresh; process_packet $sock; } }