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_unlink(struct wps_er_sta *sta)
164 struct wps_er_sta *prev, *tmp;
165 struct wps_er_ap *ap = sta->ap;
171 prev->next = sta->next;
182 static void wps_er_sta_remove_all(struct wps_er_ap *ap)
184 struct wps_er_sta *prev, *sta;
192 wps_er_sta_free(prev);
197 static struct wps_er_ap * wps_er_ap_get(struct wps_er *er,
198 struct in_addr *addr)
200 struct wps_er_ap *ap;
201 for (ap = er->ap; ap; ap = ap->next) {
202 if (ap->addr.s_addr == addr->s_addr)
209 static struct wps_er_ap * wps_er_ap_get_uuid(struct wps_er *er, const u8 *uuid)
211 struct wps_er_ap *ap;
212 for (ap = er->ap; ap; ap = ap->next) {
213 if (os_memcmp(uuid, ap->uuid, WPS_UUID_LEN) == 0)
220 static struct wps_er_ap * wps_er_ap_get_id(struct wps_er *er, unsigned int id)
222 struct wps_er_ap *ap;
223 for (ap = er->ap; ap; ap = ap->next) {
231 static void wps_er_ap_event(struct wps_context *wps, struct wps_er_ap *ap,
232 enum wps_event event)
234 union wps_event_data data;
235 struct wps_event_er_ap *evap = &data.ap;
237 if (wps->event_cb == NULL)
240 os_memset(&data, 0, sizeof(data));
241 evap->uuid = ap->uuid;
242 evap->friendly_name = ap->friendly_name;
243 evap->manufacturer = ap->manufacturer;
244 evap->manufacturer_url = ap->manufacturer_url;
245 evap->model_description = ap->model_description;
246 evap->model_name = ap->model_name;
247 evap->model_number = ap->model_number;
248 evap->model_url = ap->model_url;
249 evap->serial_number = ap->serial_number;
251 wps->event_cb(wps->cb_ctx, event, &data);
255 static void wps_er_ap_free(struct wps_er *er, struct wps_er_ap *ap)
257 /* TODO: if ap->subscribed, unsubscribe from events if the AP is still
259 wpa_printf(MSG_DEBUG, "WPS ER: Removing AP entry for %s (%s)",
260 inet_ntoa(ap->addr), ap->location);
261 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
262 wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_REMOVE);
263 os_free(ap->location);
264 http_client_free(ap->http);
268 os_free(ap->friendly_name);
269 os_free(ap->manufacturer);
270 os_free(ap->manufacturer_url);
271 os_free(ap->model_description);
272 os_free(ap->model_name);
273 os_free(ap->model_number);
274 os_free(ap->model_url);
275 os_free(ap->serial_number);
279 os_free(ap->scpd_url);
280 os_free(ap->control_url);
281 os_free(ap->event_sub_url);
283 os_free(ap->ap_settings);
285 wps_er_sta_remove_all(ap);
291 static void wps_er_ap_unlink(struct wps_er *er, struct wps_er_ap *ap)
293 struct wps_er_ap *prev, *tmp;
299 prev->next = ap->next;
309 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx)
311 struct wps_er *er = eloop_data;
312 struct wps_er_ap *ap = user_ctx;
313 wpa_printf(MSG_DEBUG, "WPS ER: AP advertisement timed out");
314 wps_er_ap_unlink(er, ap);
315 wps_er_ap_free(er, ap);
319 static void wps_er_http_subscribe_cb(void *ctx, struct http_client *c,
320 enum http_client_event event)
322 struct wps_er_ap *ap = ctx;
326 wpa_printf(MSG_DEBUG, "WPS ER: Subscribed to events");
327 wps_er_ap_event(ap->er->wps, ap, WPS_EV_ER_AP_ADD);
329 case HTTP_CLIENT_FAILED:
330 case HTTP_CLIENT_INVALID_REPLY:
331 case HTTP_CLIENT_TIMEOUT:
332 wpa_printf(MSG_DEBUG, "WPS ER: Failed to subscribe to events");
335 http_client_free(ap->http);
340 static void wps_er_subscribe(struct wps_er_ap *ap)
343 struct sockaddr_in dst;
346 if (ap->event_sub_url == NULL) {
347 wpa_printf(MSG_DEBUG, "WPS ER: No eventSubURL - cannot "
352 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request - cannot "
353 "send subscribe request");
357 url = http_client_url_parse(ap->event_sub_url, &dst, &path);
359 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse eventSubURL");
363 req = wpabuf_alloc(os_strlen(ap->event_sub_url) + 1000);
369 "SUBSCRIBE %s HTTP/1.1\r\n"
371 "CALLBACK: <http://%s:%d/event/%d>\r\n"
373 "TIMEOUT: Second-%d\r\n"
375 path, inet_ntoa(dst.sin_addr), ntohs(dst.sin_port),
376 ap->er->ip_addr_text, ap->er->http_port, ap->id, 1800);
378 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Subscription request",
379 wpabuf_head(req), wpabuf_len(req));
381 ap->http = http_client_addr(&dst, req, 1000, wps_er_http_subscribe_cb,
383 if (ap->http == NULL)
388 static void wps_er_parse_device_description(struct wps_er_ap *ap,
389 struct wpabuf *reply)
391 /* Note: reply includes null termination after the buffer data */
392 const char *data = wpabuf_head(reply);
395 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Device info",
396 wpabuf_head(reply), wpabuf_len(reply));
398 ap->friendly_name = xml_get_first_item(data, "friendlyName");
399 wpa_printf(MSG_DEBUG, "WPS ER: friendlyName='%s'", ap->friendly_name);
401 ap->manufacturer = xml_get_first_item(data, "manufacturer");
402 wpa_printf(MSG_DEBUG, "WPS ER: manufacturer='%s'", ap->manufacturer);
404 ap->manufacturer_url = xml_get_first_item(data, "manufacturerURL");
405 wpa_printf(MSG_DEBUG, "WPS ER: manufacturerURL='%s'",
406 ap->manufacturer_url);
408 ap->model_description = xml_get_first_item(data, "modelDescription");
409 wpa_printf(MSG_DEBUG, "WPS ER: modelDescription='%s'",
410 ap->model_description);
412 ap->model_name = xml_get_first_item(data, "modelName");
413 wpa_printf(MSG_DEBUG, "WPS ER: modelName='%s'", ap->model_name);
415 ap->model_number = xml_get_first_item(data, "modelNumber");
416 wpa_printf(MSG_DEBUG, "WPS ER: modelNumber='%s'", ap->model_number);
418 ap->model_url = xml_get_first_item(data, "modelURL");
419 wpa_printf(MSG_DEBUG, "WPS ER: modelURL='%s'", ap->model_url);
421 ap->serial_number = xml_get_first_item(data, "serialNumber");
422 wpa_printf(MSG_DEBUG, "WPS ER: serialNumber='%s'", ap->serial_number);
424 ap->udn = xml_get_first_item(data, "UDN");
425 wpa_printf(MSG_DEBUG, "WPS ER: UDN='%s'", ap->udn);
426 pos = os_strstr(ap->udn, "uuid:");
429 uuid_str2bin(pos, ap->uuid);
432 ap->upc = xml_get_first_item(data, "UPC");
433 wpa_printf(MSG_DEBUG, "WPS ER: UPC='%s'", ap->upc);
435 ap->scpd_url = http_link_update(
436 xml_get_first_item(data, "SCPDURL"), ap->location);
437 wpa_printf(MSG_DEBUG, "WPS ER: SCPDURL='%s'", ap->scpd_url);
439 ap->control_url = http_link_update(
440 xml_get_first_item(data, "controlURL"), ap->location);
441 wpa_printf(MSG_DEBUG, "WPS ER: controlURL='%s'", ap->control_url);
443 ap->event_sub_url = http_link_update(
444 xml_get_first_item(data, "eventSubURL"), ap->location);
445 wpa_printf(MSG_DEBUG, "WPS ER: eventSubURL='%s'", ap->event_sub_url);
449 static void wps_er_http_dev_desc_cb(void *ctx, struct http_client *c,
450 enum http_client_event event)
452 struct wps_er_ap *ap = ctx;
453 struct wpabuf *reply;
458 reply = http_client_get_body(c);
461 wps_er_parse_device_description(ap, reply);
464 case HTTP_CLIENT_FAILED:
465 case HTTP_CLIENT_INVALID_REPLY:
466 case HTTP_CLIENT_TIMEOUT:
467 wpa_printf(MSG_DEBUG, "WPS ER: Failed to fetch device info");
470 http_client_free(ap->http);
473 wps_er_subscribe(ap);
477 static void wps_er_ap_add(struct wps_er *er, struct in_addr *addr,
478 const char *location, int max_age)
480 struct wps_er_ap *ap;
482 ap = wps_er_ap_get(er, addr);
484 /* Update advertisement timeout */
485 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
486 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
490 ap = os_zalloc(sizeof(*ap));
494 ap->id = ++er->next_ap_id;
495 ap->location = os_strdup(location);
496 if (ap->location == NULL) {
503 ap->addr.s_addr = addr->s_addr;
504 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
506 wpa_printf(MSG_DEBUG, "WPS ER: Added AP entry for %s (%s)",
507 inet_ntoa(ap->addr), ap->location);
509 /* Fetch device description */
510 ap->http = http_client_url(ap->location, NULL, 10000,
511 wps_er_http_dev_desc_cb, ap);
515 static void wps_er_ap_remove(struct wps_er *er, struct in_addr *addr)
517 struct wps_er_ap *prev = NULL, *ap = er->ap;
520 if (ap->addr.s_addr == addr->s_addr) {
522 prev->next = ap->next;
525 wps_er_ap_free(er, ap);
534 static void wps_er_ap_remove_all(struct wps_er *er)
536 struct wps_er_ap *prev, *ap;
544 wps_er_ap_free(er, prev);
549 static void wps_er_ssdp_rx(int sd, void *eloop_ctx, void *sock_ctx)
551 struct wps_er *er = eloop_ctx;
552 struct sockaddr_in addr; /* client address */
555 char buf[MULTICAST_MAX_READ], *pos, *pos2, *start;
556 int wfa = 0, byebye = 0;
558 char *location = NULL;
560 addr_len = sizeof(addr);
561 nread = recvfrom(sd, buf, sizeof(buf) - 1, 0,
562 (struct sockaddr *) &addr, &addr_len);
567 wpa_printf(MSG_DEBUG, "WPS ER: Received SSDP from %s",
568 inet_ntoa(addr.sin_addr));
569 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Received SSDP contents",
572 if (sd == er->multicast_sd) {
573 /* Reply to M-SEARCH */
574 if (os_strncmp(buf, "HTTP/1.1 200 OK", 15) != 0)
575 return; /* unexpected response header */
577 /* Unsolicited message (likely NOTIFY or M-SEARCH) */
578 if (os_strncmp(buf, "NOTIFY ", 7) != 0)
579 return; /* only process notifications */
582 for (start = buf; start && *start; start = pos) {
583 pos = os_strchr(start, '\n');
589 if (os_strstr(start, "schemas-wifialliance-org:device:"
592 if (os_strstr(start, "schemas-wifialliance-org:service:"
595 if (os_strncasecmp(start, "LOCATION:", 9) == 0) {
597 while (*start == ' ')
600 } else if (os_strncasecmp(start, "NTS:", 4) == 0) {
601 if (os_strstr(start, "ssdp:byebye"))
603 } else if (os_strncasecmp(start, "CACHE-CONTROL:", 14) == 0) {
605 while (*start == ' ')
607 pos2 = os_strstr(start, "max-age=");
611 max_age = atoi(pos2);
616 return; /* Not WPS advertisement/reply */
619 wps_er_ap_remove(er, &addr.sin_addr);
624 return; /* Unknown location */
627 return; /* No max-age reported */
629 wpa_printf(MSG_DEBUG, "WPS ER: AP discovered: %s "
630 "(packet source: %s max-age: %d)",
631 location, inet_ntoa(addr.sin_addr), max_age);
633 wps_er_ap_add(er, &addr.sin_addr, location, max_age);
637 static void wps_er_send_ssdp_msearch(struct wps_er *er)
640 struct sockaddr_in dest;
642 msg = wpabuf_alloc(500);
647 "M-SEARCH * HTTP/1.1\r\n"
648 "HOST: 239.255.255.250:1900\r\n"
649 "MAN: \"ssdp:discover\"\r\n"
651 "ST: urn:schemas-wifialliance-org:device:WFADevice:1"
655 os_memset(&dest, 0, sizeof(dest));
656 dest.sin_family = AF_INET;
657 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
658 dest.sin_port = htons(UPNP_MULTICAST_PORT);
660 if (sendto(er->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
661 (struct sockaddr *) &dest, sizeof(dest)) < 0)
662 wpa_printf(MSG_DEBUG, "WPS ER: M-SEARCH sendto failed: "
663 "%d (%s)", errno, strerror(errno));
669 static void http_put_date(struct wpabuf *buf)
671 wpabuf_put_str(buf, "Date: ");
673 wpabuf_put_str(buf, "\r\n");
677 static void wps_er_http_resp_not_found(struct http_request *req)
680 buf = wpabuf_alloc(200);
682 http_request_deinit(req);
687 "HTTP/1.1 404 Not Found\r\n"
688 "Server: unspecified, UPnP/1.0, unspecified\r\n"
689 "Connection: close\r\n");
691 wpabuf_put_str(buf, "\r\n");
692 http_request_send_and_deinit(req, buf);
696 static void wps_er_http_resp_ok(struct http_request *req)
699 buf = wpabuf_alloc(200);
701 http_request_deinit(req);
706 "HTTP/1.1 200 OK\r\n"
707 "Server: unspecified, UPnP/1.0, unspecified\r\n"
708 "Connection: close\r\n"
709 "Content-Length: 0\r\n");
711 wpabuf_put_str(buf, "\r\n");
712 http_request_send_and_deinit(req, buf);
716 static void wps_er_sta_timeout(void *eloop_data, void *user_ctx)
718 struct wps_er_sta *sta = eloop_data;
719 wpa_printf(MSG_DEBUG, "WPS ER: STA entry timed out");
720 wps_er_sta_unlink(sta);
721 wps_er_sta_free(sta);
725 static struct wps_er_sta * wps_er_add_sta_data(struct wps_er_ap *ap,
727 struct wps_parse_attr *attr,
730 struct wps_er_sta *sta = wps_er_sta_get(ap, addr);
734 m1 = !probe_req && attr->msg_type && *attr->msg_type == WPS_M1;
738 * Only allow new STA entry to be added based on Probe Request
739 * or M1. This will filter out bogus events and anything that
740 * may have been ongoing at the time ER subscribed for events.
742 if (!probe_req && !m1)
745 sta = os_zalloc(sizeof(*sta));
748 os_memcpy(sta->addr, addr, ETH_ALEN);
756 sta->m1_received = 1;
758 if (attr->config_methods && (!probe_req || !sta->m1_received))
759 sta->config_methods = WPA_GET_BE16(attr->config_methods);
760 if (attr->uuid_e && (!probe_req || !sta->m1_received))
761 os_memcpy(sta->uuid, attr->uuid_e, WPS_UUID_LEN);
762 if (attr->primary_dev_type && (!probe_req || !sta->m1_received))
763 os_memcpy(sta->pri_dev_type, attr->primary_dev_type, 8);
764 if (attr->dev_password_id && (!probe_req || !sta->m1_received))
765 sta->dev_passwd_id = WPA_GET_BE16(attr->dev_password_id);
767 if (attr->manufacturer) {
768 os_free(sta->manufacturer);
769 sta->manufacturer = os_malloc(attr->manufacturer_len + 1);
770 if (sta->manufacturer) {
771 os_memcpy(sta->manufacturer, attr->manufacturer,
772 attr->manufacturer_len);
773 sta->manufacturer[attr->manufacturer_len] = '\0';
777 if (attr->model_name) {
778 os_free(sta->model_name);
779 sta->model_name = os_malloc(attr->model_name_len + 1);
780 if (sta->model_name) {
781 os_memcpy(sta->model_name, attr->model_name,
782 attr->model_name_len);
783 sta->model_name[attr->model_name_len] = '\0';
787 if (attr->model_number) {
788 os_free(sta->model_number);
789 sta->model_number = os_malloc(attr->model_number_len + 1);
790 if (sta->model_number) {
791 os_memcpy(sta->model_number, attr->model_number,
792 attr->model_number_len);
793 sta->model_number[attr->model_number_len] = '\0';
797 if (attr->serial_number) {
798 os_free(sta->serial_number);
799 sta->serial_number = os_malloc(attr->serial_number_len + 1);
800 if (sta->serial_number) {
801 os_memcpy(sta->serial_number, attr->serial_number,
802 attr->serial_number_len);
803 sta->serial_number[attr->serial_number_len] = '\0';
807 if (attr->dev_name) {
808 os_free(sta->dev_name);
809 sta->dev_name = os_malloc(attr->dev_name_len + 1);
811 os_memcpy(sta->dev_name, attr->dev_name,
813 sta->dev_name[attr->dev_name_len] = '\0';
817 eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
818 eloop_register_timeout(300, 0, wps_er_sta_timeout, sta, NULL);
821 wps_er_sta_event(ap->er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
827 static void wps_er_process_wlanevent_probe_req(struct wps_er_ap *ap,
831 struct wps_parse_attr attr;
833 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - Probe Request - from "
834 MACSTR, MAC2STR(addr));
835 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
836 "(TLVs from Probe Request)", msg);
838 if (wps_parse_msg(msg, &attr) < 0) {
839 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
840 "WLANEvent message");
844 wps_er_add_sta_data(ap, addr, &attr, 1);
848 static void wps_er_http_put_wlan_response_cb(void *ctx, struct http_client *c,
849 enum http_client_event event)
851 struct wps_er_sta *sta = ctx;
855 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse OK");
857 case HTTP_CLIENT_FAILED:
858 case HTTP_CLIENT_INVALID_REPLY:
859 case HTTP_CLIENT_TIMEOUT:
860 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse failed");
863 http_client_free(sta->http);
868 static const char *soap_prefix =
869 "<?xml version=\"1.0\"?>\n"
870 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
871 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
873 static const char *soap_postfix =
874 "</s:Body>\n</s:Envelope>\n";
875 static const char *urn_wfawlanconfig =
876 "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
878 static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
879 const char *name, const char *arg_name,
881 const struct sockaddr_in *dst,
882 char **len_ptr, char **body_ptr)
884 unsigned char *encoded;
889 encoded = base64_encode(wpabuf_head(msg), wpabuf_len(msg),
898 buf = wpabuf_alloc(1000 + encoded_len);
905 "POST %s HTTP/1.1\r\n"
907 "Content-Type: text/xml; charset=\"utf-8\"\r\n"
909 path, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
911 *len_ptr = wpabuf_put(buf, 0);
914 "SOAPACTION: \"%s#%s\"\r\n"
916 urn_wfawlanconfig, name);
918 *body_ptr = wpabuf_put(buf, 0);
920 wpabuf_put_str(buf, soap_prefix);
921 wpabuf_printf(buf, "<u:%s xmlns:u=\"", name);
922 wpabuf_put_str(buf, urn_wfawlanconfig);
923 wpabuf_put_str(buf, "\">\n");
925 wpabuf_printf(buf, "<%s>%s</%s>\n",
926 arg_name, (char *) encoded, arg_name);
934 static void wps_er_soap_end(struct wpabuf *buf, const char *name,
935 char *len_ptr, char *body_ptr)
938 wpabuf_printf(buf, "</u:%s>\n", name);
939 wpabuf_put_str(buf, soap_postfix);
940 os_snprintf(len_buf, sizeof(len_buf), "%d",
941 (int) ((char *) wpabuf_put(buf, 0) - body_ptr));
942 os_memcpy(len_ptr, len_buf, os_strlen(len_buf));
946 static void wps_er_sta_send_msg(struct wps_er_sta *sta, struct wpabuf *msg)
949 char *len_ptr, *body_ptr;
950 struct sockaddr_in dst;
954 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for STA - "
955 "ignore new request");
960 if (sta->ap->control_url == NULL) {
961 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
966 url = http_client_url_parse(sta->ap->control_url, &dst, &path);
968 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
973 buf = wps_er_soap_hdr(msg, "PutWLANResponse", "NewMessage", path, &dst,
974 &len_ptr, &body_ptr);
979 wpabuf_printf(buf, "<NewWLANEventType>%d</NewWLANEventType>\n",
980 UPNP_WPS_WLANEVENT_TYPE_EAP);
981 wpabuf_printf(buf, "<NewWLANEventMAC>" MACSTR "</NewWLANEventMAC>\n",
984 wps_er_soap_end(buf, "PutWLANResponse", len_ptr, body_ptr);
986 sta->http = http_client_addr(&dst, buf, 1000,
987 wps_er_http_put_wlan_response_cb, sta);
988 if (sta->http == NULL)
993 static void wps_er_sta_process(struct wps_er_sta *sta, struct wpabuf *msg,
994 enum wsc_op_code op_code)
996 enum wps_process_res res;
998 res = wps_process_msg(sta->wps, op_code, msg);
999 if (res == WPS_CONTINUE) {
1000 struct wpabuf *next = wps_get_msg(sta->wps, &op_code);
1002 wps_er_sta_send_msg(sta, next);
1007 static void wps_er_sta_start(struct wps_er_sta *sta, struct wpabuf *msg)
1009 struct wps_config cfg;
1012 wps_deinit(sta->wps);
1014 os_memset(&cfg, 0, sizeof(cfg));
1015 cfg.wps = sta->ap->er->wps;
1017 cfg.peer_addr = sta->addr;
1019 sta->wps = wps_init(&cfg);
1020 if (sta->wps == NULL)
1023 sta->wps->use_cred = sta->ap->ap_settings;
1025 wps_er_sta_process(sta, msg, WSC_MSG);
1029 static void wps_er_process_wlanevent_eap(struct wps_er_ap *ap, const u8 *addr,
1032 struct wps_parse_attr attr;
1033 struct wps_er_sta *sta;
1035 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - EAP - from " MACSTR,
1037 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
1038 "(TLVs from EAP-WSC)", msg);
1040 if (wps_parse_msg(msg, &attr) < 0) {
1041 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
1042 "WLANEvent message");
1046 sta = wps_er_add_sta_data(ap, addr, &attr, 0);
1050 if (attr.msg_type && *attr.msg_type == WPS_M1)
1051 wps_er_sta_start(sta, msg);
1052 else if (sta->wps) {
1053 enum wsc_op_code op_code = WSC_MSG;
1054 if (attr.msg_type) {
1055 switch (*attr.msg_type) {
1067 wps_er_sta_process(sta, msg, op_code);
1072 static void wps_er_process_wlanevent(struct wps_er_ap *ap,
1073 struct wpabuf *event)
1077 u8 wlan_event_mac[ETH_ALEN];
1080 wpa_hexdump(MSG_MSGDUMP, "WPS ER: Received WLANEvent",
1081 wpabuf_head(event), wpabuf_len(event));
1082 if (wpabuf_len(event) < 1 + 17) {
1083 wpa_printf(MSG_DEBUG, "WPS ER: Too short WLANEvent");
1087 data = wpabuf_mhead(event);
1088 wlan_event_type = data[0];
1089 if (hwaddr_aton((char *) data + 1, wlan_event_mac) < 0) {
1090 wpa_printf(MSG_DEBUG, "WPS ER: Invalid WLANEventMAC in "
1095 wpabuf_set(&msg, data + 1 + 17, wpabuf_len(event) - (1 + 17));
1097 switch (wlan_event_type) {
1099 wps_er_process_wlanevent_probe_req(ap, wlan_event_mac, &msg);
1102 wps_er_process_wlanevent_eap(ap, wlan_event_mac, &msg);
1105 wpa_printf(MSG_DEBUG, "WPS ER: Unknown WLANEventType %d",
1112 static void wps_er_http_event(struct wps_er *er, struct http_request *req,
1115 struct wps_er_ap *ap = wps_er_ap_get_id(er, ap_id);
1116 struct wpabuf *event;
1117 enum http_reply_code ret;
1120 wpa_printf(MSG_DEBUG, "WPS ER: HTTP event from unknown AP id "
1122 wps_er_http_resp_not_found(req);
1125 wpa_printf(MSG_MSGDUMP, "WPS ER: HTTP event from AP id %u: %s",
1126 ap_id, http_request_get_data(req));
1128 event = xml_get_base64_item(http_request_get_data(req), "WLANEvent",
1130 if (event == NULL) {
1131 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract WLANEvent "
1132 "from the event notification");
1134 * Reply with OK anyway to avoid getting unregistered from
1137 wps_er_http_resp_ok(req);
1141 wps_er_process_wlanevent(ap, event);
1144 wps_er_http_resp_ok(req);
1148 static void wps_er_http_notify(struct wps_er *er, struct http_request *req)
1150 char *uri = http_request_get_uri(req);
1152 if (os_strncmp(uri, "/event/", 7) == 0) {
1153 wps_er_http_event(er, req, atoi(uri + 7));
1155 wpa_printf(MSG_DEBUG, "WPS ER: Unknown HTTP NOTIFY for '%s'",
1157 wps_er_http_resp_not_found(req);
1162 static void wps_er_http_req(void *ctx, struct http_request *req)
1164 struct wps_er *er = ctx;
1165 struct sockaddr_in *cli = http_request_get_cli_addr(req);
1166 enum httpread_hdr_type type = http_request_get_type(req);
1169 wpa_printf(MSG_DEBUG, "WPS ER: HTTP request: '%s' (type %d) from "
1171 http_request_get_uri(req), type,
1172 inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));
1175 case HTTPREAD_HDR_TYPE_NOTIFY:
1176 wps_er_http_notify(er, req);
1179 wpa_printf(MSG_DEBUG, "WPS ER: Unsupported HTTP request type "
1181 buf = wpabuf_alloc(200);
1183 http_request_deinit(req);
1187 "HTTP/1.1 501 Unimplemented\r\n"
1188 "Connection: close\r\n");
1190 wpabuf_put_str(buf, "\r\n");
1191 http_request_send_and_deinit(req, buf);
1198 wps_er_init(struct wps_context *wps, const char *ifname)
1201 struct in_addr addr;
1203 er = os_zalloc(sizeof(*er));
1207 er->multicast_sd = -1;
1210 os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
1213 if (get_netif_info(ifname,
1214 &er->ip_addr, &er->ip_addr_text,
1215 er->mac_addr, &er->mac_addr_text)) {
1216 wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
1217 "for %s. Does it have IP address?", ifname);
1222 if (add_ssdp_network(ifname)) {
1227 er->multicast_sd = ssdp_open_multicast_sock(er->ip_addr);
1228 if (er->multicast_sd < 0) {
1233 er->ssdp_sd = ssdp_listener_open();
1234 if (er->ssdp_sd < 0) {
1238 if (eloop_register_sock(er->multicast_sd, EVENT_TYPE_READ,
1239 wps_er_ssdp_rx, er, NULL) ||
1240 eloop_register_sock(er->ssdp_sd, EVENT_TYPE_READ,
1241 wps_er_ssdp_rx, er, NULL)) {
1246 addr.s_addr = er->ip_addr;
1247 er->http_srv = http_server_init(&addr, -1, wps_er_http_req, er);
1248 if (er->http_srv == NULL) {
1252 er->http_port = http_server_get_port(er->http_srv);
1254 wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s "
1256 er->ifname, er->ip_addr_text, er->mac_addr_text);
1258 wps_er_send_ssdp_msearch(er);
1264 void wps_er_refresh(struct wps_er *er)
1266 struct wps_er_ap *ap;
1267 struct wps_er_sta *sta;
1269 for (ap = er->ap; ap; ap = ap->next) {
1270 wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_ADD);
1271 for (sta = ap->sta; sta; sta = sta->next)
1272 wps_er_sta_event(er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
1275 wps_er_send_ssdp_msearch(er);
1279 void wps_er_deinit(struct wps_er *er)
1283 http_server_deinit(er->http_srv);
1284 wps_er_ap_remove_all(er);
1285 if (er->multicast_sd >= 0) {
1286 eloop_unregister_sock(er->multicast_sd, EVENT_TYPE_READ);
1287 close(er->multicast_sd);
1289 if (er->ssdp_sd >= 0) {
1290 eloop_unregister_sock(er->ssdp_sd, EVENT_TYPE_READ);
1293 os_free(er->ip_addr_text);
1294 os_free(er->mac_addr_text);
1299 static void wps_er_http_set_sel_reg_cb(void *ctx, struct http_client *c,
1300 enum http_client_event event)
1302 struct wps_er_ap *ap = ctx;
1305 case HTTP_CLIENT_OK:
1306 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar OK");
1308 case HTTP_CLIENT_FAILED:
1309 case HTTP_CLIENT_INVALID_REPLY:
1310 case HTTP_CLIENT_TIMEOUT:
1311 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar failed");
1314 http_client_free(ap->http);
1319 static void wps_er_send_set_sel_reg(struct wps_er_ap *ap, struct wpabuf *msg)
1322 char *len_ptr, *body_ptr;
1323 struct sockaddr_in dst;
1326 if (ap->control_url == NULL) {
1327 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1332 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for AP - "
1333 "ignore new request");
1337 url = http_client_url_parse(ap->control_url, &dst, &path);
1339 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1343 buf = wps_er_soap_hdr(msg, "SetSelectedRegistrar", "NewMessage", path,
1344 &dst, &len_ptr, &body_ptr);
1349 wps_er_soap_end(buf, "SetSelectedRegistrar", len_ptr, body_ptr);
1351 ap->http = http_client_addr(&dst, buf, 1000,
1352 wps_er_http_set_sel_reg_cb, ap);
1353 if (ap->http == NULL)
1358 static int wps_er_build_selected_registrar(struct wpabuf *msg, int sel_reg)
1360 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
1361 wpabuf_put_be16(msg, 1);
1362 wpabuf_put_u8(msg, !!sel_reg);
1367 static int wps_er_build_dev_password_id(struct wpabuf *msg, u16 dev_passwd_id)
1369 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
1370 wpabuf_put_be16(msg, 2);
1371 wpabuf_put_be16(msg, dev_passwd_id);
1376 static int wps_er_build_sel_reg_config_methods(struct wpabuf *msg,
1377 u16 sel_reg_config_methods)
1379 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
1380 wpabuf_put_be16(msg, 2);
1381 wpabuf_put_be16(msg, sel_reg_config_methods);
1386 void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
1387 u16 sel_reg_config_methods)
1390 struct wps_er_ap *ap;
1392 msg = wpabuf_alloc(500);
1396 if (wps_build_version(msg) ||
1397 wps_er_build_selected_registrar(msg, sel_reg) ||
1398 wps_er_build_dev_password_id(msg, dev_passwd_id) ||
1399 wps_er_build_sel_reg_config_methods(msg, sel_reg_config_methods)) {
1404 for (ap = er->ap; ap; ap = ap->next)
1405 wps_er_send_set_sel_reg(ap, msg);
1411 int wps_er_pbc(struct wps_er *er, const u8 *uuid)
1413 if (er == NULL || er->wps == NULL)
1417 * TODO: Should enable PBC mode only in a single AP based on which AP
1418 * the Enrollee (uuid) is using. Now, we may end up enabling multiple
1419 * APs in PBC mode which could result in session overlap at the
1422 if (wps_registrar_button_pushed(er->wps->registrar))
1429 static void wps_er_ap_settings_cb(void *ctx, const struct wps_credential *cred)
1431 struct wps_er_ap *ap = ctx;
1432 wpa_printf(MSG_DEBUG, "WPS ER: AP Settings received");
1433 os_free(ap->ap_settings);
1434 ap->ap_settings = os_malloc(sizeof(*cred));
1435 if (ap->ap_settings) {
1436 os_memcpy(ap->ap_settings, cred, sizeof(*cred));
1437 ap->ap_settings->cred_attr = NULL;
1440 /* TODO: send info through ctrl_iface */
1444 static void wps_er_http_put_message_cb(void *ctx, struct http_client *c,
1445 enum http_client_event event)
1447 struct wps_er_ap *ap = ctx;
1448 struct wpabuf *reply;
1452 case HTTP_CLIENT_OK:
1453 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage OK");
1454 reply = http_client_get_body(c);
1457 msg = os_zalloc(wpabuf_len(reply) + 1);
1460 os_memcpy(msg, wpabuf_head(reply), wpabuf_len(reply));
1462 case HTTP_CLIENT_FAILED:
1463 case HTTP_CLIENT_INVALID_REPLY:
1464 case HTTP_CLIENT_TIMEOUT:
1465 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage failed");
1467 wps_deinit(ap->wps);
1472 http_client_free(ap->http);
1477 enum http_reply_code ret;
1478 buf = xml_get_base64_item(msg, "NewOutMessage", &ret);
1481 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1482 "NewOutMessage from PutMessage response");
1485 wps_er_ap_process(ap, buf);
1491 static void wps_er_ap_put_message(struct wps_er_ap *ap,
1492 const struct wpabuf *msg)
1495 char *len_ptr, *body_ptr;
1496 struct sockaddr_in dst;
1500 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
1501 "with the AP - cannot continue learn");
1505 if (ap->control_url == NULL) {
1506 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1510 url = http_client_url_parse(ap->control_url, &dst, &path);
1512 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1516 buf = wps_er_soap_hdr(msg, "PutMessage", "NewInMessage", path, &dst,
1517 &len_ptr, &body_ptr);
1522 wps_er_soap_end(buf, "PutMessage", len_ptr, body_ptr);
1524 ap->http = http_client_addr(&dst, buf, 10000,
1525 wps_er_http_put_message_cb, ap);
1526 if (ap->http == NULL)
1531 static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg)
1533 enum wps_process_res res;
1535 res = wps_process_msg(ap->wps, WSC_MSG, msg);
1536 if (res == WPS_CONTINUE) {
1537 enum wsc_op_code op_code;
1538 struct wpabuf *next = wps_get_msg(ap->wps, &op_code);
1540 wps_er_ap_put_message(ap, next);
1543 wpa_printf(MSG_DEBUG, "WPS ER: Failed to build "
1545 wps_deinit(ap->wps);
1549 wpa_printf(MSG_DEBUG, "WPS ER: Failed to process message from "
1550 "AP (res=%d)", res);
1551 wps_deinit(ap->wps);
1557 static void wps_er_ap_learn(struct wps_er_ap *ap, const char *dev_info)
1559 struct wpabuf *info;
1560 enum http_reply_code ret;
1561 struct wps_config cfg;
1563 wpa_printf(MSG_DEBUG, "WPS ER: Received GetDeviceInfo response (M1) "
1565 info = xml_get_base64_item(dev_info, "NewDeviceInfo", &ret);
1567 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1568 "NewDeviceInfo from GetDeviceInfo response");
1573 wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
1574 "progress with this AP");
1579 os_memset(&cfg, 0, sizeof(cfg));
1580 cfg.wps = ap->er->wps;
1582 ap->wps = wps_init(&cfg);
1583 if (ap->wps == NULL) {
1587 ap->wps->ap_settings_cb = wps_er_ap_settings_cb;
1588 ap->wps->ap_settings_cb_ctx = ap;
1590 wps_er_ap_process(ap, info);
1595 static void wps_er_http_get_dev_info_cb(void *ctx, struct http_client *c,
1596 enum http_client_event event)
1598 struct wps_er_ap *ap = ctx;
1599 struct wpabuf *reply;
1600 char *dev_info = NULL;
1603 case HTTP_CLIENT_OK:
1604 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo OK");
1605 reply = http_client_get_body(c);
1608 dev_info = os_zalloc(wpabuf_len(reply) + 1);
1609 if (dev_info == NULL)
1611 os_memcpy(dev_info, wpabuf_head(reply), wpabuf_len(reply));
1613 case HTTP_CLIENT_FAILED:
1614 case HTTP_CLIENT_INVALID_REPLY:
1615 case HTTP_CLIENT_TIMEOUT:
1616 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo failed");
1619 http_client_free(ap->http);
1623 wps_er_ap_learn(ap, dev_info);
1629 int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
1632 struct wps_er_ap *ap;
1634 char *len_ptr, *body_ptr;
1635 struct sockaddr_in dst;
1641 ap = wps_er_ap_get_uuid(er, uuid);
1643 wpa_printf(MSG_DEBUG, "WPS ER: AP not found for learn "
1647 if (ap->wps || ap->http) {
1648 wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
1649 "with the AP - cannot start learn");
1653 if (ap->control_url == NULL) {
1654 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1658 url = http_client_url_parse(ap->control_url, &dst, &path);
1660 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1664 buf = wps_er_soap_hdr(NULL, "GetDeviceInfo", NULL, path, &dst,
1665 &len_ptr, &body_ptr);
1670 wps_er_soap_end(buf, "GetDeviceInfo", len_ptr, body_ptr);
1672 ap->http = http_client_addr(&dst, buf, 10000,
1673 wps_er_http_get_dev_info_cb, ap);
1674 if (ap->http == NULL) {
1679 /* TODO: add PIN without SetSelectedRegistrar trigger to all APs */
1680 wps_registrar_add_pin(er->wps->registrar, uuid, pin, pin_len, 0);