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