wps_er.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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 = os_malloc(attr->manufacturer_len + 1);
  664. if (sta->manufacturer) {
  665. os_memcpy(sta->manufacturer, attr->manufacturer,
  666. attr->manufacturer_len);
  667. sta->manufacturer[attr->manufacturer_len] = '\0';
  668. }
  669. }
  670. if (attr->model_name) {
  671. os_free(sta->model_name);
  672. sta->model_name = os_malloc(attr->model_name_len + 1);
  673. if (sta->model_name) {
  674. os_memcpy(sta->model_name, attr->model_name,
  675. attr->model_name_len);
  676. sta->model_name[attr->model_name_len] = '\0';
  677. }
  678. }
  679. if (attr->model_number) {
  680. os_free(sta->model_number);
  681. sta->model_number = os_malloc(attr->model_number_len + 1);
  682. if (sta->model_number) {
  683. os_memcpy(sta->model_number, attr->model_number,
  684. attr->model_number_len);
  685. sta->model_number[attr->model_number_len] = '\0';
  686. }
  687. }
  688. if (attr->serial_number) {
  689. os_free(sta->serial_number);
  690. sta->serial_number = os_malloc(attr->serial_number_len + 1);
  691. if (sta->serial_number) {
  692. os_memcpy(sta->serial_number, attr->serial_number,
  693. attr->serial_number_len);
  694. sta->serial_number[attr->serial_number_len] = '\0';
  695. }
  696. }
  697. if (attr->dev_name) {
  698. os_free(sta->dev_name);
  699. sta->dev_name = os_malloc(attr->dev_name_len + 1);
  700. if (sta->dev_name) {
  701. os_memcpy(sta->dev_name, attr->dev_name,
  702. attr->dev_name_len);
  703. sta->dev_name[attr->dev_name_len] = '\0';
  704. }
  705. }
  706. eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
  707. eloop_register_timeout(300, 0, wps_er_sta_timeout, sta, NULL);
  708. if (m1 || new_sta)
  709. wps_er_sta_event(ap->er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
  710. return sta;
  711. }
  712. static void wps_er_process_wlanevent_probe_req(struct wps_er_ap *ap,
  713. const u8 *addr,
  714. struct wpabuf *msg)
  715. {
  716. struct wps_parse_attr attr;
  717. wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - Probe Request - from "
  718. MACSTR, MAC2STR(addr));
  719. wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
  720. "(TLVs from Probe Request)", msg);
  721. if (wps_validate_probe_req(msg, addr) < 0) {
  722. wpa_printf(MSG_INFO, "WPS-STRICT: ER: Ignore invalid proxied "
  723. "Probe Request frame from " MACSTR, MAC2STR(addr));
  724. return;
  725. }
  726. if (wps_parse_msg(msg, &attr) < 0) {
  727. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
  728. "WLANEvent message");
  729. return;
  730. }
  731. wps_er_add_sta_data(ap, addr, &attr, 1);
  732. wps_registrar_probe_req_rx(ap->er->wps->registrar, addr, msg, 0);
  733. }
  734. static void wps_er_http_put_wlan_response_cb(void *ctx, struct http_client *c,
  735. enum http_client_event event)
  736. {
  737. struct wps_er_sta *sta = ctx;
  738. switch (event) {
  739. case HTTP_CLIENT_OK:
  740. wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse OK");
  741. break;
  742. case HTTP_CLIENT_FAILED:
  743. case HTTP_CLIENT_INVALID_REPLY:
  744. case HTTP_CLIENT_TIMEOUT:
  745. wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse failed");
  746. break;
  747. }
  748. http_client_free(sta->http);
  749. sta->http = NULL;
  750. }
  751. static const char *soap_prefix =
  752. "<?xml version=\"1.0\"?>\n"
  753. "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  754. "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
  755. "<s:Body>\n";
  756. static const char *soap_postfix =
  757. "</s:Body>\n</s:Envelope>\n";
  758. static const char *urn_wfawlanconfig =
  759. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
  760. static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
  761. const char *name, const char *arg_name,
  762. const char *path,
  763. const struct sockaddr_in *dst,
  764. char **len_ptr, char **body_ptr)
  765. {
  766. unsigned char *encoded;
  767. size_t encoded_len;
  768. struct wpabuf *buf;
  769. if (msg) {
  770. encoded = base64_encode(wpabuf_head(msg), wpabuf_len(msg),
  771. &encoded_len);
  772. if (encoded == NULL)
  773. return NULL;
  774. } else {
  775. encoded = NULL;
  776. encoded_len = 0;
  777. }
  778. buf = wpabuf_alloc(1000 + encoded_len);
  779. if (buf == NULL) {
  780. os_free(encoded);
  781. return NULL;
  782. }
  783. wpabuf_printf(buf,
  784. "POST %s HTTP/1.1\r\n"
  785. "Host: %s:%d\r\n"
  786. "Content-Type: text/xml; charset=\"utf-8\"\r\n"
  787. "Content-Length: ",
  788. path, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
  789. *len_ptr = wpabuf_put(buf, 0);
  790. wpabuf_printf(buf,
  791. " \r\n"
  792. "SOAPACTION: \"%s#%s\"\r\n"
  793. "\r\n",
  794. urn_wfawlanconfig, name);
  795. *body_ptr = wpabuf_put(buf, 0);
  796. wpabuf_put_str(buf, soap_prefix);
  797. wpabuf_printf(buf, "<u:%s xmlns:u=\"", name);
  798. wpabuf_put_str(buf, urn_wfawlanconfig);
  799. wpabuf_put_str(buf, "\">\n");
  800. if (encoded) {
  801. wpabuf_printf(buf, "<%s>%s</%s>\n",
  802. arg_name, (char *) encoded, arg_name);
  803. os_free(encoded);
  804. }
  805. return buf;
  806. }
  807. static void wps_er_soap_end(struct wpabuf *buf, const char *name,
  808. char *len_ptr, char *body_ptr)
  809. {
  810. char len_buf[10];
  811. wpabuf_printf(buf, "</u:%s>\n", name);
  812. wpabuf_put_str(buf, soap_postfix);
  813. os_snprintf(len_buf, sizeof(len_buf), "%d",
  814. (int) ((char *) wpabuf_put(buf, 0) - body_ptr));
  815. os_memcpy(len_ptr, len_buf, os_strlen(len_buf));
  816. }
  817. static void wps_er_sta_send_msg(struct wps_er_sta *sta, struct wpabuf *msg)
  818. {
  819. struct wpabuf *buf;
  820. char *len_ptr, *body_ptr;
  821. struct sockaddr_in dst;
  822. char *url, *path;
  823. if (sta->http) {
  824. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for STA - "
  825. "ignore new request");
  826. wpabuf_free(msg);
  827. return;
  828. }
  829. if (sta->ap->control_url == NULL) {
  830. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  831. wpabuf_free(msg);
  832. return;
  833. }
  834. url = http_client_url_parse(sta->ap->control_url, &dst, &path);
  835. if (url == NULL) {
  836. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  837. wpabuf_free(msg);
  838. return;
  839. }
  840. buf = wps_er_soap_hdr(msg, "PutWLANResponse", "NewMessage", path, &dst,
  841. &len_ptr, &body_ptr);
  842. wpabuf_free(msg);
  843. os_free(url);
  844. if (buf == NULL)
  845. return;
  846. wpabuf_printf(buf, "<NewWLANEventType>%d</NewWLANEventType>\n",
  847. UPNP_WPS_WLANEVENT_TYPE_EAP);
  848. wpabuf_printf(buf, "<NewWLANEventMAC>" MACSTR "</NewWLANEventMAC>\n",
  849. MAC2STR(sta->addr));
  850. wps_er_soap_end(buf, "PutWLANResponse", len_ptr, body_ptr);
  851. sta->http = http_client_addr(&dst, buf, 1000,
  852. wps_er_http_put_wlan_response_cb, sta);
  853. if (sta->http == NULL)
  854. wpabuf_free(buf);
  855. }
  856. static void wps_er_sta_process(struct wps_er_sta *sta, struct wpabuf *msg,
  857. enum wsc_op_code op_code)
  858. {
  859. enum wps_process_res res;
  860. res = wps_process_msg(sta->wps, op_code, msg);
  861. if (res == WPS_CONTINUE) {
  862. struct wpabuf *next = wps_get_msg(sta->wps, &op_code);
  863. if (next)
  864. wps_er_sta_send_msg(sta, next);
  865. } else {
  866. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run %s with the "
  867. "enrollee (res=%d)",
  868. res == WPS_DONE ? "succeeded" : "failed", res);
  869. wps_deinit(sta->wps);
  870. sta->wps = NULL;
  871. if (res == WPS_DONE) {
  872. /* Remove the STA entry after short timeout */
  873. eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
  874. eloop_register_timeout(10, 0, wps_er_sta_timeout, sta,
  875. NULL);
  876. }
  877. }
  878. }
  879. static void wps_er_sta_start(struct wps_er_sta *sta, struct wpabuf *msg)
  880. {
  881. struct wps_config cfg;
  882. if (sta->wps)
  883. wps_deinit(sta->wps);
  884. os_memset(&cfg, 0, sizeof(cfg));
  885. cfg.wps = sta->ap->er->wps;
  886. cfg.registrar = 1;
  887. cfg.peer_addr = sta->addr;
  888. sta->wps = wps_init(&cfg);
  889. if (sta->wps == NULL)
  890. return;
  891. sta->wps->er = 1;
  892. sta->wps->use_cred = sta->ap->ap_settings;
  893. if (sta->ap->ap_settings) {
  894. os_free(sta->cred);
  895. sta->cred = os_malloc(sizeof(*sta->cred));
  896. if (sta->cred) {
  897. os_memcpy(sta->cred, sta->ap->ap_settings,
  898. sizeof(*sta->cred));
  899. sta->cred->cred_attr = NULL;
  900. os_memcpy(sta->cred->mac_addr, sta->addr, ETH_ALEN);
  901. sta->wps->use_cred = sta->cred;
  902. }
  903. }
  904. wps_er_sta_process(sta, msg, WSC_MSG);
  905. }
  906. static void wps_er_process_wlanevent_eap(struct wps_er_ap *ap, const u8 *addr,
  907. struct wpabuf *msg)
  908. {
  909. struct wps_parse_attr attr;
  910. struct wps_er_sta *sta;
  911. wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - EAP - from " MACSTR,
  912. MAC2STR(addr));
  913. wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
  914. "(TLVs from EAP-WSC)", msg);
  915. if (wps_parse_msg(msg, &attr) < 0) {
  916. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
  917. "WLANEvent message");
  918. return;
  919. }
  920. sta = wps_er_add_sta_data(ap, addr, &attr, 0);
  921. if (sta == NULL)
  922. return;
  923. if (attr.msg_type && *attr.msg_type == WPS_M1)
  924. wps_er_sta_start(sta, msg);
  925. else if (sta->wps) {
  926. enum wsc_op_code op_code = WSC_MSG;
  927. if (attr.msg_type) {
  928. switch (*attr.msg_type) {
  929. case WPS_WSC_ACK:
  930. op_code = WSC_ACK;
  931. break;
  932. case WPS_WSC_NACK:
  933. op_code = WSC_NACK;
  934. break;
  935. case WPS_WSC_DONE:
  936. op_code = WSC_Done;
  937. break;
  938. }
  939. }
  940. wps_er_sta_process(sta, msg, op_code);
  941. }
  942. }
  943. static void wps_er_process_wlanevent(struct wps_er_ap *ap,
  944. struct wpabuf *event)
  945. {
  946. u8 *data;
  947. u8 wlan_event_type;
  948. u8 wlan_event_mac[ETH_ALEN];
  949. struct wpabuf msg;
  950. wpa_hexdump(MSG_MSGDUMP, "WPS ER: Received WLANEvent",
  951. wpabuf_head(event), wpabuf_len(event));
  952. if (wpabuf_len(event) < 1 + 17) {
  953. wpa_printf(MSG_DEBUG, "WPS ER: Too short WLANEvent");
  954. return;
  955. }
  956. data = wpabuf_mhead(event);
  957. wlan_event_type = data[0];
  958. if (hwaddr_aton((char *) data + 1, wlan_event_mac) < 0) {
  959. wpa_printf(MSG_DEBUG, "WPS ER: Invalid WLANEventMAC in "
  960. "WLANEvent");
  961. return;
  962. }
  963. wpabuf_set(&msg, data + 1 + 17, wpabuf_len(event) - (1 + 17));
  964. switch (wlan_event_type) {
  965. case 1:
  966. wps_er_process_wlanevent_probe_req(ap, wlan_event_mac, &msg);
  967. break;
  968. case 2:
  969. wps_er_process_wlanevent_eap(ap, wlan_event_mac, &msg);
  970. break;
  971. default:
  972. wpa_printf(MSG_DEBUG, "WPS ER: Unknown WLANEventType %d",
  973. wlan_event_type);
  974. break;
  975. }
  976. }
  977. static void wps_er_http_event(struct wps_er *er, struct http_request *req,
  978. unsigned int ap_id)
  979. {
  980. struct wps_er_ap *ap = wps_er_ap_get_id(er, ap_id);
  981. struct wpabuf *event;
  982. enum http_reply_code ret;
  983. if (ap == NULL) {
  984. wpa_printf(MSG_DEBUG, "WPS ER: HTTP event from unknown AP id "
  985. "%u", ap_id);
  986. wps_er_http_resp_not_found(req);
  987. return;
  988. }
  989. wpa_printf(MSG_MSGDUMP, "WPS ER: HTTP event from AP id %u: %s",
  990. ap_id, http_request_get_data(req));
  991. event = xml_get_base64_item(http_request_get_data(req), "WLANEvent",
  992. &ret);
  993. if (event == NULL) {
  994. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract WLANEvent "
  995. "from the event notification");
  996. /*
  997. * Reply with OK anyway to avoid getting unregistered from
  998. * events.
  999. */
  1000. wps_er_http_resp_ok(req);
  1001. return;
  1002. }
  1003. wps_er_process_wlanevent(ap, event);
  1004. wpabuf_free(event);
  1005. wps_er_http_resp_ok(req);
  1006. }
  1007. static void wps_er_http_notify(struct wps_er *er, struct http_request *req)
  1008. {
  1009. char *uri = http_request_get_uri(req);
  1010. if (os_strncmp(uri, "/event/", 7) == 0) {
  1011. unsigned int event_id;
  1012. char *pos;
  1013. event_id = atoi(uri + 7);
  1014. if (event_id != er->event_id) {
  1015. wpa_printf(MSG_DEBUG, "WPS ER: HTTP event for an "
  1016. "unknown event id %u", event_id);
  1017. return;
  1018. }
  1019. pos = os_strchr(uri + 7, '/');
  1020. if (pos == NULL)
  1021. return;
  1022. pos++;
  1023. wps_er_http_event(er, req, atoi(pos));
  1024. } else {
  1025. wpa_printf(MSG_DEBUG, "WPS ER: Unknown HTTP NOTIFY for '%s'",
  1026. uri);
  1027. wps_er_http_resp_not_found(req);
  1028. }
  1029. }
  1030. static void wps_er_http_req(void *ctx, struct http_request *req)
  1031. {
  1032. struct wps_er *er = ctx;
  1033. struct sockaddr_in *cli = http_request_get_cli_addr(req);
  1034. enum httpread_hdr_type type = http_request_get_type(req);
  1035. struct wpabuf *buf;
  1036. wpa_printf(MSG_DEBUG, "WPS ER: HTTP request: '%s' (type %d) from "
  1037. "%s:%d",
  1038. http_request_get_uri(req), type,
  1039. inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));
  1040. switch (type) {
  1041. case HTTPREAD_HDR_TYPE_NOTIFY:
  1042. wps_er_http_notify(er, req);
  1043. break;
  1044. default:
  1045. wpa_printf(MSG_DEBUG, "WPS ER: Unsupported HTTP request type "
  1046. "%d", type);
  1047. buf = wpabuf_alloc(200);
  1048. if (buf == NULL) {
  1049. http_request_deinit(req);
  1050. return;
  1051. }
  1052. wpabuf_put_str(buf,
  1053. "HTTP/1.1 501 Unimplemented\r\n"
  1054. "Connection: close\r\n");
  1055. http_put_date(buf);
  1056. wpabuf_put_str(buf, "\r\n");
  1057. http_request_send_and_deinit(req, buf);
  1058. break;
  1059. }
  1060. }
  1061. struct wps_er *
  1062. wps_er_init(struct wps_context *wps, const char *ifname, const char *filter)
  1063. {
  1064. struct wps_er *er;
  1065. struct in_addr addr;
  1066. er = os_zalloc(sizeof(*er));
  1067. if (er == NULL)
  1068. return NULL;
  1069. dl_list_init(&er->ap);
  1070. dl_list_init(&er->ap_unsubscribing);
  1071. dl_list_init(&er->ap_settings);
  1072. er->multicast_sd = -1;
  1073. er->ssdp_sd = -1;
  1074. os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
  1075. er->wps = wps;
  1076. if (os_get_random((unsigned char *) &er->event_id,
  1077. sizeof(er->event_id)) < 0) {
  1078. wps_er_deinit(er, NULL, NULL);
  1079. return NULL;
  1080. }
  1081. /* Limit event_id to < 32 bits to avoid issues with atoi() */
  1082. er->event_id &= 0x0fffffff;
  1083. if (filter) {
  1084. if (inet_aton(filter, &er->filter_addr) == 0) {
  1085. wpa_printf(MSG_INFO, "WPS UPnP: Invalid filter "
  1086. "address %s", filter);
  1087. wps_er_deinit(er, NULL, NULL);
  1088. return NULL;
  1089. }
  1090. wpa_printf(MSG_DEBUG, "WPS UPnP: Only accepting connections "
  1091. "with %s", filter);
  1092. }
  1093. if (get_netif_info(ifname, &er->ip_addr, &er->ip_addr_text,
  1094. er->mac_addr)) {
  1095. wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
  1096. "for %s. Does it have IP address?", ifname);
  1097. wps_er_deinit(er, NULL, NULL);
  1098. return NULL;
  1099. }
  1100. if (wps_er_ssdp_init(er) < 0) {
  1101. wpa_printf(MSG_INFO, "WPS UPnP: SSDP initialization failed");
  1102. wps_er_deinit(er, NULL, NULL);
  1103. return NULL;
  1104. }
  1105. addr.s_addr = er->ip_addr;
  1106. er->http_srv = http_server_init(&addr, -1, wps_er_http_req, er);
  1107. if (er->http_srv == NULL) {
  1108. wpa_printf(MSG_INFO, "WPS UPnP: HTTP initialization failed");
  1109. wps_er_deinit(er, NULL, NULL);
  1110. return NULL;
  1111. }
  1112. er->http_port = http_server_get_port(er->http_srv);
  1113. wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s)",
  1114. er->ifname, er->ip_addr_text);
  1115. return er;
  1116. }
  1117. void wps_er_refresh(struct wps_er *er)
  1118. {
  1119. struct wps_er_ap *ap;
  1120. struct wps_er_sta *sta;
  1121. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1122. wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_ADD);
  1123. dl_list_for_each(sta, &ap->sta, struct wps_er_sta, list)
  1124. wps_er_sta_event(er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
  1125. }
  1126. wps_er_send_ssdp_msearch(er);
  1127. }
  1128. static void wps_er_deinit_finish(void *eloop_data, void *user_ctx)
  1129. {
  1130. struct wps_er *er = eloop_data;
  1131. void (*deinit_done_cb)(void *ctx);
  1132. void *deinit_done_ctx;
  1133. wpa_printf(MSG_DEBUG, "WPS ER: Finishing deinit");
  1134. deinit_done_cb = er->deinit_done_cb;
  1135. deinit_done_ctx = er->deinit_done_ctx;
  1136. os_free(er->ip_addr_text);
  1137. os_free(er);
  1138. if (deinit_done_cb)
  1139. deinit_done_cb(deinit_done_ctx);
  1140. }
  1141. void wps_er_deinit(struct wps_er *er, void (*cb)(void *ctx), void *ctx)
  1142. {
  1143. if (er == NULL)
  1144. return;
  1145. http_server_deinit(er->http_srv);
  1146. wps_er_ap_remove_all(er);
  1147. wps_er_ssdp_deinit(er);
  1148. eloop_register_timeout(dl_list_empty(&er->ap_unsubscribing) ? 0 : 5, 0,
  1149. wps_er_deinit_finish, er, NULL);
  1150. wpa_printf(MSG_DEBUG, "WPS ER: Finish deinit from timeout");
  1151. er->deinitializing = 1;
  1152. er->deinit_done_cb = cb;
  1153. er->deinit_done_ctx = ctx;
  1154. }
  1155. static void wps_er_http_set_sel_reg_cb(void *ctx, struct http_client *c,
  1156. enum http_client_event event)
  1157. {
  1158. struct wps_er_ap *ap = ctx;
  1159. union wps_event_data data;
  1160. os_memset(&data, 0, sizeof(data));
  1161. switch (event) {
  1162. case HTTP_CLIENT_OK:
  1163. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar OK");
  1164. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_DONE;
  1165. data.set_sel_reg.uuid = ap->uuid;
  1166. break;
  1167. case HTTP_CLIENT_FAILED:
  1168. case HTTP_CLIENT_INVALID_REPLY:
  1169. case HTTP_CLIENT_TIMEOUT:
  1170. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar failed");
  1171. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_FAILED;
  1172. data.set_sel_reg.uuid = ap->uuid;
  1173. break;
  1174. }
  1175. http_client_free(ap->http);
  1176. ap->http = NULL;
  1177. if (data.set_sel_reg.uuid)
  1178. ap->er->wps->event_cb(ap->er->wps->cb_ctx,
  1179. WPS_EV_ER_SET_SELECTED_REGISTRAR, &data);
  1180. }
  1181. static void wps_er_send_set_sel_reg(struct wps_er_ap *ap, struct wpabuf *msg)
  1182. {
  1183. struct wpabuf *buf;
  1184. char *len_ptr, *body_ptr;
  1185. struct sockaddr_in dst;
  1186. char *url, *path;
  1187. if (ap->control_url == NULL) {
  1188. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1189. return;
  1190. }
  1191. if (ap->http) {
  1192. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for AP - "
  1193. "ignore new request");
  1194. return;
  1195. }
  1196. if (ap->wps) {
  1197. wpa_printf(MSG_DEBUG, "WPS ER: Pending WPS operation for AP - "
  1198. "skip SetSelectedRegistrar");
  1199. return;
  1200. }
  1201. url = http_client_url_parse(ap->control_url, &dst, &path);
  1202. if (url == NULL) {
  1203. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1204. return;
  1205. }
  1206. buf = wps_er_soap_hdr(msg, "SetSelectedRegistrar", "NewMessage", path,
  1207. &dst, &len_ptr, &body_ptr);
  1208. os_free(url);
  1209. if (buf == NULL)
  1210. return;
  1211. wps_er_soap_end(buf, "SetSelectedRegistrar", len_ptr, body_ptr);
  1212. ap->http = http_client_addr(&dst, buf, 1000,
  1213. wps_er_http_set_sel_reg_cb, ap);
  1214. if (ap->http == NULL)
  1215. wpabuf_free(buf);
  1216. }
  1217. static int wps_er_build_selected_registrar(struct wpabuf *msg, int sel_reg)
  1218. {
  1219. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
  1220. wpabuf_put_be16(msg, 1);
  1221. wpabuf_put_u8(msg, !!sel_reg);
  1222. return 0;
  1223. }
  1224. static int wps_er_build_dev_password_id(struct wpabuf *msg, u16 dev_passwd_id)
  1225. {
  1226. wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
  1227. wpabuf_put_be16(msg, 2);
  1228. wpabuf_put_be16(msg, dev_passwd_id);
  1229. return 0;
  1230. }
  1231. static int wps_er_build_sel_reg_config_methods(struct wpabuf *msg,
  1232. u16 sel_reg_config_methods)
  1233. {
  1234. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
  1235. wpabuf_put_be16(msg, 2);
  1236. wpabuf_put_be16(msg, sel_reg_config_methods);
  1237. return 0;
  1238. }
  1239. static int wps_er_build_uuid_r(struct wpabuf *msg, const u8 *uuid_r)
  1240. {
  1241. #ifdef CONFIG_WPS2
  1242. wpabuf_put_be16(msg, ATTR_UUID_R);
  1243. wpabuf_put_be16(msg, WPS_UUID_LEN);
  1244. wpabuf_put_data(msg, uuid_r, WPS_UUID_LEN);
  1245. #endif /* CONFIG_WPS2 */
  1246. return 0;
  1247. }
  1248. void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
  1249. u16 sel_reg_config_methods)
  1250. {
  1251. struct wpabuf *msg;
  1252. struct wps_er_ap *ap;
  1253. struct wps_registrar *reg = er->wps->registrar;
  1254. const u8 *auth_macs;
  1255. #ifdef CONFIG_WPS2
  1256. u8 bcast[ETH_ALEN];
  1257. #endif /* CONFIG_WPS2 */
  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. #ifdef CONFIG_WPS2
  1269. if (count == 0) {
  1270. os_memset(bcast, 0xff, ETH_ALEN);
  1271. auth_macs = bcast;
  1272. count = 1;
  1273. }
  1274. #endif /* CONFIG_WPS2 */
  1275. if (wps_build_version(msg) ||
  1276. wps_er_build_selected_registrar(msg, sel_reg) ||
  1277. wps_er_build_dev_password_id(msg, dev_passwd_id) ||
  1278. wps_er_build_sel_reg_config_methods(msg, sel_reg_config_methods) ||
  1279. wps_build_wfa_ext(msg, 0, auth_macs, count) ||
  1280. wps_er_build_uuid_r(msg, er->wps->uuid)) {
  1281. wpabuf_free(msg);
  1282. return;
  1283. }
  1284. os_memset(&data, 0, sizeof(data));
  1285. data.set_sel_reg.sel_reg = sel_reg;
  1286. data.set_sel_reg.dev_passwd_id = dev_passwd_id;
  1287. data.set_sel_reg.sel_reg_config_methods = sel_reg_config_methods;
  1288. data.set_sel_reg.state = WPS_ER_SET_SEL_REG_START;
  1289. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1290. if (er->set_sel_reg_uuid_filter &&
  1291. os_memcmp(ap->uuid, er->set_sel_reg_uuid_filter,
  1292. WPS_UUID_LEN) != 0)
  1293. continue;
  1294. data.set_sel_reg.uuid = ap->uuid;
  1295. er->wps->event_cb(er->wps->cb_ctx,
  1296. WPS_EV_ER_SET_SELECTED_REGISTRAR, &data);
  1297. wps_er_send_set_sel_reg(ap, msg);
  1298. }
  1299. wpabuf_free(msg);
  1300. }
  1301. int wps_er_pbc(struct wps_er *er, const u8 *uuid, const u8 *addr)
  1302. {
  1303. int res;
  1304. struct wps_er_ap *ap;
  1305. if (er == NULL || er->wps == NULL)
  1306. return -1;
  1307. if (wps_registrar_pbc_overlap(er->wps->registrar, NULL, NULL)) {
  1308. wpa_printf(MSG_DEBUG, "WPS ER: PBC overlap - do not start PBC "
  1309. "mode");
  1310. return -2;
  1311. }
  1312. if (uuid)
  1313. ap = wps_er_ap_get(er, NULL, uuid, NULL);
  1314. else
  1315. ap = NULL;
  1316. if (ap == NULL) {
  1317. struct wps_er_sta *sta = NULL;
  1318. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1319. sta = wps_er_sta_get(ap, addr, uuid);
  1320. if (sta) {
  1321. uuid = ap->uuid;
  1322. break;
  1323. }
  1324. }
  1325. if (sta == NULL)
  1326. return -3; /* Unknown UUID */
  1327. }
  1328. if (ap->ap_settings == NULL) {
  1329. wpa_printf(MSG_DEBUG, "WPS ER: AP settings not known");
  1330. return -4;
  1331. }
  1332. er->set_sel_reg_uuid_filter = uuid;
  1333. res = wps_registrar_button_pushed(er->wps->registrar, NULL);
  1334. er->set_sel_reg_uuid_filter = NULL;
  1335. if (res)
  1336. return -1;
  1337. return 0;
  1338. }
  1339. static void wps_er_ap_settings_cb(void *ctx, const struct wps_credential *cred)
  1340. {
  1341. struct wps_er_ap *ap = ctx;
  1342. union wps_event_data data;
  1343. wpa_printf(MSG_DEBUG, "WPS ER: AP Settings received");
  1344. os_free(ap->ap_settings);
  1345. ap->ap_settings = os_malloc(sizeof(*cred));
  1346. if (ap->ap_settings) {
  1347. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1348. ap->ap_settings->cred_attr = NULL;
  1349. }
  1350. os_memset(&data, 0, sizeof(data));
  1351. data.ap_settings.uuid = ap->uuid;
  1352. data.ap_settings.cred = cred;
  1353. ap->er->wps->event_cb(ap->er->wps->cb_ctx, WPS_EV_ER_AP_SETTINGS,
  1354. &data);
  1355. }
  1356. const u8 * wps_er_get_sta_uuid(struct wps_er *er, const u8 *addr)
  1357. {
  1358. struct wps_er_ap *ap;
  1359. dl_list_for_each(ap, &er->ap, struct wps_er_ap, list) {
  1360. struct wps_er_sta *sta;
  1361. sta = wps_er_sta_get(ap, addr, NULL);
  1362. if (sta)
  1363. return sta->uuid;
  1364. }
  1365. return NULL;
  1366. }
  1367. static void wps_er_http_put_message_cb(void *ctx, struct http_client *c,
  1368. enum http_client_event event)
  1369. {
  1370. struct wps_er_ap *ap = ctx;
  1371. struct wpabuf *reply;
  1372. char *msg = NULL;
  1373. switch (event) {
  1374. case HTTP_CLIENT_OK:
  1375. wpa_printf(MSG_DEBUG, "WPS ER: PutMessage OK");
  1376. reply = http_client_get_body(c);
  1377. if (reply == NULL)
  1378. break;
  1379. msg = os_zalloc(wpabuf_len(reply) + 1);
  1380. if (msg == NULL)
  1381. break;
  1382. os_memcpy(msg, wpabuf_head(reply), wpabuf_len(reply));
  1383. break;
  1384. case HTTP_CLIENT_FAILED:
  1385. case HTTP_CLIENT_INVALID_REPLY:
  1386. case HTTP_CLIENT_TIMEOUT:
  1387. wpa_printf(MSG_DEBUG, "WPS ER: PutMessage failed");
  1388. if (ap->wps) {
  1389. wps_deinit(ap->wps);
  1390. ap->wps = NULL;
  1391. }
  1392. break;
  1393. }
  1394. http_client_free(ap->http);
  1395. ap->http = NULL;
  1396. if (msg) {
  1397. struct wpabuf *buf;
  1398. enum http_reply_code ret;
  1399. buf = xml_get_base64_item(msg, "NewOutMessage", &ret);
  1400. os_free(msg);
  1401. if (buf == NULL) {
  1402. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
  1403. "NewOutMessage from PutMessage response");
  1404. wps_deinit(ap->wps);
  1405. ap->wps = NULL;
  1406. return;
  1407. }
  1408. wps_er_ap_process(ap, buf);
  1409. wpabuf_free(buf);
  1410. }
  1411. }
  1412. static void wps_er_ap_put_message(struct wps_er_ap *ap,
  1413. const struct wpabuf *msg)
  1414. {
  1415. struct wpabuf *buf;
  1416. char *len_ptr, *body_ptr;
  1417. struct sockaddr_in dst;
  1418. char *url, *path;
  1419. if (ap->http) {
  1420. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
  1421. "with the AP - cannot continue learn");
  1422. return;
  1423. }
  1424. if (ap->control_url == NULL) {
  1425. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1426. return;
  1427. }
  1428. url = http_client_url_parse(ap->control_url, &dst, &path);
  1429. if (url == NULL) {
  1430. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1431. return;
  1432. }
  1433. buf = wps_er_soap_hdr(msg, "PutMessage", "NewInMessage", path, &dst,
  1434. &len_ptr, &body_ptr);
  1435. os_free(url);
  1436. if (buf == NULL)
  1437. return;
  1438. wps_er_soap_end(buf, "PutMessage", len_ptr, body_ptr);
  1439. ap->http = http_client_addr(&dst, buf, 10000,
  1440. wps_er_http_put_message_cb, ap);
  1441. if (ap->http == NULL)
  1442. wpabuf_free(buf);
  1443. }
  1444. static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg)
  1445. {
  1446. enum wps_process_res res;
  1447. struct wps_parse_attr attr;
  1448. enum wsc_op_code op_code;
  1449. op_code = WSC_MSG;
  1450. if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
  1451. switch (*attr.msg_type) {
  1452. case WPS_WSC_ACK:
  1453. op_code = WSC_ACK;
  1454. break;
  1455. case WPS_WSC_NACK:
  1456. op_code = WSC_NACK;
  1457. break;
  1458. case WPS_WSC_DONE:
  1459. op_code = WSC_Done;
  1460. break;
  1461. }
  1462. }
  1463. res = wps_process_msg(ap->wps, op_code, msg);
  1464. if (res == WPS_CONTINUE) {
  1465. struct wpabuf *next = wps_get_msg(ap->wps, &op_code);
  1466. if (next) {
  1467. wps_er_ap_put_message(ap, next);
  1468. wpabuf_free(next);
  1469. } else {
  1470. wpa_printf(MSG_DEBUG, "WPS ER: Failed to build "
  1471. "message");
  1472. wps_deinit(ap->wps);
  1473. ap->wps = NULL;
  1474. }
  1475. } else if (res == WPS_DONE) {
  1476. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run done");
  1477. wps_deinit(ap->wps);
  1478. ap->wps = NULL;
  1479. } else {
  1480. wpa_printf(MSG_DEBUG, "WPS ER: Failed to process message from "
  1481. "AP (res=%d)", res);
  1482. wps_deinit(ap->wps);
  1483. ap->wps = NULL;
  1484. }
  1485. }
  1486. static void wps_er_ap_learn_m1(struct wps_er_ap *ap, struct wpabuf *m1)
  1487. {
  1488. struct wps_config cfg;
  1489. if (ap->wps) {
  1490. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
  1491. "progress with this AP");
  1492. return;
  1493. }
  1494. os_memset(&cfg, 0, sizeof(cfg));
  1495. cfg.wps = ap->er->wps;
  1496. cfg.registrar = 1;
  1497. ap->wps = wps_init(&cfg);
  1498. if (ap->wps == NULL)
  1499. return;
  1500. ap->wps->ap_settings_cb = wps_er_ap_settings_cb;
  1501. ap->wps->ap_settings_cb_ctx = ap;
  1502. wps_er_ap_process(ap, m1);
  1503. }
  1504. static void wps_er_ap_learn(struct wps_er_ap *ap, const char *dev_info)
  1505. {
  1506. struct wpabuf *info;
  1507. enum http_reply_code ret;
  1508. wpa_printf(MSG_DEBUG, "WPS ER: Received GetDeviceInfo response (M1) "
  1509. "from the AP");
  1510. info = xml_get_base64_item(dev_info, "NewDeviceInfo", &ret);
  1511. if (info == NULL) {
  1512. wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
  1513. "NewDeviceInfo from GetDeviceInfo response");
  1514. return;
  1515. }
  1516. ap->m1_handler(ap, info);
  1517. wpabuf_free(info);
  1518. }
  1519. static void wps_er_http_get_dev_info_cb(void *ctx, struct http_client *c,
  1520. enum http_client_event event)
  1521. {
  1522. struct wps_er_ap *ap = ctx;
  1523. struct wpabuf *reply;
  1524. char *dev_info = NULL;
  1525. switch (event) {
  1526. case HTTP_CLIENT_OK:
  1527. wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo OK");
  1528. reply = http_client_get_body(c);
  1529. if (reply == NULL)
  1530. break;
  1531. dev_info = os_zalloc(wpabuf_len(reply) + 1);
  1532. if (dev_info == NULL)
  1533. break;
  1534. os_memcpy(dev_info, wpabuf_head(reply), wpabuf_len(reply));
  1535. break;
  1536. case HTTP_CLIENT_FAILED:
  1537. case HTTP_CLIENT_INVALID_REPLY:
  1538. case HTTP_CLIENT_TIMEOUT:
  1539. wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo failed");
  1540. break;
  1541. }
  1542. http_client_free(ap->http);
  1543. ap->http = NULL;
  1544. if (dev_info) {
  1545. wps_er_ap_learn(ap, dev_info);
  1546. os_free(dev_info);
  1547. }
  1548. }
  1549. static int wps_er_send_get_device_info(struct wps_er_ap *ap,
  1550. void (*m1_handler)(struct wps_er_ap *ap,
  1551. struct wpabuf *m1))
  1552. {
  1553. struct wpabuf *buf;
  1554. char *len_ptr, *body_ptr;
  1555. struct sockaddr_in dst;
  1556. char *url, *path;
  1557. if (ap->http) {
  1558. wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
  1559. "with the AP - cannot get device info");
  1560. return -1;
  1561. }
  1562. if (ap->control_url == NULL) {
  1563. wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
  1564. return -1;
  1565. }
  1566. url = http_client_url_parse(ap->control_url, &dst, &path);
  1567. if (url == NULL) {
  1568. wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
  1569. return -1;
  1570. }
  1571. buf = wps_er_soap_hdr(NULL, "GetDeviceInfo", NULL, path, &dst,
  1572. &len_ptr, &body_ptr);
  1573. os_free(url);
  1574. if (buf == NULL)
  1575. return -1;
  1576. wps_er_soap_end(buf, "GetDeviceInfo", len_ptr, body_ptr);
  1577. ap->http = http_client_addr(&dst, buf, 10000,
  1578. wps_er_http_get_dev_info_cb, ap);
  1579. if (ap->http == NULL) {
  1580. wpabuf_free(buf);
  1581. return -1;
  1582. }
  1583. ap->m1_handler = m1_handler;
  1584. return 0;
  1585. }
  1586. int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *addr,
  1587. const u8 *pin, size_t pin_len)
  1588. {
  1589. struct wps_er_ap *ap;
  1590. if (er == NULL)
  1591. return -1;
  1592. ap = wps_er_ap_get(er, NULL, uuid, addr);
  1593. if (ap == NULL) {
  1594. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for learn "
  1595. "request");
  1596. return -1;
  1597. }
  1598. if (uuid == NULL)
  1599. uuid = ap->uuid;
  1600. if (ap->wps) {
  1601. wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
  1602. "with the AP - cannot start learn");
  1603. return -1;
  1604. }
  1605. if (wps_er_send_get_device_info(ap, wps_er_ap_learn_m1) < 0)
  1606. return -1;
  1607. er->skip_set_sel_reg = 1;
  1608. wps_registrar_add_pin(er->wps->registrar, NULL, uuid, pin, pin_len, 0);
  1609. er->skip_set_sel_reg = 0;
  1610. return 0;
  1611. }
  1612. int wps_er_set_config(struct wps_er *er, const u8 *uuid, const u8 *addr,
  1613. const struct wps_credential *cred)
  1614. {
  1615. struct wps_er_ap *ap;
  1616. if (er == NULL)
  1617. return -1;
  1618. ap = wps_er_ap_get(er, NULL, uuid, addr);
  1619. if (ap == NULL) {
  1620. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for set config "
  1621. "request");
  1622. return -1;
  1623. }
  1624. os_free(ap->ap_settings);
  1625. ap->ap_settings = os_malloc(sizeof(*cred));
  1626. if (ap->ap_settings == NULL)
  1627. return -1;
  1628. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1629. ap->ap_settings->cred_attr = NULL;
  1630. wpa_printf(MSG_DEBUG, "WPS ER: Updated local AP settings based set "
  1631. "config request");
  1632. return 0;
  1633. }
  1634. static void wps_er_ap_config_m1(struct wps_er_ap *ap, struct wpabuf *m1)
  1635. {
  1636. struct wps_config cfg;
  1637. if (ap->wps) {
  1638. wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
  1639. "progress with this AP");
  1640. return;
  1641. }
  1642. os_memset(&cfg, 0, sizeof(cfg));
  1643. cfg.wps = ap->er->wps;
  1644. cfg.registrar = 1;
  1645. cfg.new_ap_settings = ap->ap_settings;
  1646. ap->wps = wps_init(&cfg);
  1647. if (ap->wps == NULL)
  1648. return;
  1649. ap->wps->ap_settings_cb = NULL;
  1650. ap->wps->ap_settings_cb_ctx = NULL;
  1651. wps_er_ap_process(ap, m1);
  1652. }
  1653. int wps_er_config(struct wps_er *er, const u8 *uuid, const u8 *addr,
  1654. const u8 *pin, size_t pin_len,
  1655. const struct wps_credential *cred)
  1656. {
  1657. struct wps_er_ap *ap;
  1658. if (er == NULL)
  1659. return -1;
  1660. ap = wps_er_ap_get(er, NULL, uuid, addr);
  1661. if (ap == NULL) {
  1662. wpa_printf(MSG_DEBUG, "WPS ER: AP not found for config "
  1663. "request");
  1664. return -1;
  1665. }
  1666. if (uuid == NULL)
  1667. uuid = ap->uuid;
  1668. if (ap->wps) {
  1669. wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
  1670. "with the AP - cannot start config");
  1671. return -1;
  1672. }
  1673. os_free(ap->ap_settings);
  1674. ap->ap_settings = os_malloc(sizeof(*cred));
  1675. if (ap->ap_settings == NULL)
  1676. return -1;
  1677. os_memcpy(ap->ap_settings, cred, sizeof(*cred));
  1678. ap->ap_settings->cred_attr = NULL;
  1679. if (wps_er_send_get_device_info(ap, wps_er_ap_config_m1) < 0)
  1680. return -1;
  1681. er->skip_set_sel_reg = 1;
  1682. wps_registrar_add_pin(er->wps->registrar, NULL, uuid, pin, pin_len, 0);
  1683. er->skip_set_sel_reg = 0;
  1684. return 0;
  1685. }
  1686. #ifdef CONFIG_WPS_NFC
  1687. struct wpabuf * wps_er_config_token_from_cred(struct wps_context *wps,
  1688. struct wps_credential *cred)
  1689. {
  1690. struct wpabuf *ret;
  1691. struct wps_data data;
  1692. ret = wpabuf_alloc(500);
  1693. if (ret == NULL)
  1694. return NULL;
  1695. os_memset(&data, 0, sizeof(data));
  1696. data.wps = wps;
  1697. data.use_cred = cred;
  1698. if (wps_build_version(ret) ||
  1699. wps_build_cred(&data, ret) ||
  1700. wps_build_wfa_ext(ret, 0, NULL, 0)) {
  1701. wpabuf_free(ret);
  1702. return NULL;
  1703. }
  1704. return ret;
  1705. }
  1706. struct wpabuf * wps_er_nfc_config_token(struct wps_er *er, const u8 *uuid,
  1707. const u8 *addr)
  1708. {
  1709. struct wps_er_ap *ap;
  1710. if (er == NULL)
  1711. return NULL;
  1712. ap = wps_er_ap_get(er, NULL, uuid, addr);
  1713. if (ap == NULL)
  1714. return NULL;
  1715. if (ap->ap_settings == NULL) {
  1716. wpa_printf(MSG_DEBUG, "WPS ER: No settings known for the "
  1717. "selected AP");
  1718. return NULL;
  1719. }
  1720. return wps_er_config_token_from_cred(er->wps, ap->ap_settings);
  1721. }
  1722. #endif /* CONFIG_WPS_NFC */