hostapd.c 56 KB

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