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