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