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 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx);
32 static void wps_er_sta_timeout(void *eloop_data, void *user_ctx);
33 static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg);
34 static int wps_er_send_get_device_info(struct wps_er_ap *ap,
35 void (*m1_handler)(struct wps_er_ap *ap,
39 static void wps_er_sta_event(struct wps_context *wps, struct wps_er_sta *sta,
42 union wps_event_data data;
43 struct wps_event_er_enrollee *ev = &data.enrollee;
45 if (wps->event_cb == NULL)
48 os_memset(&data, 0, sizeof(data));
50 ev->mac_addr = sta->addr;
51 ev->m1_received = sta->m1_received;
52 ev->config_methods = sta->config_methods;
53 ev->dev_passwd_id = sta->dev_passwd_id;
54 ev->pri_dev_type = sta->pri_dev_type;
55 ev->dev_name = sta->dev_name;
56 ev->manufacturer = sta->manufacturer;
57 ev->model_name = sta->model_name;
58 ev->model_number = sta->model_number;
59 ev->serial_number = sta->serial_number;
60 wps->event_cb(wps->cb_ctx, event, &data);
64 static struct wps_er_sta * wps_er_sta_get(struct wps_er_ap *ap, const u8 *addr)
66 struct wps_er_sta *sta = ap->sta;
68 if (os_memcmp(sta->addr, addr, ETH_ALEN) == 0)
76 static void wps_er_sta_free(struct wps_er_sta *sta)
78 wps_er_sta_event(sta->ap->er->wps, sta, WPS_EV_ER_ENROLLEE_REMOVE);
81 os_free(sta->manufacturer);
82 os_free(sta->model_name);
83 os_free(sta->model_number);
84 os_free(sta->serial_number);
85 os_free(sta->dev_name);
86 http_client_free(sta->http);
87 eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
92 static void wps_er_sta_unlink(struct wps_er_sta *sta)
94 struct wps_er_sta *prev, *tmp;
95 struct wps_er_ap *ap = sta->ap;
101 prev->next = sta->next;
112 static void wps_er_sta_remove_all(struct wps_er_ap *ap)
114 struct wps_er_sta *prev, *sta;
122 wps_er_sta_free(prev);
127 static struct wps_er_ap * wps_er_ap_get(struct wps_er *er,
128 struct in_addr *addr, const u8 *uuid)
130 struct wps_er_ap *ap;
131 for (ap = er->ap; ap; ap = ap->next) {
132 if ((addr == NULL || ap->addr.s_addr == addr->s_addr) &&
134 os_memcmp(uuid, ap->uuid, WPS_UUID_LEN) == 0))
141 static struct wps_er_ap * wps_er_ap_get_id(struct wps_er *er, unsigned int id)
143 struct wps_er_ap *ap;
144 for (ap = er->ap; ap; ap = ap->next) {
152 static void wps_er_ap_event(struct wps_context *wps, struct wps_er_ap *ap,
153 enum wps_event event)
155 union wps_event_data data;
156 struct wps_event_er_ap *evap = &data.ap;
158 if (wps->event_cb == NULL)
161 os_memset(&data, 0, sizeof(data));
162 evap->uuid = ap->uuid;
163 evap->friendly_name = ap->friendly_name;
164 evap->manufacturer = ap->manufacturer;
165 evap->manufacturer_url = ap->manufacturer_url;
166 evap->model_description = ap->model_description;
167 evap->model_name = ap->model_name;
168 evap->model_number = ap->model_number;
169 evap->model_url = ap->model_url;
170 evap->serial_number = ap->serial_number;
172 evap->pri_dev_type = ap->pri_dev_type;
173 evap->wps_state = ap->wps_state;
174 evap->mac_addr = ap->mac_addr;
175 wps->event_cb(wps->cb_ctx, event, &data);
179 static void wps_er_ap_free(struct wps_er *er, struct wps_er_ap *ap)
181 /* TODO: if ap->subscribed, unsubscribe from events if the AP is still
183 wpa_printf(MSG_DEBUG, "WPS ER: Removing AP entry for %s (%s)",
184 inet_ntoa(ap->addr), ap->location);
185 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
186 wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_REMOVE);
187 os_free(ap->location);
188 http_client_free(ap->http);
192 os_free(ap->friendly_name);
193 os_free(ap->manufacturer);
194 os_free(ap->manufacturer_url);
195 os_free(ap->model_description);
196 os_free(ap->model_name);
197 os_free(ap->model_number);
198 os_free(ap->model_url);
199 os_free(ap->serial_number);
203 os_free(ap->scpd_url);
204 os_free(ap->control_url);
205 os_free(ap->event_sub_url);
207 os_free(ap->ap_settings);
209 wps_er_sta_remove_all(ap);
215 static void wps_er_ap_unlink(struct wps_er *er, struct wps_er_ap *ap)
217 struct wps_er_ap *prev, *tmp;
223 prev->next = ap->next;
234 static void wps_er_ap_timeout(void *eloop_data, void *user_ctx)
236 struct wps_er *er = eloop_data;
237 struct wps_er_ap *ap = user_ctx;
238 wpa_printf(MSG_DEBUG, "WPS ER: AP advertisement timed out");
239 wps_er_ap_unlink(er, ap);
240 wps_er_ap_free(er, ap);
244 static void wps_er_http_subscribe_cb(void *ctx, struct http_client *c,
245 enum http_client_event event)
247 struct wps_er_ap *ap = ctx;
251 wpa_printf(MSG_DEBUG, "WPS ER: Subscribed to events");
252 wps_er_ap_event(ap->er->wps, ap, WPS_EV_ER_AP_ADD);
254 case HTTP_CLIENT_FAILED:
255 case HTTP_CLIENT_INVALID_REPLY:
256 case HTTP_CLIENT_TIMEOUT:
257 wpa_printf(MSG_DEBUG, "WPS ER: Failed to subscribe to events");
260 http_client_free(ap->http);
265 static void wps_er_subscribe(struct wps_er_ap *ap)
268 struct sockaddr_in dst;
271 if (ap->event_sub_url == NULL) {
272 wpa_printf(MSG_DEBUG, "WPS ER: No eventSubURL - cannot "
277 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request - cannot "
278 "send subscribe request");
282 url = http_client_url_parse(ap->event_sub_url, &dst, &path);
284 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse eventSubURL");
288 req = wpabuf_alloc(os_strlen(ap->event_sub_url) + 1000);
294 "SUBSCRIBE %s HTTP/1.1\r\n"
296 "CALLBACK: <http://%s:%d/event/%d>\r\n"
298 "TIMEOUT: Second-%d\r\n"
300 path, inet_ntoa(dst.sin_addr), ntohs(dst.sin_port),
301 ap->er->ip_addr_text, ap->er->http_port, ap->id, 1800);
303 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Subscription request",
304 wpabuf_head(req), wpabuf_len(req));
306 ap->http = http_client_addr(&dst, req, 1000, wps_er_http_subscribe_cb,
308 if (ap->http == NULL)
313 static void wps_er_ap_get_m1(struct wps_er_ap *ap, struct wpabuf *m1)
315 struct wps_parse_attr attr;
317 if (wps_parse_msg(m1, &attr) < 0) {
318 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse M1");
321 if (attr.primary_dev_type)
322 os_memcpy(ap->pri_dev_type, attr.primary_dev_type, 8);
324 ap->wps_state = *attr.wps_state;
326 os_memcpy(ap->mac_addr, attr.mac_addr, ETH_ALEN);
328 wps_er_subscribe(ap);
332 static void wps_er_get_device_info(struct wps_er_ap *ap)
334 wps_er_send_get_device_info(ap, wps_er_ap_get_m1);
338 static void wps_er_parse_device_description(struct wps_er_ap *ap,
339 struct wpabuf *reply)
341 /* Note: reply includes null termination after the buffer data */
342 const char *data = wpabuf_head(reply);
345 wpa_hexdump_ascii(MSG_MSGDUMP, "WPS ER: Device info",
346 wpabuf_head(reply), wpabuf_len(reply));
348 ap->friendly_name = xml_get_first_item(data, "friendlyName");
349 wpa_printf(MSG_DEBUG, "WPS ER: friendlyName='%s'", ap->friendly_name);
351 ap->manufacturer = xml_get_first_item(data, "manufacturer");
352 wpa_printf(MSG_DEBUG, "WPS ER: manufacturer='%s'", ap->manufacturer);
354 ap->manufacturer_url = xml_get_first_item(data, "manufacturerURL");
355 wpa_printf(MSG_DEBUG, "WPS ER: manufacturerURL='%s'",
356 ap->manufacturer_url);
358 ap->model_description = xml_get_first_item(data, "modelDescription");
359 wpa_printf(MSG_DEBUG, "WPS ER: modelDescription='%s'",
360 ap->model_description);
362 ap->model_name = xml_get_first_item(data, "modelName");
363 wpa_printf(MSG_DEBUG, "WPS ER: modelName='%s'", ap->model_name);
365 ap->model_number = xml_get_first_item(data, "modelNumber");
366 wpa_printf(MSG_DEBUG, "WPS ER: modelNumber='%s'", ap->model_number);
368 ap->model_url = xml_get_first_item(data, "modelURL");
369 wpa_printf(MSG_DEBUG, "WPS ER: modelURL='%s'", ap->model_url);
371 ap->serial_number = xml_get_first_item(data, "serialNumber");
372 wpa_printf(MSG_DEBUG, "WPS ER: serialNumber='%s'", ap->serial_number);
374 ap->udn = xml_get_first_item(data, "UDN");
375 wpa_printf(MSG_DEBUG, "WPS ER: UDN='%s'", ap->udn);
376 pos = os_strstr(ap->udn, "uuid:");
379 uuid_str2bin(pos, ap->uuid);
382 ap->upc = xml_get_first_item(data, "UPC");
383 wpa_printf(MSG_DEBUG, "WPS ER: UPC='%s'", ap->upc);
385 ap->scpd_url = http_link_update(
386 xml_get_first_item(data, "SCPDURL"), ap->location);
387 wpa_printf(MSG_DEBUG, "WPS ER: SCPDURL='%s'", ap->scpd_url);
389 ap->control_url = http_link_update(
390 xml_get_first_item(data, "controlURL"), ap->location);
391 wpa_printf(MSG_DEBUG, "WPS ER: controlURL='%s'", ap->control_url);
393 ap->event_sub_url = http_link_update(
394 xml_get_first_item(data, "eventSubURL"), ap->location);
395 wpa_printf(MSG_DEBUG, "WPS ER: eventSubURL='%s'", ap->event_sub_url);
399 static void wps_er_http_dev_desc_cb(void *ctx, struct http_client *c,
400 enum http_client_event event)
402 struct wps_er_ap *ap = ctx;
403 struct wpabuf *reply;
408 reply = http_client_get_body(c);
411 wps_er_parse_device_description(ap, reply);
414 case HTTP_CLIENT_FAILED:
415 case HTTP_CLIENT_INVALID_REPLY:
416 case HTTP_CLIENT_TIMEOUT:
417 wpa_printf(MSG_DEBUG, "WPS ER: Failed to fetch device info");
420 http_client_free(ap->http);
423 wps_er_get_device_info(ap);
427 void wps_er_ap_add(struct wps_er *er, const u8 *uuid, struct in_addr *addr,
428 const char *location, int max_age)
430 struct wps_er_ap *ap;
432 ap = wps_er_ap_get(er, addr, uuid);
434 /* Update advertisement timeout */
435 eloop_cancel_timeout(wps_er_ap_timeout, er, ap);
436 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
440 ap = os_zalloc(sizeof(*ap));
444 ap->id = ++er->next_ap_id;
445 ap->location = os_strdup(location);
446 if (ap->location == NULL) {
453 ap->addr.s_addr = addr->s_addr;
454 os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
455 eloop_register_timeout(max_age, 0, wps_er_ap_timeout, er, ap);
457 wpa_printf(MSG_DEBUG, "WPS ER: Added AP entry for %s (%s)",
458 inet_ntoa(ap->addr), ap->location);
460 /* Fetch device description */
461 ap->http = http_client_url(ap->location, NULL, 10000,
462 wps_er_http_dev_desc_cb, ap);
466 void wps_er_ap_remove(struct wps_er *er, struct in_addr *addr)
468 struct wps_er_ap *prev = NULL, *ap = er->ap;
471 if (ap->addr.s_addr == addr->s_addr) {
473 prev->next = ap->next;
476 wps_er_ap_free(er, ap);
485 static void wps_er_ap_remove_all(struct wps_er *er)
487 struct wps_er_ap *prev, *ap;
495 wps_er_ap_free(er, prev);
500 static void http_put_date(struct wpabuf *buf)
502 wpabuf_put_str(buf, "Date: ");
504 wpabuf_put_str(buf, "\r\n");
508 static void wps_er_http_resp_not_found(struct http_request *req)
511 buf = wpabuf_alloc(200);
513 http_request_deinit(req);
518 "HTTP/1.1 404 Not Found\r\n"
519 "Server: unspecified, UPnP/1.0, unspecified\r\n"
520 "Connection: close\r\n");
522 wpabuf_put_str(buf, "\r\n");
523 http_request_send_and_deinit(req, buf);
527 static void wps_er_http_resp_ok(struct http_request *req)
530 buf = wpabuf_alloc(200);
532 http_request_deinit(req);
537 "HTTP/1.1 200 OK\r\n"
538 "Server: unspecified, UPnP/1.0, unspecified\r\n"
539 "Connection: close\r\n"
540 "Content-Length: 0\r\n");
542 wpabuf_put_str(buf, "\r\n");
543 http_request_send_and_deinit(req, buf);
547 static void wps_er_sta_timeout(void *eloop_data, void *user_ctx)
549 struct wps_er_sta *sta = eloop_data;
550 wpa_printf(MSG_DEBUG, "WPS ER: STA entry timed out");
551 wps_er_sta_unlink(sta);
552 wps_er_sta_free(sta);
556 static struct wps_er_sta * wps_er_add_sta_data(struct wps_er_ap *ap,
558 struct wps_parse_attr *attr,
561 struct wps_er_sta *sta = wps_er_sta_get(ap, addr);
565 m1 = !probe_req && attr->msg_type && *attr->msg_type == WPS_M1;
569 * Only allow new STA entry to be added based on Probe Request
570 * or M1. This will filter out bogus events and anything that
571 * may have been ongoing at the time ER subscribed for events.
573 if (!probe_req && !m1)
576 sta = os_zalloc(sizeof(*sta));
579 os_memcpy(sta->addr, addr, ETH_ALEN);
587 sta->m1_received = 1;
589 if (attr->config_methods && (!probe_req || !sta->m1_received))
590 sta->config_methods = WPA_GET_BE16(attr->config_methods);
591 if (attr->uuid_e && (!probe_req || !sta->m1_received))
592 os_memcpy(sta->uuid, attr->uuid_e, WPS_UUID_LEN);
593 if (attr->primary_dev_type && (!probe_req || !sta->m1_received))
594 os_memcpy(sta->pri_dev_type, attr->primary_dev_type, 8);
595 if (attr->dev_password_id && (!probe_req || !sta->m1_received))
596 sta->dev_passwd_id = WPA_GET_BE16(attr->dev_password_id);
598 if (attr->manufacturer) {
599 os_free(sta->manufacturer);
600 sta->manufacturer = os_malloc(attr->manufacturer_len + 1);
601 if (sta->manufacturer) {
602 os_memcpy(sta->manufacturer, attr->manufacturer,
603 attr->manufacturer_len);
604 sta->manufacturer[attr->manufacturer_len] = '\0';
608 if (attr->model_name) {
609 os_free(sta->model_name);
610 sta->model_name = os_malloc(attr->model_name_len + 1);
611 if (sta->model_name) {
612 os_memcpy(sta->model_name, attr->model_name,
613 attr->model_name_len);
614 sta->model_name[attr->model_name_len] = '\0';
618 if (attr->model_number) {
619 os_free(sta->model_number);
620 sta->model_number = os_malloc(attr->model_number_len + 1);
621 if (sta->model_number) {
622 os_memcpy(sta->model_number, attr->model_number,
623 attr->model_number_len);
624 sta->model_number[attr->model_number_len] = '\0';
628 if (attr->serial_number) {
629 os_free(sta->serial_number);
630 sta->serial_number = os_malloc(attr->serial_number_len + 1);
631 if (sta->serial_number) {
632 os_memcpy(sta->serial_number, attr->serial_number,
633 attr->serial_number_len);
634 sta->serial_number[attr->serial_number_len] = '\0';
638 if (attr->dev_name) {
639 os_free(sta->dev_name);
640 sta->dev_name = os_malloc(attr->dev_name_len + 1);
642 os_memcpy(sta->dev_name, attr->dev_name,
644 sta->dev_name[attr->dev_name_len] = '\0';
648 eloop_cancel_timeout(wps_er_sta_timeout, sta, NULL);
649 eloop_register_timeout(300, 0, wps_er_sta_timeout, sta, NULL);
652 wps_er_sta_event(ap->er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
658 static void wps_er_process_wlanevent_probe_req(struct wps_er_ap *ap,
662 struct wps_parse_attr attr;
664 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - Probe Request - from "
665 MACSTR, MAC2STR(addr));
666 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
667 "(TLVs from Probe Request)", msg);
669 if (wps_parse_msg(msg, &attr) < 0) {
670 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
671 "WLANEvent message");
675 wps_er_add_sta_data(ap, addr, &attr, 1);
679 static void wps_er_http_put_wlan_response_cb(void *ctx, struct http_client *c,
680 enum http_client_event event)
682 struct wps_er_sta *sta = ctx;
686 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse OK");
688 case HTTP_CLIENT_FAILED:
689 case HTTP_CLIENT_INVALID_REPLY:
690 case HTTP_CLIENT_TIMEOUT:
691 wpa_printf(MSG_DEBUG, "WPS ER: PutWLANResponse failed");
694 http_client_free(sta->http);
699 static const char *soap_prefix =
700 "<?xml version=\"1.0\"?>\n"
701 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
702 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
704 static const char *soap_postfix =
705 "</s:Body>\n</s:Envelope>\n";
706 static const char *urn_wfawlanconfig =
707 "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";
709 static struct wpabuf * wps_er_soap_hdr(const struct wpabuf *msg,
710 const char *name, const char *arg_name,
712 const struct sockaddr_in *dst,
713 char **len_ptr, char **body_ptr)
715 unsigned char *encoded;
720 encoded = base64_encode(wpabuf_head(msg), wpabuf_len(msg),
729 buf = wpabuf_alloc(1000 + encoded_len);
736 "POST %s HTTP/1.1\r\n"
738 "Content-Type: text/xml; charset=\"utf-8\"\r\n"
740 path, inet_ntoa(dst->sin_addr), ntohs(dst->sin_port));
742 *len_ptr = wpabuf_put(buf, 0);
745 "SOAPACTION: \"%s#%s\"\r\n"
747 urn_wfawlanconfig, name);
749 *body_ptr = wpabuf_put(buf, 0);
751 wpabuf_put_str(buf, soap_prefix);
752 wpabuf_printf(buf, "<u:%s xmlns:u=\"", name);
753 wpabuf_put_str(buf, urn_wfawlanconfig);
754 wpabuf_put_str(buf, "\">\n");
756 wpabuf_printf(buf, "<%s>%s</%s>\n",
757 arg_name, (char *) encoded, arg_name);
765 static void wps_er_soap_end(struct wpabuf *buf, const char *name,
766 char *len_ptr, char *body_ptr)
769 wpabuf_printf(buf, "</u:%s>\n", name);
770 wpabuf_put_str(buf, soap_postfix);
771 os_snprintf(len_buf, sizeof(len_buf), "%d",
772 (int) ((char *) wpabuf_put(buf, 0) - body_ptr));
773 os_memcpy(len_ptr, len_buf, os_strlen(len_buf));
777 static void wps_er_sta_send_msg(struct wps_er_sta *sta, struct wpabuf *msg)
780 char *len_ptr, *body_ptr;
781 struct sockaddr_in dst;
785 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for STA - "
786 "ignore new request");
791 if (sta->ap->control_url == NULL) {
792 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
797 url = http_client_url_parse(sta->ap->control_url, &dst, &path);
799 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
804 buf = wps_er_soap_hdr(msg, "PutWLANResponse", "NewMessage", path, &dst,
805 &len_ptr, &body_ptr);
810 wpabuf_printf(buf, "<NewWLANEventType>%d</NewWLANEventType>\n",
811 UPNP_WPS_WLANEVENT_TYPE_EAP);
812 wpabuf_printf(buf, "<NewWLANEventMAC>" MACSTR "</NewWLANEventMAC>\n",
815 wps_er_soap_end(buf, "PutWLANResponse", len_ptr, body_ptr);
817 sta->http = http_client_addr(&dst, buf, 1000,
818 wps_er_http_put_wlan_response_cb, sta);
819 if (sta->http == NULL)
824 static void wps_er_sta_process(struct wps_er_sta *sta, struct wpabuf *msg,
825 enum wsc_op_code op_code)
827 enum wps_process_res res;
829 res = wps_process_msg(sta->wps, op_code, msg);
830 if (res == WPS_CONTINUE) {
831 struct wpabuf *next = wps_get_msg(sta->wps, &op_code);
833 wps_er_sta_send_msg(sta, next);
838 static void wps_er_sta_start(struct wps_er_sta *sta, struct wpabuf *msg)
840 struct wps_config cfg;
843 wps_deinit(sta->wps);
845 os_memset(&cfg, 0, sizeof(cfg));
846 cfg.wps = sta->ap->er->wps;
848 cfg.peer_addr = sta->addr;
850 sta->wps = wps_init(&cfg);
851 if (sta->wps == NULL)
854 sta->wps->use_cred = sta->ap->ap_settings;
856 wps_er_sta_process(sta, msg, WSC_MSG);
860 static void wps_er_process_wlanevent_eap(struct wps_er_ap *ap, const u8 *addr,
863 struct wps_parse_attr attr;
864 struct wps_er_sta *sta;
866 wpa_printf(MSG_DEBUG, "WPS ER: WLANEvent - EAP - from " MACSTR,
868 wpa_hexdump_buf(MSG_MSGDUMP, "WPS ER: WLANEvent - Enrollee's message "
869 "(TLVs from EAP-WSC)", msg);
871 if (wps_parse_msg(msg, &attr) < 0) {
872 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse TLVs in "
873 "WLANEvent message");
877 sta = wps_er_add_sta_data(ap, addr, &attr, 0);
881 if (attr.msg_type && *attr.msg_type == WPS_M1)
882 wps_er_sta_start(sta, msg);
884 enum wsc_op_code op_code = WSC_MSG;
886 switch (*attr.msg_type) {
898 wps_er_sta_process(sta, msg, op_code);
903 static void wps_er_process_wlanevent(struct wps_er_ap *ap,
904 struct wpabuf *event)
908 u8 wlan_event_mac[ETH_ALEN];
911 wpa_hexdump(MSG_MSGDUMP, "WPS ER: Received WLANEvent",
912 wpabuf_head(event), wpabuf_len(event));
913 if (wpabuf_len(event) < 1 + 17) {
914 wpa_printf(MSG_DEBUG, "WPS ER: Too short WLANEvent");
918 data = wpabuf_mhead(event);
919 wlan_event_type = data[0];
920 if (hwaddr_aton((char *) data + 1, wlan_event_mac) < 0) {
921 wpa_printf(MSG_DEBUG, "WPS ER: Invalid WLANEventMAC in "
926 wpabuf_set(&msg, data + 1 + 17, wpabuf_len(event) - (1 + 17));
928 switch (wlan_event_type) {
930 wps_er_process_wlanevent_probe_req(ap, wlan_event_mac, &msg);
933 wps_er_process_wlanevent_eap(ap, wlan_event_mac, &msg);
936 wpa_printf(MSG_DEBUG, "WPS ER: Unknown WLANEventType %d",
943 static void wps_er_http_event(struct wps_er *er, struct http_request *req,
946 struct wps_er_ap *ap = wps_er_ap_get_id(er, ap_id);
947 struct wpabuf *event;
948 enum http_reply_code ret;
951 wpa_printf(MSG_DEBUG, "WPS ER: HTTP event from unknown AP id "
953 wps_er_http_resp_not_found(req);
956 wpa_printf(MSG_MSGDUMP, "WPS ER: HTTP event from AP id %u: %s",
957 ap_id, http_request_get_data(req));
959 event = xml_get_base64_item(http_request_get_data(req), "WLANEvent",
962 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract WLANEvent "
963 "from the event notification");
965 * Reply with OK anyway to avoid getting unregistered from
968 wps_er_http_resp_ok(req);
972 wps_er_process_wlanevent(ap, event);
975 wps_er_http_resp_ok(req);
979 static void wps_er_http_notify(struct wps_er *er, struct http_request *req)
981 char *uri = http_request_get_uri(req);
983 if (os_strncmp(uri, "/event/", 7) == 0) {
984 wps_er_http_event(er, req, atoi(uri + 7));
986 wpa_printf(MSG_DEBUG, "WPS ER: Unknown HTTP NOTIFY for '%s'",
988 wps_er_http_resp_not_found(req);
993 static void wps_er_http_req(void *ctx, struct http_request *req)
995 struct wps_er *er = ctx;
996 struct sockaddr_in *cli = http_request_get_cli_addr(req);
997 enum httpread_hdr_type type = http_request_get_type(req);
1000 wpa_printf(MSG_DEBUG, "WPS ER: HTTP request: '%s' (type %d) from "
1002 http_request_get_uri(req), type,
1003 inet_ntoa(cli->sin_addr), ntohs(cli->sin_port));
1006 case HTTPREAD_HDR_TYPE_NOTIFY:
1007 wps_er_http_notify(er, req);
1010 wpa_printf(MSG_DEBUG, "WPS ER: Unsupported HTTP request type "
1012 buf = wpabuf_alloc(200);
1014 http_request_deinit(req);
1018 "HTTP/1.1 501 Unimplemented\r\n"
1019 "Connection: close\r\n");
1021 wpabuf_put_str(buf, "\r\n");
1022 http_request_send_and_deinit(req, buf);
1029 wps_er_init(struct wps_context *wps, const char *ifname)
1032 struct in_addr addr;
1034 er = os_zalloc(sizeof(*er));
1038 er->multicast_sd = -1;
1041 os_strlcpy(er->ifname, ifname, sizeof(er->ifname));
1044 if (get_netif_info(ifname,
1045 &er->ip_addr, &er->ip_addr_text,
1046 er->mac_addr, &er->mac_addr_text)) {
1047 wpa_printf(MSG_INFO, "WPS UPnP: Could not get IP/MAC address "
1048 "for %s. Does it have IP address?", ifname);
1053 if (wps_er_ssdp_init(er) < 0) {
1058 addr.s_addr = er->ip_addr;
1059 er->http_srv = http_server_init(&addr, -1, wps_er_http_req, er);
1060 if (er->http_srv == NULL) {
1064 er->http_port = http_server_get_port(er->http_srv);
1066 wpa_printf(MSG_DEBUG, "WPS ER: Start (ifname=%s ip_addr=%s "
1068 er->ifname, er->ip_addr_text, er->mac_addr_text);
1074 void wps_er_refresh(struct wps_er *er)
1076 struct wps_er_ap *ap;
1077 struct wps_er_sta *sta;
1079 for (ap = er->ap; ap; ap = ap->next) {
1080 wps_er_ap_event(er->wps, ap, WPS_EV_ER_AP_ADD);
1081 for (sta = ap->sta; sta; sta = sta->next)
1082 wps_er_sta_event(er->wps, sta, WPS_EV_ER_ENROLLEE_ADD);
1085 wps_er_send_ssdp_msearch(er);
1089 void wps_er_deinit(struct wps_er *er)
1093 http_server_deinit(er->http_srv);
1094 wps_er_ap_remove_all(er);
1095 wps_er_ssdp_deinit(er);
1096 os_free(er->ip_addr_text);
1097 os_free(er->mac_addr_text);
1102 static void wps_er_http_set_sel_reg_cb(void *ctx, struct http_client *c,
1103 enum http_client_event event)
1105 struct wps_er_ap *ap = ctx;
1108 case HTTP_CLIENT_OK:
1109 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar OK");
1111 case HTTP_CLIENT_FAILED:
1112 case HTTP_CLIENT_INVALID_REPLY:
1113 case HTTP_CLIENT_TIMEOUT:
1114 wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar failed");
1117 http_client_free(ap->http);
1122 static void wps_er_send_set_sel_reg(struct wps_er_ap *ap, struct wpabuf *msg)
1125 char *len_ptr, *body_ptr;
1126 struct sockaddr_in dst;
1129 if (ap->control_url == NULL) {
1130 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1135 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP request for AP - "
1136 "ignore new request");
1140 url = http_client_url_parse(ap->control_url, &dst, &path);
1142 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1146 buf = wps_er_soap_hdr(msg, "SetSelectedRegistrar", "NewMessage", path,
1147 &dst, &len_ptr, &body_ptr);
1152 wps_er_soap_end(buf, "SetSelectedRegistrar", len_ptr, body_ptr);
1154 ap->http = http_client_addr(&dst, buf, 1000,
1155 wps_er_http_set_sel_reg_cb, ap);
1156 if (ap->http == NULL)
1161 static int wps_er_build_selected_registrar(struct wpabuf *msg, int sel_reg)
1163 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
1164 wpabuf_put_be16(msg, 1);
1165 wpabuf_put_u8(msg, !!sel_reg);
1170 static int wps_er_build_dev_password_id(struct wpabuf *msg, u16 dev_passwd_id)
1172 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
1173 wpabuf_put_be16(msg, 2);
1174 wpabuf_put_be16(msg, dev_passwd_id);
1179 static int wps_er_build_sel_reg_config_methods(struct wpabuf *msg,
1180 u16 sel_reg_config_methods)
1182 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
1183 wpabuf_put_be16(msg, 2);
1184 wpabuf_put_be16(msg, sel_reg_config_methods);
1189 void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
1190 u16 sel_reg_config_methods)
1193 struct wps_er_ap *ap;
1195 msg = wpabuf_alloc(500);
1199 if (wps_build_version(msg) ||
1200 wps_er_build_selected_registrar(msg, sel_reg) ||
1201 wps_er_build_dev_password_id(msg, dev_passwd_id) ||
1202 wps_er_build_sel_reg_config_methods(msg, sel_reg_config_methods)) {
1207 for (ap = er->ap; ap; ap = ap->next)
1208 wps_er_send_set_sel_reg(ap, msg);
1214 int wps_er_pbc(struct wps_er *er, const u8 *uuid)
1216 if (er == NULL || er->wps == NULL)
1220 * TODO: Should enable PBC mode only in a single AP based on which AP
1221 * the Enrollee (uuid) is using. Now, we may end up enabling multiple
1222 * APs in PBC mode which could result in session overlap at the
1225 if (wps_registrar_button_pushed(er->wps->registrar))
1232 static void wps_er_ap_settings_cb(void *ctx, const struct wps_credential *cred)
1234 struct wps_er_ap *ap = ctx;
1235 wpa_printf(MSG_DEBUG, "WPS ER: AP Settings received");
1236 os_free(ap->ap_settings);
1237 ap->ap_settings = os_malloc(sizeof(*cred));
1238 if (ap->ap_settings) {
1239 os_memcpy(ap->ap_settings, cred, sizeof(*cred));
1240 ap->ap_settings->cred_attr = NULL;
1243 /* TODO: send info through ctrl_iface */
1247 static void wps_er_http_put_message_cb(void *ctx, struct http_client *c,
1248 enum http_client_event event)
1250 struct wps_er_ap *ap = ctx;
1251 struct wpabuf *reply;
1255 case HTTP_CLIENT_OK:
1256 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage OK");
1257 reply = http_client_get_body(c);
1260 msg = os_zalloc(wpabuf_len(reply) + 1);
1263 os_memcpy(msg, wpabuf_head(reply), wpabuf_len(reply));
1265 case HTTP_CLIENT_FAILED:
1266 case HTTP_CLIENT_INVALID_REPLY:
1267 case HTTP_CLIENT_TIMEOUT:
1268 wpa_printf(MSG_DEBUG, "WPS ER: PutMessage failed");
1270 wps_deinit(ap->wps);
1275 http_client_free(ap->http);
1280 enum http_reply_code ret;
1281 buf = xml_get_base64_item(msg, "NewOutMessage", &ret);
1284 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1285 "NewOutMessage from PutMessage response");
1288 wps_er_ap_process(ap, buf);
1294 static void wps_er_ap_put_message(struct wps_er_ap *ap,
1295 const struct wpabuf *msg)
1298 char *len_ptr, *body_ptr;
1299 struct sockaddr_in dst;
1303 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
1304 "with the AP - cannot continue learn");
1308 if (ap->control_url == NULL) {
1309 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1313 url = http_client_url_parse(ap->control_url, &dst, &path);
1315 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1319 buf = wps_er_soap_hdr(msg, "PutMessage", "NewInMessage", path, &dst,
1320 &len_ptr, &body_ptr);
1325 wps_er_soap_end(buf, "PutMessage", len_ptr, body_ptr);
1327 ap->http = http_client_addr(&dst, buf, 10000,
1328 wps_er_http_put_message_cb, ap);
1329 if (ap->http == NULL)
1334 static void wps_er_ap_process(struct wps_er_ap *ap, struct wpabuf *msg)
1336 enum wps_process_res res;
1338 res = wps_process_msg(ap->wps, WSC_MSG, msg);
1339 if (res == WPS_CONTINUE) {
1340 enum wsc_op_code op_code;
1341 struct wpabuf *next = wps_get_msg(ap->wps, &op_code);
1343 wps_er_ap_put_message(ap, next);
1346 wpa_printf(MSG_DEBUG, "WPS ER: Failed to build "
1348 wps_deinit(ap->wps);
1352 wpa_printf(MSG_DEBUG, "WPS ER: Failed to process message from "
1353 "AP (res=%d)", res);
1354 wps_deinit(ap->wps);
1360 static void wps_er_ap_learn_m1(struct wps_er_ap *ap, struct wpabuf *m1)
1362 struct wps_config cfg;
1365 wpa_printf(MSG_DEBUG, "WPS ER: Protocol run already in "
1366 "progress with this AP");
1370 os_memset(&cfg, 0, sizeof(cfg));
1371 cfg.wps = ap->er->wps;
1373 ap->wps = wps_init(&cfg);
1374 if (ap->wps == NULL)
1376 ap->wps->ap_settings_cb = wps_er_ap_settings_cb;
1377 ap->wps->ap_settings_cb_ctx = ap;
1379 wps_er_ap_process(ap, m1);
1383 static void wps_er_ap_learn(struct wps_er_ap *ap, const char *dev_info)
1385 struct wpabuf *info;
1386 enum http_reply_code ret;
1388 wpa_printf(MSG_DEBUG, "WPS ER: Received GetDeviceInfo response (M1) "
1390 info = xml_get_base64_item(dev_info, "NewDeviceInfo", &ret);
1392 wpa_printf(MSG_DEBUG, "WPS ER: Could not extract "
1393 "NewDeviceInfo from GetDeviceInfo response");
1397 ap->m1_handler(ap, info);
1402 static void wps_er_http_get_dev_info_cb(void *ctx, struct http_client *c,
1403 enum http_client_event event)
1405 struct wps_er_ap *ap = ctx;
1406 struct wpabuf *reply;
1407 char *dev_info = NULL;
1410 case HTTP_CLIENT_OK:
1411 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo OK");
1412 reply = http_client_get_body(c);
1415 dev_info = os_zalloc(wpabuf_len(reply) + 1);
1416 if (dev_info == NULL)
1418 os_memcpy(dev_info, wpabuf_head(reply), wpabuf_len(reply));
1420 case HTTP_CLIENT_FAILED:
1421 case HTTP_CLIENT_INVALID_REPLY:
1422 case HTTP_CLIENT_TIMEOUT:
1423 wpa_printf(MSG_DEBUG, "WPS ER: GetDeviceInfo failed");
1426 http_client_free(ap->http);
1430 wps_er_ap_learn(ap, dev_info);
1436 static int wps_er_send_get_device_info(struct wps_er_ap *ap,
1437 void (*m1_handler)(struct wps_er_ap *ap,
1441 char *len_ptr, *body_ptr;
1442 struct sockaddr_in dst;
1446 wpa_printf(MSG_DEBUG, "WPS ER: Pending HTTP operation ongoing "
1447 "with the AP - cannot get device info");
1451 if (ap->control_url == NULL) {
1452 wpa_printf(MSG_DEBUG, "WPS ER: No controlURL for AP");
1456 url = http_client_url_parse(ap->control_url, &dst, &path);
1458 wpa_printf(MSG_DEBUG, "WPS ER: Failed to parse controlURL");
1462 buf = wps_er_soap_hdr(NULL, "GetDeviceInfo", NULL, path, &dst,
1463 &len_ptr, &body_ptr);
1468 wps_er_soap_end(buf, "GetDeviceInfo", len_ptr, body_ptr);
1470 ap->http = http_client_addr(&dst, buf, 10000,
1471 wps_er_http_get_dev_info_cb, ap);
1472 if (ap->http == NULL) {
1477 ap->m1_handler = m1_handler;
1483 int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
1486 struct wps_er_ap *ap;
1491 ap = wps_er_ap_get(er, NULL, uuid);
1493 wpa_printf(MSG_DEBUG, "WPS ER: AP not found for learn "
1498 wpa_printf(MSG_DEBUG, "WPS ER: Pending operation ongoing "
1499 "with the AP - cannot start learn");
1503 if (wps_er_send_get_device_info(ap, wps_er_ap_learn_m1) < 0)
1506 /* TODO: add PIN without SetSelectedRegistrar trigger to all APs */
1507 wps_registrar_add_pin(er->wps->registrar, uuid, pin, pin_len, 0);