diff --git a/Makefile.am b/Makefile.am index 7d9ef085..74a96994 100644 --- a/Makefile.am +++ b/Makefile.am @@ -206,6 +206,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \ src/rtnlutil.h src/rtnlutil.c \ src/auth-proto.h \ src/anqp.h src/anqp.c \ + src/netconfig.h src/netconfig.c\ $(eap_sources) \ $(builtin_sources) src_iwd_LDADD = $(ell_ldadd) -ldl diff --git a/src/netconfig.c b/src/netconfig.c new file mode 100644 index 00000000..64d1f9fd --- /dev/null +++ b/src/netconfig.c @@ -0,0 +1,144 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2019 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 + +#include + +#include "src/iwd.h" +#include "src/netconfig.h" + +struct netconfig { + uint32_t ifindex; +}; + +static struct l_queue *netconfig_list; + +static void netconfig_destroy(void *data) +{ + struct netconfig *netconfig = data; + + l_free(netconfig); +} + +static bool netconfig_match(const void *a, const void *b) +{ + const struct netconfig *netconfig = a; + uint32_t ifindex = L_PTR_TO_UINT(b); + + if (netconfig->ifindex == ifindex) + return true; + + return false; +} + +static struct netconfig *netconfig_find(uint32_t ifindex) +{ + const struct l_queue_entry *entry; + + for (entry = l_queue_get_entries(netconfig_list); entry; + entry = entry->next) { + struct netconfig *netconfig = entry->data; + + if (netconfig->ifindex != ifindex) + continue; + + return netconfig; + } + + return NULL; +} + +bool netconfig_ifindex_add(uint32_t ifindex) +{ + struct netconfig *netconfig; + + if (!netconfig_list) + return false; + + l_debug("Starting netconfig for interface: %d", ifindex); + + netconfig = netconfig_find(ifindex); + if (netconfig) + return true; + + netconfig = l_new(struct netconfig, 1); + netconfig->ifindex = ifindex; + + l_queue_push_tail(netconfig_list, netconfig); + + return true; +} + +bool netconfig_ifindex_remove(uint32_t ifindex) +{ + struct netconfig *netconfig; + + if (!netconfig_list) + return false; + + l_debug(); + + netconfig = l_queue_remove_if(netconfig_list, netconfig_match, + L_UINT_TO_PTR(ifindex)); + if (!netconfig) + return false; + + netconfig_destroy(netconfig); + + return true; +} + +static int netconfig_init(void) +{ + bool enabled; + + if (netconfig_list) + return -EALREADY; + + if (!l_settings_get_bool(iwd_get_config(), "General", + "enable_network_config", &enabled) || + !enabled) { + l_warn("netconfig: Network configuration with the IP addresses " + "is disabled."); + return false; + } + + netconfig_list = l_queue_new(); + + return 0; +} + +static void netconfig_exit(void) +{ + if (!netconfig_list) + return; + + l_queue_destroy(netconfig_list, netconfig_destroy); +} + +IWD_MODULE(netconfig, netconfig_init, netconfig_exit) diff --git a/src/netconfig.h b/src/netconfig.h new file mode 100644 index 00000000..1df28cd9 --- /dev/null +++ b/src/netconfig.h @@ -0,0 +1,24 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2019 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 + * + */ + +bool netconfig_ifindex_add(uint32_t ifindex); +bool netconfig_ifindex_remove(uint32_t ifindex);