wps_er.c 49 KB

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