aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJouni Malinen <j@w1.fi>2005-06-18 20:09:44 (GMT)
committerJouni Malinen <j@w1.fi>2005-06-18 20:09:44 (GMT)
commite27f2c4e41dcae84f4312557e5d7e149810077bc (patch)
treeca7ce63895948e35e74d80604f2f7932e994b299
parentd73d3dc22092da421ffa29bdb22fb737fb07d69a (diff)
downloadhostap-history-e27f2c4e41dcae84f4312557e5d7e149810077bc.zip
hostap-history-e27f2c4e41dcae84f4312557e5d7e149810077bc.tar.gz
hostap-history-e27f2c4e41dcae84f4312557e5d7e149810077bc.tar.bz2
Merge FreeBSD version of l2_packet implementation from
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/wpa/l2_packet.c into wpa_supplicant tree as l2_packet_freebsd.c. This has some modifications to make the diff between l2_packet_pcap.c and l2_packet_freebsd.c minimal.
-rw-r--r--hostapd/Makefile8
-rw-r--r--wpa_supplicant/Makefile11
-rw-r--r--wpa_supplicant/l2_packet_freebsd.c275
-rw-r--r--wpa_supplicant/l2_packet_pcap.c21
4 files changed, 300 insertions, 15 deletions
diff --git a/hostapd/Makefile b/hostapd/Makefile
index 9a891ca..83e7433 100644
--- a/hostapd/Makefile
+++ b/hostapd/Makefile
@@ -60,6 +60,7 @@ CFLAGS += -DCONFIG_DRIVER_BSD
OBJS += driver_bsd.o
CONFIG_L2_PACKET=y
CONFIG_DNET_PCAP=y
+CONFIG_L2_FREEBSD=y
endif
ifdef CONFIG_DRIVER_TEST
@@ -70,8 +71,13 @@ endif
ifdef CONFIG_L2_PACKET
ifdef CONFIG_DNET_PCAP
CFLAGS += -DUSE_DNET_PCAP
-LIBS +=-ldnet -lpcap
+ifdef CONFIG_L2_FREEBSD
+LIBS += -lpcap
+OBJS += $(DIR_WPA_SUPPLICANT)/l2_packet_freebsd.o
+else
+LIBS += -ldnet -lpcap
OBJS += $(DIR_WPA_SUPPLICANT)/l2_packet_pcap.o
+endif
else
OBJS += $(DIR_WPA_SUPPLICANT)/l2_packet_linux.o
endif
diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile
index d6dcf40..e261c0b 100644
--- a/wpa_supplicant/Makefile
+++ b/wpa_supplicant/Makefile
@@ -103,6 +103,7 @@ ifdef CONFIG_DRIVER_BSD
CFLAGS += -DCONFIG_DRIVER_BSD
OBJS_d += driver_bsd.o
CONFIG_DNET_PCAP=y
+CONFIG_L2_FREEBSD=y
endif
ifdef CONFIG_DRIVER_NDIS
@@ -125,13 +126,19 @@ endif
ifdef CONFIG_DNET_PCAP
CFLAGS += -DUSE_DNET_PCAP
ifdef CONFIG_WINPCAP
+OBJS += l2_packet_pcap.o
CFLAGS += -DCONFIG_WINPCAP
LIBS += -lwpcap -lpacket
LIBS_w += -lwpcap
else
+ifdef CONFIG_L2_FREEBSD
+OBJS += l2_packet_freebsd.o
+LIBS += -lpcap
+else
+OBJS += l2_packet_pcap.o
LIBS += -ldnet -lpcap
endif
-OBJS += l2_packet_pcap.o
+endif
else
OBJS += l2_packet_linux.o
endif
@@ -274,9 +281,11 @@ OBJS += eap_tls_common.o tls_openssl.o
LIBS += -lssl -lcrypto
ifdef CONFIG_SMARTCARD
ifndef CONFIG_NATIVE_WINDOWS
+ifndef CONFIG_L2_FREEBSD
LIBS += -ldl
endif
endif
+endif
LIBS_p += -lcrypto
else
OBJS += tls_none.o
diff --git a/wpa_supplicant/l2_packet_freebsd.c b/wpa_supplicant/l2_packet_freebsd.c
new file mode 100644
index 0000000..114ad9d
--- /dev/null
+++ b/wpa_supplicant/l2_packet_freebsd.c
@@ -0,0 +1,275 @@
+/*
+ * WPA Supplicant - Layer2 packet handling with FreeBSD
+ * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi>
+ * Copyright (c) 2005, Sam Leffler <sam@errno.com>
+ *
+ * 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
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <pcap.h>
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/sysctl.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "common.h"
+#include "eloop.h"
+#include "l2_packet.h"
+
+
+struct l2_packet_data {
+ pcap_t *pcap;
+ char ifname[100];
+ u8 own_addr[ETH_ALEN];
+ void (*rx_callback)(void *ctx, unsigned char *src_addr,
+ unsigned char *buf, size_t len);
+ void *rx_callback_ctx;
+ int rx_l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
+ * to rx_callback */
+};
+
+
+int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
+{
+ memcpy(addr, l2->own_addr, ETH_ALEN);
+ return 0;
+}
+
+
+void l2_packet_set_rx_l2_hdr(struct l2_packet_data *l2, int rx_l2_hdr)
+{
+ l2->rx_l2_hdr = rx_l2_hdr;
+}
+
+
+int l2_packet_send(struct l2_packet_data *l2, u8 *buf, size_t len)
+{
+ return pcap_inject(l2->pcap, buf, len);
+}
+
+
+static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
+{
+ struct l2_packet_data *l2 = eloop_ctx;
+ pcap_t *pcap = sock_ctx;
+ struct pcap_pkthdr hdr;
+ const u_char *packet;
+ struct l2_ethhdr *ethhdr;
+ unsigned char *buf;
+ size_t len;
+
+ packet = pcap_next(pcap, &hdr);
+
+ if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
+ return;
+
+ ethhdr = (struct l2_ethhdr *) packet;
+ if (l2->rx_l2_hdr) {
+ buf = (unsigned char *) ethhdr;
+ len = hdr.caplen;
+ } else {
+ buf = (unsigned char *) (ethhdr + 1);
+ len = hdr.caplen - sizeof(*ethhdr);
+ }
+ l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
+}
+
+
+static int l2_packet_init_libpcap(struct l2_packet_data *l2,
+ unsigned short protocol)
+{
+ bpf_u_int32 pcap_maskp, pcap_netp;
+ char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
+ struct bpf_program pcap_fp;
+
+ pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
+ l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
+ if (l2->pcap == NULL) {
+ fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
+ fprintf(stderr, "ifname='%s'\n", l2->ifname);
+ return -1;
+ }
+ if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
+ pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
+ fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
+ pcap_geterr(l2->pcap));
+ return -1;
+ }
+ snprintf(pcap_filter, sizeof(pcap_filter),
+ "ether dst " MACSTR " and ether proto 0x%x",
+ MAC2STR(l2->own_addr), protocol);
+ if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
+ fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
+ return -1;
+ }
+
+ if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
+ fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
+ return -1;
+ }
+
+ pcap_freecode(&pcap_fp);
+ /*
+ * When libpcap uses BPF we must enable "immediate mode" to
+ * receive frames right away; otherwise the system may
+ * buffer them for us.
+ */
+ {
+ unsigned int on = 1;
+ if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
+ fprintf(stderr, "%s: cannot enable immediate mode on "
+ "interface %s: %s\n",
+ __func__, l2->ifname, strerror(errno));
+ /* XXX should we fail? */
+ }
+ }
+
+ eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
+ l2_packet_receive, l2, l2->pcap);
+
+ return 0;
+}
+
+
+static int eth_get(const char *device, u8 ea[ETH_ALEN])
+{
+ struct if_msghdr *ifm;
+ struct sockaddr_dl *sdl;
+ u_char *p, *buf;
+ size_t len;
+ int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
+
+ if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
+ return -1;
+ if ((buf = malloc(len)) == NULL)
+ return -1;
+ if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
+ free(buf);
+ return -1;
+ }
+ for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
+ ifm = (struct if_msghdr *)p;
+ sdl = (struct sockaddr_dl *)(ifm + 1);
+ if (ifm->ifm_type != RTM_IFINFO ||
+ (ifm->ifm_addrs & RTA_IFP) == 0)
+ continue;
+ if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
+ memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
+ continue;
+ memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
+ break;
+ }
+ free(buf);
+
+ if (p >= buf + len) {
+ errno = ESRCH;
+ return -1;
+ }
+ return 0;
+}
+
+
+struct l2_packet_data * l2_packet_init(
+ const char *ifname, const u8 *own_addr, unsigned short protocol,
+ void (*rx_callback)(void *ctx, unsigned char *src_addr,
+ unsigned char *buf, size_t len),
+ void *rx_callback_ctx)
+{
+ struct l2_packet_data *l2;
+
+ l2 = malloc(sizeof(struct l2_packet_data));
+ if (l2 == NULL)
+ return NULL;
+ memset(l2, 0, sizeof(*l2));
+ strncpy(l2->ifname, ifname, sizeof(l2->ifname));
+ l2->rx_callback = rx_callback;
+ l2->rx_callback_ctx = rx_callback_ctx;
+
+ if (eth_get(l2->ifname, l2->own_addr) < 0) {
+ fprintf(stderr, "Failed to get link-level address for "
+ "interface '%s'.\n", l2->ifname);
+ free(l2);
+ return NULL;
+ }
+
+ if (l2_packet_init_libpcap(l2, protocol)) {
+ free(l2);
+ return NULL;
+ }
+
+ return l2;
+}
+
+
+void l2_packet_deinit(struct l2_packet_data *l2)
+{
+ if (l2 != NULL) {
+ if (l2->pcap)
+ pcap_close(l2->pcap);
+ free(l2);
+ }
+}
+
+
+void * l2_packet_set_ethhdr(struct l2_ethhdr *ethhdr, const u8 *dest,
+ const u8 *source, u16 proto)
+{
+ memcpy(ethhdr->h_dest, dest, ETH_ALEN);
+ memcpy(ethhdr->h_source, source, ETH_ALEN);
+ ethhdr->h_proto = htons(proto);
+ return (void *) (ethhdr + 1);
+}
+
+
+int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
+{
+ pcap_if_t *devs, *dev;
+ struct pcap_addr *addr;
+ struct sockaddr_in *saddr;
+ int found = 0;
+ char err[PCAP_ERRBUF_SIZE + 1];
+
+ if (pcap_findalldevs(&devs, err) < 0) {
+ wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
+ return -1;
+ }
+
+ for (dev = devs; dev && !found; dev = dev->next) {
+ if (strcmp(dev->name, l2->ifname) != 0)
+ continue;
+
+ addr = dev->addresses;
+ while (addr) {
+ saddr = (struct sockaddr_in *) addr->addr;
+ if (saddr && saddr->sin_family == AF_INET) {
+ snprintf(buf, len, "%s",
+ inet_ntoa(saddr->sin_addr));
+ found = 1;
+ break;
+ }
+ addr = addr->next;
+ }
+ }
+
+ pcap_freealldevs(devs);
+
+ return found ? 0 : -1;
+}
diff --git a/wpa_supplicant/l2_packet_pcap.c b/wpa_supplicant/l2_packet_pcap.c
index 6287e79..a42b853 100644
--- a/wpa_supplicant/l2_packet_pcap.c
+++ b/wpa_supplicant/l2_packet_pcap.c
@@ -160,27 +160,23 @@ static int l2_packet_init_libpcap(struct l2_packet_data *l2,
return -1;
}
#ifndef CONFIG_WINPCAP
- if (pcap_datalink(l2->pcap) != DLT_EN10MB) {
- if (pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
- fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
- pcap_geterr(l2->pcap));
- return -1;
- }
+ if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
+ pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
+ fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
+ pcap_geterr(l2->pcap));
+ return -1;
}
#endif /* CONFIG_WINPCAP */
snprintf(pcap_filter, sizeof(pcap_filter),
"ether dst " MACSTR " and ether proto 0x%x",
MAC2STR(l2->own_addr), protocol);
- if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0)
- {
- fprintf(stderr, "pcap_compile: %s\n",
- pcap_geterr(l2->pcap));
+ if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
+ fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
return -1;
}
if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
- fprintf(stderr, "pcap_setfilter: %s\n",
- pcap_geterr(l2->pcap));
+ fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
return -1;
}
@@ -214,7 +210,6 @@ static int l2_packet_init_libpcap(struct l2_packet_data *l2,
}
-
struct l2_packet_data * l2_packet_init(
const char *ifname, const u8 *own_addr, unsigned short protocol,
void (*rx_callback)(void *ctx, unsigned char *src_addr,