3 # Copyright (c) 2007,2009 by Internet Systems Consortium, Inc. ("ISC")
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.
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.
17 # Internet Systems Consortium, Inc.
19 # Redwood City, CA 94063
21 # https://www.isc.org/
25 use Time::HiRes qw( sleep );
34 $Data::Dumper::Useqq = 1;
36 # not-yet-standard options
37 my $OPT_TIME_SERVERS = 40;
38 my $OPT_TIME_OFFSET = 41;
41 my $DOCSIS_OPT_ORO = 1;
42 # 2 to 31 are reserved
43 my $DOCSIS_OPT_TFTP_SERVERS = 32;
44 my $DOCSIS_OPT_CONFIG_FILE_NAME = 33;
45 my $DOCSIS_OPT_SYSLOG_SERVERS = 34;
46 my $DOCSIS_OPT_TLV5 = 35;
47 my $DOCSIS_OPT_DEVICE_ID = 36;
48 my $DOCSIS_OPT_CCC = 37;
49 my $DOCSIS_OPT_VERS = 38;
51 # well-known addresses
52 my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
53 my $All_DHCP_Servers = "ff05::1:3";
56 my $client_port = 546;
57 my $server_port = 547;
59 # create a new Solicit message
60 my $msg = dhcp_client::msg->new($MSG_SOLICIT);
62 # add the Client Identifier (required by DOCSIS and RFC 3315)
63 my $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));
68 # add Elapsed Time, set to 0 on first packet (required by RFC 3315)
69 $msg->add_option($OPT_ELAPSED_TIME, "\x00\x00");
71 # add IA_NA for each interface (required by DOCSIS and RFC 3315)
72 # XXX: should this be a single interface only?
74 foreach 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);
82 # add Reconfigure Accept (required by DOCSIS)
83 $msg->add_option($OPT_RECONF_ACCEPT, "");
85 # add Options Request (required by DOCSIS, recommended by RFC 3315)
86 my @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET );
87 $msg->add_option($OPT_ORO, pack("n*", @oro));
89 # add Vendor Class option (required by DOCSIS)
90 $msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
92 # add Vendor-specific Information Option option (required by DOCSIS)
93 my $vsio = pack("N", 4491);
95 # ORO (required by DOCSIS)
96 my @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS );
97 $vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro);
99 # TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS)
100 my $tlv5_data = "\x01\x02\x03\x0";
101 $vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data;
103 # DOCSIS Device (required by DOCSIS)
104 my $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;
108 $msg->add_option($OPT_VENDOR_OPTS, $vsio);
110 # add Rapid Commit option (required by DOCSIS)
111 #$msg->add_option($OPT_RAPID_COMMIT, "");
113 # timeout parameters, from DOCSIS
114 my $IRT = $SOL_TIMEOUT;
115 my $MRT = $SOL_MAX_RT;
116 my $MRC = 4; # DOCSIS says 4, RFC 3315 says it SHOULD be 0
119 # sleep a random amount of time between 0 and 1 second, required by RFC 3315
120 # XXX: this seems pretty stupid
121 sleep(rand($SOL_MAX_DELAY));
127 $mrd_end_time = time() + $MRD;
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)) {
139 "Error \%d sending DHCPv6 Solicit message;\n\%s\n",
142 } elsif ($send_ret != length($packet)) {
143 print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
148 my $RAND = rand(0.2) - 0.1;
150 $RT = 2*$RT + $RAND*$RT;
151 if (($RT > $MRT) && ($MRT != 0)) {
152 $RT = $MRT + $RAND*$RT;
155 $RT = $IRT + $RAND*$IRT;
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;
164 my $timeout = $rt_end_time - time();
166 # print STDERR "Timeout waiting for DHCPv6 Advertise ",
167 # "or Reply message.\n";
171 my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
177 $recv_ret = recv(SOCK, $reply, 1500, 0);
178 if (not defined $recv_ret) {
180 "Error \%d receiving DHCPv6 " .
186 $reply_msg = dhcp_client::msg::decode($reply, 1);
187 if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
188 ($reply_msg->{msg_type} == $MSG_REPLY)) {
194 } until ($reply_msg ||
195 (($MRC != 0) && ($count > $MRC)) ||
196 (defined($mrd_end_time) && ($mrd_end_time > time())));
198 unless ($reply_msg) {
199 if (($MRC != 0) && ($count >= $MRC)) {
201 "No reply after maximum retransmission count.\n";
204 "No reply after maximum retransmission duration.\n";
208 if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
209 print "Got DHCPv6 Reply message.\n";
210 #print Dumper($reply_msg), "\n";
214 #print Dumper($msg), "\n";
215 #print Dumper($msg->packet()), "\n";
217 #print "packet length: ", length($msg->packet()), "\n";