[picopc] Added emulation of Android private ioctls for generic WiFi drivers.
[picopc-android-gingerbread:external-wpa_supplicant_6.git] / wpa_supplicant / src / drivers / driver_wext.c
1 /*
2  * WPA Supplicant - driver interaction with generic Linux Wireless Extensions
3  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file implements a driver interface for the Linux Wireless Extensions.
15  * When used with WE-18 or newer, this interface can be used as-is with number
16  * of drivers. In addition to this, some of the common functions in this file
17  * can be used by other driver interface implementations that use generic WE
18  * ioctls, but require private ioctls for some of the functionality.
19  */
20
21 #include "includes.h"
22 #include <sys/ioctl.h>
23 #include <net/if_arp.h>
24 #include <net/if.h>
25
26 #include "wireless_copy.h"
27 #include "common.h"
28 #include "driver.h"
29 #include "eloop.h"
30 #include "priv_netlink.h"
31 #include "driver_wext.h"
32 #include "ieee802_11_defs.h"
33 #include "wpa_common.h"
34 #include "wpa_ctrl.h"
35 #include "wpa_supplicant_i.h"
36 #include "config_ssid.h"
37
38
39 static int wpa_driver_wext_flush_pmkid(void *priv);
40 static int wpa_driver_wext_get_range(void *priv);
41 static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data *drv);
42 static void wpa_driver_wext_disconnect(struct wpa_driver_wext_data *drv);
43
44
45 static int wpa_driver_wext_send_oper_ifla(struct wpa_driver_wext_data *drv,
46                                           int linkmode, int operstate)
47 {
48         struct {
49                 struct nlmsghdr hdr;
50                 struct ifinfomsg ifinfo;
51                 char opts[16];
52         } req;
53         struct rtattr *rta;
54         static int nl_seq;
55         ssize_t ret;
56
57         os_memset(&req, 0, sizeof(req));
58
59         req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
60         req.hdr.nlmsg_type = RTM_SETLINK;
61         req.hdr.nlmsg_flags = NLM_F_REQUEST;
62         req.hdr.nlmsg_seq = ++nl_seq;
63         req.hdr.nlmsg_pid = 0;
64
65         req.ifinfo.ifi_family = AF_UNSPEC;
66         req.ifinfo.ifi_type = 0;
67         req.ifinfo.ifi_index = drv->ifindex;
68         req.ifinfo.ifi_flags = 0;
69         req.ifinfo.ifi_change = 0;
70
71         if (linkmode != -1) {
72                 rta = aliasing_hide_typecast(
73                         ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
74                         struct rtattr);
75                 rta->rta_type = IFLA_LINKMODE;
76                 rta->rta_len = RTA_LENGTH(sizeof(char));
77                 *((char *) RTA_DATA(rta)) = linkmode;
78                 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
79                         RTA_LENGTH(sizeof(char));
80         }
81         if (operstate != -1) {
82                 rta = (struct rtattr *)
83                         ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
84                 rta->rta_type = IFLA_OPERSTATE;
85                 rta->rta_len = RTA_LENGTH(sizeof(char));
86                 *((char *) RTA_DATA(rta)) = operstate;
87                 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
88                         RTA_LENGTH(sizeof(char));
89         }
90
91         wpa_printf(MSG_DEBUG, "WEXT: Operstate: linkmode=%d, operstate=%d",
92                    linkmode, operstate);
93
94         ret = send(drv->event_sock, &req, req.hdr.nlmsg_len, 0);
95         if (ret < 0) {
96                 wpa_printf(MSG_DEBUG, "WEXT: Sending operstate IFLA failed: "
97                            "%s (assume operstate is not supported)",
98                            strerror(errno));
99         }
100
101         return ret < 0 ? -1 : 0;
102 }
103
104
105 int wpa_driver_wext_set_auth_param(struct wpa_driver_wext_data *drv,
106                                    int idx, u32 value)
107 {
108         struct iwreq iwr;
109         int ret = 0;
110
111         os_memset(&iwr, 0, sizeof(iwr));
112         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
113         iwr.u.param.flags = idx & IW_AUTH_INDEX;
114         iwr.u.param.value = value;
115
116         if (ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr) < 0) {
117                 if (errno != EOPNOTSUPP) {
118                         wpa_printf(MSG_DEBUG, "WEXT: SIOCSIWAUTH(param %d "
119                                    "value 0x%x) failed: %s)",
120                                    idx, value, strerror(errno));
121                 }
122                 ret = errno == EOPNOTSUPP ? -2 : -1;
123         }
124
125         return ret;
126 }
127
128
129 /**
130  * wpa_driver_wext_get_bssid - Get BSSID, SIOCGIWAP
131  * @priv: Pointer to private wext data from wpa_driver_wext_init()
132  * @bssid: Buffer for BSSID
133  * Returns: 0 on success, -1 on failure
134  */
135 int wpa_driver_wext_get_bssid(void *priv, u8 *bssid)
136 {
137         struct wpa_driver_wext_data *drv = priv;
138         struct iwreq iwr;
139         int ret = 0;
140
141         os_memset(&iwr, 0, sizeof(iwr));
142         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
143
144         if (ioctl(drv->ioctl_sock, SIOCGIWAP, &iwr) < 0) {
145                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWAP]");
146                 ret = -1;
147         }
148         os_memcpy(bssid, iwr.u.ap_addr.sa_data, ETH_ALEN);
149
150         return ret;
151 }
152
153
154 /**
155  * wpa_driver_wext_set_bssid - Set BSSID, SIOCSIWAP
156  * @priv: Pointer to private wext data from wpa_driver_wext_init()
157  * @bssid: BSSID
158  * Returns: 0 on success, -1 on failure
159  */
160 int wpa_driver_wext_set_bssid(void *priv, const u8 *bssid)
161 {
162         struct wpa_driver_wext_data *drv = priv;
163         struct iwreq iwr;
164         int ret = 0;
165
166         os_memset(&iwr, 0, sizeof(iwr));
167         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
168         iwr.u.ap_addr.sa_family = ARPHRD_ETHER;
169         if (bssid)
170                 os_memcpy(iwr.u.ap_addr.sa_data, bssid, ETH_ALEN);
171         else
172                 os_memset(iwr.u.ap_addr.sa_data, 0, ETH_ALEN);
173
174         if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr) < 0) {
175                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWAP]");
176                 ret = -1;
177         }
178
179         return ret;
180 }
181
182
183 /**
184  * wpa_driver_wext_get_ssid - Get SSID, SIOCGIWESSID
185  * @priv: Pointer to private wext data from wpa_driver_wext_init()
186  * @ssid: Buffer for the SSID; must be at least 32 bytes long
187  * Returns: SSID length on success, -1 on failure
188  */
189 int wpa_driver_wext_get_ssid(void *priv, u8 *ssid)
190 {
191         struct wpa_driver_wext_data *drv = priv;
192         struct iwreq iwr;
193         int ret = 0;
194
195         os_memset(&iwr, 0, sizeof(iwr));
196         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
197         iwr.u.essid.pointer = (caddr_t) ssid;
198         iwr.u.essid.length = 32;
199
200         if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
201                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWESSID]");
202                 ret = -1;
203         } else {
204                 ret = iwr.u.essid.length;
205                 if (ret > 32)
206                         ret = 32;
207                 /* Some drivers include nul termination in the SSID, so let's
208                  * remove it here before further processing. WE-21 changes this
209                  * to explicitly require the length _not_ to include nul
210                  * termination. */
211                 if (ret > 0 && ssid[ret - 1] == '\0' &&
212                     drv->we_version_compiled < 21)
213                         ret--;
214         }
215
216         return ret;
217 }
218
219
220 /**
221  * wpa_driver_wext_set_ssid - Set SSID, SIOCSIWESSID
222  * @priv: Pointer to private wext data from wpa_driver_wext_init()
223  * @ssid: SSID
224  * @ssid_len: Length of SSID (0..32)
225  * Returns: 0 on success, -1 on failure
226  */
227 int wpa_driver_wext_set_ssid(void *priv, const u8 *ssid, size_t ssid_len)
228 {
229         struct wpa_driver_wext_data *drv = priv;
230         struct iwreq iwr;
231         int ret = 0;
232         char buf[33];
233
234         if (ssid_len > 32)
235                 return -1;
236
237         os_memset(&iwr, 0, sizeof(iwr));
238         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
239         /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
240         iwr.u.essid.flags = (ssid_len != 0);
241         os_memset(buf, 0, sizeof(buf));
242         os_memcpy(buf, ssid, ssid_len);
243         iwr.u.essid.pointer = (caddr_t) buf;
244
245         os_memset(drv->ssid, 0, 32);
246         os_memcpy(drv->ssid, ssid, ssid_len);
247         drv->ssid_len = ssid_len;
248
249         if (drv->we_version_compiled < 21) {
250                 /* For historic reasons, set SSID length to include one extra
251                  * character, C string nul termination, even though SSID is
252                  * really an octet string that should not be presented as a C
253                  * string. Some Linux drivers decrement the length by one and
254                  * can thus end up missing the last octet of the SSID if the
255                  * length is not incremented here. WE-21 changes this to
256                  * explicitly require the length _not_ to include nul
257                  * termination. */
258                 if (ssid_len)
259                         ssid_len++;
260         }
261         iwr.u.essid.length = ssid_len;
262
263         if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
264                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID]");
265                 ret = -1;
266         }
267
268         return ret;
269 }
270
271
272 /**
273  * wpa_driver_wext_set_freq - Set frequency/channel, SIOCSIWFREQ
274  * @priv: Pointer to private wext data from wpa_driver_wext_init()
275  * @freq: Frequency in MHz
276  * Returns: 0 on success, -1 on failure
277  */
278 int wpa_driver_wext_set_freq(void *priv, int freq)
279 {
280         struct wpa_driver_wext_data *drv = priv;
281         struct iwreq iwr;
282         int ret = 0;
283
284         os_memset(&iwr, 0, sizeof(iwr));
285         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
286         iwr.u.freq.m = freq * 100000;
287         iwr.u.freq.e = 1;
288
289         if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
290                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWFREQ]");
291                 ret = -1;
292         }
293
294         return ret;
295 }
296
297
298 static void
299 wpa_driver_wext_event_wireless_custom(void *ctx, char *custom)
300 {
301         union wpa_event_data data;
302
303         wpa_printf(MSG_MSGDUMP, "WEXT: Custom wireless event: '%s'",
304                    custom);
305
306         os_memset(&data, 0, sizeof(data));
307         /* Host AP driver */
308         if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
309                 data.michael_mic_failure.unicast =
310                         os_strstr(custom, " unicast ") != NULL;
311                 /* TODO: parse parameters(?) */
312                 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
313         } else if (os_strncmp(custom, "ASSOCINFO(ReqIEs=", 17) == 0) {
314                 char *spos;
315                 int bytes;
316
317                 spos = custom + 17;
318
319                 bytes = strspn(spos, "0123456789abcdefABCDEF");
320                 if (!bytes || (bytes & 1))
321                         return;
322                 bytes /= 2;
323
324                 data.assoc_info.req_ies = os_malloc(bytes);
325                 if (data.assoc_info.req_ies == NULL)
326                         return;
327
328                 data.assoc_info.req_ies_len = bytes;
329                 hexstr2bin(spos, data.assoc_info.req_ies, bytes);
330
331                 spos += bytes * 2;
332
333                 data.assoc_info.resp_ies = NULL;
334                 data.assoc_info.resp_ies_len = 0;
335
336                 if (os_strncmp(spos, " RespIEs=", 9) == 0) {
337                         spos += 9;
338
339                         bytes = strspn(spos, "0123456789abcdefABCDEF");
340                         if (!bytes || (bytes & 1))
341                                 goto done;
342                         bytes /= 2;
343
344                         data.assoc_info.resp_ies = os_malloc(bytes);
345                         if (data.assoc_info.resp_ies == NULL)
346                                 goto done;
347
348                         data.assoc_info.resp_ies_len = bytes;
349                         hexstr2bin(spos, data.assoc_info.resp_ies, bytes);
350                 }
351
352                 wpa_supplicant_event(ctx, EVENT_ASSOCINFO, &data);
353
354         done:
355                 os_free(data.assoc_info.resp_ies);
356                 os_free(data.assoc_info.req_ies);
357 #ifdef CONFIG_PEERKEY
358         } else if (os_strncmp(custom, "STKSTART.request=", 17) == 0) {
359                 if (hwaddr_aton(custom + 17, data.stkstart.peer)) {
360                         wpa_printf(MSG_DEBUG, "WEXT: unrecognized "
361                                    "STKSTART.request '%s'", custom + 17);
362                         return;
363                 }
364                 wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
365 #endif /* CONFIG_PEERKEY */
366 #ifdef ANDROID
367         } else if (os_strncmp(custom, "STOP", 4) == 0) {
368                 wpa_msg(ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
369         } else if (os_strncmp(custom, "START", 5) == 0) {
370                 wpa_msg(ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
371         } else if (os_strncmp(custom, "HANG", 4) == 0) {
372                 wpa_msg(ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
373 #endif /* ANDROID */
374         }
375 }
376
377
378 static int wpa_driver_wext_event_wireless_michaelmicfailure(
379         void *ctx, const char *ev, size_t len)
380 {
381         const struct iw_michaelmicfailure *mic;
382         union wpa_event_data data;
383
384         if (len < sizeof(*mic))
385                 return -1;
386
387         mic = (const struct iw_michaelmicfailure *) ev;
388
389         wpa_printf(MSG_DEBUG, "Michael MIC failure wireless event: "
390                    "flags=0x%x src_addr=" MACSTR, mic->flags,
391                    MAC2STR(mic->src_addr.sa_data));
392
393         os_memset(&data, 0, sizeof(data));
394         data.michael_mic_failure.unicast = !(mic->flags & IW_MICFAILURE_GROUP);
395         wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
396
397         return 0;
398 }
399
400
401 static int wpa_driver_wext_event_wireless_pmkidcand(
402         struct wpa_driver_wext_data *drv, const char *ev, size_t len)
403 {
404         const struct iw_pmkid_cand *cand;
405         union wpa_event_data data;
406         const u8 *addr;
407
408         if (len < sizeof(*cand))
409                 return -1;
410
411         cand = (const struct iw_pmkid_cand *) ev;
412         addr = (const u8 *) cand->bssid.sa_data;
413
414         wpa_printf(MSG_DEBUG, "PMKID candidate wireless event: "
415                    "flags=0x%x index=%d bssid=" MACSTR, cand->flags,
416                    cand->index, MAC2STR(addr));
417
418         os_memset(&data, 0, sizeof(data));
419         os_memcpy(data.pmkid_candidate.bssid, addr, ETH_ALEN);
420         data.pmkid_candidate.index = cand->index;
421         data.pmkid_candidate.preauth = cand->flags & IW_PMKID_CAND_PREAUTH;
422         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
423
424         return 0;
425 }
426
427
428 static int wpa_driver_wext_event_wireless_assocreqie(
429         struct wpa_driver_wext_data *drv, const char *ev, int len)
430 {
431         if (len < 0)
432                 return -1;
433
434         wpa_hexdump(MSG_DEBUG, "AssocReq IE wireless event", (const u8 *) ev,
435                     len);
436         os_free(drv->assoc_req_ies);
437         drv->assoc_req_ies = os_malloc(len);
438         if (drv->assoc_req_ies == NULL) {
439                 drv->assoc_req_ies_len = 0;
440                 return -1;
441         }
442         os_memcpy(drv->assoc_req_ies, ev, len);
443         drv->assoc_req_ies_len = len;
444
445         return 0;
446 }
447
448
449 static int wpa_driver_wext_event_wireless_assocrespie(
450         struct wpa_driver_wext_data *drv, const char *ev, int len)
451 {
452         if (len < 0)
453                 return -1;
454
455         wpa_hexdump(MSG_DEBUG, "AssocResp IE wireless event", (const u8 *) ev,
456                     len);
457         os_free(drv->assoc_resp_ies);
458         drv->assoc_resp_ies = os_malloc(len);
459         if (drv->assoc_resp_ies == NULL) {
460                 drv->assoc_resp_ies_len = 0;
461                 return -1;
462         }
463         os_memcpy(drv->assoc_resp_ies, ev, len);
464         drv->assoc_resp_ies_len = len;
465
466         return 0;
467 }
468
469
470 static void wpa_driver_wext_event_assoc_ies(struct wpa_driver_wext_data *drv)
471 {
472         union wpa_event_data data;
473
474         if (drv->assoc_req_ies == NULL && drv->assoc_resp_ies == NULL)
475                 return;
476
477         os_memset(&data, 0, sizeof(data));
478         if (drv->assoc_req_ies) {
479                 data.assoc_info.req_ies = drv->assoc_req_ies;
480                 drv->assoc_req_ies = NULL;
481                 data.assoc_info.req_ies_len = drv->assoc_req_ies_len;
482         }
483         if (drv->assoc_resp_ies) {
484                 data.assoc_info.resp_ies = drv->assoc_resp_ies;
485                 drv->assoc_resp_ies = NULL;
486                 data.assoc_info.resp_ies_len = drv->assoc_resp_ies_len;
487         }
488
489         wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
490
491         os_free(data.assoc_info.req_ies);
492         os_free(data.assoc_info.resp_ies);
493 }
494
495
496 static void wpa_driver_wext_event_wireless(struct wpa_driver_wext_data *drv,
497                                            void *ctx, char *data, int len)
498 {
499         struct iw_event iwe_buf, *iwe = &iwe_buf;
500         char *pos, *end, *custom, *buf;
501
502         pos = data;
503         end = data + len;
504
505         while (pos + IW_EV_LCP_LEN <= end) {
506                 /* Event data may be unaligned, so make a local, aligned copy
507                  * before processing. */
508                 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
509                 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
510                            iwe->cmd, iwe->len);
511                 if (iwe->len <= IW_EV_LCP_LEN)
512                         return;
513
514                 custom = pos + IW_EV_POINT_LEN;
515                 if (drv->we_version_compiled > 18 &&
516                     (iwe->cmd == IWEVMICHAELMICFAILURE ||
517                      iwe->cmd == IWEVCUSTOM ||
518                      iwe->cmd == IWEVASSOCREQIE ||
519                      iwe->cmd == IWEVASSOCRESPIE ||
520                      iwe->cmd == IWEVPMKIDCAND)) {
521                         /* WE-19 removed the pointer from struct iw_point */
522                         char *dpos = (char *) &iwe_buf.u.data.length;
523                         int dlen = dpos - (char *) &iwe_buf;
524                         os_memcpy(dpos, pos + IW_EV_LCP_LEN,
525                                   sizeof(struct iw_event) - dlen);
526                 } else {
527                         os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
528                         custom += IW_EV_POINT_OFF;
529                 }
530
531                 switch (iwe->cmd) {
532                 case SIOCGIWAP:
533                         wpa_printf(MSG_DEBUG, "Wireless event: new AP: "
534                                    MACSTR,
535                                    MAC2STR((u8 *) iwe->u.ap_addr.sa_data));
536                         if (is_zero_ether_addr(
537                                     (const u8 *) iwe->u.ap_addr.sa_data) ||
538                             os_memcmp(iwe->u.ap_addr.sa_data,
539                                       "\x44\x44\x44\x44\x44\x44", ETH_ALEN) ==
540                             0) {
541                                 os_free(drv->assoc_req_ies);
542                                 drv->assoc_req_ies = NULL;
543                                 os_free(drv->assoc_resp_ies);
544                                 drv->assoc_resp_ies = NULL;
545 #ifdef ANDROID
546                                 if (!drv->skip_disconnect) {
547                                         drv->skip_disconnect = 1;
548 #endif
549                                         wpa_supplicant_event(ctx, EVENT_DISASSOC,
550                                                      NULL);
551 #ifdef ANDROID
552                                         wpa_driver_wext_disconnect(drv);
553                                 }
554 #endif
555                         
556                         } else {
557 #ifdef ANDROID
558                                 drv->skip_disconnect = 0;
559 #endif
560                                 wpa_driver_wext_event_assoc_ies(drv);
561                                 wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
562                         }
563                         break;
564                 case IWEVMICHAELMICFAILURE:
565                         if (custom + iwe->u.data.length > end) {
566                                 wpa_printf(MSG_DEBUG, "WEXT: Invalid "
567                                            "IWEVMICHAELMICFAILURE length");
568                                 return;
569                         }
570                         wpa_driver_wext_event_wireless_michaelmicfailure(
571                                 ctx, custom, iwe->u.data.length);
572                         break;
573                 case IWEVCUSTOM:
574                         if (custom + iwe->u.data.length > end) {
575                                 wpa_printf(MSG_DEBUG, "WEXT: Invalid "
576                                            "IWEVCUSTOM length");
577                                 return;
578                         }
579                         buf = os_malloc(iwe->u.data.length + 1);
580                         if (buf == NULL)
581                                 return;
582                         os_memcpy(buf, custom, iwe->u.data.length);
583                         buf[iwe->u.data.length] = '\0';
584                         wpa_driver_wext_event_wireless_custom(ctx, buf);
585                         os_free(buf);
586                         break;
587                 case SIOCGIWSCAN:
588                         drv->scan_complete_events = 1;
589                         eloop_cancel_timeout(wpa_driver_wext_scan_timeout,
590                                              drv, ctx);
591                         wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
592                         break;
593                 case IWEVASSOCREQIE:
594                         if (custom + iwe->u.data.length > end) {
595                                 wpa_printf(MSG_DEBUG, "WEXT: Invalid "
596                                            "IWEVASSOCREQIE length");
597                                 return;
598                         }
599                         wpa_driver_wext_event_wireless_assocreqie(
600                                 drv, custom, iwe->u.data.length);
601                         break;
602                 case IWEVASSOCRESPIE:
603                         if (custom + iwe->u.data.length > end) {
604                                 wpa_printf(MSG_DEBUG, "WEXT: Invalid "
605                                            "IWEVASSOCRESPIE length");
606                                 return;
607                         }
608                         wpa_driver_wext_event_wireless_assocrespie(
609                                 drv, custom, iwe->u.data.length);
610                         break;
611                 case IWEVPMKIDCAND:
612                         if (custom + iwe->u.data.length > end) {
613                                 wpa_printf(MSG_DEBUG, "WEXT: Invalid "
614                                            "IWEVPMKIDCAND length");
615                                 return;
616                         }
617                         wpa_driver_wext_event_wireless_pmkidcand(
618                                 drv, custom, iwe->u.data.length);
619                         break;
620                 }
621
622                 pos += iwe->len;
623         }
624 }
625
626
627 static void wpa_driver_wext_event_link(struct wpa_driver_wext_data *drv,
628                                        void *ctx, char *buf, size_t len,
629                                        int del)
630 {
631         union wpa_event_data event;
632
633         os_memset(&event, 0, sizeof(event));
634         if (len > sizeof(event.interface_status.ifname))
635                 len = sizeof(event.interface_status.ifname) - 1;
636         os_memcpy(event.interface_status.ifname, buf, len);
637         event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
638                 EVENT_INTERFACE_ADDED;
639
640         wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
641                    del ? "DEL" : "NEW",
642                    event.interface_status.ifname,
643                    del ? "removed" : "added");
644
645         if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
646                 if (del)
647                         drv->if_removed = 1;
648                 else
649                         drv->if_removed = 0;
650         }
651
652         wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
653 }
654
655
656 static int wpa_driver_wext_own_ifname(struct wpa_driver_wext_data *drv,
657                                       struct nlmsghdr *h)
658 {
659         struct ifinfomsg *ifi;
660         int attrlen, nlmsg_len, rta_len;
661         struct rtattr *attr;
662
663         ifi = NLMSG_DATA(h);
664
665         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
666
667         attrlen = h->nlmsg_len - nlmsg_len;
668         if (attrlen < 0)
669                 return 0;
670
671         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
672
673         rta_len = RTA_ALIGN(sizeof(struct rtattr));
674         while (RTA_OK(attr, attrlen)) {
675                 if (attr->rta_type == IFLA_IFNAME) {
676                         if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
677                             == 0)
678                                 return 1;
679                         else
680                                 break;
681                 }
682                 attr = RTA_NEXT(attr, attrlen);
683         }
684
685         return 0;
686 }
687
688
689 static int wpa_driver_wext_own_ifindex(struct wpa_driver_wext_data *drv,
690                                        int ifindex, struct nlmsghdr *h)
691 {
692         if (drv->ifindex == ifindex || drv->ifindex2 == ifindex)
693                 return 1;
694
695         if (drv->if_removed && wpa_driver_wext_own_ifname(drv, h)) {
696                 drv->ifindex = if_nametoindex(drv->ifname);
697                 wpa_printf(MSG_DEBUG, "WEXT: Update ifindex for a removed "
698                            "interface");
699                 wpa_driver_wext_finish_drv_init(drv);
700                 return 1;
701         }
702
703         return 0;
704 }
705
706
707 static void wpa_driver_wext_event_rtm_newlink(struct wpa_driver_wext_data *drv,
708                                               void *ctx, struct nlmsghdr *h,
709                                               size_t len)
710 {
711         struct ifinfomsg *ifi;
712         int attrlen, nlmsg_len, rta_len;
713         struct rtattr * attr;
714
715         if (len < sizeof(*ifi))
716                 return;
717
718         ifi = NLMSG_DATA(h);
719
720         if (!wpa_driver_wext_own_ifindex(drv, ifi->ifi_index, h)) {
721                 wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
722                            ifi->ifi_index);
723                 return;
724         }
725
726         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
727                    "(%s%s%s%s)",
728                    drv->operstate, ifi->ifi_flags,
729                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
730                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
731                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
732                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
733         /*
734          * Some drivers send the association event before the operup event--in
735          * this case, lifting operstate in wpa_driver_wext_set_operstate()
736          * fails. This will hit us when wpa_supplicant does not need to do
737          * IEEE 802.1X authentication
738          */
739         if (drv->operstate == 1 &&
740             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
741             !(ifi->ifi_flags & IFF_RUNNING))
742                 wpa_driver_wext_send_oper_ifla(drv, -1, IF_OPER_UP);
743
744         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
745
746         attrlen = h->nlmsg_len - nlmsg_len;
747         if (attrlen < 0)
748                 return;
749
750         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
751
752         rta_len = RTA_ALIGN(sizeof(struct rtattr));
753         while (RTA_OK(attr, attrlen)) {
754                 if (attr->rta_type == IFLA_WIRELESS) {
755                         wpa_driver_wext_event_wireless(
756                                 drv, ctx, ((char *) attr) + rta_len,
757                                 attr->rta_len - rta_len);
758                 } else if (attr->rta_type == IFLA_IFNAME) {
759                         wpa_driver_wext_event_link(drv, ctx,
760                                                    ((char *) attr) + rta_len,
761                                                    attr->rta_len - rta_len, 0);
762                 }
763                 attr = RTA_NEXT(attr, attrlen);
764         }
765 }
766
767
768 static void wpa_driver_wext_event_rtm_dellink(struct wpa_driver_wext_data *drv,
769                                               void *ctx, struct nlmsghdr *h,
770                                               size_t len)
771 {
772         struct ifinfomsg *ifi;
773         int attrlen, nlmsg_len, rta_len;
774         struct rtattr * attr;
775
776         if (len < sizeof(*ifi))
777                 return;
778
779         ifi = NLMSG_DATA(h);
780
781         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
782
783         attrlen = h->nlmsg_len - nlmsg_len;
784         if (attrlen < 0)
785                 return;
786
787         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
788
789         rta_len = RTA_ALIGN(sizeof(struct rtattr));
790         while (RTA_OK(attr, attrlen)) {
791                 if (attr->rta_type == IFLA_IFNAME) {
792                         wpa_driver_wext_event_link(drv,  ctx,
793                                                    ((char *) attr) + rta_len,
794                                                    attr->rta_len - rta_len, 1);
795                 }
796                 attr = RTA_NEXT(attr, attrlen);
797         }
798 }
799
800
801 static void wpa_driver_wext_event_receive(int sock, void *eloop_ctx,
802                                           void *sock_ctx)
803 {
804         char buf[8192];
805         int left;
806         struct sockaddr_nl from;
807         socklen_t fromlen;
808         struct nlmsghdr *h;
809         int max_events = 10;
810
811 try_again:
812         fromlen = sizeof(from);
813         left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
814                         (struct sockaddr *) &from, &fromlen);
815         if (left < 0) {
816                 if (errno != EINTR && errno != EAGAIN)
817                         wpa_printf(MSG_ERROR, "%s: recvfrom(netlink): %d", __func__, errno);
818                 return;
819         }
820
821         h = (struct nlmsghdr *) buf;
822         while (left >= (int) sizeof(*h)) {
823                 int len, plen;
824
825                 len = h->nlmsg_len;
826                 plen = len - sizeof(*h);
827                 if (len > left || plen < 0) {
828                         wpa_printf(MSG_DEBUG, "Malformed netlink message: "
829                                    "len=%d left=%d plen=%d",
830                                    len, left, plen);
831                         break;
832                 }
833
834                 switch (h->nlmsg_type) {
835                 case RTM_NEWLINK:
836                         wpa_driver_wext_event_rtm_newlink(eloop_ctx, sock_ctx,
837                                                           h, plen);
838                         break;
839                 case RTM_DELLINK:
840                         wpa_driver_wext_event_rtm_dellink(eloop_ctx, sock_ctx,
841                                                           h, plen);
842                         break;
843                 }
844
845                 len = NLMSG_ALIGN(len);
846                 left -= len;
847                 h = (struct nlmsghdr *) ((char *) h + len);
848         }
849
850         if (left > 0) {
851                 wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
852                            "message", left);
853         }
854
855         if (--max_events > 0) {
856                 /*
857                  * Try to receive all events in one eloop call in order to
858                  * limit race condition on cases where AssocInfo event, Assoc
859                  * event, and EAPOL frames are received more or less at the
860                  * same time. We want to process the event messages first
861                  * before starting EAPOL processing.
862                  */
863                 goto try_again;
864         }
865 }
866
867
868 static int wpa_driver_wext_get_ifflags_ifname(struct wpa_driver_wext_data *drv,
869                                               const char *ifname, int *flags)
870 {
871         struct ifreq ifr;
872
873         os_memset(&ifr, 0, sizeof(ifr));
874         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
875         if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
876                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]");
877                 return -1;
878         }
879         *flags = ifr.ifr_flags & 0xffff;
880         return 0;
881 }
882
883
884 /**
885  * wpa_driver_wext_get_ifflags - Get interface flags (SIOCGIFFLAGS)
886  * @drv: driver_wext private data
887  * @flags: Pointer to returned flags value
888  * Returns: 0 on success, -1 on failure
889  */
890 int wpa_driver_wext_get_ifflags(struct wpa_driver_wext_data *drv, int *flags)
891 {
892         return wpa_driver_wext_get_ifflags_ifname(drv, drv->ifname, flags);
893 }
894
895
896 static int wpa_driver_wext_set_ifflags_ifname(struct wpa_driver_wext_data *drv,
897                                               const char *ifname, int flags)
898 {
899         struct ifreq ifr;
900
901         os_memset(&ifr, 0, sizeof(ifr));
902         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
903         ifr.ifr_flags = flags & 0xffff;
904         if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
905                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]");
906                 return -1;
907         }
908         return 0;
909 }
910
911
912 /**
913  * wpa_driver_wext_set_ifflags - Set interface flags (SIOCSIFFLAGS)
914  * @drv: driver_wext private data
915  * @flags: New value for flags
916  * Returns: 0 on success, -1 on failure
917  */
918 int wpa_driver_wext_set_ifflags(struct wpa_driver_wext_data *drv, int flags)
919 {
920         return wpa_driver_wext_set_ifflags_ifname(drv, drv->ifname, flags);
921 }
922
923
924 /**
925  * wpa_driver_wext_init - Initialize WE driver interface
926  * @ctx: context to be used when calling wpa_supplicant functions,
927  * e.g., wpa_supplicant_event()
928  * @ifname: interface name, e.g., wlan0
929  * Returns: Pointer to private data, %NULL on failure
930  */
931 void * wpa_driver_wext_init(void *ctx, const char *ifname)
932 {
933         int s;
934         struct sockaddr_nl local;
935         struct wpa_driver_wext_data *drv;
936
937         drv = os_zalloc(sizeof(*drv));
938         if (drv == NULL)
939                 return NULL;
940         drv->ctx = ctx;
941         os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
942
943         drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
944         if (drv->ioctl_sock < 0) {
945                 wpa_printf(MSG_ERROR, "%s: socket(PF_INET,SOCK_DGRAM)", __func__);
946                 os_free(drv);
947                 return NULL;
948         }
949
950         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
951         if (s < 0) {
952                 wpa_printf(MSG_ERROR, "%s: socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)", __func__);
953                 close(drv->ioctl_sock);
954                 os_free(drv);
955                 return NULL;
956         }
957
958         os_memset(&local, 0, sizeof(local));
959         local.nl_family = AF_NETLINK;
960         local.nl_groups = RTMGRP_LINK;
961         if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
962                 wpa_printf(MSG_ERROR, "bind(netlink)");
963                 close(s);
964                 close(drv->ioctl_sock);
965                 os_free(drv);
966                 return NULL;
967         }
968
969         eloop_register_read_sock(s, wpa_driver_wext_event_receive, drv, ctx);
970         drv->event_sock = s;
971
972         drv->mlme_sock = -1;
973 #ifdef ANDROID
974         drv->errors = 0;
975         drv->driver_is_started = TRUE;
976         drv->skip_disconnect = 0;
977 #endif
978         wpa_driver_wext_finish_drv_init(drv);
979
980         return drv;
981 }
982
983
984 static void wpa_driver_wext_finish_drv_init(struct wpa_driver_wext_data *drv)
985 {
986         int flags;
987
988         if (wpa_driver_wext_get_ifflags(drv, &flags) != 0)
989                 printf("Could not get interface '%s' flags\n", drv->ifname);
990         else if (!(flags & IFF_UP)) {
991                 if (wpa_driver_wext_set_ifflags(drv, flags | IFF_UP) != 0) {
992                         printf("Could not set interface '%s' UP\n",
993                                drv->ifname);
994                 } else {
995                         /*
996                          * Wait some time to allow driver to initialize before
997                          * starting configuring the driver. This seems to be
998                          * needed at least some drivers that load firmware etc.
999                          * when the interface is set up.
1000                          */
1001                         wpa_printf(MSG_DEBUG, "Interface %s set UP - waiting "
1002                                    "a second for the driver to complete "
1003                                    "initialization", drv->ifname);
1004                         sleep(1);
1005                 }
1006         }
1007
1008         /*
1009          * Make sure that the driver does not have any obsolete PMKID entries.
1010          */
1011         wpa_driver_wext_flush_pmkid(drv);
1012
1013         if (wpa_driver_wext_set_mode(drv, 0) < 0) {
1014                 printf("Could not configure driver to use managed mode\n");
1015         }
1016
1017         wpa_driver_wext_get_range(drv);
1018
1019         /*
1020          * Unlock the driver's BSSID and force to a random SSID to clear any
1021          * previous association the driver might have when the supplicant
1022          * starts up.
1023          */
1024         wpa_driver_wext_disconnect(drv);
1025
1026         drv->ifindex = if_nametoindex(drv->ifname);
1027
1028         if (os_strncmp(drv->ifname, "wlan", 4) == 0) {
1029                 /*
1030                  * Host AP driver may use both wlan# and wifi# interface in
1031                  * wireless events. Since some of the versions included WE-18
1032                  * support, let's add the alternative ifindex also from
1033                  * driver_wext.c for the time being. This may be removed at
1034                  * some point once it is believed that old versions of the
1035                  * driver are not in use anymore.
1036                  */
1037                 char ifname2[IFNAMSIZ + 1];
1038                 os_strlcpy(ifname2, drv->ifname, sizeof(ifname2));
1039                 os_memcpy(ifname2, "wifi", 4);
1040                 wpa_driver_wext_alternative_ifindex(drv, ifname2);
1041         }
1042
1043         wpa_driver_wext_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
1044 }
1045
1046
1047 /**
1048  * wpa_driver_wext_deinit - Deinitialize WE driver interface
1049  * @priv: Pointer to private wext data from wpa_driver_wext_init()
1050  *
1051  * Shut down driver interface and processing of driver events. Free
1052  * private data buffer if one was allocated in wpa_driver_wext_init().
1053  */
1054 void wpa_driver_wext_deinit(void *priv)
1055 {
1056         struct wpa_driver_wext_data *drv = priv;
1057         int flags;
1058
1059         eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv, drv->ctx);
1060
1061         /*
1062          * Clear possibly configured driver parameters in order to make it
1063          * easier to use the driver after wpa_supplicant has been terminated.
1064          */
1065         wpa_driver_wext_disconnect(drv);
1066
1067         wpa_driver_wext_send_oper_ifla(priv, 0, IF_OPER_UP);
1068
1069         eloop_unregister_read_sock(drv->event_sock);
1070         if (drv->mlme_sock >= 0)
1071                 eloop_unregister_read_sock(drv->mlme_sock);
1072
1073         if (wpa_driver_wext_get_ifflags(drv, &flags) == 0)
1074                 (void) wpa_driver_wext_set_ifflags(drv, flags & ~IFF_UP);
1075
1076         close(drv->event_sock);
1077         close(drv->ioctl_sock);
1078         if (drv->mlme_sock >= 0)
1079                 close(drv->mlme_sock);
1080         os_free(drv->assoc_req_ies);
1081         os_free(drv->assoc_resp_ies);
1082         os_free(drv);
1083 }
1084
1085 /**
1086  * wpa_driver_wext_scan_timeout - Scan timeout to report scan completion
1087  * @eloop_ctx: Unused
1088  * @timeout_ctx: ctx argument given to wpa_driver_wext_init()
1089  *
1090  * This function can be used as registered timeout when starting a scan to
1091  * generate a scan completed event if the driver does not report this.
1092  */
1093 void wpa_driver_wext_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1094 {
1095         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1096         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1097 }
1098
1099 /**
1100  * wpa_driver_wext_set_scan_timeout - Set scan timeout to report scan completion
1101  * @priv:  Pointer to private wext data from wpa_driver_wext_init()
1102  *
1103  * This function can be used to set registered timeout when starting a scan to
1104  * generate a scan completed event if the driver does not report this.
1105  */
1106 static void wpa_driver_wext_set_scan_timeout(void *priv)
1107 {
1108         struct wpa_driver_wext_data *drv = priv;
1109         int timeout = 10; /* In case scan A and B bands it can be long */
1110
1111         /* Not all drivers generate "scan completed" wireless event, so try to
1112          * read results after a timeout. */
1113         if (drv->scan_complete_events) {
1114                 /*
1115                  * The driver seems to deliver SIOCGIWSCAN events to notify
1116                  * when scan is complete, so use longer timeout to avoid race
1117                  * conditions with scanning and following association request.
1118                  */
1119                 timeout = 30;
1120         }
1121         wpa_printf(MSG_DEBUG, "Scan requested - scan timeout %d seconds",
1122                    timeout);
1123         eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv, drv->ctx);
1124         eloop_register_timeout(timeout, 0, wpa_driver_wext_scan_timeout, drv,
1125                                drv->ctx);
1126 }
1127
1128 /**
1129  * wpa_driver_wext_scan - Request the driver to initiate scan
1130  * @priv: Pointer to private wext data from wpa_driver_wext_init()
1131  * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
1132  *      all SSIDs (either active scan with broadcast SSID or passive
1133  *      scan
1134  * @ssid_len: Length of the SSID
1135  * Returns: 0 on success, -1 on failure
1136  */
1137 int wpa_driver_wext_scan(void *priv, const u8 *ssid, size_t ssid_len)
1138 {
1139         struct wpa_driver_wext_data *drv = priv;
1140         struct iwreq iwr;
1141         int ret = 0;
1142         struct iw_scan_req req;
1143 #ifdef ANDROID
1144         struct wpa_supplicant *wpa_s = (struct wpa_supplicant *)(drv->ctx);
1145         int scan_probe_flag = 0;
1146 #endif
1147
1148         if (ssid_len > IW_ESSID_MAX_SIZE) {
1149                 wpa_printf(MSG_DEBUG, "%s: too long SSID (%lu)",
1150                            __FUNCTION__, (unsigned long) ssid_len);
1151                 return -1;
1152         }
1153
1154         os_memset(&iwr, 0, sizeof(iwr));
1155         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1156
1157 #ifdef ANDROID
1158         if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
1159                 scan_probe_flag = wpa_s->prev_scan_ssid->scan_ssid;
1160         }
1161         wpa_printf(MSG_DEBUG, "%s: specific scan = %d", __func__,
1162                 (scan_probe_flag && (ssid && ssid_len)) ? 1 : 0);
1163         if (scan_probe_flag && (ssid && ssid_len)) {
1164 #else
1165         if (ssid && ssid_len) {
1166 #endif
1167                 os_memset(&req, 0, sizeof(req));
1168                 req.essid_len = ssid_len;
1169                 req.bssid.sa_family = ARPHRD_ETHER;
1170                 os_memset(req.bssid.sa_data, 0xff, ETH_ALEN);
1171                 os_memcpy(req.essid, ssid, ssid_len);
1172                 iwr.u.data.pointer = (caddr_t) &req;
1173                 iwr.u.data.length = sizeof(req);
1174                 iwr.u.data.flags = IW_SCAN_THIS_ESSID;
1175         }
1176
1177         if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
1178                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWSCAN]");
1179                 ret = -1;
1180         }
1181
1182         wpa_driver_wext_set_scan_timeout(priv);
1183
1184         return ret;
1185 }
1186
1187 /**
1188  * wpa_driver_wext_combo_scan - Request the driver to initiate combo scan
1189  * @priv: Pointer to private wext data from wpa_driver_wext_init()
1190  * @ssid_ptr: Pointer to current SSID from configuration list or %NULL to
1191  *      scan for all SSIDs (either active scan with broadcast SSID or passive
1192  *      scan)
1193  * @ssid_conf: SSID list from the configuration
1194  * Returns: 0 on success, -1 on failure
1195  *      Also updates @ssid_ptr to next specific scan item
1196  */
1197 int wpa_driver_wext_combo_scan(void *priv, struct wpa_ssid **ssid_ptr,
1198                                struct wpa_ssid *ssid_conf)
1199 {
1200         char buf[WEXT_CSCAN_BUF_LEN];
1201         struct wpa_driver_wext_data *drv = priv;
1202         struct iwreq iwr;
1203         int ret = 0, timeout, i = 0, bp;
1204         struct wpa_ssid *ssid, *ssid_orig;
1205         u8 *ssid_nm = NULL;
1206         size_t ssid_len = 0;
1207
1208         struct wpa_supplicant *wpa_s = (struct wpa_supplicant *)(drv->ctx);
1209         int scan_probe_flag = 0;
1210
1211         if (!drv->driver_is_started) {
1212                 wpa_printf(MSG_DEBUG, "%s: Driver stopped", __func__);
1213                 return 0;
1214         }
1215
1216         wpa_printf(MSG_DEBUG, "%s: Start", __func__);
1217
1218         /* Set list of SSIDs */
1219         ssid_orig = (*ssid_ptr);
1220         ssid = (*ssid_ptr) ? (*ssid_ptr) : ssid_conf;
1221         bp = WEXT_CSCAN_HEADER_SIZE;
1222         os_memcpy(buf, WEXT_CSCAN_HEADER, bp);
1223         while (i < WEXT_CSCAN_AMOUNT) {
1224                 if ((bp + IW_ESSID_MAX_SIZE + 10) >= (int)sizeof(buf))
1225                         break;
1226                 *ssid_ptr = ssid;
1227                 if (ssid == NULL)
1228                         break;
1229                 if (!ssid->disabled && ssid->scan_ssid) {
1230                         wpa_printf(MSG_DEBUG, "For Scan: %s", ssid->ssid);
1231                         buf[bp++] = WEXT_CSCAN_SSID_SECTION;
1232                         buf[bp++] = ssid->ssid_len;
1233                         os_memcpy(&buf[bp], ssid->ssid, ssid->ssid_len);
1234                         bp += ssid->ssid_len;
1235                         i++;
1236                 }
1237                 ssid = ssid->next;
1238         }
1239
1240         /* Set list of channels */
1241         buf[bp++] = WEXT_CSCAN_CHANNEL_SECTION;
1242         buf[bp++] = 0;
1243
1244         /* Set passive dwell time (default is 250) */
1245         buf[bp++] = WEXT_CSCAN_PASV_DWELL_SECTION;
1246         buf[bp++] = (u8)WEXT_CSCAN_PASV_DWELL_TIME;
1247         buf[bp++] = (u8)(WEXT_CSCAN_PASV_DWELL_TIME >> 8);
1248
1249         /* Set home dwell time (default is 40) */
1250         buf[bp++] = WEXT_CSCAN_HOME_DWELL_SECTION;
1251         buf[bp++] = (u8)WEXT_CSCAN_HOME_DWELL_TIME;
1252         buf[bp++] = (u8)(WEXT_CSCAN_HOME_DWELL_TIME >> 8);
1253
1254         os_memset(&iwr, 0, sizeof(iwr));
1255         os_strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1256         iwr.u.data.pointer = buf;
1257         iwr.u.data.length = bp;
1258
1259         if ((ret = ioctl(drv->ioctl_sock, SIOCSIWPRIV, &iwr)) < 0) {
1260                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (cscan): %d", ret);
1261                 *ssid_ptr = ssid_orig;
1262                 /* Fall back to normal scan mode */
1263                 if (*ssid_ptr) {
1264                     ssid_nm = (*ssid_ptr)->ssid;
1265                     ssid_len = (*ssid_ptr)->ssid_len;
1266                 }
1267                 return wpa_driver_wext_scan(priv, ssid_nm, ssid_len);
1268         }
1269
1270         wpa_driver_wext_set_scan_timeout(priv);
1271
1272         return ret;
1273 }
1274
1275
1276 static u8 * wpa_driver_wext_giwscan(struct wpa_driver_wext_data *drv,
1277                                     size_t *len)
1278 {
1279         struct iwreq iwr;
1280         u8 *res_buf;
1281         size_t res_buf_len;
1282
1283         res_buf_len = IW_SCAN_MAX_DATA;
1284         for (;;) {
1285                 res_buf = os_malloc(res_buf_len);
1286                 if (res_buf == NULL)
1287                         return NULL;
1288                 os_memset(&iwr, 0, sizeof(iwr));
1289                 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1290                 iwr.u.data.pointer = res_buf;
1291                 iwr.u.data.length = res_buf_len;
1292
1293                 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
1294                         break;
1295
1296                 if (errno == E2BIG && res_buf_len < 65535) {
1297                         os_free(res_buf);
1298                         res_buf = NULL;
1299                         res_buf_len *= 2;
1300                         if (res_buf_len > 65535)
1301                                 res_buf_len = 65535; /* 16-bit length field */
1302                         wpa_printf(MSG_DEBUG, "Scan results did not fit - "
1303                                    "trying larger buffer (%lu bytes)",
1304                                    (unsigned long) res_buf_len);
1305                 } else {
1306                         wpa_printf(MSG_ERROR, "ioctl[SIOCGIWSCAN]: %d", errno);
1307                         os_free(res_buf);
1308                         return NULL;
1309                 }
1310         }
1311
1312         if (iwr.u.data.length > res_buf_len) {
1313                 os_free(res_buf);
1314                 return NULL;
1315         }
1316         *len = iwr.u.data.length;
1317
1318         return res_buf;
1319 }
1320
1321
1322 /*
1323  * Data structure for collecting WEXT scan results. This is needed to allow
1324  * the various methods of reporting IEs to be combined into a single IE buffer.
1325  */
1326 struct wext_scan_data {
1327         struct wpa_scan_res res;
1328         u8 *ie;
1329         size_t ie_len;
1330         u8 ssid[32];
1331         size_t ssid_len;
1332         int maxrate;
1333 };
1334
1335
1336 static void wext_get_scan_mode(struct iw_event *iwe,
1337                                struct wext_scan_data *res)
1338 {
1339         if (iwe->u.mode == IW_MODE_ADHOC)
1340                 res->res.caps |= IEEE80211_CAP_IBSS;
1341         else if (iwe->u.mode == IW_MODE_MASTER || iwe->u.mode == IW_MODE_INFRA)
1342                 res->res.caps |= IEEE80211_CAP_ESS;
1343 }
1344
1345
1346 static void wext_get_scan_ssid(struct iw_event *iwe,
1347                                struct wext_scan_data *res, char *custom,
1348                                char *end)
1349 {
1350         int ssid_len = iwe->u.essid.length;
1351         if (custom + ssid_len > end)
1352                 return;
1353         if (iwe->u.essid.flags &&
1354             ssid_len > 0 &&
1355             ssid_len <= IW_ESSID_MAX_SIZE) {
1356                 os_memcpy(res->ssid, custom, ssid_len);
1357                 res->ssid_len = ssid_len;
1358         }
1359 }
1360
1361
1362 static void wext_get_scan_freq(struct iw_event *iwe,
1363                                struct wext_scan_data *res)
1364 {
1365         int divi = 1000000, i;
1366
1367         if (iwe->u.freq.e == 0) {
1368                 /*
1369                  * Some drivers do not report frequency, but a channel.
1370                  * Try to map this to frequency by assuming they are using
1371                  * IEEE 802.11b/g.  But don't overwrite a previously parsed
1372                  * frequency if the driver sends both frequency and channel,
1373                  * since the driver may be sending an A-band channel that we
1374                  * don't handle here.
1375                  */
1376
1377                 if (res->res.freq)
1378                         return;
1379
1380                 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
1381                         res->res.freq = 2407 + 5 * iwe->u.freq.m;
1382                         return;
1383                 } else if (iwe->u.freq.m == 14) {
1384                         res->res.freq = 2484;
1385                         return;
1386                 }
1387         }
1388
1389         if (iwe->u.freq.e > 6) {
1390                 wpa_printf(MSG_DEBUG, "Invalid freq in scan results (BSSID="
1391                            MACSTR " m=%d e=%d)",
1392                            MAC2STR(res->res.bssid), iwe->u.freq.m,
1393                            iwe->u.freq.e);
1394                 return;
1395         }
1396
1397         for (i = 0; i < iwe->u.freq.e; i++)
1398                 divi /= 10;
1399         res->res.freq = iwe->u.freq.m / divi;
1400 }
1401
1402
1403 static void wext_get_scan_qual(struct iw_event *iwe,
1404                                struct wext_scan_data *res)
1405 {
1406         res->res.qual = iwe->u.qual.qual;
1407         res->res.noise = iwe->u.qual.noise;
1408         res->res.level = iwe->u.qual.level;
1409 }
1410
1411
1412 static void wext_get_scan_encode(struct iw_event *iwe,
1413                                  struct wext_scan_data *res)
1414 {
1415         if (!(iwe->u.data.flags & IW_ENCODE_DISABLED))
1416                 res->res.caps |= IEEE80211_CAP_PRIVACY;
1417 }
1418
1419
1420 static void wext_get_scan_rate(struct iw_event *iwe,
1421                                struct wext_scan_data *res, char *pos,
1422                                char *end)
1423 {
1424         int maxrate;
1425         char *custom = pos + IW_EV_LCP_LEN;
1426         struct iw_param p;
1427         size_t clen;
1428
1429         clen = iwe->len;
1430         if (custom + clen > end)
1431                 return;
1432         maxrate = 0;
1433         while (((ssize_t) clen) >= (ssize_t) sizeof(struct iw_param)) {
1434                 /* Note: may be misaligned, make a local, aligned copy */
1435                 os_memcpy(&p, custom, sizeof(struct iw_param));
1436                 if (p.value > maxrate)
1437                         maxrate = p.value;
1438                 clen -= sizeof(struct iw_param);
1439                 custom += sizeof(struct iw_param);
1440         }
1441
1442         /* Convert the maxrate from WE-style (b/s units) to
1443          * 802.11 rates (500000 b/s units).
1444          */
1445         res->maxrate = maxrate / 500000;
1446 }
1447
1448
1449 static void wext_get_scan_iwevgenie(struct iw_event *iwe,
1450                                     struct wext_scan_data *res, char *custom,
1451                                     char *end)
1452 {
1453         char *genie, *gpos, *gend;
1454         u8 *tmp;
1455
1456         if (iwe->u.data.length == 0)
1457                 return;
1458
1459         gpos = genie = custom;
1460         gend = genie + iwe->u.data.length;
1461         if (gend > end) {
1462                 wpa_printf(MSG_INFO, "IWEVGENIE overflow");
1463                 return;
1464         }
1465
1466         tmp = os_realloc(res->ie, res->ie_len + gend - gpos);
1467         if (tmp == NULL)
1468                 return;
1469         os_memcpy(tmp + res->ie_len, gpos, gend - gpos);
1470         res->ie = tmp;
1471         res->ie_len += gend - gpos;
1472 }
1473
1474
1475 static void wext_get_scan_custom(struct iw_event *iwe,
1476                                  struct wext_scan_data *res, char *custom,
1477                                  char *end)
1478 {
1479         size_t clen;
1480         u8 *tmp;
1481
1482         clen = iwe->u.data.length;
1483         if (custom + clen > end)
1484                 return;
1485
1486         if (clen > 7 && os_strncmp(custom, "wpa_ie=", 7) == 0) {
1487                 char *spos;
1488                 int bytes;
1489                 spos = custom + 7;
1490                 bytes = custom + clen - spos;
1491                 if (bytes & 1 || bytes == 0)
1492                         return;
1493                 bytes /= 2;
1494                 tmp = os_realloc(res->ie, res->ie_len + bytes);
1495                 if (tmp == NULL)
1496                         return;
1497                 hexstr2bin(spos, tmp + res->ie_len, bytes);
1498                 res->ie = tmp;
1499                 res->ie_len += bytes;
1500         } else if (clen > 7 && os_strncmp(custom, "rsn_ie=", 7) == 0) {
1501                 char *spos;
1502                 int bytes;
1503                 spos = custom + 7;
1504                 bytes = custom + clen - spos;
1505                 if (bytes & 1 || bytes == 0)
1506                         return;
1507                 bytes /= 2;
1508                 tmp = os_realloc(res->ie, res->ie_len + bytes);
1509                 if (tmp == NULL)
1510                         return;
1511                 hexstr2bin(spos, tmp + res->ie_len, bytes);
1512                 res->ie = tmp;
1513                 res->ie_len += bytes;
1514         } else if (clen > 4 && os_strncmp(custom, "tsf=", 4) == 0) {
1515                 char *spos;
1516                 int bytes;
1517                 u8 bin[8];
1518                 spos = custom + 4;
1519                 bytes = custom + clen - spos;
1520                 if (bytes != 16) {
1521                         wpa_printf(MSG_INFO, "Invalid TSF length (%d)", bytes);
1522                         return;
1523                 }
1524                 bytes /= 2;
1525                 hexstr2bin(spos, bin, bytes);
1526                 res->res.tsf += WPA_GET_BE64(bin);
1527         }
1528 }
1529
1530
1531 static int wext_19_iw_point(struct wpa_driver_wext_data *drv, u16 cmd)
1532 {
1533         return drv->we_version_compiled > 18 &&
1534                 (cmd == SIOCGIWESSID || cmd == SIOCGIWENCODE ||
1535                  cmd == IWEVGENIE || cmd == IWEVCUSTOM);
1536 }
1537
1538
1539 static void wpa_driver_wext_add_scan_entry(struct wpa_scan_results *res,
1540                                            struct wext_scan_data *data)
1541 {
1542         struct wpa_scan_res **tmp;
1543         struct wpa_scan_res *r;
1544         size_t extra_len;
1545         u8 *pos, *end, *ssid_ie = NULL, *rate_ie = NULL;
1546
1547         /* Figure out whether we need to fake any IEs */
1548         pos = data->ie;
1549         end = pos + data->ie_len;
1550         while (pos && pos + 1 < end) {
1551                 if (pos + 2 + pos[1] > end)
1552                         break;
1553                 if (pos[0] == WLAN_EID_SSID)
1554                         ssid_ie = pos;
1555                 else if (pos[0] == WLAN_EID_SUPP_RATES)
1556                         rate_ie = pos;
1557                 else if (pos[0] == WLAN_EID_EXT_SUPP_RATES)
1558                         rate_ie = pos;
1559                 pos += 2 + pos[1];
1560         }
1561
1562         extra_len = 0;
1563         if (ssid_ie == NULL)
1564                 extra_len += 2 + data->ssid_len;
1565         if (rate_ie == NULL && data->maxrate)
1566                 extra_len += 3;
1567
1568         r = os_zalloc(sizeof(*r) + extra_len + data->ie_len);
1569         if (r == NULL)
1570                 return;
1571         os_memcpy(r, &data->res, sizeof(*r));
1572         r->ie_len = extra_len + data->ie_len;
1573         pos = (u8 *) (r + 1);
1574         if (ssid_ie == NULL) {
1575                 /*
1576                  * Generate a fake SSID IE since the driver did not report
1577                  * a full IE list.
1578                  */
1579                 *pos++ = WLAN_EID_SSID;
1580                 *pos++ = data->ssid_len;
1581                 os_memcpy(pos, data->ssid, data->ssid_len);
1582                 pos += data->ssid_len;
1583         }
1584         if (rate_ie == NULL && data->maxrate) {
1585                 /*
1586                  * Generate a fake Supported Rates IE since the driver did not
1587                  * report a full IE list.
1588                  */
1589                 *pos++ = WLAN_EID_SUPP_RATES;
1590                 *pos++ = 1;
1591                 *pos++ = data->maxrate;
1592         }
1593         if (data->ie)
1594                 os_memcpy(pos, data->ie, data->ie_len);
1595
1596         tmp = os_realloc(res->res,
1597                          (res->num + 1) * sizeof(struct wpa_scan_res *));
1598         if (tmp == NULL) {
1599                 os_free(r);
1600                 return;
1601         }
1602         tmp[res->num++] = r;
1603         res->res = tmp;
1604 }
1605                                       
1606
1607 /**
1608  * wpa_driver_wext_get_scan_results - Fetch the latest scan results
1609  * @priv: Pointer to private wext data from wpa_driver_wext_init()
1610  * Returns: Scan results on success, -1 on failure
1611  */
1612 struct wpa_scan_results * wpa_driver_wext_get_scan_results(void *priv)
1613 {
1614         struct wpa_driver_wext_data *drv = priv;
1615         size_t ap_num = 0, len;
1616         int first;
1617         u8 *res_buf;
1618         struct iw_event iwe_buf, *iwe = &iwe_buf;
1619         char *pos, *end, *custom;
1620         struct wpa_scan_results *res;
1621         struct wext_scan_data data;
1622
1623 #ifdef ANDROID
1624         /* To make sure correctly parse scan results which is impacted by wext
1625          * version, first check range->we_version, if it is default value (0),
1626          * update again here */
1627         if (drv->we_version_compiled == 0)
1628                 wpa_driver_wext_get_range(drv);
1629 #endif
1630
1631         res_buf = wpa_driver_wext_giwscan(drv, &len);
1632         if (res_buf == NULL)
1633                 return NULL;
1634
1635         ap_num = 0;
1636         first = 1;
1637
1638         res = os_zalloc(sizeof(*res));
1639         if (res == NULL) {
1640                 os_free(res_buf);
1641                 return NULL;
1642         }
1643
1644         pos = (char *) res_buf;
1645         end = (char *) res_buf + len;
1646         os_memset(&data, 0, sizeof(data));
1647
1648         while (pos + IW_EV_LCP_LEN <= end) {
1649                 /* Event data may be unaligned, so make a local, aligned copy
1650                  * before processing. */
1651                 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1652                 if (iwe->len <= IW_EV_LCP_LEN)
1653                         break;
1654
1655                 custom = pos + IW_EV_POINT_LEN;
1656                 if (wext_19_iw_point(drv, iwe->cmd)) {
1657                         /* WE-19 removed the pointer from struct iw_point */
1658                         char *dpos = (char *) &iwe_buf.u.data.length;
1659                         int dlen = dpos - (char *) &iwe_buf;
1660                         os_memcpy(dpos, pos + IW_EV_LCP_LEN,
1661                                   sizeof(struct iw_event) - dlen);
1662                 } else {
1663                         os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1664                         custom += IW_EV_POINT_OFF;
1665                 }
1666
1667                 switch (iwe->cmd) {
1668                 case SIOCGIWAP:
1669                         if (!first)
1670                                 wpa_driver_wext_add_scan_entry(res, &data);
1671                         first = 0;
1672                         os_free(data.ie);
1673                         os_memset(&data, 0, sizeof(data));
1674                         os_memcpy(data.res.bssid,
1675                                   iwe->u.ap_addr.sa_data, ETH_ALEN);
1676                         break;
1677                 case SIOCGIWMODE:
1678                         wext_get_scan_mode(iwe, &data);
1679                         break;
1680                 case SIOCGIWESSID:
1681                         wext_get_scan_ssid(iwe, &data, custom, end);
1682                         break;
1683                 case SIOCGIWFREQ:
1684                         wext_get_scan_freq(iwe, &data);
1685                         break;
1686                 case IWEVQUAL:
1687                         wext_get_scan_qual(iwe, &data);
1688                         break;
1689                 case SIOCGIWENCODE:
1690                         wext_get_scan_encode(iwe, &data);
1691                         break;
1692                 case SIOCGIWRATE:
1693                         wext_get_scan_rate(iwe, &data, pos, end);
1694                         break;
1695                 case IWEVGENIE:
1696                         wext_get_scan_iwevgenie(iwe, &data, custom, end);
1697                         break;
1698                 case IWEVCUSTOM:
1699                         wext_get_scan_custom(iwe, &data, custom, end);
1700                         break;
1701                 }
1702
1703                 pos += iwe->len;
1704         }
1705         os_free(res_buf);
1706         res_buf = NULL;
1707         if (!first)
1708                 wpa_driver_wext_add_scan_entry(res, &data);
1709         os_free(data.ie);
1710
1711         wpa_printf(MSG_DEBUG, "Received %lu bytes of scan results (%lu BSSes)",
1712                    (unsigned long) len, (unsigned long) res->num);
1713
1714         return res;
1715 }
1716
1717
1718 static int wpa_driver_wext_get_range(void *priv)
1719 {
1720         struct wpa_driver_wext_data *drv = priv;
1721         struct iw_range *range;
1722         struct iwreq iwr;
1723         int minlen;
1724         size_t buflen;
1725
1726         /*
1727          * Use larger buffer than struct iw_range in order to allow the
1728          * structure to grow in the future.
1729          */
1730         buflen = sizeof(struct iw_range) + 500;
1731         range = os_zalloc(buflen);
1732         if (range == NULL)
1733                 return -1;
1734
1735         os_memset(&iwr, 0, sizeof(iwr));
1736         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1737         iwr.u.data.pointer = (caddr_t) range;
1738         iwr.u.data.length = buflen;
1739
1740         minlen = ((char *) &range->enc_capa) - (char *) range +
1741                 sizeof(range->enc_capa);
1742
1743         if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1744                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIRANGE]");
1745                 os_free(range);
1746                 return -1;
1747         } else if (iwr.u.data.length >= minlen &&
1748                    range->we_version_compiled >= 18) {
1749                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1750                            "WE(source)=%d enc_capa=0x%x",
1751                            range->we_version_compiled,
1752                            range->we_version_source,
1753                            range->enc_capa);
1754                 drv->has_capability = 1;
1755                 drv->we_version_compiled = range->we_version_compiled;
1756                 if (range->enc_capa & IW_ENC_CAPA_WPA) {
1757                         drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1758                                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1759                 }
1760                 if (range->enc_capa & IW_ENC_CAPA_WPA2) {
1761                         drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1762                                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1763                 }
1764                 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1765                         WPA_DRIVER_CAPA_ENC_WEP104;
1766                 if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP)
1767                         drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1768                 if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP)
1769                         drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1770                 if (range->enc_capa & IW_ENC_CAPA_4WAY_HANDSHAKE)
1771                         drv->capa.flags |= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;
1772                 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1773                         WPA_DRIVER_AUTH_SHARED |
1774                         WPA_DRIVER_AUTH_LEAP;
1775
1776                 wpa_printf(MSG_DEBUG, "  capabilities: key_mgmt 0x%x enc 0x%x "
1777                            "flags 0x%x",
1778                            drv->capa.key_mgmt, drv->capa.enc, drv->capa.flags);
1779         } else {
1780                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: too old (short) data - "
1781                            "assuming WPA is not supported");
1782         }
1783
1784         os_free(range);
1785         return 0;
1786 }
1787
1788
1789 static int wpa_driver_wext_set_wpa(void *priv, int enabled)
1790 {
1791         struct wpa_driver_wext_data *drv = priv;
1792         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1793
1794         return wpa_driver_wext_set_auth_param(drv, IW_AUTH_WPA_ENABLED,
1795                                               enabled);
1796 }
1797
1798
1799 static int wpa_driver_wext_set_psk(struct wpa_driver_wext_data *drv,
1800                                    const u8 *psk)
1801 {
1802         struct iw_encode_ext *ext;
1803         struct iwreq iwr;
1804         int ret;
1805
1806         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1807
1808         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1809                 return 0;
1810
1811         if (!psk)
1812                 return 0;
1813
1814         os_memset(&iwr, 0, sizeof(iwr));
1815         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1816
1817         ext = os_zalloc(sizeof(*ext) + PMK_LEN);
1818         if (ext == NULL)
1819                 return -1;
1820
1821         iwr.u.encoding.pointer = (caddr_t) ext;
1822         iwr.u.encoding.length = sizeof(*ext) + PMK_LEN;
1823         ext->key_len = PMK_LEN;
1824         os_memcpy(&ext->key, psk, ext->key_len);
1825         ext->alg = IW_ENCODE_ALG_PMK;
1826
1827         ret = ioctl(drv->ioctl_sock, SIOCSIWENCODEEXT, &iwr);
1828         if (ret < 0)
1829                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWENCODEEXT] PMK");
1830         os_free(ext);
1831
1832         return ret;
1833 }
1834
1835
1836 static int wpa_driver_wext_set_key_ext(void *priv, wpa_alg alg,
1837                                        const u8 *addr, int key_idx,
1838                                        int set_tx, const u8 *seq,
1839                                        size_t seq_len,
1840                                        const u8 *key, size_t key_len)
1841 {
1842         struct wpa_driver_wext_data *drv = priv;
1843         struct iwreq iwr;
1844         int ret = 0;
1845         struct iw_encode_ext *ext;
1846
1847         if (seq_len > IW_ENCODE_SEQ_MAX_SIZE) {
1848                 wpa_printf(MSG_DEBUG, "%s: Invalid seq_len %lu",
1849                            __FUNCTION__, (unsigned long) seq_len);
1850                 return -1;
1851         }
1852
1853         ext = os_zalloc(sizeof(*ext) + key_len);
1854         if (ext == NULL)
1855                 return -1;
1856         os_memset(&iwr, 0, sizeof(iwr));
1857         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1858         iwr.u.encoding.flags = key_idx + 1;
1859         iwr.u.encoding.flags |= IW_ENCODE_TEMP;
1860         if (alg == WPA_ALG_NONE)
1861                 iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
1862         iwr.u.encoding.pointer = (caddr_t) ext;
1863         iwr.u.encoding.length = sizeof(*ext) + key_len;
1864
1865         if (addr == NULL ||
1866             os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
1867                 ext->ext_flags |= IW_ENCODE_EXT_GROUP_KEY;
1868         if (set_tx)
1869                 ext->ext_flags |= IW_ENCODE_EXT_SET_TX_KEY;
1870
1871         ext->addr.sa_family = ARPHRD_ETHER;
1872         if (addr)
1873                 os_memcpy(ext->addr.sa_data, addr, ETH_ALEN);
1874         else
1875                 os_memset(ext->addr.sa_data, 0xff, ETH_ALEN);
1876         if (key && key_len) {
1877                 os_memcpy(ext + 1, key, key_len);
1878                 ext->key_len = key_len;
1879         }
1880         switch (alg) {
1881         case WPA_ALG_NONE:
1882                 ext->alg = IW_ENCODE_ALG_NONE;
1883                 break;
1884         case WPA_ALG_WEP:
1885                 ext->alg = IW_ENCODE_ALG_WEP;
1886                 break;
1887         case WPA_ALG_TKIP:
1888                 ext->alg = IW_ENCODE_ALG_TKIP;
1889                 break;
1890         case WPA_ALG_CCMP:
1891                 ext->alg = IW_ENCODE_ALG_CCMP;
1892                 break;
1893         case WPA_ALG_PMK:
1894                 ext->alg = IW_ENCODE_ALG_PMK;
1895                 break;
1896 #ifdef CONFIG_IEEE80211W
1897         case WPA_ALG_IGTK:
1898                 ext->alg = IW_ENCODE_ALG_AES_CMAC;
1899                 break;
1900 #endif /* CONFIG_IEEE80211W */
1901         default:
1902                 wpa_printf(MSG_DEBUG, "%s: Unknown algorithm %d",
1903                            __FUNCTION__, alg);
1904                 os_free(ext);
1905                 return -1;
1906         }
1907
1908         if (seq && seq_len) {
1909                 ext->ext_flags |= IW_ENCODE_EXT_RX_SEQ_VALID;
1910                 os_memcpy(ext->rx_seq, seq, seq_len);
1911         }
1912
1913         if (ioctl(drv->ioctl_sock, SIOCSIWENCODEEXT, &iwr) < 0) {
1914                 ret = errno == EOPNOTSUPP ? -2 : -1;
1915                 if (errno == ENODEV) {
1916                         /*
1917                          * ndiswrapper seems to be returning incorrect error
1918                          * code.. */
1919                         ret = -2;
1920                 }
1921
1922                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWENCODEEXT]");
1923         }
1924
1925         os_free(ext);
1926         return ret;
1927 }
1928
1929
1930 /**
1931  * wpa_driver_wext_set_key - Configure encryption key
1932  * @priv: Pointer to private wext data from wpa_driver_wext_init()
1933  * @priv: Private driver interface data
1934  * @alg: Encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
1935  *      %WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
1936  * @addr: Address of the peer STA or ff:ff:ff:ff:ff:ff for
1937  *      broadcast/default keys
1938  * @key_idx: key index (0..3), usually 0 for unicast keys
1939  * @set_tx: Configure this key as the default Tx key (only used when
1940  *      driver does not support separate unicast/individual key
1941  * @seq: Sequence number/packet number, seq_len octets, the next
1942  *      packet number to be used for in replay protection; configured
1943  *      for Rx keys (in most cases, this is only used with broadcast
1944  *      keys and set to zero for unicast keys)
1945  * @seq_len: Length of the seq, depends on the algorithm:
1946  *      TKIP: 6 octets, CCMP: 6 octets
1947  * @key: Key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
1948  *      8-byte Rx Mic Key
1949  * @key_len: Length of the key buffer in octets (WEP: 5 or 13,
1950  *      TKIP: 32, CCMP: 16)
1951  * Returns: 0 on success, -1 on failure
1952  *
1953  * This function uses SIOCSIWENCODEEXT by default, but tries to use
1954  * SIOCSIWENCODE if the extended ioctl fails when configuring a WEP key.
1955  */
1956 int wpa_driver_wext_set_key(void *priv, wpa_alg alg,
1957                             const u8 *addr, int key_idx,
1958                             int set_tx, const u8 *seq, size_t seq_len,
1959                             const u8 *key, size_t key_len)
1960 {
1961         struct wpa_driver_wext_data *drv = priv;
1962         struct iwreq iwr;
1963         int ret = 0;
1964
1965         wpa_printf(MSG_DEBUG, "%s: alg=%d key_idx=%d set_tx=%d seq_len=%lu "
1966                    "key_len=%lu",
1967                    __FUNCTION__, alg, key_idx, set_tx,
1968                    (unsigned long) seq_len, (unsigned long) key_len);
1969
1970         ret = wpa_driver_wext_set_key_ext(drv, alg, addr, key_idx, set_tx,
1971                                           seq, seq_len, key, key_len);
1972         if (ret == 0)
1973                 return 0;
1974
1975         if (ret == -2 &&
1976             (alg == WPA_ALG_NONE || alg == WPA_ALG_WEP)) {
1977                 wpa_printf(MSG_DEBUG, "Driver did not support "
1978                            "SIOCSIWENCODEEXT, trying SIOCSIWENCODE");
1979                 ret = 0;
1980         } else {
1981                 wpa_printf(MSG_DEBUG, "Driver did not support "
1982                            "SIOCSIWENCODEEXT");
1983                 return ret;
1984         }
1985
1986         os_memset(&iwr, 0, sizeof(iwr));
1987         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1988         iwr.u.encoding.flags = key_idx + 1;
1989         iwr.u.encoding.flags |= IW_ENCODE_TEMP;
1990         if (alg == WPA_ALG_NONE)
1991                 iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
1992         iwr.u.encoding.pointer = (caddr_t) key;
1993         iwr.u.encoding.length = key_len;
1994
1995         if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
1996                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWENCODE]");
1997                 ret = -1;
1998         }
1999
2000         if (set_tx && alg != WPA_ALG_NONE) {
2001                 os_memset(&iwr, 0, sizeof(iwr));
2002                 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2003                 iwr.u.encoding.flags = key_idx + 1;
2004                 iwr.u.encoding.flags |= IW_ENCODE_TEMP;
2005                 iwr.u.encoding.pointer = (caddr_t) NULL;
2006                 iwr.u.encoding.length = 0;
2007                 if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
2008                         wpa_printf(MSG_ERROR, "ioctl[SIOCSIWENCODE] (set_tx)");
2009                         ret = -1;
2010                 }
2011         }
2012
2013         return ret;
2014 }
2015
2016
2017 static int wpa_driver_wext_set_countermeasures(void *priv,
2018                                                int enabled)
2019 {
2020         struct wpa_driver_wext_data *drv = priv;
2021         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
2022         return wpa_driver_wext_set_auth_param(drv,
2023                                               IW_AUTH_TKIP_COUNTERMEASURES,
2024                                               enabled);
2025 }
2026
2027
2028 static int wpa_driver_wext_set_drop_unencrypted(void *priv,
2029                                                 int enabled)
2030 {
2031         struct wpa_driver_wext_data *drv = priv;
2032         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
2033         drv->use_crypt = enabled;
2034         return wpa_driver_wext_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
2035                                               enabled);
2036 }
2037
2038
2039 static int wpa_driver_wext_mlme(struct wpa_driver_wext_data *drv,
2040                                 const u8 *addr, int cmd, int reason_code)
2041 {
2042         struct iwreq iwr;
2043         struct iw_mlme mlme;
2044         int ret = 0;
2045
2046         os_memset(&iwr, 0, sizeof(iwr));
2047         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2048         os_memset(&mlme, 0, sizeof(mlme));
2049         mlme.cmd = cmd;
2050         mlme.reason_code = reason_code;
2051         mlme.addr.sa_family = ARPHRD_ETHER;
2052         os_memcpy(mlme.addr.sa_data, addr, ETH_ALEN);
2053         iwr.u.data.pointer = (caddr_t) &mlme;
2054         iwr.u.data.length = sizeof(mlme);
2055
2056         if (ioctl(drv->ioctl_sock, SIOCSIWMLME, &iwr) < 0) {
2057                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWMLME]");
2058                 ret = -1;
2059         }
2060
2061         return ret;
2062 }
2063
2064
2065 static void wpa_driver_wext_disconnect(struct wpa_driver_wext_data *drv)
2066 {
2067         struct iwreq iwr;
2068         const u8 null_bssid[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
2069 #ifndef ANDROID
2070         u8 ssid[32];
2071         int i;
2072 #endif
2073
2074         /*
2075          * Only force-disconnect when the card is in infrastructure mode,
2076          * otherwise the driver might interpret the cleared BSSID and random
2077          * SSID as an attempt to create a new ad-hoc network.
2078          */
2079         os_memset(&iwr, 0, sizeof(iwr));
2080         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2081         if (ioctl(drv->ioctl_sock, SIOCGIWMODE, &iwr) < 0) {
2082                 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWMODE]");
2083                 iwr.u.mode = IW_MODE_INFRA;
2084         }
2085
2086         if (iwr.u.mode == IW_MODE_INFRA) {
2087                 /*
2088                  * Clear the BSSID selection and set a random SSID to make sure
2089                  * the driver will not be trying to associate with something
2090                  * even if it does not understand SIOCSIWMLME commands (or
2091                  * tries to associate automatically after deauth/disassoc).
2092                  */
2093                 wpa_driver_wext_set_bssid(drv, null_bssid);
2094 #ifndef ANDROID
2095                 for (i = 0; i < 32; i++)
2096                         ssid[i] = rand() & 0xFF;
2097                 wpa_driver_wext_set_ssid(drv, ssid, 32);
2098 #endif
2099         }
2100 }
2101
2102
2103 static int wpa_driver_wext_deauthenticate(void *priv, const u8 *addr,
2104                                           int reason_code)
2105 {
2106         struct wpa_driver_wext_data *drv = priv;
2107         int ret;
2108         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
2109         ret = wpa_driver_wext_mlme(drv, addr, IW_MLME_DEAUTH, reason_code);
2110         wpa_driver_wext_disconnect(drv);
2111         return ret;
2112 }
2113
2114
2115 static int wpa_driver_wext_disassociate(void *priv, const u8 *addr,
2116                                         int reason_code)
2117 {
2118         struct wpa_driver_wext_data *drv = priv;
2119         int ret;
2120         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
2121         ret = wpa_driver_wext_mlme(drv, addr, IW_MLME_DISASSOC, reason_code);
2122         wpa_driver_wext_disconnect(drv);
2123         return ret;
2124 }
2125
2126
2127 static int wpa_driver_wext_set_gen_ie(void *priv, const u8 *ie,
2128                                       size_t ie_len)
2129 {
2130         struct wpa_driver_wext_data *drv = priv;
2131         struct iwreq iwr;
2132         int ret = 0;
2133
2134         os_memset(&iwr, 0, sizeof(iwr));
2135         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2136         iwr.u.data.pointer = (caddr_t) ie;
2137         iwr.u.data.length = ie_len;
2138
2139         if (ioctl(drv->ioctl_sock, SIOCSIWGENIE, &iwr) < 0) {
2140                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWGENIE]");
2141                 ret = -1;
2142         }
2143
2144         return ret;
2145 }
2146
2147
2148 int wpa_driver_wext_cipher2wext(int cipher)
2149 {
2150         switch (cipher) {
2151         case CIPHER_NONE:
2152                 return IW_AUTH_CIPHER_NONE;
2153         case CIPHER_WEP40:
2154                 return IW_AUTH_CIPHER_WEP40;
2155         case CIPHER_TKIP:
2156                 return IW_AUTH_CIPHER_TKIP;
2157         case CIPHER_CCMP:
2158                 return IW_AUTH_CIPHER_CCMP;
2159         case CIPHER_WEP104:
2160                 return IW_AUTH_CIPHER_WEP104;
2161         default:
2162                 return 0;
2163         }
2164 }
2165
2166
2167 int wpa_driver_wext_keymgmt2wext(int keymgmt)
2168 {
2169         switch (keymgmt) {
2170         case KEY_MGMT_802_1X:
2171         case KEY_MGMT_802_1X_NO_WPA:
2172                 return IW_AUTH_KEY_MGMT_802_1X;
2173         case KEY_MGMT_PSK:
2174                 return IW_AUTH_KEY_MGMT_PSK;
2175         default:
2176                 return 0;
2177         }
2178 }
2179
2180
2181 static int
2182 wpa_driver_wext_auth_alg_fallback(struct wpa_driver_wext_data *drv,
2183                                   struct wpa_driver_associate_params *params)
2184 {
2185         struct iwreq iwr;
2186         int ret = 0;
2187
2188         wpa_printf(MSG_DEBUG, "WEXT: Driver did not support "
2189                    "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
2190
2191         os_memset(&iwr, 0, sizeof(iwr));
2192         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2193         /* Just changing mode, not actual keys */
2194         iwr.u.encoding.flags = 0;
2195         iwr.u.encoding.pointer = (caddr_t) NULL;
2196         iwr.u.encoding.length = 0;
2197
2198         /*
2199          * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
2200          * different things. Here they are used to indicate Open System vs.
2201          * Shared Key authentication algorithm. However, some drivers may use
2202          * them to select between open/restricted WEP encrypted (open = allow
2203          * both unencrypted and encrypted frames; restricted = only allow
2204          * encrypted frames).
2205          */
2206