1e28a4053SRui Paulo /*
2e28a4053SRui Paulo * Wi-Fi Protected Setup - External Registrar (SSDP)
3e28a4053SRui Paulo * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7e28a4053SRui Paulo */
8e28a4053SRui Paulo
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo
11e28a4053SRui Paulo #include "common.h"
12e28a4053SRui Paulo #include "uuid.h"
13e28a4053SRui Paulo #include "eloop.h"
14e28a4053SRui Paulo #include "wps_i.h"
15e28a4053SRui Paulo #include "wps_upnp.h"
16e28a4053SRui Paulo #include "wps_upnp_i.h"
17e28a4053SRui Paulo #include "wps_er.h"
18e28a4053SRui Paulo
19e28a4053SRui Paulo
wps_er_ssdp_rx(int sd,void * eloop_ctx,void * sock_ctx)20e28a4053SRui Paulo static void wps_er_ssdp_rx(int sd, void *eloop_ctx, void *sock_ctx)
21e28a4053SRui Paulo {
22e28a4053SRui Paulo struct wps_er *er = eloop_ctx;
23e28a4053SRui Paulo struct sockaddr_in addr; /* client address */
24e28a4053SRui Paulo socklen_t addr_len;
25e28a4053SRui Paulo int nread;
26e28a4053SRui Paulo char buf[MULTICAST_MAX_READ], *pos, *pos2, *start;
27e28a4053SRui Paulo int wfa = 0, byebye = 0;
28e28a4053SRui Paulo int max_age = -1;
29e28a4053SRui Paulo char *location = NULL;
30e28a4053SRui Paulo u8 uuid[WPS_UUID_LEN];
31e28a4053SRui Paulo
32e28a4053SRui Paulo addr_len = sizeof(addr);
33e28a4053SRui Paulo nread = recvfrom(sd, buf, sizeof(buf) - 1, 0,
34e28a4053SRui Paulo (struct sockaddr *) &addr, &addr_len);
35e28a4053SRui Paulo if (nread <= 0)
36e28a4053SRui Paulo return;
37e28a4053SRui Paulo buf[nread] = '\0';
38f05cddf9SRui Paulo if (er->filter_addr.s_addr &&
39f05cddf9SRui Paulo er->filter_addr.s_addr != addr.sin_addr.s_addr)
40f05cddf9SRui Paulo return;
41e28a4053SRui Paulo
42e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS ER: Received SSDP from %s",
43e28a4053SRui Paulo inet_ntoa(addr.sin_addr));
44e28a4053SRui Paulo wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Received SSDP contents",
45e28a4053SRui Paulo (u8 *) buf, nread);
46e28a4053SRui Paulo
47e28a4053SRui Paulo if (sd == er->multicast_sd) {
48e28a4053SRui Paulo /* Reply to M-SEARCH */
49e28a4053SRui Paulo if (os_strncmp(buf, "HTTP/1.1 200 OK", 15) != 0)
50e28a4053SRui Paulo return; /* unexpected response header */
51e28a4053SRui Paulo } else {
52e28a4053SRui Paulo /* Unsolicited message (likely NOTIFY or M-SEARCH) */
53e28a4053SRui Paulo if (os_strncmp(buf, "NOTIFY ", 7) != 0)
54e28a4053SRui Paulo return; /* only process notifications */
55e28a4053SRui Paulo }
56e28a4053SRui Paulo
57e28a4053SRui Paulo os_memset(uuid, 0, sizeof(uuid));
58e28a4053SRui Paulo
59e28a4053SRui Paulo for (start = buf; start && *start; start = pos) {
60e28a4053SRui Paulo pos = os_strchr(start, '\n');
61e28a4053SRui Paulo if (pos) {
62e28a4053SRui Paulo if (pos[-1] == '\r')
63e28a4053SRui Paulo pos[-1] = '\0';
64e28a4053SRui Paulo *pos++ = '\0';
65e28a4053SRui Paulo }
66e28a4053SRui Paulo if (os_strstr(start, "schemas-wifialliance-org:device:"
67e28a4053SRui Paulo "WFADevice:1"))
68e28a4053SRui Paulo wfa = 1;
69e28a4053SRui Paulo if (os_strstr(start, "schemas-wifialliance-org:service:"
70e28a4053SRui Paulo "WFAWLANConfig:1"))
71e28a4053SRui Paulo wfa = 1;
72e28a4053SRui Paulo if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
73e28a4053SRui Paulo start += 9;
74e28a4053SRui Paulo while (*start == ' ')
75e28a4053SRui Paulo start++;
76e28a4053SRui Paulo location = start;
77e28a4053SRui Paulo } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
78e28a4053SRui Paulo if (os_strstr(start, "ssdp:byebye"))
79e28a4053SRui Paulo byebye = 1;
80e28a4053SRui Paulo } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
81*325151a3SRui Paulo start += 14;
82e28a4053SRui Paulo pos2 = os_strstr(start, "max-age=");
83e28a4053SRui Paulo if (pos2 == NULL)
84e28a4053SRui Paulo continue;
85e28a4053SRui Paulo pos2 += 8;
86e28a4053SRui Paulo max_age = atoi(pos2);
87e28a4053SRui Paulo } else if (os_strncasecmp(start, "USN:", 4) == 0) {
88e28a4053SRui Paulo start += 4;
89e28a4053SRui Paulo pos2 = os_strstr(start, "uuid:");
90e28a4053SRui Paulo if (pos2) {
91e28a4053SRui Paulo pos2 += 5;
92e28a4053SRui Paulo while (*pos2 == ' ')
93e28a4053SRui Paulo pos2++;
94e28a4053SRui Paulo if (uuid_str2bin(pos2, uuid) < 0) {
95e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS ER: "
96e28a4053SRui Paulo "Invalid UUID in USN: %s",
97e28a4053SRui Paulo pos2);
98e28a4053SRui Paulo return;
99e28a4053SRui Paulo }
100e28a4053SRui Paulo }
101e28a4053SRui Paulo }
102e28a4053SRui Paulo }
103e28a4053SRui Paulo
104e28a4053SRui Paulo if (!wfa)
105e28a4053SRui Paulo return; /* Not WPS advertisement/reply */
106e28a4053SRui Paulo
107e28a4053SRui Paulo if (byebye) {
108f05cddf9SRui Paulo wps_er_ap_cache_settings(er, &addr.sin_addr);
109e28a4053SRui Paulo wps_er_ap_remove(er, &addr.sin_addr);
110e28a4053SRui Paulo return;
111e28a4053SRui Paulo }
112e28a4053SRui Paulo
113e28a4053SRui Paulo if (!location)
114e28a4053SRui Paulo return; /* Unknown location */
115e28a4053SRui Paulo
116e28a4053SRui Paulo if (max_age < 1)
117e28a4053SRui Paulo return; /* No max-age reported */
118e28a4053SRui Paulo
119e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
120e28a4053SRui Paulo "(packet source: %s max-age: %d)",
121e28a4053SRui Paulo location, inet_ntoa(addr.sin_addr), max_age);
122e28a4053SRui Paulo
123e28a4053SRui Paulo wps_er_ap_add(er, uuid, &addr.sin_addr, location, max_age);
124e28a4053SRui Paulo }
125e28a4053SRui Paulo
126e28a4053SRui Paulo
wps_er_send_ssdp_msearch(struct wps_er * er)127e28a4053SRui Paulo void wps_er_send_ssdp_msearch(struct wps_er *er)
128e28a4053SRui Paulo {
129e28a4053SRui Paulo struct wpabuf *msg;
130e28a4053SRui Paulo struct sockaddr_in dest;
131e28a4053SRui Paulo
132e28a4053SRui Paulo msg = wpabuf_alloc(500);
133e28a4053SRui Paulo if (msg == NULL)
134e28a4053SRui Paulo return;
135e28a4053SRui Paulo
136e28a4053SRui Paulo wpabuf_put_str(msg,
137e28a4053SRui Paulo "M-SEARCH * HTTP/1.1\r\n"
138e28a4053SRui Paulo "HOST: 239.255.255.250:1900\r\n"
139e28a4053SRui Paulo "MAN: \"ssdp:discover\"\r\n"
140e28a4053SRui Paulo "MX: 3\r\n"
141e28a4053SRui Paulo "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
142e28a4053SRui Paulo "\r\n"
143e28a4053SRui Paulo "\r\n");
144e28a4053SRui Paulo
145e28a4053SRui Paulo os_memset(&dest, 0, sizeof(dest));
146e28a4053SRui Paulo dest.sin_family = AF_INET;
147e28a4053SRui Paulo dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
148e28a4053SRui Paulo dest.sin_port = htons(UPNP_MULTICAST_PORT);
149e28a4053SRui Paulo
150e28a4053SRui Paulo if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
151e28a4053SRui Paulo (struct sockaddr *) &dest, sizeof(dest)) < 0)
152e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
153e28a4053SRui Paulo "%d (%s)", errno, strerror(errno));
154e28a4053SRui Paulo
155e28a4053SRui Paulo wpabuf_free(msg);
156e28a4053SRui Paulo }
157e28a4053SRui Paulo
158e28a4053SRui Paulo
wps_er_ssdp_init(struct wps_er * er)159e28a4053SRui Paulo int wps_er_ssdp_init(struct wps_er *er)
160e28a4053SRui Paulo {
161f05cddf9SRui Paulo if (add_ssdp_network(er->ifname)) {
162f05cddf9SRui Paulo wpa_printf(MSG_INFO, "WPS ER: Failed to add routing entry for "
163f05cddf9SRui Paulo "SSDP");
164e28a4053SRui Paulo return -1;
165f05cddf9SRui Paulo }
166e28a4053SRui Paulo
1675b9c547cSRui Paulo er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr,
1685b9c547cSRui Paulo er->forced_ifname ?
1695b9c547cSRui Paulo er->ifname : NULL);
170f05cddf9SRui Paulo if (er->multicast_sd < 0) {
171f05cddf9SRui Paulo wpa_printf(MSG_INFO, "WPS ER: Failed to open multicast socket "
172f05cddf9SRui Paulo "for SSDP");
173e28a4053SRui Paulo return -1;
174f05cddf9SRui Paulo }
175e28a4053SRui Paulo
176e28a4053SRui Paulo er->ssdp_sd = ssdp_listener_open();
177f05cddf9SRui Paulo if (er->ssdp_sd < 0) {
178f05cddf9SRui Paulo wpa_printf(MSG_INFO, "WPS ER: Failed to open SSDP listener "
179f05cddf9SRui Paulo "socket");
180e28a4053SRui Paulo return -1;
181f05cddf9SRui Paulo }
182e28a4053SRui Paulo
183e28a4053SRui Paulo if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
184e28a4053SRui Paulo wps_er_ssdp_rx, er, NULL) ||
185e28a4053SRui Paulo eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
186e28a4053SRui Paulo wps_er_ssdp_rx, er, NULL))
187e28a4053SRui Paulo return -1;
188e28a4053SRui Paulo
189e28a4053SRui Paulo wps_er_send_ssdp_msearch(er);
190e28a4053SRui Paulo
191e28a4053SRui Paulo return 0;
192e28a4053SRui Paulo }
193e28a4053SRui Paulo
194e28a4053SRui Paulo
wps_er_ssdp_deinit(struct wps_er * er)195e28a4053SRui Paulo void wps_er_ssdp_deinit(struct wps_er *er)
196e28a4053SRui Paulo {
197e28a4053SRui Paulo if (er->multicast_sd >= 0) {
198e28a4053SRui Paulo eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
199e28a4053SRui Paulo close(er->multicast_sd);
200e28a4053SRui Paulo }
201e28a4053SRui Paulo if (er->ssdp_sd >= 0) {
202e28a4053SRui Paulo eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
203e28a4053SRui Paulo close(er->ssdp_sd);
204e28a4053SRui Paulo }
205e28a4053SRui Paulo }
206