]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/drivers/driver_nl80211.c
nl80211: Handle creation of P2P Device interface
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
1 /*
2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2012, 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 software may be distributed under the terms of the BSD license.
10 * See README for more details.
11 */
12
13 #include "includes.h"
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <net/if.h>
19 #include <netlink/genl/genl.h>
20 #include <netlink/genl/family.h>
21 #include <netlink/genl/ctrl.h>
22 #include <linux/rtnetlink.h>
23 #include <netpacket/packet.h>
24 #include <linux/filter.h>
25 #include <linux/errqueue.h>
26 #include "nl80211_copy.h"
27
28 #include "common.h"
29 #include "eloop.h"
30 #include "utils/list.h"
31 #include "common/ieee802_11_defs.h"
32 #include "common/ieee802_11_common.h"
33 #include "l2_packet/l2_packet.h"
34 #include "netlink.h"
35 #include "linux_ioctl.h"
36 #include "radiotap.h"
37 #include "radiotap_iter.h"
38 #include "rfkill.h"
39 #include "driver.h"
40
41 #ifndef SO_WIFI_STATUS
42 # if defined(__sparc__)
43 # define SO_WIFI_STATUS 0x0025
44 # elif defined(__parisc__)
45 # define SO_WIFI_STATUS 0x4022
46 # else
47 # define SO_WIFI_STATUS 41
48 # endif
49
50 # define SCM_WIFI_STATUS SO_WIFI_STATUS
51 #endif
52
53 #ifndef SO_EE_ORIGIN_TXSTATUS
54 #define SO_EE_ORIGIN_TXSTATUS 4
55 #endif
56
57 #ifndef PACKET_TX_TIMESTAMP
58 #define PACKET_TX_TIMESTAMP 16
59 #endif
60
61 #ifdef ANDROID
62 #include "android_drv.h"
63 #endif /* ANDROID */
64 #ifdef CONFIG_LIBNL20
65 /* libnl 2.0 compatibility code */
66 #define nl_handle nl_sock
67 #define nl80211_handle_alloc nl_socket_alloc_cb
68 #define nl80211_handle_destroy nl_socket_free
69 #else
70 /*
71 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72 * but when you free a socket again it will mess up its bitmap and
73 * and use the wrong number the next time it needs a socket ID.
74 * Therefore, we wrap the handle alloc/destroy and add our own pid
75 * accounting.
76 */
77 static uint32_t port_bitmap[32] = { 0 };
78
79 static struct nl_handle *nl80211_handle_alloc(void *cb)
80 {
81 struct nl_handle *handle;
82 uint32_t pid = getpid() & 0x3FFFFF;
83 int i;
84
85 handle = nl_handle_alloc_cb(cb);
86
87 for (i = 0; i < 1024; i++) {
88 if (port_bitmap[i / 32] & (1 << (i % 32)))
89 continue;
90 port_bitmap[i / 32] |= 1 << (i % 32);
91 pid += i << 22;
92 break;
93 }
94
95 nl_socket_set_local_port(handle, pid);
96
97 return handle;
98 }
99
100 static void nl80211_handle_destroy(struct nl_handle *handle)
101 {
102 uint32_t port = nl_socket_get_local_port(handle);
103
104 port >>= 22;
105 port_bitmap[port / 32] &= ~(1 << (port % 32));
106
107 nl_handle_destroy(handle);
108 }
109 #endif /* CONFIG_LIBNL20 */
110
111
112 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
113 {
114 struct nl_handle *handle;
115
116 handle = nl80211_handle_alloc(cb);
117 if (handle == NULL) {
118 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119 "callbacks (%s)", dbg);
120 return NULL;
121 }
122
123 if (genl_connect(handle)) {
124 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125 "netlink (%s)", dbg);
126 nl80211_handle_destroy(handle);
127 return NULL;
128 }
129
130 return handle;
131 }
132
133
134 static void nl_destroy_handles(struct nl_handle **handle)
135 {
136 if (*handle == NULL)
137 return;
138 nl80211_handle_destroy(*handle);
139 *handle = NULL;
140 }
141
142
143 #ifndef IFF_LOWER_UP
144 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
145 #endif
146 #ifndef IFF_DORMANT
147 #define IFF_DORMANT 0x20000 /* driver signals dormant */
148 #endif
149
150 #ifndef IF_OPER_DORMANT
151 #define IF_OPER_DORMANT 5
152 #endif
153 #ifndef IF_OPER_UP
154 #define IF_OPER_UP 6
155 #endif
156
157 struct nl80211_global {
158 struct dl_list interfaces;
159 int if_add_ifindex;
160 struct netlink_data *netlink;
161 struct nl_cb *nl_cb;
162 struct nl_handle *nl;
163 int nl80211_id;
164 int ioctl_sock; /* socket for ioctl() use */
165
166 struct nl_handle *nl_event;
167 };
168
169 struct nl80211_wiphy_data {
170 struct dl_list list;
171 struct dl_list bsss;
172 struct dl_list drvs;
173
174 struct nl_handle *nl_beacons;
175 struct nl_cb *nl_cb;
176
177 int wiphy_idx;
178 };
179
180 static void nl80211_global_deinit(void *priv);
181
182 struct i802_bss {
183 struct wpa_driver_nl80211_data *drv;
184 struct i802_bss *next;
185 int ifindex;
186 u64 wdev_id;
187 char ifname[IFNAMSIZ + 1];
188 char brname[IFNAMSIZ];
189 unsigned int beacon_set:1;
190 unsigned int added_if_into_bridge:1;
191 unsigned int added_bridge:1;
192 unsigned int in_deinit:1;
193 unsigned int wdev_id_set:1;
194
195 u8 addr[ETH_ALEN];
196
197 int freq;
198
199 void *ctx;
200 struct nl_handle *nl_preq, *nl_mgmt;
201 struct nl_cb *nl_cb;
202
203 struct nl80211_wiphy_data *wiphy_data;
204 struct dl_list wiphy_list;
205 };
206
207 struct wpa_driver_nl80211_data {
208 struct nl80211_global *global;
209 struct dl_list list;
210 struct dl_list wiphy_list;
211 char phyname[32];
212 void *ctx;
213 int ifindex;
214 int if_removed;
215 int if_disabled;
216 int ignore_if_down_event;
217 struct rfkill_data *rfkill;
218 struct wpa_driver_capa capa;
219 u8 *extended_capa, *extended_capa_mask;
220 unsigned int extended_capa_len;
221 int has_capability;
222
223 int operstate;
224
225 int scan_complete_events;
226
227 struct nl_cb *nl_cb;
228
229 u8 auth_bssid[ETH_ALEN];
230 u8 auth_attempt_bssid[ETH_ALEN];
231 u8 bssid[ETH_ALEN];
232 u8 prev_bssid[ETH_ALEN];
233 int associated;
234 u8 ssid[32];
235 size_t ssid_len;
236 enum nl80211_iftype nlmode;
237 enum nl80211_iftype ap_scan_as_station;
238 unsigned int assoc_freq;
239
240 int monitor_sock;
241 int monitor_ifidx;
242 int monitor_refcount;
243
244 unsigned int disabled_11b_rates:1;
245 unsigned int pending_remain_on_chan:1;
246 unsigned int in_interface_list:1;
247 unsigned int device_ap_sme:1;
248 unsigned int poll_command_supported:1;
249 unsigned int data_tx_status:1;
250 unsigned int scan_for_auth:1;
251 unsigned int retry_auth:1;
252 unsigned int use_monitor:1;
253 unsigned int ignore_next_local_disconnect:1;
254
255 u64 remain_on_chan_cookie;
256 u64 send_action_cookie;
257
258 unsigned int last_mgmt_freq;
259
260 struct wpa_driver_scan_filter *filter_ssids;
261 size_t num_filter_ssids;
262
263 struct i802_bss first_bss;
264
265 int eapol_tx_sock;
266
267 #ifdef HOSTAPD
268 int eapol_sock; /* socket for EAPOL frames */
269
270 int default_if_indices[16];
271 int *if_indices;
272 int num_if_indices;
273
274 int last_freq;
275 int last_freq_ht;
276 #endif /* HOSTAPD */
277
278 /* From failed authentication command */
279 int auth_freq;
280 u8 auth_bssid_[ETH_ALEN];
281 u8 auth_ssid[32];
282 size_t auth_ssid_len;
283 int auth_alg;
284 u8 *auth_ie;
285 size_t auth_ie_len;
286 u8 auth_wep_key[4][16];
287 size_t auth_wep_key_len[4];
288 int auth_wep_tx_keyidx;
289 int auth_local_state_change;
290 int auth_p2p;
291 };
292
293
294 static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
295 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
296 void *timeout_ctx);
297 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
298 enum nl80211_iftype nlmode);
299 static int
300 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
301 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
302 const u8 *addr, int cmd, u16 reason_code,
303 int local_state_change);
304 static void nl80211_remove_monitor_interface(
305 struct wpa_driver_nl80211_data *drv);
306 static int nl80211_send_frame_cmd(struct i802_bss *bss,
307 unsigned int freq, unsigned int wait,
308 const u8 *buf, size_t buf_len, u64 *cookie,
309 int no_cck, int no_ack, int offchanok);
310 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
311 int report);
312 #ifdef ANDROID
313 static int android_pno_start(struct i802_bss *bss,
314 struct wpa_driver_scan_params *params);
315 static int android_pno_stop(struct i802_bss *bss);
316 #endif /* ANDROID */
317
318 #ifdef HOSTAPD
319 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
320 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
321 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
322 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
323 enum wpa_driver_if_type type,
324 const char *ifname);
325 #else /* HOSTAPD */
326 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
327 {
328 }
329
330 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
331 {
332 }
333
334 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
335 {
336 return 0;
337 }
338 #endif /* HOSTAPD */
339
340 static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
341 struct hostapd_freq_params *freq);
342 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
343 int ifindex, int disabled);
344
345 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
346 static int wpa_driver_nl80211_authenticate_retry(
347 struct wpa_driver_nl80211_data *drv);
348
349
350 static const char * nl80211_command_to_string(enum nl80211_commands cmd)
351 {
352 #define C2S(x) case x: return #x;
353 switch (cmd) {
354 C2S(NL80211_CMD_UNSPEC)
355 C2S(NL80211_CMD_GET_WIPHY)
356 C2S(NL80211_CMD_SET_WIPHY)
357 C2S(NL80211_CMD_NEW_WIPHY)
358 C2S(NL80211_CMD_DEL_WIPHY)
359 C2S(NL80211_CMD_GET_INTERFACE)
360 C2S(NL80211_CMD_SET_INTERFACE)
361 C2S(NL80211_CMD_NEW_INTERFACE)
362 C2S(NL80211_CMD_DEL_INTERFACE)
363 C2S(NL80211_CMD_GET_KEY)
364 C2S(NL80211_CMD_SET_KEY)
365 C2S(NL80211_CMD_NEW_KEY)
366 C2S(NL80211_CMD_DEL_KEY)
367 C2S(NL80211_CMD_GET_BEACON)
368 C2S(NL80211_CMD_SET_BEACON)
369 C2S(NL80211_CMD_START_AP)
370 C2S(NL80211_CMD_STOP_AP)
371 C2S(NL80211_CMD_GET_STATION)
372 C2S(NL80211_CMD_SET_STATION)
373 C2S(NL80211_CMD_NEW_STATION)
374 C2S(NL80211_CMD_DEL_STATION)
375 C2S(NL80211_CMD_GET_MPATH)
376 C2S(NL80211_CMD_SET_MPATH)
377 C2S(NL80211_CMD_NEW_MPATH)
378 C2S(NL80211_CMD_DEL_MPATH)
379 C2S(NL80211_CMD_SET_BSS)
380 C2S(NL80211_CMD_SET_REG)
381 C2S(NL80211_CMD_REQ_SET_REG)
382 C2S(NL80211_CMD_GET_MESH_CONFIG)
383 C2S(NL80211_CMD_SET_MESH_CONFIG)
384 C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
385 C2S(NL80211_CMD_GET_REG)
386 C2S(NL80211_CMD_GET_SCAN)
387 C2S(NL80211_CMD_TRIGGER_SCAN)
388 C2S(NL80211_CMD_NEW_SCAN_RESULTS)
389 C2S(NL80211_CMD_SCAN_ABORTED)
390 C2S(NL80211_CMD_REG_CHANGE)
391 C2S(NL80211_CMD_AUTHENTICATE)
392 C2S(NL80211_CMD_ASSOCIATE)
393 C2S(NL80211_CMD_DEAUTHENTICATE)
394 C2S(NL80211_CMD_DISASSOCIATE)
395 C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
396 C2S(NL80211_CMD_REG_BEACON_HINT)
397 C2S(NL80211_CMD_JOIN_IBSS)
398 C2S(NL80211_CMD_LEAVE_IBSS)
399 C2S(NL80211_CMD_TESTMODE)
400 C2S(NL80211_CMD_CONNECT)
401 C2S(NL80211_CMD_ROAM)
402 C2S(NL80211_CMD_DISCONNECT)
403 C2S(NL80211_CMD_SET_WIPHY_NETNS)
404 C2S(NL80211_CMD_GET_SURVEY)
405 C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
406 C2S(NL80211_CMD_SET_PMKSA)
407 C2S(NL80211_CMD_DEL_PMKSA)
408 C2S(NL80211_CMD_FLUSH_PMKSA)
409 C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
410 C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
411 C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
412 C2S(NL80211_CMD_REGISTER_FRAME)
413 C2S(NL80211_CMD_FRAME)
414 C2S(NL80211_CMD_FRAME_TX_STATUS)
415 C2S(NL80211_CMD_SET_POWER_SAVE)
416 C2S(NL80211_CMD_GET_POWER_SAVE)
417 C2S(NL80211_CMD_SET_CQM)
418 C2S(NL80211_CMD_NOTIFY_CQM)
419 C2S(NL80211_CMD_SET_CHANNEL)
420 C2S(NL80211_CMD_SET_WDS_PEER)
421 C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
422 C2S(NL80211_CMD_JOIN_MESH)
423 C2S(NL80211_CMD_LEAVE_MESH)
424 C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
425 C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
426 C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
427 C2S(NL80211_CMD_GET_WOWLAN)
428 C2S(NL80211_CMD_SET_WOWLAN)
429 C2S(NL80211_CMD_START_SCHED_SCAN)
430 C2S(NL80211_CMD_STOP_SCHED_SCAN)
431 C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
432 C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
433 C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
434 C2S(NL80211_CMD_PMKSA_CANDIDATE)
435 C2S(NL80211_CMD_TDLS_OPER)
436 C2S(NL80211_CMD_TDLS_MGMT)
437 C2S(NL80211_CMD_UNEXPECTED_FRAME)
438 C2S(NL80211_CMD_PROBE_CLIENT)
439 C2S(NL80211_CMD_REGISTER_BEACONS)
440 C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
441 C2S(NL80211_CMD_SET_NOACK_MAP)
442 C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
443 C2S(NL80211_CMD_START_P2P_DEVICE)
444 C2S(NL80211_CMD_STOP_P2P_DEVICE)
445 C2S(NL80211_CMD_CONN_FAILED)
446 C2S(NL80211_CMD_SET_MCAST_RATE)
447 C2S(NL80211_CMD_SET_MAC_ACL)
448 C2S(NL80211_CMD_RADAR_DETECT)
449 C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
450 C2S(NL80211_CMD_UPDATE_FT_IES)
451 C2S(NL80211_CMD_FT_EVENT)
452 C2S(NL80211_CMD_CRIT_PROTOCOL_START)
453 C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
454 default:
455 return "NL80211_CMD_UNKNOWN";
456 }
457 #undef C2S
458 }
459
460
461 static int is_ap_interface(enum nl80211_iftype nlmode)
462 {
463 return (nlmode == NL80211_IFTYPE_AP ||
464 nlmode == NL80211_IFTYPE_P2P_GO);
465 }
466
467
468 static int is_sta_interface(enum nl80211_iftype nlmode)
469 {
470 return (nlmode == NL80211_IFTYPE_STATION ||
471 nlmode == NL80211_IFTYPE_P2P_CLIENT);
472 }
473
474
475 static int is_p2p_net_interface(enum nl80211_iftype nlmode)
476 {
477 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
478 nlmode == NL80211_IFTYPE_P2P_GO);
479 }
480
481
482 static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
483 {
484 if (drv->associated)
485 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
486 drv->associated = 0;
487 os_memset(drv->bssid, 0, ETH_ALEN);
488 }
489
490
491 struct nl80211_bss_info_arg {
492 struct wpa_driver_nl80211_data *drv;
493 struct wpa_scan_results *res;
494 unsigned int assoc_freq;
495 u8 assoc_bssid[ETH_ALEN];
496 };
497
498 static int bss_info_handler(struct nl_msg *msg, void *arg);
499
500
501 /* nl80211 code */
502 static int ack_handler(struct nl_msg *msg, void *arg)
503 {
504 int *err = arg;
505 *err = 0;
506 return NL_STOP;
507 }
508
509 static int finish_handler(struct nl_msg *msg, void *arg)
510 {
511 int *ret = arg;
512 *ret = 0;
513 return NL_SKIP;
514 }
515
516 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
517 void *arg)
518 {
519 int *ret = arg;
520 *ret = err->error;
521 return NL_SKIP;
522 }
523
524
525 static int no_seq_check(struct nl_msg *msg, void *arg)
526 {
527 return NL_OK;
528 }
529
530
531 static int send_and_recv(struct nl80211_global *global,
532 struct nl_handle *nl_handle, struct nl_msg *msg,
533 int (*valid_handler)(struct nl_msg *, void *),
534 void *valid_data)
535 {
536 struct nl_cb *cb;
537 int err = -ENOMEM;
538
539 cb = nl_cb_clone(global->nl_cb);
540 if (!cb)
541 goto out;
542
543 err = nl_send_auto_complete(nl_handle, msg);
544 if (err < 0)
545 goto out;
546
547 err = 1;
548
549 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
550 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
551 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
552
553 if (valid_handler)
554 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
555 valid_handler, valid_data);
556
557 while (err > 0)
558 nl_recvmsgs(nl_handle, cb);
559 out:
560 nl_cb_put(cb);
561 nlmsg_free(msg);
562 return err;
563 }
564
565
566 static int send_and_recv_msgs_global(struct nl80211_global *global,
567 struct nl_msg *msg,
568 int (*valid_handler)(struct nl_msg *, void *),
569 void *valid_data)
570 {
571 return send_and_recv(global, global->nl, msg, valid_handler,
572 valid_data);
573 }
574
575
576 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
577 struct nl_msg *msg,
578 int (*valid_handler)(struct nl_msg *, void *),
579 void *valid_data)
580 {
581 return send_and_recv(drv->global, drv->global->nl, msg,
582 valid_handler, valid_data);
583 }
584
585
586 struct family_data {
587 const char *group;
588 int id;
589 };
590
591
592 static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
593 {
594 if (bss->wdev_id_set)
595 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
596 else
597 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
598 return 0;
599
600 nla_put_failure:
601 return -1;
602 }
603
604
605 static int family_handler(struct nl_msg *msg, void *arg)
606 {
607 struct family_data *res = arg;
608 struct nlattr *tb[CTRL_ATTR_MAX + 1];
609 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
610 struct nlattr *mcgrp;
611 int i;
612
613 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
614 genlmsg_attrlen(gnlh, 0), NULL);
615 if (!tb[CTRL_ATTR_MCAST_GROUPS])
616 return NL_SKIP;
617
618 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
619 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
620 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
621 nla_len(mcgrp), NULL);
622 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
623 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
624 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
625 res->group,
626 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
627 continue;
628 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
629 break;
630 };
631
632 return NL_SKIP;
633 }
634
635
636 static int nl_get_multicast_id(struct nl80211_global *global,
637 const char *family, const char *group)
638 {
639 struct nl_msg *msg;
640 int ret = -1;
641 struct family_data res = { group, -ENOENT };
642
643 msg = nlmsg_alloc();
644 if (!msg)
645 return -ENOMEM;
646 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
647 0, 0, CTRL_CMD_GETFAMILY, 0);
648 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
649
650 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
651 msg = NULL;
652 if (ret == 0)
653 ret = res.id;
654
655 nla_put_failure:
656 nlmsg_free(msg);
657 return ret;
658 }
659
660
661 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
662 struct nl_msg *msg, int flags, uint8_t cmd)
663 {
664 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
665 0, flags, cmd, 0);
666 }
667
668
669 struct wiphy_idx_data {
670 int wiphy_idx;
671 enum nl80211_iftype nlmode;
672 };
673
674
675 static int netdev_info_handler(struct nl_msg *msg, void *arg)
676 {
677 struct nlattr *tb[NL80211_ATTR_MAX + 1];
678 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
679 struct wiphy_idx_data *info = arg;
680
681 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
682 genlmsg_attrlen(gnlh, 0), NULL);
683
684 if (tb[NL80211_ATTR_WIPHY])
685 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
686
687 if (tb[NL80211_ATTR_IFTYPE])
688 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
689
690 return NL_SKIP;
691 }
692
693
694 static int nl80211_get_wiphy_index(struct i802_bss *bss)
695 {
696 struct nl_msg *msg;
697 struct wiphy_idx_data data = {
698 .wiphy_idx = -1,
699 };
700
701 msg = nlmsg_alloc();
702 if (!msg)
703 return -1;
704
705 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
706
707 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
708
709 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
710 return data.wiphy_idx;
711 msg = NULL;
712 nla_put_failure:
713 nlmsg_free(msg);
714 return -1;
715 }
716
717
718 #ifndef HOSTAPD
719 static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
720 {
721 struct nl_msg *msg;
722 struct wiphy_idx_data data = {
723 .wiphy_idx = -1,
724 };
725
726 msg = nlmsg_alloc();
727 if (!msg)
728 return -1;
729
730 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
731
732 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
733
734 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
735 return data.nlmode;
736 msg = NULL;
737 nla_put_failure:
738 nlmsg_free(msg);
739 return NL80211_IFTYPE_UNSPECIFIED;
740 }
741 #endif /* HOSTAPD */
742
743
744 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
745 struct nl80211_wiphy_data *w)
746 {
747 struct nl_msg *msg;
748 int ret = -1;
749
750 msg = nlmsg_alloc();
751 if (!msg)
752 return -1;
753
754 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
755
756 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
757
758 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
759 msg = NULL;
760 if (ret) {
761 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
762 "failed: ret=%d (%s)",
763 ret, strerror(-ret));
764 goto nla_put_failure;
765 }
766 ret = 0;
767 nla_put_failure:
768 nlmsg_free(msg);
769 return ret;
770 }
771
772
773 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
774 {
775 struct nl80211_wiphy_data *w = eloop_ctx;
776
777 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
778
779 nl_recvmsgs(handle, w->nl_cb);
780 }
781
782
783 static int process_beacon_event(struct nl_msg *msg, void *arg)
784 {
785 struct nl80211_wiphy_data *w = arg;
786 struct wpa_driver_nl80211_data *drv;
787 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
788 struct nlattr *tb[NL80211_ATTR_MAX + 1];
789 union wpa_event_data event;
790
791 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
792 genlmsg_attrlen(gnlh, 0), NULL);
793
794 if (gnlh->cmd != NL80211_CMD_FRAME) {
795 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
796 gnlh->cmd);
797 return NL_SKIP;
798 }
799
800 if (!tb[NL80211_ATTR_FRAME])
801 return NL_SKIP;
802
803 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
804 wiphy_list) {
805 os_memset(&event, 0, sizeof(event));
806 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
807 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
808 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
809 }
810
811 return NL_SKIP;
812 }
813
814
815 static struct nl80211_wiphy_data *
816 nl80211_get_wiphy_data_ap(struct i802_bss *bss)
817 {
818 static DEFINE_DL_LIST(nl80211_wiphys);
819 struct nl80211_wiphy_data *w;
820 int wiphy_idx, found = 0;
821 struct i802_bss *tmp_bss;
822
823 if (bss->wiphy_data != NULL)
824 return bss->wiphy_data;
825
826 wiphy_idx = nl80211_get_wiphy_index(bss);
827
828 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
829 if (w->wiphy_idx == wiphy_idx)
830 goto add;
831 }
832
833 /* alloc new one */
834 w = os_zalloc(sizeof(*w));
835 if (w == NULL)
836 return NULL;
837 w->wiphy_idx = wiphy_idx;
838 dl_list_init(&w->bsss);
839 dl_list_init(&w->drvs);
840
841 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
842 if (!w->nl_cb) {
843 os_free(w);
844 return NULL;
845 }
846 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
847 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
848 w);
849
850 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
851 "wiphy beacons");
852 if (w->nl_beacons == NULL) {
853 os_free(w);
854 return NULL;
855 }
856
857 if (nl80211_register_beacons(bss->drv, w)) {
858 nl_destroy_handles(&w->nl_beacons);
859 os_free(w);
860 return NULL;
861 }
862
863 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
864 nl80211_recv_beacons, w, w->nl_beacons);
865
866 dl_list_add(&nl80211_wiphys, &w->list);
867
868 add:
869 /* drv entry for this bss already there? */
870 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
871 if (tmp_bss->drv == bss->drv) {
872 found = 1;
873 break;
874 }
875 }
876 /* if not add it */
877 if (!found)
878 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
879
880 dl_list_add(&w->bsss, &bss->wiphy_list);
881 bss->wiphy_data = w;
882 return w;
883 }
884
885
886 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
887 {
888 struct nl80211_wiphy_data *w = bss->wiphy_data;
889 struct i802_bss *tmp_bss;
890 int found = 0;
891
892 if (w == NULL)
893 return;
894 bss->wiphy_data = NULL;
895 dl_list_del(&bss->wiphy_list);
896
897 /* still any for this drv present? */
898 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
899 if (tmp_bss->drv == bss->drv) {
900 found = 1;
901 break;
902 }
903 }
904 /* if not remove it */
905 if (!found)
906 dl_list_del(&bss->drv->wiphy_list);
907
908 if (!dl_list_empty(&w->bsss))
909 return;
910
911 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
912
913 nl_cb_put(w->nl_cb);
914 nl_destroy_handles(&w->nl_beacons);
915 dl_list_del(&w->list);
916 os_free(w);
917 }
918
919
920 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
921 {
922 struct i802_bss *bss = priv;
923 struct wpa_driver_nl80211_data *drv = bss->drv;
924 if (!drv->associated)
925 return -1;
926 os_memcpy(bssid, drv->bssid, ETH_ALEN);
927 return 0;
928 }
929
930
931 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
932 {
933 struct i802_bss *bss = priv;
934 struct wpa_driver_nl80211_data *drv = bss->drv;
935 if (!drv->associated)
936 return -1;
937 os_memcpy(ssid, drv->ssid, drv->ssid_len);
938 return drv->ssid_len;
939 }
940
941
942 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
943 char *buf, size_t len, int del)
944 {
945 union wpa_event_data event;
946
947 os_memset(&event, 0, sizeof(event));
948 if (len > sizeof(event.interface_status.ifname))
949 len = sizeof(event.interface_status.ifname) - 1;
950 os_memcpy(event.interface_status.ifname, buf, len);
951 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
952 EVENT_INTERFACE_ADDED;
953
954 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
955 del ? "DEL" : "NEW",
956 event.interface_status.ifname,
957 del ? "removed" : "added");
958
959 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
960 if (del) {
961 if (drv->if_removed) {
962 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
963 "already set - ignore event");
964 return;
965 }
966 drv->if_removed = 1;
967 } else {
968 if (if_nametoindex(drv->first_bss.ifname) == 0) {
969 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
970 "does not exist - ignore "
971 "RTM_NEWLINK",
972 drv->first_bss.ifname);
973 return;
974 }
975 if (!drv->if_removed) {
976 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
977 "already cleared - ignore event");
978 return;
979 }
980 drv->if_removed = 0;
981 }
982 }
983
984 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
985 }
986
987
988 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
989 u8 *buf, size_t len)
990 {
991 int attrlen, rta_len;
992 struct rtattr *attr;
993
994 attrlen = len;
995 attr = (struct rtattr *) buf;
996
997 rta_len = RTA_ALIGN(sizeof(struct rtattr));
998 while (RTA_OK(attr, attrlen)) {
999 if (attr->rta_type == IFLA_IFNAME) {
1000 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
1001 == 0)
1002 return 1;
1003 else
1004 break;
1005 }
1006 attr = RTA_NEXT(attr, attrlen);
1007 }
1008
1009 return 0;
1010 }
1011
1012
1013 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
1014 int ifindex, u8 *buf, size_t len)
1015 {
1016 if (drv->ifindex == ifindex)
1017 return 1;
1018
1019 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
1020 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
1021 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
1022 "interface");
1023 wpa_driver_nl80211_finish_drv_init(drv);
1024 return 1;
1025 }
1026
1027 return 0;
1028 }
1029
1030
1031 static struct wpa_driver_nl80211_data *
1032 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
1033 {
1034 struct wpa_driver_nl80211_data *drv;
1035 dl_list_for_each(drv, &global->interfaces,
1036 struct wpa_driver_nl80211_data, list) {
1037 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
1038 have_ifidx(drv, idx))
1039 return drv;
1040 }
1041 return NULL;
1042 }
1043
1044
1045 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
1046 struct ifinfomsg *ifi,
1047 u8 *buf, size_t len)
1048 {
1049 struct nl80211_global *global = ctx;
1050 struct wpa_driver_nl80211_data *drv;
1051 int attrlen, rta_len;
1052 struct rtattr *attr;
1053 u32 brid = 0;
1054 char namebuf[IFNAMSIZ];
1055
1056 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1057 if (!drv) {
1058 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
1059 "ifindex %d", ifi->ifi_index);
1060 return;
1061 }
1062
1063 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
1064 "(%s%s%s%s)",
1065 drv->operstate, ifi->ifi_flags,
1066 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1067 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1068 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1069 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1070
1071 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
1072 if (if_indextoname(ifi->ifi_index, namebuf) &&
1073 linux_iface_up(drv->global->ioctl_sock,
1074 drv->first_bss.ifname) > 0) {
1075 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1076 "event since interface %s is up", namebuf);
1077 return;
1078 }
1079 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
1080 if (drv->ignore_if_down_event) {
1081 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1082 "event generated by mode change");
1083 drv->ignore_if_down_event = 0;
1084 } else {
1085 drv->if_disabled = 1;
1086 wpa_supplicant_event(drv->ctx,
1087 EVENT_INTERFACE_DISABLED, NULL);
1088 }
1089 }
1090
1091 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
1092 if (if_indextoname(ifi->ifi_index, namebuf) &&
1093 linux_iface_up(drv->global->ioctl_sock,
1094 drv->first_bss.ifname) == 0) {
1095 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1096 "event since interface %s is down",
1097 namebuf);
1098 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
1099 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1100 "event since interface %s does not exist",
1101 drv->first_bss.ifname);
1102 } else if (drv->if_removed) {
1103 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1104 "event since interface %s is marked "
1105 "removed", drv->first_bss.ifname);
1106 } else {
1107 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1108 drv->if_disabled = 0;
1109 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1110 NULL);
1111 }
1112 }
1113
1114 /*
1115 * Some drivers send the association event before the operup event--in
1116 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1117 * fails. This will hit us when wpa_supplicant does not need to do
1118 * IEEE 802.1X authentication
1119 */
1120 if (drv->operstate == 1 &&
1121 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
1122 !(ifi->ifi_flags & IFF_RUNNING))
1123 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
1124 -1, IF_OPER_UP);
1125
1126 attrlen = len;
1127 attr = (struct rtattr *) buf;
1128 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1129 while (RTA_OK(attr, attrlen)) {
1130 if (attr->rta_type == IFLA_IFNAME) {
1131 wpa_driver_nl80211_event_link(
1132 drv,
1133 ((char *) attr) + rta_len,
1134 attr->rta_len - rta_len, 0);
1135 } else if (attr->rta_type == IFLA_MASTER)
1136 brid = nla_get_u32((struct nlattr *) attr);
1137 attr = RTA_NEXT(attr, attrlen);
1138 }
1139
1140 if (ifi->ifi_family == AF_BRIDGE && brid) {
1141 /* device has been added to bridge */
1142 if_indextoname(brid, namebuf);
1143 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1144 brid, namebuf);
1145 add_ifidx(drv, brid);
1146 }
1147 }
1148
1149
1150 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1151 struct ifinfomsg *ifi,
1152 u8 *buf, size_t len)
1153 {
1154 struct nl80211_global *global = ctx;
1155 struct wpa_driver_nl80211_data *drv;
1156 int attrlen, rta_len;
1157 struct rtattr *attr;
1158 u32 brid = 0;
1159
1160 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1161 if (!drv) {
1162 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1163 "foreign ifindex %d", ifi->ifi_index);
1164 return;
1165 }
1166
1167 attrlen = len;
1168 attr = (struct rtattr *) buf;
1169
1170 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1171 while (RTA_OK(attr, attrlen)) {
1172 if (attr->rta_type == IFLA_IFNAME) {
1173 wpa_driver_nl80211_event_link(
1174 drv,
1175 ((char *) attr) + rta_len,
1176 attr->rta_len - rta_len, 1);
1177 } else if (attr->rta_type == IFLA_MASTER)
1178 brid = nla_get_u32((struct nlattr *) attr);
1179 attr = RTA_NEXT(attr, attrlen);
1180 }
1181
1182 if (ifi->ifi_family == AF_BRIDGE && brid) {
1183 /* device has been removed from bridge */
1184 char namebuf[IFNAMSIZ];
1185 if_indextoname(brid, namebuf);
1186 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1187 "%s", brid, namebuf);
1188 del_ifidx(drv, brid);
1189 }
1190 }
1191
1192
1193 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1194 const u8 *frame, size_t len)
1195 {
1196 const struct ieee80211_mgmt *mgmt;
1197 union wpa_event_data event;
1198
1199 wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
1200 mgmt = (const struct ieee80211_mgmt *) frame;
1201 if (len < 24 + sizeof(mgmt->u.auth)) {
1202 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1203 "frame");
1204 return;
1205 }
1206
1207 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1208 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
1209 os_memset(&event, 0, sizeof(event));
1210 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1211 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1212 event.auth.auth_transaction =
1213 le_to_host16(mgmt->u.auth.auth_transaction);
1214 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1215 if (len > 24 + sizeof(mgmt->u.auth)) {
1216 event.auth.ies = mgmt->u.auth.variable;
1217 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1218 }
1219
1220 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1221 }
1222
1223
1224 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1225 {
1226 struct nl_msg *msg;
1227 int ret;
1228 struct nl80211_bss_info_arg arg;
1229
1230 os_memset(&arg, 0, sizeof(arg));
1231 msg = nlmsg_alloc();
1232 if (!msg)
1233 goto nla_put_failure;
1234
1235 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1236 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1237
1238 arg.drv = drv;
1239 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1240 msg = NULL;
1241 if (ret == 0) {
1242 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1243 "associated BSS from scan results: %u MHz",
1244 arg.assoc_freq);
1245 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1246 }
1247 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1248 "(%s)", ret, strerror(-ret));
1249 nla_put_failure:
1250 nlmsg_free(msg);
1251 return drv->assoc_freq;
1252 }
1253
1254
1255 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1256 const u8 *frame, size_t len)
1257 {
1258 const struct ieee80211_mgmt *mgmt;
1259 union wpa_event_data event;
1260 u16 status;
1261
1262 wpa_printf(MSG_DEBUG, "nl80211: Associate event");
1263 mgmt = (const struct ieee80211_mgmt *) frame;
1264 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1265 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1266 "frame");
1267 return;
1268 }
1269
1270 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1271 if (status != WLAN_STATUS_SUCCESS) {
1272 os_memset(&event, 0, sizeof(event));
1273 event.assoc_reject.bssid = mgmt->bssid;
1274 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1275 event.assoc_reject.resp_ies =
1276 (u8 *) mgmt->u.assoc_resp.variable;
1277 event.assoc_reject.resp_ies_len =
1278 len - 24 - sizeof(mgmt->u.assoc_resp);
1279 }
1280 event.assoc_reject.status_code = status;
1281
1282 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1283 return;
1284 }
1285
1286 drv->associated = 1;
1287 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1288 os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
1289
1290 os_memset(&event, 0, sizeof(event));
1291 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1292 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1293 event.assoc_info.resp_ies_len =
1294 len - 24 - sizeof(mgmt->u.assoc_resp);
1295 }
1296
1297 event.assoc_info.freq = drv->assoc_freq;
1298
1299 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1300 }
1301
1302
1303 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1304 enum nl80211_commands cmd, struct nlattr *status,
1305 struct nlattr *addr, struct nlattr *req_ie,
1306 struct nlattr *resp_ie)
1307 {
1308 union wpa_event_data event;
1309
1310 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1311 /*
1312 * Avoid reporting two association events that would confuse
1313 * the core code.
1314 */
1315 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1316 "when using userspace SME", cmd);
1317 return;
1318 }
1319
1320 if (cmd == NL80211_CMD_CONNECT)
1321 wpa_printf(MSG_DEBUG, "nl80211: Connect event");
1322 else if (cmd == NL80211_CMD_ROAM)
1323 wpa_printf(MSG_DEBUG, "nl80211: Roam event");
1324
1325 os_memset(&event, 0, sizeof(event));
1326 if (cmd == NL80211_CMD_CONNECT &&
1327 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1328 if (addr)
1329 event.assoc_reject.bssid = nla_data(addr);
1330 if (resp_ie) {
1331 event.assoc_reject.resp_ies = nla_data(resp_ie);
1332 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1333 }
1334 event.assoc_reject.status_code = nla_get_u16(status);
1335 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1336 return;
1337 }
1338
1339 drv->associated = 1;
1340 if (addr) {
1341 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1342 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
1343 }
1344
1345 if (req_ie) {
1346 event.assoc_info.req_ies = nla_data(req_ie);
1347 event.assoc_info.req_ies_len = nla_len(req_ie);
1348 }
1349 if (resp_ie) {
1350 event.assoc_info.resp_ies = nla_data(resp_ie);
1351 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1352 }
1353
1354 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1355
1356 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1357 }
1358
1359
1360 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
1361 struct nlattr *reason, struct nlattr *addr,
1362 struct nlattr *by_ap)
1363 {
1364 union wpa_event_data data;
1365 unsigned int locally_generated = by_ap == NULL;
1366
1367 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1368 /*
1369 * Avoid reporting two disassociation events that could
1370 * confuse the core code.
1371 */
1372 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1373 "event when using userspace SME");
1374 return;
1375 }
1376
1377 if (drv->ignore_next_local_disconnect) {
1378 drv->ignore_next_local_disconnect = 0;
1379 if (locally_generated) {
1380 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1381 "event triggered during reassociation");
1382 return;
1383 }
1384 wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
1385 "disconnect but got another disconnect "
1386 "event first");
1387 }
1388
1389 wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
1390 nl80211_mark_disconnected(drv);
1391 os_memset(&data, 0, sizeof(data));
1392 if (reason)
1393 data.deauth_info.reason_code = nla_get_u16(reason);
1394 data.deauth_info.locally_generated = by_ap == NULL;
1395 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
1396 }
1397
1398
1399 static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
1400 struct nlattr *freq, struct nlattr *type)
1401 {
1402 union wpa_event_data data;
1403 int ht_enabled = 1;
1404 int chan_offset = 0;
1405
1406 wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
1407
1408 if (!freq || !type)
1409 return;
1410
1411 switch (nla_get_u32(type)) {
1412 case NL80211_CHAN_NO_HT:
1413 ht_enabled = 0;
1414 break;
1415 case NL80211_CHAN_HT20:
1416 break;
1417 case NL80211_CHAN_HT40PLUS:
1418 chan_offset = 1;
1419 break;
1420 case NL80211_CHAN_HT40MINUS:
1421 chan_offset = -1;
1422 break;
1423 }
1424
1425 data.ch_switch.freq = nla_get_u32(freq);
1426 data.ch_switch.ht_enabled = ht_enabled;
1427 data.ch_switch.ch_offset = chan_offset;
1428
1429 wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
1430 }
1431
1432
1433 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1434 enum nl80211_commands cmd, struct nlattr *addr)
1435 {
1436 union wpa_event_data event;
1437 enum wpa_event_type ev;
1438
1439 if (nla_len(addr) != ETH_ALEN)
1440 return;
1441
1442 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1443 cmd, MAC2STR((u8 *) nla_data(addr)));
1444
1445 if (cmd == NL80211_CMD_AUTHENTICATE)
1446 ev = EVENT_AUTH_TIMED_OUT;
1447 else if (cmd == NL80211_CMD_ASSOCIATE)
1448 ev = EVENT_ASSOC_TIMED_OUT;
1449 else
1450 return;
1451
1452 os_memset(&event, 0, sizeof(event));
1453 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1454 wpa_supplicant_event(drv->ctx, ev, &event);
1455 }
1456
1457
1458 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
1459 struct nlattr *freq, struct nlattr *sig,
1460 const u8 *frame, size_t len)
1461 {
1462 const struct ieee80211_mgmt *mgmt;
1463 union wpa_event_data event;
1464 u16 fc, stype;
1465 int ssi_signal = 0;
1466
1467 wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
1468 mgmt = (const struct ieee80211_mgmt *) frame;
1469 if (len < 24) {
1470 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1471 return;
1472 }
1473
1474 fc = le_to_host16(mgmt->frame_control);
1475 stype = WLAN_FC_GET_STYPE(fc);
1476
1477 if (sig)
1478 ssi_signal = (s32) nla_get_u32(sig);
1479
1480 os_memset(&event, 0, sizeof(event));
1481 if (freq) {
1482 event.rx_action.freq = nla_get_u32(freq);
1483 drv->last_mgmt_freq = event.rx_action.freq;
1484 }
1485 if (stype == WLAN_FC_STYPE_ACTION) {
1486 event.rx_action.da = mgmt->da;
1487 event.rx_action.sa = mgmt->sa;
1488 event.rx_action.bssid = mgmt->bssid;
1489 event.rx_action.category = mgmt->u.action.category;
1490 event.rx_action.data = &mgmt->u.action.category + 1;
1491 event.rx_action.len = frame + len - event.rx_action.data;
1492 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1493 } else {
1494 event.rx_mgmt.frame = frame;
1495 event.rx_mgmt.frame_len = len;
1496 event.rx_mgmt.ssi_signal = ssi_signal;
1497 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1498 }
1499 }
1500
1501
1502 static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1503 struct nlattr *cookie, const u8 *frame,
1504 size_t len, struct nlattr *ack)
1505 {
1506 union wpa_event_data event;
1507 const struct ieee80211_hdr *hdr;
1508 u16 fc;
1509
1510 wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
1511 if (!is_ap_interface(drv->nlmode)) {
1512 u64 cookie_val;
1513
1514 if (!cookie)
1515 return;
1516
1517 cookie_val = nla_get_u64(cookie);
1518 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1519 " cookie=0%llx%s (ack=%d)",
1520 (long long unsigned int) cookie_val,
1521 cookie_val == drv->send_action_cookie ?
1522 " (match)" : " (unknown)", ack != NULL);
1523 if (cookie_val != drv->send_action_cookie)
1524 return;
1525 }
1526
1527 hdr = (const struct ieee80211_hdr *) frame;
1528 fc = le_to_host16(hdr->frame_control);
1529
1530 os_memset(&event, 0, sizeof(event));
1531 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1532 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1533 event.tx_status.dst = hdr->addr1;
1534 event.tx_status.data = frame;
1535 event.tx_status.data_len = len;
1536 event.tx_status.ack = ack != NULL;
1537 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1538 }
1539
1540
1541 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1542 enum wpa_event_type type,
1543 const u8 *frame, size_t len)
1544 {
1545 const struct ieee80211_mgmt *mgmt;
1546 union wpa_event_data event;
1547 const u8 *bssid = NULL;
1548 u16 reason_code = 0;
1549
1550 if (type == EVENT_DEAUTH)
1551 wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
1552 else
1553 wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
1554
1555 mgmt = (const struct ieee80211_mgmt *) frame;
1556 if (len >= 24) {
1557 bssid = mgmt->bssid;
1558
1559 if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
1560 !drv->associated &&
1561 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
1562 os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
1563 os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
1564 /*
1565 * Avoid issues with some roaming cases where
1566 * disconnection event for the old AP may show up after
1567 * we have started connection with the new AP.
1568 */
1569 wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
1570 MAC2STR(bssid),
1571 MAC2STR(drv->auth_attempt_bssid));
1572 return;
1573 }
1574
1575 if (drv->associated != 0 &&
1576 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1577 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1578 /*
1579 * We have presumably received this deauth as a
1580 * response to a clear_state_mismatch() outgoing
1581 * deauth. Don't let it take us offline!
1582 */
1583 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1584 "from Unknown BSSID " MACSTR " -- ignoring",
1585 MAC2STR(bssid));
1586 return;
1587 }
1588 }
1589
1590 nl80211_mark_disconnected(drv);
1591 os_memset(&event, 0, sizeof(event));
1592
1593 /* Note: Same offset for Reason Code in both frame subtypes */
1594 if (len >= 24 + sizeof(mgmt->u.deauth))
1595 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1596
1597 if (type == EVENT_DISASSOC) {
1598 event.disassoc_info.locally_generated =
1599 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1600 event.disassoc_info.addr = bssid;
1601 event.disassoc_info.reason_code = reason_code;
1602 if (frame + len > mgmt->u.disassoc.variable) {
1603 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1604 event.disassoc_info.ie_len = frame + len -
1605 mgmt->u.disassoc.variable;
1606 }
1607 } else {
1608 event.deauth_info.locally_generated =
1609 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1610 event.deauth_info.addr = bssid;
1611 event.deauth_info.reason_code = reason_code;
1612 if (frame + len > mgmt->u.deauth.variable) {
1613 event.deauth_info.ie = mgmt->u.deauth.variable;
1614 event.deauth_info.ie_len = frame + len -
1615 mgmt->u.deauth.variable;
1616 }
1617 }
1618
1619 wpa_supplicant_event(drv->ctx, type, &event);
1620 }
1621
1622
1623 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1624 enum wpa_event_type type,
1625 const u8 *frame, size_t len)
1626 {
1627 const struct ieee80211_mgmt *mgmt;
1628 union wpa_event_data event;
1629 u16 reason_code = 0;
1630
1631 if (type == EVENT_UNPROT_DEAUTH)
1632 wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
1633 else
1634 wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
1635
1636 if (len < 24)
1637 return;
1638
1639 mgmt = (const struct ieee80211_mgmt *) frame;
1640
1641 os_memset(&event, 0, sizeof(event));
1642 /* Note: Same offset for Reason Code in both frame subtypes */
1643 if (len >= 24 + sizeof(mgmt->u.deauth))
1644 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1645
1646 if (type == EVENT_UNPROT_DISASSOC) {
1647 event.unprot_disassoc.sa = mgmt->sa;
1648 event.unprot_disassoc.da = mgmt->da;
1649 event.unprot_disassoc.reason_code = reason_code;
1650 } else {
1651 event.unprot_deauth.sa = mgmt->sa;
1652 event.unprot_deauth.da = mgmt->da;
1653 event.unprot_deauth.reason_code = reason_code;
1654 }
1655
1656 wpa_supplicant_event(drv->ctx, type, &event);
1657 }
1658
1659
1660 static void mlme_event(struct i802_bss *bss,
1661 enum nl80211_commands cmd, struct nlattr *frame,
1662 struct nlattr *addr, struct nlattr *timed_out,
1663 struct nlattr *freq, struct nlattr *ack,
1664 struct nlattr *cookie, struct nlattr *sig)
1665 {
1666 struct wpa_driver_nl80211_data *drv = bss->drv;
1667 const u8 *data;
1668 size_t len;
1669
1670 if (timed_out && addr) {
1671 mlme_timeout_event(drv, cmd, addr);
1672 return;
1673 }
1674
1675 if (frame == NULL) {
1676 wpa_printf(MSG_DEBUG,
1677 "nl80211: MLME event %d (%s) without frame data",
1678 cmd, nl80211_command_to_string(cmd));
1679 return;
1680 }
1681
1682 data = nla_data(frame);
1683 len = nla_len(frame);
1684 if (len < 4 + 2 * ETH_ALEN) {
1685 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1686 MACSTR ") - too short",
1687 cmd, nl80211_command_to_string(cmd), bss->ifname,
1688 MAC2STR(bss->addr));
1689 return;
1690 }
1691 wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1692 ") A1=" MACSTR " A2=" MACSTR, cmd,
1693 nl80211_command_to_string(cmd), bss->ifname,
1694 MAC2STR(bss->addr), MAC2STR(data + 4),
1695 MAC2STR(data + 4 + ETH_ALEN));
1696 if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1697 os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1698 os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
1699 wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1700 "for foreign address", bss->ifname);
1701 return;
1702 }
1703 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1704 nla_data(frame), nla_len(frame));
1705
1706 switch (cmd) {
1707 case NL80211_CMD_AUTHENTICATE:
1708 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1709 break;
1710 case NL80211_CMD_ASSOCIATE:
1711 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1712 break;
1713 case NL80211_CMD_DEAUTHENTICATE:
1714 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1715 nla_data(frame), nla_len(frame));
1716 break;
1717 case NL80211_CMD_DISASSOCIATE:
1718 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1719 nla_data(frame), nla_len(frame));
1720 break;
1721 case NL80211_CMD_FRAME:
1722 mlme_event_mgmt(drv, freq, sig, nla_data(frame),
1723 nla_len(frame));
1724 break;
1725 case NL80211_CMD_FRAME_TX_STATUS:
1726 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1727 nla_len(frame), ack);
1728 break;
1729 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1730 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1731 nla_data(frame), nla_len(frame));
1732 break;
1733 case NL80211_CMD_UNPROT_DISASSOCIATE:
1734 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1735 nla_data(frame), nla_len(frame));
1736 break;
1737 default:
1738 break;
1739 }
1740 }
1741
1742
1743 static void mlme_event_michael_mic_failure(struct i802_bss *bss,
1744 struct nlattr *tb[])
1745 {
1746 union wpa_event_data data;
1747
1748 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1749 os_memset(&data, 0, sizeof(data));
1750 if (tb[NL80211_ATTR_MAC]) {
1751 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1752 nla_data(tb[NL80211_ATTR_MAC]),
1753 nla_len(tb[NL80211_ATTR_MAC]));
1754 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1755 }
1756 if (tb[NL80211_ATTR_KEY_SEQ]) {
1757 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1758 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1759 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1760 }
1761 if (tb[NL80211_ATTR_KEY_TYPE]) {
1762 enum nl80211_key_type key_type =
1763 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1764 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1765 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1766 data.michael_mic_failure.unicast = 1;
1767 } else
1768 data.michael_mic_failure.unicast = 1;
1769
1770 if (tb[NL80211_ATTR_KEY_IDX]) {
1771 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1772 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1773 }
1774
1775 wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1776 }
1777
1778
1779 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1780 struct nlattr *tb[])
1781 {
1782 if (tb[NL80211_ATTR_MAC] == NULL) {
1783 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1784 "event");
1785 return;
1786 }
1787 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1788 drv->associated = 1;
1789 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1790 MAC2STR(drv->bssid));
1791
1792 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1793 }
1794
1795
1796 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1797 int cancel_event, struct nlattr *tb[])
1798 {
1799 unsigned int freq, chan_type, duration;
1800 union wpa_event_data data;
1801 u64 cookie;
1802
1803 if (tb[NL80211_ATTR_WIPHY_FREQ])
1804 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1805 else
1806 freq = 0;
1807
1808 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1809 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1810 else
1811 chan_type = 0;
1812
1813 if (tb[NL80211_ATTR_DURATION])
1814 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1815 else
1816 duration = 0;
1817
1818 if (tb[NL80211_ATTR_COOKIE])
1819 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1820 else
1821 cookie = 0;
1822
1823 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1824 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1825 cancel_event, freq, chan_type, duration,
1826 (long long unsigned int) cookie,
1827 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1828
1829 if (cookie != drv->remain_on_chan_cookie)
1830 return; /* not for us */
1831
1832 if (cancel_event)
1833 drv->pending_remain_on_chan = 0;
1834
1835 os_memset(&data, 0, sizeof(data));
1836 data.remain_on_channel.freq = freq;
1837 data.remain_on_channel.duration = duration;
1838 wpa_supplicant_event(drv->ctx, cancel_event ?
1839 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1840 EVENT_REMAIN_ON_CHANNEL, &data);
1841 }
1842
1843
1844 static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1845 struct nlattr *tb[])
1846 {
1847 union wpa_event_data data;
1848
1849 os_memset(&data, 0, sizeof(data));
1850
1851 if (tb[NL80211_ATTR_IE]) {
1852 data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1853 data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1854 }
1855
1856 if (tb[NL80211_ATTR_IE_RIC]) {
1857 data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1858 data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1859 }
1860
1861 if (tb[NL80211_ATTR_MAC])
1862 os_memcpy(data.ft_ies.target_ap,
1863 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1864
1865 wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1866 MAC2STR(data.ft_ies.target_ap));
1867
1868 wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1869 }
1870
1871
1872 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1873 struct nlattr *tb[])
1874 {
1875 union wpa_event_data event;
1876 struct nlattr *nl;
1877 int rem;
1878 struct scan_info *info;
1879 #define MAX_REPORT_FREQS 50
1880 int freqs[MAX_REPORT_FREQS];
1881 int num_freqs = 0;
1882
1883 if (drv->scan_for_auth) {
1884 drv->scan_for_auth = 0;
1885 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1886 "cfg80211 BSS entry");
1887 wpa_driver_nl80211_authenticate_retry(drv);
1888 return;
1889 }
1890
1891 os_memset(&event, 0, sizeof(event));
1892 info = &event.scan_info;
1893 info->aborted = aborted;
1894
1895 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1896 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1897 struct wpa_driver_scan_ssid *s =
1898 &info->ssids[info->num_ssids];
1899 s->ssid = nla_data(nl);
1900 s->ssid_len = nla_len(nl);
1901 info->num_ssids++;
1902 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1903 break;
1904 }
1905 }
1906 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1907 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1908 {
1909 freqs[num_freqs] = nla_get_u32(nl);
1910 num_freqs++;
1911 if (num_freqs == MAX_REPORT_FREQS - 1)
1912 break;
1913 }
1914 info->freqs = freqs;
1915 info->num_freqs = num_freqs;
1916 }
1917 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1918 }
1919
1920
1921 static int get_link_signal(struct nl_msg *msg, void *arg)
1922 {
1923 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1924 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1925 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1926 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1927 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1928 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
1929 };
1930 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1931 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1932 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1933 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1934 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1935 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1936 };
1937 struct wpa_signal_info *sig_change = arg;
1938
1939 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1940 genlmsg_attrlen(gnlh, 0), NULL);
1941 if (!tb[NL80211_ATTR_STA_INFO] ||
1942 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1943 tb[NL80211_ATTR_STA_INFO], policy))
1944 return NL_SKIP;
1945 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1946 return NL_SKIP;
1947
1948 sig_change->current_signal =
1949 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1950
1951 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1952 sig_change->avg_signal =
1953 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1954 else
1955 sig_change->avg_signal = 0;
1956
1957 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1958 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1959 sinfo[NL80211_STA_INFO_TX_BITRATE],
1960 rate_policy)) {
1961 sig_change->current_txrate = 0;
1962 } else {
1963 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1964 sig_change->current_txrate =
1965 nla_get_u16(rinfo[
1966 NL80211_RATE_INFO_BITRATE]) * 100;
1967 }
1968 }
1969 }
1970
1971 return NL_SKIP;
1972 }
1973
1974
1975 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1976 struct wpa_signal_info *sig)
1977 {
1978 struct nl_msg *msg;
1979
1980 sig->current_signal = -9999;
1981 sig->current_txrate = 0;
1982
1983 msg = nlmsg_alloc();
1984 if (!msg)
1985 return -ENOMEM;
1986
1987 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
1988
1989 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1990 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1991
1992 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1993 nla_put_failure:
1994 nlmsg_free(msg);
1995 return -ENOBUFS;
1996 }
1997
1998
1999 static int get_link_noise(struct nl_msg *msg, void *arg)
2000 {
2001 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2002 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2003 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2004 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2005 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2006 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2007 };
2008 struct wpa_signal_info *sig_change = arg;
2009
2010 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2011 genlmsg_attrlen(gnlh, 0), NULL);
2012
2013 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2014 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
2015 return NL_SKIP;
2016 }
2017
2018 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2019 tb[NL80211_ATTR_SURVEY_INFO],
2020 survey_policy)) {
2021 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
2022 "attributes!");
2023 return NL_SKIP;
2024 }
2025
2026 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2027 return NL_SKIP;
2028
2029 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2030 sig_change->frequency)
2031 return NL_SKIP;
2032
2033 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2034 return NL_SKIP;
2035
2036 sig_change->current_noise =
2037 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2038
2039 return NL_SKIP;
2040 }
2041
2042
2043 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
2044 struct wpa_signal_info *sig_change)
2045 {
2046 struct nl_msg *msg;
2047
2048 sig_change->current_noise = 9999;
2049 sig_change->frequency = drv->assoc_freq;
2050
2051 msg = nlmsg_alloc();
2052 if (!msg)
2053 return -ENOMEM;
2054
2055 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2056
2057 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2058
2059 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
2060 nla_put_failure:
2061 nlmsg_free(msg);
2062 return -ENOBUFS;
2063 }
2064
2065
2066 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
2067 {
2068 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2069 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2070 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
2071 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
2072 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
2073 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
2074 };
2075 struct wpa_scan_results *scan_results = arg;
2076 struct wpa_scan_res *scan_res;
2077 size_t i;
2078
2079 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2080 genlmsg_attrlen(gnlh, 0), NULL);
2081
2082 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
2083 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
2084 return NL_SKIP;
2085 }
2086
2087 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
2088 tb[NL80211_ATTR_SURVEY_INFO],
2089 survey_policy)) {
2090 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
2091 "attributes");
2092 return NL_SKIP;
2093 }
2094
2095 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
2096 return NL_SKIP;
2097
2098 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
2099 return NL_SKIP;
2100
2101 for (i = 0; i < scan_results->num; ++i) {
2102 scan_res = scan_results->res[i];
2103 if (!scan_res)
2104 continue;
2105 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
2106 scan_res->freq)
2107 continue;
2108 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
2109 continue;
2110 scan_res->noise = (s8)
2111 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
2112 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
2113 }
2114
2115 return NL_SKIP;
2116 }
2117
2118
2119 static int nl80211_get_noise_for_scan_results(
2120 struct wpa_driver_nl80211_data *drv,
2121 struct wpa_scan_results *scan_res)
2122 {
2123 struct nl_msg *msg;
2124
2125 msg = nlmsg_alloc();
2126 if (!msg)
2127 return -ENOMEM;
2128
2129 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
2130
2131 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2132
2133 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
2134 scan_res);
2135 nla_put_failure:
2136 nlmsg_free(msg);
2137 return -ENOBUFS;
2138 }
2139
2140
2141 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
2142 struct nlattr *tb[])
2143 {
2144 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
2145 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
2146 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
2147 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
2148 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
2149 };
2150 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
2151 enum nl80211_cqm_rssi_threshold_event event;
2152 union wpa_event_data ed;
2153 struct wpa_signal_info sig;
2154 int res;
2155
2156 if (tb[NL80211_ATTR_CQM] == NULL ||
2157 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
2158 cqm_policy)) {
2159 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
2160 return;
2161 }
2162
2163 os_memset(&ed, 0, sizeof(ed));
2164
2165 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
2166 if (!tb[NL80211_ATTR_MAC])
2167 return;
2168 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
2169 ETH_ALEN);
2170 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
2171 return;
2172 }
2173
2174 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
2175 return;
2176 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
2177
2178 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
2179 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2180 "event: RSSI high");
2181 ed.signal_change.above_threshold = 1;
2182 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
2183 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
2184 "event: RSSI low");
2185 ed.signal_change.above_threshold = 0;
2186 } else
2187 return;
2188
2189 res = nl80211_get_link_signal(drv, &sig);
2190 if (res == 0) {
2191 ed.signal_change.current_signal = sig.current_signal;
2192 ed.signal_change.current_txrate = sig.current_txrate;
2193 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
2194 sig.current_signal, sig.current_txrate);
2195 }
2196
2197 res = nl80211_get_link_noise(drv, &sig);
2198 if (res == 0) {
2199 ed.signal_change.current_noise = sig.current_noise;
2200 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
2201 sig.current_noise);
2202 }
2203
2204 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
2205 }
2206
2207
2208 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
2209 struct nlattr **tb)
2210 {
2211 u8 *addr;
2212 union wpa_event_data data;
2213
2214 if (tb[NL80211_ATTR_MAC] == NULL)
2215 return;
2216 addr = nla_data(tb[NL80211_ATTR_MAC]);
2217 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
2218
2219 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
2220 u8 *ies = NULL;
2221 size_t ies_len = 0;
2222 if (tb[NL80211_ATTR_IE]) {
2223 ies = nla_data(tb[NL80211_ATTR_IE]);
2224 ies_len = nla_len(tb[NL80211_ATTR_IE]);
2225 }
2226 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
2227 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
2228 return;
2229 }
2230
2231 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2232 return;
2233
2234 os_memset(&data, 0, sizeof(data));
2235 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
2236 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
2237 }
2238
2239
2240 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
2241 struct nlattr **tb)
2242 {
2243 u8 *addr;
2244 union wpa_event_data data;
2245
2246 if (tb[NL80211_ATTR_MAC] == NULL)
2247 return;
2248 addr = nla_data(tb[NL80211_ATTR_MAC]);
2249 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
2250 MAC2STR(addr));
2251
2252 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
2253 drv_event_disassoc(drv->ctx, addr);
2254 return;
2255 }
2256
2257 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
2258 return;
2259
2260 os_memset(&data, 0, sizeof(data));
2261 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2262 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2263 }
2264
2265
2266 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2267 struct nlattr **tb)
2268 {
2269 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2270 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2271 [NL80211_REKEY_DATA_KEK] = {
2272 .minlen = NL80211_KEK_LEN,
2273 .maxlen = NL80211_KEK_LEN,
2274 },
2275 [NL80211_REKEY_DATA_KCK] = {
2276 .minlen = NL80211_KCK_LEN,
2277 .maxlen = NL80211_KCK_LEN,
2278 },
2279 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2280 .minlen = NL80211_REPLAY_CTR_LEN,
2281 .maxlen = NL80211_REPLAY_CTR_LEN,
2282 },
2283 };
2284 union wpa_event_data data;
2285
2286 if (!tb[NL80211_ATTR_MAC])
2287 return;
2288 if (!tb[NL80211_ATTR_REKEY_DATA])
2289 return;
2290 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2291 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2292 return;
2293 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2294 return;
2295
2296 os_memset(&data, 0, sizeof(data));
2297 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2298 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2299 MAC2STR(data.driver_gtk_rekey.bssid));
2300 data.driver_gtk_rekey.replay_ctr =
2301 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2302 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2303 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2304 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2305 }
2306
2307
2308 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2309 struct nlattr **tb)
2310 {
2311 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2312 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2313 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2314 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2315 .minlen = ETH_ALEN,
2316 .maxlen = ETH_ALEN,
2317 },
2318 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2319 };
2320 union wpa_event_data data;
2321
2322 wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
2323
2324 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2325 return;
2326 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2327 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2328 return;
2329 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2330 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2331 return;
2332
2333 os_memset(&data, 0, sizeof(data));
2334 os_memcpy(data.pmkid_candidate.bssid,
2335 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2336 data.pmkid_candidate.index =
2337 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2338 data.pmkid_candidate.preauth =
2339 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2340 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2341 }
2342
2343
2344 static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2345 struct nlattr **tb)
2346 {
2347 union wpa_event_data data;
2348
2349 wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
2350
2351 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2352 return;
2353
2354 os_memset(&data, 0, sizeof(data));
2355 os_memcpy(data.client_poll.addr,
2356 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2357
2358 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2359 }
2360
2361
2362 static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
2363 struct nlattr **tb)
2364 {
2365 union wpa_event_data data;
2366
2367 wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
2368
2369 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
2370 return;
2371
2372 os_memset(&data, 0, sizeof(data));
2373 os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2374 switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
2375 case NL80211_TDLS_SETUP:
2376 wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
2377 MACSTR, MAC2STR(data.tdls.peer));
2378 data.tdls.oper = TDLS_REQUEST_SETUP;
2379 break;
2380 case NL80211_TDLS_TEARDOWN:
2381 wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
2382 MACSTR, MAC2STR(data.tdls.peer));
2383 data.tdls.oper = TDLS_REQUEST_TEARDOWN;
2384 break;
2385 default:
2386 wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
2387 "event");
2388 return;
2389 }
2390 if (tb[NL80211_ATTR_REASON_CODE]) {
2391 data.tdls.reason_code =
2392 nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
2393 }
2394
2395 wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
2396 }
2397
2398
2399 static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
2400 struct nlattr **tb)
2401 {
2402 union wpa_event_data data;
2403 u32 reason;
2404
2405 wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
2406
2407 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
2408 return;
2409
2410 os_memset(&data, 0, sizeof(data));
2411 os_memcpy(data.connect_failed_reason.addr,
2412 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2413
2414 reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
2415 switch (reason) {
2416 case NL80211_CONN_FAIL_MAX_CLIENTS:
2417 wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
2418 data.connect_failed_reason.code = MAX_CLIENT_REACHED;
2419 break;
2420 case NL80211_CONN_FAIL_BLOCKED_CLIENT:
2421 wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
2422 " tried to connect",
2423 MAC2STR(data.connect_failed_reason.addr));
2424 data.connect_failed_reason.code = BLOCKED_CLIENT;
2425 break;
2426 default:
2427 wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
2428 "%u", reason);
2429 return;
2430 }
2431
2432 wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
2433 }
2434
2435
2436 static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
2437 struct nlattr **tb)
2438 {
2439 union wpa_event_data data;
2440 enum nl80211_radar_event event_type;
2441
2442 if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
2443 return;
2444
2445 os_memset(&data, 0, sizeof(data));
2446 data.dfs_event.freq = nla_get_u16(tb[NL80211_ATTR_WIPHY_FREQ]);
2447 event_type = nla_get_u8(tb[NL80211_ATTR_RADAR_EVENT]);
2448
2449 wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz",
2450 data.dfs_event.freq);
2451
2452 switch (event_type) {
2453 case NL80211_RADAR_DETECTED:
2454 wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2455 break;
2456 case NL80211_RADAR_CAC_FINISHED:
2457 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2458 break;
2459 case NL80211_RADAR_CAC_ABORTED:
2460 wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2461 break;
2462 case NL80211_RADAR_NOP_FINISHED:
2463 wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2464 break;
2465 default:
2466 wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
2467 "received", event_type);
2468 break;
2469 }
2470 }
2471
2472
2473 static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2474 int wds)
2475 {
2476 struct wpa_driver_nl80211_data *drv = bss->drv;
2477 union wpa_event_data event;
2478
2479 if (!tb[NL80211_ATTR_MAC])
2480 return;
2481
2482 os_memset(&event, 0, sizeof(event));
2483 event.rx_from_unknown.bssid = bss->addr;
2484 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2485 event.rx_from_unknown.wds = wds;
2486
2487 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2488 }
2489
2490
2491 static void do_process_drv_event(struct i802_bss *bss, int cmd,
2492 struct nlattr **tb)
2493 {
2494 struct wpa_driver_nl80211_data *drv = bss->drv;
2495
2496 wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2497 cmd, nl80211_command_to_string(cmd), bss->ifname);
2498
2499 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2500 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2501 cmd == NL80211_CMD_SCAN_ABORTED)) {
2502 wpa_driver_nl80211_set_mode(&drv->first_bss,
2503 drv->ap_scan_as_station);
2504 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2505 }
2506
2507 switch (cmd) {
2508 case NL80211_CMD_TRIGGER_SCAN:
2509 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
2510 break;
2511 case NL80211_CMD_START_SCHED_SCAN:
2512 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
2513 break;
2514 case NL80211_CMD_SCHED_SCAN_STOPPED:
2515 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
2516 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2517 break;
2518 case NL80211_CMD_NEW_SCAN_RESULTS:
2519 wpa_dbg(drv->ctx, MSG_DEBUG,
2520 "nl80211: New scan results available");
2521 drv->scan_complete_events = 1;
2522 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2523 drv->ctx);
2524 send_scan_event(drv, 0, tb);
2525 break;
2526 case NL80211_CMD_SCHED_SCAN_RESULTS:
2527 wpa_dbg(drv->ctx, MSG_DEBUG,
2528 "nl80211: New sched scan results available");
2529 send_scan_event(drv, 0, tb);
2530 break;
2531 case NL80211_CMD_SCAN_ABORTED:
2532 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
2533 /*
2534 * Need to indicate that scan results are available in order
2535 * not to make wpa_supplicant stop its scanning.
2536 */
2537 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2538 drv->ctx);
2539 send_scan_event(drv, 1, tb);
2540 break;
2541 case NL80211_CMD_AUTHENTICATE:
2542 case NL80211_CMD_ASSOCIATE:
2543 case NL80211_CMD_DEAUTHENTICATE:
2544 case NL80211_CMD_DISASSOCIATE:
2545 case NL80211_CMD_FRAME_TX_STATUS:
2546 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2547 case NL80211_CMD_UNPROT_DISASSOCIATE:
2548 mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
2549 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2550 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2551 tb[NL80211_ATTR_COOKIE],
2552 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
2553 break;
2554 case NL80211_CMD_CONNECT:
2555 case NL80211_CMD_ROAM:
2556 mlme_event_connect(drv, cmd,
2557 tb[NL80211_ATTR_STATUS_CODE],
2558 tb[NL80211_ATTR_MAC],
2559 tb[NL80211_ATTR_REQ_IE],
2560 tb[NL80211_ATTR_RESP_IE]);
2561 break;
2562 case NL80211_CMD_CH_SWITCH_NOTIFY:
2563 mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
2564 tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2565 break;
2566 case NL80211_CMD_DISCONNECT:
2567 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2568 tb[NL80211_ATTR_MAC],
2569 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
2570 break;
2571 case NL80211_CMD_MICHAEL_MIC_FAILURE:
2572 mlme_event_michael_mic_failure(bss, tb);
2573 break;
2574 case NL80211_CMD_JOIN_IBSS:
2575 mlme_event_join_ibss(drv, tb);
2576 break;
2577 case NL80211_CMD_REMAIN_ON_CHANNEL:
2578 mlme_event_remain_on_channel(drv, 0, tb);
2579 break;
2580 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2581 mlme_event_remain_on_channel(drv, 1, tb);
2582 break;
2583 case NL80211_CMD_NOTIFY_CQM:
2584 nl80211_cqm_event(drv, tb);
2585 break;
2586 case NL80211_CMD_REG_CHANGE:
2587 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2588 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2589 NULL);
2590 break;
2591 case NL80211_CMD_REG_BEACON_HINT:
2592 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2593 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2594 NULL);
2595 break;
2596 case NL80211_CMD_NEW_STATION:
2597 nl80211_new_station_event(drv, tb);
2598 break;
2599 case NL80211_CMD_DEL_STATION:
2600 nl80211_del_station_event(drv, tb);
2601 break;
2602 case NL80211_CMD_SET_REKEY_OFFLOAD:
2603 nl80211_rekey_offload_event(drv, tb);
2604 break;
2605 case NL80211_CMD_PMKSA_CANDIDATE:
2606 nl80211_pmksa_candidate_event(drv, tb);
2607 break;
2608 case NL80211_CMD_PROBE_CLIENT:
2609 nl80211_client_probe_event(drv, tb);
2610 break;
2611 case NL80211_CMD_TDLS_OPER:
2612 nl80211_tdls_oper_event(drv, tb);
2613 break;
2614 case NL80211_CMD_CONN_FAILED:
2615 nl80211_connect_failed_event(drv, tb);
2616 break;
2617 case NL80211_CMD_FT_EVENT:
2618 mlme_event_ft_event(drv, tb);
2619 break;
2620 case NL80211_CMD_RADAR_DETECT:
2621 nl80211_radar_event(drv, tb);
2622 break;
2623 default:
2624 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
2625 "(cmd=%d)", cmd);
2626 break;
2627 }
2628 }
2629
2630
2631 static int process_drv_event(struct nl_msg *msg, void *arg)
2632 {
2633 struct wpa_driver_nl80211_data *drv = arg;
2634 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2635 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2636 struct i802_bss *bss;
2637 int ifidx = -1;
2638
2639 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2640 genlmsg_attrlen(gnlh, 0), NULL);
2641
2642 if (tb[NL80211_ATTR_IFINDEX]) {
2643 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2644
2645 for (bss = &drv->first_bss; bss; bss = bss->next)
2646 if (ifidx == -1 || ifidx == bss->ifindex) {
2647 do_process_drv_event(bss, gnlh->cmd, tb);
2648 return NL_SKIP;
2649 }
2650 wpa_printf(MSG_DEBUG,
2651 "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
2652 gnlh->cmd, ifidx);
2653 } else if (tb[NL80211_ATTR_WDEV]) {
2654 u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2655 wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
2656 for (bss = &drv->first_bss; bss; bss = bss->next) {
2657 if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
2658 do_process_drv_event(bss, gnlh->cmd, tb);
2659 return NL_SKIP;
2660 }
2661 }
2662 wpa_printf(MSG_DEBUG,
2663 "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
2664 gnlh->cmd, (long long unsigned int) wdev_id);
2665 }
2666
2667 return NL_SKIP;
2668 }
2669
2670
2671 static int process_global_event(struct nl_msg *msg, void *arg)
2672 {
2673 struct nl80211_global *global = arg;
2674 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2675 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2676 struct wpa_driver_nl80211_data *drv, *tmp;
2677 int ifidx = -1;
2678 struct i802_bss *bss;
2679 u64 wdev_id;
2680 int wdev_id_set = 0;
2681
2682 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2683 genlmsg_attrlen(gnlh, 0), NULL);
2684
2685 if (tb[NL80211_ATTR_IFINDEX])
2686 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2687 else if (tb[NL80211_ATTR_WDEV]) {
2688 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
2689 wdev_id_set = 1;
2690 }
2691
2692 dl_list_for_each_safe(drv, tmp, &global->interfaces,
2693 struct wpa_driver_nl80211_data, list) {
2694 for (bss = &drv->first_bss; bss; bss = bss->next) {
2695 if ((ifidx == -1 && !wdev_id_set) ||
2696 ifidx == bss->ifindex ||
2697 (wdev_id_set && bss->wdev_id_set &&
2698 wdev_id == bss->wdev_id)) {
2699 do_process_drv_event(bss, gnlh->cmd, tb);
2700 return NL_SKIP;
2701 }
2702 }
2703 }
2704
2705 return NL_SKIP;
2706 }
2707
2708
2709 static int process_bss_event(struct nl_msg *msg, void *arg)
2710 {
2711 struct i802_bss *bss = arg;
2712 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2713 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2714
2715 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2716 genlmsg_attrlen(gnlh, 0), NULL);
2717
2718 wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
2719 gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
2720 bss->ifname);
2721
2722 switch (gnlh->cmd) {
2723 case NL80211_CMD_FRAME:
2724 case NL80211_CMD_FRAME_TX_STATUS:
2725 mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2726 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2727 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2728 tb[NL80211_ATTR_COOKIE],
2729 tb[NL80211_ATTR_RX_SIGNAL_DBM]);
2730 break;
2731 case NL80211_CMD_UNEXPECTED_FRAME:
2732 nl80211_spurious_frame(bss, tb, 0);
2733 break;
2734 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2735 nl80211_spurious_frame(bss, tb, 1);
2736 break;
2737 default:
2738 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2739 "(cmd=%d)", gnlh->cmd);
2740 break;
2741 }
2742
2743 return NL_SKIP;
2744 }
2745
2746
2747 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2748 void *handle)
2749 {
2750 struct nl_cb *cb = eloop_ctx;
2751
2752 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
2753
2754 nl_recvmsgs(handle, cb);
2755 }
2756
2757
2758 /**
2759 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2760 * @priv: driver_nl80211 private data
2761 * @alpha2_arg: country to which to switch to
2762 * Returns: 0 on success, -1 on failure
2763 *
2764 * This asks nl80211 to set the regulatory domain for given
2765 * country ISO / IEC alpha2.
2766 */
2767 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2768 {
2769 struct i802_bss *bss = priv;
2770 struct wpa_driver_nl80211_data *drv = bss->drv;
2771 char alpha2[3];
2772 struct nl_msg *msg;
2773
2774 msg = nlmsg_alloc();
2775 if (!msg)
2776 return -ENOMEM;
2777
2778 alpha2[0] = alpha2_arg[0];
2779 alpha2[1] = alpha2_arg[1];
2780 alpha2[2] = '\0';
2781
2782 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
2783
2784 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2785 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2786 return -EINVAL;
2787 return 0;
2788 nla_put_failure:
2789 nlmsg_free(msg);
2790 return -EINVAL;
2791 }
2792
2793
2794 static int protocol_feature_handler(struct nl_msg *msg, void *arg)
2795 {
2796 u32 *feat = arg;
2797 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2798 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2799
2800 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2801 genlmsg_attrlen(gnlh, 0), NULL);
2802
2803 if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
2804 *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
2805
2806 return NL_SKIP;
2807 }
2808
2809
2810 static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
2811 {
2812 u32 feat = 0;
2813 struct nl_msg *msg;
2814
2815 msg = nlmsg_alloc();
2816 if (!msg)
2817 goto nla_put_failure;
2818
2819 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
2820 if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
2821 return feat;
2822
2823 msg = NULL;
2824 nla_put_failure:
2825 nlmsg_free(msg);
2826 return 0;
2827 }
2828
2829
2830 struct wiphy_info_data {
2831 struct wpa_driver_nl80211_data *drv;
2832 struct wpa_driver_capa *capa;
2833
2834 unsigned int error:1;
2835 unsigned int device_ap_sme:1;
2836 unsigned int poll_command_supported:1;
2837 unsigned int data_tx_status:1;
2838 unsigned int monitor_supported:1;
2839 unsigned int auth_supported:1;
2840 unsigned int connect_supported:1;
2841 unsigned int p2p_go_supported:1;
2842 unsigned int p2p_client_supported:1;
2843 unsigned int p2p_concurrent:1;
2844 unsigned int p2p_multichan_concurrent:1;
2845 };
2846
2847
2848 static unsigned int probe_resp_offload_support(int supp_protocols)
2849 {
2850 unsigned int prot = 0;
2851
2852 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2853 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2854 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2855 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2856 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2857 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2858 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2859 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2860
2861 return prot;
2862 }
2863
2864
2865 static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
2866 struct nlattr *tb)
2867 {
2868 struct nlattr *nl_mode;
2869 int i;
2870
2871 if (tb == NULL)
2872 return;
2873
2874 nla_for_each_nested(nl_mode, tb, i) {
2875 switch (nla_type(nl_mode)) {
2876 case NL80211_IFTYPE_AP:
2877 info->capa->flags |= WPA_DRIVER_FLAGS_AP;
2878 break;
2879 case NL80211_IFTYPE_ADHOC:
2880 info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
2881 break;
2882 case NL80211_IFTYPE_P2P_DEVICE:
2883 info->capa->flags |=
2884 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
2885 break;
2886 case NL80211_IFTYPE_P2P_GO:
2887 info->p2p_go_supported = 1;
2888 break;
2889 case NL80211_IFTYPE_P2P_CLIENT:
2890 info->p2p_client_supported = 1;
2891 break;
2892 case NL80211_IFTYPE_MONITOR:
2893 info->monitor_supported = 1;
2894 break;
2895 }
2896 }
2897 }
2898
2899
2900 static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
2901 struct nlattr *nl_combi)
2902 {
2903 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2904 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2905 struct nlattr *nl_limit, *nl_mode;
2906 int err, rem_limit, rem_mode;
2907 int combination_has_p2p = 0, combination_has_mgd = 0;
2908 static struct nla_policy
2909 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2910 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2911 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2912 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2913 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2914 [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
2915 },
2916 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2917 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2918 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2919 };
2920
2921 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2922 nl_combi, iface_combination_policy);
2923 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2924 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2925 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2926 return 0; /* broken combination */
2927
2928 if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
2929 info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
2930
2931 nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
2932 rem_limit) {
2933 err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
2934 nl_limit, iface_limit_policy);
2935 if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2936 return 0; /* broken combination */
2937
2938 nla_for_each_nested(nl_mode,
2939 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2940 rem_mode) {
2941 int ift = nla_type(nl_mode);
2942 if (ift == NL80211_IFTYPE_P2P_GO ||
2943 ift == NL80211_IFTYPE_P2P_CLIENT)
2944 combination_has_p2p = 1;
2945 if (ift == NL80211_IFTYPE_STATION)
2946 combination_has_mgd = 1;
2947 }
2948 if (combination_has_p2p && combination_has_mgd)
2949 break;
2950 }
2951
2952 if (combination_has_p2p && combination_has_mgd) {
2953 info->p2p_concurrent = 1;
2954 if (nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]) > 1)
2955 info->p2p_multichan_concurrent = 1;
2956 return 1;
2957 }
2958
2959 return 0;
2960 }
2961
2962
2963 static void wiphy_info_iface_comb(struct wiphy_info_data *info,
2964 struct nlattr *tb)
2965 {
2966 struct nlattr *nl_combi;
2967 int rem_combi;
2968
2969 if (tb == NULL)
2970 return;
2971
2972 nla_for_each_nested(nl_combi, tb, rem_combi) {
2973 if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
2974 break;
2975 }
2976 }
2977
2978
2979 static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
2980 struct nlattr *tb)
2981 {
2982 struct nlattr *nl_cmd;
2983 int i;
2984
2985 if (tb == NULL)
2986 return;
2987
2988 nla_for_each_nested(nl_cmd, tb, i) {
2989 switch (nla_get_u32(nl_cmd)) {
2990 case NL80211_CMD_AUTHENTICATE:
2991 info->auth_supported = 1;
2992 break;
2993 case NL80211_CMD_CONNECT:
2994 info->connect_supported = 1;
2995 break;
2996 case NL80211_CMD_START_SCHED_SCAN:
2997 info->capa->sched_scan_supported = 1;
2998 break;
2999 case NL80211_CMD_PROBE_CLIENT:
3000 info->poll_command_supported = 1;
3001 break;
3002 }
3003 }
3004 }
3005
3006
3007 static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
3008 struct nlattr *tb)
3009 {
3010 if (tb)
3011 capa->max_remain_on_chan = nla_get_u32(tb);
3012 }
3013
3014
3015 static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
3016 struct nlattr *ext_setup)
3017 {
3018 if (tdls == NULL)
3019 return;
3020
3021 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
3022 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
3023
3024 if (ext_setup) {
3025 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
3026 capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
3027 }
3028 }
3029
3030
3031 static void wiphy_info_feature_flags(struct wiphy_info_data *info,
3032 struct nlattr *tb)
3033 {
3034 u32 flags;
3035 struct wpa_driver_capa *capa = info->capa;
3036
3037 if (tb == NULL)
3038 return;
3039
3040 flags = nla_get_u32(tb);
3041
3042 if (flags & NL80211_FEATURE_SK_TX_STATUS)
3043 info->data_tx_status = 1;
3044
3045 if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
3046 capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
3047
3048 if (flags & NL80211_FEATURE_SAE)
3049 capa->flags |= WPA_DRIVER_FLAGS_SAE;
3050
3051 if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
3052 capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
3053 }
3054
3055
3056 static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
3057 struct nlattr *tb)
3058 {
3059 u32 protocols;
3060
3061 if (tb == NULL)
3062 return;
3063
3064 protocols = nla_get_u32(tb);
3065 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
3066 "mode");
3067 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
3068 capa->probe_resp_offloads = probe_resp_offload_support(protocols);
3069 }
3070
3071
3072 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
3073 {
3074 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3075 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3076 struct wiphy_info_data *info = arg;
3077 struct wpa_driver_capa *capa = info->capa;
3078 struct wpa_driver_nl80211_data *drv = info->drv;
3079
3080 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3081 genlmsg_attrlen(gnlh, 0), NULL);
3082
3083 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
3084 capa->max_scan_ssids =
3085 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
3086
3087 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
3088 capa->max_sched_scan_ssids =
3089 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
3090
3091 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
3092 capa->max_match_sets =
3093 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
3094
3095 if (tb[NL80211_ATTR_MAC_ACL_MAX])
3096 capa->max_acl_mac_addrs =
3097 nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
3098
3099 wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
3100 wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
3101 wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
3102
3103 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
3104 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
3105 "off-channel TX");
3106 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
3107 }
3108
3109 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
3110 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
3111 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
3112 }
3113
3114 wiphy_info_max_roc(capa,
3115 tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
3116
3117 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
3118 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
3119
3120 wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
3121 tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
3122
3123 if (tb[NL80211_ATTR_DEVICE_AP_SME])
3124 info->device_ap_sme = 1;
3125
3126 wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
3127 wiphy_info_probe_resp_offload(capa,
3128 tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
3129
3130 if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
3131 drv->extended_capa == NULL) {
3132 drv->extended_capa =
3133 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3134 if (drv->extended_capa) {
3135 os_memcpy(drv->extended_capa,
3136 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3137 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3138 drv->extended_capa_len =
3139 nla_len(tb[NL80211_ATTR_EXT_CAPA]);
3140 }
3141 drv->extended_capa_mask =
3142 os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3143 if (drv->extended_capa_mask) {
3144 os_memcpy(drv->extended_capa_mask,
3145 nla_data(tb[NL80211_ATTR_EXT_CAPA]),
3146 nla_len(tb[NL80211_ATTR_EXT_CAPA]));
3147 } else {
3148 os_free(drv->extended_capa);
3149 drv->extended_capa = NULL;
3150 drv->extended_capa_len = 0;
3151 }
3152 }
3153
3154 return NL_SKIP;
3155 }
3156
3157
3158 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
3159 struct wiphy_info_data *info)
3160 {
3161 u32 feat;
3162 struct nl_msg *msg;
3163
3164 os_memset(info, 0, sizeof(*info));
3165 info->capa = &drv->capa;
3166 info->drv = drv;
3167
3168 msg = nlmsg_alloc();
3169 if (!msg)
3170 return -1;
3171
3172 feat = get_nl80211_protocol_features(drv);
3173 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
3174 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
3175 else
3176 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
3177
3178 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
3179 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
3180
3181 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
3182 return -1;
3183
3184 if (info->auth_supported)
3185 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
3186 else if (!info->connect_supported) {
3187 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
3188 "authentication/association or connect commands");
3189 info->error = 1;
3190 }
3191
3192 if (info->p2p_go_supported && info->p2p_client_supported)
3193 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
3194 if (info->p2p_concurrent) {
3195 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
3196 "interface (driver advertised support)");
3197 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
3198 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
3199 }
3200 if (info->p2p_multichan_concurrent) {
3201 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
3202 "concurrent (driver advertised support)");
3203 drv->capa.flags |= WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT;
3204 }
3205
3206 /* default to 5000 since early versions of mac80211 don't set it */
3207 if (!drv->capa.max_remain_on_chan)
3208 drv->capa.max_remain_on_chan = 5000;
3209
3210 return 0;
3211 nla_put_failure:
3212 nlmsg_free(msg);
3213 return -1;
3214 }
3215
3216
3217 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
3218 {
3219 struct wiphy_info_data info;
3220 if (wpa_driver_nl80211_get_info(drv, &info))
3221 return -1;
3222
3223 if (info.error)
3224 return -1;
3225
3226 drv->has_capability = 1;
3227 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
3228 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3229 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3230 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3231 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
3232 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
3233 WPA_DRIVER_CAPA_ENC_WEP104 |
3234 WPA_DRIVER_CAPA_ENC_TKIP |
3235 WPA_DRIVER_CAPA_ENC_CCMP;
3236 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
3237 WPA_DRIVER_AUTH_SHARED |
3238 WPA_DRIVER_AUTH_LEAP;
3239
3240 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
3241 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
3242 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3243
3244 if (!info.device_ap_sme) {
3245 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
3246
3247 /*
3248 * No AP SME is currently assumed to also indicate no AP MLME
3249 * in the driver/firmware.
3250 */
3251 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
3252 }
3253
3254 drv->device_ap_sme = info.device_ap_sme;
3255 drv->poll_command_supported = info.poll_command_supported;
3256 drv->data_tx_status = info.data_tx_status;
3257
3258 /*
3259 * If poll command and tx status are supported, mac80211 is new enough
3260 * to have everything we need to not need monitor interfaces.
3261 */
3262 drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
3263
3264 if (drv->device_ap_sme && drv->use_monitor) {
3265 /*
3266 * Non-mac80211 drivers may not support monitor interface.
3267 * Make sure we do not get stuck with incorrect capability here
3268 * by explicitly testing this.
3269 */
3270 if (!info.monitor_supported) {
3271 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
3272 "with device_ap_sme since no monitor mode "
3273 "support detected");
3274 drv->use_monitor = 0;
3275 }
3276 }
3277
3278 /*
3279 * If we aren't going to use monitor interfaces, but the
3280 * driver doesn't support data TX status, we won't get TX
3281 * status for EAPOL frames.
3282 */
3283 if (!drv->use_monitor && !info.data_tx_status)
3284 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3285
3286 return 0;
3287 }
3288
3289
3290 #ifdef ANDROID
3291 static int android_genl_ctrl_resolve(struct nl_handle *handle,
3292 const char *name)
3293 {
3294 /*
3295 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
3296 * need to work around that.
3297 */
3298 struct nl_cache *cache = NULL;
3299 struct genl_family *nl80211 = NULL;
3300 int id = -1;
3301
3302 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
3303 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
3304 "netlink cache");
3305 goto fail;
3306 }
3307
3308 nl80211 = genl_ctrl_search_by_name(cache, name);
3309 if (nl80211 == NULL)
3310 goto fail;
3311
3312 id = genl_family_get_id(nl80211);
3313
3314 fail:
3315 if (nl80211)
3316 genl_family_put(nl80211);
3317 if (cache)
3318 nl_cache_free(cache);
3319
3320 return id;
3321 }
3322 #define genl_ctrl_resolve android_genl_ctrl_resolve
3323 #endif /* ANDROID */
3324
3325
3326 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
3327 {
3328 int ret;
3329
3330 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3331 if (global->nl_cb == NULL) {
3332 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
3333 "callbacks");
3334 return -1;
3335 }
3336
3337 global->nl = nl_create_handle(global->nl_cb, "nl");
3338 if (global->nl == NULL)
3339 goto err;
3340
3341 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
3342 if (global->nl80211_id < 0) {
3343 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
3344 "found");
3345 goto err;
3346 }
3347
3348 global->nl_event = nl_create_handle(global->nl_cb, "event");
3349 if (global->nl_event == NULL)
3350 goto err;
3351
3352 ret = nl_get_multicast_id(global, "nl80211", "scan");
3353 if (ret >= 0)
3354 ret = nl_socket_add_membership(global->nl_event, ret);
3355 if (ret < 0) {
3356 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3357 "membership for scan events: %d (%s)",
3358 ret, strerror(-ret));
3359 goto err;
3360 }
3361
3362 ret = nl_get_multicast_id(global, "nl80211", "mlme");
3363 if (ret >= 0)
3364 ret = nl_socket_add_membership(global->nl_event, ret);
3365 if (ret < 0) {
3366 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
3367 "membership for mlme events: %d (%s)",
3368 ret, strerror(-ret));
3369 goto err;
3370 }
3371
3372 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
3373 if (ret >= 0)
3374 ret = nl_socket_add_membership(global->nl_event, ret);
3375 if (ret < 0) {
3376 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
3377 "membership for regulatory events: %d (%s)",
3378 ret, strerror(-ret));
3379 /* Continue without regulatory events */
3380 }
3381
3382 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3383 no_seq_check, NULL);
3384 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3385 process_global_event, global);
3386
3387 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
3388 wpa_driver_nl80211_event_receive,
3389 global->nl_cb, global->nl_event);
3390
3391 return 0;
3392
3393 err:
3394 nl_destroy_handles(&global->nl_event);
3395 nl_destroy_handles(&global->nl);
3396 nl_cb_put(global->nl_cb);
3397 global->nl_cb = NULL;
3398 return -1;
3399 }
3400
3401
3402 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
3403 {
3404 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3405 if (!drv->nl_cb) {
3406 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
3407 return -1;
3408 }
3409
3410 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3411 no_seq_check, NULL);
3412 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3413 process_drv_event, drv);
3414
3415 return 0;
3416 }
3417
3418
3419 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
3420 {
3421 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
3422 /*
3423 * This may be for any interface; use ifdown event to disable
3424 * interface.
3425 */
3426 }
3427
3428
3429 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
3430 {
3431 struct wpa_driver_nl80211_data *drv = ctx;
3432 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
3433 if (linux_set_iface_flags(drv->global->ioctl_sock,
3434 drv->first_bss.ifname, 1)) {
3435 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
3436 "after rfkill unblock");
3437 return;
3438 }
3439 /* rtnetlink ifup handler will report interface as enabled */
3440 }
3441
3442
3443 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
3444 {
3445 /* Find phy (radio) to which this interface belongs */
3446 char buf[90], *pos;
3447 int f, rv;
3448
3449 drv->phyname[0] = '\0';
3450 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
3451 drv->first_bss.ifname);
3452 f = open(buf, O_RDONLY);
3453 if (f < 0) {
3454 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
3455 buf, strerror(errno));
3456 return;
3457 }
3458
3459 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
3460 close(f);
3461 if (rv < 0) {
3462 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
3463 buf, strerror(errno));
3464 return;
3465 }
3466
3467 drv->phyname[rv] = '\0';
3468 pos = os_strchr(drv->phyname, '\n');
3469 if (pos)
3470 *pos = '\0';
3471 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
3472 drv->first_bss.ifname, drv->phyname);
3473 }
3474
3475
3476 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
3477 void *eloop_ctx,
3478 void *handle)
3479 {
3480 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3481 u8 data[2048];
3482 struct msghdr msg;
3483 struct iovec entry;
3484 u8 control[512];
3485 struct cmsghdr *cmsg;
3486 int res, found_ee = 0, found_wifi = 0, acked = 0;
3487 union wpa_event_data event;
3488
3489 memset(&msg, 0, sizeof(msg));
3490 msg.msg_iov = &entry;
3491 msg.msg_iovlen = 1;
3492 entry.iov_base = data;
3493 entry.iov_len = sizeof(data);
3494 msg.msg_control = &control;
3495 msg.msg_controllen = sizeof(control);
3496
3497 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
3498 /* if error or not fitting 802.3 header, return */
3499 if (res < 14)
3500 return;
3501
3502 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
3503 {
3504 if (cmsg->cmsg_level == SOL_SOCKET &&
3505 cmsg->cmsg_type == SCM_WIFI_STATUS) {
3506 int *ack;
3507
3508 found_wifi = 1;
3509 ack = (void *)CMSG_DATA(cmsg);
3510 acked = *ack;
3511 }
3512
3513 if (cmsg->cmsg_level == SOL_PACKET &&
3514 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
3515 struct sock_extended_err *err =
3516 (struct sock_extended_err *)CMSG_DATA(cmsg);
3517
3518 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
3519 found_ee = 1;
3520 }
3521 }
3522
3523 if (!found_ee || !found_wifi)
3524 return;
3525
3526 memset(&event, 0, sizeof(event));
3527 event.eapol_tx_status.dst = data;
3528 event.eapol_tx_status.data = data + 14;
3529 event.eapol_tx_status.data_len = res - 14;
3530 event.eapol_tx_status.ack = acked;
3531 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
3532 }
3533
3534
3535 static int nl80211_init_bss(struct i802_bss *bss)
3536 {
3537 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
3538 if (!bss->nl_cb)
3539 return -1;
3540
3541 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
3542 no_seq_check, NULL);
3543 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
3544 process_bss_event, bss);
3545
3546 return 0;
3547 }
3548
3549
3550 static void nl80211_destroy_bss(struct i802_bss *bss)
3551 {
3552 nl_cb_put(bss->nl_cb);
3553 bss->nl_cb = NULL;
3554 }
3555
3556
3557 /**
3558 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
3559 * @ctx: context to be used when calling wpa_supplicant functions,
3560 * e.g., wpa_supplicant_event()
3561 * @ifname: interface name, e.g., wlan0
3562 * @global_priv: private driver global data from global_init()
3563 * Returns: Pointer to private data, %NULL on failure
3564 */
3565 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
3566 void *global_priv)
3567 {
3568 struct wpa_driver_nl80211_data *drv;
3569 struct rfkill_config *rcfg;
3570 struct i802_bss *bss;
3571
3572 if (global_priv == NULL)
3573 return NULL;
3574 drv = os_zalloc(sizeof(*drv));
3575 if (drv == NULL)
3576 return NULL;
3577 drv->global = global_priv;
3578 drv->ctx = ctx;
3579 bss = &drv->first_bss;
3580 bss->drv = drv;
3581 bss->ctx = ctx;
3582
3583 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
3584 drv->monitor_ifidx = -1;
3585 drv->monitor_sock = -1;
3586 drv->eapol_tx_sock = -1;
3587 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
3588
3589 if (wpa_driver_nl80211_init_nl(drv)) {
3590 os_free(drv);
3591 return NULL;
3592 }
3593
3594 if (nl80211_init_bss(bss))
3595 goto failed;
3596
3597 nl80211_get_phy_name(drv);
3598
3599 rcfg = os_zalloc(sizeof(*rcfg));
3600 if (rcfg == NULL)
3601 goto failed;
3602 rcfg->ctx = drv;
3603 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3604 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3605 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3606 drv->rfkill = rfkill_init(rcfg);
3607 if (drv->rfkill == NULL) {
3608 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3609 os_free(rcfg);
3610 }
3611
3612 if (wpa_driver_nl80211_finish_drv_init(drv))
3613 goto failed;
3614
3615 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3616 if (drv->eapol_tx_sock < 0)
3617 goto failed;
3618
3619 if (drv->data_tx_status) {
3620 int enabled = 1;
3621
3622 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3623 &enabled, sizeof(enabled)) < 0) {
3624 wpa_printf(MSG_DEBUG,
3625 "nl80211: wifi status sockopt failed\n");
3626 drv->data_tx_status = 0;
3627 if (!drv->use_monitor)
3628 drv->capa.flags &=
3629 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3630 } else {
3631 eloop_register_read_sock(drv->eapol_tx_sock,
3632 wpa_driver_nl80211_handle_eapol_tx_status,
3633 drv, NULL);
3634 }
3635 }
3636
3637 if (drv->global) {
3638 dl_list_add(&drv->global->interfaces, &drv->list);
3639 drv->in_interface_list = 1;
3640 }
3641
3642 return bss;
3643
3644 failed:
3645 wpa_driver_nl80211_deinit(bss);
3646 return NULL;
3647 }
3648
3649
3650 static int nl80211_register_frame(struct i802_bss *bss,
3651 struct nl_handle *nl_handle,
3652 u16 type, const u8 *match, size_t match_len)
3653 {
3654 struct wpa_driver_nl80211_data *drv = bss->drv;
3655 struct nl_msg *msg;
3656 int ret = -1;
3657
3658 msg = nlmsg_alloc();
3659 if (!msg)
3660 return -1;
3661
3662 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3663 type, nl_handle);
3664 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3665 match, match_len);
3666
3667 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3668
3669 if (nl80211_set_iface_id(msg, bss))
3670 goto nla_put_failure;
3671
3672 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3673 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3674
3675 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
3676 msg = NULL;
3677 if (ret) {
3678 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3679 "failed (type=%u): ret=%d (%s)",
3680 type, ret, strerror(-ret));
3681 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3682 match, match_len);
3683 goto nla_put_failure;
3684 }
3685 ret = 0;
3686 nla_put_failure:
3687 nlmsg_free(msg);
3688 return ret;
3689 }
3690
3691
3692 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3693 {
3694 struct wpa_driver_nl80211_data *drv = bss->drv;
3695
3696 if (bss->nl_mgmt) {
3697 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3698 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3699 return -1;
3700 }
3701
3702 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3703 if (bss->nl_mgmt == NULL)
3704 return -1;
3705
3706 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3707 wpa_driver_nl80211_event_receive, bss->nl_cb,
3708 bss->nl_mgmt);
3709
3710 return 0;
3711 }
3712
3713
3714 static int nl80211_register_action_frame(struct i802_bss *bss,
3715 const u8 *match, size_t match_len)
3716 {
3717 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
3718 return nl80211_register_frame(bss, bss->nl_mgmt,
3719 type, match, match_len);
3720 }
3721
3722
3723 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
3724 {
3725 struct wpa_driver_nl80211_data *drv = bss->drv;
3726
3727 if (nl80211_alloc_mgmt_handle(bss))
3728 return -1;
3729 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3730 "handle %p", bss->nl_mgmt);
3731
3732 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
3733 /* GAS Initial Request */
3734 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
3735 return -1;
3736 /* GAS Initial Response */
3737 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
3738 return -1;
3739 /* GAS Comeback Request */
3740 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
3741 return -1;
3742 /* GAS Comeback Response */
3743 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
3744 return -1;
3745 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3746 #ifdef CONFIG_P2P
3747 /* P2P Public Action */
3748 if (nl80211_register_action_frame(bss,
3749 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3750 6) < 0)
3751 return -1;
3752 /* P2P Action */
3753 if (nl80211_register_action_frame(bss,
3754 (u8 *) "\x7f\x50\x6f\x9a\x09",
3755 5) < 0)
3756 return -1;
3757 #endif /* CONFIG_P2P */
3758 #ifdef CONFIG_IEEE80211W
3759 /* SA Query Response */
3760 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
3761 return -1;
3762 #endif /* CONFIG_IEEE80211W */
3763 #ifdef CONFIG_TDLS
3764 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3765 /* TDLS Discovery Response */
3766 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3767 0)
3768 return -1;
3769 }
3770 #endif /* CONFIG_TDLS */
3771
3772 /* FT Action frames */
3773 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
3774 return -1;
3775 else
3776 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3777 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3778
3779 /* WNM - BSS Transition Management Request */
3780 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3781 return -1;
3782 /* WNM-Sleep Mode Response */
3783 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
3784 return -1;
3785
3786 return 0;
3787 }
3788
3789
3790 static int nl80211_register_spurious_class3(struct i802_bss *bss)
3791 {
3792 struct wpa_driver_nl80211_data *drv = bss->drv;
3793 struct nl_msg *msg;
3794 int ret = -1;
3795
3796 msg = nlmsg_alloc();
3797 if (!msg)
3798 return -1;
3799
3800 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3801
3802 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3803
3804 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3805 msg = NULL;
3806 if (ret) {
3807 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3808 "failed: ret=%d (%s)",
3809 ret, strerror(-ret));
3810 goto nla_put_failure;
3811 }
3812 ret = 0;
3813 nla_put_failure:
3814 nlmsg_free(msg);
3815 return ret;
3816 }
3817
3818
3819 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3820 {
3821 static const int stypes[] = {
3822 WLAN_FC_STYPE_AUTH,
3823 WLAN_FC_STYPE_ASSOC_REQ,
3824 WLAN_FC_STYPE_REASSOC_REQ,
3825 WLAN_FC_STYPE_DISASSOC,
3826 WLAN_FC_STYPE_DEAUTH,
3827 WLAN_FC_STYPE_ACTION,
3828 WLAN_FC_STYPE_PROBE_REQ,
3829 /* Beacon doesn't work as mac80211 doesn't currently allow
3830 * it, but it wouldn't really be the right thing anyway as
3831 * it isn't per interface ... maybe just dump the scan
3832 * results periodically for OLBC?
3833 */
3834 // WLAN_FC_STYPE_BEACON,
3835 };
3836 unsigned int i;
3837
3838 if (nl80211_alloc_mgmt_handle(bss))
3839 return -1;
3840 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3841 "handle %p", bss->nl_mgmt);
3842
3843 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3844 if (nl80211_register_frame(bss, bss->nl_mgmt,
3845 (WLAN_FC_TYPE_MGMT << 2) |
3846 (stypes[i] << 4),
3847 NULL, 0) < 0) {
3848 goto out_err;
3849 }
3850 }
3851
3852 if (nl80211_register_spurious_class3(bss))
3853 goto out_err;
3854
3855 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3856 goto out_err;
3857
3858 return 0;
3859
3860 out_err:
3861 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3862 nl_destroy_handles(&bss->nl_mgmt);
3863 return -1;
3864 }
3865
3866
3867 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3868 {
3869 if (nl80211_alloc_mgmt_handle(bss))
3870 return -1;
3871 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3872 "handle %p (device SME)", bss->nl_mgmt);
3873
3874 if (nl80211_register_frame(bss, bss->nl_mgmt,
3875 (WLAN_FC_TYPE_MGMT << 2) |
3876 (WLAN_FC_STYPE_ACTION << 4),
3877 NULL, 0) < 0)
3878 goto out_err;
3879
3880 return 0;
3881
3882 out_err:
3883 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3884 nl_destroy_handles(&bss->nl_mgmt);
3885 return -1;
3886 }
3887
3888
3889 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3890 {
3891 if (bss->nl_mgmt == NULL)
3892 return;
3893 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3894 "(%s)", bss->nl_mgmt, reason);
3895 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3896 nl_destroy_handles(&bss->nl_mgmt);
3897
3898 nl80211_put_wiphy_data_ap(bss);
3899 }
3900
3901
3902 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3903 {
3904 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3905 }
3906
3907
3908 static int
3909 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3910 {
3911 struct i802_bss *bss = &drv->first_bss;
3912 int send_rfkill_event = 0;
3913
3914 drv->ifindex = if_nametoindex(bss->ifname);
3915 drv->first_bss.ifindex = drv->ifindex;
3916
3917 #ifndef HOSTAPD
3918 /*
3919 * Make sure the interface starts up in station mode unless this is a
3920 * dynamically added interface (e.g., P2P) that was already configured
3921 * with proper iftype.
3922 */
3923 if (drv->ifindex != drv->global->if_add_ifindex) {
3924 enum nl80211_iftype nlmode;
3925
3926 nlmode = nl80211_get_ifmode(bss);
3927 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
3928 nlmode = NL80211_IFTYPE_STATION;
3929
3930 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
3931 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to use %s mode",
3932 nlmode == NL80211_IFTYPE_STATION ?
3933 "managed" : "P2P-Device");
3934 return -1;
3935 }
3936
3937 /* Always use managed mode internally, even for P2P Device */
3938 drv->nlmode = NL80211_IFTYPE_STATION;
3939 }
3940
3941 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
3942 if (rfkill_is_blocked(drv->rfkill)) {
3943 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3944 "interface '%s' due to rfkill",
3945 bss->ifname);
3946 drv->if_disabled = 1;
3947 send_rfkill_event = 1;
3948 } else {
3949 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3950 "interface '%s' UP", bss->ifname);
3951 return -1;
3952 }
3953 }
3954
3955 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3956 1, IF_OPER_DORMANT);
3957 #endif /* HOSTAPD */
3958
3959 if (wpa_driver_nl80211_capa(drv))
3960 return -1;
3961
3962 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3963 bss->addr))
3964 return -1;
3965
3966 if (send_rfkill_event) {
3967 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3968 drv, drv->ctx);
3969 }
3970
3971 return 0;
3972 }
3973
3974
3975 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3976 {
3977 struct nl_msg *msg;
3978
3979 msg = nlmsg_alloc();
3980 if (!msg)
3981 return -ENOMEM;
3982
3983 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
3984 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3985
3986 return send_and_recv_msgs(drv, msg, NULL, NULL);
3987 nla_put_failure:
3988 nlmsg_free(msg);
3989 return -ENOBUFS;
3990 }
3991
3992
3993 /**
3994 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
3995 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3996 *
3997 * Shut down driver interface and processing of driver events. Free
3998 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3999 */
4000 static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
4001 {
4002 struct wpa_driver_nl80211_data *drv = bss->drv;
4003
4004 bss->in_deinit = 1;
4005 if (drv->data_tx_status)
4006 eloop_unregister_read_sock(drv->eapol_tx_sock);
4007 if (drv->eapol_tx_sock >= 0)
4008 close(drv->eapol_tx_sock);
4009
4010 if (bss->nl_preq)
4011 wpa_driver_nl80211_probe_req_report(bss, 0);
4012 if (bss->added_if_into_bridge) {
4013 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
4014 bss->ifname) < 0)
4015 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4016 "interface %s from bridge %s: %s",
4017 bss->ifname, bss->brname, strerror(errno));
4018 }
4019 if (bss->added_bridge) {
4020 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
4021 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
4022 "bridge %s: %s",
4023 bss->brname, strerror(errno));
4024 }
4025
4026 nl80211_remove_monitor_interface(drv);
4027
4028 if (is_ap_interface(drv->nlmode))
4029 wpa_driver_nl80211_del_beacon(drv);
4030
4031 #ifdef HOSTAPD
4032 if (drv->last_freq_ht) {
4033 /* Clear HT flags from the driver */
4034 struct hostapd_freq_params freq;
4035 os_memset(&freq, 0, sizeof(freq));
4036 freq.freq = drv->last_freq;
4037 wpa_driver_nl80211_set_freq(bss, &freq);
4038 }
4039
4040 if (drv->eapol_sock >= 0) {
4041 eloop_unregister_read_sock(drv->eapol_sock);
4042 close(drv->eapol_sock);
4043 }
4044
4045 if (drv->if_indices != drv->default_if_indices)
4046 os_free(drv->if_indices);
4047 #endif /* HOSTAPD */
4048
4049 if (drv->disabled_11b_rates)
4050 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4051
4052 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
4053 IF_OPER_UP);
4054 rfkill_deinit(drv->rfkill);
4055
4056 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4057
4058 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
4059 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
4060 nl80211_mgmt_unsubscribe(bss, "deinit");
4061
4062 nl_cb_put(drv->nl_cb);
4063
4064 nl80211_destroy_bss(&drv->first_bss);
4065
4066 os_free(drv->filter_ssids);
4067
4068 os_free(drv->auth_ie);
4069
4070 if (drv->in_interface_list)
4071 dl_list_del(&drv->list);
4072
4073 os_free(drv->extended_capa);
4074 os_free(drv->extended_capa_mask);
4075 os_free(drv);
4076 }
4077
4078
4079 /**
4080 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
4081 * @eloop_ctx: Driver private data
4082 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
4083 *
4084 * This function can be used as registered timeout when starting a scan to
4085 * generate a scan completed event if the driver does not report this.
4086 */
4087 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
4088 {
4089 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4090 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
4091 wpa_driver_nl80211_set_mode(&drv->first_bss,
4092 drv->ap_scan_as_station);
4093 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
4094 }
4095 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
4096 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
4097 }
4098
4099
4100 static struct nl_msg *
4101 nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
4102 struct wpa_driver_scan_params *params, u64 *wdev_id)
4103 {
4104 struct nl_msg *msg;
4105 size_t i;
4106
4107 msg = nlmsg_alloc();
4108 if (!msg)
4109 return NULL;
4110
4111 nl80211_cmd(drv, msg, 0, cmd);
4112
4113 if (!wdev_id)
4114 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4115 else
4116 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
4117
4118 if (params->num_ssids) {
4119 struct nlattr *ssids;
4120
4121 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4122 if (ssids == NULL)
4123 goto fail;
4124 for (i = 0; i < params->num_ssids; i++) {
4125 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
4126 params->ssids[i].ssid,
4127 params->ssids[i].ssid_len);
4128 if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
4129 params->ssids[i].ssid) < 0)
4130 goto fail;
4131 }
4132 nla_nest_end(msg, ssids);
4133 }
4134
4135 if (params->extra_ies) {
4136 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
4137 params->extra_ies, params->extra_ies_len);
4138 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
4139 params->extra_ies) < 0)
4140 goto fail;
4141 }
4142
4143 if (params->freqs) {
4144 struct nlattr *freqs;
4145 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4146 if (freqs == NULL)
4147 goto fail;
4148 for (i = 0; params->freqs[i]; i++) {
4149 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
4150 "MHz", params->freqs[i]);
4151 if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
4152 goto fail;
4153 }
4154 nla_nest_end(msg, freqs);
4155 }
4156
4157 os_free(drv->filter_ssids);
4158 drv->filter_ssids = params->filter_ssids;
4159 params->filter_ssids = NULL;
4160 drv->num_filter_ssids = params->num_filter_ssids;
4161
4162 return msg;
4163
4164 fail:
4165 nla_put_failure:
4166 nlmsg_free(msg);
4167 return NULL;
4168 }
4169
4170
4171 /**
4172 * wpa_driver_nl80211_scan - Request the driver to initiate scan
4173 * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
4174 * @params: Scan parameters
4175 * Returns: 0 on success, -1 on failure
4176 */
4177 static int wpa_driver_nl80211_scan(struct i802_bss *bss,
4178 struct wpa_driver_scan_params *params)
4179 {
4180 struct wpa_driver_nl80211_data *drv = bss->drv;
4181 int ret = -1, timeout;
4182 struct nl_msg *msg = NULL;
4183
4184 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
4185 drv->scan_for_auth = 0;
4186
4187 msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
4188 bss->wdev_id_set ? &bss->wdev_id : NULL);
4189 if (!msg)
4190 return -1;
4191
4192 if (params->p2p_probe) {
4193 struct nlattr *rates;
4194
4195 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
4196
4197 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
4198 if (rates == NULL)
4199 goto nla_put_failure;
4200
4201 /*
4202 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
4203 * by masking out everything else apart from the OFDM rates 6,
4204 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
4205 * rates are left enabled.
4206 */
4207 NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
4208 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
4209 nla_nest_end(msg, rates);
4210
4211 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
4212 }
4213
4214 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4215 msg = NULL;
4216 if (ret) {
4217 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
4218 "(%s)", ret, strerror(-ret));
4219 #ifdef HOSTAPD
4220 if (is_ap_interface(drv->nlmode)) {
4221 /*
4222 * mac80211 does not allow scan requests in AP mode, so
4223 * try to do this in station mode.
4224 */
4225 if (wpa_driver_nl80211_set_mode(
4226 bss, NL80211_IFTYPE_STATION))
4227 goto nla_put_failure;
4228
4229 if (wpa_driver_nl80211_scan(bss, params)) {
4230 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
4231 goto nla_put_failure;
4232 }
4233
4234 /* Restore AP mode when processing scan results */
4235 drv->ap_scan_as_station = drv->nlmode;
4236 ret = 0;
4237 } else
4238 goto nla_put_failure;
4239 #else /* HOSTAPD */
4240 goto nla_put_failure;
4241 #endif /* HOSTAPD */
4242 }
4243
4244 /* Not all drivers generate "scan completed" wireless event, so try to
4245 * read results after a timeout. */
4246 timeout = 10;
4247 if (drv->scan_complete_events) {
4248 /*
4249 * The driver seems to deliver events to notify when scan is
4250 * complete, so use longer timeout to avoid race conditions
4251 * with scanning and following association request.
4252 */
4253 timeout = 30;
4254 }
4255 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
4256 "seconds", ret, timeout);
4257 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
4258 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
4259 drv, drv->ctx);
4260
4261 nla_put_failure:
4262 nlmsg_free(msg);
4263 return ret;
4264 }
4265
4266
4267 /**
4268 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
4269 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4270 * @params: Scan parameters
4271 * @interval: Interval between scan cycles in milliseconds
4272 * Returns: 0 on success, -1 on failure or if not supported
4273 */
4274 static int wpa_driver_nl80211_sched_scan(void *priv,
4275 struct wpa_driver_scan_params *params,
4276 u32 interval)
4277 {
4278 struct i802_bss *bss = priv;
4279 struct wpa_driver_nl80211_data *drv = bss->drv;
4280 int ret = -1;
4281 struct nl_msg *msg;
4282 size_t i;
4283
4284 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
4285
4286 #ifdef ANDROID
4287 if (!drv->capa.sched_scan_supported)
4288 return android_pno_start(bss, params);
4289 #endif /* ANDROID */
4290
4291 msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
4292 bss->wdev_id_set ? &bss->wdev_id : NULL);
4293 if (!msg)
4294 goto nla_put_failure;
4295
4296 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
4297
4298 if ((drv->num_filter_ssids &&
4299 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
4300 params->filter_rssi) {
4301 struct nlattr *match_sets;
4302 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
4303 if (match_sets == NULL)
4304 goto nla_put_failure;
4305
4306 for (i = 0; i < drv->num_filter_ssids; i++) {
4307 struct nlattr *match_set_ssid;
4308 wpa_hexdump_ascii(MSG_MSGDUMP,
4309 "nl80211: Sched scan filter SSID",
4310 drv->filter_ssids[i].ssid,
4311 drv->filter_ssids[i].ssid_len);
4312
4313 match_set_ssid = nla_nest_start(msg, i + 1);
4314 if (match_set_ssid == NULL)
4315 goto nla_put_failure;
4316 NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
4317 drv->filter_ssids[i].ssid_len,
4318 drv->filter_ssids[i].ssid);
4319
4320 nla_nest_end(msg, match_set_ssid);
4321 }
4322
4323 if (params->filter_rssi) {
4324 struct nlattr *match_set_rssi;
4325 match_set_rssi = nla_nest_start(msg, 0);
4326 if (match_set_rssi == NULL)
4327 goto nla_put_failure;
4328 NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
4329 params->filter_rssi);
4330 wpa_printf(MSG_MSGDUMP,
4331 "nl80211: Sched scan RSSI filter %d dBm",
4332 params->filter_rssi);
4333 nla_nest_end(msg, match_set_rssi);
4334 }
4335
4336 nla_nest_end(msg, match_sets);
4337 }
4338
4339 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4340
4341 /* TODO: if we get an error here, we should fall back to normal scan */
4342
4343 msg = NULL;
4344 if (ret) {
4345 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
4346 "ret=%d (%s)", ret, strerror(-ret));
4347 goto nla_put_failure;
4348 }
4349
4350 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
4351 "scan interval %d msec", ret, interval);
4352
4353 nla_put_failure:
4354 nlmsg_free(msg);
4355 return ret;
4356 }
4357
4358
4359 /**
4360 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
4361 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
4362 * Returns: 0 on success, -1 on failure or if not supported
4363 */
4364 static int wpa_driver_nl80211_stop_sched_scan(void *priv)
4365 {
4366 struct i802_bss *bss = priv;
4367 struct wpa_driver_nl80211_data *drv = bss->drv;
4368 int ret = 0;
4369 struct nl_msg *msg;
4370
4371 #ifdef ANDROID
4372 if (!drv->capa.sched_scan_supported)
4373 return android_pno_stop(bss);
4374 #endif /* ANDROID */
4375
4376 msg = nlmsg_alloc();
4377 if (!msg)
4378 return -1;
4379
4380 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
4381
4382 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4383
4384 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4385 msg = NULL;
4386 if (ret) {
4387 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
4388 "ret=%d (%s)", ret, strerror(-ret));
4389 goto nla_put_failure;
4390 }
4391
4392 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
4393
4394 nla_put_failure:
4395 nlmsg_free(msg);
4396 return ret;
4397 }
4398
4399
4400 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
4401 {
4402 const u8 *end, *pos;
4403
4404 if (ies == NULL)
4405 return NULL;
4406
4407 pos = ies;
4408 end = ies + ies_len;
4409
4410 while (pos + 1 < end) {
4411 if (pos + 2 + pos[1] > end)
4412 break;
4413 if (pos[0] == ie)
4414 return pos;
4415 pos += 2 + pos[1];
4416 }
4417
4418 return NULL;
4419 }
4420
4421
4422 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
4423 const u8 *ie, size_t ie_len)
4424 {
4425 const u8 *ssid;
4426 size_t i;
4427
4428 if (drv->filter_ssids == NULL)
4429 return 0;
4430
4431 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
4432 if (ssid == NULL)
4433 return 1;
4434
4435 for (i = 0; i < drv->num_filter_ssids; i++) {
4436 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
4437 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
4438 0)
4439 return 0;
4440 }
4441
4442 return 1;
4443 }
4444
4445
4446 static int bss_info_handler(struct nl_msg *msg, void *arg)
4447 {
4448 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4449 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4450 struct nlattr *bss[NL80211_BSS_MAX + 1];
4451 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
4452 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
4453 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
4454 [NL80211_BSS_TSF] = { .type = NLA_U64 },
4455 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
4456 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
4457 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
4458 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
4459 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
4460 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
4461 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
4462 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
4463 };
4464 struct nl80211_bss_info_arg *_arg = arg;
4465 struct wpa_scan_results *res = _arg->res;
4466 struct wpa_scan_res **tmp;
4467 struct wpa_scan_res *r;
4468 const u8 *ie, *beacon_ie;
4469 size_t ie_len, beacon_ie_len;
4470 u8 *pos;
4471 size_t i;
4472
4473 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4474 genlmsg_attrlen(gnlh, 0), NULL);
4475 if (!tb[NL80211_ATTR_BSS])
4476 return NL_SKIP;
4477 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
4478 bss_policy))
4479 return NL_SKIP;
4480 if (bss[NL80211_BSS_STATUS]) {
4481 enum nl80211_bss_status status;
4482 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4483 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4484 bss[NL80211_BSS_FREQUENCY]) {
4485 _arg->assoc_freq =
4486 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4487 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
4488 _arg->assoc_freq);
4489 }
4490 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
4491 bss[NL80211_BSS_BSSID]) {
4492 os_memcpy(_arg->assoc_bssid,
4493 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
4494 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
4495 MACSTR, MAC2STR(_arg->assoc_bssid));
4496 }
4497 }
4498 if (!res)
4499 return NL_SKIP;
4500 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
4501 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4502 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
4503 } else {
4504 ie = NULL;
4505 ie_len = 0;
4506 }
4507 if (bss[NL80211_BSS_BEACON_IES]) {
4508 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
4509 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
4510 } else {
4511 beacon_ie = NULL;
4512 beacon_ie_len = 0;
4513 }
4514
4515 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
4516 ie ? ie_len : beacon_ie_len))
4517 return NL_SKIP;
4518
4519 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
4520 if (r == NULL)
4521 return NL_SKIP;
4522 if (bss[NL80211_BSS_BSSID])
4523 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
4524 ETH_ALEN);
4525 if (bss[NL80211_BSS_FREQUENCY])
4526 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
4527 if (bss[NL80211_BSS_BEACON_INTERVAL])
4528 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
4529 if (bss[NL80211_BSS_CAPABILITY])
4530 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
4531 r->flags |= WPA_SCAN_NOISE_INVALID;
4532 if (bss[NL80211_BSS_SIGNAL_MBM]) {
4533 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
4534 r->level /= 100; /* mBm to dBm */
4535 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
4536 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
4537 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
4538 r->flags |= WPA_SCAN_QUAL_INVALID;
4539 } else
4540 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
4541 if (bss[NL80211_BSS_TSF])
4542 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
4543 if (bss[NL80211_BSS_SEEN_MS_AGO])
4544 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
4545 r->ie_len = ie_len;
4546 pos = (u8 *) (r + 1);
4547 if (ie) {
4548 os_memcpy(pos, ie, ie_len);
4549 pos += ie_len;
4550 }
4551 r->beacon_ie_len = beacon_ie_len;
4552 if (beacon_ie)
4553 os_memcpy(pos, beacon_ie, beacon_ie_len);
4554
4555 if (bss[NL80211_BSS_STATUS]) {
4556 enum nl80211_bss_status status;
4557 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
4558 switch (status) {
4559 case NL80211_BSS_STATUS_AUTHENTICATED:
4560 r->flags |= WPA_SCAN_AUTHENTICATED;
4561 break;
4562 case NL80211_BSS_STATUS_ASSOCIATED:
4563 r->flags |= WPA_SCAN_ASSOCIATED;
4564 break;
4565 default:
4566 break;
4567 }
4568 }
4569
4570 /*
4571 * cfg80211 maintains separate BSS table entries for APs if the same
4572 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
4573 * not use frequency as a separate key in the BSS table, so filter out
4574 * duplicated entries. Prefer associated BSS entry in such a case in
4575 * order to get the correct frequency into the BSS table.
4576 */
4577 for (i = 0; i < res->num; i++) {
4578 const u8 *s1, *s2;
4579 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
4580 continue;
4581
4582 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
4583 res->res[i]->ie_len, WLAN_EID_SSID);
4584 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
4585 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
4586 os_memcmp(s1, s2, 2 + s1[1]) != 0)
4587 continue;
4588
4589 /* Same BSSID,SSID was already included in scan results */
4590 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
4591 "for " MACSTR, MAC2STR(r->bssid));
4592
4593 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
4594 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
4595 os_free(res->res[i]);
4596 res->res[i] = r;
4597 } else
4598 os_free(r);
4599 return NL_SKIP;
4600 }
4601
4602 tmp = os_realloc_array(res->res, res->num + 1,
4603 sizeof(struct wpa_scan_res *));
4604 if (tmp == NULL) {
4605 os_free(r);
4606 return NL_SKIP;
4607 }
4608 tmp[res->num++] = r;
4609 res->res = tmp;
4610
4611 return NL_SKIP;
4612 }
4613
4614
4615 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4616 const u8 *addr)
4617 {
4618 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4619 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4620 "mismatch (" MACSTR ")", MAC2STR(addr));
4621 wpa_driver_nl80211_mlme(drv, addr,
4622 NL80211_CMD_DEAUTHENTICATE,
4623 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4624 }
4625 }
4626
4627
4628 static void wpa_driver_nl80211_check_bss_status(
4629 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4630 {
4631 size_t i;
4632
4633 for (i = 0; i < res->num; i++) {
4634 struct wpa_scan_res *r = res->res[i];
4635 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4636 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4637 "indicates BSS status with " MACSTR
4638 " as authenticated",
4639 MAC2STR(r->bssid));
4640 if (is_sta_interface(drv->nlmode) &&
4641 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4642 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4643 0) {
4644 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4645 " in local state (auth=" MACSTR
4646 " assoc=" MACSTR ")",
4647 MAC2STR(drv->auth_bssid),
4648 MAC2STR(drv->bssid));
4649 clear_state_mismatch(drv, r->bssid);
4650 }
4651 }
4652
4653 if (r->flags & WPA_SCAN_ASSOCIATED) {
4654 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4655 "indicate BSS status with " MACSTR
4656 " as associated",
4657 MAC2STR(r->bssid));
4658 if (is_sta_interface(drv->nlmode) &&
4659 !drv->associated) {
4660 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4661 "(not associated) does not match "
4662 "with BSS state");
4663 clear_state_mismatch(drv, r->bssid);
4664 } else if (is_sta_interface(drv->nlmode) &&
4665 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4666 0) {
4667 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4668 "(associated with " MACSTR ") does "
4669 "not match with BSS state",
4670 MAC2STR(drv->bssid));
4671 clear_state_mismatch(drv, r->bssid);
4672 clear_state_mismatch(drv, drv->bssid);
4673 }
4674 }
4675 }
4676 }
4677
4678
4679 static struct wpa_scan_results *
4680 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4681 {
4682 struct nl_msg *msg;
4683 struct wpa_scan_results *res;
4684 int ret;
4685 struct nl80211_bss_info_arg arg;
4686
4687 res = os_zalloc(sizeof(*res));
4688 if (res == NULL)
4689 return NULL;
4690 msg = nlmsg_alloc();
4691 if (!msg)
4692 goto nla_put_failure;
4693
4694 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
4695 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4696
4697 arg.drv = drv;
4698 arg.res = res;
4699 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4700 msg = NULL;
4701 if (ret == 0) {
4702 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4703 "BSSes)", (unsigned long) res->num);
4704 nl80211_get_noise_for_scan_results(drv, res);
4705 return res;
4706 }
4707 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4708 "(%s)", ret, strerror(-ret));
4709 nla_put_failure:
4710 nlmsg_free(msg);
4711 wpa_scan_results_free(res);
4712 return NULL;
4713 }
4714
4715
4716 /**
4717 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4718 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4719 * Returns: Scan results on success, -1 on failure
4720 */
4721 static struct wpa_scan_results *
4722 wpa_driver_nl80211_get_scan_results(void *priv)
4723 {
4724 struct i802_bss *bss = priv;
4725 struct wpa_driver_nl80211_data *drv = bss->drv;
4726 struct wpa_scan_results *res;
4727
4728 res = nl80211_get_scan_results(drv);
4729 if (res)
4730 wpa_driver_nl80211_check_bss_status(drv, res);
4731 return res;
4732 }
4733
4734
4735 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4736 {
4737 struct wpa_scan_results *res;
4738 size_t i;
4739
4740 res = nl80211_get_scan_results(drv);
4741 if (res == NULL) {
4742 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4743 return;
4744 }
4745
4746 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4747 for (i = 0; i < res->num; i++) {
4748 struct wpa_scan_res *r = res->res[i];
4749 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4750 (int) i, (int) res->num, MAC2STR(r->bssid),
4751 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4752 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4753 }
4754
4755 wpa_scan_results_free(res);
4756 }
4757
4758
4759 static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
4760 enum wpa_alg alg, const u8 *addr,
4761 int key_idx, int set_tx,
4762 const u8 *seq, size_t seq_len,
4763 const u8 *key, size_t key_len)
4764 {
4765 struct wpa_driver_nl80211_data *drv = bss->drv;
4766 int ifindex;
4767 struct nl_msg *msg;
4768 int ret;
4769
4770 /* Ignore for P2P Device */
4771 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
4772 return 0;
4773
4774 ifindex = if_nametoindex(ifname);
4775 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
4776 "set_tx=%d seq_len=%lu key_len=%lu",
4777 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
4778 (unsigned long) seq_len, (unsigned long) key_len);
4779 #ifdef CONFIG_TDLS
4780 if (key_idx == -1)
4781 key_idx = 0;
4782 #endif /* CONFIG_TDLS */
4783
4784 msg = nlmsg_alloc();
4785 if (!msg)
4786 return -ENOMEM;
4787
4788 if (alg == WPA_ALG_NONE) {
4789 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
4790 } else {
4791 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
4792 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4793 switch (alg) {
4794 case WPA_ALG_WEP:
4795 if (key_len == 5)
4796 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4797 WLAN_CIPHER_SUITE_WEP40);
4798 else
4799 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4800 WLAN_CIPHER_SUITE_WEP104);
4801 break;
4802 case WPA_ALG_TKIP:
4803 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4804 WLAN_CIPHER_SUITE_TKIP);
4805 break;
4806 case WPA_ALG_CCMP:
4807 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4808 WLAN_CIPHER_SUITE_CCMP);
4809 break;
4810 case WPA_ALG_GCMP:
4811 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4812 WLAN_CIPHER_SUITE_GCMP);
4813 break;
4814 case WPA_ALG_IGTK:
4815 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4816 WLAN_CIPHER_SUITE_AES_CMAC);
4817 break;
4818 case WPA_ALG_SMS4:
4819 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4820 WLAN_CIPHER_SUITE_SMS4);
4821 break;
4822 case WPA_ALG_KRK:
4823 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4824 WLAN_CIPHER_SUITE_KRK);
4825 break;
4826 default:
4827 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4828 "algorithm %d", __func__, alg);
4829 nlmsg_free(msg);
4830 return -1;
4831 }
4832 }
4833
4834 if (seq && seq_len)
4835 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4836
4837 if (addr && !is_broadcast_ether_addr(addr)) {
4838 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4839 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4840
4841 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4842 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4843 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4844 NL80211_KEYTYPE_GROUP);
4845 }
4846 } else if (addr && is_broadcast_ether_addr(addr)) {
4847 struct nlattr *types;
4848
4849 wpa_printf(MSG_DEBUG, " broadcast key");
4850
4851 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
4852 if (!types)
4853 goto nla_put_failure;
4854 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4855 nla_nest_end(msg, types);
4856 }
4857 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4858 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4859
4860 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4861 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4862 ret = 0;
4863 if (ret)
4864 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4865 ret, strerror(-ret));
4866
4867 /*
4868 * If we failed or don't need to set the default TX key (below),
4869 * we're done here.
4870 */
4871 if (ret || !set_tx || alg == WPA_ALG_NONE)
4872 return ret;
4873 if (is_ap_interface(drv->nlmode) && addr &&
4874 !is_broadcast_ether_addr(addr))
4875 return ret;
4876
4877 msg = nlmsg_alloc();
4878 if (!msg)
4879 return -ENOMEM;
4880
4881 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
4882 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4884 if (alg == WPA_ALG_IGTK)
4885 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4886 else
4887 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4888 if (addr && is_broadcast_ether_addr(addr)) {
4889 struct nlattr *types;
4890
4891 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
4892 if (!types)
4893 goto nla_put_failure;
4894 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4895 nla_nest_end(msg, types);
4896 } else if (addr) {
4897 struct nlattr *types;
4898
4899 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
4900 if (!types)
4901 goto nla_put_failure;
4902 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4903 nla_nest_end(msg, types);
4904 }
4905
4906 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4907 if (ret == -ENOENT)
4908 ret = 0;
4909 if (ret)
4910 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4911 "err=%d %s)", ret, strerror(-ret));
4912 return ret;
4913
4914 nla_put_failure:
4915 nlmsg_free(msg);
4916 return -ENOBUFS;
4917 }
4918
4919
4920 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4921 int key_idx, int defkey,
4922 const u8 *seq, size_t seq_len,
4923 const u8 *key, size_t key_len)
4924 {
4925 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4926 if (!key_attr)
4927 return -1;
4928
4929 if (defkey && alg == WPA_ALG_IGTK)
4930 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4931 else if (defkey)
4932 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4933
4934 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4935
4936 switch (alg) {
4937 case WPA_ALG_WEP:
4938 if (key_len == 5)
4939 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4940 WLAN_CIPHER_SUITE_WEP40);
4941 else
4942 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4943 WLAN_CIPHER_SUITE_WEP104);
4944 break;
4945 case WPA_ALG_TKIP:
4946 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4947 break;
4948 case WPA_ALG_CCMP:
4949 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4950 break;
4951 case WPA_ALG_GCMP:
4952 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
4953 break;
4954 case WPA_ALG_IGTK:
4955 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4956 WLAN_CIPHER_SUITE_AES_CMAC);
4957 break;
4958 default:
4959 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4960 "algorithm %d", __func__, alg);
4961 return -1;
4962 }
4963
4964 if (seq && seq_len)
4965 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4966
4967 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4968
4969 nla_nest_end(msg, key_attr);
4970
4971 return 0;
4972 nla_put_failure:
4973 return -1;
4974 }
4975
4976
4977 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4978 struct nl_msg *msg)
4979 {
4980 int i, privacy = 0;
4981 struct nlattr *nl_keys, *nl_key;
4982
4983 for (i = 0; i < 4; i++) {
4984 if (!params->wep_key[i])
4985 continue;
4986 privacy = 1;
4987 break;
4988 }
4989 if (params->wps == WPS_MODE_PRIVACY)
4990 privacy = 1;
4991 if (params->pairwise_suite &&
4992 params->pairwise_suite != WPA_CIPHER_NONE)
4993 privacy = 1;
4994
4995 if (!privacy)
4996 return 0;
4997
4998 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4999
5000 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
5001 if (!nl_keys)
5002 goto nla_put_failure;
5003
5004 for (i = 0; i < 4; i++) {
5005 if (!params->wep_key[i])
5006 continue;
5007
5008 nl_key = nla_nest_start(msg, i);
5009 if (!nl_key)
5010 goto nla_put_failure;
5011
5012 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
5013 params->wep_key[i]);
5014 if (params->wep_key_len[i] == 5)
5015 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5016 WLAN_CIPHER_SUITE_WEP40);
5017 else
5018 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
5019 WLAN_CIPHER_SUITE_WEP104);
5020
5021 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
5022
5023 if (i == params->wep_tx_keyidx)
5024 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
5025
5026 nla_nest_end(msg, nl_key);
5027 }
5028 nla_nest_end(msg, nl_keys);
5029
5030 return 0;
5031
5032 nla_put_failure:
5033 return -ENOBUFS;
5034 }
5035
5036
5037 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
5038 const u8 *addr, int cmd, u16 reason_code,
5039 int local_state_change)
5040 {
5041 int ret = -1;
5042 struct nl_msg *msg;
5043
5044 msg = nlmsg_alloc();
5045 if (!msg)
5046 return -1;
5047
5048 nl80211_cmd(drv, msg, 0, cmd);
5049
5050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5051 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
5052 if (addr)
5053 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5054 if (local_state_change)
5055 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5056
5057 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5058 msg = NULL;
5059 if (ret) {
5060 wpa_dbg(drv->ctx, MSG_DEBUG,
5061 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
5062 reason_code, ret, strerror(-ret));
5063 goto nla_put_failure;
5064 }
5065 ret = 0;
5066
5067 nla_put_failure:
5068 nlmsg_free(msg);
5069 return ret;
5070 }
5071
5072
5073 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
5074 int reason_code)
5075 {
5076 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
5077 nl80211_mark_disconnected(drv);
5078 drv->ignore_next_local_disconnect = 0;
5079 /* Disconnect command doesn't need BSSID - it uses cached value */
5080 return wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
5081 reason_code, 0);
5082 }
5083
5084
5085 static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
5086 const u8 *addr, int reason_code)
5087 {
5088 struct wpa_driver_nl80211_data *drv = bss->drv;
5089 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
5090 return wpa_driver_nl80211_disconnect(drv, reason_code);
5091 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
5092 __func__, MAC2STR(addr), reason_code);
5093 nl80211_mark_disconnected(drv);
5094 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
5095 return nl80211_leave_ibss(drv);
5096 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
5097 reason_code, 0);
5098 }
5099
5100
5101 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
5102 struct wpa_driver_auth_params *params)
5103 {
5104 int i;
5105
5106 drv->auth_freq = params->freq;
5107 drv->auth_alg = params->auth_alg;
5108 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
5109 drv->auth_local_state_change = params->local_state_change;
5110 drv->auth_p2p = params->p2p;
5111
5112 if (params->bssid)
5113 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
5114 else
5115 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
5116
5117 if (params->ssid) {
5118 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
5119 drv->auth_ssid_len = params->ssid_len;
5120 } else
5121 drv->auth_ssid_len = 0;
5122
5123
5124 os_free(drv->auth_ie);
5125 drv->auth_ie = NULL;
5126 drv->auth_ie_len = 0;
5127 if (params->ie) {
5128 drv->auth_ie = os_malloc(params->ie_len);
5129 if (drv->auth_ie) {
5130 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
5131 drv->auth_ie_len = params->ie_len;
5132 }
5133 }
5134
5135 for (i = 0; i < 4; i++) {
5136 if (params->wep_key[i] && params->wep_key_len[i] &&
5137 params->wep_key_len[i] <= 16) {
5138 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
5139 params->wep_key_len[i]);
5140 drv->auth_wep_key_len[i] = params->wep_key_len[i];
5141 } else
5142 drv->auth_wep_key_len[i] = 0;
5143 }
5144 }
5145
5146
5147 static int wpa_driver_nl80211_authenticate(
5148 struct i802_bss *bss, struct wpa_driver_auth_params *params)
5149 {
5150 struct wpa_driver_nl80211_data *drv = bss->drv;
5151 int ret = -1, i;
5152 struct nl_msg *msg;
5153 enum nl80211_auth_type type;
5154 enum nl80211_iftype nlmode;
5155 int count = 0;
5156 int is_retry;
5157
5158 is_retry = drv->retry_auth;
5159 drv->retry_auth = 0;
5160
5161 nl80211_mark_disconnected(drv);
5162 os_memset(drv->auth_bssid, 0, ETH_ALEN);
5163 if (params->bssid)
5164 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5165 else
5166 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5167 /* FIX: IBSS mode */
5168 nlmode = params->p2p ?
5169 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5170 if (drv->nlmode != nlmode &&
5171 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
5172 return -1;
5173
5174 retry:
5175 msg = nlmsg_alloc();
5176 if (!msg)
5177 return -1;
5178
5179 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
5180 drv->ifindex);
5181
5182 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
5183
5184 for (i = 0; i < 4; i++) {
5185 if (!params->wep_key[i])
5186 continue;
5187 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
5188 NULL, i,
5189 i == params->wep_tx_keyidx, NULL, 0,
5190 params->wep_key[i],
5191 params->wep_key_len[i]);
5192 if (params->wep_tx_keyidx != i)
5193 continue;
5194 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
5195 params->wep_key[i], params->wep_key_len[i])) {
5196 nlmsg_free(msg);
5197 return -1;
5198 }
5199 }
5200
5201 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5202 if (params->bssid) {
5203 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5204 MAC2STR(params->bssid));
5205 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5206 }
5207 if (params->freq) {
5208 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
5209 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5210 }
5211 if (params->ssid) {
5212 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5213 params->ssid, params->ssid_len);
5214 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5215 params->ssid);
5216 }
5217 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
5218 if (params->ie)
5219 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
5220 if (params->sae_data) {
5221 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
5222 params->sae_data_len);
5223 NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
5224 params->sae_data);
5225 }
5226 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5227 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5228 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5229 type = NL80211_AUTHTYPE_SHARED_KEY;
5230 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5231 type = NL80211_AUTHTYPE_NETWORK_EAP;
5232 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5233 type = NL80211_AUTHTYPE_FT;
5234 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
5235 type = NL80211_AUTHTYPE_SAE;
5236 else
5237 goto nla_put_failure;
5238 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
5239 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5240 if (params->local_state_change) {
5241 wpa_printf(MSG_DEBUG, " * Local state change only");
5242 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
5243 }
5244
5245 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5246 msg = NULL;
5247 if (ret) {
5248 wpa_dbg(drv->ctx, MSG_DEBUG,
5249 "nl80211: MLME command failed (auth): ret=%d (%s)",
5250 ret, strerror(-ret));
5251 count++;
5252 if (ret == -EALREADY && count == 1 && params->bssid &&
5253 !params->local_state_change) {
5254 /*
5255 * mac80211 does not currently accept new
5256 * authentication if we are already authenticated. As a
5257 * workaround, force deauthentication and try again.
5258 */
5259 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
5260 "after forced deauthentication");
5261 wpa_driver_nl80211_deauthenticate(
5262 bss, params->bssid,
5263 WLAN_REASON_PREV_AUTH_NOT_VALID);
5264 nlmsg_free(msg);
5265 goto retry;
5266 }
5267
5268 if (ret == -ENOENT && params->freq && !is_retry) {
5269 /*
5270 * cfg80211 has likely expired the BSS entry even
5271 * though it was previously available in our internal
5272 * BSS table. To recover quickly, start a single
5273 * channel scan on the specified channel.
5274 */
5275 struct wpa_driver_scan_params scan;
5276 int freqs[2];
5277
5278 os_memset(&scan, 0, sizeof(scan));
5279 scan.num_ssids = 1;
5280 if (params->ssid) {
5281 scan.ssids[0].ssid = params->ssid;
5282 scan.ssids[0].ssid_len = params->ssid_len;
5283 }
5284 freqs[0] = params->freq;
5285 freqs[1] = 0;
5286 scan.freqs = freqs;
5287 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
5288 "channel scan to refresh cfg80211 BSS "
5289 "entry");
5290 ret = wpa_driver_nl80211_scan(bss, &scan);
5291 if (ret == 0) {
5292 nl80211_copy_auth_params(drv, params);
5293 drv->scan_for_auth = 1;
5294 }
5295 } else if (is_retry) {
5296 /*
5297 * Need to indicate this with an event since the return
5298 * value from the retry is not delivered to core code.
5299 */
5300 union wpa_event_data event;
5301 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
5302 "failed");
5303 os_memset(&event, 0, sizeof(event));
5304 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
5305 ETH_ALEN);
5306 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
5307 &event);
5308 }
5309
5310 goto nla_put_failure;
5311 }
5312 ret = 0;
5313 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
5314 "successfully");
5315
5316 nla_put_failure:
5317 nlmsg_free(msg);
5318 return ret;
5319 }
5320
5321
5322 static int wpa_driver_nl80211_authenticate_retry(
5323 struct wpa_driver_nl80211_data *drv)
5324 {
5325 struct wpa_driver_auth_params params;
5326 struct i802_bss *bss = &drv->first_bss;
5327 int i;
5328
5329 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
5330
5331 os_memset(&params, 0, sizeof(params));
5332 params.freq = drv->auth_freq;
5333 params.auth_alg = drv->auth_alg;
5334 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
5335 params.local_state_change = drv->auth_local_state_change;
5336 params.p2p = drv->auth_p2p;
5337
5338 if (!is_zero_ether_addr(drv->auth_bssid_))
5339 params.bssid = drv->auth_bssid_;
5340
5341 if (drv->auth_ssid_len) {
5342 params.ssid = drv->auth_ssid;
5343 params.ssid_len = drv->auth_ssid_len;
5344 }
5345
5346 params.ie = drv->auth_ie;
5347 params.ie_len = drv->auth_ie_len;
5348
5349 for (i = 0; i < 4; i++) {
5350 if (drv->auth_wep_key_len[i]) {
5351 params.wep_key[i] = drv->auth_wep_key[i];
5352 params.wep_key_len[i] = drv->auth_wep_key_len[i];
5353 }
5354 }
5355
5356 drv->retry_auth = 1;
5357 return wpa_driver_nl80211_authenticate(bss, &params);
5358 }
5359
5360
5361 struct phy_info_arg {
5362 u16 *num_modes;
5363 struct hostapd_hw_modes *modes;
5364 int last_mode, last_chan_idx;
5365 };
5366
5367 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
5368 struct nlattr *ampdu_factor,
5369 struct nlattr *ampdu_density,
5370 struct nlattr *mcs_set)
5371 {
5372 if (capa)
5373 mode->ht_capab = nla_get_u16(capa);
5374
5375 if (ampdu_factor)
5376 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
5377
5378 if (ampdu_density)
5379 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
5380
5381 if (mcs_set && nla_len(mcs_set) >= 16) {
5382 u8 *mcs;
5383 mcs = nla_data(mcs_set);
5384 os_memcpy(mode->mcs_set, mcs, 16);
5385 }
5386 }
5387
5388
5389 static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
5390 struct nlattr *capa,
5391 struct nlattr *mcs_set)
5392 {
5393 if (capa)
5394 mode->vht_capab = nla_get_u32(capa);
5395
5396 if (mcs_set && nla_len(mcs_set) >= 8) {
5397 u8 *mcs;
5398 mcs = nla_data(mcs_set);
5399 os_memcpy(mode->vht_mcs_set, mcs, 8);
5400 }
5401 }
5402
5403
5404 static void phy_info_freq(struct hostapd_hw_modes *mode,
5405 struct hostapd_channel_data *chan,
5406 struct nlattr *tb_freq[])
5407 {
5408 u8 channel;
5409 chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
5410 chan->flag = 0;
5411 if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
5412 chan->chan = channel;
5413
5414 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5415 chan->flag |= HOSTAPD_CHAN_DISABLED;
5416 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
5417 chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
5418 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
5419 chan->flag |= HOSTAPD_CHAN_NO_IBSS;
5420 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
5421 chan->flag |= HOSTAPD_CHAN_RADAR;
5422
5423 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
5424 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
5425 chan->max_tx_power = nla_get_u32(
5426 tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
5427 if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
5428 enum nl80211_dfs_state state =
5429 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
5430
5431 switch (state) {
5432 case NL80211_DFS_USABLE:
5433 chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
5434 break;
5435 case NL80211_DFS_AVAILABLE:
5436 chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
5437 break;
5438 case NL80211_DFS_UNAVAILABLE:
5439 chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
5440 break;
5441 }
5442 }
5443 }
5444
5445
5446 static int phy_info_freqs(struct phy_info_arg *phy_info,
5447 struct hostapd_hw_modes *mode, struct nlattr *tb)
5448 {
5449 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5450 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
5451 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
5452 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
5453 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
5454 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
5455 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
5456 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
5457 };
5458 int new_channels = 0;
5459 struct hostapd_channel_data *channel;
5460 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
5461 struct nlattr *nl_freq;
5462 int rem_freq, idx;
5463
5464 if (tb == NULL)
5465 return NL_OK;
5466
5467 nla_for_each_nested(nl_freq, tb, rem_freq) {
5468 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5469 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5470 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5471 continue;
5472 new_channels++;
5473 }
5474
5475 channel = os_realloc_array(mode->channels,
5476 mode->num_channels + new_channels,
5477 sizeof(struct hostapd_channel_data));
5478 if (!channel)
5479 return NL_SKIP;
5480
5481 mode->channels = channel;
5482 mode->num_channels += new_channels;
5483
5484 idx = phy_info->last_chan_idx;
5485
5486 nla_for_each_nested(nl_freq, tb, rem_freq) {
5487 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
5488 nla_data(nl_freq), nla_len(nl_freq), freq_policy);
5489 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
5490 continue;
5491 phy_info_freq(mode, &mode->channels[idx], tb_freq);
5492 idx++;
5493 }
5494 phy_info->last_chan_idx = idx;
5495
5496 return NL_OK;
5497 }
5498
5499
5500 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
5501 {
5502 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
5503 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
5504 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
5505 { .type = NLA_FLAG },
5506 };
5507 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
5508 struct nlattr *nl_rate;
5509 int rem_rate, idx;
5510
5511 if (tb == NULL)
5512 return NL_OK;
5513
5514 nla_for_each_nested(nl_rate, tb, rem_rate) {
5515 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5516 nla_data(nl_rate), nla_len(nl_rate),
5517 rate_policy);
5518 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5519 continue;
5520 mode->num_rates++;
5521 }
5522
5523 mode->rates = os_calloc(mode->num_rates, sizeof(int));
5524 if (!mode->rates)
5525 return NL_SKIP;
5526
5527 idx = 0;
5528
5529 nla_for_each_nested(nl_rate, tb, rem_rate) {
5530 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
5531 nla_data(nl_rate), nla_len(nl_rate),
5532 rate_policy);
5533 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
5534 continue;
5535 mode->rates[idx] = nla_get_u32(
5536 tb_rate[NL80211_BITRATE_ATTR_RATE]);
5537 idx++;
5538 }
5539
5540 return NL_OK;
5541 }
5542
5543
5544 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
5545 {
5546 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
5547 struct hostapd_hw_modes *mode;
5548 int ret;
5549
5550 if (phy_info->last_mode != nl_band->nla_type) {
5551 mode = os_realloc_array(phy_info->modes,
5552 *phy_info->num_modes + 1,
5553 sizeof(*mode));
5554 if (!mode)
5555 return NL_SKIP;
5556 phy_info->modes = mode;
5557
5558 mode = &phy_info->modes[*(phy_info->num_modes)];
5559 os_memset(mode, 0, sizeof(*mode));
5560 mode->mode = NUM_HOSTAPD_MODES;
5561 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
5562 *(phy_info->num_modes) += 1;
5563 phy_info->last_mode = nl_band->nla_type;
5564 phy_info->last_chan_idx = 0;
5565 } else
5566 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
5567
5568 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
5569 nla_len(nl_band), NULL);
5570
5571 phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
5572 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
5573 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
5574 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
5575 phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
5576 tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
5577 ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
5578 if (ret != NL_OK)
5579 return ret;
5580 ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
5581 if (ret != NL_OK)
5582 return ret;
5583
5584 return NL_OK;
5585 }
5586
5587
5588 static int phy_info_handler(struct nl_msg *msg, void *arg)
5589 {
5590 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5591 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5592 struct phy_info_arg *phy_info = arg;
5593 struct nlattr *nl_band;
5594 int rem_band;
5595
5596 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5597 genlmsg_attrlen(gnlh, 0), NULL);
5598
5599 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
5600 return NL_SKIP;
5601
5602 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
5603 {
5604 int res = phy_info_band(phy_info, nl_band);
5605 if (res != NL_OK)
5606 return res;
5607 }
5608
5609 return NL_SKIP;
5610 }
5611
5612
5613 static struct hostapd_hw_modes *
5614 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
5615 u16 *num_modes)
5616 {
5617 u16 m;
5618 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
5619 int i, mode11g_idx = -1;
5620
5621 /* heuristic to set up modes */
5622 for (m = 0; m < *num_modes; m++) {
5623 if (!modes[m].num_channels)
5624 continue;
5625 if (modes[m].channels[0].freq < 4000) {
5626 modes[m].mode = HOSTAPD_MODE_IEEE80211B;
5627 for (i = 0; i < modes[m].num_rates; i++) {
5628 if (modes[m].rates[i] > 200) {
5629 modes[m].mode = HOSTAPD_MODE_IEEE80211G;
5630 break;
5631 }
5632 }
5633 } else if (modes[m].channels[0].freq > 50000)
5634 modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
5635 else
5636 modes[m].mode = HOSTAPD_MODE_IEEE80211A;
5637 }
5638
5639 /* If only 802.11g mode is included, use it to construct matching
5640 * 802.11b mode data. */
5641
5642 for (m = 0; m < *num_modes; m++) {
5643 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
5644 return modes; /* 802.11b already included */
5645 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
5646 mode11g_idx = m;
5647 }
5648
5649 if (mode11g_idx < 0)
5650 return modes; /* 2.4 GHz band not supported at all */
5651
5652 nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
5653 if (nmodes == NULL)
5654 return modes; /* Could not add 802.11b mode */
5655
5656 mode = &nmodes[*num_modes];
5657 os_memset(mode, 0, sizeof(*mode));
5658 (*num_modes)++;
5659 modes = nmodes;
5660
5661 mode->mode = HOSTAPD_MODE_IEEE80211B;
5662
5663 mode11g = &modes[mode11g_idx];
5664 mode->num_channels = mode11g->num_channels;
5665 mode->channels = os_malloc(mode11g->num_channels *
5666 sizeof(struct hostapd_channel_data));
5667 if (mode->channels == NULL) {
5668 (*num_modes)--;
5669 return modes; /* Could not add 802.11b mode */
5670 }
5671 os_memcpy(mode->channels, mode11g->channels,
5672 mode11g->num_channels * sizeof(struct hostapd_channel_data));
5673
5674 mode->num_rates = 0;
5675 mode->rates = os_malloc(4 * sizeof(int));
5676 if (mode->rates == NULL) {
5677 os_free(mode->channels);
5678 (*num_modes)--;
5679 return modes; /* Could not add 802.11b mode */
5680 }
5681
5682 for (i = 0; i < mode11g->num_rates; i++) {
5683 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
5684 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
5685 continue;
5686 mode->rates[mode->num_rates] = mode11g->rates[i];
5687 mode->num_rates++;
5688 if (mode->num_rates == 4)
5689 break;
5690 }
5691
5692 if (mode->num_rates == 0) {
5693 os_free(mode->channels);
5694 os_free(mode->rates);
5695 (*num_modes)--;
5696 return modes; /* No 802.11b rates */
5697 }
5698
5699 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5700 "information");
5701
5702 return modes;
5703 }
5704
5705
5706 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5707 int end)
5708 {
5709 int c;
5710
5711 for (c = 0; c < mode->num_channels; c++) {
5712 struct hostapd_channel_data *chan = &mode->channels[c];
5713 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5714 chan->flag |= HOSTAPD_CHAN_HT40;
5715 }
5716 }
5717
5718
5719 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5720 int end)
5721 {
5722 int c;
5723
5724 for (c = 0; c < mode->num_channels; c++) {
5725 struct hostapd_channel_data *chan = &mode->channels[c];
5726 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5727 continue;
5728 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5729 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5730 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5731 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5732 }
5733 }
5734
5735
5736 static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5737 struct phy_info_arg *results)
5738 {
5739 u32 start, end, max_bw;
5740 u16 m;
5741
5742 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5743 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5744 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5745 return;
5746
5747 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5748 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5749 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5750
5751 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5752 start, end, max_bw);
5753 if (max_bw < 40)
5754 return;
5755
5756 for (m = 0; m < *results->num_modes; m++) {
5757 if (!(results->modes[m].ht_capab &
5758 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5759 continue;
5760 nl80211_set_ht40_mode(&results->modes[m], start, end);
5761 }
5762 }
5763
5764
5765 static void nl80211_reg_rule_sec(struct nlattr *tb[],
5766 struct phy_info_arg *results)
5767 {
5768 u32 start, end, max_bw;
5769 u16 m;
5770
5771 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5772 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5773 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5774 return;
5775
5776 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5777 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5778 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5779
5780 if (max_bw < 20)
5781 return;
5782
5783 for (m = 0; m < *results->num_modes; m++) {
5784 if (!(results->modes[m].ht_capab &
5785 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5786 continue;
5787 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5788 }
5789 }
5790
5791
5792 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5793 {
5794 struct phy_info_arg *results = arg;
5795 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5796 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5797 struct nlattr *nl_rule;
5798 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5799 int rem_rule;
5800 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5801 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5802 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5803 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5804 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5805 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5806 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5807 };
5808
5809 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5810 genlmsg_attrlen(gnlh, 0), NULL);
5811 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5812 !tb_msg[NL80211_ATTR_REG_RULES]) {
5813 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5814 "available");
5815 return NL_SKIP;
5816 }
5817
5818 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5819 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5820
5821 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5822 {
5823 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5824 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5825 nl80211_reg_rule_ht40(tb_rule, results);
5826 }
5827
5828 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5829 {
5830 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5831 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5832 nl80211_reg_rule_sec(tb_rule, results);
5833 }
5834
5835 return NL_SKIP;
5836 }
5837
5838
5839 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5840 struct phy_info_arg *results)
5841 {
5842 struct nl_msg *msg;
5843
5844 msg = nlmsg_alloc();
5845 if (!msg)
5846 return -ENOMEM;
5847
5848 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
5849 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5850 }
5851
5852
5853 static struct hostapd_hw_modes *
5854 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5855 {
5856 u32 feat;
5857 struct i802_bss *bss = priv;
5858 struct wpa_driver_nl80211_data *drv = bss->drv;
5859 struct nl_msg *msg;
5860 struct phy_info_arg result = {
5861 .num_modes = num_modes,
5862 .modes = NULL,
5863 .last_mode = -1,
5864 };
5865
5866 *num_modes = 0;
5867 *flags = 0;
5868
5869 msg = nlmsg_alloc();
5870 if (!msg)
5871 return NULL;
5872
5873 feat = get_nl80211_protocol_features(drv);
5874 if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
5875 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
5876 else
5877 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
5878
5879 NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
5880 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5881
5882 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5883 nl80211_set_ht40_flags(drv, &result);
5884 return wpa_driver_nl80211_postprocess_modes(result.modes,
5885 num_modes);
5886 }
5887 msg = NULL;
5888 nla_put_failure:
5889 nlmsg_free(msg);
5890 return NULL;
5891 }
5892
5893
5894 static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5895 const void *data, size_t len,
5896 int encrypt, int noack)
5897 {
5898 __u8 rtap_hdr[] = {
5899 0x00, 0x00, /* radiotap version */
5900 0x0e, 0x00, /* radiotap length */
5901 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5902 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5903 0x00, /* padding */
5904 0x00, 0x00, /* RX and TX flags to indicate that */
5905 0x00, 0x00, /* this is the injected frame directly */
5906 };
5907 struct iovec iov[2] = {
5908 {
5909 .iov_base = &rtap_hdr,
5910 .iov_len = sizeof(rtap_hdr),
5911 },
5912 {
5913 .iov_base = (void *) data,
5914 .iov_len = len,
5915 }
5916 };
5917 struct msghdr msg = {
5918 .msg_name = NULL,
5919 .msg_namelen = 0,
5920 .msg_iov = iov,
5921 .msg_iovlen = 2,
5922 .msg_control = NULL,
5923 .msg_controllen = 0,
5924 .msg_flags = 0,
5925 };
5926 int res;
5927 u16 txflags = 0;
5928
5929 if (encrypt)
5930 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5931
5932 if (drv->monitor_sock < 0) {
5933 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5934 "for %s", __func__);
5935 return -1;
5936 }
5937
5938 if (noack)
5939 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
5940 WPA_PUT_LE16(&rtap_hdr[12], txflags);
5941
5942 res = sendmsg(drv->monitor_sock, &msg, 0);
5943 if (res < 0) {
5944 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5945 return -1;
5946 }
5947 return 0;
5948 }
5949
5950
5951 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5952 const void *data, size_t len,
5953 int encrypt, int noack,
5954 unsigned int freq, int no_cck,
5955 int offchanok, unsigned int wait_time)
5956 {
5957 struct wpa_driver_nl80211_data *drv = bss->drv;
5958 u64 cookie;
5959
5960 if (freq == 0)
5961 freq = bss->freq;
5962
5963 if (drv->use_monitor)
5964 return wpa_driver_nl80211_send_mntr(drv, data, len,
5965 encrypt, noack);
5966
5967 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5968 &cookie, no_cck, noack, offchanok);
5969 }
5970
5971
5972 static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
5973 size_t data_len, int noack,
5974 unsigned int freq, int no_cck,
5975 int offchanok,
5976 unsigned int wait_time)
5977 {
5978 struct wpa_driver_nl80211_data *drv = bss->drv;
5979 struct ieee80211_mgmt *mgmt;
5980 int encrypt = 1;
5981 u16 fc;
5982
5983 mgmt = (struct ieee80211_mgmt *) data;
5984 fc = le_to_host16(mgmt->frame_control);
5985
5986 if (is_sta_interface(drv->nlmode) &&
5987 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5988 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5989 /*
5990 * The use of last_mgmt_freq is a bit of a hack,
5991 * but it works due to the single-threaded nature
5992 * of wpa_supplicant.
5993 */
5994 if (freq == 0)
5995 freq = drv->last_mgmt_freq;
5996 return nl80211_send_frame_cmd(bss, freq, 0,
5997 data, data_len, NULL, 1, noack,
5998 1);
5999 }
6000
6001 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
6002 if (freq == 0)
6003 freq = bss->freq;
6004 return nl80211_send_frame_cmd(bss, freq,
6005 (int) freq == bss->freq ? 0 :
6006 wait_time,
6007 data, data_len,
6008 &drv->send_action_cookie,
6009 no_cck, noack, offchanok);
6010 }
6011
6012 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6013 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
6014 /*
6015 * Only one of the authentication frame types is encrypted.
6016 * In order for static WEP encryption to work properly (i.e.,
6017 * to not encrypt the frame), we need to tell mac80211 about
6018 * the frames that must not be encrypted.
6019 */
6020 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
6021 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
6022 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
6023 encrypt = 0;
6024 }
6025
6026 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
6027 noack, freq, no_cck, offchanok,
6028 wait_time);
6029 }
6030
6031
6032 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
6033 int slot, int ht_opmode, int ap_isolate,
6034 int *basic_rates)
6035 {
6036 struct wpa_driver_nl80211_data *drv = bss->drv;
6037 struct nl_msg *msg;
6038
6039 msg = nlmsg_alloc();
6040 if (!msg)
6041 return -ENOMEM;
6042
6043 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
6044
6045 if (cts >= 0)
6046 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
6047 if (preamble >= 0)
6048 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
6049 if (slot >= 0)
6050 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
6051 if (ht_opmode >= 0)
6052 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
6053 if (ap_isolate >= 0)
6054 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
6055
6056 if (basic_rates) {
6057 u8 rates[NL80211_MAX_SUPP_RATES];
6058 u8 rates_len = 0;
6059 int i;
6060
6061 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
6062 i++)
6063 rates[rates_len++] = basic_rates[i] / 5;
6064
6065 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
6066 }
6067
6068 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6069
6070 return send_and_recv_msgs(drv, msg, NULL, NULL);
6071 nla_put_failure:
6072 nlmsg_free(msg);
6073 return -ENOBUFS;
6074 }
6075
6076
6077 static int wpa_driver_nl80211_set_acl(void *priv,
6078 struct hostapd_acl_params *params)
6079 {
6080 struct i802_bss *bss = priv;
6081 struct wpa_driver_nl80211_data *drv = bss->drv;
6082 struct nl_msg *msg;
6083 struct nlattr *acl;
6084 unsigned int i;
6085 int ret = 0;
6086
6087 if (!(drv->capa.max_acl_mac_addrs))
6088 return -ENOTSUP;
6089
6090 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
6091 return -ENOTSUP;
6092
6093 msg = nlmsg_alloc();
6094 if (!msg)
6095 return -ENOMEM;
6096
6097 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
6098 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
6099
6100 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
6101
6102 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6103
6104 NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
6105 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
6106 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
6107
6108 acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
6109 if (acl == NULL)
6110 goto nla_put_failure;
6111
6112 for (i = 0; i < params->num_mac_acl; i++)
6113 NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
6114
6115 nla_nest_end(msg, acl);
6116
6117 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6118 msg = NULL;
6119 if (ret) {
6120 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
6121 ret, strerror(-ret));
6122 }
6123
6124 nla_put_failure:
6125 nlmsg_free(msg);
6126
6127 return ret;
6128 }
6129
6130
6131 static int wpa_driver_nl80211_set_ap(void *priv,
6132 struct wpa_driver_ap_params *params)
6133 {
6134 struct i802_bss *bss = priv;
6135 struct wpa_driver_nl80211_data *drv = bss->drv;
6136 struct nl_msg *msg;
6137 u8 cmd = NL80211_CMD_NEW_BEACON;
6138 int ret;
6139 int beacon_set;
6140 int ifindex = if_nametoindex(bss->ifname);
6141 int num_suites;
6142 u32 suites[10];
6143 u32 ver;
6144
6145 beacon_set = bss->beacon_set;
6146
6147 msg = nlmsg_alloc();
6148 if (!msg)
6149 return -ENOMEM;
6150
6151 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
6152 beacon_set);
6153 if (beacon_set)
6154 cmd = NL80211_CMD_SET_BEACON;
6155
6156 nl80211_cmd(drv, msg, 0, cmd);
6157 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
6158 params->head, params->head_len);
6159 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
6160 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
6161 params->tail, params->tail_len);
6162 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
6163 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
6164 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6165 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
6166 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
6167 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
6168 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
6169 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
6170 params->ssid, params->ssid_len);
6171 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6172 params->ssid);
6173 if (params->proberesp && params->proberesp_len) {
6174 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
6175 params->proberesp, params->proberesp_len);
6176 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
6177 params->proberesp);
6178 }
6179 switch (params->hide_ssid) {
6180 case NO_SSID_HIDING:
6181 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
6182 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6183 NL80211_HIDDEN_SSID_NOT_IN_USE);
6184 break;
6185 case HIDDEN_SSID_ZERO_LEN:
6186 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
6187 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6188 NL80211_HIDDEN_SSID_ZERO_LEN);
6189 break;
6190 case HIDDEN_SSID_ZERO_CONTENTS:
6191 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
6192 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
6193 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
6194 break;
6195 }
6196 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
6197 if (params->privacy)
6198 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
6199 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
6200 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
6201 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
6202 /* Leave out the attribute */
6203 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
6204 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6205 NL80211_AUTHTYPE_SHARED_KEY);
6206 else
6207 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
6208 NL80211_AUTHTYPE_OPEN_SYSTEM);
6209
6210 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
6211 ver = 0;
6212 if (params->wpa_version & WPA_PROTO_WPA)
6213 ver |= NL80211_WPA_VERSION_1;
6214 if (params->wpa_version & WPA_PROTO_RSN)
6215 ver |= NL80211_WPA_VERSION_2;
6216 if (ver)
6217 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6218
6219 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
6220 params->key_mgmt_suites);
6221 num_suites = 0;
6222 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
6223 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
6224 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
6225 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
6226 if (num_suites) {
6227 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
6228 num_suites * sizeof(u32), suites);
6229 }
6230
6231 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
6232 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
6233 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
6234
6235 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
6236 params->pairwise_ciphers);
6237 num_suites = 0;
6238 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
6239 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
6240 if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
6241 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
6242 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
6243 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
6244 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
6245 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
6246 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
6247 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
6248 if (num_suites) {
6249 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
6250 num_suites * sizeof(u32), suites);
6251 }
6252
6253 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
6254 params->group_cipher);
6255 switch (params->group_cipher) {
6256 case WPA_CIPHER_CCMP:
6257 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6258 WLAN_CIPHER_SUITE_CCMP);
6259 break;
6260 case WPA_CIPHER_GCMP:
6261 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6262 WLAN_CIPHER_SUITE_GCMP);
6263 break;
6264 case WPA_CIPHER_TKIP:
6265 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6266 WLAN_CIPHER_SUITE_TKIP);
6267 break;
6268 case WPA_CIPHER_WEP104:
6269 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6270 WLAN_CIPHER_SUITE_WEP104);
6271 break;
6272 case WPA_CIPHER_WEP40:
6273 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
6274 WLAN_CIPHER_SUITE_WEP40);
6275 break;
6276 }
6277
6278 if (params->beacon_ies) {
6279 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
6280 params->beacon_ies);
6281 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
6282 wpabuf_head(params->beacon_ies));
6283 }
6284 if (params->proberesp_ies) {
6285 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
6286 params->proberesp_ies);
6287 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
6288 wpabuf_len(params->proberesp_ies),
6289 wpabuf_head(params->proberesp_ies));
6290 }
6291 if (params->assocresp_ies) {
6292 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
6293 params->assocresp_ies);
6294 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
6295 wpabuf_len(params->assocresp_ies),
6296 wpabuf_head(params->assocresp_ies));
6297 }
6298
6299 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
6300 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
6301 params->ap_max_inactivity);
6302 NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
6303 params->ap_max_inactivity);
6304 }
6305
6306 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6307 if (ret) {
6308 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
6309 ret, strerror(-ret));
6310 } else {
6311 bss->beacon_set = 1;
6312 nl80211_set_bss(bss, params->cts_protect, params->preamble,
6313 params->short_slot_time, params->ht_opmode,
6314 params->isolate, params->basic_rates);
6315 }
6316 return ret;
6317 nla_put_failure:
6318 nlmsg_free(msg);
6319 return -ENOBUFS;
6320 }
6321
6322
6323 static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
6324 struct hostapd_freq_params *freq)
6325 {
6326 struct wpa_driver_nl80211_data *drv = bss->drv;
6327 struct nl_msg *msg;
6328 int ret;
6329
6330 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
6331 " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6332 freq->freq, freq->ht_enabled, freq->vht_enabled,
6333 freq->bandwidth, freq->center_freq1, freq->center_freq2);
6334 msg = nlmsg_alloc();
6335 if (!msg)
6336 return -1;
6337
6338 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6339
6340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6341 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
6342 if (freq->vht_enabled) {
6343 switch (freq->bandwidth) {
6344 case 20:
6345 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6346 NL80211_CHAN_WIDTH_20);
6347 break;
6348 case 40:
6349 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6350 NL80211_CHAN_WIDTH_40);
6351 break;
6352 case 80:
6353 if (freq->center_freq2)
6354 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6355 NL80211_CHAN_WIDTH_80P80);
6356 else
6357 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6358 NL80211_CHAN_WIDTH_80);
6359 break;
6360 case 160:
6361 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
6362 NL80211_CHAN_WIDTH_160);
6363 break;
6364 default:
6365 return -1;
6366 }
6367 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
6368 if (freq->center_freq2)
6369 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
6370 freq->center_freq2);
6371 } else if (freq->ht_enabled) {
6372 switch (freq->sec_channel_offset) {
6373 case -1:
6374 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6375 NL80211_CHAN_HT40MINUS);
6376 break;
6377 case 1:
6378 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6379 NL80211_CHAN_HT40PLUS);
6380 break;
6381 default:
6382 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
6383 NL80211_CHAN_HT20);
6384 break;
6385 }
6386 }
6387
6388 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6389 msg = NULL;
6390 if (ret == 0) {
6391 bss->freq = freq->freq;
6392 return 0;
6393 }
6394 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
6395 "%d (%s)", freq->freq, ret, strerror(-ret));
6396 nla_put_failure:
6397 nlmsg_free(msg);
6398 return -1;
6399 }
6400
6401
6402 static u32 sta_flags_nl80211(int flags)
6403 {
6404 u32 f = 0;
6405
6406 if (flags & WPA_STA_AUTHORIZED)
6407 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
6408 if (flags & WPA_STA_WMM)
6409 f |= BIT(NL80211_STA_FLAG_WME);
6410 if (flags & WPA_STA_SHORT_PREAMBLE)
6411 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
6412 if (flags & WPA_STA_MFP)
6413 f |= BIT(NL80211_STA_FLAG_MFP);
6414 if (flags & WPA_STA_TDLS_PEER)
6415 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
6416
6417 return f;
6418 }
6419
6420
6421 static int wpa_driver_nl80211_sta_add(void *priv,
6422 struct hostapd_sta_add_params *params)
6423 {
6424 struct i802_bss *bss = priv;
6425 struct wpa_driver_nl80211_data *drv = bss->drv;
6426 struct nl_msg *msg;
6427 struct nl80211_sta_flag_update upd;
6428 int ret = -ENOBUFS;
6429
6430 if ((params->flags & WPA_STA_TDLS_PEER) &&
6431 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6432 return -EOPNOTSUPP;
6433
6434 msg = nlmsg_alloc();
6435 if (!msg)
6436 return -ENOMEM;
6437
6438 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
6439 params->set ? "Set" : "Add", MAC2STR(params->addr));
6440 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
6441 NL80211_CMD_NEW_STATION);
6442
6443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6444 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
6445 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
6446 params->supp_rates);
6447 wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
6448 params->supp_rates_len);
6449 if (!params->set) {
6450 if (params->aid) {
6451 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
6452 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
6453 } else {
6454 /*
6455 * cfg80211 validates that AID is non-zero, so we have
6456 * to make this a non-zero value for the TDLS case where
6457 * a dummy STA entry is used for now.
6458 */
6459 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
6460 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
6461 }
6462 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
6463 params->listen_interval);
6464 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
6465 params->listen_interval);
6466 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
6467 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
6468 NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
6469 }
6470 if (params->ht_capabilities) {
6471 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
6472 (u8 *) params->ht_capabilities,
6473 sizeof(*params->ht_capabilities));
6474 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
6475 sizeof(*params->ht_capabilities),
6476 params->ht_capabilities);
6477 }
6478
6479 if (params->vht_capabilities) {
6480 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
6481 (u8 *) params->vht_capabilities,
6482 sizeof(*params->vht_capabilities));
6483 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
6484 sizeof(*params->vht_capabilities),
6485 params->vht_capabilities);
6486 }
6487
6488 wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
6489 NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
6490
6491 if (params->ext_capab) {
6492 wpa_hexdump(MSG_DEBUG, " * ext_capab",
6493 params->ext_capab, params->ext_capab_len);
6494 NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
6495 params->ext_capab_len, params->ext_capab);
6496 }
6497
6498 os_memset(&upd, 0, sizeof(upd));
6499 upd.mask = sta_flags_nl80211(params->flags);
6500 upd.set = upd.mask;
6501 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
6502 upd.set, upd.mask);
6503 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6504
6505 if (params->flags & WPA_STA_WMM) {
6506 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
6507
6508 if (!wme)
6509 goto nla_put_failure;
6510
6511 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
6512 NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
6513 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
6514 NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
6515 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
6516 WMM_QOSINFO_STA_SP_MASK);
6517 nla_nest_end(msg, wme);
6518 }
6519
6520 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6521 msg = NULL;
6522 if (ret)
6523 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
6524 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
6525 strerror(-ret));
6526 if (ret == -EEXIST)
6527 ret = 0;
6528 nla_put_failure:
6529 nlmsg_free(msg);
6530 return ret;
6531 }
6532
6533
6534 static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
6535 {
6536 struct wpa_driver_nl80211_data *drv = bss->drv;
6537 struct nl_msg *msg;
6538 int ret;
6539
6540 msg = nlmsg_alloc();
6541 if (!msg)
6542 return -ENOMEM;
6543
6544 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
6545
6546 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6547 if_nametoindex(bss->ifname));
6548 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6549
6550 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6551 if (ret == -ENOENT)
6552 return 0;
6553 return ret;
6554 nla_put_failure:
6555 nlmsg_free(msg);
6556 return -ENOBUFS;
6557 }
6558
6559
6560 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
6561 int ifidx)
6562 {
6563 struct nl_msg *msg;
6564
6565 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
6566
6567 /* stop listening for EAPOL on this interface */
6568 del_ifidx(drv, ifidx);
6569
6570 msg = nlmsg_alloc();
6571 if (!msg)
6572 goto nla_put_failure;
6573
6574 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
6575 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
6576
6577 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6578 return;
6579 msg = NULL;
6580 nla_put_failure:
6581 nlmsg_free(msg);
6582 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
6583 }
6584
6585
6586 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
6587 {
6588 switch (mode) {
6589 case NL80211_IFTYPE_ADHOC:
6590 return "ADHOC";
6591 case NL80211_IFTYPE_STATION:
6592 return "STATION";
6593 case NL80211_IFTYPE_AP:
6594 return "AP";
6595 case NL80211_IFTYPE_AP_VLAN:
6596 return "AP_VLAN";
6597 case NL80211_IFTYPE_WDS:
6598 return "WDS";
6599 case NL80211_IFTYPE_MONITOR:
6600 return "MONITOR";
6601 case NL80211_IFTYPE_MESH_POINT:
6602 return "MESH_POINT";
6603 case NL80211_IFTYPE_P2P_CLIENT:
6604 return "P2P_CLIENT";
6605 case NL80211_IFTYPE_P2P_GO:
6606 return "P2P_GO";
6607 case NL80211_IFTYPE_P2P_DEVICE:
6608 return "P2P_DEVICE";
6609 default:
6610 return "unknown";
6611 }
6612 }
6613
6614
6615 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
6616 const char *ifname,
6617 enum nl80211_iftype iftype,
6618 const u8 *addr, int wds,
6619 int (*handler)(struct nl_msg *, void *),
6620 void *arg)
6621 {
6622 struct nl_msg *msg;
6623 int ifidx;
6624 int ret = -ENOBUFS;
6625
6626 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
6627 iftype, nl80211_iftype_str(iftype));
6628
6629 msg = nlmsg_alloc();
6630 if (!msg)
6631 return -1;
6632
6633 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
6634 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6635 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
6636 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
6637
6638 if (iftype == NL80211_IFTYPE_MONITOR) {
6639 struct nlattr *flags;
6640
6641 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
6642 if (!flags)
6643 goto nla_put_failure;
6644
6645 NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
6646
6647 nla_nest_end(msg, flags);
6648 } else if (wds) {
6649 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
6650 }
6651
6652 ret = send_and_recv_msgs(drv, msg, handler, arg);
6653 msg = NULL;
6654 if (ret) {
6655 nla_put_failure:
6656 nlmsg_free(msg);
6657 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
6658 ifname, ret, strerror(-ret));
6659 return ret;
6660 }
6661
6662 if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
6663 wpa_printf(MSG_DEBUG,
6664 "nl80211: New P2P Device interface %s created",
6665 ifname);
6666 return 0;
6667 }
6668
6669 ifidx = if_nametoindex(ifname);
6670 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
6671 ifname, ifidx);
6672
6673 if (ifidx <= 0)
6674 return -1;
6675
6676 /* start listening for EAPOL on this interface */
6677 add_ifidx(drv, ifidx);
6678
6679 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
6680 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
6681 nl80211_remove_iface(drv, ifidx);
6682 return -1;
6683 }
6684
6685 return ifidx;
6686 }
6687
6688
6689 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
6690 const char *ifname, enum nl80211_iftype iftype,
6691 const u8 *addr, int wds,
6692 int (*handler)(struct nl_msg *, void *),
6693 void *arg)
6694 {
6695 int ret;
6696
6697 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
6698 arg);
6699
6700 /* if error occurred and interface exists already */
6701 if (ret == -ENFILE && if_nametoindex(ifname)) {
6702 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
6703
6704 /* Try to remove the interface that was already there. */
6705 nl80211_remove_iface(drv, if_nametoindex(ifname));
6706
6707 /* Try to create the interface again */
6708 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
6709 wds, handler, arg);
6710 }
6711
6712 if (ret >= 0 && is_p2p_net_interface(iftype))
6713 nl80211_disable_11b_rates(drv, ret, 1);
6714
6715 return ret;
6716 }
6717
6718
6719 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
6720 {
6721 struct ieee80211_hdr *hdr;
6722 u16 fc;
6723 union wpa_event_data event;
6724
6725 hdr = (struct ieee80211_hdr *) buf;
6726 fc = le_to_host16(hdr->frame_control);
6727
6728 os_memset(&event, 0, sizeof(event));
6729 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
6730 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
6731 event.tx_status.dst = hdr->addr1;
6732 event.tx_status.data = buf;
6733 event.tx_status.data_len = len;
6734 event.tx_status.ack = ok;
6735 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
6736 }
6737
6738
6739 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
6740 u8 *buf, size_t len)
6741 {
6742 struct ieee80211_hdr *hdr = (void *)buf;
6743 u16 fc;
6744 union wpa_event_data event;
6745
6746 if (len < sizeof(*hdr))
6747 return;
6748
6749 fc = le_to_host16(hdr->frame_control);
6750
6751 os_memset(&event, 0, sizeof(event));
6752 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
6753 event.rx_from_unknown.addr = hdr->addr2;
6754 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
6755 (WLAN_FC_FROMDS | WLAN_FC_TODS);
6756 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
6757 }
6758
6759
6760 static void handle_frame(struct wpa_driver_nl80211_data *drv,
6761 u8 *buf, size_t len, int datarate, int ssi_signal)
6762 {
6763 struct ieee80211_hdr *hdr;
6764 u16 fc;
6765 union wpa_event_data event;
6766
6767 hdr = (struct ieee80211_hdr *) buf;
6768 fc = le_to_host16(hdr->frame_control);
6769
6770 switch (WLAN_FC_GET_TYPE(fc)) {
6771 case WLAN_FC_TYPE_MGMT:
6772 os_memset(&event, 0, sizeof(event));
6773 event.rx_mgmt.frame = buf;
6774 event.rx_mgmt.frame_len = len;
6775 event.rx_mgmt.datarate = datarate;
6776 event.rx_mgmt.ssi_signal = ssi_signal;
6777 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
6778 break;
6779 case WLAN_FC_TYPE_CTRL:
6780 /* can only get here with PS-Poll frames */
6781 wpa_printf(MSG_DEBUG, "CTRL");
6782 from_unknown_sta(drv, buf, len);
6783 break;
6784 case WLAN_FC_TYPE_DATA:
6785 from_unknown_sta(drv, buf, len);
6786 break;
6787 }
6788 }
6789
6790
6791 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
6792 {
6793 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6794 int len;
6795 unsigned char buf[3000];
6796 struct ieee80211_radiotap_iterator iter;
6797 int ret;
6798 int datarate = 0, ssi_signal = 0;
6799 int injected = 0, failed = 0, rxflags = 0;
6800
6801 len = recv(sock, buf, sizeof(buf), 0);
6802 if (len < 0) {
6803 perror("recv");
6804 return;
6805 }
6806
6807 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
6808 printf("received invalid radiotap frame\n");
6809 return;
6810 }
6811
6812 while (1) {
6813 ret = ieee80211_radiotap_iterator_next(&iter);
6814 if (ret == -ENOENT)
6815 break;
6816 if (ret) {
6817 printf("received invalid radiotap frame (%d)\n", ret);
6818 return;
6819 }
6820 switch (iter.this_arg_index) {
6821 case IEEE80211_RADIOTAP_FLAGS:
6822 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
6823 len -= 4;
6824 break;
6825 case IEEE80211_RADIOTAP_RX_FLAGS:
6826 rxflags = 1;
6827 break;
6828 case IEEE80211_RADIOTAP_TX_FLAGS:
6829 injected = 1;
6830 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
6831 IEEE80211_RADIOTAP_F_TX_FAIL;
6832 break;
6833 case IEEE80211_RADIOTAP_DATA_RETRIES:
6834 break;
6835 case IEEE80211_RADIOTAP_CHANNEL:
6836 /* TODO: convert from freq/flags to channel number */
6837 break;
6838 case IEEE80211_RADIOTAP_RATE:
6839 datarate = *iter.this_arg * 5;
6840 break;
6841 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
6842 ssi_signal = (s8) *iter.this_arg;
6843 break;
6844 }
6845 }
6846
6847 if (rxflags && injected)
6848 return;
6849
6850 if (!injected)
6851 handle_frame(drv, buf + iter.max_length,
6852 len - iter.max_length, datarate, ssi_signal);
6853 else
6854 handle_tx_callback(drv->ctx, buf + iter.max_length,
6855 len - iter.max_length, !failed);
6856 }
6857
6858
6859 /*
6860 * we post-process the filter code later and rewrite
6861 * this to the offset to the last instruction
6862 */
6863 #define PASS 0xFF
6864 #define FAIL 0xFE
6865
6866 static struct sock_filter msock_filter_insns[] = {
6867 /*
6868 * do a little-endian load of the radiotap length field
6869 */
6870 /* load lower byte into A */
6871 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6872 /* put it into X (== index register) */
6873 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6874 /* load upper byte into A */
6875 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6876 /* left-shift it by 8 */
6877 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6878 /* or with X */
6879 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6880 /* put result into X */
6881 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6882
6883 /*
6884 * Allow management frames through, this also gives us those
6885 * management frames that we sent ourselves with status
6886 */
6887 /* load the lower byte of the IEEE 802.11 frame control field */
6888 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6889 /* mask off frame type and version */
6890 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6891 /* accept frame if it's both 0, fall through otherwise */
6892 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6893
6894 /*
6895 * TODO: add a bit to radiotap RX flags that indicates
6896 * that the sending station is not associated, then
6897 * add a filter here that filters on our DA and that flag
6898 * to allow us to deauth frames to that bad station.
6899 *
6900 * For now allow all To DS data frames through.
6901 */
6902 /* load the IEEE 802.11 frame control field */
6903 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6904 /* mask off frame type, version and DS status */
6905 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6906 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6907 */
6908 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6909
6910 #if 0
6911 /*
6912 * drop non-data frames
6913 */
6914 /* load the lower byte of the frame control field */
6915 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6916 /* mask off QoS bit */
6917 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6918 /* drop non-data frames */
6919 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6920 #endif
6921 /* load the upper byte of the frame control field */
6922 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6923 /* mask off toDS/fromDS */
6924 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6925 /* accept WDS frames */
6926 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6927
6928 /*
6929 * add header length to index
6930 */
6931 /* load the lower byte of the frame control field */
6932 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6933 /* mask off QoS bit */
6934 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6935 /* right shift it by 6 to give 0 or 2 */
6936 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6937 /* add data frame header length */
6938 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6939 /* add index, was start of 802.11 header */
6940 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6941 /* move to index, now start of LL header */
6942 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6943
6944 /*
6945 * Accept empty data frames, we use those for
6946 * polling activity.
6947 */
6948 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6949 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6950
6951 /*
6952 * Accept EAPOL frames
6953 */
6954 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6955 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6956 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6957 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6958
6959 /* keep these last two statements or change the code below */
6960 /* return 0 == "DROP" */
6961 BPF_STMT(BPF_RET | BPF_K, 0),
6962 /* return ~0 == "keep all" */
6963 BPF_STMT(BPF_RET | BPF_K, ~0),
6964 };
6965
6966 static struct sock_fprog msock_filter = {
6967 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6968 .filter = msock_filter_insns,
6969 };
6970
6971
6972 static int add_monitor_filter(int s)
6973 {
6974 int idx;
6975
6976 /* rewrite all PASS/FAIL jump offsets */
6977 for (idx = 0; idx < msock_filter.len; idx++) {
6978 struct sock_filter *insn = &msock_filter_insns[idx];
6979
6980 if (BPF_CLASS(insn->code) == BPF_JMP) {
6981 if (insn->code == (BPF_JMP|BPF_JA)) {
6982 if (insn->k == PASS)
6983 insn->k = msock_filter.len - idx - 2;
6984 else if (insn->k == FAIL)
6985 insn->k = msock_filter.len - idx - 3;
6986 }
6987
6988 if (insn->jt == PASS)
6989 insn->jt = msock_filter.len - idx - 2;
6990 else if (insn->jt == FAIL)
6991 insn->jt = msock_filter.len - idx - 3;
6992
6993 if (insn->jf == PASS)
6994 insn->jf = msock_filter.len - idx - 2;
6995 else if (insn->jf == FAIL)
6996 insn->jf = msock_filter.len - idx - 3;
6997 }
6998 }
6999
7000 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
7001 &msock_filter, sizeof(msock_filter))) {
7002 perror("SO_ATTACH_FILTER");
7003 return -1;
7004 }
7005
7006 return 0;
7007 }
7008
7009
7010 static void nl80211_remove_monitor_interface(
7011 struct wpa_driver_nl80211_data *drv)
7012 {
7013 drv->monitor_refcount--;
7014 if (drv->monitor_refcount > 0)
7015 return;
7016
7017 if (drv->monitor_ifidx >= 0) {
7018 nl80211_remove_iface(drv, drv->monitor_ifidx);
7019 drv->monitor_ifidx = -1;
7020 }
7021 if (drv->monitor_sock >= 0) {
7022 eloop_unregister_read_sock(drv->monitor_sock);
7023 close(drv->monitor_sock);
7024 drv->monitor_sock = -1;
7025 }
7026 }
7027
7028
7029 static int
7030 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
7031 {
7032 char buf[IFNAMSIZ];
7033 struct sockaddr_ll ll;
7034 int optval;
7035 socklen_t optlen;
7036
7037 if (drv->monitor_ifidx >= 0) {
7038 drv->monitor_refcount++;
7039 return 0;
7040 }
7041
7042 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
7043 /*
7044 * P2P interface name is of the format p2p-%s-%d. For monitor
7045 * interface name corresponding to P2P GO, replace "p2p-" with
7046 * "mon-" to retain the same interface name length and to
7047 * indicate that it is a monitor interface.
7048 */
7049 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
7050 } else {
7051 /* Non-P2P interface with AP functionality. */
7052 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
7053 }
7054
7055 buf[IFNAMSIZ - 1] = '\0';
7056
7057 drv->monitor_ifidx =
7058 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
7059 0, NULL, NULL);
7060
7061 if (drv->monitor_ifidx == -EOPNOTSUPP) {
7062 /*
7063 * This is backward compatibility for a few versions of
7064 * the kernel only that didn't advertise the right
7065 * attributes for the only driver that then supported
7066 * AP mode w/o monitor -- ath6kl.
7067 */
7068 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
7069 "monitor interface type - try to run without it");
7070 drv->device_ap_sme = 1;
7071 }
7072
7073 if (drv->monitor_ifidx < 0)
7074 return -1;
7075
7076 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
7077 goto error;
7078
7079 memset(&ll, 0, sizeof(ll));
7080 ll.sll_family = AF_PACKET;
7081 ll.sll_ifindex = drv->monitor_ifidx;
7082 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
7083 if (drv->monitor_sock < 0) {
7084 perror("socket[PF_PACKET,SOCK_RAW]");
7085 goto error;
7086 }
7087
7088 if (add_monitor_filter(drv->monitor_sock)) {
7089 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
7090 "interface; do filtering in user space");
7091 /* This works, but will cost in performance. */
7092 }
7093
7094 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
7095 perror("monitor socket bind");
7096 goto error;
7097 }
7098
7099 optlen = sizeof(optval);
7100 optval = 20;
7101 if (setsockopt
7102 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
7103 perror("Failed to set socket priority");
7104 goto error;
7105 }
7106
7107 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
7108 drv, NULL)) {
7109 printf("Could not register monitor read socket\n");
7110 goto error;
7111 }
7112
7113 return 0;
7114 error:
7115 nl80211_remove_monitor_interface(drv);
7116 return -1;
7117 }
7118
7119
7120 static int nl80211_setup_ap(struct i802_bss *bss)
7121 {
7122 struct wpa_driver_nl80211_data *drv = bss->drv;
7123
7124 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
7125 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
7126
7127 /*
7128 * Disable Probe Request reporting unless we need it in this way for
7129 * devices that include the AP SME, in the other case (unless using
7130 * monitor iface) we'll get it through the nl_mgmt socket instead.
7131 */
7132 if (!drv->device_ap_sme)
7133 wpa_driver_nl80211_probe_req_report(bss, 0);
7134
7135 if (!drv->device_ap_sme && !drv->use_monitor)
7136 if (nl80211_mgmt_subscribe_ap(bss))
7137 return -1;
7138
7139 if (drv->device_ap_sme && !drv->use_monitor)
7140 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
7141 return -1;
7142
7143 if (!drv->device_ap_sme && drv->use_monitor &&
7144 nl80211_create_monitor_interface(drv) &&
7145 !drv->device_ap_sme)
7146 return -1;
7147
7148 if (drv->device_ap_sme &&
7149 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
7150 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
7151 "Probe Request frame reporting in AP mode");
7152 /* Try to survive without this */
7153 }
7154
7155 return 0;
7156 }
7157
7158
7159 static void nl80211_teardown_ap(struct i802_bss *bss)
7160 {
7161 struct wpa_driver_nl80211_data *drv = bss->drv;
7162
7163 if (drv->device_ap_sme) {
7164 wpa_driver_nl80211_probe_req_report(bss, 0);
7165 if (!drv->use_monitor)
7166 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
7167 } else if (drv->use_monitor)
7168 nl80211_remove_monitor_interface(drv);
7169 else
7170 nl80211_mgmt_unsubscribe(bss, "AP teardown");
7171
7172 bss->beacon_set = 0;
7173 }
7174
7175
7176 static int nl80211_send_eapol_data(struct i802_bss *bss,
7177 const u8 *addr, const u8 *data,
7178 size_t data_len)
7179 {
7180 struct sockaddr_ll ll;
7181 int ret;
7182
7183 if (bss->drv->eapol_tx_sock < 0) {
7184 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
7185 return -1;
7186 }
7187
7188 os_memset(&ll, 0, sizeof(ll));
7189 ll.sll_family = AF_PACKET;
7190 ll.sll_ifindex = bss->ifindex;
7191 ll.sll_protocol = htons(ETH_P_PAE);
7192 ll.sll_halen = ETH_ALEN;
7193 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
7194 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
7195 (struct sockaddr *) &ll, sizeof(ll));
7196 if (ret < 0)
7197 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
7198 strerror(errno));
7199
7200 return ret;
7201 }
7202
7203
7204 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
7205
7206 static int wpa_driver_nl80211_hapd_send_eapol(
7207 void *priv, const u8 *addr, const u8 *data,
7208 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
7209 {
7210 struct i802_bss *bss = priv;
7211 struct wpa_driver_nl80211_data *drv = bss->drv;
7212 struct ieee80211_hdr *hdr;
7213 size_t len;
7214 u8 *pos;
7215 int res;
7216 int qos = flags & WPA_STA_WMM;
7217
7218 if (drv->device_ap_sme || !drv->use_monitor)
7219 return nl80211_send_eapol_data(bss, addr, data, data_len);
7220
7221 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
7222 data_len;
7223 hdr = os_zalloc(len);
7224 if (hdr == NULL) {
7225 printf("malloc() failed for i802_send_data(len=%lu)\n",
7226 (unsigned long) len);
7227 return -1;
7228 }
7229
7230 hdr->frame_control =
7231 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
7232 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
7233 if (encrypt)
7234 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
7235 if (qos) {
7236 hdr->frame_control |=
7237 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
7238 }
7239
7240 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7241 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7242 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7243 pos = (u8 *) (hdr + 1);
7244
7245 if (qos) {
7246 /* Set highest priority in QoS header */
7247 pos[0] = 7;
7248 pos[1] = 0;
7249 pos += 2;
7250 }
7251
7252 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
7253 pos += sizeof(rfc1042_header);
7254 WPA_PUT_BE16(pos, ETH_P_PAE);
7255 pos += 2;
7256 memcpy(pos, data, data_len);
7257
7258 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
7259 0, 0, 0, 0);
7260 if (res < 0) {
7261 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
7262 "failed: %d (%s)",
7263 (unsigned long) len, errno, strerror(errno));
7264 }
7265 os_free(hdr);
7266
7267 return res;
7268 }
7269
7270
7271 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
7272 int total_flags,
7273 int flags_or, int flags_and)
7274 {
7275 struct i802_bss *bss = priv;
7276 struct wpa_driver_nl80211_data *drv = bss->drv;
7277 struct nl_msg *msg;
7278 struct nlattr *flags;
7279 struct nl80211_sta_flag_update upd;
7280
7281 msg = nlmsg_alloc();
7282 if (!msg)
7283 return -ENOMEM;
7284
7285 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7286
7287 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7288 if_nametoindex(bss->ifname));
7289 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7290
7291 /*
7292 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
7293 * can be removed eventually.
7294 */
7295 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
7296 if (!flags)
7297 goto nla_put_failure;
7298 if (total_flags & WPA_STA_AUTHORIZED)
7299 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
7300
7301 if (total_flags & WPA_STA_WMM)
7302 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
7303
7304 if (total_flags & WPA_STA_SHORT_PREAMBLE)
7305 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
7306
7307 if (total_flags & WPA_STA_MFP)
7308 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
7309
7310 if (total_flags & WPA_STA_TDLS_PEER)
7311 NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
7312
7313 nla_nest_end(msg, flags);
7314
7315 os_memset(&upd, 0, sizeof(upd));
7316 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
7317 upd.set = sta_flags_nl80211(flags_or);
7318 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7319
7320 return send_and_recv_msgs(drv, msg, NULL, NULL);
7321 nla_put_failure:
7322 nlmsg_free(msg);
7323 return -ENOBUFS;
7324 }
7325
7326
7327 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
7328 struct wpa_driver_associate_params *params)
7329 {
7330 enum nl80211_iftype nlmode, old_mode;
7331 struct hostapd_freq_params freq = {
7332 .freq = params->freq,
7333 };
7334
7335 if (params->p2p) {
7336 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
7337 "group (GO)");
7338 nlmode = NL80211_IFTYPE_P2P_GO;
7339 } else
7340 nlmode = NL80211_IFTYPE_AP;
7341
7342 old_mode = drv->nlmode;
7343 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
7344 nl80211_remove_monitor_interface(drv);
7345 return -1;
7346 }
7347
7348 if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
7349 if (old_mode != nlmode)
7350 wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
7351 nl80211_remove_monitor_interface(drv);
7352 return -1;
7353 }
7354
7355 return 0;
7356 }
7357
7358
7359 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
7360 {
7361 struct nl_msg *msg;
7362 int ret = -1;
7363
7364 msg = nlmsg_alloc();
7365 if (!msg)
7366 return -1;
7367
7368 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
7369 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7370 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7371 msg = NULL;
7372 if (ret) {
7373 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
7374 "(%s)", ret, strerror(-ret));
7375 goto nla_put_failure;
7376 }
7377
7378 ret = 0;
7379 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
7380
7381 nla_put_failure:
7382 nlmsg_free(msg);
7383 return ret;
7384 }
7385
7386
7387 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
7388 struct wpa_driver_associate_params *params)
7389 {
7390 struct nl_msg *msg;
7391 int ret = -1;
7392 int count = 0;
7393
7394 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
7395
7396 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
7397 NL80211_IFTYPE_ADHOC)) {
7398 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
7399 "IBSS mode");
7400 return -1;
7401 }
7402
7403 retry:
7404 msg = nlmsg_alloc();
7405 if (!msg)
7406 return -1;
7407
7408 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
7409 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7410
7411 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
7412 goto nla_put_failure;
7413
7414 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7415 params->ssid, params->ssid_len);
7416 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7417 params->ssid);
7418 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7419 drv->ssid_len = params->ssid_len;
7420
7421 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7422 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7423
7424 ret = nl80211_set_conn_keys(params, msg);
7425 if (ret)
7426 goto nla_put_failure;
7427
7428 if (params->bssid && params->fixed_bssid) {
7429 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
7430 MAC2STR(params->bssid));
7431 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7432 }
7433
7434 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7435 params->key_mgmt_suite == KEY_MGMT_PSK ||
7436 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
7437 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
7438 wpa_printf(MSG_DEBUG, " * control port");
7439 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7440 }
7441
7442 if (params->wpa_ie) {
7443 wpa_hexdump(MSG_DEBUG,
7444 " * Extra IEs for Beacon/Probe Response frames",
7445 params->wpa_ie, params->wpa_ie_len);
7446 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7447 params->wpa_ie);
7448 }
7449
7450 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7451 msg = NULL;
7452 if (ret) {
7453 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
7454 ret, strerror(-ret));
7455 count++;
7456 if (ret == -EALREADY && count == 1) {
7457 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
7458 "forced leave");
7459 nl80211_leave_ibss(drv);
7460 nlmsg_free(msg);
7461 goto retry;
7462 }
7463
7464 goto nla_put_failure;
7465 }
7466 ret = 0;
7467 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
7468
7469 nla_put_failure:
7470 nlmsg_free(msg);
7471 return ret;
7472 }
7473
7474
7475 static int wpa_driver_nl80211_try_connect(
7476 struct wpa_driver_nl80211_data *drv,
7477 struct wpa_driver_associate_params *params)
7478 {
7479 struct nl_msg *msg;
7480 enum nl80211_auth_type type;
7481 int ret = 0;
7482 int algs;
7483
7484 msg = nlmsg_alloc();
7485 if (!msg)
7486 return -1;
7487
7488 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
7489 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
7490
7491 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7492 if (params->bssid) {
7493 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7494 MAC2STR(params->bssid));
7495 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7496 }
7497 if (params->freq) {
7498 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7499 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7500 }
7501 if (params->bg_scan_period >= 0) {
7502 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7503 params->bg_scan_period);
7504 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7505 params->bg_scan_period);
7506 }
7507 if (params->ssid) {
7508 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7509 params->ssid, params->ssid_len);
7510 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7511 params->ssid);
7512 if (params->ssid_len > sizeof(drv->ssid))
7513 goto nla_put_failure;
7514 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7515 drv->ssid_len = params->ssid_len;
7516 }
7517 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7518 if (params->wpa_ie)
7519 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7520 params->wpa_ie);
7521
7522 algs = 0;
7523 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7524 algs++;
7525 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7526 algs++;
7527 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7528 algs++;
7529 if (algs > 1) {
7530 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
7531 "selection");
7532 goto skip_auth_type;
7533 }
7534
7535 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
7536 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
7537 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
7538 type = NL80211_AUTHTYPE_SHARED_KEY;
7539 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
7540 type = NL80211_AUTHTYPE_NETWORK_EAP;
7541 else if (params->auth_alg & WPA_AUTH_ALG_FT)
7542 type = NL80211_AUTHTYPE_FT;
7543 else
7544 goto nla_put_failure;
7545
7546 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
7547 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
7548
7549 skip_auth_type:
7550 if (params->wpa_proto) {
7551 enum nl80211_wpa_versions ver = 0;
7552
7553 if (params->wpa_proto & WPA_PROTO_WPA)
7554 ver |= NL80211_WPA_VERSION_1;
7555 if (params->wpa_proto & WPA_PROTO_RSN)
7556 ver |= NL80211_WPA_VERSION_2;
7557
7558 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
7559 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
7560 }
7561
7562 if (params->pairwise_suite != CIPHER_NONE) {
7563 int cipher;
7564
7565 switch (params->pairwise_suite) {
7566 case CIPHER_SMS4:
7567 cipher = WLAN_CIPHER_SUITE_SMS4;
7568 break;
7569 case CIPHER_WEP40:
7570 cipher = WLAN_CIPHER_SUITE_WEP40;
7571 break;
7572 case CIPHER_WEP104:
7573 cipher = WLAN_CIPHER_SUITE_WEP104;
7574 break;
7575 case CIPHER_CCMP:
7576 cipher = WLAN_CIPHER_SUITE_CCMP;
7577 break;
7578 case CIPHER_GCMP:
7579 cipher = WLAN_CIPHER_SUITE_GCMP;
7580 break;
7581 case CIPHER_TKIP:
7582 default:
7583 cipher = WLAN_CIPHER_SUITE_TKIP;
7584 break;
7585 }
7586 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7587 }
7588
7589 if (params->group_suite != CIPHER_NONE) {
7590 int cipher;
7591
7592 switch (params->group_suite) {
7593 case CIPHER_SMS4:
7594 cipher = WLAN_CIPHER_SUITE_SMS4;
7595 break;
7596 case CIPHER_WEP40:
7597 cipher = WLAN_CIPHER_SUITE_WEP40;
7598 break;
7599 case CIPHER_WEP104:
7600 cipher = WLAN_CIPHER_SUITE_WEP104;
7601 break;
7602 case CIPHER_CCMP:
7603 cipher = WLAN_CIPHER_SUITE_CCMP;
7604 break;
7605 case CIPHER_GCMP:
7606 cipher = WLAN_CIPHER_SUITE_GCMP;
7607 break;
7608 case CIPHER_TKIP:
7609 default:
7610 cipher = WLAN_CIPHER_SUITE_TKIP;
7611 break;
7612 }
7613 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7614 }
7615
7616 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
7617 params->key_mgmt_suite == KEY_MGMT_PSK ||
7618 params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
7619 params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
7620 params->key_mgmt_suite == KEY_MGMT_CCKM) {
7621 int mgmt = WLAN_AKM_SUITE_PSK;
7622
7623 switch (params->key_mgmt_suite) {
7624 case KEY_MGMT_CCKM:
7625 mgmt = WLAN_AKM_SUITE_CCKM;
7626 break;
7627 case KEY_MGMT_802_1X:
7628 mgmt = WLAN_AKM_SUITE_8021X;
7629 break;
7630 case KEY_MGMT_FT_802_1X:
7631 mgmt = WLAN_AKM_SUITE_FT_8021X;
7632 break;
7633 case KEY_MGMT_FT_PSK:
7634 mgmt = WLAN_AKM_SUITE_FT_PSK;
7635 break;
7636 case KEY_MGMT_PSK:
7637 default:
7638 mgmt = WLAN_AKM_SUITE_PSK;
7639 break;
7640 }
7641 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
7642 }
7643
7644 #ifdef CONFIG_IEEE80211W
7645 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7646 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7647 #endif /* CONFIG_IEEE80211W */
7648
7649 if (params->disable_ht)
7650 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7651
7652 if (params->htcaps && params->htcaps_mask) {
7653 int sz = sizeof(struct ieee80211_ht_capabilities);
7654 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7655 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7656 params->htcaps_mask);
7657 }
7658
7659 #ifdef CONFIG_VHT_OVERRIDES
7660 if (params->disable_vht) {
7661 wpa_printf(MSG_DEBUG, " * VHT disabled");
7662 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7663 }
7664
7665 if (params->vhtcaps && params->vhtcaps_mask) {
7666 int sz = sizeof(struct ieee80211_vht_capabilities);
7667 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7668 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7669 params->vhtcaps_mask);
7670 }
7671 #endif /* CONFIG_VHT_OVERRIDES */
7672
7673 ret = nl80211_set_conn_keys(params, msg);
7674 if (ret)
7675 goto nla_put_failure;
7676
7677 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7678 msg = NULL;
7679 if (ret) {
7680 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
7681 "(%s)", ret, strerror(-ret));
7682 goto nla_put_failure;
7683 }
7684 ret = 0;
7685 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
7686
7687 nla_put_failure:
7688 nlmsg_free(msg);
7689 return ret;
7690
7691 }
7692
7693
7694 static int wpa_driver_nl80211_connect(
7695 struct wpa_driver_nl80211_data *drv,
7696 struct wpa_driver_associate_params *params)
7697 {
7698 int ret = wpa_driver_nl80211_try_connect(drv, params);
7699 if (ret == -EALREADY) {
7700 /*
7701 * cfg80211 does not currently accept new connections if
7702 * we are already connected. As a workaround, force
7703 * disconnection and try again.
7704 */
7705 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
7706 "disconnecting before reassociation "
7707 "attempt");
7708 if (wpa_driver_nl80211_disconnect(
7709 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
7710 return -1;
7711 /* Ignore the next local disconnect message. */
7712 drv->ignore_next_local_disconnect = 1;
7713 ret = wpa_driver_nl80211_try_connect(drv, params);
7714 }
7715 return ret;
7716 }
7717
7718
7719 static int wpa_driver_nl80211_associate(
7720 void *priv, struct wpa_driver_associate_params *params)
7721 {
7722 struct i802_bss *bss = priv;
7723 struct wpa_driver_nl80211_data *drv = bss->drv;
7724 int ret = -1;
7725 struct nl_msg *msg;
7726
7727 if (params->mode == IEEE80211_MODE_AP)
7728 return wpa_driver_nl80211_ap(drv, params);
7729
7730 if (params->mode == IEEE80211_MODE_IBSS)
7731 return wpa_driver_nl80211_ibss(drv, params);
7732
7733 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
7734 enum nl80211_iftype nlmode = params->p2p ?
7735 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
7736
7737 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
7738 return -1;
7739 return wpa_driver_nl80211_connect(drv, params);
7740 }
7741
7742 nl80211_mark_disconnected(drv);
7743
7744 msg = nlmsg_alloc();
7745 if (!msg)
7746 return -1;
7747
7748 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
7749 drv->ifindex);
7750 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
7751
7752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7753 if (params->bssid) {
7754 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
7755 MAC2STR(params->bssid));
7756 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
7757 }
7758 if (params->freq) {
7759 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
7760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
7761 drv->assoc_freq = params->freq;
7762 } else
7763 drv->assoc_freq = 0;
7764 if (params->bg_scan_period >= 0) {
7765 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
7766 params->bg_scan_period);
7767 NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
7768 params->bg_scan_period);
7769 }
7770 if (params->ssid) {
7771 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
7772 params->ssid, params->ssid_len);
7773 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
7774 params->ssid);
7775 if (params->ssid_len > sizeof(drv->ssid))
7776 goto nla_put_failure;
7777 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
7778 drv->ssid_len = params->ssid_len;
7779 }
7780 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
7781 if (params->wpa_ie)
7782 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
7783 params->wpa_ie);
7784
7785 if (params->pairwise_suite != CIPHER_NONE) {
7786 int cipher;
7787
7788 switch (params->pairwise_suite) {
7789 case CIPHER_WEP40:
7790 cipher = WLAN_CIPHER_SUITE_WEP40;
7791 break;
7792 case CIPHER_WEP104:
7793 cipher = WLAN_CIPHER_SUITE_WEP104;
7794 break;
7795 case CIPHER_CCMP:
7796 cipher = WLAN_CIPHER_SUITE_CCMP;
7797 break;
7798 case CIPHER_GCMP:
7799 cipher = WLAN_CIPHER_SUITE_GCMP;
7800 break;
7801 case CIPHER_TKIP:
7802 default:
7803 cipher = WLAN_CIPHER_SUITE_TKIP;
7804 break;
7805 }
7806 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
7807 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
7808 }
7809
7810 if (params->group_suite != CIPHER_NONE) {
7811 int cipher;
7812
7813 switch (params->group_suite) {
7814 case CIPHER_WEP40:
7815 cipher = WLAN_CIPHER_SUITE_WEP40;
7816 break;
7817 case CIPHER_WEP104:
7818 cipher = WLAN_CIPHER_SUITE_WEP104;
7819 break;
7820 case CIPHER_CCMP:
7821 cipher = WLAN_CIPHER_SUITE_CCMP;
7822 break;
7823 case CIPHER_GCMP:
7824 cipher = WLAN_CIPHER_SUITE_GCMP;
7825 break;
7826 case CIPHER_TKIP:
7827 default:
7828 cipher = WLAN_CIPHER_SUITE_TKIP;
7829 break;
7830 }
7831 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
7832 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
7833 }
7834
7835 #ifdef CONFIG_IEEE80211W
7836 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
7837 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
7838 #endif /* CONFIG_IEEE80211W */
7839
7840 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
7841
7842 if (params->prev_bssid) {
7843 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
7844 MAC2STR(params->prev_bssid));
7845 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
7846 params->prev_bssid);
7847 }
7848
7849 if (params->disable_ht)
7850 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
7851
7852 if (params->htcaps && params->htcaps_mask) {
7853 int sz = sizeof(struct ieee80211_ht_capabilities);
7854 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
7855 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
7856 params->htcaps_mask);
7857 }
7858
7859 #ifdef CONFIG_VHT_OVERRIDES
7860 if (params->disable_vht) {
7861 wpa_printf(MSG_DEBUG, " * VHT disabled");
7862 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
7863 }
7864
7865 if (params->vhtcaps && params->vhtcaps_mask) {
7866 int sz = sizeof(struct ieee80211_vht_capabilities);
7867 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
7868 NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
7869 params->vhtcaps_mask);
7870 }
7871 #endif /* CONFIG_VHT_OVERRIDES */
7872
7873 if (params->p2p)
7874 wpa_printf(MSG_DEBUG, " * P2P group");
7875
7876 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7877 msg = NULL;
7878 if (ret) {
7879 wpa_dbg(drv->ctx, MSG_DEBUG,
7880 "nl80211: MLME command failed (assoc): ret=%d (%s)",
7881 ret, strerror(-ret));
7882 nl80211_dump_scan(drv);
7883 goto nla_put_failure;
7884 }
7885 ret = 0;
7886 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
7887 "successfully");
7888
7889 nla_put_failure:
7890 nlmsg_free(msg);
7891 return ret;
7892 }
7893
7894
7895 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
7896 int ifindex, enum nl80211_iftype mode)
7897 {
7898 struct nl_msg *msg;
7899 int ret = -ENOBUFS;
7900
7901 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
7902 ifindex, mode, nl80211_iftype_str(mode));
7903
7904 msg = nlmsg_alloc();
7905 if (!msg)
7906 return -ENOMEM;
7907
7908 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
7909 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7910 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7911
7912 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7913 msg = NULL;
7914 if (!ret)
7915 return 0;
7916 nla_put_failure:
7917 nlmsg_free(msg);
7918 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7919 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7920 return ret;
7921 }
7922
7923
7924 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7925 enum nl80211_iftype nlmode)
7926 {
7927 struct wpa_driver_nl80211_data *drv = bss->drv;
7928 int ret = -1;
7929 int i;
7930 int was_ap = is_ap_interface(drv->nlmode);
7931 int res;
7932
7933 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7934 if (res == 0) {
7935 drv->nlmode = nlmode;
7936 ret = 0;
7937 goto done;
7938 }
7939
7940 if (res == -ENODEV)
7941 return -1;
7942
7943 if (nlmode == drv->nlmode) {
7944 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7945 "requested mode - ignore error");
7946 ret = 0;
7947 goto done; /* Already in the requested mode */
7948 }
7949
7950 /* mac80211 doesn't allow mode changes while the device is up, so
7951 * take the device down, try to set the mode again, and bring the
7952 * device back up.
7953 */
7954 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7955 "interface down");
7956 for (i = 0; i < 10; i++) {
7957 res = linux_set_iface_flags(drv->global->ioctl_sock,
7958 bss->ifname, 0);
7959 if (res == -EACCES || res == -ENODEV)
7960 break;
7961 if (res == 0) {
7962 /* Try to set the mode again while the interface is
7963 * down */
7964 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
7965 if (ret == -EACCES)
7966 break;
7967 res = linux_set_iface_flags(drv->global->ioctl_sock,
7968 bss->ifname, 1);
7969 if (res && !ret)
7970 ret = -1;
7971 else if (ret != -EBUSY)
7972 break;
7973 } else
7974 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7975 "interface down");
7976 os_sleep(0, 100000);
7977 }
7978
7979 if (!ret) {
7980 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7981 "interface is down");
7982 drv->nlmode = nlmode;
7983 drv->ignore_if_down_event = 1;
7984 }
7985
7986 done:
7987 if (ret) {
7988 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7989 "from %d failed", nlmode, drv->nlmode);
7990 return ret;
7991 }
7992
7993 if (is_p2p_net_interface(nlmode))
7994 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
7995 else if (drv->disabled_11b_rates)
7996 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
7997
7998 if (is_ap_interface(nlmode)) {
7999 nl80211_mgmt_unsubscribe(bss, "start AP");
8000 /* Setup additional AP mode functionality if needed */
8001 if (nl80211_setup_ap(bss))
8002 return -1;
8003 } else if (was_ap) {
8004 /* Remove additional AP mode functionality */
8005 nl80211_teardown_ap(bss);
8006 } else {
8007 nl80211_mgmt_unsubscribe(bss, "mode change");
8008 }
8009
8010 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
8011 nl80211_mgmt_subscribe_non_ap(bss) < 0)
8012 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
8013 "frame processing - ignore for now");
8014
8015 return 0;
8016 }
8017
8018
8019 static int wpa_driver_nl80211_get_capa(void *priv,
8020 struct wpa_driver_capa *capa)
8021 {
8022 struct i802_bss *bss = priv;
8023 struct wpa_driver_nl80211_data *drv = bss->drv;
8024 if (!drv->has_capability)
8025 return -1;
8026 os_memcpy(capa, &drv->capa, sizeof(*capa));
8027 if (drv->extended_capa && drv->extended_capa_mask) {
8028 capa->extended_capa = drv->extended_capa;
8029 capa->extended_capa_mask = drv->extended_capa_mask;
8030 capa->extended_capa_len = drv->extended_capa_len;
8031 }
8032 return 0;
8033 }
8034
8035
8036 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
8037 {
8038 struct i802_bss *bss = priv;
8039 struct wpa_driver_nl80211_data *drv = bss->drv;
8040
8041 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
8042 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
8043 drv->operstate = state;
8044 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
8045 state ? IF_OPER_UP : IF_OPER_DORMANT);
8046 }
8047
8048
8049 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
8050 {
8051 struct i802_bss *bss = priv;
8052 struct wpa_driver_nl80211_data *drv = bss->drv;
8053 struct nl_msg *msg;
8054 struct nl80211_sta_flag_update upd;
8055
8056 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
8057 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
8058
8059 msg = nlmsg_alloc();
8060 if (!msg)
8061 return -ENOMEM;
8062
8063 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
8064
8065 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8066 if_nametoindex(bss->ifname));
8067 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
8068
8069 os_memset(&upd, 0, sizeof(upd));
8070 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
8071 if (authorized)
8072 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
8073 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
8074
8075 return send_and_recv_msgs(drv, msg, NULL, NULL);
8076 nla_put_failure:
8077 nlmsg_free(msg);
8078 return -ENOBUFS;
8079 }
8080
8081
8082 /* Set kernel driver on given frequency (MHz) */
8083 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
8084 {
8085 struct i802_bss *bss = priv;
8086 return wpa_driver_nl80211_set_freq(bss, freq);
8087 }
8088
8089
8090 #if defined(HOSTAPD) || defined(CONFIG_AP)
8091
8092 static inline int min_int(int a, int b)
8093 {
8094 if (a < b)
8095 return a;
8096 return b;
8097 }
8098
8099
8100 static int get_key_handler(struct nl_msg *msg, void *arg)
8101 {
8102 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8103 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8104
8105 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8106 genlmsg_attrlen(gnlh, 0), NULL);
8107
8108 /*
8109 * TODO: validate the key index and mac address!
8110 * Otherwise, there's a race condition as soon as
8111 * the kernel starts sending key notifications.
8112 */
8113
8114 if (tb[NL80211_ATTR_KEY_SEQ])
8115 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
8116 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
8117 return NL_SKIP;
8118 }
8119
8120
8121 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
8122 int idx, u8 *seq)
8123 {
8124 struct i802_bss *bss = priv;
8125 struct wpa_driver_nl80211_data *drv = bss->drv;
8126 struct nl_msg *msg;
8127
8128 msg = nlmsg_alloc();
8129 if (!msg)
8130 return -ENOMEM;
8131
8132 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
8133
8134 if (addr)
8135 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8136 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
8137 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
8138
8139 memset(seq, 0, 6);
8140
8141 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
8142 nla_put_failure:
8143 nlmsg_free(msg);
8144 return -ENOBUFS;
8145 }
8146
8147
8148 static int i802_set_rts(void *priv, int rts)
8149 {
8150 struct i802_bss *bss = priv;
8151 struct wpa_driver_nl80211_data *drv = bss->drv;
8152 struct nl_msg *msg;
8153 int ret = -ENOBUFS;
8154 u32 val;
8155
8156 msg = nlmsg_alloc();
8157 if (!msg)
8158 return -ENOMEM;
8159
8160 if (rts >= 2347)
8161 val = (u32) -1;
8162 else
8163 val = rts;
8164
8165 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8167 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
8168
8169 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8170 msg = NULL;
8171 if (!ret)
8172 return 0;
8173 nla_put_failure:
8174 nlmsg_free(msg);
8175 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
8176 "%d (%s)", rts, ret, strerror(-ret));
8177 return ret;
8178 }
8179
8180
8181 static int i802_set_frag(void *priv, int frag)
8182 {
8183 struct i802_bss *bss = priv;
8184 struct wpa_driver_nl80211_data *drv = bss->drv;
8185 struct nl_msg *msg;
8186 int ret = -ENOBUFS;
8187 u32 val;
8188
8189 msg = nlmsg_alloc();
8190 if (!msg)
8191 return -ENOMEM;
8192
8193 if (frag >= 2346)
8194 val = (u32) -1;
8195 else
8196 val = frag;
8197
8198 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8199 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8200 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
8201
8202 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8203 msg = NULL;
8204 if (!ret)
8205 return 0;
8206 nla_put_failure:
8207 nlmsg_free(msg);
8208 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
8209 "%d: %d (%s)", frag, ret, strerror(-ret));
8210 return ret;
8211 }
8212
8213
8214 static int i802_flush(void *priv)
8215 {
8216 struct i802_bss *bss = priv;
8217 struct wpa_driver_nl80211_data *drv = bss->drv;
8218 struct nl_msg *msg;
8219 int res;
8220
8221 msg = nlmsg_alloc();
8222 if (!msg)
8223 return -1;
8224
8225 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
8226
8227 /*
8228 * XXX: FIX! this needs to flush all VLANs too
8229 */
8230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8231 if_nametoindex(bss->ifname));
8232
8233 res = send_and_recv_msgs(drv, msg, NULL, NULL);
8234 if (res) {
8235 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
8236 "(%s)", res, strerror(-res));
8237 }
8238 return res;
8239 nla_put_failure:
8240 nlmsg_free(msg);
8241 return -ENOBUFS;
8242 }
8243
8244 #endif /* HOSTAPD || CONFIG_AP */
8245
8246
8247 static int get_sta_handler(struct nl_msg *msg, void *arg)
8248 {
8249 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8250 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8251 struct hostap_sta_driver_data *data = arg;
8252 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
8253 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
8254 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
8255 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
8256 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
8257 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
8258 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
8259 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
8260 };
8261
8262 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8263 genlmsg_attrlen(gnlh, 0), NULL);
8264
8265 /*
8266 * TODO: validate the interface and mac address!
8267 * Otherwise, there's a race condition as soon as
8268 * the kernel starts sending station notifications.
8269 */
8270
8271 if (!tb[NL80211_ATTR_STA_INFO]) {
8272 wpa_printf(MSG_DEBUG, "sta stats missing!");
8273 return NL_SKIP;
8274 }
8275 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
8276 tb[NL80211_ATTR_STA_INFO],
8277 stats_policy)) {
8278 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
8279 return NL_SKIP;
8280 }
8281
8282 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
8283 data->inactive_msec =
8284 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
8285 if (stats[NL80211_STA_INFO_RX_BYTES])
8286 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
8287 if (stats[NL80211_STA_INFO_TX_BYTES])
8288 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
8289 if (stats[NL80211_STA_INFO_RX_PACKETS])
8290 data->rx_packets =
8291 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
8292 if (stats[NL80211_STA_INFO_TX_PACKETS])
8293 data->tx_packets =
8294 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
8295 if (stats[NL80211_STA_INFO_TX_FAILED])
8296 data->tx_retry_failed =
8297 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
8298
8299 return NL_SKIP;
8300 }
8301
8302 static int i802_read_sta_data(struct i802_bss *bss,
8303 struct hostap_sta_driver_data *data,
8304 const u8 *addr)
8305 {
8306 struct wpa_driver_nl80211_data *drv = bss->drv;
8307 struct nl_msg *msg;
8308
8309 os_memset(data, 0, sizeof(*data));
8310 msg = nlmsg_alloc();
8311 if (!msg)
8312 return -ENOMEM;
8313
8314 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
8315
8316 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8317 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8318
8319 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
8320 nla_put_failure:
8321 nlmsg_free(msg);
8322 return -ENOBUFS;
8323 }
8324
8325
8326 #if defined(HOSTAPD) || defined(CONFIG_AP)
8327
8328 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
8329 int cw_min, int cw_max, int burst_time)
8330 {
8331 struct i802_bss *bss = priv;
8332 struct wpa_driver_nl80211_data *drv = bss->drv;
8333 struct nl_msg *msg;
8334 struct nlattr *txq, *params;
8335
8336 msg = nlmsg_alloc();
8337 if (!msg)
8338 return -1;
8339
8340 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
8341
8342 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8343
8344 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
8345 if (!txq)
8346 goto nla_put_failure;
8347
8348 /* We are only sending parameters for a single TXQ at a time */
8349 params = nla_nest_start(msg, 1);
8350 if (!params)
8351 goto nla_put_failure;
8352
8353 switch (queue) {
8354 case 0:
8355 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
8356 break;
8357 case 1:
8358 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
8359 break;
8360 case 2:
8361 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
8362 break;
8363 case 3:
8364 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
8365 break;
8366 }
8367 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
8368 * 32 usec, so need to convert the value here. */
8369 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
8370 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
8371 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
8372 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
8373
8374 nla_nest_end(msg, params);
8375
8376 nla_nest_end(msg, txq);
8377
8378 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8379 return 0;
8380 msg = NULL;
8381 nla_put_failure:
8382 nlmsg_free(msg);
8383 return -1;
8384 }
8385
8386
8387 static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
8388 const char *ifname, int vlan_id)
8389 {
8390 struct wpa_driver_nl80211_data *drv = bss->drv;
8391 struct nl_msg *msg;
8392 int ret = -ENOBUFS;
8393
8394 msg = nlmsg_alloc();
8395 if (!msg)
8396 return -ENOMEM;
8397
8398 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
8399
8400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
8401 if_nametoindex(bss->ifname));
8402 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8403 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
8404 if_nametoindex(ifname));
8405
8406 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8407 msg = NULL;
8408 if (ret < 0) {
8409 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
8410 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
8411 MAC2STR(addr), ifname, vlan_id, ret,
8412 strerror(-ret));
8413 }
8414 nla_put_failure:
8415 nlmsg_free(msg);
8416 return ret;
8417 }
8418
8419
8420 static int i802_get_inact_sec(void *priv, const u8 *addr)
8421 {
8422 struct hostap_sta_driver_data data;
8423 int ret;
8424
8425 data.inactive_msec = (unsigned long) -1;
8426 ret = i802_read_sta_data(priv, &data, addr);
8427 if (ret || data.inactive_msec == (unsigned long) -1)
8428 return -1;
8429 return data.inactive_msec / 1000;
8430 }
8431
8432
8433 static int i802_sta_clear_stats(void *priv, const u8 *addr)
8434 {
8435 #if 0
8436 /* TODO */
8437 #endif
8438 return 0;
8439 }
8440
8441
8442 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
8443 int reason)
8444 {
8445 struct i802_bss *bss = priv;
8446 struct wpa_driver_nl80211_data *drv = bss->drv;
8447 struct ieee80211_mgmt mgmt;
8448
8449 if (drv->device_ap_sme)
8450 return wpa_driver_nl80211_sta_remove(bss, addr);
8451
8452 memset(&mgmt, 0, sizeof(mgmt));
8453 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8454 WLAN_FC_STYPE_DEAUTH);
8455 memcpy(mgmt.da, addr, ETH_ALEN);
8456 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8457 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8458 mgmt.u.deauth.reason_code = host_to_le16(reason);
8459 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8460 IEEE80211_HDRLEN +
8461 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
8462 0);
8463 }
8464
8465
8466 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
8467 int reason)
8468 {
8469 struct i802_bss *bss = priv;
8470 struct wpa_driver_nl80211_data *drv = bss->drv;
8471 struct ieee80211_mgmt mgmt;
8472
8473 if (drv->device_ap_sme)
8474 return wpa_driver_nl80211_sta_remove(bss, addr);
8475
8476 memset(&mgmt, 0, sizeof(mgmt));
8477 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
8478 WLAN_FC_STYPE_DISASSOC);
8479 memcpy(mgmt.da, addr, ETH_ALEN);
8480 memcpy(mgmt.sa, own_addr, ETH_ALEN);
8481 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
8482 mgmt.u.disassoc.reason_code = host_to_le16(reason);
8483 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
8484 IEEE80211_HDRLEN +
8485 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
8486 0);
8487 }
8488
8489 #endif /* HOSTAPD || CONFIG_AP */
8490
8491 #ifdef HOSTAPD
8492
8493 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8494 {
8495 int i;
8496 int *old;
8497
8498 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
8499 ifidx);
8500 for (i = 0; i < drv->num_if_indices; i++) {
8501 if (drv->if_indices[i] == 0) {
8502 drv->if_indices[i] = ifidx;
8503 return;
8504 }
8505 }
8506
8507 if (drv->if_indices != drv->default_if_indices)
8508 old = drv->if_indices;
8509 else
8510 old = NULL;
8511
8512 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
8513 sizeof(int));
8514 if (!drv->if_indices) {
8515 if (!old)
8516 drv->if_indices = drv->default_if_indices;
8517 else
8518 drv->if_indices = old;
8519 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
8520 "interfaces");
8521 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
8522 return;
8523 } else if (!old)
8524 os_memcpy(drv->if_indices, drv->default_if_indices,
8525 sizeof(drv->default_if_indices));
8526 drv->if_indices[drv->num_if_indices] = ifidx;
8527 drv->num_if_indices++;
8528 }
8529
8530
8531 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8532 {
8533 int i;
8534
8535 for (i = 0; i < drv->num_if_indices; i++) {
8536 if (drv->if_indices[i] == ifidx) {
8537 drv->if_indices[i] = 0;
8538 break;
8539 }
8540 }
8541 }
8542
8543
8544 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
8545 {
8546 int i;
8547
8548 for (i = 0; i < drv->num_if_indices; i++)
8549 if (drv->if_indices[i] == ifidx)
8550 return 1;
8551
8552 return 0;
8553 }
8554
8555
8556 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
8557 const char *bridge_ifname)
8558 {
8559 struct i802_bss *bss = priv;
8560 struct wpa_driver_nl80211_data *drv = bss->drv;
8561 char name[IFNAMSIZ + 1];
8562
8563 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
8564 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
8565 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
8566 if (val) {
8567 if (!if_nametoindex(name)) {
8568 if (nl80211_create_iface(drv, name,
8569 NL80211_IFTYPE_AP_VLAN,
8570 bss->addr, 1, NULL, NULL) < 0)
8571 return -1;
8572 if (bridge_ifname &&
8573 linux_br_add_if(drv->global->ioctl_sock,
8574 bridge_ifname, name) < 0)
8575 return -1;
8576 }
8577 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
8578 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
8579 "interface %s up", name);
8580 }
8581 return i802_set_sta_vlan(priv, addr, name, 0);
8582 } else {
8583 if (bridge_ifname)
8584 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
8585 name);
8586
8587 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
8588 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
8589 name);
8590 }
8591 }
8592
8593
8594 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
8595 {
8596 struct wpa_driver_nl80211_data *drv = eloop_ctx;
8597 struct sockaddr_ll lladdr;
8598 unsigned char buf[3000];
8599 int len;
8600 socklen_t fromlen = sizeof(lladdr);
8601
8602 len = recvfrom(sock, buf, sizeof(buf), 0,
8603 (struct sockaddr *)&lladdr, &fromlen);
8604 if (len < 0) {
8605 perror("recv");
8606 return;
8607 }
8608
8609 if (have_ifidx(drv, lladdr.sll_ifindex))
8610 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
8611 }
8612
8613
8614 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
8615 struct i802_bss *bss,
8616 const char *brname, const char *ifname)
8617 {
8618 int ifindex;
8619 char in_br[IFNAMSIZ];
8620
8621 os_strlcpy(bss->brname, brname, IFNAMSIZ);
8622 ifindex = if_nametoindex(brname);
8623 if (ifindex == 0) {
8624 /*
8625 * Bridge was configured, but the bridge device does
8626 * not exist. Try to add it now.
8627 */
8628 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
8629 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
8630 "bridge interface %s: %s",
8631 brname, strerror(errno));
8632 return -1;
8633 }
8634 bss->added_bridge = 1;
8635 add_ifidx(drv, if_nametoindex(brname));
8636 }
8637
8638 if (linux_br_get(in_br, ifname) == 0) {
8639 if (os_strcmp(in_br, brname) == 0)
8640 return 0; /* already in the bridge */
8641
8642 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
8643 "bridge %s", ifname, in_br);
8644 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
8645 0) {
8646 wpa_printf(MSG_ERROR, "nl80211: Failed to "
8647 "remove interface %s from bridge "
8648 "%s: %s",
8649 ifname, brname, strerror(errno));
8650 return -1;
8651 }
8652 }
8653
8654 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
8655 ifname, brname);
8656 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
8657 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
8658 "into bridge %s: %s",
8659 ifname, brname, strerror(errno));
8660 return -1;
8661 }
8662 bss->added_if_into_bridge = 1;
8663
8664 return 0;
8665 }
8666
8667
8668 static void *i802_init(struct hostapd_data *hapd,
8669 struct wpa_init_params *params)
8670 {
8671 struct wpa_driver_nl80211_data *drv;
8672 struct i802_bss *bss;
8673 size_t i;
8674 char brname[IFNAMSIZ];
8675 int ifindex, br_ifindex;
8676 int br_added = 0;
8677
8678 bss = wpa_driver_nl80211_init(hapd, params->ifname,
8679 params->global_priv);
8680 if (bss == NULL)
8681 return NULL;
8682
8683 drv = bss->drv;
8684 drv->nlmode = NL80211_IFTYPE_AP;
8685 drv->eapol_sock = -1;
8686
8687 if (linux_br_get(brname, params->ifname) == 0) {
8688 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
8689 params->ifname, brname);
8690 br_ifindex = if_nametoindex(brname);
8691 } else {
8692 brname[0] = '\0';
8693 br_ifindex = 0;
8694 }
8695
8696 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
8697 drv->if_indices = drv->default_if_indices;
8698 for (i = 0; i < params->num_bridge; i++) {
8699 if (params->bridge[i]) {
8700 ifindex = if_nametoindex(params->bridge[i]);
8701 if (ifindex)
8702 add_ifidx(drv, ifindex);
8703 if (ifindex == br_ifindex)
8704 br_added = 1;
8705 }
8706 }
8707 if (!br_added && br_ifindex &&
8708 (params->num_bridge == 0 || !params->bridge[0]))
8709 add_ifidx(drv, br_ifindex);
8710
8711 /* start listening for EAPOL on the default AP interface */
8712 add_ifidx(drv, drv->ifindex);
8713
8714 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
8715 goto failed;
8716
8717 if (params->bssid) {
8718 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8719 params->bssid))
8720 goto failed;
8721 }
8722
8723 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
8724 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
8725 "into AP mode", bss->ifname);
8726 goto failed;
8727 }
8728
8729 if (params->num_bridge && params->bridge[0] &&
8730 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
8731 goto failed;
8732
8733 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
8734 goto failed;
8735
8736 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
8737 if (drv->eapol_sock < 0) {
8738 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
8739 goto failed;
8740 }
8741
8742 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
8743 {
8744 printf("Could not register read socket for eapol\n");
8745 goto failed;
8746 }
8747
8748 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8749 params->own_addr))
8750 goto failed;
8751
8752 memcpy(bss->addr, params->own_addr, ETH_ALEN);
8753
8754 return bss;
8755
8756 failed:
8757 wpa_driver_nl80211_deinit(bss);
8758 return NULL;
8759 }
8760
8761
8762 static void i802_deinit(void *priv)
8763 {
8764 struct i802_bss *bss = priv;
8765 wpa_driver_nl80211_deinit(bss);
8766 }
8767
8768 #endif /* HOSTAPD */
8769
8770
8771 static enum nl80211_iftype wpa_driver_nl80211_if_type(
8772 enum wpa_driver_if_type type)
8773 {
8774 switch (type) {
8775 case WPA_IF_STATION:
8776 return NL80211_IFTYPE_STATION;
8777 case WPA_IF_P2P_CLIENT:
8778 case WPA_IF_P2P_GROUP:
8779 return NL80211_IFTYPE_P2P_CLIENT;
8780 case WPA_IF_AP_VLAN:
8781 return NL80211_IFTYPE_AP_VLAN;
8782 case WPA_IF_AP_BSS:
8783 return NL80211_IFTYPE_AP;
8784 case WPA_IF_P2P_GO:
8785 return NL80211_IFTYPE_P2P_GO;
8786 case WPA_IF_P2P_DEVICE:
8787 return NL80211_IFTYPE_P2P_DEVICE;
8788 }
8789 return -1;
8790 }
8791
8792
8793 #ifdef CONFIG_P2P
8794
8795 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
8796 {
8797 struct wpa_driver_nl80211_data *drv;
8798 dl_list_for_each(drv, &global->interfaces,
8799 struct wpa_driver_nl80211_data, list) {
8800 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
8801 return 1;
8802 }
8803 return 0;
8804 }
8805
8806
8807 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
8808 u8 *new_addr)
8809 {
8810 unsigned int idx;
8811
8812 if (!drv->global)
8813 return -1;
8814
8815 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
8816 for (idx = 0; idx < 64; idx++) {
8817 new_addr[0] = drv->first_bss.addr[0] | 0x02;
8818 new_addr[0] ^= idx << 2;
8819 if (!nl80211_addr_in_use(drv->global, new_addr))
8820 break;
8821 }
8822 if (idx == 64)
8823 return -1;
8824
8825 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
8826 MACSTR, MAC2STR(new_addr));
8827
8828 return 0;
8829 }
8830
8831 #endif /* CONFIG_P2P */
8832
8833
8834 static int nl80211_create_p2p_dev_handler(struct nl_msg *msg, void *arg)
8835 {
8836 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8837 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8838 struct i802_bss *p2p_dev = arg;
8839
8840 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8841 genlmsg_attrlen(gnlh, 0), NULL);
8842 if (tb[NL80211_ATTR_WDEV]) {
8843 p2p_dev->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
8844 p2p_dev->wdev_id_set = 1;
8845 }
8846
8847 if (tb[NL80211_ATTR_MAC])
8848 os_memcpy(p2p_dev->addr, nla_data(tb[NL80211_ATTR_MAC]),
8849 ETH_ALEN);
8850
8851 return NL_SKIP;
8852 }
8853
8854
8855 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8856 const char *ifname, const u8 *addr,
8857 void *bss_ctx, void **drv_priv,
8858 char *force_ifname, u8 *if_addr,
8859 const char *bridge)
8860 {
8861 enum nl80211_iftype nlmode;
8862 struct i802_bss *bss = priv;
8863 struct i802_bss *p2pdev;
8864 struct wpa_driver_nl80211_data *drv = bss->drv;
8865 int ifidx;
8866 #ifdef HOSTAPD
8867 struct i802_bss *new_bss = NULL;
8868
8869 if (type == WPA_IF_AP_BSS) {
8870 new_bss = os_zalloc(sizeof(*new_bss));
8871 if (new_bss == NULL)
8872 return -1;
8873 }
8874 #endif /* HOSTAPD */
8875
8876 if (addr)
8877 os_memcpy(if_addr, addr, ETH_ALEN);
8878 nlmode = wpa_driver_nl80211_if_type(type);
8879 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
8880 p2pdev = os_zalloc(sizeof(*p2pdev));
8881 if (!p2pdev)
8882 return -1;
8883 *p2pdev = *bss;
8884 os_strlcpy(p2pdev->ifname, ifname, IFNAMSIZ);
8885 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
8886 0, nl80211_create_p2p_dev_handler,
8887 p2pdev);
8888 if (!p2pdev->wdev_id_set || ifidx != 0 ||
8889 is_zero_ether_addr((const u8 *) &p2pdev->addr)) {
8890 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
8891 ifname);
8892 os_free(p2pdev);
8893 return -1;
8894 }
8895 } else {
8896 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
8897 0, NULL, NULL);
8898 if (ifidx < 0) {
8899 #ifdef HOSTAPD
8900 os_free(new_bss);
8901 #endif /* HOSTAPD */
8902 return -1;
8903 }
8904 }
8905
8906 if (!addr &&
8907 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8908 if_addr) < 0) {
8909 nl80211_remove_iface(drv, ifidx);
8910 return -1;
8911 }
8912
8913 #ifdef CONFIG_P2P
8914 if (!addr &&
8915 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8916 type == WPA_IF_P2P_GO)) {
8917 /* Enforce unique P2P Interface Address */
8918 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
8919
8920 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8921 own_addr) < 0 ||
8922 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
8923 new_addr) < 0) {
8924 nl80211_remove_iface(drv, ifidx);
8925 return -1;
8926 }
8927 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
8928 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8929 "for P2P group interface");
8930 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
8931 nl80211_remove_iface(drv, ifidx);
8932 return -1;
8933 }
8934 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
8935 new_addr) < 0) {
8936 nl80211_remove_iface(drv, ifidx);
8937 return -1;
8938 }
8939 }
8940 os_memcpy(if_addr, new_addr, ETH_ALEN);
8941 }
8942 #endif /* CONFIG_P2P */
8943
8944 #ifdef HOSTAPD
8945 if (bridge &&
8946 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
8947 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
8948 "interface %s to a bridge %s", ifname, bridge);
8949 nl80211_remove_iface(drv, ifidx);
8950 os_free(new_bss);
8951 return -1;
8952 }
8953
8954 if (type == WPA_IF_AP_BSS) {
8955 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
8956 {
8957 nl80211_remove_iface(drv, ifidx);
8958 os_free(new_bss);
8959 return -1;
8960 }
8961 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
8962 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
8963 new_bss->ifindex = ifidx;
8964 new_bss->drv = drv;
8965 new_bss->next = drv->first_bss.next;
8966 new_bss->freq = drv->first_bss.freq;
8967 new_bss->ctx = bss_ctx;
8968 drv->first_bss.next = new_bss;
8969 if (drv_priv)
8970 *drv_priv = new_bss;
8971 nl80211_init_bss(new_bss);
8972
8973 /* Subscribe management frames for this WPA_IF_AP_BSS */
8974 if (nl80211_setup_ap(new_bss))
8975 return -1;
8976 }
8977 #endif /* HOSTAPD */
8978
8979 if (drv->global)
8980 drv->global->if_add_ifindex = ifidx;
8981
8982 return 0;
8983 }
8984
8985
8986 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
8987 enum wpa_driver_if_type type,
8988 const char *ifname)
8989 {
8990 struct wpa_driver_nl80211_data *drv = bss->drv;
8991 int ifindex = if_nametoindex(ifname);
8992
8993 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8994 __func__, type, ifname, ifindex);
8995 if (ifindex <= 0)
8996 return -1;
8997
8998 nl80211_remove_iface(drv, ifindex);
8999
9000 #ifdef HOSTAPD
9001 if (type != WPA_IF_AP_BSS)
9002 return 0;
9003
9004 if (bss->added_if_into_bridge) {
9005 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
9006 bss->ifname) < 0)
9007 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9008 "interface %s from bridge %s: %s",
9009 bss->ifname, bss->brname, strerror(errno));
9010 }
9011 if (bss->added_bridge) {
9012 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
9013 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
9014 "bridge %s: %s",
9015 bss->brname, strerror(errno));
9016 }
9017
9018 if (bss != &drv->first_bss) {
9019 struct i802_bss *tbss;
9020
9021 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
9022 if (tbss->next == bss) {
9023 tbss->next = bss->next;
9024 /* Unsubscribe management frames */
9025 nl80211_teardown_ap(bss);
9026 nl80211_destroy_bss(bss);
9027 os_free(bss);
9028 bss = NULL;
9029 break;
9030 }
9031 }
9032 if (bss)
9033 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
9034 "BSS %p in the list", __func__, bss);
9035 }
9036 #endif /* HOSTAPD */
9037
9038 return 0;
9039 }
9040
9041
9042 static int cookie_handler(struct nl_msg *msg, void *arg)
9043 {
9044 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9045 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9046 u64 *cookie = arg;
9047 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9048 genlmsg_attrlen(gnlh, 0), NULL);
9049 if (tb[NL80211_ATTR_COOKIE])
9050 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
9051 return NL_SKIP;
9052 }
9053
9054
9055 static int nl80211_send_frame_cmd(struct i802_bss *bss,
9056 unsigned int freq, unsigned int wait,
9057 const u8 *buf, size_t buf_len,
9058 u64 *cookie_out, int no_cck, int no_ack,
9059 int offchanok)
9060 {
9061 struct wpa_driver_nl80211_data *drv = bss->drv;
9062 struct nl_msg *msg;
9063 u64 cookie;
9064 int ret = -1;
9065
9066 msg = nlmsg_alloc();
9067 if (!msg)
9068 return -1;
9069
9070 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
9071 "no_ack=%d offchanok=%d",
9072 freq, wait, no_cck, no_ack, offchanok);
9073 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
9074
9075 if (nl80211_set_iface_id(msg, bss))
9076 goto nla_put_failure;
9077
9078 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9079 if (wait)
9080 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
9081 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
9082 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
9083 if (no_cck)
9084 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
9085 if (no_ack)
9086 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
9087
9088 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
9089
9090 cookie = 0;
9091 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9092 msg = NULL;
9093 if (ret) {
9094 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
9095 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
9096 freq, wait);
9097 goto nla_put_failure;
9098 }
9099 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
9100 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
9101 (long long unsigned int) cookie);
9102
9103 if (cookie_out)
9104 *cookie_out = no_ack ? (u64) -1 : cookie;
9105
9106 nla_put_failure:
9107 nlmsg_free(msg);
9108 return ret;
9109 }
9110
9111
9112 static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
9113 unsigned int freq,
9114 unsigned int wait_time,
9115 const u8 *dst, const u8 *src,
9116 const u8 *bssid,
9117 const u8 *data, size_t data_len,
9118 int no_cck)
9119 {
9120 struct wpa_driver_nl80211_data *drv = bss->drv;
9121 int ret = -1;
9122 u8 *buf;
9123 struct ieee80211_hdr *hdr;
9124
9125 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
9126 "freq=%u MHz wait=%d ms no_cck=%d)",
9127 drv->ifindex, freq, wait_time, no_cck);
9128
9129 buf = os_zalloc(24 + data_len);
9130 if (buf == NULL)
9131 return ret;
9132 os_memcpy(buf + 24, data, data_len);
9133 hdr = (struct ieee80211_hdr *) buf;
9134 hdr->frame_control =
9135 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
9136 os_memcpy(hdr->addr1, dst, ETH_ALEN);
9137 os_memcpy(hdr->addr2, src, ETH_ALEN);
9138 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
9139
9140 if (is_ap_interface(drv->nlmode))
9141 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
9142 0, freq, no_cck, 1,
9143 wait_time);
9144 else
9145 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
9146 24 + data_len,
9147 &drv->send_action_cookie,
9148 no_cck, 0, 1);
9149
9150 os_free(buf);
9151 return ret;
9152 }
9153
9154
9155 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
9156 {
9157 struct i802_bss *bss = priv;
9158 struct wpa_driver_nl80211_data *drv = bss->drv;
9159 struct nl_msg *msg;
9160 int ret;
9161
9162 msg = nlmsg_alloc();
9163 if (!msg)
9164 return;
9165
9166 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
9167 (long long unsigned int) drv->send_action_cookie);
9168 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
9169
9170 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9171 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
9172
9173 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9174 msg = NULL;
9175 if (ret)
9176 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
9177 "(%s)", ret, strerror(-ret));
9178
9179 nla_put_failure:
9180 nlmsg_free(msg);
9181 }
9182
9183
9184 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
9185 unsigned int duration)
9186 {
9187 struct i802_bss *bss = priv;
9188 struct wpa_driver_nl80211_data *drv = bss->drv;
9189 struct nl_msg *msg;
9190 int ret;
9191 u64 cookie;
9192
9193 msg = nlmsg_alloc();
9194 if (!msg)
9195 return -1;
9196
9197 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
9198
9199 if (nl80211_set_iface_id(msg, bss))
9200 goto nla_put_failure;
9201
9202 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9203 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
9204
9205 cookie = 0;
9206 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
9207 msg = NULL;
9208 if (ret == 0) {
9209 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
9210 "0x%llx for freq=%u MHz duration=%u",
9211 (long long unsigned int) cookie, freq, duration);
9212 drv->remain_on_chan_cookie = cookie;
9213 drv->pending_remain_on_chan = 1;
9214 return 0;
9215 }
9216 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
9217 "(freq=%d duration=%u): %d (%s)",
9218 freq, duration, ret, strerror(-ret));
9219 nla_put_failure:
9220 nlmsg_free(msg);
9221 return -1;
9222 }
9223
9224
9225 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
9226 {
9227 struct i802_bss *bss = priv;
9228 struct wpa_driver_nl80211_data *drv = bss->drv;
9229 struct nl_msg *msg;
9230 int ret;
9231
9232 if (!drv->pending_remain_on_chan) {
9233 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
9234 "to cancel");
9235 return -1;
9236 }
9237
9238 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
9239 "0x%llx",
9240 (long long unsigned int) drv->remain_on_chan_cookie);
9241
9242 msg = nlmsg_alloc();
9243 if (!msg)
9244 return -1;
9245
9246 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
9247
9248 if (nl80211_set_iface_id(msg, bss))
9249 goto nla_put_failure;
9250
9251 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
9252
9253 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9254 msg = NULL;
9255 if (ret == 0)
9256 return 0;
9257 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
9258 "%d (%s)", ret, strerror(-ret));
9259 nla_put_failure:
9260 nlmsg_free(msg);
9261 return -1;
9262 }
9263
9264
9265 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
9266 {
9267 struct wpa_driver_nl80211_data *drv = bss->drv;
9268
9269 if (!report) {
9270 if (bss->nl_preq && drv->device_ap_sme &&
9271 is_ap_interface(drv->nlmode)) {
9272 /*
9273 * Do not disable Probe Request reporting that was
9274 * enabled in nl80211_setup_ap().
9275 */
9276 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
9277 "Probe Request reporting nl_preq=%p while "
9278 "in AP mode", bss->nl_preq);
9279 } else if (bss->nl_preq) {
9280 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
9281 "reporting nl_preq=%p", bss->nl_preq);
9282 eloop_unregister_read_sock(
9283 nl_socket_get_fd(bss->nl_preq));
9284 nl_destroy_handles(&bss->nl_preq);
9285 }
9286 return 0;
9287 }
9288
9289 if (bss->nl_preq) {
9290 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
9291 "already on! nl_preq=%p", bss->nl_preq);
9292 return 0;
9293 }
9294
9295 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
9296 if (bss->nl_preq == NULL)
9297 return -1;
9298 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
9299 "reporting nl_preq=%p", bss->nl_preq);
9300
9301 if (nl80211_register_frame(bss, bss->nl_preq,
9302 (WLAN_FC_TYPE_MGMT << 2) |
9303 (WLAN_FC_STYPE_PROBE_REQ << 4),
9304 NULL, 0) < 0)
9305 goto out_err;
9306
9307 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
9308 wpa_driver_nl80211_event_receive, bss->nl_cb,
9309 bss->nl_preq);
9310
9311 return 0;
9312
9313 out_err:
9314 nl_destroy_handles(&bss->nl_preq);
9315 return -1;
9316 }
9317
9318
9319 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
9320 int ifindex, int disabled)
9321 {
9322 struct nl_msg *msg;
9323 struct nlattr *bands, *band;
9324 int ret;
9325
9326 msg = nlmsg_alloc();
9327 if (!msg)
9328 return -1;
9329
9330 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
9331 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
9332
9333 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
9334 if (!bands)
9335 goto nla_put_failure;
9336
9337 /*
9338 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
9339 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
9340 * rates. All 5 GHz rates are left enabled.
9341 */
9342 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
9343 if (!band)
9344 goto nla_put_failure;
9345 if (disabled) {
9346 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
9347 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
9348 }
9349 nla_nest_end(msg, band);
9350
9351 nla_nest_end(msg, bands);
9352
9353 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9354 msg = NULL;
9355 if (ret) {
9356 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
9357 "(%s)", ret, strerror(-ret));
9358 } else
9359 drv->disabled_11b_rates = disabled;
9360
9361 return ret;
9362
9363 nla_put_failure:
9364 nlmsg_free(msg);
9365 return -1;
9366 }
9367
9368
9369 static int wpa_driver_nl80211_deinit_ap(void *priv)
9370 {
9371 struct i802_bss *bss = priv;
9372 struct wpa_driver_nl80211_data *drv = bss->drv;
9373 if (!is_ap_interface(drv->nlmode))
9374 return -1;
9375 wpa_driver_nl80211_del_beacon(drv);
9376 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9377 }
9378
9379
9380 static int wpa_driver_nl80211_stop_ap(void *priv)
9381 {
9382 struct i802_bss *bss = priv;
9383 struct wpa_driver_nl80211_data *drv = bss->drv;
9384 if (!is_ap_interface(drv->nlmode))
9385 return -1;
9386 wpa_driver_nl80211_del_beacon(drv);
9387 bss->beacon_set = 0;
9388 return 0;
9389 }
9390
9391
9392 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
9393 {
9394 struct i802_bss *bss = priv;
9395 struct wpa_driver_nl80211_data *drv = bss->drv;
9396 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
9397 return -1;
9398 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
9399 }
9400
9401
9402 static void wpa_driver_nl80211_resume(void *priv)
9403 {
9404 struct i802_bss *bss = priv;
9405 struct wpa_driver_nl80211_data *drv = bss->drv;
9406 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
9407 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
9408 "resume event");
9409 }
9410 }
9411
9412
9413 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
9414 const u8 *ies, size_t ies_len)
9415 {
9416 struct i802_bss *bss = priv;
9417 struct wpa_driver_nl80211_data *drv = bss->drv;
9418 int ret;
9419 u8 *data, *pos;
9420 size_t data_len;
9421 const u8 *own_addr = bss->addr;
9422
9423 if (action != 1) {
9424 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
9425 "action %d", action);
9426 return -1;
9427 }
9428
9429 /*
9430 * Action frame payload:
9431 * Category[1] = 6 (Fast BSS Transition)
9432 * Action[1] = 1 (Fast BSS Transition Request)
9433 * STA Address
9434 * Target AP Address
9435 * FT IEs
9436 */
9437
9438 data_len = 2 + 2 * ETH_ALEN + ies_len;
9439 data = os_malloc(data_len);
9440 if (data == NULL)
9441 return -1;
9442 pos = data;
9443 *pos++ = 0x06; /* FT Action category */
9444 *pos++ = action;
9445 os_memcpy(pos, own_addr, ETH_ALEN);
9446 pos += ETH_ALEN;
9447 os_memcpy(pos, target_ap, ETH_ALEN);
9448 pos += ETH_ALEN;
9449 os_memcpy(pos, ies, ies_len);
9450
9451 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
9452 drv->bssid, own_addr, drv->bssid,
9453 data, data_len, 0);
9454 os_free(data);
9455
9456 return ret;
9457 }
9458
9459
9460 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
9461 {
9462 struct i802_bss *bss = priv;
9463 struct wpa_driver_nl80211_data *drv = bss->drv;
9464 struct nl_msg *msg;
9465 struct nlattr *cqm;
9466 int ret = -1;
9467
9468 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
9469 "hysteresis=%d", threshold, hysteresis);
9470
9471 msg = nlmsg_alloc();
9472 if (!msg)
9473 return -1;
9474
9475 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
9476
9477 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9478
9479 cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
9480 if (cqm == NULL)
9481 goto nla_put_failure;
9482
9483 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
9484 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
9485 nla_nest_end(msg, cqm);
9486
9487 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9488 msg = NULL;
9489
9490 nla_put_failure:
9491 nlmsg_free(msg);
9492 return ret;
9493 }
9494
9495
9496 /* Converts nl80211_chan_width to a common format */
9497 static enum chan_width convert2width(int width)
9498 {
9499 switch (width) {
9500 case NL80211_CHAN_WIDTH_20_NOHT:
9501 return CHAN_WIDTH_20_NOHT;
9502 case NL80211_CHAN_WIDTH_20:
9503 return CHAN_WIDTH_20;
9504 case NL80211_CHAN_WIDTH_40:
9505 return CHAN_WIDTH_40;
9506 case NL80211_CHAN_WIDTH_80:
9507 return CHAN_WIDTH_80;
9508 case NL80211_CHAN_WIDTH_80P80:
9509 return CHAN_WIDTH_80P80;
9510 case NL80211_CHAN_WIDTH_160:
9511 return CHAN_WIDTH_160;
9512 }
9513 return CHAN_WIDTH_UNKNOWN;
9514 }
9515
9516
9517 static int get_channel_width(struct nl_msg *msg, void *arg)
9518 {
9519 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9520 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9521 struct wpa_signal_info *sig_change = arg;
9522
9523 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9524 genlmsg_attrlen(gnlh, 0), NULL);
9525
9526 sig_change->center_frq1 = -1;
9527 sig_change->center_frq2 = -1;
9528 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
9529
9530 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
9531 sig_change->chanwidth = convert2width(
9532 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
9533 if (tb[NL80211_ATTR_CENTER_FREQ1])
9534 sig_change->center_frq1 =
9535 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
9536 if (tb[NL80211_ATTR_CENTER_FREQ2])
9537 sig_change->center_frq2 =
9538 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
9539 }
9540
9541 return NL_SKIP;
9542 }
9543
9544
9545 static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
9546 struct wpa_signal_info *sig)
9547 {
9548 struct nl_msg *msg;
9549
9550 msg = nlmsg_alloc();
9551 if (!msg)
9552 return -ENOMEM;
9553
9554 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
9555 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9556
9557 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
9558
9559 nla_put_failure:
9560 nlmsg_free(msg);
9561 return -ENOBUFS;
9562 }
9563
9564
9565 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
9566 {
9567 struct i802_bss *bss = priv;
9568 struct wpa_driver_nl80211_data *drv = bss->drv;
9569 int res;
9570
9571 os_memset(si, 0, sizeof(*si));
9572 res = nl80211_get_link_signal(drv, si);
9573 if (res != 0)
9574 return res;
9575
9576 res = nl80211_get_channel_width(drv, si);
9577 if (res != 0)
9578 return res;
9579
9580 return nl80211_get_link_noise(drv, si);
9581 }
9582
9583
9584 static int wpa_driver_nl80211_shared_freq(void *priv)
9585 {
9586 struct i802_bss *bss = priv;
9587 struct wpa_driver_nl80211_data *drv = bss->drv;
9588 struct wpa_driver_nl80211_data *driver;
9589 int freq = 0;
9590
9591 /*
9592 * If the same PHY is in connected state with some other interface,
9593 * then retrieve the assoc freq.
9594 */
9595 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
9596 drv->phyname);
9597
9598 dl_list_for_each(driver, &drv->global->interfaces,
9599 struct wpa_driver_nl80211_data, list) {
9600 if (drv == driver ||
9601 os_strcmp(drv->phyname, driver->phyname) != 0 ||
9602 !driver->associated)
9603 continue;
9604
9605 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
9606 MACSTR,
9607 driver->phyname, driver->first_bss.ifname,
9608 MAC2STR(driver->first_bss.addr));
9609 if (is_ap_interface(driver->nlmode))
9610 freq = driver->first_bss.freq;
9611 else
9612 freq = nl80211_get_assoc_freq(driver);
9613 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
9614 drv->phyname, freq);
9615 }
9616
9617 if (!freq)
9618 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
9619 "PHY (%s) in associated state", drv->phyname);
9620
9621 return freq;
9622 }
9623
9624
9625 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
9626 int encrypt)
9627 {
9628 struct i802_bss *bss = priv;
9629 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
9630 0, 0, 0, 0);
9631 }
9632
9633
9634 static int nl80211_set_param(void *priv, const char *param)
9635 {
9636 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
9637 if (param == NULL)
9638 return 0;
9639
9640 #ifdef CONFIG_P2P
9641 if (os_strstr(param, "use_p2p_group_interface=1")) {
9642 struct i802_bss *bss = priv;
9643 struct wpa_driver_nl80211_data *drv = bss->drv;
9644
9645 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
9646 "interface");
9647 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
9648 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
9649 }
9650 #endif /* CONFIG_P2P */
9651
9652 return 0;
9653 }
9654
9655
9656 static void * nl80211_global_init(void)
9657 {
9658 struct nl80211_global *global;
9659 struct netlink_config *cfg;
9660
9661 global = os_zalloc(sizeof(*global));
9662 if (global == NULL)
9663 return NULL;
9664 global->ioctl_sock = -1;
9665 dl_list_init(&global->interfaces);
9666 global->if_add_ifindex = -1;
9667
9668 cfg = os_zalloc(sizeof(*cfg));
9669 if (cfg == NULL)
9670 goto err;
9671
9672 cfg->ctx = global;
9673 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
9674 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
9675 global->netlink = netlink_init(cfg);
9676 if (global->netlink == NULL) {
9677 os_free(cfg);
9678 goto err;
9679 }
9680
9681 if (wpa_driver_nl80211_init_nl_global(global) < 0)
9682 goto err;
9683
9684 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
9685 if (global->ioctl_sock < 0) {
9686 perror("socket(PF_INET,SOCK_DGRAM)");
9687 goto err;
9688 }
9689
9690 return global;
9691
9692 err:
9693 nl80211_global_deinit(global);
9694 return NULL;
9695 }
9696
9697
9698 static void nl80211_global_deinit(void *priv)
9699 {
9700 struct nl80211_global *global = priv;
9701 if (global == NULL)
9702 return;
9703 if (!dl_list_empty(&global->interfaces)) {
9704 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
9705 "nl80211_global_deinit",
9706 dl_list_len(&global->interfaces));
9707 }
9708
9709 if (global->netlink)
9710 netlink_deinit(global->netlink);
9711
9712 nl_destroy_handles(&global->nl);
9713
9714 if (global->nl_event) {
9715 eloop_unregister_read_sock(
9716 nl_socket_get_fd(global->nl_event));
9717 nl_destroy_handles(&global->nl_event);
9718 }
9719
9720 nl_cb_put(global->nl_cb);
9721
9722 if (global->ioctl_sock >= 0)
9723 close(global->ioctl_sock);
9724
9725 os_free(global);
9726 }
9727
9728
9729 static const char * nl80211_get_radio_name(void *priv)
9730 {
9731 struct i802_bss *bss = priv;
9732 struct wpa_driver_nl80211_data *drv = bss->drv;
9733 return drv->phyname;
9734 }
9735
9736
9737 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
9738 const u8 *pmkid)
9739 {
9740 struct nl_msg *msg;
9741
9742 msg = nlmsg_alloc();
9743 if (!msg)
9744 return -ENOMEM;
9745
9746 nl80211_cmd(bss->drv, msg, 0, cmd);
9747
9748 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
9749 if (pmkid)
9750 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
9751 if (bssid)
9752 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
9753
9754 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9755 nla_put_failure:
9756 nlmsg_free(msg);
9757 return -ENOBUFS;
9758 }
9759
9760
9761 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9762 {
9763 struct i802_bss *bss = priv;
9764 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9765 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9766 }
9767
9768
9769 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9770 {
9771 struct i802_bss *bss = priv;
9772 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9773 MAC2STR(bssid));
9774 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9775 }
9776
9777
9778 static int nl80211_flush_pmkid(void *priv)
9779 {
9780 struct i802_bss *bss = priv;
9781 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9782 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9783 }
9784
9785
9786 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9787 const u8 *replay_ctr)
9788 {
9789 struct i802_bss *bss = priv;
9790 struct wpa_driver_nl80211_data *drv = bss->drv;
9791 struct nlattr *replay_nested;
9792 struct nl_msg *msg;
9793
9794 msg = nlmsg_alloc();
9795 if (!msg)
9796 return;
9797
9798 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9799
9800 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9801
9802 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9803 if (!replay_nested)
9804 goto nla_put_failure;
9805
9806 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9807 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9808 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9809 replay_ctr);
9810
9811 nla_nest_end(msg, replay_nested);
9812
9813 send_and_recv_msgs(drv, msg, NULL, NULL);
9814 return;
9815 nla_put_failure:
9816 nlmsg_free(msg);
9817 }
9818
9819
9820 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9821 const u8 *addr, int qos)
9822 {
9823 /* send data frame to poll STA and check whether
9824 * this frame is ACKed */
9825 struct {
9826 struct ieee80211_hdr hdr;
9827 u16 qos_ctl;
9828 } STRUCT_PACKED nulldata;
9829 size_t size;
9830
9831 /* Send data frame to poll STA and check whether this frame is ACKed */
9832
9833 os_memset(&nulldata, 0, sizeof(nulldata));
9834
9835 if (qos) {
9836 nulldata.hdr.frame_control =
9837 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9838 WLAN_FC_STYPE_QOS_NULL);
9839 size = sizeof(nulldata);
9840 } else {
9841 nulldata.hdr.frame_control =
9842 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9843 WLAN_FC_STYPE_NULLFUNC);
9844 size = sizeof(struct ieee80211_hdr);
9845 }
9846
9847 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9848 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9849 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9850 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9851
9852 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
9853 0, 0) < 0)
9854 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9855 "send poll frame");
9856 }
9857
9858 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9859 int qos)
9860 {
9861 struct i802_bss *bss = priv;
9862 struct wpa_driver_nl80211_data *drv = bss->drv;
9863 struct nl_msg *msg;
9864
9865 if (!drv->poll_command_supported) {
9866 nl80211_send_null_frame(bss, own_addr, addr, qos);
9867 return;
9868 }
9869
9870 msg = nlmsg_alloc();
9871 if (!msg)
9872 return;
9873
9874 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9875
9876 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9877 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9878
9879 send_and_recv_msgs(drv, msg, NULL, NULL);
9880 return;
9881 nla_put_failure:
9882 nlmsg_free(msg);
9883 }
9884
9885
9886 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9887 {
9888 struct nl_msg *msg;
9889
9890 msg = nlmsg_alloc();
9891 if (!msg)
9892 return -ENOMEM;
9893
9894 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9895 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9896 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9897 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9898 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9899 nla_put_failure:
9900 nlmsg_free(msg);
9901 return -ENOBUFS;
9902 }
9903
9904
9905 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9906 int ctwindow)
9907 {
9908 struct i802_bss *bss = priv;
9909
9910 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9911 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9912
9913 if (opp_ps != -1 || ctwindow != -1)
9914 return -1; /* Not yet supported */
9915
9916 if (legacy_ps == -1)
9917 return 0;
9918 if (legacy_ps != 0 && legacy_ps != 1)
9919 return -1; /* Not yet supported */
9920
9921 return nl80211_set_power_save(bss, legacy_ps);
9922 }
9923
9924
9925 static int nl80211_start_radar_detection(void *priv, int freq)
9926 {
9927 struct i802_bss *bss = priv;
9928 struct wpa_driver_nl80211_data *drv = bss->drv;
9929 struct nl_msg *msg;
9930 int ret;
9931
9932 wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC)");
9933 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
9934 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
9935 "detection");
9936 return -1;
9937 }
9938
9939 msg = nlmsg_alloc();
9940 if (!msg)
9941 return -1;
9942
9943 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
9944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9945 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
9946
9947 /* only HT20 is supported at this point */
9948 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, NL80211_CHAN_HT20);
9949
9950 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9951 if (ret == 0)
9952 return 0;
9953 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
9954 "%d (%s)", ret, strerror(-ret));
9955 nla_put_failure:
9956 return -1;
9957 }
9958
9959 #ifdef CONFIG_TDLS
9960
9961 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9962 u8 dialog_token, u16 status_code,
9963 const u8 *buf, size_t len)
9964 {
9965 struct i802_bss *bss = priv;
9966 struct wpa_driver_nl80211_data *drv = bss->drv;
9967 struct nl_msg *msg;
9968
9969 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9970 return -EOPNOTSUPP;
9971
9972 if (!dst)
9973 return -EINVAL;
9974
9975 msg = nlmsg_alloc();
9976 if (!msg)
9977 return -ENOMEM;
9978
9979 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9980 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9981 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9982 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9983 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9984 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9985 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9986
9987 return send_and_recv_msgs(drv, msg, NULL, NULL);
9988
9989 nla_put_failure:
9990 nlmsg_free(msg);
9991 return -ENOBUFS;
9992 }
9993
9994
9995 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9996 {
9997 struct i802_bss *bss = priv;
9998 struct wpa_driver_nl80211_data *drv = bss->drv;
9999 struct nl_msg *msg;
10000 enum nl80211_tdls_operation nl80211_oper;
10001
10002 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
10003 return -EOPNOTSUPP;
10004
10005 switch (oper) {
10006 case TDLS_DISCOVERY_REQ:
10007 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
10008 break;
10009 case TDLS_SETUP:
10010 nl80211_oper = NL80211_TDLS_SETUP;
10011 break;
10012 case TDLS_TEARDOWN:
10013 nl80211_oper = NL80211_TDLS_TEARDOWN;
10014 break;
10015 case TDLS_ENABLE_LINK:
10016 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
10017 break;
10018 case TDLS_DISABLE_LINK:
10019 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
10020 break;
10021 case TDLS_ENABLE:
10022 return 0;
10023 case TDLS_DISABLE:
10024 return 0;
10025 default:
10026 return -EINVAL;
10027 }
10028
10029 msg = nlmsg_alloc();
10030 if (!msg)
10031 return -ENOMEM;
10032
10033 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
10034 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
10035 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10036 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
10037
10038 return send_and_recv_msgs(drv, msg, NULL, NULL);
10039
10040 nla_put_failure:
10041 nlmsg_free(msg);
10042 return -ENOBUFS;
10043 }
10044
10045 #endif /* CONFIG TDLS */
10046
10047
10048 #ifdef ANDROID
10049
10050 typedef struct android_wifi_priv_cmd {
10051 char *buf;
10052 int used_len;
10053 int total_len;
10054 } android_wifi_priv_cmd;
10055
10056 static int drv_errors = 0;
10057
10058 static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
10059 {
10060 drv_errors++;
10061 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
10062 drv_errors = 0;
10063 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
10064 }
10065 }
10066
10067
10068 static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
10069 {
10070 struct wpa_driver_nl80211_data *drv = bss->drv;
10071 struct ifreq ifr;
10072 android_wifi_priv_cmd priv_cmd;
10073 char buf[MAX_DRV_CMD_SIZE];
10074 int ret;
10075
10076 os_memset(&ifr, 0, sizeof(ifr));
10077 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
10078 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10079
10080 os_memset(buf, 0, sizeof(buf));
10081 os_strlcpy(buf, cmd, sizeof(buf));
10082
10083 priv_cmd.buf = buf;
10084 priv_cmd.used_len = sizeof(buf);
10085 priv_cmd.total_len = sizeof(buf);
10086 ifr.ifr_data = &priv_cmd;
10087
10088 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10089 if (ret < 0) {
10090 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
10091 __func__);
10092 wpa_driver_send_hang_msg(drv);
10093 return ret;
10094 }
10095
10096 drv_errors = 0;
10097 return 0;
10098 }
10099
10100
10101 static int android_pno_start(struct i802_bss *bss,
10102 struct wpa_driver_scan_params *params)
10103 {
10104 struct wpa_driver_nl80211_data *drv = bss->drv;
10105 struct ifreq ifr;
10106 android_wifi_priv_cmd priv_cmd;
10107 int ret = 0, i = 0, bp;
10108 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
10109
10110 bp = WEXT_PNOSETUP_HEADER_SIZE;
10111 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
10112 buf[bp++] = WEXT_PNO_TLV_PREFIX;
10113 buf[bp++] = WEXT_PNO_TLV_VERSION;
10114 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
10115 buf[bp++] = WEXT_PNO_TLV_RESERVED;
10116
10117 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
10118 /* Check that there is enough space needed for 1 more SSID, the
10119 * other sections and null termination */
10120 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
10121 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
10122 break;
10123 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
10124 params->ssids[i].ssid,
10125 params->ssids[i].ssid_len);
10126 buf[bp++] = WEXT_PNO_SSID_SECTION;
10127 buf[bp++] = params->ssids[i].ssid_len;
10128 os_memcpy(&buf[bp], params->ssids[i].ssid,
10129 params->ssids[i].ssid_len);
10130 bp += params->ssids[i].ssid_len;
10131 i++;
10132 }
10133
10134 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
10135 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
10136 WEXT_PNO_SCAN_INTERVAL);
10137 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
10138
10139 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
10140 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
10141 WEXT_PNO_REPEAT);
10142 bp += WEXT_PNO_REPEAT_LENGTH;
10143
10144 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
10145 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
10146 WEXT_PNO_MAX_REPEAT);
10147 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
10148
10149 memset(&ifr, 0, sizeof(ifr));
10150 memset(&priv_cmd, 0, sizeof(priv_cmd));
10151 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
10152
10153 priv_cmd.buf = buf;
10154 priv_cmd.used_len = bp;
10155 priv_cmd.total_len = bp;
10156 ifr.ifr_data = &priv_cmd;
10157
10158 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
10159
10160 if (ret < 0) {
10161 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
10162 ret);
10163 wpa_driver_send_hang_msg(drv);
10164 return ret;
10165 }
10166
10167 drv_errors = 0;
10168
10169 return android_priv_cmd(bss, "PNOFORCE 1");
10170 }
10171
10172
10173 static int android_pno_stop(struct i802_bss *bss)
10174 {
10175 return android_priv_cmd(bss, "PNOFORCE 0");
10176 }
10177
10178 #endif /* ANDROID */
10179
10180
10181 static int driver_nl80211_set_key(const char *ifname, void *priv,
10182 enum wpa_alg alg, const u8 *addr,
10183 int key_idx, int set_tx,
10184 const u8 *seq, size_t seq_len,
10185 const u8 *key, size_t key_len)
10186 {
10187 struct i802_bss *bss = priv;
10188 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
10189 set_tx, seq, seq_len, key, key_len);
10190 }
10191
10192
10193 static int driver_nl80211_scan2(void *priv,
10194 struct wpa_driver_scan_params *params)
10195 {
10196 struct i802_bss *bss = priv;
10197 return wpa_driver_nl80211_scan(bss, params);
10198 }
10199
10200
10201 static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
10202 int reason_code)
10203 {
10204 struct i802_bss *bss = priv;
10205 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
10206 }
10207
10208
10209 static int driver_nl80211_authenticate(void *priv,
10210 struct wpa_driver_auth_params *params)
10211 {
10212 struct i802_bss *bss = priv;
10213 return wpa_driver_nl80211_authenticate(bss, params);
10214 }
10215
10216
10217 static void driver_nl80211_deinit(void *priv)
10218 {
10219 struct i802_bss *bss = priv;
10220 wpa_driver_nl80211_deinit(bss);
10221 }
10222
10223
10224 static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
10225 const char *ifname)
10226 {
10227 struct i802_bss *bss = priv;
10228 return wpa_driver_nl80211_if_remove(bss, type, ifname);
10229 }
10230
10231
10232 static int driver_nl80211_send_mlme(void *priv, const u8 *data,
10233 size_t data_len, int noack)
10234 {
10235 struct i802_bss *bss = priv;
10236 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
10237 0, 0, 0, 0);
10238 }
10239
10240
10241 static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
10242 {
10243 struct i802_bss *bss = priv;
10244 return wpa_driver_nl80211_sta_remove(bss, addr);
10245 }
10246
10247
10248 #if defined(HOSTAPD) || defined(CONFIG_AP)
10249 static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
10250 const char *ifname, int vlan_id)
10251 {
10252 struct i802_bss *bss = priv;
10253 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
10254 }
10255 #endif /* HOSTAPD || CONFIG_AP */
10256
10257
10258 static int driver_nl80211_read_sta_data(void *priv,
10259 struct hostap_sta_driver_data *data,
10260 const u8 *addr)
10261 {
10262 struct i802_bss *bss = priv;
10263 return i802_read_sta_data(bss, data, addr);
10264 }
10265
10266
10267 static int driver_nl80211_send_action(void *priv, unsigned int freq,
10268 unsigned int wait_time,
10269 const u8 *dst, const u8 *src,
10270 const u8 *bssid,
10271 const u8 *data, size_t data_len,
10272 int no_cck)
10273 {
10274 struct i802_bss *bss = priv;
10275 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
10276 bssid, data, data_len, no_cck);
10277 }
10278
10279
10280 static int driver_nl80211_probe_req_report(void *priv, int report)
10281 {
10282 struct i802_bss *bss = priv;
10283 return wpa_driver_nl80211_probe_req_report(bss, report);
10284 }
10285
10286
10287 static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
10288 const u8 *ies, size_t ies_len)
10289 {
10290 int ret;
10291 struct nl_msg *msg;
10292 struct i802_bss *bss = priv;
10293 struct wpa_driver_nl80211_data *drv = bss->drv;
10294 u16 mdid = WPA_GET_LE16(md);
10295
10296 msg = nlmsg_alloc();
10297 if (!msg)
10298 return -ENOMEM;
10299
10300 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
10301 nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
10302 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
10303 NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
10304 NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
10305
10306 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
10307 if (ret) {
10308 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
10309 "err=%d (%s)", ret, strerror(-ret));
10310 }
10311
10312 return ret;
10313
10314 nla_put_failure:
10315 nlmsg_free(msg);
10316 return -ENOBUFS;
10317 }
10318
10319
10320 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
10321 .name = "nl80211",
10322 .desc = "Linux nl80211/cfg80211",
10323 .get_bssid = wpa_driver_nl80211_get_bssid,
10324 .get_ssid = wpa_driver_nl80211_get_ssid,
10325 .set_key = driver_nl80211_set_key,
10326 .scan2 = driver_nl80211_scan2,
10327 .sched_scan = wpa_driver_nl80211_sched_scan,
10328 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
10329 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
10330 .deauthenticate = driver_nl80211_deauthenticate,
10331 .authenticate = driver_nl80211_authenticate,
10332 .associate = wpa_driver_nl80211_associate,
10333 .global_init = nl80211_global_init,
10334 .global_deinit = nl80211_global_deinit,
10335 .init2 = wpa_driver_nl80211_init,
10336 .deinit = driver_nl80211_deinit,
10337 .get_capa = wpa_driver_nl80211_get_capa,
10338 .set_operstate = wpa_driver_nl80211_set_operstate,
10339 .set_supp_port = wpa_driver_nl80211_set_supp_port,
10340 .set_country = wpa_driver_nl80211_set_country,
10341 .set_ap = wpa_driver_nl80211_set_ap,
10342 .set_acl = wpa_driver_nl80211_set_acl,
10343 .if_add = wpa_driver_nl80211_if_add,
10344 .if_remove = driver_nl80211_if_remove,
10345 .send_mlme = driver_nl80211_send_mlme,
10346 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
10347 .sta_add = wpa_driver_nl80211_sta_add,
10348 .sta_remove = driver_nl80211_sta_remove,
10349 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
10350 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
10351 #ifdef HOSTAPD
10352 .hapd_init = i802_init,
10353 .hapd_deinit = i802_deinit,
10354 .set_wds_sta = i802_set_wds_sta,
10355 #endif /* HOSTAPD */
10356 #if defined(HOSTAPD) || defined(CONFIG_AP)
10357 .get_seqnum = i802_get_seqnum,
10358 .flush = i802_flush,
10359 .get_inact_sec = i802_get_inact_sec,
10360 .sta_clear_stats = i802_sta_clear_stats,
10361 .set_rts = i802_set_rts,
10362 .set_frag = i802_set_frag,
10363 .set_tx_queue_params = i802_set_tx_queue_params,
10364 .set_sta_vlan = driver_nl80211_set_sta_vlan,
10365 .sta_deauth = i802_sta_deauth,
10366 .sta_disassoc = i802_sta_disassoc,
10367 #endif /* HOSTAPD || CONFIG_AP */
10368 .read_sta_data = driver_nl80211_read_sta_data,
10369 .set_freq = i802_set_freq,
10370 .send_action = driver_nl80211_send_action,
10371 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
10372 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
10373 .cancel_remain_on_channel =
10374 wpa_driver_nl80211_cancel_remain_on_channel,
10375 .probe_req_report = driver_nl80211_probe_req_report,
10376 .deinit_ap = wpa_driver_nl80211_deinit_ap,
10377 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
10378 .resume = wpa_driver_nl80211_resume,
10379 .send_ft_action = nl80211_send_ft_action,
10380 .signal_monitor = nl80211_signal_monitor,
10381 .signal_poll = nl80211_signal_poll,
10382 .send_frame = nl80211_send_frame,
10383 .shared_freq = wpa_driver_nl80211_shared_freq,
10384 .set_param = nl80211_set_param,
10385 .get_radio_name = nl80211_get_radio_name,
10386 .add_pmkid = nl80211_add_pmkid,
10387 .remove_pmkid = nl80211_remove_pmkid,
10388 .flush_pmkid = nl80211_flush_pmkid,
10389 .set_rekey_info = nl80211_set_rekey_info,
10390 .poll_client = nl80211_poll_client,
10391 .set_p2p_powersave = nl80211_set_p2p_powersave,
10392 .start_dfs_cac = nl80211_start_radar_detection,
10393 .stop_ap = wpa_driver_nl80211_stop_ap,
10394 #ifdef CONFIG_TDLS
10395 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
10396 .tdls_oper = nl80211_tdls_oper,
10397 #endif /* CONFIG_TDLS */
10398 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
10399 };