/* * * 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 #include #include #include #include #include "src/wiphy.h" #include "src/netdev.h" static struct l_netlink *rtnl = NULL; static void do_debug(const char *str, void *user_data) { const char *prefix = user_data; l_info("%s%s", prefix, str); } struct cb_data { netdev_command_func_t callback; void *user_data; }; static void netlink_result(int error, uint16_t type, const void *data, uint32_t len, void *user_data) { struct cb_data *cb_data = user_data; if (!cb_data) return; cb_data->callback(error < 0 ? false : true, cb_data->user_data); l_free(cb_data); } static size_t rta_add_u8(void *rta_buf, unsigned short type, uint8_t value) { struct rtattr *rta = rta_buf; rta->rta_len = RTA_LENGTH(sizeof(uint8_t)); rta->rta_type = type; *((uint8_t *) RTA_DATA(rta)) = value; return RTA_SPACE(sizeof(uint8_t)); } void netdev_set_linkmode_and_operstate(uint32_t ifindex, uint8_t linkmode, uint8_t operstate, netdev_command_func_t callback, void *user_data) { struct ifinfomsg *rtmmsg; void *rta_buf; size_t bufsize; struct cb_data *cb_data = NULL; bufsize = NLMSG_LENGTH(sizeof(struct ifinfomsg)) + RTA_SPACE(sizeof(uint8_t)) + RTA_SPACE(sizeof(uint8_t)); rtmmsg = l_malloc(bufsize); memset(rtmmsg, 0, bufsize); rtmmsg->ifi_family = AF_UNSPEC; rtmmsg->ifi_index = ifindex; rta_buf = rtmmsg + 1; rta_buf += rta_add_u8(rta_buf, IFLA_LINKMODE, linkmode); rta_buf += rta_add_u8(rta_buf, IFLA_OPERSTATE, operstate); if (callback) { cb_data = l_new(struct cb_data, 1); cb_data->callback = callback; cb_data->user_data = user_data; } l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, rta_buf - (void *) rtmmsg, netlink_result, cb_data, NULL); l_free(rtmmsg); } bool netdev_init(void) { if (rtnl) return false; l_debug("Opening route netlink socket"); rtnl = l_netlink_new(NETLINK_ROUTE); if (!rtnl) { l_error("Failed to open route netlink socket"); return false; } if (getenv("IWD_RTNL_DEBUG")) l_netlink_set_debug(rtnl, do_debug, "[RTNL] ", NULL); return true; } bool netdev_exit(void) { if (!rtnl) return false; l_debug("Closing route netlink socket"); /* * The netlink object keeps track of the registered notification * callbacks and their multicast memberships. When destroying the * netlink object, all resources will be freed. */ l_netlink_destroy(rtnl); rtnl = NULL; return true; }