interfaces: clear a faulty automatic DUID; closes #3061

This commit is contained in:
Franco Fichtner 2018-12-28 18:20:48 +01:00
parent 333dc0fce8
commit ccd0f07fa8
3 changed files with 50 additions and 29 deletions

View File

@ -2858,15 +2858,10 @@ function interface_dhcpv6_configure($interface = 'wan', $wancfg)
/* write DUID if override was set */
if (!empty($config['system']['ipv6duid'])) {
$fd = fopen('/var/db/dhcp6c_duid', 'wb');
if ($fd) {
$parts = explode(':', $config['system']['ipv6duid']);
/* length is unsigned 16 bit integer, machine-dependent:*/
fwrite($fd, pack('S', count($parts)));
/* buffer is binary string, according to advertised length: */
fwrite($fd, pack('H*', implode('', $parts)));
fclose($fd);
}
dhcp6c_duid_write($config['system']['ipv6duid']);
/* clear DUID if it is faulty */
} elseif (empty(dhcp6c_duid_read())) {
dhcp6c_duid_clear();
}
if (!is_array($wancfg)) {

View File

@ -1281,3 +1281,47 @@ function is_install_media()
return true;
}
function dhcp6c_duid_read()
{
$parts = array();
$skip = 2;
if (file_exists('/var/db/dhcp6c_duid')) {
$size = filesize('/var/db/dhcp6c_duid');
if ($size > $skip && ($fd = fopen('/var/db/dhcp6c_duid', 'r'))) {
$ret = unpack('Slen/H*buf', fread($fd, $size));
fclose($fd);
if (isset($ret['len']) && isset($ret['buf'])) {
if ($ret['len'] + $skip == $size && strlen($ret['buf']) == $ret['len'] * 2) {
$parts = str_split($ret['buf'], 2);
}
}
}
}
$duid = strtoupper(implode(':', $parts));
return $duid;
}
function dhcp6c_duid_write($duid)
{
$fd = fopen('/var/db/dhcp6c_duid', 'wb');
if ($fd) {
$parts = explode(':', $duid);
/* length is unsigned 16 bit integer, machine-dependent:*/
fwrite($fd, pack('S', count($parts)));
/* buffer is binary string, according to advertised length: */
fwrite($fd, pack('H*', implode('', $parts)));
fclose($fd);
}
}
function dhcp6c_duid_clear()
{
@unlink('/var/db/dhcp6c_duid');
/* clear the backup so that it will not be restored: */
@unlink('/conf/dhcp6c_duid');
}

View File

@ -148,24 +148,7 @@ function is_duid($duid)
/* read duid from disk or return blank DUID string */
function read_duid()
{
$parts = array();
$skip = 2;
if (file_exists('/var/db/dhcp6c_duid')) {
$size = filesize('/var/db/dhcp6c_duid');
if ($size > $skip && ($fd = fopen('/var/db/dhcp6c_duid', 'r'))) {
$ret = unpack('Slen/H*buf', fread($fd, $size));
fclose($fd);
if (isset($ret['len']) && isset($ret['buf'])) {
if ($ret['len'] + $skip == $size && strlen($ret['buf']) == $ret['len'] * 2) {
$parts = str_split($ret['buf'], 2);
}
}
}
}
$duid = strtoupper(implode(':', $parts));
$duid = dhcp6c_duid_read();
if (!is_duid($duid)) {
$duid = 'XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX';
@ -235,8 +218,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
} elseif (isset($config['system']['ipv6duid'])) {
unset($config['system']['ipv6duid']);
/* clear the file as this means auto-generate */
@unlink('/var/db/dhcp6c_duid');
@unlink('/conf/dhcp6c_duid');
dhcp6c_duid_clear();
}
$savemsg = get_std_save_message();