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