wps_er_ssdp.c 4.9 KB

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