All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

fixarpinfo 9.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
#!/usr/bin/perl -wT
#
# Copyright (c) 2012 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# Wire down ARP entries for "nodes of interest".
#

use English;
use Getopt::Std;

sub usage()
{
    print STDERR "Usage: fixarpinfo [-sucn]\n";
    print STDERR "  Sets, updates or clears static ARP table entries on the control net interface.\n";
    print STDERR "  The default update action compares existing ARP entries with\n";
    print STDERR "  the desired set (from boss) and makes incremental changes.\n";
    print STDERR "  -s    set static arp entries\n";
    print STDERR "  -u    update existing static arp entries (default)\n";
    print STDERR "  -c    clear all arp entries and disable staticarp if set\n";
    print STDERR "  -n    do not change anything, just report what would be done\n";
    print STDERR "  -v    enable verbose mode\n";
    exit(1);
}
my $optlist = "sucnv";
my $iface = "";
my $myip = "";
my $doit = 1;
my $verbose = 0;
my $action = "update";

# Turn off line buffering on output
$| = 1;

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Script specific goo.

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use liblocsetup;
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
    if (!(SUBBOSS() || CONTROL()));

# XXX
my $os = `uname`;
chomp($os);
exit(0)
    if ($os ne "FreeBSD");

# Protos.
sub doset();
sub doclear();
sub doupdate();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'n'})) {
   $doit = 0;
}
if (defined($options{'v'})) {
    $verbose = 1;
}
if (defined($options{'s'})) {
   $action = "set";
}
if (defined($options{'c'})) {
   $action = "clear";
}
if (defined($options{'u'})) {
   $action = "update";
}

