1 /*
2  * WPA Supplicant - Layer2 packet handling with libpcap/libdnet and WinPcap
3  * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
17 #include <sys/ioctl.h>
18 #endif /* CONFIG_NATIVE_WINDOWS */
19 #include <pcap.h>
20 #ifndef CONFIG_WINPCAP
21 #include <dnet.h>
22 #endif /* CONFIG_WINPCAP */
23 
24 #include "common.h"
25 #include "eloop.h"
26 #include "l2_packet.h"
27 
28 
29 static const u8 pae_group_addr[ETH_ALEN] =
30 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
31 
32 struct l2_packet_data {
33 	pcap_t *pcap;
34 #ifdef CONFIG_WINPCAP
35 	unsigned int num_fast_poll;
36 #else /* CONFIG_WINPCAP */
37 	eth_t *eth;
38 #endif /* CONFIG_WINPCAP */
39 	char ifname[100];
40 	u8 own_addr[ETH_ALEN];
41 	void (*rx_callback)(void *ctx, const u8 *src_addr,
42 			    const u8 *buf, size_t len);
43 	void *rx_callback_ctx;
44 	int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
45 			* to rx_callback */
46 };
47 
48 
49 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
50 {
51 	os_memcpy(addr, l2->own_addr, ETH_ALEN);
52 	return 0;
53 }
54 
55 
56 #ifndef CONFIG_WINPCAP
57 static int l2_packet_init_libdnet(struct l2_packet_data *l2)
58 {
59 	eth_addr_t own_addr;
60 
61 	l2->eth = eth_open(l2->ifname);
62 	if (!l2->eth) {
63 		printf("Failed to open interface '%s'.\n", l2->ifname);
64 		perror("eth_open");
65 		return -1;
66 	}
67 
68 	if (eth_get(l2->eth, &own_addr) < 0) {
69 		printf("Failed to get own hw address from interface '%s'.\n",
70 		       l2->ifname);
71 		perror("eth_get");
72 		eth_close(l2->eth);
73 		l2->eth = NULL;
74 		return -1;
75 	}
76 	os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
77 
78 	return 0;
79 }
80 #endif /* CONFIG_WINPCAP */
81 
82 
83 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
84 		   const u8 *buf, size_t len)
85 {
86 	int ret;
87 	struct l2_ethhdr *eth;
88 
89 	if (l2 == NULL)
90 		return -1;
91 
92 	if (l2->l2_hdr) {
93 #ifdef CONFIG_WINPCAP
94 		ret = pcap_sendpacket(l2->pcap, buf, len);
95 #else /* CONFIG_WINPCAP */
96 		ret = eth_send(l2->eth, buf, len);
97 #endif /* CONFIG_WINPCAP */
98 	} else {
99 		size_t mlen = sizeof(*eth) + len;
100 		eth = os_malloc(mlen);
101 		if (eth == NULL)
102 			return -1;
103 
104 		os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
105 		os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
106 		eth->h_proto = htons(proto);
107 		os_memcpy(eth + 1, buf, len);
108 
109 #ifdef CONFIG_WINPCAP
110 		ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
111 #else /* CONFIG_WINPCAP */
112 		ret = eth_send(l2->eth, (u8 *) eth, mlen);
113 #endif /* CONFIG_WINPCAP */
114 
115 		os_free(eth);
116 	}
117 
118 	return ret;
119 }
120 
121 
122 #ifndef CONFIG_WINPCAP
123 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
124 {
125 	struct l2_packet_data *l2 = eloop_ctx;
126 	pcap_t *pcap = sock_ctx;
127 	struct pcap_pkthdr hdr;
128 	const u_char *packet;
129 	struct l2_ethhdr *ethhdr;
130 	unsigned char *buf;
131 	size_t len;
132 
133 	packet = pcap_next(pcap, &hdr);
134 
135 	if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
136 		return;
137 
138 	ethhdr = (struct l2_ethhdr *) packet;
139 	if (l2->l2_hdr) {
140 		buf = (unsigned char *) ethhdr;
141 		len = hdr.caplen;
142 	} else {
143 		buf = (unsigned char *) (ethhdr + 1);
144 		len = hdr.caplen - sizeof(*ethhdr);
145 	}
146 	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
147 }
148 #endif /* CONFIG_WINPCAP */
149 
150 
151 #ifdef CONFIG_WINPCAP
152 static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
153 				 const u_char *pkt_data)
154 {
155 	struct l2_packet_data *l2 = (struct l2_packet_data *) user;
156 	struct l2_ethhdr *ethhdr;
157 	unsigned char *buf;
158 	size_t len;
159 
160 	if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
161 		return;
162 
163 	ethhdr = (struct l2_ethhdr *) pkt_data;
164 	if (l2->l2_hdr) {
165 		buf = (unsigned char *) ethhdr;
166 		len = hdr->caplen;
167 	} else {
168 		buf = (unsigned char *) (ethhdr + 1);
169 		len = hdr->caplen - sizeof(*ethhdr);
170 	}
171 	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
172 	/*
173 	 * Use shorter poll interval for 3 seconds to reduce latency during key
174 	 * handshake.
175 	 */
176 	l2->num_fast_poll = 3 * 50;
177 }
178 
179 
180 static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
181 {
182 	struct l2_packet_data *l2 = eloop_ctx;
183 	pcap_t *pcap = timeout_ctx;
184 	int timeout;
185 
186 	if (l2->num_fast_poll > 0) {
187 		timeout = 20000;
188 		l2->num_fast_poll--;
189 	} else
190 		timeout = 100000;
191 
192 	/* Register new timeout before calling l2_packet_receive() since
193 	 * receive handler may free this l2_packet instance (which will
194 	 * cancel this timeout). */
195 	eloop_register_timeout(0, timeout, l2_packet_receive_timeout,
196 			       l2, pcap);
197 	pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
198 }
199 #endif /* CONFIG_WINPCAP */
200 
201 
202 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
203 				  unsigned short protocol)
204 {
205 	bpf_u_int32 pcap_maskp, pcap_netp;
206 	char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
207 	struct bpf_program pcap_fp;
208 
209 #ifdef CONFIG_WINPCAP
210 	char ifname[128];
211 	os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", l2->ifname);
212 	pcap_lookupnet(ifname, &pcap_netp, &pcap_maskp, pcap_err);
213 	l2->pcap = pcap_open_live(ifname, 2500, 0, 10, pcap_err);
214 	if (l2->pcap == NULL) {
215 		fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
216 		fprintf(stderr, "ifname='%s'\n", ifname);
217 		return -1;
218 	}
219 	if (pcap_setnonblock(l2->pcap, 1, pcap_err) < 0)
220 		fprintf(stderr, "pcap_setnonblock: %s\n",
221 			pcap_geterr(l2->pcap));
222 #else /* CONFIG_WINPCAP */
223 	pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
224 	l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
225 	if (l2->pcap == NULL) {
226 		fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
227 		fprintf(stderr, "ifname='%s'\n", l2->ifname);
228 		return -1;
229 	}
230 	if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
231 	    pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
232 		fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
233 			pcap_geterr(l2->pcap));
234 		return -1;
235 	}
236 #endif /* CONFIG_WINPCAP */
237 	os_snprintf(pcap_filter, sizeof(pcap_filter),
238 		    "not ether src " MACSTR " and "
239 		    "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
240 		    "ether proto 0x%x",
241 		    MAC2STR(l2->own_addr), /* do not receive own packets */
242 		    MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
243 		    protocol);
244 	if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
245 		fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
246 		return -1;
247 	}
248 
249 	if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
250 		fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
251 		return -1;
252 	}
253 
254 	pcap_freecode(&pcap_fp);
255 #ifdef BIOCIMMEDIATE
256 	/*
257 	 * When libpcap uses BPF we must enable "immediate mode" to
258 	 * receive frames right away; otherwise the system may
259 	 * buffer them for us.
260 	 */
261 	{
262 		unsigned int on = 1;
263 		if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
264 			fprintf(stderr, "%s: cannot enable immediate mode on "
265 				"interface %s: %s\n",
266 				__func__, l2->ifname, strerror(errno));
267 			/* XXX should we fail? */
268 		}
269 	}
270 #endif /* BIOCIMMEDIATE */
271 
272 #ifdef CONFIG_WINPCAP
273 	eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
274 			       l2, l2->pcap);
275 #else /* CONFIG_WINPCAP */
276 	eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
277 				 l2_packet_receive, l2, l2->pcap);
278 #endif /* CONFIG_WINPCAP */
279 
280 	return 0;
281 }
282 
283 
284 struct l2_packet_data * l2_packet_init(
285 	const char *ifname, const u8 *own_addr, unsigned short protocol,
286 	void (*rx_callback)(void *ctx, const u8 *src_addr,
287 			    const u8 *buf, size_t len),
288 	void *rx_callback_ctx, int l2_hdr)
289 {
290 	struct l2_packet_data *l2;
291 
292 	l2 = os_zalloc(sizeof(struct l2_packet_data));
293 	if (l2 == NULL)
294 		return NULL;
295 	os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
296 	l2->rx_callback = rx_callback;
297 	l2->rx_callback_ctx = rx_callback_ctx;
298 	l2->l2_hdr = l2_hdr;
299 
300 #ifdef CONFIG_WINPCAP
301 	if (own_addr)
302 		os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
303 #else /* CONFIG_WINPCAP */
304 	if (l2_packet_init_libdnet(l2))
305 		return NULL;
306 #endif /* CONFIG_WINPCAP */
307 
308 	if (l2_packet_init_libpcap(l2, protocol)) {
309 #ifndef CONFIG_WINPCAP
310 		eth_close(l2->eth);
311 #endif /* CONFIG_WINPCAP */
312 		os_free(l2);
313 		return NULL;
314 	}
315 
316 	return l2;
317 }
318 
319 
320 void l2_packet_deinit(struct l2_packet_data *l2)
321 {
322 	if (l2 == NULL)
323 		return;
324 
325 #ifdef CONFIG_WINPCAP
326 	eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
327 #else /* CONFIG_WINPCAP */
328 	if (l2->eth)
329 		eth_close(l2->eth);
330 	eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
331 #endif /* CONFIG_WINPCAP */
332 	if (l2->pcap)
333 		pcap_close(l2->pcap);
334 	os_free(l2);
335 }
336 
337 
338 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
339 {
340 	pcap_if_t *devs, *dev;
341 	struct pcap_addr *addr;
342 	struct sockaddr_in *saddr;
343 	int found = 0;
344 	char err[PCAP_ERRBUF_SIZE + 1];
345 
346 	if (pcap_findalldevs(&devs, err) < 0) {
347 		wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
348 		return -1;
349 	}
350 
351 	for (dev = devs; dev && !found; dev = dev->next) {
352 		if (os_strcmp(dev->name, l2->ifname) != 0)
353 			continue;
354 
355 		addr = dev->addresses;
356 		while (addr) {
357 			saddr = (struct sockaddr_in *) addr->addr;
358 			if (saddr && saddr->sin_family == AF_INET) {
359 				os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
360 					   len);
361 				found = 1;
362 				break;
363 			}
364 			addr = addr->next;
365 		}
366 	}
367 
368 	pcap_freealldevs(devs);
369 
370 	return found ? 0 : -1;
371 }
372 
373 
374 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
375 {
376 #ifdef CONFIG_WINPCAP
377 	/*
378 	 * Use shorter poll interval for 3 seconds to reduce latency during key
379 	 * handshake.
380 	 */
381 	l2->num_fast_poll = 3 * 50;
382 	eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
383 	eloop_register_timeout(0, 10000, l2_packet_receive_timeout,
384 			       l2, l2->pcap);
385 #endif /* CONFIG_WINPCAP */
386 }
387