Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / drivers / driver_nl80211.c
blob777b09745e04195022270010b8c9adfa4fd2f0ae
1 /*
2 * WPA Supplicant - driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <sys/ioctl.h>
17 #include <net/if_arp.h>
18 #include <netlink/genl/genl.h>
19 #include <netlink/genl/family.h>
20 #include <netlink/genl/ctrl.h>
21 #include <linux/nl80211.h>
23 #include "wireless_copy.h"
24 #include "common.h"
25 #include "driver.h"
26 #include "eloop.h"
27 #include "ieee802_11_defs.h"
29 #ifndef IFF_LOWER_UP
30 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
31 #endif
32 #ifndef IFF_DORMANT
33 #define IFF_DORMANT 0x20000 /* driver signals dormant */
34 #endif
36 #ifndef IF_OPER_DORMANT
37 #define IF_OPER_DORMANT 5
38 #endif
39 #ifndef IF_OPER_UP
40 #define IF_OPER_UP 6
41 #endif
44 struct wpa_driver_nl80211_data {
45 void *ctx;
46 int event_sock;
47 int ioctl_sock;
48 char ifname[IFNAMSIZ + 1];
49 int ifindex;
50 u8 *assoc_req_ies;
51 size_t assoc_req_ies_len;
52 u8 *assoc_resp_ies;
53 size_t assoc_resp_ies_len;
54 struct wpa_driver_capa capa;
55 int has_capability;
56 int we_version_compiled;
58 /* for set_auth_alg fallback */
59 int use_crypt;
60 int auth_alg_fallback;
62 int operstate;
64 char mlmedev[IFNAMSIZ + 1];
66 int scan_complete_events;
68 struct nl_handle *nl_handle;
69 struct nl_cache *nl_cache;
70 struct nl_cb *nl_cb;
71 struct genl_family *nl80211;
75 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
76 void *timeout_ctx);
77 static int wpa_driver_nl80211_set_mode(void *priv, int mode);
78 static int wpa_driver_nl80211_flush_pmkid(void *priv);
79 static int wpa_driver_nl80211_get_range(void *priv);
81 static int wpa_driver_nl80211_send_oper_ifla(
82 struct wpa_driver_nl80211_data *drv,
83 int linkmode, int operstate)
85 struct {
86 struct nlmsghdr hdr;
87 struct ifinfomsg ifinfo;
88 char opts[16];
89 } req;
90 struct rtattr *rta;
91 static int nl_seq;
92 ssize_t ret;
94 os_memset(&req, 0, sizeof(req));
96 req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
97 req.hdr.nlmsg_type = RTM_SETLINK;
98 req.hdr.nlmsg_flags = NLM_F_REQUEST;
99 req.hdr.nlmsg_seq = ++nl_seq;
100 req.hdr.nlmsg_pid = 0;
102 req.ifinfo.ifi_family = AF_UNSPEC;
103 req.ifinfo.ifi_type = 0;
104 req.ifinfo.ifi_index = drv->ifindex;
105 req.ifinfo.ifi_flags = 0;
106 req.ifinfo.ifi_change = 0;
108 if (linkmode != -1) {
109 rta = (struct rtattr *)
110 ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
111 rta->rta_type = IFLA_LINKMODE;
112 rta->rta_len = RTA_LENGTH(sizeof(char));
113 *((char *) RTA_DATA(rta)) = linkmode;
114 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
115 RTA_LENGTH(sizeof(char));
117 if (operstate != -1) {
118 rta = (struct rtattr *)
119 ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
120 rta->rta_type = IFLA_OPERSTATE;
121 rta->rta_len = RTA_LENGTH(sizeof(char));
122 *((char *) RTA_DATA(rta)) = operstate;
123 req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
124 RTA_LENGTH(sizeof(char));
127 wpa_printf(MSG_DEBUG, "WEXT: Operstate: linkmode=%d, operstate=%d",
128 linkmode, operstate);
130 ret = send(drv->event_sock, &req, req.hdr.nlmsg_len, 0);
131 if (ret < 0) {
132 wpa_printf(MSG_DEBUG, "WEXT: Sending operstate IFLA failed: "
133 "%s (assume operstate is not supported)",
134 strerror(errno));
137 return ret < 0 ? -1 : 0;
141 static int wpa_driver_nl80211_set_auth_param(
142 struct wpa_driver_nl80211_data *drv, int idx, u32 value)
144 struct iwreq iwr;
145 int ret = 0;
147 os_memset(&iwr, 0, sizeof(iwr));
148 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
149 iwr.u.param.flags = idx & IW_AUTH_INDEX;
150 iwr.u.param.value = value;
152 if (ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr) < 0) {
153 if (errno != EOPNOTSUPP) {
154 wpa_printf(MSG_DEBUG, "WEXT: SIOCSIWAUTH(param %d "
155 "value 0x%x) failed: %s)",
156 idx, value, strerror(errno));
158 ret = errno == EOPNOTSUPP ? -2 : -1;
161 return ret;
165 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
167 struct wpa_driver_nl80211_data *drv = priv;
168 struct iwreq iwr;
169 int ret = 0;
171 os_memset(&iwr, 0, sizeof(iwr));
172 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
174 if (ioctl(drv->ioctl_sock, SIOCGIWAP, &iwr) < 0) {
175 perror("ioctl[SIOCGIWAP]");
176 ret = -1;
178 os_memcpy(bssid, iwr.u.ap_addr.sa_data, ETH_ALEN);
180 return ret;
184 static int wpa_driver_nl80211_set_bssid(void *priv, const u8 *bssid)
186 struct wpa_driver_nl80211_data *drv = priv;
187 struct iwreq iwr;
188 int ret = 0;
190 os_memset(&iwr, 0, sizeof(iwr));
191 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
192 iwr.u.ap_addr.sa_family = ARPHRD_ETHER;
193 if (bssid)
194 os_memcpy(iwr.u.ap_addr.sa_data, bssid, ETH_ALEN);
195 else
196 os_memset(iwr.u.ap_addr.sa_data, 0, ETH_ALEN);
198 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr) < 0) {
199 perror("ioctl[SIOCSIWAP]");
200 ret = -1;
203 return ret;
207 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
209 struct wpa_driver_nl80211_data *drv = priv;
210 struct iwreq iwr;
211 int ret = 0;
213 os_memset(&iwr, 0, sizeof(iwr));
214 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
215 iwr.u.essid.pointer = (caddr_t) ssid;
216 iwr.u.essid.length = 32;
218 if (ioctl(drv->ioctl_sock, SIOCGIWESSID, &iwr) < 0) {
219 perror("ioctl[SIOCGIWESSID]");
220 ret = -1;
221 } else {
222 ret = iwr.u.essid.length;
223 if (ret > 32)
224 ret = 32;
225 /* Some drivers include nul termination in the SSID, so let's
226 * remove it here before further processing. WE-21 changes this
227 * to explicitly require the length _not_ to include nul
228 * termination. */
229 if (ret > 0 && ssid[ret - 1] == '\0' &&
230 drv->we_version_compiled < 21)
231 ret--;
234 return ret;
238 static int wpa_driver_nl80211_set_ssid(void *priv, const u8 *ssid,
239 size_t ssid_len)
241 struct wpa_driver_nl80211_data *drv = priv;
242 struct iwreq iwr;
243 int ret = 0;
244 char buf[33];
246 if (ssid_len > 32)
247 return -1;
249 os_memset(&iwr, 0, sizeof(iwr));
250 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
251 /* flags: 1 = ESSID is active, 0 = not (promiscuous) */
252 iwr.u.essid.flags = (ssid_len != 0);
253 os_memset(buf, 0, sizeof(buf));
254 os_memcpy(buf, ssid, ssid_len);
255 iwr.u.essid.pointer = (caddr_t) buf;
256 if (drv->we_version_compiled < 21) {
257 /* For historic reasons, set SSID length to include one extra
258 * character, C string nul termination, even though SSID is
259 * really an octet string that should not be presented as a C
260 * string. Some Linux drivers decrement the length by one and
261 * can thus end up missing the last octet of the SSID if the
262 * length is not incremented here. WE-21 changes this to
263 * explicitly require the length _not_ to include nul
264 * termination. */
265 if (ssid_len)
266 ssid_len++;
268 iwr.u.essid.length = ssid_len;
270 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
271 perror("ioctl[SIOCSIWESSID]");
272 ret = -1;
275 return ret;
279 static int wpa_driver_nl80211_set_freq(void *priv, int freq)
281 struct wpa_driver_nl80211_data *drv = priv;
282 struct iwreq iwr;
283 int ret = 0;
285 os_memset(&iwr, 0, sizeof(iwr));
286 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
287 iwr.u.freq.m = freq * 100000;
288 iwr.u.freq.e = 1;
290 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
291 perror("ioctl[SIOCSIWFREQ]");
292 ret = -1;
295 return ret;
299 static void
300 wpa_driver_nl80211_event_wireless_custom(void *ctx, char *custom)
302 union wpa_event_data data;
304 wpa_printf(MSG_MSGDUMP, "WEXT: Custom wireless event: '%s'",
305 custom);
307 os_memset(&data, 0, sizeof(data));
308 /* Host AP driver */
309 if (os_strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
310 data.michael_mic_failure.unicast =
311 os_strstr(custom, " unicast ") != NULL;
312 /* TODO: parse parameters(?) */
313 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
314 } else if (os_strncmp(custom, "ASSOCINFO(ReqIEs=", 17) == 0) {
315 char *spos;
316 int bytes;
318 spos = custom + 17;
320 bytes = strspn(spos, "0123456789abcdefABCDEF");
321 if (!bytes || (bytes & 1))
322 return;
323 bytes /= 2;
325 data.assoc_info.req_ies = os_malloc(bytes);
326 if (data.assoc_info.req_ies == NULL)
327 return;
329 data.assoc_info.req_ies_len = bytes;
330 hexstr2bin(spos, data.assoc_info.req_ies, bytes);
332 spos += bytes * 2;
334 data.assoc_info.resp_ies = NULL;
335 data.assoc_info.resp_ies_len = 0;
337 if (os_strncmp(spos, " RespIEs=", 9) == 0) {
338 spos += 9;
340 bytes = strspn(spos, "0123456789abcdefABCDEF");
341 if (!bytes || (bytes & 1))
342 goto done;
343 bytes /= 2;
345 data.assoc_info.resp_ies = os_malloc(bytes);
346 if (data.assoc_info.resp_ies == NULL)
347 goto done;
349 data.assoc_info.resp_ies_len = bytes;
350 hexstr2bin(spos, data.assoc_info.resp_ies, bytes);
353 wpa_supplicant_event(ctx, EVENT_ASSOCINFO, &data);
355 done:
356 os_free(data.assoc_info.resp_ies);
357 os_free(data.assoc_info.req_ies);
358 #ifdef CONFIG_PEERKEY
359 } else if (os_strncmp(custom, "STKSTART.request=", 17) == 0) {
360 if (hwaddr_aton(custom + 17, data.stkstart.peer)) {
361 wpa_printf(MSG_DEBUG, "WEXT: unrecognized "
362 "STKSTART.request '%s'", custom + 17);
363 return;
365 wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
366 #endif /* CONFIG_PEERKEY */
371 static int wpa_driver_nl80211_event_wireless_michaelmicfailure(
372 void *ctx, const char *ev, size_t len)
374 const struct iw_michaelmicfailure *mic;
375 union wpa_event_data data;
377 if (len < sizeof(*mic))
378 return -1;
380 mic = (const struct iw_michaelmicfailure *) ev;
382 wpa_printf(MSG_DEBUG, "Michael MIC failure wireless event: "
383 "flags=0x%x src_addr=" MACSTR, mic->flags,
384 MAC2STR(mic->src_addr.sa_data));
386 os_memset(&data, 0, sizeof(data));
387 data.michael_mic_failure.unicast = !(mic->flags & IW_MICFAILURE_GROUP);
388 wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
390 return 0;
394 static int wpa_driver_nl80211_event_wireless_pmkidcand(
395 struct wpa_driver_nl80211_data *drv, const char *ev, size_t len)
397 const struct iw_pmkid_cand *cand;
398 union wpa_event_data data;
399 const u8 *addr;
401 if (len < sizeof(*cand))
402 return -1;
404 cand = (const struct iw_pmkid_cand *) ev;
405 addr = (const u8 *) cand->bssid.sa_data;
407 wpa_printf(MSG_DEBUG, "PMKID candidate wireless event: "
408 "flags=0x%x index=%d bssid=" MACSTR, cand->flags,
409 cand->index, MAC2STR(addr));
411 os_memset(&data, 0, sizeof(data));
412 os_memcpy(data.pmkid_candidate.bssid, addr, ETH_ALEN);
413 data.pmkid_candidate.index = cand->index;
414 data.pmkid_candidate.preauth = cand->flags & IW_PMKID_CAND_PREAUTH;
415 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
417 return 0;
421 static int wpa_driver_nl80211_event_wireless_assocreqie(
422 struct wpa_driver_nl80211_data *drv, const char *ev, int len)
424 if (len < 0)
425 return -1;
427 wpa_hexdump(MSG_DEBUG, "AssocReq IE wireless event", (const u8 *) ev,
428 len);
429 os_free(drv->assoc_req_ies);
430 drv->assoc_req_ies = os_malloc(len);
431 if (drv->assoc_req_ies == NULL) {
432 drv->assoc_req_ies_len = 0;
433 return -1;
435 os_memcpy(drv->assoc_req_ies, ev, len);
436 drv->assoc_req_ies_len = len;
438 return 0;
442 static int wpa_driver_nl80211_event_wireless_assocrespie(
443 struct wpa_driver_nl80211_data *drv, const char *ev, int len)
445 if (len < 0)
446 return -1;
448 wpa_hexdump(MSG_DEBUG, "AssocResp IE wireless event", (const u8 *) ev,
449 len);
450 os_free(drv->assoc_resp_ies);
451 drv->assoc_resp_ies = os_malloc(len);
452 if (drv->assoc_resp_ies == NULL) {
453 drv->assoc_resp_ies_len = 0;
454 return -1;
456 os_memcpy(drv->assoc_resp_ies, ev, len);
457 drv->assoc_resp_ies_len = len;
459 return 0;
463 static void wpa_driver_nl80211_event_assoc_ies(struct wpa_driver_nl80211_data *drv)
465 union wpa_event_data data;
467 if (drv->assoc_req_ies == NULL && drv->assoc_resp_ies == NULL)
468 return;
470 os_memset(&data, 0, sizeof(data));
471 if (drv->assoc_req_ies) {
472 data.assoc_info.req_ies = drv->assoc_req_ies;
473 drv->assoc_req_ies = NULL;
474 data.assoc_info.req_ies_len = drv->assoc_req_ies_len;
476 if (drv->assoc_resp_ies) {
477 data.assoc_info.resp_ies = drv->assoc_resp_ies;
478 drv->assoc_resp_ies = NULL;
479 data.assoc_info.resp_ies_len = drv->assoc_resp_ies_len;
482 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &data);
484 os_free(data.assoc_info.req_ies);
485 os_free(data.assoc_info.resp_ies);
489 static void wpa_driver_nl80211_event_wireless(struct wpa_driver_nl80211_data *drv,
490 void *ctx, char *data, int len)
492 struct iw_event iwe_buf, *iwe = &iwe_buf;
493 char *pos, *end, *custom, *buf;
495 pos = data;
496 end = data + len;
498 while (pos + IW_EV_LCP_LEN <= end) {
499 /* Event data may be unaligned, so make a local, aligned copy
500 * before processing. */
501 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
502 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
503 iwe->cmd, iwe->len);
504 if (iwe->len <= IW_EV_LCP_LEN)
505 return;
507 custom = pos + IW_EV_POINT_LEN;
508 if (drv->we_version_compiled > 18 &&
509 (iwe->cmd == IWEVMICHAELMICFAILURE ||
510 iwe->cmd == IWEVCUSTOM ||
511 iwe->cmd == IWEVASSOCREQIE ||
512 iwe->cmd == IWEVASSOCRESPIE ||
513 iwe->cmd == IWEVPMKIDCAND)) {
514 /* WE-19 removed the pointer from struct iw_point */
515 char *dpos = (char *) &iwe_buf.u.data.length;
516 int dlen = dpos - (char *) &iwe_buf;
517 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
518 sizeof(struct iw_event) - dlen);
519 } else {
520 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
521 custom += IW_EV_POINT_OFF;
524 switch (iwe->cmd) {
525 case SIOCGIWAP:
526 wpa_printf(MSG_DEBUG, "Wireless event: new AP: "
527 MACSTR,
528 MAC2STR((u8 *) iwe->u.ap_addr.sa_data));
529 if (is_zero_ether_addr(
530 (const u8 *) iwe->u.ap_addr.sa_data) ||
531 os_memcmp(iwe->u.ap_addr.sa_data,
532 "\x44\x44\x44\x44\x44\x44", ETH_ALEN) ==
533 0) {
534 os_free(drv->assoc_req_ies);
535 drv->assoc_req_ies = NULL;
536 os_free(drv->assoc_resp_ies);
537 drv->assoc_resp_ies = NULL;
538 wpa_supplicant_event(ctx, EVENT_DISASSOC,
539 NULL);
541 } else {
542 wpa_driver_nl80211_event_assoc_ies(drv);
543 wpa_supplicant_event(ctx, EVENT_ASSOC, NULL);
545 break;
546 case IWEVMICHAELMICFAILURE:
547 wpa_driver_nl80211_event_wireless_michaelmicfailure(
548 ctx, custom, iwe->u.data.length);
549 break;
550 case IWEVCUSTOM:
551 if (custom + iwe->u.data.length > end)
552 return;
553 buf = os_malloc(iwe->u.data.length + 1);
554 if (buf == NULL)
555 return;
556 os_memcpy(buf, custom, iwe->u.data.length);
557 buf[iwe->u.data.length] = '\0';
558 wpa_driver_nl80211_event_wireless_custom(ctx, buf);
559 os_free(buf);
560 break;
561 case SIOCGIWSCAN:
562 drv->scan_complete_events = 1;
563 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout,
564 drv, ctx);
565 wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL);
566 break;
567 case IWEVASSOCREQIE:
568 wpa_driver_nl80211_event_wireless_assocreqie(
569 drv, custom, iwe->u.data.length);
570 break;
571 case IWEVASSOCRESPIE:
572 wpa_driver_nl80211_event_wireless_assocrespie(
573 drv, custom, iwe->u.data.length);
574 break;
575 case IWEVPMKIDCAND:
576 wpa_driver_nl80211_event_wireless_pmkidcand(
577 drv, custom, iwe->u.data.length);
578 break;
581 pos += iwe->len;
586 static void wpa_driver_nl80211_event_link(void *ctx, char *buf, size_t len,
587 int del)
589 union wpa_event_data event;
591 os_memset(&event, 0, sizeof(event));
592 if (len > sizeof(event.interface_status.ifname))
593 len = sizeof(event.interface_status.ifname) - 1;
594 os_memcpy(event.interface_status.ifname, buf, len);
595 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
596 EVENT_INTERFACE_ADDED;
598 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
599 del ? "DEL" : "NEW",
600 event.interface_status.ifname,
601 del ? "removed" : "added");
603 wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
607 static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data *drv,
608 void *ctx, struct nlmsghdr *h,
609 size_t len)
611 struct ifinfomsg *ifi;
612 int attrlen, _nlmsg_len, rta_len;
613 struct rtattr * attr;
615 if (len < sizeof(*ifi))
616 return;
618 ifi = NLMSG_DATA(h);
620 if (drv->ifindex != ifi->ifi_index) {
621 wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
622 ifi->ifi_index);
623 return;
626 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
627 "(%s%s%s%s)",
628 drv->operstate, ifi->ifi_flags,
629 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
630 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
631 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
632 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
634 * Some drivers send the association event before the operup event--in
635 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
636 * fails. This will hit us when wpa_supplicant does not need to do
637 * IEEE 802.1X authentication
639 if (drv->operstate == 1 &&
640 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
641 !(ifi->ifi_flags & IFF_RUNNING))
642 wpa_driver_nl80211_send_oper_ifla(drv, -1, IF_OPER_UP);
644 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
646 attrlen = h->nlmsg_len - _nlmsg_len;
647 if (attrlen < 0)
648 return;
650 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
652 rta_len = RTA_ALIGN(sizeof(struct rtattr));
653 while (RTA_OK(attr, attrlen)) {
654 if (attr->rta_type == IFLA_WIRELESS) {
655 wpa_driver_nl80211_event_wireless(
656 drv, ctx, ((char *) attr) + rta_len,
657 attr->rta_len - rta_len);
658 } else if (attr->rta_type == IFLA_IFNAME) {
659 wpa_driver_nl80211_event_link(ctx,
660 ((char *) attr) + rta_len,
661 attr->rta_len - rta_len, 0);
663 attr = RTA_NEXT(attr, attrlen);
668 static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data *drv,
669 void *ctx, struct nlmsghdr *h,
670 size_t len)
672 struct ifinfomsg *ifi;
673 int attrlen, _nlmsg_len, rta_len;
674 struct rtattr * attr;
676 if (len < sizeof(*ifi))
677 return;
679 ifi = NLMSG_DATA(h);
681 _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
683 attrlen = h->nlmsg_len - _nlmsg_len;
684 if (attrlen < 0)
685 return;
687 attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
689 rta_len = RTA_ALIGN(sizeof(struct rtattr));
690 while (RTA_OK(attr, attrlen)) {
691 if (attr->rta_type == IFLA_IFNAME) {
692 wpa_driver_nl80211_event_link(ctx,
693 ((char *) attr) + rta_len,
694 attr->rta_len - rta_len, 1);
696 attr = RTA_NEXT(attr, attrlen);
701 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
702 void *sock_ctx)
704 char buf[8192];
705 int left;
706 struct sockaddr_nl from;
707 socklen_t fromlen;
708 struct nlmsghdr *h;
709 int max_events = 10;
711 try_again:
712 fromlen = sizeof(from);
713 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
714 (struct sockaddr *) &from, &fromlen);
715 if (left < 0) {
716 if (errno != EINTR && errno != EAGAIN)
717 perror("recvfrom(netlink)");
718 return;
721 h = (struct nlmsghdr *) buf;
722 while (left >= (int) sizeof(*h)) {
723 int len, plen;
725 len = h->nlmsg_len;
726 plen = len - sizeof(*h);
727 if (len > left || plen < 0) {
728 wpa_printf(MSG_DEBUG, "Malformed netlink message: "
729 "len=%d left=%d plen=%d",
730 len, left, plen);
731 break;
734 switch (h->nlmsg_type) {
735 case RTM_NEWLINK:
736 wpa_driver_nl80211_event_rtm_newlink(eloop_ctx, sock_ctx,
737 h, plen);
738 break;
739 case RTM_DELLINK:
740 wpa_driver_nl80211_event_rtm_dellink(eloop_ctx, sock_ctx,
741 h, plen);
742 break;
745 len = NLMSG_ALIGN(len);
746 left -= len;
747 h = (struct nlmsghdr *) ((char *) h + len);
750 if (left > 0) {
751 wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
752 "message", left);
755 if (--max_events > 0) {
757 * Try to receive all events in one eloop call in order to
758 * limit race condition on cases where AssocInfo event, Assoc
759 * event, and EAPOL frames are received more or less at the
760 * same time. We want to process the event messages first
761 * before starting EAPOL processing.
763 goto try_again;
768 static int wpa_driver_nl80211_get_ifflags_ifname(struct wpa_driver_nl80211_data *drv,
769 const char *ifname, int *flags)
771 struct ifreq ifr;
773 os_memset(&ifr, 0, sizeof(ifr));
774 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
775 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
776 perror("ioctl[SIOCGIFFLAGS]");
777 return -1;
779 *flags = ifr.ifr_flags & 0xffff;
780 return 0;
785 * wpa_driver_nl80211_get_ifflags - Get interface flags (SIOCGIFFLAGS)
786 * @drv: driver_nl80211 private data
787 * @flags: Pointer to returned flags value
788 * Returns: 0 on success, -1 on failure
790 static int wpa_driver_nl80211_get_ifflags(struct wpa_driver_nl80211_data *drv,
791 int *flags)
793 return wpa_driver_nl80211_get_ifflags_ifname(drv, drv->ifname, flags);
797 static int wpa_driver_nl80211_set_ifflags_ifname(
798 struct wpa_driver_nl80211_data *drv,
799 const char *ifname, int flags)
801 struct ifreq ifr;
803 os_memset(&ifr, 0, sizeof(ifr));
804 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
805 ifr.ifr_flags = flags & 0xffff;
806 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
807 perror("SIOCSIFFLAGS");
808 return -1;
810 return 0;
815 * wpa_driver_nl80211_set_ifflags - Set interface flags (SIOCSIFFLAGS)
816 * @drv: driver_nl80211 private data
817 * @flags: New value for flags
818 * Returns: 0 on success, -1 on failure
820 static int wpa_driver_nl80211_set_ifflags(struct wpa_driver_nl80211_data *drv,
821 int flags)
823 return wpa_driver_nl80211_set_ifflags_ifname(drv, drv->ifname, flags);
828 * wpa_driver_nl80211_init - Initialize WE driver interface
829 * @ctx: context to be used when calling wpa_supplicant functions,
830 * e.g., wpa_supplicant_event()
831 * @ifname: interface name, e.g., wlan0
832 * Returns: Pointer to private data, %NULL on failure
834 void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
836 int s, flags;
837 struct sockaddr_nl local;
838 struct wpa_driver_nl80211_data *drv;
840 drv = os_zalloc(sizeof(*drv));
841 if (drv == NULL)
842 return NULL;
843 drv->ctx = ctx;
844 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
846 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
847 if (drv->nl_cb == NULL) {
848 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
849 "callbacks");
850 goto err1;
853 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
854 if (drv->nl_handle == NULL) {
855 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
856 "callbacks");
857 goto err2;
860 if (genl_connect(drv->nl_handle)) {
861 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
862 "netlink");
863 goto err3;
866 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
867 if (drv->nl_cache == NULL) {
868 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
869 "netlink cache");
870 goto err3;
873 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
874 if (drv->nl80211 == NULL) {
875 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
876 "found");
877 goto err4;
880 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
881 if (drv->ioctl_sock < 0) {
882 perror("socket(PF_INET,SOCK_DGRAM)");
883 goto err5;
886 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
887 if (s < 0) {
888 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
889 goto err6;
892 os_memset(&local, 0, sizeof(local));
893 local.nl_family = AF_NETLINK;
894 local.nl_groups = RTMGRP_LINK;
895 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
896 perror("bind(netlink)");
897 close(s);
898 goto err6;
901 eloop_register_read_sock(s, wpa_driver_nl80211_event_receive, drv,
902 ctx);
903 drv->event_sock = s;
905 if (wpa_driver_nl80211_get_ifflags(drv, &flags) != 0)
906 printf("Could not get interface '%s' flags\n", drv->ifname);
907 else if (!(flags & IFF_UP)) {
908 if (wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP) != 0) {
909 printf("Could not set interface '%s' UP\n",
910 drv->ifname);
911 } else {
913 * Wait some time to allow driver to initialize before
914 * starting configuring the driver. This seems to be
915 * needed at least some drivers that load firmware etc.
916 * when the interface is set up.
918 wpa_printf(MSG_DEBUG, "Interface %s set UP - waiting "
919 "a second for the driver to complete "
920 "initialization", drv->ifname);
921 sleep(1);
926 * Make sure that the driver does not have any obsolete PMKID entries.
928 wpa_driver_nl80211_flush_pmkid(drv);
930 if (wpa_driver_nl80211_set_mode(drv, 0) < 0) {
931 printf("Could not configure driver to use managed mode\n");
934 wpa_driver_nl80211_get_range(drv);
936 drv->ifindex = if_nametoindex(drv->ifname);
938 wpa_driver_nl80211_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
940 return drv;
942 err6:
943 close(drv->ioctl_sock);
944 err5:
945 genl_family_put(drv->nl80211);
946 err4:
947 nl_cache_free(drv->nl_cache);
948 err3:
949 nl_handle_destroy(drv->nl_handle);
950 err2:
951 nl_cb_put(drv->nl_cb);
952 err1:
953 os_free(drv);
954 return NULL;
959 * wpa_driver_nl80211_deinit - Deinitialize WE driver interface
960 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
962 * Shut down driver interface and processing of driver events. Free
963 * private data buffer if one was allocated in wpa_driver_nl80211_init().
965 void wpa_driver_nl80211_deinit(void *priv)
967 struct wpa_driver_nl80211_data *drv = priv;
968 int flags;
970 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
973 * Clear possibly configured driver parameters in order to make it
974 * easier to use the driver after wpa_supplicant has been terminated.
976 (void) wpa_driver_nl80211_set_bssid(drv,
977 (u8 *) "\x00\x00\x00\x00\x00\x00");
979 wpa_driver_nl80211_send_oper_ifla(priv, 0, IF_OPER_UP);
981 eloop_unregister_read_sock(drv->event_sock);
983 if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0)
984 (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_UP);
986 close(drv->event_sock);
987 close(drv->ioctl_sock);
988 os_free(drv->assoc_req_ies);
989 os_free(drv->assoc_resp_ies);
991 genl_family_put(drv->nl80211);
992 nl_cache_free(drv->nl_cache);
993 nl_handle_destroy(drv->nl_handle);
994 nl_cb_put(drv->nl_cb);
996 os_free(drv);
1001 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1002 * @eloop_ctx: Unused
1003 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1005 * This function can be used as registered timeout when starting a scan to
1006 * generate a scan completed event if the driver does not report this.
1008 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1010 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1011 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1016 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1017 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1018 * @ssid: Specific SSID to scan for (ProbeReq) or %NULL to scan for
1019 * all SSIDs (either active scan with broadcast SSID or passive
1020 * scan
1021 * @ssid_len: Length of the SSID
1022 * Returns: 0 on success, -1 on failure
1024 static int wpa_driver_nl80211_scan(void *priv, const u8 *ssid, size_t ssid_len)
1026 struct wpa_driver_nl80211_data *drv = priv;
1027 struct iwreq iwr;
1028 int ret = 0, timeout;
1029 struct iw_scan_req req;
1031 if (ssid_len > IW_ESSID_MAX_SIZE) {
1032 wpa_printf(MSG_DEBUG, "%s: too long SSID (%lu)",
1033 __FUNCTION__, (unsigned long) ssid_len);
1034 return -1;
1037 os_memset(&iwr, 0, sizeof(iwr));
1038 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1040 if (ssid && ssid_len) {
1041 os_memset(&req, 0, sizeof(req));
1042 req.essid_len = ssid_len;
1043 req.bssid.sa_family = ARPHRD_ETHER;
1044 os_memset(req.bssid.sa_data, 0xff, ETH_ALEN);
1045 os_memcpy(req.essid, ssid, ssid_len);
1046 iwr.u.data.pointer = (caddr_t) &req;
1047 iwr.u.data.length = sizeof(req);
1048 iwr.u.data.flags = IW_SCAN_THIS_ESSID;
1051 if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
1052 perror("ioctl[SIOCSIWSCAN]");
1053 ret = -1;
1056 /* Not all drivers generate "scan completed" wireless event, so try to
1057 * read results after a timeout. */
1058 timeout = 5;
1059 if (drv->scan_complete_events) {
1061 * The driver seems to deliver SIOCGIWSCAN events to notify
1062 * when scan is complete, so use longer timeout to avoid race
1063 * conditions with scanning and following association request.
1065 timeout = 30;
1067 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
1068 "seconds", ret, timeout);
1069 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1070 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, drv,
1071 drv->ctx);
1073 return ret;
1077 static u8 * wpa_driver_nl80211_giwscan(struct wpa_driver_nl80211_data *drv,
1078 size_t *len)
1080 struct iwreq iwr;
1081 u8 *res_buf;
1082 size_t res_buf_len;
1084 res_buf_len = IW_SCAN_MAX_DATA;
1085 for (;;) {
1086 res_buf = os_malloc(res_buf_len);
1087 if (res_buf == NULL)
1088 return NULL;
1089 os_memset(&iwr, 0, sizeof(iwr));
1090 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1091 iwr.u.data.pointer = res_buf;
1092 iwr.u.data.length = res_buf_len;
1094 if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
1095 break;
1097 if (errno == E2BIG && res_buf_len < 100000) {
1098 os_free(res_buf);
1099 res_buf = NULL;
1100 res_buf_len *= 2;
1101 wpa_printf(MSG_DEBUG, "Scan results did not fit - "
1102 "trying larger buffer (%lu bytes)",
1103 (unsigned long) res_buf_len);
1104 } else {
1105 perror("ioctl[SIOCGIWSCAN]");
1106 os_free(res_buf);
1107 return NULL;
1111 if (iwr.u.data.length > res_buf_len) {
1112 os_free(res_buf);
1113 return NULL;
1115 *len = iwr.u.data.length;
1117 return res_buf;
1122 * Data structure for collecting WEXT scan results. This is needed to allow
1123 * the various methods of reporting IEs to be combined into a single IE buffer.
1125 struct wext_scan_data {
1126 struct wpa_scan_res res;
1127 u8 *ie;
1128 size_t ie_len;
1129 u8 ssid[32];
1130 size_t ssid_len;
1131 int maxrate;
1135 static void wext_get_scan_mode(struct iw_event *iwe,
1136 struct wext_scan_data *res)
1138 if (iwe->u.mode == IW_MODE_ADHOC)
1139 res->res.caps |= IEEE80211_CAP_IBSS;
1140 else if (iwe->u.mode == IW_MODE_MASTER || iwe->u.mode == IW_MODE_INFRA)
1141 res->res.caps |= IEEE80211_CAP_ESS;
1145 static void wext_get_scan_ssid(struct iw_event *iwe,
1146 struct wext_scan_data *res, char *custom,
1147 char *end)
1149 int ssid_len = iwe->u.essid.length;
1150 if (custom + ssid_len > end)
1151 return;
1152 if (iwe->u.essid.flags &&
1153 ssid_len > 0 &&
1154 ssid_len <= IW_ESSID_MAX_SIZE) {
1155 os_memcpy(res->ssid, custom, ssid_len);
1156 res->ssid_len = ssid_len;
1161 static void wext_get_scan_freq(struct iw_event *iwe,
1162 struct wext_scan_data *res)
1164 int divi = 1000000, i;
1166 if (iwe->u.freq.e == 0) {
1168 * Some drivers do not report frequency, but a channel.
1169 * Try to map this to frequency by assuming they are using
1170 * IEEE 802.11b/g. But don't overwrite a previously parsed
1171 * frequency if the driver sends both frequency and channel,
1172 * since the driver may be sending an A-band channel that we
1173 * don't handle here.
1176 if (res->res.freq)
1177 return;
1179 if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
1180 res->res.freq = 2407 + 5 * iwe->u.freq.m;
1181 return;
1182 } else if (iwe->u.freq.m == 14) {
1183 res->res.freq = 2484;
1184 return;
1188 if (iwe->u.freq.e > 6) {
1189 wpa_printf(MSG_DEBUG, "Invalid freq in scan results (BSSID="
1190 MACSTR " m=%d e=%d)",
1191 MAC2STR(res->res.bssid), iwe->u.freq.m,
1192 iwe->u.freq.e);
1193 return;
1196 for (i = 0; i < iwe->u.freq.e; i++)
1197 divi /= 10;
1198 res->res.freq = iwe->u.freq.m / divi;
1202 static void wext_get_scan_qual(struct iw_event *iwe,
1203 struct wext_scan_data *res)
1205 res->res.qual = iwe->u.qual.qual;
1206 res->res.noise = iwe->u.qual.noise;
1207 res->res.level = iwe->u.qual.level;
1211 static void wext_get_scan_encode(struct iw_event *iwe,
1212 struct wext_scan_data *res)
1214 if (!(iwe->u.data.flags & IW_ENCODE_DISABLED))
1215 res->res.caps |= IEEE80211_CAP_PRIVACY;
1219 static void wext_get_scan_rate(struct iw_event *iwe,
1220 struct wext_scan_data *res, char *pos,
1221 char *end)
1223 int maxrate;
1224 char *custom = pos + IW_EV_LCP_LEN;
1225 struct iw_param p;
1226 size_t clen;
1228 clen = iwe->len;
1229 if (custom + clen > end)
1230 return;
1231 maxrate = 0;
1232 while (((ssize_t) clen) >= (ssize_t) sizeof(struct iw_param)) {
1233 /* Note: may be misaligned, make a local, aligned copy */
1234 os_memcpy(&p, custom, sizeof(struct iw_param));
1235 if (p.value > maxrate)
1236 maxrate = p.value;
1237 clen -= sizeof(struct iw_param);
1238 custom += sizeof(struct iw_param);
1241 /* Convert the maxrate from WE-style (b/s units) to
1242 * 802.11 rates (500000 b/s units).
1244 res->maxrate = maxrate / 500000;
1248 static void wext_get_scan_iwevgenie(struct iw_event *iwe,
1249 struct wext_scan_data *res, char *custom,
1250 char *end)
1252 char *genie, *gpos, *gend;
1253 u8 *tmp;
1255 gpos = genie = custom;
1256 gend = genie + iwe->u.data.length;
1257 if (gend > end) {
1258 wpa_printf(MSG_INFO, "IWEVGENIE overflow");
1259 return;
1262 tmp = os_realloc(res->ie, res->ie_len + gend - gpos);
1263 if (tmp == NULL)
1264 return;
1265 os_memcpy(tmp + res->ie_len, gpos, gend - gpos);
1266 res->ie = tmp;
1267 res->ie_len += gend - gpos;
1271 static void wext_get_scan_custom(struct iw_event *iwe,
1272 struct wext_scan_data *res, char *custom,
1273 char *end)
1275 size_t clen;
1276 u8 *tmp;
1278 clen = iwe->u.data.length;
1279 if (custom + clen > end)
1280 return;
1282 if (clen > 7 && os_strncmp(custom, "wpa_ie=", 7) == 0) {
1283 char *spos;
1284 int bytes;
1285 spos = custom + 7;
1286 bytes = custom + clen - spos;
1287 if (bytes & 1)
1288 return;
1289 bytes /= 2;
1290 tmp = os_realloc(res->ie, res->ie_len + bytes);
1291 if (tmp == NULL)
1292 return;
1293 hexstr2bin(spos, tmp + res->ie_len, bytes);
1294 res->ie = tmp;
1295 res->ie_len += bytes;
1296 } else if (clen > 7 && os_strncmp(custom, "rsn_ie=", 7) == 0) {
1297 char *spos;
1298 int bytes;
1299 spos = custom + 7;
1300 bytes = custom + clen - spos;
1301 if (bytes & 1)
1302 return;
1303 bytes /= 2;
1304 tmp = os_realloc(res->ie, res->ie_len + bytes);
1305 if (tmp == NULL)
1306 return;
1307 hexstr2bin(spos, tmp + res->ie_len, bytes);
1308 res->ie = tmp;
1309 res->ie_len += bytes;
1310 } else if (clen > 4 && os_strncmp(custom, "tsf=", 4) == 0) {
1311 char *spos;
1312 int bytes;
1313 u8 bin[8];
1314 spos = custom + 4;
1315 bytes = custom + clen - spos;
1316 if (bytes != 16) {
1317 wpa_printf(MSG_INFO, "Invalid TSF length (%d)", bytes);
1318 return;
1320 bytes /= 2;
1321 hexstr2bin(spos, bin, bytes);
1322 res->res.tsf += WPA_GET_BE64(bin);
1327 static int wext_19_iw_point(struct wpa_driver_nl80211_data *drv, u16 cmd)
1329 return drv->we_version_compiled > 18 &&
1330 (cmd == SIOCGIWESSID || cmd == SIOCGIWENCODE ||
1331 cmd == IWEVGENIE || cmd == IWEVCUSTOM);
1335 static void wpa_driver_nl80211_add_scan_entry(struct wpa_scan_results *res,
1336 struct wext_scan_data *data)
1338 struct wpa_scan_res **tmp;
1339 struct wpa_scan_res *r;
1340 size_t extra_len;
1341 u8 *pos, *end, *ssid_ie = NULL, *rate_ie = NULL;
1343 /* Figure out whether we need to fake any IEs */
1344 pos = data->ie;
1345 end = pos + data->ie_len;
1346 while (pos && pos + 1 < end) {
1347 if (pos + 2 + pos[1] > end)
1348 break;
1349 if (pos[0] == WLAN_EID_SSID)
1350 ssid_ie = pos;
1351 else if (pos[0] == WLAN_EID_SUPP_RATES)
1352 rate_ie = pos;
1353 else if (pos[0] == WLAN_EID_EXT_SUPP_RATES)
1354 rate_ie = pos;
1355 pos += 2 + pos[1];
1358 extra_len = 0;
1359 if (ssid_ie == NULL)
1360 extra_len += 2 + data->ssid_len;
1361 if (rate_ie == NULL && data->maxrate)
1362 extra_len += 3;
1364 r = os_zalloc(sizeof(*r) + extra_len + data->ie_len);
1365 if (r == NULL)
1366 return;
1367 os_memcpy(r, &data->res, sizeof(*r));
1368 r->ie_len = extra_len + data->ie_len;
1369 pos = (u8 *) (r + 1);
1370 if (ssid_ie == NULL) {
1372 * Generate a fake SSID IE since the driver did not report
1373 * a full IE list.
1375 *pos++ = WLAN_EID_SSID;
1376 *pos++ = data->ssid_len;
1377 os_memcpy(pos, data->ssid, data->ssid_len);
1378 pos += data->ssid_len;
1380 if (rate_ie == NULL && data->maxrate) {
1382 * Generate a fake Supported Rates IE since the driver did not
1383 * report a full IE list.
1385 *pos++ = WLAN_EID_SUPP_RATES;
1386 *pos++ = 1;
1387 *pos++ = data->maxrate;
1389 if (data->ie)
1390 os_memcpy(pos, data->ie, data->ie_len);
1392 tmp = os_realloc(res->res,
1393 (res->num + 1) * sizeof(struct wpa_scan_res *));
1394 if (tmp == NULL) {
1395 os_free(r);
1396 return;
1398 tmp[res->num++] = r;
1399 res->res = tmp;
1404 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1405 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1406 * Returns: Scan results on success, -1 on failure
1408 struct wpa_scan_results * wpa_driver_nl80211_get_scan_results(void *priv)
1410 struct wpa_driver_nl80211_data *drv = priv;
1411 size_t ap_num = 0, len;
1412 int first;
1413 u8 *res_buf;
1414 struct iw_event iwe_buf, *iwe = &iwe_buf;
1415 char *pos, *end, *custom;
1416 struct wpa_scan_results *res;
1417 struct wext_scan_data data;
1419 res_buf = wpa_driver_nl80211_giwscan(drv, &len);
1420 if (res_buf == NULL)
1421 return NULL;
1423 ap_num = 0;
1424 first = 1;
1426 res = os_zalloc(sizeof(*res));
1427 if (res == NULL) {
1428 os_free(res_buf);
1429 return NULL;
1432 pos = (char *) res_buf;
1433 end = (char *) res_buf + len;
1434 os_memset(&data, 0, sizeof(data));
1436 while (pos + IW_EV_LCP_LEN <= end) {
1437 /* Event data may be unaligned, so make a local, aligned copy
1438 * before processing. */
1439 os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
1440 if (iwe->len <= IW_EV_LCP_LEN)
1441 break;
1443 custom = pos + IW_EV_POINT_LEN;
1444 if (wext_19_iw_point(drv, iwe->cmd)) {
1445 /* WE-19 removed the pointer from struct iw_point */
1446 char *dpos = (char *) &iwe_buf.u.data.length;
1447 int dlen = dpos - (char *) &iwe_buf;
1448 os_memcpy(dpos, pos + IW_EV_LCP_LEN,
1449 sizeof(struct iw_event) - dlen);
1450 } else {
1451 os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
1452 custom += IW_EV_POINT_OFF;
1455 switch (iwe->cmd) {
1456 case SIOCGIWAP:
1457 if (!first)
1458 wpa_driver_nl80211_add_scan_entry(res, &data);
1459 first = 0;
1460 os_free(data.ie);
1461 os_memset(&data, 0, sizeof(data));
1462 os_memcpy(data.res.bssid,
1463 iwe->u.ap_addr.sa_data, ETH_ALEN);
1464 break;
1465 case SIOCGIWMODE:
1466 wext_get_scan_mode(iwe, &data);
1467 break;
1468 case SIOCGIWESSID:
1469 wext_get_scan_ssid(iwe, &data, custom, end);
1470 break;
1471 case SIOCGIWFREQ:
1472 wext_get_scan_freq(iwe, &data);
1473 break;
1474 case IWEVQUAL:
1475 wext_get_scan_qual(iwe, &data);
1476 break;
1477 case SIOCGIWENCODE:
1478 wext_get_scan_encode(iwe, &data);
1479 break;
1480 case SIOCGIWRATE:
1481 wext_get_scan_rate(iwe, &data, pos, end);
1482 break;
1483 case IWEVGENIE:
1484 wext_get_scan_iwevgenie(iwe, &data, custom, end);
1485 break;
1486 case IWEVCUSTOM:
1487 wext_get_scan_custom(iwe, &data, custom, end);
1488 break;
1491 pos += iwe->len;
1493 os_free(res_buf);
1494 res_buf = NULL;
1495 if (!first)
1496 wpa_driver_nl80211_add_scan_entry(res, &data);
1497 os_free(data.ie);
1499 wpa_printf(MSG_DEBUG, "Received %lu bytes of scan results (%lu BSSes)",
1500 (unsigned long) len, (unsigned long) res->num);
1502 return res;
1506 static int wpa_driver_nl80211_get_range(void *priv)
1508 struct wpa_driver_nl80211_data *drv = priv;
1509 struct iw_range *range;
1510 struct iwreq iwr;
1511 int minlen;
1512 size_t buflen;
1515 * Use larger buffer than struct iw_range in order to allow the
1516 * structure to grow in the future.
1518 buflen = sizeof(struct iw_range) + 500;
1519 range = os_zalloc(buflen);
1520 if (range == NULL)
1521 return -1;
1523 os_memset(&iwr, 0, sizeof(iwr));
1524 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1525 iwr.u.data.pointer = (caddr_t) range;
1526 iwr.u.data.length = buflen;
1528 minlen = ((char *) &range->enc_capa) - (char *) range +
1529 sizeof(range->enc_capa);
1531 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1532 perror("ioctl[SIOCGIWRANGE]");
1533 os_free(range);
1534 return -1;
1535 } else if (iwr.u.data.length >= minlen &&
1536 range->we_version_compiled >= 18) {
1537 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1538 "WE(source)=%d enc_capa=0x%x",
1539 range->we_version_compiled,
1540 range->we_version_source,
1541 range->enc_capa);
1542 drv->has_capability = 1;
1543 drv->we_version_compiled = range->we_version_compiled;
1544 if (range->enc_capa & IW_ENC_CAPA_WPA) {
1545 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1546 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK;
1548 if (range->enc_capa & IW_ENC_CAPA_WPA2) {
1549 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1550 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1552 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 |
1553 WPA_DRIVER_CAPA_ENC_WEP104;
1554 if (range->enc_capa & IW_ENC_CAPA_CIPHER_TKIP)
1555 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP;
1556 if (range->enc_capa & IW_ENC_CAPA_CIPHER_CCMP)
1557 drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP;
1558 wpa_printf(MSG_DEBUG, " capabilities: key_mgmt 0x%x enc 0x%x",
1559 drv->capa.key_mgmt, drv->capa.enc);
1560 } else {
1561 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: too old (short) data - "
1562 "assuming WPA is not supported");
1565 os_free(range);
1566 return 0;
1570 static int wpa_driver_nl80211_set_wpa(void *priv, int enabled)
1572 struct wpa_driver_nl80211_data *drv = priv;
1573 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1575 return wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_WPA_ENABLED,
1576 enabled);
1580 static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
1581 const u8 *addr, int key_idx,
1582 int set_tx, const u8 *seq,
1583 size_t seq_len,
1584 const u8 *key, size_t key_len)
1586 struct wpa_driver_nl80211_data *drv = priv;
1587 int ret = -1, err;
1588 struct nl_msg *msg;
1590 wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
1591 "seq_len=%lu key_len=%lu",
1592 __func__, alg, addr, key_idx, set_tx,
1593 (unsigned long) seq_len, (unsigned long) key_len);
1595 msg = nlmsg_alloc();
1596 if (msg == NULL)
1597 return -1;
1599 if (alg == WPA_ALG_NONE) {
1600 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1601 NL80211_CMD_DEL_KEY, 0);
1602 } else {
1603 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1604 NL80211_CMD_NEW_KEY, 0);
1605 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
1606 switch (alg) {
1607 case WPA_ALG_WEP:
1608 if (key_len == 5)
1609 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1610 0x000FAC01);
1611 else
1612 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1613 0x000FAC05);
1614 break;
1615 case WPA_ALG_TKIP:
1616 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
1617 break;
1618 case WPA_ALG_CCMP:
1619 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
1620 break;
1621 default:
1622 nlmsg_free(msg);
1623 return -1;
1627 if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1629 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1630 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1632 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1633 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1635 err = 0;
1636 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1637 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
1638 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d", err);
1639 nlmsg_free(msg);
1640 return -1;
1643 if (set_tx && alg != WPA_ALG_NONE) {
1644 nlmsg_free(msg);
1645 msg = nlmsg_alloc();
1646 if (msg == NULL)
1647 return -1;
1649 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1650 0, NL80211_CMD_SET_KEY, 0);
1651 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1652 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1653 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
1655 err = 0;
1656 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1657 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
1658 wpa_printf(MSG_DEBUG, "nl80211: set default key "
1659 "failed; err=%d", err);
1660 nlmsg_free(msg);
1661 return -1;
1665 ret = 0;
1667 nla_put_failure:
1668 nlmsg_free(msg);
1669 return ret;
1673 static int wpa_driver_nl80211_set_countermeasures(void *priv,
1674 int enabled)
1676 struct wpa_driver_nl80211_data *drv = priv;
1677 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1678 return wpa_driver_nl80211_set_auth_param(drv,
1679 IW_AUTH_TKIP_COUNTERMEASURES,
1680 enabled);
1684 static int wpa_driver_nl80211_set_drop_unencrypted(void *priv,
1685 int enabled)
1687 struct wpa_driver_nl80211_data *drv = priv;
1688 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1689 drv->use_crypt = enabled;
1690 return wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
1691 enabled);
1695 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
1696 const u8 *addr, int cmd, int reason_code)
1698 struct iwreq iwr;
1699 struct iw_mlme mlme;
1700 int ret = 0;
1702 os_memset(&iwr, 0, sizeof(iwr));
1703 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1704 os_memset(&mlme, 0, sizeof(mlme));
1705 mlme.cmd = cmd;
1706 mlme.reason_code = reason_code;
1707 mlme.addr.sa_family = ARPHRD_ETHER;
1708 os_memcpy(mlme.addr.sa_data, addr, ETH_ALEN);
1709 iwr.u.data.pointer = (caddr_t) &mlme;
1710 iwr.u.data.length = sizeof(mlme);
1712 if (ioctl(drv->ioctl_sock, SIOCSIWMLME, &iwr) < 0) {
1713 perror("ioctl[SIOCSIWMLME]");
1714 ret = -1;
1717 return ret;
1721 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
1722 int reason_code)
1724 struct wpa_driver_nl80211_data *drv = priv;
1725 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1726 return wpa_driver_nl80211_mlme(drv, addr, IW_MLME_DEAUTH, reason_code);
1730 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
1731 int reason_code)
1733 struct wpa_driver_nl80211_data *drv = priv;
1734 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1735 return wpa_driver_nl80211_mlme(drv, addr, IW_MLME_DISASSOC,
1736 reason_code);
1740 static int wpa_driver_nl80211_set_gen_ie(void *priv, const u8 *ie,
1741 size_t ie_len)
1743 struct wpa_driver_nl80211_data *drv = priv;
1744 struct iwreq iwr;
1745 int ret = 0;
1747 os_memset(&iwr, 0, sizeof(iwr));
1748 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1749 iwr.u.data.pointer = (caddr_t) ie;
1750 iwr.u.data.length = ie_len;
1752 if (ioctl(drv->ioctl_sock, SIOCSIWGENIE, &iwr) < 0) {
1753 perror("ioctl[SIOCSIWGENIE]");
1754 ret = -1;
1757 return ret;
1761 static int wpa_driver_nl80211_cipher2wext(int cipher)
1763 switch (cipher) {
1764 case CIPHER_NONE:
1765 return IW_AUTH_CIPHER_NONE;
1766 case CIPHER_WEP40:
1767 return IW_AUTH_CIPHER_WEP40;
1768 case CIPHER_TKIP:
1769 return IW_AUTH_CIPHER_TKIP;
1770 case CIPHER_CCMP:
1771 return IW_AUTH_CIPHER_CCMP;
1772 case CIPHER_WEP104:
1773 return IW_AUTH_CIPHER_WEP104;
1774 default:
1775 return 0;
1780 static int wpa_driver_nl80211_keymgmt2wext(int keymgmt)
1782 switch (keymgmt) {
1783 case KEY_MGMT_802_1X:
1784 case KEY_MGMT_802_1X_NO_WPA:
1785 return IW_AUTH_KEY_MGMT_802_1X;
1786 case KEY_MGMT_PSK:
1787 return IW_AUTH_KEY_MGMT_PSK;
1788 default:
1789 return 0;
1794 static int
1795 wpa_driver_nl80211_auth_alg_fallback(struct wpa_driver_nl80211_data *drv,
1796 struct wpa_driver_associate_params *params)
1798 struct iwreq iwr;
1799 int ret = 0;
1801 wpa_printf(MSG_DEBUG, "WEXT: Driver did not support "
1802 "SIOCSIWAUTH for AUTH_ALG, trying SIOCSIWENCODE");
1804 os_memset(&iwr, 0, sizeof(iwr));
1805 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
1806 /* Just changing mode, not actual keys */
1807 iwr.u.encoding.flags = 0;
1808 iwr.u.encoding.pointer = (caddr_t) NULL;
1809 iwr.u.encoding.length = 0;
1812 * Note: IW_ENCODE_{OPEN,RESTRICTED} can be interpreted to mean two
1813 * different things. Here they are used to indicate Open System vs.
1814 * Shared Key authentication algorithm. However, some drivers may use
1815 * them to select between open/restricted WEP encrypted (open = allow
1816 * both unencrypted and encrypted frames; restricted = only allow
1817 * encrypted frames).
1820 if (!drv->use_crypt) {
1821 iwr.u.encoding.flags |= IW_ENCODE_DISABLED;
1822 } else {
1823 if (params->auth_alg & AUTH_ALG_OPEN_SYSTEM)
1824 iwr.u.encoding.flags |= IW_ENCODE_OPEN;
1825 if (params->auth_alg & AUTH_ALG_SHARED_KEY)
1826 iwr.u.encoding.flags |= IW_ENCODE_RESTRICTED;
1829 if (ioctl(drv->ioctl_sock, SIOCSIWENCODE, &iwr) < 0) {
1830 perror("ioctl[SIOCSIWENCODE]");
1831 ret = -1;
1834 return ret;
1838 static int wpa_driver_nl80211_associate(
1839 void *priv, struct wpa_driver_associate_params *params)
1841 struct wpa_driver_nl80211_data *drv = priv;
1842 int ret = 0;
1843 int allow_unencrypted_eapol;
1844 int value;
1846 wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
1849 * If the driver did not support SIOCSIWAUTH, fallback to
1850 * SIOCSIWENCODE here.
1852 if (drv->auth_alg_fallback &&
1853 wpa_driver_nl80211_auth_alg_fallback(drv, params) < 0)
1854 ret = -1;
1856 if (!params->bssid &&
1857 wpa_driver_nl80211_set_bssid(drv, NULL) < 0)
1858 ret = -1;
1860 if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
1861 ret = -1;
1862 /* TODO: should consider getting wpa version and cipher/key_mgmt suites
1863 * from configuration, not from here, where only the selected suite is
1864 * available */
1865 if (wpa_driver_nl80211_set_gen_ie(drv, params->wpa_ie, params->wpa_ie_len)
1866 < 0)
1867 ret = -1;
1868 if (params->wpa_ie == NULL || params->wpa_ie_len == 0)
1869 value = IW_AUTH_WPA_VERSION_DISABLED;
1870 else if (params->wpa_ie[0] == WLAN_EID_RSN)
1871 value = IW_AUTH_WPA_VERSION_WPA2;
1872 else
1873 value = IW_AUTH_WPA_VERSION_WPA;
1874 if (wpa_driver_nl80211_set_auth_param(drv,
1875 IW_AUTH_WPA_VERSION, value) < 0)
1876 ret = -1;
1877 value = wpa_driver_nl80211_cipher2wext(params->pairwise_suite);
1878 if (wpa_driver_nl80211_set_auth_param(drv,
1879 IW_AUTH_CIPHER_PAIRWISE, value) < 0)
1880 ret = -1;
1881 value = wpa_driver_nl80211_cipher2wext(params->group_suite);
1882 if (wpa_driver_nl80211_set_auth_param(drv,
1883 IW_AUTH_CIPHER_GROUP, value) < 0)
1884 ret = -1;
1885 value = wpa_driver_nl80211_keymgmt2wext(params->key_mgmt_suite);
1886 if (wpa_driver_nl80211_set_auth_param(drv,
1887 IW_AUTH_KEY_MGMT, value) < 0)
1888 ret = -1;
1889 value = params->key_mgmt_suite != KEY_MGMT_NONE ||
1890 params->pairwise_suite != CIPHER_NONE ||
1891 params->group_suite != CIPHER_NONE ||
1892 params->wpa_ie_len;
1893 if (wpa_driver_nl80211_set_auth_param(drv,
1894 IW_AUTH_PRIVACY_INVOKED, value) < 0)
1895 ret = -1;
1897 /* Allow unencrypted EAPOL messages even if pairwise keys are set when
1898 * not using WPA. IEEE 802.1X specifies that these frames are not
1899 * encrypted, but WPA encrypts them when pairwise keys are in use. */
1900 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
1901 params->key_mgmt_suite == KEY_MGMT_PSK)
1902 allow_unencrypted_eapol = 0;
1903 else
1904 allow_unencrypted_eapol = 1;
1906 if (wpa_driver_nl80211_set_auth_param(drv,
1907 IW_AUTH_RX_UNENCRYPTED_EAPOL,
1908 allow_unencrypted_eapol) < 0)
1909 ret = -1;
1910 if (params->freq && wpa_driver_nl80211_set_freq(drv, params->freq) < 0)
1911 ret = -1;
1912 if (wpa_driver_nl80211_set_ssid(drv, params->ssid, params->ssid_len) < 0)
1913 ret = -1;
1914 if (params->bssid &&
1915 wpa_driver_nl80211_set_bssid(drv, params->bssid) < 0)
1916 ret = -1;
1918 return ret;
1922 static int wpa_driver_nl80211_set_auth_alg(void *priv, int auth_alg)
1924 struct wpa_driver_nl80211_data *drv = priv;
1925 int algs = 0, res;
1927 if (auth_alg & AUTH_ALG_OPEN_SYSTEM)
1928 algs |= IW_AUTH_ALG_OPEN_SYSTEM;
1929 if (auth_alg & AUTH_ALG_SHARED_KEY)
1930 algs |= IW_AUTH_ALG_SHARED_KEY;
1931 if (auth_alg & AUTH_ALG_LEAP)
1932 algs |= IW_AUTH_ALG_LEAP;
1933 if (algs == 0) {
1934 /* at least one algorithm should be set */
1935 algs = IW_AUTH_ALG_OPEN_SYSTEM;
1938 res = wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_80211_AUTH_ALG,
1939 algs);
1940 drv->auth_alg_fallback = res == -2;
1941 return res;
1946 * wpa_driver_nl80211_set_mode - Set wireless mode (infra/adhoc), SIOCSIWMODE
1947 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1948 * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
1949 * Returns: 0 on success, -1 on failure
1951 static int wpa_driver_nl80211_set_mode(void *priv, int mode)
1953 struct wpa_driver_nl80211_data *drv = priv;
1954 int ret = -1, flags;
1955 struct nl_msg *msg;
1957 msg = nlmsg_alloc();
1958 if (!msg)
1959 return -1;
1961 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1962 0, NL80211_CMD_SET_INTERFACE, 0);
1963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1964 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1966 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1967 nl_wait_for_ack(drv->nl_handle) < 0)
1968 goto try_again;
1970 nlmsg_free(msg);
1971 return 0;
1973 nla_put_failure:
1974 nlmsg_free(msg);
1975 return -1;
1977 try_again:
1978 /* mac80211 doesn't allow mode changes while the device is up, so
1979 * take the device down, try to set the mode again, and bring the
1980 * device back up.
1982 if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0) {
1983 (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_UP);
1985 /* Try to set the mode again while the interface is down */
1986 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1987 nl_wait_for_ack(drv->nl_handle) < 0) {
1988 wpa_printf(MSG_ERROR, "Failed to set interface %s "
1989 "mode", drv->ifname);
1990 } else
1991 ret = 0;
1993 /* Ignore return value of get_ifflags to ensure that the device
1994 * is always up like it was before this function was called.
1996 (void) wpa_driver_nl80211_get_ifflags(drv, &flags);
1997 (void) wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP);
2000 nlmsg_free(msg);
2001 return ret;
2005 static int wpa_driver_nl80211_pmksa(struct wpa_driver_nl80211_data *drv,
2006 u32 cmd, const u8 *bssid, const u8 *pmkid)
2008 struct iwreq iwr;
2009 struct iw_pmksa pmksa;
2010 int ret = 0;
2012 os_memset(&iwr, 0, sizeof(iwr));
2013 os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
2014 os_memset(&pmksa, 0, sizeof(pmksa));
2015 pmksa.cmd = cmd;
2016 pmksa.bssid.sa_family = ARPHRD_ETHER;
2017 if (bssid)
2018 os_memcpy(pmksa.bssid.sa_data, bssid, ETH_ALEN);
2019 if (pmkid)
2020 os_memcpy(pmksa.pmkid, pmkid, IW_PMKID_LEN);
2021 iwr.u.data.pointer = (caddr_t) &pmksa;
2022 iwr.u.data.length = sizeof(pmksa);
2024 if (ioctl(drv->ioctl_sock, SIOCSIWPMKSA, &iwr) < 0) {
2025 if (errno != EOPNOTSUPP)
2026 perror("ioctl[SIOCSIWPMKSA]");
2027 ret = -1;
2030 return ret;
2034 static int wpa_driver_nl80211_add_pmkid(void *priv, const u8 *bssid,
2035 const u8 *pmkid)
2037 struct wpa_driver_nl80211_data *drv = priv;
2038 return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_ADD, bssid, pmkid);
2042 static int wpa_driver_nl80211_remove_pmkid(void *priv, const u8 *bssid,
2043 const u8 *pmkid)
2045 struct wpa_driver_nl80211_data *drv = priv;
2046 return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_REMOVE, bssid, pmkid);
2050 static int wpa_driver_nl80211_flush_pmkid(void *priv)
2052 struct wpa_driver_nl80211_data *drv = priv;
2053 return wpa_driver_nl80211_pmksa(drv, IW_PMKSA_FLUSH, NULL, NULL);
2057 static int wpa_driver_nl80211_get_capa(void *priv,
2058 struct wpa_driver_capa *capa)
2060 struct wpa_driver_nl80211_data *drv = priv;
2061 if (!drv->has_capability)
2062 return -1;
2063 os_memcpy(capa, &drv->capa, sizeof(*capa));
2064 return 0;
2068 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
2070 struct wpa_driver_nl80211_data *drv = priv;
2072 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
2073 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
2074 drv->operstate = state;
2075 return wpa_driver_nl80211_send_oper_ifla(
2076 drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
2080 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2081 .name = "nl80211",
2082 .desc = "Linux nl80211/cfg80211",
2083 .get_bssid = wpa_driver_nl80211_get_bssid,
2084 .get_ssid = wpa_driver_nl80211_get_ssid,
2085 .set_wpa = wpa_driver_nl80211_set_wpa,
2086 .set_key = wpa_driver_nl80211_set_key,
2087 .set_countermeasures = wpa_driver_nl80211_set_countermeasures,
2088 .set_drop_unencrypted = wpa_driver_nl80211_set_drop_unencrypted,
2089 .scan = wpa_driver_nl80211_scan,
2090 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
2091 .deauthenticate = wpa_driver_nl80211_deauthenticate,
2092 .disassociate = wpa_driver_nl80211_disassociate,
2093 .associate = wpa_driver_nl80211_associate,
2094 .set_auth_alg = wpa_driver_nl80211_set_auth_alg,
2095 .init = wpa_driver_nl80211_init,
2096 .deinit = wpa_driver_nl80211_deinit,
2097 .add_pmkid = wpa_driver_nl80211_add_pmkid,
2098 .remove_pmkid = wpa_driver_nl80211_remove_pmkid,
2099 .flush_pmkid = wpa_driver_nl80211_flush_pmkid,
2100 .get_capa = wpa_driver_nl80211_get_capa,
2101 .set_operstate = wpa_driver_nl80211_set_operstate,