2008-02-28 02:34:43 +01:00
|
|
|
/*
|
2009-12-12 21:07:29 +01:00
|
|
|
* Wired Ethernet driver interface
|
2009-12-12 19:39:25 +01:00
|
|
|
* Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
|
2009-04-09 12:40:12 +02:00
|
|
|
* Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
|
2008-02-28 02:34:43 +01:00
|
|
|
*
|
2012-07-02 19:50:03 +02:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-28 02:34:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2016-02-10 12:22:11 +01:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "driver.h"
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
#include <sys/ioctl.h>
|
2016-06-24 16:36:17 +02:00
|
|
|
#undef IFNAMSIZ
|
2008-02-28 02:34:43 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <netpacket/packet.h>
|
2009-04-09 12:40:12 +02:00
|
|
|
#include <net/if_arp.h>
|
2008-02-28 02:34:43 +01:00
|
|
|
#endif /* __linux__ */
|
2010-03-06 09:16:47 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
|
2008-02-28 02:34:43 +01:00
|
|
|
#include <net/if_dl.h>
|
2013-08-25 10:40:19 +02:00
|
|
|
#include <net/if_media.h>
|
2010-03-06 09:16:47 +01:00
|
|
|
#endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) */
|
2010-08-28 10:40:07 +02:00
|
|
|
#ifdef __sun__
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif /* __sun__ */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2009-11-23 15:14:39 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
struct ieee8023_hdr {
|
|
|
|
u8 dest[6];
|
|
|
|
u8 src[6];
|
|
|
|
u16 ethertype;
|
|
|
|
} STRUCT_PACKED;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(pop)
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static const u8 pae_group_addr[ETH_ALEN] =
|
|
|
|
{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
|
|
|
|
|
|
|
|
|
|
|
|
struct wpa_driver_wired_data {
|
2009-12-12 21:07:29 +01:00
|
|
|
char ifname[IFNAMSIZ + 1];
|
|
|
|
void *ctx;
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
int sock; /* raw packet socket for driver access */
|
|
|
|
int dhcp_sock; /* socket for dhcp packets */
|
|
|
|
int use_pae_group_addr;
|
2009-12-12 21:07:29 +01:00
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
int pf_sock;
|
|
|
|
int membership, multi, iff_allmulti, iff_up;
|
2009-04-09 12:40:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
2008-02-28 02:34:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-12-12 21:18:37 +01:00
|
|
|
static int wired_multicast_membership(int sock, int ifindex,
|
|
|
|
const u8 *addr, int add)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
struct packet_mreq mreq;
|
|
|
|
|
|
|
|
if (sock < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
os_memset(&mreq, 0, sizeof(mreq));
|
|
|
|
mreq.mr_ifindex = ifindex;
|
|
|
|
mreq.mr_type = PACKET_MR_MULTICAST;
|
|
|
|
mreq.mr_alen = ETH_ALEN;
|
|
|
|
os_memcpy(mreq.mr_address, addr, ETH_ALEN);
|
|
|
|
|
|
|
|
if (setsockopt(sock, SOL_PACKET,
|
|
|
|
add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
|
|
|
|
&mreq, sizeof(mreq)) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "setsockopt: %s", strerror(errno));
|
2009-12-12 21:18:37 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else /* __linux__ */
|
|
|
|
return -1;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-09 09:53:44 +01:00
|
|
|
#ifdef __linux__
|
2009-12-12 21:07:29 +01:00
|
|
|
static void handle_data(void *ctx, unsigned char *buf, size_t len)
|
2009-04-09 12:40:12 +02:00
|
|
|
{
|
2009-12-12 21:07:29 +01:00
|
|
|
#ifdef HOSTAPD
|
2009-04-09 12:40:12 +02:00
|
|
|
struct ieee8023_hdr *hdr;
|
|
|
|
u8 *pos, *sa;
|
|
|
|
size_t left;
|
2010-01-03 15:46:18 +01:00
|
|
|
union wpa_event_data event;
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
/* 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;
|
2010-01-03 15:46:18 +01:00
|
|
|
os_memset(&event, 0, sizeof(event));
|
|
|
|
event.new_sta.addr = sa;
|
|
|
|
wpa_supplicant_event(ctx, EVENT_NEW_STA, &event);
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
pos = (u8 *) (hdr + 1);
|
|
|
|
left = len - sizeof(*hdr);
|
2010-01-03 17:35:01 +01:00
|
|
|
drv_event_eapol_rx(ctx, sa, pos, left);
|
2009-04-09 12:40:12 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
|
|
|
|
ntohs(hdr->ethertype));
|
|
|
|
break;
|
|
|
|
}
|
2009-12-12 21:07:29 +01:00
|
|
|
#endif /* HOSTAPD */
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
unsigned char buf[3000];
|
|
|
|
|
|
|
|
len = recv(sock, buf, sizeof(buf), 0);
|
|
|
|
if (len < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-12 21:07:29 +01:00
|
|
|
handle_data(eloop_ctx, buf, len);
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
unsigned char buf[3000];
|
|
|
|
struct dhcp_message *msg;
|
|
|
|
u8 *mac_address;
|
2010-01-03 15:46:18 +01:00
|
|
|
union wpa_event_data event;
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
len = recv(sock, buf, sizeof(buf), 0);
|
|
|
|
if (len < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
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));
|
|
|
|
|
2010-01-03 15:46:18 +01:00
|
|
|
os_memset(&event, 0, sizeof(event));
|
|
|
|
event.new_sta.addr = mac_address;
|
|
|
|
wpa_supplicant_event(eloop_ctx, EVENT_NEW_STA, &event);
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
2010-01-09 09:53:44 +01:00
|
|
|
#endif /* __linux__ */
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
|
2009-04-17 10:55:51 +02:00
|
|
|
static int wired_init_sockets(struct wpa_driver_wired_data *drv, u8 *own_addr)
|
2009-04-09 12:40:12 +02:00
|
|
|
{
|
2010-01-09 09:53:44 +01:00
|
|
|
#ifdef __linux__
|
2009-04-09 12:40:12 +02:00
|
|
|
struct ifreq ifr;
|
|
|
|
struct sockaddr_ll addr;
|
|
|
|
struct sockaddr_in addr2;
|
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
|
|
|
|
if (drv->sock < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket[PF_PACKET,SOCK_RAW]: %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-12 21:07:29 +01:00
|
|
|
if (eloop_register_read_sock(drv->sock, handle_read, drv->ctx, NULL)) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not register read socket");
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
2009-12-12 21:07:29 +01:00
|
|
|
os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
|
2009-04-09 12:40:12 +02:00
|
|
|
if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memset(&addr, 0, sizeof(addr));
|
2009-04-09 12:40:12 +02:00
|
|
|
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) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* filter multicast address */
|
2009-12-12 21:18:37 +01:00
|
|
|
if (wired_multicast_membership(drv->sock, ifr.ifr_ifindex,
|
|
|
|
pae_group_addr, 1) < 0) {
|
|
|
|
wpa_printf(MSG_ERROR, "wired: Failed to add multicast group "
|
|
|
|
"membership");
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
2009-12-12 21:07:29 +01:00
|
|
|
os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
|
2009-04-09 12:40:12 +02:00
|
|
|
if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl(SIOCGIFHWADDR): %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_INFO, "Invalid HW-addr family 0x%04x",
|
|
|
|
ifr.ifr_hwaddr.sa_family);
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memcpy(own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
/* setup dhcp listen socket for sta detection */
|
|
|
|
if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket call failed for dhcp: %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-12 21:07:29 +01:00
|
|
|
if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, drv->ctx,
|
|
|
|
NULL)) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not register read socket");
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memset(&addr2, 0, sizeof(addr2));
|
2009-04-09 12:40:12 +02:00
|
|
|
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) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "setsockopt[SOL_SOCKET,SO_REUSEADDR]: %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
|
|
|
|
sizeof(n)) == -1) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "setsockopt[SOL_SOCKET,SO_BROADCAST]: %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
2009-12-12 21:07:29 +01:00
|
|
|
os_strlcpy(ifr.ifr_ifrn.ifrn_name, drv->ifname, IFNAMSIZ);
|
2009-04-09 12:40:12 +02:00
|
|
|
if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
(char *) &ifr, sizeof(ifr)) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"setsockopt[SOL_SOCKET,SO_BINDTODEVICE]: %s",
|
|
|
|
strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
|
|
|
|
sizeof(struct sockaddr)) == -1) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-01-09 09:53:44 +01:00
|
|
|
#else /* __linux__ */
|
|
|
|
return -1;
|
|
|
|
#endif /* __linux__ */
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wired_send_eapol(void *priv, const u8 *addr,
|
|
|
|
const u8 *data, size_t data_len, int encrypt,
|
2011-04-02 21:03:05 +02:00
|
|
|
const u8 *own_addr, u32 flags)
|
2009-04-09 12:40:12 +02:00
|
|
|
{
|
|
|
|
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) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_INFO,
|
|
|
|
"malloc() failed for wired_send_eapol(len=%lu)",
|
|
|
|
(unsigned long) len);
|
2009-04-09 12:40:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
|
|
|
|
ETH_ALEN);
|
|
|
|
os_memcpy(hdr->src, own_addr, ETH_ALEN);
|
2009-04-09 12:40:12 +02:00
|
|
|
hdr->ethertype = htons(ETH_P_PAE);
|
|
|
|
|
|
|
|
pos = (u8 *) (hdr + 1);
|
2009-12-25 10:54:02 +01:00
|
|
|
os_memcpy(pos, data, data_len);
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
res = send(drv->sock, (u8 *) hdr, len, 0);
|
2009-12-25 10:54:02 +01:00
|
|
|
os_free(hdr);
|
2009-04-09 12:40:12 +02:00
|
|
|
|
|
|
|
if (res < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"wired_send_eapol - packet len: %lu - failed: send: %s",
|
|
|
|
(unsigned long) len, strerror(errno));
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-09 22:28:21 +02:00
|
|
|
static void * wired_driver_hapd_init(struct hostapd_data *hapd,
|
|
|
|
struct wpa_init_params *params)
|
2009-04-09 12:40:12 +02:00
|
|
|
{
|
|
|
|
struct wpa_driver_wired_data *drv;
|
|
|
|
|
|
|
|
drv = os_zalloc(sizeof(struct wpa_driver_wired_data));
|
|
|
|
if (drv == NULL) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_INFO,
|
|
|
|
"Could not allocate memory for wired driver data");
|
2009-04-09 12:40:12 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-12 21:07:29 +01:00
|
|
|
drv->ctx = hapd;
|
|
|
|
os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
|
2009-04-09 22:28:21 +02:00
|
|
|
drv->use_pae_group_addr = params->use_pae_group_addr;
|
2009-04-09 12:40:12 +02:00
|
|
|
|
2009-04-17 10:55:51 +02:00
|
|
|
if (wired_init_sockets(drv, params->own_addr)) {
|
2009-12-25 10:54:02 +01:00
|
|
|
os_free(drv);
|
2009-04-09 12:40:12 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wired_driver_hapd_deinit(void *priv)
|
|
|
|
{
|
|
|
|
struct wpa_driver_wired_data *drv = priv;
|
|
|
|
|
2014-11-16 23:57:15 +01:00
|
|
|
if (drv->sock >= 0) {
|
|
|
|
eloop_unregister_read_sock(drv->sock);
|
2009-04-09 12:40:12 +02:00
|
|
|
close(drv->sock);
|
2014-11-16 23:57:15 +01:00
|
|
|
}
|
2009-04-09 12:40:12 +02:00
|
|
|
|
2014-11-16 23:57:15 +01:00
|
|
|
if (drv->dhcp_sock >= 0) {
|
|
|
|
eloop_unregister_read_sock(drv->dhcp_sock);
|
2009-04-09 12:40:12 +02:00
|
|
|
close(drv->dhcp_sock);
|
2014-11-16 23:57:15 +01:00
|
|
|
}
|
2009-04-09 12:40:12 +02:00
|
|
|
|
2009-12-25 10:54:02 +01:00
|
|
|
os_free(drv);
|
2009-04-09 12:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static int wpa_driver_wired_get_ssid(void *priv, u8 *ssid)
|
|
|
|
{
|
|
|
|
ssid[0] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpa_driver_wired_get_bssid(void *priv, u8 *bssid)
|
|
|
|
{
|
|
|
|
/* Report PAE group address as the "BSSID" for wired connection. */
|
|
|
|
os_memcpy(bssid, pae_group_addr, ETH_ALEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-07 21:05:43 +01:00
|
|
|
static int wpa_driver_wired_get_capa(void *priv, struct wpa_driver_capa *capa)
|
|
|
|
{
|
|
|
|
os_memset(capa, 0, sizeof(*capa));
|
|
|
|
capa->flags = WPA_DRIVER_FLAGS_WIRED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static int wpa_driver_wired_get_ifflags(const char *ifname, int *flags)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
|
|
|
os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]: %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
*flags = ifr.ifr_flags & 0xffff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int wpa_driver_wired_set_ifflags(const char *ifname, int flags)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
|
|
|
os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
|
|
|
ifr.ifr_flags = flags & 0xffff;
|
|
|
|
if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-25 10:40:19 +02:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
|
|
|
|
static int wpa_driver_wired_get_ifstatus(const char *ifname, int *status)
|
|
|
|
{
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
|
2013-08-25 10:40:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
os_strlcpy(ifmr.ifm_name, ifname, IFNAMSIZ);
|
|
|
|
if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl[SIOCGIFMEDIA]: %s",
|
|
|
|
strerror(errno));
|
2013-08-25 10:40:19 +02:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
*status = (ifmr.ifm_status & (IFM_ACTIVE | IFM_AVALID)) ==
|
|
|
|
(IFM_ACTIVE | IFM_AVALID);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
|
|
|
|
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
static int wpa_driver_wired_multi(const char *ifname, const u8 *addr, int add)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
2010-08-28 10:40:07 +02:00
|
|
|
#ifdef __sun__
|
|
|
|
return -1;
|
|
|
|
#endif /* __sun__ */
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&ifr, 0, sizeof(ifr));
|
|
|
|
os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
|
|
|
|
#ifdef __linux__
|
|
|
|
ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
|
|
|
|
os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
|
|
|
|
#endif /* __linux__ */
|
2010-03-06 09:16:47 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_dl *dlp;
|
|
|
|
dlp = (struct sockaddr_dl *) &ifr.ifr_addr;
|
|
|
|
dlp->sdl_len = sizeof(struct sockaddr_dl);
|
|
|
|
dlp->sdl_family = AF_LINK;
|
|
|
|
dlp->sdl_index = 0;
|
|
|
|
dlp->sdl_nlen = 0;
|
|
|
|
dlp->sdl_alen = ETH_ALEN;
|
|
|
|
dlp->sdl_slen = 0;
|
2009-04-09 12:40:12 +02:00
|
|
|
os_memcpy(LLADDR(dlp), addr, ETH_ALEN);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
2010-03-06 09:16:47 +01:00
|
|
|
#endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
|
2009-09-26 18:29:03 +02:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
|
2009-08-15 19:59:16 +02:00
|
|
|
{
|
|
|
|
struct sockaddr *sap;
|
|
|
|
sap = (struct sockaddr *) &ifr.ifr_addr;
|
|
|
|
sap->sa_len = sizeof(struct sockaddr);
|
|
|
|
sap->sa_family = AF_UNSPEC;
|
|
|
|
os_memcpy(sap->sa_data, addr, ETH_ALEN);
|
|
|
|
}
|
2009-09-26 18:29:03 +02:00
|
|
|
#endif /* defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (ioctl(s, add ? SIOCADDMULTI : SIOCDELMULTI, (caddr_t) &ifr) < 0) {
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "ioctl[SIOC{ADD/DEL}MULTI]: %s",
|
|
|
|
strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void * wpa_driver_wired_init(void *ctx, const char *ifname)
|
|
|
|
{
|
|
|
|
struct wpa_driver_wired_data *drv;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
drv = os_zalloc(sizeof(*drv));
|
|
|
|
if (drv == NULL)
|
|
|
|
return NULL;
|
|
|
|
os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
|
|
|
|
drv->ctx = ctx;
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
drv->pf_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
|
|
|
|
if (drv->pf_sock < 0)
|
2014-12-24 11:43:35 +01:00
|
|
|
wpa_printf(MSG_ERROR, "socket(PF_PACKET): %s", strerror(errno));
|
2008-02-28 02:34:43 +01:00
|
|
|
#else /* __linux__ */
|
2009-04-09 12:40:12 +02:00
|
|
|
drv->pf_sock = -1;
|
2008-02-28 02:34:43 +01:00
|
|
|
#endif /* __linux__ */
|
2009-04-09 12:40:12 +02:00
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
if (wpa_driver_wired_get_ifflags(ifname, &flags) == 0 &&
|
|
|
|
!(flags & IFF_UP) &&
|
|
|
|
wpa_driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0) {
|
|
|
|
drv->iff_up = 1;
|
|
|
|
}
|
|
|
|
|
2009-12-12 21:18:37 +01:00
|
|
|
if (wired_multicast_membership(drv->pf_sock,
|
|
|
|
if_nametoindex(drv->ifname),
|
|
|
|
pae_group_addr, 1) == 0) {
|
2008-02-28 02:34:43 +01:00
|
|
|
wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
|
|
|
|
"packet socket", __func__);
|
|
|
|
drv->membership = 1;
|
|
|
|
} else if (wpa_driver_wired_multi(ifname, pae_group_addr, 1) == 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
|
|
|
|
"SIOCADDMULTI", __func__);
|
|
|
|
drv->multi = 1;
|
|
|
|
} else if (wpa_driver_wired_get_ifflags(ifname, &flags) < 0) {
|
|
|
|
wpa_printf(MSG_INFO, "%s: Could not get interface "
|
|
|
|
"flags", __func__);
|
|
|
|
os_free(drv);
|
|
|
|
return NULL;
|
|
|
|
} else if (flags & IFF_ALLMULTI) {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Interface is already configured "
|
|
|
|
"for multicast", __func__);
|
|
|
|
} else if (wpa_driver_wired_set_ifflags(ifname,
|
|
|
|
flags | IFF_ALLMULTI) < 0) {
|
|
|
|
wpa_printf(MSG_INFO, "%s: Failed to enable allmulti",
|
|
|
|
__func__);
|
|
|
|
os_free(drv);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Enabled allmulti mode",
|
|
|
|
__func__);
|
|
|
|
drv->iff_allmulti = 1;
|
|
|
|
}
|
2013-08-25 10:40:19 +02:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: waiting for link to become active",
|
|
|
|
__func__);
|
|
|
|
while (wpa_driver_wired_get_ifstatus(ifname, &status) == 0 &&
|
|
|
|
status == 0)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
#endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
return drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void wpa_driver_wired_deinit(void *priv)
|
|
|
|
{
|
|
|
|
struct wpa_driver_wired_data *drv = priv;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (drv->membership &&
|
2009-12-12 21:18:37 +01:00
|
|
|
wired_multicast_membership(drv->pf_sock,
|
|
|
|
if_nametoindex(drv->ifname),
|
|
|
|
pae_group_addr, 0) < 0) {
|
2008-02-28 02:34:43 +01:00
|
|
|
wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
|
|
|
|
"group (PACKET)", __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drv->multi &&
|
|
|
|
wpa_driver_wired_multi(drv->ifname, pae_group_addr, 0) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
|
|
|
|
"group (SIOCDELMULTI)", __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drv->iff_allmulti &&
|
|
|
|
(wpa_driver_wired_get_ifflags(drv->ifname, &flags) < 0 ||
|
|
|
|
wpa_driver_wired_set_ifflags(drv->ifname,
|
|
|
|
flags & ~IFF_ALLMULTI) < 0)) {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Failed to disable allmulti mode",
|
|
|
|
__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drv->iff_up &&
|
|
|
|
wpa_driver_wired_get_ifflags(drv->ifname, &flags) == 0 &&
|
|
|
|
(flags & IFF_UP) &&
|
|
|
|
wpa_driver_wired_set_ifflags(drv->ifname, flags & ~IFF_UP) < 0) {
|
|
|
|
wpa_printf(MSG_DEBUG, "%s: Failed to set the interface down",
|
|
|
|
__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drv->pf_sock != -1)
|
|
|
|
close(drv->pf_sock);
|
2009-04-09 12:40:12 +02:00
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
os_free(drv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const struct wpa_driver_ops wpa_driver_wired_ops = {
|
|
|
|
.name = "wired",
|
2009-04-09 12:40:12 +02:00
|
|
|
.desc = "Wired Ethernet driver",
|
|
|
|
.hapd_init = wired_driver_hapd_init,
|
|
|
|
.hapd_deinit = wired_driver_hapd_deinit,
|
|
|
|
.hapd_send_eapol = wired_send_eapol,
|
2008-02-28 02:34:43 +01:00
|
|
|
.get_ssid = wpa_driver_wired_get_ssid,
|
|
|
|
.get_bssid = wpa_driver_wired_get_bssid,
|
2009-03-07 21:05:43 +01:00
|
|
|
.get_capa = wpa_driver_wired_get_capa,
|
2008-02-28 02:34:43 +01:00
|
|
|
.init = wpa_driver_wired_init,
|
|
|
|
.deinit = wpa_driver_wired_deinit,
|
|
|
|
};
|