xref: /freebsd/contrib/wpa/src/l2_packet/l2_packet_freebsd.c (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1 /*
2  * WPA Supplicant - Layer2 packet handling with FreeBSD
3  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2005, Sam Leffler <sam@errno.com>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "includes.h"
11 #include <sys/param.h>
12 #if defined(__APPLE__) || defined(__GLIBC__) || defined(__FreeBSD_version)
13 #include <net/bpf.h>
14 #endif /* __APPLE__ */
15 #include <pcap.h>
16 
17 #include <sys/ioctl.h>
18 #ifdef __sun__
19 #include <libdlpi.h>
20 #else /* __sun__ */
21 #include <sys/sysctl.h>
22 #endif /* __sun__ */
23 
24 #include <net/if.h>
25 #include <net/if_dl.h>
26 #include <net/route.h>
27 #include <netinet/in.h>
28 
29 #include "common.h"
30 #include "eloop.h"
31 #include "l2_packet.h"
32 
33 
34 static const u8 pae_group_addr[ETH_ALEN] =
35 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
36 
37 struct l2_packet_data {
38 	pcap_t *pcap;
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 data
45 		     * buffers */
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 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
57 		   const u8 *buf, size_t len)
58 {
59 	if (!l2->l2_hdr) {
60 		int ret;
61 		struct l2_ethhdr *eth = os_malloc(sizeof(*eth) + len);
62 		if (eth == NULL)
63 			return -1;
64 		os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
65 		os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
66 		eth->h_proto = htons(proto);
67 		os_memcpy(eth + 1, buf, len);
68 		ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
69 		os_free(eth);
70 		return ret;
71 	} else
72 		return pcap_inject(l2->pcap, buf, len);
73 }
74 
75 
76 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
77 {
78 	struct l2_packet_data *l2 = eloop_ctx;
79 	pcap_t *pcap = sock_ctx;
80 	struct pcap_pkthdr *hdr;
81 	const u_char *packet;
82 	struct l2_ethhdr *ethhdr;
83 	unsigned char *buf;
84 	size_t len;
85 
86 	if (pcap_next_ex(pcap, &hdr, &packet) == -1) {
87 		wpa_printf(MSG_ERROR, "Error reading packet, has device disappeared?");
88 		packet = NULL;
89 		eloop_terminate();
90 	}
91 
92 	if (!l2->rx_callback || !packet || hdr->caplen < sizeof(*ethhdr))
93 		return;
94 
95 	ethhdr = (struct l2_ethhdr *) packet;
96 	if (l2->l2_hdr) {
97 		buf = (unsigned char *) ethhdr;
98 		len = hdr->caplen;
99 	} else {
100 		buf = (unsigned char *) (ethhdr + 1);
101 		len = hdr->caplen - sizeof(*ethhdr);
102 	}
103 	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
104 }
105 
106 
107 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
108 				  unsigned short protocol)
109 {
110 	bpf_u_int32 pcap_maskp, pcap_netp;
111 	char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
112 	struct bpf_program pcap_fp;
113 
114 	pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
115 	l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
116 	if (l2->pcap == NULL) {
117 		fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
118 		fprintf(stderr, "ifname='%s'\n", l2->ifname);
119 		return -1;
120 	}
121 	if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
122 	    pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
123 		fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
124 			pcap_geterr(l2->pcap));
125 		return -1;
126 	}
127 	os_snprintf(pcap_filter, sizeof(pcap_filter),
128 		    "not ether src " MACSTR " and "
129 		    "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
130 		    "ether proto 0x%x",
131 		    MAC2STR(l2->own_addr), /* do not receive own packets */
132 		    MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
133 		    protocol);
134 	if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
135 		fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
136 		return -1;
137 	}
138 
139 	if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
140 		fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
141 		return -1;
142 	}
143 
144 	pcap_freecode(&pcap_fp);
145 #ifndef __sun__
146 	/*
147 	 * When libpcap uses BPF we must enable "immediate mode" to
148 	 * receive frames right away; otherwise the system may
149 	 * buffer them for us.
150 	 */
151 	{
152 		unsigned int on = 1;
153 		if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
154 			fprintf(stderr, "%s: cannot enable immediate mode on "
155 				"interface %s: %s\n",
156 				__func__, l2->ifname, strerror(errno));
157 			/* XXX should we fail? */
158 		}
159 	}
160 #endif /* __sun__ */
161 
162 	eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
163 				 l2_packet_receive, l2, l2->pcap);
164 
165 	return 0;
166 }
167 
168 
169 static int eth_get(const char *device, u8 ea[ETH_ALEN])
170 {
171 #ifdef __sun__
172 	dlpi_handle_t dh;
173 	u32 physaddrlen = DLPI_PHYSADDR_MAX;
174 	u8 physaddr[DLPI_PHYSADDR_MAX];
175 	int retval;
176 
177 	retval = dlpi_open(device, &dh, 0);
178 	if (retval != DLPI_SUCCESS) {
179 		wpa_printf(MSG_ERROR, "dlpi_open error: %s",
180 			   dlpi_strerror(retval));
181 		return -1;
182 	}
183 
184 	retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr,
185 				   &physaddrlen);
186 	if (retval != DLPI_SUCCESS) {
187 		wpa_printf(MSG_ERROR, "dlpi_get_physaddr error: %s",
188 			   dlpi_strerror(retval));
189 		dlpi_close(dh);
190 		return -1;
191 	}
192 	os_memcpy(ea, physaddr, ETH_ALEN);
193 	dlpi_close(dh);
194 #else /* __sun__ */
195 	struct if_msghdr *ifm;
196 	struct sockaddr_dl *sdl;
197 	u_char *p, *buf;
198 	size_t len;
199 	int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
200 
201 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
202 		return -1;
203 	if ((buf = os_malloc(len)) == NULL)
204 		return -1;
205 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
206 		os_free(buf);
207 		return -1;
208 	}
209 	for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
210 		ifm = (struct if_msghdr *)p;
211 		sdl = (struct sockaddr_dl *)(ifm + 1);
212 		if (ifm->ifm_type != RTM_IFINFO ||
213 		    (ifm->ifm_addrs & RTA_IFP) == 0)
214 			continue;
215 		if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
216 		    os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
217 			continue;
218 		os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
219 		break;
220 	}
221 	os_free(buf);
222 
223 	if (p >= buf + len) {
224 		errno = ESRCH;
225 		return -1;
226 	}
227 #endif /* __sun__ */
228 	return 0;
229 }
230 
231 
232 struct l2_packet_data * l2_packet_init(
233 	const char *ifname, const u8 *own_addr, unsigned short protocol,
234 	void (*rx_callback)(void *ctx, const u8 *src_addr,
235 			    const u8 *buf, size_t len),
236 	void *rx_callback_ctx, int l2_hdr)
237 {
238 	struct l2_packet_data *l2;
239 
240 	l2 = os_zalloc(sizeof(struct l2_packet_data));
241 	if (l2 == NULL)
242 		return NULL;
243 	os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
244 	l2->rx_callback = rx_callback;
245 	l2->rx_callback_ctx = rx_callback_ctx;
246 	l2->l2_hdr = l2_hdr;
247 
248 	if (eth_get(l2->ifname, l2->own_addr) < 0) {
249 		fprintf(stderr, "Failed to get link-level address for "
250 			"interface '%s'.\n", l2->ifname);
251 		os_free(l2);
252 		return NULL;
253 	}
254 
255 	if (l2_packet_init_libpcap(l2, protocol)) {
256 		os_free(l2);
257 		return NULL;
258 	}
259 
260 	return l2;
261 }
262 
263 
264 struct l2_packet_data * l2_packet_init_bridge(
265 	const char *br_ifname, const char *ifname, const u8 *own_addr,
266 	unsigned short protocol,
267 	void (*rx_callback)(void *ctx, const u8 *src_addr,
268 			    const u8 *buf, size_t len),
269 	void *rx_callback_ctx, int l2_hdr)
270 {
271 	return l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
272 			      rx_callback_ctx, l2_hdr);
273 }
274 
275 
276 void l2_packet_deinit(struct l2_packet_data *l2)
277 {
278 	if (l2 != NULL) {
279 		if (l2->pcap) {
280 			eloop_unregister_read_sock(
281 				pcap_get_selectable_fd(l2->pcap));
282 			pcap_close(l2->pcap);
283 		}
284 		os_free(l2);
285 	}
286 }
287 
288 
289 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
290 {
291 	pcap_if_t *devs, *dev;
292 	struct pcap_addr *addr;
293 	struct sockaddr_in *saddr;
294 	int found = 0;
295 	char err[PCAP_ERRBUF_SIZE + 1];
296 
297 	if (pcap_findalldevs(&devs, err) < 0) {
298 		wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
299 		return -1;
300 	}
301 
302 	for (dev = devs; dev && !found; dev = dev->next) {
303 		if (os_strcmp(dev->name, l2->ifname) != 0)
304 			continue;
305 
306 		addr = dev->addresses;
307 		while (addr) {
308 			saddr = (struct sockaddr_in *) addr->addr;
309 			if (saddr && saddr->sin_family == AF_INET) {
310 				os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
311 					   len);
312 				found = 1;
313 				break;
314 			}
315 			addr = addr->next;
316 		}
317 	}
318 
319 	pcap_freealldevs(devs);
320 
321 	return found ? 0 : -1;
322 }
323 
324 
325 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
326 {
327 }
328 
329 
330 int l2_packet_set_packet_filter(struct l2_packet_data *l2,
331 				enum l2_packet_filter_type type)
332 {
333 	return -1;
334 }
335