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 "http_client.h"
23 #include "http_server.h"
27 #include "wps_upnp_i.h"
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);
38 static void wps_er_sta_timeout(void *eloop_data, void *user_ctx);
42 struct wps_er_sta *next;
46 u8 uuid[WPS_UUID_LEN];
56 struct http_client *http;
60 struct wps_er_ap *next;
62 struct wps_er_sta *sta; /* list of STAs/Enrollees using this AP */
65 struct http_client *http;
68 u8 uuid[WPS_UUID_LEN];
71 char *manufacturer_url;
72 char *model_description;
87 struct wps_credential *ap_settings;
91 struct wps_context *wps;
93 char *mac_addr_text; /* mac addr of network i.f. we use */
94 u8 mac_addr[ETH_ALEN]; /* mac addr of network i.f. we use */
95 char *ip_addr_text; /* IP address of network i.f. we use */
96 unsigned ip_addr; /* IP address of network i.f. we use (host order) */
100 struct http_server *http_srv;
102 unsigned int next_ap_id;
106 static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg);
109 static void wps_er_sta_event(struct wps_context *wps, struct wps_er_sta *sta,
110 enum wps_event event)
112 union wps_event_data data;
113 struct wps_event_er_enrollee *ev = &data.enrollee;
115 if (wps->event_cb == NULL)
118 os_memset(&data, 0, sizeof(data));
119 ev->uuid = sta->uuid;
120 ev->mac_addr = sta->addr;
121 ev->m1_received = sta->m1_received;
122 ev->config_methods = sta->config_methods;
123 ev->dev_passwd_id = sta->dev_passwd_id;
124 ev->pri_dev_type = sta->pri_dev_type;
125 ev->dev_name = sta->dev_name;
126 ev->manufacturer = sta->manufacturer;
127 ev->model_name = sta->model_name;
128 ev->model_number = sta->model_number;
129 ev->serial_number = sta->serial_number;
130 wps->event_cb(wps->cb_ctx, event, &data);
134 static struct wps_er_sta * wps_er_sta_get(struct wps_er_ap *ap, const u8 *addr)
136 struct wps_er_sta *sta = ap->sta;
138 if (os_memcmp(sta->addr, addr, ETH_ALEN) == 0)
146 static void wps_er_sta_free(struct wps_er_sta *sta)
148 wps_er_sta_event(sta->ap->er->wps, sta, WPS_EV_ER_ENROLLEE_REMOVE);
150 wps_deinit(sta->wps);
151 os_free(sta->manufacturer);
152 os_free(sta->model_name);
153 os_free(sta->model_number);
154 os_free(sta->serial_number);
155 os_free(sta->dev_name);
156 http_client_free(sta->http);
157 eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
162 static void wps_er_sta_remove_all(struct wps_er_ap *ap)
164 struct wps_er_sta *prev, *sta;
172 wps_er_sta_free(prev);
177 static struct wps_er_ap * wps_er_ap_get(struct wps_er *er,
178 struct in_addr *addr)
180 struct wps_er_ap *ap;
181 for (ap = er->ap; ap; ap = ap->next) {
182 if (ap->addr.s_addr == addr->s_addr)
189 static struct wps_er_ap * wps_er_ap_get_uuid(struct wps_er *er, const u8 *uuid)
191 struct wps_er_ap *ap;
192 for (ap = er->ap; ap; ap = ap->next) {
193 if (os_memcmp(uuid, ap->uuid, WPS_UUID_LEN) == 0)
200 static struct wps_er_ap * wps_er_ap_get_id(struct wps_er *er, unsigned int id)
202 struct wps_er_ap *ap;
203 for (ap = er->ap; ap; ap = ap->next) {
211 static void wps_er_ap_event(struct wps_context *wps, struct wps_er_ap *ap,
212 enum wps_event event)
214 union wps_event_data data;
215 struct wps_event_er_ap *evap = &data.ap;
217 if (wps->event_cb == NULL)
220 os_memset(&data, 0, sizeof(data));
221 evap->uuid = ap->uuid;
222 evap->friendly_name = ap->friendly_name;
223 evap->manufacturer = ap->manufacturer;
224 evap->manufacturer_url = ap->manufacturer_url;
225 evap->model_description = ap->model_description;
226 evap->model_name = ap->model_name;
227 evap->model_number = ap->model_number;
228 evap->model_url = ap->model_url;
229 evap->serial_number = ap->serial_number;
231 wps->event_cb(wps->cb_ctx, event, &data);
235 static void wps_er_ap_free(struct wps_er *er, struct wps_er_ap *ap)
237 /* TODO: if ap->subscribed, unsubscribe from events if the AP is still
239 wpa_printf(MSG_DEBUG, "WPS ER: Removing AP entry for %s (%s)",
240 inet_ntoa(ap->addr), ap->location);
241 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
242 wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_REMOVE);
243 os_free(ap->location);
244 http_client_free(ap->http);
248 os_free(ap->friendly_name);
249 os_free(ap->manufacturer);
250 os_free(ap->manufacturer_url);
251 os_free(ap->model_description);
252 os_free(ap->model_name);
253 os_free(ap->model_number);
254 os_free(ap->model_url);
255 os_free(ap->serial_number);
259 os_free(ap->scpd_url);
260 os_free(ap->control_url);
261 os_free(ap->event_sub_url);
263 os_free(ap->ap_settings);
265 wps_er_sta_remove_all(ap);
271 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx)
273 struct wps_er *er = eloop_data;
274 struct wps_er_ap *ap = user_ctx;
275 wpa_printf(MSG_DEBUG, "WPS ER: AP advertisement timed out");
276 wps_er_ap_free(er, ap);
280 static void wps_er_http_subscribe_cb(void *ctx, struct http_client *c,
281 enum http_client_event event)
283 struct wps_er_ap *ap = ctx;
287 wpa_printf(MSG_DEBUG, "WPS ER: Subscribed to events");
288 wps_er_ap_event(ap->er->wps, ap, WPS_EV_ER_AP_ADD);
290 case HTTP_CLIENT_FAILED:
291 case HTTP_CLIENT_INVALID_REPLY:
292 case HTTP_CLIENT_TIMEOUT:
293 wpa_printf(MSG_DEBUG, "WPS ER: Failed to subscribe to events");
296 http_client_free(ap->http);
301 static void wps_er_subscribe(struct wps_er_ap *ap)
304 struct sockaddr_in dst;
307 if (ap->event_sub_url == NULL) {
308 wpa_printf(MSG_DEBUG, "WPS ER: No eventSubURL - cannot "
313 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request - cannot "
314 "send subscribe request");
318 url = http_client_url_parse(ap->event_sub_url, &dst, &path);
320 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse eventSubURL");
324 req = wpabuf_alloc(os_strlen(ap->event_sub_url) + 1000);
330 "SUBSCRIBE %s HTTP/1.1\r\n"
332 "CALLBACK: <http://%s:%d/event/%d>\r\n"
334 "TIMEOUT: Second-%d\r\n"
336 path, inet_ntoa(dst.sin_addr), ntohs(dst.sin_port),
337 ap->er->ip_addr_text, ap->er->http_port, ap->id, 1800);
339 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Subscription request",
340 wpabuf_head(req), wpabuf_len(req));
342 ap->http = http_client_addr(&dst, req, 1000, wps_er_http_subscribe_cb,
344 if (ap->http == NULL)
349 static void wps_er_parse_device_description(struct wps_er_ap *ap,
350 struct wpabuf *reply)
352 /* Note: reply includes null termination after the buffer data */
353 const char *data = wpabuf_head(reply);
356 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Device info",
357 wpabuf_head(reply), wpabuf_len(reply));
359 ap->friendly_name = xml_get_first_item(data, "friendlyName");
360 wpa_printf(MSG_DEBUG, "WPS ER: friendlyName='%s'", ap->friendly_name);
362 ap->manufacturer = xml_get_first_item(data, "manufacturer");
363 wpa_printf(MSG_DEBUG, "WPS ER: manufacturer='%s'", ap->manufacturer);
365 ap->manufacturer_url = xml_get_first_item(data, "manufacturerURL");
366 wpa_printf(MSG_DEBUG, "WPS ER: manufacturerURL='%s'",
367 ap->manufacturer_url);
369 ap->model_description = xml_get_first_item(data, "modelDescription");
370 wpa_printf(MSG_DEBUG, "WPS ER: modelDescription='%s'",
371 ap->model_description);
373 ap->model_name = xml_get_first_item(data, "modelName");
374 wpa_printf(MSG_DEBUG, "WPS ER: modelName='%s'", ap->model_name);
376 ap->model_number = xml_get_first_item(data, "modelNumber");
377 wpa_printf(MSG_DEBUG, "WPS ER: modelNumber='%s'", ap->model_number);
379 ap->model_url = xml_get_first_item(data, "modelURL");
380 wpa_printf(MSG_DEBUG, "WPS ER: modelURL='%s'", ap->model_url);
382 ap->serial_number = xml_get_first_item(data, "serialNumber");
383 wpa_printf(MSG_DEBUG, "WPS ER: serialNumber='%s'", ap->serial_number);
385 ap->udn = xml_get_first_item(data, "UDN");
386 wpa_printf(MSG_DEBUG, "WPS ER: UDN='%s'", ap->udn);
387 pos = os_strstr(ap->udn, "uuid:");
390 uuid_str2bin(pos, ap->uuid);
393 ap->upc = xml_get_first_item(data, "UPC");
394 wpa_printf(MSG_DEBUG, "WPS ER: UPC='%s'", ap->upc);
396 ap->scpd_url = http_link_update(
397 xml_get_first_item(data, "SCPDURL"), ap->location);
398 wpa_printf(MSG_DEBUG, "WPS ER: SCPDURL='%s'", ap->scpd_url);
400 ap->control_url = http_link_update(
401 xml_get_first_item(data, "controlURL"), ap->location);
402 wpa_printf(MSG_DEBUG, "WPS ER: controlURL='%s'", ap->control_url);
404 ap->event_sub_url = http_link_update(
405 xml_get_first_item(data, "eventSubURL"), ap->location);
406 wpa_printf(MSG_DEBUG, "WPS ER: eventSubURL='%s'", ap->event_sub_url);
410 static void wps_er_http_dev_desc_cb(void *ctx, struct http_client *c,
411 enum http_client_event event)
413 struct wps_er_ap *ap = ctx;
414 struct wpabuf *reply;
419 reply = http_client_get_body(c);
422 wps_er_parse_device_description(ap, reply);
425 case HTTP_CLIENT_FAILED:
426 case HTTP_CLIENT_INVALID_REPLY:
427 case HTTP_CLIENT_TIMEOUT:
428 wpa_printf(MSG_DEBUG, "WPS ER: Failed to fetch device info");
431 http_client_free(ap->http);
434 wps_er_subscribe(ap);
438 static void wps_er_ap_add(struct wps_er *er, struct in_addr *addr,
439 const char *location, int max_age)
441 struct wps_er_ap *ap;
443 ap = wps_er_ap_get(er, addr);
445 /* Update advertisement timeout */
446 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
447 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
451 ap = os_zalloc(sizeof(*ap));
455 ap->id = ++er->next_ap_id;
456 ap->location = os_strdup(location);
457 if (ap->location == NULL) {
464 ap->addr.s_addr = addr->s_addr;
465 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
467 wpa_printf(MSG_DEBUG, "WPS ER: Added AP entry for %s (%s)",
468 inet_ntoa(ap->addr), ap->location);
470 /* Fetch device description */
471 ap->http = http_client_url(ap->location, NULL, 10000,
472 wps_er_http_dev_desc_cb, ap);
476 static void wps_er_ap_remove(struct wps_er *er, struct in_addr *addr)
478 struct wps_er_ap *prev = NULL, *ap = er->ap;
481 if (ap->addr.s_addr == addr->s_addr) {
483 prev->next = ap->next;
486 wps_er_ap_free(er, ap);
495 static void wps_er_ap_remove_all(struct wps_er *er)
497 struct wps_er_ap *prev, *ap;
505 wps_er_ap_free(er, prev);
510 static void wps_er_ssdp_rx(int sd, void *eloop_ctx, void *sock_ctx)
512 struct wps_er *er = eloop_ctx;
513 struct sockaddr_in addr; /* client address */
516 char buf[MULTICAST_MAX_READ], *pos, *pos2, *start;
517 int wfa = 0, byebye = 0;
519 char *location = NULL;
521 addr_len = sizeof(addr);
522 nread = recvfrom(sd, buf, sizeof(buf) - 1, 0,
523 (struct sockaddr *) &addr, &addr_len);
528 wpa_printf(MSG_DEBUG, "WPS ER: Received SSDP from %s",
529 inet_ntoa(addr.sin_addr));
530 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Received SSDP contents",
533 if (sd == er->multicast_sd) {
534 /* Reply to M-SEARCH */
535 if (os_strncmp(buf, "HTTP/1.1 200 OK", 15) != 0)
536 return; /* unexpected response header */
538 /* Unsolicited message (likely NOTIFY or M-SEARCH) */
539 if (os_strncmp(buf, "NOTIFY ", 7) != 0)
540 return; /* only process notifications */
543 for (start = buf; start && *start; start = pos) {
544 pos = os_strchr(start, '\n');
550 if (os_strstr(start, "schemas-wifialliance-org:device:"
553 if (os_strstr(start, "schemas-wifialliance-org:service:"
556 if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
558 while (*start == ' ')
561 } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
562 if (os_strstr(start, "ssdp:byebye"))
564 } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
566 while (*start == ' ')
568 pos2 = os_strstr(start, "max-age=");
572 max_age = atoi(pos2);
577 return; /* Not WPS advertisement/reply */
580 wps_er_ap_remove(er, &addr.sin_addr);
585 return; /* Unknown location */
588 return; /* No max-age reported */
590 wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
591 "(packet source: %s max-age: %d)",
592 location, inet_ntoa(addr.sin_addr), max_age);
594 wps_er_ap_add(er, &addr.sin_addr, location, max_age);
598 static void wps_er_send_ssdp_msearch(struct wps_er *er)
601 struct sockaddr_in dest;
603 msg = wpabuf_alloc(500);
608 "M-SEARCH * HTTP/1.1\r\n"
609 "HOST: 239.255.255.250:1900\r\n"
610 "MAN: \"ssdp:discover\"\r\n"
612 "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
616 os_memset(&dest, 0, sizeof(dest));
617 dest.sin_family = AF_INET;
618 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
619 dest.sin_port = htons(UPNP_MULTICAST_PORT);
621 if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
622 (struct sockaddr *) &dest, sizeof(dest)) < 0)
623 wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
624 "%d (%s)", errno, strerror(errno));
630 static void http_put_date(struct wpabuf *buf)
632 wpabuf_put_str(buf, "Date: ");
634 wpabuf_put_str(buf, "\r\n");
638 static void wps_er_http_resp_not_found(struct http_request *req)
641 buf = wpabuf_alloc(200);
643 http_request_deinit(req);
648 "HTTP/1.1 404 Not Found\r\n"
649 "Server: unspecified, UPnP/1.0, unspecified\r\n"
650 "Connection: close\r\n");
652 wpabuf_put_str(buf, "\r\n");
653 http_request_send_and_deinit(req, buf);
657 static void wps_er_http_resp_ok(struct http_request *req)
660 buf = wpabuf_alloc(200);
662 http_request_deinit(req);
667 "HTTP/1.1 200 OK\r\n"
668 "Server: unspecified, UPnP/1.0, unspecified\r\n"
669 "Connection: close\r\n"
670 "Content-Length: 0\r\n");
672 wpabuf_put_str(buf, "\r\n");
673 http_request_send_and_deinit(req, buf);
677 static void wps_er_sta_timeout(void *eloop_data, void *user_ctx)
679 struct wps_er_sta *sta = eloop_data;
680 wpa_printf(MSG_DEBUG, "WPS ER: STA entry timed out");
681 wps_er_sta_free(sta);
685 static struct wps_er_sta * wps_er_add_sta_data(struct wps_er_ap *ap,
687 struct wps_parse_attr *attr,
690 struct wps_er_sta *sta = wps_er_sta_get(ap, addr);
694 m1 = !probe_req && attr->msg_type && *attr->msg_type == WPS_M1;
698 * Only allow new STA entry to be added based on Probe Request
699 * or M1. This will filter out bogus events and anything that
700 * may have been ongoing at the time ER subscribed for events.
702 if (!probe_req && !m1)
705 sta = os_zalloc(sizeof(*sta));
708 os_memcpy(sta->addr, addr, ETH_ALEN);
716 sta->m1_received = 1;
718 if (attr->config_methods && (!probe_req || !sta->m1_received))
719 sta->config_methods = WPA_GET_BE16(attr->config_methods);
720 if (attr->uuid_e && (!probe_req || !sta->m1_received))
721 os_memcpy(sta->uuid, attr->uuid_e, WPS_UUID_LEN);
722 if (attr->primary_dev_type && (!probe_req || !sta->m1_received))
723 os_memcpy(sta->pri_dev_type, attr->primary_dev_type, 8);
724 if (attr->dev_password_id && (!probe_req || !sta->m1_received))
725 sta->dev_passwd_id = WPA_GET_BE16(attr->dev_password_id);
727 if (attr->manufacturer) {
728 os_free(sta->manufacturer);
729 sta->manufacturer = os_malloc(attr->manufacturer_len + 1);
730 if (sta->manufacturer) {
731 os_memcpy(sta->manufacturer, attr->manufacturer,
732 attr->manufacturer_len);
733 sta->manufacturer[attr->manufacturer_len] = '\0';
737 if (attr->model_name) {
738 os_free(sta->model_name);
739 sta->model_name = os_malloc(attr->model_name_len + 1);
740 if (sta->model_name) {
741 os_memcpy(sta->model_name, attr->model_name,
742 attr->model_name_len);
743 sta->model_name[attr->model_name_len] = '\0';
747 if (attr->model_number) {
748 os_free(sta->model_number);
749 sta->model_number = os_malloc(attr->model_number_len + 1);
750 if (sta->model_number) {
751 os_memcpy(sta->model_number, attr->model_number,
752 attr->model_number_len);
753 sta->model_number[attr->model_number_len] = '\0';
757 if (attr->serial_number) {
758 os_free(sta->serial_number);
759 sta->serial_number = os_malloc(attr->serial_number_len + 1);
760 if (sta->serial_number) {
761 os_memcpy(sta->serial_number, attr->serial_number,
762 attr->serial_number_len);
763 sta->serial_number[attr->serial_number_len] = '\0';
767 if (attr->dev_name) {
768 os_free(sta->dev_name);
769 sta->dev_name = os_malloc(attr->dev_name_len + 1);
771 os_memcpy(sta->dev_name, attr->dev_name,
773 sta->dev_name[attr->dev_name_len] = '\0';
777 eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
778 eloop_register_timeout(300, 0, wps_er_sta_timeout, sta, NULL);
781 wps_er_sta_event(ap->er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
787 static void wps_er_process_wlanevent_probe_req(struct wps_er_ap *ap,
791 struct wps_parse_attr attr;
793 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - Probe Request - from "
794 MACSTR, MAC2STR(addr));
795 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
796 "(TLVs from Probe Request)", msg);
798 if (wps_parse_msg(msg, &attr) < 0) {
799 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
800 "WLANEvent message");
804 wps_er_add_sta_data(ap, addr, &attr, 1);
808 static void wps_er_http_put_wlan_response_cb(void *ctx, struct http_client *c,
809 enum http_client_event event)
811 struct wps_er_sta *sta = ctx;
815 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse OK");
817 case HTTP_CLIENT_FAILED:
818 case HTTP_CLIENT_INVALID_REPLY:
819 case HTTP_CLIENT_TIMEOUT:
820 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse failed");
823 http_client_free(sta->http);
828 static const char *soap_prefix =
829 "<?xml version=\"1.0\"?>\n"
830 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
831 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
833 static const char *soap_postfix =
834 "</s:Body>\n</s:Envelope>\n";
835 static const char *urn_wfawlanconfig =
836 "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
838 static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
839 const char *name, const char *arg_name,
841 const struct sockaddr_in *dst,
842 char **len_ptr, char **body_ptr)
844 unsigned char *encoded;
849 encoded = base64_encode(wpabuf_head(msg), wpabuf_len(msg),
858 buf = wpabuf_alloc(1000 + encoded_len);
865 "POST %s HTTP/1.1\r\n"
867 "Content-Type: text/xml; charset=\"utf-8\"\r\n"
869 path, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
871 *len_ptr = wpabuf_put(buf, 0);
874 "SOAPACTION: \"%s#%s\"\r\n"
876 urn_wfawlanconfig, name);
878 *body_ptr = wpabuf_put(buf, 0);
880 wpabuf_put_str(buf, soap_prefix);
881 wpabuf_printf(buf, "<u:%s xmlns:u=\"", name);
882 wpabuf_put_str(buf, urn_wfawlanconfig);
883 wpabuf_put_str(buf, "\">\n");
885 wpabuf_printf(buf, "<%s>%s</%s>\n",
886 arg_name, (char *) encoded, arg_name);
894 static void wps_er_soap_end(struct wpabuf *buf, const char *name,
895 char *len_ptr, char *body_ptr)
898 wpabuf_printf(buf, "</u:%s>\n", name);
899 wpabuf_put_str(buf, soap_postfix);
900 os_snprintf(len_buf, sizeof(len_buf), "%d",
901 (int) ((char *) wpabuf_put(buf, 0) - body_ptr));
902 os_memcpy(len_ptr, len_buf, os_strlen(len_buf));
906 static void wps_er_sta_send_msg(struct wps_er_sta *sta, struct wpabuf *msg)
909 char *len_ptr, *body_ptr;
910 struct sockaddr_in dst;
914 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for STA - "
915 "ignore new request");
920 if (sta->ap->control_url == NULL) {
921 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
926 url = http_client_url_parse(sta->ap->control_url, &dst, &path);
928 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
933 buf = wps_er_soap_hdr(msg, "PutWLANResponse", "NewMessage", path, &dst,
934 &len_ptr, &body_ptr);
939 wpabuf_printf(buf, "<NewWLANEventType>%d</NewWLANEventType>\n",
940 UPNP_WPS_WLANEVENT_TYPE_EAP);
941 wpabuf_printf(buf, "<NewWLANEventMAC>" MACSTR "</NewWLANEventMAC>\n",
944 wps_er_soap_end(buf, "PutWLANResponse", len_ptr, body_ptr);
946 sta->http = http_client_addr(&dst, buf, 1000,
947 wps_er_http_put_wlan_response_cb, sta);
948 if (sta->http == NULL)
953 static void wps_er_sta_process(struct wps_er_sta *sta, struct wpabuf *msg,
954 enum wsc_op_code op_code)
956 enum wps_process_res res;
958 res = wps_process_msg(sta->wps, op_code, msg);
959 if (res == WPS_CONTINUE) {
960 struct wpabuf *next = wps_get_msg(sta->wps, &op_code);
962 wps_er_sta_send_msg(sta, next);
967 static void wps_er_sta_start(struct wps_er_sta *sta, struct wpabuf *msg)
969 struct wps_config cfg;
972 wps_deinit(sta->wps);
974 os_memset(&cfg, 0, sizeof(cfg));
975 cfg.wps = sta->ap->er->wps;
977 cfg.peer_addr = sta->addr;
979 sta->wps = wps_init(&cfg);
980 if (sta->wps == NULL)
983 sta->wps->use_cred = sta->ap->ap_settings;
985 wps_er_sta_process(sta, msg, WSC_MSG);
989 static void wps_er_process_wlanevent_eap(struct wps_er_ap *ap, const u8 *addr,
992 struct wps_parse_attr attr;
993 struct wps_er_sta *sta;
995 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - EAP - from " MACSTR,
997 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
998 "(TLVs from EAP-WSC)", msg);
1000 if (wps_parse_msg(msg, &attr) < 0) {
1001 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
1002 "WLANEvent message");
1006 sta = wps_er_add_sta_data(ap, addr, &attr, 0);
1010 if (attr.msg_type && *attr.msg_type == WPS_M1)
1011 wps_er_sta_start(sta, msg);
1012 else if (sta->wps) {
1013 enum wsc_op_code op_code = WSC_MSG;
1014 if (attr.msg_type) {
1015 switch (*attr.msg_type) {
1027 wps_er_sta_process(sta, msg, op_code);
1032 static void wps_er_process_wlanevent(struct wps_er_ap *ap,
1033 struct wpabuf *event)
1037 u8 wlan_event_mac[ETH_ALEN];
1040 wpa_hexdump(MSG_MSGDUMP, "WPS ER: Received WLANEvent",
1041 wpabuf_head(event), wpabuf_len(event));
1042 if (wpabuf_len(event) < 1 + 17) {
1043 wpa_printf(MSG_DEBUG, "WPS ER: Too short WLANEvent");
1047 data = wpabuf_mhead(event);
1048 wlan_event_type = data[0];
1049 if (hwaddr_aton((char *) data + 1, wlan_event_mac) < 0) {
1050 wpa_printf(MSG_DEBUG, "WPS ER: Invalid WLANEventMAC in "
1055 wpabuf_set(&msg, data + 1 + 17, wpabuf_len(event) - (1 + 17));
1057 switch (wlan_event_type) {
1059 wps_er_process_wlanevent_probe_req(ap, wlan_event_mac, &msg);
1062 wps_er_process_wlanevent_eap(ap, wlan_event_mac, &msg);
1065 wpa_printf(MSG_DEBUG, "WPS ER: Unknown WLANEventType %d",
1072 static void wps_er_http_event(struct wps_er *er, struct http_request *req,
1075 struct wps_er_ap *ap = wps_er_ap_get_id(er, ap_id);
1076 struct wpabuf *event;
1077 enum http_reply_code ret;
1080 wpa_printf(MSG_DEBUG, "WPS ER: HTTP event from unknown AP id "
1082 wps_er_http_resp_not_found(req);
1085 wpa_printf(MSG_MSGDUMP, "WPS ER: HTTP event from AP id %u: %s",
1086 ap_id, http_request_get_data(req));
1088 event = xml_get_base64_item(http_request_get_data(req), "WLANEvent",
1090 if (event == NULL) {
1091 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract WLANEvent "
1092 "from the event notification");
1094 * Reply with OK anyway to avoid getting unregistered from
1097 wps_er_http_resp_ok(req);
1101 wps_er_process_wlanevent(ap, event);
1104 wps_er_http_resp_ok(req);
1108 static void wps_er_http_notify(struct wps_er *er, struct http_request *req)
1110 char *uri = http_request_get_uri(req);
1112 if (os_strncmp(uri, "/event/", 7) == 0) {
1113 wps_er_http_event(er, req, atoi(uri + 7));
1115 wpa_printf(MSG_DEBUG, "WPS ER: Unknown HTTP NOTIFY for '%s'",
1117 wps_er_http_resp_not_found(req);
1122 static void wps_er_http_req(void *ctx, struct http_request *req)
1124 struct wps_er *er = ctx;
1125 struct sockaddr_in *cli = http_request_get_cli_addr(req);
1126 enum httpread_hdr_type type = http_request_get_type(req);
1129 wpa_printf(MSG_DEBUG, "WPS ER: HTTP request: '%s' (type %d) from "
1131 http_request_get_uri(req), type,
1132 inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));
1135 case HTTPREAD_HDR_TYPE_NOTIFY:
1136 wps_er_http_notify(er, req);
1139 wpa_printf(MSG_DEBUG, "WPS ER: Unsupported HTTP request type "
1141 buf = wpabuf_alloc(200);
1143 http_request_deinit(req);
1147 "HTTP/1.1 501 Unimplemented\r\n"
1148 "Connection: close\r\n");
1150 wpabuf_put_str(buf, "\r\n");
1151 http_request_send_and_deinit(req, buf);
1158 wps_er_init(struct wps_context *wps, const char *ifname)
1161 struct in_addr addr;
1163 er = os_zalloc(sizeof(*er));
1167 er->multicast_sd = -1;
1170 os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
1173 if (get_netif_info(ifname,
1174 &er->ip_addr, &er->ip_addr_text,
1175 er->mac_addr, &er->mac_addr_text)) {
1176 wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
1177 "for %s. Does it have IP address?", ifname);
1182 if (add_ssdp_network(ifname)) {
1187 er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr);
1188 if (er->multicast_sd < 0) {
1193 er->ssdp_sd = ssdp_listener_open();
1194 if (er->ssdp_sd < 0) {
1198 if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
1199 wps_er_ssdp_rx, er, NULL) ||
1200 eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
1201 wps_er_ssdp_rx, er, NULL)) {
1206 addr.s_addr = er->ip_addr;
1207 er->http_srv = http_server_init(&addr, -1, wps_er_http_req, er);
1208 if (er->http_srv == NULL) {
1212 er->http_port = http_server_get_port(er->http_srv);
1214 wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s "
1216 er->ifname, er->ip_addr_text, er->mac_addr_text);
1218 wps_er_send_ssdp_msearch(er);
1224 void wps_er_deinit(struct wps_er *er)
1228 http_server_deinit(er->http_srv);
1229 wps_er_ap_remove_all(er);
1230 if (er->multicast_sd >= 0) {
1231 eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
1232 close(er->multicast_sd);
1234 if (er->ssdp_sd >= 0) {
1235 eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
1238 os_free(er->ip_addr_text);
1239 os_free(er->mac_addr_text);
1244 static void wps_er_http_set_sel_reg_cb(void *ctx, struct http_client *c,
1245 enum http_client_event event)
1247 struct wps_er_ap *ap = ctx;
1250 case HTTP_CLIENT_OK:
1251 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar OK");
1253 case HTTP_CLIENT_FAILED:
1254 case HTTP_CLIENT_INVALID_REPLY:
1255 case HTTP_CLIENT_TIMEOUT:
1256 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar failed");
1259 http_client_free(ap->http);
1264 static void wps_er_send_set_sel_reg(struct wps_er_ap *ap, struct wpabuf *msg)
1267 char *len_ptr, *body_ptr;
1268 struct sockaddr_in dst;
1271 if (ap->control_url == NULL) {
1272 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1277 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for AP - "
1278 "ignore new request");
1282 url = http_client_url_parse(ap->control_url, &dst, &path);
1284 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1288 buf = wps_er_soap_hdr(msg, "SetSelectedRegistrar", "NewMessage", path,
1289 &dst, &len_ptr, &body_ptr);
1294 wps_er_soap_end(buf, "SetSelectedRegistrar", len_ptr, body_ptr);
1296 ap->http = http_client_addr(&dst, buf, 1000,
1297 wps_er_http_set_sel_reg_cb, ap);
1298 if (ap->http == NULL)
1303 static int wps_er_build_selected_registrar(struct wpabuf *msg, int sel_reg)
1305 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
1306 wpabuf_put_be16(msg, 1);
1307 wpabuf_put_u8(msg, !!sel_reg);
1312 static int wps_er_build_dev_password_id(struct wpabuf *msg, u16 dev_passwd_id)
1314 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
1315 wpabuf_put_be16(msg, 2);
1316 wpabuf_put_be16(msg, dev_passwd_id);
1321 static int wps_er_build_sel_reg_config_methods(struct wpabuf *msg,
1322 u16 sel_reg_config_methods)
1324 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
1325 wpabuf_put_be16(msg, 2);
1326 wpabuf_put_be16(msg, sel_reg_config_methods);
1331 void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
1332 u16 sel_reg_config_methods)
1335 struct wps_er_ap *ap;
1337 msg = wpabuf_alloc(500);
1341 if (wps_build_version(msg) ||
1342 wps_er_build_selected_registrar(msg, sel_reg) ||
1343 wps_er_build_dev_password_id(msg, dev_passwd_id) ||
1344 wps_er_build_sel_reg_config_methods(msg, sel_reg_config_methods)) {
1349 for (ap = er->ap; ap; ap = ap->next)
1350 wps_er_send_set_sel_reg(ap, msg);
1356 int wps_er_pbc(struct wps_er *er, const u8 *uuid)
1358 if (er == NULL || er->wps == NULL)
1362 * TODO: Should enable PBC mode only in a single AP based on which AP
1363 * the Enrollee (uuid) is using. Now, we may end up enabling multiple
1364 * APs in PBC mode which could result in session overlap at the
1367 if (wps_registrar_button_pushed(er->wps->registrar))
1374 static void wps_er_ap_settings_cb(void *ctx, const struct wps_credential *cred)
1376 struct wps_er_ap *ap = ctx;
1377 wpa_printf(MSG_DEBUG, "WPS ER: AP Settings received");
1378 os_free(ap->ap_settings);
1379 ap->ap_settings = os_malloc(sizeof(*cred));
1380 if (ap->ap_settings) {
1381 os_memcpy(ap->ap_settings, cred, sizeof(*cred));
1382 ap->ap_settings->cred_attr = NULL;
1385 /* TODO: send info through ctrl_iface */
1389 static void wps_er_http_put_message_cb(void *ctx, struct http_client *c,
1390 enum http_client_event event)
1392 struct wps_er_ap *ap = ctx;
1393 struct wpabuf *reply;
1397 case HTTP_CLIENT_OK:
1398 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage OK");
1399 reply = http_client_get_body(c);
1402 msg = os_zalloc(wpabuf_len(reply) + 1);
1405 os_memcpy(msg, wpabuf_head(reply), wpabuf_len(reply));
1407 case HTTP_CLIENT_FAILED:
1408 case HTTP_CLIENT_INVALID_REPLY:
1409 case HTTP_CLIENT_TIMEOUT:
1410 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage failed");
1413 http_client_free(ap->http);
1418 enum http_reply_code ret;
1419 buf = xml_get_base64_item(msg, "NewOutMessage", &ret);
1422 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1423 "NewOutMessage from PutMessage response");
1426 wps_er_ap_process(ap, buf);
1432 static void wps_er_ap_put_message(struct wps_er_ap *ap,
1433 const struct wpabuf *msg)
1436 char *len_ptr, *body_ptr;
1437 struct sockaddr_in dst;
1441 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
1442 "with the AP - cannot continue learn");
1446 if (ap->control_url == NULL) {
1447 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1451 url = http_client_url_parse(ap->control_url, &dst, &path);
1453 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1457 buf = wps_er_soap_hdr(msg, "PutMessage", "NewInMessage", path, &dst,
1458 &len_ptr, &body_ptr);
1463 wps_er_soap_end(buf, "PutMessage", len_ptr, body_ptr);
1465 ap->http = http_client_addr(&dst, buf, 10000,
1466 wps_er_http_put_message_cb, ap);
1467 if (ap->http == NULL)
1472 static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg)
1474 enum wps_process_res res;
1476 res = wps_process_msg(ap->wps, WSC_MSG, msg);
1477 if (res == WPS_CONTINUE) {
1478 enum wsc_op_code op_code;
1479 struct wpabuf *next = wps_get_msg(ap->wps, &op_code);
1481 wps_er_ap_put_message(ap, next);
1484 wpa_printf(MSG_DEBUG, "WPS ER: Failed to build "
1486 wps_deinit(ap->wps);
1490 wpa_printf(MSG_DEBUG, "WPS ER: Failed to process message from "
1491 "AP (res=%d)", res);
1492 wps_deinit(ap->wps);
1498 static void wps_er_ap_learn(struct wps_er_ap *ap, const char *dev_info)
1500 struct wpabuf *info;
1501 enum http_reply_code ret;
1502 struct wps_config cfg;
1504 wpa_printf(MSG_DEBUG, "WPS ER: Received GetDeviceInfo response (M1) "
1506 info = xml_get_base64_item(dev_info, "NewDeviceInfo", &ret);
1508 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1509 "NewDeviceInfo from GetDeviceInfo response");
1514 wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
1515 "progress with this AP");
1520 os_memset(&cfg, 0, sizeof(cfg));
1521 cfg.wps = ap->er->wps;
1523 ap->wps = wps_init(&cfg);
1524 if (ap->wps == NULL) {
1528 ap->wps->ap_settings_cb = wps_er_ap_settings_cb;
1529 ap->wps->ap_settings_cb_ctx = ap;
1531 wps_er_ap_process(ap, info);
1536 static void wps_er_http_get_dev_info_cb(void *ctx, struct http_client *c,
1537 enum http_client_event event)
1539 struct wps_er_ap *ap = ctx;
1540 struct wpabuf *reply;
1541 char *dev_info = NULL;
1544 case HTTP_CLIENT_OK:
1545 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo OK");
1546 reply = http_client_get_body(c);
1549 dev_info = os_zalloc(wpabuf_len(reply) + 1);
1550 if (dev_info == NULL)
1552 os_memcpy(dev_info, wpabuf_head(reply), wpabuf_len(reply));
1554 case HTTP_CLIENT_FAILED:
1555 case HTTP_CLIENT_INVALID_REPLY:
1556 case HTTP_CLIENT_TIMEOUT:
1557 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo failed");
1560 http_client_free(ap->http);
1564 wps_er_ap_learn(ap, dev_info);
1570 int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
1573 struct wps_er_ap *ap;
1575 char *len_ptr, *body_ptr;
1576 struct sockaddr_in dst;
1582 ap = wps_er_ap_get_uuid(er, uuid);
1584 wpa_printf(MSG_DEBUG, "WPS ER: AP not found for learn "
1588 if (ap->wps || ap->http) {
1589 wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
1590 "with the AP - cannot start learn");
1594 if (ap->control_url == NULL) {
1595 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1599 url = http_client_url_parse(ap->control_url, &dst, &path);
1601 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1605 buf = wps_er_soap_hdr(NULL, "GetDeviceInfo", NULL, path, &dst,
1606 &len_ptr, &body_ptr);
1611 wps_er_soap_end(buf, "GetDeviceInfo", len_ptr, body_ptr);
1613 ap->http = http_client_addr(&dst, buf, 10000,
1614 wps_er_http_get_dev_info_cb, ap);
1615 if (ap->http == NULL) {
1620 /* TODO: add PIN without SetSelectedRegistrar trigger to all APs */
1621 wps_registrar_add_pin(er->wps->registrar, uuid, pin, pin_len, 0);