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