reconfig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * hostapd / Configuration reloading
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2002-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include "hostapd.h"
  18. #include "beacon.h"
  19. #include "hw_features.h"
  20. #include "driver.h"
  21. #include "sta_info.h"
  22. #include "radius/radius_client.h"
  23. #include "ieee802_11.h"
  24. #include "iapp.h"
  25. #include "ap_list.h"
  26. #include "wpa.h"
  27. #include "vlan_init.h"
  28. #include "ieee802_11_auth.h"
  29. #include "ieee802_1x.h"
  30. #include "accounting.h"
  31. #include "eloop.h"
  32. /**
  33. * struct hostapd_config_change - Configuration change information
  34. * This is for two purposes:
  35. * - Storing configuration information in the hostapd_iface during
  36. * the asynchronous parts of reconfiguration.
  37. * - Passing configuration information for per-station reconfiguration.
  38. */
  39. struct hostapd_config_change {
  40. struct hostapd_data *hapd;
  41. struct hostapd_config *newconf, *oldconf;
  42. struct hostapd_bss_config *newbss, *oldbss;
  43. int mac_acl_changed;
  44. int num_sta_remove; /* number of STAs that need to be removed */
  45. int beacon_changed;
  46. struct hostapd_iface *hapd_iface;
  47. struct hostapd_data **new_hapd, **old_hapd;
  48. int num_old_hapd;
  49. };
  50. static int hostapd_config_reload_sta(struct hostapd_data *hapd,
  51. struct sta_info *sta, void *data)
  52. {
  53. struct hostapd_config_change *change = data;
  54. struct hostapd_bss_config *newbss, *oldbss;
  55. int deauth = 0;
  56. u8 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
  57. newbss = change->newbss;
  58. oldbss = change->oldbss;
  59. hapd = change->hapd;
  60. if (sta->ssid == &oldbss->ssid) {
  61. sta->ssid = &newbss->ssid;
  62. if (newbss->ssid.ssid_len != oldbss->ssid.ssid_len ||
  63. os_memcmp(newbss->ssid.ssid, oldbss->ssid.ssid,
  64. newbss->ssid.ssid_len) != 0) {
  65. /* main SSID was changed - kick STA out */
  66. deauth++;
  67. }
  68. }
  69. sta->ssid_probe = sta->ssid;
  70. /*
  71. * If MAC ACL configuration has changed, deauthenticate stations that
  72. * have been removed from accepted list or have been added to denied
  73. * list. If external RADIUS server is used for ACL, all stations are
  74. * deauthenticated and they will need to authenticate again. This
  75. * limits sudden load on the RADIUS server since the verification will
  76. * be done over the time needed for the STAs to reauthenticate
  77. * themselves.
  78. */
  79. if (change->mac_acl_changed &&
  80. (newbss->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH ||
  81. !hostapd_allowed_address(hapd, sta->addr, NULL, 0, NULL, NULL,
  82. NULL)))
  83. deauth++;
  84. if (newbss->ieee802_1x != oldbss->ieee802_1x &&
  85. sta->ssid == &hapd->conf->ssid)
  86. deauth++;
  87. if (newbss->wpa != oldbss->wpa)
  88. deauth++;
  89. if (!newbss->wme_enabled && (sta->flags & WLAN_STA_WME))
  90. deauth++;
  91. if (newbss->auth_algs != oldbss->auth_algs &&
  92. ((sta->auth_alg == WLAN_AUTH_OPEN &&
  93. !(newbss->auth_algs & WPA_AUTH_ALG_OPEN)) ||
  94. (sta->auth_alg == WLAN_AUTH_SHARED_KEY &&
  95. !(newbss->auth_algs & WPA_AUTH_ALG_SHARED))))
  96. deauth++;
  97. if (change->num_sta_remove > 0) {
  98. deauth++;
  99. reason = WLAN_REASON_DISASSOC_AP_BUSY;
  100. }
  101. if (deauth) {
  102. wpa_printf(MSG_DEBUG, "STA " MACSTR " deauthenticated during "
  103. "config reloading (reason=%d)",
  104. MAC2STR(sta->addr), reason);
  105. ieee802_11_send_deauth(hapd, sta->addr, reason);
  106. ap_sta_deauthenticate(hapd, sta, reason);
  107. change->num_sta_remove--;
  108. }
  109. return 0;
  110. }
  111. static void hostapd_reconfig_tx_queue_params(struct hostapd_data *hapd,
  112. struct hostapd_config *newconf,
  113. struct hostapd_config *oldconf)
  114. {
  115. int i;
  116. struct hostapd_tx_queue_params *o, *n;
  117. for (i = 0; i < NUM_TX_QUEUES; i++) {
  118. o = &oldconf->tx_queue[i];
  119. n = &newconf->tx_queue[i];
  120. if (!n->configured)
  121. continue;
  122. if ((n->aifs != o->aifs || n->cwmin != o->cwmin ||
  123. n->cwmax != o->cwmax || n->burst != o->burst) &&
  124. hostapd_set_tx_queue_params(hapd, i, n->aifs, n->cwmin,
  125. n->cwmax, n->burst))
  126. printf("Failed to set TX queue parameters for queue %d"
  127. ".\n", i);
  128. }
  129. }
  130. static int hostapd_reconfig_wme(struct hostapd_data *hapd,
  131. struct hostapd_config *newconf,
  132. struct hostapd_config *oldconf)
  133. {
  134. int beacon_changed = 0;
  135. size_t i;
  136. struct hostapd_wme_ac_params *o, *n;
  137. for (i = 0; i < sizeof(newconf->wme_ac_params) /
  138. sizeof(newconf->wme_ac_params[0]); i++) {
  139. o = &oldconf->wme_ac_params[i];
  140. n = &newconf->wme_ac_params[i];
  141. if (n->cwmin != o->cwmin ||
  142. n->cwmax != o->cwmax ||
  143. n->aifs != o->aifs ||
  144. n->txopLimit != o->txopLimit ||
  145. n->admission_control_mandatory !=
  146. o->admission_control_mandatory) {
  147. beacon_changed++;
  148. hapd->parameter_set_count++;
  149. }
  150. }
  151. return beacon_changed;
  152. }
  153. static int rate_array_diff(int *a1, int *a2)
  154. {
  155. int i;
  156. if (a1 == NULL && a2 == NULL)
  157. return 0;
  158. if (a1 == NULL || a2 == NULL)
  159. return 1;
  160. i = 0;
  161. for (;;) {
  162. if (a1[i] != a2[i])
  163. return 1;
  164. if (a1[i] == -1)
  165. break;
  166. i++;
  167. }
  168. return 0;
  169. }
  170. static int hostapd_acl_diff(struct hostapd_bss_config *a,
  171. struct hostapd_bss_config *b)
  172. {
  173. int i;
  174. if (a->macaddr_acl != b->macaddr_acl ||
  175. a->num_accept_mac != b->num_accept_mac ||
  176. a->num_deny_mac != b->num_deny_mac)
  177. return 1;
  178. for (i = 0; i < a->num_accept_mac; i++) {
  179. if (os_memcmp(a->accept_mac[i], b->accept_mac[i], ETH_ALEN) !=
  180. 0)
  181. return 1;
  182. }
  183. for (i = 0; i < a->num_deny_mac; i++) {
  184. if (os_memcmp(a->deny_mac[i], b->deny_mac[i], ETH_ALEN) != 0)
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. /**
  190. * reload_iface2 - Part 2 of reload_iface
  191. * @hapd_iface: Pointer to hostapd interface data.
  192. */
  193. static void reload_iface2(struct hostapd_iface *hapd_iface)
  194. {
  195. struct hostapd_data *hapd = hapd_iface->bss[0];
  196. struct hostapd_config *newconf = hapd_iface->change->newconf;
  197. struct hostapd_config *oldconf = hapd_iface->change->oldconf;
  198. int beacon_changed = hapd_iface->change->beacon_changed;
  199. hostapd_iface_cb cb = hapd_iface->reload_iface_cb;
  200. if (newconf->preamble != oldconf->preamble) {
  201. if (hostapd_set_preamble(hapd, hapd->iconf->preamble))
  202. printf("Could not set preamble for kernel driver\n");
  203. beacon_changed++;
  204. }
  205. if (newconf->beacon_int != oldconf->beacon_int) {
  206. /* Need to change beacon interval if it has changed or if
  207. * auto channel selection was used. */
  208. if (hostapd_set_beacon_int(hapd, newconf->beacon_int))
  209. printf("Could not set beacon interval for kernel "
  210. "driver\n");
  211. if (newconf->beacon_int != oldconf->beacon_int)
  212. beacon_changed++;
  213. }
  214. if (newconf->cts_protection_type != oldconf->cts_protection_type)
  215. beacon_changed++;
  216. if (newconf->rts_threshold > -1 &&
  217. newconf->rts_threshold != oldconf->rts_threshold &&
  218. hostapd_set_rts(hapd, newconf->rts_threshold))
  219. printf("Could not set RTS threshold for kernel driver\n");
  220. if (newconf->fragm_threshold > -1 &&
  221. newconf->fragm_threshold != oldconf->fragm_threshold &&
  222. hostapd_set_frag(hapd, newconf->fragm_threshold))
  223. printf("Could not set fragmentation threshold for kernel "
  224. "driver\n");
  225. hostapd_reconfig_tx_queue_params(hapd, newconf, oldconf);
  226. if (hostapd_reconfig_wme(hapd, newconf, oldconf) > 0)
  227. beacon_changed++;
  228. ap_list_reconfig(hapd_iface, oldconf);
  229. hapd_iface->change->beacon_changed = beacon_changed;
  230. hapd_iface->reload_iface_cb = NULL;
  231. cb(hapd_iface, 0);
  232. }
  233. /**
  234. * reload_iface2_handler - Handler that calls reload_face2
  235. * @eloop_data: Stores the struct hostapd_iface for the interface.
  236. * @user_ctx: Unused.
  237. */
  238. static void reload_iface2_handler(void *eloop_data, void *user_ctx)
  239. {
  240. struct hostapd_iface *hapd_iface = eloop_data;
  241. reload_iface2(hapd_iface);
  242. }
  243. /**
  244. * reload_hw_mode_done - Callback for after the HW mode is setup
  245. * @hapd_iface: Pointer to interface data.
  246. * @status: Status of the HW mode setup.
  247. */
  248. static void reload_hw_mode_done(struct hostapd_iface *hapd_iface, int status)
  249. {
  250. struct hostapd_data *hapd = hapd_iface->bss[0];
  251. struct hostapd_config_change *change = hapd_iface->change;
  252. struct hostapd_config *newconf = change->newconf;
  253. hostapd_iface_cb cb;
  254. int freq;
  255. if (status) {
  256. printf("Failed to select hw_mode.\n");
  257. cb = hapd_iface->reload_iface_cb;
  258. hapd_iface->reload_iface_cb = NULL;
  259. cb(hapd_iface, -1);
  260. return;
  261. }
  262. freq = hostapd_hw_get_freq(hapd, newconf->channel);
  263. wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d Frequency: %d MHz",
  264. hostapd_hw_mode_txt(newconf->hw_mode),
  265. newconf->channel, freq);
  266. if (hostapd_set_freq(hapd, newconf->hw_mode, freq)) {
  267. printf("Could not set channel %d (%d MHz) for kernel "
  268. "driver\n", newconf->channel, freq);
  269. }
  270. change->beacon_changed++;
  271. reload_iface2(hapd_iface);
  272. }
  273. /**
  274. * hostapd_config_reload_iface_start - Start interface reload
  275. * @hapd_iface: Pointer to interface data.
  276. * @cb: The function to callback when done.
  277. * Returns: 0 if it starts successfully; cb will be called when done.
  278. * -1 on failure; cb will not be called.
  279. */
  280. static int hostapd_config_reload_iface_start(struct hostapd_iface *hapd_iface,
  281. hostapd_iface_cb cb)
  282. {
  283. struct hostapd_config_change *change = hapd_iface->change;
  284. struct hostapd_config *newconf = change->newconf;
  285. struct hostapd_config *oldconf = change->oldconf;
  286. struct hostapd_data *hapd = hapd_iface->bss[0];
  287. if (hapd_iface->reload_iface_cb) {
  288. wpa_printf(MSG_DEBUG,
  289. "%s: Interface reload already in progress.",
  290. hapd_iface->bss[0]->conf->iface);
  291. return -1;
  292. }
  293. hapd_iface->reload_iface_cb = cb;
  294. if (newconf->bridge_packets != oldconf->bridge_packets &&
  295. hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  296. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets))
  297. printf("Failed to set bridge_packets for kernel driver\n");
  298. if (newconf->channel != oldconf->channel ||
  299. newconf->hw_mode != oldconf->hw_mode ||
  300. rate_array_diff(newconf->supported_rates,
  301. oldconf->supported_rates) ||
  302. rate_array_diff(newconf->basic_rates, oldconf->basic_rates)) {
  303. hostapd_free_stas(hapd);
  304. if (hostapd_get_hw_features(hapd_iface)) {
  305. printf("Could not read HW feature info from the kernel"
  306. " driver.\n");
  307. hapd_iface->reload_iface_cb = NULL;
  308. return -1;
  309. }
  310. if (hostapd_select_hw_mode_start(hapd_iface,
  311. reload_hw_mode_done)) {
  312. printf("Failed to start select hw_mode.\n");
  313. hapd_iface->reload_iface_cb = NULL;
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. eloop_register_timeout(0, 0, reload_iface2_handler, hapd_iface, NULL);
  319. return 0;
  320. }
  321. static void hostapd_reconfig_bss(struct hostapd_data *hapd,
  322. struct hostapd_bss_config *newbss,
  323. struct hostapd_bss_config *oldbss,
  324. struct hostapd_config *oldconf,
  325. int beacon_changed)
  326. {
  327. struct hostapd_config_change change;
  328. int encr_changed = 0;
  329. struct radius_client_data *old_radius;
  330. radius_client_flush(hapd->radius, 0);
  331. if (hostapd_set_dtim_period(hapd, newbss->dtim_period))
  332. printf("Could not set DTIM period for kernel driver\n");
  333. if (newbss->ssid.ssid_len != oldbss->ssid.ssid_len ||
  334. os_memcmp(newbss->ssid.ssid, oldbss->ssid.ssid,
  335. newbss->ssid.ssid_len) != 0) {
  336. if (hostapd_set_ssid(hapd, (u8 *) newbss->ssid.ssid,
  337. newbss->ssid.ssid_len))
  338. printf("Could not set SSID for kernel driver\n");
  339. beacon_changed++;
  340. }
  341. if (newbss->ignore_broadcast_ssid != oldbss->ignore_broadcast_ssid)
  342. beacon_changed++;
  343. if (hostapd_wep_key_cmp(&newbss->ssid.wep, &oldbss->ssid.wep)) {
  344. encr_changed++;
  345. beacon_changed++;
  346. }
  347. vlan_reconfig(hapd, oldconf, oldbss);
  348. if (beacon_changed) {
  349. wpa_printf(MSG_DEBUG, "Updating beacon frame information");
  350. ieee802_11_set_beacon(hapd);
  351. }
  352. change.hapd = hapd;
  353. change.oldconf = oldconf;
  354. change.newconf = hapd->iconf;
  355. change.oldbss = oldbss;
  356. change.newbss = newbss;
  357. change.mac_acl_changed = hostapd_acl_diff(newbss, oldbss);
  358. if (newbss->max_num_sta != oldbss->max_num_sta &&
  359. newbss->max_num_sta < hapd->num_sta) {
  360. change.num_sta_remove = hapd->num_sta - newbss->max_num_sta;
  361. } else
  362. change.num_sta_remove = 0;
  363. ap_for_each_sta(hapd, hostapd_config_reload_sta, &change);
  364. old_radius = hapd->radius;
  365. hapd->radius = radius_client_reconfig(hapd->radius, hapd,
  366. oldbss->radius, newbss->radius);
  367. hapd->radius_client_reconfigured = old_radius != hapd->radius ||
  368. hostapd_ip_diff(&newbss->own_ip_addr, &oldbss->own_ip_addr);
  369. ieee802_1x_reconfig(hapd, oldconf, oldbss);
  370. iapp_reconfig(hapd, oldconf, oldbss);
  371. hostapd_acl_reconfig(hapd, oldconf);
  372. accounting_reconfig(hapd, oldconf);
  373. }
  374. /**
  375. * config_reload2 - Part 2 of configuration reloading
  376. * @hapd_iface:
  377. */
  378. static void config_reload2(struct hostapd_iface *hapd_iface, int status)
  379. {
  380. struct hostapd_config_change *change = hapd_iface->change;
  381. struct hostapd_data *hapd = change->hapd;
  382. struct hostapd_config *newconf = change->newconf;
  383. struct hostapd_config *oldconf = change->oldconf;
  384. int beacon_changed = change->beacon_changed;
  385. struct hostapd_data **new_hapd = change->new_hapd;
  386. struct hostapd_data **old_hapd = change->old_hapd;
  387. int num_old_hapd = change->num_old_hapd;
  388. size_t i, j, max_bss, same_bssid;
  389. struct hostapd_bss_config *newbss, *oldbss;
  390. u8 *prev_addr;
  391. hostapd_iface_cb cb;
  392. os_free(change);
  393. hapd_iface->change = NULL;
  394. if (status) {
  395. printf("Failed to setup new interface config\n");
  396. cb = hapd_iface->config_reload_cb;
  397. hapd_iface->config_reload_cb = NULL;
  398. /* Invalid configuration - cleanup and terminate hostapd */
  399. hapd_iface->bss = old_hapd;
  400. hapd_iface->num_bss = num_old_hapd;
  401. hapd_iface->conf = hapd->iconf = oldconf;
  402. hapd->conf = &oldconf->bss[0];
  403. hostapd_config_free(newconf);
  404. os_free(new_hapd);
  405. cb(hapd_iface, -2);
  406. return;
  407. }
  408. /*
  409. * If any BSSes have been removed, added, or had their BSSIDs changed,
  410. * completely remove and reinitialize such BSSes and all the BSSes
  411. * following them since their BSSID might have changed.
  412. */
  413. max_bss = oldconf->num_bss;
  414. if (max_bss > newconf->num_bss)
  415. max_bss = newconf->num_bss;
  416. for (i = 0; i < max_bss; i++) {
  417. if (os_strcmp(oldconf->bss[i].iface, newconf->bss[i].iface) !=
  418. 0 || hostapd_mac_comp(oldconf->bss[i].bssid,
  419. newconf->bss[i].bssid) != 0)
  420. break;
  421. }
  422. same_bssid = i;
  423. for (i = 0; i < oldconf->num_bss; i++) {
  424. oldbss = &oldconf->bss[i];
  425. newbss = NULL;
  426. for (j = 0; j < newconf->num_bss; j++) {
  427. if (os_strcmp(oldbss->iface, newconf->bss[j].iface) ==
  428. 0) {
  429. newbss = &newconf->bss[j];
  430. break;
  431. }
  432. }
  433. if (newbss && i < same_bssid) {
  434. hapd = hapd_iface->bss[j] = old_hapd[i];
  435. hapd->iconf = newconf;
  436. hapd->conf = newbss;
  437. hostapd_reconfig_bss(hapd, newbss, oldbss, oldconf,
  438. beacon_changed);
  439. } else {
  440. hapd = old_hapd[i];
  441. wpa_printf(MSG_DEBUG, "Removing BSS (ifname %s)",
  442. hapd->conf->iface);
  443. hostapd_free_stas(hapd);
  444. /* Send broadcast deauthentication for this BSS, but do
  445. * not clear all STAs from the driver since other BSSes
  446. * may have STA entries. The driver will remove all STA
  447. * entries for this BSS anyway when the interface is
  448. * being removed. */
  449. #if 0
  450. hostapd_deauth_all_stas(hapd);
  451. hostapd_cleanup(hapd);
  452. #endif
  453. os_free(hapd);
  454. }
  455. }
  456. prev_addr = hapd_iface->bss[0]->own_addr;
  457. hapd = hapd_iface->bss[0];
  458. for (j = 0; j < newconf->num_bss; j++) {
  459. if (hapd_iface->bss[j] != NULL) {
  460. prev_addr = hapd_iface->bss[j]->own_addr;
  461. continue;
  462. }
  463. newbss = &newconf->bss[j];
  464. wpa_printf(MSG_DEBUG, "Reconfiguration: adding new BSS "
  465. "(ifname=%s)", newbss->iface);
  466. #if 0
  467. hapd = hapd_iface->bss[j] =
  468. hostapd_alloc_bss_data(hapd_iface, newconf, newbss);
  469. if (hapd == NULL) {
  470. printf("Failed to initialize new BSS\n");
  471. /* FIX: This one is somewhat hard to recover
  472. * from.. Would need to remove this BSS from
  473. * conf and BSS list. */
  474. exit(1);
  475. }
  476. #endif
  477. hapd->driver = hapd_iface->bss[0]->driver;
  478. hapd->iface = hapd_iface;
  479. hapd->iconf = newconf;
  480. hapd->conf = newbss;
  481. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  482. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  483. prev_addr = hapd->own_addr;
  484. #if 0
  485. if (hostapd_setup_bss(hapd, j == 0)) {
  486. printf("Failed to setup new BSS\n");
  487. /* FIX */
  488. exit(1);
  489. }
  490. #endif
  491. }
  492. os_free(old_hapd);
  493. hostapd_config_free(oldconf);
  494. cb = hapd_iface->config_reload_cb;
  495. hapd_iface->config_reload_cb = NULL;
  496. cb(hapd_iface, 0);
  497. }
  498. /**
  499. * hostapd_config_reload_start - Start reconfiguration of an interface
  500. * @hapd_iface: Pointer to hostapd interface data
  501. * @cb: Function to be called back when done.
  502. * The status indicates:
  503. * 0 = success, new configuration in use;
  504. * -1 = failed to update configuraiton, old configuration in use;
  505. * -2 = failed to update configuration and failed to recover; caller
  506. * should cleanup and terminate hostapd
  507. * Returns:
  508. * 0 = reconfiguration started;
  509. * -1 = failed to update configuration, old configuration in use;
  510. * -2 = failed to update configuration and failed to recover; caller
  511. * should cleanup and terminate hostapd
  512. */
  513. int hostapd_config_reload_start(struct hostapd_iface *hapd_iface,
  514. hostapd_iface_cb cb)
  515. {
  516. struct hostapd_config *newconf, *oldconf;
  517. struct hostapd_config_change *change;
  518. struct hostapd_data *hapd = NULL;
  519. struct hostapd_data **old_hapd, **new_hapd;
  520. int num_old_hapd;
  521. if (hapd_iface->config_reload_cb) {
  522. wpa_printf(MSG_DEBUG, "%s: Config reload already in progress.",
  523. hapd_iface->bss[0]->conf->iface);
  524. return -1;
  525. }
  526. newconf = hostapd_config_read(hapd_iface->config_fname);
  527. if (newconf == NULL) {
  528. printf("Failed to read new configuration file - continuing "
  529. "with old.\n");
  530. return -1;
  531. }
  532. if (os_strcmp(newconf->bss[0].iface, hapd_iface->conf->bss[0].iface) !=
  533. 0) {
  534. printf("Interface name changing is not allowed in "
  535. "configuration reloading (%s -> %s).\n",
  536. hapd_iface->conf->bss[0].iface, newconf->bss[0].iface);
  537. hostapd_config_free(newconf);
  538. return -1;
  539. }
  540. new_hapd = os_zalloc(newconf->num_bss *
  541. sizeof(struct hostapd_data *));
  542. if (new_hapd == NULL) {
  543. hostapd_config_free(newconf);
  544. return -1;
  545. }
  546. old_hapd = hapd_iface->bss;
  547. num_old_hapd = hapd_iface->num_bss;
  548. hapd_iface->bss = new_hapd;
  549. hapd_iface->num_bss = newconf->num_bss;
  550. /*
  551. * First BSS remains the same since interface name changing was
  552. * prohibited above. Now, this is only used in
  553. * hostapd_config_reload_iface() and following loop will anyway set
  554. * this again.
  555. */
  556. hapd = hapd_iface->bss[0] = old_hapd[0];
  557. oldconf = hapd_iface->conf;
  558. hapd->iconf = hapd_iface->conf = newconf;
  559. hapd->conf = &newconf->bss[0];
  560. change = os_zalloc(sizeof(struct hostapd_config_change));
  561. if (change == NULL) {
  562. hostapd_config_free(newconf);
  563. return -1;
  564. }
  565. change->hapd = hapd;
  566. change->newconf = newconf;
  567. change->oldconf = oldconf;
  568. change->beacon_changed = 0;
  569. change->hapd_iface = hapd_iface;
  570. change->new_hapd = new_hapd;
  571. change->old_hapd = old_hapd;
  572. change->num_old_hapd = num_old_hapd;
  573. hapd_iface->config_reload_cb = cb;
  574. hapd_iface->change = change;
  575. if (hostapd_config_reload_iface_start(hapd_iface, config_reload2)) {
  576. printf("Failed to start setup of new interface config\n");
  577. hapd_iface->config_reload_cb = NULL;
  578. os_free(change);
  579. hapd_iface->change = NULL;
  580. /* Invalid configuration - cleanup and terminate hostapd */
  581. hapd_iface->bss = old_hapd;
  582. hapd_iface->num_bss = num_old_hapd;
  583. hapd_iface->conf = hapd->iconf = oldconf;
  584. hapd->conf = &oldconf->bss[0];
  585. hostapd_config_free(newconf);
  586. os_free(new_hapd);
  587. return -2;
  588. }
  589. return 0;
  590. }