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