]>
Commit | Line | Data |
---|---|---|
3f5285e8 | 1 | /* |
c5121837 | 2 | * Driver interaction with Linux nl80211/cfg80211 |
90a545cc | 3 | * Copyright (c) 2002-2014, 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 | 8 | * |
0f3d578e JM |
9 | * This software may be distributed under the terms of the BSD license. |
10 | * See README for more details. | |
3f5285e8 JM |
11 | */ |
12 | ||
13 | #include "includes.h" | |
6859f1cb | 14 | #include <sys/types.h> |
73d2294f | 15 | #include <fcntl.h> |
37b7d082 | 16 | #include <net/if.h> |
3f5285e8 | 17 | #include <netlink/genl/genl.h> |
3f5285e8 | 18 | #include <netlink/genl/ctrl.h> |
97ed9a06 KP |
19 | #ifdef CONFIG_LIBNL3_ROUTE |
20 | #include <netlink/route/neighbour.h> | |
21 | #endif /* CONFIG_LIBNL3_ROUTE */ | |
8602b0f2 | 22 | #include <linux/rtnetlink.h> |
1b648c7e | 23 | #include <netpacket/packet.h> |
32ab4855 | 24 | #include <linux/errqueue.h> |
625f587b | 25 | |
3f5285e8 | 26 | #include "common.h" |
1b648c7e | 27 | #include "eloop.h" |
1682c623 | 28 | #include "common/qca-vendor.h" |
a26582cb | 29 | #include "common/qca-vendor-attr.h" |
1b648c7e | 30 | #include "common/ieee802_11_defs.h" |
9b90955e | 31 | #include "common/ieee802_11_common.h" |
f10bfc9a | 32 | #include "l2_packet/l2_packet.h" |
e2d02c29 | 33 | #include "netlink.h" |
1474e6e9 | 34 | #include "linux_defines.h" |
34f2f814 | 35 | #include "linux_ioctl.h" |
e2d02c29 JM |
36 | #include "radiotap.h" |
37 | #include "radiotap_iter.h" | |
8401a6b0 | 38 | #include "rfkill.h" |
1474e6e9 | 39 | #include "driver_nl80211.h" |
32ab4855 | 40 | |
1474e6e9 JM |
41 | |
42 | #ifndef CONFIG_LIBNL20 | |
a65a9aed JB |
43 | /* |
44 | * libnl 1.1 has a bug, it tries to allocate socket numbers densely | |
45 | * but when you free a socket again it will mess up its bitmap and | |
46 | * and use the wrong number the next time it needs a socket ID. | |
47 | * Therefore, we wrap the handle alloc/destroy and add our own pid | |
48 | * accounting. | |
49 | */ | |
50 | static uint32_t port_bitmap[32] = { 0 }; | |
51 | ||
52 | static struct nl_handle *nl80211_handle_alloc(void *cb) | |
53 | { | |
54 | struct nl_handle *handle; | |
55 | uint32_t pid = getpid() & 0x3FFFFF; | |
56 | int i; | |
57 | ||
58 | handle = nl_handle_alloc_cb(cb); | |
59 | ||
60 | for (i = 0; i < 1024; i++) { | |
61 | if (port_bitmap[i / 32] & (1 << (i % 32))) | |
62 | continue; | |
63 | port_bitmap[i / 32] |= 1 << (i % 32); | |
64 | pid += i << 22; | |
65 | break; | |
66 | } | |
67 | ||
68 | nl_socket_set_local_port(handle, pid); | |
69 | ||
70 | return handle; | |
71 | } | |
72 | ||
73 | static void nl80211_handle_destroy(struct nl_handle *handle) | |
74 | { | |
75 | uint32_t port = nl_socket_get_local_port(handle); | |
76 | ||
77 | port >>= 22; | |
78 | port_bitmap[port / 32] &= ~(1 << (port % 32)); | |
79 | ||
80 | nl_handle_destroy(handle); | |
81 | } | |
c5121837 JM |
82 | #endif /* CONFIG_LIBNL20 */ |
83 | ||
c5121837 | 84 | |
1c6edec6 JM |
85 | #ifdef ANDROID |
86 | /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */ | |
1c6edec6 JM |
87 | #undef nl_socket_set_nonblocking |
88 | #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h) | |
70a96c81 JM |
89 | |
90 | #define genl_ctrl_resolve android_genl_ctrl_resolve | |
1c6edec6 JM |
91 | #endif /* ANDROID */ |
92 | ||
93 | ||
481234cf | 94 | static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg) |
a92dfde8 | 95 | { |
481234cf | 96 | struct nl_handle *handle; |
a92dfde8 | 97 | |
481234cf JM |
98 | handle = nl80211_handle_alloc(cb); |
99 | if (handle == NULL) { | |
a92dfde8 JB |
100 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " |
101 | "callbacks (%s)", dbg); | |
481234cf | 102 | return NULL; |
a92dfde8 JB |
103 | } |
104 | ||
481234cf | 105 | if (genl_connect(handle)) { |
a92dfde8 JB |
106 | wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " |
107 | "netlink (%s)", dbg); | |
481234cf JM |
108 | nl80211_handle_destroy(handle); |
109 | return NULL; | |
a92dfde8 JB |
110 | } |
111 | ||
481234cf | 112 | return handle; |
a92dfde8 JB |
113 | } |
114 | ||
115 | ||
481234cf | 116 | static void nl_destroy_handles(struct nl_handle **handle) |
a92dfde8 | 117 | { |
481234cf | 118 | if (*handle == NULL) |
a92dfde8 | 119 | return; |
481234cf JM |
120 | nl80211_handle_destroy(*handle); |
121 | *handle = NULL; | |
a92dfde8 JB |
122 | } |
123 | ||
124 | ||
10b85921 JB |
125 | #if __WORDSIZE == 64 |
126 | #define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL | |
127 | #else | |
128 | #define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL | |
129 | #endif | |
130 | ||
5f65e9f7 JB |
131 | static void nl80211_register_eloop_read(struct nl_handle **handle, |
132 | eloop_sock_handler handler, | |
133 | void *eloop_data) | |
134 | { | |
10b85921 | 135 | nl_socket_set_nonblocking(*handle); |
5f65e9f7 JB |
136 | eloop_register_read_sock(nl_socket_get_fd(*handle), handler, |
137 | eloop_data, *handle); | |
10b85921 | 138 | *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID); |
5f65e9f7 JB |
139 | } |
140 | ||
141 | ||
142 | static void nl80211_destroy_eloop_handle(struct nl_handle **handle) | |
143 | { | |
10b85921 | 144 | *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID); |
5f65e9f7 JB |
145 | eloop_unregister_read_sock(nl_socket_get_fd(*handle)); |
146 | nl_destroy_handles(handle); | |
147 | } | |
148 | ||
149 | ||
36d84860 BG |
150 | static void nl80211_global_deinit(void *priv); |
151 | ||
9ebce9c5 | 152 | static void wpa_driver_nl80211_deinit(struct i802_bss *bss); |
4ec68377 JD |
153 | static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss, |
154 | struct hostapd_freq_params *freq); | |
3c5d34e3 | 155 | |
362f781e | 156 | static int |
0d547d5f | 157 | wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv, |
0ecff8d7 JM |
158 | const u8 *set_addr, int first, |
159 | const char *driver_params); | |
88df0ef7 | 160 | static int nl80211_send_frame_cmd(struct i802_bss *bss, |
5dfca53f | 161 | unsigned int freq, unsigned int wait, |
b106173a | 162 | const u8 *buf, size_t buf_len, u64 *cookie, |
88df0ef7 | 163 | int no_cck, int no_ack, int offchanok); |
9ebce9c5 JM |
164 | static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, |
165 | int report); | |
0915d02c | 166 | |
2135f224 JM |
167 | static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); |
168 | static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); | |
97cfcf64 | 169 | static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); |
072ad14c | 170 | |
e87ef751 PX |
171 | static int nl80211_set_channel(struct i802_bss *bss, |
172 | struct hostapd_freq_params *freq, int set_chan); | |
4e5cb1a3 JM |
173 | static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, |
174 | int ifindex, int disabled); | |
504e905c | 175 | |
666e508c JM |
176 | static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv, |
177 | int reset_mode); | |
21bdbe38 | 178 | |
bf83eab5 | 179 | static int i802_set_iface_flags(struct i802_bss *bss, int up); |
0ecff8d7 | 180 | static int nl80211_set_param(void *priv, const char *param); |
bf83eab5 | 181 | |
3f5285e8 | 182 | |
8d1fdde7 | 183 | /* Converts nl80211_chan_width to a common format */ |
f3407c66 | 184 | enum chan_width convert2width(int width) |
8d1fdde7 JD |
185 | { |
186 | switch (width) { | |
187 | case NL80211_CHAN_WIDTH_20_NOHT: | |
188 | return CHAN_WIDTH_20_NOHT; | |
189 | case NL80211_CHAN_WIDTH_20: | |
190 | return CHAN_WIDTH_20; | |
191 | case NL80211_CHAN_WIDTH_40: | |
192 | return CHAN_WIDTH_40; | |
193 | case NL80211_CHAN_WIDTH_80: | |
194 | return CHAN_WIDTH_80; | |
195 | case NL80211_CHAN_WIDTH_80P80: | |
196 | return CHAN_WIDTH_80P80; | |
197 | case NL80211_CHAN_WIDTH_160: | |
198 | return CHAN_WIDTH_160; | |
199 | } | |
200 | return CHAN_WIDTH_UNKNOWN; | |
201 | } | |
202 | ||
203 | ||
f3407c66 | 204 | int is_ap_interface(enum nl80211_iftype nlmode) |
b1f625e0 | 205 | { |
0e80ea2c JM |
206 | return nlmode == NL80211_IFTYPE_AP || |
207 | nlmode == NL80211_IFTYPE_P2P_GO; | |
b1f625e0 EP |
208 | } |
209 | ||
210 | ||
477af8f8 | 211 | int is_sta_interface(enum nl80211_iftype nlmode) |
b1f625e0 | 212 | { |
0e80ea2c JM |
213 | return nlmode == NL80211_IFTYPE_STATION || |
214 | nlmode == NL80211_IFTYPE_P2P_CLIENT; | |
b1f625e0 EP |
215 | } |
216 | ||
217 | ||
6a71413e | 218 | static int is_p2p_net_interface(enum nl80211_iftype nlmode) |
b3af99d2 | 219 | { |
0e80ea2c JM |
220 | return nlmode == NL80211_IFTYPE_P2P_CLIENT || |
221 | nlmode == NL80211_IFTYPE_P2P_GO; | |
b3af99d2 JM |
222 | } |
223 | ||
224 | ||
f3407c66 JM |
225 | struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv, |
226 | int ifindex) | |
5dfbd725 JM |
227 | { |
228 | struct i802_bss *bss; | |
229 | ||
230 | for (bss = drv->first_bss; bss; bss = bss->next) { | |
231 | if (bss->ifindex == ifindex) | |
232 | return bss; | |
233 | } | |
234 | ||
235 | return NULL; | |
236 | } | |
237 | ||
238 | ||
afb0550a BC |
239 | static int is_mesh_interface(enum nl80211_iftype nlmode) |
240 | { | |
241 | return nlmode == NL80211_IFTYPE_MESH_POINT; | |
242 | } | |
243 | ||
244 | ||
f3407c66 | 245 | void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv) |
add9b7a4 JM |
246 | { |
247 | if (drv->associated) | |
248 | os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN); | |
249 | drv->associated = 0; | |
250 | os_memset(drv->bssid, 0, ETH_ALEN); | |
251 | } | |
252 | ||
253 | ||
6241fcb1 JM |
254 | /* nl80211 code */ |
255 | static int ack_handler(struct nl_msg *msg, void *arg) | |
256 | { | |
257 | int *err = arg; | |
258 | *err = 0; | |
259 | return NL_STOP; | |
260 | } | |
261 | ||
262 | static int finish_handler(struct nl_msg *msg, void *arg) | |
263 | { | |
8e8df255 JM |
264 | int *ret = arg; |
265 | *ret = 0; | |
6241fcb1 JM |
266 | return NL_SKIP; |
267 | } | |
268 | ||
269 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, | |
270 | void *arg) | |
271 | { | |
272 | int *ret = arg; | |
273 | *ret = err->error; | |
274 | return NL_SKIP; | |
275 | } | |
276 | ||
5b7b85f6 JM |
277 | |
278 | static int no_seq_check(struct nl_msg *msg, void *arg) | |
279 | { | |
280 | return NL_OK; | |
281 | } | |
282 | ||
283 | ||
d6c9aab8 | 284 | static int send_and_recv(struct nl80211_global *global, |
58f6fbe0 JM |
285 | struct nl_handle *nl_handle, struct nl_msg *msg, |
286 | int (*valid_handler)(struct nl_msg *, void *), | |
287 | void *valid_data) | |
6241fcb1 JM |
288 | { |
289 | struct nl_cb *cb; | |
290 | int err = -ENOMEM; | |
291 | ||
9725b784 JM |
292 | if (!msg) |
293 | return -ENOMEM; | |
294 | ||
d6c9aab8 | 295 | cb = nl_cb_clone(global->nl_cb); |
6241fcb1 JM |
296 | if (!cb) |
297 | goto out; | |
298 | ||
58f6fbe0 | 299 | err = nl_send_auto_complete(nl_handle, msg); |
6241fcb1 JM |
300 | if (err < 0) |
301 | goto out; | |
302 | ||
303 | err = 1; | |
304 | ||
305 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); | |
8e8df255 | 306 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); |
6241fcb1 JM |
307 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); |
308 | ||
309 | if (valid_handler) | |
310 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, | |
311 | valid_handler, valid_data); | |
312 | ||
34068ac3 JM |
313 | while (err > 0) { |
314 | int res = nl_recvmsgs(nl_handle, cb); | |
d3d04831 | 315 | if (res < 0) { |
34068ac3 JM |
316 | wpa_printf(MSG_INFO, |
317 | "nl80211: %s->nl_recvmsgs failed: %d", | |
318 | __func__, res); | |
319 | } | |
320 | } | |
6241fcb1 JM |
321 | out: |
322 | nl_cb_put(cb); | |
323 | nlmsg_free(msg); | |
324 | return err; | |
325 | } | |
326 | ||
327 | ||
f3407c66 JM |
328 | int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv, |
329 | struct nl_msg *msg, | |
330 | int (*valid_handler)(struct nl_msg *, void *), | |
331 | void *valid_data) | |
58f6fbe0 | 332 | { |
481234cf | 333 | return send_and_recv(drv->global, drv->global->nl, msg, |
d6c9aab8 | 334 | valid_handler, valid_data); |
58f6fbe0 JM |
335 | } |
336 | ||
337 | ||
97865538 JM |
338 | struct family_data { |
339 | const char *group; | |
340 | int id; | |
341 | }; | |
342 | ||
343 | ||
344 | static int family_handler(struct nl_msg *msg, void *arg) | |
345 | { | |
346 | struct family_data *res = arg; | |
347 | struct nlattr *tb[CTRL_ATTR_MAX + 1]; | |
348 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
349 | struct nlattr *mcgrp; | |
350 | int i; | |
351 | ||
352 | nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
353 | genlmsg_attrlen(gnlh, 0), NULL); | |
354 | if (!tb[CTRL_ATTR_MCAST_GROUPS]) | |
355 | return NL_SKIP; | |
356 | ||
357 | nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { | |
358 | struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; | |
359 | nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), | |
360 | nla_len(mcgrp), NULL); | |
361 | if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || | |
362 | !tb2[CTRL_ATTR_MCAST_GRP_ID] || | |
363 | os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]), | |
364 | res->group, | |
365 | nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0) | |
366 | continue; | |
367 | res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); | |
368 | break; | |
369 | }; | |
370 | ||
371 | return NL_SKIP; | |
372 | } | |
373 | ||
374 | ||
d6c9aab8 | 375 | static int nl_get_multicast_id(struct nl80211_global *global, |
97865538 JM |
376 | const char *family, const char *group) |
377 | { | |
378 | struct nl_msg *msg; | |
a862e4a3 | 379 | int ret; |
97865538 JM |
380 | struct family_data res = { group, -ENOENT }; |
381 | ||
382 | msg = nlmsg_alloc(); | |
383 | if (!msg) | |
384 | return -ENOMEM; | |
a862e4a3 JM |
385 | if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"), |
386 | 0, 0, CTRL_CMD_GETFAMILY, 0) || | |
387 | nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) { | |
388 | nlmsg_free(msg); | |
389 | return -1; | |
390 | } | |
97865538 | 391 | |
1033315f | 392 | ret = send_and_recv(global, global->nl, msg, family_handler, &res); |
97865538 JM |
393 | if (ret == 0) |
394 | ret = res.id; | |
97865538 JM |
395 | return ret; |
396 | } | |
397 | ||
398 | ||
f3407c66 JM |
399 | void * nl80211_cmd(struct wpa_driver_nl80211_data *drv, |
400 | struct nl_msg *msg, int flags, uint8_t cmd) | |
9fb04070 | 401 | { |
335d42b1 | 402 | return genlmsg_put(msg, 0, 0, drv->global->nl80211_id, |
276e2d67 | 403 | 0, flags, cmd, 0); |
9fb04070 JM |
404 | } |
405 | ||
406 | ||
350acc35 JM |
407 | static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss) |
408 | { | |
409 | if (bss->wdev_id_set) | |
410 | return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id); | |
411 | return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); | |
412 | } | |
413 | ||
414 | ||
07c7757c JM |
415 | struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd) |
416 | { | |
417 | struct nl_msg *msg; | |
418 | ||
419 | msg = nlmsg_alloc(); | |
420 | if (!msg) | |
421 | return NULL; | |
422 | ||
423 | if (!nl80211_cmd(bss->drv, msg, flags, cmd) || | |
424 | nl80211_set_iface_id(msg, bss) < 0) { | |
425 | nlmsg_free(msg); | |
426 | return NULL; | |
427 | } | |
428 | ||
429 | return msg; | |
430 | } | |
431 | ||
432 | ||
433 | static struct nl_msg * | |
434 | nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex, | |
435 | int flags, uint8_t cmd) | |
436 | { | |
437 | struct nl_msg *msg; | |
438 | ||
439 | msg = nlmsg_alloc(); | |
440 | if (!msg) | |
441 | return NULL; | |
442 | ||
443 | if (!nl80211_cmd(drv, msg, flags, cmd) || | |
444 | nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) { | |
445 | nlmsg_free(msg); | |
446 | return NULL; | |
447 | } | |
448 | ||
449 | return msg; | |
450 | } | |
451 | ||
452 | ||
453 | struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags, | |
454 | uint8_t cmd) | |
455 | { | |
456 | return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd); | |
457 | } | |
458 | ||
459 | ||
460 | struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd) | |
461 | { | |
462 | return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd); | |
463 | } | |
464 | ||
465 | ||
e32ad281 JB |
466 | struct wiphy_idx_data { |
467 | int wiphy_idx; | |
01517c8b | 468 | enum nl80211_iftype nlmode; |
597b94f5 | 469 | u8 *macaddr; |
e32ad281 JB |
470 | }; |
471 | ||
472 | ||
473 | static int netdev_info_handler(struct nl_msg *msg, void *arg) | |
474 | { | |
475 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
476 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
477 | struct wiphy_idx_data *info = arg; | |
478 | ||
479 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
480 | genlmsg_attrlen(gnlh, 0), NULL); | |
481 | ||
482 | if (tb[NL80211_ATTR_WIPHY]) | |
483 | info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]); | |
484 | ||
01517c8b JB |
485 | if (tb[NL80211_ATTR_IFTYPE]) |
486 | info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]); | |
487 | ||
597b94f5 AS |
488 | if (tb[NL80211_ATTR_MAC] && info->macaddr) |
489 | os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]), | |
490 | ETH_ALEN); | |
491 | ||
e32ad281 JB |
492 | return NL_SKIP; |
493 | } | |
494 | ||
495 | ||
f3407c66 | 496 | int nl80211_get_wiphy_index(struct i802_bss *bss) |
e32ad281 JB |
497 | { |
498 | struct nl_msg *msg; | |
499 | struct wiphy_idx_data data = { | |
500 | .wiphy_idx = -1, | |
597b94f5 | 501 | .macaddr = NULL, |
e32ad281 JB |
502 | }; |
503 | ||
56f77852 | 504 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE))) |
a862e4a3 | 505 | return -1; |
e32ad281 JB |
506 | |
507 | if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0) | |
508 | return data.wiphy_idx; | |
e32ad281 JB |
509 | return -1; |
510 | } | |
511 | ||
512 | ||
01517c8b JB |
513 | static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss) |
514 | { | |
515 | struct nl_msg *msg; | |
516 | struct wiphy_idx_data data = { | |
597b94f5 AS |
517 | .nlmode = NL80211_IFTYPE_UNSPECIFIED, |
518 | .macaddr = NULL, | |
01517c8b JB |
519 | }; |
520 | ||
56f77852 | 521 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE))) |
a862e4a3 | 522 | return NL80211_IFTYPE_UNSPECIFIED; |
01517c8b JB |
523 | |
524 | if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0) | |
525 | return data.nlmode; | |
01517c8b JB |
526 | return NL80211_IFTYPE_UNSPECIFIED; |
527 | } | |
01517c8b JB |
528 | |
529 | ||
597b94f5 AS |
530 | static int nl80211_get_macaddr(struct i802_bss *bss) |
531 | { | |
532 | struct nl_msg *msg; | |
533 | struct wiphy_idx_data data = { | |
534 | .macaddr = bss->addr, | |
535 | }; | |
536 | ||
56f77852 | 537 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE))) |
a862e4a3 | 538 | return -1; |
597b94f5 | 539 | |
597b94f5 | 540 | return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data); |
597b94f5 | 541 | } |
597b94f5 AS |
542 | |
543 | ||
e32ad281 JB |
544 | static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv, |
545 | struct nl80211_wiphy_data *w) | |
546 | { | |
547 | struct nl_msg *msg; | |
a862e4a3 | 548 | int ret; |
e32ad281 JB |
549 | |
550 | msg = nlmsg_alloc(); | |
551 | if (!msg) | |
552 | return -1; | |
553 | ||
a862e4a3 JM |
554 | if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) || |
555 | nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) { | |
556 | nlmsg_free(msg); | |
557 | return -1; | |
558 | } | |
e32ad281 | 559 | |
481234cf | 560 | ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL); |
e32ad281 JB |
561 | if (ret) { |
562 | wpa_printf(MSG_DEBUG, "nl80211: Register beacons command " | |
563 | "failed: ret=%d (%s)", | |
564 | ret, strerror(-ret)); | |
e32ad281 | 565 | } |
e32ad281 JB |
566 | return ret; |
567 | } | |
568 | ||
569 | ||
570 | static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle) | |
571 | { | |
572 | struct nl80211_wiphy_data *w = eloop_ctx; | |
34068ac3 | 573 | int res; |
e32ad281 | 574 | |
ee9fc67a | 575 | wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available"); |
e32ad281 | 576 | |
34068ac3 | 577 | res = nl_recvmsgs(handle, w->nl_cb); |
d3d04831 | 578 | if (res < 0) { |
34068ac3 JM |
579 | wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d", |
580 | __func__, res); | |
581 | } | |
e32ad281 JB |
582 | } |
583 | ||
584 | ||
585 | static int process_beacon_event(struct nl_msg *msg, void *arg) | |
586 | { | |
587 | struct nl80211_wiphy_data *w = arg; | |
588 | struct wpa_driver_nl80211_data *drv; | |
589 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
590 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
591 | union wpa_event_data event; | |
592 | ||
593 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
594 | genlmsg_attrlen(gnlh, 0), NULL); | |
595 | ||
596 | if (gnlh->cmd != NL80211_CMD_FRAME) { | |
597 | wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)", | |
598 | gnlh->cmd); | |
599 | return NL_SKIP; | |
600 | } | |
601 | ||
602 | if (!tb[NL80211_ATTR_FRAME]) | |
603 | return NL_SKIP; | |
604 | ||
605 | dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data, | |
606 | wiphy_list) { | |
607 | os_memset(&event, 0, sizeof(event)); | |
608 | event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]); | |
609 | event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]); | |
610 | wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); | |
611 | } | |
612 | ||
613 | return NL_SKIP; | |
614 | } | |
615 | ||
616 | ||
617 | static struct nl80211_wiphy_data * | |
618 | nl80211_get_wiphy_data_ap(struct i802_bss *bss) | |
619 | { | |
620 | static DEFINE_DL_LIST(nl80211_wiphys); | |
621 | struct nl80211_wiphy_data *w; | |
622 | int wiphy_idx, found = 0; | |
623 | struct i802_bss *tmp_bss; | |
624 | ||
625 | if (bss->wiphy_data != NULL) | |
626 | return bss->wiphy_data; | |
627 | ||
628 | wiphy_idx = nl80211_get_wiphy_index(bss); | |
629 | ||
630 | dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) { | |
631 | if (w->wiphy_idx == wiphy_idx) | |
632 | goto add; | |
633 | } | |
634 | ||
635 | /* alloc new one */ | |
636 | w = os_zalloc(sizeof(*w)); | |
637 | if (w == NULL) | |
638 | return NULL; | |
639 | w->wiphy_idx = wiphy_idx; | |
640 | dl_list_init(&w->bsss); | |
641 | dl_list_init(&w->drvs); | |
642 | ||
643 | w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); | |
644 | if (!w->nl_cb) { | |
645 | os_free(w); | |
646 | return NULL; | |
647 | } | |
648 | nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); | |
649 | nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event, | |
650 | w); | |
651 | ||
481234cf JM |
652 | w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb, |
653 | "wiphy beacons"); | |
654 | if (w->nl_beacons == NULL) { | |
e32ad281 JB |
655 | os_free(w); |
656 | return NULL; | |
657 | } | |
658 | ||
659 | if (nl80211_register_beacons(bss->drv, w)) { | |
660 | nl_destroy_handles(&w->nl_beacons); | |
661 | os_free(w); | |
662 | return NULL; | |
663 | } | |
664 | ||
5f65e9f7 | 665 | nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w); |
e32ad281 JB |
666 | |
667 | dl_list_add(&nl80211_wiphys, &w->list); | |
668 | ||
669 | add: | |
670 | /* drv entry for this bss already there? */ | |
671 | dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { | |
672 | if (tmp_bss->drv == bss->drv) { | |
673 | found = 1; | |
674 | break; | |
675 | } | |
676 | } | |
677 | /* if not add it */ | |
678 | if (!found) | |
679 | dl_list_add(&w->drvs, &bss->drv->wiphy_list); | |
680 | ||
681 | dl_list_add(&w->bsss, &bss->wiphy_list); | |
682 | bss->wiphy_data = w; | |
683 | return w; | |
684 | } | |
685 | ||
686 | ||
687 | static void nl80211_put_wiphy_data_ap(struct i802_bss *bss) | |
688 | { | |
689 | struct nl80211_wiphy_data *w = bss->wiphy_data; | |
690 | struct i802_bss *tmp_bss; | |
691 | int found = 0; | |
692 | ||
693 | if (w == NULL) | |
694 | return; | |
695 | bss->wiphy_data = NULL; | |
696 | dl_list_del(&bss->wiphy_list); | |
697 | ||
698 | /* still any for this drv present? */ | |
699 | dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) { | |
700 | if (tmp_bss->drv == bss->drv) { | |
701 | found = 1; | |
702 | break; | |
703 | } | |
704 | } | |
705 | /* if not remove it */ | |
706 | if (!found) | |
707 | dl_list_del(&bss->drv->wiphy_list); | |
708 | ||
709 | if (!dl_list_empty(&w->bsss)) | |
710 | return; | |
711 | ||
5f65e9f7 | 712 | nl80211_destroy_eloop_handle(&w->nl_beacons); |
e32ad281 JB |
713 | |
714 | nl_cb_put(w->nl_cb); | |
e32ad281 JB |
715 | dl_list_del(&w->list); |
716 | os_free(w); | |
717 | } | |
718 | ||
719 | ||
3f5285e8 JM |
720 | static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid) |
721 | { | |
a2e40bb6 FF |
722 | struct i802_bss *bss = priv; |
723 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c2a04078 JM |
724 | if (!drv->associated) |
725 | return -1; | |
726 | os_memcpy(bssid, drv->bssid, ETH_ALEN); | |
727 | return 0; | |
3f5285e8 JM |
728 | } |
729 | ||
730 | ||
3f5285e8 JM |
731 | static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid) |
732 | { | |
a2e40bb6 FF |
733 | struct i802_bss *bss = priv; |
734 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
fd05d64e JM |
735 | if (!drv->associated) |
736 | return -1; | |
737 | os_memcpy(ssid, drv->ssid, drv->ssid_len); | |
738 | return drv->ssid_len; | |
3f5285e8 JM |
739 | } |
740 | ||
741 | ||
90a545cc JM |
742 | static void wpa_driver_nl80211_event_newlink( |
743 | struct wpa_driver_nl80211_data *drv, char *ifname) | |
3f5285e8 JM |
744 | { |
745 | union wpa_event_data event; | |
746 | ||
90a545cc JM |
747 | if (os_strcmp(drv->first_bss->ifname, ifname) == 0) { |
748 | if (if_nametoindex(drv->first_bss->ifname) == 0) { | |
749 | wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK", | |
750 | drv->first_bss->ifname); | |
751 | return; | |
752 | } | |
753 | if (!drv->if_removed) | |
754 | return; | |
755 | wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event", | |
756 | drv->first_bss->ifname); | |
757 | drv->if_removed = 0; | |
758 | } | |
759 | ||
3f5285e8 | 760 | os_memset(&event, 0, sizeof(event)); |
90a545cc JM |
761 | os_strlcpy(event.interface_status.ifname, ifname, |
762 | sizeof(event.interface_status.ifname)); | |
763 | event.interface_status.ievent = EVENT_INTERFACE_ADDED; | |
764 | wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); | |
765 | } | |
766 | ||
767 | ||
768 | static void wpa_driver_nl80211_event_dellink( | |
769 | struct wpa_driver_nl80211_data *drv, char *ifname) | |
770 | { | |
771 | union wpa_event_data event; | |
772 | ||
773 | if (os_strcmp(drv->first_bss->ifname, ifname) == 0) { | |
774 | if (drv->if_removed) { | |
775 | wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s", | |
776 | ifname); | |
777 | return; | |
d1f4942b | 778 | } |
90a545cc JM |
779 | wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1", |
780 | ifname); | |
781 | drv->if_removed = 1; | |
782 | } else { | |
783 | wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed", | |
784 | ifname); | |
7524cfb1 JM |
785 | } |
786 | ||
90a545cc JM |
787 | os_memset(&event, 0, sizeof(event)); |
788 | os_strlcpy(event.interface_status.ifname, ifname, | |
789 | sizeof(event.interface_status.ifname)); | |
790 | event.interface_status.ievent = EVENT_INTERFACE_REMOVED; | |
08063178 | 791 | wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); |
3f5285e8 JM |
792 | } |
793 | ||
794 | ||
7524cfb1 | 795 | static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv, |
62d680c3 | 796 | u8 *buf, size_t len) |
7524cfb1 | 797 | { |
62d680c3 | 798 | int attrlen, rta_len; |
7524cfb1 JM |
799 | struct rtattr *attr; |
800 | ||
62d680c3 JM |
801 | attrlen = len; |
802 | attr = (struct rtattr *) buf; | |
7524cfb1 JM |
803 | |
804 | rta_len = RTA_ALIGN(sizeof(struct rtattr)); | |
805 | while (RTA_OK(attr, attrlen)) { | |
806 | if (attr->rta_type == IFLA_IFNAME) { | |
834ee56f KP |
807 | if (os_strcmp(((char *) attr) + rta_len, |
808 | drv->first_bss->ifname) == 0) | |
7524cfb1 JM |
809 | return 1; |
810 | else | |
811 | break; | |
812 | } | |
813 | attr = RTA_NEXT(attr, attrlen); | |
814 | } | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
819 | ||
820 | static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv, | |
62d680c3 | 821 | int ifindex, u8 *buf, size_t len) |
7524cfb1 JM |
822 | { |
823 | if (drv->ifindex == ifindex) | |
824 | return 1; | |
825 | ||
62d680c3 | 826 | if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) { |
7524cfb1 JM |
827 | wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed " |
828 | "interface"); | |
0ecff8d7 | 829 | wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL); |
7524cfb1 JM |
830 | return 1; |
831 | } | |
832 | ||
833 | return 0; | |
834 | } | |
835 | ||
836 | ||
36d84860 BG |
837 | static struct wpa_driver_nl80211_data * |
838 | nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len) | |
839 | { | |
840 | struct wpa_driver_nl80211_data *drv; | |
841 | dl_list_for_each(drv, &global->interfaces, | |
842 | struct wpa_driver_nl80211_data, list) { | |
843 | if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) || | |
844 | have_ifidx(drv, idx)) | |
845 | return drv; | |
846 | } | |
847 | return NULL; | |
848 | } | |
849 | ||
850 | ||
62d680c3 JM |
851 | static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, |
852 | struct ifinfomsg *ifi, | |
853 | u8 *buf, size_t len) | |
3f5285e8 | 854 | { |
36d84860 BG |
855 | struct nl80211_global *global = ctx; |
856 | struct wpa_driver_nl80211_data *drv; | |
90a545cc | 857 | int attrlen; |
62d680c3 | 858 | struct rtattr *attr; |
97cfcf64 | 859 | u32 brid = 0; |
aef85ba2 | 860 | char namebuf[IFNAMSIZ]; |
90a545cc JM |
861 | char ifname[IFNAMSIZ + 1]; |
862 | char extra[100], *pos, *end; | |
3f5285e8 | 863 | |
36d84860 BG |
864 | drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); |
865 | if (!drv) { | |
90a545cc JM |
866 | wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d", |
867 | ifi->ifi_index); | |
3f5285e8 JM |
868 | return; |
869 | } | |
870 | ||
90a545cc JM |
871 | extra[0] = '\0'; |
872 | pos = extra; | |
873 | end = pos + sizeof(extra); | |
874 | ifname[0] = '\0'; | |
875 | ||
876 | attrlen = len; | |
877 | attr = (struct rtattr *) buf; | |
878 | while (RTA_OK(attr, attrlen)) { | |
879 | switch (attr->rta_type) { | |
880 | case IFLA_IFNAME: | |
881 | if (RTA_PAYLOAD(attr) >= IFNAMSIZ) | |
882 | break; | |
883 | os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr)); | |
884 | ifname[RTA_PAYLOAD(attr)] = '\0'; | |
885 | break; | |
886 | case IFLA_MASTER: | |
887 | brid = nla_get_u32((struct nlattr *) attr); | |
888 | pos += os_snprintf(pos, end - pos, " master=%u", brid); | |
889 | break; | |
890 | case IFLA_WIRELESS: | |
891 | pos += os_snprintf(pos, end - pos, " wext"); | |
892 | break; | |
893 | case IFLA_OPERSTATE: | |
894 | pos += os_snprintf(pos, end - pos, " operstate=%u", | |
895 | nla_get_u32((struct nlattr *) attr)); | |
896 | break; | |
897 | case IFLA_LINKMODE: | |
898 | pos += os_snprintf(pos, end - pos, " linkmode=%u", | |
899 | nla_get_u32((struct nlattr *) attr)); | |
900 | break; | |
901 | } | |
902 | attr = RTA_NEXT(attr, attrlen); | |
903 | } | |
904 | extra[sizeof(extra) - 1] = '\0'; | |
905 | ||
f2e90835 JM |
906 | wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)", |
907 | ifi->ifi_index, ifname, extra, ifi->ifi_family, | |
908 | ifi->ifi_flags, | |
3f5285e8 JM |
909 | (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", |
910 | (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", | |
911 | (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", | |
912 | (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); | |
a63063b4 JM |
913 | |
914 | if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) { | |
59d24925 JM |
915 | if (if_indextoname(ifi->ifi_index, namebuf) && |
916 | linux_iface_up(drv->global->ioctl_sock, | |
834ee56f | 917 | drv->first_bss->ifname) > 0) { |
59d24925 JM |
918 | wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " |
919 | "event since interface %s is up", namebuf); | |
7a94120e | 920 | drv->ignore_if_down_event = 0; |
59d24925 JM |
921 | return; |
922 | } | |
a63063b4 | 923 | wpa_printf(MSG_DEBUG, "nl80211: Interface down"); |
7d9c3698 JM |
924 | if (drv->ignore_if_down_event) { |
925 | wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down " | |
926 | "event generated by mode change"); | |
927 | drv->ignore_if_down_event = 0; | |
928 | } else { | |
929 | drv->if_disabled = 1; | |
930 | wpa_supplicant_event(drv->ctx, | |
931 | EVENT_INTERFACE_DISABLED, NULL); | |
819f096f AO |
932 | |
933 | /* | |
934 | * Try to get drv again, since it may be removed as | |
935 | * part of the EVENT_INTERFACE_DISABLED handling for | |
936 | * dynamic interfaces | |
937 | */ | |
938 | drv = nl80211_find_drv(global, ifi->ifi_index, | |
939 | buf, len); | |
940 | if (!drv) | |
941 | return; | |
7d9c3698 | 942 | } |
a63063b4 JM |
943 | } |
944 | ||
945 | if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) { | |
aef85ba2 JM |
946 | if (if_indextoname(ifi->ifi_index, namebuf) && |
947 | linux_iface_up(drv->global->ioctl_sock, | |
834ee56f | 948 | drv->first_bss->ifname) == 0) { |
aef85ba2 JM |
949 | wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " |
950 | "event since interface %s is down", | |
951 | namebuf); | |
834ee56f | 952 | } else if (if_nametoindex(drv->first_bss->ifname) == 0) { |
d1f4942b JM |
953 | wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " |
954 | "event since interface %s does not exist", | |
834ee56f | 955 | drv->first_bss->ifname); |
d1f4942b JM |
956 | } else if (drv->if_removed) { |
957 | wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up " | |
958 | "event since interface %s is marked " | |
834ee56f | 959 | "removed", drv->first_bss->ifname); |
aef85ba2 | 960 | } else { |
3e0272ca DW |
961 | struct i802_bss *bss; |
962 | u8 addr[ETH_ALEN]; | |
963 | ||
964 | /* Re-read MAC address as it may have changed */ | |
965 | bss = get_bss_ifindex(drv, ifi->ifi_index); | |
966 | if (bss && | |
967 | linux_get_ifhwaddr(drv->global->ioctl_sock, | |
968 | bss->ifname, addr) < 0) { | |
969 | wpa_printf(MSG_DEBUG, | |
970 | "nl80211: %s: failed to re-read MAC address", | |
971 | bss->ifname); | |
972 | } else if (bss && | |
973 | os_memcmp(addr, bss->addr, ETH_ALEN) != 0) { | |
974 | wpa_printf(MSG_DEBUG, | |
975 | "nl80211: Own MAC address on ifindex %d (%s) changed from " | |
976 | MACSTR " to " MACSTR, | |
977 | ifi->ifi_index, bss->ifname, | |
978 | MAC2STR(bss->addr), | |
979 | MAC2STR(addr)); | |
980 | os_memcpy(bss->addr, addr, ETH_ALEN); | |
981 | } | |
982 | ||
aef85ba2 JM |
983 | wpa_printf(MSG_DEBUG, "nl80211: Interface up"); |
984 | drv->if_disabled = 0; | |
985 | wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, | |
986 | NULL); | |
987 | } | |
a63063b4 JM |
988 | } |
989 | ||
3f5285e8 JM |
990 | /* |
991 | * Some drivers send the association event before the operup event--in | |
992 | * this case, lifting operstate in wpa_driver_nl80211_set_operstate() | |
993 | * fails. This will hit us when wpa_supplicant does not need to do | |
994 | * IEEE 802.1X authentication | |
995 | */ | |
996 | if (drv->operstate == 1 && | |
997 | (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP && | |
90a545cc JM |
998 | !(ifi->ifi_flags & IFF_RUNNING)) { |
999 | wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate"); | |
36d84860 | 1000 | netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, |
e2d02c29 | 1001 | -1, IF_OPER_UP); |
3f5285e8 | 1002 | } |
97cfcf64 | 1003 | |
90a545cc JM |
1004 | if (ifname[0]) |
1005 | wpa_driver_nl80211_event_newlink(drv, ifname); | |
1006 | ||
97cfcf64 | 1007 | if (ifi->ifi_family == AF_BRIDGE && brid) { |
392dfd37 JM |
1008 | struct i802_bss *bss; |
1009 | ||
97cfcf64 | 1010 | /* device has been added to bridge */ |
97cfcf64 B |
1011 | if_indextoname(brid, namebuf); |
1012 | wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s", | |
1013 | brid, namebuf); | |
1014 | add_ifidx(drv, brid); | |
392dfd37 JM |
1015 | |
1016 | for (bss = drv->first_bss; bss; bss = bss->next) { | |
1017 | if (os_strcmp(ifname, bss->ifname) == 0) { | |
1018 | os_strlcpy(bss->brname, namebuf, IFNAMSIZ); | |
1019 | break; | |
1020 | } | |
1021 | } | |
97cfcf64 | 1022 | } |
3f5285e8 JM |
1023 | } |
1024 | ||
1025 | ||
62d680c3 JM |
1026 | static void wpa_driver_nl80211_event_rtm_dellink(void *ctx, |
1027 | struct ifinfomsg *ifi, | |
1028 | u8 *buf, size_t len) | |
3f5285e8 | 1029 | { |
36d84860 BG |
1030 | struct nl80211_global *global = ctx; |
1031 | struct wpa_driver_nl80211_data *drv; | |
90a545cc | 1032 | int attrlen; |
62d680c3 | 1033 | struct rtattr *attr; |
97cfcf64 | 1034 | u32 brid = 0; |
90a545cc | 1035 | char ifname[IFNAMSIZ + 1]; |
f2e90835 | 1036 | char extra[100], *pos, *end; |
3f5285e8 | 1037 | |
36d84860 BG |
1038 | drv = nl80211_find_drv(global, ifi->ifi_index, buf, len); |
1039 | if (!drv) { | |
90a545cc JM |
1040 | wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d", |
1041 | ifi->ifi_index); | |
36d84860 BG |
1042 | return; |
1043 | } | |
1044 | ||
f2e90835 JM |
1045 | extra[0] = '\0'; |
1046 | pos = extra; | |
1047 | end = pos + sizeof(extra); | |
90a545cc JM |
1048 | ifname[0] = '\0'; |
1049 | ||
62d680c3 JM |
1050 | attrlen = len; |
1051 | attr = (struct rtattr *) buf; | |
3f5285e8 | 1052 | while (RTA_OK(attr, attrlen)) { |
90a545cc JM |
1053 | switch (attr->rta_type) { |
1054 | case IFLA_IFNAME: | |
1055 | if (RTA_PAYLOAD(attr) >= IFNAMSIZ) | |
1056 | break; | |
1057 | os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr)); | |
1058 | ifname[RTA_PAYLOAD(attr)] = '\0'; | |
1059 | break; | |
1060 | case IFLA_MASTER: | |
97cfcf64 | 1061 | brid = nla_get_u32((struct nlattr *) attr); |
f2e90835 JM |
1062 | pos += os_snprintf(pos, end - pos, " master=%u", brid); |
1063 | break; | |
1064 | case IFLA_OPERSTATE: | |
1065 | pos += os_snprintf(pos, end - pos, " operstate=%u", | |
1066 | nla_get_u32((struct nlattr *) attr)); | |
1067 | break; | |
1068 | case IFLA_LINKMODE: | |
1069 | pos += os_snprintf(pos, end - pos, " linkmode=%u", | |
1070 | nla_get_u32((struct nlattr *) attr)); | |
90a545cc JM |
1071 | break; |
1072 | } | |
3f5285e8 JM |
1073 | attr = RTA_NEXT(attr, attrlen); |
1074 | } | |
f2e90835 JM |
1075 | extra[sizeof(extra) - 1] = '\0'; |
1076 | ||
1077 | wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)", | |
1078 | ifi->ifi_index, ifname, extra, ifi->ifi_family, | |
1079 | ifi->ifi_flags, | |
1080 | (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", | |
1081 | (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", | |
1082 | (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", | |
1083 | (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); | |
97cfcf64 | 1084 | |
728ff2f4 | 1085 | if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid)) |
90a545cc JM |
1086 | wpa_driver_nl80211_event_dellink(drv, ifname); |
1087 | ||
97cfcf64 B |
1088 | if (ifi->ifi_family == AF_BRIDGE && brid) { |
1089 | /* device has been removed from bridge */ | |
1090 | char namebuf[IFNAMSIZ]; | |
1091 | if_indextoname(brid, namebuf); | |
1092 | wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge " | |
1093 | "%s", brid, namebuf); | |
1094 | del_ifidx(drv, brid); | |
1095 | } | |
3f5285e8 JM |
1096 | } |
1097 | ||
1098 | ||
f3407c66 | 1099 | unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv) |
f5a8d422 JM |
1100 | { |
1101 | struct nl_msg *msg; | |
1102 | int ret; | |
1103 | struct nl80211_bss_info_arg arg; | |
1104 | ||
9725b784 | 1105 | msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN); |
f5a8d422 | 1106 | os_memset(&arg, 0, sizeof(arg)); |
f5a8d422 JM |
1107 | arg.drv = drv; |
1108 | ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); | |
f5a8d422 | 1109 | if (ret == 0) { |
c7caac56 JM |
1110 | unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ? |
1111 | arg.ibss_freq : arg.assoc_freq; | |
f5a8d422 | 1112 | wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the " |
c7caac56 JM |
1113 | "associated BSS from scan results: %u MHz", freq); |
1114 | if (freq) | |
1115 | drv->assoc_freq = freq; | |
30158a0d | 1116 | return drv->assoc_freq; |
f5a8d422 JM |
1117 | } |
1118 | wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " | |
1119 | "(%s)", ret, strerror(-ret)); | |
f5a8d422 JM |
1120 | return drv->assoc_freq; |
1121 | } | |
1122 | ||
1123 | ||
60a972a6 JM |
1124 | static int get_link_signal(struct nl_msg *msg, void *arg) |
1125 | { | |
1126 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
1127 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1128 | struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; | |
1129 | static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = { | |
1130 | [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, | |
95783298 | 1131 | [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 }, |
60a972a6 | 1132 | }; |
7ee35bf3 PS |
1133 | struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; |
1134 | static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { | |
1135 | [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, | |
1136 | [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, | |
1137 | [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, | |
1138 | [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG }, | |
1139 | }; | |
1c5c7273 | 1140 | struct wpa_signal_info *sig_change = arg; |
60a972a6 JM |
1141 | |
1142 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
1143 | genlmsg_attrlen(gnlh, 0), NULL); | |
1144 | if (!tb[NL80211_ATTR_STA_INFO] || | |
1145 | nla_parse_nested(sinfo, NL80211_STA_INFO_MAX, | |
1146 | tb[NL80211_ATTR_STA_INFO], policy)) | |
1147 | return NL_SKIP; | |
1148 | if (!sinfo[NL80211_STA_INFO_SIGNAL]) | |
1149 | return NL_SKIP; | |
1150 | ||
7ee35bf3 PS |
1151 | sig_change->current_signal = |
1152 | (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]); | |
1153 | ||
95783298 AO |
1154 | if (sinfo[NL80211_STA_INFO_SIGNAL_AVG]) |
1155 | sig_change->avg_signal = | |
1156 | (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]); | |
1157 | else | |
1158 | sig_change->avg_signal = 0; | |
1159 | ||
7ee35bf3 PS |
1160 | if (sinfo[NL80211_STA_INFO_TX_BITRATE]) { |
1161 | if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX, | |
1162 | sinfo[NL80211_STA_INFO_TX_BITRATE], | |
1163 | rate_policy)) { | |
1164 | sig_change->current_txrate = 0; | |
1165 | } else { | |
1166 | if (rinfo[NL80211_RATE_INFO_BITRATE]) { | |
1167 | sig_change->current_txrate = | |
1168 | nla_get_u16(rinfo[ | |
1169 | NL80211_RATE_INFO_BITRATE]) * 100; | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | ||
60a972a6 JM |
1174 | return NL_SKIP; |
1175 | } | |
1176 | ||
1177 | ||
f3407c66 JM |
1178 | int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv, |
1179 | struct wpa_signal_info *sig) | |
60a972a6 JM |
1180 | { |
1181 | struct nl_msg *msg; | |
1182 | ||
7ee35bf3 PS |
1183 | sig->current_signal = -9999; |
1184 | sig->current_txrate = 0; | |
60a972a6 | 1185 | |
9725b784 | 1186 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) || |
a862e4a3 JM |
1187 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) { |
1188 | nlmsg_free(msg); | |
1189 | return -ENOBUFS; | |
1190 | } | |
60a972a6 JM |
1191 | |
1192 | return send_and_recv_msgs(drv, msg, get_link_signal, sig); | |
60a972a6 JM |
1193 | } |
1194 | ||
1195 | ||
7ee35bf3 PS |
1196 | static int get_link_noise(struct nl_msg *msg, void *arg) |
1197 | { | |
1198 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
1199 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1200 | struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; | |
1201 | static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { | |
1202 | [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, | |
1203 | [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, | |
1204 | }; | |
1c5c7273 | 1205 | struct wpa_signal_info *sig_change = arg; |
7ee35bf3 PS |
1206 | |
1207 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
1208 | genlmsg_attrlen(gnlh, 0), NULL); | |
1209 | ||
1210 | if (!tb[NL80211_ATTR_SURVEY_INFO]) { | |
1211 | wpa_printf(MSG_DEBUG, "nl80211: survey data missing!"); | |
1212 | return NL_SKIP; | |
1213 | } | |
1214 | ||
1215 | if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, | |
1216 | tb[NL80211_ATTR_SURVEY_INFO], | |
1217 | survey_policy)) { | |
1218 | wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested " | |
1219 | "attributes!"); | |
1220 | return NL_SKIP; | |
1221 | } | |
1222 | ||
1223 | if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) | |
1224 | return NL_SKIP; | |
1225 | ||
1226 | if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) != | |
1227 | sig_change->frequency) | |
1228 | return NL_SKIP; | |
1229 | ||
1230 | if (!sinfo[NL80211_SURVEY_INFO_NOISE]) | |
1231 | return NL_SKIP; | |
1232 | ||
1233 | sig_change->current_noise = | |
1234 | (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); | |
1235 | ||
1236 | return NL_SKIP; | |
1237 | } | |
1238 | ||
1239 | ||
f3407c66 JM |
1240 | int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv, |
1241 | struct wpa_signal_info *sig_change) | |
7ee35bf3 PS |
1242 | { |
1243 | struct nl_msg *msg; | |
1244 | ||
1245 | sig_change->current_noise = 9999; | |
1246 | sig_change->frequency = drv->assoc_freq; | |
1247 | ||
9725b784 | 1248 | msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); |
7ee35bf3 | 1249 | return send_and_recv_msgs(drv, msg, get_link_noise, sig_change); |
7ee35bf3 PS |
1250 | } |
1251 | ||
1252 | ||
97865538 | 1253 | static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx, |
5582a5d1 | 1254 | void *handle) |
97865538 | 1255 | { |
a4ae123c | 1256 | struct nl_cb *cb = eloop_ctx; |
34068ac3 | 1257 | int res; |
97865538 | 1258 | |
cc2ada86 | 1259 | wpa_printf(MSG_MSGDUMP, "nl80211: Event message available"); |
97865538 | 1260 | |
34068ac3 | 1261 | res = nl_recvmsgs(handle, cb); |
d3d04831 | 1262 | if (res < 0) { |
34068ac3 JM |
1263 | wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d", |
1264 | __func__, res); | |
1265 | } | |
97865538 JM |
1266 | } |
1267 | ||
1268 | ||
6d158490 LR |
1269 | /** |
1270 | * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain | |
1271 | * @priv: driver_nl80211 private data | |
1272 | * @alpha2_arg: country to which to switch to | |
1273 | * Returns: 0 on success, -1 on failure | |
1274 | * | |
1275 | * This asks nl80211 to set the regulatory domain for given | |
1276 | * country ISO / IEC alpha2. | |
1277 | */ | |
1278 | static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg) | |
1279 | { | |
a2e40bb6 FF |
1280 | struct i802_bss *bss = priv; |
1281 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6d158490 LR |
1282 | char alpha2[3]; |
1283 | struct nl_msg *msg; | |
1284 | ||
1285 | msg = nlmsg_alloc(); | |
1286 | if (!msg) | |
e785c2ba | 1287 | return -ENOMEM; |
6d158490 LR |
1288 | |
1289 | alpha2[0] = alpha2_arg[0]; | |
1290 | alpha2[1] = alpha2_arg[1]; | |
1291 | alpha2[2] = '\0'; | |
1292 | ||
a862e4a3 JM |
1293 | if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) || |
1294 | nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) { | |
1295 | nlmsg_free(msg); | |
1296 | return -EINVAL; | |
1297 | } | |
6d158490 LR |
1298 | if (send_and_recv_msgs(drv, msg, NULL, NULL)) |
1299 | return -EINVAL; | |
1300 | return 0; | |
6d158490 LR |
1301 | } |
1302 | ||
1303 | ||
f0793bf1 JM |
1304 | static int nl80211_get_country(struct nl_msg *msg, void *arg) |
1305 | { | |
1306 | char *alpha2 = arg; | |
1307 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
1308 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1309 | ||
1310 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
1311 | genlmsg_attrlen(gnlh, 0), NULL); | |
1312 | if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) { | |
1313 | wpa_printf(MSG_DEBUG, "nl80211: No country information available"); | |
1314 | return NL_SKIP; | |
1315 | } | |
1316 | os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3); | |
1317 | return NL_SKIP; | |
1318 | } | |
1319 | ||
1320 | ||
1321 | static int wpa_driver_nl80211_get_country(void *priv, char *alpha2) | |
1322 | { | |
1323 | struct i802_bss *bss = priv; | |
1324 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
1325 | struct nl_msg *msg; | |
1326 | int ret; | |
1327 | ||
1328 | msg = nlmsg_alloc(); | |
1329 | if (!msg) | |
1330 | return -ENOMEM; | |
1331 | ||
1332 | nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG); | |
1333 | alpha2[0] = '\0'; | |
1334 | ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2); | |
1335 | if (!alpha2[0]) | |
1336 | ret = -1; | |
1337 | ||
1338 | return ret; | |
1339 | } | |
1340 | ||
1341 | ||
2a7b66f5 BG |
1342 | static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global) |
1343 | { | |
d6c9aab8 JB |
1344 | int ret; |
1345 | ||
2a7b66f5 BG |
1346 | global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); |
1347 | if (global->nl_cb == NULL) { | |
1348 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " | |
1349 | "callbacks"); | |
1350 | return -1; | |
1351 | } | |
1352 | ||
481234cf JM |
1353 | global->nl = nl_create_handle(global->nl_cb, "nl"); |
1354 | if (global->nl == NULL) | |
d6c9aab8 | 1355 | goto err; |
276e2d67 | 1356 | |
481234cf | 1357 | global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211"); |
335d42b1 | 1358 | if (global->nl80211_id < 0) { |
276e2d67 BG |
1359 | wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not " |
1360 | "found"); | |
d6c9aab8 | 1361 | goto err; |
276e2d67 BG |
1362 | } |
1363 | ||
481234cf JM |
1364 | global->nl_event = nl_create_handle(global->nl_cb, "event"); |
1365 | if (global->nl_event == NULL) | |
d6c9aab8 | 1366 | goto err; |
9fff9fdc | 1367 | |
d6c9aab8 | 1368 | ret = nl_get_multicast_id(global, "nl80211", "scan"); |
97865538 | 1369 | if (ret >= 0) |
481234cf | 1370 | ret = nl_socket_add_membership(global->nl_event, ret); |
97865538 JM |
1371 | if (ret < 0) { |
1372 | wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " | |
1373 | "membership for scan events: %d (%s)", | |
1374 | ret, strerror(-ret)); | |
d6c9aab8 | 1375 | goto err; |
97865538 | 1376 | } |
c2a04078 | 1377 | |
d6c9aab8 | 1378 | ret = nl_get_multicast_id(global, "nl80211", "mlme"); |
c2a04078 | 1379 | if (ret >= 0) |
481234cf | 1380 | ret = nl_socket_add_membership(global->nl_event, ret); |
c2a04078 JM |
1381 | if (ret < 0) { |
1382 | wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " | |
1383 | "membership for mlme events: %d (%s)", | |
1384 | ret, strerror(-ret)); | |
d6c9aab8 | 1385 | goto err; |
c2a04078 | 1386 | } |
c2a04078 | 1387 | |
d6c9aab8 | 1388 | ret = nl_get_multicast_id(global, "nl80211", "regulatory"); |
33c5deb8 | 1389 | if (ret >= 0) |
481234cf | 1390 | ret = nl_socket_add_membership(global->nl_event, ret); |
33c5deb8 JM |
1391 | if (ret < 0) { |
1392 | wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast " | |
1393 | "membership for regulatory events: %d (%s)", | |
1394 | ret, strerror(-ret)); | |
1395 | /* Continue without regulatory events */ | |
1396 | } | |
1397 | ||
17b79e65 JM |
1398 | ret = nl_get_multicast_id(global, "nl80211", "vendor"); |
1399 | if (ret >= 0) | |
1400 | ret = nl_socket_add_membership(global->nl_event, ret); | |
1401 | if (ret < 0) { | |
1402 | wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast " | |
1403 | "membership for vendor events: %d (%s)", | |
1404 | ret, strerror(-ret)); | |
1405 | /* Continue without vendor events */ | |
1406 | } | |
1407 | ||
d6c9aab8 JB |
1408 | nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, |
1409 | no_seq_check, NULL); | |
1410 | nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, | |
1411 | process_global_event, global); | |
1412 | ||
5f65e9f7 JB |
1413 | nl80211_register_eloop_read(&global->nl_event, |
1414 | wpa_driver_nl80211_event_receive, | |
1415 | global->nl_cb); | |
d6c9aab8 JB |
1416 | |
1417 | return 0; | |
1418 | ||
1419 | err: | |
1420 | nl_destroy_handles(&global->nl_event); | |
1421 | nl_destroy_handles(&global->nl); | |
1422 | nl_cb_put(global->nl_cb); | |
671a5039 | 1423 | global->nl_cb = NULL; |
d6c9aab8 JB |
1424 | return -1; |
1425 | } | |
1426 | ||
1427 | ||
1428 | static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv) | |
1429 | { | |
1afc986d JB |
1430 | drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); |
1431 | if (!drv->nl_cb) { | |
1432 | wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct"); | |
d6c9aab8 | 1433 | return -1; |
1afc986d JB |
1434 | } |
1435 | ||
1436 | nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, | |
1437 | no_seq_check, NULL); | |
f06aedd9 JB |
1438 | nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, |
1439 | process_drv_event, drv); | |
1afc986d | 1440 | |
9fff9fdc | 1441 | return 0; |
9fff9fdc JM |
1442 | } |
1443 | ||
1444 | ||
8401a6b0 JM |
1445 | static void wpa_driver_nl80211_rfkill_blocked(void *ctx) |
1446 | { | |
8401a6b0 | 1447 | wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked"); |
a63063b4 JM |
1448 | /* |
1449 | * This may be for any interface; use ifdown event to disable | |
1450 | * interface. | |
1451 | */ | |
8401a6b0 JM |
1452 | } |
1453 | ||
1454 | ||
1455 | static void wpa_driver_nl80211_rfkill_unblocked(void *ctx) | |
1456 | { | |
1457 | struct wpa_driver_nl80211_data *drv = ctx; | |
1458 | wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked"); | |
834ee56f | 1459 | if (i802_set_iface_flags(drv->first_bss, 1)) { |
8401a6b0 JM |
1460 | wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP " |
1461 | "after rfkill unblock"); | |
1462 | return; | |
1463 | } | |
a63063b4 | 1464 | /* rtnetlink ifup handler will report interface as enabled */ |
8401a6b0 JM |
1465 | } |
1466 | ||
1467 | ||
32ab4855 JB |
1468 | static void wpa_driver_nl80211_handle_eapol_tx_status(int sock, |
1469 | void *eloop_ctx, | |
1470 | void *handle) | |
1471 | { | |
1472 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
1473 | u8 data[2048]; | |
1474 | struct msghdr msg; | |
1475 | struct iovec entry; | |
cad0f50e | 1476 | u8 control[512]; |
32ab4855 JB |
1477 | struct cmsghdr *cmsg; |
1478 | int res, found_ee = 0, found_wifi = 0, acked = 0; | |
1479 | union wpa_event_data event; | |
1480 | ||
1481 | memset(&msg, 0, sizeof(msg)); | |
1482 | msg.msg_iov = &entry; | |
1483 | msg.msg_iovlen = 1; | |
1484 | entry.iov_base = data; | |
1485 | entry.iov_len = sizeof(data); | |
1486 | msg.msg_control = &control; | |
1487 | msg.msg_controllen = sizeof(control); | |
1488 | ||
1489 | res = recvmsg(sock, &msg, MSG_ERRQUEUE); | |
1490 | /* if error or not fitting 802.3 header, return */ | |
1491 | if (res < 14) | |
1492 | return; | |
1493 | ||
1494 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) | |
1495 | { | |
1496 | if (cmsg->cmsg_level == SOL_SOCKET && | |
1497 | cmsg->cmsg_type == SCM_WIFI_STATUS) { | |
1498 | int *ack; | |
1499 | ||
1500 | found_wifi = 1; | |
1501 | ack = (void *)CMSG_DATA(cmsg); | |
1502 | acked = *ack; | |
1503 | } | |
1504 | ||
1505 | if (cmsg->cmsg_level == SOL_PACKET && | |
1506 | cmsg->cmsg_type == PACKET_TX_TIMESTAMP) { | |
1507 | struct sock_extended_err *err = | |
1508 | (struct sock_extended_err *)CMSG_DATA(cmsg); | |
1509 | ||
1510 | if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS) | |
1511 | found_ee = 1; | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | if (!found_ee || !found_wifi) | |
1516 | return; | |
1517 | ||
1518 | memset(&event, 0, sizeof(event)); | |
1519 | event.eapol_tx_status.dst = data; | |
1520 | event.eapol_tx_status.data = data + 14; | |
1521 | event.eapol_tx_status.data_len = res - 14; | |
1522 | event.eapol_tx_status.ack = acked; | |
1523 | wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event); | |
1524 | } | |
1525 | ||
1526 | ||
cc7a48d1 JB |
1527 | static int nl80211_init_bss(struct i802_bss *bss) |
1528 | { | |
1529 | bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); | |
1530 | if (!bss->nl_cb) | |
1531 | return -1; | |
1532 | ||
1533 | nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, | |
1534 | no_seq_check, NULL); | |
1535 | nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, | |
1536 | process_bss_event, bss); | |
1537 | ||
1538 | return 0; | |
1539 | } | |
1540 | ||
1541 | ||
1542 | static void nl80211_destroy_bss(struct i802_bss *bss) | |
1543 | { | |
1544 | nl_cb_put(bss->nl_cb); | |
1545 | bss->nl_cb = NULL; | |
1546 | } | |
1547 | ||
1548 | ||
0d547d5f JM |
1549 | static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname, |
1550 | void *global_priv, int hostapd, | |
0ecff8d7 JM |
1551 | const u8 *set_addr, |
1552 | const char *driver_params) | |
9fff9fdc | 1553 | { |
9fff9fdc | 1554 | struct wpa_driver_nl80211_data *drv; |
8401a6b0 | 1555 | struct rfkill_config *rcfg; |
a2e40bb6 | 1556 | struct i802_bss *bss; |
9fff9fdc | 1557 | |
a5c696ad JM |
1558 | if (global_priv == NULL) |
1559 | return NULL; | |
9fff9fdc JM |
1560 | drv = os_zalloc(sizeof(*drv)); |
1561 | if (drv == NULL) | |
1562 | return NULL; | |
f2ed8023 | 1563 | drv->global = global_priv; |
9fff9fdc | 1564 | drv->ctx = ctx; |
0d547d5f JM |
1565 | drv->hostapd = !!hostapd; |
1566 | drv->eapol_sock = -1; | |
1567 | drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int); | |
1568 | drv->if_indices = drv->default_if_indices; | |
834ee56f KP |
1569 | |
1570 | drv->first_bss = os_zalloc(sizeof(*drv->first_bss)); | |
1571 | if (!drv->first_bss) { | |
1572 | os_free(drv); | |
1573 | return NULL; | |
1574 | } | |
1575 | bss = drv->first_bss; | |
a2e40bb6 | 1576 | bss->drv = drv; |
a5e1eb20 SE |
1577 | bss->ctx = ctx; |
1578 | ||
a2e40bb6 | 1579 | os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname)); |
9fff9fdc JM |
1580 | drv->monitor_ifidx = -1; |
1581 | drv->monitor_sock = -1; | |
d12dab4c | 1582 | drv->eapol_tx_sock = -1; |
b1f625e0 | 1583 | drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED; |
9fff9fdc | 1584 | |
ba2d0d7d | 1585 | if (wpa_driver_nl80211_init_nl(drv)) { |
bbaf0837 JM |
1586 | os_free(drv); |
1587 | return NULL; | |
1588 | } | |
9fff9fdc | 1589 | |
cc7a48d1 JB |
1590 | if (nl80211_init_bss(bss)) |
1591 | goto failed; | |
1592 | ||
8401a6b0 JM |
1593 | rcfg = os_zalloc(sizeof(*rcfg)); |
1594 | if (rcfg == NULL) | |
1595 | goto failed; | |
1596 | rcfg->ctx = drv; | |
1597 | os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname)); | |
1598 | rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked; | |
1599 | rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked; | |
1600 | drv->rfkill = rfkill_init(rcfg); | |
52169389 | 1601 | if (drv->rfkill == NULL) { |
8401a6b0 | 1602 | wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available"); |
52169389 JM |
1603 | os_free(rcfg); |
1604 | } | |
8401a6b0 | 1605 | |
146fa9b3 JM |
1606 | if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0) |
1607 | drv->start_iface_up = 1; | |
1608 | ||
0ecff8d7 | 1609 | if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params)) |
bbaf0837 | 1610 | goto failed; |
7524cfb1 | 1611 | |
d12dab4c | 1612 | drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0); |
32ab4855 JB |
1613 | if (drv->eapol_tx_sock < 0) |
1614 | goto failed; | |
1615 | ||
1616 | if (drv->data_tx_status) { | |
1617 | int enabled = 1; | |
1618 | ||
1619 | if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS, | |
1620 | &enabled, sizeof(enabled)) < 0) { | |
1621 | wpa_printf(MSG_DEBUG, | |
1622 | "nl80211: wifi status sockopt failed\n"); | |
1623 | drv->data_tx_status = 0; | |
a11241fa JB |
1624 | if (!drv->use_monitor) |
1625 | drv->capa.flags &= | |
1626 | ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS; | |
32ab4855 JB |
1627 | } else { |
1628 | eloop_register_read_sock(drv->eapol_tx_sock, | |
1629 | wpa_driver_nl80211_handle_eapol_tx_status, | |
1630 | drv, NULL); | |
1631 | } | |
1632 | } | |
f10bfc9a | 1633 | |
dac12351 | 1634 | if (drv->global) { |
c4bb8817 | 1635 | dl_list_add(&drv->global->interfaces, &drv->list); |
dac12351 BG |
1636 | drv->in_interface_list = 1; |
1637 | } | |
c4bb8817 | 1638 | |
a2e40bb6 | 1639 | return bss; |
7524cfb1 | 1640 | |
bbaf0837 | 1641 | failed: |
dac12351 | 1642 | wpa_driver_nl80211_deinit(bss); |
7524cfb1 JM |
1643 | return NULL; |
1644 | } | |
1645 | ||
1646 | ||
0d547d5f JM |
1647 | /** |
1648 | * wpa_driver_nl80211_init - Initialize nl80211 driver interface | |
1649 | * @ctx: context to be used when calling wpa_supplicant functions, | |
1650 | * e.g., wpa_supplicant_event() | |
1651 | * @ifname: interface name, e.g., wlan0 | |
1652 | * @global_priv: private driver global data from global_init() | |
1653 | * Returns: Pointer to private data, %NULL on failure | |
1654 | */ | |
1655 | static void * wpa_driver_nl80211_init(void *ctx, const char *ifname, | |
1656 | void *global_priv) | |
1657 | { | |
0ecff8d7 JM |
1658 | return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL, |
1659 | NULL); | |
0d547d5f JM |
1660 | } |
1661 | ||
1662 | ||
a11241fa | 1663 | static int nl80211_register_frame(struct i802_bss *bss, |
5582a5d1 | 1664 | struct nl_handle *nl_handle, |
bd94971e | 1665 | u16 type, const u8 *match, size_t match_len) |
58f6fbe0 | 1666 | { |
a11241fa | 1667 | struct wpa_driver_nl80211_data *drv = bss->drv; |
58f6fbe0 | 1668 | struct nl_msg *msg; |
a862e4a3 | 1669 | int ret; |
880de885 | 1670 | char buf[30]; |
58f6fbe0 | 1671 | |
880de885 JM |
1672 | buf[0] = '\0'; |
1673 | wpa_snprintf_hex(buf, sizeof(buf), match, match_len); | |
dedfa440 PF |
1674 | wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s", |
1675 | type, fc2str(type), nl_handle, buf); | |
36488c05 | 1676 | |
56f77852 | 1677 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) || |
a862e4a3 JM |
1678 | nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) || |
1679 | nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) { | |
1680 | nlmsg_free(msg); | |
1681 | return -1; | |
1682 | } | |
58f6fbe0 | 1683 | |
d6c9aab8 | 1684 | ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL); |
58f6fbe0 | 1685 | if (ret) { |
5582a5d1 JB |
1686 | wpa_printf(MSG_DEBUG, "nl80211: Register frame command " |
1687 | "failed (type=%u): ret=%d (%s)", | |
1688 | type, ret, strerror(-ret)); | |
1689 | wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match", | |
58f6fbe0 | 1690 | match, match_len); |
58f6fbe0 | 1691 | } |
58f6fbe0 JM |
1692 | return ret; |
1693 | } | |
1694 | ||
1695 | ||
a11241fa JB |
1696 | static int nl80211_alloc_mgmt_handle(struct i802_bss *bss) |
1697 | { | |
1698 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
1699 | ||
481234cf | 1700 | if (bss->nl_mgmt) { |
a11241fa | 1701 | wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting " |
36488c05 | 1702 | "already on! (nl_mgmt=%p)", bss->nl_mgmt); |
a11241fa JB |
1703 | return -1; |
1704 | } | |
1705 | ||
481234cf JM |
1706 | bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt"); |
1707 | if (bss->nl_mgmt == NULL) | |
a11241fa JB |
1708 | return -1; |
1709 | ||
a11241fa JB |
1710 | return 0; |
1711 | } | |
1712 | ||
1713 | ||
5f65e9f7 JB |
1714 | static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss) |
1715 | { | |
1716 | nl80211_register_eloop_read(&bss->nl_mgmt, | |
1717 | wpa_driver_nl80211_event_receive, | |
1718 | bss->nl_cb); | |
1719 | } | |
1720 | ||
1721 | ||
a11241fa | 1722 | static int nl80211_register_action_frame(struct i802_bss *bss, |
bd94971e JB |
1723 | const u8 *match, size_t match_len) |
1724 | { | |
1725 | u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4); | |
481234cf | 1726 | return nl80211_register_frame(bss, bss->nl_mgmt, |
5582a5d1 | 1727 | type, match, match_len); |
bd94971e JB |
1728 | } |
1729 | ||
1730 | ||
a11241fa | 1731 | static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss) |
58f6fbe0 | 1732 | { |
a11241fa | 1733 | struct wpa_driver_nl80211_data *drv = bss->drv; |
6f06766e | 1734 | int ret = 0; |
a11241fa JB |
1735 | |
1736 | if (nl80211_alloc_mgmt_handle(bss)) | |
1737 | return -1; | |
36488c05 JM |
1738 | wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP " |
1739 | "handle %p", bss->nl_mgmt); | |
a11241fa | 1740 | |
e8d1168b JB |
1741 | if (drv->nlmode == NL80211_IFTYPE_ADHOC) { |
1742 | u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4); | |
1743 | ||
1744 | /* register for any AUTH message */ | |
1745 | nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0); | |
1746 | } | |
1747 | ||
56f5af48 JM |
1748 | #ifdef CONFIG_INTERWORKING |
1749 | /* QoS Map Configure */ | |
1750 | if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0) | |
6f06766e | 1751 | ret = -1; |
56f5af48 | 1752 | #endif /* CONFIG_INTERWORKING */ |
4fe9fa0d | 1753 | #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING) |
046b26a2 | 1754 | /* GAS Initial Request */ |
a11241fa | 1755 | if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0) |
6f06766e | 1756 | ret = -1; |
046b26a2 | 1757 | /* GAS Initial Response */ |
a11241fa | 1758 | if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0) |
6f06766e | 1759 | ret = -1; |
18708aad | 1760 | /* GAS Comeback Request */ |
a11241fa | 1761 | if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0) |
6f06766e | 1762 | ret = -1; |
18708aad | 1763 | /* GAS Comeback Response */ |
a11241fa | 1764 | if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0) |
6f06766e | 1765 | ret = -1; |
c5a64e2d JM |
1766 | /* Protected GAS Initial Request */ |
1767 | if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0) | |
1768 | ret = -1; | |
1769 | /* Protected GAS Initial Response */ | |
1770 | if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0) | |
1771 | ret = -1; | |
1772 | /* Protected GAS Comeback Request */ | |
1773 | if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0) | |
1774 | ret = -1; | |
1775 | /* Protected GAS Comeback Response */ | |
1776 | if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0) | |
1777 | ret = -1; | |
4fe9fa0d JM |
1778 | #endif /* CONFIG_P2P || CONFIG_INTERWORKING */ |
1779 | #ifdef CONFIG_P2P | |
046b26a2 | 1780 | /* P2P Public Action */ |
a11241fa | 1781 | if (nl80211_register_action_frame(bss, |
046b26a2 JM |
1782 | (u8 *) "\x04\x09\x50\x6f\x9a\x09", |
1783 | 6) < 0) | |
6f06766e | 1784 | ret = -1; |
046b26a2 | 1785 | /* P2P Action */ |
a11241fa | 1786 | if (nl80211_register_action_frame(bss, |
046b26a2 JM |
1787 | (u8 *) "\x7f\x50\x6f\x9a\x09", |
1788 | 5) < 0) | |
6f06766e | 1789 | ret = -1; |
046b26a2 | 1790 | #endif /* CONFIG_P2P */ |
7d878ca7 JM |
1791 | #ifdef CONFIG_IEEE80211W |
1792 | /* SA Query Response */ | |
a11241fa | 1793 | if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0) |
6f06766e | 1794 | ret = -1; |
7d878ca7 | 1795 | #endif /* CONFIG_IEEE80211W */ |
35287637 AN |
1796 | #ifdef CONFIG_TDLS |
1797 | if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) { | |
1798 | /* TDLS Discovery Response */ | |
aa543c0c | 1799 | if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) < |
35287637 | 1800 | 0) |
6f06766e | 1801 | ret = -1; |
35287637 AN |
1802 | } |
1803 | #endif /* CONFIG_TDLS */ | |
046b26a2 | 1804 | |
7b90c16a | 1805 | /* FT Action frames */ |
a11241fa | 1806 | if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0) |
6f06766e | 1807 | ret = -1; |
7b90c16a JM |
1808 | else |
1809 | drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT | | |
1810 | WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; | |
1811 | ||
71269b37 | 1812 | /* WNM - BSS Transition Management Request */ |
a11241fa | 1813 | if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0) |
6f06766e | 1814 | ret = -1; |
bd896433 JM |
1815 | /* WNM-Sleep Mode Response */ |
1816 | if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0) | |
6f06766e | 1817 | ret = -1; |
a11241fa | 1818 | |
95a3ea94 JM |
1819 | #ifdef CONFIG_HS20 |
1820 | /* WNM-Notification */ | |
1821 | if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0) | |
3ee18569 | 1822 | ret = -1; |
95a3ea94 JM |
1823 | #endif /* CONFIG_HS20 */ |
1824 | ||
dfa87878 MB |
1825 | /* WMM-AC ADDTS Response */ |
1826 | if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0) | |
1827 | return -1; | |
1828 | ||
1829 | /* WMM-AC DELTS */ | |
1830 | if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0) | |
1831 | return -1; | |
1832 | ||
2526ccd9 AK |
1833 | /* Radio Measurement - Neighbor Report Response */ |
1834 | if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0) | |
1835 | ret = -1; | |
1836 | ||
7dc03388 AO |
1837 | /* Radio Measurement - Link Measurement Request */ |
1838 | if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) && | |
1839 | (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0)) | |
1840 | ret = -1; | |
1841 | ||
5f65e9f7 JB |
1842 | nl80211_mgmt_handle_register_eloop(bss); |
1843 | ||
6f06766e | 1844 | return ret; |
a11241fa JB |
1845 | } |
1846 | ||
1847 | ||
afb0550a BC |
1848 | static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss) |
1849 | { | |
1850 | int ret = 0; | |
1851 | ||
1852 | if (nl80211_alloc_mgmt_handle(bss)) | |
1853 | return -1; | |
1854 | ||
1855 | wpa_printf(MSG_DEBUG, | |
1856 | "nl80211: Subscribe to mgmt frames with mesh handle %p", | |
1857 | bss->nl_mgmt); | |
1858 | ||
1859 | /* Auth frames for mesh SAE */ | |
1860 | if (nl80211_register_frame(bss, bss->nl_mgmt, | |
1861 | (WLAN_FC_TYPE_MGMT << 2) | | |
1862 | (WLAN_FC_STYPE_AUTH << 4), | |
1863 | NULL, 0) < 0) | |
1864 | ret = -1; | |
1865 | ||
1866 | /* Mesh peering open */ | |
1867 | if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0) | |
1868 | ret = -1; | |
1869 | /* Mesh peering confirm */ | |
1870 | if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0) | |
1871 | ret = -1; | |
1872 | /* Mesh peering close */ | |
1873 | if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0) | |
1874 | ret = -1; | |
1875 | ||
1876 | nl80211_mgmt_handle_register_eloop(bss); | |
1877 | ||
1878 | return ret; | |
1879 | } | |
1880 | ||
1881 | ||
02bb32c3 JB |
1882 | static int nl80211_register_spurious_class3(struct i802_bss *bss) |
1883 | { | |
02bb32c3 | 1884 | struct nl_msg *msg; |
a862e4a3 | 1885 | int ret; |
02bb32c3 | 1886 | |
13f83980 JM |
1887 | msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME); |
1888 | ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL); | |
02bb32c3 JB |
1889 | if (ret) { |
1890 | wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 " | |
1891 | "failed: ret=%d (%s)", | |
1892 | ret, strerror(-ret)); | |
02bb32c3 | 1893 | } |
02bb32c3 JB |
1894 | return ret; |
1895 | } | |
1896 | ||
1897 | ||
a11241fa JB |
1898 | static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss) |
1899 | { | |
1900 | static const int stypes[] = { | |
1901 | WLAN_FC_STYPE_AUTH, | |
1902 | WLAN_FC_STYPE_ASSOC_REQ, | |
1903 | WLAN_FC_STYPE_REASSOC_REQ, | |
1904 | WLAN_FC_STYPE_DISASSOC, | |
1905 | WLAN_FC_STYPE_DEAUTH, | |
1906 | WLAN_FC_STYPE_ACTION, | |
1907 | WLAN_FC_STYPE_PROBE_REQ, | |
1908 | /* Beacon doesn't work as mac80211 doesn't currently allow | |
1909 | * it, but it wouldn't really be the right thing anyway as | |
1910 | * it isn't per interface ... maybe just dump the scan | |
1911 | * results periodically for OLBC? | |
1912 | */ | |
0e80ea2c | 1913 | /* WLAN_FC_STYPE_BEACON, */ |
a11241fa JB |
1914 | }; |
1915 | unsigned int i; | |
1916 | ||
1917 | if (nl80211_alloc_mgmt_handle(bss)) | |
71269b37 | 1918 | return -1; |
36488c05 JM |
1919 | wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " |
1920 | "handle %p", bss->nl_mgmt); | |
71269b37 | 1921 | |
e7ecab4a | 1922 | for (i = 0; i < ARRAY_SIZE(stypes); i++) { |
481234cf | 1923 | if (nl80211_register_frame(bss, bss->nl_mgmt, |
a11241fa JB |
1924 | (WLAN_FC_TYPE_MGMT << 2) | |
1925 | (stypes[i] << 4), | |
1926 | NULL, 0) < 0) { | |
1927 | goto out_err; | |
1928 | } | |
1929 | } | |
1930 | ||
02bb32c3 JB |
1931 | if (nl80211_register_spurious_class3(bss)) |
1932 | goto out_err; | |
1933 | ||
e32ad281 JB |
1934 | if (nl80211_get_wiphy_data_ap(bss) == NULL) |
1935 | goto out_err; | |
1936 | ||
5f65e9f7 | 1937 | nl80211_mgmt_handle_register_eloop(bss); |
58f6fbe0 | 1938 | return 0; |
a11241fa JB |
1939 | |
1940 | out_err: | |
a11241fa JB |
1941 | nl_destroy_handles(&bss->nl_mgmt); |
1942 | return -1; | |
1943 | } | |
1944 | ||
1945 | ||
a6cc0602 JM |
1946 | static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss) |
1947 | { | |
1948 | if (nl80211_alloc_mgmt_handle(bss)) | |
1949 | return -1; | |
1950 | wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP " | |
1951 | "handle %p (device SME)", bss->nl_mgmt); | |
1952 | ||
1953 | if (nl80211_register_frame(bss, bss->nl_mgmt, | |
1954 | (WLAN_FC_TYPE_MGMT << 2) | | |
1955 | (WLAN_FC_STYPE_ACTION << 4), | |
1956 | NULL, 0) < 0) | |
1957 | goto out_err; | |
1958 | ||
5f65e9f7 | 1959 | nl80211_mgmt_handle_register_eloop(bss); |
a6cc0602 JM |
1960 | return 0; |
1961 | ||
1962 | out_err: | |
a6cc0602 JM |
1963 | nl_destroy_handles(&bss->nl_mgmt); |
1964 | return -1; | |
1965 | } | |
1966 | ||
1967 | ||
36488c05 | 1968 | static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason) |
a11241fa | 1969 | { |
481234cf | 1970 | if (bss->nl_mgmt == NULL) |
a11241fa | 1971 | return; |
36488c05 JM |
1972 | wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p " |
1973 | "(%s)", bss->nl_mgmt, reason); | |
5f65e9f7 | 1974 | nl80211_destroy_eloop_handle(&bss->nl_mgmt); |
e32ad281 JB |
1975 | |
1976 | nl80211_put_wiphy_data_ap(bss); | |
58f6fbe0 JM |
1977 | } |
1978 | ||
1979 | ||
8401a6b0 JM |
1980 | static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx) |
1981 | { | |
1982 | wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL); | |
1983 | } | |
1984 | ||
1985 | ||
eb4582f2 AS |
1986 | static void nl80211_del_p2pdev(struct i802_bss *bss) |
1987 | { | |
eb4582f2 AS |
1988 | struct nl_msg *msg; |
1989 | int ret; | |
1990 | ||
a3249fdf JM |
1991 | msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE); |
1992 | ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL); | |
eb4582f2 AS |
1993 | |
1994 | wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s", | |
1995 | bss->ifname, (long long unsigned int) bss->wdev_id, | |
6cb4f11d | 1996 | strerror(-ret)); |
eb4582f2 AS |
1997 | } |
1998 | ||
1999 | ||
eb4582f2 | 2000 | static int nl80211_set_p2pdev(struct i802_bss *bss, int start) |
f632e483 | 2001 | { |
f632e483 | 2002 | struct nl_msg *msg; |
a862e4a3 | 2003 | int ret; |
f632e483 | 2004 | |
a3249fdf JM |
2005 | msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE : |
2006 | NL80211_CMD_STOP_P2P_DEVICE); | |
2007 | ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL); | |
f632e483 | 2008 | |
eb4582f2 AS |
2009 | wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s", |
2010 | start ? "Start" : "Stop", | |
2011 | bss->ifname, (long long unsigned int) bss->wdev_id, | |
6cb4f11d | 2012 | strerror(-ret)); |
f632e483 AS |
2013 | return ret; |
2014 | } | |
f632e483 AS |
2015 | |
2016 | ||
91724d6f AS |
2017 | static int i802_set_iface_flags(struct i802_bss *bss, int up) |
2018 | { | |
2019 | enum nl80211_iftype nlmode; | |
2020 | ||
2021 | nlmode = nl80211_get_ifmode(bss); | |
2022 | if (nlmode != NL80211_IFTYPE_P2P_DEVICE) { | |
2023 | return linux_set_iface_flags(bss->drv->global->ioctl_sock, | |
2024 | bss->ifname, up); | |
2025 | } | |
2026 | ||
2027 | /* P2P Device has start/stop which is equivalent */ | |
2028 | return nl80211_set_p2pdev(bss, up); | |
2029 | } | |
2030 | ||
2031 | ||
362f781e | 2032 | static int |
0d547d5f | 2033 | wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv, |
0ecff8d7 JM |
2034 | const u8 *set_addr, int first, |
2035 | const char *driver_params) | |
7524cfb1 | 2036 | { |
834ee56f | 2037 | struct i802_bss *bss = drv->first_bss; |
8401a6b0 | 2038 | int send_rfkill_event = 0; |
0d547d5f | 2039 | enum nl80211_iftype nlmode; |
a2e40bb6 FF |
2040 | |
2041 | drv->ifindex = if_nametoindex(bss->ifname); | |
f632e483 AS |
2042 | bss->ifindex = drv->ifindex; |
2043 | bss->wdev_id = drv->global->if_add_wdevid; | |
2044 | bss->wdev_id_set = drv->global->if_add_wdevid_set; | |
2045 | ||
60b13c20 IP |
2046 | bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex; |
2047 | bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set; | |
f632e483 AS |
2048 | drv->global->if_add_wdevid_set = 0; |
2049 | ||
bf144cf6 AP |
2050 | if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP) |
2051 | bss->static_ap = 1; | |
2052 | ||
f632e483 AS |
2053 | if (wpa_driver_nl80211_capa(drv)) |
2054 | return -1; | |
a87c9d96 | 2055 | |
0ecff8d7 JM |
2056 | if (driver_params && nl80211_set_param(bss, driver_params) < 0) |
2057 | return -1; | |
2058 | ||
5fbcb45d AS |
2059 | wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s", |
2060 | bss->ifname, drv->phyname); | |
2061 | ||
0d547d5f JM |
2062 | if (set_addr && |
2063 | (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) || | |
2064 | linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, | |
2065 | set_addr))) | |
2066 | return -1; | |
2067 | ||
49b4b205 JM |
2068 | if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP) |
2069 | drv->start_mode_ap = 1; | |
2070 | ||
bf144cf6 | 2071 | if (drv->hostapd || bss->static_ap) |
0d547d5f JM |
2072 | nlmode = NL80211_IFTYPE_AP; |
2073 | else if (bss->if_dynamic) | |
8e12685c | 2074 | nlmode = nl80211_get_ifmode(bss); |
0d547d5f JM |
2075 | else |
2076 | nlmode = NL80211_IFTYPE_STATION; | |
f632e483 | 2077 | |
8e12685c | 2078 | if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) { |
0d547d5f | 2079 | wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode"); |
8e12685c | 2080 | return -1; |
a87c9d96 JM |
2081 | } |
2082 | ||
8c06db70 | 2083 | if (nlmode == NL80211_IFTYPE_P2P_DEVICE) |
597b94f5 | 2084 | nl80211_get_macaddr(bss); |
f632e483 | 2085 | |
8c06db70 MB |
2086 | if (!rfkill_is_blocked(drv->rfkill)) { |
2087 | int ret = i802_set_iface_flags(bss, 1); | |
2088 | if (ret) { | |
8401a6b0 JM |
2089 | wpa_printf(MSG_ERROR, "nl80211: Could not set " |
2090 | "interface '%s' UP", bss->ifname); | |
8c06db70 | 2091 | return ret; |
8401a6b0 | 2092 | } |
8c06db70 MB |
2093 | if (nlmode == NL80211_IFTYPE_P2P_DEVICE) |
2094 | return ret; | |
2095 | } else { | |
2096 | wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable " | |
2097 | "interface '%s' due to rfkill", bss->ifname); | |
2098 | if (nlmode == NL80211_IFTYPE_P2P_DEVICE) | |
2099 | return 0; | |
2100 | drv->if_disabled = 1; | |
2101 | send_rfkill_event = 1; | |
362f781e | 2102 | } |
3f5285e8 | 2103 | |
0d547d5f JM |
2104 | if (!drv->hostapd) |
2105 | netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, | |
2106 | 1, IF_OPER_DORMANT); | |
362f781e | 2107 | |
c81eff1a | 2108 | if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, |
341eebee | 2109 | bss->addr)) |
2136f480 | 2110 | return -1; |
fee354c7 | 2111 | os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN); |
f2ed8023 | 2112 | |
8401a6b0 JM |
2113 | if (send_rfkill_event) { |
2114 | eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill, | |
2115 | drv, drv->ctx); | |
2116 | } | |
2117 | ||
362f781e | 2118 | return 0; |
3f5285e8 JM |
2119 | } |
2120 | ||
2121 | ||
8a27af5c JM |
2122 | static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv) |
2123 | { | |
2124 | struct nl_msg *msg; | |
2125 | ||
08e55ebb JM |
2126 | wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)", |
2127 | drv->ifindex); | |
9725b784 | 2128 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON); |
8a27af5c | 2129 | return send_and_recv_msgs(drv, msg, NULL, NULL); |
8a27af5c | 2130 | } |
8a27af5c JM |
2131 | |
2132 | ||
3f5285e8 | 2133 | /** |
7e5ba1b9 | 2134 | * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface |
9ebce9c5 | 2135 | * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init() |
3f5285e8 JM |
2136 | * |
2137 | * Shut down driver interface and processing of driver events. Free | |
2138 | * private data buffer if one was allocated in wpa_driver_nl80211_init(). | |
2139 | */ | |
9ebce9c5 | 2140 | static void wpa_driver_nl80211_deinit(struct i802_bss *bss) |
3f5285e8 | 2141 | { |
a2e40bb6 | 2142 | struct wpa_driver_nl80211_data *drv = bss->drv; |
3f5285e8 | 2143 | |
3e208481 JM |
2144 | wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d", |
2145 | bss->ifname, drv->disabled_11b_rates); | |
2146 | ||
873d0fcf | 2147 | bss->in_deinit = 1; |
32ab4855 JB |
2148 | if (drv->data_tx_status) |
2149 | eloop_unregister_read_sock(drv->eapol_tx_sock); | |
d12dab4c JB |
2150 | if (drv->eapol_tx_sock >= 0) |
2151 | close(drv->eapol_tx_sock); | |
f10bfc9a | 2152 | |
481234cf | 2153 | if (bss->nl_preq) |
5582a5d1 | 2154 | wpa_driver_nl80211_probe_req_report(bss, 0); |
e17a2477 | 2155 | if (bss->added_if_into_bridge) { |
c81eff1a BG |
2156 | if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, |
2157 | bss->ifname) < 0) | |
94627f6c JM |
2158 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " |
2159 | "interface %s from bridge %s: %s", | |
e17a2477 | 2160 | bss->ifname, bss->brname, strerror(errno)); |
97ed9a06 | 2161 | if (drv->rtnl_sk) |
ca3c6b4d | 2162 | nl80211_handle_destroy(drv->rtnl_sk); |
94627f6c | 2163 | } |
e17a2477 | 2164 | if (bss->added_bridge) { |
5ef8e39f | 2165 | linux_set_iface_flags(drv->global->ioctl_sock, bss->brname, 0); |
c81eff1a | 2166 | if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) |
94627f6c JM |
2167 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " |
2168 | "bridge %s: %s", | |
e17a2477 | 2169 | bss->brname, strerror(errno)); |
94627f6c JM |
2170 | } |
2171 | ||
460456f8 | 2172 | nl80211_remove_monitor_interface(drv); |
8a27af5c | 2173 | |
b1f625e0 | 2174 | if (is_ap_interface(drv->nlmode)) |
8a27af5c | 2175 | wpa_driver_nl80211_del_beacon(drv); |
0915d02c | 2176 | |
bbaf0837 JM |
2177 | if (drv->eapol_sock >= 0) { |
2178 | eloop_unregister_read_sock(drv->eapol_sock); | |
2179 | close(drv->eapol_sock); | |
2180 | } | |
2181 | ||
2182 | if (drv->if_indices != drv->default_if_indices) | |
2183 | os_free(drv->if_indices); | |
3f5285e8 | 2184 | |
b3af99d2 | 2185 | if (drv->disabled_11b_rates) |
4e5cb1a3 JM |
2186 | nl80211_disable_11b_rates(drv, drv->ifindex, 0); |
2187 | ||
36d84860 BG |
2188 | netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0, |
2189 | IF_OPER_UP); | |
e390df05 | 2190 | eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx); |
8401a6b0 | 2191 | rfkill_deinit(drv->rfkill); |
3f5285e8 | 2192 | |
bbaf0837 JM |
2193 | eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); |
2194 | ||
146fa9b3 JM |
2195 | if (!drv->start_iface_up) |
2196 | (void) i802_set_iface_flags(bss, 0); | |
fee354c7 JM |
2197 | |
2198 | if (drv->addr_changed) { | |
93da0498 JM |
2199 | if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, |
2200 | 0) < 0) { | |
2201 | wpa_printf(MSG_DEBUG, | |
2202 | "nl80211: Could not set interface down to restore permanent MAC address"); | |
2203 | } | |
fee354c7 JM |
2204 | if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, |
2205 | drv->perm_addr) < 0) { | |
2206 | wpa_printf(MSG_DEBUG, | |
2207 | "nl80211: Could not restore permanent MAC address"); | |
2208 | } | |
2209 | } | |
2210 | ||
8e12685c | 2211 | if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) { |
49b4b205 JM |
2212 | if (!drv->hostapd || !drv->start_mode_ap) |
2213 | wpa_driver_nl80211_set_mode(bss, | |
2214 | NL80211_IFTYPE_STATION); | |
b378c41f | 2215 | nl80211_mgmt_unsubscribe(bss, "deinit"); |
8e12685c AS |
2216 | } else { |
2217 | nl80211_mgmt_unsubscribe(bss, "deinit"); | |
eb4582f2 | 2218 | nl80211_del_p2pdev(bss); |
8e12685c | 2219 | } |
1afc986d | 2220 | nl_cb_put(drv->nl_cb); |
3f5285e8 | 2221 | |
834ee56f | 2222 | nl80211_destroy_bss(drv->first_bss); |
cc7a48d1 | 2223 | |
3812464c JM |
2224 | os_free(drv->filter_ssids); |
2225 | ||
536fd62d JM |
2226 | os_free(drv->auth_ie); |
2227 | ||
dac12351 | 2228 | if (drv->in_interface_list) |
f2ed8023 JM |
2229 | dl_list_del(&drv->list); |
2230 | ||
8cd6b7bc JB |
2231 | os_free(drv->extended_capa); |
2232 | os_free(drv->extended_capa_mask); | |
834ee56f | 2233 | os_free(drv->first_bss); |
3f5285e8 JM |
2234 | os_free(drv); |
2235 | } | |
2236 | ||
2237 | ||
de4ed4a8 JM |
2238 | static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len) |
2239 | { | |
2240 | switch (alg) { | |
2241 | case WPA_ALG_WEP: | |
2242 | if (key_len == 5) | |
2243 | return WLAN_CIPHER_SUITE_WEP40; | |
2244 | return WLAN_CIPHER_SUITE_WEP104; | |
2245 | case WPA_ALG_TKIP: | |
2246 | return WLAN_CIPHER_SUITE_TKIP; | |
2247 | case WPA_ALG_CCMP: | |
2248 | return WLAN_CIPHER_SUITE_CCMP; | |
2249 | case WPA_ALG_GCMP: | |
2250 | return WLAN_CIPHER_SUITE_GCMP; | |
2251 | case WPA_ALG_CCMP_256: | |
2252 | return WLAN_CIPHER_SUITE_CCMP_256; | |
2253 | case WPA_ALG_GCMP_256: | |
2254 | return WLAN_CIPHER_SUITE_GCMP_256; | |
2255 | case WPA_ALG_IGTK: | |
2256 | return WLAN_CIPHER_SUITE_AES_CMAC; | |
2257 | case WPA_ALG_BIP_GMAC_128: | |
2258 | return WLAN_CIPHER_SUITE_BIP_GMAC_128; | |
2259 | case WPA_ALG_BIP_GMAC_256: | |
2260 | return WLAN_CIPHER_SUITE_BIP_GMAC_256; | |
2261 | case WPA_ALG_BIP_CMAC_256: | |
2262 | return WLAN_CIPHER_SUITE_BIP_CMAC_256; | |
2263 | case WPA_ALG_SMS4: | |
2264 | return WLAN_CIPHER_SUITE_SMS4; | |
2265 | case WPA_ALG_KRK: | |
2266 | return WLAN_CIPHER_SUITE_KRK; | |
2267 | case WPA_ALG_NONE: | |
2268 | case WPA_ALG_PMK: | |
2269 | wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d", | |
2270 | alg); | |
2271 | return 0; | |
2272 | } | |
2273 | ||
2274 | wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d", | |
2275 | alg); | |
2276 | return 0; | |
2277 | } | |
2278 | ||
2279 | ||
2280 | static u32 wpa_cipher_to_cipher_suite(unsigned int cipher) | |
2281 | { | |
2282 | switch (cipher) { | |
2283 | case WPA_CIPHER_CCMP_256: | |
2284 | return WLAN_CIPHER_SUITE_CCMP_256; | |
2285 | case WPA_CIPHER_GCMP_256: | |
2286 | return WLAN_CIPHER_SUITE_GCMP_256; | |
2287 | case WPA_CIPHER_CCMP: | |
2288 | return WLAN_CIPHER_SUITE_CCMP; | |
2289 | case WPA_CIPHER_GCMP: | |
2290 | return WLAN_CIPHER_SUITE_GCMP; | |
2291 | case WPA_CIPHER_TKIP: | |
2292 | return WLAN_CIPHER_SUITE_TKIP; | |
2293 | case WPA_CIPHER_WEP104: | |
2294 | return WLAN_CIPHER_SUITE_WEP104; | |
2295 | case WPA_CIPHER_WEP40: | |
2296 | return WLAN_CIPHER_SUITE_WEP40; | |
ae6f9272 JM |
2297 | case WPA_CIPHER_GTK_NOT_USED: |
2298 | return WLAN_CIPHER_SUITE_NO_GROUP_ADDR; | |
de4ed4a8 JM |
2299 | } |
2300 | ||
2301 | return 0; | |
2302 | } | |
2303 | ||
2304 | ||
2305 | static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[], | |
2306 | int max_suites) | |
2307 | { | |
2308 | int num_suites = 0; | |
2309 | ||
2310 | if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256) | |
2311 | suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256; | |
2312 | if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256) | |
2313 | suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256; | |
2314 | if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP) | |
2315 | suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP; | |
2316 | if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP) | |
2317 | suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP; | |
2318 | if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP) | |
2319 | suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP; | |
2320 | if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104) | |
2321 | suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104; | |
2322 | if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40) | |
2323 | suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40; | |
2324 | ||
2325 | return num_suites; | |
2326 | } | |
2327 | ||
2328 | ||
b41f2684 CL |
2329 | static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv, |
2330 | const u8 *key, size_t key_len) | |
2331 | { | |
2332 | struct nl_msg *msg; | |
a862e4a3 | 2333 | int ret; |
b41f2684 | 2334 | |
15badebd | 2335 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) |
b41f2684 CL |
2336 | return 0; |
2337 | ||
9725b784 | 2338 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) || |
a862e4a3 JM |
2339 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) || |
2340 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, | |
2341 | QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) || | |
2342 | nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) { | |
2343 | nlmsg_free(msg); | |
2344 | return -1; | |
2345 | } | |
b41f2684 | 2346 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
b41f2684 CL |
2347 | if (ret) { |
2348 | wpa_printf(MSG_DEBUG, | |
2349 | "nl80211: Key management set key failed: ret=%d (%s)", | |
2350 | ret, strerror(-ret)); | |
2351 | } | |
2352 | ||
b41f2684 CL |
2353 | return ret; |
2354 | } | |
2355 | ||
2356 | ||
9ebce9c5 | 2357 | static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss, |
71934751 JM |
2358 | enum wpa_alg alg, const u8 *addr, |
2359 | int key_idx, int set_tx, | |
642187d6 JM |
2360 | const u8 *seq, size_t seq_len, |
2361 | const u8 *key, size_t key_len) | |
3f5285e8 | 2362 | { |
a2e40bb6 | 2363 | struct wpa_driver_nl80211_data *drv = bss->drv; |
e472e1b4 | 2364 | int ifindex; |
3f5285e8 | 2365 | struct nl_msg *msg; |
1ad1cdc2 | 2366 | int ret; |
dc01de8a | 2367 | int tdls = 0; |
3f5285e8 | 2368 | |
e472e1b4 AS |
2369 | /* Ignore for P2P Device */ |
2370 | if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) | |
2371 | return 0; | |
2372 | ||
2373 | ifindex = if_nametoindex(ifname); | |
8393e1a0 | 2374 | wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d " |
1ad1cdc2 | 2375 | "set_tx=%d seq_len=%lu key_len=%lu", |
8393e1a0 | 2376 | __func__, ifindex, ifname, alg, addr, key_idx, set_tx, |
3f5285e8 | 2377 | (unsigned long) seq_len, (unsigned long) key_len); |
8c66e185 | 2378 | #ifdef CONFIG_TDLS |
dc01de8a | 2379 | if (key_idx == -1) { |
8c66e185 | 2380 | key_idx = 0; |
dc01de8a JM |
2381 | tdls = 1; |
2382 | } | |
8c66e185 | 2383 | #endif /* CONFIG_TDLS */ |
3f5285e8 | 2384 | |
15badebd CL |
2385 | if (alg == WPA_ALG_PMK && |
2386 | (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) { | |
b41f2684 CL |
2387 | wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key", |
2388 | __func__); | |
2389 | ret = issue_key_mgmt_set_key(drv, key, key_len); | |
2390 | return ret; | |
2391 | } | |
2392 | ||
3f5285e8 | 2393 | if (alg == WPA_ALG_NONE) { |
95376e1a JM |
2394 | msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY); |
2395 | if (!msg) | |
2396 | return -ENOBUFS; | |
3f5285e8 | 2397 | } else { |
95376e1a JM |
2398 | msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY); |
2399 | if (!msg || | |
a862e4a3 JM |
2400 | nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) || |
2401 | nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, | |
2402 | wpa_alg_to_cipher_suite(alg, key_len))) | |
2403 | goto fail; | |
e6ef73f1 | 2404 | wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len); |
3f5285e8 JM |
2405 | } |
2406 | ||
e6ef73f1 | 2407 | if (seq && seq_len) { |
a862e4a3 JM |
2408 | if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq)) |
2409 | goto fail; | |
e6ef73f1 JM |
2410 | wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len); |
2411 | } | |
1ad1cdc2 | 2412 | |
0382097e | 2413 | if (addr && !is_broadcast_ether_addr(addr)) { |
3f5285e8 | 2414 | wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); |
a862e4a3 JM |
2415 | if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) |
2416 | goto fail; | |
89c38e32 JM |
2417 | |
2418 | if (alg != WPA_ALG_WEP && key_idx && !set_tx) { | |
2419 | wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK"); | |
a862e4a3 JM |
2420 | if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, |
2421 | NL80211_KEYTYPE_GROUP)) | |
2422 | goto fail; | |
89c38e32 | 2423 | } |
60ea8187 | 2424 | } else if (addr && is_broadcast_ether_addr(addr)) { |
8970bae8 JB |
2425 | struct nlattr *types; |
2426 | ||
60ea8187 | 2427 | wpa_printf(MSG_DEBUG, " broadcast key"); |
8970bae8 JB |
2428 | |
2429 | types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); | |
a862e4a3 JM |
2430 | if (!types || |
2431 | nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST)) | |
2432 | goto fail; | |
8970bae8 | 2433 | nla_nest_end(msg, types); |
3f5285e8 | 2434 | } |
95376e1a | 2435 | if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx)) |
a862e4a3 | 2436 | goto fail; |
3f5285e8 | 2437 | |
1ad1cdc2 | 2438 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
15664ad0 | 2439 | if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE) |
1ad1cdc2 JM |
2440 | ret = 0; |
2441 | if (ret) | |
2442 | wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)", | |
2443 | ret, strerror(-ret)); | |
3f5285e8 | 2444 | |
1ad1cdc2 JM |
2445 | /* |
2446 | * If we failed or don't need to set the default TX key (below), | |
2447 | * we're done here. | |
2448 | */ | |
dc01de8a | 2449 | if (ret || !set_tx || alg == WPA_ALG_NONE || tdls) |
1ad1cdc2 | 2450 | return ret; |
b1f625e0 | 2451 | if (is_ap_interface(drv->nlmode) && addr && |
0382097e | 2452 | !is_broadcast_ether_addr(addr)) |
de12717a | 2453 | return ret; |
3f5285e8 | 2454 | |
95376e1a JM |
2455 | msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY); |
2456 | if (!msg || | |
a862e4a3 | 2457 | nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) || |
a862e4a3 JM |
2458 | nla_put_flag(msg, alg == WPA_ALG_IGTK ? |
2459 | NL80211_ATTR_KEY_DEFAULT_MGMT : | |
2460 | NL80211_ATTR_KEY_DEFAULT)) | |
2461 | goto fail; | |
60ea8187 | 2462 | if (addr && is_broadcast_ether_addr(addr)) { |
8970bae8 JB |
2463 | struct nlattr *types; |
2464 | ||
2465 | types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); | |
a862e4a3 JM |
2466 | if (!types || |
2467 | nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST)) | |
2468 | goto fail; | |
8970bae8 | 2469 | nla_nest_end(msg, types); |
60ea8187 | 2470 | } else if (addr) { |
8970bae8 JB |
2471 | struct nlattr *types; |
2472 | ||
2473 | types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); | |
a862e4a3 JM |
2474 | if (!types || |
2475 | nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST)) | |
2476 | goto fail; | |
8970bae8 | 2477 | nla_nest_end(msg, types); |
60ea8187 | 2478 | } |
3f5285e8 | 2479 | |
1ad1cdc2 JM |
2480 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
2481 | if (ret == -ENOENT) | |
2482 | ret = 0; | |
2483 | if (ret) | |
2484 | wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; " | |
2485 | "err=%d %s)", ret, strerror(-ret)); | |
2486 | return ret; | |
3f5285e8 | 2487 | |
a862e4a3 | 2488 | fail: |
5883168a | 2489 | nlmsg_free(msg); |
6241fcb1 | 2490 | return -ENOBUFS; |
3f5285e8 JM |
2491 | } |
2492 | ||
2493 | ||
71934751 | 2494 | static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg, |
0194fedb JB |
2495 | int key_idx, int defkey, |
2496 | const u8 *seq, size_t seq_len, | |
2497 | const u8 *key, size_t key_len) | |
2498 | { | |
2499 | struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY); | |
2500 | if (!key_attr) | |
2501 | return -1; | |
2502 | ||
a862e4a3 JM |
2503 | if (defkey && alg == WPA_ALG_IGTK) { |
2504 | if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT)) | |
2505 | return -1; | |
2506 | } else if (defkey) { | |
2507 | if (nla_put_flag(msg, NL80211_KEY_DEFAULT)) | |
2508 | return -1; | |
2509 | } | |
0194fedb | 2510 | |
a862e4a3 JM |
2511 | if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) || |
2512 | nla_put_u32(msg, NL80211_KEY_CIPHER, | |
2513 | wpa_alg_to_cipher_suite(alg, key_len)) || | |
2514 | (seq && seq_len && | |
2515 | nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) || | |
2516 | nla_put(msg, NL80211_KEY_DATA, key_len, key)) | |
2517 | return -1; | |
0194fedb JB |
2518 | |
2519 | nla_nest_end(msg, key_attr); | |
2520 | ||
2521 | return 0; | |
0194fedb JB |
2522 | } |
2523 | ||
c811d5bc | 2524 | |
cfaab580 ZY |
2525 | static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params, |
2526 | struct nl_msg *msg) | |
2527 | { | |
2528 | int i, privacy = 0; | |
2529 | struct nlattr *nl_keys, *nl_key; | |
2530 | ||
2531 | for (i = 0; i < 4; i++) { | |
2532 | if (!params->wep_key[i]) | |
2533 | continue; | |
2534 | privacy = 1; | |
2535 | break; | |
2536 | } | |
ce04af5a JM |
2537 | if (params->wps == WPS_MODE_PRIVACY) |
2538 | privacy = 1; | |
2539 | if (params->pairwise_suite && | |
2540 | params->pairwise_suite != WPA_CIPHER_NONE) | |
2541 | privacy = 1; | |
2542 | ||
cfaab580 ZY |
2543 | if (!privacy) |
2544 | return 0; | |
2545 | ||
a862e4a3 JM |
2546 | if (nla_put_flag(msg, NL80211_ATTR_PRIVACY)) |
2547 | return -ENOBUFS; | |
cfaab580 ZY |
2548 | |
2549 | nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS); | |
2550 | if (!nl_keys) | |
a862e4a3 | 2551 | return -ENOBUFS; |
cfaab580 ZY |
2552 | |
2553 | for (i = 0; i < 4; i++) { | |
2554 | if (!params->wep_key[i]) | |
2555 | continue; | |
2556 | ||
2557 | nl_key = nla_nest_start(msg, i); | |
a862e4a3 JM |
2558 | if (!nl_key || |
2559 | nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i], | |
2560 | params->wep_key[i]) || | |
2561 | nla_put_u32(msg, NL80211_KEY_CIPHER, | |
2562 | params->wep_key_len[i] == 5 ? | |
2563 | WLAN_CIPHER_SUITE_WEP40 : | |
2564 | WLAN_CIPHER_SUITE_WEP104) || | |
2565 | nla_put_u8(msg, NL80211_KEY_IDX, i) || | |
2566 | (i == params->wep_tx_keyidx && | |
2567 | nla_put_flag(msg, NL80211_KEY_DEFAULT))) | |
2568 | return -ENOBUFS; | |
cfaab580 ZY |
2569 | |
2570 | nla_nest_end(msg, nl_key); | |
2571 | } | |
2572 | nla_nest_end(msg, nl_keys); | |
2573 | ||
2574 | return 0; | |
cfaab580 ZY |
2575 | } |
2576 | ||
2577 | ||
477af8f8 JM |
2578 | int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, |
2579 | const u8 *addr, int cmd, u16 reason_code, | |
2580 | int local_state_change) | |
c2a04078 | 2581 | { |
a862e4a3 | 2582 | int ret; |
c2a04078 JM |
2583 | struct nl_msg *msg; |
2584 | ||
9725b784 | 2585 | if (!(msg = nl80211_drv_msg(drv, 0, cmd)) || |
a862e4a3 JM |
2586 | nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) || |
2587 | (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) || | |
2588 | (local_state_change && | |
2589 | nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) { | |
2590 | nlmsg_free(msg); | |
2591 | return -1; | |
2592 | } | |
c2a04078 JM |
2593 | |
2594 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
c2a04078 | 2595 | if (ret) { |
3b7ea880 BG |
2596 | wpa_dbg(drv->ctx, MSG_DEBUG, |
2597 | "nl80211: MLME command failed: reason=%u ret=%d (%s)", | |
2598 | reason_code, ret, strerror(-ret)); | |
c2a04078 | 2599 | } |
c2a04078 JM |
2600 | return ret; |
2601 | } | |
3f5285e8 JM |
2602 | |
2603 | ||
cfaab580 | 2604 | static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv, |
817762d9 | 2605 | int reason_code) |
cfaab580 | 2606 | { |
3f53c006 JJ |
2607 | int ret; |
2608 | ||
817762d9 | 2609 | wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code); |
add9b7a4 | 2610 | nl80211_mark_disconnected(drv); |
817762d9 | 2611 | /* Disconnect command doesn't need BSSID - it uses cached value */ |
3f53c006 JJ |
2612 | ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT, |
2613 | reason_code, 0); | |
2614 | /* | |
2615 | * For locally generated disconnect, supplicant already generates a | |
2616 | * DEAUTH event, so ignore the event from NL80211. | |
2617 | */ | |
2618 | drv->ignore_next_local_disconnect = ret == 0; | |
2619 | ||
2620 | return ret; | |
cfaab580 ZY |
2621 | } |
2622 | ||
2623 | ||
9ebce9c5 JM |
2624 | static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss, |
2625 | const u8 *addr, int reason_code) | |
3f5285e8 | 2626 | { |
a2e40bb6 | 2627 | struct wpa_driver_nl80211_data *drv = bss->drv; |
d6a36f39 | 2628 | int ret; |
9392c9be AS |
2629 | |
2630 | if (drv->nlmode == NL80211_IFTYPE_ADHOC) { | |
2631 | nl80211_mark_disconnected(drv); | |
666e508c | 2632 | return nl80211_leave_ibss(drv, 1); |
9392c9be | 2633 | } |
cfaab580 | 2634 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) |
817762d9 | 2635 | return wpa_driver_nl80211_disconnect(drv, reason_code); |
2e75a2b3 JM |
2636 | wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)", |
2637 | __func__, MAC2STR(addr), reason_code); | |
add9b7a4 | 2638 | nl80211_mark_disconnected(drv); |
d6a36f39 JM |
2639 | ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE, |
2640 | reason_code, 0); | |
2641 | /* | |
2642 | * For locally generated deauthenticate, supplicant already generates a | |
2643 | * DEAUTH event, so ignore the event from NL80211. | |
2644 | */ | |
2645 | drv->ignore_next_local_deauth = ret == 0; | |
2646 | return ret; | |
3f5285e8 JM |
2647 | } |
2648 | ||
2649 | ||
536fd62d JM |
2650 | static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv, |
2651 | struct wpa_driver_auth_params *params) | |
2652 | { | |
2653 | int i; | |
2654 | ||
2655 | drv->auth_freq = params->freq; | |
2656 | drv->auth_alg = params->auth_alg; | |
2657 | drv->auth_wep_tx_keyidx = params->wep_tx_keyidx; | |
2658 | drv->auth_local_state_change = params->local_state_change; | |
2659 | drv->auth_p2p = params->p2p; | |
2660 | ||
2661 | if (params->bssid) | |
2662 | os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN); | |
2663 | else | |
2664 | os_memset(drv->auth_bssid_, 0, ETH_ALEN); | |
2665 | ||
2666 | if (params->ssid) { | |
2667 | os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len); | |
2668 | drv->auth_ssid_len = params->ssid_len; | |
2669 | } else | |
2670 | drv->auth_ssid_len = 0; | |
2671 | ||
2672 | ||
2673 | os_free(drv->auth_ie); | |
2674 | drv->auth_ie = NULL; | |
2675 | drv->auth_ie_len = 0; | |
2676 | if (params->ie) { | |
2677 | drv->auth_ie = os_malloc(params->ie_len); | |
2678 | if (drv->auth_ie) { | |
2679 | os_memcpy(drv->auth_ie, params->ie, params->ie_len); | |
2680 | drv->auth_ie_len = params->ie_len; | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | for (i = 0; i < 4; i++) { | |
2685 | if (params->wep_key[i] && params->wep_key_len[i] && | |
2686 | params->wep_key_len[i] <= 16) { | |
2687 | os_memcpy(drv->auth_wep_key[i], params->wep_key[i], | |
2688 | params->wep_key_len[i]); | |
2689 | drv->auth_wep_key_len[i] = params->wep_key_len[i]; | |
2690 | } else | |
2691 | drv->auth_wep_key_len[i] = 0; | |
2692 | } | |
2693 | } | |
2694 | ||
2695 | ||
4bd71954 JM |
2696 | static void nl80211_unmask_11b_rates(struct i802_bss *bss) |
2697 | { | |
2698 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
2699 | ||
2700 | if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates) | |
2701 | return; | |
2702 | ||
2703 | /* | |
2704 | * Looks like we failed to unmask 11b rates previously. This could | |
2705 | * happen, e.g., if the interface was down at the point in time when a | |
2706 | * P2P group was terminated. | |
2707 | */ | |
2708 | wpa_printf(MSG_DEBUG, | |
2709 | "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them", | |
2710 | bss->ifname); | |
2711 | nl80211_disable_11b_rates(drv, drv->ifindex, 0); | |
2712 | } | |
2713 | ||
2714 | ||
c2a04078 | 2715 | static int wpa_driver_nl80211_authenticate( |
9ebce9c5 | 2716 | struct i802_bss *bss, struct wpa_driver_auth_params *params) |
c2a04078 | 2717 | { |
a2e40bb6 | 2718 | struct wpa_driver_nl80211_data *drv = bss->drv; |
a0b2f99b | 2719 | int ret = -1, i; |
c2a04078 JM |
2720 | struct nl_msg *msg; |
2721 | enum nl80211_auth_type type; | |
2f4f73b1 | 2722 | enum nl80211_iftype nlmode; |
6d6f4bb8 | 2723 | int count = 0; |
536fd62d JM |
2724 | int is_retry; |
2725 | ||
4bd71954 JM |
2726 | nl80211_unmask_11b_rates(bss); |
2727 | ||
536fd62d JM |
2728 | is_retry = drv->retry_auth; |
2729 | drv->retry_auth = 0; | |
e8d70a73 | 2730 | drv->ignore_deauth_event = 0; |
c2a04078 | 2731 | |
add9b7a4 | 2732 | nl80211_mark_disconnected(drv); |
e6b8efeb | 2733 | os_memset(drv->auth_bssid, 0, ETH_ALEN); |
add9b7a4 JM |
2734 | if (params->bssid) |
2735 | os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN); | |
2736 | else | |
2737 | os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN); | |
af473088 | 2738 | /* FIX: IBSS mode */ |
2f4f73b1 EP |
2739 | nlmode = params->p2p ? |
2740 | NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; | |
2741 | if (drv->nlmode != nlmode && | |
9ebce9c5 | 2742 | wpa_driver_nl80211_set_mode(bss, nlmode) < 0) |
4a867032 JM |
2743 | return -1; |
2744 | ||
6d6f4bb8 | 2745 | retry: |
c2a04078 JM |
2746 | wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)", |
2747 | drv->ifindex); | |
a0b2f99b | 2748 | |
9725b784 JM |
2749 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE); |
2750 | if (!msg) | |
2751 | goto fail; | |
0194fedb | 2752 | |
a0b2f99b JM |
2753 | for (i = 0; i < 4; i++) { |
2754 | if (!params->wep_key[i]) | |
2755 | continue; | |
9ebce9c5 | 2756 | wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP, |
2ea2fcc7 | 2757 | NULL, i, |
a0b2f99b JM |
2758 | i == params->wep_tx_keyidx, NULL, 0, |
2759 | params->wep_key[i], | |
2760 | params->wep_key_len[i]); | |
0194fedb JB |
2761 | if (params->wep_tx_keyidx != i) |
2762 | continue; | |
2763 | if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0, | |
9725b784 JM |
2764 | params->wep_key[i], params->wep_key_len[i])) |
2765 | goto fail; | |
a0b2f99b JM |
2766 | } |
2767 | ||
c2a04078 JM |
2768 | if (params->bssid) { |
2769 | wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, | |
2770 | MAC2STR(params->bssid)); | |
a862e4a3 JM |
2771 | if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) |
2772 | goto fail; | |
c2a04078 JM |
2773 | } |
2774 | if (params->freq) { | |
2775 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
a862e4a3 JM |
2776 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq)) |
2777 | goto fail; | |
c2a04078 JM |
2778 | } |
2779 | if (params->ssid) { | |
2780 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
2781 | params->ssid, params->ssid_len); | |
a862e4a3 JM |
2782 | if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, |
2783 | params->ssid)) | |
2784 | goto fail; | |
c2a04078 JM |
2785 | } |
2786 | wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len); | |
a862e4a3 JM |
2787 | if (params->ie && |
2788 | nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie)) | |
2789 | goto fail; | |
569fed90 JM |
2790 | if (params->sae_data) { |
2791 | wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data, | |
2792 | params->sae_data_len); | |
a862e4a3 JM |
2793 | if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len, |
2794 | params->sae_data)) | |
2795 | goto fail; | |
569fed90 | 2796 | } |
abd9fafa | 2797 | if (params->auth_alg & WPA_AUTH_ALG_OPEN) |
c2a04078 | 2798 | type = NL80211_AUTHTYPE_OPEN_SYSTEM; |
abd9fafa | 2799 | else if (params->auth_alg & WPA_AUTH_ALG_SHARED) |
c2a04078 | 2800 | type = NL80211_AUTHTYPE_SHARED_KEY; |
abd9fafa | 2801 | else if (params->auth_alg & WPA_AUTH_ALG_LEAP) |
c2a04078 | 2802 | type = NL80211_AUTHTYPE_NETWORK_EAP; |
abd9fafa | 2803 | else if (params->auth_alg & WPA_AUTH_ALG_FT) |
c2a04078 | 2804 | type = NL80211_AUTHTYPE_FT; |
569fed90 JM |
2805 | else if (params->auth_alg & WPA_AUTH_ALG_SAE) |
2806 | type = NL80211_AUTHTYPE_SAE; | |
c2a04078 | 2807 | else |
a862e4a3 | 2808 | goto fail; |
c2a04078 | 2809 | wpa_printf(MSG_DEBUG, " * Auth Type %d", type); |
a862e4a3 JM |
2810 | if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type)) |
2811 | goto fail; | |
77339912 JM |
2812 | if (params->local_state_change) { |
2813 | wpa_printf(MSG_DEBUG, " * Local state change only"); | |
a862e4a3 JM |
2814 | if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE)) |
2815 | goto fail; | |
77339912 | 2816 | } |
c2a04078 JM |
2817 | |
2818 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2819 | msg = NULL; | |
2820 | if (ret) { | |
3b7ea880 BG |
2821 | wpa_dbg(drv->ctx, MSG_DEBUG, |
2822 | "nl80211: MLME command failed (auth): ret=%d (%s)", | |
2823 | ret, strerror(-ret)); | |
6d6f4bb8 | 2824 | count++; |
77339912 JM |
2825 | if (ret == -EALREADY && count == 1 && params->bssid && |
2826 | !params->local_state_change) { | |
6d6f4bb8 JM |
2827 | /* |
2828 | * mac80211 does not currently accept new | |
2829 | * authentication if we are already authenticated. As a | |
2830 | * workaround, force deauthentication and try again. | |
2831 | */ | |
2832 | wpa_printf(MSG_DEBUG, "nl80211: Retry authentication " | |
2833 | "after forced deauthentication"); | |
e8d70a73 | 2834 | drv->ignore_deauth_event = 1; |
6d6f4bb8 | 2835 | wpa_driver_nl80211_deauthenticate( |
5205c4f9 | 2836 | bss, params->bssid, |
6d6f4bb8 JM |
2837 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
2838 | nlmsg_free(msg); | |
2839 | goto retry; | |
2840 | } | |
536fd62d JM |
2841 | |
2842 | if (ret == -ENOENT && params->freq && !is_retry) { | |
2843 | /* | |
2844 | * cfg80211 has likely expired the BSS entry even | |
2845 | * though it was previously available in our internal | |
2846 | * BSS table. To recover quickly, start a single | |
2847 | * channel scan on the specified channel. | |
2848 | */ | |
2849 | struct wpa_driver_scan_params scan; | |
2850 | int freqs[2]; | |
2851 | ||
2852 | os_memset(&scan, 0, sizeof(scan)); | |
2853 | scan.num_ssids = 1; | |
2854 | if (params->ssid) { | |
2855 | scan.ssids[0].ssid = params->ssid; | |
2856 | scan.ssids[0].ssid_len = params->ssid_len; | |
2857 | } | |
2858 | freqs[0] = params->freq; | |
2859 | freqs[1] = 0; | |
2860 | scan.freqs = freqs; | |
2861 | wpa_printf(MSG_DEBUG, "nl80211: Trigger single " | |
2862 | "channel scan to refresh cfg80211 BSS " | |
2863 | "entry"); | |
2864 | ret = wpa_driver_nl80211_scan(bss, &scan); | |
2865 | if (ret == 0) { | |
2866 | nl80211_copy_auth_params(drv, params); | |
2867 | drv->scan_for_auth = 1; | |
2868 | } | |
8c3ba078 JM |
2869 | } else if (is_retry) { |
2870 | /* | |
2871 | * Need to indicate this with an event since the return | |
2872 | * value from the retry is not delivered to core code. | |
2873 | */ | |
2874 | union wpa_event_data event; | |
2875 | wpa_printf(MSG_DEBUG, "nl80211: Authentication retry " | |
2876 | "failed"); | |
2877 | os_memset(&event, 0, sizeof(event)); | |
2878 | os_memcpy(event.timeout_event.addr, drv->auth_bssid_, | |
2879 | ETH_ALEN); | |
2880 | wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT, | |
2881 | &event); | |
536fd62d | 2882 | } |
a862e4a3 JM |
2883 | } else { |
2884 | wpa_printf(MSG_DEBUG, | |
2885 | "nl80211: Authentication request send successfully"); | |
c2a04078 | 2886 | } |
c2a04078 | 2887 | |
a862e4a3 | 2888 | fail: |
c2a04078 JM |
2889 | nlmsg_free(msg); |
2890 | return ret; | |
2891 | } | |
2892 | ||
2893 | ||
f3407c66 | 2894 | int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv) |
536fd62d JM |
2895 | { |
2896 | struct wpa_driver_auth_params params; | |
834ee56f | 2897 | struct i802_bss *bss = drv->first_bss; |
536fd62d JM |
2898 | int i; |
2899 | ||
2900 | wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again"); | |
2901 | ||
2902 | os_memset(¶ms, 0, sizeof(params)); | |
2903 | params.freq = drv->auth_freq; | |
2904 | params.auth_alg = drv->auth_alg; | |
2905 | params.wep_tx_keyidx = drv->auth_wep_tx_keyidx; | |
2906 | params.local_state_change = drv->auth_local_state_change; | |
2907 | params.p2p = drv->auth_p2p; | |
2908 | ||
2909 | if (!is_zero_ether_addr(drv->auth_bssid_)) | |
2910 | params.bssid = drv->auth_bssid_; | |
2911 | ||
2912 | if (drv->auth_ssid_len) { | |
2913 | params.ssid = drv->auth_ssid; | |
2914 | params.ssid_len = drv->auth_ssid_len; | |
2915 | } | |
2916 | ||
2917 | params.ie = drv->auth_ie; | |
2918 | params.ie_len = drv->auth_ie_len; | |
2919 | ||
2920 | for (i = 0; i < 4; i++) { | |
2921 | if (drv->auth_wep_key_len[i]) { | |
2922 | params.wep_key[i] = drv->auth_wep_key[i]; | |
2923 | params.wep_key_len[i] = drv->auth_wep_key_len[i]; | |
2924 | } | |
2925 | } | |
2926 | ||
2927 | drv->retry_auth = 1; | |
2928 | return wpa_driver_nl80211_authenticate(bss, ¶ms); | |
2929 | } | |
2930 | ||
2931 | ||
a11241fa JB |
2932 | static int wpa_driver_nl80211_send_frame(struct i802_bss *bss, |
2933 | const void *data, size_t len, | |
55231068 JM |
2934 | int encrypt, int noack, |
2935 | unsigned int freq, int no_cck, | |
2936 | int offchanok, unsigned int wait_time) | |
a11241fa JB |
2937 | { |
2938 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
2939 | u64 cookie; | |
41cc50d1 | 2940 | int res; |
a11241fa | 2941 | |
c7caac56 JM |
2942 | if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) { |
2943 | freq = nl80211_get_assoc_freq(drv); | |
2944 | wpa_printf(MSG_DEBUG, | |
2945 | "nl80211: send_frame - Use assoc_freq=%u for IBSS", | |
2946 | freq); | |
2947 | } | |
af964484 JM |
2948 | if (freq == 0) { |
2949 | wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u", | |
2950 | bss->freq); | |
55231068 | 2951 | freq = bss->freq; |
af964484 | 2952 | } |
55231068 | 2953 | |
739faee2 | 2954 | if (drv->use_monitor) { |
981cf85a | 2955 | wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor", |
739faee2 | 2956 | freq, bss->freq); |
981cf85a | 2957 | return nl80211_send_monitor(drv, data, len, encrypt, noack); |
739faee2 | 2958 | } |
a11241fa | 2959 | |
739faee2 | 2960 | wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd"); |
41cc50d1 JM |
2961 | res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len, |
2962 | &cookie, no_cck, noack, offchanok); | |
2963 | if (res == 0 && !noack) { | |
2964 | const struct ieee80211_mgmt *mgmt; | |
2965 | u16 fc; | |
2966 | ||
2967 | mgmt = (const struct ieee80211_mgmt *) data; | |
2968 | fc = le_to_host16(mgmt->frame_control); | |
2969 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && | |
2970 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) { | |
2971 | wpa_printf(MSG_MSGDUMP, | |
2972 | "nl80211: Update send_action_cookie from 0x%llx to 0x%llx", | |
2973 | (long long unsigned int) | |
2974 | drv->send_action_cookie, | |
2975 | (long long unsigned int) cookie); | |
2976 | drv->send_action_cookie = cookie; | |
2977 | } | |
2978 | } | |
2979 | ||
2980 | return res; | |
a11241fa JB |
2981 | } |
2982 | ||
2983 | ||
9ebce9c5 JM |
2984 | static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data, |
2985 | size_t data_len, int noack, | |
2986 | unsigned int freq, int no_cck, | |
2987 | int offchanok, | |
2988 | unsigned int wait_time) | |
2c2010ac | 2989 | { |
a2e40bb6 | 2990 | struct wpa_driver_nl80211_data *drv = bss->drv; |
2c2010ac | 2991 | struct ieee80211_mgmt *mgmt; |
7a47d567 | 2992 | int encrypt = 1; |
2c2010ac JM |
2993 | u16 fc; |
2994 | ||
2995 | mgmt = (struct ieee80211_mgmt *) data; | |
2996 | fc = le_to_host16(mgmt->frame_control); | |
f95a4524 PF |
2997 | wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR |
2998 | " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d", | |
2999 | MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time, | |
3000 | fc, fc2str(fc), drv->nlmode); | |
2c2010ac | 3001 | |
8e12685c AS |
3002 | if ((is_sta_interface(drv->nlmode) || |
3003 | drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) && | |
5582a5d1 JB |
3004 | WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && |
3005 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) { | |
3006 | /* | |
3007 | * The use of last_mgmt_freq is a bit of a hack, | |
3008 | * but it works due to the single-threaded nature | |
3009 | * of wpa_supplicant. | |
3010 | */ | |
af964484 JM |
3011 | if (freq == 0) { |
3012 | wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d", | |
3013 | drv->last_mgmt_freq); | |
55231068 | 3014 | freq = drv->last_mgmt_freq; |
af964484 | 3015 | } |
55231068 | 3016 | return nl80211_send_frame_cmd(bss, freq, 0, |
88df0ef7 JB |
3017 | data, data_len, NULL, 1, noack, |
3018 | 1); | |
5582a5d1 JB |
3019 | } |
3020 | ||
61cbe2ff | 3021 | if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) { |
af964484 JM |
3022 | if (freq == 0) { |
3023 | wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d", | |
3024 | bss->freq); | |
55231068 | 3025 | freq = bss->freq; |
af964484 | 3026 | } |
b5671498 JM |
3027 | return nl80211_send_frame_cmd(bss, freq, |
3028 | (int) freq == bss->freq ? 0 : | |
3029 | wait_time, | |
d8d6b32e DG |
3030 | data, data_len, |
3031 | &drv->send_action_cookie, | |
55231068 | 3032 | no_cck, noack, offchanok); |
86957e62 JM |
3033 | } |
3034 | ||
2c2010ac JM |
3035 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && |
3036 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) { | |
3037 | /* | |
3038 | * Only one of the authentication frame types is encrypted. | |
3039 | * In order for static WEP encryption to work properly (i.e., | |
3040 | * to not encrypt the frame), we need to tell mac80211 about | |
3041 | * the frames that must not be encrypted. | |
3042 | */ | |
3043 | u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg); | |
3044 | u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction); | |
7a47d567 JB |
3045 | if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3) |
3046 | encrypt = 0; | |
2c2010ac JM |
3047 | } |
3048 | ||
739faee2 | 3049 | wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame"); |
a11241fa | 3050 | return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, |
55231068 JM |
3051 | noack, freq, no_cck, offchanok, |
3052 | wait_time); | |
3053 | } | |
3054 | ||
3055 | ||
31357268 | 3056 | static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble, |
e5693c47 JM |
3057 | int slot, int ht_opmode, int ap_isolate, |
3058 | int *basic_rates) | |
31357268 JM |
3059 | { |
3060 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3061 | struct nl_msg *msg; | |
3062 | ||
13f83980 | 3063 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) || |
a862e4a3 JM |
3064 | (cts >= 0 && |
3065 | nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) || | |
3066 | (preamble >= 0 && | |
3067 | nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) || | |
3068 | (slot >= 0 && | |
3069 | nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) || | |
3070 | (ht_opmode >= 0 && | |
3071 | nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) || | |
3072 | (ap_isolate >= 0 && | |
3073 | nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate))) | |
3074 | goto fail; | |
e5693c47 JM |
3075 | |
3076 | if (basic_rates) { | |
3077 | u8 rates[NL80211_MAX_SUPP_RATES]; | |
3078 | u8 rates_len = 0; | |
3079 | int i; | |
3080 | ||
3081 | for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; | |
3082 | i++) | |
3083 | rates[rates_len++] = basic_rates[i] / 5; | |
3084 | ||
a862e4a3 JM |
3085 | if (nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, |
3086 | rates)) | |
3087 | goto fail; | |
e5693c47 JM |
3088 | } |
3089 | ||
31357268 | 3090 | return send_and_recv_msgs(drv, msg, NULL, NULL); |
a862e4a3 | 3091 | fail: |
5883168a | 3092 | nlmsg_free(msg); |
31357268 JM |
3093 | return -ENOBUFS; |
3094 | } | |
3095 | ||
3096 | ||
3c4ca363 VN |
3097 | static int wpa_driver_nl80211_set_acl(void *priv, |
3098 | struct hostapd_acl_params *params) | |
3099 | { | |
3100 | struct i802_bss *bss = priv; | |
3101 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3102 | struct nl_msg *msg; | |
3103 | struct nlattr *acl; | |
3104 | unsigned int i; | |
a862e4a3 | 3105 | int ret; |
3c4ca363 VN |
3106 | |
3107 | if (!(drv->capa.max_acl_mac_addrs)) | |
3108 | return -ENOTSUP; | |
3109 | ||
3110 | if (params->num_mac_acl > drv->capa.max_acl_mac_addrs) | |
3111 | return -ENOTSUP; | |
3112 | ||
3c4ca363 VN |
3113 | wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)", |
3114 | params->acl_policy ? "Accept" : "Deny", params->num_mac_acl); | |
3115 | ||
9725b784 | 3116 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) || |
a862e4a3 JM |
3117 | nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ? |
3118 | NL80211_ACL_POLICY_DENY_UNLESS_LISTED : | |
3119 | NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) || | |
3120 | (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) { | |
3121 | nlmsg_free(msg); | |
3122 | return -ENOMEM; | |
3123 | } | |
3c4ca363 | 3124 | |
a862e4a3 JM |
3125 | for (i = 0; i < params->num_mac_acl; i++) { |
3126 | if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) { | |
3127 | nlmsg_free(msg); | |
3128 | return -ENOMEM; | |
3129 | } | |
3130 | } | |
3c4ca363 VN |
3131 | |
3132 | nla_nest_end(msg, acl); | |
3133 | ||
3134 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
3c4ca363 VN |
3135 | if (ret) { |
3136 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)", | |
3137 | ret, strerror(-ret)); | |
3138 | } | |
3139 | ||
3c4ca363 VN |
3140 | return ret; |
3141 | } | |
3142 | ||
3143 | ||
19c3b566 JM |
3144 | static int wpa_driver_nl80211_set_ap(void *priv, |
3145 | struct wpa_driver_ap_params *params) | |
d2440ba0 | 3146 | { |
a2e40bb6 FF |
3147 | struct i802_bss *bss = priv; |
3148 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
d2440ba0 JM |
3149 | struct nl_msg *msg; |
3150 | u8 cmd = NL80211_CMD_NEW_BEACON; | |
3151 | int ret; | |
b4fd6fab | 3152 | int beacon_set; |
b11d1d64 | 3153 | int num_suites; |
da1080d7 | 3154 | int smps_mode; |
de4ed4a8 | 3155 | u32 suites[10], suite; |
b11d1d64 | 3156 | u32 ver; |
b4fd6fab | 3157 | |
b4fd6fab | 3158 | beacon_set = bss->beacon_set; |
d2440ba0 | 3159 | |
d2440ba0 | 3160 | wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)", |
b4fd6fab JM |
3161 | beacon_set); |
3162 | if (beacon_set) | |
d2440ba0 JM |
3163 | cmd = NL80211_CMD_SET_BEACON; |
3164 | ||
b92e08fc JM |
3165 | wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head", |
3166 | params->head, params->head_len); | |
b92e08fc JM |
3167 | wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail", |
3168 | params->tail, params->tail_len); | |
13f83980 | 3169 | wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex); |
b92e08fc | 3170 | wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int); |
b92e08fc | 3171 | wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period); |
b92e08fc JM |
3172 | wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid", |
3173 | params->ssid, params->ssid_len); | |
13f83980 | 3174 | if (!(msg = nl80211_bss_msg(bss, 0, cmd)) || |
a862e4a3 JM |
3175 | nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, |
3176 | params->head) || | |
3177 | nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, | |
3178 | params->tail) || | |
a862e4a3 JM |
3179 | nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL, |
3180 | params->beacon_int) || | |
3181 | nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) || | |
3182 | nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) | |
3183 | goto fail; | |
b92e08fc JM |
3184 | if (params->proberesp && params->proberesp_len) { |
3185 | wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)", | |
3186 | params->proberesp, params->proberesp_len); | |
a862e4a3 JM |
3187 | if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len, |
3188 | params->proberesp)) | |
3189 | goto fail; | |
b92e08fc | 3190 | } |
97a7a0b5 JM |
3191 | switch (params->hide_ssid) { |
3192 | case NO_SSID_HIDING: | |
b92e08fc | 3193 | wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use"); |
a862e4a3 JM |
3194 | if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID, |
3195 | NL80211_HIDDEN_SSID_NOT_IN_USE)) | |
3196 | goto fail; | |
97a7a0b5 JM |
3197 | break; |
3198 | case HIDDEN_SSID_ZERO_LEN: | |
b92e08fc | 3199 | wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len"); |
a862e4a3 JM |
3200 | if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID, |
3201 | NL80211_HIDDEN_SSID_ZERO_LEN)) | |
3202 | goto fail; | |
97a7a0b5 JM |
3203 | break; |
3204 | case HIDDEN_SSID_ZERO_CONTENTS: | |
b92e08fc | 3205 | wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents"); |
a862e4a3 JM |
3206 | if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID, |
3207 | NL80211_HIDDEN_SSID_ZERO_CONTENTS)) | |
3208 | goto fail; | |
97a7a0b5 JM |
3209 | break; |
3210 | } | |
b92e08fc | 3211 | wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy); |
a862e4a3 JM |
3212 | if (params->privacy && |
3213 | nla_put_flag(msg, NL80211_ATTR_PRIVACY)) | |
3214 | goto fail; | |
b92e08fc | 3215 | wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs); |
b11d1d64 JM |
3216 | if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) == |
3217 | (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) { | |
3218 | /* Leave out the attribute */ | |
a862e4a3 JM |
3219 | } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) { |
3220 | if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, | |
3221 | NL80211_AUTHTYPE_SHARED_KEY)) | |
3222 | goto fail; | |
3223 | } else { | |
3224 | if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, | |
3225 | NL80211_AUTHTYPE_OPEN_SYSTEM)) | |
3226 | goto fail; | |
3227 | } | |
b11d1d64 | 3228 | |
b92e08fc | 3229 | wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version); |
b11d1d64 JM |
3230 | ver = 0; |
3231 | if (params->wpa_version & WPA_PROTO_WPA) | |
3232 | ver |= NL80211_WPA_VERSION_1; | |
3233 | if (params->wpa_version & WPA_PROTO_RSN) | |
3234 | ver |= NL80211_WPA_VERSION_2; | |
a862e4a3 JM |
3235 | if (ver && |
3236 | nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver)) | |
3237 | goto fail; | |
b11d1d64 | 3238 | |
b92e08fc JM |
3239 | wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x", |
3240 | params->key_mgmt_suites); | |
b11d1d64 JM |
3241 | num_suites = 0; |
3242 | if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X) | |
3243 | suites[num_suites++] = WLAN_AKM_SUITE_8021X; | |
3244 | if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK) | |
3245 | suites[num_suites++] = WLAN_AKM_SUITE_PSK; | |
a862e4a3 JM |
3246 | if (num_suites && |
3247 | nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32), | |
3248 | suites)) | |
3249 | goto fail; | |
b11d1d64 | 3250 | |
533fe09b | 3251 | if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA && |
a862e4a3 JM |
3252 | params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) && |
3253 | nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)) | |
3254 | goto fail; | |
9f12614b | 3255 | |
b92e08fc JM |
3256 | wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x", |
3257 | params->pairwise_ciphers); | |
de4ed4a8 JM |
3258 | num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers, |
3259 | suites, ARRAY_SIZE(suites)); | |
a862e4a3 JM |
3260 | if (num_suites && |
3261 | nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, | |
3262 | num_suites * sizeof(u32), suites)) | |
3263 | goto fail; | |
b11d1d64 | 3264 | |
b92e08fc JM |
3265 | wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x", |
3266 | params->group_cipher); | |
de4ed4a8 | 3267 | suite = wpa_cipher_to_cipher_suite(params->group_cipher); |
a862e4a3 JM |
3268 | if (suite && |
3269 | nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite)) | |
3270 | goto fail; | |
d2440ba0 | 3271 | |
da1080d7 EP |
3272 | switch (params->smps_mode) { |
3273 | case HT_CAP_INFO_SMPS_DYNAMIC: | |
3274 | wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic"); | |
3275 | smps_mode = NL80211_SMPS_DYNAMIC; | |
3276 | break; | |
3277 | case HT_CAP_INFO_SMPS_STATIC: | |
3278 | wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static"); | |
3279 | smps_mode = NL80211_SMPS_STATIC; | |
3280 | break; | |
3281 | default: | |
3282 | /* invalid - fallback to smps off */ | |
3283 | case HT_CAP_INFO_SMPS_DISABLED: | |
3284 | wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off"); | |
3285 | smps_mode = NL80211_SMPS_OFF; | |
3286 | break; | |
3287 | } | |
a862e4a3 JM |
3288 | if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode)) |
3289 | goto fail; | |
da1080d7 | 3290 | |
fb91db56 | 3291 | if (params->beacon_ies) { |
b92e08fc JM |
3292 | wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies", |
3293 | params->beacon_ies); | |
a862e4a3 JM |
3294 | if (nla_put(msg, NL80211_ATTR_IE, |
3295 | wpabuf_len(params->beacon_ies), | |
3296 | wpabuf_head(params->beacon_ies))) | |
3297 | goto fail; | |
fb91db56 JM |
3298 | } |
3299 | if (params->proberesp_ies) { | |
b92e08fc JM |
3300 | wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies", |
3301 | params->proberesp_ies); | |
a862e4a3 JM |
3302 | if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP, |
3303 | wpabuf_len(params->proberesp_ies), | |
3304 | wpabuf_head(params->proberesp_ies))) | |
3305 | goto fail; | |
fb91db56 JM |
3306 | } |
3307 | if (params->assocresp_ies) { | |
b92e08fc JM |
3308 | wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies", |
3309 | params->assocresp_ies); | |
a862e4a3 JM |
3310 | if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP, |
3311 | wpabuf_len(params->assocresp_ies), | |
3312 | wpabuf_head(params->assocresp_ies))) | |
3313 | goto fail; | |
fb91db56 JM |
3314 | } |
3315 | ||
a0133ee1 | 3316 | if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) { |
b92e08fc JM |
3317 | wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d", |
3318 | params->ap_max_inactivity); | |
a862e4a3 JM |
3319 | if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT, |
3320 | params->ap_max_inactivity)) | |
3321 | goto fail; | |
a0133ee1 VT |
3322 | } |
3323 | ||
d2440ba0 JM |
3324 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
3325 | if (ret) { | |
3326 | wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)", | |
3327 | ret, strerror(-ret)); | |
b4fd6fab | 3328 | } else { |
b4fd6fab | 3329 | bss->beacon_set = 1; |
31357268 | 3330 | nl80211_set_bss(bss, params->cts_protect, params->preamble, |
d03e8d11 | 3331 | params->short_slot_time, params->ht_opmode, |
e5693c47 | 3332 | params->isolate, params->basic_rates); |
e87ef751 PX |
3333 | if (beacon_set && params->freq && |
3334 | params->freq->bandwidth != bss->bandwidth) { | |
3335 | wpa_printf(MSG_DEBUG, | |
3336 | "nl80211: Update BSS %s bandwidth: %d -> %d", | |
3337 | bss->ifname, bss->bandwidth, | |
3338 | params->freq->bandwidth); | |
3339 | ret = nl80211_set_channel(bss, params->freq, 1); | |
3340 | if (ret) { | |
3341 | wpa_printf(MSG_DEBUG, | |
3342 | "nl80211: Frequency set failed: %d (%s)", | |
3343 | ret, strerror(-ret)); | |
3344 | } else { | |
3345 | wpa_printf(MSG_DEBUG, | |
3346 | "nl80211: Frequency set succeeded for ht2040 coex"); | |
3347 | bss->bandwidth = params->freq->bandwidth; | |
3348 | } | |
3349 | } else if (!beacon_set) { | |
3350 | /* | |
3351 | * cfg80211 updates the driver on frequence change in AP | |
3352 | * mode only at the point when beaconing is started, so | |
3353 | * set the initial value here. | |
3354 | */ | |
3355 | bss->bandwidth = params->freq->bandwidth; | |
3356 | } | |
b4fd6fab | 3357 | } |
d2440ba0 | 3358 | return ret; |
a862e4a3 | 3359 | fail: |
5883168a | 3360 | nlmsg_free(msg); |
d2440ba0 JM |
3361 | return -ENOBUFS; |
3362 | } | |
3363 | ||
3364 | ||
1c4ffa87 AO |
3365 | static int nl80211_put_freq_params(struct nl_msg *msg, |
3366 | struct hostapd_freq_params *freq) | |
1581b38b | 3367 | { |
a862e4a3 JM |
3368 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq)) |
3369 | return -ENOBUFS; | |
3370 | ||
89b800d7 | 3371 | if (freq->vht_enabled) { |
a862e4a3 JM |
3372 | enum nl80211_chan_width cw; |
3373 | ||
89b800d7 JB |
3374 | switch (freq->bandwidth) { |
3375 | case 20: | |
a862e4a3 | 3376 | cw = NL80211_CHAN_WIDTH_20; |
89b800d7 JB |
3377 | break; |
3378 | case 40: | |
a862e4a3 | 3379 | cw = NL80211_CHAN_WIDTH_40; |
89b800d7 JB |
3380 | break; |
3381 | case 80: | |
3382 | if (freq->center_freq2) | |
a862e4a3 | 3383 | cw = NL80211_CHAN_WIDTH_80P80; |
89b800d7 | 3384 | else |
a862e4a3 | 3385 | cw = NL80211_CHAN_WIDTH_80; |
89b800d7 JB |
3386 | break; |
3387 | case 160: | |
a862e4a3 | 3388 | cw = NL80211_CHAN_WIDTH_160; |
89b800d7 JB |
3389 | break; |
3390 | default: | |
1c4ffa87 | 3391 | return -EINVAL; |
89b800d7 | 3392 | } |
a862e4a3 JM |
3393 | |
3394 | if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) || | |
3395 | nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, | |
3396 | freq->center_freq1) || | |
3397 | (freq->center_freq2 && | |
3398 | nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, | |
3399 | freq->center_freq2))) | |
3400 | return -ENOBUFS; | |
89b800d7 | 3401 | } else if (freq->ht_enabled) { |
a862e4a3 JM |
3402 | enum nl80211_channel_type ct; |
3403 | ||
89b800d7 | 3404 | switch (freq->sec_channel_offset) { |
f019981a | 3405 | case -1: |
a862e4a3 | 3406 | ct = NL80211_CHAN_HT40MINUS; |
f019981a JM |
3407 | break; |
3408 | case 1: | |
a862e4a3 | 3409 | ct = NL80211_CHAN_HT40PLUS; |
f019981a JM |
3410 | break; |
3411 | default: | |
a862e4a3 | 3412 | ct = NL80211_CHAN_HT20; |
f019981a JM |
3413 | break; |
3414 | } | |
a862e4a3 JM |
3415 | |
3416 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct)) | |
3417 | return -ENOBUFS; | |
f019981a | 3418 | } |
1c4ffa87 | 3419 | return 0; |
1c4ffa87 AO |
3420 | } |
3421 | ||
3422 | ||
e87ef751 PX |
3423 | static int nl80211_set_channel(struct i802_bss *bss, |
3424 | struct hostapd_freq_params *freq, int set_chan) | |
1c4ffa87 AO |
3425 | { |
3426 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3427 | struct nl_msg *msg; | |
3428 | int ret; | |
3429 | ||
3430 | wpa_printf(MSG_DEBUG, | |
3431 | "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)", | |
3432 | freq->freq, freq->ht_enabled, freq->vht_enabled, | |
3433 | freq->bandwidth, freq->center_freq1, freq->center_freq2); | |
1c4ffa87 | 3434 | |
9725b784 JM |
3435 | msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL : |
3436 | NL80211_CMD_SET_WIPHY); | |
3437 | if (!msg || nl80211_put_freq_params(msg, freq) < 0) { | |
a862e4a3 JM |
3438 | nlmsg_free(msg); |
3439 | return -1; | |
3440 | } | |
1581b38b | 3441 | |
d2440ba0 | 3442 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
e4fb2167 | 3443 | if (ret == 0) { |
89b800d7 | 3444 | bss->freq = freq->freq; |
1581b38b | 3445 | return 0; |
e4fb2167 | 3446 | } |
f019981a | 3447 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): " |
89b800d7 | 3448 | "%d (%s)", freq->freq, ret, strerror(-ret)); |
1581b38b JM |
3449 | return -1; |
3450 | } | |
3451 | ||
0f4e8b4f | 3452 | |
95ab6063 AN |
3453 | static u32 sta_flags_nl80211(int flags) |
3454 | { | |
3455 | u32 f = 0; | |
3456 | ||
3457 | if (flags & WPA_STA_AUTHORIZED) | |
3458 | f |= BIT(NL80211_STA_FLAG_AUTHORIZED); | |
3459 | if (flags & WPA_STA_WMM) | |
3460 | f |= BIT(NL80211_STA_FLAG_WME); | |
3461 | if (flags & WPA_STA_SHORT_PREAMBLE) | |
3462 | f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); | |
3463 | if (flags & WPA_STA_MFP) | |
3464 | f |= BIT(NL80211_STA_FLAG_MFP); | |
45b722f1 AN |
3465 | if (flags & WPA_STA_TDLS_PEER) |
3466 | f |= BIT(NL80211_STA_FLAG_TDLS_PEER); | |
7a228b5c BC |
3467 | if (flags & WPA_STA_AUTHENTICATED) |
3468 | f |= BIT(NL80211_STA_FLAG_AUTHENTICATED); | |
95ab6063 AN |
3469 | |
3470 | return f; | |
3471 | } | |
3472 | ||
3473 | ||
7c7e7877 BC |
3474 | #ifdef CONFIG_MESH |
3475 | static u32 sta_plink_state_nl80211(enum mesh_plink_state state) | |
3476 | { | |
3477 | switch (state) { | |
3478 | case PLINK_LISTEN: | |
3479 | return NL80211_PLINK_LISTEN; | |
3480 | case PLINK_OPEN_SENT: | |
3481 | return NL80211_PLINK_OPN_SNT; | |
3482 | case PLINK_OPEN_RCVD: | |
3483 | return NL80211_PLINK_OPN_RCVD; | |
3484 | case PLINK_CNF_RCVD: | |
3485 | return NL80211_PLINK_CNF_RCVD; | |
3486 | case PLINK_ESTAB: | |
3487 | return NL80211_PLINK_ESTAB; | |
3488 | case PLINK_HOLDING: | |
3489 | return NL80211_PLINK_HOLDING; | |
3490 | case PLINK_BLOCKED: | |
3491 | return NL80211_PLINK_BLOCKED; | |
3492 | default: | |
3493 | wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d", | |
3494 | state); | |
3495 | } | |
3496 | return -1; | |
3497 | } | |
3498 | #endif /* CONFIG_MESH */ | |
3499 | ||
3500 | ||
62847751 | 3501 | static int wpa_driver_nl80211_sta_add(void *priv, |
0f4e8b4f JM |
3502 | struct hostapd_sta_add_params *params) |
3503 | { | |
a2e40bb6 FF |
3504 | struct i802_bss *bss = priv; |
3505 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
8970bae8 | 3506 | struct nl_msg *msg; |
95ab6063 | 3507 | struct nl80211_sta_flag_update upd; |
0f4e8b4f JM |
3508 | int ret = -ENOBUFS; |
3509 | ||
45b722f1 AN |
3510 | if ((params->flags & WPA_STA_TDLS_PEER) && |
3511 | !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) | |
3512 | return -EOPNOTSUPP; | |
3513 | ||
e4dea253 JM |
3514 | wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR, |
3515 | params->set ? "Set" : "Add", MAC2STR(params->addr)); | |
13f83980 JM |
3516 | msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION : |
3517 | NL80211_CMD_NEW_STATION); | |
3518 | if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr)) | |
a862e4a3 | 3519 | goto fail; |
5551bc9c BC |
3520 | |
3521 | if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) { | |
5551bc9c BC |
3522 | wpa_hexdump(MSG_DEBUG, " * supported rates", |
3523 | params->supp_rates, params->supp_rates_len); | |
a862e4a3 JM |
3524 | wpa_printf(MSG_DEBUG, " * capability=0x%x", |
3525 | params->capability); | |
3526 | if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES, | |
3527 | params->supp_rates_len, params->supp_rates) || | |
3528 | nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY, | |
3529 | params->capability)) | |
3530 | goto fail; | |
e503861f JM |
3531 | |
3532 | if (params->ht_capabilities) { | |
3533 | wpa_hexdump(MSG_DEBUG, " * ht_capabilities", | |
3534 | (u8 *) params->ht_capabilities, | |
3535 | sizeof(*params->ht_capabilities)); | |
a862e4a3 JM |
3536 | if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, |
3537 | sizeof(*params->ht_capabilities), | |
3538 | params->ht_capabilities)) | |
3539 | goto fail; | |
e503861f JM |
3540 | } |
3541 | ||
3542 | if (params->vht_capabilities) { | |
3543 | wpa_hexdump(MSG_DEBUG, " * vht_capabilities", | |
3544 | (u8 *) params->vht_capabilities, | |
3545 | sizeof(*params->vht_capabilities)); | |
a862e4a3 JM |
3546 | if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, |
3547 | sizeof(*params->vht_capabilities), | |
3548 | params->vht_capabilities)) | |
3549 | goto fail; | |
e503861f JM |
3550 | } |
3551 | ||
e503861f JM |
3552 | if (params->ext_capab) { |
3553 | wpa_hexdump(MSG_DEBUG, " * ext_capab", | |
3554 | params->ext_capab, params->ext_capab_len); | |
a862e4a3 JM |
3555 | if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY, |
3556 | params->ext_capab_len, params->ext_capab)) | |
3557 | goto fail; | |
e503861f | 3558 | } |
5551bc9c | 3559 | } |
45b722f1 | 3560 | if (!params->set) { |
f11b72c3 JM |
3561 | if (params->aid) { |
3562 | wpa_printf(MSG_DEBUG, " * aid=%u", params->aid); | |
a862e4a3 JM |
3563 | if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid)) |
3564 | goto fail; | |
f11b72c3 JM |
3565 | } else { |
3566 | /* | |
3567 | * cfg80211 validates that AID is non-zero, so we have | |
3568 | * to make this a non-zero value for the TDLS case where | |
3569 | * a dummy STA entry is used for now. | |
3570 | */ | |
3571 | wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)"); | |
a862e4a3 JM |
3572 | if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1)) |
3573 | goto fail; | |
f11b72c3 | 3574 | } |
e4dea253 JM |
3575 | wpa_printf(MSG_DEBUG, " * listen_interval=%u", |
3576 | params->listen_interval); | |
a862e4a3 JM |
3577 | if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, |
3578 | params->listen_interval)) | |
3579 | goto fail; | |
e112764e JM |
3580 | } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) { |
3581 | wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid); | |
a862e4a3 JM |
3582 | if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid)) |
3583 | goto fail; | |
45b722f1 | 3584 | } |
05a8d422 | 3585 | |
8a458116 MK |
3586 | if (params->vht_opmode_enabled) { |
3587 | wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode); | |
a862e4a3 JM |
3588 | if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF, |
3589 | params->vht_opmode)) | |
3590 | goto fail; | |
8a458116 MK |
3591 | } |
3592 | ||
efc64886 SD |
3593 | if (params->supp_channels) { |
3594 | wpa_hexdump(MSG_DEBUG, " * supported channels", | |
3595 | params->supp_channels, params->supp_channels_len); | |
a862e4a3 JM |
3596 | if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS, |
3597 | params->supp_channels_len, params->supp_channels)) | |
3598 | goto fail; | |
efc64886 SD |
3599 | } |
3600 | ||
3601 | if (params->supp_oper_classes) { | |
3602 | wpa_hexdump(MSG_DEBUG, " * supported operating classes", | |
3603 | params->supp_oper_classes, | |
3604 | params->supp_oper_classes_len); | |
a862e4a3 JM |
3605 | if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES, |
3606 | params->supp_oper_classes_len, | |
3607 | params->supp_oper_classes)) | |
3608 | goto fail; | |
efc64886 SD |
3609 | } |
3610 | ||
95ab6063 | 3611 | os_memset(&upd, 0, sizeof(upd)); |
7e31703e BC |
3612 | upd.set = sta_flags_nl80211(params->flags); |
3613 | upd.mask = upd.set | sta_flags_nl80211(params->flags_mask); | |
e4dea253 JM |
3614 | wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x", |
3615 | upd.set, upd.mask); | |
a862e4a3 JM |
3616 | if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) |
3617 | goto fail; | |
95ab6063 | 3618 | |
7c7e7877 | 3619 | #ifdef CONFIG_MESH |
a862e4a3 JM |
3620 | if (params->plink_state && |
3621 | nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE, | |
3622 | sta_plink_state_nl80211(params->plink_state))) | |
3623 | goto fail; | |
7c7e7877 BC |
3624 | #endif /* CONFIG_MESH */ |
3625 | ||
774bfa62 | 3626 | if (params->flags & WPA_STA_WMM) { |
8970bae8 JB |
3627 | struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME); |
3628 | ||
e4dea253 | 3629 | wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo); |
a862e4a3 JM |
3630 | if (!wme || |
3631 | nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES, | |
3632 | params->qosinfo & WMM_QOSINFO_STA_AC_MASK) || | |
3633 | nla_put_u8(msg, NL80211_STA_WME_MAX_SP, | |
3634 | (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) & | |
3635 | WMM_QOSINFO_STA_SP_MASK)) | |
3636 | goto fail; | |
8970bae8 | 3637 | nla_nest_end(msg, wme); |
774bfa62 EP |
3638 | } |
3639 | ||
0f4e8b4f | 3640 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
5883168a | 3641 | msg = NULL; |
0f4e8b4f | 3642 | if (ret) |
45b722f1 AN |
3643 | wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION " |
3644 | "result: %d (%s)", params->set ? "SET" : "NEW", ret, | |
3645 | strerror(-ret)); | |
0f4e8b4f JM |
3646 | if (ret == -EEXIST) |
3647 | ret = 0; | |
a862e4a3 | 3648 | fail: |
5883168a | 3649 | nlmsg_free(msg); |
0f4e8b4f JM |
3650 | return ret; |
3651 | } | |
3652 | ||
3653 | ||
97ed9a06 KP |
3654 | static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr) |
3655 | { | |
3656 | #ifdef CONFIG_LIBNL3_ROUTE | |
3657 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3658 | struct rtnl_neigh *rn; | |
3659 | struct nl_addr *nl_addr; | |
3660 | int err; | |
3661 | ||
3662 | rn = rtnl_neigh_alloc(); | |
3663 | if (!rn) | |
3664 | return; | |
3665 | ||
3666 | rtnl_neigh_set_family(rn, AF_BRIDGE); | |
3667 | rtnl_neigh_set_ifindex(rn, bss->ifindex); | |
3668 | nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN); | |
3669 | if (!nl_addr) { | |
3670 | rtnl_neigh_put(rn); | |
3671 | return; | |
3672 | } | |
3673 | rtnl_neigh_set_lladdr(rn, nl_addr); | |
3674 | ||
3675 | err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0); | |
3676 | if (err < 0) { | |
3677 | wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for " | |
3678 | MACSTR " ifindex=%d failed: %s", MAC2STR(addr), | |
3679 | bss->ifindex, nl_geterror(err)); | |
3680 | } else { | |
3681 | wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for " | |
3682 | MACSTR, MAC2STR(addr)); | |
3683 | } | |
3684 | ||
3685 | nl_addr_put(nl_addr); | |
3686 | rtnl_neigh_put(rn); | |
3687 | #endif /* CONFIG_LIBNL3_ROUTE */ | |
3688 | } | |
3689 | ||
3690 | ||
59d7148a JM |
3691 | static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr, |
3692 | int deauth, u16 reason_code) | |
0f4e8b4f | 3693 | { |
a2e40bb6 | 3694 | struct wpa_driver_nl80211_data *drv = bss->drv; |
0f4e8b4f JM |
3695 | struct nl_msg *msg; |
3696 | int ret; | |
3697 | ||
13f83980 | 3698 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) || |
a862e4a3 JM |
3699 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) || |
3700 | (deauth == 0 && | |
3701 | nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE, | |
3702 | WLAN_FC_STYPE_DISASSOC)) || | |
3703 | (deauth == 1 && | |
3704 | nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE, | |
3705 | WLAN_FC_STYPE_DEAUTH)) || | |
3706 | (reason_code && | |
3707 | nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) { | |
3708 | nlmsg_free(msg); | |
3709 | return -ENOBUFS; | |
3710 | } | |
0f4e8b4f JM |
3711 | |
3712 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
83e7bb0e JM |
3713 | wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR |
3714 | " --> %d (%s)", | |
3715 | bss->ifname, MAC2STR(addr), ret, strerror(-ret)); | |
97ed9a06 KP |
3716 | |
3717 | if (drv->rtnl_sk) | |
3718 | rtnl_neigh_delete_fdb_entry(bss, addr); | |
3719 | ||
0f4e8b4f JM |
3720 | if (ret == -ENOENT) |
3721 | return 0; | |
3722 | return ret; | |
0f4e8b4f JM |
3723 | } |
3724 | ||
1581b38b | 3725 | |
f3407c66 | 3726 | void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx) |
0915d02c JM |
3727 | { |
3728 | struct nl_msg *msg; | |
de884303 | 3729 | struct wpa_driver_nl80211_data *drv2; |
0915d02c | 3730 | |
c6e8e8e4 JM |
3731 | wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx); |
3732 | ||
2135f224 | 3733 | /* stop listening for EAPOL on this interface */ |
de884303 JM |
3734 | dl_list_for_each(drv2, &drv->global->interfaces, |
3735 | struct wpa_driver_nl80211_data, list) | |
3736 | del_ifidx(drv2, ifidx); | |
2135f224 | 3737 | |
95376e1a | 3738 | msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE); |
0915d02c JM |
3739 | if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) |
3740 | return; | |
e748062b | 3741 | wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx); |
0915d02c JM |
3742 | } |
3743 | ||
3744 | ||
a1922f93 JM |
3745 | static const char * nl80211_iftype_str(enum nl80211_iftype mode) |
3746 | { | |
3747 | switch (mode) { | |
3748 | case NL80211_IFTYPE_ADHOC: | |
3749 | return "ADHOC"; | |
3750 | case NL80211_IFTYPE_STATION: | |
3751 | return "STATION"; | |
3752 | case NL80211_IFTYPE_AP: | |
3753 | return "AP"; | |
1045ec36 JM |
3754 | case NL80211_IFTYPE_AP_VLAN: |
3755 | return "AP_VLAN"; | |
3756 | case NL80211_IFTYPE_WDS: | |
3757 | return "WDS"; | |
a1922f93 JM |
3758 | case NL80211_IFTYPE_MONITOR: |
3759 | return "MONITOR"; | |
1045ec36 JM |
3760 | case NL80211_IFTYPE_MESH_POINT: |
3761 | return "MESH_POINT"; | |
a1922f93 JM |
3762 | case NL80211_IFTYPE_P2P_CLIENT: |
3763 | return "P2P_CLIENT"; | |
3764 | case NL80211_IFTYPE_P2P_GO: | |
3765 | return "P2P_GO"; | |
7aad838c NS |
3766 | case NL80211_IFTYPE_P2P_DEVICE: |
3767 | return "P2P_DEVICE"; | |
a1922f93 JM |
3768 | default: |
3769 | return "unknown"; | |
3770 | } | |
3771 | } | |
3772 | ||
3773 | ||
a35187e7 KH |
3774 | static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, |
3775 | const char *ifname, | |
3776 | enum nl80211_iftype iftype, | |
d6dcfcda DS |
3777 | const u8 *addr, int wds, |
3778 | int (*handler)(struct nl_msg *, void *), | |
3779 | void *arg) | |
0915d02c | 3780 | { |
8970bae8 | 3781 | struct nl_msg *msg; |
0915d02c JM |
3782 | int ifidx; |
3783 | int ret = -ENOBUFS; | |
3784 | ||
a1922f93 JM |
3785 | wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)", |
3786 | iftype, nl80211_iftype_str(iftype)); | |
3787 | ||
56f77852 JM |
3788 | msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE); |
3789 | if (!msg || | |
a862e4a3 JM |
3790 | nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) || |
3791 | nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype)) | |
3792 | goto fail; | |
0915d02c JM |
3793 | |
3794 | if (iftype == NL80211_IFTYPE_MONITOR) { | |
8970bae8 | 3795 | struct nlattr *flags; |
0915d02c | 3796 | |
8970bae8 | 3797 | flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS); |
a862e4a3 JM |
3798 | if (!flags || |
3799 | nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES)) | |
3800 | goto fail; | |
0915d02c | 3801 | |
8970bae8 | 3802 | nla_nest_end(msg, flags); |
fbbfcbac | 3803 | } else if (wds) { |
a862e4a3 JM |
3804 | if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds)) |
3805 | goto fail; | |
0915d02c JM |
3806 | } |
3807 | ||
52f5877a IP |
3808 | /* |
3809 | * Tell cfg80211 that the interface belongs to the socket that created | |
3810 | * it, and the interface should be deleted when the socket is closed. | |
3811 | */ | |
a862e4a3 JM |
3812 | if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER)) |
3813 | goto fail; | |
52f5877a | 3814 | |
d6dcfcda | 3815 | ret = send_and_recv_msgs(drv, msg, handler, arg); |
5883168a | 3816 | msg = NULL; |
0915d02c | 3817 | if (ret) { |
a862e4a3 | 3818 | fail: |
5883168a | 3819 | nlmsg_free(msg); |
a35187e7 KH |
3820 | wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)", |
3821 | ifname, ret, strerror(-ret)); | |
0915d02c JM |
3822 | return ret; |
3823 | } | |
3824 | ||
f632e483 | 3825 | if (iftype == NL80211_IFTYPE_P2P_DEVICE) |
6bae92e0 | 3826 | return 0; |
6bae92e0 | 3827 | |
0915d02c | 3828 | ifidx = if_nametoindex(ifname); |
c6e8e8e4 JM |
3829 | wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d", |
3830 | ifname, ifidx); | |
0915d02c JM |
3831 | |
3832 | if (ifidx <= 0) | |
3833 | return -1; | |
3834 | ||
147848ec JM |
3835 | /* |
3836 | * Some virtual interfaces need to process EAPOL packets and events on | |
3837 | * the parent interface. This is used mainly with hostapd. | |
3838 | */ | |
3839 | if (drv->hostapd || | |
3840 | iftype == NL80211_IFTYPE_AP_VLAN || | |
3841 | iftype == NL80211_IFTYPE_WDS || | |
3842 | iftype == NL80211_IFTYPE_MONITOR) { | |
3843 | /* start listening for EAPOL on this interface */ | |
3844 | add_ifidx(drv, ifidx); | |
3845 | } | |
2135f224 | 3846 | |
7bfc47c3 | 3847 | if (addr && iftype != NL80211_IFTYPE_MONITOR && |
c81eff1a | 3848 | linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) { |
41d931ee JM |
3849 | nl80211_remove_iface(drv, ifidx); |
3850 | return -1; | |
2135f224 | 3851 | } |
2135f224 | 3852 | |
0915d02c JM |
3853 | return ifidx; |
3854 | } | |
22a7c9d7 JM |
3855 | |
3856 | ||
f3407c66 JM |
3857 | int nl80211_create_iface(struct wpa_driver_nl80211_data *drv, |
3858 | const char *ifname, enum nl80211_iftype iftype, | |
3859 | const u8 *addr, int wds, | |
3860 | int (*handler)(struct nl_msg *, void *), | |
3861 | void *arg, int use_existing) | |
a35187e7 KH |
3862 | { |
3863 | int ret; | |
3864 | ||
d6dcfcda DS |
3865 | ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler, |
3866 | arg); | |
a35187e7 | 3867 | |
ffbf1eaa | 3868 | /* if error occurred and interface exists already */ |
a35187e7 | 3869 | if (ret == -ENFILE && if_nametoindex(ifname)) { |
2aec4f3c JM |
3870 | if (use_existing) { |
3871 | wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s", | |
3872 | ifname); | |
6997f8ba JM |
3873 | if (addr && iftype != NL80211_IFTYPE_MONITOR && |
3874 | linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, | |
3875 | addr) < 0 && | |
3876 | (linux_set_iface_flags(drv->global->ioctl_sock, | |
3877 | ifname, 0) < 0 || | |
3878 | linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, | |
3879 | addr) < 0 || | |
3880 | linux_set_iface_flags(drv->global->ioctl_sock, | |
3881 | ifname, 1) < 0)) | |
3882 | return -1; | |
2aec4f3c JM |
3883 | return -ENFILE; |
3884 | } | |
a35187e7 KH |
3885 | wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname); |
3886 | ||
3887 | /* Try to remove the interface that was already there. */ | |
3888 | nl80211_remove_iface(drv, if_nametoindex(ifname)); | |
3889 | ||
3890 | /* Try to create the interface again */ | |
fbbfcbac | 3891 | ret = nl80211_create_iface_once(drv, ifname, iftype, addr, |
d6dcfcda | 3892 | wds, handler, arg); |
a35187e7 KH |
3893 | } |
3894 | ||
3e208481 JM |
3895 | if (ret >= 0 && is_p2p_net_interface(iftype)) { |
3896 | wpa_printf(MSG_DEBUG, | |
3897 | "nl80211: Interface %s created for P2P - disable 11b rates", | |
3898 | ifname); | |
4e5cb1a3 | 3899 | nl80211_disable_11b_rates(drv, ret, 1); |
3e208481 | 3900 | } |
4e5cb1a3 | 3901 | |
a35187e7 KH |
3902 | return ret; |
3903 | } | |
0915d02c | 3904 | |
2135f224 | 3905 | |
3fd1cefb JB |
3906 | static int nl80211_setup_ap(struct i802_bss *bss) |
3907 | { | |
3908 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3909 | ||
748c0ac0 JM |
3910 | wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d", |
3911 | bss->ifname, drv->device_ap_sme, drv->use_monitor); | |
36488c05 | 3912 | |
a11241fa JB |
3913 | /* |
3914 | * Disable Probe Request reporting unless we need it in this way for | |
3915 | * devices that include the AP SME, in the other case (unless using | |
3916 | * monitor iface) we'll get it through the nl_mgmt socket instead. | |
3917 | */ | |
3918 | if (!drv->device_ap_sme) | |
3919 | wpa_driver_nl80211_probe_req_report(bss, 0); | |
3920 | ||
3921 | if (!drv->device_ap_sme && !drv->use_monitor) | |
3922 | if (nl80211_mgmt_subscribe_ap(bss)) | |
3923 | return -1; | |
3924 | ||
a6cc0602 JM |
3925 | if (drv->device_ap_sme && !drv->use_monitor) |
3926 | if (nl80211_mgmt_subscribe_ap_dev_sme(bss)) | |
3927 | return -1; | |
3928 | ||
a11241fa | 3929 | if (!drv->device_ap_sme && drv->use_monitor && |
3fd1cefb JB |
3930 | nl80211_create_monitor_interface(drv) && |
3931 | !drv->device_ap_sme) | |
3932 | return -1; | |
3933 | ||
3934 | if (drv->device_ap_sme && | |
3935 | wpa_driver_nl80211_probe_req_report(bss, 1) < 0) { | |
3936 | wpa_printf(MSG_DEBUG, "nl80211: Failed to enable " | |
3937 | "Probe Request frame reporting in AP mode"); | |
3938 | /* Try to survive without this */ | |
3939 | } | |
3940 | ||
3941 | return 0; | |
3942 | } | |
3943 | ||
3944 | ||
3945 | static void nl80211_teardown_ap(struct i802_bss *bss) | |
3946 | { | |
3947 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3948 | ||
748c0ac0 JM |
3949 | wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d", |
3950 | bss->ifname, drv->device_ap_sme, drv->use_monitor); | |
a6cc0602 | 3951 | if (drv->device_ap_sme) { |
3fd1cefb | 3952 | wpa_driver_nl80211_probe_req_report(bss, 0); |
a6cc0602 JM |
3953 | if (!drv->use_monitor) |
3954 | nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)"); | |
3955 | } else if (drv->use_monitor) | |
3fd1cefb | 3956 | nl80211_remove_monitor_interface(drv); |
a11241fa | 3957 | else |
36488c05 | 3958 | nl80211_mgmt_unsubscribe(bss, "AP teardown"); |
a11241fa | 3959 | |
3fd1cefb JB |
3960 | bss->beacon_set = 0; |
3961 | } | |
3962 | ||
3963 | ||
f10bfc9a JM |
3964 | static int nl80211_send_eapol_data(struct i802_bss *bss, |
3965 | const u8 *addr, const u8 *data, | |
d12dab4c | 3966 | size_t data_len) |
f10bfc9a | 3967 | { |
d12dab4c JB |
3968 | struct sockaddr_ll ll; |
3969 | int ret; | |
3970 | ||
3971 | if (bss->drv->eapol_tx_sock < 0) { | |
3972 | wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL"); | |
f10bfc9a JM |
3973 | return -1; |
3974 | } | |
3975 | ||
d12dab4c JB |
3976 | os_memset(&ll, 0, sizeof(ll)); |
3977 | ll.sll_family = AF_PACKET; | |
3978 | ll.sll_ifindex = bss->ifindex; | |
3979 | ll.sll_protocol = htons(ETH_P_PAE); | |
3980 | ll.sll_halen = ETH_ALEN; | |
3981 | os_memcpy(ll.sll_addr, addr, ETH_ALEN); | |
3982 | ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0, | |
3983 | (struct sockaddr *) &ll, sizeof(ll)); | |
3984 | if (ret < 0) | |
3985 | wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s", | |
3986 | strerror(errno)); | |
3987 | ||
3988 | return ret; | |
f10bfc9a | 3989 | } |
5fb1a232 | 3990 | |
f10bfc9a | 3991 | |
db149ac9 JM |
3992 | static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; |
3993 | ||
3994 | static int wpa_driver_nl80211_hapd_send_eapol( | |
3995 | void *priv, const u8 *addr, const u8 *data, | |
4378fc14 | 3996 | size_t data_len, int encrypt, const u8 *own_addr, u32 flags) |
db149ac9 | 3997 | { |
a2e40bb6 FF |
3998 | struct i802_bss *bss = priv; |
3999 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
db149ac9 JM |
4000 | struct ieee80211_hdr *hdr; |
4001 | size_t len; | |
4002 | u8 *pos; | |
4003 | int res; | |
4378fc14 | 4004 | int qos = flags & WPA_STA_WMM; |
db149ac9 | 4005 | |
a11241fa | 4006 | if (drv->device_ap_sme || !drv->use_monitor) |
d12dab4c | 4007 | return nl80211_send_eapol_data(bss, addr, data, data_len); |
f10bfc9a | 4008 | |
db149ac9 JM |
4009 | len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 + |
4010 | data_len; | |
4011 | hdr = os_zalloc(len); | |
4012 | if (hdr == NULL) { | |
7ac3616d JM |
4013 | wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)", |
4014 | (unsigned long) len); | |
db149ac9 JM |
4015 | return -1; |
4016 | } | |
4017 | ||
4018 | hdr->frame_control = | |
4019 | IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA); | |
4020 | hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS); | |
4021 | if (encrypt) | |
4022 | hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP); | |
db149ac9 JM |
4023 | if (qos) { |
4024 | hdr->frame_control |= | |
4025 | host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4); | |
4026 | } | |
db149ac9 JM |
4027 | |
4028 | memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN); | |
4029 | memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); | |
4030 | memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); | |
4031 | pos = (u8 *) (hdr + 1); | |
4032 | ||
db149ac9 | 4033 | if (qos) { |
92d521d8 FF |
4034 | /* Set highest priority in QoS header */ |
4035 | pos[0] = 7; | |
db149ac9 JM |
4036 | pos[1] = 0; |
4037 | pos += 2; | |
4038 | } | |
db149ac9 JM |
4039 | |
4040 | memcpy(pos, rfc1042_header, sizeof(rfc1042_header)); | |
4041 | pos += sizeof(rfc1042_header); | |
4042 | WPA_PUT_BE16(pos, ETH_P_PAE); | |
4043 | pos += 2; | |
4044 | memcpy(pos, data, data_len); | |
4045 | ||
55231068 JM |
4046 | res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0, |
4047 | 0, 0, 0, 0); | |
db149ac9 JM |
4048 | if (res < 0) { |
4049 | wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - " | |
4050 | "failed: %d (%s)", | |
4051 | (unsigned long) len, errno, strerror(errno)); | |
4052 | } | |
7bf12757 | 4053 | os_free(hdr); |
db149ac9 JM |
4054 | |
4055 | return res; | |
4056 | } | |
4057 | ||
a8d6ffa4 | 4058 | |
3234cba4 JM |
4059 | static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, |
4060 | int total_flags, | |
4c32757d | 4061 | int flags_or, int flags_and) |
a8d6ffa4 | 4062 | { |
a2e40bb6 | 4063 | struct i802_bss *bss = priv; |
8970bae8 JB |
4064 | struct nl_msg *msg; |
4065 | struct nlattr *flags; | |
7e76ee9c | 4066 | struct nl80211_sta_flag_update upd; |
a8d6ffa4 | 4067 | |
0cd9846c JM |
4068 | wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR |
4069 | " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d", | |
4070 | bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and, | |
4071 | !!(total_flags & WPA_STA_AUTHORIZED)); | |
4072 | ||
13f83980 | 4073 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) || |
a862e4a3 JM |
4074 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) |
4075 | goto fail; | |
a8d6ffa4 | 4076 | |
7e76ee9c JM |
4077 | /* |
4078 | * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This | |
4079 | * can be removed eventually. | |
4080 | */ | |
8970bae8 | 4081 | flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS); |
a862e4a3 JM |
4082 | if (!flags || |
4083 | ((total_flags & WPA_STA_AUTHORIZED) && | |
4084 | nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) || | |
4085 | ((total_flags & WPA_STA_WMM) && | |
4086 | nla_put_flag(msg, NL80211_STA_FLAG_WME)) || | |
4087 | ((total_flags & WPA_STA_SHORT_PREAMBLE) && | |
4088 | nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) || | |
4089 | ((total_flags & WPA_STA_MFP) && | |
4090 | nla_put_flag(msg, NL80211_STA_FLAG_MFP)) || | |
4091 | ((total_flags & WPA_STA_TDLS_PEER) && | |
4092 | nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER))) | |
4093 | goto fail; | |
45b722f1 | 4094 | |
8970bae8 | 4095 | nla_nest_end(msg, flags); |
a8d6ffa4 | 4096 | |
7e76ee9c JM |
4097 | os_memset(&upd, 0, sizeof(upd)); |
4098 | upd.mask = sta_flags_nl80211(flags_or | ~flags_and); | |
4099 | upd.set = sta_flags_nl80211(flags_or); | |
a862e4a3 JM |
4100 | if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) |
4101 | goto fail; | |
7e76ee9c | 4102 | |
13f83980 | 4103 | return send_and_recv_msgs(bss->drv, msg, NULL, NULL); |
a862e4a3 | 4104 | fail: |
5883168a | 4105 | nlmsg_free(msg); |
a8d6ffa4 JM |
4106 | return -ENOBUFS; |
4107 | } | |
4108 | ||
0915d02c | 4109 | |
1581b38b JM |
4110 | static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv, |
4111 | struct wpa_driver_associate_params *params) | |
4112 | { | |
708bc8e0 | 4113 | enum nl80211_iftype nlmode, old_mode; |
b1f625e0 EP |
4114 | |
4115 | if (params->p2p) { | |
046b26a2 JM |
4116 | wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P " |
4117 | "group (GO)"); | |
b1f625e0 EP |
4118 | nlmode = NL80211_IFTYPE_P2P_GO; |
4119 | } else | |
4120 | nlmode = NL80211_IFTYPE_AP; | |
4121 | ||
708bc8e0 | 4122 | old_mode = drv->nlmode; |
834ee56f | 4123 | if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) { |
708bc8e0 JM |
4124 | nl80211_remove_monitor_interface(drv); |
4125 | return -1; | |
4126 | } | |
4127 | ||
4ec68377 | 4128 | if (nl80211_set_channel(drv->first_bss, ¶ms->freq, 0)) { |
708bc8e0 | 4129 | if (old_mode != nlmode) |
834ee56f | 4130 | wpa_driver_nl80211_set_mode(drv->first_bss, old_mode); |
460456f8 | 4131 | nl80211_remove_monitor_interface(drv); |
1581b38b | 4132 | return -1; |
0915d02c | 4133 | } |
1581b38b | 4134 | |
1581b38b JM |
4135 | return 0; |
4136 | } | |
1581b38b JM |
4137 | |
4138 | ||
666e508c JM |
4139 | static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv, |
4140 | int reset_mode) | |
5cc4d64b JM |
4141 | { |
4142 | struct nl_msg *msg; | |
9725b784 | 4143 | int ret; |
a862e4a3 | 4144 | |
9725b784 | 4145 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS); |
5cc4d64b | 4146 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
5cc4d64b JM |
4147 | if (ret) { |
4148 | wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d " | |
4149 | "(%s)", ret, strerror(-ret)); | |
9725b784 JM |
4150 | } else { |
4151 | wpa_printf(MSG_DEBUG, | |
4152 | "nl80211: Leave IBSS request sent successfully"); | |
5cc4d64b JM |
4153 | } |
4154 | ||
666e508c JM |
4155 | if (reset_mode && |
4156 | wpa_driver_nl80211_set_mode(drv->first_bss, | |
5d4c78fb JM |
4157 | NL80211_IFTYPE_STATION)) { |
4158 | wpa_printf(MSG_INFO, "nl80211: Failed to set interface into " | |
4159 | "station mode"); | |
4160 | } | |
4161 | ||
5cc4d64b JM |
4162 | return ret; |
4163 | } | |
4164 | ||
4165 | ||
4166 | static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv, | |
4167 | struct wpa_driver_associate_params *params) | |
4168 | { | |
4169 | struct nl_msg *msg; | |
4170 | int ret = -1; | |
4171 | int count = 0; | |
4172 | ||
4173 | wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex); | |
4174 | ||
4ec68377 | 4175 | if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, ¶ms->freq)) { |
5cc4d64b JM |
4176 | wpa_printf(MSG_INFO, "nl80211: Failed to set interface into " |
4177 | "IBSS mode"); | |
4178 | return -1; | |
4179 | } | |
4180 | ||
4181 | retry: | |
9725b784 | 4182 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) || |
a862e4a3 JM |
4183 | params->ssid == NULL || params->ssid_len > sizeof(drv->ssid)) |
4184 | goto fail; | |
5cc4d64b JM |
4185 | |
4186 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
4187 | params->ssid, params->ssid_len); | |
a862e4a3 JM |
4188 | if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid)) |
4189 | goto fail; | |
5cc4d64b JM |
4190 | os_memcpy(drv->ssid, params->ssid, params->ssid_len); |
4191 | drv->ssid_len = params->ssid_len; | |
4192 | ||
4ec68377 JD |
4193 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq); |
4194 | wpa_printf(MSG_DEBUG, " * ht_enabled=%d", params->freq.ht_enabled); | |
4195 | wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d", | |
4196 | params->freq.sec_channel_offset); | |
4197 | wpa_printf(MSG_DEBUG, " * vht_enabled=%d", params->freq.vht_enabled); | |
4198 | wpa_printf(MSG_DEBUG, " * center_freq1=%d", params->freq.center_freq1); | |
4199 | wpa_printf(MSG_DEBUG, " * center_freq2=%d", params->freq.center_freq2); | |
4200 | wpa_printf(MSG_DEBUG, " * bandwidth=%d", params->freq.bandwidth); | |
4201 | if (nl80211_put_freq_params(msg, ¶ms->freq) < 0) | |
a862e4a3 | 4202 | goto fail; |
5cc4d64b | 4203 | |
8f05577d JM |
4204 | if (params->beacon_int > 0) { |
4205 | wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int); | |
a862e4a3 JM |
4206 | if (nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL, |
4207 | params->beacon_int)) | |
4208 | goto fail; | |
8f05577d JM |
4209 | } |
4210 | ||
5cc4d64b JM |
4211 | ret = nl80211_set_conn_keys(params, msg); |
4212 | if (ret) | |
a862e4a3 | 4213 | goto fail; |
5cc4d64b | 4214 | |
913e3cf7 NC |
4215 | if (params->bssid && params->fixed_bssid) { |
4216 | wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR, | |
4217 | MAC2STR(params->bssid)); | |
a862e4a3 JM |
4218 | if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) |
4219 | goto fail; | |
913e3cf7 NC |
4220 | } |
4221 | ||
4848a38d JM |
4222 | if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X || |
4223 | params->key_mgmt_suite == WPA_KEY_MGMT_PSK || | |
4224 | params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 || | |
4225 | params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) { | |
e640888c | 4226 | wpa_printf(MSG_DEBUG, " * control port"); |
a862e4a3 JM |
4227 | if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT)) |
4228 | goto fail; | |
e640888c AQ |
4229 | } |
4230 | ||
a95795ad JM |
4231 | if (params->wpa_ie) { |
4232 | wpa_hexdump(MSG_DEBUG, | |
4233 | " * Extra IEs for Beacon/Probe Response frames", | |
4234 | params->wpa_ie, params->wpa_ie_len); | |
a862e4a3 JM |
4235 | if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, |
4236 | params->wpa_ie)) | |
4237 | goto fail; | |
a95795ad JM |
4238 | } |
4239 | ||
5cc4d64b JM |
4240 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
4241 | msg = NULL; | |
4242 | if (ret) { | |
4243 | wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)", | |
4244 | ret, strerror(-ret)); | |
4245 | count++; | |
4246 | if (ret == -EALREADY && count == 1) { | |
4247 | wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after " | |
4248 | "forced leave"); | |
666e508c | 4249 | nl80211_leave_ibss(drv, 0); |
5cc4d64b JM |
4250 | nlmsg_free(msg); |
4251 | goto retry; | |
4252 | } | |
a862e4a3 JM |
4253 | } else { |
4254 | wpa_printf(MSG_DEBUG, | |
4255 | "nl80211: Join IBSS request sent successfully"); | |
5cc4d64b | 4256 | } |
5cc4d64b | 4257 | |
a862e4a3 | 4258 | fail: |
5cc4d64b JM |
4259 | nlmsg_free(msg); |
4260 | return ret; | |
4261 | } | |
4262 | ||
4263 | ||
a0bdd191 JM |
4264 | static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv, |
4265 | struct wpa_driver_associate_params *params, | |
4266 | struct nl_msg *msg) | |
cfaab580 | 4267 | { |
cfaab580 ZY |
4268 | if (params->bssid) { |
4269 | wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, | |
4270 | MAC2STR(params->bssid)); | |
a862e4a3 JM |
4271 | if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid)) |
4272 | return -1; | |
cfaab580 | 4273 | } |
a0bdd191 | 4274 | |
7ac7fd43 DS |
4275 | if (params->bssid_hint) { |
4276 | wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR, | |
4277 | MAC2STR(params->bssid_hint)); | |
a862e4a3 JM |
4278 | if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN, |
4279 | params->bssid_hint)) | |
4280 | return -1; | |
7ac7fd43 DS |
4281 | } |
4282 | ||
4ec68377 JD |
4283 | if (params->freq.freq) { |
4284 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq); | |
a862e4a3 JM |
4285 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, |
4286 | params->freq.freq)) | |
4287 | return -1; | |
4ec68377 | 4288 | drv->assoc_freq = params->freq.freq; |
30158a0d SD |
4289 | } else |
4290 | drv->assoc_freq = 0; | |
a0bdd191 | 4291 | |
7ac7fd43 DS |
4292 | if (params->freq_hint) { |
4293 | wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint); | |
a862e4a3 JM |
4294 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT, |
4295 | params->freq_hint)) | |
4296 | return -1; | |
7ac7fd43 DS |
4297 | } |
4298 | ||
1f6c0ab8 BS |
4299 | if (params->bg_scan_period >= 0) { |
4300 | wpa_printf(MSG_DEBUG, " * bg scan period=%d", | |
4301 | params->bg_scan_period); | |
a862e4a3 JM |
4302 | if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD, |
4303 | params->bg_scan_period)) | |
4304 | return -1; | |
1f6c0ab8 | 4305 | } |
a0bdd191 | 4306 | |
cfaab580 ZY |
4307 | if (params->ssid) { |
4308 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
4309 | params->ssid, params->ssid_len); | |
a862e4a3 JM |
4310 | if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, |
4311 | params->ssid)) | |
4312 | return -1; | |
cfaab580 | 4313 | if (params->ssid_len > sizeof(drv->ssid)) |
a862e4a3 | 4314 | return -1; |
cfaab580 ZY |
4315 | os_memcpy(drv->ssid, params->ssid, params->ssid_len); |
4316 | drv->ssid_len = params->ssid_len; | |
4317 | } | |
a0bdd191 | 4318 | |
cfaab580 | 4319 | wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); |
a862e4a3 JM |
4320 | if (params->wpa_ie && |
4321 | nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie)) | |
4322 | return -1; | |
cfaab580 | 4323 | |
64fa840a JM |
4324 | if (params->wpa_proto) { |
4325 | enum nl80211_wpa_versions ver = 0; | |
cfaab580 | 4326 | |
64fa840a JM |
4327 | if (params->wpa_proto & WPA_PROTO_WPA) |
4328 | ver |= NL80211_WPA_VERSION_1; | |
4329 | if (params->wpa_proto & WPA_PROTO_RSN) | |
4330 | ver |= NL80211_WPA_VERSION_2; | |
cfaab580 | 4331 | |
64fa840a | 4332 | wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver); |
a862e4a3 JM |
4333 | if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver)) |
4334 | return -1; | |
cfaab580 ZY |
4335 | } |
4336 | ||
4848a38d | 4337 | if (params->pairwise_suite != WPA_CIPHER_NONE) { |
a0bdd191 JM |
4338 | u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite); |
4339 | wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher); | |
a862e4a3 JM |
4340 | if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, |
4341 | cipher)) | |
4342 | return -1; | |
cfaab580 ZY |
4343 | } |
4344 | ||
ae6f9272 JM |
4345 | if (params->group_suite == WPA_CIPHER_GTK_NOT_USED && |
4346 | !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) { | |
4347 | /* | |
4348 | * This is likely to work even though many drivers do not | |
4349 | * advertise support for operations without GTK. | |
4350 | */ | |
4351 | wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement"); | |
4352 | } else if (params->group_suite != WPA_CIPHER_NONE) { | |
a0bdd191 JM |
4353 | u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite); |
4354 | wpa_printf(MSG_DEBUG, " * group=0x%x", cipher); | |
a862e4a3 JM |
4355 | if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher)) |
4356 | return -1; | |
cfaab580 ZY |
4357 | } |
4358 | ||
4848a38d JM |
4359 | if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X || |
4360 | params->key_mgmt_suite == WPA_KEY_MGMT_PSK || | |
4361 | params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X || | |
4362 | params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK || | |
163f801e | 4363 | params->key_mgmt_suite == WPA_KEY_MGMT_CCKM || |
8802326f JJ |
4364 | params->key_mgmt_suite == WPA_KEY_MGMT_OSEN || |
4365 | params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 || | |
666497c8 JM |
4366 | params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 || |
4367 | params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B) { | |
cfaab580 ZY |
4368 | int mgmt = WLAN_AKM_SUITE_PSK; |
4369 | ||
4370 | switch (params->key_mgmt_suite) { | |
4848a38d | 4371 | case WPA_KEY_MGMT_CCKM: |
369c8d7b JM |
4372 | mgmt = WLAN_AKM_SUITE_CCKM; |
4373 | break; | |
4848a38d | 4374 | case WPA_KEY_MGMT_IEEE8021X: |
cfaab580 ZY |
4375 | mgmt = WLAN_AKM_SUITE_8021X; |
4376 | break; | |
4848a38d | 4377 | case WPA_KEY_MGMT_FT_IEEE8021X: |
6a1ce395 DG |
4378 | mgmt = WLAN_AKM_SUITE_FT_8021X; |
4379 | break; | |
4848a38d | 4380 | case WPA_KEY_MGMT_FT_PSK: |
6a1ce395 DG |
4381 | mgmt = WLAN_AKM_SUITE_FT_PSK; |
4382 | break; | |
8802326f JJ |
4383 | case WPA_KEY_MGMT_IEEE8021X_SHA256: |
4384 | mgmt = WLAN_AKM_SUITE_8021X_SHA256; | |
4385 | break; | |
4386 | case WPA_KEY_MGMT_PSK_SHA256: | |
4387 | mgmt = WLAN_AKM_SUITE_PSK_SHA256; | |
4388 | break; | |
163f801e JM |
4389 | case WPA_KEY_MGMT_OSEN: |
4390 | mgmt = WLAN_AKM_SUITE_OSEN; | |
4391 | break; | |
666497c8 JM |
4392 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
4393 | mgmt = WLAN_AKM_SUITE_8021X_SUITE_B; | |
4394 | break; | |
4848a38d | 4395 | case WPA_KEY_MGMT_PSK: |
cfaab580 ZY |
4396 | default: |
4397 | mgmt = WLAN_AKM_SUITE_PSK; | |
4398 | break; | |
4399 | } | |
163f801e | 4400 | wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt); |
a862e4a3 JM |
4401 | if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt)) |
4402 | return -1; | |
cfaab580 ZY |
4403 | } |
4404 | ||
a862e4a3 JM |
4405 | if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT)) |
4406 | return -1; | |
a0bdd191 | 4407 | |
a862e4a3 JM |
4408 | if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED && |
4409 | nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED)) | |
4410 | return -1; | |
a565084f | 4411 | |
58162adf AK |
4412 | if (params->rrm_used) { |
4413 | u32 drv_rrm_flags = drv->capa.rrm_flags; | |
4414 | if (!(drv_rrm_flags & | |
4415 | WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) || | |
a862e4a3 JM |
4416 | !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) || |
4417 | nla_put_flag(msg, NL80211_ATTR_USE_RRM)) | |
4418 | return -1; | |
58162adf AK |
4419 | } |
4420 | ||
a862e4a3 JM |
4421 | if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT)) |
4422 | return -1; | |
80e8a5ee BG |
4423 | |
4424 | if (params->htcaps && params->htcaps_mask) { | |
4425 | int sz = sizeof(struct ieee80211_ht_capabilities); | |
6d99bd80 | 4426 | wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz); |
6d99bd80 JM |
4427 | wpa_hexdump(MSG_DEBUG, " * htcaps_mask", |
4428 | params->htcaps_mask, sz); | |
a862e4a3 JM |
4429 | if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz, |
4430 | params->htcaps) || | |
4431 | nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz, | |
4432 | params->htcaps_mask)) | |
4433 | return -1; | |
80e8a5ee BG |
4434 | } |
4435 | ||
e9ee8dc3 JB |
4436 | #ifdef CONFIG_VHT_OVERRIDES |
4437 | if (params->disable_vht) { | |
4438 | wpa_printf(MSG_DEBUG, " * VHT disabled"); | |
a862e4a3 JM |
4439 | if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT)) |
4440 | return -1; | |
e9ee8dc3 JB |
4441 | } |
4442 | ||
4443 | if (params->vhtcaps && params->vhtcaps_mask) { | |
4444 | int sz = sizeof(struct ieee80211_vht_capabilities); | |
6d99bd80 | 4445 | wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz); |
6d99bd80 JM |
4446 | wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask", |
4447 | params->vhtcaps_mask, sz); | |
a862e4a3 JM |
4448 | if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz, |
4449 | params->vhtcaps) || | |
4450 | nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz, | |
4451 | params->vhtcaps_mask)) | |
4452 | return -1; | |
e9ee8dc3 JB |
4453 | } |
4454 | #endif /* CONFIG_VHT_OVERRIDES */ | |
4455 | ||
a0bdd191 JM |
4456 | if (params->p2p) |
4457 | wpa_printf(MSG_DEBUG, " * P2P group"); | |
4458 | ||
4459 | return 0; | |
a0bdd191 JM |
4460 | } |
4461 | ||
4462 | ||
4463 | static int wpa_driver_nl80211_try_connect( | |
4464 | struct wpa_driver_nl80211_data *drv, | |
4465 | struct wpa_driver_associate_params *params) | |
4466 | { | |
4467 | struct nl_msg *msg; | |
4468 | enum nl80211_auth_type type; | |
4469 | int ret; | |
4470 | int algs; | |
4471 | ||
b41f2684 CL |
4472 | if (params->req_key_mgmt_offload && params->psk && |
4473 | (params->key_mgmt_suite == WPA_KEY_MGMT_PSK || | |
4474 | params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 || | |
4475 | params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) { | |
4476 | wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK"); | |
4477 | ret = issue_key_mgmt_set_key(drv, params->psk, 32); | |
4478 | if (ret) | |
4479 | return ret; | |
4480 | } | |
4481 | ||
9725b784 JM |
4482 | wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex); |
4483 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT); | |
a0bdd191 JM |
4484 | if (!msg) |
4485 | return -1; | |
4486 | ||
a0bdd191 JM |
4487 | ret = nl80211_connect_common(drv, params, msg); |
4488 | if (ret) | |
a862e4a3 | 4489 | goto fail; |
a0bdd191 JM |
4490 | |
4491 | algs = 0; | |
4492 | if (params->auth_alg & WPA_AUTH_ALG_OPEN) | |
4493 | algs++; | |
4494 | if (params->auth_alg & WPA_AUTH_ALG_SHARED) | |
4495 | algs++; | |
4496 | if (params->auth_alg & WPA_AUTH_ALG_LEAP) | |
4497 | algs++; | |
4498 | if (algs > 1) { | |
4499 | wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic " | |
4500 | "selection"); | |
4501 | goto skip_auth_type; | |
4502 | } | |
4503 | ||
4504 | if (params->auth_alg & WPA_AUTH_ALG_OPEN) | |
4505 | type = NL80211_AUTHTYPE_OPEN_SYSTEM; | |
4506 | else if (params->auth_alg & WPA_AUTH_ALG_SHARED) | |
4507 | type = NL80211_AUTHTYPE_SHARED_KEY; | |
4508 | else if (params->auth_alg & WPA_AUTH_ALG_LEAP) | |
4509 | type = NL80211_AUTHTYPE_NETWORK_EAP; | |
4510 | else if (params->auth_alg & WPA_AUTH_ALG_FT) | |
4511 | type = NL80211_AUTHTYPE_FT; | |
4512 | else | |
a862e4a3 | 4513 | goto fail; |
a0bdd191 JM |
4514 | |
4515 | wpa_printf(MSG_DEBUG, " * Auth Type %d", type); | |
a862e4a3 JM |
4516 | if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type)) |
4517 | goto fail; | |
a0bdd191 JM |
4518 | |
4519 | skip_auth_type: | |
cfaab580 ZY |
4520 | ret = nl80211_set_conn_keys(params, msg); |
4521 | if (ret) | |
a862e4a3 | 4522 | goto fail; |
cfaab580 ZY |
4523 | |
4524 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4525 | msg = NULL; | |
4526 | if (ret) { | |
4527 | wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d " | |
4528 | "(%s)", ret, strerror(-ret)); | |
a862e4a3 JM |
4529 | } else { |
4530 | wpa_printf(MSG_DEBUG, | |
4531 | "nl80211: Connect request send successfully"); | |
cfaab580 | 4532 | } |
cfaab580 | 4533 | |
a862e4a3 | 4534 | fail: |
cfaab580 ZY |
4535 | nlmsg_free(msg); |
4536 | return ret; | |
4537 | ||
4538 | } | |
4539 | ||
4540 | ||
a8c5b43a CW |
4541 | static int wpa_driver_nl80211_connect( |
4542 | struct wpa_driver_nl80211_data *drv, | |
4543 | struct wpa_driver_associate_params *params) | |
4544 | { | |
0d4e3d1d JJ |
4545 | int ret; |
4546 | ||
4547 | /* Store the connection attempted bssid for future use */ | |
4548 | if (params->bssid) | |
4549 | os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN); | |
4550 | else | |
4551 | os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN); | |
4552 | ||
4553 | ret = wpa_driver_nl80211_try_connect(drv, params); | |
a8c5b43a CW |
4554 | if (ret == -EALREADY) { |
4555 | /* | |
4556 | * cfg80211 does not currently accept new connections if | |
4557 | * we are already connected. As a workaround, force | |
4558 | * disconnection and try again. | |
4559 | */ | |
4560 | wpa_printf(MSG_DEBUG, "nl80211: Explicitly " | |
4561 | "disconnecting before reassociation " | |
4562 | "attempt"); | |
4563 | if (wpa_driver_nl80211_disconnect( | |
4564 | drv, WLAN_REASON_PREV_AUTH_NOT_VALID)) | |
4565 | return -1; | |
a8c5b43a CW |
4566 | ret = wpa_driver_nl80211_try_connect(drv, params); |
4567 | } | |
4568 | return ret; | |
4569 | } | |
4570 | ||
4571 | ||
c2a04078 JM |
4572 | static int wpa_driver_nl80211_associate( |
4573 | void *priv, struct wpa_driver_associate_params *params) | |
4574 | { | |
a2e40bb6 FF |
4575 | struct i802_bss *bss = priv; |
4576 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
a862e4a3 | 4577 | int ret = -1; |
c2a04078 JM |
4578 | struct nl_msg *msg; |
4579 | ||
4bd71954 JM |
4580 | nl80211_unmask_11b_rates(bss); |
4581 | ||
5cc4d64b | 4582 | if (params->mode == IEEE80211_MODE_AP) |
1581b38b | 4583 | return wpa_driver_nl80211_ap(drv, params); |
1581b38b | 4584 | |
5cc4d64b JM |
4585 | if (params->mode == IEEE80211_MODE_IBSS) |
4586 | return wpa_driver_nl80211_ibss(drv, params); | |
4587 | ||
4a867032 | 4588 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) { |
b1f625e0 EP |
4589 | enum nl80211_iftype nlmode = params->p2p ? |
4590 | NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION; | |
4591 | ||
4592 | if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0) | |
4a867032 | 4593 | return -1; |
cfaab580 | 4594 | return wpa_driver_nl80211_connect(drv, params); |
4a867032 | 4595 | } |
cfaab580 | 4596 | |
add9b7a4 | 4597 | nl80211_mark_disconnected(drv); |
c2a04078 | 4598 | |
c2a04078 JM |
4599 | wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)", |
4600 | drv->ifindex); | |
9725b784 JM |
4601 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE); |
4602 | if (!msg) | |
4603 | return -1; | |
c2a04078 | 4604 | |
a0bdd191 JM |
4605 | ret = nl80211_connect_common(drv, params, msg); |
4606 | if (ret) | |
a862e4a3 | 4607 | goto fail; |
01652550 | 4608 | |
62fa124c JM |
4609 | if (params->prev_bssid) { |
4610 | wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR, | |
4611 | MAC2STR(params->prev_bssid)); | |
a862e4a3 JM |
4612 | if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN, |
4613 | params->prev_bssid)) | |
4614 | goto fail; | |
62fa124c JM |
4615 | } |
4616 | ||
c2a04078 JM |
4617 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
4618 | msg = NULL; | |
4619 | if (ret) { | |
3b7ea880 BG |
4620 | wpa_dbg(drv->ctx, MSG_DEBUG, |
4621 | "nl80211: MLME command failed (assoc): ret=%d (%s)", | |
4622 | ret, strerror(-ret)); | |
8856462d | 4623 | nl80211_dump_scan(drv); |
a862e4a3 JM |
4624 | } else { |
4625 | wpa_printf(MSG_DEBUG, | |
4626 | "nl80211: Association request send successfully"); | |
c2a04078 | 4627 | } |
c2a04078 | 4628 | |
a862e4a3 | 4629 | fail: |
c2a04078 JM |
4630 | nlmsg_free(msg); |
4631 | return ret; | |
4632 | } | |
3f5285e8 JM |
4633 | |
4634 | ||
ad1e68e6 | 4635 | static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv, |
a1922f93 | 4636 | int ifindex, enum nl80211_iftype mode) |
3f5285e8 | 4637 | { |
3f5285e8 | 4638 | struct nl_msg *msg; |
ad1e68e6 JM |
4639 | int ret = -ENOBUFS; |
4640 | ||
a1922f93 JM |
4641 | wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)", |
4642 | ifindex, mode, nl80211_iftype_str(mode)); | |
4643 | ||
56f77852 JM |
4644 | msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE); |
4645 | if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode)) | |
a862e4a3 | 4646 | goto fail; |
ad1e68e6 JM |
4647 | |
4648 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
5883168a | 4649 | msg = NULL; |
ad1e68e6 JM |
4650 | if (!ret) |
4651 | return 0; | |
a862e4a3 | 4652 | fail: |
5883168a | 4653 | nlmsg_free(msg); |
ad1e68e6 JM |
4654 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:" |
4655 | " %d (%s)", ifindex, mode, ret, strerror(-ret)); | |
4656 | return ret; | |
4657 | } | |
4658 | ||
4659 | ||
3c5d34e3 CW |
4660 | static int wpa_driver_nl80211_set_mode_impl( |
4661 | struct i802_bss *bss, | |
4662 | enum nl80211_iftype nlmode, | |
4663 | struct hostapd_freq_params *desired_freq_params) | |
ad1e68e6 | 4664 | { |
a2e40bb6 | 4665 | struct wpa_driver_nl80211_data *drv = bss->drv; |
ad1e68e6 | 4666 | int ret = -1; |
26af9dca | 4667 | int i; |
86957e62 | 4668 | int was_ap = is_ap_interface(drv->nlmode); |
671a5039 | 4669 | int res; |
ebffdbc4 | 4670 | int mode_switch_res; |
1581b38b | 4671 | |
ebffdbc4 CW |
4672 | mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode); |
4673 | if (mode_switch_res && nlmode == nl80211_get_ifmode(bss)) | |
4674 | mode_switch_res = 0; | |
8e12685c | 4675 | |
ebffdbc4 | 4676 | if (mode_switch_res == 0) { |
ad1e68e6 | 4677 | drv->nlmode = nlmode; |
460456f8 JM |
4678 | ret = 0; |
4679 | goto done; | |
ad1e68e6 | 4680 | } |
3f5285e8 | 4681 | |
ebffdbc4 | 4682 | if (mode_switch_res == -ENODEV) |
671a5039 JM |
4683 | return -1; |
4684 | ||
460456f8 | 4685 | if (nlmode == drv->nlmode) { |
c6e8e8e4 JM |
4686 | wpa_printf(MSG_DEBUG, "nl80211: Interface already in " |
4687 | "requested mode - ignore error"); | |
460456f8 JM |
4688 | ret = 0; |
4689 | goto done; /* Already in the requested mode */ | |
4690 | } | |
3f5285e8 | 4691 | |
3f5285e8 JM |
4692 | /* mac80211 doesn't allow mode changes while the device is up, so |
4693 | * take the device down, try to set the mode again, and bring the | |
4694 | * device back up. | |
4695 | */ | |
26af9dca JM |
4696 | wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting " |
4697 | "interface down"); | |
4698 | for (i = 0; i < 10; i++) { | |
91724d6f | 4699 | res = i802_set_iface_flags(bss, 0); |
6e8183d7 JM |
4700 | if (res == -EACCES || res == -ENODEV) |
4701 | break; | |
ebffdbc4 | 4702 | if (res != 0) { |
26af9dca JM |
4703 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set " |
4704 | "interface down"); | |
ebffdbc4 CW |
4705 | os_sleep(0, 100000); |
4706 | continue; | |
4707 | } | |
3c5d34e3 CW |
4708 | |
4709 | /* | |
4710 | * Setting the mode will fail for some drivers if the phy is | |
4711 | * on a frequency that the mode is disallowed in. | |
4712 | */ | |
4713 | if (desired_freq_params) { | |
bdf5ccb2 | 4714 | res = nl80211_set_channel(bss, desired_freq_params, 0); |
3c5d34e3 CW |
4715 | if (res) { |
4716 | wpa_printf(MSG_DEBUG, | |
4717 | "nl80211: Failed to set frequency on interface"); | |
4718 | } | |
4719 | } | |
4720 | ||
ebffdbc4 CW |
4721 | /* Try to set the mode again while the interface is down */ |
4722 | mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode); | |
4723 | if (mode_switch_res == -EBUSY) { | |
4724 | wpa_printf(MSG_DEBUG, | |
4725 | "nl80211: Delaying mode set while interface going down"); | |
4726 | os_sleep(0, 100000); | |
4727 | continue; | |
4728 | } | |
4729 | ret = mode_switch_res; | |
4730 | break; | |
3f5285e8 JM |
4731 | } |
4732 | ||
c6e8e8e4 JM |
4733 | if (!ret) { |
4734 | wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while " | |
4735 | "interface is down"); | |
ad1e68e6 | 4736 | drv->nlmode = nlmode; |
7d9c3698 | 4737 | drv->ignore_if_down_event = 1; |
c6e8e8e4 | 4738 | } |
ad1e68e6 | 4739 | |
ebffdbc4 CW |
4740 | /* Bring the interface back up */ |
4741 | res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1); | |
4742 | if (res != 0) { | |
4743 | wpa_printf(MSG_DEBUG, | |
4744 | "nl80211: Failed to set interface up after switching mode"); | |
4745 | ret = -1; | |
4746 | } | |
4747 | ||
460456f8 | 4748 | done: |
3fd1cefb JB |
4749 | if (ret) { |
4750 | wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d " | |
4751 | "from %d failed", nlmode, drv->nlmode); | |
4752 | return ret; | |
4753 | } | |
4754 | ||
3e208481 JM |
4755 | if (is_p2p_net_interface(nlmode)) { |
4756 | wpa_printf(MSG_DEBUG, | |
4757 | "nl80211: Interface %s mode change to P2P - disable 11b rates", | |
4758 | bss->ifname); | |
edb9bfba | 4759 | nl80211_disable_11b_rates(drv, drv->ifindex, 1); |
3e208481 JM |
4760 | } else if (drv->disabled_11b_rates) { |
4761 | wpa_printf(MSG_DEBUG, | |
4762 | "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates", | |
4763 | bss->ifname); | |
1d0c6fb1 | 4764 | nl80211_disable_11b_rates(drv, drv->ifindex, 0); |
3e208481 | 4765 | } |
edb9bfba | 4766 | |
3fd1cefb | 4767 | if (is_ap_interface(nlmode)) { |
36488c05 | 4768 | nl80211_mgmt_unsubscribe(bss, "start AP"); |
460456f8 | 4769 | /* Setup additional AP mode functionality if needed */ |
3fd1cefb | 4770 | if (nl80211_setup_ap(bss)) |
460456f8 | 4771 | return -1; |
3fd1cefb | 4772 | } else if (was_ap) { |
460456f8 | 4773 | /* Remove additional AP mode functionality */ |
3fd1cefb | 4774 | nl80211_teardown_ap(bss); |
a11241fa | 4775 | } else { |
36488c05 | 4776 | nl80211_mgmt_unsubscribe(bss, "mode change"); |
460456f8 | 4777 | } |
460456f8 | 4778 | |
afb0550a BC |
4779 | if (is_mesh_interface(nlmode) && |
4780 | nl80211_mgmt_subscribe_mesh(bss)) | |
4781 | return -1; | |
4782 | ||
873d0fcf | 4783 | if (!bss->in_deinit && !is_ap_interface(nlmode) && |
afb0550a | 4784 | !is_mesh_interface(nlmode) && |
a11241fa JB |
4785 | nl80211_mgmt_subscribe_non_ap(bss) < 0) |
4786 | wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action " | |
4787 | "frame processing - ignore for now"); | |
08359050 | 4788 | |
3fd1cefb | 4789 | return 0; |
3f5285e8 JM |
4790 | } |
4791 | ||
4792 | ||
f3407c66 JM |
4793 | int wpa_driver_nl80211_set_mode(struct i802_bss *bss, |
4794 | enum nl80211_iftype nlmode) | |
3c5d34e3 CW |
4795 | { |
4796 | return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL); | |
4797 | } | |
4798 | ||
4799 | ||
4ec68377 JD |
4800 | static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss, |
4801 | struct hostapd_freq_params *freq) | |
3c5d34e3 | 4802 | { |
3c5d34e3 | 4803 | return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC, |
4ec68377 | 4804 | freq); |
3c5d34e3 | 4805 | } |
65d645ce AS |
4806 | |
4807 | ||
3f5285e8 JM |
4808 | static int wpa_driver_nl80211_get_capa(void *priv, |
4809 | struct wpa_driver_capa *capa) | |
4810 | { | |
a2e40bb6 FF |
4811 | struct i802_bss *bss = priv; |
4812 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
65d645ce | 4813 | |
3f5285e8 JM |
4814 | if (!drv->has_capability) |
4815 | return -1; | |
4816 | os_memcpy(capa, &drv->capa, sizeof(*capa)); | |
8cd6b7bc JB |
4817 | if (drv->extended_capa && drv->extended_capa_mask) { |
4818 | capa->extended_capa = drv->extended_capa; | |
4819 | capa->extended_capa_mask = drv->extended_capa_mask; | |
4820 | capa->extended_capa_len = drv->extended_capa_len; | |
4821 | } | |
851b0c55 | 4822 | |
906bc4c7 | 4823 | return 0; |
3f5285e8 JM |
4824 | } |
4825 | ||
4826 | ||
4827 | static int wpa_driver_nl80211_set_operstate(void *priv, int state) | |
4828 | { | |
a2e40bb6 FF |
4829 | struct i802_bss *bss = priv; |
4830 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3f5285e8 | 4831 | |
90a545cc JM |
4832 | wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)", |
4833 | bss->ifname, drv->operstate, state, | |
4834 | state ? "UP" : "DORMANT"); | |
3f5285e8 | 4835 | drv->operstate = state; |
36d84860 | 4836 | return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1, |
e2d02c29 | 4837 | state ? IF_OPER_UP : IF_OPER_DORMANT); |
3f5285e8 JM |
4838 | } |
4839 | ||
01652550 JM |
4840 | |
4841 | static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized) | |
4842 | { | |
a2e40bb6 FF |
4843 | struct i802_bss *bss = priv; |
4844 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
01652550 JM |
4845 | struct nl_msg *msg; |
4846 | struct nl80211_sta_flag_update upd; | |
a862e4a3 | 4847 | int ret; |
2eef5177 JM |
4848 | |
4849 | if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) { | |
4850 | wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated"); | |
4851 | return 0; | |
4852 | } | |
01652550 | 4853 | |
1ba51ec0 JM |
4854 | wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for " |
4855 | MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid)); | |
4856 | ||
01652550 JM |
4857 | os_memset(&upd, 0, sizeof(upd)); |
4858 | upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED); | |
4859 | if (authorized) | |
4860 | upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED); | |
a862e4a3 | 4861 | |
13f83980 | 4862 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) || |
a862e4a3 JM |
4863 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) || |
4864 | nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) { | |
4865 | nlmsg_free(msg); | |
4866 | return -ENOBUFS; | |
4867 | } | |
01652550 | 4868 | |
2eef5177 | 4869 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
2eef5177 JM |
4870 | if (!ret) |
4871 | return 0; | |
2eef5177 JM |
4872 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)", |
4873 | ret, strerror(-ret)); | |
4874 | return ret; | |
01652550 JM |
4875 | } |
4876 | ||
3f5285e8 | 4877 | |
f07ead6a JM |
4878 | /* Set kernel driver on given frequency (MHz) */ |
4879 | static int i802_set_freq(void *priv, struct hostapd_freq_params *freq) | |
c5121837 | 4880 | { |
f07ead6a | 4881 | struct i802_bss *bss = priv; |
13a524a3 | 4882 | return nl80211_set_channel(bss, freq, 0); |
c5121837 JM |
4883 | } |
4884 | ||
f7b3920c | 4885 | |
c5121837 JM |
4886 | static inline int min_int(int a, int b) |
4887 | { | |
4888 | if (a < b) | |
4889 | return a; | |
4890 | return b; | |
4891 | } | |
4892 | ||
4893 | ||
4894 | static int get_key_handler(struct nl_msg *msg, void *arg) | |
4895 | { | |
4896 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
4897 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
4898 | ||
4899 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
4900 | genlmsg_attrlen(gnlh, 0), NULL); | |
4901 | ||
4902 | /* | |
4903 | * TODO: validate the key index and mac address! | |
4904 | * Otherwise, there's a race condition as soon as | |
4905 | * the kernel starts sending key notifications. | |
4906 | */ | |
4907 | ||
4908 | if (tb[NL80211_ATTR_KEY_SEQ]) | |
4909 | memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]), | |
4910 | min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6)); | |
4911 | return NL_SKIP; | |
4912 | } | |
4913 | ||
4914 | ||
4915 | static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr, | |
4916 | int idx, u8 *seq) | |
4917 | { | |
a2e40bb6 FF |
4918 | struct i802_bss *bss = priv; |
4919 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4920 | struct nl_msg *msg; |
4921 | ||
95376e1a JM |
4922 | msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0, |
4923 | NL80211_CMD_GET_KEY); | |
4924 | if (!msg || | |
a862e4a3 | 4925 | (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) || |
95376e1a | 4926 | nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) { |
a862e4a3 JM |
4927 | nlmsg_free(msg); |
4928 | return -ENOBUFS; | |
4929 | } | |
c5121837 JM |
4930 | |
4931 | memset(seq, 0, 6); | |
4932 | ||
4933 | return send_and_recv_msgs(drv, msg, get_key_handler, seq); | |
c5121837 JM |
4934 | } |
4935 | ||
4936 | ||
c5121837 JM |
4937 | static int i802_set_rts(void *priv, int rts) |
4938 | { | |
a2e40bb6 FF |
4939 | struct i802_bss *bss = priv; |
4940 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
ad649451 | 4941 | struct nl_msg *msg; |
a862e4a3 | 4942 | int ret; |
ad649451 | 4943 | u32 val; |
c5121837 | 4944 | |
ad649451 JM |
4945 | if (rts >= 2347) |
4946 | val = (u32) -1; | |
4947 | else | |
4948 | val = rts; | |
c5121837 | 4949 | |
9725b784 | 4950 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) || |
a862e4a3 JM |
4951 | nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) { |
4952 | nlmsg_free(msg); | |
4953 | return -ENOBUFS; | |
4954 | } | |
ad649451 JM |
4955 | |
4956 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4957 | if (!ret) | |
4958 | return 0; | |
ad649451 JM |
4959 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: " |
4960 | "%d (%s)", rts, ret, strerror(-ret)); | |
4961 | return ret; | |
c5121837 JM |
4962 | } |
4963 | ||
4964 | ||
4965 | static int i802_set_frag(void *priv, int frag) | |
4966 | { | |
a2e40bb6 FF |
4967 | struct i802_bss *bss = priv; |
4968 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
ad649451 | 4969 | struct nl_msg *msg; |
a862e4a3 | 4970 | int ret; |
ad649451 | 4971 | u32 val; |
c5121837 | 4972 | |
ad649451 JM |
4973 | if (frag >= 2346) |
4974 | val = (u32) -1; | |
4975 | else | |
4976 | val = frag; | |
c5121837 | 4977 | |
9725b784 | 4978 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) || |
a862e4a3 JM |
4979 | nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) { |
4980 | nlmsg_free(msg); | |
4981 | return -ENOBUFS; | |
4982 | } | |
ad649451 JM |
4983 | |
4984 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4985 | if (!ret) | |
4986 | return 0; | |
ad649451 JM |
4987 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold " |
4988 | "%d: %d (%s)", frag, ret, strerror(-ret)); | |
4989 | return ret; | |
c5121837 JM |
4990 | } |
4991 | ||
4992 | ||
c5121837 JM |
4993 | static int i802_flush(void *priv) |
4994 | { | |
a2e40bb6 | 4995 | struct i802_bss *bss = priv; |
c5121837 | 4996 | struct nl_msg *msg; |
82554b10 | 4997 | int res; |
c5121837 | 4998 | |
83e7bb0e JM |
4999 | wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)", |
5000 | bss->ifname); | |
c5121837 JM |
5001 | |
5002 | /* | |
5003 | * XXX: FIX! this needs to flush all VLANs too | |
5004 | */ | |
13f83980 JM |
5005 | msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION); |
5006 | res = send_and_recv_msgs(bss->drv, msg, NULL, NULL); | |
82554b10 JM |
5007 | if (res) { |
5008 | wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d " | |
5009 | "(%s)", res, strerror(-res)); | |
5010 | } | |
5011 | return res; | |
c5121837 JM |
5012 | } |
5013 | ||
5014 | ||
5015 | static int get_sta_handler(struct nl_msg *msg, void *arg) | |
5016 | { | |
5017 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
5018 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
5019 | struct hostap_sta_driver_data *data = arg; | |
5020 | struct nlattr *stats[NL80211_STA_INFO_MAX + 1]; | |
5021 | static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { | |
5022 | [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, | |
5023 | [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, | |
5024 | [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, | |
5025 | [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, | |
5026 | [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, | |
dc7785f8 | 5027 | [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 }, |
c5121837 JM |
5028 | }; |
5029 | ||
5030 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
5031 | genlmsg_attrlen(gnlh, 0), NULL); | |
5032 | ||
5033 | /* | |
5034 | * TODO: validate the interface and mac address! | |
5035 | * Otherwise, there's a race condition as soon as | |
5036 | * the kernel starts sending station notifications. | |
5037 | */ | |
5038 | ||
5039 | if (!tb[NL80211_ATTR_STA_INFO]) { | |
5040 | wpa_printf(MSG_DEBUG, "sta stats missing!"); | |
5041 | return NL_SKIP; | |
5042 | } | |
5043 | if (nla_parse_nested(stats, NL80211_STA_INFO_MAX, | |
5044 | tb[NL80211_ATTR_STA_INFO], | |
5045 | stats_policy)) { | |
5046 | wpa_printf(MSG_DEBUG, "failed to parse nested attributes!"); | |
5047 | return NL_SKIP; | |
5048 | } | |
5049 | ||
5050 | if (stats[NL80211_STA_INFO_INACTIVE_TIME]) | |
5051 | data->inactive_msec = | |
5052 | nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]); | |
5053 | if (stats[NL80211_STA_INFO_RX_BYTES]) | |
5054 | data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]); | |
5055 | if (stats[NL80211_STA_INFO_TX_BYTES]) | |
5056 | data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]); | |
5057 | if (stats[NL80211_STA_INFO_RX_PACKETS]) | |
5058 | data->rx_packets = | |
5059 | nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]); | |
5060 | if (stats[NL80211_STA_INFO_TX_PACKETS]) | |
5061 | data->tx_packets = | |
5062 | nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]); | |
dc7785f8 YZ |
5063 | if (stats[NL80211_STA_INFO_TX_FAILED]) |
5064 | data->tx_retry_failed = | |
5065 | nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]); | |
c5121837 JM |
5066 | |
5067 | return NL_SKIP; | |
5068 | } | |
5069 | ||
9ebce9c5 JM |
5070 | static int i802_read_sta_data(struct i802_bss *bss, |
5071 | struct hostap_sta_driver_data *data, | |
c5121837 JM |
5072 | const u8 *addr) |
5073 | { | |
c5121837 JM |
5074 | struct nl_msg *msg; |
5075 | ||
5076 | os_memset(data, 0, sizeof(*data)); | |
c5121837 | 5077 | |
13f83980 JM |
5078 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) || |
5079 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) { | |
a862e4a3 JM |
5080 | nlmsg_free(msg); |
5081 | return -ENOBUFS; | |
5082 | } | |
c5121837 | 5083 | |
13f83980 | 5084 | return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data); |
c5121837 JM |
5085 | } |
5086 | ||
5087 | ||
c5121837 JM |
5088 | static int i802_set_tx_queue_params(void *priv, int queue, int aifs, |
5089 | int cw_min, int cw_max, int burst_time) | |
5090 | { | |
a2e40bb6 FF |
5091 | struct i802_bss *bss = priv; |
5092 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
5093 | struct nl_msg *msg; |
5094 | struct nlattr *txq, *params; | |
5095 | ||
13f83980 | 5096 | msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY); |
c5121837 JM |
5097 | if (!msg) |
5098 | return -1; | |
5099 | ||
c5121837 JM |
5100 | txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS); |
5101 | if (!txq) | |
a862e4a3 | 5102 | goto fail; |
c5121837 JM |
5103 | |
5104 | /* We are only sending parameters for a single TXQ at a time */ | |
5105 | params = nla_nest_start(msg, 1); | |
5106 | if (!params) | |
a862e4a3 | 5107 | goto fail; |
c5121837 | 5108 | |
7e3c1781 JM |
5109 | switch (queue) { |
5110 | case 0: | |
a862e4a3 JM |
5111 | if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO)) |
5112 | goto fail; | |
7e3c1781 JM |
5113 | break; |
5114 | case 1: | |
a862e4a3 JM |
5115 | if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI)) |
5116 | goto fail; | |
7e3c1781 JM |
5117 | break; |
5118 | case 2: | |
a862e4a3 JM |
5119 | if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE)) |
5120 | goto fail; | |
7e3c1781 JM |
5121 | break; |
5122 | case 3: | |
a862e4a3 JM |
5123 | if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK)) |
5124 | goto fail; | |
7e3c1781 JM |
5125 | break; |
5126 | } | |
c5121837 JM |
5127 | /* Burst time is configured in units of 0.1 msec and TXOP parameter in |
5128 | * 32 usec, so need to convert the value here. */ | |
a862e4a3 JM |
5129 | if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP, |
5130 | (burst_time * 100 + 16) / 32) || | |
5131 | nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) || | |
5132 | nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) || | |
5133 | nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs)) | |
5134 | goto fail; | |
c5121837 JM |
5135 | |
5136 | nla_nest_end(msg, params); | |
5137 | ||
5138 | nla_nest_end(msg, txq); | |
5139 | ||
5140 | if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) | |
5141 | return 0; | |
9e088e74 | 5142 | msg = NULL; |
a862e4a3 | 5143 | fail: |
9e088e74 | 5144 | nlmsg_free(msg); |
c5121837 JM |
5145 | return -1; |
5146 | } | |
5147 | ||
5148 | ||
9ebce9c5 | 5149 | static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr, |
c5121837 JM |
5150 | const char *ifname, int vlan_id) |
5151 | { | |
a2e40bb6 | 5152 | struct wpa_driver_nl80211_data *drv = bss->drv; |
c5121837 | 5153 | struct nl_msg *msg; |
a862e4a3 | 5154 | int ret; |
c5121837 | 5155 | |
bbc706a3 JM |
5156 | wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR |
5157 | ", ifname=%s[%d], vlan_id=%d)", | |
5158 | bss->ifname, if_nametoindex(bss->ifname), | |
5159 | MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id); | |
13f83980 | 5160 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) || |
a862e4a3 JM |
5161 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) || |
5162 | nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) { | |
5163 | nlmsg_free(msg); | |
5164 | return -ENOBUFS; | |
5165 | } | |
c5121837 | 5166 | |
cd1d72c1 JM |
5167 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
5168 | if (ret < 0) { | |
5169 | wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr=" | |
5170 | MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)", | |
5171 | MAC2STR(addr), ifname, vlan_id, ret, | |
5172 | strerror(-ret)); | |
5173 | } | |
cd1d72c1 | 5174 | return ret; |
c5121837 JM |
5175 | } |
5176 | ||
fbbfcbac | 5177 | |
c5121837 JM |
5178 | static int i802_get_inact_sec(void *priv, const u8 *addr) |
5179 | { | |
5180 | struct hostap_sta_driver_data data; | |
5181 | int ret; | |
5182 | ||
5183 | data.inactive_msec = (unsigned long) -1; | |
5184 | ret = i802_read_sta_data(priv, &data, addr); | |
5185 | if (ret || data.inactive_msec == (unsigned long) -1) | |
5186 | return -1; | |
5187 | return data.inactive_msec / 1000; | |
5188 | } | |
5189 | ||
5190 | ||
5191 | static int i802_sta_clear_stats(void *priv, const u8 *addr) | |
5192 | { | |
5193 | #if 0 | |
5194 | /* TODO */ | |
5195 | #endif | |
5196 | return 0; | |
5197 | } | |
5198 | ||
5199 | ||
731723a5 JM |
5200 | static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, |
5201 | int reason) | |
c5121837 | 5202 | { |
a2e40bb6 | 5203 | struct i802_bss *bss = priv; |
e1bd4e19 | 5204 | struct wpa_driver_nl80211_data *drv = bss->drv; |
c5121837 JM |
5205 | struct ieee80211_mgmt mgmt; |
5206 | ||
0d391b03 BC |
5207 | if (is_mesh_interface(drv->nlmode)) |
5208 | return -1; | |
5209 | ||
e1bd4e19 | 5210 | if (drv->device_ap_sme) |
59d7148a | 5211 | return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason); |
e1bd4e19 | 5212 | |
c5121837 JM |
5213 | memset(&mgmt, 0, sizeof(mgmt)); |
5214 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
5215 | WLAN_FC_STYPE_DEAUTH); | |
5216 | memcpy(mgmt.da, addr, ETH_ALEN); | |
731723a5 JM |
5217 | memcpy(mgmt.sa, own_addr, ETH_ALEN); |
5218 | memcpy(mgmt.bssid, own_addr, ETH_ALEN); | |
c5121837 | 5219 | mgmt.u.deauth.reason_code = host_to_le16(reason); |
a2e40bb6 | 5220 | return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, |
9f324b61 | 5221 | IEEE80211_HDRLEN + |
9ebce9c5 JM |
5222 | sizeof(mgmt.u.deauth), 0, 0, 0, 0, |
5223 | 0); | |
c5121837 JM |
5224 | } |
5225 | ||
5226 | ||
731723a5 JM |
5227 | static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, |
5228 | int reason) | |
c5121837 | 5229 | { |
a2e40bb6 | 5230 | struct i802_bss *bss = priv; |
e1bd4e19 | 5231 | struct wpa_driver_nl80211_data *drv = bss->drv; |
c5121837 JM |
5232 | struct ieee80211_mgmt mgmt; |
5233 | ||
0d391b03 BC |
5234 | if (is_mesh_interface(drv->nlmode)) |
5235 | return -1; | |
5236 | ||
e1bd4e19 | 5237 | if (drv->device_ap_sme) |
59d7148a | 5238 | return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason); |
e1bd4e19 | 5239 | |
c5121837 JM |
5240 | memset(&mgmt, 0, sizeof(mgmt)); |
5241 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
5242 | WLAN_FC_STYPE_DISASSOC); | |
5243 | memcpy(mgmt.da, addr, ETH_ALEN); | |
731723a5 JM |
5244 | memcpy(mgmt.sa, own_addr, ETH_ALEN); |
5245 | memcpy(mgmt.bssid, own_addr, ETH_ALEN); | |
c5121837 | 5246 | mgmt.u.disassoc.reason_code = host_to_le16(reason); |
a2e40bb6 | 5247 | return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, |
9f324b61 | 5248 | IEEE80211_HDRLEN + |
9ebce9c5 JM |
5249 | sizeof(mgmt.u.disassoc), 0, 0, 0, 0, |
5250 | 0); | |
c5121837 JM |
5251 | } |
5252 | ||
5253 | ||
9b4d9c8b JM |
5254 | static void dump_ifidx(struct wpa_driver_nl80211_data *drv) |
5255 | { | |
5256 | char buf[200], *pos, *end; | |
5257 | int i, res; | |
5258 | ||
5259 | pos = buf; | |
5260 | end = pos + sizeof(buf); | |
5261 | ||
5262 | for (i = 0; i < drv->num_if_indices; i++) { | |
5263 | if (!drv->if_indices[i]) | |
5264 | continue; | |
5265 | res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]); | |
d85e1fc8 | 5266 | if (os_snprintf_error(end - pos, res)) |
9b4d9c8b JM |
5267 | break; |
5268 | pos += res; | |
5269 | } | |
5270 | *pos = '\0'; | |
5271 | ||
5272 | wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s", | |
5273 | drv->num_if_indices, buf); | |
5274 | } | |
5275 | ||
5276 | ||
f07ead6a JM |
5277 | static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) |
5278 | { | |
5279 | int i; | |
5280 | int *old; | |
5281 | ||
5282 | wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d", | |
5283 | ifidx); | |
b36935be MB |
5284 | if (have_ifidx(drv, ifidx)) { |
5285 | wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list", | |
5286 | ifidx); | |
5287 | return; | |
5288 | } | |
f07ead6a JM |
5289 | for (i = 0; i < drv->num_if_indices; i++) { |
5290 | if (drv->if_indices[i] == 0) { | |
5291 | drv->if_indices[i] = ifidx; | |
9b4d9c8b | 5292 | dump_ifidx(drv); |
f07ead6a JM |
5293 | return; |
5294 | } | |
5295 | } | |
5296 | ||
5297 | if (drv->if_indices != drv->default_if_indices) | |
5298 | old = drv->if_indices; | |
5299 | else | |
5300 | old = NULL; | |
5301 | ||
067ffa26 JM |
5302 | drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1, |
5303 | sizeof(int)); | |
f07ead6a JM |
5304 | if (!drv->if_indices) { |
5305 | if (!old) | |
5306 | drv->if_indices = drv->default_if_indices; | |
5307 | else | |
5308 | drv->if_indices = old; | |
5309 | wpa_printf(MSG_ERROR, "Failed to reallocate memory for " | |
5310 | "interfaces"); | |
5311 | wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx); | |
5312 | return; | |
5313 | } else if (!old) | |
5314 | os_memcpy(drv->if_indices, drv->default_if_indices, | |
5315 | sizeof(drv->default_if_indices)); | |
5316 | drv->if_indices[drv->num_if_indices] = ifidx; | |
5317 | drv->num_if_indices++; | |
9b4d9c8b | 5318 | dump_ifidx(drv); |
f07ead6a JM |
5319 | } |
5320 | ||
5321 | ||
5322 | static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) | |
5323 | { | |
5324 | int i; | |
5325 | ||
5326 | for (i = 0; i < drv->num_if_indices; i++) { | |
5327 | if (drv->if_indices[i] == ifidx) { | |
5328 | drv->if_indices[i] = 0; | |
5329 | break; | |
5330 | } | |
5331 | } | |
9b4d9c8b | 5332 | dump_ifidx(drv); |
f07ead6a JM |
5333 | } |
5334 | ||
5335 | ||
5336 | static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) | |
5337 | { | |
5338 | int i; | |
5339 | ||
5340 | for (i = 0; i < drv->num_if_indices; i++) | |
5341 | if (drv->if_indices[i] == ifidx) | |
5342 | return 1; | |
5343 | ||
5344 | return 0; | |
5345 | } | |
5346 | ||
5347 | ||
5348 | static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val, | |
0e80ea2c | 5349 | const char *bridge_ifname, char *ifname_wds) |
f07ead6a JM |
5350 | { |
5351 | struct i802_bss *bss = priv; | |
5352 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
5353 | char name[IFNAMSIZ + 1]; | |
5354 | ||
5355 | os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid); | |
69dd2967 SM |
5356 | if (ifname_wds) |
5357 | os_strlcpy(ifname_wds, name, IFNAMSIZ + 1); | |
5358 | ||
f07ead6a JM |
5359 | wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR |
5360 | " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name); | |
5361 | if (val) { | |
5362 | if (!if_nametoindex(name)) { | |
5363 | if (nl80211_create_iface(drv, name, | |
5364 | NL80211_IFTYPE_AP_VLAN, | |
2aec4f3c JM |
5365 | bss->addr, 1, NULL, NULL, 0) < |
5366 | 0) | |
f07ead6a JM |
5367 | return -1; |
5368 | if (bridge_ifname && | |
c81eff1a BG |
5369 | linux_br_add_if(drv->global->ioctl_sock, |
5370 | bridge_ifname, name) < 0) | |
f07ead6a JM |
5371 | return -1; |
5372 | } | |
75227f3a JM |
5373 | if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) { |
5374 | wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA " | |
5375 | "interface %s up", name); | |
5376 | } | |
f07ead6a JM |
5377 | return i802_set_sta_vlan(priv, addr, name, 0); |
5378 | } else { | |
c34e618d FF |
5379 | if (bridge_ifname) |
5380 | linux_br_del_if(drv->global->ioctl_sock, bridge_ifname, | |
5381 | name); | |
5382 | ||
f07ead6a | 5383 | i802_set_sta_vlan(priv, addr, bss->ifname, 0); |
d0595b25 FF |
5384 | nl80211_remove_iface(drv, if_nametoindex(name)); |
5385 | return 0; | |
f07ead6a JM |
5386 | } |
5387 | } | |
5388 | ||
5389 | ||
5390 | static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx) | |
5391 | { | |
5392 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
5393 | struct sockaddr_ll lladdr; | |
5394 | unsigned char buf[3000]; | |
5395 | int len; | |
5396 | socklen_t fromlen = sizeof(lladdr); | |
5397 | ||
5398 | len = recvfrom(sock, buf, sizeof(buf), 0, | |
5399 | (struct sockaddr *)&lladdr, &fromlen); | |
5400 | if (len < 0) { | |
7ac3616d JM |
5401 | wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s", |
5402 | strerror(errno)); | |
f07ead6a JM |
5403 | return; |
5404 | } | |
5405 | ||
5406 | if (have_ifidx(drv, lladdr.sll_ifindex)) | |
5407 | drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len); | |
5408 | } | |
5409 | ||
5410 | ||
94627f6c | 5411 | static int i802_check_bridge(struct wpa_driver_nl80211_data *drv, |
e17a2477 | 5412 | struct i802_bss *bss, |
94627f6c JM |
5413 | const char *brname, const char *ifname) |
5414 | { | |
6c6678e7 | 5415 | int br_ifindex; |
94627f6c JM |
5416 | char in_br[IFNAMSIZ]; |
5417 | ||
e17a2477 | 5418 | os_strlcpy(bss->brname, brname, IFNAMSIZ); |
6c6678e7 | 5419 | br_ifindex = if_nametoindex(brname); |
6c6678e7 | 5420 | if (br_ifindex == 0) { |
94627f6c JM |
5421 | /* |
5422 | * Bridge was configured, but the bridge device does | |
5423 | * not exist. Try to add it now. | |
5424 | */ | |
c81eff1a | 5425 | if (linux_br_add(drv->global->ioctl_sock, brname) < 0) { |
94627f6c JM |
5426 | wpa_printf(MSG_ERROR, "nl80211: Failed to add the " |
5427 | "bridge interface %s: %s", | |
5428 | brname, strerror(errno)); | |
5429 | return -1; | |
5430 | } | |
e17a2477 | 5431 | bss->added_bridge = 1; |
8997613c JM |
5432 | br_ifindex = if_nametoindex(brname); |
5433 | add_ifidx(drv, br_ifindex); | |
94627f6c | 5434 | } |
8997613c | 5435 | bss->br_ifindex = br_ifindex; |
94627f6c JM |
5436 | |
5437 | if (linux_br_get(in_br, ifname) == 0) { | |
5438 | if (os_strcmp(in_br, brname) == 0) | |
5439 | return 0; /* already in the bridge */ | |
5440 | ||
5441 | wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from " | |
5442 | "bridge %s", ifname, in_br); | |
c81eff1a BG |
5443 | if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) < |
5444 | 0) { | |
94627f6c JM |
5445 | wpa_printf(MSG_ERROR, "nl80211: Failed to " |
5446 | "remove interface %s from bridge " | |
5447 | "%s: %s", | |
5448 | ifname, brname, strerror(errno)); | |
5449 | return -1; | |
5450 | } | |
5451 | } | |
5452 | ||
5453 | wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s", | |
5454 | ifname, brname); | |
c81eff1a | 5455 | if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) { |
94627f6c JM |
5456 | wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s " |
5457 | "into bridge %s: %s", | |
5458 | ifname, brname, strerror(errno)); | |
5459 | return -1; | |
5460 | } | |
e17a2477 | 5461 | bss->added_if_into_bridge = 1; |
94627f6c JM |
5462 | |
5463 | return 0; | |
5464 | } | |
5465 | ||
5466 | ||
92f475b4 JM |
5467 | static void *i802_init(struct hostapd_data *hapd, |
5468 | struct wpa_init_params *params) | |
c5121837 JM |
5469 | { |
5470 | struct wpa_driver_nl80211_data *drv; | |
a2e40bb6 | 5471 | struct i802_bss *bss; |
c5121837 | 5472 | size_t i; |
94627f6c JM |
5473 | char brname[IFNAMSIZ]; |
5474 | int ifindex, br_ifindex; | |
5475 | int br_added = 0; | |
c5121837 | 5476 | |
0d547d5f JM |
5477 | bss = wpa_driver_nl80211_drv_init(hapd, params->ifname, |
5478 | params->global_priv, 1, | |
0ecff8d7 | 5479 | params->bssid, params->driver_params); |
a2e40bb6 | 5480 | if (bss == NULL) |
c5121837 | 5481 | return NULL; |
c5121837 | 5482 | |
a2e40bb6 | 5483 | drv = bss->drv; |
7635bfb0 | 5484 | |
94627f6c JM |
5485 | if (linux_br_get(brname, params->ifname) == 0) { |
5486 | wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s", | |
5487 | params->ifname, brname); | |
5488 | br_ifindex = if_nametoindex(brname); | |
392dfd37 | 5489 | os_strlcpy(bss->brname, brname, IFNAMSIZ); |
94627f6c JM |
5490 | } else { |
5491 | brname[0] = '\0'; | |
5492 | br_ifindex = 0; | |
5493 | } | |
6c6678e7 | 5494 | bss->br_ifindex = br_ifindex; |
94627f6c | 5495 | |
92f475b4 | 5496 | for (i = 0; i < params->num_bridge; i++) { |
94627f6c JM |
5497 | if (params->bridge[i]) { |
5498 | ifindex = if_nametoindex(params->bridge[i]); | |
5499 | if (ifindex) | |
5500 | add_ifidx(drv, ifindex); | |
5501 | if (ifindex == br_ifindex) | |
5502 | br_added = 1; | |
5503 | } | |
c5121837 | 5504 | } |
c5121837 | 5505 | |
ad1e68e6 JM |
5506 | /* start listening for EAPOL on the default AP interface */ |
5507 | add_ifidx(drv, drv->ifindex); | |
5508 | ||
5ef8e39f JM |
5509 | if (params->num_bridge && params->bridge[0]) { |
5510 | if (i802_check_bridge(drv, bss, params->bridge[0], | |
5511 | params->ifname) < 0) | |
5512 | goto failed; | |
5513 | if (os_strcmp(params->bridge[0], brname) != 0) | |
5514 | br_added = 1; | |
5515 | } | |
5516 | ||
5517 | if (!br_added && br_ifindex && | |
5518 | (params->num_bridge == 0 || !params->bridge[0])) | |
5519 | add_ifidx(drv, br_ifindex); | |
94627f6c | 5520 | |
97ed9a06 KP |
5521 | #ifdef CONFIG_LIBNL3_ROUTE |
5522 | if (bss->added_if_into_bridge) { | |
5523 | drv->rtnl_sk = nl_socket_alloc(); | |
5524 | if (drv->rtnl_sk == NULL) { | |
5525 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock"); | |
5526 | goto failed; | |
5527 | } | |
5528 | ||
5529 | if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) { | |
5530 | wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s", | |
5531 | strerror(errno)); | |
5532 | goto failed; | |
5533 | } | |
5534 | } | |
5535 | #endif /* CONFIG_LIBNL3_ROUTE */ | |
5536 | ||
ad1e68e6 JM |
5537 | drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE)); |
5538 | if (drv->eapol_sock < 0) { | |
7ac3616d JM |
5539 | wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s", |
5540 | strerror(errno)); | |
bbaf0837 | 5541 | goto failed; |
ad1e68e6 JM |
5542 | } |
5543 | ||
5544 | if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL)) | |
5545 | { | |
7ac3616d | 5546 | wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol"); |
c5121837 | 5547 | goto failed; |
ad1e68e6 JM |
5548 | } |
5549 | ||
c81eff1a BG |
5550 | if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, |
5551 | params->own_addr)) | |
bbaf0837 | 5552 | goto failed; |
fee354c7 | 5553 | os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN); |
c5121837 | 5554 | |
341eebee JB |
5555 | memcpy(bss->addr, params->own_addr, ETH_ALEN); |
5556 | ||
a2e40bb6 | 5557 | return bss; |
c5121837 JM |
5558 | |
5559 | failed: | |
7635bfb0 | 5560 | wpa_driver_nl80211_deinit(bss); |
bbaf0837 JM |
5561 | return NULL; |
5562 | } | |
c5121837 | 5563 | |
c5121837 | 5564 | |
bbaf0837 JM |
5565 | static void i802_deinit(void *priv) |
5566 | { | |
9ebce9c5 JM |
5567 | struct i802_bss *bss = priv; |
5568 | wpa_driver_nl80211_deinit(bss); | |
c5121837 JM |
5569 | } |
5570 | ||
c5121837 | 5571 | |
22a7c9d7 JM |
5572 | static enum nl80211_iftype wpa_driver_nl80211_if_type( |
5573 | enum wpa_driver_if_type type) | |
5574 | { | |
5575 | switch (type) { | |
5576 | case WPA_IF_STATION: | |
9f51b113 | 5577 | return NL80211_IFTYPE_STATION; |
75bde05d JM |
5578 | case WPA_IF_P2P_CLIENT: |
5579 | case WPA_IF_P2P_GROUP: | |
9f51b113 | 5580 | return NL80211_IFTYPE_P2P_CLIENT; |
22a7c9d7 JM |
5581 | case WPA_IF_AP_VLAN: |
5582 | return NL80211_IFTYPE_AP_VLAN; | |
5583 | case WPA_IF_AP_BSS: | |
5584 | return NL80211_IFTYPE_AP; | |
9f51b113 JB |
5585 | case WPA_IF_P2P_GO: |
5586 | return NL80211_IFTYPE_P2P_GO; | |
7aad838c NS |
5587 | case WPA_IF_P2P_DEVICE: |
5588 | return NL80211_IFTYPE_P2P_DEVICE; | |
5b78493f MH |
5589 | case WPA_IF_MESH: |
5590 | return NL80211_IFTYPE_MESH_POINT; | |
22a7c9d7 JM |
5591 | } |
5592 | return -1; | |
5593 | } | |
5594 | ||
5595 | ||
5b78493f | 5596 | #if defined(CONFIG_P2P) || defined(CONFIG_MESH) |
482856c8 | 5597 | |
f2ed8023 JM |
5598 | static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr) |
5599 | { | |
5600 | struct wpa_driver_nl80211_data *drv; | |
5601 | dl_list_for_each(drv, &global->interfaces, | |
5602 | struct wpa_driver_nl80211_data, list) { | |
834ee56f | 5603 | if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0) |
f2ed8023 JM |
5604 | return 1; |
5605 | } | |
5606 | return 0; | |
5607 | } | |
5608 | ||
5609 | ||
5b78493f | 5610 | static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr) |
f2ed8023 JM |
5611 | { |
5612 | unsigned int idx; | |
5613 | ||
5614 | if (!drv->global) | |
5615 | return -1; | |
5616 | ||
834ee56f | 5617 | os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN); |
f2ed8023 | 5618 | for (idx = 0; idx < 64; idx++) { |
834ee56f | 5619 | new_addr[0] = drv->first_bss->addr[0] | 0x02; |
f2ed8023 JM |
5620 | new_addr[0] ^= idx << 2; |
5621 | if (!nl80211_addr_in_use(drv->global, new_addr)) | |
5622 | break; | |
5623 | } | |
5624 | if (idx == 64) | |
5625 | return -1; | |
5626 | ||
5b78493f | 5627 | wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address " |
f2ed8023 JM |
5628 | MACSTR, MAC2STR(new_addr)); |
5629 | ||
5630 | return 0; | |
5631 | } | |
5632 | ||
5b78493f | 5633 | #endif /* CONFIG_P2P || CONFIG_MESH */ |
482856c8 | 5634 | |
f2ed8023 | 5635 | |
f632e483 AS |
5636 | struct wdev_info { |
5637 | u64 wdev_id; | |
5638 | int wdev_id_set; | |
5639 | u8 macaddr[ETH_ALEN]; | |
5640 | }; | |
5641 | ||
5642 | static int nl80211_wdev_handler(struct nl_msg *msg, void *arg) | |
e472e1b4 AS |
5643 | { |
5644 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
5645 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
f632e483 | 5646 | struct wdev_info *wi = arg; |
e472e1b4 AS |
5647 | |
5648 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
5649 | genlmsg_attrlen(gnlh, 0), NULL); | |
5650 | if (tb[NL80211_ATTR_WDEV]) { | |
f632e483 AS |
5651 | wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]); |
5652 | wi->wdev_id_set = 1; | |
e472e1b4 AS |
5653 | } |
5654 | ||
5655 | if (tb[NL80211_ATTR_MAC]) | |
f632e483 | 5656 | os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]), |
e472e1b4 AS |
5657 | ETH_ALEN); |
5658 | ||
5659 | return NL_SKIP; | |
5660 | } | |
5661 | ||
5662 | ||
7ab68865 | 5663 | static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type, |
8043e725 | 5664 | const char *ifname, const u8 *addr, |
f3585c8a | 5665 | void *bss_ctx, void **drv_priv, |
e17a2477 | 5666 | char *force_ifname, u8 *if_addr, |
2aec4f3c | 5667 | const char *bridge, int use_existing) |
22a7c9d7 | 5668 | { |
e472e1b4 | 5669 | enum nl80211_iftype nlmode; |
a2e40bb6 FF |
5670 | struct i802_bss *bss = priv; |
5671 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
22a7c9d7 | 5672 | int ifidx; |
2aec4f3c | 5673 | int added = 1; |
22a7c9d7 | 5674 | |
f3585c8a JM |
5675 | if (addr) |
5676 | os_memcpy(if_addr, addr, ETH_ALEN); | |
e472e1b4 AS |
5677 | nlmode = wpa_driver_nl80211_if_type(type); |
5678 | if (nlmode == NL80211_IFTYPE_P2P_DEVICE) { | |
f632e483 AS |
5679 | struct wdev_info p2pdev_info; |
5680 | ||
5681 | os_memset(&p2pdev_info, 0, sizeof(p2pdev_info)); | |
e472e1b4 | 5682 | ifidx = nl80211_create_iface(drv, ifname, nlmode, addr, |
f632e483 | 5683 | 0, nl80211_wdev_handler, |
2aec4f3c | 5684 | &p2pdev_info, use_existing); |
f632e483 | 5685 | if (!p2pdev_info.wdev_id_set || ifidx != 0) { |
e472e1b4 AS |
5686 | wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s", |
5687 | ifname); | |
e472e1b4 AS |
5688 | return -1; |
5689 | } | |
f632e483 AS |
5690 | |
5691 | drv->global->if_add_wdevid = p2pdev_info.wdev_id; | |
5692 | drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set; | |
5693 | if (!is_zero_ether_addr(p2pdev_info.macaddr)) | |
5694 | os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN); | |
5695 | wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created", | |
5696 | ifname, | |
5697 | (long long unsigned int) p2pdev_info.wdev_id); | |
e472e1b4 AS |
5698 | } else { |
5699 | ifidx = nl80211_create_iface(drv, ifname, nlmode, addr, | |
2aec4f3c JM |
5700 | 0, NULL, NULL, use_existing); |
5701 | if (use_existing && ifidx == -ENFILE) { | |
5702 | added = 0; | |
5703 | ifidx = if_nametoindex(ifname); | |
5704 | } else if (ifidx < 0) { | |
e472e1b4 AS |
5705 | return -1; |
5706 | } | |
22a7c9d7 JM |
5707 | } |
5708 | ||
ab7a1add AS |
5709 | if (!addr) { |
5710 | if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) | |
5711 | os_memcpy(if_addr, bss->addr, ETH_ALEN); | |
5712 | else if (linux_get_ifhwaddr(drv->global->ioctl_sock, | |
5713 | bss->ifname, if_addr) < 0) { | |
2aec4f3c JM |
5714 | if (added) |
5715 | nl80211_remove_iface(drv, ifidx); | |
ab7a1add AS |
5716 | return -1; |
5717 | } | |
c55f774d JM |
5718 | } |
5719 | ||
5b78493f | 5720 | #if defined(CONFIG_P2P) || defined(CONFIG_MESH) |
c55f774d JM |
5721 | if (!addr && |
5722 | (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP || | |
5b78493f | 5723 | type == WPA_IF_P2P_GO || type == WPA_IF_MESH)) { |
c55f774d | 5724 | /* Enforce unique P2P Interface Address */ |
ab7a1add | 5725 | u8 new_addr[ETH_ALEN]; |
c55f774d | 5726 | |
ab7a1add | 5727 | if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname, |
c81eff1a | 5728 | new_addr) < 0) { |
ea39367c MK |
5729 | if (added) |
5730 | nl80211_remove_iface(drv, ifidx); | |
c55f774d JM |
5731 | return -1; |
5732 | } | |
f608081c | 5733 | if (nl80211_addr_in_use(drv->global, new_addr)) { |
c55f774d | 5734 | wpa_printf(MSG_DEBUG, "nl80211: Allocate new address " |
5b78493f MH |
5735 | "for %s interface", type == WPA_IF_MESH ? |
5736 | "mesh" : "P2P group"); | |
5737 | if (nl80211_vif_addr(drv, new_addr) < 0) { | |
ea39367c MK |
5738 | if (added) |
5739 | nl80211_remove_iface(drv, ifidx); | |
c55f774d JM |
5740 | return -1; |
5741 | } | |
c81eff1a | 5742 | if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, |
c55f774d | 5743 | new_addr) < 0) { |
ea39367c MK |
5744 | if (added) |
5745 | nl80211_remove_iface(drv, ifidx); | |
c55f774d JM |
5746 | return -1; |
5747 | } | |
c55f774d | 5748 | } |
f67eeb5c | 5749 | os_memcpy(if_addr, new_addr, ETH_ALEN); |
c55f774d | 5750 | } |
5b78493f | 5751 | #endif /* CONFIG_P2P || CONFIG_MESH */ |
f3585c8a | 5752 | |
22a7c9d7 | 5753 | if (type == WPA_IF_AP_BSS) { |
f5eb9da3 JM |
5754 | struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss)); |
5755 | if (new_bss == NULL) { | |
2aec4f3c JM |
5756 | if (added) |
5757 | nl80211_remove_iface(drv, ifidx); | |
f5eb9da3 JM |
5758 | return -1; |
5759 | } | |
5760 | ||
5761 | if (bridge && | |
5762 | i802_check_bridge(drv, new_bss, bridge, ifname) < 0) { | |
5763 | wpa_printf(MSG_ERROR, "nl80211: Failed to add the new " | |
5764 | "interface %s to a bridge %s", | |
5765 | ifname, bridge); | |
2aec4f3c JM |
5766 | if (added) |
5767 | nl80211_remove_iface(drv, ifidx); | |
f5eb9da3 JM |
5768 | os_free(new_bss); |
5769 | return -1; | |
5770 | } | |
5771 | ||
c81eff1a BG |
5772 | if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1)) |
5773 | { | |
ea39367c MK |
5774 | if (added) |
5775 | nl80211_remove_iface(drv, ifidx); | |
07179987 | 5776 | os_free(new_bss); |
22a7c9d7 JM |
5777 | return -1; |
5778 | } | |
a2e40bb6 | 5779 | os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ); |
341eebee | 5780 | os_memcpy(new_bss->addr, if_addr, ETH_ALEN); |
a2e40bb6 FF |
5781 | new_bss->ifindex = ifidx; |
5782 | new_bss->drv = drv; | |
834ee56f KP |
5783 | new_bss->next = drv->first_bss->next; |
5784 | new_bss->freq = drv->first_bss->freq; | |
a5e1eb20 | 5785 | new_bss->ctx = bss_ctx; |
2aec4f3c | 5786 | new_bss->added_if = added; |
834ee56f | 5787 | drv->first_bss->next = new_bss; |
a2e40bb6 FF |
5788 | if (drv_priv) |
5789 | *drv_priv = new_bss; | |
cc7a48d1 | 5790 | nl80211_init_bss(new_bss); |
3dd1d890 YAP |
5791 | |
5792 | /* Subscribe management frames for this WPA_IF_AP_BSS */ | |
5793 | if (nl80211_setup_ap(new_bss)) | |
5794 | return -1; | |
22a7c9d7 | 5795 | } |
22a7c9d7 | 5796 | |
ff6a158b JM |
5797 | if (drv->global) |
5798 | drv->global->if_add_ifindex = ifidx; | |
5799 | ||
d1bb7aed JJ |
5800 | /* |
5801 | * Some virtual interfaces need to process EAPOL packets and events on | |
5802 | * the parent interface. This is used mainly with hostapd. | |
5803 | */ | |
5804 | if (ifidx > 0 && | |
5805 | (drv->hostapd || | |
5806 | nlmode == NL80211_IFTYPE_AP_VLAN || | |
5807 | nlmode == NL80211_IFTYPE_WDS || | |
5808 | nlmode == NL80211_IFTYPE_MONITOR)) | |
b36935be MB |
5809 | add_ifidx(drv, ifidx); |
5810 | ||
22a7c9d7 JM |
5811 | return 0; |
5812 | } | |
5813 | ||
5814 | ||
9ebce9c5 | 5815 | static int wpa_driver_nl80211_if_remove(struct i802_bss *bss, |
22a7c9d7 JM |
5816 | enum wpa_driver_if_type type, |
5817 | const char *ifname) | |
5818 | { | |
a2e40bb6 | 5819 | struct wpa_driver_nl80211_data *drv = bss->drv; |
22a7c9d7 JM |
5820 | int ifindex = if_nametoindex(ifname); |
5821 | ||
2aec4f3c JM |
5822 | wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d", |
5823 | __func__, type, ifname, ifindex, bss->added_if); | |
158b090c | 5824 | if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex)) |
2b72df63 | 5825 | nl80211_remove_iface(drv, ifindex); |
de884303 JM |
5826 | else if (ifindex > 0 && !bss->added_if) { |
5827 | struct wpa_driver_nl80211_data *drv2; | |
5828 | dl_list_for_each(drv2, &drv->global->interfaces, | |
5829 | struct wpa_driver_nl80211_data, list) | |
5830 | del_ifidx(drv2, ifindex); | |
5831 | } | |
c34e618d | 5832 | |
c34e618d FF |
5833 | if (type != WPA_IF_AP_BSS) |
5834 | return 0; | |
5835 | ||
e17a2477 | 5836 | if (bss->added_if_into_bridge) { |
c81eff1a BG |
5837 | if (linux_br_del_if(drv->global->ioctl_sock, bss->brname, |
5838 | bss->ifname) < 0) | |
e17a2477 JM |
5839 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " |
5840 | "interface %s from bridge %s: %s", | |
5841 | bss->ifname, bss->brname, strerror(errno)); | |
5842 | } | |
5843 | if (bss->added_bridge) { | |
c81eff1a | 5844 | if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0) |
e17a2477 JM |
5845 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " |
5846 | "bridge %s: %s", | |
5847 | bss->brname, strerror(errno)); | |
5848 | } | |
a2e40bb6 | 5849 | |
834ee56f | 5850 | if (bss != drv->first_bss) { |
8546ea19 | 5851 | struct i802_bss *tbss; |
a2e40bb6 | 5852 | |
2aec4f3c | 5853 | wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it"); |
834ee56f | 5854 | for (tbss = drv->first_bss; tbss; tbss = tbss->next) { |
8546ea19 JM |
5855 | if (tbss->next == bss) { |
5856 | tbss->next = bss->next; | |
3dd1d890 YAP |
5857 | /* Unsubscribe management frames */ |
5858 | nl80211_teardown_ap(bss); | |
cc7a48d1 | 5859 | nl80211_destroy_bss(bss); |
5c9da160 MB |
5860 | if (!bss->added_if) |
5861 | i802_set_iface_flags(bss, 0); | |
8546ea19 JM |
5862 | os_free(bss); |
5863 | bss = NULL; | |
5864 | break; | |
5865 | } | |
22a7c9d7 | 5866 | } |
8546ea19 JM |
5867 | if (bss) |
5868 | wpa_printf(MSG_INFO, "nl80211: %s - could not find " | |
5869 | "BSS %p in the list", __func__, bss); | |
390e489c | 5870 | } else { |
2aec4f3c | 5871 | wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context"); |
390e489c | 5872 | nl80211_teardown_ap(bss); |
2aec4f3c JM |
5873 | if (!bss->added_if && !drv->first_bss->next) |
5874 | wpa_driver_nl80211_del_beacon(drv); | |
390e489c | 5875 | nl80211_destroy_bss(bss); |
2aec4f3c JM |
5876 | if (!bss->added_if) |
5877 | i802_set_iface_flags(bss, 0); | |
390e489c KP |
5878 | if (drv->first_bss->next) { |
5879 | drv->first_bss = drv->first_bss->next; | |
5880 | drv->ctx = drv->first_bss->ctx; | |
5881 | os_free(bss); | |
5882 | } else { | |
5883 | wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to"); | |
5884 | } | |
22a7c9d7 | 5885 | } |
22a7c9d7 JM |
5886 | |
5887 | return 0; | |
5888 | } | |
5889 | ||
5890 | ||
55777702 JM |
5891 | static int cookie_handler(struct nl_msg *msg, void *arg) |
5892 | { | |
5893 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
5894 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
5895 | u64 *cookie = arg; | |
5896 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
5897 | genlmsg_attrlen(gnlh, 0), NULL); | |
5898 | if (tb[NL80211_ATTR_COOKIE]) | |
5899 | *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); | |
5900 | return NL_SKIP; | |
5901 | } | |
5902 | ||
5903 | ||
88df0ef7 | 5904 | static int nl80211_send_frame_cmd(struct i802_bss *bss, |
5dfca53f JB |
5905 | unsigned int freq, unsigned int wait, |
5906 | const u8 *buf, size_t buf_len, | |
88df0ef7 JB |
5907 | u64 *cookie_out, int no_cck, int no_ack, |
5908 | int offchanok) | |
9884f9cc | 5909 | { |
88df0ef7 | 5910 | struct wpa_driver_nl80211_data *drv = bss->drv; |
9884f9cc JB |
5911 | struct nl_msg *msg; |
5912 | u64 cookie; | |
5913 | int ret = -1; | |
5914 | ||
cc2ada86 | 5915 | wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d " |
2e3e4566 JM |
5916 | "no_ack=%d offchanok=%d", |
5917 | freq, wait, no_cck, no_ack, offchanok); | |
c91f796f | 5918 | wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len); |
9884f9cc | 5919 | |
56f77852 | 5920 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) || |
a862e4a3 JM |
5921 | (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) || |
5922 | (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) || | |
5923 | (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) || | |
5924 | drv->test_use_roc_tx) && | |
5925 | nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) || | |
5926 | (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) || | |
5927 | (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) || | |
5928 | nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf)) | |
5929 | goto fail; | |
9884f9cc JB |
5930 | |
5931 | cookie = 0; | |
5932 | ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); | |
5933 | msg = NULL; | |
5934 | if (ret) { | |
5935 | wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d " | |
a05225c8 JM |
5936 | "(%s) (freq=%u wait=%u)", ret, strerror(-ret), |
5937 | freq, wait); | |
a862e4a3 JM |
5938 | } else { |
5939 | wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; " | |
5940 | "cookie 0x%llx", no_ack ? " (no ACK)" : "", | |
5941 | (long long unsigned int) cookie); | |
9884f9cc | 5942 | |
a862e4a3 JM |
5943 | if (cookie_out) |
5944 | *cookie_out = no_ack ? (u64) -1 : cookie; | |
5945 | } | |
9884f9cc | 5946 | |
a862e4a3 | 5947 | fail: |
9884f9cc JB |
5948 | nlmsg_free(msg); |
5949 | return ret; | |
5950 | } | |
5951 | ||
5952 | ||
9ebce9c5 JM |
5953 | static int wpa_driver_nl80211_send_action(struct i802_bss *bss, |
5954 | unsigned int freq, | |
190b9062 | 5955 | unsigned int wait_time, |
58f6fbe0 JM |
5956 | const u8 *dst, const u8 *src, |
5957 | const u8 *bssid, | |
b106173a JM |
5958 | const u8 *data, size_t data_len, |
5959 | int no_cck) | |
58f6fbe0 | 5960 | { |
a2e40bb6 | 5961 | struct wpa_driver_nl80211_data *drv = bss->drv; |
58f6fbe0 | 5962 | int ret = -1; |
58f6fbe0 JM |
5963 | u8 *buf; |
5964 | struct ieee80211_hdr *hdr; | |
58f6fbe0 | 5965 | |
5dfca53f | 5966 | wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, " |
55231068 JM |
5967 | "freq=%u MHz wait=%d ms no_cck=%d)", |
5968 | drv->ifindex, freq, wait_time, no_cck); | |
58f6fbe0 JM |
5969 | |
5970 | buf = os_zalloc(24 + data_len); | |
5971 | if (buf == NULL) | |
5972 | return ret; | |
5973 | os_memcpy(buf + 24, data, data_len); | |
5974 | hdr = (struct ieee80211_hdr *) buf; | |
5975 | hdr->frame_control = | |
5976 | IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION); | |
5977 | os_memcpy(hdr->addr1, dst, ETH_ALEN); | |
5978 | os_memcpy(hdr->addr2, src, ETH_ALEN); | |
5979 | os_memcpy(hdr->addr3, bssid, ETH_ALEN); | |
5980 | ||
f78f2785 JM |
5981 | if (is_ap_interface(drv->nlmode) && |
5982 | (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) || | |
5983 | (int) freq == bss->freq || drv->device_ap_sme || | |
5984 | !drv->use_monitor)) | |
9ebce9c5 JM |
5985 | ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len, |
5986 | 0, freq, no_cck, 1, | |
5987 | wait_time); | |
9884f9cc | 5988 | else |
88df0ef7 | 5989 | ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf, |
5dfca53f | 5990 | 24 + data_len, |
b106173a | 5991 | &drv->send_action_cookie, |
88df0ef7 | 5992 | no_cck, 0, 1); |
58f6fbe0 | 5993 | |
f8bf1421 | 5994 | os_free(buf); |
58f6fbe0 JM |
5995 | return ret; |
5996 | } | |
5997 | ||
5998 | ||
5dfca53f JB |
5999 | static void wpa_driver_nl80211_send_action_cancel_wait(void *priv) |
6000 | { | |
6001 | struct i802_bss *bss = priv; | |
6002 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6003 | struct nl_msg *msg; | |
6004 | int ret; | |
6005 | ||
316a9e4d JM |
6006 | wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx", |
6007 | (long long unsigned int) drv->send_action_cookie); | |
56f77852 | 6008 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) || |
a862e4a3 JM |
6009 | nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) { |
6010 | nlmsg_free(msg); | |
6011 | return; | |
6012 | } | |
5dfca53f JB |
6013 | |
6014 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
5dfca53f JB |
6015 | if (ret) |
6016 | wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d " | |
6017 | "(%s)", ret, strerror(-ret)); | |
5dfca53f JB |
6018 | } |
6019 | ||
6020 | ||
55777702 JM |
6021 | static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq, |
6022 | unsigned int duration) | |
6023 | { | |
a2e40bb6 FF |
6024 | struct i802_bss *bss = priv; |
6025 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
55777702 JM |
6026 | struct nl_msg *msg; |
6027 | int ret; | |
6028 | u64 cookie; | |
6029 | ||
56f77852 | 6030 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) || |
a862e4a3 JM |
6031 | nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) || |
6032 | nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) { | |
6033 | nlmsg_free(msg); | |
6034 | return -1; | |
6035 | } | |
55777702 JM |
6036 | |
6037 | cookie = 0; | |
6038 | ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); | |
6039 | if (ret == 0) { | |
6040 | wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie " | |
6041 | "0x%llx for freq=%u MHz duration=%u", | |
6042 | (long long unsigned int) cookie, freq, duration); | |
6043 | drv->remain_on_chan_cookie = cookie; | |
531f0331 | 6044 | drv->pending_remain_on_chan = 1; |
55777702 JM |
6045 | return 0; |
6046 | } | |
6047 | wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel " | |
15ed5535 JM |
6048 | "(freq=%d duration=%u): %d (%s)", |
6049 | freq, duration, ret, strerror(-ret)); | |
55777702 JM |
6050 | return -1; |
6051 | } | |
6052 | ||
6053 | ||
6054 | static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv) | |
6055 | { | |
a2e40bb6 FF |
6056 | struct i802_bss *bss = priv; |
6057 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
55777702 JM |
6058 | struct nl_msg *msg; |
6059 | int ret; | |
6060 | ||
6061 | if (!drv->pending_remain_on_chan) { | |
6062 | wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel " | |
6063 | "to cancel"); | |
6064 | return -1; | |
6065 | } | |
6066 | ||
6067 | wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie " | |
6068 | "0x%llx", | |
6069 | (long long unsigned int) drv->remain_on_chan_cookie); | |
6070 | ||
56f77852 JM |
6071 | msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL); |
6072 | if (!msg || | |
a862e4a3 JM |
6073 | nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) { |
6074 | nlmsg_free(msg); | |
6075 | return -1; | |
6076 | } | |
55777702 JM |
6077 | |
6078 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
6079 | if (ret == 0) | |
6080 | return 0; | |
6081 | wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: " | |
6082 | "%d (%s)", ret, strerror(-ret)); | |
55777702 JM |
6083 | return -1; |
6084 | } | |
6085 | ||
6086 | ||
9ebce9c5 | 6087 | static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report) |
504e905c | 6088 | { |
a2e40bb6 | 6089 | struct wpa_driver_nl80211_data *drv = bss->drv; |
504e905c | 6090 | |
5582a5d1 | 6091 | if (!report) { |
0d891981 | 6092 | if (bss->nl_preq && drv->device_ap_sme && |
b8d87ed2 AP |
6093 | is_ap_interface(drv->nlmode) && !bss->in_deinit && |
6094 | !bss->static_ap) { | |
0d891981 JM |
6095 | /* |
6096 | * Do not disable Probe Request reporting that was | |
6097 | * enabled in nl80211_setup_ap(). | |
6098 | */ | |
6099 | wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of " | |
6100 | "Probe Request reporting nl_preq=%p while " | |
6101 | "in AP mode", bss->nl_preq); | |
6102 | } else if (bss->nl_preq) { | |
36488c05 JM |
6103 | wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request " |
6104 | "reporting nl_preq=%p", bss->nl_preq); | |
5f65e9f7 | 6105 | nl80211_destroy_eloop_handle(&bss->nl_preq); |
5582a5d1 JB |
6106 | } |
6107 | return 0; | |
6108 | } | |
6109 | ||
481234cf | 6110 | if (bss->nl_preq) { |
5582a5d1 | 6111 | wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting " |
36488c05 | 6112 | "already on! nl_preq=%p", bss->nl_preq); |
5582a5d1 JB |
6113 | return 0; |
6114 | } | |
6115 | ||
481234cf JM |
6116 | bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq"); |
6117 | if (bss->nl_preq == NULL) | |
5582a5d1 | 6118 | return -1; |
36488c05 JM |
6119 | wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request " |
6120 | "reporting nl_preq=%p", bss->nl_preq); | |
5582a5d1 | 6121 | |
481234cf | 6122 | if (nl80211_register_frame(bss, bss->nl_preq, |
5582a5d1 JB |
6123 | (WLAN_FC_TYPE_MGMT << 2) | |
6124 | (WLAN_FC_STYPE_PROBE_REQ << 4), | |
a92dfde8 JB |
6125 | NULL, 0) < 0) |
6126 | goto out_err; | |
5582a5d1 | 6127 | |
5f65e9f7 JB |
6128 | nl80211_register_eloop_read(&bss->nl_preq, |
6129 | wpa_driver_nl80211_event_receive, | |
6130 | bss->nl_cb); | |
5582a5d1 | 6131 | |
504e905c | 6132 | return 0; |
5582a5d1 | 6133 | |
a92dfde8 | 6134 | out_err: |
221a59c9 | 6135 | nl_destroy_handles(&bss->nl_preq); |
5582a5d1 | 6136 | return -1; |
504e905c JM |
6137 | } |
6138 | ||
6139 | ||
4e5cb1a3 JM |
6140 | static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, |
6141 | int ifindex, int disabled) | |
6142 | { | |
6143 | struct nl_msg *msg; | |
6144 | struct nlattr *bands, *band; | |
6145 | int ret; | |
6146 | ||
3e208481 JM |
6147 | wpa_printf(MSG_DEBUG, |
6148 | "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)", | |
6149 | ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" : | |
6150 | "no NL80211_TXRATE_LEGACY constraint"); | |
6151 | ||
95376e1a JM |
6152 | msg = nl80211_ifindex_msg(drv, ifindex, 0, |
6153 | NL80211_CMD_SET_TX_BITRATE_MASK); | |
4e5cb1a3 JM |
6154 | if (!msg) |
6155 | return -1; | |
6156 | ||
4e5cb1a3 JM |
6157 | bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES); |
6158 | if (!bands) | |
a862e4a3 | 6159 | goto fail; |
4e5cb1a3 JM |
6160 | |
6161 | /* | |
6162 | * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything | |
6163 | * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS | |
6164 | * rates. All 5 GHz rates are left enabled. | |
6165 | */ | |
6166 | band = nla_nest_start(msg, NL80211_BAND_2GHZ); | |
a862e4a3 JM |
6167 | if (!band || |
6168 | (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8, | |
6169 | "\x0c\x12\x18\x24\x30\x48\x60\x6c"))) | |
6170 | goto fail; | |
4e5cb1a3 JM |
6171 | nla_nest_end(msg, band); |
6172 | ||
6173 | nla_nest_end(msg, bands); | |
6174 | ||
6175 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4e5cb1a3 JM |
6176 | if (ret) { |
6177 | wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d " | |
6178 | "(%s)", ret, strerror(-ret)); | |
1d0c6fb1 JM |
6179 | } else |
6180 | drv->disabled_11b_rates = disabled; | |
4e5cb1a3 JM |
6181 | |
6182 | return ret; | |
6183 | ||
a862e4a3 | 6184 | fail: |
4e5cb1a3 JM |
6185 | nlmsg_free(msg); |
6186 | return -1; | |
6187 | } | |
6188 | ||
6189 | ||
af473088 JM |
6190 | static int wpa_driver_nl80211_deinit_ap(void *priv) |
6191 | { | |
a2e40bb6 FF |
6192 | struct i802_bss *bss = priv; |
6193 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
b1f625e0 | 6194 | if (!is_ap_interface(drv->nlmode)) |
af473088 JM |
6195 | return -1; |
6196 | wpa_driver_nl80211_del_beacon(drv); | |
9bc5cfa3 | 6197 | bss->beacon_set = 0; |
60b13c20 IP |
6198 | |
6199 | /* | |
6200 | * If the P2P GO interface was dynamically added, then it is | |
6201 | * possible that the interface change to station is not possible. | |
6202 | */ | |
6203 | if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic) | |
6204 | return 0; | |
6205 | ||
b1f625e0 | 6206 | return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); |
af473088 JM |
6207 | } |
6208 | ||
6209 | ||
695c7038 SW |
6210 | static int wpa_driver_nl80211_stop_ap(void *priv) |
6211 | { | |
6212 | struct i802_bss *bss = priv; | |
6213 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6214 | if (!is_ap_interface(drv->nlmode)) | |
6215 | return -1; | |
6216 | wpa_driver_nl80211_del_beacon(drv); | |
6217 | bss->beacon_set = 0; | |
6218 | return 0; | |
6219 | } | |
6220 | ||
6221 | ||
3c29244e EP |
6222 | static int wpa_driver_nl80211_deinit_p2p_cli(void *priv) |
6223 | { | |
6224 | struct i802_bss *bss = priv; | |
6225 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6226 | if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT) | |
6227 | return -1; | |
60b13c20 IP |
6228 | |
6229 | /* | |
6230 | * If the P2P Client interface was dynamically added, then it is | |
6231 | * possible that the interface change to station is not possible. | |
6232 | */ | |
6233 | if (bss->if_dynamic) | |
6234 | return 0; | |
6235 | ||
3c29244e EP |
6236 | return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION); |
6237 | } | |
6238 | ||
6239 | ||
207ef3fb JM |
6240 | static void wpa_driver_nl80211_resume(void *priv) |
6241 | { | |
a2e40bb6 | 6242 | struct i802_bss *bss = priv; |
91724d6f AS |
6243 | |
6244 | if (i802_set_iface_flags(bss, 1)) | |
6245 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event"); | |
207ef3fb JM |
6246 | } |
6247 | ||
6248 | ||
b625473c JM |
6249 | static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) |
6250 | { | |
6251 | struct i802_bss *bss = priv; | |
6252 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
8970bae8 JB |
6253 | struct nl_msg *msg; |
6254 | struct nlattr *cqm; | |
b625473c JM |
6255 | |
6256 | wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " | |
6257 | "hysteresis=%d", threshold, hysteresis); | |
6258 | ||
13f83980 | 6259 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) || |
a862e4a3 JM |
6260 | !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) || |
6261 | nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) || | |
6262 | nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) { | |
6263 | nlmsg_free(msg); | |
6264 | return -1; | |
6265 | } | |
8970bae8 | 6266 | nla_nest_end(msg, cqm); |
21270bb4 | 6267 | |
a862e4a3 | 6268 | return send_and_recv_msgs(drv, msg, NULL, NULL); |
b625473c JM |
6269 | } |
6270 | ||
6271 | ||
2cc8d8f4 AO |
6272 | static int get_channel_width(struct nl_msg *msg, void *arg) |
6273 | { | |
6274 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
6275 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
6276 | struct wpa_signal_info *sig_change = arg; | |
6277 | ||
6278 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
6279 | genlmsg_attrlen(gnlh, 0), NULL); | |
6280 | ||
6281 | sig_change->center_frq1 = -1; | |
6282 | sig_change->center_frq2 = -1; | |
6283 | sig_change->chanwidth = CHAN_WIDTH_UNKNOWN; | |
6284 | ||
6285 | if (tb[NL80211_ATTR_CHANNEL_WIDTH]) { | |
6286 | sig_change->chanwidth = convert2width( | |
6287 | nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH])); | |
6288 | if (tb[NL80211_ATTR_CENTER_FREQ1]) | |
6289 | sig_change->center_frq1 = | |
6290 | nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]); | |
6291 | if (tb[NL80211_ATTR_CENTER_FREQ2]) | |
6292 | sig_change->center_frq2 = | |
6293 | nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]); | |
6294 | } | |
6295 | ||
6296 | return NL_SKIP; | |
6297 | } | |
6298 | ||
6299 | ||
6300 | static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv, | |
6301 | struct wpa_signal_info *sig) | |
6302 | { | |
6303 | struct nl_msg *msg; | |
6304 | ||
9725b784 | 6305 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE); |
2cc8d8f4 | 6306 | return send_and_recv_msgs(drv, msg, get_channel_width, sig); |
2cc8d8f4 AO |
6307 | } |
6308 | ||
6309 | ||
1c5c7273 PS |
6310 | static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si) |
6311 | { | |
6312 | struct i802_bss *bss = priv; | |
6313 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6314 | int res; | |
6315 | ||
6316 | os_memset(si, 0, sizeof(*si)); | |
6317 | res = nl80211_get_link_signal(drv, si); | |
6318 | if (res != 0) | |
6319 | return res; | |
6320 | ||
2cc8d8f4 AO |
6321 | res = nl80211_get_channel_width(drv, si); |
6322 | if (res != 0) | |
6323 | return res; | |
6324 | ||
1c5c7273 PS |
6325 | return nl80211_get_link_noise(drv, si); |
6326 | } | |
6327 | ||
6328 | ||
57ebba59 JJ |
6329 | static int wpa_driver_nl80211_shared_freq(void *priv) |
6330 | { | |
6331 | struct i802_bss *bss = priv; | |
6332 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6333 | struct wpa_driver_nl80211_data *driver; | |
6334 | int freq = 0; | |
6335 | ||
6336 | /* | |
6337 | * If the same PHY is in connected state with some other interface, | |
6338 | * then retrieve the assoc freq. | |
6339 | */ | |
6340 | wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s", | |
6341 | drv->phyname); | |
6342 | ||
6343 | dl_list_for_each(driver, &drv->global->interfaces, | |
6344 | struct wpa_driver_nl80211_data, list) { | |
6345 | if (drv == driver || | |
6346 | os_strcmp(drv->phyname, driver->phyname) != 0 || | |
6347 | !driver->associated) | |
6348 | continue; | |
6349 | ||
6350 | wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s " | |
6351 | MACSTR, | |
834ee56f KP |
6352 | driver->phyname, driver->first_bss->ifname, |
6353 | MAC2STR(driver->first_bss->addr)); | |
d3bd0f05 | 6354 | if (is_ap_interface(driver->nlmode)) |
834ee56f | 6355 | freq = driver->first_bss->freq; |
d3bd0f05 JJ |
6356 | else |
6357 | freq = nl80211_get_assoc_freq(driver); | |
57ebba59 JJ |
6358 | wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d", |
6359 | drv->phyname, freq); | |
6360 | } | |
6361 | ||
6362 | if (!freq) | |
6363 | wpa_printf(MSG_DEBUG, "nl80211: No shared interface for " | |
6364 | "PHY (%s) in associated state", drv->phyname); | |
6365 | ||
6366 | return freq; | |
6367 | } | |
6368 | ||
6369 | ||
b91ab76e JM |
6370 | static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len, |
6371 | int encrypt) | |
6372 | { | |
6373 | struct i802_bss *bss = priv; | |
55231068 JM |
6374 | return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0, |
6375 | 0, 0, 0, 0); | |
b91ab76e JM |
6376 | } |
6377 | ||
6378 | ||
c55f774d JM |
6379 | static int nl80211_set_param(void *priv, const char *param) |
6380 | { | |
c55f774d JM |
6381 | wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param); |
6382 | if (param == NULL) | |
6383 | return 0; | |
6384 | ||
6385 | #ifdef CONFIG_P2P | |
6386 | if (os_strstr(param, "use_p2p_group_interface=1")) { | |
482856c8 JM |
6387 | struct i802_bss *bss = priv; |
6388 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6389 | ||
c55f774d JM |
6390 | wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group " |
6391 | "interface"); | |
6392 | drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT; | |
6393 | drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P; | |
6394 | } | |
6395 | #endif /* CONFIG_P2P */ | |
6396 | ||
327b01d3 JM |
6397 | if (os_strstr(param, "use_monitor=1")) { |
6398 | struct i802_bss *bss = priv; | |
6399 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6400 | drv->use_monitor = 1; | |
6401 | } | |
6402 | ||
6403 | if (os_strstr(param, "force_connect_cmd=1")) { | |
6404 | struct i802_bss *bss = priv; | |
6405 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6406 | drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME; | |
b497a212 | 6407 | drv->force_connect_cmd = 1; |
327b01d3 JM |
6408 | } |
6409 | ||
64abb725 JM |
6410 | if (os_strstr(param, "no_offchannel_tx=1")) { |
6411 | struct i802_bss *bss = priv; | |
6412 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6413 | drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX; | |
6414 | drv->test_use_roc_tx = 1; | |
6415 | } | |
6416 | ||
c55f774d JM |
6417 | return 0; |
6418 | } | |
6419 | ||
6420 | ||
f2ed8023 JM |
6421 | static void * nl80211_global_init(void) |
6422 | { | |
6423 | struct nl80211_global *global; | |
36d84860 BG |
6424 | struct netlink_config *cfg; |
6425 | ||
f2ed8023 JM |
6426 | global = os_zalloc(sizeof(*global)); |
6427 | if (global == NULL) | |
6428 | return NULL; | |
c81eff1a | 6429 | global->ioctl_sock = -1; |
f2ed8023 | 6430 | dl_list_init(&global->interfaces); |
ff6a158b | 6431 | global->if_add_ifindex = -1; |
36d84860 BG |
6432 | |
6433 | cfg = os_zalloc(sizeof(*cfg)); | |
6434 | if (cfg == NULL) | |
6435 | goto err; | |
6436 | ||
6437 | cfg->ctx = global; | |
6438 | cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink; | |
6439 | cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink; | |
6440 | global->netlink = netlink_init(cfg); | |
6441 | if (global->netlink == NULL) { | |
6442 | os_free(cfg); | |
6443 | goto err; | |
6444 | } | |
6445 | ||
2a7b66f5 BG |
6446 | if (wpa_driver_nl80211_init_nl_global(global) < 0) |
6447 | goto err; | |
6448 | ||
c81eff1a BG |
6449 | global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); |
6450 | if (global->ioctl_sock < 0) { | |
7ac3616d JM |
6451 | wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s", |
6452 | strerror(errno)); | |
c81eff1a BG |
6453 | goto err; |
6454 | } | |
6455 | ||
f2ed8023 | 6456 | return global; |
36d84860 BG |
6457 | |
6458 | err: | |
6459 | nl80211_global_deinit(global); | |
6460 | return NULL; | |
f2ed8023 JM |
6461 | } |
6462 | ||
6463 | ||
6464 | static void nl80211_global_deinit(void *priv) | |
6465 | { | |
6466 | struct nl80211_global *global = priv; | |
6467 | if (global == NULL) | |
6468 | return; | |
6469 | if (!dl_list_empty(&global->interfaces)) { | |
6470 | wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at " | |
6471 | "nl80211_global_deinit", | |
6472 | dl_list_len(&global->interfaces)); | |
6473 | } | |
36d84860 BG |
6474 | |
6475 | if (global->netlink) | |
6476 | netlink_deinit(global->netlink); | |
6477 | ||
276e2d67 BG |
6478 | nl_destroy_handles(&global->nl); |
6479 | ||
5f65e9f7 JB |
6480 | if (global->nl_event) |
6481 | nl80211_destroy_eloop_handle(&global->nl_event); | |
d6c9aab8 JB |
6482 | |
6483 | nl_cb_put(global->nl_cb); | |
2a7b66f5 | 6484 | |
c81eff1a BG |
6485 | if (global->ioctl_sock >= 0) |
6486 | close(global->ioctl_sock); | |
6487 | ||
f2ed8023 JM |
6488 | os_free(global); |
6489 | } | |
6490 | ||
6491 | ||
6859f1cb BG |
6492 | static const char * nl80211_get_radio_name(void *priv) |
6493 | { | |
6494 | struct i802_bss *bss = priv; | |
6495 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6496 | return drv->phyname; | |
6497 | } | |
6498 | ||
6499 | ||
a6efc65d JM |
6500 | static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid, |
6501 | const u8 *pmkid) | |
6502 | { | |
6503 | struct nl_msg *msg; | |
6504 | ||
13f83980 | 6505 | if (!(msg = nl80211_bss_msg(bss, 0, cmd)) || |
a862e4a3 JM |
6506 | (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) || |
6507 | (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) { | |
6508 | nlmsg_free(msg); | |
6509 | return -ENOBUFS; | |
6510 | } | |
a6efc65d JM |
6511 | |
6512 | return send_and_recv_msgs(bss->drv, msg, NULL, NULL); | |
a6efc65d JM |
6513 | } |
6514 | ||
6515 | ||
6516 | static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) | |
6517 | { | |
6518 | struct i802_bss *bss = priv; | |
6519 | wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid)); | |
6520 | return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid); | |
6521 | } | |
6522 | ||
6523 | ||
6524 | static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid) | |
6525 | { | |
6526 | struct i802_bss *bss = priv; | |
6527 | wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR, | |
6528 | MAC2STR(bssid)); | |
6529 | return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid); | |
6530 | } | |
6531 | ||
6532 | ||
6533 | static int nl80211_flush_pmkid(void *priv) | |
6534 | { | |
6535 | struct i802_bss *bss = priv; | |
6536 | wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs"); | |
6537 | return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL); | |
6538 | } | |
6539 | ||
6540 | ||
0185007c MK |
6541 | static void clean_survey_results(struct survey_results *survey_results) |
6542 | { | |
6543 | struct freq_survey *survey, *tmp; | |
6544 | ||
6545 | if (dl_list_empty(&survey_results->survey_list)) | |
6546 | return; | |
6547 | ||
6548 | dl_list_for_each_safe(survey, tmp, &survey_results->survey_list, | |
6549 | struct freq_survey, list) { | |
6550 | dl_list_del(&survey->list); | |
6551 | os_free(survey); | |
6552 | } | |
6553 | } | |
6554 | ||
6555 | ||
6556 | static void add_survey(struct nlattr **sinfo, u32 ifidx, | |
6557 | struct dl_list *survey_list) | |
6558 | { | |
6559 | struct freq_survey *survey; | |
6560 | ||
6561 | survey = os_zalloc(sizeof(struct freq_survey)); | |
6562 | if (!survey) | |
6563 | return; | |
6564 | ||
6565 | survey->ifidx = ifidx; | |
6566 | survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); | |
6567 | survey->filled = 0; | |
6568 | ||
6569 | if (sinfo[NL80211_SURVEY_INFO_NOISE]) { | |
6570 | survey->nf = (int8_t) | |
6571 | nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]); | |
6572 | survey->filled |= SURVEY_HAS_NF; | |
6573 | } | |
6574 | ||
6575 | if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) { | |
6576 | survey->channel_time = | |
6577 | nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]); | |
6578 | survey->filled |= SURVEY_HAS_CHAN_TIME; | |
6579 | } | |
6580 | ||
6581 | if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) { | |
6582 | survey->channel_time_busy = | |
6583 | nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]); | |
6584 | survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY; | |
6585 | } | |
6586 | ||
6587 | if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) { | |
6588 | survey->channel_time_rx = | |
6589 | nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]); | |
6590 | survey->filled |= SURVEY_HAS_CHAN_TIME_RX; | |
6591 | } | |
6592 | ||
6593 | if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) { | |
6594 | survey->channel_time_tx = | |
6595 | nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]); | |
6596 | survey->filled |= SURVEY_HAS_CHAN_TIME_TX; | |
6597 | } | |
6598 | ||
6599 | wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)", | |
6600 | survey->freq, | |
6601 | survey->nf, | |
6602 | (unsigned long int) survey->channel_time, | |
6603 | (unsigned long int) survey->channel_time_busy, | |
6604 | (unsigned long int) survey->channel_time_tx, | |
6605 | (unsigned long int) survey->channel_time_rx, | |
6606 | survey->filled); | |
6607 | ||
6608 | dl_list_add_tail(survey_list, &survey->list); | |
6609 | } | |
6610 | ||
6611 | ||
6612 | static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq, | |
6613 | unsigned int freq_filter) | |
6614 | { | |
6615 | if (!freq_filter) | |
6616 | return 1; | |
6617 | ||
6618 | return freq_filter == surveyed_freq; | |
6619 | } | |
6620 | ||
6621 | ||
6622 | static int survey_handler(struct nl_msg *msg, void *arg) | |
6623 | { | |
6624 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
6625 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
6626 | struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1]; | |
6627 | struct survey_results *survey_results; | |
6628 | u32 surveyed_freq = 0; | |
6629 | u32 ifidx; | |
6630 | ||
6631 | static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = { | |
6632 | [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 }, | |
6633 | [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 }, | |
6634 | }; | |
6635 | ||
6636 | survey_results = (struct survey_results *) arg; | |
6637 | ||
6638 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
6639 | genlmsg_attrlen(gnlh, 0), NULL); | |
6640 | ||
e28f39b7 SJ |
6641 | if (!tb[NL80211_ATTR_IFINDEX]) |
6642 | return NL_SKIP; | |
6643 | ||
0185007c MK |
6644 | ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); |
6645 | ||
6646 | if (!tb[NL80211_ATTR_SURVEY_INFO]) | |
6647 | return NL_SKIP; | |
6648 | ||
6649 | if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX, | |
6650 | tb[NL80211_ATTR_SURVEY_INFO], | |
6651 | survey_policy)) | |
6652 | return NL_SKIP; | |
6653 | ||
6654 | if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) { | |
6655 | wpa_printf(MSG_ERROR, "nl80211: Invalid survey data"); | |
6656 | return NL_SKIP; | |
6657 | } | |
6658 | ||
6659 | surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]); | |
6660 | ||
6661 | if (!check_survey_ok(sinfo, surveyed_freq, | |
6662 | survey_results->freq_filter)) | |
6663 | return NL_SKIP; | |
6664 | ||
6665 | if (survey_results->freq_filter && | |
6666 | survey_results->freq_filter != surveyed_freq) { | |
6667 | wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz", | |
6668 | surveyed_freq); | |
6669 | return NL_SKIP; | |
6670 | } | |
6671 | ||
6672 | add_survey(sinfo, ifidx, &survey_results->survey_list); | |
6673 | ||
6674 | return NL_SKIP; | |
6675 | } | |
6676 | ||
6677 | ||
6678 | static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq) | |
6679 | { | |
6680 | struct i802_bss *bss = priv; | |
6681 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6682 | struct nl_msg *msg; | |
9725b784 | 6683 | int err; |
0185007c MK |
6684 | union wpa_event_data data; |
6685 | struct survey_results *survey_results; | |
6686 | ||
6687 | os_memset(&data, 0, sizeof(data)); | |
6688 | survey_results = &data.survey_results; | |
6689 | ||
6690 | dl_list_init(&survey_results->survey_list); | |
6691 | ||
9725b784 | 6692 | msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY); |
0185007c | 6693 | if (!msg) |
a862e4a3 | 6694 | return -ENOBUFS; |
0185007c | 6695 | |
0185007c MK |
6696 | if (freq) |
6697 | data.survey_results.freq_filter = freq; | |
6698 | ||
6699 | do { | |
6700 | wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data"); | |
6701 | err = send_and_recv_msgs(drv, msg, survey_handler, | |
6702 | survey_results); | |
6703 | } while (err > 0); | |
6704 | ||
a862e4a3 | 6705 | if (err) |
0185007c | 6706 | wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data"); |
a862e4a3 JM |
6707 | else |
6708 | wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data); | |
0185007c | 6709 | |
0185007c | 6710 | clean_survey_results(survey_results); |
0185007c MK |
6711 | return err; |
6712 | } | |
6713 | ||
6714 | ||
b14a210c JB |
6715 | static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck, |
6716 | const u8 *replay_ctr) | |
6717 | { | |
6718 | struct i802_bss *bss = priv; | |
6719 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6720 | struct nlattr *replay_nested; | |
6721 | struct nl_msg *msg; | |
6722 | ||
13f83980 | 6723 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) || |
a862e4a3 JM |
6724 | !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) || |
6725 | nla_put(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek) || | |
6726 | nla_put(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck) || | |
6727 | nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN, | |
6728 | replay_ctr)) { | |
6729 | nlmsg_free(msg); | |
6730 | return; | |
6731 | } | |
b14a210c JB |
6732 | |
6733 | nla_nest_end(msg, replay_nested); | |
6734 | ||
6735 | send_and_recv_msgs(drv, msg, NULL, NULL); | |
b14a210c JB |
6736 | } |
6737 | ||
6738 | ||
39718852 JB |
6739 | static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr, |
6740 | const u8 *addr, int qos) | |
bcf24348 | 6741 | { |
39718852 JB |
6742 | /* send data frame to poll STA and check whether |
6743 | * this frame is ACKed */ | |
bcf24348 JB |
6744 | struct { |
6745 | struct ieee80211_hdr hdr; | |
6746 | u16 qos_ctl; | |
6747 | } STRUCT_PACKED nulldata; | |
6748 | size_t size; | |
6749 | ||
6750 | /* Send data frame to poll STA and check whether this frame is ACKed */ | |
6751 | ||
6752 | os_memset(&nulldata, 0, sizeof(nulldata)); | |
6753 | ||
6754 | if (qos) { | |
6755 | nulldata.hdr.frame_control = | |
6756 | IEEE80211_FC(WLAN_FC_TYPE_DATA, | |
6757 | WLAN_FC_STYPE_QOS_NULL); | |
6758 | size = sizeof(nulldata); | |
6759 | } else { | |
6760 | nulldata.hdr.frame_control = | |
6761 | IEEE80211_FC(WLAN_FC_TYPE_DATA, | |
6762 | WLAN_FC_STYPE_NULLFUNC); | |
6763 | size = sizeof(struct ieee80211_hdr); | |
6764 | } | |
6765 | ||
6766 | nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS); | |
6767 | os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN); | |
6768 | os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); | |
6769 | os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); | |
6770 | ||
9ebce9c5 JM |
6771 | if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0, |
6772 | 0, 0) < 0) | |
bcf24348 JB |
6773 | wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to " |
6774 | "send poll frame"); | |
6775 | } | |
6776 | ||
39718852 JB |
6777 | static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr, |
6778 | int qos) | |
6779 | { | |
6780 | struct i802_bss *bss = priv; | |
6781 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6782 | struct nl_msg *msg; | |
6783 | ||
6784 | if (!drv->poll_command_supported) { | |
6785 | nl80211_send_null_frame(bss, own_addr, addr, qos); | |
6786 | return; | |
6787 | } | |
6788 | ||
13f83980 | 6789 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) || |
a862e4a3 JM |
6790 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) { |
6791 | nlmsg_free(msg); | |
6792 | return; | |
6793 | } | |
39718852 JB |
6794 | |
6795 | send_and_recv_msgs(drv, msg, NULL, NULL); | |
39718852 JB |
6796 | } |
6797 | ||
bcf24348 | 6798 | |
29f338af JM |
6799 | static int nl80211_set_power_save(struct i802_bss *bss, int enabled) |
6800 | { | |
6801 | struct nl_msg *msg; | |
6802 | ||
13f83980 | 6803 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) || |
a862e4a3 JM |
6804 | nla_put_u32(msg, NL80211_ATTR_PS_STATE, |
6805 | enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) { | |
6806 | nlmsg_free(msg); | |
6807 | return -ENOBUFS; | |
6808 | } | |
29f338af | 6809 | return send_and_recv_msgs(bss->drv, msg, NULL, NULL); |
29f338af JM |
6810 | } |
6811 | ||
6812 | ||
6813 | static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps, | |
6814 | int ctwindow) | |
6815 | { | |
6816 | struct i802_bss *bss = priv; | |
6817 | ||
6818 | wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d " | |
6819 | "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow); | |
6820 | ||
0de38036 JM |
6821 | if (opp_ps != -1 || ctwindow != -1) { |
6822 | #ifdef ANDROID_P2P | |
6823 | wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow); | |
6824 | #else /* ANDROID_P2P */ | |
29f338af | 6825 | return -1; /* Not yet supported */ |
0de38036 JM |
6826 | #endif /* ANDROID_P2P */ |
6827 | } | |
29f338af JM |
6828 | |
6829 | if (legacy_ps == -1) | |
6830 | return 0; | |
6831 | if (legacy_ps != 0 && legacy_ps != 1) | |
6832 | return -1; /* Not yet supported */ | |
6833 | ||
6834 | return nl80211_set_power_save(bss, legacy_ps); | |
6835 | } | |
6836 | ||
6837 | ||
04e8003c JD |
6838 | static int nl80211_start_radar_detection(void *priv, |
6839 | struct hostapd_freq_params *freq) | |
f90e9c1c SW |
6840 | { |
6841 | struct i802_bss *bss = priv; | |
6842 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6843 | struct nl_msg *msg; | |
6844 | int ret; | |
6845 | ||
04e8003c JD |
6846 | wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)", |
6847 | freq->freq, freq->ht_enabled, freq->vht_enabled, | |
6848 | freq->bandwidth, freq->center_freq1, freq->center_freq2); | |
6849 | ||
f90e9c1c SW |
6850 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) { |
6851 | wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar " | |
6852 | "detection"); | |
6853 | return -1; | |
6854 | } | |
6855 | ||
9725b784 | 6856 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) || |
a862e4a3 JM |
6857 | nl80211_put_freq_params(msg, freq) < 0) { |
6858 | nlmsg_free(msg); | |
6859 | return -1; | |
6860 | } | |
f90e9c1c SW |
6861 | |
6862 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
6863 | if (ret == 0) | |
6864 | return 0; | |
6865 | wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: " | |
6866 | "%d (%s)", ret, strerror(-ret)); | |
f90e9c1c SW |
6867 | return -1; |
6868 | } | |
6869 | ||
03ea1786 AN |
6870 | #ifdef CONFIG_TDLS |
6871 | ||
6872 | static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code, | |
6873 | u8 dialog_token, u16 status_code, | |
984dadc2 AN |
6874 | u32 peer_capab, int initiator, const u8 *buf, |
6875 | size_t len) | |
03ea1786 AN |
6876 | { |
6877 | struct i802_bss *bss = priv; | |
6878 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6879 | struct nl_msg *msg; | |
6880 | ||
6881 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) | |
6882 | return -EOPNOTSUPP; | |
6883 | ||
6884 | if (!dst) | |
6885 | return -EINVAL; | |
6886 | ||
9725b784 | 6887 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) || |
a862e4a3 JM |
6888 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) || |
6889 | nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) || | |
6890 | nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) || | |
6891 | nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code)) | |
6892 | goto fail; | |
96ecea5e SD |
6893 | if (peer_capab) { |
6894 | /* | |
6895 | * The internal enum tdls_peer_capability definition is | |
6896 | * currently identical with the nl80211 enum | |
6897 | * nl80211_tdls_peer_capability, so no conversion is needed | |
6898 | * here. | |
6899 | */ | |
a862e4a3 JM |
6900 | if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY, |
6901 | peer_capab)) | |
6902 | goto fail; | |
96ecea5e | 6903 | } |
a862e4a3 JM |
6904 | if ((initiator && |
6905 | nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) || | |
6906 | nla_put(msg, NL80211_ATTR_IE, len, buf)) | |
6907 | goto fail; | |
03ea1786 AN |
6908 | |
6909 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
6910 | ||
a862e4a3 | 6911 | fail: |
03ea1786 AN |
6912 | nlmsg_free(msg); |
6913 | return -ENOBUFS; | |
6914 | } | |
6915 | ||
6916 | ||
6917 | static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer) | |
6918 | { | |
6919 | struct i802_bss *bss = priv; | |
6920 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6921 | struct nl_msg *msg; | |
6922 | enum nl80211_tdls_operation nl80211_oper; | |
6923 | ||
6924 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) | |
6925 | return -EOPNOTSUPP; | |
6926 | ||
6927 | switch (oper) { | |
6928 | case TDLS_DISCOVERY_REQ: | |
6929 | nl80211_oper = NL80211_TDLS_DISCOVERY_REQ; | |
6930 | break; | |
6931 | case TDLS_SETUP: | |
6932 | nl80211_oper = NL80211_TDLS_SETUP; | |
6933 | break; | |
6934 | case TDLS_TEARDOWN: | |
6935 | nl80211_oper = NL80211_TDLS_TEARDOWN; | |
6936 | break; | |
6937 | case TDLS_ENABLE_LINK: | |
6938 | nl80211_oper = NL80211_TDLS_ENABLE_LINK; | |
6939 | break; | |
6940 | case TDLS_DISABLE_LINK: | |
6941 | nl80211_oper = NL80211_TDLS_DISABLE_LINK; | |
6942 | break; | |
6943 | case TDLS_ENABLE: | |
6944 | return 0; | |
6945 | case TDLS_DISABLE: | |
6946 | return 0; | |
6947 | default: | |
6948 | return -EINVAL; | |
6949 | } | |
6950 | ||
9725b784 | 6951 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) || |
a862e4a3 | 6952 | nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) || |
a862e4a3 JM |
6953 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) { |
6954 | nlmsg_free(msg); | |
6955 | return -ENOBUFS; | |
6956 | } | |
03ea1786 AN |
6957 | |
6958 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
03ea1786 AN |
6959 | } |
6960 | ||
6961 | #endif /* CONFIG TDLS */ | |
6962 | ||
6963 | ||
9ebce9c5 JM |
6964 | static int driver_nl80211_set_key(const char *ifname, void *priv, |
6965 | enum wpa_alg alg, const u8 *addr, | |
6966 | int key_idx, int set_tx, | |
6967 | const u8 *seq, size_t seq_len, | |
6968 | const u8 *key, size_t key_len) | |
6969 | { | |
6970 | struct i802_bss *bss = priv; | |
6971 | return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx, | |
6972 | set_tx, seq, seq_len, key, key_len); | |
6973 | } | |
6974 | ||
6975 | ||
6976 | static int driver_nl80211_scan2(void *priv, | |
6977 | struct wpa_driver_scan_params *params) | |
6978 | { | |
6979 | struct i802_bss *bss = priv; | |
6980 | return wpa_driver_nl80211_scan(bss, params); | |
6981 | } | |
6982 | ||
6983 | ||
6984 | static int driver_nl80211_deauthenticate(void *priv, const u8 *addr, | |
6985 | int reason_code) | |
6986 | { | |
6987 | struct i802_bss *bss = priv; | |
6988 | return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code); | |
6989 | } | |
6990 | ||
6991 | ||
6992 | static int driver_nl80211_authenticate(void *priv, | |
6993 | struct wpa_driver_auth_params *params) | |
6994 | { | |
6995 | struct i802_bss *bss = priv; | |
6996 | return wpa_driver_nl80211_authenticate(bss, params); | |
6997 | } | |
6998 | ||
6999 | ||
7000 | static void driver_nl80211_deinit(void *priv) | |
7001 | { | |
7002 | struct i802_bss *bss = priv; | |
7003 | wpa_driver_nl80211_deinit(bss); | |
7004 | } | |
7005 | ||
7006 | ||
7007 | static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type, | |
7008 | const char *ifname) | |
7009 | { | |
7010 | struct i802_bss *bss = priv; | |
7011 | return wpa_driver_nl80211_if_remove(bss, type, ifname); | |
7012 | } | |
7013 | ||
7014 | ||
7015 | static int driver_nl80211_send_mlme(void *priv, const u8 *data, | |
7016 | size_t data_len, int noack) | |
7017 | { | |
7018 | struct i802_bss *bss = priv; | |
7019 | return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack, | |
7020 | 0, 0, 0, 0); | |
7021 | } | |
7022 | ||
7023 | ||
7024 | static int driver_nl80211_sta_remove(void *priv, const u8 *addr) | |
7025 | { | |
7026 | struct i802_bss *bss = priv; | |
59d7148a | 7027 | return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0); |
9ebce9c5 JM |
7028 | } |
7029 | ||
7030 | ||
9ebce9c5 JM |
7031 | static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr, |
7032 | const char *ifname, int vlan_id) | |
7033 | { | |
7034 | struct i802_bss *bss = priv; | |
7035 | return i802_set_sta_vlan(bss, addr, ifname, vlan_id); | |
7036 | } | |
9ebce9c5 JM |
7037 | |
7038 | ||
7039 | static int driver_nl80211_read_sta_data(void *priv, | |
7040 | struct hostap_sta_driver_data *data, | |
7041 | const u8 *addr) | |
7042 | { | |
7043 | struct i802_bss *bss = priv; | |
7044 | return i802_read_sta_data(bss, data, addr); | |
7045 | } | |
7046 | ||
7047 | ||
7048 | static int driver_nl80211_send_action(void *priv, unsigned int freq, | |
7049 | unsigned int wait_time, | |
7050 | const u8 *dst, const u8 *src, | |
7051 | const u8 *bssid, | |
7052 | const u8 *data, size_t data_len, | |
7053 | int no_cck) | |
7054 | { | |
7055 | struct i802_bss *bss = priv; | |
7056 | return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src, | |
7057 | bssid, data, data_len, no_cck); | |
7058 | } | |
7059 | ||
7060 | ||
7061 | static int driver_nl80211_probe_req_report(void *priv, int report) | |
7062 | { | |
7063 | struct i802_bss *bss = priv; | |
7064 | return wpa_driver_nl80211_probe_req_report(bss, report); | |
7065 | } | |
7066 | ||
7067 | ||
6a1ce395 DG |
7068 | static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md, |
7069 | const u8 *ies, size_t ies_len) | |
7070 | { | |
7071 | int ret; | |
7072 | struct nl_msg *msg; | |
7073 | struct i802_bss *bss = priv; | |
7074 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7075 | u16 mdid = WPA_GET_LE16(md); | |
7076 | ||
6a1ce395 | 7077 | wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs"); |
9725b784 | 7078 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) || |
a862e4a3 JM |
7079 | nla_put(msg, NL80211_ATTR_IE, ies_len, ies) || |
7080 | nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) { | |
7081 | nlmsg_free(msg); | |
7082 | return -ENOBUFS; | |
7083 | } | |
6a1ce395 DG |
7084 | |
7085 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7086 | if (ret) { | |
7087 | wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed " | |
7088 | "err=%d (%s)", ret, strerror(-ret)); | |
7089 | } | |
7090 | ||
7091 | return ret; | |
6a1ce395 DG |
7092 | } |
7093 | ||
7094 | ||
597b94f5 AS |
7095 | const u8 * wpa_driver_nl80211_get_macaddr(void *priv) |
7096 | { | |
7097 | struct i802_bss *bss = priv; | |
7098 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7099 | ||
7100 | if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) | |
7101 | return NULL; | |
7102 | ||
7103 | return bss->addr; | |
7104 | } | |
7105 | ||
7106 | ||
a771c07d JM |
7107 | static const char * scan_state_str(enum scan_states scan_state) |
7108 | { | |
7109 | switch (scan_state) { | |
7110 | case NO_SCAN: | |
7111 | return "NO_SCAN"; | |
7112 | case SCAN_REQUESTED: | |
7113 | return "SCAN_REQUESTED"; | |
7114 | case SCAN_STARTED: | |
7115 | return "SCAN_STARTED"; | |
7116 | case SCAN_COMPLETED: | |
7117 | return "SCAN_COMPLETED"; | |
7118 | case SCAN_ABORTED: | |
7119 | return "SCAN_ABORTED"; | |
7120 | case SCHED_SCAN_STARTED: | |
7121 | return "SCHED_SCAN_STARTED"; | |
7122 | case SCHED_SCAN_STOPPED: | |
7123 | return "SCHED_SCAN_STOPPED"; | |
7124 | case SCHED_SCAN_RESULTS: | |
7125 | return "SCHED_SCAN_RESULTS"; | |
7126 | } | |
7127 | ||
7128 | return "??"; | |
7129 | } | |
7130 | ||
7131 | ||
7132 | static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen) | |
7133 | { | |
7134 | struct i802_bss *bss = priv; | |
7135 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7136 | int res; | |
7137 | char *pos, *end; | |
7138 | ||
7139 | pos = buf; | |
7140 | end = buf + buflen; | |
7141 | ||
7142 | res = os_snprintf(pos, end - pos, | |
7143 | "ifindex=%d\n" | |
7144 | "ifname=%s\n" | |
7145 | "brname=%s\n" | |
7146 | "addr=" MACSTR "\n" | |
7147 | "freq=%d\n" | |
7148 | "%s%s%s%s%s", | |
7149 | bss->ifindex, | |
7150 | bss->ifname, | |
7151 | bss->brname, | |
7152 | MAC2STR(bss->addr), | |
7153 | bss->freq, | |
7154 | bss->beacon_set ? "beacon_set=1\n" : "", | |
7155 | bss->added_if_into_bridge ? | |
7156 | "added_if_into_bridge=1\n" : "", | |
7157 | bss->added_bridge ? "added_bridge=1\n" : "", | |
7158 | bss->in_deinit ? "in_deinit=1\n" : "", | |
7159 | bss->if_dynamic ? "if_dynamic=1\n" : ""); | |
d85e1fc8 | 7160 | if (os_snprintf_error(end - pos, res)) |
a771c07d JM |
7161 | return pos - buf; |
7162 | pos += res; | |
7163 | ||
7164 | if (bss->wdev_id_set) { | |
7165 | res = os_snprintf(pos, end - pos, "wdev_id=%llu\n", | |
7166 | (unsigned long long) bss->wdev_id); | |
d85e1fc8 | 7167 | if (os_snprintf_error(end - pos, res)) |
a771c07d JM |
7168 | return pos - buf; |
7169 | pos += res; | |
7170 | } | |
7171 | ||
7172 | res = os_snprintf(pos, end - pos, | |
7173 | "phyname=%s\n" | |
fee354c7 | 7174 | "perm_addr=" MACSTR "\n" |
a771c07d JM |
7175 | "drv_ifindex=%d\n" |
7176 | "operstate=%d\n" | |
7177 | "scan_state=%s\n" | |
7178 | "auth_bssid=" MACSTR "\n" | |
7179 | "auth_attempt_bssid=" MACSTR "\n" | |
7180 | "bssid=" MACSTR "\n" | |
7181 | "prev_bssid=" MACSTR "\n" | |
7182 | "associated=%d\n" | |
7183 | "assoc_freq=%u\n" | |
7184 | "monitor_sock=%d\n" | |
7185 | "monitor_ifidx=%d\n" | |
7186 | "monitor_refcount=%d\n" | |
7187 | "last_mgmt_freq=%u\n" | |
7188 | "eapol_tx_sock=%d\n" | |
97752f79 | 7189 | "%s%s%s%s%s%s%s%s%s%s%s%s%s", |
a771c07d | 7190 | drv->phyname, |
fee354c7 | 7191 | MAC2STR(drv->perm_addr), |
a771c07d JM |
7192 | drv->ifindex, |
7193 | drv->operstate, | |
7194 | scan_state_str(drv->scan_state), | |
7195 | MAC2STR(drv->auth_bssid), | |
7196 | MAC2STR(drv->auth_attempt_bssid), | |
7197 | MAC2STR(drv->bssid), | |
7198 | MAC2STR(drv->prev_bssid), | |
7199 | drv->associated, | |
7200 | drv->assoc_freq, | |
7201 | drv->monitor_sock, | |
7202 | drv->monitor_ifidx, | |
7203 | drv->monitor_refcount, | |
7204 | drv->last_mgmt_freq, | |
7205 | drv->eapol_tx_sock, | |
7206 | drv->ignore_if_down_event ? | |
7207 | "ignore_if_down_event=1\n" : "", | |
7208 | drv->scan_complete_events ? | |
7209 | "scan_complete_events=1\n" : "", | |
7210 | drv->disabled_11b_rates ? | |
7211 | "disabled_11b_rates=1\n" : "", | |
7212 | drv->pending_remain_on_chan ? | |
7213 | "pending_remain_on_chan=1\n" : "", | |
7214 | drv->in_interface_list ? "in_interface_list=1\n" : "", | |
7215 | drv->device_ap_sme ? "device_ap_sme=1\n" : "", | |
7216 | drv->poll_command_supported ? | |
7217 | "poll_command_supported=1\n" : "", | |
7218 | drv->data_tx_status ? "data_tx_status=1\n" : "", | |
7219 | drv->scan_for_auth ? "scan_for_auth=1\n" : "", | |
7220 | drv->retry_auth ? "retry_auth=1\n" : "", | |
7221 | drv->use_monitor ? "use_monitor=1\n" : "", | |
7222 | drv->ignore_next_local_disconnect ? | |
7223 | "ignore_next_local_disconnect=1\n" : "", | |
d6a36f39 | 7224 | drv->ignore_next_local_deauth ? |
97752f79 | 7225 | "ignore_next_local_deauth=1\n" : ""); |
d85e1fc8 | 7226 | if (os_snprintf_error(end - pos, res)) |
a771c07d JM |
7227 | return pos - buf; |
7228 | pos += res; | |
7229 | ||
7230 | if (drv->has_capability) { | |
7231 | res = os_snprintf(pos, end - pos, | |
7232 | "capa.key_mgmt=0x%x\n" | |
7233 | "capa.enc=0x%x\n" | |
7234 | "capa.auth=0x%x\n" | |
24bd4e0b | 7235 | "capa.flags=0x%llx\n" |
a0a34d53 | 7236 | "capa.rrm_flags=0x%x\n" |
a771c07d JM |
7237 | "capa.max_scan_ssids=%d\n" |
7238 | "capa.max_sched_scan_ssids=%d\n" | |
7239 | "capa.sched_scan_supported=%d\n" | |
7240 | "capa.max_match_sets=%d\n" | |
7241 | "capa.max_remain_on_chan=%u\n" | |
7242 | "capa.max_stations=%u\n" | |
7243 | "capa.probe_resp_offloads=0x%x\n" | |
7244 | "capa.max_acl_mac_addrs=%u\n" | |
7245 | "capa.num_multichan_concurrent=%u\n", | |
7246 | drv->capa.key_mgmt, | |
7247 | drv->capa.enc, | |
7248 | drv->capa.auth, | |
24bd4e0b | 7249 | (unsigned long long) drv->capa.flags, |
a0a34d53 | 7250 | drv->capa.rrm_flags, |
a771c07d JM |
7251 | drv->capa.max_scan_ssids, |
7252 | drv->capa.max_sched_scan_ssids, | |
7253 | drv->capa.sched_scan_supported, | |
7254 | drv->capa.max_match_sets, | |
7255 | drv->capa.max_remain_on_chan, | |
7256 | drv->capa.max_stations, | |
7257 | drv->capa.probe_resp_offloads, | |
7258 | drv->capa.max_acl_mac_addrs, | |
7259 | drv->capa.num_multichan_concurrent); | |
d85e1fc8 | 7260 | if (os_snprintf_error(end - pos, res)) |
a771c07d JM |
7261 | return pos - buf; |
7262 | pos += res; | |
7263 | } | |
7264 | ||
7265 | return pos - buf; | |
7266 | } | |
7267 | ||
7268 | ||
1c4ffa87 AO |
7269 | static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings) |
7270 | { | |
a862e4a3 JM |
7271 | if ((settings->head && |
7272 | nla_put(msg, NL80211_ATTR_BEACON_HEAD, | |
7273 | settings->head_len, settings->head)) || | |
7274 | (settings->tail && | |
7275 | nla_put(msg, NL80211_ATTR_BEACON_TAIL, | |
7276 | settings->tail_len, settings->tail)) || | |
7277 | (settings->beacon_ies && | |
7278 | nla_put(msg, NL80211_ATTR_IE, | |
7279 | settings->beacon_ies_len, settings->beacon_ies)) || | |
7280 | (settings->proberesp_ies && | |
7281 | nla_put(msg, NL80211_ATTR_IE_PROBE_RESP, | |
7282 | settings->proberesp_ies_len, settings->proberesp_ies)) || | |
7283 | (settings->assocresp_ies && | |
7284 | nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP, | |
7285 | settings->assocresp_ies_len, settings->assocresp_ies)) || | |
7286 | (settings->probe_resp && | |
7287 | nla_put(msg, NL80211_ATTR_PROBE_RESP, | |
7288 | settings->probe_resp_len, settings->probe_resp))) | |
7289 | return -ENOBUFS; | |
1c4ffa87 AO |
7290 | |
7291 | return 0; | |
1c4ffa87 AO |
7292 | } |
7293 | ||
7294 | ||
7295 | static int nl80211_switch_channel(void *priv, struct csa_settings *settings) | |
7296 | { | |
7297 | struct nl_msg *msg; | |
7298 | struct i802_bss *bss = priv; | |
7299 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7300 | struct nlattr *beacon_csa; | |
7301 | int ret = -ENOBUFS; | |
7302 | ||
8d1fdde7 | 7303 | wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)", |
1c4ffa87 | 7304 | settings->cs_count, settings->block_tx, |
8d1fdde7 JD |
7305 | settings->freq_params.freq, settings->freq_params.bandwidth, |
7306 | settings->freq_params.center_freq1, | |
7307 | settings->freq_params.center_freq2); | |
1c4ffa87 | 7308 | |
991aa9c7 | 7309 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) { |
1c4ffa87 AO |
7310 | wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command"); |
7311 | return -EOPNOTSUPP; | |
7312 | } | |
7313 | ||
7314 | if ((drv->nlmode != NL80211_IFTYPE_AP) && | |
7315 | (drv->nlmode != NL80211_IFTYPE_P2P_GO)) | |
7316 | return -EOPNOTSUPP; | |
7317 | ||
7318 | /* check settings validity */ | |
7319 | if (!settings->beacon_csa.tail || | |
7320 | ((settings->beacon_csa.tail_len <= | |
7321 | settings->counter_offset_beacon) || | |
7322 | (settings->beacon_csa.tail[settings->counter_offset_beacon] != | |
7323 | settings->cs_count))) | |
7324 | return -EINVAL; | |
7325 | ||
7326 | if (settings->beacon_csa.probe_resp && | |
7327 | ((settings->beacon_csa.probe_resp_len <= | |
7328 | settings->counter_offset_presp) || | |
7329 | (settings->beacon_csa.probe_resp[settings->counter_offset_presp] != | |
7330 | settings->cs_count))) | |
7331 | return -EINVAL; | |
7332 | ||
13f83980 | 7333 | if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) || |
a862e4a3 JM |
7334 | nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, |
7335 | settings->cs_count) || | |
7336 | (ret = nl80211_put_freq_params(msg, &settings->freq_params)) || | |
7337 | (settings->block_tx && | |
7338 | nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))) | |
1c4ffa87 AO |
7339 | goto error; |
7340 | ||
1c4ffa87 AO |
7341 | /* beacon_after params */ |
7342 | ret = set_beacon_data(msg, &settings->beacon_after); | |
7343 | if (ret) | |
7344 | goto error; | |
7345 | ||
7346 | /* beacon_csa params */ | |
7347 | beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES); | |
7348 | if (!beacon_csa) | |
a862e4a3 | 7349 | goto fail; |
1c4ffa87 AO |
7350 | |
7351 | ret = set_beacon_data(msg, &settings->beacon_csa); | |
7352 | if (ret) | |
7353 | goto error; | |
7354 | ||
a862e4a3 JM |
7355 | if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON, |
7356 | settings->counter_offset_beacon) || | |
7357 | (settings->beacon_csa.probe_resp && | |
7358 | nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP, | |
7359 | settings->counter_offset_presp))) | |
7360 | goto fail; | |
1c4ffa87 AO |
7361 | |
7362 | nla_nest_end(msg, beacon_csa); | |
7363 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7364 | if (ret) { | |
7365 | wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)", | |
7366 | ret, strerror(-ret)); | |
7367 | } | |
7368 | return ret; | |
7369 | ||
a862e4a3 | 7370 | fail: |
1c4ffa87 AO |
7371 | ret = -ENOBUFS; |
7372 | error: | |
7373 | nlmsg_free(msg); | |
7374 | wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request"); | |
7375 | return ret; | |
7376 | } | |
7377 | ||
7378 | ||
dfa87878 MB |
7379 | static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr, |
7380 | u8 user_priority, u16 admitted_time) | |
7381 | { | |
7382 | struct i802_bss *bss = priv; | |
7383 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7384 | struct nl_msg *msg; | |
7385 | int ret; | |
7386 | ||
7387 | wpa_printf(MSG_DEBUG, | |
7388 | "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d", | |
7389 | tsid, admitted_time, user_priority); | |
7390 | ||
7391 | if (!is_sta_interface(drv->nlmode)) | |
7392 | return -ENOTSUP; | |
7393 | ||
56f77852 JM |
7394 | msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS); |
7395 | if (!msg || | |
a862e4a3 JM |
7396 | nla_put_u8(msg, NL80211_ATTR_TSID, tsid) || |
7397 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) || | |
7398 | nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) || | |
7399 | nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) { | |
7400 | nlmsg_free(msg); | |
7401 | return -ENOBUFS; | |
7402 | } | |
dfa87878 MB |
7403 | |
7404 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7405 | if (ret) | |
7406 | wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)", | |
7407 | ret, strerror(-ret)); | |
7408 | return ret; | |
dfa87878 MB |
7409 | } |
7410 | ||
7411 | ||
7412 | static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr) | |
7413 | { | |
7414 | struct i802_bss *bss = priv; | |
7415 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7416 | struct nl_msg *msg; | |
7417 | int ret; | |
7418 | ||
7419 | wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid); | |
7420 | ||
7421 | if (!is_sta_interface(drv->nlmode)) | |
7422 | return -ENOTSUP; | |
7423 | ||
56f77852 | 7424 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) || |
a862e4a3 JM |
7425 | nla_put_u8(msg, NL80211_ATTR_TSID, tsid) || |
7426 | nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) { | |
7427 | nlmsg_free(msg); | |
7428 | return -ENOBUFS; | |
7429 | } | |
dfa87878 MB |
7430 | |
7431 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7432 | if (ret) | |
7433 | wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)", | |
7434 | ret, strerror(-ret)); | |
7435 | return ret; | |
dfa87878 MB |
7436 | } |
7437 | ||
7438 | ||
6b9f7af6 JM |
7439 | #ifdef CONFIG_TESTING_OPTIONS |
7440 | static int cmd_reply_handler(struct nl_msg *msg, void *arg) | |
7441 | { | |
7442 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
7443 | struct wpabuf *buf = arg; | |
7444 | ||
7445 | if (!buf) | |
7446 | return NL_SKIP; | |
7447 | ||
7448 | if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) { | |
7449 | wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply"); | |
7450 | return NL_SKIP; | |
7451 | } | |
7452 | ||
7453 | wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0), | |
7454 | genlmsg_attrlen(gnlh, 0)); | |
7455 | ||
7456 | return NL_SKIP; | |
7457 | } | |
7458 | #endif /* CONFIG_TESTING_OPTIONS */ | |
7459 | ||
7460 | ||
adef8948 BL |
7461 | static int vendor_reply_handler(struct nl_msg *msg, void *arg) |
7462 | { | |
7463 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
7464 | struct nlattr *nl_vendor_reply, *nl; | |
7465 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
7466 | struct wpabuf *buf = arg; | |
7467 | int rem; | |
7468 | ||
7469 | if (!buf) | |
7470 | return NL_SKIP; | |
7471 | ||
7472 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
7473 | genlmsg_attrlen(gnlh, 0), NULL); | |
7474 | nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA]; | |
7475 | ||
7476 | if (!nl_vendor_reply) | |
7477 | return NL_SKIP; | |
7478 | ||
7479 | if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) { | |
7480 | wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply"); | |
7481 | return NL_SKIP; | |
7482 | } | |
7483 | ||
7484 | nla_for_each_nested(nl, nl_vendor_reply, rem) { | |
7485 | wpabuf_put_data(buf, nla_data(nl), nla_len(nl)); | |
7486 | } | |
7487 | ||
7488 | return NL_SKIP; | |
7489 | } | |
7490 | ||
7491 | ||
7492 | static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id, | |
7493 | unsigned int subcmd, const u8 *data, | |
7494 | size_t data_len, struct wpabuf *buf) | |
7495 | { | |
7496 | struct i802_bss *bss = priv; | |
7497 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7498 | struct nl_msg *msg; | |
7499 | int ret; | |
7500 | ||
6b9f7af6 JM |
7501 | #ifdef CONFIG_TESTING_OPTIONS |
7502 | if (vendor_id == 0xffffffff) { | |
56f77852 JM |
7503 | msg = nlmsg_alloc(); |
7504 | if (!msg) | |
7505 | return -ENOMEM; | |
7506 | ||
6b9f7af6 JM |
7507 | nl80211_cmd(drv, msg, 0, subcmd); |
7508 | if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) < | |
7509 | 0) | |
a862e4a3 | 7510 | goto fail; |
6b9f7af6 JM |
7511 | ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf); |
7512 | if (ret) | |
7513 | wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d", | |
7514 | ret); | |
7515 | return ret; | |
7516 | } | |
7517 | #endif /* CONFIG_TESTING_OPTIONS */ | |
7518 | ||
56f77852 | 7519 | if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) || |
a862e4a3 JM |
7520 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) || |
7521 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) || | |
7522 | (data && | |
7523 | nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data))) | |
7524 | goto fail; | |
adef8948 BL |
7525 | |
7526 | ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf); | |
7527 | if (ret) | |
7528 | wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d", | |
7529 | ret); | |
7530 | return ret; | |
7531 | ||
a862e4a3 | 7532 | fail: |
adef8948 BL |
7533 | nlmsg_free(msg); |
7534 | return -ENOBUFS; | |
7535 | } | |
7536 | ||
7537 | ||
049105b4 KP |
7538 | static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set, |
7539 | u8 qos_map_set_len) | |
7540 | { | |
7541 | struct i802_bss *bss = priv; | |
7542 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7543 | struct nl_msg *msg; | |
7544 | int ret; | |
7545 | ||
049105b4 KP |
7546 | wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map", |
7547 | qos_map_set, qos_map_set_len); | |
7548 | ||
9725b784 | 7549 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) || |
a862e4a3 JM |
7550 | nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) { |
7551 | nlmsg_free(msg); | |
7552 | return -ENOBUFS; | |
7553 | } | |
049105b4 KP |
7554 | |
7555 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7556 | if (ret) | |
7557 | wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed"); | |
7558 | ||
7559 | return ret; | |
049105b4 KP |
7560 | } |
7561 | ||
7562 | ||
e4fa8b12 EP |
7563 | static int nl80211_set_wowlan(void *priv, |
7564 | const struct wowlan_triggers *triggers) | |
7565 | { | |
7566 | struct i802_bss *bss = priv; | |
7567 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7568 | struct nl_msg *msg; | |
7569 | struct nlattr *wowlan_triggers; | |
7570 | int ret; | |
7571 | ||
e4fa8b12 EP |
7572 | wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan"); |
7573 | ||
9725b784 | 7574 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WOWLAN)) || |
a862e4a3 JM |
7575 | !(wowlan_triggers = nla_nest_start(msg, |
7576 | NL80211_ATTR_WOWLAN_TRIGGERS)) || | |
7577 | (triggers->any && | |
7578 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) || | |
7579 | (triggers->disconnect && | |
7580 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) || | |
7581 | (triggers->magic_pkt && | |
7582 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) || | |
7583 | (triggers->gtk_rekey_failure && | |
7584 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) || | |
7585 | (triggers->eap_identity_req && | |
7586 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) || | |
7587 | (triggers->four_way_handshake && | |
7588 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) || | |
7589 | (triggers->rfkill_release && | |
7590 | nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) { | |
7591 | nlmsg_free(msg); | |
7592 | return -ENOBUFS; | |
7593 | } | |
e4fa8b12 EP |
7594 | |
7595 | nla_nest_end(msg, wowlan_triggers); | |
7596 | ||
7597 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7598 | if (ret) | |
7599 | wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed"); | |
7600 | ||
7601 | return ret; | |
e4fa8b12 EP |
7602 | } |
7603 | ||
7604 | ||
0800f9ee JM |
7605 | static int nl80211_roaming(void *priv, int allowed, const u8 *bssid) |
7606 | { | |
7607 | struct i802_bss *bss = priv; | |
7608 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7609 | struct nl_msg *msg; | |
7610 | struct nlattr *params; | |
7611 | ||
7612 | wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed); | |
7613 | ||
7614 | if (!drv->roaming_vendor_cmd_avail) { | |
7615 | wpa_printf(MSG_DEBUG, | |
7616 | "nl80211: Ignore roaming policy change since driver does not provide command for setting it"); | |
7617 | return -1; | |
7618 | } | |
7619 | ||
9725b784 | 7620 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) || |
a862e4a3 JM |
7621 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) || |
7622 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, | |
7623 | QCA_NL80211_VENDOR_SUBCMD_ROAMING) || | |
7624 | !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) || | |
7625 | nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY, | |
7626 | allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS : | |
7627 | QCA_ROAMING_NOT_ALLOWED) || | |
7628 | (bssid && | |
7629 | nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) { | |
7630 | nlmsg_free(msg); | |
7631 | return -1; | |
7632 | } | |
0800f9ee JM |
7633 | nla_nest_end(msg, params); |
7634 | ||
7635 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
0800f9ee JM |
7636 | } |
7637 | ||
7638 | ||
fee354c7 JM |
7639 | static int nl80211_set_mac_addr(void *priv, const u8 *addr) |
7640 | { | |
7641 | struct i802_bss *bss = priv; | |
7642 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7643 | int new_addr = addr != NULL; | |
7644 | ||
7645 | if (!addr) | |
7646 | addr = drv->perm_addr; | |
7647 | ||
7648 | if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0) | |
7649 | return -1; | |
7650 | ||
7651 | if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0) | |
7652 | { | |
7653 | wpa_printf(MSG_DEBUG, | |
7654 | "nl80211: failed to set_mac_addr for %s to " MACSTR, | |
7655 | bss->ifname, MAC2STR(addr)); | |
7656 | if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, | |
7657 | 1) < 0) { | |
7658 | wpa_printf(MSG_DEBUG, | |
7659 | "nl80211: Could not restore interface UP after failed set_mac_addr"); | |
7660 | } | |
7661 | return -1; | |
7662 | } | |
7663 | ||
7664 | wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR, | |
7665 | bss->ifname, MAC2STR(addr)); | |
7666 | drv->addr_changed = new_addr; | |
7667 | os_memcpy(bss->addr, addr, ETH_ALEN); | |
7668 | ||
7669 | if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0) | |
7670 | { | |
7671 | wpa_printf(MSG_DEBUG, | |
7672 | "nl80211: Could not restore interface UP after set_mac_addr"); | |
7673 | } | |
7674 | ||
7675 | return 0; | |
7676 | } | |
7677 | ||
7678 | ||
6c1664f6 BC |
7679 | #ifdef CONFIG_MESH |
7680 | ||
7681 | static int wpa_driver_nl80211_init_mesh(void *priv) | |
7682 | { | |
7683 | if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) { | |
7684 | wpa_printf(MSG_INFO, | |
7685 | "nl80211: Failed to set interface into mesh mode"); | |
7686 | return -1; | |
7687 | } | |
7688 | return 0; | |
7689 | } | |
7690 | ||
7691 | ||
7692 | static int | |
7693 | wpa_driver_nl80211_join_mesh(void *priv, | |
7694 | struct wpa_driver_mesh_join_params *params) | |
7695 | { | |
7696 | struct i802_bss *bss = priv; | |
7697 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7698 | struct nl_msg *msg; | |
7699 | struct nlattr *container; | |
7700 | int ret = 0; | |
7701 | ||
6c1664f6 | 7702 | wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex); |
9725b784 JM |
7703 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH); |
7704 | if (!msg) | |
a862e4a3 | 7705 | goto fail; |
6c1664f6 BC |
7706 | if (params->freq) { |
7707 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
a862e4a3 JM |
7708 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq)) |
7709 | goto fail; | |
6c1664f6 BC |
7710 | } |
7711 | ||
5cfb672d JM |
7712 | if (params->ht_mode) { |
7713 | unsigned int ht_value; | |
7714 | char *ht_mode = ""; | |
7715 | ||
7716 | switch (params->ht_mode) { | |
7717 | default: | |
7718 | case CHAN_NO_HT: | |
7719 | ht_value = NL80211_CHAN_NO_HT; | |
7720 | ht_mode = "NOHT"; | |
7721 | break; | |
7722 | case CHAN_HT20: | |
7723 | ht_value = NL80211_CHAN_HT20; | |
7724 | ht_mode = "HT20"; | |
7725 | break; | |
7726 | case CHAN_HT40PLUS: | |
7727 | ht_value = NL80211_CHAN_HT40PLUS; | |
7728 | ht_mode = "HT40+"; | |
7729 | break; | |
7730 | case CHAN_HT40MINUS: | |
7731 | ht_value = NL80211_CHAN_HT40MINUS; | |
7732 | ht_mode = "HT40-"; | |
7733 | break; | |
7734 | } | |
7735 | wpa_printf(MSG_DEBUG, " * ht_mode=%s", ht_mode); | |
a862e4a3 JM |
7736 | if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ht_value)) |
7737 | goto fail; | |
5cfb672d JM |
7738 | } |
7739 | ||
6c1664f6 BC |
7740 | if (params->basic_rates) { |
7741 | u8 rates[NL80211_MAX_SUPP_RATES]; | |
7742 | u8 rates_len = 0; | |
7743 | int i; | |
7744 | ||
7745 | for (i = 0; i < NL80211_MAX_SUPP_RATES; i++) { | |
7746 | if (params->basic_rates[i] < 0) | |
7747 | break; | |
7748 | rates[rates_len++] = params->basic_rates[i] / 5; | |
7749 | } | |
7750 | ||
a862e4a3 JM |
7751 | if (nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, |
7752 | rates)) | |
7753 | goto fail; | |
6c1664f6 BC |
7754 | } |
7755 | ||
7756 | if (params->meshid) { | |
7757 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
7758 | params->meshid, params->meshid_len); | |
a862e4a3 JM |
7759 | if (nla_put(msg, NL80211_ATTR_MESH_ID, params->meshid_len, |
7760 | params->meshid)) | |
7761 | goto fail; | |
6c1664f6 BC |
7762 | } |
7763 | ||
9c58c5f7 MH |
7764 | if (params->beacon_int > 0) { |
7765 | wpa_printf(MSG_DEBUG, " * beacon_int=%d", params->beacon_int); | |
7766 | if (nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL, | |
7767 | params->beacon_int)) | |
7768 | goto fail; | |
7769 | } | |
7770 | ||
6c1664f6 BC |
7771 | wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags); |
7772 | ||
7773 | container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP); | |
7774 | if (!container) | |
a862e4a3 | 7775 | goto fail; |
6c1664f6 BC |
7776 | |
7777 | if (params->ies) { | |
7778 | wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len); | |
a862e4a3 JM |
7779 | if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len, |
7780 | params->ies)) | |
7781 | goto fail; | |
6c1664f6 BC |
7782 | } |
7783 | /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */ | |
7784 | if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) { | |
a862e4a3 JM |
7785 | if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) || |
7786 | nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH)) | |
7787 | goto fail; | |
6c1664f6 | 7788 | } |
a862e4a3 JM |
7789 | if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) && |
7790 | nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE)) | |
7791 | goto fail; | |
7792 | if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) && | |
7793 | nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM)) | |
7794 | goto fail; | |
6c1664f6 BC |
7795 | nla_nest_end(msg, container); |
7796 | ||
7797 | container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG); | |
7798 | if (!container) | |
a862e4a3 | 7799 | goto fail; |
6c1664f6 | 7800 | |
a862e4a3 JM |
7801 | if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) && |
7802 | nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0)) | |
7803 | goto fail; | |
4b409368 MH |
7804 | if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) && |
7805 | nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS, | |
7806 | params->max_peer_links)) | |
7807 | goto fail; | |
6c1664f6 BC |
7808 | nla_nest_end(msg, container); |
7809 | ||
7810 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
7811 | msg = NULL; | |
7812 | if (ret) { | |
7813 | wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)", | |
7814 | ret, strerror(-ret)); | |
a862e4a3 | 7815 | goto fail; |
6c1664f6 BC |
7816 | } |
7817 | ret = 0; | |
7818 | bss->freq = params->freq; | |
7819 | wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully"); | |
7820 | ||
a862e4a3 | 7821 | fail: |
6c1664f6 BC |
7822 | nlmsg_free(msg); |
7823 | return ret; | |
7824 | } | |
7825 | ||
7826 | ||
7827 | static int wpa_driver_nl80211_leave_mesh(void *priv) | |
7828 | { | |
7829 | struct i802_bss *bss = priv; | |
7830 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7831 | struct nl_msg *msg; | |
9725b784 | 7832 | int ret; |
6c1664f6 BC |
7833 | |
7834 | wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex); | |
9725b784 | 7835 | msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH); |
6c1664f6 | 7836 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
6c1664f6 BC |
7837 | if (ret) { |
7838 | wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)", | |
7839 | ret, strerror(-ret)); | |
a862e4a3 JM |
7840 | } else { |
7841 | wpa_printf(MSG_DEBUG, | |
7842 | "nl80211: mesh leave request send successfully"); | |
6c1664f6 | 7843 | } |
6c1664f6 | 7844 | |
f33c82d2 JM |
7845 | if (wpa_driver_nl80211_set_mode(drv->first_bss, |
7846 | NL80211_IFTYPE_STATION)) { | |
7847 | wpa_printf(MSG_INFO, | |
7848 | "nl80211: Failed to set interface into station mode"); | |
7849 | } | |
6c1664f6 BC |
7850 | return ret; |
7851 | } | |
7852 | ||
7853 | #endif /* CONFIG_MESH */ | |
7854 | ||
7855 | ||
ed4ddb6d KP |
7856 | static int wpa_driver_br_add_ip_neigh(void *priv, u8 version, |
7857 | const u8 *ipaddr, int prefixlen, | |
7858 | const u8 *addr) | |
71103bed KP |
7859 | { |
7860 | #ifdef CONFIG_LIBNL3_ROUTE | |
7861 | struct i802_bss *bss = priv; | |
7862 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7863 | struct rtnl_neigh *rn; | |
7864 | struct nl_addr *nl_ipaddr = NULL; | |
7865 | struct nl_addr *nl_lladdr = NULL; | |
ed4ddb6d | 7866 | int family, addrsize; |
71103bed KP |
7867 | int res; |
7868 | ||
ed4ddb6d | 7869 | if (!ipaddr || prefixlen == 0 || !addr) |
71103bed KP |
7870 | return -EINVAL; |
7871 | ||
7872 | if (bss->br_ifindex == 0) { | |
7873 | wpa_printf(MSG_DEBUG, | |
7874 | "nl80211: bridge must be set before adding an ip neigh to it"); | |
7875 | return -1; | |
7876 | } | |
7877 | ||
7878 | if (!drv->rtnl_sk) { | |
7879 | wpa_printf(MSG_DEBUG, | |
7880 | "nl80211: nl_sock for NETLINK_ROUTE is not initialized"); | |
7881 | return -1; | |
7882 | } | |
7883 | ||
ed4ddb6d KP |
7884 | if (version == 4) { |
7885 | family = AF_INET; | |
7886 | addrsize = 4; | |
7887 | } else if (version == 6) { | |
7888 | family = AF_INET6; | |
7889 | addrsize = 16; | |
7890 | } else { | |
7891 | return -EINVAL; | |
7892 | } | |
7893 | ||
71103bed KP |
7894 | rn = rtnl_neigh_alloc(); |
7895 | if (rn == NULL) | |
7896 | return -ENOMEM; | |
7897 | ||
7898 | /* set the destination ip address for neigh */ | |
ed4ddb6d | 7899 | nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize); |
71103bed KP |
7900 | if (nl_ipaddr == NULL) { |
7901 | wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed"); | |
7902 | res = -ENOMEM; | |
7903 | goto errout; | |
7904 | } | |
7905 | nl_addr_set_prefixlen(nl_ipaddr, prefixlen); | |
7906 | res = rtnl_neigh_set_dst(rn, nl_ipaddr); | |
7907 | if (res) { | |
7908 | wpa_printf(MSG_DEBUG, | |
7909 | "nl80211: neigh set destination addr failed"); | |
7910 | goto errout; | |
7911 | } | |
7912 | ||
7913 | /* set the corresponding lladdr for neigh */ | |
7914 | nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN); | |
7915 | if (nl_lladdr == NULL) { | |
7916 | wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed"); | |
7917 | res = -ENOMEM; | |
7918 | goto errout; | |
7919 | } | |
7920 | rtnl_neigh_set_lladdr(rn, nl_lladdr); | |
7921 | ||
7922 | rtnl_neigh_set_ifindex(rn, bss->br_ifindex); | |
7923 | rtnl_neigh_set_state(rn, NUD_PERMANENT); | |
7924 | ||
7925 | res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE); | |
7926 | if (res) { | |
7927 | wpa_printf(MSG_DEBUG, | |
7928 | "nl80211: Adding bridge ip neigh failed: %s", | |
7929 | strerror(errno)); | |
7930 | } | |
7931 | errout: | |
7932 | if (nl_lladdr) | |
7933 | nl_addr_put(nl_lladdr); | |
7934 | if (nl_ipaddr) | |
7935 | nl_addr_put(nl_ipaddr); | |
7936 | if (rn) | |
7937 | rtnl_neigh_put(rn); | |
7938 | return res; | |
7939 | #else /* CONFIG_LIBNL3_ROUTE */ | |
7940 | return -1; | |
7941 | #endif /* CONFIG_LIBNL3_ROUTE */ | |
7942 | } | |
7943 | ||
7944 | ||
ed4ddb6d KP |
7945 | static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version, |
7946 | const u8 *ipaddr) | |
71103bed KP |
7947 | { |
7948 | #ifdef CONFIG_LIBNL3_ROUTE | |
7949 | struct i802_bss *bss = priv; | |
7950 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
7951 | struct rtnl_neigh *rn; | |
7952 | struct nl_addr *nl_ipaddr; | |
ed4ddb6d | 7953 | int family, addrsize; |
71103bed KP |
7954 | int res; |
7955 | ||
ed4ddb6d | 7956 | if (!ipaddr) |
71103bed KP |
7957 | return -EINVAL; |
7958 | ||
ed4ddb6d KP |
7959 | if (version == 4) { |
7960 | family = AF_INET; | |
7961 | addrsize = 4; | |
7962 | } else if (version == 6) { | |
7963 | family = AF_INET6; | |
7964 | addrsize = 16; | |
7965 | } else { | |
7966 | return -EINVAL; | |
7967 | } | |
7968 | ||
71103bed KP |
7969 | if (bss->br_ifindex == 0) { |
7970 | wpa_printf(MSG_DEBUG, | |
7971 | "nl80211: bridge must be set to delete an ip neigh"); | |
7972 | return -1; | |
7973 | } | |
7974 | ||
7975 | if (!drv->rtnl_sk) { | |
7976 | wpa_printf(MSG_DEBUG, | |
7977 | "nl80211: nl_sock for NETLINK_ROUTE is not initialized"); | |
7978 | return -1; | |
7979 | } | |
7980 | ||
7981 | rn = rtnl_neigh_alloc(); | |
7982 | if (rn == NULL) | |
7983 | return -ENOMEM; | |
7984 | ||
7985 | /* set the destination ip address for neigh */ | |
ed4ddb6d | 7986 | nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize); |
71103bed KP |
7987 | if (nl_ipaddr == NULL) { |
7988 | wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed"); | |
7989 | res = -ENOMEM; | |
7990 | goto errout; | |
7991 | } | |
7992 | res = rtnl_neigh_set_dst(rn, nl_ipaddr); | |
7993 | if (res) { | |
7994 | wpa_printf(MSG_DEBUG, | |
7995 | "nl80211: neigh set destination addr failed"); | |
7996 | goto errout; | |
7997 | } | |
7998 | ||
7999 | rtnl_neigh_set_ifindex(rn, bss->br_ifindex); | |
8000 | ||
8001 | res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0); | |
8002 | if (res) { | |
8003 | wpa_printf(MSG_DEBUG, | |
8004 | "nl80211: Deleting bridge ip neigh failed: %s", | |
8005 | strerror(errno)); | |
8006 | } | |
8007 | errout: | |
8008 | if (nl_ipaddr) | |
8009 | nl_addr_put(nl_ipaddr); | |
8010 | if (rn) | |
8011 | rtnl_neigh_put(rn); | |
8012 | return res; | |
8013 | #else /* CONFIG_LIBNL3_ROUTE */ | |
8014 | return -1; | |
8015 | #endif /* CONFIG_LIBNL3_ROUTE */ | |
8016 | } | |
8017 | ||
8018 | ||
73d2294f KP |
8019 | static int linux_write_system_file(const char *path, unsigned int val) |
8020 | { | |
8021 | char buf[50]; | |
8022 | int fd, len; | |
8023 | ||
8024 | len = os_snprintf(buf, sizeof(buf), "%u\n", val); | |
a9aaacbb | 8025 | if (os_snprintf_error(sizeof(buf), len)) |
73d2294f KP |
8026 | return -1; |
8027 | ||
8028 | fd = open(path, O_WRONLY); | |
8029 | if (fd < 0) | |
8030 | return -1; | |
8031 | ||
8032 | if (write(fd, buf, len) < 0) { | |
8033 | wpa_printf(MSG_DEBUG, | |
8034 | "nl80211: Failed to write Linux system file: %s with the value of %d", | |
8035 | path, val); | |
8036 | close(fd); | |
8037 | return -1; | |
8038 | } | |
8039 | close(fd); | |
8040 | ||
8041 | return 0; | |
8042 | } | |
8043 | ||
8044 | ||
8045 | static const char * drv_br_port_attr_str(enum drv_br_port_attr attr) | |
8046 | { | |
8047 | switch (attr) { | |
8048 | case DRV_BR_PORT_ATTR_PROXYARP: | |
8049 | return "proxyarp"; | |
8050 | case DRV_BR_PORT_ATTR_HAIRPIN_MODE: | |
8051 | return "hairpin_mode"; | |
8052 | } | |
8053 | ||
8054 | return NULL; | |
8055 | } | |
8056 | ||
8057 | ||
8058 | static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr, | |
8059 | unsigned int val) | |
8060 | { | |
8061 | struct i802_bss *bss = priv; | |
8062 | char path[128]; | |
8063 | const char *attr_txt; | |
8064 | ||
8065 | attr_txt = drv_br_port_attr_str(attr); | |
8066 | if (attr_txt == NULL) | |
8067 | return -EINVAL; | |
8068 | ||
8069 | os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s", | |
8070 | bss->ifname, attr_txt); | |
8071 | ||
8072 | if (linux_write_system_file(path, val)) | |
8073 | return -1; | |
8074 | ||
8075 | return 0; | |
8076 | } | |
8077 | ||
8078 | ||
7565752d KP |
8079 | static const char * drv_br_net_param_str(enum drv_br_net_param param) |
8080 | { | |
8081 | switch (param) { | |
8082 | case DRV_BR_NET_PARAM_GARP_ACCEPT: | |
8083 | return "arp_accept"; | |
8084 | } | |
8085 | ||
8086 | return NULL; | |
8087 | } | |
8088 | ||
8089 | ||
8090 | static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param, | |
8091 | unsigned int val) | |
8092 | { | |
8093 | struct i802_bss *bss = priv; | |
8094 | char path[128]; | |
8095 | const char *param_txt; | |
8096 | int ip_version = 4; | |
8097 | ||
8098 | param_txt = drv_br_net_param_str(param); | |
8099 | if (param_txt == NULL) | |
8100 | return -EINVAL; | |
8101 | ||
8102 | switch (param) { | |
8103 | case DRV_BR_NET_PARAM_GARP_ACCEPT: | |
8104 | ip_version = 4; | |
8105 | break; | |
8106 | default: | |
8107 | return -EINVAL; | |
8108 | } | |
8109 | ||
8110 | os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s", | |
8111 | ip_version, bss->brname, param_txt); | |
8112 | ||
8113 | if (linux_write_system_file(path, val)) | |
8114 | return -1; | |
8115 | ||
8116 | return 0; | |
8117 | } | |
8118 | ||
8119 | ||
16689c7c PX |
8120 | static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode) |
8121 | { | |
8122 | switch (hw_mode) { | |
8123 | case HOSTAPD_MODE_IEEE80211B: | |
8124 | return QCA_ACS_MODE_IEEE80211B; | |
8125 | case HOSTAPD_MODE_IEEE80211G: | |
8126 | return QCA_ACS_MODE_IEEE80211G; | |
8127 | case HOSTAPD_MODE_IEEE80211A: | |
8128 | return QCA_ACS_MODE_IEEE80211A; | |
8129 | case HOSTAPD_MODE_IEEE80211AD: | |
8130 | return QCA_ACS_MODE_IEEE80211AD; | |
8131 | default: | |
8132 | return -1; | |
8133 | } | |
8134 | } | |
8135 | ||
8136 | ||
8137 | static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params) | |
8138 | { | |
8139 | struct i802_bss *bss = priv; | |
8140 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
8141 | struct nl_msg *msg; | |
8142 | struct nlattr *data; | |
a862e4a3 | 8143 | int ret; |
16689c7c PX |
8144 | int mode; |
8145 | ||
8146 | mode = hw_mode_to_qca_acs(params->hw_mode); | |
8147 | if (mode < 0) | |
8148 | return -1; | |
8149 | ||
9725b784 | 8150 | if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) || |
a862e4a3 JM |
8151 | nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) || |
8152 | nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, | |
8153 | QCA_NL80211_VENDOR_SUBCMD_DO_ACS) || | |
8154 | !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) || | |
8155 | nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) || | |
8156 | (params->ht_enabled && | |
8157 | nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) || | |
8158 | (params->ht40_enabled && | |
8159 | nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED))) { | |
8160 | nlmsg_free(msg); | |
8161 | return -ENOBUFS; | |
8162 | } | |
16689c7c PX |
8163 | nla_nest_end(msg, data); |
8164 | ||
8165 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
16689c7c PX |
8166 | if (ret) { |
8167 | wpa_printf(MSG_DEBUG, | |
8168 | "nl80211: Failed to invoke driver ACS function: %s", | |
8169 | strerror(errno)); | |
8170 | } | |
16689c7c PX |
8171 | return ret; |
8172 | } | |
8173 | ||
8174 | ||
3f5285e8 JM |
8175 | const struct wpa_driver_ops wpa_driver_nl80211_ops = { |
8176 | .name = "nl80211", | |
8177 | .desc = "Linux nl80211/cfg80211", | |
8178 | .get_bssid = wpa_driver_nl80211_get_bssid, | |
8179 | .get_ssid = wpa_driver_nl80211_get_ssid, | |
9ebce9c5 JM |
8180 | .set_key = driver_nl80211_set_key, |
8181 | .scan2 = driver_nl80211_scan2, | |
d21c63b9 LC |
8182 | .sched_scan = wpa_driver_nl80211_sched_scan, |
8183 | .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan, | |
3f5285e8 | 8184 | .get_scan_results2 = wpa_driver_nl80211_get_scan_results, |
9ebce9c5 JM |
8185 | .deauthenticate = driver_nl80211_deauthenticate, |
8186 | .authenticate = driver_nl80211_authenticate, | |
3f5285e8 | 8187 | .associate = wpa_driver_nl80211_associate, |
f2ed8023 JM |
8188 | .global_init = nl80211_global_init, |
8189 | .global_deinit = nl80211_global_deinit, | |
8190 | .init2 = wpa_driver_nl80211_init, | |
9ebce9c5 | 8191 | .deinit = driver_nl80211_deinit, |
3f5285e8 JM |
8192 | .get_capa = wpa_driver_nl80211_get_capa, |
8193 | .set_operstate = wpa_driver_nl80211_set_operstate, | |
01652550 | 8194 | .set_supp_port = wpa_driver_nl80211_set_supp_port, |
6d158490 | 8195 | .set_country = wpa_driver_nl80211_set_country, |
f0793bf1 | 8196 | .get_country = wpa_driver_nl80211_get_country, |
19c3b566 | 8197 | .set_ap = wpa_driver_nl80211_set_ap, |
3c4ca363 | 8198 | .set_acl = wpa_driver_nl80211_set_acl, |
22a7c9d7 | 8199 | .if_add = wpa_driver_nl80211_if_add, |
9ebce9c5 JM |
8200 | .if_remove = driver_nl80211_if_remove, |
8201 | .send_mlme = driver_nl80211_send_mlme, | |
0fafeb54 | 8202 | .get_hw_feature_data = nl80211_get_hw_feature_data, |
0f4e8b4f | 8203 | .sta_add = wpa_driver_nl80211_sta_add, |
9ebce9c5 | 8204 | .sta_remove = driver_nl80211_sta_remove, |
db149ac9 | 8205 | .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol, |
a8d6ffa4 | 8206 | .sta_set_flags = wpa_driver_nl80211_sta_set_flags, |
c5121837 | 8207 | .hapd_init = i802_init, |
c5121837 | 8208 | .hapd_deinit = i802_deinit, |
f7b3920c | 8209 | .set_wds_sta = i802_set_wds_sta, |
c5121837 JM |
8210 | .get_seqnum = i802_get_seqnum, |
8211 | .flush = i802_flush, | |
c5121837 JM |
8212 | .get_inact_sec = i802_get_inact_sec, |
8213 | .sta_clear_stats = i802_sta_clear_stats, | |
c5121837 JM |
8214 | .set_rts = i802_set_rts, |
8215 | .set_frag = i802_set_frag, | |
c5121837 | 8216 | .set_tx_queue_params = i802_set_tx_queue_params, |
9ebce9c5 | 8217 | .set_sta_vlan = driver_nl80211_set_sta_vlan, |
ee7ab173 JB |
8218 | .sta_deauth = i802_sta_deauth, |
8219 | .sta_disassoc = i802_sta_disassoc, | |
9ebce9c5 | 8220 | .read_sta_data = driver_nl80211_read_sta_data, |
e3802622 | 8221 | .set_freq = i802_set_freq, |
9ebce9c5 | 8222 | .send_action = driver_nl80211_send_action, |
5dfca53f | 8223 | .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait, |
55777702 JM |
8224 | .remain_on_channel = wpa_driver_nl80211_remain_on_channel, |
8225 | .cancel_remain_on_channel = | |
8226 | wpa_driver_nl80211_cancel_remain_on_channel, | |
9ebce9c5 | 8227 | .probe_req_report = driver_nl80211_probe_req_report, |
af473088 | 8228 | .deinit_ap = wpa_driver_nl80211_deinit_ap, |
3c29244e | 8229 | .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli, |
207ef3fb | 8230 | .resume = wpa_driver_nl80211_resume, |
b625473c | 8231 | .signal_monitor = nl80211_signal_monitor, |
1c5c7273 | 8232 | .signal_poll = nl80211_signal_poll, |
b91ab76e | 8233 | .send_frame = nl80211_send_frame, |
57ebba59 | 8234 | .shared_freq = wpa_driver_nl80211_shared_freq, |
c55f774d | 8235 | .set_param = nl80211_set_param, |
6859f1cb | 8236 | .get_radio_name = nl80211_get_radio_name, |
a6efc65d JM |
8237 | .add_pmkid = nl80211_add_pmkid, |
8238 | .remove_pmkid = nl80211_remove_pmkid, | |
8239 | .flush_pmkid = nl80211_flush_pmkid, | |
b14a210c | 8240 | .set_rekey_info = nl80211_set_rekey_info, |
bcf24348 | 8241 | .poll_client = nl80211_poll_client, |
29f338af | 8242 | .set_p2p_powersave = nl80211_set_p2p_powersave, |
f90e9c1c | 8243 | .start_dfs_cac = nl80211_start_radar_detection, |
695c7038 | 8244 | .stop_ap = wpa_driver_nl80211_stop_ap, |
03ea1786 AN |
8245 | #ifdef CONFIG_TDLS |
8246 | .send_tdls_mgmt = nl80211_send_tdls_mgmt, | |
8247 | .tdls_oper = nl80211_tdls_oper, | |
8248 | #endif /* CONFIG_TDLS */ | |
6a1ce395 | 8249 | .update_ft_ies = wpa_driver_nl80211_update_ft_ies, |
597b94f5 | 8250 | .get_mac_addr = wpa_driver_nl80211_get_macaddr, |
0185007c | 8251 | .get_survey = wpa_driver_nl80211_get_survey, |
a771c07d | 8252 | .status = wpa_driver_nl80211_status, |
1c4ffa87 | 8253 | .switch_channel = nl80211_switch_channel, |
0de38036 JM |
8254 | #ifdef ANDROID_P2P |
8255 | .set_noa = wpa_driver_set_p2p_noa, | |
8256 | .get_noa = wpa_driver_get_p2p_noa, | |
8257 | .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie, | |
8258 | #endif /* ANDROID_P2P */ | |
5e2c3490 JM |
8259 | #ifdef ANDROID |
8260 | .driver_cmd = wpa_driver_nl80211_driver_cmd, | |
8261 | #endif /* ANDROID */ | |
adef8948 | 8262 | .vendor_cmd = nl80211_vendor_cmd, |
049105b4 | 8263 | .set_qos_map = nl80211_set_qos_map, |
e4fa8b12 | 8264 | .set_wowlan = nl80211_set_wowlan, |
0800f9ee | 8265 | .roaming = nl80211_roaming, |
fee354c7 | 8266 | .set_mac_addr = nl80211_set_mac_addr, |
6c1664f6 BC |
8267 | #ifdef CONFIG_MESH |
8268 | .init_mesh = wpa_driver_nl80211_init_mesh, | |
8269 | .join_mesh = wpa_driver_nl80211_join_mesh, | |
8270 | .leave_mesh = wpa_driver_nl80211_leave_mesh, | |
8271 | #endif /* CONFIG_MESH */ | |
71103bed KP |
8272 | .br_add_ip_neigh = wpa_driver_br_add_ip_neigh, |
8273 | .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh, | |
73d2294f | 8274 | .br_port_set_attr = wpa_driver_br_port_set_attr, |
7565752d | 8275 | .br_set_net_param = wpa_driver_br_set_net_param, |
dfa87878 MB |
8276 | .add_tx_ts = nl80211_add_ts, |
8277 | .del_tx_ts = nl80211_del_ts, | |
16689c7c | 8278 | .do_acs = wpa_driver_do_acs, |
3f5285e8 | 8279 | }; |