From f2d6a44b4c05f37ee7eeb53f6676e319354dbd5c Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Fri, 1 Nov 2019 11:13:26 +0100 Subject: [PATCH] plugins: add plugins_devices() facility for device plugging --- src/etc/inc/console.inc | 14 ++++++++++++-- src/etc/inc/plugins.inc | 21 +++++++++++++++++++++ src/etc/inc/plugins.inc.d/core.inc | 29 +++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/etc/inc/console.inc b/src/etc/inc/console.inc index 3efced002..1208c6516 100644 --- a/src/etc/inc/console.inc +++ b/src/etc/inc/console.inc @@ -53,14 +53,24 @@ function timeout($timer = 5) function is_interface_mismatch() { + $patterns = array('^impossiblecontrolmatch$'); /* XXX */ $mismatch = false; - foreach (legacy_config_get_interfaces(array("virtual" => false)) as $ifname => $ifcfg) { + foreach (plugins_devices() as $device) { + if (!$device['critical']) { + $patterns[] = "({$device['pattern']})"; + } + } + + /* XXX we can match known devices names, much easier as soon as it is provided */ + $regex = '/' . implode('|', $patterns) . '/'; + + foreach (legacy_config_get_interfaces(['virtual' => false]) as $ifname => $ifcfg) { if (!empty($ifcfg['lock'])) { /* Do not mismatch if any lock was issued */ $mismatch = false; break; - } elseif (preg_match('/^enc|^cua|^tun|^tap|^l2tp|^pptp|^ppp|^ovpn|^ocvpn|^tinc|^wg|^zt|^ue|^gif|^gre|^lagg|^bridge|^ipsec|vlan|_wlan|_stf/i', $ifcfg['if'])) { + } elseif (preg_match($regex, $ifcfg['if'])) { /* Do not check these interfaces */ continue; } elseif (does_interface_exist($ifcfg['if']) == false) { diff --git a/src/etc/inc/plugins.inc b/src/etc/inc/plugins.inc index 654bba779..ae1458030 100644 --- a/src/etc/inc/plugins.inc +++ b/src/etc/inc/plugins.inc @@ -91,6 +91,27 @@ function plugins_services() return $services; } +function plugins_devices() +{ + $devices = array(); + + foreach (plugins_scan() as $name => $path) { + try { + include_once $path; + } catch (ParseError $e) { + error_log($e); + } + $func = sprintf('%s_devices', $name); + if (function_exists($func)) { + foreach ($func() as $work) { + $devices[] = $work; + } + } + } + + return $devices; +} + function plugins_cron() { $jobs = array(); diff --git a/src/etc/inc/plugins.inc.d/core.inc b/src/etc/inc/plugins.inc.d/core.inc index 0bf77efb3..25f61e6c6 100644 --- a/src/etc/inc/plugins.inc.d/core.inc +++ b/src/etc/inc/plugins.inc.d/core.inc @@ -104,6 +104,35 @@ function core_services() return $services; } +function core_devices() +{ + $devices = array(); + + # XXX is a plugin collection... + $devices[] = array('pattern' => '^bridge', 'critical' => false); + $devices[] = array('pattern' => '^cua', 'critical' => false); + $devices[] = array('pattern' => '^enc', 'critical' => false); + $devices[] = array('pattern' => '^gif', 'critical' => false); + $devices[] = array('pattern' => '^gre', 'critical' => false); + $devices[] = array('pattern' => '^ipsec', 'critical' => false); + $devices[] = array('pattern' => '^l2tp', 'critical' => false); + $devices[] = array('pattern' => '^lagg', 'critical' => false); + $devices[] = array('pattern' => '^ocvpn', 'critical' => false); + $devices[] = array('pattern' => '^ovpn', 'critical' => false); + $devices[] = array('pattern' => '^ppp', 'critical' => false); + $devices[] = array('pattern' => '^pptp', 'critical' => false); + $devices[] = array('pattern' => '^tinc', 'critical' => false); + $devices[] = array('pattern' => '^tun|^tap', 'critical' => false); + $devices[] = array('pattern' => '^ue', 'critical' => false); + $devices[] = array('pattern' => '^wg', 'critical' => false); + $devices[] = array('pattern' => '^zt', 'critical' => false); + $devices[] = array('pattern' => '_stf', 'critical' => false); + $devices[] = array('pattern' => '_wlan', 'critical' => false); + $devices[] = array('pattern' => 'vlan', 'critical' => false); + + return $devices; +} + function core_cron() { global $config;