p2p_sd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * Wi-Fi Direct - P2P service discovery
  3. * Copyright (c) 2009, Atheros Communications
  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 "common/ieee802_11_defs.h"
  11. #include "common/gas.h"
  12. #include "p2p_i.h"
  13. #include "p2p.h"
  14. #ifdef CONFIG_WIFI_DISPLAY
  15. static int wfd_wsd_supported(struct wpabuf *wfd)
  16. {
  17. const u8 *pos, *end;
  18. u8 subelem;
  19. u16 len;
  20. if (wfd == NULL)
  21. return 0;
  22. pos = wpabuf_head(wfd);
  23. end = pos + wpabuf_len(wfd);
  24. while (end - pos >= 3) {
  25. subelem = *pos++;
  26. len = WPA_GET_BE16(pos);
  27. pos += 2;
  28. if (len > end - pos)
  29. break;
  30. if (subelem == WFD_SUBELEM_DEVICE_INFO && len >= 6) {
  31. u16 info = WPA_GET_BE16(pos);
  32. return !!(info & 0x0040);
  33. }
  34. pos += len;
  35. }
  36. return 0;
  37. }
  38. #endif /* CONFIG_WIFI_DISPLAY */
  39. struct p2p_sd_query * p2p_pending_sd_req(struct p2p_data *p2p,
  40. struct p2p_device *dev)
  41. {
  42. struct p2p_sd_query *q;
  43. int wsd = 0;
  44. int count = 0;
  45. if (!(dev->info.dev_capab & P2P_DEV_CAPAB_SERVICE_DISCOVERY))
  46. return NULL; /* peer does not support SD */
  47. #ifdef CONFIG_WIFI_DISPLAY
  48. if (wfd_wsd_supported(dev->info.wfd_subelems))
  49. wsd = 1;
  50. #endif /* CONFIG_WIFI_DISPLAY */
  51. for (q = p2p->sd_queries; q; q = q->next) {
  52. /* Use WSD only if the peer indicates support or it */
  53. if (q->wsd && !wsd)
  54. continue;
  55. /* if the query is a broadcast query */
  56. if (q->for_all_peers) {
  57. /*
  58. * check if there are any broadcast queries pending for
  59. * this device
  60. */
  61. if (dev->sd_pending_bcast_queries <= 0)
  62. return NULL;
  63. /* query number that needs to be send to the device */
  64. if (count == dev->sd_pending_bcast_queries - 1)
  65. goto found;
  66. count++;
  67. }
  68. if (!q->for_all_peers &&
  69. os_memcmp(q->peer, dev->info.p2p_device_addr, ETH_ALEN) ==
  70. 0)
  71. goto found;
  72. }
  73. return NULL;
  74. found:
  75. if (dev->sd_reqs > 100) {
  76. p2p_dbg(p2p, "Too many SD request attempts to " MACSTR
  77. " - skip remaining queries",
  78. MAC2STR(dev->info.p2p_device_addr));
  79. return NULL;
  80. }
  81. return q;
  82. }
  83. static void p2p_decrease_sd_bc_queries(struct p2p_data *p2p, int query_number)
  84. {
  85. struct p2p_device *dev;
  86. p2p->num_p2p_sd_queries--;
  87. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  88. if (query_number <= dev->sd_pending_bcast_queries - 1) {
  89. /*
  90. * Query not yet sent to the device and it is to be
  91. * removed, so update the pending count.
  92. */
  93. dev->sd_pending_bcast_queries--;
  94. }
  95. }
  96. }
  97. static int p2p_unlink_sd_query(struct p2p_data *p2p,
  98. struct p2p_sd_query *query)
  99. {
  100. struct p2p_sd_query *q, *prev;
  101. int query_number = 0;
  102. q = p2p->sd_queries;
  103. prev = NULL;
  104. while (q) {
  105. if (q == query) {
  106. /* If the query is a broadcast query, decrease one from
  107. * all the devices */
  108. if (query->for_all_peers)
  109. p2p_decrease_sd_bc_queries(p2p, query_number);
  110. if (prev)
  111. prev->next = q->next;
  112. else
  113. p2p->sd_queries = q->next;
  114. if (p2p->sd_query == query)
  115. p2p->sd_query = NULL;
  116. return 1;
  117. }
  118. if (q->for_all_peers)
  119. query_number++;
  120. prev = q;
  121. q = q->next;
  122. }
  123. return 0;
  124. }
  125. static void p2p_free_sd_query(struct p2p_sd_query *q)
  126. {
  127. if (q == NULL)
  128. return;
  129. wpabuf_free(q->tlvs);
  130. os_free(q);
  131. }
  132. void p2p_free_sd_queries(struct p2p_data *p2p)
  133. {
  134. struct p2p_sd_query *q, *prev;
  135. q = p2p->sd_queries;
  136. p2p->sd_queries = NULL;
  137. while (q) {
  138. prev = q;
  139. q = q->next;
  140. p2p_free_sd_query(prev);
  141. }
  142. p2p->num_p2p_sd_queries = 0;
  143. }
  144. static struct wpabuf * p2p_build_sd_query(u16 update_indic,
  145. struct wpabuf *tlvs)
  146. {
  147. struct wpabuf *buf;
  148. u8 *len_pos;
  149. buf = gas_anqp_build_initial_req(0, 100 + wpabuf_len(tlvs));
  150. if (buf == NULL)
  151. return NULL;
  152. /* ANQP Query Request Frame */
  153. len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  154. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  155. wpabuf_put_le16(buf, update_indic); /* Service Update Indicator */
  156. wpabuf_put_buf(buf, tlvs);
  157. gas_anqp_set_element_len(buf, len_pos);
  158. gas_anqp_set_len(buf);
  159. return buf;
  160. }
  161. static void p2p_send_gas_comeback_req(struct p2p_data *p2p, const u8 *dst,
  162. u8 dialog_token, int freq)
  163. {
  164. struct wpabuf *req;
  165. req = gas_build_comeback_req(dialog_token);
  166. if (req == NULL)
  167. return;
  168. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  169. if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr, dst,
  170. wpabuf_head(req), wpabuf_len(req), 200) < 0)
  171. p2p_dbg(p2p, "Failed to send Action frame");
  172. wpabuf_free(req);
  173. }
  174. static struct wpabuf * p2p_build_sd_response(u8 dialog_token, u16 status_code,
  175. u16 comeback_delay,
  176. u16 update_indic,
  177. const struct wpabuf *tlvs)
  178. {
  179. struct wpabuf *buf;
  180. u8 *len_pos;
  181. buf = gas_anqp_build_initial_resp(dialog_token, status_code,
  182. comeback_delay,
  183. 100 + (tlvs ? wpabuf_len(tlvs) : 0));
  184. if (buf == NULL)
  185. return NULL;
  186. if (tlvs) {
  187. /* ANQP Query Response Frame */
  188. len_pos = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  189. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  190. /* Service Update Indicator */
  191. wpabuf_put_le16(buf, update_indic);
  192. wpabuf_put_buf(buf, tlvs);
  193. gas_anqp_set_element_len(buf, len_pos);
  194. }
  195. gas_anqp_set_len(buf);
  196. return buf;
  197. }
  198. static struct wpabuf * p2p_build_gas_comeback_resp(u8 dialog_token,
  199. u16 status_code,
  200. u16 update_indic,
  201. const u8 *data, size_t len,
  202. u8 frag_id, u8 more,
  203. u16 total_len)
  204. {
  205. struct wpabuf *buf;
  206. buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
  207. more, 0, 100 + len);
  208. if (buf == NULL)
  209. return NULL;
  210. if (frag_id == 0) {
  211. /* ANQP Query Response Frame */
  212. wpabuf_put_le16(buf, ANQP_VENDOR_SPECIFIC); /* Info ID */
  213. wpabuf_put_le16(buf, 3 + 1 + 2 + total_len);
  214. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  215. /* Service Update Indicator */
  216. wpabuf_put_le16(buf, update_indic);
  217. }
  218. wpabuf_put_data(buf, data, len);
  219. gas_anqp_set_len(buf);
  220. return buf;
  221. }
  222. int p2p_start_sd(struct p2p_data *p2p, struct p2p_device *dev)
  223. {
  224. struct wpabuf *req;
  225. int ret = 0;
  226. struct p2p_sd_query *query;
  227. int freq;
  228. unsigned int wait_time;
  229. freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
  230. if (freq <= 0) {
  231. p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
  232. MACSTR " to send SD Request",
  233. MAC2STR(dev->info.p2p_device_addr));
  234. return -1;
  235. }
  236. query = p2p_pending_sd_req(p2p, dev);
  237. if (query == NULL)
  238. return -1;
  239. if (p2p->state == P2P_SEARCH &&
  240. os_memcmp(p2p->sd_query_no_ack, dev->info.p2p_device_addr,
  241. ETH_ALEN) == 0) {
  242. p2p_dbg(p2p, "Do not start Service Discovery with " MACSTR
  243. " due to it being the first no-ACK peer in this search iteration",
  244. MAC2STR(dev->info.p2p_device_addr));
  245. return -2;
  246. }
  247. p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
  248. MAC2STR(dev->info.p2p_device_addr));
  249. req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
  250. if (req == NULL)
  251. return -1;
  252. dev->sd_reqs++;
  253. p2p->sd_peer = dev;
  254. p2p->sd_query = query;
  255. p2p->pending_action_state = P2P_PENDING_SD;
  256. wait_time = 5000;
  257. if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
  258. wait_time = p2p->cfg->max_listen;
  259. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  260. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  261. wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
  262. p2p_dbg(p2p, "Failed to send Action frame");
  263. ret = -1;
  264. }
  265. wpabuf_free(req);
  266. return ret;
  267. }
  268. void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
  269. const u8 *data, size_t len, int rx_freq)
  270. {
  271. const u8 *pos = data;
  272. const u8 *end = data + len;
  273. const u8 *next;
  274. u8 dialog_token;
  275. u16 slen;
  276. int freq;
  277. u16 update_indic;
  278. if (p2p->cfg->sd_request == NULL)
  279. return;
  280. if (rx_freq > 0)
  281. freq = rx_freq;
  282. else
  283. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  284. p2p->cfg->channel);
  285. if (freq < 0)
  286. return;
  287. if (len < 1 + 2)
  288. return;
  289. dialog_token = *pos++;
  290. p2p_dbg(p2p, "GAS Initial Request from " MACSTR
  291. " (dialog token %u, freq %d)",
  292. MAC2STR(sa), dialog_token, rx_freq);
  293. if (*pos != WLAN_EID_ADV_PROTO) {
  294. p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
  295. return;
  296. }
  297. pos++;
  298. slen = *pos++;
  299. if (slen > end - pos || slen < 2) {
  300. p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
  301. return;
  302. }
  303. next = pos + slen;
  304. pos++; /* skip QueryRespLenLimit and PAME-BI */
  305. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  306. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  307. *pos);
  308. return;
  309. }
  310. pos = next;
  311. /* Query Request */
  312. if (end - pos < 2)
  313. return;
  314. slen = WPA_GET_LE16(pos);
  315. pos += 2;
  316. if (slen > end - pos)
  317. return;
  318. end = pos + slen;
  319. /* ANQP Query Request */
  320. if (end - pos < 4)
  321. return;
  322. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  323. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  324. return;
  325. }
  326. pos += 2;
  327. slen = WPA_GET_LE16(pos);
  328. pos += 2;
  329. if (slen > end - pos || slen < 3 + 1) {
  330. p2p_dbg(p2p, "Invalid ANQP Query Request length");
  331. return;
  332. }
  333. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  334. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  335. WPA_GET_BE32(pos));
  336. return;
  337. }
  338. pos += 4;
  339. if (end - pos < 2)
  340. return;
  341. update_indic = WPA_GET_LE16(pos);
  342. p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
  343. pos += 2;
  344. p2p->cfg->sd_request(p2p->cfg->cb_ctx, freq, sa, dialog_token,
  345. update_indic, pos, end - pos);
  346. /* the response will be indicated with a call to p2p_sd_response() */
  347. }
  348. void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
  349. u8 dialog_token, const struct wpabuf *resp_tlvs)
  350. {
  351. struct wpabuf *resp;
  352. /* TODO: fix the length limit to match with the maximum frame length */
  353. if (wpabuf_len(resp_tlvs) > 1400) {
  354. p2p_dbg(p2p, "SD response long enough to require fragmentation");
  355. if (p2p->sd_resp) {
  356. /*
  357. * TODO: Could consider storing the fragmented response
  358. * separately for each peer to avoid having to drop old
  359. * one if there is more than one pending SD query.
  360. * Though, that would eat more memory, so there are
  361. * also benefits to just using a single buffer.
  362. */
  363. p2p_dbg(p2p, "Drop previous SD response");
  364. wpabuf_free(p2p->sd_resp);
  365. }
  366. p2p->sd_resp = wpabuf_dup(resp_tlvs);
  367. if (p2p->sd_resp == NULL) {
  368. p2p_err(p2p, "Failed to allocate SD response fragmentation area");
  369. return;
  370. }
  371. os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
  372. p2p->sd_resp_dialog_token = dialog_token;
  373. p2p->sd_resp_pos = 0;
  374. p2p->sd_frag_id = 0;
  375. resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
  376. 1, p2p->srv_update_indic, NULL);
  377. } else {
  378. p2p_dbg(p2p, "SD response fits in initial response");
  379. resp = p2p_build_sd_response(dialog_token,
  380. WLAN_STATUS_SUCCESS, 0,
  381. p2p->srv_update_indic, resp_tlvs);
  382. }
  383. if (resp == NULL)
  384. return;
  385. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  386. if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
  387. p2p->cfg->dev_addr,
  388. wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
  389. p2p_dbg(p2p, "Failed to send Action frame");
  390. wpabuf_free(resp);
  391. }
  392. void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
  393. const u8 *data, size_t len, int rx_freq)
  394. {
  395. const u8 *pos = data;
  396. const u8 *end = data + len;
  397. const u8 *next;
  398. u8 dialog_token;
  399. u16 status_code;
  400. u16 comeback_delay;
  401. u16 slen;
  402. u16 update_indic;
  403. if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
  404. os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
  405. p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
  406. MACSTR, MAC2STR(sa));
  407. return;
  408. }
  409. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  410. p2p_clear_timeout(p2p);
  411. p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
  412. MAC2STR(sa), (int) len);
  413. if (len < 5 + 2) {
  414. p2p_dbg(p2p, "Too short GAS Initial Response frame");
  415. return;
  416. }
  417. dialog_token = *pos++;
  418. /* TODO: check dialog_token match */
  419. status_code = WPA_GET_LE16(pos);
  420. pos += 2;
  421. comeback_delay = WPA_GET_LE16(pos);
  422. pos += 2;
  423. p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
  424. dialog_token, status_code, comeback_delay);
  425. if (status_code) {
  426. p2p_dbg(p2p, "Service Discovery failed: status code %u",
  427. status_code);
  428. return;
  429. }
  430. if (*pos != WLAN_EID_ADV_PROTO) {
  431. p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
  432. return;
  433. }
  434. pos++;
  435. slen = *pos++;
  436. if (slen > end - pos || slen < 2) {
  437. p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
  438. return;
  439. }
  440. next = pos + slen;
  441. pos++; /* skip QueryRespLenLimit and PAME-BI */
  442. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  443. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  444. *pos);
  445. return;
  446. }
  447. pos = next;
  448. /* Query Response */
  449. if (end - pos < 2) {
  450. p2p_dbg(p2p, "Too short Query Response");
  451. return;
  452. }
  453. slen = WPA_GET_LE16(pos);
  454. pos += 2;
  455. p2p_dbg(p2p, "Query Response Length: %d", slen);
  456. if (slen > end - pos) {
  457. p2p_dbg(p2p, "Not enough Query Response data");
  458. return;
  459. }
  460. end = pos + slen;
  461. if (comeback_delay) {
  462. p2p_dbg(p2p, "Fragmented response - request fragments");
  463. if (p2p->sd_rx_resp) {
  464. p2p_dbg(p2p, "Drop old SD reassembly buffer");
  465. wpabuf_free(p2p->sd_rx_resp);
  466. p2p->sd_rx_resp = NULL;
  467. }
  468. p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
  469. return;
  470. }
  471. /* ANQP Query Response */
  472. if (end - pos < 4)
  473. return;
  474. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  475. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  476. return;
  477. }
  478. pos += 2;
  479. slen = WPA_GET_LE16(pos);
  480. pos += 2;
  481. if (slen > end - pos || slen < 3 + 1) {
  482. p2p_dbg(p2p, "Invalid ANQP Query Response length");
  483. return;
  484. }
  485. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  486. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  487. WPA_GET_BE32(pos));
  488. return;
  489. }
  490. pos += 4;
  491. if (end - pos < 2)
  492. return;
  493. update_indic = WPA_GET_LE16(pos);
  494. p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
  495. pos += 2;
  496. p2p->sd_peer = NULL;
  497. if (p2p->sd_query) {
  498. if (!p2p->sd_query->for_all_peers) {
  499. struct p2p_sd_query *q;
  500. p2p_dbg(p2p, "Remove completed SD query %p",
  501. p2p->sd_query);
  502. q = p2p->sd_query;
  503. p2p_unlink_sd_query(p2p, p2p->sd_query);
  504. p2p_free_sd_query(q);
  505. }
  506. p2p->sd_query = NULL;
  507. }
  508. if (p2p->cfg->sd_response)
  509. p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
  510. pos, end - pos);
  511. p2p_continue_find(p2p);
  512. }
  513. void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
  514. const u8 *data, size_t len, int rx_freq)
  515. {
  516. struct wpabuf *resp;
  517. u8 dialog_token;
  518. size_t frag_len;
  519. int more = 0;
  520. wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
  521. if (len < 1)
  522. return;
  523. dialog_token = *data;
  524. p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
  525. if (dialog_token != p2p->sd_resp_dialog_token) {
  526. p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
  527. dialog_token);
  528. return;
  529. }
  530. if (p2p->sd_resp == NULL) {
  531. p2p_dbg(p2p, "No pending SD response fragment available");
  532. return;
  533. }
  534. if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
  535. p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
  536. MAC2STR(sa));
  537. return;
  538. }
  539. frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
  540. if (frag_len > 1400) {
  541. frag_len = 1400;
  542. more = 1;
  543. }
  544. resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
  545. p2p->srv_update_indic,
  546. wpabuf_head_u8(p2p->sd_resp) +
  547. p2p->sd_resp_pos, frag_len,
  548. p2p->sd_frag_id, more,
  549. wpabuf_len(p2p->sd_resp));
  550. if (resp == NULL)
  551. return;
  552. p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
  553. p2p->sd_frag_id, more, (int) frag_len);
  554. p2p->sd_frag_id++;
  555. p2p->sd_resp_pos += frag_len;
  556. if (more) {
  557. p2p_dbg(p2p, "%d more bytes remain to be sent",
  558. (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
  559. } else {
  560. p2p_dbg(p2p, "All fragments of SD response sent");
  561. wpabuf_free(p2p->sd_resp);
  562. p2p->sd_resp = NULL;
  563. }
  564. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  565. if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
  566. p2p->cfg->dev_addr,
  567. wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
  568. p2p_dbg(p2p, "Failed to send Action frame");
  569. wpabuf_free(resp);
  570. }
  571. void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
  572. const u8 *data, size_t len, int rx_freq)
  573. {
  574. const u8 *pos = data;
  575. const u8 *end = data + len;
  576. const u8 *next;
  577. u8 dialog_token;
  578. u16 status_code;
  579. u8 frag_id;
  580. u8 more_frags;
  581. u16 comeback_delay;
  582. u16 slen;
  583. wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
  584. if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
  585. os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
  586. p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
  587. MACSTR, MAC2STR(sa));
  588. return;
  589. }
  590. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  591. p2p_clear_timeout(p2p);
  592. p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
  593. MAC2STR(sa), (int) len);
  594. if (len < 6 + 2) {
  595. p2p_dbg(p2p, "Too short GAS Comeback Response frame");
  596. return;
  597. }
  598. dialog_token = *pos++;
  599. /* TODO: check dialog_token match */
  600. status_code = WPA_GET_LE16(pos);
  601. pos += 2;
  602. frag_id = *pos & 0x7f;
  603. more_frags = (*pos & 0x80) >> 7;
  604. pos++;
  605. comeback_delay = WPA_GET_LE16(pos);
  606. pos += 2;
  607. p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
  608. "comeback_delay=%u",
  609. dialog_token, status_code, frag_id, more_frags,
  610. comeback_delay);
  611. /* TODO: check frag_id match */
  612. if (status_code) {
  613. p2p_dbg(p2p, "Service Discovery failed: status code %u",
  614. status_code);
  615. return;
  616. }
  617. if (*pos != WLAN_EID_ADV_PROTO) {
  618. p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
  619. *pos);
  620. return;
  621. }
  622. pos++;
  623. slen = *pos++;
  624. if (slen > end - pos || slen < 2) {
  625. p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
  626. return;
  627. }
  628. next = pos + slen;
  629. pos++; /* skip QueryRespLenLimit and PAME-BI */
  630. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  631. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  632. *pos);
  633. return;
  634. }
  635. pos = next;
  636. /* Query Response */
  637. if (end - pos < 2) {
  638. p2p_dbg(p2p, "Too short Query Response");
  639. return;
  640. }
  641. slen = WPA_GET_LE16(pos);
  642. pos += 2;
  643. p2p_dbg(p2p, "Query Response Length: %d", slen);
  644. if (slen > end - pos) {
  645. p2p_dbg(p2p, "Not enough Query Response data");
  646. return;
  647. }
  648. if (slen == 0) {
  649. p2p_dbg(p2p, "No Query Response data");
  650. return;
  651. }
  652. end = pos + slen;
  653. if (p2p->sd_rx_resp) {
  654. /*
  655. * ANQP header is only included in the first fragment; rest of
  656. * the fragments start with continue TLVs.
  657. */
  658. goto skip_nqp_header;
  659. }
  660. /* ANQP Query Response */
  661. if (end - pos < 4)
  662. return;
  663. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  664. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  665. return;
  666. }
  667. pos += 2;
  668. slen = WPA_GET_LE16(pos);
  669. pos += 2;
  670. p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
  671. if (slen < 3 + 1) {
  672. p2p_dbg(p2p, "Invalid ANQP Query Response length");
  673. return;
  674. }
  675. if (end - pos < 4)
  676. return;
  677. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  678. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  679. WPA_GET_BE32(pos));
  680. return;
  681. }
  682. pos += 4;
  683. if (end - pos < 2)
  684. return;
  685. p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
  686. p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
  687. pos += 2;
  688. skip_nqp_header:
  689. if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
  690. return;
  691. wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
  692. p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
  693. (unsigned int) wpabuf_len(p2p->sd_rx_resp));
  694. if (more_frags) {
  695. p2p_dbg(p2p, "More fragments remains");
  696. /* TODO: what would be a good size limit? */
  697. if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
  698. wpabuf_free(p2p->sd_rx_resp);
  699. p2p->sd_rx_resp = NULL;
  700. p2p_dbg(p2p, "Too long SD response - drop it");
  701. return;
  702. }
  703. p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
  704. return;
  705. }
  706. p2p->sd_peer = NULL;
  707. if (p2p->sd_query) {
  708. if (!p2p->sd_query->for_all_peers) {
  709. struct p2p_sd_query *q;
  710. p2p_dbg(p2p, "Remove completed SD query %p",
  711. p2p->sd_query);
  712. q = p2p->sd_query;
  713. p2p_unlink_sd_query(p2p, p2p->sd_query);
  714. p2p_free_sd_query(q);
  715. }
  716. p2p->sd_query = NULL;
  717. }
  718. if (p2p->cfg->sd_response)
  719. p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
  720. p2p->sd_rx_update_indic,
  721. wpabuf_head(p2p->sd_rx_resp),
  722. wpabuf_len(p2p->sd_rx_resp));
  723. wpabuf_free(p2p->sd_rx_resp);
  724. p2p->sd_rx_resp = NULL;
  725. p2p_continue_find(p2p);
  726. }
  727. void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
  728. const struct wpabuf *tlvs)
  729. {
  730. struct p2p_sd_query *q;
  731. q = os_zalloc(sizeof(*q));
  732. if (q == NULL)
  733. return NULL;
  734. if (dst)
  735. os_memcpy(q->peer, dst, ETH_ALEN);
  736. else
  737. q->for_all_peers = 1;
  738. q->tlvs = wpabuf_dup(tlvs);
  739. if (q->tlvs == NULL) {
  740. p2p_free_sd_query(q);
  741. return NULL;
  742. }
  743. q->next = p2p->sd_queries;
  744. p2p->sd_queries = q;
  745. p2p_dbg(p2p, "Added SD Query %p", q);
  746. if (dst == NULL) {
  747. struct p2p_device *dev;
  748. p2p->num_p2p_sd_queries++;
  749. /* Update all the devices for the newly added broadcast query */
  750. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  751. if (dev->sd_pending_bcast_queries <= 0)
  752. dev->sd_pending_bcast_queries = 1;
  753. else
  754. dev->sd_pending_bcast_queries++;
  755. }
  756. }
  757. return q;
  758. }
  759. #ifdef CONFIG_WIFI_DISPLAY
  760. void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
  761. const struct wpabuf *tlvs)
  762. {
  763. struct p2p_sd_query *q;
  764. q = p2p_sd_request(p2p, dst, tlvs);
  765. if (q)
  766. q->wsd = 1;
  767. return q;
  768. }
  769. #endif /* CONFIG_WIFI_DISPLAY */
  770. void p2p_sd_service_update(struct p2p_data *p2p)
  771. {
  772. p2p->srv_update_indic++;
  773. }
  774. int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
  775. {
  776. if (p2p_unlink_sd_query(p2p, req)) {
  777. p2p_dbg(p2p, "Cancel pending SD query %p", req);
  778. p2p_free_sd_query(req);
  779. return 0;
  780. }
  781. return -1;
  782. }