hostapd.c 54 KB

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