wps_upnp.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. if (date == NULL)
  216. return;
  217. wpabuf_printf(buf, "%s, %02d %s %d %02d:%02d:%02d GMT",
  218. &weekday_str[date->tm_wday * 4], date->tm_mday,
  219. &month_str[date->tm_mon * 4], date->tm_year + 1900,
  220. date->tm_hour, date->tm_min, date->tm_sec);
  221. }
  222. /***************************************************************************
  223. * UUIDs (unique identifiers)
  224. *
  225. * These are supposed to be unique in all the world.
  226. * Sometimes permanent ones are used, sometimes temporary ones
  227. * based on random numbers... there are different rules for valid content
  228. * of different types.
  229. * Each uuid is 16 bytes long.
  230. **************************************************************************/
  231. /* uuid_make -- construct a random UUID
  232. * The UPnP documents don't seem to offer any guidelines as to which method to
  233. * use for constructing UUIDs for subscriptions. Presumably any method from
  234. * rfc4122 is good enough; I've chosen random number method.
  235. */
  236. static int uuid_make(u8 uuid[UUID_LEN])
  237. {
  238. if (os_get_random(uuid, UUID_LEN) < 0)
  239. return -1;
  240. /* Replace certain bits as specified in rfc4122 or X.667 */
  241. uuid[6] &= 0x0f; uuid[6] |= (4 << 4); /* version 4 == random gen */
  242. uuid[8] &= 0x3f; uuid[8] |= 0x80;
  243. return 0;
  244. }
  245. /*
  246. * Subscriber address handling.
  247. * Since a subscriber may have an arbitrary number of addresses, we have to
  248. * add a bunch of code to handle them.
  249. *
  250. * Addresses are passed in text, and MAY be domain names instead of the (usual
  251. * and expected) dotted IP addresses. Resolving domain names consumes a lot of
  252. * resources. Worse, we are currently using the standard Linux getaddrinfo()
  253. * which will block the entire program until complete or timeout! The proper
  254. * solution would be to use the "ares" library or similar with more state
  255. * machine steps etc. or just disable domain name resolution by setting
  256. * NO_DOMAIN_NAME_RESOLUTION to 1 at top of this file.
  257. */
  258. /* subscr_addr_delete -- delete single unlinked subscriber address
  259. * (be sure to unlink first if need be)
  260. */
  261. void subscr_addr_delete(struct subscr_addr *a)
  262. {
  263. /*
  264. * Note: do NOT free domain_and_port or path because they point to
  265. * memory within the allocation of "a".
  266. */
  267. os_free(a);
  268. }
  269. /* subscr_addr_free_all -- unlink and delete list of subscriber addresses. */
  270. static void subscr_addr_free_all(struct subscription *s)
  271. {
  272. struct subscr_addr *a, *tmp;
  273. dl_list_for_each_safe(a, tmp, &s->addr_list, struct subscr_addr, list)
  274. {
  275. dl_list_del(&a->list);
  276. subscr_addr_delete(a);
  277. }
  278. }
  279. /* subscr_addr_add_url -- add address(es) for one url to subscription */
  280. static void subscr_addr_add_url(struct subscription *s, const char *url,
  281. size_t url_len)
  282. {
  283. int alloc_len;
  284. char *scratch_mem = NULL;
  285. char *mem;
  286. char *host;
  287. char *delim;
  288. char *path;
  289. int port = 80; /* port to send to (default is port 80) */
  290. struct addrinfo hints;
  291. struct addrinfo *result = NULL;
  292. struct addrinfo *rp;
  293. int rerr;
  294. size_t host_len, path_len;
  295. /* url MUST begin with http: */
  296. if (url_len < 7 || os_strncasecmp(url, "http://", 7))
  297. goto fail;
  298. url += 7;
  299. url_len -= 7;
  300. /* Make a copy of the string to allow modification during parsing */
  301. scratch_mem = dup_binstr(url, url_len);
  302. if (scratch_mem == NULL)
  303. goto fail;
  304. wpa_printf(MSG_DEBUG, "WPS UPnP: Adding URL '%s'", scratch_mem);
  305. host = scratch_mem;
  306. path = os_strchr(host, '/');
  307. if (path)
  308. *path++ = '\0'; /* null terminate host */
  309. /* Process and remove optional port component */
  310. delim = os_strchr(host, ':');
  311. if (delim) {
  312. *delim = '\0'; /* null terminate host name for now */
  313. if (isdigit(delim[1]))
  314. port = atol(delim + 1);
  315. }
  316. /*
  317. * getaddrinfo does the right thing with dotted decimal notations, or
  318. * will resolve domain names. Resolving domain names will unfortunately
  319. * hang the entire program until it is resolved or it times out
  320. * internal to getaddrinfo; fortunately we think that the use of actual
  321. * domain names (vs. dotted decimal notations) should be uncommon.
  322. */
  323. os_memset(&hints, 0, sizeof(struct addrinfo));
  324. hints.ai_family = AF_INET; /* IPv4 */
  325. hints.ai_socktype = SOCK_STREAM;
  326. #if NO_DOMAIN_NAME_RESOLUTION
  327. /* Suppress domain name resolutions that would halt
  328. * the program for periods of time
  329. */
  330. hints.ai_flags = AI_NUMERICHOST;
  331. #else
  332. /* Allow domain name resolution. */
  333. hints.ai_flags = 0;
  334. #endif
  335. hints.ai_protocol = 0; /* Any protocol? */
  336. rerr = getaddrinfo(host, NULL /* fill in port ourselves */,
  337. &hints, &result);
  338. if (rerr) {
  339. wpa_printf(MSG_INFO, "WPS UPnP: Resolve error %d (%s) on: %s",
  340. rerr, gai_strerror(rerr), host);
  341. goto fail;
  342. }
  343. if (delim)
  344. *delim = ':'; /* Restore port */
  345. host_len = os_strlen(host);
  346. path_len = path ? os_strlen(path) : 0;
  347. alloc_len = host_len + 1 + 1 + path_len + 1;
  348. for (rp = result; rp; rp = rp->ai_next) {
  349. struct subscr_addr *a;
  350. /* Limit no. of address to avoid denial of service attack */
  351. if (dl_list_len(&s->addr_list) >= MAX_ADDR_PER_SUBSCRIPTION) {
  352. wpa_printf(MSG_INFO, "WPS UPnP: subscr_addr_add_url: "
  353. "Ignoring excessive addresses");
  354. break;
  355. }
  356. a = os_zalloc(sizeof(*a) + alloc_len);
  357. if (a == NULL)
  358. break;
  359. mem = (char *) (a + 1);
  360. a->domain_and_port = mem;
  361. os_memcpy(mem, host, host_len);
  362. mem += host_len + 1;
  363. a->path = mem;
  364. if (path == NULL || path[0] != '/')
  365. *mem++ = '/';
  366. if (path)
  367. os_memcpy(mem, path, path_len);
  368. os_memcpy(&a->saddr, rp->ai_addr, sizeof(a->saddr));
  369. a->saddr.sin_port = htons(port);
  370. dl_list_add(&s->addr_list, &a->list);
  371. }
  372. fail:
  373. if (result)
  374. freeaddrinfo(result);
  375. os_free(scratch_mem);
  376. }
  377. /* subscr_addr_list_create -- create list from urls in string.
  378. * Each url is enclosed by angle brackets.
  379. */
  380. static void subscr_addr_list_create(struct subscription *s,
  381. const char *url_list)
  382. {
  383. const char *end;
  384. wpa_printf(MSG_DEBUG, "WPS UPnP: Parsing URL list '%s'", url_list);
  385. for (;;) {
  386. while (*url_list == ' ' || *url_list == '\t')
  387. url_list++;
  388. if (*url_list != '<')
  389. break;
  390. url_list++;
  391. end = os_strchr(url_list, '>');
  392. if (end == NULL)
  393. break;
  394. subscr_addr_add_url(s, url_list, end - url_list);
  395. url_list = end + 1;
  396. }
  397. }
  398. static void wpabuf_put_property(struct wpabuf *buf, const char *name,
  399. const char *value)
  400. {
  401. wpabuf_put_str(buf, "<e:property>");
  402. wpabuf_printf(buf, "<%s>", name);
  403. if (value)
  404. wpabuf_put_str(buf, value);
  405. wpabuf_printf(buf, "</%s>", name);
  406. wpabuf_put_str(buf, "</e:property>\n");
  407. }
  408. /**
  409. * upnp_wps_device_send_event - Queue event messages for subscribers
  410. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  411. *
  412. * This function queues the last WLANEvent to be sent for all currently
  413. * subscribed UPnP control points. sm->wlanevent must have been set with the
  414. * encoded data before calling this function.
  415. */
  416. static void upnp_wps_device_send_event(struct upnp_wps_device_sm *sm)
  417. {
  418. /* Enqueue event message for all subscribers */
  419. struct wpabuf *buf; /* holds event message */
  420. int buf_size = 0;
  421. struct subscription *s, *tmp;
  422. /* Actually, utf-8 is the default, but it doesn't hurt to specify it */
  423. const char *format_head =
  424. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  425. "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
  426. const char *format_tail = "</e:propertyset>\n";
  427. struct os_reltime now;
  428. if (dl_list_empty(&sm->subscriptions)) {
  429. /* optimize */
  430. return;
  431. }
  432. if (os_get_reltime(&now) == 0) {
  433. if (now.sec != sm->last_event_sec) {
  434. sm->last_event_sec = now.sec;
  435. sm->num_events_in_sec = 1;
  436. } else {
  437. sm->num_events_in_sec++;
  438. /*
  439. * In theory, this should apply to all WLANEvent
  440. * notifications, but EAP messages are of much higher
  441. * priority and Probe Request notifications should not
  442. * be allowed to drop EAP messages, so only throttle
  443. * Probe Request notifications.
  444. */
  445. if (sm->num_events_in_sec > MAX_EVENTS_PER_SEC &&
  446. sm->wlanevent_type ==
  447. UPNP_WPS_WLANEVENT_TYPE_PROBE) {
  448. wpa_printf(MSG_DEBUG, "WPS UPnP: Throttle "
  449. "event notifications (%u seen "
  450. "during one second)",
  451. sm->num_events_in_sec);
  452. return;
  453. }
  454. }
  455. }
  456. /* Determine buffer size needed first */
  457. buf_size += os_strlen(format_head);
  458. buf_size += 50 + 2 * os_strlen("WLANEvent");
  459. if (sm->wlanevent)
  460. buf_size += os_strlen(sm->wlanevent);
  461. buf_size += os_strlen(format_tail);
  462. buf = wpabuf_alloc(buf_size);
  463. if (buf == NULL)
  464. return;
  465. wpabuf_put_str(buf, format_head);
  466. wpabuf_put_property(buf, "WLANEvent", sm->wlanevent);
  467. wpabuf_put_str(buf, format_tail);
  468. wpa_printf(MSG_MSGDUMP, "WPS UPnP: WLANEvent message:\n%s",
  469. (char *) wpabuf_head(buf));
  470. dl_list_for_each_safe(s, tmp, &sm->subscriptions, struct subscription,
  471. list) {
  472. event_add(s, buf,
  473. sm->wlanevent_type == UPNP_WPS_WLANEVENT_TYPE_PROBE);
  474. }
  475. wpabuf_free(buf);
  476. }
  477. /*
  478. * Event subscription (subscriber machines register with us to receive event
  479. * messages).
  480. * This is the result of an incoming HTTP over TCP SUBSCRIBE request.
  481. */
  482. /* subscription_destroy -- destroy an unlinked subscription
  483. * Be sure to unlink first if necessary.
  484. */
  485. void subscription_destroy(struct subscription *s)
  486. {
  487. struct upnp_wps_device_interface *iface;
  488. wpa_printf(MSG_DEBUG, "WPS UPnP: Destroy subscription %p", s);
  489. subscr_addr_free_all(s);
  490. event_delete_all(s);
  491. dl_list_for_each(iface, &s->sm->interfaces,
  492. struct upnp_wps_device_interface, list)
  493. upnp_er_remove_notification(iface->wps->registrar, s);
  494. os_free(s);
  495. }
  496. /* subscription_list_age -- remove expired subscriptions */
  497. static void subscription_list_age(struct upnp_wps_device_sm *sm, time_t now)
  498. {
  499. struct subscription *s, *tmp;
  500. dl_list_for_each_safe(s, tmp, &sm->subscriptions,
  501. struct subscription, list) {
  502. if (s->timeout_time > now)
  503. break;
  504. wpa_printf(MSG_DEBUG, "WPS UPnP: Removing aged subscription");
  505. dl_list_del(&s->list);
  506. subscription_destroy(s);
  507. }
  508. }
  509. /* subscription_find -- return existing subscription matching uuid, if any
  510. * returns NULL if not found
  511. */
  512. struct subscription * subscription_find(struct upnp_wps_device_sm *sm,
  513. const u8 uuid[UUID_LEN])
  514. {
  515. struct subscription *s;
  516. dl_list_for_each(s, &sm->subscriptions, struct subscription, list) {
  517. if (os_memcmp(s->uuid, uuid, UUID_LEN) == 0)
  518. return s; /* Found match */
  519. }
  520. return NULL;
  521. }
  522. static struct wpabuf * build_fake_wsc_ack(void)
  523. {
  524. struct wpabuf *msg = wpabuf_alloc(100);
  525. if (msg == NULL)
  526. return NULL;
  527. wpabuf_put_u8(msg, UPNP_WPS_WLANEVENT_TYPE_EAP);
  528. wpabuf_put_str(msg, "00:00:00:00:00:00");
  529. if (wps_build_version(msg) ||
  530. wps_build_msg_type(msg, WPS_WSC_ACK)) {
  531. wpabuf_free(msg);
  532. return NULL;
  533. }
  534. /* Enrollee Nonce */
  535. wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
  536. wpabuf_put_be16(msg, WPS_NONCE_LEN);
  537. wpabuf_put(msg, WPS_NONCE_LEN);
  538. /* Registrar Nonce */
  539. wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
  540. wpabuf_put_be16(msg, WPS_NONCE_LEN);
  541. wpabuf_put(msg, WPS_NONCE_LEN);
  542. if (wps_build_wfa_ext(msg, 0, NULL, 0)) {
  543. wpabuf_free(msg);
  544. return NULL;
  545. }
  546. return msg;
  547. }
  548. /* subscription_first_event -- send format/queue event that is automatically
  549. * sent on a new subscription.
  550. */
  551. static int subscription_first_event(struct subscription *s)
  552. {
  553. /*
  554. * Actually, utf-8 is the default, but it doesn't hurt to specify it.
  555. *
  556. * APStatus is apparently a bit set,
  557. * 0x1 = configuration change (but is always set?)
  558. * 0x10 = ap is locked
  559. *
  560. * Per UPnP spec, we send out the last value of each variable, even
  561. * for WLANEvent, whatever it was.
  562. */
  563. char *wlan_event;
  564. struct wpabuf *buf;
  565. int ap_status = 1; /* TODO: add 0x10 if access point is locked */
  566. const char *head =
  567. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  568. "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
  569. const char *tail = "</e:propertyset>\n";
  570. char txt[10];
  571. int ret;
  572. if (s->sm->wlanevent == NULL) {
  573. /*
  574. * There has been no events before the subscription. However,
  575. * UPnP device architecture specification requires all the
  576. * evented variables to be included, so generate a dummy event
  577. * for this particular case using a WSC_ACK and all-zeros
  578. * nonces. The ER (UPnP control point) will ignore this, but at
  579. * least it will learn that WLANEvent variable will be used in
  580. * event notifications in the future.
  581. */
  582. struct wpabuf *msg;
  583. wpa_printf(MSG_DEBUG, "WPS UPnP: Use a fake WSC_ACK as the "
  584. "initial WLANEvent");
  585. msg = build_fake_wsc_ack();
  586. if (msg) {
  587. s->sm->wlanevent = (char *)
  588. base64_encode(wpabuf_head(msg),
  589. wpabuf_len(msg), NULL);
  590. wpabuf_free(msg);
  591. }
  592. }
  593. wlan_event = s->sm->wlanevent;
  594. if (wlan_event == NULL || *wlan_event == '\0') {
  595. wpa_printf(MSG_DEBUG, "WPS UPnP: WLANEvent not known for "
  596. "initial event message");
  597. wlan_event = "";
  598. }
  599. buf = wpabuf_alloc(500 + os_strlen(wlan_event));
  600. if (buf == NULL)
  601. return -1;
  602. wpabuf_put_str(buf, head);
  603. wpabuf_put_property(buf, "STAStatus", "1");
  604. os_snprintf(txt, sizeof(txt), "%d", ap_status);
  605. wpabuf_put_property(buf, "APStatus", txt);
  606. if (*wlan_event)
  607. wpabuf_put_property(buf, "WLANEvent", wlan_event);
  608. wpabuf_put_str(buf, tail);
  609. ret = event_add(s, buf, 0);
  610. if (ret) {
  611. wpabuf_free(buf);
  612. return ret;
  613. }
  614. wpabuf_free(buf);
  615. return 0;
  616. }
  617. /**
  618. * subscription_start - Remember a UPnP control point to send events to.
  619. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  620. * @callback_urls: Callback URLs
  621. * Returns: %NULL on error, or pointer to new subscription structure.
  622. */
  623. struct subscription * subscription_start(struct upnp_wps_device_sm *sm,
  624. const char *callback_urls)
  625. {
  626. struct subscription *s;
  627. time_t now = time(NULL);
  628. time_t expire = now + UPNP_SUBSCRIBE_SEC;
  629. char str[80];
  630. /* Get rid of expired subscriptions so we have room */
  631. subscription_list_age(sm, now);
  632. /* If too many subscriptions, remove oldest */
  633. if (dl_list_len(&sm->subscriptions) >= MAX_SUBSCRIPTIONS) {
  634. s = dl_list_first(&sm->subscriptions, struct subscription,
  635. list);
  636. if (s) {
  637. wpa_printf(MSG_INFO,
  638. "WPS UPnP: Too many subscriptions, trashing oldest");
  639. dl_list_del(&s->list);
  640. subscription_destroy(s);
  641. }
  642. }
  643. s = os_zalloc(sizeof(*s));
  644. if (s == NULL)
  645. return NULL;
  646. dl_list_init(&s->addr_list);
  647. dl_list_init(&s->event_queue);
  648. s->sm = sm;
  649. s->timeout_time = expire;
  650. if (uuid_make(s->uuid) < 0) {
  651. subscription_destroy(s);
  652. return NULL;
  653. }
  654. subscr_addr_list_create(s, callback_urls);
  655. if (dl_list_empty(&s->addr_list)) {
  656. wpa_printf(MSG_DEBUG, "WPS UPnP: No valid callback URLs in "
  657. "'%s' - drop subscription", callback_urls);
  658. subscription_destroy(s);
  659. return NULL;
  660. }
  661. /* Add to end of list, since it has the highest expiration time */
  662. dl_list_add_tail(&sm->subscriptions, &s->list);
  663. /* Queue up immediate event message (our last event)
  664. * as required by UPnP spec.
  665. */
  666. if (subscription_first_event(s)) {
  667. wpa_printf(MSG_INFO, "WPS UPnP: Dropping subscriber due to "
  668. "event backlog");
  669. dl_list_del(&s->list);
  670. subscription_destroy(s);
  671. return NULL;
  672. }
  673. uuid_bin2str(s->uuid, str, sizeof(str));
  674. wpa_printf(MSG_DEBUG,
  675. "WPS UPnP: Subscription %p (SID %s) started with %s",
  676. s, str, callback_urls);
  677. /* Schedule sending this */
  678. event_send_all_later(sm);
  679. return s;
  680. }
  681. /* subscription_renew -- find subscription and reset timeout */
  682. struct subscription * subscription_renew(struct upnp_wps_device_sm *sm,
  683. const u8 uuid[UUID_LEN])
  684. {
  685. time_t now = time(NULL);
  686. time_t expire = now + UPNP_SUBSCRIBE_SEC;
  687. struct subscription *s = subscription_find(sm, uuid);
  688. if (s == NULL)
  689. return NULL;
  690. wpa_printf(MSG_DEBUG, "WPS UPnP: Subscription renewed");
  691. dl_list_del(&s->list);
  692. s->timeout_time = expire;
  693. /* add back to end of list, since it now has highest expiry */
  694. dl_list_add_tail(&sm->subscriptions, &s->list);
  695. return s;
  696. }
  697. /**
  698. * upnp_wps_device_send_wlan_event - Event notification
  699. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  700. * @from_mac_addr: Source (Enrollee) MAC address for the event
  701. * @ev_type: Event type
  702. * @msg: Event data
  703. * Returns: 0 on success, -1 on failure
  704. *
  705. * Tell external Registrars (UPnP control points) that something happened. In
  706. * particular, events include WPS messages from clients that are proxied to
  707. * external Registrars.
  708. */
  709. int upnp_wps_device_send_wlan_event(struct upnp_wps_device_sm *sm,
  710. const u8 from_mac_addr[ETH_ALEN],
  711. enum upnp_wps_wlanevent_type ev_type,
  712. const struct wpabuf *msg)
  713. {
  714. int ret = -1;
  715. char type[2];
  716. const u8 *mac = from_mac_addr;
  717. char mac_text[18];
  718. u8 *raw = NULL;
  719. size_t raw_len;
  720. char *val;
  721. size_t val_len;
  722. int pos = 0;
  723. if (!sm)
  724. goto fail;
  725. os_snprintf(type, sizeof(type), "%1u", ev_type);
  726. raw_len = 1 + 17 + (msg ? wpabuf_len(msg) : 0);
  727. raw = os_zalloc(raw_len);
  728. if (!raw)
  729. goto fail;
  730. *(raw + pos) = (u8) ev_type;
  731. pos += 1;
  732. os_snprintf(mac_text, sizeof(mac_text), MACSTR, MAC2STR(mac));
  733. wpa_printf(MSG_DEBUG, "WPS UPnP: Proxying WLANEvent from %s",
  734. mac_text);
  735. os_memcpy(raw + pos, mac_text, 17);
  736. pos += 17;
  737. if (msg) {
  738. os_memcpy(raw + pos, wpabuf_head(msg), wpabuf_len(msg));
  739. pos += wpabuf_len(msg);
  740. }
  741. raw_len = pos;
  742. val = (char *) base64_encode(raw, raw_len, &val_len);
  743. if (val == NULL)
  744. goto fail;
  745. os_free(sm->wlanevent);
  746. sm->wlanevent = val;
  747. sm->wlanevent_type = ev_type;
  748. upnp_wps_device_send_event(sm);
  749. ret = 0;
  750. fail:
  751. os_free(raw);
  752. return ret;
  753. }
  754. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  755. #include <sys/sysctl.h>
  756. #include <net/route.h>
  757. #include <net/if_dl.h>
  758. static int eth_get(const char *device, u8 ea[ETH_ALEN])
  759. {
  760. struct if_msghdr *ifm;
  761. struct sockaddr_dl *sdl;
  762. u_char *p, *buf;
  763. size_t len;
  764. int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
  765. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
  766. return -1;
  767. if ((buf = os_malloc(len)) == NULL)
  768. return -1;
  769. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  770. os_free(buf);
  771. return -1;
  772. }
  773. for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
  774. ifm = (struct if_msghdr *)p;
  775. sdl = (struct sockaddr_dl *)(ifm + 1);
  776. if (ifm->ifm_type != RTM_IFINFO ||
  777. (ifm->ifm_addrs & RTA_IFP) == 0)
  778. continue;
  779. if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
  780. os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
  781. continue;
  782. os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
  783. break;
  784. }
  785. os_free(buf);
  786. if (p >= buf + len) {
  787. errno = ESRCH;
  788. return -1;
  789. }
  790. return 0;
  791. }
  792. #endif /* __FreeBSD__ */
  793. /**
  794. * get_netif_info - Get hw and IP addresses for network device
  795. * @net_if: Selected network interface name
  796. * @ip_addr: Buffer for returning IP address in network byte order
  797. * @ip_addr_text: Buffer for returning a pointer to allocated IP address text
  798. * @mac: Buffer for returning MAC address
  799. * Returns: 0 on success, -1 on failure
  800. */
  801. int get_netif_info(const char *net_if, unsigned *ip_addr, char **ip_addr_text,
  802. u8 mac[ETH_ALEN])
  803. {
  804. struct ifreq req;
  805. int sock = -1;
  806. struct sockaddr_in *addr;
  807. struct in_addr in_addr;
  808. *ip_addr_text = os_zalloc(16);
  809. if (*ip_addr_text == NULL)
  810. goto fail;
  811. sock = socket(AF_INET, SOCK_DGRAM, 0);
  812. if (sock < 0)
  813. goto fail;
  814. os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
  815. if (ioctl(sock, SIOCGIFADDR, &req) < 0) {
  816. wpa_printf(MSG_ERROR, "WPS UPnP: SIOCGIFADDR failed: %d (%s)",
  817. errno, strerror(errno));
  818. goto fail;
  819. }
  820. addr = (void *) &req.ifr_addr;
  821. *ip_addr = addr->sin_addr.s_addr;
  822. in_addr.s_addr = *ip_addr;
  823. os_snprintf(*ip_addr_text, 16, "%s", inet_ntoa(in_addr));
  824. #ifdef __linux__
  825. os_strlcpy(req.ifr_name, net_if, sizeof(req.ifr_name));
  826. if (ioctl(sock, SIOCGIFHWADDR, &req) < 0) {
  827. wpa_printf(MSG_ERROR, "WPS UPnP: SIOCGIFHWADDR failed: "
  828. "%d (%s)", errno, strerror(errno));
  829. goto fail;
  830. }
  831. os_memcpy(mac, req.ifr_addr.sa_data, 6);
  832. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  833. if (eth_get(net_if, mac) < 0) {
  834. wpa_printf(MSG_ERROR, "WPS UPnP: Failed to get MAC address");
  835. goto fail;
  836. }
  837. #else
  838. #error MAC address fetch not implemented
  839. #endif
  840. close(sock);
  841. return 0;
  842. fail:
  843. if (sock >= 0)
  844. close(sock);
  845. os_free(*ip_addr_text);
  846. *ip_addr_text = NULL;
  847. return -1;
  848. }
  849. static void upnp_wps_free_msearchreply(struct dl_list *head)
  850. {
  851. struct advertisement_state_machine *a, *tmp;
  852. dl_list_for_each_safe(a, tmp, head, struct advertisement_state_machine,
  853. list)
  854. msearchreply_state_machine_stop(a);
  855. }
  856. static void upnp_wps_free_subscriptions(struct dl_list *head,
  857. struct wps_registrar *reg)
  858. {
  859. struct subscription *s, *tmp;
  860. dl_list_for_each_safe(s, tmp, head, struct subscription, list) {
  861. if (reg && s->reg != reg)
  862. continue;
  863. dl_list_del(&s->list);
  864. subscription_destroy(s);
  865. }
  866. }
  867. /**
  868. * upnp_wps_device_stop - Stop WPS UPnP operations on an interface
  869. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  870. */
  871. static void upnp_wps_device_stop(struct upnp_wps_device_sm *sm)
  872. {
  873. if (!sm || !sm->started)
  874. return;
  875. wpa_printf(MSG_DEBUG, "WPS UPnP: Stop device");
  876. web_listener_stop(sm);
  877. ssdp_listener_stop(sm);
  878. upnp_wps_free_msearchreply(&sm->msearch_replies);
  879. upnp_wps_free_subscriptions(&sm->subscriptions, NULL);
  880. advertisement_state_machine_stop(sm, 1);
  881. event_send_stop_all(sm);
  882. os_free(sm->wlanevent);
  883. sm->wlanevent = NULL;
  884. os_free(sm->ip_addr_text);
  885. sm->ip_addr_text = NULL;
  886. if (sm->multicast_sd >= 0)
  887. close(sm->multicast_sd);
  888. sm->multicast_sd = -1;
  889. sm->started = 0;
  890. }
  891. /**
  892. * upnp_wps_device_start - Start WPS UPnP operations on an interface
  893. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  894. * @net_if: Selected network interface name
  895. * Returns: 0 on success, -1 on failure
  896. */
  897. static int upnp_wps_device_start(struct upnp_wps_device_sm *sm, char *net_if)
  898. {
  899. if (!sm || !net_if)
  900. return -1;
  901. if (sm->started)
  902. upnp_wps_device_stop(sm);
  903. sm->multicast_sd = -1;
  904. sm->ssdp_sd = -1;
  905. sm->started = 1;
  906. sm->advertise_count = 0;
  907. /* Fix up linux multicast handling */
  908. if (add_ssdp_network(net_if))
  909. goto fail;
  910. /* Determine which IP and mac address we're using */
  911. if (get_netif_info(net_if, &sm->ip_addr, &sm->ip_addr_text,
  912. sm->mac_addr)) {
  913. wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
  914. "for %s. Does it have IP address?", net_if);
  915. goto fail;
  916. }
  917. /* Listen for incoming TCP connections so that others
  918. * can fetch our "xml files" from us.
  919. */
  920. if (web_listener_start(sm))
  921. goto fail;
  922. /* Set up for receiving discovery (UDP) packets */
  923. if (ssdp_listener_start(sm))
  924. goto fail;
  925. /* Set up for sending multicast */
  926. if (ssdp_open_multicast(sm) < 0)
  927. goto fail;
  928. /*
  929. * Broadcast NOTIFY messages to let the world know we exist.
  930. * This is done via a state machine since the messages should not be
  931. * all sent out at once.
  932. */
  933. if (advertisement_state_machine_start(sm))
  934. goto fail;
  935. return 0;
  936. fail:
  937. upnp_wps_device_stop(sm);
  938. return -1;
  939. }
  940. static struct upnp_wps_device_interface *
  941. upnp_wps_get_iface(struct upnp_wps_device_sm *sm, void *priv)
  942. {
  943. struct upnp_wps_device_interface *iface;
  944. dl_list_for_each(iface, &sm->interfaces,
  945. struct upnp_wps_device_interface, list) {
  946. if (iface->priv == priv)
  947. return iface;
  948. }
  949. return NULL;
  950. }
  951. /**
  952. * upnp_wps_device_deinit - Deinitialize WPS UPnP
  953. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  954. * @priv: External context data that was used in upnp_wps_device_init() call
  955. */
  956. void upnp_wps_device_deinit(struct upnp_wps_device_sm *sm, void *priv)
  957. {
  958. struct upnp_wps_device_interface *iface;
  959. if (!sm)
  960. return;
  961. iface = upnp_wps_get_iface(sm, priv);
  962. if (iface == NULL) {
  963. wpa_printf(MSG_ERROR, "WPS UPnP: Could not find the interface "
  964. "instance to deinit");
  965. return;
  966. }
  967. wpa_printf(MSG_DEBUG, "WPS UPnP: Deinit interface instance %p", iface);
  968. if (dl_list_len(&sm->interfaces) == 1) {
  969. wpa_printf(MSG_DEBUG, "WPS UPnP: Deinitializing last instance "
  970. "- free global device instance");
  971. upnp_wps_device_stop(sm);
  972. } else
  973. upnp_wps_free_subscriptions(&sm->subscriptions,
  974. iface->wps->registrar);
  975. dl_list_del(&iface->list);
  976. if (iface->peer.wps)
  977. wps_deinit(iface->peer.wps);
  978. os_free(iface->ctx->ap_pin);
  979. os_free(iface->ctx);
  980. os_free(iface);
  981. if (dl_list_empty(&sm->interfaces)) {
  982. os_free(sm->root_dir);
  983. os_free(sm->desc_url);
  984. os_free(sm);
  985. shared_upnp_device = NULL;
  986. }
  987. }
  988. /**
  989. * upnp_wps_device_init - Initialize WPS UPnP
  990. * @ctx: callback table; we must eventually free it
  991. * @wps: Pointer to longterm WPS context
  992. * @priv: External context data that will be used in callbacks
  993. * @net_if: Selected network interface name
  994. * Returns: WPS UPnP state or %NULL on failure
  995. */
  996. struct upnp_wps_device_sm *
  997. upnp_wps_device_init(struct upnp_wps_device_ctx *ctx, struct wps_context *wps,
  998. void *priv, char *net_if)
  999. {
  1000. struct upnp_wps_device_sm *sm;
  1001. struct upnp_wps_device_interface *iface;
  1002. int start = 0;
  1003. iface = os_zalloc(sizeof(*iface));
  1004. if (iface == NULL) {
  1005. os_free(ctx->ap_pin);
  1006. os_free(ctx);
  1007. return NULL;
  1008. }
  1009. wpa_printf(MSG_DEBUG, "WPS UPnP: Init interface instance %p", iface);
  1010. iface->ctx = ctx;
  1011. iface->wps = wps;
  1012. iface->priv = priv;
  1013. if (shared_upnp_device) {
  1014. wpa_printf(MSG_DEBUG, "WPS UPnP: Share existing device "
  1015. "context");
  1016. sm = shared_upnp_device;
  1017. } else {
  1018. wpa_printf(MSG_DEBUG, "WPS UPnP: Initialize device context");
  1019. sm = os_zalloc(sizeof(*sm));
  1020. if (!sm) {
  1021. wpa_printf(MSG_ERROR, "WPS UPnP: upnp_wps_device_init "
  1022. "failed");
  1023. os_free(iface);
  1024. os_free(ctx->ap_pin);
  1025. os_free(ctx);
  1026. return NULL;
  1027. }
  1028. shared_upnp_device = sm;
  1029. dl_list_init(&sm->msearch_replies);
  1030. dl_list_init(&sm->subscriptions);
  1031. dl_list_init(&sm->interfaces);
  1032. start = 1;
  1033. }
  1034. dl_list_add(&sm->interfaces, &iface->list);
  1035. if (start && upnp_wps_device_start(sm, net_if)) {
  1036. upnp_wps_device_deinit(sm, priv);
  1037. return NULL;
  1038. }
  1039. return sm;
  1040. }
  1041. /**
  1042. * upnp_wps_subscribers - Check whether there are any event subscribers
  1043. * @sm: WPS UPnP state machine from upnp_wps_device_init()
  1044. * Returns: 0 if no subscribers, 1 if subscribers
  1045. */
  1046. int upnp_wps_subscribers(struct upnp_wps_device_sm *sm)
  1047. {
  1048. return !dl_list_empty(&sm->subscriptions);
  1049. }
  1050. int upnp_wps_set_ap_pin(struct upnp_wps_device_sm *sm, const char *ap_pin)
  1051. {
  1052. struct upnp_wps_device_interface *iface;
  1053. if (sm == NULL)
  1054. return 0;
  1055. dl_list_for_each(iface, &sm->interfaces,
  1056. struct upnp_wps_device_interface, list) {
  1057. os_free(iface->ctx->ap_pin);
  1058. if (ap_pin) {
  1059. iface->ctx->ap_pin = os_strdup(ap_pin);
  1060. if (iface->ctx->ap_pin == NULL)
  1061. return -1;
  1062. } else
  1063. iface->ctx->ap_pin = NULL;
  1064. }
  1065. return 0;
  1066. }