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