wps_upnp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * UPnP WPS Device
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * Copyright (c) 2006-2007 Sony Corporation
  5. * Copyright (c) 2008-2009 Atheros Communications
  6. * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
  7. *
  8. * See below for more details on licensing and code history.
  9. */
  10. /*
  11. * This has been greatly stripped down from the original file
  12. * (upnp_wps_device.c) by Ted Merrill, Atheros Communications
  13. * in order to eliminate use of the bulky libupnp library etc.
  14. *
  15. * History:
  16. * upnp_wps_device.c is/was a shim layer between wps_opt_upnp.c and
  17. * the libupnp library.
  18. * The layering (by Sony) was well done; only a very minor modification
  19. * to API of upnp_wps_device.c was required.
  20. * libupnp was found to be undesirable because:
  21. * -- It consumed too much code and data space
  22. * -- It uses multiple threads, making debugging more difficult
  23. * and possibly reducing reliability.
  24. * -- It uses static variables and only supports one instance.
  25. * The shim and libupnp are here replaced by special code written
  26. * specifically for the needs of hostapd.
  27. * Various shortcuts can and are taken to keep the code size small.
  28. * Generally, execution time is not as crucial.
  29. *
  30. * BUGS:
  31. * -- UPnP requires that we be able to resolve domain names.
  32. * While uncommon, if we have to do it then it will stall the entire
  33. * hostapd program, which is bad.
  34. * This is because we use the standard linux getaddrinfo() function
  35. * which is syncronous.
  36. * An asyncronous solution would be to use the free "ares" library.
  37. * -- Does not have a robust output buffering scheme. Uses a single
  38. * fixed size output buffer per TCP/HTTP connection, with possible (although
  39. * unlikely) possibility of overflow and likely excessive use of RAM.
  40. * A better solution would be to write the HTTP output as a buffered stream,
  41. * using chunking: (handle header specially, then) generate data with
  42. * a printf-like function into a buffer, catching buffer full condition,
  43. * then send it out surrounded by http chunking.
  44. * -- There is some code that could be separated out into the common
  45. * library to be shared with wpa_supplicant.
  46. * -- Needs renaming with module prefix to avoid polluting the debugger
  47. * namespace and causing possible collisions with other static fncs
  48. * and structure declarations when using the debugger.
  49. * -- The http error code generation is pretty bogus, hopefully no one cares.
  50. *
  51. * Author: Ted Merrill, Atheros Communications, based upon earlier work
  52. * as explained above and below.
  53. *
  54. * Copyright:
  55. * Copyright 2008 Atheros Communications.
  56. *
  57. * The original header (of upnp_wps_device.c) reads:
  58. *
  59. * Copyright (c) 2006-2007 Sony Corporation. All Rights Reserved.
  60. *
  61. * File Name: upnp_wps_device.c
  62. * Description: EAP-WPS UPnP device source
  63. *
  64. * Redistribution and use in source and binary forms, with or without
  65. * modification, are permitted provided that the following conditions
  66. * are met:
  67. *
  68. * * Redistributions of source code must retain the above copyright
  69. * notice, this list of conditions and the following disclaimer.
  70. * * Redistributions in binary form must reproduce the above copyright
  71. * notice, this list of conditions and the following disclaimer in
  72. * the documentation and/or other materials provided with the
  73. * distribution.
  74. * * Neither the name of Sony Corporation nor the names of its
  75. * contributors may be used to endorse or promote products derived
  76. * from this software without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  79. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  80. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  81. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  82. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  83. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  84. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  85. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  86. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  87. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  88. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  89. *
  90. * Portions from Intel libupnp files, e.g. genlib/net/http/httpreadwrite.c
  91. * typical header:
  92. *
  93. * Copyright (c) 2000-2003 Intel Corporation
  94. * All rights reserved.
  95. *
  96. * Redistribution and use in source and binary forms, with or without
  97. * modification, are permitted provided that the following conditions are met:
  98. *
  99. * * Redistributions of source code must retain the above copyright notice,
  100. * this list of conditions and the following disclaimer.
  101. * * Redistributions in binary form must reproduce the above copyright notice,
  102. * this list of conditions and the following disclaimer in the documentation
  103. * and/or other materials provided with the distribution.
  104. * * Neither name of Intel Corporation nor the names of its contributors
  105. * may be used to endorse or promote products derived from this software
  106. * without specific prior written permission.
  107. *
  108. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  109. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  110. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  111. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  112. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  113. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  114. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  115. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  116. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  117. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  118. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  119. */
  120. /*
  121. * Overview of WPS over UPnP:
  122. *
  123. * UPnP is a protocol that allows devices to discover each other and control
  124. * each other. In UPnP terminology, a device is either a "device" (a server
  125. * that provides information about itself and allows itself to be controlled)
  126. * or a "control point" (a client that controls "devices") or possibly both.
  127. * This file implements a UPnP "device".
  128. *
  129. * For us, we use mostly basic UPnP discovery, but the control part of interest
  130. * is WPS carried via UPnP messages. There is quite a bit of basic UPnP
  131. * discovery to do before we can get to WPS, however.
  132. *
  133. * UPnP discovery begins with "devices" send out multicast UDP packets to a
  134. * certain fixed multicast IP address and port, and "control points" sending
  135. * out other such UDP packets.
  136. *
  137. * The packets sent by devices are NOTIFY packets (not to be confused with TCP
  138. * NOTIFY packets that are used later) and those sent by control points are
  139. * M-SEARCH packets. These packets contain a simple HTTP style header. The
  140. * packets are sent redundantly to get around packet loss. Devices respond to
  141. * M-SEARCH packets with HTTP-like UDP packets containing HTTP/1.1 200 OK
  142. * messages, which give similar information as the UDP NOTIFY packets.
  143. *
  144. * The above UDP packets advertise the (arbitrary) TCP ports that the
  145. * respective parties will listen to. The control point can then do a HTTP
  146. * SUBSCRIBE (something like an HTTP PUT) after which the device can do a
  147. * separate HTTP NOTIFY (also like an HTTP PUT) to do event messaging.
  148. *
  149. * The control point will also do HTTP GET of the "device file" listed in the
  150. * original UDP information from the device (see UPNP_WPS_DEVICE_XML_FILE
  151. * data), and based on this will do additional GETs... HTTP POSTs are done to
  152. * cause an action.
  153. *
  154. * Beyond some basic information in HTTP headers, additional information is in
  155. * the HTTP bodies, in a format set by the SOAP and XML standards, a markup
  156. * language related to HTML used for web pages. This language is intended to
  157. * provide the ultimate in self-documentation by providing a universal
  158. * namespace based on pseudo-URLs called URIs. Note that although a URI looks
  159. * like a URL (a web address), they are never accessed as such but are used
  160. * only as identifiers.
  161. *
  162. * The POST of a GetDeviceInfo gets information similar to what might be
  163. * obtained from a probe request or response on Wi-Fi. WPS messages M1-M8
  164. * are passed via a POST of a PutMessage; the M1-M8 WPS messages are converted
  165. * to a bin64 ascii representation for encapsulation. When proxying messages,
  166. * WLANEvent and PutWLANResponse are used.
  167. *
  168. * This of course glosses over a lot of details.
  169. */
  170. #include "includes.h"
  171. #include <time.h>
  172. #include <net/if.h>
  173. #include <netdb.h>
  174. #include <sys/ioctl.h>
  175. #include "common.h"
  176. #include "uuid.h"
  177. #include "base64.h"
  178. #include "wps.h"
  179. #include "wps_i.h"
  180. #include "wps_upnp.h"
  181. #include "wps_upnp_i.h"
  182. /*
  183. * UPnP allows a client ("control point") to send a server like us ("device")
  184. * a domain name for registration, and we are supposed to resolve it. This is
  185. * bad because, using the standard Linux library, we will stall the entire
  186. * hostapd waiting for resolution.
  187. *
  188. * The "correct" solution would be to use an event driven library for domain
  189. * name resolution such as "ares". However, this would increase code size
  190. * further. Since it is unlikely that we'll actually see such domain names, we
  191. * can just refuse to accept them.
  192. */
  193. #define NO_DOMAIN_NAME_RESOLUTION 1 /* 1 to allow only dotted ip addresses */
  194. /*
  195. * UPnP does not scale well. If we were in a room with thousands of control
  196. * points then potentially we could be expected to handle subscriptions for
  197. * each of them, which would exhaust our memory. So we must set a limit. In
  198. * practice we are unlikely to see more than one or two.
  199. */
  200. #define MAX_SUBSCRIPTIONS 4 /* how many subscribing clients we handle */
  201. #define MAX_ADDR_PER_SUBSCRIPTION 8
  202. /* Maximum number of Probe Request events per second */
  203. #define MAX_EVENTS_PER_SEC 5
  204. static struct upnp_wps_device_sm *shared_upnp_device = NULL;
  205. /* Write the current date/time per RFC */
  206. void format_date(struct wpabuf *buf)
  207. {
  208. const char *weekday_str = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
  209. const char *month_str = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
  210. "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
  211. struct tm *date;
  212. time_t t;
  213. t = time(NULL);
  214. date = gmtime(&t);
  215. wpabuf_printf(buf, "%s, %02d %s %d %02d:%02d:%02d GMT",
  216. &weekday_str[date->tm_wday * 4], date->tm_mday,
  217. &month_str[date->tm_mon * 4], date->tm_year + 1900,
  218. date->tm_hour, date->tm_min, date->tm_sec);
  219. }
  220. /***************************************************************************
  221. * UUIDs (unique identifiers)
  222. *
  223. * These are supposed to be unique in all the world.
  224. * Sometimes permanent ones are used, sometimes temporary ones
  225. * based on random numbers... there are different rules for valid content
  226. * of different types.
  227. * Each uuid is 16 bytes long.
  228. **************************************************************************/
  229. /* uuid_make -- construct a random UUID
  230. * The UPnP documents don't seem to offer any guidelines as to which method to
  231. * use for constructing UUIDs for subscriptions. Presumably any method from
  232. * rfc4122 is good enough; I've chosen random number method.
  233. */
  234. static void uuid_make(u8 uuid[UUID_LEN])
  235. {
  236. os_get_random(uuid, UUID_LEN);
  237. /* Replace certain bits as specified in rfc4122 or X.667 */
  238. uuid[6] &= 0x0f; uuid[6] |= (4 << 4); /* version 4 == random gen */
  239. uuid[8] &= 0x3f; uuid[8] |= 0x80;
  240. }
  241. /*
  242. * Subscriber address handling.
  243. * Since a subscriber may have an arbitrary number of addresses, we have to
  244. * add a bunch of code to handle them.
  245. *
  246. * Addresses are passed in text, and MAY be domain names instead of the (usual
  247. * and expected) dotted IP addresses. Resolving domain names consumes a lot of
  248. * resources. Worse, we are currently using the standard Linux getaddrinfo()
  249. * which will block the entire program until complete or timeout! The proper
  250. * solution would be to use the "ares" library or similar with more state
  251. * machine steps etc. or just disable domain name resolution by setting
  252. * NO_DOMAIN_NAME_RESOLUTION to 1 at top of this file.
  253. */
  254. /* subscr_addr_delete -- delete single unlinked subscriber address
  255. * (be sure to unlink first if need be)
  256. */
  257. void subscr_addr_delete(struct subscr_addr *a)
  258. {
  259. /*
  260. * Note: do NOT free domain_and_port or path because they point to
  261. * memory within the allocation of "a".
  262. */
  263. os_free(a);
  264. }
  265. /* subscr_addr_free_all -- unlink and delete list of subscriber addresses. */
  266. static void subscr_addr_free_all(struct subscription *s)
  267. {
  268. struct subscr_addr *a, *tmp;
  269. dl_list_for_each_safe(a, tmp, &s->addr_list, struct subscr_addr, list)
  270. {
  271. dl_list_del(&a->list);
  272. subscr_addr_delete(a);
  273. }
  274. }
  275. /* subscr_addr_add_url -- add address(es) for one url to subscription */
  276. static void subscr_addr_add_url(struct subscription *s, const char *url,
  277. size_t url_len)
  278. {
  279. int alloc_len;
  280. char *scratch_mem = NULL;
  281. char *mem;
  282. char *host;
  283. char *delim;
  284. char *path;
  285. int port = 80; /* port to send to (default is port 80) */
  286. struct addrinfo hints;
  287. struct addrinfo *result = NULL;
  288. struct addrinfo *rp;
  289. int rerr;
  290. size_t host_len, path_len;
  291. /* url MUST begin with http: */
  292. if (url_len < 7 || os_strncasecmp(url, "http://", 7))
  293. goto fail;
  294. url += 7;
  295. url_len -= 7;
  296. /* Make a copy of the string to allow modification during parsing */
  297. scratch_mem = dup_binstr(url, url_len);
  298. if (scratch_mem == NULL)
  299. goto fail;
  300. wpa_printf(MSG_DEBUG, "WPS UPnP: Adding URL '%s'", scratch_mem);
  301. host = scratch_mem;
  302. path = os_strchr(host, '/');
  303. if (path)
  304. *path++ = '\0'; /* null terminate host */
  305. /* Process and remove optional port component */
  306. delim = os_strchr(host, ':');
  307. if (delim) {
  308. *delim = '\0'; /* null terminate host name for now */
  309. if (isdigit(delim[1]))
  310. port = atol(delim + 1);
  311. }
  312. /*
  313. * getaddrinfo does the right thing with dotted decimal notations, or
  314. * will resolve domain names. Resolving domain names will unfortunately
  315. * hang the entire program until it is resolved or it times out
  316. * internal to getaddrinfo; fortunately we think that the use of actual
  317. * domain names (vs. dotted decimal notations) should be uncommon.
  318. */
  319. os_memset(&hints, 0, sizeof(struct addrinfo));
  320. hints.ai_family = AF_INET; /* IPv4 */
  321. hints.ai_socktype = SOCK_STREAM;
  322. #if NO_DOMAIN_NAME_RESOLUTION
  323. /* Suppress domain name resolutions that would halt
  324. * the program for periods of time
  325. */
  326. hints.ai_flags = AI_NUMERICHOST;
  327. #else
  328. /* Allow domain name resolution. */
  329. hints.ai_flags = 0;
  330. #endif
  331. hints.ai_protocol = 0; /* Any protocol? */
  332. rerr = getaddrinfo(host, NULL /* fill in port ourselves */,
  333. &hints, &result);
  334. if (rerr) {
  335. wpa_printf(MSG_INFO, "WPS UPnP: Resolve error %d (%s) on: %s",
  336. rerr, gai_strerror(rerr), host);
  337. goto fail;
  338. }
  339. if (delim)
  340. *delim = ':'; /* Restore port */
  341. host_len = os_strlen(host);
  342. path_len = path ? os_strlen(path) : 0;
  343. alloc_len = host_len + 1 + 1 + path_len + 1;
  344. for (rp = result; rp; rp = rp->ai_next) {
  345. struct subscr_addr *a;
  346. /* Limit no. of address to avoid denial of service attack */
  347. if (dl_list_len(&s->addr_list) >= MAX_ADDR_PER_SUBSCRIPTION) {
  348. wpa_printf(MSG_INFO, "WPS UPnP: subscr_addr_add_url: "
  349. "Ignoring excessive addresses");
  350. break;
  351. }
  352. a = os_zalloc(sizeof(*a) + alloc_len);
  353. if (a == NULL)
  354. break;
  355. mem = (char *) (a + 1);
  356. a->domain_and_port = mem;
  357. os_memcpy(mem, host, host_len);
  358. mem += host_len + 1;
  359. a->path = mem;
  360. if (path == NULL || path[0] != '/')
  361. *mem++ = '/';
  362. if (path)
  363. os_memcpy(mem, path, path_len);
  364. os_memcpy(&a->saddr, rp->ai_addr, sizeof(a->saddr));
  365. a->saddr.sin_port = htons(port);
  366. dl_list_add(&s->addr_list, &a->list);
  367. }
  368. fail:
  369. if (result)
  370. freeaddrinfo(result);
  371. os_free(scratch_mem);
  372. }
  373. /* subscr_addr_list_create -- create list from urls in string.
  374. * Each url is enclosed by angle brackets.
  375. */
  376. static void subscr_addr_list_create(struct subscription *s,
  377. const char *url_list)
  378. {
  379. const char *end;
  380. wpa_printf(MSG_DEBUG, "WPS UPnP: Parsing URL list '%s'", url_list);
  381. for (;;) {
  382. while (*url_list == ' ' || *url_list == '\t')
  383. url_list++;
  384. if (*url_list != '<')
  385. break;
  386. url_list++;
  387. end = os_strchr(url_list, '>');
  388. if (end == NULL)
  389. break;
  390. subscr_addr_add_url(s, url_list, end - url_list);
  391. url_list = end + 1;
  392. }
  393. }
  394. int send_wpabuf(int fd, struct wpabuf *buf)
  395. {
  396. wpa_printf(MSG_DEBUG, "WPS UPnP: Send %lu byte message",
  397. (unsigned long) wpabuf_len(buf));
  398. errno = 0;
  399. if (write(fd, wpabuf_head(buf), wpabuf_len(buf)) !=
  400. (int) wpabuf_len(buf)) {
  401. wpa_printf(MSG_ERROR, "WPS UPnP: Failed to send buffer: "
  402. "errno=%d (%s)",
  403. errno, strerror(errno));
  404. return -1;
  405. }
  406. return 0;
  407. }
  408. static void wpabuf_put_property(struct wpabuf *buf, const char *name,
  409. const char *value)
  410. {
  411. wpabuf_put_str(buf, "<e:property>");
  412. wpabuf_printf(buf, "<%s>", name);
  413. if (value)
  414. wpabuf_put_str(buf, value);
  415. wpabuf_printf(buf, "</%s>", name);
  416. wpabuf_put_str(buf, "</e:property>\n");
  417. }
  418. /**
  419. * upnp_wps_device_send_event - Queue event messages for subscribers
  420. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  421. *
  422. * This function queues the last WLANEvent to be sent for all currently
  423. * subscribed UPnP control points. sm->wlanevent must have been set with the
  424. * encoded data before calling this function.
  425. */
  426. static void upnp_wps_device_send_event(struct upnp_wps_device_sm *sm)
  427. {
  428. /* Enqueue event message for all subscribers */
  429. struct wpabuf *buf; /* holds event message */
  430. int buf_size = 0;
  431. struct subscription *s, *tmp;
  432. /* Actually, utf-8 is the default, but it doesn't hurt to specify it */
  433. const char *format_head =
  434. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  435. "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
  436. const char *format_tail = "</e:propertyset>\n";
  437. struct os_time now;
  438. if (dl_list_empty(&sm->subscriptions)) {
  439. /* optimize */
  440. return;
  441. }
  442. if (os_get_time(&now) == 0) {
  443. if (now.sec != sm->last_event_sec) {
  444. sm->last_event_sec = now.sec;
  445. sm->num_events_in_sec = 1;
  446. } else {
  447. sm->num_events_in_sec++;
  448. /*
  449. * In theory, this should apply to all WLANEvent
  450. * notifications, but EAP messages are of much higher
  451. * priority and Probe Request notifications should not
  452. * be allowed to drop EAP messages, so only throttle
  453. * Probe Request notifications.
  454. */
  455. if (sm->num_events_in_sec > MAX_EVENTS_PER_SEC &&
  456. sm->wlanevent_type ==
  457. UPNP_WPS_WLANEVENT_TYPE_PROBE) {
  458. wpa_printf(MSG_DEBUG, "WPS UPnP: Throttle "
  459. "event notifications (%u seen "
  460. "during one second)",
  461. sm->num_events_in_sec);
  462. return;
  463. }
  464. }
  465. }
  466. /* Determine buffer size needed first */
  467. buf_size += os_strlen(format_head);
  468. buf_size += 50 + 2 * os_strlen("WLANEvent");
  469. if (sm->wlanevent)
  470. buf_size += os_strlen(sm->wlanevent);
  471. buf_size += os_strlen(format_tail);
  472. buf = wpabuf_alloc(buf_size);
  473. if (buf == NULL)
  474. return;
  475. wpabuf_put_str(buf, format_head);
  476. wpabuf_put_property(buf, "WLANEvent", sm->wlanevent);
  477. wpabuf_put_str(buf, format_tail);
  478. wpa_printf(MSG_MSGDUMP, "WPS UPnP: WLANEvent message:\n%s",
  479. (char *) wpabuf_head(buf));
  480. dl_list_for_each_safe(s, tmp, &sm->subscriptions, struct subscription,
  481. list) {
  482. event_add(s, buf,
  483. sm->wlanevent_type == UPNP_WPS_WLANEVENT_TYPE_PROBE);
  484. }
  485. wpabuf_free(buf);
  486. }
  487. /*
  488. * Event subscription (subscriber machines register with us to receive event
  489. * messages).
  490. * This is the result of an incoming HTTP over TCP SUBSCRIBE request.
  491. */
  492. /* subscription_destroy -- destroy an unlinked subscription
  493. * Be sure to unlink first if necessary.
  494. */
  495. void subscription_destroy(struct subscription *s)
  496. {
  497. struct upnp_wps_device_interface *iface;
  498. wpa_printf(MSG_DEBUG, "WPS UPnP: Destroy subscription %p", s);
  499. subscr_addr_free_all(s);
  500. event_delete_all(s);
  501. dl_list_for_each(iface, &s->sm->interfaces,
  502. struct upnp_wps_device_interface, list)
  503. upnp_er_remove_notification(iface->wps->registrar, s);
  504. os_free(s);
  505. }
  506. /* subscription_list_age -- remove expired subscriptions */
  507. static void subscription_list_age(struct upnp_wps_device_sm *sm, time_t now)
  508. {
  509. struct subscription *s, *tmp;
  510. dl_list_for_each_safe(s, tmp, &sm->subscriptions,
  511. struct subscription, list) {
  512. if (s->timeout_time > now)
  513. break;
  514. wpa_printf(MSG_DEBUG, "WPS UPnP: Removing aged subscription");
  515. dl_list_del(&s->list);
  516. subscription_destroy(s);
  517. }
  518. }
  519. /* subscription_find -- return existing subscription matching uuid, if any
  520. * returns NULL if not found
  521. */
  522. struct subscription * subscription_find(struct upnp_wps_device_sm *sm,
  523. const u8 uuid[UUID_LEN])
  524. {
  525. struct subscription *s;
  526. dl_list_for_each(s, &sm->subscriptions, struct subscription, list) {
  527. if (os_memcmp(s->uuid, uuid, UUID_LEN) == 0)
  528. return s; /* Found match */
  529. }
  530. return NULL;
  531. }
  532. static struct wpabuf * build_fake_wsc_ack(void)
  533. {
  534. struct wpabuf *msg = wpabuf_alloc(100);
  535. if (msg == NULL)
  536. return NULL;
  537. wpabuf_put_u8(msg, UPNP_WPS_WLANEVENT_TYPE_EAP);
  538. wpabuf_put_str(msg, "00:00:00:00:00:00");
  539. if (wps_build_version(msg) ||
  540. wps_build_msg_type(msg, WPS_WSC_ACK)) {
  541. wpabuf_free(msg);
  542. return NULL;
  543. }
  544. /* Enrollee Nonce */
  545. wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
  546. wpabuf_put_be16(msg, WPS_NONCE_LEN);
  547. wpabuf_put(msg, WPS_NONCE_LEN);
  548. /* Registrar Nonce */
  549. wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
  550. wpabuf_put_be16(msg, WPS_NONCE_LEN);
  551. wpabuf_put(msg, WPS_NONCE_LEN);
  552. wps_build_wfa_ext(msg, 0, NULL, 0);
  553. return msg;
  554. }
  555. /* subscription_first_event -- send format/queue event that is automatically
  556. * sent on a new subscription.
  557. */
  558. static int subscription_first_event(struct subscription *s)
  559. {
  560. /*
  561. * Actually, utf-8 is the default, but it doesn't hurt to specify it.
  562. *
  563. * APStatus is apparently a bit set,
  564. * 0x1 = configuration change (but is always set?)
  565. * 0x10 = ap is locked
  566. *
  567. * Per UPnP spec, we send out the last value of each variable, even
  568. * for WLANEvent, whatever it was.
  569. */
  570. char *wlan_event;
  571. struct wpabuf *buf;
  572. int ap_status = 1; /* TODO: add 0x10 if access point is locked */
  573. const char *head =
  574. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  575. "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
  576. const char *tail = "</e:propertyset>\n";
  577. char txt[10];
  578. int ret;
  579. if (s->sm->wlanevent == NULL) {
  580. /*
  581. * There has been no events before the subscription. However,
  582. * UPnP device architecture specification requires all the
  583. * evented variables to be included, so generate a dummy event
  584. * for this particular case using a WSC_ACK and all-zeros
  585. * nonces. The ER (UPnP control point) will ignore this, but at
  586. * least it will learn that WLANEvent variable will be used in
  587. * event notifications in the future.
  588. */
  589. struct wpabuf *msg;
  590. wpa_printf(MSG_DEBUG, "WPS UPnP: Use a fake WSC_ACK as the "
  591. "initial WLANEvent");
  592. msg = build_fake_wsc_ack();
  593. if (msg) {
  594. s->sm->wlanevent = (char *)
  595. base64_encode(wpabuf_head(msg),
  596. wpabuf_len(msg), NULL);
  597. wpabuf_free(msg);
  598. }
  599. }
  600. wlan_event = s->sm->wlanevent;
  601. if (wlan_event == NULL || *wlan_event == '\0') {
  602. wpa_printf(MSG_DEBUG, "WPS UPnP: WLANEvent not known for "
  603. "initial event message");
  604. wlan_event = "";
  605. }
  606. buf = wpabuf_alloc(500 + os_strlen(wlan_event));
  607. if (buf == NULL)
  608. return -1;
  609. wpabuf_put_str(buf, head);
  610. wpabuf_put_property(buf, "STAStatus", "1");
  611. os_snprintf(txt, sizeof(txt), "%d", ap_status);
  612. wpabuf_put_property(buf, "APStatus", txt);
  613. if (*wlan_event)
  614. wpabuf_put_property(buf, "WLANEvent", wlan_event);
  615. wpabuf_put_str(buf, tail);
  616. ret = event_add(s, buf, 0);
  617. if (ret) {
  618. wpabuf_free(buf);
  619. return ret;
  620. }
  621. wpabuf_free(buf);
  622. return 0;
  623. }
  624. /**
  625. * subscription_start - Remember a UPnP control point to send events to.
  626. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  627. * @callback_urls: Callback URLs
  628. * Returns: %NULL on error, or pointer to new subscription structure.
  629. */
  630. struct subscription * subscription_start(struct upnp_wps_device_sm *sm,
  631. const char *callback_urls)
  632. {
  633. struct subscription *s;
  634. time_t now = time(NULL);
  635. time_t expire = now + UPNP_SUBSCRIBE_SEC;
  636. /* Get rid of expired subscriptions so we have room */
  637. subscription_list_age(sm, now);
  638. /* If too many subscriptions, remove oldest */
  639. if (dl_list_len(&sm->subscriptions) >= MAX_SUBSCRIPTIONS) {
  640. s = dl_list_first(&sm->subscriptions, struct subscription,
  641. list);
  642. wpa_printf(MSG_INFO, "WPS UPnP: Too many subscriptions, "
  643. "trashing oldest");
  644. dl_list_del(&s->list);
  645. subscription_destroy(s);
  646. }
  647. s = os_zalloc(sizeof(*s));
  648. if (s == NULL)
  649. return NULL;
  650. dl_list_init(&s->addr_list);
  651. dl_list_init(&s->event_queue);
  652. s->sm = sm;
  653. s->timeout_time = expire;
  654. uuid_make(s->uuid);
  655. subscr_addr_list_create(s, callback_urls);
  656. if (dl_list_empty(&s->addr_list)) {
  657. wpa_printf(MSG_DEBUG, "WPS UPnP: No valid callback URLs in "
  658. "'%s' - drop subscription", callback_urls);
  659. subscription_destroy(s);
  660. return NULL;
  661. }
  662. /* Add to end of list, since it has the highest expiration time */
  663. dl_list_add_tail(&sm->subscriptions, &s->list);
  664. /* Queue up immediate event message (our last event)
  665. * as required by UPnP spec.
  666. */
  667. if (subscription_first_event(s)) {
  668. wpa_printf(MSG_INFO, "WPS UPnP: Dropping subscriber due to "
  669. "event backlog");
  670. dl_list_del(&s->list);
  671. subscription_destroy(s);
  672. return NULL;
  673. }
  674. wpa_printf(MSG_DEBUG, "WPS UPnP: Subscription %p started with %s",
  675. s, callback_urls);
  676. /* Schedule sending this */
  677. event_send_all_later(sm);
  678. return s;
  679. }
  680. /* subscription_renew -- find subscription and reset timeout */
  681. struct subscription * subscription_renew(struct upnp_wps_device_sm *sm,
  682. const u8 uuid[UUID_LEN])
  683. {
  684. time_t now = time(NULL);
  685. time_t expire = now + UPNP_SUBSCRIBE_SEC;
  686. struct subscription *s = subscription_find(sm, uuid);
  687. if (s == NULL)
  688. return NULL;
  689. wpa_printf(MSG_DEBUG, "WPS UPnP: Subscription renewed");
  690. dl_list_del(&s->list);
  691. s->timeout_time = expire;
  692. /* add back to end of list, since it now has highest expiry */
  693. dl_list_add_tail(&sm->subscriptions, &s->list);
  694. return s;
  695. }
  696. /**
  697. * upnp_wps_device_send_wlan_event - Event notification
  698. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  699. * @from_mac_addr: Source (Enrollee) MAC address for the event
  700. * @ev_type: Event type
  701. * @msg: Event data
  702. * Returns: 0 on success, -1 on failure
  703. *
  704. * Tell external Registrars (UPnP control points) that something happened. In
  705. * particular, events include WPS messages from clients that are proxied to
  706. * external Registrars.
  707. */
  708. int upnp_wps_device_send_wlan_event(struct upnp_wps_device_sm *sm,
  709. const u8 from_mac_addr[ETH_ALEN],
  710. enum upnp_wps_wlanevent_type ev_type,
  711. const struct wpabuf *msg)
  712. {
  713. int ret = -1;
  714. char type[2];
  715. const u8 *mac = from_mac_addr;
  716. char mac_text[18];
  717. u8 *raw = NULL;
  718. size_t raw_len;
  719. char *val;
  720. size_t val_len;
  721. int pos = 0;
  722. if (!sm)
  723. goto fail;
  724. os_snprintf(type, sizeof(type), "%1u", ev_type);
  725. raw_len = 1 + 17 + (msg ? wpabuf_len(msg) : 0);
  726. raw = os_zalloc(raw_len);
  727. if (!raw)
  728. goto fail;
  729. *(raw + pos) = (u8) ev_type;
  730. pos += 1;
  731. os_snprintf(mac_text, sizeof(mac_text), MACSTR, MAC2STR(mac));
  732. wpa_printf(MSG_DEBUG, "WPS UPnP: Proxying WLANEvent from %s",
  733. mac_text);
  734. os_memcpy(raw + pos, mac_text, 17);
  735. pos += 17;
  736. if (msg) {
  737. os_memcpy(raw + pos, wpabuf_head(msg), wpabuf_len(msg));
  738. pos += wpabuf_len(msg);
  739. }
  740. raw_len = pos;
  741. val = (char *) base64_encode(raw, raw_len, &val_len);
  742. if (val == NULL)
  743. goto fail;
  744. os_free(sm->wlanevent);
  745. sm->wlanevent = val;
  746. sm->wlanevent_type = ev_type;
  747. upnp_wps_device_send_event(sm);
  748. ret = 0;
  749. fail:
  750. os_free(raw);
  751. return ret;
  752. }
  753. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  754. #include <sys/sysctl.h>
  755. #include <net/route.h>
  756. #include <net/if_dl.h>
  757. static int eth_get(const char *device, u8 ea[ETH_ALEN])
  758. {
  759. struct if_msghdr *ifm;
  760. struct sockaddr_dl *sdl;
  761. u_char *p, *buf;
  762. size_t len;
  763. int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
  764. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
  765. return -1;
  766. if ((buf = os_malloc(len)) == NULL)
  767. return -1;
  768. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  769. os_free(buf);
  770. return -1;
  771. }
  772. for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
  773. ifm = (struct if_msghdr *)p;
  774. sdl = (struct sockaddr_dl *)(ifm + 1);
  775. if (ifm->ifm_type != RTM_IFINFO ||
  776. (ifm->ifm_addrs & RTA_IFP) == 0)
  777. continue;
  778. if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
  779. os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
  780. continue;
  781. os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
  782. break;
  783. }
  784. os_free(buf);
  785. if (p >= buf + len) {
  786. errno = ESRCH;
  787. return -1;
  788. }
  789. return 0;
  790. }
  791. #endif /* __FreeBSD__ */
  792. /**
  793. * get_netif_info - Get hw and IP addresses for network device
  794. * @net_if: Selected network interface name
  795. * @ip_addr: Buffer for returning IP address in network byte order
  796. * @ip_addr_text: Buffer for returning a pointer to allocated IP address text
  797. * @mac: Buffer for returning MAC address
  798. * Returns: 0 on success, -1 on failure
  799. */
  800. int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
  801. u8 mac[ETH_ALEN])
  802. {
  803. struct ifreq req;
  804. int sock = -1;
  805. struct sockaddr_in *addr;
  806. struct in_addr in_addr;
  807. *ip_addr_text = os_zalloc(16);
  808. if (*ip_addr_text == NULL)
  809. goto fail;
  810. sock = socket(AF_INET, SOCK_DGRAM, 0);
  811. if (sock < 0)
  812. goto fail;
  813. os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
  814. if (ioctl(sock, SIOCGIFADDR, &req) < 0) {
  815. wpa_printf(MSG_ERROR, "WPS UPnP: SIOCGIFADDR failed: %d (%s)",
  816. errno, strerror(errno));
  817. goto fail;
  818. }
  819. addr = (void *) &req.ifr_addr;
  820. *ip_addr = addr->sin_addr.s_addr;
  821. in_addr.s_addr = *ip_addr;
  822. os_snprintf(*ip_addr_text, 16, "%s", inet_ntoa(in_addr));
  823. #ifdef __linux__
  824. os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
  825. if (ioctl(sock, SIOCGIFHWADDR, &req) < 0) {
  826. wpa_printf(MSG_ERROR, "WPS UPnP: SIOCGIFHWADDR failed: "
  827. "%d (%s)", errno, strerror(errno));
  828. goto fail;
  829. }
  830. os_memcpy(mac, req.ifr_addr.sa_data, 6);
  831. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  832. if (eth_get(net_if, mac) < 0) {
  833. wpa_printf(MSG_ERROR, "WPS UPnP: Failed to get MAC address");
  834. goto fail;
  835. }
  836. #else
  837. #error MAC address fetch not implemented
  838. #endif
  839. close(sock);
  840. return 0;
  841. fail:
  842. if (sock >= 0)
  843. close(sock);
  844. os_free(*ip_addr_text);
  845. *ip_addr_text = NULL;
  846. return -1;
  847. }
  848. static void upnp_wps_free_msearchreply(struct dl_list *head)
  849. {
  850. struct advertisement_state_machine *a, *tmp;
  851. dl_list_for_each_safe(a, tmp, head, struct advertisement_state_machine,
  852. list)
  853. msearchreply_state_machine_stop(a);
  854. }
  855. static void upnp_wps_free_subscriptions(struct dl_list *head,
  856. struct wps_registrar *reg)
  857. {
  858. struct subscription *s, *tmp;
  859. dl_list_for_each_safe(s, tmp, head, struct subscription, list) {
  860. if (reg && s->reg != reg)
  861. continue;
  862. dl_list_del(&s->list);
  863. subscription_destroy(s);
  864. }
  865. }
  866. /**
  867. * upnp_wps_device_stop - Stop WPS UPnP operations on an interface
  868. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  869. */
  870. static void upnp_wps_device_stop(struct upnp_wps_device_sm *sm)
  871. {
  872. if (!sm || !sm->started)
  873. return;
  874. wpa_printf(MSG_DEBUG, "WPS UPnP: Stop device");
  875. web_listener_stop(sm);
  876. upnp_wps_free_msearchreply(&sm->msearch_replies);
  877. upnp_wps_free_subscriptions(&sm->subscriptions, NULL);
  878. advertisement_state_machine_stop(sm, 1);
  879. event_send_stop_all(sm);
  880. os_free(sm->wlanevent);
  881. sm->wlanevent = NULL;
  882. os_free(sm->ip_addr_text);
  883. sm->ip_addr_text = NULL;
  884. if (sm->multicast_sd >= 0)
  885. close(sm->multicast_sd);
  886. sm->multicast_sd = -1;
  887. ssdp_listener_stop(sm);
  888. sm->started = 0;
  889. }
  890. /**
  891. * upnp_wps_device_start - Start WPS UPnP operations on an interface
  892. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  893. * @net_if: Selected network interface name
  894. * Returns: 0 on success, -1 on failure
  895. */
  896. static int upnp_wps_device_start(struct upnp_wps_device_sm *sm, char *net_if)
  897. {
  898. if (!sm || !net_if)
  899. return -1;
  900. if (sm->started)
  901. upnp_wps_device_stop(sm);
  902. sm->multicast_sd = -1;
  903. sm->ssdp_sd = -1;
  904. sm->started = 1;
  905. sm->advertise_count = 0;
  906. /* Fix up linux multicast handling */
  907. if (add_ssdp_network(net_if))
  908. goto fail;
  909. /* Determine which IP and mac address we're using */
  910. if (get_netif_info(net_if, &sm->ip_addr, &sm->ip_addr_text,
  911. sm->mac_addr)) {
  912. wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
  913. "for %s. Does it have IP address?", net_if);
  914. goto fail;
  915. }
  916. /* Listen for incoming TCP connections so that others
  917. * can fetch our "xml files" from us.
  918. */
  919. if (web_listener_start(sm))
  920. goto fail;
  921. /* Set up for receiving discovery (UDP) packets */
  922. if (ssdp_listener_start(sm))
  923. goto fail;
  924. /* Set up for sending multicast */
  925. if (ssdp_open_multicast(sm) < 0)
  926. goto fail;
  927. /*
  928. * Broadcast NOTIFY messages to let the world know we exist.
  929. * This is done via a state machine since the messages should not be
  930. * all sent out at once.
  931. */
  932. if (advertisement_state_machine_start(sm))
  933. goto fail;
  934. return 0;
  935. fail:
  936. upnp_wps_device_stop(sm);
  937. return -1;
  938. }
  939. static struct upnp_wps_device_interface *
  940. upnp_wps_get_iface(struct upnp_wps_device_sm *sm, void *priv)
  941. {
  942. struct upnp_wps_device_interface *iface;
  943. dl_list_for_each(iface, &sm->interfaces,
  944. struct upnp_wps_device_interface, list) {
  945. if (iface->priv == priv)
  946. return iface;
  947. }
  948. return NULL;
  949. }
  950. /**
  951. * upnp_wps_device_deinit - Deinitialize WPS UPnP
  952. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  953. * @priv: External context data that was used in upnp_wps_device_init() call
  954. */
  955. void upnp_wps_device_deinit(struct upnp_wps_device_sm *sm, void *priv)
  956. {
  957. struct upnp_wps_device_interface *iface;
  958. if (!sm)
  959. return;
  960. iface = upnp_wps_get_iface(sm, priv);
  961. if (iface == NULL) {
  962. wpa_printf(MSG_ERROR, "WPS UPnP: Could not find the interface "
  963. "instance to deinit");
  964. return;
  965. }
  966. wpa_printf(MSG_DEBUG, "WPS UPnP: Deinit interface instance %p", iface);
  967. if (dl_list_len(&sm->interfaces) == 1) {
  968. wpa_printf(MSG_DEBUG, "WPS UPnP: Deinitializing last instance "
  969. "- free global device instance");
  970. upnp_wps_device_stop(sm);
  971. } else
  972. upnp_wps_free_subscriptions(&sm->subscriptions,
  973. iface->wps->registrar);
  974. dl_list_del(&iface->list);
  975. if (iface->peer.wps)
  976. wps_deinit(iface->peer.wps);
  977. os_free(iface->ctx->ap_pin);
  978. os_free(iface->ctx);
  979. os_free(iface);
  980. if (dl_list_empty(&sm->interfaces)) {
  981. os_free(sm->root_dir);
  982. os_free(sm->desc_url);
  983. os_free(sm);
  984. shared_upnp_device = NULL;
  985. }
  986. }
  987. /**
  988. * upnp_wps_device_init - Initialize WPS UPnP
  989. * @ctx: callback table; we must eventually free it
  990. * @wps: Pointer to longterm WPS context
  991. * @priv: External context data that will be used in callbacks
  992. * @net_if: Selected network interface name
  993. * Returns: WPS UPnP state or %NULL on failure
  994. */
  995. struct upnp_wps_device_sm *
  996. upnp_wps_device_init(struct upnp_wps_device_ctx *ctx, struct wps_context *wps,
  997. void *priv, char *net_if)
  998. {
  999. struct upnp_wps_device_sm *sm;
  1000. struct upnp_wps_device_interface *iface;
  1001. int start = 0;
  1002. iface = os_zalloc(sizeof(*iface));
  1003. if (iface == NULL) {
  1004. os_free(ctx->ap_pin);
  1005. os_free(ctx);
  1006. return NULL;
  1007. }
  1008. wpa_printf(MSG_DEBUG, "WPS UPnP: Init interface instance %p", iface);
  1009. iface->ctx = ctx;
  1010. iface->wps = wps;
  1011. iface->priv = priv;
  1012. if (shared_upnp_device) {
  1013. wpa_printf(MSG_DEBUG, "WPS UPnP: Share existing device "
  1014. "context");
  1015. sm = shared_upnp_device;
  1016. } else {
  1017. wpa_printf(MSG_DEBUG, "WPS UPnP: Initialize device context");
  1018. sm = os_zalloc(sizeof(*sm));
  1019. if (!sm) {
  1020. wpa_printf(MSG_ERROR, "WPS UPnP: upnp_wps_device_init "
  1021. "failed");
  1022. os_free(iface);
  1023. os_free(ctx->ap_pin);
  1024. os_free(ctx);
  1025. return NULL;
  1026. }
  1027. shared_upnp_device = sm;
  1028. dl_list_init(&sm->msearch_replies);
  1029. dl_list_init(&sm->subscriptions);
  1030. dl_list_init(&sm->interfaces);
  1031. start = 1;
  1032. }
  1033. dl_list_add(&sm->interfaces, &iface->list);
  1034. if (start && upnp_wps_device_start(sm, net_if)) {
  1035. upnp_wps_device_deinit(sm, priv);
  1036. return NULL;
  1037. }
  1038. return sm;
  1039. }
  1040. /**
  1041. * upnp_wps_subscribers - Check whether there are any event subscribers
  1042. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  1043. * Returns: 0 if no subscribers, 1 if subscribers
  1044. */
  1045. int upnp_wps_subscribers(struct upnp_wps_device_sm *sm)
  1046. {
  1047. return !dl_list_empty(&sm->subscriptions);
  1048. }
  1049. int upnp_wps_set_ap_pin(struct upnp_wps_device_sm *sm, const char *ap_pin)
  1050. {
  1051. struct upnp_wps_device_interface *iface;
  1052. if (sm == NULL)
  1053. return 0;
  1054. dl_list_for_each(iface, &sm->interfaces,
  1055. struct upnp_wps_device_interface, list) {
  1056. os_free(iface->ctx->ap_pin);
  1057. if (ap_pin) {
  1058. iface->ctx->ap_pin = os_strdup(ap_pin);
  1059. if (iface->ctx->ap_pin == NULL)
  1060. return -1;
  1061. } else
  1062. iface->ctx->ap_pin = NULL;
  1063. }
  1064. return 0;
  1065. }