From a73813684721aeed6bbb1133fa38dbee56f3ce9c Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Mon, 12 Jun 2023 13:14:05 +0200 Subject: [PATCH] dhcp: use static mapping to find a gateway to delegated prefix PR: https://forum.opnsense.org/index.php?topic=34416.0 --- src/opnsense/scripts/dhcp/prefixes.php | 51 +++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/opnsense/scripts/dhcp/prefixes.php b/src/opnsense/scripts/dhcp/prefixes.php index 52f81172f..94dd28d20 100755 --- a/src/opnsense/scripts/dhcp/prefixes.php +++ b/src/opnsense/scripts/dhcp/prefixes.php @@ -27,9 +27,37 @@ * POSSIBILITY OF SUCH DAMAGE. */ +require_once 'config.inc'; require_once 'util.inc'; +require_once 'plugins.inc'; -$leases_file = "/var/dhcpd/var/db/dhcpd6.leases"; +function parse_duid($duid_string) +{ + $parsed_duid = []; + + for ($i = 0; $i < strlen($duid_string); $i++) { + $s = substr($duid_string, $i, 1); + if ($s == '\\') { + $n = substr($duid_string, $i + 1, 1); + if ($n == '\\' || $n == '"') { + $parsed_duid[] = sprintf('%02x', ord($n)); + $i += 1; + } elseif (is_numeric($n)) { + $parsed_duid[] = sprintf('%02x', octdec(substr($duid_string, $i + 1, 3))); + $i += 3; + } + } else { + $parsed_duid[] = sprintf('%02x', ord($s)); + } + } + + $iaid = array_slice($parsed_duid, 0, 4); + $duid = array_slice($parsed_duid, 4); + + return [$iaid, $duid]; +} + +$leases_file = '/var/dhcpd/var/db/dhcpd6.leases'; if (!file_exists($leases_file)) { exit(1); } @@ -54,6 +82,9 @@ foreach (new SplFileObject($leases_file) as $line) { /* closing bracket */ if (preg_match("/^}/i ", $line)) { + $iaid_duid = parse_duid($duid); + $duid = implode(':', $iaid_duid[1]); + switch ($type) { case "ia-na": if (!empty($ia_na)) { @@ -74,6 +105,24 @@ foreach (new SplFileObject($leases_file) as $line) { } } +/* since a route requires a gateway try to derive it from static mapping as well */ +foreach (plugins_run('static_mapping') as $map) { + foreach ($map as $host) { + if (empty($host['duid'])) { + continue; + } + + if (empty($duid_arr[$host['duid']])) { + continue; + } + + if (!empty($host['ipaddrv6'])) { + /* we want static mapping to have a higher priority */ + $duid_arr[$host['duid']]['ia-na'] = $host['ipaddrv6']; + } + } +} + $routes = []; foreach ($duid_arr as $entry) {