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