1#! /usr/bin/perl -w 2 3# Copyright (c) 2007,2009 by Internet Systems Consortium, Inc. ("ISC") 4# 5# Permission to use, copy, modify, and distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16# 17# Internet Systems Consortium, Inc. 18# 950 Charter Street 19# Redwood City, CA 94063 20# <info@isc.org> 21# https://www.isc.org/ 22 23use strict; 24use English; 25use Time::HiRes qw( sleep ); 26use Socket; 27use Socket6; 28use IO::Select; 29 30use dhcp_client; 31 32# XXX: for debugging 33use Data::Dumper; 34$Data::Dumper::Useqq = 1; 35 36# not-yet-standard options 37my $OPT_TIME_SERVERS = 40; 38my $OPT_TIME_OFFSET = 41; 39 40# DOCSIS sub-options 41my $DOCSIS_OPT_ORO = 1; 42# 2 to 31 are reserved 43my $DOCSIS_OPT_TFTP_SERVERS = 32; 44my $DOCSIS_OPT_CONFIG_FILE_NAME = 33; 45my $DOCSIS_OPT_SYSLOG_SERVERS = 34; 46my $DOCSIS_OPT_TLV5 = 35; 47my $DOCSIS_OPT_DEVICE_ID = 36; 48my $DOCSIS_OPT_CCC = 37; 49my $DOCSIS_OPT_VERS = 38; 50 51# well-known addresses 52my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2"; 53my $All_DHCP_Servers = "ff05::1:3"; 54 55# ports 56my $client_port = 546; 57my $server_port = 547; 58 59# create a new Solicit message 60my $msg = dhcp_client::msg->new($MSG_SOLICIT); 61 62# add the Client Identifier (required by DOCSIS and RFC 3315) 63my $client_id = "\x00\x01\x00\x01\x0c\x00\xa1\x41\x00\x06\x5b\x50\x99\xf6"; 64#my $client_id = dhcp_client::duid(3); 65$msg->add_option($OPT_CLIENTID, $client_id); 66#$msg->add_option($OPT_CLIENTID, dhcp_client::duid(3)); 67 68# add Elapsed Time, set to 0 on first packet (required by RFC 3315) 69$msg->add_option($OPT_ELAPSED_TIME, "\x00\x00"); 70 71# add IA_NA for each interface (required by DOCSIS and RFC 3315) 72# XXX: should this be a single interface only? 73my $iaid = 0; 74foreach my $iface (dhcp_client::iface()) { 75 my $option_data = pack("NNN", ++$iaid, 0, 0); 76 my $enc_opt = dhcp_client::msg->new(0); 77 $enc_opt->add_option($OPT_CLIENTID, $client_id); 78 $option_data .= $enc_opt->packed_options(); 79 $msg->add_option($OPT_IA_NA, $option_data); 80} 81 82# add Reconfigure Accept (required by DOCSIS) 83$msg->add_option($OPT_RECONF_ACCEPT, ""); 84 85# add Options Request (required by DOCSIS, recommended by RFC 3315) 86my @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET ); 87$msg->add_option($OPT_ORO, pack("n*", @oro)); 88 89# add Vendor Class option (required by DOCSIS) 90$msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0"); 91 92# add Vendor-specific Information Option option (required by DOCSIS) 93my $vsio = pack("N", 4491); 94 95# ORO (required by DOCSIS) 96my @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS ); 97$vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro); 98 99# TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS) 100my $tlv5_data = "\x01\x02\x03\x0"; 101$vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data; 102 103# DOCSIS Device (required by DOCSIS) 104my $docsis_device_id = dhcp_client::mac_addr_binary(); 105$vsio .= pack("nn", $DOCSIS_OPT_DEVICE_ID, length($docsis_device_id)); 106$vsio .= $docsis_device_id; 107 108$msg->add_option($OPT_VENDOR_OPTS, $vsio); 109 110# add Rapid Commit option (required by DOCSIS) 111$msg->add_option($OPT_RAPID_COMMIT, ""); 112 113# timeout parameters, from DOCSIS 114my $IRT = $SOL_TIMEOUT; 115my $MRT = $SOL_MAX_RT; 116my $MRC = 4; # DOCSIS says 4, RFC 3315 says it SHOULD be 0 117my $MRD = 0; 118 119# sleep a random amount of time between 0 and 1 second, required by RFC 3315 120# XXX: this seems pretty stupid 121sleep(rand($SOL_MAX_DELAY)); 122 123my $RT; 124my $count = 0; 125my $mrd_end_time; 126if ($MRD != 0) { 127 $mrd_end_time = time() + $MRD; 128} 129my $reply_msg; 130do { 131 # create our socket, and send our Solicit 132 socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die; 133 my $addr = inet_pton(AF_INET6, $All_DHCP_Servers); 134 my $packet = $msg->packet(); 135 my $send_ret = send(SOCK, $packet, 0, 136 pack_sockaddr_in6($server_port, $addr)); 137 if (not defined($send_ret)) { 138 printf STDERR 139 "Error \%d sending DHCPv6 Solicit message;\n\%s\n", 140 0+$ERRNO, $ERRNO; 141 exit(1); 142 } elsif ($send_ret != length($packet)) { 143 print STDERR "Unable to send entire DHCPv6 Solicit message.\n"; 144 exit(1); 145 } 146 $count++; 147 148 my $RAND = rand(0.2) - 0.1; 149 if (defined $RT) { 150 $RT = 2*$RT + $RAND*$RT; 151 if (($RT > $MRT) && ($MRT != 0)) { 152 $RT = $MRT + $RAND*$RT; 153 } 154 } else { 155 $RT = $IRT + $RAND*$IRT; 156 } 157 158 my $rt_end_time = time() + $RT; 159 if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) { 160 $rt_end_time = $mrd_end_time; 161 } 162 163 for (;;) { 164 my $timeout = $rt_end_time - time(); 165 if ($timeout < 0) { 166# print STDERR "Timeout waiting for DHCPv6 Advertise ", 167# "or Reply message.\n"; 168 last; 169 } 170 171 my @ready = IO::Select->new(\*SOCK)->can_read($timeout); 172 173 if (@ready) { 174 my $reply; 175 my $recv_ret; 176 177 $recv_ret = recv(SOCK, $reply, 1500, 0); 178 if (not defined $recv_ret) { 179 printf STDERR 180 "Error \%d receiving DHCPv6 " . 181 "message;\n\%s\n", 182 0+$ERRNO, $ERRNO; 183 exit(1); 184 } 185 186 $reply_msg = dhcp_client::msg::decode($reply, 1); 187 if (($reply_msg->{msg_type} == $MSG_ADVERTISE) || 188 ($reply_msg->{msg_type} == $MSG_REPLY)) { 189 last; 190 } 191 } 192 } 193 194} until ($reply_msg || 195 (($MRC != 0) && ($count > $MRC)) || 196 (defined($mrd_end_time) && ($mrd_end_time > time()))); 197 198unless ($reply_msg) { 199 if (($MRC != 0) && ($count >= $MRC)) { 200 print STDERR 201 "No reply after maximum retransmission count.\n"; 202 } else { 203 print STDERR 204 "No reply after maximum retransmission duration.\n"; 205 } 206} 207 208if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) { 209 print "Got DHCPv6 Reply message.\n"; 210#print Dumper($reply_msg), "\n"; 211 exit(0); 212} 213 214#print Dumper($msg), "\n"; 215#print Dumper($msg->packet()), "\n"; 216# 217#print "packet length: ", length($msg->packet()), "\n"; 218 219