wps_er.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * Wi-Fi Protected Setup - External Registrar
  3. * Copyright (c) 2009-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 "includes.h"
  9. #include "common.h"
  10. #include "base64.h"
  11. #include "uuid.h"
  12. #include "eloop.h"
  13. #include "httpread.h"
  14. #include "http_client.h"
  15. #include "http_server.h"
  16. #include "upnp_xml.h"
  17. #include "wps_i.h"
  18. #include "wps_upnp.h"
  19. #include "wps_upnp_i.h"
  20. #include "wps_er.h"
  21. static void wps_er_deinit_finish(void *eloop_data, void *user_ctx);
  22. static void wps_er_ap_timeout(void *eloop_data, void *user_ctx);
  23. static void wps_er_sta_timeout(void *eloop_data, void *user_ctx);
  24. static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg);
  25. static int wps_er_send_get_device_info(struct wps_er_ap *ap,
  26. void (*m1_handler)(struct wps_er_ap *ap,
  27. struct wpabuf *m1));
  28. static void wps_er_sta_event(struct wps_context *wps, struct wps_er_sta *sta,
  29. enum wps_event event)
  30. {
  31. union wps_event_data data;
  32. struct wps_event_er_enrollee *ev = &data.enrollee;
  33. if (wps->event_cb == NULL)
  34. return;
  35. os_memset(&data, 0, sizeof(data));
  36. ev->uuid = sta->uuid;
  37. ev->mac_addr = sta->addr;
  38. ev->m1_received = sta->m1_received;
  39. ev->config_methods = sta->config_methods;
  40. ev->dev_passwd_id = sta->dev_passwd_id;
  41. ev->pri_dev_type = sta->pri_dev_type;
  42. ev->dev_name = sta->dev_name;
  43. ev->manufacturer = sta->manufacturer;
  44. ev->model_name = sta->model_name;
  45. ev->model_number = sta->model_number;
  46. ev->serial_number = sta->serial_number;
  47. wps->event_cb(wps->cb_ctx, event, &data);
  48. }
  49. static struct wps_er_sta * wps_er_sta_get(struct wps_er_ap *ap, const u8 *addr,
  50. const u8 *uuid)
  51. {
  52. struct wps_er_sta *sta;
  53. dl_list_for_each(sta, &ap->sta, struct wps_er_sta, list) {
  54. if ((addr == NULL ||
  55. os_memcmp(sta->addr, addr, ETH_ALEN) == 0) &&
  56. (uuid == NULL ||
  57. os_memcmp(uuid, sta->uuid, WPS_UUID_LEN) == 0))
  58. return sta;
  59. }
  60. return NULL;
  61. }
  62. static void wps_er_sta_free(struct wps_er_sta *sta)
  63. {
  64. wps_er_sta_event(sta->ap->er->wps, sta, WPS_EV_ER_ENROLLEE_REMOVE);
  65. if (sta->wps)
  66. wps_deinit(sta->wps);
  67. os_free(sta->manufacturer);
  68. os_free(sta->model_name);
  69. os_free(sta->model_number);
  70. os_free(sta->serial_number);
  71. os_free(sta->dev_name);
  72. http_client_free(sta->http);
  73. eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
  74. os_free(sta->cred);
  75. os_free(sta);
  76. }
  77. static void wps_er_sta_remove_all(struct wps_er_ap *ap)
  78. {
  79. struct wps_er_sta *prev, *sta;
  80. dl_list_for_each_safe(sta, prev, &ap->sta, struct wps_er_sta, list)
  81. wps_er_sta_free(sta);
  82. }
  83. static struct wps_er_ap * wps_er_ap_get(struct wps_er *er,
  84. struct in_addr *addr, const u8 *uuid)
  85. {
  86. struct wps_er_ap *ap;
  87. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  88. if ((addr == NULL || ap->addr.s_addr == addr->s_addr) &&
  89. (uuid == NULL ||
  90. os_memcmp(uuid, ap->uuid, WPS_UUID_LEN) == 0))
  91. return ap;
  92. }
  93. return NULL;
  94. }
  95. static struct wps_er_ap * wps_er_ap_get_id(struct wps_er *er, unsigned int id)
  96. {
  97. struct wps_er_ap *ap;
  98. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  99. if (ap->id == id)
  100. return ap;
  101. }
  102. return NULL;
  103. }
  104. static void wps_er_ap_event(struct wps_context *wps, struct wps_er_ap *ap,
  105. enum wps_event event)
  106. {
  107. union wps_event_data data;
  108. struct wps_event_er_ap *evap = &data.ap;
  109. if (wps->event_cb == NULL)
  110. return;
  111. os_memset(&data, 0, sizeof(data));
  112. evap->uuid = ap->uuid;
  113. evap->friendly_name = ap->friendly_name;
  114. evap->manufacturer = ap->manufacturer;
  115. evap->manufacturer_url = ap->manufacturer_url;
  116. evap->model_description = ap->model_description;
  117. evap->model_name = ap->model_name;
  118. evap->model_number = ap->model_number;
  119. evap->model_url = ap->model_url;
  120. evap->serial_number = ap->serial_number;
  121. evap->upc = ap->upc;
  122. evap->pri_dev_type = ap->pri_dev_type;
  123. evap->wps_state = ap->wps_state;
  124. evap->mac_addr = ap->mac_addr;
  125. wps->event_cb(wps->cb_ctx, event, &data);
  126. }
  127. static void wps_er_ap_free(struct wps_er_ap *ap)
  128. {
  129. http_client_free(ap->http);
  130. ap->http = NULL;
  131. os_free(ap->location);
  132. os_free(ap->friendly_name);
  133. os_free(ap->manufacturer);
  134. os_free(ap->manufacturer_url);
  135. os_free(ap->model_description);
  136. os_free(ap->model_name);
  137. os_free(ap->model_number);
  138. os_free(ap->model_url);
  139. os_free(ap->serial_number);
  140. os_free(ap->udn);
  141. os_free(ap->upc);
  142. os_free(ap->scpd_url);
  143. os_free(ap->control_url);
  144. os_free(ap->event_sub_url);
  145. os_free(ap->ap_settings);
  146. os_free(ap);
  147. }
  148. static void wps_er_ap_unsubscribed(struct wps_er *er, struct wps_er_ap *ap)
  149. {
  150. wpa_printf(MSG_DEBUG, "WPS ER: Unsubscribed from AP %s (%s)",
  151. inet_ntoa(ap->addr), ap->location);
  152. dl_list_del(&ap->list);
  153. wps_er_ap_free(ap);
  154. if (er->deinitializing && dl_list_empty(&er->ap_unsubscribing)) {
  155. eloop_cancel_timeout(wps_er_deinit_finish, er, NULL);
  156. wps_er_deinit_finish(er, NULL);
  157. }
  158. }
  159. static void wps_er_http_unsubscribe_cb(void *ctx, struct http_client *c,
  160. enum http_client_event event)
  161. {
  162. struct wps_er_ap *ap = ctx;
  163. switch (event) {
  164. case HTTP_CLIENT_OK:
  165. wpa_printf(MSG_DEBUG, "WPS ER: Unsubscribed from events");
  166. ap->subscribed = 0;
  167. break;
  168. case HTTP_CLIENT_FAILED:
  169. case HTTP_CLIENT_INVALID_REPLY:
  170. case HTTP_CLIENT_TIMEOUT:
  171. wpa_printf(MSG_DEBUG, "WPS ER: Failed to unsubscribe from "
  172. "events");
  173. break;
  174. }
  175. http_client_free(ap->http);
  176. ap->http = NULL;
  177. /*
  178. * Need to get rid of the AP entry regardless of whether we managed to
  179. * unsubscribe cleanly or not.
  180. */
  181. wps_er_ap_unsubscribed(ap->er, ap);
  182. }
  183. static void wps_er_ap_unsubscribe(struct wps_er *er, struct wps_er_ap *ap)
  184. {
  185. struct wpabuf *req;
  186. struct sockaddr_in dst;
  187. char *url, *path;
  188. char sid[100];
  189. if (ap->event_sub_url == NULL) {
  190. wpa_printf(MSG_DEBUG, "WPS ER: No eventSubURL - cannot "
  191. "subscribe");
  192. goto fail;
  193. }
  194. if (ap->http) {
  195. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request - cannot "
  196. "send subscribe request");
  197. goto fail;
  198. }
  199. url = http_client_url_parse(ap->event_sub_url, &dst, &path);
  200. if (url == NULL) {
  201. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse eventSubURL");
  202. goto fail;
  203. }
  204. req = wpabuf_alloc(os_strlen(ap->event_sub_url) + 1000);
  205. if (req == NULL) {
  206. os_free(url);
  207. goto fail;
  208. }
  209. uuid_bin2str(ap->sid, sid, sizeof(sid));
  210. wpabuf_printf(req,
  211. "UNSUBSCRIBE %s HTTP/1.1\r\n"
  212. "HOST: %s:%d\r\n"
  213. "SID: uuid:%s\r\n"
  214. "\r\n",
  215. path, inet_ntoa(dst.sin_addr), ntohs(dst.sin_port), sid);
  216. os_free(url);
  217. wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Unsubscription request",
  218. wpabuf_head(req), wpabuf_len(req));
  219. ap->http = http_client_addr(&dst, req, 1000,
  220. wps_er_http_unsubscribe_cb, ap);
  221. if (ap->http == NULL) {
  222. wpabuf_free(req);
  223. goto fail;
  224. }
  225. return;
  226. fail:
  227. /*
  228. * Need to get rid of the AP entry even when we fail to unsubscribe
  229. * cleanly.
  230. */
  231. wps_er_ap_unsubscribed(ap->er, ap);
  232. }
  233. static struct wps_er_ap_settings * wps_er_ap_get_settings(struct wps_er *er,
  234. const u8 *uuid)
  235. {
  236. struct wps_er_ap_settings *s;
  237. dl_list_for_each(s, &er->ap_settings, struct wps_er_ap_settings, list)
  238. if (os_memcmp(uuid, s->uuid, WPS_UUID_LEN) == 0)
  239. return s;
  240. return NULL;
  241. }
  242. int wps_er_ap_cache_settings(struct wps_er *er, struct in_addr *addr)
  243. {
  244. struct wps_er_ap *ap;
  245. struct wps_er_ap_settings *settings;
  246. ap = wps_er_ap_get(er, addr, NULL);
  247. if (ap == NULL || ap->ap_settings == NULL)
  248. return -1;
  249. settings = wps_er_ap_get_settings(er, ap->uuid);
  250. if (!settings) {
  251. settings = os_zalloc(sizeof(*settings));
  252. if (settings == NULL)
  253. return -1;
  254. os_memcpy(settings->uuid, ap->uuid, WPS_UUID_LEN);
  255. dl_list_add(&er->ap_settings, &settings->list);
  256. }
  257. os_memcpy(&settings->ap_settings, ap->ap_settings,
  258. sizeof(struct wps_credential));
  259. return 0;
  260. }
  261. static int wps_er_ap_use_cached_settings(struct wps_er *er,
  262. struct wps_er_ap *ap)
  263. {
  264. struct wps_er_ap_settings *s;
  265. if (ap->ap_settings)
  266. return 0;
  267. s = wps_er_ap_get_settings(ap->er, ap->uuid);
  268. if (!s)
  269. return -1;
  270. ap->ap_settings = os_malloc(sizeof(*ap->ap_settings));
  271. if (ap->ap_settings == NULL)
  272. return -1;
  273. os_memcpy(ap->ap_settings, &s->ap_settings, sizeof(*ap->ap_settings));
  274. wpa_printf(MSG_DEBUG, "WPS ER: Use cached AP settings");
  275. return 0;
  276. }
  277. static void wps_er_ap_remove_entry(struct wps_er *er, struct wps_er_ap *ap)
  278. {
  279. wpa_printf(MSG_DEBUG, "WPS ER: Removing AP entry for %s (%s)",
  280. inet_ntoa(ap->addr), ap->location);
  281. eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
  282. wps_er_sta_remove_all(ap);
  283. wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_REMOVE);
  284. http_client_free(ap->http);
  285. ap->http = NULL;
  286. if (ap->wps) {
  287. wps_deinit(ap->wps);
  288. ap->wps = NULL;
  289. }
  290. dl_list_del(&ap->list);
  291. if (ap->subscribed) {
  292. dl_list_add(&er->ap_unsubscribing, &ap->list);
  293. wps_er_ap_unsubscribe(er, ap);
  294. } else
  295. wps_er_ap_free(ap);
  296. }
  297. static void wps_er_ap_timeout(void *eloop_data, void *user_ctx)
  298. {
  299. struct wps_er *er = eloop_data;
  300. struct wps_er_ap *ap = user_ctx;
  301. wpa_printf(MSG_DEBUG, "WPS ER: AP advertisement timed out");
  302. wps_er_ap_remove_entry(er, ap);
  303. }
  304. static int wps_er_get_sid(struct wps_er_ap *ap, char *sid)
  305. {
  306. char *pos;
  307. char txt[100];
  308. if (!sid) {
  309. wpa_printf(MSG_DEBUG, "WPS ER: No SID received from %s (%s)",
  310. inet_ntoa(ap->addr), ap->location);
  311. return -1;
  312. }
  313. pos = os_strstr(sid, "uuid:");
  314. if (!pos) {
  315. wpa_printf(MSG_DEBUG, "WPS ER: Invalid SID received from "
  316. "%s (%s): '%s'", inet_ntoa(ap->addr), ap->location,
  317. sid);
  318. return -1;
  319. }
  320. pos += 5;
  321. if (uuid_str2bin(pos, ap->sid) < 0) {
  322. wpa_printf(MSG_DEBUG, "WPS ER: Invalid SID received from "
  323. "%s (%s): '%s'", inet_ntoa(ap->addr), ap->location,
  324. sid);
  325. return -1;
  326. }
  327. uuid_bin2str(ap->sid, txt, sizeof(txt));
  328. wpa_printf(MSG_DEBUG, "WPS ER: SID for subscription with %s (%s): %s",
  329. inet_ntoa(ap->addr), ap->location, txt);
  330. return 0;
  331. }
  332. static void wps_er_http_subscribe_cb(void *ctx, struct http_client *c,
  333. enum http_client_event event)
  334. {
  335. struct wps_er_ap *ap = ctx;
  336. switch (event) {
  337. case HTTP_CLIENT_OK:
  338. wpa_printf(MSG_DEBUG, "WPS ER: Subscribed to events");
  339. ap->subscribed = 1;
  340. wps_er_get_sid(ap, http_client_get_hdr_line(c, "SID"));
  341. wps_er_ap_use_cached_settings(ap->er, ap);
  342. wps_er_ap_event(ap->er->wps, ap, WPS_EV_ER_AP_ADD);
  343. break;
  344. case HTTP_CLIENT_FAILED:
  345. case HTTP_CLIENT_INVALID_REPLY:
  346. case HTTP_CLIENT_TIMEOUT:
  347. wpa_printf(MSG_DEBUG, "WPS ER: Failed to subscribe to events");
  348. break;
  349. }
  350. http_client_free(ap->http);
  351. ap->http = NULL;
  352. }
  353. static void wps_er_subscribe(struct wps_er_ap *ap)
  354. {
  355. struct wpabuf *req;
  356. struct sockaddr_in dst;
  357. char *url, *path;
  358. if (ap->event_sub_url == NULL) {
  359. wpa_printf(MSG_DEBUG, "WPS ER: No eventSubURL - cannot "
  360. "subscribe");
  361. return;
  362. }
  363. if (ap->http) {
  364. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request - cannot "
  365. "send subscribe request");
  366. return;
  367. }
  368. url = http_client_url_parse(ap->event_sub_url, &dst, &path);
  369. if (url == NULL) {
  370. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse eventSubURL");
  371. return;
  372. }
  373. req = wpabuf_alloc(os_strlen(ap->event_sub_url) + 1000);
  374. if (req == NULL) {
  375. os_free(url);
  376. return;
  377. }
  378. wpabuf_printf(req,
  379. "SUBSCRIBE %s HTTP/1.1\r\n"
  380. "HOST: %s:%d\r\n"
  381. "CALLBACK: <http://%s:%d/event/%u/%u>\r\n"
  382. "NT: upnp:event\r\n"
  383. "TIMEOUT: Second-%d\r\n"
  384. "\r\n",
  385. path, inet_ntoa(dst.sin_addr), ntohs(dst.sin_port),
  386. ap->er->ip_addr_text, ap->er->http_port,
  387. ap->er->event_id, ap->id, 1800);
  388. os_free(url);
  389. wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Subscription request",
  390. wpabuf_head(req), wpabuf_len(req));
  391. ap->http = http_client_addr(&dst, req, 1000, wps_er_http_subscribe_cb,
  392. ap);
  393. if (ap->http == NULL)
  394. wpabuf_free(req);
  395. }
  396. static void wps_er_ap_get_m1(struct wps_er_ap *ap, struct wpabuf *m1)
  397. {
  398. struct wps_parse_attr attr;
  399. if (wps_parse_msg(m1, &attr) < 0) {
  400. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse M1");
  401. return;
  402. }
  403. if (attr.primary_dev_type)
  404. os_memcpy(ap->pri_dev_type, attr.primary_dev_type, 8);
  405. if (attr.wps_state)
  406. ap->wps_state = *attr.wps_state;
  407. if (attr.mac_addr)
  408. os_memcpy(ap->mac_addr, attr.mac_addr, ETH_ALEN);
  409. wps_er_subscribe(ap);
  410. }
  411. static void wps_er_get_device_info(struct wps_er_ap *ap)
  412. {
  413. wps_er_send_get_device_info(ap, wps_er_ap_get_m1);
  414. }
  415. static const char * wps_er_find_wfadevice(const char *data)
  416. {
  417. const char *tag, *tagname, *end;
  418. char *val;
  419. int found = 0;
  420. while (!found) {
  421. /* Find next <device> */
  422. for (;;) {
  423. if (xml_next_tag(data, &tag, &tagname, &end))
  424. return NULL;
  425. data = end;
  426. if (!os_strncasecmp(tagname, "device", 6) &&
  427. *tag != '/' &&
  428. (tagname[6] == '>' || !isgraph(tagname[6]))) {
  429. break;
  430. }
  431. }
  432. /* Check whether deviceType is WFADevice */
  433. val = xml_get_first_item(data, "deviceType");
  434. if (val == NULL)
  435. return NULL;
  436. wpa_printf(MSG_DEBUG, "WPS ER: Found deviceType '%s'", val);
  437. found = os_strcasecmp(val, "urn:schemas-wifialliance-org:"
  438. "device:WFADevice:1") == 0;
  439. os_free(val);
  440. }
  441. return data;
  442. }
  443. static void wps_er_parse_device_description(struct wps_er_ap *ap,
  444. struct wpabuf *reply)
  445. {
  446. /* Note: reply includes null termination after the buffer data */
  447. const char *tmp, *data = wpabuf_head(reply);
  448. char *pos;
  449. wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Device info",
  450. wpabuf_head(reply), wpabuf_len(reply));
  451. /*
  452. * The root device description may include multiple devices, so first
  453. * find the beginning of the WFADevice description to allow the
  454. * simplistic parser to pick the correct entries.
  455. */
  456. tmp = wps_er_find_wfadevice(data);
  457. if (tmp == NULL) {
  458. wpa_printf(MSG_DEBUG, "WPS ER: WFADevice:1 device not found - "
  459. "trying to parse invalid data");
  460. } else
  461. data = tmp;
  462. ap->friendly_name = xml_get_first_item(data, "friendlyName");
  463. wpa_printf(MSG_DEBUG, "WPS ER: friendlyName='%s'", ap->friendly_name);
  464. ap->manufacturer = xml_get_first_item(data, "manufacturer");
  465. wpa_printf(MSG_DEBUG, "WPS ER: manufacturer='%s'", ap->manufacturer);
  466. ap->manufacturer_url = xml_get_first_item(data, "manufacturerURL");
  467. wpa_printf(MSG_DEBUG, "WPS ER: manufacturerURL='%s'",
  468. ap->manufacturer_url);
  469. ap->model_description = xml_get_first_item(data, "modelDescription");
  470. wpa_printf(MSG_DEBUG, "WPS ER: modelDescription='%s'",
  471. ap->model_description);
  472. ap->model_name = xml_get_first_item(data, "modelName");
  473. wpa_printf(MSG_DEBUG, "WPS ER: modelName='%s'", ap->model_name);
  474. ap->model_number = xml_get_first_item(data, "modelNumber");
  475. wpa_printf(MSG_DEBUG, "WPS ER: modelNumber='%s'", ap->model_number);
  476. ap->model_url = xml_get_first_item(data, "modelURL");
  477. wpa_printf(MSG_DEBUG, "WPS ER: modelURL='%s'", ap->model_url);
  478. ap->serial_number = xml_get_first_item(data, "serialNumber");
  479. wpa_printf(MSG_DEBUG, "WPS ER: serialNumber='%s'", ap->serial_number);
  480. ap->udn = xml_get_first_item(data, "UDN");
  481. wpa_printf(MSG_DEBUG, "WPS ER: UDN='%s'", ap->udn);
  482. pos = os_strstr(ap->udn, "uuid:");
  483. if (pos) {
  484. pos += 5;
  485. if (uuid_str2bin(pos, ap->uuid) < 0)
  486. wpa_printf(MSG_DEBUG, "WPS ER: Invalid UUID in UDN");
  487. }
  488. ap->upc = xml_get_first_item(data, "UPC");
  489. wpa_printf(MSG_DEBUG, "WPS ER: UPC='%s'", ap->upc);
  490. ap->scpd_url = http_link_update(
  491. xml_get_first_item(data, "SCPDURL"), ap->location);
  492. wpa_printf(MSG_DEBUG, "WPS ER: SCPDURL='%s'", ap->scpd_url);
  493. ap->control_url = http_link_update(
  494. xml_get_first_item(data, "controlURL"), ap->location);
  495. wpa_printf(MSG_DEBUG, "WPS ER: controlURL='%s'", ap->control_url);
  496. ap->event_sub_url = http_link_update(
  497. xml_get_first_item(data, "eventSubURL"), ap->location);
  498. wpa_printf(MSG_DEBUG, "WPS ER: eventSubURL='%s'", ap->event_sub_url);
  499. }
  500. static void wps_er_http_dev_desc_cb(void *ctx, struct http_client *c,
  501. enum http_client_event event)
  502. {
  503. struct wps_er_ap *ap = ctx;
  504. struct wpabuf *reply;
  505. int ok = 0;
  506. switch (event) {
  507. case HTTP_CLIENT_OK:
  508. reply = http_client_get_body(c);
  509. if (reply == NULL)
  510. break;
  511. wps_er_parse_device_description(ap, reply);
  512. ok = 1;
  513. break;
  514. case HTTP_CLIENT_FAILED:
  515. case HTTP_CLIENT_INVALID_REPLY:
  516. case HTTP_CLIENT_TIMEOUT:
  517. wpa_printf(MSG_DEBUG, "WPS ER: Failed to fetch device info");
  518. break;
  519. }
  520. http_client_free(ap->http);
  521. ap->http = NULL;
  522. if (ok)
  523. wps_er_get_device_info(ap);
  524. }
  525. void wps_er_ap_add(struct wps_er *er, const u8 *uuid, struct in_addr *addr,
  526. const char *location, int max_age)
  527. {
  528. struct wps_er_ap *ap;
  529. ap = wps_er_ap_get(er, addr, uuid);
  530. if (ap) {
  531. /* Update advertisement timeout */
  532. eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
  533. eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
  534. return;
  535. }
  536. ap = os_zalloc(sizeof(*ap));
  537. if (ap == NULL)
  538. return;
  539. dl_list_init(&ap->sta);
  540. ap->er = er;
  541. ap->id = ++er->next_ap_id;
  542. ap->location = os_strdup(location);
  543. if (ap->location == NULL) {
  544. os_free(ap);
  545. return;
  546. }
  547. dl_list_add(&er->ap, &ap->list);
  548. ap->addr.s_addr = addr->s_addr;
  549. os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
  550. eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
  551. wpa_printf(MSG_DEBUG, "WPS ER: Added AP entry for %s (%s)",
  552. inet_ntoa(ap->addr), ap->location);
  553. /* Fetch device description */
  554. ap->http = http_client_url(ap->location, NULL, 10000,
  555. wps_er_http_dev_desc_cb, ap);
  556. }
  557. void wps_er_ap_remove(struct wps_er *er, struct in_addr *addr)
  558. {
  559. struct wps_er_ap *ap;
  560. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  561. if (ap->addr.s_addr == addr->s_addr) {
  562. wps_er_ap_remove_entry(er, ap);
  563. return;
  564. }
  565. }
  566. }
  567. static void wps_er_ap_remove_all(struct wps_er *er)
  568. {
  569. struct wps_er_ap *prev, *ap;
  570. struct wps_er_ap_settings *prev_s, *s;
  571. dl_list_for_each_safe(ap, prev, &er->ap, struct wps_er_ap, list)
  572. wps_er_ap_remove_entry(er, ap);
  573. dl_list_for_each_safe(s, prev_s, &er->ap_settings,
  574. struct wps_er_ap_settings, list)
  575. os_free(s);
  576. }
  577. static void http_put_date(struct wpabuf *buf)
  578. {
  579. wpabuf_put_str(buf, "Date: ");
  580. format_date(buf);
  581. wpabuf_put_str(buf, "\r\n");
  582. }
  583. static void wps_er_http_resp_not_found(struct http_request *req)
  584. {
  585. struct wpabuf *buf;
  586. buf = wpabuf_alloc(200);
  587. if (buf == NULL) {
  588. http_request_deinit(req);
  589. return;
  590. }
  591. wpabuf_put_str(buf,
  592. "HTTP/1.1 404 Not Found\r\n"
  593. "Server: unspecified, UPnP/1.0, unspecified\r\n"
  594. "Connection: close\r\n");
  595. http_put_date(buf);
  596. wpabuf_put_str(buf, "\r\n");
  597. http_request_send_and_deinit(req, buf);
  598. }
  599. static void wps_er_http_resp_ok(struct http_request *req)
  600. {
  601. struct wpabuf *buf;
  602. buf = wpabuf_alloc(200);
  603. if (buf == NULL) {
  604. http_request_deinit(req);
  605. return;
  606. }
  607. wpabuf_put_str(buf,
  608. "HTTP/1.1 200 OK\r\n"
  609. "Server: unspecified, UPnP/1.0, unspecified\r\n"
  610. "Connection: close\r\n"
  611. "Content-Length: 0\r\n");
  612. http_put_date(buf);
  613. wpabuf_put_str(buf, "\r\n");
  614. http_request_send_and_deinit(req, buf);
  615. }
  616. static void wps_er_sta_timeout(void *eloop_data, void *user_ctx)
  617. {
  618. struct wps_er_sta *sta = eloop_data;
  619. wpa_printf(MSG_DEBUG, "WPS ER: STA entry timed out");
  620. dl_list_del(&sta->list);
  621. wps_er_sta_free(sta);
  622. }
  623. static struct wps_er_sta * wps_er_add_sta_data(struct wps_er_ap *ap,
  624. const u8 *addr,
  625. struct wps_parse_attr *attr,
  626. int probe_req)
  627. {
  628. struct wps_er_sta *sta = wps_er_sta_get(ap, addr, NULL);
  629. int new_sta = 0;
  630. int m1;
  631. m1 = !probe_req && attr->msg_type && *attr->msg_type == WPS_M1;
  632. if (sta == NULL) {
  633. /*
  634. * Only allow new STA entry to be added based on Probe Request
  635. * or M1. This will filter out bogus events and anything that
  636. * may have been ongoing at the time ER subscribed for events.
  637. */
  638. if (!probe_req && !m1)
  639. return NULL;
  640. sta = os_zalloc(sizeof(*sta));
  641. if (sta == NULL)
  642. return NULL;
  643. os_memcpy(sta->addr, addr, ETH_ALEN);
  644. sta->ap = ap;
  645. dl_list_add(&ap->sta, &sta->list);
  646. new_sta = 1;
  647. }
  648. if (m1)
  649. sta->m1_received = 1;
  650. if (attr->config_methods && (!probe_req || !sta->m1_received))
  651. sta->config_methods = WPA_GET_BE16(attr->config_methods);
  652. if (attr->uuid_e && (!probe_req || !sta->m1_received))
  653. os_memcpy(sta->uuid, attr->uuid_e, WPS_UUID_LEN);
  654. if (attr->primary_dev_type && (!probe_req || !sta->m1_received))
  655. os_memcpy(sta->pri_dev_type, attr->primary_dev_type, 8);
  656. if (attr->dev_password_id && (!probe_req || !sta->m1_received))
  657. sta->dev_passwd_id = WPA_GET_BE16(attr->dev_password_id);
  658. if (attr->manufacturer) {
  659. os_free(sta->manufacturer);
  660. sta->manufacturer = os_malloc(attr->manufacturer_len + 1);
  661. if (sta->manufacturer) {
  662. os_memcpy(sta->manufacturer, attr->manufacturer,
  663. attr->manufacturer_len);
  664. sta->manufacturer[attr->manufacturer_len] = '\0';
  665. }
  666. }
  667. if (attr->model_name) {
  668. os_free(sta->model_name);
  669. sta->model_name = os_malloc(attr->model_name_len + 1);
  670. if (sta->model_name) {
  671. os_memcpy(sta->model_name, attr->model_name,
  672. attr->model_name_len);
  673. sta->model_name[attr->model_name_len] = '\0';
  674. }
  675. }
  676. if (attr->model_number) {
  677. os_free(sta->model_number);
  678. sta->model_number = os_malloc(attr->model_number_len + 1);
  679. if (sta->model_number) {
  680. os_memcpy(sta->model_number, attr->model_number,
  681. attr->model_number_len);
  682. sta->model_number[attr->model_number_len] = '\0';
  683. }
  684. }
  685. if (attr->serial_number) {
  686. os_free(sta->serial_number);
  687. sta->serial_number = os_malloc(attr->serial_number_len + 1);
  688. if (sta->serial_number) {
  689. os_memcpy(sta->serial_number, attr->serial_number,
  690. attr->serial_number_len);
  691. sta->serial_number[attr->serial_number_len] = '\0';
  692. }
  693. }
  694. if (attr->dev_name) {
  695. os_free(sta->dev_name);
  696. sta->dev_name = os_malloc(attr->dev_name_len + 1);
  697. if (sta->dev_name) {
  698. os_memcpy(sta->dev_name, attr->dev_name,
  699. attr->dev_name_len);
  700. sta->dev_name[attr->dev_name_len] = '\0';
  701. }
  702. }
  703. eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
  704. eloop_register_timeout(300, 0, wps_er_sta_timeout, sta, NULL);
  705. if (m1 || new_sta)
  706. wps_er_sta_event(ap->er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
  707. return sta;
  708. }
  709. static void wps_er_process_wlanevent_probe_req(struct wps_er_ap *ap,
  710. const u8 *addr,
  711. struct wpabuf *msg)
  712. {
  713. struct wps_parse_attr attr;
  714. wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - Probe Request - from "
  715. MACSTR, MAC2STR(addr));
  716. wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
  717. "(TLVs from Probe Request)", msg);
  718. if (wps_validate_probe_req(msg, addr) < 0) {
  719. wpa_printf(MSG_INFO, "WPS-STRICT: ER: Ignore invalid proxied "
  720. "Probe Request frame from " MACSTR, MAC2STR(addr));
  721. return;
  722. }
  723. if (wps_parse_msg(msg, &attr) < 0) {
  724. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
  725. "WLANEvent message");
  726. return;
  727. }
  728. wps_er_add_sta_data(ap, addr, &attr, 1);
  729. wps_registrar_probe_req_rx(ap->er->wps->registrar, addr, msg, 0);
  730. }
  731. static void wps_er_http_put_wlan_response_cb(void *ctx, struct http_client *c,
  732. enum http_client_event event)
  733. {
  734. struct wps_er_sta *sta = ctx;
  735. switch (event) {
  736. case HTTP_CLIENT_OK:
  737. wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse OK");
  738. break;
  739. case HTTP_CLIENT_FAILED:
  740. case HTTP_CLIENT_INVALID_REPLY:
  741. case HTTP_CLIENT_TIMEOUT:
  742. wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse failed");
  743. break;
  744. }
  745. http_client_free(sta->http);
  746. sta->http = NULL;
  747. }
  748. static const char *soap_prefix =
  749. "<?xml version=\"1.0\"?>\n"
  750. "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  751. "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
  752. "<s:Body>\n";
  753. static const char *soap_postfix =
  754. "</s:Body>\n</s:Envelope>\n";
  755. static const char *urn_wfawlanconfig =
  756. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
  757. static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
  758. const char *name, const char *arg_name,
  759. const char *path,
  760. const struct sockaddr_in *dst,
  761. char **len_ptr, char **body_ptr)
  762. {
  763. unsigned char *encoded;
  764. size_t encoded_len;
  765. struct wpabuf *buf;
  766. if (msg) {
  767. encoded = base64_encode(wpabuf_head(msg), wpabuf_len(msg),
  768. &encoded_len);
  769. if (encoded == NULL)
  770. return NULL;
  771. } else {
  772. encoded = NULL;
  773. encoded_len = 0;
  774. }
  775. buf = wpabuf_alloc(1000 + encoded_len);
  776. if (buf == NULL) {
  777. os_free(encoded);
  778. return NULL;
  779. }
  780. wpabuf_printf(buf,
  781. "POST %s HTTP/1.1\r\n"
  782. "Host: %s:%d\r\n"
  783. "Content-Type: text/xml; charset=\"utf-8\"\r\n"
  784. "Content-Length: ",
  785. path, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
  786. *len_ptr = wpabuf_put(buf, 0);
  787. wpabuf_printf(buf,
  788. " \r\n"
  789. "SOAPACTION: \"%s#%s\"\r\n"
  790. "\r\n",
  791. urn_wfawlanconfig, name);
  792. *body_ptr = wpabuf_put(buf, 0);
  793. wpabuf_put_str(buf, soap_prefix);
  794. wpabuf_printf(buf, "<u:%s xmlns:u=\"", name);
  795. wpabuf_put_str(buf, urn_wfawlanconfig);
  796. wpabuf_put_str(buf, "\">\n");
  797. if (encoded) {
  798. wpabuf_printf(buf, "<%s>%s</%s>\n",
  799. arg_name, (char *) encoded, arg_name);
  800. os_free(encoded);
  801. }
  802. return buf;
  803. }
  804. static void wps_er_soap_end(struct wpabuf *buf, const char *name,
  805. char *len_ptr, char *body_ptr)
  806. {
  807. char len_buf[10];
  808. wpabuf_printf(buf, "</u:%s>\n", name);
  809. wpabuf_put_str(buf, soap_postfix);
  810. os_snprintf(len_buf, sizeof(len_buf), "%d",
  811. (int) ((char *) wpabuf_put(buf, 0) - body_ptr));
  812. os_memcpy(len_ptr, len_buf, os_strlen(len_buf));
  813. }
  814. static void wps_er_sta_send_msg(struct wps_er_sta *sta, struct wpabuf *msg)
  815. {
  816. struct wpabuf *buf;
  817. char *len_ptr, *body_ptr;
  818. struct sockaddr_in dst;
  819. char *url, *path;
  820. if (sta->http) {
  821. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for STA - "
  822. "ignore new request");
  823. wpabuf_free(msg);
  824. return;
  825. }
  826. if (sta->ap->control_url == NULL) {
  827. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  828. wpabuf_free(msg);
  829. return;
  830. }
  831. url = http_client_url_parse(sta->ap->control_url, &dst, &path);
  832. if (url == NULL) {
  833. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  834. wpabuf_free(msg);
  835. return;
  836. }
  837. buf = wps_er_soap_hdr(msg, "PutWLANResponse", "NewMessage", path, &dst,
  838. &len_ptr, &body_ptr);
  839. wpabuf_free(msg);
  840. os_free(url);
  841. if (buf == NULL)
  842. return;
  843. wpabuf_printf(buf, "<NewWLANEventType>%d</NewWLANEventType>\n",
  844. UPNP_WPS_WLANEVENT_TYPE_EAP);
  845. wpabuf_printf(buf, "<NewWLANEventMAC>" MACSTR "</NewWLANEventMAC>\n",
  846. MAC2STR(sta->addr));
  847. wps_er_soap_end(buf, "PutWLANResponse", len_ptr, body_ptr);
  848. sta->http = http_client_addr(&dst, buf, 1000,
  849. wps_er_http_put_wlan_response_cb, sta);
  850. if (sta->http == NULL)
  851. wpabuf_free(buf);
  852. }
  853. static void wps_er_sta_process(struct wps_er_sta *sta, struct wpabuf *msg,
  854. enum wsc_op_code op_code)
  855. {
  856. enum wps_process_res res;
  857. res = wps_process_msg(sta->wps, op_code, msg);
  858. if (res == WPS_CONTINUE) {
  859. struct wpabuf *next = wps_get_msg(sta->wps, &op_code);
  860. if (next)
  861. wps_er_sta_send_msg(sta, next);
  862. } else {
  863. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run %s with the "
  864. "enrollee (res=%d)",
  865. res == WPS_DONE ? "succeeded" : "failed", res);
  866. wps_deinit(sta->wps);
  867. sta->wps = NULL;
  868. if (res == WPS_DONE) {
  869. /* Remove the STA entry after short timeout */
  870. eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
  871. eloop_register_timeout(10, 0, wps_er_sta_timeout, sta,
  872. NULL);
  873. }
  874. }
  875. }
  876. static void wps_er_sta_start(struct wps_er_sta *sta, struct wpabuf *msg)
  877. {
  878. struct wps_config cfg;
  879. if (sta->wps)
  880. wps_deinit(sta->wps);
  881. os_memset(&cfg, 0, sizeof(cfg));
  882. cfg.wps = sta->ap->er->wps;
  883. cfg.registrar = 1;
  884. cfg.peer_addr = sta->addr;
  885. sta->wps = wps_init(&cfg);
  886. if (sta->wps == NULL)
  887. return;
  888. sta->wps->er = 1;
  889. sta->wps->use_cred = sta->ap->ap_settings;
  890. if (sta->ap->ap_settings) {
  891. os_free(sta->cred);
  892. sta->cred = os_malloc(sizeof(*sta->cred));
  893. if (sta->cred) {
  894. os_memcpy(sta->cred, sta->ap->ap_settings,
  895. sizeof(*sta->cred));
  896. sta->cred->cred_attr = NULL;
  897. os_memcpy(sta->cred->mac_addr, sta->addr, ETH_ALEN);
  898. sta->wps->use_cred = sta->cred;
  899. }
  900. }
  901. wps_er_sta_process(sta, msg, WSC_MSG);
  902. }
  903. static void wps_er_process_wlanevent_eap(struct wps_er_ap *ap, const u8 *addr,
  904. struct wpabuf *msg)
  905. {
  906. struct wps_parse_attr attr;
  907. struct wps_er_sta *sta;
  908. wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - EAP - from " MACSTR,
  909. MAC2STR(addr));
  910. wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
  911. "(TLVs from EAP-WSC)", msg);
  912. if (wps_parse_msg(msg, &attr) < 0) {
  913. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
  914. "WLANEvent message");
  915. return;
  916. }
  917. sta = wps_er_add_sta_data(ap, addr, &attr, 0);
  918. if (sta == NULL)
  919. return;
  920. if (attr.msg_type && *attr.msg_type == WPS_M1)
  921. wps_er_sta_start(sta, msg);
  922. else if (sta->wps) {
  923. enum wsc_op_code op_code = WSC_MSG;
  924. if (attr.msg_type) {
  925. switch (*attr.msg_type) {
  926. case WPS_WSC_ACK:
  927. op_code = WSC_ACK;
  928. break;
  929. case WPS_WSC_NACK:
  930. op_code = WSC_NACK;
  931. break;
  932. case WPS_WSC_DONE:
  933. op_code = WSC_Done;
  934. break;
  935. }
  936. }
  937. wps_er_sta_process(sta, msg, op_code);
  938. }
  939. }
  940. static void wps_er_process_wlanevent(struct wps_er_ap *ap,
  941. struct wpabuf *event)
  942. {
  943. u8 *data;
  944. u8 wlan_event_type;
  945. u8 wlan_event_mac[ETH_ALEN];
  946. struct wpabuf msg;
  947. wpa_hexdump(MSG_MSGDUMP, "WPS ER: Received WLANEvent",
  948. wpabuf_head(event), wpabuf_len(event));
  949. if (wpabuf_len(event) < 1 + 17) {
  950. wpa_printf(MSG_DEBUG, "WPS ER: Too short WLANEvent");
  951. return;
  952. }
  953. data = wpabuf_mhead(event);
  954. wlan_event_type = data[0];
  955. if (hwaddr_aton((char *) data + 1, wlan_event_mac) < 0) {
  956. wpa_printf(MSG_DEBUG, "WPS ER: Invalid WLANEventMAC in "
  957. "WLANEvent");
  958. return;
  959. }
  960. wpabuf_set(&msg, data + 1 + 17, wpabuf_len(event) - (1 + 17));
  961. switch (wlan_event_type) {
  962. case 1:
  963. wps_er_process_wlanevent_probe_req(ap, wlan_event_mac, &msg);
  964. break;
  965. case 2:
  966. wps_er_process_wlanevent_eap(ap, wlan_event_mac, &msg);
  967. break;
  968. default:
  969. wpa_printf(MSG_DEBUG, "WPS ER: Unknown WLANEventType %d",
  970. wlan_event_type);
  971. break;
  972. }
  973. }
  974. static void wps_er_http_event(struct wps_er *er, struct http_request *req,
  975. unsigned int ap_id)
  976. {
  977. struct wps_er_ap *ap = wps_er_ap_get_id(er, ap_id);
  978. struct wpabuf *event;
  979. enum http_reply_code ret;
  980. if (ap == NULL) {
  981. wpa_printf(MSG_DEBUG, "WPS ER: HTTP event from unknown AP id "
  982. "%u", ap_id);
  983. wps_er_http_resp_not_found(req);
  984. return;
  985. }
  986. wpa_printf(MSG_MSGDUMP, "WPS ER: HTTP event from AP id %u: %s",
  987. ap_id, http_request_get_data(req));
  988. event = xml_get_base64_item(http_request_get_data(req), "WLANEvent",
  989. &ret);
  990. if (event == NULL) {
  991. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract WLANEvent "
  992. "from the event notification");
  993. /*
  994. * Reply with OK anyway to avoid getting unregistered from
  995. * events.
  996. */
  997. wps_er_http_resp_ok(req);
  998. return;
  999. }
  1000. wps_er_process_wlanevent(ap, event);
  1001. wpabuf_free(event);
  1002. wps_er_http_resp_ok(req);
  1003. }
  1004. static void wps_er_http_notify(struct wps_er *er, struct http_request *req)
  1005. {
  1006. char *uri = http_request_get_uri(req);
  1007. if (os_strncmp(uri, "/event/", 7) == 0) {
  1008. unsigned int event_id;
  1009. char *pos;
  1010. event_id = atoi(uri + 7);
  1011. if (event_id != er->event_id) {
  1012. wpa_printf(MSG_DEBUG, "WPS ER: HTTP event for an "
  1013. "unknown event id %u", event_id);
  1014. return;
  1015. }
  1016. pos = os_strchr(uri + 7, '/');
  1017. if (pos == NULL)
  1018. return;
  1019. pos++;
  1020. wps_er_http_event(er, req, atoi(pos));
  1021. } else {
  1022. wpa_printf(MSG_DEBUG, "WPS ER: Unknown HTTP NOTIFY for '%s'",
  1023. uri);
  1024. wps_er_http_resp_not_found(req);
  1025. }
  1026. }
  1027. static void wps_er_http_req(void *ctx, struct http_request *req)
  1028. {
  1029. struct wps_er *er = ctx;
  1030. struct sockaddr_in *cli = http_request_get_cli_addr(req);
  1031. enum httpread_hdr_type type = http_request_get_type(req);
  1032. struct wpabuf *buf;
  1033. wpa_printf(MSG_DEBUG, "WPS ER: HTTP request: '%s' (type %d) from "
  1034. "%s:%d",
  1035. http_request_get_uri(req), type,
  1036. inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));
  1037. switch (type) {
  1038. case HTTPREAD_HDR_TYPE_NOTIFY:
  1039. wps_er_http_notify(er, req);
  1040. break;
  1041. default:
  1042. wpa_printf(MSG_DEBUG, "WPS ER: Unsupported HTTP request type "
  1043. "%d", type);
  1044. buf = wpabuf_alloc(200);
  1045. if (buf == NULL) {
  1046. http_request_deinit(req);
  1047. return;
  1048. }
  1049. wpabuf_put_str(buf,
  1050. "HTTP/1.1 501 Unimplemented\r\n"
  1051. "Connection: close\r\n");
  1052. http_put_date(buf);
  1053. wpabuf_put_str(buf, "\r\n");
  1054. http_request_send_and_deinit(req, buf);
  1055. break;
  1056. }
  1057. }
  1058. struct wps_er *
  1059. wps_er_init(struct wps_context *wps, const char *ifname, const char *filter)
  1060. {
  1061. struct wps_er *er;
  1062. struct in_addr addr;
  1063. er = os_zalloc(sizeof(*er));
  1064. if (er == NULL)
  1065. return NULL;
  1066. dl_list_init(&er->ap);
  1067. dl_list_init(&er->ap_unsubscribing);
  1068. dl_list_init(&er->ap_settings);
  1069. er->multicast_sd = -1;
  1070. er->ssdp_sd = -1;
  1071. os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
  1072. er->wps = wps;
  1073. if (os_get_random((unsigned char *) &er->event_id,
  1074. sizeof(er->event_id)) < 0) {
  1075. wps_er_deinit(er, NULL, NULL);
  1076. return NULL;
  1077. }
  1078. /* Limit event_id to < 32 bits to avoid issues with atoi() */
  1079. er->event_id &= 0x0fffffff;
  1080. if (filter) {
  1081. if (inet_aton(filter, &er->filter_addr) == 0) {
  1082. wpa_printf(MSG_INFO, "WPS UPnP: Invalid filter "
  1083. "address %s", filter);
  1084. wps_er_deinit(er, NULL, NULL);
  1085. return NULL;
  1086. }
  1087. wpa_printf(MSG_DEBUG, "WPS UPnP: Only accepting connections "
  1088. "with %s", filter);
  1089. }
  1090. if (get_netif_info(ifname, &er->ip_addr, &er->ip_addr_text,
  1091. er->mac_addr)) {
  1092. wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
  1093. "for %s. Does it have IP address?", ifname);
  1094. wps_er_deinit(er, NULL, NULL);
  1095. return NULL;
  1096. }
  1097. if (wps_er_ssdp_init(er) < 0) {
  1098. wpa_printf(MSG_INFO, "WPS UPnP: SSDP initialization failed");
  1099. wps_er_deinit(er, NULL, NULL);
  1100. return NULL;
  1101. }
  1102. addr.s_addr = er->ip_addr;
  1103. er->http_srv = http_server_init(&addr, -1, wps_er_http_req, er);
  1104. if (er->http_srv == NULL) {
  1105. wpa_printf(MSG_INFO, "WPS UPnP: HTTP initialization failed");
  1106. wps_er_deinit(er, NULL, NULL);
  1107. return NULL;
  1108. }
  1109. er->http_port = http_server_get_port(er->http_srv);
  1110. wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s)",
  1111. er->ifname, er->ip_addr_text);
  1112. return er;
  1113. }
  1114. void wps_er_refresh(struct wps_er *er)
  1115. {
  1116. struct wps_er_ap *ap;
  1117. struct wps_er_sta *sta;
  1118. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1119. wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_ADD);
  1120. dl_list_for_each(sta, &ap->sta, struct wps_er_sta, list)
  1121. wps_er_sta_event(er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
  1122. }
  1123. wps_er_send_ssdp_msearch(er);
  1124. }
  1125. static void wps_er_deinit_finish(void *eloop_data, void *user_ctx)
  1126. {
  1127. struct wps_er *er = eloop_data;
  1128. void (*deinit_done_cb)(void *ctx);
  1129. void *deinit_done_ctx;
  1130. wpa_printf(MSG_DEBUG, "WPS ER: Finishing deinit");
  1131. deinit_done_cb = er->deinit_done_cb;
  1132. deinit_done_ctx = er->deinit_done_ctx;
  1133. os_free(er->ip_addr_text);
  1134. os_free(er);
  1135. if (deinit_done_cb)
  1136. deinit_done_cb(deinit_done_ctx);
  1137. }
  1138. void wps_er_deinit(struct wps_er *er, void (*cb)(void *ctx), void *ctx)
  1139. {
  1140. if (er == NULL)
  1141. return;
  1142. http_server_deinit(er->http_srv);
  1143. wps_er_ap_remove_all(er);
  1144. wps_er_ssdp_deinit(er);
  1145. eloop_register_timeout(dl_list_empty(&er->ap_unsubscribing) ? 0 : 5, 0,
  1146. wps_er_deinit_finish, er, NULL);
  1147. wpa_printf(MSG_DEBUG, "WPS ER: Finish deinit from timeout");
  1148. er->deinitializing = 1;
  1149. er->deinit_done_cb = cb;
  1150. er->deinit_done_ctx = ctx;
  1151. }
  1152. static void wps_er_http_set_sel_reg_cb(void *ctx, struct http_client *c,
  1153. enum http_client_event event)
  1154. {
  1155. struct wps_er_ap *ap = ctx;
  1156. union wps_event_data data;
  1157. os_memset(&data, 0, sizeof(data));
  1158. switch (event) {
  1159. case HTTP_CLIENT_OK:
  1160. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar OK");
  1161. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_DONE;
  1162. data.set_sel_reg.uuid = ap->uuid;
  1163. break;
  1164. case HTTP_CLIENT_FAILED:
  1165. case HTTP_CLIENT_INVALID_REPLY:
  1166. case HTTP_CLIENT_TIMEOUT:
  1167. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar failed");
  1168. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_FAILED;
  1169. data.set_sel_reg.uuid = ap->uuid;
  1170. break;
  1171. }
  1172. http_client_free(ap->http);
  1173. ap->http = NULL;
  1174. if (data.set_sel_reg.uuid)
  1175. ap->er->wps->event_cb(ap->er->wps->cb_ctx,
  1176. WPS_EV_ER_SET_SELECTED_REGISTRAR, &data);
  1177. }
  1178. static void wps_er_send_set_sel_reg(struct wps_er_ap *ap, struct wpabuf *msg)
  1179. {
  1180. struct wpabuf *buf;
  1181. char *len_ptr, *body_ptr;
  1182. struct sockaddr_in dst;
  1183. char *url, *path;
  1184. if (ap->control_url == NULL) {
  1185. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1186. return;
  1187. }
  1188. if (ap->http) {
  1189. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for AP - "
  1190. "ignore new request");
  1191. return;
  1192. }
  1193. if (ap->wps) {
  1194. wpa_printf(MSG_DEBUG, "WPS ER: Pending WPS operation for AP - "
  1195. "skip SetSelectedRegistrar");
  1196. return;
  1197. }
  1198. url = http_client_url_parse(ap->control_url, &dst, &path);
  1199. if (url == NULL) {
  1200. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1201. return;
  1202. }
  1203. buf = wps_er_soap_hdr(msg, "SetSelectedRegistrar", "NewMessage", path,
  1204. &dst, &len_ptr, &body_ptr);
  1205. os_free(url);
  1206. if (buf == NULL)
  1207. return;
  1208. wps_er_soap_end(buf, "SetSelectedRegistrar", len_ptr, body_ptr);
  1209. ap->http = http_client_addr(&dst, buf, 1000,
  1210. wps_er_http_set_sel_reg_cb, ap);
  1211. if (ap->http == NULL)
  1212. wpabuf_free(buf);
  1213. }
  1214. static int wps_er_build_selected_registrar(struct wpabuf *msg, int sel_reg)
  1215. {
  1216. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
  1217. wpabuf_put_be16(msg, 1);
  1218. wpabuf_put_u8(msg, !!sel_reg);
  1219. return 0;
  1220. }
  1221. static int wps_er_build_dev_password_id(struct wpabuf *msg, u16 dev_passwd_id)
  1222. {
  1223. wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
  1224. wpabuf_put_be16(msg, 2);
  1225. wpabuf_put_be16(msg, dev_passwd_id);
  1226. return 0;
  1227. }
  1228. static int wps_er_build_sel_reg_config_methods(struct wpabuf *msg,
  1229. u16 sel_reg_config_methods)
  1230. {
  1231. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
  1232. wpabuf_put_be16(msg, 2);
  1233. wpabuf_put_be16(msg, sel_reg_config_methods);
  1234. return 0;
  1235. }
  1236. static int wps_er_build_uuid_r(struct wpabuf *msg, const u8 *uuid_r)
  1237. {
  1238. #ifdef CONFIG_WPS2
  1239. wpabuf_put_be16(msg, ATTR_UUID_R);
  1240. wpabuf_put_be16(msg, WPS_UUID_LEN);
  1241. wpabuf_put_data(msg, uuid_r, WPS_UUID_LEN);
  1242. #endif /* CONFIG_WPS2 */
  1243. return 0;
  1244. }
  1245. void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
  1246. u16 sel_reg_config_methods)
  1247. {
  1248. struct wpabuf *msg;
  1249. struct wps_er_ap *ap;
  1250. struct wps_registrar *reg = er->wps->registrar;
  1251. const u8 *auth_macs;
  1252. #ifdef CONFIG_WPS2
  1253. u8 bcast[ETH_ALEN];
  1254. #endif /* CONFIG_WPS2 */
  1255. size_t count;
  1256. union wps_event_data data;
  1257. if (er->skip_set_sel_reg) {
  1258. wpa_printf(MSG_DEBUG, "WPS ER: Skip SetSelectedRegistrar");
  1259. return;
  1260. }
  1261. msg = wpabuf_alloc(500);
  1262. if (msg == NULL)
  1263. return;
  1264. auth_macs = wps_authorized_macs(reg, &count);
  1265. #ifdef CONFIG_WPS2
  1266. if (count == 0) {
  1267. os_memset(bcast, 0xff, ETH_ALEN);
  1268. auth_macs = bcast;
  1269. count = 1;
  1270. }
  1271. #endif /* CONFIG_WPS2 */
  1272. if (wps_build_version(msg) ||
  1273. wps_er_build_selected_registrar(msg, sel_reg) ||
  1274. wps_er_build_dev_password_id(msg, dev_passwd_id) ||
  1275. wps_er_build_sel_reg_config_methods(msg, sel_reg_config_methods) ||
  1276. wps_build_wfa_ext(msg, 0, auth_macs, count) ||
  1277. wps_er_build_uuid_r(msg, er->wps->uuid)) {
  1278. wpabuf_free(msg);
  1279. return;
  1280. }
  1281. os_memset(&data, 0, sizeof(data));
  1282. data.set_sel_reg.sel_reg = sel_reg;
  1283. data.set_sel_reg.dev_passwd_id = dev_passwd_id;
  1284. data.set_sel_reg.sel_reg_config_methods = sel_reg_config_methods;
  1285. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_START;
  1286. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1287. if (er->set_sel_reg_uuid_filter &&
  1288. os_memcmp(ap->uuid, er->set_sel_reg_uuid_filter,
  1289. WPS_UUID_LEN) != 0)
  1290. continue;
  1291. data.set_sel_reg.uuid = ap->uuid;
  1292. er->wps->event_cb(er->wps->cb_ctx,
  1293. WPS_EV_ER_SET_SELECTED_REGISTRAR, &data);
  1294. wps_er_send_set_sel_reg(ap, msg);
  1295. }
  1296. wpabuf_free(msg);
  1297. }
  1298. int wps_er_pbc(struct wps_er *er, const u8 *uuid)
  1299. {
  1300. int res;
  1301. struct wps_er_ap *ap;
  1302. if (er == NULL || er->wps == NULL)
  1303. return -1;
  1304. if (wps_registrar_pbc_overlap(er->wps->registrar, NULL, NULL)) {
  1305. wpa_printf(MSG_DEBUG, "WPS ER: PBC overlap - do not start PBC "
  1306. "mode");
  1307. return -2;
  1308. }
  1309. ap = wps_er_ap_get(er, NULL, uuid);
  1310. if (ap == NULL) {
  1311. struct wps_er_sta *sta = NULL;
  1312. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1313. sta = wps_er_sta_get(ap, NULL, uuid);
  1314. if (sta) {
  1315. uuid = ap->uuid;
  1316. break;
  1317. }
  1318. }
  1319. if (sta == NULL)
  1320. return -3; /* Unknown UUID */
  1321. }
  1322. if (ap->ap_settings == NULL) {
  1323. wpa_printf(MSG_DEBUG, "WPS ER: AP settings not known");
  1324. return -4;
  1325. }
  1326. er->set_sel_reg_uuid_filter = uuid;
  1327. res = wps_registrar_button_pushed(er->wps->registrar, NULL);
  1328. er->set_sel_reg_uuid_filter = NULL;
  1329. if (res)
  1330. return -1;
  1331. return 0;
  1332. }
  1333. static void wps_er_ap_settings_cb(void *ctx, const struct wps_credential *cred)
  1334. {
  1335. struct wps_er_ap *ap = ctx;
  1336. union wps_event_data data;
  1337. wpa_printf(MSG_DEBUG, "WPS ER: AP Settings received");
  1338. os_free(ap->ap_settings);
  1339. ap->ap_settings = os_malloc(sizeof(*cred));
  1340. if (ap->ap_settings) {
  1341. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1342. ap->ap_settings->cred_attr = NULL;
  1343. }
  1344. os_memset(&data, 0, sizeof(data));
  1345. data.ap_settings.uuid = ap->uuid;
  1346. data.ap_settings.cred = cred;
  1347. ap->er->wps->event_cb(ap->er->wps->cb_ctx, WPS_EV_ER_AP_SETTINGS,
  1348. &data);
  1349. }
  1350. static void wps_er_http_put_message_cb(void *ctx, struct http_client *c,
  1351. enum http_client_event event)
  1352. {
  1353. struct wps_er_ap *ap = ctx;
  1354. struct wpabuf *reply;
  1355. char *msg = NULL;
  1356. switch (event) {
  1357. case HTTP_CLIENT_OK:
  1358. wpa_printf(MSG_DEBUG, "WPS ER: PutMessage OK");
  1359. reply = http_client_get_body(c);
  1360. if (reply == NULL)
  1361. break;
  1362. msg = os_zalloc(wpabuf_len(reply) + 1);
  1363. if (msg == NULL)
  1364. break;
  1365. os_memcpy(msg, wpabuf_head(reply), wpabuf_len(reply));
  1366. break;
  1367. case HTTP_CLIENT_FAILED:
  1368. case HTTP_CLIENT_INVALID_REPLY:
  1369. case HTTP_CLIENT_TIMEOUT:
  1370. wpa_printf(MSG_DEBUG, "WPS ER: PutMessage failed");
  1371. if (ap->wps) {
  1372. wps_deinit(ap->wps);
  1373. ap->wps = NULL;
  1374. }
  1375. break;
  1376. }
  1377. http_client_free(ap->http);
  1378. ap->http = NULL;
  1379. if (msg) {
  1380. struct wpabuf *buf;
  1381. enum http_reply_code ret;
  1382. buf = xml_get_base64_item(msg, "NewOutMessage", &ret);
  1383. os_free(msg);
  1384. if (buf == NULL) {
  1385. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
  1386. "NewOutMessage from PutMessage response");
  1387. wps_deinit(ap->wps);
  1388. ap->wps = NULL;
  1389. return;
  1390. }
  1391. wps_er_ap_process(ap, buf);
  1392. wpabuf_free(buf);
  1393. }
  1394. }
  1395. static void wps_er_ap_put_message(struct wps_er_ap *ap,
  1396. const struct wpabuf *msg)
  1397. {
  1398. struct wpabuf *buf;
  1399. char *len_ptr, *body_ptr;
  1400. struct sockaddr_in dst;
  1401. char *url, *path;
  1402. if (ap->http) {
  1403. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
  1404. "with the AP - cannot continue learn");
  1405. return;
  1406. }
  1407. if (ap->control_url == NULL) {
  1408. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1409. return;
  1410. }
  1411. url = http_client_url_parse(ap->control_url, &dst, &path);
  1412. if (url == NULL) {
  1413. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1414. return;
  1415. }
  1416. buf = wps_er_soap_hdr(msg, "PutMessage", "NewInMessage", path, &dst,
  1417. &len_ptr, &body_ptr);
  1418. os_free(url);
  1419. if (buf == NULL)
  1420. return;
  1421. wps_er_soap_end(buf, "PutMessage", len_ptr, body_ptr);
  1422. ap->http = http_client_addr(&dst, buf, 10000,
  1423. wps_er_http_put_message_cb, ap);
  1424. if (ap->http == NULL)
  1425. wpabuf_free(buf);
  1426. }
  1427. static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg)
  1428. {
  1429. enum wps_process_res res;
  1430. struct wps_parse_attr attr;
  1431. enum wsc_op_code op_code;
  1432. op_code = WSC_MSG;
  1433. if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
  1434. switch (*attr.msg_type) {
  1435. case WPS_WSC_ACK:
  1436. op_code = WSC_ACK;
  1437. break;
  1438. case WPS_WSC_NACK:
  1439. op_code = WSC_NACK;
  1440. break;
  1441. case WPS_WSC_DONE:
  1442. op_code = WSC_Done;
  1443. break;
  1444. }
  1445. }
  1446. res = wps_process_msg(ap->wps, op_code, msg);
  1447. if (res == WPS_CONTINUE) {
  1448. struct wpabuf *next = wps_get_msg(ap->wps, &op_code);
  1449. if (next) {
  1450. wps_er_ap_put_message(ap, next);
  1451. wpabuf_free(next);
  1452. } else {
  1453. wpa_printf(MSG_DEBUG, "WPS ER: Failed to build "
  1454. "message");
  1455. wps_deinit(ap->wps);
  1456. ap->wps = NULL;
  1457. }
  1458. } else if (res == WPS_DONE) {
  1459. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run done");
  1460. wps_deinit(ap->wps);
  1461. ap->wps = NULL;
  1462. } else {
  1463. wpa_printf(MSG_DEBUG, "WPS ER: Failed to process message from "
  1464. "AP (res=%d)", res);
  1465. wps_deinit(ap->wps);
  1466. ap->wps = NULL;
  1467. }
  1468. }
  1469. static void wps_er_ap_learn_m1(struct wps_er_ap *ap, struct wpabuf *m1)
  1470. {
  1471. struct wps_config cfg;
  1472. if (ap->wps) {
  1473. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
  1474. "progress with this AP");
  1475. return;
  1476. }
  1477. os_memset(&cfg, 0, sizeof(cfg));
  1478. cfg.wps = ap->er->wps;
  1479. cfg.registrar = 1;
  1480. ap->wps = wps_init(&cfg);
  1481. if (ap->wps == NULL)
  1482. return;
  1483. ap->wps->ap_settings_cb = wps_er_ap_settings_cb;
  1484. ap->wps->ap_settings_cb_ctx = ap;
  1485. wps_er_ap_process(ap, m1);
  1486. }
  1487. static void wps_er_ap_learn(struct wps_er_ap *ap, const char *dev_info)
  1488. {
  1489. struct wpabuf *info;
  1490. enum http_reply_code ret;
  1491. wpa_printf(MSG_DEBUG, "WPS ER: Received GetDeviceInfo response (M1) "
  1492. "from the AP");
  1493. info = xml_get_base64_item(dev_info, "NewDeviceInfo", &ret);
  1494. if (info == NULL) {
  1495. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
  1496. "NewDeviceInfo from GetDeviceInfo response");
  1497. return;
  1498. }
  1499. ap->m1_handler(ap, info);
  1500. wpabuf_free(info);
  1501. }
  1502. static void wps_er_http_get_dev_info_cb(void *ctx, struct http_client *c,
  1503. enum http_client_event event)
  1504. {
  1505. struct wps_er_ap *ap = ctx;
  1506. struct wpabuf *reply;
  1507. char *dev_info = NULL;
  1508. switch (event) {
  1509. case HTTP_CLIENT_OK:
  1510. wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo OK");
  1511. reply = http_client_get_body(c);
  1512. if (reply == NULL)
  1513. break;
  1514. dev_info = os_zalloc(wpabuf_len(reply) + 1);
  1515. if (dev_info == NULL)
  1516. break;
  1517. os_memcpy(dev_info, wpabuf_head(reply), wpabuf_len(reply));
  1518. break;
  1519. case HTTP_CLIENT_FAILED:
  1520. case HTTP_CLIENT_INVALID_REPLY:
  1521. case HTTP_CLIENT_TIMEOUT:
  1522. wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo failed");
  1523. break;
  1524. }
  1525. http_client_free(ap->http);
  1526. ap->http = NULL;
  1527. if (dev_info) {
  1528. wps_er_ap_learn(ap, dev_info);
  1529. os_free(dev_info);
  1530. }
  1531. }
  1532. static int wps_er_send_get_device_info(struct wps_er_ap *ap,
  1533. void (*m1_handler)(struct wps_er_ap *ap,
  1534. struct wpabuf *m1))
  1535. {
  1536. struct wpabuf *buf;
  1537. char *len_ptr, *body_ptr;
  1538. struct sockaddr_in dst;
  1539. char *url, *path;
  1540. if (ap->http) {
  1541. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
  1542. "with the AP - cannot get device info");
  1543. return -1;
  1544. }
  1545. if (ap->control_url == NULL) {
  1546. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1547. return -1;
  1548. }
  1549. url = http_client_url_parse(ap->control_url, &dst, &path);
  1550. if (url == NULL) {
  1551. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1552. return -1;
  1553. }
  1554. buf = wps_er_soap_hdr(NULL, "GetDeviceInfo", NULL, path, &dst,
  1555. &len_ptr, &body_ptr);
  1556. os_free(url);
  1557. if (buf == NULL)
  1558. return -1;
  1559. wps_er_soap_end(buf, "GetDeviceInfo", len_ptr, body_ptr);
  1560. ap->http = http_client_addr(&dst, buf, 10000,
  1561. wps_er_http_get_dev_info_cb, ap);
  1562. if (ap->http == NULL) {
  1563. wpabuf_free(buf);
  1564. return -1;
  1565. }
  1566. ap->m1_handler = m1_handler;
  1567. return 0;
  1568. }
  1569. int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
  1570. size_t pin_len)
  1571. {
  1572. struct wps_er_ap *ap;
  1573. if (er == NULL)
  1574. return -1;
  1575. ap = wps_er_ap_get(er, NULL, uuid);
  1576. if (ap == NULL) {
  1577. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for learn "
  1578. "request");
  1579. return -1;
  1580. }
  1581. if (ap->wps) {
  1582. wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
  1583. "with the AP - cannot start learn");
  1584. return -1;
  1585. }
  1586. if (wps_er_send_get_device_info(ap, wps_er_ap_learn_m1) < 0)
  1587. return -1;
  1588. er->skip_set_sel_reg = 1;
  1589. wps_registrar_add_pin(er->wps->registrar, NULL, uuid, pin, pin_len, 0);
  1590. er->skip_set_sel_reg = 0;
  1591. return 0;
  1592. }
  1593. int wps_er_set_config(struct wps_er *er, const u8 *uuid,
  1594. const struct wps_credential *cred)
  1595. {
  1596. struct wps_er_ap *ap;
  1597. if (er == NULL)
  1598. return -1;
  1599. ap = wps_er_ap_get(er, NULL, uuid);
  1600. if (ap == NULL) {
  1601. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for set config "
  1602. "request");
  1603. return -1;
  1604. }
  1605. os_free(ap->ap_settings);
  1606. ap->ap_settings = os_malloc(sizeof(*cred));
  1607. if (ap->ap_settings == NULL)
  1608. return -1;
  1609. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1610. ap->ap_settings->cred_attr = NULL;
  1611. wpa_printf(MSG_DEBUG, "WPS ER: Updated local AP settings based set "
  1612. "config request");
  1613. return 0;
  1614. }
  1615. static void wps_er_ap_config_m1(struct wps_er_ap *ap, struct wpabuf *m1)
  1616. {
  1617. struct wps_config cfg;
  1618. if (ap->wps) {
  1619. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
  1620. "progress with this AP");
  1621. return;
  1622. }
  1623. os_memset(&cfg, 0, sizeof(cfg));
  1624. cfg.wps = ap->er->wps;
  1625. cfg.registrar = 1;
  1626. cfg.new_ap_settings = ap->ap_settings;
  1627. ap->wps = wps_init(&cfg);
  1628. if (ap->wps == NULL)
  1629. return;
  1630. ap->wps->ap_settings_cb = NULL;
  1631. ap->wps->ap_settings_cb_ctx = NULL;
  1632. wps_er_ap_process(ap, m1);
  1633. }
  1634. int wps_er_config(struct wps_er *er, const u8 *uuid, const u8 *pin,
  1635. size_t pin_len, const struct wps_credential *cred)
  1636. {
  1637. struct wps_er_ap *ap;
  1638. if (er == NULL)
  1639. return -1;
  1640. ap = wps_er_ap_get(er, NULL, uuid);
  1641. if (ap == NULL) {
  1642. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for config "
  1643. "request");
  1644. return -1;
  1645. }
  1646. if (ap->wps) {
  1647. wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
  1648. "with the AP - cannot start config");
  1649. return -1;
  1650. }
  1651. os_free(ap->ap_settings);
  1652. ap->ap_settings = os_malloc(sizeof(*cred));
  1653. if (ap->ap_settings == NULL)
  1654. return -1;
  1655. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1656. ap->ap_settings->cred_attr = NULL;
  1657. if (wps_er_send_get_device_info(ap, wps_er_ap_config_m1) < 0)
  1658. return -1;
  1659. er->skip_set_sel_reg = 1;
  1660. wps_registrar_add_pin(er->wps->registrar, NULL, uuid, pin, pin_len, 0);
  1661. er->skip_set_sel_reg = 0;
  1662. return 0;
  1663. }
  1664. #ifdef CONFIG_WPS_NFC
  1665. struct wpabuf * wps_er_nfc_config_token(struct wps_er *er, const u8 *uuid)
  1666. {
  1667. struct wps_er_ap *ap;
  1668. struct wpabuf *ret;
  1669. struct wps_data data;
  1670. if (er == NULL)
  1671. return NULL;
  1672. ap = wps_er_ap_get(er, NULL, uuid);
  1673. if (ap == NULL)
  1674. return NULL;
  1675. if (ap->ap_settings == NULL) {
  1676. wpa_printf(MSG_DEBUG, "WPS ER: No settings known for the "
  1677. "selected AP");
  1678. return NULL;
  1679. }
  1680. ret = wpabuf_alloc(500);
  1681. if (ret == NULL)
  1682. return NULL;
  1683. os_memset(&data, 0, sizeof(data));
  1684. data.wps = er->wps;
  1685. data.use_cred = ap->ap_settings;
  1686. if (wps_build_version(ret) ||
  1687. wps_build_cred(&data, ret) ||
  1688. wps_build_wfa_ext(ret, 0, NULL, 0)) {
  1689. wpabuf_free(ret);
  1690. return NULL;
  1691. }
  1692. return ret;
  1693. }
  1694. #endif /* CONFIG_WPS_NFC */