Merge wpa_supplicant and hostapd driver wrapper implementations
[wpasupplicant] / src / drivers / driver_wired.c
index 419fc45..200a2b3 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * WPA Supplicant - wired Ethernet driver interface
  * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -17,6 +18,8 @@
 #include <net/if.h>
 #ifdef __linux__
 #include <netpacket/packet.h>
+#include <net/if_arp.h>
+#include <net/if.h>
 #endif /* __linux__ */
 #ifdef __FreeBSD__
 #include <net/if_dl.h>
 #include "common.h"
 #include "driver.h"
 
+#ifdef HOSTAPD
+#include "eloop.h"
+#include "../../hostapd/hostapd.h"
+#include "../../hostapd/config.h"
+#include "../../hostapd/sta_info.h"
+#include "../../hostapd/accounting.h"
+#endif /* HOSTAPD */
 
 static const u8 pae_group_addr[ETH_ALEN] =
 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
 
 
 struct wpa_driver_wired_data {
+#ifdef HOSTAPD
+       struct hostapd_data *hapd;
+       char iface[IFNAMSIZ + 1];
+
+       int sock; /* raw packet socket for driver access */
+       int dhcp_sock; /* socket for dhcp packets */
+       int use_pae_group_addr;
+#else /* HOSTAPD */
        void *ctx;
        int pf_sock;
        char ifname[IFNAMSIZ + 1];
        int membership, multi, iff_allmulti, iff_up;
+#endif /* HOSTAPD */
+};
+
+
+#ifdef HOSTAPD
+
+/* TODO: detecting new devices should eventually be changed from using DHCP
+ * snooping to trigger on any packet from a new layer 2 MAC address, e.g.,
+ * based on ebtables, etc. */
+
+struct dhcp_message {
+       u_int8_t op;
+       u_int8_t htype;
+       u_int8_t hlen;
+       u_int8_t hops;
+       u_int32_t xid;
+       u_int16_t secs;
+       u_int16_t flags;
+       u_int32_t ciaddr;
+       u_int32_t yiaddr;
+       u_int32_t siaddr;
+       u_int32_t giaddr;
+       u_int8_t chaddr[16];
+       u_int8_t sname[64];
+       u_int8_t file[128];
+       u_int32_t cookie;
+       u_int8_t options[308]; /* 312 - cookie */
 };
 
 