# Only root can actual perform the operations.
if ($doit && $EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

# Figure out the control net interface
if (-e "$BOOTDIR/controlif") {
    $iface = `cat $BOOTDIR/controlif`;
    chomp($iface);
}
if ($iface =~ /^(.*)$/) {
    $iface = $1;
} else {
    fatal("arpinfo could not identify control net iface\n");
}

# and our own IP
$myip = os_get_ctrlnet_ip();
if (!$myip) {
    fatal("arpinfo could not identify control net address\n");
}


print STDERR libsetup::TBTimeStampWithDate() . ": arpinfo $action\n";

# Execute the action.
SWITCH: for ($action) {
    /^set$/i && do {
	doset();
	last SWITCH;
    };
    /^clear$/i && do {
    	doclear();
	last SWITCH;
    };
    /^update$/i && do {
	doupdate();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

#
# Unconditionally set static ARP entries.
# All old entries are cleared and the desired set is installed.
# -staticarp is turned on.
#
sub doset()
{
    #
    # Get desired ARP info from boss.
    #
    # If either we fail to make the call or get nothing back, don't
    # change anything. The former might indicate that someone is spoofing
    # boss (though probably not) so we die with an error to bring attention
    # to the issue.
    #
    my %arpinfo = ();
    my $atype = getarpinfo(\%arpinfo);
    if (!$atype) {
	fatal("fixarpinfo: Could not get arpinfo from libsetup!\n");
    }
    if (scalar(keys %arpinfo) == 0) {
	print STDERR "fixarpinfo: WARNING: ".
		     "got no ARP info from boss, leaving ARP table alone.\n";
	return;
    }

    #
    # XXX at least on FreeBSD, we cannot remove the arp entry for the
    # local IF, so attempting to create it below would cause failure.
    #
    delete($arpinfo{$myip});

    # Out with the old...
    if (!$doit) {
	print STDERR "Would do: arp -i $iface -da\n";
    } else {
	print STDERR "  Removing all arp entries\n"
	    if ($verbose);
	if (os_removearpentry($iface)) {
	    fatal("Error removing old ARP entries!\n");
	}
    }

    # ...and in with the new.
    my $err = 0;
    foreach my $ip (keys %arpinfo) {
	my $mac = $arpinfo{$ip}{'mac'};
	if (!$doit) {
	    print STDERR "Would do: arp -i $iface -s $ip $mac\n";
	} else {
	    print STDERR "  Adding arp entry for $ip ($mac) on $iface\n"
		if ($verbose);
	    if (os_createarpentry($iface, $ip, $mac)) {
		$err++;
	    }
	}
    }
    if ($err) {
	fatal("Could not create one or more ARP entries!\n");
    }

    #
    # Set/clear static-only setting.
    #
    my $setit = ($atype eq "staticonly") ? 1 : 0;
    if (!$doit) {
	print STDERR "Would do: ifconfig $iface ",
	      $setit ? "staticarp" : "-staticarp", "\n";
    } else {
	print STDERR "  " . ($setit ? "En" : "Dis") . "abling staticarp\n"
	    if ($verbose);
	if (os_setstaticarp($iface, $setit)) {
	    fatal("Could not turn " . ($setit ? "on" : "off") . " static ARP!\n");
	}
    }
}

sub doclear()
{
    #
    # XXX always clear static arp here or we would not be able to
    # talk to anyone!
    #
    if (!$doit) {
	print STDERR "Would do: ifconfig $iface -staticarp\n";
    } else {
	print STDERR "  Disabling staticarp\n"
	    if ($verbose);
	if (os_setstaticarp($iface, 0)) {
	    fatal("Could not turn off static ARP!\n");
	}
    }

    if (!$doit) {
	print STDERR "Would do: arp -i $iface -da\n";
    } else {
	print STDERR "  Removing all arp entries\n"
	    if ($verbose);
	if (os_removearpentry($iface)) {
	    fatal("Error removing old ARP entries!\n");
	}
    }
}

sub doupdate()
{
    #
    # Read current info from the kernel.
    #
    my %oldinfo = ();
    if (os_getarpinfo($iface, \%oldinfo)) {
	fatal("Could not get current arpinfo!\n");
    }

    # XXX don't touch our cnet entry
    delete($oldinfo{$myip});

    # easy #1: no old, just install new
    if (scalar(keys %oldinfo) == 0) {
	doset();
	return;
    }

    #
    # Get desired ARP info from boss.
    #
    # If either we fail to make the call or get nothing back, don't
    # change anything. The former might indicate that someone is spoofing
    # boss (though probably not) so we die with an error to bring attention
    # to the issue.
    #
    my %newinfo = ();
    my $atype = getarpinfo(\%newinfo);
    if (!$atype) {
	fatal("Could not get arpinfo from tmcc!\n");
    }
    if (scalar(keys %newinfo) == 0) {
	print STDERR "fixarpinfo: WARNING: ".
		     "got no ARP info from boss, leaving ARP table alone.\n";
	return;
    }
    delete($newinfo{$myip});

    # make sure static arp is enabled or disabled as appropriate
    my $setit = ($atype eq "staticonly") ? 1 : 0;
    if (!$doit) {
	print STDERR "Would do: ifconfig $iface ",
	      $setit ? "staticarp" : "-staticarp", "\n";
    } else {
	print STDERR "  " . ($setit ? "En" : "Dis") . "abling staticarp\n"
	    if ($verbose);
	if (os_setstaticarp($iface, $setit)) {
	    fatal("Could not turn " . ($setit ? "on" : "off") . " static ARP!\n");
	}
    }

    # easy #2: no new, just remove all old entries
    if (scalar(keys %newinfo) == 0) {
	my $err = 0;
	foreach my $ip (keys %oldinfo) {
	    if (!$doit) {
		print STDERR "Would do: arp -i $iface -d $ip\n";
	    } else {
		my $omac = $oldinfo{$ip}{'mac'};
		print STDERR "  Removing arp entry for $ip ($omac) on $iface\n"
		    if ($verbose);
		#
		# XXX not all current entries may be permanent, so they
		# may go away before we remove them here. Hence, no errors.
		#
		os_removearpentry($iface, $ip);
	    }
	}
    }

    #
    # Otherwise, we must reconcile the old and new lists. Three cases:
    # 1. In the old list but not the new, remove the entry
    # 2. In the new list but not the old, add the entry
    # 3. In both lists, modify if necessary
    #
    foreach my $ip (keys %oldinfo) {
	# case #1: remove arp entry
	if (!exists($newinfo{$ip})) {
	    if (!$doit) {
		print STDERR "Would do: arp -i $iface -d $ip\n";
	    } else {
		my $omac = $oldinfo{$ip}{'mac'};
		print STDERR "  Removing arp entry for $ip ($omac) on $iface\n"
		    if ($verbose);
		os_removearpentry($iface, $ip);
	    }
	}

	# case #3a: mapping has changed, remove the old and insert the new
	elsif ($oldinfo{$ip}{'mac'} ne $newinfo{$ip}{'mac'} ||
	    $oldinfo{$ip}{'static'} == 0) {
	    my $mac = $newinfo{$ip}{'mac'};

	    if ($doit && $verbose) {
		my $omac = $oldinfo{$ip}{'mac'};
		print STDERR "  Replacing arp entry for $ip ($omac -> $mac) on $iface\n";
	    }
	    if (!$doit) {
		print STDERR "Would do: arp -i $iface -d $ip\n";
	    } else {
		os_removearpentry($iface, $ip);
	    }
	    if (!$doit) {
		print STDERR "Would do: arp -i $iface -s $ip $mac\n";
	    } else {
		os_createarpentry($iface, $ip, $mac);
	    }

	    # remove from the list so we don't add it again
	    delete $newinfo{$ip};
	}

	# case #3b: nothing has changed between old and new, do nothing
	else {
	    delete $newinfo{$ip};
	}
    }

    # anything left in newinfo is new stuff to add (case #2)
    foreach my $ip (keys %newinfo) {
	my $mac = $newinfo{$ip}{'mac'};

	if (!$doit) {
	    print STDERR "Would do: arp -i $iface -s $ip $mac\n";
	} else {
	    print STDERR "  Adding arp entry for $ip ($mac) on $iface\n"
		if ($verbose);
	    os_createarpentry($iface, $ip, $mac);
	}
    }
}