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