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