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 message
59my $msg = dhcp_client::msg->new($MSG_RELEASE);
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 RFC 3315)
65$msg->add_option($OPT_SERVERID, "InfiniteEntropy");
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# timeout parameters, from DOCSIS
71my $IRT = $SOL_TIMEOUT;
72my $MRT = $SOL_MAX_RT;
73my $MRC = 4;	# DOCSIS says 4, RFC 3315 says it SHOULD be 0
74my $MRD = 0;
75
76# sleep a random amount of time between 0 and 1 second, required by RFC 3315
77# XXX: this seems pretty stupid
78sleep(rand($SOL_MAX_DELAY));
79
80my $RT;
81my $count = 0;
82my $mrd_end_time;
83if ($MRD != 0) {
84	$mrd_end_time = time() + $MRD;
85}
86my $reply_msg;
87do {
88	# create our socket, and send our message
89	socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
90	my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
91	my $packet = $msg->packet();
92	my $send_ret = send(SOCK, $packet, 0,
93			    pack_sockaddr_in6($server_port, $addr));
94	if (not defined($send_ret)) {
95		printf STDERR
96			"Error \%d sending DHCPv6 message;\n\%s\n",
97			0+$ERRNO, $ERRNO;
98		exit(1);
99	} elsif ($send_ret != length($packet)) {
100		print STDERR "Unable to send entire DHCPv6 message.\n";
101		exit(1);
102	}
103	$count++;
104
105	my $RAND = rand(0.2) - 0.1;
106	if (defined $RT) {
107		$RT = 2*$RT + $RAND*$RT;
108		if (($RT > $MRT) && ($MRT != 0)) {
109			$RT = $MRT + $RAND*$RT;
110		}
111	} else {
112		$RT = $IRT + $RAND*$IRT;
113	}
114
115	my $rt_end_time = time() + $RT;
116	if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) {
117		$rt_end_time = $mrd_end_time;
118	}
119
120	for (;;) {
121		my $timeout = $rt_end_time - time();
122		if ($timeout < 0) {
123#			print STDERR "Timeout waiting for DHCPv6 Advertise ",
124#				"or Reply message.\n";
125			last;
126		}
127
128		my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
129
130		if (@ready) {
131			my $reply;
132			my $recv_ret;
133
134			$recv_ret = recv(SOCK, $reply, 1500, 0);
135			if (not defined $recv_ret) {
136				printf STDERR
137					"Error \%d receiving DHCPv6 " .
138						"message;\n\%s\n",
139					0+$ERRNO, $ERRNO;
140				exit(1);
141			}
142
143			$reply_msg = dhcp_client::msg::decode($reply, 1);
144			if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
145			    ($reply_msg->{msg_type} == $MSG_REPLY)) {
146			    	last;
147			}
148		}
149	}
150
151} until ($reply_msg ||
152	 (($MRC != 0) && ($count > $MRC)) ||
153	 (defined($mrd_end_time) && ($mrd_end_time > time())));
154
155unless ($reply_msg) {
156	if (($MRC != 0) && ($count >= $MRC)) {
157		print STDERR
158			"No reply after maximum retransmission count.\n";
159	} else {
160		print STDERR
161			"No reply after maximum retransmission duration.\n";
162	}
163}
164
165if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
166	print "Got DHCPv6 Reply message.\n";
167	exit(0);
168}
169
170#$Data::Dumper::Useqq = 1;
171#print Dumper($msg), "\n";
172#print Dumper($msg->packet()), "\n";
173#
174#print "packet length: ", length($msg->packet()), "\n";
175
176