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