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$msg->add_option($OPT_CLIENTID, $client_id);
65#$msg->add_option($OPT_CLIENTID, dhcp_client::duid());
66
67# add Elapsed Time, set to 0 on first packet (required by RFC 3315)
68$msg->add_option($OPT_ELAPSED_TIME, "\x00\x00");
69
70# add IA_NA for each interface (required by DOCSIS and RFC 3315)
71# XXX: should this be a single interface only?
72my $iaid = 0;
73foreach my $iface (dhcp_client::iface()) {
74	my $option_data = pack("NNN", ++$iaid, 0, 0);
75	my $enc_opt = dhcp_client::msg->new(0);
76	$enc_opt->add_option($OPT_CLIENTID, $client_id);
77	$option_data .= $enc_opt->packed_options();
78	$msg->add_option($OPT_IA_NA, $option_data);
79}
80
81# add Reconfigure Accept (required by DOCSIS)
82$msg->add_option($OPT_RECONF_ACCEPT, "");
83
84# add Options Request (required by DOCSIS, recommended by RFC 3315)
85my @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET );
86$msg->add_option($OPT_ORO, pack("n*", @oro));
87
88# add Vendor Class option (required by DOCSIS)
89$msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
90
91# add Vendor-specific Information Option option (required by DOCSIS)
92my $vsio = pack("N", 4491);
93
94# ORO (required by DOCSIS)
95my @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS );
96$vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro);
97
98# TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS)
99my $tlv5_data = "\x01\x02\x03\x0";
100$vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data;
101
102# DOCSIS Device (required by DOCSIS)
103my $docsis_device_id = dhcp_client::mac_addr_binary();
104$vsio .= pack("nn", $DOCSIS_OPT_DEVICE_ID, length($docsis_device_id));
105$vsio .= $docsis_device_id;
106
107$msg->add_option($OPT_VENDOR_OPTS, $vsio);
108
109# add Rapid Commit option (required by DOCSIS)
110$msg->add_option($OPT_RAPID_COMMIT, "");
111
112# timeout parameters, from DOCSIS
113my $IRT = $SOL_TIMEOUT;
114my $MRT = $SOL_MAX_RT;
115my $MRC = 4;	# DOCSIS says 4, RFC 3315 says it SHOULD be 0
116my $MRD = 0;
117
118# sleep a random amount of time between 0 and 1 second, required by RFC 3315
119# XXX: this seems pretty stupid
120sleep(rand($SOL_MAX_DELAY));
121
122my $RT;
123my $count = 0;
124my $mrd_end_time;
125if ($MRD != 0) {
126	$mrd_end_time = time() + $MRD;
127}
128my $reply_msg;
129do {
130	# create our socket, and send our Solicit
131	socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
132	my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
133	my $packet = $msg->packet();
134	my $send_ret = send(SOCK, $packet, 0,
135			    pack_sockaddr_in6($server_port, $addr));
136	if (not defined($send_ret)) {
137		printf STDERR
138			"Error \%d sending DHCPv6 Solicit message;\n\%s\n",
139			0+$ERRNO, $ERRNO;
140		exit(1);
141	} elsif ($send_ret != length($packet)) {
142		print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
143		exit(1);
144	}
145	$count++;
146
147	my $RAND = rand(0.2) - 0.1;
148	if (defined $RT) {
149		$RT = 2*$RT + $RAND*$RT;
150		if (($RT > $MRT) && ($MRT != 0)) {
151			$RT = $MRT + $RAND*$RT;
152		}
153	} else {
154		$RT = $IRT + $RAND*$IRT;
155	}
156
157	my $rt_end_time = time() + $RT;
158	if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) {
159		$rt_end_time = $mrd_end_time;
160	}
161
162	for (;;) {
163		my $timeout = $rt_end_time - time();
164		if ($timeout < 0) {
165#			print STDERR "Timeout waiting for DHCPv6 Advertise ",
166#				"or Reply message.\n";
167			last;
168		}
169
170		my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
171
172		if (@ready) {
173			my $reply;
174			my $recv_ret;
175
176			$recv_ret = recv(SOCK, $reply, 1500, 0);
177			if (not defined $recv_ret) {
178				printf STDERR
179					"Error \%d receiving DHCPv6 " .
180						"message;\n\%s\n",
181					0+$ERRNO, $ERRNO;
182				exit(1);
183			}
184
185			$reply_msg = dhcp_client::msg::decode($reply, 1);
186			if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
187			    ($reply_msg->{msg_type} == $MSG_REPLY)) {
188			    	last;
189			}
190		}
191	}
192
193} until ($reply_msg ||
194	 (($MRC != 0) && ($count > $MRC)) ||
195	 (defined($mrd_end_time) && ($mrd_end_time > time())));
196
197unless ($reply_msg) {
198	if (($MRC != 0) && ($count >= $MRC)) {
199		print STDERR
200			"No reply after maximum retransmission count.\n";
201	} else {
202		print STDERR
203			"No reply after maximum retransmission duration.\n";
204	}
205}
206
207if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
208	print "Got DHCPv6 Reply message.\n";
209#print Dumper($reply_msg), "\n";
210	exit(0);
211}
212
213#print Dumper($msg), "\n";
214#print Dumper($msg->packet()), "\n";
215#
216#print "packet length: ", length($msg->packet()), "\n";
217
218