hostapd.c 54 KB

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