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