wps_upnp_ssdp.c revision 281806
1189251Ssam/* 2189251Ssam * UPnP SSDP for WPS 3189251Ssam * Copyright (c) 2000-2003 Intel Corporation 4189251Ssam * Copyright (c) 2006-2007 Sony Corporation 5189251Ssam * Copyright (c) 2008-2009 Atheros Communications 6281806Srpaulo * Copyright (c) 2009-2013, Jouni Malinen <j@w1.fi> 7189251Ssam * 8189251Ssam * See wps_upnp.c for more details on licensing and code history. 9189251Ssam */ 10189251Ssam 11189251Ssam#include "includes.h" 12189251Ssam 13189251Ssam#include <fcntl.h> 14189251Ssam#include <sys/ioctl.h> 15189251Ssam#include <net/route.h> 16281806Srpaulo#ifdef __linux__ 17281806Srpaulo#include <net/if.h> 18281806Srpaulo#endif /* __linux__ */ 19189251Ssam 20189251Ssam#include "common.h" 21189251Ssam#include "uuid.h" 22189251Ssam#include "eloop.h" 23189251Ssam#include "wps.h" 24189251Ssam#include "wps_upnp.h" 25189251Ssam#include "wps_upnp_i.h" 26189251Ssam 27189251Ssam#define UPNP_CACHE_SEC (UPNP_CACHE_SEC_MIN + 1) /* cache time we use */ 28189251Ssam#define UPNP_CACHE_SEC_MIN 1800 /* min cachable time per UPnP standard */ 29189251Ssam#define UPNP_ADVERTISE_REPEAT 2 /* no more than 3 */ 30189251Ssam#define MAX_MSEARCH 20 /* max simultaneous M-SEARCH replies ongoing */ 31189251Ssam#define SSDP_TARGET "239.0.0.0" 32189251Ssam#define SSDP_NETMASK "255.0.0.0" 33189251Ssam 34189251Ssam 35189251Ssam/* Check tokens for equality, where tokens consist of letters, digits, 36189251Ssam * underscore and hyphen, and are matched case insensitive. 37189251Ssam */ 38189251Ssamstatic int token_eq(const char *s1, const char *s2) 39189251Ssam{ 40189251Ssam int c1; 41189251Ssam int c2; 42189251Ssam int end1 = 0; 43189251Ssam int end2 = 0; 44189251Ssam for (;;) { 45189251Ssam c1 = *s1++; 46189251Ssam c2 = *s2++; 47189251Ssam if (isalpha(c1) && isupper(c1)) 48189251Ssam c1 = tolower(c1); 49189251Ssam if (isalpha(c2) && isupper(c2)) 50189251Ssam c2 = tolower(c2); 51189251Ssam end1 = !(isalnum(c1) || c1 == '_' || c1 == '-'); 52189251Ssam end2 = !(isalnum(c2) || c2 == '_' || c2 == '-'); 53189251Ssam if (end1 || end2 || c1 != c2) 54189251Ssam break; 55189251Ssam } 56189251Ssam return end1 && end2; /* reached end of both words? */ 57189251Ssam} 58189251Ssam 59189251Ssam 60189251Ssam/* Return length of token (see above for definition of token) */ 61189251Ssamstatic int token_length(const char *s) 62189251Ssam{ 63189251Ssam const char *begin = s; 64189251Ssam for (;; s++) { 65189251Ssam int c = *s; 66189251Ssam int end = !(isalnum(c) || c == '_' || c == '-'); 67189251Ssam if (end) 68189251Ssam break; 69189251Ssam } 70189251Ssam return s - begin; 71189251Ssam} 72189251Ssam 73189251Ssam 74189251Ssam/* return length of interword separation. 75189251Ssam * This accepts only spaces/tabs and thus will not traverse a line 76189251Ssam * or buffer ending. 77189251Ssam */ 78189251Ssamstatic int word_separation_length(const char *s) 79189251Ssam{ 80189251Ssam const char *begin = s; 81189251Ssam for (;; s++) { 82189251Ssam int c = *s; 83189251Ssam if (c == ' ' || c == '\t') 84189251Ssam continue; 85189251Ssam break; 86189251Ssam } 87189251Ssam return s - begin; 88189251Ssam} 89189251Ssam 90189251Ssam 91189251Ssam/* No. of chars through (including) end of line */ 92189251Ssamstatic int line_length(const char *l) 93189251Ssam{ 94189251Ssam const char *lp = l; 95189251Ssam while (*lp && *lp != '\n') 96189251Ssam lp++; 97189251Ssam if (*lp == '\n') 98189251Ssam lp++; 99189251Ssam return lp - l; 100189251Ssam} 101189251Ssam 102189251Ssam 103189251Ssamstatic int str_starts(const char *str, const char *start) 104189251Ssam{ 105189251Ssam return os_strncmp(str, start, os_strlen(start)) == 0; 106189251Ssam} 107189251Ssam 108189251Ssam 109189251Ssam/*************************************************************************** 110189251Ssam * Advertisements. 111189251Ssam * These are multicast to the world to tell them we are here. 112189251Ssam * The individual packets are spread out in time to limit loss, 113189251Ssam * and then after a much longer period of time the whole sequence 114189251Ssam * is repeated again (for NOTIFYs only). 115189251Ssam **************************************************************************/ 116189251Ssam 117189251Ssam/** 118189251Ssam * next_advertisement - Build next message and advance the state machine 119189251Ssam * @a: Advertisement state 120189251Ssam * @islast: Buffer for indicating whether this is the last message (= 1) 121189251Ssam * Returns: The new message (caller is responsible for freeing this) 122189251Ssam * 123189251Ssam * Note: next_advertisement is shared code with msearchreply_* functions 124189251Ssam */ 125189251Ssamstatic struct wpabuf * 126214734Srpaulonext_advertisement(struct upnp_wps_device_sm *sm, 127214734Srpaulo struct advertisement_state_machine *a, int *islast) 128189251Ssam{ 129189251Ssam struct wpabuf *msg; 130189251Ssam char *NTString = ""; 131189251Ssam char uuid_string[80]; 132252726Srpaulo struct upnp_wps_device_interface *iface; 133189251Ssam 134189251Ssam *islast = 0; 135252726Srpaulo iface = dl_list_first(&sm->interfaces, 136252726Srpaulo struct upnp_wps_device_interface, list); 137281806Srpaulo if (!iface) 138281806Srpaulo return NULL; 139252726Srpaulo uuid_bin2str(iface->wps->uuid, uuid_string, sizeof(uuid_string)); 140189251Ssam msg = wpabuf_alloc(800); /* more than big enough */ 141189251Ssam if (msg == NULL) 142189251Ssam goto fail; 143189251Ssam switch (a->type) { 144189251Ssam case ADVERTISE_UP: 145189251Ssam case ADVERTISE_DOWN: 146189251Ssam NTString = "NT"; 147189251Ssam wpabuf_put_str(msg, "NOTIFY * HTTP/1.1\r\n"); 148189251Ssam wpabuf_printf(msg, "HOST: %s:%d\r\n", 149189251Ssam UPNP_MULTICAST_ADDRESS, UPNP_MULTICAST_PORT); 150189251Ssam wpabuf_printf(msg, "CACHE-CONTROL: max-age=%d\r\n", 151189251Ssam UPNP_CACHE_SEC); 152189251Ssam wpabuf_printf(msg, "NTS: %s\r\n", 153189251Ssam (a->type == ADVERTISE_UP ? 154189251Ssam "ssdp:alive" : "ssdp:byebye")); 155189251Ssam break; 156189251Ssam case MSEARCH_REPLY: 157189251Ssam NTString = "ST"; 158189251Ssam wpabuf_put_str(msg, "HTTP/1.1 200 OK\r\n"); 159189251Ssam wpabuf_printf(msg, "CACHE-CONTROL: max-age=%d\r\n", 160189251Ssam UPNP_CACHE_SEC); 161189251Ssam 162189251Ssam wpabuf_put_str(msg, "DATE: "); 163189251Ssam format_date(msg); 164189251Ssam wpabuf_put_str(msg, "\r\n"); 165189251Ssam 166189251Ssam wpabuf_put_str(msg, "EXT:\r\n"); 167189251Ssam break; 168189251Ssam } 169189251Ssam 170189251Ssam if (a->type != ADVERTISE_DOWN) { 171189251Ssam /* Where others may get our XML files from */ 172189251Ssam wpabuf_printf(msg, "LOCATION: http://%s:%d/%s\r\n", 173214734Srpaulo sm->ip_addr_text, sm->web_port, 174189251Ssam UPNP_WPS_DEVICE_XML_FILE); 175189251Ssam } 176189251Ssam 177189251Ssam /* The SERVER line has three comma-separated fields: 178189251Ssam * operating system / version 179189251Ssam * upnp version 180189251Ssam * software package / version 181189251Ssam * However, only the UPnP version is really required, the 182189251Ssam * others can be place holders... for security reasons 183189251Ssam * it is better to NOT provide extra information. 184189251Ssam */ 185189251Ssam wpabuf_put_str(msg, "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"); 186189251Ssam 187189251Ssam switch (a->state / UPNP_ADVERTISE_REPEAT) { 188189251Ssam case 0: 189189251Ssam wpabuf_printf(msg, "%s: upnp:rootdevice\r\n", NTString); 190189251Ssam wpabuf_printf(msg, "USN: uuid:%s::upnp:rootdevice\r\n", 191189251Ssam uuid_string); 192189251Ssam break; 193189251Ssam case 1: 194189251Ssam wpabuf_printf(msg, "%s: uuid:%s\r\n", NTString, uuid_string); 195189251Ssam wpabuf_printf(msg, "USN: uuid:%s\r\n", uuid_string); 196189251Ssam break; 197189251Ssam case 2: 198189251Ssam wpabuf_printf(msg, "%s: urn:schemas-wifialliance-org:device:" 199189251Ssam "WFADevice:1\r\n", NTString); 200189251Ssam wpabuf_printf(msg, "USN: uuid:%s::urn:schemas-wifialliance-" 201189251Ssam "org:device:WFADevice:1\r\n", uuid_string); 202189251Ssam break; 203189251Ssam case 3: 204189251Ssam wpabuf_printf(msg, "%s: urn:schemas-wifialliance-org:service:" 205189251Ssam "WFAWLANConfig:1\r\n", NTString); 206189251Ssam wpabuf_printf(msg, "USN: uuid:%s::urn:schemas-wifialliance-" 207189251Ssam "org:service:WFAWLANConfig:1\r\n", uuid_string); 208189251Ssam break; 209189251Ssam } 210189251Ssam wpabuf_put_str(msg, "\r\n"); 211189251Ssam 212189251Ssam if (a->state + 1 >= 4 * UPNP_ADVERTISE_REPEAT) 213189251Ssam *islast = 1; 214189251Ssam 215189251Ssam return msg; 216189251Ssam 217189251Ssamfail: 218189251Ssam wpabuf_free(msg); 219189251Ssam return NULL; 220189251Ssam} 221189251Ssam 222189251Ssam 223189251Ssamstatic void advertisement_state_machine_handler(void *eloop_data, 224189251Ssam void *user_ctx); 225189251Ssam 226189251Ssam 227189251Ssam/** 228189251Ssam * advertisement_state_machine_stop - Stop SSDP advertisements 229189251Ssam * @sm: WPS UPnP state machine from upnp_wps_device_init() 230209158Srpaulo * @send_byebye: Send byebye advertisement messages immediately 231189251Ssam */ 232209158Srpaulovoid advertisement_state_machine_stop(struct upnp_wps_device_sm *sm, 233209158Srpaulo int send_byebye) 234189251Ssam{ 235209158Srpaulo struct advertisement_state_machine *a = &sm->advertisement; 236209158Srpaulo int islast = 0; 237209158Srpaulo struct wpabuf *msg; 238209158Srpaulo struct sockaddr_in dest; 239209158Srpaulo 240189251Ssam eloop_cancel_timeout(advertisement_state_machine_handler, NULL, sm); 241209158Srpaulo if (!send_byebye || sm->multicast_sd < 0) 242209158Srpaulo return; 243209158Srpaulo 244209158Srpaulo a->type = ADVERTISE_DOWN; 245209158Srpaulo a->state = 0; 246209158Srpaulo 247209158Srpaulo os_memset(&dest, 0, sizeof(dest)); 248209158Srpaulo dest.sin_family = AF_INET; 249209158Srpaulo dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS); 250209158Srpaulo dest.sin_port = htons(UPNP_MULTICAST_PORT); 251209158Srpaulo 252209158Srpaulo while (!islast) { 253214734Srpaulo msg = next_advertisement(sm, a, &islast); 254209158Srpaulo if (msg == NULL) 255209158Srpaulo break; 256209158Srpaulo if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 257209158Srpaulo 0, (struct sockaddr *) &dest, sizeof(dest)) < 0) { 258209158Srpaulo wpa_printf(MSG_INFO, "WPS UPnP: Advertisement sendto " 259209158Srpaulo "failed: %d (%s)", errno, strerror(errno)); 260209158Srpaulo } 261209158Srpaulo wpabuf_free(msg); 262209158Srpaulo a->state++; 263209158Srpaulo } 264189251Ssam} 265189251Ssam 266189251Ssam 267189251Ssamstatic void advertisement_state_machine_handler(void *eloop_data, 268189251Ssam void *user_ctx) 269189251Ssam{ 270189251Ssam struct upnp_wps_device_sm *sm = user_ctx; 271189251Ssam struct advertisement_state_machine *a = &sm->advertisement; 272189251Ssam struct wpabuf *msg; 273189251Ssam int next_timeout_msec = 100; 274189251Ssam int next_timeout_sec = 0; 275189251Ssam struct sockaddr_in dest; 276189251Ssam int islast = 0; 277189251Ssam 278189251Ssam /* 279189251Ssam * Each is sent twice (in case lost) w/ 100 msec delay between; 280189251Ssam * spec says no more than 3 times. 281189251Ssam * One pair for rootdevice, one pair for uuid, and a pair each for 282189251Ssam * each of the two urns. 283189251Ssam * The entire sequence must be repeated before cache control timeout 284189251Ssam * (which is min 1800 seconds), 285189251Ssam * recommend random portion of half of the advertised cache control age 286189251Ssam * to ensure against loss... perhaps 1800/4 + rand*1800/4 ? 287189251Ssam * Delay random interval < 100 msec prior to initial sending. 288189251Ssam * TTL of 4 289189251Ssam */ 290189251Ssam 291189251Ssam wpa_printf(MSG_MSGDUMP, "WPS UPnP: Advertisement state=%d", a->state); 292214734Srpaulo msg = next_advertisement(sm, a, &islast); 293189251Ssam if (msg == NULL) 294189251Ssam return; 295189251Ssam 296189251Ssam os_memset(&dest, 0, sizeof(dest)); 297189251Ssam dest.sin_family = AF_INET; 298189251Ssam dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS); 299189251Ssam dest.sin_port = htons(UPNP_MULTICAST_PORT); 300189251Ssam 301189251Ssam if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0, 302189251Ssam (struct sockaddr *) &dest, sizeof(dest)) == -1) { 303189251Ssam wpa_printf(MSG_ERROR, "WPS UPnP: Advertisement sendto failed:" 304189251Ssam "%d (%s)", errno, strerror(errno)); 305189251Ssam next_timeout_msec = 0; 306189251Ssam next_timeout_sec = 10; /* ... later */ 307189251Ssam } else if (islast) { 308189251Ssam a->state = 0; /* wrap around */ 309189251Ssam if (a->type == ADVERTISE_DOWN) { 310189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: ADVERTISE_DOWN->UP"); 311189251Ssam a->type = ADVERTISE_UP; 312189251Ssam /* do it all over again right away */ 313189251Ssam } else { 314189251Ssam u16 r; 315189251Ssam /* 316189251Ssam * Start over again after a long timeout 317189251Ssam * (see notes above) 318189251Ssam */ 319189251Ssam next_timeout_msec = 0; 320281806Srpaulo if (os_get_random((void *) &r, sizeof(r)) < 0) 321281806Srpaulo r = 32768; 322189251Ssam next_timeout_sec = UPNP_CACHE_SEC / 4 + 323189251Ssam (((UPNP_CACHE_SEC / 4) * r) >> 16); 324189251Ssam sm->advertise_count++; 325189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: ADVERTISE_UP (#%u); " 326189251Ssam "next in %d sec", 327189251Ssam sm->advertise_count, next_timeout_sec); 328189251Ssam } 329189251Ssam } else { 330189251Ssam a->state++; 331189251Ssam } 332189251Ssam 333189251Ssam wpabuf_free(msg); 334189251Ssam 335189251Ssam eloop_register_timeout(next_timeout_sec, next_timeout_msec, 336189251Ssam advertisement_state_machine_handler, NULL, sm); 337189251Ssam} 338189251Ssam 339189251Ssam 340189251Ssam/** 341189251Ssam * advertisement_state_machine_start - Start SSDP advertisements 342189251Ssam * @sm: WPS UPnP state machine from upnp_wps_device_init() 343189251Ssam * Returns: 0 on success, -1 on failure 344189251Ssam */ 345189251Ssamint advertisement_state_machine_start(struct upnp_wps_device_sm *sm) 346189251Ssam{ 347189251Ssam struct advertisement_state_machine *a = &sm->advertisement; 348189251Ssam int next_timeout_msec; 349189251Ssam 350209158Srpaulo advertisement_state_machine_stop(sm, 0); 351189251Ssam 352189251Ssam /* 353189251Ssam * Start out advertising down, this automatically switches 354189251Ssam * to advertising up which signals our restart. 355189251Ssam */ 356189251Ssam a->type = ADVERTISE_DOWN; 357189251Ssam a->state = 0; 358189251Ssam /* (other fields not used here) */ 359189251Ssam 360189251Ssam /* First timeout should be random interval < 100 msec */ 361189251Ssam next_timeout_msec = (100 * (os_random() & 0xFF)) >> 8; 362189251Ssam return eloop_register_timeout(0, next_timeout_msec, 363189251Ssam advertisement_state_machine_handler, 364189251Ssam NULL, sm); 365189251Ssam} 366189251Ssam 367189251Ssam 368189251Ssam/*************************************************************************** 369189251Ssam * M-SEARCH replies 370189251Ssam * These are very similar to the multicast advertisements, with some 371189251Ssam * small changes in data content; and they are sent (UDP) to a specific 372189251Ssam * unicast address instead of multicast. 373189251Ssam * They are sent in response to a UDP M-SEARCH packet. 374189251Ssam **************************************************************************/ 375189251Ssam 376189251Ssam/** 377189251Ssam * msearchreply_state_machine_stop - Stop M-SEARCH reply state machine 378189251Ssam * @a: Selected advertisement/reply state 379189251Ssam */ 380189251Ssamvoid msearchreply_state_machine_stop(struct advertisement_state_machine *a) 381189251Ssam{ 382189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH stop"); 383214734Srpaulo dl_list_del(&a->list); 384189251Ssam os_free(a); 385189251Ssam} 386189251Ssam 387189251Ssam 388189251Ssamstatic void msearchreply_state_machine_handler(void *eloop_data, 389189251Ssam void *user_ctx) 390189251Ssam{ 391189251Ssam struct advertisement_state_machine *a = user_ctx; 392214734Srpaulo struct upnp_wps_device_sm *sm = eloop_data; 393189251Ssam struct wpabuf *msg; 394189251Ssam int next_timeout_msec = 100; 395189251Ssam int next_timeout_sec = 0; 396189251Ssam int islast = 0; 397189251Ssam 398189251Ssam /* 399189251Ssam * Each response is sent twice (in case lost) w/ 100 msec delay 400189251Ssam * between; spec says no more than 3 times. 401189251Ssam * One pair for rootdevice, one pair for uuid, and a pair each for 402189251Ssam * each of the two urns. 403189251Ssam */ 404189251Ssam 405189251Ssam /* TODO: should only send the requested response types */ 406189251Ssam 407189251Ssam wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply state=%d (%s:%d)", 408189251Ssam a->state, inet_ntoa(a->client.sin_addr), 409189251Ssam ntohs(a->client.sin_port)); 410214734Srpaulo msg = next_advertisement(sm, a, &islast); 411189251Ssam if (msg == NULL) 412189251Ssam return; 413189251Ssam 414189251Ssam /* 415189251Ssam * Send it on the multicast socket to avoid having to set up another 416189251Ssam * socket. 417189251Ssam */ 418189251Ssam if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0, 419189251Ssam (struct sockaddr *) &a->client, sizeof(a->client)) < 0) { 420189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply sendto " 421189251Ssam "errno %d (%s) for %s:%d", 422189251Ssam errno, strerror(errno), 423189251Ssam inet_ntoa(a->client.sin_addr), 424189251Ssam ntohs(a->client.sin_port)); 425189251Ssam /* Ignore error and hope for the best */ 426189251Ssam } 427189251Ssam wpabuf_free(msg); 428189251Ssam if (islast) { 429189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply done"); 430189251Ssam msearchreply_state_machine_stop(a); 431189251Ssam return; 432189251Ssam } 433189251Ssam a->state++; 434189251Ssam 435189251Ssam wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply in %d.%03d sec", 436189251Ssam next_timeout_sec, next_timeout_msec); 437189251Ssam eloop_register_timeout(next_timeout_sec, next_timeout_msec, 438189251Ssam msearchreply_state_machine_handler, sm, a); 439189251Ssam} 440189251Ssam 441189251Ssam 442189251Ssam/** 443189251Ssam * msearchreply_state_machine_start - Reply to M-SEARCH discovery request 444189251Ssam * @sm: WPS UPnP state machine from upnp_wps_device_init() 445189251Ssam * @client: Client address 446189251Ssam * @mx: Maximum delay in seconds 447189251Ssam * 448189251Ssam * Use TTL of 4 (this was done when socket set up). 449189251Ssam * A response should be given in randomized portion of min(MX,120) seconds 450189251Ssam * 451189251Ssam * UPnP-arch-DeviceArchitecture, 1.2.3: 452189251Ssam * To be found, a device must send a UDP response to the source IP address and 453189251Ssam * port that sent the request to the multicast channel. Devices respond if the 454189251Ssam * ST header of the M-SEARCH request is "ssdp:all", "upnp:rootdevice", "uuid:" 455189251Ssam * followed by a UUID that exactly matches one advertised by the device. 456189251Ssam */ 457189251Ssamstatic void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm, 458189251Ssam struct sockaddr_in *client, 459189251Ssam int mx) 460189251Ssam{ 461189251Ssam struct advertisement_state_machine *a; 462189251Ssam int next_timeout_sec; 463189251Ssam int next_timeout_msec; 464214734Srpaulo int replies; 465189251Ssam 466214734Srpaulo replies = dl_list_len(&sm->msearch_replies); 467189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply start (%d " 468214734Srpaulo "outstanding)", replies); 469214734Srpaulo if (replies >= MAX_MSEARCH) { 470189251Ssam wpa_printf(MSG_INFO, "WPS UPnP: Too many outstanding " 471189251Ssam "M-SEARCH replies"); 472189251Ssam return; 473189251Ssam } 474189251Ssam 475189251Ssam a = os_zalloc(sizeof(*a)); 476189251Ssam if (a == NULL) 477189251Ssam return; 478189251Ssam a->type = MSEARCH_REPLY; 479189251Ssam a->state = 0; 480209158Srpaulo os_memcpy(&a->client, client, sizeof(*client)); 481189251Ssam /* Wait time depending on MX value */ 482189251Ssam next_timeout_msec = (1000 * mx * (os_random() & 0xFF)) >> 8; 483189251Ssam next_timeout_sec = next_timeout_msec / 1000; 484189251Ssam next_timeout_msec = next_timeout_msec % 1000; 485189251Ssam if (eloop_register_timeout(next_timeout_sec, next_timeout_msec, 486189251Ssam msearchreply_state_machine_handler, sm, 487189251Ssam a)) { 488189251Ssam /* No way to recover (from malloc failure) */ 489189251Ssam goto fail; 490189251Ssam } 491189251Ssam /* Remember for future cleanup */ 492214734Srpaulo dl_list_add(&sm->msearch_replies, &a->list); 493189251Ssam return; 494189251Ssam 495189251Ssamfail: 496189251Ssam wpa_printf(MSG_INFO, "WPS UPnP: M-SEARCH reply failure!"); 497189251Ssam eloop_cancel_timeout(msearchreply_state_machine_handler, sm, a); 498189251Ssam os_free(a); 499189251Ssam} 500189251Ssam 501189251Ssam 502189251Ssam/** 503189251Ssam * ssdp_parse_msearch - Process a received M-SEARCH 504189251Ssam * @sm: WPS UPnP state machine from upnp_wps_device_init() 505189251Ssam * @client: Client address 506189251Ssam * @data: NULL terminated M-SEARCH message 507189251Ssam * 508189251Ssam * Given that we have received a header w/ M-SEARCH, act upon it 509189251Ssam * 510189251Ssam * Format of M-SEARCH (case insensitive!): 511189251Ssam * 512189251Ssam * First line must be: 513189251Ssam * M-SEARCH * HTTP/1.1 514189251Ssam * Other lines in arbitrary order: 515189251Ssam * HOST:239.255.255.250:1900 516189251Ssam * ST:<varies -- must match> 517189251Ssam * MAN:"ssdp:discover" 518189251Ssam * MX:<varies> 519189251Ssam * 520189251Ssam * It should be noted that when Microsoft Vista is still learning its IP 521189251Ssam * address, it sends out host lines like: HOST:[FF02::C]:1900 522189251Ssam */ 523189251Ssamstatic void ssdp_parse_msearch(struct upnp_wps_device_sm *sm, 524189251Ssam struct sockaddr_in *client, const char *data) 525189251Ssam{ 526214734Srpaulo#ifndef CONFIG_NO_STDOUT_DEBUG 527189251Ssam const char *start = data; 528214734Srpaulo#endif /* CONFIG_NO_STDOUT_DEBUG */ 529189251Ssam int got_host = 0; 530189251Ssam int got_st = 0, st_match = 0; 531189251Ssam int got_man = 0; 532189251Ssam int got_mx = 0; 533189251Ssam int mx = 0; 534189251Ssam 535189251Ssam /* 536189251Ssam * Skip first line M-SEARCH * HTTP/1.1 537189251Ssam * (perhaps we should check remainder of the line for syntax) 538189251Ssam */ 539189251Ssam data += line_length(data); 540189251Ssam 541189251Ssam /* Parse remaining lines */ 542189251Ssam for (; *data != '\0'; data += line_length(data)) { 543189251Ssam if (token_eq(data, "host")) { 544189251Ssam /* The host line indicates who the packet 545189251Ssam * is addressed to... but do we really care? 546189251Ssam * Note that Microsoft sometimes does funny 547189251Ssam * stuff with the HOST: line. 548189251Ssam */ 549189251Ssam#if 0 /* could be */ 550189251Ssam data += token_length(data); 551189251Ssam data += word_separation_length(data); 552189251Ssam if (*data != ':') 553189251Ssam goto bad; 554189251Ssam data++; 555189251Ssam data += word_separation_length(data); 556189251Ssam /* UPNP_MULTICAST_ADDRESS */ 557189251Ssam if (!str_starts(data, "239.255.255.250")) 558189251Ssam goto bad; 559189251Ssam data += os_strlen("239.255.255.250"); 560189251Ssam if (*data == ':') { 561189251Ssam if (!str_starts(data, ":1900")) 562189251Ssam goto bad; 563189251Ssam } 564189251Ssam#endif /* could be */ 565189251Ssam got_host = 1; 566189251Ssam continue; 567189251Ssam } else if (token_eq(data, "st")) { 568189251Ssam /* There are a number of forms; we look 569189251Ssam * for one that matches our case. 570189251Ssam */ 571189251Ssam got_st = 1; 572189251Ssam data += token_length(data); 573189251Ssam data += word_separation_length(data); 574189251Ssam if (*data != ':') 575189251Ssam continue; 576189251Ssam data++; 577189251Ssam data += word_separation_length(data); 578189251Ssam if (str_starts(data, "ssdp:all")) { 579189251Ssam st_match = 1; 580189251Ssam continue; 581189251Ssam } 582189251Ssam if (str_starts(data, "upnp:rootdevice")) { 583189251Ssam st_match = 1; 584189251Ssam continue; 585189251Ssam } 586189251Ssam if (str_starts(data, "uuid:")) { 587189251Ssam char uuid_string[80]; 588252726Srpaulo struct upnp_wps_device_interface *iface; 589252726Srpaulo iface = dl_list_first( 590252726Srpaulo &sm->interfaces, 591252726Srpaulo struct upnp_wps_device_interface, 592252726Srpaulo list); 593281806Srpaulo if (!iface) 594281806Srpaulo continue; 595189251Ssam data += os_strlen("uuid:"); 596252726Srpaulo uuid_bin2str(iface->wps->uuid, uuid_string, 597189251Ssam sizeof(uuid_string)); 598189251Ssam if (str_starts(data, uuid_string)) 599189251Ssam st_match = 1; 600189251Ssam continue; 601189251Ssam } 602189251Ssam#if 0 603189251Ssam /* FIX: should we really reply to IGD string? */ 604189251Ssam if (str_starts(data, "urn:schemas-upnp-org:device:" 605189251Ssam "InternetGatewayDevice:1")) { 606189251Ssam st_match = 1; 607189251Ssam continue; 608189251Ssam } 609189251Ssam#endif 610189251Ssam if (str_starts(data, "urn:schemas-wifialliance-org:" 611189251Ssam "service:WFAWLANConfig:1")) { 612189251Ssam st_match = 1; 613189251Ssam continue; 614189251Ssam } 615189251Ssam if (str_starts(data, "urn:schemas-wifialliance-org:" 616189251Ssam "device:WFADevice:1")) { 617189251Ssam st_match = 1; 618189251Ssam continue; 619189251Ssam } 620189251Ssam continue; 621189251Ssam } else if (token_eq(data, "man")) { 622189251Ssam data += token_length(data); 623189251Ssam data += word_separation_length(data); 624189251Ssam if (*data != ':') 625189251Ssam continue; 626189251Ssam data++; 627189251Ssam data += word_separation_length(data); 628189251Ssam if (!str_starts(data, "\"ssdp:discover\"")) { 629189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: Unexpected " 630189251Ssam "M-SEARCH man-field"); 631189251Ssam goto bad; 632189251Ssam } 633189251Ssam got_man = 1; 634189251Ssam continue; 635189251Ssam } else if (token_eq(data, "mx")) { 636189251Ssam data += token_length(data); 637189251Ssam data += word_separation_length(data); 638189251Ssam if (*data != ':') 639189251Ssam continue; 640189251Ssam data++; 641189251Ssam data += word_separation_length(data); 642189251Ssam mx = atol(data); 643189251Ssam got_mx = 1; 644189251Ssam continue; 645189251Ssam } 646189251Ssam /* ignore anything else */ 647189251Ssam } 648189251Ssam if (!got_host || !got_st || !got_man || !got_mx || mx < 0) { 649189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: Invalid M-SEARCH: %d %d %d " 650189251Ssam "%d mx=%d", got_host, got_st, got_man, got_mx, mx); 651189251Ssam goto bad; 652189251Ssam } 653189251Ssam if (!st_match) { 654189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored M-SEARCH (no ST " 655189251Ssam "match)"); 656189251Ssam return; 657189251Ssam } 658189251Ssam if (mx > 120) 659189251Ssam mx = 120; /* UPnP-arch-DeviceArchitecture, 1.2.3 */ 660189251Ssam msearchreply_state_machine_start(sm, client, mx); 661189251Ssam return; 662189251Ssam 663189251Ssambad: 664189251Ssam wpa_printf(MSG_INFO, "WPS UPnP: Failed to parse M-SEARCH"); 665189251Ssam wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH data:\n%s", start); 666189251Ssam} 667189251Ssam 668189251Ssam 669189251Ssam/* Listening for (UDP) discovery (M-SEARCH) packets */ 670189251Ssam 671189251Ssam/** 672189251Ssam * ssdp_listener_stop - Stop SSDP listered 673189251Ssam * @sm: WPS UPnP state machine from upnp_wps_device_init() 674189251Ssam * 675214734Srpaulo * This function stops the SSDP listener that was started by calling 676189251Ssam * ssdp_listener_start(). 677189251Ssam */ 678189251Ssamvoid ssdp_listener_stop(struct upnp_wps_device_sm *sm) 679189251Ssam{ 680189251Ssam if (sm->ssdp_sd_registered) { 681189251Ssam eloop_unregister_sock(sm->ssdp_sd, EVENT_TYPE_READ); 682189251Ssam sm->ssdp_sd_registered = 0; 683189251Ssam } 684189251Ssam 685189251Ssam if (sm->ssdp_sd != -1) { 686189251Ssam close(sm->ssdp_sd); 687189251Ssam sm->ssdp_sd = -1; 688189251Ssam } 689189251Ssam 690189251Ssam eloop_cancel_timeout(msearchreply_state_machine_handler, sm, 691189251Ssam ELOOP_ALL_CTX); 692189251Ssam} 693189251Ssam 694189251Ssam 695189251Ssamstatic void ssdp_listener_handler(int sd, void *eloop_ctx, void *sock_ctx) 696189251Ssam{ 697189251Ssam struct upnp_wps_device_sm *sm = sock_ctx; 698189251Ssam struct sockaddr_in addr; /* client address */ 699189251Ssam socklen_t addr_len; 700189251Ssam int nread; 701189251Ssam char buf[MULTICAST_MAX_READ], *pos; 702189251Ssam 703189251Ssam addr_len = sizeof(addr); 704189251Ssam nread = recvfrom(sm->ssdp_sd, buf, sizeof(buf) - 1, 0, 705189251Ssam (struct sockaddr *) &addr, &addr_len); 706189251Ssam if (nread <= 0) 707189251Ssam return; 708189251Ssam buf[nread] = '\0'; /* need null termination for algorithm */ 709189251Ssam 710189251Ssam if (str_starts(buf, "NOTIFY ")) { 711189251Ssam /* 712189251Ssam * Silently ignore NOTIFYs to avoid filling debug log with 713189251Ssam * unwanted messages. 714189251Ssam */ 715189251Ssam return; 716189251Ssam } 717189251Ssam 718189251Ssam pos = os_strchr(buf, '\n'); 719189251Ssam if (pos) 720189251Ssam *pos = '\0'; 721189251Ssam wpa_printf(MSG_MSGDUMP, "WPS UPnP: Received SSDP packet from %s:%d: " 722189251Ssam "%s", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), buf); 723189251Ssam if (pos) 724189251Ssam *pos = '\n'; 725189251Ssam 726189251Ssam /* Parse first line */ 727189251Ssam if (os_strncasecmp(buf, "M-SEARCH", os_strlen("M-SEARCH")) == 0 && 728189251Ssam !isgraph(buf[strlen("M-SEARCH")])) { 729189251Ssam ssdp_parse_msearch(sm, &addr, buf); 730189251Ssam return; 731189251Ssam } 732189251Ssam 733189251Ssam /* Ignore anything else */ 734189251Ssam} 735189251Ssam 736189251Ssam 737214734Srpauloint ssdp_listener_open(void) 738189251Ssam{ 739189251Ssam struct sockaddr_in addr; 740189251Ssam struct ip_mreq mcast_addr; 741189251Ssam int on = 1; 742189251Ssam /* per UPnP spec, keep IP packet time to live (TTL) small */ 743189251Ssam unsigned char ttl = 4; 744214734Srpaulo int sd; 745189251Ssam 746214734Srpaulo sd = socket(AF_INET, SOCK_DGRAM, 0); 747189251Ssam if (sd < 0) 748189251Ssam goto fail; 749189251Ssam if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) 750189251Ssam goto fail; 751189251Ssam if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) 752189251Ssam goto fail; 753189251Ssam os_memset(&addr, 0, sizeof(addr)); 754189251Ssam addr.sin_family = AF_INET; 755189251Ssam addr.sin_addr.s_addr = htonl(INADDR_ANY); 756189251Ssam addr.sin_port = htons(UPNP_MULTICAST_PORT); 757189251Ssam if (bind(sd, (struct sockaddr *) &addr, sizeof(addr))) 758189251Ssam goto fail; 759189251Ssam os_memset(&mcast_addr, 0, sizeof(mcast_addr)); 760189251Ssam mcast_addr.imr_interface.s_addr = htonl(INADDR_ANY); 761189251Ssam mcast_addr.imr_multiaddr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS); 762189251Ssam if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 763189251Ssam (char *) &mcast_addr, sizeof(mcast_addr))) 764189251Ssam goto fail; 765189251Ssam if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, 766189251Ssam &ttl, sizeof(ttl))) 767189251Ssam goto fail; 768214734Srpaulo 769214734Srpaulo return sd; 770214734Srpaulo 771214734Srpaulofail: 772214734Srpaulo if (sd >= 0) 773214734Srpaulo close(sd); 774214734Srpaulo return -1; 775214734Srpaulo} 776214734Srpaulo 777214734Srpaulo 778214734Srpaulo/** 779214734Srpaulo * ssdp_listener_start - Set up for receiving discovery (UDP) packets 780214734Srpaulo * @sm: WPS UPnP state machine from upnp_wps_device_init() 781214734Srpaulo * Returns: 0 on success, -1 on failure 782214734Srpaulo * 783214734Srpaulo * The SSDP listener is stopped by calling ssdp_listener_stop(). 784214734Srpaulo */ 785214734Srpauloint ssdp_listener_start(struct upnp_wps_device_sm *sm) 786214734Srpaulo{ 787214734Srpaulo sm->ssdp_sd = ssdp_listener_open(); 788214734Srpaulo 789214734Srpaulo if (eloop_register_sock(sm->ssdp_sd, EVENT_TYPE_READ, 790214734Srpaulo ssdp_listener_handler, NULL, sm)) 791189251Ssam goto fail; 792189251Ssam sm->ssdp_sd_registered = 1; 793189251Ssam return 0; 794189251Ssam 795189251Ssamfail: 796189251Ssam /* Error */ 797189251Ssam wpa_printf(MSG_ERROR, "WPS UPnP: ssdp_listener_start failed"); 798189251Ssam ssdp_listener_stop(sm); 799189251Ssam return -1; 800189251Ssam} 801189251Ssam 802189251Ssam 803189251Ssam/** 804189251Ssam * add_ssdp_network - Add routing entry for SSDP 805189251Ssam * @net_if: Selected network interface name 806189251Ssam * Returns: 0 on success, -1 on failure 807189251Ssam * 808189251Ssam * This function assures that the multicast address will be properly 809189251Ssam * handled by Linux networking code (by a modification to routing tables). 810189251Ssam * This must be done per network interface. It really only needs to be done 811189251Ssam * once after booting up, but it does not hurt to call this more frequently 812189251Ssam * "to be safe". 813189251Ssam */ 814214734Srpauloint add_ssdp_network(const char *net_if) 815189251Ssam{ 816209158Srpaulo#ifdef __linux__ 817189251Ssam int ret = -1; 818189251Ssam int sock = -1; 819189251Ssam struct rtentry rt; 820189251Ssam struct sockaddr_in *sin; 821189251Ssam 822189251Ssam if (!net_if) 823189251Ssam goto fail; 824189251Ssam 825189251Ssam os_memset(&rt, 0, sizeof(rt)); 826189251Ssam sock = socket(AF_INET, SOCK_DGRAM, 0); 827189251Ssam if (sock < 0) 828189251Ssam goto fail; 829189251Ssam 830214734Srpaulo rt.rt_dev = (char *) net_if; 831209158Srpaulo sin = aliasing_hide_typecast(&rt.rt_dst, struct sockaddr_in); 832189251Ssam sin->sin_family = AF_INET; 833189251Ssam sin->sin_port = 0; 834189251Ssam sin->sin_addr.s_addr = inet_addr(SSDP_TARGET); 835209158Srpaulo sin = aliasing_hide_typecast(&rt.rt_genmask, struct sockaddr_in); 836189251Ssam sin->sin_family = AF_INET; 837189251Ssam sin->sin_port = 0; 838189251Ssam sin->sin_addr.s_addr = inet_addr(SSDP_NETMASK); 839189251Ssam rt.rt_flags = RTF_UP; 840189251Ssam if (ioctl(sock, SIOCADDRT, &rt) < 0) { 841189251Ssam if (errno == EPERM) { 842189251Ssam wpa_printf(MSG_DEBUG, "add_ssdp_network: No " 843189251Ssam "permissions to add routing table entry"); 844189251Ssam /* Continue to allow testing as non-root */ 845189251Ssam } else if (errno != EEXIST) { 846189251Ssam wpa_printf(MSG_INFO, "add_ssdp_network() ioctl errno " 847189251Ssam "%d (%s)", errno, strerror(errno)); 848189251Ssam goto fail; 849189251Ssam } 850189251Ssam } 851189251Ssam 852189251Ssam ret = 0; 853189251Ssam 854189251Ssamfail: 855189251Ssam if (sock >= 0) 856189251Ssam close(sock); 857189251Ssam 858189251Ssam return ret; 859209158Srpaulo#else /* __linux__ */ 860209158Srpaulo return 0; 861209158Srpaulo#endif /* __linux__ */ 862189251Ssam} 863189251Ssam 864189251Ssam 865281806Srpauloint ssdp_open_multicast_sock(u32 ip_addr, const char *forced_ifname) 866189251Ssam{ 867214734Srpaulo int sd; 868189251Ssam /* per UPnP-arch-DeviceArchitecture, 1. Discovery, keep IP packet 869189251Ssam * time to live (TTL) small */ 870189251Ssam unsigned char ttl = 4; 871189251Ssam 872214734Srpaulo sd = socket(AF_INET, SOCK_DGRAM, 0); 873189251Ssam if (sd < 0) 874189251Ssam return -1; 875189251Ssam 876281806Srpaulo if (forced_ifname) { 877281806Srpaulo#ifdef __linux__ 878281806Srpaulo struct ifreq req; 879281806Srpaulo os_memset(&req, 0, sizeof(req)); 880281806Srpaulo os_strlcpy(req.ifr_name, forced_ifname, sizeof(req.ifr_name)); 881281806Srpaulo if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, &req, 882281806Srpaulo sizeof(req)) < 0) { 883281806Srpaulo wpa_printf(MSG_INFO, "WPS UPnP: Failed to bind " 884281806Srpaulo "multicast socket to ifname %s: %s", 885281806Srpaulo forced_ifname, strerror(errno)); 886281806Srpaulo close(sd); 887281806Srpaulo return -1; 888281806Srpaulo } 889281806Srpaulo#endif /* __linux__ */ 890281806Srpaulo } 891281806Srpaulo 892189251Ssam#if 0 /* maybe ok if we sometimes block on writes */ 893252726Srpaulo if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) { 894252726Srpaulo close(sd); 895189251Ssam return -1; 896252726Srpaulo } 897189251Ssam#endif 898189251Ssam 899189251Ssam if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, 900252726Srpaulo &ip_addr, sizeof(ip_addr))) { 901252726Srpaulo wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_IF) %x: " 902252726Srpaulo "%d (%s)", ip_addr, errno, strerror(errno)); 903252726Srpaulo close(sd); 904189251Ssam return -1; 905252726Srpaulo } 906189251Ssam if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, 907252726Srpaulo &ttl, sizeof(ttl))) { 908252726Srpaulo wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_TTL): " 909252726Srpaulo "%d (%s)", errno, strerror(errno)); 910252726Srpaulo close(sd); 911189251Ssam return -1; 912252726Srpaulo } 913189251Ssam 914189251Ssam#if 0 /* not needed, because we don't receive using multicast_sd */ 915189251Ssam { 916189251Ssam struct ip_mreq mreq; 917189251Ssam mreq.imr_multiaddr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS); 918214734Srpaulo mreq.imr_interface.s_addr = ip_addr; 919189251Ssam wpa_printf(MSG_DEBUG, "WPS UPnP: Multicast addr 0x%x if addr " 920189251Ssam "0x%x", 921189251Ssam mreq.imr_multiaddr.s_addr, 922189251Ssam mreq.imr_interface.s_addr); 923189251Ssam if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, 924189251Ssam sizeof(mreq))) { 925189251Ssam wpa_printf(MSG_ERROR, 926189251Ssam "WPS UPnP: setsockopt " 927189251Ssam "IP_ADD_MEMBERSHIP errno %d (%s)", 928189251Ssam errno, strerror(errno)); 929252726Srpaulo close(sd); 930189251Ssam return -1; 931189251Ssam } 932189251Ssam } 933189251Ssam#endif /* not needed */ 934189251Ssam 935189251Ssam /* 936189251Ssam * TODO: What about IP_MULTICAST_LOOP? It seems to be on by default? 937189251Ssam * which aids debugging I suppose but isn't really necessary? 938189251Ssam */ 939189251Ssam 940214734Srpaulo return sd; 941214734Srpaulo} 942214734Srpaulo 943214734Srpaulo 944214734Srpaulo/** 945214734Srpaulo * ssdp_open_multicast - Open socket for sending multicast SSDP messages 946214734Srpaulo * @sm: WPS UPnP state machine from upnp_wps_device_init() 947214734Srpaulo * Returns: 0 on success, -1 on failure 948214734Srpaulo */ 949214734Srpauloint ssdp_open_multicast(struct upnp_wps_device_sm *sm) 950214734Srpaulo{ 951281806Srpaulo sm->multicast_sd = ssdp_open_multicast_sock(sm->ip_addr, NULL); 952214734Srpaulo if (sm->multicast_sd < 0) 953214734Srpaulo return -1; 954189251Ssam return 0; 955189251Ssam} 956