2 * Wi-Fi Protected Setup - External Registrar
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "wps_upnp_i.h"
26 * SSDP M-SEARCH multicast TX for WFA
28 * fetch wps_device info based on LOCATION: from SSDP NOTIFY
29 * parse wps_device info into AP entry (name, SCPD/control/eventSub URLs, etc.
31 * send notification of new AP device with wpa_msg
32 * re-send notifications with wpa_msg if ER re-started (to update wpa_gui-qt4)
33 * (also re-send SSDP M-SEARCH in this case to find new APs)
34 * parse UPnP event messages
37 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx);
41 struct wps_er_ap *next;
48 struct wps_registrar *reg;
50 char *mac_addr_text; /* mac addr of network i.f. we use */
51 u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
52 char *ip_addr_text; /* IP address of network i.f. we use */
53 unsigned ip_addr; /* IP address of network i.f. we use (host order) */
60 static void wps_er_pin_needed_cb(void *ctx, const u8 *uuid_e,
61 const struct wps_device_data *dev)
63 wpa_printf(MSG_DEBUG, "WPS ER: PIN needed");
67 static struct wps_er_ap * wps_er_ap_get(struct wps_er *er,
71 for (ap = er->ap; ap; ap = ap->next) {
72 if (ap->addr.s_addr == addr->s_addr)
79 static void wps_er_ap_free(struct wps_er *er, struct wps_er_ap *ap)
81 wpa_printf(MSG_DEBUG, "WPS ER: Removing AP entry for %s (%s)",
82 inet_ntoa(ap->addr), ap->location);
83 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
84 os_free(ap->location);
89 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx)
91 struct wps_er *er = eloop_data;
92 struct wps_er_ap *ap = user_ctx;
93 wpa_printf(MSG_DEBUG, "WPS ER: AP advertisement timed out");
94 wps_er_ap_free(er, ap);
98 static void wps_er_ap_add(struct wps_er *er, struct in_addr *addr,
99 const char *location, int max_age)
101 struct wps_er_ap *ap;
103 ap = wps_er_ap_get(er, addr);
105 /* Update advertisement timeout */
106 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
107 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
111 ap = os_zalloc(sizeof(*ap));
114 ap->location = os_strdup(location);
115 if (ap->location == NULL) {
122 ap->addr.s_addr = addr->s_addr;
123 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
125 wpa_printf(MSG_DEBUG, "WPS ER: Added AP entry for %s (%s)",
126 inet_ntoa(ap->addr), ap->location);
128 /* TODO: get device data and subscribe for events */
132 static void wps_er_ap_remove(struct wps_er *er, struct in_addr *addr)
134 struct wps_er_ap *prev = NULL, *ap = er->ap;
137 if (ap->addr.s_addr == addr->s_addr) {
139 prev->next = ap->next;
142 wps_er_ap_free(er, ap);
151 static void wps_er_ap_remove_all(struct wps_er *er)
153 struct wps_er_ap *prev, *ap;
161 wps_er_ap_free(er, prev);
166 static void wps_er_ssdp_rx(int sd, void *eloop_ctx, void *sock_ctx)
168 struct wps_er *er = eloop_ctx;
169 struct sockaddr_in addr; /* client address */
172 char buf[MULTICAST_MAX_READ], *pos, *pos2, *start;
173 int wfa = 0, byebye = 0;
175 char *location = NULL;
177 addr_len = sizeof(addr);
178 nread = recvfrom(sd, buf, sizeof(buf) - 1, 0,
179 (struct sockaddr *) &addr, &addr_len);
184 wpa_printf(MSG_DEBUG, "WPS ER: Received SSDP from %s",
185 inet_ntoa(addr.sin_addr));
186 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Received SSDP contents",
189 if (sd == er->multicast_sd) {
190 /* Reply to M-SEARCH */
191 if (os_strncmp(buf, "HTTP/1.1 200 OK", 15) != 0)
192 return; /* unexpected response header */
194 /* Unsolicited message (likely NOTIFY or M-SEARCH) */
195 if (os_strncmp(buf, "NOTIFY ", 7) != 0)
196 return; /* only process notifications */
199 for (start = buf; start && *start; start = pos) {
200 pos = os_strchr(start, '\n');
206 if (os_strstr(start, "schemas-wifialliance-org:device:"
209 if (os_strstr(start, "schemas-wifialliance-org:service:"
212 if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
214 while (*start == ' ')
217 } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
218 if (os_strstr(start, "ssdp:byebye"))
220 } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
222 while (*start == ' ')
224 pos2 = os_strstr(start, "max-age=");
228 max_age = atoi(pos2);
233 return; /* Not WPS advertisement/reply */
236 wps_er_ap_remove(er, &addr.sin_addr);
241 return; /* Unknown location */
244 return; /* No max-age reported */
246 wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
247 "(packet source: %s max-age: %d)",
248 location, inet_ntoa(addr.sin_addr), max_age);
250 wps_er_ap_add(er, &addr.sin_addr, location, max_age);
254 static void wps_er_send_ssdp_msearch(struct wps_er *er)
257 struct sockaddr_in dest;
259 msg = wpabuf_alloc(500);
264 "M-SEARCH * HTTP/1.1\r\n"
265 "HOST: 239.255.255.250:1900\r\n"
266 "MAN: \"ssdp:discover\"\r\n"
268 "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
272 os_memset(&dest, 0, sizeof(dest));
273 dest.sin_family = AF_INET;
274 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
275 dest.sin_port = htons(UPNP_MULTICAST_PORT);
277 if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
278 (struct sockaddr *) &dest, sizeof(dest)) < 0)
279 wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
280 "%d (%s)", errno, strerror(errno));
287 wps_er_init(struct wps_context *wps, const char *ifname)
290 struct wps_registrar_config rcfg;
292 er = os_zalloc(sizeof(*er));
296 er->multicast_sd = -1;
299 os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
300 os_memset(&rcfg, 0, sizeof(rcfg));
301 rcfg.pin_needed_cb = wps_er_pin_needed_cb;
304 er->reg = wps_registrar_init(wps, &rcfg);
305 if (er->reg == NULL) {
310 if (get_netif_info(ifname,
311 &er->ip_addr, &er->ip_addr_text,
312 er->mac_addr, &er->mac_addr_text)) {
313 wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
314 "for %s. Does it have IP address?", ifname);
319 if (add_ssdp_network(ifname)) {
324 er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr);
325 if (er->multicast_sd < 0) {
330 er->ssdp_sd = ssdp_listener_open();
331 if (er->ssdp_sd < 0) {
335 if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
336 wps_er_ssdp_rx, er, NULL) ||
337 eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
338 wps_er_ssdp_rx, er, NULL)) {
343 wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s "
345 er->ifname, er->ip_addr_text, er->mac_addr_text);
347 wps_er_send_ssdp_msearch(er);
353 void wps_er_deinit(struct wps_er *er)
357 wps_er_ap_remove_all(er);
358 if (er->multicast_sd >= 0) {
359 eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
360 close(er->multicast_sd);
362 if (er->ssdp_sd >= 0) {
363 eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
366 wps_registrar_deinit(er->reg);
367 os_free(er->ip_addr_text);
368 os_free(er->mac_addr_text);