interfaces: audit find_interface_ipv6() and callers

This commit is contained in:
Franco Fichtner 2018-09-03 22:33:01 +02:00
parent 18dcbcbd72
commit 92fc44c365
4 changed files with 100 additions and 88 deletions

View File

@ -661,7 +661,7 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive
$gateway = array();
$gateway['dynamic'] = false;
$gateway['ipprotocol'] = "inet";
$gateway['ipprotocol'] = 'inet';
$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
$gateway['interface'] = get_real_interface($ifname);
$gateway['friendlyiface'] = $ifname;
@ -757,9 +757,9 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive
$gateway = array();
$gateway['dynamic'] = false;
$gateway['ipprotocol'] = "inet6";
$gateway['ipprotocol'] = 'inet6';
$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
$gateway['interface'] = get_real_interface($ifname, "inet6");
$gateway['interface'] = get_real_interface($ifname, 'inet6');
switch ($ifcfg['ipaddrv6']) {
case "6rd":
case "6to4":
@ -818,16 +818,16 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive
$gateway['gateway'] = get_interface_gateway($gateway['interface']);
/* no IP address found, set to dynamic */
if (!is_ipaddrv4($gateway['gateway'])) {
$gateway['gateway'] = "dynamic";
$gateway['gateway'] = 'dynamic';
}
$gateway['dynamic'] = true;
} elseif ($gateway['ipprotocol'] == "inet6") {
} elseif ($gateway['ipprotocol'] == 'inet6') {
/* if the gateway is dynamic and we can find the IPv6, Great! */
/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
/* no IPv6 address found, set to dynamic */
if (!is_ipaddrv6($gateway['gateway'])) {
$gateway['gateway'] = "dynamic";
$gateway['gateway'] = 'dynamic';
}
$gateway['dynamic'] = true;
}
@ -840,13 +840,10 @@ function return_gateways_array($disabled = false, $localhost = false, $inactive
}
$gateway['friendlyiface'] = $gateway['interface'];
/* special treatment for tunnel interfaces */
if ($gateway['ipprotocol'] == "inet6") {
$gateway['interface'] = get_real_interface($gateway['interface'], 'inet6');
} else {
$gateway['interface'] = get_real_interface($gateway['interface'], 'all');
}
$gateway['interface'] = get_real_interface(
$gateway['interface'],
$gateway['ipprotocol'] != 'inet6' ? 'all' : 'inet6'
);
/* entry has a default flag, use it */
if (isset($gateway['defaultgw'])) {
@ -1081,64 +1078,68 @@ function get_interface_gateway($interface, &$dynamic = false)
{
global $config;
$gw = NULL;
$gw = null;
if (isset($config['interfaces'][$interface])) {
$gwcfg = $config['interfaces'][$interface];
if (!empty($gwcfg['gateway']) && isset($config['gateways']['gateway_item'])) {
foreach ($config['gateways']['gateway_item'] as $gateway) {
if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
$gw = $gateway['gateway'];
break;
}
}
}
// for dynamic interfaces we handle them through the $interface_router file.
if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) {
$realif = get_real_interface($interface);
if (file_exists("/tmp/{$realif}_router")) {
$gw = trim(file_get_contents("/tmp/{$realif}_router"), " \n");
$dynamic = true;
}
if (file_exists("/tmp/{$realif}_defaultgw")) {
$dynamic = "default";
}
}
if (!isset($config['interfaces'][$interface])) {
return $gw;
}
/* return gateway */
return ($gw);
}
function get_interface_gateway_v6($interface, &$dynamic = false)
{
global $config;
$gw = NULL;
$gwcfg = $config['interfaces'][$interface];
if (!empty($gwcfg['gatewayv6']) && isset($config['gateways']['gateway_item'])) {
if (!empty($gwcfg['gateway']) && isset($config['gateways']['gateway_item'])) {
foreach ($config['gateways']['gateway_item'] as $gateway) {
if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
$gw = $gateway['gateway'];
break;
}
}
}
// for dynamic interfaces we handle them through the $interface_router file.
if (!is_ipaddrv6($gw) && ( !isset($gwcfg['ipaddrv6']) || !is_ipaddrv6($gwcfg['ipaddrv6']))) {
$realif = get_real_interface($interface);
if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) {
$realif = get_real_interface($interface);
if (file_exists("/tmp/{$realif}_router")) {
$gw = trim(file_get_contents("/tmp/{$realif}_router"), " \n");
$dynamic = true;
}
if (file_exists("/tmp/{$realif}_defaultgw")) {
$dynamic = 'default';
}
}
return $gw;
}
function get_interface_gateway_v6($interface, &$dynamic = false)
{
global $config;
$gw = null;
if (!isset($config['interfaces'][$interface])) {
return $gw;
}
$gwcfg = $config['interfaces'][$interface];
if (!empty($gwcfg['gatewayv6']) && isset($config['gateways']['gateway_item'])) {
foreach ($config['gateways']['gateway_item'] as $gateway) {
if ($gateway['name'] == $gwcfg['gatewayv6'] && is_ipaddrv6($gateway['gateway'])) {
$gw = $gateway['gateway'];
break;
}
}
}
if (!is_ipaddrv6($gw) && (!isset($gwcfg['ipaddrv6']) || !is_ipaddrv6($gwcfg['ipaddrv6']))) {
$realif = get_real_interface($interface, 'inet6');
if (file_exists("/tmp/{$realif}_routerv6")) {
$gw = trim(file_get_contents("/tmp/{$realif}_routerv6"), " \n");
$dynamic = true;
}
if (file_exists("/tmp/{$realif}_defaultgwv6")) {
$dynamic = "default";
$dynamic = 'default';
}
}
/* return gateway */
return ($gw);
return $gw;
}
/*

View File

@ -1087,8 +1087,10 @@ function interface_bring_down($interface = "wan", $ifacecfg = false)
$pfctlflush[$realif] = 1;
}
/* apparently deletes the first IP it finds */
mwexecf('/sbin/ifconfig %s delete', $realif);
$ip4 = find_interface_ip($realif, true);
if (is_ipaddrv4($ip4)) {
mwexecf('/sbin/ifconfig %s delete %s', array($realif, $ip4));
}
}
if (does_interface_exist($realifv6)) {
@ -1098,7 +1100,7 @@ function interface_bring_down($interface = "wan", $ifacecfg = false)
$pfctlflush[$realifv6] = 1;
}
$ip6 = find_interface_ipv6($interface);
$ip6 = find_interface_ipv6($realifv6, true);
if (is_ipaddrv6($ip6)) {
mwexecf('/sbin/ifconfig %s inet6 %s delete', array($realifv6, $ip6));
}
@ -2622,7 +2624,7 @@ function interface_track6_6rd_configure($interface = 'lan', $lancfg)
/* convert the 128 bits for the lan address back into a valid IPv6 address */
$rd6lan = convert_128bit_to_ipv6($rd6lanbin) ."1";
$lanif = get_real_interface($interface);
$lanif = get_real_interface($interface, 'inet6');
$oip = find_interface_ipv6($lanif);
if (is_ipaddrv6($oip)) {
mwexec("/sbin/ifconfig {$lanif} inet6 {$oip} delete");
@ -2662,7 +2664,7 @@ function interface_track6_6to4_configure($interface = 'lan', $lancfg)
/* convert the 128 bits for the lan address back into a valid IPv6 address */
$sixto4lan = convert_128bit_to_ipv6($sixto4lanbin) ."1";
$lanif = get_real_interface($interface);
$lanif = get_real_interface($interface, 'inet6');
$oip = find_interface_ipv6($lanif);
if (is_ipaddrv6($oip)) {
mwexec("/sbin/ifconfig {$lanif} inet6 {$oip} delete");
@ -3884,42 +3886,51 @@ function link_interface_to_gif($interface)
/*
* find_interface_ip($interface): return the interface ip (first found)
*/
function find_interface_ip($interface)
function find_interface_ip($interface, $exists = false)
{
$interface = trim($interface);
if (does_interface_exist($interface)) {
$ifinfo = legacy_get_interface_addresses($interface);
if (isset($ifinfo['ipaddr'])) {
return $ifinfo['ipaddr'];
}
if (!$exists && !does_interface_exist($interface)) {
return null;
}
$ifinfo = legacy_get_interface_addresses($interface);
if (isset($ifinfo['ipaddr'])) {
return $ifinfo['ipaddr'];
}
return null;
}
/*
* find_interface_ipv6($interface): return the interface ip (first found)
*/
function find_interface_ipv6($interface)
function find_interface_ipv6($interface, $exists = false)
{
// a bit obscure, why should this be different then find_interface_ip?
$interface = get_real_interface(trim($interface));
if (does_interface_exist($interface)) {
$ifinfo = legacy_get_interface_addresses($interface);
if (isset($ifinfo['ipaddr6'])) {
return $ifinfo['ipaddr6'];
}
$interface = trim($interface);
if (!$exists && !does_interface_exist($interface)) {
return null;
}
$ifinfo = legacy_get_interface_addresses($interface);
if (isset($ifinfo['ipaddr6'])) {
return $ifinfo['ipaddr6'];
}
return null;
}
/*
* find_interface_ipv6_ll($interface): return the interface ipv6 link local (first found)
*/
function find_interface_ipv6_ll($interface)
function find_interface_ipv6_ll($interface, $exists = false)
{
$interface = trim($interface);
if (!does_interface_exist($interface)) {
if (!$exists && !does_interface_exist($interface)) {
return null;
}
@ -3991,7 +4002,7 @@ function get_interface_ip($interface = 'wan')
{
$realif = get_failover_interface($interface);
if (!$realif) {
if (strstr($interface, "_vip")) {
if (strstr($interface, '_vip')) {
return get_configured_carp_interface_list($interface);
} else {
return null;
@ -3999,7 +4010,7 @@ function get_interface_ip($interface = 'wan')
}
$curip = find_interface_ip($realif);
if ($curip && is_ipaddr($curip) && $curip != '0.0.0.0') {
if (is_ipaddrv4($curip)) {
return $curip;
}
@ -4012,8 +4023,8 @@ function get_interface_ipv6($interface = 'wan')
$realif = get_failover_interface($interface, 'inet6');
if (!$realif) {
if (strstr($interface, "_vip")) {
return get_configured_carp_interface_list($interface, "inet6");
if (strstr($interface, '_vip')) {
return get_configured_carp_interface_list($interface, 'inet6');
} else {
return null;
}
@ -4021,7 +4032,7 @@ function get_interface_ipv6($interface = 'wan')
$curip = isset($config['interfaces'][$interface]['dhcp6prefixonly']) ?
find_interface_ipv6_ll($realif) : find_interface_ipv6($realif);
if ($curip && is_ipaddrv6($curip) && $curip != '::') {
if (is_ipaddrv6($curip)) {
return $curip;
}

View File

@ -530,11 +530,11 @@ function unbound_add_host_entries()
if ($interface == 'lo0') {
continue;
}
$if = get_real_interface($interface);
if (!does_interface_exist($if)) {
continue;
}
$laddr = find_interface_ip($if);
$realifv4 = get_real_interface($interface);
$realifv6 = get_real_interface($interface, 'inet6');
$laddr = find_interface_ip($realifv4);
if (is_ipaddrv4($laddr)) {
$domain = $config['system']['domain'];
if (isset($config['dhcpd'][$interface]['enable']) && !empty($config['dhcpd'][$interface]['domain'])) {
@ -544,7 +544,7 @@ function unbound_add_host_entries()
$unbound_entries .= "local-data: \"{$config['system']['hostname']}.{$domain} A {$laddr}\"\n";
$unbound_entries .= "local-data: \"{$config['system']['hostname']} A {$laddr}\"\n";
}
$laddr6 = find_interface_ipv6($if);
$laddr6 = find_interface_ipv6($realifv6);
if (is_ipaddrv6($laddr6)) {
$domain = $config['system']['domain'];
if (isset($config['dhcpdv6'][$interface]['enable']) && !empty($config['dhcpdv6'][$interface]['domain'])) {
@ -555,7 +555,7 @@ function unbound_add_host_entries()
$unbound_entries .= "local-data: \"{$config['system']['hostname']} AAAA {$laddr6}\"\n";
}
if (empty($config['unbound']['noreglladdr6'])) {
$lladdr6 = find_interface_ipv6_ll($if);
$lladdr6 = find_interface_ipv6_ll($realifv6);
if (is_ipaddrv6($lladdr6)) {
$domain = $config['system']['domain'];
if (isset($config['dhcpdv6'][$interface]['enable']) && !empty($config['dhcpdv6'][$interface]['domain'])) {

View File

@ -408,7 +408,7 @@ function system_host_route($host, $gateway, $delete = true, $add = true)
function system_default_route($gateway, $family, $interface, $far = false)
{
$realif = get_real_interface($interface);
$realif = get_real_interface($interface, $family == 'inet' ? 'all' : 'inet6');
switch ($family) {
case 'inet':
@ -994,7 +994,7 @@ function get_possible_listen_ips($include_ipv6_link_local = false, $include_loop
$tmp["value"] = $iface;
$listenips[] = $tmp;
if ($include_ipv6_link_local) {
$llip = find_interface_ipv6_ll(get_real_interface($iface));
$llip = find_interface_ipv6_ll(get_real_interface($iface, 'inet6'));
if (!empty($llip)) {
$tmp["name"] = "{$ifacename} IPv6 Link-Local";
$tmp["value"] = $llip;