source: fedkit/fed-tun.pl @ a65a65a

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since a65a65a was b641cc7, checked in by Ted Faber <faber@…>, 15 years ago

Add a default.

  • Property mode set to 100755
File size: 14.6 KB
RevLine 
[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.
[fd7a59b]16#
[c9f5490]17
18use strict;
19use Getopt::Std;
20use POSIX qw(strftime);
21use Sys::Hostname;
[a098fab]22use IO::File;
[7a8d667]23use File::Copy;
[c9f5490]24
[d501c38]25my $IFCONFIG = "/sbin/ifconfig";
[c9f5490]26my $TMCC = "/usr/local/etc/emulab/tmcc";
[b0c185f]27my $FINDIF = "/usr/local/etc/emulab/findif";
[01073f7]28# If a special version of ssh is required, it will be installed in
29# /usr/local/bin/ssh.  Otherwise use the usual one.
30my $SSH = -x "/usr/local/bin/ssh" ? "/usr/local/bin/ssh" : "/usr/bin/ssh";
[a098fab]31my $NC = "/usr/bin/nc";
32my $SSH_PORT = 22;
[fd7a59b]33my $ROUTE_GET = "/sbin/route get";  # XXX:  works on FreeBSD, but should
34                                    #       should be 'ip route get' for Linux
[7a8d667]35my $sshd_config = "/etc/ssh/sshd_config";    # Probably should be a param
[c9f5490]36
[f64fa81]37# Ports that are forwarded between testbeds
38my $TMCD_PORT = 7777;
39my $SMBFS_PORT = 139;
40my $PUBSUB_PORT = 16505;
[27b6aea]41my $SEER_PORT = 16606;
[f64fa81]42
[33e3537]43
44my $remote_pubsub_port = $PUBSUB_PORT - 1;  # There will be a local
45                                            # pubsubd running, so we
46                                            # dodge the port on the
47                                            # remote tunnel node.
[01073f7]48die "Cannot exec $SSH" unless -x $SSH;
49
[a098fab]50sub setup_bridging;
51sub setup_tunnel_cfg;
52sub parse_config;
[c9f5490]53
[a098fab]54# Option use is as follows:
55#    -f         filename containing config file
56#    -d         turn on debugging output
57#    -r         remotely invoked
58#               (if remotely invoked the last two args are the tun interface
59#               number and the remote address of the tunnel)
[c9f5490]60
[a098fab]61my $usage = "Usage: fed-tun.pl [-r] [-d] [-f config-filename] [count addr]\n";
[c9f5490]62
[fd7a59b]63my %opts;       # Note that this is used for both getops and the options file
[cd3eceb]64my @expected_opts = qw(active tunnelcfg bossname fsname type
65                       peer pubkeys privkeys);
[a098fab]66my $filename;
67my $remote;
68my $debug = 1;
[c9f5490]69my $count;
70my $addr;
[cd3eceb]71my $active;
72my $type;
73my $tunnelcfg;
[fe53e75]74my @ssh_port_fwds;              # Queue of ssh portforwarders to start.  The
75                                # -L or -R is in here.
[c8c45ee]76my $remote_script_dir;          # location of the other sides fed-tun.pl
[8034579]77my $event_repeater;             # The pathname of the event repeater
[c9f5490]78
[7a8d667]79if ($#ARGV != 0 && !getopts('df:rn', \%opts)) {
[c9f5490]80    die "$usage";
81}
82
[a098fab]83if (defined($opts{'d'})) {
84    $debug = 1;
85}
[c9f5490]86
[a098fab]87if (defined($opts{'f'})) {
88    $filename = $opts{'f'};
[c9f5490]89}
[157ac77]90else {
91    # Find the configuration file in the usual place, which depends on what
92    # experiment and project we're in.
93    my $pid;
94    my $eid;
95
96    open(TMCD, "$TMCC status |") || die "tmcc failed\n";
97    print "Opened $TMCC status\n" if ($debug);
98    while (<TMCD>) {
99        chomp;
100        /ALLOCATED=([^\/]+)\/(\S+)/ && do {
101            $pid = $1;
102            $eid = $2;
103        };
104    }
105    close(TMCD);
106    my $hn = `hostname`;
107    chomp $hn;
108    $filename = "/proj/$pid/exp/$eid/tmp/$hn.gw.conf"
109        if $pid and $eid;
110}
[c9f5490]111
112if (defined($opts{'r'})) {
113    $remote = 1;
114    die "$usage" if ($#ARGV != 1);
115   
116    $count = pop @ARGV;
117    $addr = pop @ARGV;
118}
119
[a098fab]120die "$usage" if (!defined($remote) && !defined($filename));
[c9f5490]121
[a098fab]122if (defined($filename)) {
123    &parse_config("$filename", \%opts) || 
124        die "Cannot read config file $filename: $!\n";
[c9f5490]125
[cd3eceb]126    foreach my $opt (@expected_opts) {
127        warn "Missing $opt option\n" if (!defined($opts{$opt}));
128    }
129
130    $active = 1 if ($opts{'active'} =~ /true/i);
131    $tunnelcfg = 1 if ($opts{'tunnelcfg'} =~ /true/i);
132    $type = $opts{'type'};
133    $type =~ tr/A-Z/a-z/;
[c8c45ee]134    $remote_script_dir = $opts{'remotescriptdir'} || ".";
[8034579]135    $event_repeater = $opts{'eventrepeater'};
[b641cc7]136    my $listen_on = $opts{'sshlisten'}||"";
[c9f5490]137
[cd3eceb]138    if (defined($opts{'fsname'})) {
[4702322]139        push(@ssh_port_fwds,"-R $listen_on:$SMBFS_PORT:$opts{'fsname'}:$SMBFS_PORT");
[cd3eceb]140    }
[c9f5490]141
[cd3eceb]142    if (defined($opts{'bossname'})) {
[4702322]143        push(@ssh_port_fwds, "-R $listen_on:$TMCD_PORT:$opts{'bossname'}:$TMCD_PORT");
[33e3537]144    }
145
146    if (defined($opts{'eventservername'})) {
[fe53e75]147        push(@ssh_port_fwds,"-R ". 
[4702322]148            "$listen_on:$remote_pubsub_port:$opts{'eventservername'}:$PUBSUB_PORT");
[cd3eceb]149    }
[33e3537]150    if (defined($opts{'remoteeventservername'})) {
[fe53e75]151        push(@ssh_port_fwds,"-L :$remote_pubsub_port:" . 
152            "$opts{'remoteeventservername'}:$PUBSUB_PORT");
[33e3537]153    }
[c9f5490]154
[27b6aea]155    # Forward connections to seer from remote TBs to control in this TB
156    if (defined($opts{'seercontrol'})) {
[4702322]157        push(@ssh_port_fwds,"-R $listen_on:$SEER_PORT:$opts{seercontrol}:$SEER_PORT");
[27b6aea]158    }
159
[7a8d667]160    # -n just starts the ssh tap tunnel
161    @ssh_port_fwds = () if ($opts{'type'} eq 'experiment' || $opts{'n'});
[c9f5490]162
[fe53e75]163    print "ssh_port_fwds = ", join("\n",@ssh_port_fwds), "\n" if ($debug);
[cd3eceb]164}
[c9f5490]165
[01073f7]166# Both sides need to have GatewayPorts and PermitTunnel set.  Copy the existing
167# sshd_config, making sure GatewayPorts and PermitTunnel are set to yes,
168# replace the original, and restart sshd.
[7a8d667]169my $ports_on = 0;
[01073f7]170my $tunnel_on = 0;
[7a8d667]171
172my $conf = new IO::File($sshd_config) || die "Can't open $sshd_config: $!\n";
173my $new_conf = new IO::File(">/tmp/sshd_config") || 
174    die "Can't open new ssh_config: $!\n";
175
176while(<$conf>) {
177    s/^\s*GatewayPorts.*/GatewayPorts yes/ && do {
178        print $new_conf $_ unless $ports_on++;
179        next;
180    };
[01073f7]181    s/^\s*PermitTunnel.*/PermitTunnel yes/ && do {
182        print $new_conf $_ unless $tunnel_on++;
183        next;
184    };
[7a8d667]185    print $new_conf $_;
186}
187print $new_conf "GatewayPorts yes\n" unless $ports_on;
[01073f7]188print $new_conf "PermitTunnel yes\n" unless $tunnel_on;
[7a8d667]189$conf->close();
190$new_conf->close();
191
192copy("/tmp/sshd_config", $sshd_config) || 
193    die "Cannot replace $sshd_config: $!\n";
194
195system("/etc/rc.d/sshd restart");
196
[a098fab]197# Need these to make the Ethernet tap and bridge to work...
[01073f7]198system("kldload /boot/kernel/bridgestp.ko") 
199    if -r "/boot/kernel/bridgestp.ko";
[a098fab]200system("kldload /boot/kernel/if_bridge.ko");
201system("kldload /boot/kernel/if_tap.ko");
[c9f5490]202
[cd3eceb]203if ($tunnelcfg && !$remote) {
[a098fab]204    # Most Emulab-like testbeds use globally-routable addresses on the
205    # control net;  at DETER, we isolate the control net from the Internet.
206    # On DETER-like testbeds, we need to create special tunneling nodes
207    # with external access.  Set up the external addresses as necessary.
208    &setup_tunnel_cfg(%opts);
[c9f5490]209}
210
211if (!$remote) {
[a098fab]212    system("umask 077 && cp $opts{'privkeys'} /root/.ssh/id_rsa");
213    system("umask 077 && cp $opts{'pubkeys'} /root/.ssh/id_rsa.pub");
214    system("umask 077 && cat $opts{'pubkeys'} >> /root/.ssh/authorized_keys");
215}
[c9f5490]216
[cd3eceb]217if ($active) {
[a098fab]218    # If we're the initiator, open up a separate tunnel to the remote
219    # host for each of the different experiment net interfaces on this
220    # machine.  Execute this startup script on the far end, but with
221    # the -r option to indicate that it's getting invoked remotely and
222    # we should just handle the remote-end tasks.
[c9f5490]223
[a098fab]224    # Set up synchronization, so that the various user machines won't try to
225    # contact boss before the tunnels are set up.  (Thanks jjh!)
[c9f5490]226
[a098fab]227    do {
228        system("$NC -z $opts{'peer'} $SSH_PORT");
229    } until (!$?);
[c9f5490]230
231    # XXX:  Do we need to clear out previously created bridge interfaces?
232
233    my $count = 0;
[a098fab]234    my @SSHCMD;
[c9f5490]235
[d501c38]236    # If we are just setting up a control net connection, just fire up
237    # ssh with the null command, and hang loose.
238
239    if ($type eq "control") {
[fe53e75]240        foreach my $fwd (@ssh_port_fwds) {
[4702322]241            system("$SSH -N $fwd -o \"StrictHostKeyChecking no\" ". 
[fe53e75]242                "$opts{'peer'} &"); #or die "Failed to run ssh";
243        }
[d501c38]244
245        exit;
246    }
247
[c9f5490]248    open(IFFILE, "/var/emulab/boot/ifmap") || die "couldn't open ifmap\n";
249    while (<IFFILE>) {
250        my @a = split(' ');
251        my $iface = $a[0];
252        my $addr = $a[1];
253        my $bridge = "bridge" . $count;
254        my $tun = "tap" . $count;
[fe53e75]255        my $cmd;
[c9f5490]256
257        print "Found $iface, $addr, to bridge on $bridge\n" if ($debug);
258
[a098fab]259        # Note that we're going to fire off an ssh which will never return;
260        # we need the connection to stay up to keep the tunnel up.
261        # In order to check for problems, we open it this way and read
262        # the expected single line of output when the tunnel is connected.
[fe53e75]263        # To make debugging easier and to degrade more gracefully, I've split
264        # these out into multiple processes.
265       
266        foreach my $fwd (@ssh_port_fwds) {
267            $cmd = "$SSH -N $fwd -o \"StrictHostKeyChecking no\" ". 
268                "$opts{'peer'} &";
269
270            print "$cmd\n" if $debug;
271            system("$cmd"); # or die "Failed to run ssh";
272        }
[01073f7]273        # The Tunnel option specifies the level to tunnel at.  Ethernet creates
274        # a tap device rather than a tun device.  Strict host key checking
275        # avoids asking the user to OK a strange host key.
276        $cmd =  "$SSH -w $count:$count -o \"Tunnel ethernet\" " . 
277            "-o \"StrictHostKeyChecking no\" " . 
[fe53e75]278            "$opts{'peer'}  \"$remote_script_dir/fed-tun.pl " . 
[4702322]279            "-r $addr $count\" & |";
[fe53e75]280
281        print "$cmd\n" if $debug;
[c9f5490]282
[0ea5050]283        open(SSHCMD, $cmd) 
[fe53e75]284           or die "Failed to run ssh";
[227f558]285        # Wait for the other end to report its work done.  This string comes
286        # from the block below (after the elsif) -- tvf
287        my $check;  # Make sure something ran...
[c9f5490]288
[227f558]289        while ($check = <SSHCMD>) {
290            last if $check =~ /^Remote connection all/;
291        }
[d501c38]292        print "Got line [$check] from remote side\n" if ($debug);
[c9f5490]293
[a098fab]294        &setup_bridging($tun, $bridge, $iface, $addr);
[c9f5490]295        $count++;
[fe53e75]296        @ssh_port_fwds = ();  # only do this on the first connection
[c9f5490]297    }
298    close(IFFILE);
[33e3537]299
300    # Start a local event repeater (unless we're missing parameters
301    die "Missing event repeater params (No config file ?)\n"
302        unless $event_repeater && $opts{'remoteexperiment'} && 
303        $opts{'localexperiment'};
304
305    print "Starting event repeater\n" if $debug;
306   
307    print("$event_repeater -M -P $remote_pubsub_port -S localhost " . 
308        "-E $opts{'remoteexperiment'} -e $opts{'localexperiment'}\n") 
309        if $debug;
310    # Connect to the forwarded pubsub port on this host to listen to the
311    # other experiment's events, and to the local experiment to forward
312    # events.
313    system("$event_repeater -M -P $remote_pubsub_port -S localhost " . 
314        "-E $opts{'remoteexperiment'} -e $opts{'localexperiment'}");
315    warn "Event repeater returned $?\n" if $?;
[a098fab]316} elsif ($remote) {
[fd7a59b]317    # We're on the remote system;  figure out which interface to
318    # tweak, based on the IP address passed in.
[c9f5490]319
320    my $iter = 0;
[fd7a59b]321    my $iface;
[c9f5490]322
[fd7a59b]323    open(RTFILE, "$ROUTE_GET $addr |") || die "couldn't do $ROUTE_GET\n";
324    while (<RTFILE>) {
325        if (/interface: (\w+)/) {
326            $iface = $1;
327        }
[c9f5490]328    }
[fd7a59b]329    close(RTFILE);
330
331    die "Couldn't find interface to use." if (!defined($iface));
332    my $bridge = "bridge" . $count;
333    my $tun = "tap" . $count;
[a098fab]334
[fd7a59b]335    &setup_bridging($tun, $bridge, $iface, $addr);
[a098fab]336    print "Remote connection all set up!\n";  # Trigger other end with output
[8034579]337
338    # If this is the first remote invocation on a control gateway, start the
339    # event repeater.
340
341    if ( $count == 0 && $type ne "experiment" ) {
342        my $remote_pubsub_port = $PUBSUB_PORT - 1;  # There will be a local
343                                                    # pubsubd running, so we
344                                                    # dodge the port on the
345                                                    # remote tunnel node.
346        # Make sure we have the relevant parameters
347        die "Missing event repeater params (No config file ?)\n"
348            unless $event_repeater && $opts{'remoteexperiment'} && 
349            $opts{'localexperiment'};
350
351        print "Starting event repeater\n" if $debug;
352       
353        # Connect to the forwarded pubsub port on this host to listen to the
354        # other experiment's events, and to the local experiment to forward
355        # events.
356        system("$event_repeater -P $remote_pubsub_port -S localhost " . 
357            "-E $opts{'remoteexperiment'} -e $opts{'localexperiment'}");
358        warn "Event repeater returned $?\n" if $?;
359    }
[a098fab]360} else {
361    print "inactive end of a connection, finishing" if ($debug);
[c9f5490]362}
363
364print "all done!\n" if ($debug);
365exit;
366
367
368# Set up the bridging for the new stuff...
369
[a098fab]370sub setup_bridging($; $; $; $) {
[c9f5490]371    my ($tun, $bridge, $iface, $addr) = @_;
372
[d501c38]373    print "Waiting to see if new iface $tun is up\n" if ($debug);
374
375    do {
376        sleep 1;
377        system("$IFCONFIG $tun");
378    } until (!$?);
379
[c9f5490]380    print "setting up $bridge with $iface and nuking $addr\n" if ($debug);
381
382    system("ifconfig $bridge create");
383    system("ifconfig $iface delete $addr");
384    system("ifconfig $bridge addm $iface up");
385    system("ifconfig $bridge addm $tun");
386}
[a098fab]387
388# Set up tunnel info for DETER-like testbeds.
389
390sub setup_tunnel_cfg {
391    my (%opts) = @_;
[b09f346]392    #my $tunnel_iface = "em0";   # XXX
393    my $tunnel_iface = $opts{'interface'} || "em0";
[a098fab]394    my $tunnel_ip;
395    my $tunnel_mask;
396    my $tunnel_mac;
397    my $tunnel_router;
398
399    print "Opening $TMCC tunnelip\n" if ($debug);
400
401    open(TMCD, "$TMCC tunnelip |") || die "tmcc failed\n";
402    print "Opened $TMCC tunnelip\n" if ($debug);
403    while (<TMCD>) {
404        print "got one line from tmcc\n" if ($debug);
405        print if ($debug);
406        if (/^TUNNELIP=([0-9.]*) TUNNELMASK=([0-9.]*) TUNNELMAC=(\w*) TUNNELROUTER=([0-9.]*)$/) {
407            $tunnel_ip = $1;
408            $tunnel_mask = $2;
409            $tunnel_mac = $3;
410            $tunnel_router = $4;
411        }
412    }
413    close(TMCD);
414
415    print "tunnel options:  ip=$tunnel_ip mask=$tunnel_mask mac=$tunnel_mac router=$tunnel_router\n" if ($debug);
416
[b0c185f]417    if (defined($tunnel_mac) && ($tunnel_mac ne "")) {
418       print "Opening $FINDIF $tunnel_mac\n" if ($debug);
419       open(FIF, "$FINDIF $tunnel_mac |") || die "findif failed\n";
420       my $line = <FIF>;
421       chomp($line);
422       print "$FINDIF returned $line\n" if ($debug);
423       $tunnel_iface = $line;
424    }
[a098fab]425
426    system("ifconfig $tunnel_iface $tunnel_ip" .
[2aeb39e]427           ($tunnel_mask ? " netmask $tunnel_mask" : "") . " up");
[a098fab]428    warn "configuration of tunnel interface failed" if ($?);
[4abace9]429
[4702322]430    if ($tunnel_router) {
431
432        # Sometimes the insertion of DNS names lags a bit.  Retry this
433        # configuration a few times to let DNS catch up.  Might want to really
434        # check the DNS name before we try this...
435        my $config_succeeded = 0;
436        my $tries = 0;
437        my $max_retries = 300;
438
439        do {
440            system("route add $opts{'peer'} $tunnel_router");
441            if ( $? ) {
442                warn "configuration routes via tunnel interface failed";
443                $tries++;
444                sleep(10);
445            }
446            else { $config_succeeded = 1; }
447        } until ( $config_succeeded || $tries > $max_retries );
448    }
[a098fab]449
450    print "setup_tunnel_cfg done\n" if ($debug);
451}
452
453# Trick config-file parsing code from Ted Faber:
454
455# Parse the config file.  The format is a colon-separated parameter name
456# followed by the value of that parameter to the end of the line.  This parses
457# that format and puts the parameters into the referenced hash.  Parameter
458# names are mapped to lower case, parameter values are unchanged.  Returns 0 on
459# failure (e.g. file open) and 1 on success.
460sub parse_config {
461    my($file, $href) = @_;
462    my($fh) = new IO::File($file);
463       
464    unless ($fh) {
465        warn "Can't open $file: $!\n";
466        return 0;
467    }
468
469    while (<$fh>) {
470        next if /^\s*#/ || /^\s*$/;     # Skip comments & blanks
471        chomp;
472        /^([^:]+):\s*(.*)/ && do {
473            my($key) = $1; 
474
475            $key =~ tr/A-Z/a-z/;
476            $href->{$key} = $2;
477            next;
478        };
479        warn "Unparasble line in $file: $_\n";
480    }
481    $fh->close();   # It will close when it goes out of scope, but...
482    return 1;
483}
Note: See TracBrowser for help on using the repository browser.