From 2b9f029819df94d44fcbb0cb5ca19cda062554ad Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Wed, 24 Aug 2022 12:02:16 +0200 Subject: [PATCH] interfaces: stop calling match_wireless_interface() everywhere #5987 Restructure return value of legacy_interface_listget() to return WLAN-only devices present in the system and avoid returning null value to simplify the couple of callers (some already assumed as much). Assume that node is properly set since console.inc always did store this. Not sure about wireless clones yet, but will check and fix in the scope of this ticket anyway. get_interface_list() moves to interfaces.lib.inc since it uses most functions from there and util.inc should not want to know about interface details in the first place. We need this later when we work through interfaces_assign.php for device iteration reasons. --- src/etc/inc/console.inc | 7 +- src/etc/inc/interfaces.inc | 71 ++++------------ src/etc/inc/interfaces.lib.inc | 117 +++++++++++++++++++++++++-- src/etc/inc/util.inc | 103 ----------------------- src/www/interfaces_assign.php | 34 ++++++++ src/www/interfaces_wireless_edit.php | 10 +-- 6 files changed, 167 insertions(+), 175 deletions(-) diff --git a/src/etc/inc/console.inc b/src/etc/inc/console.inc index e7db891f7..bd99120ac 100644 --- a/src/etc/inc/console.inc +++ b/src/etc/inc/console.inc @@ -101,6 +101,7 @@ function _set_networking_interfaces_ports($probe = false) $key = null; $iflist_all = get_interface_list(false, true); + $iflist_wlan = legacy_interface_listget('wlan'); $iflist_lagg = []; $iflist = []; @@ -404,7 +405,7 @@ EOD; $config['nat']['outbound']['mode'] = 'automatic'; } - if (match_wireless_interface($lanif)) { + if (in_array($lanif, $iflist_wlan)) { config_read_array('interfaces', 'lan', 'wireless'); $config['interfaces']['lan']['if'] .= '_wlan0'; } elseif (isset($config['interfaces']['lan']['wireless'])) { @@ -440,7 +441,7 @@ EOD; $config['interfaces']['wan']['blockpriv'] = true; } - if (match_wireless_interface($wanif)) { + if (in_array($wanif, $iflist_wlan)) { config_read_array('interfaces', 'wan', 'wireless'); $config['interfaces']['wan']['if'] .= '_wlan0'; } elseif (isset($config['interfaces']['wan']['wireless'])) { @@ -456,7 +457,7 @@ EOD; config_read_array('interfaces', 'opt' . ($i + 1)); $config['interfaces']['opt' . ($i + 1)]['if'] = $optif[$i]; - if (match_wireless_interface($optif[$i])) { + if (in_array($optif[$i], $iflist_wlan)) { config_read_array('interfaces', 'opt' . ($i + 1), 'wireless'); $config['interfaces']['opt' . ($i + 1)]['if'] .= '_wlan0'; } elseif (isset($config['interfaces']['opt' . ($i + 1)]['wireless'])) { diff --git a/src/etc/inc/interfaces.inc b/src/etc/inc/interfaces.inc index d591720a7..6505293db 100644 --- a/src/etc/inc/interfaces.inc +++ b/src/etc/inc/interfaces.inc @@ -91,40 +91,6 @@ function convert_128bit_to_ipv6($ip6bin) return $ip6addr; } -function match_wireless_interface($int) -{ - $wireless_prefix = [ - 'an', - 'ath', - 'bwi', - 'bwn', - 'ipw', - 'iwi', - 'iwlwifi', - 'iwm', - 'iwn', - 'malo', - 'mwl', - 'ndis', - 'otus', - 'ral', - 'rsu', - 'rtwn', - 'rum', - 'run', - 'uath', - 'upgt', - 'ural', - 'urtw', - 'wi', - 'wlan', - 'wpi', - 'zyd', - ]; - - return preg_match('/^(' . implode('|', $wireless_prefix) . ')/', $int); -} - function interfaces_bring_up($interface) { legacy_interface_flags($interface, 'up'); @@ -132,12 +98,7 @@ function interfaces_bring_up($interface) function does_interface_exist($interface) { - $ints = legacy_interface_listget(); - if (empty($interface) || $ints == null || !in_array($interface, $ints)) { - return false; - } else { - return true; - } + return !empty($interface) && in_array($interface, legacy_interface_listget()); } function interfaces_loopback_configure($verbose = false) @@ -2070,7 +2031,7 @@ EOD; /* find which clones are up and bring them down */ $ifup = legacy_interface_listget('up'); - $clones_up = array(); + $clones_up = []; foreach ($clone_list as $clone_if) { if (in_array($clone_if, $ifup)) { $clones_up[] = $clone_if; @@ -3523,7 +3484,8 @@ function get_real_interface($interface = 'wan', $family = 'all') case 'ppp': case 'pppoe': case 'pptp': - if (isset($cfg['wireless']) || match_wireless_interface($cfg['if'])) { + if (isset($cfg['wireless'])) { + /* XXX in nominal case $cfg['if'] is already correct */ $realif = interface_get_wireless_clone($cfg['if']); } else { $realif = $cfg['if']; @@ -3542,7 +3504,8 @@ function get_real_interface($interface = 'wan', $family = 'all') } break; default: - if (isset($cfg['wireless']) || match_wireless_interface($cfg['if'])) { + if (isset($cfg['wireless'])) { + /* XXX in nominal case $cfg['if'] is already correct */ $realif = interface_get_wireless_clone($cfg['if']); } else { $realif = $cfg['if']; @@ -3555,7 +3518,8 @@ function get_real_interface($interface = 'wan', $family = 'all') // Wireless cloned NIC support (FreeBSD 8+) // interface name format: $parentnic_wlanparentnic# // example: ath0_wlan0 - if (isset($cfg['wireless']) || match_wireless_interface($cfg['if'])) { + if (isset($cfg['wireless'])) { + /* XXX in nominal case $cfg['if'] is already correct */ $realif = interface_get_wireless_clone($cfg['if']); } else { $realif = $cfg['if']; @@ -3806,15 +3770,10 @@ function is_interface_wireless($interface) global $config; $friendly = convert_real_interface_to_friendly_interface_name($interface); - if (!isset($config['interfaces'][$friendly]['wireless'])) { - if (match_wireless_interface($interface)) { - config_read_array('interfaces', $friendly, 'wireless'); - return true; - } - return false; - } - return true; + /* XXX we might consider checking clones if wireless node is not set by default */ + + return isset($config['interfaces'][$friendly]['wireless']); } function get_interface_mac($interface, $ifconfig_details = null) @@ -3889,8 +3848,8 @@ function get_interfaces_info($include_unlinked = false) $all_intf_stats = legacy_interface_stats(); $gateways = new \OPNsense\Routing\Gateways($all_intf_details); $ifup = legacy_interface_listget('up'); - $result = array(); - $interfaces = legacy_config_get_interfaces(array('virtual' => false)); + $result = []; + $interfaces = legacy_config_get_interfaces(['virtual' => false]); $known_interfaces = []; foreach (array_keys($interfaces) as $ifdescr) { $interfaces[$ifdescr]['if'] = get_real_interface($ifdescr); @@ -3913,8 +3872,8 @@ function get_interfaces_info($include_unlinked = false) } foreach ($interfaces as $ifdescr => $ifinfo) { - $ifinfo['status'] = (is_array($ifup) && in_array($ifinfo['if'], $ifup)) ? 'up' : 'down'; - $ifinfo['statusv6'] = (is_array($ifup) && in_array($ifinfo['ifv6'], $ifup)) ? 'up' : 'down'; + $ifinfo['status'] = in_array($ifinfo['if'], $ifup) ? 'up' : 'down'; + $ifinfo['statusv6'] = in_array($ifinfo['ifv6'], $ifup) ? 'up' : 'down'; if (!empty($all_intf_details[$ifinfo['if']])) { if ( diff --git a/src/etc/inc/interfaces.lib.inc b/src/etc/inc/interfaces.lib.inc index 7d0a9c8e3..409a31d82 100644 --- a/src/etc/inc/interfaces.lib.inc +++ b/src/etc/inc/interfaces.lib.inc @@ -1,7 +1,7 @@ + * Copyright (c) 2015-2022 Franco Fichtner * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,12 +26,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ -function legacy_interface_listget($flag = '') +function legacy_interface_listget($flag = 'all') { $cmd_wlan = 'sysctl -n net.wlan.devices'; $cmd = '/sbin/ifconfig -l'; - $ifs_wlan = null; - $ifs = null; + $ifs_wlan = []; + $ifs = []; exec($cmd_wlan . ' 2>&1', $out_wlan, $ret_wlan); if (!$ret_wlan && !empty($out_wlan[0])) { @@ -40,8 +40,8 @@ function legacy_interface_listget($flag = '') if ($flag === 'up') { $cmd .= 'u'; - } elseif ($flag === 'down') { - $cmd .= 'd'; + } elseif ($flag === 'wlan') { + return ($ifs_wlan); } exec($cmd . ' 2>&1', $out, $ret); @@ -54,7 +54,7 @@ function legacy_interface_listget($flag = '') $ifs = explode(' ', $out[0]); } - if ($ifs_wlan != null) { + if (count($ifs_wlan)) { $ifs = array_merge($ifs, $ifs_wlan); } @@ -529,3 +529,106 @@ function configure_interface_hardware($ifs, $intf_details = null) } } } + +/* + * get_interface_list() - Return a list of all physical interfaces + * along with MAC, IPv4 and status. + * + * $only_active = false -- interfaces that are available in the system + * true -- interfaces that are physically connected + * + * $include_dmesg = false -- skip probing dmesg for more information + * true -- probe dmesg for more information + */ +function get_interface_list($only_active = false, $include_dmesg = false) +{ + $dmesg_arr = []; + $iflist = []; + + if ($include_dmesg) { + exec('/sbin/dmesg', $dmesg_arr); + } + + /* list of virtual interface types */ + $vfaces = [ + '_stf', + '_vip', + '_vlan', + '_wlan', + 'bridge', + 'carp', + 'enc', + 'faith', + 'gif', + 'gre', + 'ipfw', + 'l2tp', + 'lagg', + 'lo', + 'ng', + 'ovpnc', + 'ovpns', + 'pflog', + 'pfsync', + 'plip', + 'ppp', + 'pppoe', + 'pptp', + 'qinq', + 'sl', + 'tap', + 'tun', + 'vlan', + ]; + + $ifnames_up = legacy_interface_listget('up'); + $ifnames = legacy_interface_listget(); + $all_interfaces = legacy_config_get_interfaces(['virtual' => false]); + $all_interface_data = legacy_interfaces_details(); + + if ($only_active) { + $_ifnames = []; + $all_stats = legacy_interface_stats(); + foreach ($ifnames as $ifname) { + $ifinfo = $all_stats[$ifname]; + if (!empty($ifinfo['link state']) && $ifinfo['link state'] == '2') { + $_ifnames[] = $ifname; + } + } + $ifnames = $_ifnames; + } + + foreach ($ifnames as $ifname) { + $tmp_ifnames = preg_split('/\d+/', $ifname); + if (count(array_intersect($tmp_ifnames, $vfaces))) { + continue; + } + + $ifdata = !empty($all_interface_data[$ifname]) ? $all_interface_data[$ifname] : []; + $toput = [ + 'up' => in_array($ifname, $ifnames_up), + 'ipaddr' => !empty($ifdata['ipv4'][0]['ipaddr']) ? $ifdata['ipv4'][0]['ipaddr'] : null, + 'mac' => !empty($ifdata['macaddr']) ? $ifdata['macaddr'] : null, + 'dmesg' => '', + ]; + + foreach ($all_interfaces as $name => $int) { + if ($int['if'] == $ifname) { + $toput['friendly'] = $name; + break; + } + } + + foreach ($dmesg_arr as $dmesg_line) { + $dmesg = []; + if (preg_match("/^{$ifname}: <(.*?)>/i", $dmesg_line, $dmesg) == 1) { + $toput['dmesg'] = $dmesg[1]; + break; + } + } + + $iflist[$ifname] = $toput; + } + + return $iflist; +} diff --git a/src/etc/inc/util.inc b/src/etc/inc/util.inc index ee1982101..ec40ee8a8 100644 --- a/src/etc/inc/util.inc +++ b/src/etc/inc/util.inc @@ -839,109 +839,6 @@ function get_configured_ip_addresses() return $ip_array; } -/* - * get_interface_list() - Return a list of all physical interfaces - * along with MAC, IPv4 and status. - * - * $only_active = false -- interfaces that are available in the system - * true -- interfaces that are physically connected - * - * $include_dmesg = false -- skip probing dmesg for more information - * true -- probe dmesg for more information - */ -function get_interface_list($only_active = false, $include_dmesg = false) -{ - $dmesg_arr = []; - $iflist = []; - - if ($include_dmesg) { - exec('/sbin/dmesg', $dmesg_arr); - } - - /* list of virtual interface types */ - $vfaces = [ - '_stf', - '_vip', - '_vlan', - '_wlan', - 'bridge', - 'carp', - 'enc', - 'faith', - 'gif', - 'gre', - 'ipfw', - 'l2tp', - 'lagg', - 'lo', - 'ng', - 'ovpnc', - 'ovpns', - 'pflog', - 'pfsync', - 'plip', - 'ppp', - 'pppoe', - 'pptp', - 'qinq', - 'sl', - 'tap', - 'tun', - 'vlan', - ]; - - $ifnames_up = legacy_interface_listget('up'); - $ifnames = legacy_interface_listget(); - $all_interfaces = legacy_config_get_interfaces(['virtual' => false]); - $all_interface_data = legacy_interfaces_details(); - - if ($only_active) { - $_ifnames = []; - $all_stats = legacy_interface_stats(); - foreach ($ifnames as $ifname) { - $ifinfo = $all_stats[$ifname]; - if (!empty($ifinfo['link state']) && $ifinfo['link state'] == '2') { - $_ifnames[] = $ifname; - } - } - $ifnames = $_ifnames; - } - - foreach ($ifnames as $ifname) { - $tmp_ifnames = preg_split('/\d+/', $ifname); - if (count(array_intersect($tmp_ifnames, $vfaces))) { - continue; - } - - $ifdata = !empty($all_interface_data[$ifname]) ? $all_interface_data[$ifname] : []; - $toput = [ - 'up' => in_array($ifname, $ifnames_up), - 'ipaddr' => !empty($ifdata['ipv4'][0]['ipaddr']) ? $ifdata['ipv4'][0]['ipaddr'] : null, - 'mac' => !empty($ifdata['macaddr']) ? $ifdata['macaddr'] : null, - 'dmesg' => '', - ]; - - foreach ($all_interfaces as $name => $int) { - if ($int['if'] == $ifname) { - $toput['friendly'] = $name; - break; - } - } - - foreach ($dmesg_arr as $dmesg_line) { - $dmesg = []; - if (preg_match("/^{$ifname}: <(.*?)>/i", $dmesg_line, $dmesg) == 1) { - $toput['dmesg'] = $dmesg[1]; - break; - } - } - - $iflist[$ifname] = $toput; - } - - return $iflist; -} - /****f* util/log_error * NAME * log_error - Sends a string to syslog with LOG_ERR severity. diff --git a/src/www/interfaces_assign.php b/src/www/interfaces_assign.php index 56ff5133f..0eacb95dc 100644 --- a/src/www/interfaces_assign.php +++ b/src/www/interfaces_assign.php @@ -34,6 +34,40 @@ require_once("rrd.inc"); require_once("system.inc"); require_once("interfaces.inc"); +function match_wireless_interface($int) +{ + $wireless_prefix = [ + 'an', + 'ath', + 'bwi', + 'bwn', + 'ipw', + 'iwi', + 'iwlwifi', + 'iwm', + 'iwn', + 'malo', + 'mwl', + 'ndis', + 'otus', + 'ral', + 'rsu', + 'rtwn', + 'rum', + 'run', + 'uath', + 'upgt', + 'ural', + 'urtw', + 'wi', + 'wlan', + 'wpi', + 'zyd', + ]; + + return preg_match('/^(' . implode('|', $wireless_prefix) . ')/', $int); +} + function link_interface_to_group($int) { global $config; diff --git a/src/www/interfaces_wireless_edit.php b/src/www/interfaces_wireless_edit.php index a0dedb82e..6cb679669 100644 --- a/src/www/interfaces_wireless_edit.php +++ b/src/www/interfaces_wireless_edit.php @@ -54,6 +54,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { $pconfig['cloneif'] = $a_clones[$id]['cloneif']; $pconfig['mode'] = $a_clones[$id]['mode']; $pconfig['descr'] = $a_clones[$id]['descr']; + } else { + $pconfig = ['if' => '', 'cloneif' => '', 'mode' => 'bss', 'descr' => '']; } } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { $input_errors = array(); @@ -153,12 +155,8 @@ include("head.inc");