+static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
+{
+       struct sta_info *sta;
+
+       sta = ap_get_sta(hapd, addr);
+       if (sta)
+               return;
+
+       wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
+                  " - adding a new STA", MAC2STR(addr));
+       sta = ap_sta_add(hapd, addr);
+       if (sta) {
+               hostapd_new_assoc_sta(hapd, sta, 0);
+       } else {
+               wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
+                          MAC2STR(addr));
+       }
+}
+
+
+static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
+                       size_t len)
+{
+       struct ieee8023_hdr *hdr;
+       u8 *pos, *sa;
+       size_t left;
+
+       /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
+        * 2 byte ethertype */
+       if (len < 14) {
+               wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
+                          (unsigned long) len);
+               return;
+       }
+
+       hdr = (struct ieee8023_hdr *) buf;
+
+       switch (ntohs(hdr->ethertype)) {
+               case ETH_P_PAE:
+                       wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
+                       sa = hdr->src;
+                       wired_possible_new_sta(hapd, sa);
+
+                       pos = (u8 *) (hdr + 1);
+                       left = len - sizeof(*hdr);
+
+                       hostapd_eapol_receive(hapd, sa, pos, left);
+               break;
+
+       default:
+               wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
+                          ntohs(hdr->ethertype));
+               break;
+       }
+}
+
+
+static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
+{
+       struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
+       int len;
+       unsigned char buf[3000];
+
+       len = recv(sock, buf, sizeof(buf), 0);
+       if (len < 0) {
+               perror("recv");
+               return;
+       }
+
+       handle_data(hapd, buf, len);
+}
+
+
+static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
+{
+       struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
+       int len;
+       unsigned char buf[3000];
+       struct dhcp_message *msg;
+       u8 *mac_address;
+
+       len = recv(sock, buf, sizeof(buf), 0);
+       if (len < 0) {
+               perror("recv");
+               return;
+       }
+
+       /* must contain at least dhcp_message->chaddr */
+       if (len < 44) {
+               wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
+               return;
+       }
+
+       msg = (struct dhcp_message *) buf;
+       mac_address = (u8 *) &(msg->chaddr);
+
+       wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
+                  MAC2STR(mac_address));
+
+       wired_possible_new_sta(hapd, mac_address);
+}
+
+
+static int wired_init_sockets(struct wpa_driver_wired_data *drv)
+{
+       struct hostapd_data *hapd = drv->hapd;
+       struct ifreq ifr;
+       struct sockaddr_ll addr;
+       struct sockaddr_in addr2;
+       struct packet_mreq mreq;
+       int n = 1;
+
+       drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
+       if (drv->sock < 0) {
+               perror("socket[PF_PACKET,SOCK_RAW]");
+               return -1;
+       }
+
+       if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
+               printf("Could not register read socket\n");
+               return -1;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
+       if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
+               perror("ioctl(SIOCGIFINDEX)");
+               return -1;
+       }
+
+       memset(&addr, 0, sizeof(addr));
+       addr.sll_family = AF_PACKET;
+       addr.sll_ifindex = ifr.ifr_ifindex;
+       wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
+                  addr.sll_ifindex);
+
+       if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+               perror("bind");
+               return -1;
+       }
+
+       /* filter multicast address */
+       memset(&mreq, 0, sizeof(mreq));
+       mreq.mr_ifindex = ifr.ifr_ifindex;
+       mreq.mr_type = PACKET_MR_MULTICAST;
+       mreq.mr_alen = 6;
+       memcpy(mreq.mr_address, pae_group_addr, mreq.mr_alen);
+
+       if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
+                      sizeof(mreq)) < 0) {
+               perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
+               return -1;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
+       if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
+               perror("ioctl(SIOCGIFHWADDR)");
+               return -1;
+       }
+
+       if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
+               printf("Invalid HW-addr family 0x%04x\n",
+                      ifr.ifr_hwaddr.sa_family);
+               return -1;
+       }
+       memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+
+       /* setup dhcp listen socket for sta detection */
+       if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
+               perror("socket call failed for dhcp");
+               return -1;
+       }
+
+       if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
+       {
+               printf("Could not register read socket\n");
+               return -1;
+       }
+
+       memset(&addr2, 0, sizeof(addr2));
+       addr2.sin_family = AF_INET;
+       addr2.sin_port = htons(67);
+       addr2.sin_addr.s_addr = INADDR_ANY;
+
+       if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
+                      sizeof(n)) == -1) {
+               perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
+               return -1;
+       }
+       if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
+                      sizeof(n)) == -1) {
+               perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
+               return -1;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_ifrn.ifrn_name, drv->iface, IFNAMSIZ);
+       if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
+                      (char *) &ifr, sizeof(ifr)) < 0) {
+               perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
+               return -1;
+       }
+
+       if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
+                sizeof(struct sockaddr)) == -1) {
+               perror("bind");
+               return -1;
+       }
+
+       return 0;
+}
+
+
+static int wired_send_eapol(void *priv, const u8 *addr,
+                           const u8 *data, size_t data_len, int encrypt,
+                           const u8 *own_addr)
+{
+       struct wpa_driver_wired_data *drv = priv;
+       struct ieee8023_hdr *hdr;
+       size_t len;
+       u8 *pos;
+       int res;
+
+       len = sizeof(*hdr) + data_len;
+       hdr = os_zalloc(len);
+       if (hdr == NULL) {
+               printf("malloc() failed for wired_send_eapol(len=%lu)\n",
+                      (unsigned long) len);
+               return -1;
+       }
+
+       memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
+              ETH_ALEN);
+       memcpy(hdr->src, own_addr, ETH_ALEN);
+       hdr->ethertype = htons(ETH_P_PAE);
+
+       pos = (u8 *) (hdr + 1);
+       memcpy(pos, data, data_len);
+
+       res = send(drv->sock, (u8 *) hdr, len, 0);
+       free(hdr);
+
+       if (res < 0) {
+               perror("wired_send_eapol: send");
+               printf("wired_send_eapol - packet len: %lu - failed\n",
+                      (unsigned long) len);
+       }
+
+       return res;
+}
+
+
+static void * wired_driver_hapd_init(struct hostapd_data *hapd)
+{
+       struct wpa_driver_wired_data *drv;
+
+       drv = os_zalloc(sizeof(struct wpa_driver_wired_data));
+       if (drv == NULL) {
+               printf("Could not allocate memory for wired driver data\n");
+               return NULL;
+       }
+
+       drv->hapd = hapd;
+       os_strlcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
+       drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
+
+       if (wired_init_sockets(drv)) {
+               free(drv);
+               return NULL;
+       }
+
+       return drv;
+}
+
+
+static void wired_driver_hapd_deinit(void *priv)
+{
+       struct wpa_driver_wired_data *drv = priv;
+
+       if (drv->sock >= 0)
+               close(drv->sock);
+
+       if (drv->dhcp_sock >= 0)
+               close(drv->dhcp_sock);
+
+       free(drv);
+}
+
+#else /* HOSTAPD */
+
 static int wpa_driver_wired_get_ssid(void *priv, u8 *ssid)
 {
        ssid[0] = 0;
@@ -136,7 +472,7 @@ static int wpa_driver_wired_multi(const char *ifname, const u8 *addr, int add)
                dlp->sdl_nlen = 0;
                dlp->sdl_alen = ETH_ALEN;
                dlp->sdl_slen = 0;
-               os_memcpy(LLADDR(dlp), addr, ETH_ALEN); 
+               os_memcpy(LLADDR(dlp), addr, ETH_ALEN);
        }
 #endif /* __FreeBSD__ */
 
@@ -194,9 +530,9 @@ static void * wpa_driver_wired_init(void *ctx, const char *ifname)
        if (drv->pf_sock < 0)
                perror("socket(PF_PACKET)");
 #else /* __linux__ */
-       drv->pf_sock = -1;       
+       drv->pf_sock = -1;
 #endif /* __linux__ */
-       
+
        if (wpa_driver_wired_get_ifflags(ifname, &flags) == 0 &&
            !(flags & IFF_UP) &&
            wpa_driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0) {
@@ -270,17 +606,24 @@ static void wpa_driver_wired_deinit(void *priv)
 
        if (drv->pf_sock != -1)
                close(drv->pf_sock);
-       
+
        os_free(drv);
 }
+#endif /* HOSTAPD */
 
 
 const struct wpa_driver_ops wpa_driver_wired_ops = {
        .name = "wired",
-       .desc = "wpa_supplicant wired Ethernet driver",
+       .desc = "Wired Ethernet driver",
+#ifdef HOSTAPD
+       .hapd_init = wired_driver_hapd_init,
+       .hapd_deinit = wired_driver_hapd_deinit,
+       .hapd_send_eapol = wired_send_eapol,
+#else /* HOSTAPD */
        .get_ssid = wpa_driver_wired_get_ssid,
        .get_bssid = wpa_driver_wired_get_bssid,
        .get_capa = wpa_driver_wired_get_capa,
        .init = wpa_driver_wired_init,
        .deinit = wpa_driver_wired_deinit,
+#endif /* HOSTAPD */
 };