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 software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #ifndef CONFIG_NATIVE_WINDOWS
11 #include <sys/ioctl.h>
12 #endif /* CONFIG_NATIVE_WINDOWS */
13 #include <pcap.h>
14 #ifndef CONFIG_WINPCAP
15 #include <dnet.h>
16 #endif /* CONFIG_WINPCAP */
17 
18 #include "common.h"
19 #include "eloop.h"
20 #include "l2_packet.h"
21 
22 
23 static const u8 pae_group_addr[ETH_ALEN] =
24 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
25 
26 struct l2_packet_data {
27 	pcap_t *pcap;
28 #ifdef CONFIG_WINPCAP
29 	unsigned int num_fast_poll;
30 #else /* CONFIG_WINPCAP */
31 	eth_t *eth;
32 #endif /* CONFIG_WINPCAP */
33 	char ifname[100];
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
39 			* to rx_callback */
40 };
41 
42 
43 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
44 {
45 	os_memcpy(addr, l2->own_addr, ETH_ALEN);
46 	return 0;
47 }
48 
49 
50 #ifndef CONFIG_WINPCAP
51 static int l2_packet_init_libdnet(struct l2_packet_data *l2)
52 {
53 	eth_addr_t own_addr;
54 
55 	l2->eth = eth_open(l2->ifname);
56 	if (!l2->eth) {
57 		printf("Failed to open interface '%s'.\n", l2->ifname);
58 		perror("eth_open");
59 		return -1;
60 	}
61 
62 	if (eth_get(l2->eth, &own_addr) < 0) {
63 		printf("Failed to get own hw address from interface '%s'.\n",
64 		       l2->ifname);
65 		perror("eth_get");
66 		eth_close(l2->eth);
67 		l2->eth = NULL;
68 		return -1;
69 	}
70 	os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
71 
72 	return 0;
73 }
74 #endif /* CONFIG_WINPCAP */
75 
76 
77 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
78 		   const u8 *buf, size_t len)
79 {
80 	int ret;
81 	struct l2_ethhdr *eth;
82 
83 	if (l2 == NULL)
84 		return -1;
85 
86 	if (l2->l2_hdr) {
87 #ifdef CONFIG_WINPCAP
88 		ret = pcap_sendpacket(l2->pcap, buf, len);
89 #else /* CONFIG_WINPCAP */
90 		ret = eth_send(l2->eth, buf, len);
91 #endif /* CONFIG_WINPCAP */
92 	} else {
93 		size_t mlen = sizeof(*eth) + len;
94 		eth = os_malloc(mlen);
95 		if (eth == NULL)
96 			return -1;
97 
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);
102 
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 */
108 
109 		os_free(eth);
110 	}
111 
112 	return ret;
113 }
114 
115 
116 #ifndef CONFIG_WINPCAP
117 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
118 {
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;
124 	unsigned char *buf;
125 	size_t len;
126 
127 	packet = pcap_next(pcap, &hdr);
128 
129 	if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
130 		return;
131 
132 	ethhdr = (struct l2_ethhdr *) packet;
133 	if (l2->l2_hdr) {
134 		buf = (unsigned char *) ethhdr;
135 		len = hdr.caplen;
136 	} else {
137 		buf = (unsigned char *) (ethhdr + 1);
138 		len = hdr.caplen - sizeof(*ethhdr);
139 	}
140 	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
141 }
142 #endif /* CONFIG_WINPCAP */
143 
144 
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)
148 {
149 	struct l2_packet_data *l2 = (struct l2_packet_data *) user;
150 	struct l2_ethhdr *ethhdr;
151 	unsigned char *buf;
152 	size_t len;
153 
154 	if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
155 		return;
156 
157 	ethhdr = (struct l2_ethhdr *) pkt_data;
158 	if (l2->l2_hdr) {
159 		buf = (unsigned char *) ethhdr;
160 		len = hdr->caplen;
161 	} else {
162 		buf = (unsigned char *) (ethhdr + 1);
163 		len = hdr->caplen - sizeof(*ethhdr);
164 	}
165 	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
166 	/*
167 	 * Use shorter poll interval for 3 seconds to reduce latency during key
168 	 * handshake.
169 	 */
170 	l2->num_fast_poll = 3 * 50;
171 }
172 
173 
174 static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176 	struct l2_packet_data *l2 = eloop_ctx;
177 	pcap_t *pcap = timeout_ctx;
178 	int timeout;
179 
180 	if (l2->num_fast_poll > 0) {
181 		timeout = 20000;
182 		l2->num_fast_poll--;
183 	} else
184 		timeout = 100000;
185 
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,
190 			       l2, pcap);
191 	pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
192 }
193 #endif /* CONFIG_WINPCAP */
194 
195 
196 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
197 				  unsigned short protocol)
198 {
199 	bpf_u_int32 pcap_maskp, pcap_netp;
200 	char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
201 	struct bpf_program pcap_fp;
202 
203 #ifdef CONFIG_WINPCAP
204 	char ifname[128];
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);
211 		return -1;
212 	}
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);
222 		return -1;
223 	}
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));
228 		return -1;
229 	}
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 "
234 		    "ether proto 0x%x",
235 		    MAC2STR(l2->own_addr), /* do not receive own packets */
236 		    MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
237 		    protocol);
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));
240 		return -1;
241 	}
242 
243 	if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
244 		fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
245 		return -1;
246 	}
247 
248 	pcap_freecode(&pcap_fp);
249 #ifdef BIOCIMMEDIATE
250 	/*
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.
254 	 */
255 	{
256 		unsigned int on = 1;
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? */
262 		}
263 	}
264 #endif /* BIOCIMMEDIATE */
265 
266 #ifdef CONFIG_WINPCAP
267 	eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
268 			       l2, l2->pcap);
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 */
273 
274 	return 0;
275 }
276 
277 
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)
283 {
284 	struct l2_packet_data *l2;
285 
286 	l2 = os_zalloc(sizeof(struct l2_packet_data));
287 	if (l2 == NULL)
288 		return NULL;
289 	os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
290 	l2->rx_callback = rx_callback;
291 	l2->rx_callback_ctx = rx_callback_ctx;
292 	l2->l2_hdr = l2_hdr;
293 
294 #ifdef CONFIG_WINPCAP
295 	if (own_addr)
296 		os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
297 #else /* CONFIG_WINPCAP */
298 	if (l2_packet_init_libdnet(l2))
299 		return NULL;
300 #endif /* CONFIG_WINPCAP */
301 
302 	if (l2_packet_init_libpcap(l2, protocol)) {
303 #ifndef CONFIG_WINPCAP
304 		eth_close(l2->eth);
305 #endif /* CONFIG_WINPCAP */
306 		os_free(l2);
307 		return NULL;
308 	}
309 
310 	return l2;
311 }
312 
313 
314 void l2_packet_deinit(struct l2_packet_data *l2)
315 {
316 	if (l2 == NULL)
317 		return;
318 
319 #ifdef CONFIG_WINPCAP
320 	eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
321 #else /* CONFIG_WINPCAP */
322 	if (l2->eth)
323 		eth_close(l2->eth);
324 	eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
325 #endif /* CONFIG_WINPCAP */
326 	if (l2->pcap)
327 		pcap_close(l2->pcap);
328 	os_free(l2);
329 }
330 
331 
332 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
333 {
334 	pcap_if_t *devs, *dev;
335 	struct pcap_addr *addr;
336 	struct sockaddr_in *saddr;
337 	int found = 0;
338 	char err[PCAP_ERRBUF_SIZE + 1];
339 
340 	if (pcap_findalldevs(&devs, err) < 0) {
341 		wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
342 		return -1;
343 	}
344 
345 	for (dev = devs; dev && !found; dev = dev->next) {
346 		if (os_strcmp(dev->name, l2->ifname) != 0)
347 			continue;
348 
349 		addr = dev->addresses;
350 		while (addr) {
351 			saddr = (struct sockaddr_in *) addr->addr;
352 			if (saddr && saddr->sin_family == AF_INET) {
353 				os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
354 					   len);
355 				found = 1;
356 				break;
357 			}
358 			addr = addr->next;
359 		}
360 	}
361 
362 	pcap_freealldevs(devs);
363 
364 	return found ? 0 : -1;
365 }
366 
367 
368 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
369 {
370 #ifdef CONFIG_WINPCAP
371 	/*
372 	 * Use shorter poll interval for 3 seconds to reduce latency during key
373 	 * handshake.
374 	 */
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,
378 			       l2, l2->pcap);
379 #endif /* CONFIG_WINPCAP */
380 }
381