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