Networking: smart default gateway implementation

This commit is contained in:
bergware
2025-02-25 17:16:03 +01:00
parent 4f5556409b
commit 71334439aa
2 changed files with 35 additions and 35 deletions

View File

@@ -52,15 +52,15 @@ function index($key) {
return filter_var($key,FILTER_SANITIZE_NUMBER_INT);
}
function metric($eth, $prot) {
function metric($eth, $prot, $index) {
$system = '/sys/class/net';
$bridge = str_replace('eth','br',$eth);
$bond = str_replace('eth','bond',$eth);
$port = file_exists("$system/$bridge") ? $bridge : (file_exists("$system/$bond") ? $bond : $eth);
$metric = exec("ip -$prot route show default dev $port | grep -Pom1 ' metric \K\d+'");
if ($metric) return $metric;
exec("ip -$prot route show default | grep -Po ' metric \K\d+'",$metrics);
return max($metrics) + 1;
$metric = exec("ip -$prot route show default dev $port 2>/dev/null | grep -Pom1 ' metric \K\d+'");
if ($metric) return $metric + $index;
exec("ip -$prot route show default 2>/dev/null | grep -Po ' metric \K\d+'",$metrics);
return max($metrics) + $index + 1;
}
// remove non-existing ethernet ports
@@ -717,7 +717,7 @@ _(IPv4 address)_:
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:0" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY:0")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC:0")?:metric('eth0',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC:0")?:metric('eth0',4,0)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv4_default_gateway_help:
@@ -734,13 +734,13 @@ _(IPv6 address assignment)_:
<div markdown="1" class="more-ipv6-eth0-0 hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:0" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"NETMASK6:0")?>" class="slim">
: <input type="text" name="IPADDR6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:0" min="7" max="128" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"NETMASK6:0","64")?>" class="slim">
:eth_ipv6_address_help:
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC6:0")?:metric('eth0',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC6:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC6:0")?:metric('eth0',6,0)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv6_default_gateway_help:
@@ -817,7 +817,7 @@ _(IPv4 address)_:
<div markdown="1" class="more-gw4-eth0-<?=$i?> hide">
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:<?=$i?>" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY:$i")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC:$i")?:metric('eth0',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC:$i")?:metric('eth0.<?=_var($eth0,"VLANID:$i")?>',4,$i)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv4_default_gateway_help:
@@ -839,14 +839,14 @@ _(IPv6 address assignment)_:
<div markdown="1" class="more-ipv6-eth0-<?=$i?> hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:<?=$i?>" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"NETMASK6:$i")?>" class="slim">
: <input type="text" name="IPADDR6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:<?=$i?>" min="7" max="128" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"NETMASK6:$i","64")?>" class="slim">
:eth_ipv6_address_help:
<div markdown="1" class="more-gw6-eth0-<?=$i?> hide">
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC6:$i")?:metric('eth0',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC6:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC6:$i")?:metric('eth0.<?=_var($eth0,"VLANID:$i")?>',6,$i)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv6_default_gateway_help:
@@ -897,18 +897,18 @@ _(IPv4 address assignment)_:
<?=mk_option(_var($eth0,"USE_DHCP:INDEX"), 'no', _('Static'))?>
<?=mk_option(_var($eth0,"USE_DHCP:INDEX"), '', _('None'))?>
</select>
<span class="gw4-eth0-INDEX hide"><input type="checkbox" name="USE_GW4:INDEX" onchange="selectGW(this.form,4,INDEX,'slow')" <?=_var($eth0,"USE_GW:INDEX")?'checked':''?>><?=$enable?></span>
<span class="gw4-eth0-INDEX hide"><input type="checkbox" name="USE_GW4:INDEX" onchange="selectGW(this.form,4,INDEX,'slow')"><?=$enable?></span>
<div markdown="1" class="more-ipv4-eth0-INDEX hide">
_(IPv4 address)_:
: <input type="text" name="IPADDR:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR:INDEX")?>" class="narrow" pattern="<?=$validIP4?>" title="=_(IPv4 address A.B.C.D)_">/<select name="NETMASK:INDEX" class="slim">
<?foreach ($masks as $mask => $prefix) echo mk_option(_var($eth0,"NETMASK:INDEX"), $mask, $prefix, $prefix=='24'?'selected':'');?>
: <input type="text" name="IPADDR:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">/<select name="NETMASK:INDEX" class="slim">
<?foreach ($masks as $mask => $prefix) echo mk_option(0, $mask, $prefix, $prefix=='24'?'selected':'');?>
</select>
<div markdown="1" class="more-gw4-eth0-INDEX> hide">
<div markdown="1" class="more-gw4-eth0-INDEX hide">
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY:INDEX")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC:INDEX")?:metric('eth0',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
: <input type="text" name="GATEWAY:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=metric('eth0.INDEX',4,$i+1)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
</div>
</div>
@@ -920,16 +920,16 @@ _(IPv6 address assignment)_:
<?=mk_option(_var($eth0,"USE_DHCP6:INDEX"), 'no', _('Static'))?>
<?=mk_option(_var($eth0,"USE_DHCP6:INDEX"), '', _('None'))?>
</select>
<span class="gw6-eth0-INDEX hide"><input type="checkbox" name="USE_GW6:INDEX" onchange="selectGW(this.form,6,INDEX,'slow')" <?=_var($eth0,"USE_GW6:INDEX")?'checked':''?>><?=$enable?></span>
<span class="gw6-eth0-INDEX hide"><input type="checkbox" name="USE_GW6:INDEX" onchange="selectGW(this.form,6,INDEX,'slow')"><?=$enable?></span>
<div markdown="1" class="more-ipv6-eth0-INDEX hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"IPADDR6:INDEX")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:INDEX" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"NETMASK6:INDEX")?>" class="slim">
: <input type="text" name="IPADDR6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:INDEX" min="7" max="128" autocomplete="off" spellcheck="false" value="64" class="slim">
<div markdown="1" class="more-gw6-eth0-INDEX> hide">
<div markdown="1" class="more-gw6-eth0-INDEX hide">
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"GATEWAY6:INDEX")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($eth0,"METRIC6:INDEX")?:metric('eth0',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
: <input type="text" name="GATEWAY6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=metric('eth0.INDEX',6,$i+1)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
</div>
</div>

View File

@@ -185,7 +185,7 @@ _(IPv4 address)_:
<div markdown="1" class="more-gw4-ethX-0 hide">
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:0" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY:0")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC:0")?:metric('ethX',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC:0")?:metric('ethX',4,0)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv4_default_gateway_help:
@@ -205,14 +205,14 @@ _(IPv6 address assignment)_:
<div markdown="1" class="more-ipv6-ethX-0 hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:0" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"NETMASK6:0")?>" class="slim">
: <input type="text" name="IPADDR6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:0" min="7" max="128" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"NETMASK6:0","64")?>" class="slim">
:eth_ipv6_address_help:
<div markdown="1" class="more-gw6-ethX-0 hide">
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:0" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY6:0")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC6:0")?:metric('ethX',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC6:0" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC6:0")?:metric('ethX',6,0)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv6_default_gateway_help:
@@ -291,7 +291,7 @@ _(IPv4 address)_:
<div markdown="1" class="more-gw4-ethX-<?=$i?> hide">
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:<?=$i?>" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY:$i")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC:$i")?:metric('ethX',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC:$i")?:metric('ethX.<?=_var($ethX,"VLANID:$i")?>',4,$i)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv4_default_gateway_help:
@@ -311,14 +311,14 @@ _(IPv6 address assignment)_:
<div markdown="1" class="more-ipv6-ethX-<?=$i?> hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:<?=$i?>" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"NETMASK6:$i")?>" class="slim">
: <input type="text" name="IPADDR6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:<?=$i?>" min="7" max="128" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"NETMASK6:$i","64")?>" class="slim">
:eth_ipv6_address_help:
<div markdown="1" class="more-gw6-ethX-<?=$i?> hide">
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:<?=$i?>" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY6:$i")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC6:$i")?:metric('ethX',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
<input type="text" name="METRIC6:<?=$i?>" min="0" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC6:$i")?:metric('ethX.<?=_var($ethX,"VLANID:$i")?>',6,$i)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
:eth_ipv6_default_gateway_help:
@@ -374,14 +374,14 @@ _(IPv4 address assignment)_:
<div markdown="1" class="more-ipv4-ethX-INDEX hide">
_(IPv4 address)_:
: <input type="text" name="IPADDR:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR:INDEX")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">/<select name="NETMASK:INDEX" class="slim">
<?foreach ($masks as $mask => $prefix) echo mk_option(_var($ethX,"NETMASK:INDEX"), $mask, $prefix, $prefix=='24'?'selected':'');?>
: <input type="text" name="IPADDR:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">/<select name="NETMASK:INDEX" class="slim">
<?foreach ($masks as $mask => $prefix) echo mk_option(0, $mask, $prefix, $prefix=='24'?'selected':'');?>
</select>
<div markdown="1" class="more-gw4-ethX-INDEX hide">
_(IPv4 default gateway)_:
: <input type="text" name="GATEWAY:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY:INDEX")?>" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC:INDEX")?:metric('ethX',4)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
: <input type="text" name="GATEWAY:INDEX" maxlength="15" autocomplete="off" spellcheck="false" value="" class="narrow" pattern="<?=$validIP4?>" title="_(IPv4 address A.B.C.D)_">
<input type="text" name="METRIC:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=metric('ethX.INDEX',4,$i+1)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
</div>
</div>
@@ -397,12 +397,12 @@ _(IPv6 address assignment)_:
<div markdown="1" class="more-ipv6-ethX-INDEX hide">
_(IPv6 address)_:
: <input type="text" name="IPADDR6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"IPADDR6:INDEX")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:INDEX" min="1" max="128" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"NETMASK6:INDEX")?>" class="slim">
: <input type="text" name="IPADDR6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">/<input type="number" name="NETMASK6:INDEX" min="7" max="128" autocomplete="off" spellcheck="false" value="64" class="slim">
<div markdown="1" class="more-gw6-ethX-INDEX hide">
_(IPv6 default gateway)_:
: <input type="text" name="GATEWAY6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"GATEWAY6:INDEX")?>" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=_var($ethX,"METRIC6:INDEX")?:metric('ethX',6)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
: <input type="text" name="GATEWAY6:INDEX" maxlength="39" autocomplete="off" spellcheck="false" value="" pattern="<?=$validIP6?>" title="_(IPv6 address nnnn:xxxx::yyyy)_">
<input type="text" name="METRIC6:INDEX" min="1" max="9999" autocomplete="off" spellcheck="false" value="<?=metric('ethX.INDEX',6,$i+1)?>" class="slim"><i class="fa fa-sort-numeric-asc"></i> *<?=$metric?>*
</div>
</div>