eiwd

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

commit b8d45a440a5f71bd1ac3fed892b8520a3e8feb8d
parent dd5143456026bfbee5818d8ef795af5c9a0527ef
Author: Marcel Holtmann <marcel@holtmann.org>
Date:   Fri, 14 Sep 2018 19:48:23 +0200

wired: Add initial version of Ethernet authentication daemon

Diffstat:
MMakefile.am | 3++-
Mwired/ethdev.c | 530++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mwired/ethdev.h | 4+++-
Mwired/main.c | 107++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Awired/network.c | 175+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Awired/network.h | 28++++++++++++++++++++++++++++
6 files changed, 843 insertions(+), 4 deletions(-)

diff --git a/Makefile.am b/Makefile.am @@ -196,7 +196,8 @@ endif if WIRED libexec_PROGRAMS += wired/ead -wired_ead_SOURCES = wired/main.c wired/ethdev.h wired/ethdev.c $(eap_sources) +wired_ead_SOURCES = wired/main.c wired/ethdev.h wired/ethdev.c \ + wired/network.h wired/network.c $(eap_sources) wired_ead_LDADD = ell/libell-internal.la wired_ead_DEPENDENCIES = ell/libell-internal.la diff --git a/wired/ethdev.c b/wired/ethdev.c @@ -20,16 +20,544 @@ * */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fnmatch.h> +#include <linux/rtnetlink.h> +#include <linux/if.h> +#include <linux/if_ether.h> +#include <linux/if_packet.h> +#include <linux/filter.h> +#include <arpa/inet.h> +#include <net/if_arp.h> #include <ell/ell.h> #include "src/eap.h" +#include "wired/network.h" #include "wired/ethdev.h" -bool ethdev_init(void) +struct ethdev { + uint32_t index; + char ifname[IFNAMSIZ]; + uint8_t addr[ETH_ALEN]; + struct l_queue *eapol_sessions; +}; + +struct eapol { + struct ethdev *dev; + uint8_t addr[ETH_ALEN]; + struct eap_state *eap; + struct l_settings *cred; +}; + +static struct l_netlink *rtnl = NULL; +static struct l_queue *ethdev_list = NULL; +static char **whitelist_filter; +static char **blacklist_filter; + +static struct l_io *pae_io; + +struct eapol_hdr { + uint8_t proto_ver; + uint8_t pkt_type; + __be16 pkt_len; +} __attribute__ ((packed)); + +static const uint8_t eapol_start[] = { 0x02, 0x01, 0x00, 0x00 }; +static const uint8_t pae_group_addr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }; + +static bool pae_write(struct ethdev *dev, const uint8_t *addr, + const uint8_t *frame, size_t len) +{ + int fd = l_io_get_fd(pae_io); + struct sockaddr_ll sll; + ssize_t res; + + memset(&sll, 0, sizeof(sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = dev->index; + sll.sll_protocol = htons(ETH_P_PAE); + sll.sll_halen = ETH_ALEN; + memcpy(sll.sll_addr, addr, ETH_ALEN); + + res = sendto(fd, frame, len, 0, + (struct sockaddr *) &sll, sizeof(sll)); + if (res < 0) + return false; + + return true; +} + +static void eapol_free(void *data) +{ + struct eapol *eapol = data; + + l_debug("Freeing EAPoL session"); + + eap_free(eapol->eap); + l_settings_free(eapol->cred); + l_free(eapol); +} + +static bool eapol_match(const void *a, const void *b) +{ + const struct eapol *eapol = a; + + return !memcmp(eapol->addr, b, ETH_ALEN); +} + +static struct eapol *eapol_lookup(struct ethdev *dev, const uint8_t *addr) +{ + return l_queue_find(dev->eapol_sessions, eapol_match, addr); +} + +static bool ethdev_match(const void *a, const void *b) +{ + const struct ethdev *dev = a; + uint32_t index = L_PTR_TO_UINT(b); + + return (dev->index == index); +} + +static struct ethdev *ethdev_lookup(uint32_t index) +{ + return l_queue_find(ethdev_list, ethdev_match, L_UINT_TO_PTR(index)); +} + +static void eap_tx_packet(const uint8_t *eap_data, size_t len, void *user_data) +{ + struct eapol *eapol = user_data; + uint8_t frame[1500]; + + l_put_u8(0x02, frame); + l_put_u8(0x00, frame + 1); + l_put_be16(len, frame + 2); + memcpy(frame + 4, eap_data, len); + + pae_write(eapol->dev, eapol->addr, frame, len + 4); +} + +static void eap_complete(enum eap_result result, void *user_data) +{ + struct eapol *eapol = user_data; + struct ethdev *dev = eapol->dev; + + l_debug("result %u", result); + + l_queue_remove(dev->eapol_sessions, eapol); + eapol_free(eapol); +} + +static void rx_packet(struct ethdev *dev, const uint8_t *addr, + const void *frame, size_t len) +{ + const struct eapol_hdr *hdr = frame; + struct eapol *eapol; + + if (len < 4) { + l_error("Too short EAPoL packet with %zu bytes", len); + return; + } + + if (L_BE16_TO_CPU(hdr->pkt_len) != len - 4) { + l_error("Length mismatch with EAPoL packet"); + return; + } + + switch (hdr->pkt_type) { + case 0x00: /* EAP-Packet */ + eapol = eapol_lookup(dev, addr); + if (!eapol) { + eapol = l_new(struct eapol, 1); + eapol->dev = dev; + memcpy(eapol->addr, addr, ETH_ALEN); + eapol->eap = eap_new(eap_tx_packet, + eap_complete, eapol); + if (!eapol->eap) { + l_error("Failed to create EAP instance"); + l_free(eapol); + return; + } + + l_debug("Created new EAPoL session"); + + l_queue_push_tail(dev->eapol_sessions, eapol); + + eapol->cred = network_lookup_security("default"); + eap_load_settings(eapol->eap, eapol->cred, "EAP-"); + } + eap_rx_packet(eapol->eap, frame + 4, len - 4); + break; + } +} + +/* + * BPF filter to match skb->dev->type == 1 (ARPHRD_ETHER) and + * match skb->protocol == 0x888e (PAE). + */ +static struct sock_filter pae_filter[] = { + { 0x28, 0, 0, 0xfffff01c }, /* ldh #hatype */ + { 0x15, 0, 3, 0x00000001 }, /* jne #1, drop */ + { 0x28, 0, 0, 0xfffff000 }, /* ldh #proto */ + { 0x15, 0, 1, 0x0000888e }, /* jne #0x888e, drop */ + { 0x06, 0, 0, 0xffffffff }, /* keep: ret #-1 */ + { 0x06, 0, 0, 0000000000 }, /* drop: ret #0 */ +}; + +static const struct sock_fprog pae_fprog = { .len = 6, .filter = pae_filter }; + +static bool pae_read(struct l_io *io, void *user_data) +{ + int fd = l_io_get_fd(io); + struct ethdev *dev; + struct sockaddr_ll sll; + socklen_t sll_len; + ssize_t bytes; + uint8_t frame[1500]; + + memset(&sll, 0, sizeof(sll)); + sll_len = sizeof(sll); + + bytes = recvfrom(fd, frame, sizeof(frame), 0, + (struct sockaddr *) &sll, &sll_len); + if (bytes <= 0) { + l_error("Reading from PAE socket failed: %s", strerror(errno)); + return false; + } + + if (sll.sll_halen != ETH_ALEN) + return true; + + if (ntohs(sll.sll_protocol) != ETH_P_PAE) + return true; + + dev = ethdev_lookup(sll.sll_ifindex); + if (!dev) + return true; + + rx_packet(dev, sll.sll_addr, frame, bytes); + + return true; +} + +static void pae_destroy() +{ + pae_io = NULL; +} + +static bool pae_open(void) +{ + int fd; + + fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + htons(ETH_P_ALL)); + if (fd < 0) + return false; + + if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, + &pae_fprog, sizeof(pae_fprog)) < 0) { + close(fd); + return false; + } + + pae_io = l_io_new(fd); + l_io_set_close_on_destroy(pae_io, true); + + l_debug("Opened PAE socket"); + + l_io_set_read_handler(pae_io, pae_read, NULL, pae_destroy); + + return true; +} + +static void pae_close(void) +{ + l_io_destroy(pae_io); + pae_io = NULL; + + l_debug("Closed PAE socket"); +} + +static void do_debug(const char *str, void *user_data) +{ + const char *prefix = user_data; + + l_info("%s%s", prefix, str); +} + +static char *read_devtype_from_uevent(const char *ifname) { + char line[128], *filename, *devtype = NULL; + FILE *f; + + if (!ifname) + return NULL; + + filename = l_strdup_printf("/sys/class/net/%s/uevent", ifname); + f = fopen(filename, "re"); + l_free(filename); + + if (!f) + return NULL; + + while (fgets(line, sizeof(line), f)) { + char *pos; + + pos = strchr(line, '\n'); + if (!pos) + continue; + pos[0] = '\0'; + + if (!strncmp(line, "DEVTYPE=", 8)) { + devtype = l_strdup(line + 8); + break; + } + } + + fclose(f); + + return devtype; +} + +static void ethdev_free(void *data) +{ + struct ethdev *dev = data; + + l_debug("Freeing device %s", dev->ifname); + + l_queue_destroy(dev->eapol_sessions, eapol_free); + l_free(dev); +} + +static bool is_ifname_valid(const char *ifname) +{ + char *pattern; + unsigned int i; + + if (!whitelist_filter) + goto check_blacklist; + + for (i = 0; (pattern = whitelist_filter[i]); i++) { + if (fnmatch(pattern, ifname, 0)) + continue; + + goto check_blacklist; + } + return false; + +check_blacklist: + if (!blacklist_filter) + return true; + + for (i = 0; (pattern = blacklist_filter[i]); i++) { + if (!fnmatch(pattern, ifname, 0)) + return false; + } + + return true; +} + +static void newlink_notify(const struct ifinfomsg *ifi, int bytes) +{ + uint32_t index = ifi->ifi_index; + struct ethdev *dev; + const struct rtattr *attr; + uint8_t *addr = NULL; + const char *ifname = NULL; + uint8_t linkmode = 0, operstate = 0; + + if (ifi->ifi_type != ARPHRD_ETHER) + return; + + for (attr = IFLA_RTA(ifi); RTA_OK(attr, bytes); + attr = RTA_NEXT(attr, bytes)) { + switch (attr->rta_type) { + case IFLA_ADDRESS: + if (RTA_PAYLOAD(attr) == ETH_ALEN) + addr = RTA_DATA(attr); + break; + case IFLA_IFNAME: + ifname = RTA_DATA(attr); + break; + case IFLA_LINKMODE: + linkmode = l_get_u8(RTA_DATA(attr)); + break; + case IFLA_OPERSTATE: + operstate = l_get_u8(RTA_DATA(attr)); + break; + } + } + + if (!addr || !ifname) + return; + + l_info("%s: linkmode %u operstate %u", ifname, linkmode, operstate); + + if (!is_ifname_valid(ifname)) { + l_debug("Ignoring device with interface name %s", ifname); + return; + } + + dev = ethdev_lookup(index); + if (!dev) { + char *devtype; + + /* + * If there is no existing Ethernet device structure, then + * first check uevent if this is wired Ethernet or not. + */ + devtype = read_devtype_from_uevent(ifname); + if (devtype) { + l_free(devtype); + return; + } + + if (l_queue_isempty(ethdev_list)) { + if (!pae_open()) { + l_error("Failed to open PAE port"); + return; + } + } + + dev = l_new(struct ethdev, 1); + dev->index = index; + dev->eapol_sessions = l_queue_new(); + + l_debug("Creating device %u", dev->index); + + l_queue_push_tail(ethdev_list, dev); + + pae_write(dev, pae_group_addr, + eapol_start, sizeof(eapol_start)); + } + + if (ifname) + strcpy(dev->ifname, ifname); + + memcpy(dev->addr, addr, ETH_ALEN); +} + +static void dellink_notify(const struct ifinfomsg *ifi, int bytes) +{ + uint32_t index = ifi->ifi_index; + struct ethdev *dev; + + if (ifi->ifi_type != ARPHRD_ETHER) + return; + + dev = l_queue_remove_if(ethdev_list, ethdev_match, + L_UINT_TO_PTR(index)); + if (!dev) + return; + + l_debug("Removing device %u", dev->index); + + ethdev_free(dev); + + if (l_queue_isempty(ethdev_list)) + pae_close(); +} + +static void link_notify(uint16_t type, const void *data, uint32_t len, + void *user_data) +{ + const struct ifinfomsg *ifi = data; + unsigned int bytes; + + if (ifi->ifi_type != ARPHRD_ETHER) + return; + + bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg)); + + switch (type) { + case RTM_NEWLINK: + newlink_notify(ifi, bytes); + break; + case RTM_DELLINK: + dellink_notify(ifi, bytes); + break; + } +} + +static void getlink_callback(int error, uint16_t type, const void *data, + uint32_t len, void *user_data) +{ + const struct ifinfomsg *ifi = data; + unsigned int bytes; + + if (error) { + l_error("Failure with link information dump (%d)", error); + return; + } + + if (type != RTM_NEWLINK) + return; + + bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg)); + + newlink_notify(ifi, bytes); +} + +bool ethdev_init(const char *whitelist, const char *blacklist) +{ + struct ifinfomsg msg; + + 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("EAD_RTNL_DEBUG")) + l_netlink_set_debug(rtnl, do_debug, "[RTNL] ", NULL); + + if (!l_netlink_register(rtnl, RTNLGRP_LINK, link_notify, NULL, NULL)) { + l_error("Failed to register for RTNL link notifications"); + l_netlink_destroy(rtnl); + return false; + } + + ethdev_list = l_queue_new(); + + if (whitelist) + whitelist_filter = l_strsplit(whitelist, ','); + + if (blacklist) + blacklist_filter = l_strsplit(blacklist, ','); + + memset(&msg, 0, sizeof(msg)); + + l_netlink_send(rtnl, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg), + getlink_callback, NULL, NULL); + + return true; } void ethdev_exit(void) { + if (!rtnl) + return; + + l_queue_destroy(ethdev_list, ethdev_free); + ethdev_list = NULL; + + pae_close(); + + l_strfreev(whitelist_filter); + l_strfreev(blacklist_filter); + + l_debug("Closing route netlink socket"); + l_netlink_destroy(rtnl); + rtnl = NULL; } diff --git a/wired/ethdev.h b/wired/ethdev.h @@ -20,5 +20,7 @@ * */ -bool ethdev_init(void); +struct ethdev; + +bool ethdev_init(const char *whitelist, const char *blacklist); void ethdev_exit(void); diff --git a/wired/main.c b/wired/main.c @@ -24,19 +24,124 @@ #include <config.h> #endif +#include <stdio.h> #include <stdlib.h> +#include <errno.h> +#include <getopt.h> #include <ell/ell.h> #include "src/eap.h" #include "wired/ethdev.h" +#include "wired/network.h" + +static void signal_handler(struct l_signal *signal, uint32_t signo, + void *user_data) +{ + switch (signo) { + case SIGINT: + case SIGTERM: + l_info("Terminate"); + l_main_quit(); + break; + } +} + +static void usage(void) +{ + printf("ead - Authentication daemon\n" + "Usage:\n"); + printf("\tead [options]\n"); + printf("Options:\n" + "\t-i, --interfaces Interfaces to manage\n" + "\t-I, --nointerfaces Interfaces to ignore\n" + "\t-d, --debug Enable debug output\n" + "\t-h, --help Show help options\n"); +} + +static const struct option main_options[] = { + { "interfaces", required_argument, NULL, 'i' }, + { "nointerfaces", required_argument, NULL, 'I' }, + { "debug", optional_argument, NULL, 'd' }, + { "version", no_argument, NULL, 'v' }, + { "help", no_argument, NULL, 'h' }, + { } +}; int main(int argc, char *argv[]) { + struct l_signal *signal; + sigset_t mask; + const char *interfaces = NULL; + const char *nointerfaces = NULL; + const char *debugopt = NULL; + + for (;;) { + int opt; + + opt = getopt_long(argc, argv, "i:I:d::vh", main_options, NULL); + if (opt < 0) + break; + + switch (opt) { + case 'i': + interfaces = optarg; + break; + case 'I': + nointerfaces = optarg; + break; + case 'd': + if (optarg) + debugopt = optarg; + else if (argv[optind] && argv[optind][0] != '-') + debugopt = argv[optind++]; + else + debugopt = "*"; + break; + case 'v': + printf("%s\n", VERSION); + return EXIT_SUCCESS; + case 'h': + usage(); + return EXIT_SUCCESS; + default: + return EXIT_FAILURE; + } + } + + if (argc - optind > 0) { + fprintf(stderr, "Invalid command line parameters\n"); + return EXIT_FAILURE; + } + + l_log_set_stderr(); + + if (!l_main_init()) + return EXIT_FAILURE; + + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); + + signal = l_signal_create(&mask, signal_handler, NULL, NULL); + + if (debugopt) + l_debug_enable(debugopt); + + l_info("Authentication daemon version %s", VERSION); + eap_init(0); - ethdev_init(); + network_init(); + ethdev_init(interfaces, nointerfaces); + + l_main_run(); ethdev_exit(); + network_exit(); eap_exit(); + l_signal_remove(signal); + + l_main_exit(); + return EXIT_SUCCESS; } diff --git a/wired/network.c b/wired/network.c @@ -0,0 +1,175 @@ +/* + * + * Ethernet daemon for Linux + * + * Copyright (C) 2017-2018 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 <config.h> +#endif + +#include <errno.h> +#include <dirent.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <ell/ell.h> + +#include "wired/network.h" + +struct network { + char *filename; +}; + +static struct l_queue *network_list; +static struct l_fswatch *storage_watch; + +static struct network *network_new(const char *filename) +{ + struct network *net; + + l_debug("Creating network %s", filename); + + net = l_new(struct network, 1); + net->filename = l_strdup(filename); + + return net; +} + +static void network_free(void *data) +{ + struct network *net = data; + + l_debug("Freeing network %s", net->filename); + + l_free(net->filename); + l_free(net); +} + +static bool network_match(const void *a, const void *b) +{ + const struct network *net = a; + const char *filename = b; + + return strcmp(net->filename, filename); +} + +static struct network *network_lookup(const char *filename) +{ + return l_queue_find(network_list, network_match, filename); +} + +struct l_settings *network_lookup_security(const char *filename) +{ + struct l_settings *conf; + char *path; + + path = l_strdup_printf("%s/%s.8021x", WIRED_STORAGEDIR, filename); + + l_debug("Loading %s", path); + + conf = l_settings_new(); + l_settings_load_from_file(conf, path); + + l_free(path); + + return conf; +} + +static void network_storage_watch_cb(struct l_fswatch *watch, + const char *filename, + enum l_fswatch_event event, + void *user_data) +{ + struct network *net; + + /* + * Ignore notifications for the actual directory, we can't do + * anything about some of them anyway. Only react to + * notifications for files in the storage directory. + */ + if (!filename) + return; + + switch (event) { + case L_FSWATCH_EVENT_DELETE: + case L_FSWATCH_EVENT_MOVE: + case L_FSWATCH_EVENT_MODIFY: + case L_FSWATCH_EVENT_ATTRIB: + case L_FSWATCH_EVENT_CREATE: + /* + * For now treat all the operations the same. E.g. they may + * result in the removal of the network (file moved out, not + * readable or invalid) or the creation of a new network (file + * created, permissions granted, syntax fixed, etc.) + * so we always need to re-read the file. + */ + net = network_lookup(filename); + if (!net) { + net = network_new(filename); + l_queue_push_tail(network_list, net); + } + } +} + +static void network_storage_watch_destroy(void *user_data) +{ + storage_watch = NULL; +} + +bool network_init(void) +{ + DIR *dir; + struct dirent *dirent; + + dir = opendir(WIRED_STORAGEDIR); + if (!dir) { + l_info("Unable to open %s: %s", WIRED_STORAGEDIR, + strerror(errno)); + return false; + } + + network_list = l_queue_new(); + + while ((dirent = readdir(dir))) { + struct network *net; + + if (dirent->d_type != DT_REG && dirent->d_type != DT_LNK) + continue; + + net = network_new(dirent->d_name); + l_queue_push_tail(network_list, net); + } + + closedir(dir); + + storage_watch = l_fswatch_new(WIRED_STORAGEDIR, + network_storage_watch_cb, NULL, + network_storage_watch_destroy); + + return true; +} + +void network_exit(void) +{ + l_fswatch_destroy(storage_watch); + + l_queue_destroy(network_list, network_free); + network_list = NULL; +} diff --git a/wired/network.h b/wired/network.h @@ -0,0 +1,28 @@ +/* + * + * Ethernet daemon for Linux + * + * Copyright (C) 2017-2018 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 + * + */ + +struct l_settings; + +bool network_init(void); +void network_exit(void); + +struct l_settings *network_lookup_security(const char *filename);