source: fedkit/fed-tun.pl @ cd3eceb

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

Ouch, tighten up handling of the options I read in from the configuration
file; Perl isn't as flexible about true and false as I thought.

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