wps_hostapd.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /*
  2. * hostapd / WPS integration
  3. * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "eapol_auth/eapol_auth_sm.h"
  16. #include "eapol_auth/eapol_auth_sm_i.h"
  17. #include "wps/wps.h"
  18. #include "wps/wps_defs.h"
  19. #include "wps/wps_dev_attr.h"
  20. #include "wps/wps_attr_parse.h"
  21. #include "hostapd.h"
  22. #include "ap_config.h"
  23. #include "ap_drv_ops.h"
  24. #include "beacon.h"
  25. #include "sta_info.h"
  26. #include "wps_hostapd.h"
  27. #ifdef CONFIG_WPS_UPNP
  28. #include "wps/wps_upnp.h"
  29. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  30. struct wps_context *wps);
  31. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
  32. #endif /* CONFIG_WPS_UPNP */
  33. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
  34. const u8 *bssid,
  35. const u8 *ie, size_t ie_len,
  36. int ssi_signal);
  37. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
  38. static void hostapd_wps_nfc_clear(struct wps_context *wps);
  39. struct wps_for_each_data {
  40. int (*func)(struct hostapd_data *h, void *ctx);
  41. void *ctx;
  42. struct hostapd_data *calling_hapd;
  43. };
  44. static int wps_for_each(struct hostapd_iface *iface, void *ctx)
  45. {
  46. struct wps_for_each_data *data = ctx;
  47. size_t j;
  48. if (iface == NULL)
  49. return 0;
  50. for (j = 0; j < iface->num_bss; j++) {
  51. struct hostapd_data *hapd = iface->bss[j];
  52. int ret;
  53. if (hapd != data->calling_hapd &&
  54. (hapd->conf->wps_independent ||
  55. data->calling_hapd->conf->wps_independent))
  56. continue;
  57. ret = data->func(hapd, data->ctx);
  58. if (ret)
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. static int hostapd_wps_for_each(struct hostapd_data *hapd,
  64. int (*func)(struct hostapd_data *h, void *ctx),
  65. void *ctx)
  66. {
  67. struct hostapd_iface *iface = hapd->iface;
  68. struct wps_for_each_data data;
  69. data.func = func;
  70. data.ctx = ctx;
  71. data.calling_hapd = hapd;
  72. if (iface->interfaces == NULL ||
  73. iface->interfaces->for_each_interface == NULL)
  74. return wps_for_each(iface, &data);
  75. return iface->interfaces->for_each_interface(iface->interfaces,
  76. wps_for_each, &data);
  77. }
  78. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
  79. const u8 *p2p_dev_addr, const u8 *psk,
  80. size_t psk_len)
  81. {
  82. struct hostapd_data *hapd = ctx;
  83. struct hostapd_wpa_psk *p;
  84. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  85. if (is_zero_ether_addr(p2p_dev_addr)) {
  86. wpa_printf(MSG_DEBUG,
  87. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
  88. MAC2STR(mac_addr));
  89. } else {
  90. wpa_printf(MSG_DEBUG,
  91. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
  92. " P2P Device Addr " MACSTR,
  93. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  94. }
  95. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  96. if (psk_len != PMK_LEN) {
  97. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  98. (unsigned long) psk_len);
  99. return -1;
  100. }
  101. /* Add the new PSK to runtime PSK list */
  102. p = os_zalloc(sizeof(*p));
  103. if (p == NULL)
  104. return -1;
  105. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  106. os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  107. os_memcpy(p->psk, psk, PMK_LEN);
  108. if (hapd->new_psk_cb) {
  109. hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
  110. psk, psk_len);
  111. }
  112. p->next = ssid->wpa_psk;
  113. ssid->wpa_psk = p;
  114. if (ssid->wpa_psk_file) {
  115. FILE *f;
  116. char hex[PMK_LEN * 2 + 1];
  117. /* Add the new PSK to PSK list file */
  118. f = fopen(ssid->wpa_psk_file, "a");
  119. if (f == NULL) {
  120. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  121. "'%s'", ssid->wpa_psk_file);
  122. return -1;
  123. }
  124. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  125. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  126. fclose(f);
  127. }
  128. return 0;
  129. }
  130. static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
  131. struct wpabuf *probe_resp_ie)
  132. {
  133. struct hostapd_data *hapd = ctx;
  134. wpabuf_free(hapd->wps_beacon_ie);
  135. hapd->wps_beacon_ie = beacon_ie;
  136. wpabuf_free(hapd->wps_probe_resp_ie);
  137. hapd->wps_probe_resp_ie = probe_resp_ie;
  138. if (hapd->beacon_set_done)
  139. ieee802_11_set_beacon(hapd);
  140. return hostapd_set_ap_wps_ie(hapd);
  141. }
  142. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  143. const struct wps_device_data *dev)
  144. {
  145. struct hostapd_data *hapd = ctx;
  146. char uuid[40], txt[400];
  147. int len;
  148. char devtype[WPS_DEV_TYPE_BUFSIZE];
  149. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  150. return;
  151. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  152. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  153. "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
  154. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  155. dev->manufacturer, dev->model_name,
  156. dev->model_number, dev->serial_number,
  157. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  158. sizeof(devtype)));
  159. if (!os_snprintf_error(sizeof(txt), len))
  160. wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
  161. if (hapd->conf->wps_pin_requests) {
  162. FILE *f;
  163. struct os_time t;
  164. f = fopen(hapd->conf->wps_pin_requests, "a");
  165. if (f == NULL)
  166. return;
  167. os_get_time(&t);
  168. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  169. "\t%s\n",
  170. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  171. dev->manufacturer, dev->model_name, dev->model_number,
  172. dev->serial_number,
  173. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  174. sizeof(devtype)));
  175. fclose(f);
  176. }
  177. }
  178. struct wps_stop_reg_data {
  179. struct hostapd_data *current_hapd;
  180. const u8 *uuid_e;
  181. const u8 *dev_pw;
  182. size_t dev_pw_len;
  183. };
  184. static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
  185. {
  186. struct wps_stop_reg_data *data = ctx;
  187. if (hapd != data->current_hapd && hapd->wps != NULL)
  188. wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
  189. data->dev_pw, data->dev_pw_len);
  190. return 0;
  191. }
  192. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  193. const u8 *uuid_e, const u8 *dev_pw,
  194. size_t dev_pw_len)
  195. {
  196. struct hostapd_data *hapd = ctx;
  197. char uuid[40];
  198. struct wps_stop_reg_data data;
  199. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  200. return;
  201. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  202. MAC2STR(mac_addr), uuid);
  203. if (hapd->wps_reg_success_cb)
  204. hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
  205. mac_addr, uuid_e);
  206. data.current_hapd = hapd;
  207. data.uuid_e = uuid_e;
  208. data.dev_pw = dev_pw;
  209. data.dev_pw_len = dev_pw_len;
  210. hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
  211. }
  212. static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
  213. const u8 *uuid_e,
  214. const u8 *pri_dev_type,
  215. u16 config_methods,
  216. u16 dev_password_id, u8 request_type,
  217. const char *dev_name)
  218. {
  219. struct hostapd_data *hapd = ctx;
  220. char uuid[40];
  221. char devtype[WPS_DEV_TYPE_BUFSIZE];
  222. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  223. return;
  224. if (dev_name == NULL)
  225. dev_name = "";
  226. wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
  227. " %s %s 0x%x %u %u [%s]",
  228. MAC2STR(addr), uuid,
  229. wps_dev_type_bin2str(pri_dev_type, devtype,
  230. sizeof(devtype)),
  231. config_methods, dev_password_id, request_type, dev_name);
  232. }
  233. static int str_starts(const char *str, const char *start)
  234. {
  235. return os_strncmp(str, start, os_strlen(start)) == 0;
  236. }
  237. static void wps_reload_config(void *eloop_data, void *user_ctx)
  238. {
  239. struct hostapd_iface *iface = eloop_data;
  240. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  241. if (iface->interfaces == NULL ||
  242. iface->interfaces->reload_config(iface) < 0) {
  243. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  244. "configuration");
  245. }
  246. }
  247. void hostapd_wps_eap_completed(struct hostapd_data *hapd)
  248. {
  249. /*
  250. * Reduce race condition of the station trying to reconnect immediately
  251. * after AP reconfiguration through WPS by rescheduling the reload
  252. * timeout to happen after EAP completion rather than the originally
  253. * scheduled 100 ms after new configuration became known.
  254. */
  255. if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
  256. 1)
  257. wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
  258. }
  259. static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
  260. size_t attr_len)
  261. {
  262. size_t blen = attr_len * 2 + 1;
  263. char *buf = os_malloc(blen);
  264. if (buf) {
  265. wpa_snprintf_hex(buf, blen, attr, attr_len);
  266. wpa_msg(hapd->msg_ctx, MSG_INFO,
  267. WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
  268. os_free(buf);
  269. }
  270. }
  271. static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
  272. const struct wps_credential *cred)
  273. {
  274. struct hostapd_bss_config *bss = hapd->conf;
  275. wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
  276. bss->wps_state = 2;
  277. if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
  278. os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
  279. bss->ssid.ssid_len = cred->ssid_len;
  280. bss->ssid.ssid_set = 1;
  281. }
  282. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  283. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  284. bss->wpa = 3;
  285. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  286. bss->wpa = 2;
  287. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  288. bss->wpa = 1;
  289. else
  290. bss->wpa = 0;
  291. if (bss->wpa) {
  292. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
  293. bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  294. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  295. bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  296. bss->wpa_pairwise = 0;
  297. if (cred->encr_type & WPS_ENCR_AES)
  298. bss->wpa_pairwise |= WPA_CIPHER_CCMP;
  299. if (cred->encr_type & WPS_ENCR_TKIP)
  300. bss->wpa_pairwise |= WPA_CIPHER_TKIP;
  301. bss->rsn_pairwise = bss->wpa_pairwise;
  302. bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
  303. bss->wpa_pairwise,
  304. bss->rsn_pairwise);
  305. if (cred->key_len >= 8 && cred->key_len < 64) {
  306. os_free(bss->ssid.wpa_passphrase);
  307. bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
  308. if (bss->ssid.wpa_passphrase)
  309. os_memcpy(bss->ssid.wpa_passphrase, cred->key,
  310. cred->key_len);
  311. hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
  312. } else if (cred->key_len == 64) {
  313. hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
  314. bss->ssid.wpa_psk =
  315. os_zalloc(sizeof(struct hostapd_wpa_psk));
  316. if (bss->ssid.wpa_psk &&
  317. hexstr2bin((const char *) cred->key,
  318. bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
  319. bss->ssid.wpa_psk->group = 1;
  320. os_free(bss->ssid.wpa_passphrase);
  321. bss->ssid.wpa_passphrase = NULL;
  322. }
  323. }
  324. bss->auth_algs = 1;
  325. } else {
  326. /*
  327. * WPS 2.0 does not allow WEP to be configured, so no need to
  328. * process that option here either.
  329. */
  330. bss->auth_algs = 1;
  331. }
  332. /* Schedule configuration reload after short period of time to allow
  333. * EAP-WSC to be finished.
  334. */
  335. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  336. NULL);
  337. return 0;
  338. }
  339. static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
  340. {
  341. const struct wps_credential *cred = ctx;
  342. FILE *oconf, *nconf;
  343. size_t len, i;
  344. char *tmp_fname;
  345. char buf[1024];
  346. int multi_bss;
  347. int wpa;
  348. if (hapd->wps == NULL)
  349. return 0;
  350. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  351. cred->cred_attr, cred->cred_attr_len);
  352. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  353. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  354. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  355. cred->auth_type);
  356. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  357. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  358. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  359. cred->key, cred->key_len);
  360. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  361. MAC2STR(cred->mac_addr));
  362. if ((hapd->conf->wps_cred_processing == 1 ||
  363. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  364. hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
  365. } else if (hapd->conf->wps_cred_processing == 1 ||
  366. hapd->conf->wps_cred_processing == 2) {
  367. struct wpabuf *attr;
  368. attr = wpabuf_alloc(200);
  369. if (attr && wps_build_credential_wrap(attr, cred) == 0)
  370. hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
  371. wpabuf_len(attr));
  372. wpabuf_free(attr);
  373. } else
  374. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  375. if (hapd->conf->wps_cred_processing == 1)
  376. return 0;
  377. os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
  378. hapd->wps->ssid_len = cred->ssid_len;
  379. hapd->wps->encr_types = cred->encr_type;
  380. hapd->wps->auth_types = cred->auth_type;
  381. hapd->wps->ap_encr_type = cred->encr_type;
  382. hapd->wps->ap_auth_type = cred->auth_type;
  383. if (cred->key_len == 0) {
  384. os_free(hapd->wps->network_key);
  385. hapd->wps->network_key = NULL;
  386. hapd->wps->network_key_len = 0;
  387. } else {
  388. if (hapd->wps->network_key == NULL ||
  389. hapd->wps->network_key_len < cred->key_len) {
  390. hapd->wps->network_key_len = 0;
  391. os_free(hapd->wps->network_key);
  392. hapd->wps->network_key = os_malloc(cred->key_len);
  393. if (hapd->wps->network_key == NULL)
  394. return -1;
  395. }
  396. hapd->wps->network_key_len = cred->key_len;
  397. os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
  398. }
  399. hapd->wps->wps_state = WPS_STATE_CONFIGURED;
  400. if (hapd->iface->config_fname == NULL)
  401. return hapd_wps_reconfig_in_memory(hapd, cred);
  402. len = os_strlen(hapd->iface->config_fname) + 5;
  403. tmp_fname = os_malloc(len);
  404. if (tmp_fname == NULL)
  405. return -1;
  406. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  407. oconf = fopen(hapd->iface->config_fname, "r");
  408. if (oconf == NULL) {
  409. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  410. "configuration file");
  411. os_free(tmp_fname);
  412. return -1;
  413. }
  414. nconf = fopen(tmp_fname, "w");
  415. if (nconf == NULL) {
  416. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  417. "configuration file");
  418. os_free(tmp_fname);
  419. fclose(oconf);
  420. return -1;
  421. }
  422. fprintf(nconf, "# WPS configuration - START\n");
  423. fprintf(nconf, "wps_state=2\n");
  424. if (is_hex(cred->ssid, cred->ssid_len)) {
  425. fprintf(nconf, "ssid2=");
  426. for (i = 0; i < cred->ssid_len; i++)
  427. fprintf(nconf, "%02x", cred->ssid[i]);
  428. fprintf(nconf, "\n");
  429. } else {
  430. fprintf(nconf, "ssid=");
  431. for (i = 0; i < cred->ssid_len; i++)
  432. fputc(cred->ssid[i], nconf);
  433. fprintf(nconf, "\n");
  434. }
  435. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  436. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  437. wpa = 3;
  438. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  439. wpa = 2;
  440. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  441. wpa = 1;
  442. else
  443. wpa = 0;
  444. if (wpa) {
  445. char *prefix;
  446. fprintf(nconf, "wpa=%d\n", wpa);
  447. fprintf(nconf, "wpa_key_mgmt=");
  448. prefix = "";
  449. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  450. fprintf(nconf, "WPA-EAP");
  451. prefix = " ";
  452. }
  453. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  454. fprintf(nconf, "%sWPA-PSK", prefix);
  455. fprintf(nconf, "\n");
  456. fprintf(nconf, "wpa_pairwise=");
  457. prefix = "";
  458. if (cred->encr_type & WPS_ENCR_AES) {
  459. fprintf(nconf, "CCMP");
  460. prefix = " ";
  461. }
  462. if (cred->encr_type & WPS_ENCR_TKIP) {
  463. fprintf(nconf, "%sTKIP", prefix);
  464. }
  465. fprintf(nconf, "\n");
  466. if (cred->key_len >= 8 && cred->key_len < 64) {
  467. fprintf(nconf, "wpa_passphrase=");
  468. for (i = 0; i < cred->key_len; i++)
  469. fputc(cred->key[i], nconf);
  470. fprintf(nconf, "\n");
  471. } else if (cred->key_len == 64) {
  472. fprintf(nconf, "wpa_psk=");
  473. for (i = 0; i < cred->key_len; i++)
  474. fputc(cred->key[i], nconf);
  475. fprintf(nconf, "\n");
  476. } else {
  477. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  478. "for WPA/WPA2",
  479. (unsigned long) cred->key_len);
  480. }
  481. fprintf(nconf, "auth_algs=1\n");
  482. } else {
  483. /*
  484. * WPS 2.0 does not allow WEP to be configured, so no need to
  485. * process that option here either.
  486. */
  487. fprintf(nconf, "auth_algs=1\n");
  488. }
  489. fprintf(nconf, "# WPS configuration - END\n");
  490. multi_bss = 0;
  491. while (fgets(buf, sizeof(buf), oconf)) {
  492. if (os_strncmp(buf, "bss=", 4) == 0)
  493. multi_bss = 1;
  494. if (!multi_bss &&
  495. (str_starts(buf, "ssid=") ||
  496. str_starts(buf, "ssid2=") ||
  497. str_starts(buf, "auth_algs=") ||
  498. str_starts(buf, "wep_default_key=") ||
  499. str_starts(buf, "wep_key") ||
  500. str_starts(buf, "wps_state=") ||
  501. str_starts(buf, "wpa=") ||
  502. str_starts(buf, "wpa_psk=") ||
  503. str_starts(buf, "wpa_pairwise=") ||
  504. str_starts(buf, "rsn_pairwise=") ||
  505. str_starts(buf, "wpa_key_mgmt=") ||
  506. str_starts(buf, "wpa_passphrase="))) {
  507. fprintf(nconf, "#WPS# %s", buf);
  508. } else
  509. fprintf(nconf, "%s", buf);
  510. }
  511. fclose(nconf);
  512. fclose(oconf);
  513. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  514. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  515. "configuration file: %s", strerror(errno));
  516. os_free(tmp_fname);
  517. return -1;
  518. }
  519. os_free(tmp_fname);
  520. /* Schedule configuration reload after short period of time to allow
  521. * EAP-WSC to be finished.
  522. */
  523. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  524. NULL);
  525. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  526. return 0;
  527. }
  528. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  529. {
  530. struct hostapd_data *hapd = ctx;
  531. return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
  532. }
  533. static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
  534. {
  535. struct hostapd_data *hapd = eloop_data;
  536. if (hapd->conf->ap_setup_locked)
  537. return;
  538. if (hapd->ap_pin_failures_consecutive >= 10)
  539. return;
  540. wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
  541. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  542. hapd->wps->ap_setup_locked = 0;
  543. wps_registrar_update_ie(hapd->wps->registrar);
  544. }
  545. static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
  546. {
  547. struct wps_event_pwd_auth_fail *data = ctx;
  548. if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  549. return 0;
  550. /*
  551. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  552. * for some time if this happens multiple times to slow down brute
  553. * force attacks.
  554. */
  555. hapd->ap_pin_failures++;
  556. hapd->ap_pin_failures_consecutive++;
  557. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
  558. "(%u consecutive)",
  559. hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
  560. if (hapd->ap_pin_failures < 3)
  561. return 0;
  562. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  563. hapd->wps->ap_setup_locked = 1;
  564. wps_registrar_update_ie(hapd->wps->registrar);
  565. if (!hapd->conf->ap_setup_locked &&
  566. hapd->ap_pin_failures_consecutive >= 10) {
  567. /*
  568. * In indefinite lockdown - disable automatic AP PIN
  569. * reenablement.
  570. */
  571. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  572. wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
  573. } else if (!hapd->conf->ap_setup_locked) {
  574. if (hapd->ap_pin_lockout_time == 0)
  575. hapd->ap_pin_lockout_time = 60;
  576. else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
  577. (hapd->ap_pin_failures % 3) == 0)
  578. hapd->ap_pin_lockout_time *= 2;
  579. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
  580. hapd->ap_pin_lockout_time);
  581. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  582. eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
  583. hostapd_wps_reenable_ap_pin, hapd,
  584. NULL);
  585. }
  586. return 0;
  587. }
  588. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  589. struct wps_event_pwd_auth_fail *data)
  590. {
  591. /* Update WPS Status - Authentication Failure */
  592. wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
  593. hapd->wps_stats.status = WPS_STATUS_FAILURE;
  594. hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
  595. os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
  596. hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
  597. }
  598. static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
  599. {
  600. if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
  601. return 0;
  602. if (hapd->ap_pin_failures_consecutive == 0)
  603. return 0;
  604. wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
  605. "- total validation failures %u (%u consecutive)",
  606. hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
  607. hapd->ap_pin_failures_consecutive = 0;
  608. return 0;
  609. }
  610. static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
  611. {
  612. hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
  613. }
  614. static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
  615. {
  616. /* Update WPS Status - PBC Overlap */
  617. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
  618. }
  619. static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
  620. {
  621. /* Update WPS PBC Status:PBC Timeout */
  622. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
  623. }
  624. static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
  625. {
  626. /* Update WPS PBC status - Active */
  627. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
  628. }
  629. static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
  630. {
  631. /* Update WPS PBC status - Active */
  632. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
  633. }
  634. static void hostapd_wps_event_success(struct hostapd_data *hapd,
  635. struct wps_event_success *success)
  636. {
  637. /* Update WPS status - Success */
  638. hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
  639. hapd->wps_stats.status = WPS_STATUS_SUCCESS;
  640. os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
  641. }
  642. static void hostapd_wps_event_fail(struct hostapd_data *hapd,
  643. struct wps_event_fail *fail)
  644. {
  645. /* Update WPS status - Failure */
  646. hapd->wps_stats.status = WPS_STATUS_FAILURE;
  647. os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
  648. hapd->wps_stats.failure_reason = fail->error_indication;
  649. if (fail->error_indication > 0 &&
  650. fail->error_indication < NUM_WPS_EI_VALUES) {
  651. wpa_msg(hapd->msg_ctx, MSG_INFO,
  652. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  653. fail->msg, fail->config_error, fail->error_indication,
  654. wps_ei_str(fail->error_indication));
  655. } else {
  656. wpa_msg(hapd->msg_ctx, MSG_INFO,
  657. WPS_EVENT_FAIL "msg=%d config_error=%d",
  658. fail->msg, fail->config_error);
  659. }
  660. }
  661. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  662. union wps_event_data *data)
  663. {
  664. struct hostapd_data *hapd = ctx;
  665. switch (event) {
  666. case WPS_EV_M2D:
  667. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
  668. break;
  669. case WPS_EV_FAIL:
  670. hostapd_wps_event_fail(hapd, &data->fail);
  671. break;
  672. case WPS_EV_SUCCESS:
  673. hostapd_wps_event_success(hapd, &data->success);
  674. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
  675. break;
  676. case WPS_EV_PWD_AUTH_FAIL:
  677. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  678. break;
  679. case WPS_EV_PBC_OVERLAP:
  680. hostapd_wps_event_pbc_overlap(hapd);
  681. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
  682. break;
  683. case WPS_EV_PBC_TIMEOUT:
  684. hostapd_wps_event_pbc_timeout(hapd);
  685. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
  686. break;
  687. case WPS_EV_PBC_ACTIVE:
  688. hostapd_wps_event_pbc_active(hapd);
  689. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
  690. break;
  691. case WPS_EV_PBC_DISABLE:
  692. hostapd_wps_event_pbc_disable(hapd);
  693. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
  694. break;
  695. case WPS_EV_ER_AP_ADD:
  696. break;
  697. case WPS_EV_ER_AP_REMOVE:
  698. break;
  699. case WPS_EV_ER_ENROLLEE_ADD:
  700. break;
  701. case WPS_EV_ER_ENROLLEE_REMOVE:
  702. break;
  703. case WPS_EV_ER_AP_SETTINGS:
  704. break;
  705. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  706. break;
  707. case WPS_EV_AP_PIN_SUCCESS:
  708. hostapd_wps_ap_pin_success(hapd);
  709. break;
  710. }
  711. if (hapd->wps_event_cb)
  712. hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
  713. }
  714. static int hostapd_wps_rf_band_cb(void *ctx)
  715. {
  716. struct hostapd_data *hapd = ctx;
  717. return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  718. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  719. }
  720. static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
  721. {
  722. wpabuf_free(hapd->wps_beacon_ie);
  723. hapd->wps_beacon_ie = NULL;
  724. wpabuf_free(hapd->wps_probe_resp_ie);
  725. hapd->wps_probe_resp_ie = NULL;
  726. if (deinit_only)
  727. return;
  728. hostapd_set_ap_wps_ie(hapd);
  729. }
  730. static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
  731. {
  732. const u8 **uuid = ctx;
  733. size_t j;
  734. if (iface == NULL)
  735. return 0;
  736. for (j = 0; j < iface->num_bss; j++) {
  737. struct hostapd_data *hapd = iface->bss[j];
  738. if (hapd->wps && !hapd->conf->wps_independent &&
  739. !is_nil_uuid(hapd->wps->uuid)) {
  740. *uuid = hapd->wps->uuid;
  741. return 1;
  742. }
  743. }
  744. return 0;
  745. }
  746. static const u8 * get_own_uuid(struct hostapd_iface *iface)
  747. {
  748. const u8 *uuid;
  749. if (iface->interfaces == NULL ||
  750. iface->interfaces->for_each_interface == NULL)
  751. return NULL;
  752. uuid = NULL;
  753. iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
  754. &uuid);
  755. return uuid;
  756. }
  757. static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
  758. {
  759. int *count= ctx;
  760. (*count)++;
  761. return 0;
  762. }
  763. static int interface_count(struct hostapd_iface *iface)
  764. {
  765. int count = 0;
  766. if (iface->interfaces == NULL ||
  767. iface->interfaces->for_each_interface == NULL)
  768. return 0;
  769. iface->interfaces->for_each_interface(iface->interfaces,
  770. count_interface_cb, &count);
  771. return count;
  772. }
  773. static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
  774. struct wps_context *wps)
  775. {
  776. int i;
  777. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  778. wpabuf_free(wps->dev.vendor_ext[i]);
  779. wps->dev.vendor_ext[i] = NULL;
  780. if (hapd->conf->wps_vendor_ext[i] == NULL)
  781. continue;
  782. wps->dev.vendor_ext[i] =
  783. wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
  784. if (wps->dev.vendor_ext[i] == NULL) {
  785. while (--i >= 0)
  786. wpabuf_free(wps->dev.vendor_ext[i]);
  787. return -1;
  788. }
  789. }
  790. return 0;
  791. }
  792. static void hostapd_free_wps(struct wps_context *wps)
  793. {
  794. int i;
  795. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
  796. wpabuf_free(wps->dev.vendor_ext[i]);
  797. wps_device_data_free(&wps->dev);
  798. os_free(wps->network_key);
  799. hostapd_wps_nfc_clear(wps);
  800. wpabuf_free(wps->dh_pubkey);
  801. wpabuf_free(wps->dh_privkey);
  802. os_free(wps);
  803. }
  804. int hostapd_init_wps(struct hostapd_data *hapd,
  805. struct hostapd_bss_config *conf)
  806. {
  807. struct wps_context *wps;
  808. struct wps_registrar_config cfg;
  809. if (conf->wps_state == 0) {
  810. hostapd_wps_clear_ies(hapd, 0);
  811. return 0;
  812. }
  813. wps = os_zalloc(sizeof(*wps));
  814. if (wps == NULL)
  815. return -1;
  816. wps->cred_cb = hostapd_wps_cred_cb;
  817. wps->event_cb = hostapd_wps_event_cb;
  818. wps->rf_band_cb = hostapd_wps_rf_band_cb;
  819. wps->cb_ctx = hapd;
  820. os_memset(&cfg, 0, sizeof(cfg));
  821. wps->wps_state = hapd->conf->wps_state;
  822. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  823. if (is_nil_uuid(hapd->conf->uuid)) {
  824. const u8 *uuid;
  825. uuid = get_own_uuid(hapd->iface);
  826. if (uuid && !conf->wps_independent) {
  827. os_memcpy(wps->uuid, uuid, UUID_LEN);
  828. wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
  829. "interface", wps->uuid, UUID_LEN);
  830. } else {
  831. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  832. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  833. "address", wps->uuid, UUID_LEN);
  834. }
  835. } else {
  836. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  837. wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
  838. wps->uuid, UUID_LEN);
  839. }
  840. wps->ssid_len = hapd->conf->ssid.ssid_len;
  841. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  842. wps->ap = 1;
  843. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  844. wps->dev.device_name = hapd->conf->device_name ?
  845. os_strdup(hapd->conf->device_name) : NULL;
  846. wps->dev.manufacturer = hapd->conf->manufacturer ?
  847. os_strdup(hapd->conf->manufacturer) : NULL;
  848. wps->dev.model_name = hapd->conf->model_name ?
  849. os_strdup(hapd->conf->model_name) : NULL;
  850. wps->dev.model_number = hapd->conf->model_number ?
  851. os_strdup(hapd->conf->model_number) : NULL;
  852. wps->dev.serial_number = hapd->conf->serial_number ?
  853. os_strdup(hapd->conf->serial_number) : NULL;
  854. wps->config_methods =
  855. wps_config_methods_str2bin(hapd->conf->config_methods);
  856. if ((wps->config_methods &
  857. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  858. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  859. wpa_printf(MSG_INFO, "WPS: Converting display to "
  860. "virtual_display for WPS 2.0 compliance");
  861. wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  862. }
  863. if ((wps->config_methods &
  864. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  865. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  866. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  867. "virtual_push_button for WPS 2.0 compliance");
  868. wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  869. }
  870. os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
  871. WPS_DEV_TYPE_LEN);
  872. if (hostapd_wps_set_vendor_ext(hapd, wps) < 0)
  873. goto fail;
  874. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  875. if (conf->wps_rf_bands) {
  876. wps->dev.rf_bands = conf->wps_rf_bands;
  877. } else {
  878. wps->dev.rf_bands =
  879. hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  880. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  881. }
  882. if (conf->wpa & WPA_PROTO_RSN) {
  883. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  884. wps->auth_types |= WPS_AUTH_WPA2PSK;
  885. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  886. wps->auth_types |= WPS_AUTH_WPA2;
  887. if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP))
  888. wps->encr_types |= WPS_ENCR_AES;
  889. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  890. wps->encr_types |= WPS_ENCR_TKIP;
  891. }
  892. if (conf->wpa & WPA_PROTO_WPA) {
  893. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  894. wps->auth_types |= WPS_AUTH_WPAPSK;
  895. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  896. wps->auth_types |= WPS_AUTH_WPA;
  897. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  898. wps->encr_types |= WPS_ENCR_AES;
  899. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  900. wps->encr_types |= WPS_ENCR_TKIP;
  901. }
  902. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  903. wps->encr_types |= WPS_ENCR_NONE;
  904. wps->auth_types |= WPS_AUTH_OPEN;
  905. }
  906. if (conf->ssid.wpa_psk_file) {
  907. /* Use per-device PSKs */
  908. } else if (conf->ssid.wpa_passphrase) {
  909. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  910. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  911. } else if (conf->ssid.wpa_psk) {
  912. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  913. if (wps->network_key == NULL)
  914. goto fail;
  915. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  916. conf->ssid.wpa_psk->psk, PMK_LEN);
  917. wps->network_key_len = 2 * PMK_LEN;
  918. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  919. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  920. if (wps->network_key == NULL)
  921. goto fail;
  922. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  923. conf->ssid.wep.len[0]);
  924. wps->network_key_len = conf->ssid.wep.len[0];
  925. }
  926. if (conf->ssid.wpa_psk) {
  927. os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
  928. wps->psk_set = 1;
  929. }
  930. wps->ap_auth_type = wps->auth_types;
  931. wps->ap_encr_type = wps->encr_types;
  932. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  933. /* Override parameters to enable security by default */
  934. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  935. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  936. }
  937. wps->ap_settings = conf->ap_settings;
  938. wps->ap_settings_len = conf->ap_settings_len;
  939. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  940. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  941. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  942. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  943. cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
  944. cfg.cb_ctx = hapd;
  945. cfg.skip_cred_build = conf->skip_cred_build;
  946. cfg.extra_cred = conf->extra_cred;
  947. cfg.extra_cred_len = conf->extra_cred_len;
  948. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  949. conf->skip_cred_build;
  950. if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
  951. cfg.static_wep_only = 1;
  952. cfg.dualband = interface_count(hapd->iface) > 1;
  953. if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
  954. (WPS_RF_50GHZ | WPS_RF_24GHZ))
  955. cfg.dualband = 1;
  956. if (cfg.dualband)
  957. wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
  958. cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
  959. wps->registrar = wps_registrar_init(wps, &cfg);
  960. if (wps->registrar == NULL) {
  961. wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
  962. goto fail;
  963. }
  964. #ifdef CONFIG_WPS_UPNP
  965. wps->friendly_name = hapd->conf->friendly_name;
  966. wps->manufacturer_url = hapd->conf->manufacturer_url;
  967. wps->model_description = hapd->conf->model_description;
  968. wps->model_url = hapd->conf->model_url;
  969. wps->upc = hapd->conf->upc;
  970. #endif /* CONFIG_WPS_UPNP */
  971. hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
  972. hapd->wps = wps;
  973. return 0;
  974. fail:
  975. hostapd_free_wps(wps);
  976. return -1;
  977. }
  978. int hostapd_init_wps_complete(struct hostapd_data *hapd)
  979. {
  980. struct wps_context *wps = hapd->wps;
  981. if (wps == NULL)
  982. return 0;
  983. #ifdef CONFIG_WPS_UPNP
  984. if (hostapd_wps_upnp_init(hapd, wps) < 0) {
  985. wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
  986. wps_registrar_deinit(wps->registrar);
  987. hostapd_free_wps(wps);
  988. hapd->wps = NULL;
  989. return -1;
  990. }
  991. #endif /* CONFIG_WPS_UPNP */
  992. return 0;
  993. }
  994. static void hostapd_wps_nfc_clear(struct wps_context *wps)
  995. {
  996. #ifdef CONFIG_WPS_NFC
  997. wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
  998. wps->ap_nfc_dev_pw_id = 0;
  999. wpabuf_free(wps->ap_nfc_dh_pubkey);
  1000. wps->ap_nfc_dh_pubkey = NULL;
  1001. wpabuf_free(wps->ap_nfc_dh_privkey);
  1002. wps->ap_nfc_dh_privkey = NULL;
  1003. wpabuf_free(wps->ap_nfc_dev_pw);
  1004. wps->ap_nfc_dev_pw = NULL;
  1005. #endif /* CONFIG_WPS_NFC */
  1006. }
  1007. void hostapd_deinit_wps(struct hostapd_data *hapd)
  1008. {
  1009. eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
  1010. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1011. eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
  1012. if (hapd->wps == NULL) {
  1013. hostapd_wps_clear_ies(hapd, 1);
  1014. return;
  1015. }
  1016. #ifdef CONFIG_WPS_UPNP
  1017. hostapd_wps_upnp_deinit(hapd);
  1018. #endif /* CONFIG_WPS_UPNP */
  1019. wps_registrar_deinit(hapd->wps->registrar);
  1020. wps_free_pending_msgs(hapd->wps->upnp_msgs);
  1021. hostapd_free_wps(hapd->wps);
  1022. hapd->wps = NULL;
  1023. hostapd_wps_clear_ies(hapd, 1);
  1024. }
  1025. void hostapd_update_wps(struct hostapd_data *hapd)
  1026. {
  1027. if (hapd->wps == NULL)
  1028. return;
  1029. #ifdef CONFIG_WPS_UPNP
  1030. hapd->wps->friendly_name = hapd->conf->friendly_name;
  1031. hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
  1032. hapd->wps->model_description = hapd->conf->model_description;
  1033. hapd->wps->model_url = hapd->conf->model_url;
  1034. hapd->wps->upc = hapd->conf->upc;
  1035. #endif /* CONFIG_WPS_UPNP */
  1036. hostapd_wps_set_vendor_ext(hapd, hapd->wps);
  1037. if (hapd->conf->wps_state)
  1038. wps_registrar_update_ie(hapd->wps->registrar);
  1039. else
  1040. hostapd_deinit_wps(hapd);
  1041. }
  1042. struct wps_add_pin_data {
  1043. const u8 *addr;
  1044. const u8 *uuid;
  1045. const u8 *pin;
  1046. size_t pin_len;
  1047. int timeout;
  1048. int added;
  1049. };
  1050. static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
  1051. {
  1052. struct wps_add_pin_data *data = ctx;
  1053. int ret;
  1054. if (hapd->wps == NULL)
  1055. return 0;
  1056. ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
  1057. data->uuid, data->pin, data->pin_len,
  1058. data->timeout);
  1059. if (ret == 0)
  1060. data->added++;
  1061. return ret;
  1062. }
  1063. int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
  1064. const char *uuid, const char *pin, int timeout)
  1065. {
  1066. u8 u[UUID_LEN];
  1067. struct wps_add_pin_data data;
  1068. data.addr = addr;
  1069. data.uuid = u;
  1070. data.pin = (const u8 *) pin;
  1071. data.pin_len = os_strlen(pin);
  1072. data.timeout = timeout;
  1073. data.added = 0;
  1074. if (os_strcmp(uuid, "any") == 0)
  1075. data.uuid = NULL;
  1076. else {
  1077. if (uuid_str2bin(uuid, u))
  1078. return -1;
  1079. data.uuid = u;
  1080. }
  1081. if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
  1082. return -1;
  1083. return data.added ? 0 : -1;
  1084. }
  1085. static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
  1086. {
  1087. const u8 *p2p_dev_addr = ctx;
  1088. if (hapd->wps == NULL)
  1089. return -1;
  1090. return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
  1091. }
  1092. int hostapd_wps_button_pushed(struct hostapd_data *hapd,
  1093. const u8 *p2p_dev_addr)
  1094. {
  1095. return hostapd_wps_for_each(hapd, wps_button_pushed,
  1096. (void *) p2p_dev_addr);
  1097. }
  1098. static int wps_cancel(struct hostapd_data *hapd, void *ctx)
  1099. {
  1100. if (hapd->wps == NULL)
  1101. return -1;
  1102. wps_registrar_wps_cancel(hapd->wps->registrar);
  1103. ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
  1104. return 0;
  1105. }
  1106. int hostapd_wps_cancel(struct hostapd_data *hapd)
  1107. {
  1108. return hostapd_wps_for_each(hapd, wps_cancel, NULL);
  1109. }
  1110. static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
  1111. const u8 *bssid,
  1112. const u8 *ie, size_t ie_len,
  1113. int ssi_signal)
  1114. {
  1115. struct hostapd_data *hapd = ctx;
  1116. struct wpabuf *wps_ie;
  1117. struct ieee802_11_elems elems;
  1118. if (hapd->wps == NULL)
  1119. return 0;
  1120. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  1121. wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
  1122. MACSTR, MAC2STR(addr));
  1123. return 0;
  1124. }
  1125. if (elems.ssid && elems.ssid_len > 0 &&
  1126. (elems.ssid_len != hapd->conf->ssid.ssid_len ||
  1127. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
  1128. 0))
  1129. return 0; /* Not for us */
  1130. wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  1131. if (wps_ie == NULL)
  1132. return 0;
  1133. if (wps_validate_probe_req(wps_ie, addr) < 0) {
  1134. wpabuf_free(wps_ie);
  1135. return 0;
  1136. }
  1137. if (wpabuf_len(wps_ie) > 0) {
  1138. int p2p_wildcard = 0;
  1139. #ifdef CONFIG_P2P
  1140. if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  1141. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  1142. P2P_WILDCARD_SSID_LEN) == 0)
  1143. p2p_wildcard = 1;
  1144. #endif /* CONFIG_P2P */
  1145. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
  1146. p2p_wildcard);
  1147. #ifdef CONFIG_WPS_UPNP
  1148. /* FIX: what exactly should be included in the WLANEvent?
  1149. * WPS attributes? Full ProbeReq frame? */
  1150. if (!p2p_wildcard)
  1151. upnp_wps_device_send_wlan_event(
  1152. hapd->wps_upnp, addr,
  1153. UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
  1154. #endif /* CONFIG_WPS_UPNP */
  1155. }
  1156. wpabuf_free(wps_ie);
  1157. return 0;
  1158. }
  1159. #ifdef CONFIG_WPS_UPNP
  1160. static int hostapd_rx_req_put_wlan_response(
  1161. void *priv, enum upnp_wps_wlanevent_type ev_type,
  1162. const u8 *mac_addr, const struct wpabuf *msg,
  1163. enum wps_msg_type msg_type)
  1164. {
  1165. struct hostapd_data *hapd = priv;
  1166. struct sta_info *sta;
  1167. struct upnp_pending_message *p;
  1168. wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
  1169. MACSTR, ev_type, MAC2STR(mac_addr));
  1170. wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
  1171. wpabuf_head(msg), wpabuf_len(msg));
  1172. if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
  1173. wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
  1174. "PutWLANResponse WLANEventType %d", ev_type);
  1175. return -1;
  1176. }
  1177. /*
  1178. * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
  1179. * server implementation for delivery to the peer.
  1180. */
  1181. sta = ap_get_sta(hapd, mac_addr);
  1182. #ifndef CONFIG_WPS_STRICT
  1183. if (!sta) {
  1184. /*
  1185. * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
  1186. * Pick STA that is in an ongoing WPS registration without
  1187. * checking the MAC address.
  1188. */
  1189. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
  1190. "on NewWLANEventMAC; try wildcard match");
  1191. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1192. if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
  1193. break;
  1194. }
  1195. }
  1196. #endif /* CONFIG_WPS_STRICT */
  1197. if (!sta || !(sta->flags & WLAN_STA_WPS)) {
  1198. wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
  1199. return 0;
  1200. }
  1201. if (!sta->eapol_sm) {
  1202. /*
  1203. * This can happen, e.g., if an ER sends an extra message after
  1204. * the station has disassociated (but not fully
  1205. * deauthenticated).
  1206. */
  1207. wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
  1208. return 0;
  1209. }
  1210. p = os_zalloc(sizeof(*p));
  1211. if (p == NULL)
  1212. return -1;
  1213. os_memcpy(p->addr, sta->addr, ETH_ALEN);
  1214. p->msg = wpabuf_dup(msg);
  1215. p->type = msg_type;
  1216. p->next = hapd->wps->upnp_msgs;
  1217. hapd->wps->upnp_msgs = p;
  1218. return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
  1219. }
  1220. static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
  1221. struct wps_context *wps)
  1222. {
  1223. struct upnp_wps_device_ctx *ctx;
  1224. if (!hapd->conf->upnp_iface)
  1225. return 0;
  1226. ctx = os_zalloc(sizeof(*ctx));
  1227. if (ctx == NULL)
  1228. return -1;
  1229. ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
  1230. if (hapd->conf->ap_pin)
  1231. ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
  1232. hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
  1233. hapd->conf->upnp_iface);
  1234. if (hapd->wps_upnp == NULL)
  1235. return -1;
  1236. wps->wps_upnp = hapd->wps_upnp;
  1237. return 0;
  1238. }
  1239. static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
  1240. {
  1241. upnp_wps_device_deinit(hapd->wps_upnp, hapd);
  1242. }
  1243. #endif /* CONFIG_WPS_UPNP */
  1244. int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
  1245. char *buf, size_t buflen)
  1246. {
  1247. if (hapd->wps == NULL)
  1248. return 0;
  1249. return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
  1250. }
  1251. static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  1252. {
  1253. struct hostapd_data *hapd = eloop_data;
  1254. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  1255. hostapd_wps_ap_pin_disable(hapd);
  1256. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
  1257. }
  1258. static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
  1259. {
  1260. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  1261. hapd->ap_pin_failures = 0;
  1262. hapd->ap_pin_failures_consecutive = 0;
  1263. hapd->conf->ap_setup_locked = 0;
  1264. if (hapd->wps->ap_setup_locked) {
  1265. wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
  1266. hapd->wps->ap_setup_locked = 0;
  1267. wps_registrar_update_ie(hapd->wps->registrar);
  1268. }
  1269. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1270. if (timeout > 0)
  1271. eloop_register_timeout(timeout, 0,
  1272. hostapd_wps_ap_pin_timeout, hapd, NULL);
  1273. }
  1274. static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
  1275. {
  1276. os_free(hapd->conf->ap_pin);
  1277. hapd->conf->ap_pin = NULL;
  1278. #ifdef CONFIG_WPS_UPNP
  1279. upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
  1280. #endif /* CONFIG_WPS_UPNP */
  1281. eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
  1282. return 0;
  1283. }
  1284. void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
  1285. {
  1286. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  1287. hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
  1288. }
  1289. struct wps_ap_pin_data {
  1290. char pin_txt[9];
  1291. int timeout;
  1292. };
  1293. static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
  1294. {
  1295. struct wps_ap_pin_data *data = ctx;
  1296. os_free(hapd->conf->ap_pin);
  1297. hapd->conf->ap_pin = os_strdup(data->pin_txt);
  1298. #ifdef CONFIG_WPS_UPNP
  1299. upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
  1300. #endif /* CONFIG_WPS_UPNP */
  1301. hostapd_wps_ap_pin_enable(hapd, data->timeout);
  1302. return 0;
  1303. }
  1304. const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
  1305. {
  1306. unsigned int pin;
  1307. struct wps_ap_pin_data data;
  1308. pin = wps_generate_pin();
  1309. os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
  1310. data.timeout = timeout;
  1311. hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1312. return hapd->conf->ap_pin;
  1313. }
  1314. const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
  1315. {
  1316. return hapd->conf->ap_pin;
  1317. }
  1318. int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
  1319. int timeout)
  1320. {
  1321. struct wps_ap_pin_data data;
  1322. int ret;
  1323. ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
  1324. if (os_snprintf_error(sizeof(data.pin_txt), ret))
  1325. return -1;
  1326. data.timeout = timeout;
  1327. return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
  1328. }
  1329. static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
  1330. {
  1331. if (hapd->wps)
  1332. wps_registrar_update_ie(hapd->wps->registrar);
  1333. return 0;
  1334. }
  1335. void hostapd_wps_update_ie(struct hostapd_data *hapd)
  1336. {
  1337. hostapd_wps_for_each(hapd, wps_update_ie, NULL);
  1338. }
  1339. int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
  1340. const char *auth, const char *encr, const char *key)
  1341. {
  1342. struct wps_credential cred;
  1343. size_t len;
  1344. os_memset(&cred, 0, sizeof(cred));
  1345. len = os_strlen(ssid);
  1346. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1347. hexstr2bin(ssid, cred.ssid, len / 2))
  1348. return -1;
  1349. cred.ssid_len = len / 2;
  1350. if (os_strncmp(auth, "OPEN", 4) == 0)
  1351. cred.auth_type = WPS_AUTH_OPEN;
  1352. else if (os_strncmp(auth, "WPAPSK", 6) == 0)
  1353. cred.auth_type = WPS_AUTH_WPAPSK;
  1354. else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
  1355. cred.auth_type = WPS_AUTH_WPA2PSK;
  1356. else
  1357. return -1;
  1358. if (encr) {
  1359. if (os_strncmp(encr, "NONE", 4) == 0)
  1360. cred.encr_type = WPS_ENCR_NONE;
  1361. else if (os_strncmp(encr, "TKIP", 4) == 0)
  1362. cred.encr_type = WPS_ENCR_TKIP;
  1363. else if (os_strncmp(encr, "CCMP", 4) == 0)
  1364. cred.encr_type = WPS_ENCR_AES;
  1365. else
  1366. return -1;
  1367. } else
  1368. cred.encr_type = WPS_ENCR_NONE;
  1369. if (key) {
  1370. len = os_strlen(key);
  1371. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1372. hexstr2bin(key, cred.key, len / 2))
  1373. return -1;
  1374. cred.key_len = len / 2;
  1375. }
  1376. return wps_registrar_config_ap(hapd->wps->registrar, &cred);
  1377. }
  1378. #ifdef CONFIG_WPS_NFC
  1379. struct wps_nfc_password_token_data {
  1380. const u8 *oob_dev_pw;
  1381. size_t oob_dev_pw_len;
  1382. int added;
  1383. };
  1384. static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
  1385. {
  1386. struct wps_nfc_password_token_data *data = ctx;
  1387. int ret;
  1388. if (hapd->wps == NULL)
  1389. return 0;
  1390. ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
  1391. data->oob_dev_pw,
  1392. data->oob_dev_pw_len);
  1393. if (ret == 0)
  1394. data->added++;
  1395. return ret;
  1396. }
  1397. static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
  1398. struct wps_parse_attr *attr)
  1399. {
  1400. struct wps_nfc_password_token_data data;
  1401. data.oob_dev_pw = attr->oob_dev_password;
  1402. data.oob_dev_pw_len = attr->oob_dev_password_len;
  1403. data.added = 0;
  1404. if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
  1405. return -1;
  1406. return data.added ? 0 : -1;
  1407. }
  1408. static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
  1409. const struct wpabuf *wps)
  1410. {
  1411. struct wps_parse_attr attr;
  1412. wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
  1413. if (wps_parse_msg(wps, &attr)) {
  1414. wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
  1415. return -1;
  1416. }
  1417. if (attr.oob_dev_password)
  1418. return hostapd_wps_add_nfc_password_token(hapd, &attr);
  1419. wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
  1420. return -1;
  1421. }
  1422. int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
  1423. const struct wpabuf *data)
  1424. {
  1425. const struct wpabuf *wps = data;
  1426. struct wpabuf *tmp = NULL;
  1427. int ret;
  1428. if (wpabuf_len(data) < 4)
  1429. return -1;
  1430. if (*wpabuf_head_u8(data) != 0x10) {
  1431. /* Assume this contains full NDEF record */
  1432. tmp = ndef_parse_wifi(data);
  1433. if (tmp == NULL) {
  1434. wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
  1435. return -1;
  1436. }
  1437. wps = tmp;
  1438. }
  1439. ret = hostapd_wps_nfc_tag_process(hapd, wps);
  1440. wpabuf_free(tmp);
  1441. return ret;
  1442. }
  1443. struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
  1444. int ndef)
  1445. {
  1446. struct wpabuf *ret;
  1447. if (hapd->wps == NULL)
  1448. return NULL;
  1449. ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
  1450. hapd->iconf->channel);
  1451. if (ndef && ret) {
  1452. struct wpabuf *tmp;
  1453. tmp = ndef_build_wifi(ret);
  1454. wpabuf_free(ret);
  1455. if (tmp == NULL)
  1456. return NULL;
  1457. ret = tmp;
  1458. }
  1459. return ret;
  1460. }
  1461. struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
  1462. {
  1463. struct wpabuf *ret;
  1464. if (hapd->wps == NULL)
  1465. return NULL;
  1466. if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
  1467. struct wps_context *wps = hapd->wps;
  1468. if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
  1469. &hapd->conf->wps_nfc_dh_privkey) < 0)
  1470. return NULL;
  1471. hostapd_wps_nfc_clear(wps);
  1472. wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  1473. wps->ap_nfc_dh_pubkey =
  1474. wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
  1475. wps->ap_nfc_dh_privkey =
  1476. wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
  1477. if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
  1478. hostapd_wps_nfc_clear(wps);
  1479. return NULL;
  1480. }
  1481. }
  1482. ret = wps_build_nfc_handover_sel(hapd->wps,
  1483. hapd->conf->wps_nfc_dh_pubkey,
  1484. hapd->own_addr, hapd->iface->freq);
  1485. if (ndef && ret) {
  1486. struct wpabuf *tmp;
  1487. tmp = ndef_build_wifi(ret);
  1488. wpabuf_free(ret);
  1489. if (tmp == NULL)
  1490. return NULL;
  1491. ret = tmp;
  1492. }
  1493. return ret;
  1494. }
  1495. int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
  1496. const struct wpabuf *req,
  1497. const struct wpabuf *sel)
  1498. {
  1499. struct wpabuf *wps;
  1500. int ret = -1;
  1501. u16 wsc_len;
  1502. const u8 *pos;
  1503. struct wpabuf msg;
  1504. struct wps_parse_attr attr;
  1505. u16 dev_pw_id;
  1506. /*
  1507. * Enrollee/station is always initiator of the NFC connection handover,
  1508. * so use the request message here to find Enrollee public key hash.
  1509. */
  1510. wps = ndef_parse_wifi(req);
  1511. if (wps == NULL)
  1512. return -1;
  1513. wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
  1514. "payload from NFC connection handover");
  1515. wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
  1516. if (wpabuf_len(wps) < 2) {
  1517. wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
  1518. "Message");
  1519. goto out;
  1520. }
  1521. pos = wpabuf_head(wps);
  1522. wsc_len = WPA_GET_BE16(pos);
  1523. if (wsc_len > wpabuf_len(wps) - 2) {
  1524. wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
  1525. "in rt Wi-Fi Handover Request Message", wsc_len);
  1526. goto out;
  1527. }
  1528. pos += 2;
  1529. wpa_hexdump(MSG_DEBUG,
  1530. "WPS: WSC attributes in Wi-Fi Handover Request Message",
  1531. pos, wsc_len);
  1532. if (wsc_len < wpabuf_len(wps) - 2) {
  1533. wpa_hexdump(MSG_DEBUG,
  1534. "WPS: Ignore extra data after WSC attributes",
  1535. pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
  1536. }
  1537. wpabuf_set(&msg, pos, wsc_len);
  1538. ret = wps_parse_msg(&msg, &attr);
  1539. if (ret < 0) {
  1540. wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
  1541. "Wi-Fi Handover Request Message");
  1542. goto out;
  1543. }
  1544. if (attr.oob_dev_password == NULL ||
  1545. attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  1546. wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
  1547. "included in Wi-Fi Handover Request Message");
  1548. ret = -1;
  1549. goto out;
  1550. }
  1551. if (attr.uuid_e == NULL) {
  1552. wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
  1553. "Handover Request Message");
  1554. ret = -1;
  1555. goto out;
  1556. }
  1557. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
  1558. wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
  1559. attr.oob_dev_password, attr.oob_dev_password_len);
  1560. dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
  1561. WPS_OOB_PUBKEY_HASH_LEN);
  1562. if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
  1563. wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
  1564. "%u in Wi-Fi Handover Request Message", dev_pw_id);
  1565. ret = -1;
  1566. goto out;
  1567. }
  1568. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
  1569. attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
  1570. ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
  1571. attr.oob_dev_password,
  1572. DEV_PW_NFC_CONNECTION_HANDOVER,
  1573. NULL, 0, 1);
  1574. out:
  1575. wpabuf_free(wps);
  1576. return ret;
  1577. }
  1578. struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
  1579. {
  1580. if (hapd->conf->wps_nfc_pw_from_config) {
  1581. return wps_nfc_token_build(ndef,
  1582. hapd->conf->wps_nfc_dev_pw_id,
  1583. hapd->conf->wps_nfc_dh_pubkey,
  1584. hapd->conf->wps_nfc_dev_pw);
  1585. }
  1586. return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
  1587. &hapd->conf->wps_nfc_dh_pubkey,
  1588. &hapd->conf->wps_nfc_dh_privkey,
  1589. &hapd->conf->wps_nfc_dev_pw);
  1590. }
  1591. int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
  1592. {
  1593. struct wps_context *wps = hapd->wps;
  1594. struct wpabuf *pw;
  1595. if (wps == NULL)
  1596. return -1;
  1597. if (!hapd->conf->wps_nfc_dh_pubkey ||
  1598. !hapd->conf->wps_nfc_dh_privkey ||
  1599. !hapd->conf->wps_nfc_dev_pw ||
  1600. !hapd->conf->wps_nfc_dev_pw_id)
  1601. return -1;
  1602. hostapd_wps_nfc_clear(wps);
  1603. wpa_printf(MSG_DEBUG,
  1604. "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
  1605. hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
  1606. wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
  1607. wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
  1608. wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
  1609. pw = hapd->conf->wps_nfc_dev_pw;
  1610. wps->ap_nfc_dev_pw = wpabuf_alloc(
  1611. wpabuf_len(pw) * 2 + 1);
  1612. if (wps->ap_nfc_dev_pw) {
  1613. wpa_snprintf_hex_uppercase(
  1614. (char *) wpabuf_put(wps->ap_nfc_dev_pw,
  1615. wpabuf_len(pw) * 2),
  1616. wpabuf_len(pw) * 2 + 1,
  1617. wpabuf_head(pw), wpabuf_len(pw));
  1618. }
  1619. if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
  1620. !wps->ap_nfc_dev_pw) {
  1621. hostapd_wps_nfc_clear(wps);
  1622. return -1;
  1623. }
  1624. return 0;
  1625. }
  1626. void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
  1627. {
  1628. wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
  1629. hapd->conf->iface);
  1630. hostapd_wps_nfc_clear(hapd->wps);
  1631. }
  1632. #endif /* CONFIG_WPS_NFC */