source: fedkit/fed-tun.pl @ c8c45ee

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since c8c45ee was c8c45ee, checked in by Ted Faber <faber@…>, 17 years ago

add script dir

  • Property mode set to 100755
File size: 8.1 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 @expected_opts = qw(active tunnelcfg bossname fsname type
43                       peer pubkeys privkeys);
44my $filename;
45my $remote;
46my $debug = 1;
47my $count;
48my $addr;
49my $active;
50my $type;
51my $tunnelcfg;
52my $ssh_port_fwds = "";
53my $remote_script_dir;          # location of the other sides fed-tun.pl
54
55if ($#ARGV != 0 && !getopts('df:r', \%opts)) {
56    die "$usage";
57}
58
59if (defined($opts{'d'})) {
60    $debug = 1;
61}
62
63if (defined($opts{'f'})) {
64    $filename = $opts{'f'};
65}
66
67if (defined($opts{'r'})) {
68    $remote = 1;
69    die "$usage" if ($#ARGV != 1);
70   
71    $count = pop @ARGV;
72    $addr = pop @ARGV;
73}
74
75die "$usage" if (!defined($remote) && !defined($filename));
76
77if (defined($filename)) {
78    &parse_config("$filename", \%opts) || 
79        die "Cannot read config file $filename: $!\n";
80
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/;
89    $remote_script_dir = $opts{'remotescriptdir'} || ".";
90
91    if (defined($opts{'fsname'})) {
92        $ssh_port_fwds = "-R :139:$opts{'fsname'}:139 ";
93    }
94
95    if (defined($opts{'bossname'})) {
96        $ssh_port_fwds .= "-R :7777:$opts{'bossname'}:7777 ";
97    }
98
99    $ssh_port_fwds = "" if ($opts{'type'} eq 'experiment');
100
101    print "ssh_port_fwds = $ssh_port_fwds\n" if ($debug);
102}
103
104# Need these to make the Ethernet tap and bridge to work...
105
106system("kldload /boot/kernel/if_bridge.ko");
107system("kldload /boot/kernel/if_tap.ko");
108
109if ($tunnelcfg && !$remote) {
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);
115}
116
117if (!$remote) {
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}
122
123if ($active) {
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.
129
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!)
132
133    do {
134        system("$NC -z $opts{'peer'} $SSH_PORT");
135    } until (!$?);
136
137    # XXX:  Do we need to clear out previously created bridge interfaces?
138
139    my $count = 0;
140    my @SSHCMD;
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
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.
156
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";
158
159        my $check = <$SSHCMD[$count]>;  # Make sure something ran...
160
161        &setup_bridging($tun, $bridge, $iface, $addr);
162        $count++;
163        $ssh_port_fwds = "";  # only do this on the first connection
164    }
165    close(IFFILE);
166} elsif ($remote) {
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
182        &setup_bridging($tun, $bridge, $iface, $addr);
183        $iter++;
184    }
185    close(IFFILE);
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);
190}
191
192print "all done!\n" if ($debug);
193exit;
194
195
196# Set up the bridging for the new stuff...
197
198sub setup_bridging($; $; $; $) {
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}
209
210# Set up tunnel info for DETER-like testbeds.
211
212sub 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.
262sub 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}
Note: See TracBrowser for help on using the repository browser.