2 * WPA Supplicant - Layer2 packet handling
3 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright (c) 2005, Sam Leffler <sam@errno.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
15 * $FreeBSD: src/usr.sbin/wpa/l2_packet.c,v 1.1.2.2 2006/03/24 01:43:17 sam Exp $
16 * $DragonFly: src/usr.sbin/802_11/l2_packet.c,v 1.3 2007/01/01 01:45:40 sephe Exp $
20 * FreeBSD-specific implementation.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/sysctl.h>
29 #include <net/if_dl.h>
30 #include <net/route.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
43 #include "l2_packet.h"
45 struct l2_packet_data {
48 u8 own_addr[ETH_ALEN];
49 void (*rx_callback)(void *ctx, const u8 *src_addr,
50 const u8 *buf, size_t len);
51 void *rx_callback_ctx;
52 int l2_hdr; /* whether to include layer 2 (Ethernet) header data
57 l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
59 memcpy(addr, l2->own_addr, ETH_ALEN);
64 l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
66 pcap_if_t *devs, *dev;
67 struct pcap_addr *addr;
68 struct sockaddr_in *saddr;
70 char err[PCAP_ERRBUF_SIZE + 1];
72 if (pcap_findalldevs(&devs, err) < 0) {
73 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
77 for (dev = devs; dev && !found; dev = dev->next) {
78 if (strcmp(dev->name, l2->ifname) != 0)
81 addr = dev->addresses;
83 saddr = (struct sockaddr_in *) addr->addr;
84 if (saddr && saddr->sin_family == AF_INET) {
85 snprintf(buf, len, "%s",
86 inet_ntoa(saddr->sin_addr));
94 pcap_freealldevs(devs);
96 return found ? 0 : -1;
100 l2_packet_notify_auth_start(struct l2_packet_data *l2)
105 l2_packet_send(struct l2_packet_data *l2,
106 const u8 *dst_addr, u16 proto, const u8 *buf, size_t len)
110 struct l2_ethhdr *eth = malloc(sizeof(*eth) + len);
113 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
114 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
115 eth->h_proto = htons(proto);
116 memcpy(eth + 1, buf, len);
117 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
121 return pcap_inject(l2->pcap, buf, len);
126 l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
128 struct l2_packet_data *l2 = eloop_ctx;
129 pcap_t *pcap = sock_ctx;
130 struct pcap_pkthdr hdr;
131 const u_char *packet;
132 struct l2_ethhdr *ethhdr;
136 packet = pcap_next(pcap, &hdr);
138 if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
141 ethhdr = (struct l2_ethhdr *) packet;
143 buf = (unsigned char *) ethhdr;
146 buf = (unsigned char *) (ethhdr + 1);
147 len = hdr.caplen - sizeof(*ethhdr);
149 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
153 l2_packet_init_libpcap(struct l2_packet_data *l2, unsigned short protocol)
155 bpf_u_int32 pcap_maskp, pcap_netp;
156 char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
157 struct bpf_program pcap_fp;
159 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
160 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
161 if (l2->pcap == NULL) {
162 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
163 fprintf(stderr, "ifname='%s'\n", l2->ifname);
166 if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
167 pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
168 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
169 pcap_geterr(l2->pcap));
172 snprintf(pcap_filter, sizeof(pcap_filter),
173 "ether dst " MACSTR " and ether proto 0x%x",
174 MAC2STR(l2->own_addr), protocol);
175 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
176 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
180 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
181 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
185 pcap_freecode(&pcap_fp);
187 * When libpcap uses BPF we must enable "immediate mode" to
188 * receive frames right away; otherwise the system may
189 * buffer them for us.
191 { unsigned int on = 1;
192 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
193 fprintf(stderr, "%s: cannot enable immediate mode on "
194 "interface %s: %s\n",
195 __func__, l2->ifname, strerror(errno));
196 /* XXX should we fail? */
200 eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
201 l2_packet_receive, l2, l2->pcap);
207 l2_packet_deinit_libpcap(struct l2_packet_data *l2)
209 if (l2->pcap != NULL) {
210 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
211 pcap_close(l2->pcap);
217 eth_get(const char *device, u8 ea[ETH_ALEN])
219 struct if_msghdr *ifm;
220 struct sockaddr_dl *sdl;
223 int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
225 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
227 if ((buf = malloc(len)) == NULL)
229 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
233 for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
234 ifm = (struct if_msghdr *)p;
235 sdl = (struct sockaddr_dl *)(ifm + 1);
236 if (ifm->ifm_type != RTM_IFINFO ||
237 (ifm->ifm_addrs & RTA_IFP) == 0)
239 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
240 memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
242 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
247 if (p >= buf + len) {
254 struct l2_packet_data *
255 l2_packet_init(const char *ifname, const u8 *own_addr, unsigned short protocol,
256 void (*rx_callback)(void *ctx, const u8 *src_addr,
257 const u8 *buf, size_t len),
258 void *rx_callback_ctx, int l2_hdr)
260 struct l2_packet_data *l2;
262 l2 = malloc(sizeof(struct l2_packet_data));
265 memset(l2, 0, sizeof(*l2));
266 strncpy(l2->ifname, ifname, sizeof(l2->ifname));
267 l2->rx_callback = rx_callback;
268 l2->rx_callback_ctx = rx_callback_ctx;
271 if (eth_get(l2->ifname, l2->own_addr) < 0) {
272 fprintf(stderr, "Failed to get link-level address for "
273 "interface '%s'.\n", l2->ifname);
278 if (l2_packet_init_libpcap(l2, protocol) != 0) {
286 l2_packet_deinit(struct l2_packet_data *l2)
289 l2_packet_deinit_libpcap(l2);