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