hostapd: fix more dynamic reload issues
[openwrt/openwrt.git] / package / network / services / hostapd / patches / 601-ucode_support.patch
1 --- a/hostapd/Makefile
2 +++ b/hostapd/Makefile
3 @@ -168,9 +168,21 @@ OBJS += ../src/eapol_auth/eapol_auth_sm.
4
5 ifdef CONFIG_UBUS
6 CFLAGS += -DUBUS_SUPPORT
7 -OBJS += ../src/utils/uloop.o
8 OBJS += ../src/ap/ubus.o
9 -LIBS += -lubox -lubus
10 +LIBS += -lubus
11 +NEED_ULOOP:=y
12 +endif
13 +
14 +ifdef CONFIG_UCODE
15 +CFLAGS += -DUCODE_SUPPORT
16 +OBJS += ../src/utils/ucode.o
17 +OBJS += ../src/ap/ucode.o
18 +NEED_ULOOP:=y
19 +endif
20 +
21 +ifdef NEED_ULOOP
22 +OBJS += ../src/utils/uloop.o
23 +LIBS += -lubox
24 endif
25
26 ifdef CONFIG_CODE_COVERAGE
27 --- a/hostapd/main.c
28 +++ b/hostapd/main.c
29 @@ -1007,6 +1007,7 @@ int main(int argc, char *argv[])
30 }
31
32 hostapd_global_ctrl_iface_init(&interfaces);
33 + hostapd_ucode_init(&interfaces);
34
35 if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
36 wpa_printf(MSG_ERROR, "Failed to start eloop");
37 @@ -1016,6 +1017,7 @@ int main(int argc, char *argv[])
38 ret = 0;
39
40 out:
41 + hostapd_ucode_free();
42 hostapd_global_ctrl_iface_deinit(&interfaces);
43 /* Deinitialize all interfaces */
44 for (i = 0; i < interfaces.count; i++) {
45 --- a/src/ap/hostapd.h
46 +++ b/src/ap/hostapd.h
47 @@ -19,6 +19,7 @@
48 #include "ap_config.h"
49 #include "drivers/driver.h"
50 #include "ubus.h"
51 +#include "ucode.h"
52
53 #define OCE_STA_CFON_ENABLED(hapd) \
54 ((hapd->conf->oce & OCE_STA_CFON) && \
55 @@ -51,6 +52,10 @@ struct hapd_interfaces {
56 struct hostapd_config * (*config_read_cb)(const char *config_fname);
57 int (*ctrl_iface_init)(struct hostapd_data *hapd);
58 void (*ctrl_iface_deinit)(struct hostapd_data *hapd);
59 + int (*ctrl_iface_recv)(struct hostapd_data *hapd,
60 + char *buf, char *reply, int reply_size,
61 + struct sockaddr_storage *from,
62 + socklen_t fromlen);
63 int (*for_each_interface)(struct hapd_interfaces *interfaces,
64 int (*cb)(struct hostapd_iface *iface,
65 void *ctx), void *ctx);
66 @@ -186,6 +191,7 @@ struct hostapd_data {
67 struct hostapd_config *iconf;
68 struct hostapd_bss_config *conf;
69 struct hostapd_ubus_bss ubus;
70 + struct hostapd_ucode_bss ucode;
71 int interface_added; /* virtual interface added for this BSS */
72 unsigned int started:1;
73 unsigned int disabled:1;
74 @@ -506,6 +512,7 @@ struct hostapd_sta_info {
75 */
76 struct hostapd_iface {
77 struct hapd_interfaces *interfaces;
78 + struct hostapd_ucode_iface ucode;
79 void *owner;
80 char *config_fname;
81 struct hostapd_config *conf;
82 @@ -706,6 +713,8 @@ struct hostapd_iface * hostapd_init(stru
83 struct hostapd_iface *
84 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
85 const char *config_fname, int debug);
86 +int hostapd_setup_bss(struct hostapd_data *hapd, int first, bool start_beacon);
87 +void hostapd_bss_deinit(struct hostapd_data *hapd);
88 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
89 int reassoc);
90 void hostapd_interface_deinit_free(struct hostapd_iface *iface);
91 --- a/src/ap/hostapd.c
92 +++ b/src/ap/hostapd.c
93 @@ -252,6 +252,8 @@ int hostapd_reload_config(struct hostapd
94 struct hostapd_config *newconf, *oldconf;
95 size_t j;
96
97 + hostapd_ucode_reload_bss(hapd);
98 +
99 if (iface->config_fname == NULL) {
100 /* Only in-memory config in use - assume it has been updated */
101 hostapd_clear_old(iface);
102 @@ -435,6 +437,7 @@ void hostapd_free_hapd_data(struct hosta
103 hapd->beacon_set_done = 0;
104
105 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
106 + hostapd_ucode_free_bss(hapd);
107 hostapd_ubus_free_bss(hapd);
108 accounting_deinit(hapd);
109 hostapd_deinit_wpa(hapd);
110 @@ -599,6 +602,7 @@ void hostapd_cleanup_iface_partial(struc
111 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
112 {
113 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
114 + hostapd_ucode_free_iface(iface);
115 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
116 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
117 NULL);
118 @@ -1189,6 +1193,7 @@ static int hostapd_start_beacon(struct h
119 hapd->driver->set_operstate(hapd->drv_priv, 1);
120
121 hostapd_ubus_add_bss(hapd);
122 + hostapd_ucode_add_bss(hapd);
123
124 return 0;
125 }
126 @@ -1211,8 +1216,7 @@ static int hostapd_start_beacon(struct h
127 * initialized. Most of the modules that are initialized here will be
128 * deinitialized in hostapd_cleanup().
129 */
130 -static int hostapd_setup_bss(struct hostapd_data *hapd, int first,
131 - bool start_beacon)
132 +int hostapd_setup_bss(struct hostapd_data *hapd, int first, bool start_beacon)
133 {
134 struct hostapd_bss_config *conf = hapd->conf;
135 u8 ssid[SSID_MAX_LEN + 1];
136 @@ -2698,7 +2702,7 @@ hostapd_alloc_bss_data(struct hostapd_if
137 }
138
139
140 -static void hostapd_bss_deinit(struct hostapd_data *hapd)
141 +void hostapd_bss_deinit(struct hostapd_data *hapd)
142 {
143 if (!hapd)
144 return;
145 --- a/wpa_supplicant/Makefile
146 +++ b/wpa_supplicant/Makefile
147 @@ -195,8 +195,20 @@ endif
148 ifdef CONFIG_UBUS
149 CFLAGS += -DUBUS_SUPPORT
150 OBJS += ubus.o
151 +LIBS += -lubus
152 +NEED_ULOOP:=y
153 +endif
154 +
155 +ifdef CONFIG_UCODE
156 +CFLAGS += -DUCODE_SUPPORT
157 +OBJS += ../src/utils/ucode.o
158 +OBJS += ucode.o
159 +NEED_ULOOP:=y
160 +endif
161 +
162 +ifdef NEED_ULOOP
163 OBJS += ../src/utils/uloop.o
164 -LIBS += -lubox -lubus
165 +LIBS += -lubox
166 endif
167
168 ifdef CONFIG_CODE_COVERAGE
169 @@ -997,6 +1009,9 @@ OBJS += ../src/ap/ctrl_iface_ap.o
170 ifdef CONFIG_UBUS
171 OBJS += ../src/ap/ubus.o
172 endif
173 +ifdef CONFIG_UCODE
174 +OBJS += ../src/ap/ucode.o
175 +endif
176 endif
177
178 CFLAGS += -DEAP_SERVER -DEAP_SERVER_IDENTITY
179 --- a/wpa_supplicant/wpa_supplicant.c
180 +++ b/wpa_supplicant/wpa_supplicant.c
181 @@ -1044,6 +1044,7 @@ void wpa_supplicant_set_state(struct wpa
182 sme_sched_obss_scan(wpa_s, 0);
183 }
184 wpa_s->wpa_state = state;
185 + wpas_ucode_update_state(wpa_s);
186
187 #ifdef CONFIG_BGSCAN
188 if (state == WPA_COMPLETED && wpa_s->current_ssid != wpa_s->bgscan_ssid)
189 @@ -7594,6 +7595,7 @@ struct wpa_supplicant * wpa_supplicant_a
190 #endif /* CONFIG_P2P */
191
192 wpas_ubus_add_bss(wpa_s);
193 + wpas_ucode_add_bss(wpa_s);
194
195 return wpa_s;
196 }
197 @@ -7621,6 +7623,7 @@ int wpa_supplicant_remove_iface(struct w
198 struct wpa_supplicant *parent = wpa_s->parent;
199 #endif /* CONFIG_MESH */
200
201 + wpas_ucode_free_bss(wpa_s);
202 wpas_ubus_free_bss(wpa_s);
203
204 /* Remove interface from the global list of interfaces */
205 @@ -7931,6 +7934,7 @@ struct wpa_global * wpa_supplicant_init(
206
207 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
208 wpas_periodic, global, NULL);
209 + wpas_ucode_init(global);
210
211 return global;
212 }
213 @@ -7969,12 +7973,8 @@ int wpa_supplicant_run(struct wpa_global
214 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
215 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
216
217 - wpas_ubus_add(global);
218 -
219 eloop_run();
220
221 - wpas_ubus_free(global);
222 -
223 return 0;
224 }
225
226 @@ -8007,6 +8007,8 @@ void wpa_supplicant_deinit(struct wpa_gl
227
228 wpas_notify_supplicant_deinitialized(global);
229
230 + wpas_ucode_free();
231 +
232 eap_peer_unregister_methods();
233 #ifdef CONFIG_AP
234 eap_server_unregister_methods();
235 --- a/wpa_supplicant/wpa_supplicant_i.h
236 +++ b/wpa_supplicant/wpa_supplicant_i.h
237 @@ -22,6 +22,7 @@
238 #include "wmm_ac.h"
239 #include "pasn/pasn_common.h"
240 #include "ubus.h"
241 +#include "ucode.h"
242
243 extern const char *const wpa_supplicant_version;
244 extern const char *const wpa_supplicant_license;
245 @@ -689,6 +690,7 @@ struct wpa_supplicant {
246 unsigned char perm_addr[ETH_ALEN];
247 char ifname[100];
248 struct wpas_ubus_bss ubus;
249 + struct wpas_ucode_bss ucode;
250 #ifdef CONFIG_MATCH_IFACE
251 int matched;
252 #endif /* CONFIG_MATCH_IFACE */
253 --- a/hostapd/ctrl_iface.c
254 +++ b/hostapd/ctrl_iface.c
255 @@ -4856,6 +4856,7 @@ try_again:
256 return -1;
257 }
258
259 + interface->ctrl_iface_recv = hostapd_ctrl_iface_receive_process;
260 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
261
262 return 0;
263 @@ -4957,6 +4958,7 @@ fail:
264 os_free(fname);
265
266 interface->global_ctrl_sock = s;
267 + interface->ctrl_iface_recv = hostapd_ctrl_iface_receive_process;
268 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
269 interface, NULL);
270
271 --- a/src/drivers/driver.h
272 +++ b/src/drivers/driver.h
273 @@ -3787,6 +3787,25 @@ struct wpa_driver_ops {
274 const char *ifname);
275
276 /**
277 + * if_rename - Rename a virtual interface
278 + * @priv: Private driver interface data
279 + * @type: Interface type
280 + * @ifname: Interface name of the virtual interface to be renamed
281 + * (NULL when renaming the AP BSS interface)
282 + * @new_name: New interface name of the virtual interface
283 + * Returns: 0 on success, -1 on failure
284 + */
285 + int (*if_rename)(void *priv, enum wpa_driver_if_type type,
286 + const char *ifname, const char *new_name);
287 +
288 + /**
289 + * set_first_bss - Make a virtual interface the first (primary) bss
290 + * @priv: Private driver interface data
291 + * Returns: 0 on success, -1 on failure
292 + */
293 + int (*set_first_bss)(void *priv);
294 +
295 + /**
296 * set_sta_vlan - Bind a station into a specific interface (AP only)
297 * @priv: Private driver interface data
298 * @ifname: Interface (main or virtual BSS or VLAN)
299 @@ -6440,6 +6459,7 @@ union wpa_event_data {
300
301 /**
302 * struct ch_switch
303 + * @count: Count until channel switch activates
304 * @freq: Frequency of new channel in MHz
305 * @ht_enabled: Whether this is an HT channel
306 * @ch_offset: Secondary channel offset
307 @@ -6450,6 +6470,7 @@ union wpa_event_data {
308 * @punct_bitmap: Puncturing bitmap
309 */
310 struct ch_switch {
311 + int count;
312 int freq;
313 int ht_enabled;
314 int ch_offset;
315 --- a/src/drivers/driver_nl80211_event.c
316 +++ b/src/drivers/driver_nl80211_event.c
317 @@ -1202,6 +1202,7 @@ static void mlme_event_ch_switch(struct
318 struct nlattr *bw, struct nlattr *cf1,
319 struct nlattr *cf2,
320 struct nlattr *punct_bitmap,
321 + struct nlattr *count,
322 int finished)
323 {
324 struct i802_bss *bss;
325 @@ -1265,6 +1266,8 @@ static void mlme_event_ch_switch(struct
326 data.ch_switch.cf1 = nla_get_u32(cf1);
327 if (cf2)
328 data.ch_switch.cf2 = nla_get_u32(cf2);
329 + if (count)
330 + data.ch_switch.count = nla_get_u32(count);
331
332 if (finished)
333 bss->flink->freq = data.ch_switch.freq;
334 @@ -3912,6 +3915,7 @@ static void do_process_drv_event(struct
335 tb[NL80211_ATTR_CENTER_FREQ1],
336 tb[NL80211_ATTR_CENTER_FREQ2],
337 tb[NL80211_ATTR_PUNCT_BITMAP],
338 + tb[NL80211_ATTR_CH_SWITCH_COUNT],
339 0);
340 break;
341 case NL80211_CMD_CH_SWITCH_NOTIFY:
342 @@ -3924,6 +3928,7 @@ static void do_process_drv_event(struct
343 tb[NL80211_ATTR_CENTER_FREQ1],
344 tb[NL80211_ATTR_CENTER_FREQ2],
345 tb[NL80211_ATTR_PUNCT_BITMAP],
346 + NULL,
347 1);
348 break;
349 case NL80211_CMD_DISCONNECT:
350 --- a/wpa_supplicant/events.c
351 +++ b/wpa_supplicant/events.c
352 @@ -5389,6 +5389,7 @@ void supplicant_event(void *ctx, enum wp
353 event_to_string(event), event);
354 #endif /* CONFIG_NO_STDOUT_DEBUG */
355
356 + wpas_ucode_event(wpa_s, event, data);
357 switch (event) {
358 case EVENT_AUTH:
359 #ifdef CONFIG_FST
360 --- a/src/ap/ap_drv_ops.h
361 +++ b/src/ap/ap_drv_ops.h
362 @@ -393,6 +393,23 @@ static inline int hostapd_drv_stop_ap(st
363 return hapd->driver->stop_ap(hapd->drv_priv);
364 }
365
366 +static inline int hostapd_drv_if_rename(struct hostapd_data *hapd,
367 + enum wpa_driver_if_type type,
368 + const char *ifname,
369 + const char *new_name)
370 +{
371 + if (!hapd->driver || !hapd->driver->if_rename || !hapd->drv_priv)
372 + return -1;
373 + return hapd->driver->if_rename(hapd->drv_priv, type, ifname, new_name);
374 +}
375 +
376 +static inline int hostapd_drv_set_first_bss(struct hostapd_data *hapd)
377 +{
378 + if (!hapd->driver || !hapd->driver->set_first_bss || !hapd->drv_priv)
379 + return 0;
380 + return hapd->driver->set_first_bss(hapd->drv_priv);
381 +}
382 +
383 static inline int hostapd_drv_channel_info(struct hostapd_data *hapd,
384 struct wpa_channel_info *ci)
385 {
386 --- a/src/drivers/driver_nl80211.c
387 +++ b/src/drivers/driver_nl80211.c
388 @@ -1333,7 +1333,7 @@ static void wpa_driver_nl80211_event_rtm
389 }
390 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
391 namebuf, ifname);
392 - if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
393 + if (drv->first_bss->ifindex != ifi->ifi_index) {
394 wpa_printf(MSG_DEBUG,
395 "nl80211: Not the main interface (%s) - do not indicate interface down",
396 drv->first_bss->ifname);
397 @@ -1369,7 +1369,7 @@ static void wpa_driver_nl80211_event_rtm
398 }
399 wpa_printf(MSG_DEBUG, "nl80211: Interface up (%s/%s)",
400 namebuf, ifname);
401 - if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
402 + if (drv->first_bss->ifindex != ifi->ifi_index) {
403 wpa_printf(MSG_DEBUG,
404 "nl80211: Not the main interface (%s) - do not indicate interface up",
405 drv->first_bss->ifname);
406 @@ -8432,6 +8432,7 @@ static void *i802_init(struct hostapd_da
407 char master_ifname[IFNAMSIZ];
408 int ifindex, br_ifindex = 0;
409 int br_added = 0;
410 + int err;
411
412 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
413 params->global_priv, 1,
414 @@ -8491,21 +8492,17 @@ static void *i802_init(struct hostapd_da
415 (params->num_bridge == 0 || !params->bridge[0]))
416 add_ifidx(drv, br_ifindex, drv->ifindex);
417
418 - if (bss->added_if_into_bridge || bss->already_in_bridge) {
419 - int err;
420 -
421 - drv->rtnl_sk = nl_socket_alloc();
422 - if (drv->rtnl_sk == NULL) {
423 - wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
424 - goto failed;
425 - }
426 + drv->rtnl_sk = nl_socket_alloc();
427 + if (drv->rtnl_sk == NULL) {
428 + wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
429 + goto failed;
430 + }
431
432 - err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
433 - if (err) {
434 - wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
435 - nl_geterror(err));
436 - goto failed;
437 - }
438 + err = nl_connect(drv->rtnl_sk, NETLINK_ROUTE);
439 + if (err) {
440 + wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
441 + nl_geterror(err));
442 + goto failed;
443 }
444
445 if (drv->capa.flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX) {
446 @@ -8875,6 +8872,50 @@ static int wpa_driver_nl80211_if_remove(
447 return 0;
448 }
449
450 +static int wpa_driver_nl80211_if_rename(struct i802_bss *bss,
451 + enum wpa_driver_if_type type,
452 + const char *ifname, const char *new_name)
453 +{
454 + struct wpa_driver_nl80211_data *drv = bss->drv;
455 + struct ifinfomsg ifi = {
456 + .ifi_family = AF_UNSPEC,
457 + .ifi_index = bss->ifindex,
458 + };
459 + struct nl_msg *msg;
460 + int res = -ENOMEM;
461 +
462 + if (ifname)
463 + ifi.ifi_index = if_nametoindex(ifname);
464 +
465 + msg = nlmsg_alloc_simple(RTM_SETLINK, 0);
466 + if (!msg)
467 + return res;
468 +
469 + if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0)
470 + goto out;
471 +
472 + if (nla_put_string(msg, IFLA_IFNAME, new_name))
473 + goto out;
474 +
475 + res = nl_send_auto_complete(drv->rtnl_sk, msg);
476 + if (res < 0)
477 + goto out;
478 +
479 + res = nl_wait_for_ack(drv->rtnl_sk);
480 + if (res) {
481 + wpa_printf(MSG_INFO,
482 + "nl80211: Renaming device %s to %s failed: %s",
483 + ifname ? ifname : bss->ifname, new_name, nl_geterror(res));
484 + goto out;
485 + }
486 +
487 + if (type == WPA_IF_AP_BSS && !ifname)
488 + os_strlcpy(bss->ifname, new_name, sizeof(bss->ifname));
489 +
490 +out:
491 + nlmsg_free(msg);
492 + return res;
493 +}
494
495 static int cookie_handler(struct nl_msg *msg, void *arg)
496 {
497 @@ -10513,6 +10554,37 @@ static int driver_nl80211_if_remove(void
498 }
499
500
501 +static int driver_nl80211_if_rename(void *priv, enum wpa_driver_if_type type,
502 + const char *ifname, const char *new_name)
503 +{
504 + struct i802_bss *bss = priv;
505 + return wpa_driver_nl80211_if_rename(bss, type, ifname, new_name);
506 +}
507 +
508 +
509 +static int driver_nl80211_set_first_bss(void *priv)
510 +{
511 + struct i802_bss *bss = priv, *tbss;
512 + struct wpa_driver_nl80211_data *drv = bss->drv;
513 +
514 + if (drv->first_bss == bss)
515 + return 0;
516 +
517 + for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
518 + if (tbss->next != bss)
519 + continue;
520 +
521 + tbss->next = bss->next;
522 + bss->next = drv->first_bss;
523 + drv->first_bss = bss;
524 + drv->ctx = bss->ctx;
525 + return 0;
526 + }
527 +
528 + return -1;
529 +}
530 +
531 +
532 static int driver_nl80211_send_mlme(void *priv, const u8 *data,
533 size_t data_len, int noack,
534 unsigned int freq,
535 @@ -13697,6 +13769,8 @@ const struct wpa_driver_ops wpa_driver_n
536 .set_acl = wpa_driver_nl80211_set_acl,
537 .if_add = wpa_driver_nl80211_if_add,
538 .if_remove = driver_nl80211_if_remove,
539 + .if_rename = driver_nl80211_if_rename,
540 + .set_first_bss = driver_nl80211_set_first_bss,
541 .send_mlme = driver_nl80211_send_mlme,
542 .get_hw_feature_data = nl80211_get_hw_feature_data,
543 .sta_add = wpa_driver_nl80211_sta_add,