blob: df309c530902011f2892ac00fb6674b0240bcd95 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Alternatively, this software may be distributed under the terms of BSD
14 * license.
15 *
16 * See README and COPYING for more details.
17 */
18
19#include "includes.h"
20#include <sys/ioctl.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <net/if.h>
25#include <netlink/genl/genl.h>
26#include <netlink/genl/family.h>
27#include <netlink/genl/ctrl.h>
28#include <linux/rtnetlink.h>
29#include <netpacket/packet.h>
30#include <linux/filter.h>
31#include "nl80211_copy.h"
32
33#include "common.h"
34#include "eloop.h"
35#include "utils/list.h"
36#include "common/ieee802_11_defs.h"
37#include "netlink.h"
38#include "linux_ioctl.h"
39#include "radiotap.h"
40#include "radiotap_iter.h"
41#include "rfkill.h"
42#include "driver.h"
Dmitry Shmidt497c1d52011-07-21 15:19:46 -070043#if defined(ANDROID_BRCM_P2P_PATCH) && !defined(HOSTAPD)
44#include "wpa_supplicant_i.h"
45#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#ifdef CONFIG_LIBNL20
47/* libnl 2.0 compatibility code */
48#define nl_handle nl_sock
49#define nl80211_handle_alloc nl_socket_alloc_cb
50#define nl80211_handle_destroy nl_socket_free
51#else
52/*
53 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
54 * but when you free a socket again it will mess up its bitmap and
55 * and use the wrong number the next time it needs a socket ID.
56 * Therefore, we wrap the handle alloc/destroy and add our own pid
57 * accounting.
58 */
59static uint32_t port_bitmap[32] = { 0 };
60
61static struct nl_handle *nl80211_handle_alloc(void *cb)
62{
63 struct nl_handle *handle;
64 uint32_t pid = getpid() & 0x3FFFFF;
65 int i;
66
67 handle = nl_handle_alloc_cb(cb);
68
69 for (i = 0; i < 1024; i++) {
70 if (port_bitmap[i / 32] & (1 << (i % 32)))
71 continue;
72 port_bitmap[i / 32] |= 1 << (i % 32);
73 pid += i << 22;
74 break;
75 }
76
77 nl_socket_set_local_port(handle, pid);
78
79 return handle;
80}
81
82static void nl80211_handle_destroy(struct nl_handle *handle)
83{
84 uint32_t port = nl_socket_get_local_port(handle);
85
86 port >>= 22;
87 port_bitmap[port / 32] &= ~(1 << (port % 32));
88
89 nl_handle_destroy(handle);
90}
91#endif /* CONFIG_LIBNL20 */
92
93
94#ifndef IFF_LOWER_UP
95#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
96#endif
97#ifndef IFF_DORMANT
98#define IFF_DORMANT 0x20000 /* driver signals dormant */
99#endif
100
101#ifndef IF_OPER_DORMANT
102#define IF_OPER_DORMANT 5
103#endif
104#ifndef IF_OPER_UP
105#define IF_OPER_UP 6
106#endif
107
108struct nl80211_global {
109 struct dl_list interfaces;
110};
111
112struct i802_bss {
113 struct wpa_driver_nl80211_data *drv;
114 struct i802_bss *next;
115 int ifindex;
116 char ifname[IFNAMSIZ + 1];
117 char brname[IFNAMSIZ];
118 unsigned int beacon_set:1;
119 unsigned int added_if_into_bridge:1;
120 unsigned int added_bridge:1;
121};
122
123struct wpa_driver_nl80211_data {
124 struct nl80211_global *global;
125 struct dl_list list;
126 u8 addr[ETH_ALEN];
127 char phyname[32];
128 void *ctx;
129 struct netlink_data *netlink;
130 int ioctl_sock; /* socket for ioctl() use */
131 int ifindex;
132 int if_removed;
133 int if_disabled;
134 struct rfkill_data *rfkill;
135 struct wpa_driver_capa capa;
136 int has_capability;
137
138 int operstate;
139
140 int scan_complete_events;
141
142 struct nl_handle *nl_handle;
143 struct nl_handle *nl_handle_event;
144 struct nl_handle *nl_handle_preq;
145 struct nl_cache *nl_cache;
146 struct nl_cache *nl_cache_event;
147 struct nl_cache *nl_cache_preq;
148 struct nl_cb *nl_cb;
149 struct genl_family *nl80211;
150
151 u8 auth_bssid[ETH_ALEN];
152 u8 bssid[ETH_ALEN];
153 int associated;
154 u8 ssid[32];
155 size_t ssid_len;
156 int nlmode;
157 int ap_scan_as_station;
158 unsigned int assoc_freq;
159
160 int monitor_sock;
161 int monitor_ifidx;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700162 int no_monitor_iface_capab;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 int disable_11b_rates;
164
165 unsigned int pending_remain_on_chan:1;
166
167 u64 remain_on_chan_cookie;
168 u64 send_action_cookie;
169
170 unsigned int last_mgmt_freq;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700171 unsigned int ap_oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172
173 struct wpa_driver_scan_filter *filter_ssids;
174 size_t num_filter_ssids;
175
176 struct i802_bss first_bss;
177
178#ifdef HOSTAPD
179 int eapol_sock; /* socket for EAPOL frames */
180
181 int default_if_indices[16];
182 int *if_indices;
183 int num_if_indices;
184
185 int last_freq;
186 int last_freq_ht;
187#endif /* HOSTAPD */
188};
189
190
191static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
192 void *timeout_ctx);
193static int wpa_driver_nl80211_set_mode(void *priv, int mode);
194static int
195wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
196static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
197 const u8 *addr, int cmd, u16 reason_code,
198 int local_state_change);
199static void nl80211_remove_monitor_interface(
200 struct wpa_driver_nl80211_data *drv);
201static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
202 unsigned int freq, unsigned int wait,
203 const u8 *buf, size_t buf_len, u64 *cookie);
204static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700205#ifdef ANDROID_BRCM_P2P_PATCH
206static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
207 enum wpa_event_type type,
208 const u8 *frame, size_t len);
Dmitry Shmidtfc41cad2011-09-28 13:29:53 -0700209int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700210int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration);
211int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow);
212int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
213 const struct wpabuf *proberesp,
214 const struct wpabuf *assocresp);
215#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216
217#ifdef HOSTAPD
218static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
219static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
220static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
221static int wpa_driver_nl80211_if_remove(void *priv,
222 enum wpa_driver_if_type type,
223 const char *ifname);
224#else /* HOSTAPD */
225static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
226{
227 return 0;
228}
229#endif /* HOSTAPD */
230
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700231#ifdef ANDROID
232extern int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
233 size_t buf_len);
234#endif
235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
237static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
238 int ifindex, int disabled);
239
240static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
241
242
Jouni Malinen87fd2792011-05-16 18:35:42 +0300243struct nl80211_bss_info_arg {
244 struct wpa_driver_nl80211_data *drv;
245 struct wpa_scan_results *res;
246 unsigned int assoc_freq;
247};
248
249static int bss_info_handler(struct nl_msg *msg, void *arg);
250
251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252/* nl80211 code */
253static int ack_handler(struct nl_msg *msg, void *arg)
254{
255 int *err = arg;
256 *err = 0;
257 return NL_STOP;
258}
259
260static int finish_handler(struct nl_msg *msg, void *arg)
261{
262 int *ret = arg;
263 *ret = 0;
264 return NL_SKIP;
265}
266
267static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
268 void *arg)
269{
270 int *ret = arg;
271 *ret = err->error;
272 return NL_SKIP;
273}
274
275
276static int no_seq_check(struct nl_msg *msg, void *arg)
277{
278 return NL_OK;
279}
280
281
282static int send_and_recv(struct wpa_driver_nl80211_data *drv,
283 struct nl_handle *nl_handle, struct nl_msg *msg,
284 int (*valid_handler)(struct nl_msg *, void *),
285 void *valid_data)
286{
287 struct nl_cb *cb;
288 int err = -ENOMEM;
289
290 cb = nl_cb_clone(drv->nl_cb);
291 if (!cb)
292 goto out;
293
294 err = nl_send_auto_complete(nl_handle, msg);
295 if (err < 0)
296 goto out;
297
298 err = 1;
299
300 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
301 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
302 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
303
304 if (valid_handler)
305 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
306 valid_handler, valid_data);
307
308 while (err > 0)
309 nl_recvmsgs(nl_handle, cb);
310 out:
311 nl_cb_put(cb);
312 nlmsg_free(msg);
313 return err;
314}
315
316
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700317int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 struct nl_msg *msg,
319 int (*valid_handler)(struct nl_msg *, void *),
320 void *valid_data)
321{
322 return send_and_recv(drv, drv->nl_handle, msg, valid_handler,
323 valid_data);
324}
325
326
327struct family_data {
328 const char *group;
329 int id;
330};
331
332
333static int family_handler(struct nl_msg *msg, void *arg)
334{
335 struct family_data *res = arg;
336 struct nlattr *tb[CTRL_ATTR_MAX + 1];
337 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
338 struct nlattr *mcgrp;
339 int i;
340
341 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
342 genlmsg_attrlen(gnlh, 0), NULL);
343 if (!tb[CTRL_ATTR_MCAST_GROUPS])
344 return NL_SKIP;
345
346 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
347 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
348 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
349 nla_len(mcgrp), NULL);
350 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
351 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
352 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
353 res->group,
354 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
355 continue;
356 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
357 break;
358 };
359
360 return NL_SKIP;
361}
362
363
364static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
365 const char *family, const char *group)
366{
367 struct nl_msg *msg;
368 int ret = -1;
369 struct family_data res = { group, -ENOENT };
370
371 msg = nlmsg_alloc();
372 if (!msg)
373 return -ENOMEM;
374 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
375 0, 0, CTRL_CMD_GETFAMILY, 0);
376 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
377
378 ret = send_and_recv_msgs(drv, msg, family_handler, &res);
379 msg = NULL;
380 if (ret == 0)
381 ret = res.id;
382
383nla_put_failure:
384 nlmsg_free(msg);
385 return ret;
386}
387
388
389static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
390{
391 struct i802_bss *bss = priv;
392 struct wpa_driver_nl80211_data *drv = bss->drv;
393 if (!drv->associated)
394 return -1;
395 os_memcpy(bssid, drv->bssid, ETH_ALEN);
396 return 0;
397}
398
399
400static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
401{
402 struct i802_bss *bss = priv;
403 struct wpa_driver_nl80211_data *drv = bss->drv;
404 if (!drv->associated)
405 return -1;
406 os_memcpy(ssid, drv->ssid, drv->ssid_len);
407 return drv->ssid_len;
408}
409
410
411static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
412 char *buf, size_t len, int del)
413{
414 union wpa_event_data event;
415
416 os_memset(&event, 0, sizeof(event));
417 if (len > sizeof(event.interface_status.ifname))
418 len = sizeof(event.interface_status.ifname) - 1;
419 os_memcpy(event.interface_status.ifname, buf, len);
420 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
421 EVENT_INTERFACE_ADDED;
422
423 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
424 del ? "DEL" : "NEW",
425 event.interface_status.ifname,
426 del ? "removed" : "added");
427
428 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
429 if (del)
430 drv->if_removed = 1;
431 else
432 drv->if_removed = 0;
433 }
434
435 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
436}
437
438
439static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
440 u8 *buf, size_t len)
441{
442 int attrlen, rta_len;
443 struct rtattr *attr;
444
445 attrlen = len;
446 attr = (struct rtattr *) buf;
447
448 rta_len = RTA_ALIGN(sizeof(struct rtattr));
449 while (RTA_OK(attr, attrlen)) {
450 if (attr->rta_type == IFLA_IFNAME) {
451 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
452 == 0)
453 return 1;
454 else
455 break;
456 }
457 attr = RTA_NEXT(attr, attrlen);
458 }
459
460 return 0;
461}
462
463
464static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
465 int ifindex, u8 *buf, size_t len)
466{
467 if (drv->ifindex == ifindex)
468 return 1;
469
470 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
471 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
472 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
473 "interface");
474 wpa_driver_nl80211_finish_drv_init(drv);
475 return 1;
476 }
477
478 return 0;
479}
480
481
482static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
483 struct ifinfomsg *ifi,
484 u8 *buf, size_t len)
485{
486 struct wpa_driver_nl80211_data *drv = ctx;
487 int attrlen, rta_len;
488 struct rtattr *attr;
489 u32 brid = 0;
490
491 if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len) &&
492 !have_ifidx(drv, ifi->ifi_index)) {
493 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
494 "ifindex %d", ifi->ifi_index);
495 return;
496 }
497
498 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
499 "(%s%s%s%s)",
500 drv->operstate, ifi->ifi_flags,
501 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
502 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
503 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
504 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
505
506 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
507 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
508 drv->if_disabled = 1;
509 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
510 }
511
512 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
513 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
514 drv->if_disabled = 0;
515 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
516 }
517
518 /*
519 * Some drivers send the association event before the operup event--in
520 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
521 * fails. This will hit us when wpa_supplicant does not need to do
522 * IEEE 802.1X authentication
523 */
524 if (drv->operstate == 1 &&
525 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
526 !(ifi->ifi_flags & IFF_RUNNING))
527 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
528 -1, IF_OPER_UP);
529
530 attrlen = len;
531 attr = (struct rtattr *) buf;
532 rta_len = RTA_ALIGN(sizeof(struct rtattr));
533 while (RTA_OK(attr, attrlen)) {
534 if (attr->rta_type == IFLA_IFNAME) {
535 wpa_driver_nl80211_event_link(
536 drv,
537 ((char *) attr) + rta_len,
538 attr->rta_len - rta_len, 0);
539 } else if (attr->rta_type == IFLA_MASTER)
540 brid = nla_get_u32((struct nlattr *) attr);
541 attr = RTA_NEXT(attr, attrlen);
542 }
543
544#ifdef HOSTAPD
545 if (ifi->ifi_family == AF_BRIDGE && brid) {
546 /* device has been added to bridge */
547 char namebuf[IFNAMSIZ];
548 if_indextoname(brid, namebuf);
549 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
550 brid, namebuf);
551 add_ifidx(drv, brid);
552 }
553#endif /* HOSTAPD */
554}
555
556
557static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
558 struct ifinfomsg *ifi,
559 u8 *buf, size_t len)
560{
561 struct wpa_driver_nl80211_data *drv = ctx;
562 int attrlen, rta_len;
563 struct rtattr *attr;
564 u32 brid = 0;
565
566 attrlen = len;
567 attr = (struct rtattr *) buf;
568
569 rta_len = RTA_ALIGN(sizeof(struct rtattr));
570 while (RTA_OK(attr, attrlen)) {
571 if (attr->rta_type == IFLA_IFNAME) {
572 wpa_driver_nl80211_event_link(
573 drv,
574 ((char *) attr) + rta_len,
575 attr->rta_len - rta_len, 1);
576 } else if (attr->rta_type == IFLA_MASTER)
577 brid = nla_get_u32((struct nlattr *) attr);
578 attr = RTA_NEXT(attr, attrlen);
579 }
580
581#ifdef HOSTAPD
582 if (ifi->ifi_family == AF_BRIDGE && brid) {
583 /* device has been removed from bridge */
584 char namebuf[IFNAMSIZ];
585 if_indextoname(brid, namebuf);
586 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
587 "%s", brid, namebuf);
588 del_ifidx(drv, brid);
589 }
590#endif /* HOSTAPD */
591}
592
593
594static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
595 const u8 *frame, size_t len)
596{
597 const struct ieee80211_mgmt *mgmt;
598 union wpa_event_data event;
599
600 mgmt = (const struct ieee80211_mgmt *) frame;
601 if (len < 24 + sizeof(mgmt->u.auth)) {
602 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
603 "frame");
604 return;
605 }
606
607 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
608 os_memset(&event, 0, sizeof(event));
609 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
610 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
611 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
612 if (len > 24 + sizeof(mgmt->u.auth)) {
613 event.auth.ies = mgmt->u.auth.variable;
614 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
615 }
616
617 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
618}
619
620
Jouni Malinen87fd2792011-05-16 18:35:42 +0300621static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
622{
623 struct nl_msg *msg;
624 int ret;
625 struct nl80211_bss_info_arg arg;
626
627 os_memset(&arg, 0, sizeof(arg));
628 msg = nlmsg_alloc();
629 if (!msg)
630 goto nla_put_failure;
631
632 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
633 NL80211_CMD_GET_SCAN, 0);
634 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
635
636 arg.drv = drv;
637 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
638 msg = NULL;
639 if (ret == 0) {
640 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
641 "associated BSS from scan results: %u MHz",
642 arg.assoc_freq);
643 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
644 }
645 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
646 "(%s)", ret, strerror(-ret));
647nla_put_failure:
648 nlmsg_free(msg);
649 return drv->assoc_freq;
650}
651
652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
654 const u8 *frame, size_t len)
655{
656 const struct ieee80211_mgmt *mgmt;
657 union wpa_event_data event;
658 u16 status;
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700659#ifdef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700660 struct wpa_supplicant *wpa_s = drv->ctx;
661#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662
663 mgmt = (const struct ieee80211_mgmt *) frame;
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700664#if (defined (CONFIG_AP) || defined (HOSTAPD) ) && defined (ANDROID_BRCM_P2P_PATCH)
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700665 if (drv->nlmode == NL80211_IFTYPE_AP || drv->nlmode == NL80211_IFTYPE_P2P_GO) {
666 if (len < 24 + sizeof(mgmt->u.assoc_req)) {
667 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
668 "frame");
669 return;
670 }
671 os_memset(&event, 0, sizeof(event));
672 event.assoc_info.freq = drv->assoc_freq;
673 event.assoc_info.req_ies = (u8 *) mgmt->u.assoc_req.variable;
674 event.assoc_info.req_ies_len = len - 24 - sizeof(mgmt->u.assoc_req);
675 event.assoc_info.addr = mgmt->sa;
676 } else {
677#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
679 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
680 "frame");
681 return;
682 }
683
684 status = le_to_host16(mgmt->u.assoc_resp.status_code);
685 if (status != WLAN_STATUS_SUCCESS) {
686 os_memset(&event, 0, sizeof(event));
687 event.assoc_reject.bssid = mgmt->bssid;
688 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
689 event.assoc_reject.resp_ies =
690 (u8 *) mgmt->u.assoc_resp.variable;
691 event.assoc_reject.resp_ies_len =
692 len - 24 - sizeof(mgmt->u.assoc_resp);
693 }
694 event.assoc_reject.status_code = status;
695
696 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
697 return;
698 }
699
700 drv->associated = 1;
701 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
702
703 os_memset(&event, 0, sizeof(event));
704 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
705 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
706 event.assoc_info.resp_ies_len =
707 len - 24 - sizeof(mgmt->u.assoc_resp);
708 }
709
710 event.assoc_info.freq = drv->assoc_freq;
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700711#if (defined (CONFIG_AP) || defined(HOSTAPD)) && defined (ANDROID_BRCM_P2P_PATCH)
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700712 }
713#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
715}
716
717
718static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
719 enum nl80211_commands cmd, struct nlattr *status,
720 struct nlattr *addr, struct nlattr *req_ie,
721 struct nlattr *resp_ie)
722{
723 union wpa_event_data event;
724
725 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
726 /*
727 * Avoid reporting two association events that would confuse
728 * the core code.
729 */
730 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
731 "when using userspace SME", cmd);
732 return;
733 }
734
735 os_memset(&event, 0, sizeof(event));
736 if (cmd == NL80211_CMD_CONNECT &&
737 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
738 if (addr)
739 event.assoc_reject.bssid = nla_data(addr);
740 if (resp_ie) {
741 event.assoc_reject.resp_ies = nla_data(resp_ie);
742 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
743 }
744 event.assoc_reject.status_code = nla_get_u16(status);
745 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
746 return;
747 }
748
749 drv->associated = 1;
750 if (addr)
751 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
752
753 if (req_ie) {
754 event.assoc_info.req_ies = nla_data(req_ie);
755 event.assoc_info.req_ies_len = nla_len(req_ie);
756 }
757 if (resp_ie) {
758 event.assoc_info.resp_ies = nla_data(resp_ie);
759 event.assoc_info.resp_ies_len = nla_len(resp_ie);
760 }
761
Jouni Malinen87fd2792011-05-16 18:35:42 +0300762 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
765}
766
767
768static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
769 enum nl80211_commands cmd, struct nlattr *addr)
770{
771 union wpa_event_data event;
772 enum wpa_event_type ev;
773
774 if (nla_len(addr) != ETH_ALEN)
775 return;
776
777 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
778 cmd, MAC2STR((u8 *) nla_data(addr)));
779
780 if (cmd == NL80211_CMD_AUTHENTICATE)
781 ev = EVENT_AUTH_TIMED_OUT;
782 else if (cmd == NL80211_CMD_ASSOCIATE)
783 ev = EVENT_ASSOC_TIMED_OUT;
784 else
785 return;
786
787 os_memset(&event, 0, sizeof(event));
788 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
789 wpa_supplicant_event(drv->ctx, ev, &event);
790}
791
792
793static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
794 struct nlattr *freq, const u8 *frame, size_t len)
795{
796 const struct ieee80211_mgmt *mgmt;
797 union wpa_event_data event;
798 u16 fc, stype;
799
800 mgmt = (const struct ieee80211_mgmt *) frame;
801 if (len < 24) {
802 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
803 return;
804 }
805
806 fc = le_to_host16(mgmt->frame_control);
807 stype = WLAN_FC_GET_STYPE(fc);
808
809 os_memset(&event, 0, sizeof(event));
810 if (freq) {
811 event.rx_action.freq = nla_get_u32(freq);
812 drv->last_mgmt_freq = event.rx_action.freq;
813 }
814 if (stype == WLAN_FC_STYPE_ACTION) {
815 event.rx_action.da = mgmt->da;
816 event.rx_action.sa = mgmt->sa;
817 event.rx_action.bssid = mgmt->bssid;
818 event.rx_action.category = mgmt->u.action.category;
819 event.rx_action.data = &mgmt->u.action.category + 1;
820 event.rx_action.len = frame + len - event.rx_action.data;
821 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -0700822#ifdef ANDROID_BRCM_P2P_PATCH
823 } else if (stype == WLAN_FC_STYPE_ASSOC_REQ) {
824 mlme_event_assoc(drv, frame, len);
825 } else if (stype == WLAN_FC_STYPE_DISASSOC) {
826 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, frame, len);
827 } else if (stype == WLAN_FC_STYPE_DEAUTH) {
828 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, frame, len);
829#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 } else {
831 event.rx_mgmt.frame = frame;
832 event.rx_mgmt.frame_len = len;
833 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
834 }
835}
836
837
838static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
839 struct nlattr *cookie, const u8 *frame,
840 size_t len, struct nlattr *ack)
841{
842 union wpa_event_data event;
843 const struct ieee80211_hdr *hdr;
844 u16 fc;
845 u64 cookie_val;
846
847 if (!cookie)
848 return;
849
850 cookie_val = nla_get_u64(cookie);
851 wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s "
852 "(ack=%d)",
853 (long long unsigned int) cookie_val,
854 cookie_val == drv->send_action_cookie ?
855 " (match)" : " (unknown)", ack != NULL);
856 if (cookie_val != drv->send_action_cookie)
857 return;
858
859 hdr = (const struct ieee80211_hdr *) frame;
860 fc = le_to_host16(hdr->frame_control);
861
862 os_memset(&event, 0, sizeof(event));
863 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
864 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
865 event.tx_status.dst = hdr->addr1;
866 event.tx_status.data = frame;
867 event.tx_status.data_len = len;
868 event.tx_status.ack = ack != NULL;
869 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
870}
871
872
873static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
874 enum wpa_event_type type,
875 const u8 *frame, size_t len)
876{
877 const struct ieee80211_mgmt *mgmt;
878 union wpa_event_data event;
879 const u8 *bssid = NULL;
880 u16 reason_code = 0;
881
882 mgmt = (const struct ieee80211_mgmt *) frame;
883 if (len >= 24) {
884 bssid = mgmt->bssid;
885
886 if (drv->associated != 0 &&
887 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
888 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
889 /*
890 * We have presumably received this deauth as a
891 * response to a clear_state_mismatch() outgoing
892 * deauth. Don't let it take us offline!
893 */
894 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
895 "from Unknown BSSID " MACSTR " -- ignoring",
896 MAC2STR(bssid));
897 return;
898 }
899 }
900
901 drv->associated = 0;
902 os_memset(&event, 0, sizeof(event));
903
904 /* Note: Same offset for Reason Code in both frame subtypes */
905 if (len >= 24 + sizeof(mgmt->u.deauth))
906 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
907
908 if (type == EVENT_DISASSOC) {
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700909#ifdef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700910 if (drv->nlmode == NL80211_IFTYPE_AP ||
911 drv->nlmode == NL80211_IFTYPE_P2P_GO) {
912 event.disassoc_info.addr = mgmt->sa;
913 } else
914#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 event.disassoc_info.addr = bssid;
916 event.disassoc_info.reason_code = reason_code;
917 if (frame + len > mgmt->u.disassoc.variable) {
918 event.disassoc_info.ie = mgmt->u.disassoc.variable;
919 event.disassoc_info.ie_len = frame + len -
920 mgmt->u.disassoc.variable;
921 }
922 } else {
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700923#ifdef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700924 if (drv->nlmode == NL80211_IFTYPE_AP ||
925 drv->nlmode == NL80211_IFTYPE_P2P_GO) {
926 event.deauth_info.addr = mgmt->sa;
927 } else
928#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 event.deauth_info.addr = bssid;
930 event.deauth_info.reason_code = reason_code;
931 if (frame + len > mgmt->u.deauth.variable) {
932 event.deauth_info.ie = mgmt->u.deauth.variable;
933 event.deauth_info.ie_len = frame + len -
934 mgmt->u.deauth.variable;
935 }
936 }
937
938 wpa_supplicant_event(drv->ctx, type, &event);
939}
940
941
942static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
943 enum wpa_event_type type,
944 const u8 *frame, size_t len)
945{
946 const struct ieee80211_mgmt *mgmt;
947 union wpa_event_data event;
948 u16 reason_code = 0;
949
950 if (len < 24)
951 return;
952
953 mgmt = (const struct ieee80211_mgmt *) frame;
954
955 os_memset(&event, 0, sizeof(event));
956 /* Note: Same offset for Reason Code in both frame subtypes */
957 if (len >= 24 + sizeof(mgmt->u.deauth))
958 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
959
960 if (type == EVENT_UNPROT_DISASSOC) {
961 event.unprot_disassoc.sa = mgmt->sa;
962 event.unprot_disassoc.da = mgmt->da;
963 event.unprot_disassoc.reason_code = reason_code;
964 } else {
965 event.unprot_deauth.sa = mgmt->sa;
966 event.unprot_deauth.da = mgmt->da;
967 event.unprot_deauth.reason_code = reason_code;
968 }
969
970 wpa_supplicant_event(drv->ctx, type, &event);
971}
972
973
974static void mlme_event(struct wpa_driver_nl80211_data *drv,
975 enum nl80211_commands cmd, struct nlattr *frame,
976 struct nlattr *addr, struct nlattr *timed_out,
977 struct nlattr *freq, struct nlattr *ack,
978 struct nlattr *cookie)
979{
980 if (timed_out && addr) {
981 mlme_timeout_event(drv, cmd, addr);
982 return;
983 }
984
985 if (frame == NULL) {
986 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
987 "data", cmd);
988 return;
989 }
990
991 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
992 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
993 nla_data(frame), nla_len(frame));
994
995 switch (cmd) {
996 case NL80211_CMD_AUTHENTICATE:
997 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
998 break;
999 case NL80211_CMD_ASSOCIATE:
1000 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1001 break;
1002 case NL80211_CMD_DEAUTHENTICATE:
1003 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1004 nla_data(frame), nla_len(frame));
1005 break;
1006 case NL80211_CMD_DISASSOCIATE:
1007 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1008 nla_data(frame), nla_len(frame));
1009 break;
1010 case NL80211_CMD_FRAME:
1011 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
1012 break;
1013 case NL80211_CMD_FRAME_TX_STATUS:
1014 mlme_event_action_tx_status(drv, cookie, nla_data(frame),
1015 nla_len(frame), ack);
1016 break;
1017 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1018 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1019 nla_data(frame), nla_len(frame));
1020 break;
1021 case NL80211_CMD_UNPROT_DISASSOCIATE:
1022 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1023 nla_data(frame), nla_len(frame));
1024 break;
1025 default:
1026 break;
1027 }
1028}
1029
1030
1031static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1032 struct nlattr *tb[])
1033{
1034 union wpa_event_data data;
1035
1036 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1037 os_memset(&data, 0, sizeof(data));
1038 if (tb[NL80211_ATTR_MAC]) {
1039 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1040 nla_data(tb[NL80211_ATTR_MAC]),
1041 nla_len(tb[NL80211_ATTR_MAC]));
1042 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1043 }
1044 if (tb[NL80211_ATTR_KEY_SEQ]) {
1045 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1046 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1047 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1048 }
1049 if (tb[NL80211_ATTR_KEY_TYPE]) {
1050 enum nl80211_key_type key_type =
1051 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1052 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1053 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1054 data.michael_mic_failure.unicast = 1;
1055 } else
1056 data.michael_mic_failure.unicast = 1;
1057
1058 if (tb[NL80211_ATTR_KEY_IDX]) {
1059 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1060 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1061 }
1062
1063 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1064}
1065
1066
1067static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1068 struct nlattr *tb[])
1069{
1070 if (tb[NL80211_ATTR_MAC] == NULL) {
1071 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1072 "event");
1073 return;
1074 }
1075 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1076 drv->associated = 1;
1077 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1078 MAC2STR(drv->bssid));
1079
1080 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1081}
1082
1083
1084static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1085 int cancel_event, struct nlattr *tb[])
1086{
1087 unsigned int freq, chan_type, duration;
1088 union wpa_event_data data;
1089 u64 cookie;
1090
1091 if (tb[NL80211_ATTR_WIPHY_FREQ])
1092 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1093 else
1094 freq = 0;
1095
1096 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1097 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1098 else
1099 chan_type = 0;
1100
1101 if (tb[NL80211_ATTR_DURATION])
1102 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1103 else
1104 duration = 0;
1105
1106 if (tb[NL80211_ATTR_COOKIE])
1107 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1108 else
1109 cookie = 0;
1110
1111 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1112 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1113 cancel_event, freq, chan_type, duration,
1114 (long long unsigned int) cookie,
1115 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1116
1117 if (cookie != drv->remain_on_chan_cookie)
1118 return; /* not for us */
1119
1120 drv->pending_remain_on_chan = !cancel_event;
1121
1122 os_memset(&data, 0, sizeof(data));
1123 data.remain_on_channel.freq = freq;
1124 data.remain_on_channel.duration = duration;
1125 wpa_supplicant_event(drv->ctx, cancel_event ?
1126 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1127 EVENT_REMAIN_ON_CHANNEL, &data);
1128}
1129
1130
1131static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1132 struct nlattr *tb[])
1133{
1134 union wpa_event_data event;
1135 struct nlattr *nl;
1136 int rem;
1137 struct scan_info *info;
1138#define MAX_REPORT_FREQS 50
1139 int freqs[MAX_REPORT_FREQS];
1140 int num_freqs = 0;
1141
1142 os_memset(&event, 0, sizeof(event));
1143 info = &event.scan_info;
1144 info->aborted = aborted;
1145
1146 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1147 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1148 struct wpa_driver_scan_ssid *s =
1149 &info->ssids[info->num_ssids];
1150 s->ssid = nla_data(nl);
1151 s->ssid_len = nla_len(nl);
1152 info->num_ssids++;
1153 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1154 break;
1155 }
1156 }
1157 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1158 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1159 {
1160 freqs[num_freqs] = nla_get_u32(nl);
1161 num_freqs++;
1162 if (num_freqs == MAX_REPORT_FREQS - 1)
1163 break;
1164 }
1165 info->freqs = freqs;
1166 info->num_freqs = num_freqs;
1167 }
1168 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1169}
1170
1171
1172static int get_link_signal(struct nl_msg *msg, void *arg)
1173{
1174 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1175 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1176 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1177 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1178 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1179 };
1180 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1181 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1182 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1183 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1184 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1185 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1186 };
1187 struct wpa_signal_info *sig_change = arg;
1188
1189 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1190 genlmsg_attrlen(gnlh, 0), NULL);
1191 if (!tb[NL80211_ATTR_STA_INFO] ||
1192 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1193 tb[NL80211_ATTR_STA_INFO], policy))
1194 return NL_SKIP;
1195 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1196 return NL_SKIP;
1197
1198 sig_change->current_signal =
1199 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1200
1201 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1202 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1203 sinfo[NL80211_STA_INFO_TX_BITRATE],
1204 rate_policy)) {
1205 sig_change->current_txrate = 0;
1206 } else {
1207 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1208 sig_change->current_txrate =
1209 nla_get_u16(rinfo[
1210 NL80211_RATE_INFO_BITRATE]) * 100;
1211 }
1212 }
1213 }
1214
1215 return NL_SKIP;
1216}
1217
1218
1219static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1220 struct wpa_signal_info *sig)
1221{
1222 struct nl_msg *msg;
1223
1224 sig->current_signal = -9999;
1225 sig->current_txrate = 0;
1226
1227 msg = nlmsg_alloc();
1228 if (!msg)
1229 return -ENOMEM;
1230
1231 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1232 0, NL80211_CMD_GET_STATION, 0);
1233
1234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1235 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1236
1237 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1238 nla_put_failure:
1239 return -ENOBUFS;
1240}
1241
1242
1243static int get_link_noise(struct nl_msg *msg, void *arg)
1244{
1245 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1246 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1247 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1248 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1249 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1250 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1251 };
1252 struct wpa_signal_info *sig_change = arg;
1253
1254 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1255 genlmsg_attrlen(gnlh, 0), NULL);
1256
1257 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1258 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1259 return NL_SKIP;
1260 }
1261
1262 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1263 tb[NL80211_ATTR_SURVEY_INFO],
1264 survey_policy)) {
1265 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1266 "attributes!");
1267 return NL_SKIP;
1268 }
1269
1270 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1271 return NL_SKIP;
1272
1273 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1274 sig_change->frequency)
1275 return NL_SKIP;
1276
1277 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1278 return NL_SKIP;
1279
1280 sig_change->current_noise =
1281 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1282
1283 return NL_SKIP;
1284}
1285
1286
1287static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1288 struct wpa_signal_info *sig_change)
1289{
1290 struct nl_msg *msg;
1291
1292 sig_change->current_noise = 9999;
1293 sig_change->frequency = drv->assoc_freq;
1294
1295 msg = nlmsg_alloc();
1296 if (!msg)
1297 return -ENOMEM;
1298
1299 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1300 NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0);
1301
1302 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1303
1304 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1305 nla_put_failure:
1306 return -ENOBUFS;
1307}
1308
1309
1310static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1311 struct nlattr *tb[])
1312{
1313 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1314 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1315 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1316 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1317 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1318 };
1319 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1320 enum nl80211_cqm_rssi_threshold_event event;
1321 union wpa_event_data ed;
1322 struct wpa_signal_info sig;
1323 int res;
1324
1325 if (tb[NL80211_ATTR_CQM] == NULL ||
1326 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1327 cqm_policy)) {
1328 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1329 return;
1330 }
1331
1332 os_memset(&ed, 0, sizeof(ed));
1333
1334 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1335 if (!tb[NL80211_ATTR_MAC])
1336 return;
1337 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1338 ETH_ALEN);
1339 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1340 return;
1341 }
1342
1343 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1344 return;
1345 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1346
1347 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1348 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1349 "event: RSSI high");
1350 ed.signal_change.above_threshold = 1;
1351 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1352 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1353 "event: RSSI low");
1354 ed.signal_change.above_threshold = 0;
1355 } else
1356 return;
1357
1358 res = nl80211_get_link_signal(drv, &sig);
1359 if (res == 0) {
1360 ed.signal_change.current_signal = sig.current_signal;
1361 ed.signal_change.current_txrate = sig.current_txrate;
1362 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1363 sig.current_signal, sig.current_txrate);
1364 }
1365
1366 res = nl80211_get_link_noise(drv, &sig);
1367 if (res == 0) {
1368 ed.signal_change.current_noise = sig.current_noise;
1369 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1370 sig.current_noise);
1371 }
1372
1373 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1374}
1375
1376
1377static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1378 struct nlattr **tb)
1379{
1380 u8 *addr;
1381 union wpa_event_data data;
1382
1383 if (tb[NL80211_ATTR_MAC] == NULL)
1384 return;
1385 addr = nla_data(tb[NL80211_ATTR_MAC]);
1386 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001387
1388 if (drv->nlmode == NL80211_IFTYPE_AP &&
1389 drv->no_monitor_iface_capab) {
1390 u8 *ies = NULL;
1391 size_t ies_len = 0;
1392 if (tb[NL80211_ATTR_IE]) {
1393 ies = nla_data(tb[NL80211_ATTR_IE]);
1394 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1395 }
1396 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1397 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1398 return;
1399 }
1400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1402 return;
1403
1404 os_memset(&data, 0, sizeof(data));
1405 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1406 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1407}
1408
1409
1410static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1411 struct nlattr **tb)
1412{
1413 u8 *addr;
1414 union wpa_event_data data;
1415
1416 if (tb[NL80211_ATTR_MAC] == NULL)
1417 return;
1418 addr = nla_data(tb[NL80211_ATTR_MAC]);
1419 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1420 MAC2STR(addr));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07001421
1422 if (drv->nlmode == NL80211_IFTYPE_AP &&
1423 drv->no_monitor_iface_capab) {
1424 drv_event_disassoc(drv->ctx, addr);
1425 return;
1426 }
1427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1429 return;
1430
1431 os_memset(&data, 0, sizeof(data));
1432 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1433 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1434}
1435
1436
1437static int process_event(struct nl_msg *msg, void *arg)
1438{
1439 struct wpa_driver_nl80211_data *drv = arg;
1440 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1441 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1442 union wpa_event_data data;
1443
1444 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1445 genlmsg_attrlen(gnlh, 0), NULL);
1446
1447 if (tb[NL80211_ATTR_IFINDEX]) {
1448 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1449 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
1450 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
1451 " for foreign interface (ifindex %d)",
1452 gnlh->cmd, ifindex);
1453 return NL_SKIP;
1454 }
1455 }
1456
1457 if (drv->ap_scan_as_station &&
1458 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
1459 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
1460 wpa_driver_nl80211_set_mode(&drv->first_bss,
1461 IEEE80211_MODE_AP);
1462 drv->ap_scan_as_station = 0;
1463 }
1464
1465 switch (gnlh->cmd) {
1466 case NL80211_CMD_TRIGGER_SCAN:
1467 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
1468 break;
1469 case NL80211_CMD_NEW_SCAN_RESULTS:
1470 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
1471 drv->scan_complete_events = 1;
1472 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1473 drv->ctx);
1474 send_scan_event(drv, 0, tb);
1475 break;
1476 case NL80211_CMD_SCAN_ABORTED:
1477 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
1478 /*
1479 * Need to indicate that scan results are available in order
1480 * not to make wpa_supplicant stop its scanning.
1481 */
1482 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1483 drv->ctx);
1484 send_scan_event(drv, 1, tb);
1485 break;
1486 case NL80211_CMD_AUTHENTICATE:
1487 case NL80211_CMD_ASSOCIATE:
1488 case NL80211_CMD_DEAUTHENTICATE:
1489 case NL80211_CMD_DISASSOCIATE:
1490 case NL80211_CMD_FRAME:
1491 case NL80211_CMD_FRAME_TX_STATUS:
1492 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1493 case NL80211_CMD_UNPROT_DISASSOCIATE:
1494 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
1495 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
1496 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
1497 tb[NL80211_ATTR_COOKIE]);
1498 break;
1499 case NL80211_CMD_CONNECT:
1500 case NL80211_CMD_ROAM:
1501 mlme_event_connect(drv, gnlh->cmd,
1502 tb[NL80211_ATTR_STATUS_CODE],
1503 tb[NL80211_ATTR_MAC],
1504 tb[NL80211_ATTR_REQ_IE],
1505 tb[NL80211_ATTR_RESP_IE]);
1506 break;
1507 case NL80211_CMD_DISCONNECT:
1508 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1509 /*
1510 * Avoid reporting two disassociation events that could
1511 * confuse the core code.
1512 */
1513 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1514 "event when using userspace SME");
1515 break;
1516 }
1517 drv->associated = 0;
1518 os_memset(&data, 0, sizeof(data));
Dmitry Shmidtcf5b2572011-12-19 13:28:51 -08001519 if (tb[NL80211_ATTR_REASON_CODE])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520 data.disassoc_info.reason_code =
1521 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
1522 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
1523 break;
1524 case NL80211_CMD_MICHAEL_MIC_FAILURE:
1525 mlme_event_michael_mic_failure(drv, tb);
1526 break;
1527 case NL80211_CMD_JOIN_IBSS:
1528 mlme_event_join_ibss(drv, tb);
1529 break;
1530 case NL80211_CMD_REMAIN_ON_CHANNEL:
1531 mlme_event_remain_on_channel(drv, 0, tb);
1532 break;
1533 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
1534 mlme_event_remain_on_channel(drv, 1, tb);
1535 break;
1536 case NL80211_CMD_NOTIFY_CQM:
1537 nl80211_cqm_event(drv, tb);
1538 break;
1539 case NL80211_CMD_REG_CHANGE:
1540 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
1541 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1542 NULL);
1543 break;
1544 case NL80211_CMD_REG_BEACON_HINT:
1545 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
1546 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1547 NULL);
1548 break;
1549 case NL80211_CMD_NEW_STATION:
1550 nl80211_new_station_event(drv, tb);
1551 break;
1552 case NL80211_CMD_DEL_STATION:
1553 nl80211_del_station_event(drv, tb);
1554 break;
1555 default:
1556 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
1557 "(cmd=%d)", gnlh->cmd);
1558 break;
1559 }
1560
1561 return NL_SKIP;
1562}
1563
1564
1565static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1566 void *handle)
1567{
1568 struct nl_cb *cb;
1569 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1570
1571 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
1572
1573 cb = nl_cb_clone(drv->nl_cb);
1574 if (!cb)
1575 return;
1576 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1577 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
1578 nl_recvmsgs(handle, cb);
1579 nl_cb_put(cb);
1580}
1581
1582
1583/**
1584 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1585 * @priv: driver_nl80211 private data
1586 * @alpha2_arg: country to which to switch to
1587 * Returns: 0 on success, -1 on failure
1588 *
1589 * This asks nl80211 to set the regulatory domain for given
1590 * country ISO / IEC alpha2.
1591 */
1592static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1593{
1594 struct i802_bss *bss = priv;
1595 struct wpa_driver_nl80211_data *drv = bss->drv;
1596 char alpha2[3];
1597 struct nl_msg *msg;
1598
1599 msg = nlmsg_alloc();
1600 if (!msg)
1601 return -ENOMEM;
1602
1603 alpha2[0] = alpha2_arg[0];
1604 alpha2[1] = alpha2_arg[1];
1605 alpha2[2] = '\0';
1606
1607 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1608 0, NL80211_CMD_REQ_SET_REG, 0);
1609
1610 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1611 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1612 return -EINVAL;
1613 return 0;
1614nla_put_failure:
1615 return -EINVAL;
1616}
1617
1618
1619struct wiphy_info_data {
1620 int max_scan_ssids;
1621 int ap_supported;
1622 int p2p_supported;
1623 int auth_supported;
1624 int connect_supported;
1625 int offchan_tx_supported;
1626 int max_remain_on_chan;
1627};
1628
1629
1630static int wiphy_info_handler(struct nl_msg *msg, void *arg)
1631{
1632 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1633 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1634 struct wiphy_info_data *info = arg;
1635 int p2p_go_supported = 0, p2p_client_supported = 0;
1636
1637 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1638 genlmsg_attrlen(gnlh, 0), NULL);
1639
1640 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
1641 info->max_scan_ssids =
1642 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
1643
1644 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
1645 struct nlattr *nl_mode;
1646 int i;
1647 nla_for_each_nested(nl_mode,
1648 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
1649 switch (nla_type(nl_mode)) {
1650 case NL80211_IFTYPE_AP:
1651 info->ap_supported = 1;
1652 break;
1653 case NL80211_IFTYPE_P2P_GO:
1654 p2p_go_supported = 1;
1655 break;
1656 case NL80211_IFTYPE_P2P_CLIENT:
1657 p2p_client_supported = 1;
1658 break;
1659 }
1660 }
1661 }
1662
1663 info->p2p_supported = p2p_go_supported && p2p_client_supported;
1664
1665 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
1666 struct nlattr *nl_cmd;
1667 int i;
1668
1669 nla_for_each_nested(nl_cmd,
1670 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
1671 u32 cmd = nla_get_u32(nl_cmd);
1672 if (cmd == NL80211_CMD_AUTHENTICATE)
1673 info->auth_supported = 1;
1674 else if (cmd == NL80211_CMD_CONNECT)
1675 info->connect_supported = 1;
1676 }
1677 }
1678
1679 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK])
1680 info->offchan_tx_supported = 1;
1681
1682 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
1683 info->max_remain_on_chan =
1684 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
1685
1686 return NL_SKIP;
1687}
1688
1689
1690static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
1691 struct wiphy_info_data *info)
1692{
1693 struct nl_msg *msg;
1694
1695 os_memset(info, 0, sizeof(*info));
1696
1697 /* default to 5000 since early versions of mac80211 don't set it */
1698 info->max_remain_on_chan = 5000;
1699
1700 msg = nlmsg_alloc();
1701 if (!msg)
1702 return -1;
1703
1704 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1705 0, NL80211_CMD_GET_WIPHY, 0);
1706
1707 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
1708
1709 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
1710 return 0;
1711 msg = NULL;
1712nla_put_failure:
1713 nlmsg_free(msg);
1714 return -1;
1715}
1716
1717
1718static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
1719{
1720 struct wiphy_info_data info;
1721 if (wpa_driver_nl80211_get_info(drv, &info))
1722 return -1;
1723 drv->has_capability = 1;
1724 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1725 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1726 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1727 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1728 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1729 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
1730 WPA_DRIVER_CAPA_ENC_WEP104 |
1731 WPA_DRIVER_CAPA_ENC_TKIP |
1732 WPA_DRIVER_CAPA_ENC_CCMP;
1733 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1734 WPA_DRIVER_AUTH_SHARED |
1735 WPA_DRIVER_AUTH_LEAP;
1736
1737 drv->capa.max_scan_ssids = info.max_scan_ssids;
1738 if (info.ap_supported)
1739 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1740
1741 if (info.auth_supported)
1742 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
1743 else if (!info.connect_supported) {
1744 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
1745 "authentication/association or connect commands");
1746 return -1;
1747 }
1748
1749 if (info.offchan_tx_supported) {
1750 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
1751 "off-channel TX");
1752 drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
1753 }
1754
1755 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
1756 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1757 if (info.p2p_supported)
1758 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
1759 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1760 drv->capa.max_remain_on_chan = info.max_remain_on_chan;
1761
1762 return 0;
1763}
1764
1765
1766static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
1767{
1768 int ret;
1769
1770 /* Initialize generic netlink and nl80211 */
1771
1772 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1773 if (drv->nl_cb == NULL) {
1774 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1775 "callbacks");
1776 goto err1;
1777 }
1778
1779 drv->nl_handle = nl80211_handle_alloc(drv->nl_cb);
1780 if (drv->nl_handle == NULL) {
1781 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1782 "callbacks");
1783 goto err2;
1784 }
1785
1786 drv->nl_handle_event = nl80211_handle_alloc(drv->nl_cb);
1787 if (drv->nl_handle_event == NULL) {
1788 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1789 "callbacks (event)");
1790 goto err2b;
1791 }
1792
1793 if (genl_connect(drv->nl_handle)) {
1794 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1795 "netlink");
1796 goto err3;
1797 }
1798
1799 if (genl_connect(drv->nl_handle_event)) {
1800 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1801 "netlink (event)");
1802 goto err3;
1803 }
1804
1805#ifdef CONFIG_LIBNL20
1806 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
1807 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1808 "netlink cache");
1809 goto err3;
1810 }
1811 if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
1812 0) {
1813 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1814 "netlink cache (event)");
1815 goto err3b;
1816 }
1817#else /* CONFIG_LIBNL20 */
1818 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1819 if (drv->nl_cache == NULL) {
1820 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1821 "netlink cache");
1822 goto err3;
1823 }
1824 drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
1825 if (drv->nl_cache_event == NULL) {
1826 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1827 "netlink cache (event)");
1828 goto err3b;
1829 }
1830#endif /* CONFIG_LIBNL20 */
1831
1832 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1833 if (drv->nl80211 == NULL) {
1834 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1835 "found");
1836 goto err4;
1837 }
1838
1839 ret = nl_get_multicast_id(drv, "nl80211", "scan");
1840 if (ret >= 0)
1841 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1842 if (ret < 0) {
1843 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1844 "membership for scan events: %d (%s)",
1845 ret, strerror(-ret));
1846 goto err4;
1847 }
1848
1849 ret = nl_get_multicast_id(drv, "nl80211", "mlme");
1850 if (ret >= 0)
1851 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1852 if (ret < 0) {
1853 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1854 "membership for mlme events: %d (%s)",
1855 ret, strerror(-ret));
1856 goto err4;
1857 }
1858
1859 ret = nl_get_multicast_id(drv, "nl80211", "regulatory");
1860 if (ret >= 0)
1861 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1862 if (ret < 0) {
1863 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1864 "membership for regulatory events: %d (%s)",
1865 ret, strerror(-ret));
1866 /* Continue without regulatory events */
1867 }
1868
1869 eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
1870 wpa_driver_nl80211_event_receive, drv,
1871 drv->nl_handle_event);
1872
1873 return 0;
1874
1875err4:
1876 nl_cache_free(drv->nl_cache_event);
1877err3b:
1878 nl_cache_free(drv->nl_cache);
1879err3:
1880 nl80211_handle_destroy(drv->nl_handle_event);
1881err2b:
1882 nl80211_handle_destroy(drv->nl_handle);
1883err2:
1884 nl_cb_put(drv->nl_cb);
1885err1:
1886 return -1;
1887}
1888
1889
1890static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1891{
1892 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1893 /*
1894 * This may be for any interface; use ifdown event to disable
1895 * interface.
1896 */
1897}
1898
1899
1900static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1901{
1902 struct wpa_driver_nl80211_data *drv = ctx;
1903 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
1904 if (linux_set_iface_flags(drv->ioctl_sock, drv->first_bss.ifname, 1)) {
1905 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1906 "after rfkill unblock");
1907 return;
1908 }
1909 /* rtnetlink ifup handler will report interface as enabled */
1910}
1911
1912
1913static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
1914{
1915 /* Find phy (radio) to which this interface belongs */
1916 char buf[90], *pos;
1917 int f, rv;
1918
1919 drv->phyname[0] = '\0';
1920 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
1921 drv->first_bss.ifname);
1922 f = open(buf, O_RDONLY);
1923 if (f < 0) {
1924 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
1925 buf, strerror(errno));
1926 return;
1927 }
1928
1929 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
1930 close(f);
1931 if (rv < 0) {
1932 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
1933 buf, strerror(errno));
1934 return;
1935 }
1936
1937 drv->phyname[rv] = '\0';
1938 pos = os_strchr(drv->phyname, '\n');
1939 if (pos)
1940 *pos = '\0';
1941 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
1942 drv->first_bss.ifname, drv->phyname);
1943}
1944
1945
1946/**
1947 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1948 * @ctx: context to be used when calling wpa_supplicant functions,
1949 * e.g., wpa_supplicant_event()
1950 * @ifname: interface name, e.g., wlan0
1951 * @global_priv: private driver global data from global_init()
1952 * Returns: Pointer to private data, %NULL on failure
1953 */
1954static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1955 void *global_priv)
1956{
1957 struct wpa_driver_nl80211_data *drv;
1958 struct netlink_config *cfg;
1959 struct rfkill_config *rcfg;
1960 struct i802_bss *bss;
1961
1962 drv = os_zalloc(sizeof(*drv));
1963 if (drv == NULL)
1964 return NULL;
1965 drv->global = global_priv;
1966 drv->ctx = ctx;
1967 bss = &drv->first_bss;
1968 bss->drv = drv;
1969 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1970 drv->monitor_ifidx = -1;
1971 drv->monitor_sock = -1;
1972 drv->ioctl_sock = -1;
1973
1974 if (wpa_driver_nl80211_init_nl(drv)) {
1975 os_free(drv);
1976 return NULL;
1977 }
1978
1979 nl80211_get_phy_name(drv);
1980
1981 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1982 if (drv->ioctl_sock < 0) {
1983 perror("socket(PF_INET,SOCK_DGRAM)");
1984 goto failed;
1985 }
1986
1987 cfg = os_zalloc(sizeof(*cfg));
1988 if (cfg == NULL)
1989 goto failed;
1990 cfg->ctx = drv;
1991 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
1992 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
1993 drv->netlink = netlink_init(cfg);
1994 if (drv->netlink == NULL) {
1995 os_free(cfg);
1996 goto failed;
1997 }
1998
1999 rcfg = os_zalloc(sizeof(*rcfg));
2000 if (rcfg == NULL)
2001 goto failed;
2002 rcfg->ctx = drv;
2003 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
2004 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2005 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2006 drv->rfkill = rfkill_init(rcfg);
2007 if (drv->rfkill == NULL) {
2008 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2009 os_free(rcfg);
2010 }
2011
2012 if (wpa_driver_nl80211_finish_drv_init(drv))
2013 goto failed;
2014
2015 if (drv->global)
2016 dl_list_add(&drv->global->interfaces, &drv->list);
2017
2018 return bss;
2019
2020failed:
2021 rfkill_deinit(drv->rfkill);
2022 netlink_deinit(drv->netlink);
2023 if (drv->ioctl_sock >= 0)
2024 close(drv->ioctl_sock);
2025
2026 genl_family_put(drv->nl80211);
2027 nl_cache_free(drv->nl_cache);
2028 nl80211_handle_destroy(drv->nl_handle);
2029 nl_cb_put(drv->nl_cb);
2030 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
2031
2032 os_free(drv);
2033 return NULL;
2034}
2035
2036
2037static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
2038 struct nl_handle *nl_handle,
2039 u16 type, const u8 *match, size_t match_len)
2040{
2041 struct nl_msg *msg;
2042 int ret = -1;
2043
2044 msg = nlmsg_alloc();
2045 if (!msg)
2046 return -1;
2047
2048 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2049 NL80211_CMD_REGISTER_ACTION, 0);
2050
2051 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2052 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
2053 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
2054
2055 ret = send_and_recv(drv, nl_handle, msg, NULL, NULL);
2056 msg = NULL;
2057 if (ret) {
2058 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2059 "failed (type=%u): ret=%d (%s)",
2060 type, ret, strerror(-ret));
2061 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2062 match, match_len);
2063 goto nla_put_failure;
2064 }
2065 ret = 0;
2066nla_put_failure:
2067 nlmsg_free(msg);
2068 return ret;
2069}
2070
2071
2072static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
2073 const u8 *match, size_t match_len)
2074{
2075 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
2076 return nl80211_register_frame(drv, drv->nl_handle_event,
2077 type, match, match_len);
2078}
2079
2080
2081static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
2082{
2083#ifdef CONFIG_P2P
2084 /* GAS Initial Request */
2085 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
2086 return -1;
2087 /* GAS Initial Response */
2088 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
2089 return -1;
2090 /* GAS Comeback Request */
2091 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
2092 return -1;
2093 /* GAS Comeback Response */
2094 if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
2095 return -1;
2096 /* P2P Public Action */
2097 if (nl80211_register_action_frame(drv,
2098 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2099 6) < 0)
2100 return -1;
2101 /* P2P Action */
2102 if (nl80211_register_action_frame(drv,
2103 (u8 *) "\x7f\x50\x6f\x9a\x09",
2104 5) < 0)
2105 return -1;
2106#endif /* CONFIG_P2P */
2107#ifdef CONFIG_IEEE80211W
2108 /* SA Query Response */
2109 if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0)
2110 return -1;
2111#endif /* CONFIG_IEEE80211W */
2112
2113 /* FT Action frames */
2114 if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
2115 return -1;
2116 else
2117 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2118 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2119
2120 return 0;
2121}
2122
2123
2124static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2125{
2126 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2127}
2128
2129
2130static int
2131wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
2132{
2133 struct i802_bss *bss = &drv->first_bss;
2134 int send_rfkill_event = 0;
2135
2136 drv->ifindex = if_nametoindex(bss->ifname);
2137 drv->first_bss.ifindex = drv->ifindex;
2138
2139#ifndef HOSTAPD
2140 if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA) < 0) {
2141 wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
2142 "use managed mode");
2143 }
2144
2145 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
2146 if (rfkill_is_blocked(drv->rfkill)) {
2147 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2148 "interface '%s' due to rfkill",
2149 bss->ifname);
2150 drv->if_disabled = 1;
2151 send_rfkill_event = 1;
2152 } else {
2153 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2154 "interface '%s' UP", bss->ifname);
2155 return -1;
2156 }
2157 }
2158
2159 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
2160 1, IF_OPER_DORMANT);
2161#endif /* HOSTAPD */
2162
2163 if (wpa_driver_nl80211_capa(drv))
2164 return -1;
2165
2166 if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, drv->addr))
2167 return -1;
2168
2169 if (nl80211_register_action_frames(drv) < 0) {
2170 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
2171 "frame processing - ignore for now");
2172 /*
2173 * Older kernel versions did not support this, so ignore the
2174 * error for now. Some functionality may not be available
2175 * because of this.
2176 */
2177 }
2178
2179 if (send_rfkill_event) {
2180 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2181 drv, drv->ctx);
2182 }
2183
2184 return 0;
2185}
2186
2187
2188static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2189{
2190 struct nl_msg *msg;
2191
2192 msg = nlmsg_alloc();
2193 if (!msg)
2194 return -ENOMEM;
2195
2196 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2197 0, NL80211_CMD_DEL_BEACON, 0);
2198 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2199
2200 return send_and_recv_msgs(drv, msg, NULL, NULL);
2201 nla_put_failure:
2202 return -ENOBUFS;
2203}
2204
2205
2206/**
2207 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
2208 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
2209 *
2210 * Shut down driver interface and processing of driver events. Free
2211 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2212 */
2213static void wpa_driver_nl80211_deinit(void *priv)
2214{
2215 struct i802_bss *bss = priv;
2216 struct wpa_driver_nl80211_data *drv = bss->drv;
2217
2218 if (drv->nl_handle_preq)
2219 wpa_driver_nl80211_probe_req_report(bss, 0);
2220 if (bss->added_if_into_bridge) {
2221 if (linux_br_del_if(drv->ioctl_sock, bss->brname, bss->ifname)
2222 < 0)
2223 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2224 "interface %s from bridge %s: %s",
2225 bss->ifname, bss->brname, strerror(errno));
2226 }
2227 if (bss->added_bridge) {
2228 if (linux_br_del(drv->ioctl_sock, bss->brname) < 0)
2229 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2230 "bridge %s: %s",
2231 bss->brname, strerror(errno));
2232 }
2233
2234 nl80211_remove_monitor_interface(drv);
2235
2236 if (drv->nlmode == NL80211_IFTYPE_AP)
2237 wpa_driver_nl80211_del_beacon(drv);
2238
2239#ifdef HOSTAPD
2240 if (drv->last_freq_ht) {
2241 /* Clear HT flags from the driver */
2242 struct hostapd_freq_params freq;
2243 os_memset(&freq, 0, sizeof(freq));
2244 freq.freq = drv->last_freq;
2245 i802_set_freq(priv, &freq);
2246 }
2247
2248 if (drv->eapol_sock >= 0) {
2249 eloop_unregister_read_sock(drv->eapol_sock);
2250 close(drv->eapol_sock);
2251 }
2252
2253 if (drv->if_indices != drv->default_if_indices)
2254 os_free(drv->if_indices);
2255#endif /* HOSTAPD */
2256
2257 if (drv->disable_11b_rates)
2258 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2259
2260 netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
2261 netlink_deinit(drv->netlink);
2262 rfkill_deinit(drv->rfkill);
2263
2264 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2265
2266 (void) linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0);
2267 wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA);
2268
2269 if (drv->ioctl_sock >= 0)
2270 close(drv->ioctl_sock);
2271
2272 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
2273 genl_family_put(drv->nl80211);
2274 nl_cache_free(drv->nl_cache);
2275 nl_cache_free(drv->nl_cache_event);
2276 nl80211_handle_destroy(drv->nl_handle);
2277 nl80211_handle_destroy(drv->nl_handle_event);
2278 nl_cb_put(drv->nl_cb);
2279
2280 os_free(drv->filter_ssids);
2281
2282 if (drv->global)
2283 dl_list_del(&drv->list);
2284
2285 os_free(drv);
2286}
2287
2288
2289/**
2290 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
2291 * @eloop_ctx: Driver private data
2292 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
2293 *
2294 * This function can be used as registered timeout when starting a scan to
2295 * generate a scan completed event if the driver does not report this.
2296 */
2297static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
2298{
2299 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2300 if (drv->ap_scan_as_station) {
2301 wpa_driver_nl80211_set_mode(&drv->first_bss,
2302 IEEE80211_MODE_AP);
2303 drv->ap_scan_as_station = 0;
2304 }
2305 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
2306 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
2307}
2308
2309
2310/**
2311 * wpa_driver_nl80211_scan - Request the driver to initiate scan
2312 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2313 * @params: Scan parameters
2314 * Returns: 0 on success, -1 on failure
2315 */
2316static int wpa_driver_nl80211_scan(void *priv,
2317 struct wpa_driver_scan_params *params)
2318{
2319 struct i802_bss *bss = priv;
2320 struct wpa_driver_nl80211_data *drv = bss->drv;
2321 int ret = 0, timeout;
2322 struct nl_msg *msg, *ssids, *freqs;
2323 size_t i;
2324
2325 msg = nlmsg_alloc();
2326 ssids = nlmsg_alloc();
2327 freqs = nlmsg_alloc();
2328 if (!msg || !ssids || !freqs) {
2329 nlmsg_free(msg);
2330 nlmsg_free(ssids);
2331 nlmsg_free(freqs);
2332 return -1;
2333 }
2334
2335 os_free(drv->filter_ssids);
2336 drv->filter_ssids = params->filter_ssids;
2337 params->filter_ssids = NULL;
2338 drv->num_filter_ssids = params->num_filter_ssids;
2339
2340 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2341 NL80211_CMD_TRIGGER_SCAN, 0);
2342
2343 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2344
2345 for (i = 0; i < params->num_ssids; i++) {
2346 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
2347 params->ssids[i].ssid,
2348 params->ssids[i].ssid_len);
2349 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
2350 params->ssids[i].ssid);
2351 }
2352 if (params->num_ssids)
2353 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
2354
2355 if (params->extra_ies) {
2356 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
2357 params->extra_ies, params->extra_ies_len);
2358 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
2359 params->extra_ies);
2360 }
2361
2362 if (params->freqs) {
2363 for (i = 0; params->freqs[i]; i++) {
2364 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
2365 "MHz", params->freqs[i]);
2366 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
2367 }
2368 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
2369 }
2370
2371 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2372 msg = NULL;
2373 if (ret) {
2374 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
2375 "(%s)", ret, strerror(-ret));
2376#ifdef HOSTAPD
2377 if (drv->nlmode == NL80211_IFTYPE_AP) {
2378 /*
2379 * mac80211 does not allow scan requests in AP mode, so
2380 * try to do this in station mode.
2381 */
2382 if (wpa_driver_nl80211_set_mode(bss,
2383 IEEE80211_MODE_INFRA))
2384 goto nla_put_failure;
2385
2386 if (wpa_driver_nl80211_scan(drv, params)) {
2387 wpa_driver_nl80211_set_mode(bss,
2388 IEEE80211_MODE_AP);
2389 goto nla_put_failure;
2390 }
2391
2392 /* Restore AP mode when processing scan results */
2393 drv->ap_scan_as_station = 1;
2394 ret = 0;
2395 } else
2396 goto nla_put_failure;
2397#else /* HOSTAPD */
2398 goto nla_put_failure;
2399#endif /* HOSTAPD */
2400 }
2401
2402 /* Not all drivers generate "scan completed" wireless event, so try to
2403 * read results after a timeout. */
2404 timeout = 10;
2405 if (drv->scan_complete_events) {
2406 /*
2407 * The driver seems to deliver events to notify when scan is
2408 * complete, so use longer timeout to avoid race conditions
2409 * with scanning and following association request.
2410 */
2411 timeout = 30;
2412 }
2413 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
2414 "seconds", ret, timeout);
2415 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2416 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
2417 drv, drv->ctx);
2418
2419nla_put_failure:
2420 nlmsg_free(ssids);
2421 nlmsg_free(msg);
2422 nlmsg_free(freqs);
2423 return ret;
2424}
2425
2426
2427static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
2428{
2429 const u8 *end, *pos;
2430
2431 if (ies == NULL)
2432 return NULL;
2433
2434 pos = ies;
2435 end = ies + ies_len;
2436
2437 while (pos + 1 < end) {
2438 if (pos + 2 + pos[1] > end)
2439 break;
2440 if (pos[0] == ie)
2441 return pos;
2442 pos += 2 + pos[1];
2443 }
2444
2445 return NULL;
2446}
2447
2448
2449static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
2450 const u8 *ie, size_t ie_len)
2451{
2452 const u8 *ssid;
2453 size_t i;
2454
2455 if (drv->filter_ssids == NULL)
2456 return 0;
2457
2458 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
2459 if (ssid == NULL)
2460 return 1;
2461
2462 for (i = 0; i < drv->num_filter_ssids; i++) {
2463 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
2464 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
2465 0)
2466 return 0;
2467 }
2468
2469 return 1;
2470}
2471
2472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473static int bss_info_handler(struct nl_msg *msg, void *arg)
2474{
2475 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2476 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2477 struct nlattr *bss[NL80211_BSS_MAX + 1];
2478 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2479 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
2480 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2481 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2482 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2483 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2484 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
2485 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2486 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2487 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2488 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2489 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
2490 };
2491 struct nl80211_bss_info_arg *_arg = arg;
2492 struct wpa_scan_results *res = _arg->res;
2493 struct wpa_scan_res **tmp;
2494 struct wpa_scan_res *r;
2495 const u8 *ie, *beacon_ie;
2496 size_t ie_len, beacon_ie_len;
2497 u8 *pos;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002498 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499
2500 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2501 genlmsg_attrlen(gnlh, 0), NULL);
2502 if (!tb[NL80211_ATTR_BSS])
2503 return NL_SKIP;
2504 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2505 bss_policy))
2506 return NL_SKIP;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002507 if (bss[NL80211_BSS_STATUS]) {
2508 enum nl80211_bss_status status;
2509 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
2510 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
2511 bss[NL80211_BSS_FREQUENCY]) {
2512 _arg->assoc_freq =
2513 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2514 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
2515 _arg->assoc_freq);
2516 }
2517 }
2518 if (!res)
2519 return NL_SKIP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
2521 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2522 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2523 } else {
2524 ie = NULL;
2525 ie_len = 0;
2526 }
2527 if (bss[NL80211_BSS_BEACON_IES]) {
2528 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
2529 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
2530 } else {
2531 beacon_ie = NULL;
2532 beacon_ie_len = 0;
2533 }
2534
2535 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
2536 ie ? ie_len : beacon_ie_len))
2537 return NL_SKIP;
2538
2539 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
2540 if (r == NULL)
2541 return NL_SKIP;
2542 if (bss[NL80211_BSS_BSSID])
2543 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
2544 ETH_ALEN);
2545 if (bss[NL80211_BSS_FREQUENCY])
2546 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2547 if (bss[NL80211_BSS_BEACON_INTERVAL])
2548 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
2549 if (bss[NL80211_BSS_CAPABILITY])
2550 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2551 r->flags |= WPA_SCAN_NOISE_INVALID;
2552 if (bss[NL80211_BSS_SIGNAL_MBM]) {
2553 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
2554 r->level /= 100; /* mBm to dBm */
2555 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
2556 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
2557 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
2558 r->flags |= WPA_SCAN_LEVEL_INVALID;
2559 } else
2560 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
2561 if (bss[NL80211_BSS_TSF])
2562 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
2563 if (bss[NL80211_BSS_SEEN_MS_AGO])
2564 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
2565 r->ie_len = ie_len;
2566 pos = (u8 *) (r + 1);
2567 if (ie) {
2568 os_memcpy(pos, ie, ie_len);
2569 pos += ie_len;
2570 }
2571 r->beacon_ie_len = beacon_ie_len;
2572 if (beacon_ie)
2573 os_memcpy(pos, beacon_ie, beacon_ie_len);
2574
2575 if (bss[NL80211_BSS_STATUS]) {
2576 enum nl80211_bss_status status;
2577 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
2578 switch (status) {
2579 case NL80211_BSS_STATUS_AUTHENTICATED:
2580 r->flags |= WPA_SCAN_AUTHENTICATED;
2581 break;
2582 case NL80211_BSS_STATUS_ASSOCIATED:
2583 r->flags |= WPA_SCAN_ASSOCIATED;
2584 break;
2585 default:
2586 break;
2587 }
2588 }
2589
Jouni Malinen87fd2792011-05-16 18:35:42 +03002590 /*
2591 * cfg80211 maintains separate BSS table entries for APs if the same
2592 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
2593 * not use frequency as a separate key in the BSS table, so filter out
2594 * duplicated entries. Prefer associated BSS entry in such a case in
2595 * order to get the correct frequency into the BSS table.
2596 */
2597 for (i = 0; i < res->num; i++) {
2598 const u8 *s1, *s2;
2599 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
2600 continue;
2601
2602 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
2603 res->res[i]->ie_len, WLAN_EID_SSID);
2604 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
2605 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
2606 os_memcmp(s1, s2, 2 + s1[1]) != 0)
2607 continue;
2608
2609 /* Same BSSID,SSID was already included in scan results */
2610 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
2611 "for " MACSTR, MAC2STR(r->bssid));
2612
2613 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
2614 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
2615 os_free(res->res[i]);
2616 res->res[i] = r;
2617 } else
2618 os_free(r);
2619 return NL_SKIP;
2620 }
2621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 tmp = os_realloc(res->res,
2623 (res->num + 1) * sizeof(struct wpa_scan_res *));
2624 if (tmp == NULL) {
2625 os_free(r);
2626 return NL_SKIP;
2627 }
2628 tmp[res->num++] = r;
2629 res->res = tmp;
2630
2631 return NL_SKIP;
2632}
2633
2634
2635static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
2636 const u8 *addr)
2637{
2638 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
2639 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
2640 "mismatch (" MACSTR ")", MAC2STR(addr));
2641 wpa_driver_nl80211_mlme(drv, addr,
2642 NL80211_CMD_DEAUTHENTICATE,
2643 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
2644 }
2645}
2646
2647
2648static void wpa_driver_nl80211_check_bss_status(
2649 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
2650{
2651 size_t i;
2652
2653 for (i = 0; i < res->num; i++) {
2654 struct wpa_scan_res *r = res->res[i];
2655 if (r->flags & WPA_SCAN_AUTHENTICATED) {
2656 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
2657 "indicates BSS status with " MACSTR
2658 " as authenticated",
2659 MAC2STR(r->bssid));
2660 if (drv->nlmode == NL80211_IFTYPE_STATION &&
2661 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
2662 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
2663 0) {
2664 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
2665 " in local state (auth=" MACSTR
2666 " assoc=" MACSTR ")",
2667 MAC2STR(drv->auth_bssid),
2668 MAC2STR(drv->bssid));
2669 clear_state_mismatch(drv, r->bssid);
2670 }
2671 }
2672
2673 if (r->flags & WPA_SCAN_ASSOCIATED) {
2674 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
2675 "indicate BSS status with " MACSTR
2676 " as associated",
2677 MAC2STR(r->bssid));
2678 if (drv->nlmode == NL80211_IFTYPE_STATION &&
2679 !drv->associated) {
2680 wpa_printf(MSG_DEBUG, "nl80211: Local state "
2681 "(not associated) does not match "
2682 "with BSS state");
2683 clear_state_mismatch(drv, r->bssid);
2684 } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
2685 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
2686 0) {
2687 wpa_printf(MSG_DEBUG, "nl80211: Local state "
2688 "(associated with " MACSTR ") does "
2689 "not match with BSS state",
2690 MAC2STR(drv->bssid));
2691 clear_state_mismatch(drv, r->bssid);
2692 clear_state_mismatch(drv, drv->bssid);
2693 }
2694 }
2695 }
2696}
2697
2698
2699static void wpa_scan_results_free(struct wpa_scan_results *res)
2700{
2701 size_t i;
2702
2703 if (res == NULL)
2704 return;
2705
2706 for (i = 0; i < res->num; i++)
2707 os_free(res->res[i]);
2708 os_free(res->res);
2709 os_free(res);
2710}
2711
2712
2713static struct wpa_scan_results *
2714nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
2715{
2716 struct nl_msg *msg;
2717 struct wpa_scan_results *res;
2718 int ret;
2719 struct nl80211_bss_info_arg arg;
2720
2721 res = os_zalloc(sizeof(*res));
2722 if (res == NULL)
2723 return NULL;
2724 msg = nlmsg_alloc();
2725 if (!msg)
2726 goto nla_put_failure;
2727
2728 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
2729 NL80211_CMD_GET_SCAN, 0);
2730 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2731
2732 arg.drv = drv;
2733 arg.res = res;
2734 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
2735 msg = NULL;
2736 if (ret == 0) {
2737 wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
2738 (unsigned long) res->num);
2739 return res;
2740 }
2741 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
2742 "(%s)", ret, strerror(-ret));
2743nla_put_failure:
2744 nlmsg_free(msg);
2745 wpa_scan_results_free(res);
2746 return NULL;
2747}
2748
2749
2750/**
2751 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
2752 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
2753 * Returns: Scan results on success, -1 on failure
2754 */
2755static struct wpa_scan_results *
2756wpa_driver_nl80211_get_scan_results(void *priv)
2757{
2758 struct i802_bss *bss = priv;
2759 struct wpa_driver_nl80211_data *drv = bss->drv;
2760 struct wpa_scan_results *res;
2761
2762 res = nl80211_get_scan_results(drv);
2763 if (res)
2764 wpa_driver_nl80211_check_bss_status(drv, res);
2765 return res;
2766}
2767
2768
2769static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
2770{
2771 struct wpa_scan_results *res;
2772 size_t i;
2773
2774 res = nl80211_get_scan_results(drv);
2775 if (res == NULL) {
2776 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
2777 return;
2778 }
2779
2780 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
2781 for (i = 0; i < res->num; i++) {
2782 struct wpa_scan_res *r = res->res[i];
2783 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
2784 (int) i, (int) res->num, MAC2STR(r->bssid),
2785 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
2786 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
2787 }
2788
2789 wpa_scan_results_free(res);
2790}
2791
2792
2793static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
2794 enum wpa_alg alg, const u8 *addr,
2795 int key_idx, int set_tx,
2796 const u8 *seq, size_t seq_len,
2797 const u8 *key, size_t key_len)
2798{
2799 struct i802_bss *bss = priv;
2800 struct wpa_driver_nl80211_data *drv = bss->drv;
2801 int ifindex = if_nametoindex(ifname);
2802 struct nl_msg *msg;
2803 int ret;
2804
2805 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
2806 "set_tx=%d seq_len=%lu key_len=%lu",
2807 __func__, ifindex, alg, addr, key_idx, set_tx,
2808 (unsigned long) seq_len, (unsigned long) key_len);
2809
2810 msg = nlmsg_alloc();
2811 if (!msg)
2812 return -ENOMEM;
2813
2814 if (alg == WPA_ALG_NONE) {
2815 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2816 0, NL80211_CMD_DEL_KEY, 0);
2817 } else {
2818 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2819 0, NL80211_CMD_NEW_KEY, 0);
2820 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
2821 switch (alg) {
2822 case WPA_ALG_WEP:
2823 if (key_len == 5)
2824 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
2825 WLAN_CIPHER_SUITE_WEP40);
2826 else
2827 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
2828 WLAN_CIPHER_SUITE_WEP104);
2829 break;
2830 case WPA_ALG_TKIP:
2831 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
2832 WLAN_CIPHER_SUITE_TKIP);
2833 break;
2834 case WPA_ALG_CCMP:
2835 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
2836 WLAN_CIPHER_SUITE_CCMP);
2837 break;
2838 case WPA_ALG_IGTK:
2839 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
2840 WLAN_CIPHER_SUITE_AES_CMAC);
2841 break;
2842 default:
2843 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
2844 "algorithm %d", __func__, alg);
2845 nlmsg_free(msg);
2846 return -1;
2847 }
2848 }
2849
2850 if (seq && seq_len)
2851 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
2852
2853 if (addr && !is_broadcast_ether_addr(addr)) {
2854 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
2855 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
2856
2857 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2858 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
2859 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
2860 NL80211_KEYTYPE_GROUP);
2861 }
2862 } else if (addr && is_broadcast_ether_addr(addr)) {
2863 struct nl_msg *types;
2864 int err;
2865 wpa_printf(MSG_DEBUG, " broadcast key");
2866 types = nlmsg_alloc();
2867 if (!types)
2868 goto nla_put_failure;
2869 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
2870 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
2871 types);
2872 nlmsg_free(types);
2873 if (err)
2874 goto nla_put_failure;
2875 }
2876 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
2877 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
2878
2879 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2880 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2881 ret = 0;
2882 if (ret)
2883 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2884 ret, strerror(-ret));
2885
2886 /*
2887 * If we failed or don't need to set the default TX key (below),
2888 * we're done here.
2889 */
2890 if (ret || !set_tx || alg == WPA_ALG_NONE)
2891 return ret;
2892 if (drv->nlmode == NL80211_IFTYPE_AP && addr &&
2893 !is_broadcast_ether_addr(addr))
2894 return ret;
2895
2896 msg = nlmsg_alloc();
2897 if (!msg)
2898 return -ENOMEM;
2899
2900 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2901 0, NL80211_CMD_SET_KEY, 0);
2902 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
2903 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
2904 if (alg == WPA_ALG_IGTK)
2905 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
2906 else
2907 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
2908 if (addr && is_broadcast_ether_addr(addr)) {
2909 struct nl_msg *types;
2910 int err;
2911 types = nlmsg_alloc();
2912 if (!types)
2913 goto nla_put_failure;
2914 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
2915 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
2916 types);
2917 nlmsg_free(types);
2918 if (err)
2919 goto nla_put_failure;
2920 } else if (addr) {
2921 struct nl_msg *types;
2922 int err;
2923 types = nlmsg_alloc();
2924 if (!types)
2925 goto nla_put_failure;
2926 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
2927 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
2928 types);
2929 nlmsg_free(types);
2930 if (err)
2931 goto nla_put_failure;
2932 }
2933
2934 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2935 if (ret == -ENOENT)
2936 ret = 0;
2937 if (ret)
2938 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2939 "err=%d %s)", ret, strerror(-ret));
2940 return ret;
2941
2942nla_put_failure:
2943 return -ENOBUFS;
2944}
2945
2946
2947static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2948 int key_idx, int defkey,
2949 const u8 *seq, size_t seq_len,
2950 const u8 *key, size_t key_len)
2951{
2952 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
2953 if (!key_attr)
2954 return -1;
2955
2956 if (defkey && alg == WPA_ALG_IGTK)
2957 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
2958 else if (defkey)
2959 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
2960
2961 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
2962
2963 switch (alg) {
2964 case WPA_ALG_WEP:
2965 if (key_len == 5)
2966 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
2967 WLAN_CIPHER_SUITE_WEP40);
2968 else
2969 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
2970 WLAN_CIPHER_SUITE_WEP104);
2971 break;
2972 case WPA_ALG_TKIP:
2973 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
2974 break;
2975 case WPA_ALG_CCMP:
2976 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
2977 break;
2978 case WPA_ALG_IGTK:
2979 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
2980 WLAN_CIPHER_SUITE_AES_CMAC);
2981 break;
2982 default:
2983 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
2984 "algorithm %d", __func__, alg);
2985 return -1;
2986 }
2987
2988 if (seq && seq_len)
2989 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
2990
2991 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
2992
2993 nla_nest_end(msg, key_attr);
2994
2995 return 0;
2996 nla_put_failure:
2997 return -1;
2998}
2999
3000
3001static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3002 struct nl_msg *msg)
3003{
3004 int i, privacy = 0;
3005 struct nlattr *nl_keys, *nl_key;
3006
3007 for (i = 0; i < 4; i++) {
3008 if (!params->wep_key[i])
3009 continue;
3010 privacy = 1;
3011 break;
3012 }
3013 if (params->wps == WPS_MODE_PRIVACY)
3014 privacy = 1;
3015 if (params->pairwise_suite &&
3016 params->pairwise_suite != WPA_CIPHER_NONE)
3017 privacy = 1;
3018
3019 if (!privacy)
3020 return 0;
3021
3022 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
3023
3024 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3025 if (!nl_keys)
3026 goto nla_put_failure;
3027
3028 for (i = 0; i < 4; i++) {
3029 if (!params->wep_key[i])
3030 continue;
3031
3032 nl_key = nla_nest_start(msg, i);
3033 if (!nl_key)
3034 goto nla_put_failure;
3035
3036 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3037 params->wep_key[i]);
3038 if (params->wep_key_len[i] == 5)
3039 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3040 WLAN_CIPHER_SUITE_WEP40);
3041 else
3042 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3043 WLAN_CIPHER_SUITE_WEP104);
3044
3045 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
3046
3047 if (i == params->wep_tx_keyidx)
3048 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
3049
3050 nla_nest_end(msg, nl_key);
3051 }
3052 nla_nest_end(msg, nl_keys);
3053
3054 return 0;
3055
3056nla_put_failure:
3057 return -ENOBUFS;
3058}
3059
3060
3061static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3062 const u8 *addr, int cmd, u16 reason_code,
3063 int local_state_change)
3064{
3065 int ret = -1;
3066 struct nl_msg *msg;
3067
3068 msg = nlmsg_alloc();
3069 if (!msg)
3070 return -1;
3071
3072 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
3073
3074 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3075 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
3076 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3077 if (local_state_change)
3078 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3079
3080 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3081 msg = NULL;
3082 if (ret) {
3083 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3084 "(%s)", ret, strerror(-ret));
3085 goto nla_put_failure;
3086 }
3087 ret = 0;
3088
3089nla_put_failure:
3090 nlmsg_free(msg);
3091 return ret;
3092}
3093
3094
3095static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
3096 const u8 *addr, int reason_code)
3097{
3098 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3099 __func__, MAC2STR(addr), reason_code);
3100 drv->associated = 0;
3101 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
3102 reason_code, 0);
3103}
3104
3105
3106static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
3107 int reason_code)
3108{
3109 struct i802_bss *bss = priv;
3110 struct wpa_driver_nl80211_data *drv = bss->drv;
3111 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3112 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3113 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3114 __func__, MAC2STR(addr), reason_code);
3115 drv->associated = 0;
3116 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
3117 return nl80211_leave_ibss(drv);
3118 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3119 reason_code, 0);
3120}
3121
3122
3123static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
3124 int reason_code)
3125{
3126 struct i802_bss *bss = priv;
3127 struct wpa_driver_nl80211_data *drv = bss->drv;
3128 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3129 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3130 wpa_printf(MSG_DEBUG, "%s", __func__);
3131 drv->associated = 0;
3132 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
3133 reason_code, 0);
3134}
3135
3136
3137static int wpa_driver_nl80211_authenticate(
3138 void *priv, struct wpa_driver_auth_params *params)
3139{
3140 struct i802_bss *bss = priv;
3141 struct wpa_driver_nl80211_data *drv = bss->drv;
3142 int ret = -1, i;
3143 struct nl_msg *msg;
3144 enum nl80211_auth_type type;
3145 int count = 0;
3146
3147 drv->associated = 0;
3148 os_memset(drv->auth_bssid, 0, ETH_ALEN);
3149 /* FIX: IBSS mode */
3150 if (drv->nlmode != NL80211_IFTYPE_STATION &&
3151 wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA) < 0)
3152 return -1;
3153
3154retry:
3155 msg = nlmsg_alloc();
3156 if (!msg)
3157 return -1;
3158
3159 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3160 drv->ifindex);
3161
3162 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3163 NL80211_CMD_AUTHENTICATE, 0);
3164
3165 for (i = 0; i < 4; i++) {
3166 if (!params->wep_key[i])
3167 continue;
3168 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
3169 NULL, i,
3170 i == params->wep_tx_keyidx, NULL, 0,
3171 params->wep_key[i],
3172 params->wep_key_len[i]);
3173 if (params->wep_tx_keyidx != i)
3174 continue;
3175 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
3176 params->wep_key[i], params->wep_key_len[i])) {
3177 nlmsg_free(msg);
3178 return -1;
3179 }
3180 }
3181
3182 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3183 if (params->bssid) {
3184 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3185 MAC2STR(params->bssid));
3186 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3187 }
3188 if (params->freq) {
3189 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3190 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3191 }
3192 if (params->ssid) {
3193 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3194 params->ssid, params->ssid_len);
3195 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3196 params->ssid);
3197 }
3198 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
3199 if (params->ie)
3200 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
3201 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3202 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3203 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3204 type = NL80211_AUTHTYPE_SHARED_KEY;
3205 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3206 type = NL80211_AUTHTYPE_NETWORK_EAP;
3207 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3208 type = NL80211_AUTHTYPE_FT;
3209 else
3210 goto nla_put_failure;
3211 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3212 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3213 if (params->local_state_change) {
3214 wpa_printf(MSG_DEBUG, " * Local state change only");
3215 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3216 }
3217
3218 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3219 msg = NULL;
3220 if (ret) {
3221 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3222 "(%s)", ret, strerror(-ret));
3223 count++;
3224 if (ret == -EALREADY && count == 1 && params->bssid &&
3225 !params->local_state_change) {
3226 /*
3227 * mac80211 does not currently accept new
3228 * authentication if we are already authenticated. As a
3229 * workaround, force deauthentication and try again.
3230 */
3231 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3232 "after forced deauthentication");
3233 wpa_driver_nl80211_deauthenticate(
3234 bss, params->bssid,
3235 WLAN_REASON_PREV_AUTH_NOT_VALID);
3236 nlmsg_free(msg);
3237 goto retry;
3238 }
3239 goto nla_put_failure;
3240 }
3241 ret = 0;
3242 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
3243 "successfully");
3244
3245nla_put_failure:
3246 nlmsg_free(msg);
3247 return ret;
3248}
3249
3250
3251struct phy_info_arg {
3252 u16 *num_modes;
3253 struct hostapd_hw_modes *modes;
3254};
3255
3256static int phy_info_handler(struct nl_msg *msg, void *arg)
3257{
3258 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3259 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3260 struct phy_info_arg *phy_info = arg;
3261
3262 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
3263
3264 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
3265 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
3266 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
3267 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
3268 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
3269 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
3270 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
3271 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
3272 };
3273
3274 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
3275 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
3276 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3277 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
3278 };
3279
3280 struct nlattr *nl_band;
3281 struct nlattr *nl_freq;
3282 struct nlattr *nl_rate;
3283 int rem_band, rem_freq, rem_rate;
3284 struct hostapd_hw_modes *mode;
3285 int idx, mode_is_set;
3286
3287 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3288 genlmsg_attrlen(gnlh, 0), NULL);
3289
3290 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
3291 return NL_SKIP;
3292
3293 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
3294 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
3295 if (!mode)
3296 return NL_SKIP;
3297 phy_info->modes = mode;
3298
3299 mode_is_set = 0;
3300
3301 mode = &phy_info->modes[*(phy_info->num_modes)];
3302 memset(mode, 0, sizeof(*mode));
3303 *(phy_info->num_modes) += 1;
3304
3305 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
3306 nla_len(nl_band), NULL);
3307
3308 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
3309 mode->ht_capab = nla_get_u16(
3310 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
3311 }
3312
3313 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
3314 mode->a_mpdu_params |= nla_get_u8(
3315 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
3316 0x03;
3317 }
3318
3319 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
3320 mode->a_mpdu_params |= nla_get_u8(
3321 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
3322 2;
3323 }
3324
3325 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
3326 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
3327 u8 *mcs;
3328 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
3329 os_memcpy(mode->mcs_set, mcs, 16);
3330 }
3331
3332 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3333 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3334 nla_len(nl_freq), freq_policy);
3335 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3336 continue;
3337 mode->num_channels++;
3338 }
3339
3340 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
3341 if (!mode->channels)
3342 return NL_SKIP;
3343
3344 idx = 0;
3345
3346 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3347 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3348 nla_len(nl_freq), freq_policy);
3349 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3350 continue;
3351
3352 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
3353 mode->channels[idx].flag = 0;
3354
3355 if (!mode_is_set) {
3356 /* crude heuristic */
3357 if (mode->channels[idx].freq < 4000)
3358 mode->mode = HOSTAPD_MODE_IEEE80211B;
3359 else
3360 mode->mode = HOSTAPD_MODE_IEEE80211A;
3361 mode_is_set = 1;
3362 }
3363
3364 /* crude heuristic */
3365 if (mode->channels[idx].freq < 4000)
3366 if (mode->channels[idx].freq == 2484)
3367 mode->channels[idx].chan = 14;
3368 else
3369 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
3370 else
3371 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
3372
3373 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3374 mode->channels[idx].flag |=
3375 HOSTAPD_CHAN_DISABLED;
3376 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
3377 mode->channels[idx].flag |=
3378 HOSTAPD_CHAN_PASSIVE_SCAN;
3379 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
3380 mode->channels[idx].flag |=
3381 HOSTAPD_CHAN_NO_IBSS;
3382 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
3383 mode->channels[idx].flag |=
3384 HOSTAPD_CHAN_RADAR;
3385
3386 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
3387 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3388 mode->channels[idx].max_tx_power =
3389 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
3390
3391 idx++;
3392 }
3393
3394 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3395 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3396 nla_len(nl_rate), rate_policy);
3397 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3398 continue;
3399 mode->num_rates++;
3400 }
3401
3402 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
3403 if (!mode->rates)
3404 return NL_SKIP;
3405
3406 idx = 0;
3407
3408 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3409 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3410 nla_len(nl_rate), rate_policy);
3411 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3412 continue;
3413 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
3414
3415 /* crude heuristic */
3416 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
3417 mode->rates[idx] > 200)
3418 mode->mode = HOSTAPD_MODE_IEEE80211G;
3419
3420 idx++;
3421 }
3422 }
3423
3424 return NL_SKIP;
3425}
3426
3427static struct hostapd_hw_modes *
3428wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
3429{
3430 u16 m;
3431 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
3432 int i, mode11g_idx = -1;
3433
3434 /* If only 802.11g mode is included, use it to construct matching
3435 * 802.11b mode data. */
3436
3437 for (m = 0; m < *num_modes; m++) {
3438 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
3439 return modes; /* 802.11b already included */
3440 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
3441 mode11g_idx = m;
3442 }
3443
3444 if (mode11g_idx < 0)
3445 return modes; /* 2.4 GHz band not supported at all */
3446
3447 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
3448 if (nmodes == NULL)
3449 return modes; /* Could not add 802.11b mode */
3450
3451 mode = &nmodes[*num_modes];
3452 os_memset(mode, 0, sizeof(*mode));
3453 (*num_modes)++;
3454 modes = nmodes;
3455
3456 mode->mode = HOSTAPD_MODE_IEEE80211B;
3457
3458 mode11g = &modes[mode11g_idx];
3459 mode->num_channels = mode11g->num_channels;
3460 mode->channels = os_malloc(mode11g->num_channels *
3461 sizeof(struct hostapd_channel_data));
3462 if (mode->channels == NULL) {
3463 (*num_modes)--;
3464 return modes; /* Could not add 802.11b mode */
3465 }
3466 os_memcpy(mode->channels, mode11g->channels,
3467 mode11g->num_channels * sizeof(struct hostapd_channel_data));
3468
3469 mode->num_rates = 0;
3470 mode->rates = os_malloc(4 * sizeof(int));
3471 if (mode->rates == NULL) {
3472 os_free(mode->channels);
3473 (*num_modes)--;
3474 return modes; /* Could not add 802.11b mode */
3475 }
3476
3477 for (i = 0; i < mode11g->num_rates; i++) {
3478 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
3479 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
3480 continue;
3481 mode->rates[mode->num_rates] = mode11g->rates[i];
3482 mode->num_rates++;
3483 if (mode->num_rates == 4)
3484 break;
3485 }
3486
3487 if (mode->num_rates == 0) {
3488 os_free(mode->channels);
3489 os_free(mode->rates);
3490 (*num_modes)--;
3491 return modes; /* No 802.11b rates */
3492 }
3493
3494 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
3495 "information");
3496
3497 return modes;
3498}
3499
3500
3501static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
3502 int end)
3503{
3504 int c;
3505
3506 for (c = 0; c < mode->num_channels; c++) {
3507 struct hostapd_channel_data *chan = &mode->channels[c];
3508 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
3509 chan->flag |= HOSTAPD_CHAN_HT40;
3510 }
3511}
3512
3513
3514static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
3515 int end)
3516{
3517 int c;
3518
3519 for (c = 0; c < mode->num_channels; c++) {
3520 struct hostapd_channel_data *chan = &mode->channels[c];
3521 if (!(chan->flag & HOSTAPD_CHAN_HT40))
3522 continue;
3523 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
3524 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
3525 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
3526 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
3527 }
3528}
3529
3530
3531static void nl80211_reg_rule_ht40(struct nlattr *tb[],
3532 struct phy_info_arg *results)
3533{
3534 u32 start, end, max_bw;
3535 u16 m;
3536
3537 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
3538 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
3539 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
3540 return;
3541
3542 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
3543 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
3544 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
3545
3546 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
3547 start, end, max_bw);
3548 if (max_bw < 40)
3549 return;
3550
3551 for (m = 0; m < *results->num_modes; m++) {
3552 if (!(results->modes[m].ht_capab &
3553 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
3554 continue;
3555 nl80211_set_ht40_mode(&results->modes[m], start, end);
3556 }
3557}
3558
3559
3560static void nl80211_reg_rule_sec(struct nlattr *tb[],
3561 struct phy_info_arg *results)
3562{
3563 u32 start, end, max_bw;
3564 u16 m;
3565
3566 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
3567 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
3568 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
3569 return;
3570
3571 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
3572 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
3573 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
3574
3575 if (max_bw < 20)
3576 return;
3577
3578 for (m = 0; m < *results->num_modes; m++) {
3579 if (!(results->modes[m].ht_capab &
3580 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
3581 continue;
3582 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
3583 }
3584}
3585
3586
3587static int nl80211_get_reg(struct nl_msg *msg, void *arg)
3588{
3589 struct phy_info_arg *results = arg;
3590 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3591 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3592 struct nlattr *nl_rule;
3593 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
3594 int rem_rule;
3595 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
3596 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3597 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3598 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3599 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3600 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3601 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3602 };
3603
3604 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3605 genlmsg_attrlen(gnlh, 0), NULL);
3606 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
3607 !tb_msg[NL80211_ATTR_REG_RULES]) {
3608 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
3609 "available");
3610 return NL_SKIP;
3611 }
3612
3613 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
3614 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
3615
3616 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
3617 {
3618 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
3619 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
3620 nl80211_reg_rule_ht40(tb_rule, results);
3621 }
3622
3623 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
3624 {
3625 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
3626 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
3627 nl80211_reg_rule_sec(tb_rule, results);
3628 }
3629
3630 return NL_SKIP;
3631}
3632
3633
3634static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
3635 struct phy_info_arg *results)
3636{
3637 struct nl_msg *msg;
3638
3639 msg = nlmsg_alloc();
3640 if (!msg)
3641 return -ENOMEM;
3642
3643 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3644 0, NL80211_CMD_GET_REG, 0);
3645 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
3646}
3647
3648
3649static struct hostapd_hw_modes *
3650wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
3651{
3652 struct i802_bss *bss = priv;
3653 struct wpa_driver_nl80211_data *drv = bss->drv;
3654 struct nl_msg *msg;
3655 struct phy_info_arg result = {
3656 .num_modes = num_modes,
3657 .modes = NULL,
3658 };
3659
3660 *num_modes = 0;
3661 *flags = 0;
3662
3663 msg = nlmsg_alloc();
3664 if (!msg)
3665 return NULL;
3666
3667 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3668 0, NL80211_CMD_GET_WIPHY, 0);
3669
3670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3671
3672 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
3673 nl80211_set_ht40_flags(drv, &result);
3674 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
3675 }
3676 nla_put_failure:
3677 return NULL;
3678}
3679
3680
3681static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
3682 const void *data, size_t len,
3683 int encrypt)
3684{
3685 __u8 rtap_hdr[] = {
3686 0x00, 0x00, /* radiotap version */
3687 0x0e, 0x00, /* radiotap length */
3688 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
3689 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
3690 0x00, /* padding */
3691 0x00, 0x00, /* RX and TX flags to indicate that */
3692 0x00, 0x00, /* this is the injected frame directly */
3693 };
3694 struct iovec iov[2] = {
3695 {
3696 .iov_base = &rtap_hdr,
3697 .iov_len = sizeof(rtap_hdr),
3698 },
3699 {
3700 .iov_base = (void *) data,
3701 .iov_len = len,
3702 }
3703 };
3704 struct msghdr msg = {
3705 .msg_name = NULL,
3706 .msg_namelen = 0,
3707 .msg_iov = iov,
3708 .msg_iovlen = 2,
3709 .msg_control = NULL,
3710 .msg_controllen = 0,
3711 .msg_flags = 0,
3712 };
3713 int res;
3714
3715 if (encrypt)
3716 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
3717
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003718 if (drv->monitor_sock < 0) {
3719 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
3720 "for %s", __func__);
3721 return -1;
3722 }
3723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 res = sendmsg(drv->monitor_sock, &msg, 0);
3725 if (res < 0) {
3726 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
3727 return -1;
3728 }
3729 return 0;
3730}
3731
3732
3733static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
3734 size_t data_len)
3735{
3736 struct i802_bss *bss = priv;
3737 struct wpa_driver_nl80211_data *drv = bss->drv;
3738 struct ieee80211_mgmt *mgmt;
3739 int encrypt = 1;
3740 u16 fc;
3741
3742 mgmt = (struct ieee80211_mgmt *) data;
3743 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (drv->nlmode == NL80211_IFTYPE_STATION &&
3745 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3746 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3747 /*
3748 * The use of last_mgmt_freq is a bit of a hack,
3749 * but it works due to the single-threaded nature
3750 * of wpa_supplicant.
3751 */
3752 return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0,
3753 data, data_len, NULL);
3754 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003755#ifdef ANDROID_BRCM_P2P_PATCH
3756 if (drv->nlmode == NL80211_IFTYPE_AP) {
3757 wpa_printf(MSG_DEBUG, "%s: Sending frame on ap_oper_freq %d using nl80211_send_frame_cmd", __func__, drv->ap_oper_freq);
3758 return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0,
3759 data, data_len, &drv->send_action_cookie);
3760 }
3761#else
3762 if (drv->no_monitor_iface_capab && drv->nlmode == NL80211_IFTYPE_AP ) {
3763 return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0,
3764 data, data_len, NULL);
3765 }
3766#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3768 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3769 /*
3770 * Only one of the authentication frame types is encrypted.
3771 * In order for static WEP encryption to work properly (i.e.,
3772 * to not encrypt the frame), we need to tell mac80211 about
3773 * the frames that must not be encrypted.
3774 */
3775 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3776 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3777 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3778 encrypt = 0;
3779 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003780 wpa_printf(MSG_DEBUG, "%s: Sending frame using monitor interface/l2 socket", __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
3782}
3783
3784
3785static int wpa_driver_nl80211_set_beacon(void *priv,
3786 const u8 *head, size_t head_len,
3787 const u8 *tail, size_t tail_len,
3788 int dtim_period, int beacon_int)
3789{
3790 struct i802_bss *bss = priv;
3791 struct wpa_driver_nl80211_data *drv = bss->drv;
3792 struct nl_msg *msg;
3793 u8 cmd = NL80211_CMD_NEW_BEACON;
3794 int ret;
3795 int beacon_set;
3796 int ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003797 beacon_set = bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798
3799 msg = nlmsg_alloc();
3800 if (!msg)
3801 return -ENOMEM;
3802
3803 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3804 beacon_set);
3805 if (beacon_set)
3806 cmd = NL80211_CMD_SET_BEACON;
3807
3808 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3809 0, cmd, 0);
3810 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
3811 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
3812 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3813 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
3814 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3815
3816 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3817 if (ret) {
3818 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3819 ret, strerror(-ret));
3820 } else {
3821 bss->beacon_set = 1;
3822 }
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07003823#if defined(ANDROID_BRCM_P2P_PATCH) && defined(HOSTAPD)
3824 wpa_driver_nl80211_probe_req_report(priv, 1);
3825#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 return ret;
3827 nla_put_failure:
3828 return -ENOBUFS;
3829}
3830
3831
3832static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
3833 int freq, int ht_enabled,
3834 int sec_channel_offset)
3835{
3836 struct nl_msg *msg;
3837 int ret;
3838
3839 msg = nlmsg_alloc();
3840 if (!msg)
3841 return -1;
3842
3843 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3844 NL80211_CMD_SET_WIPHY, 0);
3845
3846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3847 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
3848 if (ht_enabled) {
3849 switch (sec_channel_offset) {
3850 case -1:
3851 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3852 NL80211_CHAN_HT40MINUS);
3853 break;
3854 case 1:
3855 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3856 NL80211_CHAN_HT40PLUS);
3857 break;
3858 default:
Dmitry Shmidt44da0252011-08-23 12:30:30 -07003859#ifndef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07003860/* Should be change to HT20 as a default value because P2P firmware does not support 11n for BCM4329 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3862 NL80211_CHAN_HT20);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07003863#else
3864 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3865 NL80211_CHAN_NO_HT);
3866#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 break;
3868 }
3869 }
3870
3871 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3872 if (ret == 0)
3873 return 0;
3874 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
3875 "%d (%s)", freq, ret, strerror(-ret));
3876nla_put_failure:
3877 return -1;
3878}
3879
3880
3881static int wpa_driver_nl80211_sta_add(void *priv,
3882 struct hostapd_sta_add_params *params)
3883{
3884 struct i802_bss *bss = priv;
3885 struct wpa_driver_nl80211_data *drv = bss->drv;
3886 struct nl_msg *msg;
3887 int ret = -ENOBUFS;
3888
3889 msg = nlmsg_alloc();
3890 if (!msg)
3891 return -ENOMEM;
3892
3893 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3894 0, NL80211_CMD_NEW_STATION, 0);
3895
3896 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
3897 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
3898 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
3899 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
3900 params->supp_rates);
3901 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3902 params->listen_interval);
3903 if (params->ht_capabilities) {
3904 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
3905 sizeof(*params->ht_capabilities),
3906 params->ht_capabilities);
3907 }
3908
3909 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3910 if (ret)
3911 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
3912 "result: %d (%s)", ret, strerror(-ret));
3913 if (ret == -EEXIST)
3914 ret = 0;
3915 nla_put_failure:
3916 return ret;
3917}
3918
3919
3920static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
3921{
3922 struct i802_bss *bss = priv;
3923 struct wpa_driver_nl80211_data *drv = bss->drv;
3924 struct nl_msg *msg;
3925 int ret;
3926
3927 msg = nlmsg_alloc();
3928 if (!msg)
3929 return -ENOMEM;
3930
3931 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3932 0, NL80211_CMD_DEL_STATION, 0);
3933
3934 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3935 if_nametoindex(bss->ifname));
3936 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3937
3938 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3939 if (ret == -ENOENT)
3940 return 0;
3941 return ret;
3942 nla_put_failure:
3943 return -ENOBUFS;
3944}
3945
3946
3947static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
3948 int ifidx)
3949{
3950 struct nl_msg *msg;
3951
3952 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3953
3954#ifdef HOSTAPD
3955 /* stop listening for EAPOL on this interface */
3956 del_ifidx(drv, ifidx);
3957#endif /* HOSTAPD */
3958
3959 msg = nlmsg_alloc();
3960 if (!msg)
3961 goto nla_put_failure;
3962
3963 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3964 0, NL80211_CMD_DEL_INTERFACE, 0);
3965 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
3966
3967 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3968 return;
3969 nla_put_failure:
3970 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
3971}
3972
3973
3974static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3975 const char *ifname,
3976 enum nl80211_iftype iftype,
3977 const u8 *addr, int wds)
3978{
3979 struct nl_msg *msg, *flags = NULL;
3980 int ifidx;
3981 int ret = -ENOBUFS;
3982
3983 msg = nlmsg_alloc();
3984 if (!msg)
3985 return -1;
3986
3987 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3988 0, NL80211_CMD_NEW_INTERFACE, 0);
3989 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3990 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
3991 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
3992
3993 if (iftype == NL80211_IFTYPE_MONITOR) {
3994 int err;
3995
3996 flags = nlmsg_alloc();
3997 if (!flags)
3998 goto nla_put_failure;
3999
4000 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
4001
4002 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
4003
4004 nlmsg_free(flags);
4005
4006 if (err)
4007 goto nla_put_failure;
4008 } else if (wds) {
4009 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
4010 }
4011
4012 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4013 if (ret) {
4014 nla_put_failure:
4015 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4016 ifname, ret, strerror(-ret));
4017 return ret;
4018 }
4019
4020 ifidx = if_nametoindex(ifname);
4021 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4022 ifname, ifidx);
4023
4024 if (ifidx <= 0)
4025 return -1;
4026
4027#ifdef HOSTAPD
4028 /* start listening for EAPOL on this interface */
4029 add_ifidx(drv, ifidx);
4030#endif /* HOSTAPD */
4031
4032 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4033 linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) {
4034 nl80211_remove_iface(drv, ifidx);
4035 return -1;
4036 }
4037
4038 return ifidx;
4039}
4040
4041
4042static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4043 const char *ifname, enum nl80211_iftype iftype,
4044 const u8 *addr, int wds)
4045{
4046 int ret;
4047
4048 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
4049
4050 /* if error occured and interface exists already */
4051 if (ret == -ENFILE && if_nametoindex(ifname)) {
4052 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4053
4054 /* Try to remove the interface that was already there. */
4055 nl80211_remove_iface(drv, if_nametoindex(ifname));
4056
4057 /* Try to create the interface again */
4058 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
4059 wds);
4060 }
4061
4062 if (ret >= 0 && drv->disable_11b_rates)
4063 nl80211_disable_11b_rates(drv, ret, 1);
4064
4065 return ret;
4066}
4067
4068
4069static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
4070{
4071 struct ieee80211_hdr *hdr;
4072 u16 fc;
4073 union wpa_event_data event;
4074
4075 hdr = (struct ieee80211_hdr *) buf;
4076 fc = le_to_host16(hdr->frame_control);
4077
4078 os_memset(&event, 0, sizeof(event));
4079 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
4080 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
4081 event.tx_status.dst = hdr->addr1;
4082 event.tx_status.data = buf;
4083 event.tx_status.data_len = len;
4084 event.tx_status.ack = ok;
4085 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
4086}
4087
4088
4089static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
4090 u8 *buf, size_t len)
4091{
4092 union wpa_event_data event;
4093 os_memset(&event, 0, sizeof(event));
4094 event.rx_from_unknown.frame = buf;
4095 event.rx_from_unknown.len = len;
4096 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4097}
4098
4099
4100static void handle_frame(struct wpa_driver_nl80211_data *drv,
4101 u8 *buf, size_t len, int datarate, int ssi_signal)
4102{
4103 struct ieee80211_hdr *hdr;
4104 u16 fc;
4105 union wpa_event_data event;
4106
4107 hdr = (struct ieee80211_hdr *) buf;
4108 fc = le_to_host16(hdr->frame_control);
4109
4110 switch (WLAN_FC_GET_TYPE(fc)) {
4111 case WLAN_FC_TYPE_MGMT:
4112 os_memset(&event, 0, sizeof(event));
4113 event.rx_mgmt.frame = buf;
4114 event.rx_mgmt.frame_len = len;
4115 event.rx_mgmt.datarate = datarate;
4116 event.rx_mgmt.ssi_signal = ssi_signal;
4117 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
4118 break;
4119 case WLAN_FC_TYPE_CTRL:
4120 /* can only get here with PS-Poll frames */
4121 wpa_printf(MSG_DEBUG, "CTRL");
4122 from_unknown_sta(drv, buf, len);
4123 break;
4124 case WLAN_FC_TYPE_DATA:
4125 from_unknown_sta(drv, buf, len);
4126 break;
4127 }
4128}
4129
4130
4131static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
4132{
4133 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4134 int len;
4135 unsigned char buf[3000];
4136 struct ieee80211_radiotap_iterator iter;
4137 int ret;
4138 int datarate = 0, ssi_signal = 0;
4139 int injected = 0, failed = 0, rxflags = 0;
4140
4141 len = recv(sock, buf, sizeof(buf), 0);
4142 if (len < 0) {
4143 perror("recv");
4144 return;
4145 }
4146
4147 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
4148 printf("received invalid radiotap frame\n");
4149 return;
4150 }
4151
4152 while (1) {
4153 ret = ieee80211_radiotap_iterator_next(&iter);
4154 if (ret == -ENOENT)
4155 break;
4156 if (ret) {
4157 printf("received invalid radiotap frame (%d)\n", ret);
4158 return;
4159 }
4160 switch (iter.this_arg_index) {
4161 case IEEE80211_RADIOTAP_FLAGS:
4162 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
4163 len -= 4;
4164 break;
4165 case IEEE80211_RADIOTAP_RX_FLAGS:
4166 rxflags = 1;
4167 break;
4168 case IEEE80211_RADIOTAP_TX_FLAGS:
4169 injected = 1;
4170 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
4171 IEEE80211_RADIOTAP_F_TX_FAIL;
4172 break;
4173 case IEEE80211_RADIOTAP_DATA_RETRIES:
4174 break;
4175 case IEEE80211_RADIOTAP_CHANNEL:
4176 /* TODO: convert from freq/flags to channel number */
4177 break;
4178 case IEEE80211_RADIOTAP_RATE:
4179 datarate = *iter.this_arg * 5;
4180 break;
4181 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
4182 ssi_signal = *iter.this_arg;
4183 break;
4184 }
4185 }
4186
4187 if (rxflags && injected)
4188 return;
4189
4190 if (!injected)
4191 handle_frame(drv, buf + iter.max_length,
4192 len - iter.max_length, datarate, ssi_signal);
4193 else
4194 handle_tx_callback(drv->ctx, buf + iter.max_length,
4195 len - iter.max_length, !failed);
4196}
4197
4198
4199/*
4200 * we post-process the filter code later and rewrite
4201 * this to the offset to the last instruction
4202 */
4203#define PASS 0xFF
4204#define FAIL 0xFE
4205
4206static struct sock_filter msock_filter_insns[] = {
4207 /*
4208 * do a little-endian load of the radiotap length field
4209 */
4210 /* load lower byte into A */
4211 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
4212 /* put it into X (== index register) */
4213 BPF_STMT(BPF_MISC| BPF_TAX, 0),
4214 /* load upper byte into A */
4215 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
4216 /* left-shift it by 8 */
4217 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
4218 /* or with X */
4219 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
4220 /* put result into X */
4221 BPF_STMT(BPF_MISC| BPF_TAX, 0),
4222
4223 /*
4224 * Allow management frames through, this also gives us those
4225 * management frames that we sent ourselves with status
4226 */
4227 /* load the lower byte of the IEEE 802.11 frame control field */
4228 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4229 /* mask off frame type and version */
4230 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
4231 /* accept frame if it's both 0, fall through otherwise */
4232 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
4233
4234 /*
4235 * TODO: add a bit to radiotap RX flags that indicates
4236 * that the sending station is not associated, then
4237 * add a filter here that filters on our DA and that flag
4238 * to allow us to deauth frames to that bad station.
4239 *
4240 * For now allow all To DS data frames through.
4241 */
4242 /* load the IEEE 802.11 frame control field */
4243 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
4244 /* mask off frame type, version and DS status */
4245 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
4246 /* accept frame if version 0, type 2 and To DS, fall through otherwise
4247 */
4248 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
4249
4250#if 0
4251 /*
4252 * drop non-data frames
4253 */
4254 /* load the lower byte of the frame control field */
4255 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4256 /* mask off QoS bit */
4257 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
4258 /* drop non-data frames */
4259 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
4260#endif
4261 /* load the upper byte of the frame control field */
4262 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
4263 /* mask off toDS/fromDS */
4264 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
4265 /* accept WDS frames */
4266 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
4267
4268 /*
4269 * add header length to index
4270 */
4271 /* load the lower byte of the frame control field */
4272 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
4273 /* mask off QoS bit */
4274 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
4275 /* right shift it by 6 to give 0 or 2 */
4276 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
4277 /* add data frame header length */
4278 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
4279 /* add index, was start of 802.11 header */
4280 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
4281 /* move to index, now start of LL header */
4282 BPF_STMT(BPF_MISC | BPF_TAX, 0),
4283
4284 /*
4285 * Accept empty data frames, we use those for
4286 * polling activity.
4287 */
4288 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
4289 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
4290
4291 /*
4292 * Accept EAPOL frames
4293 */
4294 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
4295 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
4296 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
4297 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
4298
4299 /* keep these last two statements or change the code below */
4300 /* return 0 == "DROP" */
4301 BPF_STMT(BPF_RET | BPF_K, 0),
4302 /* return ~0 == "keep all" */
4303 BPF_STMT(BPF_RET | BPF_K, ~0),
4304};
4305
4306static struct sock_fprog msock_filter = {
4307 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
4308 .filter = msock_filter_insns,
4309};
4310
4311
4312static int add_monitor_filter(int s)
4313{
4314 int idx;
4315
4316 /* rewrite all PASS/FAIL jump offsets */
4317 for (idx = 0; idx < msock_filter.len; idx++) {
4318 struct sock_filter *insn = &msock_filter_insns[idx];
4319
4320 if (BPF_CLASS(insn->code) == BPF_JMP) {
4321 if (insn->code == (BPF_JMP|BPF_JA)) {
4322 if (insn->k == PASS)
4323 insn->k = msock_filter.len - idx - 2;
4324 else if (insn->k == FAIL)
4325 insn->k = msock_filter.len - idx - 3;
4326 }
4327
4328 if (insn->jt == PASS)
4329 insn->jt = msock_filter.len - idx - 2;
4330 else if (insn->jt == FAIL)
4331 insn->jt = msock_filter.len - idx - 3;
4332
4333 if (insn->jf == PASS)
4334 insn->jf = msock_filter.len - idx - 2;
4335 else if (insn->jf == FAIL)
4336 insn->jf = msock_filter.len - idx - 3;
4337 }
4338 }
4339
4340 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
4341 &msock_filter, sizeof(msock_filter))) {
4342 perror("SO_ATTACH_FILTER");
4343 return -1;
4344 }
4345
4346 return 0;
4347}
4348
4349
4350static void nl80211_remove_monitor_interface(
4351 struct wpa_driver_nl80211_data *drv)
4352{
4353 if (drv->monitor_ifidx >= 0) {
4354 nl80211_remove_iface(drv, drv->monitor_ifidx);
4355 drv->monitor_ifidx = -1;
4356 }
4357 if (drv->monitor_sock >= 0) {
4358 eloop_unregister_read_sock(drv->monitor_sock);
4359 close(drv->monitor_sock);
4360 drv->monitor_sock = -1;
4361 }
4362}
4363
4364
4365static int
4366nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
4367{
4368 char buf[IFNAMSIZ];
4369 struct sockaddr_ll ll;
4370 int optval;
4371 socklen_t optlen;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07004372#ifdef ANDROID_BRCM_P2P_PATCH
4373 snprintf(buf, IFNAMSIZ, "%s%s", WPA_MONITOR_IFNAME_PREFIX, drv->first_bss.ifname);
4374#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07004376#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 buf[IFNAMSIZ - 1] = '\0';
4378
4379 drv->monitor_ifidx =
4380 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
4381 0);
4382
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004383 if (drv->monitor_ifidx == -EOPNOTSUPP) {
4384 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
4385 "monitor interface type - try to run without it");
4386 drv->no_monitor_iface_capab = 1;
4387 }
4388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 if (drv->monitor_ifidx < 0)
4390 return -1;
4391
4392 if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
4393 goto error;
4394
4395 memset(&ll, 0, sizeof(ll));
4396 ll.sll_family = AF_PACKET;
4397 ll.sll_ifindex = drv->monitor_ifidx;
4398 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
4399 if (drv->monitor_sock < 0) {
4400 perror("socket[PF_PACKET,SOCK_RAW]");
4401 goto error;
4402 }
4403
4404 if (add_monitor_filter(drv->monitor_sock)) {
4405 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
4406 "interface; do filtering in user space");
4407 /* This works, but will cost in performance. */
4408 }
4409
4410 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
4411 perror("monitor socket bind");
4412 goto error;
4413 }
4414
4415 optlen = sizeof(optval);
4416 optval = 20;
4417 if (setsockopt
4418 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
4419 perror("Failed to set socket priority");
4420 goto error;
4421 }
4422
4423 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
4424 drv, NULL)) {
4425 printf("Could not register monitor read socket\n");
4426 goto error;
4427 }
4428
4429 return 0;
4430 error:
4431 nl80211_remove_monitor_interface(drv);
4432 return -1;
4433}
4434
4435
4436static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4437
4438static int wpa_driver_nl80211_hapd_send_eapol(
4439 void *priv, const u8 *addr, const u8 *data,
4440 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4441{
4442 struct i802_bss *bss = priv;
4443 struct wpa_driver_nl80211_data *drv = bss->drv;
4444 struct ieee80211_hdr *hdr;
4445 size_t len;
4446 u8 *pos;
4447 int res;
4448 int qos = flags & WPA_STA_WMM;
4449
4450 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4451 data_len;
4452 hdr = os_zalloc(len);
4453 if (hdr == NULL) {
4454 printf("malloc() failed for i802_send_data(len=%lu)\n",
4455 (unsigned long) len);
4456 return -1;
4457 }
4458
4459 hdr->frame_control =
4460 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4461 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4462 if (encrypt)
4463 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4464 if (qos) {
4465 hdr->frame_control |=
4466 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4467 }
4468
4469 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4470 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4471 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4472 pos = (u8 *) (hdr + 1);
4473
4474 if (qos) {
4475 /* add an empty QoS header if needed */
4476 pos[0] = 0;
4477 pos[1] = 0;
4478 pos += 2;
4479 }
4480
4481 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4482 pos += sizeof(rfc1042_header);
4483 WPA_PUT_BE16(pos, ETH_P_PAE);
4484 pos += 2;
4485 memcpy(pos, data, data_len);
4486
4487 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
4488 if (res < 0) {
4489 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4490 "failed: %d (%s)",
4491 (unsigned long) len, errno, strerror(errno));
4492 }
4493 os_free(hdr);
4494
4495 return res;
4496}
4497
4498
4499static u32 sta_flags_nl80211(int flags)
4500{
4501 u32 f = 0;
4502
4503 if (flags & WPA_STA_AUTHORIZED)
4504 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4505 if (flags & WPA_STA_WMM)
4506 f |= BIT(NL80211_STA_FLAG_WME);
4507 if (flags & WPA_STA_SHORT_PREAMBLE)
4508 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4509 if (flags & WPA_STA_MFP)
4510 f |= BIT(NL80211_STA_FLAG_MFP);
4511
4512 return f;
4513}
4514
4515
4516static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4517 int total_flags,
4518 int flags_or, int flags_and)
4519{
4520 struct i802_bss *bss = priv;
4521 struct wpa_driver_nl80211_data *drv = bss->drv;
4522 struct nl_msg *msg, *flags = NULL;
4523 struct nl80211_sta_flag_update upd;
4524
4525 msg = nlmsg_alloc();
4526 if (!msg)
4527 return -ENOMEM;
4528
4529 flags = nlmsg_alloc();
4530 if (!flags) {
4531 nlmsg_free(msg);
4532 return -ENOMEM;
4533 }
4534
4535 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4536 0, NL80211_CMD_SET_STATION, 0);
4537
4538 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4539 if_nametoindex(bss->ifname));
4540 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4541
4542 /*
4543 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4544 * can be removed eventually.
4545 */
4546 if (total_flags & WPA_STA_AUTHORIZED)
4547 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
4548
4549 if (total_flags & WPA_STA_WMM)
4550 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
4551
4552 if (total_flags & WPA_STA_SHORT_PREAMBLE)
4553 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
4554
4555 if (total_flags & WPA_STA_MFP)
4556 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
4557
4558 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
4559 goto nla_put_failure;
4560
4561 os_memset(&upd, 0, sizeof(upd));
4562 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4563 upd.set = sta_flags_nl80211(flags_or);
4564 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
4565
4566 nlmsg_free(flags);
4567
4568 return send_and_recv_msgs(drv, msg, NULL, NULL);
4569 nla_put_failure:
4570 nlmsg_free(flags);
4571 return -ENOBUFS;
4572}
4573
4574
4575static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4576 struct wpa_driver_associate_params *params)
4577{
4578 if (params->p2p)
4579 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4580 "group (GO)");
4581 if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) ||
4582 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
4583 nl80211_remove_monitor_interface(drv);
4584 return -1;
4585 }
4586
4587 /* TODO: setup monitor interface (and add code somewhere to remove this
4588 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07004589 wpa_printf(MSG_DEBUG, "nl80211: Update ap_oper_freq with params->freq %d", params->freq);
4590 drv->ap_oper_freq = params->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591
4592 return 0;
4593}
4594
4595
4596static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
4597{
4598 struct nl_msg *msg;
4599 int ret = -1;
4600
4601 msg = nlmsg_alloc();
4602 if (!msg)
4603 return -1;
4604
4605 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4606 NL80211_CMD_LEAVE_IBSS, 0);
4607 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4608 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4609 msg = NULL;
4610 if (ret) {
4611 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4612 "(%s)", ret, strerror(-ret));
4613 goto nla_put_failure;
4614 }
4615
4616 ret = 0;
4617 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
4618
4619nla_put_failure:
4620 nlmsg_free(msg);
4621 return ret;
4622}
4623
4624
4625static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4626 struct wpa_driver_associate_params *params)
4627{
4628 struct nl_msg *msg;
4629 int ret = -1;
4630 int count = 0;
4631
4632 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4633
4634 if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode)) {
4635 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4636 "IBSS mode");
4637 return -1;
4638 }
4639
4640retry:
4641 msg = nlmsg_alloc();
4642 if (!msg)
4643 return -1;
4644
4645 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4646 NL80211_CMD_JOIN_IBSS, 0);
4647 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4648
4649 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4650 goto nla_put_failure;
4651
4652 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4653 params->ssid, params->ssid_len);
4654 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4655 params->ssid);
4656 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4657 drv->ssid_len = params->ssid_len;
4658
4659 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4660 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4661
4662 ret = nl80211_set_conn_keys(params, msg);
4663 if (ret)
4664 goto nla_put_failure;
4665
4666 if (params->wpa_ie) {
4667 wpa_hexdump(MSG_DEBUG,
4668 " * Extra IEs for Beacon/Probe Response frames",
4669 params->wpa_ie, params->wpa_ie_len);
4670 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4671 params->wpa_ie);
4672 }
4673
4674 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4675 msg = NULL;
4676 if (ret) {
4677 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4678 ret, strerror(-ret));
4679 count++;
4680 if (ret == -EALREADY && count == 1) {
4681 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4682 "forced leave");
4683 nl80211_leave_ibss(drv);
4684 nlmsg_free(msg);
4685 goto retry;
4686 }
4687
4688 goto nla_put_failure;
4689 }
4690 ret = 0;
4691 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
4692
4693nla_put_failure:
4694 nlmsg_free(msg);
4695 return ret;
4696}
4697
4698
4699static int wpa_driver_nl80211_connect(
4700 struct wpa_driver_nl80211_data *drv,
4701 struct wpa_driver_associate_params *params)
4702{
4703 struct nl_msg *msg;
4704 enum nl80211_auth_type type;
4705 int ret = 0;
4706 int algs;
4707
4708 msg = nlmsg_alloc();
4709 if (!msg)
4710 return -1;
4711
4712 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4713 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4714 NL80211_CMD_CONNECT, 0);
4715
4716 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4717 if (params->bssid) {
4718 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4719 MAC2STR(params->bssid));
4720 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4721 }
4722 if (params->freq) {
4723 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4724 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4725 }
4726 if (params->ssid) {
4727 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4728 params->ssid, params->ssid_len);
4729 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4730 params->ssid);
4731 if (params->ssid_len > sizeof(drv->ssid))
4732 goto nla_put_failure;
4733 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4734 drv->ssid_len = params->ssid_len;
4735 }
4736 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
4737 if (params->wpa_ie)
4738 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4739 params->wpa_ie);
4740
4741 algs = 0;
4742 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4743 algs++;
4744 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4745 algs++;
4746 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4747 algs++;
4748 if (algs > 1) {
4749 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4750 "selection");
4751 goto skip_auth_type;
4752 }
4753
4754 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4755 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4756 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4757 type = NL80211_AUTHTYPE_SHARED_KEY;
4758 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4759 type = NL80211_AUTHTYPE_NETWORK_EAP;
4760 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4761 type = NL80211_AUTHTYPE_FT;
4762 else
4763 goto nla_put_failure;
4764
4765 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4766 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4767
4768skip_auth_type:
4769 if (params->wpa_ie && params->wpa_ie_len) {
4770 enum nl80211_wpa_versions ver;
4771
4772 if (params->wpa_ie[0] == WLAN_EID_RSN)
4773 ver = NL80211_WPA_VERSION_2;
4774 else
4775 ver = NL80211_WPA_VERSION_1;
4776
4777 wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
4778 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
4779 }
4780
4781 if (params->pairwise_suite != CIPHER_NONE) {
4782 int cipher;
4783
4784 switch (params->pairwise_suite) {
4785 case CIPHER_WEP40:
4786 cipher = WLAN_CIPHER_SUITE_WEP40;
4787 break;
4788 case CIPHER_WEP104:
4789 cipher = WLAN_CIPHER_SUITE_WEP104;
4790 break;
4791 case CIPHER_CCMP:
4792 cipher = WLAN_CIPHER_SUITE_CCMP;
4793 break;
4794 case CIPHER_TKIP:
4795 default:
4796 cipher = WLAN_CIPHER_SUITE_TKIP;
4797 break;
4798 }
4799 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
4800 }
4801
4802 if (params->group_suite != CIPHER_NONE) {
4803 int cipher;
4804
4805 switch (params->group_suite) {
4806 case CIPHER_WEP40:
4807 cipher = WLAN_CIPHER_SUITE_WEP40;
4808 break;
4809 case CIPHER_WEP104:
4810 cipher = WLAN_CIPHER_SUITE_WEP104;
4811 break;
4812 case CIPHER_CCMP:
4813 cipher = WLAN_CIPHER_SUITE_CCMP;
4814 break;
4815 case CIPHER_TKIP:
4816 default:
4817 cipher = WLAN_CIPHER_SUITE_TKIP;
4818 break;
4819 }
4820 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
4821 }
4822
4823 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
4824 params->key_mgmt_suite == KEY_MGMT_PSK) {
4825 int mgmt = WLAN_AKM_SUITE_PSK;
4826
4827 switch (params->key_mgmt_suite) {
4828 case KEY_MGMT_802_1X:
4829 mgmt = WLAN_AKM_SUITE_8021X;
4830 break;
4831 case KEY_MGMT_PSK:
4832 default:
4833 mgmt = WLAN_AKM_SUITE_PSK;
4834 break;
4835 }
4836 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
4837 }
4838
4839 ret = nl80211_set_conn_keys(params, msg);
4840 if (ret)
4841 goto nla_put_failure;
4842
4843 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4844 msg = NULL;
4845 if (ret) {
4846 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4847 "(%s)", ret, strerror(-ret));
4848 goto nla_put_failure;
4849 }
4850 ret = 0;
4851 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
4852
4853nla_put_failure:
4854 nlmsg_free(msg);
4855 return ret;
4856
4857}
4858
4859
4860static int wpa_driver_nl80211_associate(
4861 void *priv, struct wpa_driver_associate_params *params)
4862{
4863 struct i802_bss *bss = priv;
4864 struct wpa_driver_nl80211_data *drv = bss->drv;
4865 int ret = -1;
4866 struct nl_msg *msg;
4867
4868 if (params->mode == IEEE80211_MODE_AP)
4869 return wpa_driver_nl80211_ap(drv, params);
4870
4871 if (params->mode == IEEE80211_MODE_IBSS)
4872 return wpa_driver_nl80211_ibss(drv, params);
4873
4874 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
4875 if (wpa_driver_nl80211_set_mode(priv, params->mode) < 0)
4876 return -1;
4877 return wpa_driver_nl80211_connect(drv, params);
4878 }
4879
4880 drv->associated = 0;
4881
4882 msg = nlmsg_alloc();
4883 if (!msg)
4884 return -1;
4885
4886 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4887 drv->ifindex);
4888 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4889 NL80211_CMD_ASSOCIATE, 0);
4890
4891 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4892 if (params->bssid) {
4893 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4894 MAC2STR(params->bssid));
4895 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4896 }
4897 if (params->freq) {
4898 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4899 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4900 drv->assoc_freq = params->freq;
4901 } else
4902 drv->assoc_freq = 0;
4903 if (params->ssid) {
4904 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4905 params->ssid, params->ssid_len);
4906 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4907 params->ssid);
4908 if (params->ssid_len > sizeof(drv->ssid))
4909 goto nla_put_failure;
4910 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4911 drv->ssid_len = params->ssid_len;
4912 }
4913 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
4914 if (params->wpa_ie)
4915 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4916 params->wpa_ie);
4917
4918 if (params->pairwise_suite != CIPHER_NONE) {
4919 int cipher;
4920
4921 switch (params->pairwise_suite) {
4922 case CIPHER_WEP40:
4923 cipher = WLAN_CIPHER_SUITE_WEP40;
4924 break;
4925 case CIPHER_WEP104:
4926 cipher = WLAN_CIPHER_SUITE_WEP104;
4927 break;
4928 case CIPHER_CCMP:
4929 cipher = WLAN_CIPHER_SUITE_CCMP;
4930 break;
4931 case CIPHER_TKIP:
4932 default:
4933 cipher = WLAN_CIPHER_SUITE_TKIP;
4934 break;
4935 }
4936 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
4937 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
4938 }
4939
4940 if (params->group_suite != CIPHER_NONE) {
4941 int cipher;
4942
4943 switch (params->group_suite) {
4944 case CIPHER_WEP40:
4945 cipher = WLAN_CIPHER_SUITE_WEP40;
4946 break;
4947 case CIPHER_WEP104:
4948 cipher = WLAN_CIPHER_SUITE_WEP104;
4949 break;
4950 case CIPHER_CCMP:
4951 cipher = WLAN_CIPHER_SUITE_CCMP;
4952 break;
4953 case CIPHER_TKIP:
4954 default:
4955 cipher = WLAN_CIPHER_SUITE_TKIP;
4956 break;
4957 }
4958 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
4959 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
4960 }
4961
4962#ifdef CONFIG_IEEE80211W
4963 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
4964 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
4965#endif /* CONFIG_IEEE80211W */
4966
4967 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
4968
4969 if (params->prev_bssid) {
4970 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4971 MAC2STR(params->prev_bssid));
4972 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4973 params->prev_bssid);
4974 }
4975
4976 if (params->p2p)
4977 wpa_printf(MSG_DEBUG, " * P2P group");
4978
4979 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4980 msg = NULL;
4981 if (ret) {
4982 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
4983 "(%s)", ret, strerror(-ret));
4984 nl80211_dump_scan(drv);
4985 goto nla_put_failure;
4986 }
4987 ret = 0;
4988 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
4989 "successfully");
4990
4991nla_put_failure:
4992 nlmsg_free(msg);
4993 return ret;
4994}
4995
4996
4997static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
4998 int ifindex, int mode)
4999{
5000 struct nl_msg *msg;
5001 int ret = -ENOBUFS;
5002
5003 msg = nlmsg_alloc();
5004 if (!msg)
5005 return -ENOMEM;
5006
5007 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5008 0, NL80211_CMD_SET_INTERFACE, 0);
5009 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5010 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
5011
5012 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5013 if (!ret)
5014 return 0;
5015nla_put_failure:
5016 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5017 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5018 return ret;
5019}
5020
5021
5022static int wpa_driver_nl80211_set_mode(void *priv, int mode)
5023{
5024 struct i802_bss *bss = priv;
5025 struct wpa_driver_nl80211_data *drv = bss->drv;
5026 int ret = -1;
5027 int nlmode;
5028 int i;
5029
5030 switch (mode) {
5031 case 0:
5032 nlmode = NL80211_IFTYPE_STATION;
5033 break;
5034 case 1:
5035 nlmode = NL80211_IFTYPE_ADHOC;
5036 break;
5037 case 2:
5038 nlmode = NL80211_IFTYPE_AP;
5039 break;
5040 default:
5041 return -1;
5042 }
5043
5044 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
5045 drv->nlmode = nlmode;
5046 ret = 0;
5047 goto done;
5048 }
5049
5050 if (nlmode == drv->nlmode) {
5051 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5052 "requested mode - ignore error");
5053 ret = 0;
5054 goto done; /* Already in the requested mode */
5055 }
5056
5057 /* mac80211 doesn't allow mode changes while the device is up, so
5058 * take the device down, try to set the mode again, and bring the
5059 * device back up.
5060 */
5061 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5062 "interface down");
5063 for (i = 0; i < 10; i++) {
5064 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0) ==
5065 0) {
5066 /* Try to set the mode again while the interface is
5067 * down */
5068 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
5069 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname,
5070 1))
5071 ret = -1;
5072 if (!ret)
5073 break;
5074 } else
5075 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5076 "interface down");
5077 os_sleep(0, 100000);
5078 }
5079
5080 if (!ret) {
5081 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5082 "interface is down");
5083 drv->nlmode = nlmode;
5084 }
5085
5086done:
5087 if (!ret && nlmode == NL80211_IFTYPE_AP) {
5088 /* Setup additional AP mode functionality if needed */
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07005089 if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 &&
5090 nl80211_create_monitor_interface(drv) &&
5091 !drv->no_monitor_iface_capab)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 return -1;
5093 } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
5094 /* Remove additional AP mode functionality */
5095 nl80211_remove_monitor_interface(drv);
5096 bss->beacon_set = 0;
5097 }
5098
5099 if (ret)
5100 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5101 "from %d failed", nlmode, drv->nlmode);
5102
5103 return ret;
5104}
5105
5106
5107static int wpa_driver_nl80211_get_capa(void *priv,
5108 struct wpa_driver_capa *capa)
5109{
5110 struct i802_bss *bss = priv;
5111 struct wpa_driver_nl80211_data *drv = bss->drv;
5112 if (!drv->has_capability)
5113 return -1;
5114 os_memcpy(capa, &drv->capa, sizeof(*capa));
5115 return 0;
5116}
5117
5118
5119static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5120{
5121 struct i802_bss *bss = priv;
5122 struct wpa_driver_nl80211_data *drv = bss->drv;
5123
5124 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
5125 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
5126 drv->operstate = state;
5127 return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
5128 state ? IF_OPER_UP : IF_OPER_DORMANT);
5129}
5130
5131
5132static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5133{
5134 struct i802_bss *bss = priv;
5135 struct wpa_driver_nl80211_data *drv = bss->drv;
5136 struct nl_msg *msg;
5137 struct nl80211_sta_flag_update upd;
5138
5139 msg = nlmsg_alloc();
5140 if (!msg)
5141 return -ENOMEM;
5142
5143 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5144 0, NL80211_CMD_SET_STATION, 0);
5145
5146 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5147 if_nametoindex(bss->ifname));
5148 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
5149
5150 os_memset(&upd, 0, sizeof(upd));
5151 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5152 if (authorized)
5153 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
5154 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5155
5156 return send_and_recv_msgs(drv, msg, NULL, NULL);
5157 nla_put_failure:
5158 return -ENOBUFS;
5159}
5160
5161
Jouni Malinen75ecf522011-06-27 15:19:46 -07005162/* Set kernel driver on given frequency (MHz) */
5163static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005165 struct i802_bss *bss = priv;
5166 struct wpa_driver_nl80211_data *drv = bss->drv;
5167 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
5168 freq->sec_channel_offset);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169}
5170
5171
Jouni Malinen75ecf522011-06-27 15:19:46 -07005172#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173
5174static inline int min_int(int a, int b)
5175{
5176 if (a < b)
5177 return a;
5178 return b;
5179}
5180
5181
5182static int get_key_handler(struct nl_msg *msg, void *arg)
5183{
5184 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5185 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5186
5187 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5188 genlmsg_attrlen(gnlh, 0), NULL);
5189
5190 /*
5191 * TODO: validate the key index and mac address!
5192 * Otherwise, there's a race condition as soon as
5193 * the kernel starts sending key notifications.
5194 */
5195
5196 if (tb[NL80211_ATTR_KEY_SEQ])
5197 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5198 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5199 return NL_SKIP;
5200}
5201
5202
5203static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5204 int idx, u8 *seq)
5205{
5206 struct i802_bss *bss = priv;
5207 struct wpa_driver_nl80211_data *drv = bss->drv;
5208 struct nl_msg *msg;
5209
5210 msg = nlmsg_alloc();
5211 if (!msg)
5212 return -ENOMEM;
5213
5214 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5215 0, NL80211_CMD_GET_KEY, 0);
5216
5217 if (addr)
5218 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5219 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
5220 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
5221
5222 memset(seq, 0, 6);
5223
5224 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
5225 nla_put_failure:
5226 return -ENOBUFS;
5227}
5228
5229
5230static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
5231 int mode)
5232{
5233 struct i802_bss *bss = priv;
5234 struct wpa_driver_nl80211_data *drv = bss->drv;
5235 struct nl_msg *msg;
5236 u8 rates[NL80211_MAX_SUPP_RATES];
5237 u8 rates_len = 0;
5238 int i;
5239
5240 msg = nlmsg_alloc();
5241 if (!msg)
5242 return -ENOMEM;
5243
5244 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
5245 NL80211_CMD_SET_BSS, 0);
5246
5247 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
5248 rates[rates_len++] = basic_rates[i] / 5;
5249
5250 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5251
5252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5253
5254 return send_and_recv_msgs(drv, msg, NULL, NULL);
5255 nla_put_failure:
5256 return -ENOBUFS;
5257}
5258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259
5260static int i802_set_rts(void *priv, int rts)
5261{
5262 struct i802_bss *bss = priv;
5263 struct wpa_driver_nl80211_data *drv = bss->drv;
5264 struct nl_msg *msg;
5265 int ret = -ENOBUFS;
5266 u32 val;
5267
5268 msg = nlmsg_alloc();
5269 if (!msg)
5270 return -ENOMEM;
5271
5272 if (rts >= 2347)
5273 val = (u32) -1;
5274 else
5275 val = rts;
5276
5277 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5278 0, NL80211_CMD_SET_WIPHY, 0);
5279 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5280 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
5281
5282 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5283 if (!ret)
5284 return 0;
5285nla_put_failure:
5286 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5287 "%d (%s)", rts, ret, strerror(-ret));
5288 return ret;
5289}
5290
5291
5292static int i802_set_frag(void *priv, int frag)
5293{
5294 struct i802_bss *bss = priv;
5295 struct wpa_driver_nl80211_data *drv = bss->drv;
5296 struct nl_msg *msg;
5297 int ret = -ENOBUFS;
5298 u32 val;
5299
5300 msg = nlmsg_alloc();
5301 if (!msg)
5302 return -ENOMEM;
5303
5304 if (frag >= 2346)
5305 val = (u32) -1;
5306 else
5307 val = frag;
5308
5309 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5310 0, NL80211_CMD_SET_WIPHY, 0);
5311 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5312 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
5313
5314 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5315 if (!ret)
5316 return 0;
5317nla_put_failure:
5318 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5319 "%d: %d (%s)", frag, ret, strerror(-ret));
5320 return ret;
5321}
5322
5323
5324static int i802_flush(void *priv)
5325{
5326 struct i802_bss *bss = priv;
5327 struct wpa_driver_nl80211_data *drv = bss->drv;
5328 struct nl_msg *msg;
5329
5330 msg = nlmsg_alloc();
5331 if (!msg)
5332 return -1;
5333
5334 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5335 0, NL80211_CMD_DEL_STATION, 0);
5336
5337 /*
5338 * XXX: FIX! this needs to flush all VLANs too
5339 */
5340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5341 if_nametoindex(bss->ifname));
5342
5343 return send_and_recv_msgs(drv, msg, NULL, NULL);
5344 nla_put_failure:
5345 return -ENOBUFS;
5346}
5347
5348
5349static int get_sta_handler(struct nl_msg *msg, void *arg)
5350{
5351 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5352 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5353 struct hostap_sta_driver_data *data = arg;
5354 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5355 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5356 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5357 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5358 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5359 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5360 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
5361 };
5362
5363 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5364 genlmsg_attrlen(gnlh, 0), NULL);
5365
5366 /*
5367 * TODO: validate the interface and mac address!
5368 * Otherwise, there's a race condition as soon as
5369 * the kernel starts sending station notifications.
5370 */
5371
5372 if (!tb[NL80211_ATTR_STA_INFO]) {
5373 wpa_printf(MSG_DEBUG, "sta stats missing!");
5374 return NL_SKIP;
5375 }
5376 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5377 tb[NL80211_ATTR_STA_INFO],
5378 stats_policy)) {
5379 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5380 return NL_SKIP;
5381 }
5382
5383 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5384 data->inactive_msec =
5385 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5386 if (stats[NL80211_STA_INFO_RX_BYTES])
5387 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5388 if (stats[NL80211_STA_INFO_TX_BYTES])
5389 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5390 if (stats[NL80211_STA_INFO_RX_PACKETS])
5391 data->rx_packets =
5392 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5393 if (stats[NL80211_STA_INFO_TX_PACKETS])
5394 data->tx_packets =
5395 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
5396
5397 return NL_SKIP;
5398}
5399
5400static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
5401 const u8 *addr)
5402{
5403 struct i802_bss *bss = priv;
5404 struct wpa_driver_nl80211_data *drv = bss->drv;
5405 struct nl_msg *msg;
5406
5407 os_memset(data, 0, sizeof(*data));
5408 msg = nlmsg_alloc();
5409 if (!msg)
5410 return -ENOMEM;
5411
5412 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5413 0, NL80211_CMD_GET_STATION, 0);
5414
5415 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5416 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5417
5418 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
5419 nla_put_failure:
5420 return -ENOBUFS;
5421}
5422
5423
5424static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5425 int cw_min, int cw_max, int burst_time)
5426{
5427 struct i802_bss *bss = priv;
5428 struct wpa_driver_nl80211_data *drv = bss->drv;
5429 struct nl_msg *msg;
5430 struct nlattr *txq, *params;
5431
5432 msg = nlmsg_alloc();
5433 if (!msg)
5434 return -1;
5435
5436 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5437 0, NL80211_CMD_SET_WIPHY, 0);
5438
5439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5440
5441 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5442 if (!txq)
5443 goto nla_put_failure;
5444
5445 /* We are only sending parameters for a single TXQ at a time */
5446 params = nla_nest_start(msg, 1);
5447 if (!params)
5448 goto nla_put_failure;
5449
5450 switch (queue) {
5451 case 0:
5452 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
5453 break;
5454 case 1:
5455 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
5456 break;
5457 case 2:
5458 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
5459 break;
5460 case 3:
5461 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
5462 break;
5463 }
5464 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5465 * 32 usec, so need to convert the value here. */
5466 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
5467 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
5468 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
5469 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
5470
5471 nla_nest_end(msg, params);
5472
5473 nla_nest_end(msg, txq);
5474
5475 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5476 return 0;
5477 nla_put_failure:
5478 return -1;
5479}
5480
5481
5482static int i802_set_bss(void *priv, int cts, int preamble, int slot,
5483 int ht_opmode)
5484{
5485 struct i802_bss *bss = priv;
5486 struct wpa_driver_nl80211_data *drv = bss->drv;
5487 struct nl_msg *msg;
5488
5489 msg = nlmsg_alloc();
5490 if (!msg)
5491 return -ENOMEM;
5492
5493 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
5494 NL80211_CMD_SET_BSS, 0);
5495
5496 if (cts >= 0)
5497 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5498 if (preamble >= 0)
5499 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5500 if (slot >= 0)
5501 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5502 if (ht_opmode >= 0)
5503 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5504 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5505
5506 return send_and_recv_msgs(drv, msg, NULL, NULL);
5507 nla_put_failure:
5508 return -ENOBUFS;
5509}
5510
5511
5512static int i802_set_cts_protect(void *priv, int value)
5513{
5514 return i802_set_bss(priv, value, -1, -1, -1);
5515}
5516
5517
5518static int i802_set_preamble(void *priv, int value)
5519{
5520 return i802_set_bss(priv, -1, value, -1, -1);
5521}
5522
5523
5524static int i802_set_short_slot_time(void *priv, int value)
5525{
5526 return i802_set_bss(priv, -1, -1, value, -1);
5527}
5528
5529
5530static int i802_set_sta_vlan(void *priv, const u8 *addr,
5531 const char *ifname, int vlan_id)
5532{
5533 struct i802_bss *bss = priv;
5534 struct wpa_driver_nl80211_data *drv = bss->drv;
5535 struct nl_msg *msg;
5536 int ret = -ENOBUFS;
5537
5538 msg = nlmsg_alloc();
5539 if (!msg)
5540 return -ENOMEM;
5541
5542 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
5543 0, NL80211_CMD_SET_STATION, 0);
5544
5545 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5546 if_nametoindex(bss->ifname));
5547 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5548 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
5549 if_nametoindex(ifname));
5550
5551 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5552 if (ret < 0) {
5553 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5554 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5555 MAC2STR(addr), ifname, vlan_id, ret,
5556 strerror(-ret));
5557 }
5558 nla_put_failure:
5559 return ret;
5560}
5561
5562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563static int i802_set_ht_params(void *priv, const u8 *ht_capab,
5564 size_t ht_capab_len, const u8 *ht_oper,
5565 size_t ht_oper_len)
5566{
5567 if (ht_oper_len >= 6) {
5568 /* ht opmode uses 16bit in octet 5 & 6 */
5569 u16 ht_opmode = le_to_host16(((u16 *) ht_oper)[2]);
5570 return i802_set_bss(priv, -1, -1, -1, ht_opmode);
5571 } else
5572 return -1;
5573}
5574
5575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576static int i802_get_inact_sec(void *priv, const u8 *addr)
5577{
5578 struct hostap_sta_driver_data data;
5579 int ret;
5580
5581 data.inactive_msec = (unsigned long) -1;
5582 ret = i802_read_sta_data(priv, &data, addr);
5583 if (ret || data.inactive_msec == (unsigned long) -1)
5584 return -1;
5585 return data.inactive_msec / 1000;
5586}
5587
5588
5589static int i802_sta_clear_stats(void *priv, const u8 *addr)
5590{
5591#if 0
5592 /* TODO */
5593#endif
5594 return 0;
5595}
5596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597
5598static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5599 int reason)
5600{
5601 struct i802_bss *bss = priv;
5602 struct ieee80211_mgmt mgmt;
5603
5604 memset(&mgmt, 0, sizeof(mgmt));
5605 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5606 WLAN_FC_STYPE_DEAUTH);
5607 memcpy(mgmt.da, addr, ETH_ALEN);
5608 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5609 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5610 mgmt.u.deauth.reason_code = host_to_le16(reason);
5611 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5612 IEEE80211_HDRLEN +
5613 sizeof(mgmt.u.deauth));
5614}
5615
5616
5617static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5618 int reason)
5619{
5620 struct i802_bss *bss = priv;
5621 struct ieee80211_mgmt mgmt;
5622
5623 memset(&mgmt, 0, sizeof(mgmt));
5624 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5625 WLAN_FC_STYPE_DISASSOC);
5626 memcpy(mgmt.da, addr, ETH_ALEN);
5627 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5628 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5629 mgmt.u.disassoc.reason_code = host_to_le16(reason);
5630 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5631 IEEE80211_HDRLEN +
5632 sizeof(mgmt.u.disassoc));
5633}
5634
5635#endif /* HOSTAPD || CONFIG_AP */
5636
5637#ifdef HOSTAPD
5638
Jouni Malinen75ecf522011-06-27 15:19:46 -07005639static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5640{
5641 int i;
5642 int *old;
5643
5644 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5645 ifidx);
5646 for (i = 0; i < drv->num_if_indices; i++) {
5647 if (drv->if_indices[i] == 0) {
5648 drv->if_indices[i] = ifidx;
5649 return;
5650 }
5651 }
5652
5653 if (drv->if_indices != drv->default_if_indices)
5654 old = drv->if_indices;
5655 else
5656 old = NULL;
5657
5658 drv->if_indices = os_realloc(old,
5659 sizeof(int) * (drv->num_if_indices + 1));
5660 if (!drv->if_indices) {
5661 if (!old)
5662 drv->if_indices = drv->default_if_indices;
5663 else
5664 drv->if_indices = old;
5665 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5666 "interfaces");
5667 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5668 return;
5669 } else if (!old)
5670 os_memcpy(drv->if_indices, drv->default_if_indices,
5671 sizeof(drv->default_if_indices));
5672 drv->if_indices[drv->num_if_indices] = ifidx;
5673 drv->num_if_indices++;
5674}
5675
5676
5677static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5678{
5679 int i;
5680
5681 for (i = 0; i < drv->num_if_indices; i++) {
5682 if (drv->if_indices[i] == ifidx) {
5683 drv->if_indices[i] = 0;
5684 break;
5685 }
5686 }
5687}
5688
5689
5690static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5691{
5692 int i;
5693
5694 for (i = 0; i < drv->num_if_indices; i++)
5695 if (drv->if_indices[i] == ifidx)
5696 return 1;
5697
5698 return 0;
5699}
5700
5701
5702static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
5703 const char *bridge_ifname)
5704{
5705 struct i802_bss *bss = priv;
5706 struct wpa_driver_nl80211_data *drv = bss->drv;
5707 char name[IFNAMSIZ + 1];
5708
5709 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
5710 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5711 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5712 if (val) {
5713 if (!if_nametoindex(name)) {
5714 if (nl80211_create_iface(drv, name,
5715 NL80211_IFTYPE_AP_VLAN,
5716 NULL, 1) < 0)
5717 return -1;
5718 if (bridge_ifname &&
5719 linux_br_add_if(drv->ioctl_sock, bridge_ifname,
5720 name) < 0)
5721 return -1;
5722 }
5723 linux_set_iface_flags(drv->ioctl_sock, name, 1);
5724 return i802_set_sta_vlan(priv, addr, name, 0);
5725 } else {
5726 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
5727 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
5728 name);
5729 }
5730}
5731
5732
5733static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5734{
5735 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5736 struct sockaddr_ll lladdr;
5737 unsigned char buf[3000];
5738 int len;
5739 socklen_t fromlen = sizeof(lladdr);
5740
5741 len = recvfrom(sock, buf, sizeof(buf), 0,
5742 (struct sockaddr *)&lladdr, &fromlen);
5743 if (len < 0) {
5744 perror("recv");
5745 return;
5746 }
5747
5748 if (have_ifidx(drv, lladdr.sll_ifindex))
5749 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5750}
5751
5752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5754 struct i802_bss *bss,
5755 const char *brname, const char *ifname)
5756{
5757 int ifindex;
5758 char in_br[IFNAMSIZ];
5759
5760 os_strlcpy(bss->brname, brname, IFNAMSIZ);
5761 ifindex = if_nametoindex(brname);
5762 if (ifindex == 0) {
5763 /*
5764 * Bridge was configured, but the bridge device does
5765 * not exist. Try to add it now.
5766 */
5767 if (linux_br_add(drv->ioctl_sock, brname) < 0) {
5768 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5769 "bridge interface %s: %s",
5770 brname, strerror(errno));
5771 return -1;
5772 }
5773 bss->added_bridge = 1;
5774 add_ifidx(drv, if_nametoindex(brname));
5775 }
5776
5777 if (linux_br_get(in_br, ifname) == 0) {
5778 if (os_strcmp(in_br, brname) == 0)
5779 return 0; /* already in the bridge */
5780
5781 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5782 "bridge %s", ifname, in_br);
5783 if (linux_br_del_if(drv->ioctl_sock, in_br, ifname) < 0) {
5784 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5785 "remove interface %s from bridge "
5786 "%s: %s",
5787 ifname, brname, strerror(errno));
5788 return -1;
5789 }
5790 }
5791
5792 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5793 ifname, brname);
5794 if (linux_br_add_if(drv->ioctl_sock, brname, ifname) < 0) {
5795 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5796 "into bridge %s: %s",
5797 ifname, brname, strerror(errno));
5798 return -1;
5799 }
5800 bss->added_if_into_bridge = 1;
5801
5802 return 0;
5803}
5804
5805
5806static void *i802_init(struct hostapd_data *hapd,
5807 struct wpa_init_params *params)
5808{
5809 struct wpa_driver_nl80211_data *drv;
5810 struct i802_bss *bss;
5811 size_t i;
5812 char brname[IFNAMSIZ];
5813 int ifindex, br_ifindex;
5814 int br_added = 0;
5815
5816 bss = wpa_driver_nl80211_init(hapd, params->ifname, NULL);
5817 if (bss == NULL)
5818 return NULL;
5819
5820 drv = bss->drv;
5821 drv->nlmode = NL80211_IFTYPE_AP;
5822 if (linux_br_get(brname, params->ifname) == 0) {
5823 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5824 params->ifname, brname);
5825 br_ifindex = if_nametoindex(brname);
5826 } else {
5827 brname[0] = '\0';
5828 br_ifindex = 0;
5829 }
5830
5831 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
5832 drv->if_indices = drv->default_if_indices;
5833 for (i = 0; i < params->num_bridge; i++) {
5834 if (params->bridge[i]) {
5835 ifindex = if_nametoindex(params->bridge[i]);
5836 if (ifindex)
5837 add_ifidx(drv, ifindex);
5838 if (ifindex == br_ifindex)
5839 br_added = 1;
5840 }
5841 }
5842 if (!br_added && br_ifindex &&
5843 (params->num_bridge == 0 || !params->bridge[0]))
5844 add_ifidx(drv, br_ifindex);
5845
5846 /* start listening for EAPOL on the default AP interface */
5847 add_ifidx(drv, drv->ifindex);
5848
5849 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0))
5850 goto failed;
5851
5852 if (params->bssid) {
5853 if (linux_set_ifhwaddr(drv->ioctl_sock, bss->ifname,
5854 params->bssid))
5855 goto failed;
5856 }
5857
5858 if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_AP)) {
5859 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
5860 "into AP mode", bss->ifname);
5861 goto failed;
5862 }
5863
5864 if (params->num_bridge && params->bridge[0] &&
5865 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
5866 goto failed;
5867
5868 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1))
5869 goto failed;
5870
5871 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5872 if (drv->eapol_sock < 0) {
5873 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
5874 goto failed;
5875 }
5876
5877 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5878 {
5879 printf("Could not register read socket for eapol\n");
5880 goto failed;
5881 }
5882
5883 if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, params->own_addr))
5884 goto failed;
5885
5886 return bss;
5887
5888failed:
5889 nl80211_remove_monitor_interface(drv);
5890 rfkill_deinit(drv->rfkill);
5891 netlink_deinit(drv->netlink);
5892 if (drv->ioctl_sock >= 0)
5893 close(drv->ioctl_sock);
5894
5895 genl_family_put(drv->nl80211);
5896 nl_cache_free(drv->nl_cache);
5897 nl80211_handle_destroy(drv->nl_handle);
5898 nl_cb_put(drv->nl_cb);
5899 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
5900
5901 os_free(drv);
5902 return NULL;
5903}
5904
5905
5906static void i802_deinit(void *priv)
5907{
5908 wpa_driver_nl80211_deinit(priv);
5909}
5910
5911#endif /* HOSTAPD */
5912
5913
5914static enum nl80211_iftype wpa_driver_nl80211_if_type(
5915 enum wpa_driver_if_type type)
5916{
5917 switch (type) {
5918 case WPA_IF_STATION:
5919 return NL80211_IFTYPE_STATION;
5920 case WPA_IF_P2P_CLIENT:
5921 case WPA_IF_P2P_GROUP:
5922 return NL80211_IFTYPE_P2P_CLIENT;
5923 case WPA_IF_AP_VLAN:
5924 return NL80211_IFTYPE_AP_VLAN;
5925 case WPA_IF_AP_BSS:
5926 return NL80211_IFTYPE_AP;
5927 case WPA_IF_P2P_GO:
5928 return NL80211_IFTYPE_P2P_GO;
5929 }
5930 return -1;
5931}
5932
5933
5934#ifdef CONFIG_P2P
5935
5936static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5937{
5938 struct wpa_driver_nl80211_data *drv;
5939 dl_list_for_each(drv, &global->interfaces,
5940 struct wpa_driver_nl80211_data, list) {
5941 if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0)
5942 return 1;
5943 }
5944 return 0;
5945}
5946
5947
5948static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
5949 u8 *new_addr)
5950{
5951 unsigned int idx;
5952
5953 if (!drv->global)
5954 return -1;
5955
5956 os_memcpy(new_addr, drv->addr, ETH_ALEN);
5957 for (idx = 0; idx < 64; idx++) {
5958 new_addr[0] = drv->addr[0] | 0x02;
5959 new_addr[0] ^= idx << 2;
5960 if (!nl80211_addr_in_use(drv->global, new_addr))
5961 break;
5962 }
5963 if (idx == 64)
5964 return -1;
5965
5966 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
5967 MACSTR, MAC2STR(new_addr));
5968
5969 return 0;
5970}
5971
5972#endif /* CONFIG_P2P */
5973
5974
5975static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5976 const char *ifname, const u8 *addr,
5977 void *bss_ctx, void **drv_priv,
5978 char *force_ifname, u8 *if_addr,
5979 const char *bridge)
5980{
5981 struct i802_bss *bss = priv;
5982 struct wpa_driver_nl80211_data *drv = bss->drv;
5983 int ifidx;
5984#ifdef HOSTAPD
5985 struct i802_bss *new_bss = NULL;
5986
5987 if (type == WPA_IF_AP_BSS) {
5988 new_bss = os_zalloc(sizeof(*new_bss));
5989 if (new_bss == NULL)
5990 return -1;
5991 }
5992#endif /* HOSTAPD */
5993
5994 if (addr)
5995 os_memcpy(if_addr, addr, ETH_ALEN);
5996 ifidx = nl80211_create_iface(drv, ifname,
5997 wpa_driver_nl80211_if_type(type), addr,
5998 0);
5999 if (ifidx < 0) {
6000#ifdef HOSTAPD
6001 os_free(new_bss);
6002#endif /* HOSTAPD */
6003 return -1;
6004 }
6005
6006 if (!addr &&
6007 linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, if_addr) < 0) {
6008 nl80211_remove_iface(drv, ifidx);
6009 return -1;
6010 }
6011
6012#ifdef CONFIG_P2P
6013 if (!addr &&
6014 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
6015 type == WPA_IF_P2P_GO)) {
6016 /* Enforce unique P2P Interface Address */
6017 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
6018
6019 if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr)
6020 < 0 ||
6021 linux_get_ifhwaddr(drv->ioctl_sock, ifname, new_addr) < 0)
6022 {
6023 nl80211_remove_iface(drv, ifidx);
6024 return -1;
6025 }
6026 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
6027 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
6028 "for P2P group interface");
6029 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
6030 nl80211_remove_iface(drv, ifidx);
6031 return -1;
6032 }
6033 if (linux_set_ifhwaddr(drv->ioctl_sock, ifname,
6034 new_addr) < 0) {
6035 nl80211_remove_iface(drv, ifidx);
6036 return -1;
6037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006039 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 }
6041#endif /* CONFIG_P2P */
6042
6043#ifdef HOSTAPD
6044 if (bridge &&
6045 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6046 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6047 "interface %s to a bridge %s", ifname, bridge);
6048 nl80211_remove_iface(drv, ifidx);
6049 os_free(new_bss);
6050 return -1;
6051 }
6052
6053 if (type == WPA_IF_AP_BSS) {
6054 if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
6055 nl80211_remove_iface(drv, ifidx);
6056 os_free(new_bss);
6057 return -1;
6058 }
6059 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
6060 new_bss->ifindex = ifidx;
6061 new_bss->drv = drv;
6062 new_bss->next = drv->first_bss.next;
6063 drv->first_bss.next = new_bss;
6064 if (drv_priv)
6065 *drv_priv = new_bss;
6066 }
6067#endif /* HOSTAPD */
6068
6069 return 0;
6070}
6071
6072
6073static int wpa_driver_nl80211_if_remove(void *priv,
6074 enum wpa_driver_if_type type,
6075 const char *ifname)
6076{
6077 struct i802_bss *bss = priv;
6078 struct wpa_driver_nl80211_data *drv = bss->drv;
6079 int ifindex = if_nametoindex(ifname);
6080
6081 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
6082 __func__, type, ifname, ifindex);
6083 if (ifindex <= 0)
6084 return -1;
6085
6086#ifdef HOSTAPD
6087 if (bss->added_if_into_bridge) {
6088 if (linux_br_del_if(drv->ioctl_sock, bss->brname, bss->ifname)
6089 < 0)
6090 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6091 "interface %s from bridge %s: %s",
6092 bss->ifname, bss->brname, strerror(errno));
6093 }
6094 if (bss->added_bridge) {
6095 if (linux_br_del(drv->ioctl_sock, bss->brname) < 0)
6096 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6097 "bridge %s: %s",
6098 bss->brname, strerror(errno));
6099 }
6100#endif /* HOSTAPD */
6101
6102 nl80211_remove_iface(drv, ifindex);
6103
6104#ifdef HOSTAPD
6105 if (type != WPA_IF_AP_BSS)
6106 return 0;
6107
6108 if (bss != &drv->first_bss) {
6109 struct i802_bss *tbss;
6110
6111 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
6112 if (tbss->next == bss) {
6113 tbss->next = bss->next;
6114 os_free(bss);
6115 bss = NULL;
6116 break;
6117 }
6118 }
6119 if (bss)
6120 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6121 "BSS %p in the list", __func__, bss);
6122 }
6123#endif /* HOSTAPD */
6124
6125 return 0;
6126}
6127
6128
6129static int cookie_handler(struct nl_msg *msg, void *arg)
6130{
6131 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6132 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6133 u64 *cookie = arg;
6134 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6135 genlmsg_attrlen(gnlh, 0), NULL);
6136 if (tb[NL80211_ATTR_COOKIE])
6137 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6138 return NL_SKIP;
6139}
6140
6141
6142static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
6143 unsigned int freq, unsigned int wait,
6144 const u8 *buf, size_t buf_len,
6145 u64 *cookie_out)
6146{
6147 struct nl_msg *msg;
6148 u64 cookie;
6149 int ret = -1;
6150
6151 msg = nlmsg_alloc();
6152 if (!msg)
6153 return -1;
6154
6155 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6156 NL80211_CMD_FRAME, 0);
6157
6158 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6159 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07006160#ifndef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006161 if (wait)
6162 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
6163#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
6165 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
6166
6167 cookie = 0;
6168 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6169 msg = NULL;
6170 if (ret) {
6171 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006172 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6173 freq, wait);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174 goto nla_put_failure;
6175 }
6176 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
6177 "cookie 0x%llx", (long long unsigned int) cookie);
6178
6179 if (cookie_out)
6180 *cookie_out = cookie;
6181
6182nla_put_failure:
6183 nlmsg_free(msg);
6184 return ret;
6185}
6186
6187
6188static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
6189 unsigned int wait_time,
6190 const u8 *dst, const u8 *src,
6191 const u8 *bssid,
6192 const u8 *data, size_t data_len)
6193{
6194 struct i802_bss *bss = priv;
6195 struct wpa_driver_nl80211_data *drv = bss->drv;
6196 int ret = -1;
6197 u8 *buf;
6198 struct ieee80211_hdr *hdr;
6199
6200 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
6201 "wait=%d ms)", drv->ifindex, wait_time);
6202
6203 buf = os_zalloc(24 + data_len);
6204 if (buf == NULL)
6205 return ret;
6206 os_memcpy(buf + 24, data, data_len);
6207 hdr = (struct ieee80211_hdr *) buf;
6208 hdr->frame_control =
6209 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6210 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6211 os_memcpy(hdr->addr2, src, ETH_ALEN);
6212 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6213
6214 if (drv->nlmode == NL80211_IFTYPE_AP)
6215 ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
6216 else
6217 ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
6218 24 + data_len,
6219 &drv->send_action_cookie);
6220
6221 os_free(buf);
6222 return ret;
6223}
6224
6225
6226static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6227{
6228 struct i802_bss *bss = priv;
6229 struct wpa_driver_nl80211_data *drv = bss->drv;
6230 struct nl_msg *msg;
6231 int ret;
6232
6233 msg = nlmsg_alloc();
6234 if (!msg)
6235 return;
6236
6237 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6238 NL80211_CMD_FRAME_WAIT_CANCEL, 0);
6239
6240 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6241 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
6242
6243 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6244 msg = NULL;
6245 if (ret)
6246 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6247 "(%s)", ret, strerror(-ret));
6248
6249 nla_put_failure:
6250 nlmsg_free(msg);
6251}
6252
6253
6254static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6255 unsigned int duration)
6256{
6257 struct i802_bss *bss = priv;
6258 struct wpa_driver_nl80211_data *drv = bss->drv;
6259 struct nl_msg *msg;
6260 int ret;
6261 u64 cookie;
6262
6263 msg = nlmsg_alloc();
6264 if (!msg)
6265 return -1;
6266
6267 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6268 NL80211_CMD_REMAIN_ON_CHANNEL, 0);
6269
6270 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6271 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6272 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6273
6274 cookie = 0;
6275 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6276 if (ret == 0) {
6277 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6278 "0x%llx for freq=%u MHz duration=%u",
6279 (long long unsigned int) cookie, freq, duration);
6280 drv->remain_on_chan_cookie = cookie;
6281 return 0;
6282 }
6283 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6284 "(freq=%d duration=%u): %d (%s)",
6285 freq, duration, ret, strerror(-ret));
6286nla_put_failure:
6287 return -1;
6288}
6289
6290
6291static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6292{
6293 struct i802_bss *bss = priv;
6294 struct wpa_driver_nl80211_data *drv = bss->drv;
6295 struct nl_msg *msg;
6296 int ret;
6297
6298 if (!drv->pending_remain_on_chan) {
6299 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6300 "to cancel");
6301 return -1;
6302 }
6303
6304 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6305 "0x%llx",
6306 (long long unsigned int) drv->remain_on_chan_cookie);
6307
6308 msg = nlmsg_alloc();
6309 if (!msg)
6310 return -1;
6311
6312 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6313 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
6314
6315 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6316 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
6317
6318 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6319 if (ret == 0)
6320 return 0;
6321 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6322 "%d (%s)", ret, strerror(-ret));
6323nla_put_failure:
6324 return -1;
6325}
6326
6327
6328static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
6329{
6330 struct i802_bss *bss = priv;
6331 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333 if (!report) {
6334 if (drv->nl_handle_preq) {
6335 eloop_unregister_read_sock(
6336 nl_socket_get_fd(drv->nl_handle_preq));
6337 nl_cache_free(drv->nl_cache_preq);
6338 nl80211_handle_destroy(drv->nl_handle_preq);
6339 drv->nl_handle_preq = NULL;
6340 }
6341 return 0;
6342 }
6343
6344 if (drv->nl_handle_preq) {
6345 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
6346 "already on!");
6347 return 0;
6348 }
6349
6350 drv->nl_handle_preq = nl80211_handle_alloc(drv->nl_cb);
6351 if (drv->nl_handle_preq == NULL) {
6352 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate "
6353 "netlink callbacks (preq)");
6354 goto out_err1;
6355 }
6356
6357 if (genl_connect(drv->nl_handle_preq)) {
6358 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to "
6359 "generic netlink (preq)");
6360 goto out_err2;
6361 return -1;
6362 }
6363
6364#ifdef CONFIG_LIBNL20
6365 if (genl_ctrl_alloc_cache(drv->nl_handle_preq,
6366 &drv->nl_cache_preq) < 0) {
6367 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
6368 "netlink cache (preq)");
6369 goto out_err2;
6370 }
6371#else /* CONFIG_LIBNL20 */
6372 drv->nl_cache_preq = genl_ctrl_alloc_cache(drv->nl_handle_preq);
6373 if (drv->nl_cache_preq == NULL) {
6374 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
6375 "netlink cache (preq)");
6376 goto out_err2;
6377 }
6378#endif /* CONFIG_LIBNL20 */
6379
6380 if (nl80211_register_frame(drv, drv->nl_handle_preq,
6381 (WLAN_FC_TYPE_MGMT << 2) |
6382 (WLAN_FC_STYPE_PROBE_REQ << 4),
6383 NULL, 0) < 0) {
6384 goto out_err3;
6385 }
6386
Dmitry Shmidt44da0252011-08-23 12:30:30 -07006387#ifdef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07006388 if (drv->nlmode != NL80211_IFTYPE_AP &&
6389 drv->nlmode != NL80211_IFTYPE_P2P_GO) {
6390 wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
6391 "allowed in AP or P2P GO mode (iftype=%d)",
6392 drv->nlmode);
6393 goto done;
6394 }
6395
6396 if (nl80211_register_frame(drv, drv->nl_handle_preq,
6397 (WLAN_FC_TYPE_MGMT << 2) |
6398 (WLAN_FC_STYPE_ASSOC_REQ << 4),
6399 NULL, 0) < 0) {
6400 goto out_err3;
6401 }
6402
6403 if (nl80211_register_frame(drv, drv->nl_handle_preq,
6404 (WLAN_FC_TYPE_MGMT << 2) |
6405 (WLAN_FC_STYPE_REASSOC_REQ << 4),
6406 NULL, 0) < 0) {
6407 goto out_err3;
6408 }
6409
6410 if (nl80211_register_frame(drv, drv->nl_handle_preq,
6411 (WLAN_FC_TYPE_MGMT << 2) |
6412 (WLAN_FC_STYPE_DISASSOC << 4),
6413 NULL, 0) < 0) {
6414 goto out_err3;
6415 }
6416
6417 if (nl80211_register_frame(drv, drv->nl_handle_preq,
6418 (WLAN_FC_TYPE_MGMT << 2) |
6419 (WLAN_FC_STYPE_DEAUTH << 4),
6420 NULL, 0) < 0) {
6421 goto out_err3;
6422 }
6423
6424done:
6425#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006426 eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_preq),
6427 wpa_driver_nl80211_event_receive, drv,
6428 drv->nl_handle_preq);
6429
6430 return 0;
6431
6432 out_err3:
6433 nl_cache_free(drv->nl_cache_preq);
6434 out_err2:
6435 nl80211_handle_destroy(drv->nl_handle_preq);
6436 drv->nl_handle_preq = NULL;
6437 out_err1:
6438 return -1;
6439}
6440
6441
6442static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6443 int ifindex, int disabled)
6444{
6445 struct nl_msg *msg;
6446 struct nlattr *bands, *band;
6447 int ret;
6448
6449 msg = nlmsg_alloc();
6450 if (!msg)
6451 return -1;
6452
6453 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6454 NL80211_CMD_SET_TX_BITRATE_MASK, 0);
6455 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6456
6457 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6458 if (!bands)
6459 goto nla_put_failure;
6460
6461 /*
6462 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6463 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6464 * rates. All 5 GHz rates are left enabled.
6465 */
6466 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
6467 if (!band)
6468 goto nla_put_failure;
6469 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
6470 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
6471 nla_nest_end(msg, band);
6472
6473 nla_nest_end(msg, bands);
6474
6475 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6476 msg = NULL;
6477 if (ret) {
6478 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6479 "(%s)", ret, strerror(-ret));
6480 }
6481
6482 return ret;
6483
6484nla_put_failure:
6485 nlmsg_free(msg);
6486 return -1;
6487}
6488
6489
6490static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled)
6491{
6492 struct i802_bss *bss = priv;
6493 struct wpa_driver_nl80211_data *drv = bss->drv;
6494 drv->disable_11b_rates = disabled;
6495 return nl80211_disable_11b_rates(drv, drv->ifindex, disabled);
6496}
6497
6498
6499static int wpa_driver_nl80211_deinit_ap(void *priv)
6500{
6501 struct i802_bss *bss = priv;
6502 struct wpa_driver_nl80211_data *drv = bss->drv;
6503 if (drv->nlmode != NL80211_IFTYPE_AP)
6504 return -1;
6505 wpa_driver_nl80211_del_beacon(drv);
6506 return wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA);
6507}
6508
6509
6510static void wpa_driver_nl80211_resume(void *priv)
6511{
6512 struct i802_bss *bss = priv;
6513 struct wpa_driver_nl80211_data *drv = bss->drv;
6514 if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
6515 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
6516 "resume event");
6517 }
6518}
6519
6520
6521static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
6522 const u8 *ies, size_t ies_len)
6523{
6524 struct i802_bss *bss = priv;
6525 struct wpa_driver_nl80211_data *drv = bss->drv;
6526 int ret;
6527 u8 *data, *pos;
6528 size_t data_len;
6529 u8 own_addr[ETH_ALEN];
6530
6531 if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr) < 0)
6532 return -1;
6533
6534 if (action != 1) {
6535 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
6536 "action %d", action);
6537 return -1;
6538 }
6539
6540 /*
6541 * Action frame payload:
6542 * Category[1] = 6 (Fast BSS Transition)
6543 * Action[1] = 1 (Fast BSS Transition Request)
6544 * STA Address
6545 * Target AP Address
6546 * FT IEs
6547 */
6548
6549 data_len = 2 + 2 * ETH_ALEN + ies_len;
6550 data = os_malloc(data_len);
6551 if (data == NULL)
6552 return -1;
6553 pos = data;
6554 *pos++ = 0x06; /* FT Action category */
6555 *pos++ = action;
6556 os_memcpy(pos, own_addr, ETH_ALEN);
6557 pos += ETH_ALEN;
6558 os_memcpy(pos, target_ap, ETH_ALEN);
6559 pos += ETH_ALEN;
6560 os_memcpy(pos, ies, ies_len);
6561
6562 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
6563 drv->bssid, own_addr, drv->bssid,
6564 data, data_len);
6565 os_free(data);
6566
6567 return ret;
6568}
6569
6570
6571static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6572{
6573 struct i802_bss *bss = priv;
6574 struct wpa_driver_nl80211_data *drv = bss->drv;
6575 struct nl_msg *msg, *cqm = NULL;
6576
6577 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6578 "hysteresis=%d", threshold, hysteresis);
6579
6580 msg = nlmsg_alloc();
6581 if (!msg)
6582 return -1;
6583
6584 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
6585 0, NL80211_CMD_SET_CQM, 0);
6586
6587 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
6588
6589 cqm = nlmsg_alloc();
6590 if (cqm == NULL)
6591 return -1;
6592
6593 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
6594 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
6595 nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
6596
6597 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6598 return 0;
6599 msg = NULL;
6600
6601nla_put_failure:
6602 if (cqm)
6603 nlmsg_free(cqm);
6604 nlmsg_free(msg);
6605 return -1;
6606}
6607
6608
6609static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6610{
6611 struct i802_bss *bss = priv;
6612 struct wpa_driver_nl80211_data *drv = bss->drv;
6613 int res;
6614
6615 os_memset(si, 0, sizeof(*si));
6616 res = nl80211_get_link_signal(drv, si);
6617 if (res != 0)
6618 return res;
6619
6620 return nl80211_get_link_noise(drv, si);
6621}
6622
6623
6624static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6625 int encrypt)
6626{
6627 struct i802_bss *bss = priv;
6628 struct wpa_driver_nl80211_data *drv = bss->drv;
6629 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
6630}
6631
6632
6633static int nl80211_set_intra_bss(void *priv, int enabled)
6634{
6635 struct i802_bss *bss = priv;
6636 struct wpa_driver_nl80211_data *drv = bss->drv;
6637 struct nl_msg *msg;
6638
6639 msg = nlmsg_alloc();
6640 if (!msg)
6641 return -ENOMEM;
6642
6643 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
6644 NL80211_CMD_SET_BSS, 0);
6645
6646 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6647 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, !enabled);
6648
6649 return send_and_recv_msgs(drv, msg, NULL, NULL);
6650 nla_put_failure:
6651 return -ENOBUFS;
6652}
6653
6654
6655static int nl80211_set_param(void *priv, const char *param)
6656{
6657 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6658 if (param == NULL)
6659 return 0;
6660
6661#ifdef CONFIG_P2P
6662 if (os_strstr(param, "use_p2p_group_interface=1")) {
6663 struct i802_bss *bss = priv;
6664 struct wpa_driver_nl80211_data *drv = bss->drv;
6665
6666 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6667 "interface");
6668 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6669 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6670 }
6671#endif /* CONFIG_P2P */
6672
6673 return 0;
6674}
6675
6676
6677static void * nl80211_global_init(void)
6678{
6679 struct nl80211_global *global;
6680 global = os_zalloc(sizeof(*global));
6681 if (global == NULL)
6682 return NULL;
6683 dl_list_init(&global->interfaces);
6684 return global;
6685}
6686
6687
6688static void nl80211_global_deinit(void *priv)
6689{
6690 struct nl80211_global *global = priv;
6691 if (global == NULL)
6692 return;
6693 if (!dl_list_empty(&global->interfaces)) {
6694 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6695 "nl80211_global_deinit",
6696 dl_list_len(&global->interfaces));
6697 }
6698 os_free(global);
6699}
6700
6701
6702static const char * nl80211_get_radio_name(void *priv)
6703{
6704 struct i802_bss *bss = priv;
6705 struct wpa_driver_nl80211_data *drv = bss->drv;
6706 return drv->phyname;
6707}
6708
6709
Jouni Malinen75ecf522011-06-27 15:19:46 -07006710static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6711 const u8 *pmkid)
6712{
6713 struct nl_msg *msg;
6714
6715 msg = nlmsg_alloc();
6716 if (!msg)
6717 return -ENOMEM;
6718
6719 genlmsg_put(msg, 0, 0, genl_family_get_id(bss->drv->nl80211), 0, 0,
6720 cmd, 0);
6721
6722 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6723 if (pmkid)
6724 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
6725 if (bssid)
6726 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6727
6728 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
6729 nla_put_failure:
6730 return -ENOBUFS;
6731}
6732
6733
6734static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6735{
6736 struct i802_bss *bss = priv;
6737 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6738 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6739}
6740
6741
6742static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6743{
6744 struct i802_bss *bss = priv;
6745 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6746 MAC2STR(bssid));
6747 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6748}
6749
6750
6751static int nl80211_flush_pmkid(void *priv)
6752{
6753 struct i802_bss *bss = priv;
6754 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6755 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6756}
6757
6758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759const struct wpa_driver_ops wpa_driver_nl80211_ops = {
6760 .name = "nl80211",
6761 .desc = "Linux nl80211/cfg80211",
6762 .get_bssid = wpa_driver_nl80211_get_bssid,
6763 .get_ssid = wpa_driver_nl80211_get_ssid,
6764 .set_key = wpa_driver_nl80211_set_key,
6765 .scan2 = wpa_driver_nl80211_scan,
6766 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
6767 .deauthenticate = wpa_driver_nl80211_deauthenticate,
6768 .disassociate = wpa_driver_nl80211_disassociate,
6769 .authenticate = wpa_driver_nl80211_authenticate,
6770 .associate = wpa_driver_nl80211_associate,
6771 .global_init = nl80211_global_init,
6772 .global_deinit = nl80211_global_deinit,
6773 .init2 = wpa_driver_nl80211_init,
6774 .deinit = wpa_driver_nl80211_deinit,
6775 .get_capa = wpa_driver_nl80211_get_capa,
6776 .set_operstate = wpa_driver_nl80211_set_operstate,
6777 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6778 .set_country = wpa_driver_nl80211_set_country,
6779 .set_beacon = wpa_driver_nl80211_set_beacon,
6780 .if_add = wpa_driver_nl80211_if_add,
6781 .if_remove = wpa_driver_nl80211_if_remove,
6782 .send_mlme = wpa_driver_nl80211_send_mlme,
6783 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
6784 .sta_add = wpa_driver_nl80211_sta_add,
6785 .sta_remove = wpa_driver_nl80211_sta_remove,
6786 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
6787 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
6788#ifdef HOSTAPD
6789 .hapd_init = i802_init,
6790 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07006791 .set_wds_sta = i802_set_wds_sta,
6792#endif /* HOSTAPD */
6793#if defined(HOSTAPD) || defined(CONFIG_AP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794 .get_seqnum = i802_get_seqnum,
6795 .flush = i802_flush,
6796 .read_sta_data = i802_read_sta_data,
6797 .get_inact_sec = i802_get_inact_sec,
6798 .sta_clear_stats = i802_sta_clear_stats,
6799 .set_rts = i802_set_rts,
6800 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801 .set_cts_protect = i802_set_cts_protect,
6802 .set_preamble = i802_set_preamble,
6803 .set_short_slot_time = i802_set_short_slot_time,
6804 .set_tx_queue_params = i802_set_tx_queue_params,
6805 .set_sta_vlan = i802_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006806 .set_ht_params = i802_set_ht_params,
Jouni Malinen75ecf522011-06-27 15:19:46 -07006807 .set_rate_sets = i802_set_rate_sets,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006808 .sta_deauth = i802_sta_deauth,
6809 .sta_disassoc = i802_sta_disassoc,
6810#endif /* HOSTAPD || CONFIG_AP */
6811 .set_freq = i802_set_freq,
6812 .send_action = wpa_driver_nl80211_send_action,
6813 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
6814 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
6815 .cancel_remain_on_channel =
6816 wpa_driver_nl80211_cancel_remain_on_channel,
6817 .probe_req_report = wpa_driver_nl80211_probe_req_report,
6818 .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates,
6819 .deinit_ap = wpa_driver_nl80211_deinit_ap,
6820 .resume = wpa_driver_nl80211_resume,
6821 .send_ft_action = nl80211_send_ft_action,
6822 .signal_monitor = nl80211_signal_monitor,
6823 .signal_poll = nl80211_signal_poll,
6824 .send_frame = nl80211_send_frame,
6825 .set_intra_bss = nl80211_set_intra_bss,
6826 .set_param = nl80211_set_param,
6827 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07006828 .add_pmkid = nl80211_add_pmkid,
6829 .remove_pmkid = nl80211_remove_pmkid,
6830 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006831#ifdef ANDROID_BRCM_P2P_PATCH
Dmitry Shmidtfc41cad2011-09-28 13:29:53 -07006832 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006833 .set_noa = wpa_driver_set_p2p_noa,
6834 .set_p2p_powersave = wpa_driver_set_p2p_ps,
6835 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
6836#endif
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07006837#ifdef ANDROID
6838 .driver_cmd = wpa_driver_nl80211_driver_cmd,
6839#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840};