wps_hostapd.c 48 KB

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