"Inital commit to Gerrit"
[profile/ivi/dhcp.git] / tests / DHCPv6 / 212-solicit-opt-in-na-norapidcommit.pl
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
23 use strict;
24 use English;
25 use Time::HiRes qw( sleep );
26 use Socket;
27 use Socket6;
28 use IO::Select;
29
30 use dhcp_client;
31
32 # XXX: for debugging
33 use Data::Dumper;
34 $Data::Dumper::Useqq = 1;
35
36 # not-yet-standard options
37 my $OPT_TIME_SERVERS = 40;
38 my $OPT_TIME_OFFSET = 41;
39
40 # DOCSIS sub-options
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;
50
51 # well-known addresses
52 my $All_DHCP_Relay_Agents_and_Servers = "ff02::1:2";
53 my $All_DHCP_Servers = "ff05::1:3";
54
55 # ports
56 my $client_port = 546;
57 my $server_port = 547;
58
59 # create a new Solicit message
60 my $msg = dhcp_client::msg->new($MSG_SOLICIT);
61
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));
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?
73 my $iaid = 0;
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);
80 }
81
82 # add Reconfigure Accept (required by DOCSIS)
83 $msg->add_option($OPT_RECONF_ACCEPT, "");
84
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));
88
89 # add Vendor Class option (required by DOCSIS)
90 $msg->add_option($OPT_VENDOR_CLASS, pack("N", 4491) . "docsis3.0");
91
92 # add Vendor-specific Information Option option (required by DOCSIS)
93 my $vsio = pack("N", 4491);
94
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);
98
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;
102
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;
107
108 $msg->add_option($OPT_VENDOR_OPTS, $vsio);
109
110 # add Rapid Commit option (required by DOCSIS)
111 #$msg->add_option($OPT_RAPID_COMMIT, "");
112
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
117 my $MRD = 0;
118
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));
122
123 my $RT;
124 my $count = 0;
125 my $mrd_end_time;
126 if ($MRD != 0) {
127         $mrd_end_time = time() + $MRD;
128 }
129 my $reply_msg;
130 do {
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)) {
138                 printf STDERR 
139                         "Error \%d sending DHCPv6 Solicit message;\n\%s\n",
140                         0+$ERRNO, $ERRNO;
141                 exit(1);
142         } elsif ($send_ret != length($packet)) {
143                 print STDERR "Unable to send entire DHCPv6 Solicit message.\n";
144                 exit(1);
145         }
146         $count++;
147
148         my $RAND = rand(0.2) - 0.1;
149         if (defined $RT) {
150                 $RT = 2*$RT + $RAND*$RT;
151                 if (($RT > $MRT) && ($MRT != 0)) {
152                         $RT = $MRT + $RAND*$RT;
153                 }
154         } else {
155                 $RT = $IRT + $RAND*$IRT;
156         }
157
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;
161         }
162
163         for (;;) {
164                 my $timeout = $rt_end_time - time();
165                 if ($timeout < 0) {
166 #                       print STDERR "Timeout waiting for DHCPv6 Advertise ",
167 #                               "or Reply message.\n";
168                         last;
169                 }
170
171                 my @ready = IO::Select->new(\*SOCK)->can_read($timeout);
172
173                 if (@ready) {
174                         my $reply;
175                         my $recv_ret;
176         
177                         $recv_ret = recv(SOCK, $reply, 1500, 0);
178                         if (not defined $recv_ret) {
179                                 printf STDERR 
180                                         "Error \%d receiving DHCPv6 " . 
181                                                 "message;\n\%s\n",
182                                         0+$ERRNO, $ERRNO;
183                                 exit(1);
184                         }
185
186                         $reply_msg = dhcp_client::msg::decode($reply, 1);
187                         if (($reply_msg->{msg_type} == $MSG_ADVERTISE) ||
188                             ($reply_msg->{msg_type} == $MSG_REPLY)) {
189                                 last;
190                         }
191                 }
192         }
193
194 } until ($reply_msg || 
195          (($MRC != 0) && ($count > $MRC)) ||
196          (defined($mrd_end_time) && ($mrd_end_time > time())));
197
198 unless ($reply_msg) {
199         if (($MRC != 0) && ($count >= $MRC)) {
200                 print STDERR 
201                         "No reply after maximum retransmission count.\n";
202         } else {
203                 print STDERR 
204                         "No reply after maximum retransmission duration.\n";
205         }
206 }
207
208 if ($reply_msg && ($reply_msg->{msg_type} == $MSG_REPLY)) {
209         print "Got DHCPv6 Reply message.\n";
210 #print Dumper($reply_msg), "\n";
211         exit(0);
212 }
213
214 #print Dumper($msg), "\n";
215 #print Dumper($msg->packet()), "\n";
216 #
217 #print "packet length: ", length($msg->packet()), "\n";
218