1# freebsd-lib.pl 2# Networking functions for FreeBSD 3 4$virtual_netmask = "255.255.255.255"; # Netmask for virtual interfaces 5 6# active_interfaces() 7# Returns a list of currently ifconfig'd interfaces 8sub active_interfaces 9{ 10local(@rv, @lines, $l); 11my @boot = &boot_interfaces(); 12my %boot = map { $_->{'address'}, $_ } @boot; 13my %bootname = map { $_->{'fullname'}, $_ } @boot; 14&open_execute_command(IFC, "ifconfig -a", 1, 1); 15while(<IFC>) { 16 s/\r|\n//g; 17 if (/^\S+:/) { push(@lines, $_); } 18 elsif (@lines) { $lines[$#lines] .= $_; } 19 } 20close(IFC); 21foreach $l (@lines) { 22 my %ifc; 23 $l =~ /^([^:\s]+):/; 24 $ifc{'name'} = $ifc{'fullname'} = $1; 25 if ($l =~ /^(\S+):(\d+):\s/) { $ifc{'virtual'} = $2; } 26 my $bootiface = $bootname{$ifc{'fullname'}}; 27 my $bootip = $bootiface ? $bootiface->{'address'} : undef; 28 if ($l =~ s/inet\s+($bootip)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)// || 29 $l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)//) { 30 $ifc{'address'} = $1; 31 $ifc{'netmask'} = &parse_hex($2); 32 $ifc{'broadcast'} = $3; 33 } 34 elsif ($l =~ s/inet\s+($bootip)\s+netmask\s+(\S+)// || 35 $l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)//) { 36 $ifc{'address'} = $1; 37 $ifc{'netmask'} = &parse_hex($2); 38 } 39 else { next; } 40 if ($l =~ /ether\s+(\S+)/) { $ifc{'ether'} = $1; } 41 if ($l =~ /mtu\s+(\S+)/) { $ifc{'mtu'} = $1; } 42 $ifc{'up'}++ if ($l =~ /\<UP/); 43 $ifc{'edit'} = &iface_type($ifc{'name'}) =~ /ethernet|loopback/i; 44 $ifc{'index'} = scalar(@rv); 45 if ($ifc{'ether'}) { 46 $ifc{'ether'} = join(":", map { length($_) == 1 ? "0".$_ : $_ } 47 split(/:/, $ifc{'ether'})); 48 } 49 push(@rv, \%ifc); 50 51 # Add v6 addresses 52 my (@address6, @netmask6, @scope6); 53 while($l =~ s/inet6\s+([0-9a-f:]+)(%\S+)?\s+prefixlen\s+(\d+)(\s+scopeid\s+(\S+))?//) { 54 push(@address6, $1); 55 push(@netmask6, $3); 56 push(@scope6, $5); 57 } 58 $ifc{'address6'} = \@address6; 59 $ifc{'netmask6'} = \@netmask6; 60 $ifc{'scope6'} = \@scope6; 61 62 # Add aliases as virtual interfaces. Try to match boot-time interface 63 # numbers where possible 64 my %vtaken = map { $_->{'virtual'}, 1 } 65 grep { $_->{'name'} eq $vifc{'name'} && 66 $_->{'virtual'} ne "" } @boot; 67 while($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)(\s+broadcast\s+(\S+))?//) { 68 my %vifc = %ifc; 69 $vifc{'address'} = $1; 70 $vifc{'netmask'} = &parse_hex($2); 71 $vifc{'broadcast'} = $4; 72 $vifc{'up'} = 1; 73 $vifc{'edit'} = $ifc{'edit'}; 74 my $boot = $boot{$vifc{'address'}}; 75 if ($boot) { 76 $vifc{'virtual'} = $boot->{'virtual'}; 77 } 78 else { 79 for($vifc{'virtual'}=0; $vtaken{$vifc{'virtual'}}; 80 $vifc{'virtual'}++) { } 81 } 82 $vtaken{$vifc{'virtual'}}++; 83 $vifc{'fullname'} = $vifc{'name'}.':'.$vifc{'virtual'}; 84 $vifc{'index'} = scalar(@rv); 85 push(@rv, \%vifc); 86 } 87 } 88return @rv; 89} 90 91# activate_interface(&details) 92# Create or modify an interface 93sub activate_interface 94{ 95my ($iface) = @_; 96my %act = map { $_->{'fullname'}, $_ } &active_interfaces(); 97my $old = $act{$iface->{'fullname'}}; 98$act{$iface->{'fullname'}} = $iface; 99&interface_sync(\%act, $iface->{'name'}, $iface->{'fullname'}); 100 101if ($iface->{'virtual'} eq '') { 102 # Remove old IPv6 addresses 103 my $l = &backquote_command("ifconfig ".quotemeta($iface->{'name'})); 104 while($l =~ s/inet6\s*(\S+)\s+prefixlen\s+(\d+)//) { 105 my $cmd = "ifconfig $iface->{'name'} inet6 $1 -alias 2>&1"; 106 $out = &backquote_logged($cmd); 107 &error("Failed to remove old IPv6 address : $out") if ($?); 108 } 109 110 # Add IPv6 addresses 111 for(my $i=0; $i<@{$iface->{'address6'}}; $i++) { 112 my $cmd = "ifconfig $iface->{'name'} inet6 ". 113 $iface->{'address6'}->[$i]. 114 " prefixlen ".$iface->{'netmask6'}->[$i]." 2>&1"; 115 $out = &backquote_logged($cmd); 116 &error("Failed to add IPv6 address : $out") if ($?); 117 } 118 } 119} 120 121# deactivate_interface(&details) 122# Deactive an interface 123sub deactivate_interface 124{ 125my ($iface) = @_; 126my @act = &active_interfaces(); 127if ($iface->{'virtual'} eq '') { 128 @act = grep { $_->{'name'} ne $iface->{'name'} } @act; 129 } 130else { 131 @act = grep { $_->{'fullname'} ne $iface->{'fullname'} } @act; 132 } 133my %act = map { $_->{'fullname'}, $_ } @act; 134&interface_sync(\%act, $iface->{'name'}, $iface->{'fullname'}); 135} 136 137# interface_sync(&interfaces-hash, name, changee) 138# Given a hash from interface name to details, make them live. This is needed 139# because on FreeBSD, alias interfaces are just IPs on the main interface, 140# rather than separate eth0:N interfaces like on Linux. 141sub interface_sync 142{ 143my ($act, $name, $change) = @_; 144 145# Remove all IP addresses except for the primary one (unless it is being edited) 146my $pri = $act->{$name}; 147my $ifconfig = &has_command("ifconfig"); 148while(1) { 149 my $out; 150 &execute_command("$ifconfig ".quotemeta($name), undef, \$out); 151 last if ($out !~ /([\000-\377]*)\s+inet\s+(\d+\.\d+\.\d+\.\d+)/); 152 last if ($2 eq $pri->{'address'} && $change ne $pri->{'fullname'}); 153 &system_logged("$ifconfig ".quotemeta($name)." delete $2 >/dev/null 2>&1"); 154 } 155 156# Add them back again, except for the primary unless it is being changed 157foreach $a (sort { $a->{'fullname'} cmp $b->{'fullname'} } 158 grep { $_->{'name'} eq $name } values(%$act)) { 159 next if ($a->{'fullname'} eq $pri->{'fullname'} && 160 $change ne $pri->{'fullname'}); 161 my $cmd = "$ifconfig ".quotemeta($a->{'name'}); 162 if ($a->{'virtual'} ne '') { 163 $cmd .= " alias $a->{'address'}"; 164 } 165 else { 166 $cmd .= " $a->{'address'}"; 167 } 168 if ($a->{'netmask'}) { $cmd .= " netmask $a->{'netmask'}"; } 169 if ($a->{'broadcast'}) { $cmd .= " broadcast $a->{'broadcast'}"; } 170 if ($a->{'mtu'}) { $cmd .= " mtu $a->{'mtu'}"; } 171 my $out = &backquote_logged("$cmd 2>&1"); 172 &error($out) if ($?); 173 if ($a->{'virtual'} eq '') { 174 if ($a->{'up'}) { 175 $out = &backquote_logged( 176 "$ifconfig ".quotemeta($a->{'name'})." up 2>&1"); 177 } 178 else { 179 $out = &backquote_logged( 180 "$ifconfig ".quotemeta($a->{'name'})." down 2>&1"); 181 } 182 &error($out) if ($?); 183 } 184 } 185 186} 187 188# boot_interfaces() 189# Returns a list of interfaces brought up at boot time 190sub boot_interfaces 191{ 192my %rc = &get_rc_conf(); 193my @rv; 194foreach my $r (keys %rc) { 195 my $v = $rc{$r}; 196 my %ifc; 197 if ($r =~ /^ifconfig_([a-z0-9]+)$/) { 198 # Non-virtual interface 199 %ifc = ( 'name' => $1, 200 'fullname' => $1 ); 201 } 202 elsif ($r =~ /^ifconfig_([a-z0-9]+)_alias(\d+)$/) { 203 # Virtual interface 204 %ifc = ( 'name' => $1, 205 'virtual' => $2, 206 'fullname' => "$1:$2" ); 207 } 208 else { next; } 209 210 if ($v =~ /^inet\s+(\S+)/ || /^([0-9\.]+)/) { 211 $ifc{'address'} = $1; 212 } 213 elsif ($v eq 'DHCP') { 214 $ifc{'dhcp'} = 1; 215 } 216 my @a = split(/\./, $ifc{'address'}); 217 if ($v =~ /netmask\s+(0x\S+)/) { 218 $ifc{'netmask'} = &parse_hex($1); 219 } 220 elsif ($v =~ /netmask\s+([0-9\.]+)/) { 221 $ifc{'netmask'} = $1; 222 } 223 else { 224 $ifc{'netmask'} = $a[0] >= 192 ? "255.255.255.0" : 225 $a[0] >= 128 ? "255.255.0.0" : 226 "255.0.0.0"; 227 } 228 if ($v =~ /broadcast\s+(0x\S+)/) { 229 $ifc{'broadcast'} = &parse_hex($1); 230 } 231 elsif ($v =~ /broadcast\s+([0-9\.]+)/) { 232 $ifc{'broadcast'} = $1; 233 } 234 else { 235 my @n = split(/\./, $ifc{'netmask'}); 236 $ifc{'broadcast'} = sprintf "%d.%d.%d.%d", 237 ($a[0] | ~int($n[0]))&0xff, 238 ($a[1] | ~int($n[1]))&0xff, 239 ($a[2] | ~int($n[2]))&0xff, 240 ($a[3] | ~int($n[3]))&0xff; 241 } 242 $ifc{'mtu'} = $1 if ($v =~ /mtu\s+(\d+)/); 243 $ifc{'up'} = 1; 244 $ifc{'edit'} = 1; 245 $ifc{'index'} = scalar(@rv); 246 $ifc{'file'} = "/etc/rc.conf"; 247 248 # Check for IPv6 params 249 my $v6 = $rc{'ipv6_ifconfig_'.$ifc{'fullname'}}; 250 if ($v6 =~ /^inet6\s+(\S+)/ || $v6 =~ /^([0-9a-f:]+)/) { 251 $ifc{'address6'} = [ $1 ]; 252 } 253 elsif (!$v6 && $rc{'ipv6_enable'}) { 254 $ifc{'auto6'} = 1; 255 } 256 if ($v6 =~ /prefixlen\s+(\d+)/) { 257 $ifc{'netmask6'} = [ $1 ]; 258 } 259 260 # Add IPv6 aliases 261 foreach my $rr (sort { $a cmp $b } keys %rc) { 262 if ($rr =~ /^ipv6_ifconfig_(\S+)_alias\d+$/ && 263 $1 eq $ifc{'fullname'}) { 264 my $v6 = $rc{$rr}; 265 if ($v6 =~ /^inet6\s+(\S+)/ || $v6 =~ /^([0-9a-f:]+)/) { 266 push(@{$ifc{'address6'}}, $1); 267 } 268 if ($v6 =~ /prefixlen\s+(\d+)/) { 269 push(@{$ifc{'netmask6'}}, $1); 270 } 271 } 272 } 273 274 push(@rv, \%ifc); 275 } 276return @rv; 277} 278 279# save_interface(&details) 280# Create or update a boot-time interface 281sub save_interface 282{ 283my $str; 284if ($_[0]->{'dhcp'}) { 285 $str = "DHCP"; 286 } 287else { 288 $str = "inet $_[0]->{'address'}"; 289 $str .= " netmask $_[0]->{'netmask'}" if ($_[0]->{'netmask'}); 290 $str .= " broadcast $_[0]->{'broadcast'}" if ($_[0]->{'broadcast'}); 291 } 292&lock_file("/etc/rc.conf"); 293if ($_[0]->{'virtual'} eq '') { 294 &save_rc_conf('ifconfig_'.$_[0]->{'name'}, $str); 295 } 296else { 297 my @boot = &boot_interfaces(); 298 my ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot; 299 if (!$old && $_[0]->{'virtual'} ne '') { 300 # A new virtual interface .. pick a virtual number automaticlly 301 my $b; 302 $_[0]->{'virtual'} = 0; 303 foreach $b (&boot_interfaces()) { 304 if ($b->{'name'} eq $_[0]->{'name'} && 305 $b->{'virtual'} ne '' && 306 $b->{'virtual'} >= $_[0]->{'virtual'}) { 307 $_[0]->{'virtual'} = $b->{'virtual'}+1; 308 } 309 } 310 $_[0]->{'fullname'} = $_[0]->{'name'}.':'.$_[0]->{'virtual'}; 311 } 312 &save_rc_conf('ifconfig_'.$_[0]->{'name'}.'_alias'.$_[0]->{'virtual'}, 313 $str); 314 } 315 316# Update IPv6 settings 317if ($_[0]->{'virtual'} eq '') { 318 my @a = @{$_[0]->{'address6'}}; 319 my @n = @{$_[0]->{'netmask6'}}; 320 if (@a || $_[0]->{'auto6'}) { 321 &save_rc_conf('ipv6_enable', 'YES'); 322 } 323 if (@a) { 324 &save_rc_conf('ipv6_ifconfig_'.$_[0]->{'name'}, 325 $a[0].' prefixlen '.$n[0]); 326 } 327 else { 328 &save_rc_conf('ipv6_ifconfig_'.$_[0]->{'name'}); 329 } 330 331 # Delete any IPv6 aliases 332 my %rc = &get_rc_conf(); 333 foreach my $r (keys %rc) { 334 if ($r =~ /^ipv6_ifconfig_(\S+)_alias\d+$/ && 335 $1 eq $_[0]->{'fullname'}) { 336 &save_rc_conf($r); 337 } 338 } 339 340 # Re-create IPv6 aliases 341 shift(@a); 342 shift(@n); 343 for(my $i=0; $i<@a; $i++) { 344 &save_rc_conf( 345 "ipv6_ifconfig_".$_[0]->{'fullname'}."_alias".$i, 346 $a[$i]." prefixlen ".$n[$i]); 347 } 348 } 349 350&unlock_file("/etc/rc.conf"); 351} 352 353# delete_interface(&details, [noshift]) 354# Delete a boot-time interface 355sub delete_interface 356{ 357&lock_file("/etc/rc.conf"); 358if ($_[0]->{'virtual'} eq '') { 359 # Remove the real interface 360 &save_rc_conf('ifconfig_'.$_[0]->{'name'}); 361 &save_rc_conf('ipv6_ifconfig_'.$_[0]->{'name'}); 362 # XXX ipv6 too 363 } 364else { 365 # Remove a virtual interface, and shift down all aliases above it 366 &save_rc_conf('ifconfig_'.$_[0]->{'name'}.'_alias'.$_[0]->{'virtual'}); 367 if (!$_[1]) { 368 my ($b, %lastb); 369 foreach $b (&boot_interfaces()) { 370 if ($b->{'name'} eq $_[0]->{'name'} && 371 $b->{'virtual'} ne '' && 372 $b->{'virtual'} > $_[0]->{'virtual'}) { 373 # This one needs to be shifted down 374 %lastb = %$b; 375 $b->{'virtual'}--; 376 &save_interface($b); 377 } 378 } 379 &delete_interface(\%lastb, 1) if (%lastb); 380 } 381 } 382&unlock_file("/etc/rc.conf"); 383} 384 385# iface_type(name) 386# Returns a human-readable interface type name 387sub iface_type 388{ 389return $_[0] =~ /^tun/ ? "Loopback tunnel" : 390 $_[0] =~ /^sl/ ? "SLIP" : 391 $_[0] =~ /^ppp/ ? "PPP" : 392 $_[0] =~ /^lo/ ? "Loopback" : 393 $_[0] =~ /^ar/ ? "Arnet" : 394 $_[0] =~ /^(wlan|athi|ral)/ ? "Wireless ethernet" : 395 $_[0] =~ /^(bge|em|myk)/ ? "Gigabit ethernet" : 396 $_[0] =~ /^(ax|mx|nve|pn|rl|tx|wb|nfe|sis)/ ? "Fast ethernet" : 397 $_[0] =~ /^(cs|dc|de|ed|el|ex|fe|fxp|ie|le|lnc|tl|vr|vx|xl|ze|zp|re)/ ? "Ethernet" : $text{'ifcs_unknown'}; 398} 399 400# iface_hardware(name) 401# Does some interface have an editable hardware address 402sub iface_hardware 403{ 404return 0; 405} 406 407# can_edit(what) 408# Can some boot-time interface parameter be edited? 409sub can_edit 410{ 411return $_[0] =~ /netmask|broadcast|dhcp/; 412} 413 414sub can_broadcast_def 415{ 416return 0; 417} 418 419# valid_boot_address(address) 420# Is some address valid for a bootup interface 421sub valid_boot_address 422{ 423return &check_ipaddress($_[0]); 424} 425 426# get_dns_config() 427# Returns a hashtable containing keys nameserver, domain, search & order 428sub get_dns_config 429{ 430my $dns; 431&open_readfile(RESOLV, "/etc/resolv.conf"); 432while(<RESOLV>) { 433 s/\r|\n//g; 434 s/#.*$//g; 435 if (/nameserver\s+(.*)/) { 436 push(@{$dns->{'nameserver'}}, split(/\s+/, $1)); 437 } 438 elsif (/domain\s+(\S+)/) { 439 $dns->{'domain'} = [ $1 ]; 440 } 441 elsif (/search\s+(.*)/) { 442 $dns->{'domain'} = [ split(/\s+/, $1) ]; 443 } 444 } 445close(RESOLV); 446 447my @order; 448my $orderfile; 449if (-r "/etc/nsswitch.conf") { 450 # FreeBSD 5.0 and later use nsswitch.conf 451 $orderfile = "/etc/nsswitch.conf"; 452 &open_readfile(SWITCH, $orderfile); 453 while(<SWITCH>) { 454 s/\r|\n//g; 455 if (/^\s*hosts:\s+(.*)/) { 456 $dns->{'order'} = $1; 457 } 458 } 459 close(SWITCH); 460 } 461else { 462 # Older versions use host.conf 463 $orderfile = "/etc/host.conf"; 464 &open_readfile(HOST, $orderfile); 465 while(<HOST>) { 466 s/\r|\n//g; 467 s/#.*$//; 468 push(@order, $_) if (/\S/); 469 } 470 close(HOST); 471 $dns->{'order'} = join(" ", @order); 472 } 473$dns->{'files'} = [ "/etc/resolv.conf", $orderfile ]; 474return $dns; 475} 476 477# save_dns_config(&config) 478# Writes out the resolv.conf and host.conf files 479sub save_dns_config 480{ 481&lock_file("/etc/resolv.conf"); 482&open_readfile(RESOLV, "/etc/resolv.conf"); 483my @resolv = <RESOLV>; 484close(RESOLV); 485&open_tempfile(RESOLV, ">/etc/resolv.conf"); 486foreach (@{$_[0]->{'nameserver'}}) { 487 print RESOLV "nameserver $_\n"; 488 } 489if ($_[0]->{'domain'}) { 490 if ($_[0]->{'domain'}->[1]) { 491 &print_tempfile(RESOLV, "search ",join(" ", @{$_[0]->{'domain'}}),"\n"); 492 } 493 else { 494 &print_tempfile(RESOLV, "domain $_[0]->{'domain'}->[0]\n"); 495 } 496 } 497foreach (@resolv) { 498 &print_tempfile(RESOLV, $_) if (!/^\s*(nameserver|domain|search)\s+/); 499 } 500&close_tempfile(RESOLV); 501&unlock_file("/etc/resolv.conf"); 502 503if (-r "/etc/nsswitch.conf") { 504 # Save to new nsswitch.conf, for FreeBSD 5.0 and later 505 &lock_file("/etc/nsswitch.conf"); 506 &open_readfile(SWITCH, "/etc/nsswitch.conf"); 507 my @switch = <SWITCH>; 508 close(SWITCH); 509 &open_tempfile(SWITCH, ">/etc/nsswitch.conf"); 510 foreach (@switch) { 511 if (/^\s*hosts:\s+/) { 512 &print_tempfile(SWITCH, "hosts:\t$_[0]->{'order'}\n"); 513 } 514 else { 515 &print_tempfile(SWITCH, $_); 516 } 517 } 518 &close_tempfile(SWITCH); 519 &unlock_file("/etc/nsswitch.conf"); 520 } 521else { 522 # Save to older host.conf 523 &open_lock_tempfile(HOST, ">/etc/host.conf"); 524 foreach my $o (split(/\s+/, $_[0]->{'order'})) { 525 &print_tempfile(HOST, $o,"\n"); 526 } 527 &close_tempfile(HOST); 528 } 529} 530 531$max_dns_servers = 3; 532 533# order_input(&dns) 534# Returns HTML for selecting the name resolution order 535sub order_input 536{ 537if (-r "/etc/nsswitch.conf") { 538 # FreeBSD 5.0 and later use nsswitch.conf with more options 539 return &common_order_input("order", $_[0]->{'order'}, 540 [ [ "files", "Files" ], [ "dns", "DNS" ], 541 [ "nis", "NIS" ], [ "cache", "NSCD" ] ]); 542 } 543else { 544 # Older FreeBSD's have fewer options 545 my $dnsopt = $_[0]->{'order'} =~ /dns/ ? 'dns' : 'bind'; 546 return &common_order_input("order", $_[0]->{'order'}, 547 [ [ "hosts", "Hosts" ], [ $dnsopt, "DNS" ], [ "nis", "NIS" ] ]); 548 } 549} 550 551# parse_order(&dns) 552# Parses the form created by order_input() 553sub parse_order 554{ 555if (defined($in{'order'})) { 556 $in{'order'} =~ /\S/ || &error($text{'dns_eorder'}); 557 $_[0]->{'order'} = $in{'order'}; 558 } 559else { 560 local($i, @order); 561 for($i=0; defined($in{"order_$i"}); $i++) { 562 push(@order, $in{"order_$i"}) if ($in{"order_$i"}); 563 } 564 $_[0]->{'order'} = join(" ", @order); 565 } 566} 567 568# get_hostname() 569sub get_hostname 570{ 571my %rc = &get_rc_conf(); 572if ($rc{'hostname'}) { 573 return $rc{'hostname'}; 574 } 575return &get_system_hostname(); 576} 577 578# save_hostname(name) 579sub save_hostname 580{ 581my ($hostname) = @_; 582&lock_file("/etc/rc.conf"); 583&system_logged("hostname ".quotemeta($hostname)." >/dev/null 2>&1"); 584&save_rc_conf('hostname', $_[0]); 585&unlock_file("/etc/rc.conf"); 586undef(@main::get_system_hostname); # clear cache 587} 588 589sub routing_config_files 590{ 591return ( "/etc/defaults/rc.conf", "/etc/rc.conf" ); 592} 593 594sub routing_input 595{ 596my %rc = &get_rc_conf(); 597 598# Default router 599my $defr = $rc{'defaultrouter'}; 600print &ui_table_row($text{'routes_default'}, 601 &ui_opt_textbox("defr", $defr eq 'NO' ? '' : $defr, 20, 602 $text{'routes_none'})); 603 604if (&supports_address6()) { 605 # IPv6 efault router 606 my $defr = $rc{'ipv6_defaultrouter'}; 607 print &ui_table_row($text{'routes_default6'}, 608 &ui_opt_textbox("defr6", $defr eq 'NO' ? '' : $defr, 20, 609 $text{'routes_none'})); 610 } 611 612# Act as router? 613my $gw = $rc{'gateway_enable'}; 614print &ui_table_row($text{'routes_forward'}, 615 &ui_radio("gw", $gw || 'NO', [ [ 'YES', $text{'yes'} ], 616 [ 'NO', $text{'no'} ] ])); 617 618# Run route discovery 619my $rd = $rc{'router_enable'}; 620print &ui_table_row($text{'routes_routed'}, 621 &ui_radio("rd", $rd || 'NO', [ [ 'YES', $text{'yes'} ], 622 [ 'NO', $text{'no'} ] ])); 623} 624 625sub parse_routing 626{ 627&lock_file("/etc/rc.conf"); 628$in{'defr_def'} || &check_ipaddress($in{'defr'}) || 629 &error(&text('routes_edefault', &html_escape($in{'defr'}))); 630&save_rc_conf('defaultrouter', $in{'defr_def'} ? 'NO' : $in{'defr'}); 631if (&supports_address6()) { 632 $in{'defr6_def'} || &check_ip6address($in{'defr6'}) || 633 &error(&text('routes_edefault6', &html_escape($in{'defr6'}))); 634 &save_rc_conf('ipv6_defaultrouter', 635 $in{'defr6_def'} ? 'NO' : $in{'defr6'}); 636 } 637&save_rc_conf('gateway_enable', $in{'gw'}); 638&save_rc_conf('router_enable', $in{'rd'}); 639&unlock_file("/etc/rc.conf"); 640} 641 642# save_rc_conf(name, value) 643sub save_rc_conf 644{ 645my $found; 646&open_readfile(CONF, "/etc/rc.conf"); 647my @conf = <CONF>; 648close(CONF); 649&open_tempfile(CONF, ">/etc/rc.conf"); 650foreach (@conf) { 651 if (/^\s*([^=]+)\s*=\s*(.*)/ && $1 eq $_[0]) { 652 &print_tempfile(CONF, "$_[0]=\"$_[1]\"\n") if (@_ > 1); 653 $found++; 654 } 655 else { 656 &print_tempfile(CONF, $_); 657 } 658 } 659if (!$found && @_ > 1) { 660 &print_tempfile(CONF, "$_[0]=\"$_[1]\"\n"); 661 } 662&close_tempfile(CONF); 663} 664 665# get_rc_conf() 666sub get_rc_conf 667{ 668my ($file, %rv); 669foreach $file ("/etc/defaults/rc.conf", 670 glob("/etc/rc.conf.d/*"), 671 "/etc/rc.conf") { 672 &open_readfile(FILE, $file); 673 while(<FILE>) { 674 s/\r|\n//g; 675 s/#.*$//; 676 if (/^\s*([^=\s]+)\s*=\s*"(.*)"/ || 677 /^\s*([^=\s]+)\s*=\s*(\S+)/) { 678 $rv{$1} = $2; 679 } 680 } 681 close(FILE); 682 } 683return %rv; 684} 685 686# apply_network() 687# Apply the interface and routing settings 688sub apply_network 689{ 690my $oldpwd = &get_current_dir(); 691chdir("/"); 692 693# Take down all active alias interfaces, and any that no longer exist 694my %boot = map { $_->{'fullname'}, $_ } &boot_interfaces(); 695foreach my $i (&active_interfaces()) { 696 if ($i->{'virtual'} ne '' || !$boot{$i->{'fullname'}}) { 697 &deactivate_interface($i); 698 } 699 } 700# Bring everything up 701&system_logged("/etc/netstart >/dev/null 2>&1"); 702chdir($oldpwd); 703} 704 705# supports_address6([&iface]) 706# Returns 1 if managing IPv6 interfaces is supported 707sub supports_address6 708{ 709my ($iface) = @_; 710return $gconfig{'os_version'} >= 8; 711} 712 713# list_routes() 714# Returns a list of active routes 715sub list_routes 716{ 717my @rv; 718&open_execute_command(ROUTES, "netstat -rn", 1, 1); 719while(<ROUTES>) { 720 s/\s+$//; 721 if (/^([0-9\.]+|default)(\/\d+)?\s+([0-9\.]+|link\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)/) { 722 my $r = { 'dest' => $1, 723 'gateway' => $3, 724 'netmask' => $2, 725 'iface' => $4 }; 726 if ($r->{'gateway'} =~ /^link/) { 727 $r->{'gateway'} = '0.0.0.0'; 728 } 729 if ($r->{'dest'} eq 'default' && 730 &check_ip6address($r->{'gateway'})) { 731 $r->{'dest'} = '::'; 732 } 733 elsif ($r->{'dest'} eq 'default') { 734 $r->{'dest'} = '0.0.0.0'; 735 } 736 if ($r->{'netmask'} =~ /^\/(\d+)$/) { 737 $r->{'netmask'} = &prefix_to_mask($1); 738 } 739 push(@rv, $r); 740 } 741 } 742close(ROUTES); 743return @rv; 744} 745 746# get_default_gateway() 747# Returns the default gateway IP (if one is set) and device (if set) boot time 748# settings. 749sub get_default_gateway 750{ 751my %rc = &get_rc_conf(); 752return ( $rc{'defaultrouter'} eq 'NO' ? undef : $rc{'defaultrouter'}, 753 undef ); 754} 755 756# set_default_gateway(gateway, device) 757# Sets the default gateway to the given IP accessible via the given device, 758# in the boot time settings. 759sub set_default_gateway 760{ 761my ($gw, $gwdev) = @_; 762&lock_file("/etc/rc.conf"); 763&save_rc_conf('defaultrouter', $gw || "NO"); 764&unlock_file("/etc/rc.conf"); 765} 766 7671; 768 769