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