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