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