2 * WPA Supplicant - Layer2 packet handling with libpcap/libdnet and WinPcap
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #ifndef CONFIG_NATIVE_WINDOWS
11 #include <sys/ioctl.h>
12 #endif /* CONFIG_NATIVE_WINDOWS */
14 #ifndef CONFIG_WINPCAP
16 #endif /* CONFIG_WINPCAP */
20 #include "l2_packet.h"
23 static const u8 pae_group_addr[ETH_ALEN] =
24 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
26 struct l2_packet_data {
29 unsigned int num_fast_poll;
30 #else /* CONFIG_WINPCAP */
32 #endif /* CONFIG_WINPCAP */
34 u8 own_addr[ETH_ALEN];
35 void (*rx_callback)(void *ctx, const u8 *src_addr,
36 const u8 *buf, size_t len);
37 void *rx_callback_ctx;
38 int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
43 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
45 os_memcpy(addr, l2->own_addr, ETH_ALEN);
50 #ifndef CONFIG_WINPCAP
51 static int l2_packet_init_libdnet(struct l2_packet_data *l2)
55 l2->eth = eth_open(l2->ifname);
57 printf("Failed to open interface '%s'.\n", l2->ifname);
62 if (eth_get(l2->eth, &own_addr) < 0) {
63 printf("Failed to get own hw address from interface '%s'.\n",
70 os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
74 #endif /* CONFIG_WINPCAP */
77 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
78 const u8 *buf, size_t len)
81 struct l2_ethhdr *eth;
88 ret = pcap_sendpacket(l2->pcap, buf, len);
89 #else /* CONFIG_WINPCAP */
90 ret = eth_send(l2->eth, buf, len);
91 #endif /* CONFIG_WINPCAP */
93 size_t mlen = sizeof(*eth) + len;
94 eth = os_malloc(mlen);
98 os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
99 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
100 eth->h_proto = htons(proto);
101 os_memcpy(eth + 1, buf, len);
103 #ifdef CONFIG_WINPCAP
104 ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
105 #else /* CONFIG_WINPCAP */
106 ret = eth_send(l2->eth, (u8 *) eth, mlen);
107 #endif /* CONFIG_WINPCAP */
116 #ifndef CONFIG_WINPCAP
117 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
119 struct l2_packet_data *l2 = eloop_ctx;
120 pcap_t *pcap = sock_ctx;
121 struct pcap_pkthdr hdr;
122 const u_char *packet;
123 struct l2_ethhdr *ethhdr;
127 packet = pcap_next(pcap, &hdr);
129 if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
132 ethhdr = (struct l2_ethhdr *) packet;
134 buf = (unsigned char *) ethhdr;
137 buf = (unsigned char *) (ethhdr + 1);
138 len = hdr.caplen - sizeof(*ethhdr);
140 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
142 #endif /* CONFIG_WINPCAP */
145 #ifdef CONFIG_WINPCAP
146 static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
147 const u_char *pkt_data)
149 struct l2_packet_data *l2 = (struct l2_packet_data *) user;
150 struct l2_ethhdr *ethhdr;
154 if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
157 ethhdr = (struct l2_ethhdr *) pkt_data;
159 buf = (unsigned char *) ethhdr;
162 buf = (unsigned char *) (ethhdr + 1);
163 len = hdr->caplen - sizeof(*ethhdr);
165 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
167 * Use shorter poll interval for 3 seconds to reduce latency during key
170 l2->num_fast_poll = 3 * 50;
174 static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
176 struct l2_packet_data *l2 = eloop_ctx;
177 pcap_t *pcap = timeout_ctx;
180 if (l2->num_fast_poll > 0) {
186 /* Register new timeout before calling l2_packet_receive() since
187 * receive handler may free this l2_packet instance (which will
188 * cancel this timeout). */
189 eloop_register_timeout(0, timeout, l2_packet_receive_timeout,
191 pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
193 #endif /* CONFIG_WINPCAP */
196 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
197 unsigned short protocol)
199 bpf_u_int32 pcap_maskp, pcap_netp;
200 char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
201 struct bpf_program pcap_fp;
203 #ifdef CONFIG_WINPCAP
205 os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", l2->ifname);
206 pcap_lookupnet(ifname, &pcap_netp, &pcap_maskp, pcap_err);
207 l2->pcap = pcap_open_live(ifname, 2500, 0, 10, pcap_err);
208 if (l2->pcap == NULL) {
209 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
210 fprintf(stderr, "ifname='%s'\n", ifname);
213 if (pcap_setnonblock(l2->pcap, 1, pcap_err) < 0)
214 fprintf(stderr, "pcap_setnonblock: %s\n",
215 pcap_geterr(l2->pcap));
216 #else /* CONFIG_WINPCAP */
217 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
218 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
219 if (l2->pcap == NULL) {
220 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
221 fprintf(stderr, "ifname='%s'\n", l2->ifname);
224 if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
225 pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
226 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
227 pcap_geterr(l2->pcap));
230 #endif /* CONFIG_WINPCAP */
231 os_snprintf(pcap_filter, sizeof(pcap_filter),
232 "not ether src " MACSTR " and "
233 "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
235 MAC2STR(l2->own_addr), /* do not receive own packets */
236 MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
238 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
239 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
243 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
244 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
248 pcap_freecode(&pcap_fp);
251 * When libpcap uses BPF we must enable "immediate mode" to
252 * receive frames right away; otherwise the system may
253 * buffer them for us.
257 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
258 fprintf(stderr, "%s: cannot enable immediate mode on "
259 "interface %s: %s\n",
260 __func__, l2->ifname, strerror(errno));
261 /* XXX should we fail? */
264 #endif /* BIOCIMMEDIATE */
266 #ifdef CONFIG_WINPCAP
267 eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
269 #else /* CONFIG_WINPCAP */
270 eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
271 l2_packet_receive, l2, l2->pcap);
272 #endif /* CONFIG_WINPCAP */
278 struct l2_packet_data * l2_packet_init(
279 const char *ifname, const u8 *own_addr, unsigned short protocol,
280 void (*rx_callback)(void *ctx, const u8 *src_addr,
281 const u8 *buf, size_t len),
282 void *rx_callback_ctx, int l2_hdr)
284 struct l2_packet_data *l2;
286 l2 = os_zalloc(sizeof(struct l2_packet_data));
289 os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
290 l2->rx_callback = rx_callback;
291 l2->rx_callback_ctx = rx_callback_ctx;
294 #ifdef CONFIG_WINPCAP
296 os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
297 #else /* CONFIG_WINPCAP */
298 if (l2_packet_init_libdnet(l2))
300 #endif /* CONFIG_WINPCAP */
302 if (l2_packet_init_libpcap(l2, protocol)) {
303 #ifndef CONFIG_WINPCAP
305 #endif /* CONFIG_WINPCAP */
314 void l2_packet_deinit(struct l2_packet_data *l2)
319 #ifdef CONFIG_WINPCAP
320 eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
321 #else /* CONFIG_WINPCAP */
324 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
325 #endif /* CONFIG_WINPCAP */
327 pcap_close(l2->pcap);
332 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
334 pcap_if_t *devs, *dev;
335 struct pcap_addr *addr;
336 struct sockaddr_in *saddr;
338 char err[PCAP_ERRBUF_SIZE + 1];
340 if (pcap_findalldevs(&devs, err) < 0) {
341 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
345 for (dev = devs; dev && !found; dev = dev->next) {
346 if (os_strcmp(dev->name, l2->ifname) != 0)
349 addr = dev->addresses;
351 saddr = (struct sockaddr_in *) addr->addr;
352 if (saddr && saddr->sin_family == AF_INET) {
353 os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
362 pcap_freealldevs(devs);
364 return found ? 0 : -1;
368 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
370 #ifdef CONFIG_WINPCAP
372 * Use shorter poll interval for 3 seconds to reduce latency during key
375 l2->num_fast_poll = 3 * 50;
376 eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
377 eloop_register_timeout(0, 10000, l2_packet_receive_timeout,
379 #endif /* CONFIG_WINPCAP */