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