]> git.saurik.com Git - apple/network_cmds.git/blob - ip6conf.tproj/ip6config
network_cmds-115.tar.gz
[apple/network_cmds.git] / ip6conf.tproj / ip6config
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 #
5 # @APPLE_LICENSE_HEADER_START@
6 #
7 # "Portions Copyright (c) 2002 Apple Computer, Inc. All Rights
8 # Reserved. This file contains Original Code and/or Modifications of
9 # Original Code as defined in and that are subject to the Apple Public
10 # Source License Version 1.0 (the 'License'). You may not use this file
11 # except in compliance with the License. Please obtain a copy of the
12 # License at http://www.apple.com/publicsource and read it before using
13 # this file.
14 #
15 # The Original Code and all software distributed under the License are
16 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
20 # License for the specific language governing rights and limitations
21 # under the License."
22 #
23 # @APPLE_LICENSE_HEADER_END@
24 #
25 # Setup IPv6 for Darwin
26 # - Startup/shutdown IPv6 on the given interface
27 # - Startup/shutdown 6to4 on the given interface
28 # - Start/stop router advertisement.
29 #
30 # Setup 6to4 IPv6, for NetBSD (and maybe others)
31 #
32 # (c) Copyright 2000 Hubert Feyrer <hubert@feyrer.de>
33 #
34
35
36 # Directory for conf file
37 $etcdir="/private/etc";
38 require "$etcdir/6to4.conf";
39
40 use Getopt::Std;
41
42 ###########################################################################
43 sub do_6to4_setup
44 {
45 #
46 # Some sanity checks - check for link-local address and stf
47 #
48 if (`ifconfig -a | grep fe80: | wc -l` <= 0 or
49 `ifconfig -a | grep stf | wc -l` <= 0) {
50 die "$0: It seems your kernel does not support IPv6 or 6to4 (stf).\n";
51 }
52
53 #
54 # Take the requested interface from the user
55 # Figure out addressing, etc.
56 #
57 $localadr4 = `ifconfig $ARGV[1] inet | grep inet`;
58 $localadr4 =~ s/^.*inet\s*//;
59 $localadr4 =~ s/\s.*$//;
60 chomp($localadr4);
61
62 @l4c = split('\.', $localadr4);
63 $prefix = sprintf("2002:%02x%02x:%02x%02x", @l4c[0..3]);
64
65 $localadr6 = sprintf("$prefix:%04x", $v6_net);
66
67 #
68 # Anycast is default in 6to4.conf file
69 #
70 if ($peer eq "6to4-anycast") {
71 # magic values from rfc 3068
72 $remoteadr4 = "192.88.99.1";
73 $remoteadr6 = "2002:c058:6301::";
74 }
75 else {
76 chomp($remoteadr4 = `host $peer`);
77 $remoteadr4 =~ s/^.*address //;
78
79 chomp($remoteadr6 = `host -t AAAA $peer`);
80 $remoteadr6 =~ s/^.*address //;
81 }
82
83 }
84
85 ###########################################################################
86 sub do_usage
87 {
88 print "Usage: $0 \n";
89 print " start-v6 all | stop-v6 all\n";
90 print " start-v6 [interface] | stop-v6 [interface]\n";
91 print " start-stf [interface] | stop-stf\n";
92 print " start-rtadvd | stop-rtadvd\n";
93 }
94 ###########################################################################
95
96 #
97 # Process options - just help for now
98 #
99 getopts('h');
100
101 if ($opt_h) {
102 do_usage;
103 exit 0;
104 }
105
106 #
107 # Handle commands
108 #
109
110 # Start IPv6
111 if ($ARGV[0] eq "start-v6" or $ARGV[0] eq "v6-start") {
112 if ($ARGV[1] eq "all") {
113 print "Starting IPv6 on all interfaces.\n";
114 system "ip6 -a";
115 }
116 else {
117 print "Starting IPv6 on $ARGV[1].\n";
118 system "ip6 -u $ARGV[1]";
119 }
120 }
121
122 # Stop IPv6
123 elsif ($ARGV[0] eq "stop-v6" or $ARGV[0] eq "v6-stop") {
124 if ($ARGV[1] eq "all") {
125 print "Stopping IPv6 on all interfaces.\n";
126 system "ip6 -x";
127 }
128 else {
129 print "Stopping IPv6 on $ARGV[1].\n";
130 system "ip6 -d $ARGV[1]";
131 }
132 }
133
134 # Start 6to4
135 elsif ($ARGV[0] eq "start-stf" or $ARGV[0] eq "stf-start") {
136 do_6to4_setup;
137 print "Starting 6to4 on $ARGV[1].\n";
138 system "ifconfig stf0 inet6 $localadr6:$hostbits6 prefixlen $v6_prefixlen alias";
139 system "route add -inet6 default $remoteadr6";
140 if ($in_if ne "") {
141 system "ifconfig $in_if inet6 $prefix:$v6_innernet:$hostbits6";
142 }
143 }
144
145 # Stop 6to4
146 elsif ($ARGV[0] eq "stop-stf" or $ARGV[0] eq "stf-stop") {
147 print "Stopping 6to4.\n";
148 system "ifconfig stf0 down";
149 $cmd="ifconfig stf0 inet6 " .
150 "| grep inet6 " .
151 "| sed -e 's/inet6//' " .
152 "-e 's/prefix.*//g' " .
153 "-e 's/^[ ]*//' " .
154 "-e 's/[ ]*\$//'";
155 foreach $ip ( split('\s+', `$cmd`)) {
156 system "ifconfig stf0 inet6 -alias $ip";
157 }
158 system "route delete -inet6 default";
159 }
160
161 # Start router advertisement
162 elsif ($ARGV[0] eq "rtadvd-start" or $ARGV[0] eq "start-rtadvd") {
163 print "WARNING: Setting up router advertisement should be done with great care\n";
164 print "because of a number of security issues. You should make sure this is\n";
165 print "allowed on your network and possibly fine-tune rtadvd.conf.\n";
166 print "\n";
167 print "Are you sure you want to start router advertisement (yes/no) ?: ";
168 while (<STDIN>) {
169 chomp;
170 if ($_ eq "yes" or $_ eq "y") {
171 if ( -f "/var/run/rtadvd.pid" ) {
172 print "rtadvd already running!\n";
173 } else {
174 print "Starting router advertisement.\n";
175 system "sysctl -w net.inet6.ip6.forwarding=1";
176 system "sysctl -w net.inet6.ip6.accept_rtadv=0";
177 shift @ARGV;
178 system "rtadvd @ARGV";
179 }
180 last;
181 }
182 elsif ($_ eq "no" or $_ eq "n") {
183 print "Router advertisement startup aborted.\n";
184 last;
185 }
186 else {
187 print "Invalid entry! Try again.\n";
188 print "Are you sure you want to start router advertisement? (yes/no): ";
189 }
190 }
191 }
192
193 # Stop router advertisement
194 elsif ($ARGV[0] eq "rtadvd-stop" or $ARGV[0] eq "stop-rtadvd") {
195 if ( -f "/var/run/rtadvd.pid" ) {
196 print "Stopping router advertisement.\n";
197 $pid = `cat /var/run/rtadvd.pid`;
198 system "kill -TERM $pid";
199 system "rm -f /var/run/rtadvd.pid";
200 system "rm -f /var/run/6to4-rtadvd.conf.$pid";
201 } else {
202 print "no rtadvd running!\n";
203 }
204 }
205 else {
206 do_usage;
207 }