hostapd.c 51 KB

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