[c9f5490] | 1 | #!/usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | # Kevin Lahey, lahey@isi.edu |
---|
| 4 | # July 11, 2007 |
---|
| 5 | |
---|
| 6 | # Set up ssh tunnel infrastructure for federation: |
---|
| 7 | # |
---|
[a098fab] | 8 | # * Parse the configuration file provided. |
---|
| 9 | # |
---|
| 10 | # * Figure out whether we're the initiator or the reciever; if we're |
---|
| 11 | # the receiver, we just need to set up ssh keys and exit. |
---|
| 12 | # |
---|
| 13 | # * Pick out the experimental interface, remove the IP address |
---|
[c9f5490] | 14 | # |
---|
[a098fab] | 15 | # * Create a layer 2 ssh tunnel, set up bridging, and hang loose. |
---|
[c9f5490] | 16 | |
---|
| 17 | use strict; |
---|
| 18 | use Getopt::Std; |
---|
| 19 | use POSIX qw(strftime); |
---|
| 20 | use Sys::Hostname; |
---|
[a098fab] | 21 | use IO::File; |
---|
[c9f5490] | 22 | |
---|
[d501c38] | 23 | my $IFCONFIG = "/sbin/ifconfig"; |
---|
[c9f5490] | 24 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
| 25 | my $SSH = "/usr/local/bin/ssh"; |
---|
[a098fab] | 26 | my $NC = "/usr/bin/nc"; |
---|
| 27 | my $SSH_PORT = 22; |
---|
[c9f5490] | 28 | |
---|
[a098fab] | 29 | sub setup_bridging; |
---|
| 30 | sub setup_tunnel_cfg; |
---|
| 31 | sub parse_config; |
---|
[c9f5490] | 32 | |
---|
[a098fab] | 33 | # Option use is as follows: |
---|
| 34 | # -f filename containing config file |
---|
| 35 | # -d turn on debugging output |
---|
| 36 | # -r remotely invoked |
---|
| 37 | # (if remotely invoked the last two args are the tun interface |
---|
| 38 | # number and the remote address of the tunnel) |
---|
[c9f5490] | 39 | |
---|
[a098fab] | 40 | my $usage = "Usage: fed-tun.pl [-r] [-d] [-f config-filename] [count addr]\n"; |
---|
[c9f5490] | 41 | |
---|
[a098fab] | 42 | my %opts; |
---|
[cd3eceb] | 43 | my @expected_opts = qw(active tunnelcfg bossname fsname type |
---|
| 44 | peer pubkeys privkeys); |
---|
[a098fab] | 45 | my $filename; |
---|
| 46 | my $remote; |
---|
| 47 | my $debug = 1; |
---|
[c9f5490] | 48 | my $count; |
---|
| 49 | my $addr; |
---|
[cd3eceb] | 50 | my $active; |
---|
| 51 | my $type; |
---|
| 52 | my $tunnelcfg; |
---|
| 53 | my $ssh_port_fwds = ""; |
---|
[c8c45ee] | 54 | my $remote_script_dir; # location of the other sides fed-tun.pl |
---|
[c9f5490] | 55 | |
---|
[a098fab] | 56 | if ($#ARGV != 0 && !getopts('df:r', \%opts)) { |
---|
[c9f5490] | 57 | die "$usage"; |
---|
| 58 | } |
---|
| 59 | |
---|
[a098fab] | 60 | if (defined($opts{'d'})) { |
---|
| 61 | $debug = 1; |
---|
| 62 | } |
---|
[c9f5490] | 63 | |
---|
[a098fab] | 64 | if (defined($opts{'f'})) { |
---|
| 65 | $filename = $opts{'f'}; |
---|
[c9f5490] | 66 | } |
---|
| 67 | |
---|
| 68 | if (defined($opts{'r'})) { |
---|
| 69 | $remote = 1; |
---|
| 70 | die "$usage" if ($#ARGV != 1); |
---|
| 71 | |
---|
| 72 | $count = pop @ARGV; |
---|
| 73 | $addr = pop @ARGV; |
---|
| 74 | } |
---|
| 75 | |
---|
[a098fab] | 76 | die "$usage" if (!defined($remote) && !defined($filename)); |
---|
[c9f5490] | 77 | |
---|
[a098fab] | 78 | if (defined($filename)) { |
---|
| 79 | &parse_config("$filename", \%opts) || |
---|
| 80 | die "Cannot read config file $filename: $!\n"; |
---|
[c9f5490] | 81 | |
---|
[cd3eceb] | 82 | foreach my $opt (@expected_opts) { |
---|
| 83 | warn "Missing $opt option\n" if (!defined($opts{$opt})); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | $active = 1 if ($opts{'active'} =~ /true/i); |
---|
| 87 | $tunnelcfg = 1 if ($opts{'tunnelcfg'} =~ /true/i); |
---|
| 88 | $type = $opts{'type'}; |
---|
| 89 | $type =~ tr/A-Z/a-z/; |
---|
[c8c45ee] | 90 | $remote_script_dir = $opts{'remotescriptdir'} || "."; |
---|
[c9f5490] | 91 | |
---|
[cd3eceb] | 92 | if (defined($opts{'fsname'})) { |
---|
[a098fab] | 93 | $ssh_port_fwds = "-R :139:$opts{'fsname'}:139 "; |
---|
[cd3eceb] | 94 | } |
---|
[c9f5490] | 95 | |
---|
[cd3eceb] | 96 | if (defined($opts{'bossname'})) { |
---|
[a098fab] | 97 | $ssh_port_fwds .= "-R :7777:$opts{'bossname'}:7777 "; |
---|
[cd3eceb] | 98 | } |
---|
[c9f5490] | 99 | |
---|
[cd3eceb] | 100 | $ssh_port_fwds = "" if ($opts{'type'} eq 'experiment'); |
---|
[c9f5490] | 101 | |
---|
[cd3eceb] | 102 | print "ssh_port_fwds = $ssh_port_fwds\n" if ($debug); |
---|
| 103 | } |
---|
[c9f5490] | 104 | |
---|
[a098fab] | 105 | # Need these to make the Ethernet tap and bridge to work... |
---|
[c9f5490] | 106 | |
---|
[a098fab] | 107 | system("kldload /boot/kernel/if_bridge.ko"); |
---|
| 108 | system("kldload /boot/kernel/if_tap.ko"); |
---|
[c9f5490] | 109 | |
---|
[cd3eceb] | 110 | if ($tunnelcfg && !$remote) { |
---|
[a098fab] | 111 | # Most Emulab-like testbeds use globally-routable addresses on the |
---|
| 112 | # control net; at DETER, we isolate the control net from the Internet. |
---|
| 113 | # On DETER-like testbeds, we need to create special tunneling nodes |
---|
| 114 | # with external access. Set up the external addresses as necessary. |
---|
| 115 | &setup_tunnel_cfg(%opts); |
---|
[c9f5490] | 116 | } |
---|
| 117 | |
---|
| 118 | if (!$remote) { |
---|
[a098fab] | 119 | system("umask 077 && cp $opts{'privkeys'} /root/.ssh/id_rsa"); |
---|
| 120 | system("umask 077 && cp $opts{'pubkeys'} /root/.ssh/id_rsa.pub"); |
---|
| 121 | system("umask 077 && cat $opts{'pubkeys'} >> /root/.ssh/authorized_keys"); |
---|
| 122 | } |
---|
[c9f5490] | 123 | |
---|
[cd3eceb] | 124 | if ($active) { |
---|
[a098fab] | 125 | # If we're the initiator, open up a separate tunnel to the remote |
---|
| 126 | # host for each of the different experiment net interfaces on this |
---|
| 127 | # machine. Execute this startup script on the far end, but with |
---|
| 128 | # the -r option to indicate that it's getting invoked remotely and |
---|
| 129 | # we should just handle the remote-end tasks. |
---|
[c9f5490] | 130 | |
---|
[a098fab] | 131 | # Set up synchronization, so that the various user machines won't try to |
---|
| 132 | # contact boss before the tunnels are set up. (Thanks jjh!) |
---|
[c9f5490] | 133 | |
---|
[a098fab] | 134 | do { |
---|
| 135 | system("$NC -z $opts{'peer'} $SSH_PORT"); |
---|
| 136 | } until (!$?); |
---|
[c9f5490] | 137 | |
---|
| 138 | # XXX: Do we need to clear out previously created bridge interfaces? |
---|
| 139 | |
---|
| 140 | my $count = 0; |
---|
[a098fab] | 141 | my @SSHCMD; |
---|
[c9f5490] | 142 | |
---|
[d501c38] | 143 | # If we are just setting up a control net connection, just fire up |
---|
| 144 | # ssh with the null command, and hang loose. |
---|
| 145 | |
---|
| 146 | if ($type eq "control") { |
---|
| 147 | system("$SSH $ssh_port_fwds -Nno \"StrictHostKeyChecking no\" $opts{'peer'} &"); #or die "Failed to run ssh"; |
---|
| 148 | |
---|
| 149 | exit; |
---|
| 150 | } |
---|
| 151 | |
---|
[c9f5490] | 152 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
| 153 | while (<IFFILE>) { |
---|
| 154 | my @a = split(' '); |
---|
| 155 | my $iface = $a[0]; |
---|
| 156 | my $addr = $a[1]; |
---|
| 157 | my $bridge = "bridge" . $count; |
---|
| 158 | my $tun = "tap" . $count; |
---|
| 159 | |
---|
| 160 | print "Found $iface, $addr, to bridge on $bridge\n" if ($debug); |
---|
| 161 | |
---|
[a098fab] | 162 | # Note that we're going to fire off an ssh which will never return; |
---|
| 163 | # we need the connection to stay up to keep the tunnel up. |
---|
| 164 | # In order to check for problems, we open it this way and read |
---|
| 165 | # the expected single line of output when the tunnel is connected. |
---|
[c9f5490] | 166 | |
---|
[c8c45ee] | 167 | open($SSHCMD[$count], "$SSH -w $count:$count $ssh_port_fwds -o \"StrictHostKeyChecking no\" $opts{'peer'} \"$remote_script_dir/fed-tun.pl -r $addr $count\" |") or die "Failed to run ssh"; |
---|
[c9f5490] | 168 | |
---|
[a098fab] | 169 | my $check = <$SSHCMD[$count]>; # Make sure something ran... |
---|
[d501c38] | 170 | print "Got line [$check] from remote side\n" if ($debug); |
---|
[c9f5490] | 171 | |
---|
[a098fab] | 172 | &setup_bridging($tun, $bridge, $iface, $addr); |
---|
[c9f5490] | 173 | $count++; |
---|
[a098fab] | 174 | $ssh_port_fwds = ""; # only do this on the first connection |
---|
[c9f5490] | 175 | } |
---|
| 176 | close(IFFILE); |
---|
[a098fab] | 177 | } elsif ($remote) { |
---|
[c9f5490] | 178 | # We're on the remote system; for now, we just grab and use |
---|
| 179 | # the one experimental interface. Later, we'll actually look |
---|
| 180 | # at the address passed to match up the appropriate interfaces. |
---|
| 181 | |
---|
| 182 | my $iter = 0; |
---|
| 183 | |
---|
| 184 | open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n"; |
---|
| 185 | while (<IFFILE>) { |
---|
| 186 | die "Argh, too many experimental interfaces!" if ($iter > 0); |
---|
| 187 | my @a = split(' '); |
---|
| 188 | my $iface = $a[0]; |
---|
| 189 | my $addr = $a[1]; |
---|
| 190 | my $bridge = "bridge" . $count; |
---|
| 191 | my $tun = "tap" . $count; |
---|
| 192 | |
---|
[a098fab] | 193 | &setup_bridging($tun, $bridge, $iface, $addr); |
---|
[c9f5490] | 194 | $iter++; |
---|
| 195 | } |
---|
| 196 | close(IFFILE); |
---|
[a098fab] | 197 | |
---|
| 198 | print "Remote connection all set up!\n"; # Trigger other end with output |
---|
| 199 | } else { |
---|
| 200 | print "inactive end of a connection, finishing" if ($debug); |
---|
[c9f5490] | 201 | } |
---|
| 202 | |
---|
| 203 | print "all done!\n" if ($debug); |
---|
| 204 | exit; |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | # Set up the bridging for the new stuff... |
---|
| 208 | |
---|
[a098fab] | 209 | sub setup_bridging($; $; $; $) { |
---|
[c9f5490] | 210 | my ($tun, $bridge, $iface, $addr) = @_; |
---|
| 211 | |
---|
[d501c38] | 212 | print "Waiting to see if new iface $tun is up\n" if ($debug); |
---|
| 213 | |
---|
| 214 | do { |
---|
| 215 | sleep 1; |
---|
| 216 | system("$IFCONFIG $tun"); |
---|
| 217 | } until (!$?); |
---|
| 218 | |
---|
[c9f5490] | 219 | print "setting up $bridge with $iface and nuking $addr\n" if ($debug); |
---|
| 220 | |
---|
| 221 | system("ifconfig $bridge create"); |
---|
| 222 | system("ifconfig $iface delete $addr"); |
---|
| 223 | system("ifconfig $bridge addm $iface up"); |
---|
| 224 | system("ifconfig $bridge addm $tun"); |
---|
| 225 | } |
---|
[a098fab] | 226 | |
---|
| 227 | # Set up tunnel info for DETER-like testbeds. |
---|
| 228 | |
---|
| 229 | sub setup_tunnel_cfg { |
---|
| 230 | my (%opts) = @_; |
---|
| 231 | my $tunnel_iface = "em0"; # XXX |
---|
| 232 | my $tunnel_ip; |
---|
| 233 | my $tunnel_mask; |
---|
| 234 | my $tunnel_mac; |
---|
| 235 | my $tunnel_router; |
---|
| 236 | |
---|
| 237 | print "Opening $TMCC tunnelip\n" if ($debug); |
---|
| 238 | |
---|
| 239 | open(TMCD, "$TMCC tunnelip |") || die "tmcc failed\n"; |
---|
| 240 | print "Opened $TMCC tunnelip\n" if ($debug); |
---|
| 241 | while (<TMCD>) { |
---|
| 242 | print "got one line from tmcc\n" if ($debug); |
---|
| 243 | print if ($debug); |
---|
| 244 | if (/^TUNNELIP=([0-9.]*) TUNNELMASK=([0-9.]*) TUNNELMAC=(\w*) TUNNELROUTER=([0-9.]*)$/) { |
---|
| 245 | $tunnel_ip = $1; |
---|
| 246 | $tunnel_mask = $2; |
---|
| 247 | $tunnel_mac = $3; |
---|
| 248 | $tunnel_router = $4; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | close(TMCD); |
---|
| 252 | |
---|
| 253 | die "Unable to determine tunnel node configuration information" |
---|
| 254 | if (!defined($tunnel_router)); |
---|
| 255 | |
---|
| 256 | print "tunnel options: ip=$tunnel_ip mask=$tunnel_mask mac=$tunnel_mac router=$tunnel_router\n" if ($debug); |
---|
| 257 | |
---|
| 258 | # Sadly, we ignore the tunnel mac for now -- we should eventually |
---|
| 259 | # use it to determine which interface to use, just like the |
---|
| 260 | # Emulab startup scripts. |
---|
| 261 | |
---|
| 262 | system("ifconfig $tunnel_iface $tunnel_ip" . |
---|
| 263 | ($tunnel_mask ? " netmask $tunnel_mask" : "")); |
---|
| 264 | warn "configuration of tunnel interface failed" if ($?); |
---|
| 265 | |
---|
| 266 | system("route add $opts{'peer'} $tunnel_router"); |
---|
| 267 | warn "configuration routes via tunnel interface failed" if ($?); |
---|
| 268 | |
---|
| 269 | print "setup_tunnel_cfg done\n" if ($debug); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | # Trick config-file parsing code from Ted Faber: |
---|
| 273 | |
---|
| 274 | # Parse the config file. The format is a colon-separated parameter name |
---|
| 275 | # followed by the value of that parameter to the end of the line. This parses |
---|
| 276 | # that format and puts the parameters into the referenced hash. Parameter |
---|
| 277 | # names are mapped to lower case, parameter values are unchanged. Returns 0 on |
---|
| 278 | # failure (e.g. file open) and 1 on success. |
---|
| 279 | sub parse_config { |
---|
| 280 | my($file, $href) = @_; |
---|
| 281 | my($fh) = new IO::File($file); |
---|
| 282 | |
---|
| 283 | unless ($fh) { |
---|
| 284 | warn "Can't open $file: $!\n"; |
---|
| 285 | return 0; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | while (<$fh>) { |
---|
| 289 | next if /^\s*#/ || /^\s*$/; # Skip comments & blanks |
---|
| 290 | chomp; |
---|
| 291 | /^([^:]+):\s*(.*)/ && do { |
---|
| 292 | my($key) = $1; |
---|
| 293 | |
---|
| 294 | $key =~ tr/A-Z/a-z/; |
---|
| 295 | $href->{$key} = $2; |
---|
| 296 | next; |
---|
| 297 | }; |
---|
| 298 | warn "Unparasble line in $file: $_\n"; |
---|
| 299 | } |
---|
| 300 | $fh->close(); # It will close when it goes out of scope, but... |
---|
| 301 | return 1; |
---|
| 302 | } |
---|