7 perl -Ilib/PXElator -Ilib -Mdhcpd -e start
9 based on L<http://www.perlmonks.org/index.pl?node_id=325248>
20 use Data::Dump qw/dump/;
23 use Net::DHCP::Packet;
24 use Net::DHCP::Constants 0.67;
30 my $debug = server::debug;
32 if ( ! $server::ip ) {
33 my $server_ip = `/sbin/ifconfig`;
34 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
35 $server::ip = $server_ip;
38 warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
43 my ( $mac, $request_ip ) = @_;
46 warn "W: no mac in requiest\n";
50 my $conf = $server::conf;
51 mkdir $conf unless -e $conf;
55 if ( $ip = client::ip_from_mac( $mac ) ) {
56 print "RENEW $mac $ip\n";
57 client::save_ip_mac( $ip, $mac );
59 } elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) {
60 $ip = client::next_ip( $mac );
61 print "NEW $mac $ip\n";
64 client::save_ip_mac( $ip, $mac );
65 warn "W: $ip our of server range $server::ip $server::netmask\n";
77 our $transaction = 0; # FIXME predictible transaction numbers
85 $sock->recv($buf, 1024);
87 $size = length($buf) . ' bytes' if defined $buf;
89 print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
92 my $dhcp = Net::DHCP::Packet->new($buf);
94 warn "recv: ", $dhcp->toString if $debug;
96 $dhcp->comment( $transaction++ );
98 my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
99 my $ip = client_mac_ip($mac, $dhcp->ciaddr);
101 my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
102 print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
104 my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
107 ( $_ => $dhcp->getOptionValue( $_ ) )
108 } @{ $dhcp->{options_order} }
114 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
116 if ( $user_class eq 'gPXE' ) {
118 } elsif ( ! $file ) {
119 $file = 'undionly.kpxe';
124 config::for_ip( $ip );
128 Hops => $dhcp->hops(),
130 Flags => $dhcp->flags(),
131 Ciaddr => $dhcp->ciaddr(),
133 Siaddr => $server::ip,
134 Giaddr => $dhcp->giaddr(),
135 Chaddr => $dhcp->chaddr(),
137 DHO_DHCP_SERVER_IDENTIFIER() => $server::ip, # busybox/udhcpc needs it but doesn't request
141 DHO_SUBNET_MASK() => $server::netmask,
142 DHO_ROUTERS() => $server::ip,
143 DHO_DOMAIN_NAME() => $server::domain,
144 DHO_NAME_SERVERS() => $server::ip,
145 DHO_DOMAIN_NAME_SERVERS() => $server::ip,
146 DHO_HOST_NAME() => client::conf( $ip, 'hostname' ),
147 DHO_BROADCAST_ADDRESS() => $server::bcast,
148 # DHO_NTP_SERVERS() => '',
151 my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
152 warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
155 foreach ( @requested ) {
156 if ( defined $options->{$_} ) {
157 $packet->{$_} = $options->{$_};
163 warn "W: options requested but missing: ",dump( @missing ),$/;
164 $audit->{requested} = [ @requested ];
165 $audit->{missing} = [ @missing ];
167 foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
168 my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
169 warn "DH0: $@" if $@;
170 my $v = eval "\$pxelinux::$opt";
172 next unless defined $v;
173 warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
174 $packet->{ $DH0 } = $v;
177 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
181 if ($messagetype eq DHCPDISCOVER()) {
182 $packet->{Comment} = $dhcp->comment();
183 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
184 @type = qw( discover offer );
185 } elsif ($messagetype eq DHCPREQUEST()) {
186 @type = qw( request );
187 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
188 if ( $ip eq $requested_ip ) {
189 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
190 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
191 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
194 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
195 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
198 } elsif ($messagetype eq DHCPINFORM()) {
199 @type = qw( inform ignored );
201 @type = ( $messagetype, 'ignored' );
204 warn "# type ",dump @type;
205 $audit->{type} = [ @type ];
207 $audit->{response} = $packet;
209 $packet = new Net::DHCP::Packet( %$packet );
210 warn "send ",$packet->toString() if $debug;
212 if ( ip::in_dhcp_range( $ip ) ) {
213 my $buff = $packet->serialize();
215 my $reply = IO::Socket::INET->new(
216 LocalAddr => $server::ip,
220 PeerAddr => '255.255.255.255',
223 ) or die "socket: $@";
225 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
226 warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n";
228 $audit->{error} = "$ip our of our range $server::ip $server::netmask";
229 warn $audit->{error};
232 CouchDB::audit( @type, $audit );
234 # system("arp -s $ip $mac"),
240 my $sock = IO::Socket::INET->new(
242 # LocalAddr => 'localhost',
243 # LocalAddr => '10.0.0.100',
244 LocalAddr => '0.0.0.0',
247 # PeerPort => getservbyname('bootpc', 'udp'),
250 ) or die "Failed to bind to socket: $@";
252 print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
254 CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
257 process_packet $sock;