wps_er.c 51 KB

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