eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

ethdev.c (18006B)


      1 /*
      2  *
      3  *  Ethernet daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <unistd.h>
     31 #include <fnmatch.h>
     32 #include <net/if_arp.h>
     33 #include <linux/rtnetlink.h>
     34 #include <linux/if.h>
     35 #include <linux/if_ether.h>
     36 #include <linux/if_packet.h>
     37 #include <linux/filter.h>
     38 #include <arpa/inet.h>
     39 #include <ell/ell.h>
     40 
     41 #include "src/module.h"
     42 #include "src/eap.h"
     43 #include "wired/dbus.h"
     44 #include "wired/network.h"
     45 #include "wired/ethdev.h"
     46 
     47 #define ADAPTER_INTERFACE	"net.connman.ead.Adapter"
     48 #define ADAPTER_BASEPATH	EAD_BASE_PATH "/adapter"
     49 
     50 #define PROP_NAME		"Name"
     51 #define PROP_ADDRESS		"Address"
     52 #define PROP_ACTIVE		"Active"
     53 #define PROP_CONNECTED		"Connected"
     54 #define PROP_AUTHENTICATED	"Authenticated"
     55 
     56 struct ethdev {
     57 	uint32_t index;
     58 	char ifname[IFNAMSIZ];
     59 	uint8_t addr[ETH_ALEN];
     60 	bool active;
     61 	bool lower_up;
     62 	bool auth_done;
     63 	struct l_queue *eapol_sessions;
     64 	char *path;
     65 };
     66 
     67 struct eapol {
     68 	struct ethdev *dev;
     69 	uint8_t addr[ETH_ALEN];
     70 	struct eap_state *eap;
     71 	struct l_settings *cred;
     72 };
     73 
     74 static struct l_netlink *rtnl = NULL;
     75 static struct l_queue *ethdev_list = NULL;
     76 static char **whitelist_filter = NULL;
     77 static char **blacklist_filter = NULL;
     78 
     79 static struct l_io *pae_io;
     80 
     81 struct eapol_hdr {
     82 	uint8_t proto_ver;
     83 	uint8_t pkt_type;
     84 	__be16  pkt_len;
     85 } __attribute__ ((packed));
     86 
     87 static const uint8_t eapol_start[] = { 0x01, 0x01, 0x00, 0x00 };
     88 static const uint8_t pae_group_addr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
     89 
     90 static bool pae_write(struct ethdev *dev, const uint8_t *addr,
     91 					const uint8_t *frame, size_t len)
     92 {
     93 	int fd = l_io_get_fd(pae_io);
     94 	struct sockaddr_ll sll;
     95 	ssize_t res;
     96 
     97 	memset(&sll, 0, sizeof(sll));
     98 	sll.sll_family = AF_PACKET;
     99 	sll.sll_ifindex = dev->index;
    100 	sll.sll_protocol = htons(ETH_P_PAE);
    101 	sll.sll_halen = ETH_ALEN;
    102 	memcpy(sll.sll_addr, addr, ETH_ALEN);
    103 
    104 	res = sendto(fd, frame, len, 0,
    105 				(struct sockaddr *) &sll, sizeof(sll));
    106 	if (res < 0)
    107 		return false;
    108 
    109 	return true;
    110 }
    111 
    112 static void eapol_free(void *data)
    113 {
    114 	struct eapol *eapol = data;
    115 
    116 	l_debug("Freeing EAPoL session");
    117 
    118 	eap_free(eapol->eap);
    119 	l_settings_free(eapol->cred);
    120 	l_free(eapol);
    121 }
    122 
    123 static bool eapol_match(const void *a, const void *b)
    124 {
    125 	const struct eapol *eapol = a;
    126 
    127 	return !memcmp(eapol->addr, b, ETH_ALEN);
    128 }
    129 
    130 static struct eapol *eapol_lookup(struct ethdev *dev, const uint8_t *addr)
    131 {
    132 	return l_queue_find(dev->eapol_sessions, eapol_match, addr);
    133 }
    134 
    135 static bool ethdev_match(const void *a, const void *b)
    136 {
    137 	const struct ethdev *dev = a;
    138 	uint32_t index = L_PTR_TO_UINT(b);
    139 
    140 	return (dev->index == index);
    141 }
    142 
    143 static struct ethdev *ethdev_lookup(uint32_t index)
    144 {
    145 	return l_queue_find(ethdev_list, ethdev_match, L_UINT_TO_PTR(index));
    146 }
    147 
    148 static void eap_tx_packet(const uint8_t *eap_data, size_t len, void *user_data)
    149 {
    150 	struct eapol *eapol = user_data;
    151 	uint8_t frame[1500];
    152 
    153 	l_put_u8(0x01, frame);
    154 	l_put_u8(0x00, frame + 1);
    155 	l_put_be16(len, frame + 2);
    156 	memcpy(frame + 4, eap_data, len);
    157 
    158 	/*
    159 	 * The supplicant / client always uses the PAE group address for
    160 	 * sending the EAP packets.
    161 	 */
    162 	pae_write(eapol->dev, pae_group_addr, frame, len + 4);
    163 }
    164 
    165 static void eap_complete(enum eap_result result, void *user_data)
    166 {
    167 	struct eapol *eapol = user_data;
    168 	struct ethdev *dev = eapol->dev;
    169 
    170 	l_debug("result %u", result);
    171 
    172 	if (result == EAP_RESULT_SUCCESS) {
    173 		if (!dev->auth_done) {
    174 			dev->auth_done = true;
    175 			l_dbus_property_changed(dbus_app_get(), dev->path,
    176 							ADAPTER_INTERFACE,
    177 							PROP_AUTHENTICATED);
    178 		}
    179 	}
    180 
    181 	l_queue_remove(dev->eapol_sessions, eapol);
    182 	eapol_free(eapol);
    183 }
    184 
    185 static void eap_key_material(const uint8_t *msk_data, size_t msk_len,
    186 				const uint8_t *emsk_data, size_t emsk_len,
    187 				const uint8_t *iv, size_t iv_len,
    188 				const uint8_t *session_id, size_t session_len,
    189 				void *user_data)
    190 {
    191 	l_debug("EAP key material received");
    192 }
    193 
    194 static void eap_event(unsigned int event, const void *event_data,
    195 							void *user_data)
    196 {
    197 	l_debug("event %u", event);
    198 }
    199 
    200 static void rx_packet(struct ethdev *dev, const uint8_t *addr,
    201 					const void *frame, size_t len)
    202 {
    203 	const struct eapol_hdr *hdr = frame;
    204 	struct eapol *eapol;
    205 	uint16_t pkt_len;
    206 
    207 	if (len < 4) {
    208 		l_error("Too short EAPoL packet with %zu bytes", len);
    209 		return;
    210 	}
    211 
    212 	pkt_len = L_BE16_TO_CPU(hdr->pkt_len);
    213 
    214 	/*
    215 	 * EAPoL packet frames might contain padding at the end and so just
    216 	 * ensure that at least packet body length worth of packet body is
    217 	 * actually present.
    218 	 */
    219 	if (len - 4 < pkt_len) {
    220 		l_error("Missing %zu bytes from EAPoL packet",
    221 							pkt_len - (len - 4));
    222 		return;
    223 	}
    224 
    225 	switch (hdr->pkt_type) {
    226 	case 0x00:	/* EAP-Packet */
    227 		eapol = eapol_lookup(dev, addr);
    228 		if (!eapol) {
    229 			eapol = l_new(struct eapol, 1);
    230 			eapol->dev = dev;
    231 			memcpy(eapol->addr, addr, ETH_ALEN);
    232 			eapol->eap = eap_new(eap_tx_packet,
    233 							eap_complete, eapol);
    234 			if (!eapol->eap) {
    235 				l_error("Failed to create EAP instance");
    236 				l_free(eapol);
    237 				return;
    238 			}
    239 
    240 			l_debug("Created new EAPoL session");
    241 
    242 			eapol->cred = network_lookup_security("default");
    243 			if (!eapol->cred || !eap_load_settings(eapol->eap,
    244 						eapol->cred, "EAP-")) {
    245 				l_error("Failed to load EAP settings");
    246 				eapol_free(eapol);
    247 				return;
    248 			}
    249 
    250 			l_queue_push_tail(dev->eapol_sessions, eapol);
    251 
    252 			eap_set_key_material_func(eapol->eap, eap_key_material);
    253 			eap_set_event_func(eapol->eap, eap_event);
    254 		}
    255 		eap_rx_packet(eapol->eap, frame + 4, pkt_len);
    256 		break;
    257 	}
    258 }
    259 
    260 /*
    261  * BPF filter to match skb->dev->type == 1 (ARPHRD_ETHER) and
    262  * match skb->protocol == 0x888e (PAE).
    263  */
    264 static struct sock_filter pae_filter[] = {
    265 	{ 0x28,  0,  0, 0xfffff01c },	/* ldh #hatype		*/
    266 	{ 0x15,  0,  3, 0x00000001 },	/* jne #1, drop		*/
    267 	{ 0x28,  0,  0, 0xfffff000 },	/* ldh #proto		*/
    268 	{ 0x15,  0,  1, 0x0000888e },	/* jne #0x888e, drop	*/
    269 	{ 0x06,  0,  0, 0xffffffff },	/* keep: ret #-1	*/
    270 	{ 0x06,  0,  0, 0000000000 },	/* drop: ret #0		*/
    271 };
    272 
    273 static const struct sock_fprog pae_fprog = { .len = 6, .filter = pae_filter };
    274 
    275 static bool pae_read(struct l_io *io, void *user_data)
    276 {
    277 	int fd = l_io_get_fd(io);
    278 	struct ethdev *dev;
    279 	struct sockaddr_ll sll;
    280 	socklen_t sll_len;
    281 	ssize_t bytes;
    282 	uint8_t frame[1500];
    283 
    284 	memset(&sll, 0, sizeof(sll));
    285 	sll_len = sizeof(sll);
    286 
    287 	bytes = recvfrom(fd, frame, sizeof(frame), 0,
    288 					(struct sockaddr *) &sll, &sll_len);
    289 	if (bytes <= 0) {
    290 		l_error("Reading from PAE socket failed: %s", strerror(errno));
    291 		return false;
    292 	}
    293 
    294 	if (sll.sll_hatype != ARPHRD_ETHER)
    295 		return true;
    296 
    297 	if (sll.sll_halen != ETH_ALEN)
    298 		return true;
    299 
    300 	if (ntohs(sll.sll_protocol) != ETH_P_PAE)
    301 		return true;
    302 
    303 	if (sll.sll_pkttype != PACKET_HOST &&
    304 					sll.sll_pkttype != PACKET_MULTICAST)
    305 		return true;
    306 
    307 	dev = ethdev_lookup(sll.sll_ifindex);
    308 	if (!dev)
    309 		return true;
    310 
    311 	rx_packet(dev, sll.sll_addr, frame, bytes);
    312 
    313 	return true;
    314 }
    315 
    316 static void pae_destroy(void * user_data)
    317 {
    318 	(void)user_data;
    319 	pae_io = NULL;
    320 }
    321 
    322 static bool pae_open(void)
    323 {
    324 	int fd;
    325 
    326 	fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
    327 							htons(ETH_P_ALL));
    328 	if (fd < 0)
    329 		return false;
    330 
    331 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER,
    332 					&pae_fprog, sizeof(pae_fprog)) < 0) {
    333 		close(fd);
    334 		return false;
    335 	}
    336 
    337 	pae_io = l_io_new(fd);
    338 	l_io_set_close_on_destroy(pae_io, true);
    339 
    340 	l_debug("Opened PAE socket");
    341 
    342 	l_io_set_read_handler(pae_io, pae_read, NULL, pae_destroy);
    343 
    344 	return true;
    345 }
    346 
    347 static void pae_close(void)
    348 {
    349 	l_io_destroy(pae_io);
    350 	pae_io = NULL;
    351 
    352 	l_debug("Closed PAE socket");
    353 }
    354 
    355 static void do_debug(const char *str, void *user_data)
    356 {
    357 	const char *prefix = user_data;
    358 
    359 	l_info("%s%s", prefix, str);
    360 }
    361 
    362 static char *read_devtype_from_uevent(const char *ifname)
    363 {
    364 	char line[128], *filename, *devtype = NULL;
    365 	FILE *f;
    366 
    367 	if (!ifname)
    368 		return NULL;
    369 
    370 	filename = l_strdup_printf("/sys/class/net/%s/uevent", ifname);
    371 	f = fopen(filename, "re");
    372 	l_free(filename);
    373 
    374 	if (!f)
    375 		return NULL;
    376 
    377 	while (fgets(line, sizeof(line), f)) {
    378 		char *pos;
    379 
    380 		pos = strchr(line, '\n');
    381 		if (!pos)
    382 			continue;
    383 		pos[0] = '\0';
    384 
    385 		if (!strncmp(line, "DEVTYPE=", 8)) {
    386 			devtype = l_strdup(line + 8);
    387 			break;
    388 		}
    389 	}
    390 
    391 	fclose(f);
    392 
    393 	return devtype;
    394 }
    395 
    396 static int modify_membership(uint32_t index, int optname)
    397 {
    398 	struct packet_mreq mreq;
    399 	int fd;
    400 
    401 	fd = l_io_get_fd(pae_io);
    402 	if (fd < 0)
    403 		return -1;
    404 
    405 	memset(&mreq, 0, sizeof(mreq));
    406 	mreq.mr_ifindex = index;
    407 	mreq.mr_type = PACKET_MR_MULTICAST;
    408 	mreq.mr_alen = ETH_ALEN;
    409 	memcpy(mreq.mr_address, pae_group_addr, ETH_ALEN);
    410 
    411 	return setsockopt(fd, SOL_PACKET, optname, &mreq, sizeof(mreq));
    412 }
    413 
    414 static void ethdev_free(void *data)
    415 {
    416 	struct ethdev *dev = data;
    417 
    418 	l_debug("Freeing device %s", dev->ifname);
    419 
    420 	if (modify_membership(dev->index, PACKET_DROP_MEMBERSHIP) < 0)
    421 		l_error("Failed to drop membership");
    422 
    423 	l_queue_destroy(dev->eapol_sessions, eapol_free);
    424 
    425 	l_dbus_object_remove_interface(dbus_app_get(), dev->path,
    426 							ADAPTER_INTERFACE);
    427 
    428 	l_free(dev->path);
    429 	l_free(dev);
    430 }
    431 
    432 static bool is_ifname_valid(const char *ifname)
    433 {
    434 	char *pattern;
    435 	unsigned int i;
    436 
    437 	if (!whitelist_filter)
    438 		goto check_blacklist;
    439 
    440 	for (i = 0; (pattern = whitelist_filter[i]); i++) {
    441 		if (fnmatch(pattern, ifname, 0))
    442 			continue;
    443 
    444 		goto check_blacklist;
    445 	}
    446 
    447 	return false;
    448 
    449 check_blacklist:
    450 	if (!blacklist_filter)
    451 		return true;
    452 
    453 	for (i = 0; (pattern = blacklist_filter[i]); i++) {
    454 		if (!fnmatch(pattern, ifname, 0))
    455 			return false;
    456 	}
    457 
    458 	return true;
    459 }
    460 
    461 static void newlink_notify(const struct ifinfomsg *ifi, int bytes)
    462 {
    463 	uint32_t index = ifi->ifi_index;
    464 	struct ethdev *dev;
    465 	const struct rtattr *attr;
    466 	uint8_t *addr = NULL;
    467 	const char *ifname = NULL;
    468 	uint8_t linkmode = 0, operstate = 0;
    469 	bool active, lower_up, lower_changed = false;
    470 
    471 	if (ifi->ifi_type != ARPHRD_ETHER)
    472 		return;
    473 
    474 	active = ifi->ifi_flags & IFF_UP;
    475 	lower_up = ifi->ifi_flags & IFF_LOWER_UP;
    476 
    477 	for (attr = IFLA_RTA(ifi); RTA_OK(attr, bytes);
    478 						attr = RTA_NEXT(attr, bytes)) {
    479 		switch (attr->rta_type) {
    480 		case IFLA_ADDRESS:
    481 			if (RTA_PAYLOAD(attr) == ETH_ALEN)
    482 				addr = RTA_DATA(attr);
    483 			break;
    484 		case IFLA_IFNAME:
    485 			ifname = RTA_DATA(attr);
    486 			break;
    487 		case IFLA_LINKMODE:
    488 			linkmode = l_get_u8(RTA_DATA(attr));
    489 			break;
    490 		case IFLA_OPERSTATE:
    491 			operstate = l_get_u8(RTA_DATA(attr));
    492 			break;
    493 		}
    494 	}
    495 
    496 	if (!addr || !ifname)
    497 		return;
    498 
    499 	l_debug("%s: linkmode %u operstate %u", ifname, linkmode, operstate);
    500 
    501 	if (!is_ifname_valid(ifname)) {
    502 		l_debug("Ignoring device with interface name %s", ifname);
    503 		return;
    504 	}
    505 
    506 	dev = ethdev_lookup(index);
    507 	if (!dev) {
    508 		char *devtype;
    509 
    510 		/*
    511 		 * If there is no existing Ethernet device structure, then
    512 		 * first check uevent if this is wired Ethernet or not.
    513 		 */
    514 		devtype = read_devtype_from_uevent(ifname);
    515 		if (devtype) {
    516 			l_free(devtype);
    517 			return;
    518 		}
    519 
    520 		if (l_queue_isempty(ethdev_list)) {
    521 			if (!pae_open()) {
    522 				l_error("Failed to open PAE port");
    523 				return;
    524 			}
    525 		}
    526 
    527 		if (modify_membership(index, PACKET_ADD_MEMBERSHIP) < 0) {
    528 			l_error("Failed to add membership");
    529 			return;
    530 		}
    531 
    532 		dev = l_new(struct ethdev, 1);
    533 		dev->index = index;
    534 		dev->active = active;
    535 		dev->lower_up = lower_up;
    536 		dev->auth_done = false;
    537 		dev->eapol_sessions = l_queue_new();
    538 		dev->path = l_strdup_printf("%s/%u", ADAPTER_BASEPATH,
    539 								dev->index);
    540 
    541 		l_debug("Creating device %u", dev->index);
    542 
    543 		l_dbus_object_add_interface(dbus_app_get(), dev->path,
    544 						ADAPTER_INTERFACE, dev);
    545 
    546 		l_dbus_object_add_interface(dbus_app_get(), dev->path,
    547 					L_DBUS_INTERFACE_PROPERTIES, NULL);
    548 
    549 		l_queue_push_tail(ethdev_list, dev);
    550 
    551 		lower_changed = true;
    552 	}
    553 
    554 	if (ifname)
    555 		strcpy(dev->ifname, ifname);
    556 
    557 	memcpy(dev->addr, addr, ETH_ALEN);
    558 
    559 	if (active != dev->active) {
    560 		dev->active = active;
    561 		l_dbus_property_changed(dbus_app_get(), dev->path,
    562 					ADAPTER_INTERFACE, PROP_ACTIVE);
    563 	}
    564 
    565 	if (lower_up != dev->lower_up) {
    566 		if (!lower_up) {
    567 			dev->auth_done = false;
    568 			l_dbus_property_changed(dbus_app_get(), dev->path,
    569 					ADAPTER_INTERFACE, PROP_AUTHENTICATED);
    570 		}
    571 
    572 		dev->lower_up = lower_up;
    573 		l_dbus_property_changed(dbus_app_get(), dev->path,
    574 					ADAPTER_INTERFACE, PROP_CONNECTED);
    575 
    576 		lower_changed = true;
    577 	}
    578 
    579 	if (lower_changed) {
    580 		if (dev->lower_up)
    581 			pae_write(dev, pae_group_addr,
    582 					eapol_start, sizeof(eapol_start));
    583 		else
    584 			l_queue_clear(dev->eapol_sessions, eapol_free);
    585 	}
    586 }
    587 
    588 static void dellink_notify(const struct ifinfomsg *ifi, int bytes)
    589 {
    590 	uint32_t index = ifi->ifi_index;
    591 	struct ethdev *dev;
    592 
    593 	if (ifi->ifi_type != ARPHRD_ETHER)
    594 		return;
    595 
    596 	dev = l_queue_remove_if(ethdev_list, ethdev_match,
    597 						L_UINT_TO_PTR(index));
    598 	if (!dev)
    599 		return;
    600 
    601 	l_debug("Removing device %u", dev->index);
    602 
    603 	ethdev_free(dev);
    604 
    605 	if (l_queue_isempty(ethdev_list))
    606 		pae_close();
    607 }
    608 
    609 static void link_notify(uint16_t type, const void *data, uint32_t len,
    610 							void *user_data)
    611 {
    612 	const struct ifinfomsg *ifi = data;
    613 	unsigned int bytes;
    614 
    615 	if (ifi->ifi_type != ARPHRD_ETHER)
    616 		return;
    617 
    618 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
    619 
    620 	switch (type) {
    621 	case RTM_NEWLINK:
    622 		newlink_notify(ifi, bytes);
    623 		break;
    624 	case RTM_DELLINK:
    625 		dellink_notify(ifi, bytes);
    626 		break;
    627 	}
    628 }
    629 
    630 static void getlink_callback(int error, uint16_t type, const void *data,
    631 						uint32_t len, void *user_data)
    632 {
    633 	const struct ifinfomsg *ifi = data;
    634 	unsigned int bytes;
    635 
    636 	if (error) {
    637 		l_error("Failure with link information dump (%d)", error);
    638 		return;
    639 	}
    640 
    641 	if (type != RTM_NEWLINK)
    642 		return;
    643 
    644 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
    645 
    646 	newlink_notify(ifi, bytes);
    647 }
    648 
    649 static bool property_get_name(struct l_dbus *dbus,
    650 					struct l_dbus_message *message,
    651 					struct l_dbus_message_builder *builder,
    652 					void *user_data)
    653 {
    654 	struct ethdev *dev = user_data;
    655 
    656 	l_dbus_message_builder_append_basic(builder, 's', dev->ifname);
    657 	return true;
    658 }
    659 
    660 static bool property_get_address(struct l_dbus *dbus,
    661 					struct l_dbus_message *message,
    662 					struct l_dbus_message_builder *builder,
    663 					void *user_data)
    664 {
    665 	struct ethdev *dev = user_data;
    666         char str[18];
    667 
    668 	sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
    669 				dev->addr[0], dev->addr[1], dev->addr[2],
    670 				dev->addr[3], dev->addr[4], dev->addr[5]);
    671 
    672 	l_dbus_message_builder_append_basic(builder, 's', str);
    673 	return true;
    674 }
    675 
    676 static bool property_get_active(struct l_dbus *dbus,
    677 					struct l_dbus_message *message,
    678 					struct l_dbus_message_builder *builder,
    679 					void *user_data)
    680 {
    681 	struct ethdev *dev = user_data;
    682 	bool active = dev->active;
    683 
    684 	l_dbus_message_builder_append_basic(builder, 'b', &active);
    685 	return true;
    686 }
    687 
    688 static bool property_get_connected(struct l_dbus *dbus,
    689 					struct l_dbus_message *message,
    690 					struct l_dbus_message_builder *builder,
    691 					void *user_data)
    692 {
    693 	struct ethdev *dev = user_data;
    694 	bool connected = dev->lower_up;
    695 
    696 	l_dbus_message_builder_append_basic(builder, 'b', &connected);
    697 	return true;
    698 }
    699 
    700 static bool property_get_authenticated(struct l_dbus *dbus,
    701 					struct l_dbus_message *message,
    702 					struct l_dbus_message_builder *builder,
    703 					void *user_data)
    704 {
    705 	struct ethdev *dev = user_data;
    706 	bool authenticated = dev->auth_done;
    707 
    708 	l_dbus_message_builder_append_basic(builder, 'b', &authenticated);
    709 	return true;
    710 }
    711 
    712 static void setup_adapter_interface(struct l_dbus_interface *interface)
    713 {
    714 	l_dbus_interface_property(interface, PROP_NAME, 0, "s",
    715 					property_get_name, NULL);
    716 	l_dbus_interface_property(interface, PROP_ADDRESS, 0, "s",
    717 					property_get_address, NULL);
    718 	l_dbus_interface_property(interface, PROP_ACTIVE, 0, "b",
    719 					property_get_active, NULL);
    720 	l_dbus_interface_property(interface, PROP_CONNECTED, 0, "b",
    721 					property_get_connected, NULL);
    722 	l_dbus_interface_property(interface, PROP_AUTHENTICATED, 0, "b",
    723 					property_get_authenticated, NULL);
    724 }
    725 
    726 bool ethdev_init(const char *whitelist, const char *blacklist)
    727 {
    728 	struct ifinfomsg ifi;
    729 	struct l_netlink_message *nlm;
    730 
    731 	if (rtnl)
    732 		return false;
    733 
    734 	l_debug("Opening route netlink socket");
    735 
    736 	rtnl = l_netlink_new(NETLINK_ROUTE);
    737 	if (!rtnl) {
    738 		l_error("Failed to open route netlink socket");
    739 		return false;
    740 	}
    741 
    742 	if (getenv("EAD_RTNL_DEBUG"))
    743 		l_netlink_set_debug(rtnl, do_debug, "[RTNL] ", NULL);
    744 
    745 	if (!l_netlink_register(rtnl, RTNLGRP_LINK, link_notify, NULL, NULL)) {
    746 		l_error("Failed to register for RTNL link notifications");
    747 		l_netlink_destroy(rtnl);
    748 		return false;
    749 	}
    750 
    751 	ethdev_list = l_queue_new();
    752 
    753 	if (!l_dbus_register_interface(dbus_app_get(), ADAPTER_INTERFACE,
    754 					setup_adapter_interface, NULL, false)) {
    755 		l_error("Unable to register the adapter interface");
    756 		l_netlink_destroy(rtnl);
    757 		return false;
    758 	}
    759 
    760 	if (whitelist)
    761 		whitelist_filter = l_strsplit(whitelist, ',');
    762 
    763 	if (blacklist)
    764 		blacklist_filter = l_strsplit(blacklist, ',');
    765 
    766 	memset(&ifi, 0, sizeof(ifi));
    767 	nlm = l_netlink_message_new_sized(RTM_GETLINK, NLM_F_DUMP, sizeof(ifi));
    768 	l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
    769 
    770 	l_netlink_send(rtnl, nlm, getlink_callback, NULL, NULL);
    771 
    772 	return true;
    773 }
    774 
    775 void ethdev_exit(void)
    776 {
    777 	if (!rtnl)
    778 		return;
    779 
    780 	l_dbus_unregister_interface(dbus_app_get(), ADAPTER_INTERFACE);
    781 
    782 	pae_close();
    783 
    784 	l_strfreev(whitelist_filter);
    785 	l_strfreev(blacklist_filter);
    786 
    787 	l_debug("Closing route netlink socket");
    788 	l_netlink_destroy(rtnl);
    789 	rtnl = NULL;
    790 
    791 	l_queue_destroy(ethdev_list, ethdev_free);
    792 	ethdev_list = NULL;
    793 }