p2p_sd.c 21 KB

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