hostapd.c 52 KB

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