source: fedkit/fed-tun.pl @ a098fab

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since a098fab was a098fab, checked in by Kevin Lahey <lahey@…>, 17 years ago

Considerable update to what is, alas, a pretty big ol' hack. This version
will accept a configuration file, and is a little bit slicker about firing
off the remote ssh command. Hopefully we'll get IPsec working soon, and
we can ditch this altogether.

  • Property mode set to 100755
File size: 7.6 KB
Line 
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#
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
14#
15# * Create a layer 2 ssh tunnel, set up bridging, and hang loose.
16
17use strict;
18use Getopt::Std;
19use POSIX qw(strftime);
20use Sys::Hostname;
21use IO::File;
22
23my $TMCC = "/usr/local/etc/emulab/tmcc";
24my $SSH = "/usr/local/bin/ssh";
25my $NC = "/usr/bin/nc";
26my $SSH_PORT = 22;
27
28sub setup_bridging;
29sub setup_tunnel_cfg;
30sub parse_config;
31
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)
38
39my $usage = "Usage: fed-tun.pl [-r] [-d] [-f config-filename] [count addr]\n";
40
41my %opts;
42my $filename;
43my $remote;
44my $debug = 1;
45my $count;
46my $addr;
47
48if ($#ARGV != 0 && !getopts('df:r', \%opts)) {
49    die "$usage";
50}
51
52if (defined($opts{'d'})) {
53    $debug = 1;
54}
55
56if (defined($opts{'f'})) {
57    $filename = $opts{'f'};
58}
59
60if (defined($opts{'r'})) {
61    $remote = 1;
62    die "$usage" if ($#ARGV != 1);
63   
64    $count = pop @ARGV;
65    $addr = pop @ARGV;
66}
67
68die "$usage" if (!defined($remote) && !defined($filename));
69
70if (defined($filename)) {
71    &parse_config("$filename", \%opts) || 
72        die "Cannot read config file $filename: $!\n";
73}
74
75my $ssh_port_fwds = "";
76
77if (defined($opts{'fsname'})) {
78        $ssh_port_fwds = "-R :139:$opts{'fsname'}:139 ";
79}
80
81if (defined($opts{'bossname'})) {
82        $ssh_port_fwds .= "-R :7777:$opts{'bossname'}:7777 ";
83}
84
85$ssh_port_fwds = "" if ($opts{'type'} eq 'experiment');
86
87print "ssh_port_fwds = $ssh_port_fwds\n" if ($debug);
88
89print "opts %opts\n";
90
91# Need these to make the Ethernet tap and bridge to work...
92
93system("kldload /boot/kernel/if_bridge.ko");
94system("kldload /boot/kernel/if_tap.ko");
95
96if ($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);
102}
103
104if (!$remote) {
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}
109
110if ($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.
116
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!)
119
120    do {
121        system("$NC -z $opts{'peer'} $SSH_PORT");
122    } until (!$?);
123
124    # XXX:  Do we need to clear out previously created bridge interfaces?
125
126    my $count = 0;
127    my @SSHCMD;
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
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.
143
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";
145
146        my $check = <$SSHCMD[$count]>;  # Make sure something ran...
147
148        &setup_bridging($tun, $bridge, $iface, $addr);
149        $count++;
150        $ssh_port_fwds = "";  # only do this on the first connection
151    }
152    close(IFFILE);
153} elsif ($remote) {
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
169        &setup_bridging($tun, $bridge, $iface, $addr);
170        $iter++;
171    }
172    close(IFFILE);
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);
177}
178
179print "all done!\n" if ($debug);
180exit;
181
182
183# Set up the bridging for the new stuff...
184
185sub setup_bridging($; $; $; $) {
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}
196
197# Set up tunnel info for DETER-like testbeds.
198
199sub 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.
249sub 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}
Note: See TracBrowser for help on using the repository browser.