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