hostapd.c 54 KB

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