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
35# not-yet-standard options
36my $OPT_TIME_SERVERS = 40;
37my $OPT_TIME_OFFSET = 41;
38
39# DOCSIS sub-options
40my $DOCSIS_OPT_ORO = 1;
41# 2 to 31 are reserved
42my $DOCSIS_OPT_TFTP_SERVERS = 32;
43my $DOCSIS_OPT_CONFIG_FILE_NAME = 33;
44my $DOCSIS_OPT_SYSLOG_SERVERS = 34;
45my $DOCSIS_OPT_TLV5 = 35;
46my $DOCSIS_OPT_DEVICE_ID = 36;
47my $DOCSIS_OPT_CCC = 37;
48my $DOCSIS_OPT_VERS = 38;
49
50# well-known addresses
51my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
52my $All_DHCP_Servers = "ff05::1:3";
53
54# ports
55my $client_port = 546;
56my $server_port = 547;
57
58# create a new Solicit message
59my $msg = dhcp_client::msg->new($MSG_REQUEST);
60
61# add the Client Identifier (required by DOCSIS and RFC 3315)
62$msg->add_option($OPT_CLIENTID, dhcp_client::duid());
63
64# add the Server Identifier (required by DOCSIS and RFC 3315)
65# but use *our* DUID
66$msg->add_option($OPT_SERVERID, dhcp_client::duid());
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	$msg->add_option($OPT_IA_NA, $option_data);
77}
78
79# add Reconfigure Accept (required by DOCSIS)
80$msg->add_option($OPT_RECONF_ACCEPT, "");
81
82# add Options Request (required by DOCSIS, recommended by RFC 3315)
83my @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET );
84$msg->add_option($OPT_ORO, pack("n*", @oro));
85
86
87# add Vendor Class option (required by DOCSIS)
88$msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
89
90# add Vendor-specific Information Option option (required by DOCSIS)
91my $vsio = pack("N", 4491);
92
93# ORO (required by DOCSIS)
94my @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS );
95$vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro);
96
97# TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS)
98my $tlv5_data = "\x01\x02\x03\x0";
99$vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data;
100
101# DOCSIS Device (required by DOCSIS)
102my $docsis_device_id = dhcp_client::mac_addr_binary();
103$vsio .= pack("nn", $DOCSIS_OPT_DEVICE_ID, length($docsis_device_id));
104$vsio .= $docsis_device_id;
105
106$msg->add_option($OPT_VENDOR_OPTS, $vsio);
107
108# add Rapid Commit option (required by DOCSIS)
109$msg->add_option($OPT_RAPID_COMMIT, "");
110
111# timeout parameters, from DOCSIS
112my $IRT = $SOL_TIMEOUT;
113my $MRT = $SOL_MAX_RT;
114my $MRC = 1;	# DOCSIS says 4, RFC 3315 says it SHOULD be 0
115my $MRD = 0;
116
117# sleep a random amount of time between 0 and 1 second, required by RFC 3315
118# XXX: this seems pretty stupid
119sleep(rand($SOL_MAX_DELAY));
120
121my $RT;
122my $count = 0;
123my $mrd_end_time;
124if ($MRD != 0) {
125	$mrd_end_time = time() + $MRD;
126}
127my $reply_msg;
128do {
129	# create our socket, and send our Solicit
130	socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
131	my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
132	my $packet = $msg->packet();
133	my $send_ret = send(SOCK, $packet, 0,
134			    pack_sockaddr_in6($server_port, $addr));
135	if (not defined($send_ret)) {
136		printf STDERR
137			"Error \%d sending DHCPv6 Solicit message;\n\%s\n",
138			0+$ERRNO, $ERRNO;
139		exit(1);
140	} elsif ($send_ret != length($packet)) {
141		print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
142		exit(1);
143	}
144	$count++;
145
146	my $RAND = rand(0.2) - 0.1;
147	if (defined $RT) {
148		$RT = 2*$RT + $RAND*$RT;
149		if (($RT > $MRT) && ($MRT != 0)) {
150			$RT = $MRT + $RAND*$RT;
151		}
152	} else {
153		$RT = $IRT + $RAND*$IRT;
154	}
155
156	my $rt_end_time = time() + $RT;
157	if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) {
158		$rt_end_time = $mrd_end_time;
159	}
160
161	for (;;) {
162		my $timeout = $rt_end_time - time();
163		if ($timeout < 0) {
164#			print STDERR "Timeout waiting for DHCPv6 Advertise ",
165#				"or Reply message.\n";
166			last;
167		}
168
169		my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
170
171		if (@ready) {
172			my $reply;
173			my $recv_ret;
174
175			$recv_ret = recv(SOCK, $reply, 1500, 0);
176			if (not defined $recv_ret) {
177				printf STDERR
178					"Error \%d receiving DHCPv6 " .
179						"message;\n\%s\n",
180					0+$ERRNO, $ERRNO;
181				exit(1);
182			}
183
184			$reply_msg = dhcp_client::msg::decode($reply);
185			if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
186			    ($reply_msg->{msg_type} == $MSG_REPLY)) {
187			    	last;
188			}
189		}
190	}
191
192} until ($reply_msg ||
193	 (($MRC != 0) && ($count > $MRC)) ||
194	 (defined($mrd_end_time) && ($mrd_end_time > time())));
195
196unless ($reply_msg) {
197	if (($MRC != 0) && ($count >= $MRC)) {
198		print STDERR
199			"No reply after maximum retransmission count.\n";
200	} else {
201		print STDERR
202			"No reply after maximum retransmission duration.\n";
203	}
204}
205
206if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
207	print "Got DHCPv6 Reply message.\n";
208	exit(0);
209}
210
211#$Data::Dumper::Useqq = 1;
212#print Dumper($msg), "\n";
213#print Dumper($msg->packet()), "\n";
214#
215#print "packet length: ", length($msg->packet()), "\n";
216
217