hostapd.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. /*
  2. * hostapd / Initialization and configuration
  3. * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <syslog.h>
  17. #endif /* CONFIG_NATIVE_WINDOWS */
  18. #include "eloop.h"
  19. #include "hostapd.h"
  20. #include "ieee802_1x.h"
  21. #include "ieee802_11.h"
  22. #include "beacon.h"
  23. #include "hw_features.h"
  24. #include "accounting.h"
  25. #include "eapol_sm.h"
  26. #include "iapp.h"
  27. #include "ap.h"
  28. #include "ieee802_11_auth.h"
  29. #include "ap_list.h"
  30. #include "sta_info.h"
  31. #include "driver.h"
  32. #include "radius/radius_client.h"
  33. #include "radius/radius_server.h"
  34. #include "wpa.h"
  35. #include "preauth.h"
  36. #include "wme.h"
  37. #include "vlan_init.h"
  38. #include "ctrl_iface.h"
  39. #include "tls.h"
  40. #include "eap_server/eap_sim_db.h"
  41. #include "eap_server/eap.h"
  42. #include "eap_server/tncs.h"
  43. #include "version.h"
  44. #include "l2_packet/l2_packet.h"
  45. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  46. size_t identity_len, int phase2,
  47. struct eap_user *user);
  48. struct hapd_interfaces {
  49. size_t count;
  50. struct hostapd_iface **iface;
  51. };
  52. unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  53. extern int wpa_debug_level;
  54. extern int wpa_debug_show_keys;
  55. extern int wpa_debug_timestamp;
  56. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  57. int level, const char *txt, size_t len)
  58. {
  59. struct hostapd_data *hapd = ctx;
  60. char *format, *module_str;
  61. int maxlen;
  62. int conf_syslog_level, conf_stdout_level;
  63. unsigned int conf_syslog, conf_stdout;
  64. maxlen = len + 100;
  65. format = os_malloc(maxlen);
  66. if (!format)
  67. return;
  68. if (hapd && hapd->conf) {
  69. conf_syslog_level = hapd->conf->logger_syslog_level;
  70. conf_stdout_level = hapd->conf->logger_stdout_level;
  71. conf_syslog = hapd->conf->logger_syslog;
  72. conf_stdout = hapd->conf->logger_stdout;
  73. } else {
  74. conf_syslog_level = conf_stdout_level = 0;
  75. conf_syslog = conf_stdout = (unsigned int) -1;
  76. }
  77. switch (module) {
  78. case HOSTAPD_MODULE_IEEE80211:
  79. module_str = "IEEE 802.11";
  80. break;
  81. case HOSTAPD_MODULE_IEEE8021X:
  82. module_str = "IEEE 802.1X";
  83. break;
  84. case HOSTAPD_MODULE_RADIUS:
  85. module_str = "RADIUS";
  86. break;
  87. case HOSTAPD_MODULE_WPA:
  88. module_str = "WPA";
  89. break;
  90. case HOSTAPD_MODULE_DRIVER:
  91. module_str = "DRIVER";
  92. break;
  93. case HOSTAPD_MODULE_IAPP:
  94. module_str = "IAPP";
  95. break;
  96. case HOSTAPD_MODULE_MLME:
  97. module_str = "MLME";
  98. break;
  99. default:
  100. module_str = NULL;
  101. break;
  102. }
  103. if (hapd && hapd->conf && addr)
  104. os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
  105. hapd->conf->iface, MAC2STR(addr),
  106. module_str ? " " : "", module_str, txt);
  107. else if (hapd && hapd->conf)
  108. os_snprintf(format, maxlen, "%s:%s%s %s",
  109. hapd->conf->iface, module_str ? " " : "",
  110. module_str, txt);
  111. else if (addr)
  112. os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
  113. MAC2STR(addr), module_str ? " " : "",
  114. module_str, txt);
  115. else
  116. os_snprintf(format, maxlen, "%s%s%s",
  117. module_str, module_str ? ": " : "", txt);
  118. if ((conf_stdout & module) && level >= conf_stdout_level) {
  119. wpa_debug_print_timestamp();
  120. printf("%s\n", format);
  121. }
  122. #ifndef CONFIG_NATIVE_WINDOWS
  123. if ((conf_syslog & module) && level >= conf_syslog_level) {
  124. int priority;
  125. switch (level) {
  126. case HOSTAPD_LEVEL_DEBUG_VERBOSE:
  127. case HOSTAPD_LEVEL_DEBUG:
  128. priority = LOG_DEBUG;
  129. break;
  130. case HOSTAPD_LEVEL_INFO:
  131. priority = LOG_INFO;
  132. break;
  133. case HOSTAPD_LEVEL_NOTICE:
  134. priority = LOG_NOTICE;
  135. break;
  136. case HOSTAPD_LEVEL_WARNING:
  137. priority = LOG_WARNING;
  138. break;
  139. default:
  140. priority = LOG_INFO;
  141. break;
  142. }
  143. syslog(priority, "%s", format);
  144. }
  145. #endif /* CONFIG_NATIVE_WINDOWS */
  146. os_free(format);
  147. }
  148. static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
  149. {
  150. #if 0
  151. u8 addr[ETH_ALEN];
  152. os_memset(addr, 0xff, ETH_ALEN);
  153. hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  154. #else
  155. /* New Prism2.5/3 STA firmware versions seem to have issues with this
  156. * broadcast deauth frame. This gets the firmware in odd state where
  157. * nothing works correctly, so let's skip sending this for a while
  158. * until the issue has been resolved. */
  159. #endif
  160. }
  161. /**
  162. * hostapd_prune_associations - Remove extraneous associations
  163. * @hapd: Pointer to BSS data for the most recent association
  164. * @sta: Pointer to the associated STA data
  165. *
  166. * This function looks through all radios and BSS's for previous
  167. * (stale) associations of STA. If any are found they are removed.
  168. */
  169. static void hostapd_prune_associations(struct hostapd_data *hapd,
  170. struct sta_info *sta)
  171. {
  172. struct sta_info *osta;
  173. struct hostapd_data *ohapd;
  174. size_t i, j;
  175. struct hapd_interfaces *interfaces = eloop_get_user_data();
  176. for (i = 0; i < interfaces->count; i++) {
  177. for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
  178. ohapd = interfaces->iface[i]->bss[j];
  179. if (ohapd == hapd)
  180. continue;
  181. osta = ap_get_sta(ohapd, sta->addr);
  182. if (!osta)
  183. continue;
  184. ap_sta_disassociate(ohapd, osta,
  185. WLAN_REASON_UNSPECIFIED);
  186. }
  187. }
  188. }
  189. /**
  190. * hostapd_new_assoc_sta - Notify that a new station associated with the AP
  191. * @hapd: Pointer to BSS data
  192. * @sta: Pointer to the associated STA data
  193. * @reassoc: 1 to indicate this was a re-association; 0 = first association
  194. *
  195. * This function will be called whenever a station associates with the AP. It
  196. * can be called for ieee802_11.c for drivers that export MLME to hostapd and
  197. * from driver_*.c for drivers that take care of management frames (IEEE 802.11
  198. * authentication and association) internally.
  199. */
  200. void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
  201. int reassoc)
  202. {
  203. if (hapd->tkip_countermeasures) {
  204. hostapd_sta_deauth(hapd, sta->addr,
  205. WLAN_REASON_MICHAEL_MIC_FAILURE);
  206. return;
  207. }
  208. hostapd_prune_associations(hapd, sta);
  209. /* IEEE 802.11F (IAPP) */
  210. if (hapd->conf->ieee802_11f)
  211. iapp_new_station(hapd->iapp, sta);
  212. /* Start accounting here, if IEEE 802.1X and WPA are not used.
  213. * IEEE 802.1X/WPA code will start accounting after the station has
  214. * been authorized. */
  215. if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
  216. accounting_sta_start(hapd, sta);
  217. hostapd_wme_sta_config(hapd, sta);
  218. /* Start IEEE 802.1X authentication process for new stations */
  219. ieee802_1x_new_station(hapd, sta);
  220. if (reassoc) {
  221. if (sta->auth_alg != WLAN_AUTH_FT)
  222. wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
  223. } else
  224. wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
  225. }
  226. #ifdef EAP_SERVER
  227. static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
  228. struct sta_info *sta, void *ctx)
  229. {
  230. if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
  231. return 1;
  232. return 0;
  233. }
  234. static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
  235. {
  236. struct hostapd_data *hapd = ctx;
  237. if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
  238. radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
  239. }
  240. #endif /* EAP_SERVER */
  241. static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
  242. {
  243. printf("Signal %d received - terminating\n", sig);
  244. eloop_terminate();
  245. }
  246. static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
  247. struct wpa_auth_config *wconf)
  248. {
  249. wconf->wpa = conf->wpa;
  250. wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
  251. wconf->wpa_pairwise = conf->wpa_pairwise;
  252. wconf->wpa_group = conf->wpa_group;
  253. wconf->wpa_group_rekey = conf->wpa_group_rekey;
  254. wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
  255. wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
  256. wconf->rsn_pairwise = conf->rsn_pairwise;
  257. wconf->rsn_preauth = conf->rsn_preauth;
  258. wconf->eapol_version = conf->eapol_version;
  259. wconf->peerkey = conf->peerkey;
  260. wconf->wme_enabled = conf->wme_enabled;
  261. #ifdef CONFIG_IEEE80211W
  262. wconf->ieee80211w = conf->ieee80211w;
  263. #endif /* CONFIG_IEEE80211W */
  264. #ifdef CONFIG_IEEE80211R
  265. wconf->ssid_len = conf->ssid.ssid_len;
  266. if (wconf->ssid_len > SSID_LEN)
  267. wconf->ssid_len = SSID_LEN;
  268. os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
  269. os_memcpy(wconf->mobility_domain, conf->mobility_domain,
  270. MOBILITY_DOMAIN_ID_LEN);
  271. if (conf->nas_identifier &&
  272. os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
  273. wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
  274. os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
  275. wconf->r0_key_holder_len);
  276. }
  277. os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
  278. wconf->r0_key_lifetime = conf->r0_key_lifetime;
  279. wconf->reassociation_deadline = conf->reassociation_deadline;
  280. wconf->r0kh_list = conf->r0kh_list;
  281. wconf->r1kh_list = conf->r1kh_list;
  282. wconf->pmk_r1_push = conf->pmk_r1_push;
  283. #endif /* CONFIG_IEEE80211R */
  284. }
  285. #ifndef CONFIG_NATIVE_WINDOWS
  286. static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
  287. {
  288. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  289. struct hostapd_config *newconf;
  290. size_t i;
  291. struct wpa_auth_config wpa_auth_conf;
  292. printf("Signal %d received - reloading configuration\n", sig);
  293. for (i = 0; i < hapds->count; i++) {
  294. struct hostapd_data *hapd = hapds->iface[i]->bss[0];
  295. newconf = hostapd_config_read(hapds->iface[i]->config_fname);
  296. if (newconf == NULL) {
  297. printf("Failed to read new configuration file - "
  298. "continuing with old.\n");
  299. continue;
  300. }
  301. /* TODO: update dynamic data based on changed configuration
  302. * items (e.g., open/close sockets, remove stations added to
  303. * deny list, etc.) */
  304. radius_client_flush(hapd->radius, 0);
  305. hostapd_config_free(hapd->iconf);
  306. hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
  307. wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
  308. hapd->iconf = newconf;
  309. hapd->conf = &newconf->bss[0];
  310. hapds->iface[i]->conf = newconf;
  311. if (hostapd_setup_wpa_psk(hapd->conf)) {
  312. wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
  313. "after reloading configuration");
  314. }
  315. }
  316. }
  317. #ifdef HOSTAPD_DUMP_STATE
  318. static void hostapd_dump_state(struct hostapd_data *hapd)
  319. {
  320. FILE *f;
  321. time_t now;
  322. struct sta_info *sta;
  323. int i;
  324. char *buf;
  325. if (!hapd->conf->dump_log_name) {
  326. printf("Dump file not defined - ignoring dump request\n");
  327. return;
  328. }
  329. printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
  330. f = fopen(hapd->conf->dump_log_name, "w");
  331. if (f == NULL) {
  332. printf("Could not open dump file '%s' for writing.\n",
  333. hapd->conf->dump_log_name);
  334. return;
  335. }
  336. time(&now);
  337. fprintf(f, "hostapd state dump - %s", ctime(&now));
  338. fprintf(f, "num_sta=%d num_sta_non_erp=%d "
  339. "num_sta_no_short_slot_time=%d\n"
  340. "num_sta_no_short_preamble=%d\n",
  341. hapd->num_sta, hapd->iface->num_sta_non_erp,
  342. hapd->iface->num_sta_no_short_slot_time,
  343. hapd->iface->num_sta_no_short_preamble);
  344. for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
  345. fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
  346. fprintf(f,
  347. " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
  348. " capability=0x%x listen_interval=%d\n",
  349. sta->aid,
  350. sta->flags,
  351. (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
  352. (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
  353. (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
  354. (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
  355. (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
  356. (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
  357. ""),
  358. (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
  359. ""),
  360. (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
  361. "[SHORT_PREAMBLE]" : ""),
  362. (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
  363. (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
  364. sta->capability,
  365. sta->listen_interval);
  366. fprintf(f, " supported_rates=");
  367. for (i = 0; i < sta->supported_rates_len; i++)
  368. fprintf(f, "%02x ", sta->supported_rates[i]);
  369. fprintf(f, "\n");
  370. fprintf(f,
  371. " timeout_next=%s\n",
  372. (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
  373. (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
  374. "DEAUTH")));
  375. ieee802_1x_dump_state(f, " ", sta);
  376. }
  377. buf = os_malloc(4096);
  378. if (buf) {
  379. int count = radius_client_get_mib(hapd->radius, buf, 4096);
  380. if (count < 0)
  381. count = 0;
  382. else if (count > 4095)
  383. count = 4095;
  384. buf[count] = '\0';
  385. fprintf(f, "%s", buf);
  386. count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
  387. if (count < 0)
  388. count = 0;
  389. else if (count > 4095)
  390. count = 4095;
  391. buf[count] = '\0';
  392. fprintf(f, "%s", buf);
  393. os_free(buf);
  394. }
  395. fclose(f);
  396. }
  397. #endif /* HOSTAPD_DUMP_STATE */
  398. static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
  399. {
  400. #ifdef HOSTAPD_DUMP_STATE
  401. struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
  402. size_t i, j;
  403. for (i = 0; i < hapds->count; i++) {
  404. struct hostapd_iface *hapd_iface = hapds->iface[i];
  405. for (j = 0; j < hapd_iface->num_bss; j++)
  406. hostapd_dump_state(hapd_iface->bss[j]);
  407. }
  408. #endif /* HOSTAPD_DUMP_STATE */
  409. }
  410. #endif /* CONFIG_NATIVE_WINDOWS */
  411. static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
  412. char *ifname)
  413. {
  414. int i;
  415. for (i = 0; i < NUM_WEP_KEYS; i++) {
  416. if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
  417. 0, i == 0 ? 1 : 0)) {
  418. printf("Failed to clear default encryption keys "
  419. "(ifname=%s keyidx=%d)\n", ifname, i);
  420. }
  421. }
  422. }
  423. static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
  424. {
  425. hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
  426. return 0;
  427. }
  428. static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
  429. {
  430. int errors = 0, idx;
  431. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  432. idx = ssid->wep.idx;
  433. if (ssid->wep.default_len &&
  434. hostapd_set_encryption(hapd->conf->iface,
  435. hapd, "WEP", NULL, idx,
  436. ssid->wep.key[idx],
  437. ssid->wep.len[idx],
  438. idx == ssid->wep.idx)) {
  439. printf("Could not set WEP encryption.\n");
  440. errors++;
  441. }
  442. if (ssid->dyn_vlan_keys) {
  443. size_t i;
  444. for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
  445. const char *ifname;
  446. struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
  447. if (key == NULL)
  448. continue;
  449. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
  450. i);
  451. if (ifname == NULL)
  452. continue;
  453. idx = key->idx;
  454. if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
  455. idx, key->key[idx],
  456. key->len[idx],
  457. idx == key->idx)) {
  458. printf("Could not set dynamic VLAN WEP "
  459. "encryption.\n");
  460. errors++;
  461. }
  462. }
  463. }
  464. return errors;
  465. }
  466. /**
  467. * hostapd_cleanup - Per-BSS cleanup (deinitialization)
  468. * @hapd: Pointer to BSS data
  469. *
  470. * This function is used to free all per-BSS data structures and resources.
  471. * This gets called in a loop for each BSS between calls to
  472. * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
  473. * is deinitialized. Most of the modules that are initialized in
  474. * hostapd_setup_bss() are deinitialized here.
  475. */
  476. static void hostapd_cleanup(struct hostapd_data *hapd)
  477. {
  478. hostapd_ctrl_iface_deinit(hapd);
  479. os_free(hapd->default_wep_key);
  480. hapd->default_wep_key = NULL;
  481. iapp_deinit(hapd->iapp);
  482. hapd->iapp = NULL;
  483. accounting_deinit(hapd);
  484. rsn_preauth_iface_deinit(hapd);
  485. if (hapd->wpa_auth) {
  486. wpa_deinit(hapd->wpa_auth);
  487. hapd->wpa_auth = NULL;
  488. if (hostapd_set_privacy(hapd, 0)) {
  489. wpa_printf(MSG_DEBUG, "Could not disable "
  490. "PrivacyInvoked for interface %s",
  491. hapd->conf->iface);
  492. }
  493. if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
  494. wpa_printf(MSG_DEBUG, "Could not remove generic "
  495. "information element from interface %s",
  496. hapd->conf->iface);
  497. }
  498. }
  499. ieee802_1x_deinit(hapd);
  500. vlan_deinit(hapd);
  501. hostapd_acl_deinit(hapd);
  502. radius_client_deinit(hapd->radius);
  503. hapd->radius = NULL;
  504. radius_server_deinit(hapd->radius_srv);
  505. hapd->radius_srv = NULL;
  506. #ifdef CONFIG_IEEE80211R
  507. l2_packet_deinit(hapd->l2);
  508. #endif /* CONFIG_IEEE80211R */
  509. hostapd_wireless_event_deinit(hapd);
  510. #ifdef EAP_TLS_FUNCS
  511. if (hapd->ssl_ctx) {
  512. tls_deinit(hapd->ssl_ctx);
  513. hapd->ssl_ctx = NULL;
  514. }
  515. #endif /* EAP_TLS_FUNCS */
  516. #ifdef EAP_SERVER
  517. if (hapd->eap_sim_db_priv) {
  518. eap_sim_db_deinit(hapd->eap_sim_db_priv);
  519. hapd->eap_sim_db_priv = NULL;
  520. }
  521. #endif /* EAP_SERVER */
  522. if (hapd->interface_added &&
  523. hostapd_bss_remove(hapd, hapd->conf->iface)) {
  524. printf("Failed to remove BSS interface %s\n",
  525. hapd->conf->iface);
  526. }
  527. }
  528. /**
  529. * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
  530. * @iface: Pointer to interface data
  531. *
  532. * This function is called before per-BSS data structures are deinitialized
  533. * with hostapd_cleanup().
  534. */
  535. static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
  536. {
  537. }
  538. /**
  539. * hostapd_cleanup_iface - Complete per-interface cleanup
  540. * @iface: Pointer to interface data
  541. *
  542. * This function is called after per-BSS data structures are deinitialized
  543. * with hostapd_cleanup().
  544. */
  545. static void hostapd_cleanup_iface(struct hostapd_iface *iface)
  546. {
  547. hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
  548. iface->hw_features = NULL;
  549. os_free(iface->current_rates);
  550. iface->current_rates = NULL;
  551. ap_list_deinit(iface);
  552. hostapd_config_free(iface->conf);
  553. iface->conf = NULL;
  554. os_free(iface->config_fname);
  555. os_free(iface->bss);
  556. os_free(iface);
  557. }
  558. static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
  559. {
  560. int i;
  561. hostapd_broadcast_wep_set(hapd);
  562. if (hapd->conf->ssid.wep.default_len)
  563. return 0;
  564. for (i = 0; i < 4; i++) {
  565. if (hapd->conf->ssid.wep.key[i] &&
  566. hostapd_set_encryption(iface, hapd, "WEP", NULL,
  567. i, hapd->conf->ssid.wep.key[i],
  568. hapd->conf->ssid.wep.len[i],
  569. i == hapd->conf->ssid.wep.idx)) {
  570. printf("Could not set WEP encryption.\n");
  571. return -1;
  572. }
  573. if (hapd->conf->ssid.wep.key[i] &&
  574. i == hapd->conf->ssid.wep.idx)
  575. hostapd_set_privacy(hapd, 1);
  576. }
  577. return 0;
  578. }
  579. static int hostapd_flush_old_stations(struct hostapd_data *hapd)
  580. {
  581. int ret = 0;
  582. wpa_printf(MSG_DEBUG, "Flushing old station entries");
  583. if (hostapd_flush(hapd)) {
  584. printf("Could not connect to kernel driver.\n");
  585. ret = -1;
  586. }
  587. wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
  588. hostapd_deauth_all_stas(hapd);
  589. return ret;
  590. }
  591. static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
  592. logger_level level, const char *txt)
  593. {
  594. struct hostapd_data *hapd = ctx;
  595. int hlevel;
  596. switch (level) {
  597. case LOGGER_WARNING:
  598. hlevel = HOSTAPD_LEVEL_WARNING;
  599. break;
  600. case LOGGER_INFO:
  601. hlevel = HOSTAPD_LEVEL_INFO;
  602. break;
  603. case LOGGER_DEBUG:
  604. default:
  605. hlevel = HOSTAPD_LEVEL_DEBUG;
  606. break;
  607. }
  608. hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
  609. }
  610. static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
  611. u16 reason)
  612. {
  613. struct hostapd_data *hapd = ctx;
  614. struct sta_info *sta;
  615. wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
  616. "STA " MACSTR " reason %d",
  617. __func__, MAC2STR(addr), reason);
  618. sta = ap_get_sta(hapd, addr);
  619. hostapd_sta_deauth(hapd, addr, reason);
  620. if (sta == NULL)
  621. return;
  622. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  623. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  624. eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
  625. sta->timeout_next = STA_REMOVE;
  626. }
  627. static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
  628. {
  629. struct hostapd_data *hapd = ctx;
  630. ieee80211_michael_mic_failure(hapd, addr, 0);
  631. }
  632. static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
  633. wpa_eapol_variable var, int value)
  634. {
  635. struct hostapd_data *hapd = ctx;
  636. struct sta_info *sta = ap_get_sta(hapd, addr);
  637. if (sta == NULL)
  638. return;
  639. switch (var) {
  640. case WPA_EAPOL_portEnabled:
  641. ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
  642. break;
  643. case WPA_EAPOL_portValid:
  644. ieee802_1x_notify_port_valid(sta->eapol_sm, value);
  645. break;
  646. case WPA_EAPOL_authorized:
  647. ieee802_1x_set_sta_authorized(hapd, sta, value);
  648. break;
  649. case WPA_EAPOL_portControl_Auto:
  650. if (sta->eapol_sm)
  651. sta->eapol_sm->portControl = Auto;
  652. break;
  653. case WPA_EAPOL_keyRun:
  654. if (sta->eapol_sm)
  655. sta->eapol_sm->keyRun = value ? TRUE : FALSE;
  656. break;
  657. case WPA_EAPOL_keyAvailable:
  658. if (sta->eapol_sm)
  659. sta->eapol_sm->eap_if->eapKeyAvailable =
  660. value ? TRUE : FALSE;
  661. break;
  662. case WPA_EAPOL_keyDone:
  663. if (sta->eapol_sm)
  664. sta->eapol_sm->keyDone = value ? TRUE : FALSE;
  665. break;
  666. case WPA_EAPOL_inc_EapolFramesTx:
  667. if (sta->eapol_sm)
  668. sta->eapol_sm->dot1xAuthEapolFramesTx++;
  669. break;
  670. }
  671. }
  672. static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
  673. wpa_eapol_variable var)
  674. {
  675. struct hostapd_data *hapd = ctx;
  676. struct sta_info *sta = ap_get_sta(hapd, addr);
  677. if (sta == NULL || sta->eapol_sm == NULL)
  678. return -1;
  679. switch (var) {
  680. case WPA_EAPOL_keyRun:
  681. return sta->eapol_sm->keyRun;
  682. case WPA_EAPOL_keyAvailable:
  683. return sta->eapol_sm->eap_if->eapKeyAvailable;
  684. default:
  685. return -1;
  686. }
  687. }
  688. static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
  689. const u8 *prev_psk)
  690. {
  691. struct hostapd_data *hapd = ctx;
  692. return hostapd_get_psk(hapd->conf, addr, prev_psk);
  693. }
  694. static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
  695. size_t *len)
  696. {
  697. struct hostapd_data *hapd = ctx;
  698. const u8 *key;
  699. size_t keylen;
  700. struct sta_info *sta;
  701. sta = ap_get_sta(hapd, addr);
  702. if (sta == NULL)
  703. return -1;
  704. key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
  705. if (key == NULL)
  706. return -1;
  707. if (keylen > *len)
  708. keylen = *len;
  709. os_memcpy(msk, key, keylen);
  710. *len = keylen;
  711. return 0;
  712. }
  713. static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
  714. const u8 *addr, int idx, u8 *key,
  715. size_t key_len)
  716. {
  717. struct hostapd_data *hapd = ctx;
  718. const char *ifname = hapd->conf->iface;
  719. if (vlan_id > 0) {
  720. ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
  721. if (ifname == NULL)
  722. return -1;
  723. }
  724. return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
  725. key, key_len, 1);
  726. }
  727. static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
  728. u8 *seq)
  729. {
  730. struct hostapd_data *hapd = ctx;
  731. return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
  732. }
  733. static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
  734. u8 *seq)
  735. {
  736. struct hostapd_data *hapd = ctx;
  737. return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
  738. seq);
  739. }
  740. static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
  741. const u8 *data, size_t data_len,
  742. int encrypt)
  743. {
  744. struct hostapd_data *hapd = ctx;
  745. return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
  746. }
  747. static int hostapd_wpa_auth_for_each_sta(
  748. void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
  749. void *cb_ctx)
  750. {
  751. struct hostapd_data *hapd = ctx;
  752. struct sta_info *sta;
  753. for (sta = hapd->sta_list; sta; sta = sta->next) {
  754. if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
  755. return 1;
  756. }
  757. return 0;
  758. }
  759. static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
  760. const u8 *data, size_t data_len)
  761. {
  762. struct hostapd_data *hapd = ctx;
  763. if (hapd->driver && hapd->driver->send_ether)
  764. return hapd->driver->send_ether(hapd->drv_priv, dst,
  765. hapd->own_addr, proto,
  766. data, data_len);
  767. if (hapd->l2 == NULL)
  768. return -1;
  769. return l2_packet_send(hapd->l2, dst, proto, data, data_len);
  770. }
  771. #ifdef CONFIG_IEEE80211R
  772. static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
  773. const u8 *data, size_t data_len)
  774. {
  775. struct hostapd_data *hapd = ctx;
  776. int res;
  777. struct ieee80211_mgmt *m;
  778. size_t mlen;
  779. struct sta_info *sta;
  780. sta = ap_get_sta(hapd, dst);
  781. if (sta == NULL || sta->wpa_sm == NULL)
  782. return -1;
  783. m = os_zalloc(sizeof(*m) + data_len);
  784. if (m == NULL)
  785. return -1;
  786. mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
  787. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  788. WLAN_FC_STYPE_ACTION);
  789. os_memcpy(m->da, dst, ETH_ALEN);
  790. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  791. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  792. os_memcpy(&m->u, data, data_len);
  793. res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
  794. os_free(m);
  795. return res;
  796. }
  797. static struct wpa_state_machine *
  798. hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
  799. {
  800. struct hostapd_data *hapd = ctx;
  801. struct sta_info *sta;
  802. sta = ap_sta_add(hapd, sta_addr);
  803. if (sta == NULL)
  804. return NULL;
  805. if (sta->wpa_sm)
  806. return sta->wpa_sm;
  807. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
  808. if (sta->wpa_sm == NULL) {
  809. ap_free_sta(hapd, sta);
  810. return NULL;
  811. }
  812. sta->auth_alg = WLAN_AUTH_FT;
  813. return sta->wpa_sm;
  814. }
  815. static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  816. size_t len)
  817. {
  818. struct hostapd_data *hapd = ctx;
  819. wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
  820. }
  821. #endif /* CONFIG_IEEE80211R */
  822. /**
  823. * hostapd_validate_bssid_configuration - Validate BSSID configuration
  824. * @iface: Pointer to interface data
  825. * Returns: 0 on success, -1 on failure
  826. *
  827. * This function is used to validate that the configured BSSIDs are valid.
  828. */
  829. static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
  830. {
  831. u8 mask[ETH_ALEN] = { 0 };
  832. struct hostapd_data *hapd = iface->bss[0];
  833. unsigned int i = iface->conf->num_bss, bits = 0, j;
  834. int res;
  835. /* Generate BSSID mask that is large enough to cover the BSSIDs. */
  836. /* Determine the bits necessary to cover the number of BSSIDs. */
  837. for (i--; i; i >>= 1)
  838. bits++;
  839. /* Determine the bits necessary to any configured BSSIDs,
  840. if they are higher than the number of BSSIDs. */
  841. for (j = 0; j < iface->conf->num_bss; j++) {
  842. if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
  843. continue;
  844. for (i = 0; i < ETH_ALEN; i++) {
  845. mask[i] |=
  846. iface->conf->bss[j].bssid[i] ^
  847. hapd->own_addr[i];
  848. }
  849. }
  850. for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
  851. ;
  852. j = 0;
  853. if (i < ETH_ALEN) {
  854. j = (5 - i) * 8;
  855. while (mask[i] != 0) {
  856. mask[i] >>= 1;
  857. j++;
  858. }
  859. }
  860. if (bits < j)
  861. bits = j;
  862. if (bits > 40)
  863. return -1;
  864. os_memset(mask, 0xff, ETH_ALEN);
  865. j = bits / 8;
  866. for (i = 5; i > 5 - j; i--)
  867. mask[i] = 0;
  868. j = bits % 8;
  869. while (j--)
  870. mask[i] <<= 1;
  871. wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
  872. (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
  873. res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
  874. if (res == 0)
  875. return 0;
  876. if (res < 0) {
  877. printf("Driver did not accept BSSID mask " MACSTR " for start "
  878. "address " MACSTR ".\n",
  879. MAC2STR(mask), MAC2STR(hapd->own_addr));
  880. return -1;
  881. }
  882. for (i = 0; i < ETH_ALEN; i++) {
  883. if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
  884. printf("Invalid BSSID mask " MACSTR " for start "
  885. "address " MACSTR ".\n"
  886. "Start address must be the first address in the"
  887. " block (i.e., addr AND mask == addr).\n",
  888. MAC2STR(mask), MAC2STR(hapd->own_addr));
  889. return -1;
  890. }
  891. }
  892. return 0;
  893. }
  894. static int mac_in_conf(struct hostapd_config *conf, const void *a)
  895. {
  896. size_t i;
  897. for (i = 0; i < conf->num_bss; i++) {
  898. if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
  899. return 1;
  900. }
  901. }
  902. return 0;
  903. }
  904. static int hostapd_setup_wpa(struct hostapd_data *hapd)
  905. {
  906. struct wpa_auth_config _conf;
  907. struct wpa_auth_callbacks cb;
  908. const u8 *wpa_ie;
  909. size_t wpa_ie_len;
  910. hostapd_wpa_auth_conf(hapd->conf, &_conf);
  911. os_memset(&cb, 0, sizeof(cb));
  912. cb.ctx = hapd;
  913. cb.logger = hostapd_wpa_auth_logger;
  914. cb.disconnect = hostapd_wpa_auth_disconnect;
  915. cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
  916. cb.set_eapol = hostapd_wpa_auth_set_eapol;
  917. cb.get_eapol = hostapd_wpa_auth_get_eapol;
  918. cb.get_psk = hostapd_wpa_auth_get_psk;
  919. cb.get_msk = hostapd_wpa_auth_get_msk;
  920. cb.set_key = hostapd_wpa_auth_set_key;
  921. cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
  922. cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
  923. cb.send_eapol = hostapd_wpa_auth_send_eapol;
  924. cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
  925. cb.send_ether = hostapd_wpa_auth_send_ether;
  926. #ifdef CONFIG_IEEE80211R
  927. cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
  928. cb.add_sta = hostapd_wpa_auth_add_sta;
  929. #endif /* CONFIG_IEEE80211R */
  930. hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
  931. if (hapd->wpa_auth == NULL) {
  932. printf("WPA initialization failed.\n");
  933. return -1;
  934. }
  935. if (hostapd_set_privacy(hapd, 1)) {
  936. wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
  937. "for interface %s", hapd->conf->iface);
  938. return -1;
  939. }
  940. wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
  941. if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
  942. wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
  943. "the kernel driver.");
  944. return -1;
  945. }
  946. if (rsn_preauth_iface_init(hapd)) {
  947. printf("Initialization of RSN pre-authentication "
  948. "failed.\n");
  949. return -1;
  950. }
  951. return 0;
  952. }
  953. static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
  954. struct hostapd_bss_config *conf)
  955. {
  956. struct radius_server_conf srv;
  957. os_memset(&srv, 0, sizeof(srv));
  958. srv.client_file = conf->radius_server_clients;
  959. srv.auth_port = conf->radius_server_auth_port;
  960. srv.conf_ctx = conf;
  961. srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
  962. srv.ssl_ctx = hapd->ssl_ctx;
  963. srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
  964. srv.eap_fast_a_id = conf->eap_fast_a_id;
  965. srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
  966. srv.ipv6 = conf->radius_server_ipv6;
  967. srv.get_eap_user = hostapd_radius_get_eap_user;
  968. hapd->radius_srv = radius_server_init(&srv);
  969. if (hapd->radius_srv == NULL) {
  970. printf("RADIUS server initialization failed.\n");
  971. return -1;
  972. }
  973. return 0;
  974. }
  975. /**
  976. * hostapd_setup_bss - Per-BSS setup (initialization)
  977. * @hapd: Pointer to BSS data
  978. * @first: Whether this BSS is the first BSS of an interface
  979. *
  980. * This function is used to initialize all per-BSS data structures and
  981. * resources. This gets called in a loop for each BSS when an interface is
  982. * initialized. Most of the modules that are initialized here will be
  983. * deinitialized in hostapd_cleanup().
  984. */
  985. static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
  986. {
  987. struct hostapd_bss_config *conf = hapd->conf;
  988. u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
  989. int ssid_len, set_ssid;
  990. if (!first) {
  991. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
  992. /* Allocate the next available BSSID. */
  993. do {
  994. inc_byte_array(hapd->own_addr, ETH_ALEN);
  995. } while (mac_in_conf(hapd->iconf, hapd->own_addr));
  996. } else {
  997. /* Allocate the configured BSSID. */
  998. os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
  999. if (hostapd_mac_comp(hapd->own_addr,
  1000. hapd->iface->bss[0]->own_addr) ==
  1001. 0) {
  1002. printf("BSS '%s' may not have BSSID "
  1003. "set to the MAC address of the radio\n",
  1004. hapd->conf->iface);
  1005. return -1;
  1006. }
  1007. }
  1008. hapd->interface_added = 1;
  1009. if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
  1010. hapd->own_addr)) {
  1011. printf("Failed to add BSS (BSSID=" MACSTR ")\n",
  1012. MAC2STR(hapd->own_addr));
  1013. return -1;
  1014. }
  1015. }
  1016. /*
  1017. * Fetch the SSID from the system and use it or,
  1018. * if one was specified in the config file, verify they
  1019. * match.
  1020. */
  1021. ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
  1022. if (ssid_len < 0) {
  1023. printf("Could not read SSID from system\n");
  1024. return -1;
  1025. }
  1026. if (conf->ssid.ssid_set) {
  1027. /*
  1028. * If SSID is specified in the config file and it differs
  1029. * from what is being used then force installation of the
  1030. * new SSID.
  1031. */
  1032. set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
  1033. os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
  1034. } else {
  1035. /*
  1036. * No SSID in the config file; just use the one we got
  1037. * from the system.
  1038. */
  1039. set_ssid = 0;
  1040. conf->ssid.ssid_len = ssid_len;
  1041. os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
  1042. conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
  1043. }
  1044. printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
  1045. hapd->conf->iface, MAC2STR(hapd->own_addr),
  1046. hapd->conf->ssid.ssid);
  1047. if (hostapd_setup_wpa_psk(conf)) {
  1048. printf("WPA-PSK setup failed.\n");
  1049. return -1;
  1050. }
  1051. /* Set flag for whether SSID is broadcast in beacons */
  1052. if (hostapd_set_broadcast_ssid(hapd,
  1053. !!hapd->conf->ignore_broadcast_ssid)) {
  1054. printf("Could not set broadcast SSID flag for kernel "
  1055. "driver\n");
  1056. return -1;
  1057. }
  1058. if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
  1059. printf("Could not set DTIM period for kernel driver\n");
  1060. return -1;
  1061. }
  1062. /* Set SSID for the kernel driver (to be used in beacon and probe
  1063. * response frames) */
  1064. if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
  1065. conf->ssid.ssid_len)) {
  1066. printf("Could not set SSID for kernel driver\n");
  1067. return -1;
  1068. }
  1069. if (wpa_debug_level == MSG_MSGDUMP)
  1070. conf->radius->msg_dumps = 1;
  1071. hapd->radius = radius_client_init(hapd, conf->radius);
  1072. if (hapd->radius == NULL) {
  1073. printf("RADIUS client initialization failed.\n");
  1074. return -1;
  1075. }
  1076. if (hostapd_acl_init(hapd)) {
  1077. printf("ACL initialization failed.\n");
  1078. return -1;
  1079. }
  1080. if (ieee802_1x_init(hapd)) {
  1081. printf("IEEE 802.1X initialization failed.\n");
  1082. return -1;
  1083. }
  1084. if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
  1085. return -1;
  1086. if (accounting_init(hapd)) {
  1087. printf("Accounting initialization failed.\n");
  1088. return -1;
  1089. }
  1090. if (hapd->conf->ieee802_11f &&
  1091. (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
  1092. printf("IEEE 802.11F (IAPP) initialization failed.\n");
  1093. return -1;
  1094. }
  1095. if (hostapd_ctrl_iface_init(hapd)) {
  1096. printf("Failed to setup control interface\n");
  1097. return -1;
  1098. }
  1099. if (vlan_init(hapd)) {
  1100. printf("VLAN initialization failed.\n");
  1101. return -1;
  1102. }
  1103. #ifdef CONFIG_IEEE80211R
  1104. hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
  1105. hostapd_rrb_receive, hapd, 0);
  1106. if (hapd->l2 == NULL &&
  1107. (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
  1108. printf("Failed to open l2_packet interface\n");
  1109. return -1;
  1110. }
  1111. #endif /* CONFIG_IEEE80211R */
  1112. ieee802_11_set_beacon(hapd);
  1113. if (conf->radius_server_clients &&
  1114. hostapd_setup_radius_srv(hapd, conf))
  1115. return -1;
  1116. return 0;
  1117. }
  1118. /**
  1119. * setup_interface2 - Setup (initialize) an interface (part 2)
  1120. * @iface: Pointer to interface data.
  1121. * Returns: 0 on success; -1 on failure.
  1122. *
  1123. * Flushes old stations, sets the channel, DFS parameters, encryption,
  1124. * beacons, and WDS links based on the configuration.
  1125. */
  1126. static int setup_interface2(struct hostapd_iface *iface)
  1127. {
  1128. struct hostapd_data *hapd = iface->bss[0];
  1129. int freq;
  1130. size_t j;
  1131. int ret = 0;
  1132. u8 *prev_addr;
  1133. hostapd_flush_old_stations(hapd);
  1134. hostapd_set_privacy(hapd, 0);
  1135. if (hapd->iconf->channel) {
  1136. freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
  1137. printf("Mode: %s Channel: %d Frequency: %d MHz\n",
  1138. hostapd_hw_mode_txt(hapd->iconf->hw_mode),
  1139. hapd->iconf->channel, freq);
  1140. if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
  1141. printf("Could not set channel for kernel driver\n");
  1142. return -1;
  1143. }
  1144. }
  1145. hostapd_broadcast_wep_clear(hapd);
  1146. if (hostapd_setup_encryption(hapd->conf->iface, hapd))
  1147. return -1;
  1148. hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
  1149. ieee802_11_set_beacon(hapd);
  1150. if (hapd->iconf->rts_threshold > -1 &&
  1151. hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
  1152. printf("Could not set RTS threshold for kernel driver\n");
  1153. return -1;
  1154. }
  1155. if (hapd->iconf->fragm_threshold > -1 &&
  1156. hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
  1157. printf("Could not set fragmentation threshold for kernel "
  1158. "driver\n");
  1159. return -1;
  1160. }
  1161. prev_addr = hapd->own_addr;
  1162. for (j = 0; j < iface->num_bss; j++) {
  1163. hapd = iface->bss[j];
  1164. if (j)
  1165. os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
  1166. if (hostapd_setup_bss(hapd, j == 0))
  1167. return -1;
  1168. if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
  1169. prev_addr = hapd->own_addr;
  1170. }
  1171. ap_list_init(iface);
  1172. if (hostapd_driver_commit(hapd) < 0) {
  1173. wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
  1174. "configuration", __func__);
  1175. return -1;
  1176. }
  1177. return ret;
  1178. }
  1179. static void setup_interface_start(void *eloop_data, void *user_ctx);
  1180. static void setup_interface2_handler(void *eloop_data, void *user_ctx);
  1181. /**
  1182. * setup_interface_finalize - Finish setup interface & call the callback
  1183. * @iface: Pointer to interface data.
  1184. * @status: Status of the setup interface (0 on success; -1 on failure).
  1185. * Returns: 0 on success; -1 on failure (e.g., was not in progress).
  1186. */
  1187. static int setup_interface_finalize(struct hostapd_iface *iface, int status)
  1188. {
  1189. hostapd_iface_cb cb;
  1190. if (!iface->setup_cb)
  1191. return -1;
  1192. eloop_cancel_timeout(setup_interface_start, iface, NULL);
  1193. eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
  1194. hostapd_select_hw_mode_stop(iface);
  1195. cb = iface->setup_cb;
  1196. iface->setup_cb = NULL;
  1197. cb(iface, status);
  1198. return 0;
  1199. }
  1200. /**
  1201. * setup_interface2_wrapper - Wrapper for setup_interface2()
  1202. * @iface: Pointer to interface data.
  1203. * @status: Status of the hw mode select.
  1204. *
  1205. * Wrapper for setup_interface2() to calls finalize function upon completion.
  1206. */
  1207. static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
  1208. {
  1209. int ret = status;
  1210. if (ret)
  1211. printf("Could not select hw_mode and channel. (%d)\n", ret);
  1212. else
  1213. ret = setup_interface2(iface);
  1214. setup_interface_finalize(iface, ret);
  1215. }
  1216. /**
  1217. * setup_interface2_handler - Used for immediate call of setup_interface2
  1218. * @eloop_data: Stores the struct hostapd_iface * for the interface.
  1219. * @user_ctx: Unused.
  1220. */
  1221. static void setup_interface2_handler(void *eloop_data, void *user_ctx)
  1222. {
  1223. struct hostapd_iface *iface = eloop_data;
  1224. setup_interface2_wrapper(iface, 0);
  1225. }
  1226. static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
  1227. size_t identity_len, int phase2,
  1228. struct eap_user *user)
  1229. {
  1230. const struct hostapd_eap_user *eap_user;
  1231. int i, count;
  1232. eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
  1233. if (eap_user == NULL)
  1234. return -1;
  1235. if (user == NULL)
  1236. return 0;
  1237. os_memset(user, 0, sizeof(*user));
  1238. count = EAP_USER_MAX_METHODS;
  1239. if (count > EAP_MAX_METHODS)
  1240. count = EAP_MAX_METHODS;
  1241. for (i = 0; i < count; i++) {
  1242. user->methods[i].vendor = eap_user->methods[i].vendor;
  1243. user->methods[i].method = eap_user->methods[i].method;
  1244. }
  1245. if (eap_user->password) {
  1246. user->password = os_malloc(eap_user->password_len);
  1247. if (user->password == NULL)
  1248. return -1;
  1249. os_memcpy(user->password, eap_user->password,
  1250. eap_user->password_len);
  1251. user->password_len = eap_user->password_len;
  1252. user->password_hash = eap_user->password_hash;
  1253. }
  1254. user->force_version = eap_user->force_version;
  1255. user->ttls_auth = eap_user->ttls_auth;
  1256. return 0;
  1257. }
  1258. /**
  1259. * setup_interface1 - Setup (initialize) an interface (part 1)
  1260. * @iface: Pointer to interface data
  1261. * Returns: 0 on success, -1 on failure
  1262. *
  1263. * Initializes the driver interface, validates the configuration,
  1264. * and sets driver parameters based on the configuration.
  1265. * Schedules setup_interface2() to be called immediately or after
  1266. * hardware mode setup takes place.
  1267. */
  1268. static int setup_interface1(struct hostapd_iface *iface)
  1269. {
  1270. struct hostapd_data *hapd = iface->bss[0];
  1271. struct hostapd_bss_config *conf = hapd->conf;
  1272. size_t i;
  1273. char country[4];
  1274. u8 *b = conf->bssid;
  1275. /*
  1276. * Initialize the driver interface and make sure that all BSSes get
  1277. * configured with a pointer to this driver interface.
  1278. */
  1279. if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
  1280. hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
  1281. } else {
  1282. hapd->drv_priv = hostapd_driver_init(hapd);
  1283. }
  1284. if (hapd->drv_priv == NULL) {
  1285. printf("%s driver initialization failed.\n",
  1286. hapd->driver ? hapd->driver->name : "Unknown");
  1287. hapd->driver = NULL;
  1288. return -1;
  1289. }
  1290. for (i = 0; i < iface->num_bss; i++) {
  1291. iface->bss[i]->driver = hapd->driver;
  1292. iface->bss[i]->drv_priv = hapd->drv_priv;
  1293. }
  1294. if (hostapd_validate_bssid_configuration(iface))
  1295. return -1;
  1296. os_memcpy(country, hapd->iconf->country, 3);
  1297. country[3] = '\0';
  1298. if (hostapd_set_country(hapd, country) < 0) {
  1299. printf("Failed to set country code\n");
  1300. return -1;
  1301. }
  1302. if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
  1303. if (hostapd_set_ieee80211d(hapd, 1) < 0) {
  1304. printf("Failed to set ieee80211d (%d)\n",
  1305. hapd->iconf->ieee80211d);
  1306. return -1;
  1307. }
  1308. }
  1309. if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
  1310. hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
  1311. printf("Failed to set bridge_packets for kernel driver\n");
  1312. return -1;
  1313. }
  1314. /* TODO: merge with hostapd_driver_init() ? */
  1315. if (hostapd_wireless_event_init(hapd) < 0)
  1316. return -1;
  1317. if (hostapd_get_hw_features(iface)) {
  1318. /* Not all drivers support this yet, so continue without hw
  1319. * feature data. */
  1320. } else {
  1321. return hostapd_select_hw_mode_start(iface,
  1322. setup_interface2_wrapper);
  1323. }
  1324. eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
  1325. return 0;
  1326. }
  1327. /**
  1328. * setup_interface_start - Handler to start setup interface
  1329. * @eloop_data: Stores the struct hostapd_iface * for the interface.
  1330. * @user_ctx: Unused.
  1331. *
  1332. * An eloop handler is used so that all errors can be processed by the
  1333. * callback without introducing stack recursion.
  1334. */
  1335. static void setup_interface_start(void *eloop_data, void *user_ctx)
  1336. {
  1337. struct hostapd_iface *iface = eloop_data;
  1338. int ret;
  1339. ret = setup_interface1(iface);
  1340. if (ret)
  1341. setup_interface_finalize(iface, ret);
  1342. }
  1343. /**
  1344. * hostapd_setup_interface_start - Start the setup of an interface
  1345. * @iface: Pointer to interface data.
  1346. * @cb: The function to callback when done.
  1347. * Returns: 0 if it starts successfully; cb will be called when done.
  1348. * -1 on failure; cb will not be called.
  1349. *
  1350. * Initializes the driver interface, validates the configuration,
  1351. * and sets driver parameters based on the configuration.
  1352. * Flushes old stations, sets the channel, DFS parameters, encryption,
  1353. * beacons, and WDS links based on the configuration.
  1354. */
  1355. int hostapd_setup_interface_start(struct hostapd_iface *iface,
  1356. hostapd_iface_cb cb)
  1357. {
  1358. if (iface->setup_cb) {
  1359. wpa_printf(MSG_DEBUG,
  1360. "%s: Interface setup already in progress.\n",
  1361. iface->bss[0]->conf->iface);
  1362. return -1;
  1363. }
  1364. iface->setup_cb = cb;
  1365. eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
  1366. return 0;
  1367. }
  1368. /**
  1369. * hostapd_setup_interace_stop - Stops the setup of an interface
  1370. * @iface: Pointer to interface data
  1371. * Returns: 0 if successfully stopped;
  1372. * -1 on failure (i.e., was not in progress)
  1373. */
  1374. int hostapd_setup_interface_stop(struct hostapd_iface *iface)
  1375. {
  1376. return setup_interface_finalize(iface, -1);
  1377. }
  1378. static void show_version(void)
  1379. {
  1380. fprintf(stderr,
  1381. "hostapd v" VERSION_STR "\n"
  1382. "User space daemon for IEEE 802.11 AP management,\n"
  1383. "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
  1384. "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
  1385. "and contributors\n");
  1386. }
  1387. static void usage(void)
  1388. {
  1389. show_version();
  1390. fprintf(stderr,
  1391. "\n"
  1392. "usage: hostapd [-hdBKtv] [-P <PID file>] "
  1393. "<configuration file(s)>\n"
  1394. "\n"
  1395. "options:\n"
  1396. " -h show this usage\n"
  1397. " -d show more debug messages (-dd for even more)\n"
  1398. " -B run daemon in the background\n"
  1399. " -P PID file\n"
  1400. " -K include key data in debug messages\n"
  1401. " -t include timestamps in some debug messages\n"
  1402. " -v show hostapd version\n");
  1403. exit(1);
  1404. }
  1405. /**
  1406. * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
  1407. * @hapd_iface: Pointer to interface data
  1408. * @conf: Pointer to per-interface configuration
  1409. * @bss: Pointer to per-BSS configuration for this BSS
  1410. * Returns: Pointer to allocated BSS data
  1411. *
  1412. * This function is used to allocate per-BSS data structure. This data will be
  1413. * freed after hostapd_cleanup() is called for it during interface
  1414. * deinitialization.
  1415. */
  1416. static struct hostapd_data *
  1417. hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
  1418. struct hostapd_config *conf,
  1419. struct hostapd_bss_config *bss)
  1420. {
  1421. struct hostapd_data *hapd;
  1422. hapd = os_zalloc(sizeof(*hapd));
  1423. if (hapd == NULL)
  1424. return NULL;
  1425. hapd->iconf = conf;
  1426. hapd->conf = bss;
  1427. hapd->iface = hapd_iface;
  1428. if (hapd->conf->individual_wep_key_len > 0) {
  1429. /* use key0 in individual key and key1 in broadcast key */
  1430. hapd->default_wep_key_idx = 1;
  1431. }
  1432. #ifdef EAP_TLS_FUNCS
  1433. if (hapd->conf->eap_server &&
  1434. (hapd->conf->ca_cert || hapd->conf->server_cert ||
  1435. hapd->conf->dh_file)) {
  1436. struct tls_connection_params params;
  1437. hapd->ssl_ctx = tls_init(NULL);
  1438. if (hapd->ssl_ctx == NULL) {
  1439. printf("Failed to initialize TLS\n");
  1440. goto fail;
  1441. }
  1442. os_memset(&params, 0, sizeof(params));
  1443. params.ca_cert = hapd->conf->ca_cert;
  1444. params.client_cert = hapd->conf->server_cert;
  1445. params.private_key = hapd->conf->private_key;
  1446. params.private_key_passwd = hapd->conf->private_key_passwd;
  1447. params.dh_file = hapd->conf->dh_file;
  1448. if (tls_global_set_params(hapd->ssl_ctx, &params)) {
  1449. printf("Failed to set TLS parameters\n");
  1450. goto fail;
  1451. }
  1452. if (tls_global_set_verify(hapd->ssl_ctx,
  1453. hapd->conf->check_crl)) {
  1454. printf("Failed to enable check_crl\n");
  1455. goto fail;
  1456. }
  1457. }
  1458. #endif /* EAP_TLS_FUNCS */
  1459. #ifdef EAP_SERVER
  1460. if (hapd->conf->eap_sim_db) {
  1461. hapd->eap_sim_db_priv =
  1462. eap_sim_db_init(hapd->conf->eap_sim_db,
  1463. hostapd_sim_db_cb, hapd);
  1464. if (hapd->eap_sim_db_priv == NULL) {
  1465. printf("Failed to initialize EAP-SIM database "
  1466. "interface\n");
  1467. goto fail;
  1468. }
  1469. }
  1470. #endif /* EAP_SERVER */
  1471. if (hapd->conf->assoc_ap)
  1472. hapd->assoc_ap_state = WAIT_BEACON;
  1473. hapd->driver = hapd->iconf->driver;
  1474. return hapd;
  1475. #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
  1476. fail:
  1477. #endif
  1478. /* TODO: cleanup allocated resources(?) */
  1479. os_free(hapd);
  1480. return NULL;
  1481. }
  1482. /**
  1483. * hostapd_init - Allocate and initialize per-interface data
  1484. * @config_file: Path to the configuration file
  1485. * Returns: Pointer to the allocated interface data or %NULL on failure
  1486. *
  1487. * This function is used to allocate main data structures for per-interface
  1488. * data. The allocated data buffer will be freed by calling
  1489. * hostapd_cleanup_iface().
  1490. */
  1491. static struct hostapd_iface * hostapd_init(const char *config_file)
  1492. {
  1493. struct hostapd_iface *hapd_iface = NULL;
  1494. struct hostapd_config *conf = NULL;
  1495. struct hostapd_data *hapd;
  1496. size_t i;
  1497. hapd_iface = os_zalloc(sizeof(*hapd_iface));
  1498. if (hapd_iface == NULL)
  1499. goto fail;
  1500. hapd_iface->config_fname = os_strdup(config_file);
  1501. if (hapd_iface->config_fname == NULL)
  1502. goto fail;
  1503. conf = hostapd_config_read(hapd_iface->config_fname);
  1504. if (conf == NULL)
  1505. goto fail;
  1506. hapd_iface->conf = conf;
  1507. hapd_iface->num_bss = conf->num_bss;
  1508. hapd_iface->bss = os_zalloc(conf->num_bss *
  1509. sizeof(struct hostapd_data *));
  1510. if (hapd_iface->bss == NULL)
  1511. goto fail;
  1512. for (i = 0; i < conf->num_bss; i++) {
  1513. hapd = hapd_iface->bss[i] =
  1514. hostapd_alloc_bss_data(hapd_iface, conf,
  1515. &conf->bss[i]);
  1516. if (hapd == NULL)
  1517. goto fail;
  1518. }
  1519. return hapd_iface;
  1520. fail:
  1521. if (conf)
  1522. hostapd_config_free(conf);
  1523. if (hapd_iface) {
  1524. for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
  1525. hapd = hapd_iface->bss[i];
  1526. if (hapd && hapd->ssl_ctx)
  1527. tls_deinit(hapd->ssl_ctx);
  1528. }
  1529. os_free(hapd_iface->config_fname);
  1530. os_free(hapd_iface->bss);
  1531. os_free(hapd_iface);
  1532. }
  1533. return NULL;
  1534. }
  1535. /**
  1536. * register_drivers - Register driver interfaces
  1537. *
  1538. * This function is generated by Makefile (into driver_conf.c) to call all
  1539. * configured driver interfaces to register them to core hostapd.
  1540. */
  1541. void register_drivers(void);
  1542. /**
  1543. * setup_interface_done - Callback when an interface is done being setup.
  1544. * @iface: Pointer to interface data.
  1545. * @status: Status of the interface setup (0 on success; -1 on failure).
  1546. */
  1547. static void setup_interface_done(struct hostapd_iface *iface, int status)
  1548. {
  1549. if (status) {
  1550. wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
  1551. iface->bss[0]->conf->iface);
  1552. eloop_terminate();
  1553. } else
  1554. wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
  1555. iface->bss[0]->conf->iface);
  1556. }
  1557. int main(int argc, char *argv[])
  1558. {
  1559. struct hapd_interfaces interfaces;
  1560. int ret = 1, k;
  1561. size_t i, j;
  1562. int c, debug = 0, daemonize = 0, tnc = 0;
  1563. const char *pid_file = NULL;
  1564. hostapd_logger_register_cb(hostapd_logger_cb);
  1565. for (;;) {
  1566. c = getopt(argc, argv, "BdhKP:tv");
  1567. if (c < 0)
  1568. break;
  1569. switch (c) {
  1570. case 'h':
  1571. usage();
  1572. break;
  1573. case 'd':
  1574. debug++;
  1575. if (wpa_debug_level > 0)
  1576. wpa_debug_level--;
  1577. break;
  1578. case 'B':
  1579. daemonize++;
  1580. break;
  1581. case 'K':
  1582. wpa_debug_show_keys++;
  1583. break;
  1584. case 'P':
  1585. pid_file = optarg;
  1586. break;
  1587. case 't':
  1588. wpa_debug_timestamp++;
  1589. break;
  1590. case 'v':
  1591. show_version();
  1592. exit(1);
  1593. break;
  1594. default:
  1595. usage();
  1596. break;
  1597. }
  1598. }
  1599. if (optind == argc)
  1600. usage();
  1601. if (eap_server_register_methods()) {
  1602. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1603. return -1;
  1604. }
  1605. interfaces.count = argc - optind;
  1606. interfaces.iface = os_malloc(interfaces.count *
  1607. sizeof(struct hostapd_iface *));
  1608. if (interfaces.iface == NULL) {
  1609. wpa_printf(MSG_ERROR, "malloc failed\n");
  1610. return -1;
  1611. }
  1612. if (eloop_init(&interfaces)) {
  1613. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1614. return -1;
  1615. }
  1616. #ifndef CONFIG_NATIVE_WINDOWS
  1617. eloop_register_signal(SIGHUP, handle_reload, NULL);
  1618. eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
  1619. #endif /* CONFIG_NATIVE_WINDOWS */
  1620. eloop_register_signal_terminate(handle_term, NULL);
  1621. /* Initialize interfaces */
  1622. for (i = 0; i < interfaces.count; i++) {
  1623. printf("Configuration file: %s\n", argv[optind + i]);
  1624. interfaces.iface[i] = hostapd_init(argv[optind + i]);
  1625. if (!interfaces.iface[i])
  1626. goto out;
  1627. for (k = 0; k < debug; k++) {
  1628. if (interfaces.iface[i]->bss[0]->conf->
  1629. logger_stdout_level > 0)
  1630. interfaces.iface[i]->bss[0]->conf->
  1631. logger_stdout_level--;
  1632. }
  1633. ret = hostapd_setup_interface_start(interfaces.iface[i],
  1634. setup_interface_done);
  1635. if (ret)
  1636. goto out;
  1637. for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
  1638. if (interfaces.iface[i]->bss[0]->conf->tnc)
  1639. tnc++;
  1640. }
  1641. }
  1642. #ifdef EAP_TNC
  1643. if (tnc && tncs_global_init() < 0) {
  1644. wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
  1645. goto out;
  1646. }
  1647. #endif /* EAP_TNC */
  1648. if (daemonize && os_daemonize(pid_file)) {
  1649. perror("daemon");
  1650. goto out;
  1651. }
  1652. #ifndef CONFIG_NATIVE_WINDOWS
  1653. openlog("hostapd", 0, LOG_DAEMON);
  1654. #endif /* CONFIG_NATIVE_WINDOWS */
  1655. eloop_run();
  1656. /* Disconnect associated stations from all interfaces and BSSes */
  1657. for (i = 0; i < interfaces.count; i++) {
  1658. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1659. struct hostapd_data *hapd =
  1660. interfaces.iface[i]->bss[j];
  1661. hostapd_free_stas(hapd);
  1662. hostapd_flush_old_stations(hapd);
  1663. }
  1664. }
  1665. ret = 0;
  1666. out:
  1667. /* Deinitialize all interfaces */
  1668. for (i = 0; i < interfaces.count; i++) {
  1669. if (!interfaces.iface[i])
  1670. continue;
  1671. hostapd_setup_interface_stop(interfaces.iface[i]);
  1672. hostapd_cleanup_iface_pre(interfaces.iface[i]);
  1673. for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
  1674. struct hostapd_data *hapd =
  1675. interfaces.iface[i]->bss[j];
  1676. hostapd_cleanup(hapd);
  1677. if (j == interfaces.iface[i]->num_bss - 1 &&
  1678. hapd->driver)
  1679. hostapd_driver_deinit(hapd);
  1680. }
  1681. for (j = 0; j < interfaces.iface[i]->num_bss; j++)
  1682. os_free(interfaces.iface[i]->bss[j]);
  1683. hostapd_cleanup_iface(interfaces.iface[i]);
  1684. }
  1685. os_free(interfaces.iface);
  1686. #ifdef EAP_TNC
  1687. tncs_global_deinit();
  1688. #endif /* EAP_TNC */
  1689. eloop_destroy();
  1690. #ifndef CONFIG_NATIVE_WINDOWS
  1691. closelog();
  1692. #endif /* CONFIG_NATIVE_WINDOWS */
  1693. eap_server_unregister_methods();
  1694. os_daemonize_terminate(pid_file);
  1695. return ret;
  1696. }