13965be93Schristos#! /usr/bin/perl -w
23965be93Schristos
3*f5e96ebaSchristos# Copyright (C) 2007-2022 Internet Systems Consortium, Inc. ("ISC")
43965be93Schristos#
53965be93Schristos# Permission to use, copy, modify, and distribute this software for any
63965be93Schristos# purpose with or without fee is hereby granted, provided that the above
73965be93Schristos# copyright notice and this permission notice appear in all copies.
83965be93Schristos#
93965be93Schristos# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
103965be93Schristos# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
113965be93Schristos# MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
123965be93Schristos# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
133965be93Schristos# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
143965be93Schristos# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
153965be93Schristos# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
163965be93Schristos#
173965be93Schristos#   Internet Systems Consortium, Inc.
18*f5e96ebaSchristos#   PO Box 360
19*f5e96ebaSchristos#   Newmarket, NH 03857 USA
203965be93Schristos#   <info@isc.org>
213965be93Schristos#   https://www.isc.org/
223965be93Schristos
233965be93Schristosuse strict;
243965be93Schristosuse English;
253965be93Schristosuse Time::HiRes qw( sleep );
263965be93Schristosuse Socket;
273965be93Schristosuse Socket6;
283965be93Schristosuse IO::Select;
293965be93Schristos
303965be93Schristosuse dhcp_client;
313965be93Schristos
323965be93Schristos# XXX: for debugging
333965be93Schristosuse Data::Dumper;
343965be93Schristos
353965be93Schristos# not-yet-standard options
363965be93Schristosmy $OPT_TIME_SERVERS = 40;
373965be93Schristosmy $OPT_TIME_OFFSET = 41;
383965be93Schristos
393965be93Schristos# DOCSIS sub-options
403965be93Schristosmy $DOCSIS_OPT_ORO = 1;
413965be93Schristos# 2 to 31 are reserved
423965be93Schristosmy $DOCSIS_OPT_TFTP_SERVERS = 32;
433965be93Schristosmy $DOCSIS_OPT_CONFIG_FILE_NAME = 33;
443965be93Schristosmy $DOCSIS_OPT_SYSLOG_SERVERS = 34;
453965be93Schristosmy $DOCSIS_OPT_TLV5 = 35;
463965be93Schristosmy $DOCSIS_OPT_DEVICE_ID = 36;
473965be93Schristosmy $DOCSIS_OPT_CCC = 37;
483965be93Schristosmy $DOCSIS_OPT_VERS = 38;
493965be93Schristos
503965be93Schristos# well-known addresses
513965be93Schristosmy $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
523965be93Schristosmy $All_DHCP_Servers = "ff05::1:3";
533965be93Schristos
543965be93Schristos# ports
553965be93Schristosmy $client_port = 546;
563965be93Schristosmy $server_port = 547;
573965be93Schristos
583965be93Schristos# create a new Solicit message
593965be93Schristosmy $msg = dhcp_client::msg->new($MSG_REQUEST);
603965be93Schristos
613965be93Schristos# add the Client Identifier (required by DOCSIS and RFC 3315)
623965be93Schristos$msg->add_option($OPT_CLIENTID, dhcp_client::duid());
633965be93Schristos
643965be93Schristos# add Elapsed Time, set to 0 on first packet (required by RFC 3315)
653965be93Schristos$msg->add_option($OPT_ELAPSED_TIME, "\x00\x00");
663965be93Schristos
673965be93Schristos# add IA_NA for each interface (required by DOCSIS and RFC 3315)
683965be93Schristos# XXX: should this be a single interface only?
693965be93Schristosmy $iaid = 0;
703965be93Schristosforeach my $iface (dhcp_client::iface()) {
713965be93Schristos	my $option_data = pack("NNN", ++$iaid, 0, 0);
723965be93Schristos	$msg->add_option($OPT_IA_NA, $option_data);
733965be93Schristos}
743965be93Schristos
753965be93Schristos# add Reconfigure Accept (required by DOCSIS)
763965be93Schristos$msg->add_option($OPT_RECONF_ACCEPT, "");
773965be93Schristos
783965be93Schristos# add Options Request (required by DOCSIS, recommended by RFC 3315)
793965be93Schristosmy @oro = ( $OPT_TIME_SERVERS, $OPT_TIME_OFFSET );
803965be93Schristos$msg->add_option($OPT_ORO, pack("n*", @oro));
813965be93Schristos
823965be93Schristos
833965be93Schristos# add Vendor Class option (required by DOCSIS)
843965be93Schristos$msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
853965be93Schristos
863965be93Schristos# add Vendor-specific Information Option option (required by DOCSIS)
873965be93Schristosmy $vsio = pack("N", 4491);
883965be93Schristos
893965be93Schristos# ORO (required by DOCSIS)
903965be93Schristosmy @docsis_oro = ( $DOCSIS_OPT_TFTP_SERVERS );
913965be93Schristos$vsio .= pack("nnC*", $DOCSIS_OPT_ORO, 0+@docsis_oro, @docsis_oro);
923965be93Schristos
933965be93Schristos# TLV5 data: CMTS DOCSIS version number 3.0 (required by DOCSIS)
943965be93Schristosmy $tlv5_data = "\x01\x02\x03\x0";
953965be93Schristos$vsio .= pack("nn", $DOCSIS_OPT_TLV5, length($tlv5_data)) . $tlv5_data;
963965be93Schristos
973965be93Schristos# DOCSIS Device (required by DOCSIS)
983965be93Schristosmy $docsis_device_id = dhcp_client::mac_addr_binary();
993965be93Schristos$vsio .= pack("nn", $DOCSIS_OPT_DEVICE_ID, length($docsis_device_id));
1003965be93Schristos$vsio .= $docsis_device_id;
1013965be93Schristos
1023965be93Schristos$msg->add_option($OPT_VENDOR_OPTS, $vsio);
1033965be93Schristos
1043965be93Schristos# add Rapid Commit option (required by DOCSIS)
1053965be93Schristos$msg->add_option($OPT_RAPID_COMMIT, "");
1063965be93Schristos
1073965be93Schristos# timeout parameters, from DOCSIS
1083965be93Schristosmy $IRT = $SOL_TIMEOUT;
1093965be93Schristosmy $MRT = $SOL_MAX_RT;
1103965be93Schristosmy $MRC = 1;	# DOCSIS says 4, RFC 3315 says it SHOULD be 0
1113965be93Schristosmy $MRD = 0;
1123965be93Schristos
1133965be93Schristos# sleep a random amount of time between 0 and 1 second, required by RFC 3315
1143965be93Schristos# XXX: this seems pretty stupid
1153965be93Schristossleep(rand($SOL_MAX_DELAY));
1163965be93Schristos
1173965be93Schristosmy $RT;
1183965be93Schristosmy $count = 0;
1193965be93Schristosmy $mrd_end_time;
1203965be93Schristosif ($MRD != 0) {
1213965be93Schristos	$mrd_end_time = time() + $MRD;
1223965be93Schristos}
1233965be93Schristosmy $reply_msg;
1243965be93Schristosdo {
1253965be93Schristos	# create our socket, and send our Solicit
1263965be93Schristos	socket(SOCK, PF_INET6, SOCK_DGRAM, getprotobyname('udp')) || die;
1273965be93Schristos	my $addr = inet_pton(AF_INET6, $All_DHCP_Servers);
1283965be93Schristos	my $packet = $msg->packet();
1293965be93Schristos	my $send_ret = send(SOCK, $packet, 0,
1303965be93Schristos			    pack_sockaddr_in6($server_port, $addr));
1313965be93Schristos	if (not defined($send_ret)) {
1323965be93Schristos		printf STDERR
1333965be93Schristos			"Error \%d sending DHCPv6 Solicit message;\n\%s\n",
1343965be93Schristos			0+$ERRNO, $ERRNO;
1353965be93Schristos		exit(1);
1363965be93Schristos	} elsif ($send_ret != length($packet)) {
1373965be93Schristos		print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
1383965be93Schristos		exit(1);
1393965be93Schristos	}
1403965be93Schristos	$count++;
1413965be93Schristos
1423965be93Schristos	my $RAND = rand(0.2) - 0.1;
1433965be93Schristos	if (defined $RT) {
1443965be93Schristos		$RT = 2*$RT + $RAND*$RT;
1453965be93Schristos		if (($RT > $MRT) && ($MRT != 0)) {
1463965be93Schristos			$RT = $MRT + $RAND*$RT;
1473965be93Schristos		}
1483965be93Schristos	} else {
1493965be93Schristos		$RT = $IRT + $RAND*$IRT;
1503965be93Schristos	}
1513965be93Schristos
1523965be93Schristos	my $rt_end_time = time() + $RT;
1533965be93Schristos	if (defined($mrd_end_time) && ($mrd_end_time > $rt_end_time)) {
1543965be93Schristos		$rt_end_time = $mrd_end_time;
1553965be93Schristos	}
1563965be93Schristos
1573965be93Schristos	for (;;) {
1583965be93Schristos		my $timeout = $rt_end_time - time();
1593965be93Schristos		if ($timeout < 0) {
1603965be93Schristos#			print STDERR "Timeout waiting for DHCPv6 Advertise ",
1613965be93Schristos#				"or Reply message.\n";
1623965be93Schristos			last;
1633965be93Schristos		}
1643965be93Schristos
1653965be93Schristos		my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
1663965be93Schristos
1673965be93Schristos		if (@ready) {
1683965be93Schristos			my $reply;
1693965be93Schristos			my $recv_ret;
1703965be93Schristos
1713965be93Schristos			$recv_ret = recv(SOCK, $reply, 1500, 0);
1723965be93Schristos			if (not defined $recv_ret) {
1733965be93Schristos				printf STDERR
1743965be93Schristos					"Error \%d receiving DHCPv6 " .
1753965be93Schristos						"message;\n\%s\n",
1763965be93Schristos					0+$ERRNO, $ERRNO;
1773965be93Schristos				exit(1);
1783965be93Schristos			}
1793965be93Schristos
1803965be93Schristos			$reply_msg = dhcp_client::msg::decode($reply);
1813965be93Schristos			if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
1823965be93Schristos			    ($reply_msg->{msg_type} == $MSG_REPLY)) {
1833965be93Schristos			    	last;
1843965be93Schristos			}
1853965be93Schristos		}
1863965be93Schristos	}
1873965be93Schristos
1883965be93Schristos} until ($reply_msg ||
1893965be93Schristos	 (($MRC != 0) && ($count > $MRC)) ||
1903965be93Schristos	 (defined($mrd_end_time) && ($mrd_end_time > time())));
1913965be93Schristos
1923965be93Schristosunless ($reply_msg) {
1933965be93Schristos	if (($MRC != 0) && ($count >= $MRC)) {
1943965be93Schristos		print STDERR
1953965be93Schristos			"No reply after maximum retransmission count.\n";
1963965be93Schristos	} else {
1973965be93Schristos		print STDERR
1983965be93Schristos			"No reply after maximum retransmission duration.\n";
1993965be93Schristos	}
2003965be93Schristos}
2013965be93Schristos
2023965be93Schristosif ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
2033965be93Schristos	print "Got DHCPv6 Reply message.\n";
2043965be93Schristos	exit(0);
2053965be93Schristos}
2063965be93Schristos
2073965be93Schristos#$Data::Dumper::Useqq = 1;
2083965be93Schristos#print Dumper($msg), "\n";
2093965be93Schristos#print Dumper($msg->packet()), "\n";
2103965be93Schristos#
2113965be93Schristos#print "packet length: ", length($msg->packet()), "\n";
212