From cacd0d83f4dc80d496da477f7295ea8eb1e7ea1a Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Thu, 16 Mar 2017 16:45:10 -0500 Subject: [PATCH] wiphy: Add phy filtering --- src/main.c | 17 +++++++++++++++-- src/wiphy.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/wiphy.h | 3 ++- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 3eb79486..7ec40f9a 100644 --- a/src/main.c +++ b/src/main.c @@ -50,6 +50,8 @@ static struct l_settings *iwd_config; static struct l_timeout *timeout; static const char *interfaces; static const char *nointerfaces; +static const char *phys; +static const char *nophys; static const char *config_dir; static void main_loop_quit(struct l_timeout *timeout, void *user_data) @@ -87,6 +89,8 @@ static void usage(void) "\t-B, --dbus-debug Enable D-Bus debugging\n" "\t-i, --interfaces Interfaces to manage\n" "\t-I, --nointerfaces Interfaces to ignore\n" + "\t-p, --phys Phys to manage\n" + "\t-P, --nophys Phys to ignore\n" "\t-c, --config Configuration directory to use\n" "\t-h, --help Show help options\n"); } @@ -96,6 +100,8 @@ static const struct option main_options[] = { { "version", no_argument, NULL, 'v' }, { "interfaces", required_argument, NULL, 'i' }, { "nointerfaces", required_argument, NULL, 'I' }, + { "phys", required_argument, NULL, 'p' }, + { "nophys", required_argument, NULL, 'P' }, { "config", required_argument, NULL, 'c' }, { "help", no_argument, NULL, 'h' }, { } @@ -114,7 +120,7 @@ static void nl80211_appeared(void *user_data) l_debug("Found nl80211 interface"); - if (!wiphy_init(nl80211)) + if (!wiphy_init(nl80211, phys, nophys)) l_error("Unable to init wiphy functionality"); if (!netdev_init(nl80211, interfaces, nointerfaces)) @@ -151,7 +157,8 @@ int main(int argc, char *argv[]) for (;;) { int opt; - opt = getopt_long(argc, argv, "Bi:I:c:vh", main_options, NULL); + opt = getopt_long(argc, argv, "Bi:I:p:P:c:vh", + main_options, NULL); if (opt < 0) break; @@ -165,6 +172,12 @@ int main(int argc, char *argv[]) case 'I': nointerfaces = optarg; break; + case 'p': + phys = optarg; + break; + case 'P': + nophys = optarg; + break; case 'v': printf("%s\n", VERSION); return EXIT_SUCCESS; diff --git a/src/wiphy.c b/src/wiphy.c index 7b360521..650d6a16 100644 --- a/src/wiphy.c +++ b/src/wiphy.c @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -45,6 +46,8 @@ static struct l_genl_family *nl80211 = NULL; static struct l_hwdb *hwdb; +static char **whitelist_filter; +static char **blacklist_filter; struct wiphy { uint32_t id; @@ -104,6 +107,38 @@ struct wiphy *wiphy_find(int wiphy_id) return l_queue_find(wiphy_list, wiphy_match, L_UINT_TO_PTR(wiphy_id)); } +static bool wiphy_is_managed(const char *phy) +{ + char *pattern; + unsigned int i; + + if (!whitelist_filter) + goto check_blacklist; + + for (i = 0; (pattern = whitelist_filter[i]); i++) { + if (fnmatch(pattern, phy, 0) != 0) + continue; + + goto check_blacklist; + } + + l_debug("whitelist filtered phy: %s", phy); + return false; + +check_blacklist: + if (!blacklist_filter) + return true; + + for (i = 0; (pattern = blacklist_filter[i]); i++) { + if (fnmatch(pattern, phy, 0) == 0) { + l_debug("blacklist filtered ifname: %s", phy); + return false; + } + } + + return true; +} + const char *wiphy_get_path(struct wiphy *wiphy) { static char path[15]; @@ -411,6 +446,9 @@ static void wiphy_dump_callback(struct l_genl_msg *msg, void *user_data) wiphy = l_queue_find(wiphy_list, wiphy_match, L_UINT_TO_PTR(id)); if (!wiphy) { + if (!wiphy_is_managed(name)) + return; + wiphy = l_new(struct wiphy, 1); wiphy->id = id; wiphy->supported_freqs = scan_freq_set_new(); @@ -520,6 +558,9 @@ static void wiphy_new_wiphy_event(struct l_genl_msg *msg) return; } + if (!wiphy_is_managed(name)) + return; + wiphy = l_new(struct wiphy, 1); wiphy->id = id; memcpy(wiphy->name, name, name_len); @@ -773,7 +814,8 @@ static void setup_wiphy_interface(struct l_dbus_interface *interface) wiphy_property_get_name, NULL); } -bool wiphy_init(struct l_genl_family *in) +bool wiphy_init(struct l_genl_family *in, const char *whitelist, + const char *blacklist) { struct l_genl_msg *msg; @@ -824,11 +866,20 @@ bool wiphy_init(struct l_genl_family *in) hwdb = l_hwdb_new_default(); + if (whitelist) + whitelist_filter = l_strsplit(whitelist, ','); + + if (blacklist) + blacklist_filter = l_strsplit(blacklist, ','); + return true; } bool wiphy_exit(void) { + l_strfreev(whitelist_filter); + l_strfreev(blacklist_filter); + l_queue_destroy(wiphy_list, wiphy_free); wiphy_list = NULL; diff --git a/src/wiphy.h b/src/wiphy.h index d3297097..c4a2cc30 100644 --- a/src/wiphy.h +++ b/src/wiphy.h @@ -35,5 +35,6 @@ const char *wiphy_get_path(struct wiphy *wiphy); uint32_t wiphy_get_supported_bands(struct wiphy *wiphy); bool wiphy_can_connect(struct wiphy *wiphy, struct scan_bss *bss); -bool wiphy_init(struct l_genl_family *in); +bool wiphy_init(struct l_genl_family *in, const char *whitelist, + const char *blacklist); bool wiphy_exit(void);