From 999ba12f315880efdbf230f34688c55ceaf2836c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 29 Jul 2014 21:25:01 +0200 Subject: [PATCH] core: Add skeleton for nl80211 setup --- Makefile.am | 1 + src/main.c | 8 ++++ src/wiphy.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wiphy.h | 26 +++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 src/wiphy.c create mode 100644 src/wiphy.h diff --git a/Makefile.am b/Makefile.am index 88d3cb4b..fa20c594 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,6 +39,7 @@ bin_PROGRAMS = src/iwd client/iwctl src_iwd_SOURCES = src/main.c linux/nl80211.h linux/kdbus.h \ src/kdbus.h src/kdbus.c \ src/netdev.h src/netdev.c \ + src/wiphy.h src/wiphy.c \ src/sha1.h src/sha1.c \ src/ie.h src/ie.c src_iwd_LDADD = ell/libell-internal.la diff --git a/src/main.c b/src/main.c index b2a9e316..3e020f64 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include #include "src/netdev.h" +#include "src/wiphy.h" #include "src/kdbus.h" static void signal_handler(struct l_signal *signal, uint32_t signo, @@ -83,8 +84,15 @@ int main(int argc, char *argv[]) goto destroy; } + if (!wiphy_init()) { + netdev_exit(); + exit_status = EXIT_FAILURE; + goto destroy; + } + l_main_run(); + wiphy_exit(); netdev_exit(); exit_status = EXIT_SUCCESS; diff --git a/src/wiphy.c b/src/wiphy.c new file mode 100644 index 00000000..dccf1088 --- /dev/null +++ b/src/wiphy.c @@ -0,0 +1,106 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2013-2014 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "linux/nl80211.h" +#include "src/wiphy.h" + +static struct l_genl *genl = NULL; +static struct l_genl_family *nl80211 = NULL; + +static void do_debug(const char *str, void *user_data) +{ + const char *prefix = user_data; + + l_info("%s%s", prefix, str); +} + +static void nl80211_appeared(void *user_data) +{ + struct l_genl_msg *msg; + + l_debug("Found nl80211 interface"); + + msg = l_genl_msg_new(NL80211_CMD_GET_WIPHY); + + l_genl_family_dump(nl80211, msg, NULL, NULL, NULL); +} + +static void nl80211_vanished(void *user_data) +{ + l_debug("Lost nl80211 interface"); +} + +bool wiphy_init(void) +{ + if (genl) + return false; + + genl = l_genl_new_default(); + if (!genl) { + l_error("Failed to open generic netlink socket"); + return false; + } + + l_genl_set_debug(genl, do_debug, "[GENL] ", NULL); + + l_debug("Opening nl80211 interface"); + + nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME); + if (!nl80211) { + l_error("Failed to open nl80211 interface"); + goto failed; + } + + l_genl_family_set_watches(nl80211, nl80211_appeared, nl80211_vanished, + NULL, NULL); + + return true; + +failed: + l_genl_unref(genl); + genl = NULL; + + return false; +} + +bool wiphy_exit(void) +{ + if (!genl) + return false; + + l_debug("Closing nl80211 interface"); + + /* + * The generic netlink master object keeps track of all families + * and closing it will take care of freeing all associated resources. + */ + l_genl_unref(genl); + genl = NULL; + + return true; +} diff --git a/src/wiphy.h b/src/wiphy.h new file mode 100644 index 00000000..b1d75505 --- /dev/null +++ b/src/wiphy.h @@ -0,0 +1,26 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2013-2014 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include + +bool wiphy_init(void); +bool wiphy_exit(void);