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